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

  1. Inspect the wrapped %v error to see why buildCompactionSource failed and fix the offending session content
  2. Repair or discard the corrupted session history; start a new agent session
  3. Ensure tools/plugins emit well-formed message content
  4. 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

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


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