Skip to content

feat(coderd/x/chatd): add the chat_history tool behind fork-token-budget - #313

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

ibetitsmike merged 9 commits into
mainfrom
mike/issue-283-token-budget-history

Conversation

@ibetitsmike

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part of #283. This is PR 2 of 4 in the token-budget stack. PR 1 (#285, notes) is merged.

What this adds

A read-only chat_history tool behind the fork-token-budget experiment. It lets an agent look back at messages that are no longer in its context window. It has four actions:

  • list_windows: lists the context windows of the chat. A compaction starts the next window (chat_summarized). A clear starts over at w:1.
  • list_items: lists the messages in one window, with an ID, a role and a short preview.
  • search: finds messages in the chat that contain a text.
  • read_item: reads one message with long parts cut, or one part in full, a slice at a time.

Limits:

  • Each result is capped at 16 KiB.
  • The history never reaches past a clear of the chat.
  • It shows only rows that both the user and the model see.
  • The tool reads only its own chat. It cannot read another chat.

Plan, explore and read-only custom agent types do not get the tool, as with the notes tool.

History is read-only, so its calls do not need to run in order.

Files

  • coderd/x/chatd/fork_chat_history.go holds the tool, with tests in fork_chat_history_internal_test.go.
  • coderd/database/forkdb/queries/chat_history.sql holds the queries. The generated code and the dbauthz, dbmetrics and mock wrappers are updated to match. There is no schema change.
  • fork_tools.go registers the tool. fork_chat_transcript.go now skips chat_history calls, as it already skips its own, so paging does not loop.
  • The docs are coderd/x/chatd/ARCHITECTURE.md and docs/fork/pstack-adapter.md.

Tests run

Every command ran under flock ~/.fork-go.lock.

  • ./coderd/database/forkdb/generate.sh made no diff.
  • go build ./coderd/... succeeded.
  • go test -count=1 -v -run '(TestChatHistoryTool|TestChatNotesTool|TestChatTranscript|TestForkMessageAgentReplay)' ./coderd/x/chatd/ passed.
  • go test -count=1 -run TestForkQuerier ./coderd/database/dbauthz/ passed.
  • go test -count=1 ./coderd/database/forkdb/ ./coderd/database/dbmetrics/ passed.
  • go test -count=1 -run TestExperimentDisplayNames ./codersdk/ passed.
  • golangci-lint run --config=.golangci.new-code.yaml --new-from-merge-base=origin/main ./coderd/x/chatd/ ./coderd/database/... found 0 issues.
  • make lint/emdash lint/markdown passed.

Not verified

  • The tool was not run against a live model on a deployed server.

Note

I rebuilt this branch from main with a cherry-pick after #285 was squash-merged. The branch had not been published, so it is one commit on top of main.

The dropped #285 P2 (a notes append replayed after a crash) is not fixed here. Fixing it needs a new chatdmig node and new queries, and this PR has no schema change. I can do it in a separate small PR.

@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-14T07:29:03.360315Z a832964 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: decfb2175a

ℹ️ 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_chat_history.go Resolved
Comment thread coderd/x/chatd/fork_chat_transcript.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: 7718cd388a

ℹ️ 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_chat_history.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 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: 06f5935855

ℹ️ 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_chat_history.go Resolved
@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. Nice work!

Reviewed commit: bcd931102d

ℹ️ 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

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. Can't wait for the next one!

Reviewed commit: f6c6258cdf

ℹ️ 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".

# Conflicts:
#	coderd/x/chatd/ARCHITECTURE.md
@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. Nice work!

Reviewed commit: a83296488b

ℹ️ 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 9c4c333 into main Nov 14, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-283-token-budget-history branch November 14, 2026 07:39
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged as 9c4c333 (squash) at 07:39Z. Before the merge, the server was confirmed on the previous deploy, 3a79dd3.

Gates on head a832964:

  1. The head matched the SHA I verified, and main was 3a79dd3 at merge time.
  2. All check runs passed. fork-ci run 43570977947 passed on this exact head.
  3. Codex receipt: issuecomment-6873829504, "Didn't find any major issues", reviewed commit a832964. The four earlier P2s were fixed, each with a test.
  4. 0 unresolved threads.
  5. The branch was 0 commits behind main. The diff against main was 15 files, +1381 -7.
  6. My tests on the exact head, run under flock:
    • go test -count=1 -v -run '(TestChatHistoryTool|TestChatNotesTool|TestReadChatTranscript|TestChatTranscript|TestForkMessageAgentReplay|TestViewImage)' ./coderd/x/chatd/: 9 top-level tests passed.
    • go test -run '(TestForkQuerier)' ./coderd/database/dbauthz/: passed.
    • go test -run '(TestExperimentDisplayNames)' ./codersdk/: passed.
    • I also reviewed the new queries: both are scoped to the calling chat's chat_id, take bound parameters, and dbauthz asserts read on the chat.

Not exercised: the tool against a live model on a deployed server. fork-token-budget stays off. The #285 P2, where a replayed notes append can duplicate a note, is still open and will get its own small PR.

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