Repository navigation
Description
Filed by Xum (an AI coding agent) on behalf of @ibetitsmike. Part of #311. Line numbers are at 607a95b.
Summary
The fork's dbauthz wrapper authorizes almost every fork query by first reading the row's chat with GetChatByID (and for wake-up, workflow run and workflow step queries, the wake-up, run or step row as well). It does this for every caller, including the chatd system subject, which holds create, read, update and delete on every chat. For chatd the pre-reads can never deny, so they are pure overhead, and chatd is the caller on every hot path: the wake-up worker, the generation step, the workflow journal.
This is the cheapest fix with the widest effect. It removes 6 of the 12 statements of every idle wake-up poll and 3 of the 4 statements of every workflow step transition.
Evidence
The helper and its two wrappers:
fork_forkdb.go:965-972authorizeChat:GetChatByID, thenauthorizeContext(ctx, action, chat).fork_forkdb.go:974-984authorizeChatWakeup:GetChatWakeupByID, thenauthorizeChat.fork_forkdb.go:866-879UpdateWorkflowStep:GetWorkflowStep,GetWorkflowRun,authorizeChat, then the UPDATE. Four statements for one write.
The chatd subject can act on every chat, so the object-level check is always true for it:
dbauthz.go:807-837: rolechatdhas site-wideResourceChat: {create, read, update, delete}.
Some fork queries already authorize at type level with no read, which shows the pattern is accepted:
fork_forkdb.go:52-57ClaimDueChatWakeups:authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceChat).
Hot callers that pay the pre-reads as chatd or system-restricted:
| Caller | Fork query | Statements issued | Needed |
|---|---|---|---|
Wake-up worker, every poll of every waiting wake-up (fork_wait_for_events.go:379) |
ListChatWakeupRunKeys |
3 | 1 |
Wake-up worker, every poll (fork_wakeups.go:375-376) |
FinishChatWakeupClaim |
3 | 1 |
Wake-up worker, every poll (fork_wait_for_events.go:621-629) |
ListChildChatIDsWithActiveEventsWakeups, ListPendingChatDescendantQuestionsByRoot |
2 each | 1 each |
Generation step, every step (fork_pinned_skills.go:64, fork_context.go:115, fork_agent_tool_policy.go:238) |
GetChatPinnedSkillsByChatID, GetChatTodosByChatID, GetChatSubagentByChatID |
2 each | 1 each |
| Workflow journal, every step transition | UpdateWorkflowStep |
4 | 1 |
Workflow journal, every run transition (fork_forkdb.go:853-863) |
UpdateWorkflowRunStatus |
3 | 1 |
GetChatByID is not a bare primary key read: it selects from the chats_expanded view.
The HTTP handlers pay too. The route middleware has already loaded and authorized the chat, and the wrapper loads it again per query (for example the workflow run detail endpoint reads the run row three times per request).
Proposed design
-
In
forkQuerier, add one helper:// actsOnAllChats reports whether the subject holds action on the chat // type itself, so reading one chat to authorize it cannot change the // outcome. func (f *forkQuerier) actsOnAllChats(ctx context.Context, action policy.Action) bool { return f.q.authorizeContext(ctx, action, rbac.ResourceChat) == nil }
authorizeChat,authorizeChatWakeupand the workflow run and step wrappers return early when it is true. This is the same type-level checkClaimDueChatWakeupsalready uses. It runs in memory, so it costs no statement. A subject that fails it (every ordinary user, and any subject whose API key scope lists specific resource IDs) takes today's object-level path unchanged, so the change can only skip a read whose answer was already "allowed". -
For HTTP callers, stop paying the second read by passing what the handler already has. Change the fork query params that take a bare
chat_idon user-facing read paths to carry the loadeddatabase.Chatin the wrapper call (aforkdbwrapper method variant such asListChatWakeupsOfChat(ctx, chat)that authorizes the given object and then queries). Keep the SQL unchanged. -
Do not add a cache. The fix is to not ask.
Acceptance criteria
- A dbauthz test proves that, with
AsChatdand withAsSystemRestricted(both hold site-wide chat permissions), each of the fork methods listed above issues exactly one store call (use the existing dbmock recorder), and that a member without access to the chat is still denied with no change in error. - The negative control for that test is restoring the pre-read: the store-call count assertion fails.
- A wake-up worker test (
TestWakeup*) counts store calls for one idle poll of oneeventswake-up and asserts the new total. - No change to RBAC policy files and no new subject.
Notes
- This issue does not change what any user may do. It removes reads whose result cannot affect the decision.
- chatd: replace the 15 s wake-up claim loop with a waker driven by the chat state machine #294 and chatd: store wake-up watches as rows instead of an events JSON and run keys #293 remove the polls themselves. This issue still matters after them, because the per-step and journal callers remain.
Activity
ibetitsmike commentedon Nov 14, 2026
Delegated to a workstream agent on branch
mike/issue-292-*. A PR follows.ibetitsmike commentedon Nov 14, 2026
Implemented in #320 (branch
mike/issue-292-dbauthz-chatd-fastpath, headda33d4993a). The fork dbauthz wrappers skip the chat/wake-up/workflow pre-reads for the chatd and system-restricted subject types, gated on subject type likeauthorizeProvisionerJob. Review round-trips with Codex and an independent authorization review ended with no open findings; fork-ci is green on the head.