Skip to content

fix(compute): retry watch-driven sandbox updates after CAS conflicts - #5960

Draft
EmilienM wants to merge 1 commit into
ai-openshell:mainfrom
EmilienM:fix/5959-watch-apply-retry/EmilienM
Draft

EmilienM wants to merge 1 commit into
ai-openshell:mainfrom
EmilienM:fix/5959-watch-apply-retry/EmilienM

Conversation

@EmilienM

Copy link
Copy Markdown
Contributor

Summary

Watch-driven sandbox updates performed a single CAS against the sandbox record, so a concurrent write between the update's version read and its CAS failed the update and the watch loop dropped the event with only a warning. This PR retries the update a bounded number of times on a conflict. Each pass re-reads the record and revalidates the snapshot; when the retries run out, the update defers to reconciliation.

Related Issue

Closes #5959

Found while investigating #5954. That issue's invisibility symptom is a separate problem: scoped reads are stateless SQL over columns the watch CAS never touches, and providers and services the watch loop never writes fail the same way. The analysis there points at a masked authorization failure or a workspace-column divergence; pinning it needs the objects.workspace column value for the failing rows, the client's principal flavor from the gateway logon records, and any OCSF authz-denial findings during the NOT_FOUND window. The dropped-event WARN from that report is what this PR fixes.

Changes

  • apply_sandbox_update retries a PersistenceError::Conflict up to 3 times, re-entering the full update so each attempt re-reads the record; every retry forces the revalidation path (live driver read), so a stale snapshot cannot overwrite a newer gateway-owned transition such as a concurrent stop or a Starting-to-Ready promotion
  • A retry never blocks on the sandbox's lifecycle gate: it takes the gate without waiting and defers to reconciliation when a lifecycle operation holds it, keeping the sequential watch loop moving
  • Failures other than a CAS conflict still surface immediately; the session cleanup that runs after a successful CAS is never retried
  • After the bound, the update defers to the reconciliation sweep with a warning naming the sandbox, the current resource version, and the attempt count
  • The snapshot is borrowed through the update chain, so the conflict-free path pays no clone
  • Replaces the inaccurate "the next watch event will naturally retry" comment on the update path
  • Follow-ups (not in this PR): a gateway metric for watch-apply conflicts and retries; the reconcile sweep currently stops at the first per-sandbox error, which weakens the exhaustion fallback for a persistently contended sandbox

Release Notes

Sandbox updates from a compute driver's watch stream now retry when a concurrent write to the same sandbox record makes their store update fail, instead of being dropped with a warning. On drivers that do not re-emit unchanged sandboxes, a dropped update previously left the gateway's record stale until the sandbox changed state again.

Testing

  • Checks appropriate to the affected code and behavior pass
  • Unit tests added/updated (if applicable)
  • E2E tests added/updated (if applicable)

cargo fmt --all --check; cargo clippy --workspace --all-targets --features experimental -- -D warnings; cargo clippy -p openshell-server --all-targets --features test-support,experimental -- -D warnings; full cargo test -p openshell-server --features test-support,experimental; cargo deny check licenses bans sources; the PostgreSQL-backed server lane. Twelve new tests in compute/tests/error_recovery.rs cover: convergence after one conflicting write, revalidation against a concurrent stop, retention of a concurrently promoted Ready row against a stale terminal-failure event, the exhaustion deferral, non-conflict errors surfacing without retry, cleanup-after-CAS failures, the conflict error text at the reconcile call site, head-of-line behavior through the real watch loop, a conflict during the Starting-phase gated path with driver re-query, deferral when the lifecycle gate is held, and the session-probe failure path.

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)
  • Architecture docs updated (if applicable)

A driver snapshot for an existing sandbox applied through the watch loop
pinned the record version it had just read and performed a single CAS.
Any concurrent write to the same record between that read and the CAS,
such as the supervisor session state written while the sandbox comes up,
failed the update with a Conflict, and the watch loop dropped the event
with only a warning. Drivers that do not re-emit unchanged sandboxes
then kept a stale record until the sandbox changed state again or the
reconciliation sweep caught up.

Re-enter the full update for a bounded number of attempts on Conflict,
so each pass re-reads the record and re-runs the ambiguous-snapshot
revalidation; a stale snapshot can no longer overwrite a newer
gateway-owned transition such as a concurrent stop. Non-conflict
failures still surface immediately, and after the bound the update
defers to reconciliation with a warning instead of blocking the
sequentially processed watch stream.

Closes ai-openshell#5959

Signed-off-by: Emilien Macchi <[email protected]>

This branch has not been deployed

No deployments
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.

bug: watch-driven sandbox updates are dropped after a CAS conflict

1 participant