siyuan-note/siyuan · error

agent context cannot be compacted enough: persist compaction

Error message

agent context cannot be compacted enough: persist compaction: %v

What it means

This wraps errContextCannotBeCompacted with the underlying persistence failure when saveRuntimeCompaction cannot store the newly computed compaction state for the session. The compaction itself was valid (it fit the budget), but the runtime could not be saved, so the compaction cannot proceed safely.

Source

Thrown at kernel/agent/agent.go:883

				if summaryErr != nil {
					return false, summaryErr
				}
				nextCompaction, compactionStateErr = newRuntimeProtocolSummaryCompaction(
					sessionEntries, selectedEntryCount, summary, protocol)
			}
			if compactionStateErr != nil {
				return false, fmt.Errorf(
					"%w: build runtime state: %v", errContextCannotBeCompacted, compactionStateErr)
			}
			nextMessages := checkpointMessagesToOpenAIWithSummary(
				selectedCheckpointMsgs, language, capabilities, nextCompaction)
			estimatedNextMessages, _ := projectImageMessages(nextMessages)
			if estimateProtocolRequestTokens(model, protocol, estimatedNextMessages, selectedCheckpointMsgs,
				nextCompaction, requestTools) > inputBudget {
				return false, fmt.Errorf("%w: compacted context still exceeds the input budget", errContextCannotBeCompacted)
			}
			if err := saveRuntimeCompaction(sessionID, nextCompaction); err != nil {
				return false, fmt.Errorf("%w: persist compaction: %v", errContextCannotBeCompacted, err)
			}
			compaction = nextCompaction
			checkpointMsgs = selectedCheckpointMsgs
			messages = nextMessages
			return true, nil
		}

		for {
			select {
			case <-ctx.Done():
				return
			default:
			}

			roundID := fmt.Sprintf("%s_%d", turn.TurnID, modelRound)
			if modelRound == 0 {
				sendCriticalEvent(ctx, ch, AgentEvent{Type: "thinking", Reasoning: "analyzing", RoundID: roundID})
			} else {

View on GitHub (pinned to 8641553a1f)

Solutions

  1. Inspect the wrapped cause (%v) to identify the storage failure
  2. Check disk space and write permissions on the workspace data directory
  3. Verify the session still exists and is not locked/being modified concurrently
  4. Retry the request; if persistence keeps failing, start a new session
  5. Check application logs around saveRuntimeCompaction for the underlying error
Defensive patterns

Strategy: try-catch

Try / catch

if errors.Is(err, errContextCannotBeCompacted) && strings.Contains(err.Error(), "persist compaction") {
    log.Warnf("compaction persistence failed: %v", err)
    // retry later or fall back to a new session
}

Prevention

When it happens

Trigger: Inside the compaction loop, after the token check passes, saveRuntimeCompaction(sessionID, nextCompaction) returns a non-nil error (e.g. storage/IO or serialization failure for the session's runtime state), producing "persist compaction: <cause>".

Common situations: Disk full or unwritable workspace data directory; corrupted or locked session state store; session removed concurrently by another process; database write failure while persisting compaction.

Related errors


AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11). Data as JSON: /api/errors/8fbaa35067113aa0. Report an issue: GitHub.