Skip to content

perf(coderd/x/chatd): pack path cache, one linkable plugins query, subscribe before a step wait - #370

Merged
ibetitsmike merged 8 commits into
mainfrom
mike/issue-307-plugin-reads
Nov 17, 2026
Merged

ibetitsmike merged 8 commits into
mainfrom
mike/issue-307-plugin-reads

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part of #307: items 4, 7 and 8. Items 2, 3 and 6 are in #368, item 1 is in #369, and item 5 is covered by #294.

Changes

Item 4: pack path cache. packPaths in coderd/x/chatd/fork_plugin_gate.go kept one entry per (pack, commit) and never dropped old commits. It now keeps one entry per pack ID (packPathsEntry) that maps each commit to its paths and last read. A store sweeps every pack, at most once per minute (packPathsSweepEvery), so a cold replica that reads many packs does not scan the cache once per pack. The sweep works so: a commit not read for an hour (packPathsIdle) is dropped, and an entry left with no commits is removed with CompareAndDelete, so a deleted pack's entry goes away too. A step reads its pinned commit each time it filters pack rows, so a step still pinned to an older commit keeps its paths while newer steps read newer commits. Memory per pack is the commits read in the last hour. plugin_pack_resources holds only a pack's current files, so on a miss packPaths reads the pack commit and its files in one statement, the new forkdb query GetPluginPackCommitPaths (plugin_packs LEFT JOIN plugin_pack_resources), and caches them only when that commit is the pinned commit. One statement means both come from one snapshot. When the pack moved past the pinned commit, the gate fails closed for that step (no paths, nothing cached) instead of using a newer commit's files under the old commit. On main such a read returned and cached the newer files.

Item 7: linkable plugins. GET /api/experimental/users/me/plugins read the user's organizations and then ran GetPluginsByOrganizationID per organization, which also selected and decrypted config. It now runs one forkdb query, GetLinkablePluginsByUserID, that joins organization_members and live organizations, keeps enabled plugins, and selects no config. The chats grant filter stays in Go. dbauthz (ResourceSystem read, like GetPluginUserLinksByUserID) and dbmetrics wrappers are added. dbcrypt needs nothing because the row has no config.

Item 8: first notification. waitStep called awaitSubagentCompletion, which reads the child and then subscribes, so a child that ended its turn in between was seen only at the 5 s fallback tick. The new fork helper awaitStepChild in fork_workflow.go subscribes to the child's state channel first, then calls awaitSubagentCompletion, and restarts it when a state change arrives, so the restarted call reads the child at once. Each wait is capped at the time left before the step deadline and the restarts stop once it passed, so a child that changes state often still returns control to waitStep, which fails the step after workflowStepTimeout. Upstream subagent_operations.go is unchanged.

Tests

