Skip to content

test(app-shell): useDeferredFirstSend waits on the replay, not a fixed tick - #37014

Merged
objectstack-fleet[bot] merged 1 commit into
mainfrom
claude/issue-36967-deferred-first-send-flake
Nov 24, 2026
Merged

objectstack-fleet[bot] merged 1 commit into
mainfrom
claude/issue-36967-deferred-first-send-flake

Conversation

@objectstack-fleet

Copy link
Copy Markdown
Contributor

Fixes #36967
Clause-②: no
Metadata-protocol: none

维护者速读

  • 用户无感:只改了一个测试文件里「等待自动补发首条消息」的写法,产品代码一行未动。
  • 属于合并队列稳定性:这个测试在繁忙的队列机器上偶发失败,曾把一个 p1 修复踢出合并队列,这里替开发席位清掉这个绊脚石。
  • 出错的影响只在测试层:最坏是这个测试仍偶发失败、需要重新排队,可直接撤回本 PR,对用户和数据都没有影响。
  • 无。

What changes

ui/packages/app-shell/src/console/ai/__tests__/useDeferredFirstSend.test.tsx only. The flushReplay() helper, which waited on its own setTimeout(0), is gone; every call site now waits on a condition:

  • Steps that should replay (12 sites) wait with waitFor on the exact observable the next assertion reads: doSend's call count or its call arguments, or, in the StrictMode case, the stash being consumed (pendingRef.current null; the timer clears the stash and sends in the same task).
  • Steps that must NOT replay (2 sites: the c2 id change after a consumed send, and the targeted stash held by a mismatched pane) cannot use waitFor, because a waitFor on a negative passes before the replay has had its chance. They run through a new helper, actThenRunPendingTimers(step): fake setTimeout/clearTimeout only, act(step), then act(vi.runOnlyPendingTimers), then real timers again in a finally. Any replay timer the step armed has fired before the negative assertion reads.

No assertion is removed or loosened, there are no retries, and nothing is skipped. Where a waitFor now holds the first assertion of a block, that assertion has moved into the waitFor and is not repeated after it. RTL's asyncWrapper ends every waitFor with its own setTimeout(0) drain, so an exactly-once count read after a waitFor still sees any sibling timer that fired in the same tick.

Why the old wait raced: the hook arms the replay from a useEffect with setTimeout(0). In the conversation-create cases, setConversationId('conv-1') lands after await res.json() inside waitFor, where RTL has turned act off. React 19 then runs the remounted pane's passive effects as a later scheduler task, so on a starved runner the test's setTimeout(0) could be registered before the replay timer was armed. The test's timer then fired first, and doSend read 0 calls (queue run 45373444137, Test UI (7/16)). No product change is needed: the product's ordering is correct, and only the test's wait was too short.

Read first (by risk):

  1. actThenRunPendingTimers and its comment block near the top of the file: the only new mechanism. Check that it restores real timers in finally.
  2. mounting creates nothing; the first send creates one row and is replayed into it: the case that failed in the queue.
  3. the replayed first send survives the effect teardown StrictMode runs on mount: the wait moved from the abort check to stash consumption.

Tests

All local readings are on head c2c6bbd1223. Each section below folds its log excerpts.

30 repeated runs of the file: 30/30 green

bash os-verify-lock.sh -- bash loop.sh 30, where each iteration runs pnpm exec vitest run --maxWorkers=2 packages/app-shell/src/console/ai/__tests__/useDeferredFirstSend.test.tsx from ui/:

run 1 exit=0       Tests  15 passed (15)
...
run 30 exit=0       Tests  15 passed (15)
TALLY pass=30 fail=0 of 30
os-verify-lock: VERDICT command-exit 0
Under CPU load: 10/10 green (six busy loops on 4 cores, one fork, load average 9.4 to 14.1)

The setup mirrors the seat's diagnosis run: six busy loops (recorded PIDs, killed by trap), and vitest run --pool forks --maxWorkers=1 on the file, ten times, under the lock. Other agents' work was also running on the same box.

load before runs: 6.46 5.81 5.59
load-run 1 exit=0       Tests  15 passed (15) · loadavg 14.08
...
load-run 10 exit=0       Tests  15 passed (15) · loadavg 9.47
TALLY-LOAD pass=10 fail=0 of 10
os-verify-lock: VERDICT command-exit 0

A green run under load cannot prove the race is closed: the old file also passed 31 local runs. The mutation run below is the deterministic evidence.

One-off mutations (not committed): the new waits are conditions and the negative drain can fail

Both mutations ran through scripts/ablation-replace.mjs (anchor hit x1, blob changed, restore proven: blob == HEAD and git diff HEAD empty). They touch the product file only for the duration of the run.

Leg A: replay delayed from 0 ms to 50 ms (AiChatPage.tsx replay timer }, 0); changed to }, 50);). This is the queue failure made deterministic: the replay timer fires after any fixed setTimeout(0). The origin/main copy of this test file was run beside the new one, from a temporary sibling file that was deleted afterwards.

NEW_EXIT=0       Tests  15 passed (15)
BASE_EXIT=1       Tests  12 failed | 3 passed (15)
     × mounting creates nothing; the first send creates one row and is replayed into it
     × replays the first message across the id-keyed pane REMOUNT (reproduces the reported bug)
     ... (all 12 replay cases)

Leg B: the targeted-stash guard deleted (a mismatched pane now replays). This checks that actThenRunPendingTimers really fires the replay timer before the negative assertion reads, so the check cannot pass trivially:

NEW_EXIT=1       Tests  1 failed | 14 passed (15)
     × holds a targeted stash in a mismatched-agent pane, replays it in the matching one
AssertionError: expected "vi.fn()" to not be called at all, but actually been called 1 times
app-shell type-check: green (both tsc programs, edited file compiled)

