Skip to content

feat(wrap): openadapt wrap --example, and compiled browser tools that run over MCP - #2406

Merged
abrichr merged 13 commits into
mainfrom
claude/wrap-example-cmd
Nov 23, 2026
Merged

abrichr merged 13 commits into
mainfrom
claude/wrap-example-cmd

Conversation

@abrichr

@abrichr abrichr commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

What this does

openadapt wrap --example runs the whole wrap loop from one command: uvx --python 3.12 'openadapt[wrap]@latest' wrap --example. The only setup is a model key.

  1. A bundled synthetic clinic app (MockMed) starts on 127.0.0.1. The task, its record and its read-back come from a packaged data file, openadapt_flow/wrap/examples/clinic-note.json. The engine code names no app.
  2. Run 1. OpenAdapt's built-in Claude agent (build_agent, with its token-count reservation and spend cap) saves a note in a headless browser. The save is read back, the run is compiled, a canary runs, and the draft is admitted.
  3. Run 2. A new note runs the compiled program through its emitted MCP tool, with no model calls, and the save is read back again.

The command prints each run's model calls, cost and time, then the app's own record after run 2. It exits 0 only when run 1 was admitted and run 2 was VERIFIED with no model calls. --json prints one JSON result instead.

Bug fix: compiled browser programs over MCP

Emitted MCP tools ran the program on the MCP server's event loop thread. There, Playwright's sync API refuses to start ("It looks like you are using Playwright Sync API inside the asyncio loop"), so every compiled browser program failed when called over MCP under mcp 1.x. test_wrap_sign_in_e2e.py worked around this by skipping its cached run on mcp 1.

The emitted tool now calls run_compiled_tool, which runs the program on a worker thread with a copy of the caller's context, so the request binding carries over. server_needs_reemit re-emits older servers. The sign-in e2e no longer skips, and the example e2e exercises this path in CI.

For the reviewer to rule on

  • Unscrubbed screenshots. The example sets OPENADAPT_FLOW_SEND_UNSCRUBBED for its own process, with the reason "bundled synthetic sample app, no real data". It prints one line saying so, and each send is logged in the opt-out ledger. Without this, the one-line run would need the privacy extra, which brings presidio and a spaCy model.
  • Unattended admission, example only. The proposed checks are confirmed automatically, and a throwaway admitter key admits the draft. Both keys live in the example's own folder, never in the user's key directory. docs/WRAP.md has a table that sets out how this differs from a real task.
  • Launcher dependency. The base launcher does not depend on anthropic. A new wrap extra (openadapt[wrap]) brings anthropic>=0.40, and the documented one-liner installs it. Without the SDK, wrap --example exits 2 with one line naming that command. The install-weight allow list is unchanged; uv.lock gains the extra.

Other changes

  • wrap stays a launcher command (openadapt wrap). It is not added to the openadapt-flow parser, which keeps tests/test_cli_documented_tasks.py's rule that discovery must not invent an engine wrap verb.
  • --example is matched only before a --.
  • Documentation: packages/flow/docs/WRAP.md, a row in CAPABILITIES.md, and the refreshed llms-full.txt.
  • wrap/examples/ may name the bundled demo app (scripts/generality.toml), like the other demo paths. The example and its recorded run are in the public artifact inventory.

Tests

  • tests/e2e/test_wrap_example_e2e.py runs the real loop: the app, headless Chromium, the build_agent Claude loop, compile, canary, admission, the emitted MCP server, and run 2. A scripted client stands in for anthropic.Anthropic. It clicks at the controls' page positions and reports token usage the way the API does. A second test checks the missing-key message.

  • tests/e2e/test_wrap_sign_in_e2e.py now runs its cached run under mcp 1.30.

  • tests/test_wrap_command.py covers launcher passthrough and checks that anthropic is in the wrap extra and not in the base dependencies.

  • tests/test_compile_agent.py: a generated tool runs the program on a worker thread under a running loop, keeping the request context, and raises the program's errors unchanged. server_needs_reemit also covers the new marker.

  • packages.yml adds test_wrap_example_e2e.py to the critical-e2e step, so PR CI runs it. Without this, nothing in PR CI runs a compiled browser tool over MCP.

On macOS with Python 3.12 and mcp 1.30, both e2e files pass (3 tests). In a fresh venv with only what openadapt[wrap] installs (the built Flow wheel with [browser,hosted], openadapt-agent, anthropic, no privacy extra, no test conftest), the example e2e passes. That shows the packaged example data and the dependency set are enough for the one-line run. The built wheel contains openadapt_flow/wrap/examples/clinic-note.json.

