{"record":{"id":"56e83485df579413","repo":"siyuan-note/siyuan","slug":"errcontextcannotbecompacted","errorCode":"errContextCannotBeCompacted","errorMessage":"%w: model context length is unknown","messagePattern":"%w: model context length is unknown","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/agent/agent.go","lineNumber":767,"sourceCode":"\t\t\tif imageInputDisabled {\n\t\t\t\treturn downgradeImageInput(source)\n\t\t\t}\n\t\t\treturn source, false\n\t\t}\n\t\tcompactionErrorMessage := func(err error) string {\n\t\t\tif errors.Is(err, errContextCannotBeCompacted) || isContextOverflow(err) {\n\t\t\t\treturn kernelModel.Conf.Language(352)\n\t\t\t}\n\t\t\trequestMessages, _ := projectImageMessages(messages)\n\t\t\treturn getAgentRequestErrorMessage(err, requestMessages)\n\t\t}\n\n\t\tcompactContext := func(requestTools []openai.Tool, force bool) (bool, error) {\n\t\t\tif contextLimit <= 0 {\n\t\t\t\tif !force {\n\t\t\t\t\treturn false, nil\n\t\t\t\t}\n\t\t\t\treturn false, fmt.Errorf(\"%w: model context length is unknown\", errContextCannotBeCompacted)\n\t\t\t}\n\t\t\tinputBudget := contextInputBudget(contextLimit, maxCompletionTokens)\n\t\t\tif inputBudget <= 0 {\n\t\t\t\treturn false, fmt.Errorf(\"%w: no input budget remains\", errContextCannotBeCompacted)\n\t\t\t}\n\t\t\testimatedMessages, _ := projectImageMessages(messages)\n\t\t\tif !force && estimateProtocolRequestTokens(model, protocol, estimatedMessages, checkpointMsgs,\n\t\t\t\tcompaction, requestTools) <= inputBudget {\n\t\t\t\treturn false, nil\n\t\t\t}\n\n\t\t\tcoveredEntryCount := 0\n\t\t\tpreviousSummary := \"\"\n\t\t\tif compaction != nil {\n\t\t\t\tcoveredEntryCount = compaction.CoveredEntryCount\n\t\t\t\tpreviousSummary = compaction.Summary\n\t\t\t}\n\t\t\tcandidates := compactionCandidateEntryCounts(sessionEntries, coveredEntryCount, userEntryID)","sourceCodeStart":749,"sourceCodeEnd":785,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/agent/agent.go#L749-L785","documentation":"The agent's context compaction routine wraps errContextCannotBeCompacted and reports that the model's context length is unknown (contextLimit <= 0), so it cannot compute an input budget or decide what to compact. When force is false it silently skips compaction; when force is true it must fail loudly because compaction is impossible.","triggerScenarios":"compactContext(requestTools, force=true) runs while contextLimit <= 0 — i.e. the model metadata lookup returned no context window (unknown/0/missing model info) and compaction was explicitly requested (e.g. after a context-length API error or manual compaction).","commonSituations":"Using a model whose context size is not in the provider/model registry or config; a custom OpenAI-compatible endpoint that omits context_length; renamed model IDs the size table does not recognize.","solutions":["Configure the model's context length explicitly (model config/metadata) so contextLimit > 0","Use a model name the provider registry recognizes so its context window can be resolved","Update model metadata/config if the provider recently renamed or added the model","If compaction was auto-forced after a context error, verify the endpoint reports token usage/limits correctly"],"exampleFix":"// before: unknown context size for custom model\nmodel := \"my-finetune-v2\" // no context length known\n// after: declare the context window in the model config\nmodelCfg.ContextLength = 128000","handlingStrategy":"validation","validationCode":"if model.ContextLength <= 0 {\n    return fmt.Errorf(\"model %q has no configured context length; set it before enabling forced compaction\", model.Name)\n}","typeGuard":"func hasKnownContext(limit int) bool { return limit > 0 }","tryCatchPattern":"if err := agent.Compact(ctx, true); err != nil {\n    if errors.Is(err, errContextCannotBeCompacted) && strings.Contains(err.Error(), \"context length is unknown\") {\n        // configure model context length and retry, or skip compaction\n    }\n}","preventionTips":["Always configure context length for custom/finetuned models","Keep model metadata tables updated for renamed models","Validate model config at agent startup","Only force compaction when the context limit is known"],"tags":["agent","llm","context-window","compaction"],"backgroundTag":"missing-config-value","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}