siyuan-note/siyuan · error
agent context cannot be compacted enough: build summary sour
Error message
agent context cannot be compacted enough: build summary source: %v
What it means
When no native protocol compaction is available (nextCompaction == nil), the kernel builds the summary source with buildCompactionSource(previousSummary, sourceMessages). If that fails, compactContext returns errContextCannotBeCompacted wrapping the underlying error as "build summary source: %v". The summarizer could not assemble a valid prompt source from the previous summary and the selected source messages.
Source
Thrown at kernel/agent/agent.go:852
}
responseOutput, promptTokens, completionTokens, compactErr := createResponseCompaction(
ctx, client, compactRequest, responseInput, maxRetries, requestTimeout, ch)
totalPrompt += promptTokens
totalCompletion += completionTokens
if compactErr == nil {
nextCompaction, compactionStateErr = newRuntimeResponseCompaction(
sessionEntries, selectedEntryCount, responseOutput,
compactionOutputTokenCost(model, responseOutput, completionTokens))
} else if compaction != nil && len(compaction.ResponseOutput) > 0 {
return false, compactErr
} else {
logging.LogWarnf("responses compaction failed, fallback to summary: %s", compactErr)
}
}
if nextCompaction == nil {
source, sourceErr := buildCompactionSource(previousSummary, sourceMessages)
if sourceErr != nil {
return false, fmt.Errorf("%w: build summary source: %v", errContextCannotBeCompacted, sourceErr)
}
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)
}View on GitHub (pinned to 8641553a1f)
Solutions
- Inspect the wrapped %v error to see why buildCompactionSource failed and fix the offending session content
- Repair or discard the corrupted session history; start a new agent session
- Ensure tools/plugins emit well-formed message content
- Retry the turn after fixing; if transient, re-sending may rebuild valid source messages
Example fix
// before // session history contains a tool message with nil content after a crash // after // restore a consistent session or drop the malformed entry so sourceMessages is renderable
Defensive patterns
Strategy: try-catch
Validate before calling
if len(sourceMessages) == 0 && previousSummary == "" {
return fmt.Errorf("nothing to compact: no previous summary and no selected source messages")
} Try / catch
if err := agent.Send(ctx, msg); errors.Is(err, errContextCannotBeCompacted) &&
strings.Contains(err.Error(), "build summary source:") {
log.Warnf("compaction source invalid: %v", err) // fix session content or start a new session
} Prevention
- Keep session history well-formed; do not hand-edit .sy/session files
- Ensure tools and plugins always emit valid message content (never nil/empty parts)
- Restore sessions only from complete, consistent backups
- Log and inspect the wrapped cause rather than retrying blindly
When it happens
Trigger: buildCompactionSource returns an error — e.g. both previousSummary and sourceMessages are empty/invalid, or the selected messages cannot be rendered into summary source text (malformed message content in the session history).
Common situations: Corrupted or hand-edited session history with malformed message content; a previous compaction summary that is missing/empty while no messages were selected; unusual content (e.g. nil or wrong-typed message parts) produced by a plugin or tool.
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
- agent context cannot be compacted enough: current user turn
- agent context cannot be compacted enough: build runtime stat
- %w: model context length is unknown
- %w: no input budget remains
- %w: the current turn is too large
AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11).
Data as JSON: /api/errors/3bfe7686e7de90e6.
Report an issue: GitHub.