Skip to content

fix(orchestration): exactly-once concurrency seat release - #1101

Open
isadeks wants to merge 2 commits into
mainfrom
fix/1003-exactly-once-seat-release
Open

isadeks wants to merge 2 commits into
mainfrom
fix/1003-exactly-once-seat-release

Conversation

@isadeks

@isadeks isadeks commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

The seat release from #1009, rebased onto current main and split out of #1086 as the reviewer asked. The change is the same; the emulator tests that recorded these defects as known failures are now plain tests.

Area

  • cdk — infrastructure, handlers, constructs
  • agent — Python runtime / Docker image
  • cli — bgagent client
  • docs — guides or design sources (docs/guides/, docs/design/)
  • tooling — root mise.toml, scripts, CI workflows

Related

Closes #1003. Replaces #1009 (closed when it was folded into #1086).

Each task now holds a reservation marker (concurrency_slot: held → released, versioned). Taking and returning a seat is one DynamoDB transaction with the per-user counter, so a seat is released exactly once, under retries, finalizer replays, cancel/complete races and lost replies alike.

Changes

  • shared/task-concurrency.ts: acquireTaskSlot / releaseTaskSlot, transactional with the counter, and conditioned on the task's own marker.
  • orchestrator.ts:
    • admission uses acquireTaskSlot;
    • finalizeTask releases in finally;
    • a task still in an active state throws instead of releasing;
    • failTask rethrows when the task isn't terminal;
    • the old decrementConcurrency is removed.
  • AWAITING_APPROVAL poll timeout now goes to FAILED. On main, AWAITING_APPROVAL → TIMED_OUT is an invalid transition, so the task stayed active while its seat was freed.
  • orchestrate-task.ts: consistent reads, plus release steps on the early-return paths. Only new paths are added, so in-flight durable executions replay unchanged.
  • reconcile-concurrency.ts, reconcile-stranded-tasks.ts and the admission queue: they repair drift through the marker and never release another task's seat. reconcile-stranded-tasks no longer decrements the counter directly.
  • Behaviour change: confirm-uploads no longer reserves capacity at submit. The orchestrator owns admission, so an upload-confirmed task over the cap is queued like any other submission, instead of rejected with 429.
  • ORCHESTRATOR.md: the new scheme, plus an upgrade note.

Upgrade

Drain in-flight tasks before deploying. Tasks admitted before this change have no marker; they can't release another task's seat, and their user's count is treated as ambiguous until they finish.

Tests

  • CDK full suite on current main: 9,625 passed, 57 skipped.
  • Emulator suite (mise run test:integration tier, cdk): 29 passed, including the three seat tests. Two of them were test.failing witnesses for this defect on main (a replayed finalize releasing a second seat, and the reconciler dropping the seat of a task awaiting approval) and now pass as plain tests. The leaked-seat test writes the leak to the counter directly, because admission now ties every seat to a task record.
  • DynamoDB Local race suite (task-concurrency-local.test.ts): 15/15 as in fix(orchestration): exactly-once concurrency seat release #1009.
  • Lint, drift checks, the CI SAST range and masking scans: clean.

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.

Per-user concurrency was a counter incremented on admission and
decremented on completion; retries and races (finalizer replay, cancel
vs completion, a lost DynamoDB reply, the stranded-task reconciler) could
release a seat twice or never.

- Each admitted task carries a concurrency_slot marker (held -> released).
  acquireTaskSlot / releaseTaskSlot (shared/task-concurrency.ts) change the
  marker and the counter in one transaction: acquire needs SUBMITTED with
  no marker and room under the cap; release needs a held marker on a
  terminal task. Lost replies are recovered from the marker; transaction
  conflicts retry with bounded jitter.
- Admission, finalization (release in finally), failTask, the orchestrator's
  early-return paths and the stranded-task reconciler all release through
  the marker; no handler decrements active_count directly. confirm-uploads
  no longer reserves at submit; the orchestrator owns admission.
- A held task can never be re-queued (SUBMITTED -> QUEUED and the admission
  queue require no marker).
- An approval wait that outlives the poll window finalizes to FAILED. It
  used to attempt the invalid AWAITING_APPROVAL -> TIMED_OUT, leave the task
  active and still free its seat. A finalization that cannot reach a
  terminal state now throws and is retried, keeping the seat.
- The concurrency reconciler reads counters, then strongly reads every
  reservation, repairs only under the observed reservation_version, and
  releases terminal reservations left by interrupted cleanup. Users with
  unmarked active tasks (admitted before this change) are skipped until
  they settle.
- DynamoDB Local race suite (15 cases) runs when ABCA_DDB_LOCAL_ENDPOINT is
  set; ORCHESTRATOR.md documents the scheme and the upgrade/drain note.

Rebased onto main after the Lambda MicroVM approval lifecycle (#904): the
MicroVM supervisor-failure path no longer decrements the counter itself.
finalizeTask keeps the worker-ownership check (a changed owner neither
mutates the task nor releases its seat) and releases in `finally`; the
MicroVM finalization tests assert the release instead of the decrement.

(cherry picked from commit adb9b2c)
…in tests

The replayed-finalize double release and the reconciler that dropped the
seat of a task awaiting approval were test.failing witnesses until this
fix. The leaked-seat test now writes the leak to the counter directly:
admission ties every seat to a task record.

(cherry picked from commit 2487167)

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.

fix(orchestration): concurrency seats can be double-released or leaked

1 participant