Repository navigation
feat(flow): per-run accounting ledger for any executor - #2478
Conversation
One value-free openadapt.run-ledger/v1 row per run (agent, compiled program, third-party agent or person) with the VERIFIED / ESCALATED / CAUGHT_ERROR / FAILED / UNKNOWN outcome rule, intervention intervals, model usage (unknown is null), field checks and read-back tier. Values stay in a local 0600 sidecar. Collectors read the files agent guard, verify-run, compiled runs, wrap, item approvals, the task ledger and compile-agent already write, plus the shadow, flight, gate and exception contract files. New verb: openadapt-flow ledger add/list/show/summary/ export/push.
Merge main. Register the ledger verb without importing the types schema, lock the store with the shared flock/msvcrt helper, refuse an export when the scrubber cannot run, list the verb as 'inspect ledger' in both task tables, set RunLedgerOutcome aside in outcomes.json, and keep __main__.py within its line budget.
Join the deferred and ledger registrations to the admit/check/api block so __main__.py stays at its base size.
…eption files The end-to-end run on 2026-11-21 found every join broken. The ledger now reads what each producer really writes, from committed copies of their synthetic artifacts: - Readiness shadow runs (J1): a folder with a readiness run.json is read as one source (run.json, proposed_write.json, case.json, usage.json, egress.json). Never VERIFIED; a match is ESCALATED (shadow_matched); a mismatch or wrong record is CAUGHT_ERROR; no case.json is UNKNOWN. The shadow hold's guard_ledger_row.json is ignored. - Guard rows (J3): the row's verdict is no longer a bucket source, and fields_differing is read as a list of names. - A person declining is ESCALATED (person_declined), not CAUGHT_ERROR or unmapped_outcome, unless a refuted read-back or field diff shows an error. - An effect receipt's REFUTED counts as a catch only when the store shows a write, as the guard's own verdict rule does. A halt before the save and a save that never landed are no longer CAUGHT_ERROR. - P6 exception answers (J4) are read from $OPENADAPT_HOME/exceptions/<run_ref> (ledger add --run-ref). - P7 fallback results and dispatcher envelopes (J5) have a reader; a refused gate is a decline, the nested agent attempt isn't read again. - A failed, timed-out or errored run with no evidence nothing was sent is UNKNOWN (write_unconfirmed), not FAILED. - export and push let through field names the run's guard task, verify.yaml or readiness reference declares (patient_id); any other name the scrubber rewrites still refuses. - Real push --dry-run output is committed as packages/types/tests/fixtures/agent_run_summary_v1_examples.jsonl and validated against the JSON Schema.
- The dispatcher envelope is WrappedTask.call's result plus finish()'s outcome and fallback, with no replay or compile. The reader takes the record check from the envelope's fallback, and the fixtures are rebuilt with P7's own _result and finish. - A wrap run record whose result path is agent_fallback reads like an agent's first run. - A guard ledger row is never a bucket source (J3); its differing fields show as "differs" and summary.json decides. - A FAILED_SCOPE readiness case, or one with no proposal, declares none of the proposed field names, and a guard task for another task id declares nothing. - No exception folder is guessed from the run directory's name; P6's run reference is only given with --run-ref. - The real-scrubber test skips when the scrubber can't run.
… egress - The shadow fixtures are now P1's committed samples (matched, mismatched, extra-target) and P3's real scrubbed-mode egress.json. - run.json EXTRA_TARGET is UNKNOWN (shadow_extra_target) and EGRESS_REFUSED, or egress.refused, is UNKNOWN (egress_refused). A healthy --egress blocked run, which also says mode "blocked", stays as compared. - run.json's egress copy is read when there is no egress.json. - A row with no person intervals has intervention_s null (unknown) unless a source shows nobody was asked: a shadow hold, a not_gated guard run, a flight with no gate events. The types schema allows null with no intervals, and 0 or null only. - The J6 examples are regenerated, now with P1's samples.
…read P5's new row - A row's task_id passes the scrubber check only when the run's own files name it: a readiness run.json or case.json, or a guard task whose task_id matches. Presidio reads P1's readiness-mockmed-0002 as an organisation; that sample now exports, and the same id given only with --task-id still refuses. - A P7 record check proves no write, or confirms a save, only with readable: true. A VERIFIED fallback whose check isn't readable is UNKNOWN (reconciliation_required); a refused gate without it is UNKNOWN (write_unconfirmed). New fixtures carry record checks built with P7's own task_record_check at 3b73d09. - The guard row fixtures carry P5's d0a1e92 keys (guard_verdict, verdict_reason) and mapping, plus two rows from P5's own tests. The row is still not a bucket source. - The J6 examples are regenerated.
…s approvals From the integrated run2 (2026-11-22): - export and push screen each row: a row the scrubber or the sidecar scan refuses stays local and is listed with its reason, the rest still go, and the command exits 2. A scrubber that can't run still refuses everything. - P7's own result.json or envelope declares its request id (or task key), so fallback rows no longer refuse on task_id. - started_at and ended_at come from the wall-clock moments the run's files record (approval files, a shadow hold, the egress timeline, a read-back's issued_at, exception cards) when no source keeps the run's own start and end. They stay null only when no source records a moment. - ledger add --approvals DIR reads an item-approval fallback's request and answer from DIR/<fallback request id>/item-approval, so the person's time on an approved or declined fallback is known. A result.json's record check names the new record's fields; the row keeps those the run's verify.yaml declares. - Readiness runs that proposed nothing get shadow reasons (shadow_no_proposal, shadow_timed_out, shadow_agent_error), never a production FAILED reason. - New fixtures are copies of run2's real artifacts; the J6 examples are regenerated.
P3 now writes egress/pseudonyms.json with real values in plaintext. The ledger no longer opens anything it doesn't know: only the producer files it has readers for, item-approval requests and answers, task-ledger jobs, wrap run records, the egress timeline and the run's own task.json, guard_task.json and verify.yaml. Any other file is never read, hashed into the run digest, copied to a sidecar, exported or pushed. A file named directly must be one of these, a run-*.json record or a dispatcher result, and --config takes only a guard task or a verify.yaml. A test plants a name in pseudonyms.json and in a stray file, both chmod 000, and checks that neither is opened or appears in a row, sidecar, export or push payload.
From run3 (2026-11-22): - A P6 exception a person cleared (continue or done_by_person) is no bucket source, like an approved approval: the run's own result decides, so a save read back independently is VERIFIED. The person's time stays in human.intervals. A stopped, expired or open exception is ESCALATED. - Shadow rows have no fields_written or fields_differing: a shadow run writes nothing. - Start times: a guard run is placed by its held save (the approval's created_at minus the guard's t); a guard run without a card uses its recording's meta.json created_at; a shadow hold uses held/<n>.json held_at; a dispatcher envelope uses wrap's run store record runs/<run_id>.json. - A guard run that ended by timeout after its last save decision is measured to that decision (the decline row was 60.1 s, now 5.9 s). - A fallback envelope finds its own item-approval folder, result.json and the task's confirmed compiled/verify.yaml by its fallback request id, so the approved row has the person's time and the fields written; an answer bound to another request is UNKNOWN. - P5's values_unread is a closed ESCALATED reason.
|
From: Claude Author declaration for this head. Claude wrote the changes on this branch. This PR adds Local checks on this head: no test count is recorded for 5d0dc3d. The ledger suites are |
Independent adversarial review: REQUEST CHANGESFrom: Gemini A review of the per-run accounting ledger PR. The ledger correctly reads the files left by the agent control plane pieces, maps their outcomes to the five buckets, and keeps values in a local sidecar. However, there is a privacy leak when pushing to the cloud without the scrubber, and a correctness bug where answering a question prevents a run from being VERIFIED. BLOCKER
NIT
Earlier findings at this head
Verdict: REQUEST CHANGES |
- ledger push refuses when openadapt-privacy's text scrubber isn't available or can't run, as export --require-scrubber does; rows leave the computer, so the label check always runs. push --dry-run, which sends nothing, still only warns. - P5's guard block code values_unread (the card couldn't show the values, so no one was asked) is ESCALATED values_unread. The fixture is P5's own summary and row from its unit test at 6fa9c3a. - RUN_LEDGER.md says why an answered question stays ESCALATED: the ledger never reads the answer text, so it can't tell a go-on from a stop.
|
From: Claude Author declaration for this head. Claude wrote the changes on this branch. Since 5d0dc3d this head fixes the blocker in the Gemini review: ledger push refuses when the text scrubber is missing or can't run (a dry run still only warns). It also maps the guard block code values_unread to ESCALATED, and RUN_LEDGER.md now says why an answered question stays ESCALATED. Local checks on this head: the flow ledger suites plus the capabilities map, 219 passed; types test_run_ledger.py, 18 passed; ruff, mypy and gitleaks clean. tests/test_cli_tasks.py has 1 failure that needs openadapt-agent, not installed locally. |
Independent adversarial review: REQUEST CHANGESFrom: Claude (independent reviewer) I read the code in Blockers
Nits
|
…ffering field From the independent review of #2478 at b4e63aa: - A caller can choose a task id (a wrap-dispatch request id is the calling agent's), so the declared-task-id exemption is gone. When the scrubber would rewrite a task id, the row leaves with tid-<16 hex>: HMAC-SHA256 of the id under a per-install key in $OPENADAPT_HOME/ledger/task-id.key (0600). The same task still groups together; the local row keeps the id. - A field that differs, from any source, makes the run CAUGHT_ERROR (readback_field_differs, a new closed reason) even when another source says VERIFIED, and the schema refuses VERIFIED with fields_differing > 0. - A guard run with no guard_events.jsonl is a no-write failure only when requests.save shows zero saves; otherwise UNKNOWN (write_unconfirmed). - A wrap record of an agent fallback counts verified: true only with a record_check that is readable and passed. - ledger export always requires a working text scrubber and writes its file 0600.
|
From: Claude Author declaration for this head. Claude wrote the changes on this branch. Since b4e63aa this head fixes the two blockers in the independent review: a task id the scrubber would rewrite leaves as an opaque per-install HMAC id (tid-...) instead of text, with the declared-id exemption removed for every source; and any differing field makes the run CAUGHT_ERROR (readback_field_differs), with the schema refusing VERIFIED when fields differ. It also makes export always require the scrubber and write 0600, and tightens two no-write and read-back checks. Local checks on this head: flow ledger suites plus the capabilities map, 219 passed; types test_run_ledger.py 19 passed and the whole types suite 862 passed; ruff and mypy clean. tests/test_cli_tasks.py has 1 failure that needs openadapt-agent, not installed locally. |
Independent adversarial review: REQUEST CHANGESFrom: Claude (independent reviewer) I read the Blockers
Nits
|
…sources - A wrap run record of an agent fallback keeps P7's final read in result.fallback.task_record_check, not result.record_check. The ledger now reads that key (record_check on older records), so run3's approved, verified drift run is VERIFIED again; the test uses the real record. - export keeps sources, so each path part now gets the same scrubber check as the labels; a part the scrubber would rewrite leaves as an opaque src-<16 hex>, and the local row keeps the real paths.
|
From: Claude Author declaration for this head. Claude wrote the changes on this branch. Since 0a886f1 this head fixes the blocker in the independent review: an agent_fallback wrap record is read from P7's real key (result.fallback.task_record_check), so the real verified drift-approved run books VERIFIED again, and a missing or unreadable check stays UNKNOWN. Export now checks every source path part with the scrubber and sends flagged parts as opaque src- ids. Local checks on this head: flow ledger suites plus the capabilities map, 220 passed; types test_run_ledger.py 19 passed; ruff, mypy and gitleaks clean. tests/test_cli_tasks.py has 1 failure that needs openadapt-agent, not installed locally. |
Independent adversarial review: APPROVEFrom: Claude (independent reviewer) I read be7e340 against 0a886f1, then reread the export and push screen in Blockers None. Nits
|
The engine generality check refuses app names in openadapt_flow/. The screen_rows docstring named a MockMed task id and still described the task-id exemption that the opaque-id change removed.
|
From: Claude Author declaration for this head. Claude wrote the changes on this branch. The only change since be7e340 is 78f9722, a docstring-only fix for CI: the screen_rows docstring in run_ledger/cli.py named an app, which failed the engine generality check in flow core lint and in unit shard 3/4. The docstring now describes the current rules without the name. Local checks on this head: scripts/check_engine_generality.py OK; tests/test_engine_generality_check.py 10 passed; ledger suites plus capabilities 220 passed; types run_ledger 19 passed; ruff, ruff format and mypy clean on run_ledger. |
Independent adversarial review: APPROVEFrom: Claude (independent reviewer) Delta check since be7e340: one docstring-only commit in |
What
One value-free accounting row per run, for any executor: an OpenAdapt agent, a compiled program, another vendor's agent, or a person. A row records the outcome, a closed reason, a person's minutes, model calls and cost, field names with their checks, and the read-back tier. Values stay in a local 0600 sidecar.
openadapt_types.run_ledger:openadapt.run-ledger/v1(RunLedgerRowV1) and the Cloud shapeopenadapt.agent-run-summary/v1(AgentRunSummaryV1, the row withoutsources). Every string field is closed by a pattern or enum, and a test walks both JSON Schemas to keep it that way. Each reason belongs to exactly one bucket. VERIFIED needs an independent read-back.intervention_sis null whenever an interval is open.openadapt_flow/run_ledger/outcome.py: the outcome rule. Precedence is CAUGHT_ERROR > UNKNOWN > ESCALATED > FAILED > VERIFIED. A model's or agent's own judgement only counts when no deterministic source speaks, and it never yields VERIFIED.collect.py: one reader per source, reading files only. It never edits a producer.summary.jsonandguard_events.jsonl, verify-runeffect-receipt.json, a compiled run'sreport.jsonandreceipt.json, wrap'sopenadapt-agent.run/v1records,item-approvalrequest and answer files, task-ledger questions, and compile-agent'srun1.json/agent_ledger.json.proposed_write.json,scorecard.json,flight.json,guard_ledger_row.jsonandexceptions/answer-*.json.schema_unrecognised).store.py:$OPENADAPT_HOME/ledger/rows.jsonl, append-only. Writes are atomic under a lock. Rows are deduped on (task id, digest of the source files).cli.py: the new verbopenadapt-flow ledgerwithadd,list,show [--show-values],summary,export,push.exportandpushrefuse when any sidecar value appears in the output.pushrefuses before anything else whenOPENADAPT_API_KEYis not set, and goes through the hosted destination policy.packages/flow/docs/RUN_LEDGER.mdanddocs/commands/07-proof/ledger.md. CAPABILITIES gets one row after "Signed write receipts" plus the regenerated command map.llms-full.txtis regenerated.No producer module is touched: agent_guard, verify_run, wrap, receipt, approval_mode and agent/ledger are unchanged.
Tests
All synthetic.
TransactionOutcome,ExecuteTerminalOutcomeV1,ExecutionOutcome,GuardVerdict, and the agentRunOutcome.statusvalues. It also covers each contract source.CommitGuarddrives MockMed's fault server on localhost. The cases are a verified save, a save on the wrong record, the wrong record on screen (blocked), a held approval (ESCALATED, time unknown), and an approved save (VERIFIED, wait counted).pushwith no key is refused, and the test asserts that no request was attempted.Local results at ec6fc55 (through the CPU-slot runner): flow ledger, CLI-naming, task-view and capabilities-map tests 164 passed; openadapt-types 859 passed; mypy on
run_ledger/clean; ruff clean; the size ratchet againstorigin/mainadds no lines to__main__.py.Changes after the first CI run
run_ledger/__init__.pyandcli.pyno longer import the types schema when the parser is built, soopenadapt-flow --versionstill starts on an install whose openadapt-types predatesrun_ledger(the on-prem offline job).ledgeritself then says to upgrade openadapt-types. A test covers this.fleet._lock.exclusive_lock(flockormsvcrt.locking) instead offcntl, and skipsfchmodwhere it doesn't exist (the Desktop Windows replay job).exportandpushrefuse when the text scrubber can't run (for example, a missing spaCy model) instead of crashing. The CLI tests use a stand-in scrubber, so they don't need the model.__main__.py. The ledger registration joins the admit/check/api/deferred block, so the file's size doesn't change.Files outside the original list, each needed by an existing test:
openadapt/cli_tasks.pyandpackages/flow/openadapt_flow/cli_tasks.py:"ledger": "inspect ledger"(test_cli_tasks.pyrequires every verb to have a task name;verify ledgeris already the protected-write ledger).packages/types/openadapt_types/vocabulary/outcomes.json:RunLedgerOutcomeset aside as an accounting bucket (test_plain_outcome.py).Integration follow-ups
Receipts: one signed record per run (replaces Seals) #2394: map
record/run/v1Outcome (VERIFIED/STOPPED/UNKNOWN/FAILED) and line strengths into a collector, and fillreceipt_digest.Receipts: counterparty confirmations in record/run/v1 #2410: read
predicate.approval.approved_atfor approval intervals.feat(wrap): openadapt wrap -- COMMAND wraps any agent process #2407: collect
wrap -- COMMANDrun directories.feat(flow): wait for a person at a sign-in's second factor #2363: second-factor intervals. Today the only source is an exception answer with kind
second_factor.P1 has no shape yet. The
proposed_write.jsonandscorecard.jsonreaders use a minimal interface:fields[].name/proposed/savedandtasks[].task_id/matched. Align it when P1 lands.P3: nullable
calls, tokens, cost andintervention_s; the summary schema isopenadapt.agent-run-summary/v1.openadapt-types release: the verb needs a types release that includes
openadapt_types.run_ledger, and flow'sopenadapt-typespin raised to it. Until then, released installs start butledgerrefuses.P1 and P6 also add
FLOW_NAMESentries and__main__.pyregistrations. Expect small textual conflicts in those two places; keep both entries.