Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/client/python/agentguard/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _cmd_check(args: argparse.Namespace) -> int:
except PolicyError as exc:
print(str(exc), file=sys.stderr)
return 1
print(f"ok: {target} ({len(rules)} rules)")
print(f"ok: {target.as_posix()} ({len(rules)} rules)")
return 0


Expand All @@ -121,11 +121,11 @@ def _check_rules_file(path: Path) -> tuple[bool, int]:

_, report = parse_legacy_rules(source)
if report.ok:
print(f"ok: {path} ({report.rule_count} rule block(s))")
print(f"ok: {path.as_posix()} ({report.rule_count} rule block(s))")
return True, report.rule_count

for error in report.errors:
print(f"error: {path}: {error['message']}", file=sys.stderr)
print(f"error: {path.as_posix()}: {error['message']}", file=sys.stderr)
return False, report.rule_count


Expand Down
19 changes: 12 additions & 7 deletions src/client/python/agentguard/rules/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@ def load_rules_file(path: str | Path) -> list[PolicyRule]:
p = Path(path)
if not p.exists():
raise PolicyError(f"rule file not found: {p}")
if p.suffix.lower() == ".rules":
try:
parsed, report = parse_legacy_rules(p.read_text(encoding="utf-8"))
except OSError as exc:
raise PolicyError(f"cannot read rule file {p}: {exc}") from exc
try:
text = p.read_text(encoding="utf-8")
except OSError as exc:
raise PolicyError(f"cannot read rule file {p}: {exc}") from exc
# `.rules` is the legacy text-DSL extension, but some callers write plain
# JSON rule lists to a `.rules`-suffixed path; sniff the content rather
# than trusting the suffix so both work.
looks_like_json = text.lstrip()[:1] in ("[", "{")
if p.suffix.lower() == ".rules" and not looks_like_json:
parsed, report = parse_legacy_rules(text)
if not report.ok:
raise PolicyError(f"cannot parse rule file {p}: {report.errors[0]['message']}")
return [PolicyRule.from_dict(rule.to_dict()) for rule in parsed]
try:
data = json.loads(p.read_text(encoding="utf-8"))
except (OSError, json.JSONDecodeError) as exc:
data = json.loads(text)
except json.JSONDecodeError as exc:
raise PolicyError(f"cannot read rule file {p}: {exc}") from exc
return _coerce_rules(data)

Expand Down
Loading
Loading