Mandatory-Human Sign-off Mechanism: one Pending record, any record type
engine/mandatory_human_signoff.py's SignoffStore implements Mandatory-Human Status: a status that a Gate's branch can set on a record so that the record waits for a person, instead of the branch resolving it itself. Resolving the record later is a separate, out-of-band action, not a resumed workflow execution.
The mechanism is generic across record types. The same store handles a Playbook entry draft and a case summary draft with no record-type-specific code, because both need identical behaviour from it.
The steps below set the status on a record, list what is waiting, approve or reject a record, and edit and resubmit one.
1. Set Mandatory-Human Status on a record
from engine.mandatory_human_signoff import SignoffStore
store = SignoffStore() # production default table name: pending_signoffs
store.ensure_schema()
record = store.submit(
"playbook_entry",
{"title": "SOP-42 lockout/tagout", "body": "..."},
submitted_by="ai-draft",
)
# record.status == PendingStatus.PENDING
A Sign-off Gate branch calls this instead of resolving its own output directly. The run that produced the draft completes normally either way; submit only persists the draft tagged Pending.
record_type and content are both opaque to this module. Whatever string and JSON-shaped payload the caller uses is round-tripped unchanged, and never inspected or branched on.
2. The pending-items view
store.list_pending() # every record still awaiting resolution, any record_type
store.list_pending(record_type="case_summary") # narrowed to one record_type
store.list_pending(
record_type="case_summary", start=window_start, end=window_end
) # + a time window
start and end filter submitted_at in the SQL WHERE clause, half-open [start, end). Either bound may be given alone. This is the same retrieval pattern that engine/shift_handoff.py and engine/time_ranged_activity_narrative.py share, and the same filtering shape that ActivityLog.query in engine/activity_log.py uses for occurred_at.
Over HTTP, the same view is the management API's sign-off routes in engine/management/signoffs.py:
GET /v1/signoffsandGET /v1/signoffs/{id};- the resolve and edit-and-resubmit
POSTs.
All of them need the signoff scope (see management-api.md), and they read through SignoffStore.list_page. In process, list_pending is the pending-items view, in the same way that assemble_order_trail in engine/order_trail.py is itself "the trail" and not a wrapper around a separate API layer.
The rest of the store's reading and bookkeeping surface:
list_page(limit=, after=, record_type=, status=, start=, end=)returns aPendingPage(records,next_cursor) of records of any status, oldest submission first, with keyset paging on(submitted_at, id). A cursor the store did not issue raisesInvalidCursorError.get(record_id)fetches one record whatever its status (orNone).get_many(record_ids)andstatuses(ids)fetch several in one query: the records that exist by id, or just the status of each.record_types()lists every distinct record type the table holds, of any status, in alphabetical order. It is what a record-type filter can offer as its choices.pending_summary()returns(count, earliest submitted_at)of Pending records without loading any content or creating the table ((0, None)when there is none).submit_within(conn, record_type, content, ...)sets Mandatory-Human Status inside the caller's own transaction, so a record and the caller's own row are one atomic change. The caller commits.supersede_pending(record_ids, actor=, action_id=)marks the Pending records among those ids Rejected-Superseded with no successor, along with any Pending successor an earlier edit made. Settled records are left as they are. It is for a record whose subject was replaced by something newer.drop_schema()drops the table. It is test teardown, not a production operation.
3. Approve or reject, recorded with who and when
import uuid
from engine.mandatory_human_signoff import SignoffDecision
resolved = store.resolve(
record.id, SignoffDecision.APPROVED, reviewer="alice", action_id=uuid.uuid4()
)
# resolved.status == PendingStatus.APPROVED
# resolved.resolved_by == "alice"
# resolved.resolved_at == the moment resolve() actually applied
One atomic UPDATE ... WHERE status = 'pending' is the whole mechanism. It is the same "let the database do the atomic part" principle that merge_attributes in engine/identity_resolution.py and claim in engine/trigger_queue.py already use.
The update wins for exactly one caller. Every other caller finds the row no longer pending and gets back its already-resolved state unchanged, which is a no-op and not a second transition. That covers a double-click retrying the identical action_id, and a second reviewer's own attempt racing the first.
resolve never raises for "already resolved". It raises only for a record_id with no row on file at all.
4. Edit & Resubmit, the third resolution option
successor = store.edit_and_resubmit(
record.id, {"title": "SOP-42", "body": "corrected"}, reviewer="alice", action_id=uuid.uuid4()
)
# the original: status == REJECTED_SUPERSEDED, resolved_by == "alice"
# successor: status == PENDING, predecessor_id == record.id, a fresh id
Rather than a flat reject-then-resubmit-fresh, edit_and_resubmit:
- marks the original
REJECTED_SUPERSEDED(not a flatREJECTED), and - creates a new Pending record that references the original as its
predecessor_id.
The audit trail keeps both, one superseding the other, so the edit history is not lost.
It is idempotent in the way resolve is, but only for a retry of the same action. Calling it again with the same action_id finds the original already REJECTED_SUPERSEDED by that action, and returns the successor already created for it instead of creating a second one.
Unlike resolve, it does raise if the original was already resolved by a different action. There is no sensible successor to return in that case, and a silent no-op would hide that someone else already acted on the record.
Pitfalls
resolveon an already-resolved record does not compare the new call against what was applied.- Whichever call wins the atomic
UPDATEdecides the outcome. Every other call gets that winning state back as a no-op, whether it is the same action retried or a different reviewer's conflicting decision racing it. - This is deliberate: first writer wins, with no comparison-and-raise step.
- The consequence is that a caller can't tell "my own retry" from "someone else resolved it first" by the return value alone, because both look identical. If the distinction matters, check
resolved_byandresolved_atagainst what you expect.
- Whichever call wins the atomic
submithas no natural key to deduplicate on. UnlikeIdentityStore.save'sreference_id-keyed partial unique index,SignoffStore.submitalways inserts a fresh row. A caller that must not submit the same draft twice (for example, a retried Task run re-drafting the same content) owns that idempotency itself. This module does not treat "same content" as "same submission".edit_and_resubmitmarks the original and inserts its successor in one transaction, with one commit.- Both writes land together or neither does, so a crash between them can never leave the original
REJECTED_SUPERSEDEDwith no successor to retry onto. - Two concurrent calls carrying the same
action_iddon't race into that gap either. Postgres's row lock on theUPDATE ... WHERE status = 'pending'blocks the second transaction until the first commits. The second then either wins the transition itself (and the first lost instead), or sees both the transition and its successor already committed. It never sees one without the other. - The two
concurrenttests intests/test_mandatory_human_signoff.pyexercise both shapes of that race directly: the sameaction_idracing itself, and two different reviewers' actions racing each other. They use the samethreading.Barrier-synchronized pattern as the concurrency tests ofengine/identity_resolution.pyandengine/trigger_queue.py.
- Both writes land together or neither does, so a crash between them can never leave the original
engine/identity_resolution.py's ownPENDINGIdentityRecords are not stored here.- Identity Resolution's inferred branch sets its own Pending status directly on
identity_records, not throughSignoffStore. - This module is the mechanism that the Playbook's and case triage's drafts call into. It is not a retrofit of every existing Mandatory-Human Status instance onto one shared table.
- A person's decision on one of those identity records is
IdentityStore.resolve_pending, which has the same first-writer-wins shape asresolvehere (seedocs/operations/identity-resolution-and-reconciliation.md, section 5).
- Identity Resolution's inferred branch sets its own Pending status directly on
Release: v0.11.0