Item Test Result Negative control
4 TestPackPathsCacheOnePerPack (each install moves the pack commit and its files; one cache entry across c1 to c3; c1 is still served from the cache after the pack moved to c4; once every commit is an hour idle, reading c3 keeps it and storing c4 drops c1 and c2; a read of dropped c1 fails closed and caches nothing; a store within a minute of the last sweep does not sweep; a deleted pack's entry is dropped on the next sweep) PASS (also -race -count=3) Sweep gate removed: FAIL at line 549, a store soon after a sweep does not sweep. The line numbers of the mutations below were taken at a5700f4243, before the gate test moved them by about 6 lines.
4 TestPluginPackResources (GetPluginPackCommitPaths: one row per file, a NULL path for an empty pack, no row for a deleted pack) PASS not run
7 TestLinkablePluginsOneQuery (coderdtest with a counting store, member of 4 orgs) PASS coderd/plugins.go from main: FAIL, no query per organization expected 0 actual 4
7 TestGetLinkablePluginsByUserID (member orgs only, disabled and deleted org excluded, order) PASS not run
8 TestWorkflowWaitStepSeesFirstNotification (store ends the child's turn on the first completion check and returns the stale row; mock clock never fires the fallback tick) PASS, also -count=30 -race fork_workflow.go from main: FAIL, waitStep missed the notification... after 10 s
8 TestWorkflowAwaitStepChildKeepsDeadline (store publishes a state change on every check of a running child; mock clock, deadline 1 s ahead; the test waits for restarts, then advances the clock to the deadline) PASS, also -race -count=3 Wait timeout not capped at the deadline: FAIL, awaitStepChild kept restarting past the step deadline after 10 s

Commands, all under flock ~/.fork-go.lock:

  • go test ./coderd/ -run 'TestLinkablePluginsOneQuery|TestPlugins' -count=1: ok (TestPlugins, TestPluginsAudit, TestPluginsDisabled, TestLinkablePluginsOneQuery PASS)
  • go test ./coderd/x/chatd/ -run 'Workflow|Subagent|Pack|Plugin' -count=1 -v: ok, 282 PASS, 0 FAIL
  • go test ./coderd/database/forkdb/... ./coderd/database/dbmetrics/... ./enterprise/dbcrypt/... -count=1: ok
  • go test ./coderd/database/dbauthz/ -run 'TestForkQuerier|TestMethodTestSuite' -count=1: ok, includes TestForkQuerier/GetLinkablePluginsByUserID and TestForkQuerier/GetPluginPackCommitPaths
  • golangci-lint run --fix=false --new-from-rev=origin/main on the changed packages: 0 issues; gofmt -l: clean

Not verified

  • The single-statement read prevents a rollback from landing between the commit read and the paths read. No test drives that race deterministically.
  • No run against a live deployment. The linkable plugin list was checked through coderdtest only.
  • The restart in awaitStepChild re-runs the descendant check and one chat read per state change of the step child. That cost was not measured.

… in one query, and subscribe before waiting on a step

Part of #307 (items 4, 7 and 8).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 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-17T04:32:20.490755Z 6c58014 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: 40d265778f

ℹ️ 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_plugin_gate.go Resolved
Comment thread coderd/x/chatd/fork_workflow.go Resolved
…p deadline across wait restarts

The pack path cache holds the two latest commits per pack, so a step
still pinned to the previous commit keeps its paths. awaitStepChild caps
each wait at the step deadline and stops restarting once it passed, so
a child that changes state often cannot hold waitStep past the step
timeout.

Part of #307.
@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: 15549fbe6e

ℹ️ 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_plugin_gate.go Resolved
The two-slot cache dropped the commit of a step pinned to it once the
pack moved twice, and the next read stored the current files under the
old commit. The cache now keeps every commit read in the last hour and
drops the others when a new commit is stored.
@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: 0417bd9930

ℹ️ 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_plugin_gate.go Resolved
…moved past

The resource table holds only a pack's current files. A miss for a
pinned commit now reads the pack commit after the files, caches them
only when it still matches, and returns no paths otherwise, so the
files of a newer commit are never used as the pinned commit's.
@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: 6d92082844

ℹ️ 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_plugin_gate.go Resolved
Comment thread coderd/x/chatd/fork_plugin_gate.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. Another round soon, please!

Reviewed commit: 799ad2e2e9

ℹ️ 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: 38b25933a2

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

Merged main at 76b39d63c8 (#369) into this branch by merge commit. No conflicts. New head a5700f4243. chatdmig check passes and forkdb/generate.sh produces no diff. Build and vet pass; tests for forkdb, dbpurge, dbauthz TestForkQuerier, chatd -run "Plugin|Pack|Workflow|Notify|Wakeup" and coderd -run Plugin pass; golangci-lint 0 issues, gofmt clean. #368 will carry Closes #307 because it merges last.

@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: a5700f4243

ℹ️ 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_plugin_gate.go Resolved
A cold replica stores one commit per enabled pack, and a sweep on every
store scanned the whole cache once per pack. The step deadline test now
runs on a mock clock instead of a 300 ms wall-clock deadline.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Pushed 6c580143cc for the two P2 findings on a5700f4243. The pack path cache now sweeps at most once a minute, and the step deadline test runs on a mock clock. Both are mutation checked. chatd -run "Plugin|Pack|Workflow" passes, golangci-lint 0 issues, gofmt clean.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 6c580143cc

ℹ️ 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 d310975 into main Nov 17, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-307-plugin-reads branch November 17, 2026 04:54
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head 6c580143cc6cddab8f3517baa2ba330b280b1b38 as d310975, on main 76b39d63c89b70fb2a7b43b73445abd9476748d4, with the server running that main.

Gates on this exact head:

  1. The head matched the SHA I verified (--match-head-commit).
  2. Every check run passed. fork-ci run 44017211529 passed on this head.
  3. Codex receipt for this head: perf(coderd/x/chatd): pack path cache, one linkable plugins query, subscribe before a step wait #370 (comment).
  4. No unresolved review thread.
  5. 0 commits behind main.
  6. I ran the tests below on this exact head.

Tests I ran on 6c580143cc (detached worktree, under flock ~/.fork-go.lock, Postgres 16 at 127.0.0.1:5432):

  • go test -count=1 -v ./coderd/database/forkdb/...: 68 PASS, 0 FAIL.
  • go test -count=1 -v -run TestForkQuerier ./coderd/database/dbauthz/: 894 PASS, 0 FAIL.
  • go test -count=1 -v -run Plugin ./coderd/: 20 PASS, 0 FAIL.
  • go test -count=1 -race -v -run 'Plugin|Pack|Workflow' ./coderd/x/chatd/: 187 PASS, 0 FAIL.
  • go test -count=10 -race -v -run 'TestWorkflowWaitStepSeesFirstNotification|TestWorkflowAwaitStepChildKeepsDeadline' ./coderd/x/chatd/: 20 PASS, 0 FAIL.
  • Negative control: with waitStep calling awaitSubagentCompletion directly again, TestWorkflowWaitStepSeesFirstNotification fails ("waitStep missed the notification of a child that ended its turn before the wait subscribed"). The source was restored after the control.

Review notes:

  • The pack path cache is keyed by pack ID, and one statement now reads the current commit and its paths. The cache is swept at most once a minute; that was the Codex finding fixed in 6c580143cc.
  • The linkable plugins list reads in one query.
  • A workflow step wait subscribes to the child's state channel before it reads the child, so a child that ends its turn in between is seen at once instead of at the 5 s fallback tick. No wait runs past the step deadline.
  • These are coderd-only changes with the same results, so no experiment.

Not exercised: a running server with real plugin packs, and many replicas sweeping the cache.

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