pnpm exec turbo run type-check --concurrency=2 --filter=@object-ui/app-shell, under the lock. It builds the ^build closure first, then runs tsc --noEmit followed by tsc -p tsconfig.test.json.

@object-ui/app-shell:type-check: cache miss, executing 70efc8ea30bbbe0f
 Tasks:    69 successful, 69 total
os-verify-lock: VERDICT command-exit 0

tsc -p tsconfig.test.json --listFilesOnly lists the edited file (1 hit among 6941 files), so this reading covers it.

My first attempt, a bare pnpm --filter @object-ui/app-shell type-check without the @object-ui/* dists built, exited 2 on TS2307 "cannot find module" cascades. That was a missing build, not a type-check verdict, so I re-ran it through turbo as above.

Acceptance notes

  • The await act(async () => {}) lines in the three conversation-create cases ("let the mount-time resolve settle") are left as they are. They are act-scope flushes, not timer waits. On the cache-miss path the mount resolve runs synchronously inside the effect, so these lines race nothing. The assertion that follows them is a negative ("mounting creates nothing"), which no condition wait could express.
  • The full @object-ui/app-shell test suite (2416 files) is left to CI's Test UI (16 shards). Locally I ran only the edited file, plus the derived gate families below.

Verification

Two-minute check: from ui/, run pnpm exec vitest run packages/app-shell/src/console/ai/__tests__/useDeferredFirstSend.test.tsx. Expect Tests 15 passed (15).

Derived gate families: 27/27 run, all exit 0

The list comes from node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack: 27 commands for the one changed path. Each was run on c2c6bbd1223, with its exit code captured before any pipe. Reconciliation:

Run reconciliation — 27 derived, 27 run, 0 NOT-MEASURED, 0 UNRUN.
✓ dispatch-gates --ran: 27 derived famil(ies) accounted for — 27 run, 0 NOT-MEASURED (a DERIVED zero — all 27 recorded an exit code and none of them is 3).

pnpm --dir ui exec vitest run scripts/: Test Files 172 passed | 2 skipped (174), Tests 5129 passed | 2 skipped (5131).

No changeset: @object-ui/app-shell is private: true, and its files list (dist, src/styles.css, README, CHANGELOG, LICENSE) does not ship tests. The PR therefore carries skip-changeset.


Generated by Claude Code

…d tick

The replay of a deferred first send fires from a setTimeout(0) armed in the
pane's effect. The test's flushReplay() waited on its own setTimeout(0), which
races that effect whenever React flushes it outside act (the remount after the
async create): on a starved merge-queue runner the test's tick fired first and
`doSend` read 0 calls.

Every positive step now waits on the observable its assertion reads (waitFor
on doSend or the stash). The two no-replay steps, which have no observable to
wait for, run under fake setTimeout and fire every timer the step armed before
asserting. No assertion is weakened; no product change.

Claude-Session: https://claude.ai/code/session_01QSeKYpEsWWE9Y5YtSsDgqZ
Co-authored-by: Claude <noreply@anthropic.com>
@objectstack-fleet objectstack-fleet Bot added the skip-changeset PR has no user-facing published change; bypasses the changeset gate label Nov 24, 2026
@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

repo:objectstack#4 · os-justin · session_01QSeKYpEsWWE9Y5YtSsDgqZ

席位核对 — ACCEPT · L1 · head c2c6bbd122372bc624aa1ad9a4aa59e8a7b527a1 · 2026-11-24T15:58Z

  • Shape. Draft PR to main, first line Fixes #36967; one test file, ui/packages/app-shell/src/console/ai/__tests__/useDeferredFirstSend.test.tsx (+40/−36), inside the claim's surface. Test-only: skip-changeset is right (nothing published changes). Clause-②: no · Metadata-protocol: none.
  • Premise and fix. The fixed setTimeout(0) wait (flushReplay, 14 sites) raced the effect that arms the replay timer when React flushes it outside act (the pane remount after the async create). The 12 sites that should replay now waitFor the observable their next assertion reads (doSend count or arguments). The 2 sites that must NOT replay cannot use waitFor (it passes on a negative at once); they go through actThenRunPendingTimers (fake setTimeout/clearTimeout only, act(step), then act(runOnlyPendingTimers), real timers restored in finally), so every timer the step armed has fired before the negative is read. No assertion weakened, no retry, no skip.
  • Evidence. 30/30 green in a row; 10/10 green under CPU load (six busy loops on 4 cores, one fork worker). Leg A (replay timer delayed to 50 ms in AiChatPage.tsx, not committed): the new file stays green, the main copy fails 12 of 15 — the new waits are robust to the race the old ones lost. Leg B (targeted-stash guard deleted): exactly the negative case fails — the drain still catches a leaked replay. Restores blob-equal. app-shell type-check (source and test program) exit 0; 27 derived gate families exit 0.
  • Accepted deviation. The dispatch asked for condition waits everywhere; the 2 negative sites use the deterministic timer drain instead, for the reason above. The three await act(async () => {}) mount settles are microtask flushes, not timer waits, and stay.
  • Gates. Check-runs at this head: all green when this record was posted (pr-state.mjs --pr 37014).
  • Landing K. landing-k.mjs could not answer (no Test Core shard ran on this UI-only head); counted by hand: 8 first-parent commits on main since the merge base, K = 8 < 15.
  • No must-fix, no should-fix. After this merges, PR fix(core, console): the offboarding hand-over's owner follows each object's Change Owner switch #36869 (Offboarding hand-over: owner_id entries follow the object-level Change Owner switch (userActions.transfer) #35399) re-queues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/s skip-changeset PR has no user-facing published change; bypasses the changeset gate

Projects

None yet

2 participants