Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
eaeec8f
feat(wrap): openadapt wrap --example runs the whole loop on a bundled…
abrichr Nov 21, 2026
0dd7909
docs: refresh the review brief
abrichr Nov 21, 2026
9caa729
docs(wrap): record one real --example run
abrichr Nov 21, 2026
31adfed
test(wrap): run the example e2e in PR CI; cover the re-emit marker
abrichr Nov 21, 2026
f49a876
test(compile-agent): generated tools run the program off the server loop
abrichr Nov 21, 2026
87e244c
fix(wrap): type-check the example's pack lookup and provider
abrichr Nov 21, 2026
e1d0eb9
feat(wrap): openadapt wrap -- COMMAND runs any agent process through …
abrichr Nov 21, 2026
59a6821
docs: refresh the review brief
abrichr Nov 21, 2026
9803275
fix(wrap): keep the plain _task call for runs without a command
abrichr Nov 21, 2026
59595e7
fix(wrap): no OpenAdapt settings reach the wrapped command; name a wa…
abrichr Nov 21, 2026
0e68532
ci: run the wrap -- COMMAND e2e in PR CI
abrichr Nov 21, 2026
5e7da87
fix(wrap): the example agent uses the MCP 1 and 2 HTTP client calls
abrichr Nov 21, 2026
3e6eb48
test(wrap): a failed command is a failed capture; wrap run uses the t…
abrichr Nov 21, 2026
3a0fe98
Merge remote-tracking branch 'origin/main' into HEAD
abrichr Nov 21, 2026
6aad21a
fix(wrap): keep __main__ within its budget, put the agent SDK behind …
abrichr Nov 21, 2026
e49e747
Merge commit '6aad21ae1' into HEAD
abrichr Nov 21, 2026
711a40f
fix(wrap): keep wrap a launcher command, not an openadapt-flow verb
abrichr Nov 22, 2026
33cc89e
Merge commit '711a40f43' into HEAD
abrichr Nov 22, 2026
33f71b9
fix(wrap): stop the command's group on signals, derive request IDs, w…
abrichr Nov 22, 2026
eba83fe
fix(wrap): signals stop the command mid tool call; withhold secret_en…
abrichr Nov 22, 2026
f5e314a
fix(wrap): an interrupted earlier attempt says check the record first…
abrichr Nov 22, 2026
6797fe8
fix(wrap): type the saved signal handlers by signal, for mypy
abrichr Nov 22, 2026
2bb8f56
Merge remote-tracking branch 'origin/main' into HEAD
abrichr Nov 22, 2026
5da79d1
Merge commit '31b6dcd6a0ae17f5a43ac62cee770ae379423855' into HEAD
abrichr Nov 23, 2026
3f3cd1d
Merge remote-tracking branch 'origin/claude/wrap-example-cmd' into HEAD
abrichr Nov 23, 2026
6735730
Merge remote-tracking branch 'origin/main' into HEAD
abrichr Nov 23, 2026
42d6728
Merge branch 'main' into claude/wrap-command
abrichr Nov 23, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,7 @@ jobs:
tests/e2e/test_browser_teach_write_guard_e2e.py \
tests/e2e/test_agent_guard_e2e.py \
tests/e2e/test_wrap_example_e2e.py \
tests/e2e/test_wrap_command_e2e.py \
--basetemp=runs/ci-critical-e2e --junitxml=runs/critical-e2e-junit.xml
- name: Golden runs (report, artifacts and actions unchanged)
# docs/design/DECOMPOSITION.md, section 3.1. The baselines are Linux
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| Wrap any agent process: `openadapt wrap --task TASK --param ... -- COMMAND` starts the command for the first run with `OPENADAPT_MCP_URL` (a streamable-HTTP MCP endpoint on 127.0.0.1), `OPENADAPT_MCP_TOKEN` (a random per-run bearer token, compared in constant time, never printed) and `OPENADAPT_TASK`. The endpoint's tools (`observe`, `click`, `type_text`, `press_key`, `scroll`, `finish`) go through compile-agent's harness: the save gate, the read-back after every save, the task's origin limits and the recorder. The command gets no browser handle (no DevTools endpoint), so the draft carries the signed capture admission requires; the endpoint closes when the command exits, a nonzero exit makes no program and is passed through as wrap's exit status, and the command's process group is stopped after `--command-timeout`. Run 1's cost is reported as unknown (the command's model calls are its own). `openadapt wrap run --task TASK` runs only the admitted program, never an agent | In PR | Synthetic (an outside Python process with the `mcp` client SDK and no model does the bundled clinic task through the endpoint in headless Chromium: save gate, read-back, canary, admission by a registered admitter key under a signed teach authorization, then the emitted tool with no model calls; a failing command and a request without the token make no program) | [WRAP.md](packages/flow/docs/WRAP.md#wrap-your-own-agent), `openadapt_flow.wrap.process`, `packages/flow/examples/wrap_command/agent.py`, `packages/flow/tests/e2e/test_wrap_command_e2e.py` |
21 changes: 20 additions & 1 deletion openadapt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2286,7 +2286,26 @@ def wrap(command_ctx: click.Context) -> None:
"Error: this openadapt-flow installation has no wrap command.", err=True
)
sys.exit(2)
sys.exit(int(wrap_main(list(command_ctx.args)) or 0))
sys.exit(int(wrap_main(_wrap_arguments(list(command_ctx.args))) or 0))


