Skip to content

chatd: token-budget context windows instead of summary compaction #283

Closed

Description

@ibetitsmike

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, head 73708924b9, marked "do not merge". Behind chat-token-budget: two once-per-window nudges past the threshold, a chat-scoped notes tool 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, migration chat_notes, config route /api/experimental/chats/config/context-windows, admin setting UI.
  • Xum docs/workspaces/compaction/token-budget.md in 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_context honored once per window after sibling tools settle, a model-only lead-in that points at the checkpoint, session_history with a 16 KiB cap per call, rollover only after a tool step settles, atomic boundary save, manual /compact still 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 and forkdb queries, no numbered migrations.
  • Reuse what the fork already has where it fits: fork-chat-todos and read_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.md and coderd/x/chatd/ARCHITECTURE.md.

Activity

  1. added
    enhancement
    pstack-parity
    on Nov 13, 2026
  2. ibetitsmike commented on Nov 13, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    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

    • Tools. appendForkTools in coderd/x/chatd/fork_tools.go is the one place that adds the notes, chat_history and new_context tools. Plan-mode turns, explore chats and readonly custom types filter fork tools as they do today.
    • Nudges. syncForkContext in fork_context.go already 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.
    • Rollover. The upstream compaction flow (decideGenerationAction, generateCompaction, buildCompactionMessages) already writes the boundary rows and replays the pending user message in one CommitStep. A window boundary reuses it. A one-line hook in generateCompaction builds the lead-in instead of calling the summary model.
    • Reading old windows. read_chat_transcript (fork-chat-todos) already pages compacted messages through dbauthz. chat_history reuses 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 holds checkpoint.md and other notes, at most 20 notes of 16384 bytes each, and the rows cascade with the chat. The existing pieces do not fit:

    • chat_todos holds 50 items of 500 bytes, which is too small for a checkpoint.
    • Project memory is shared by the whole project, and subagent chats have no project.

    Schema

    One chatdmig node, chat_notes, requires the adopt node and the latest node on main. Columns are chat_id (FK to chats, on delete cascade), path, content (check length <= 16384), created_at and updated_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-budget has 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:

    • usable limit = limit minus min(8192, 25% of limit)
    • final point = max(usable minus 10192, half of usable)
    • handoff point = min(threshold, final point)
    • A threshold of 100% turns the budget off for that model and leaves summary compaction as on main. A threshold below 10% counts as 10%.

    Behavior

    • The handoff request is sent once per window, when the projected usage passes the handoff point. The final request is sent once, at the final point. A window that already starts over the handoff point gets no requests, so it cannot loop.
    • new_context takes 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.
    • A model that ignores the requests gets a forced rollover at the usable limit. This replaces the summary call for automatic compaction.
    • The next window starts with a model-only lead-in, <context_window id="w:N">. It lists the notes and points to checkpoint.md. It copies no text from the old window and needs no model call.
    • Manual /compact still summarizes.
    • chat_history offers list_windows, list_items, search and read_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 or new_context tools would lose its state at a rollover. Those are plan-mode turns, explore chats, 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.

    1. mike/issue-283-token-budget-notes: experiment, chat_notes node, forkdb queries, dbauthz, dbmetrics, the notes tool (list, read, write, append), tests, pstack-adapter rows.
    2. mike/issue-283-token-budget-history: the chat_history tool with windows, search, the 16 KiB cap, the clear floor, and tests.
    3. 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.
    4. mike/issue-283-token-budget-ui: a window divider in the chat timeline for new_context and context_limit boundaries, the final docs, and Closes #283.

    I start PR 1 now.

  3. ibetitsmike commented on Nov 14, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    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).

  4. ibetitsmike commented on Nov 14, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    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-budget stays off until then.

  5. ibetitsmike commented on Nov 14, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    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.

  6. ibetitsmike commented on Nov 14, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    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-budget stays off until then.

  7. ibetitsmike commented on Nov 14, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    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.

  8. ibetitsmike commented on Nov 14, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementpstack-parity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions