Skip to content

feat(coderd): conditional writes for saved workflows (fork-workflows) - #394

Merged
ibetitsmike merged 9 commits into
mainfrom
mike/issue-392-conditional-workflow-writes
Nov 20, 2026
Merged

ibetitsmike merged 9 commits into
mainfrom
mike/issue-392-conditional-workflow-writes

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part of #392. The site client part waits for #387 and lands separately.

What

The saved workflow PATCH and DELETE endpoints (user and organization tiers) accept an optional expect_id query parameter. When present, the write lands only when the stored workflow still has that id. A mismatch answers 409 Conflict with nothing changed and no stored id in the response. Without the parameter, behavior is unchanged from main.

How

The condition lives inside the four forkdb write queries:

AND (sqlc.narg('expect_id')::uuid IS NULL OR id = sqlc.narg('expect_id')::uuid)

so:

  • dbauthz authorizes the caller before any id comparison, and no pre-write read can leak a hidden workflow's id or existence (the first review draft read as system before authorization; this closes that).
  • The check is atomic with the write, so no check-then-write race.
  • A caller whose write authorization comes from a narrow scope (for example user_skill:update without read) can still send a conditional write.

On sql.ErrNoRows with expect_id, the handler re-reads once to tell 404 (row gone) from 409 (row exists with a different id). Denials probe with the caller's own authorization, so a caller who cannot read a hidden workflow keeps getting 404.

Tests

TestForkSavedWorkflowConditionalWrites (18 subtests, user and organization tiers): mismatch 409 with no change, match succeeds, no-parameter old behavior, invalid id 400, missing workflow 404, update-only scope succeeds on match, delete-only scope gets 409 on mismatch, hidden workflow stays 404 on denial and on mismatch for a non-reader.

Negative control: neutralizing the SQL clause to a tautology makes PatchMismatchConflicts fail (update succeeded, expected conflict), and removing the clause outright fails the build because the generated params struct loses ExpectID. Both restored afterwards.

Verification

  • go build ./... passes.
  • go test ./coderd/ ./coderd/database/forkdb/ ./coderd/database/dbauthz/ ./coderd/x/chatd/ ./codersdk/ passes. One pre-existing flake, TestForkPluginPackEnablement, fails under full-suite load with context deadline exceeded and passes in isolation and on the base commit.
  • bash .github/fork/check-apidoc.sh exits 0.
  • golangci-lint run ./coderd/... reports no findings in the touched files. The baseline has pre-existing findings elsewhere.

Part of #392 (backend part; the site client follows in a separate PR once #387 merges).

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 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-20T03:49:58.181025Z 5e3140e 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: 9f59537e22

ℹ️ 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/fork_saved_workflows.go Resolved
Comment thread coderd/fork_saved_workflows.go Resolved
Comment thread coderd/fork_saved_workflows.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. Can't wait for the next one!

Reviewed commit: 9f59537e22

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

PATCH and DELETE for saved workflows (user and organization tiers)
accept an optional expect_id query parameter. When present, the write
lands only when the stored workflow still has that id, and a mismatch
answers 409 Conflict with nothing changed. Without the parameter
behavior is unchanged.

The check lives in the write queries themselves, so the dbauthz wrappers
authorize the caller before any id comparison, no read leaks a hidden
workflow's id, and the check is atomic with the write. A no-rows
conditional write re-reads with the caller's authorization to tell 404
from 409.

codersdk client update and delete methods take an optional
ForkSavedWorkflowWriteParams with ExpectID.
@ibetitsmike
ibetitsmike force-pushed the mike/issue-392-conditional-workflow-writes branch from 9f59537 to 0b86d4b Compare November 19, 2026 20:03
@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. Already looking forward to the next diff.

Reviewed commit: 0b86d4bb62

ℹ️ 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 (49f454e) to clear the not-behind gate. @codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: c19cec5568

ℹ️ 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 (c631d81) and regenerated the apidoc enum ordering the sync moved. @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: 2632cd0d94

ℹ️ 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/fork_saved_workflows.go Resolved
…fetch miss

