mirror of
https://github.com/Strategic-Automation/violin.git
synced 2026-08-14 12:33:37 +02:00
Fix bounded sync credit enforcement
This commit is contained in:
@@ -27,23 +27,9 @@ __all__ = [
|
||||
"check_skill_load",
|
||||
"check_history_staleness",
|
||||
"check_hypothesis_freshness",
|
||||
"command_leading_tool",
|
||||
"LOCAL_TOOLS",
|
||||
]
|
||||
|
||||
|
||||
# Local tools that don't touch targets (no sync credit spent)
|
||||
LOCAL_TOOLS = {"curl", "dig", "host", "nslookup", "whois"}
|
||||
|
||||
|
||||
def command_leading_tool(command: str) -> str:
|
||||
"""Extract the leading tool name from a command string."""
|
||||
parts = command.strip().split()
|
||||
if not parts:
|
||||
return ""
|
||||
return parts[0]
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Argument / Result dataclasses
|
||||
# --------------------------------------------------------------------------- #
|
||||
@@ -465,17 +451,22 @@ def check_command(args: CheckCommandArgs) -> CheckResult:
|
||||
pending = state.get_pending_sync(str(eng_dir))
|
||||
if pending:
|
||||
credit = state.sync_credit_remaining(str(eng_dir))
|
||||
last_command = (pending.get("commands") or [{}])[-1].get(
|
||||
"command", pending.get("command", "prior command")
|
||||
)
|
||||
if credit == 0:
|
||||
# Hard block only after sync-credit window exhausted
|
||||
result.add_error(
|
||||
f"prior command's artifacts not synced: {pending.get('command')} "
|
||||
f"prior command's artifacts not synced: {last_command} "
|
||||
f"(phase: {pending.get('phase')})"
|
||||
)
|
||||
else:
|
||||
# Warning within window
|
||||
result.add_warning(
|
||||
f"prior command's artifacts not synced: {pending.get('command')} "
|
||||
f"(phase: {pending.get('phase')}) — sync before window exhausts"
|
||||
# A bounded batch is intentionally allowed to consume its five
|
||||
# credits. This must be informational, not a REVIEW: otherwise
|
||||
# handle_exec denies command two unless global YOLO mode is on.
|
||||
result.add_info(
|
||||
f"bounded batch in progress after: {last_command} "
|
||||
f"(phase: {pending.get('phase')}); {credit} credit(s) remain"
|
||||
)
|
||||
|
||||
# 8. Sync-credit window exhausted
|
||||
|
||||
@@ -18,7 +18,6 @@ from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from . import state
|
||||
from .command import LOCAL_TOOLS, command_leading_tool
|
||||
from .phases import normalize_phase
|
||||
|
||||
__all__ = [
|
||||
@@ -264,7 +263,7 @@ def execute(
|
||||
|
||||
_record_history(engagement, command, exit_code, phase, rel_manifest)
|
||||
|
||||
if command_leading_tool(command) in LOCAL_TOOLS:
|
||||
if state.is_local_bookkeeping_command(command):
|
||||
remaining = state.sync_credit_remaining(str(engagement))
|
||||
else:
|
||||
remaining = _commit_guard_state(engagement, command, phase)
|
||||
|
||||
@@ -30,9 +30,22 @@ MESSAGE_INTERVAL = 30
|
||||
MAX_BURST_COMMANDS = 20
|
||||
RETRY_LIMIT = 3
|
||||
|
||||
# Commands that are purely local bookkeeping. Network clients remain guarded.
|
||||
# Commands that are purely local bookkeeping. Network clients are deliberately
|
||||
# absent: curl/dig/host/nslookup/whois can touch an engagement target and must
|
||||
# consume the same bounded review credits as every other violin_exec command.
|
||||
LOCAL_TOOLS = {"echo", "true", "false", "printf", "pwd", "ls", "cat", "date"}
|
||||
|
||||
|
||||
def is_local_bookkeeping_command(command: str) -> bool:
|
||||
"""Whether a command is a harmless local bookkeeping action.
|
||||
|
||||
The executor is the only consumer of this classification. Keeping it here
|
||||
avoids a second, divergent LOCAL_TOOLS list in command policy.
|
||||
"""
|
||||
leading = command.strip().split(maxsplit=1)
|
||||
return bool(leading) and leading[0] in LOCAL_TOOLS
|
||||
|
||||
|
||||
_STATE_DIR = "state"
|
||||
_SYNC_FILE = "sync.json"
|
||||
_HEARTBEAT_FILE = "heartbeat.json"
|
||||
@@ -376,6 +389,7 @@ __all__ = [
|
||||
"artifacts_are_fresh",
|
||||
"suppresses_heartbeat",
|
||||
"LOCAL_TOOLS",
|
||||
"is_local_bookkeeping_command",
|
||||
"append_history",
|
||||
"mark_ptt_reviewed",
|
||||
"history_contains",
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
"""Regression coverage for the executor-owned bounded sync window."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from plugins.violin_guard.core import bootstrap, execution, service, state
|
||||
|
||||
|
||||
def _engagement(tmp_path: Path) -> Path:
|
||||
eng = tmp_path / "10.10.10.10-2026-07-13"
|
||||
assert bootstrap.init_engagement(eng, host="10.10.10.10") == 0
|
||||
(eng / "state" / ".skill-loaded-test").write_text("skill-loaded: test\n", encoding="utf-8")
|
||||
ptt_path = eng / "state" / "ptt.md"
|
||||
ptt_path.write_text(
|
||||
ptt_path.read_text(encoding="utf-8").replace("| PT-001 | [ ] |", "| PT-001 | [~] |"),
|
||||
encoding="utf-8",
|
||||
)
|
||||
return eng
|
||||
|
||||
|
||||
def test_network_clients_are_not_local_bookkeeping() -> None:
|
||||
"""Target-facing network tools must always arm review state."""
|
||||
for command in ("curl https://10.10.10.10", "dig 10.10.10.10", "host 10.10.10.10"):
|
||||
assert not state.is_local_bookkeeping_command(command)
|
||||
assert state.is_local_bookkeeping_command("echo local-note")
|
||||
|
||||
|
||||
def test_five_commands_run_without_yolo_then_sixth_blocks(monkeypatch, tmp_path: Path) -> None:
|
||||
"""The bounded window is an allowance, not five REVIEW responses."""
|
||||
eng = _engagement(tmp_path)
|
||||
|
||||
def fake_execute(command: str, *, eng_dir: str, phase: str, **_kwargs):
|
||||
remaining = execution._commit_guard_state(Path(eng_dir), command, phase)
|
||||
return {
|
||||
"status": "completed",
|
||||
"executed": True,
|
||||
"exit_code": 0,
|
||||
"sync_required": remaining <= 0,
|
||||
"sync_credit_remaining": remaining,
|
||||
"evidence_paths": {},
|
||||
}
|
||||
|
||||
monkeypatch.setattr(execution, "execute", fake_execute)
|
||||
args = {
|
||||
"eng_dir": str(eng),
|
||||
"scope": str(eng / "scope" / "scope.yaml"),
|
||||
"phase": "recon",
|
||||
"session_id": "test",
|
||||
}
|
||||
|
||||
for port in range(1, state.DEFAULT_SYNC_CREDIT + 1):
|
||||
result = json.loads(service.handle_exec({**args, "command": f"nmap -p {port} 10.10.10.10"}))
|
||||
assert result["status"] == "ok", result
|
||||
|
||||
sixth = json.loads(service.handle_exec({**args, "command": "nmap -p 99 10.10.10.10"}))
|
||||
assert sixth["status"] == "sync_required", sixth
|
||||
Reference in New Issue
Block a user