The uvx --python 3.12 'openadapt[wrap]@latest' wrap --example line works only after a Flow and launcher release that includes this PR.

One real run

On macOS, headless, with Claude Opus 5.5 and ANTHROPIC_API_KEY from the environment, built from this branch before release (openadapt-flow wrap --example --max-cost-usd 5, a spelling the branch had then; the command is now openadapt wrap --example):

Run 1: claude-opus-5-5 does the task (note "Synthetic follow-up in two weeks").
  5 model calls, $0.1484, 73.524 s; status admitted
Run 2: the compiled program, new input (note "Recheck knee in ten days").
  0 model calls, $0.0000, 19.713 s; status VERIFIED

Read back from the app's records (/api/db):
  {"id": 1, "note": "Recheck knee in ten days", "patient_id": "p1", "type": "Triage"}

The run's example-result.json is in packages/flow/benchmark/wrap_example/real-run-2026-11-21.json; the printed output above is the only copy of the text.

Known failure that is not from this PR

tests/e2e/test_wrap_sign_in_e2e.py fails with canary_failed in run 1 on the zenbook's WSL Ubuntu. Current main fails there the same way, at a step before any change in this PR. It passes on macOS.

CodeQL

Alerts 106 to 109 (py/clear-text-logging-sensitive-data) were dismissed as false positives. The tainted source is the example's throwaway admission-key passphrase (secrets.token_hex, set in the process environment for the example's own key folder). The flagged prints emit the app label, run status, cost and output folder, never the passphrase.

… 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.
@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 87e244c

First review of this PR. Author is GitHub abrichr, not Grok. I will not merge.

run_compiled_tool and server_needs_reemit

run_compiled_tool runs run_compiled_params on the caller's thread when there is no event loop, and otherwise on a worker thread with contextvars.copy_context(). Exceptions are raised in the caller unchanged. That is the right shape for Playwright's sync API under mcp 1.x. server_needs_reemit is true when the emitted mcp/server.py lacks mcp_request_scope or run_compiled_tool, so a pre-#2154 or pre-this-fix server is marked for re-emit.

Throwaway keys stay in the example folder

run_example writes keys under out_dir / "keys" (and the admitter PEM under out_dir / "keyfiles"), sets a throwaway KEY_PASSPHRASE_ENV only inside _env, and refuses a non-empty out_dir. I did not find a path that registers those keys in the user's default key directory. Auto-confirmed proposals are also scoped to this synthetic run.

Unscrubbed screenshots: allowed only for this bundled sample

Ruling: this opt-out is acceptable only for openadapt wrap --example on the packaged synthetic pack. The reason string is fixed ("openadapt wrap --example: bundled synthetic sample app, no real data"), it is recorded through OUTBOUND_OPT_OUT_ENV for the duration of _env, and the CLI does not take a user pack path. It must not become a flag or escape hatch on wrap --task or on a caller-supplied pack. If it ever applies to real data, that is a regression.

Review only; I am not merging.

…openadapt[wrap]

- The qualify default-form rewrite moves to cli_forms.py and the wrap
  dispatch comment shrinks, so __main__.py stays at its 14331-line budget.
- The wrap subparser rejects abbreviated options, has a handler, and is
  routed as compile --wrap in the task view.
- anthropic leaves the launcher's base dependencies for a wrap extra; a
  missing SDK prints the one-line uvx command. Docs use that command.
- wrap/examples/ may name the bundled demo app, like the other demo paths.
- Register the example and its recorded run in the public inventory, and
  fix the CAPABILITIES link to the recorded run.
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: REQUEST CHANGES

From: Claude (independent reviewer)
Reviewed-Head: 6aad21a

