From 5030b32d3f7e423346b129bb458a7ca2c82d2134 Mon Sep 17 00:00:00 2001 From: Violin Date: Sun, 12 Jul 2026 09:57:02 +0100 Subject: [PATCH] fix first command history gate --- scripts/guard/command.py | 5 ++++- scripts/guard/record.py | 15 ++++++++++++++- tests/test_plugin_guard.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/scripts/guard/command.py b/scripts/guard/command.py index 6772073..a23b8de 100644 --- a/scripts/guard/command.py +++ b/scripts/guard/command.py @@ -192,7 +192,10 @@ def _check_command_core(args: argparse.Namespace) -> CheckResult: skill_result = check_skill_load_gate(skill_loaded_file, mandatory=False) merge_result(result, skill_result, prefix="skill guard") - ptt_result = _ptt_staleness_guard(eng_dir_path / "state" / "ptt.md") + ptt_result = _ptt_staleness_guard( + eng_dir_path / "state" / "ptt.md", + eng_dir_path / "state" / "history.md", + ) merge_result(result, ptt_result, prefix="ptt guard") # Freshness guard: PTT "Last updated" + phase desync diff --git a/scripts/guard/record.py b/scripts/guard/record.py index 8a9ddde..5fd518d 100644 --- a/scripts/guard/record.py +++ b/scripts/guard/record.py @@ -380,7 +380,7 @@ def record_history(args: argparse.Namespace) -> int: return 0 -def _ptt_staleness_guard(ptt_path: Path) -> CheckResult: +def _ptt_staleness_guard(ptt_path: Path, history_path: Path | None = None) -> CheckResult: result = CheckResult() if not ptt_path.exists() or not ptt_path.is_file(): result.add_error(f"PTT missing: {ptt_path}") @@ -389,6 +389,19 @@ def _ptt_staleness_guard(ptt_path: Path) -> CheckResult: ) return result if _ptt_is_stale(ptt_path): + # On a fresh engagement, the first command necessarily runs before the + # executor can append its history record. Requiring a PTT transition at + # this point creates a bootstrap deadlock: the task must look started + # before any work has actually happened. Keep the exception narrow: as + # soon as history contains a recorded command, normal PTT enforcement + # resumes. + if history_path is not None and history_path.is_file(): + history = history_path.read_text(encoding="utf-8") + if not re.findall(r"`([^`]+)`", history): + result.add_info( + "PTT is pristine; the first command may run before task progress is recorded" + ) + return result result.add_error("PTT is stale: no PT-XXX row has moved past [ ]; update before advancing") result.add_info( 'run: python scripts/violin_guard.py record-ptt --eng-dir "$ENG_DIR" --id --status [~] --note ""' diff --git a/tests/test_plugin_guard.py b/tests/test_plugin_guard.py index 70cc31f..f9effdc 100644 --- a/tests/test_plugin_guard.py +++ b/tests/test_plugin_guard.py @@ -207,6 +207,38 @@ def test_fresh_history_allows_first_command(tmp_path): assert any("recorded after it runs" in info for info in result.infos) +def test_first_command_does_not_require_preemptive_ptt_progress(tmp_path): + """A pristine PTT is valid until the executor records the first command.""" + skill_file = tmp_path / ".skill-loaded-ts" + eng = _init_e2e(tmp_path, skill_file) + ptt = eng / "state" / "ptt.md" + ptt.write_text( + ptt.read_text(encoding="utf-8").replace("| PT-001 | [~] |", "| PT-001 | [ ] |"), + encoding="utf-8", + ) + + args = SimpleNamespace( + command="nmap -sV 10.10.10.10", + phase="recon", + eng_dir=str(eng), + scope=str(eng / "scope" / "scope.yaml"), + skill_loaded_file=str(skill_file), + session_id="ts", + ) + first = _check_command_core(args) + + assert not first.errors + assert any("first command may run" in info for info in first.infos) + + (eng / "state" / "history.md").write_text( + "# Command History\n- [2026-07-12T09:00:00Z] [RECON] exit=0 `nmap -sV 10.10.10.10`\n", + encoding="utf-8", + ) + second = _check_command_core(args) + + assert any("PTT is stale" in error for error in second.errors) + + def test_exec_blocked_without_skill_load(monkeypatch, tmp_path): """Skill-load gate: check-command BLOCKs when the SKILL.md marker is absent, and handle_exec honours that BLOCK (status 'denied')."""