Repository navigation
Closed
Labels
Description
Coder Agents created this issue on behalf of @ibetitsmike.
Part of #4. Mike asked for this on 2026-11-13: add token-budget compaction to the fork, as Xum has it.
Gap
Coder Agents compacts a long chat by summarizing it when usage passes the model's compaction threshold. The summary is lossy, and a lead or workstream chat that runs for days loses decisions and IDs at each compaction. Xum's Token Budget strategy replaces the automatic summary with fresh context windows: the agent is asked to save a checkpoint and start a new window itself, rollover is forced only near the usable limit, and earlier windows stay retrievable on demand.
References (read only; upstream is a sync source, nothing is filed there)
- Upstream prototype feat: prototype token-budget context windows for Coder Agents (do not merge) coder/coder#31191, branch
tk/token-budget-prototype, head73708924b9, marked "do not merge". Behindchat-token-budget: two once-per-window nudges past the threshold, a chat-scopednotestool for the checkpoint,new_context,chat_history(list_windows, list_items, search, read_item), a forced point (90% by default) with a deployment fallback (summary or new window), window boundaries reusing the compaction rows, migrationchat_notes, config route/api/experimental/chats/config/context-windows, admin setting UI. - Xum
docs/workspaces/compaction/token-budget.mdin coder/xum: handoff target from the usage slider, usable limit = context window minus an output reserve, handoff request and final prompt once per window,new_contexthonored once per window after sibling tools settle, a model-only lead-in that points at the checkpoint,session_historywith a 16 KiB cap per call, rollover only after a tool step settles, atomic boundary save, manual/compactstill summarizes, 100% disables rollover.
Wanted
- Token-budget context windows behind a new experiment
fork-token-budget, with the fork split rules from pstack parity: tracking #4: fork files, one hook line per upstream file, chatdmig nodes andforkdbqueries, no numbered migrations. - Reuse what the fork already has where it fits:
fork-chat-todosandread_chat_transcript, project memory, compaction rows. - A design note and PR split before the bulk of the code, posted on this issue.
Acceptance
- In a test with a small context limit, a chat passes the handoff target, gets the handoff request once, saves a checkpoint, calls
new_context, and the next window starts with the lead-in and no summary call. Ignoring the requests forces the rollover at the usable limit. - The next window can find and read a message from an earlier window.
- With the experiment off, compaction behaves as on
main. - Docs in
docs/fork/pstack-adapter.mdandcoderd/x/chatd/ARCHITECTURE.md.
Activity
ibetitsmike commentedon Nov 13, 2026
Design note and PR split for token-budget context windows
The target is Xum's token budget (docs/workspaces/compaction/token-budget.md, ADR 0005 and 0007), built on the upstream prototype coder#31191 and on fork pieces that already exist.
What maps to existing fork pieces
appendForkToolsincoderd/x/chatd/fork_tools.gois the one place that adds the notes,chat_historyandnew_contexttools. Plan-mode turns,explorechats andreadonlycustom types filter fork tools as they do today.syncForkContextinfork_context.goalready commits one model-only message per step and waits while a step has unresolved tool calls. The handoff and final requests use the same path, so a request never lands between a tool call and its result.decideGenerationAction,generateCompaction,buildCompactionMessages) already writes the boundary rows and replays the pending user message in oneCommitStep. A window boundary reuses it. A one-line hook ingenerateCompactionbuilds the lead-in instead of calling the summary model.read_chat_transcript(fork-chat-todos) already pages compacted messages through dbauthz.chat_historyreuses its loading and size caps, and adds windows and search.Checkpoint storage
A new chat-scoped notes table,
chatd.chat_notes, keyed by(chat_id, path). It holdscheckpoint.mdand other notes, at most 20 notes of 16384 bytes each, and the rows cascade with the chat. The existing pieces do not fit:chat_todosholds 50 items of 500 bytes, which is too small for a checkpoint.Schema
One chatdmig node,
chat_notes, requires the adopt node and the latest node on main. Columns arechat_id(FK to chats, on delete cascade),path,content(check length <= 16384),created_atandupdated_at, with primary key(chat_id, path). It uses forkdb queries with dbauthz wrappers: a read authorizes through the chat, and a write needs update on the chat. There is no numbered migration.Experiment and config surface
The new experiment
fork-token-budgethas display name "Fork Token Budget". With it off, compaction runs as on main, and a test pins that.There is no new deployment setting. The handoff target is the per-model compression threshold that exists today, with the per-user override on top. The rest is derived as in Xum:
Behavior
new_contexttakes no arguments. It refuses until a note changed in the current window. When it succeeds, the boundary is committed after its sibling tool calls settle, through the normal compaction decision. A rollover can therefore never split a tool call from its result.<context_window id="w:N">. It lists the notes and points tocheckpoint.md. It copies no text from the old window and needs no model call./compactstill summarizes.chat_historyofferslist_windows,list_items,searchandread_item, with 16 KiB per call. It never reaches past a chat clear. The new window uses it to find and read a message from an earlier window.Subagents
Each chat has its own windows and its own notes, keyed by
chat_id, so a subagent keeps its own checkpoint, as in Xum. A turn that lacks the notes ornew_contexttools would lose its state at a rollover. Those are plan-mode turns,explorechats, and custom types that drop the tools. Such a turn keeps summary compaction as on main and gets no requests.PR stack
Each PR has about 500 changed lines, not counting generated code. They land one at a time, each based on main after the previous one merges.
mike/issue-283-token-budget-notes: experiment,chat_notesnode, forkdb queries, dbauthz, dbmetrics, thenotestool (list, read, write, append), tests, pstack-adapter rows.mike/issue-283-token-budget-history: thechat_historytool with windows, search, the 16 KiB cap, the clear floor, and tests.mike/issue-283-token-budget-windows: budget ladder, handoff and final requests,new_context, the window boundary and lead-in, the forced rollover, a small-context-limit end-to-end test, an experiment-off test, and ARCHITECTURE.md text.mike/issue-283-token-budget-ui: a window divider in the chat timeline fornew_contextandcontext_limitboundaries, the final docs, andCloses #283.I start PR 1 now.
ibetitsmike commentedon Nov 14, 2026
Part 1 (#285, notes tool behind
fork-token-budget) merged as c7efc9c. The history and windows PRs come next, one per deploy. Follow-up to track: a replayed notes call can append the same note twice after a crash (dropped P2 on #285, same class as #287).ibetitsmike commentedon Nov 14, 2026
Progress: PR 2 (
chat_history, #313) merged as 9c4c333 at 07:39Z, after #285 (notes, c7efc9c). Next up are PR 3 (context windows), rebuilt on main, and a small PR that fixes the #285 P2 about a replayed notes append. The UI PR comes last and closes this issue.fork-token-budgetstays off until then.ibetitsmike commentedon Nov 14, 2026
Progress: #315 merged as 6ae5568 at 08:08Z. It fixes the #285 P2: a replayed notes call no longer appends twice. Still open: #314 (context windows), then the UI PR.
ibetitsmike commentedon Nov 14, 2026
Progress: #314 (context windows) merged as 67c143c at 08:26Z. The backend is complete: notes (#285, #315), chat_history (#313) and windows (#314). Next is the UI window divider PR, which closes this issue.
fork-token-budgetstays off until then.ibetitsmike commentedon Nov 14, 2026
Done. The token-budget stack landed: #285 notes (c7efc9c), #313 chat_history (9c4c333), #315 notes replay fix (6ae5568), #314 context windows (67c143c) and #316 UI window divider (5d0cc97). Everything sits behind
fork-token-budget, which is off by default. Enabling it on the daily server is an ops step. Not exercised: a live rollover with a real model on a deployed server.ibetitsmike commentedon Nov 14, 2026
Follow-up: #321 adds the admin setting that this issue left out (strategy Summary or Token budget, and the forced rollover fallback). Mike looked for the option on the daily server on 2026-11-14 and found none.