Blockers

  1. CI is red because of this PR's wrap alias. cli_tasks.FLOW_NAMES gains "wrap": "compile --wrap". No compile --wrap route exists (__main__.py has no --wrap option), so the alias points at a command that isn't there. It breaks two tests at this head:

    • launcher / run-ci: tests/test_cli_tasks.py::test_launcher_route_table_matches_the_engine_source fails because the engine table has {'wrap': 'compile --wrap'} and the launcher route table does not.
    • flow core unit (shard 3/4): tests/test_cli_tasks.py::test_alias_notice_uses_registered_labels_and_never_argument_rendering[True-launcher] expects a "...is also available as ... compile --wrap" notice and gets none. That is expected, because main hands wrap to the wrap parser before alias_notice runs.

    The entry looks like it was added to satisfy the earlier legacy-leaf failure. wrap is a top-level verb, not a legacy alias of a task route. Exempt it in the legacy-leaf inventory instead of mapping it to a route that doesn't exist.

  2. CodeQL is failing on alerts 106 to 109. The four py/clear-text-logging-sensitive-data alerts are at wrap/cli.py:373 and :399 and wrap/example.py:260 and :407. Those lines print the variable name ANTHROPIC_API_KEY, the output folder, say() text and the summary JSON. I found no key, passphrase or token value in any of them, so I read them as false positives. Still, the check is red. Dismiss them with that reason, or restructure the prints so the taint does not reach them.

  3. The PR body says the opposite of the code. It still says "The launcher now depends on anthropic>=0.40 so that uvx openadapt wrap --example works", and it spells the one-liner uvx ... openadapt@latest wrap --example. At this head, anthropic is in the openadapt[wrap] extra, guarded by python_version < '3.13'. tests/test_wrap_command.py asserts that the base dependencies do not include it, and WRAP.md, CAPABILITIES.md, llms-full.txt and WRAP_EXAMPLE_COMMAND all say uvx --python 3.12 'openadapt[wrap]@latest' wrap --example. The body becomes the squash message, so update it. Also, benchmark/wrap_example/README.md and the body ("The full output and result are in ...") both say a .txt output file is committed, but only the .json file is.

Checked and fine

  • Earlier CI failures are fixed at this head:
    • The __main__.py line budget (the code moved to cli_forms.py).
    • The public-artifacts inventory.
    • Install weight (anthropic left the base launcher).
    • No-abbreviations (allow_abbrev=False on the wrap subparser).
    • The generality baseline (wrap/examples/ added to generality.toml).
    • The internal link to the missing .txt file (CAPABILITIES now links the .json).
  • The e2e tests ran: critical-e2e ran 65 tests with 0 skipped, including test_wrap_example_e2e.py and the unskipped test_wrap_sign_in_e2e.py.
  • Missing SDK: I ran example_main(["--example"]) at this head with sys.modules["anthropic"] = None and ANTHROPIC_API_KEY set. It printed one line, openadapt wrap --example: the built-in agent needs the wrap extra; run: uvx --python 3.12 'openadapt[wrap]@latest' wrap --example, and exited 2 with no traceback. build_agent checks the SDK with find_spec, and anthropic is imported only inside compile_agent/agents.py when the agent runs. The uv.lock change matches the extra.
  • run_compiled_tool: it runs on the caller's thread when no loop is running. Otherwise it runs on a worker thread under contextvars.copy_context(), so the request binding carries over, and errors are re-raised unchanged. server_needs_reemit also checks for the new marker.
  • Throwaway keys and opt-out: the keys and the passphrase stay in the example's folder and in its scoped _env. The unscrubbed opt-out applies only to the packaged pack, and the CLI takes no pack path. This agrees with Grok's ruling at 87e244c1.
  • Fail-closed checks: a non-empty --out is refused, and the app must listen on 127.0.0.1. The exit status is 0 only when run 1 was admitted and run 2 has path == "skill", verified is True and 0 model calls.

Out of scope

  • windows replay (Patient Notes, WindowsBackend) fails in tests/test_windows_attempt_ledger.py, the junction tests. This PR doesn't touch that code, and other branches fail it intermittently too.
  • openadapt wrap --task with the built-in Claude agent and no SDK goes through build_agent's existing blocker path, not the new uvx hint. That behavior was already there before this PR.
  • The all extra does not include wrap.
  • run_compiled_tool still blocks the MCP server's event loop for the whole run, as the old synchronous call did.
  • No Gemini or Codex review is on this PR. The only earlier review is Grok's APPROVE at 87e244c1.

tests/test_cli_documented_tasks.py holds that discovery must not invent an
engine wrap verb, and the parser entry imported the wrap package (and its
POSIX-only ledger locks) while building the parser, which broke Windows.
Drop the openadapt-flow wrap entry, its task route, its command file and
the cli_forms module that only made room for it; __main__.py is main's again.
The benchmark README no longer promises a .txt output file.
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Claude (independent reviewer)
Reviewed-Head: 711a40f

Delta from 6aad21ae1 to this head. All three blockers from the last review are fixed.

