From 345d6faddd4fa8fc5380b682adfd953847e9d03d Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 11:05:39 +0000 Subject: [PATCH 01/14] M9.31: door gate on every host and role The door gate now reads every host's calls: Codex's request_user_input is the question tool; apply_patch (Codex command, OpenCode patchText) is judged path by path as a Write; dsh's {"arguments": ""} MCP input is read as its object (same doors, same once digest); OpenCode's _ MCP names match; OpenCode and dsh seed the Bash reader from tool_input.workdir. authorize/revoke refuse DSH_SHELL, CODEX_THREAD_ID and ALOOP_AGENT_SHELL shells as well as CLAUDECODE. door.denied/door.crossed name the agent (agent_id, agent_type) and a subagent is told to report ASK[door] to its supervisor. One deadline covers the whole decision and the lock is polled until it, so a held doors.lock is exit 2, never a hang. change-agent-settings covers Codex, OpenCode and dsh settings and aloop's host adapters. mission-doors.test.py: 21 -> 29 checks. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- plugin/mission/doors.json | 65 ++++++++-- plugin/mission/doors.py | 195 +++++++++++++++++++++-------- scripts/test/mission-doors.test.py | 177 +++++++++++++++++++++++++- 3 files changed, 366 insertions(+), 71 deletions(-) diff --git a/plugin/mission/doors.json b/plugin/mission/doors.json index 837e1fce..29177d8e 100644 --- a/plugin/mission/doors.json +++ b/plugin/mission/doors.json @@ -24,9 +24,9 @@ } ], "tools": [ - "^mcp__github__(?:merge_pull_request|enable_pr_auto_merge)$", + "^(?:mcp__github__|github_)(?:merge_pull_request|enable_pr_auto_merge)$", { - "tool": "^mcp__github__(?:push_files|create_or_update_file|delete_file)$", + "tool": "^(?:mcp__github__|github_)(?:push_files|create_or_update_file|delete_file)$", "input": { "branch": "{DEF}" } @@ -47,7 +47,9 @@ "git push origin HEAD" ], "tools": [ - "mcp__github__merge_pull_request" + "mcp__github__merge_pull_request", + "github_merge_pull_request", + "github_enable_pr_auto_merge" ] } }, @@ -223,7 +225,7 @@ } ], "tools": [ - "(?i)^mcp__\\w*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)\\w*?__(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)_)?(?:send|reply|forward|post|chat_post)\\w*$" + "(?i)^(?:mcp__\\w*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)\\w*?__|(?!mcp__)\\w*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)[A-Za-z0-9-]*_)(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)_)?(?:send|reply|forward|post|chat_post)\\w*$" ], "examples": { "bash": [ @@ -233,7 +235,10 @@ "mcp__Gmail__send_message", "mcp__slack__post_message", "mcp__slack__slack_post_message", - "mcp__slack__slack_reply_to_thread" + "mcp__slack__slack_reply_to_thread", + "Gmail_send_message", + "slack_slack_post_message", + "slack_post_message" ] } }, @@ -355,7 +360,7 @@ } ], "tools": [ - "(?i)^mcp__\\w*?__(?:share_file|share|add_permission|create_permission)$" + "(?i)^(?:mcp__\\w*?__|(?!mcp__)\\w+?_)(?:share_file|share|add_permission|create_permission)$" ], "examples": { "bash": [ @@ -363,7 +368,8 @@ "gh gist create --public a.txt" ], "tools": [ - "mcp__Google_Drive__share_file" + "mcp__Google_Drive__share_file", + "Google_Drive_share_file" ] } }, @@ -413,17 +419,24 @@ "why": "settings and gates decide what every later step may do", "paths": [ "(?:^|/)\\.claude/settings(?:\\.local)?\\.json$", - "(?:^|/)\\.codex/config\\.toml$" + "(?:^|/)\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)$", + "(?:^|/)(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js$", + "(?:^|/)opencode\\.jsonc?$", + "(?:^|/)\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json)$" ], "plugin_paths": [ "^hooks/hooks\\.json$", - "^bin/(?:gate-[^/]+|_failclosed\\.sh|_gatelib\\.sh)$", + "^bin/(?:gate-[^/]+|_failclosed\\.sh|_gatelib\\.sh|host-adapter|install)$", + "^hosts/(?:opencode/aloop\\.js|dsh/aloop\\.mjs)$", "^mission/doors\\.(?:json|py)$" ], "bash": [ { "writes": "(?:^|/)\\.claude/settings(?:\\.local)?\\.json$" }, + { + "writes": "(?:^|/)(?:\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|opencode\\.jsonc?|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$" + }, { "prog": "git", "args": "(?i)^config\\b(?!.*\\s(?:--get\\S*|--list|-l|get|list)(?:\\s|$)).*\\score\\.hookspath\\s+\\S" @@ -438,10 +451,22 @@ ".claude/settings.json", ".claude/settings.local.json", "~/.claude/settings.json", - "home/wt/.claude/settings.local.json" + "home/wt/.claude/settings.local.json", + ".codex/config.toml", + ".codex/hooks.json", + "~/.codex/agents/red-worker.toml", + ".opencode/plugins/aloop.js", + "~/.config/opencode/plugins/aloop.js", + ".opencode/opencode.json", + "opencode.jsonc", + "~/.dsh/profiles/aloop/cordis.patch.yml", + "~/.dsh/profiles/aloop/package.json" ], "bash": [ "echo '{}' > .claude/settings.json", + "echo '{}' > opencode.json", + "cat x >> .codex/hooks.json", + "printf x | tee ~/.dsh/profiles/aloop/cordis.patch.yml", "git config core.hooksPath /dev/null", "git config --unset core.hooksPath" ] @@ -708,19 +733,33 @@ "gh secret set TOKEN --help", "perl -Ilib -ne 'print if /door/' .aladdin/logs/events.jsonl", "gcloud functions describe deploy-notifier --region=us-central1", - "python3 -c \"import _mission as M; print(len(M.records('.', 'door.authorized')))\"" + "python3 -c \"import _mission as M; print(len(M.records('.', 'door.authorized')))\"", + "cat .codex/config.toml", + "jq . opencode.json", + "cat ~/.dsh/profiles/aloop/cordis.patch.yml", + "cat plugin/hosts/opencode/aloop.js" ], "tools": [ "Read", "mcp__github__create_pull_request", "mcp__github__push_files", - "mcp__slack__slack_list_channels" + "mcp__slack__slack_list_channels", + "github_create_pull_request", + "github_push_files", + "slack_slack_list_channels", + "Gmail_search_threads", + "slack_get_post_history", + "mcp__drive__get_share" ], "paths": [ "src/a.py", "docs/agents/mission/research/x.json", "README.md", - "src/mission/ledger.jsonl" + "src/mission/ledger.jsonl", + ".codex/notes.md", + ".dsh/skills/aloop-mission/SKILL.md", + "docs/codex/config.toml", + "src/opencode.ts" ] } } diff --git a/plugin/mission/doors.py b/plugin/mission/doors.py index 8fd2053e..f2499ecd 100644 --- a/plugin/mission/doors.py +++ b/plugin/mission/doors.py @@ -27,12 +27,18 @@ every two-way example to none) and the ledger is intact. Exit 0 · 2 mission doors hook the PreToolUse gate (the hook payload on stdin): deny JSON for an unauthorized one-way - door or a wall, silent otherwise; exit 2 when it cannot decide (fails closed) + door or a wall, silent otherwise; exit 2 when it cannot decide within its deadline (fails + closed). On every host (M9.31): Claude Code and Codex run it from hooks.json, OpenCode and + dsh from their plugins; a patch (apply_patch) is judged path by path as a Write, dsh's + JSON-string input of an MCP tool as the object it encodes, and a bound worker's + tool_input.workdir (OpenCode, dsh) is where its command runs. A subagent that hits a door + reports ASK[door] to its supervisor, which asks the human Grants: standing (one authorization covers the door), once (one exact action), never (a wall: the human runs it in their own terminal), command (a question-only door, answered by its own human command). An agent never runs `doors authorize` or writes the ledger (forge-human-record), -and `authorize` refuses a shell an agent host runs (CLAUDECODE set). +and `authorize` refuses a shell an agent host runs (CLAUDECODE, DSH_SHELL, CODEX_THREAD_ID or +ALOOP_AGENT_SHELL set). """ import contextlib import functools @@ -64,11 +70,18 @@ ID = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$") SHA = re.compile(r"^[0-9a-f]{64}$") DATE = re.compile(r"^\d{4}-\d{2}-\d{2}$") -QUIET = {"Read", "Grep", "Glob", "LS", "TodoWrite", "TodoRead", "WebSearch", "WebFetch", "NotebookRead", "ToolSearch"} +# read-only tools, as each host names them (OpenCode and dsh pass their own built-ins' names through) +QUIET = {"Read", "Grep", "Glob", "LS", "TodoWrite", "TodoRead", "WebSearch", "WebFetch", "NotebookRead", "ToolSearch", + "grep", "glob", "list", "read_image", "todoread", "todowrite", "webfetch", "websearch", "codesearch"} EDITS = ("Write", "Edit", "MultiEdit", "NotebookEdit") -QUESTION = "AskUserQuestion" -# set in every shell an agent host runs (the agent's Bash tool): an authorization never comes from there -AGENT_SHELL = ("CLAUDECODE",) +# the question tool: Claude Code's, Codex's (OpenCode's `question` and dsh's `ask_user_question` reach the +# gate as AskUserQuestion, mapped by their plugins) +QUESTIONS = ("AskUserQuestion", "request_user_input", "request_user_input_async") +# set in every shell an agent host runs (the agent's Bash tool): Claude Code, dsh, Codex, and OpenCode (its +# plugin exports ALOOP_AGENT_SHELL); an authorization never comes from there +AGENT_SHELL = ("CLAUDECODE", "DSH_SHELL", "CODEX_THREAD_ID", "ALOOP_AGENT_SHELL") +# the paths a patch adds, updates, deletes or moves to (host-adapter's PATCH_PATHS) +PATCH = re.compile(r"^\*\*\* (?:Add|Update|Delete) File: (.+)$|^\*\*\* Move to: (.+)$", re.M) TAG = re.compile(r"\[door:([^\]\s]*)\]") WRAPPERS = {"sudo", "doas", "env", "nohup", "time", "command", "exec", "nice", "timeout", "stdbuf", "caffeinate", "ionice"} SHELLS = {"sh", "bash", "zsh", "dash", "ksh"} @@ -698,15 +711,57 @@ def rel_path(cwd, path): return [x for x in (r, given, full) if not x.startswith("..")], (plug if not plug.startswith("..") else None) +def input_of(tool, inp): + """The input the doors read. dsh hands a tool outside its built-ins (an MCP tool) its input as one JSON + string, {"arguments": "{…}", "workdir": …}: it is read as the object it encodes, so an input pattern + matches and a once-digest is the same as on a host that sends the object.""" + inp = inp if isinstance(inp, dict) else {} + if tool not in ("Bash", "apply_patch") + EDITS and isinstance(inp.get("arguments"), str): + try: + args = json.loads(inp["arguments"]) + except ValueError: + return inp + if isinstance(args, dict): + return args + return inp + + +def written(tool, inp): + """The paths an edit writes: a Write's or an Edit's file, each file a patch adds, updates, deletes or + moves to (apply_patch: Codex's command, OpenCode's patchText), each judged as a Write to it.""" + if tool in EDITS: + paths = [inp.get("file_path") or inp.get("notebook_path")] + elif tool == "apply_patch": + paths = [(a or b).strip() for k in ("command", "patchText") if isinstance(inp.get(k), str) + for a, b in PATCH.findall(inp[k])] + else: + paths = [] + return [x for x in paths if isinstance(x, str) and x] + + +def workdir(inp): + """Where a Bash command starts: OpenCode and dsh run a bound worker's command in tool_input.workdir (the + payload's cwd stays the project, which still names the catalogue and the ledger) when it is an absolute + directory that exists; anywhere else the payload's cwd (""). Claude Code sends none.""" + wd = inp.get("workdir") + if os.environ.get("AGENTIC_HOST") in ("opencode", "dsh") and isinstance(wd, str) and os.path.isabs(wd) \ + and os.path.isdir(wd): + return wd + return "" + + def classify(cwd, tool, inp, cat=None, assume_when=False): """[door] the action crosses, in catalogue order (none: two-way).""" cat = cat or catalogue() default = re.compile("(?:%s)" % cat.get("default_branch", "main|master")) ctx = contexts(cwd, assume_when) - inp = inp if isinstance(inp, dict) else {} - vs = place(cwd, branches(cwd, views(str(inp.get("command", ""))))) if tool == "Bash" else [] - path = inp.get("file_path") or inp.get("notebook_path") if tool in EDITS else None - proj_rel, plug_rel = rel_path(cwd, path) if isinstance(path, str) and path else ([], None) + inp = input_of(tool, inp) + vs = place(cwd, branches(cwd, views(str(inp.get("command", "")), 0, workdir(inp)))) if tool == "Bash" else [] + proj_rel, plug_rel = [], [] + for path in written(tool, inp): + rel, plug = rel_path(cwd, path) + proj_rel += rel + plug_rel += [plug] if plug else [] out = [] for d in cat["doors"]: if d["grant"] == "command": @@ -715,7 +770,7 @@ def classify(cwd, tool, inp, cat=None, assume_when=False): hit = hit or any(t["tool"].search(tool) and all(isinstance(inp.get(k), str) and r.fullmatch(inp[k]) for k, r in t["input"].items()) for t in d["_tools"]) hit = hit or any(r.search(x) for r in d["_paths"] for x in proj_rel) - hit = hit or bool(plug_rel and any(r.search(plug_rel) for r in d["_plugin_paths"])) + hit = hit or any(r.search(x) for r in d["_plugin_paths"] for x in plug_rel) if hit: out.append(d) return out @@ -891,78 +946,109 @@ def deny(reason): return 0 +def worker_text(asked): + """A subagent has no question tool: it stops and hands the door to the supervisor, which asks.""" + return ("You are a subagent with no question tool: do not ask; stop this step and report %s in your output, " + "so your supervisor asks the human." % ", ".join("ASK[door] %s" % d["id"] for d in asked)) + + def hook(cwd_default): if os.environ.get("SKIP_HOOKS") == "1": return 0 - payload = json.loads(sys.stdin.read() or "{}") - if not isinstance(payload, dict): - raise ValueError("the hook payload is not an object") - tool = payload.get("tool_name") or "" - if tool in QUIET: - return 0 - cwd = payload.get("cwd") or cwd_default - if not os.path.isdir(cwd) or not os.path.isdir(_paths.docs(cwd)): - return 0 # not an aloop project - inp = payload.get("tool_input") if isinstance(payload.get("tool_input"), dict) else {} - session = payload.get("session_id") or "" - cat = catalogue() - if tool == QUESTION: - why = question_problem(cwd, inp, session, cat) if live_run(cwd, session) else None - return deny(why) if why else 0 - with deadline(READ_SECONDS): + with deadline(READ_SECONDS) as end: # the whole decision, its lock included: past it the gate fails closed + payload = json.loads(sys.stdin.read() or "{}") + if not isinstance(payload, dict): + raise ValueError("the hook payload is not an object") + tool = payload.get("tool_name") or "" + if tool in QUIET: + return 0 + cwd = payload.get("cwd") or cwd_default + if not os.path.isdir(cwd) or not os.path.isdir(_paths.docs(cwd)): + return 0 # not an aloop project + inp = input_of(tool, payload.get("tool_input")) + session = payload.get("session_id") or "" + # the role that hit the door: a subagent's tool call names its agent (every host's payload) + who = {k: payload[k] for k in ("agent_id", "agent_type") if isinstance(payload.get(k), str) and payload[k]} + cat = catalogue() + if tool in QUESTIONS: + why = question_problem(cwd, inp, session, cat) if live_run(cwd, session) else None + return deny(why) if why else 0 hits = classify(cwd, tool, inp, cat) - if not hits: - return 0 - with serialized(cwd): # check, then record: a once-authorization is crossed once - rc, lines, crossings = verdict(cwd, tool, inp, cat, hits) - sha = action_sha(tool, inp) - if rc: - for d in hits: - if d["grant"] in ("standing", "once") and not authorization(cwd, d, sha): + if not hits: + return 0 + with serialized(cwd, end): # check, then record: a once-authorization is crossed once + rc, lines, crossings = verdict(cwd, tool, inp, cat, hits) + sha = action_sha(tool, inp) + asked = [d for d in hits if d["grant"] in ("standing", "once") and not authorization(cwd, d, sha)] if rc else [] + disarm() # decided: the records below are written whole, never cut by the alarm + if rc: + for d in asked: M.emit(cwd, "door.denied", role="gate", door=d["id"], grant=d["grant"], action_sha256=sha, tool=tool, - session_id=session) - return deny(" ".join(l for l in lines if not l.startswith("[door]"))) - for d, row in crossings: - if row["data"].get("action_sha256"): # a once-authorization is used up by its first crossing - M.record(cwd, "door.crossed", subject(d["id"]), "harness", door=d["id"], authorization=row["event_id"], - action_sha256=sha, session_id=session) - else: - M.emit(cwd, "door.crossed", role="harness", door=d["id"], authorization=row["event_id"], - action_sha256=sha, session_id=session) + session_id=session, **who) + reason = " ".join(l for l in lines if not l.startswith("[door]")) + return deny(reason + (" " + worker_text(asked) if asked and "agent_type" in who else "")) + for d, row in crossings: + if row["data"].get("action_sha256"): # a once-authorization is used up by its first crossing + M.record(cwd, "door.crossed", subject(d["id"]), "harness", door=d["id"], authorization=row["event_id"], + action_sha256=sha, session_id=session, **who) + else: + M.emit(cwd, "door.crossed", role="harness", door=d["id"], authorization=row["event_id"], + action_sha256=sha, session_id=session, **who) return 0 -READ_SECONDS = 8 # under the hook's own timeout (plugin/hooks/hooks.json: 15 s) +READ_SECONDS = 8 # under the hook's own timeout (plugin/hooks/hooks.json, the OpenCode and dsh plugins: 15 s) @contextlib.contextmanager def deadline(seconds): - """A TimeoutError after `seconds` (POSIX): a gate that cannot read the action in time fails closed (exit 2) - before the host's hook timeout, which a host may treat as a non-blocking error, lets the call through.""" + """A TimeoutError after `seconds` (POSIX); yields the monotonic time it ends at, which the lock waits for + too: a gate that cannot decide in time fails closed (exit 2) before the host's hook timeout, which a host + may treat as a non-blocking error, lets the call through.""" + end = time.monotonic() + seconds if not hasattr(signal, "setitimer"): - yield + yield end return def stop(*_): - raise TimeoutError("the door gate could not read the action within %d s" % seconds) + raise TimeoutError("the door gate could not decide within %g s" % seconds) old = signal.signal(signal.SIGALRM, stop) signal.setitimer(signal.ITIMER_REAL, seconds) try: - yield + yield end finally: signal.setitimer(signal.ITIMER_REAL, 0) signal.signal(signal.SIGALRM, old) +def disarm(): + """Stop the deadline's alarm once the decision is made.""" + if hasattr(signal, "setitimer"): + signal.setitimer(signal.ITIMER_REAL, 0) + + @contextlib.contextmanager -def serialized(cwd): - """One door decision at a time per project (an exclusive lock on .aladdin/state/doors.lock).""" +def serialized(cwd, end): + """One door decision at a time per project (an exclusive lock on .aladdin/state/doors.lock), waited for + until the decision's deadline `end` (time.monotonic()), never forever: a lock another gate holds past + it fails closed.""" if fcntl is None: yield return - fd = os.open(os.path.join(_paths.home(cwd, "state"), "doors.lock"), os.O_RDWR | os.O_CREAT, 0o644) + path = os.path.join(_paths.home(cwd, "state"), "doors.lock") + fd = os.open(path, os.O_RDWR | os.O_CREAT, 0o644) try: - fcntl.flock(fd, fcntl.LOCK_EX) + try: + while True: + try: + fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) + break + except BlockingIOError: + if time.monotonic() >= end: + raise TimeoutError from None + time.sleep(0.02) + except TimeoutError: # the poll's own deadline, or the alarm's + raise TimeoutError("another door gate held %s past this decision's deadline" % M.rel(cwd, path)) from None yield finally: os.close(fd) @@ -1043,6 +1129,7 @@ def cmd_classify(cwd, o): else: print("mission doors classify (--bash CMD | --tool NAME [--input JSON] [--path P])", file=sys.stderr) return 64 + inp = input_of(tool, inp) # as the hook reads it, so the digest it prints is the hook's rc, lines, _ = verdict(cwd, tool, inp) if o.get("json"): print(json.dumps({"exit": rc, "doors": [d["id"] for d in classify(cwd, tool, inp)], "lines": lines, diff --git a/scripts/test/mission-doors.test.py b/scripts/test/mission-doors.test.py index 9811268f..f61a41aa 100644 --- a/scripts/test/mission-doors.test.py +++ b/scripts/test/mission-doors.test.py @@ -8,12 +8,17 @@ the human must name a one-way door the gate stopped and the human has not authorized. python3 scripts/test/mission-doors.test.py """ +import contextlib +import fcntl import hashlib +import io import json import os import shutil import subprocess import sys +import threading +import time sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "fixtures")) from mission_fixture import Project, M, BIN # noqa: E402 @@ -25,6 +30,8 @@ EVENTS = os.path.join(BIN, "events") PASS = [0] GENV = {"GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@t", "GIT_COMMITTER_NAME": "t", "GIT_COMMITTER_EMAIL": "t@t"} +for k in ("DSH_SHELL", "CODEX_THREAD_ID", "ALOOP_AGENT_SHELL"): + os.environ.pop(k, None) # the human's own terminal: no agent host's shell may authorize a door def ok(name): @@ -36,9 +43,9 @@ def classify(p, cmd): return p.run("doors", "classify", "--bash", cmd) -def hook(p, tool, inp, session="s1", cwd=None, env=None): - payload = {"tool_name": tool, "tool_input": inp, "cwd": cwd or p.dir, "session_id": session, - "hook_event_name": "PreToolUse"} +def hook(p, tool, inp, session="s1", cwd=None, env=None, **extra): + payload = dict({"tool_name": tool, "tool_input": inp, "cwd": cwd or p.dir, "session_id": session, + "hook_event_name": "PreToolUse"}, **extra) return subprocess.run([os.path.join(BIN, "mission"), "doors", "hook"], input=json.dumps(payload), cwd=cwd or p.dir, env=dict(p.env, **(env or {})), capture_output=True, text=True) @@ -680,7 +687,169 @@ def wrapped(*a, **k): ok("round 3: [[ ]] and heredoc delimiters, subshell pipes, stdin order, arithmetic fallbacks, entry points, placed paths, " "branch switches; reads pass") -# 21) list and the area's docstring +# 21) M9.31: the door rule on every host and role. Codex asks with request_user_input; a patch (Codex's +# apply_patch command, OpenCode's patchText) is judged path by path as a Write; dsh hands an MCP tool +# its input as one JSON string and OpenCode names it _; OpenCode and dsh send a bound +# worker's directory as tool_input.workdir; every agent host's shell is refused by authorize; the +# role that hit a door is recorded and a worker reports the ASK; the whole decision has one deadline +def ask_as(tool, text): + return hook(p, tool, {"questions": [{"id": "q", "header": "h", "question": text, "options": []}]}) + + +for tool in ("request_user_input", "request_user_input_async"): + assert "two-way" in denied(ask_as(tool, "Should I name the helper parse_row or parse_line?")), tool + assert "unknown door" in denied(ask_as(tool, "[door:nope] ?")), tool + assert ask_as(tool, "[door:change-intent] The goal says X but the code needs Y: change the intent?").stdout == "" +assert D.QUESTIONS == ("AskUserQuestion", "request_user_input", "request_user_input_async") +ok("questions: Codex's request_user_input is the question tool too (a two-way question is decided, not asked)") + + +def patch(*lines): + return "*** Begin Patch\n" + "\n".join(lines) + "\n*** End Patch\n" + + +assert "ASK[door] change-agent-settings" in denied(hook(p, "apply_patch", {"command": patch("*** Add File: .claude/settings.json", "+{}")})) +assert "BLOCK[door] forge-human-record" in denied(hook(p, "apply_patch", {"patchText": patch( + "*** Update File: docs/agents/mission/ledger.jsonl", "@@", "-a", "+b")})) +assert hook(p, "apply_patch", {"command": patch("*** Update File: src/x.py", "@@", "-a", "+b")}).stdout == "" +assert hook(p, "apply_patch", {"patchText": patch("*** Add File: src/y.py", "+*** Add File: .claude/settings.json")}).stdout == "" +assert "change-agent-settings" in denied(hook(p, "apply_patch", {"command": patch( + "*** Update File: src/x.py", "*** Move to: .codex/config.toml", "@@", "-a", "+b")})) +assert "forge-human-record" in denied(hook(p, "apply_patch", {"command": patch( + "*** Add File: src/z.py", "+x", "*** Delete File: %s" % os.path.join(wt, "docs/agents/mission/ledger.jsonl"))})) +ok("apply_patch (Codex command, OpenCode patchText): each added, updated, deleted or moved path is judged as a Write") + + +def ids(tool, inp, cwd=None): + return [d["id"] for d in D.classify(cwd or p.dir, tool, inp, CAT)] + + +pushed = {"owner": "o", "repo": "r", "branch": "main", "files": [{"path": "a", "content": "b"}], "message": "m"} +dsh_push = {"arguments": json.dumps(pushed), "workdir": p.dir} +assert ids("mcp__github__push_files", dsh_push) == ids("mcp__github__push_files", pushed) == ["push-default-branch"] +assert ids("mcp__github__push_files", {"arguments": json.dumps(dict(pushed, branch="feat")), "workdir": p.dir}) == [] +assert ids("mcp__github__push_files", {"arguments": "[1]"}) == ids("mcp__github__push_files", {"arguments": "{"}) == [] +shared = {"fileId": "f1", "role": "writer", "type": "anyone"} +r = denied(hook(p, "mcp__Google_Drive__share_file", {"arguments": json.dumps(shared), "workdir": p.dir})) +assert "ASK[door] visibility-or-sharing" in r and "--once %s" % D.action_sha("mcp__Google_Drive__share_file", shared) in r, r +for tool, inp, want in (("github_merge_pull_request", {"pullNumber": 1}, ["push-default-branch"]), + ("github_enable_pr_auto_merge", {}, ["push-default-branch"]), + ("github_push_files", pushed, ["push-default-branch"]), ("github_push_files", dict(pushed, branch="feat"), []), + ("Gmail_send_message", {}, ["send-external-message"]), + ("slack_slack_post_message", {}, ["send-external-message"]), + ("Google_Drive_share_file", {}, ["visibility-or-sharing"]), + ("github_create_pull_request", {}, []), ("slack_slack_list_channels", {}, []), + ("Gmail_search_threads", {}, []), ("slack_get_post_history", {}, []), + ("mcp__drive__get_share", {}, [])): + assert ids(tool, inp) == want, (tool, ids(tool, inp)) +ok("MCP: dsh's {arguments: JSON} input and OpenCode's _ names cross the same doors (and the same once digest)") + +g = Project(mode="spoon-fed") +subprocess.run(["git", "branch", "-M", "main"], cwd=g.dir, check=True) +subprocess.run(["git", "add", "-A"], cwd=g.dir, check=True) +subprocess.run(["git", "commit", "-qm", "seed"], cwd=g.dir, env=dict(g.env, **GENV), check=True) +gwt = os.path.join(g.dir, "home", "feat-wt") +subprocess.run(["git", "worktree", "add", "-q", gwt, "-b", "feat"], cwd=g.dir, check=True) +push = {"command": "git push origin HEAD", "workdir": gwt} +assert "ASK[door] push-default-branch" in denied(hook(g, "Bash", push)) # Claude Code sends no workdir: ignored +for host in ("opencode", "dsh"): + on = {"AGENTIC_HOST": host} + assert hook(g, "Bash", push, env=on).stdout == "", host # the worker's own branch: two-way + assert hook(g, "Bash", {"command": "git reset --hard", "workdir": gwt}, env=on).stdout == "", host + assert "push-default-branch" in denied(hook(g, "Bash", dict(push, workdir=g.dir), env=on)) + assert "discard-local-work" in denied(hook(g, "Bash", {"command": "git reset --hard", "workdir": g.dir}, env=on)) +for wd in ("home/feat-wt", os.path.join(g.dir, "gone"), 7): # not absolute, not there: the cwd decides + assert "push-default-branch" in denied(hook(g, "Bash", dict(push, workdir=wd), env={"AGENTIC_HOST": "dsh"})), wd +assert "push-default-branch" in denied(hook(g, "Bash", push, env={"AGENTIC_HOST": "codex"})) +ok("OpenCode and dsh: a bound worker's tool_input.workdir is where its command runs (its branch, its worktree)") + +ledger_before = open(M.ledger(p.dir)).read() +for var, val in (("DSH_SHELL", "1"), ("CODEX_THREAD_ID", "x"), ("ALOOP_AGENT_SHELL", "1"), ("CLAUDECODE", "1")): + for verb in ("authorize", "revoke"): + r = subprocess.run([os.path.join(BIN, "mission"), "doors", verb, "production-deploy", "--by", "Adeel", "--reason", "r"], + cwd=p.dir, env=dict(p.env, **{var: val}), capture_output=True, text=True) + assert r.returncode == 2 and "own terminal" in r.stderr and var in r.stderr, (var, verb, r.returncode, r.stderr) +assert open(M.ledger(p.dir)).read() == ledger_before +assert D.AGENT_SHELL == ("CLAUDECODE", "DSH_SHELL", "CODEX_THREAD_ID", "ALOOP_AGENT_SHELL") +ok("authorize and revoke refuse every agent host's shell (Claude Code, dsh, Codex, OpenCode) and write nothing") + +worker = {"agent_id": "a-7", "agent_type": "aloop:green-worker"} +r = denied(hook(p, "Bash", {"command": "terraform apply"}, **worker)) +assert "ASK[door] production-deploy" in r and "report ASK[door] production-deploy" in r and "supervisor" in r, r +ev = p.events("door.denied")[-1] # the event's own agent (ids.agent), its type +assert (ev["data"]["door"], ev["agent_id"], ev["ids"]["agent"], ev["data"]["agent_type"]) == \ + ("production-deploy", "a-7", "a-7", "aloop:green-worker"), ev +r = denied(hook(p, "Bash", {"command": "terraform apply"})) # the main thread asks the human itself +ev = p.events("door.denied")[-1] +assert "supervisor" not in r and ev["agent_id"] == "" and "agent_type" not in ev["data"], (r, ev) +assert "report ASK" not in denied(hook(p, "Bash", {"command": "git commit --no-verify -m x"}, **worker)) # a wall: nothing to ask +assert hook(p, "Bash", {"command": "git push origin main"}, **worker).stdout == "" # standing, authorized above +ev = p.events("door.crossed")[-1] +assert (ev["data"]["door"], ev["agent_id"], ev["data"]["agent_type"]) == ("push-default-branch", "a-7", "aloop:green-worker"), ev +assert p.run("doors", "authorize", "credentials-and-secrets", "--by", "Adeel", "--reason", "rotate", "--once", + D.action_sha("Bash", {"command": "gh secret set W"})).returncode == 0 +assert hook(p, "Bash", {"command": "gh secret set W"}, **worker).stdout == "" +x = M.records(p.dir, "door.crossed", "door:credentials-and-secrets")[-1]["data"] +assert (x["agent_id"], x["agent_type"]) == ("a-7", "aloop:green-worker") and M.ledger_problems(p.dir) == [], x +ok("roles: door.denied and door.crossed name the agent; a worker is told to stop and report ASK[door] to its supervisor") + +stopping = json.dumps({"tool_name": "Bash", "tool_input": {"command": "terraform apply"}, "cwd": p.dir, "session_id": "s1", + "hook_event_name": "PreToolUse"}) + + +def hook_in(payload, seconds=1): + """The hook in this process, with a short deadline: (exit, stdout, stderr, seconds taken).""" + real, stdin = D.READ_SECONDS, sys.stdin + D.READ_SECONDS, sys.stdin = seconds, io.StringIO(payload) + out, err, t0 = io.StringIO(), io.StringIO(), time.time() + try: + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): + rc = D.main(["hook"], p.dir) + finally: + D.READ_SECONDS, sys.stdin = real, stdin + return rc, out.getvalue(), err.getvalue(), time.time() - t0 + + +held = os.open(os.path.join(D._paths.home(p.dir, "state"), "doors.lock"), os.O_RDWR | os.O_CREAT, 0o644) +fcntl.flock(held, fcntl.LOCK_EX) # another gate holds the lock (and never lets go) +watchdog = threading.Timer(10, fcntl.flock, (held, fcntl.LOCK_UN)) # so a gate that blocks fails here, not hangs +watchdog.start() +n = len(p.events("door.denied")) +try: + rc, out, err, took = hook_in(stopping) +finally: + watchdog.cancel() + os.close(held) +assert rc == 2 and out == "" and "BLOCK[doors]" in err and "doors.lock" in err and took < 3, (rc, out, err, took) +assert len(p.events("door.denied")) == n # nothing decided, nothing recorded +rc, out, err, took = hook_in(stopping) +assert rc == 0 and "ASK[door] production-deploy" in out, (rc, out, err) +real_verdict = D.verdict +D.verdict = lambda *a, **k: time.sleep(10) # a decision that hangs past classify +try: + rc, out, err, took = hook_in(stopping) +finally: + D.verdict = real_verdict +assert rc == 2 and out == "" and "BLOCK[doors]" in err and took < 3, (rc, out, err, took) +ok("deadline: one deadline covers the whole decision; a held doors.lock or a hung verdict is exit 2 within it, never a hang") + +for x in ("hosts/opencode/aloop.js", "hosts/dsh/aloop.mjs", "bin/host-adapter", "bin/install"): + assert ids("Write", {"file_path": os.path.join(D.PLUGIN, x)}) == ["change-agent-settings"], x + assert ids("apply_patch", {"command": patch("*** Update File: %s" % os.path.join(D.PLUGIN, x))}) == ["change-agent-settings"] +for x in (".codex/hooks.json", ".codex/config.toml", "~/.codex/agents/red-worker.toml", ".opencode/plugins/aloop.js", + ".opencode/opencode.jsonc", "opencode.json", "~/.config/opencode/plugins/aloop.js", + "~/.dsh/profiles/aloop/cordis.patch.yml", "~/.dsh/profiles/aloop/package.json"): + assert "change-agent-settings" in denied(hook(p, "Write", {"file_path": x, "content": "{}"})), x +assert ids("Write", {"file_path": os.path.join(D.PLUGIN, "bin", "README.md")}) == [] +for cmd in ("echo '{}' > opencode.json", "cat x >> .codex/hooks.json", "printf x | tee ~/.dsh/profiles/aloop/cordis.patch.yml"): + assert "change-agent-settings" in doors_of(cmd), cmd +for cmd in ("cat .codex/config.toml", "jq . opencode.json", "cat ~/.dsh/profiles/aloop/cordis.patch.yml"): + assert doors_of(cmd) == [], cmd +assert p.run("doors", "check").returncode == 0 +ok("settings: every host's settings and hooks and aloop's own host adapters are one-way; reading them is two-way") +shutil.rmtree(g.dir, ignore_errors=True) + +# 22) list and the area's docstring r = p.run("doors", "list", "--json") lst = json.loads(r.stdout) pd = next(d for d in lst["doors"] if d["id"] == "publish-release") From 2eb9b50ceab58c5502b6dc2ce7cef1e1b5d50f00 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 11:04:04 +0000 Subject: [PATCH 02/14] M9.31: dispatched roles carry the core principles; hosts name the door gate `mission principles hook` is a SubagentStart hook: inside an aloop project it hands the role the core block (the door rule first) as additionalContext, exactly as `principles show --core` prints it; outside one, or on any error, it prints nothing and exits 0, so it never blocks a dispatch. hooks.json runs it third in the SubagentStart "*" group (timeout 10), which covers Claude Code and Codex. Each host adapter's tool policy now names the door gate, and each names its ask-the-human tool (QUESTION, shown by `host describe`). Tests: the hook's output in and out of a project and on bad input; hooks.json wiring for every role; the Codex runner delivers the block and the door gate denies a root `git push origin main`; host describe. The OpenCode/dsh parity check in hook-matchers stays red until their M9.31 parts land. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- plugin/bin/_host.py | 18 ++++++++--- plugin/hooks/hooks.json | 3 +- plugin/mission/principles.py | 31 +++++++++++++++++-- scripts/test/codex-hooks.test.py | 9 +++++- scripts/test/hook-matchers.test.py | 31 +++++++++++++++++++ scripts/test/host-adapter.test.py | 12 ++++++++ scripts/test/mission-principles.test.py | 40 ++++++++++++++++++++++++- 7 files changed, 135 insertions(+), 9 deletions(-) diff --git a/plugin/bin/_host.py b/plugin/bin/_host.py index 15d81840..fbbc7e2f 100644 --- a/plugin/bin/_host.py +++ b/plugin/bin/_host.py @@ -52,6 +52,7 @@ class HostAdapter: INSTALL_GATE = "" # how the hooks reach the host ENFORCE_GATE = "native" # native | outer — never warn TOOL_POLICY = "" # what the hooks deny a worker + QUESTION = "" # the tool a role asks the human with (in a live run, only about a one-way door) # ── capabilities ──────────────────────────────────────────────── def capabilities(self): @@ -139,6 +140,7 @@ def describe(self, cwd=None): d[m] = getattr(self, m)() except Unsupported as e: d[m] = {"unsupported": str(e)} + d["question"] = self.QUESTION d["models"] = {g: self.resolve_model(g, cwd) for g in ("planning", "worker")} return d @@ -153,7 +155,9 @@ class Claude(HostAdapter): ISOLATION = "host-native: isolation: \"worktree\" creates .claude/worktrees/agent- at dispatch" INSTALL_GATE = "plugin/hooks/hooks.json, loaded by the plugin install (/plugin install)" ENFORCE_GATE = "native" - TOOL_POLICY = "gate-supervisor-scope · gate-commit-author · budget hook pre (PreToolUse, exit 2 denies)" + TOOL_POLICY = ("gate-supervisor-scope · gate-commit-author · budget hook pre · mission doors hook, the door gate " + "(PreToolUse, exit 2 or a deny denies)") + QUESTION = "AskUserQuestion" class Codex(HostAdapter): @@ -164,7 +168,9 @@ class Codex(HostAdapter): ISOLATION = "aloop task-worktree add ; the worker's first command is `aloop bind ` (host-adapter confines it)" INSTALL_GATE = "codex plugin add aloop@aloop-marketplace, then trust the hooks once in /hooks" ENFORCE_GATE = "native" - TOOL_POLICY = "host-adapter hook: writers denied until bound; then every command/patch must stay under the worktree" + TOOL_POLICY = ("host-adapter hook: writers denied until bound; then every command/patch must stay under the worktree; " + "mission doors hook (the door gate) on every tool call") + QUESTION = "request_user_input (Plan mode, root thread only; otherwise it asks in chat)" class OpenCode(HostAdapter): @@ -175,7 +181,9 @@ class OpenCode(HostAdapter): ISOLATION = "aloop task-worktree add ; `aloop bind `; the plugin rewrites commands/edits into it" INSTALL_GATE = "aloop install opencode writes .opencode/plugins/aloop.js, agents and skills" ENFORCE_GATE = "outer" # the plugin runs the gates around the host's tool calls and sub-agent ends - TOOL_POLICY = "aloop.js: tool.execute.before rewrites or denies; a gate's exit 2 denies the call" + TOOL_POLICY = ("aloop.js: tool.execute.before rewrites or denies; a gate's exit 2 denies the call; " + "mission doors hook (the door gate) runs last") + QUESTION = "question" class Dsh(HostAdapter): @@ -190,7 +198,9 @@ class Dsh(HostAdapter): "`aloop bind `; host-adapter then denies any command or path outside it (dsh cannot rewrite a call)") INSTALL_GATE = "aloop install dsh writes the aloop profile ($DSH_HOME/profiles/aloop): run `dsh --profile aloop`" ENFORCE_GATE = "native" # a block is dsh's own deny / post-execute block / steer before the child settles - TOOL_POLICY = "aloop.mjs: tools/pre-execute denies on a gate's exit 2; a writer is denied until bound, then confined" + TOOL_POLICY = ("aloop.mjs: tools/pre-execute denies on a gate's exit 2; a writer is denied until bound, then confined; " + "mission doors hook (the door gate) runs last") + QUESTION = "ask_user_question (not mounted in the aloop profile; the supervisor's final answer asks)" ADAPTERS = {"claude": Claude, "codex": Codex, "opencode": OpenCode, "dsh": Dsh} diff --git a/plugin/hooks/hooks.json b/plugin/hooks/hooks.json index ac4cc296..e6ed3c4f 100644 --- a/plugin/hooks/hooks.json +++ b/plugin/hooks/hooks.json @@ -32,7 +32,8 @@ { "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}/bin/gate-mission-artifact\" start", "timeout": 30 } ] }, { "matcher": "*", "hooks": [ { "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}/bin/host-adapter\" hook", "timeout": 15 }, - { "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}/bin/transcripts\" stage-in" } ] } + { "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}/bin/transcripts\" stage-in" }, + { "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}/bin/mission\" principles hook", "timeout": 10 } ] } ], "PostToolUse": [ { "matcher": "Write", "hooks": [ diff --git a/plugin/mission/principles.py b/plugin/mission/principles.py index 0a73b61d..cee4d498 100644 --- a/plugin/mission/principles.py +++ b/plugin/mission/principles.py @@ -8,13 +8,17 @@ The `## Core` section is separate: the core principles every aloop agent carries on every turn, the one-way / two-way door rule first (M9.30). They render apart from the goal blocks, so editing -them never stales a goal's stamp, and the SessionStart hook injects them into an aloop project. +them never stales a goal's stamp, and the SessionStart hook injects them into an aloop project; +`principles hook` hands the same block to every dispatched role (M9.31). Usage: mission principles show [--mode autonomous|spoon-fed] [--json] [--file PATH] the block for a mode (default: the mission's own mode); --json adds sha256 and ids mission principles show --core [--file PATH] the core principles block (every agent, every turn) + mission principles hook [--file PATH] + the SubagentStart hook (the payload on stdin): in an aloop project, the core block as the + role's additionalContext; silent outside one. Always exit 0: it never blocks a dispatch mission principles hash [--mode autonomous|spoon-fed] [--file PATH] the sha256 of exactly that block (what a goal's stamp records) mission principles check [--file PATH] @@ -36,6 +40,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "bin")) import _mission as M # noqa: E402 +import _paths # noqa: E402 SOURCE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "principles.md") MODES = ("autonomous", "spoon-fed") @@ -275,8 +280,30 @@ def parse_args(argv): return opts, pos +def hook(cwd_default, path=None): + """SubagentStart (Claude Code and Codex read hooks.json): a dispatched role carries the core block too, + as the main session does from session-start. Raises on any problem; main() keeps it silent.""" + payload = json.loads(sys.stdin.read() or "{}") + if not isinstance(payload, dict): + raise ValueError("the hook payload is not an object") + cwd = payload.get("cwd") or cwd_default + if not isinstance(cwd, str) or not os.path.isdir(cwd) or not os.path.isdir(_paths.docs(cwd)): + return 0 # not an aloop project + if path and not os.path.isfile(path): + raise ValueError("no such file %s" % path) + print(json.dumps({"hookSpecificOutput": {"hookEventName": "SubagentStart", "additionalContext": render_core(path)}})) + return 0 + + def main(argv, cwd): cmd, (o, _) = argv[0], parse_args(argv[1:]) + if cmd == "hook": + try: + return hook(cwd, o.get("file") or None) + except Exception as e: # noqa: BLE001 — context, not a gate: a failure never blocks the dispatch + print("mission principles hook: no core principles for this role (%s: %s)" % (type(e).__name__, e), + file=sys.stderr) + return 0 for k in ("file", "mode"): if k in o and not o[k]: print("mission principles: --%s needs a value" % k, file=sys.stderr) @@ -294,7 +321,7 @@ def main(argv, cwd): % len(load(path)["principles"]), file=sys.stderr) return 2 if bad else 0 if cmd not in ("show", "hash"): - print("mission principles: unknown command %r (show, hash, check)" % cmd, file=sys.stderr) + print("mission principles: unknown command %r (show, hash, check, hook)" % cmd, file=sys.stderr) return 64 if o.get("core"): if cmd != "show" or o.get("mode"): diff --git a/scripts/test/codex-hooks.test.py b/scripts/test/codex-hooks.test.py index 31df8252..d61cae6a 100644 --- a/scripts/test/codex-hooks.test.py +++ b/scripts/test/codex-hooks.test.py @@ -123,10 +123,17 @@ def rollout(thread, rows): why = denied(fire("PreToolUse", dict(base, tool_name="Bash", tool_input={"command": "git commit -m 'x\n\nCo-Authored-By: Claude '"}))) assert why and "co-author" in why.lower(); ok("commit-author blocks a Claude trailer") +# the door gate (M9.30, M9.31): a root push to the default branch is a one-way door, stopped with ASK[door] +why = [w for w in (denied([x]) for x in fire("PreToolUse", dict(base, tool_name="Bash", tool_input={"command": "git push origin main"}))) if w] +assert any(w.startswith("doors:") and "ASK[door]" in w for w in why), why; ok("the door gate denies the root's `git push origin main`") + # worker: SubagentStart registers; unbound writes refused; bind; confinement worker = dict(base, agent_id="th-red", agent_type="red-worker") -fire("SubagentStart", worker) +r = fire("SubagentStart", worker) assert os.path.isfile(os.path.join(T, ".aladdin/state/agents/th-red.json")); ok("SubagentStart registers the worker") +core = subprocess.run([os.path.join(BIN, "mission"), "principles", "show", "--core"], cwd=T, env=ENV, capture_output=True, text=True).stdout +assert core.startswith("Core principles") and core in context(r), context(r) +ok("SubagentStart hands the role the core principles (the door rule first) as additionalContext") assert denied(fire("PreToolUse", dict(worker, tool_name="Bash", tool_input={"command": "ls"}))) ok("unbound worker's shell command is refused") wt = sh(os.path.join(BIN, "task-worktree"), "add", "S1-01-T1") diff --git a/scripts/test/hook-matchers.test.py b/scripts/test/hook-matchers.test.py index 8d2d400f..62ec2dd7 100644 --- a/scripts/test/hook-matchers.test.py +++ b/scripts/test/hook-matchers.test.py @@ -54,4 +54,35 @@ def ok(msg): assert any("gate-" in c for c in stop_gates[r]), (r, stop_gates[r]) ok("every role has a SubagentStop gate when dispatched as aloop:") +# the door rule on every host and role (M9.31): the door gate guards every tool call, and every dispatched +# role starts with the core principles (the main session gets them from session-start) +DOORS = '"${CLAUDE_PLUGIN_ROOT}/bin/mission" doors hook' +PRINCIPLES = '"${CLAUDE_PLUGIN_ROOT}/bin/mission" principles hook' +pre_all = [h for g in hooks["PreToolUse"] if g.get("matcher") == "*" for h in g["hooks"]] +cmds = [h["command"] for h in pre_all] +assert DOORS in cmds, cmds +# listed last, after budget hook pre: the order OpenCode and dsh run it in, since it records door.crossed (uses +# up a once-grant) and must only do so once every other gate admitted the call +assert cmds.index(DOORS) == len(cmds) - 1 and cmds.index('"${CLAUDE_PLUGIN_ROOT}/bin/budget" hook pre') < cmds.index(DOORS), cmds +assert pre_all[cmds.index(DOORS)].get("timeout") == 15, pre_all +ok("PreToolUse *: the door gate (mission doors hook) guards every tool call, listed last") + +start_all = [h for g in hooks["SubagentStart"] if g.get("matcher") == "*" for h in g["hooks"]] +cmds = [h["command"] for h in start_all] +assert PRINCIPLES in cmds and cmds.index(PRINCIPLES) == 2, cmds # the third command of the SubagentStart * group +assert start_all[2].get("timeout") == 10, start_all[2] +for r in ROLES: + got = [h["command"] for g in hooks["SubagentStart"] if matches(g.get("matcher", ""), "aloop:" + r) for h in g["hooks"]] + assert got.count(PRINCIPLES) == 1, (r, got) # once: Claude Code joins every hook's context +ok("SubagentStart *: every role (%d) gets the core principles once (mission principles hook)" % len(ROLES)) + +# parity: the hosts that do not read hooks.json run the door gate from their plugin (expected red until +# M9.31's OpenCode and dsh parts land) +DOOR_CALL = re.compile(r'"mission"\s*,\s*\[\s*"doors"\s*,\s*"hook"\s*\]') +PLUGINS = os.path.join(os.path.dirname(HOOKS), "..", "hosts") +for host, f in (("opencode", "aloop.js"), ("dsh", "aloop.mjs")): + src = open(os.path.join(PLUGINS, host, f)).read() + assert DOOR_CALL.search(src), "%s/%s runs no [\"mission\", [\"doors\", \"hook\"]] check" % (host, f) +ok("OpenCode and dsh plugins run the door gate too (mission doors hook)") + print("\n%d checks passed" % n) diff --git a/scripts/test/host-adapter.test.py b/scripts/test/host-adapter.test.py index 06853967..391885ee 100644 --- a/scripts/test/host-adapter.test.py +++ b/scripts/test/host-adapter.test.py @@ -68,6 +68,18 @@ def run(*args, inp=None, **env): assert run(H, "describe", "--host", "vim").returncode == 64 ok("host describe: capabilities, dispatch vocabulary, boundary, isolation, gates and models per host") +# the door rule on every host (M9.31): each tool policy names the door gate, and each host names the tool a +# role asks the human with (the door gate holds a question to a one-way door the gate stopped) +for h in d: + assert "mission doors hook" in d[h]["tool_policy"], (h, d[h]["tool_policy"]) +assert {h: d[h]["question"].split(" ")[0] for h in d} == {"claude": "AskUserQuestion", "codex": "request_user_input", + "opencode": "question", "dsh": "ask_user_question"}, d +assert "Plan mode" in d["codex"]["question"] and "not mounted" in d["dsh"]["question"], d +assert all(_host.ADAPTERS[h].QUESTION == d[h]["question"] for h in d) +r = run(H, "describe", "--host", "codex") +assert "question" in r.stdout and "mission doors hook" in r.stdout, r.stdout +ok("host describe: every tool policy names the door gate; each host names its ask-the-human tool") + # 3) what a host cannot do is Unsupported, never faked for h in ("claude", "codex", "opencode", "dsh"): assert "unsupported" in d[h]["send"], (h, d[h]["send"]) # no host has live messaging (dsh: unproven) diff --git a/scripts/test/mission-principles.test.py b/scripts/test/mission-principles.test.py index 8ccef8a3..c0339792 100644 --- a/scripts/test/mission-principles.test.py +++ b/scripts/test/mission-principles.test.py @@ -217,11 +217,49 @@ def check_variant(new_text): assert r.returncode == 0 and r.stdout == "", r.stdout # not an aloop project ok("core principles: the door rule first, single-sourced, apart from the goal blocks, checked, injected at session start") +# 9) a dispatched role gets the same core block (M9.31): `principles hook` is the SubagentStart hook on Claude +# Code and Codex (both read hooks.json). Inside an aloop project it prints the block as additionalContext; +# outside one, or on any error, it prints nothing and exits 0 (it never blocks a dispatch) + + +def hook(stdin, *extra, cwd=T): + return subprocess.run([MI, "principles", "hook"] + list(extra), cwd=cwd, env=ENV, input=stdin, + capture_output=True, text=True) + + +start = {"hook_event_name": "SubagentStart", "session_id": "s1", "agent_id": "a1", "agent_type": "aloop:red-worker"} +r = hook(json.dumps(dict(start, cwd=proj))) +assert r.returncode == 0 and r.stderr == "", r.stderr +assert json.loads(r.stdout) == {"hookSpecificOutput": {"hookEventName": "SubagentStart", "additionalContext": core}}, r.stdout +assert len(r.stdout.splitlines()) == 1, r.stdout # one JSON object, nothing else +r = hook(json.dumps(dict(start, cwd=os.path.join(proj, "docs")))) # a subdirectory is the same project +assert r.returncode == 0 and json.loads(r.stdout)["hookSpecificOutput"]["additionalContext"] == core, r.stdout +r = hook(json.dumps(dict(start, cwd=bare))) +assert r.returncode == 0 and r.stdout == "", r.stdout # not an aloop project +for junk in ("not json", "[1, 2]", '{"cwd": 7}', json.dumps({"cwd": os.path.join(T, "gone")}), "\x00\xff"): + r = hook(junk, cwd=bare) + assert r.returncode == 0 and r.stdout == "" and "Traceback" not in r.stderr, (junk, r.stdout, r.stderr) +r = hook("", cwd=proj) # no payload cwd: the hook's own +assert r.returncode == 0 and json.loads(r.stdout)["hookSpecificOutput"]["additionalContext"] == core, r.stdout +check_variant(cases[0][0]) # an invalid principles.md +r = hook(json.dumps(dict(start, cwd=proj)), "--file", draft) +assert r.returncode == 0 and r.stdout == "" and "principles" in r.stderr and "Traceback" not in r.stderr, r.stderr +r = hook(json.dumps(dict(start, cwd=proj)), "--file", os.path.join(T, "missing.md")) +assert r.returncode == 0 and r.stdout == "", r.stdout +check_variant(text.replace(ev, "- evidence: Claim only what a test or a source shows.")) +r = hook(json.dumps(dict(start, cwd=proj)), "--file", draft) # --file: a draft's block, as show +assert r.returncode == 0 and json.loads(r.stdout)["hookSpecificOutput"]["additionalContext"] == \ + run(MI, "principles", "show", "--core", "--file", draft).stdout != core, r.stdout +assert not os.path.exists(os.path.join(proj, ".aladdin")) and not os.path.exists(os.path.join(proj, ".agentic")) +ok("principles hook: the core block as SubagentStart additionalContext in an aloop project; silent and exit 0 otherwise") + # 7) the area is discovered (no shared list edited) and `mission principles --help` is its docstring assert "principles" in run(MI, "--list").stdout.split() r = run(MI, "principles", "--help") assert r.returncode == 0 and "two variants" in r.stdout, r.stdout -assert run(MI, "principles", "nope").returncode == 64 +assert "mission principles hook" in r.stdout and "SubagentStart" in r.stdout, r.stdout +r = run(MI, "principles", "nope") +assert r.returncode == 64 and "hook" in r.stderr, r.stderr shutil.rmtree(T, ignore_errors=True) ok("discovered as an area; --help is the module docstring; an unknown subcommand is a usage error") From 23f8fffee4ec8aaeb77c49eb9b654999955c1f39 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 11:24:40 +0000 Subject: [PATCH 03/14] OpenCode: door rule on every call and role (M9.31) tool.execute.before runs `mission doors hook` as the last PreToolUse check (after budget; it records door.crossed), skips read-only tools, gives it 15 s and fails closed on a timeout, signal, spawn error or an exit other than 0/2. `question` maps to AskUserQuestion. shell.env marks the agent's shell ALOOP_AGENT_SHELL=1. Sub-agents and a resumed root get the core principles block (read once per process), never twice in one call. Tests: door denials, the live-run question rule, ordering and fail-closed via a stub plugin root; the workdir (C2) and authorize-refusal (C3) cases are expected red until the gate side lands. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- plugin/hosts/opencode/aloop.js | 58 ++++++++++--- scripts/test/opencode-plugin.test.mjs | 119 +++++++++++++++++++++++++- 2 files changed, 164 insertions(+), 13 deletions(-) diff --git a/plugin/hosts/opencode/aloop.js b/plugin/hosts/opencode/aloop.js index 2c3aa58e..a9a69a08 100644 --- a/plugin/hosts/opencode/aloop.js +++ b/plugin/hosts/opencode/aloop.js @@ -6,17 +6,22 @@ // // Claude Code hook OpenCode equivalent used here // SessionStart event session.created (root session) → ensure-tools, session-start; -// the context is added to that session's system prompt +// the context is added to that session's system prompt (a resumed root, +// which has none, still gets the core principles) // UserPromptSubmit chat.message on a root session → transcripts prompt, intent-trigger // (its context goes into the next turn's system prompt) // PreToolUse tool.execute.before → host-adapter, gate-supervisor-scope, -// gate-commit-author, budget pre; a deny THROWS (the tool call fails -// with the reason). Bound workers' commands/edits are REWRITTEN into -// their worktree (bash workdir, file paths, patch headers). +// gate-mission-scope, gate-commit-author, budget pre, then mission doors +// hook LAST (it records door.crossed, so only once every other gate admitted +// the call; not for read-only tools; a hang, crash or odd exit denies); a +// deny THROWS (the tool call fails with the reason). Bound workers' +// commands/edits are REWRITTEN into their worktree (bash workdir, file +// paths, patch headers); the gates read bash's workdir as tool_input.workdir. // PostToolUse tool.execute.after → transcripts record, budget post; context is // appended to the tool output; `continue: false` aborts the session // SubagentStart event session.created (child session) → host-adapter, transcripts -// stage-in; gate-tooling on the worker's first tool call (role known); +// stage-in; the core principles go into its system prompt; gate-tooling +// on the worker's first tool call (role known); // a mission role's gate-mission-artifact start on its first chat.message // SubagentStop tool.execute.after of the `task` tool → the role's gate; a block is // sent back to the sub-agent (it keeps working) up to 3 rounds, then @@ -42,9 +47,14 @@ const GATES = { "state-assessor": "gate-mission-artifact", "evaluator": "gate-mission-artifact", "mission-planner": "gate-mission-artifact", "ethics-reviewer": "gate-mission-artifact", } -const TOOL = { bash: "Bash", write: "Write", edit: "Edit", apply_patch: "apply_patch", read: "Read", task: "Agent" } +// OpenCode's `question` is the door gate's question tool (AskUserQuestion) +const TOOL = { bash: "Bash", write: "Write", edit: "Edit", apply_patch: "apply_patch", read: "Read", task: "Agent", question: "AskUserQuestion" } const EDITS = new Set(["write", "edit", "apply_patch"]) +// read-only built-ins (the door gate's QUIET set): they cross no door, so no spawn +const QUIET = new Set(["read", "grep", "glob", "list", "todowrite", "todoread", "webfetch", "websearch"]) const GATE_ROUNDS = 3 +// the door gate's timeout: hooks.json's 15 s; ALOOP_DOORS_TIMEOUT_MS only shortens it (tests) +const DOORS_MS = ((t) => (t > 0 && t < 15000 ? t : 15000))(Number(process.env.ALOOP_DOORS_TIMEOUT_MS)) export const AgenticAgile = async ({ client, directory, worktree }) => { const project = worktree || directory @@ -66,12 +76,12 @@ export const AgenticAgile = async ({ client, directory, worktree }) => { const hostDir = join(homeDir, "host", "opencode") const safe = (s) => String(s || "").replace(/[^A-Za-z0-9_.-]/g, "_") - const run = (script, args, payload, cwd = project) => { - const r = spawnSync(join(BIN, script), args, { input: JSON.stringify(payload || {}), cwd, env, encoding: "utf8", timeout: 600000 }) + const run = (script, args, payload, cwd = project, timeout = 600000) => { + const r = spawnSync(join(BIN, script), args, { input: JSON.stringify(payload || {}), cwd, env, encoding: "utf8", timeout }) let json = null const last = (r.stdout || "").trim().split("\n").filter((l) => l.startsWith("{")).pop() try { if (last) json = JSON.parse(last) } catch {} - return { code: r.status ?? 1, stdout: r.stdout || "", stderr: (r.stderr || "").trim(), json } + return { code: r.status ?? 1, stdout: r.stdout || "", stderr: (r.stderr || "").trim(), json, error: r.error, signal: r.signal } } const denyOf = (res) => { const h = res.json?.hookSpecificOutput @@ -79,6 +89,24 @@ export const AgenticAgile = async ({ client, directory, worktree }) => { if (res.code === 2) return res.stderr || "blocked" return null } + // The door gate fails closed: a gate that cannot start, hangs, is killed or exits other than 0/2 has not + // admitted the call (a host that read that as "no objection" would let a one-way door through) + const doorsDenyOf = (res) => { + const how = res.error?.code === "ETIMEDOUT" ? `timed out after ${DOORS_MS / 1000} s` + : res.error ? `could not start (${res.error.code || res.error.message})` + : res.signal ? `was killed (${res.signal})` : res.code !== 0 && res.code !== 2 ? `exited ${res.code}` : "" + if (how) return `BLOCK[doors]: the door gate (aloop mission doors hook) ${how}, so the call is denied (it fails closed)${res.stderr ? `: ${res.stderr}` : ""}` + return denyOf(res) + } + let core = null // the core principles block (mission principles show --core), read once + const coreBlock = () => { + if (core === null) { + const r = run("mission", ["principles", "show", "--core"], {}, project, 15000) + if (r.code !== 0 || !r.stdout.trim()) return "" // retried next turn + core = r.stdout.trim() + } + return core + } async function info(id) { if (sessions.has(id)) return sessions.get(id) @@ -145,6 +173,7 @@ export const AgenticAgile = async ({ client, directory, worktree }) => { "shell.env": async (_input, output) => { output.env.PATH = `${BIN}:${output.env.PATH || process.env.PATH || ""}` output.env.AGENTIC_HOST = "opencode" + output.env.ALOOP_AGENT_SHELL = "1" // an agent's shell: `mission doors authorize|revoke` refuses it }, "chat.message": async (input, output) => { @@ -175,6 +204,10 @@ export const AgenticAgile = async ({ client, directory, worktree }) => { const s = await info(input.sessionID) const ctx = startCtx.get(s.root) if (ctx && !s.parentID) output.system.push(ctx) + // the core principles reach every agent: a sub-agent, and a resumed root (no SessionStart context + // in this process); a root's SessionStart context already carries them, so never twice + const cb = active() ? coreBlock() : "" + if (cb && !output.system.some((x) => x.includes(cb.split("\n")[0]))) output.system.push(cb) const trig = promptCtx.get(s.root) if (trig && !s.parentID) { output.system.push(trig); promptCtx.delete(s.root) } }, @@ -209,8 +242,13 @@ export const AgenticAgile = async ({ client, directory, worktree }) => { if (EDITS.has(input.tool) || input.tool === "bash") checks.push(["gate-mission-scope", []]) // role path guards if (input.tool === "bash") checks.push(["gate-commit-author", []]) checks.push(["budget", ["hook", "pre"]]) + // the door rule, LAST: it records door.crossed (a once-grant is used up), so only for a call every + // other gate admitted. Payload cwd stays the project; a bound worker's directory is tool_input.workdir + if (!QUIET.has(input.tool)) checks.push(["mission", ["doors", "hook"]]) for (const [script, a] of checks) { - const why = denyOf(run(script, a, p)) + const doors = script === "mission" + const res = run(script, a, p, project, doors ? DOORS_MS : undefined) + const why = doors ? doorsDenyOf(res) : denyOf(res) if (why) throw new Error(why) } }, diff --git a/scripts/test/opencode-plugin.test.mjs b/scripts/test/opencode-plugin.test.mjs index d4576f54..d018a1dd 100644 --- a/scripts/test/opencode-plugin.test.mjs +++ b/scripts/test/opencode-plugin.test.mjs @@ -1,7 +1,7 @@ // End-to-end test of the OpenCode plugin against a mock OpenCode client. // Runs the REAL generated plugin (aloop install opencode) and the REAL bin/ scripts in a // throw-away git repo. No OpenCode, no network, no tokens. node scripts/test/opencode-plugin.test.mjs -import { execFileSync } from "node:child_process" +import { execFileSync, spawnSync } from "node:child_process" import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { join } from "node:path" @@ -20,6 +20,7 @@ process.env.HOME = join(T, "home") process.env.AGENTIC_NO_AUTOINSTALL = "1" process.env.CLAUDE_PLUGIN_DATA = join(T, "pdata") process.env.AGENTIC_NO_AUTOINSTALL = "1" // no background cargo build into the temp dir +delete process.env.AGENTIC_AUTONOMY_MODE // a live run is armed on its own plugin instance below mkdirSync(join(process.env.CLAUDE_PLUGIN_DATA, "bin"), { recursive: true }) for (const t of ["md-db", "ctx-symbols"]) { const f = join(process.env.CLAUDE_PLUGIN_DATA, "bin", t) @@ -51,17 +52,34 @@ const hooks = await AgenticAgile({ client, directory: T, worktree: T, project: { let pass = 0 const ok = (name) => { pass++; console.log("ok -", name) } const throws = async (fn, re, name) => { await assert.rejects(fn, re); ok(name) } +// expected red until another part of M9.31 lands (the door gate's side): reported, not counted, never fails the run +let todo = 0 +const pending = async (name, until, fn) => { + try { await fn(); console.log(`ok - ${name} # TODO passes now: make it a hard check`) } + catch { todo++; console.log(`not ok - ${name} # TODO expected red until ${until}`) } +} +const CORE = "Core principles (every aloop agent):" // the core block's head (mission principles show --core) +const count = (s, x) => s.split(x).length - 1 +const systemOf = async (h, sessionID) => { + const out = { system: [] } + await h["experimental.chat.system.transform"]({ sessionID, model: {} }, out) + return out.system.join("\n") +} // SessionStart: NEXT.md reaches the root session's system prompt await hooks.event({ event: { type: "session.created", properties: { info: { id: "root" } } } }) const sys = { system: [] } await hooks["experimental.chat.system.transform"]({ sessionID: "root", model: {} }, sys) assert.ok(sys.system.join("\n").includes("NEXT-MARKER")); ok("SessionStart handoff injected into system prompt") +assert.equal(count(sys.system.join("\n"), CORE), 1, sys.system.join("\n")); ok("the root gets the core principles exactly once") +// a resumed root session (no session.created in this process, so no SessionStart context) still carries them +assert.equal(count(await systemOf(hooks, "resumed"), CORE), 1); ok("a resumed root session gets the core principles once") -// shell.env puts bin/ on PATH +// shell.env puts bin/ on PATH and marks the shell as an agent's (`mission doors authorize` refuses it) const envOut = { env: { PATH: "/usr/bin" } } await hooks["shell.env"]({ cwd: T }, envOut) assert.ok(envOut.env.PATH.startsWith(BIN)); ok("shell.env prepends plugin bin/") +assert.equal(envOut.env.ALOOP_AGENT_SHELL, "1"); ok("shell.env exports ALOOP_AGENT_SHELL=1 into the agent's shell") // UserPromptSubmit capture await hooks["chat.message"]({ sessionID: "root" }, { message: {}, parts: [{ type: "text", text: "build X" }] }) @@ -76,9 +94,25 @@ await throws(() => hooks["tool.execute.before"]({ tool: "write", sessionID: "roo await throws(() => hooks["tool.execute.before"]({ tool: "bash", sessionID: "root", callID: "3" }, { args: { command: 'git commit -m "x\n\nCo-Authored-By: Claude "' } }), /co-author/i, "gate-commit-author blocks Claude trailer") +// the door rule (M9.30, M9.31): mission doors hook is a PreToolUse gate on OpenCode too +await throws(() => hooks["tool.execute.before"]({ tool: "bash", sessionID: "root", callID: "d1" }, { args: { command: "git push --force origin main" } }), + /ASK\[door\] push-default-branch/, "doors: a force-push to the default branch stops at ASK[door]") +await throws(() => hooks["tool.execute.before"]({ tool: "bash", sessionID: "root", callID: "d2" }, + { args: { command: "aloop mission doors authorize push-default-branch --by x --reason r" } }), + /BLOCK\[door\] forge-human-record/, "doors: an agent authorizing a door is a wall (forge-human-record)") +await throws(() => hooks["tool.execute.before"]({ tool: "bash", sessionID: "root", callID: "d3" }, { args: { command: "git reset --hard" } }), + /ASK\[door\] discard-local-work/, "doors: discarding work in the main checkout stops at ASK[door]") +await hooks["tool.execute.before"]({ tool: "question", sessionID: "root", callID: "d4" }, + { args: { questions: [{ question: "Which name?", header: "Name", options: [{ label: "a", description: "a" }] }] } }) +ok("doors: outside a live run a question is asked") + // worker isolation: must bind first, then commands/edits are confined to its worktree await hooks.event({ event: { type: "session.created", properties: { info: { id: "child", parentID: "root" } } } }) await hooks["chat.message"]({ sessionID: "child", agent: "red-worker" }, { message: {}, parts: [{ type: "text", text: "task" }] }) +// SubagentStart: the sub-agent carries the core principles too (once), never the root's handoff +const childSys = await systemOf(hooks, "child") +assert.ok(count(childSys, CORE) === 1 && !childSys.includes("NEXT-MARKER"), childSys); ok("a sub-agent's system prompt gets the core principles once") +assert.equal(count(await systemOf(hooks, "root"), CORE), 1); ok("the root still gets them exactly once") await throws(() => hooks["tool.execute.before"]({ tool: "bash", sessionID: "child", callID: "4" }, { args: { command: "ls" } }), /bind your worktree first/, "unbound worker is refused") const wt = sh(join(BIN, "task-worktree"), ["add", "S1-01-T1"]).trim() @@ -93,6 +127,10 @@ assert.equal(writeArgs.filePath, join(wt, "tests/a.rs")); ok("bound worker's rel const patchArgs = { patchText: `*** Begin Patch\n*** Add File: ${join(T, "src/lib.rs")}\n+x\n*** End Patch` } await hooks["tool.execute.before"]({ tool: "apply_patch", sessionID: "child", callID: "8" }, { args: patchArgs }) assert.ok(patchArgs.patchText.includes(`*** Add File: ${join(wt, "src/lib.rs")}`)); ok("patch paths rewritten into the worktree") +// the door gate reads a bound worker's command where it runs (tool_input.workdir, its worktree): a reset +// there is two-way, the same reset in the main checkout (above) is not +await pending("doors: a bound worker's reset in its own worktree is two-way", "the gate reads tool_input.workdir (C2)", + () => hooks["tool.execute.before"]({ tool: "bash", sessionID: "child", callID: "d5" }, { args: { command: "git reset --hard" } })) // token ledger + time box: usage recorded per assistant message; soft token telemetry then hard stop const usage = (id, input, output) => ({ type: "message.updated", properties: { info: { @@ -170,4 +208,79 @@ await hooks.event({ event: { type: "session.idle", properties: { sessionID: "roo assert.ok(calls.toast.length === 1 && /Sprint 1 closed/.test(calls.prompt.at(-1).body.parts[0].text)); ok("sprint close announced on idle") assert.ok(existsSync(join(T, ".aladdin/ledger/root.jsonl"))); ok("token ledger written for the session") -console.log(`\n${pass} checks passed`) +// a live run (an autonomous run, read at plugin load): a two-way question is decided, not asked; a +// question at a door the gate stopped in this session (the force-push above) is asked +process.env.AGENTIC_AUTONOMY_MODE = "autonomous" +const live = await AgenticAgile({ client, directory: T, worktree: T, project: {}, $: null }) +delete process.env.AGENTIC_AUTONOMY_MODE +const ask = (text) => ({ args: { questions: [{ question: text, header: "Q", options: [{ label: "yes", description: "recommended" }] }] } }) +await throws(() => live["tool.execute.before"]({ tool: "question", sessionID: "root", callID: "q1" }, ask("Which name should the module get?")), + /two-way question/, "doors: in a live run an untagged question is refused") +await live["tool.execute.before"]({ tool: "question", sessionID: "root", callID: "q2" }, + ask("[door:push-default-branch] Push the fix to main? Recommended: no, a draft PR.")) +ok("doors: in a live run a question at a door the gate stopped is asked") + +// the door gate runs LAST (it records door.crossed, which uses up a once-grant) and fails closed. A stub +// plugin root: each gate logs its call and does what S/mode. says (default: admit) +const S = mkdtempSync(join(tmpdir(), "agentic-oc-stub-")) +process.on("exit", () => rmSync(S, { recursive: true, force: true })) +mkdirSync(join(S, "plugin/bin"), { recursive: true }) +for (const g of ["host-adapter", "gate-tooling", "gate-supervisor-scope", "gate-mission-scope", "gate-commit-author", "budget", "mission"]) { + writeFileSync(join(S, "plugin/bin", g), `#!/bin/sh +cat >/dev/null +S="$(dirname "$0")/../.." +echo "$(basename "$0") $*" >> "$S/calls" +case "$(cat "$S/mode.$(basename "$0")" 2>/dev/null)" in + hang) exec sleep 30 ;; + crash) kill -KILL $$ ;; + exit1) exit 1 ;; + deny) echo "BLOCK[stub] no" >&2; exit 2 ;; +esac +exit 0 +`) + chmodSync(join(S, "plugin/bin", g), 0o755) +} +writeFileSync(join(S, "aloop.mjs"), readFileSync(join(ROOT, "hosts/opencode/aloop.js"), "utf8").replaceAll("__AGENTIC_ROOT__", join(S, "plugin"))) +process.env.ALOOP_DOORS_TIMEOUT_MS = "500" +const stub = await (await import(pathToFileURL(join(S, "aloop.mjs")).href)).AgenticAgile({ client, directory: T, worktree: T, project: {}, $: null }) +delete process.env.ALOOP_DOORS_TIMEOUT_MS +const mode = (g, m) => writeFileSync(join(S, "mode." + g), m) +const gated = async (tool, args) => { + writeFileSync(join(S, "calls"), "") + let err = null + try { await stub["tool.execute.before"]({ tool, sessionID: "root", callID: "s" }, { args }) } catch (e) { err = e } + return { err, calls: readFileSync(join(S, "calls"), "utf8").split("\n").filter(Boolean) } +} +let g = await gated("bash", { command: "ls" }) +assert.ok(!g.err && g.calls.at(-1) === "mission doors hook" && g.calls.at(-2) === "budget hook pre", g.calls.join(" | ")) +ok("doors: the door gate runs last, after budget") +g = await gated("read", { filePath: join(T, "README.md") }) +assert.ok(!g.err && g.calls.length && !g.calls.some((c) => c.startsWith("mission")), g.calls.join(" | ")) +ok("doors: a read-only tool never spawns the door gate") +mode("budget", "deny") +g = await gated("bash", { command: "ls" }) +assert.ok(/BLOCK\[stub\]/.test(g.err?.message) && !g.calls.some((c) => c.startsWith("mission")), g.calls.join(" | ")) +ok("doors: a call another gate denied never reaches the door gate") +mode("budget", "") +for (const [m, what] of [["hang", "hangs (past its timeout)"], ["crash", "is killed by a signal"], ["exit1", "exits 1"]]) { + mode("mission", m) + const t0 = Date.now() + g = await gated("bash", { command: "ls" }) + assert.ok(/door gate/.test(g.err?.message) && Date.now() - t0 < 10000, `${m}: ${g.err?.message}`) + ok(`doors: a door gate that ${what} denies the call (fails closed)`) +} +rmSync(join(S, "plugin/bin/mission")) +g = await gated("bash", { command: "ls" }) +assert.ok(/door gate/.test(g.err?.message), g.err?.message); ok("doors: a door gate that cannot start denies the call (fails closed)") + +// end to end, last (a red run may record an authorization): the agent's shell, marked only by shell.env, +// cannot authorize a door +const agentEnv = { ...process.env, ALOOP_AGENT_SHELL: envOut.env.ALOOP_AGENT_SHELL, AGENTIC_HOST: envOut.env.AGENTIC_HOST } +for (const k of ["CLAUDECODE", "DSH_SHELL", "CODEX_THREAD_ID"]) delete agentEnv[k] +await pending("doors: `mission doors authorize` refuses the agent's OpenCode shell", "authorize reads ALOOP_AGENT_SHELL (C3)", () => { + const a = spawnSync(join(BIN, "aloop"), ["mission", "doors", "authorize", "publish-release", "--by", "x", "--reason", "r"], + { cwd: T, env: agentEnv, encoding: "utf8" }) + assert.ok(a.status === 2 && /agent session/.test(a.stderr), a.stderr) +}) + +console.log(`\n${pass} checks passed${todo ? `, ${todo} expected red (TODO)` : ""}`) From 648c87a36458545039f424c4da4a5b2812661bd0 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 11:21:11 +0000 Subject: [PATCH 04/14] dsh: the door rule on every tool and role (M9.31) The dsh plugin runs `mission doors hook` last in PreToolUse, after budget, because it records the crossing (a once-authorization is used up). Read-only built-ins skip the spawn. A 15 s timeout, a kill, a crash, a spawn error or any exit other than 0/2 denies, so the gate fails closed. ask_user_question maps to AskUserQuestion and copies only its declared fields. An MCP tool's tool_input is its arguments object, as on Claude Code, so a door that matches an input field (push_files to main) now hits. A nested `arguments` key carries the whole object, so it cannot hide a field. `aloop install dsh` adds the core principles block (`mission principles show --core`) to the end of every role's persona, once. Children get no SessionStart context, and the text is the same on every render. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- plugin/bin/install | 11 ++++- plugin/hosts/dsh/aloop.mjs | 72 ++++++++++++++++++++++------- scripts/test/dsh-install.test.py | 16 +++++++ scripts/test/dsh-plugin.test.mjs | 79 ++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+), 18 deletions(-) diff --git a/plugin/bin/install b/plugin/bin/install index ec67ab68..2db96f0a 100755 --- a/plugin/bin/install +++ b/plugin/bin/install @@ -31,6 +31,7 @@ dsh: $DSH_HOME/profiles/aloop/ (DSH_HOME defaults to ~/.dsh): the `aloop` p skills go to /.dsh/skills (or $DSH_HOME/skills with --global). + ~/.local/bin/aloop. Run: dsh --profile aloop """ +import importlib.util import json import os import re @@ -340,8 +341,16 @@ def dsh_patch(st, owner): " name: '@deepseek-ai/dsh-agent-preset'", " config: {id: aloop-supervisor, name: aloop supervisor, description: %s, plugins: []}" % yq( "Drives the aloop loop and dispatches the aloop roles.")] + # the core principles block, as `mission principles show --core` prints it: a child gets no SessionStart + # context, so it closes each role's persona, the same text every render (a cacheable prompt prefix) + spec = importlib.util.spec_from_file_location("aloop_principles", os.path.join(ROOT, "mission", "principles.py")) + principles = importlib.util.module_from_spec(spec) + spec.loader.exec_module(principles) + core = principles.render_core() + if "{{" in core: + raise ValueError("mission/principles.md's core block contains '{{', which dsh's persona prefix would interpolate") for name, desc, body in roles: - prefix = "You are aloop's `%s` role.\n\n%s" % (name, host_body(body, "dsh")) + prefix = "You are aloop's `%s` role.\n\n%s\n%s" % (name, host_body(body, "dsh"), core) if "{{" in prefix: # dsh-persona interpolates {{var}} strictly: an unknown var fails the role raise ValueError("agents/%s.md contains '{{', which dsh's persona prefix would interpolate" % name) out += [" - id: preset-aloop-%s" % name, diff --git a/plugin/hosts/dsh/aloop.mjs b/plugin/hosts/dsh/aloop.mjs index 86f42689..3a06f3dc 100644 --- a/plugin/hosts/dsh/aloop.mjs +++ b/plugin/hosts/dsh/aloop.mjs @@ -10,8 +10,11 @@ // UserPromptSubmit agent/pre-step of a root agent with a user message → transcripts prompt, // intent-trigger; exit 2 rejects the step, context joins it // PreToolUse tools/pre-execute → host-adapter, gate-supervisor-scope, gate-mission-scope, -// gate-commit-author, gate-dispatch, budget pre; a deny hands the reason to the model. dsh cannot -// rewrite a call, so a bound worker is confined by denial (host-adapter) +// gate-commit-author, gate-dispatch, budget pre, then mission doors hook LAST: it records +// the crossing (uses up a once-authorization), so only once every other gate admitted the +// call. A read-only tool skips it; a hang (15 s), a kill, a crash or an exit but 0/2 denies +// (fail closed). A deny hands the reason to the model. dsh cannot rewrite a call, so a +// bound worker is confined by denial (host-adapter) // PostToolUse tools/post-execute → gate-validate-artifact, transcripts record, budget post; // exit 2 turns the result into the gate's feedback, context joins the next // request, `continue: false` cancels the agent @@ -51,7 +54,11 @@ const GATES = { } const MISSION = new Set(Object.keys(GATES).filter((r) => GATES[r] === "gate-mission-artifact")) const DISPATCH = new Set(["subagent", "subagent_planning"]) -const TOOL = { bash: "Bash", write: "Write", edit: "Edit", read: "Read", glob: "Glob", grep: "Grep", subagent: "Agent", subagent_planning: "Agent" } +const TOOL = { bash: "Bash", write: "Write", edit: "Edit", read: "Read", glob: "Glob", grep: "Grep", subagent: "Agent", subagent_planning: "Agent", + ask_user_question: "AskUserQuestion" } +// the built-ins the door gate passes unread (doors.py QUIET: Read, Glob, Grep, TodoWrite, WebFetch, WebSearch): no spawn +const READ_ONLY = new Set(["read", "read_image", "glob", "grep", "todo_write", "web_fetch", "web_search"]) +const DOORS_MS = 15000 // the door gate's timeout, as in hooks/hooks.json const STUCK = /\bSTUCK\b.*NOT verified/ const TOOLS_NOTE = "aloop tools: call `aloop ` (e.g. `aloop selfcheck`, `aloop stats show`); never search for them." @@ -163,19 +170,24 @@ export function apply(ctx, config = {}) { if (s.child) { p.agent_id = s.id; p.agent_type = s.role; p.agent_transcript_path = transcriptOf(s) } return p } + const UNREAD = Object.freeze({}) // arguments that are not JSON: read as none const argsOf = (exec) => { if (typeof exec.arguments !== "string") return exec.arguments || {} - try { return JSON.parse(exec.arguments) } catch { return {} } + try { return JSON.parse(exec.arguments) } catch { return UNREAD } } // Claude-shaped tool_input, built from each tool's declared arguments only: dsh accepts extra // keys, and a gate must never read a field the model made up (an `agent_type` beside a - // file_path). Any other tool's arguments travel as one JSON string. `workdir` is the directory - // dsh really runs in (bash's own `workdir`, else the agent's). Relative paths are joined the - // way dsh joins them (`/`, `..` kept), never normalised here: like dsh, - // host-adapter follows a symlink before `..`. + // file_path). An MCP tool's arguments object is its input, as on Claude Code, so a door that + // names an input field (push_files to the default branch) matches; a key of its own named + // `arguments` carries the whole object, so a gate reading the older shape sees the same fields. + // Any other tool's arguments (and MCP arguments that are no object) travel as one JSON string. + // `workdir` is the directory dsh really runs in (bash's own `workdir`, else the agent's). + // Relative paths are joined the way dsh joins them (`/`, `..` kept), never + // normalised here: like dsh, host-adapter follows a symlink before `..`. function toolInput(tool, a, wd) { const str = (v) => (typeof v === "string" ? v : undefined) const at = (p) => (p === undefined || isAbsolute(p) ? p : `${wd}${sep}${p}`) + const obj = (v) => !!v && typeof v === "object" && !Array.isArray(v) && v !== UNREAD if (DISPATCH.has(tool)) return { subagent_type: String(a.agent_preset || "").replace(/^aloop-/, ""), description: str(a.description), prompt: str(a.prompt) || "", cwd: str(a.cwd), model: MODELS[tool] || "" } switch (tool) { @@ -187,36 +199,49 @@ export function apply(ctx, config = {}) { case "read": return { file_path: at(str(a.file_path)), offset: a.offset, limit: a.limit, workdir: wd } case "read_image": return { file_path: at(str(a.file_path)), workdir: wd } case "glob": case "grep": return { pattern: str(a.pattern), path: at(str(a.path)), include: str(a.include), workdir: wd } - default: return { arguments: JSON.stringify(a), workdir: wd } + // what the human is shown; no workdir: a directory named `[door:]` is no tag + case "ask_user_question": return { questions: (Array.isArray(a.questions) ? a.questions : []).map((q) => ({ + id: str(q?.id), question: str(q?.question), header: str(q?.header), + options: Array.isArray(q?.options) ? q.options.map((o) => ({ label: str(o?.label), description: str(o?.description) })) : undefined, + multi_select: typeof q?.multi_select === "boolean" ? q.multi_select : undefined })) } + default: + if (tool.startsWith("mcp__") && obj(a)) + return { ...a, ...(Object.hasOwn(a, "arguments") ? { arguments: JSON.stringify(a) } : {}), workdir: wd } + return { arguments: JSON.stringify(a), workdir: wd } } } // Run one of this plugin's bin/ scripts with the hook JSON on stdin. Async: one Node process // drives every agent, so a synchronous wait would stall all of them. - function run(script, args, input, cwd) { + function run(script, args, input, cwd, ms = 600000) { return new Promise((done) => { let out = "", err = "", settled = false, timer = null - const finish = (code, error) => { + const finish = (code, error, extra = {}) => { if (settled) return settled = true clearTimeout(timer) let json = null const last = out.trim().split("\n").filter((l) => l.startsWith("{")).pop() try { if (last) json = JSON.parse(last) } catch {} - done({ code, stdout: out, stderr: err.trim(), json, error }) + done({ code, stdout: out, stderr: err.trim(), json, error, ...extra }) } let child try { child = spawn(join(BIN, script), args, { cwd, env: gateEnv(ROOT, FROM_FILE) }) } catch (e) { return finish(1, e) } - // a hung script is killed after 10 minutes (as on OpenCode); spawn's own `timeout` would keep a - // timer alive for a script that never started - timer = setTimeout(() => { try { child.kill("SIGKILL") } catch {} }, 600000) + // a hung script is killed after `ms` (10 minutes, as on OpenCode) and answers at once: a child it + // left holding the pipes does not keep the call waiting. spawn's own `timeout` would keep a timer + // alive for a script that never started + timer = setTimeout(() => { + try { child.kill("SIGKILL") } catch {} + child.stdout.destroy(); child.stderr.destroy() + finish(1, undefined, { timedOut: true }) + }, ms) timer.unref?.() child.stdout.on("data", (d) => (out += d)) child.stderr.on("data", (d) => (err += d)) child.on("error", (e) => finish(1, e)) - child.on("close", (code) => finish(code ?? 1)) + child.on("close", (code, signal) => finish(code ?? 1, undefined, { signal })) child.stdin.on("error", () => {}) child.stdin.end(JSON.stringify(input || {})) }) @@ -230,6 +255,18 @@ export function apply(ctx, config = {}) { return null } const gate = async (script, args, input, cwd) => denyOf(await run(script, args, input, cwd), script) + // The door gate fails closed: it decides with exit 0 (a deny is its JSON) or 2 (it could not + // decide); a hang, a kill, a crash or any other exit denies, never a silent pass. + const DOORS = Number.isInteger(config.doorsTimeoutMs) ? config.doorsTimeoutMs : DOORS_MS // a test knob + const doorGate = async (script, args, input, cwd) => { + const res = await run(script, args, input, cwd, DOORS) + const why = res.timedOut ? `did not answer within ${DOORS / 1000} s` + : res.error ? `could not run (${res.error.code || res.error.message})` + : res.signal ? `was killed (${res.signal})` + : res.code !== 0 && res.code !== 2 ? `exited ${res.code}${res.stderr ? `: ${res.stderr.slice(-300)}` : ""}` : "" + return why ? `aloop: the door gate (mission doors hook) ${why}, so the call is refused (fail closed); \`aloop mission doors check\` says what is wrong` + : denyOf(res, script) + } async function preTool(exec) { const s = info(exec.agent) @@ -253,7 +290,8 @@ export function apply(ctx, config = {}) { if (tool === "bash") checks.push(["gate-commit-author", []]) if (DISPATCH.has(tool)) checks.push(["gate-dispatch", []]) checks.push(["budget", ["hook", "pre"]]) - for (const [script, args] of checks) if ((why = await gate(script, args, p, s.project))) return why + if (!READ_ONLY.has(tool)) checks.push(["mission", ["doors", "hook"], doorGate]) // last: it records the crossing + for (const [script, args, judge = gate] of checks) if ((why = await judge(script, args, p, s.project))) return why return null } diff --git a/scripts/test/dsh-install.test.py b/scripts/test/dsh-install.test.py index 41b6a56f..b5a37d48 100755 --- a/scripts/test/dsh-install.test.py +++ b/scripts/test/dsh-install.test.py @@ -7,6 +7,7 @@ import importlib.machinery import importlib.util import atexit +import json import os import re import shutil @@ -56,6 +57,21 @@ def project(name): assert "dsh" in run(os.path.join(BIN, "install"), "status", cwd=A) ok("the profile records the project it was rendered for, with that project's models") +core = subprocess.run([os.path.join(BIN, "mission"), "principles", "show", "--core"], cwd=A, env=ENV, capture_output=True, + text=True, check=True).stdout +head = core.split("\n")[0] +personas = dict(re.findall(r"^ - id: preset-aloop-([\w-]+)\n(?: .*\n)*? config: \{prefix: (\".*\")\}$", profile(), re.M)) +roles = [os.path.basename(f)[:-3] for f in os.listdir(os.path.join(ROOT, "plugin", "agents")) if f.endswith(".md")] +assert head.startswith("Core principles") and sorted(personas) == sorted(roles), (head, sorted(personas), sorted(roles)) +for role, quoted in personas.items(): + prefix = json.loads(quoted) + assert prefix.count(head) == 1 and prefix.endswith(core) and "{{" not in prefix, (role, prefix[-900:]) + assert prefix.index("You are aloop's `%s` role." % role) == 0 and prefix.index("## On DeepSeek Harness") < prefix.index(head), role +before = profile() +run(os.path.join(BIN, "aloop"), "install", "dsh", "--project", A, cwd=A) +assert profile() == before, "a re-render changed the profile (the persona prefixes must stay cache-stable)" +ok("every role's persona carries the core principles block once, after its body, exactly as `mission principles show --core` prints it") + before = profile() run(os.path.join(BIN, "aloop-init"), "--apply", "MODEL_WORKER=strongest", cwd=B) assert profile() == before, "another project's aloop-init re-rendered A's profile" diff --git a/scripts/test/dsh-plugin.test.mjs b/scripts/test/dsh-plugin.test.mjs index d3d55026..b3d485ea 100644 --- a/scripts/test/dsh-plugin.test.mjs +++ b/scripts/test/dsh-plugin.test.mjs @@ -104,6 +104,30 @@ ok("arguments the model adds (agent_type, a nested file_path) never reach a gate assert.ok(denied(await pre(h, root, "bash", { command: 'git commit -m "x\n\nCo-Authored-By: Claude "' }), /co-author/i)) ok("gate-commit-author blocks a Claude trailer") +// the door rule (M9.30): `mission doors hook` runs last on every tool that can cross a door +assert.ok(denied(await pre(h, root, "bash", { command: "gh pr merge 12 --squash" }), /ASK\[door\] push-default-branch/)) +assert.ok(denied(await pre(h, root, "bash", { command: "git push origin main" }), /ASK\[door\] push-default-branch/)) +assert.ok(allowed(await pre(h, root, "bash", { command: "git push origin s1-01-x" }))) +ok("a one-way door (a merge, a push to the default branch) stops at ASK[door]; a push to a branch is two-way") +assert.ok(denied(await pre(h, root, "bash", { command: "aloop mission doors authorize push-default-branch --by me --reason r" }), /BLOCK\[door\] forge-human-record/)) +ok("an agent authorizing a door itself hits a wall (BLOCK[door] forge-human-record)") +const pushFiles = { owner: "o", repo: "r", branch: "main", files: [{ path: "a.txt", content: "x" }], message: "m" } +assert.ok(denied(await pre(h, root, "mcp__github__push_files", pushFiles), /ASK\[door\] push-default-branch/)) +assert.ok(denied(await pre(h, root, "mcp__github__push_files", JSON.stringify(pushFiles)), /ASK\[door\] push-default-branch/)) +assert.ok(denied(await pre(h, root, "mcp__github__push_files", { ...pushFiles, arguments: JSON.stringify({ ...pushFiles, branch: "x" }) }), /ASK\[door\] push-default-branch/)) +assert.ok(allowed(await pre(h, root, "mcp__github__push_files", { ...pushFiles, branch: "s1-01-x" }))) +ok("an MCP tool's arguments reach the door gate as its input: push_files to the default branch is asked (a nested `arguments` key hides nothing), to a branch not") +// a live run: a question to the human must name a one-way door the gate stopped ([door:]) +process.env.AGENTIC_AUTONOMY_MODE = "autonomous" +const ask = (question, extra = {}) => ({ questions: [{ id: "q1", question, header: "Merge", multi_select: false, + options: [{ label: "Wait (Recommended)", description: "keep the draft" }, { label: "Merge" }] }], ...extra }) +assert.ok(denied(await pre(h, root, "ask_user_question", ask("Which name do you prefer?")), /two-way question/)) +assert.ok(denied(await pre(h, root, "ask_user_question", ask("Which name?", { note: "[door:push-default-branch]" })), /two-way question/)) +assert.ok(allowed(await pre(h, root, "ask_user_question", ask("[door:push-default-branch] Merge PR 12 into main?")))) +delete process.env.AGENTIC_AUTONOMY_MODE +assert.ok(allowed(await pre(h, root, "ask_user_question", ask("Which name do you prefer?")))) +ok("ask_user_question is the question tool: in a live run an untagged one is denied (a tag only in an undeclared field counts for nothing); a door the gate stopped may be asked") + // PostToolUse: gate-validate-artifact's block becomes the call's result writeFileSync(join(story, "init.md"), "no frontmatter\n") const v = await post(h, root, "write", { file_path: join(story, "init.md"), content: "no frontmatter\n" }) @@ -242,6 +266,61 @@ const broken = host({ root: join(T, "no-such-plugin") }) plugin.apply(broken.ctx, broken.config) assert.ok(denied(await pre(broken, agentOf("root2"), "bash", { command: "ls" }), /could not run host-adapter/)); ok("a gate that cannot run denies (fail closed)") +// the door gate runs last (it records a crossing, using up a once-authorization), never for a +// read-only tool, and fails closed: a hang, a kill, a crash or an exit but 0/2 denies +const FD = join(T, "fake-doors") +mkdirSync(join(FD, "bin"), { recursive: true }) +const CALLS = join(FD, "calls.log"), MODE = join(FD, "mode") +for (const g of ["host-adapter", "gate-supervisor-scope", "gate-mission-scope", "gate-commit-author", "gate-dispatch", "budget"]) { + writeFileSync(join(FD, "bin", g), `#!/bin/sh\necho ${g} >> "${CALLS}"\n[ "$(cat "${MODE}")" = "budget-deny" ] && [ ${g} = budget ] && { echo "budget says no" >&2; exit 2; }\nexit 0\n`) + chmodSync(join(FD, "bin", g), 0o755) +} +writeFileSync(join(FD, "bin", "mission"), `#!/bin/sh +echo "mission $*" >> "${CALLS}" +case "$(cat "${MODE}")" in + hang) exec sleep 30 ;; + crash) echo "Traceback: boom" >&2; exit 1 ;; + kill) kill -9 $$ ;; + odd) exit 3 ;; + block) echo "BLOCK[doors]: the door gate could not decide" >&2; exit 2 ;; + deny) echo '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"doors: ASK[door] x"}}' ;; +esac +exit 0 +`) +chmodSync(join(FD, "bin", "mission"), 0o755) +const fd = host({ root: FD, doorsTimeoutMs: 500 }) +plugin.apply(fd.ctx, fd.config) +const fdRoot = agentOf("root8") +const doorsRun = async (mode, name = "bash", args = { command: "ls" }) => { + writeFileSync(MODE, mode); writeFileSync(CALLS, "") + const d = await pre(fd, fdRoot, name, args) + return { d, calls: readFileSync(CALLS, "utf8").trim().split("\n") } +} +let r = await doorsRun("ok") +assert.ok(allowed(r.d) && r.calls.at(-1) === "mission doors hook" && r.calls.at(-2) === "budget", r.calls.join(",")) +r = await doorsRun("ok", "mcp__github__push_files", pushFiles) +assert.ok(allowed(r.d) && r.calls.at(-1) === "mission doors hook", r.calls.join(",")) +ok("the door gate runs on every tool that can cross a door, last (after budget)") +r = await doorsRun("budget-deny") +assert.ok(denied(r.d, /budget says no/) && !r.calls.includes("mission doors hook"), r.calls.join(",")) +ok("a call another gate refused never reaches the door gate (no crossing recorded for it)") +for (const [name, args] of [["read", { file_path: "README.md" }], ["glob", { pattern: "*" }], ["grep", { pattern: "x" }], ["read_image", { file_path: "a.png" }], ["todo_write", { todos: [] }], ["web_fetch", { url: "https://x.io" }], ["web_search", { query: "x" }]]) { + r = await doorsRun("hang", name, args) + assert.ok(allowed(r.d) && !r.calls.includes("mission doors hook"), `${name}: ${r.calls.join(",")}`) +} +ok("a read-only tool (read, glob, grep, todo, web) skips the door gate's spawn") +const t0 = Date.now() +r = await doorsRun("hang") +assert.ok(denied(r.d, /door gate.*did not answer.*fail closed/) && Date.now() - t0 < 10000, JSON.stringify(r.d)) +for (const [mode, re] of [["crash", /door gate.*exited 1.*boom.*fail closed/], ["kill", /door gate.*(SIGKILL|killed).*fail closed/], ["odd", /door gate.*exited 3.*fail closed/], + ["block", /BLOCK\[doors\]: the door gate could not decide/], ["deny", /doors: ASK\[door\] x/]]) { + r = await doorsRun(mode) + assert.ok(denied(r.d, re), `${mode}: ${JSON.stringify(r.d)}`) +} +rmSync(join(FD, "bin", "mission")) +assert.ok(denied((await doorsRun("ok")).d, /door gate.*could not run.*fail closed/)) +ok("the door gate fails closed: a hang (timeout), a crash, a kill, an odd exit or a missing gate denies; its own block or deny is the reason") + // a gate runs the worker's code, so it gets the worker shell's environment: dsh's credential scrub, // aloop's own settings kept, and the plugin root the gates read their schemas from const FR = join(T, "fake-root") From 091c598da007c4d2963f20140bb8ce3b726724ef Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 11:34:25 +0000 Subject: [PATCH 05/14] M9.31 integration: decode a dsh MCP input once, and only its own shape The gate read any `arguments` string as an MCP tool's whole input, so on Claude Code `branch: main` beside `arguments: '{"branch": "x"}'` crossed no door, and the hook decoded dsh's object shape twice. Only the exact {arguments, workdir} shape is decoded now, once. The OpenCode test's two expected-red checks are hard checks. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- plugin/mission/doors.py | 20 +++++++++++++++----- scripts/test/mission-doors.test.py | 12 ++++++++++++ scripts/test/opencode-plugin.test.mjs | 21 +++++++-------------- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/plugin/mission/doors.py b/plugin/mission/doors.py index f2499ecd..33a5b67f 100644 --- a/plugin/mission/doors.py +++ b/plugin/mission/doors.py @@ -711,19 +711,29 @@ def rel_path(cwd, path): return [x for x in (r, given, full) if not x.startswith("..")], (plug if not plug.startswith("..") else None) +class Read(dict): + """An input input_of already read: reading it again would decode a nested `arguments` string the tool + itself gets as a string.""" + + def input_of(tool, inp): """The input the doors read. dsh hands a tool outside its built-ins (an MCP tool) its input as one JSON string, {"arguments": "{…}", "workdir": …}: it is read as the object it encodes, so an input pattern - matches and a once-digest is the same as on a host that sends the object.""" + matches and a once-digest is the same as on a host that sends the object. Only that exact shape is + decoded: an `arguments` string beside other fields is one more field the tool gets, and reading it as + the input would hide the real ones (`branch: main` beside `arguments: '{"branch": "x"}'`).""" + if isinstance(inp, Read): + return inp inp = inp if isinstance(inp, dict) else {} - if tool not in ("Bash", "apply_patch") + EDITS and isinstance(inp.get("arguments"), str): + if (tool not in ("Bash", "apply_patch") + EDITS and isinstance(inp.get("arguments"), str) + and set(inp) <= {"arguments", "workdir"}): try: args = json.loads(inp["arguments"]) except ValueError: - return inp + return Read(inp) if isinstance(args, dict): - return args - return inp + return Read(args) + return Read(inp) def written(tool, inp): diff --git a/scripts/test/mission-doors.test.py b/scripts/test/mission-doors.test.py index f61a41aa..cfc092dc 100644 --- a/scripts/test/mission-doors.test.py +++ b/scripts/test/mission-doors.test.py @@ -729,6 +729,18 @@ def ids(tool, inp, cwd=None): assert ids("mcp__github__push_files", dsh_push) == ids("mcp__github__push_files", pushed) == ["push-default-branch"] assert ids("mcp__github__push_files", {"arguments": json.dumps(dict(pushed, branch="feat")), "workdir": p.dir}) == [] assert ids("mcp__github__push_files", {"arguments": "[1]"}) == ids("mcp__github__push_files", {"arguments": "{"}) == [] +# an `arguments` string beside the real fields is one more field the tool gets, not the input: it hides nothing +spoof = dict(pushed, arguments=json.dumps(dict(pushed, branch="feat"))) +assert ids("mcp__github__push_files", spoof) == ["push-default-branch"], ids("mcp__github__push_files", spoof) +fresh = Project() # p authorized push-default-branch above +assert "ASK[door] push-default-branch" in denied(hook(fresh, "mcp__github__push_files", spoof)) +# dsh's object shape keeps a model's own `arguments` key as the whole object: decoded once, never twice +whole = dict(spoof, arguments=json.dumps(spoof), workdir=fresh.dir) +assert "ASK[door] push-default-branch" in denied(hook(fresh, "mcp__github__push_files", whole)) +shutil.rmtree(fresh.dir, ignore_errors=True) +nested = {"arguments": json.dumps({"arguments": json.dumps(pushed)}), "workdir": p.dir} # the tool gets {arguments: "…"} +assert D.input_of("mcp__github__push_files", nested) == {"arguments": json.dumps(pushed)} +assert D.input_of("mcp__github__push_files", D.input_of("mcp__github__push_files", nested)) == {"arguments": json.dumps(pushed)} shared = {"fileId": "f1", "role": "writer", "type": "anyone"} r = denied(hook(p, "mcp__Google_Drive__share_file", {"arguments": json.dumps(shared), "workdir": p.dir})) assert "ASK[door] visibility-or-sharing" in r and "--once %s" % D.action_sha("mcp__Google_Drive__share_file", shared) in r, r diff --git a/scripts/test/opencode-plugin.test.mjs b/scripts/test/opencode-plugin.test.mjs index d018a1dd..da2d7f3a 100644 --- a/scripts/test/opencode-plugin.test.mjs +++ b/scripts/test/opencode-plugin.test.mjs @@ -52,12 +52,6 @@ const hooks = await AgenticAgile({ client, directory: T, worktree: T, project: { let pass = 0 const ok = (name) => { pass++; console.log("ok -", name) } const throws = async (fn, re, name) => { await assert.rejects(fn, re); ok(name) } -// expected red until another part of M9.31 lands (the door gate's side): reported, not counted, never fails the run -let todo = 0 -const pending = async (name, until, fn) => { - try { await fn(); console.log(`ok - ${name} # TODO passes now: make it a hard check`) } - catch { todo++; console.log(`not ok - ${name} # TODO expected red until ${until}`) } -} const CORE = "Core principles (every aloop agent):" // the core block's head (mission principles show --core) const count = (s, x) => s.split(x).length - 1 const systemOf = async (h, sessionID) => { @@ -129,8 +123,8 @@ await hooks["tool.execute.before"]({ tool: "apply_patch", sessionID: "child", ca assert.ok(patchArgs.patchText.includes(`*** Add File: ${join(wt, "src/lib.rs")}`)); ok("patch paths rewritten into the worktree") // the door gate reads a bound worker's command where it runs (tool_input.workdir, its worktree): a reset // there is two-way, the same reset in the main checkout (above) is not -await pending("doors: a bound worker's reset in its own worktree is two-way", "the gate reads tool_input.workdir (C2)", - () => hooks["tool.execute.before"]({ tool: "bash", sessionID: "child", callID: "d5" }, { args: { command: "git reset --hard" } })) +await hooks["tool.execute.before"]({ tool: "bash", sessionID: "child", callID: "d5" }, { args: { command: "git reset --hard" } }) +ok("doors: a bound worker's reset in its own worktree is two-way") // token ledger + time box: usage recorded per assistant message; soft token telemetry then hard stop const usage = (id, input, output) => ({ type: "message.updated", properties: { info: { @@ -277,10 +271,9 @@ assert.ok(/door gate/.test(g.err?.message), g.err?.message); ok("doors: a door g // cannot authorize a door const agentEnv = { ...process.env, ALOOP_AGENT_SHELL: envOut.env.ALOOP_AGENT_SHELL, AGENTIC_HOST: envOut.env.AGENTIC_HOST } for (const k of ["CLAUDECODE", "DSH_SHELL", "CODEX_THREAD_ID"]) delete agentEnv[k] -await pending("doors: `mission doors authorize` refuses the agent's OpenCode shell", "authorize reads ALOOP_AGENT_SHELL (C3)", () => { - const a = spawnSync(join(BIN, "aloop"), ["mission", "doors", "authorize", "publish-release", "--by", "x", "--reason", "r"], - { cwd: T, env: agentEnv, encoding: "utf8" }) - assert.ok(a.status === 2 && /agent session/.test(a.stderr), a.stderr) -}) +const a = spawnSync(join(BIN, "aloop"), ["mission", "doors", "authorize", "publish-release", "--by", "x", "--reason", "r"], + { cwd: T, env: agentEnv, encoding: "utf8" }) +assert.ok(a.status === 2 && /agent session/.test(a.stderr), a.stderr) +ok("doors: `mission doors authorize` refuses the agent's OpenCode shell") -console.log(`\n${pass} checks passed${todo ? `, ${todo} expected red (TODO)` : ""}`) +console.log(`\n${pass} checks passed`) From b169796885588ac9eb3dc8a758396a7fdc598273 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 13:24:06 +0000 Subject: [PATCH 06/14] M9.31 review: the door gate holds where its steps fail, patch and path forms, MCP names - A deadline is a BaseException (Deadline): a step's `except Exception` can no longer swallow it and turn a stalled decision into an allow. - apply_patch is read as the hosts parse it: any spacing or indent around a header (doors, host-adapter, both scope gates, the OpenCode worktree rewrite), and run through the shell (Codex's heredoc and argument forms, applypatch, after a cd, from a pipe). - A relative OpenCode or dsh workdir is resolved under the payload's cwd; a missing one is unknown. - A path through a symlink then `..` is also read as the kernel walks it, and an edit's path relative to this checkout when outside the project. - dsh: an MCP tool's input is its own arguments (no workdir), so its once digest is the same from any directory. - OpenCode: an MCP tool reaches the gates as mcp____, from client.mcp.status(), the longest server first; codesearch skips the gate. - opencode.json is a door only where OpenCode reads it; settings replaced, moved, linked or removed by cp/mv/ln/install/rsync/rm/sed -i/perl -i are the settings door. Each fix has a test that failed on the code before it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- plugin/bin/gate-mission-scope | 2 +- plugin/bin/gate-supervisor-scope | 2 +- plugin/bin/host-adapter | 2 +- plugin/hosts/dsh/aloop.mjs | 3 +- plugin/hosts/opencode/aloop.js | 30 +++++++-- plugin/mission/doors.json | 34 ++++++++-- plugin/mission/doors.py | 96 ++++++++++++++++++--------- scripts/test/codex-hooks.test.py | 6 +- scripts/test/dsh-plugin.test.mjs | 12 ++++ scripts/test/mission-doors.test.py | 70 ++++++++++++++++++- scripts/test/mission-roles.test.py | 1 + scripts/test/opencode-plugin.test.mjs | 24 +++++-- 12 files changed, 229 insertions(+), 53 deletions(-) diff --git a/plugin/bin/gate-mission-scope b/plugin/bin/gate-mission-scope index 786dfe87..92d2c68f 100755 --- a/plugin/bin/gate-mission-scope +++ b/plugin/bin/gate-mission-scope @@ -48,7 +48,7 @@ import _shellread as SR # noqa: E402 GATE = "mission-scope" EDIT_TOOLS = ("Write", "Edit", "MultiEdit", "NotebookEdit", "write", "edit") SHELL_TOOLS = ("Bash", "bash", "shell") -PATCH_PATHS = re.compile(r"^\*\*\* (?:Add|Update|Delete) File: (.+)$|^\*\*\* Move to: (.+)$", re.M) +PATCH_PATHS = re.compile(r"^[^\S\n]*\*\*\*[^\S\n]*(?:Add|Update|Delete)[^\S\n]+File:[^\S\n]*(.+)$|^[^\S\n]*\*\*\*[^\S\n]*Move[^\S\n]+to:[^\S\n]*(.+)$", re.M) GIT_WRITES = {"commit", "push", "pull", "merge", "rebase", "cherry-pick", "am", "apply", "reset", "restore", "checkout", "switch", "stash", "rm", "mv", "add", "revert", "clean", "tag", "worktree", "notes", "update-ref", "branch", "config", "submodule", "sparse-checkout", "filter-branch", "replace"} diff --git a/plugin/bin/gate-supervisor-scope b/plugin/bin/gate-supervisor-scope index 17c6a46f..2b520634 100755 --- a/plugin/bin/gate-supervisor-scope +++ b/plugin/bin/gate-supervisor-scope @@ -65,7 +65,7 @@ if [ "$tool" = "apply_patch" ] && command -v python3 >/dev/null 2>&1; then import json, re, sys p = json.load(sys.stdin) patch = (p.get("tool_input") or {}).get("command") or "" -for a, b in re.findall(r"^\*\*\* (?:Add|Update|Delete) File: (.+)$|^\*\*\* Move to: (.+)$", patch, re.M): +for a, b in re.findall(r"^[^\S\n]*\*\*\*[^\S\n]*(?:Add|Update|Delete)[^\S\n]+File:[^\S\n]*(.+)$|^[^\S\n]*\*\*\*[^\S\n]*Move[^\S\n]+to:[^\S\n]*(.+)$", patch, re.M): q = {k: v for k, v in p.items() if k in ("session_id", "agent_type", "agent_id", "cwd")} q["tool_name"] = "Write"; q["tool_input"] = {"file_path": (a or b).strip()} print(json.dumps(q))' | while IFS= read -r one; do diff --git a/plugin/bin/host-adapter b/plugin/bin/host-adapter index 267e5e20..f4061129 100755 --- a/plugin/bin/host-adapter +++ b/plugin/bin/host-adapter @@ -44,7 +44,7 @@ _SAFE = r"[A-Za-z0-9_./@%+=:,-]" _WORD = r"(?:'[^'\n]*'|\"[^\"$`\\\n!]*\"|%s+)" % _SAFE ONLY_BIND = re.compile(r"[ \t]*(?:cd[ \t]+%s[ \t]*&&[ \t]*)?(?:%s*/)?(?:(?:aloop|agentic)[ \t]+bind|bin/bind)[ \t]+%s" r"(?:[ \t]+(?:[12]?>[ \t]*/dev/null|2>&1))*\s*\Z" % (_WORD, _SAFE, _WORD)) -PATCH_PATHS = re.compile(r"^\*\*\* (?:Add|Update|Delete) File: (.+)$|^\*\*\* Move to: (.+)$", re.M) +PATCH_PATHS = re.compile(r"^[^\S\n]*\*\*\*[^\S\n]*(?:Add|Update|Delete)[^\S\n]+File:[^\S\n]*(.+)$|^[^\S\n]*\*\*\*[^\S\n]*Move[^\S\n]+to:[^\S\n]*(.+)$", re.M) def role_of(agent_type): diff --git a/plugin/hosts/dsh/aloop.mjs b/plugin/hosts/dsh/aloop.mjs index 3a06f3dc..4aa0ed57 100644 --- a/plugin/hosts/dsh/aloop.mjs +++ b/plugin/hosts/dsh/aloop.mjs @@ -205,8 +205,9 @@ export function apply(ctx, config = {}) { options: Array.isArray(q?.options) ? q.options.map((o) => ({ label: str(o?.label), description: str(o?.description) })) : undefined, multi_select: typeof q?.multi_select === "boolean" ? q.multi_select : undefined })) } default: + // an MCP tool's own arguments, no workdir: its once digest is the one any host prints for them if (tool.startsWith("mcp__") && obj(a)) - return { ...a, ...(Object.hasOwn(a, "arguments") ? { arguments: JSON.stringify(a) } : {}), workdir: wd } + return { ...a, ...(Object.hasOwn(a, "arguments") ? { arguments: JSON.stringify(a) } : {}) } return { arguments: JSON.stringify(a), workdir: wd } } } diff --git a/plugin/hosts/opencode/aloop.js b/plugin/hosts/opencode/aloop.js index a9a69a08..c727ddc8 100644 --- a/plugin/hosts/opencode/aloop.js +++ b/plugin/hosts/opencode/aloop.js @@ -51,7 +51,7 @@ const GATES = { const TOOL = { bash: "Bash", write: "Write", edit: "Edit", apply_patch: "apply_patch", read: "Read", task: "Agent", question: "AskUserQuestion" } const EDITS = new Set(["write", "edit", "apply_patch"]) // read-only built-ins (the door gate's QUIET set): they cross no door, so no spawn -const QUIET = new Set(["read", "grep", "glob", "list", "todowrite", "todoread", "webfetch", "websearch"]) +const QUIET = new Set(["read", "grep", "glob", "list", "todowrite", "todoread", "webfetch", "websearch", "codesearch"]) const GATE_ROUNDS = 3 // the door gate's timeout: hooks.json's 15 s; ALOOP_DOORS_TIMEOUT_MS only shortens it (tests) const DOORS_MS = ((t) => (t > 0 && t < 15000 ? t : 15000))(Number(process.env.ALOOP_DOORS_TIMEOUT_MS)) @@ -108,6 +108,26 @@ export const AgenticAgile = async ({ client, directory, worktree }) => { return core } + // OpenCode names an MCP tool _, each part with [^a-zA-Z0-9_-] as "_": the gates read it as + // Claude Code's mcp____ (the longest server name that prefixes it), so a door matches a + // server and a tool, not a word inside either. The servers are re-read on a miss, at most once a second. + let servers = [], serversAt = 0 + async function toolName(tool) { + if (TOOL[tool]) return TOOL[tool] + if (QUIET.has(tool) || !tool.includes("_")) return tool + const of = () => servers.find((n) => tool.startsWith(n + "_") && tool.length > n.length + 1) + if (!of() && Date.now() - serversAt > 1000) { + serversAt = Date.now() + try { + const data = (await client.mcp.status()).data + if (data && typeof data === "object") + servers = Object.keys(data).map((n) => n.replace(/[^a-zA-Z0-9_-]/g, "_")).sort((a, b) => b.length - a.length) + } catch {} + } + const n = of() + return n ? `mcp__${n}__${tool.slice(n.length + 1)}` : tool + } + async function info(id) { if (sessions.has(id)) return sessions.get(id) let parentID = null @@ -216,7 +236,7 @@ export const AgenticAgile = async ({ client, directory, worktree }) => { if (!active()) return const s = await info(input.sessionID) const args = output.args || {} - let p = await payload("PreToolUse", input.sessionID, { tool_name: TOOL[input.tool] || input.tool, tool_input: toolInput(input.tool, args) }) + let p = await payload("PreToolUse", input.sessionID, { tool_name: await toolName(input.tool), tool_input: toolInput(input.tool, args) }) run("host-adapter", ["hook"], p) // records `aloop bind` const role = s.agent || "" if (s.parentID && GATES[role] && WRITERS.has(role) && !s.toolingOk) { // SubagentStart preflight @@ -233,8 +253,8 @@ export const AgenticAgile = async ({ client, directory, worktree }) => { if (input.tool === "bash") args.workdir = wt if (input.tool === "write" || input.tool === "edit") args.filePath = intoWt(args.filePath, wt) if (input.tool === "apply_patch" && args.patchText) - args.patchText = args.patchText.replace(/^(\*\*\* (?:Add|Update|Delete) File: |\*\*\* Move to: )(.+)$/gm, (_, h, f) => h + intoWt(f.trim(), wt)) - p = await payload("PreToolUse", input.sessionID, { tool_name: TOOL[input.tool] || input.tool, tool_input: toolInput(input.tool, args) }) + args.patchText = args.patchText.replace(/^([^\S\n]*\*\*\*[^\S\n]*(?:Add|Update|Delete)[^\S\n]+File:[^\S\n]*|[^\S\n]*\*\*\*[^\S\n]*Move[^\S\n]+to:[^\S\n]*)(.+)$/gm, (_, h, f) => h + intoWt(f.trim(), wt)) + p = await payload("PreToolUse", input.sessionID, { tool_name: await toolName(input.tool), tool_input: toolInput(input.tool, args) }) } } const checks = [] @@ -256,7 +276,7 @@ export const AgenticAgile = async ({ client, directory, worktree }) => { "tool.execute.after": async (input, output) => { if (!active()) return const p = await payload("PostToolUse", input.sessionID, { - tool_name: TOOL[input.tool] || input.tool, tool_input: toolInput(input.tool, input.args || {}), tool_response: output.output, + tool_name: await toolName(input.tool), tool_input: toolInput(input.tool, input.args || {}), tool_response: output.output, }) run("transcripts", ["record"], p) const res = run("budget", ["hook", "post"], p) diff --git a/plugin/mission/doors.json b/plugin/mission/doors.json index 29177d8e..c729f6c2 100644 --- a/plugin/mission/doors.json +++ b/plugin/mission/doors.json @@ -421,7 +421,8 @@ "(?:^|/)\\.claude/settings(?:\\.local)?\\.json$", "(?:^|/)\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)$", "(?:^|/)(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js$", - "(?:^|/)opencode\\.jsonc?$", + "^opencode\\.jsonc?$", + "(?:^|/)(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?$", "(?:^|/)\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json)$" ], "plugin_paths": [ @@ -435,7 +436,21 @@ "writes": "(?:^|/)\\.claude/settings(?:\\.local)?\\.json$" }, { - "writes": "(?:^|/)(?:\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|opencode\\.jsonc?|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$" + "writes": "(?:^|/)(?:\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" + }, + { + "prog": "cp|mv|ln|install|rsync|rm|unlink|truncate|shred", + "files": "(?:^|/)(?:\\.claude/settings(?:\\.local)?\\.json|\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" + }, + { + "prog": "sed", + "flag": "-[^-\\sefl]*i.*|--in-place(?:=.*)?", + "files": "(?:^|/)(?:\\.claude/settings(?:\\.local)?\\.json|\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" + }, + { + "prog": "perl", + "flag": "-[^-\\sMmIeEFxdDC]*i.*", + "files": "(?:^|/)(?:\\.claude/settings(?:\\.local)?\\.json|\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" }, { "prog": "git", @@ -459,12 +474,18 @@ "~/.config/opencode/plugins/aloop.js", ".opencode/opencode.json", "opencode.jsonc", + "home/worktrees/S1-01-T1/opencode.json", + "~/.config/opencode/opencode.jsonc", "~/.dsh/profiles/aloop/cordis.patch.yml", "~/.dsh/profiles/aloop/package.json" ], "bash": [ "echo '{}' > .claude/settings.json", "echo '{}' > opencode.json", + "cd .opencode && echo '{}' > opencode.json", + "cp /tmp/s .claude/settings.json", + "sed -i 's/doors//' .codex/hooks.json", + "rm -f .claude/settings.local.json", "cat x >> .codex/hooks.json", "printf x | tee ~/.dsh/profiles/aloop/cordis.patch.yml", "git config core.hooksPath /dev/null", @@ -736,6 +757,8 @@ "python3 -c \"import _mission as M; print(len(M.records('.', 'door.authorized')))\"", "cat .codex/config.toml", "jq . opencode.json", + "echo '{}' > docs/examples/opencode.json", + "sed 's/x//' .claude/settings.json", "cat ~/.dsh/profiles/aloop/cordis.patch.yml", "cat plugin/hosts/opencode/aloop.js" ], @@ -749,7 +772,8 @@ "slack_slack_list_channels", "Gmail_search_threads", "slack_get_post_history", - "mcp__drive__get_share" + "mcp__drive__get_share", + "mcp__notion__search_slack_posts" ], "paths": [ "src/a.py", @@ -759,7 +783,9 @@ ".codex/notes.md", ".dsh/skills/aloop-mission/SKILL.md", "docs/codex/config.toml", - "src/opencode.ts" + "src/opencode.ts", + "/tmp/opencode.json", + "docs/examples/opencode.json" ] } } diff --git a/plugin/mission/doors.py b/plugin/mission/doors.py index 33a5b67f..dd23bb06 100644 --- a/plugin/mission/doors.py +++ b/plugin/mission/doors.py @@ -80,8 +80,11 @@ # set in every shell an agent host runs (the agent's Bash tool): Claude Code, dsh, Codex, and OpenCode (its # plugin exports ALOOP_AGENT_SHELL); an authorization never comes from there AGENT_SHELL = ("CLAUDECODE", "DSH_SHELL", "CODEX_THREAD_ID", "ALOOP_AGENT_SHELL") -# the paths a patch adds, updates, deletes or moves to (host-adapter's PATCH_PATHS) -PATCH = re.compile(r"^\*\*\* (?:Add|Update|Delete) File: (.+)$|^\*\*\* Move to: (.+)$", re.M) +# the paths a patch adds, updates, deletes or moves to (host-adapter's PATCH_PATHS), read as loosely as a +# host parses a header: Codex trims each line, OpenCode splits at the colon and trims the path +PATCH = re.compile(r"^[^\S\n]*\*\*\*[^\S\n]*(?:Add|Update|Delete)[^\S\n]+File:[^\S\n]*(.+)$" + r"|^[^\S\n]*\*\*\*[^\S\n]*Move[^\S\n]+to:[^\S\n]*(.+)$", re.M) +PATCHERS = ("apply_patch", "applypatch") # Codex's patch tool run as a program (its argument or stdin form) TAG = re.compile(r"\[door:([^\]\s]*)\]") WRAPPERS = {"sudo", "doas", "env", "nohup", "time", "command", "exec", "nice", "timeout", "stdbuf", "caffeinate", "ionice"} SHELLS = {"sh", "bash", "zsh", "dash", "ksh"} @@ -358,6 +361,8 @@ def feed(vs, text, depth): for v in vs: if "code" in v: # python: its stdin is code it may run (exec(sys.stdin.read())) v["code"] = (v["code"] + "\n" + text).lstrip("\n") + elif v["prog"] in PATCHERS: # apply_patch <<'EOF' … EOF: each path the patch names is a write + v["writes"] = v["writes"] + patched(text) elif reads_stdin(v): out += [dict(x, env=x["env"] + v["env"]) for x in views(text, depth + 1, v["dir"])] return out @@ -412,6 +417,8 @@ def chain(words, env, writes, depth, cur): prog, rest, code = os.path.basename(rest[j]), rest[j + 1:], None # python SCRIPT: the script runs if prog == "tee": writes = writes + [w for w in rest if not w.startswith("-")] + if prog in PATCHERS and rest: # apply_patch 'PATCH': each path it names is a write + writes = writes + patched(rest[0]) if prog == "git": while rest and rest[0].startswith("-"): o, v = rest[0], rest[1] if len(rest) > 1 else None @@ -560,13 +567,15 @@ def toplevel(cwd): return r.stdout.strip() if r.returncode == 0 else "" -def resolve(cwd, where): - """The absolute directory of a view's `dir` (TOP: the checkout's top, from git; "" when there is none).""" +def resolve(cwd, where, kernel=False): + """The absolute directory of a view's `dir` (TOP: the checkout's top, from git; "" when there is none); + `kernel`: its symlinks resolved as the kernel walks it (before each `..`).""" if where.startswith(TOP): if not toplevel(cwd): return "" where = toplevel(cwd) + where[len(TOP):] - return os.path.normpath(os.path.join(cwd, os.path.expanduser(where))) + where = os.path.join(cwd, os.path.expanduser(where)) + return os.path.realpath(where) if kernel else os.path.normpath(where) def branches(cwd, vs): @@ -638,11 +647,12 @@ def dry(args): GUARDED = ("docs/agents/mission/ledger.jsonl", ".aladdin/logs/events.jsonl", ".agentic/logs/events.jsonl") -def placed(cwd, top, v, path): +def placed(cwd, top, v, path, kernel=False): """A path a command names as its patterns read it: resolved where the command runs, relative to the project `top` or this checkout when inside one (`cd docs/agents && … mission/ledger.jsonl` is the ledger; /tmp/x/.aladdin is not the run's home). Where the gate cannot resolve it, a name a guarded - record ends with is that record.""" + record ends with is that record. `kernel`: walked as the kernel walks it, a symlink resolved before + the `..` after it (link/../x is beside the link's target), instead of as the words say.""" if path.startswith("-") or path in (STDIN, "") or "\n" in path or len(path) > 4096: return path if re.match("(?:%s|\\$PWD|\\$CLAUDE_PROJECT_DIR)(?:/|$)" % re.escape(TOP), path): @@ -654,7 +664,7 @@ def placed(cwd, top, v, path): if where == UNKNOWN: x = os.path.normpath(path) return next((g for g in GUARDED if not os.path.isabs(x) and (g == x or g.endswith("/" + x))), path) - full = resolve(cwd, where) + full = resolve(cwd, where, kernel) if not full: return path for root in (top, toplevel(os.path.abspath(cwd))): # the project, else this checkout (a worktree) @@ -668,8 +678,9 @@ def place(cwd, vs): """Each view's operands and writes as placed() reads them (files, placed_writes).""" top = os.path.realpath(_paths.project(cwd)) for v in vs: - v["files"] = [placed(cwd, top, v, a) for a in v["args"] if not a.startswith("-")] - v["placed_writes"] = [placed(cwd, top, v, w) for w in v["writes"]] + v["files"] = list(dict.fromkeys(placed(cwd, top, v, a, k) for a in v["args"] if not a.startswith("-") + for k in (False, True))) + v["placed_writes"] = list(dict.fromkeys(placed(cwd, top, v, w, k) for w in v["writes"] for k in (False, True))) return vs @@ -702,13 +713,22 @@ def bash_hits(d, v, ctx, default): def rel_path(cwd, path): """(the forms a project path pattern is matched against, the path relative to the plugin or None): a - path relative to the project, and the absolute path as written and resolved, so a suffix pattern - (one that ends in /.claude/settings.json) also covers ~/.claude, a worktree's own and a linked file.""" - given = os.path.normpath(os.path.join(cwd, os.path.expanduser(path))) - full = os.path.realpath(given) - r = os.path.relpath(full, os.path.realpath(_paths.project(cwd))) - plug = os.path.relpath(full, PLUGIN) - return [x for x in (r, given, full) if not x.startswith("..")], (plug if not plug.startswith("..") else None) + path relative to the project (else to this checkout, a linked worktree), and the absolute path as + written and resolved, so a suffix pattern (one that ends in /.claude/settings.json) also covers + ~/.claude, a worktree's own and a linked file. Resolved twice: as the words say, and as the kernel + walks it (a symlink before the `..` after it: link/../x is beside the link's target).""" + raw = os.path.join(cwd, os.path.expanduser(path)) + given = os.path.normpath(raw) + roots = [os.path.realpath(_paths.project(cwd))] + forms, plug = [given], None + for full in dict.fromkeys((os.path.realpath(given), os.path.realpath(raw))): + r = os.path.relpath(full, roots[0]) + if r.startswith("..") and len(roots) == 1: + roots.append(os.path.realpath(toplevel(cwd) or roots[0])) + forms += [next((x for x in (os.path.relpath(full, t) for t in roots) if not x.startswith("..")), ".."), full] + p = os.path.relpath(full, PLUGIN) + plug = plug or (p if not p.startswith("..") else None) + return [x for x in dict.fromkeys(forms) if not x.startswith("..")], plug class Read(dict): @@ -742,22 +762,27 @@ def written(tool, inp): if tool in EDITS: paths = [inp.get("file_path") or inp.get("notebook_path")] elif tool == "apply_patch": - paths = [(a or b).strip() for k in ("command", "patchText") if isinstance(inp.get(k), str) - for a, b in PATCH.findall(inp[k])] + paths = [x for k in ("command", "patchText") if isinstance(inp.get(k), str) for x in patched(inp[k])] else: paths = [] return [x for x in paths if isinstance(x, str) and x] -def workdir(inp): +def patched(text): + """Each path a patch adds, updates, deletes or moves to.""" + return [x for x in ((a or b).strip() for a, b in PATCH.findall(text)) if x] + + +def workdir(inp, cwd): """Where a Bash command starts: OpenCode and dsh run a bound worker's command in tool_input.workdir (the - payload's cwd stays the project, which still names the catalogue and the ledger) when it is an absolute - directory that exists; anywhere else the payload's cwd (""). Claude Code sends none.""" + payload's cwd stays the project, which still names the catalogue and the ledger), a relative one under + the payload's cwd; a directory that is not there is UNKNOWN (its git state assumed). Anywhere else, or + no path, the payload's cwd (""). Claude Code sends none.""" wd = inp.get("workdir") - if os.environ.get("AGENTIC_HOST") in ("opencode", "dsh") and isinstance(wd, str) and os.path.isabs(wd) \ - and os.path.isdir(wd): - return wd - return "" + if os.environ.get("AGENTIC_HOST") not in ("opencode", "dsh") or not isinstance(wd, str) or not wd: + return "" + wd = os.path.normpath(os.path.join(cwd, wd)) + return wd if os.path.isdir(wd) else UNKNOWN def classify(cwd, tool, inp, cat=None, assume_when=False): @@ -766,7 +791,7 @@ def classify(cwd, tool, inp, cat=None, assume_when=False): default = re.compile("(?:%s)" % cat.get("default_branch", "main|master")) ctx = contexts(cwd, assume_when) inp = input_of(tool, inp) - vs = place(cwd, branches(cwd, views(str(inp.get("command", "")), 0, workdir(inp)))) if tool == "Bash" else [] + vs = place(cwd, branches(cwd, views(str(inp.get("command", "")), 0, workdir(inp, cwd)))) if tool == "Bash" else [] proj_rel, plug_rel = [], [] for path in written(tool, inp): rel, plug = rel_path(cwd, path) @@ -1010,9 +1035,14 @@ def hook(cwd_default): READ_SECONDS = 8 # under the hook's own timeout (plugin/hooks/hooks.json, the OpenCode and dsh plugins: 15 s) +class Deadline(BaseException): + """The decision ran past its deadline. Not an Exception, so no `except Exception` or `except OSError` in + a step (a git call, a parse) can swallow it and let the gate fall through to an allow.""" + + @contextlib.contextmanager def deadline(seconds): - """A TimeoutError after `seconds` (POSIX); yields the monotonic time it ends at, which the lock waits for + """A Deadline after `seconds` (POSIX); yields the monotonic time it ends at, which the lock waits for too: a gate that cannot decide in time fails closed (exit 2) before the host's hook timeout, which a host may treat as a non-blocking error, lets the call through.""" end = time.monotonic() + seconds @@ -1021,7 +1051,7 @@ def deadline(seconds): return def stop(*_): - raise TimeoutError("the door gate could not decide within %g s" % seconds) + raise Deadline("the door gate could not decide within %g s" % seconds) old = signal.signal(signal.SIGALRM, stop) signal.setitimer(signal.ITIMER_REAL, seconds) try: @@ -1055,10 +1085,10 @@ def serialized(cwd, end): break except BlockingIOError: if time.monotonic() >= end: - raise TimeoutError from None + raise Deadline from None time.sleep(0.02) - except TimeoutError: # the poll's own deadline, or the alarm's - raise TimeoutError("another door gate held %s past this decision's deadline" % M.rel(cwd, path)) from None + except Deadline: # the poll's own deadline, or the alarm's + raise Deadline("another door gate held %s past this decision's deadline" % M.rel(cwd, path)) from None yield finally: os.close(fd) @@ -1226,7 +1256,7 @@ def main(argv, cwd): if cmd == "hook": try: return hook(cwd) - except Exception as e: # noqa: BLE001 — a gate that cannot decide fails closed + except (Exception, Deadline) as e: # noqa: BLE001 — a gate that cannot decide fails closed print("BLOCK[doors]: the door gate could not decide (%s: %s); fix it or run the action yourself" % (type(e).__name__, e), file=sys.stderr) return 2 diff --git a/scripts/test/codex-hooks.test.py b/scripts/test/codex-hooks.test.py index d61cae6a..c9c5155c 100644 --- a/scripts/test/codex-hooks.test.py +++ b/scripts/test/codex-hooks.test.py @@ -118,7 +118,10 @@ def rollout(thread, rows): # supervisor (no agent_id) patching source mid-sprint is blocked; apply_patch matches Write|Edit fire("PreToolUse", dict(base, tool_name="apply_patch", tool_input={"command": "*** Begin Patch\n*** Add File: docs/agents/sprint1/s1-01-x/init.md\n+x\n*** End Patch"})) why = denied(fire("PreToolUse", dict(base, tool_name="apply_patch", tool_input={"command": "*** Begin Patch\n*** Update File: src/main.rs\n@@\n-a\n+b\n*** End Patch"}))) -assert why and "production source" in why; ok("supervisor-scope blocks the supervisor's apply_patch to source") +assert why and "production source" in why +why = denied(fire("PreToolUse", dict(base, tool_name="apply_patch", tool_input={"command": "*** Begin Patch\n *** Update File:src/main.rs\n@@\n-a\n+b\n*** End Patch"}))) +assert why and "production source" in why, why # a header Codex accepts once trimmed, with no space after the colon +ok("supervisor-scope blocks the supervisor's apply_patch to source") why = denied(fire("PreToolUse", dict(base, tool_name="Bash", tool_input={"command": "git commit -m 'x\n\nCo-Authored-By: Claude '"}))) assert why and "co-author" in why.lower(); ok("commit-author blocks a Claude trailer") @@ -143,6 +146,7 @@ def rollout(thread, rows): assert denied(fire("PreToolUse", dict(worker, tool_name="Bash", tool_input={"command": "cargo test"}))); ok("command outside the worktree refused") assert not denied(fire("PreToolUse", dict(worker, tool_name="Bash", tool_input={"command": "cd %s && cargo test" % wt}))); ok("command inside the worktree allowed") assert denied(fire("PreToolUse", dict(worker, tool_name="apply_patch", tool_input={"command": "*** Begin Patch\n*** Add File: tests/a.rs\n+x\n*** End Patch"}))) +assert denied(fire("PreToolUse", dict(worker, tool_name="apply_patch", tool_input={"command": "*** Begin Patch\n\t*** Add File:tests/a.rs\n+x\n*** End Patch"}))) ok("patch outside the worktree refused") # time box from the worker's own rollout: soft token telemetry, then hard stop (no report call) via continue:false diff --git a/scripts/test/dsh-plugin.test.mjs b/scripts/test/dsh-plugin.test.mjs index b3d485ea..355ea46a 100644 --- a/scripts/test/dsh-plugin.test.mjs +++ b/scripts/test/dsh-plugin.test.mjs @@ -4,6 +4,7 @@ // dsh-runtime.test.mjs runs the same plugin on dsh itself; this one covers what a scripted model // cannot reach cheaply: the budget limits, the STUCK release, the cancel, the fallbacks. import { execFileSync } from "node:child_process" +import { createHash } from "node:crypto" import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { join } from "node:path" @@ -117,6 +118,17 @@ assert.ok(denied(await pre(h, root, "mcp__github__push_files", JSON.stringify(pu assert.ok(denied(await pre(h, root, "mcp__github__push_files", { ...pushFiles, arguments: JSON.stringify({ ...pushFiles, branch: "x" }) }), /ASK\[door\] push-default-branch/)) assert.ok(allowed(await pre(h, root, "mcp__github__push_files", { ...pushFiles, branch: "s1-01-x" }))) ok("an MCP tool's arguments reach the door gate as its input: push_files to the default branch is asked (a nested `arguments` key hides nothing), to a branch not") +// a once-door's digest is the MCP tool's own arguments wherever the agent runs, the digest any host prints +// for them: the human's `--once` opens the action the ASK named, from any directory +const share = { fileId: "f1", role: "reader", type: "anyone" } +const digestOf = (d) => (JSON.stringify(d).match(/--once ([0-9a-f]{64})/) || [])[1] +const shareHere = digestOf(await pre(h, root, "mcp__Google_Drive__share_file", share)) +mkdirSync(join(T, "elsewhere"), { recursive: true }); h.wd.set("root", join(T, "elsewhere")) +const shareThere = digestOf(await pre(h, root, "mcp__Google_Drive__share_file", share)) +h.wd.delete("root") +const want = createHash("sha256").update("mcp__Google_Drive__share_file\0" + JSON.stringify(share)).digest("hex") +assert.ok(shareHere === want && shareThere === want, `${shareHere} ${shareThere} ${want}`) +ok("an MCP tool's once digest is its own arguments, the same from any directory") // a live run: a question to the human must name a one-way door the gate stopped ([door:]) process.env.AGENTIC_AUTONOMY_MODE = "autonomous" const ask = (question, extra = {}) => ({ questions: [{ id: "q1", question, header: "Merge", multi_select: false, diff --git a/scripts/test/mission-doors.test.py b/scripts/test/mission-doors.test.py index cfc092dc..09da61f6 100644 --- a/scripts/test/mission-doors.test.py +++ b/scripts/test/mission-doors.test.py @@ -478,7 +478,7 @@ def ask_in(x, text, session="S1", env=None): with D.deadline(0.2): # the hook's read deadline: past it the gate fails closed (exit 2) while True: pass -except TimeoutError: +except D.Deadline: pass else: raise AssertionError("no deadline") @@ -724,6 +724,38 @@ def ids(tool, inp, cwd=None): return [d["id"] for d in D.classify(cwd or p.dir, tool, inp, CAT)] +# a patch is read as loosely as a host parses it: any spacing around a header's words, an indented header +# (Codex trims each line); and apply_patch run through the shell (Codex's heredoc and argument forms, +# applypatch, after a cd, fed by a pipe) judges its paths as writes where it runs +for head in ("*** Add File:.claude/settings.json", "\t*** Add File:\t.claude/settings.json ", + " *** Update File: src/x.py\n ***\tMove to:.codex/config.toml"): + assert ids("apply_patch", {"command": patch(head, "+{}")}) == ["change-agent-settings"], head +settings = patch("*** Add File: .claude/settings.json", "+{}") +for cmd in ("apply_patch <<'EOF'\n%sEOF" % settings, "apply_patch '%s'" % settings, "applypatch <<'EOF'\n%sEOF" % settings, + "cd .claude && apply_patch <<'EOF'\n%sEOF" % patch("*** Add File: settings.json", "+{}"), + "printf '%%s' '%s' | apply_patch" % settings, "timeout 9 apply_patch <<< '%s'" % settings): + assert ids("Bash", {"command": cmd}) == ["change-agent-settings"], (cmd, ids("Bash", {"command": cmd})) +assert ids("Bash", {"command": "apply_patch <<'EOF'\n%sEOF" % patch("*** Update File: src/x.py", "@@", "-a", "+b")}) == [] +assert ids("Bash", {"command": "echo '%s' > notes.txt" % settings}) == [] # a patch written to a file runs nothing +ok("apply_patch: headers with any spacing or indent, and apply_patch run through the shell, judge their paths") + +# a path through a symlink and then `..` is where the kernel lands (the link's target's parent), not where the +# words say; opencode.json is a door where OpenCode reads it (the project's, a worktree's, .opencode/, the user's) +os.makedirs(os.path.join(p.dir, ".claude", "sub"), exist_ok=True) +os.makedirs(os.path.join(p.dir, "src", "deep"), exist_ok=True) +os.symlink(os.path.join(p.dir, ".claude", "sub"), os.path.join(p.dir, "into-claude")) +os.symlink(os.path.join(p.dir, "src", "deep"), os.path.join(p.dir, "deep-link")) +for path in ("into-claude/../settings.json", os.path.join(p.dir, "into-claude/../settings.json"), "deep-link/../../opencode.json"): + assert ids("Write", {"file_path": path if os.path.isabs(path) else os.path.join(p.dir, path)}) == ["change-agent-settings"], path + assert ids("Bash", {"command": "echo {} > %s" % path}) == ["change-agent-settings"], path +for path in ("opencode.json", "home/worktrees/S1-01-T1/opencode.jsonc", ".opencode/opencode.json", "~/.config/opencode/opencode.json", + os.path.join(p.dir, "opencode.json")): + assert ids("Write", {"file_path": path}) == ["change-agent-settings"], path +for path in ("/tmp/opencode.json", "docs/examples/opencode.json", "src/opencode.json"): + assert ids("Write", {"file_path": path}) == [], path + assert ids("Bash", {"command": "echo {} > %s" % path}) == [], path +ok("paths: a symlink then `..` is resolved as the kernel does; opencode.json only where OpenCode reads it") + pushed = {"owner": "o", "repo": "r", "branch": "main", "files": [{"path": "a", "content": "b"}], "message": "m"} dsh_push = {"arguments": json.dumps(pushed), "workdir": p.dir} assert ids("mcp__github__push_files", dsh_push) == ids("mcp__github__push_files", pushed) == ["push-default-branch"] @@ -770,8 +802,17 @@ def ids(tool, inp, cwd=None): assert hook(g, "Bash", {"command": "git reset --hard", "workdir": gwt}, env=on).stdout == "", host assert "push-default-branch" in denied(hook(g, "Bash", dict(push, workdir=g.dir), env=on)) assert "discard-local-work" in denied(hook(g, "Bash", {"command": "git reset --hard", "workdir": g.dir}, env=on)) -for wd in ("home/feat-wt", os.path.join(g.dir, "gone"), 7): # not absolute, not there: the cwd decides +for wd in (os.path.join(g.dir, "gone"), "gone", 7): # not there (unknown: assumed), not a path (the cwd decides) assert "push-default-branch" in denied(hook(g, "Bash", dict(push, workdir=wd), env={"AGENTIC_HOST": "dsh"})), wd +# a relative workdir is where the host runs the command: under the payload's cwd (OpenCode's bash resolves it so) +for host in ("opencode", "dsh"): + on = {"AGENTIC_HOST": host} + assert hook(g, "Bash", dict(push, workdir="home/feat-wt"), env=on).stdout == "", host + assert hook(g, "Bash", dict(push, workdir="home/../home/feat-wt/"), env=on).stdout == "", host + os.makedirs(os.path.join(g.dir, ".claude"), exist_ok=True) + assert "change-agent-settings" in denied(hook(g, "Bash", {"command": "echo {} > settings.json", "workdir": ".claude"}, env=on)) + assert "push-default-branch" in denied(hook(g, "Bash", {"command": "git push origin HEAD", "workdir": "home/feat-wt/../.."}, + env=on)) assert "push-default-branch" in denied(hook(g, "Bash", push, env={"AGENTIC_HOST": "codex"})) ok("OpenCode and dsh: a bound worker's tool_input.workdir is where its command runs (its branch, its worktree)") @@ -843,6 +884,23 @@ def hook_in(payload, seconds=1): finally: D.verdict = real_verdict assert rc == 2 and out == "" and "BLOCK[doors]" in err and took < 3, (rc, out, err, took) + + +def swallowing(*a, **k): + """A step that catches its own errors (a git call's OSError, a parse's ValueError) as `except Exception`.""" + try: + time.sleep(10) + except Exception: # noqa: BLE001 — the deadline is not an Exception: this cannot turn a stall into an allow + return 0, [], [] + raise AssertionError("slept past the deadline") + + +D.verdict = swallowing +try: + rc, out, err, took = hook_in(stopping) +finally: + D.verdict = real_verdict +assert rc == 2 and out == "" and "BLOCK[doors]" in err and took < 3, (rc, out, err, took) ok("deadline: one deadline covers the whole decision; a held doors.lock or a hung verdict is exit 2 within it, never a hang") for x in ("hosts/opencode/aloop.js", "hosts/dsh/aloop.mjs", "bin/host-adapter", "bin/install"): @@ -857,6 +915,14 @@ def hook_in(payload, seconds=1): assert "change-agent-settings" in doors_of(cmd), cmd for cmd in ("cat .codex/config.toml", "jq . opencode.json", "cat ~/.dsh/profiles/aloop/cordis.patch.yml"): assert doors_of(cmd) == [], cmd +# a program that replaces, moves, links or removes a settings file changes it as a redirect does +for cmd in ("cp x .claude/settings.json", "mv /tmp/s .claude/settings.local.json", "ln -sf /tmp/h .codex/hooks.json", + "install -m 644 x ~/.codex/config.toml", "rm -f .claude/settings.json", "rsync -a x/ .opencode/plugins/aloop.js", + "sed -i 's/doors//' .claude/settings.json", "perl -pi -e 's/x//' opencode.json", "truncate -s 0 .codex/hooks.json", + "cd .claude && cp ../x settings.json"): + assert "change-agent-settings" in doors_of(cmd), (cmd, doors_of(cmd)) +for cmd in ("cp src/a.py src/b.py", "sed 's/x//' .claude/settings.json", "rm -rf build", "ln -s x docs/opencode.json"): + assert doors_of(cmd) == [], cmd assert p.run("doors", "check").returncode == 0 ok("settings: every host's settings and hooks and aloop's own host adapters are one-way; reading them is two-way") shutil.rmtree(g.dir, ignore_errors=True) diff --git a/scripts/test/mission-roles.test.py b/scripts/test/mission-roles.test.py index 151c6c98..1d06ea25 100644 --- a/scripts/test/mission-roles.test.py +++ b/scripts/test/mission-roles.test.py @@ -335,6 +335,7 @@ def supervisor_write(path): patch = "*** Begin Patch\n*** Update File: %s\n@@\n-a\n+b\n*** End Patch" % os.path.join(P, "src", "x.py") assert blocked(scope("apply_patch", E, command=patch), "source") +assert blocked(scope("apply_patch", E, command=patch.replace("*** Update File: ", " ***\tUpdate File:")), "source") assert allowed(scope("apply_patch", E, command=patch.replace(os.path.join(P, "src", "x.py"), os.path.join(MD, "eval", "e.json")))) assert blocked(scope("write", E, filePath="src/x.py"), "source") # OpenCode-shaped assert blocked(scope("NotebookEdit", E, notebook_path=os.path.join(P, "src", "n.ipynb"))) diff --git a/scripts/test/opencode-plugin.test.mjs b/scripts/test/opencode-plugin.test.mjs index da2d7f3a..10c9891b 100644 --- a/scripts/test/opencode-plugin.test.mjs +++ b/scripts/test/opencode-plugin.test.mjs @@ -45,6 +45,9 @@ const client = { abort: async (a) => { calls.abort.push(a); return { data: true } }, }, tui: { showToast: async (a) => { calls.toast.push(a) } }, + // the MCP servers OpenCode runs (their names as configured; a tool is _, sanitized) + mcp: { status: async () => ({ data: { github: { status: "connected" }, "Google Drive": { status: "connected" }, + notion: { status: "connected" }, drive: { status: "failed", error: "x" }, "drive_x": { status: "connected" } } }) }, } const { AgenticAgile } = await import(pathToFileURL(pluginFile).href) process.env.AGENTIC_STUCK_BLOCKS = "99" // the plugin captures its env at load; the stuck release is tested on a second instance @@ -121,6 +124,9 @@ assert.equal(writeArgs.filePath, join(wt, "tests/a.rs")); ok("bound worker's rel const patchArgs = { patchText: `*** Begin Patch\n*** Add File: ${join(T, "src/lib.rs")}\n+x\n*** End Patch` } await hooks["tool.execute.before"]({ tool: "apply_patch", sessionID: "child", callID: "8" }, { args: patchArgs }) assert.ok(patchArgs.patchText.includes(`*** Add File: ${join(wt, "src/lib.rs")}`)); ok("patch paths rewritten into the worktree") +const looseArgs = { patchText: `*** Begin Patch\n *** Add File:${join(T, "src/b.rs")}\n+x\n*** End Patch` } +await hooks["tool.execute.before"]({ tool: "apply_patch", sessionID: "child", callID: "8b" }, { args: looseArgs }) +assert.ok(looseArgs.patchText.includes(join(wt, "src/b.rs")), looseArgs.patchText); ok("a loosely spaced patch header is rewritten too") // the door gate reads a bound worker's command where it runs (tool_input.workdir, its worktree): a reset // there is two-way, the same reset in the main checkout (above) is not await hooks["tool.execute.before"]({ tool: "bash", sessionID: "child", callID: "d5" }, { args: { command: "git reset --hard" } }) @@ -221,8 +227,8 @@ process.on("exit", () => rmSync(S, { recursive: true, force: true })) mkdirSync(join(S, "plugin/bin"), { recursive: true }) for (const g of ["host-adapter", "gate-tooling", "gate-supervisor-scope", "gate-mission-scope", "gate-commit-author", "budget", "mission"]) { writeFileSync(join(S, "plugin/bin", g), `#!/bin/sh -cat >/dev/null S="$(dirname "$0")/../.." +cat > "$S/stdin.$(basename "$0")" echo "$(basename "$0") $*" >> "$S/calls" case "$(cat "$S/mode.$(basename "$0")" 2>/dev/null)" in hang) exec sleep 30 ;; @@ -248,9 +254,19 @@ const gated = async (tool, args) => { let g = await gated("bash", { command: "ls" }) assert.ok(!g.err && g.calls.at(-1) === "mission doors hook" && g.calls.at(-2) === "budget hook pre", g.calls.join(" | ")) ok("doors: the door gate runs last, after budget") -g = await gated("read", { filePath: join(T, "README.md") }) -assert.ok(!g.err && g.calls.length && !g.calls.some((c) => c.startsWith("mission")), g.calls.join(" | ")) -ok("doors: a read-only tool never spawns the door gate") +for (const tool of ["read", "codesearch"]) { + g = await gated(tool, { filePath: join(T, "README.md"), query: "x" }) + assert.ok(!g.err && g.calls.length && !g.calls.some((c) => c.startsWith("mission")), `${tool}: ${g.calls.join(" | ")}`) +} +ok("doors: a read-only tool (read, codesearch) never spawns the door gate") +for (const [tool, want] of [["github_create_pull_request", "mcp__github__create_pull_request"], + ["Google_Drive_share_file", "mcp__Google_Drive__share_file"], ["notion_search_slack_posts", "mcp__notion__search_slack_posts"], + ["drive_get_share", "mcp__drive__get_share"], ["drive_x_send", "mcp__drive_x__send"], ["lsp_hover", "lsp_hover"], ["bash", "Bash"]]) { + await gated(tool, { command: "ls" }) + const sent = JSON.parse(readFileSync(join(S, "stdin.mission"), "utf8")) + assert.equal(sent.tool_name, want, tool) +} +ok("doors: an MCP tool reaches the gates as mcp____ (OpenCode's servers, the longest first), so a word inside its name is no door") mode("budget", "deny") g = await gated("bash", { command: "ls" }) assert.ok(/BLOCK\[stub\]/.test(g.err?.message) && !g.calls.some((c) => c.startsWith("mission")), g.calls.join(" | ")) From 96f5c299118f46fb9acce4c73235e2d7c8c7aec6 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 14:30:17 +0000 Subject: [PATCH 07/14] ROADMAP M9.54: the static evidence for Claude Code's `!` command Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- docs/ROADMAP.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index d78f3dc3..66193c3a 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -488,7 +488,7 @@ Each task flips only its own row here and in TRACKING.md. | M9.51 | Lifting a block (`mission tick --force --by`) is the human's own words: it asks (exit 3) for their typed `lift ` line, the code bound to this occurrence of the block (its key, the lifts of it so far, and the handout that tripped it or the newest failed goal), and records a `mission.lifted` ledger row (`APPROVAL_EVENTS`, `GRANT_VERBS`); a repeat restarts only at that row's own run-log event (first copy), and the 3-failed-goals count only up to the goals the human lifted; `/aloop:mission` counts a lift among the typed grants. Reason: found after M9.17 merged, `--by` alone still lifted a block and the lift was run data, so a fresh checkout blocked again (M9.24's first part) | M9.8, M9.12, M9.17 | `plugin/mission/tick.py`, `plugin/bin/_mission.py`, `plugin/bin/_events.py`, `plugin/skills/mission/`, `scripts/test/mission-{tick,skill}.test.py` | yes | done: `mission-tick.test.py` check 30 (`--by` alone, a run-log-only or replayed lift, a negated line, another code, an old line and a line typed for an earlier block lift nothing; a far `--now` lifts no later goals; a fresh checkout keeps a lift), `mission-skill.test.py` check 8 | | M9.52 | `tick` reads a gap list only when it still recomputes from its inputs, as `evaluate check` does (an intact eval that a changed `compute`, an edited journey or goal no longer reproduces asks for `EVALUATE`). Reason: found in M9.27's review, `tick` reads the record, shape and seal but not the recomputation (the tick test's synthetic evals cannot recompute), so after an upgrade that changes `compute` `goal derive` refuses while `tick` hands out `DERIVE_GOAL` until it blocks | M9.27 | `plugin/mission/tick.py`, `mission-tick.test.py` (real evals in its synthetic sections) | yes | open | | M9.53 | `tick` hands out a goal's commands (`PLAN_INTENTS`, `SPRINT`, its close) only when `goal check` passes, else names the goal's problem. Reason: found in M9.27's review, a goal whose source eval changed or is gone (or one derived before evals were ledger records) gets `PLAN_INTENTS` 3 times while `goal intents` refuses, then `BLOCKED` with no cause named (M9.43 is one case of it) | M9.11, M9.28 | `plugin/mission/tick.py`, `mission-tick.test.py` | yes | open | -| M9.54 | Measure on each host whether the human's own `!` shell command reaches PreToolUse (it should pass ungated: the human crossing a door is the authorization), and whether a subagent keeps the core block after a compaction (SessionStart `compact` re-injects it for the root only). Reason: M9.31 left both unmeasured | M9.31 | live runs, `plugin/hooks/hooks.json` | yes | open | +| M9.54 | Measure live on each host whether the human's own `!` shell command reaches PreToolUse (it should pass ungated: the human crossing a door is the authorization; on Claude Code the 2.1.304 binary's bash mode calls the Bash tool directly with `userTypedShellDispatch`, static evidence only), and whether a subagent keeps the core block after a compaction (SessionStart `compact` re-injects it for the root only). Reason: M9.31 left both unmeasured | M9.31 | live runs, `plugin/hooks/hooks.json` | yes | open | | M9.55 | Codex's shell tool takes a `workdir` that its hook payload is not known to carry: verify, and read it as the door gate reads OpenCode's and dsh's. Reason: the M9.31 review; a Codex command run elsewhere is judged in the session's directory | M9.31 | `plugin/mission/doors.py`, `codex-hooks.test.py` | yes | open | | M9.56 | Agent settings the catalogue does not place: a copy into a settings directory (`cp settings.json .claude/`), a script that writes them (`python -c`, M9.47's class), a custom `CODEX_HOME`, `DSH_HOME` or `OPENCODE_CONFIG`, an `opencode.json` above the project, and Bash writes to the installed plugin (`plugin_paths` cover edits only). Reason: the M9.31 review | M9.31 | `plugin/mission/doors.{json,py}`, its test | yes | open | | M9.57 | Confine MCP tools to a worker's worktree (host-adapter confines Bash, edits and patches; a filesystem MCP server writes anywhere), and read `apply_patch` run through the shell in host-adapter and `gate-mission-scope` as the door gate does. Reason: the M9.31 review | M9.9 | `plugin/bin/host-adapter`, `plugin/bin/gate-mission-scope` | yes | open | From 8bcd9deb98127e4ca267a57d4a2665af15d15152 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 14:49:06 +0000 Subject: [PATCH 08/14] M9.31: second review fixes (groups, conditional cd, copies, symlinks, MCP names, BOM) - A bare redirect, and a group's, subshell's or compound's own redirect or heredoc, reach the commands inside (views() follows "(" ")" "cwd+" "cwd|" "cwd-"; simple() emits a write-only view for a Cmd with no words). - A cd after && or || leaves the directory unknown once its chain ends. - git -C, cd -P and a dsh workdir walk a symlink before `..` (an Either context of the lexical and the kernel path); paths also match through a glob and relative to the checkout that holds them. - copied(): cp/mv/ln/install/rsync write DEST, DIR/NAME and DIR/*; removing or moving a directory that holds settings, sed's abbreviated --in-place and OpenCode's config.json are the settings door. - MCP tool names: hyphenated servers and aggregators (zapier, composio) reach send-external-message, visibility-or-sharing and push-default-branch; a service word inside a read tool's name is no door. - plugin/bin/_patch.py is the one apply_patch header reader (U+FEFF counts as space, as OpenCode's trim does), shared by doors, host-adapter and both scope gates; OpenCode's header rewrite takes the whole line. - ROADMAP: M9.31 text no longer claims the scope gates read the shell form (M9.58); M9.57 lists the remaining settings writers; M9.61 computed paths. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- CHANGELOG.md | 2 +- docs/ROADMAP.md | 7 +- plugin/bin/_patch.py | 15 +++ plugin/bin/gate-mission-scope | 6 +- plugin/bin/gate-supervisor-scope | 11 +- plugin/bin/host-adapter | 5 +- plugin/hosts/opencode/aloop.js | 2 +- plugin/mission/doors.json | 63 +++++++--- plugin/mission/doors.py | 196 +++++++++++++++++++++++++---- scripts/test/mission-doors.test.py | 80 ++++++++++++ 10 files changed, 330 insertions(+), 57 deletions(-) create mode 100644 plugin/bin/_patch.py diff --git a/CHANGELOG.md b/CHANGELOG.md index ac9e1761..a9c44622 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ tag in lockstep. Tag releases as `vMAJOR.MINOR.PATCH`. ### Added - Plan approval is a gate (ROADMAP M0.9): `gate-dispatch` starts no execution role (RED, SCAFFOLD, GREEN, structural review, final gate) of a sprint until the human approved that sprint's plan as it is now. `events authority --sprint N` exits 3 with the line to type, `approve SN ` (reviewed, the code naming the sha256 of the sprint's plan files) or `autonomous SN ` (autonomy for that sprint, re-plans included, the code naming the authority record); the task contract's `STORY_DIR` and task must be a live story and task of that plan's frozen `ids.json`; the line must be among the human's captured prompts and typed after the sprint's last decision (M9.17's mechanism). A changed plan, a re-plan in reviewed mode, a roll-back to an old line, another sprint's line, a negated line and a hand-edited or emitted record all block (`plan.approved` and `human.approval` cannot be `events emit`ted); an approved autonomous mission grants its own sprints autonomy with no line. `events approval SPRINT|TASK_ID` is the check. Codex's `spawn_agent` (`agent_type`, `message`) and OpenCode's `task` now reach `gate-dispatch` too, and `claude -p`, `codex exec` and `opencode run` from a tool call are blocked in every aloop project, not only where a mission is set. `e2e-live.sh` pre-approves with an `autonomous S1 ` line in its prompt. Reason: the north star allows no human help beyond approval, and the approval was a playbook rule a supervisor could skip. A 4-lens adversarial review found 9 defects: 7 fixed with a check red on the first head (cross-sprint or unplanned `STORY_DIR`, a retyped line kept stale, mission autonomy over a non-mission sprint, `transcripts prune` dropping the prompts grants cite, `codex plugin add` refused, a code past column 200, a crash on a malformed run-log line), 2 in the playbook (intake reuses a mission's handed-off intent; a reviewed re-plan waits for the human's new line). `transcripts prune` now keeps the human's prompts. Test: `plan-approval.test.py` (20 checks, red on master), plus the host suites. - Core agent principles and the one-way / two-way door rule (ROADMAP §4.12, M9.30): seven core principles from Amazon's Leadership Principles, the door rule first, go to every aloop agent at session start (`aloop mission principles show --core`). A two-way door (a branch, a local edit, a test, a draft PR) is decided, not asked. A one-way door (`aloop mission doors list`: a push or merge to the default branch, a force-push, a release, a deploy, a message sent outside, a secret, dropped data, …) is stopped by a PreToolUse gate with `ASK[door] ` until the human records an authorization in their own terminal (`aloop mission doors authorize ID --by NAME --reason TEXT [--until DATE] [--once DIGEST]`, a hash-chained ledger row); a wall (switching a gate off, forging a human record) is never crossed. While a run is live, a question to the human must name a door the gate stopped and nobody authorized. Reason: Adeel's request that agents decide two-way doors themselves and ask only at a one-way door not already authorized. Test: `mission-doors.test.py`. Hardened by an adversarial review (15 findings, each a test): the gate reads Bash as the shell does (comments, line continuations, heredoc bodies, `xargs`, `find -exec`, `echo … | sh`), a branch condition reads the directory the command acts on (`git -C`, `cd`), settings are guarded wherever they live, dry runs and `command -v` are two-way, the newest decision by time counts and a revoke holds on every branch, a once-grant is crossed once under a lock, a broken ledger is named, and `doors authorize` refuses an agent's shell. -- The door rule on every host and role (ROADMAP §4.12, M9.31): every dispatched role gets the core principles (`aloop mission principles hook` at SubagentStart, Codex agents' instructions, OpenCode's system prompt, each dsh role persona), and the door gate runs on Codex, OpenCode and dsh with each host's question tool mapped. In the OpenCode and dsh plugins it runs last, after every other gate admitted the call, and fails closed after 15 s. A bound worker's command is judged where it runs (its `workdir`), an MCP tool by its own arguments (`mcp____` on OpenCode too), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Hardened by a review (12 findings, each a test): a stalled decision can no longer fall through to an allow, `apply_patch` is read with any header spacing and through the shell, a symlink then `..` is read as the kernel walks it, and replacing a settings file with `cp`, `mv`, `ln`, `install`, `rsync`, `rm`, `sed -i` or `perl -i` is the settings door. Tests: `mission-doors.test.py`, `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`. +- The door rule on every host and role (ROADMAP §4.12, M9.31): every dispatched role gets the core principles (`aloop mission principles hook` at SubagentStart, Codex agents' instructions, OpenCode's system prompt, each dsh role persona), and the door gate runs on Codex, OpenCode and dsh with each host's question tool mapped. In the OpenCode and dsh plugins it runs last, after every other gate admitted the call, and fails closed after 15 s. A bound worker's command is judged where it runs (its `workdir`), an MCP tool by its own arguments (`mcp____` on OpenCode too), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Hardened by two reviews (19 findings, each a test that failed first): a stalled decision can no longer fall through to an allow, `apply_patch` is read with any header spacing (a U+FEFF too) and through the shell, a bare redirect or a group's redirect reaches the commands inside, a `cd` that may not run leaves the directory unknown, a symlink then `..` is read as the kernel walks it, replacing a settings file with `cp`, `mv`, `ln`, `install`, `rsync` (into a settings directory too), `rm`, `sed -i` or `perl -i`, or removing a directory that holds one, is the settings door, and an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`) crosses the same door as the service's own. Tests: `mission-doors.test.py`, `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`. - DeepSeek Harness (dsh) host (ROADMAP §4.9): `aloop install dsh` (alias `deepseek`; `--project P` or `--global`) renders a `dsh --profile aloop` profile in `$DSH_HOME` and installs the skills. Its native plugin (`plugin/hosts/dsh/aloop.mjs`) runs the same gates as Claude Code on dsh's own events: a deny before a tool runs, a block after an invalid artifact write, and a stop gate that steers a role back up to 3 times before the supervisor is told it is BLOCKED (or STUCK, released unverified). Roles are agent presets on two subagent tools, one per model route (`deepseek-v4-pro` for planning, `deepseek-flash` for work by default; `MODEL_PLANNING_DSH` / `MODEL_WORKER_DSH` override). A writer's `aloop bind` moves its working directory into its worktree, and `host-adapter` confines its commands, edits and `working_directory` there. Usage is recorded Claude-shaped per role, so budgets, the ledger and `run-summary` work unchanged. `make e2e-live HOST=dsh` drives it live and exits 2 with the fix when no `DEEPSEEK_API_KEY` is set. Reason: DeepSeek's own harness is the cheapest route to a strong model. The profile records the project it serves (or `--global`), so another project's `aloop-init` never re-renders it, and gates get the worker shell's environment (no API key). The mission layer's six roles run on dsh too: presets on their route group's subagent tool, `gate-mission-scope` on every write and command, and `gate-mission-artifact` at each role's start and stop; the mission skill installs as `aloop-mission`. A second review (3 lenses; 11 confirmed findings, each fixed with a test) closed: a working directory that vanished (dsh's tools move it back to the main tree, so the plugin judges `ensure()`'s directory and `host-adapter` refuses to run from a missing one), gate switches from the project's `.env` (dsh loads it; `SKIP_HOOKS`, `GATE_RUN_TESTS`, `REPO_DIR`, `AGENTIC_*` from it never reach a gate), an interrupted Stop that dropped the sprint-close notice, and the shared profile: its owner is the main tree, an older unrecorded profile is claimed only by the project that installed it, and a project it does not serve is told so by `install status`, `aloop-init` and the session itself. Tests: `dsh-plugin.test.mjs` (40 checks, emulated), `dsh-runtime.test.mjs` (32 checks on the real pinned dsh 0.2.1-rc.2 with a scripted model; the CI hosts job installs it) and `dsh-install.test.py` (9). - Brand kit and repo front page: `docs/brand/` holds the mark (an **a** whose bowl is a red-to-green loop passing a gold gate, with a 16 px cut), lockups, light and dark README banners, favicons, app icons, the site's OG card and the GitHub social preview, all generated by `docs/brand/gen_brand.py` from one palette with WCAG checks (rules in `docs/brand/README.md`). The README leads with the mission layer, the agent principles and the door rule, the supported hosts and the measured demo runs; the site carries the favicon and Open Graph/Twitter card metadata; `SECURITY.md` and bug/feature issue forms are new. - A mission's gap lists count only as `mission evaluate` computed them (ROADMAP §4.12, M9.27): `evaluate` records `eval.completed` in the committed ledger, and `mission tick`, `evaluate check` and `goal` take the newest eval whose content that record covers, with a matching seal, in the ledger's order. An eval file written by hand, or edited (or re-stamped) after it was recorded, no longer sends the loop to `RESEARCH` as "goal met" or makes `goal derive` refuse what tick handed out, a clock that is behind no longer reorders them, and a fresh checkout derives and closes goals from the evals it committed. An eval or state named as journey or baseline evidence needs its record too. Tests: `mission-tick` check 32, `mission-journeys`. diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 1ad4dd47..f79dda99 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -466,7 +466,7 @@ Each task flips only its own row here and in TRACKING.md. | M9.28 | `tick` drives the goal commands (M9.11): a goal counts only with its `goal.derived`/`goal.approved` record and closes by `goal.met`; handouts name `goal derive`, `options` (spoon-fed), `intents`, `next` and `close`; an eval narrowed to a closed goal asks for a whole-mission eval. Reason: found when M9.11 landed, `tick` read goal files and their status, so a goal nobody derived steered the loop and a met goal was never closed | M9.8, M9.11 | `plugin/mission/tick.py`, `plugin/skills/mission/SKILL.md`, `mission-tick.test.py` | yes | done: `mission-tick.test.py` (29 checks; a real goal run on the real clock: derive → review → intents → next and a sprint per intent → narrowed eval → close → whole-mission eval → `RESEARCH`) | | M9.29 | A goal that crosses a hard line stops being the active goal (`evaluate.active_goal` skips it, or a recorded retirement). Reason: found wiring M9.28, `evaluate` keeps narrowing to a hard-lined goal and `goal derive` refuses a narrowed eval, so the mission deadlocks (`tick` hands it to the human meanwhile) | M9.10, M9.11 | `plugin/mission/{evaluate,goal}.py`, their tests | yes | done: `evaluate.active_goal` skips a goal an ethics hard line retired (it never closes), so the next eval covers the whole mission; `goal derive` from an eval narrowed to a retired goal says to evaluate again; `tick` hands out EVALUATE for such an eval, then DERIVE_GOAL, which takes the next gap (a hard line holds the retired goal's own gaps); when every open gap is held it is BLOCKED for the human (a change of the mission), not handed to a derive that would refuse. `mission-goal.test.py` (section 11), `mission-tick.test.py` (section 27). Call: retirement is derived from the hard line itself, not a new record, so it holds on a fresh clone with the ledger | | M9.30 | Core agent principles and the one-way / two-way door rule, enforced: seven core principles from Amazon's Leadership Principles (the door rule first) for every agent, injected at session start; a door catalogue, the human's standing or once authorization in the ledger, and a PreToolUse gate that stops an unauthorized one-way door (ASK) and a wall (BLOCK) and lets two-way doors pass; while a run is live a question must name a door the gate stopped. Reason: Adeel, 2026-10-20: "If it is two-way, then the agent should take the decision directly without bothering the user. But if it is one-way door, then it can bother user if user already have not specified to cross that door." | M9.0, M9.7 | `plugin/mission/doors.{py,json}`, `principles.{md,py}` (`## Core`), `plugin/bin/session-start`, `hooks.json` (one line), core: `_events.py` / `_mission.py` (door events), `events` (`GATE_OWNED`), `scripts/test/mission-doors.test.py` | after M9.7 | done: `mission doors list / classify / authorize / revoke / check / hook`, `mission principles show --core`; `mission-doors.test.py` (21 checks), `mission-principles.test.py` (+1 check). Calls: anything the catalogue does not list is two-way (a finite list cannot claim full enforcement, §7); Bash is matched by program and arguments after reading the line as the shell does, with the scope gate's reader (`_shellread.py`, M9.38: quotes, comments, line continuations, `$(…)`, a heredoc's body as data unless a shell or python runs it) and unwrapping `sudo`, `env`, `sh -c`, `eval`, `xargs`, `find -exec` and `echo … | sh`, so text that only mentions a door (`grep`, a commit message, a file written by a heredoc) stays two-way; a `when` condition (on the default branch, the main checkout) reads the directory the command acts on (`git -C DIR`, `cd DIR &&`), and one the gate cannot resolve counts; grants are standing (until revoked or `--until`), once (one action's digest, 24 h, used up by its first crossing, recorded in the ledger), never (a wall: switching a gate off, forging a human record; refused even for the human's `authorize`) and command (question-only doors answered by their own command); authorizations are hash-chained human ledger rows (an edited row, `events emit` or a stream-only event opens nothing; `HUMAN_EVENTS` are now gate-owned, which also closes a forged `baseline.vetoed`); a question is checked only while a run is live (an approved autonomous mission or an autonomous run, or a sprint whose latest plan is approved, for the session holding its lock), so intake, planning and the plan's own approval are asked; the core block is kept out of the goal blocks, so editing it never stales a goal's stamp; the hook fails closed (exit 2) and is silent outside an aloop project; measured: median 81 ms per two-way call, 102 ms per stopped one-way call (30 calls each, in parallel with the other `*` hooks). Calls from its adversarial review (15 confirmed findings over 3 lenses, each now a test that failed before its fix): the newest decision about a door is the newest by time (a union merge that puts an older authorization last reopens nothing), and the run log's revokes and crossings close on any branch of the checkout; a once-grant is checked and crossed under a lock (`.aladdin/state/doors.lock`); a broken ledger is named in the ASK, fails `doors check` and blocks `authorize` with the restore line; settings and the ledger are matched by path suffix (`~/.claude`, a worktree's own); a dry run, a help page and `command -v` are two-way, `--dry-run=none` is not; `git -c core.hooksPath=` is a wall and `git config core.hooksPath VALUE` a one-way door; arguments `xargs` adds from stdin may be any flag of a one-way door; `doors authorize` refuses an agent's shell (`CLAUDECODE` set: the human authorizes in their own terminal); Slack's real send tools (`slack_post_message`, `slack_reply_to_thread`) are one-way; a hard ethics line only the run log holds (recorded before the ledger did) still binds. Calls from its second review (26 confirmed findings over parser, catalogue and state lenses, each now a test that failed before its fix): the gate reads Bash with `_shellread.py` (M9.38), which now keeps each heredoc's body, the operator that ends a command and heredoc bodies inside `$(…)`, so touching operators (`); git push`), keywords, groups, function bodies, here-strings, IO-number redirects (`2>&1`) and quoted operators (`find \( … \) -exec`) hide nothing, a quote in `"$(cat <<'EOF' … EOF)"` is data, stdin (a heredoc, a here-string, `echo|printf|cat … |`) reaches only the command it feeds (a shell with options or `-s`, a python with `-`), and `pushd`/`popd`, `sh -c`, `eval` and a fed shell keep the directory; nesting deeper than 8 is unreadable and a read past 8 s fails closed (exit 2, under the hook's 15 s); python is a forge only when it calls a writer, `npm|pnpm|yarn -g` a system change only with an installing verb, the ledger only at `docs/agents/mission/ledger.jsonl`, `xargs … doors` only when stdin can be the subcommand (`-I R` replaces R), a secret command's dry run or help page is two-way, `git push origin HEAD|@` on the default branch is one-way, and erasing or editing the run log (`.aladdin/logs/events.jsonl`, whose revokes and crossings close doors) is a wall; a ledger row is written before its stream event and `ledger_problems` reads the stream first, so no gate sees another writer's half-written record as a lost row; a record is dated no earlier than any ledger row, so the human's fresh decision is the newest even after a merged future-dated row; a run stops being live when its plan's sprint closes (`sprints.json`) or a new intent arrives; a malformed run-log line never crashes the ethics check | -| M9.31 | Door rule on every host and role: the core block in SubagentStart for dispatched roles, Codex `developer_instructions`, OpenCode agents and the dsh profile; the hook in Codex PreToolUse, the OpenCode plugin (`aloop.js`) and the dsh plugin (`aloop.mjs`, whose PreToolUse list mirrors `hooks.json` by hand and so lacks `mission doors hook`), with each host's question tool mapped. Also measure whether a human's own `!` shell command reaches PreToolUse (it should not be gated: the human crossing a door is the authorization). Reason: M9.30 wires Claude Code's main session only | M9.30, M9.9 | `plugin/bin/host-adapter`, `install`, `aloop.js`, `aloop.mjs`, `_host.py` | after M9.9 | done: the core block reaches every role on every host (`mission principles hook`, the third SubagentStart command; Codex agents' `developer_instructions`; OpenCode's system transform for a sub-agent and a resumed root; each dsh role persona, written by `install`). The door gate runs on Codex through `hooks.json`, and last in the OpenCode and dsh plugins (after every other gate admitted the call, so only a call that runs uses up a once-authorization; read-only tools skip it; 15 s, fail closed). Each host's question tool is the gate's (`AskUserQuestion`, Codex `request_user_input`, OpenCode `question`, dsh `ask_user_question`). A bound worker's command is read where it runs (`tool_input.workdir`, a relative one under the project), a dsh MCP tool's input is its own arguments (its once digest is the one any host prints), and an OpenCode MCP tool is `mcp____` (from `client.mcp.status()`, the longest server first). One deadline covers the decision (`Deadline`, which no step's `except Exception` can swallow into an allow), and a subagent stopped at a door reports `ASK[door]` to its supervisor. A review (12 findings, each a test that failed first) added: `apply_patch` read as the hosts parse it (any header spacing or indent, and through the shell: Codex's heredoc and argument forms, `applypatch`, after a `cd`, from a pipe), also in host-adapter and both scope gates; a path through a symlink then `..` read as the kernel walks it; settings replaced, moved, linked or removed by `cp`/`mv`/`ln`/`install`/`rsync`/`rm`/`sed -i`/`perl -i`; `opencode.json` a door only where OpenCode reads it. Tests: `mission-doors.test.py` (31 checks), `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`, `hook-matchers.test.py`, `mission-principles.test.py`, `dsh-install.test.py`. Calls: the catalogue keeps its generic `_` alternatives for an OpenCode that cannot list its servers (an extra question beats a silent one-way door). Open: M9.48, M9.49, M9.55–M9.60 (the `!` measurement moved to M9.55) | +| M9.31 | Door rule on every host and role: the core block in SubagentStart for dispatched roles, Codex `developer_instructions`, OpenCode agents and the dsh profile; the hook in Codex PreToolUse, the OpenCode plugin (`aloop.js`) and the dsh plugin (`aloop.mjs`, whose PreToolUse list mirrors `hooks.json` by hand and so lacks `mission doors hook`), with each host's question tool mapped. Also measure whether a human's own `!` shell command reaches PreToolUse (it should not be gated: the human crossing a door is the authorization). Reason: M9.30 wires Claude Code's main session only | M9.30, M9.9 | `plugin/bin/host-adapter`, `install`, `aloop.js`, `aloop.mjs`, `_host.py` | after M9.9 | done: the core block reaches every role on every host (`mission principles hook`, the third SubagentStart command; Codex agents' `developer_instructions`; OpenCode's system transform for a sub-agent and a resumed root; each dsh role persona, written by `install`). The door gate runs on Codex through `hooks.json`, and last in the OpenCode and dsh plugins (after every other gate admitted the call, so only a call that runs uses up a once-authorization; read-only tools skip it; 15 s, fail closed). Each host's question tool is the gate's (`AskUserQuestion`, Codex `request_user_input`, OpenCode `question`, dsh `ask_user_question`). A bound worker's command is read where it runs (`tool_input.workdir`, a relative one under the project), a dsh MCP tool's input is its own arguments (its once digest is the one any host prints), and an OpenCode MCP tool is `mcp____` (from `client.mcp.status()`, the longest server first). One deadline covers the decision (`Deadline`, which no step's `except Exception` can swallow into an allow), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Two reviews (12 and 7 findings, each a test that failed first) added: `apply_patch` read as the hosts parse it (any header spacing or indent, a U+FEFF around the path cut as OpenCode's `trim()` cuts it, and through the shell: Codex's heredoc and argument forms, `applypatch`, after a `cd`, from a pipe; the header reader `_patch.py` is shared with host-adapter and both scope gates, but only the door gate reads the shell form, M9.58); a bare redirect, and a group's or compound's own redirect or heredoc, reach the commands inside; a `cd` that may not run leaves the directory unknown; a path through a symlink then `..` read as the kernel walks it (`git -C`, `cd -P`, a dsh `workdir`); settings replaced, moved, linked or removed by `cp`/`mv`/`ln`/`install`/`rsync` (into a settings directory too, by `-t` or a trailing `DIR/`), `rm`/`mv` of a directory that holds them, a glob that names them, `sed -i` (and `--in-pl`), `perl -i`; `opencode.json` a door only where OpenCode reads it (a nested checkout's root, `~/.config/opencode`, its `config.json`); an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`, `mcp__google-drive__share_file`) crosses the same door as the service's own server, while a service word inside another tool's name (`search_slack_posts`, `get_post_history`) is none. Tests: `mission-doors.test.py` (36 checks), `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`, `hook-matchers.test.py`, `mission-principles.test.py`, `dsh-install.test.py`. Calls: the catalogue keeps its generic `_` alternatives for an OpenCode that cannot list its servers (an extra question beats a silent one-way door). Open: M9.48, M9.49, M9.55–M9.61 (the `!` measurement moved to M9.55) | | M9.32 | Intent questions name their door: `open_questions[].door`; `intent check` blocks an assume/default on a one-way door and treats an `ask` on a door the human authorized as answered. Reason: the plan-time half of the door rule | M9.30, M9.11 | `plugin/bin/intent`, its test | yes | open | | M9.33 | Remove the model-facing `SKIP_HOOKS=1` hints (`gate-supervisor-scope`, `_failclosed.sh`, `SKILL.md`). Reason: they hand the agent the bypass-gate wall's own key | M9.30 | those files | yes | open | | M9.34 | A git pre-push backstop from `aloop init` for pushes to the default branch and force pushes. Reason: holds when Bash matching is evaded (a script, an alias) | M9.30 | `plugin/bin/aloop-init`, its test | yes | open | @@ -491,10 +491,11 @@ Each task flips only its own row here and in TRACKING.md. | M9.54 | What the scope gate's Bash reader still cannot follow, each a known residual: a `cd` to a computed dir (writes relative to it go unjudged), a `cd` that fails before a `;`, `cd`s that accumulate over loop iterations, a case `;&` fall-through, `eval` with a redirect, a function body's writes judged where it is defined, and interpreter code (`python3 -c`). Reason: the M9.9 round-4 review; the reader follows each state a command may leave the shell in (`walk`/`fork`), but these need running it, so M9.41's tree diff at a role's stop is the backstop | M9.9, M9.41 | `plugin/bin/gate-mission-scope`, `plugin/bin/_shellread.py`, `mission-scope-bash.test.py` | after its needs | open | | M9.55 | Measure live on each host whether the human's own `!` shell command reaches PreToolUse (it should pass ungated: the human crossing a door is the authorization; on Claude Code the 2.1.304 binary's bash mode calls the Bash tool directly with `userTypedShellDispatch`, static evidence only), and whether a subagent keeps the core block after a compaction (SessionStart `compact` re-injects it for the root only). Reason: M9.31 left both unmeasured | M9.31 | live runs, `plugin/hooks/hooks.json` | yes | open | | M9.56 | Codex's shell tool takes a `workdir` that its hook payload is not known to carry: verify, and read it as the door gate reads OpenCode's and dsh's. Reason: the M9.31 review; a Codex command run elsewhere is judged in the session's directory | M9.31 | `plugin/mission/doors.py`, `codex-hooks.test.py` | yes | open | -| M9.57 | Agent settings the catalogue does not place: a copy into a settings directory (`cp settings.json .claude/`), a script that writes them (`python -c`, M9.47's class), a custom `CODEX_HOME`, `DSH_HOME` or `OPENCODE_CONFIG`, an `opencode.json` above the project, and Bash writes to the installed plugin (`plugin_paths` cover edits only). Reason: the M9.31 review | M9.31 | `plugin/mission/doors.{json,py}`, its test | yes | open | -| M9.58 | Confine MCP tools to a worker's worktree (host-adapter confines Bash, edits and patches; a filesystem MCP server writes anywhere), and read `apply_patch` run through the shell in host-adapter and `gate-mission-scope` as the door gate does. Reason: the M9.31 review | M9.9 | `plugin/bin/host-adapter`, `plugin/bin/gate-mission-scope` | yes | open | +| M9.57 | Agent settings the catalogue does not place: another program that writes them (`git checkout`/`restore` of one, `find … -delete`, `curl -o`, `tar -x`, `dd of=`, GNU `patch`), a script that writes them (`python -c`, M9.47's class), a custom `CODEX_HOME`, `DSH_HOME` or `OPENCODE_CONFIG`, an `opencode.json` above the project, and Bash writes to the installed plugin (`plugin_paths` cover edits only). Reason: the M9.31 review | M9.31 | `plugin/mission/doors.{json,py}`, its test | yes | open | +| M9.58 | Confine MCP tools to a worker's worktree (host-adapter confines Bash, edits and patches; a filesystem MCP server writes anywhere), and read `apply_patch` run through the shell in `gate-mission-scope` as the door gate does (`_patch.paths`, judged against the `cd` dir: an evaluator's shell patch can write source today). host-adapter and the supervisor gate confine no shell write by path, so reading only the patch there would be cosmetic. Reason: the M9.31 reviews | M9.9 | `plugin/bin/host-adapter`, `plugin/bin/gate-mission-scope` | yes | open | | M9.59 | In the OpenCode plugin only the door gate has a fail-closed timeout: a hung host-adapter, scope or budget gate waits 10 minutes and its timeout reads as an allow (no exit status, no deny). Give each gate the hook's timeout and fail closed. Reason: the M9.31 review | M9.31 | `plugin/hosts/opencode/aloop.js`, its test | yes | open | | M9.60 | An OpenCode that cannot list its MCP servers (`client.mcp.status()` fails) hands the gates raw `_` names, which the catalogue's generic alternatives read with false positives (`drive_get_share` asks). Read the server names from the config too. Reason: the M9.31 review | M9.31 | `plugin/hosts/opencode/aloop.js` | yes | open | +| M9.61 | A path the shell computes inside a patch or a write target: an unquoted heredoc's `$HOME`/`$(…)` in an `apply_patch` header, or `> "$D/settings.json"`, is judged as written (M9.47's class). Expand what the line assigns before it, and ask for a computed path under a settings directory. Reason: the second M9.31 review | M9.47 | `plugin/mission/doors.py`, its test | yes | open | **Parallel tracks** (one thread each, from M9.0 on master): (1) Research + Judges, M9.1 + M9.5; (2) Baseline + Journeys → Assess/Evaluate, M9.2 + M9.3 → M9.10; (3) Ethics + Principles → Goals, diff --git a/plugin/bin/_patch.py b/plugin/bin/_patch.py new file mode 100644 index 00000000..d98be8b2 --- /dev/null +++ b/plugin/bin/_patch.py @@ -0,0 +1,15 @@ +"""The paths an apply_patch patch adds, updates, deletes or moves to, read as loosely as a host parses a +header: Codex trims each line (Rust's trim), OpenCode cuts the path after the colon and trims it (JS trim, +which also cuts a U+FEFF). One copy for the door gate, host-adapter and both scope gates.""" +import re + +WS = r"(?:[^\S\n]|)" +PATCH = re.compile(r"^{0}*\*\*\*{0}*(?:Add|Update|Delete){0}+File:{0}*(.+)$|^{0}*\*\*\*{0}*Move{0}+to:{0}*(.+)$".format(WS), + re.M) +EDGE = re.compile(r"^(?:\s|)+|(?:\s|)+$") +PATCHERS = ("apply_patch", "applypatch") # Codex's patch tool run as a program (its argument or stdin form) + + +def paths(text): + """Each path a patch names, trimmed of what either host's trim cuts (Python's whitespace and JS's).""" + return [x for x in (EDGE.sub("", a or b) for a, b in PATCH.findall(text or "")) if x] diff --git a/plugin/bin/gate-mission-scope b/plugin/bin/gate-mission-scope index e79cf597..9171e04f 100755 --- a/plugin/bin/gate-mission-scope +++ b/plugin/bin/gate-mission-scope @@ -76,12 +76,12 @@ import sys import tempfile sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) +import _patch # noqa: E402 import _shellread as SR # noqa: E402 GATE = "mission-scope" EDIT_TOOLS = ("Write", "Edit", "MultiEdit", "NotebookEdit", "write", "edit") SHELL_TOOLS = ("Bash", "bash", "shell") -PATCH_PATHS = re.compile(r"^[^\S\n]*\*\*\*[^\S\n]*(?:Add|Update|Delete)[^\S\n]+File:[^\S\n]*(.+)$|^[^\S\n]*\*\*\*[^\S\n]*Move[^\S\n]+to:[^\S\n]*(.+)$", re.M) GIT_WRITES = {"commit", "push", "pull", "merge", "rebase", "cherry-pick", "am", "apply", "reset", "restore", "checkout", "switch", "stash", "rm", "mv", "add", "revert", "clean", "tag", "worktree", "notes", "update-ref", "branch", "config", "submodule", "sparse-checkout", "filter-branch", "replace", @@ -1810,8 +1810,8 @@ def main(state): if target: s.path(target, cwd, tool) elif tool == "apply_patch": - for a, b in PATCH_PATHS.findall(ti.get("command") or ti.get("patchText") or ""): - s.path((a or b).strip(), cwd, "apply_patch") + for path in _patch.paths(ti.get("command") or ti.get("patchText") or ""): + s.path(path, cwd, "apply_patch") else: s.shell(ti.get("command") or "", ti.get("workdir") or cwd) return 0 diff --git a/plugin/bin/gate-supervisor-scope b/plugin/bin/gate-supervisor-scope index 2b520634..2ad81ba4 100755 --- a/plugin/bin/gate-supervisor-scope +++ b/plugin/bin/gate-supervisor-scope @@ -62,13 +62,14 @@ lock_recent() { # live in the patch headers. Check each path exactly as if it were a Write. if [ "$tool" = "apply_patch" ] && command -v python3 >/dev/null 2>&1; then printf '%s' "$payload" | python3 -c ' -import json, re, sys +import json, sys +sys.path.insert(0, sys.argv[1]) +import _patch p = json.load(sys.stdin) -patch = (p.get("tool_input") or {}).get("command") or "" -for a, b in re.findall(r"^[^\S\n]*\*\*\*[^\S\n]*(?:Add|Update|Delete)[^\S\n]+File:[^\S\n]*(.+)$|^[^\S\n]*\*\*\*[^\S\n]*Move[^\S\n]+to:[^\S\n]*(.+)$", patch, re.M): +for path in _patch.paths((p.get("tool_input") or {}).get("command") or ""): q = {k: v for k, v in p.items() if k in ("session_id", "agent_type", "agent_id", "cwd")} - q["tool_name"] = "Write"; q["tool_input"] = {"file_path": (a or b).strip()} - print(json.dumps(q))' | while IFS= read -r one; do + q["tool_name"] = "Write"; q["tool_input"] = {"file_path": path} + print(json.dumps(q))' "$(dirname "$0")" | while IFS= read -r one; do printf '%s' "$one" | "$0" || exit 2 done exit $? diff --git a/plugin/bin/host-adapter b/plugin/bin/host-adapter index f4061129..cb234674 100755 --- a/plugin/bin/host-adapter +++ b/plugin/bin/host-adapter @@ -33,6 +33,7 @@ import sys import time sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) +import _patch # noqa: E402 import _paths # noqa: E402 import _usage # noqa: E402 @@ -44,7 +45,6 @@ _SAFE = r"[A-Za-z0-9_./@%+=:,-]" _WORD = r"(?:'[^'\n]*'|\"[^\"$`\\\n!]*\"|%s+)" % _SAFE ONLY_BIND = re.compile(r"[ \t]*(?:cd[ \t]+%s[ \t]*&&[ \t]*)?(?:%s*/)?(?:(?:aloop|agentic)[ \t]+bind|bin/bind)[ \t]+%s" r"(?:[ \t]+(?:[12]?>[ \t]*/dev/null|2>&1))*\s*\Z" % (_WORD, _SAFE, _WORD)) -PATCH_PATHS = re.compile(r"^[^\S\n]*\*\*\*[^\S\n]*(?:Add|Update|Delete)[^\S\n]+File:[^\S\n]*(.+)$|^[^\S\n]*\*\*\*[^\S\n]*Move[^\S\n]+to:[^\S\n]*(.+)$", re.M) def role_of(agent_type): @@ -136,8 +136,7 @@ def pre_tool(payload, cwd, agent_id, role, host): return if tool == "apply_patch": bad = [] - for a, b in PATCH_PATHS.findall(cmd): - path = (a or b).strip() + for path in _patch.paths(cmd): full = path if os.path.isabs(path) else os.path.join(base, path) if not under(full, wt): bad.append(path) diff --git a/plugin/hosts/opencode/aloop.js b/plugin/hosts/opencode/aloop.js index 23feb676..36bc912c 100644 --- a/plugin/hosts/opencode/aloop.js +++ b/plugin/hosts/opencode/aloop.js @@ -253,7 +253,7 @@ export const AgenticAgile = async ({ client, directory, worktree }) => { if (input.tool === "bash") args.workdir = wt if (input.tool === "write" || input.tool === "edit") args.filePath = intoWt(args.filePath, wt) if (input.tool === "apply_patch" && args.patchText) - args.patchText = args.patchText.replace(/^([^\S\n]*\*\*\*[^\S\n]*(?:Add|Update|Delete)[^\S\n]+File:[^\S\n]*|[^\S\n]*\*\*\*[^\S\n]*Move[^\S\n]+to:[^\S\n]*)(.+)$/gm, (_, h, f) => h + intoWt(f.trim(), wt)) + args.patchText = args.patchText.replace(/^([^\S\n]*\*\*\*[^\S\n]*(?:Add|Update|Delete)[^\S\n]+File:[^\S\n]*|[^\S\n]*\*\*\*[^\S\n]*Move[^\S\n]+to:[^\S\n]*)([^\n]+)$/gm, (_, h, f) => h + intoWt(f.trim(), wt)) p = await payload("PreToolUse", input.sessionID, { tool_name: await toolName(input.tool), tool_input: toolInput(input.tool, args) }) } } diff --git a/plugin/mission/doors.json b/plugin/mission/doors.json index c729f6c2..1db5d032 100644 --- a/plugin/mission/doors.json +++ b/plugin/mission/doors.json @@ -24,9 +24,9 @@ } ], "tools": [ - "^(?:mcp__github__|github_)(?:merge_pull_request|enable_pr_auto_merge)$", + "^(?:mcp__[\\w-]+?__|(?!mcp__)[\\w-]+?_)(?:merge_pull_request|enable_pr_auto_merge)$", { - "tool": "^(?:mcp__github__|github_)(?:push_files|create_or_update_file|delete_file)$", + "tool": "^(?:mcp__[\\w-]+?__|(?!mcp__)[\\w-]+?_)(?:push_files|create_or_update_file|delete_file)$", "input": { "branch": "{DEF}" } @@ -49,7 +49,8 @@ "tools": [ "mcp__github__merge_pull_request", "github_merge_pull_request", - "github_enable_pr_auto_merge" + "github_enable_pr_auto_merge", + "mcp__github-mcp__merge_pull_request" ] } }, @@ -225,7 +226,7 @@ } ], "tools": [ - "(?i)^(?:mcp__\\w*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)\\w*?__|(?!mcp__)\\w*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)[A-Za-z0-9-]*_)(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)_)?(?:send|reply|forward|post|chat_post)\\w*$" + "(?i)^(?:mcp__(?:[\\w-]*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)[\\w-]*?__(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)_)?|[\\w-]+?__\\w*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)[A-Za-z0-9]*_)|(?!mcp__)[\\w-]*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)[A-Za-z0-9-]*_(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin)_)?)(?:send|reply|forward|post|chat_post)(?!s(?:$|_)|ed(?:$|_)|ing)\\w*$" ], "examples": { "bash": [ @@ -238,7 +239,11 @@ "mcp__slack__slack_reply_to_thread", "Gmail_send_message", "slack_slack_post_message", - "slack_post_message" + "slack_post_message", + "mcp__slack-mcp__post_message", + "mcp__zapier__gmail_send_email", + "mcp__composio__GMAIL_SEND_EMAIL", + "mcp__slack__chat_postMessage" ] } }, @@ -360,7 +365,7 @@ } ], "tools": [ - "(?i)^(?:mcp__\\w*?__|(?!mcp__)\\w+?_)(?:share_file|share|add_permission|create_permission)$" + "(?i)^(?:mcp__[\\w-]+?__|(?!mcp__)[\\w-]+?_)(?:(?!(?:get|list|read|search|find|fetch|describe|view|show|query)_)\\w*?_)?(?:share_file|share|add_permission|create_permission)$" ], "examples": { "bash": [ @@ -369,7 +374,10 @@ ], "tools": [ "mcp__Google_Drive__share_file", - "Google_Drive_share_file" + "Google_Drive_share_file", + "mcp__zapier__google_drive_share_file", + "mcp__google-drive__share_file", + "google-drive_share_file" ] } }, @@ -423,6 +431,7 @@ "(?:^|/)(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js$", "^opencode\\.jsonc?$", "(?:^|/)(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?$", + "(?:^|/)\\.config/opencode/config\\.json$", "(?:^|/)\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json)$" ], "plugin_paths": [ @@ -436,21 +445,24 @@ "writes": "(?:^|/)\\.claude/settings(?:\\.local)?\\.json$" }, { - "writes": "(?:^|/)(?:\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" + "writes": "(?:^|/)(?:\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.config/opencode/config\\.json|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" }, { - "prog": "cp|mv|ln|install|rsync|rm|unlink|truncate|shred", - "files": "(?:^|/)(?:\\.claude/settings(?:\\.local)?\\.json|\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" + "writes": "(?:^|/)(?:\\.claude|\\.codex(?:/agents)?|\\.opencode(?:/plugins)?|\\.config(?:/opencode(?:/plugins)?)?|\\.dsh(?:/profiles(?:/[^/]+)?)?|worktrees/[^/]+)/\\*$|^\\*$|(?:^|/)(?:\\.claude|\\.codex(?:/agents)?|\\.opencode(?:/plugins)?|\\.config(?:/opencode(?:/plugins)?)?|\\.dsh(?:/profiles(?:/[^/]+)?)?)/?$" + }, + { + "prog": "mv|rm|unlink|truncate|shred", + "files": "(?:^|/)(?:\\.claude/settings(?:\\.local)?\\.json|\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.config/opencode/config\\.json|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$|(?:^|/)(?:\\.claude|\\.codex(?:/agents)?|\\.opencode(?:/plugins)?|\\.config(?:/opencode(?:/plugins)?)?|\\.dsh(?:/profiles(?:/[^/]+)?)?)/?$" }, { "prog": "sed", - "flag": "-[^-\\sefl]*i.*|--in-place(?:=.*)?", - "files": "(?:^|/)(?:\\.claude/settings(?:\\.local)?\\.json|\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" + "flag": "-[^-\\sefl]*i.*|--i(?:n(?:-(?:p(?:l(?:a(?:c(?:e)?)?)?)?)?)?)?(?:=.*)?", + "files": "(?:^|/)(?:\\.claude/settings(?:\\.local)?\\.json|\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.config/opencode/config\\.json|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" }, { "prog": "perl", "flag": "-[^-\\sMmIeEFxdDC]*i.*", - "files": "(?:^|/)(?:\\.claude/settings(?:\\.local)?\\.json|\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" + "files": "(?:^|/)(?:\\.claude/settings(?:\\.local)?\\.json|\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.config/opencode/config\\.json|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" }, { "prog": "git", @@ -477,6 +489,7 @@ "home/worktrees/S1-01-T1/opencode.json", "~/.config/opencode/opencode.jsonc", "~/.dsh/profiles/aloop/cordis.patch.yml", + "~/.config/opencode/config.json", "~/.dsh/profiles/aloop/package.json" ], "bash": [ @@ -486,6 +499,11 @@ "cp /tmp/s .claude/settings.json", "sed -i 's/doors//' .codex/hooks.json", "rm -f .claude/settings.local.json", + "rm -rf .claude", + "mv .codex /tmp/codex-old", + "cp -r /tmp/x/. .claude", + "rsync -a /tmp/x/ .opencode/", + "sed --in-pl 's/x//' .claude/settings.json", "cat x >> .codex/hooks.json", "printf x | tee ~/.dsh/profiles/aloop/cordis.patch.yml", "git config core.hooksPath /dev/null", @@ -558,7 +576,7 @@ }, { "prog": "sed", - "flag": "-[^-\\sefl]*i.*|--in-place(?:=.*)?", + "flag": "-[^-\\sefl]*i.*|--i(?:n(?:-(?:p(?:l(?:a(?:c(?:e)?)?)?)?)?)?)?(?:=.*)?", "files": "(?:^|/)docs/agents/mission/ledger\\.jsonl$" }, { @@ -577,7 +595,7 @@ }, { "prog": "sed", - "flag": "-[^-\\sefl]*i.*|--in-place(?:=.*)?", + "flag": "-[^-\\sefl]*i.*|--i(?:n(?:-(?:p(?:l(?:a(?:c(?:e)?)?)?)?)?)?)?(?:=.*)?", "files": "^\\.(?:aladdin|agentic)(?:/logs(?:/(?:events\\.jsonl|[^/]*[*?][^/]*))?)?$" }, { @@ -760,7 +778,13 @@ "echo '{}' > docs/examples/opencode.json", "sed 's/x//' .claude/settings.json", "cat ~/.dsh/profiles/aloop/cordis.patch.yml", - "cat plugin/hosts/opencode/aloop.js" + "cat plugin/hosts/opencode/aloop.js", + "cp .claude/settings.json /tmp/", + "rsync -a .claude/ /tmp/backup/", + "rm -rf .opencode/node_modules", + "cp -r docs/. /tmp/site", + "sed --quiet -n p .claude/settings.json", + "cp notes.md .claude/agents/" ], "tools": [ "Read", @@ -773,7 +797,12 @@ "Gmail_search_threads", "slack_get_post_history", "mcp__drive__get_share", - "mcp__notion__search_slack_posts" + "mcp__notion__search_slack_posts", + "mcp__slack__get_post_history", + "mcp__gmail__list_drafts", + "mcp__zapier__gmail_find_email", + "mcp__drive__list_shares", + "mcp__twitter__get_posts" ], "paths": [ "src/a.py", diff --git a/plugin/mission/doors.py b/plugin/mission/doors.py index 6e930d7b..25754d54 100644 --- a/plugin/mission/doors.py +++ b/plugin/mission/doors.py @@ -42,6 +42,7 @@ """ import contextlib import functools +import glob import hashlib import itertools import json @@ -61,6 +62,7 @@ sys.path.insert(0, os.path.join(HERE, "..", "bin")) import _events # noqa: E402 import _mission as M # noqa: E402 +import _patch # noqa: E402 import _paths # noqa: E402 import _shellread as R # noqa: E402 @@ -80,11 +82,6 @@ # set in every shell an agent host runs (the agent's Bash tool): Claude Code, dsh, Codex, and OpenCode (its # plugin exports ALOOP_AGENT_SHELL); an authorization never comes from there AGENT_SHELL = ("CLAUDECODE", "DSH_SHELL", "CODEX_THREAD_ID", "ALOOP_AGENT_SHELL") -# the paths a patch adds, updates, deletes or moves to (host-adapter's PATCH_PATHS), read as loosely as a -# host parses a header: Codex trims each line, OpenCode splits at the colon and trims the path -PATCH = re.compile(r"^[^\S\n]*\*\*\*[^\S\n]*(?:Add|Update|Delete)[^\S\n]+File:[^\S\n]*(.+)$" - r"|^[^\S\n]*\*\*\*[^\S\n]*Move[^\S\n]+to:[^\S\n]*(.+)$", re.M) -PATCHERS = ("apply_patch", "applypatch") # Codex's patch tool run as a program (its argument or stdin form) TAG = re.compile(r"\[door:([^\]\s]*)\]") WRAPPERS = {"sudo", "doas", "env", "nohup", "time", "command", "exec", "nice", "timeout", "stdbuf", "caffeinate", "ionice"} SHELLS = {"sh", "bash", "zsh", "dash", "ksh"} @@ -246,22 +243,41 @@ def views(cmd, depth=0, cur=""): timeout, xargs, find -exec, …) around it, the commands inside `sh -c`, `eval`, a script a shell runs, a command substitution, and the stdin that feeds a shell or python (the last heredoc, here-string or `<` on fd 0, else the pipe, into a subshell too), each with the directory it runs in (`cd DIR`, - `pushd`/`popd`, `git -C DIR`; a subshell's `cd` stays in the subshell). Deeper than DEPTH is a - ValueError: a gate that cannot read the whole command fails closed.""" + `pushd`/`popd`, `git -C DIR`; a subshell's `cd` stays in the subshell). A group's, subshell's or + compound's own redirect (`{ …; } > F`, `( … ) < F`) is every command's inside it, and a bare + redirect (`> F`) is a view with no program. A `cd` that may not run (in an if, case or loop body or a + `{ …; }` group, or after `&&`/`||`) leaves the directory UNKNOWN once its list ends. Deeper than DEPTH + is a ValueError: a gate that cannot read the whole command fails closed.""" if depth > DEPTH: raise ValueError("the command nests shells, evals or substitutions deeper than the door gate reads (%d)" % DEPTH) out, stack, dirs, fed, group = [], [], [], None, None + opened, closed, scopes, prev, maybe = [], None, [], None, None for c in R.read(cmd): - if c in ("(", ")"): + if isinstance(c, str): if c == "(": # a pipe into a subshell feeds the commands inside it stack.append((cur, group)) group = fed if fed is not None else group - elif stack: - cur, group = stack.pop() - fed = None - continue - if isinstance(c, str): # "cwd+" / "cwd|" / "cwd-": a compound's bounds (the scope gate's) + opened.append(len(out)) + elif c == ")": + if stack: + cur, group = stack.pop() + closed = opened.pop() if opened else None + elif c == "cwd+": # an if, case, loop body or { …; } group: its `cd` may or may not run + scopes.append([cur, False]) + opened.append(len(out)) + elif c == "cwd|" and scopes: # a branch ends: the next one starts where the compound did + scopes[-1][1] = scopes[-1][1] or cur != scopes[-1][0] + cur = scopes[-1][0] + elif c == "cwd-": + if scopes: + entry, moved = scopes.pop() + cur = UNKNOWN if moved or cur != entry else entry + closed = opened.pop() if opened else None + if c in ("(", ")"): + fed = None + prev = None continue + own, closed = (closed if not c.words and not c.assigns else None), None for sub in c.subs: out += views(sub, depth + 1, cur) vs = simple(c, depth, cur) @@ -274,6 +290,8 @@ def views(cmd, depth=0, cur=""): elif prog in ("cd", "pushd"): if prog == "pushd": dirs.append(cur) + if prev in ("&&", "||") and maybe is None: + maybe = cur # it may not run: known only along its own && chain cur = chdir(cur, next((a for a in vs[0]["args"] if not a.startswith("-")), "")) elif prog == "popd": cur = dirs.pop() if dirs else UNKNOWN @@ -281,10 +299,19 @@ def views(cmd, depth=0, cur=""): for op, fd, w in c.redirs: # the last redirect of fd 0 wins, left to right if fd in ("", "0") and op in ("<<", "<<-", "<<<", "<", "<&", "<>"): stdin = w.body if op in ("<<", "<<-") else render(w.parts) if op == "<<<" else None - if stdin is not None and vs: + if own is not None: # the group's own redirects: its commands write and read them + inner = out[own:] + for v in inner: + v["writes"] = v["writes"] + (vs[0]["writes"] if vs else []) + if stdin is not None: + out += feed(inner, stdin, depth) + elif stdin is not None and vs: out += feed(vs, stdin, depth) fed = stdout(vs, stdin) if c.end in ("|", "|&") else None out += vs + if maybe is not None and c.end not in ("&&", "|", "|&"): + cur, maybe = (UNKNOWN if cur != maybe else cur), None + prev = c.end return out @@ -365,7 +392,7 @@ def feed(vs, text, depth): for v in vs: if "code" in v: # python: its stdin is code it may run (exec(sys.stdin.read())) v["code"] = (v["code"] + "\n" + text).lstrip("\n") - elif v["prog"] in PATCHERS: # apply_patch <<'EOF' … EOF: each path the patch names is a write + elif v["prog"] in _patch.PATCHERS: # apply_patch <<'EOF' … EOF: each path the patch names is a write v["writes"] = v["writes"] + patched(text) elif reads_stdin(v): out += [dict(x, env=x["env"] + v["env"]) for x in views(text, depth + 1, v["dir"])] @@ -387,11 +414,81 @@ def feed(vs, text, depth): "exec": {"-a"}, "time": {"-f", "-o", "--format", "--output"}} +COPY_PAIRED = { # the options of a copy, move, link or install that take a separate value + "cp": {"-S", "--suffix"}, "mv": {"-S", "--suffix"}, "ln": {"-S", "--suffix"}, + "install": {"-m", "-o", "-g", "-S", "--mode", "--owner", "--group", "--suffix", "--strip-program"}, + "rsync": {"-e", "-f", "-B", "-M", "-T", "-@", "--rsh", "--rsync-path", "--filter", "--exclude", "--include", + "--exclude-from", "--include-from", "--files-from", "--temp-dir", "--compare-dest", "--copy-dest", + "--link-dest", "--backup-dir", "--suffix", "--chmod", "--chown", "--usermap", "--groupmap", + "--password-file", "--log-file", "--log-file-format", "--out-format", "--partial-dir", "--max-size", + "--min-size", "--timeout", "--contimeout", "--port", "--sockopts", "--bwlimit", "--iconv", + "--checksum-choice", "--compress-choice", "--skip-compress", "--write-batch", "--read-batch", + "--only-write-batch", "--max-delete", "--block-size", "--modify-window", "--address", "--outbuf", + "--info", "--debug", "--stop-after", "--stop-at", "--copy-as", "--remote-option"}} + + +def copied(prog, args): + """The paths a copy, move, link or install writes: DEST, and DIR/NAME for each source when the + destination may be a directory (-t DIR, more than one source, or a DEST the gate cannot rule out), or + DIR/* for a source whose contents are copied (rsync's SRC/, SRC/.). Nothing for `install -d`, a + remote rsync destination or one operand.""" + paired, ops, target, whole, i = COPY_PAIRED[prog], [], None, False, 0 + while i < len(args): + a = args[i] + if a == "--": + ops += args[i + 1:] + break + if a.startswith("--"): + k, eq, v = a.partition("=") + if k == "--target-directory" and prog != "rsync": + target, i = (v, i) if eq else (args[i + 1] if i + 1 < len(args) else None, i + 1) + elif k == "--no-target-directory": + whole = True + elif k == "--directory" and prog == "install": + return [] + elif not eq and k in paired: + i += 1 + elif a.startswith("-") and a != "-": + for j in range(1, len(a)): + f = "-" + a[j] + if f == "-t" and prog != "rsync": + target, i = (a[j + 1:], i) if a[j + 1:] else (args[i + 1] if i + 1 < len(args) else None, i + 1) + break + if f == "-T" and prog != "rsync": + whole = True + elif f == "-d" and prog == "install": + return [] + elif f in paired: + i += 0 if a[j + 1:] else 1 + break + else: + ops.append(a) + i += 1 + if target is not None: + dest, srcs, out = target, ops, [] + elif prog == "ln" and len(ops) == 1: + dest, srcs, out = ".", ops, [] # ln -s TARGET: a link named after it, here + elif len(ops) >= 2: + dest, srcs, out = ops[-1], ops[:-1], [ops[-1]] + else: + return [] + if prog == "rsync" and re.match(r"^[^/]*:", dest): + return [] # host:path is not this machine's + for s in [] if whole else srcs: + if s.endswith("/.") or prog == "rsync" and s.endswith("/"): + out.append(dest.rstrip("/") + "/*") # its contents, whatever they are named + elif os.path.basename(s.rstrip("/")) not in ("", ".", ".."): + out.append(dest.rstrip("/") + "/" + os.path.basename(s.rstrip("/"))) + return out + + def simple(c, depth, cur=""): """The views of one simple command (a _shellread.Cmd).""" env = ["%s=%s" % (n, render(v)) for n, v in c.assigns] writes = [render(w.parts) for op, _, w in c.redirs if op in WRITES or (op == ">&" and not re.fullmatch(r"\d+|-", w.text() or "0"))] + if not c.words: # `> F`, `X=1 > F`: no program, still a write + return [{"prog": "", "args": [], "env": env, "writes": writes, "dir": cur, "config": []}] if writes else [] return chain([render(w.parts) for w in c.words], env, writes, depth, cur) @@ -421,7 +518,9 @@ def chain(words, env, writes, depth, cur): prog, rest, code = os.path.basename(rest[j]), rest[j + 1:], None # python SCRIPT: the script runs if prog == "tee": writes = writes + [w for w in rest if not w.startswith("-")] - if prog in PATCHERS and rest: # apply_patch 'PATCH': each path it names is a write + if prog in COPY_PAIRED: + writes = writes + copied(prog, rest) + if prog in _patch.PATCHERS and rest: # apply_patch 'PATCH': each path it names is a write writes = writes + patched(rest[0]) if prog == "git": while rest and rest[0].startswith("-"): @@ -561,10 +660,24 @@ def at(where): if where not in made: full = resolve(cwd, where) if where != UNKNOWN else "" made[where] = Context(full or cwd, assume_when or not (full and os.path.isdir(full)), assume_when) + real = resolve(cwd, where, True) if full else "" + if real and real != os.path.realpath(full): # git -C, cd -P, a host workdir: a link before `..` + made[where] = Either(made[where], Context(real, assume_when or not os.path.isdir(real), assume_when)) return made[where] return at +class Either: + """A directory read two ways (as written, and as the kernel walks it): a condition holds if it holds in + either.""" + + def __init__(self, *ctxs): + self.ctxs = ctxs + + def when(self, *a, **k): + return any(c.when(*a, **k) for c in self.ctxs) + + @functools.lru_cache(maxsize=8) def toplevel(cwd): r = subprocess.run(["git", "-C", cwd, "rev-parse", "--show-toplevel"], capture_output=True, text=True) @@ -678,13 +791,47 @@ def placed(cwd, top, v, path, kernel=False): return full +GLOB_MAX = 512 # the files a glob operand is expanded to, at most + + +def where_of(v, path): + """Where a command's path operand is, as resolve() takes it (UNKNOWN: the gate cannot tell).""" + if v["dir"] == UNKNOWN or re.search(r"[$`]", path) or "\n" in path or len(path) > 4096: + return UNKNOWN + return path if os.path.isabs(path) or path.startswith(("~", TOP)) or not v["dir"] else v["dir"] + "/" + path + + +def matches(cwd, v, path): + """The files a glob operand names now: the shell expands it before the command runs.""" + where = where_of(v, path) if re.search(r"[*?]", path) else UNKNOWN + full = resolve(cwd, where) if where != UNKNOWN else "" + return sorted(itertools.islice(glob.iglob(full), GLOB_MAX)) if full else [] + + +def checkout_rel(full): + """A path relative to the checkout that holds it (the nearest directory with a .git: a nested or + linked worktree's own root, where a host reads its config), or None.""" + d = os.path.dirname(full) + while d: + if os.path.exists(os.path.join(d, ".git")): + return os.path.relpath(full, d) + d = "" if os.path.dirname(d) == d else os.path.dirname(d) + return None + + def place(cwd, vs): - """Each view's operands and writes as placed() reads them (files, placed_writes).""" + """Each view's operands and writes as placed() reads them (files, placed_writes): as written and as the + kernel walks them, each file a glob names, and relative to the checkout that holds it.""" top = os.path.realpath(_paths.project(cwd)) + + def forms(v, a): + out = [placed(cwd, top, v, a, k) for k in (False, True)] + [placed(cwd, top, v, m) for m in matches(cwd, v, a)] + where = where_of(v, a) if not a.startswith("-") and a != STDIN else UNKNOWN + own = checkout_rel(resolve(cwd, where, True)) if where != UNKNOWN and resolve(cwd, where) else None + return out + ([own] if own and not own.startswith("..") else []) for v in vs: - v["files"] = list(dict.fromkeys(placed(cwd, top, v, a, k) for a in v["args"] if not a.startswith("-") - for k in (False, True))) - v["placed_writes"] = list(dict.fromkeys(placed(cwd, top, v, w, k) for w in v["writes"] for k in (False, True))) + v["files"] = list(dict.fromkeys(x for a in v["args"] if not a.startswith("-") for x in forms(v, a))) + v["placed_writes"] = list(dict.fromkeys(x for w in v["writes"] for x in forms(v, w))) return vs @@ -729,7 +876,8 @@ def rel_path(cwd, path): r = os.path.relpath(full, roots[0]) if r.startswith("..") and len(roots) == 1: roots.append(os.path.realpath(toplevel(cwd) or roots[0])) - forms += [next((x for x in (os.path.relpath(full, t) for t in roots) if not x.startswith("..")), ".."), full] + forms += [next((x for x in (os.path.relpath(full, t) for t in roots) if not x.startswith("..")), ".."), full, + checkout_rel(full) or ".."] p = os.path.relpath(full, PLUGIN) plug = plug or (p if not p.startswith("..") else None) return [x for x in dict.fromkeys(forms) if not x.startswith("..")], plug @@ -773,8 +921,8 @@ def written(tool, inp): def patched(text): - """Each path a patch adds, updates, deletes or moves to.""" - return [x for x in ((a or b).strip() for a, b in PATCH.findall(text)) if x] + """Each path a patch adds, updates, deletes or moves to (_patch.py: as the hosts read a header).""" + return _patch.paths(text) def workdir(inp, cwd): @@ -785,7 +933,7 @@ def workdir(inp, cwd): wd = inp.get("workdir") if os.environ.get("AGENTIC_HOST") not in ("opencode", "dsh") or not isinstance(wd, str) or not wd: return "" - wd = os.path.normpath(os.path.join(cwd, wd)) + wd = os.path.join(cwd, wd) # as given: contexts() reads it as written and as the kernel walks it return wd if os.path.isdir(wd) else UNKNOWN diff --git a/scripts/test/mission-doors.test.py b/scripts/test/mission-doors.test.py index efff0104..eceab39c 100644 --- a/scripts/test/mission-doors.test.py +++ b/scripts/test/mission-doors.test.py @@ -17,6 +17,7 @@ import shutil import subprocess import sys +import tempfile import threading import time @@ -929,7 +930,86 @@ def swallowing(*a, **k): assert doors_of(cmd) == [], cmd assert p.run("doors", "check").returncode == 0 ok("settings: every host's settings and hooks and aloop's own host adapters are one-way; reading them is two-way") + +# 21b) the second M9.31 review. A redirect with no command word, or on a group, a subshell or a compound, is +# theirs (its writes, its heredoc feeding every command inside) +force = "git push --force origin main" +for cmd in ("> .claude/settings.json", "{ echo '{}'; } > .claude/settings.json", "(echo '{}') > .claude/settings.json", + "if true; then echo '{}'; fi > .claude/settings.json", "(apply_patch) <<'EOF'\n%sEOF" % settings, + "{ apply_patch; } <<'EOF'\n%sEOF" % settings, "for x in a; do :; done >> .codex/hooks.json"): + assert "change-agent-settings" in doors_of(cmd), (cmd, doors_of(cmd)) +assert "forge-human-record" in doors_of("> docs/agents/mission/ledger.jsonl") +for cmd in ("(bash) <<'EOF'\n%s\nEOF" % force, "{ bash; } <<'EOF'\n%s\nEOF" % force, "if :; then sh; fi <<< '%s'" % force, + "( (bash) ) <<'EOF'\n%s\nEOF" % force): + assert {"push-default-branch", "force-push-or-delete-remote"} <= set(doors_of(cmd)), (cmd, doors_of(cmd)) +for cmd in ("{ echo a; echo b; } > out.txt", "(cd src && make) > build.log 2>&1", "> build.log", "(cat) <<'EOF'\nhi\nEOF"): + assert doors_of(cmd) == [], (cmd, doors_of(cmd)) +ok("groups: a bare redirect, and a group's or compound's own redirect or heredoc, reach the commands inside") + +# a `cd` that may not run (in an if, case or loop body, after && or ||) leaves the directory unknown after it; +# a directory is also read as the kernel walks it (git -C, cd -P and dsh's workdir follow a link before `..`) +other = tempfile.mkdtemp() # a directory that is no checkout +for cmd in ("if false; then cd %s; fi; git push origin HEAD", "false && cd %s; git push origin HEAD", + "[ -d /nope ] && cd %s || true; git push origin HEAD", "while false; do cd %s; done; git push origin HEAD", + "case x in y) cd %s ;; esac; git push origin HEAD", "if false; then :; else cd %s; fi\ngit push origin HEAD"): + assert "push-default-branch" in denied(hook(g, "Bash", {"command": cmd % other})), cmd +for cmd in ("cd %s && git push origin HEAD" % gwt, "test -d %s && cd %s && git push origin HEAD" % (gwt, gwt), + "if true; then cd %s && git push origin HEAD; fi" % gwt, "cd %s; git push origin HEAD" % gwt): + assert hook(g, "Bash", {"command": cmd}).stdout == "", cmd +os.makedirs(os.path.join(g.dir, "tools"), exist_ok=True) +os.symlink(os.path.join(g.dir, "tools"), os.path.join(gwt, "link")) +assert hook(g, "Bash", {"command": "git push origin HEAD"}, cwd=gwt).stdout == "" # the worktree: its branch +for cmd in ("git -C link/.. push origin HEAD", "cd -P link/.. && git push origin HEAD", "git -C %s/link/.. push origin HEAD" % gwt): + assert "push-default-branch" in denied(hook(g, "Bash", {"command": cmd}, cwd=gwt)), cmd +assert "discard-local-work" in denied(hook(g, "Bash", {"command": "git -C link/.. reset --hard"}, cwd=gwt)) +assert "push-default-branch" in denied(hook(g, "Bash", {"command": "git push origin HEAD", "workdir": gwt + "/link/.."}, + env={"AGENTIC_HOST": "dsh"})) +ok("directories: a cd that may not run leaves the directory unknown; git -C, cd -P and a workdir walk a link first") + +# a copy, move, link or install into a directory writes DIR/NAME; removing or moving a directory that holds +# settings, or a glob that names one, changes them; sed's abbreviated --in-place; a nested checkout's +# opencode.json and OpenCode's global config.json +os.makedirs(os.path.join(p.dir, ".opencode", "plugins"), exist_ok=True) +open(os.path.join(p.dir, ".opencode", "plugins", "aloop.js"), "w").write("//") +for cmd in ("cp /tmp/settings.json .claude/", "cp /tmp/settings.json .claude", "cp -t .claude /tmp/settings.json", + "cp --target-directory=.claude /tmp/settings.json", "cp /tmp/opencode.json .", "mv /tmp/settings.json .claude/", + "install -t .claude -m 644 /tmp/settings.json", "ln -s /tmp/settings.json .claude/", "rsync /tmp/settings.json .claude/", + "rsync -a /tmp/conf/ .claude/", "cp -r /tmp/conf/. .opencode/plugins", "cp -S .bak x /tmp/y .codex/hooks.json", + "rm -rf .opencode/plugins", "mv .opencode /tmp/off", "rm -rf .claude", "rm -rf ~/.config/opencode", + "rm .opencode/plugins/*.js", "rm -f .opencode/plugins/*", "sed --in-pl=.bak 's/x//' .claude/settings.json", + "sed --i 's/x//' .codex/hooks.json", "echo {} > ~/.config/opencode/config.json"): + assert "change-agent-settings" in doors_of(cmd), (cmd, doors_of(cmd)) +assert doors_of("echo {} > home/feat-wt/opencode.json", cwd=g.dir) == ["change-agent-settings"] +for cmd in ("cp .claude/settings.json /tmp/", "cp .claude/settings.json /tmp/backup.json", "cp src/a.py build/", + "rsync -a .claude/ /tmp/backup/", "rm -f src/*.pyc", "rm -rf .opencode/node_modules", "mv src/a.py src/b.py", + "cp -r docs/. /tmp/site", "sed --quiet -n p .claude/settings.json", "rm -rf docs/examples/.claude/tmp"): + assert doors_of(cmd) == [], (cmd, doors_of(cmd)) +assert "forge-human-record" in doors_of("sed --in-pla 's/a/b/' docs/agents/mission/ledger.jsonl") +for x in (os.path.join(gwt, "opencode.json"), "~/.config/opencode/config.json"): + assert ids("Write", {"file_path": x}) == ["change-agent-settings"], x +ok("settings: copies into a settings directory, a settings directory removed or moved, globs, sed --in-pl, nested checkouts") shutil.rmtree(g.dir, ignore_errors=True) +shutil.rmtree(other, ignore_errors=True) + +# OpenCode MCP names keep a '-' in a server's name, and an aggregator's tool names carry the service: both +# reach the same doors as the service's own server; a word inside another tool's name is no door +for tool, want in (("mcp__slack-mcp__post_message", "send-external-message"), ("mcp__zapier__gmail_send_email", "send-external-message"), + ("mcp__composio__GMAIL_SEND_EMAIL", "send-external-message"), ("mcp__slack__chat_postMessage", "send-external-message"), + ("mcp__zapier__google_drive_share_file", "visibility-or-sharing"), ("mcp__google-drive__share_file", "visibility-or-sharing"), + ("google-drive_share_file", "visibility-or-sharing"), ("mcp__github-mcp__merge_pull_request", "push-default-branch")): + assert ids(tool, {}) == [want], (tool, ids(tool, {})) +for tool in ("mcp__notion__search_slack_posts", "mcp__drive__get_share", "mcp__slack__get_post_history", "mcp__gmail__list_drafts", + "mcp__zapier__gmail_find_email", "github_create_pull_request", "mcp__drive__list_shares"): + assert ids(tool, {}) == [], (tool, ids(tool, {})) +ok("MCP: hyphenated servers and aggregators' tools cross the same doors; a word inside a tool's name is none") + +# a U+FEFF around a patch's path: OpenCode's trim() cuts it (JS whitespace), so every gate reads the path without it +bom = "\ufeff" +for head in ("*** Add File:%s.claude/settings.json" % bom, "*** Add File: .claude/settings.json%s" % bom, + "*** Add File: %sopencode.json" % bom): + assert ids("apply_patch", {"patchText": patch(head, "+{}")}) == ["change-agent-settings"], repr(head) +assert "forge-human-record" in ids("apply_patch", {"patchText": patch("*** Update File:%sdocs/agents/mission/ledger.jsonl" % bom, "@@")}) +ok("apply_patch: a U+FEFF around a header's path is cut as OpenCode cuts it") # 22) list and the area's docstring r = p.run("doors", "list", "--json") From 88204c6b96e3f47fc70e4fcd6c7c85187a3240f2 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 15:59:20 +0000 Subject: [PATCH 09/14] M9.31: third review fixes (cd candidates, copies placed where they run, MCP names) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - A cd that may not run (after && or ||, before ||, in a group or compound) leaves each later command in either directory: one view per directory instead of UNKNOWN, so a settings write after it is still placed, an ordinary command is no wall, and an absolute cd returns. - cd -, cd -- -, a bare pushd, pushd/popd -n and ±N, cd X &, an eval's cd, cd '', ~+ and ~-, and GIT_DIR / GIT_WORK_TREE (prefix, env, export) are read as the shell reads them; a pipe into a { } group or compound feeds each command inside; a group's heredoc feeds only commands with no stdin of their own (no exponential re-feeding). - copied() returns plans; place() resolves them where the command runs: DEST/NAME only into an existing directory (backups and copies into .claude pass), -t with {} or xargs arguments, --parents, rsync -R and --remove-source-files, GNU long-option prefixes, options after operands; mv's sources are writes; ln and cp -l/-s of a settings file are doors. - Globs expand only where the shell expands them, through links, keep every config-like name; checkout_rel only for this project's checkouts; a home-relative form for ~/.claude and friends. - builtin, setsid, flock, taskset, chrt and script -c are wrappers; source /dev/stdin runs its stdin. - Catalogue: anchored settings directories, Composio VERBS_A_, kebab-case, scheduled, conversations_add_message and verb-first sends; drafts pass; GitLab/Bitbucket/Composio merges; Drive access grants. - ROADMAP M9.62 records what is left (while-condition heredoc, recursive record copies, ln -sT of a settings dir, git aliases). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- CHANGELOG.md | 2 +- docs/ROADMAP.md | 3 +- plugin/mission/doors.json | 68 +++++- plugin/mission/doors.py | 370 ++++++++++++++++++++++------- scripts/test/mission-doors.test.py | 120 +++++++++- 5 files changed, 466 insertions(+), 97 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9c44622..fc0fa825 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ tag in lockstep. Tag releases as `vMAJOR.MINOR.PATCH`. ### Added - Plan approval is a gate (ROADMAP M0.9): `gate-dispatch` starts no execution role (RED, SCAFFOLD, GREEN, structural review, final gate) of a sprint until the human approved that sprint's plan as it is now. `events authority --sprint N` exits 3 with the line to type, `approve SN ` (reviewed, the code naming the sha256 of the sprint's plan files) or `autonomous SN ` (autonomy for that sprint, re-plans included, the code naming the authority record); the task contract's `STORY_DIR` and task must be a live story and task of that plan's frozen `ids.json`; the line must be among the human's captured prompts and typed after the sprint's last decision (M9.17's mechanism). A changed plan, a re-plan in reviewed mode, a roll-back to an old line, another sprint's line, a negated line and a hand-edited or emitted record all block (`plan.approved` and `human.approval` cannot be `events emit`ted); an approved autonomous mission grants its own sprints autonomy with no line. `events approval SPRINT|TASK_ID` is the check. Codex's `spawn_agent` (`agent_type`, `message`) and OpenCode's `task` now reach `gate-dispatch` too, and `claude -p`, `codex exec` and `opencode run` from a tool call are blocked in every aloop project, not only where a mission is set. `e2e-live.sh` pre-approves with an `autonomous S1 ` line in its prompt. Reason: the north star allows no human help beyond approval, and the approval was a playbook rule a supervisor could skip. A 4-lens adversarial review found 9 defects: 7 fixed with a check red on the first head (cross-sprint or unplanned `STORY_DIR`, a retyped line kept stale, mission autonomy over a non-mission sprint, `transcripts prune` dropping the prompts grants cite, `codex plugin add` refused, a code past column 200, a crash on a malformed run-log line), 2 in the playbook (intake reuses a mission's handed-off intent; a reviewed re-plan waits for the human's new line). `transcripts prune` now keeps the human's prompts. Test: `plan-approval.test.py` (20 checks, red on master), plus the host suites. - Core agent principles and the one-way / two-way door rule (ROADMAP §4.12, M9.30): seven core principles from Amazon's Leadership Principles, the door rule first, go to every aloop agent at session start (`aloop mission principles show --core`). A two-way door (a branch, a local edit, a test, a draft PR) is decided, not asked. A one-way door (`aloop mission doors list`: a push or merge to the default branch, a force-push, a release, a deploy, a message sent outside, a secret, dropped data, …) is stopped by a PreToolUse gate with `ASK[door] ` until the human records an authorization in their own terminal (`aloop mission doors authorize ID --by NAME --reason TEXT [--until DATE] [--once DIGEST]`, a hash-chained ledger row); a wall (switching a gate off, forging a human record) is never crossed. While a run is live, a question to the human must name a door the gate stopped and nobody authorized. Reason: Adeel's request that agents decide two-way doors themselves and ask only at a one-way door not already authorized. Test: `mission-doors.test.py`. Hardened by an adversarial review (15 findings, each a test): the gate reads Bash as the shell does (comments, line continuations, heredoc bodies, `xargs`, `find -exec`, `echo … | sh`), a branch condition reads the directory the command acts on (`git -C`, `cd`), settings are guarded wherever they live, dry runs and `command -v` are two-way, the newest decision by time counts and a revoke holds on every branch, a once-grant is crossed once under a lock, a broken ledger is named, and `doors authorize` refuses an agent's shell. -- The door rule on every host and role (ROADMAP §4.12, M9.31): every dispatched role gets the core principles (`aloop mission principles hook` at SubagentStart, Codex agents' instructions, OpenCode's system prompt, each dsh role persona), and the door gate runs on Codex, OpenCode and dsh with each host's question tool mapped. In the OpenCode and dsh plugins it runs last, after every other gate admitted the call, and fails closed after 15 s. A bound worker's command is judged where it runs (its `workdir`), an MCP tool by its own arguments (`mcp____` on OpenCode too), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Hardened by two reviews (19 findings, each a test that failed first): a stalled decision can no longer fall through to an allow, `apply_patch` is read with any header spacing (a U+FEFF too) and through the shell, a bare redirect or a group's redirect reaches the commands inside, a `cd` that may not run leaves the directory unknown, a symlink then `..` is read as the kernel walks it, replacing a settings file with `cp`, `mv`, `ln`, `install`, `rsync` (into a settings directory too), `rm`, `sed -i` or `perl -i`, or removing a directory that holds one, is the settings door, and an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`) crosses the same door as the service's own. Tests: `mission-doors.test.py`, `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`. +- The door rule on every host and role (ROADMAP §4.12, M9.31): every dispatched role gets the core principles (`aloop mission principles hook` at SubagentStart, Codex agents' instructions, OpenCode's system prompt, each dsh role persona), and the door gate runs on Codex, OpenCode and dsh with each host's question tool mapped. In the OpenCode and dsh plugins it runs last, after every other gate admitted the call, and fails closed after 15 s. A bound worker's command is judged where it runs (its `workdir`), an MCP tool by its own arguments (`mcp____` on OpenCode too), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Hardened by three reviews (53 findings, each a test that failed first): a stalled decision can no longer fall through to an allow, `apply_patch` is read with any header spacing (a U+FEFF too) and through the shell, a bare redirect or a group's redirect reaches the commands inside, a `cd` that may not run leaves the directory unknown, a symlink then `..` is read as the kernel walks it, replacing a settings file with `cp`, `mv`, `ln`, `install`, `rsync` (into a settings directory too), `rm`, `sed -i` or `perl -i`, or removing a directory that holds one, is the settings door, and an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`) crosses the same door as the service's own. A `cd` that may not run leaves each later command in either directory, a copy is placed where it runs (so backing up `.claude` passes), and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Drive access grants are doors too. Tests: `mission-doors.test.py`, `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`. - DeepSeek Harness (dsh) host (ROADMAP §4.9): `aloop install dsh` (alias `deepseek`; `--project P` or `--global`) renders a `dsh --profile aloop` profile in `$DSH_HOME` and installs the skills. Its native plugin (`plugin/hosts/dsh/aloop.mjs`) runs the same gates as Claude Code on dsh's own events: a deny before a tool runs, a block after an invalid artifact write, and a stop gate that steers a role back up to 3 times before the supervisor is told it is BLOCKED (or STUCK, released unverified). Roles are agent presets on two subagent tools, one per model route (`deepseek-v4-pro` for planning, `deepseek-flash` for work by default; `MODEL_PLANNING_DSH` / `MODEL_WORKER_DSH` override). A writer's `aloop bind` moves its working directory into its worktree, and `host-adapter` confines its commands, edits and `working_directory` there. Usage is recorded Claude-shaped per role, so budgets, the ledger and `run-summary` work unchanged. `make e2e-live HOST=dsh` drives it live and exits 2 with the fix when no `DEEPSEEK_API_KEY` is set. Reason: DeepSeek's own harness is the cheapest route to a strong model. The profile records the project it serves (or `--global`), so another project's `aloop-init` never re-renders it, and gates get the worker shell's environment (no API key). The mission layer's six roles run on dsh too: presets on their route group's subagent tool, `gate-mission-scope` on every write and command, and `gate-mission-artifact` at each role's start and stop; the mission skill installs as `aloop-mission`. A second review (3 lenses; 11 confirmed findings, each fixed with a test) closed: a working directory that vanished (dsh's tools move it back to the main tree, so the plugin judges `ensure()`'s directory and `host-adapter` refuses to run from a missing one), gate switches from the project's `.env` (dsh loads it; `SKIP_HOOKS`, `GATE_RUN_TESTS`, `REPO_DIR`, `AGENTIC_*` from it never reach a gate), an interrupted Stop that dropped the sprint-close notice, and the shared profile: its owner is the main tree, an older unrecorded profile is claimed only by the project that installed it, and a project it does not serve is told so by `install status`, `aloop-init` and the session itself. Tests: `dsh-plugin.test.mjs` (40 checks, emulated), `dsh-runtime.test.mjs` (32 checks on the real pinned dsh 0.2.1-rc.2 with a scripted model; the CI hosts job installs it) and `dsh-install.test.py` (9). - Brand kit and repo front page: `docs/brand/` holds the mark (an **a** whose bowl is a red-to-green loop passing a gold gate, with a 16 px cut), lockups, light and dark README banners, favicons, app icons, the site's OG card and the GitHub social preview, all generated by `docs/brand/gen_brand.py` from one palette with WCAG checks (rules in `docs/brand/README.md`). The README leads with the mission layer, the agent principles and the door rule, the supported hosts and the measured demo runs; the site carries the favicon and Open Graph/Twitter card metadata; `SECURITY.md` and bug/feature issue forms are new. - A mission's gap lists count only as `mission evaluate` computed them (ROADMAP §4.12, M9.27): `evaluate` records `eval.completed` in the committed ledger, and `mission tick`, `evaluate check` and `goal` take the newest eval whose content that record covers, with a matching seal, in the ledger's order. An eval file written by hand, or edited (or re-stamped) after it was recorded, no longer sends the loop to `RESEARCH` as "goal met" or makes `goal derive` refuse what tick handed out, a clock that is behind no longer reorders them, and a fresh checkout derives and closes goals from the evals it committed. An eval or state named as journey or baseline evidence needs its record too. Tests: `mission-tick` check 32, `mission-journeys`. diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index f79dda99..d6f689f4 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -466,7 +466,7 @@ Each task flips only its own row here and in TRACKING.md. | M9.28 | `tick` drives the goal commands (M9.11): a goal counts only with its `goal.derived`/`goal.approved` record and closes by `goal.met`; handouts name `goal derive`, `options` (spoon-fed), `intents`, `next` and `close`; an eval narrowed to a closed goal asks for a whole-mission eval. Reason: found when M9.11 landed, `tick` read goal files and their status, so a goal nobody derived steered the loop and a met goal was never closed | M9.8, M9.11 | `plugin/mission/tick.py`, `plugin/skills/mission/SKILL.md`, `mission-tick.test.py` | yes | done: `mission-tick.test.py` (29 checks; a real goal run on the real clock: derive → review → intents → next and a sprint per intent → narrowed eval → close → whole-mission eval → `RESEARCH`) | | M9.29 | A goal that crosses a hard line stops being the active goal (`evaluate.active_goal` skips it, or a recorded retirement). Reason: found wiring M9.28, `evaluate` keeps narrowing to a hard-lined goal and `goal derive` refuses a narrowed eval, so the mission deadlocks (`tick` hands it to the human meanwhile) | M9.10, M9.11 | `plugin/mission/{evaluate,goal}.py`, their tests | yes | done: `evaluate.active_goal` skips a goal an ethics hard line retired (it never closes), so the next eval covers the whole mission; `goal derive` from an eval narrowed to a retired goal says to evaluate again; `tick` hands out EVALUATE for such an eval, then DERIVE_GOAL, which takes the next gap (a hard line holds the retired goal's own gaps); when every open gap is held it is BLOCKED for the human (a change of the mission), not handed to a derive that would refuse. `mission-goal.test.py` (section 11), `mission-tick.test.py` (section 27). Call: retirement is derived from the hard line itself, not a new record, so it holds on a fresh clone with the ledger | | M9.30 | Core agent principles and the one-way / two-way door rule, enforced: seven core principles from Amazon's Leadership Principles (the door rule first) for every agent, injected at session start; a door catalogue, the human's standing or once authorization in the ledger, and a PreToolUse gate that stops an unauthorized one-way door (ASK) and a wall (BLOCK) and lets two-way doors pass; while a run is live a question must name a door the gate stopped. Reason: Adeel, 2026-10-20: "If it is two-way, then the agent should take the decision directly without bothering the user. But if it is one-way door, then it can bother user if user already have not specified to cross that door." | M9.0, M9.7 | `plugin/mission/doors.{py,json}`, `principles.{md,py}` (`## Core`), `plugin/bin/session-start`, `hooks.json` (one line), core: `_events.py` / `_mission.py` (door events), `events` (`GATE_OWNED`), `scripts/test/mission-doors.test.py` | after M9.7 | done: `mission doors list / classify / authorize / revoke / check / hook`, `mission principles show --core`; `mission-doors.test.py` (21 checks), `mission-principles.test.py` (+1 check). Calls: anything the catalogue does not list is two-way (a finite list cannot claim full enforcement, §7); Bash is matched by program and arguments after reading the line as the shell does, with the scope gate's reader (`_shellread.py`, M9.38: quotes, comments, line continuations, `$(…)`, a heredoc's body as data unless a shell or python runs it) and unwrapping `sudo`, `env`, `sh -c`, `eval`, `xargs`, `find -exec` and `echo … | sh`, so text that only mentions a door (`grep`, a commit message, a file written by a heredoc) stays two-way; a `when` condition (on the default branch, the main checkout) reads the directory the command acts on (`git -C DIR`, `cd DIR &&`), and one the gate cannot resolve counts; grants are standing (until revoked or `--until`), once (one action's digest, 24 h, used up by its first crossing, recorded in the ledger), never (a wall: switching a gate off, forging a human record; refused even for the human's `authorize`) and command (question-only doors answered by their own command); authorizations are hash-chained human ledger rows (an edited row, `events emit` or a stream-only event opens nothing; `HUMAN_EVENTS` are now gate-owned, which also closes a forged `baseline.vetoed`); a question is checked only while a run is live (an approved autonomous mission or an autonomous run, or a sprint whose latest plan is approved, for the session holding its lock), so intake, planning and the plan's own approval are asked; the core block is kept out of the goal blocks, so editing it never stales a goal's stamp; the hook fails closed (exit 2) and is silent outside an aloop project; measured: median 81 ms per two-way call, 102 ms per stopped one-way call (30 calls each, in parallel with the other `*` hooks). Calls from its adversarial review (15 confirmed findings over 3 lenses, each now a test that failed before its fix): the newest decision about a door is the newest by time (a union merge that puts an older authorization last reopens nothing), and the run log's revokes and crossings close on any branch of the checkout; a once-grant is checked and crossed under a lock (`.aladdin/state/doors.lock`); a broken ledger is named in the ASK, fails `doors check` and blocks `authorize` with the restore line; settings and the ledger are matched by path suffix (`~/.claude`, a worktree's own); a dry run, a help page and `command -v` are two-way, `--dry-run=none` is not; `git -c core.hooksPath=` is a wall and `git config core.hooksPath VALUE` a one-way door; arguments `xargs` adds from stdin may be any flag of a one-way door; `doors authorize` refuses an agent's shell (`CLAUDECODE` set: the human authorizes in their own terminal); Slack's real send tools (`slack_post_message`, `slack_reply_to_thread`) are one-way; a hard ethics line only the run log holds (recorded before the ledger did) still binds. Calls from its second review (26 confirmed findings over parser, catalogue and state lenses, each now a test that failed before its fix): the gate reads Bash with `_shellread.py` (M9.38), which now keeps each heredoc's body, the operator that ends a command and heredoc bodies inside `$(…)`, so touching operators (`); git push`), keywords, groups, function bodies, here-strings, IO-number redirects (`2>&1`) and quoted operators (`find \( … \) -exec`) hide nothing, a quote in `"$(cat <<'EOF' … EOF)"` is data, stdin (a heredoc, a here-string, `echo|printf|cat … |`) reaches only the command it feeds (a shell with options or `-s`, a python with `-`), and `pushd`/`popd`, `sh -c`, `eval` and a fed shell keep the directory; nesting deeper than 8 is unreadable and a read past 8 s fails closed (exit 2, under the hook's 15 s); python is a forge only when it calls a writer, `npm|pnpm|yarn -g` a system change only with an installing verb, the ledger only at `docs/agents/mission/ledger.jsonl`, `xargs … doors` only when stdin can be the subcommand (`-I R` replaces R), a secret command's dry run or help page is two-way, `git push origin HEAD|@` on the default branch is one-way, and erasing or editing the run log (`.aladdin/logs/events.jsonl`, whose revokes and crossings close doors) is a wall; a ledger row is written before its stream event and `ledger_problems` reads the stream first, so no gate sees another writer's half-written record as a lost row; a record is dated no earlier than any ledger row, so the human's fresh decision is the newest even after a merged future-dated row; a run stops being live when its plan's sprint closes (`sprints.json`) or a new intent arrives; a malformed run-log line never crashes the ethics check | -| M9.31 | Door rule on every host and role: the core block in SubagentStart for dispatched roles, Codex `developer_instructions`, OpenCode agents and the dsh profile; the hook in Codex PreToolUse, the OpenCode plugin (`aloop.js`) and the dsh plugin (`aloop.mjs`, whose PreToolUse list mirrors `hooks.json` by hand and so lacks `mission doors hook`), with each host's question tool mapped. Also measure whether a human's own `!` shell command reaches PreToolUse (it should not be gated: the human crossing a door is the authorization). Reason: M9.30 wires Claude Code's main session only | M9.30, M9.9 | `plugin/bin/host-adapter`, `install`, `aloop.js`, `aloop.mjs`, `_host.py` | after M9.9 | done: the core block reaches every role on every host (`mission principles hook`, the third SubagentStart command; Codex agents' `developer_instructions`; OpenCode's system transform for a sub-agent and a resumed root; each dsh role persona, written by `install`). The door gate runs on Codex through `hooks.json`, and last in the OpenCode and dsh plugins (after every other gate admitted the call, so only a call that runs uses up a once-authorization; read-only tools skip it; 15 s, fail closed). Each host's question tool is the gate's (`AskUserQuestion`, Codex `request_user_input`, OpenCode `question`, dsh `ask_user_question`). A bound worker's command is read where it runs (`tool_input.workdir`, a relative one under the project), a dsh MCP tool's input is its own arguments (its once digest is the one any host prints), and an OpenCode MCP tool is `mcp____` (from `client.mcp.status()`, the longest server first). One deadline covers the decision (`Deadline`, which no step's `except Exception` can swallow into an allow), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Two reviews (12 and 7 findings, each a test that failed first) added: `apply_patch` read as the hosts parse it (any header spacing or indent, a U+FEFF around the path cut as OpenCode's `trim()` cuts it, and through the shell: Codex's heredoc and argument forms, `applypatch`, after a `cd`, from a pipe; the header reader `_patch.py` is shared with host-adapter and both scope gates, but only the door gate reads the shell form, M9.58); a bare redirect, and a group's or compound's own redirect or heredoc, reach the commands inside; a `cd` that may not run leaves the directory unknown; a path through a symlink then `..` read as the kernel walks it (`git -C`, `cd -P`, a dsh `workdir`); settings replaced, moved, linked or removed by `cp`/`mv`/`ln`/`install`/`rsync` (into a settings directory too, by `-t` or a trailing `DIR/`), `rm`/`mv` of a directory that holds them, a glob that names them, `sed -i` (and `--in-pl`), `perl -i`; `opencode.json` a door only where OpenCode reads it (a nested checkout's root, `~/.config/opencode`, its `config.json`); an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`, `mcp__google-drive__share_file`) crosses the same door as the service's own server, while a service word inside another tool's name (`search_slack_posts`, `get_post_history`) is none. Tests: `mission-doors.test.py` (36 checks), `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`, `hook-matchers.test.py`, `mission-principles.test.py`, `dsh-install.test.py`. Calls: the catalogue keeps its generic `_` alternatives for an OpenCode that cannot list its servers (an extra question beats a silent one-way door). Open: M9.48, M9.49, M9.55–M9.61 (the `!` measurement moved to M9.55) | +| M9.31 | Door rule on every host and role: the core block in SubagentStart for dispatched roles, Codex `developer_instructions`, OpenCode agents and the dsh profile; the hook in Codex PreToolUse, the OpenCode plugin (`aloop.js`) and the dsh plugin (`aloop.mjs`, whose PreToolUse list mirrors `hooks.json` by hand and so lacks `mission doors hook`), with each host's question tool mapped. Also measure whether a human's own `!` shell command reaches PreToolUse (it should not be gated: the human crossing a door is the authorization). Reason: M9.30 wires Claude Code's main session only | M9.30, M9.9 | `plugin/bin/host-adapter`, `install`, `aloop.js`, `aloop.mjs`, `_host.py` | after M9.9 | done: the core block reaches every role on every host (`mission principles hook`, the third SubagentStart command; Codex agents' `developer_instructions`; OpenCode's system transform for a sub-agent and a resumed root; each dsh role persona, written by `install`). The door gate runs on Codex through `hooks.json`, and last in the OpenCode and dsh plugins (after every other gate admitted the call, so only a call that runs uses up a once-authorization; read-only tools skip it; 15 s, fail closed). Each host's question tool is the gate's (`AskUserQuestion`, Codex `request_user_input`, OpenCode `question`, dsh `ask_user_question`). A bound worker's command is read where it runs (`tool_input.workdir`, a relative one under the project), a dsh MCP tool's input is its own arguments (its once digest is the one any host prints), and an OpenCode MCP tool is `mcp____` (from `client.mcp.status()`, the longest server first). One deadline covers the decision (`Deadline`, which no step's `except Exception` can swallow into an allow), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Three reviews (12, 7 and 34 findings, each a test that failed first) added: `apply_patch` read as the hosts parse it (any header spacing or indent, a U+FEFF around the path cut as OpenCode's `trim()` cuts it, and through the shell: Codex's heredoc and argument forms, `applypatch`, after a `cd`, from a pipe; the header reader `_patch.py` is shared with host-adapter and both scope gates, but only the door gate reads the shell form, M9.58); a bare redirect, and a group's or compound's own redirect or heredoc, reach the commands inside; a `cd` that may not run leaves the directory unknown; a path through a symlink then `..` read as the kernel walks it (`git -C`, `cd -P`, a dsh `workdir`); settings replaced, moved, linked or removed by `cp`/`mv`/`ln`/`install`/`rsync` (into a settings directory too, by `-t` or a trailing `DIR/`), `rm`/`mv` of a directory that holds them, a glob that names them, `sed -i` (and `--in-pl`), `perl -i`; `opencode.json` a door only where OpenCode reads it (a nested checkout's root, `~/.config/opencode`, its `config.json`); an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`, `mcp__google-drive__share_file`) crosses the same door as the service's own server, while a service word inside another tool's name (`search_slack_posts`, `get_post_history`) is none. The third review: a `cd` that may not run leaves each later command in either directory (one view per directory, not an unknown one), `cd -`, a bare `pushd`, `popd -n`, `cd X &`, an eval's `cd`, `~+` and `GIT_DIR`/`GIT_WORK_TREE` are read as the shell reads them, and a pipe into a `{ }` group or a compound reaches each command inside; a copy is placed where it runs (DEST/NAME only into an existing directory, so a backup of `.claude` or a file copied into it passes; `-t` with `{}` or xargs' arguments, `--parents`, `-R`, `--remove-source-files`, a link to a settings file, a home directory's contents, GNU option abbreviations); a glob is expanded only where the shell expands it, through links, and never loses a config-like name; another checkout's run home is not this project's; and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Google Workspace's Drive grants cross their doors. Tests: `mission-doors.test.py` (39 checks), `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`, `hook-matchers.test.py`, `mission-principles.test.py`, `dsh-install.test.py`. Calls: the catalogue keeps its generic `_` alternatives for an OpenCode that cannot list its servers (an extra question beats a silent one-way door). Open: M9.48, M9.49, M9.55–M9.62 (the `!` measurement moved to M9.55) | | M9.32 | Intent questions name their door: `open_questions[].door`; `intent check` blocks an assume/default on a one-way door and treats an `ask` on a door the human authorized as answered. Reason: the plan-time half of the door rule | M9.30, M9.11 | `plugin/bin/intent`, its test | yes | open | | M9.33 | Remove the model-facing `SKIP_HOOKS=1` hints (`gate-supervisor-scope`, `_failclosed.sh`, `SKILL.md`). Reason: they hand the agent the bypass-gate wall's own key | M9.30 | those files | yes | open | | M9.34 | A git pre-push backstop from `aloop init` for pushes to the default branch and force pushes. Reason: holds when Bash matching is evaded (a script, an alias) | M9.30 | `plugin/bin/aloop-init`, its test | yes | open | @@ -496,6 +496,7 @@ Each task flips only its own row here and in TRACKING.md. | M9.59 | In the OpenCode plugin only the door gate has a fail-closed timeout: a hung host-adapter, scope or budget gate waits 10 minutes and its timeout reads as an allow (no exit status, no deny). Give each gate the hook's timeout and fail closed. Reason: the M9.31 review | M9.31 | `plugin/hosts/opencode/aloop.js`, its test | yes | open | | M9.60 | An OpenCode that cannot list its MCP servers (`client.mcp.status()` fails) hands the gates raw `_` names, which the catalogue's generic alternatives read with false positives (`drive_get_share` asks). Read the server names from the config too. Reason: the M9.31 review | M9.31 | `plugin/hosts/opencode/aloop.js` | yes | open | | M9.61 | A path the shell computes inside a patch or a write target: an unquoted heredoc's `$HOME`/`$(…)` in an `apply_patch` header, or `> "$D/settings.json"`, is judged as written (M9.47's class). Expand what the line assigns before it, and ask for a computed path under a settings directory. Reason: the second M9.31 review | M9.47 | `plugin/mission/doors.py`, its test | yes | open | +| M9.62 | What the third M9.31 review left in the door gate's reader: a heredoc or redirect on a `while`/`until` loop reaches its body but not its condition (`while bash; do …; done <> .codex/hooks.json", "printf x | tee ~/.dsh/profiles/aloop/cordis.patch.yml", "git config core.hooksPath /dev/null", - "git config --unset core.hooksPath" + "git config --unset core.hooksPath", + "ln .claude/settings.json alias.json", + "cp -l .claude/settings.json x", + "cp -a /tmp/dot/. ~", + "rsync --remove-source-files .claude/settings.json /tmp/bak/", + "find /tmp/e -exec cp -t .claude {} +" ] } }, @@ -784,7 +820,10 @@ "rm -rf .opencode/node_modules", "cp -r docs/. /tmp/site", "sed --quiet -n p .claude/settings.json", - "cp notes.md .claude/agents/" + "cp notes.md .claude/agents/", + "cp -r .claude /tmp/claude-backup", + "cp -r .claude .claude.bak", + "rsync -a .claude /tmp/backup/" ], "tools": [ "Read", @@ -802,7 +841,12 @@ "mcp__gmail__list_drafts", "mcp__zapier__gmail_find_email", "mcp__drive__list_shares", - "mcp__twitter__get_posts" + "mcp__twitter__get_posts", + "mcp__slack__slack_send_message_draft", + "mcp__slack__slack_posts_list", + "mcp__gitlab__get_merge_request", + "mcp__google-workspace__draft_gmail_message", + "mcp__workspace-mcp__get_drive_file_permissions" ], "paths": [ "src/a.py", diff --git a/plugin/mission/doors.py b/plugin/mission/doors.py index 25754d54..b06f5eb3 100644 --- a/plugin/mission/doors.py +++ b/plugin/mission/doors.py @@ -83,7 +83,8 @@ # plugin exports ALOOP_AGENT_SHELL); an authorization never comes from there AGENT_SHELL = ("CLAUDECODE", "DSH_SHELL", "CODEX_THREAD_ID", "ALOOP_AGENT_SHELL") TAG = re.compile(r"\[door:([^\]\s]*)\]") -WRAPPERS = {"sudo", "doas", "env", "nohup", "time", "command", "exec", "nice", "timeout", "stdbuf", "caffeinate", "ionice"} +WRAPPERS = {"sudo", "doas", "env", "nohup", "time", "command", "exec", "nice", "timeout", "stdbuf", "caffeinate", "ionice", + "builtin", "setsid", "taskset", "chrt", "flock"} SHELLS = {"sh", "bash", "zsh", "dash", "ksh"} ASSIGN = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*=") # {DRY} in a pattern: the command is not a dry run or a help page, read from its words (dry()), so a @@ -219,22 +220,77 @@ def render(parts): out.append("<(…)") else: out.append("*") - return "".join(out) + text = "".join(out) # ~+ is $PWD, ~- is $OLDPWD + return re.sub(r"^~([+-])(?=/|$)", lambda m: "$PWD" if m.group(1) == "+" else "$OLDPWD", text) def chdir(cur, arg): """The directory a `cd ARG` (or `git -C ARG`) moves to, relative to the call's cwd ("" = it): `$PWD` is where it is, `$(git rev-parse --show-toplevel)` the checkout's top, `$CLAUDE_PROJECT_DIR` - the project's.""" + the project's; `cd ''` stays; an absolute ARG is itself wherever the command was.""" if arg is not None and arg.startswith(TOP): return arg for var, base in (("$PWD", cur), ("$CLAUDE_PROJECT_DIR", os.environ.get("CLAUDE_PROJECT_DIR") or UNKNOWN)): if arg is not None and re.match(re.escape(var) + r"(?:/|$)", arg): return UNKNOWN if base == UNKNOWN else os.path.join(base, arg[len(var) + 1:]) if arg[len(var):] else base - if cur == UNKNOWN or arg is None or arg == "-" or re.search(r"[$`*?]", arg): + if arg is None or arg == "-" or re.search(r"[$`*?]", arg): return UNKNOWN - arg = os.path.expanduser(arg or "~") - return arg if os.path.isabs(arg) else os.path.join(cur, arg) + if arg == "": + return cur + arg = os.path.expanduser(arg) + return arg if os.path.isabs(arg) else UNKNOWN if cur == UNKNOWN else os.path.join(cur, arg) + + +MAXDIRS = 8 # the directories a line's conditional cds may leave it in; past that, one more is UNKNOWN +GITENV = ("GIT_DIR", "GIT_WORK_TREE", "GIT_COMMON_DIR") # git reads its repository from these, not its cwd + + +def union(*groups): + """The directories a command may run in (a tuple, in order, without repeats).""" + out = tuple(dict.fromkeys(d for g in groups for d in g)) + return out if len(out) <= MAXDIRS else out[:MAXDIRS - 1] + (UNKNOWN,) + + +def moved(prog, args, cur, old, dirs): + """(cur, old) after a cd, pushd or popd (dirs, the pushd stack, is changed in place): `cd -` goes back + to where the last cd left, a bare pushd swaps with the stack's top, `-n` moves nothing, a ±N rotation + is unknown.""" + ops, flags, i = [], set(), 0 + while i < len(args): + a = args[i] + if a == "--": + ops += args[i + 1:] + break + if re.fullmatch(r"-[A-Za-z@]+", a): # -L, -P, -e, -n; `-` and ±N are operands + flags |= set(a[1:]) + else: + ops.append(a) + i += 1 + arg = ops[0] if ops else None + if prog == "cd": + if arg == "-": + return old, cur + return union(*((chdir(d, "~" if arg is None else arg),) for d in cur)), cur + if arg is not None and re.fullmatch(r"[-+]\d+", arg): # a rotation of the stack: unknown + return union(cur, (UNKNOWN,)), cur + if prog == "pushd": + if arg is None: + if not dirs: + return cur, old + top = dirs.pop() + dirs.append(cur) + return top, cur + to = union(*((chdir(d, arg),) for d in cur)) + if "n" in flags: + dirs.append(to) + return cur, old + dirs.append(cur) + return to, cur + if "n" in flags: # popd -n: the stack's top goes, the shell stays + if dirs: + dirs.pop() + return cur, old + return (dirs.pop() if dirs else (UNKNOWN,)), cur def views(cmd, depth=0, cur=""): @@ -242,79 +298,120 @@ def views(cmd, depth=0, cur=""): the scope gate uses (plugin/bin/_shellread.py): one per simple command, one per wrapper (sudo, env, timeout, xargs, find -exec, …) around it, the commands inside `sh -c`, `eval`, a script a shell runs, a command substitution, and the stdin that feeds a shell or python (the last heredoc, here-string or - `<` on fd 0, else the pipe, into a subshell too), each with the directory it runs in (`cd DIR`, - `pushd`/`popd`, `git -C DIR`; a subshell's `cd` stays in the subshell). A group's, subshell's or - compound's own redirect (`{ …; } > F`, `( … ) < F`) is every command's inside it, and a bare - redirect (`> F`) is a view with no program. A `cd` that may not run (in an if, case or loop body or a - `{ …; }` group, or after `&&`/`||`) leaves the directory UNKNOWN once its list ends. Deeper than DEPTH - is a ValueError: a gate that cannot read the whole command fails closed.""" + `<` on fd 0, else the pipe, into a subshell, a group or a compound too), each with the directory it runs + in (`cd DIR`, `cd -`, `pushd`/`popd`, `git -C DIR`; a subshell's, a pipeline element's or a background + job's `cd` stays there; GIT_DIR or GIT_WORK_TREE makes git's own unknown). A group's, subshell's or + compound's own redirect (`{ …; } > F`, `( … ) < F`) is every command's inside it that has + none of its own, and a bare redirect (`> F`) is a view with no program. A `cd` that may not run (in an + if, case or loop body or a `{ …; }` group, after `&&` or `||`, or one that may fail before `||`) leaves + each later command in either directory: one view per directory. Deeper than DEPTH is a ValueError: a + gate that cannot read the whole command fails closed.""" if depth > DEPTH: raise ValueError("the command nests shells, evals or substitutions deeper than the door gate reads (%d)" % DEPTH) + cur = (cur,) if isinstance(cur, str) else tuple(cur) out, stack, dirs, fed, group = [], [], [], None, None - opened, closed, scopes, prev, maybe = [], None, [], None, None + opened, closed, scopes, prev = [], None, [], None + maybe, failed, old, gitenv = None, None, (UNKNOWN,), False for c in R.read(cmd): if isinstance(c, str): if c == "(": # a pipe into a subshell feeds the commands inside it - stack.append((cur, group)) - group = fed if fed is not None else group + stack.append((cur, group, maybe, failed)) + group, maybe, failed = (fed if fed is not None else group), None, None opened.append(len(out)) elif c == ")": if stack: - cur, group = stack.pop() + cur, group, maybe, failed = stack.pop() closed = opened.pop() if opened else None elif c == "cwd+": # an if, case, loop body or { …; } group: its `cd` may or may not run - scopes.append([cur, False]) + scopes.append((cur, [], group, maybe, failed)) + group, maybe, failed = (fed if fed is not None else group), None, None opened.append(len(out)) elif c == "cwd|" and scopes: # a branch ends: the next one starts where the compound did - scopes[-1][1] = scopes[-1][1] or cur != scopes[-1][0] + scopes[-1][1].append(cur) cur = scopes[-1][0] elif c == "cwd-": if scopes: - entry, moved = scopes.pop() - cur = UNKNOWN if moved or cur != entry else entry + entry, tails, group, maybe, failed = scopes.pop() + cur = union(entry, *tails, cur) closed = opened.pop() if opened else None - if c in ("(", ")"): - fed = None - prev = None + if c in (")", "cwd-") and maybe is not None: # the list around it may end here (its end is not read) + cur, maybe, failed = union(maybe, cur), None, None + fed, prev = None, None continue own, closed = (closed if not c.words and not c.assigns else None), None + here = cur + if prev == "||" and failed is not None: + here = failed # `cd X || Y`: Y runs where the cd failed + elif prev == "||" and maybe is not None: + here = union(maybe, cur) # `… && cd X && a || Y`: Y runs after any of them failed for sub in c.subs: - out += views(sub, depth + 1, cur) - vs = simple(c, depth, cur) + for d in here: + out += views(sub, depth + 1, d) + vs = [v for d in here for v in simple(c, depth, d)] for v in vs: # what ended it (`&&`: the next runs only if it succeeded) v.setdefault("end", c.end) v.setdefault("depth", depth) + if v["prog"] == "git" and (gitenv or any(e.split("=", 1)[0] in GITENV for e in v["env"])): + v["dir"] = UNKNOWN # git's repository is elsewhere: its branch, its worktree unknown + word0 = c.words[0].text() if c.words else None + if word0 in ("export", "declare", "typeset", "readonly", "local") and any( + (w.text() or "").split("=", 1)[0] in GITENV for w in c.words[1:]) or not c.words and any( + n in GITENV for n, _ in c.assigns): + gitenv = True + mover = next((v for v in vs if v["prog"] in ("cd", "pushd", "popd") and v["depth"] == depth), None) prog = vs[0]["prog"] if vs else "" - if c.pipe: # a pipeline element runs in a subshell: its `cd` does not leak + if c.pipe or c.end == "&": # a pipeline element or a background job runs in a subshell pass - elif prog in ("cd", "pushd"): - if prog == "pushd": - dirs.append(cur) + elif mover is not None: + before = here + cur, old = moved(mover["prog"], mover["args"], here, old, dirs) if prev in ("&&", "||") and maybe is None: - maybe = cur # it may not run: known only along its own && chain - cur = chdir(cur, next((a for a in vs[0]["args"] if not a.startswith("-")), "")) - elif prog == "popd": - cur = dirs.pop() if dirs else UNKNOWN + maybe = before # it may not run: known only along its own list + if prev == "||": # after `a ||` it ran only if a failed + cur = union(before, cur) + if c.end == "||": # `cd X || Y`: Y runs where the cd failed; after it, either + failed, maybe = before, (maybe if maybe is not None else before) + elif prev == "||" and failed is not None and prog in ("exit", "return"): + maybe = None # `cd X || exit`: what follows runs in X + if any(v["prog"] == "eval" and v["depth"] == depth for v in vs) and any( + v["prog"] in ("cd", "pushd", "popd") and v["depth"] > depth for v in vs): + cur = union(cur, (UNKNOWN,)) # eval's cd moves this shell too stdin = fed if fed is not None else group + inherited = fed is None for op, fd, w in c.redirs: # the last redirect of fd 0 wins, left to right if fd in ("", "0") and op in ("<<", "<<-", "<<<", "<", "<&", "<>"): stdin = w.body if op in ("<<", "<<-") else render(w.parts) if op == "<<<" else None + inherited = False if own is not None: # the group's own redirects: its commands write and read them inner = out[own:] for v in inner: v["writes"] = v["writes"] + (vs[0]["writes"] if vs else []) - if stdin is not None: - out += feed(inner, stdin, depth) + if stdin is not None: # each command inside reads it unless it has its own stdin + targets = [v for v in inner if v.get("stdin") != "own"] + for v in inner: + v["stdin"] = "own" + out += own_stdin(feed(targets, stdin, depth)) elif stdin is not None and vs: - out += feed(vs, stdin, depth) + for v in vs: + v["stdin"] = "inherited" if inherited else "own" + out += own_stdin(feed(vs, stdin, depth)) fed = stdout(vs, stdin) if c.end in ("|", "|&") else None out += vs - if maybe is not None and c.end not in ("&&", "|", "|&"): - cur, maybe = (UNKNOWN if cur != maybe else cur), None + if c.end not in ("&&", "||", "|", "|&"): + if maybe is not None: + cur = union(maybe, cur) + maybe, failed = None, None prev = c.end return out +def own_stdin(vs): + """Views read from a stdin text: theirs is that text, no enclosing group's.""" + for v in vs: + v["stdin"] = "own" + return vs + + def stdout(vs, stdin): """The text a command writes to the pipe, when the gate can know it: echo's and printf's words, and cat or tee passing its own heredoc, here-string or pipe through.""" @@ -367,6 +464,8 @@ def reads_stdin(v): """Does this shell or python run the program on its stdin? A shell with only options (no -c, no script operand), or with `-s` or `-`; a python with no -c, -m or script, or `-`. xargs feeds arguments, not stdin.""" shell, args, i = v["prog"] in SHELLS, v["args"], 0 + if v["prog"] in ("source", "."): # source /dev/stdin: this shell runs its stdin + return bool(args) and args[0] in STDIN_FILES if STDIN in args or not (shell or PYTHON.fullmatch(v["prog"])): return False while i < len(args): @@ -411,7 +510,9 @@ def feed(vs, text, depth): "doas": {"-u", "-C"}, "nice": {"-n", "--adjustment"}, "timeout": {"-s", "-k", "--signal", "--kill-after"}, "stdbuf": {"-i", "-o", "-e", "--input", "--output", "--error"}, "caffeinate": {"-t", "-w"}, "ionice": {"-c", "-n", "-p", "-P", "-u", "--class", "--classdata", "--pid", "--pgid", "--uid"}, - "exec": {"-a"}, "time": {"-f", "-o", "--format", "--output"}} + "exec": {"-a"}, "time": {"-f", "-o", "--format", "--output"}, + "flock": {"-w", "-E", "--wait", "--timeout", "--conflict-exit-code"}, + "chrt": {"-T", "-P", "-D", "--sched-runtime", "--sched-period", "--sched-deadline"}} COPY_PAIRED = { # the options of a copy, move, link or install that take a separate value @@ -428,11 +529,16 @@ def feed(vs, text, depth): def copied(prog, args): - """The paths a copy, move, link or install writes: DEST, and DIR/NAME for each source when the - destination may be a directory (-t DIR, more than one source, or a DEST the gate cannot rule out), or - DIR/* for a source whose contents are copied (rsync's SRC/, SRC/.). Nothing for `install -d`, a - remote rsync destination or one operand.""" - paired, ops, target, whole, i = COPY_PAIRED[prog], [], None, False, 0 + """What a copy, move, link or install writes, as plans place() resolves where the command runs: + {prog, dest, srcs, target (-t DIR), whole (-T), parents (cp --parents, rsync -R: DEST/SRC), removes + (mv, rsync --remove-source-files: its sources go)}. A long option matches by its unique GNU prefix. + After an option that follows an operand, any later operand may be the destination (the option may + take a value the gate does not know). Nothing for `install -d` or one operand (but `ln -s TARGET`).""" + paired, ops, target, i = COPY_PAIRED[prog], [], None, 0 + whole, parents, removes, late = False, False, prog == "mv", False + + def is_(k, name, least): + return len(k) >= least and name.startswith(k) while i < len(args): a = args[i] if a == "--": @@ -440,15 +546,21 @@ def copied(prog, args): break if a.startswith("--"): k, eq, v = a.partition("=") - if k == "--target-directory" and prog != "rsync": + late = late or bool(ops) + if prog != "rsync" and is_(k, "--target-directory", 3): target, i = (v, i) if eq else (args[i + 1] if i + 1 < len(args) else None, i + 1) - elif k == "--no-target-directory": + elif prog != "rsync" and is_(k, "--no-target-directory", 5): whole = True - elif k == "--directory" and prog == "install": + elif prog == "install" and is_(k, "--directory", 4): return [] - elif not eq and k in paired: + elif prog == "cp" and is_(k, "--parents", 4) or prog == "rsync" and is_(k, "--relative", 5): + parents = True + elif prog == "rsync" and is_(k, "--remove-source-files", 7): + removes = True + elif not eq and any(is_(k, p, 4) for p in paired if p.startswith("--")): i += 1 elif a.startswith("-") and a != "-": + late = late or bool(ops) for j in range(1, len(a)): f = "-" + a[j] if f == "-t" and prog != "rsync": @@ -458,28 +570,61 @@ def copied(prog, args): whole = True elif f == "-d" and prog == "install": return [] + elif f == "-R" and prog == "rsync": + parents = True elif f in paired: i += 0 if a[j + 1:] else 1 break else: ops.append(a) i += 1 + plan = {"prog": prog, "target": False, "whole": whole, "parents": parents, "removes": removes} if target is not None: - dest, srcs, out = target, ops, [] - elif prog == "ln" and len(ops) == 1: - dest, srcs, out = ".", ops, [] # ln -s TARGET: a link named after it, here - elif len(ops) >= 2: - dest, srcs, out = ops[-1], ops[:-1], [ops[-1]] - else: - return [] - if prog == "rsync" and re.match(r"^[^/]*:", dest): - return [] # host:path is not this machine's - for s in [] if whole else srcs: - if s.endswith("/.") or prog == "rsync" and s.endswith("/"): - out.append(dest.rstrip("/") + "/*") # its contents, whatever they are named - elif os.path.basename(s.rstrip("/")) not in ("", ".", ".."): - out.append(dest.rstrip("/") + "/" + os.path.basename(s.rstrip("/"))) - return out + return [dict(plan, dest=target, srcs=ops, target=True)] + if prog == "ln" and len(ops) == 1: + return [dict(plan, dest=".", srcs=ops, target=True)] # ln -s TARGET: a link named after it, here + return [dict(plan, dest=ops[k], srcs=ops[:k]) for k in (range(1, len(ops)) if late else [len(ops) - 1]) + if k >= 1] + + +def copy_writes(cwd, v, plan): + """The paths a copy plan writes where its command runs: DEST/NAME for each source when DEST is a + directory (or -t DIR, or more than one source or a trailing / or a DEST the gate cannot place), else + DEST itself (the copy becomes DEST); DEST/* for a source whose contents are copied (SRC/., rsync's + SRC/) or that the gate cannot see ({}, xargs' arguments, a variable); DEST/SRC under --parents or -R; + and the sources a move takes away. A remote rsync destination is not this machine's.""" + dest, srcs, rsync = plan["dest"], plan["srcs"], plan["prog"] == "rsync" + out = list(srcs) if plan["removes"] else [] + if dest is None or rsync and re.match(r"^[^/]*:", dest): + return out + if plan["whole"]: + return out + [dest] + where = where_of(v, dest) + full = resolve(cwd, where) if where != UNKNOWN else "" + isdir = os.path.isdir(full) if full else None # None: the gate cannot tell + base = dest.rstrip("/") or "/" + + def inside(s): + if plan["parents"]: + s = s.split("/./", 1)[1] if rsync and "/./" in s else s + return base + "/" + s.lstrip("/") + if s in (STDIN, "{}") or re.search(r"[$`]", s) or s.endswith("/.") or rsync and s.endswith("/"): + return base + "/*" # its contents, whatever they are named + name = os.path.basename(s.rstrip("/")) + return base + "/" + name if name not in ("", ".", "..") else base + "/*" + + def a_dir(s): # rsync copies a directory into DEST, as DEST/NAME + w = where_of(v, s) + f = resolve(cwd, w) if w != UNKNOWN else "" + return not f or os.path.isdir(f) + if plan["target"] or isdir: + return out + [inside(s) for s in srcs or [STDIN]] + if isdir is False and len(srcs) == 1 and not dest.endswith("/"): + s = srcs[0] + if rsync and not s.endswith("/") and a_dir(s): + return out + [inside(s)] + return out + [dest] + ([base + "/*"] if s.endswith("/.") or rsync and s.endswith("/") else []) + return out + [dest] + [inside(s) for s in srcs or [STDIN]] def simple(c, depth, cur=""): @@ -487,9 +632,15 @@ def simple(c, depth, cur=""): env = ["%s=%s" % (n, render(v)) for n, v in c.assigns] writes = [render(w.parts) for op, _, w in c.redirs if op in WRITES or (op == ">&" and not re.fullmatch(r"\d+|-", w.text() or "0"))] + globs = {render(w.parts) for w in list(c.words) + [w for _, _, w in c.redirs] + if any(isinstance(p, tuple) and p[0] == "unknown" for p in w.parts)} # the shell expands these if not c.words: # `> F`, `X=1 > F`: no program, still a write - return [{"prog": "", "args": [], "env": env, "writes": writes, "dir": cur, "config": []}] if writes else [] - return chain([render(w.parts) for w in c.words], env, writes, depth, cur) + return [{"prog": "", "args": [], "env": env, "writes": writes, "dir": cur, "config": [], "globs": globs}] \ + if writes else [] + vs = chain([render(w.parts) for w in c.words], env, writes, depth, cur) + for v in vs: + v.setdefault("globs", globs) + return vs def chain(words, env, writes, depth, cur): @@ -518,8 +669,6 @@ def chain(words, env, writes, depth, cur): prog, rest, code = os.path.basename(rest[j]), rest[j + 1:], None # python SCRIPT: the script runs if prog == "tee": writes = writes + [w for w in rest if not w.startswith("-")] - if prog in COPY_PAIRED: - writes = writes + copied(prog, rest) if prog in _patch.PATCHERS and rest: # apply_patch 'PATCH': each path it names is a write writes = writes + patched(rest[0]) if prog == "git": @@ -538,6 +687,8 @@ def chain(words, env, writes, depth, cur): rest = [] # `command -v X` looks X up; it runs nothing out.append({"prog": prog, "args": rest + ([STDIN] if stdin_args is True else []), "env": list(env), "writes": list(writes), "dir": cur, "config": list(config)}) + if prog in COPY_PAIRED: # its destination (and a move's sources), placed where it runs + out[-1]["copies"] = copied(prog, out[-1]["args"]) if code is not None: out[-1]["code"] = code if prog in SHELLS: # sh -c [options] STRING: the first operand after the options is the command @@ -562,6 +713,15 @@ def chain(words, env, writes, depth, cur): if prog == "eval": out += views(" ".join(rest), depth + 1, cur) break + if prog == "script": # script [-q…] -c CMD [FILE]: CMD runs in a shell + for j, a in enumerate(rest): + if a.startswith("--command="): + out += [dict(v, env=v["env"] + env) for v in views(a.split("=", 1)[1], depth + 1, cur)] + break + if (a == "--command" or re.fullmatch(r"-[a-zA-Z]*c", a)) and j + 1 < len(rest): + out += [dict(v, env=v["env"] + env) for v in views(rest[j + 1], depth + 1, cur)] + break + break if prog == "find": j = 0 while j < len(rest): @@ -612,8 +772,11 @@ def chain(words, env, writes, depth, cur): paired = WRAPPER_PAIRED.get(prog, ()) while words and words[0].startswith("-") and words[0] != "--": words = words[2:] if words[0] in paired else words[1:] - if prog == "timeout" and words and words[0] != "--": - words = words[1:] # the duration + if prog in ("timeout", "taskset", "chrt", "flock") and words and words[0] != "--": + words = words[1:] # the duration, the CPU mask, the priority, the lock file + if prog == "flock" and words[:1] in (["-c"], ["--command"]): + out += [dict(v, env=v["env"] + env) for v in views(words[1] if len(words) > 1 else "", depth + 1, cur)] + break return out @@ -791,7 +954,9 @@ def placed(cwd, top, v, path, kernel=False): return full -GLOB_MAX = 512 # the files a glob operand is expanded to, at most +GLOB_MAX = 512 # the files of a glob the gate lists (and any name a host's config or a run record may have) +GLOB_SCAN = 20000 # past this many, the glob's directory stands for the rest +KEEP = re.compile(r"(?:^|/)(?:\.[^/]*|[^/]*\.(?:jsonc?|jsonl|toml|ya?ml|m?js))$") def where_of(v, path): @@ -802,34 +967,75 @@ def where_of(v, path): def matches(cwd, v, path): - """The files a glob operand names now: the shell expands it before the command runs.""" - where = where_of(v, path) if re.search(r"[*?]", path) else UNKNOWN + """The files a glob operand names now (the shell expands an unquoted one before the command runs): + the first GLOB_MAX and every dotfile or config-like name, and past GLOB_SCAN its directory's `/*`.""" + if path not in v.get("globs", ()) or not re.search(r"[*?]", path): + return [] + where = where_of(v, path) full = resolve(cwd, where) if where != UNKNOWN else "" - return sorted(itertools.islice(glob.iglob(full), GLOB_MAX)) if full else [] + out = [] + for n, m in enumerate(glob.iglob(full) if full else ()): + if n >= GLOB_SCAN or len(out) >= 4 * GLOB_MAX: + out.append(os.path.join(os.path.dirname(full), "*")) + break + if n < GLOB_MAX or KEEP.search(m): + out.append(m) + return sorted(out) + +def ours(d, top): + """Is the checkout at d this project's: the project, one inside it, or one of its linked worktrees?""" + real = os.path.realpath(d) + if real == top or real.startswith(top + os.sep): + return True + try: + with open(os.path.join(d, ".git")) as f: + m = re.match(r"gitdir:\s*(.+)", f.read(4096)) + except OSError: + return False + return bool(m) and os.path.realpath(os.path.join(d, m.group(1).strip())).startswith( + os.path.join(top, ".git") + os.sep) -def checkout_rel(full): - """A path relative to the checkout that holds it (the nearest directory with a .git: a nested or - linked worktree's own root, where a host reads its config), or None.""" + +def checkout_rel(full, top): + """A path relative to the checkout of this project that holds it (the nearest directory with a .git, + if it is the project's: a nested or linked worktree's own root, where a host reads its config), or None.""" d = os.path.dirname(full) while d: if os.path.exists(os.path.join(d, ".git")): - return os.path.relpath(full, d) + return os.path.relpath(full, d) if ours(d, top) else None d = "" if os.path.dirname(d) == d else os.path.dirname(d) return None +def home_rel(full): + """`~/PATH` for a path in the home directory (where a host reads its user settings), or None.""" + home = os.path.realpath(os.path.expanduser("~")) + for f in dict.fromkeys((os.path.normpath(full), os.path.realpath(full))): + if f == home: + return "~" + if f.startswith(home + os.sep): + return "~/" + os.path.relpath(f, home) + return None + + def place(cwd, vs): """Each view's operands and writes as placed() reads them (files, placed_writes): as written and as the - kernel walks them, each file a glob names, and relative to the checkout that holds it.""" + kernel walks them, each file a glob names, relative to the project's checkout that holds it and to + the home directory; a copy's destination as copy_writes() places it.""" top = os.path.realpath(_paths.project(cwd)) def forms(v, a): - out = [placed(cwd, top, v, a, k) for k in (False, True)] + [placed(cwd, top, v, m) for m in matches(cwd, v, a)] + out = [placed(cwd, top, v, a, k) for k in (False, True)] + out += [placed(cwd, top, v, m, k) for m in matches(cwd, v, a) for k in (False, True)] where = where_of(v, a) if not a.startswith("-") and a != STDIN else UNKNOWN - own = checkout_rel(resolve(cwd, where, True)) if where != UNKNOWN and resolve(cwd, where) else None - return out + ([own] if own and not own.startswith("..") else []) + if where != UNKNOWN and resolve(cwd, where): + for k in (False, True): + full = resolve(cwd, where, k) + out += [x for x in (checkout_rel(full, top), home_rel(full)) if x and not x.startswith("..")] + return out for v in vs: + v["writes"] = v["writes"] + [w for p in v.get("copies", ()) for w in copy_writes(cwd, v, p)] v["files"] = list(dict.fromkeys(x for a in v["args"] if not a.startswith("-") for x in forms(v, a))) v["placed_writes"] = list(dict.fromkeys(x for w in v["writes"] for x in forms(v, w))) return vs @@ -877,7 +1083,7 @@ def rel_path(cwd, path): if r.startswith("..") and len(roots) == 1: roots.append(os.path.realpath(toplevel(cwd) or roots[0])) forms += [next((x for x in (os.path.relpath(full, t) for t in roots) if not x.startswith("..")), ".."), full, - checkout_rel(full) or ".."] + checkout_rel(full, roots[0]) or "..", home_rel(full) or ".."] p = os.path.relpath(full, PLUGIN) plug = plug or (p if not p.startswith("..") else None) return [x for x in dict.fromkeys(forms) if not x.startswith("..")], plug diff --git a/scripts/test/mission-doors.test.py b/scripts/test/mission-doors.test.py index eceab39c..786ce3f9 100644 --- a/scripts/test/mission-doors.test.py +++ b/scripts/test/mission-doors.test.py @@ -986,8 +986,126 @@ def swallowing(*a, **k): assert doors_of(cmd) == [], (cmd, doors_of(cmd)) assert "forge-human-record" in doors_of("sed --in-pla 's/a/b/' docs/agents/mission/ledger.jsonl") for x in (os.path.join(gwt, "opencode.json"), "~/.config/opencode/config.json"): - assert ids("Write", {"file_path": x}) == ["change-agent-settings"], x + assert ids("Write", {"file_path": x}, cwd=g.dir) == ["change-agent-settings"], x # this project's worktree ok("settings: copies into a settings directory, a settings directory removed or moved, globs, sed --in-pl, nested checkouts") + +# 21c) the third M9.31 review. A cd that may not run keeps both directories (where it started and where it +# goes), not an unknown one: a settings write after it is still placed, an ordinary command after it is +# not a wall, and an absolute cd brings the directory back +os.makedirs(os.path.join(p.dir, ".codex"), exist_ok=True) +open(os.path.join(p.dir, ".claude", "settings.json"), "w").write("{}") +for cmd in ("mkdir -p .claude && cd .claude; echo '{}' > settings.local.json", "{ cd .claude; }; echo '{}' > settings.json", + "if [ -d .claude ]; then cd .claude; fi\ncp /tmp/s settings.json", "git fetch && cd .codex; echo x > hooks.json", + "true && cd .claude; rm settings.json", "true && cd .codex; sed -i s/a/b/ hooks.json"): + assert "change-agent-settings" in doors_of(cmd), (cmd, doors_of(cmd)) +for cmd in ("mkdir -p out && cd out; python3 gen.py > events.jsonl", "test -d out || cd out; echo x > ledger.jsonl"): + assert doors_of(cmd) == [], (cmd, doors_of(cmd)) +os.makedirs(os.path.join(gwt, "web"), exist_ok=True) +for cmd in ("npm ci && cd web && npm run build; git push -u origin HEAD", "if [ -d web ]; then cd web; fi; npm test; git push", + "{ cd web; npm test; }; git push origin HEAD", "git commit -m x && cd web && npm test; cd %s && git push -u origin HEAD" % gwt, + "false && cd web; cd %s; git push origin HEAD" % gwt, "if [ -d web ]; then cd web; fi; cd %s; git push origin HEAD" % gwt): + assert hook(g, "Bash", {"command": cmd}, cwd=gwt).stdout == "", cmd # the worktree's own branch +# after `||` the cd may have been skipped for the rest of the chain; a subshell in the chain does not end it +# early; `cd -` and a bare pushd go back; popd -n moves nothing; `cd X &` and `cd ''` leave it; eval's cd counts +for cmd in ("true || cd %s && git push origin HEAD", "false && cd %s && (true); git push origin HEAD", + "cd %s && git log -1 && cd - && git push origin HEAD", "cd %s; cd -- -; git push origin HEAD", + "pushd %s; pushd; git push origin HEAD", "cd %s; pushd ..; popd -n; git push origin HEAD", + "cd %s & git push origin HEAD", "cd %s; eval cd ..; git push origin HEAD"): + assert "push-default-branch" in denied(hook(g, "Bash", {"command": cmd % gwt})), cmd +assert "push-default-branch" in denied(hook(g, "Bash", {"command": "cd '' && git push origin HEAD"})) +for cmd in ("GIT_DIR=%s/.git git push origin HEAD", "export GIT_DIR=%s/.git; git push origin HEAD", + "env GIT_DIR=%s/.git git push origin HEAD", "GIT_WORK_TREE=%s git reset --hard"): + assert denied(hook(g, "Bash", {"command": cmd % g.dir}, cwd=gwt)), cmd # git's own dir is elsewhere +for cmd in ("cd /tmp & rm -rf .aladdin", "rm -rf ~+/.aladdin", "truncate -s0 ~+/.aladdin/logs/events.jsonl"): + assert "forge-human-record" in doors_of(cmd), (cmd, doors_of(cmd)) +assert "change-agent-settings" in doors_of("echo {} > ~+/opencode.json") +# a pipe into a { } group or a compound feeds each command inside; `source /dev/stdin` and more exec +# wrappers (builtin, setsid, flock, taskset, script -c) run what they are given +main = "git push origin main" +for cmd in ("echo '%s' | { true; bash; }" % main, "echo '%s' | if true; then bash; fi" % main, + "printf 'x\\n%s\\n' | { read l; bash; }" % main, "source /dev/stdin <<< '%s'" % main, + ". /dev/stdin <<'EOF'\n%s\nEOF" % main, "echo '%s' | source /dev/stdin" % main, "builtin eval '%s'" % main, + "builtin source /dev/stdin <<< '%s'" % main, "setsid %s" % main, "flock /tmp/l %s" % main, + "flock -w 5 /tmp/l %s" % main, "taskset -c 0 %s" % main, "taskset 0x1 %s" % main, "script -qc '%s' /dev/null" % main): + assert "push-default-branch" in doors_of(cmd), (cmd, doors_of(cmd)) +# nested groups each with their own heredoc: read once each, not once per enclosing group (no exponential blowup) +nest = "bash; bash" +for i in range(16): + nest = "{ %s\n} < s", + "ln .claude/settings.json alias.json", "cp -l .claude/settings.json alias.json", "cp -as $PWD/.claude/settings.json s.json", + "cp --target=.claude /tmp/cfg/settings.json", "mv --targ=.claude /tmp/cfg/settings.json", + "install --target=.claude /tmp/cfg/settings.json", "ln -s --target=.claude /tmp/cfg/settings.json", + "cp /tmp/s .claude/settings.json --no-preserve mode", "cp /tmp/s .claude/settings.json --sparse always", + "rsync -a /tmp/s .claude/settings.json --log-format %n", "cp -r /tmp/conf oc.d && mv oc.d .config/opencode"): + assert "change-agent-settings" in doors_of(cmd), (cmd, doors_of(cmd)) +for cmd in ("cp -a /tmp/dot/. ~/", "cp -a /tmp/dot/. ~", "rsync -a /tmp/dot/ ~/"): + assert "change-agent-settings" in denied(hook(p, "Bash", {"command": cmd})), cmd +for cmd in ("cp notes.md .claude/", "cp -r .claude .claude.bak", "cp -r .claude/ /tmp/bak/", "cp -r .claude /tmp/claude-backup", + "cp -r .opencode /tmp/oc", "rsync -a .claude /tmp/backup/", "ln -s ../shared/CLAUDE.md .claude/", + "cp .claude/settings.json /tmp/x.json", "cp -r src/. build"): + assert doors_of(cmd) == [], (cmd, doors_of(cmd)) +# a glob is the shell's: through a link it reaches the link's target, and one that names more files than the gate +# lists asks (its directory is written); a quoted pattern is no glob +far = os.path.join(tempfile.mkdtemp(), ".claude") +os.makedirs(far) +open(os.path.join(far, "settings.json"), "w").write("{}") +os.symlink(far, os.path.join(p.dir, "h")) +for cmd in ("rm -f h/*", "rm -f h/s*", "echo {} > h/settings.j*", "sed -i s/a/b/ h/*.json"): + assert "change-agent-settings" in doors_of(cmd), (cmd, doors_of(cmd)) +many = tempfile.mkdtemp() +subprocess.run(["git", "init", "-q", many], check=True) +os.makedirs(os.path.join(many, ".claude")) +for i in range(D.GLOB_MAX + 100): + open(os.path.join(many, ".claude", "f%04d" % i), "w").close() +open(os.path.join(many, ".claude", "settings.json"), "w").write("{}") +assert "change-agent-settings" in doors_of("rm -f .claude/*", cwd=many) +t0 = time.time() +assert doors_of("find / -path '/*/*/*/*/*/x' -prune") == [] and time.time() - t0 < 3, time.time() - t0 +# another checkout's run home is not this project's; copying a tree into another checkout is no settings change here +demo = tempfile.mkdtemp() +subprocess.run(["git", "init", "-q", demo], check=True) +for cmd in ("rm -rf %s/.aladdin", "cd %s && rm -rf .aladdin", "truncate -s0 %s/.aladdin/logs/events.jsonl", + "rsync -a --delete dist/ %s/", "cp -r dist/. %s"): + assert doors_of(cmd % demo) == [], (cmd, doors_of(cmd % demo)) +ok("copies: -t, --parents, -R, --remove-source-files, links, home, abbreviations; globs through links; backups pass") +shutil.rmtree(many, ignore_errors=True) +shutil.rmtree(demo, ignore_errors=True) +shutil.rmtree(os.path.dirname(far), ignore_errors=True) +os.unlink(os.path.join(p.dir, "h")) + +# MCP names: Composio's VERBS_A_ form, kebab-case tools, scheduled messages, a verb before the service, GitLab and +# Bitbucket merges, Google Workspace's Drive access tools; drafts and reads pass +for tool, inp, want in ( + ("mcp__slack__SLACK_SENDS_A_MESSAGE_TO_A_SLACK_CHANNEL", {}, "send-external-message"), + ("mcp__composio__SLACK_SENDS_A_MESSAGE_TO_A_SLACK_CHANNEL", {}, "send-external-message"), + ("mcp__discord__send-message", {}, "send-external-message"), ("mcp__ms365__send-mail", {}, "send-external-message"), + ("mcp__slack__slack_schedule_message", {}, "send-external-message"), + ("mcp__composio__SLACK_SCHEDULE_MESSAGE", {}, "send-external-message"), + ("mcp__slack__conversations_add_message", {}, "send-external-message"), + ("mcp__google-workspace__send_gmail_message", {}, "send-external-message"), ("mcp__gmail__send_draft", {}, "send-external-message"), + ("mcp__gitlab__merge_merge_request", {}, "push-default-branch"), + ("mcp__composio__GITHUB_MERGE_A_PULL_REQUEST", {}, "push-default-branch"), + ("mcp__bitbucket__mergePullRequest", {}, "push-default-branch"), + ("mcp__workspace-mcp__manage_drive_access", {"action": "grant", "share_type": "anyone"}, "visibility-or-sharing"), + ("mcp__workspace-mcp__set_drive_file_permissions", {"link_sharing": "reader"}, "visibility-or-sharing")): + assert ids(tool, inp) == [want], (tool, ids(tool, inp)) +for tool, inp in (("mcp__slack__slack_send_message_draft", {}), ("mcp__slack__slack_posts_list", {}), + ("mcp__slack__slack_list_scheduled_messages", {}), ("mcp__google-workspace__draft_gmail_message", {}), + ("mcp__claude-code-remote__send_message", {}), ("mcp__gitlab__get_merge_request", {}), + ("mcp__gitlab__list_merge_requests", {}), ("mcp__workspace-mcp__manage_drive_access", {"action": "remove"}), + ("mcp__workspace-mcp__get_drive_file_permissions", {}), + ("mcp__workspace-mcp__set_drive_file_permissions", {"link_sharing": "off"})): + assert ids(tool, inp) == [], (tool, ids(tool, inp)) +ok("MCP: Composio, kebab-case, scheduled and verb-first sends; GitLab and Bitbucket merges; Drive access grants") shutil.rmtree(g.dir, ignore_errors=True) shutil.rmtree(other, ignore_errors=True) From 44ec2caec8185b36a8474672740a1369a6652536 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 17:04:26 +0000 Subject: [PATCH 10/14] M9.31: fourth review fixes (copy options, glob and contents copies, cd after ||, OLDPWD, wrapped cd) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - copied(): a no-value long option (rsync --checksum, install --strip) is no prefix of a value option; only an operand right after an unknown option widens the destination (at most 8), so long operand lists stay linear; ln -n/--no-dereference replaces a linked DEST. - copy_writes(): glob sources expand to what they match, contents copies list SRC's entries, unseen names are DEST/? (no root settings ASK). - views(): cd after || unions with the success path, OLDPWD follows a cd that may not run and is saved around subshells, `|| { …; exit; }` keeps the line in the cd's dir, a group after || starts where the left failed, a wrapped cd moves no shell; candidate dirs pass into nested reads. - _shellread: a closing mark carries the list operator after it (Mark.end). - placed(): an unplaceable relative path is normalized (./.claude). - Catalogue: $VAR/ prefix and DEST/? for settings dirs, record-dir contents forge the record, draft-sending tools send, Drive grant_batch shares. - mission-doors.test.py section 21d (3 checks, 92 cases red on 88204c6). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- CHANGELOG.md | 2 +- docs/ROADMAP.md | 2 +- plugin/bin/_shellread.py | 16 +- plugin/mission/doors.json | 36 ++-- plugin/mission/doors.py | 263 +++++++++++++++++++---------- scripts/test/mission-doors.test.py | 92 ++++++++++ 6 files changed, 309 insertions(+), 102 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc0fa825..655eba99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ tag in lockstep. Tag releases as `vMAJOR.MINOR.PATCH`. ### Added - Plan approval is a gate (ROADMAP M0.9): `gate-dispatch` starts no execution role (RED, SCAFFOLD, GREEN, structural review, final gate) of a sprint until the human approved that sprint's plan as it is now. `events authority --sprint N` exits 3 with the line to type, `approve SN ` (reviewed, the code naming the sha256 of the sprint's plan files) or `autonomous SN ` (autonomy for that sprint, re-plans included, the code naming the authority record); the task contract's `STORY_DIR` and task must be a live story and task of that plan's frozen `ids.json`; the line must be among the human's captured prompts and typed after the sprint's last decision (M9.17's mechanism). A changed plan, a re-plan in reviewed mode, a roll-back to an old line, another sprint's line, a negated line and a hand-edited or emitted record all block (`plan.approved` and `human.approval` cannot be `events emit`ted); an approved autonomous mission grants its own sprints autonomy with no line. `events approval SPRINT|TASK_ID` is the check. Codex's `spawn_agent` (`agent_type`, `message`) and OpenCode's `task` now reach `gate-dispatch` too, and `claude -p`, `codex exec` and `opencode run` from a tool call are blocked in every aloop project, not only where a mission is set. `e2e-live.sh` pre-approves with an `autonomous S1 ` line in its prompt. Reason: the north star allows no human help beyond approval, and the approval was a playbook rule a supervisor could skip. A 4-lens adversarial review found 9 defects: 7 fixed with a check red on the first head (cross-sprint or unplanned `STORY_DIR`, a retyped line kept stale, mission autonomy over a non-mission sprint, `transcripts prune` dropping the prompts grants cite, `codex plugin add` refused, a code past column 200, a crash on a malformed run-log line), 2 in the playbook (intake reuses a mission's handed-off intent; a reviewed re-plan waits for the human's new line). `transcripts prune` now keeps the human's prompts. Test: `plan-approval.test.py` (20 checks, red on master), plus the host suites. - Core agent principles and the one-way / two-way door rule (ROADMAP §4.12, M9.30): seven core principles from Amazon's Leadership Principles, the door rule first, go to every aloop agent at session start (`aloop mission principles show --core`). A two-way door (a branch, a local edit, a test, a draft PR) is decided, not asked. A one-way door (`aloop mission doors list`: a push or merge to the default branch, a force-push, a release, a deploy, a message sent outside, a secret, dropped data, …) is stopped by a PreToolUse gate with `ASK[door] ` until the human records an authorization in their own terminal (`aloop mission doors authorize ID --by NAME --reason TEXT [--until DATE] [--once DIGEST]`, a hash-chained ledger row); a wall (switching a gate off, forging a human record) is never crossed. While a run is live, a question to the human must name a door the gate stopped and nobody authorized. Reason: Adeel's request that agents decide two-way doors themselves and ask only at a one-way door not already authorized. Test: `mission-doors.test.py`. Hardened by an adversarial review (15 findings, each a test): the gate reads Bash as the shell does (comments, line continuations, heredoc bodies, `xargs`, `find -exec`, `echo … | sh`), a branch condition reads the directory the command acts on (`git -C`, `cd`), settings are guarded wherever they live, dry runs and `command -v` are two-way, the newest decision by time counts and a revoke holds on every branch, a once-grant is crossed once under a lock, a broken ledger is named, and `doors authorize` refuses an agent's shell. -- The door rule on every host and role (ROADMAP §4.12, M9.31): every dispatched role gets the core principles (`aloop mission principles hook` at SubagentStart, Codex agents' instructions, OpenCode's system prompt, each dsh role persona), and the door gate runs on Codex, OpenCode and dsh with each host's question tool mapped. In the OpenCode and dsh plugins it runs last, after every other gate admitted the call, and fails closed after 15 s. A bound worker's command is judged where it runs (its `workdir`), an MCP tool by its own arguments (`mcp____` on OpenCode too), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Hardened by three reviews (53 findings, each a test that failed first): a stalled decision can no longer fall through to an allow, `apply_patch` is read with any header spacing (a U+FEFF too) and through the shell, a bare redirect or a group's redirect reaches the commands inside, a `cd` that may not run leaves the directory unknown, a symlink then `..` is read as the kernel walks it, replacing a settings file with `cp`, `mv`, `ln`, `install`, `rsync` (into a settings directory too), `rm`, `sed -i` or `perl -i`, or removing a directory that holds one, is the settings door, and an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`) crosses the same door as the service's own. A `cd` that may not run leaves each later command in either directory, a copy is placed where it runs (so backing up `.claude` passes), and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Drive access grants are doors too. Tests: `mission-doors.test.py`, `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`. +- The door rule on every host and role (ROADMAP §4.12, M9.31): every dispatched role gets the core principles (`aloop mission principles hook` at SubagentStart, Codex agents' instructions, OpenCode's system prompt, each dsh role persona), and the door gate runs on Codex, OpenCode and dsh with each host's question tool mapped. In the OpenCode and dsh plugins it runs last, after every other gate admitted the call, and fails closed after 15 s. A bound worker's command is judged where it runs (its `workdir`), an MCP tool by its own arguments (`mcp____` on OpenCode too), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Hardened by four reviews (71 findings, each a test that failed first): a stalled decision can no longer fall through to an allow, `apply_patch` is read with any header spacing (a U+FEFF too) and through the shell, a bare redirect or a group's redirect reaches the commands inside, a `cd` that may not run leaves the directory unknown, a symlink then `..` is read as the kernel walks it, replacing a settings file with `cp`, `mv`, `ln`, `install`, `rsync` (into a settings directory too), `rm`, `sed -i` or `perl -i`, or removing a directory that holds one, is the settings door, and an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`) crosses the same door as the service's own. A `cd` that may not run leaves each later command in either directory, a copy is placed where it runs (so backing up `.claude` passes), and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Drive access grants are doors too. After `cd A || cd B` a line may be in A, B or where it started, a glob or contents copy names the files it copies, a name the gate cannot see is no settings change at the project root, and a wrapped `cd` (`nohup cd`) moves no shell. Tests: `mission-doors.test.py`, `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`. - DeepSeek Harness (dsh) host (ROADMAP §4.9): `aloop install dsh` (alias `deepseek`; `--project P` or `--global`) renders a `dsh --profile aloop` profile in `$DSH_HOME` and installs the skills. Its native plugin (`plugin/hosts/dsh/aloop.mjs`) runs the same gates as Claude Code on dsh's own events: a deny before a tool runs, a block after an invalid artifact write, and a stop gate that steers a role back up to 3 times before the supervisor is told it is BLOCKED (or STUCK, released unverified). Roles are agent presets on two subagent tools, one per model route (`deepseek-v4-pro` for planning, `deepseek-flash` for work by default; `MODEL_PLANNING_DSH` / `MODEL_WORKER_DSH` override). A writer's `aloop bind` moves its working directory into its worktree, and `host-adapter` confines its commands, edits and `working_directory` there. Usage is recorded Claude-shaped per role, so budgets, the ledger and `run-summary` work unchanged. `make e2e-live HOST=dsh` drives it live and exits 2 with the fix when no `DEEPSEEK_API_KEY` is set. Reason: DeepSeek's own harness is the cheapest route to a strong model. The profile records the project it serves (or `--global`), so another project's `aloop-init` never re-renders it, and gates get the worker shell's environment (no API key). The mission layer's six roles run on dsh too: presets on their route group's subagent tool, `gate-mission-scope` on every write and command, and `gate-mission-artifact` at each role's start and stop; the mission skill installs as `aloop-mission`. A second review (3 lenses; 11 confirmed findings, each fixed with a test) closed: a working directory that vanished (dsh's tools move it back to the main tree, so the plugin judges `ensure()`'s directory and `host-adapter` refuses to run from a missing one), gate switches from the project's `.env` (dsh loads it; `SKIP_HOOKS`, `GATE_RUN_TESTS`, `REPO_DIR`, `AGENTIC_*` from it never reach a gate), an interrupted Stop that dropped the sprint-close notice, and the shared profile: its owner is the main tree, an older unrecorded profile is claimed only by the project that installed it, and a project it does not serve is told so by `install status`, `aloop-init` and the session itself. Tests: `dsh-plugin.test.mjs` (40 checks, emulated), `dsh-runtime.test.mjs` (32 checks on the real pinned dsh 0.2.1-rc.2 with a scripted model; the CI hosts job installs it) and `dsh-install.test.py` (9). - Brand kit and repo front page: `docs/brand/` holds the mark (an **a** whose bowl is a red-to-green loop passing a gold gate, with a 16 px cut), lockups, light and dark README banners, favicons, app icons, the site's OG card and the GitHub social preview, all generated by `docs/brand/gen_brand.py` from one palette with WCAG checks (rules in `docs/brand/README.md`). The README leads with the mission layer, the agent principles and the door rule, the supported hosts and the measured demo runs; the site carries the favicon and Open Graph/Twitter card metadata; `SECURITY.md` and bug/feature issue forms are new. - A mission's gap lists count only as `mission evaluate` computed them (ROADMAP §4.12, M9.27): `evaluate` records `eval.completed` in the committed ledger, and `mission tick`, `evaluate check` and `goal` take the newest eval whose content that record covers, with a matching seal, in the ledger's order. An eval file written by hand, or edited (or re-stamped) after it was recorded, no longer sends the loop to `RESEARCH` as "goal met" or makes `goal derive` refuse what tick handed out, a clock that is behind no longer reorders them, and a fresh checkout derives and closes goals from the evals it committed. An eval or state named as journey or baseline evidence needs its record too. Tests: `mission-tick` check 32, `mission-journeys`. diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index d6f689f4..d3a9866c 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -466,7 +466,7 @@ Each task flips only its own row here and in TRACKING.md. | M9.28 | `tick` drives the goal commands (M9.11): a goal counts only with its `goal.derived`/`goal.approved` record and closes by `goal.met`; handouts name `goal derive`, `options` (spoon-fed), `intents`, `next` and `close`; an eval narrowed to a closed goal asks for a whole-mission eval. Reason: found when M9.11 landed, `tick` read goal files and their status, so a goal nobody derived steered the loop and a met goal was never closed | M9.8, M9.11 | `plugin/mission/tick.py`, `plugin/skills/mission/SKILL.md`, `mission-tick.test.py` | yes | done: `mission-tick.test.py` (29 checks; a real goal run on the real clock: derive → review → intents → next and a sprint per intent → narrowed eval → close → whole-mission eval → `RESEARCH`) | | M9.29 | A goal that crosses a hard line stops being the active goal (`evaluate.active_goal` skips it, or a recorded retirement). Reason: found wiring M9.28, `evaluate` keeps narrowing to a hard-lined goal and `goal derive` refuses a narrowed eval, so the mission deadlocks (`tick` hands it to the human meanwhile) | M9.10, M9.11 | `plugin/mission/{evaluate,goal}.py`, their tests | yes | done: `evaluate.active_goal` skips a goal an ethics hard line retired (it never closes), so the next eval covers the whole mission; `goal derive` from an eval narrowed to a retired goal says to evaluate again; `tick` hands out EVALUATE for such an eval, then DERIVE_GOAL, which takes the next gap (a hard line holds the retired goal's own gaps); when every open gap is held it is BLOCKED for the human (a change of the mission), not handed to a derive that would refuse. `mission-goal.test.py` (section 11), `mission-tick.test.py` (section 27). Call: retirement is derived from the hard line itself, not a new record, so it holds on a fresh clone with the ledger | | M9.30 | Core agent principles and the one-way / two-way door rule, enforced: seven core principles from Amazon's Leadership Principles (the door rule first) for every agent, injected at session start; a door catalogue, the human's standing or once authorization in the ledger, and a PreToolUse gate that stops an unauthorized one-way door (ASK) and a wall (BLOCK) and lets two-way doors pass; while a run is live a question must name a door the gate stopped. Reason: Adeel, 2026-10-20: "If it is two-way, then the agent should take the decision directly without bothering the user. But if it is one-way door, then it can bother user if user already have not specified to cross that door." | M9.0, M9.7 | `plugin/mission/doors.{py,json}`, `principles.{md,py}` (`## Core`), `plugin/bin/session-start`, `hooks.json` (one line), core: `_events.py` / `_mission.py` (door events), `events` (`GATE_OWNED`), `scripts/test/mission-doors.test.py` | after M9.7 | done: `mission doors list / classify / authorize / revoke / check / hook`, `mission principles show --core`; `mission-doors.test.py` (21 checks), `mission-principles.test.py` (+1 check). Calls: anything the catalogue does not list is two-way (a finite list cannot claim full enforcement, §7); Bash is matched by program and arguments after reading the line as the shell does, with the scope gate's reader (`_shellread.py`, M9.38: quotes, comments, line continuations, `$(…)`, a heredoc's body as data unless a shell or python runs it) and unwrapping `sudo`, `env`, `sh -c`, `eval`, `xargs`, `find -exec` and `echo … | sh`, so text that only mentions a door (`grep`, a commit message, a file written by a heredoc) stays two-way; a `when` condition (on the default branch, the main checkout) reads the directory the command acts on (`git -C DIR`, `cd DIR &&`), and one the gate cannot resolve counts; grants are standing (until revoked or `--until`), once (one action's digest, 24 h, used up by its first crossing, recorded in the ledger), never (a wall: switching a gate off, forging a human record; refused even for the human's `authorize`) and command (question-only doors answered by their own command); authorizations are hash-chained human ledger rows (an edited row, `events emit` or a stream-only event opens nothing; `HUMAN_EVENTS` are now gate-owned, which also closes a forged `baseline.vetoed`); a question is checked only while a run is live (an approved autonomous mission or an autonomous run, or a sprint whose latest plan is approved, for the session holding its lock), so intake, planning and the plan's own approval are asked; the core block is kept out of the goal blocks, so editing it never stales a goal's stamp; the hook fails closed (exit 2) and is silent outside an aloop project; measured: median 81 ms per two-way call, 102 ms per stopped one-way call (30 calls each, in parallel with the other `*` hooks). Calls from its adversarial review (15 confirmed findings over 3 lenses, each now a test that failed before its fix): the newest decision about a door is the newest by time (a union merge that puts an older authorization last reopens nothing), and the run log's revokes and crossings close on any branch of the checkout; a once-grant is checked and crossed under a lock (`.aladdin/state/doors.lock`); a broken ledger is named in the ASK, fails `doors check` and blocks `authorize` with the restore line; settings and the ledger are matched by path suffix (`~/.claude`, a worktree's own); a dry run, a help page and `command -v` are two-way, `--dry-run=none` is not; `git -c core.hooksPath=` is a wall and `git config core.hooksPath VALUE` a one-way door; arguments `xargs` adds from stdin may be any flag of a one-way door; `doors authorize` refuses an agent's shell (`CLAUDECODE` set: the human authorizes in their own terminal); Slack's real send tools (`slack_post_message`, `slack_reply_to_thread`) are one-way; a hard ethics line only the run log holds (recorded before the ledger did) still binds. Calls from its second review (26 confirmed findings over parser, catalogue and state lenses, each now a test that failed before its fix): the gate reads Bash with `_shellread.py` (M9.38), which now keeps each heredoc's body, the operator that ends a command and heredoc bodies inside `$(…)`, so touching operators (`); git push`), keywords, groups, function bodies, here-strings, IO-number redirects (`2>&1`) and quoted operators (`find \( … \) -exec`) hide nothing, a quote in `"$(cat <<'EOF' … EOF)"` is data, stdin (a heredoc, a here-string, `echo|printf|cat … |`) reaches only the command it feeds (a shell with options or `-s`, a python with `-`), and `pushd`/`popd`, `sh -c`, `eval` and a fed shell keep the directory; nesting deeper than 8 is unreadable and a read past 8 s fails closed (exit 2, under the hook's 15 s); python is a forge only when it calls a writer, `npm|pnpm|yarn -g` a system change only with an installing verb, the ledger only at `docs/agents/mission/ledger.jsonl`, `xargs … doors` only when stdin can be the subcommand (`-I R` replaces R), a secret command's dry run or help page is two-way, `git push origin HEAD|@` on the default branch is one-way, and erasing or editing the run log (`.aladdin/logs/events.jsonl`, whose revokes and crossings close doors) is a wall; a ledger row is written before its stream event and `ledger_problems` reads the stream first, so no gate sees another writer's half-written record as a lost row; a record is dated no earlier than any ledger row, so the human's fresh decision is the newest even after a merged future-dated row; a run stops being live when its plan's sprint closes (`sprints.json`) or a new intent arrives; a malformed run-log line never crashes the ethics check | -| M9.31 | Door rule on every host and role: the core block in SubagentStart for dispatched roles, Codex `developer_instructions`, OpenCode agents and the dsh profile; the hook in Codex PreToolUse, the OpenCode plugin (`aloop.js`) and the dsh plugin (`aloop.mjs`, whose PreToolUse list mirrors `hooks.json` by hand and so lacks `mission doors hook`), with each host's question tool mapped. Also measure whether a human's own `!` shell command reaches PreToolUse (it should not be gated: the human crossing a door is the authorization). Reason: M9.30 wires Claude Code's main session only | M9.30, M9.9 | `plugin/bin/host-adapter`, `install`, `aloop.js`, `aloop.mjs`, `_host.py` | after M9.9 | done: the core block reaches every role on every host (`mission principles hook`, the third SubagentStart command; Codex agents' `developer_instructions`; OpenCode's system transform for a sub-agent and a resumed root; each dsh role persona, written by `install`). The door gate runs on Codex through `hooks.json`, and last in the OpenCode and dsh plugins (after every other gate admitted the call, so only a call that runs uses up a once-authorization; read-only tools skip it; 15 s, fail closed). Each host's question tool is the gate's (`AskUserQuestion`, Codex `request_user_input`, OpenCode `question`, dsh `ask_user_question`). A bound worker's command is read where it runs (`tool_input.workdir`, a relative one under the project), a dsh MCP tool's input is its own arguments (its once digest is the one any host prints), and an OpenCode MCP tool is `mcp____` (from `client.mcp.status()`, the longest server first). One deadline covers the decision (`Deadline`, which no step's `except Exception` can swallow into an allow), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Three reviews (12, 7 and 34 findings, each a test that failed first) added: `apply_patch` read as the hosts parse it (any header spacing or indent, a U+FEFF around the path cut as OpenCode's `trim()` cuts it, and through the shell: Codex's heredoc and argument forms, `applypatch`, after a `cd`, from a pipe; the header reader `_patch.py` is shared with host-adapter and both scope gates, but only the door gate reads the shell form, M9.58); a bare redirect, and a group's or compound's own redirect or heredoc, reach the commands inside; a `cd` that may not run leaves the directory unknown; a path through a symlink then `..` read as the kernel walks it (`git -C`, `cd -P`, a dsh `workdir`); settings replaced, moved, linked or removed by `cp`/`mv`/`ln`/`install`/`rsync` (into a settings directory too, by `-t` or a trailing `DIR/`), `rm`/`mv` of a directory that holds them, a glob that names them, `sed -i` (and `--in-pl`), `perl -i`; `opencode.json` a door only where OpenCode reads it (a nested checkout's root, `~/.config/opencode`, its `config.json`); an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`, `mcp__google-drive__share_file`) crosses the same door as the service's own server, while a service word inside another tool's name (`search_slack_posts`, `get_post_history`) is none. The third review: a `cd` that may not run leaves each later command in either directory (one view per directory, not an unknown one), `cd -`, a bare `pushd`, `popd -n`, `cd X &`, an eval's `cd`, `~+` and `GIT_DIR`/`GIT_WORK_TREE` are read as the shell reads them, and a pipe into a `{ }` group or a compound reaches each command inside; a copy is placed where it runs (DEST/NAME only into an existing directory, so a backup of `.claude` or a file copied into it passes; `-t` with `{}` or xargs' arguments, `--parents`, `-R`, `--remove-source-files`, a link to a settings file, a home directory's contents, GNU option abbreviations); a glob is expanded only where the shell expands it, through links, and never loses a config-like name; another checkout's run home is not this project's; and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Google Workspace's Drive grants cross their doors. Tests: `mission-doors.test.py` (39 checks), `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`, `hook-matchers.test.py`, `mission-principles.test.py`, `dsh-install.test.py`. Calls: the catalogue keeps its generic `_` alternatives for an OpenCode that cannot list its servers (an extra question beats a silent one-way door). Open: M9.48, M9.49, M9.55–M9.62 (the `!` measurement moved to M9.55) | +| M9.31 | Door rule on every host and role: the core block in SubagentStart for dispatched roles, Codex `developer_instructions`, OpenCode agents and the dsh profile; the hook in Codex PreToolUse, the OpenCode plugin (`aloop.js`) and the dsh plugin (`aloop.mjs`, whose PreToolUse list mirrors `hooks.json` by hand and so lacks `mission doors hook`), with each host's question tool mapped. Also measure whether a human's own `!` shell command reaches PreToolUse (it should not be gated: the human crossing a door is the authorization). Reason: M9.30 wires Claude Code's main session only | M9.30, M9.9 | `plugin/bin/host-adapter`, `install`, `aloop.js`, `aloop.mjs`, `_host.py` | after M9.9 | done: the core block reaches every role on every host (`mission principles hook`, the third SubagentStart command; Codex agents' `developer_instructions`; OpenCode's system transform for a sub-agent and a resumed root; each dsh role persona, written by `install`). The door gate runs on Codex through `hooks.json`, and last in the OpenCode and dsh plugins (after every other gate admitted the call, so only a call that runs uses up a once-authorization; read-only tools skip it; 15 s, fail closed). Each host's question tool is the gate's (`AskUserQuestion`, Codex `request_user_input`, OpenCode `question`, dsh `ask_user_question`). A bound worker's command is read where it runs (`tool_input.workdir`, a relative one under the project), a dsh MCP tool's input is its own arguments (its once digest is the one any host prints), and an OpenCode MCP tool is `mcp____` (from `client.mcp.status()`, the longest server first). One deadline covers the decision (`Deadline`, which no step's `except Exception` can swallow into an allow), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Four reviews (12, 7, 34 and 18 findings, each a test that failed first) added: `apply_patch` read as the hosts parse it (any header spacing or indent, a U+FEFF around the path cut as OpenCode's `trim()` cuts it, and through the shell: Codex's heredoc and argument forms, `applypatch`, after a `cd`, from a pipe; the header reader `_patch.py` is shared with host-adapter and both scope gates, but only the door gate reads the shell form, M9.58); a bare redirect, and a group's or compound's own redirect or heredoc, reach the commands inside; a `cd` that may not run leaves the directory unknown; a path through a symlink then `..` read as the kernel walks it (`git -C`, `cd -P`, a dsh `workdir`); settings replaced, moved, linked or removed by `cp`/`mv`/`ln`/`install`/`rsync` (into a settings directory too, by `-t` or a trailing `DIR/`), `rm`/`mv` of a directory that holds them, a glob that names them, `sed -i` (and `--in-pl`), `perl -i`; `opencode.json` a door only where OpenCode reads it (a nested checkout's root, `~/.config/opencode`, its `config.json`); an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`, `mcp__google-drive__share_file`) crosses the same door as the service's own server, while a service word inside another tool's name (`search_slack_posts`, `get_post_history`) is none. The third review: a `cd` that may not run leaves each later command in either directory (one view per directory, not an unknown one), `cd -`, a bare `pushd`, `popd -n`, `cd X &`, an eval's `cd`, `~+` and `GIT_DIR`/`GIT_WORK_TREE` are read as the shell reads them, and a pipe into a `{ }` group or a compound reaches each command inside; a copy is placed where it runs (DEST/NAME only into an existing directory, so a backup of `.claude` or a file copied into it passes; `-t` with `{}` or xargs' arguments, `--parents`, `-R`, `--remove-source-files`, a link to a settings file, a home directory's contents, GNU option abbreviations); a glob is expanded only where the shell expands it, through links, and never loses a config-like name; another checkout's run home is not this project's; and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Google Workspace's Drive grants cross their doors. The fourth: a copy option that takes no value (`rsync --checksum`, `install --strip`) is no prefix of one that takes one, and only an operand right after an unknown option may be its value, so only then does the destination widen (to at most 8 operands: 600 files and a trailing `-f` stay fast); a glob source names the files it matches now, a contents copy (`SRC/.`, rsync's `SRC/`) its entries, and a name the gate cannot see (`"$f"`, `{}`) is `DEST/?`: no settings change at the project's, a checkout's or the home root, but one into a settings or run-record directory; `ln -n` over a linked settings directory replaces it; `./.claude` after an unknown `cd` and `"$REPO/.claude"` are read by name; after `cd A || cd B` the line is in A, B or where it started; OLDPWD follows a `cd` that may not run and survives a subshell; `cd X || { …; exit 1; }` and `&& ( … ) &&` keep the line in X (the shell reader carries the operator after a subshell or compound, `Mark.end`), and a group after `||` starts where the left side failed; a wrapped `cd` (`nohup`, `env`, `flock`) moves no shell; candidate directories pass into nested reads instead of multiplying them, and a path is placed once per directory; a tool that sends a draft sends (`send-shared-mailbox-draft`) and Drive's `grant_batch` shares. Tests: `mission-doors.test.py` (42 checks), `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`, `hook-matchers.test.py`, `mission-principles.test.py`, `dsh-install.test.py`. Calls: the catalogue keeps its generic `_` alternatives for an OpenCode that cannot list its servers (an extra question beats a silent one-way door). Open: M9.48, M9.49, M9.55–M9.62 (the `!` measurement moved to M9.55) | | M9.32 | Intent questions name their door: `open_questions[].door`; `intent check` blocks an assume/default on a one-way door and treats an `ask` on a door the human authorized as answered. Reason: the plan-time half of the door rule | M9.30, M9.11 | `plugin/bin/intent`, its test | yes | open | | M9.33 | Remove the model-facing `SKIP_HOOKS=1` hints (`gate-supervisor-scope`, `_failclosed.sh`, `SKILL.md`). Reason: they hand the agent the bypass-gate wall's own key | M9.30 | those files | yes | open | | M9.34 | A git pre-push backstop from `aloop init` for pushes to the default branch and force pushes. Reason: holds when Bash matching is evaded (a script, an alias) | M9.30 | `plugin/bin/aloop-init`, its test | yes | open | diff --git a/plugin/bin/_shellread.py b/plugin/bin/_shellread.py index c94d3ea2..da91f86c 100644 --- a/plugin/bin/_shellread.py +++ b/plugin/bin/_shellread.py @@ -64,6 +64,12 @@ def value(self): return parts +class Mark(str): + """The mark that closes a subshell (")") or a compound ("cwd-"), with the list operator after it + (`(a) && b`: "&&"; None at the line's end, or when the compound's own redirect carries it).""" + end = None + + class Cmd: def __init__(self): self.assigns, self.words, self.redirs, self.subs = [], [], [], [] @@ -727,7 +733,7 @@ def flush(): if mode == "pattern": if t == "esac": flush() - out.append("cwd-") + out.append(Mark("cwd-")) closing = True cases -= 1 mode = None @@ -745,7 +751,7 @@ def flush(): continue if start and t in ("fi", "done", "}"): flush() - out.append("cwd-") + out.append(Mark("cwd-")) closing = True continue if start and t in ("elif", "else"): # the branch before it ends; the next starts afresh @@ -770,7 +776,7 @@ def flush(): continue if start and t == "esac" and cases: flush() - out.append("cwd-") + out.append(Mark("cwd-")) closing = True cases -= 1 continue @@ -816,8 +822,10 @@ def flush(): continue cur.end = v flush() - out.append(")") + out.append(Mark(")")) continue + if cur.empty() and out and isinstance(out[-1], Mark) and out[-1].end is None: + out[-1].end = v # `( … ) && b`, `fi; b`: what follows the compound cur.end = v if v in ("|", "|&"): # a pipeline element runs in a subshell cur.pipe = True diff --git a/plugin/mission/doors.json b/plugin/mission/doors.json index 61fdbfeb..f8f0dfd5 100644 --- a/plugin/mission/doors.json +++ b/plugin/mission/doors.json @@ -229,8 +229,8 @@ } ], "tools": [ - "(?i)^(?:mcp__(?:[\\w-]*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[\\w-]*?__(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[_-])?|[\\w-]+?__\\w*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[A-Za-z0-9]*[_-])|(?!mcp__)[\\w-]*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[A-Za-z0-9-]*_(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[_-])?)(?:(?:chat[_-]?)?(?:send|reply|forward|post|schedule[_-]?(?:message|post|email|mail|send))|(?:conversations[_-])?add[_-]message)(?!s(?:$|[_-](?!an?[_-]))|ed(?:$|[_-])|ing)(?![\\w-]+[_-]draft$)[\\w-]*$", - "(?i)^(?:mcp__[\\w-]+?__|(?!mcp__)[\\w-]+?_)(?:send|reply|forward|post)[_-](?:[\\w-]*?[_-])?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)(?![\\w-]*draft)[\\w-]*$" + "(?i)^(?:mcp__(?:[\\w-]*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[\\w-]*?__(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[_-])?|[\\w-]+?__\\w*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[A-Za-z0-9]*[_-])|(?!mcp__)[\\w-]*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[A-Za-z0-9-]*_(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[_-])?)(?:(?:chat[_-]?)?(?:send|reply|forward|post|schedule[_-]?(?:message|post|email|mail|send))|(?:conversations[_-])?add[_-]message)(?!s(?:$|[_-](?!an?[_-]))|ed(?:$|[_-])|ing)(?![\\w-]*(?:message|mail|email)[_-]draft$)[\\w-]*$", + "(?i)^(?:mcp__[\\w-]+?__|(?!mcp__)[\\w-]+?_)(?:send|reply|forward|post)[_-](?:[\\w-]*?[_-])?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)(?![\\w-]*(?:message|mail|email)[_-]draft$)[\\w-]*$" ], "examples": { "bash": [ @@ -253,7 +253,9 @@ "mcp__ms365__send-mail", "mcp__slack__slack_schedule_message", "mcp__slack__conversations_add_message", - "mcp__google-workspace__send_gmail_message" + "mcp__google-workspace__send_gmail_message", + "mcp__ms365__send-shared-mailbox-draft", + "mcp__gmail__send_saved_draft" ] } }, @@ -379,7 +381,7 @@ { "tool": "(?i)^(?:mcp__[\\w-]+?__|(?!mcp__)[\\w-]+?_)manage_\\w*access$", "input": { - "action": "(?i)grant|update|transfer\\w*" + "action": "(?i)(?:grant|update|transfer)\\w*" } }, { @@ -470,11 +472,11 @@ "writes": "(?:^|/)(?:\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.config/opencode/config\\.json|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" }, { - "writes": "^(?:~/)?(?:(?:\\.claude|\\.codex(?:/agents)?|\\.opencode(?:/plugins)?|\\.config(?:/opencode(?:/plugins)?)?|\\.dsh(?:/profiles(?:/[^/]+)?)?)(?:/\\*)?/?|\\*)$" + "writes": "^(?:(?:~/|\\$[^/]*/)?(?:\\.claude|\\.codex(?:/agents)?|\\.opencode(?:/plugins)?|\\.config(?:/opencode(?:/plugins)?)?|\\.dsh(?:/profiles(?:/[^/]+)?)?)(?:/[*?])?/?|(?:~/)?\\*)$" }, { "prog": "rm|unlink|truncate|shred", - "files": "(?:^|/)(?:\\.claude/settings(?:\\.local)?\\.json|\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.config/opencode/config\\.json|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$|^(?:~/)?(?:\\.claude|\\.codex(?:/agents)?|\\.opencode(?:/plugins)?|\\.config(?:/opencode(?:/plugins)?)?|\\.dsh(?:/profiles(?:/[^/]+)?)?)(?:/\\*)?/?$" + "files": "(?:^|/)(?:\\.claude/settings(?:\\.local)?\\.json|\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.config/opencode/config\\.json|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$|^(?:~/|\\$[^/]*/)?(?:\\.claude|\\.codex(?:/agents)?|\\.opencode(?:/plugins)?|\\.config(?:/opencode(?:/plugins)?)?|\\.dsh(?:/profiles(?:/[^/]+)?)?)(?:/[*?])?/?$" }, { "prog": "ln", @@ -543,7 +545,12 @@ "cp -l .claude/settings.json x", "cp -a /tmp/dot/. ~", "rsync --remove-source-files .claude/settings.json /tmp/bak/", - "find /tmp/e -exec cp -t .claude {} +" + "find /tmp/e -exec cp -t .claude {} +", + "rsync -a --checksum /tmp/evil/settings.json .claude/settings.json", + "install --strip /tmp/evil/settings.json .claude/settings.json", + "cp -t .claude $SRC", + "cd \"$REPO\" && rm -rf ./.claude", + "rm -rf \"$(pwd)/.claude\"" ] } }, @@ -605,10 +612,10 @@ "args": "^doors\\s+(?:authorize|revoke)\\b" }, { - "writes": "(?:^|/)docs/agents/mission/ledger\\.jsonl$" + "writes": "(?:^|/)docs/agents/mission/(?:ledger\\.jsonl|\\*)$" }, { - "writes": "(?:^|/)\\.(?:aladdin|agentic)/logs/events\\.jsonl$" + "writes": "(?:^|/)\\.(?:aladdin|agentic)/logs/(?:events\\.jsonl|[*?])$" }, { "prog": "sed", @@ -693,7 +700,10 @@ "python3 -c \"import _mission as M; f = M.record_human; f('.', 'door.authorized', 'door:x', 'me')\"", "python3 -c 'import sys; exec(sys.stdin.read())' <<'EOF'\nimport _mission as M\nM.record_human('.', 'door.authorized', 'door:x', 'me')\nEOF", "perl -0777pi -e 's/a/b/' .aladdin/logs/events.jsonl", - "python3 - <<'EOF'\nimport _events\nev = 'door.authorized'\n_events.emit('.', ev, role='human', subject='door:x')\nEOF" + "python3 - <<'EOF'\nimport _events\nev = 'door.authorized'\n_events.emit('.', ev, role='human', subject='door:x')\nEOF", + "rsync -a --compress /tmp/fake/events.jsonl .aladdin/logs/events.jsonl", + "cp -t .aladdin/logs \"$f\"", + "cp -r \"$SRC\"/. docs/agents/mission/" ], "paths": [ "docs/agents/mission/ledger.jsonl", @@ -823,7 +833,11 @@ "cp notes.md .claude/agents/", "cp -r .claude /tmp/claude-backup", "cp -r .claude .claude.bak", - "rsync -a .claude /tmp/backup/" + "rsync -a .claude /tmp/backup/", + "cp \"$OUT/bundle.js\" ./", + "mv \"$tmp\" .", + "cp \"$f\" ~/", + "rsync -a package.json .claude/settings.json host:/bak/ -z" ], "tools": [ "Read", diff --git a/plugin/mission/doors.py b/plugin/mission/doors.py index b06f5eb3..82a0e440 100644 --- a/plugin/mission/doors.py +++ b/plugin/mission/doors.py @@ -299,55 +299,67 @@ def views(cmd, depth=0, cur=""): timeout, xargs, find -exec, …) around it, the commands inside `sh -c`, `eval`, a script a shell runs, a command substitution, and the stdin that feeds a shell or python (the last heredoc, here-string or `<` on fd 0, else the pipe, into a subshell, a group or a compound too), each with the directory it runs - in (`cd DIR`, `cd -`, `pushd`/`popd`, `git -C DIR`; a subshell's, a pipeline element's or a background - job's `cd` stays there; GIT_DIR or GIT_WORK_TREE makes git's own unknown). A group's, subshell's or - compound's own redirect (`{ …; } > F`, `( … ) < F`) is every command's inside it that has - none of its own, and a bare redirect (`> F`) is a view with no program. A `cd` that may not run (in an - if, case or loop body or a `{ …; }` group, after `&&` or `||`, or one that may fail before `||`) leaves - each later command in either directory: one view per directory. Deeper than DEPTH is a ValueError: a - gate that cannot read the whole command fails closed.""" + in (`cd DIR`, `cd -`, `pushd`/`popd`, `git -C DIR`; a subshell's, a pipeline element's, a background + job's or a wrapped command's `cd` stays there; GIT_DIR or GIT_WORK_TREE makes git's own unknown). A + group's, subshell's or compound's own redirect (`{ …; } > F`, `( … ) < F`) is every command's + inside it that has none of its own, and a bare redirect (`> F`) is a view with no program. A `cd` that + may not run (in an if, case or loop body or a `{ …; }` group, after `&&` or `||`, or one that may fail + before `||`) leaves each later command in either directory: one view per directory (cur may be a + tuple of them). Deeper than DEPTH is a ValueError: a gate that cannot read the whole command fails + closed.""" if depth > DEPTH: raise ValueError("the command nests shells, evals or substitutions deeper than the door gate reads (%d)" % DEPTH) cur = (cur,) if isinstance(cur, str) else tuple(cur) out, stack, dirs, fed, group = [], [], [], None, None - opened, closed, scopes, prev = [], None, [], None + opened, closed, scopes, prev, last = [], None, [], None, None maybe, failed, old, gitenv = None, None, (UNKNOWN,), False for c in R.read(cmd): if isinstance(c, str): + link = getattr(c, "end", None) # the list operator after a subshell or compound + inner = cur # where it starts: after `cd X ||`, where the cd failed + if prev == "||" and failed is not None: + inner = failed + elif prev == "||" and maybe is not None: + inner = union(maybe, cur) if c == "(": # a pipe into a subshell feeds the commands inside it - stack.append((cur, group, maybe, failed)) - group, maybe, failed = (fed if fed is not None else group), None, None + stack.append((cur, group, maybe, failed, old, list(dirs))) + cur, group, maybe, failed = inner, (fed if fed is not None else group), None, None opened.append(len(out)) elif c == ")": if stack: - cur, group, maybe, failed = stack.pop() + cur, group, maybe, failed, old, dirs[:] = stack.pop() closed = opened.pop() if opened else None elif c == "cwd+": # an if, case, loop body or { …; } group: its `cd` may or may not run - scopes.append((cur, [], group, maybe, failed)) - group, maybe, failed = (fed if fed is not None else group), None, None + scopes.append((inner, [], group, maybe, failed, old, cur if prev == "||" else None)) + cur, group, maybe, failed = inner, (fed if fed is not None else group), None, None opened.append(len(out)) elif c == "cwd|" and scopes: # a branch ends: the next one starts where the compound did - scopes[-1][1].append(cur) - cur = scopes[-1][0] + scopes[-1][1].append((cur, old)) + cur, old = scopes[-1][0], scopes[-1][5] elif c == "cwd-": if scopes: - entry, tails, group, maybe, failed = scopes.pop() - cur = union(entry, *tails, cur) + entry, tails, group, maybe, failed, old0, skip = scopes.pop() + cur = union(entry, *(t for t, _ in tails), cur) + old = union(old0, *(o for _, o in tails), old) + if skip is not None and last in ("exit", "return"): + cur, maybe, failed = skip, None, None # `cd X || { …; exit 1; }`: what follows runs in X + elif skip is not None: + cur = union(skip, cur) # `A || { … }`: A succeeded, or the group ran closed = opened.pop() if opened else None - if c in (")", "cwd-") and maybe is not None: # the list around it may end here (its end is not read) - cur, maybe, failed = union(maybe, cur), None, None - fed, prev = None, None + if c in (")", "cwd-") and maybe is not None and link not in ("&&", "||"): + cur, maybe, failed = union(maybe, cur), None, None # its list ends here + fed, last = None, None + prev = link if c in (")", "cwd-") and link in ("&&", "||") else None continue own, closed = (closed if not c.words and not c.assigns else None), None - here = cur + start = here = cur if prev == "||" and failed is not None: here = failed # `cd X || Y`: Y runs where the cd failed elif prev == "||" and maybe is not None: here = union(maybe, cur) # `… && cd X && a || Y`: Y runs after any of them failed for sub in c.subs: - for d in here: - out += views(sub, depth + 1, d) - vs = [v for d in here for v in simple(c, depth, d)] + out += views(sub, depth + 1, here) + vs = simple(c, depth, here) for v in vs: # what ended it (`&&`: the next runs only if it succeeded) v.setdefault("end", c.end) v.setdefault("depth", depth) @@ -358,17 +370,19 @@ def views(cmd, depth=0, cur=""): (w.text() or "").split("=", 1)[0] in GITENV for w in c.words[1:]) or not c.words and any( n in GITENV for n, _ in c.assigns): gitenv = True - mover = next((v for v in vs if v["prog"] in ("cd", "pushd", "popd") and v["depth"] == depth), None) + lead = next((v for v in vs if v["prog"] not in ("builtin", "command", "time")), None) # `nohup cd` moves none + mover = lead if lead and lead["prog"] in ("cd", "pushd", "popd") and lead["depth"] == depth else None prog = vs[0]["prog"] if vs else "" if c.pipe or c.end == "&": # a pipeline element or a background job runs in a subshell pass elif mover is not None: - before = here + before, old0 = here, old cur, old = moved(mover["prog"], mover["args"], here, old, dirs) - if prev in ("&&", "||") and maybe is None: - maybe = before # it may not run: known only along its own list - if prev == "||": # after `a ||` it ran only if a failed - cur = union(before, cur) + if prev in ("&&", "||"): # it may not run: known only along its own list + old = union(old0, old) + maybe = before if maybe is None else maybe + if prev == "||": # `A || cd X`: X, or where A left it + cur = union(start, cur) if c.end == "||": # `cd X || Y`: Y runs where the cd failed; after it, either failed, maybe = before, (maybe if maybe is not None else before) elif prev == "||" and failed is not None and prog in ("exit", "return"): @@ -401,7 +415,7 @@ def views(cmd, depth=0, cur=""): if maybe is not None: cur = union(maybe, cur) maybe, failed = None, None - prev = c.end + prev, last = c.end, prog return out @@ -487,14 +501,16 @@ def reads_stdin(v): def feed(vs, text, depth): """What a command's stdin runs: a shell reading its stdin runs the text, and a python gets it as code (python3 - <<'EOF' … EOF, or -c that execs its stdin).""" - out = [] + out, shells = [], {} for v in vs: if "code" in v: # python: its stdin is code it may run (exec(sys.stdin.read())) v["code"] = (v["code"] + "\n" + text).lstrip("\n") elif v["prog"] in _patch.PATCHERS: # apply_patch <<'EOF' … EOF: each path the patch names is a write v["writes"] = v["writes"] + patched(text) - elif reads_stdin(v): - out += [dict(x, env=x["env"] + v["env"]) for x in views(text, depth + 1, v["dir"])] + elif reads_stdin(v): # one read per shell, in every directory it may run in + shells.setdefault((v["prog"], tuple(v["args"]), tuple(v["env"])), []).append(v["dir"]) + for (_, _, env), where in shells.items(): + out += [dict(x, env=x["env"] + list(env)) for x in views(text, depth + 1, union(where))] return out @@ -516,7 +532,7 @@ def feed(vs, text, depth): COPY_PAIRED = { # the options of a copy, move, link or install that take a separate value - "cp": {"-S", "--suffix"}, "mv": {"-S", "--suffix"}, "ln": {"-S", "--suffix"}, + "cp": {"-S", "--suffix", "--sparse", "--no-preserve"}, "mv": {"-S", "--suffix"}, "ln": {"-S", "--suffix"}, "install": {"-m", "-o", "-g", "-S", "--mode", "--owner", "--group", "--suffix", "--strip-program"}, "rsync": {"-e", "-f", "-B", "-M", "-T", "-@", "--rsh", "--rsync-path", "--filter", "--exclude", "--include", "--exclude-from", "--include-from", "--files-from", "--temp-dir", "--compare-dest", "--copy-dest", @@ -525,17 +541,47 @@ def feed(vs, text, depth): "--min-size", "--timeout", "--contimeout", "--port", "--sockopts", "--bwlimit", "--iconv", "--checksum-choice", "--compress-choice", "--skip-compress", "--write-batch", "--read-batch", "--only-write-batch", "--max-delete", "--block-size", "--modify-window", "--address", "--outbuf", - "--info", "--debug", "--stop-after", "--stop-at", "--copy-as", "--remote-option"}} + "--info", "--debug", "--stop-after", "--stop-at", "--copy-as", "--remote-option", "--log-format", + "--compress-level", "--checksum-seed", "--protocol", "--max-alloc", "--cc", "--zc", "--zl", + "--early-input"}} +COPY_FLAGS = { # their long options that take no separate value (a bare --backup, not --backup=CONTROL) + "cp": {"--archive", "--attributes-only", "--backup", "--copy-contents", "--debug", "--force", "--interactive", + "--link", "--dereference", "--no-clobber", "--no-dereference", "--preserve", "--parents", "--recursive", + "--reflink", "--remove-destination", "--strip-trailing-slashes", "--symbolic-link", "--update", + "--verbose", "--one-file-system", "--context", "--keep-directory-symlink", "--help", "--version"}, + "mv": {"--backup", "--debug", "--exchange", "--force", "--interactive", "--no-clobber", "--no-copy", + "--strip-trailing-slashes", "--update", "--verbose", "--context", "--help", "--version"}, + "ln": {"--backup", "--directory", "--force", "--interactive", "--logical", "--no-dereference", "--physical", + "--relative", "--symbolic", "--verbose", "--help", "--version"}, + "install": {"--backup", "--compare", "--debug", "--preserve-timestamps", "--strip", "--verbose", + "--preserve-context", "--context", "--help", "--version"}, + "rsync": {"--verbose", "--quiet", "--no-motd", "--checksum", "--archive", "--recursive", "--relative", + "--no-implied-dirs", "--backup", "--update", "--inplace", "--append", "--append-verify", "--dirs", + "--mkpath", "--links", "--copy-links", "--copy-unsafe-links", "--safe-links", "--munge-links", + "--copy-dirlinks", "--keep-dirlinks", "--hard-links", "--perms", "--executability", "--acls", + "--xattrs", "--owner", "--group", "--devices", "--copy-devices", "--write-devices", "--specials", + "--times", "--atimes", "--open-noatime", "--crtimes", "--omit-dir-times", "--omit-link-times", + "--super", "--fake-super", "--sparse", "--preallocate", "--dry-run", "--whole-file", + "--one-file-system", "--existing", "--ignore-existing", "--remove-source-files", "--delete", + "--delete-before", "--delete-during", "--delete-delay", "--delete-after", "--delete-excluded", + "--ignore-missing-args", "--delete-missing-args", "--ignore-errors", "--force", "--partial", + "--delay-updates", "--prune-empty-dirs", "--numeric-ids", "--ignore-times", "--size-only", + "--fuzzy", "--compress", "--cvs-exclude", "--from0", "--protect-args", "--secluded-args", + "--trust-sender", "--blocking-io", "--stats", "--8-bit-output", "--human-readable", "--progress", + "--itemize-changes", "--list-only", "--ipv4", "--ipv6", "--fsync", "--old-args", "--old-dirs", + "--help", "--version"}} +MAXDESTS = 8 # the operands a copy's unknown trailing options leave as its possible destination def copied(prog, args): """What a copy, move, link or install writes, as plans place() resolves where the command runs: {prog, dest, srcs, target (-t DIR), whole (-T), parents (cp --parents, rsync -R: DEST/SRC), removes - (mv, rsync --remove-source-files: its sources go)}. A long option matches by its unique GNU prefix. - After an option that follows an operand, any later operand may be the destination (the option may - take a value the gate does not know). Nothing for `install -d` or one operand (but `ln -s TARGET`).""" - paired, ops, target, i = COPY_PAIRED[prog], [], None, 0 - whole, parents, removes, late = False, False, prog == "mv", False + (mv, rsync --remove-source-files: its sources go), nodir (ln -n: a link DEST is replaced, not + entered)}. A long option matches exactly, else by its unique GNU prefix. An operand right after a long + option the gate does not know may be that option's value, so the operand before it may be the + destination (at most MAXDESTS of them). Nothing for `install -d` or one operand (but `ln -s TARGET`).""" + paired, flags, ops, target, i = COPY_PAIRED[prog], COPY_FLAGS[prog], [], None, 0 + whole, parents, removes, nodir, maybe = False, False, prog == "mv", False, set() def is_(k, name, least): return len(k) >= least and name.startswith(k) @@ -546,10 +592,11 @@ def is_(k, name, least): break if a.startswith("--"): k, eq, v = a.partition("=") - late = late or bool(ops) + longs = [p for p in paired if p.startswith("--") and is_(k, p, 3)] + known = k in flags or [f for f in flags if is_(k, f, 3)] if prog != "rsync" and is_(k, "--target-directory", 3): target, i = (v, i) if eq else (args[i + 1] if i + 1 < len(args) else None, i + 1) - elif prog != "rsync" and is_(k, "--no-target-directory", 5): + elif prog != "rsync" and is_(k, "--no-target-directory", 6): whole = True elif prog == "install" and is_(k, "--directory", 4): return [] @@ -557,10 +604,15 @@ def is_(k, name, least): parents = True elif prog == "rsync" and is_(k, "--remove-source-files", 7): removes = True - elif not eq and any(is_(k, p, 4) for p in paired if p.startswith("--")): - i += 1 + elif prog == "ln" and is_(k, "--no-dereference", 6): + nodir = True + elif k in flags or eq: + pass + elif k in paired or longs and not known: + i += 1 # its value (a unique prefix of an option that takes one) + elif not known and i + 1 < len(args): + maybe.add(i + 1) # unknown: the next word may be its value elif a.startswith("-") and a != "-": - late = late or bool(ops) for j in range(1, len(a)): f = "-" + a[j] if f == "-t" and prog != "rsync": @@ -572,27 +624,37 @@ def is_(k, name, least): return [] elif f == "-R" and prog == "rsync": parents = True + elif f == "-n" and prog == "ln": + nodir = True elif f in paired: i += 0 if a[j + 1:] else 1 break else: - ops.append(a) + ops.append((a, i in maybe)) i += 1 - plan = {"prog": prog, "target": False, "whole": whole, "parents": parents, "removes": removes} + plan = {"prog": prog, "target": False, "whole": whole, "parents": parents, "removes": removes, "nodir": nodir} + words = [o for o, _ in ops] if target is not None: - return [dict(plan, dest=target, srcs=ops, target=True)] + return [dict(plan, dest=target, srcs=words, target=True)] if prog == "ln" and len(ops) == 1: - return [dict(plan, dest=".", srcs=ops, target=True)] # ln -s TARGET: a link named after it, here - return [dict(plan, dest=ops[k], srcs=ops[:k]) for k in (range(1, len(ops)) if late else [len(ops) - 1]) - if k >= 1] + return [dict(plan, dest=".", srcs=words, target=True)] # ln -s TARGET: a link named after it, here + k, out = len(ops) - 1, [] + while k >= 1 and len(out) < MAXDESTS: # the last operand, and before it while it may be an option's value + out.append(dict(plan, dest=words[k], srcs=words[:k])) + if not ops[k][1]: + break + k -= 1 + return out def copy_writes(cwd, v, plan): """The paths a copy plan writes where its command runs: DEST/NAME for each source when DEST is a directory (or -t DIR, or more than one source or a trailing / or a DEST the gate cannot place), else - DEST itself (the copy becomes DEST); DEST/* for a source whose contents are copied (SRC/., rsync's - SRC/) or that the gate cannot see ({}, xargs' arguments, a variable); DEST/SRC under --parents or -R; - and the sources a move takes away. A remote rsync destination is not this machine's.""" + DEST itself (the copy becomes DEST); a glob source's NAMEs are the files it names now; a source whose + contents are copied (SRC/., rsync's SRC/) gives each of its entries (DEST/* when the gate cannot list + them); a source whose name the gate cannot see ({}, xargs' arguments, a variable) is DEST/?; DEST/SRC + under --parents or -R; `ln -n` over a link replaces DEST; and the sources a move takes away. A remote + rsync destination is not this machine's.""" dest, srcs, rsync = plan["dest"], plan["srcs"], plan["prog"] == "rsync" out = list(srcs) if plan["removes"] else [] if dest is None or rsync and re.match(r"^[^/]*:", dest): @@ -601,42 +663,61 @@ def copy_writes(cwd, v, plan): return out + [dest] where = where_of(v, dest) full = resolve(cwd, where) if where != UNKNOWN else "" + if plan.get("nodir") and full and not dest.endswith("/") and os.path.islink(full): + return out + [dest] # ln -sfn X LINK: the link itself is replaced isdir = os.path.isdir(full) if full else None # None: the gate cannot tell base = dest.rstrip("/") or "/" + def contents(s): # the entries of SRC/. (rsync SRC/), as copied + w = where_of(v, s) + f = resolve(cwd, w) if w != UNKNOWN else "" + try: + names = os.listdir(f) if f else None + except OSError: + names = None + if names is None or len(names) > GLOB_SCAN: + return [base + "/*"] + keep = sorted(n for k, n in enumerate(sorted(names)) if k < GLOB_MAX or KEEP.search(n)) + return [base + "/" + n for n in keep[:4 * GLOB_MAX]] + ([base + "/*"] if len(keep) > 4 * GLOB_MAX else []) + def inside(s): if plan["parents"]: s = s.split("/./", 1)[1] if rsync and "/./" in s else s - return base + "/" + s.lstrip("/") - if s in (STDIN, "{}") or re.search(r"[$`]", s) or s.endswith("/.") or rsync and s.endswith("/"): - return base + "/*" # its contents, whatever they are named + return [base + "/" + s.lstrip("/")] + if s.endswith("/.") or rsync and s.endswith("/"): + return contents(s) + if s in v.get("globs", ()) and re.search(r"[*?]", s): # the shell expands it: each file it names now + return [base + "/" + os.path.basename(m.rstrip("/")) for m in matches(cwd, v, s)] or [base + "/?"] name = os.path.basename(s.rstrip("/")) - return base + "/" + name if name not in ("", ".", "..") else base + "/*" + if s in (STDIN, "{}") or re.search(r"[$`]", name): + return [base + "/?"] # a name the gate cannot see + return [base + "/" + name] if name not in ("", ".", "..") else contents(s + "/.") def a_dir(s): # rsync copies a directory into DEST, as DEST/NAME w = where_of(v, s) f = resolve(cwd, w) if w != UNKNOWN else "" return not f or os.path.isdir(f) if plan["target"] or isdir: - return out + [inside(s) for s in srcs or [STDIN]] + return out + [w for s in srcs or [STDIN] for w in inside(s)] if isdir is False and len(srcs) == 1 and not dest.endswith("/"): s = srcs[0] if rsync and not s.endswith("/") and a_dir(s): - return out + [inside(s)] - return out + [dest] + ([base + "/*"] if s.endswith("/.") or rsync and s.endswith("/") else []) - return out + [dest] + [inside(s) for s in srcs or [STDIN]] + return out + inside(s) + return out + [dest] + (contents(s) if s.endswith("/.") or rsync and s.endswith("/") else []) + return out + [dest] + [w for s in srcs or [STDIN] for w in inside(s)] def simple(c, depth, cur=""): - """The views of one simple command (a _shellread.Cmd).""" + """The views of one simple command (a _shellread.Cmd): one per directory it may run in.""" + cur = (cur,) if isinstance(cur, str) else tuple(cur) env = ["%s=%s" % (n, render(v)) for n, v in c.assigns] writes = [render(w.parts) for op, _, w in c.redirs if op in WRITES or (op == ">&" and not re.fullmatch(r"\d+|-", w.text() or "0"))] globs = {render(w.parts) for w in list(c.words) + [w for _, _, w in c.redirs] if any(isinstance(p, tuple) and p[0] == "unknown" for p in w.parts)} # the shell expands these if not c.words: # `> F`, `X=1 > F`: no program, still a write - return [{"prog": "", "args": [], "env": env, "writes": writes, "dir": cur, "config": [], "globs": globs}] \ - if writes else [] + return [{"prog": "", "args": [], "env": list(env), "writes": list(writes), "dir": d, "config": [], + "globs": globs} for d in cur] if writes else [] vs = chain([render(w.parts) for w in c.words], env, writes, depth, cur) for v in vs: v.setdefault("globs", globs) @@ -644,7 +725,9 @@ def simple(c, depth, cur=""): def chain(words, env, writes, depth, cur): - """The views of a command's words: the program, then each program a wrapper runs.""" + """The views of a command's words: the program, then each program a wrapper runs, each once per + directory it may run in (cur, a tuple of them).""" + cur = (cur,) if isinstance(cur, str) else tuple(cur) out, config, stdin_args = [], [], False while words and ASSIGN.match(words[0]): env.append(words.pop(0)) @@ -675,22 +758,25 @@ def chain(words, env, writes, depth, cur): while rest and rest[0].startswith("-"): o, v = rest[0], rest[1] if len(rest) > 1 else None if o == "-C": - cur = chdir(cur, v) + cur = union(*((chdir(d, v),) for d in cur)) elif o in ("-c", "--config-env") and v: config.append(v) elif o.startswith(("--git-dir", "--work-tree")): - cur = UNKNOWN + cur = (UNKNOWN,) elif o.startswith("--config-env="): config.append(o.split("=", 1)[1]) rest = rest[2:] if o in GIT_PAIRED else rest[1:] if prog == "command" and rest[:1] in (["-v"], ["-V"]): rest = [] # `command -v X` looks X up; it runs nothing - out.append({"prog": prog, "args": rest + ([STDIN] if stdin_args is True else []), "env": list(env), - "writes": list(writes), "dir": cur, "config": list(config)}) - if prog in COPY_PAIRED: # its destination (and a move's sources), placed where it runs - out[-1]["copies"] = copied(prog, out[-1]["args"]) - if code is not None: - out[-1]["code"] = code + args = rest + ([STDIN] if stdin_args is True else []) + plans = copied(prog, args) if prog in COPY_PAIRED else None # placed where it runs + for d in cur: + out.append({"prog": prog, "args": args, "env": list(env), "writes": list(writes), "dir": d, + "config": list(config)}) + if plans is not None: + out[-1]["copies"] = plans + if code is not None: + out[-1]["code"] = code if prog in SHELLS: # sh -c [options] STRING: the first operand after the options is the command j, has_c, script = 0, False, None while j < len(rest): @@ -927,12 +1013,13 @@ def dry(args): GUARDED = ("docs/agents/mission/ledger.jsonl", ".aladdin/logs/events.jsonl", ".agentic/logs/events.jsonl") -def placed(cwd, top, v, path, kernel=False): +def placed(cwd, top, v, path, kernel=False, here=None): """A path a command names as its patterns read it: resolved where the command runs, relative to the project `top` or this checkout when inside one (`cd docs/agents && … mission/ledger.jsonl` is the ledger; /tmp/x/.aladdin is not the run's home). Where the gate cannot resolve it, a name a guarded record ends with is that record. `kernel`: walked as the kernel walks it, a symlink resolved before - the `..` after it (link/../x is beside the link's target), instead of as the words say.""" + the `..` after it (link/../x is beside the link's target), instead of as the words say. `here`: this + checkout's top, when the caller knows it.""" if path.startswith("-") or path in (STDIN, "") or "\n" in path or len(path) > 4096: return path if re.match("(?:%s|\\$PWD|\\$CLAUDE_PROJECT_DIR)(?:/|$)" % re.escape(TOP), path): @@ -942,12 +1029,12 @@ def placed(cwd, top, v, path, kernel=False): else: where = path if os.path.isabs(path) or path.startswith("~") or not v["dir"] else v["dir"] + "/" + path if where == UNKNOWN: - x = os.path.normpath(path) - return next((g for g in GUARDED if not os.path.isabs(x) and (g == x or g.endswith("/" + x))), path) + x = os.path.normpath(path) # as written (./.claude is .claude) + return next((g for g in GUARDED if not os.path.isabs(x) and (g == x or g.endswith("/" + x))), x) full = resolve(cwd, where, kernel) if not full: return path - for root in (top, toplevel(os.path.abspath(cwd))): # the project, else this checkout (a worktree) + for root in (top, toplevel(os.path.abspath(cwd)) if here is None else here): # the project, else this checkout r = os.path.relpath(os.path.realpath(full), os.path.realpath(root)) if root else ".." if not r.startswith(".."): return r @@ -1023,11 +1110,17 @@ def place(cwd, vs): """Each view's operands and writes as placed() reads them (files, placed_writes): as written and as the kernel walks them, each file a glob names, relative to the project's checkout that holds it and to the home directory; a copy's destination as copy_writes() places it.""" - top = os.path.realpath(_paths.project(cwd)) + top, seen, here = os.path.realpath(_paths.project(cwd)), {}, toplevel(os.path.abspath(cwd)) + + def forms(v, a): # once per (directory, path): candidate dirs repeat them + key = (v["dir"], a, a in v.get("globs", ())) + if key not in seen: + seen[key] = _forms(v, a) + return seen[key] - def forms(v, a): - out = [placed(cwd, top, v, a, k) for k in (False, True)] - out += [placed(cwd, top, v, m, k) for m in matches(cwd, v, a) for k in (False, True)] + def _forms(v, a): + out = [placed(cwd, top, v, a, k, here) for k in (False, True)] + out += [placed(cwd, top, v, m, k, here) for m in matches(cwd, v, a) for k in (False, True)] where = where_of(v, a) if not a.startswith("-") and a != STDIN else UNKNOWN if where != UNKNOWN and resolve(cwd, where): for k in (False, True): @@ -1035,7 +1128,7 @@ def forms(v, a): out += [x for x in (checkout_rel(full, top), home_rel(full)) if x and not x.startswith("..")] return out for v in vs: - v["writes"] = v["writes"] + [w for p in v.get("copies", ()) for w in copy_writes(cwd, v, p)] + v["writes"] = list(dict.fromkeys(v["writes"] + [w for p in v.get("copies", ()) for w in copy_writes(cwd, v, p)])) v["files"] = list(dict.fromkeys(x for a in v["args"] if not a.startswith("-") for x in forms(v, a))) v["placed_writes"] = list(dict.fromkeys(x for w in v["writes"] for x in forms(v, w))) return vs diff --git a/scripts/test/mission-doors.test.py b/scripts/test/mission-doors.test.py index 786ce3f9..9a863535 100644 --- a/scripts/test/mission-doors.test.py +++ b/scripts/test/mission-doors.test.py @@ -1082,6 +1082,98 @@ def swallowing(*a, **k): shutil.rmtree(os.path.dirname(far), ignore_errors=True) os.unlink(os.path.join(p.dir, "h")) +# 21d) the fourth M9.31 review. A copy option that takes no value (rsync --checksum, install --strip) is no +# prefix of one that does; a glob source names the files it matches now; a contents or glob copy into a +# run-record directory forges it; ln -n replaces a linked settings directory; a name the gate cannot see, +# copied into the project, a checkout or ~, is no settings change; trailing flags neither widen nor slow a copy +evil, fake = tempfile.mkdtemp(), tempfile.mkdtemp() +for d, n in ((evil, "settings.json"), (evil, "config.toml"), (fake, "events.jsonl"), (fake, "ledger.jsonl")): + open(os.path.join(d, n), "w").write("{}") +for cmd in ("rsync -a --checksum E/settings.json .claude/settings.json", "rsync -a --partial E/ .codex/", + "rsync -a --backup E/settings.json .claude/", "rsync -a --group E/settings.json .claude/settings.json", + "install --strip E/settings.json .claude/settings.json", "cp E/*.json .claude/", "mv E/*.json .claude/", + "cp -t .claude E/*.json", "install -m 644 E/*.json .claude/", "cp E/*.toml .codex/", "cp E/s* .claude/", + "cp \"$f\" .claude/", "cd \"$REPO\" && rm -rf ./.codex", "cd \"$REPO\" && cp -r /tmp/x/. ./.claude/", + "cd - && rm -rf ./.claude", "rm -rf \"$(pwd)/.claude\"", "rm -rf \"$REPO/.claude\"", "cd \"$REPO\" && mv /tmp/x ./.claude"): + assert "change-agent-settings" in doors_of(cmd.replace("E/", evil + "/")), (cmd, doors_of(cmd.replace("E/", evil + "/"))) +for cmd in ("rsync -a --compress F/events.jsonl .aladdin/logs/events.jsonl", "cp F/* .aladdin/logs/", "rsync -a F/ .aladdin/logs/", + "cp -r F/. docs/agents/mission/", "cp -a F/. .aladdin/logs", "mv F/* docs/agents/mission/", "cp -t .aladdin/logs F/*", + "cp F/*.jsonl docs/agents/mission/", "cp -r \"$SRC\"/. docs/agents/mission/", "cp \"$f\" .aladdin/logs/"): + assert "forge-human-record" in doors_of(cmd.replace("F/", fake + "/")), (cmd, doors_of(cmd.replace("F/", fake + "/"))) +lnr = tempfile.mkdtemp() +subprocess.run(["git", "init", "-q", lnr], check=True) +os.makedirs(os.path.join(lnr, "shared", "claude")) +os.symlink("shared/claude", os.path.join(lnr, ".claude")) +for cmd in ("ln -sfn ../evil/claude .claude", "ln -snf ../evil/claude .claude", "ln --no-dereference -sf ../evil/claude .claude"): + assert "change-agent-settings" in doors_of(cmd, cwd=lnr), (cmd, doors_of(cmd, cwd=lnr)) +assert doors_of("ln -sf ../notes/README.md .claude", cwd=lnr) == [] # a link inside it, by its own name +for cmd in ("cp \"$f\" .", "cp \"$OUT/bundle.js\" ./", "mv \"$tmp\" .", "find /tmp/gen -name '*.py' -exec cp {} . \\;", + "ls dist/*.whl | xargs cp -t .", "ls /tmp/gen/*.py | xargs -I{} cp {} .", "ln -s \"$(pwd)/scripts/hook.sh\" .", + "cp ${BUILD}/app .", "install -m 755 \"$BIN\" .", "cp -r \"$SRC\" .", "cp \"$f\" ~/", + "rsync -a .claude/settings.json .codex/config.toml host:/bak/ --progress", + "rsync -a .claude/settings.json .codex/config.toml /tmp/bak/ --progress", + "rsync -a package.json .claude/settings.json host:/bak/ -z", "cp README.md .claude/settings.json /tmp/bak/ -v"): + assert doors_of(cmd) == [], (cmd, doors_of(cmd)) + assert doors_of(cmd, cwd=gwt) == [], (cmd, doors_of(cmd, cwd=gwt)) # a worktree's root too +files = " ".join("src/file%04d.py" % i for i in range(600)) +for cmd in ("mv %s build/ -f" % files, "cp %s build/ -v" % files, "cp %s build/ --verbose" % files): + t0 = time.time() + assert doors_of(cmd) == [] and time.time() - t0 < 3, (cmd[:20], time.time() - t0) +assert "change-agent-settings" in doors_of("cp /tmp/s .claude/settings.json --unknown-flag x") # a value it may take +ok("copies: no-value options, glob sources, record directories, ln -n over a link, unseen names, trailing flags") + +# a `cd` after `||` keeps where the left side left it; OLDPWD follows a cd that may not run; `cd X || { …; exit; }` +# and `&& (…) &&` keep the list in X; a subshell keeps its OLDPWD; a wrapped cd (nohup, env, flock) moves no shell +gwt2 = os.path.join(g.dir, "home", "feat2-wt") +subprocess.run(["git", "worktree", "add", "-q", gwt2, "-b", "feat2"], cwd=g.dir, check=True) +R, W, W2 = g.dir, gwt, gwt2 +for cmd, at, want in ( + ("cd R || cd W2; git push origin HEAD", W, "push-default-branch"), + ("cd R 2>/dev/null || cd /tmp; git push origin HEAD", W, "push-default-branch"), + ("cd R || cd W2 || exit 1; git push origin HEAD", W, "push-default-branch"), + ("cd R || cd W2; git reset --hard", W, "discard-local-work"), + ("cd W; [ -d nosuch ] && cd nosuch; cd -; git push origin HEAD", R, "push-default-branch"), + ("cd W; if [ -d nosuch ]; then cd nosuch; fi; cd -; git push origin HEAD", R, "push-default-branch"), + ("cd W\n[ -d nosuch ] && cd nosuch\ncd -\ngit push origin HEAD", R, "push-default-branch"), + ("cd W || { git push origin HEAD; }", R, "push-default-branch"), # the group runs where the cd failed + ("cd W || { echo no; }; git push origin HEAD", R, "push-default-branch"), + ("nohup cd /tmp; git push origin HEAD", R, "push-default-branch"), ("env cd /tmp; git push origin HEAD", R, "push-default-branch"), + ("timeout 5 cd /tmp; git push origin HEAD", R, "push-default-branch"), + ("flock /tmp/l cd /tmp; git push origin HEAD", R, "push-default-branch"), + ("setsid cd /; git push origin HEAD", R, "push-default-branch")): + cmd = cmd.replace("W2", W2).replace("R ", R + " ").replace("W;", W + ";").replace("W\n", W + "\n").replace("W ", W + " ") + assert want in denied(hook(g, "Bash", {"command": cmd}, cwd=at)), cmd +for cmd, at in (("cd W 2>/dev/null || cd W2 && git push -u origin HEAD", R), + ("cd W || { echo no worktree; exit 1; }; git push -u origin HEAD", R), + ("cd W || {\n echo no worktree >&2\n exit 1\n}\ngit push -u origin HEAD", R), + ("bash <<'SH'\ncd W || { echo no; exit 1; }\ngit push -u origin HEAD\nSH", R), + ("cd W || { return 1; }; git push origin HEAD", R), ("cd W || { exit 1; }\ngit reset --hard origin/feat", R), + ("git fetch && cd W && (make test) && git push -u origin HEAD", R), + ("git fetch && cd W && (cd sub && make) && git push -u origin HEAD", R), + ("git fetch && cd W && (make test) && git reset --hard origin/feat", R), + ("cd R; (cd sub && make); cd -; git push origin HEAD", W), ("cd R; (cd sub; make); cd - >/dev/null; git push origin HEAD", W), + ("builtin cd W; git push origin HEAD", R), ("time cd W; git push origin HEAD", R)): + cmd = cmd.replace("W2", W2).replace("R;", R + ";").replace("W ", W + " ").replace("W;", W + ";") + assert hook(g, "Bash", {"command": cmd}, cwd=at).stdout == "", cmd +# candidate directories are carried into nested reads, not multiplied by them +nest = "make test" +for i in range(5): + nest = "[ -d a ] && cd a; [ -d b ] && cd b; [ -d c ] && cd c; echo \"$(%s)\"" % nest +t0 = time.time() +assert doors_of(nest) == [] and time.time() - t0 < 3, time.time() - t0 +ok("directories: cd after ||, OLDPWD of a cd that may not run, || { exit; } guards, && (…) &&, wrapped cds") + +for tool, inp, want in (("mcp__ms365__send-shared-mailbox-draft", {}, "send-external-message"), + ("mcp__gmail__send_saved_draft", {}, "send-external-message"), + ("mcp__outlook__send_existing_draft", {}, "send-external-message"), + ("mcp__workspace-mcp__manage_drive_access", {"action": "grant_batch"}, "visibility-or-sharing")): + assert ids(tool, inp) == [want], (tool, ids(tool, inp)) +for tool in ("mcp__slack__slack_send_message_draft", "mcp__gmail__create_draft", "mcp__outlook__create_mail_draft"): + assert ids(tool, {}) == [], (tool, ids(tool, {})) +ok("MCP: a tool that sends a draft sends; one that writes a draft does not; Drive grant_batch shares") +for x in (evil, fake, lnr): + shutil.rmtree(x, ignore_errors=True) + # MCP names: Composio's VERBS_A_ form, kebab-case tools, scheduled messages, a verb before the service, GitLab and # Bitbucket merges, Google Workspace's Drive access tools; drafts and reads pass for tool, inp, want in ( From 2ffdd59161a2b10631acae7327fb92d057f06bd3 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 18:21:21 +0000 Subject: [PATCH 11/14] M9.31: fifth review fixes (conditional exits, background lists, find -execdir, env -C, staged copies, dotglob, cp -T, ${X:?}, drafts) Seventeen confirmed findings from the fifth adversarial review of the fourth review's fixes, each with a test that was red before the change (section 21e of mission-doors.test.py, 45 checks): - directories: an exit inside `|| { ... }` that may not run keeps the group's cd; a failed cd's directory no longer survives `&&` or a compound end; a second conditional cd keeps where the list stopped; a background list (`cd X && make &`) moves no shell; find -execdir and env -C/--chdir move their command - copies: `--` ends the options (crashed before); glob and contents copies see files the same line staged; `*` names dotfiles; cp -T and rsync -R SRC/./ copy SRC's entries; `$VAR` directories are settings roots only for $HOME; an unseen name into the ledger's directory is a forgery; a large directory copied whole is placed once per directory - catalogue: absolute paths after an unknown cd, `$(pwd)` and `${X:?}` paths, and an even draft guard (forward_as_draft and send_mail_draft write drafts; send_saved_draft sends one) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- CHANGELOG.md | 2 +- docs/ROADMAP.md | 2 +- plugin/bin/_shellread.py | 7 +- plugin/mission/doors.json | 8 +- plugin/mission/doors.py | 142 ++++++++++++++++++++--------- scripts/test/mission-doors.test.py | 83 +++++++++++++++++ 6 files changed, 192 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 688571b5..04d4d47a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ tag in lockstep. Tag releases as `vMAJOR.MINOR.PATCH`. ### Added - Plan approval is a gate (ROADMAP M0.9): `gate-dispatch` starts no execution role (RED, SCAFFOLD, GREEN, structural review, final gate) of a sprint until the human approved that sprint's plan as it is now. `events authority --sprint N` exits 3 with the line to type, `approve SN ` (reviewed, the code naming the sha256 of the sprint's plan files) or `autonomous SN ` (autonomy for that sprint, re-plans included, the code naming the authority record); the task contract's `STORY_DIR` and task must be a live story and task of that plan's frozen `ids.json`; the line must be among the human's captured prompts and typed after the sprint's last decision (M9.17's mechanism). A changed plan, a re-plan in reviewed mode, a roll-back to an old line, another sprint's line, a negated line and a hand-edited or emitted record all block (`plan.approved` and `human.approval` cannot be `events emit`ted); an approved autonomous mission grants its own sprints autonomy with no line. `events approval SPRINT|TASK_ID` is the check. Codex's `spawn_agent` (`agent_type`, `message`) and OpenCode's `task` now reach `gate-dispatch` too, and `claude -p`, `codex exec` and `opencode run` from a tool call are blocked in every aloop project, not only where a mission is set. `e2e-live.sh` pre-approves with an `autonomous S1 ` line in its prompt. Reason: the north star allows no human help beyond approval, and the approval was a playbook rule a supervisor could skip. A 4-lens adversarial review found 9 defects: 7 fixed with a check red on the first head (cross-sprint or unplanned `STORY_DIR`, a retyped line kept stale, mission autonomy over a non-mission sprint, `transcripts prune` dropping the prompts grants cite, `codex plugin add` refused, a code past column 200, a crash on a malformed run-log line), 2 in the playbook (intake reuses a mission's handed-off intent; a reviewed re-plan waits for the human's new line). `transcripts prune` now keeps the human's prompts. Test: `plan-approval.test.py` (20 checks, red on master), plus the host suites. - Core agent principles and the one-way / two-way door rule (ROADMAP §4.12, M9.30): seven core principles from Amazon's Leadership Principles, the door rule first, go to every aloop agent at session start (`aloop mission principles show --core`). A two-way door (a branch, a local edit, a test, a draft PR) is decided, not asked. A one-way door (`aloop mission doors list`: a push or merge to the default branch, a force-push, a release, a deploy, a message sent outside, a secret, dropped data, …) is stopped by a PreToolUse gate with `ASK[door] ` until the human records an authorization in their own terminal (`aloop mission doors authorize ID --by NAME --reason TEXT [--until DATE] [--once DIGEST]`, a hash-chained ledger row); a wall (switching a gate off, forging a human record) is never crossed. While a run is live, a question to the human must name a door the gate stopped and nobody authorized. Reason: Adeel's request that agents decide two-way doors themselves and ask only at a one-way door not already authorized. Test: `mission-doors.test.py`. Hardened by an adversarial review (15 findings, each a test): the gate reads Bash as the shell does (comments, line continuations, heredoc bodies, `xargs`, `find -exec`, `echo … | sh`), a branch condition reads the directory the command acts on (`git -C`, `cd`), settings are guarded wherever they live, dry runs and `command -v` are two-way, the newest decision by time counts and a revoke holds on every branch, a once-grant is crossed once under a lock, a broken ledger is named, and `doors authorize` refuses an agent's shell. -- The door rule on every host and role (ROADMAP §4.12, M9.31): every dispatched role gets the core principles (`aloop mission principles hook` at SubagentStart, Codex agents' instructions, OpenCode's system prompt, each dsh role persona), and the door gate runs on Codex, OpenCode and dsh with each host's question tool mapped. In the OpenCode and dsh plugins it runs last, after every other gate admitted the call, and fails closed after 15 s. A bound worker's command is judged where it runs (its `workdir`), an MCP tool by its own arguments (`mcp____` on OpenCode too), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Hardened by four reviews (71 findings, each a test that failed first): a stalled decision can no longer fall through to an allow, `apply_patch` is read with any header spacing (a U+FEFF too) and through the shell, a bare redirect or a group's redirect reaches the commands inside, a `cd` that may not run leaves the directory unknown, a symlink then `..` is read as the kernel walks it, replacing a settings file with `cp`, `mv`, `ln`, `install`, `rsync` (into a settings directory too), `rm`, `sed -i` or `perl -i`, or removing a directory that holds one, is the settings door, and an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`) crosses the same door as the service's own. A `cd` that may not run leaves each later command in either directory, a copy is placed where it runs (so backing up `.claude` passes), and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Drive access grants are doors too. After `cd A || cd B` a line may be in A, B or where it started, a glob or contents copy names the files it copies, a name the gate cannot see is no settings change at the project root, and a wrapped `cd` (`nohup cd`) moves no shell. Tests: `mission-doors.test.py`, `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`. +- The door rule on every host and role (ROADMAP §4.12, M9.31): every dispatched role gets the core principles (`aloop mission principles hook` at SubagentStart, Codex agents' instructions, OpenCode's system prompt, each dsh role persona), and the door gate runs on Codex, OpenCode and dsh with each host's question tool mapped. In the OpenCode and dsh plugins it runs last, after every other gate admitted the call, and fails closed after 15 s. A bound worker's command is judged where it runs (its `workdir`), an MCP tool by its own arguments (`mcp____` on OpenCode too), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Hardened by five reviews (88 findings, each a test that failed first): a stalled decision can no longer fall through to an allow, `apply_patch` is read with any header spacing (a U+FEFF too) and through the shell, a bare redirect or a group's redirect reaches the commands inside, a `cd` that may not run leaves the directory unknown, a symlink then `..` is read as the kernel walks it, replacing a settings file with `cp`, `mv`, `ln`, `install`, `rsync` (into a settings directory too), `rm`, `sed -i` or `perl -i`, or removing a directory that holds one, is the settings door, and an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`) crosses the same door as the service's own. A `cd` that may not run leaves each later command in either directory, a copy is placed where it runs (so backing up `.claude` passes), and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Drive access grants are doors too. After `cd A || cd B` a line may be in A, B or where it started, a glob or contents copy names the files it copies, a name the gate cannot see is no settings change at the project root, a wrapped `cd` (`nohup cd`) or a background list (`cd X && npm run dev &`) moves no shell, and a copy names what the line staged before it. Tests: `mission-doors.test.py`, `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`. - DeepSeek Harness (dsh) host (ROADMAP §4.9): `aloop install dsh` (alias `deepseek`; `--project P` or `--global`) renders a `dsh --profile aloop` profile in `$DSH_HOME` and installs the skills. Its native plugin (`plugin/hosts/dsh/aloop.mjs`) runs the same gates as Claude Code on dsh's own events: a deny before a tool runs, a block after an invalid artifact write, and a stop gate that steers a role back up to 3 times before the supervisor is told it is BLOCKED (or STUCK, released unverified). Roles are agent presets on two subagent tools, one per model route (`deepseek-v4-pro` for planning, `deepseek-flash` for work by default; `MODEL_PLANNING_DSH` / `MODEL_WORKER_DSH` override). A writer's `aloop bind` moves its working directory into its worktree, and `host-adapter` confines its commands, edits and `working_directory` there. Usage is recorded Claude-shaped per role, so budgets, the ledger and `run-summary` work unchanged. `make e2e-live HOST=dsh` drives it live and exits 2 with the fix when no `DEEPSEEK_API_KEY` is set. Reason: DeepSeek's own harness is the cheapest route to a strong model. The profile records the project it serves (or `--global`), so another project's `aloop-init` never re-renders it, and gates get the worker shell's environment (no API key). The mission layer's six roles run on dsh too: presets on their route group's subagent tool, `gate-mission-scope` on every write and command, and `gate-mission-artifact` at each role's start and stop; the mission skill installs as `aloop-mission`. A second review (3 lenses; 11 confirmed findings, each fixed with a test) closed: a working directory that vanished (dsh's tools move it back to the main tree, so the plugin judges `ensure()`'s directory and `host-adapter` refuses to run from a missing one), gate switches from the project's `.env` (dsh loads it; `SKIP_HOOKS`, `GATE_RUN_TESTS`, `REPO_DIR`, `AGENTIC_*` from it never reach a gate), an interrupted Stop that dropped the sprint-close notice, and the shared profile: its owner is the main tree, an older unrecorded profile is claimed only by the project that installed it, and a project it does not serve is told so by `install status`, `aloop-init` and the session itself. Tests: `dsh-plugin.test.mjs` (46 checks, emulated), `dsh-runtime.test.mjs` (50 checks on the real pinned dsh 0.2.1-rc.2 with a scripted model; the CI hosts job installs it) and `dsh-install.test.py` (10). - dsh follow-ups (ROADMAP §4.9). One `aloop install dsh` serves every project: the plugin routes each role's requests through dsh's `agent/request` to its own project's model (`aloop models resolve `), so gate-dispatch no longer refuses another project's roles until a re-install, and a role whose model cannot be resolved is refused (fail closed). Roles are no longer offered `send_message`, so dsh adds no "send your result" guidance and no call is spent on a deny. dsh compaction is recorded as `context.compacted` and the handoff comes back after it; the capability matrix marks dsh compaction and live messaging as advertised, proven on the real runtime. Two mission roles (one per subagent tool) run through both of their gates on the real runtime. `run-summary --host dsh` reports a dollar cost: each logged call is priced from a dated, sourced DeepSeek table (`plugin/bin/_prices.py`; cache hit, cache miss, output, peak or off-peak), with `cost_by_model` in `summary.json`; an unpriced model id keeps its tokens and leaves the cost unmeasured, never guessed. - Brand kit and repo front page: `docs/brand/` holds the mark (an **a** whose bowl is a red-to-green loop passing a gold gate, with a 16 px cut), lockups, light and dark README banners, favicons, app icons, the site's OG card and the GitHub social preview, all generated by `docs/brand/gen_brand.py` from one palette with WCAG checks (rules in `docs/brand/README.md`). The README leads with the mission layer, the agent principles and the door rule, the supported hosts and the measured demo runs; the site carries the favicon and Open Graph/Twitter card metadata; `SECURITY.md` and bug/feature issue forms are new. diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 738933c0..1fa1f929 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -484,7 +484,7 @@ Each task flips only its own row here and in TRACKING.md. | M9.28 | `tick` drives the goal commands (M9.11): a goal counts only with its `goal.derived`/`goal.approved` record and closes by `goal.met`; handouts name `goal derive`, `options` (spoon-fed), `intents`, `next` and `close`; an eval narrowed to a closed goal asks for a whole-mission eval. Reason: found when M9.11 landed, `tick` read goal files and their status, so a goal nobody derived steered the loop and a met goal was never closed | M9.8, M9.11 | `plugin/mission/tick.py`, `plugin/skills/mission/SKILL.md`, `mission-tick.test.py` | yes | done: `mission-tick.test.py` (29 checks; a real goal run on the real clock: derive → review → intents → next and a sprint per intent → narrowed eval → close → whole-mission eval → `RESEARCH`) | | M9.29 | A goal that crosses a hard line stops being the active goal (`evaluate.active_goal` skips it, or a recorded retirement). Reason: found wiring M9.28, `evaluate` keeps narrowing to a hard-lined goal and `goal derive` refuses a narrowed eval, so the mission deadlocks (`tick` hands it to the human meanwhile) | M9.10, M9.11 | `plugin/mission/{evaluate,goal}.py`, their tests | yes | done: `evaluate.active_goal` skips a goal an ethics hard line retired (it never closes), so the next eval covers the whole mission; `goal derive` from an eval narrowed to a retired goal says to evaluate again; `tick` hands out EVALUATE for such an eval, then DERIVE_GOAL, which takes the next gap (a hard line holds the retired goal's own gaps); when every open gap is held it is BLOCKED for the human (a change of the mission), not handed to a derive that would refuse. `mission-goal.test.py` (section 11), `mission-tick.test.py` (section 27). Call: retirement is derived from the hard line itself, not a new record, so it holds on a fresh clone with the ledger | | M9.30 | Core agent principles and the one-way / two-way door rule, enforced: seven core principles from Amazon's Leadership Principles (the door rule first) for every agent, injected at session start; a door catalogue, the human's standing or once authorization in the ledger, and a PreToolUse gate that stops an unauthorized one-way door (ASK) and a wall (BLOCK) and lets two-way doors pass; while a run is live a question must name a door the gate stopped. Reason: Adeel, 2026-10-20: "If it is two-way, then the agent should take the decision directly without bothering the user. But if it is one-way door, then it can bother user if user already have not specified to cross that door." | M9.0, M9.7 | `plugin/mission/doors.{py,json}`, `principles.{md,py}` (`## Core`), `plugin/bin/session-start`, `hooks.json` (one line), core: `_events.py` / `_mission.py` (door events), `events` (`GATE_OWNED`), `scripts/test/mission-doors.test.py` | after M9.7 | done: `mission doors list / classify / authorize / revoke / check / hook`, `mission principles show --core`; `mission-doors.test.py` (21 checks), `mission-principles.test.py` (+1 check). Calls: anything the catalogue does not list is two-way (a finite list cannot claim full enforcement, §7); Bash is matched by program and arguments after reading the line as the shell does, with the scope gate's reader (`_shellread.py`, M9.38: quotes, comments, line continuations, `$(…)`, a heredoc's body as data unless a shell or python runs it) and unwrapping `sudo`, `env`, `sh -c`, `eval`, `xargs`, `find -exec` and `echo … | sh`, so text that only mentions a door (`grep`, a commit message, a file written by a heredoc) stays two-way; a `when` condition (on the default branch, the main checkout) reads the directory the command acts on (`git -C DIR`, `cd DIR &&`), and one the gate cannot resolve counts; grants are standing (until revoked or `--until`), once (one action's digest, 24 h, used up by its first crossing, recorded in the ledger), never (a wall: switching a gate off, forging a human record; refused even for the human's `authorize`) and command (question-only doors answered by their own command); authorizations are hash-chained human ledger rows (an edited row, `events emit` or a stream-only event opens nothing; `HUMAN_EVENTS` are now gate-owned, which also closes a forged `baseline.vetoed`); a question is checked only while a run is live (an approved autonomous mission or an autonomous run, or a sprint whose latest plan is approved, for the session holding its lock), so intake, planning and the plan's own approval are asked; the core block is kept out of the goal blocks, so editing it never stales a goal's stamp; the hook fails closed (exit 2) and is silent outside an aloop project; measured: median 81 ms per two-way call, 102 ms per stopped one-way call (30 calls each, in parallel with the other `*` hooks). Calls from its adversarial review (15 confirmed findings over 3 lenses, each now a test that failed before its fix): the newest decision about a door is the newest by time (a union merge that puts an older authorization last reopens nothing), and the run log's revokes and crossings close on any branch of the checkout; a once-grant is checked and crossed under a lock (`.aladdin/state/doors.lock`); a broken ledger is named in the ASK, fails `doors check` and blocks `authorize` with the restore line; settings and the ledger are matched by path suffix (`~/.claude`, a worktree's own); a dry run, a help page and `command -v` are two-way, `--dry-run=none` is not; `git -c core.hooksPath=` is a wall and `git config core.hooksPath VALUE` a one-way door; arguments `xargs` adds from stdin may be any flag of a one-way door; `doors authorize` refuses an agent's shell (`CLAUDECODE` set: the human authorizes in their own terminal); Slack's real send tools (`slack_post_message`, `slack_reply_to_thread`) are one-way; a hard ethics line only the run log holds (recorded before the ledger did) still binds. Calls from its second review (26 confirmed findings over parser, catalogue and state lenses, each now a test that failed before its fix): the gate reads Bash with `_shellread.py` (M9.38), which now keeps each heredoc's body, the operator that ends a command and heredoc bodies inside `$(…)`, so touching operators (`); git push`), keywords, groups, function bodies, here-strings, IO-number redirects (`2>&1`) and quoted operators (`find \( … \) -exec`) hide nothing, a quote in `"$(cat <<'EOF' … EOF)"` is data, stdin (a heredoc, a here-string, `echo|printf|cat … |`) reaches only the command it feeds (a shell with options or `-s`, a python with `-`), and `pushd`/`popd`, `sh -c`, `eval` and a fed shell keep the directory; nesting deeper than 8 is unreadable and a read past 8 s fails closed (exit 2, under the hook's 15 s); python is a forge only when it calls a writer, `npm|pnpm|yarn -g` a system change only with an installing verb, the ledger only at `docs/agents/mission/ledger.jsonl`, `xargs … doors` only when stdin can be the subcommand (`-I R` replaces R), a secret command's dry run or help page is two-way, `git push origin HEAD|@` on the default branch is one-way, and erasing or editing the run log (`.aladdin/logs/events.jsonl`, whose revokes and crossings close doors) is a wall; a ledger row is written before its stream event and `ledger_problems` reads the stream first, so no gate sees another writer's half-written record as a lost row; a record is dated no earlier than any ledger row, so the human's fresh decision is the newest even after a merged future-dated row; a run stops being live when its plan's sprint closes (`sprints.json`) or a new intent arrives; a malformed run-log line never crashes the ethics check | -| M9.31 | Door rule on every host and role: the core block in SubagentStart for dispatched roles, Codex `developer_instructions`, OpenCode agents and the dsh profile; the hook in Codex PreToolUse, the OpenCode plugin (`aloop.js`) and the dsh plugin (`aloop.mjs`, whose PreToolUse list mirrors `hooks.json` by hand and so lacks `mission doors hook`), with each host's question tool mapped. Also measure whether a human's own `!` shell command reaches PreToolUse (it should not be gated: the human crossing a door is the authorization). Reason: M9.30 wires Claude Code's main session only | M9.30, M9.9 | `plugin/bin/host-adapter`, `install`, `aloop.js`, `aloop.mjs`, `_host.py` | after M9.9 | done: the core block reaches every role on every host (`mission principles hook`, the third SubagentStart command; Codex agents' `developer_instructions`; OpenCode's system transform for a sub-agent and a resumed root; each dsh role persona, written by `install`). The door gate runs on Codex through `hooks.json`, and last in the OpenCode and dsh plugins (after every other gate admitted the call, so only a call that runs uses up a once-authorization; read-only tools skip it; 15 s, fail closed). Each host's question tool is the gate's (`AskUserQuestion`, Codex `request_user_input`, OpenCode `question`, dsh `ask_user_question`). A bound worker's command is read where it runs (`tool_input.workdir`, a relative one under the project), a dsh MCP tool's input is its own arguments (its once digest is the one any host prints), and an OpenCode MCP tool is `mcp____` (from `client.mcp.status()`, the longest server first). One deadline covers the decision (`Deadline`, which no step's `except Exception` can swallow into an allow), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Four reviews (12, 7, 34 and 18 findings, each a test that failed first) added: `apply_patch` read as the hosts parse it (any header spacing or indent, a U+FEFF around the path cut as OpenCode's `trim()` cuts it, and through the shell: Codex's heredoc and argument forms, `applypatch`, after a `cd`, from a pipe; the header reader `_patch.py` is shared with host-adapter and both scope gates, but only the door gate reads the shell form, M9.61); a bare redirect, and a group's or compound's own redirect or heredoc, reach the commands inside; a `cd` that may not run leaves the directory unknown; a path through a symlink then `..` read as the kernel walks it (`git -C`, `cd -P`, a dsh `workdir`); settings replaced, moved, linked or removed by `cp`/`mv`/`ln`/`install`/`rsync` (into a settings directory too, by `-t` or a trailing `DIR/`), `rm`/`mv` of a directory that holds them, a glob that names them, `sed -i` (and `--in-pl`), `perl -i`; `opencode.json` a door only where OpenCode reads it (a nested checkout's root, `~/.config/opencode`, its `config.json`); an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`, `mcp__google-drive__share_file`) crosses the same door as the service's own server, while a service word inside another tool's name (`search_slack_posts`, `get_post_history`) is none. The third review: a `cd` that may not run leaves each later command in either directory (one view per directory, not an unknown one), `cd -`, a bare `pushd`, `popd -n`, `cd X &`, an eval's `cd`, `~+` and `GIT_DIR`/`GIT_WORK_TREE` are read as the shell reads them, and a pipe into a `{ }` group or a compound reaches each command inside; a copy is placed where it runs (DEST/NAME only into an existing directory, so a backup of `.claude` or a file copied into it passes; `-t` with `{}` or xargs' arguments, `--parents`, `-R`, `--remove-source-files`, a link to a settings file, a home directory's contents, GNU option abbreviations); a glob is expanded only where the shell expands it, through links, and never loses a config-like name; another checkout's run home is not this project's; and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Google Workspace's Drive grants cross their doors. The fourth: a copy option that takes no value (`rsync --checksum`, `install --strip`) is no prefix of one that takes one, and only an operand right after an unknown option may be its value, so only then does the destination widen (to at most 8 operands: 600 files and a trailing `-f` stay fast); a glob source names the files it matches now, a contents copy (`SRC/.`, rsync's `SRC/`) its entries, and a name the gate cannot see (`"$f"`, `{}`) is `DEST/?`: no settings change at the project's, a checkout's or the home root, but one into a settings or run-record directory; `ln -n` over a linked settings directory replaces it; `./.claude` after an unknown `cd` and `"$REPO/.claude"` are read by name; after `cd A || cd B` the line is in A, B or where it started; OLDPWD follows a `cd` that may not run and survives a subshell; `cd X || { …; exit 1; }` and `&& ( … ) &&` keep the line in X (the shell reader carries the operator after a subshell or compound, `Mark.end`), and a group after `||` starts where the left side failed; a wrapped `cd` (`nohup`, `env`, `flock`) moves no shell; candidate directories pass into nested reads instead of multiplying them, and a path is placed once per directory; a tool that sends a draft sends (`send-shared-mailbox-draft`) and Drive's `grant_batch` shares. Tests: `mission-doors.test.py` (42 checks), `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`, `hook-matchers.test.py`, `mission-principles.test.py`, `dsh-install.test.py`. Calls: the catalogue keeps its generic `_` alternatives for an OpenCode that cannot list its servers (an extra question beats a silent one-way door). Open: M9.56–M9.65 (the `!` measurement is M9.58) | +| M9.31 | Door rule on every host and role: the core block in SubagentStart for dispatched roles, Codex `developer_instructions`, OpenCode agents and the dsh profile; the hook in Codex PreToolUse, the OpenCode plugin (`aloop.js`) and the dsh plugin (`aloop.mjs`, whose PreToolUse list mirrors `hooks.json` by hand and so lacks `mission doors hook`), with each host's question tool mapped. Also measure whether a human's own `!` shell command reaches PreToolUse (it should not be gated: the human crossing a door is the authorization). Reason: M9.30 wires Claude Code's main session only | M9.30, M9.9 | `plugin/bin/host-adapter`, `install`, `aloop.js`, `aloop.mjs`, `_host.py` | after M9.9 | done: the core block reaches every role on every host (`mission principles hook`, the third SubagentStart command; Codex agents' `developer_instructions`; OpenCode's system transform for a sub-agent and a resumed root; each dsh role persona, written by `install`). The door gate runs on Codex through `hooks.json`, and last in the OpenCode and dsh plugins (after every other gate admitted the call, so only a call that runs uses up a once-authorization; read-only tools skip it; 15 s, fail closed). Each host's question tool is the gate's (`AskUserQuestion`, Codex `request_user_input`, OpenCode `question`, dsh `ask_user_question`). A bound worker's command is read where it runs (`tool_input.workdir`, a relative one under the project), a dsh MCP tool's input is its own arguments (its once digest is the one any host prints), and an OpenCode MCP tool is `mcp____` (from `client.mcp.status()`, the longest server first). One deadline covers the decision (`Deadline`, which no step's `except Exception` can swallow into an allow), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Five reviews (12, 7, 34, 18 and 17 findings, each a test that failed first) added: `apply_patch` read as the hosts parse it (any header spacing or indent, a U+FEFF around the path cut as OpenCode's `trim()` cuts it, and through the shell: Codex's heredoc and argument forms, `applypatch`, after a `cd`, from a pipe; the header reader `_patch.py` is shared with host-adapter and both scope gates, but only the door gate reads the shell form, M9.61); a bare redirect, and a group's or compound's own redirect or heredoc, reach the commands inside; a `cd` that may not run leaves the directory unknown; a path through a symlink then `..` read as the kernel walks it (`git -C`, `cd -P`, a dsh `workdir`); settings replaced, moved, linked or removed by `cp`/`mv`/`ln`/`install`/`rsync` (into a settings directory too, by `-t` or a trailing `DIR/`), `rm`/`mv` of a directory that holds them, a glob that names them, `sed -i` (and `--in-pl`), `perl -i`; `opencode.json` a door only where OpenCode reads it (a nested checkout's root, `~/.config/opencode`, its `config.json`); an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`, `mcp__google-drive__share_file`) crosses the same door as the service's own server, while a service word inside another tool's name (`search_slack_posts`, `get_post_history`) is none. The third review: a `cd` that may not run leaves each later command in either directory (one view per directory, not an unknown one), `cd -`, a bare `pushd`, `popd -n`, `cd X &`, an eval's `cd`, `~+` and `GIT_DIR`/`GIT_WORK_TREE` are read as the shell reads them, and a pipe into a `{ }` group or a compound reaches each command inside; a copy is placed where it runs (DEST/NAME only into an existing directory, so a backup of `.claude` or a file copied into it passes; `-t` with `{}` or xargs' arguments, `--parents`, `-R`, `--remove-source-files`, a link to a settings file, a home directory's contents, GNU option abbreviations); a glob is expanded only where the shell expands it, through links, and never loses a config-like name; another checkout's run home is not this project's; and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Google Workspace's Drive grants cross their doors. The fourth: a copy option that takes no value (`rsync --checksum`, `install --strip`) is no prefix of one that takes one, and only an operand right after an unknown option may be its value, so only then does the destination widen (to at most 8 operands: 600 files and a trailing `-f` stay fast); a glob source names the files it matches now, a contents copy (`SRC/.`, rsync's `SRC/`) its entries, and a name the gate cannot see (`"$f"`, `{}`) is `DEST/?`: no settings change at the project's, a checkout's or the home root, but one into a settings or run-record directory; `ln -n` over a linked settings directory replaces it; `./.claude` after an unknown `cd` and `"$REPO/.claude"` are read by name; after `cd A || cd B` the line is in A, B or where it started; OLDPWD follows a `cd` that may not run and survives a subshell; `cd X || { …; exit 1; }` and `&& ( … ) &&` keep the line in X (the shell reader carries the operator after a subshell or compound, `Mark.end`), and a group after `||` starts where the left side failed; a wrapped `cd` (`nohup`, `env`, `flock`) moves no shell; candidate directories pass into nested reads instead of multiplying them, and a path is placed once per directory; a tool that sends a draft sends (`send-shared-mailbox-draft`) and Drive's `grant_batch` shares. The fifth, a regression hunt on the fourth's fixes: `cp --` no longer crashes the gate; an exit inside `|| { … }` counts only when it always runs; past `&&` or a compound, `|| Y` is reached from where the list succeeded too; a second `cd` that may not run keeps where the list stopped; a background list (`cd X && npm run dev &`) moves no shell; `find -execdir` runs in an unknown directory and `env -C DIR` in DIR; a glob or contents copy also names what the line staged before it and the dotfiles a `*` may name (dotglob); `cp -T` and `rsync -R SRC/./` copy SRC's entries; an absolute path is placed after an unknown `cd`; `$(pwd)` is `$PWD`, `${X:?}` is `$X`, and a run record under a variable directory is that record; a variable directory is a settings root only for `$HOME` (so `cp -a . "$tmp"` passes); an unseen name copied into `docs/agents/mission/` forges the ledger; a large directory's copy is placed by its directory once; and a tool that writes a draft (`send_mail_draft`, `forward_as_draft`) does not send. Tests: `mission-doors.test.py` (45 checks), `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`, `hook-matchers.test.py`, `mission-principles.test.py`, `dsh-install.test.py`. Calls: the catalogue keeps its generic `_` alternatives for an OpenCode that cannot list its servers (an extra question beats a silent one-way door). Open: M9.56–M9.65 (the `!` measurement is M9.58) | | M9.32 | Intent questions name their door: `open_questions[].door`; `intent check` blocks an assume/default on a one-way door and treats an `ask` on a door the human authorized as answered. Reason: the plan-time half of the door rule | M9.30, M9.11 | `plugin/bin/intent`, its test | yes | open | | M9.33 | Remove the model-facing `SKIP_HOOKS=1` hints (`gate-supervisor-scope`, `_failclosed.sh`, `SKILL.md`). Reason: they hand the agent the bypass-gate wall's own key | M9.30 | those files | yes | open | | M9.34 | A git pre-push backstop from `aloop init` for pushes to the default branch and force pushes. Reason: holds when Bash matching is evaded (a script, an alias) | M9.30 | `plugin/bin/aloop-init`, its test | yes | open | diff --git a/plugin/bin/_shellread.py b/plugin/bin/_shellread.py index da91f86c..145b05eb 100644 --- a/plugin/bin/_shellread.py +++ b/plugin/bin/_shellread.py @@ -431,9 +431,10 @@ def dollar(self, w): return rest = inner[m.end():] op = re.match(r":?[-=]", rest) - if not rest: - w.add(("var", m.group(), None)) - elif op: + if not rest or re.fullmatch(r":?\?[^$`]*", rest): + w.add(("var", m.group(), None)) # ${x:?msg}: $x, or the shell stops + elif op or re.match(r":?\?", rest): + op = op or re.match(r":?\?", rest) sub = Lexer(rest[op.end():]) dw = sub.word(stop="") w.add(("var", m.group(), dw.parts)) diff --git a/plugin/mission/doors.json b/plugin/mission/doors.json index f8f0dfd5..c5201d8a 100644 --- a/plugin/mission/doors.json +++ b/plugin/mission/doors.json @@ -229,8 +229,8 @@ } ], "tools": [ - "(?i)^(?:mcp__(?:[\\w-]*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[\\w-]*?__(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[_-])?|[\\w-]+?__\\w*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[A-Za-z0-9]*[_-])|(?!mcp__)[\\w-]*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[A-Za-z0-9-]*_(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[_-])?)(?:(?:chat[_-]?)?(?:send|reply|forward|post|schedule[_-]?(?:message|post|email|mail|send))|(?:conversations[_-])?add[_-]message)(?!s(?:$|[_-](?!an?[_-]))|ed(?:$|[_-])|ing)(?![\\w-]*(?:message|mail|email)[_-]draft$)[\\w-]*$", - "(?i)^(?:mcp__[\\w-]+?__|(?!mcp__)[\\w-]+?_)(?:send|reply|forward|post)[_-](?:[\\w-]*?[_-])?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)(?![\\w-]*(?:message|mail|email)[_-]draft$)[\\w-]*$" + "(?i)^(?:mcp__(?:[\\w-]*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[\\w-]*?__(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[_-])?|[\\w-]+?__\\w*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[A-Za-z0-9]*[_-])|(?!mcp__)[\\w-]*?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[A-Za-z0-9-]*_(?:(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[_-])?)(?:(?:chat[_-]?)?(?:send|reply|forward|post|schedule[_-]?(?:message|post|email|mail|send))|(?:conversations[_-])?add[_-]message)(?!s(?:$|[_-](?!an?[_-]))|ed(?:$|[_-])|ing)(?![\\w-]*(?:message|mail|email|as)[_-]draft$)[\\w-]*$", + "(?i)^(?:mcp__[\\w-]+?__|(?!mcp__)[\\w-]+?_)(?:send|reply|forward|post)[_-](?![\\w-]*(?:message|mail|email|as)[_-]draft$)(?:[\\w-]*?[_-])?(?:gmail|mail|outlook|slack|teams|discord|telegram|twitter|linkedin|ms365|m365)[\\w-]*$" ], "examples": { "bash": [ @@ -472,7 +472,7 @@ "writes": "(?:^|/)(?:\\.codex/(?:config\\.toml|hooks\\.json|agents/[^/]+\\.toml)|(?:\\.opencode|\\.config/opencode)/plugins/aloop\\.js|(?:\\.opencode|\\.config/opencode|worktrees/[^/]+)/opencode\\.jsonc?|\\.config/opencode/config\\.json|\\.dsh/profiles/[^/]+/(?:cordis\\.patch\\.yml|package\\.json))$|^opencode\\.jsonc?$" }, { - "writes": "^(?:(?:~/|\\$[^/]*/)?(?:\\.claude|\\.codex(?:/agents)?|\\.opencode(?:/plugins)?|\\.config(?:/opencode(?:/plugins)?)?|\\.dsh(?:/profiles(?:/[^/]+)?)?)(?:/[*?])?/?|(?:~/)?\\*)$" + "writes": "^(?:(?:~/|\\$\\{?HOME\\}?/)?(?:\\.claude|\\.codex(?:/agents)?|\\.opencode(?:/plugins)?|\\.config(?:/opencode(?:/plugins)?)?|\\.dsh(?:/profiles(?:/[^/]+)?)?)(?:/[*?])?/?|\\$[^/]*/(?:\\.claude|\\.codex(?:/agents)?|\\.opencode(?:/plugins)?|\\.config(?:/opencode(?:/plugins)?)?|\\.dsh(?:/profiles(?:/[^/]+)?)?)/[*?]/?|(?:~/)?\\*)$" }, { "prog": "rm|unlink|truncate|shred", @@ -612,7 +612,7 @@ "args": "^doors\\s+(?:authorize|revoke)\\b" }, { - "writes": "(?:^|/)docs/agents/mission/(?:ledger\\.jsonl|\\*)$" + "writes": "(?:^|/)docs/agents/mission/(?:ledger\\.jsonl|[*?])$" }, { "writes": "(?:^|/)\\.(?:aladdin|agentic)/logs/(?:events\\.jsonl|[*?])$" diff --git a/plugin/mission/doors.py b/plugin/mission/doors.py index 82a0e440..5f55a8e1 100644 --- a/plugin/mission/doors.py +++ b/plugin/mission/doors.py @@ -42,6 +42,7 @@ """ import contextlib import functools +import fnmatch import glob import hashlib import itertools @@ -215,7 +216,8 @@ def render(parts): elif p[0] == "var": out.append("~" if p[1] == "HOME" and p[2] is None else "$" + p[1]) elif p[0] == "cmd": - out.append(TOP if " ".join(p[1].split()) == "git rev-parse --show-toplevel" else "$(…)") + t = " ".join(p[1].split()) + out.append(TOP if t == "git rev-parse --show-toplevel" else "$PWD" if t in ("pwd", "pwd -L", "pwd -P") else "$(…)") elif p[0] == "proc": out.append("<(…)") else: @@ -313,24 +315,27 @@ def views(cmd, depth=0, cur=""): out, stack, dirs, fed, group = [], [], [], None, None opened, closed, scopes, prev, last = [], None, [], None, None maybe, failed, old, gitenv = None, None, (UNKNOWN,), False + base = (cur, old, []) # where the current and-or list began (`a && b &` runs in a subshell) for c in R.read(cmd): if isinstance(c, str): link = getattr(c, "end", None) # the list operator after a subshell or compound + if c in ("(", "cwd+") and prev not in ("&&", "||"): + base = (cur, old, list(dirs)) inner = cur # where it starts: after `cd X ||`, where the cd failed if prev == "||" and failed is not None: inner = failed elif prev == "||" and maybe is not None: inner = union(maybe, cur) if c == "(": # a pipe into a subshell feeds the commands inside it - stack.append((cur, group, maybe, failed, old, list(dirs))) + stack.append((cur, group, maybe, failed, old, list(dirs), base)) cur, group, maybe, failed = inner, (fed if fed is not None else group), None, None opened.append(len(out)) elif c == ")": if stack: - cur, group, maybe, failed, old, dirs[:] = stack.pop() + cur, group, maybe, failed, old, dirs[:], base = stack.pop() closed = opened.pop() if opened else None elif c == "cwd+": # an if, case, loop body or { …; } group: its `cd` may or may not run - scopes.append((inner, [], group, maybe, failed, old, cur if prev == "||" else None)) + scopes.append((inner, [], group, maybe, failed, old, cur if prev == "||" else None, base)) cur, group, maybe, failed = inner, (fed if fed is not None else group), None, None opened.append(len(out)) elif c == "cwd|" and scopes: # a branch ends: the next one starts where the compound did @@ -338,7 +343,7 @@ def views(cmd, depth=0, cur=""): cur, old = scopes[-1][0], scopes[-1][5] elif c == "cwd-": if scopes: - entry, tails, group, maybe, failed, old0, skip = scopes.pop() + entry, tails, group, maybe, failed, old0, skip, base = scopes.pop() cur = union(entry, *(t for t, _ in tails), cur) old = union(old0, *(o for _, o in tails), old) if skip is not None and last in ("exit", "return"): @@ -346,12 +351,19 @@ def views(cmd, depth=0, cur=""): elif skip is not None: cur = union(skip, cur) # `A || { … }`: A succeeded, or the group ran closed = opened.pop() if opened else None + if c in (")", "cwd-") and (link == "&&" or c == "cwd-"): + failed = None # past it, `|| Y` is reached from the success path too if c in (")", "cwd-") and maybe is not None and link not in ("&&", "||"): cur, maybe, failed = union(maybe, cur), None, None # its list ends here + if c in (")", "cwd-") and link == "&": + cur, old, dirs[:] = base[0], base[1], base[2] # a background list moves no shell + maybe, failed = None, None fed, last = None, None prev = link if c in (")", "cwd-") and link in ("&&", "||") else None continue own, closed = (closed if not c.words and not c.assigns else None), None + if prev not in ("&&", "||", "|", "|&"): + base = (cur, old, list(dirs)) start = here = cur if prev == "||" and failed is not None: here = failed # `cd X || Y`: Y runs where the cd failed @@ -380,7 +392,7 @@ def views(cmd, depth=0, cur=""): cur, old = moved(mover["prog"], mover["args"], here, old, dirs) if prev in ("&&", "||"): # it may not run: known only along its own list old = union(old0, old) - maybe = before if maybe is None else maybe + maybe = before if maybe is None else union(maybe, before) if prev == "||": # `A || cd X`: X, or where A left it cur = union(start, cur) if c.end == "||": # `cd X || Y`: Y runs where the cd failed; after it, either @@ -411,11 +423,16 @@ def views(cmd, depth=0, cur=""): out += own_stdin(feed(vs, stdin, depth)) fed = stdout(vs, stdin) if c.end in ("|", "|&") else None out += vs + if c.end == "&&": + failed = None # `cd X || a && b || Y`: Y is reached from X too if c.end not in ("&&", "||", "|", "|&"): if maybe is not None: cur = union(maybe, cur) maybe, failed = None, None - prev, last = c.end, prog + if c.end == "&" and prev in ("&&", "||", "|", "|&"): + cur, old, dirs[:] = base[0], base[1], base[2] # `cd X && make &`: the whole list is a background job + last = prog if prev not in ("&&", "||") and not c.pipe and c.end != "&" else None # an exit that always runs + prev = c.end return out @@ -543,7 +560,7 @@ def feed(vs, text, depth): "--only-write-batch", "--max-delete", "--block-size", "--modify-window", "--address", "--outbuf", "--info", "--debug", "--stop-after", "--stop-at", "--copy-as", "--remote-option", "--log-format", "--compress-level", "--checksum-seed", "--protocol", "--max-alloc", "--cc", "--zc", "--zl", - "--early-input"}} + "--early-input", "--stderr"}} COPY_FLAGS = { # their long options that take no separate value (a bare --backup, not --backup=CONTROL) "cp": {"--archive", "--attributes-only", "--backup", "--copy-contents", "--debug", "--force", "--interactive", "--link", "--dereference", "--no-clobber", "--no-dereference", "--preserve", "--parents", "--recursive", @@ -588,7 +605,7 @@ def is_(k, name, least): while i < len(args): a = args[i] if a == "--": - ops += args[i + 1:] + ops += [(o, False) for o in args[i + 1:]] break if a.startswith("--"): k, eq, v = a.partition("=") @@ -639,64 +656,81 @@ def is_(k, name, least): if prog == "ln" and len(ops) == 1: return [dict(plan, dest=".", srcs=words, target=True)] # ln -s TARGET: a link named after it, here k, out = len(ops) - 1, [] - while k >= 1 and len(out) < MAXDESTS: # the last operand, and before it while it may be an option's value - out.append(dict(plan, dest=words[k], srcs=words[:k])) + while k >= 1: # the last operand, and before it while it may be an option's value + out.append(dict(plan, dest=words[k], srcs=words[:k] if len(out) < MAXDESTS else [])) if not ops[k][1]: break k -= 1 return out -def copy_writes(cwd, v, plan): +def copy_writes(cwd, v, plan, made=()): """The paths a copy plan writes where its command runs: DEST/NAME for each source when DEST is a directory (or -t DIR, or more than one source or a trailing / or a DEST the gate cannot place), else DEST itself (the copy becomes DEST); a glob source's NAMEs are the files it names now; a source whose contents are copied (SRC/., rsync's SRC/) gives each of its entries (DEST/* when the gate cannot list them); a source whose name the gate cannot see ({}, xargs' arguments, a variable) is DEST/?; DEST/SRC under --parents or -R; `ln -n` over a link replaces DEST; and the sources a move takes away. A remote - rsync destination is not this machine's.""" + rsync destination is not this machine's. `made`: the absolute paths the line wrote before this command + (a directory it filled, a file it staged), listed with what is there now.""" dest, srcs, rsync = plan["dest"], plan["srcs"], plan["prog"] == "rsync" out = list(srcs) if plan["removes"] else [] if dest is None or rsync and re.match(r"^[^/]*:", dest): return out - if plan["whole"]: - return out + [dest] - where = where_of(v, dest) - full = resolve(cwd, where) if where != UNKNOWN else "" - if plan.get("nodir") and full and not dest.endswith("/") and os.path.islink(full): - return out + [dest] # ln -sfn X LINK: the link itself is replaced - isdir = os.path.isdir(full) if full else None # None: the gate cannot tell base = dest.rstrip("/") or "/" - def contents(s): # the entries of SRC/. (rsync SRC/), as copied - w = where_of(v, s) - f = resolve(cwd, w) if w != UNKNOWN else "" + def entries(f): # a directory's names now and the line's own try: - names = os.listdir(f) if f else None + names = os.listdir(f) except OSError: names = None + made_here = [m[len(f.rstrip("/")) + 1:].split("/")[0] for m in made if m.startswith(f.rstrip("/") + "/")] + return None if names is None and not made_here else sorted(set(names or []) | set(made_here)) + + def contents(s): # the entries of SRC/. (rsync SRC/), as copied + w = where_of(v, s) + f = resolve(cwd, w) if w != UNKNOWN else "" + names = entries(f) if f else None if names is None or len(names) > GLOB_SCAN: return [base + "/*"] - keep = sorted(n for k, n in enumerate(sorted(names)) if k < GLOB_MAX or KEEP.search(n)) + keep = sorted(n for k, n in enumerate(names) if k < GLOB_MAX or KEEP.search(n)) return [base + "/" + n for n in keep[:4 * GLOB_MAX]] + ([base + "/*"] if len(keep) > 4 * GLOB_MAX else []) + def a_dir(s): # rsync copies a directory into DEST, as DEST/NAME + w = where_of(v, s) + f = resolve(cwd, w) if w != UNKNOWN else "" + return not f or os.path.isdir(f) or any(m.startswith(f.rstrip("/") + "/") for m in made) + if plan["whole"]: # cp -T SRCDIR DEST: DEST gets SRCDIR's entries + return out + [dest] + [w for s in srcs if plan["prog"] == "cp" and a_dir(s) + for w in contents(s.rstrip("/") + "/.")] + where = where_of(v, dest) + full = resolve(cwd, where) if where != UNKNOWN else "" + if plan.get("nodir") and full and not dest.endswith("/") and os.path.islink(full): + return out + [dest] # ln -sfn X LINK: the link itself is replaced + isdir = os.path.isdir(full) if full else None # None: the gate cannot tell + + def globbed(s): # what the shell's glob names: now, made, dot entries + names = [os.path.basename(m.rstrip("/")) for m in matches(cwd, v, s)] + head, last = os.path.split(s) + w = where_of(v, head or ".") if not re.search(r"[*?\[]", head) else UNKNOWN + f = resolve(cwd, w) if w != UNKNOWN else "" + if f and re.match(r"[*?\[]", last): # dotglob or GLOBIGNORE: `*` names dotfiles too + names += [n for n in (entries(f) or []) if fnmatch.fnmatch(n, last) + and (n.startswith(".") or not os.path.lexists(os.path.join(f, n)))] + return sorted(set(names)) + def inside(s): if plan["parents"]: - s = s.split("/./", 1)[1] if rsync and "/./" in s else s - return [base + "/" + s.lstrip("/")] + rel = s.split("/./", 1)[1] if rsync and "/./" in s else s + return contents(s) if rel.strip("/") in ("", ".") else [base + "/" + rel.lstrip("/")] if s.endswith("/.") or rsync and s.endswith("/"): return contents(s) - if s in v.get("globs", ()) and re.search(r"[*?]", s): # the shell expands it: each file it names now - return [base + "/" + os.path.basename(m.rstrip("/")) for m in matches(cwd, v, s)] or [base + "/?"] + if s in v.get("globs", ()) and re.search(r"[*?]", s): # the shell expands it: each file it names + return [base + "/" + n for n in globbed(s)] or [base + "/?"] name = os.path.basename(s.rstrip("/")) if s in (STDIN, "{}") or re.search(r"[$`]", name): return [base + "/?"] # a name the gate cannot see return [base + "/" + name] if name not in ("", ".", "..") else contents(s + "/.") - - def a_dir(s): # rsync copies a directory into DEST, as DEST/NAME - w = where_of(v, s) - f = resolve(cwd, w) if w != UNKNOWN else "" - return not f or os.path.isdir(f) if plan["target"] or isdir: return out + [w for s in srcs or [STDIN] for w in inside(s)] if isdir is False and len(srcs) == 1 and not dest.endswith("/"): @@ -816,7 +850,7 @@ def chain(words, env, writes, depth, cur): if inner: if depth >= DEPTH: raise ValueError("find -exec nests deeper than the door gate reads (%d)" % DEPTH) - out += chain(inner, [], [], depth + 1, cur) + out += chain(inner, [], [], depth + 1, (UNKNOWN,) if rest[j] in ("-execdir", "-okdir") else cur) j += len(inner) j += 1 break @@ -848,7 +882,13 @@ def chain(words, env, writes, depth, cur): o.split("=", 1)[1] if o.startswith("--") else o[2:] words = split.split() + words[2 if o in ("-S", "--split-string") else 1:] continue - if o in ("-u", "--unset", "-C", "--chdir"): + if o in ("-C", "--chdir") or o.startswith("--chdir=") or o.startswith("-C") and len(o) > 2: + to = (words[1] if len(words) > 1 else None) if o in ("-C", "--chdir") else \ + o.split("=", 1)[1] if o.startswith("--") else o[2:] + cur = union(*((chdir(d, to),) for d in cur)) # env -C DIR: the command runs there + words = words[2:] if o in ("-C", "--chdir") else words[1:] + continue + if o in ("-u", "--unset"): words = words[2:] continue if ASSIGN.match(o): @@ -1024,13 +1064,14 @@ def placed(cwd, top, v, path, kernel=False, here=None): return path if re.match("(?:%s|\\$PWD|\\$CLAUDE_PROJECT_DIR)(?:/|$)" % re.escape(TOP), path): where = chdir(v["dir"], path) - elif v["dir"] == UNKNOWN or re.search(r"[$`]", path): + elif re.search(r"[$`]", path) or v["dir"] == UNKNOWN and not (os.path.isabs(path) or path.startswith("~")): where = UNKNOWN else: where = path if os.path.isabs(path) or path.startswith("~") or not v["dir"] else v["dir"] + "/" + path if where == UNKNOWN: x = os.path.normpath(path) # as written (./.claude is .claude) - return next((g for g in GUARDED if not os.path.isabs(x) and (g == x or g.endswith("/" + x))), x) + return next((g for g in GUARDED if not os.path.isabs(x) and (g == x or g.endswith("/" + x) or re.fullmatch( + r"\$[^/]*/" + re.escape(g), x))), x) full = resolve(cwd, where, kernel) if not full: return path @@ -1041,6 +1082,7 @@ def placed(cwd, top, v, path, kernel=False, here=None): return full +NAMED = "\x01name" # a name placed by its directory's template (place(): DIR/NAME is DIR's forms plus NAME) GLOB_MAX = 512 # the files of a glob the gate lists (and any name a host's config or a run record may have) GLOB_SCAN = 20000 # past this many, the glob's directory stands for the rest KEEP = re.compile(r"(?:^|/)(?:\.[^/]*|[^/]*\.(?:jsonc?|jsonl|toml|ya?ml|m?js))$") @@ -1048,9 +1090,13 @@ def placed(cwd, top, v, path, kernel=False, here=None): def where_of(v, path): """Where a command's path operand is, as resolve() takes it (UNKNOWN: the gate cannot tell).""" - if v["dir"] == UNKNOWN or re.search(r"[$`]", path) or "\n" in path or len(path) > 4096: + if re.search(r"[$`]", path) or "\n" in path or len(path) > 4096: return UNKNOWN - return path if os.path.isabs(path) or path.startswith(("~", TOP)) or not v["dir"] else v["dir"] + "/" + path + if os.path.isabs(path) or path.startswith("~"): + return path # where it says, wherever the command runs + if v["dir"] == UNKNOWN: + return UNKNOWN + return path if path.startswith(TOP) or not v["dir"] else v["dir"] + "/" + path def matches(cwd, v, path): @@ -1110,7 +1156,7 @@ def place(cwd, vs): """Each view's operands and writes as placed() reads them (files, placed_writes): as written and as the kernel walks them, each file a glob names, relative to the project's checkout that holds it and to the home directory; a copy's destination as copy_writes() places it.""" - top, seen, here = os.path.realpath(_paths.project(cwd)), {}, toplevel(os.path.abspath(cwd)) + top, seen, here, made = os.path.realpath(_paths.project(cwd)), {}, toplevel(os.path.abspath(cwd)), [] def forms(v, a): # once per (directory, path): candidate dirs repeat them key = (v["dir"], a, a in v.get("globs", ())) @@ -1118,6 +1164,15 @@ def forms(v, a): # once per (directory, path): candidate seen[key] = _forms(v, a) return seen[key] + def named(v, a): # DIR/NAME as DIR/'s forms, unless NAME is a link (or special) + head, _, name = a.rpartition("/") + where = where_of(v, a) if head and name not in ("", ".", "..") and not re.search(r"[*?$`\x01]", name) \ + and a not in v.get("globs", ()) else UNKNOWN + full = resolve(cwd, where) if where != UNKNOWN else "" + if not full or os.path.islink(full): + return forms(v, a) + return [f.replace(NAMED, name) for f in forms(v, head + "/" + NAMED)] + def _forms(v, a): out = [placed(cwd, top, v, a, k, here) for k in (False, True)] out += [placed(cwd, top, v, m, k, here) for m in matches(cwd, v, a) for k in (False, True)] @@ -1128,9 +1183,10 @@ def _forms(v, a): out += [x for x in (checkout_rel(full, top), home_rel(full)) if x and not x.startswith("..")] return out for v in vs: - v["writes"] = list(dict.fromkeys(v["writes"] + [w for p in v.get("copies", ()) for w in copy_writes(cwd, v, p)])) + v["writes"] = list(dict.fromkeys(v["writes"] + [w for p in v.get("copies", ()) for w in copy_writes(cwd, v, p, made)])) v["files"] = list(dict.fromkeys(x for a in v["args"] if not a.startswith("-") for x in forms(v, a))) - v["placed_writes"] = list(dict.fromkeys(x for w in v["writes"] for x in forms(v, w))) + v["placed_writes"] = list(dict.fromkeys(x for w in v["writes"] for x in named(v, w))) + made += [f for f in (resolve(cwd, w) for w in (where_of(v, w) for w in v["writes"]) if w != UNKNOWN) if f] return vs diff --git a/scripts/test/mission-doors.test.py b/scripts/test/mission-doors.test.py index 9a863535..3955bd9a 100644 --- a/scripts/test/mission-doors.test.py +++ b/scripts/test/mission-doors.test.py @@ -1171,6 +1171,89 @@ def swallowing(*a, **k): for tool in ("mcp__slack__slack_send_message_draft", "mcp__gmail__create_draft", "mcp__outlook__create_mail_draft"): assert ids(tool, {}) == [], (tool, ids(tool, {})) ok("MCP: a tool that sends a draft sends; one that writes a draft does not; Drive grant_batch shares") +# 21e) the fifth M9.31 review (of the fourth's fixes). Directories: an exit inside `|| { … }` that may not run +# keeps the group's cd; past `&&` or a compound `|| Y` is reached from the success path too; a second cd that +# may not run keeps where the list stopped; a background list (`cd X && make &`) moves no shell; find -execdir +# runs in each match's directory; env -C moves its command +for cmd, at, want in ( + ("test -d .git || { cd R || exit 1; }; git push origin HEAD", W, "push-default-branch"), + ("[ -f .ok ] || { cd R || exit 1; }; git reset --hard", W, "discard-local-work"), + ("cd R || echo staying && git pull --ff-only || { git stash && git reset --hard origin/main; }", W, "discard-local-work"), + ("cd R || echo staying && git pull --ff-only || ( git reset --hard origin/main )", W, "discard-local-work"), + ("cd R || { echo no main; } && git pull || git reset --hard", W, "discard-local-work"), + ("cd W2 || { cd R && false; } || git push origin HEAD", W, "push-default-branch"), + ("cd R 2>/dev/null || cd W && npm ci && cd W2; git push origin HEAD", W, "push-default-branch"), + ("find /tmp -maxdepth 0 -execdir git push origin HEAD \\;", W, "push-default-branch"), + ("env -C R git push origin HEAD", W, "push-default-branch"), ("env --chdir=R git push origin HEAD", W, "push-default-branch"), + ("cd W && make & git reset --hard", R, "discard-local-work"), + ("cd W && npm run dev & sleep 1; git push origin HEAD", R, "push-default-branch"), + ("cd W && (npm run dev) & sleep 1; git push origin HEAD", R, "push-default-branch")): + cmd = cmd.replace("W2", W2).replace("R ", R + " ").replace("W ", W + " ") + assert want in denied(hook(g, "Bash", {"command": cmd}, cwd=at)), cmd +for cmd, at in (("cd W || { echo no >&2; exit 1; }; git reset --hard", R), ("env -C W git reset --hard", R), + ("cd W2 && npm run dev & sleep 1; git push origin HEAD", W), + ("cd W2 2>/dev/null || cd W && npm ci && cd W2; git push origin HEAD", W)): + cmd = cmd.replace("W2", W2).replace("W ", W + " ") + assert hook(g, "Bash", {"command": cmd}, cwd=at).stdout == "", cmd +ok("directories: a conditional exit, || after &&, a second conditional cd, background lists, find -execdir, env -C") + +# copies: `--` ends the options (no crash); a glob or contents copy also names what the line made before it, and a +# `*` names dotfiles (dotglob); cp -T and rsync -R `SRC/./` copy SRC's entries; a variable directory is a settings +# root only for $HOME (`cp -a . "$tmp"` passes); an unseen name into the ledger's directory forges it +for cmd in ("mv -- build/app.js dist/app.js", "cp -- build/app.js dist/", "cp -r -- build dist", "rsync -a -- src/ dist/", + "ln -sf -- a b", "install -m 644 -- a b"): + assert doors_of(cmd) == [], (cmd, doors_of(cmd)) +assert doors_of("cp -- x .claude/settings.json") == ["change-agent-settings"] +os.makedirs(os.path.join(p.dir, "build"), exist_ok=True) +os.makedirs(os.path.join(p.dir, "dist"), exist_ok=True) +open(os.path.join(p.dir, "build", "app.js"), "w").write("//") +dot = tempfile.mkdtemp() +os.makedirs(os.path.join(dot, ".claude")) +for cmd in ("echo '{}' > build/settings.json && cp build/* .claude/", "echo '{}' > build/settings.json && cp -r build/. .claude", + "echo '{}' > build/settings.json && rsync -a build/ .claude/", "cp E/settings.json dist/ && cp -r dist/. .claude/", + "shopt -s dotglob; cp -r D/* ~/", "GLOBIGNORE=x; cp -r D/* .", "cp -rT D ~", "rsync -aR D/./ ~/", + "cp \"$f\" \"$REPO/.claude/\"", "cp -r \"$SRC\"/. \"$HOME\"/.claude", "rm -rf \"${HOME:?}/.claude\"", + "rm -rf \"${REPO:?}/.claude\"", "cp -a \"$SRC\"/. \"$(pwd)\"", + "rsync -a E/settings.json .claude/settings.json" + " --stderr e" * 8): + cmd = cmd.replace("E/", evil + "/").replace("D/", dot + "/").replace("D ", dot + " ") + assert "change-agent-settings" in doors_of(cmd), (cmd, doors_of(cmd)) +for cmd in ("cp -rT F .aladdin/logs", "rsync -aR F/./ .aladdin/logs/", "cp \"$f\" docs/agents/mission/", + "mv \"$tmp\" docs/agents/mission/", "cp -t docs/agents/mission \"$f\"", "rm -f \"$(pwd)/.aladdin/logs/events.jsonl\"", + "sed -i /door.revoked/d \"$REPO/.aladdin/logs/events.jsonl\"", "rm -rf \"$(pwd)/.aladdin\"", + "rm -f \"${REPO:?}/.aladdin/logs/events.jsonl\"", "cd \"$X\" && sed -i /door.revoked/d P/.aladdin/logs/events.jsonl", + "cd \"$X\" && rm -rf P/.aladdin"): + cmd = cmd.replace("F/", fake + "/").replace("F ", fake + " ").replace("P/", p.dir + "/") + assert "forge-human-record" in doors_of(cmd), (cmd, doors_of(cmd)) +for cmd in ("cd \"$X\" && rm -rf P/.claude", "cd \"$(mktemp -d)\" && cp \"$f\" P/.claude/"): + assert "change-agent-settings" in doors_of(cmd.replace("P/", p.dir + "/")), cmd +tree = tempfile.mkdtemp() +os.makedirs(os.path.join(tree, "plugin")) +open(os.path.join(tree, "Makefile"), "w").write("all:\n") +for cmd in ("echo x > build/v.txt && cp build/* .claude/", "echo x > build/v.txt && cp -r build/. .", "cp -r build/* dist/", + "cp -rT build out", "mv -T build out", "ln -sfT build out", "rsync -aR src/./lib/ dist/", + "tmp=$(mktemp -d) && cp -a . \"$tmp\" && cd \"$tmp\" && npm test", "cp -r . \"$TMPDIR\"", "cp -r .claude \"$tmp\"", + "cp \"$f\" docs/agents/mission/research/", "rm -rf \"$(pwd)/dist\"", "rm -rf \"$OUT/.aladdin\"", "rm -rf \"${OUT:?}/dist\"", + "cd \"$X\" && rm -rf /tmp/nonexistent-x/.aladdin", "cd \"$(mktemp -d)\" && cp -r TREE/. . && make test", + "cd \"$(mktemp -d)\" && rsync -a --exclude .git TREE/ ./"): + cmd = cmd.replace("TREE/", tree + "/") + assert doors_of(cmd) == [], (cmd, doors_of(cmd)) +# a large directory copied whole is placed by its directory once, not name by name +big = os.path.join(p.dir, "big") +os.makedirs(big, exist_ok=True) +for i in range(3000): + open(os.path.join(big, "m%04d.js" % i), "w").close() +for cmd in ("cp -r big/. out/ && cp -r big/. dist/ && cp -r big/. bak/", "cd src || cd build || cd dist; cp -r ../big/. ../out/", + "cp big/* out/"): + t0 = time.time() + assert doors_of(cmd) == [] and time.time() - t0 < 3, (cmd, time.time() - t0) +ok("copies: --, what the line staged, dotglob, cp -T and rsync -R /./, $VAR roots, the ledger's directory, absolute paths") +for tool in ("mcp__gmail__send_mail_draft", "mcp__gmail__send_email_draft", "mcp__outlook__forward_as_draft"): + assert ids(tool, {}) == [], (tool, ids(tool, {})) +for tool in ("mcp__ms365__send-shared-mailbox-draft", "mcp__gmail__send_saved_draft", "mcp__gmail__send_draft"): + assert ids(tool, {}) == ["send-external-message"], (tool, ids(tool, {})) +ok("MCP: a tool that writes a draft (send_mail_draft, forward_as_draft) does not send; one that sends a draft does") +for x in (dot, tree, big, os.path.join(p.dir, "build"), os.path.join(p.dir, "dist")): + shutil.rmtree(x, ignore_errors=True) for x in (evil, fake, lnr): shutil.rmtree(x, ignore_errors=True) From 4d0b1ded71634af2890dfef950f2b29483977779 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 19:12:58 +0000 Subject: [PATCH 12/14] M9.31: sixth review fixes (the reading shell's $PWD, echo && exit, unlisted copy sources, $VAR/.claude, variable last names, draft names) Eight confirmed regressions from a hunt on the fifth review's fixes, each with a test that was red before the change (section 21f of mission-doors.test.py, 48 checks; 45 new assertions red on 2ffdd59): - the shell that reads a word expands its $PWD and $(pwd): words carry a mark that chain() binds, so the text a nested shell reads (sh -c "...", eval, script -c, flock -c, <<<, an unquoted heredoc, echo into a pipe) keeps the outer directory, and env -C, git -C or find -execdir move the command but not its already-expanded words (view["shell"]) - `echo ... && exit 1` and `return 1 || exit 1` always exit (printf only without conversions, and no write redirect) - a contents or glob copy of a directory that is missing now is DEST/*, and of one that is empty now adds DEST/? to the names the line wrote: tar, curl -o and unzip fill it unseen - a single-source copy onto "$VAR/.claude" may become that directory - a variable last name in a guarded directory (.aladdin/logs/$F, .claude/${F:?}) is any name there - globbed() lists a glob's directory once for dot entries instead of lstat-ing every name (95,000 files: 1.1 s to 0.4 s) - the draft-writer exclusion needs a separator before message, mail, email or as (send_gmail_draft sends again) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- CHANGELOG.md | 2 +- docs/ROADMAP.md | 4 +- plugin/mission/doors.json | 15 ++- plugin/mission/doors.py | 178 +++++++++++++++++++---------- scripts/test/mission-doors.test.py | 67 +++++++++++ 5 files changed, 200 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25a19638..e9c31970 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ tag in lockstep. Tag releases as `vMAJOR.MINOR.PATCH`. ### Added - Plan approval is a gate (ROADMAP M0.9): `gate-dispatch` starts no execution role (RED, SCAFFOLD, GREEN, structural review, final gate) of a sprint until the human approved that sprint's plan as it is now. `events authority --sprint N` exits 3 with the line to type, `approve SN ` (reviewed, the code naming the sha256 of the sprint's plan files, its intake and standards included) or `autonomous SN ` (autonomy for that sprint, re-plans included, the code naming the authority record); the task contract's `STORY_DIR` and task must be a live story and task of that plan's frozen `ids.json`; the line must be among the human's captured prompts and typed after the sprint's last decision (M9.17's mechanism). A changed plan, a re-plan in reviewed mode, a roll-back to an old line, another sprint's line, a negated line and a hand-edited or emitted record all block (`plan.approved` and `human.approval` cannot be `events emit`ted); an approved autonomous mission grants its own sprints autonomy with no line. `events approval SPRINT|TASK_ID` is the check. Codex's `spawn_agent` (`agent_type`, `message`) and OpenCode's `task` now reach `gate-dispatch` too, and `claude -p`, `codex exec` and `opencode run` from a tool call are blocked in every aloop project, not only where a mission is set. `e2e-live.sh` pre-approves with an `autonomous S1 ` line in its prompt. Reason: the north star allows no human help beyond approval, and the approval was a playbook rule a supervisor could skip. A 4-lens adversarial review found 9 defects: 7 fixed with a check red on the first head (cross-sprint or unplanned `STORY_DIR`, a retyped line kept stale, mission autonomy over a non-mission sprint, `transcripts prune` dropping the prompts grants cite, `codex plugin add` refused, a code past column 200, a crash on a malformed run-log line), 2 in the playbook (intake reuses a mission's handed-off intent; a reviewed re-plan waits for the human's new line). `transcripts prune` now keeps the human's prompts. Test: `plan-approval.test.py` (20 checks, red on master), plus the host suites. The approved plan's files include the sprint's `intake.md` and `standards.md`, and a grant pins them file by file: GREEN's check for an unplanned dependency reads only the text the human's grant pins, the plan they approved or, under their autonomy grant, the plan as the planner's gate last passed it (`gate-stage2-complete` records `plan.gated` on the planner's stop), as it was then, so text added later names nothing and a re-plan does not undo an approved dependency. - Core agent principles and the one-way / two-way door rule (ROADMAP §4.12, M9.30): seven core principles from Amazon's Leadership Principles, the door rule first, go to every aloop agent at session start (`aloop mission principles show --core`). A two-way door (a branch, a local edit, a test, a draft PR) is decided, not asked. A one-way door (`aloop mission doors list`: a push or merge to the default branch, a force-push, a release, a deploy, a message sent outside, a secret, dropped data, …) is stopped by a PreToolUse gate with `ASK[door] ` until the human records an authorization in their own terminal (`aloop mission doors authorize ID --by NAME --reason TEXT [--until DATE] [--once DIGEST]`, a hash-chained ledger row); a wall (switching a gate off, forging a human record) is never crossed. While a run is live, a question to the human must name a door the gate stopped and nobody authorized. Reason: Adeel's request that agents decide two-way doors themselves and ask only at a one-way door not already authorized. Test: `mission-doors.test.py`. Hardened by an adversarial review (15 findings, each a test): the gate reads Bash as the shell does (comments, line continuations, heredoc bodies, `xargs`, `find -exec`, `echo … | sh`), a branch condition reads the directory the command acts on (`git -C`, `cd`), settings are guarded wherever they live, dry runs and `command -v` are two-way, the newest decision by time counts and a revoke holds on every branch, a once-grant is crossed once under a lock, a broken ledger is named, and `doors authorize` refuses an agent's shell. -- The door rule on every host and role (ROADMAP §4.12, M9.31): every dispatched role gets the core principles (`aloop mission principles hook` at SubagentStart, Codex agents' instructions, OpenCode's system prompt, each dsh role persona), and the door gate runs on Codex, OpenCode and dsh with each host's question tool mapped. In the OpenCode and dsh plugins it runs last, after every other gate admitted the call, and fails closed after 15 s. A bound worker's command is judged where it runs (its `workdir`), an MCP tool by its own arguments (`mcp____` on OpenCode too), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Hardened by five reviews (88 findings, each a test that failed first): a stalled decision can no longer fall through to an allow, `apply_patch` is read with any header spacing (a U+FEFF too) and through the shell, a bare redirect or a group's redirect reaches the commands inside, a `cd` that may not run leaves the directory unknown, a symlink then `..` is read as the kernel walks it, replacing a settings file with `cp`, `mv`, `ln`, `install`, `rsync` (into a settings directory too), `rm`, `sed -i` or `perl -i`, or removing a directory that holds one, is the settings door, and an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`) crosses the same door as the service's own. A `cd` that may not run leaves each later command in either directory, a copy is placed where it runs (so backing up `.claude` passes), and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Drive access grants are doors too. After `cd A || cd B` a line may be in A, B or where it started, a glob or contents copy names the files it copies, a name the gate cannot see is no settings change at the project root, a wrapped `cd` (`nohup cd`) or a background list (`cd X && npm run dev &`) moves no shell, and a copy names what the line staged before it. Tests: `mission-doors.test.py`, `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`. +- The door rule on every host and role (ROADMAP §4.12, M9.31): every dispatched role gets the core principles (`aloop mission principles hook` at SubagentStart, Codex agents' instructions, OpenCode's system prompt, each dsh role persona), and the door gate runs on Codex, OpenCode and dsh with each host's question tool mapped. In the OpenCode and dsh plugins it runs last, after every other gate admitted the call, and fails closed after 15 s. A bound worker's command is judged where it runs (its `workdir`), an MCP tool by its own arguments (`mcp____` on OpenCode too), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Hardened by six reviews (96 findings, each a test that failed first): a stalled decision can no longer fall through to an allow, `apply_patch` is read with any header spacing (a U+FEFF too) and through the shell, a bare redirect or a group's redirect reaches the commands inside, a `cd` that may not run leaves the directory unknown, a symlink then `..` is read as the kernel walks it, replacing a settings file with `cp`, `mv`, `ln`, `install`, `rsync` (into a settings directory too), `rm`, `sed -i` or `perl -i`, or removing a directory that holds one, is the settings door, and an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`) crosses the same door as the service's own. A `cd` that may not run leaves each later command in either directory, a copy is placed where it runs (so backing up `.claude` passes), and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Drive access grants are doors too. After `cd A || cd B` a line may be in A, B or where it started, a glob or contents copy names the files it copies, a name the gate cannot see is no settings change at the project root, a wrapped `cd` (`nohup cd`) or a background list (`cd X && npm run dev &`) moves no shell, and a copy names what the line staged before it. The shell's own `$PWD` stays where the shell expanded it (in a nested shell's text, under `env -C` or `find -execdir`), and a source directory the gate cannot list, a copy that becomes `"$VAR/.claude"` and a variable last name in a guarded directory are read as any name. Tests: `mission-doors.test.py`, `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`. - DeepSeek Harness (dsh) host (ROADMAP §4.9): `aloop install dsh` (alias `deepseek`; `--project P` or `--global`) renders a `dsh --profile aloop` profile in `$DSH_HOME` and installs the skills. Its native plugin (`plugin/hosts/dsh/aloop.mjs`) runs the same gates as Claude Code on dsh's own events: a deny before a tool runs, a block after an invalid artifact write, and a stop gate that steers a role back up to 3 times before the supervisor is told it is BLOCKED (or STUCK, released unverified). Roles are agent presets on two subagent tools, one per model route (`deepseek-v4-pro` for planning, `deepseek-flash` for work by default; `MODEL_PLANNING_DSH` / `MODEL_WORKER_DSH` override). A writer's `aloop bind` moves its working directory into its worktree, and `host-adapter` confines its commands, edits and `working_directory` there. Usage is recorded Claude-shaped per role, so budgets, the ledger and `run-summary` work unchanged. `make e2e-live HOST=dsh` drives it live and exits 2 with the fix when no `DEEPSEEK_API_KEY` is set. Reason: DeepSeek's own harness is the cheapest route to a strong model. The profile records the project it serves (or `--global`), so another project's `aloop-init` never re-renders it, and gates get the worker shell's environment (no API key). The mission layer's six roles run on dsh too: presets on their route group's subagent tool, `gate-mission-scope` on every write and command, and `gate-mission-artifact` at each role's start and stop; the mission skill installs as `aloop-mission`. A second review (3 lenses; 11 confirmed findings, each fixed with a test) closed: a working directory that vanished (dsh's tools move it back to the main tree, so the plugin judges `ensure()`'s directory and `host-adapter` refuses to run from a missing one), gate switches from the project's `.env` (dsh loads it; `SKIP_HOOKS`, `GATE_RUN_TESTS`, `REPO_DIR`, `AGENTIC_*` from it never reach a gate), an interrupted Stop that dropped the sprint-close notice, and the shared profile: its owner is the main tree, an older unrecorded profile is claimed only by the project that installed it, and a project it does not serve is told so by `install status`, `aloop-init` and the session itself. Tests: `dsh-plugin.test.mjs` (46 checks, emulated), `dsh-runtime.test.mjs` (50 checks on the real pinned dsh 0.2.1-rc.2 with a scripted model; the CI hosts job installs it) and `dsh-install.test.py` (10). - dsh follow-ups (ROADMAP §4.9). One `aloop install dsh` serves every project: the plugin routes each role's requests through dsh's `agent/request` to its own project's model (`aloop models resolve `), so gate-dispatch no longer refuses another project's roles until a re-install, and a role whose model cannot be resolved is refused (fail closed). Roles are no longer offered `send_message`, so dsh adds no "send your result" guidance and no call is spent on a deny. dsh compaction is recorded as `context.compacted` and the handoff comes back after it; the capability matrix marks dsh compaction and live messaging as advertised, proven on the real runtime. Two mission roles (one per subagent tool) run through both of their gates on the real runtime. `run-summary --host dsh` reports a dollar cost: each logged call is priced from a dated, sourced DeepSeek table (`plugin/bin/_prices.py`; cache hit, cache miss, output, peak or off-peak), with `cost_by_model` in `summary.json`; an unpriced model id keeps its tokens and leaves the cost unmeasured, never guessed. - Brand kit and repo front page: `docs/brand/` holds the mark (an **a** whose bowl is a red-to-green loop passing a gold gate, with a 16 px cut), lockups, light and dark README banners, favicons, app icons, the site's OG card and the GitHub social preview, all generated by `docs/brand/gen_brand.py` from one palette with WCAG checks (rules in `docs/brand/README.md`). The README leads with the mission layer, the agent principles and the door rule, the supported hosts and the measured demo runs; the site carries the favicon and Open Graph/Twitter card metadata; `SECURITY.md` and bug/feature issue forms are new. diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index d15b3936..89e7eb5a 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -484,7 +484,7 @@ Each task flips only its own row here and in TRACKING.md. | M9.28 | `tick` drives the goal commands (M9.11): a goal counts only with its `goal.derived`/`goal.approved` record and closes by `goal.met`; handouts name `goal derive`, `options` (spoon-fed), `intents`, `next` and `close`; an eval narrowed to a closed goal asks for a whole-mission eval. Reason: found when M9.11 landed, `tick` read goal files and their status, so a goal nobody derived steered the loop and a met goal was never closed | M9.8, M9.11 | `plugin/mission/tick.py`, `plugin/skills/mission/SKILL.md`, `mission-tick.test.py` | yes | done: `mission-tick.test.py` (29 checks; a real goal run on the real clock: derive → review → intents → next and a sprint per intent → narrowed eval → close → whole-mission eval → `RESEARCH`) | | M9.29 | A goal that crosses a hard line stops being the active goal (`evaluate.active_goal` skips it, or a recorded retirement). Reason: found wiring M9.28, `evaluate` keeps narrowing to a hard-lined goal and `goal derive` refuses a narrowed eval, so the mission deadlocks (`tick` hands it to the human meanwhile) | M9.10, M9.11 | `plugin/mission/{evaluate,goal}.py`, their tests | yes | done: `evaluate.active_goal` skips a goal an ethics hard line retired (it never closes), so the next eval covers the whole mission; `goal derive` from an eval narrowed to a retired goal says to evaluate again; `tick` hands out EVALUATE for such an eval, then DERIVE_GOAL, which takes the next gap (a hard line holds the retired goal's own gaps); when every open gap is held it is BLOCKED for the human (a change of the mission), not handed to a derive that would refuse. `mission-goal.test.py` (section 11), `mission-tick.test.py` (section 27). Call: retirement is derived from the hard line itself, not a new record, so it holds on a fresh clone with the ledger | | M9.30 | Core agent principles and the one-way / two-way door rule, enforced: seven core principles from Amazon's Leadership Principles (the door rule first) for every agent, injected at session start; a door catalogue, the human's standing or once authorization in the ledger, and a PreToolUse gate that stops an unauthorized one-way door (ASK) and a wall (BLOCK) and lets two-way doors pass; while a run is live a question must name a door the gate stopped. Reason: Adeel, 2026-10-20: "If it is two-way, then the agent should take the decision directly without bothering the user. But if it is one-way door, then it can bother user if user already have not specified to cross that door." | M9.0, M9.7 | `plugin/mission/doors.{py,json}`, `principles.{md,py}` (`## Core`), `plugin/bin/session-start`, `hooks.json` (one line), core: `_events.py` / `_mission.py` (door events), `events` (`GATE_OWNED`), `scripts/test/mission-doors.test.py` | after M9.7 | done: `mission doors list / classify / authorize / revoke / check / hook`, `mission principles show --core`; `mission-doors.test.py` (21 checks), `mission-principles.test.py` (+1 check). Calls: anything the catalogue does not list is two-way (a finite list cannot claim full enforcement, §7); Bash is matched by program and arguments after reading the line as the shell does, with the scope gate's reader (`_shellread.py`, M9.38: quotes, comments, line continuations, `$(…)`, a heredoc's body as data unless a shell or python runs it) and unwrapping `sudo`, `env`, `sh -c`, `eval`, `xargs`, `find -exec` and `echo … | sh`, so text that only mentions a door (`grep`, a commit message, a file written by a heredoc) stays two-way; a `when` condition (on the default branch, the main checkout) reads the directory the command acts on (`git -C DIR`, `cd DIR &&`), and one the gate cannot resolve counts; grants are standing (until revoked or `--until`), once (one action's digest, 24 h, used up by its first crossing, recorded in the ledger), never (a wall: switching a gate off, forging a human record; refused even for the human's `authorize`) and command (question-only doors answered by their own command); authorizations are hash-chained human ledger rows (an edited row, `events emit` or a stream-only event opens nothing; `HUMAN_EVENTS` are now gate-owned, which also closes a forged `baseline.vetoed`); a question is checked only while a run is live (an approved autonomous mission or an autonomous run, or a sprint whose latest plan is approved, for the session holding its lock), so intake, planning and the plan's own approval are asked; the core block is kept out of the goal blocks, so editing it never stales a goal's stamp; the hook fails closed (exit 2) and is silent outside an aloop project; measured: median 81 ms per two-way call, 102 ms per stopped one-way call (30 calls each, in parallel with the other `*` hooks). Calls from its adversarial review (15 confirmed findings over 3 lenses, each now a test that failed before its fix): the newest decision about a door is the newest by time (a union merge that puts an older authorization last reopens nothing), and the run log's revokes and crossings close on any branch of the checkout; a once-grant is checked and crossed under a lock (`.aladdin/state/doors.lock`); a broken ledger is named in the ASK, fails `doors check` and blocks `authorize` with the restore line; settings and the ledger are matched by path suffix (`~/.claude`, a worktree's own); a dry run, a help page and `command -v` are two-way, `--dry-run=none` is not; `git -c core.hooksPath=` is a wall and `git config core.hooksPath VALUE` a one-way door; arguments `xargs` adds from stdin may be any flag of a one-way door; `doors authorize` refuses an agent's shell (`CLAUDECODE` set: the human authorizes in their own terminal); Slack's real send tools (`slack_post_message`, `slack_reply_to_thread`) are one-way; a hard ethics line only the run log holds (recorded before the ledger did) still binds. Calls from its second review (26 confirmed findings over parser, catalogue and state lenses, each now a test that failed before its fix): the gate reads Bash with `_shellread.py` (M9.38), which now keeps each heredoc's body, the operator that ends a command and heredoc bodies inside `$(…)`, so touching operators (`); git push`), keywords, groups, function bodies, here-strings, IO-number redirects (`2>&1`) and quoted operators (`find \( … \) -exec`) hide nothing, a quote in `"$(cat <<'EOF' … EOF)"` is data, stdin (a heredoc, a here-string, `echo|printf|cat … |`) reaches only the command it feeds (a shell with options or `-s`, a python with `-`), and `pushd`/`popd`, `sh -c`, `eval` and a fed shell keep the directory; nesting deeper than 8 is unreadable and a read past 8 s fails closed (exit 2, under the hook's 15 s); python is a forge only when it calls a writer, `npm|pnpm|yarn -g` a system change only with an installing verb, the ledger only at `docs/agents/mission/ledger.jsonl`, `xargs … doors` only when stdin can be the subcommand (`-I R` replaces R), a secret command's dry run or help page is two-way, `git push origin HEAD|@` on the default branch is one-way, and erasing or editing the run log (`.aladdin/logs/events.jsonl`, whose revokes and crossings close doors) is a wall; a ledger row is written before its stream event and `ledger_problems` reads the stream first, so no gate sees another writer's half-written record as a lost row; a record is dated no earlier than any ledger row, so the human's fresh decision is the newest even after a merged future-dated row; a run stops being live when its plan's sprint closes (`sprints.json`) or a new intent arrives; a malformed run-log line never crashes the ethics check | -| M9.31 | Door rule on every host and role: the core block in SubagentStart for dispatched roles, Codex `developer_instructions`, OpenCode agents and the dsh profile; the hook in Codex PreToolUse, the OpenCode plugin (`aloop.js`) and the dsh plugin (`aloop.mjs`, whose PreToolUse list mirrors `hooks.json` by hand and so lacks `mission doors hook`), with each host's question tool mapped. Also measure whether a human's own `!` shell command reaches PreToolUse (it should not be gated: the human crossing a door is the authorization). Reason: M9.30 wires Claude Code's main session only | M9.30, M9.9 | `plugin/bin/host-adapter`, `install`, `aloop.js`, `aloop.mjs`, `_host.py` | after M9.9 | done: the core block reaches every role on every host (`mission principles hook`, the third SubagentStart command; Codex agents' `developer_instructions`; OpenCode's system transform for a sub-agent and a resumed root; each dsh role persona, written by `install`). The door gate runs on Codex through `hooks.json`, and last in the OpenCode and dsh plugins (after every other gate admitted the call, so only a call that runs uses up a once-authorization; read-only tools skip it; 15 s, fail closed). Each host's question tool is the gate's (`AskUserQuestion`, Codex `request_user_input`, OpenCode `question`, dsh `ask_user_question`). A bound worker's command is read where it runs (`tool_input.workdir`, a relative one under the project), a dsh MCP tool's input is its own arguments (its once digest is the one any host prints), and an OpenCode MCP tool is `mcp____` (from `client.mcp.status()`, the longest server first). One deadline covers the decision (`Deadline`, which no step's `except Exception` can swallow into an allow), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Five reviews (12, 7, 34, 18 and 17 findings, each a test that failed first) added: `apply_patch` read as the hosts parse it (any header spacing or indent, a U+FEFF around the path cut as OpenCode's `trim()` cuts it, and through the shell: Codex's heredoc and argument forms, `applypatch`, after a `cd`, from a pipe; the header reader `_patch.py` is shared with host-adapter and both scope gates, but only the door gate reads the shell form, M9.61); a bare redirect, and a group's or compound's own redirect or heredoc, reach the commands inside; a `cd` that may not run leaves the directory unknown; a path through a symlink then `..` read as the kernel walks it (`git -C`, `cd -P`, a dsh `workdir`); settings replaced, moved, linked or removed by `cp`/`mv`/`ln`/`install`/`rsync` (into a settings directory too, by `-t` or a trailing `DIR/`), `rm`/`mv` of a directory that holds them, a glob that names them, `sed -i` (and `--in-pl`), `perl -i`; `opencode.json` a door only where OpenCode reads it (a nested checkout's root, `~/.config/opencode`, its `config.json`); an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`, `mcp__google-drive__share_file`) crosses the same door as the service's own server, while a service word inside another tool's name (`search_slack_posts`, `get_post_history`) is none. The third review: a `cd` that may not run leaves each later command in either directory (one view per directory, not an unknown one), `cd -`, a bare `pushd`, `popd -n`, `cd X &`, an eval's `cd`, `~+` and `GIT_DIR`/`GIT_WORK_TREE` are read as the shell reads them, and a pipe into a `{ }` group or a compound reaches each command inside; a copy is placed where it runs (DEST/NAME only into an existing directory, so a backup of `.claude` or a file copied into it passes; `-t` with `{}` or xargs' arguments, `--parents`, `-R`, `--remove-source-files`, a link to a settings file, a home directory's contents, GNU option abbreviations); a glob is expanded only where the shell expands it, through links, and never loses a config-like name; another checkout's run home is not this project's; and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Google Workspace's Drive grants cross their doors. The fourth: a copy option that takes no value (`rsync --checksum`, `install --strip`) is no prefix of one that takes one, and only an operand right after an unknown option may be its value, so only then does the destination widen (to at most 8 operands: 600 files and a trailing `-f` stay fast); a glob source names the files it matches now, a contents copy (`SRC/.`, rsync's `SRC/`) its entries, and a name the gate cannot see (`"$f"`, `{}`) is `DEST/?`: no settings change at the project's, a checkout's or the home root, but one into a settings or run-record directory; `ln -n` over a linked settings directory replaces it; `./.claude` after an unknown `cd` and `"$REPO/.claude"` are read by name; after `cd A || cd B` the line is in A, B or where it started; OLDPWD follows a `cd` that may not run and survives a subshell; `cd X || { …; exit 1; }` and `&& ( … ) &&` keep the line in X (the shell reader carries the operator after a subshell or compound, `Mark.end`), and a group after `||` starts where the left side failed; a wrapped `cd` (`nohup`, `env`, `flock`) moves no shell; candidate directories pass into nested reads instead of multiplying them, and a path is placed once per directory; a tool that sends a draft sends (`send-shared-mailbox-draft`) and Drive's `grant_batch` shares. The fifth, a regression hunt on the fourth's fixes: `cp --` no longer crashes the gate; an exit inside `|| { … }` counts only when it always runs; past `&&` or a compound, `|| Y` is reached from where the list succeeded too; a second `cd` that may not run keeps where the list stopped; a background list (`cd X && npm run dev &`) moves no shell; `find -execdir` runs in an unknown directory and `env -C DIR` in DIR; a glob or contents copy also names what the line staged before it and the dotfiles a `*` may name (dotglob); `cp -T` and `rsync -R SRC/./` copy SRC's entries; an absolute path is placed after an unknown `cd`; `$(pwd)` is `$PWD`, `${X:?}` is `$X`, and a run record under a variable directory is that record; a variable directory is a settings root only for `$HOME` (so `cp -a . "$tmp"` passes); an unseen name copied into `docs/agents/mission/` forges the ledger; a large directory's copy is placed by its directory once; and a tool that writes a draft (`send_mail_draft`, `forward_as_draft`) does not send. Tests: `mission-doors.test.py` (45 checks), `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`, `hook-matchers.test.py`, `mission-principles.test.py`, `dsh-install.test.py`. Calls: the catalogue keeps its generic `_` alternatives for an OpenCode that cannot list its servers (an extra question beats a silent one-way door). Open: M9.56–M9.65 (the `!` measurement is M9.58) | +| M9.31 | Door rule on every host and role: the core block in SubagentStart for dispatched roles, Codex `developer_instructions`, OpenCode agents and the dsh profile; the hook in Codex PreToolUse, the OpenCode plugin (`aloop.js`) and the dsh plugin (`aloop.mjs`, whose PreToolUse list mirrors `hooks.json` by hand and so lacks `mission doors hook`), with each host's question tool mapped. Also measure whether a human's own `!` shell command reaches PreToolUse (it should not be gated: the human crossing a door is the authorization). Reason: M9.30 wires Claude Code's main session only | M9.30, M9.9 | `plugin/bin/host-adapter`, `install`, `aloop.js`, `aloop.mjs`, `_host.py` | after M9.9 | done: the core block reaches every role on every host (`mission principles hook`, the third SubagentStart command; Codex agents' `developer_instructions`; OpenCode's system transform for a sub-agent and a resumed root; each dsh role persona, written by `install`). The door gate runs on Codex through `hooks.json`, and last in the OpenCode and dsh plugins (after every other gate admitted the call, so only a call that runs uses up a once-authorization; read-only tools skip it; 15 s, fail closed). Each host's question tool is the gate's (`AskUserQuestion`, Codex `request_user_input`, OpenCode `question`, dsh `ask_user_question`). A bound worker's command is read where it runs (`tool_input.workdir`, a relative one under the project), a dsh MCP tool's input is its own arguments (its once digest is the one any host prints), and an OpenCode MCP tool is `mcp____` (from `client.mcp.status()`, the longest server first). One deadline covers the decision (`Deadline`, which no step's `except Exception` can swallow into an allow), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Six reviews (12, 7, 34, 18, 17 and 8 findings, each a test that failed first) added: `apply_patch` read as the hosts parse it (any header spacing or indent, a U+FEFF around the path cut as OpenCode's `trim()` cuts it, and through the shell: Codex's heredoc and argument forms, `applypatch`, after a `cd`, from a pipe; the header reader `_patch.py` is shared with host-adapter and both scope gates, but only the door gate reads the shell form, M9.61); a bare redirect, and a group's or compound's own redirect or heredoc, reach the commands inside; a `cd` that may not run leaves the directory unknown; a path through a symlink then `..` read as the kernel walks it (`git -C`, `cd -P`, a dsh `workdir`); settings replaced, moved, linked or removed by `cp`/`mv`/`ln`/`install`/`rsync` (into a settings directory too, by `-t` or a trailing `DIR/`), `rm`/`mv` of a directory that holds them, a glob that names them, `sed -i` (and `--in-pl`), `perl -i`; `opencode.json` a door only where OpenCode reads it (a nested checkout's root, `~/.config/opencode`, its `config.json`); an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`, `mcp__google-drive__share_file`) crosses the same door as the service's own server, while a service word inside another tool's name (`search_slack_posts`, `get_post_history`) is none. The third review: a `cd` that may not run leaves each later command in either directory (one view per directory, not an unknown one), `cd -`, a bare `pushd`, `popd -n`, `cd X &`, an eval's `cd`, `~+` and `GIT_DIR`/`GIT_WORK_TREE` are read as the shell reads them, and a pipe into a `{ }` group or a compound reaches each command inside; a copy is placed where it runs (DEST/NAME only into an existing directory, so a backup of `.claude` or a file copied into it passes; `-t` with `{}` or xargs' arguments, `--parents`, `-R`, `--remove-source-files`, a link to a settings file, a home directory's contents, GNU option abbreviations); a glob is expanded only where the shell expands it, through links, and never loses a config-like name; another checkout's run home is not this project's; and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Google Workspace's Drive grants cross their doors. The fourth: a copy option that takes no value (`rsync --checksum`, `install --strip`) is no prefix of one that takes one, and only an operand right after an unknown option may be its value, so only then does the destination widen (to at most 8 operands: 600 files and a trailing `-f` stay fast); a glob source names the files it matches now, a contents copy (`SRC/.`, rsync's `SRC/`) its entries, and a name the gate cannot see (`"$f"`, `{}`) is `DEST/?`: no settings change at the project's, a checkout's or the home root, but one into a settings or run-record directory; `ln -n` over a linked settings directory replaces it; `./.claude` after an unknown `cd` and `"$REPO/.claude"` are read by name; after `cd A || cd B` the line is in A, B or where it started; OLDPWD follows a `cd` that may not run and survives a subshell; `cd X || { …; exit 1; }` and `&& ( … ) &&` keep the line in X (the shell reader carries the operator after a subshell or compound, `Mark.end`), and a group after `||` starts where the left side failed; a wrapped `cd` (`nohup`, `env`, `flock`) moves no shell; candidate directories pass into nested reads instead of multiplying them, and a path is placed once per directory; a tool that sends a draft sends (`send-shared-mailbox-draft`) and Drive's `grant_batch` shares. The fifth, a regression hunt on the fourth's fixes: `cp --` no longer crashes the gate; an exit inside `|| { … }` counts only when it always runs; past `&&` or a compound, `|| Y` is reached from where the list succeeded too; a second `cd` that may not run keeps where the list stopped; a background list (`cd X && npm run dev &`) moves no shell; `find -execdir` runs in an unknown directory and `env -C DIR` in DIR; a glob or contents copy also names what the line staged before it and the dotfiles a `*` may name (dotglob); `cp -T` and `rsync -R SRC/./` copy SRC's entries; an absolute path is placed after an unknown `cd`; `$(pwd)` is `$PWD`, `${X:?}` is `$X`, and a run record under a variable directory is that record; a variable directory is a settings root only for `$HOME` (so `cp -a . "$tmp"` passes); an unseen name copied into `docs/agents/mission/` forges the ledger; a large directory's copy is placed by its directory once; and a tool that writes a draft (`send_mail_draft`, `forward_as_draft`) does not send. The sixth, a regression hunt on the fifth's fixes: the shell that reads a word expands its `$PWD` and `$(pwd)`, so the text a nested shell reads (`sh -c "…"`, `eval`, `<<<`, an unquoted heredoc, a pipe) keeps the outer directory, and `env -C`, `git -C` or `find -execdir` move the command but not its words; `echo … && exit` and `return 1 || exit 1` always exit; a source directory the gate cannot list now (missing, or empty) holds more than what the line visibly wrote into it (tar, `curl -o` and unzip are unseen); a copy onto `"$VAR/.claude"` may become that directory; a variable last name in a guarded directory (`.aladdin/logs/$F`) is any name; a glob over a large directory lists it once (95,000 files: 1.1 s to 0.4 s); and a draft writer's name ends in message, mail, email or as, then draft, after a separator (so `send_gmail_draft` sends). Tests: `mission-doors.test.py` (48 checks), `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`, `hook-matchers.test.py`, `mission-principles.test.py`, `dsh-install.test.py`. Calls: the catalogue keeps its generic `_` alternatives for an OpenCode that cannot list its servers (an extra question beats a silent one-way door). Open: M9.56–M9.65 (the `!` measurement is M9.58) | | M9.32 | Intent questions name their door: `open_questions[].door`; `intent check` blocks an assume/default on a one-way door and treats an `ask` on a door the human authorized as answered. Reason: the plan-time half of the door rule | M9.30, M9.11 | `plugin/bin/intent`, its test | yes | open | | M9.33 | Remove the model-facing `SKIP_HOOKS=1` hints (`gate-supervisor-scope`, `_failclosed.sh`, `SKILL.md`). Reason: they hand the agent the bypass-gate wall's own key | M9.30 | those files | yes | open | | M9.34 | A git pre-push backstop from `aloop init` for pushes to the default branch and force pushes. Reason: holds when Bash matching is evaded (a script, an alias) | M9.30 | `plugin/bin/aloop-init`, its test | yes | open | @@ -516,7 +516,7 @@ Each task flips only its own row here and in TRACKING.md. | M9.62 | In the OpenCode plugin only the door gate has a fail-closed timeout: a hung host-adapter, scope or budget gate waits 10 minutes and its timeout reads as an allow (no exit status, no deny). Give each gate the hook's timeout and fail closed. Reason: the M9.31 review | M9.31 | `plugin/hosts/opencode/aloop.js`, its test | yes | open | | M9.63 | An OpenCode that cannot list its MCP servers (`client.mcp.status()` fails) hands the gates raw `_` names, which the catalogue's generic alternatives read with false positives (`drive_get_share` asks). Read the server names from the config too. Reason: the M9.31 review | M9.31 | `plugin/hosts/opencode/aloop.js` | yes | open | | M9.64 | A path the shell computes inside a patch or a write target: an unquoted heredoc's `$HOME`/`$(…)` in an `apply_patch` header, or `> "$D/settings.json"`, is judged as written (M9.47's class). Expand what the line assigns before it, and ask for a computed path under a settings directory. Reason: the second M9.31 review | M9.47 | `plugin/mission/doors.py`, its test | yes | open | -| M9.65 | What the third M9.31 review left in the door gate's reader: a heredoc or redirect on a `while`/`until` loop reaches its body but not its condition (`while bash; do …; done <"): - stdin = w.body if op in ("<<", "<<-") else render(w.parts) if op == "<<<" else None + stdin = (w.body if w.quoted or w.body is None else OUTER_PWD.sub(lambda _: outer(here), w.body)) \ + if op in ("<<", "<<-") else nested(render(w.parts, True), here) if op == "<<<" else None inherited = False if own is not None: # the group's own redirects: its commands write and read them inner = out[own:] @@ -431,7 +466,10 @@ def views(cmd, depth=0, cur=""): maybe, failed = None, None if c.end == "&" and prev in ("&&", "||", "|", "|&"): cur, old, dirs[:] = base[0], base[1], base[2] # `cd X && make &`: the whole list is a background job - last = prog if prev not in ("&&", "||") and not c.pipe and c.end != "&" else None # an exit that always runs + sure = prog in ("echo", "true", ":") or prog == "printf" and vs[0]["args"][:1] and "%" not in \ + vs[0]["args"][0].replace("%%", "") # cannot fail (printf's conversions can) + always = prev not in ("&&", "||") or prev == "||" and last in ("exit", "return") or prev == "&&" and last == "ok" + last = ("ok" if sure and not vs[0]["writes"] else prog) if always and not c.pipe and c.end != "&" else None prev = c.end return out @@ -449,7 +487,7 @@ def stdout(vs, stdin): if not vs: return None v = vs[-1] - args = [a for a in v["args"] if a != STDIN] + args = [a for a in v.get("said", v["args"]) if a != STDIN] if v["prog"] == "echo": esc = False while args and re.fullmatch(r"-[neE]+", args[0]): @@ -679,22 +717,22 @@ def copy_writes(cwd, v, plan, made=()): return out base = dest.rstrip("/") or "/" - def entries(f): # a directory's names now and the line's own - try: - names = os.listdir(f) - except OSError: - names = None - made_here = [m[len(f.rstrip("/")) + 1:].split("/")[0] for m in made if m.startswith(f.rstrip("/") + "/")] - return None if names is None and not made_here else sorted(set(names or []) | set(made_here)) + def mine(f): # the names the line wrote into f before this command + return [m[len(f.rstrip("/")) + 1:].split("/")[0] for m in made if m.startswith(f.rstrip("/") + "/")] def contents(s): # the entries of SRC/. (rsync SRC/), as copied w = where_of(v, s) f = resolve(cwd, w) if w != UNKNOWN else "" - names = entries(f) if f else None - if names is None or len(names) > GLOB_SCAN: + try: + now = os.listdir(f) if f else None + except OSError: + now = None # missing now: what fills it (tar, curl -o) is unseen + if now is None or len(now) > GLOB_SCAN: return [base + "/*"] + names = sorted(set(now) | set(mine(f))) keep = sorted(n for k, n in enumerate(names) if k < GLOB_MAX or KEEP.search(n)) - return [base + "/" + n for n in keep[:4 * GLOB_MAX]] + ([base + "/*"] if len(keep) > 4 * GLOB_MAX else []) + return [base + "/" + n for n in keep[:4 * GLOB_MAX]] + ([base + "/*"] if len(keep) > 4 * GLOB_MAX else []) + ( + [base + "/?"] if not now and names else []) # empty now: the line filled it, not only by what it shows def a_dir(s): # rsync copies a directory into DEST, as DEST/NAME w = where_of(v, s) @@ -709,15 +747,20 @@ def a_dir(s): # rsync copies a directo return out + [dest] # ln -sfn X LINK: the link itself is replaced isdir = os.path.isdir(full) if full else None # None: the gate cannot tell - def globbed(s): # what the shell's glob names: now, made, dot entries - names = [os.path.basename(m.rstrip("/")) for m in matches(cwd, v, s)] + def globbed(s): # what the shell's glob names (now, made, dot entries), + now = [os.path.basename(m.rstrip("/")) for m in matches(cwd, v, s)] # and whether it names any now head, last = os.path.split(s) w = where_of(v, head or ".") if not re.search(r"[*?\[]", head) else UNKNOWN f = resolve(cwd, w) if w != UNKNOWN else "" + names = list(now) + if f: + names += [n for n in mine(f) if fnmatch.fnmatch(n, last)] if f and re.match(r"[*?\[]", last): # dotglob or GLOBIGNORE: `*` names dotfiles too - names += [n for n in (entries(f) or []) if fnmatch.fnmatch(n, last) - and (n.startswith(".") or not os.path.lexists(os.path.join(f, n)))] - return sorted(set(names)) + try: + names += [n for n in os.listdir(f) if n.startswith(".") and fnmatch.fnmatch(n, last)] + except OSError: + pass + return sorted(set(names)), bool(now) def inside(s): if plan["parents"]: @@ -726,7 +769,8 @@ def inside(s): if s.endswith("/.") or rsync and s.endswith("/"): return contents(s) if s in v.get("globs", ()) and re.search(r"[*?]", s): # the shell expands it: each file it names - return [base + "/" + n for n in globbed(s)] or [base + "/?"] + names, now = globbed(s) # (none now: the line fills it, not only as it shows) + return [base + "/" + n for n in names] + ([] if now else [base + "/?"]) name = os.path.basename(s.rstrip("/")) if s in (STDIN, "{}") or re.search(r"[$`]", name): return [base + "/?"] # a name the gate cannot see @@ -738,7 +782,8 @@ def inside(s): if rsync and not s.endswith("/") and a_dir(s): return out + inside(s) return out + [dest] + (contents(s) if s.endswith("/.") or rsync and s.endswith("/") else []) - return out + [dest] + [w for s in srcs or [STDIN] for w in inside(s)] + becomes = isdir is None and re.search(r"[$`]", dest) and len(srcs) == 1 and not rsync and not srcs[0].endswith("/.") + return out + [dest] + ([base + "/*"] if becomes else []) + [w for s in srcs or [STDIN] for w in inside(s)] def simple(c, depth, cur=""): @@ -752,24 +797,31 @@ def simple(c, depth, cur=""): if not c.words: # `> F`, `X=1 > F`: no program, still a write return [{"prog": "", "args": [], "env": list(env), "writes": list(writes), "dir": d, "config": [], "globs": globs} for d in cur] if writes else [] - vs = chain([render(w.parts) for w in c.words], env, writes, depth, cur) + vs = chain([render(w.parts, True) for w in c.words], env, writes, depth, cur) for v in vs: v.setdefault("globs", globs) return vs -def chain(words, env, writes, depth, cur): +def chain(words, env, writes, depth, cur, shell=None): """The views of a command's words: the program, then each program a wrapper runs, each once per - directory it may run in (cur, a tuple of them).""" + directory it may run in (cur, a tuple of them). `shell`: where the shell that expanded the words is, + when a wrapper (env -C, git -C, find -execdir) moved the command from there.""" cur = (cur,) if isinstance(cur, str) else tuple(cur) out, config, stdin_args = [], [], False + + def moving(): # the shell stays where it was when the command moves + return shell if shell is not None else cur[0] if len(cur) == 1 else UNKNOWN + + def nest(text): # text a nested shell reads: this shell's $PWD is where it is + return nested(text, (shell,) if shell is not None else cur) while words and ASSIGN.match(words[0]): - env.append(words.pop(0)) + env.append(own(words.pop(0))) while words: if words[0] == "--": # the end of a wrapper's options (sudo -u root -- CMD, time -p -- CMD) words = words[1:] continue - prog = os.path.basename(words[0]) + prog = os.path.basename(own(words[0])) rest = words[1:] code = None if PYTHON.fullmatch(prog): # python [options] (-c CODE | -m MOD | SCRIPT | -) … @@ -778,35 +830,40 @@ def chain(words, env, writes, depth, cur): m = re.match(r"^-[bBdEhiIOqsSuvVxP]*([cm])(.*)$", rest[j]) if m: if m.group(1) == "c": - code = m.group(2) or (rest[j + 1] if j + 1 < len(rest) else "") + code = own(m.group(2) or (rest[j + 1] if j + 1 < len(rest) else "")) break j += 2 if rest[j] in PY_PAIRED else 1 else: if j < len(rest) and rest[j] != "-" and rest[j] not in STDIN_FILES: prog, rest, code = os.path.basename(rest[j]), rest[j + 1:], None # python SCRIPT: the script runs if prog == "tee": - writes = writes + [w for w in rest if not w.startswith("-")] + writes = writes + [own(w) for w in rest if not w.startswith("-")] if prog in _patch.PATCHERS and rest: # apply_patch 'PATCH': each path it names is a write - writes = writes + patched(rest[0]) + writes = writes + patched(own(rest[0])) if prog == "git": while rest and rest[0].startswith("-"): o, v = rest[0], rest[1] if len(rest) > 1 else None if o == "-C": - cur = union(*((chdir(d, v),) for d in cur)) + v = own(v) if v is not None else v + shell, cur = moving(), union(*((chdir(d, v, shell),) for d in cur)) elif o in ("-c", "--config-env") and v: - config.append(v) + config.append(own(v)) elif o.startswith(("--git-dir", "--work-tree")): cur = (UNKNOWN,) elif o.startswith("--config-env="): - config.append(o.split("=", 1)[1]) + config.append(own(o.split("=", 1)[1])) rest = rest[2:] if o in GIT_PAIRED else rest[1:] if prog == "command" and rest[:1] in (["-v"], ["-V"]): rest = [] # `command -v X` looks X up; it runs nothing - args = rest + ([STDIN] if stdin_args is True else []) + args = [own(a) for a in rest] + ([STDIN] if stdin_args is True else []) plans = copied(prog, args) if prog in COPY_PAIRED else None # placed where it runs for d in cur: out.append({"prog": prog, "args": args, "env": list(env), "writes": list(writes), "dir": d, "config": list(config)}) + if shell is not None: + out[-1]["shell"] = shell + if prog in ("echo", "printf"): # what it writes to a pipe another shell reads + out[-1]["said"] = [nest(a) for a in rest] if plans is not None: out[-1]["copies"] = plans if code is not None: @@ -824,22 +881,22 @@ def chain(words, env, writes, depth, cur): has_c = has_c or bool(re.match(r"^-[A-Za-z]*c[A-Za-z]*$", a)) j += 2 if a in SHELL_PAIRED else 1 if has_c and script is not None: - out += [dict(v, env=v["env"] + env) for v in views(script, depth + 1, cur)] + out += [dict(v, env=v["env"] + env) for v in views(nest(script), depth + 1, cur)] elif script is not None and j < len(rest): words = rest[j + 1:] if rest[j] in ("--", "-") else rest[j:] # bash SCRIPT ARGS: the script runs if words and words[0] != "-" and words[0] not in STDIN_FILES: continue break if prog == "eval": - out += views(" ".join(rest), depth + 1, cur) + out += views(nest(" ".join(rest)), depth + 1, cur) break if prog == "script": # script [-q…] -c CMD [FILE]: CMD runs in a shell for j, a in enumerate(rest): if a.startswith("--command="): - out += [dict(v, env=v["env"] + env) for v in views(a.split("=", 1)[1], depth + 1, cur)] + out += [dict(v, env=v["env"] + env) for v in views(nest(a.split("=", 1)[1]), depth + 1, cur)] break if (a == "--command" or re.fullmatch(r"-[a-zA-Z]*c", a)) and j + 1 < len(rest): - out += [dict(v, env=v["env"] + env) for v in views(rest[j + 1], depth + 1, cur)] + out += [dict(v, env=v["env"] + env) for v in views(nest(rest[j + 1]), depth + 1, cur)] break break if prog == "find": @@ -850,7 +907,8 @@ def chain(words, env, writes, depth, cur): if inner: if depth >= DEPTH: raise ValueError("find -exec nests deeper than the door gate reads (%d)" % DEPTH) - out += chain(inner, [], [], depth + 1, (UNKNOWN,) if rest[j] in ("-execdir", "-okdir") else cur) + out += chain(inner, [], [], depth + 1, *(((UNKNOWN,), moving()) if rest[j] in ("-execdir", "-okdir") + else (cur, shell))) j += len(inner) j += 1 break @@ -885,14 +943,15 @@ def chain(words, env, writes, depth, cur): if o in ("-C", "--chdir") or o.startswith("--chdir=") or o.startswith("-C") and len(o) > 2: to = (words[1] if len(words) > 1 else None) if o in ("-C", "--chdir") else \ o.split("=", 1)[1] if o.startswith("--") else o[2:] - cur = union(*((chdir(d, to),) for d in cur)) # env -C DIR: the command runs there + to = own(to) if to is not None else to + shell, cur = moving(), union(*((chdir(d, to, shell),) for d in cur)) # env -C DIR: the command runs there words = words[2:] if o in ("-C", "--chdir") else words[1:] continue if o in ("-u", "--unset"): words = words[2:] continue if ASSIGN.match(o): - env.append(o) + env.append(own(o)) words = words[1:] else: # sudo -u USER …, timeout -s KILL 5 …, nice -n 5 …: skip options and their values paired = WRAPPER_PAIRED.get(prog, ()) @@ -901,7 +960,7 @@ def chain(words, env, writes, depth, cur): if prog in ("timeout", "taskset", "chrt", "flock") and words and words[0] != "--": words = words[1:] # the duration, the CPU mask, the priority, the lock file if prog == "flock" and words[:1] in (["-c"], ["--command"]): - out += [dict(v, env=v["env"] + env) for v in views(words[1] if len(words) > 1 else "", depth + 1, cur)] + out += [dict(v, env=v["env"] + env) for v in views(nest(words[1]) if len(words) > 1 else "", depth + 1, cur)] break return out @@ -980,6 +1039,8 @@ def resolve(cwd, where, kernel=False): if not toplevel(cwd): return "" where = toplevel(cwd) + where[len(TOP):] + if where.startswith(CWD): + where = where[len(CWD):].lstrip("/") or "." where = os.path.join(cwd, os.path.expanduser(where)) return os.path.realpath(where) if kernel else os.path.normpath(where) @@ -1026,7 +1087,7 @@ def branches(cwd, vs): elif not a[j].startswith("-"): ops.append(a[j]) j += 1 - at = chdir(v["dir"], ops[0]) if ops else UNKNOWN + at = chdir(v["dir"], ops[0], v.get("shell")) if ops else UNKNOWN at = None if at == UNKNOWN else os.path.normpath(at) name = (name or (ops[1] if len(ops) > 1 else os.path.basename(ops[0].rstrip("/")))) if at else None if name and at: @@ -1062,8 +1123,8 @@ def placed(cwd, top, v, path, kernel=False, here=None): checkout's top, when the caller knows it.""" if path.startswith("-") or path in (STDIN, "") or "\n" in path or len(path) > 4096: return path - if re.match("(?:%s|\\$PWD|\\$CLAUDE_PROJECT_DIR)(?:/|$)" % re.escape(TOP), path): - where = chdir(v["dir"], path) + if re.match("(?:%s|%s|\\$PWD|\\$CLAUDE_PROJECT_DIR)(?:/|$)" % (re.escape(TOP), re.escape(CWD)), path): + where = chdir(v["dir"], path, v.get("shell")) elif re.search(r"[$`]", path) or v["dir"] == UNKNOWN and not (os.path.isabs(path) or path.startswith("~")): where = UNKNOWN else: @@ -1092,11 +1153,11 @@ def where_of(v, path): """Where a command's path operand is, as resolve() takes it (UNKNOWN: the gate cannot tell).""" if re.search(r"[$`]", path) or "\n" in path or len(path) > 4096: return UNKNOWN - if os.path.isabs(path) or path.startswith("~"): + if os.path.isabs(path) or path.startswith(("~", CWD)): return path # where it says, wherever the command runs if v["dir"] == UNKNOWN: return UNKNOWN - return path if path.startswith(TOP) or not v["dir"] else v["dir"] + "/" + path + return path if path.startswith((TOP, CWD)) or not v["dir"] else v["dir"] + "/" + path def matches(cwd, v, path): @@ -1159,7 +1220,7 @@ def place(cwd, vs): top, seen, here, made = os.path.realpath(_paths.project(cwd)), {}, toplevel(os.path.abspath(cwd)), [] def forms(v, a): # once per (directory, path): candidate dirs repeat them - key = (v["dir"], a, a in v.get("globs", ())) + key = (v["dir"], v.get("shell"), a, a in v.get("globs", ())) if key not in seen: seen[key] = _forms(v, a) return seen[key] @@ -1174,9 +1235,10 @@ def named(v, a): # DIR/NAME as DIR/'s forms, unless return [f.replace(NAMED, name) for f in forms(v, head + "/" + NAMED)] def _forms(v, a): - out = [placed(cwd, top, v, a, k, here) for k in (False, True)] + p = anyname(a) + out = [placed(cwd, top, v, p, k, here) for k in (False, True)] out += [placed(cwd, top, v, m, k, here) for m in matches(cwd, v, a) for k in (False, True)] - where = where_of(v, a) if not a.startswith("-") and a != STDIN else UNKNOWN + where = where_of(v, p) if not p.startswith("-") and p != STDIN else UNKNOWN if where != UNKNOWN and resolve(cwd, where): for k in (False, True): full = resolve(cwd, where, k) diff --git a/scripts/test/mission-doors.test.py b/scripts/test/mission-doors.test.py index 3955bd9a..a808c825 100644 --- a/scripts/test/mission-doors.test.py +++ b/scripts/test/mission-doors.test.py @@ -1254,6 +1254,73 @@ def swallowing(*a, **k): ok("MCP: a tool that writes a draft (send_mail_draft, forward_as_draft) does not send; one that sends a draft does") for x in (dot, tree, big, os.path.join(p.dir, "build"), os.path.join(p.dir, "dist")): shutil.rmtree(x, ignore_errors=True) +# 21f) the sixth M9.31 review (of the fifth's fixes). The shell that reads a word expands its $PWD and $(pwd): +# the text a nested shell reads (sh -c "…", eval, <<<, an unquoted heredoc) carries the outer directory, +# and env -C or find -execdir moves the command, not the words the shell already expanded; `echo … && +# exit` and `return 1 || exit 1` always exit +for cmd, want in (("bash -c \"cd W && git -C $(pwd) push origin HEAD\"", "push-default-branch"), + ("eval \"cd W && git -C $(pwd) push origin HEAD\"", "push-default-branch"), + ("bash <&2 && exit 1; }; git push origin HEAD", + "cd W || { echo x; return 1 2>/dev/null || exit 1; }; git push origin HEAD", + "cd W || { echo no && exit 1; } && git push origin HEAD", "cd W || { printf 'no\\n' && exit 1; }; git reset --hard", + "cd W || { : && exit 1; }; git reset --hard"): + cmd = cmd.replace("W ", W + " ").replace("W\n", W + "\n") + assert hook(g, "Bash", {"command": cmd}, cwd=R).stdout == "", cmd +for cmd in ("env -C /tmp rm -rf \"$PWD/.aladdin\"", "find /tmp -maxdepth 0 -execdir rm -rf \"$PWD/.aladdin\" \\;", + "env -C /tmp rm -f \"$(pwd)/.aladdin/logs/events.jsonl\"", "find . -name '*.bak' -execdir rm -rf \"$PWD/.aladdin\" \\;", + "env --chdir=/tmp sh -c \"rm -rf $PWD/.aladdin\"", "bash -c \"rm -rf $PWD/.aladdin\"", + "bash < S/.stamp && cp -r S/. .claude/", + "mkdir -p stage && echo x > stage/README && curl -fsSL URL -o stage/settings.json && cp stage/* .claude/", + "mkdir -p stage && echo x > stage/README && curl -fsSL URL -o stage/settings.json && rsync -a stage/ .claude/", + "mkdir -p stage && unzip -o cfg.zip -d stage && echo '{}' > stage/keybindings.json && cp -r stage/. ~/.claude/", + "echo x > dist/README && curl -fsSL URL -o dist/settings.json && cp dist/* .claude/", + "echo x > dist/README && curl -fsSL URL -o dist/settings.json && cp -r dist/. .claude/", + "cp -r .claude \"$WT/.claude\"", "git worktree add \"$WT\" -b x && cp -r .claude \"$WT/.claude\"", + "mv .claude.bak \"$REPO/.claude\"", "cp -a ~/.claude-template \"$PROJECT/.claude\"", "cp -r tmpl \"$WT/.claude/\"", + "cp -r cfg \"$WT/.codex\"", "mv opencode-cfg \"$PROJ/.opencode\"", "cd \"$X\" && cp -r cfg \"$(pwd)/.claude\"", + "cp x .claude/${F:?}", "cp x ~/.claude/${F:?}"): + cmd = cmd.replace("S/", stage + "/").replace("S ", stage + " ") + assert "change-agent-settings" in doors_of(cmd), (cmd, doors_of(cmd)) +for cmd in ("rm -f .aladdin/logs/${F:?}", "echo x > docs/agents/mission/${F:?}", "rm -f .aladdin/logs/$F", + "sed -i /door.revoked/d \".aladdin/logs/$F\"", "rm -f \".aladdin/logs/${F?}\""): + assert "forge-human-record" in doors_of(cmd), (cmd, doors_of(cmd)) +for cmd in ("cp -r .claude \"$tmp\"", "cp -r .claude \"$WT/\"", "cp -r . \"$TMPDIR\"", "rm -f build/$F", "echo x > \"logs/$name.log\"", + "mkdir -p out && echo x > out/a.txt && cp -r out/. dist/"): + assert doors_of(cmd) == [], (cmd, doors_of(cmd)) +ok("copies: an unlisted or empty source directory, a copy that becomes $VAR/.claude, a variable last name") +for tool in ("mcp__zapier__send_gmail_draft", "mcp__composio__send_gmail_draft", "mcp__gmail__send_from_alias_draft", + "mcp__gmail__send_canvas_draft"): + assert ids(tool, {}) == ["send-external-message"], (tool, ids(tool, {})) +for tool in ("mcp__gmail__send_mail_draft", "mcp__gmail__send_email_as_draft", "mcp__outlook__forward_as_draft", + "mcp__slack__slack_send_message_draft"): + assert ids(tool, {}) == [], (tool, ids(tool, {})) +ok("MCP: a draft writer is one whose last words are message, mail, email or as, then draft") +shutil.rmtree(os.path.dirname(stage), ignore_errors=True) +shutil.rmtree(os.path.join(p.dir, "dist"), ignore_errors=True) for x in (evil, fake, lnr): shutil.rmtree(x, ignore_errors=True) From b12daf740d0f6cb2992f697da445c258e6c1d33b Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 19:21:54 +0000 Subject: [PATCH 13/14] Door test: a held lock runs the decision's clock out, not a slow runner's classify The macOS runner failed the held-lock check (4d0b1de): with a 1 s deadline, the work before the lock took the whole second there, so the gate failed closed with the generic deadline message instead of naming doors.lock. The check now warms and times that work in the same project and gives the decision max(2 s, 4x it), so only the held lock can run the clock out; the assertions (exit 2, BLOCK naming doors.lock, nothing recorded, bounded time) are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- scripts/test/mission-doors.test.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/test/mission-doors.test.py b/scripts/test/mission-doors.test.py index a808c825..221a353f 100644 --- a/scripts/test/mission-doors.test.py +++ b/scripts/test/mission-doors.test.py @@ -868,17 +868,21 @@ def hook_in(payload, seconds=1): return rc, out.getvalue(), err.getvalue(), time.time() - t0 +t0 = time.time() +D.classify(p.dir, "Bash", {"command": "terraform apply"}, CAT) # what runs before the lock, warmed and timed +before = time.time() - t0 held = os.open(os.path.join(D._paths.home(p.dir, "state"), "doors.lock"), os.O_RDWR | os.O_CREAT, 0o644) fcntl.flock(held, fcntl.LOCK_EX) # another gate holds the lock (and never lets go) watchdog = threading.Timer(10, fcntl.flock, (held, fcntl.LOCK_UN)) # so a gate that blocks fails here, not hangs watchdog.start() n = len(p.events("door.denied")) +wait = max(2, 4 * before) # the lock, not a slow runner's classify, runs the clock out try: - rc, out, err, took = hook_in(stopping) + rc, out, err, took = hook_in(stopping, wait) finally: watchdog.cancel() os.close(held) -assert rc == 2 and out == "" and "BLOCK[doors]" in err and "doors.lock" in err and took < 3, (rc, out, err, took) +assert rc == 2 and out == "" and "BLOCK[doors]" in err and "doors.lock" in err and took < wait + 2, (rc, out, err, took, before) assert len(p.events("door.denied")) == n # nothing decided, nothing recorded rc, out, err, took = hook_in(stopping) assert rc == 0 and "ASK[door] production-deploy" in out, (rc, out, err) From 2975cd2c3cf021daf09b8023f2fae150a0456a9b Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 20 Oct 2026 19:48:09 +0000 Subject: [PATCH 14/14] M9.31: seventh review fixes (a mark is a path only where it starts a word, nested cd $PWD keys, multi-dir $PWD, "$PWD" copies, whole substituted names) Five confirmed regressions from a hunt on the sixth review's fixes, and one older crash found with them, each with a test that was red before the change (section 21g of mission-doors.test.py, 49 checks; 39 new assertions red on 4d0b1de): - a NUL mark (the call's cwd, the checkout's top) inside a word is no path: `bash -c "make PREFIX=$PWD/out"` and, since M9.30, `make PREFIX=$(git rev-parse --show-toplevel)/out` crashed the gate (ValueError: embedded null), which failed closed and blocked them - chdir() names the call's cwd as the line's directories do ("" and relative), so branches() finds a `git checkout main` before a nested `cd $PWD && git push` - where the line may be in more than one directory, a nested $PWD is read in each of them and a nested $(pwd) is unknown (two marks) - a copy into "$PWD" or "$CLAUDE_PROJECT_DIR" is into the directory it is, and a regular file source never becomes "$VAR/.claude" - only a wholly substituted last name ($F, ${F:?}, $(...)) is any name: `settings.json.bak.$(date +%s)` and `mission-cron.log.$(date +%F)` are backups and rotations, not the guarded records Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012rsDo9GYzNx8tXucywZHnA --- CHANGELOG.md | 2 +- docs/ROADMAP.md | 4 +-- plugin/mission/doors.py | 51 +++++++++++++++++++----------- scripts/test/mission-doors.test.py | 40 +++++++++++++++++++++++ 4 files changed, 76 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9c31970..12be7595 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,7 +25,7 @@ tag in lockstep. Tag releases as `vMAJOR.MINOR.PATCH`. ### Added - Plan approval is a gate (ROADMAP M0.9): `gate-dispatch` starts no execution role (RED, SCAFFOLD, GREEN, structural review, final gate) of a sprint until the human approved that sprint's plan as it is now. `events authority --sprint N` exits 3 with the line to type, `approve SN ` (reviewed, the code naming the sha256 of the sprint's plan files, its intake and standards included) or `autonomous SN ` (autonomy for that sprint, re-plans included, the code naming the authority record); the task contract's `STORY_DIR` and task must be a live story and task of that plan's frozen `ids.json`; the line must be among the human's captured prompts and typed after the sprint's last decision (M9.17's mechanism). A changed plan, a re-plan in reviewed mode, a roll-back to an old line, another sprint's line, a negated line and a hand-edited or emitted record all block (`plan.approved` and `human.approval` cannot be `events emit`ted); an approved autonomous mission grants its own sprints autonomy with no line. `events approval SPRINT|TASK_ID` is the check. Codex's `spawn_agent` (`agent_type`, `message`) and OpenCode's `task` now reach `gate-dispatch` too, and `claude -p`, `codex exec` and `opencode run` from a tool call are blocked in every aloop project, not only where a mission is set. `e2e-live.sh` pre-approves with an `autonomous S1 ` line in its prompt. Reason: the north star allows no human help beyond approval, and the approval was a playbook rule a supervisor could skip. A 4-lens adversarial review found 9 defects: 7 fixed with a check red on the first head (cross-sprint or unplanned `STORY_DIR`, a retyped line kept stale, mission autonomy over a non-mission sprint, `transcripts prune` dropping the prompts grants cite, `codex plugin add` refused, a code past column 200, a crash on a malformed run-log line), 2 in the playbook (intake reuses a mission's handed-off intent; a reviewed re-plan waits for the human's new line). `transcripts prune` now keeps the human's prompts. Test: `plan-approval.test.py` (20 checks, red on master), plus the host suites. The approved plan's files include the sprint's `intake.md` and `standards.md`, and a grant pins them file by file: GREEN's check for an unplanned dependency reads only the text the human's grant pins, the plan they approved or, under their autonomy grant, the plan as the planner's gate last passed it (`gate-stage2-complete` records `plan.gated` on the planner's stop), as it was then, so text added later names nothing and a re-plan does not undo an approved dependency. - Core agent principles and the one-way / two-way door rule (ROADMAP §4.12, M9.30): seven core principles from Amazon's Leadership Principles, the door rule first, go to every aloop agent at session start (`aloop mission principles show --core`). A two-way door (a branch, a local edit, a test, a draft PR) is decided, not asked. A one-way door (`aloop mission doors list`: a push or merge to the default branch, a force-push, a release, a deploy, a message sent outside, a secret, dropped data, …) is stopped by a PreToolUse gate with `ASK[door] ` until the human records an authorization in their own terminal (`aloop mission doors authorize ID --by NAME --reason TEXT [--until DATE] [--once DIGEST]`, a hash-chained ledger row); a wall (switching a gate off, forging a human record) is never crossed. While a run is live, a question to the human must name a door the gate stopped and nobody authorized. Reason: Adeel's request that agents decide two-way doors themselves and ask only at a one-way door not already authorized. Test: `mission-doors.test.py`. Hardened by an adversarial review (15 findings, each a test): the gate reads Bash as the shell does (comments, line continuations, heredoc bodies, `xargs`, `find -exec`, `echo … | sh`), a branch condition reads the directory the command acts on (`git -C`, `cd`), settings are guarded wherever they live, dry runs and `command -v` are two-way, the newest decision by time counts and a revoke holds on every branch, a once-grant is crossed once under a lock, a broken ledger is named, and `doors authorize` refuses an agent's shell. -- The door rule on every host and role (ROADMAP §4.12, M9.31): every dispatched role gets the core principles (`aloop mission principles hook` at SubagentStart, Codex agents' instructions, OpenCode's system prompt, each dsh role persona), and the door gate runs on Codex, OpenCode and dsh with each host's question tool mapped. In the OpenCode and dsh plugins it runs last, after every other gate admitted the call, and fails closed after 15 s. A bound worker's command is judged where it runs (its `workdir`), an MCP tool by its own arguments (`mcp____` on OpenCode too), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Hardened by six reviews (96 findings, each a test that failed first): a stalled decision can no longer fall through to an allow, `apply_patch` is read with any header spacing (a U+FEFF too) and through the shell, a bare redirect or a group's redirect reaches the commands inside, a `cd` that may not run leaves the directory unknown, a symlink then `..` is read as the kernel walks it, replacing a settings file with `cp`, `mv`, `ln`, `install`, `rsync` (into a settings directory too), `rm`, `sed -i` or `perl -i`, or removing a directory that holds one, is the settings door, and an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`) crosses the same door as the service's own. A `cd` that may not run leaves each later command in either directory, a copy is placed where it runs (so backing up `.claude` passes), and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Drive access grants are doors too. After `cd A || cd B` a line may be in A, B or where it started, a glob or contents copy names the files it copies, a name the gate cannot see is no settings change at the project root, a wrapped `cd` (`nohup cd`) or a background list (`cd X && npm run dev &`) moves no shell, and a copy names what the line staged before it. The shell's own `$PWD` stays where the shell expanded it (in a nested shell's text, under `env -C` or `find -execdir`), and a source directory the gate cannot list, a copy that becomes `"$VAR/.claude"` and a variable last name in a guarded directory are read as any name. Tests: `mission-doors.test.py`, `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`. +- The door rule on every host and role (ROADMAP §4.12, M9.31): every dispatched role gets the core principles (`aloop mission principles hook` at SubagentStart, Codex agents' instructions, OpenCode's system prompt, each dsh role persona), and the door gate runs on Codex, OpenCode and dsh with each host's question tool mapped. In the OpenCode and dsh plugins it runs last, after every other gate admitted the call, and fails closed after 15 s. A bound worker's command is judged where it runs (its `workdir`), an MCP tool by its own arguments (`mcp____` on OpenCode too), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Hardened by seven reviews (101 findings, each a test that failed first): a stalled decision can no longer fall through to an allow, `apply_patch` is read with any header spacing (a U+FEFF too) and through the shell, a bare redirect or a group's redirect reaches the commands inside, a `cd` that may not run leaves the directory unknown, a symlink then `..` is read as the kernel walks it, replacing a settings file with `cp`, `mv`, `ln`, `install`, `rsync` (into a settings directory too), `rm`, `sed -i` or `perl -i`, or removing a directory that holds one, is the settings door, and an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`) crosses the same door as the service's own. A `cd` that may not run leaves each later command in either directory, a copy is placed where it runs (so backing up `.claude` passes), and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Drive access grants are doors too. After `cd A || cd B` a line may be in A, B or where it started, a glob or contents copy names the files it copies, a name the gate cannot see is no settings change at the project root, a wrapped `cd` (`nohup cd`) or a background list (`cd X && npm run dev &`) moves no shell, and a copy names what the line staged before it. The shell's own `$PWD` stays where the shell expanded it (in a nested shell's text, under `env -C` or `find -execdir`), and a source directory the gate cannot list, a copy that becomes `"$VAR/.claude"` and a wholly substituted last name in a guarded directory are read as any name, and a word that only contains a directory (`PREFIX=$PWD/out`) no longer stops the gate. Tests: `mission-doors.test.py`, `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`. - DeepSeek Harness (dsh) host (ROADMAP §4.9): `aloop install dsh` (alias `deepseek`; `--project P` or `--global`) renders a `dsh --profile aloop` profile in `$DSH_HOME` and installs the skills. Its native plugin (`plugin/hosts/dsh/aloop.mjs`) runs the same gates as Claude Code on dsh's own events: a deny before a tool runs, a block after an invalid artifact write, and a stop gate that steers a role back up to 3 times before the supervisor is told it is BLOCKED (or STUCK, released unverified). Roles are agent presets on two subagent tools, one per model route (`deepseek-v4-pro` for planning, `deepseek-flash` for work by default; `MODEL_PLANNING_DSH` / `MODEL_WORKER_DSH` override). A writer's `aloop bind` moves its working directory into its worktree, and `host-adapter` confines its commands, edits and `working_directory` there. Usage is recorded Claude-shaped per role, so budgets, the ledger and `run-summary` work unchanged. `make e2e-live HOST=dsh` drives it live and exits 2 with the fix when no `DEEPSEEK_API_KEY` is set. Reason: DeepSeek's own harness is the cheapest route to a strong model. The profile records the project it serves (or `--global`), so another project's `aloop-init` never re-renders it, and gates get the worker shell's environment (no API key). The mission layer's six roles run on dsh too: presets on their route group's subagent tool, `gate-mission-scope` on every write and command, and `gate-mission-artifact` at each role's start and stop; the mission skill installs as `aloop-mission`. A second review (3 lenses; 11 confirmed findings, each fixed with a test) closed: a working directory that vanished (dsh's tools move it back to the main tree, so the plugin judges `ensure()`'s directory and `host-adapter` refuses to run from a missing one), gate switches from the project's `.env` (dsh loads it; `SKIP_HOOKS`, `GATE_RUN_TESTS`, `REPO_DIR`, `AGENTIC_*` from it never reach a gate), an interrupted Stop that dropped the sprint-close notice, and the shared profile: its owner is the main tree, an older unrecorded profile is claimed only by the project that installed it, and a project it does not serve is told so by `install status`, `aloop-init` and the session itself. Tests: `dsh-plugin.test.mjs` (46 checks, emulated), `dsh-runtime.test.mjs` (50 checks on the real pinned dsh 0.2.1-rc.2 with a scripted model; the CI hosts job installs it) and `dsh-install.test.py` (10). - dsh follow-ups (ROADMAP §4.9). One `aloop install dsh` serves every project: the plugin routes each role's requests through dsh's `agent/request` to its own project's model (`aloop models resolve `), so gate-dispatch no longer refuses another project's roles until a re-install, and a role whose model cannot be resolved is refused (fail closed). Roles are no longer offered `send_message`, so dsh adds no "send your result" guidance and no call is spent on a deny. dsh compaction is recorded as `context.compacted` and the handoff comes back after it; the capability matrix marks dsh compaction and live messaging as advertised, proven on the real runtime. Two mission roles (one per subagent tool) run through both of their gates on the real runtime. `run-summary --host dsh` reports a dollar cost: each logged call is priced from a dated, sourced DeepSeek table (`plugin/bin/_prices.py`; cache hit, cache miss, output, peak or off-peak), with `cost_by_model` in `summary.json`; an unpriced model id keeps its tokens and leaves the cost unmeasured, never guessed. - Brand kit and repo front page: `docs/brand/` holds the mark (an **a** whose bowl is a red-to-green loop passing a gold gate, with a 16 px cut), lockups, light and dark README banners, favicons, app icons, the site's OG card and the GitHub social preview, all generated by `docs/brand/gen_brand.py` from one palette with WCAG checks (rules in `docs/brand/README.md`). The README leads with the mission layer, the agent principles and the door rule, the supported hosts and the measured demo runs; the site carries the favicon and Open Graph/Twitter card metadata; `SECURITY.md` and bug/feature issue forms are new. diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 89e7eb5a..93d2084d 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -484,7 +484,7 @@ Each task flips only its own row here and in TRACKING.md. | M9.28 | `tick` drives the goal commands (M9.11): a goal counts only with its `goal.derived`/`goal.approved` record and closes by `goal.met`; handouts name `goal derive`, `options` (spoon-fed), `intents`, `next` and `close`; an eval narrowed to a closed goal asks for a whole-mission eval. Reason: found when M9.11 landed, `tick` read goal files and their status, so a goal nobody derived steered the loop and a met goal was never closed | M9.8, M9.11 | `plugin/mission/tick.py`, `plugin/skills/mission/SKILL.md`, `mission-tick.test.py` | yes | done: `mission-tick.test.py` (29 checks; a real goal run on the real clock: derive → review → intents → next and a sprint per intent → narrowed eval → close → whole-mission eval → `RESEARCH`) | | M9.29 | A goal that crosses a hard line stops being the active goal (`evaluate.active_goal` skips it, or a recorded retirement). Reason: found wiring M9.28, `evaluate` keeps narrowing to a hard-lined goal and `goal derive` refuses a narrowed eval, so the mission deadlocks (`tick` hands it to the human meanwhile) | M9.10, M9.11 | `plugin/mission/{evaluate,goal}.py`, their tests | yes | done: `evaluate.active_goal` skips a goal an ethics hard line retired (it never closes), so the next eval covers the whole mission; `goal derive` from an eval narrowed to a retired goal says to evaluate again; `tick` hands out EVALUATE for such an eval, then DERIVE_GOAL, which takes the next gap (a hard line holds the retired goal's own gaps); when every open gap is held it is BLOCKED for the human (a change of the mission), not handed to a derive that would refuse. `mission-goal.test.py` (section 11), `mission-tick.test.py` (section 27). Call: retirement is derived from the hard line itself, not a new record, so it holds on a fresh clone with the ledger | | M9.30 | Core agent principles and the one-way / two-way door rule, enforced: seven core principles from Amazon's Leadership Principles (the door rule first) for every agent, injected at session start; a door catalogue, the human's standing or once authorization in the ledger, and a PreToolUse gate that stops an unauthorized one-way door (ASK) and a wall (BLOCK) and lets two-way doors pass; while a run is live a question must name a door the gate stopped. Reason: Adeel, 2026-10-20: "If it is two-way, then the agent should take the decision directly without bothering the user. But if it is one-way door, then it can bother user if user already have not specified to cross that door." | M9.0, M9.7 | `plugin/mission/doors.{py,json}`, `principles.{md,py}` (`## Core`), `plugin/bin/session-start`, `hooks.json` (one line), core: `_events.py` / `_mission.py` (door events), `events` (`GATE_OWNED`), `scripts/test/mission-doors.test.py` | after M9.7 | done: `mission doors list / classify / authorize / revoke / check / hook`, `mission principles show --core`; `mission-doors.test.py` (21 checks), `mission-principles.test.py` (+1 check). Calls: anything the catalogue does not list is two-way (a finite list cannot claim full enforcement, §7); Bash is matched by program and arguments after reading the line as the shell does, with the scope gate's reader (`_shellread.py`, M9.38: quotes, comments, line continuations, `$(…)`, a heredoc's body as data unless a shell or python runs it) and unwrapping `sudo`, `env`, `sh -c`, `eval`, `xargs`, `find -exec` and `echo … | sh`, so text that only mentions a door (`grep`, a commit message, a file written by a heredoc) stays two-way; a `when` condition (on the default branch, the main checkout) reads the directory the command acts on (`git -C DIR`, `cd DIR &&`), and one the gate cannot resolve counts; grants are standing (until revoked or `--until`), once (one action's digest, 24 h, used up by its first crossing, recorded in the ledger), never (a wall: switching a gate off, forging a human record; refused even for the human's `authorize`) and command (question-only doors answered by their own command); authorizations are hash-chained human ledger rows (an edited row, `events emit` or a stream-only event opens nothing; `HUMAN_EVENTS` are now gate-owned, which also closes a forged `baseline.vetoed`); a question is checked only while a run is live (an approved autonomous mission or an autonomous run, or a sprint whose latest plan is approved, for the session holding its lock), so intake, planning and the plan's own approval are asked; the core block is kept out of the goal blocks, so editing it never stales a goal's stamp; the hook fails closed (exit 2) and is silent outside an aloop project; measured: median 81 ms per two-way call, 102 ms per stopped one-way call (30 calls each, in parallel with the other `*` hooks). Calls from its adversarial review (15 confirmed findings over 3 lenses, each now a test that failed before its fix): the newest decision about a door is the newest by time (a union merge that puts an older authorization last reopens nothing), and the run log's revokes and crossings close on any branch of the checkout; a once-grant is checked and crossed under a lock (`.aladdin/state/doors.lock`); a broken ledger is named in the ASK, fails `doors check` and blocks `authorize` with the restore line; settings and the ledger are matched by path suffix (`~/.claude`, a worktree's own); a dry run, a help page and `command -v` are two-way, `--dry-run=none` is not; `git -c core.hooksPath=` is a wall and `git config core.hooksPath VALUE` a one-way door; arguments `xargs` adds from stdin may be any flag of a one-way door; `doors authorize` refuses an agent's shell (`CLAUDECODE` set: the human authorizes in their own terminal); Slack's real send tools (`slack_post_message`, `slack_reply_to_thread`) are one-way; a hard ethics line only the run log holds (recorded before the ledger did) still binds. Calls from its second review (26 confirmed findings over parser, catalogue and state lenses, each now a test that failed before its fix): the gate reads Bash with `_shellread.py` (M9.38), which now keeps each heredoc's body, the operator that ends a command and heredoc bodies inside `$(…)`, so touching operators (`); git push`), keywords, groups, function bodies, here-strings, IO-number redirects (`2>&1`) and quoted operators (`find \( … \) -exec`) hide nothing, a quote in `"$(cat <<'EOF' … EOF)"` is data, stdin (a heredoc, a here-string, `echo|printf|cat … |`) reaches only the command it feeds (a shell with options or `-s`, a python with `-`), and `pushd`/`popd`, `sh -c`, `eval` and a fed shell keep the directory; nesting deeper than 8 is unreadable and a read past 8 s fails closed (exit 2, under the hook's 15 s); python is a forge only when it calls a writer, `npm|pnpm|yarn -g` a system change only with an installing verb, the ledger only at `docs/agents/mission/ledger.jsonl`, `xargs … doors` only when stdin can be the subcommand (`-I R` replaces R), a secret command's dry run or help page is two-way, `git push origin HEAD|@` on the default branch is one-way, and erasing or editing the run log (`.aladdin/logs/events.jsonl`, whose revokes and crossings close doors) is a wall; a ledger row is written before its stream event and `ledger_problems` reads the stream first, so no gate sees another writer's half-written record as a lost row; a record is dated no earlier than any ledger row, so the human's fresh decision is the newest even after a merged future-dated row; a run stops being live when its plan's sprint closes (`sprints.json`) or a new intent arrives; a malformed run-log line never crashes the ethics check | -| M9.31 | Door rule on every host and role: the core block in SubagentStart for dispatched roles, Codex `developer_instructions`, OpenCode agents and the dsh profile; the hook in Codex PreToolUse, the OpenCode plugin (`aloop.js`) and the dsh plugin (`aloop.mjs`, whose PreToolUse list mirrors `hooks.json` by hand and so lacks `mission doors hook`), with each host's question tool mapped. Also measure whether a human's own `!` shell command reaches PreToolUse (it should not be gated: the human crossing a door is the authorization). Reason: M9.30 wires Claude Code's main session only | M9.30, M9.9 | `plugin/bin/host-adapter`, `install`, `aloop.js`, `aloop.mjs`, `_host.py` | after M9.9 | done: the core block reaches every role on every host (`mission principles hook`, the third SubagentStart command; Codex agents' `developer_instructions`; OpenCode's system transform for a sub-agent and a resumed root; each dsh role persona, written by `install`). The door gate runs on Codex through `hooks.json`, and last in the OpenCode and dsh plugins (after every other gate admitted the call, so only a call that runs uses up a once-authorization; read-only tools skip it; 15 s, fail closed). Each host's question tool is the gate's (`AskUserQuestion`, Codex `request_user_input`, OpenCode `question`, dsh `ask_user_question`). A bound worker's command is read where it runs (`tool_input.workdir`, a relative one under the project), a dsh MCP tool's input is its own arguments (its once digest is the one any host prints), and an OpenCode MCP tool is `mcp____` (from `client.mcp.status()`, the longest server first). One deadline covers the decision (`Deadline`, which no step's `except Exception` can swallow into an allow), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Six reviews (12, 7, 34, 18, 17 and 8 findings, each a test that failed first) added: `apply_patch` read as the hosts parse it (any header spacing or indent, a U+FEFF around the path cut as OpenCode's `trim()` cuts it, and through the shell: Codex's heredoc and argument forms, `applypatch`, after a `cd`, from a pipe; the header reader `_patch.py` is shared with host-adapter and both scope gates, but only the door gate reads the shell form, M9.61); a bare redirect, and a group's or compound's own redirect or heredoc, reach the commands inside; a `cd` that may not run leaves the directory unknown; a path through a symlink then `..` read as the kernel walks it (`git -C`, `cd -P`, a dsh `workdir`); settings replaced, moved, linked or removed by `cp`/`mv`/`ln`/`install`/`rsync` (into a settings directory too, by `-t` or a trailing `DIR/`), `rm`/`mv` of a directory that holds them, a glob that names them, `sed -i` (and `--in-pl`), `perl -i`; `opencode.json` a door only where OpenCode reads it (a nested checkout's root, `~/.config/opencode`, its `config.json`); an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`, `mcp__google-drive__share_file`) crosses the same door as the service's own server, while a service word inside another tool's name (`search_slack_posts`, `get_post_history`) is none. The third review: a `cd` that may not run leaves each later command in either directory (one view per directory, not an unknown one), `cd -`, a bare `pushd`, `popd -n`, `cd X &`, an eval's `cd`, `~+` and `GIT_DIR`/`GIT_WORK_TREE` are read as the shell reads them, and a pipe into a `{ }` group or a compound reaches each command inside; a copy is placed where it runs (DEST/NAME only into an existing directory, so a backup of `.claude` or a file copied into it passes; `-t` with `{}` or xargs' arguments, `--parents`, `-R`, `--remove-source-files`, a link to a settings file, a home directory's contents, GNU option abbreviations); a glob is expanded only where the shell expands it, through links, and never loses a config-like name; another checkout's run home is not this project's; and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Google Workspace's Drive grants cross their doors. The fourth: a copy option that takes no value (`rsync --checksum`, `install --strip`) is no prefix of one that takes one, and only an operand right after an unknown option may be its value, so only then does the destination widen (to at most 8 operands: 600 files and a trailing `-f` stay fast); a glob source names the files it matches now, a contents copy (`SRC/.`, rsync's `SRC/`) its entries, and a name the gate cannot see (`"$f"`, `{}`) is `DEST/?`: no settings change at the project's, a checkout's or the home root, but one into a settings or run-record directory; `ln -n` over a linked settings directory replaces it; `./.claude` after an unknown `cd` and `"$REPO/.claude"` are read by name; after `cd A || cd B` the line is in A, B or where it started; OLDPWD follows a `cd` that may not run and survives a subshell; `cd X || { …; exit 1; }` and `&& ( … ) &&` keep the line in X (the shell reader carries the operator after a subshell or compound, `Mark.end`), and a group after `||` starts where the left side failed; a wrapped `cd` (`nohup`, `env`, `flock`) moves no shell; candidate directories pass into nested reads instead of multiplying them, and a path is placed once per directory; a tool that sends a draft sends (`send-shared-mailbox-draft`) and Drive's `grant_batch` shares. The fifth, a regression hunt on the fourth's fixes: `cp --` no longer crashes the gate; an exit inside `|| { … }` counts only when it always runs; past `&&` or a compound, `|| Y` is reached from where the list succeeded too; a second `cd` that may not run keeps where the list stopped; a background list (`cd X && npm run dev &`) moves no shell; `find -execdir` runs in an unknown directory and `env -C DIR` in DIR; a glob or contents copy also names what the line staged before it and the dotfiles a `*` may name (dotglob); `cp -T` and `rsync -R SRC/./` copy SRC's entries; an absolute path is placed after an unknown `cd`; `$(pwd)` is `$PWD`, `${X:?}` is `$X`, and a run record under a variable directory is that record; a variable directory is a settings root only for `$HOME` (so `cp -a . "$tmp"` passes); an unseen name copied into `docs/agents/mission/` forges the ledger; a large directory's copy is placed by its directory once; and a tool that writes a draft (`send_mail_draft`, `forward_as_draft`) does not send. The sixth, a regression hunt on the fifth's fixes: the shell that reads a word expands its `$PWD` and `$(pwd)`, so the text a nested shell reads (`sh -c "…"`, `eval`, `<<<`, an unquoted heredoc, a pipe) keeps the outer directory, and `env -C`, `git -C` or `find -execdir` move the command but not its words; `echo … && exit` and `return 1 || exit 1` always exit; a source directory the gate cannot list now (missing, or empty) holds more than what the line visibly wrote into it (tar, `curl -o` and unzip are unseen); a copy onto `"$VAR/.claude"` may become that directory; a variable last name in a guarded directory (`.aladdin/logs/$F`) is any name; a glob over a large directory lists it once (95,000 files: 1.1 s to 0.4 s); and a draft writer's name ends in message, mail, email or as, then draft, after a separator (so `send_gmail_draft` sends). Tests: `mission-doors.test.py` (48 checks), `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`, `hook-matchers.test.py`, `mission-principles.test.py`, `dsh-install.test.py`. Calls: the catalogue keeps its generic `_` alternatives for an OpenCode that cannot list its servers (an extra question beats a silent one-way door). Open: M9.56–M9.65 (the `!` measurement is M9.58) | +| M9.31 | Door rule on every host and role: the core block in SubagentStart for dispatched roles, Codex `developer_instructions`, OpenCode agents and the dsh profile; the hook in Codex PreToolUse, the OpenCode plugin (`aloop.js`) and the dsh plugin (`aloop.mjs`, whose PreToolUse list mirrors `hooks.json` by hand and so lacks `mission doors hook`), with each host's question tool mapped. Also measure whether a human's own `!` shell command reaches PreToolUse (it should not be gated: the human crossing a door is the authorization). Reason: M9.30 wires Claude Code's main session only | M9.30, M9.9 | `plugin/bin/host-adapter`, `install`, `aloop.js`, `aloop.mjs`, `_host.py` | after M9.9 | done: the core block reaches every role on every host (`mission principles hook`, the third SubagentStart command; Codex agents' `developer_instructions`; OpenCode's system transform for a sub-agent and a resumed root; each dsh role persona, written by `install`). The door gate runs on Codex through `hooks.json`, and last in the OpenCode and dsh plugins (after every other gate admitted the call, so only a call that runs uses up a once-authorization; read-only tools skip it; 15 s, fail closed). Each host's question tool is the gate's (`AskUserQuestion`, Codex `request_user_input`, OpenCode `question`, dsh `ask_user_question`). A bound worker's command is read where it runs (`tool_input.workdir`, a relative one under the project), a dsh MCP tool's input is its own arguments (its once digest is the one any host prints), and an OpenCode MCP tool is `mcp____` (from `client.mcp.status()`, the longest server first). One deadline covers the decision (`Deadline`, which no step's `except Exception` can swallow into an allow), and a subagent stopped at a door reports `ASK[door]` to its supervisor. Seven reviews (12, 7, 34, 18, 17, 8 and 5 findings, each a test that failed first) added: `apply_patch` read as the hosts parse it (any header spacing or indent, a U+FEFF around the path cut as OpenCode's `trim()` cuts it, and through the shell: Codex's heredoc and argument forms, `applypatch`, after a `cd`, from a pipe; the header reader `_patch.py` is shared with host-adapter and both scope gates, but only the door gate reads the shell form, M9.61); a bare redirect, and a group's or compound's own redirect or heredoc, reach the commands inside; a `cd` that may not run leaves the directory unknown; a path through a symlink then `..` read as the kernel walks it (`git -C`, `cd -P`, a dsh `workdir`); settings replaced, moved, linked or removed by `cp`/`mv`/`ln`/`install`/`rsync` (into a settings directory too, by `-t` or a trailing `DIR/`), `rm`/`mv` of a directory that holds them, a glob that names them, `sed -i` (and `--in-pl`), `perl -i`; `opencode.json` a door only where OpenCode reads it (a nested checkout's root, `~/.config/opencode`, its `config.json`); an MCP tool of a hyphenated server or an aggregator (`mcp__zapier__gmail_send_email`, `mcp__google-drive__share_file`) crosses the same door as the service's own server, while a service word inside another tool's name (`search_slack_posts`, `get_post_history`) is none. The third review: a `cd` that may not run leaves each later command in either directory (one view per directory, not an unknown one), `cd -`, a bare `pushd`, `popd -n`, `cd X &`, an eval's `cd`, `~+` and `GIT_DIR`/`GIT_WORK_TREE` are read as the shell reads them, and a pipe into a `{ }` group or a compound reaches each command inside; a copy is placed where it runs (DEST/NAME only into an existing directory, so a backup of `.claude` or a file copied into it passes; `-t` with `{}` or xargs' arguments, `--parents`, `-R`, `--remove-source-files`, a link to a settings file, a home directory's contents, GNU option abbreviations); a glob is expanded only where the shell expands it, through links, and never loses a config-like name; another checkout's run home is not this project's; and Composio, kebab-case, scheduled and verb-first sends, GitLab and Bitbucket merges and Google Workspace's Drive grants cross their doors. The fourth: a copy option that takes no value (`rsync --checksum`, `install --strip`) is no prefix of one that takes one, and only an operand right after an unknown option may be its value, so only then does the destination widen (to at most 8 operands: 600 files and a trailing `-f` stay fast); a glob source names the files it matches now, a contents copy (`SRC/.`, rsync's `SRC/`) its entries, and a name the gate cannot see (`"$f"`, `{}`) is `DEST/?`: no settings change at the project's, a checkout's or the home root, but one into a settings or run-record directory; `ln -n` over a linked settings directory replaces it; `./.claude` after an unknown `cd` and `"$REPO/.claude"` are read by name; after `cd A || cd B` the line is in A, B or where it started; OLDPWD follows a `cd` that may not run and survives a subshell; `cd X || { …; exit 1; }` and `&& ( … ) &&` keep the line in X (the shell reader carries the operator after a subshell or compound, `Mark.end`), and a group after `||` starts where the left side failed; a wrapped `cd` (`nohup`, `env`, `flock`) moves no shell; candidate directories pass into nested reads instead of multiplying them, and a path is placed once per directory; a tool that sends a draft sends (`send-shared-mailbox-draft`) and Drive's `grant_batch` shares. The fifth, a regression hunt on the fourth's fixes: `cp --` no longer crashes the gate; an exit inside `|| { … }` counts only when it always runs; past `&&` or a compound, `|| Y` is reached from where the list succeeded too; a second `cd` that may not run keeps where the list stopped; a background list (`cd X && npm run dev &`) moves no shell; `find -execdir` runs in an unknown directory and `env -C DIR` in DIR; a glob or contents copy also names what the line staged before it and the dotfiles a `*` may name (dotglob); `cp -T` and `rsync -R SRC/./` copy SRC's entries; an absolute path is placed after an unknown `cd`; `$(pwd)` is `$PWD`, `${X:?}` is `$X`, and a run record under a variable directory is that record; a variable directory is a settings root only for `$HOME` (so `cp -a . "$tmp"` passes); an unseen name copied into `docs/agents/mission/` forges the ledger; a large directory's copy is placed by its directory once; and a tool that writes a draft (`send_mail_draft`, `forward_as_draft`) does not send. The sixth, a regression hunt on the fifth's fixes: the shell that reads a word expands its `$PWD` and `$(pwd)`, so the text a nested shell reads (`sh -c "…"`, `eval`, `<<<`, an unquoted heredoc, a pipe) keeps the outer directory, and `env -C`, `git -C` or `find -execdir` move the command but not its words; `echo … && exit` and `return 1 || exit 1` always exit; a source directory the gate cannot list now (missing, or empty) holds more than what the line visibly wrote into it (tar, `curl -o` and unzip are unseen); a copy onto `"$VAR/.claude"` may become that directory; a variable last name in a guarded directory (`.aladdin/logs/$F`) is any name; a glob over a large directory lists it once (95,000 files: 1.1 s to 0.4 s); and a draft writer's name ends in message, mail, email or as, then draft, after a separator (so `send_gmail_draft` sends). The seventh, on the sixth's: the outer directory in a nested shell's text is a path only where it starts a word, so `bash -c "make PREFIX=$PWD/out"` and `make PREFIX=$(git rev-parse --show-toplevel)/out` (a crash since M9.30) no longer stop the gate; a nested `cd $PWD` is the line's own checkout, so a `git checkout main` before it still counts; after a `cd` that may not run, a nested `$PWD` is read in each directory; a copy into `"$PWD"` is into the directory it is, and a regular file never becomes `"$VAR/.claude"`; and only a wholly substituted last name is any name (`settings.json.bak.$(date +%s)` is a backup). Tests: `mission-doors.test.py` (49 checks), `opencode-plugin.test.mjs`, `dsh-plugin.test.mjs`, `codex-hooks.test.py`, `hook-matchers.test.py`, `mission-principles.test.py`, `dsh-install.test.py`. Calls: the catalogue keeps its generic `_` alternatives for an OpenCode that cannot list its servers (an extra question beats a silent one-way door). Open: M9.56–M9.65 (the `!` measurement is M9.58) | | M9.32 | Intent questions name their door: `open_questions[].door`; `intent check` blocks an assume/default on a one-way door and treats an `ask` on a door the human authorized as answered. Reason: the plan-time half of the door rule | M9.30, M9.11 | `plugin/bin/intent`, its test | yes | open | | M9.33 | Remove the model-facing `SKIP_HOOKS=1` hints (`gate-supervisor-scope`, `_failclosed.sh`, `SKILL.md`). Reason: they hand the agent the bypass-gate wall's own key | M9.30 | those files | yes | open | | M9.34 | A git pre-push backstop from `aloop init` for pushes to the default branch and force pushes. Reason: holds when Bash matching is evaded (a script, an alias) | M9.30 | `plugin/bin/aloop-init`, its test | yes | open | @@ -516,7 +516,7 @@ Each task flips only its own row here and in TRACKING.md. | M9.62 | In the OpenCode plugin only the door gate has a fail-closed timeout: a hung host-adapter, scope or budget gate waits 10 minutes and its timeout reads as an allow (no exit status, no deny). Give each gate the hook's timeout and fail closed. Reason: the M9.31 review | M9.31 | `plugin/hosts/opencode/aloop.js`, its test | yes | open | | M9.63 | An OpenCode that cannot list its MCP servers (`client.mcp.status()` fails) hands the gates raw `_` names, which the catalogue's generic alternatives read with false positives (`drive_get_share` asks). Read the server names from the config too. Reason: the M9.31 review | M9.31 | `plugin/hosts/opencode/aloop.js` | yes | open | | M9.64 | A path the shell computes inside a patch or a write target: an unquoted heredoc's `$HOME`/`$(…)` in an `apply_patch` header, or `> "$D/settings.json"`, is judged as written (M9.47's class). Expand what the line assigns before it, and ask for a computed path under a settings directory. Reason: the second M9.31 review | M9.47 | `plugin/mission/doors.py`, its test | yes | open | -| M9.65 | What the third M9.31 review left in the door gate's reader: a heredoc or redirect on a `while`/`until` loop reaches its body but not its condition (`while bash; do …; done <"): - stdin = (w.body if w.quoted or w.body is None else OUTER_PWD.sub(lambda _: outer(here), w.body)) \ + stdin = (w.body if w.quoted or w.body is None else OUTER_PWD.sub( + lambda m: outer(here, m.group().startswith(("$(", "`"))), w.body)) \ if op in ("<<", "<<-") else nested(render(w.parts, True), here) if op == "<<<" else None inherited = False if own is not None: # the group's own redirects: its commands write and read them @@ -734,6 +740,11 @@ def contents(s): # the entries of SRC/. ( return [base + "/" + n for n in keep[:4 * GLOB_MAX]] + ([base + "/*"] if len(keep) > 4 * GLOB_MAX else []) + ( [base + "/?"] if not now and names else []) # empty now: the line filled it, not only by what it shows + def a_file(s): # a regular file now: it never becomes a directory + w = where_of(v, s) + f = resolve(cwd, w) if w != UNKNOWN else "" + return bool(f) and os.path.isfile(f) + def a_dir(s): # rsync copies a directory into DEST, as DEST/NAME w = where_of(v, s) f = resolve(cwd, w) if w != UNKNOWN else "" @@ -741,7 +752,8 @@ def a_dir(s): # rsync copies a directo if plan["whole"]: # cp -T SRCDIR DEST: DEST gets SRCDIR's entries return out + [dest] + [w for s in srcs if plan["prog"] == "cp" and a_dir(s) for w in contents(s.rstrip("/") + "/.")] - where = where_of(v, dest) + where = chdir(v["dir"], dest, v.get("shell")) if re.match(r"\$(?:PWD|CLAUDE_PROJECT_DIR)(?:/|$)", dest) \ + else where_of(v, dest) # "$PWD": the directory the shell is in full = resolve(cwd, where) if where != UNKNOWN else "" if plan.get("nodir") and full and not dest.endswith("/") and os.path.islink(full): return out + [dest] # ln -sfn X LINK: the link itself is replaced @@ -782,7 +794,8 @@ def inside(s): if rsync and not s.endswith("/") and a_dir(s): return out + inside(s) return out + [dest] + (contents(s) if s.endswith("/.") or rsync and s.endswith("/") else []) - becomes = isdir is None and re.search(r"[$`]", dest) and len(srcs) == 1 and not rsync and not srcs[0].endswith("/.") + becomes = isdir is None and re.search(r"[$`]", dest) and len(srcs) == 1 and not rsync and not srcs[0].endswith("/.") \ + and not a_file(srcs[0]) # cp -r X "$WT/.claude": X may become that directory return out + [dest] + ([base + "/*"] if becomes else []) + [w for s in srcs or [STDIN] for w in inside(s)] @@ -1041,6 +1054,8 @@ def resolve(cwd, where, kernel=False): where = toplevel(cwd) + where[len(TOP):] if where.startswith(CWD): where = where[len(CWD):].lstrip("/") or "." + if "\0" in where: + return "" # a mark inside a word (PREFIX=$(git rev-parse --show-toplevel)/x): no path where = os.path.join(cwd, os.path.expanduser(where)) return os.path.realpath(where) if kernel else os.path.normpath(where) @@ -1125,7 +1140,7 @@ def placed(cwd, top, v, path, kernel=False, here=None): return path if re.match("(?:%s|%s|\\$PWD|\\$CLAUDE_PROJECT_DIR)(?:/|$)" % (re.escape(TOP), re.escape(CWD)), path): where = chdir(v["dir"], path, v.get("shell")) - elif re.search(r"[$`]", path) or v["dir"] == UNKNOWN and not (os.path.isabs(path) or path.startswith("~")): + elif re.search(r"[$`\0]", path) or v["dir"] == UNKNOWN and not (os.path.isabs(path) or path.startswith("~")): where = UNKNOWN else: where = path if os.path.isabs(path) or path.startswith("~") or not v["dir"] else v["dir"] + "/" + path @@ -1151,7 +1166,7 @@ def placed(cwd, top, v, path, kernel=False, here=None): def where_of(v, path): """Where a command's path operand is, as resolve() takes it (UNKNOWN: the gate cannot tell).""" - if re.search(r"[$`]", path) or "\n" in path or len(path) > 4096: + if re.search(r"[$`]", path) or "\n" in path or len(path) > 4096 or "\0" in path[1:]: return UNKNOWN if os.path.isabs(path) or path.startswith(("~", CWD)): return path # where it says, wherever the command runs diff --git a/scripts/test/mission-doors.test.py b/scripts/test/mission-doors.test.py index 221a353f..796e15ec 100644 --- a/scripts/test/mission-doors.test.py +++ b/scripts/test/mission-doors.test.py @@ -1325,6 +1325,46 @@ def swallowing(*a, **k): ok("MCP: a draft writer is one whose last words are message, mail, email or as, then draft") shutil.rmtree(os.path.dirname(stage), ignore_errors=True) shutil.rmtree(os.path.join(p.dir, "dist"), ignore_errors=True) +# 21g) the seventh M9.31 review (of the sixth's fixes): this shell's directory in a nested shell's text is a path +# only where it starts a word (`PREFIX=$PWD/out`, and `$(git rev-parse --show-toplevel)` inside a word, no +# longer crash the gate); a nested `cd $PWD` is the line's own checkout (its branch switch too); after a cd +# that may not run, a nested $PWD is each directory; a copy into "$PWD" is into the directory it is; only a +# whole substituted name is any name (`settings.json.bak.$(date +%s)` is a backup) +open(os.path.join(p.dir, "notes.md"), "w").write("n") +for cmd in ("bash <> .env\"", "bash -c \"pip install -e file://$PWD\"", "eval \"echo cwd=$(pwd)\"", + "bash <<< \"echo dir:$PWD\"", "cat < .aladdin/logs/gates-$(date +%F).jsonl.gz", + "cp .claude/settings.json .claude/settings.json.bak.$(date +%s)", "rm -f .claude/settings.json.bak.$TS", + "echo \"$out\" > .claude/session-$SESSION.log", "rm -rf ~/.config/myapp-$VERSION"): + assert doors_of(cmd) == [], (cmd, doors_of(cmd)) +for cmd in ("[ -d sub ] && cd sub; bash -c \"rm -rf $PWD/.aladdin\"", "if [ -d sub ]; then cd sub; fi; bash -c \"rm -rf $PWD/.aladdin\"", + "cd sub || true; bash -c \"rm -rf $PWD/.aladdin\"", "[ -d sub ] && cd sub; bash <