feat(kvb-check-gate): W5 gate receipts, hooks and queue after-wait

gates.py installs hooks that check the merge level before a push without writing git config, writes receipts from green queue gates, and the queue gains the after-wait and rank. Requirement 003 strikes the hook-enforcement criterion: agents keep --no-verify, hooks bind humans and CI. Committed below the bar on the operator's word 2026-11-12 (one gate after the merge): test_gates 15, test_queue 63 and test_watch 46 green, security 100 %, break risk 6 % for the live queue and hook change.

🎯 Fix: 90 %
💥 Break: 6 %
💸 Cost: 100 %
⏱️ Time: 100 %
🛡️ Security: 100 %
🏆 Score: 85
🚦 Gate: ⛔ break 6 % > 5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-12 16:16:22 +00:00
1 parent fd8fc08603
commit 8a44d504b5
9 files changed
+1035 -99

No files matched your search

+3 -2
View File
@@ -35,7 +35,8 @@ Light runs (no Docker, about a minute) self-admit with
Every 20 s each waiting hold evaluates the queue under one lock:
1. Hotfix jobs first, then the register rank (`rasci.py show`), one place
earlier per whole hour waited, then arrival.
earlier per whole hour waited, then arrival; an open `after` task holds a
job.
2. In that order, a job is blocked at disk stage stop or critical, at warn
when it needs disk, at memory stage stop, during a radio-silence mayday,
while its kind's slots are taken, or while its workdir runs another job;
@@ -75,7 +76,7 @@ queue.py cancel <id>
```
`list` shows each job's position, ETA and blocking reason, `history` the
learned values, `cancel` removes your own waiting job (its hold exits 3).
learned values, `cancel` removes your own waiting job.
## Rules
+71 -6
View File
@@ -2,7 +2,8 @@
"""Queue the heavy jobs of parallel agents and grant them in a fixed order, without messages.
Usage:
queue.py hold --kind KIND --task TASK [--need-gb N] [--hotfix SLUG] [--session ID] -- CMD...
queue.py hold --kind KIND --task TASK [--need-gb N] [--hotfix SLUG] [--session ID]
[--receipt LEVEL --log PATH] -- CMD...
queue.py list
queue.py history
queue.py cancel ID [--session ID]
@@ -40,12 +41,21 @@ Args:
--kind: a key of slot_need_gb in the config.
--task: an open task of the kvb-org-rasci register; its rank orders
the queue, and the operator's agent order (`rasci.py agents
order`) of the holding session breaks ties.
order`) of the holding session breaks ties. While one of its
`after` tasks in the register is neither done nor stopped, the
entry waits with `waits for <task>` (the reason names the
`rasci.py set --drop-after` that lifts it), after every other
entry, and reserves nothing.
--need-gb: disk the job takes, in GB; when omitted the need learned
for the command's signature, else slot_need_gb[kind].
--hotfix: the slug of the register row hotfix/<slug> the job proves;
hotfix jobs go first.
--session: the caller's session for runtimes other than Claude Code.
--receipt: a gate level of kvb-check-gate's gates.py above none; the
command's stdout and stderr go to --log instead, and when it
exits 0 on the tree it started on (gates.py tree of the workdir
before and after) hold records a receipt for that tree, level
and log, with INFINITO_TEST_GPU of its environment.
list: a header, then one line per entry: position (run for active jobs),
id, status, kind, task, need GB, minutes waited, ETA minutes (until a
running job ends or a waiting one starts, from learned medians; - while
@@ -129,6 +139,7 @@ SIBLINGS = {
"history": Path(__file__).resolve().parent / "history.py",
"nofollow": SKILLS / "kvb-org-organize" / "scripts" / "nofollow.py",
}
TOOLS = {"gates": SKILLS / "kvb-check-gate" / "scripts" / "gates.py"}
HOST_SKILLS = Path.home() / ".agents" / "skills"
EXE = os.path.realpath(sys.executable)
CODE_FILES = tuple(path.relative_to(SKILLS) for path in (Path(__file__).resolve(), *SIBLINGS.values()))
@@ -152,7 +163,7 @@ HOTFIX = "hotfix/"
@functools.cache
def sibling(name: str) -> ModuleType:
spec = importlib.util.spec_from_file_location(f"kvb_queue_{name}", SIBLINGS[name])
spec = importlib.util.spec_from_file_location(f"kvb_queue_{name}", {**SIBLINGS, **TOOLS}[name])
loaded = importlib.util.module_from_spec(spec)
spec.loader.exec_module(loaded)
return loaded
@@ -536,6 +547,22 @@ def agent_ranks() -> dict[str, int]:
return {name: index for index, name in enumerate(sibling("rasci").agent_order())}
def after_waits(entries: list[dict]) -> dict[str, str]:
rasci = sibling("rasci")
register = rasci.load()
waits = {}
for entry in entries:
task = entry["task"]
ahead = [
other for other in register.get(task, {}).get("after", [])
if other in register and register[other].get("status") not in rasci.ENDED
]
if entry["status"] == "waiting" and ahead:
lift = shlex.join(["rasci.py", "set", task, "--drop-after", ahead[0]])
waits[entry["id"]] = f"waits for {ahead[0]}, an after task of {task} not yet done or stopped; {lift} lifts it"
return waits
def stage(cfg: dict, free_gb: float, now: float) -> str:
state = watch.read_json(watch.STATE)
return state["stage"] if "stage" in state else watch.stage_for(free_gb, [], cfg, now)
@@ -569,12 +596,13 @@ def decide(cfg: dict, entries: list[dict], free_gb: float, now: float, signature
for entry in entries:
share = footprint(entry, signatures, kinds)
footprints[entry["id"]] = dict(budget) if share is None else share
waits = after_waits(entries)
return evaluate(
entries, free_gb, cfg["thresholds_gb"]["stop"], stage(cfg, free_gb, now),
[e for e in entries if e["id"] not in waits], free_gb, cfg["thresholds_gb"]["stop"], stage(cfg, free_gb, now),
watch.memory_stage(watch.memory(), cfg), silenced(now), slots, footprints, budget,
register_ranks(), agent_ranks(), loaded,
set(cfg.get("exclusive_kinds", [])), set(cfg.get("light_kinds", [])), now,
)
) | waits
def load_reason(limits: dict, cpu: float, mem: float, ram_share: float) -> str | None:
@@ -741,6 +769,36 @@ def record(job: dict, start: float, code: int, samples: list[dict[str, float]],
keep(history.row(job, start, time.time(), code, samples, concurrent))
def gated(args: argparse.Namespace, here: str) -> tuple[ModuleType | None, int | None]:
if args.receipt is None:
if args.log is not None:
fail("--log goes with --receipt")
return None, None
gates = sibling("gates")
if args.receipt not in gates.LEVELS[1:] or args.log is None:
fail(f"--receipt takes one of {', '.join(gates.LEVELS[1:])} and needs --log")
gates.root(here)
try:
return gates, os.open(args.log, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW | os.O_CLOEXEC, 0o600)
except OSError as error:
fail(f"cannot write the log {args.log}: {error.strerror}")
def start_tree(gates: ModuleType | None, here: str) -> str | None:
return None if gates is None else gates.worktree_tree(here)
def keep_receipt(gates: ModuleType | None, args: argparse.Namespace, here: str, before: str | None, code: int) -> None:
if gates is None or code != 0:
return
after = gates.worktree_tree(here)
if after != before:
print(f"queue: the command changed the tree from {before} to {after}; no receipt", file=sys.stderr)
return
gates.add_receipt(gates.root(here), before, args.receipt, os.path.abspath(args.log))
print(f"queue: receipt {args.receipt} for tree {before}", file=sys.stderr)
def cmd_hold(args: argparse.Namespace) -> int:
cfg = config()
if args.kind not in cfg["slot_need_gb"]:
@@ -751,6 +809,7 @@ def cmd_hold(args: argparse.Namespace) -> int:
if args.hotfix is not None and HOTFIX + args.hotfix not in rasci.load():
fail(f"the register has no row {HOTFIX}{args.hotfix}")
here = workdir()
gates, out = gated(args, here)
signature = qualified(here, args.command)
signatures, _ = learned(cfg, time.time())
need = args.need_gb
@@ -788,9 +847,12 @@ def cmd_hold(args: argparse.Namespace) -> int:
if caught:
return 128 + caught[0]
samples = [measure(cfg)]
before = start_tree(gates, here)
start = time.time()
try:
child = subprocess.Popen(args.command, start_new_session=True, pass_fds=(lock_fd,))
child = subprocess.Popen(
args.command, start_new_session=True, pass_fds=(lock_fd,), stdout=out, stderr=out
)
except OSError as error:
print(f"queue: cannot start {args.command[0]}: {error.strerror}", file=sys.stderr)
return 2
@@ -802,6 +864,7 @@ def cmd_hold(args: argparse.Namespace) -> int:
job = {"id": f"{entry['id']}-{time.time_ns()}", "kind": args.kind, "signature": signature,
"workdir": entry["workdir"]}
record(job, start, code, samples, concurrent)
keep_receipt(gates, args, here, before, code)
return code
finally:
leave(entry["id"])
@@ -968,6 +1031,8 @@ def cli() -> argparse.ArgumentParser:
hold.add_argument("--need-gb", type=float)
hold.add_argument("--hotfix")
hold.add_argument("--session")
hold.add_argument("--receipt")
hold.add_argument("--log")
hold.add_argument("command", nargs="+")
sub.add_parser("list")
sub.add_parser("history")