Skip to content

agentHost: roll back a failed artifact persistence - #342137

Merged
Sandeep Somavarapu (sandy081) merged 3 commits into
mainfrom
agents/rollback-failed-artifact-removal
Oct 16, 2026
Merged

Sandeep Somavarapu (sandy081) merged 3 commits into
mainfrom
agents/rollback-failed-artifact-removal

Conversation

@sandy081

Copy link
Copy Markdown
Member

Follow-up to #341001, #342060 and #342096.

Problem

Since Block 3a (#341626), a list-visible write (_persistListVisibleSessionStateNow) first writes its owned values to the session state store, which mirrors them to session.db immediately. Only then does it synchronize the session catalog. If that synchronization fails, the owned write stays.

For a failed artifact removal, central state and session.db no longer have the artifact, while the published state and the catalog still list it. After a restart it would be gone, even though the user saw the removal fail. The 'logs and propagates…' test encoded that inconsistent state.

Fix

Roll back the owned write when the catalog synchronization fails, for callers that publish only after persisting:

  • _persistOrderedListVisibleSessionState(..., { rollbackOnFailure }) threads the caller's own overrides into _persistListVisibleSessionStateNow.
  • There, _swapOwnedSessionValues writes the owned values in one exclusive section of the store and records what they replaced.
  • If the synchronization throws, _rollBackOwnedSessionValues restores the previous value of each key that still holds this write's value, or the value its synchronization adopted. It uses the store's exclusive section and an Immediate mirror, so session.db is restored before the error propagates. A newer write (another caller, a later mutation) is left alone.
  • Artifact persistence opts in. It is the only caller that publishes strictly after persisting and propagates the failure to the user.

Scope audit of the seam

All owned writes in _persistListVisibleSessionStateNow share the path, but only callers that persist before publishing need rollback:

  • Artifact persistence (SessionArtifacts.mutate: tool additions and user removals) publishes only after persisting, so it opts in.
  • Background writes (_queueCatalogSync, _drainBackgroundCatalogStateWrites) persist state that is already published, so rolling back would diverge from it. They roll forward through catalog reconciliation, as before.
  • Already-published writes don't need it. That covers titles (rename from a tool, retained import title, promoted-session title, generated surfaced title), working directories (_persistListVisibleSessionState), adoption metadata, chat removal and creation, and catalog restores with {} overrides. Each persists state the caller has already applied to the state manager, or carries no owned values.

Crash safety

If the process crashes between the owned write and its rollback, it recovers forward to one consistent state, never removed-in-one-place and published-in-another:

  • The store's central write (writeSessionState) marks the catalog payload dirty in the same agent-host.db transaction.
  • On restart, session.db's mirror is repaired from central state.
  • The startup reconciliation pass re-projects the dirty catalog from central state.
  • Listing, restore, central state, session.db and the catalog all agree on the removal.

The rollback itself is a single central write, with the same guarantees.

Tests

  • 'logs and propagates…': after the failure, published state, central state, session.db and the catalog all equal the pre-removal state. A retry then persists everywhere.
  • New 'a failed removal stays consistent when a background write lands while it is persisting': a summary change is flushed while the failing removal persists. Afterwards the artifact is in central state, session.db mirrors central state, the catalog still has it, and a retry persists.
  • New 'a crash between the owned write and its rollback recovers to one consistent state': a restarted service shares agent-host.db and session.db while the removal's catalog write is in flight. Listing, restore, central state, session.db and the catalog all agree.

Validation

  • Mutations:
    • no rollback (drop rollbackOnFailure): 'logs and propagates…' fails, with central and persisted missing the PR;
    • rollback without the session.db mirror (Coalesced plus noteMirrored): 'logs and propagates…' fails, with persisted missing the PR.
  • npm run transpile-client: passed.
  • mocha test/unit/node/index.js --delay --ui=tdd --exit --run src/vs/platform/agentHost/test/node/sessionArtifactRemoval.test.ts: 14 passing, and 200/200 consecutive runs passed after rebasing onto main.
  • agentHost node suite (all 357 src/vs/platform/agentHost/test/node/**/*.test.ts files): 11644 passing, 75 pending, 14 failing. All 14 are in copilotAgent.test.ts and identical without this change (750 passing, 14 failing), so they are pre-existing.
  • npx tsc --noEmit -p src/tsconfig.json: the only 2 errors are in copilotSdkManagedSettings.integrationTest.ts (ensembleTier), and they are identical on origin/main.
  • npm run eslint and build/hygiene.ts on the changed files: passed, and the pre-commit hygiene hook passed.

A list-visible write first stores its owned values in the session state
store (and session.db's mirror), then synchronizes the session catalog.
When that synchronization failed, the owned values stayed: a failed
artifact removal left central state and session.db without the artifact
while the published state and the catalog still listed it, so a restart
would have hidden it.

Ordered writes whose caller publishes only after persisting can now roll
back. The owned values are swapped in one exclusive section of the store,
which records what they replaced; if the synchronization throws, each key
that still holds this write's value (or the value its synchronization
adopted) gets the previous value back, mirrored to session.db before the
error propagates. A newer write is left alone. Artifact persistence opts
in; writes of already-published state still roll forward through catalog
reconciliation.

A crash before the rollback recovers forward: the owned write marks the
catalog payload dirty in the same agent-host.db transaction, session.db's
mirror is repaired from central state, and the startup reconciliation pass
re-projects the catalog.

Co-authored-by: Copilot <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 16, 2026 18:00
@sandy081 Sandeep Somavarapu (sandy081) added this to the 1.143.0 milestone Oct 16, 2026
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Screenshot Changes

Base: fb429118 Current: 567089b9

Changed (1)

sessions/sessionActivityBar/sessionActivityBar/SubmenuOpen/Light
Before After
before after

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.

🟡 Changes recommended

Post-commit failures can leave catalog state inconsistent or discard the previous values needed for rollback.

2 open findings
What changed in this PR

Adds rollback for failed artifact persistence so unpublished removals do not remain durable, following the earlier catalog-mirroring fixes.

Changes:

  • Enables conditional rollback of artifact-owned session values.
  • Adds failure, background-write, and crash-recovery regression tests.
File Description
src/​vs/​platform/​agentHost/​test/​node/​sessionArtifactRemoval.test.ts Checks consistency across published state, central storage, session.db, and catalog.
src/​vs/​platform/​agentHost/​node/​agentService.ts Captures previous owned values and restores them after synchronization failures.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

roblourens
roblourens previously approved these changes Oct 16, 2026
…t failures

Two review findings on the rollback of a failed artifact persistence:

- The values to restore were recorded only once the owned write's
  exclusive section resolved. That section commits central state and
  session.db, then records the mirror acknowledgement, which can still
  reject. The previous values were then lost and a later catalog failure
  skipped the rollback, leaving the failed removal durable. Record them in
  caller-owned state inside the exclusive section, before the write; the
  compare-and-restore rollback stays a no-op if the write never committed.

- A receipt acknowledgement that failed after the catalog upsert committed
  was thrown, so the owned values were rolled back while the catalog and
  session.db's pending receipt kept the removal. After a restart,
  reconciliation replayed that receipt and marked the payload clean, so the
  listing omitted an artifact that restore, central state and session.db
  still had. The catalog sync now reports such a failure as pending
  ('acknowledgementFailed'), as it already does for a superseded
  acknowledgement: the mutation is committed, the caller publishes it, and
  reconciliation acknowledges the receipt. Rollback is thus limited to
  synchronizations that did not commit the catalog.

Co-authored-by: Copilot <[email protected]>
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.

4 participants