fix(kvb-org-organize): harden the council hooks against sandboxed agents

- Move the council config to ~/.claude/agents-council/config.json, which the sandbox cannot write; the installer adds Edit(~/.claude/agents-council/**) to permissions.deny.
- Run every kvb hook as /usr/bin/python3 -I and rewrite old bare python3 entries.
- Refuse a council root that is a symlink, another uid's, or group or other writable, in watch, queue and radio.
- Run worktree_cleanup entries as argv without a shell in /, only for compose project names.
- queue block: tokenize every Bash command like bash (quotes, word-start comments, line continuations), and hold each held command to the Bash deny and ask rules of the managed, user and project settings; refuse a hold it cannot parse.
- radio: deliver fields on one line of at most 1000 characters, at most 10 rows.

Operator step after install: move ~/.config/agents-council/config.json to ~/.claude/agents-council/config.json.

MEDUSA acceptances (kvb-think-criticize 2026-11-06, skeptics A and B): ACP-SCAN-028 load_config (config outside the sandbox, Docker residual 093); CODEGEN-SCAN-004 and LLM06 run_cleanup and worktrees_command shell strings (operator config only); SAST-PATH-001 queue hold Popen (the caller's own command in its sandbox); PA-133 radio hook context (peer rows, clipped); JB-SCAN-641/616/2979 clip and MM-SCAN-058 replace (name matches).

🎯 Fix: 90 %
💥 Break: 5 %
💸 Cost: 100 %
⏱️ Time: 100 %
🛡️ Security: 100 %
💰 Value: ?
🏆 Score: 86
🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 %

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-06 13:26:59 +00:00
1 parent 6985c3a27e
commit 3843821674
11 files changed
+414 -54

No files matched your search

+5 -2
View File
@@ -41,8 +41,11 @@ not answer.
The chair keeps the system from running out instead of reacting once it has.
`${CLAUDE_SKILL_DIR}/scripts/watch.py` measures as hooks in every session, so
no agent probes the disk by hand; it reads thresholds, job patterns, cleanup
commands and cache directories from `~/.config/agents-council/config.json`
(keys in the script's docstring) and stays silent below the thresholds.
commands and cache directories from `~/.claude/agents-council/config.json`
(keys in the script's docstring; there because its hooks run outside the
sandbox, whose file rules keep it out of `~/.claude` unless a Docker daemon it
can reach mounts it, and the install denies agents' edits of that directory)
and stays silent below the thresholds.
- **Warn**: the chair and every session get the biggest consumers; the chair
plans a cleanup and grants no large slot.
+50 -16
View File
@@ -21,15 +21,24 @@ Usage:
Environment:
AGENTS_COUNCIL_ROOT: shared state directory; /tmp/agents-council unless set,
because every agent on the host must agree on it.
AGENTS_COUNCIL_CONFIG: config file; ~/.config/agents-council/config.json
unless set. Without a config file every command exits 0 and does nothing.
because every agent on the host must agree on it. Every command creates
it with mode 700; while it is a symlink, another uid's, or group or
other writable, a hook mode prints why and exits 0 doing nothing and
any other mode exits 2. queue.py and radio.py check it the same way.
AGENTS_COUNCIL_CONFIG: config file; ~/.claude/agents-council/config.json
unless set, a path the install denies to Claude Code's file tools and
the sandbox's file rules do not let it write, because the hooks run
its commands outside the sandbox; a Docker daemon the sandbox can
reach still writes it. Without a config file every command exits 0 and does
nothing.
Config keys, all required: disk, thresholds_gb {warn, stop, critical},
projection_minutes, interval_seconds, cache_interval_seconds, heavy_commands,
never_block (regexes), cleanup (shell commands), worktree_cleanup (shell
commands with {worktree}), worktrees_command (shell command printing the
running stacks' worktree paths, one per line), worktree_marker (path relative
never_block (regexes), cleanup (shell commands), worktree_cleanup (argv lists
with {project}, run without a shell in /, so no worktree file is read),
worktrees_command (shell command printing per running stack its worktree path
and compose project, tab-separated, one per line; a project that is not a
compose project name is skipped), worktree_marker (path relative
to a worktree that must exist), busy_processes (process names; a worktree
where one of them runs is skipped), cache_dirs, cache_budget_percent,
slot_need_gb {kind: GB}, notify (argv the alert text is appended to),
@@ -45,6 +54,7 @@ import json
import os
import re
import shlex
import stat
import subprocess
import sys
import time
@@ -54,9 +64,10 @@ ROOT = Path(os.environ.get("AGENTS_COUNCIL_ROOT", "/tmp/agents-council"))
CONFIG = Path(
os.environ.get(
"AGENTS_COUNCIL_CONFIG",
str(Path.home() / ".config" / "agents-council" / "config.json"),
str(Path.home() / ".claude" / "agents-council" / "config.json"),
)
)
PROJECT = re.compile(r"[a-z0-9][a-z0-9_-]*")
STAGES = ("ok", "warn", "stop", "critical")
STATE = ROOT / "watch.json"
SILENCE = ROOT / "radio-silence.json"
@@ -72,6 +83,22 @@ LOCK_TTL = 1800
GB = 1e9
def guard_root(hook: bool) -> int | None:
try:
ROOT.mkdir(mode=0o700, parents=True, exist_ok=True)
except FileExistsError:
pass
st = ROOT.lstat()
if stat.S_ISDIR(st.st_mode) and st.st_uid == os.getuid() and not st.st_mode & 0o022:
return None
print(
f"agents council: {ROOT} must be a directory of uid {os.getuid()} that group and others cannot write; "
"the council hooks stay off until it is",
file=sys.stdout if hook else sys.stderr,
)
return 0 if hook else 2
def load_config() -> dict | None:
if not CONFIG.is_file():
return None
@@ -375,15 +402,19 @@ def trim_caches(cfg: dict) -> float:
return freed / GB
def running_worktrees(cfg: dict) -> list[str]:
def running_worktrees(cfg: dict) -> list[tuple[str, str]]:
try:
out = subprocess.run(
cfg["worktrees_command"], shell=True, capture_output=True, text=True, timeout=60, check=False
).stdout
except subprocess.TimeoutExpired:
return []
trees = sorted({line.strip() for line in out.splitlines() if line.strip()})
return [t for t in trees if os.path.exists(os.path.join(t, cfg["worktree_marker"]))]
pairs = {tuple(line.split("\t")) for line in out.splitlines()}
return sorted(
(tree, project)
for tree, project in (pair for pair in pairs if len(pair) == 2)
if PROJECT.fullmatch(project) and os.path.exists(os.path.join(tree, cfg["worktree_marker"]))
)
def busy(tree: str, names: list[str]) -> bool:
@@ -401,18 +432,18 @@ def busy(tree: str, names: list[str]) -> bool:
def run_cleanup(cfg: dict) -> list[str]:
commands = list(cfg["cleanup"])
for tree in running_worktrees(cfg):
commands = [(command, True) for command in cfg["cleanup"]]
for tree, project in running_worktrees(cfg):
if busy(tree, cfg["busy_processes"]):
continue
commands += [t.format(worktree=shlex.quote(tree)) for t in cfg["worktree_cleanup"]]
commands += [([arg.format(project=project) for arg in argv], False) for argv in cfg["worktree_cleanup"]]
results = []
for command in commands:
for command, shell in commands:
try:
code = subprocess.run(command, shell=True, capture_output=True, timeout=900, check=False).returncode
code = subprocess.run(command, shell=shell, cwd="/", capture_output=True, timeout=900, check=False).returncode
except subprocess.TimeoutExpired:
code = "timeout"
results.append(f"{'ok' if code == 0 else f'failed ({code})'}: {command}")
results.append(f"{'ok' if code == 0 else f'failed ({code})'}: {command if shell else shlex.join(command)}")
return results
@@ -468,6 +499,9 @@ def main() -> int:
mode.add_argument("--full", action="store_true")
sub.add_parser("status")
args = parser.parse_args()
refused = guard_root(hook=args.cmd in ("watch", "block"))
if refused is not None:
return refused
cfg = load_config()
if cfg is None:
return 0
+1 -1
View File
@@ -17,7 +17,7 @@ queue.py hold --kind <kind> --task <register task> -- <command>
Start it in the background (`run_in_background` or equivalent) and end the
turn: hold waits, runs the command on its grant and exits with its code.
- `<kind>`: a key of `slot_need_gb` in `~/.config/agents-council/config.json`.
- `<kind>`: a key of `slot_need_gb` in `~/.claude/agents-council/config.json`.
- `<register task>`: your open `kvb-org-rasci` task; its rank places you.
- `--hotfix <slug>`: the job proves the register row `hotfix/<slug>`
(`kvb-git-hotfix`) and goes first.
+145 -6
View File
@@ -21,9 +21,11 @@ Environment:
~/.local/state/agents-council/queue-history.jsonl, and reading merges
both by row id.
AGENTS_COUNCIL_CONFIG: the config of the kvb-org-organize watch.py;
~/.config/agents-council/config.json unless set.
~/.claude/agents-council/config.json unless set.
CLAUDE_CODE_SESSION_ID: the caller's session, taken over --session; other
runtimes pass --session.
CLAUDE_PROJECT_DIR: block reads the project's .claude/settings.json and
settings.local.json there, next to the managed and user settings.
Args:
hold: enqueue CMD, then every POLL_SECONDS evaluate the whole queue under
@@ -48,8 +50,13 @@ Args:
history: a markdown table per command signature and one per kind of what
the last 30 days of history teach, with each slot limit's source.
cancel ID: remove the caller's waiting entry ID; its hold exits 3.
block: read the hook payload on stdin; with queue_enforce true in the
config, refuse a command matching heavy_commands or `gate.py run|hold`
block: read the hook payload on stdin. The permission matcher sees only
`queue.py hold`, so every hold in the command is parsed like hold
itself, and a held command any of whose word-started tails meets a
Bash deny rule of the settings is refused, one meeting an ask rule
asks the operator, with or without queue_enforce; a hold that cannot
be parsed or checked is refused. With queue_enforce true in the config,
refuse a command matching heavy_commands or `gate.py run|hold`
unless it matches never_block or runs `queue.py hold`.
Config keys: slot_limits {kind: N}, optional, caps the learned slot limit of a
@@ -67,7 +74,9 @@ Exit codes:
reached hold.
list, history: 0 done, 2 config error.
cancel: 0 removed, 2 no waiting entry of the caller with that id.
block: 0 allow, 2 refuse the tool call; every error allows.
block: 0 allow, or ask when it prints an ask decision, or a council root
that watch.py refuses; 2 refuse the tool call; every other error allows.
list, history, cancel, hold: 2 on a council root that watch.py refuses.
"""
from __future__ import annotations
@@ -108,6 +117,11 @@ CANCELLED = 3
FORWARDED = (signal.SIGINT, signal.SIGTERM)
GATE = re.compile(r"\bgate\.py\s+(run|hold)\b")
THROUGH_QUEUE = re.compile(r"\bqueue\.py\s+hold\b")
BASH_RULE = re.compile(r"Bash(?:\((.*)\))?", re.DOTALL)
RULE_PART = re.compile(r"( \*|\*)")
SEPARATOR = re.compile(r"[();<>|&]+")
WORD_BREAK = " \t\n;&|()<>"
MANAGED = Path("/etc/claude-code")
HOTFIX = "hotfix/"
@@ -573,8 +587,126 @@ def cmd_cancel(args: argparse.Namespace) -> int:
return 0
def settings_files(project: Path) -> list[Path]:
user = Path.home() / ".claude"
return [
MANAGED / "managed-settings.json",
*sorted((MANAGED / "managed-settings.d").glob("*.json")),
user / "settings.json",
user / "settings.local.json",
project / ".claude" / "settings.json",
project / ".claude" / "settings.local.json",
]
def bash_rule(rule: str) -> re.Pattern | None:
match = BASH_RULE.fullmatch(rule)
if match is None:
return None
body = match.group(1) or "*"
if body.endswith(":*"):
body = body[:-2] + " *"
return re.compile(
"".join(
r"(?:\s.*)?" if part == " *" else ".*" if part == "*" else re.escape(part)
for part in RULE_PART.split(body)
),
re.DOTALL,
)
def permission(commands: list[str], project: Path) -> str | None:
settings = []
for path in settings_files(project):
with contextlib.suppress(FileNotFoundError):
settings.append(json.loads(path.read_text(encoding="utf-8")).get("permissions") or {})
for decision in ("deny", "ask"):
rules = [bash_rule(rule) for permissions in settings for rule in permissions.get(decision) or []]
if any(rule and rule.fullmatch(command) for rule in rules for command in commands):
return decision
return None
def uncommented(command: str) -> str:
kept: list[str] = []
quote = ""
escaped = skipping = False
word_start = True
for char in command:
if skipping and char != "\n":
continue
skipping = False
if escaped:
escaped = False
elif quote == "'":
quote = "" if char == "'" else quote
elif char == "\\":
escaped = True
elif quote == '"':
quote = "" if char == '"' else quote
elif char in "'\"":
quote = char
elif char == "#" and word_start:
skipping = True
continue
kept.append(char)
word_start = not quote and not escaped and char in WORD_BREAK
return "".join(kept)
def segments(command: str) -> list[list[str]]:
# bash drops a backslash-newline between words and starts a comment only at a word start; shlex does neither
lexer = shlex.shlex(uncommented(command.replace("\\\n", "")), posix=True, punctuation_chars=True)
lexer.whitespace_split = True
lexer.commenters = ""
parts: list[list[str]] = [[]]
for token in lexer:
if SEPARATOR.fullmatch(token):
parts.append([])
else:
parts[-1].append(token)
return parts
def held_commands(command: str) -> list[list[str]]:
return [
cli().parse_args(part[at + 1 :]).command
for part in segments(command)
for at, token in enumerate(part)
if token.endswith("queue.py") and part[at + 1 : at + 2] == ["hold"]
]
def tails(argv: list[str]) -> list[str]:
return [shlex.join(argv[at:]) for at, word in enumerate(argv) if not word.startswith("-")]
def cmd_block() -> int:
payload = json.load(sys.stdin)
raw = (payload.get("tool_input") or {}).get("command") or ""
try:
holds = held_commands(raw)
commands = [t for argv in holds for t in tails(argv)]
decision = permission(commands, Path(os.environ["CLAUDE_PROJECT_DIR"])) if commands else None
except (ValueError, KeyError, OSError, SystemExit) as error:
if "queue" in raw:
print(
f"kvb-org-queue: cannot check the held command against the settings ({error!r}); "
"fix the settings or run the command outside queue.py hold.",
file=sys.stderr,
)
return 2
holds, decision = [], None
shown = " ; ".join(shlex.join(argv) for argv in holds)
if decision == "deny":
print(f"kvb-org-queue: the held command `{shown}` meets a deny rule of the settings.", file=sys.stderr)
return 2
if decision == "ask":
reason = f"kvb-org-queue: the held command `{shown}` meets an ask rule of the settings"
print(json.dumps({"hookSpecificOutput": {
"hookEventName": "PreToolUse", "permissionDecision": "ask", "permissionDecisionReason": reason,
}}))
return 0
cfg = watch.load_config()
if cfg is None or cfg.get("queue_enforce") is not True:
return 0
@@ -591,7 +723,7 @@ def cmd_block() -> int:
return 0
def main() -> int:
def cli() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
sub = parser.add_subparsers(dest="cmd", required=True)
hold = sub.add_parser("hold")
@@ -607,7 +739,14 @@ def main() -> int:
cancel.add_argument("id")
cancel.add_argument("--session")
sub.add_parser("block")
args = parser.parse_args()
return parser
def main() -> int:
args = cli().parse_args()
refused = watch.guard_root(hook=args.cmd == "block")
if refused is not None:
return refused
if args.cmd == "block":
try:
return cmd_block()
+28 -14
View File
@@ -6,7 +6,7 @@ Environment:
/tmp/agents-council unless set. Rows live in <root>/news.jsonl under
<root>/news.lock, the store the old news.py reads and writes too.
AGENTS_COUNCIL_CONFIG: config file whose "notify" argv tells the operator
of a mayday; ~/.config/agents-council/config.json unless set.
of a mayday; ~/.claude/agents-council/config.json unless set.
CLAUDE_CODE_SESSION_ID: the calling session; hooks read the payload's
session_id first, other runtimes pass --session.
@@ -59,7 +59,7 @@ from types import ModuleType
# Every agent on the host must agree on one council directory and one config without being told.
ROOT = Path(os.environ.get("AGENTS_COUNCIL_ROOT", "/tmp/agents-council"))
CONFIG = Path(
os.environ.get("AGENTS_COUNCIL_CONFIG", str(Path.home() / ".config" / "agents-council" / "config.json"))
os.environ.get("AGENTS_COUNCIL_CONFIG", str(Path.home() / ".claude" / "agents-council" / "config.json"))
)
NEWS = ROOT / "news.jsonl"
LOCK = ROOT / "news.lock"
@@ -68,11 +68,12 @@ STATE = ROOT / "radio-seen"
LISTEN = ROOT / "radio-listen"
SILENCE = ROOT / "radio-silence.json"
NOTIFIED = ROOT / "radio-notified"
COUNCIL = Path(__file__).resolve().parents[2] / "kvb-org-organize" / "scripts" / "council.py"
ORGANIZE = Path(__file__).resolve().parents[2] / "kvb-org-organize" / "scripts"
CLASSES = ("broadcast", "direct", "security", "pan-pan", "mayday")
URGENT = ("mayday", "pan-pan", "security")
WINDOW = 86400
SHOWN = 10
CLIP = 1000
KEPT = 500
SILENCE_SECONDS = 7200
NOTIFY_SECONDS = 10
@@ -86,13 +87,17 @@ def fail(message: str) -> None:
@functools.cache
def council() -> ModuleType:
spec = importlib.util.spec_from_file_location("council", COUNCIL)
def organize(name: str) -> ModuleType:
spec = importlib.util.spec_from_file_location(f"kvb_radio_{name}", ORGANIZE / f"{name}.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def council() -> ModuleType:
return organize("council")
def name_of(sid: str) -> str:
return council().live_sessions().get(sid) or sid
@@ -135,9 +140,19 @@ def clock(ts: float) -> str:
return datetime.datetime.fromtimestamp(ts).strftime("%H:%M")
def clip(value: object) -> str:
return " ".join(str(value).split())[:CLIP]
def line(row: dict) -> str:
target = f" to {', '.join(row['to'])}" if row.get("to") else ""
return f"RADIO {kind(row).upper()} {clock(row['ts'])} {row.get('from')}{target}: {row.get('text')}"
target = f" to {', '.join(clip(name) for name in row['to'])}" if row.get("to") else ""
return f"RADIO {kind(row).upper()} {clock(row['ts'])} {clip(row.get('from'))}{target}: {clip(row.get('text'))}"
def capped(rows: list[dict]) -> list[str]:
picked = sorted(rows, key=lambda row: (rank(row), -row["ts"]))[:SHOWN]
more = [f"RADIO {len(rows) - SHOWN} more items: radio.py show --all"] if len(rows) > SHOWN else []
return [line(row) for row in ordered(picked)] + more
def for_me(row: dict, name: str) -> bool:
@@ -204,7 +219,7 @@ def silence() -> dict | None:
def silence_line(active: dict) -> str:
return (
f"MAYDAY ACTIVE since {clock(active['since'])} by {active['by']}: {active['text']}; "
f"MAYDAY ACTIVE since {clock(active['since'])} by {clip(active['by'])}: {clip(active['text'])}; "
"no new heavy jobs until radio.py clear"
)
@@ -333,11 +348,7 @@ def cmd_hook(_: argparse.Namespace) -> int:
name = name_of(sid)
with session(sid) as state:
rows = unseen(sid, name)
fresh = [row for row in rows if not delivered(row, state)]
picked = sorted(fresh, key=lambda row: (rank(row), -row["ts"]))[:SHOWN]
out += [line(row) for row in ordered(picked)]
if len(fresh) > SHOWN:
out.append(f"RADIO {len(fresh) - SHOWN} more items: radio.py show --all")
out += capped([row for row in rows if not delivered(row, state)])
if rows:
SEEN.mkdir(parents=True, exist_ok=True)
(SEEN / sid).write_text(str(max(row["ts"] for row in rows)), encoding="utf-8")
@@ -366,7 +377,7 @@ def cmd_tool(_: argparse.Namespace) -> int:
state["urgent"] = max(row["ts"] for row in rows)
save(sid, state)
if rows:
context = "\n".join(line(row) for row in rows)
context = "\n".join(capped(rows))
print(json.dumps({"hookSpecificOutput": {"hookEventName": "PostToolBatch", "additionalContext": context}}))
if any(kind(row) == "mayday" for row in rows):
notify_once(silence())
@@ -466,6 +477,9 @@ def main() -> int:
p.add_argument("--session")
p.set_defaults(func=cmd_show)
args = parser.parse_args()
refused = organize("watch").guard_root(hook=args.command in ("hook", "tool", "listen"))
if refused is not None:
return refused
return args.func(args)