Skip to content

fix(chatd): adopt a workflow step another runner journaled, and fix fork-ci flakes - #375

Merged
ibetitsmike merged 2 commits into
mainfrom
mike/issue-373-flaky-tests
Nov 17, 2026
Merged

ibetitsmike merged 2 commits into
mainfrom
mike/issue-373-flaky-tests

Conversation

@ibetitsmike

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Fixes the root causes of the fork tests that failed fork-ci on main. No retries, skips, longer sleeps or reruns.

Closes #373
Closes #353

Product race: duplicate workflow_steps_pkey

The most common failure: journal the workflow step: pq: duplicate key value violates unique constraint "workflow_steps_pkey", in 3 subtests across 4 runs (StopLinesBypassTheEchoSuppression among them).

The startup sweep MarkOpenWorkflowRunsInterrupted marks every open run interrupted. A run can then be resumed while the process that started it still runs the script, for example during a rolling restart, on another replica, or with the two servers of the test. Step IDs derive from the run and the step spec, so both runners insert the same key. The later insert failed, and startStep also interrupted the child chat of the other runner.

startStep now adopts the journaled row on that unique violation (adoptJournaledStep). It checks the run ID, rebuilds the step with the helper the rehydrate loop now shares (journaledWorkflowStep), and replays it like a rehydrated step. The child chat keeps running.

New subtest TestForkWorkflowJournal/ResumeAdoptsAStepAnotherRunnerJournaled. Negative control: with the adopt branch disabled it fails with the exact CI error.

Test fixes

