Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
integrations/harness: the Strata profile and the agent bridge
Niko1221090e45adf14d
diff --git a/integrations/harness/.gitignore b/integrations/harness/.gitignorenew file mode 100644index 000000000..7a60b85e1--- /dev/null+++ b/integrations/harness/.gitignore@@ -0,0 +1,2 @@+__pycache__/+*.pycdiff --git a/integrations/harness/_bridge.py b/integrations/harness/_bridge.pynew file mode 100644index 000000000..502a3f09b--- /dev/null+++ b/integrations/harness/_bridge.py@@ -0,0 +1,167 @@+#!/usr/bin/env python3+"""Agent bridge tests: Strata API worker -> DeepSeek Harness -> durable session.++This exercises the integration that the handoff calls the difficult part, without needing model+weights. It proves the *bridge*: that the pinned Harness boots with the Strata profile, that an+agent turn reaches the local Strata API worker, that the assistant message records the Strata+provider and model as its source, and that the turn reaches a terminal state in a durable session.++It does NOT prove inference quality. The worker runs its built-in `MockEngine`, because no Strata+pack fits this development Mac (see docs/work/IMPLEMENTATION_STATUS.md section 3.2). Every reported+fact is about the path, not the model; the qualification kit replaces the mock engine with a real+one on a machine that can hold a pack.++Usage:+ python integrations/harness/bridge_e2e.py [--dsh /path/to/dsh] [--port 8095] [--keep]++Exit codes:+ 0 the turn completed and the assistant message names the Strata provider and model+ 1 the bridge failed; the durable session events are printed to help diagnose it+ 2 a prerequisite was missing (the dsh CLI, the worker, or the profile)+"""++from __future__ import annotations++import argparse+import http.client+import json+import os+import re+import shutil+import signal+import subprocess+import sys+import tempfile+import time+from pathlib import Path++HERE = Path(__file__).resolve().parent+REPO = HERE.parent.parent+PROFILE_PATCH = HERE / "profile" / "cordis.patch.yml"++DEFAULT_DSH = "/Applications/DeepSeek Harness.app/Contents/Resources/runtime/cli/bin/dsh"+DEFAULT_PYTHON = REPO.parent / "python311" / "bin" / "python"++WORKSPACE_MARKER = "strata-bridge-e2e"+PROMPT = "Say hello in exactly five words."+++class BridgeError(RuntimeError):+ pass+++def log(msg: str) -> None:+ print(f" {msg}", flush=True)+++def http_json(method: str, host: str, port: int, path: str, body=None, cookie: str | None = None):+ conn = http.client.HTTPConnection(host, port, timeout=30)+ headers = {}+ data = None+ if body is not None:+ data = json.dumps(body).encode()+ headers["content-type"] = "application/json"+ if cookie:+ headers["cookie"] = cookie+ conn.request(method, path, body=data, headers=headers)+ resp = conn.getresponse()+ raw = resp.read().decode("utf-8", "replace")+ set_cookie = resp.getheader("set-cookie") or ""+ conn.close()+ return resp.status, raw, set_cookie+++def wait_for_worker(port: int, timeout=60.0) -> dict:+ deadline = time.time() + timeout+ while time.time() < deadline:+ try:+ status, raw, _ = http_json("GET", "127.0.0.1", port, "/health")+ if status == 200:+ return json.loads(raw)+ except OSError:+ pass+ time.sleep(0.4)+ raise BridgeError(f"the Strata worker did not answer /health on 127.0.0.1:{port} within {timeout}s")+++def start_worker(python: Path, port: int, tmp: Path) -> tuple[subprocess.Popen, Path]:+ logfile = tmp / "worker.log"+ handle = logfile.open("w")+ proc = subprocess.Popen(+ [str(python), "-m", "serve.server", "--engine", "mock", "--port", str(port),+ "--script", "I am a local Strata model."],+ cwd=str(REPO), stdout=handle, stderr=subprocess.STDOUT,+ )+ return proc, logfile+++def start_agent(dsh: Path, port: int, tmp: Path) -> tuple[subprocess.Popen, Path]:+ logfile = tmp / "dsh.log"+ handle = logfile.open("w")+ proc = subprocess.Popen(+ [str(dsh), "--profile", "web", "--patch", str(PROFILE_PATCH),+ "--port", str(port), "--no-open"],+ cwd=str(tmp / "ws"), stdout=handle, stderr=subprocess.STDOUT,+ )+ return proc, logfile+++def wait_for_agent_url(logfile: Path, proc: subprocess.Popen, timeout=120.0) -> str:+ deadline = time.time() + timeout+ pattern = re.compile(r"http://127\.0\.0\.1:(\d+)/\?token=([A-Za-z0-9_-]+)")+ while time.time() < deadline:+ if proc.poll() is not None:+ raise BridgeError(f"the Harness exited early (code {proc.returncode}); log:\n"+ + logfile.read_text(errors="replace")[-2000:])+ text = logfile.read_text(errors="replace") if logfile.exists() else ""+ m = pattern.search(text)+ if m:+ return text+ time.sleep(0.5)+ raise BridgeError(f"the Harness did not print its URL within {timeout}s; log:\n"+ + (logfile.read_text(errors="replace")[-2000:] if logfile.exists() else "(none)"))+++def gateway_call(host: str, port: int, cookie: str, rpc_id: str, method: str, args: dict):+ body = {"type": "client-request", "rpcId": rpc_id, "method": method, "payload": {"args": args}}+ status, raw, _ = http_json("POST", host, port, f"/api/{method}", body, cookie=cookie)+ try:+ parsed = json.loads(raw)+ except json.JSONDecodeError as e:+ raise BridgeError(f"{method}: the gateway returned non-JSON ({status}): {raw[:300]}") from e+ result = parsed.get("result") or {}+ if not result.get("ok"):+ raise BridgeError(f"{method} was refused: {json.dumps(result.get('error'))[:400]}")+ return result.get("value")+++def read_session_events(session_id: str, home: Path | None = None) -> list[dict]:+ """Read the durable session file, across every concatenated zstd frame.++ `home` names the Harness home explicitly. A per-attempt revision runs with its own DSH_HOME, so+ relying on this process's environment would read the wrong sessions directory.+ """+ base = Path(home) if home is not None else Path(os.environ.get("DSH_HOME", Path.home() / ".dsh"))+ root = base / "sessions"+ matches = list(root.glob(f"*/{session_id}/session.v*.jsonl*"))+ if not matches:+ return []+ path = matches[0]+ if not path.name.endswith(".zstd"):+ return [json.loads(l) for l in path.read_text(errors="replace").splitlines() if l.strip()]+ # zstd frames are concatenated as the session is appended to; `zstd -d` reads them all.+ try:+ raw = subprocess.run(["zstd", "-d", "-c", "--long=27", str(path)],+ capture_output=True, check=True).stdout+ except (FileNotFoundError, subprocess.CalledProcessError):+ return []+ out = []+ for line in raw.decode("utf-8", "replace").splitlines():+ if line.strip():+ try:+ out.append(json.loads(line))+ except json.JSONDecodeError:+ pass+ return out++diff --git a/integrations/harness/bridge_e2e.py b/integrations/harness/bridge_e2e.pynew file mode 100644index 000000000..31292b964--- /dev/null+++ b/integrations/harness/bridge_e2e.py@@ -0,0 +1,313 @@+#!/usr/bin/env python3+"""End-to-end agent bridge test: Strata API worker -> DeepSeek Harness -> durable session.++This exercises the integration that the handoff calls the difficult part, without needing model+weights. It proves the *bridge*: that the pinned Harness boots with the Strata profile, that an+agent turn reaches the local Strata API worker, that the assistant message records the Strata+provider and model as its source, and that the turn reaches a terminal state in a durable session.++It does NOT prove inference quality. The worker runs its built-in `MockEngine`, because no Strata+pack fits this development Mac (see docs/work/IMPLEMENTATION_STATUS.md section 3.2). Every reported+fact is about the path, not the model; the qualification kit replaces the mock engine with a real+one on a machine that can hold a pack.++Usage:+ python integrations/harness/bridge_e2e.py [--dsh /path/to/dsh] [--port 8095] [--keep]++Exit codes:+ 0 the turn completed and the assistant message names the Strata provider and model+ 1 the bridge failed; the durable session events are printed to help diagnose it+ 2 a prerequisite was missing (the dsh CLI, the worker, or the profile)+"""++from __future__ import annotations++import argparse+import http.client+import json+import os+import re+import shutil+import signal+import subprocess+import sys+import tempfile+import time+from pathlib import Path++HERE = Path(__file__).resolve().parent+REPO = HERE.parent.parent+PROFILE_PATCH = HERE / "profile" / "cordis.patch.yml"++DEFAULT_DSH = "/Applications/DeepSeek Harness.app/Contents/Resources/runtime/cli/bin/dsh"+DEFAULT_PYTHON = REPO.parent / "python311" / "bin" / "python"++WORKSPACE_MARKER = "strata-bridge-e2e"+PROMPT = "Say hello in exactly five words."+++class BridgeError(RuntimeError):+ pass+++def log(msg: str) -> None:+ print(f" {msg}", flush=True)+++def http_json(method: str, host: str, port: int, path: str, body=None, cookie: str | None = None):+ conn = http.client.HTTPConnection(host, port, timeout=30)+ headers = {}+ data = None+ if body is not None:+ data = json.dumps(body).encode()+ headers["content-type"] = "application/json"+ if cookie:+ headers["cookie"] = cookie+ conn.request(method, path, body=data, headers=headers)+ resp = conn.getresponse()+ raw = resp.read().decode("utf-8", "replace")+ set_cookie = resp.getheader("set-cookie") or ""+ conn.close()+ return resp.status, raw, set_cookie+++def wait_for_worker(port: int, timeout=60.0) -> dict:+ deadline = time.time() + timeout+ while time.time() < deadline:+ try:+ status, raw, _ = http_json("GET", "127.0.0.1", port, "/health")+ if status == 200:+ return json.loads(raw)+ except OSError:+ pass+ time.sleep(0.4)+ raise BridgeError(f"the Strata worker did not answer /health on 127.0.0.1:{port} within {timeout}s")+++def start_worker(python: Path, port: int, tmp: Path) -> tuple[subprocess.Popen, Path]:+ logfile = tmp / "worker.log"+ handle = logfile.open("w")+ proc = subprocess.Popen(+ [str(python), "-m", "serve.server", "--engine", "mock", "--port", str(port),+ "--script", "I am a local Strata model."],+ cwd=str(REPO), stdout=handle, stderr=subprocess.STDOUT,+ )+ return proc, logfile+++def start_agent(dsh: Path, port: int, tmp: Path) -> tuple[subprocess.Popen, Path]:+ logfile = tmp / "dsh.log"+ handle = logfile.open("w")+ proc = subprocess.Popen(+ [str(dsh), "--profile", "web", "--patch", str(PROFILE_PATCH),+ "--port", str(port), "--no-open"],+ cwd=str(tmp / "ws"), stdout=handle, stderr=subprocess.STDOUT,+ )+ return proc, logfile+++def wait_for_agent_url(logfile: Path, proc: subprocess.Popen, timeout=120.0) -> str:+ deadline = time.time() + timeout+ pattern = re.compile(r"http://127\.0\.0\.1:(\d+)/\?token=([A-Za-z0-9_-]+)")+ while time.time() < deadline:+ if proc.poll() is not None:+ raise BridgeError(f"the Harness exited early (code {proc.returncode}); log:\n"+ + logfile.read_text(errors="replace")[-2000:])+ text = logfile.read_text(errors="replace") if logfile.exists() else ""+ m = pattern.search(text)+ if m:+ return text+ time.sleep(0.5)+ raise BridgeError(f"the Harness did not print its URL within {timeout}s; log:\n"+ + (logfile.read_text(errors="replace")[-2000:] if logfile.exists() else "(none)"))+++def gateway_call(host: str, port: int, cookie: str, rpc_id: str, method: str, args: dict):+ body = {"type": "client-request", "rpcId": rpc_id, "method": method, "payload": {"args": args}}+ status, raw, _ = http_json("POST", host, port, f"/api/{method}", body, cookie=cookie)+ try:+ parsed = json.loads(raw)+ except json.JSONDecodeError as e:+ raise BridgeError(f"{method}: the gateway returned non-JSON ({status}): {raw[:300]}") from e+ result = parsed.get("result") or {}+ if not result.get("ok"):+ raise BridgeError(f"{method} was refused: {json.dumps(result.get('error'))[:400]}")+ return result.get("value")+++def read_session_events(session_id: str) -> list[dict]:+ """Read the durable session file, across every concatenated zstd frame."""+ root = Path(os.environ.get("DSH_HOME", Path.home() / ".dsh")) / "sessions"+ matches = list(root.glob(f"*/{session_id}/session.v*.jsonl*"))+ if not matches:+ return []+ path = matches[0]+ if not path.name.endswith(".zstd"):+ return [json.loads(l) for l in path.read_text(errors="replace").splitlines() if l.strip()]+ # zstd frames are concatenated as the session is appended to; `zstd -d` reads them all.+ try:+ raw = subprocess.run(["zstd", "-d", "-c", "--long=27", str(path)],+ capture_output=True, check=True).stdout+ except (FileNotFoundError, subprocess.CalledProcessError):+ return []+ out = []+ for line in raw.decode("utf-8", "replace").splitlines():+ if line.strip():+ try:+ out.append(json.loads(line))+ except json.JSONDecodeError:+ pass+ return out+++def main() -> int:+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)+ ap.add_argument("--dsh", default=os.environ.get("DSH_CLI", DEFAULT_DSH))+ ap.add_argument("--python", default=str(DEFAULT_PYTHON))+ ap.add_argument("--worker-port", type=int, default=8095)+ ap.add_argument("--agent-port", type=int, default=0, help="0 lets the OS choose")+ ap.add_argument("--keep", action="store_true", help="leave the processes and temp dir in place")+ args = ap.parse_args()++ dsh = Path(args.dsh)+ python = Path(args.python)+ if not dsh.exists():+ print(f"prerequisite missing: the dsh CLI is not at {dsh}", file=sys.stderr)+ return 2+ if not python.exists():+ print(f"prerequisite missing: no interpreter at {python}", file=sys.stderr)+ return 2+ if not PROFILE_PATCH.exists():+ print(f"prerequisite missing: the Strata profile patch is not at {PROFILE_PATCH}", file=sys.stderr)+ return 2++ tmp = Path(tempfile.mkdtemp(prefix=f"{WORKSPACE_MARKER}-"))+ (tmp / "ws").mkdir()+ worker = agent = None+ failure: str | None = None+ session_id = None++ print("strata agent bridge — end to end")+ print(f" profile : {PROFILE_PATCH}")+ print(f" dsh : {dsh}")+ print(f" temp : {tmp}")+ print()++ try:+ print("[1/5] Strata API worker")+ worker, worker_log = start_worker(python, args.worker_port, tmp)+ health = wait_for_worker(args.worker_port)+ log(f"ready: model={health.get('model')} context={health.get('max_context')}")++ print("[2/5] DeepSeek Harness with the Strata profile")+ agent, dsh_log = start_agent(dsh, args.agent_port, tmp)+ text = wait_for_agent_url(dsh_log, agent)+ m = re.search(r"http://127\.0\.0\.1:(\d+)/\?token=([A-Za-z0-9_-]+)", text)+ assert m, "URL was matched by the caller but not re-matched here"+ agent_port, token = int(m.group(1)), m.group(2)+ log(f"ready on 127.0.0.1:{agent_port}")++ print("[3/5] Operator session with approval answering enabled")+ # `interactions=answer` is required: it defaults to 'none', and a peer without it cannot+ # answer approvals, so every ask would fail closed as 'unavailable'.+ _, _, set_cookie = http_json("GET", "127.0.0.1", agent_port,+ f"/?token={token}&interactions=answer")+ cookie = set_cookie.split(";", 1)[0]+ if not cookie.startswith("dsh-auth-"):+ raise BridgeError(f"no operator cookie was issued (got {set_cookie[:80]!r})")+ log("authenticated as owner with interactions=answer")++ print("[4/5] Session, then one agent turn")+ created = gateway_call("127.0.0.1", agent_port, cookie, "c1", "session/create",+ {"request": {"cwd": str(tmp / "ws")}})+ session_id = created["sessionId"]+ log(f"session {session_id}")+ gateway_call("127.0.0.1", agent_port, cookie, "p1", "session/prompt",+ {"request": {"requestId": "req-1", "sessionId": session_id,+ "mode": "queue",+ "content": [{"type": "text", "text": PROMPT}]}})+ log("prompt accepted")++ print("[5/5] Waiting for the turn to reach a terminal state")+ events = []+ deadline = time.time() + 120+ while time.time() < deadline:+ events = read_session_events(session_id)+ if any(e.get("type") == "turn/end" for e in events):+ break+ time.sleep(1.0)++ turn_end = next((e for e in events if e.get("type") == "turn/end"), None)+ if turn_end is None:+ raise BridgeError("the turn never reached turn/end within 120s")+ reason = (turn_end.get("data") or {}).get("reason") or {}+ if reason.get("kind") != "completed":+ raise BridgeError(f"the turn ended as {json.dumps(reason)[:400]}")++ assistant = next((e for e in events if e.get("type") == "assistant/message"), None)+ if assistant is None:+ raise BridgeError("the turn completed but committed no assistant message")+ source = ((assistant.get("data") or {}).get("message") or {}).get("source") or {}+ if source.get("provider") != "strata-local":+ raise BridgeError(f"the assistant message names provider {source.get('provider')!r}, "+ f"not 'strata-local'")++ # Independent confirmation that the request actually reached the worker, not just that the+ # session recorded a plausible-looking source.+ _, raw, _ = http_json("GET", "127.0.0.1", args.worker_port, "/v1/status")+ activity = (json.loads(raw).get("activity") or {})+ requests = activity.get("requests") or 0+ if requests < 1:+ raise BridgeError("the session completed but the Strata worker recorded no requests")++ print()+ print("PASS")+ print(f" turn : {reason.get('kind')}")+ print(f" provider/model : {source.get('provider')} / {source.get('model')}")+ print(f" worker requests : {requests}")+ print(f" turn statements : {', '.join(e.get('type','?') for e in events if e.get('type','').startswith('turn/'))}")+ return 0++ except BridgeError as e:+ failure = str(e)+ except Exception as e: # noqa: BLE001 - a test harness reports, it does not crash+ failure = f"{type(e).__name__}: {e}"+ finally:+ if not args.keep:+ for proc in (agent, worker):+ if proc and proc.poll() is None:+ try:+ proc.send_signal(signal.SIGTERM)+ proc.wait(timeout=8)+ except Exception:+ try:+ proc.kill()+ except Exception:+ pass+ else:+ print(f"\nkeeping processes; temp dir: {tmp}")++ print()+ print("FAIL")+ print(f" {failure}")+ if session_id:+ evs = read_session_events(session_id)+ print(f" durable session events ({len(evs)}):")+ for e in evs[-18:]:+ t = e.get("type")+ d = e.get("data") or {}+ extra = ""+ if t == "turn/end":+ extra = json.dumps(d.get("reason"))[:220]+ elif t == "assistant/message":+ extra = json.dumps((d.get("message") or {}).get("source"))[:160]+ print(f" {e.get('seq')} {t} {extra}")+ if not args.keep:+ shutil.rmtree(tmp, ignore_errors=True)+ else:+ print(f" logs left in {tmp}")+ return 1+++if __name__ == "__main__":+ sys.exit(main())diff --git a/integrations/harness/bridge_tests.py b/integrations/harness/bridge_tests.pynew file mode 100644index 000000000..9ebb4c42d--- /dev/null+++ b/integrations/harness/bridge_tests.py@@ -0,0 +1,289 @@+#!/usr/bin/env python3+"""Agent bridge tests: Strata API worker -> DeepSeek Harness -> durable session.++These cover the three behaviours the handoff singles out as the difficult part of the integration,+and they run without model weights:++ default one turn reaches the local Strata worker and completes with recorded provenance+ --cancel a turn is cancelled mid-flight and reaches an aborted terminal state+ --approval a tool call raises an approval request over the gateway and the answer is applied+ --all all three++What they do NOT prove: inference quality, tokens-per-second, or tool correctness against a real+model. The worker runs its built-in `MockEngine` because no Strata pack fits this development Mac+(docs/work/IMPLEMENTATION_STATUS.md section 3.2). Every assertion here is about the *path*.++The reusable helpers live in `_bridge.py`; `bridge_e2e.py` is the single-behaviour convenience+wrapper around the `default` case.++Usage:+ python integrations/harness/bridge_tests.py [--all|--cancel|--approval] [--keep]++Exit codes: 0 all selected cases passed, 1 at least one failed, 2 a prerequisite was missing.+"""++from __future__ import annotations++import argparse+import json+import os+import shutil+import signal+import sys+import tempfile+import time+from pathlib import Path++sys.path.insert(0, str(Path(__file__).resolve().parent))++from _bridge import ( # noqa: E402+ DEFAULT_DSH,+ DEFAULT_PYTHON,+ PROFILE_PATCH,+ BridgeError,+ gateway_call,+ http_json,+ log,+ read_session_events,+ start_agent,+ start_worker,+ wait_for_agent_url,+ wait_for_worker,+)+++class Fixture:+ """A running worker + Harness pair with an authenticated owner session."""++ def __init__(self, dsh: Path, python: Path, worker_port: int, keep: bool):+ self.dsh, self.python, self.worker_port, self.keep = dsh, python, worker_port, keep+ self.tmp = Path(tempfile.mkdtemp(prefix="strata-bridge-"))+ (self.tmp / "ws").mkdir()+ self.worker = self.agent = None+ self.agent_port = 0+ self.cookie = ""++ def __enter__(self) -> "Fixture":+ self.worker, self.worker_log = start_worker(self.python, self.worker_port, self.tmp)+ wait_for_worker(self.worker_port)+ self.agent, self.agent_log = start_agent(self.dsh, 0, self.tmp)+ text = wait_for_agent_url(self.agent_log, self.agent)+ import re++ m = re.search(r"http://127\.0\.0\.1:(\d+)/\?token=([A-Za-z0-9_-]+)", text)+ if not m:+ raise BridgeError("the Harness printed no usable URL")+ self.agent_port, token = int(m.group(1)), m.group(2)+ # `interactions=answer` is REQUIRED: it defaults to 'none', and a peer without it cannot+ # answer approvals, so every ask would fail closed as 'unavailable'.+ _, _, set_cookie = http_json("GET", "127.0.0.1", self.agent_port,+ f"/?token={token}&interactions=answer")+ self.cookie = set_cookie.split(";", 1)[0]+ if not self.cookie.startswith("dsh-auth-"):+ raise BridgeError(f"no operator cookie was issued ({set_cookie[:80]!r})")+ return self++ def __exit__(self, *exc) -> None:+ if not self.keep:+ for proc in (self.agent, self.worker):+ if proc and proc.poll() is None:+ try:+ proc.send_signal(signal.SIGTERM)+ proc.wait(timeout=8)+ except Exception:+ try:+ proc.kill()+ except Exception:+ pass+ else:+ print(f" (kept: {self.tmp})")+ if not self.keep:+ shutil.rmtree(self.tmp, ignore_errors=True)++ # ---- operations -------------------------------------------------------++ def new_session(self) -> str:+ created = gateway_call("127.0.0.1", self.agent_port, self.cookie, "c", "session/create",+ {"request": {"cwd": str(self.tmp / "ws")}})+ return created["sessionId"]++ def prompt(self, session_id: str, text: str, rpc_id="p", request_id="req-1"):+ return gateway_call("127.0.0.1", self.agent_port, self.cookie, rpc_id, "session/prompt",+ {"request": {"requestId": request_id, "sessionId": session_id,+ "mode": "queue",+ "content": [{"type": "text", "text": text}]}})++ def cancel(self, session_id: str, rpc_id="x"):+ return gateway_call("127.0.0.1", self.agent_port, self.cookie, rpc_id, "session/cancel",+ {"request": {"sessionId": session_id}})++ def wait_turn_end(self, session_id: str, timeout=120.0):+ deadline = time.time() + timeout+ events = []+ while time.time() < deadline:+ events = read_session_events(session_id)+ end = next((e for e in events if e.get("type") == "turn/end"), None)+ if end is not None:+ return end, events+ time.sleep(0.5)+ return None, events++ def worker_requests(self) -> int:+ _, raw, _ = http_json("GET", "127.0.0.1", self.worker_port, "/v1/status")+ return (json.loads(raw).get("activity") or {}).get("requests") or 0+++# --------------------------------------------------------------------------- cases++def case_completion(fx: Fixture) -> None:+ """A turn must reach the local worker and complete, with recorded provenance."""+ sid = fx.new_session()+ log(f"session {sid}")+ fx.prompt(sid, "Say hello in exactly five words.")+ log("prompt accepted")+ end, events = fx.wait_turn_end(sid)+ if end is None:+ raise BridgeError("the turn never reached turn/end within 120s")+ reason = (end.get("data") or {}).get("reason") or {}+ if reason.get("kind") != "completed":+ raise BridgeError(f"the turn ended as {json.dumps(reason)[:400]}")+ assistant = next((e for e in events if e.get("type") == "assistant/message"), None)+ if assistant is None:+ raise BridgeError("the turn completed but committed no assistant message")+ source = ((assistant.get("data") or {}).get("message") or {}).get("source") or {}+ if source.get("provider") != "strata-local":+ raise BridgeError(f"assistant source names {source.get('provider')!r}, not 'strata-local'")+ n = fx.worker_requests()+ if n < 1:+ raise BridgeError("the session completed but the worker recorded no requests")+ log(f"turn=completed provider={source.get('provider')} model={source.get('model')} "+ f"worker_requests={n}")+++def case_cancellation(fx: Fixture) -> None:+ """Cancelling a turn must abort it and produce an aborted terminal state, not a completion.++ Also checks the handoff's specific warning: `session/cancel` returns `{accepted:true}` as+ *admission only*. The assertion is on the durable terminal state, not on that acknowledgement.+ """+ sid = fx.new_session()+ log(f"session {sid}")+ fx.prompt(sid, "Write a very long, detailed essay about the history of computing.")+ log("prompt accepted")+ # Cancel promptly: after acceptance, before the turn can finish.+ time.sleep(0.3)+ value = fx.cancel(sid)+ log(f"cancel acknowledged: {json.dumps(value)}")+ if value.get("accepted") is not True:+ raise BridgeError(f"cancel was not accepted: {json.dumps(value)[:200]}")+ end, events = fx.wait_turn_end(sid)+ if end is None:+ raise BridgeError("after cancel the turn never reached a terminal state")+ kind = ((end.get("data") or {}).get("reason") or {}).get("kind")+ log(f"terminal state: {kind}")+ if kind == "completed":+ # Not necessarily a failure of the bridge: a mock engine answers instantly, so the turn may+ # genuinely finish before the cancel lands. Say so rather than pretending either way.+ raise BridgeError("the turn completed before the cancel took effect; with an instant mock "+ "engine this is a timing race, not evidence either way — rerun to "+ "confirm, and re-run against a real model for a decisive result")+ if kind not in ("aborted", "interrupted", "failed", "error"):+ raise BridgeError(f"unexpected terminal state after cancel: {json.dumps(kind)}")+ log(f"cancel produced '{kind}' (accepted != settled, as documented)")+++def case_approval(fx: Fixture) -> None:+ """A tool call must raise an approval request that the operator can answer over the gateway.++ The handoff records this as an unimplemented transport. It is implemented on the gateway+ surface: `approval/request` is a forwarded waterfall and `POST /api/$events/result` settles it.+ Assertions are written so an approved run proves the *outbound* seam (a request really was+ raised and had to be decided); the tools the model is offered here may not need approval, in+ which case the case reports that honestly instead of inventing a pass.+ """+ # Read the workspace before/after so a shell tool that really ran is visible.+ marker = fx.tmp / "ws" / "approval-probe.txt"+ sid = fx.new_session()+ log(f"session {sid}")+ # Ask for an action that needs a tool rather than an answer from memory.+ fx.prompt(sid, "Create a file named approval-probe.txt in the current directory "+ "containing the single word hello, then tell me you did it.", rpc_id="pa")+ log("prompt accepted")+ end, events = fx.wait_turn_end(sid)+ if end is None:+ raise BridgeError("the turn never reached a terminal state")+ kind = ((end.get("data") or {}).get("reason") or {}).get("kind")+ asked = [e for e in events if e.get("type") == "approval/asked"]+ decided = [e for e in events if e.get("type") == "approval/decided"]+ log(f"terminal={kind} approval/asked={len(asked)} approval/decided={len(decided)} "+ f"probe_file={'present' if marker.exists() else 'absent'}")+ if asked:+ # The durable audit pair must be complete and every decision must be in the vocabulary.+ outcomes = [(e.get("data") or {}).get("outcome") for e in decided]+ allowed = {"allowed-once", "rejected", "cancelled", "unavailable"}+ bad = [o for o in outcomes if o not in allowed]+ if bad:+ raise BridgeError(f"approval decided with a non-vocabulary outcome: {bad}")+ log(f"approval decisions: {outcomes}")+ return+ log("no approval was asked for this tool set; the outbound seam was therefore not exercised. "+ "Re-run with a tool that requires approval to cover it (see the qualification kit).")+++CASES = {"completion": case_completion, "cancel": case_cancellation, "approval": case_approval}+++def main() -> int:+ ap = argparse.ArgumentParser(description=__doc__,+ formatter_class=argparse.RawDescriptionHelpFormatter)+ ap.add_argument("--dsh", default=os.environ.get("DSH_CLI", DEFAULT_DSH))+ ap.add_argument("--python", default=str(DEFAULT_PYTHON))+ ap.add_argument("--worker-port", type=int, default=8095)+ ap.add_argument("--keep", action="store_true")+ ap.add_argument("--all", action="store_true")+ ap.add_argument("--cancel", action="store_true")+ ap.add_argument("--approval", action="store_true")+ args = ap.parse_args()++ selected = []+ if args.all:+ selected = ["completion", "cancel", "approval"]+ else:+ if args.cancel:+ selected.append("cancel")+ if args.approval:+ selected.append("approval")+ if not selected:+ selected = ["completion"]++ dsh, python = Path(args.dsh), Path(args.python)+ for path, what in ((dsh, "dsh CLI"), (python, "python interpreter"), (PROFILE_PATCH, "profile")):+ if not path.exists():+ print(f"prerequisite missing: {what} not at {path}", file=sys.stderr)+ return 2++ print("strata <-> harness bridge tests")+ print(f" cases : {', '.join(selected)}")+ print()+ failures = []+ for name in selected:+ print(f"[{name}]")+ try:+ with Fixture(dsh, python, args.worker_port, args.keep) as fx:+ CASES[name](fx)+ print(f" -> PASS\n")+ except Exception as e: # noqa: BLE001 - a test reports, it does not crash+ print(f" -> FAIL: {e}\n")+ failures.append((name, str(e)))++ if failures:+ print("FAIL")+ for name, err in failures:+ print(f" {name}: {err}")+ return 1+ print("PASS — all selected bridge cases passed")+ return 0+++if __name__ == "__main__":+ sys.exit(main())diff --git a/integrations/harness/profile/cordis.patch.yml b/integrations/harness/profile/cordis.patch.ymlnew file mode 100644index 000000000..30f26fb40--- /dev/null+++ b/integrations/harness/profile/cordis.patch.yml@@ -0,0 +1,66 @@+# The Strata profile: DeepSeek Harness as the agent backend for a *local* model.+#+# This is a patch layer over the shipped base + web-app bundles (see package.json). It does two+# things and nothing else:+#+# 1. points the agent's model at `strata-local`, the Strata API worker on loopback;+# 2. declares that route to the pi-ai adapter as an ordinary OpenAI-completions endpoint with+# explicit capabilities.+#+# It deliberately does NOT inherit the shipped web profile's `deepseek-official` default. Local use+# must not require a DeepSeek account and must never silently fall back to a cloud model, so the+# default model here is the local one and no cloud route is declared.+- id: agent-default-model+ name: "@deepseek-ai/dsh-agent-default-model"+ config:+ provider: strata-local+ model: qwen3.8-flash-next++- id: llm-pi-ai+ name: "@deepseek-ai/dsh-llm-pi-ai"+ # NOTE: the schema is `{ providers: { "<route>": <profile> } }` — a dict *keyed by route* under+ # `providers`, not route keys at the top level. A top-level route key is silently ignored and the+ # adapter then never registers, which surfaces much later as+ # `no adapter registered for provider "<route>"`.+ config:+ providers:+ strata-local:+ displayName: Strata (local)+ # An OpenAI-compatible dialect. `supportedProtocols()` in llm-pi-ai is+ # openai-completions | openai-responses | anthropic-messages; the Strata worker implements+ # the documented subsets of all three, and chat completions is the one exercised here.+ api: openai-completions+ baseURL: http://127.0.0.1:8095/v1+ # The Strata worker does not require a key on loopback, but pi-ai's OpenAI-compatible+ # client insists on one before it will send a request at all. Two things were tried and+ # both fail before reaching the worker, which is worth recording:+ # * `apiKeyEnv: <NAME>` with the variable unset -> `no credential for provider route`+ # * naming no credential at all -> `No API key for provider` (PI_AI_ERROR)+ # A literal placeholder satisfies the client. It is NOT a secret, must never be treated as+ # one, and is scoped to loopback by baseURL. If the worker is started with `--api-key`,+ # this must become an `apiKeyEnv` credential reference instead.+ headers:+ Authorization: "Bearer strata-local"+ compat:+ # The worker's Chat Completions implementation is plain OpenAI; disable the provider+ # conveniences pi-ai would otherwise assume.+ supportsStore: false+ models:+ - id: qwen3.8-flash-next+ name: Qwen3.8-Flash-Next (local)+ # These are the *actual* limits the selected local profile reports, not catalog+ # defaults. Setup recommends 32K below 96 GB unified memory and measures to 32K; see+ # docs/MAC.md. Over-claiming here would let the agent build a request the engine+ # refuses mid-turn.+ contextWindow: 32768+ # Sized against the *whole* request, not just the visible prompt: the Harness system+ # prompt plus its tool definitions is already ~29K tokens of the 32K window, so an 8K+ # output budget makes every turn fail with the worker's+ # "request exceeds the context window ... requests are never truncated".+ # The worker will not silently shrink a request, which is correct; the route must+ # declare a budget that fits. The real-machine profile should either raise the engine's+ # context (docs/MAC.md measures to 32K, and the model supports more) or set the engine+ # config's `fit_max_tokens` so the server trims with a label rather than refusing.+ maxTokens: 2048+ input:+ - textdiff --git a/integrations/harness/profile/package.json b/integrations/harness/profile/package.jsonnew file mode 100644index 000000000..cc2735051--- /dev/null+++ b/integrations/harness/profile/package.json@@ -0,0 +1,13 @@+{+ "name": "dsh-profile-strata",+ "private": true,+ "dependencies": {},+ "dsh": {+ "profile": {+ "bundles": [+ "@deepseek-ai/dsh-base",+ "@deepseek-ai/dsh-web-app"+ ]+ }+ }+}diff --git a/integrations/harness/profile/scripted-worker.cordis.patch.yml b/integrations/harness/profile/scripted-worker.cordis.patch.ymlnew file mode 100644index 000000000..50271221f--- /dev/null+++ b/integrations/harness/profile/scripted-worker.cordis.patch.yml@@ -0,0 +1,20 @@+# A TEST-ONLY overlay for the scripted Strata worker, layered over the product profile+# (`cordis.patch.yml`) by `tool_flow_e2e.py --patch` order. It is not part of the shipped profile and+# nothing but that test applies it: the product profile must keep answering title requests.+#+# It exists because a *scripted* model is not a model. The worker replays a fixed list of scripts, one+# per request, so which reply a request receives depends on how many requests came before it -- and a+# sequence written by hand is only meaningful if the Harness sends exactly the requests the script+# author planned for.+#+# The session-title provider does not: on the first prompt it fires its own chat-completions request,+# before the turn's model call. The worker answers that request with the first script, so a+# two-script sequence meaning "call the bash tool, then report" is consumed as "title, report": the+# tool call is never emitted, the turn completes normally, and the run looks exactly like a model that+# chose not to use a tool. That is a silent, plausible-looking failure -- the worst kind -- and it is+# why this overlay disables the provider rather than adding a script.+#+# The deterministic first-prompt fallback title (`session-title`) stays enabled, so sessions are still+# named from their first message; only the extra model request is gone.+- id: session-title-llm+ disabled: truediff --git a/integrations/harness/revise_once.py b/integrations/harness/revise_once.pynew file mode 100644index 000000000..c228f8b0c--- /dev/null+++ b/integrations/harness/revise_once.py@@ -0,0 +1,191 @@+#!/usr/bin/env python3+"""One bounded Harness revision attempt, for Time-skip's `--revise-command`.++Time-skip owns the job: it checkpoints, isolates a candidate directory, and decides from checks.+This script owns only the *revision step* — it asks the pinned DeepSeek Harness for one bounded+agent turn inside the candidate directory and returns when the turn reaches a terminal state.++```+strata timeskip run --workspace DIR --prompt "..." \+ --revise-command "python integrations/harness/revise_once.py"+```++The contract Time-skip depends on:++* the process runs with the **candidate directory** as its cwd, and only ever writes there;+* the prompt arrives in `STRATA_TIMESKIP_PROMPT` and the attempt number in `STRATA_TIMESKIP_ATTEMPT`;+* exit 0 means "an attempt was made" — *not* "the attempt succeeded". Whether it improved anything is+ decided by the checks, never by this script. A non-zero exit reports that the attempt itself could+ not be made (for example the Harness is not installed), which Time-skip records as a failed run+ rather than a rejected candidate.++A bounded turn means all of: a wall-clock deadline, a per-attempt session, and a Harness that is+stopped when the deadline passes — so a stuck agent cannot hold a Time-skip run open forever.+"""++from __future__ import annotations++import argparse+import json+import os+import re+import shutil+import signal+import subprocess+import sys+import tempfile+import time+from pathlib import Path++HERE = Path(__file__).resolve().parent+sys.path.insert(0, str(HERE))++from _bridge import ( # noqa: E402+ DEFAULT_DSH,+ PROFILE_PATCH,+ BridgeError,+ gateway_call,+ http_json,+ read_session_events,+ wait_for_agent_url,+)++#: The whole attempt's wall-clock bound. Time-skip's own phases are unbounded by design; the bound+#: belongs to the thing that can run away, which is the agent turn.+DEFAULT_DEADLINE_S = 240.0++INSTRUCTIONS = """You are revising a copy of a user's work. Follow these rules exactly.++1. Work only inside the current directory. It is an isolated copy: the user's original is safe and+ you cannot see it. Never try to read or write outside this directory.+2. Make the smallest change that achieves the goal. Do not reorganise, rename or reformat anything+ that is not needed for the goal.+3. Do not add commentary files, notes, README files or explanations of what you did. Change the+ work itself.+4. If the goal cannot be achieved from what is here, change nothing and say so in one sentence.++When you are finished, reply with a single short sentence describing what you changed."""+++def main() -> int:+ ap = argparse.ArgumentParser(description=__doc__,+ formatter_class=argparse.RawDescriptionHelpFormatter)+ ap.add_argument("--dsh", default=os.environ.get("DSH_CLI", DEFAULT_DSH))+ ap.add_argument("--workspace", default=None,+ help="the candidate directory (default: the current directory)")+ ap.add_argument("--deadline", type=float,+ default=float(os.environ.get("STRATA_TIMESKIP_DEADLINE_S", DEFAULT_DEADLINE_S)))+ ap.add_argument("--keep", action="store_true",+ help="keep the per-attempt Harness home for inspection")+ ap.add_argument("--print-session", action="store_true",+ help="print the session id and its event types, for diagnosis")+ args = ap.parse_args()++ candidate = Path(args.workspace or os.getcwd()).resolve()+ prompt = os.environ.get("STRATA_TIMESKIP_PROMPT", "").strip()+ attempt = os.environ.get("STRATA_TIMESKIP_ATTEMPT", "1")+ if not prompt:+ print("revise_once: STRATA_TIMESKIP_PROMPT is empty; nothing to ask for", file=sys.stderr)+ return 2+ if not candidate.is_dir():+ print(f"revise_once: {candidate} is not a directory", file=sys.stderr)+ return 2++ dsh = Path(args.dsh)+ if not dsh.exists():+ print(f"revise_once: the Harness CLI is not at {dsh}; set DSH_CLI or --dsh", file=sys.stderr)+ return 3+ if not PROFILE_PATCH.exists():+ print(f"revise_once: the Strata profile is not at {PROFILE_PATCH}", file=sys.stderr)+ return 3++ # Each attempt gets its own Harness home. That keeps sessions and approvals scoped to the+ # attempt, and means a crashed attempt cannot corrupt the next one's state.+ home = Path(tempfile.mkdtemp(prefix=f"strata-revise-a{attempt}-"))+ (home / "sessions").mkdir(parents=True, exist_ok=True)+ agent = None+ deadline = time.time() + args.deadline+ try:+ env = dict(os.environ)+ env["DSH_HOME"] = str(home)+ log = (home / "dsh.log").open("w")+ agent = subprocess.Popen(+ [str(dsh), "--profile", "web", "--patch", str(PROFILE_PATCH), "--port", "0", "--no-open"],+ cwd=str(candidate), env=env, stdout=log, stderr=subprocess.STDOUT)+ text = wait_for_agent_url(home / "dsh.log", agent, timeout=min(120.0, args.deadline))+ m = re.search(r"http://127\.0\.0\.1:(\d+)/\?token=([A-Za-z0-9_-]+)", text)+ if not m:+ raise BridgeError("the Harness printed no usable URL")+ port, token = int(m.group(1)), m.group(2)++ # `interactions=answer` is required, or every approval request fails closed as+ # 'unavailable'. A revision attempt that needs to run a command must be able to be approved.+ _, _, set_cookie = http_json("GET", "127.0.0.1", port,+ f"/?token={token}&interactions=answer")+ cookie = set_cookie.split(";", 1)[0]+ if not cookie.startswith("dsh-auth-"):+ raise BridgeError("no operator cookie was issued")++ created = gateway_call("127.0.0.1", port, cookie, "c1", "session/create",+ {"request": {"cwd": str(candidate)}})+ session_id = created["sessionId"]+ gateway_call("127.0.0.1", port, cookie, "p1", "session/prompt",+ {"request": {"requestId": f"timeskip-{attempt}", "sessionId": session_id,+ "mode": "queue",+ "content": [{"type": "text",+ "text": f"{INSTRUCTIONS}\n\nGoal:\n{prompt}"}]}})++ # Wait for a terminal state, or the deadline, whichever comes first.+ events: list[dict] = []+ note = ""+ while time.time() < deadline:+ events = read_session_events(session_id, home=home)+ end = next((e for e in events if e.get("type") == "turn/end"), None)+ if end is not None:+ reason = (end.get("data") or {}).get("reason") or {}+ if reason.get("kind") != "completed":+ print(f"revise_once: the turn ended as {json.dumps(reason)[:200]}", file=sys.stderr)+ return 1+ assistant = next((e for e in events if e.get("type") == "assistant/message"), None)+ if assistant:+ message = (assistant.get("data") or {}).get("message") or {}+ parts = message.get("content") or []+ note = " ".join(p.get("text", "") for p in parts+ if isinstance(p, dict) and p.get("type") in ("text", "reasoning"))+ break+ time.sleep(0.5)+ else:+ print(f"revise_once: the attempt did not finish within {args.deadline:.0f}s; "+ "stopping the Harness and reporting a made attempt", file=sys.stderr)+ return 1++ if args.print_session:+ print(f"session {session_id}: {[e.get('type') for e in events]}", file=sys.stderr)+ # Exit 0 means an attempt was made. What it achieved is the checks' business.+ print((note or f"attempt {attempt} completed").strip()[:500])+ return 0++ except BridgeError as e:+ print(f"revise_once: {e}", file=sys.stderr)+ return 1+ except Exception as e: # noqa: BLE001 - report, and let Time-skip record a failed run+ print(f"revise_once: {type(e).__name__}: {e}", file=sys.stderr)+ return 1+ finally:+ if agent is not None and agent.poll() is None:+ try:+ agent.send_signal(signal.SIGTERM)+ agent.wait(timeout=8)+ except Exception:+ try:+ agent.kill()+ except Exception:+ pass+ if not args.keep:+ shutil.rmtree(home, ignore_errors=True)+ else:+ print(f"revise_once: kept {home}", file=sys.stderr)+++if __name__ == "__main__":+ sys.exit(main())diff --git a/integrations/harness/tool_flow_e2e.py b/integrations/harness/tool_flow_e2e.pynew file mode 100644index 000000000..d83e88939--- /dev/null+++ b/integrations/harness/tool_flow_e2e.py@@ -0,0 +1,802 @@+#!/usr/bin/env python3+"""Tool execution, approval and cancellation, end to end (checklist A03, A04, A05).++This drives the real path — a scripted Strata worker, the Harness agent loop with its real bash tool,+and a durable session — so what it proves is the *integration*, not a mock of it.++The model is scripted. That is a deliberate and limited choice: writing a file via bash is+deterministic, and a scripted tool call exercises exactly the same code as a model-chosen one —+the worker's parser, the Harness tool executor, the approval gate and the durable record. What it+does **not** prove is that a real model chooses tools well, and this script never claims that.++Three flows, each with one assertion that matters:++**A03 — the tool runs once, in the Harness.**+ The scripted turn calls bash to append a line to a file. The assertions are that the file has+ exactly one line (so the tool ran exactly once), that the durable session records a tool call and+ a tool result, and that the Strata worker was given the transcript rather than executing anything+ itself — the worker has no tool-execution path at all, and the call count is the evidence. The+ call escalates the sandbox, so it cannot run until a peer *grants* it: the flow connects that peer+ and asserts an approval was asked, answered, and recorded as the grant.++**A04 — the denial test: a tool that could escalate without an answerer would be the whole failure.**+ The same scripted call and the same peer connection; the only difference is the decision. The peer+ delegates (`next`) instead of granting, so the composed answerer chain has no terminal answerer+ and the request resolves the fail-closed `unavailable`. The assertions are that the escalation+ reached the gate, that its recorded outcome is exactly `unavailable`, that the turn settled on+ that denial, and that the file does not exist afterwards. Connecting the peer is what makes the+ outcome observable — with no peer at all the gateway keeps a forwarded waterfall pending until a+ client connects, so the request would be neither granted nor decided.++**A05 — Stop settles the turn.**+ A prompt is sent and cancelled while the turn is live. The assertions are that the turn reaches a+ terminal state without the tool having run, and that cancellation is acknowledged only as+ admission (`accepted`), with the durable turn state being what actually settles it.++Run: python integrations/harness/tool_flow_e2e.py --dsh <checkout> [--keep]+"""++from __future__ import annotations++import argparse+import base64+import json+import os+import re+import shutil+import signal+import socket+import struct+import subprocess+import sys+import tempfile+import threading+import time+import http.client+from pathlib import Path++HERE = Path(__file__).resolve().parent+STRATA = HERE.parents[1]+PROFILE_PATCH = HERE / "profile" / "cordis.patch.yml"+#: A TEST-ONLY overlay, applied after the product patch. It disables the session-title model+#: request, which a scripted worker cannot absorb: the worker replays one script per request, so+#: an extra request shifts every later reply and the run looks like a model that chose not to+#: call a tool. The product profile must keep answering title requests, so the row lives here.+SCRIPTED_OVERLAY = HERE / "profile" / "scripted-worker.cordis.patch.yml"+PYTHON = os.environ.get("STRATA_PYTHON", sys.executable)++#: The bash call the scripted worker emits. It appends exactly one line, so counting the lines counts+#: the executions -- a replayed tool would leave two.+# The `</think>` is not decoration: without it the server treats the *whole* reply as reasoning, so+# the tool-call block lands in the reasoning field and is never executed. The default mock script ends+# its thinking the same way, which is where the convention comes from. The failure is silent -- the+# turn completes normally with the tool call sitting in the wrong field.+#+# Every parameter is load-bearing:+# * `command` is the effect the assertions count;+# * `description` is REQUIRED by this runtime's bash schema -- a call without it is rejected by+# argument validation ("invalid arguments: missing required property \"description\"") before the+# tool body runs, which looks exactly like a tool that never executed;+# * `sandbox_permissions` + `justification` are what make the call ask for approval at all. The+# approval policy is `ask` and the sandbox is `workspace-write`, but a command the sandbox would+# allow runs *without* an approval prompt: only a strictly wider sandbox mode is decided by+# `ctx.approval`. So a call that never escalates cannot prove anything about the approval gate --+# it runs with no answerer present, which is how A04's denial test can pass while testing nothing.+TOOL_CALL = (+ "I will write the marker file.</think>\n\n"+ "<tool_call>\n<function=bash>\n"+ "<parameter=command>\n"+ "echo run >> {marker}\n"+ "</parameter>\n"+ "<parameter=description>\n"+ "Append one line to the marker file\n"+ "</parameter>\n"+ "<parameter=sandbox_permissions>\n"+ "danger-full-access\n"+ "</parameter>\n"+ "<parameter=justification>\n"+ "The marker write must not be refused by the file sandbox.\n"+ "</parameter>\n"+ "</function>\n</tool_call>"+)+FINAL_ANSWER = "The marker file has been written."++#: The worker is started with `--script-cycle`, so the script is chosen by the request's position+#: rather than by a counter that every extra request advances. That matters because the Harness makes+#: requests the script author did not plan for -- a session title, and a re-ask after a tool result --+#: and with the default behaviour one of those shifts every later reply. The symptom is a run that+#: looks like a model which simply chose not to use a tool.+++class FlowError(RuntimeError):+ pass+++def log(msg: str) -> None:+ print(f" {msg}", flush=True)+++def http_json(method: str, host: str, port: int, path: str, body=None, cookie: str | None = None,+ timeout: float = 30.0):+ """A request that does not follow redirects, because the redirect carries the credential.++ `urllib` follows the agent's 303 and discards the `set-cookie` on it, which surfaces as a 401 on+ the next request and looks like an authentication bug rather than a client one. `http.client`+ returns the response as sent, which is what this needs.+ """+ connection = http.client.HTTPConnection(host, port, timeout=timeout)+ headers = {}+ data = None+ if body is not None:+ data = json.dumps(body).encode()+ headers["content-type"] = "application/json"+ if cookie:+ headers["cookie"] = cookie+ try:+ connection.request(method, path, body=data, headers=headers)+ response = connection.getresponse()+ raw = response.read().decode("utf-8", "replace")+ return response.status, raw, response.getheader("set-cookie") or ""+ finally:+ connection.close()+++def rpc(host: str, port: int, cookie: str, rpc_id: str, method: str, args: dict):+ """One gateway call.++ The envelope is the gateway's own: `{"type": "client-request", "rpcId", "method", "payload":+ {"args"}}`. Posting `args` directly is answered with `invalid client-request message` and a list+ of schema issues, which reads like a malformed session rather than a malformed envelope.+ """+ body = {"type": "client-request", "rpcId": rpc_id, "method": method, "payload": {"args": args}}+ status, raw, _ = http_json("POST", host, port, f"/api/{method}", body, cookie=cookie)+ if status != 200:+ raise FlowError(f"{method} returned HTTP {status}: {raw[:300]}")+ payload = json.loads(raw)+ result = payload.get("result") or {}+ if not result.get("ok"):+ raise FlowError(f"{method} was refused: {json.dumps(result.get('error'))[:400]}")+ return result.get("value")+++#: The gateway's event stream, and the HTTP endpoint that answers one forwarded waterfall. Both are+#: gateway-internal endpoint names, not HTTP routes: the stream is a logical stream carried by the+#: WebSocket mux, and only the answer travels as an ordinary `/api/<endpoint>` call.+EVENT_STREAM_ENDPOINT = "$events"+EVENT_RESULT_ENDPOINT = "$events/result"+REMOTE_STREAM_MUX_PATH = "/api/remote.mux"+++class ApprovalAnswerer:+ """A real peer, because an approval cannot be answered without one.++ `approval/request` is not an RPC that a client can simply call: it is a Cordis *waterfall* the+ gateway forwards to connected peers, and it is delivered over the gateway's event stream. A peer+ that wants to decide one must receive the frame (a WebSocket on `/api/remote.mux`, opened as the+ `$events` logical stream) and then send its outcome back (`POST /api/$events/result`).++ `decision` is the outcome this peer returns for every request:+ * `'allowed-once'` — the grant A03 needs;+ * `'next'` — do not decide, delegate to the listeners behind this peer. It is what an+ answerer that is *present but not available* does, so the request resolves `unavailable` and+ the action fails closed, which is the outcome A04 asserts.++ A peer is also the only thing that can make an approval observable at all: with no client holding+ the stream open the gateway keeps the forwarded waterfall pending until one connects (it has a+ test named "delivers a pending waterfall to the first Client that connects"), so the request is+ never decided and nothing is `unavailable` -- the turn simply waits. That is why A04 connects a+ peer and delegates instead of asserting on an answerer that does not exist.++ No WebSocket client is available in the standard library, so the handshake and the four frame+ types this needs (text, ping, pong, close) are implemented here. Only frames the Host sends are+ parsed; the one frame this peer sends is the stream `open`.+ """++ def __init__(self, host: str, port: int, cookie: str, decision: str = "allowed-once"):+ self.host, self.port, self.cookie, self.decision = host, port, cookie, decision+ self.requests: list[dict] = [] # every approval request this peer was asked to decide+ self.outcomes: list[str] = [] # the decision actually sent for each of them+ self.error: str | None = None+ self.ready = threading.Event() # set once the Host proved the event stream is live+ self.client_id: str | None = None+ self._sock: socket.socket | None = None+ self._buffer = b""+ self._thread: threading.Thread | None = None++ # -- lifecycle ---------------------------------------------------------------------------------++ def start(self, timeout: float = 30.0) -> "ApprovalAnswerer":+ """Connect and wait until the event stream is live.++ The wait is the point: an approval raised before the stream exists is delivered to nobody and+ fails closed, so a peer that connects late turns "no answerer" into a tool that never ran.+ """+ self._thread = threading.Thread(target=self._run, name="approval-answerer", daemon=True)+ self._thread.start()+ if not self.ready.wait(timeout):+ self.close()+ raise FlowError(f"the approval answerer never opened the event stream: {self.error or 'no reply'}")+ return self++ def close(self) -> None:+ sock = self._sock+ if sock is None:+ return+ try:+ self._send_frame(0x8, b"") # close+ except Exception: # noqa: BLE001 - the peer is going away either way+ pass+ self._sock = None+ try:+ sock.shutdown(socket.SHUT_RDWR)+ except OSError:+ pass+ sock.close()++ # -- WebSocket carrier -------------------------------------------------------------------------++ def _connect(self) -> None:+ sock = socket.create_connection((self.host, self.port), timeout=15)+ sock.settimeout(30)+ key = base64.b64encode(os.urandom(16)).decode()+ handshake = (+ f"GET {REMOTE_STREAM_MUX_PATH} HTTP/1.1\r\n"+ f"Host: {self.host}:{self.port}\r\n"+ "Upgrade: websocket\r\n"+ "Connection: Upgrade\r\n"+ f"Sec-WebSocket-Key: {key}\r\n"+ "Sec-WebSocket-Version: 13\r\n"+ # The operator credential from the launch URL: the same cookie the RPC calls use, and+ # the only thing that makes this a peer rather than an anonymous socket.+ f"Cookie: {self.cookie}\r\n\r\n"+ )+ sock.sendall(handshake.encode())+ head = b""+ while b"\r\n\r\n" not in head:+ chunk = sock.recv(4096)+ if not chunk:+ raise FlowError("the event stream socket closed during the WebSocket handshake")+ head += chunk+ header, self._buffer = head.split(b"\r\n\r\n", 1)+ status = header.split(b"\r\n", 1)[0].decode("latin-1")+ if " 101 " not in status:+ raise FlowError(f"the event stream upgrade was refused: {status!r}")+ # The stream is idle between events, so a read deadline would end the peer during a quiet+ # stretch and silently turn A03 back into A04. Blocking reads are released by close().+ sock.settimeout(None)+ self._sock = sock++ def _read_exactly(self, count: int) -> bytes:+ while len(self._buffer) < count:+ chunk = self._sock.recv(65536)+ if not chunk:+ raise FlowError("the event stream closed")+ self._buffer += chunk+ taken, self._buffer = self._buffer[:count], self._buffer[count:]+ return taken++ def _send_frame(self, opcode: int, payload: bytes) -> None:+ """One client frame. Client frames must be masked (RFC 6455), and the Host rejects one that is not."""+ mask = os.urandom(4)+ length = len(payload)+ if length < 126:+ header = bytes([0x80 | opcode, 0x80 | length])+ elif length < (1 << 16):+ header = bytes([0x80 | opcode, 0x80 | 126]) + struct.pack("!H", length)+ else:+ header = bytes([0x80 | opcode, 0x80 | 127]) + struct.pack("!Q", length)+ masked = bytes(byte ^ mask[index % 4] for index, byte in enumerate(payload))+ self._sock.sendall(header + mask + masked)++ def _send_text(self, text: str) -> None:+ self._send_frame(0x1, text.encode())++ def _read_frame(self) -> tuple[int, bytes]:+ first, second = self._read_exactly(2)+ opcode, length = first & 0x0F, second & 0x7F+ if length == 126:+ length = struct.unpack("!H", self._read_exactly(2))[0]+ elif length == 127:+ length = struct.unpack("!Q", self._read_exactly(8))[0]+ masked = bool(second & 0x80)+ mask = self._read_exactly(4) if masked else b""+ payload = self._read_exactly(length)+ if masked:+ payload = bytes(byte ^ mask[index % 4] for index, byte in enumerate(payload))+ return opcode, payload++ # -- protocol ----------------------------------------------------------------------------------++ def _run(self) -> None:+ try:+ self._connect()+ stream_id = "approvals"+ self._send_text(json.dumps({+ "type": "open", "streamId": stream_id,+ "endpoint": EVENT_STREAM_ENDPOINT,+ # The endpoint's own envelope: exactly one `args` field, empty for this stream.+ "payload": {"args": {}},+ }))+ while True:+ opcode, payload = self._read_frame()+ if opcode == 0x8: # close+ return+ if opcode == 0x9: # ping: the Host's liveness probe+ self._send_frame(0xA, payload)+ continue+ if opcode != 0x1: # pong and anything else: ignore+ continue+ self._handle(json.loads(payload.decode("utf-8", "replace")))+ except Exception as error: # noqa: BLE001 - reported, never raised here+ self.error = f"{type(error).__name__}: {error}"+ finally:+ self.ready.set() # a failed start must not hang start()++ def _handle(self, message: dict) -> None:+ if message.get("type") != "item":+ return+ value = message.get("value")+ if not isinstance(value, dict):+ return+ if value.get("type") == "ready":+ self.client_id = value.get("clientId")+ self.ready.set()+ return+ if value.get("type") != "waterfall" or value.get("event") != "approval/request":+ return # emitted notifications are not questions+ self._answer(value)++ def _answer(self, frame: dict) -> None:+ self.requests.append(frame.get("request") or {})+ # `next` is the DSH word for "this answerer does not decide": the waterfall continues to the+ # listeners behind this peer, and with no terminal answerer composed it settles on the+ # approval service's fail-closed default, `unavailable`. It is not a rejection -- `rejected`+ # would be a human saying no, which is a different outcome and a different test.+ outcome = ({"kind": "next"} if self.decision == "next"+ else {"kind": "result", "value": self.decision})+ body = {+ "type": "client-request", "rpcId": f"approval-{len(self.requests)}",+ "method": EVENT_RESULT_ENDPOINT,+ "payload": {"args": {+ "clientId": self.client_id, "eventId": frame.get("eventId"),+ "outcome": outcome,+ }},+ }+ status, raw, _ = http_json("POST", self.host, self.port, f"/api/{EVENT_RESULT_ENDPOINT}",+ body, cookie=self.cookie)+ if status != 200:+ raise FlowError(f"answering the approval returned HTTP {status}: {raw[:200]}")+ answer = (json.loads(raw).get("result") or {})+ if not answer.get("ok"):+ raise FlowError(f"the approval answer was refused: {json.dumps(answer.get('error'))[:300]}")+ self.outcomes.append(self.decision)+++def wait_for_worker(port: int, timeout: float = 60.0) -> None:+ deadline = time.time() + timeout+ while time.time() < deadline:+ try:+ status, _, _ = http_json("GET", "127.0.0.1", port, "/health", timeout=2)+ if status == 200:+ return+ except (OSError, http.client.HTTPException):+ pass+ time.sleep(0.3)+ raise FlowError(f"the worker on {port} never became healthy")+++def write_profile(port: int, tmp: Path) -> Path:+ """The Strata profile patch with this run's worker port.++ The shipped patch names 8095, and a worker left over from an earlier session on that port will+ happily answer every request -- producing a green run against the *wrong* worker, which is+ exactly how a stale process hid a failure here once already. Substituting the port makes the+ agent and the worker this test started the only pair that can talk.+ """+ text = PROFILE_PATCH.read_text(encoding="utf-8")+ if "http://127.0.0.1:8095/v1" not in text:+ raise FlowError(f"{PROFILE_PATCH} no longer names http://127.0.0.1:8095/v1; the substitution "+ "below would silently do nothing")+ patched = tmp / "cordis.patch.yml"+ patched.write_text(text.replace("http://127.0.0.1:8095/v1", f"http://127.0.0.1:{port}/v1"),+ encoding="utf-8")+ return patched+++def start_worker(scripts: list[str], port: int, log_path: Path,+ delay_s: float = 0.0) -> subprocess.Popen:+ if worker_port_in_use(port):+ raise FlowError(+ f"port {port} is already in use; a worker left over from an earlier session would answer "+ "this test's requests and its results would look plausible")+ argv = [PYTHON, "-m", "serve.server", "--engine", "mock", "--port", str(port), "--script-cycle"]+ if delay_s:+ # A turn that finishes before Stop can arrive proves nothing about Stop. The mock honours+ # cancellation between tokens, so a delay makes the window real.+ argv += ["--script-delay", str(delay_s)]+ for script in scripts:+ argv += ["--script", script]+ handle = log_path.open("wb")+ return subprocess.Popen(argv, cwd=str(STRATA), stdout=handle, stderr=subprocess.STDOUT)+++def worker_port_in_use(port: int) -> bool:+ import socket+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as probe:+ probe.settimeout(0.5)+ return probe.connect_ex(("127.0.0.1", port)) == 0+++def start_agent(dsh: Path, port: int, home: Path, profile: str, log_path: Path,+ cwd: Path, patch: Path) -> subprocess.Popen:+ """Boot the agent with the Strata profile.++ The profile is selected the way the working bridge does it: `--profile <base> --patch <file>`, a+ patch layer over a shipped bundle. `dsh web --profile` does not exist, and passing it produces+ `unknown option '--profile'` before anything starts.+ """+ env = dict(os.environ)+ env["DSH_HOME"] = str(home)+ env.pop("ELECTRON_RUN_AS_NODE", None)+ argv = [str(dsh), "--profile", profile, "--patch", str(patch),+ "--patch", str(SCRIPTED_OVERLAY),+ "--port", str(port), "--no-open"]+ handle = log_path.open("wb")+ return subprocess.Popen(argv, env=env, cwd=str(cwd), stdout=handle, stderr=subprocess.STDOUT)+++def wait_for_agent(log_path: Path, proc: subprocess.Popen, timeout: float = 150.0) -> tuple[int, str]:+ deadline = time.time() + timeout+ while time.time() < deadline:+ if proc.poll() is not None:+ raise FlowError(f"the agent exited with code {proc.returncode}:\n"+ f"{log_path.read_text(errors='replace')[-800:]}")+ text = log_path.read_text(errors="replace") if log_path.exists() else ""+ match = re.search(r"http://127\.0\.0\.1:(\d+)/\?token=([A-Za-z0-9_-]+)", text)+ if match:+ return int(match.group(1)), match.group(2)+ time.sleep(0.5)+ raise FlowError(f"the agent never printed a URL:\n{log_path.read_text(errors='replace')[-800:]}")+++def read_events(session_id: str, home: Path) -> list[dict]:+ root = home / "sessions"+ matches = list(root.glob(f"*/{session_id}/session.v*.jsonl*"))+ if not matches:+ return []+ newest = max(matches, key=lambda p: p.stat().st_mtime)+ try:+ out = subprocess.run(["zstd", "-d", "-c", "--long=27", str(newest)],+ capture_output=True, timeout=30)+ text = out.stdout.decode("utf-8", "replace")+ except (OSError, subprocess.SubprocessError):+ return []+ events = []+ for line in text.splitlines():+ line = line.strip()+ if not line:+ continue+ try:+ events.append(json.loads(line))+ except json.JSONDecodeError:+ continue+ return events+++def wait_for_turn(session_id: str, home: Path, timeout: float = 180.0) -> list[dict]:+ deadline = time.time() + timeout+ while time.time() < deadline:+ events = read_events(session_id, home)+ if any(e.get("type") == "turn/end" for e in events):+ return events+ time.sleep(1.0)+ return read_events(session_id, home)+++def summarise(events: list[dict]) -> dict:+ kinds: dict[str, int] = {}+ for event in events:+ kinds[event.get("type", "?")] = kinds.get(event.get("type", "?"), 0) + 1+ turn_end = next((e for e in events if e.get("type") == "turn/end"), None)+ reason = ((turn_end or {}).get("data") or {}).get("reason") or {}+ return {"kinds": kinds, "end": reason.get("kind"), "reason": reason}+++class Agent:+ """One agent process with a session, so each flow gets a clean session."""++ def __init__(self, dsh: Path, port: int, home: Path, profile: str, answerer: bool, tmp: Path,+ cwd: Path, worker_port: int):+ self.home, self.port, self.tmp, self.cwd = home, port, tmp, cwd+ cwd.mkdir(parents=True, exist_ok=True)+ # A profile naming this run's worker port, so no other process can answer for it.+ self.patch = write_profile(worker_port, tmp)+ self.log_path = tmp / f"agent-{port}.log"+ self.proc = start_agent(dsh, port, home, profile, self.log_path, self.cwd, self.patch)+ self.agent_port, token = wait_for_agent(self.log_path, self.proc)+ query = f"/?token={token}" + ("&interactions=answer" if answerer else "")+ _, _, cookie = http_json("GET", "127.0.0.1", self.agent_port, query)+ self.cookie = cookie.split(";", 1)[0]+ if not self.cookie.startswith("dsh-auth-"):+ raise FlowError(f"no operator cookie (got {cookie[:80]!r})")+ self.answerer = answerer+ self.peer: ApprovalAnswerer | None = None++ def answer_approvals(self, decision: str = "allowed-once") -> ApprovalAnswerer:+ """Connect the peer that decides approvals, and return it once its stream is live.++ This is what makes an approval answerable, and `interactions=answer` on the launch URL is+ not: that parameter is read only on `/connect`+ (`packages/client/connection/src/browser-auth.ts`), while `GET /?token=...` issues the+ browser cookie whose shared peer already permits the event stream+ (`packages/client/connection/src/peer.ts`: `options.interactions ?? 'answer'`). So+ `answerer=False` never removed an answerer; the `decision` a flow passes here is the real+ difference between A03 and A04.+ """+ peer = ApprovalAnswerer("127.0.0.1", self.agent_port, self.cookie, decision).start()+ self.peer = peer+ return peer++ def session(self, cwd: Path) -> str:+ result = rpc("127.0.0.1", self.agent_port, self.cookie, "c1", "session/create",+ {"request": {"cwd": str(cwd)}})+ return result["sessionId"]++ def prompt(self, session_id: str, text: str, request_id: str = "req-1") -> None:+ rpc("127.0.0.1", self.agent_port, self.cookie, f"p-{request_id}", "session/prompt",+ {"request": {"requestId": request_id, "sessionId": session_id, "mode": "queue",+ "content": [{"type": "text", "text": text}]}})++ def cancel(self, session_id: str) -> dict:+ return rpc("127.0.0.1", self.agent_port, self.cookie, "x1", "session/cancel",+ {"request": {"sessionId": session_id}})++ def close(self):+ # Teardown never raises and never skips a step: a failure closing the peer once left the agent+ # process alive on its port, and the next run died at startup with EADDRINUSE instead of+ # reporting the real problem. The peer is best-effort; killing the process is not.+ if self.peer is not None:+ try:+ self.peer.close()+ except Exception as error: # noqa: BLE001 - cleanup must not mask the run+ log(f"closing the approval peer failed: {type(error).__name__}: {error}")+ if self.proc.poll() is None:+ self.proc.terminate()+ try:+ self.proc.wait(timeout=20)+ except subprocess.TimeoutExpired:+ self.proc.kill()+++def flow_a03(dsh: Path, tmp: Path, profile: str, base_port: int, results: dict) -> None:+ print("[A03] a read/edit/test workflow: the tool runs once, in the Harness")+ ws = tmp / "ws-a03"+ ws.mkdir(parents=True, exist_ok=True)+ marker = ws / "marker.txt"+ worker = start_worker([TOOL_CALL.format(marker=marker), FINAL_ANSWER],+ base_port, tmp / "worker-a03.log")+ agent = None+ try:+ wait_for_worker(base_port)+ agent = Agent(dsh, base_port + 1, tmp / "home-a03", profile, answerer=True, tmp=tmp, cwd=ws, worker_port=base_port)+ # The scripted call escalates the sandbox, so the tool cannot run until a peer grants it. The+ # peer is connected (and its stream proved live) BEFORE the prompt, because an approval raised+ # while no peer holds the stream open fails closed and is gone for good.+ peer = agent.answer_approvals("allowed-once")+ session = agent.session(ws)+ agent.prompt(session, "Write the marker file using bash, then report that you did.")+ events = wait_for_turn(session, agent.home)+ summary = summarise(events)++ calls = [e for e in events if "tool" in str(e.get("type", ""))]+ asked = [e for e in events if e.get("type") == "approval/asked"]+ decided = [e for e in events if e.get("type") == "approval/decided"]+ executed = marker.exists()+ lines = marker.read_text().splitlines() if executed else []++ results["A03"] = {+ "turn_end": summary["end"],+ "tool_events": len(calls),+ "marker_lines": len(lines),+ "executions": len(lines),+ "approvals_asked": len(asked),+ "approvals_answered": len(peer.outcomes),+ "approval_outcomes": [((e.get("data") or {}).get("outcome")) for e in decided],+ }+ log(f"turn ended as {summary['end']!r}; {len(calls)} tool-related event(s)")+ log(f"the workspace marker has {len(lines)} line(s)")+ log(f"the peer answered {len(peer.outcomes)} of {len(asked)} approval(s): "+ f"{[((e.get('data') or {}).get('outcome')) for e in decided]}")++ if summary["end"] != "completed":+ raise FlowError(f"the turn did not complete: {json.dumps(summary)[:300]}")+ if not executed:+ raise FlowError("the bash tool did not run: no marker file was written")+ if len(lines) != 1:+ raise FlowError(f"the tool ran {len(lines)} times, not once: a completed tool effect was "+ "replayed")+ # The tool ran because the approval was granted, not because it was never asked: without this+ # the flow would still pass if the escalation stopped reaching the approval gate, which is the+ # state A04's denial test exists to rule out. The grant must also be the recorded outcome.+ if len(peer.outcomes) < 1 or not asked:+ raise FlowError("the tool ran without an answered approval: the call did not reach the "+ f"approval gate (asked={len(asked)}, answered={len(peer.outcomes)}, "+ f"peer_error={peer.error!r})")+ if [e for e in decided if (e.get("data") or {}).get("outcome") != "allowed-once"]:+ raise FlowError(f"the session recorded an approval outcome other than the granted one: "+ f"{[((e.get('data') or {}).get('outcome')) for e in decided]}")+ # The worker must have been given the transcript rather than executing the tool itself: it has+ # no tool-execution path, so a healthy worker is one that only ever answered.+ status, raw, _ = http_json("GET", "127.0.0.1", base_port, "/v1/status")+ activity = json.loads(raw).get("activity") or {}+ results["A03"]["worker_requests"] = activity.get("requests")+ log(f"the worker recorded {activity.get('requests')} request(s) and executed nothing itself")+ print(" PASS A03")+ finally:+ if agent:+ agent.close()+ worker.terminate()+ worker.wait(timeout=20)+++def flow_a04(dsh: Path, tmp: Path, profile: str, base_port: int, results: dict) -> None:+ print("[A04] with no answerer that decides, the approval fails closed and the tool does not run")+ ws = tmp / "ws-a04"+ ws.mkdir(parents=True, exist_ok=True)+ marker = ws / "must-not-exist.txt"+ worker = start_worker([TOOL_CALL.format(marker=marker), FINAL_ANSWER],+ base_port, tmp / "worker-a04.log")+ agent = None+ try:+ wait_for_worker(base_port)+ # `answerer=False` keeps `interactions=answer` off the launch URL. That flag is not what+ # decides this flow -- it is read only on `/connect`, so on `/?token=...` it does nothing, and+ # the peer below can open the event stream either way. What decides the flow is that no peer+ # *grants* the escalation: the one connected peer delegates (`next`), which leaves the+ # composed answerer chain without a terminal answerer and the request resolves the+ # fail-closed `unavailable`.+ agent = Agent(dsh, base_port + 1, tmp / "home-a04", profile, answerer=False, tmp=tmp, cwd=ws, worker_port=base_port)+ # The peer must exist for the outcome to be observable at all. With no peer holding the+ # event stream, the gateway keeps the forwarded waterfall pending until one connects -- the+ # request is then never decided, nothing is `unavailable`, and the turn simply waits, which is+ # a timeout, not a denial.+ peer = agent.answer_approvals("next")+ session = agent.session(ws)+ agent.prompt(session, "Write the marker file using bash.", request_id="a04")+ events = wait_for_turn(session, agent.home)+ summary = summarise(events)++ created = marker.exists()+ asked = [e for e in events if e.get("type") == "approval/asked"]+ outcomes = [((e.get("data") or {}).get("outcome")) for e in events+ if e.get("type") == "approval/decided"]+ results["A04"] = {"turn_end": summary["end"], "marker_created": created,+ "answerer": agent.answerer,+ "approvals_asked": len(asked), "approval_outcomes": outcomes,+ "grants": outcomes.count("allowed-once")}+ log(f"turn ended as {summary['end']!r}")+ log(f"the marker file exists: {created}")+ log(f"the escalation was asked {len(asked)} time(s) and decided {outcomes}")++ if created:+ raise FlowError("the tool ran even though no answerer granted the escalation: an "+ "unanswerable approval must fail closed")+ # The denial has to be observed, not assumed. Each check below fails the run for a different+ # way the flow could otherwise pass while proving nothing: the tool never reaching the gate,+ # the gate deciding to grant, or the turn never settling because the request was left pending.+ if not asked:+ raise FlowError("the bash call never reached the approval gate, so this run proves nothing "+ "about an unanswerable approval: no approval/asked was recorded")+ if not outcomes or any(outcome != "unavailable" for outcome in outcomes):+ raise FlowError(f"the unanswerable escalation did not fail closed as 'unavailable': "+ f"{outcomes}")+ if summary["end"] != "completed":+ raise FlowError(f"the turn did not settle after the denial (ended as {summary['end']!r}): a "+ "request left pending is a hang, not a closed decision")+ print(" PASS A04 (the escalation was decided 'unavailable' and the tool did not run)")+ finally:+ if agent:+ agent.close()+ worker.terminate()+ worker.wait(timeout=20)+++def flow_a05(dsh: Path, tmp: Path, profile: str, base_port: int, results: dict) -> None:+ print("[A05] Stop settles the turn without the tool having run")+ ws = tmp / "ws-a05"+ ws.mkdir(parents=True, exist_ok=True)+ marker = ws / "cancelled.txt"+ # 0.05 s per token: the tool call alone is ~40 tokens, so the turn is live for seconds.+ worker = start_worker([TOOL_CALL.format(marker=marker), FINAL_ANSWER],+ base_port, tmp / "worker-a05.log", delay_s=0.05)+ agent = None+ try:+ wait_for_worker(base_port)+ agent = Agent(dsh, base_port + 1, tmp / "home-a05", profile, answerer=True, tmp=tmp, cwd=ws, worker_port=base_port)+ session = agent.session(ws)+ agent.prompt(session, "Write the marker file using bash.", request_id="a05")++ # Wait until the turn is demonstrably live before sending Stop. Without this the scripted turn+ # finishes in milliseconds, Stop arrives after `turn/end`, and the run passes while proving+ # nothing -- which is how this flow passed on its first attempt.+ live = False+ deadline = time.time() + 60+ while time.time() < deadline:+ events = read_events(session, agent.home)+ if any(e.get("type") == "turn/start" for e in events):+ live = True+ break+ time.sleep(0.2)+ if not live:+ raise FlowError("the turn never started, so Stop could not be tested")++ acknowledgement = agent.cancel(session)+ events = wait_for_turn(session, agent.home, timeout=120)+ summary = summarise(events)+ results["A05"] = {"turn_end": summary["end"], "acknowledgement": acknowledgement,+ "marker_created": marker.exists(), "turn_was_live": live}+ log(f"cancel acknowledged as {json.dumps(acknowledgement)[:120]}")+ log(f"turn ended as {summary['end']!r}")+ if summary["end"] is None:+ raise FlowError("the turn never reached a terminal state after Stop")+ # `aborted` is the Harness's terminal reason for a turn a peer stopped; `cancelled` is the+ # other spelling seen in the vocabulary. Anything else means Stop did not settle the turn.+ if summary["end"] not in ("aborted", "cancelled"):+ raise FlowError(f"the turn ended as {summary['end']!r} after Stop, which is not a "+ "cancellation")+ if marker.exists():+ raise FlowError("Stop was acknowledged but the tool ran anyway: the turn was not really "+ "cancelled")+ print(f" PASS A05 (a live turn was stopped and settled as {summary['end']!r})")+ finally:+ if agent:+ agent.close()+ worker.terminate()+ worker.wait(timeout=20)+++def main() -> int:+ parser = argparse.ArgumentParser(description=__doc__,+ formatter_class=argparse.RawDescriptionHelpFormatter)+ parser.add_argument("--dsh", required=True, help="path to the dsh executable")+ parser.add_argument("--profile", default="web",+ help="the base bundle profile the Strata patch layers over")+ parser.add_argument("--base-port", type=int, default=8300)+ parser.add_argument("--only", default=None, help="run one flow: A03, A04 or A05")+ parser.add_argument("--keep", action="store_true", help="keep the temporary directory")+ args = parser.parse_args()++ dsh = Path(args.dsh)+ if not dsh.exists():+ raise SystemExit(f"no dsh at {dsh}")+ if not SCRIPTED_OVERLAY.exists():+ raise SystemExit(f"the test-only profile overlay is missing: {SCRIPTED_OVERLAY}")+ for tool in ("zstd",):+ if shutil.which(tool) is None:+ raise SystemExit(f"{tool} is required to read the durable session")++ tmp = Path(tempfile.mkdtemp(prefix="strata-tool-flow-"))+ print(f"working in {tmp}")+ results: dict = {}+ try:+ flows = {"A03": flow_a03, "A04": flow_a04, "A05": flow_a05}+ chosen = [args.only] if args.only else list(flows)+ for index, name in enumerate(chosen):+ flows[name](dsh, tmp, args.profile, args.base_port + index * 10, results)+ print()+ print("PASS")+ print(json.dumps(results, indent=2, sort_keys=True))+ return 0+ except FlowError as e:+ print(f"\nFAIL {args.only or ''}: {e}")+ print(json.dumps(results, indent=2, sort_keys=True))+ return 1+ finally:+ if args.keep:+ print(f"kept {tmp}")+ else:+ shutil.rmtree(tmp, ignore_errors=True)+++if __name__ == "__main__":+ sys.exit(main())