Files
planning-with-files/tests/test_root_mode_floor.py
OthmanAdi 3749f86ac3 fix: bind explicit plan selectors, floor slug modes on the project root, and classify doctor output on the data frame
Three defects reported by Raymond Manaloto (@sortakool), each reproduced on
Windows with Git Bash before any code changed. Two of them let a session work
on a plan nobody selected; the third made the diagnostic that should have
caught them report PASS.

closes #237: an explicit PLAN_ID was a hint, not a binding

resolve_from_env returned 1 both when no selector was set and when the selector
was rejected, so a PLAN_ID of valid slug shape naming no directory fell through
to .active_plan and then to newest-by-mtime. attest-plan.sh locked that other
plan at rc=0 and injection followed it. The guard in attest-plan.sh written for
exactly this case was unreachable, because the resolver never returned empty.
commands/plan-attest.md has promised the opposite since v3.9.0.

A non-empty PLAN_ID now resolves or stops, for every rejection reason: slug
shape (traversal included), naming no directory, or failing containment. An
empty value still means unset, which init-session.sh depends on. The rule went
into every resolver, because they disagreed otherwise: resolve-plan-dir.sh,
resolve-plan-dir.ps1, the inline resolver inside inject-plan.sh, and the
Hermes, OpenCode and Pi plugin resolvers. v3.14.0 had made the two plugin
resolvers fall through to match the shell; that parity was right and its target
was wrong.

Fixing the resolver alone would have moved the bug rather than removed it, so
every consumer was audited. check-complete.sh would have read the root plan's
completion state, which decides whether an autonomous run may stop.
ledger-summary.sh feeds phase counts into autonomous turns. phase-status.sh and
ledger-append.sh write into the directory they pick. The five Codex hooks call
the resolver directly and each fell through to the legacy root, stop.sh and
pre-compact.sh included. All of them now stop on a rejected selector, and the
once-per-turn routes print a notice, because a fail-closed path with no stated
cause would trade a wrong-plan bug for a silent one.

closes #238: a slug plan with no .mode bypassed the project's root .mode

A project makes attestation mandatory by committing a root .mode, a reviewed
project setting. inject-plan.sh read the slug's .mode and never the root's, and
init-session.sh writes no .mode unless --autonomous or --gated was passed, so
init-session.sh <name>, which /plan runs, produced a plan with no attestation
requirement and full injection. The policy was a flag the agent chose at plan
creation.

The root .mode is now a floor. Strictness-raising tokens count from either
file, so a slug may raise and never lower; the one strictness-lowering token,
plan-guard-off, needs the root's agreement. The same floor applies to the
completion gate in check-complete.sh and check-complete.ps1, which read .mode
the same way and had the same bypass, and to the Hermes and OpenCode plugins.
init-session.sh seeds a new slug from the root .mode so the effective policy is
visible in the plan directory, not only in the resolver. With no root .mode
present the effective token set is exactly the slug's, so existing projects are
unchanged.

closes #236: plan-doctor.sh reported PASS on a fully dark-hooks state

The injection check substring-matched five control strings against output that
carries the plan body verbatim inside the BEGIN-PWF-DATA fences. A plan whose
phase line read "fix the false PLAN TAMPERED warning in plan-doctor" reported a
hash mismatch while correctly attested. Worse, the PWF_PLAN_ROOT arm matched a
literal that is not a substring of what inject-plan.sh emits, so the arm was
dead code and execution fell to the success arm: PWF_PLAN_ROOT=/nonexistent
printed "PASS injection: emits plan context (123 bytes)" while nothing was
injected, counting the refusal notice's own bytes as plan context. The file's
own comment four lines above already warned against exactly that.

Classification now branches on the frame. Every refusal path exits before
frame_file runs, so a frame proves injection happened and unframed output is by
construction a notice. The default arm warns, so a reworded or translated
banner degrades noisily instead of silently. The stale literal is corrected
against the emitting echo.

Tests: Python suite 653 to 696, OpenCode Vitest 28 to 34. New files
test_plan_selector_binding.py, test_root_mode_floor.py and
test_plan_doctor_classification.py; new arms in test_resolver_parity.py,
test_resolve_plan_dir.py, test_resolver_plan_root_pin.py and
test_hermes_first_class.py. Five existing tests asserted the fall-through and
were rewritten to assert the refusal, keeping their original security
assertion. Each suite carries a control arm: two first drafts passed on
fixtures that could not have failed, a gate assertion over a plan with every
phase complete and a parallel-write-guard assertion over a plan with nothing
checked to lose.
2026-09-03 00:23:03 +02:00

