Skip to content

agentHost: fix flaky chat movability test racing a background catalog write - #342256

Merged
Alexandru Dima (alexdima) merged 2 commits into
mainfrom
agents/investigate-vscode-test-intermittent-failure
Oct 17, 2026
Merged

Alexandru Dima (alexdima) merged 2 commits into
mainfrom
agents/investigate-vscode-test-intermittent-failure

Conversation

@alexdima

@alexdima Alexandru Dima (alexdima) commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

cc Sandeep Somavarapu (@sandy081)


This fixes an intermittent failure of a node unit test. No issue is filed for it. The change is test-only; no product code changes.

Test: AgentService (node dispatcher) › peer chat catalog persistence › chat moves (Block 4) › "a chat's movability follows the events of the services that hold its session's state, and a refused move keeps its reason" in src/vs/platform/agentHost/test/node/agentService.test.ts.

Failure

It failed once in about 11 runs of the whole file on a busy machine, in the "parked" step. After _catalogReconciliationService._park(...), the chat was still reported as movable:

  parked: {
+   parked: true,      <- actual
-   parked: false,     <- expected
    released: true
  },

Root cause

The movability update itself is not slow. _park fires onDidChangeParkedSessions synchronously, and AgentHostChatMovability recomputes movable inside that listener, reading isParked live. So parked: true means the session was not parked when the test read it. That happens because _park itself declined, or because something woke the session right after.

Both come from a background catalog write that the test never awaits:

  1. The earlier steps (worktree, conversion) flip the chat's movable flag. That changes the session summary, and SessionSummaryNotifier flushes summary changes on a 100 ms debounce.
  2. The flush reaches the onDidChangeSessionSummary listener in AgentService, which calls _queueCatalogSync(source) and starts a background catalog write (_drainBackgroundCatalogStateWrites → _persistListVisibleSessionStateNow).
  3. That write bumps the session's payload dirty marker twice, in synchronizeWithFactory. In this test it also ends pending (acknowledgementSuperseded), so it then calls _markCatalogPayloadDirty. That bumps the marker again and calls wakeParkedSessions(source).
  4. The test reads the dirty marker and passes it to _park. _park re-reads the marker and, by design, declines as superseded when it changed. A wake that lands after a successful park releases the session just the same.

Normally the 100 ms flush fires after the test finished. When the machine is busy and the steps take longer, it fires mid-test, and the write can overlap the parking step.

Evidence

  • Deterministic repro: with a temporary 150 ms wait between the test's dirty-marker read and _park, the single test fails every time with exactly the diff above. Logging showed observedDirty: 6, currentDirty: 9 and _park returning { status: 'retry', reason: 'superseded' }. Stacks showed two bumps from _drainBackgroundCatalogStateWrites → _persistListVisibleSessionStateNow and a third from _markCatalogPayloadDirty.
  • Control: without the wait, both reads see the same value and the session parks (reason: 'sourceUnresolvable').

Fix

Just before parking, the test flushes the session's dirty summary through the normal path and waits for the catalog write that it queues:

stateManager.prepareSessionSummariesForListing([stateManager.getSessionSummary(source.toString())!]);
await service.whenCatalogReconciliationIdle();
  • prepareSessionSummariesForListing flushes a published session's dirty summary right away, as a listing does. The onDidChangeSessionSummary → _queueCatalogSync write is queued now instead of 100 ms later. When the debounce timer fires later, it finds nothing new to emit. The same file already uses this in its unloadSession helper.
  • whenCatalogReconciliationIdle() then waits for that write, including the dirty-marker bump and wake from its pending result, so all of it lands before the test reads the marker and parks.
  • Both lines are needed. With a 150 ms wait forced before _park, the drain alone still fails the same way: the debounced write lands in that window.

The test also records _park's outcome reason in its snapshot. If this regresses, the failure will say reason: 'superseded' rather than only parked: true.

No product behavior is disabled. An earlier revision of this PR replaced _queueCatalogSync with a no-op; this revision replaces that.

This is a test-only fix. When parking races a concurrent catalog write, the product code declines or re-wakes on purpose, so a mutation is never hidden behind a parked session.

Why the write ends pending here

The background write returns pending (acknowledgementSuperseded), which adds the third dirty-marker bump and the wake. This is very likely a fixture artifact, not a product bug. setupMove() without perSessionData shares one TestSessionDatabase, and so one catalog sync snapshot, across all sessions and chat storage. So writes for the target or peer can supersede the source's acknowledgement, which can't happen with one database per session in production. The fix doesn't depend on this.

How to test

npm run build-fast -- --client-only
npm run test-node -- --run src/vs/platform/agentHost/test/node/agentService.test.ts

Note: the node runner ignores --grep, so this runs the whole file. To run only this test, temporarily mark it test.only.

Validation

  • Single-test runs: with the fix, it passes with no wait, and with a 150 ms wait between the dirty-marker read and _park. Control: the drain alone with the same wait fails with reason: 'superseded', parked: true.
  • Whole file: npm run test-node -- --run src/vs/platform/agentHost/test/node/agentService.test.ts passed: 1134 passing, 16 pending.
  • Lint: npm run eslint -- src/vs/platform/agentHost/test/node/agentService.test.ts passed.
  • Not run: a full typecheck-client.

… write

The "movability follows the events" test parks the source session and
expects its peer chat to stop being movable. The earlier steps of the test
flip the chat's movable flag, which marks the session summary dirty; its
debounced (100 ms) flush queues a background catalog write for the session.
On a slow machine that write can overlap the parking step: it bumps the
session's payload dirty marker, so `_park` declines as superseded, and when
it ends pending it also wakes the session. Either way the chat stays movable.

Stop queueing background catalog writes and drain those in flight before
parking, so the dirty marker the test passes to `_park` stays current.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 17, 2026 09:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The narrow test-only fix addresses both late and in-flight writes without weakening the movability assertions.

No open issues 🎉

What changed in this PR

Stabilizes the chat movability unit test by preventing background catalog writes from racing its parking assertions. No product code changes.

Changes:

  • Stops new catalog writes before parking.
  • Waits for in-flight writes to finish while preserving existing assertions.

No actionable findings remain.

File Description
src/​vs/​platform/​agentHost/​test/​node/​agentService.test.ts Adds catalog-write isolation and draining before the parking step.

Review effort: Balanced

…he movability test

Rather than replacing `_queueCatalogSync` with a no-op, flush the session's
dirty summary through `prepareSessionSummariesForListing` (as a listing does),
which queues its catalog write now instead of after the 100 ms debounce, and
drain it before parking. The test also records `_park`'s reason, so a future
regression reports `superseded` instead of only `parked: true`.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@alexdima
Alexandru Dima (alexdima) merged commit 70cda9d into main Oct 17, 2026
86 of 87 checks passed
@alexdima
Alexandru Dima (alexdima) deleted the agents/investigate-vscode-test-intermittent-failure branch October 17, 2026 18:15
@vs-code-engineering vs-code-engineering Bot added this to the 1.143.0 milestone Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants