{"record":{"id":"115c124543a7d713","repo":"siyuan-note/siyuan","slug":"agent-context-cannot-be-compacted-enough-build-ru","errorCode":null,"errorMessage":"agent context cannot be compacted enough: build runtime state: %v","messagePattern":"agent context cannot be compacted enough: build runtime state: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/agent/agent.go","lineNumber":872,"sourceCode":"\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}\n\t\t\tif compactionStateErr != nil {\n\t\t\t\treturn false, fmt.Errorf(\n\t\t\t\t\t\"%w: build runtime state: %v\", errContextCannotBeCompacted, compactionStateErr)\n\t\t\t}\n\t\t\tnextMessages := checkpointMessagesToOpenAIWithSummary(\n\t\t\t\tselectedCheckpointMsgs, language, capabilities, nextCompaction)\n\t\t\testimatedNextMessages, _ := projectImageMessages(nextMessages)\n\t\t\tif estimateProtocolRequestTokens(model, protocol, estimatedNextMessages, selectedCheckpointMsgs,\n\t\t\t\tnextCompaction, requestTools) > inputBudget {\n\t\t\t\treturn false, fmt.Errorf(\"%w: compacted context still exceeds the input budget\", errContextCannotBeCompacted)\n\t\t\t}\n\t\t\tif err := saveRuntimeCompaction(sessionID, nextCompaction); err != nil {\n\t\t\t\treturn false, fmt.Errorf(\"%w: persist compaction: %v\", errContextCannotBeCompacted, err)\n\t\t\t}\n\t\t\tcompaction = nextCompaction\n\t\t\tcheckpointMsgs = selectedCheckpointMsgs\n\t\t\tmessages = nextMessages\n\t\t\treturn true, nil\n\t\t}\n","sourceCodeStart":854,"sourceCodeEnd":890,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/agent/agent.go#L854-L890","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the wrapped %v error to find the concrete cause and fix the matching input (entry counts, summary content, protocol support)","Verify the protocol supports summary compaction or switch to a supported protocol/model","Retry: if the model returned an empty/invalid summary, re-running compaction may produce a valid one","If the session entries were mutated concurrently, retry after the session is quiescent or start a new session"],"exampleFix":"// before\nsummary, err := createSummary(...) // empty summary on provider hiccup\nnext, err := newRuntimeProtocolSummaryCompaction(entries, n, summary, protocol) // fails: build runtime state\n// after\nif strings.TrimSpace(summary) == \"\" {\n    return retryOrReportEmptySummary() // avoid feeding an empty summary into runtime state\n}","handlingStrategy":"retry","validationCode":"if selectedEntryCount <= 0 || selectedEntryCount > len(sessionEntries) {\n    return fmt.Errorf(\"invalid selectedEntryCount %d for %d entries; refusing to build runtime compaction\",\n        selectedEntryCount, len(sessionEntries))\n}\nif strings.TrimSpace(summary) == \"\" {\n    return fmt.Errorf(\"refusing to build runtime compaction from an empty summary\")\n}","typeGuard":null,"tryCatchPattern":"if err := agent.Send(ctx, msg); errors.Is(err, errContextCannotBeCompacted) &&\n    strings.Contains(err.Error(), \"build runtime state:\") {\n    // inspect wrapped cause; retry once after fixing, or start a new session\n}","preventionTips":["Verify protocol support for summary compaction before relying on it","Retry compaction when the model returns an empty or truncated summary","Avoid mutating session entries while compaction is in flight","Read the wrapped %v cause to target the fix instead of blind retries"],"tags":["agent","compaction","summary","runtime-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"}