def _wrap_arguments(parsed: list[str]) -> list[str]:
"""``wrap``'s arguments with the ``--`` before a wrapped command kept.

Click consumes ``--``, but ``openadapt wrap --task T -- CMD`` needs it to
tell wrap's options from the command's. The process's own argv is used
only when it holds exactly the same arguments apart from separators.
"""

argv = list(sys.argv[1:])
if "wrap" not in argv:
return parsed
raw = argv[argv.index("wrap") + 1 :]
if "--" in raw and [a for a in raw if a != "--"] == [
a for a in parsed if a != "--"
]:
return raw
return parsed


@main.command(
Expand Down
91 changes: 91 additions & 0 deletions packages/flow/docs/WRAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,97 @@ recorded in `~/.openadapt/unscrubbed-sends.jsonl`.
Without a model key the example stops before anything starts and names the
variable to set (`[MISSING_MODEL_KEY]`).

## Wrap your own agent

`openadapt wrap --task task.json --param NAME=VALUE -- COMMAND ...` runs your
agent, any program, for the first run instead of OpenAdapt's built-in agent.
OpenAdapt opens the task's app in its own headless browser and starts
`COMMAND` with three environment variables:

| Variable | What it holds |
|---|---|
| `OPENADAPT_MCP_URL` | A Model Context Protocol endpoint (streamable HTTP) on `127.0.0.1` |
| `OPENADAPT_MCP_TOKEN` | A random token for this run. Send it as `Authorization: Bearer <token>`; other requests get `401` |
| `OPENADAPT_TASK` | The task with its inputs, as the agent should read it |

The endpoint has six tools: `observe` (the page as a listing, with refs such
as `e3` on its controls), `click`, `type_text`, `press_key`, `scroll` and
`finish`. They are the tools OpenAdapt's own agents use, with the same
checks:

- Before a save, the record the task names must be on the screen, or the
save is refused and the run ends.
- Every save is read back from the system of record. A change to another
record, a second save or an unreadable store ends the run.
- An action that leaves the task's origins ends the run.
- Every action is recorded for the compiled program.

The command gets no handle to the browser itself, so it can't act around
these checks. It also gets your environment without any `OPENADAPT_*`
variable and without any variable the task's `verify.yaml` reads (each
`${NAME}` reference and each `*_env` value, including every value of a
`secret_env` map, such as a database password or an API token), so
OpenAdapt's own keys and settings and the system of record's credentials
never reach it; its own variables, such as its model key, pass through.

The command runs in its own process group. `--command-timeout` (default 600
seconds) ends the whole group, and so do Ctrl-C and SIGTERM sent to
`openadapt wrap`. Without `--request-id`, the run's request ID is derived from
the task, the command and its inputs, so asking for the same run again
dedupes instead of saving twice. A deduped run prints its earlier result and
says so on stderr; to run it again (for example after a failure), pass a new
`--request-id`. If the earlier attempt was interrupted and may have saved
(`RECONCILIATION_REQUIRED`), check the record in the app first: a new
`--request-id` then could save twice.

When the command exits, the endpoint closes. Then the run goes through the
same record check, proposed checks, canary and admission as any
`openadapt wrap --task` run (see [JIT_ADMISSION.md](JIT_ADMISSION.md)). Run it
in a terminal with `--person "Your Name"`: you confirm the proposed read-back
checks, review the steps and attend the canary there. Without `--person`, run 1
stays a draft, and `wrap run` reports `[DRAFT_NOT_ADMITTED]`.

