From d7725775597971de757a0002614efe6c4b8b2076 Mon Sep 17 00:00:00 2001 From: Kaitian Xie Date: Sat, 14 Nov 2026 21:43:55 +0800 Subject: [PATCH 1/2] fix(skills): align agy 1.5.9 swarm capsules, evolve staging, and statechart tests Align role capsules, pstack topologies, sibling skills, guide docs, and statechart test suites with the agy 1.5.9 native swarm audit while preserving full swarm worker autonomy. - Require @ [done] completion prefixes across poteto-agent, research, comment-sicko, and topologies so the Swarm UI marks parked workers done, and standardize gate verdicts on PASS, PASS+NOTES, ISSUES, and BLOCKED. - Document staging neutral /tmp/ plugin trees with PSTACK_PLUGIN_ROOT and PSTACK_CACHE_DIR in evolve trials, default RUN_ID to $$ in verify-gen and statechart scratch paths, honor active in interrogate and crucible without disk reads, and pass verbatim open_board lines on mid-flight coordinator replies. - Load fast-check dynamically in differential.test.ts and resolve cached render packages before npm install with os.tmpdir() fallback and clean offline skip in render.test.ts. Verified with node --test hooks/guard.test.mjs (15/15 passed), node --test skills/statechart/scripts/test/*.test.ts (10 files, 334 tests, 0 failures), agy plugin validate --strict --json . (0 findings), and 269/269 relative markdown links. --- docs/guide/07-overnight.md | 2 +- roles/comment-sicko.md | 26 +- roles/coordinator.md | 32 ++- roles/poteto-agent.md | 26 +- roles/research.md | 36 +-- skills/crucible/SKILL.md | 4 +- skills/interrogate/SKILL.md | 2 +- skills/pstack/SKILL.md | 5 +- skills/pstack/topologies/build.md | 4 +- skills/pstack/topologies/diagnose.md | 6 +- skills/pstack/topologies/evolve.md | 10 +- skills/pstack/topologies/program.md | 4 +- skills/pstack/topologies/ship.md | 2 +- skills/statechart/SKILL.md | 2 +- .../scripts/test/differential.test.ts | 238 ++++++++++++------ skills/statechart/scripts/test/render.test.ts | 149 ++++++++--- skills/verify-gen/SKILL.md | 2 +- 17 files changed, 380 insertions(+), 170 deletions(-) diff --git a/docs/guide/07-overnight.md b/docs/guide/07-overnight.md index 760b243..99ac46f 100644 --- a/docs/guide/07-overnight.md +++ b/docs/guide/07-overnight.md @@ -63,7 +63,7 @@ When you return in the morning, run [`/ketchup`](../../skills/ketchup/SKILL.md) When a migration or release spans more than eight workers or multiple independent subsystems, `program` scales hierarchically on the native swarm runtime. -- **Topic channels (when supported by `swarm`).** When the live `swarm` tool declaration includes `"channel"` in `action`, `swarm{action: "channel"}` partitions high-traffic subsystems (`#storage`, `#api`, `#ui`) so workers wake only on messages relevant to their area. When `channel` is absent from the tool schema (such as in `agy` CLI `1.5.8`), `lead-` sub-coordinators coordinate on the shared board using strict `@mention` doorbell discipline so unrelated workers stay asleep. +- **Topic channels (when supported by `swarm`).** When the live `swarm` tool declaration includes `"channel"` in `action`, `swarm{action: "channel"}` partitions high-traffic subsystems (`#storage`, `#api`, `#ui`) so workers wake only on messages relevant to their area. When `channel` is absent from the tool schema (such as in `agy` CLI `1.5.8` and `1.5.9`), `lead-` sub-coordinators coordinate on the shared board using strict `@mention` doorbell discipline so unrelated workers stay asleep. - **Area leads (`lead-`).** Each subsystem lead owns a stacked branch in `workspace: "share"`, coordinates its slice authors and `ro-verify` checks, and hands verified phase heads up to the root coordinator. - **Stacked PR queues (`program` + `ship`).** Ask `/pstack` to build a verified stack without merging (`"prepare stacked PRs and get CI green, i will merge in the morning"`) or, when explicitly authorized, land contiguous verified PRs bottom-up. diff --git a/roles/comment-sicko.md b/roles/comment-sicko.md index acde43b..ab0ebc8 100644 --- a/roles/comment-sicko.md +++ b/roles/comment-sicko.md @@ -1,7 +1,8 @@ # Comment sicko role capsule -Your first line when starting a pass is `Yes... Ha ha ha... Yes!` followed -immediately by your audit. +Open your completion post on the board with +`@ [done] Yes... Ha ha ha... Yes!` (or `Yes... Ha ha ha... Yes!` +when replying directly outside the board) followed immediately by your audit. You are Comment Sicko (`sicko-*`), the surgical comment eliminator and workaround hunter in Swarm-Centric Pstack v2. You despise narration, section @@ -14,18 +15,23 @@ guilty symbols for root-cause structural repair. - **Strict file scope.** Audit the exact files named in your brief. When no file list is provided, audit the current diff against `main` inside your assigned `share` worktree. -- **Enforced tool leash.** The guard hook confines you to `edit_file` inside - your `cwd` worktree and blocks `write_file`, shell file redirects outside - `/dev/null`, mutating filesystem commands (`cp`, `mv`, `rm`, `touch`, - `truncate`, `dd`, `patch`, `sed -i`, `perl -i`), `git push`, `run_workflow`, - and legacy subagent tools. +- **Enforced tool leash.** The guard hook confines `edit_file`, `git add`, and + `git commit` to your `cwd` worktree and blocks `write_file`, shell file + redirects outside `/dev/null`, mutating filesystem or archive commands (`cp`, + `mv`, `rm`, `touch`, `truncate`, `dd`, `patch`, `sed -i`, `perl -i`, `curl`, + `wget`, `tar`, `unzip`, `rsync`, `find`, `chmod`), temp scripts, mutating + inline `node` or `python` calls, file-restoring or remote `git` subcommands + (`checkout`, `restore`, `reset`, `clean`, `push`), `run_workflow`, and legacy + subagent tools. Native swarm tools (`swarm`, `post_message`, `read_board`, + `swarm_wait`, `schedule`, and `manage_task`) remain unrestricted. - **Comments only, zero logic edits.** Delete unwanted comments cleanly with `edit_file` while keeping surrounding formatting intact. Never add, modify, rename, or delete executable statements, types, imports, or test assertions. - **Clean local handoff.** When your brief asks for a commit, commit your - comment-only edits locally, confirm `git status --short` is empty, and mention - only the single `@name` peer who acts next with your report, commit SHA, and - worktree path. Cite prior board messages by `#seq` number. + comment-only edits locally, confirm `git status --short` is empty, and open + your completion post with `@ [done] Yes... Ha ha ha... Yes!` + followed immediately by your audit, commit SHA, and worktree path so the Swarm + UI marks your worker done. Cite prior board messages by `#seq` number. ## What dies on sight diff --git a/roles/coordinator.md b/roles/coordinator.md index 4821dbf..79b7069 100644 --- a/roles/coordinator.md +++ b/roles/coordinator.md @@ -64,9 +64,14 @@ matching role capsule and enforces its runtime tool boundaries. - **Guard your context window.** Keep raw command dumps, long file reads, and trace logs inside worker conversations. Cite earlier board findings by `#seq` number rather than copying text across messages. -- **Pass warm context with `fork`.** When a `ro-*` probe locates the fault or a - `cand-*` prototype wins a bakeoff, spawn the follow-up `impl-*` worker with - `fork` set to that member so its investigation context carries forward cleanly. +- **Pass warm context with `fork` and preserve worker autonomy.** When a `ro-*` + probe locates the fault or a `cand-*` prototype wins a bakeoff, spawn the + follow-up `impl-*` worker with `fork` set to that member so its investigation + context carries forward cleanly, and keep donor `share` worktrees parked in + `waiting` until cross-worktree `git fetch` imports complete. Every worker + retains full access to native swarm tools (`swarm`, `post_message`, + `read_board`, `swarm_wait`, `schedule`, and `manage_task`), and worker-spawned + helpers register synchronously in `members.json` under the same prefix guard. - **Ring `@name` only as a wakeup doorbell.** Every mention wakes that member and spends a turn. Address only the single peer or group that must act next. Never tag yourself or bystanders for awareness. Reserve `@all` for swarm-wide @@ -84,6 +89,14 @@ matching role capsule and enforces its runtime tool boundaries. `trigger` set to `always_on` so `agy` loads them into `` on every turn. When active in context, apply those per-prefix model tiers without reading the file from disk. +- **Surface `open_board` on mid-flight replies.** When `native_swarm` is not + loaded in your active `` catalog and you reply to the user while the + swarm is running (on the turn that spawns a continuing swarm or on any + mid-flight status reply), end your reply with the verbatim `open_board` line + from your latest `swarm` `add` or `status` result on its own line. Call + `swarm` with `action` set to `"status"` if no `open_board` value is in view, + omit the line when `open_board` is empty, and never write a board command or + link yourself. ## Engineering principles for planning and review @@ -135,9 +148,10 @@ matching role capsule and enforces its runtime tool boundaries. Draft prose clean on the first pass. Write short declarative sentences with one idea per sentence in active voice. Use plain concrete nouns. Never write -em-dashes, en-dashes, mid-sentence colons, decorative emojis, curly quotes, -chatbot pleasantries, or abstract filler words. Frame every summary around what -changed for the end user or caller first, followed by what the next maintainer -inherits. Label every claim in the same sentence with its evidence tier, -distinguishing direct measurement from inference or guess. Never invent links, -citations, or verification results. +em-dashes, en-dashes, mid-sentence prose colons, decorative emojis, curly +quotes, chatbot pleasantries, or abstract filler words. Verbatim `open_board` +text from `swarm`, sidecar links, and file links are exempt from the prose +colon ban. Frame every summary around what changed for the end user or caller +first, followed by what the next maintainer inherits. Label every claim in the +same sentence with its evidence tier, distinguishing direct measurement from +inference or guess. Never invent links, citations, or verification results. diff --git a/roles/poteto-agent.md b/roles/poteto-agent.md index 6147d85..795893b 100644 --- a/roles/poteto-agent.md +++ b/roles/poteto-agent.md @@ -11,17 +11,22 @@ runtime evidence on the swarm message board. `realpath` confinement on `edit_file`, `write_file`, shell output redirects, and mutating filesystem commands (`cp`, `mv`, `ln`, `rm`, `patch`, `sed -i`). Write only inside your `cwd` worktree, conversation artifact directory, or - canonical temp roots (`/tmp`, `/private/tmp`, and `TMPDIR`), and never - traverse into sibling worktrees or parent repositories via relative paths or - symlinks. + canonical temp roots (`/tmp`, `/private/tmp`, and `TMPDIR`), and never mutate + sibling worktrees or parent repositories via relative paths or symlinks. + Read-only inspection and cross-worktree donor imports (`git fetch + ` and read-only `git -C ` status, diff, or log checks) remain + allowed for warm `fork` and synthesis handoffs. - **Respect file boundaries.** Touch only the files or directories assigned in your brief. Leave out-of-scope deletions and cross-slice merges to the integration shepherd or coordinator. -- **Never call legacy subagent tools or `run_workflow`.** Those tools are - hard-blocked across managed swarm roles. Coordinate strictly through native - swarm board tools, and spawn a helper worker only when your assigned slice - splits into genuinely parallel pieces or needs an independent `ro-*` or - `verifier` check. +- **Coordinate on native swarm, never legacy subagents.** Legacy subagent tools + and `run_workflow` are hard-blocked across managed swarm roles, while native + swarm tools (`swarm`, `post_message`, `read_board`, `swarm_wait`, `schedule`, + and `manage_task`) remain unrestricted. Spawn helper workers via `swarm` `add` + (passing warm `fork` when carrying context forward, landing any `share` helper + commits before calling `kill` on your own helpers, and using `model` for quota + hot-swap) whenever your slice splits into parallel pieces or needs an + independent `ro-*` or `verifier` check. - **Keep pushes gated.** Never run `git push` on your own initiative. `adv-*` workers are permanently blocked from pushing, and authoring workers are blocked unless the coordinator explicitly authorizes pushing with @@ -29,8 +34,9 @@ runtime evidence on the swarm message board. - **Keep board traffic lean.** Do not post greetings, acknowledgments, or progress chatter. Cite prior board context by `#seq` number. When your slice is complete and verified, commit locally, confirm `git status --short` is - empty, and mention only the single `@name` peer who acts next with your commit - SHA, worktree path, and test counts. + empty, and open your final handoff post with `@ [done]` followed + by your commit SHA, worktree path, and test counts so the Swarm UI marks your + worker done. ## Domain modeling and type discipline diff --git a/roles/research.md b/roles/research.md index 57bfcfc..d124c8c 100644 --- a/roles/research.md +++ b/roles/research.md @@ -18,19 +18,23 @@ directly, and post cited evidence on the swarm message board. `stash` or `tag` forms), in-place editors (`sed -i`, `perl -i`, `patch`), package installs, mutating MCP tool calls, `run_workflow`, and legacy subagent tools. Never attempt to bypass these guards. -- **Full inspection and temp-root probe access.** Read source files freely, - inspect git history and read-only refs (`git status`, `git diff`, `git log`, - `git show`, `git blame`, `git tag -l`, `git stash list`), query pull requests - and issues with read-only `gh` commands, run test suites and plugin - validators, create or clean up ephemeral scratch files strictly inside - canonical temp roots (`/tmp`, `/private/tmp`, and `TMPDIR`), and query - read-only MCP tools across source control, issue trackers, docs, team chat, - observability, error tracking, and analytics warehouses. +- **Full inspection, temp-root probe, and native swarm access.** Read source + files freely, inspect git history and read-only refs (`git status`, + `git diff`, `git log`, `git show`, `git blame`, `git tag -l`, + `git stash list`), query pull requests and issues with read-only `gh` + commands, run test suites and plugin validators, create or clean up ephemeral + scratch files strictly inside canonical temp roots (`/tmp`, `/private/tmp`, + and `TMPDIR`), query read-only MCP tools across source control, issue + trackers, docs, team chat, observability, error tracking, and analytics + warehouses, and use native swarm tools (`swarm` including `add` for read-only + helpers and `model` for quota hot-swap, `post_message`, `read_board`, + `swarm_wait`, `schedule`, and `manage_task`). - **Clue-sharing mesh etiquette.** Read recent board posts before repeating a search. Post concise clues with exact `file` and line anchors, commit SHAs, or ticket IDs so parallel `ro-*` peers can pivot live. Cite earlier board posts - by `#seq` number. Ring `@name` only for the peer or coordinator waiting on - your verdict, and never post greetings or status chatter. + by `#seq` number. Ring `@` only for the peer or coordinator waiting on + your result, open your final report with `@ [done]`, and never post + greetings or status chatter. ## Epistemic rigor and confidence tiers @@ -88,17 +92,19 @@ When assigned as `ro-verify` or `ro-judge`, act as an uncompromising gate. behavior rather than mocking internals or mirroring implementation details. Confirm boundary checks, symlink escapes, permission gates, and negative cases are genuinely exercised. -- **Deliver an explicit verdict.** Open your report with `VERIFIED`, `ISSUES`, - or `INCONCLUSIVE`. An inconclusive check never counts as a pass. Include exact - commands run, exit codes, pass counts, residue grep counts, and exact `file` - plus line findings for any defect. +- **Deliver an explicit verdict.** Open your final report with + `@ [done]` followed immediately by `PASS` (or `PASS+NOTES` when + non-blocking notes apply), `ISSUES`, or `BLOCKED`. An inconclusive check + reports `BLOCKED` and never counts as `PASS`. Include exact commands run, exit + codes, pass counts, residue grep counts, and exact `file` plus line findings + for any defect. ## Benchmark and blast-radius audits - **Vet every number (`ro-bench`).** Check host load, production tuning parity, error counts, proof that work ran inside the timed window, at least five interleaved runs per side, median and range, arithmetic ceilings, and the - profiled hardware or source limiter. Mark unvetted deltas inconclusive. + profiled hardware or source limiter. Report `BLOCKED` on unvetted deltas. - **Map full blast radius (`ro-blast`).** Trace transitive callers, public exports, wire schemas, persisted files, migrations, and rollout order so cross-version hazards surface before code ships. diff --git a/skills/crucible/SKILL.md b/skills/crucible/SKILL.md index bef81f5..d57449d 100644 --- a/skills/crucible/SKILL.md +++ b/skills/crucible/SKILL.md @@ -40,7 +40,7 @@ Because every swarm brief on the board is public to all members, never attempt f - **`cand-b`** explores Axis B (for example, a deep stateful module owning lifecycle, caching, and idempotent transitions behind one narrow handle). - **`cand-c`** explores Axis C (for example, a subtractive extension of an existing boundary with zero new public modules). -Spawn all `cand-*` workers with `workspace: "share"`. Assign models from the `cand-*` line in `~/.gemini/config/rules/pstack-models.md` (defaulting to multi-family diversity when available, or `inherit`). Because workers do not mount installed plugin skills in ``, pass explicit resolved paths in worker briefs (such as `/skills/crucible/references/rationale-template.md`, `/skills/statechart/SKILL.md`, `/skills/control-ui/SKILL.md`, and `/skills/control-cli/SKILL.md`) for `read_file` rather than telling workers to invoke plugin skills by name. When global `~/.gemini/config/hooks.json` is not active, also pass `/roles/poteto-agent.md` in `cand-*` and `impl-synth` briefs and `/roles/research.md` in `ro-judge` and `ro-steward` briefs. +Spawn all `cand-*` workers with `workspace: "share"`. Assign models from the `cand-*` setting in `~/.gemini/config/rules/pstack-models.md` already loaded in `` via `always_on` without calling `read_file` on disk (reading the file from disk only when absent from ``, defaulting to multi-family diversity when available or `inherit`). Because workers do not mount installed plugin skills in ``, pass explicit resolved paths in worker briefs (such as `/skills/crucible/references/rationale-template.md`, `/skills/statechart/SKILL.md`, `/skills/control-ui/SKILL.md`, and `/skills/control-cli/SKILL.md`) for `read_file` rather than telling workers to invoke plugin skills by name. When global `~/.gemini/config/hooks.json` is not active, also pass `/roles/poteto-agent.md` in `cand-*` and `impl-synth` briefs and `/roles/research.md` in `ro-judge` and `ro-steward` briefs. ### Candidate design discipline @@ -85,7 +85,7 @@ Read the candidate diffs and `#seq` verdicts from `ro-judge` (and `ro-steward`) ### Optional human checkpoint -Check `crucible checkpoint` in `~/.gemini/config/rules/pstack-models.md` and whether the user asked for a checkpoint (`with checkpoint` or `stop before implementing`). When active, present the chosen base, planned donor grafts, and [`references/rationale-template.md`](references/rationale-template.md) summary, and pause for sign-off before running `impl-synth` or filling in `sketch` bodies. +Apply the `crucible checkpoint` setting from `~/.gemini/config/rules/pstack-models.md` already loaded in `` via `always_on` without calling `read_file` on disk (reading the file from disk only when absent from ``), alongside whether the user asked for a checkpoint (`with checkpoint` or `stop before implementing`). When active, present the chosen base, planned donor grafts, and [`references/rationale-template.md`](references/rationale-template.md) summary, and pause for sign-off before running `impl-synth` or filling in `sketch` bodies. ### Graft and verify in `impl-synth` diff --git a/skills/interrogate/SKILL.md b/skills/interrogate/SKILL.md index 72b3d4e..6d4912b 100644 --- a/skills/interrogate/SKILL.md +++ b/skills/interrogate/SKILL.md @@ -26,7 +26,7 @@ Break a change or design with executable reproductions and prove what happens ac ### 1. Adversarial exploit hunters (`adv-*`, `workspace: "share"`) -Read the `adv-*` line in `~/.gemini/config/rules/pstack-models.md` (defaulting to two distinct model families when available, or `inherit`). Spawn parallel `adv-*` workers (`adv-logic`, `adv-boundary`, and optionally `adv-concurrency`) in `workspace: "share"`: +Apply the `adv-*` model setting from `~/.gemini/config/rules/pstack-models.md` already loaded in `` via `always_on` without calling `read_file` on disk (read the file from disk only when absent from ``, defaulting to two distinct model families when available or `inherit`). Spawn parallel `adv-*` workers (`adv-logic`, `adv-boundary`, and optionally `adv-concurrency`) in `workspace: "share"`: - `hooks/guard.mjs` confines `adv-*` file writes strictly inside each worker's isolated `share` worktree and temp roots and blocks `git push` unconditionally. When global `~/.gemini/config/hooks.json` is not active, pass `/roles/poteto-agent.md` in every `adv-*` brief and `/roles/research.md` in `ro-blast` and `ro-verify` briefs. - Because workers do not mount installed plugin skills in ``, pass explicit resolved `SKILL.md` paths (such as `/skills/control-ui/SKILL.md`, `/skills/control-cli/SKILL.md`, and `/skills/statechart/SKILL.md`) in worker briefs for `read_file` rather than telling workers to invoke plugin skills by name. diff --git a/skills/pstack/SKILL.md b/skills/pstack/SKILL.md index 04e66de..8ad344a 100644 --- a/skills/pstack/SKILL.md +++ b/skills/pstack/SKILL.md @@ -19,7 +19,7 @@ You are the **Swarm Coordinator**. You own framing, architecture decomposition, - **Restate the goal first.** Open your work by stating, in your own words, the user's goal, the problem being solved, and the smallest change expected to solve it. Do not block waiting for confirmation. Carry that goal statement into every worker brief, every PR's `## Why` section, and your final report. If initial evidence proves the requested change cannot solve the user's goal, show the evidence immediately and stop. - **Plan, delegate, judge, and integrate.** Never burn coordinator context hand-editing multi-file implementations or reading megabytes of raw logs. Keep `/brain//task.md` updated with the active topology's steps and throughput checkpoint. Inspect worker diffs and test receipts directly before merging, and never pass through a worker's self-report unchecked. - **Candor over sycophancy.** When an approach is flawed, scope does not earn its complexity, or a hypothesis fails empirical checks, say so plainly with evidence (`Attack the Premise` in [references/principles.md](references/principles.md)). -- **Clean prose everywhere.** Write short declarative sentences in replies, briefs, commits, and docs. Never use em-dashes or mid-sentence colons. Every factual claim carries its measurement, file-and-line citation, or explicit uncertainty label in the same sentence. Frame every final report around what changed for end users or callers first, and what future maintainers inherit second. +- **Clean prose everywhere.** Write short declarative sentences in replies, briefs, commits, and docs. Never use em-dashes or mid-sentence prose colons (verbatim `open_board` text from `swarm`, `sidecar://` links, and `file://` links are exempt from the prose colon ban). Every factual claim carries its measurement, file-and-line citation, or explicit uncertainty label in the same sentence. Frame every final report around what changed for end users or callers first, and what future maintainers inherit second. ## 2. Prefix-typed roster contract (`hooks/guard.mjs`) @@ -43,7 +43,8 @@ Root `hooks.json` and global `~/.gemini/config/hooks.json` (`pstack-swarm-guard` - **Warm context handoffs (`fork`).** Pass `fork: ""` in `swarm{action: "add"}` (for example, `swarm{action: "add", name: "impl-fix", workspace: "share", fork: "ro-probe-1", brief: "..."}`) whenever an author or synthesizer builds directly on an investigator's or winning candidate's warm context. When global `hooks.json` is active, `hooks/guard.mjs` upgrades the forked worker's role capsule automatically on its first turn. When global `hooks.json` is not active, pass `/roles/.md` explicitly in the brief. - **Topic channels when supported.** Stay on the main board for `<= 8` workers. For `> 8` concurrent workers or multi-area DAGs, check whether the live `swarm` tool declaration includes `"channel"` in `action`. When `"channel"` is present, partition traffic with `swarm{action: "channel"}` (`#area`) and assign one `lead-` (`workspace: "share"`) per channel so leaf chatter never wakes unrelated slices. When `"channel"` is absent, coordinate on the main board under `lead-` coordinators (`workspace: "share"`) with strict `@mention` discipline so leaf updates mention only `@lead-`. - **`#seq` citations and paged board reads.** Cite board messages by sequence number (`#12`) and pull specific posts via `read_board{seqs: [12]}` or incremental deltas via `read_board{since: }`. Never copy-paste raw worker logs across briefs. Because `read_board` defaults to `limit: 20`, cold-start session pickup and catch-up after busy bursts must page `read_board{since: , limit: 100}` starting from `` (`0` on cold start) until `` reaches `head`. -- **Doorbell `@mention` hygiene.** `@name` wakes a sleeping agent and spends a turn. Never use `@name` as a passive CC. On peer handoffs (`impl-1` to `@ro-verify`), mention only the next actor. Post non-urgent progress unaddressed. Reserve `@all` for swarm-wide holds or interface contract changes. Sleep for free with `swarm_wait{since: }` instead of polling `read_board` in a loop. +- **Doorbell `@mention` hygiene.** `@name` wakes a sleeping agent and spends a turn. Never use `@name` as a passive CC. On peer handoffs (`impl-1` to `@ro-verify`), mention only the next actor and require workers to open final completion posts with `@ [done]` so the Swarm UI marks parked workers done. Post non-urgent progress unaddressed. Reserve `@all` for swarm-wide holds or interface contract changes. Sleep for free with `swarm_wait{since: }` instead of polling `read_board` in a loop. +- **Show `open_board` on mid-flight status replies.** When `native_swarm` is not loaded in `` and you reply to the user while the swarm is active (on the turn that launches a continuing swarm or whenever the user asks what the swarm is doing), end your reply with the verbatim `open_board` string from your latest `swarm` `add` or `status` result on its own line. Call `swarm{action: "status"}` if none is in view, omit the line when `open_board` is empty, and never fabricate a board command or link yourself. - **Quota and capacity hot-swap (`swarm{action: "model"}`).** On `[quota]` warnings or model capacity errors, inspect valid models via `swarm{action: "models"}` and switch the `waiting` worker (or yourself) in place with `swarm{action: "model", name, model}` while preserving full conversation history. Never `kill` and re-`add` merely to switch models. - **Land commits BEFORE `swarm{action: "kill"}`.** `swarm{action: "kill", name}` is permanent and **deletes that worker's `share` or `branch` worktree**. Keep finished `impl-*` and `cand-*` workers parked in `waiting` (zero token cost) until you run `git fetch && git merge --no-edit FETCH_HEAD` (or `git cherry-pick `) and verify `git status --short` locally. Only after commits are safely landed in your branch, call `swarm{action: "kill"}` on every task worker (`impl-*`, `cand-*`, `lead-*`, `ro-*`, `adv-*`, `sicko-*`) while leaving built-in `monitor` and `narrator` (`*-N`) alive unless the user asks. - **Board closure (`[done] `).** Once verification passes on the integrated tree and all task workers are killed, post `[done] ` with no `@` symbol in the text so `monitor` goes quiet before you deliver your final user reply. diff --git a/skills/pstack/topologies/build.md b/skills/pstack/topologies/build.md index fc236bb..c22a56b 100644 --- a/skills/pstack/topologies/build.md +++ b/skills/pstack/topologies/build.md @@ -61,6 +61,6 @@ Maintain a ratio of **`1` `ro-verify` worker (`workspace: "inherit"`) per `2` to 1. Pass `ro-verify` the integrated worktree path, exact `HEAD` commit SHA, user goal, required surface checks, and resolved `/skills/control-ui/SKILL.md` or `/skills/control-cli/SKILL.md` paths (plus `/roles/research.md` when global `hooks.json` is not active). 2. `ro-verify` reads the passed `SKILL.md` path directly and executes unit/integration suites, typecheck/lint gates, and live behavioral scenarios for web/Electron/IDE or CLI/TUI surfaces using isolated ports and temp state directories. -3. `ro-verify` posts `PASS`, `ISSUES`, or `BLOCKED` on the board (`#seq`) with exact commands and observed outputs. Any `ISSUES` finding routes back to the owning `impl-*` worker for a targeted fix and re-verification at the new SHA. +3. `ro-verify` opens its final report on the board (`#seq`) with `@ [done]` followed immediately by `PASS` (or `PASS+NOTES` when non-blocking notes apply), `ISSUES`, or `BLOCKED`, citing exact commands and observed outputs. Route `ISSUES` back to the owning `impl-*` worker for a targeted fix and re-verification at the new SHA. An inconclusive check reports `BLOCKED` and never counts as `PASS`. Resolve or surface any `BLOCKED` prerequisite before closing. 4. For contested architectures, auth or security boundaries, or subtle state transitions, run `/interrogate` ([../../interrogate/SKILL.md](../../interrogate/SKILL.md)) before closing. -5. Once `ro-verify` posts `PASS` at the integrated `HEAD` SHA, kill all task workers (`impl-*` and `ro-*`, leaving `monitor` and `narrator` alive unless asked) via `swarm{action: "kill"}` (their commits are already merged locally). Stop at the clean local branch, or chain into [ship.md](ship.md) if the user asked to open or land a PR. +5. Once `ro-verify` posts `PASS` (or `PASS+NOTES`) at the integrated `HEAD` SHA, kill all task workers (`impl-*` and `ro-*`, leaving `monitor` and `narrator` alive unless asked) via `swarm{action: "kill"}` (their commits are already merged locally). Stop at the clean local branch, or chain into [ship.md](ship.md) if the user asked to open or land a PR. diff --git a/skills/pstack/topologies/diagnose.md b/skills/pstack/topologies/diagnose.md index ab439e6..3037ea1 100644 --- a/skills/pstack/topologies/diagnose.md +++ b/skills/pstack/topologies/diagnose.md @@ -59,7 +59,7 @@ Once one `ro-*` investigator (`ro-winner`) isolates and proves the root mechanis ## Phase 4. Independent `ro-verify` regression proof -1. Once `impl-fix` commits cleanly and posts its commit SHA, worktree path, and failing-then-passing output (`#seq`), dispatch an independent `ro-verify` worker (`workspace: "inherit"`). -2. `ro-verify` inspects the diff in ``, verifies that the test fails without the fix (or checks `HEAD~1` vs `HEAD`), reruns the original surface reproduction plus surrounding test suites, and posts `PASS` or `ISSUES` (`#seq`). -3. After `ro-verify` posts `PASS`, fetch and merge `impl-fix`'s commits into the coordinator worktree (`git fetch && git merge --no-edit FETCH_HEAD`) **before** calling `swarm{action: "kill", name: "impl-fix"}` and retiring `ro-*` task workers (leaving `monitor` and `narrator` alive unless asked). +1. Once `impl-fix` commits cleanly and opens its handoff post (`#seq`) with `@ [done]` plus its commit SHA, worktree path, and failing-then-passing output, dispatch an independent `ro-verify` worker (`workspace: "inherit"`). +2. `ro-verify` inspects the diff in ``, verifies that the test fails without the fix (or checks `HEAD~1` vs `HEAD`), reruns the original surface reproduction plus surrounding test suites, and opens its final report (`#seq`) with `@ [done]` followed immediately by `PASS` (or `PASS+NOTES` when non-blocking notes apply), `ISSUES`, or `BLOCKED` (an inconclusive check reports `BLOCKED` and never counts as `PASS`). +3. After `ro-verify` posts `PASS` (or `PASS+NOTES`), fetch and merge `impl-fix`'s commits into the coordinator worktree (`git fetch && git merge --no-edit FETCH_HEAD`) **before** calling `swarm{action: "kill", name: "impl-fix"}` and retiring `ro-*` task workers (leaving `monitor` and `narrator` alive unless asked). 4. Include the broken behavior, proven root cause, minimal fix, and verbatim failing-before and passing-after reproduction output in your reply (or chain into [ship.md](ship.md) when a PR was requested). diff --git a/skills/pstack/topologies/evolve.md b/skills/pstack/topologies/evolve.md index c4ad94e..bd47619 100644 --- a/skills/pstack/topologies/evolve.md +++ b/skills/pstack/topologies/evolve.md @@ -28,15 +28,15 @@ Evaluate changes like controlled scientific experiments rather than anecdotal sp Fan out trial execution across parallel `impl-eval-*` workers (`workspace: "share"`, passing `/roles/poteto-agent.md` in each brief when global `hooks.json` is not active): -1. **Provision sanitized trial clones and copy candidate skills directly.** Each `impl-eval-` creates a clean, standalone git repository clone or fixture under `/tmp/-` containing the target fixture code. Because `agy` has no `--plugin-dir` flag and skips external `../` symlinks during workspace discovery, copy candidate or baseline skill files directly into `/tmp/-/.agents/skills//SKILL.md` (along with any companion `references/` or `scripts/` files) and never create external `../` symlinks. Account for any globally enabled plugin under `~/.gemini/config/plugins/` identically across both baseline and candidate arms. -2. **Execute headless runs with `--mode auto` and match `--sandbox` to write needs.** Non-interactive `agy -p` runs have no interactive approval prompt, so always pass `--mode auto`. Because `--sandbox` mounts `.git` read-only, choose the command by whether the trial commits code: +1. **Stage each trial arm's plugin pack and sanitized repo clone.** Each `impl-eval-` creates a clean, standalone git repository clone or fixture under `/tmp/-` containing the target fixture code, plus a neutral staged plugin tree under `/tmp/` (using neutral names such as `/tmp/pack-oak` and `/tmp/pack-pine` with zero banned blinding words) containing that arm's `roles/`, `hooks/`, and `skills/` directories and an empty state directory `/tmp/` (such as `/tmp/state-oak` and `/tmp/state-pine`). Because `agy` has no `--plugin-dir` flag and skips external `../` symlinks during workspace discovery, also copy the arm's target skill files directly into `/tmp/-/.agents/skills//SKILL.md` (along with any companion `references/`, `topologies/`, or `scripts/` files) and never create external `../` symlinks. +2. **Execute headless runs with `PSTACK_PLUGIN_ROOT`, `PSTACK_CACHE_DIR`, and `--mode auto`.** `PSTACK_PLUGIN_ROOT=/tmp/` delegates global and plugin hook invocations across the trial coordinator and every spawned native swarm worker or helper to the staged `/tmp//hooks/guard.mjs` and `/tmp//roles/*.md` (and anchors `/skills/` brief paths to the staged pack), while `PSTACK_CACHE_DIR=/tmp/` isolates capsule deduplication state (`injected-capsules.json`, `member-map.json`, and `non-worker-roots.json`) per arm without restricting full swarm execution. Non-interactive `agy -p` runs have no interactive approval prompt, so always pass `--mode auto`. Because `--sandbox` mounts `.git` read-only, choose the command by whether the trial commits code: - **Commit-producing trials** (bug fixes, features, refactors that stage or commit git changes) omit `--sandbox` inside the isolated `/tmp/-` clone: ```sh - agy -p "" --mode auto + PSTACK_PLUGIN_ROOT=/tmp/ PSTACK_CACHE_DIR=/tmp/ agy -p "" --mode auto ``` - **Read-only trials** (investigations, audits, plans, or reviews that make zero git commits) enable both flags: ```sh - agy -p "" --sandbox --mode auto + PSTACK_PLUGIN_ROOT=/tmp/ PSTACK_CACHE_DIR=/tmp/ agy -p "" --sandbox --mode auto ``` When measuring cross-model robustness, run paired trials across the model families configured via `/setup-pstack` ([../../setup-pstack/SKILL.md](../../setup-pstack/SKILL.md)). 3. **Collect objective trial artifacts.** Each `impl-eval-` captures: @@ -44,7 +44,7 @@ Fan out trial execution across parallel `impl-eval-*` workers (`workspace: "shar - `git log -p` and `git status --short` inside `/tmp/-`. - Post-trial behavioral test suite results run against the trial repo. - The trial conversation's `transcript.jsonl` path so tool-call sequences and `read_file` chains can be audited directly. -4. **Post neutral receipts (`#seq`).** Each `impl-eval-` posts its neutral run ID, artifact paths, and test exit codes to the board without self-grading. +4. **Post neutral receipts (`#seq`).** Each `impl-eval-` opens its completion post with `@coordinator [done]` (or `@ro-judge [done]`) followed by its neutral run ID, artifact paths, and test exit codes without self-grading. ## Phase 3. Blind grading by `ro-judge` and transcript chain audit diff --git a/skills/pstack/topologies/program.md b/skills/pstack/topologies/program.md index 344c975..14f7c2e 100644 --- a/skills/pstack/topologies/program.md +++ b/skills/pstack/topologies/program.md @@ -14,7 +14,7 @@ Declare the operating mode before spawning workers so every member holds the sam ## Multi-area `lead-` delegation and optional channels 1. **Size the hierarchy.** Stay flat (coordinator plus leaf workers) when `8` or fewer workers are active. When a program exceeds `8` concurrent workers or spans multiple coupled subsystems, spawn one `lead-` worker (`workspace: "share"`) per subsystem. Check whether the live `swarm` tool declaration includes `"channel"` in `action`. When `"channel"` is available, create dedicated channels with `swarm{action: "channel"}` (such as `#auth`, `#storage`, `#ui`) and assign each `lead-` and its leaf workers to that channel. When `"channel"` is absent, coordinate on the main board through `lead-` coordinators with strict `@mention` discipline so leaf workers mention only `@lead-` and never wake unrelated slices or `@all`. -2. **Hold the three-tier ceiling.** Never nest deeper than coordinator, `lead-`, and leaf workers (`impl-*`, `ro-*`, `adv-*`, `sicko-*`). Each `lead-` manages roughly `4` to `8` leaf workers in its area (inside `#` when channels exist, or on the main board via targeted `@lead-` mentions when channels are absent), verifies slice diffs, merges passing leaf commits into its area branch, and posts a single consolidated `#seq` rollup back to `@coordinator`. Pass explicit resolved `/skills//SKILL.md` paths (and `/roles/.md` paths when global `hooks.json` is not active) in `lead-` and leaf briefs rather than naming plugin skills. +2. **Hold the three-tier ceiling.** Never nest deeper than coordinator, `lead-`, and leaf workers (`impl-*`, `ro-*`, `adv-*`, `sicko-*`). Each `lead-` manages roughly `4` to `8` leaf workers in its area (inside `#` when channels exist, or on the main board via targeted `@lead-` mentions when channels are absent), verifies slice diffs, merges passing leaf commits into its area branch, and opens its consolidated `#seq` completion rollup with `@coordinator [done]`. Pass explicit resolved `/skills//SKILL.md` paths (and `/roles/.md` paths when global `hooks.json` is not active) in `lead-` and leaf briefs rather than naming plugin skills. 3. **Run one canary unit first.** Before fanning out across dozens of similar modules or PRs, take one representative slice all the way through `impl-1`, `ro-verify`, and branch integration. Fix any flaw in the brief, harness, or file fence while the mistake costs one worker instead of twenty. 4. **Enforce disjoint write fences.** Assign non-overlapping directories or files to every `impl-*` and `lead-*` brief per `Separate Before Serializing Shared State` in [../references/principles.md](../references/principles.md). Coordinator and `lead-*` own decomposition, boundary checks, and merge integration rather than hand-editing application files. @@ -33,7 +33,7 @@ Treat the swarm board as the authoritative operational ledger. At the end of eve - Unit identifier and goal slice. - Exact commit SHA and `share` worktree path (`pwd`). -- Verification commands run, `ro-verify` `#seq` citation, and verdict (`PASS`, `ISSUES`, or `BLOCKED`). +- Verification commands run, `ro-verify` `#seq` citation (`@ [done]`), and verdict (`PASS`, `PASS+NOTES`, `ISSUES`, or `BLOCKED`). - Next pending unit or open dependency. ### Heartbeat supervision on long runs diff --git a/skills/pstack/topologies/ship.md b/skills/pstack/topologies/ship.md index fe325b0..611f939 100644 --- a/skills/pstack/topologies/ship.md +++ b/skills/pstack/topologies/ship.md @@ -16,7 +16,7 @@ When picking up an active swarm or shipping queue cold, first reconstruct board 1. **Fetch and integrate before `kill`.** Confirm every active `impl-*`, `cand-*`, `lead-*`, or `sicko-*` worker has a clean worktree (`git -C status --short` empty). Fetch and merge or cherry-pick each commit into your local branch (`git fetch && git merge --no-edit FETCH_HEAD`) **before** calling `swarm{action: "kill"}`, because `kill` deletes `share` worktrees immediately. 2. **Shape clean commits.** Order commits into small, self-proving steps (`Sequence Verifiable Units` in [../references/principles.md](../references/principles.md)) following [../../technical-writing/SKILL.md](../../technical-writing/SKILL.md) (pass `/skills/technical-writing/SKILL.md` in the brief when a worker shapes commits). Commit bodies add context rather than restating subjects. -3. **Run `sicko-*` and `ro-verify` gates.** Run a `sicko-*` pass (`workspace: "share"`, passing `/roles/comment-sicko.md` when global `hooks.json` is not active) when diffs carry unnecessary comments or AI slop, merge its edit, then gate the final `HEAD` SHA with an independent `ro-verify` (`workspace: "inherit"`). Pass resolved `/skills/control-ui/SKILL.md` or `/skills/control-cli/SKILL.md` paths (and `/roles/research.md` when global `hooks.json` is not active) in `ro-verify`'s brief rather than telling workers to invoke plugin skills by name. Run `/interrogate` ([../../interrogate/SKILL.md](../../interrogate/SKILL.md)) on security-sensitive, concurrent, or contested changes. +3. **Run `sicko-*` and `ro-verify` gates.** Run a `sicko-*` pass (`workspace: "share"`, passing `/roles/comment-sicko.md` when global `hooks.json` is not active) when diffs carry unnecessary comments or AI slop, merge its edit, then gate the final `HEAD` SHA with an independent `ro-verify` (`workspace: "inherit"`). Pass resolved `/skills/control-ui/SKILL.md` or `/skills/control-cli/SKILL.md` paths (and `/roles/research.md` when global `hooks.json` is not active) in `ro-verify`'s brief rather than telling workers to invoke plugin skills by name. Require `ro-verify` to open its final report with `@ [done]` followed by `PASS` (`PASS+NOTES` when non-blocking notes apply), `ISSUES`, or `BLOCKED`, holding the line on any `ISSUES` or `BLOCKED` result. Run `/interrogate` ([../../interrogate/SKILL.md](../../interrogate/SKILL.md)) on security-sensitive, concurrent, or contested changes. If the user did not ask to push or open a PR, retire all task workers (`impl-*`, `cand-*`, `lead-*`, `ro-*`, `adv-*`, `sicko-*`) via `swarm{action: "kill"}` while leaving built-in `monitor` and `narrator` (`*-N`) alive unless the user asks, post `[done] ` with no `@` so `monitor` goes quiet, and present your local branch and verification evidence. diff --git a/skills/statechart/SKILL.md b/skills/statechart/SKILL.md index ac6be00..41a5e45 100644 --- a/skills/statechart/SKILL.md +++ b/skills/statechart/SKILL.md @@ -18,7 +18,7 @@ Swarm workers do not mount installed plugin skills in ``, so coordinator ## Run -Work in an isolated per-worker scratch folder outside the repository, such as `/tmp/statechart-${WORKER_NAME:-local}-${RUN_ID}`, so parallel swarm workers never overwrite each other's `.tla`, `.cfg`, or `--out` files. Write a TLA+ spec and config, then run the script below. +Work in an isolated per-worker scratch folder outside the repository, such as `/tmp/statechart-${WORKER_NAME:-local}-${RUN_ID:-$$}`, so parallel swarm workers never overwrite each other's `.tla`, `.cfg`, or `--out` files. Write a TLA+ spec and config, then run the script below. ```sh /scripts/statechart SPEC.tla [--config FILE] [--view FILE] [--out DIR] diff --git a/skills/statechart/scripts/test/differential.test.ts b/skills/statechart/scripts/test/differential.test.ts index c6aff9c..3609058 100644 --- a/skills/statechart/scripts/test/differential.test.ts +++ b/skills/statechart/scripts/test/differential.test.ts @@ -1,15 +1,73 @@ import assert from "node:assert/strict"; import { spawn } from "node:child_process"; import fs from "node:fs"; +import { createRequire } from "node:module"; import os from "node:os"; import path from "node:path"; import { test } from "node:test"; -import { fileURLToPath } from "node:url"; -import fc from "fast-check"; +import { fileURLToPath, pathToFileURL } from "node:url"; const here = path.dirname(fileURLToPath(import.meta.url)); +const requireFromHere = createRequire(import.meta.url); const skip = !process.env.TLA2TOOLS && "set TLA2TOOLS to the path of tla2tools.jar to run TLC"; +type Arbitrary = { + map(mapper: (value: T) => U): Arbitrary; +}; + +type AsyncProperty = unknown; + +type FastCheck = { + integer(constraints: { min: number; max: number }): Arbitrary; + nat(max: number): Arbitrary; + boolean(): Arbitrary; + constantFrom(...values: T): Arbitrary; + array( + arb: Arbitrary, + constraints?: { minLength?: number; maxLength?: number }, + ): Arbitrary; + uniqueArray( + arb: Arbitrary, + constraints?: { minLength?: number; maxLength?: number }, + ): Arbitrary; + shuffledSubarray( + originalArray: readonly T[], + constraints?: { minLength?: number; maxLength?: number }, + ): Arbitrary; + tuple(a: Arbitrary, b: Arbitrary): Arbitrary<[T1, T2]>; + record>>( + recordModel: T, + ): Arbitrary<{ [K in keyof T]: T[K] extends Arbitrary ? V : never }>; + oneof[]>( + ...arbs: T + ): Arbitrary ? V : never>; + option(arb: Arbitrary, constraints?: { freq?: number }): Arbitrary; + asyncProperty(arb: Arbitrary, predicate: (value: T) => Promise): AsyncProperty; + assert( + property: AsyncProperty, + params?: { numRuns?: number; seed?: number; includeErrorInReport?: boolean }, + ): Promise; +}; + +async function loadFastCheck(): Promise { + const specifier: string = "fast-check"; + try { + const mod = (await import(specifier)) as { default?: FastCheck } & Partial; + return (mod.default ?? mod) as FastCheck; + } catch (error) { + if ((error as NodeJS.ErrnoException)?.code !== "ERR_MODULE_NOT_FOUND") throw error; + } + try { + const resolved = requireFromHere.resolve("fast-check"); + const mod = (await import(pathToFileURL(resolved).href)) as { + default?: FastCheck; + } & Partial; + return (mod.default ?? mod) as FastCheck; + } catch { + return null; + } +} + const PROTOTYPE_KEYS = ["constructor", "__proto__", "toString", "valueOf", "hasOwnProperty"]; const NAMES = [ ...PROTOTYPE_KEYS, @@ -40,84 +98,109 @@ const STEPS = ["you press play", ...PROTOTYPE_KEYS, "the connect fails [while of const GROUPS = ["paying", "a b", ...PROTOTYPE_KEYS]; const COLORS = ["blue", "orange", "purple", "yellow"]; -const INT = fc.integer({ min: -3, max: 3 }).map(String); -const STRING = fc.constantFrom( - String.raw`""`, - String.raw`"a b"`, - String.raw`"say \"hi\""`, - String.raw`"back\\slash"`, - String.raw`"tab\tstop"`, - String.raw`"two\nlines"`, - String.raw`"__proto__"`, -); -const values = (value: fc.Arbitrary, maxLength = 4) => - fc.uniqueArray(value, { minLength: 1, maxLength }); const tuple = (items: string[]) => `<<${items.join(", ")}>>`; type Domain = string[] | { modelValues: number }; -const DOMAIN: fc.Arbitrary = fc.oneof( - values(INT), - values(STRING), - values(fc.constantFrom("TRUE", "FALSE"), 2), - fc.integer({ min: 1, max: 4 }).map((modelValues) => ({ modelValues })), - values(fc.tuple(INT, STRING).map(([i, s]) => `[constructor |-> ${i}, __proto__ |-> ${s}]`)), - values( - fc - .uniqueArray(fc.tuple(INT, STRING), { maxLength: 2 }) - .map((ps) => `{${ps.map(tuple).join(", ")}}`), - ), - values(fc.array(INT, { maxLength: 2 }).map(tuple)), -); - -const ATOM = fc.record({ v: fc.nat(2), i: fc.nat(3), not: fc.boolean() }); type Atom = { v: number; i: number; not: boolean }; +type ModelView = { + form: "string" | "parents" | "regions"; + labels: string[]; + regions: string[]; + steps: (string | null)[] | null; + title: "A b c" | "constructor" | null; + groups: string[] | null; +}; +type Model = { + module: "Model" | "prototype"; + names: string[]; + vars: { domain: Domain; init: number[] }[]; + actions: { guard: Atom[]; set: { v: number; to: number[] }[] }[]; + invariants: Atom[][]; + deadlock: boolean; + initNext: boolean; + constraint: Atom | null; + actionConstraint: { v: number; from: number; to: number } | null; + property: { + form: number; + atoms: [Atom, Atom]; + fairness: { strong: boolean; on: number | null } | null; + } | null; + view: ModelView | null; +}; -const MODEL = fc.record({ - module: fc.constantFrom("Model", "prototype"), - names: fc.shuffledSubarray(NAMES, { minLength: NAMES.length }), - vars: fc.array( - fc.record({ domain: DOMAIN, init: fc.array(fc.nat(3), { minLength: 1, maxLength: 2 }) }), - { minLength: 1, maxLength: 3 }, - ), - actions: fc.array( - fc.record({ - guard: fc.array(ATOM, { maxLength: 2 }), - set: fc.array( - fc.record({ v: fc.nat(2), to: fc.array(fc.nat(3), { minLength: 1, maxLength: 2 }) }), - { minLength: 1, maxLength: 2 }, - ), - }), - { minLength: 1, maxLength: 6 }, - ), - invariants: fc.array(fc.array(ATOM, { minLength: 1, maxLength: 2 }), { maxLength: 2 }), - deadlock: fc.boolean(), - initNext: fc.boolean(), - constraint: fc.option(ATOM, { freq: 2 }), - actionConstraint: fc.option(fc.record({ v: fc.nat(2), from: fc.nat(3), to: fc.nat(3) }), { - freq: 2, - }), - property: fc.option( - fc.record({ - form: fc.nat(3), - atoms: fc.tuple(ATOM, ATOM), - fairness: fc.option(fc.record({ strong: fc.boolean(), on: fc.option(fc.nat(5)) })), - }), - ), - view: fc.option( - fc.record({ - form: fc.constantFrom("string", "parents", "regions"), - labels: fc.uniqueArray(fc.constantFrom(...LABELS), { minLength: 1, maxLength: 4 }), - regions: fc.uniqueArray(fc.constantFrom("Session", ...LABELS), { - minLength: 1, - maxLength: 3, +function makeModelArbitrary(fc: FastCheck): Arbitrary { + const int = fc.integer({ min: -3, max: 3 }).map(String); + const str = fc.constantFrom( + String.raw`""`, + String.raw`"a b"`, + String.raw`"say \"hi\""`, + String.raw`"back\\slash"`, + String.raw`"tab\tstop"`, + String.raw`"two\nlines"`, + String.raw`"__proto__"`, + ); + const values = (value: Arbitrary, maxLength = 4) => + fc.uniqueArray(value, { minLength: 1, maxLength }); + const domain: Arbitrary = fc.oneof( + values(int), + values(str), + values(fc.constantFrom("TRUE", "FALSE"), 2), + fc.integer({ min: 1, max: 4 }).map((modelValues) => ({ modelValues })), + values(fc.tuple(int, str).map(([i, s]) => `[constructor |-> ${i}, __proto__ |-> ${s}]`)), + values( + fc + .uniqueArray(fc.tuple(int, str), { maxLength: 2 }) + .map((ps) => `{${ps.map(tuple).join(", ")}}`), + ), + values(fc.array(int, { maxLength: 2 }).map(tuple)), + ); + const atom: Arbitrary = fc.record({ v: fc.nat(2), i: fc.nat(3), not: fc.boolean() }); + return fc.record({ + module: fc.constantFrom("Model", "prototype"), + names: fc.shuffledSubarray(NAMES, { minLength: NAMES.length }), + vars: fc.array( + fc.record({ domain, init: fc.array(fc.nat(3), { minLength: 1, maxLength: 2 }) }), + { minLength: 1, maxLength: 3 }, + ), + actions: fc.array( + fc.record({ + guard: fc.array(atom, { maxLength: 2 }), + set: fc.array( + fc.record({ v: fc.nat(2), to: fc.array(fc.nat(3), { minLength: 1, maxLength: 2 }) }), + { minLength: 1, maxLength: 2 }, + ), }), - steps: fc.option(fc.array(fc.option(fc.constantFrom(...STEPS)), { maxLength: 6 })), - title: fc.option(fc.constantFrom("A b c", "constructor")), - groups: fc.option(fc.uniqueArray(fc.constantFrom(...GROUPS), { minLength: 1, maxLength: 3 })), + { minLength: 1, maxLength: 6 }, + ), + invariants: fc.array(fc.array(atom, { minLength: 1, maxLength: 2 }), { maxLength: 2 }), + deadlock: fc.boolean(), + initNext: fc.boolean(), + constraint: fc.option(atom, { freq: 2 }), + actionConstraint: fc.option(fc.record({ v: fc.nat(2), from: fc.nat(3), to: fc.nat(3) }), { + freq: 2, }), - ), -}); -type Model = typeof MODEL extends fc.Arbitrary ? M : never; + property: fc.option( + fc.record({ + form: fc.nat(3), + atoms: fc.tuple(atom, atom), + fairness: fc.option(fc.record({ strong: fc.boolean(), on: fc.option(fc.nat(5)) })), + }), + ), + view: fc.option( + fc.record({ + form: fc.constantFrom("string", "parents", "regions"), + labels: fc.uniqueArray(fc.constantFrom(...LABELS), { minLength: 1, maxLength: 4 }), + regions: fc.uniqueArray(fc.constantFrom("Session", ...LABELS), { + minLength: 1, + maxLength: 3, + }), + steps: fc.option(fc.array(fc.option(fc.constantFrom(...STEPS)), { maxLength: 6 })), + title: fc.option(fc.constantFrom("A b c", "constructor")), + groups: fc.option(fc.uniqueArray(fc.constantFrom(...GROUPS), { minLength: 1, maxLength: 3 })), + }), + ), + }); +} type Case = { module: string; tla: string; @@ -398,9 +481,14 @@ async function answer(c: Case) { test( "the compiler answers each rule as plain TLC does for generated models", { skip }, - async () => { + async (t) => { + const fc = await loadFastCheck(); + if (!fc) { + t.skip("install fast-check in skills/statechart/scripts to run generated-model differential checks"); + return; + } await fc.assert( - fc.asyncProperty(MODEL.map(caseOf), async (c) => { + fc.asyncProperty(makeModelArbitrary(fc).map(caseOf), async (c) => { await answer(c); }), { diff --git a/skills/statechart/scripts/test/render.test.ts b/skills/statechart/scripts/test/render.test.ts index 5309a92..4395269 100644 --- a/skills/statechart/scripts/test/render.test.ts +++ b/skills/statechart/scripts/test/render.test.ts @@ -18,36 +18,112 @@ const guide = fs.readFileSync(path.join(here, "../../references/render.md"), "ut const css = guide.match(/```css\n([\s\S]*?)```/); assert.ok(css, "references/render.md has no css block"); const theme = css[1]; -const cache = path.join( - process.env.XDG_CACHE_HOME ?? path.join(os.homedir(), ".cache"), +const configuredCache = path.join( + process.env.XDG_CACHE_HOME || path.join(os.homedir(), ".cache"), "statechart", ); -const install = (name: string, ...packages: string[]) => { - const dir = path.join(cache, name); - execFileSync( - "npm", - ["install", "--prefix", dir, "--no-audit", "--no-fund", "--save-exact", ...packages], - { stdio: ["ignore", "pipe", "pipe"] }, - ); - return createRequire(path.join(dir, "package.json")); -}; - -let render: Render; -let mermaid: Mermaid; -let html: Html; +const fallbackCache = path.join(os.tmpdir(), "statechart"); + +function parsePackageSpec(spec: string): { pkg: string; version: string | null } { + const at = spec.lastIndexOf("@"); + if (at <= 0) return { pkg: spec, version: null }; + return { pkg: spec.slice(0, at), version: spec.slice(at + 1) }; +} + +function resolveCached(root: string, name: string, packages: string[]): NodeRequire | null { + const manifestPath = path.join(root, name, "package.json"); + if (!fs.existsSync(manifestPath)) return null; + try { + const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")) as { + dependencies?: Record; + }; + const req = createRequire(manifestPath); + for (const spec of packages) { + const { pkg, version } = parsePackageSpec(spec); + if (version && manifest.dependencies?.[pkg] !== version) return null; + req.resolve(pkg); + } + return req; + } catch { + return null; + } +} + +function isWritableCacheRoot(root: string): boolean { + try { + fs.mkdirSync(root, { recursive: true }); + const probe = path.join(root, `.write-probe-${process.pid}-${Date.now()}`); + fs.writeFileSync(probe, ""); + fs.unlinkSync(probe); + return true; + } catch { + return false; + } +} + +type PackageLoad = { req: NodeRequire } | { skipReason: string }; + +function resolveOrInstall(name: string, ...packages: string[]): PackageLoad { + const cachedPrimary = resolveCached(configuredCache, name, packages); + if (cachedPrimary) return { req: cachedPrimary }; + + let targetRoot = configuredCache; + if (!isWritableCacheRoot(configuredCache)) { + const cachedFallback = resolveCached(fallbackCache, name, packages); + if (cachedFallback) return { req: cachedFallback }; + targetRoot = fallbackCache; + } + + const dir = path.join(targetRoot, name); + const env = + targetRoot === fallbackCache && !process.env.NPM_CONFIG_CACHE && !process.env.npm_config_cache + ? { ...process.env, npm_config_cache: path.join(fallbackCache, "npm-cache") } + : process.env; + + try { + fs.mkdirSync(dir, { recursive: true }); + execFileSync( + "npm", + ["install", "--prefix", dir, "--no-audit", "--no-fund", "--save-exact", ...packages], + { stdio: ["ignore", "pipe", "pipe"], env }, + ); + return { req: createRequire(path.join(dir, "package.json")) }; + } catch (error) { + const code = (error as NodeJS.ErrnoException)?.code; + const summary = code ? ` (${code})` : ""; + return { + skipReason: `optional packages ${packages.join(", ")} are not cached in ${targetRoot} and npm install failed${summary}`, + }; + } +} + +let render: Render | undefined; +let mermaid: Mermaid | undefined; +let html: Html | undefined; +let peleSkipReason: string | undefined; +let mermaidSkipReason: string | undefined; + before(async () => { - const pele = install("test-pele-1.9.0", "pele@1.9.0"); - ({ render } = await import(pathToFileURL(pele.resolve("pele")).href)); - - const parser = install("test-mermaid-11.16.0", "mermaid@11.16.0", "jsdom@26.1.0"); - const { window } = new (parser("jsdom").JSDOM)(""); - Object.assign(globalThis, { window, document: window.document, DOMParser: window.DOMParser }); - mermaid = (await import(pathToFileURL(parser.resolve("mermaid")).href)).default; - html = (markup) => { - const t = window.document.createElement("template"); - t.innerHTML = markup; - return t.content.textContent ?? ""; - }; + const pele = resolveOrInstall("test-pele-1.9.0", "pele@1.9.0"); + if ("skipReason" in pele) { + peleSkipReason = pele.skipReason; + } else { + ({ render } = await import(pathToFileURL(pele.req.resolve("pele")).href)); + } + + const parser = resolveOrInstall("test-mermaid-11.16.0", "mermaid@11.16.0", "jsdom@26.1.0"); + if ("skipReason" in parser) { + mermaidSkipReason = parser.skipReason; + } else { + const { window } = new (parser.req("jsdom").JSDOM)(""); + Object.assign(globalThis, { window, document: window.document, DOMParser: window.DOMParser }); + mermaid = (await import(pathToFileURL(parser.req.resolve("mermaid")).href)).default; + html = (markup) => { + const t = window.document.createElement("template"); + t.innerHTML = markup; + return t.content.textContent ?? ""; + }; + } }); const classes = new Set(); @@ -57,19 +133,32 @@ for (const chart of charts) { const marks = [...text.matchAll(/^ {2}class (\w+) (\w+)$/gm)]; for (const [, , mark] of marks) classes.add(mark); - test(`${name} renders in Pele 1.9.0 with each class on its node`, () => { + test(`${name} renders in Pele 1.9.0 with each class on its node`, (t) => { + if (peleSkipReason || !render) { + t.skip(peleSkipReason ?? "pele@1.9.0 unavailable"); + return; + } const { svg } = render(text); assert.match(svg, /^ { + test(`${name} parses in mermaid.js, which chat views use`, async (t) => { + if (mermaidSkipReason || !mermaid) { + t.skip(mermaidSkipReason ?? "mermaid@11.16.0 unavailable"); + return; + } await mermaid.parse(text); }); } -test("random labels and colors keep their text and the chart's shape in Mermaid and Pele", async () => { +test("random labels and colors keep their text and the chart's shape in Mermaid and Pele", async (t) => { + const skipReason = peleSkipReason ?? mermaidSkipReason; + if (skipReason || !render || !mermaid || !html) { + t.skip(skipReason ?? "render packages unavailable"); + return; + } const { drawn, refused, failure } = await fuzz({ seed: 1, charts: 500, render, mermaid, html }); if (failure) assert.fail(failure); assert.ok(drawn > 400 && refused > 20, `${drawn} charts drawn, ${refused} refused`); diff --git a/skills/verify-gen/SKILL.md b/skills/verify-gen/SKILL.md index f9f44ea..801e6fa 100644 --- a/skills/verify-gen/SKILL.md +++ b/skills/verify-gen/SKILL.md @@ -35,7 +35,7 @@ Have `ro-surface` and `ro-harness` answer five questions from the codebase and a - **Run.** Locate the documented local startup command, migrations, seed fixtures, auth setup, and readiness signal. Ready means usable from an empty data directory, not merely an open socket. - **Drive.** Reuse checked-in Playwright, Cypress, PTY, or HTTP harnesses first. Otherwise pair with [`control-ui`](../control-ui/SKILL.md) for browser and CDP surfaces or [`control-cli`](../control-cli/SKILL.md) for interactive terminals, passing the resolved `/skills/control-ui/SKILL.md` or `/skills/control-cli/SKILL.md` path in `ro-harness` and `impl-verify` briefs. - **Observe.** List concrete proof artifacts such as ARIA snapshots, screenshots, terminal transcripts, exit codes, HTTP payloads, and stored database or filesystem state. -- **Isolate.** Determine how parallel swarm workers avoid collisions via ephemeral ports (`--port 0`), disposable data directories (`/tmp/verify--$RUN_ID`), isolated browser `--user-data-dir` paths, and per-run tmux or PTY session names. If two instances cannot run side by side safely, state that lock constraint explicitly in the generated skill. +- **Isolate.** Determine how parallel swarm workers avoid collisions via ephemeral ports (`--port 0`), disposable data directories (`/tmp/verify--${RUN_ID:-$$}`), isolated browser `--user-data-dir` paths, and per-run tmux or PTY session names. If two instances cannot run side by side safely, state that lock constraint explicitly in the generated skill. If the checkout fails to build or start on `HEAD`, report the exact failure or fix startup first before generating a skill. Never encode broken startup workarounds as normal steps. From 8a12063825121d536586eabb591a95cbc0f0f69f Mon Sep 17 00:00:00 2001 From: Kaitian Xie Date: Sat, 14 Nov 2026 21:47:10 +0800 Subject: [PATCH 2/2] fix(guard): harden confinement, shell expansion, and agy 1.5.9 session detection Resolve the shell-variable, evolve-isolation, verifier-sharding, and confinement findings from the agy 1.5.9 swarm audit without restricting worker swarm autonomy or codemod capacity. - Track active segment environments across shell redirects, expand HOME, defaulted shell variables, and safe runtime tokens in resolveShellTargetPath, and anchor canonicalTempRoots strictly to outer host env. - Delegate hook runs through PSTACK_PLUGIN_ROOT and isolate deduplication state via PSTACK_CACHE_DIR for evolve trials, strip numeric suffixes on *-verifier worker groups, and detect forkStep memory markers plus swarm.json anchors. - Confine mutating git, download, archive, find, and temp-script calls across ro-*, author, adversary, and sicko-* roles while preserving dynamic in-worktree codemod and fuzzer loops, /tmp harness expressions, and cross-worktree donor fetches. --- hooks/guard.mjs | 2278 +++++++++++++++++++++++++++++++++++------- hooks/guard.test.mjs | 570 +++++++++++ 2 files changed, 2508 insertions(+), 340 deletions(-) diff --git a/hooks/guard.mjs b/hooks/guard.mjs index 707cc80..d97eefe 100755 --- a/hooks/guard.mjs +++ b/hooks/guard.mjs @@ -1,4 +1,5 @@ #!/usr/bin/env node +import { execFileSync } from "node:child_process"; import { existsSync, lstatSync, @@ -8,11 +9,12 @@ import { readlinkSync, realpathSync, renameSync, + statSync, unlinkSync, writeFileSync, } from "node:fs"; import { homedir, tmpdir } from "node:os"; -import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path"; +import { basename, dirname, extname, isAbsolute, join, relative, resolve } from "node:path"; import { fileURLToPath } from "node:url"; const LEGACY_SUBAGENT_TOOLS = new Set([ @@ -35,6 +37,8 @@ const PSTACK_SLASH_COMMAND_RE = const ROLE_CAPSULE_HEADING_RE = /(?:^|\r?\n|\\n)# [^\r\n\\]+ role capsule(?:\r?\n|\\n|$)/i; +const FORK_MEMORY_MARKER_RE = /Everything before this turn is @[A-Za-z0-9_-]+'s memory/; + const ROLE_OVERRIDE_NOTICE = "> **Role override.** Your active swarm role has changed. The role capsule below supersedes every earlier role capsule in this conversation history, including rules inherited through `fork`.\n\n"; @@ -42,8 +46,11 @@ const MUTATING_MCP_PATTERN = /(^|_)(create|update|delete|remove|set|post|reply|resolve|merge|close|push|write|edit|append|restore|upload|generate|install|uninstall|send|modify|patch|drop|insert)(_|$)/i; const MUTATING_GIT_SUBCOMMANDS = new Set([ + "add", + "stage", "commit", "push", + "pull", "merge", "rebase", "cherry-pick", @@ -59,6 +66,7 @@ const MUTATING_GIT_SUBCOMMANDS = new Set([ "restore", "init", "clone", + "update-ref", ]); const GIT_GLOBAL_VALUE_FLAGS = new Set([ @@ -75,11 +83,8 @@ const HARD_BLOCKED_RO_FS_COMMANDS = new Set([ "rmdir", "chown", "chgrp", - "truncate", - "dd", "patch", "ln", - "install", ]); const SICKO_BLOCKED_FS_COMMANDS = new Set([ @@ -95,6 +100,9 @@ const SICKO_BLOCKED_FS_COMMANDS = new Set([ "truncate", "dd", "patch", + "chmod", + "chown", + "chgrp", ]); const NPM_INSTALL_SUBCOMMANDS = new Set([ @@ -254,6 +262,9 @@ function resolveAppDataDir(payload, env) { } function resolveCacheDir(appDataDir, env) { + if (typeof env.PSTACK_CACHE_DIR === "string" && env.PSTACK_CACHE_DIR.length > 0) { + return env.PSTACK_CACHE_DIR; + } if (typeof env.PLUGIN_DATA === "string" && env.PLUGIN_DATA.length > 0) { return env.PLUGIN_DATA; } @@ -263,6 +274,9 @@ function resolveCacheDir(appDataDir, env) { } function resolvePluginRoot(env) { + if (typeof env.PSTACK_PLUGIN_ROOT === "string" && env.PSTACK_PLUGIN_ROOT.length > 0) { + return env.PSTACK_PLUGIN_ROOT; + } if (typeof env.PLUGIN_ROOT === "string" && env.PLUGIN_ROOT.length > 0) { return env.PLUGIN_ROOT; } @@ -289,44 +303,57 @@ function resolveOwnTranscriptPath(payload, appDataDir) { return null; } +function isSystemRoleCapsuleStep(parsed) { + if (!parsed || typeof parsed !== "object") return false; + if (parsed.type !== "SYSTEM_MESSAGE" && parsed.source !== "SYSTEM_SDK") { + return false; + } + return typeof parsed.content === "string" && ROLE_CAPSULE_HEADING_RE.test(parsed.content); +} + function transcriptIsUnforkedUserRoot(transcriptPath, hasPriorInjectedCapsule) { if (!transcriptPath || !existsSync(transcriptPath)) return false; try { const content = readFileSync(transcriptPath, "utf8"); - if (/\[swarm\] new board message|Your assignment \(recorded on the board/.test(content)) { - return false; - } - if (!hasPriorInjectedCapsule && ROLE_CAPSULE_HEADING_RE.test(content)) { + if ( + /\[swarm\] new board message|Your assignment \(recorded on the board/.test(content) || + FORK_MEMORY_MARKER_RE.test(content) + ) { return false; } + let sawFirstStep = false; for (const line of content.split(/\r?\n/)) { const trimmed = line.trim(); if (!trimmed) continue; const parsed = JSON.parse(trimmed); - return Boolean(parsed && typeof parsed === "object" && parsed.type === "USER_INPUT"); + if (!parsed || typeof parsed !== "object") return false; + if (!sawFirstStep) { + if (parsed.type !== "USER_INPUT") return false; + sawFirstStep = true; + } + if (typeof parsed.content === "string" && FORK_MEMORY_MARKER_RE.test(parsed.content)) { + return false; + } + if (!hasPriorInjectedCapsule && isSystemRoleCapsuleStep(parsed)) { + return false; + } } + return sawFirstStep; } catch { return false; } - return false; } function transcriptContainsRoleCapsule(transcriptPath) { if (!transcriptPath || !existsSync(transcriptPath)) return false; try { const content = readFileSync(transcriptPath, "utf8"); - if (ROLE_CAPSULE_HEADING_RE.test(content)) return true; for (const line of content.split(/\r?\n/)) { const trimmed = line.trim(); if (!trimmed) continue; try { const parsed = JSON.parse(trimmed); - if ( - parsed && - typeof parsed === "object" && - typeof parsed.content === "string" && - ROLE_CAPSULE_HEADING_RE.test(parsed.content) - ) { + if (isSystemRoleCapsuleStep(parsed)) { return true; } } catch {} @@ -357,6 +384,20 @@ function extractMembersFromDoc(doc, targetConversationId) { return { hasMembers: true, workerEntries, matchedCoordinator }; } +function anchorMatchesCoordinator(anchorPath, targetConversationId, ownerDirId) { + if (!existsSync(anchorPath)) return false; + const doc = readJsonObject(anchorPath); + const explicitCoord = + doc.coordinator_conversation_id || + doc.coordinatorConversationId || + doc.coordinator_id || + doc.coordinatorId; + if (typeof explicitCoord === "string" && explicitCoord.length > 0) { + return explicitCoord === targetConversationId; + } + return ownerDirId === targetConversationId; +} + function resolveWorkerOrCoordinatorFromSwarm( conversationId, payload, @@ -381,13 +422,24 @@ function resolveWorkerOrCoordinatorFromSwarm( const candidateRoots = [payload.rootConversationId, payload.parentConversationId]; const checkedRoots = new Set(); + let hasAnchorWithoutMembers = false; for (const rootId of candidateRoots) { if (typeof rootId !== "string" || rootId.length === 0 || rootId === conversationId || checkedRoots.has(rootId)) { continue; } checkedRoots.add(rootId); - const membersPath = join(appDataDir, "brain", rootId, ".system_generated", "swarm", "members.json"); - if (!existsSync(membersPath)) continue; + const swarmDir = join(appDataDir, "brain", rootId, ".system_generated", "swarm"); + const membersPath = join(swarmDir, "members.json"); + if (!existsSync(membersPath)) { + const anchorPath = join(swarmDir, "swarm.json"); + if (existsSync(anchorPath)) { + if (anchorMatchesCoordinator(anchorPath, conversationId, rootId)) { + return { workerName: null, isKnownCoordinator: true }; + } + hasAnchorWithoutMembers = true; + } + continue; + } const { workerEntries, matchedCoordinator } = extractMembersFromDoc( readJsonObject(membersPath), conversationId, @@ -401,7 +453,8 @@ function resolveWorkerOrCoordinatorFromSwarm( } } - const ownMembersPath = join(appDataDir, "brain", conversationId, ".system_generated", "swarm", "members.json"); + const ownSwarmDir = join(appDataDir, "brain", conversationId, ".system_generated", "swarm"); + const ownMembersPath = join(ownSwarmDir, "members.json"); if (existsSync(ownMembersPath)) { const { workerEntries } = extractMembersFromDoc(readJsonObject(ownMembersPath), conversationId); updateJsonKeys(mapPath, workerEntries); @@ -411,6 +464,11 @@ function resolveWorkerOrCoordinatorFromSwarm( return { workerName: null, isKnownCoordinator: true }; } + const ownAnchorPath = join(ownSwarmDir, "swarm.json"); + if (anchorMatchesCoordinator(ownAnchorPath, conversationId, conversationId)) { + return { workerName: null, isKnownCoordinator: true }; + } + const nonWorkerRootsPath = join(cacheDir, "non-worker-roots.json"); const nonWorkerRoots = readJsonObject(nonWorkerRootsPath); if (nonWorkerRoots[conversationId]) { @@ -440,8 +498,19 @@ function resolveWorkerOrCoordinatorFromSwarm( if ((!entry.isDirectory() && !entry.isSymbolicLink()) || entry.name === conversationId || checkedRoots.has(entry.name)) { continue; } - const membersPath = join(brainDir, entry.name, ".system_generated", "swarm", "members.json"); - if (!existsSync(membersPath)) continue; + const swarmDir = join(brainDir, entry.name, ".system_generated", "swarm"); + const membersPath = join(swarmDir, "members.json"); + if (!existsSync(membersPath)) { + const anchorPath = join(swarmDir, "swarm.json"); + if (existsSync(anchorPath)) { + if (anchorMatchesCoordinator(anchorPath, conversationId, entry.name)) { + matchedCoordinator = true; + } else { + hasAnchorWithoutMembers = true; + } + } + continue; + } const extracted = extractMembersFromDoc(readJsonObject(membersPath), conversationId); Object.assign(discoveredWorkers, extracted.workerEntries); if (extracted.matchedCoordinator) matchedCoordinator = true; @@ -455,7 +524,9 @@ function resolveWorkerOrCoordinatorFromSwarm( return { workerName: null, isKnownCoordinator: true }; } - updateJsonKey(nonWorkerRootsPath, conversationId, true); + if (!hasAnchorWithoutMembers) { + updateJsonKey(nonWorkerRootsPath, conversationId, true); + } return { workerName: null, isKnownCoordinator: false }; } @@ -484,10 +555,11 @@ function classifyWorkerMember(memberName) { if (memberName.startsWith("sicko-")) { return { kind: "sicko", capsule: "roles/comment-sicko.md" }; } + const baseMemberName = memberName.replace(/-\d+$/, ""); if ( - memberName === "verifier" || + baseMemberName === "verifier" || memberName.startsWith("verifier-") || - memberName.endsWith("-verifier") || + baseMemberName.endsWith("-verifier") || memberName.startsWith("verify-") ) { return { kind: "readonly", capsule: "roles/research.md" }; @@ -495,7 +567,7 @@ function classifyWorkerMember(memberName) { return { kind: "author", capsule: "roles/poteto-agent.md" }; } -function classifySession(payload, env, normalizedToolName = "") { +function classifySession(payload, env) { const appDataDir = resolveAppDataDir(payload, env); const cacheDir = resolveCacheDir(appDataDir, env); const conversationId = typeof payload.conversationId === "string" ? payload.conversationId : ""; @@ -529,14 +601,12 @@ function classifySession(payload, env, normalizedToolName = "") { const matchesSlashCommand = typeof payload.lastUserInput === "string" && PSTACK_SLASH_COMMAND_RE.test(payload.lastUserInput); - const matchesSwarmBoardTool = SWARM_BOARD_TOOLS.has(normalizedToolName); const forcedCoordinator = env.PSTACK_FORCE_COORDINATOR === "1"; if ( isKnownCoordinator || alreadyInjected || matchesSlashCommand || - matchesSwarmBoardTool || forcedCoordinator ) { return { @@ -647,24 +717,146 @@ function isInsideCanonicalTempRoots(realTarget, canonicalTempRoots, realCwd, opt ); } -function resolveShellTargetPath(rawToken, baseDir, env) { +function replaceSafeRuntimeTokens(input) { + return input + .replace(/\$\{\$\}|\$\$/g, "0") + .replace(/\$\{(?:PPID|RANDOM|BASHPID)\}|\$(?:PPID|RANDOM|BASHPID)(?![A-Za-z0-9_])/g, "0") + .replace( + /\$\(\s*date\s+(?:-u\s+)?(?:\+[A-Za-z0-9_%:.-]+|"\+[A-Za-z0-9_%:.-]+"|'\+[A-Za-z0-9_%:.-]+')\s*\)/g, + "0", + ) + .replace(/\$\(\s*uuidgen\s*\)/g, "0"); +} + +function findMatchingBrace(str, openBraceIdx) { + let depth = 1; + let i = openBraceIdx + 1; + let inSingle = false; + let inDouble = false; + while (i < str.length && depth > 0) { + const ch = str[i]; + if (ch === "\\" && !inSingle) { + i += 2; + continue; + } + if (ch === "'" && !inDouble) { + inSingle = !inSingle; + i += 1; + continue; + } + if (ch === '"' && !inSingle) { + inDouble = !inDouble; + i += 1; + continue; + } + if (!inSingle && !inDouble) { + if (ch === "{") depth += 1; + else if (ch === "}") { + depth -= 1; + if (depth === 0) return i; + } + } + i += 1; + } + return -1; +} + +function expandShellVarsOnce(input, envState) { + let out = ""; + let changed = false; + let i = 0; + + while (i < input.length) { + const ch = input[i]; + if (ch === "\\" && i + 1 < input.length && (input[i + 1] === "$" || input[i + 1] === "`" || input[i + 1] === "~")) { + out += input.slice(i, i + 2); + i += 2; + continue; + } + if (ch !== "$") { + out += ch; + i += 1; + continue; + } + + if (input[i + 1] === "{") { + const closeIdx = findMatchingBrace(input, i + 1); + if (closeIdx < 0) return { ok: false, value: "", changed: false }; + const body = input.slice(i + 2, closeIdx); + const paramMatch = body.match(/^([A-Za-z_][A-Za-z0-9_]*)(?:(:?[-=])([\s\S]*))?$/); + if (!paramMatch) return { ok: false, value: "", changed: false }; + const [, varName, op, fallbackVal = ""] = paramMatch; + const rawVal = typeof envState[varName] === "string" ? envState[varName] : undefined; + + if (!op) { + if (rawVal === undefined) return { ok: false, value: "", changed: false }; + out += rawVal; + } else if (op === ":-" || op === ":=") { + const chosen = rawVal !== undefined && rawVal.length > 0 ? rawVal : fallbackVal; + if (op === ":=" && (rawVal === undefined || rawVal.length === 0)) { + envState[varName] = chosen; + } + out += chosen; + } else { + const chosen = rawVal !== undefined ? rawVal : fallbackVal; + if (op === "=" && rawVal === undefined) { + envState[varName] = chosen; + } + out += chosen; + } + changed = true; + i = closeIdx + 1; + continue; + } + + const bareMatch = input.slice(i + 1).match(/^([A-Za-z_][A-Za-z0-9_]*)/); + if (bareMatch) { + const varName = bareMatch[1]; + const rawVal = typeof envState[varName] === "string" ? envState[varName] : undefined; + if (rawVal === undefined) return { ok: false, value: "", changed: false }; + out += rawVal; + changed = true; + i += 1 + varName.length; + continue; + } + + out += ch; + i += 1; + } + + return { ok: true, value: out, changed }; +} + +function resolveShellTargetPath(rawToken, baseDir, mergedEnv = {}, options = {}) { + const { requireAbsoluteAfterExpand = false } = options; if (typeof rawToken !== "string" || rawToken.length === 0) return null; if (rawToken === "/dev/null") return "/dev/null"; + const envState = mergedEnv && typeof mergedEnv === "object" ? { ...mergedEnv } : {}; let expanded = rawToken; - if (expanded === "~" || expanded.startsWith("~/")) { - const home = env.HOME || homedir(); + if (expanded.startsWith("\\~")) { + expanded = expanded.slice(1); + } else if (expanded === "~" || expanded.startsWith("~/")) { + const home = envState.HOME || homedir(); expanded = expanded === "~" ? home : join(home, expanded.slice(2)); } else if (expanded.startsWith("~")) { return null; } - if (/^\$(?:TMPDIR|\{TMPDIR\})(?=\/|$)/.test(expanded)) { - const tmpVal = env.TMPDIR || tmpdir(); - expanded = expanded.replace(/^\$(?:TMPDIR|\{TMPDIR\})/, tmpVal); + for (let pass = 0; pass < 4; pass += 1) { + expanded = replaceSafeRuntimeTokens(expanded); + if (!expanded.includes("$")) break; + const step = expandShellVarsOnce(expanded, envState); + if (!step.ok) return null; + expanded = step.value; + if (!step.changed) break; } + expanded = replaceSafeRuntimeTokens(expanded); - if (/[$`]/.test(expanded)) { + if (!expanded || /[$`]/.test(expanded)) { + return null; + } + if (requireAbsoluteAfterExpand && !isAbsolute(expanded)) { return null; } @@ -763,6 +955,28 @@ function preprocessHeredocs(command) { }; } +function scanBalancedDollarSub(str, dollarIdx) { + let depth = 1; + let j = dollarIdx + 2; + let subSingle = false; + let subDouble = false; + while (j < str.length && depth > 0) { + const c = str[j]; + if (c === "\\" && !subSingle) { + j += 2; + continue; + } + if (c === "'" && !subDouble) subSingle = !subSingle; + else if (c === '"' && !subSingle) subDouble = !subDouble; + else if (!subSingle && !subDouble) { + if (c === "(") depth += 1; + else if (c === ")") depth -= 1; + } + j += 1; + } + return depth === 0 ? j : -1; +} + function extractCommandSubstitutions(command) { const subs = []; let i = 0; @@ -786,27 +1000,10 @@ function extractCommandSubstitutions(command) { continue; } if (!inSingle && ch === "$" && command[i + 1] === "(") { - let depth = 1; - let j = i + 2; - let subSingle = false; - let subDouble = false; - while (j < command.length && depth > 0) { - const c = command[j]; - if (c === "\\" && !subSingle) { - j += 2; - continue; - } - if (c === "'" && !subDouble) subSingle = !subSingle; - else if (c === '"' && !subSingle) subDouble = !subDouble; - else if (!subSingle && !subDouble) { - if (c === "(") depth += 1; - else if (c === ")") depth -= 1; - } - j += 1; - } - if (depth === 0) { - subs.push(command.slice(i + 2, j - 1)); - i = j; + const endIdx = scanBalancedDollarSub(command, i); + if (endIdx > 0) { + subs.push(command.slice(i + 2, endIdx - 1)); + i = endIdx; continue; } } @@ -831,7 +1028,8 @@ function extractCommandSubstitutions(command) { return subs; } -function readNextShellToken(str, startIdx) { +function readNextShellToken(str, startIdx, options = {}) { + const { preserveLiteralEscapes = false } = options; let i = startIdx; while (i < str.length && (str[i] === " " || str[i] === "\t")) { i += 1; @@ -840,35 +1038,243 @@ function readNextShellToken(str, startIdx) { let token = ""; let inSingle = false; let inDouble = false; + let sawAnyChar = false; while (i < str.length) { const ch = str[i]; if (!inSingle && !inDouble && " \t\r\n|&;()<>".includes(ch)) { break; } + if (!inSingle && ch === "$" && str[i + 1] === "(") { + const endIdx = scanBalancedDollarSub(str, i); + if (endIdx > 0) { + token += str.slice(i, endIdx); + sawAnyChar = true; + i = endIdx; + continue; + } + } + if (!inSingle && ch === "$" && str[i + 1] === "{") { + const closeIdx = findMatchingBrace(str, i + 1); + if (closeIdx > 0) { + token += str.slice(i, closeIdx + 1); + sawAnyChar = true; + i = closeIdx + 1; + continue; + } + } + if (!inSingle && ch === "`") { + let j = i + 1; + while (j < str.length) { + if (str[j] === "\\") { + j += 2; + continue; + } + if (str[j] === "`") break; + j += 1; + } + if (j < str.length) { + token += str.slice(i, j + 1); + sawAnyChar = true; + i = j + 1; + continue; + } + } if (ch === "\\" && !inSingle) { - if (i + 1 < str.length) token += str[i + 1]; + if (i + 1 < str.length) { + const next = str[i + 1]; + if (preserveLiteralEscapes && (next === "$" || next === "`" || (!sawAnyChar && next === "~"))) { + token += "\\" + next; + } else { + token += next; + } + } + sawAnyChar = true; i += 2; continue; } if (ch === "'" && !inDouble) { inSingle = !inSingle; + if (preserveLiteralEscapes && !sawAnyChar && str[i + 1] === "~") { + token += "\\~"; + sawAnyChar = true; + i += 2; + continue; + } + sawAnyChar = true; i += 1; continue; } if (ch === '"' && !inSingle) { inDouble = !inDouble; + if (preserveLiteralEscapes && !sawAnyChar && str[i + 1] === "~") { + token += "\\~"; + sawAnyChar = true; + i += 2; + continue; + } + sawAnyChar = true; + i += 1; + continue; + } + if (preserveLiteralEscapes && inSingle && (ch === "$" || ch === "`")) { + token += "\\" + ch; + sawAnyChar = true; i += 1; continue; } token += ch; + sawAnyChar = true; i += 1; } return { token, nextIdx: i }; } -function extractOutputRedirectTargets(command) { +function splitRawShellSegments(command) { + const items = []; + let start = 0; + let inSingle = false; + let inDouble = false; + let atTokenBoundary = true; + let connectorBefore = ""; + let i = 0; + + const pushSlice = (endIdx, connectorAfter = "") => { + const sub = command.slice(start, endIdx); + if (sub.trim().length > 0) { + items.push({ type: "segment", text: sub, connectorBefore, connectorAfter }); + } + }; + + while (i < command.length) { + const ch = command[i]; + if (ch === "\\" && !inSingle) { + if (command[i + 1] !== "\n") atTokenBoundary = false; + i += 2; + continue; + } + if (ch === "'" && !inDouble) { + inSingle = !inSingle; + atTokenBoundary = false; + i += 1; + continue; + } + if (ch === '"' && !inSingle) { + inDouble = !inDouble; + atTokenBoundary = false; + i += 1; + continue; + } + if (!inSingle && ch === "$" && command[i + 1] === "(") { + const endIdx = scanBalancedDollarSub(command, i); + if (endIdx > 0) { + atTokenBoundary = false; + i = endIdx; + continue; + } + } + if (!inSingle && ch === "$" && command[i + 1] === "{") { + const closeIdx = findMatchingBrace(command, i + 1); + if (closeIdx > 0) { + atTokenBoundary = false; + i = closeIdx + 1; + continue; + } + } + if (!inSingle && ch === "`") { + let j = i + 1; + while (j < command.length) { + if (command[j] === "\\") { + j += 2; + continue; + } + if (command[j] === "`") break; + j += 1; + } + if (j < command.length) { + atTokenBoundary = false; + i = j + 1; + continue; + } + } + if (!inSingle && !inDouble) { + if (ch === " " || ch === "\t" || ch === "\r") { + atTokenBoundary = true; + i += 1; + continue; + } + if (ch === "#" && atTokenBoundary) { + while (i < command.length && command[i] !== "\n") i += 1; + continue; + } + if (ch === "(" || ch === ")") { + pushSlice(i, ch); + items.push({ type: "paren", dir: ch }); + i += 1; + start = i; + connectorBefore = ch; + atTokenBoundary = true; + continue; + } + if (ch === "\n" || ch === ";") { + pushSlice(i, ch); + i += 1; + start = i; + connectorBefore = ch; + atTokenBoundary = true; + continue; + } + if (ch === "&" && command[i + 1] === ">") { + const opLen = command[i + 2] === ">" ? 3 : 2; + const { nextIdx } = readNextShellToken(command, i + opLen); + i = nextIdx; + atTokenBoundary = true; + continue; + } + if (ch === ">" && command[i + 1] === "&") { + const fdMatch = command.slice(i + 2).match(/^(?:\d+|-)(?=$|[\s|&;()<>])/); + if (fdMatch) { + i += 2 + fdMatch[0].length; + atTokenBoundary = true; + continue; + } + const { nextIdx } = readNextShellToken(command, i + 2); + i = nextIdx; + atTokenBoundary = true; + continue; + } + if (ch === "<" && command[i + 1] === "&") { + const fdMatch = command.slice(i + 2).match(/^(?:\d+|-)(?=$|[\s|&;()<>])/); + if (fdMatch) { + i += 2 + fdMatch[0].length; + atTokenBoundary = true; + continue; + } + const { nextIdx } = readNextShellToken(command, i + 2); + i = nextIdx; + atTokenBoundary = true; + continue; + } + if (ch === "|" || ch === "&") { + const op = command[i + 1] === ch ? ch + ch : ch; + pushSlice(i, op); + i += op.length; + start = i; + connectorBefore = op; + atTokenBoundary = true; + continue; + } + } + atTokenBoundary = false; + i += 1; + } + pushSlice(command.length, ""); + return items; +} + +function extractOutputRedirectTargets(command, activeEnv = {}) { const targets = []; + const envSnapshot = { ...activeEnv }; let i = 0; let inSingle = false; let inDouble = false; @@ -889,20 +1295,49 @@ function extractOutputRedirectTargets(command) { i += 1; continue; } + if (!inSingle && ch === "$" && command[i + 1] === "(") { + const endIdx = scanBalancedDollarSub(command, i); + if (endIdx > 0) { + i = endIdx; + continue; + } + } + if (!inSingle && ch === "$" && command[i + 1] === "{") { + const closeIdx = findMatchingBrace(command, i + 1); + if (closeIdx > 0) { + i = closeIdx + 1; + continue; + } + } + if (!inSingle && ch === "`") { + let j = i + 1; + while (j < command.length) { + if (command[j] === "\\") { + j += 2; + continue; + } + if (command[j] === "`") break; + j += 1; + } + if (j < command.length) { + i = j + 1; + continue; + } + } if (!inSingle && !inDouble && ch === "#" && (i === 0 || " \t\r\n;|(&".includes(command[i - 1]))) { while (i < command.length && command[i] !== "\n") i += 1; continue; } if (!inSingle && !inDouble && ch === "&" && command[i + 1] === ">") { const opLen = command[i + 2] === ">" ? 3 : 2; - const { token, nextIdx } = readNextShellToken(command, i + opLen); - targets.push(token); + const { token, nextIdx } = readNextShellToken(command, i + opLen, { preserveLiteralEscapes: true }); + targets.push({ target: token, env: envSnapshot }); i = nextIdx; continue; } if (!inSingle && !inDouble && ch === "<" && command[i + 1] === ">") { - const { token, nextIdx } = readNextShellToken(command, i + 2); - targets.push(token); + const { token, nextIdx } = readNextShellToken(command, i + 2, { preserveLiteralEscapes: true }); + targets.push({ target: token, env: envSnapshot }); i = nextIdx; continue; } @@ -913,14 +1348,14 @@ function extractOutputRedirectTargets(command) { i += 2 + fdMatch[0].length; continue; } - const { token, nextIdx } = readNextShellToken(command, i + 2); - targets.push(token); + const { token, nextIdx } = readNextShellToken(command, i + 2, { preserveLiteralEscapes: true }); + targets.push({ target: token, env: envSnapshot }); i = nextIdx; continue; } const opLen = command[i + 1] === ">" || command[i + 1] === "|" ? 2 : 1; - const { token, nextIdx } = readNextShellToken(command, i + opLen); - targets.push(token); + const { token, nextIdx } = readNextShellToken(command, i + opLen, { preserveLiteralEscapes: true }); + targets.push({ target: token, env: envSnapshot }); i = nextIdx; continue; } @@ -929,9 +1364,8 @@ function extractOutputRedirectTargets(command) { return targets; } -function tokenizeShellSegments(command) { - const segments = []; - let current = []; +function tokenizeSingleSegment(segment) { + const tokens = []; let token = ""; let hasTokenChars = false; let inSingle = false; @@ -940,29 +1374,21 @@ function tokenizeShellSegments(command) { const flushToken = () => { if (hasTokenChars) { - current.push(token); + tokens.push(token); token = ""; hasTokenChars = false; } }; - const flushSegment = () => { - flushToken(); - if (current.length > 0) { - segments.push(current); - current = []; - } - }; - - while (i < command.length) { - const ch = command[i]; + while (i < segment.length) { + const ch = segment[i]; if (ch === "\\" && !inSingle) { - if (i + 1 < command.length && command[i + 1] === "\n") { + if (i + 1 < segment.length && segment[i + 1] === "\n") { i += 2; continue; } hasTokenChars = true; - if (i + 1 < command.length) token += command[i + 1]; + if (i + 1 < segment.length) token += segment[i + 1]; i += 2; continue; } @@ -978,32 +1404,56 @@ function tokenizeShellSegments(command) { i += 1; continue; } - if (!inSingle && !inDouble) { - if (ch === " " || ch === "\t" || ch === "\r") { - flushToken(); - i += 1; + if (!inSingle && ch === "$" && segment[i + 1] === "(") { + const endIdx = scanBalancedDollarSub(segment, i); + if (endIdx > 0) { + hasTokenChars = true; + token += segment.slice(i, endIdx); + i = endIdx; continue; } - if (ch === "#" && !hasTokenChars) { - while (i < command.length && command[i] !== "\n") i += 1; + } + if (!inSingle && ch === "$" && segment[i + 1] === "{") { + const closeIdx = findMatchingBrace(segment, i + 1); + if (closeIdx > 0) { + hasTokenChars = true; + token += segment.slice(i, closeIdx + 1); + i = closeIdx + 1; + continue; + } + } + if (!inSingle && ch === "`") { + let j = i + 1; + while (j < segment.length) { + if (segment[j] === "\\") { + j += 2; + continue; + } + if (segment[j] === "`") break; + j += 1; + } + if (j < segment.length) { + hasTokenChars = true; + token += segment.slice(i, j + 1); + i = j + 1; continue; } - if (ch === "\n" || ch === ";" || ch === "(" || ch === ")") { - flushSegment(); + } + if (!inSingle && !inDouble) { + if (ch === " " || ch === "\t" || ch === "\r") { + flushToken(); i += 1; continue; } - if (ch === "|" || ch === "&") { - if (ch === "&" && command[i + 1] === ">") { - flushToken(); - const opLen = command[i + 2] === ">" ? 3 : 2; - const { nextIdx } = readNextShellToken(command, i + opLen); - i = nextIdx; - continue; - } - flushSegment(); - if (command[i + 1] === ch) i += 2; - else i += 1; + if (ch === "#" && !hasTokenChars) { + while (i < segment.length && segment[i] !== "\n") i += 1; + continue; + } + if (ch === "&" && segment[i + 1] === ">") { + flushToken(); + const opLen = segment[i + 2] === ">" ? 3 : 2; + const { nextIdx } = readNextShellToken(segment, i + opLen); + i = nextIdx; continue; } if (ch === ">" || ch === "<") { @@ -1013,29 +1463,32 @@ function tokenizeShellSegments(command) { } else { flushToken(); } - if (ch === "<" && command[i + 1] === "<" && command[i + 2] === "<") { - const { nextIdx } = readNextShellToken(command, i + 3); + if (ch === "<" && segment[i + 1] === "<" && segment[i + 2] === "<") { + const { nextIdx } = readNextShellToken(segment, i + 3); i = nextIdx; continue; } - if (ch === "<" && command[i + 1] === "<") { - const opLen = command[i + 2] === "-" ? 3 : 2; - const { nextIdx } = readNextShellToken(command, i + opLen); + if (ch === "<" && segment[i + 1] === "<") { + const opLen = segment[i + 2] === "-" ? 3 : 2; + const { nextIdx } = readNextShellToken(segment, i + opLen); i = nextIdx; continue; } - if (ch === ">" && command[i + 1] === "&") { - const fdMatch = command.slice(i + 2).match(/^(?:\d+|-)/); + if ((ch === ">" || ch === "<") && segment[i + 1] === "&") { + const fdMatch = segment.slice(i + 2).match(/^(?:\d+|-)/); if (fdMatch) { i += 2 + fdMatch[0].length; continue; } - const { nextIdx } = readNextShellToken(command, i + 2); + const { nextIdx } = readNextShellToken(segment, i + 2); i = nextIdx; continue; } - const opLen = command[i + 1] === ch || command[i + 1] === "|" || command[i + 1] === "&" || command[i + 1] === ">" ? 2 : 1; - const { nextIdx } = readNextShellToken(command, i + opLen); + const opLen = + segment[i + 1] === ch || segment[i + 1] === "|" || segment[i + 1] === "&" || segment[i + 1] === ">" + ? 2 + : 1; + const { nextIdx } = readNextShellToken(segment, i + opLen); i = nextIdx; continue; } @@ -1045,32 +1498,110 @@ function tokenizeShellSegments(command) { i += 1; } - flushSegment(); - return segments; + flushToken(); + return tokens; +} + +function assignEnvVar(envObj, outerEnv, key, rawVal) { + const priorVal = + typeof envObj[key] === "string" + ? envObj[key] + : outerEnv && typeof outerEnv[key] === "string" + ? outerEnv[key] + : undefined; + if (priorVal !== undefined && rawVal.includes("$")) { + const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + const selfExpanded = rawVal + .replace(new RegExp(`\\$\\{${escapedKey}\\}`, "g"), priorVal) + .replace(new RegExp(`\\$${escapedKey}(?![A-Za-z0-9_])`, "g"), priorVal); + envObj[key] = selfExpanded; + return; + } + envObj[key] = rawVal; } -function unwrapCommandSegment(tokens, inheritedEnv = {}) { +const SHELL_KEYWORD_PREFIXES = new Set([ + "if", + "then", + "else", + "elif", + "fi", + "while", + "until", + "do", + "done", + "esac", + "{", + "}", + "!", +]); + +const READ_VALUE_FLAGS = new Set(["-p", "-t", "-n", "-N", "-d", "-u", "-i", "-a"]); + +function unwrapCommandSegment(tokens, inheritedEnv = {}, outerEnv = {}) { const env = { ...inheritedEnv }; + const assignedKeys = []; + const taintedVars = []; let idx = 0; while (idx < tokens.length) { const tok = tokens[idx]; + if (SHELL_KEYWORD_PREFIXES.has(tok)) { + idx += 1; + continue; + } + if ((tok === "for" || tok === "select") && idx === 0) { + if (idx + 1 < tokens.length && /^[A-Za-z_][A-Za-z0-9_]*$/.test(tokens[idx + 1])) { + taintedVars.push(tokens[idx + 1]); + } + return { cmd: "", rawCmd: "", args: [], env, assignedKeys, taintedVars }; + } + if (tok === "case" && idx === 0) { + return { cmd: "", rawCmd: "", args: [], env, assignedKeys, taintedVars }; + } if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(tok)) { const eq = tok.indexOf("="); - env[tok.slice(0, eq)] = tok.slice(eq + 1); + const key = tok.slice(0, eq); + assignEnvVar(env, outerEnv, key, tok.slice(eq + 1)); + assignedKeys.push(key); idx += 1; continue; } const base = basename(tok); - if (base === "export" && idx === 0) { - for (let j = 1; j < tokens.length; j += 1) { + if ( + (base === "export" || base === "local" || base === "readonly" || base === "declare" || base === "typeset") && + tok === base + ) { + for (let j = idx + 1; j < tokens.length; j += 1) { const arg = tokens[j]; if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(arg)) { const eq = arg.indexOf("="); - env[arg.slice(0, eq)] = arg.slice(eq + 1); + const key = arg.slice(0, eq); + assignEnvVar(env, outerEnv, key, arg.slice(eq + 1)); + assignedKeys.push(key); + } + } + return { cmd: "export", rawCmd: tok, args: tokens.slice(idx + 1), env, assignedKeys, taintedVars }; + } + if (base === "unset" && tok === "unset") { + for (let j = idx + 1; j < tokens.length; j += 1) { + const arg = tokens[j]; + if (!arg.startsWith("-")) delete env[arg]; + } + return { cmd: "unset", rawCmd: tok, args: tokens.slice(idx + 1), env, assignedKeys, taintedVars }; + } + if (base === "read" && tok === "read") { + for (let j = idx + 1; j < tokens.length; j += 1) { + const arg = tokens[j]; + if (arg.startsWith("-") && arg.length > 1) { + if (READ_VALUE_FLAGS.has(arg)) j += 1; + continue; + } + if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(arg)) { + taintedVars.push(arg); } } - return { cmd: "export", args: tokens.slice(1), env }; + return { cmd: "read", rawCmd: tok, args: tokens.slice(idx + 1), env, assignedKeys, taintedVars }; } if (base === "env") { idx += 1; @@ -1081,12 +1612,13 @@ function unwrapCommandSegment(tokens, inheritedEnv = {}) { continue; } if (envArg === "-u") { + if (idx + 1 < tokens.length) delete env[tokens[idx + 1]]; idx += 2; continue; } if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(envArg)) { const eq = envArg.indexOf("="); - env[envArg.slice(0, eq)] = envArg.slice(eq + 1); + assignEnvVar(env, outerEnv, envArg.slice(0, eq), envArg.slice(eq + 1)); idx += 1; continue; } @@ -1128,42 +1660,85 @@ function unwrapCommandSegment(tokens, inheritedEnv = {}) { } if (idx >= tokens.length) { - return { cmd: "", args: [], env }; + return { cmd: "", rawCmd: "", args: [], env, assignedKeys, taintedVars }; } return { cmd: basename(tokens[idx]), + rawCmd: tokens[idx], args: tokens.slice(idx + 1), env, + assignedKeys, + taintedVars, }; } -function collectInvocations(command, depth = 0) { +function replaceObjectKeys(target, source) { + for (const k of Object.keys(target)) delete target[k]; + Object.assign(target, source); +} + +function collectInvocations(command, depth = 0, inheritedEnv = {}, outerEnv = {}) { if (depth > 8 || !command) return { invocations: [], redirectTargets: [] }; const { strippedCommand, unquotedBodies } = preprocessHeredocs(command); - const redirectTargets = [...extractOutputRedirectTargets(strippedCommand)]; + const redirectTargets = []; const invocations = []; - const scriptEnv = {}; + const scriptEnv = { ...inheritedEnv }; + const envStack = []; + + const rawItems = splitRawShellSegments(strippedCommand); + for (const item of rawItems) { + if (item.type === "paren") { + if (item.dir === "(") { + envStack.push({ ...scriptEnv }); + } else if (envStack.length > 0) { + replaceObjectKeys(scriptEnv, envStack.pop()); + } + continue; + } - const substitutions = [...extractCommandSubstitutions(strippedCommand)]; - for (const body of unquotedBodies) { - substitutions.push(...extractCommandSubstitutions(body)); - } + const rawSeg = item.text; + const isPipelineStage = item.connectorBefore === "|" || item.connectorAfter === "|"; + const isConditional = item.connectorBefore === "&&" || item.connectorBefore === "||"; + const tokens = tokenizeSingleSegment(rawSeg); + const parsed = unwrapCommandSegment(tokens, scriptEnv, outerEnv); - for (const sub of substitutions) { - const subRes = collectInvocations(sub, depth + 1); - redirectTargets.push(...subRes.redirectTargets); - invocations.push(...subRes.invocations); - } + for (const sub of extractCommandSubstitutions(rawSeg)) { + const subRes = collectInvocations(sub, depth + 1, parsed.env, outerEnv); + redirectTargets.push(...subRes.redirectTargets); + invocations.push(...subRes.invocations); + } + + redirectTargets.push(...extractOutputRedirectTargets(rawSeg, parsed.env)); + + for (const tv of parsed.taintedVars) { + if (!isPipelineStage) { + parsed.env[tv] = "$__TAINTED__"; + scriptEnv[tv] = "$__TAINTED__"; + } + } - const rawSegments = tokenizeShellSegments(strippedCommand); - for (const seg of rawSegments) { - const parsed = unwrapCommandSegment(seg, scriptEnv); if (!parsed.cmd) { - Object.assign(scriptEnv, parsed.env); + if (!isPipelineStage) { + if (isConditional) { + for (const key of parsed.assignedKeys) { + scriptEnv[key] = "$__TAINTED__"; + } + } else { + Object.assign(scriptEnv, parsed.env); + } + } continue; } - if (parsed.cmd === "export") { - Object.assign(scriptEnv, parsed.env); + if (parsed.cmd === "export" || parsed.cmd === "unset" || parsed.cmd === "read") { + if (!isPipelineStage) { + if (isConditional) { + for (const key of parsed.assignedKeys) { + scriptEnv[key] = "$__TAINTED__"; + } + } else { + replaceObjectKeys(scriptEnv, parsed.env); + } + } continue; } invocations.push(parsed); @@ -1173,20 +1748,24 @@ function collectInvocations(command, depth = 0) { const arg = parsed.args[i]; if (arg === "--") break; if (/^-[a-zA-Z]*c$/.test(arg) && i + 1 < parsed.args.length) { - const inner = collectInvocations(parsed.args[i + 1], depth + 1); + const inner = collectInvocations(parsed.args[i + 1], depth + 1, parsed.env, outerEnv); redirectTargets.push(...inner.redirectTargets); - for (const inv of inner.invocations) { - invocations.push({ ...inv, env: { ...parsed.env, ...inv.env } }); - } + invocations.push(...inner.invocations); break; } } } else if (parsed.cmd === "eval" && parsed.args.length > 0) { - const inner = collectInvocations(parsed.args.join(" "), depth + 1); + const inner = collectInvocations(parsed.args.join(" "), depth + 1, parsed.env, outerEnv); redirectTargets.push(...inner.redirectTargets); - for (const inv of inner.invocations) { - invocations.push({ ...inv, env: { ...parsed.env, ...inv.env } }); - } + invocations.push(...inner.invocations); + } + } + + for (const body of unquotedBodies) { + for (const sub of extractCommandSubstitutions(body)) { + const subRes = collectInvocations(sub, depth + 1, scriptEnv, outerEnv); + redirectTargets.push(...subRes.redirectTargets); + invocations.push(...subRes.invocations); } } @@ -1630,22 +2209,154 @@ function parseStaticPyString(expr) { return { ok: true, value: unescapeSimpleLiteral(inner, isRaw) }; } -function checkInlineNodeFs(code, allowReadOnlyTempPath) { - if (/\[[^\]]*(?:writeFile|appendFile|unlink|rm|rmdir|rename|copyFile|cp|mkdir|mkdtemp|chmod|chown|truncate|symlink|link|createWriteStream|openSync)/.test(code)) { - return "Dynamic fs mutation via inline Node script is blocked for read-only ro-* workers."; +const JS_NON_MEMBER_PREFIX_KEYWORDS = new Set([ + "return", + "typeof", + "await", + "yield", + "throw", + "delete", + "void", + "case", + "in", + "of", + "else", + "new", +]); + +const NODE_MUTATION_NAME_RE = + /\b(?:writeFileSync|writeFile|appendFileSync|appendFile|unlinkSync|unlink|rmSync|rm|rmdirSync|rmdir|renameSync|rename|copyFileSync|copyFile|cpSync|cp|mkdirSync|mkdir|mkdtempSync|mkdtemp|chmodSync|chmod|chownSync|chown|truncateSync|truncate|symlinkSync|symlink|linkSync|link|createWriteStream|openSync)\b/; + +const PYTHON_MUTATION_NAME_RE = + /\b(?:remove|unlink|rmdir|removedirs|mkdir|makedirs|chmod|chown|truncate|rename|renames|replace|rmtree|move|copy|copy2|copyfile|copytree|symlink|link|write_text|write_bytes|touch|lchmod|symlink_to|hardlink_to)\b/; + +function isVarBoundToVerifiedTmpRoot(varName, code) { + if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(varName)) return false; + const varEsc = varName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + const assignRe = new RegExp( + `(?:^|[\\n;])\\s*(?:(?:const|let|var)\\s+)?${varEsc}\\s*=\\s*([^\\n;]+)`, + "g", + ); + let lastRhs = null; + let m; + while ((m = assignRe.exec(code)) !== null) { + lastRhs = m[1].trim(); + } + if (!lastRhs || lastRhs.includes("..")) return false; + return /^(?:[fFrRuUbB]{0,2}['"`](?:\/private)?\/tmp(?:\/|['"`\b])|[^\n;]*(?:mkdtemp|mkdtempSync|gettempdir|tmpdir)\s*\(|os\.environ\.get\(\s*['"]TMPDIR['"])/.test( + lastRhs, + ); +} + +function isSafeTmpTemplateOrJoin(expr, code, allowPath) { + if (!allowPath("/tmp/__pstack_probe__", { allowRootEqual: false, allowDevNull: false })) { + return false; + } + const trimmed = expr.trim(); + if (trimmed.includes("..")) return false; + if (/^`(?:\/private)?\/tmp\/[^`]+`$/.test(trimmed)) { + return true; + } + const jsVarTemplate = trimmed.match(/^`\$\{([A-Za-z_][A-Za-z0-9_]*)\}(?:\/[^`]*)?`$/); + if (jsVarTemplate && isVarBoundToVerifiedTmpRoot(jsVarTemplate[1], code)) { + return true; + } + if ( + /^[fF](?:'''[\s\S]*'''|"""[\s\S]*"""|'[^']*'|"[^"]*")$/.test(trimmed) && + /^[fF]['"]{1,3}(?:\/private)?\/tmp\//.test(trimmed) + ) { + return true; + } + const pyVarTemplate = trimmed.match(/^[fF](?:'''|"""|'|\")\{([A-Za-z_][A-Za-z0-9_]*)\}(?:\/[^'"]*)?(?:'''|"""|'|\")$/); + if (pyVarTemplate && isVarBoundToVerifiedTmpRoot(pyVarTemplate[1], code)) { + return true; + } + const joinMatch = trimmed.match(/^(?:path\s*\.\s*join|os\s*\.\s*path\s*\.\s*join|join)\s*\(([\s\S]*)\)$/); + if (joinMatch) { + const innerArgs = parseCallArguments(`(${joinMatch[1]})`, 0); + if (innerArgs && innerArgs.length >= 1) { + if (innerArgs.some((a) => a.includes(".."))) return false; + const rootArg = innerArgs[0].trim(); + if ( + /^['"`](?:\/private)?\/tmp(?:\/|['"`]$)/.test(rootArg) || + /\b(?:mkdtemp|mkdtempSync|gettempdir|tmpdir)\s*\(/.test(rootArg) + ) { + return true; + } + if (isVarBoundToVerifiedTmpRoot(rootArg, code)) { + return true; + } + } + } + return false; +} + +function resolveSimpleVarBinding(code, expr, isPython) { + const trimmed = expr.trim(); + if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(trimmed)) return null; + const varEsc = trimmed.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + const assignRe = new RegExp(`(?:^|[\\n;])\\s*(?:(?:const|let|var)\\s+)?${varEsc}\\s*=\\s*([^\\n;]+)`, "g"); + let lastRhs = null; + let m; + while ((m = assignRe.exec(code)) !== null) { + lastRhs = m[1].trim(); + } + if (!lastRhs) return null; + return isPython ? parseStaticPyString(lastRhs) : parseStaticJsString(lastRhs); +} + +function checkInlineNodeFs(code, allowPath, options = {}) { + const { allowDynamicPaths = false, roleLabel = "read-only ro-* workers" } = options; + + const bracketRe = /(?:([A-Za-z_$][A-Za-z0-9_$]*)|([)\]'"]))\s*\[([^\]]*)\]/g; + let bm; + while ((bm = bracketRe.exec(code)) !== null) { + const ident = bm[1] || ""; + const bracketBody = bm[3] || ""; + if (ident && JS_NON_MEMBER_PREFIX_KEYWORDS.has(ident)) continue; + const afterBracket = code.slice(bm.index + bm[0].length); + const isFsReceiver = + ident === "fs" || + ident === "fsp" || + ident === "promises" || + /require\s*\(\s*['"](?:node:)?fs(?:\/promises)?['"]\s*\)\s*$/.test(code.slice(0, bm.index + (bm[2] ? 1 : 0))); + if (NODE_MUTATION_NAME_RE.test(bracketBody) || (isFsReceiver && /^\s*\(/.test(afterBracket))) { + if (!allowDynamicPaths) { + return `Dynamic fs mutation via inline Node script is blocked for ${roleLabel}.`; + } + } } if ( - /\.\s*(writeFileSync|writeFile|appendFileSync|appendFile|unlinkSync|unlink|rmSync|rm|rmdirSync|rmdir|renameSync|rename|copyFileSync|copyFile|cpSync|cp|mkdirSync|mkdir|mkdtempSync|mkdtemp|chmodSync|chmod|chownSync|chown|truncateSync|truncate|symlinkSync|symlink|linkSync|link|createWriteStream|openSync)\b(?!\s*\()/.test( + !allowDynamicPaths && + (/\.\s*(writeFileSync|writeFile|appendFileSync|appendFile|unlinkSync|unlink|rmSync|rm|rmdirSync|rmdir|renameSync|rename|copyFileSync|copyFile|cpSync|cp|mkdirSync|mkdir|mkdtempSync|mkdtemp|chmodSync|chmod|chownSync|chown|truncateSync|truncate|symlinkSync|symlink|linkSync|link|createWriteStream|openSync)\b(?!\s*\()/.test( code, ) || - /\b(writeFileSync|writeFile|appendFileSync|appendFile|unlinkSync|unlink|rmSync|rm|rmdirSync|rmdir|renameSync|rename|copyFileSync|copyFile|cpSync|cp|mkdirSync|mkdir|mkdtempSync|mkdtemp|chmodSync|chmod|chownSync|chown|truncateSync|truncate|symlinkSync|symlink|linkSync|link|createWriteStream|openSync)\s*(?::|\bas\b)/.test( - code, - ) + /\b(writeFileSync|writeFile|appendFileSync|appendFile|unlinkSync|unlink|rmSync|rm|rmdirSync|rmdir|renameSync|rename|copyFileSync|copyFile|cpSync|cp|mkdirSync|mkdir|mkdtempSync|mkdtemp|chmodSync|chmod|chownSync|chown|truncateSync|truncate|symlinkSync|symlink|linkSync|link|createWriteStream|openSync)\s*\bas\b/.test( + code, + ) || + /(?:const|let|var|\(|^|[;\n,])\s*\{[^}]*\b(?:writeFileSync|writeFile|appendFileSync|appendFile|unlinkSync|unlink|rmSync|rm|rmdirSync|rmdir|renameSync|rename|copyFileSync|copyFile|cpSync|cp|mkdirSync|mkdir|mkdtempSync|mkdtemp|chmodSync|chmod|chownSync|chown|truncateSync|truncate|symlinkSync|symlink|linkSync|link|createWriteStream|openSync)\s*:[^}]*\}\s*=/.test( + code, + )) ) { - return "Aliased fs mutation API in inline Node script is blocked for read-only ro-* workers."; + return `Aliased fs mutation API in inline Node script is blocked for ${roleLabel}.`; } + const checkNodePathArg = (argExpr, opts) => { + const lit = parseStaticJsString(argExpr || ""); + if (lit.ok) { + return allowPath(lit.value, opts); + } + const bound = resolveSimpleVarBinding(code, argExpr || "", false); + if (bound && bound.ok) { + return allowPath(bound.value, opts); + } + if (isSafeTmpTemplateOrJoin(argExpr || "", code, allowPath)) { + return true; + } + return allowDynamicPaths; + }; + const callRe = /\b(writeFileSync|writeFile|appendFileSync|appendFile|unlinkSync|unlink|rmSync|rm|rmdirSync|rmdir|renameSync|rename|copyFileSync|copyFile|cpSync|cp|mkdirSync|mkdir|mkdtempSync|mkdtemp|chmodSync|chmod|chownSync|chown|truncateSync|truncate|symlinkSync|symlink|linkSync|link|createWriteStream|openSync|open)\s*\(/g; @@ -1658,7 +2369,7 @@ function checkInlineNodeFs(code, allowReadOnlyTempPath) { const openParenIdx = match.index + match[0].length - 1; const callArgs = parseCallArguments(code, openParenIdx); if (!callArgs) { - return `Unparseable fs.${fnName} call in inline Node script is blocked for read-only ro-* workers.`; + return `Unparseable fs.${fnName} call in inline Node script is blocked for ${roleLabel}.`; } if (fnName === "open" || fnName === "openSync") { @@ -1670,41 +2381,39 @@ function checkInlineNodeFs(code, allowReadOnlyTempPath) { } else { continue; } - const pathLit = parseStaticJsString(callArgs[0] || ""); - if (!pathLit.ok || !allowReadOnlyTempPath(pathLit.value, { allowRootEqual: false, allowDevNull: true })) { - return `Inline Node fs.${fnName} write outside canonical temp roots is blocked for read-only ro-* workers.`; + if (!checkNodePathArg(callArgs[0], { allowRootEqual: false, allowDevNull: true })) { + return `Inline Node fs.${fnName} write outside allowed roots is blocked for ${roleLabel}.`; } continue; } if (NODE_SINGLE_PATH_FS_APIS.has(fnName)) { - const pathLit = parseStaticJsString(callArgs[0] || ""); const allowRoot = fnName === "mkdirSync" || fnName === "mkdir"; - const allowNull = fnName === "writeFileSync" || fnName === "writeFile" || fnName === "appendFileSync" || fnName === "appendFile" || fnName === "createWriteStream"; - if (!pathLit.ok || !allowReadOnlyTempPath(pathLit.value, { allowRootEqual: allowRoot, allowDevNull: allowNull })) { - return `Inline Node fs.${fnName} outside canonical temp roots is blocked for read-only ro-* workers.`; + const allowNull = + fnName === "writeFileSync" || + fnName === "writeFile" || + fnName === "appendFileSync" || + fnName === "appendFile" || + fnName === "createWriteStream"; + if (!checkNodePathArg(callArgs[0], { allowRootEqual: allowRoot, allowDevNull: allowNull })) { + return `Inline Node fs.${fnName} outside allowed roots is blocked for ${roleLabel}.`; } continue; } if (NODE_COPY_FS_APIS.has(fnName)) { - const dstLit = parseStaticJsString(callArgs[1] || ""); - if (!dstLit.ok || !allowReadOnlyTempPath(dstLit.value, { allowRootEqual: true, allowDevNull: true })) { - return `Inline Node fs.${fnName} destination outside canonical temp roots is blocked for read-only ro-* workers.`; + if (!checkNodePathArg(callArgs[1], { allowRootEqual: true, allowDevNull: true })) { + return `Inline Node fs.${fnName} destination outside allowed roots is blocked for ${roleLabel}.`; } continue; } if (NODE_TWO_PATH_FS_APIS.has(fnName)) { - const firstLit = parseStaticJsString(callArgs[0] || ""); - const secondLit = parseStaticJsString(callArgs[1] || ""); if ( - !firstLit.ok || - !secondLit.ok || - !allowReadOnlyTempPath(firstLit.value, { allowRootEqual: false, allowDevNull: false }) || - !allowReadOnlyTempPath(secondLit.value, { allowRootEqual: true, allowDevNull: false }) + !checkNodePathArg(callArgs[0], { allowRootEqual: false, allowDevNull: false }) || + !checkNodePathArg(callArgs[1], { allowRootEqual: true, allowDevNull: false }) ) { - return `Inline Node fs.${fnName} outside canonical temp roots is blocked for read-only ro-* workers.`; + return `Inline Node fs.${fnName} outside allowed roots is blocked for ${roleLabel}.`; } } } @@ -1726,17 +2435,59 @@ function splitPythonCallArgs(callArgs) { return { positionals, kwargs }; } -function checkInlinePythonFs(code, allowReadOnlyTempPath) { +function hasDisallowedPythonAlias(code) { if ( /\.\s*(write_text|write_bytes|unlink|rmdir|mkdir|touch|chmod|lchmod|rename|replace|symlink_to|hardlink_to|remove|removedirs|makedirs|chown|truncate|renames|rmtree|move|copy|copy2|copyfile|copytree|symlink|link)\b(?!\s*\()/.test( code, ) || - /\bfrom\s+(?:os|posix|shutil|pathlib)\s+import\b[^\n;]*\bas\b/.test(code) || /(?:^|[^\w.])=\s*(?:builtins\s*\.\s*|io\s*\.\s*)?open\b(?!\s*\()/.test(code) ) { - return "Aliased filesystem mutation API in inline Python script is blocked for read-only ro-* workers."; + return true; + } + const importRe = /\bimport\s+(?:os|posix|shutil)\s+as\s+([A-Za-z_][A-Za-z0-9_]*)/g; + let im; + while ((im = importRe.exec(code)) !== null) { + const alias = im[1].replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + if (new RegExp(`\\b${alias}\\s*\\.\\s*${PYTHON_MUTATION_NAME_RE.source}`).test(code)) { + return true; + } + } + const fromImportRe = /\bfrom\s+(os|posix|shutil|pathlib)\s+import\s+([^\n;]+)/g; + while ((im = fromImportRe.exec(code)) !== null) { + const mod = im[1]; + const clause = im[2]; + if (!/\bas\b/.test(clause)) continue; + if (mod === "pathlib") { + if (PYTHON_MUTATION_NAME_RE.test(clause)) return true; + continue; + } + if (PYTHON_MUTATION_NAME_RE.test(clause)) return true; + } + return false; +} + +function checkInlinePythonFs(code, allowPath, options = {}) { + const { allowDynamicPaths = false, roleLabel = "read-only ro-* workers" } = options; + + if (!allowDynamicPaths && hasDisallowedPythonAlias(code)) { + return `Aliased filesystem mutation API in inline Python script is blocked for ${roleLabel}.`; } + const checkPyPathArg = (argExpr, opts) => { + const lit = parseStaticPyString(argExpr || ""); + if (lit.ok) { + return allowPath(lit.value, opts); + } + const bound = resolveSimpleVarBinding(code, argExpr || "", true); + if (bound && bound.ok) { + return allowPath(bound.value, opts); + } + if (isSafeTmpTemplateOrJoin(argExpr || "", code, allowPath)) { + return true; + } + return allowDynamicPaths; + }; + const pathMethodRe = /\.\s*(write_text|write_bytes|unlink|rmdir|mkdir|touch|chmod|lchmod|rename|replace|symlink_to|hardlink_to|open)\s*\(/g; let match; @@ -1745,7 +2496,7 @@ function checkInlinePythonFs(code, allowReadOnlyTempPath) { const openParenIdx = match.index + match[0].length - 1; const methodArgs = parseCallArguments(code, openParenIdx); if (!methodArgs) { - return `Unparseable .${method}() call in inline Python script is blocked for read-only ro-* workers.`; + return `Unparseable .${method}() call in inline Python script is blocked for ${roleLabel}.`; } if (method === "open") { const { positionals, kwargs } = splitPythonCallArgs(methodArgs); @@ -1759,18 +2510,19 @@ function checkInlinePythonFs(code, allowReadOnlyTempPath) { const prefix = code.slice(0, match.index); const ctorMatch = prefix.match(/(?:pathlib\s*\.\s*)?Path\s*\(\s*([^()]*)\s*\)\s*$/); if (!ctorMatch) { - return `Inline Python Path.${method}() on non-literal receiver is blocked for read-only ro-* workers.`; + if (!allowDynamicPaths) { + return `Inline Python Path.${method}() on non-literal receiver is blocked for ${roleLabel}.`; + } + continue; } const ctorArgs = parseCallArguments(`(${ctorMatch[1]})`, 0); - const receiverLit = ctorArgs && ctorArgs.length === 1 ? parseStaticPyString(ctorArgs[0]) : { ok: false, value: "" }; const allowRoot = method === "mkdir"; - if (!receiverLit.ok || !allowReadOnlyTempPath(receiverLit.value, { allowRootEqual: allowRoot, allowDevNull: false })) { - return `Inline Python Path.${method}() outside canonical temp roots is blocked for read-only ro-* workers.`; + if (!ctorArgs || ctorArgs.length !== 1 || !checkPyPathArg(ctorArgs[0], { allowRootEqual: allowRoot, allowDevNull: false })) { + return `Inline Python Path.${method}() outside allowed roots is blocked for ${roleLabel}.`; } if (method === "rename" || method === "replace") { - const dstLit = parseStaticPyString(methodArgs[0] || ""); - if (!dstLit.ok || !allowReadOnlyTempPath(dstLit.value, { allowRootEqual: true, allowDevNull: false })) { - return `Inline Python Path.${method}() target outside canonical temp roots is blocked for read-only ro-* workers.`; + if (!checkPyPathArg(methodArgs[0] || "", { allowRootEqual: true, allowDevNull: false })) { + return `Inline Python Path.${method}() target outside allowed roots is blocked for ${roleLabel}.`; } } } @@ -1780,7 +2532,7 @@ function checkInlinePythonFs(code, allowReadOnlyTempPath) { const openParenIdx = match.index + match[0].length - 1; const openArgs = parseCallArguments(code, openParenIdx); if (!openArgs) { - return "Unparseable open() call in inline Python script is blocked for read-only ro-* workers."; + return `Unparseable open() call in inline Python script is blocked for ${roleLabel}.`; } const { positionals, kwargs } = splitPythonCallArgs(openArgs); const modeExpr = positionals[1] ?? kwargs.mode ?? null; @@ -1790,9 +2542,8 @@ function checkInlinePythonFs(code, allowReadOnlyTempPath) { continue; } const pathExpr = positionals[0] ?? kwargs.file ?? ""; - const pathLit = parseStaticPyString(pathExpr); - if (!pathLit.ok || !allowReadOnlyTempPath(pathLit.value, { allowRootEqual: false, allowDevNull: true })) { - return "Inline Python open() for write outside canonical temp roots is blocked for read-only ro-* workers."; + if (!checkPyPathArg(pathExpr, { allowRootEqual: false, allowDevNull: true })) { + return `Inline Python open() for write outside allowed roots is blocked for ${roleLabel}.`; } } @@ -1809,34 +2560,28 @@ function checkInlinePythonFs(code, allowReadOnlyTempPath) { const openParenIdx = match.index + match[0].length - 1; const callArgs = parseCallArguments(code, openParenIdx); if (!callArgs) { - return `Unparseable ${fnName}() call in inline Python script is blocked for read-only ro-* workers.`; + return `Unparseable ${fnName}() call in inline Python script is blocked for ${roleLabel}.`; } const { positionals } = splitPythonCallArgs(callArgs); if (["copy", "copy2", "copyfile", "copytree"].includes(fnName)) { if (!qualifier && !/\bshutil\b/.test(code)) continue; - const dstLit = parseStaticPyString(positionals[1] || ""); - if (!dstLit.ok || !allowReadOnlyTempPath(dstLit.value, { allowRootEqual: true, allowDevNull: true })) { - return `Inline Python ${fnName}() destination outside canonical temp roots is blocked for read-only ro-* workers.`; + if (!checkPyPathArg(positionals[1] || "", { allowRootEqual: true, allowDevNull: true })) { + return `Inline Python ${fnName}() destination outside allowed roots is blocked for ${roleLabel}.`; } continue; } if (["rename", "renames", "replace", "move", "symlink", "link"].includes(fnName)) { - const srcLit = parseStaticPyString(positionals[0] || ""); - const dstLit = parseStaticPyString(positionals[1] || ""); if ( - !srcLit.ok || - !dstLit.ok || - !allowReadOnlyTempPath(srcLit.value, { allowRootEqual: false, allowDevNull: false }) || - !allowReadOnlyTempPath(dstLit.value, { allowRootEqual: true, allowDevNull: false }) + !checkPyPathArg(positionals[0] || "", { allowRootEqual: false, allowDevNull: false }) || + !checkPyPathArg(positionals[1] || "", { allowRootEqual: true, allowDevNull: false }) ) { - return `Inline Python ${fnName}() outside canonical temp roots is blocked for read-only ro-* workers.`; + return `Inline Python ${fnName}() outside allowed roots is blocked for ${roleLabel}.`; } continue; } - const pathLit = parseStaticPyString(positionals[0] || ""); const allowRoot = fnName === "mkdir" || fnName === "makedirs"; - if (!pathLit.ok || !allowReadOnlyTempPath(pathLit.value, { allowRootEqual: allowRoot, allowDevNull: false })) { - return `Inline Python ${fnName}() outside canonical temp roots is blocked for read-only ro-* workers.`; + if (!checkPyPathArg(positionals[0] || "", { allowRootEqual: allowRoot, allowDevNull: false })) { + return `Inline Python ${fnName}() outside allowed roots is blocked for ${roleLabel}.`; } } @@ -1844,18 +2589,36 @@ function checkInlinePythonFs(code, allowReadOnlyTempPath) { } function parseGitCommand(args) { + const globalDirs = []; let i = 0; while (i < args.length) { const arg = args[i]; + if (arg === "-C" || arg === "--git-dir" || arg === "--work-tree") { + if (i + 1 < args.length) globalDirs.push(args[i + 1]); + i += 2; + continue; + } + if (arg.startsWith("-C") && arg.length > 2) { + globalDirs.push(arg.slice(2)); + i += 1; + continue; + } + if (arg.startsWith("--git-dir=")) { + globalDirs.push(arg.slice("--git-dir=".length)); + i += 1; + continue; + } + if (arg.startsWith("--work-tree=")) { + globalDirs.push(arg.slice("--work-tree=".length)); + i += 1; + continue; + } if (GIT_GLOBAL_VALUE_FLAGS.has(arg)) { i += 2; continue; } if ( - arg.startsWith("-C") || arg.startsWith("-c") || - arg.startsWith("--git-dir=") || - arg.startsWith("--work-tree=") || arg.startsWith("--namespace=") || arg.startsWith("--config-env=") ) { @@ -1866,9 +2629,9 @@ function parseGitCommand(args) { i += 1; continue; } - return { subcmd: arg, subArgs: args.slice(i + 1) }; + return { subcmd: arg, subArgs: args.slice(i + 1), globalDirs }; } - return { subcmd: "", subArgs: [] }; + return { subcmd: "", subArgs: [], globalDirs }; } function isReadOnlyGitTagArgs(subArgs) { @@ -1911,6 +2674,62 @@ function isReadOnlyGitStashArgs(subArgs) { return subArgs.length > 0 && (subArgs[0] === "list" || subArgs[0] === "show"); } +function isMutatingGitAction(subcmd, subArgs) { + if (MUTATING_GIT_SUBCOMMANDS.has(subcmd)) return true; + if (subcmd === "tag") return !isReadOnlyGitTagArgs(subArgs); + if (subcmd === "stash") return !isReadOnlyGitStashArgs(subArgs); + if (subcmd === "branch") { + return subArgs.some((a) => ["-d", "-D", "-m", "-M", "-c", "-C", "--delete", "--move", "--copy"].includes(a)); + } + if (subcmd === "worktree") { + const action = subArgs.find((a) => !a.startsWith("-")) || ""; + return Boolean(action && action !== "list"); + } + if (subcmd === "notes") { + const action = subArgs.find((a) => !a.startsWith("-")) || ""; + return Boolean(action && action !== "list" && action !== "show"); + } + if (subcmd === "replace") { + return subArgs.length > 0 && !subArgs.some((a) => a === "-l" || a === "--list"); + } + if (subcmd === "submodule") { + const action = subArgs.find((a) => !a.startsWith("-")) || ""; + return Boolean(action && action !== "status" && action !== "summary"); + } + if (subcmd === "symbolic-ref") { + if (subArgs.some((a) => a === "-d" || a === "--delete")) return true; + const positionals = extractPositionalPaths(subArgs); + return positionals.length >= 2; + } + return false; +} + +function isMutatingGitFetchForReadOnly(subArgs) { + const positionals = []; + let afterDoubleDash = false; + for (let i = 0; i < subArgs.length; i += 1) { + const arg = subArgs[i]; + if (!afterDoubleDash && arg === "--") { + afterDoubleDash = true; + continue; + } + if (!afterDoubleDash && arg.startsWith("-")) { + if ( + ["-p", "--prune", "-f", "--force", "-u", "--update-head-ok"].includes(arg) || + (!arg.startsWith("--") && /^-[a-zA-Z]*[pfu]/.test(arg)) + ) { + return true; + } + if (["--depth", "--deepen", "--shallow-since", "--shallow-exclude", "--negotiation-tip", "-o", "--server-option", "--upload-pack", "--refmap", "-j", "--jobs"].includes(arg)) { + i += 1; + } + continue; + } + positionals.push(arg); + } + return positionals.slice(1).some((refspec) => refspec.includes(":")); +} + function isForcePushToProtectedBranch(subArgs) { let hasForceOrDeleteFlag = false; const positionals = []; @@ -1956,136 +2775,764 @@ function isForcePushToProtectedBranch(subArgs) { return false; } -function isMutatingReadOnlyInvocation(inv, allowReadOnlyTempPath) { - const { cmd, args } = inv; - if (!cmd) return null; - - if (HARD_BLOCKED_RO_FS_COMMANDS.has(cmd)) { - return `Filesystem mutation command '${cmd}' is blocked for read-only ro-* workers.`; - } - - if (cmd === "rm" || cmd === "unlink" || cmd === "touch" || cmd === "mkdir") { - const paths = extractPositionalPaths(args); - const allowRoot = cmd === "mkdir"; - if ( - paths.length === 0 || - paths.some((p) => !allowReadOnlyTempPath(p, { allowRootEqual: allowRoot, allowDevNull: false })) - ) { - return `Filesystem command '${cmd}' outside canonical temp roots is blocked for read-only ro-* workers.`; +function extractPathsSkippingValueFlags(args, valueFlags) { + const paths = []; + let afterDoubleDash = false; + for (let i = 0; i < args.length; i += 1) { + const arg = args[i]; + if (!afterDoubleDash && arg === "--") { + afterDoubleDash = true; + continue; } - return null; - } - - if (cmd === "chmod") { - const paths = extractChmodTargets(args); - if ( - paths.length === 0 || - paths.some((p) => !allowReadOnlyTempPath(p, { allowRootEqual: false, allowDevNull: false })) - ) { - return "Filesystem command 'chmod' outside canonical temp roots is blocked for read-only ro-* workers."; + if (!afterDoubleDash && arg.startsWith("-") && arg.length > 1) { + if (valueFlags.has(arg)) { + i += 1; + } + continue; } - return null; + paths.push(arg); } + return paths; +} - if (cmd === "cp") { - const cpSpec = extractCpOrMvSpec(args); - if ( - !cpSpec.valid || - cpSpec.destinations.some((d) => !allowReadOnlyTempPath(d, { allowRootEqual: true, allowDevNull: true })) - ) { - return "Filesystem command 'cp' targeting paths outside canonical temp roots is blocked for read-only ro-* workers."; +function extractInstallTargets(args) { + let dirMode = false; + let targetDir = null; + const positionals = []; + let afterDoubleDash = false; + for (let i = 0; i < args.length; i += 1) { + const arg = args[i]; + if (!afterDoubleDash && arg === "--") { + afterDoubleDash = true; + continue; } - return null; - } - - if (cmd === "mv") { - const mvSpec = extractCpOrMvSpec(args); - if ( - !mvSpec.valid || - mvSpec.sources.some((s) => !allowReadOnlyTempPath(s, { allowRootEqual: false, allowDevNull: false })) || - mvSpec.destinations.some((d) => !allowReadOnlyTempPath(d, { allowRootEqual: true, allowDevNull: false })) - ) { - return "Filesystem command 'mv' outside canonical temp roots is blocked for read-only ro-* workers."; + if (!afterDoubleDash && arg.startsWith("-") && arg.length > 1) { + if (arg === "-d" || arg === "--directory" || (!arg.startsWith("--") && /^-[a-zA-Z]*d/.test(arg))) { + dirMode = true; + } + if (arg === "-t" || arg === "--target-directory") { + if (i + 1 >= args.length) return { valid: false, targets: [] }; + targetDir = args[i + 1]; + i += 1; + continue; + } + if (arg.startsWith("--target-directory=")) { + targetDir = arg.slice("--target-directory=".length); + continue; + } + if (["-m", "--mode", "-o", "--owner", "-g", "--group", "-S", "--suffix"].includes(arg)) { + i += 1; + } + continue; } - return null; + positionals.push(arg); } - - if (cmd === "tee") { - const files = extractPositionalPaths(args); - if (files.some((f) => !allowReadOnlyTempPath(f, { allowRootEqual: false, allowDevNull: true }))) { - return "Command 'tee' targeting paths outside /dev/null or canonical temp roots is blocked for read-only ro-* workers."; + if (dirMode) { + return positionals.length > 0 ? { valid: true, targets: positionals } : { valid: false, targets: [] }; + } + if (targetDir !== null) { + return positionals.length > 0 ? { valid: true, targets: [targetDir] } : { valid: false, targets: [] }; + } + if (positionals.length < 2) return { valid: false, targets: [] }; + return { valid: true, targets: [positionals[positionals.length - 1]] }; +} + +function extractCurlWriteTargets(args) { + const fileTargets = []; + let usesRemoteName = false; + let outputDir = "."; + for (let i = 0; i < args.length; i += 1) { + const arg = args[i]; + if (arg === "--") break; + if (arg === "-o" || arg === "--output") { + if (i + 1 < args.length) { + if (args[i + 1] !== "-") fileTargets.push(args[i + 1]); + i += 1; + } + continue; + } + if (arg.startsWith("--output=")) { + const v = arg.slice("--output=".length); + if (v !== "-") fileTargets.push(v); + continue; + } + if (arg.startsWith("-o") && arg.length > 2) { + const v = arg.slice(2); + if (v !== "-") fileTargets.push(v); + continue; + } + if (arg === "--output-dir" && i + 1 < args.length) { + outputDir = args[i + 1]; + i += 1; + continue; + } + if (arg.startsWith("--output-dir=")) { + outputDir = arg.slice("--output-dir=".length); + continue; + } + if (arg === "-O" || arg === "--remote-name" || arg === "--remote-name-all" || (!arg.startsWith("--") && /^-[a-zA-Z]*O/.test(arg))) { + usesRemoteName = true; + } + } + return { fileTargets, dirTargets: usesRemoteName ? [outputDir] : [] }; +} + +function extractWgetWriteTargets(args) { + let isSpider = false; + let outputDoc = null; + let dirPrefix = null; + for (let i = 0; i < args.length; i += 1) { + const arg = args[i]; + if (arg === "--") break; + if (arg === "--spider") { + isSpider = true; + continue; + } + if (arg === "-O" || arg === "--output-document") { + if (i + 1 < args.length) { + outputDoc = args[i + 1]; + i += 1; + } + continue; + } + if (arg.startsWith("--output-document=")) { + outputDoc = arg.slice("--output-document=".length); + continue; + } + if (arg.startsWith("-O") && arg.length > 2) { + outputDoc = arg.slice(2); + continue; + } + if (arg === "-P" || arg === "--directory-prefix") { + if (i + 1 < args.length) { + dirPrefix = args[i + 1]; + i += 1; + } + continue; + } + if (arg.startsWith("--directory-prefix=")) { + dirPrefix = arg.slice("--directory-prefix=".length); + continue; + } + if (arg.startsWith("-P") && arg.length > 2) { + dirPrefix = arg.slice(2); + continue; + } + } + if (isSpider) return { fileTargets: [], dirTargets: [] }; + if (outputDoc !== null) { + return outputDoc === "-" ? { fileTargets: [], dirTargets: [] } : { fileTargets: [outputDoc], dirTargets: [] }; + } + return { fileTargets: [], dirTargets: [dirPrefix || "."] }; +} + +function extractTarWriteTargets(args) { + let isExtract = false; + let isCreateOrUpdate = false; + let toStdout = false; + let targetDir = "."; + let archiveFile = null; + for (let i = 0; i < args.length; i += 1) { + const arg = args[i]; + if (arg === "--") break; + if (arg === "-O" || arg === "--to-stdout") toStdout = true; + if (arg === "-x" || arg === "--extract" || arg === "--get") isExtract = true; + if (arg === "-c" || arg === "--create" || arg === "-r" || arg === "--append" || arg === "-u" || arg === "--update") { + isCreateOrUpdate = true; + } + if (!arg.startsWith("--") && (arg.startsWith("-") || i === 0)) { + const flags = arg.startsWith("-") ? arg.slice(1) : arg; + if (flags.includes("x")) isExtract = true; + if (/[cru]/.test(flags)) isCreateOrUpdate = true; + if (flags.includes("O")) toStdout = true; + if (flags.endsWith("f") && i + 1 < args.length) { + archiveFile = args[i + 1]; + i += 1; + continue; + } + if (flags.endsWith("C") && i + 1 < args.length) { + targetDir = args[i + 1]; + i += 1; + continue; + } + } + if (arg === "-C" || arg === "--directory") { + if (i + 1 < args.length) { + targetDir = args[i + 1]; + i += 1; + } + continue; + } + if (arg.startsWith("--directory=")) { + targetDir = arg.slice("--directory=".length); + continue; + } + if (arg === "-f" || arg === "--file") { + if (i + 1 < args.length) { + archiveFile = args[i + 1]; + i += 1; + } + continue; + } + if (arg.startsWith("--file=")) { + archiveFile = arg.slice("--file=".length); + } + } + const fileTargets = []; + const dirTargets = []; + if (isExtract && !toStdout) dirTargets.push(targetDir); + if (isCreateOrUpdate && archiveFile && archiveFile !== "-" && archiveFile !== "/dev/null") { + fileTargets.push(archiveFile); + } + return { fileTargets, dirTargets }; +} + +function extractUnzipWriteTargets(args) { + let readOnly = false; + let targetDir = "."; + for (let i = 0; i < args.length; i += 1) { + const arg = args[i]; + if (arg === "--") break; + if (["-l", "-v", "-t", "-p", "-c", "-Z"].includes(arg) || (!arg.startsWith("--") && /^-[a-zA-Z]*[lvtpcZ]/.test(arg))) { + readOnly = true; + } + if (arg === "-d" && i + 1 < args.length) { + targetDir = args[i + 1]; + i += 1; + continue; + } + if (arg.startsWith("-d") && arg.length > 2) { + targetDir = arg.slice(2); + } + } + return readOnly ? [] : [targetDir]; +} + +function extractRsyncWriteSpec(args) { + let dryRun = false; + const positionals = []; + let afterDoubleDash = false; + for (let i = 0; i < args.length; i += 1) { + const arg = args[i]; + if (!afterDoubleDash && arg === "--") { + afterDoubleDash = true; + continue; + } + if (!afterDoubleDash && arg.startsWith("-") && arg.length > 1) { + if (arg === "-n" || arg === "--dry-run" || arg === "--list-only" || (!arg.startsWith("--") && /^-[a-zA-Z]*n/.test(arg))) { + dryRun = true; + } + if (["-e", "--rsh", "--exclude", "--include", "--filter", "--files-from", "--log-file", "--out-format", "--compare-dest", "--copy-dest", "--link-dest"].includes(arg)) { + i += 1; + } + continue; + } + positionals.push(arg); + } + if (dryRun || positionals.length < 2) return { dryRun: true, destination: null }; + return { dryRun: false, destination: positionals[positionals.length - 1] }; +} + +function extractFindEffects(args) { + const roots = []; + let i = 0; + while (i < args.length) { + const arg = args[i]; + if (arg === "-H" || arg === "-L" || arg === "-P" || arg === "-O" || arg === "-D") { + if (arg === "-D") i += 1; + i += 1; + continue; + } + if (arg.startsWith("-") || arg === "!" || arg === "(" || arg === ",") break; + roots.push(arg); + i += 1; + } + const searchRoots = roots.length > 0 ? roots : ["."]; + let hasDelete = false; + const fprintTargets = []; + const execCommands = []; + + for (; i < args.length; i += 1) { + const arg = args[i]; + if (arg === "-delete") { + hasDelete = true; + continue; + } + if (arg === "-fprint" || arg === "-fprint0" || arg === "-fls" || arg === "-fprintf") { + if (i + 1 < args.length) { + fprintTargets.push(args[i + 1]); + i += arg === "-fprintf" ? 2 : 1; + } + continue; + } + if (arg === "-exec" || arg === "-execdir" || arg === "-ok" || arg === "-okdir") { + const innerTokens = []; + let j = i + 1; + while (j < args.length && args[j] !== ";" && args[j] !== "+") { + innerTokens.push(args[j]); + j += 1; + } + if (innerTokens.length > 0) { + execCommands.push(innerTokens); + } + i = j; + } + } + return { searchRoots, hasDelete, fprintTargets, execCommands }; +} + +function checkSharedTargetConfinement(inv, allowPath, roleLabel, validateInnerTokens) { + const { cmd, args } = inv; + if (cmd === "mkdir") { + const paths = extractPathsSkippingValueFlags(args, new Set(["-m", "--mode"])); + if (paths.length === 0 || paths.some((p) => !allowPath(p, { allowRootEqual: true, allowDevNull: false }))) { + return `Command 'mkdir' outside allowed paths is blocked for ${roleLabel}.`; + } + return null; + } + if (cmd === "touch") { + const paths = extractPathsSkippingValueFlags(args, new Set(["-d", "--date", "-r", "--reference", "-t"])); + if (paths.length === 0 || paths.some((p) => !allowPath(p, { allowRootEqual: false, allowDevNull: false }))) { + return `Command 'touch' outside allowed paths is blocked for ${roleLabel}.`; + } + return null; + } + if (cmd === "chmod") { + const paths = extractChmodTargets(args); + if (paths.length === 0 || paths.some((p) => !allowPath(p, { allowRootEqual: false, allowDevNull: false }))) { + return `Command 'chmod' outside allowed paths is blocked for ${roleLabel}.`; + } + return null; + } + if (cmd === "truncate") { + const paths = extractPathsSkippingValueFlags(args, new Set(["-s", "--size", "-r", "--reference"])); + if (paths.length === 0 || paths.some((p) => !allowPath(p, { allowRootEqual: false, allowDevNull: false }))) { + return `Command 'truncate' outside allowed paths is blocked for ${roleLabel}.`; + } + return null; + } + if (cmd === "dd") { + const ofArg = args.find((a) => a.startsWith("of=")); + if (ofArg) { + const target = ofArg.slice(3); + if (!allowPath(target, { allowRootEqual: false, allowDevNull: true })) { + return `Command 'dd' output target outside allowed paths is blocked for ${roleLabel}.`; + } + } + return null; + } + if (cmd === "install") { + const spec = extractInstallTargets(args); + if (!spec.valid || spec.targets.some((t) => !allowPath(t, { allowRootEqual: true, allowDevNull: false }))) { + return `Command 'install' targeting outside allowed paths is blocked for ${roleLabel}.`; } return null; } + if (cmd === "curl") { + const { fileTargets, dirTargets } = extractCurlWriteTargets(args); + if ( + fileTargets.some((f) => !allowPath(f, { allowRootEqual: false, allowDevNull: true })) || + dirTargets.some((d) => !allowPath(d, { allowRootEqual: true, allowDevNull: false })) + ) { + return `Command 'curl' writing outside allowed paths is blocked for ${roleLabel}.`; + } + return null; + } + if (cmd === "wget") { + const { fileTargets, dirTargets } = extractWgetWriteTargets(args); + if ( + fileTargets.some((f) => !allowPath(f, { allowRootEqual: false, allowDevNull: true })) || + dirTargets.some((d) => !allowPath(d, { allowRootEqual: true, allowDevNull: false })) + ) { + return `Command 'wget' writing outside allowed paths is blocked for ${roleLabel}.`; + } + return null; + } + if (cmd === "tar") { + const { fileTargets, dirTargets } = extractTarWriteTargets(args); + if ( + fileTargets.some((f) => !allowPath(f, { allowRootEqual: false, allowDevNull: true })) || + dirTargets.some((d) => !allowPath(d, { allowRootEqual: true, allowDevNull: false })) + ) { + return `Command 'tar' writing outside allowed paths is blocked for ${roleLabel}.`; + } + return null; + } + if (cmd === "unzip") { + const dirTargets = extractUnzipWriteTargets(args); + if (dirTargets.some((d) => !allowPath(d, { allowRootEqual: true, allowDevNull: false }))) { + return `Command 'unzip' extracting outside allowed paths is blocked for ${roleLabel}.`; + } + return null; + } + if (cmd === "rsync") { + const spec = extractRsyncWriteSpec(args); + if (!spec.dryRun && spec.destination) { + if (/^(?:[A-Za-z0-9._-]+@)?[A-Za-z0-9._-]+:/.test(spec.destination) || spec.destination.startsWith("rsync://")) { + return `Command 'rsync' to remote destination is blocked for ${roleLabel}.`; + } + if (!allowPath(spec.destination, { allowRootEqual: true, allowDevNull: false })) { + return `Command 'rsync' targeting outside allowed paths is blocked for ${roleLabel}.`; + } + } + return null; + } + if (cmd === "find") { + const { searchRoots, hasDelete, fprintTargets, execCommands } = extractFindEffects(args); + if (hasDelete && searchRoots.some((r) => !allowPath(r, { allowRootEqual: true, allowDevNull: false }))) { + return `Command 'find -delete' outside allowed paths is blocked for ${roleLabel}.`; + } + if (fprintTargets.some((f) => !allowPath(f, { allowRootEqual: false, allowDevNull: true }))) { + return `Command 'find -fprint/-fls' outside allowed paths is blocked for ${roleLabel}.`; + } + for (const innerTokens of execCommands) { + const innerDeny = validateInnerTokens(innerTokens, searchRoots); + if (innerDeny) return innerDeny; + } + return null; + } + return undefined; +} - if (cmd === "sed" && extractSedInPlaceTargets(args) !== null) { - return "In-place 'sed -i' is blocked for read-only ro-* workers."; +function extractNodePreloadTargets(args, mergedEnv) { + const targets = []; + const scanArgs = (list) => { + for (let i = 0; i < list.length; i += 1) { + const arg = list[i]; + if (arg === "--") break; + if ( + (arg === "-r" || arg === "--require" || arg === "--import" || arg === "--loader" || arg === "--experimental-loader") && + i + 1 < list.length + ) { + targets.push(list[i + 1]); + i += 1; + continue; + } + for (const prefix of ["--require=", "--import=", "--loader=", "--experimental-loader="]) { + if (arg.startsWith(prefix)) { + targets.push(arg.slice(prefix.length)); + break; + } + } + } + }; + scanArgs(args); + if (typeof mergedEnv.NODE_OPTIONS === "string" && mergedEnv.NODE_OPTIONS.trim().length > 0) { + scanArgs(tokenizeSingleSegment(mergedEnv.NODE_OPTIONS)); } + return targets; +} - if ((cmd === "perl" || cmd === "ruby") && extractPerlRubyInPlaceTargets(args) !== null) { - return `In-place '${cmd} -i' is blocked for read-only ro-* workers.`; +function findInterpreterScriptArg(cmd, args) { + const isNode = cmd === "node" || cmd === "nodejs" || cmd === "bun"; + const isPy = /^python(?:\d+(?:\.\d+)?)?$/.test(cmd); + const isShell = ["sh", "bash", "zsh", "dash", "ksh"].includes(cmd); + const isPerlRuby = cmd === "perl" || cmd === "ruby"; + if (!isNode && !isPy && !isShell && !isPerlRuby) return null; + + const valueFlags = isNode + ? new Set(["-e", "--eval", "-p", "--print", "-r", "--require", "--import", "--loader", "--experimental-loader", "--conditions", "-C"]) + : isPy + ? new Set(["-c", "-m", "-W", "-X", "-Q"]) + : isShell + ? new Set(["-c", "-o"]) + : new Set(["-e", "-E", "-I", "-M", "-m", "-C"]); + + let hasTestFlag = false; + for (let i = 0; i < args.length; i += 1) { + const arg = args[i]; + if (arg === "--") { + return i + 1 < args.length ? { scriptToken: args[i + 1], hasTestFlag } : null; + } + if (isNode && arg === "--test") { + hasTestFlag = true; + continue; + } + if (isNode && (arg === "-e" || arg === "--eval" || arg === "-p" || arg === "--print" || arg.startsWith("--eval=") || arg.startsWith("--print="))) { + return null; + } + if (isPy && (arg === "-c" || /^-[A-Za-z]+c$/.test(arg) || arg === "-m")) { + return null; + } + if (isShell && /^-[a-zA-Z]*c$/.test(arg)) { + return null; + } + if (isPerlRuby && (arg === "-e" || arg === "-E" || /^-[a-zA-Z.]*[eE]$/.test(arg))) { + return null; + } + if (arg.startsWith("-") && arg.length > 1) { + if (valueFlags.has(arg)) i += 1; + continue; + } + return { scriptToken: arg, hasTestFlag }; + } + return null; +} + +function inspectSingleTempFile(scriptToken, modeHint, context) { + const { + allowPath, + nodePyOptions, + canonicalTempRoots, + realCwd, + fallbackBaseDir, + mergedEnv, + outerEnv, + roleLabel, + kind, + validateShellCode, + } = context; + + const resolved = resolveShellTargetPath(scriptToken, fallbackBaseDir, mergedEnv); + if (!resolved) return null; + + const inTemp = isInsideCanonicalTempRoots(resolved, canonicalTempRoots, realCwd, { + allowRootEqual: false, + excludeRealCwd: false, + }); + if (!inTemp) return null; + + if (kind === "sicko") { + return `Executing temporary script '${scriptToken}' is blocked for comment-sicko workers.`; + } + + if (!existsSync(resolved)) { + return `Temporary script '${scriptToken}' must exist on disk before execution for ${roleLabel}.`; + } + + let rawBuf; + try { + if (!statSync(resolved).isFile()) { + return `Temporary script '${scriptToken}' is not a readable file for ${roleLabel}.`; + } + rawBuf = readFileSync(resolved); + } catch { + return `Temporary script '${scriptToken}' could not be read for ${roleLabel}.`; } + if (rawBuf.includes(0)) { + if (kind === "readonly" || kind === "sicko") { + return `Executing binary file '${scriptToken}' in canonical temp roots is blocked for ${roleLabel}.`; + } + return null; + } + + const code = rawBuf.toString("utf8"); + const ext = extname(resolved).toLowerCase(); + const firstLine = code.split(/\r?\n/, 1)[0] || ""; + const isNode = + modeHint === "node" || + [".js", ".mjs", ".cjs"].includes(ext) || + /#!\s*\/.*\bnode\b/.test(firstLine); + const isPy = + modeHint === "python" || + ext === ".py" || + /#!\s*\/.*\bpython/.test(firstLine); + const isPerlRuby = + modeHint === "perl" || + modeHint === "ruby" || + ext === ".pl" || + ext === ".rb" || + /#!\s*\/.*\b(?:perl|ruby)\b/.test(firstLine); + const isShell = + modeHint === "shell" || + [".sh", ".bash", ".zsh"].includes(ext) || + /#!\s*\/.*\b(?:sh|bash|zsh|dash|ksh)\b/.test(firstLine); + + if (isPerlRuby) { + if (kind === "readonly" || kind === "sicko") { + return `Executing temporary ${modeHint || "perl/ruby"} script '${scriptToken}' is blocked for ${roleLabel}.`; + } + return null; + } + if (isNode) { + return checkInlineNodeFs(code, allowPath, nodePyOptions); + } + if (isPy) { + return checkInlinePythonFs(code, allowPath, nodePyOptions); + } + if (isShell) { + return validateShellCode(code, mergedEnv, outerEnv); + } + return validateShellCode(code, mergedEnv, outerEnv); +} + +function inspectTempScriptFile(inv, context) { + const { canonicalTempRoots, realCwd, fallbackBaseDir, mergedEnv, roleLabel, kind } = context; + + if (inv.cmd === "node" || inv.cmd === "nodejs" || inv.cmd === "bun") { + for (const preloadTarget of extractNodePreloadTargets(inv.args, mergedEnv)) { + const preloadDeny = inspectSingleTempFile(preloadTarget, "node", context); + if (preloadDeny) return preloadDeny; + } + } + + if (/^python(?:\d+(?:\.\d+)?)?$/.test(inv.cmd)) { + const mIdx = inv.args.indexOf("-m"); + if (mIdx >= 0 && mIdx + 1 < inv.args.length && typeof mergedEnv.PYTHONPATH === "string") { + const modName = inv.args[mIdx + 1]; + const modRel = modName.replace(/\./g, "/"); + for (const pyRoot of mergedEnv.PYTHONPATH.split(/[:;]/).filter(Boolean)) { + const resolvedRoot = resolveShellTargetPath(pyRoot, fallbackBaseDir, mergedEnv); + if ( + resolvedRoot && + isInsideCanonicalTempRoots(resolvedRoot, canonicalTempRoots, realCwd, { + allowRootEqual: true, + excludeRealCwd: false, + }) + ) { + if (kind === "sicko") { + return `Executing Python module '${modName}' from PYTHONPATH in canonical temp roots is blocked for ${roleLabel}.`; + } + const modFile = join(resolvedRoot, `${modRel}.py`); + const modInit = join(resolvedRoot, modRel, "__init__.py"); + const candidate = existsSync(modFile) ? modFile : existsSync(modInit) ? modInit : null; + if (candidate) { + const modDeny = inspectSingleTempFile(candidate, "python", context); + if (modDeny) return modDeny; + } else if (kind === "readonly") { + return `Unresolvable Python module '${modName}' with PYTHONPATH in canonical temp roots is blocked for ${roleLabel}.`; + } + } + } + } + } + + if (inv.cmd === "source" || inv.cmd === ".") { + const sourced = inv.args[0]; + if (sourced) { + return inspectSingleTempFile(sourced, "shell", context); + } + return null; + } + + let scriptToken = null; + let modeHint = ""; + const interp = findInterpreterScriptArg(inv.cmd, inv.args); + if (interp) { + scriptToken = interp.scriptToken; + if (inv.cmd === "node" || inv.cmd === "nodejs" || inv.cmd === "bun") modeHint = "node"; + else if (/^python/.test(inv.cmd)) modeHint = "python"; + else if (inv.cmd === "perl" || inv.cmd === "ruby") modeHint = inv.cmd; + else modeHint = "shell"; + } else if (inv.rawCmd && (inv.rawCmd.includes("/") || inv.rawCmd.startsWith(".") || inv.rawCmd.startsWith("~"))) { + scriptToken = inv.rawCmd; + } + + if (!scriptToken) return null; + return inspectSingleTempFile(scriptToken, modeHint, context); +} + +function extractInlineCodeCheck(inv, allowPath, nodePyOptions) { + const { cmd, args } = inv; if (cmd === "node" || cmd === "nodejs" || cmd === "bun") { for (let i = 0; i < args.length; i += 1) { const arg = args[i]; if (arg === "--") break; if ((arg === "-e" || arg === "--eval" || arg === "-p" || arg === "--print") && i + 1 < args.length) { - const deny = checkInlineNodeFs(args[i + 1], allowReadOnlyTempPath); + const deny = checkInlineNodeFs(args[i + 1], allowPath, nodePyOptions); if (deny) return deny; i += 1; continue; } if (arg.startsWith("--eval=")) { - const deny = checkInlineNodeFs(arg.slice("--eval=".length), allowReadOnlyTempPath); + const deny = checkInlineNodeFs(arg.slice("--eval=".length), allowPath, nodePyOptions); if (deny) return deny; } else if (arg.startsWith("--print=")) { - const deny = checkInlineNodeFs(arg.slice("--print=".length), allowReadOnlyTempPath); + const deny = checkInlineNodeFs(arg.slice("--print=".length), allowPath, nodePyOptions); if (deny) return deny; } } } - if (/^python(?:\d+(?:\.\d+)?)?$/.test(cmd)) { for (let i = 0; i < args.length; i += 1) { const arg = args[i]; if (arg === "--" || arg === "-m") break; if ((arg === "-c" || /^-[A-Za-z]+c$/.test(arg)) && i + 1 < args.length) { - const deny = checkInlinePythonFs(args[i + 1], allowReadOnlyTempPath); + const deny = checkInlinePythonFs(args[i + 1], allowPath, nodePyOptions); if (deny) return deny; break; } if (!arg.startsWith("-")) break; } } + return null; +} + +function isMutatingReadOnlyInvocation(inv, allowReadOnlyTempPath, validateInnerTokens) { + const { cmd, args } = inv; + if (!cmd) return null; + + if (HARD_BLOCKED_RO_FS_COMMANDS.has(cmd)) { + return `Filesystem mutation command '${cmd}' is blocked for read-only ro-* workers.`; + } - if (cmd === "git") { - const { subcmd, subArgs } = parseGitCommand(args); - if (subcmd === "tag") { - if (!isReadOnlyGitTagArgs(subArgs)) { - return "Mutating git command 'git tag' is blocked for read-only ro-* workers."; - } - return null; + const sharedDeny = checkSharedTargetConfinement(inv, allowReadOnlyTempPath, "read-only ro-* workers", validateInnerTokens); + if (sharedDeny !== undefined) return sharedDeny; + + if (cmd === "rm" || cmd === "unlink") { + const paths = extractPositionalPaths(args); + if ( + paths.length === 0 || + paths.some((p) => !allowReadOnlyTempPath(p, { allowRootEqual: false, allowDevNull: false })) + ) { + return `Filesystem command '${cmd}' outside canonical temp roots is blocked for read-only ro-* workers.`; } - if (subcmd === "stash") { - if (!isReadOnlyGitStashArgs(subArgs)) { - return "Mutating git command 'git stash' is blocked for read-only ro-* workers."; - } - return null; + return null; + } + + if (cmd === "cp") { + const cpSpec = extractCpOrMvSpec(args); + if ( + !cpSpec.valid || + cpSpec.destinations.some((d) => !allowReadOnlyTempPath(d, { allowRootEqual: true, allowDevNull: true })) + ) { + return "Filesystem command 'cp' targeting paths outside canonical temp roots is blocked for read-only ro-* workers."; } - if (MUTATING_GIT_SUBCOMMANDS.has(subcmd)) { - return `Mutating git command 'git ${subcmd}' is blocked for read-only ro-* workers.`; + return null; + } + + if (cmd === "mv") { + const mvSpec = extractCpOrMvSpec(args); + if ( + !mvSpec.valid || + mvSpec.sources.some((s) => !allowReadOnlyTempPath(s, { allowRootEqual: false, allowDevNull: false })) || + mvSpec.destinations.some((d) => !allowReadOnlyTempPath(d, { allowRootEqual: true, allowDevNull: false })) + ) { + return "Filesystem command 'mv' outside canonical temp roots is blocked for read-only ro-* workers."; } - if (subcmd === "branch") { - if (subArgs.some((a) => ["-d", "-D", "-m", "-M", "-c", "-C", "--delete", "--move", "--copy"].includes(a))) { - return "Mutating 'git branch' flag is blocked for read-only ro-* workers."; - } + return null; + } + + if (cmd === "tee") { + const files = extractPositionalPaths(args); + if (files.some((f) => !allowReadOnlyTempPath(f, { allowRootEqual: false, allowDevNull: true }))) { + return "Command 'tee' targeting paths outside /dev/null or canonical temp roots is blocked for read-only ro-* workers."; } - if (subcmd === "worktree") { - const action = subArgs.find((a) => !a.startsWith("-")) || ""; - if (action && action !== "list") { - return `Mutating 'git worktree ${action}' is blocked for read-only ro-* workers.`; - } + return null; + } + + if (cmd === "sed" && extractSedInPlaceTargets(args) !== null) { + return "In-place 'sed -i' is blocked for read-only ro-* workers."; + } + + if ((cmd === "perl" || cmd === "ruby") && extractPerlRubyInPlaceTargets(args) !== null) { + return `In-place '${cmd} -i' is blocked for read-only ro-* workers.`; + } + + const inlineDeny = extractInlineCodeCheck(inv, allowReadOnlyTempPath, { + allowDynamicPaths: false, + roleLabel: "read-only ro-* workers", + }); + if (inlineDeny) return inlineDeny; + + if (cmd === "git") { + const { subcmd, subArgs } = parseGitCommand(args); + if (subcmd === "fetch" && isMutatingGitFetchForReadOnly(subArgs)) { + return "Mutating 'git fetch' with colon refspec or prune/force/update flags is blocked for read-only ro-* workers."; + } + if (isMutatingGitAction(subcmd, subArgs)) { + return `Mutating git command 'git ${subcmd}' is blocked for read-only ro-* workers.`; } } @@ -2141,10 +3588,18 @@ function isMutatingReadOnlyInvocation(inv, allowReadOnlyTempPath) { return null; } -function checkAuthorShellConfinement(inv, allowAuthorShellPath, resolveForBase) { +function checkAuthorShellConfinement(inv, allowAuthorShellPath, resolveForBase, validateInnerTokens) { const { cmd, args } = inv; if (!cmd) return null; + const sharedDeny = checkSharedTargetConfinement( + inv, + allowAuthorShellPath, + "confined worker worktree, artifact directory, or canonical temp roots", + validateInnerTokens, + ); + if (sharedDeny !== undefined) return sharedDeny; + if (cmd === "cp") { const cpSpec = extractCpOrMvSpec(args); if ( @@ -2237,10 +3692,23 @@ function checkAuthorShellConfinement(inv, allowAuthorShellPath, resolveForBase) } } + const inlineDeny = extractInlineCodeCheck(inv, allowAuthorShellPath, { + allowDynamicPaths: true, + roleLabel: "author/adversary workers", + }); + if (inlineDeny) return inlineDeny; + + if (cmd === "git") { + const { subcmd, subArgs, globalDirs } = parseGitCommand(args); + if (isMutatingGitAction(subcmd, subArgs) && globalDirs.some((d) => !allowAuthorShellPath(d, { allowRootEqual: true, allowDevNull: false }))) { + return `Mutating git command 'git ${subcmd}' targeting a repository outside confined worktree or canonical temp roots is blocked.`; + } + } + return null; } -function checkSickoShellInvocation(inv) { +function checkSickoShellInvocation(inv, allowSickoWritePath, resolveForBase, realCwd, validateInnerTokens) { const { cmd, args } = inv; if (!cmd) return null; @@ -2248,6 +3716,11 @@ function checkSickoShellInvocation(inv) { return `Filesystem mutation command '${cmd}' is blocked for comment-sicko workers; use edit_file on existing files only.`; } + if (cmd === "curl" || cmd === "wget" || cmd === "tar" || cmd === "unzip" || cmd === "rsync" || cmd === "find") { + const sharedDeny = checkSharedTargetConfinement(inv, allowSickoWritePath, "comment-sicko workers", validateInnerTokens); + if (sharedDeny) return sharedDeny; + } + if (cmd === "tee") { const files = extractPositionalPaths(args); if (files.some((f) => f !== "/dev/null")) { @@ -2263,6 +3736,25 @@ function checkSickoShellInvocation(inv) { return `In-place '${cmd} -i' is blocked for comment-sicko workers; use edit_file only.`; } + const inlineDeny = extractInlineCodeCheck(inv, allowSickoWritePath, { + allowDynamicPaths: false, + roleLabel: "comment-sicko workers", + }); + if (inlineDeny) return inlineDeny; + + if (cmd === "git") { + const { subcmd, subArgs, globalDirs } = parseGitCommand(args); + for (const dir of globalDirs) { + const resolved = resolveForBase(dir); + if (!resolved || !realCwd || !isInsideOrEqual(resolved, realCwd)) { + return "Git command targeting a directory outside the confined worktree is blocked for comment-sicko workers."; + } + } + if (subcmd !== "add" && subcmd !== "commit" && isMutatingGitAction(subcmd, subArgs)) { + return `Mutating or file-restoring git command 'git ${subcmd}' is blocked for comment-sicko workers.`; + } + } + return null; } @@ -2393,32 +3885,12 @@ function handlePreToolUse(payload, env) { if (toolName === "run_command") { const commandLine = String(args.CommandLine ?? args.command ?? args.commandLine ?? ""); - const { invocations, redirectTargets } = collectInvocations(commandLine); - - const allowReadOnlyTempPath = (rawToken, opts = {}) => { - const { allowRootEqual = false, allowDevNull = false } = opts; - const resolved = resolveShellTargetPath(rawToken, fallbackBaseDir, env); - if (!resolved) return false; - if (allowDevNull && resolved === "/dev/null") return true; - return isInsideCanonicalTempRoots(resolved, canonicalTempRoots, realCwd, { - allowRootEqual, - excludeRealCwd: true, - }); - }; - for (const redirectTarget of redirectTargets) { - if (!allowReadOnlyTempPath(redirectTarget, { allowRootEqual: false, allowDevNull: true })) { - return { - decision: "deny", - reason: - "Output file redirection outside /dev/null or canonical temp roots is blocked for read-only ro-* workers.", - }; - } - } + const validateReadOnlyShell = (code, inheritedEnv = {}, outerEnv = env, depth = 0) => { + if (depth > 4) return "Nested shell script depth limit exceeded for read-only ro-* workers."; + const { invocations, redirectTargets } = collectInvocations(code, 0, inheritedEnv, outerEnv); - for (const inv of invocations) { - const mergedEnv = { ...env, ...inv.env }; - const allowInvTempPath = (rawToken, opts = {}) => { + const makeAllowInvTempPath = (mergedEnv) => (rawToken, opts = {}) => { const { allowRootEqual = false, allowDevNull = false } = opts; const resolved = resolveShellTargetPath(rawToken, fallbackBaseDir, mergedEnv); if (!resolved) return false; @@ -2428,10 +3900,52 @@ function handlePreToolUse(payload, env) { excludeRealCwd: true, }); }; - const denyReason = isMutatingReadOnlyInvocation(inv, allowInvTempPath); - if (denyReason) { - return { decision: "deny", reason: denyReason }; + + for (const redirectTarget of redirectTargets) { + const mergedEnv = { ...outerEnv, ...redirectTarget.env }; + if (!makeAllowInvTempPath(mergedEnv)(redirectTarget.target, { allowRootEqual: false, allowDevNull: true })) { + return "Output file redirection outside /dev/null or canonical temp roots is blocked for read-only ro-* workers."; + } } + + for (const inv of invocations) { + const mergedEnv = { ...outerEnv, ...inv.env }; + const allowInvTempPath = makeAllowInvTempPath(mergedEnv); + const validateInnerTokens = (innerTokens, searchRoots) => { + const innerInv = unwrapCommandSegment(innerTokens, inv.env, outerEnv); + if (!innerInv.cmd) return null; + const innerAllow = (rawToken, opts = {}) => { + if (rawToken === "{}" || rawToken.includes("{}")) { + return searchRoots.every((r) => allowInvTempPath(r, { allowRootEqual: true, allowDevNull: false })); + } + return allowInvTempPath(rawToken, opts); + }; + return isMutatingReadOnlyInvocation(innerInv, innerAllow, validateInnerTokens); + }; + const denyReason = isMutatingReadOnlyInvocation(inv, allowInvTempPath, validateInnerTokens); + if (denyReason) return denyReason; + + const scriptDeny = inspectTempScriptFile(inv, { + allowPath: allowInvTempPath, + nodePyOptions: { allowDynamicPaths: false, roleLabel: "read-only ro-* workers" }, + canonicalTempRoots, + realCwd, + fallbackBaseDir, + mergedEnv, + outerEnv, + roleLabel: "read-only ro-* workers", + kind, + validateShellCode: (scriptCode, scriptEnv, baseOuterEnv) => + validateReadOnlyShell(scriptCode, scriptEnv, baseOuterEnv, depth + 1), + }); + if (scriptDeny) return scriptDeny; + } + return null; + }; + + const roDeny = validateReadOnlyShell(commandLine, {}, env, 0); + if (roDeny) { + return { decision: "deny", reason: roDeny }; } } @@ -2493,11 +4007,11 @@ function handlePreToolUse(payload, env) { if (toolName === "run_command") { const commandLine = String(args.CommandLine ?? args.command ?? args.commandLine ?? ""); - const { invocations, redirectTargets } = collectInvocations(commandLine); + const { invocations, redirectTargets } = collectInvocations(commandLine, 0, {}, env); if (kind === "sicko") { for (const redirectTarget of redirectTargets) { - if (redirectTarget !== "/dev/null") { + if (redirectTarget.target !== "/dev/null") { return { decision: "deny", reason: @@ -2505,11 +4019,38 @@ function handlePreToolUse(payload, env) { }; } } + const allowSickoWritePath = (_rawToken, opts = {}) => { + if (opts.allowDevNull && _rawToken === "/dev/null") return true; + return false; + }; for (const inv of invocations) { - const sickoDeny = checkSickoShellInvocation(inv); + const mergedEnv = { ...env, ...inv.env }; + const resolveForBase = (rawToken) => + resolveShellTargetPath(rawToken, realCwd || fallbackBaseDir, mergedEnv); + const validateInnerTokens = (innerTokens) => { + const innerInv = unwrapCommandSegment(innerTokens, inv.env, env); + if (!innerInv.cmd) return null; + return checkSickoShellInvocation(innerInv, allowSickoWritePath, resolveForBase, realCwd, validateInnerTokens); + }; + const sickoDeny = checkSickoShellInvocation(inv, allowSickoWritePath, resolveForBase, realCwd, validateInnerTokens); if (sickoDeny) { return { decision: "deny", reason: sickoDeny }; } + const scriptDeny = inspectTempScriptFile(inv, { + allowPath: allowSickoWritePath, + nodePyOptions: { allowDynamicPaths: false, roleLabel: "comment-sicko workers" }, + canonicalTempRoots, + realCwd, + fallbackBaseDir, + mergedEnv, + outerEnv: env, + roleLabel: "comment-sicko workers", + kind, + validateShellCode: () => "Executing temporary shell script is blocked for comment-sicko workers.", + }); + if (scriptDeny) { + return { decision: "deny", reason: scriptDeny }; + } } } @@ -2517,7 +4058,7 @@ function handlePreToolUse(payload, env) { const checkConfinedShellPath = (rawToken, mergedEnv, opts = {}) => { const { allowRootEqual = false, allowDevNull = false, customBaseDir = null } = opts; const activeBase = customBaseDir || realCwd; - if (!activeBase && !isAbsolute(rawToken) && !rawToken.startsWith("~") && !rawToken.startsWith("$TMPDIR") && !rawToken.startsWith("${TMPDIR}")) { + if (!activeBase && !isAbsolute(rawToken) && !rawToken.startsWith("~") && !rawToken.startsWith("$")) { return false; } const resolved = resolveShellTargetPath(rawToken, activeBase || fallbackBaseDir, mergedEnv); @@ -2538,25 +4079,57 @@ function handlePreToolUse(payload, env) { }); }; - for (const redirectTarget of redirectTargets) { - if (!checkConfinedShellPath(redirectTarget, env, { allowRootEqual: false, allowDevNull: true })) { - return { - decision: "deny", - reason: - "Shell output redirection outside confined worker worktree, artifact directory, /dev/null, or canonical temp roots is blocked.", - }; + const validateAuthorShell = (code, inheritedEnv = {}, outerEnv = env, depth = 0) => { + if (depth > 4) return "Nested shell script depth limit exceeded."; + const shellData = depth === 0 ? { invocations, redirectTargets } : collectInvocations(code, 0, inheritedEnv, outerEnv); + + for (const redirectTarget of shellData.redirectTargets) { + const mergedEnv = { ...outerEnv, ...redirectTarget.env }; + if (!checkConfinedShellPath(redirectTarget.target, mergedEnv, { allowRootEqual: false, allowDevNull: true })) { + return "Shell output redirection outside confined worker worktree, artifact directory, /dev/null, or canonical temp roots is blocked."; + } } - } - for (const inv of invocations) { - const mergedEnv = { ...env, ...inv.env }; - const allowInvPath = (rawToken, opts = {}) => checkConfinedShellPath(rawToken, mergedEnv, opts); - const resolveForBase = (rawToken) => - resolveShellTargetPath(rawToken, realCwd || fallbackBaseDir, mergedEnv); - const authorDeny = checkAuthorShellConfinement(inv, allowInvPath, resolveForBase); - if (authorDeny) { - return { decision: "deny", reason: authorDeny }; + for (const inv of shellData.invocations) { + const mergedEnv = { ...outerEnv, ...inv.env }; + const allowInvPath = (rawToken, opts = {}) => checkConfinedShellPath(rawToken, mergedEnv, opts); + const resolveForBase = (rawToken) => + resolveShellTargetPath(rawToken, realCwd || fallbackBaseDir, mergedEnv); + const validateInnerTokens = (innerTokens, searchRoots) => { + const innerInv = unwrapCommandSegment(innerTokens, inv.env, outerEnv); + if (!innerInv.cmd) return null; + const innerAllow = (rawToken, opts = {}) => { + if (rawToken === "{}" || rawToken.includes("{}")) { + return searchRoots.every((r) => allowInvPath(r, { allowRootEqual: true, allowDevNull: false })); + } + return allowInvPath(rawToken, opts); + }; + return checkAuthorShellConfinement(innerInv, innerAllow, resolveForBase, validateInnerTokens); + }; + const authorDeny = checkAuthorShellConfinement(inv, allowInvPath, resolveForBase, validateInnerTokens); + if (authorDeny) return authorDeny; + + const scriptDeny = inspectTempScriptFile(inv, { + allowPath: allowInvPath, + nodePyOptions: { allowDynamicPaths: true, roleLabel: "author/adversary workers" }, + canonicalTempRoots, + realCwd, + fallbackBaseDir, + mergedEnv, + outerEnv, + roleLabel: "author/adversary workers", + kind, + validateShellCode: (scriptCode, scriptEnv, baseOuterEnv) => + validateAuthorShell(scriptCode, scriptEnv, baseOuterEnv, depth + 1), + }); + if (scriptDeny) return scriptDeny; } + return null; + }; + + const authorDeny = validateAuthorShell(commandLine, {}, env, 0); + if (authorDeny) { + return { decision: "deny", reason: authorDeny }; } } @@ -2605,6 +4178,31 @@ function main() { rawInput = ""; } + if ( + typeof process.env.PSTACK_PLUGIN_ROOT === "string" && + process.env.PSTACK_PLUGIN_ROOT.length > 0 && + process.env.PSTACK_GUARD_DELEGATED !== "1" + ) { + const targetGuard = join(process.env.PSTACK_PLUGIN_ROOT, "hooks", "guard.mjs"); + if (existsSync(targetGuard)) { + let selfReal = null; + let targetReal = null; + try { + selfReal = realpathSync(fileURLToPath(import.meta.url)); + targetReal = realpathSync(targetGuard); + } catch {} + if (selfReal && targetReal && selfReal !== targetReal) { + const delegatedOut = execFileSync(process.execPath, [targetReal], { + input: rawInput, + env: { ...process.env, PSTACK_GUARD_DELEGATED: "1" }, + encoding: "utf8", + }); + process.stdout.write(delegatedOut); + return; + } + } + } + let payload = {}; if (rawInput.trim().length > 0) { try { diff --git a/hooks/guard.test.mjs b/hooks/guard.test.mjs index 8179412..98a6255 100644 --- a/hooks/guard.test.mjs +++ b/hooks/guard.test.mjs @@ -70,6 +70,9 @@ function runGuard(payload, envOverrides = {}) { delete env.ANTIGRAVITY_APP_DATA_DIR; delete env.PLUGIN_DATA; delete env.PLUGIN_ROOT; + delete env.PSTACK_PLUGIN_ROOT; + delete env.PSTACK_CACHE_DIR; + delete env.PSTACK_GUARD_DELEGATED; delete env.PSTACK_ALLOW_PUSH; delete env.PSTACK_FORCE_COORDINATOR; for (const [k, v] of Object.entries(envOverrides)) { @@ -458,6 +461,14 @@ describe("Session classification (worker, coordinator, unmanaged) and O(1) looku ); assert.equal(out.decision, "allow"); } + const unmanagedAfterBoardTool = runGuard( + { + hookEventName: "SessionStart", + conversationId: "spontaneous-swarm-root", + }, + { PSTACK_APPDATA_DIR: sb.appDataDir }, + ); + assert.deepEqual(unmanagedAfterBoardTool, {}); writeSwarmMembers(sb.appDataDir, "own-swarm-coord", { coordinator: { conversation_id: "own-swarm-coord", role: "coordinator" }, @@ -1357,3 +1368,562 @@ describe("sicko-* Guard and git push Gate", () => { } }); }); + +describe("agy 1.5.9 audit fixes: shell vars, evolve isolation, numbered verifiers, B4 confinement, M2/M3/T1, and swarm capacity", () => { + test("B1+M2+B4 expands per-segment shell vars and redirects while anchoring canonicalTempRoots to outer env", () => { + const sb = makeSandbox(); + try { + writeSwarmMembers(sb.appDataDir, "coord-1", { + "ro-cli": { conversation_id: "ro-id", role: "worker" }, + "impl-cli": { conversation_id: "impl-id", role: "worker" }, + "adv-cli": { conversation_id: "adv-id", role: "worker" }, + }); + + const runCmd = (convId, CommandLine) => + runGuard( + { + hookEventName: "PreToolUse", + conversationId: convId, + cwd: sb.worktree, + toolCall: { id: "v1", name: "run_command", args: { CommandLine } }, + }, + { PSTACK_APPDATA_DIR: sb.appDataDir, TMPDIR: sb.customTmpDir }, + ); + + const allowedCommands = [ + 'RUN_DIR="/tmp/cli-${WORKER_NAME:-local}-$$-$(date +%s)"; mkdir -p "$RUN_DIR/home" "$RUN_DIR/tmp" "$RUN_DIR/artifacts" && echo ok > "$RUN_DIR/artifacts/help.txt"', + 'RUN_ID="run-42"; mkdir -p "/tmp/verify-app-$RUN_ID" "/tmp/statechart-${WORKER_NAME:-local}-${RUN_ID:-$$}"', + 'RUN_DIR="/tmp/cli-$(date -u +%Y%m%d)-$(uuidgen)"; mkdir -p "$RUN_DIR" && echo x > "$RUN_DIR/out.txt"', + "HOME=/tmp/h echo hi > ~/out.txt", + "export HOME=/tmp/h; echo ok > ~/ok.txt", + ]; + + for (const convId of ["ro-id", "impl-id", "adv-id"]) { + for (const cmd of allowedCommands) { + assert.equal(runCmd(convId, cmd).decision, "allow", `expected allow for ${convId}: ${cmd}`); + } + } + + const deniedCommands = [ + `export TMPDIR=${sb.outsideDir}; echo x > $TMPDIR/pwn.txt`, + `TMPDIR=${sb.outsideDir} mkdir -p $TMPDIR/pwn`, + "echo x > $UNSET_VAR/pwn.txt", + "echo x > $(id)/pwn.txt", + `RUN_DIR="${sb.outsideDir}"; echo x > "$RUN_DIR/pwn.txt"`, + ]; + + for (const convId of ["ro-id", "impl-id", "adv-id"]) { + for (const cmd of deniedCommands) { + assert.equal(runCmd(convId, cmd).decision, "deny", `expected deny for ${convId}: ${cmd}`); + } + } + } finally { + sb.cleanup(); + } + }); + + test("B2 isolates evolve trials via PSTACK_PLUGIN_ROOT delegation and PSTACK_CACHE_DIR", () => { + const sb = makeSandbox(); + try { + const stagedPlugin = join(sb.root, "pack-oak"); + const stagedCache = join(sb.root, "state-oak"); + mkdirSync(join(stagedPlugin, "hooks"), { recursive: true }); + mkdirSync(join(stagedPlugin, "roles"), { recursive: true }); + writeFileSync(join(stagedPlugin, "roles", "coordinator.md"), "staged-oak-coord\n", "utf8"); + writeFileSync( + join(stagedPlugin, "hooks", "guard.mjs"), + `#!/usr/bin/env node\nprocess.stdout.write(JSON.stringify({ delegatedFrom: "pack-oak", delegatedFlag: process.env.PSTACK_GUARD_DELEGATED }) + "\\n");\n`, + "utf8", + ); + + const delegatedOut = runGuard( + { hookEventName: "SessionStart", conversationId: "oak-conv" }, + { PSTACK_PLUGIN_ROOT: stagedPlugin, PSTACK_CACHE_DIR: stagedCache }, + ); + assert.deepEqual(delegatedOut, { delegatedFrom: "pack-oak", delegatedFlag: "1" }); + + writeSwarmMembers(sb.appDataDir, "coord-evolve", { + "ro-oak": { conversation_id: "ro-oak-id", role: "worker" }, + }); + const stagedRolesOnly = join(sb.root, "pack-pine"); + mkdirSync(join(stagedRolesOnly, "roles"), { recursive: true }); + writeFileSync(join(stagedRolesOnly, "roles", "research.md"), "pine-research-capsule\n", "utf8"); + + const isolatedSessionOut = runGuard( + { hookEventName: "SessionStart", conversationId: "ro-oak-id" }, + { + PSTACK_APPDATA_DIR: sb.appDataDir, + PSTACK_PLUGIN_ROOT: stagedRolesOnly, + PSTACK_CACHE_DIR: stagedCache, + }, + ); + assert.deepEqual(isolatedSessionOut, { + injectSteps: [{ systemMessage: { systemMessage: "pine-research-capsule\n" } }], + }); + assert.equal(existsSync(join(stagedCache, "injected-capsules.json")), true); + assert.equal(existsSync(join(stagedCache, "member-map.json")), true); + assert.equal(existsSync(join(sb.appDataDir, "plugin_data", "agy-pstack-next", "injected-capsules.json")), false); + } finally { + sb.cleanup(); + } + }); + + test("B3 maps numbered *-verifier-k copies to readonly while keeping impl-verifier-k and sicko-verifier-k roles", () => { + const sb = makeSandbox(); + try { + const researchCapsule = readFileSync(join(PLUGIN_ROOT, "roles", "research.md"), "utf8"); + const agentCapsule = readFileSync(join(PLUGIN_ROOT, "roles", "poteto-agent.md"), "utf8"); + const sickoCapsule = readFileSync(join(PLUGIN_ROOT, "roles", "comment-sicko.md"), "utf8"); + + writeSwarmMembers(sb.appDataDir, "coord-b3", { + "api-verifier-1": { conversation_id: "api-v1", role: "worker" }, + "api-verifier-2": { conversation_id: "api-v2", role: "worker" }, + "impl-verifier-1": { conversation_id: "impl-v1", role: "worker" }, + "sicko-verifier-1": { conversation_id: "sicko-v1", role: "worker" }, + }); + + for (const id of ["api-v1", "api-v2"]) { + const sOut = runGuard( + { hookEventName: "SessionStart", conversationId: id }, + { PSTACK_APPDATA_DIR: sb.appDataDir }, + ); + assert.deepEqual(sOut, { injectSteps: [{ systemMessage: { systemMessage: researchCapsule } }] }); + + const wOut = runGuard( + { + hookEventName: "PreToolUse", + conversationId: id, + cwd: sb.worktree, + toolCall: { id: "t1", name: "write_file", args: { Path: join(sb.worktree, "x.txt") } }, + }, + { PSTACK_APPDATA_DIR: sb.appDataDir }, + ); + assert.equal(wOut.decision, "deny"); + } + + assert.deepEqual( + runGuard({ hookEventName: "SessionStart", conversationId: "impl-v1" }, { PSTACK_APPDATA_DIR: sb.appDataDir }), + { injectSteps: [{ systemMessage: { systemMessage: agentCapsule } }] }, + ); + assert.deepEqual( + runGuard({ hookEventName: "SessionStart", conversationId: "sicko-v1" }, { PSTACK_APPDATA_DIR: sb.appDataDir }), + { injectSteps: [{ systemMessage: { systemMessage: sickoCapsule } }] }, + ); + } finally { + sb.cleanup(); + } + }); + + test("B4 enforces git, download/archive, find, and temp script confinement while preserving swarm capacity", () => { + const sb = makeSandbox(); + const slashTmpDir = mkdtempSync("/tmp/pstack-b4-"); + try { + writeSwarmMembers(sb.appDataDir, "coord-b4", { + "ro-verify": { conversation_id: "ro-id", role: "worker" }, + "impl-1": { conversation_id: "impl-id", role: "worker" }, + "adv-1": { conversation_id: "adv-id", role: "worker" }, + "sicko-1": { conversation_id: "sicko-id", role: "worker" }, + }); + + const badJs = join(slashTmpDir, "bad.mjs"); + const okJs = join(slashTmpDir, "ok.mjs"); + const badPy = join(slashTmpDir, "bad.py"); + const okPy = join(slashTmpDir, "ok.py"); + const badSh = join(slashTmpDir, "bad.sh"); + const okSh = join(slashTmpDir, "ok.sh"); + const perlScript = join(slashTmpDir, "probe.pl"); + const codemodJs = join(slashTmpDir, "codemod.mjs"); + const outsidePy = join(slashTmpDir, "outside.py"); + + writeFileSync(badJs, "import fs from 'node:fs'; fs.writeFileSync('README.md', 'pwn');\n", "utf8"); + writeFileSync( + okJs, + `import fs from 'node:fs'; import path from 'node:path'; const runDir = '${slashTmpDir}'; fs.writeFileSync(path.join(runDir, 'after.png'), 'ok'); fs.writeFileSync(\`/tmp/ui-\${id}/shot.png\`, 'ok');\n`, + "utf8", + ); + writeFileSync(badPy, "from pathlib import Path\nPath('README.md').write_text('pwn')\n", "utf8"); + writeFileSync( + okPy, + `import os, tempfile\nrun_dir = tempfile.mkdtemp(prefix='ui-')\nopen(os.path.join(run_dir, 'out.txt'), 'w').write('ok')\nopen(f'/tmp/ui-{id}/snap.txt', 'w').write('ok')\n`, + "utf8", + ); + writeFileSync(badSh, "#!/bin/sh\nrm -f README.md\n", "utf8"); + writeFileSync(okSh, `#!/bin/sh\necho ok > ${join(slashTmpDir, "sh-ok.txt")}\n`, "utf8"); + writeFileSync(perlScript, "print 'hi';\n", "utf8"); + writeFileSync( + codemodJs, + "import fs from 'node:fs'; for (const f of files) { fs.writeFileSync(f, 'updated'); }\n", + "utf8", + ); + writeFileSync( + outsidePy, + `from pathlib import Path\nPath('${join(sb.outsideDir, "pwn.txt")}').write_text('x')\n`, + "utf8", + ); + + const runCmd = (convId, CommandLine) => + runGuard( + { + hookEventName: "PreToolUse", + conversationId: convId, + cwd: sb.worktree, + toolCall: { id: "b4", name: "run_command", args: { CommandLine } }, + }, + { PSTACK_APPDATA_DIR: sb.appDataDir, TMPDIR: sb.customTmpDir }, + ); + + const roAllowed = [ + "git fetch origin", + "git notes list", + "git replace -l", + "git submodule status", + "git symbolic-ref HEAD", + "curl https://example.com", + `curl -o ${join(slashTmpDir, "dl.json")} https://example.com`, + "wget --spider https://example.com", + `wget -P ${slashTmpDir} https://example.com/a.txt`, + "tar -tf archive.tar", + `tar -xf archive.tar -C ${slashTmpDir}`, + "unzip -l archive.zip", + `unzip archive.zip -d ${slashTmpDir}`, + `rsync -n -a src/ ${sb.outsideDir}/`, + `find ${slashTmpDir} -delete`, + `node ${okJs}`, + `python3 ${okPy}`, + `sh ${okSh}`, + `node -e "import fs from 'node:fs'; fs.writeFileSync(\\\`/tmp/ui-\${id}/after.png\\\`, 'ok')"`, + "python3 -c \"open(f'/tmp/ui-{id}/snap.txt', 'w').write('ok')\"", + ]; + for (const cmd of roAllowed) { + assert.equal(runCmd("ro-id", cmd).decision, "allow", `expected ro allow: ${cmd}`); + } + + const roDenied = [ + "git pull", + "git update-ref HEAD HEAD~1", + "git fetch origin main:refs/heads/x", + "git fetch -p", + "git fetch --force", + "git notes add -m x", + "git replace HEAD~1 HEAD", + "git submodule update", + "git symbolic-ref HEAD refs/heads/x", + "truncate -s 0 README.md", + "dd if=/dev/zero of=README.md count=1", + "install src.txt README.md", + "curl -o README.md https://example.com", + "curl -O https://example.com/a.txt", + "wget https://example.com/a.txt", + "wget -O README.md https://example.com", + "tar -xf archive.tar", + "tar -cf out.tar src/", + "unzip archive.zip", + "rsync -a src/ dst/", + "rsync -a src/ user@host:dst/", + "find . -delete", + "find . -fprint out.txt", + "find . -exec rm {} ;", + `node ${join(slashTmpDir, "nonexistent.mjs")}`, + `node ${badJs}`, + `python3 ${badPy}`, + `sh ${badSh}`, + `perl ${perlScript}`, + ]; + for (const cmd of roDenied) { + assert.equal(runCmd("ro-id", cmd).decision, "deny", `expected ro deny: ${cmd}`); + } + + const authorAllowed = [ + `git fetch ${sb.siblingWorktree} abc1234 && git cherry-pick abc1234`, + `git -C ${sb.siblingWorktree} status --short`, + `git -C ${join(slashTmpDir, "trial")} init && git -C ${join(slashTmpDir, "trial")} add . && git -C ${join(slashTmpDir, "trial")} commit -m init`, + `node ${codemodJs}`, + "node -e \"const fs = require('fs'); for (const f of files) fs.writeFileSync(f, 'x')\"", + "python3 -c \"from pathlib import Path\nfor p in paths:\n Path(p).write_text('x')\"", + `mkdir -p ${join(slashTmpDir, "d")} && touch ${join(slashTmpDir, "d", "f")}`, + `curl -o ${join(slashTmpDir, "a.json")} https://example.com`, + ]; + for (const convId of ["impl-id", "adv-id"]) { + for (const cmd of authorAllowed) { + assert.equal(runCmd(convId, cmd).decision, "allow", `expected ${convId} allow: ${cmd}`); + } + } + + const authorDenied = [ + `git -C ${sb.outsideDir} commit -m pwn`, + `mkdir -p ${join(sb.outsideDir, "pwn")}`, + `touch ${join(sb.outsideDir, "pwn.txt")}`, + `chmod +x ${join(sb.outsideDir, "pwn.sh")}`, + `truncate -s 0 ${join(sb.outsideDir, "pwn.txt")}`, + `dd if=/dev/zero of=${join(sb.outsideDir, "pwn.bin")} count=1`, + `install README.md ${join(sb.outsideDir, "README.md")}`, + `curl -o ${join(sb.outsideDir, "pwn.txt")} https://example.com`, + `wget -P ${sb.outsideDir} https://example.com/a.txt`, + `tar -xf a.tar -C ${sb.outsideDir}`, + `unzip a.zip -d ${sb.outsideDir}`, + `rsync -a src/ ${sb.outsideDir}/`, + `find ${sb.outsideDir} -delete`, + `find ${sb.outsideDir} -exec rm {} ;`, + `node -e "require('fs').writeFileSync('${join(sb.outsideDir, "pwn.txt")}', 'x')"`, + `python3 -c "from pathlib import Path; Path('../../outside/x').write_text('x')"`, + `python3 ${outsidePy}`, + ]; + for (const convId of ["impl-id", "adv-id"]) { + for (const cmd of authorDenied) { + assert.equal(runCmd(convId, cmd).decision, "deny", `expected ${convId} deny: ${cmd}`); + } + } + + for (const sickoOk of ["git add app.mjs", "git commit -m 'strip comments'", "git status --short"]) { + assert.equal(runCmd("sicko-id", sickoOk).decision, "allow", `expected sicko allow: ${sickoOk}`); + } + for (const sickoBad of [ + "git checkout -- .", + "git restore app.mjs", + "git reset --hard HEAD", + "git clean -fd", + "git pull", + `git -C ${sb.outsideDir} status`, + "chmod +x app.mjs", + "curl -o app.mjs https://example.com", + "wget https://example.com/a.txt", + "tar -xf a.tar", + "unzip a.zip", + "rsync -a a b", + "find . -delete", + `node ${okJs}`, + `node -e "require('fs').writeFileSync('${join(slashTmpDir, "x.txt")}', 'x')"`, + `python3 -c "open('${join(slashTmpDir, "x.txt")}', 'w').write('x')"`, + ]) { + assert.equal(runCmd("sicko-id", sickoBad).decision, "deny", `expected sicko deny: ${sickoBad}`); + } + } finally { + rmSync(slashTmpDir, { recursive: true, force: true }); + sb.cleanup(); + } + }); + + test("M2+M3+T1 fixes node array literals, python Path aliases, transcript step filtering, forkStep, and swarm.json anchor", () => { + const sb = makeSandbox(); + try { + const researchCapsule = readFileSync(join(PLUGIN_ROOT, "roles", "research.md"), "utf8"); + const agentCapsule = readFileSync(join(PLUGIN_ROOT, "roles", "poteto-agent.md"), "utf8"); + const coordCapsule = readFileSync(join(PLUGIN_ROOT, "roles", "coordinator.md"), "utf8"); + + writeSwarmMembers(sb.appDataDir, "coord-m", { + "ro-m": { conversation_id: "ro-m-id", role: "worker" }, + "impl-fork": { conversation_id: "impl-fork-id", role: "worker" }, + }); + + const runRo = (CommandLine) => + runGuard( + { + hookEventName: "PreToolUse", + conversationId: "ro-m-id", + cwd: sb.worktree, + toolCall: { id: "m2", name: "run_command", args: { CommandLine } }, + }, + { PSTACK_APPDATA_DIR: sb.appDataDir }, + ); + + for (const okCmd of [ + "node -e \"console.log(['arm64']); console.log(['blink']); const x = ['rm'];\"", + "node -e \"function f() { return ['rm']; }\"", + "python3 -c \"from pathlib import Path as P, PurePath as PP; print(P('.'), PP('a'))\"", + ]) { + assert.equal(runRo(okCmd).decision, "allow", `expected M2 allow: ${okCmd}`); + } + + for (const badCmd of [ + "node -e \"const fs = require('fs'); fs['rmSync']('README.md')\"", + "node -e \"require('fs')['rm']('README.md', () => {})\"", + "node -e \"const fs = require('fs'); const fn = 'rmSync'; fs[fn]('README.md')\"", + "python3 -c \"from os import remove as r; r('hooks.json')\"", + "python3 -c \"import shutil as s; s.rmtree('skills')\"", + ]) { + assert.equal(runRo(badCmd).decision, "deny", `expected M2 deny: ${badCmd}`); + } + + const readFileTranscript = join( + sb.appDataDir, + "brain", + "read-file-user-conv", + ".system_generated", + "logs", + "transcript.jsonl", + ); + writeTranscriptLines(readFileTranscript, [ + { step_index: 0, type: "USER_INPUT", content: "Read roles/coordinator.md" }, + { step_index: 1, type: "TOOL_OUTPUT", content: "# Coordinator role capsule\n..." }, + ]); + writeSwarmMembers(sb.appDataDir, "decoy-swarm", { + "ro-decoy": { conversation_id: "read-file-user-conv", role: "worker" }, + }); + assert.deepEqual( + runGuard( + { + hookEventName: "SessionStart", + conversationId: "read-file-user-conv", + transcriptPath: readFileTranscript, + }, + { PSTACK_APPDATA_DIR: sb.appDataDir }, + ), + {}, + ); + + const forkTranscript = join( + sb.appDataDir, + "brain", + "impl-fork-id", + ".system_generated", + "logs", + "transcript.jsonl", + ); + writeTranscriptLines(forkTranscript, [ + { step_index: 0, type: "USER_INPUT", content: "Help me refactor this repo" }, + { + step_index: 1, + type: "USER_INPUT", + content: "You are @impl-fork. Everything before this turn is @coordinator's memory.", + }, + ]); + const forkSessionOut = runGuard( + { + hookEventName: "SessionStart", + conversationId: "impl-fork-id", + transcriptPath: forkTranscript, + }, + { PSTACK_APPDATA_DIR: sb.appDataDir }, + ); + assert.deepEqual(forkSessionOut, { + injectSteps: [{ systemMessage: { systemMessage: agentCapsule } }], + }); + + const spannerCoordDir = join(sb.appDataDir, "brain", "spanner-coord", ".system_generated", "swarm"); + mkdirSync(spannerCoordDir, { recursive: true }); + writeFileSync( + join(spannerCoordDir, "swarm.json"), + JSON.stringify({ backend: "spanner", coordinator_conversation_id: "spanner-coord" }), + "utf8", + ); + const spannerCoordOut = runGuard( + { hookEventName: "SessionStart", conversationId: "spanner-coord" }, + { PSTACK_APPDATA_DIR: sb.appDataDir }, + ); + assert.deepEqual(spannerCoordOut, { + injectSteps: [{ systemMessage: { systemMessage: coordCapsule } }], + }); + + const spannerWorkerTranscript = join( + sb.appDataDir, + "brain", + "spanner-worker", + ".system_generated", + "logs", + "transcript.jsonl", + ); + writeTranscriptLines(spannerWorkerTranscript, [ + { step_index: 0, type: "SYSTEM_MESSAGE", content: "[swarm] new board message #1" }, + ]); + runGuard( + { + hookEventName: "SessionStart", + conversationId: "spanner-worker", + transcriptPath: spannerWorkerTranscript, + }, + { PSTACK_APPDATA_DIR: sb.appDataDir }, + ); + const nonWorkerPath = join(sb.appDataDir, "plugin_data", "agy-pstack-next", "non-worker-roots.json"); + const nonWorkerDoc = existsSync(nonWorkerPath) ? JSON.parse(readFileSync(nonWorkerPath, "utf8")) : {}; + assert.equal(nonWorkerDoc["spanner-worker"], undefined); + assert.equal(researchCapsule.length > 0, true); + } finally { + sb.cleanup(); + } + }); + + test("regression #18: source/., preload flags, shebang-less /tmp execs, scriptEnv subshell/conditional/for scoping, and ro-* harness literals", () => { + const sb = makeSandbox(); + const slashTmpDir = mkdtempSync("/tmp/pstack-reg18-"); + try { + writeSwarmMembers(sb.appDataDir, "coord-reg18", { + "ro-v": { conversation_id: "ro-id", role: "worker" }, + "impl-v": { conversation_id: "impl-id", role: "worker" }, + "sicko-v": { conversation_id: "sicko-id", role: "worker" }, + }); + + const malSh = join(slashTmpDir, "mal.sh"); + const malJs = join(slashTmpDir, "mal.mjs"); + const malPy = join(slashTmpDir, "mal_mod.py"); + const malNoExt = join(slashTmpDir, "mal_noext"); + const binFile = join(slashTmpDir, "bin_noext"); + const outsideTarget = join(sb.outsideDir, "pwn.txt"); + writeFileSync(malSh, `curl -o ${outsideTarget} https://example.com\n`, "utf8"); + writeFileSync(malJs, "require('fs').writeFileSync('README.md', 'pwn');\n", "utf8"); + writeFileSync(malPy, "from pathlib import Path\nPath('README.md').write_text('pwn')\n", "utf8"); + writeFileSync(malNoExt, "rm -f README.md\n", "utf8"); + writeFileSync(binFile, Buffer.from([0x7f, 0x45, 0x4c, 0x46, 0x00, 0x01])); + + const runCmd = (convId, CommandLine) => + runGuard( + { + hookEventName: "PreToolUse", + conversationId: convId, + cwd: sb.worktree, + toolCall: { id: "r18", name: "run_command", args: { CommandLine } }, + }, + { PSTACK_APPDATA_DIR: sb.appDataDir, TMPDIR: sb.customTmpDir }, + ); + + for (const convId of ["ro-id", "impl-id", "sicko-id"]) { + assert.equal(runCmd(convId, `source ${malSh}`).decision, "deny", `source denied on ${convId}`); + assert.equal(runCmd(convId, `. ${malSh}`).decision, "deny", `. denied on ${convId}`); + } + + for (const roDeny of [ + `node --import ${malJs} -e "1"`, + `node -r ${malJs} -e "1"`, + `node --require=${malJs} -e "1"`, + `NODE_OPTIONS="--import ${malJs}" node -e "1"`, + `PYTHONPATH=${slashTmpDir} python3 -m mal_mod`, + malNoExt, + binFile, + `A=${sb.worktree}; (A=${slashTmpDir}); echo pwn > $A/README.md`, + `A=${sb.worktree}; false && A=${slashTmpDir}; echo pwn > $A/README.md`, + `A=${sb.worktree}; true || A=${slashTmpDir}; echo pwn > $A/README.md`, + `A=${slashTmpDir}; for A in ${sb.worktree}; do echo pwn > $A/README.md; done`, + `A=${slashTmpDir}; read A; echo pwn > $A/README.md`, + `echo x | A=${slashTmpDir}; echo pwn > $A/README.md`, + "node -e \"const { link: fn } = require('fs'); fn('a', 'b')\"", + ]) { + assert.equal(runCmd("ro-id", roDeny).decision, "deny", `expected ro deny: ${roDeny}`); + } + + assert.equal( + runCmd("impl-id", `A=${sb.outsideDir}; (A=${slashTmpDir}); echo pwn > $A/pwn.txt`).decision, + "deny", + ); + assert.equal( + runCmd("impl-id", `A=${sb.outsideDir}; false && A=${slashTmpDir}; echo pwn > $A/pwn.txt`).decision, + "deny", + ); + assert.equal( + runCmd("impl-id", `A=${slashTmpDir}; for A in ${sb.outsideDir}; do echo pwn > $A/pwn.txt; done`).decision, + "deny", + ); + + for (const roAllow of [ + "python3 -c \"import os; run_dir = f'/tmp/cli-{os.getpid()}'; open(os.path.join(run_dir, 'out.txt'), 'w').write('ok')\"", + "python3 -c \"import os; run_dir = f'/tmp/cli-{os.getpid()}'; open(f'{run_dir}/out.txt', 'w').write('ok')\"", + "node -e \"const fs = require('node:fs'); const runDir = `/tmp/ui-artifacts-${Date.now()}`; fs.writeFileSync(`${runDir}/aria.json`, '{}')\"", + "node -e \"console.log(pkg['platform'], pkg['arm64'], pkg['blink'])\"", + "node -e \"const item = { from: f, link: raw }; console.log(item)\"", + ]) { + assert.equal(runCmd("ro-id", roAllow).decision, "allow", `expected ro allow: ${roAllow}`); + } + } finally { + rmSync(slashTmpDir, { recursive: true, force: true }); + sb.cleanup(); + } + }); +});