210 lines
9.2 KiB
Python

"""A project's root .mode is a floor, not a default slug scope replaces (#238).
A project makes attestation mandatory by committing a root `.mode`
(`autonomous inject-smart`), a reviewed project setting. Creating a slug plan
silently exempted the session from it: inject-plan.sh read the SLUG's `.mode`
and never the root's, and init-session.sh writes no `.mode` unless
`--autonomous` or `--gated` was passed. So `init-session.sh <name>`, which
`/plan` runs, produced a plan with no mode, no attestation requirement and full
injection. The project's policy became a flag the agent chose at plan creation.
The fix reads strictness-RAISING tokens from either file (a slug may raise,
never lower) and honors the one strictness-LOWERING token, `plan-guard-off`,
only when the root carries it too. With no root `.mode` present the effective
token set is exactly the slug's, so existing projects are unchanged: the last
two tests pin that.
"""
from __future__ import annotations
import os
import shutil
import subprocess
import unittest
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
SCRIPTS = REPO_ROOT / "scripts"
SLUG = "2026-09-02-arbtask"
def have_sh() -> bool:
return shutil.which("sh") is not None
@unittest.skipUnless(have_sh(), "requires a POSIX sh")
class RootModeFloorTests(unittest.TestCase):
def setUp(self) -> None:
import tempfile
self.tempdir = tempfile.TemporaryDirectory(prefix="pwf-modefloor-")
self.root = Path(self.tempdir.name)
self.slug_dir = self.root / ".planning" / SLUG
self.slug_dir.mkdir(parents=True)
(self.root / "task_plan.md").write_text(
"# root plan (project policy)\n", encoding="utf-8"
)
# An in_progress phase and a pending one, so an ARMED gate has
# something to block on. With every phase complete the gate resolves to
# advisory whether or not it is armed, and a gate assertion over that
# fixture passes without testing anything.
(self.slug_dir / "task_plan.md").write_text(
"# slug plan\n"
"### Phase 1\n**Status:** in_progress\n"
"### Phase 2\n**Status:** pending\n",
encoding="utf-8",
)
def tearDown(self) -> None:
self.tempdir.cleanup()
def _run(self, script: str, *args: str, env_extra: dict | None = None):
env = os.environ.copy()
for key in ("PLAN_ID", "PWF_PLAN_ROOT", "PLANNING_DISABLED", "PWF_INJECT",
"PWF_PLAN_GUARD"):
env.pop(key, None)
if env_extra:
env.update(env_extra)
return subprocess.run(
["sh", str(SCRIPTS / script), *args],
cwd=str(self.root),
env=env,
capture_output=True,
text=True,
timeout=180,
)
def _inject(self):
return self._run("inject-plan.sh", "--context=userprompt").stdout
def test_slug_without_mode_inherits_the_root_attestation_requirement(self):
"""The reported fixture: one slug directory turned the policy off."""
(self.root / ".mode").write_text("autonomous inject-smart\n", encoding="utf-8")
out = self._inject()
self.assertIn("requires attested plan", out)
self.assertNotIn("BEGIN-PWF-DATA", out)
def test_attesting_the_slug_arms_injection_under_the_inherited_mode(self):
"""Fail-closed must not mean permanently dark: the documented remedy
has to work."""
(self.root / ".mode").write_text("autonomous inject-smart\n", encoding="utf-8")
attest = self._run("attest-plan.sh", env_extra={"PLAN_ID": SLUG})
self.assertEqual(0, attest.returncode, attest.stderr)
self.assertIn("BEGIN-PWF-DATA", self._inject())
def _gate(self) -> str:
env = os.environ.copy()
for key in ("PLAN_ID", "PWF_PLAN_ROOT", "PLANNING_DISABLED"):
env.pop(key, None)
return subprocess.run(
["sh", str(SCRIPTS / "check-complete.sh"), "--gate"],
cwd=str(self.root), env=env, capture_output=True, text=True,
timeout=180, stdin=subprocess.DEVNULL,
).stdout
def test_gate_stays_advisory_when_no_mode_file_asks_for_it(self):
"""The negative control for the two gate tests below. Without it they
would pass on a fixture where the gate can only ever be advisory."""
self._run("attest-plan.sh", env_extra={"PLAN_ID": SLUG})
self.assertNotIn('"decision":"block"', self._gate())
def test_a_slug_may_raise_strictness_above_the_root(self):
"""gate in the slug survives a root that only asks for autonomous."""
(self.root / ".mode").write_text("autonomous\n", encoding="utf-8")
(self.slug_dir / ".mode").write_text("autonomous gate\n", encoding="utf-8")
self._run("attest-plan.sh", env_extra={"PLAN_ID": SLUG})
self.assertIn('"decision":"block"', self._gate())
def _regress_the_plan(self) -> str:
"""Prime the parallel-write guard, then destroy checked work behind it.
The guard fires on checked items or completed phases that vanished
since the hooks last read the file, so the primed version must contain
some. Returns the injection output of the fire that follows the loss.
"""
(self.slug_dir / "task_plan.md").write_text(
"# slug plan\n- [x] finished work\n### Phase 1\n**Status:** complete\n",
encoding="utf-8",
)
self._run("attest-plan.sh", env_extra={"PLAN_ID": SLUG})
self._inject()
(self.slug_dir / "task_plan.md").write_text(
"# slug plan\n- [ ] finished work\n### Phase 1\n**Status:** pending\n",
encoding="utf-8",
)
self._run("attest-plan.sh", env_extra={"PLAN_ID": SLUG})
return self._inject()
def test_a_slug_cannot_lower_a_protection_the_root_kept_on(self):
"""plan-guard-off is the only strictness-lowering token. A root .mode
without it means the slug alone cannot switch the parallel-write guard
off."""
(self.root / ".mode").write_text("autonomous\n", encoding="utf-8")
(self.slug_dir / ".mode").write_text(
"autonomous plan-guard-off\n", encoding="utf-8"
)
self.assertIn("PLAN REGRESSED", self._regress_the_plan())
def test_plan_guard_off_still_works_when_the_root_agrees(self):
(self.root / ".mode").write_text(
"autonomous plan-guard-off\n", encoding="utf-8"
)
(self.slug_dir / ".mode").write_text(
"autonomous plan-guard-off\n", encoding="utf-8"
)
self.assertNotIn("PLAN REGRESSED", self._regress_the_plan())
def test_root_gate_token_arms_the_completion_gate_for_a_slug_plan(self):
"""The same bypass reached check-complete.sh's guard 1, which the issue
did not test but which reads .mode the same way."""
(self.root / ".mode").write_text("autonomous gate\n", encoding="utf-8")
self._run("attest-plan.sh", env_extra={"PLAN_ID": SLUG})
self.assertIn('"decision":"block"', self._gate())
def test_no_root_mode_leaves_a_slug_plan_exactly_as_before(self):
"""The legacy invariant: projects without a root .mode see no change."""
out = self._inject()
self.assertIn("BEGIN-PWF-DATA", out)
self.assertNotIn("requires attested plan", out)
def test_no_root_mode_still_lets_a_slug_turn_the_guard_off(self):
(self.slug_dir / ".mode").write_text(
"autonomous plan-guard-off\n", encoding="utf-8"
)
self.assertNotIn("PLAN REGRESSED", self._regress_the_plan())
def test_the_guard_fires_on_this_fixture_without_any_mode_file(self):
"""Positive control for the three tests above: the fixture really does
trip the guard, so a NotIn assertion is evidence of the token working
rather than of a fixture that never regressed anything."""
self.assertIn("PLAN REGRESSED", self._regress_the_plan())
def test_init_session_seeds_a_new_slug_from_the_root_mode(self):
"""inject-plan.sh enforces the floor at read time, so this is not the
guard. It makes the effective policy visible in the plan directory and
gives the new plan the nonce and attestation autonomous mode needs."""
(self.root / ".mode").write_text("autonomous\n", encoding="utf-8")
result = self._run("init-session.sh", "arbitrary task")
self.assertEqual(0, result.returncode, result.stderr)
created = [
d for d in (self.root / ".planning").iterdir()
if d.is_dir() and d.name != SLUG
]
self.assertEqual(1, len(created), created)
new_slug = created[0]
self.assertIn("autonomous", (new_slug / ".mode").read_text(encoding="utf-8"))
self.assertTrue((new_slug / ".attestation").exists())
def test_init_session_without_a_root_mode_writes_no_mode(self):
result = self._run("init-session.sh", "arbitrary task")
self.assertEqual(0, result.returncode, result.stderr)
created = [
d for d in (self.root / ".planning").iterdir()
if d.is_dir() and d.name != SLUG
]
self.assertEqual(1, len(created), created)
self.assertFalse((created[0] / ".mode").exists())
if __name__ == "__main__":
unittest.main()