Merge t3/enforce-orchestration-meter into spec-reconciliation: one before-tool path (worker exempt, contract guard, delegation guard, meter), routing decision read from the meter, retry-of on routing.retry_choice, ledger kind names. done: merge / next: tests on the merged tree, record parts

This commit is contained in:
swaits committed 2026-11-23 15:10:44 -07:00
commit c5df3068ce
36 files changed
+3567 -118

No files matched your search

+52
View File
@@ -336,6 +336,58 @@ worker).
- `sisi delegate --brief FILE …` still runs a worker through its own CLI when the host has no
delegation of its own.
**Enforcement** (`sisi/orchestration.py`, `sisi/fallback.py`; host tool names in `canon/orchestration-tools.json`).
The before- and after-tool hooks run for every tool, not only the shell, and keep a per-session,
per-turn meter in the scratch store (`sessions/<id>.meter.json`, under a file lock; every counted event is
also appended to `orchestration.jsonl`). It meters main threads only: a session the host marks as a subagent,
one whose first prompt opens with the brief template's first line, and one started by `sisi delegate` are
workers and are never counted or refused; neither is anything refused when no usable worker other than the
session's own model exists. Every entry point fails open.
- **Direct-work budget.** Every tool call that is not a delegation, the question dialog, a planning or to-do
tool, a message or a `sisi` command counts, with the characters of its result. Past `orchestrate.direct_calls`
(6) or `orchestrate.direct_chars` (30000) the next counted call is refused with at most 12 lines: the count,
rule 6, the router's pick for reading and for building (taken from the routing code, with its mechanism
line) and two ways on: delegate with a brief, or `sisi unit direct "<reason>"`. A specific reason (a generic
one is refused) extends the turn by `orchestrate.extension` (10) calls and the matching share of characters;
each extension and its reason is stored and shown by `sisi status`. The turn starts again at each prompt (a
host's notice about a background worker is not a prompt) and when the gate lets the turn end.
- **Delegation guard.** A delegation that names no model, or the session's own, is refused with the router's
pick; an allowed one is checked with `brief.check` (advice only) and written to the ledger as a pending run
(worker, project, session, start), completed with minutes and tokens when the result reports them, or, for an
async launch, when the host's completion notice arrives as a prompt. The gate asks once per set of finished,
unjudged runs for the judgements.
- **The router is asked first.** `sisi route` writes a routing decision for the session (kind, difficulty
guess, pick, time). The first delegation with no decision inside `orchestrate.route_window_min` (15) is
refused once; afterwards the pending run carries the kind and difficulty, so `sisi delegate judge ID` needs
only the outcome and the check. A worker other than the router's pick is allowed and recorded
(`chosen_against_route`); `sisi workers` shows how often and how those turned out (too few under five).
`sisi route --retry-of ID` keeps the same worker with an instruction to improve the brief after the first
rejection and moves to a stronger one (judged record, else benchmark standing, else host-native) after a
second.
- **Repeat failure.** The fourth identical failing shell command (whitespace-normalised) with no file changed
in between is refused once (rule 5).
- **Unavailable workers.** A limit, quota, login or connection failure arriving as a delegation result or as a
host notice is classified (`workers.classify_failure`), the worker's provider (or just the model) is marked
down until the reset time the message names, its pending run is marked `superseded` (never asked to be
judged), and the agent gets at most six lines: the router's next worker for the same kind of work, to fall
back now with the same brief, not to ask and not to report it (worker availability is internal, SPEC 9.7 and
17), and to cancel the failed task. A question-dialog call right after is refused once; ending the turn with
no fallback started is sent back (twice at most); a final message naming the failed worker or the failure is
sent back once. All state-based. The person hears of it only when nothing usable is left or the outcome or
its timing changes for them.
- **New workers.** A model discovery finds that the previous one did not have is named once at session start
as unproven (low-stakes work first, checked more carefully); never one whose id marks a tier that trains on
its prompts.
- **The record.** The unit record gains an `orchestration` part, and the scorecard's parallelism dimension the
same values, each class `measured`: direct calls and characters, refusals, extensions with reasons,
delegations started, judged and unjudged, and the share of tool calls delegated. `sisi status` prints it for
the unit in progress too. `sisi rule 6` lists what is enforced.
- **Hosts.** Claude Code: the meter can refuse (observed live). Codex, Gemini and Cursor have before-tool
events and are wired for every tool; the refusal is exit status 2 with the reason as the host documents it,
not run live here. Copilot only counts. Hosts with no before-tool hook (Qwen, Pi, OpenCode and the
rulebook-only tools) cannot run the meter; `sisi doctor` says which.
The owner's hand-written orchestrator (`orch.py`, its ledger, and the section of his Claude
instructions) is replaced by this. Its ledger is merged into the user store with a row count
check, then it is removed.
+17
View File
@@ -19,6 +19,23 @@ atomically, so a running tool never sees a half-written copy: a process that has
keeps running the release it started with. `sisi doctor` shows which copy is live and
whether the source folder is ahead of it.
## The live orchestration check
Before any release that touches the hooks or the rulebook, run
python3 tools/live_orchestration_check.py
It starts the host's command-line tool headless with its least expensive model in two small temporary
projects, with the hooks from this checkout and a temporary `SISI_DATA` and `SISI_STATE` (the real sisi data is
never read or written; the script asserts that it did not change). Scenario 1 asserts that the meter refused a
tool call with its message, that the session then ran `sisi route` and delegated with a named model or recorded
a reason with `sisi unit direct`, and that the ledger or events hold the matching record. Scenario 2 uses a fake
provider that always answers with a quota error and a reset time: the provider must be marked down until that
time, the session must go on with another worker, never call the question dialog and never mention the
failure in its final message. It costs a few cents and needs the host's login, so it is not part of the unit
suite; synthetic hook input is what let an earlier release ship broken. If the agent ignores a refusal or the
host does not honour it, that is a finding: fix the hook output, do not weaken the assertion.
## Make a release
Run from the clone (a release is made from the running launcher's folder):
+23 -3
View File
@@ -29,13 +29,33 @@ between each tool's hook JSON and the gate lives in `sisi/hookio.py`; the adapte
skills shipped under `canon/skills` into `~/.agents/skills`; `sisi doctor` reports the linked
state. Hooks fail open: any error lets the agent finish. Where a tool's hook input has no transcript path, the gate falls back to its pre-attribution behaviour (tree-change based, with no per-session edit attribution). Shell commands are attributed by measurement, not by their text, wherever the tool has confirmed before/after tool events (`sisi watch` takes a stat-only tree snapshot before and after the command and attributes the paths that changed to the session; provably read-only commands skip the pair); tools without confirmed before/after tool events keep the command-text classifier described in [docs/design.md](design.md).
## The orchestration meter, per tool
The before- and after-tool hooks count the main thread's direct tool calls and refuse past a budget (rule 6,
[design.md](design.md) section 9). `sisi doctor` prints what it can do in each tool:
| tool | meter | evidence |
|---|---|---|
| Claude Code | counts and refuses every tool (permission-deny output), delegation guard, fallback | observed live (`tools/live_orchestration_check.py`) |
| Codex CLI | counts and can refuse every tool (exit status 2 with the reason) | the host's hook documentation; not run live here |
| Gemini CLI | counts and can refuse every tool (exit status 2) | documentation; not run live |
| Cursor | counts and can refuse every tool (exit status 2) | documentation; not run live |
| GitHub Copilot CLI | only counts: its refusal contract is not confirmed | not run live |
| Qwen, Pi, OpenCode, Muse, Grok, Crush and the rulebook-only tools | cannot run: no before-tool hook is wired | see each section |
Host tool names (delegation, question dialog, planning, messaging) are data in
`canon/orchestration-tools.json`; a tool named nowhere there still counts. Only Claude Code's delegation tool is
named for the guard and the fallback; elsewhere delegation goes through `sisi delegate`, which is exempt.
## Claude Code
Fully wired, as before: an `@import` line in `~/.claude/CLAUDE.md`, `SessionStart` → `sisi tick`,
`UserPromptSubmit` → `sisi prompt` (the matching recipe and lessons for the human's request, as
added context; silent when nothing strictly matches, and never able to block a turn),
`Stop`/`SubagentStop` → `sisi gate` (blocking), and `PreToolUse`/`PostToolUse` with matcher `Bash` →
`sisi watch` (before/after tree measurement) in `~/.claude/settings.json`. The gate also enforces
`Stop`/`SubagentStop` → `sisi gate` (blocking), and `PreToolUse`/`PostToolUse`/`PostToolUseFailure` with no
matcher (every tool) → `sisi watch` (the orchestration meter, and before/after tree measurement for shell
commands) in `~/.claude/settings.json`. An installation from an earlier release (shell-only matchers) is
changed by `sisi install` and by `sisi update`, span-preserving; `sisi uninstall` restores the original bytes. The gate also enforces
the question-dialog rule here: `AskUserQuestion` exists and the transcript is readable, so ending a
turn by asking in reply text without using the dialog is sent back once. Evidence: the hooks
reference documents `UserPromptSubmit` and its added-context output. Nothing for you to do.
@@ -54,7 +74,7 @@ Rulebook symlink at `~/.codex/AGENTS.md` (or `$CODEX_HOME/AGENTS.md`), plus hook
`~/.codex/hooks.json`: `SessionStart` → tick, `UserPromptSubmit` → prompt (the hooks page documents
the event and its added-context output; it needs the same one-time trust below),
`Stop`/`SubagentStop` → gate,
`PreToolUse`/`PostToolUse` (matcher `Bash`) → `sisi watch`, blocking with
`PreToolUse`/`PostToolUse` (every tool) → `sisi watch`, blocking with
`{"decision":"block","reason":...}`. One step is yours: Codex only runs hooks you have trusted, by exact
definition hash. After `sisi install`, open Codex, run `/hooks`, and trust the sisi hooks; repeat after
any change to the hook commands (for example after moving the sisi clone). `sisi doctor` cannot verify
+17
View File
@@ -92,6 +92,23 @@ through to the next choice, and quality is untouched. Poor work is a rejected ju
never marks a worker down. No approval- or sandbox-switching flag is passed unless the config
allows it per tool (`workers.allow_bypass`).
## Orchestration the hooks enforce
`sisi route` records the session's routing decision (kind, difficulty guess, pick). A delegation started with
the host's own tool is written to the ledger by the hooks as a pending run with that kind and difficulty, and
completed when it returns; `sisi delegate judge ID --outcome O --check C` then needs nothing else. A worker
other than the router's pick is recorded as an override, and `sisi workers` ends with one line on how often
and how they turned out. After a rejection `sisi delegate judge` prints `sisi route --retry-of ID`: the same
worker with a better brief first, a stronger one after a second rejection.
When a worker fails for a reason that is not about its work (a quota, limit, login or connection failure,
arriving as a delegation result or as a host notice), the hooks record it down until the reset time in the
message, mark its pending run `superseded` and hand the agent the router's next worker. The agent is told to
fall back at once with the same brief and neither ask the person nor report it. `sisi delegate` itself says
the same when only host-native workers remain. Models discovery finds that it had not seen are named once at
session start as unproven. Settings under `orchestrate`: `direct_calls`, `direct_chars`, `extension`,
`route_window_min`. Mechanics: [design.md](design.md) section 9.
## Stored, and when facts go stale
In the user store: `workers.json` (discovery cache), `delegations.jsonl` (the ledger),