siyuan-note/siyuan · error

agent context cannot be compacted enough: build runtime stat

Error message

agent context cannot be compacted enough: build runtime state: %v

What it means

After generating the summary, newRuntimeProtocolSummaryCompaction(sessionEntries, selectedEntryCount, summary, protocol) builds the runtime compaction state to apply to the session. If it errors, compactContext returns errContextCannotBeCompacted wrapping it as "build runtime state: %v". The summary succeeded, but it could not be converted into a valid runtime compaction record for the session/protocol.

Source

Thrown at kernel/agent/agent.go:872

				summaryRequestMessages := compactionSummaryMessages(source)
				summaryInputBudget := contextInputBudget(contextLimit, summaryMaxTokens)
				if summaryInputBudget <= 0 ||
					estimateChatRequestTokens(model, summaryRequestMessages, nil) > summaryInputBudget {
					return false, fmt.Errorf("%w: summary input exceeds the model context", errContextCannotBeCompacted)
				}
				summary, promptTokens, completionTokens, summaryErr := createProtocolCompactionSummary(
					ctx, client, protocol, model, source, summaryMaxTokens, maxRetries, requestTimeout,
					streamIdleTimeout, ch)
				totalPrompt += promptTokens
				totalCompletion += completionTokens
				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
		}

View on GitHub (pinned to 8641553a1f)

Solutions

  1. Read the wrapped %v error to find the concrete cause and fix the matching input (entry counts, summary content, protocol support)
  2. Verify the protocol supports summary compaction or switch to a supported protocol/model
  3. Retry: if the model returned an empty/invalid summary, re-running compaction may produce a valid one
  4. If the session entries were mutated concurrently, retry after the session is quiescent or start a new session

Example fix

// before
summary, err := createSummary(...) // empty summary on provider hiccup
next, err := newRuntimeProtocolSummaryCompaction(entries, n, summary, protocol) // fails: build runtime state
// after
if strings.TrimSpace(summary) == "" {
    return retryOrReportEmptySummary() // avoid feeding an empty summary into runtime state
}
Defensive patterns

Strategy: retry

Validate before calling

if selectedEntryCount <= 0 || selectedEntryCount > len(sessionEntries) {
    return fmt.Errorf("invalid selectedEntryCount %d for %d entries; refusing to build runtime compaction",
        selectedEntryCount, len(sessionEntries))
}
if strings.TrimSpace(summary) == "" {
    return fmt.Errorf("refusing to build runtime compaction from an empty summary")
}

Try / catch

if err := agent.Send(ctx, msg); errors.Is(err, errContextCannotBeCompacted) &&
    strings.Contains(err.Error(), "build runtime state:") {
    // inspect wrapped cause; retry once after fixing, or start a new session
}

Prevention

When it happens

Trigger: newRuntimeProtocolSummaryCompaction fails — e.g. selectedEntryCount does not match the session entries, the summary is empty/invalid, or the protocol does not support the required summary-compaction representation.

Common situations: Protocol-specific limitations (e.g. a protocol where the summary record cannot represent covered entries); session entries mutated between selection and state build; an empty summary returned by the model despite a successful call.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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