Blockers from the last review

  1. Alias failure: fixed. "wrap": "compile --wrap" is gone from cli_tasks.FLOW_NAMES. The engine-side add_parser, _cmd_wrap, the argv[:1] == ["wrap"] hand-off in __main__.main, docs/commands/01-record-and-build/wrap.md and the CAPABILITIES command-list row are removed too. __main__.py and cli_forms.py are now the same as the merge base, so the line budget is back to what main has. wrap stays a launcher command: openadapt/cli.py loads openadapt_flow.wrap.cli.main through _optional_engine_entrypoint. A grep of the head finds no cli_forms, _cmd_wrap, add_parser for wrap, or compile --wrap.
  2. CodeQL 106 to 109: dismissed with a sound reason. All four are dismissed as false positives. Each alert's message reads "logs sensitive data (password)", which matches the dismissal's named source: the example's throwaway KEY_PASSPHRASE_ENV value from secrets.token_hex. The flagged lines print the provider's variable name, the output folder, say() text and the summary JSON, never the passphrase. No code-scanning alert is open on refs/pull/2406/merge or refs/pull/2407/merge.
  3. PR body and README: accurate. The body now describes the openadapt[wrap] extra (not a base dependency), the uvx --python 3.12 'openadapt[wrap]@latest' wrap --example spelling, and "wrap stays a launcher command". It explains that the recorded run used the old openadapt-flow wrap spelling, which is the only remaining use of that spelling, in real-run-2026-11-21.json's command field. benchmark/wrap_example/README.md now names only the committed .json.

CI at this head (gh pr checks)

  • Passing: launcher / run-ci, which holds test_launcher_route_table_matches_the_engine_source and failed last round. Also passing: CodeQL Analyze (python, javascript-typescript, rust), gitleaks, dependency-review, smoke, internal links, validate-platform-manifest, check-source-boundary, protected-safety-review, windows replay, GTK and freerdp.
  • Still pending when this was posted, because runners are starved: the flow core unit shards (shard 3 holds the alias-notice test), flow core critical and the rest of the packages / jobs. The delta only removes code and restores __main__.py to main, so I expect them to pass. Merge only once they are green.

Out of scope (unchanged)

The all extra still leaves out wrap, and run_compiled_tool still blocks the MCP event loop for the length of a run.

@abrichr abrichr closed this Nov 22, 2026
@abrichr abrichr reopened this Nov 22, 2026
@abrichr abrichr closed this Nov 22, 2026
@abrichr abrichr reopened this Nov 22, 2026
# Conflicts:
#	CAPABILITIES.md
#	llms-full.txt
#	packages/flow/public-artifacts.json
#	uv.lock
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Claude
Reviewed-Head: 2bb8f56

I found no P0 or P1. The head matches the PR. The diff touches no path listed in .github/agent-safety-manifest.json, so this PR does not need a cross-vendor protected-path review.

Fail-closed behaviour. run_example returns 0 only when run 1 came back on the agent path with status admitted, and run 2 came back on the skill path with verified is True and model_calls == 0. When run 1 isn't admitted, run 2 never runs, so second stays empty and ok is false. If run_example raises, example_main exits 2 and names only the exception type, so no provider text is reflected. A missing key, a missing SDK or a bad cap also exit 2 before anything starts. The printed read-back is for display and doesn't feed ok. Cost and call counts print as unknown when usage isn't known. They are never shown as $0 or as a count. I found no path that reports VERIFIED or exit 0 without the wrap read-back.

run_compiled_tool. When a loop is running, the program runs on a worker thread under contextvars.copy_context(). That copy carries the request binding set by the generation-1 with block, which test_compile_agent.py confirms. Errors are re-raised unchanged, and with no loop the program runs in place, as it does under mcp 2. The worker blocks the loop thread until it finishes, just as the old in-loop call did. server_needs_reemit now also re-emits servers that predate run_compiled_tool, so cached servers pick up the fix. This is a real fix for every compiled browser tool under mcp 1.x. The critical-e2e job on this head ran test_wrap_example_e2e.py, and both tests passed.

Example trust shortcuts. The admitter key is encrypted with a random passphrase that is set only inside _env and then discarded, so the key file can't be reused after the run. The approver and the teach_authorization come from demo_authority and live in the example's own out_dir. The skill cache is under out_dir/cache, so nothing touches the user's key directory or other tasks. A later wrap publish would refuse this admission, because the organization has not registered the admitter key. The unscrubbed opt-out is set only while the example runs, then restored, and it names its reason. The sample app must bind to http://127.0.0.1:, and _serve imports only from openadapt_flow.. Given that, the screenshot opt-out is acceptable for a synthetic app in a fresh headless profile. WRAP.md's comparison table states all three differences accurately.

Generality and secrets. The engine code (example.py, cli.py, pipeline.py, emit.py) names no app. MockMed appears only in the packaged JSON, which generality.toml allow-lists, and in docs. The recorded run JSON contains no key, path or personal data. Its out_dir is redacted.

Claims. The CAPABILITIES row is marked "In PR" and says the one-liner needs a release, which matches the code. The benchmark JSON records the old openadapt-flow wrap --example spelling, and its run block says so openly.

Non-blocking notes:

  • main treats --example anywhere before -- as the example. So openadapt wrap publish --task T --example fails with an argparse error instead of reaching publish. That is harmless, but matching it only as the first argument would be clearer.
  • The wrap extra pins anthropic to python_version < '3.13'. On a 3.13 interpreter, the missing-SDK message points at the 3.12 one-liner, which is correct but may confuse readers.
  • Two red jobs on this head look unrelated to this diff. tests/test_cli_surface.py reports snapshot drift for commands this PR doesn't add, and the desktop overlay contract test expects openadapt-types 0.24.0 but found 0.24.1. They still block the merge until they are fixed on main or by a rebase.

@abrichr

abrichr commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: 5da79d1

Verified clean merge with origin/main (31b6dcd6a0ae17f5a43ac62cee770ae379423855) over the previously approved head 2bb8f562c85a23f91277ea1daf17f8ec1cda8b85:

  • Zero merge conflicts; picks up the main fixes for cli_surface.json and the openadapt-types 0.24.0 revert that caused the prior CI failures.
  • Verified tests/test_cli_surface.py and tests/test_capabilities_map.py: 5 passed, 1 skipped (3.01s).
  • Generality & durability: data-driven wrap example (examples/clinic-note.json), no per-app engine code.

@abrichr

abrichr commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: 098ac19

Head confirmed open and unchanged versus this SHA. I claimed the review in openadapt-internal#211 (7093757371), at Claude's request (7093167041). This is a cross-vendor re-review of a Claude-authored PR; my earlier APPROVE was only at 87e244c106e0. I will not merge it.

This is not publish- or release-triggering: pyproject.toml stays at launcher 1.23.0 and only adds an optional wrap extra (anthropic>=0.40). The capability work adds capabilities/10-computer-use-agents/0145-try-openadapt-wrap-with-one-command.md and leaves the committed CAPABILITIES.md / stamp matching main (93e2f3c7…), so the page is correctly stale until the refresh workflow runs.

Latest head (098ac190)

  • _child_errlog: _call_skill no longer passes sys.stderr blindly to stdio_client. It picks a live, fd-backed stream (sys.stderr, then sys.__stderr__) and otherwise opens /dev/null on an ExitStack. That is the right fix for pytest capsys (and any closed or in-memory stderr) without retrying a failed MCP call. os was already imported.
  • Capability row: the wrap row was removed from the generated page and placed in 0145-…. Status is In PR. The row honestly describes auto-confirmation only on the synthetic app, unscrubbed screenshots under the recorded opt-out, a spend cap, CI using a scripted client, and one dated real Opus 5.5 run.

Rest of the PR, independently checked at this head

  • openadapt wrap remains a launcher verb that forwards argv to openadapt_flow.wrap.cli. --example is a separate parser. The base install does not depend on anthropic.
  • load_pack only reads packaged openadapt_flow.wrap JSON whose name is alphanumeric. _serve only imports openadapt_flow.* and refuses anything that is not listening on 127.0.0.1. The example uses a throwaway admitter in its own folder and demo_authority. Exit status 0 requires run 1 admitted, run 2 on the skill path, verified is True, and model_calls == 0.
  • Generated MCP tools call run_compiled_tool, which runs run_compiled_params on a worker thread when an event loop is already running, so Playwright's sync API is not started on the MCP server loop. Admission, record checks, and receipts stay on the compiler path; the emitted server does not copy the bundle.

Residual P2s (not blocking)

  • When no fd-backed stderr exists, the MCP child's stderr goes to /dev/null, so a host without a real stderr can hide child errors. That is acceptable for tests; a temp file would be noisier to debug but safer for operators.
  • There is no unit test that asserts _child_errlog rejects a StringIO and accepts sys.__stderr__. The e2e path is what failed under capsys.

Review only; I will not merge this PR.

@abrichr
abrichr merged commit 42be285 into main Nov 23, 2026
65 checks passed
@abrichr
abrichr deleted the claude/wrap-example-cmd branch November 23, 2026 07:34
abrichr added a commit that referenced this pull request Nov 23, 2026
The wrap extra lean-install check imported tomllib unconditionally.
tomllib is stdlib only on 3.11+, so Python CI run-ci on ubuntu 3.10
failed deterministically after #2406. Use the same tomllib/tomli
fallback as the other launcher tests. Assertions are unchanged.
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.

2 participants