```bash
openadapt wrap --task task.json --param note="Synthetic follow-up" \
--person "Your Name" -- python my_agent.py
```

A nonzero exit makes no program, and `openadapt wrap` exits with the
command's status. The command is stopped after `--command-timeout` seconds
(default 600).

When the privacy extra scrubs what the agent sees, the task's own values (its
record's identity and its inputs) appear as placeholders such as
`⟦task.identity_1⟧` in `OPENADAPT_TASK` and in every listing. Typing a
placeholder types its value. The endpoint's instructions say so too.

[`examples/wrap_command/agent.py`](../examples/wrap_command/agent.py) is a
small agent with no model that does the sample clinic task through the
endpoint, with the `mcp` client SDK:

```python
async with streamablehttp_client(url, headers=headers) as (read, write, _):
async with ClientSession(read, write) as session:
await session.initialize()
listing = await session.call_tool("observe", {})
```

OpenAdapt can't see the model calls your command makes, so `openadapt wrap`
reports run 1's cost as unknown.

The command runs as an ordinary process with your permissions. Only the
browser it drives belongs to OpenAdapt.

### Later runs

```bash
openadapt wrap run --task task.json --param note="Recheck knee in ten days"
```

`wrap run` runs the admitted program only: no command and no model calls,
and the save is read back. A task with no admitted program stops with
`[NO_ADMITTED_PROGRAM]` and runs nothing.

## Wrap your own task

Describe the task, its read-back and its record in a task file, then run
Expand Down
91 changes: 91 additions & 0 deletions packages/flow/examples/wrap_command/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""A small outside agent for ``openadapt wrap -- COMMAND`` (no model).

It connects to the MCP endpoint OpenAdapt gives it (``OPENADAPT_MCP_URL``,
with ``OPENADAPT_MCP_TOKEN``), reads ``OPENADAPT_TASK``, and does the sample
clinic task by reading each listing and choosing the next control, the way a
model-driven agent would. Its choices use the listing only.

Run it under wrap::

openadapt wrap --task task.json --param note="..." -- python mcp_agent.py
"""

from __future__ import annotations

import asyncio
import os
import re
import sys


def _ref(listing: str, name: str, near: str | None = None) -> str:
for line in listing.splitlines():
if near and near not in line:
continue
found = re.search(r"\[(e\d+)\] \S+ \"" + re.escape(name) + '"', line)
if found:
return found.group(1)
raise SystemExit(f"no control named {name!r} on the page")


async def main() -> int:
from mcp import ClientSession
from mcp.client.streamable_http import streamable_http_client
from mcp.shared._httpx_utils import create_mcp_http_client

url = os.environ["OPENADAPT_MCP_URL"]
headers = {"Authorization": "Bearer " + os.environ["OPENADAPT_MCP_TOKEN"]}
task = os.environ["OPENADAPT_TASK"]
# The task names the record and the note either as values or, when the
# listings are scrubbed, as placeholders; the listings use the same form.
found = re.search(r"referral for (.+?), start", task)
if found is None:
raise SystemExit("the task names no referral")
record = found.group(1)
note = task.rsplit(": ", 1)[1].strip()
# The same calls work with the MCP 1 and MCP 2 client SDKs.
async with create_mcp_http_client(headers=headers) as http:
async with streamable_http_client(url, http_client=http) as streams:
read, write = streams[0], streams[1]
async with ClientSession(read, write) as session:
await session.initialize()

async def call(name: str, **args: str) -> str:
result = await session.call_tool(name, args)
text = "".join(getattr(c, "text", "") for c in result.content)
if text.startswith("Stopped:"):
raise SystemExit(text)
return text

listing = await call("observe")
triage_done = typed = False
for _ in range(20):
if "Encounter saved" in listing:
await call("finish", summary="saved the note")
return 0
if "# Referral Tasks" in listing:
listing = await call("click", ref=_ref(listing, "Open", record))
elif "# New Encounter" in listing:
if not triage_done:
triage_done = True
listing = await call("click", ref=_ref(listing, "Triage"))
elif not typed:
typed = True
listing = await call(
"type_text", text=note, ref=_ref(listing, "Note")
)
else:
listing = await call(
"click", ref=_ref(listing, "Save Encounter")
)
elif "New Encounter" in listing:
listing = await call(
"click", ref=_ref(listing, "New Encounter")
)
else:
raise SystemExit("unexpected page")
return 1


if __name__ == "__main__":
sys.exit(asyncio.run(main()))
Loading
Loading