Repository navigation
Load prompt rows once per generation step - #334
Conversation
One generation step loaded the chat's prompt rows up to 4 times: fork context for the todo snapshot check, the pinned skills check, the token budget check, and buildTurnEnvironment. syncForkContext now loads the rows once when any of those checks is enabled and hands them to each check and to the turn environment build. Correctness comes from reload on insert: a fork context message commit bumps the chat history version, which restarts the step, and the new pass syncs and loads again. When sync commits nothing, no inserts happen between the load and the prompt build in the same pass. Turns that fork context restart keep the same load count as before. The upstream loads outside a step pass (memory index pre-read, final turn run result, chat summary) are unchanged and out of scope. Closes #298
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. |
A failing shared prompt-row load returned an error to runTaskWithRetry, which retries the whole task with no attempt cap and never marks the chat errored. Before the shared load, the same query failing inside the turn environment build was retried three times under the prepare phase and then errored the chat. Log the failure and return no rows instead, so the todo snapshot, pinned skills, token budget and turn environment consumers fall back to their own loads and the established error path. Also keep the loaded rows on the token budget error path, where nothing was inserted, so the turn environment does not reload them.
Addressed the review findings:
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a8a14a87ec
ℹ️ 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".
…lback After the organization token budget merge, forkGenerateCompaction loaded prompt rows again on the summary fallback path of an automatic compaction. The step's turn environment now carries the rows the fork context sync loaded once, and the fallback reads them first, loading its own only when the environment has none. This also shares the load with tokenBudgetTriggers, which read the rows the turn environment already held, so no new load appears there.
Merged origin/main (9308bf6, #330). That merge added a prompt-row load in Counting tests rerun on the new head, all pass; full |
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 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". |
…ilure A failed shared prompt-row load skipped the todo snapshot, pinned skills and token budget checks for the pass, while the turn environment could reload rows under its own retry and run the step without a due fork context message. Continue with nil rows instead: each check falls back to its own load, so a step that is due a message still commits it, and a persistent failure still errors the chat on the prepare retry path.
Fixed the P2 review finding in 1032a06: the shared-load failure no longer returns early. All tests and the lint gate rerun green on the new head. Requesting a fresh review. @codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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 origin/main (9f99450, #331, site-only, no code conflicts). New head 8645472, not behind main. Waiting for fork-ci on this head, then a fresh Codex receipt. |
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8645472f77
ℹ️ 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".
Merged origin/main (5f049c1, #332). #334 adds no chatdmig node, so no requires line is needed; the new node 01a1da95 merges in cleanly with its requires line on 01a1da86. New head 78a3c62. Waiting for fork-ci, then a fresh Codex receipt. |
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 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". |
…tion build Codex flagged the exported TodoSnapshotMessageForTest, PinnedSkillsMessageForTest and WriteChatTodosForTest wrappers in fork_testhooks.go: they compiled into every normal chatd build and were referenced only by the prompt-load test. Delete the file and make fork_prompt_load_internal_test.go a real package chatd internal test that calls todoSnapshotMessage, pinnedSkillsMessage and writeChatTodos directly, with its own Anthropic server fixture, counting store and request recorder instead of the package chatd_test helpers. No production code changes. Test expectations are unchanged.
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! 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 99ced87 (squash, Gates on head 89a8009, with main at 3f37f49 (live on the server at merge time):
Tests I ran on this head:
Not exercised: the token budget summary-fallback reuse of |
Closes #298
What changed
One generation step loaded the chat's prompt rows (
GetChatMessagesForPromptByChatID) up to 4 times: fork context loaded them for the todo snapshot check, the pinned skills check and the token budget check, andbuildTurnEnvironmentloaded them again.syncForkContextnow loads the rows once per step pass when any offorkContextSharesPromptRows()(ForkChatTodos, ForkPinnedSkills, ForkTokenBudget) is enabled, and hands the slice topinnedSkillsMessage,tokenBudgetMessageand, viagenerationPrepareInput.PromptRows,buildTurnEnvironment.Each consumer keeps its nil-rows self-load path, so callers that do not share rows behave exactly as before.
Correctness: reload on insert
A fork context message commit bumps
chats.history_version, which restarts the generation step, and the new pass runssyncForkContextagain, which loads fresh rows. When sync commits nothing, no insert happens between the load and the prompt build in the same pass: the rows the prompt is built from are the rows that were checked. The turn environment build only reads rows. This is asserted byTestForkContextInsertRestartsStepWithRows.Load counts
The turn runner runs one generation step per
history_version, so a fork-context-restarting turn runs two passes and loads the rows twice, the same as before this PR (the old code also loaded 2-3 times per pass). Named upstream loads outside a step pass are unchanged and out of scope:memoryIndexMessagefirst-turn pre-read (upstream, one load)deriveFinalTurnRunResultend-of-turn status label (upstream, one load)generateAndStoreChatSummarybackground summary (upstream, one load)Tests
New
coderd/x/chatd/fork_prompt_load_internal_test.go:TestGenerationStepLoadsPromptRowsOnce: preset rows, fork experiments on, asserts exactly 5 loads in the turn (2 step passes + 3 named upstream loads) and both fork context headers in the first streaming request.TestGenerationStepExperimentsOffKeepsUpstreamLoads: all fork experiments off, asserts 5 loads and neither header in the request.TestGenerationStepNoPinsNoTodosLoadsOnce: fork experiments on, nothing seeded, asserts 5 loads and exactly one streaming request.TestForkContextInsertRestartsStepWithRows: no preset, asserts both fork context messages land as model-only rows and the request contains them.A counting store wraps the DB and counts
GetChatMessagesForPromptByChatIDcalls. The fixture creates the chat idle and starts the turn withSendMessage, so no seed row can race the worker.fork_testhooks.goexports the three unexported entry points for the external test package; production code paths are unchanged.Without the change the count test fails with 9 loads, so the reduction is measured, not assumed.
Notes
memoryIndexMessagekeeps its own load: it runs insyncMemoryIndexbefore the shared load, and changing upstream memory index behavior is out of scope.generationPrepareInputlives in an upstream file (generation.go); the field addition follows the established fork pattern of one hook line per upstream file, extended here by the struct field the hook needs.Verification
go test -count=1 -timeout 10m ./coderd/x/chatd/...: all 21 packages ok.go vet ./coderd/x/chatd/: clean.golangci-lint run --config=.golangci.new-code.yaml --new-from-merge-base=origin/main: 0 issues.