Skip to content

feat(coderd/x/chatd): start token-budget context windows behind fork-token-budget - #314

Merged
ibetitsmike merged 4 commits into
mainfrom
mike/issue-283-token-budget-windows
Nov 14, 2026
Merged

ibetitsmike merged 4 commits into
mainfrom
mike/issue-283-token-budget-windows

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part of #283

With the fork-token-budget experiment on, an eligible turn no longer summarizes its conversation when the context fills up. It works in token-budget windows. The model keeps its state in notes and starts a fresh window from them. chat_history (#313) still reads the earlier windows.

What changed

  • The ladder of a window, newTokenBudget in fork_token_budget.go, from the model's context limit and the compression threshold that already applies (per-model value or the user's override):
    • usable = limit - min(8192, limit/4)
    • final = max(usable - 10192, usable/2)
    • handoff = min(threshold, final), with the threshold floored at 10%
    • a threshold of 100% turns the ladder off, as it turns compaction off
  • Before each step, syncForkContext may commit one model-only <context_budget> request: the handoff request once per window at the handoff point, and the final request once per window at the final point. A window whose first prompt is already past the handoff point, or whose new_context call succeeded, gets none.
  • New tool new_context. It takes no arguments, runs serially after the other calls of its step, and refuses until a note changed in the current window.
  • tokenBudgetTriggers (hook in turn_environment.go) makes compaction required when new_context succeeded in the window or the last prompt reached the usable limit. The other automatic triggers do not apply. The binding trigger becomes the usable percent, so the existing still-over-limit guard stops a loop of rollovers.
  • forkGenerateCompaction (hook in generation.go) sends an automatic compaction of an eligible turn to contextWindowRollover. It makes no model call. Its summary is the lead-in <context_window id="w:N">, which lists the notes, points at checkpoint.md and carries the model context of a pre_compact hook. The boundary rows and the chat_summarized part are unchanged, so chat_history lists and reads the earlier windows.
  • Unanswered tool calls run before any compaction (decideGenerationAction), so a rollover never splits a tool call from its result.
  • Manual /compact still summarizes.

Subagents and restricted turns

A turn is eligible only when it keeps notes, chat_history and new_context, so the lead-in never points at a missing tool. Plan mode, explore children and read-only or allowlisted custom agent types drop those tools, so they keep summary compaction. tokenBudgetMessage applies the same check before the tools exist, so those chats get no requests either. A general subagent chat has its own notes and its own windows; the parent's windows do not affect it.

Experiment off

With fork-token-budget off, tokenBudgetEligible is false, the triggers come back unchanged, forkGenerateCompaction calls chatloop.GenerateCompaction as before, no request is sent and new_context is not listed. TestTokenBudgetWindows/ExperimentOff proves the summary path still runs.

Tests

TestTokenBudgetWindows runs a chat with a small context limit:

  • Cooperative: the handoff request goes out once, the model writes the checkpoint and calls new_context, the next window starts with the lead-in and no summary call, and chat_history reads a message from the earlier window.
  • ForcedRollover: the chat ignores the handoff and final requests, each sent once. The prompt that reaches the usable limit rolls the window over without a new_context call, and its tool call and result both stay in the first window.
  • PlanModeSummarizes: plan mode keeps summary compaction.
  • ExperimentOff: summary compaction as on main.

Unit tests cover the ladder, the request schedule, the triggers and the eligibility.

Run locally, all passing:

  • flock ~/.fork-go.lock go build ./coderd/...
  • flock ~/.fork-go.lock go test -count=1 ./coderd/x/chatd/...
  • flock ~/.fork-go.lock go test -count=1 ./codersdk/
  • flock ~/.fork-go.lock golangci-lint run --config=.golangci.new-code.yaml --new-from-merge-base=origin/main ./coderd/x/chatd/ (0 issues)
  • make lint/emdash lint/markdown lint/architecture lint/agents

Not verified: a live model on a deployed server. The UI divider for a window boundary comes in the next PR.

Docs: coderd/x/chatd/ARCHITECTURE.md and docs/fork/pstack-adapter.md.

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T08:14:38.076577Z 44daaea Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f6fa6ff5de

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/generation.go Resolved
Comment thread coderd/x/chatd/fork_token_budget.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1cd3f6cdf3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/fork_token_budget.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 82a5a6d779

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/fork_token_budget.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 44daaea52c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike merged commit 67c143c into main Nov 14, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-283-token-budget-windows branch November 14, 2026 08:26
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged as 67c143c (squash) at 08:26Z. Before the merge, the server was confirmed on the previous deploy, 6ae5568.

Gates on head 44daaea:

  1. The head matched the SHA I verified, and main was 6ae5568 at merge time.
  2. All check runs passed. fork-ci run 43574548857 passed on this exact head.
  3. Codex receipt: issuecomment-6874385183, "Didn't find any major issues", reviewed commit 44daaea. Codex raised four P2s on earlier heads. Two were fixed. Two were dropped with reasons: the override failure is the same on main, and the rounding is defensive.
  4. 0 unresolved threads.
  5. The branch was 0 commits behind main. The diff against main was 11 files, +1218 -3.
  6. My tests on the exact head, run under flock:
    • go test -count=1 -v -run '(TestTokenBudget|TestNewContextTool|TestContextWindow|TestChatNotesTool|TestChatHistoryTool|TestForkMessageAgentReplay|TestViewImage|CompactionModelOverride)' ./coderd/x/chatd/: 18 top-level tests passed.
    • go test -count=1 -run '(Compact)' ./coderd/x/chatd/: passed.
    • I also reviewed the hooks in upstream files. Each is one line. forkGenerateCompaction and tokenBudgetTriggers fall back to the upstream path unless tokenBudgetEligible, which needs the new_context tool that only fork-token-budget adds.

Not exercised: a live chat with a real model rolling over windows on a deployed server, and a real pre_compact hook. The tests use fake models. fork-token-budget stays off.

ibetitsmike added a commit that referenced this pull request Nov 14, 2026
* feat(coderd/x/chatd): add organization token budget setting

The fork-token-budget experiment now reads a per-organization setting.
The summary strategy, the default, keeps main's summary compaction with
notes and chat_history. The token_budget strategy keeps the windows of
#314, and its fallback picks a new window or a summary at the usable
limit.

* fix(coderd/x/chatd): keep the token budget fallback with the turn's tools

The new_context tool holds the fallback of the step that built it, so triggers and compaction follow one setting and a foreign new_context tool does not make a turn eligible. With the summary fallback, the compaction model override's trigger still applies.
ibetitsmike added a commit that referenced this pull request Nov 14, 2026
* feat(coderd/x/chatd): add organization token budget setting

The fork-token-budget experiment now reads a per-organization setting.
The summary strategy, the default, keeps main's summary compaction with
notes and chat_history. The token_budget strategy keeps the windows of
#314, and its fallback picks a new window or a summary at the usable
limit.

* feat(site): add the organization token budget setting

* fix(coderd/x/chatd): keep the token budget fallback with the turn's tools

The new_context tool holds the fallback of the step that built it, so triggers and compaction follow one setting and a foreign new_context tool does not make a turn eligible. With the summary fallback, the compaction model override's trigger still applies.

* fix(site): announce token budget save errors and name user thresholds

* fix(site): describe token budget options to screen readers and keep saves

* fix(site): scope token budget form ids and tighten its stories, tests and docs

* fix(site): document token budget section contracts and show a failed save after an edit

* test(site): cover token budget read-only, hidden and fallback gate states

* fix(site): drop repeated token budget comments and the disabled label pointer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant