Plan: tools allowed by rule live in one table, and every registered tool is classified

Closes: #941. Relates to: #136, #310, #565 (tools that fell through one at a time). Branch: fix/codemode-allowed-by-rule. Status: Done (2026-11-27) — tools allowed by rule are one table; codemode and three others joined it

Context

Ruled by the operator (2026-11-27): fix this structurally, not with one more copied line; all four unruled tools below go in the table. What the tree says today, by reading:

  • packages/pi-modes/modes.json gives auto, plan and manual a permission map each (yolo has none). 26 tools carry a one-rule map in each of the three: 78 entries, 77 of them {"*": "allow"}. The one difference is msg_post, which plan denies.

  • A tool with no rule in a mode reaches the judge stage (GATE_STAGES in packages/pi-modes/authorize.ts: "rule allow" needs a rule). Four tools the distribution loads have none: pi’s codemode and tool_search, and pi-subagents' get_subagent_result and steer_subagent.

  • For a helper the ask is forwarded to the lead’s session carrying the tool’s name as its value, so the judge sees no input, defers, and the step lands in /pending as «codemode: codemode» (#941 has the counts).

  • A call a tool makes through ctx.executeTool(), which is how a codemode script calls tools, goes "through argument validation and the tool_call and tool_result handlers like model-issued calls", and a blocked one comes back as an error result (node_modules/@earendil-works/pi-coding-agent/docs/extensions.md, "Calling Other Tools"). The permission system and the guards gate on tool_call, so each call inside a script is gated on its own.

  • Nothing checks that a registered tool has a rule. remember (#136), forget (#310) and count each got a test of their own after falling through (packages/pi-modes/test/mode-policy.test.ts, "deterministic instruments are not judge questions"; test/judge-wiring-callback.test.ts, "the package’s own tools never reach the judge").

  • The shared-list pattern exists: safetyValve is one top-level list that applySharedValve (packages/pi-modes/config.ts) fills into each mode’s judge when the final config is validated. validateModesConfig(raw, source, { resolveValve: false }) leaves it unresolved for the baseline that loadModesConfig merges an overlay over, so an overlay’s list is the one resolved. Every top-level key is listed in TOP_LEVEL_KEYS, which a test holds equal to modes.adoc’s summary table (`tools/lib/test/public-surface.test.mjs).

  • The tools our packages register are already inventoried from source: toolsIn(shippedSources(root)) in tools/lib/public-surface.mjs. packages/pi/test/web-tools.test.ts reads the tools pi-web-access registers through pi’s DefaultResourceLoader and holds them equal to a list.

Design

The table and the fold

ModesConfig gains allowedByRule?: string[] (added to TOP_LEVEL_KEYS): the tools allowed by rule in every mode that has rules. modes.json lists 30 names there, after safetyValve:

count, monitor, monitor_stop, monitor_list, monitor_restore, job, job_stop, job_list, msg_channels, msg_subscribe, msg_handle, msg_post, msg_read, pending_withdraw, generate_image, remember, forget, yield, get_search_content, fetch_content, web_enable, web_search, source_check, enter_plan_mode, exit_plan_mode, ask, and the four new ones: codemode, get_subagent_result, steer_subagent, tool_search.

The 77 {"": "allow"} entries leave the three modes. Plan keeps "msg_post": {"": "deny"}.

A new applySharedAllows(config) in config.ts, beside applySharedValve: for each mode with a permission map and each listed tool, a tool with no rules in that mode gets {"": "allow"}; a tool whose own rules there do not say what is gains "": "allow" beside them; a tool whose own rules set is left as the mode wrote it. validateModesConfig’s option is renamed `resolveShared and governs both lists: the final config is applySharedAllows(applySharedValve(cfg)), the baseline pass resolves neither. The option has three callers, all renamed: its definition and loadModesConfig in config.ts, and tools/lib/test/stage.test.mjs ("is an overlay pi-modes accepts, merged as pi-modes merges one"), which is untyped and would otherwise pass an option nothing reads. So an overlay’s allowedByRule replaces the shipped list, as its safetyValve does, and an overlay’s rule for a tool in a mode stands.

Tools that are never allowed by name

config.ts exports DECIDED_BY_INPUT: bash, powershell, read, write, edit, grep, find, ls, path, path_read, path_write, external_directory, external_directory_read, external_directory_write. Their asks are decided by the command or the path they carry. The config check refuses any of them in allowedByRule, naming it ("is decided by its command or path, never by name"): the shipped bash and read maps, and auto’s and manual’s write and edit, set no *, so listing one would allow everything no pattern names.

Classifying every registered tool

config.ts exports DECIDED_ELSEWHERE, a record of tool to reason, holding subagent ("the delegation gate answers every spawn"), and unruledTools(config, names): for each mode with rules, each name that has no rules in the resolved mode, is not in DECIDED_BY_INPUT, is not a key of DECIDED_ELSEWHERE and does not start mcp__ (an operator’s own servers), as { mode, tool }. Three tests hold it empty over every tool the distribution loads; see Tests.

Scope

  1. config.ts, modes.json, the renamed option’s caller in tools/lib/test/stage.test.mjs, the tests below, the docs and the CHANGELOG, on the branch; validate and consumer-sim; the MR.

  2. At approval, a follow-up issue is filed for the other half of the cause, which this plan does not touch: a forwarded ask does carry the tool’s input, as the "input" entry of its payload’s evidence, cut at 200 characters by the permission system, and pi-modes' normalizeAsk never reads it. Any unruled tool a helper calls (an MCP tool) still reaches the judge blind.

Not in this plan: reading that forwarded input; whether a helper may delegate; any judge prompt.

Tests

Category Test Pins

contract

packages/pi-modes/test/config.test.ts, new: the shipped config, validated, gives auto, plan and manual {"": "allow"} for each of the 30 tools, except plan’s msg_post, which is {"": "deny"}; yolo has no permission; and the shipped file itself holds none of the 30 as a rule in any mode but that one deny

the copies are gone and nothing lost its allow

contract

same file, new, on a small config: a mode’s own rule (deny, ask) is left; own pattern rules with no keep their patterns and gain "*": "allow"; a mode with no permission gets none; no list changes nothing; { resolveShared: false } leaves the modes unfilled

the fold

contract

same file, new: allowedByRule that is not an array of strings is refused, and so is each name in DECIDED_BY_INPUT, with its message; DECIDED_BY_INPUT holds every member of PATH_SURFACES (exported from judge-wiring.ts for this) and both shells

a path or a command is never allowed by name

contract

tools/lib/test/stage.test.mjs, the renamed call: the baseline it merges over also has no rule for a listed tool (count) in auto, so an option the validator ignores fails here

the rename reached the untyped caller

contract

same file as the rows above, beside "overlay safetyValve reaches the merged config": an overlay’s allowedByRule is the list resolved (a tool it drops has no rule, one it adds is allowed); an overlay’s {"*": "deny"} for a listed tool in a mode stands

the overlay

regression

test/judge-wiring-callback.test.ts, in "the package’s own tools never reach the judge": a codemode ask in auto, plan and manual is a rule allow with the judge never consulted, as a local ask and in the forwarded shape a helper’s ask has (surface and value both codemode, forwarding, no toolName)

#941’s case

contract

test/mode-policy.test.ts, in "deterministic instruments are not judge questions": evaluateRules allows each of the four new tools in auto, plan and manual

the four

contract

tools/lib/test/public-surface.test.mjs, new: unruledTools over the validated shipped config and toolsIn(shippedSources(ROOT)) is empty

a tool a package registers is classified

contract

packages/pi/test/web-tools.test.ts: reads the validated config, not the raw file; its loader helper takes the shim’s name; new: pi’s loader registers exactly get_subagent_result, steer_subagent, subagent from 30-subagents.ts; unruledTools over the web tools, those three and pi’s own codemode and tool_search is empty

a bundled extension’s tool is classified, and a renamed or added one fails

contract

new packages/pi-modes/test/nested-calls.test.ts: a real pi session on a scripted model, built as packages/pi-workflow/test/batch-order.test.ts builds one, with an inline extension that registers a tool calling ctx.executeTool("read", …) and a tool_call handler that blocks read: the handler sees the nested read with its parentToolCallId, and the outer tool’s result reports the block

a call made inside another tool is gated like a direct one, on the pinned pi

Mutants, each to fail its test: codemode removed from the list; the fold overwriting a mode’s own * rule; the check accepting bash; a tool name added to the inventory’s input with no classification. Coverage bars hold (pi-modes 97/90).

Risks

Risk Mode Mitigation

A tool decided by its input is listed and every command or path no pattern names is allowed

fail-closed

the config check refuses each DECIDED_BY_INPUT name; its test

The fold overrides a rule a mode wrote (plan’s msg_post deny)

fail-open

the fold never changes a * the mode set; tested on the shipped plan mode and on a small config

Removing the copies drops an allow, and a tool starts going to the judge

loud

the contract test over all 30 tools in three modes

codemode allowed by rule lets a script do what a direct call could not

fail-closed

each call a script makes fires tool_call and is gated on its own (pi’s documented contract); nested-calls.test.ts pins it on the pinned pi; a script whose text holds a secret-shaped value or a denied path is still held or denied by the stages ahead of the rule allow

An overlay adds a tool to the list

silent

the operator’s own file, as safetyValve is; the dangerous names are refused

A tool pi itself adds is not seen by the tests (the two built-ins are a literal list)

silent

it has no rule, so its calls go to the judge as today; the list is re-read when pi is bumped

The subagents shim does not load alone through the loader in a unit test

loud

the test fails; its three names are then read from pi-subagents' source as toolsIn reads ours, recorded as an erratum

Delivery

  • Canonical page: docs/modules/ROOT/pages/modes.adoc: a row for allowedByRule in the top-level table, and the "Mode rules" paragraph rewritten around the table (what it holds, that a mode’s own rule wins, which tools can never be listed, what an unlisted tool does).

  • CHANGELOG.adoc == Unreleased: a Fixed entry (a helper’s codemode call no longer waits on the human) and a Changed entry (the table).

  • This plan committed as docs/modules/ROOT/pages/plans/tools-allowed-by-rule.adoc, listed in docs/modules/ROOT/nav.adoc; Status In progress at start, Done at the merge.

  • Validate and consumer-sim before the push; the MR (Closes #941) merged with squash off; the merge SHA on the issue. pi-modes takes a bump at the next release.

Edit this page · latest