Test Root cause Fix
forkdb TestInTx The parallel subtests shared one WaitShort context. In CI log 43965522540 they ran about 12s after the parent made it, so it had expired (context canceled, context deadline exceeded). Each subtest makes its own context.
FallbackRearmsWhileTheTokenIsHeld The timer callback counts the attempt before it re-arms. The test read the count and the timer separately, inside that window. Read both under one journalMu lock and stop only when both hold.
ReplayKeepsACappedTail Paced writes land in parts; an early write already holds 50 lines starting at line 18. Wait for the final capped tail (line 30 to line 79).
ReplayReEmitsAFailureLineOnce The restart could run before both steps were journaled. Wait for the steps first, like ReplaySkipsAnUnmatchedBaselineLine.
TestPlugins/LinkSendAndDeliver (#353) ConverseScopeOnlyReachesChats makes an unlabeled chat for the shared member, and delivery makes it the idle link's active chat (intended behavior), so send posts to the chat with label "". The subtest links a user of its own.
TestForkChatSecretAnswer, TestForkChatSecretAnswerExpired The fake busy chat has a worker ID but no runner, so the real worker acquires it and promotes the queued answer before the DELETE (404 instead of 204, run 43613373311). Disable the chat worker, as TestForkChatSecretQuestionQueuedMessage already does.

Evidence (all under the workspace Go lock, race builds unless noted)

Negative controls, original code vs fix:

  • Duplicate key: adopt branch disabled fails with workflow_steps_pkey; fix passes.
  • FallbackRearms: with a 500ms delay before the re-arm, the old loop failed 5/5 ("the second round must still be armed"), the new loop passed 5/5.
  • TestInTx: with an 11s wait after t.Parallel(), the original failed with all three CI errors; the fix passed.
  • TestForkChatSecretAnswer: with a 3s wait before the DELETE, the original failed 3/3 (204 expected, 404 actual); the fix passed 3/3.
  • LinkSendAndDeliver: original -count=20 failed 2/20 with expected "bridge", actual "".

Stress on the fix:

  • TestForkWorkflowLiveLogs|TestForkWorkflowJournal -count=50 -cpu 1,2: PASS.
  • TestInTx -count=50 -cpu 1,2: PASS.
  • TestPlugins -count=50 (default GOMAXPROCS): 50/50 PASS.
  • CI shape, no race: -run '(?i)chat|skill|mcp|plugin' ./coderd -cpu 4 -count=2: PASS.
  • golangci-lint --new-from-rev origin/main on the three packages: 0 issues. gofmt -l: clean.

Not verified

  • TestPlugins and the coderd secret answer tests cannot run under race at -cpu 1 or -cpu 2 on this VM, with or without this change. On main, TestPlugins failed 2/2 at -cpu 1 and 6/10 at -cpu 2: the plugin's coder_on_http hits its 10s limit and the 60s context expires (a chat create took 7.7s, a plugin install 10.3s). At -cpu 1 the secret tests time out in CreateFirstUser and createChatModel. That is a load limit, separate from these flakes. CI runs without race on 4 vCPUs.
  • TestWakeupPullRequestReadFailures/OtherError and PhaseBuiltinsRejectBadNames failed only on branches before their merge; main already has the fixes. Not changed.
  • TestRunnerManager_HeartbeatDoesNotReviveStaleLease is upstream and already skipped in fork-ci.

The startup sweep marks every open workflow run interrupted, so a run can
be resumed while the process that started it still runs the script. Step
IDs derive from the run and the step spec, so both runners insert the
same workflow_steps key. The later insert failed with a duplicate key and
interrupted the other runner's child chat. startStep now adopts the
journaled row and replays it like a rehydrated step.

Also fix the root causes of the other fork-ci flakes:

- TestInTx: each parallel subtest makes its own context. A shared
  WaitShort context expired while the subtests waited for a slot.
- FallbackRearmsWhileTheTokenIsHeld: read the attempt count and the timer
  under one lock. The timer callback counts the attempt before it
  re-arms, so separate reads raced the re-arm.
- ReplayKeepsACappedTail and ReplayReEmitsAFailureLineOnce: wait for the
  full journaled state before the restart.
- TestPlugins/LinkSendAndDeliver: link a user of its own. Delivery made
  the unlabeled chat of a parallel subtest the link's active chat.
- TestForkChatSecretAnswer(Expired): disable the chat worker. The fake
  busy chat has no runner, so the worker acquired it and promoted the
  queued answer before the test deleted it.

Closes #373
Closes #353
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 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-17T02:42:34.457791Z 25f95fe PR opened
ℹ️ 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.

@ibetitsmike
ibetitsmike merged commit 99d1b24 into main Nov 17, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-373-flaky-tests branch November 17, 2026 03:05
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head 25f95fe1b5013ca7e99f0e53fb63c85411e962b5 as 99d1b24, on main 57bea8f79ab311eb1b2f0886176c2e87e1e6d1ef, 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 44004506625 passed on this head.
  3. Codex on this head: fix(chatd): adopt a workflow step another runner journaled, and fix fork-ci flakes #375 (comment). Codex now posts a summary comment on a clean review instead of the old receipt. Its state shows code review completed on 25f95fe1b5 with no findings, and the bot added a +1 reaction.
  4. No unresolved review thread.
  5. 0 commits behind main.
  6. I ran the tests below on this exact head.

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

  • go test -count=3 -race -v -run TestForkWorkflow ./coderd/x/chatd/: 384 PASS, 0 FAIL. ResumeAdoptsAStepAnotherRunnerJournaled passed 3 of 3.
  • go test -count=3 -race -v -run TestInTx ./coderd/database/forkdb/: 12 PASS, 0 FAIL.
  • go test -count=2 -v -run 'TestPlugins|TestForkChatSecretAnswer' ./coderd/: 28 PASS, 0 FAIL. LinkSendAndDeliver passed 2 of 2.
  • Negative control: with the IsUniqueViolation(err, workflowStepsPkey) branch in startStep turned off, ResumeAdoptsAStepAnotherRunnerJournaled fails with pq: duplicate key value violates unique constraint "workflow_steps_pkey", the CI error. The source was restored after the control.
  • The workstream ran the workflow, TestInTx and TestPlugins tests at -count=50 -race with the results in the PR body, plus a negative control for each flake.

Review notes: the duplicate key was a product race, not only a test flake. The startup sweep marks every open run interrupted, so a second runner can resume a run while the first still runs its script. Both derive the same step ID. createWorkflowStepChat already returns the existing child chat, so the later runner now adopts the journaled row and replays it like a rehydrated step, instead of failing and stopping the first runner's child. The other changes are test-only.

Not exercised: two coderd replicas or a rolling restart against a real database. The -cpu 1 passes of TestPlugins and the secret tests hit VM load limits under race (see the PR body), so they were not used as evidence.

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.

test: fix the flaky fork tests that block autodeploy on main test(coderd): TestPlugins/LinkSendAndDeliver is flaky (chat label empty)

1 participant