{"record":{"id":"3bfe7686e7de90e6","repo":"siyuan-note/siyuan","slug":"agent-context-cannot-be-compacted-enough-build-su","errorCode":null,"errorMessage":"agent context cannot be compacted enough: build summary source: %v","messagePattern":"agent context cannot be compacted enough: build summary source: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/agent/agent.go","lineNumber":852,"sourceCode":"\t\t\t\t}\n\t\t\t\tresponseOutput, promptTokens, completionTokens, compactErr := createResponseCompaction(\n\t\t\t\t\tctx, client, compactRequest, responseInput, maxRetries, requestTimeout, ch)\n\t\t\t\ttotalPrompt += promptTokens\n\t\t\t\ttotalCompletion += completionTokens\n\t\t\t\tif compactErr == nil {\n\t\t\t\t\tnextCompaction, compactionStateErr = newRuntimeResponseCompaction(\n\t\t\t\t\t\tsessionEntries, selectedEntryCount, responseOutput,\n\t\t\t\t\t\tcompactionOutputTokenCost(model, responseOutput, completionTokens))\n\t\t\t\t} else if compaction != nil && len(compaction.ResponseOutput) > 0 {\n\t\t\t\t\treturn false, compactErr\n\t\t\t\t} else {\n\t\t\t\t\tlogging.LogWarnf(\"responses compaction failed, fallback to summary: %s\", compactErr)\n\t\t\t\t}\n\t\t\t}\n\t\t\tif nextCompaction == nil {\n\t\t\t\tsource, sourceErr := buildCompactionSource(previousSummary, sourceMessages)\n\t\t\t\tif sourceErr != nil {\n\t\t\t\t\treturn false, fmt.Errorf(\"%w: build summary source: %v\", errContextCannotBeCompacted, sourceErr)\n\t\t\t\t}\n\t\t\t\tsummaryRequestMessages := compactionSummaryMessages(source)\n\t\t\t\tsummaryInputBudget := contextInputBudget(contextLimit, summaryMaxTokens)\n\t\t\t\tif summaryInputBudget <= 0 ||\n\t\t\t\t\testimateChatRequestTokens(model, summaryRequestMessages, nil) > summaryInputBudget {\n\t\t\t\t\treturn false, fmt.Errorf(\"%w: summary input exceeds the model context\", errContextCannotBeCompacted)\n\t\t\t\t}\n\t\t\t\tsummary, promptTokens, completionTokens, summaryErr := createProtocolCompactionSummary(\n\t\t\t\t\tctx, client, protocol, model, source, summaryMaxTokens, maxRetries, requestTimeout,\n\t\t\t\t\tstreamIdleTimeout, ch)\n\t\t\t\ttotalPrompt += promptTokens\n\t\t\t\ttotalCompletion += completionTokens\n\t\t\t\tif summaryErr != nil {\n\t\t\t\t\treturn false, summaryErr\n\t\t\t\t}\n\t\t\t\tnextCompaction, compactionStateErr = newRuntimeProtocolSummaryCompaction(\n\t\t\t\t\tsessionEntries, selectedEntryCount, summary, protocol)\n\t\t\t}","sourceCodeStart":834,"sourceCodeEnd":870,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/agent/agent.go#L834-L870","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\n// session history contains a tool message with nil content after a crash\n// after\n// restore a consistent session or drop the malformed entry so sourceMessages is renderable","handlingStrategy":"try-catch","validationCode":"if len(sourceMessages) == 0 && previousSummary == \"\" {\n    return fmt.Errorf(\"nothing to compact: no previous summary and no selected source messages\")\n}","typeGuard":null,"tryCatchPattern":"if err := agent.Send(ctx, msg); errors.Is(err, errContextCannotBeCompacted) &&\n    strings.Contains(err.Error(), \"build summary source:\") {\n    log.Warnf(\"compaction source invalid: %v\", err) // fix session content or start a new session\n}","preventionTips":["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"],"tags":["agent","compaction","summary","session-state"],"backgroundTag":"internal-invariant-violation","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}