{"record":{"id":"097433fe4856862a","repo":"siyuan-note/siyuan","slug":"w-the-current-turn-is-too-large","errorCode":null,"errorMessage":"%w: the current turn is too large","messagePattern":"%w: the current turn is too large","errorType":"error_code","errorClass":"errContextCannotBeCompacted","httpStatus":null,"severity":"error","filePath":"kernel/agent/agent.go","lineNumber":784,"sourceCode":"\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)\n\t\t\tif len(candidates) == 0 {\n\t\t\t\treturn false, fmt.Errorf(\"%w: the current turn is too large\", errContextCannotBeCompacted)\n\t\t\t}\n\t\t\ttail, ok := currentTurnTail(checkpointMsgs, userEntryID, userMessage)\n\t\t\tif !ok {\n\t\t\t\treturn false, fmt.Errorf(\"%w: current user turn not found\", errContextCannotBeCompacted)\n\t\t\t}\n\n\t\t\tnativeResponsesCompaction := util.IsOpenAIResponsesProtocol(protocol) &&\n\t\t\t\tutil.SupportsOpenAIResponsesCompaction(ctx)\n\t\t\t// 原生 compact 无法限制输出长度，覆盖所有已完成轮次，避免 opaque window 返回后仍超出预算。\n\t\t\tselectedCandidateIndex := len(candidates) - 1\n\t\t\tif !nativeResponsesCompaction {\n\t\t\t\tselectedCandidateIndex = sort.Search(len(candidates), func(i int) bool {\n\t\t\t\t\tcandidate := candidates[i]\n\t\t\t\t\tcandidateCheckpointMsgs := checkpointMessagesAfterCompaction(sessionEntries, candidate, tail)\n\t\t\t\t\tcandidateMessages := checkpointMessagesToOpenAIWithSummary(\n\t\t\t\t\t\tcandidateCheckpointMsgs, language, capabilities, nil)\n\t\t\t\t\tcandidateMessages, _ = projectImageMessages(candidateMessages)\n\t\t\t\t\tbaseTokens := estimateProtocolRequestTokens(model, protocol, candidateMessages,","sourceCodeStart":766,"sourceCodeEnd":802,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/agent/agent.go#L766-L802","documentation":"After computing compaction candidates via compactionCandidateEntryCounts(sessionEntries, coveredEntryCount, userEntryID), an empty list means the only uncompacted entry is the current (in-flight) user turn, which cannot be summarized. compactContext then returns errContextCannotBeCompacted wrapped with \"the current turn is too large\". Compaction needs at least one completed entry beyond the current turn to work with.","triggerScenarios":"A single user turn (plus its tool results) alone exceeds the input budget: there are no prior session entries to compact away because previous ones are already covered by an existing compaction summary (coveredEntryCount), and the current turn cannot be dropped.","commonSituations":"Pasting a huge document or attaching many large tool outputs in one message; long-running tool calls in one turn generating enormous content; a session where all earlier turns were already summarized and only the giant current turn remains.","solutions":["Split the large request into multiple smaller user turns","Reduce the size of attachments/tool outputs (truncate pasted content, limit tool result sizes)","Start a new agent session instead of continuing a fully-compacted one","Increase the model's context window"],"exampleFix":"// before\nagent.Send(\"here is the full 2MB log: \" + hugeLog) // single turn exceeds budget\n// after\nagent.Send(\"Here are the relevant log excerpts:\")\nagent.Send(excerpt) // send a trimmed, focused excerpt instead","handlingStrategy":"validation","validationCode":"estimatedTurn := estimateProtocolRequestTokens(model, protocol, currentTurnMessages, nil, nil, nil)\nif estimatedTurn > contextInputBudget(contextLimit, maxCompletionTokens) {\n    return fmt.Errorf(\"current turn (~%d tokens) exceeds input budget (%d); split or trim it\",\n        estimatedTurn, contextInputBudget(contextLimit, maxCompletionTokens))\n}","typeGuard":null,"tryCatchPattern":"if err := agent.Send(ctx, msg); errors.Is(err, errContextCannotBeCompacted) &&\n    strings.Contains(err.Error(), \"the current turn is too large\") {\n    // trim/split the message or start a new session, then retry\n}","preventionTips":["Avoid pasting entire files/logs into one turn; send focused excerpts","Cap tool output sizes so a single turn cannot balloon","Start a new session once prior turns are mostly compacted","Pre-estimate turn size for known-large payloads before sending"],"tags":["agent","compaction","context-window","large-turn"],"backgroundTag":"payload-too-large","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"}