Codex review of 2632cd0 found that the organization conditional write
wrappers fetch the row before Authorize runs, so a sql.ErrNoRows from the
fetch reached okAfterConditionalWrite as if the authorized write had
dropped every row, and the conflict check then read the row as the
system. A caller whose conditional write raced with a delete and a
recreate of a hidden workflow could get a 409 that proves the workflow
exists. Mark a fetch-phase miss in fork_forkdb.go and let the handler
skip its system probe for it, which keeps the pre-existing 404.
@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: b7cf24fcad

ℹ️ 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/fork_saved_workflows.go Resolved
Comment thread coderd/fork_saved_workflows.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Replied above:

  • The P1 (architecture TODO) is fixed in 7b02d9b, which adds the TODO item to coderd/x/chatd/ARCHITECTURE.md.
  • The P2 (reauthorize the replacement before reporting a conflict) is fixed in the same commit: the org conflict probe fetches the current row and reauthorizes it for the requested write action (update or delete) before answering 409. The fetch is system restricted so write-only API key scopes still reach the probe, and the reauthorization sends a replacement that hides from the caller to 404 instead of a conflict.
  • The 7b02d9b commit also drops the dbauthz marking helper from b7cf24f, which the new probe makes unnecessary. TestForkSavedWorkflow|TestForkOrganizationSavedWorkflow and the dbauthz and forkdb packages pass with it.

@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Coder Agents acted on behalf of @ibetitsmike.

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

ℹ️ 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/ARCHITECTURE.md Resolved
Comment thread coderd/fork_saved_workflows.go Resolved
Comment thread coderd/fork_saved_workflows.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. Chef's kiss.

Reviewed commit: 1b71293f46

ℹ️ 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 origin/main (080e195, the upstream sync from #398) into the branch after 1b71293. No code changes. All checks re-verified locally. @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: e85f319162

ℹ️ 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/fork_saved_workflows.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Fixed the last finding and pushed 5e3140e. @codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 5e3140eaae

ℹ️ 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 6ca1e1f into main Nov 20, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-392-conditional-workflow-writes branch November 20, 2026 04:06
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head 5e3140eaaeb9a05e49c494f066a67183cb2fa306 as 6ca1e1f, on main 080e1950c0438a985ae213c98c6bb5851937dd14, 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 44669933948 passed on this head.
  3. Codex receipt for this head: feat(coderd): conditional writes for saved workflows (fork-workflows) #394 (comment) ("Didn't find any major issues", Reviewed commit: 5e3140eaae).
  4. No unresolved review thread.
  5. 0 commits behind main.
  6. I ran the tests below on this exact head.

Tests I ran on 5e3140eaae (detached worktree, under flock ~/.fork-go.lock):

  • go test -count=1 -v -run 'TestForkSavedWorkflow|TestForkOrganizationSavedWorkflow' ./coderd/: 27 PASS, 0 FAIL.
  • go test -count=1 ./coderd/database/forkdb/ ./coderd/database/dbauthz/ ./codersdk/: 3 packages ok.
  • Negative control: with the four expect_id clauses in the generated queries turned into TRUE, TestForkSavedWorkflowConditionalWrites fails for both tiers (mismatched PATCH and DELETE no longer conflict, and a hidden workflow stops answering 404). The source was restored after it.

Review notes:

  • PATCH and DELETE on personal and organization saved workflows take an optional expect_id query parameter. The check sits inside the write query, so it is atomic with the write and runs after dbauthz. A mismatch answers 409 without the stored id; a workflow the caller cannot read or write still answers 404. Without expect_id the behavior is as on main. An empty, invalid or unparsable expect_id answers 400.
  • The endpoints are already behind fork-workflows, so no new experiment.
  • The ARCHITECTURE.md note is still a TODO comment. Lead decision: the site client PR for saved workflows: id-conditional PATCH and DELETE #392 turns it into final prose in the fork section, once the whole feature is in.
  • This is the backend part of saved workflows: id-conditional PATCH and DELETE #392, so saved workflows: id-conditional PATCH and DELETE #392 stays open.

Not exercised: a running server with two users racing a delete and recreate, and the hidden-replacement probe path, which has no dedicated test.

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