From eaeec8ff5806071c39baa66a9f84a37aaba73eb0 Mon Sep 17 00:00:00 2001 From: abrichr Date: Fri, 20 Nov 2026 23:39:04 -0500 Subject: [PATCH 01/19] feat(wrap): openadapt wrap --example runs the whole loop on a bundled task Run 1 by the built-in agent on the bundled synthetic clinic app, read back, compiled, canaried and admitted with throwaway keys; run 2 with a new note through the emitted MCP tool with no model calls. Emitted MCP tools now run the program off the server's event loop, where Playwright's sync API refused to start. --- CAPABILITIES.md | 2 + packages/flow/docs/WRAP.md | 77 +++ .../docs/commands/01-record-and-build/wrap.md | 1 + packages/flow/openadapt_flow/__main__.py | 10 + .../openadapt_flow/compile_agent/__init__.py | 2 + .../flow/openadapt_flow/compile_agent/emit.py | 8 +- .../openadapt_flow/compile_agent/pipeline.py | 49 +- packages/flow/openadapt_flow/wrap/cli.py | 153 ++++++ packages/flow/openadapt_flow/wrap/example.py | 442 ++++++++++++++++++ .../wrap/examples/clinic-note.json | 27 ++ .../flow/tests/e2e/test_wrap_example_e2e.py | 156 +++++++ .../flow/tests/e2e/test_wrap_sign_in_e2e.py | 9 +- pyproject.toml | 3 + tests/test_wrap_command.py | 24 + uv.lock | 2 + 15 files changed, 952 insertions(+), 13 deletions(-) create mode 100644 packages/flow/docs/WRAP.md create mode 100644 packages/flow/docs/commands/01-record-and-build/wrap.md create mode 100644 packages/flow/openadapt_flow/wrap/example.py create mode 100644 packages/flow/openadapt_flow/wrap/examples/clinic-note.json create mode 100644 packages/flow/tests/e2e/test_wrap_example_e2e.py diff --git a/CAPABILITIES.md b/CAPABILITIES.md index 3214679ba6..6e33178ad1 100644 --- a/CAPABILITIES.md +++ b/CAPABILITIES.md @@ -319,6 +319,7 @@ improvising or retrying when something is off. | See what your own agent gets wrong, with no risk: `openadapt shadow` is a loopback pass-through proxy beside the agent. It checks each write against the system of record (FHIR through `ensure_record`; SQL through the verifier's whole-database frame), checks each `CONFIRMED` again later, and writes `report.html` and `report.json`. It never blocks, changes or resends a write; the only delay is the read before a write (at most `hold_ms`) | [#1678](https://github.com/OpenAdaptAI/OpenAdapt/pull/1678) | Real app (Medplum 5.2.8, 37 cases through a fault proxy: scripted agent 18 of 18 harms caught, 0 correct writes refuted; Claude Sonnet 5.5 9 of 9 caught, 4 correct but corrected writes refuted; 0 writes by the shadow; 15 to 25 ms median added per write; setup 76.5 s machine time, 3 decisions); real app, SQL mode (Dolibarr 21 on MariaDB 11.4, 13 cases: 8 of 8 harms caught, 5 of 5 correct writes confirmed, 292 ms median added); synthetic (SQLite) | `openadapt shadow`, [SHADOW_MODE.md](packages/flow/docs/SHADOW_MODE.md), [SHADOW_RESULTS.md](packages/flow/benchmark/fhir_medplum/public/SHADOW_RESULTS.md) | | Skill cache for framework agents: an agent in any framework does a browser task through compile-agent's harness as plain tools (`BrowserTools`); when the system of record shows exactly the task's record and the operator confirms the checks, the run is canaried, admitted and emitted as an MCP server. A repeat of the same task (instruction, record, app, contract, environment) calls that tool instead of the agent: no model calls, the program's checks and this task's record check on every run, eviction after a failed run | Released | Synthetic (MockMed, LangGraph on `claude-sonnet-5-5`, 3 trials, repeats on the same record: run 1 6 model calls and $0.022; runs 2 to 10 27/27 VERIFIED, 0 model calls, 7.8 s median); Trac 1.6: run 1 verified, not admitted | `openadapt_flow.integrations.agents.skills`, [benchmark/skill_cache](packages/flow/benchmark/skill_cache/README.md) | | Publish a wrapped task as a hosted endpoint with one credential: `openadapt wrap publish --task TASK` uses only `OPENADAPT_API_KEY`. It finds the task's admitted program, re-verifies its admission signature, refuses a held task, a program without a read-back check (no saving step, a save with no effect contract, or no confirmed `verify.yaml`), symbolic links and a non-public app URL, and stages the bundle with its read-back configuration. It sanitizes the bundle and the recording it was compiled from (the generated wrap files declare each field as content, always scrubbed, or structural, kept only with its machine shape, so schema ids, step ids, digests and the signed admission survive the scrub byte-for-byte), shows one summary and asks once (`--yes` for scripts), finds the key's organization (`GET /api/v1/whoami`, or `--org`), registers the local admission public key with it once, attests the program from the wrap run's own signed recording and attended canary (strict lint with no warn or error finding, certification, VERIFIED replay) against a challenge from the organization's API-key route, uploads it through the governed bundle push to `POST /api/v1/org/{org}/workflow` (or `PUT .../workflow/{id}/definition`), then reads the Cloud workflow definition and prints the endpoint URL and `curl` examples from the acknowledged `execute_path` and `qualification_id`. Each refusal is one line with a code. The upload carries the program's value-free Standard run shape. A managed run of the program rests on its signed admission: `openadapt-flow run --managed-dispatch-file` checks the admission against the registered admission key, requires the authorization to name that admission and match the shape, and reads back through the bundled `verify.yaml`. The CLI, public runtime and deployment API carry one-use verification bound to that dispatch's admission, bundle, inputs, policy, run, session and current trust registry; resume verifies a fresh proof. Each input checks that scope again after preparation and requires a signed v3 permit with a receiver-checked exclusive start deadline. API sequences acknowledge every request separately. Missing receipts or late completion evidence halt further input and require reconciliation. Publish refuses, before anything is uploaded, an admission mode a hosted run cannot verify (only a person's admission or a registered CI admitter key), and an admitter-key admission whose key the organization has not registered (`GET /api/v1/org/{org}/admitter-keys`). The recording stays on the machine; only its sanitized hash is attested. Regulated is refused (`openadapt_flow.jit.hosted_authority`) | On main | Unit tests (real bundle, record check, staging, local sanitization and signed wrap evidence; the attestation's recording binding checked on real derivatives; the whoami, admission-key, challenge, workflow upload and definition routes mocked at the HTTP boundary); no run against a live Cloud deployment. Relies on the API-key workflow routes in openadapt-cloud#430 (draft), which accept this v2 attestation only behind a flag that is off by default; the whoami route is not in #430 yet | `openadapt_flow.wrap.publish`, [WRAP_PUBLISH.md](packages/flow/docs/WRAP_PUBLISH.md), `packages/flow/tests/test_wrap_publish.py`, `packages/flow/tests/test_jit_hosted_authority.py`, `packages/flow/tests/test_jit_hosted_managed_replay.py` | +| Try `openadapt wrap` with one command: `openadapt wrap --example` starts a bundled synthetic clinic app (MockMed) on 127.0.0.1, has OpenAdapt's built-in Claude agent save one note in a headless browser, reads the save back, compiles the run, runs a canary, and admits it with a throwaway key made in the example's own folder (the proposed checks are confirmed automatically, which only a synthetic app allows). Then a new note runs the compiled program over its emitted MCP server with no model calls, and it prints the app's record and the cost of both runs. It needs `ANTHROPIC_API_KEY` (or `OPENAI_API_KEY` with `--provider openai`, `--model` and reviewed `--price` values); the task is packaged data (`openadapt_flow/wrap/examples/`). Because the data is synthetic, screenshots go to the model unscrubbed under the recorded opt-out. Emitted MCP tools now run the program off the server's event loop, so a compiled browser program runs when called over MCP (it failed with Playwright's "Sync API inside the asyncio loop" error before) | In PR | Synthetic (the bundled app, headless Chromium and the real `build_agent` Claude loop with its token-count reservation and spend cap; CI uses a scripted model client in place of the Anthropic SDK). One real-model run is in the pull request | [WRAP.md](packages/flow/docs/WRAP.md), `openadapt_flow.wrap.example`, `openadapt_flow.compile_agent.run_compiled_tool`, `packages/flow/tests/e2e/test_wrap_example_e2e.py` | | Wrapped tasks that sign in: during `openadapt wrap`'s first run the recorder finds a password field (`type=password`, or `autocomplete` `current-password`/`new-password`) and the username field paired with it (`autocomplete=username`, or a unique text, email or phone candidate paired across all password controls in the composed form; ambiguous or unreadable controls refuse declared inputs) from the page's markup alone. Automatic withholding requires that observation seam; a missing or noncallable seam refuses before capture or actor entry. Screen-only native backends use the existing explicit person-secret handoff instead of this automatic path. Their inputs become secret parameters: the recording, program, run records, receipts and publish upload keep the name and never the value, the observed field regions are blacked out in recording frames (arbitrary field movement or echoes are not proven redacted), a credential field given text that matches no declared input is refused before typing, a form with exactly a username and current password is classified as sign-in without lowering independent keyword risk, and replay run screenshots contain black frames while checks use the original pixels in memory. Secret environment/vault aliases must be unambiguous, and managed params files cannot supply secret values. The canary and cached runs type them from `OPENADAPT_FLOW_SECRET_`, set from memory for one run; `publish` lists them as `secret_parameters`, outside the call's inputs | In PR [#2294](https://github.com/OpenAdaptAI/OpenAdapt/pull/2294) | Synthetic (a sign-in notes app in headless Chromium through the real wrap path: first run, canary, admission, publish staging, and a cached run under mcp 2, with a byte scan for the values in every file; unit tests with a fake backend for detection, frame redaction, refusals and the sign-in submit rule). Genuine RDP/Citrix backends refuse automatic withholding before input; explicit person-secret handoff remains tested. Hosted runs need an endpoint vault set (openadapt-cloud) | `openadapt_flow.recorder` (`withhold_credentials`), `openadapt_flow.secret_inputs`, [WRAP_PUBLISH.md](packages/flow/docs/WRAP_PUBLISH.md#tasks-that-sign-in), `packages/flow/tests/test_wrap_sign_in.py`, `packages/flow/tests/e2e/test_wrap_sign_in_e2e.py` | | Verified write tools for agent frameworks: `verified(tool, contract=...)` wraps a Claude Agent SDK, OpenAI Agents SDK, LangGraph or CrewAI write tool in one line. The contract and any values the operator pins come from the operator, never the agent's arguments; the store is read before and after; the agent gets a receipt or a refusal, and a refused write is held instead of retried | Released | Synthetic (each framework's own agent loop with a scripted model, writing through the example app's order form over real HTTP: verified save, a save the app drops, a save it keeps twice); one real-model run per framework, opt-in | `openadapt_flow.integrations.agents`, extras `claude-agent-sdk`, `openai-agents`, `langgraph` (CrewAI: install `crewai` beside it); [AGENT_FRAMEWORKS.md](packages/flow/docs/AGENT_FRAMEWORKS.md) | | Teach a browser write as a verified tool: with write contracts and a person's signed teach authorization (`serve --teach-browser --contracts DIR --teach-authorization STATEMENT`), the agent teaches a save once. Run 1 starts only when the statement covers the write (origin, contract, write adapter, the record's fields and values) and runs under it; it is verified in the system of record before the `taught_` tool exists. Every call checks the statement and the tool's signature again, makes the write and returns a verified receipt or a refusal. Sign-in values come from the server's environment and never reach the agent or the program. Controls in named iframes are found through their frame | On main ([#1589](https://github.com/OpenAdaptAI/OpenAdapt/pull/1589), [#1763](https://github.com/OpenAdaptAI/OpenAdapt/pull/1763)) | Synthetic (the example app's sign-in and order form in headless Chromium over MCP: verified save, a save the app drops, a save it keeps twice, a page that swaps the record during run 1; refusals for a missing, uncovering, expired, edited or wrongly signed statement) | `openadapt-agent serve --teach-browser --contracts --teach-authorization`, [VERIFY_WRITES.md](packages/flow/docs/VERIFY_WRITES.md#teach-a-write-as-a-tool) | @@ -391,6 +392,7 @@ A test fails when a command has no file or this list is out of date. - `skill`: Bind a pulled skill to your instance, qualify it there, and check whether it may run. - `variants` - `visualize` +- `wrap`: Runs an agent task once, reads the save back, compiles and admits the run, then runs the program with no model calls. `--example` does all of it on a bundled synthetic task. Also `openadapt wrap`. **Check and admit:** diff --git a/packages/flow/docs/WRAP.md b/packages/flow/docs/WRAP.md new file mode 100644 index 0000000000..4b97c3880b --- /dev/null +++ b/packages/flow/docs/WRAP.md @@ -0,0 +1,77 @@ +# Wrap an agent task + +`openadapt wrap` lets an agent do a task once, reads the save back from the +system of record, compiles the run into a program, and admits it. Later +calls with new inputs run the program instead of the agent: no model calls, +and every save is read back again. + +## Try it with one command + +```bash +export ANTHROPIC_API_KEY=... +uvx --python 3.12 openadapt@latest wrap --example +``` + +`--example` runs the whole loop on a bundled sample task. Nothing leaves the +computer except the agent's model requests. + +1. A synthetic clinic app (MockMed) starts on `127.0.0.1`. Its patients and + notes are made up. +2. **Run 1.** OpenAdapt's built-in Claude agent opens a patient's referral, + starts a Triage encounter and saves a note, in a headless browser. The + save is read back from the app's records. The run is compiled, a canary + runs with another note, and the draft is admitted. +3. **Run 2.** A new note runs the compiled program through its MCP tool. It + makes no model calls, and the save is read back again. + +It prints each run's model calls, cost and time, the record the app holds +after run 2, and the folder with every file it wrote: + +```text +Run 1: claude-opus-5-5 does the task (note "Synthetic follow-up in two weeks"). + ... +Run 2: the compiled program, new input (note "Recheck knee in ten days"). + 0 model calls, $0.0000, ...; status VERIFIED + +Read back from the app's records (/api/db): + {"id": ..., "note": "Recheck knee in ten days", "patient_id": "p1", "type": "Triage"} +``` + +The exit status is 0 only when run 1 was admitted and run 2 made no model +calls and was read back `VERIFIED`. `--json` prints one JSON result instead. + +### What the example does differently from a real task + +The example is safe to run unattended only because its app and data are +synthetic. A real task differs in three ways: + +| | `--example` | A real task (`--task`) | +|---|---|---| +| Who confirms the proposed read-back checks | Confirmed automatically | A person at the terminal (`--person`) | +| Who admits the draft | A throwaway key made in the example's folder | A person, or a registered admitter key (`--admit-key`) | +| Screenshots sent to the model | Unscrubbed, under the recorded opt-out | Scrubbed by the `privacy` extra, unless you opt out | + +The throwaway approver and admitter keys live in the example's own folder +(`~/.openadapt/wrap/example/