Repository navigation
feat(transducer): shared work lease and bounded settle-and-reconcile - #2902
Conversation
Add openadapt/transducer_concurrency.py with two local runtime primitives for workers that share one queue of items: - SharedWorkLease: a claim lease keyed by (app, canonical item key) in a SQLite file in WAL mode. Claims run in one BEGIN IMMEDIATE transaction, so exactly one concurrent worker wins. PENDING claims carry a TTL renewed by heartbeat; COMMITTED is terminal (later workers skip); RELEASED can be claimed again. Each takeover bumps a fence number, and heartbeat, commit and release only succeed for the current fence, so a holder that resumes after its TTL passed cannot commit over the new holder. A claim marked uncertain is never taken over until it is resolved. - BoundedSettleAndReconcile: runs one submit, never repeats it on an uncertain outcome (dropped response, proxy timeout), marks the lease uncertain, then polls a read-back with bounded backoff. Returns COMMITTED when the record shows up, RETRY_SAFE only when nothing could have been saved, and COMPLETED_UNVERIFIED otherwise. Cooperates with FormCommitLatch through a new latch_now() method. Wiring: SimplexTransducer and openadapt.wrap accept work_lease=. An episode claims the task's work_item_key before its first commit is released, waits (guard WORK_LEASE_HELD) while another worker holds it, stops with WORK_ITEM_ALREADY_COMMITTED when it is done elsewhere, heartbeats each step and commits the lease when a commit is verified. Without work_lease, behavior is unchanged. Tests: tests/test_transducer_concurrency.py (26 tests), including an 8-process and a 16-thread race (one winner per item, zero duplicate saves), TTL takeover after a real process crash, fence rejection of a stale holder, and dropped-ack / async-save settle cases.
A worker that stalled past its TTL could still run submit() on an item another worker had taken over. Heartbeat the claim right before submit and return WAIT if it was lost. The stale-holder test now asserts no submit.
Independent adversarial review: REQUEST CHANGESFrom: Grok The lease itself is sound on the paths the tests cover: The class this PR names is "the same item is submitted twice": by two workers, after a lost response, or after a crash and takeover. Three holes still let a second submit through. 1. A submit that outlasts the TTL can be taken over and sent again. 2. An unknown pre-submit read-back still sends. That same 3. After a takeover, Generality gate. Point 1 passes: no application names or branches; keys come from the task. Point 2 fails for the class above. Point 3 (held-out live runs) is not shown, as the PR says. CI at this head. I didn't push to this branch, and I won't merge it. |
…firmed absence Smoke failed on a new swallow_wide verdict fold: the submit handler caught BaseException and fell through with a status from an opaque call. The handler now catches not_sent_errors narrowly and sets SubmitStatus.UNKNOWN for any other exception (renamed from UNCERTAIN to match ReadBack.UNKNOWN). Also addresses the Grok review at 7b8236d: - Mark the claim uncertain before submit(), so a send that outlasts the TTL cannot be taken over and sent again by another worker. - Send only when the pre-submit read-back shows the record is ABSENT. An UNKNOWN, unparseable or raising read-back ends with the new UNCHECKED outcome, sends nothing and releases the lease. - WorkLeaseHooks.gate heartbeats a cached claim before each later commit and stops the episode with WORK_LEASE_LOST once the claim was taken over.
Independent adversarial review: APPROVEFrom: Grok Re-review of the push since my REQUEST CHANGES at 1. Fixed. 2. Fixed. With 3. Fixed. Smoke verdict-fold. Tests at this exact head (Linux, Python 3.12, Generality gate. Point 1 passes: no application names or branches; keys come from the task. Point 2 now passes for the class this PR names (the same item submitted twice: by two workers, after a lost response, or after a crash and takeover) on the settle path and on later transducer commits. Point 3 (held-out live runs) is not shown, as the PR says. Limitation, not a blocker. The transducer still releases the first commit while the lease is CI at this head. I didn't push to this branch, and I won't merge it. |
What this adds
openadapt/transducer_concurrency.py, two local runtime pieces for workers that share one queue of items:SharedWorkLease: a claim lease keyed by(app, canonical item key)in a SQLite file in WAL mode. A claim runs in oneBEGIN IMMEDIATEtransaction, so exactly one concurrent worker (thread or process) wins.PENDING(with a TTL that the holder renews by heartbeat),COMMITTED(terminal; later workers skip the item) andRELEASED(anyone may claim it again).BoundedSettleAndReconcile: runs one submit and never repeats it after an uncertain outcome, such as a dropped response or a proxy timeout. While settling it holds the lease as uncertain. It then reopens the record (read_back, the check a careful operator would do) with bounded backoff. Results:COMMITTED: the record showed up.RETRY_SAFE: only when the request was never sent, or when the caller setsabsent_is_finaland every poll saw the record missing.COMPLETED_UNVERIFIED: anything else.Unknown read-backs (page error, 403) never count as missing. It works with
FormCommitLatchthrough a newlatch_now()method, so the same content on the same control is blocked afterwards.Wiring
SimplexTransducer(..., work_lease=)andopenadapt.wrap(agent, work_lease=). An episode claims the task'swork_item_key(oritem_key) before its first commit is released. While another worker holds the item, the commit is replaced by awaitaction (guardWORK_LEASE_HELD). If another worker already committed the item, the episode stops withWORK_ITEM_ALREADY_COMMITTED. The claim is renewed every step and committed when a commit is verified.BoundedSettleAndReconcileis exported and works with the same lease and latch. The code that actually sends the submit (the harness or tool executor) calls it; the transducer does not, because the transducer releases actions and does not send them.wrap(fn, work_lease=...)for a plain function raisesTypeErrorrather than silently ignoring the lease.work_lease, behavior is unchanged. All new parameters are optional keyword arguments.Tests
tests/test_transducer_concurrency.py: 26 tests, all passing in 3 repeat runs (about 3 s each).os._exitwhile holding a claim), plus fence rejection of a stale holder's heartbeat, commit and release.COMMITTED, 1 submit. A second attempt during the settle window isBLOCKEDand another worker getsHELD_UNCERTAIN.COMPLETED_UNVERIFIED, with no resubmit from this worker or any other, even after the TTL.Existing suites run locally: 605 passed across
test_simplex_transducer.py,test_openadapt_wrap.py,test_wrap_command.pyand everytest_transducer_*.py.test_wrap_receipt_trust.pyfails collection on my machine because of a stale installedopenadapt_types(No module named 'openadapt_types.receipt'). That is not related to this change; CI should run it.ruff checkandruff format --checkpass.mypyreports no errors in the new module or test file and none added elsewhere: 41 before and after, all in existing code.Generality gate
Durable item: safe writes and checking effects the way a careful operator would. These pieces keep repeated or concurrent agent saves from creating duplicate records, whichever model drives the agent.