Repository navigation
feat(coderd/x/chatd): add organization token budget setting - #330
Conversation
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.
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8930e21831
ℹ️ 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".
…ools 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.
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3047c90be5
ℹ️ 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".
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3047c90be5
ℹ️ 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".
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Merged as 9308bf6 (squash). Part of #321; the UI in #331 closes it. Gates on head
Tests I ran on the exact head, in a detached worktree:
Not exercised: a live deployment with |
Part of #321. This is the API and chatd half. The settings UI follows in a stacked PR that closes the issue.
What
An organization setting picks how chats compact their context when
fork-token-budgetis on.strategy:summary(default) ortoken_budget.fallback:new_window(default, the feat(coderd/x/chatd): start token-budget context windows behind fork-token-budget #314 forced rollover) orsummary. It applies only withtoken_budget.API:
GETandPUT /api/v2/chats/fork/organizations/{organization}/token-budget, behindhttpmw.RequireExperiment(..., ExperimentForkTokenBudget). It followsForkSubagentLimits: codersdk types and client methods, dbauthz onrbac.ResourceChatModelConfig.InOrg, read returns 404 when not readable, update returns 403 when readable but not updatable, 400 on unknown values. With no row the API returns the defaults.Storage: a chatdmig node creates
chat_organization_token_budget(org PK, CHECK constraints on both columns). Queries live inforkdb. No numbered migration.Why organization scope
Chats belong to an organization, and the neighbouring settings (subagent limits, wake-up caps) are organization settings on the same page. A per-org row also keeps the read keyed by the chat's own organization.
chatd behavior
summarystrategy equals themainpath. Nonew_contexttool, no<context_budget>handoff or final requests, and upstream summary compaction runs. The tools that stay arenotesandchat_history, since they are not tied to the budget.token_budgetstrategy keeps the feat(coderd/x/chatd): start token-budget context windows behind fork-token-budget #314 behavior:new_context, the handoff and final requests, and the forced rollover.token_budgetwithsummaryfallback runs upstream summary compaction (chatloop.GenerateCompaction) at the forced point instead of opening a new window. Anew_contextcall still rolls over without a summary. The compaction model override's trigger still applies with this fallback, so an override whose trigger comes before the usable limit summarizes there first, as it does before the threshold upstream.Snapshot: the
new_contexttool holds the fallback it was built with.turnTokenBudgetFallbackreads it from the step's tools, sotokenBudgetTriggersandforkGenerateCompactionfollow the same setting within a step, even if an admin changes it mid-step. Only the server's ownnew_contextcounts, through an unexported interface thatsecretAnswerToolforwards, so a dynamic or MCP tool namednew_contextdoes not make a turn eligible.Reads:
appendForkToolsreads the setting once per step in tool assembly.tokenBudgetMessagereads it again only when a request is due. Compaction reads nothing.Code is in fork files. Upstream files get no new hook lines;
fork_tools.gocalls the newtokenBudgetToolshelper.How tested
go test ./coderd/x/chatd/... -count=1: pass.TestTokenBudgetWindows: Cooperative and ForcedRollover settoken_budget/new_windowand pass (the feat(coderd/x/chatd): start token-budget context windows behind fork-token-budget #314 e2e path). SummaryFallback checks one summary compaction at the usable limit and no new window. SummaryFallbackNewContext checks anew_contextrollover with no summary and aw:2new_contextboundary. SummaryStrategy ramps past the handoff point and checks nonew_context, no<context_budgetin any request or stored message,notesandchat_historypresent, and summary compaction at the threshold. Removing the strategy gate intokenBudgetMessagemakes SummaryStrategy fail. PlanModeSummarizes now runs withtoken_budget.TestTokenBudgetTriggers: the summary fallback binds to a smaller override, keeps an override that comes later as the other trigger, and thenew_windowfallback drops it.TestTokenBudgetEligibility: a foreignnew_context, wrapped or not, is not eligible, and the secret answer wrapper forwards the fallback.TestChatNotes,TestNewContextTool,TestContextWindow,TestChatHistory,TestChatTranscript: pass.go test ./coderd -run 'TestForkChatOrganizationTokenBudget|TestForkChatOrganizationWakeupCaps': pass. Covers defaults when unset, round trip, 400 on bad values, 404 and 403 for members without read or update, and 403 fromRequireExperimentwith the experiment off.go test ./coderd/database/dbauthz -run TestForkQuerier, chatdmig and forkdb packages: pass.go test ./coderd/coderdtest -run 'EndpointsDocumented|TestSwagger',.github/fork/check-apidoc.sh,go run ./scripts/check-scopes: pass.golangci-lint run --fix=falseon the touched packages reports nothing in changed lines, and the new-code config reports 0 issues.lint/markdown,lint/emdash,lint/architecture,lint/migrations,lint/agents,lint/check-experiment-keys: pass.Docs:
docs/fork/pstack-adapter.mdandcoderd/x/chatd/ARCHITECTURE.md.Generated files:
make genregeneratedcoderd/apidoc,site/src/api/typesGenerated.tsand the API reference. I kept only the token-budget hunks indocs/reference/api/*.md; the generator also reorders unrelated enum values and fills older stale sections, which I left out.Not verified: a live deployment run, and an end-to-end run with a compaction model override configured (covered by the unit test of
tokenBudgetTriggers). The UI is in the follow-up PR.