modify documents
This commit is contained in:
@@ -13,6 +13,7 @@ if str(SCRIPTS_DIR) not in sys.path:
|
||||
|
||||
from msvc_harness.config import ConfigError, load_config
|
||||
from msvc_harness.tdd_policy import CPP_SUFFIXES, evaluate_paths
|
||||
from hooks.validation_scope import EDIT_TOOL_NAMES, record_edit_scope
|
||||
|
||||
|
||||
DANGEROUS_PATTERNS = (
|
||||
@@ -29,15 +30,27 @@ DANGEROUS_PATTERNS = (
|
||||
),
|
||||
re.compile(r"\bDROP\s+TABLE\b", re.IGNORECASE),
|
||||
)
|
||||
EXECUTOR_OWNED_GIT = re.compile(
|
||||
r"\bgit(?:\.exe)?\b[^\r\n;&|]*\b(?:commit|push|switch|checkout|branch|tag|"
|
||||
r"merge|rebase|reset|cherry-pick|revert|stash|clean|update-ref|symbolic-ref|"
|
||||
r"worktree|fetch|pull)\b",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
PATCH_PATH = re.compile(
|
||||
r"^\*\*\* (?:(?:Add|Update|Delete) File:|Move to:) (?P<path>.+?)\s*$",
|
||||
re.MULTILINE,
|
||||
)
|
||||
|
||||
|
||||
class HookInputError(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
def _tool_input(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
value = payload.get("tool_input", {})
|
||||
return value if isinstance(value, dict) else {}
|
||||
value = payload.get("tool_input")
|
||||
if not isinstance(value, dict):
|
||||
raise HookInputError("hook tool_input must be an object")
|
||||
return value
|
||||
|
||||
|
||||
def _candidate_paths(payload: dict[str, Any], root: Path) -> list[Path]:
|
||||
@@ -57,7 +70,7 @@ def _candidate_paths(payload: dict[str, Any], root: Path) -> list[Path]:
|
||||
if isinstance(value, str) and value.strip():
|
||||
raw_paths.append(value.strip())
|
||||
|
||||
for key in ("patch", "input"):
|
||||
for key in ("command", "patch", "input"):
|
||||
value = tool_input.get(key)
|
||||
if isinstance(value, str):
|
||||
raw_paths.extend(match.group("path") for match in PATCH_PATH.finditer(value))
|
||||
@@ -71,20 +84,33 @@ def _candidate_paths(payload: dict[str, Any], root: Path) -> list[Path]:
|
||||
|
||||
def evaluate(payload: dict[str, Any], root: Path) -> str | None:
|
||||
"""Return a blocking reason, or None when the tool call is allowed."""
|
||||
tool_name = str(payload.get("tool_name", ""))
|
||||
tool_name = payload.get("tool_name")
|
||||
if not isinstance(tool_name, str) or not tool_name:
|
||||
raise HookInputError("hook tool_name must be a non-empty string")
|
||||
tool_input = _tool_input(payload)
|
||||
|
||||
if tool_name in {"Bash", "shell_command", "PowerShell"}:
|
||||
command = tool_input.get("command", "")
|
||||
if isinstance(command, str) and any(
|
||||
command = tool_input.get("command")
|
||||
if not isinstance(command, str):
|
||||
raise HookInputError("shell tool_input.command must be a string")
|
||||
if EXECUTOR_OWNED_GIT.search(command):
|
||||
return (
|
||||
"Harness-owned Git operation detected. The implementation session "
|
||||
"must leave branch, commit, push, and history operations to the Executor."
|
||||
)
|
||||
if any(
|
||||
pattern.search(command) for pattern in DANGEROUS_PATTERNS
|
||||
):
|
||||
return "위험한 명령어가 감지되어 실행을 차단했습니다."
|
||||
|
||||
if tool_name not in {"apply_patch", "Edit", "MultiEdit", "Write"}:
|
||||
if tool_name not in EDIT_TOOL_NAMES:
|
||||
return None
|
||||
|
||||
paths = _candidate_paths(payload, root)
|
||||
if not paths:
|
||||
raise HookInputError(
|
||||
f"{tool_name} input did not contain a path that can be checked"
|
||||
)
|
||||
try:
|
||||
config = load_config(root)
|
||||
except ConfigError as exc:
|
||||
@@ -98,19 +124,33 @@ def main() -> int:
|
||||
try:
|
||||
payload = json.load(sys.stdin)
|
||||
if not isinstance(payload, dict):
|
||||
raise TypeError("hook input must be a JSON object")
|
||||
raise HookInputError("hook input must be a JSON object")
|
||||
cwd = payload.get("cwd")
|
||||
if cwd is not None and not isinstance(cwd, str):
|
||||
raise TypeError("hook cwd must be a string")
|
||||
except (json.JSONDecodeError, TypeError) as exc:
|
||||
print(f"TDD GUARD: hook 입력을 해석하지 못해 검사를 건너뜁니다: {exc}", file=sys.stderr)
|
||||
return 0
|
||||
raise HookInputError("hook cwd must be a string")
|
||||
|
||||
root = Path(cwd or Path.cwd()).resolve()
|
||||
reason = evaluate(payload, root)
|
||||
except (
|
||||
json.JSONDecodeError,
|
||||
HookInputError,
|
||||
ConfigError,
|
||||
KeyError,
|
||||
ValueError,
|
||||
OSError,
|
||||
) as exc:
|
||||
print(f"TDD GUARD: hook input rejected: {exc}", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
root = Path(payload.get("cwd") or Path.cwd()).resolve()
|
||||
reason = evaluate(payload, root)
|
||||
if reason:
|
||||
print(reason, file=sys.stderr)
|
||||
return 2
|
||||
if payload["tool_name"] in EDIT_TOOL_NAMES:
|
||||
try:
|
||||
record_edit_scope(payload, root, _candidate_paths(payload, root))
|
||||
except (ValueError, OSError) as exc:
|
||||
print(f"TDD GUARD: edit scope could not be recorded: {exc}", file=sys.stderr)
|
||||
return 2
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user