{"record":{"id":"43d7e1f88a55e9b3","repo":"siyuan-note/siyuan","slug":"agent-context-cannot-be-compacted-enough","errorCode":null,"errorMessage":"agent context cannot be compacted enough","messagePattern":"agent context cannot be compacted enough","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/agent/compaction.go","lineNumber":41,"sourceCode":"\t\"encoding/json\"\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"strings\"\n\t\"time\"\n\n\t\"github.com/sashabaranov/go-openai\"\n\t\"github.com/siyuan-note/siyuan/kernel/util\"\n)\n\nconst (\n\tcompactionVersion          = 1\n\tcompactionSummaryMinTokens = 256\n\tcompactionSummaryMaxTokens = 2048\n\tcompactionSummaryOverhead  = 128\n)\n\nvar errContextCannotBeCompacted = errors.New(\"agent context cannot be compacted enough\")\nvar errCompactionSummaryEmpty = errors.New(\"agent compaction summary is empty\")\n\nfunc isContextOverflow(err error) bool {\n\tmsg := err.Error()\n\treturn strings.Contains(msg, \"context_length_exceeded\") ||\n\t\tstrings.Contains(msg, \"maximum context length\") ||\n\t\tstrings.Contains(msg, \"reduce the length\") ||\n\t\tstrings.Contains(msg, \"too many tokens\") ||\n\t\tstrings.Contains(msg, \"input is too long\") ||\n\t\tstrings.Contains(msg, \"exceeds the context window\") ||\n\t\tstrings.Contains(msg, \"超出上下文\") ||\n\t\tstrings.Contains(msg, \"上下文长度\")\n}\n\nfunc cloneRuntimeCompaction(compaction *runtimeCompaction) *runtimeCompaction {\n\tif compaction == nil {\n\t\treturn nil\n\t}","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/agent/compaction.go#L23-L59","documentation":"errContextCannotBeCompacted signals that the agent's conversation context could not be shrunk below the model's input limit via compaction. It is returned by createProtocolCompactionSummary in two cases: the protocol cannot produce a summary at all, or (at agent.go:764) the model's context length is unknown so no compaction budget can be computed. The UI maps it (and context-overflow errors) to a localized message (Language 352).","triggerScenarios":"Running agent compaction when the model reports/implies an unknown context limit (no contextWindow metadata), or when the compaction protocol cannot reduce the context far enough given compactionSummaryMinTokens/MaxTokens budgets.","commonSituations":"Using a custom or newly released model whose context length is not in the models.dev catalog; extremely long conversations that cannot fit even after summarization; offline usage where the context catalog was never refreshed.","solutions":["Switch to or select a model whose context length is known (present in the context catalog)","Refresh the model context catalog (see refreshModelsDevContextCatalog) so contextLimit can be resolved","Manually trim the conversation: delete old messages/attachments or start a new session","Lower maxCompletionTokens so the input budget is larger if the limit is marginally exceeded"],"exampleFix":"// before: model without known context limit\nconf.Model = \"my-custom-model\" // contextWindow unknown\n// after\nconf.Model = \"gpt-4o\" // contextWindow known, budget computable","handlingStrategy":"fallback","validationCode":"limit, known := modelContextLimit(conf.Model)\nif !known { return errors.New(\"model context length unknown; choose a cataloged model before long sessions\") }","typeGuard":null,"tryCatchPattern":"if errors.Is(err, errContextCannotBeCompacted) || isContextOverflow(err) { showUserLocalizedMessage(kernelModel.Conf.Language(352)); return }","preventionTips":["Choose models present in the context catalog (known contextWindow)","Keep sessions below a conservative token budget rather than relying on compaction","Refresh the context catalog regularly so new models resolve limits","Cap maxCompletionTokens to leave a larger input budget"],"tags":["agent","compaction","context-window","llm"],"backgroundTag":"context-overflow","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}