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
3 changes: 2 additions & 1 deletion bin/gstack-settings-hook
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ var KNOWN_HOOKS = {
"autoplan-stop-gate-stop": { source: "gstack-autoplan-gate", event: "Stop", matcher: "", relpath: "hosts/claude/hooks/autoplan-stop-gate-stop" },
"autoplan-stop-gate-publish": { source: "gstack-autoplan-gate", event: "PreToolUse", matcher: "Bash", relpath: "hosts/claude/hooks/autoplan-stop-gate-publish" },
"codex-skill-route-hook": { source: "gstack-skill-route", event: "UserPromptSubmit", matcher: "", relpath: "hosts/codex/hooks/codex-skill-route-hook", host: "codex" },
"codex-destructive-guard-hook": { source: "gstack-destructive-guard", event: "PreToolUse", matcher: "Bash", relpath: "hosts/codex/hooks/codex-destructive-guard-hook", host: "codex" }
"codex-destructive-guard-hook": { source: "gstack-destructive-guard", event: "PreToolUse", matcher: "Bash", relpath: "hosts/codex/hooks/codex-destructive-guard-hook", host: "codex" },
"codex-skill-contract-stop-hook": { source: "gstack-skill-contract", event: "Stop", matcher: "", relpath: "hosts/codex/hooks/codex-skill-contract-stop-hook", host: "codex" }
};
// GSTACK_HOOKS_FORMAT=codex: the file is Codex hooks.json, and only rows with
// host codex identify gstack items in it; every other row belongs to
Expand Down
21 changes: 14 additions & 7 deletions bin/gstack-skill-contract
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env -S bun --no-env-file --config=/dev/null
/**
* gstack-skill-contract — check a skill's contract against a Claude Code
* transcript, list recorded misses, and acknowledge a waiver (GST-1088).
* transcript or a Codex rollout, list recorded misses, and acknowledge a
* waiver (GST-1088, GST-3173).
*
* Usage:
* gstack-skill-contract check --transcript <path> --skill <name> [--skill-file <SKILL.md>] [--json]
Expand All @@ -19,7 +20,7 @@
*/
import {
EXIT, StateError, UnknownShape, blockMessage, checkContract, isMiss, lookupContract, parseTranscript, readAllRecords,
readTranscriptFile, reportRows, skippedLine, type CheckResult,
readTranscriptFile, reportRows, skippedLine, transcriptContract, type CheckResult, type Transcript,
} from '../lib/skill-contract';
import { fileURLToPath } from 'node:url';
import { installedSkillsDir } from '../lib/skill-route';
Expand Down Expand Up @@ -67,13 +68,18 @@ function check(args: string[]): never {
const skill = flag(args, '--skill');
const json = args.includes('--json');
if (!transcriptPath || !skill) die(EXIT.USAGE, `SKILL_CONTRACT_USAGE: check needs --transcript and --skill\n${USAGE}`);
const found = lookupContract(skill, installedSkillsDir(), flag(args, '--skill-file'));
const skillFile = flag(args, '--skill-file');
let transcript: Transcript | null = null;
let readError: unknown = null;
try { transcript = parseTranscript(readTranscriptFile(transcriptPath)); } catch (e) { readError = e; }
const found = skillFile || !transcript ? lookupContract(skill, installedSkillsDir(), skillFile) : transcriptContract(transcript, skill);
if (!('contract' in found)) die(EXIT.NO_CONTRACT, `SKILL_CONTRACT_NO_CONTRACT ${skill}: ${found.error}`);
for (const why of found.contract.skipped ?? []) process.stderr.write(`${skippedLine(found.file, why)}
`);
let result: CheckResult;
try {
result = checkContract(parseTranscript(readTranscriptFile(transcriptPath)), skill, found.contract);
if (readError) throw readError;
result = checkContract(transcript as Transcript, skill, found.contract);
} catch (e) {
if (e instanceof StateError) die(EXIT.STATE_ERROR, `SKILL_CONTRACT_STATE_ERROR ${e.file}: ${e.why}`);
if (e instanceof UnknownShape) die(EXIT.UNKNOWN_SHAPE, `SKILL_CONTRACT_UNKNOWN_SHAPE ${transcriptPath}: ${e.message}`);
Expand All @@ -90,10 +96,11 @@ function report(args: string[]): never {
const session = flag(args, '--session');
const { records, errors } = readAllRecords();
const rows = reportRows(records.filter(r => !session || r.session_id === session), (rec, run) => {
const found = lookupContract(run.skill, installedSkillsDir());
if (!('contract' in found)) return null;
try {
const again = checkContract(parseTranscript(readTranscriptFile(rec.transcript_path)), run.skill, found.contract, { loadLine: run.load_line });
const t = parseTranscript(readTranscriptFile(rec.transcript_path));
const found = transcriptContract(t, run.skill);
if (!('contract' in found)) return null;
const again = checkContract(t, run.skill, found.contract, { loadLine: run.load_line });
return again.load ? again : null;
} catch {
return null;
Expand Down
1 change: 1 addition & 0 deletions changelog.d/codex-review-contract-hook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **On Codex, /review is now held once when it tries to finish without its adversarial pass or its report, as it already is on Claude Code.** `./setup --host codex` adds the skill-contract Stop hook to `~/.codex/hooks.json` next to the routing hint and the destructive-command check, and Codex runs it once you trust it at its startup prompt or in `/hooks`. The hook reads Codex's rollout file (it tells a Codex rollout from a Claude Code transcript by what the file holds), counts a skill as loaded when Codex injects or the model reads a `gstack-*` SKILL.md, and counts a `spawn_agent` call that started an agent as a dispatch. /review's specialist-reviewer item is skipped on Codex, which doesn't run those reviewers yet. `gstack-skill-contract check` takes a Codex rollout too. The same `skill_contracts` switch turns it off on both hosts. Details: docs/skill-routing-and-contracts.md#codex.
24 changes: 16 additions & 8 deletions docs/skill-routing-and-contracts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Skill routing and skill contracts

Three Claude Code hooks that help a model actually run gstack's skills (GST-1088, GST-3110); two of them also run on Codex ([Codex](#codex)). All three are on by default. On Opus 5.5, unattended, a hook line naming a skill got it loaded in 58 of 60 bug-fix sessions, where the same skill relying on its description loaded in 0 of 60 (gstack-evals#369, and B1 before it on the same 60 tasks). With the contract hook, all 66 /review sessions ran its specialist reviewers, 42 of them after the hook's one block; in an earlier run without it, 0 of 66 did (gstack-evals board posts GEVA-271 and GEVA-105). Each has one default, `SWITCH_DEFAULT` in `lib/skill-route.ts`; `gstack-config defaults` prints it. With all three off nothing changes. The opt-in gates that hold one skill rule mid-run (the /plan-ceo-review and /autoplan gates) are in [skill-gates.md](skill-gates.md).
Three Claude Code hooks that help a model actually run gstack's skills (GST-1088, GST-3110); all three also run on Codex ([Codex](#codex)). All three are on by default. On Opus 5.5, unattended, a hook line naming a skill got it loaded in 58 of 60 bug-fix sessions, where the same skill relying on its description loaded in 0 of 60 (gstack-evals#369, and B1 before it on the same 60 tasks). With the contract hook, all 66 /review sessions ran its specialist reviewers, 42 of them after the hook's one block; in an earlier run without it, 0 of 66 did (gstack-evals board posts GEVA-271 and GEVA-105). Each has one default, `SWITCH_DEFAULT` in `lib/skill-route.ts`; `gstack-config defaults` prints it. With all three off nothing changes. The opt-in gates that hold one skill rule mid-run (the /plan-ceo-review and /autoplan gates) are in [skill-gates.md](skill-gates.md).

- **Skill routing** (`skill_routing`, a UserPromptSubmit hook). When a prompt matches an installed skill's `triggers:`, the hook adds one line of context naming that skill, for example "this request fits /verify. Run /verify with the Skill tool before you start".
- **Skill contracts** (`skill_contracts`, a Stop hook). A skill can declare the outputs that must exist before it is done. When the model tries to stop without them, the hook blocks once and names what is missing.
Expand Down Expand Up @@ -32,8 +32,8 @@ The off switch is for people. The text the Stop hook shows the model never menti
| Host | Routing hook | Contract Stop hook | Destructive-command check | Notes |
|---|---|---|---|---|
| Claude Code | yes | yes | yes | Registered in `~/.claude/settings.json` by `./setup` |
| Codex | yes | not yet | yes, with a deny where Claude Code asks | Registered in `${CODEX_HOME:-~/.codex}/hooks.json` by `./setup --host codex` (Codex 0.170 or newer). Codex runs them only after you trust them: see [Codex](#codex) |
| OpenCode, Cursor and other hosts | no | no | no | gstack registers no hooks on these hosts. `gstack-skill-contract check` still works on a Claude Code transcript |
| Codex | yes | yes, without /review's specialists item | yes, with a deny where Claude Code asks | Registered in `${CODEX_HOME:-~/.codex}/hooks.json` by `./setup --host codex` (Codex 0.170 or newer). Codex runs them only after you trust them: see [Codex](#codex) |
| OpenCode, Cursor and other hosts | no | no | no | gstack registers no hooks on these hosts. `gstack-skill-contract check` still works on a Claude Code transcript or a Codex rollout |

## Routing

Expand Down Expand Up @@ -83,16 +83,24 @@ The hook reads `destructive_guard` every time it runs, so `gstack-config set des
<a id="codex"></a>
## Codex

`./setup --host codex` (or `./setup --host auto` when `codex` is on your PATH) registers the routing hook and the destructive-command check in `${CODEX_HOME:-~/.codex}/hooks.json`, each in its own matcher group: routing on `UserPromptSubmit` with a 5-second timeout, the check on `PreToolUse` for `Bash` with a 10-second timeout. The contract Stop hook is not on Codex yet. The same three switches, in the same order, turn them on and off on both hosts; Codex adds its own two, a hook's toggle in `/hooks` and `[features] hooks = false` in `config.toml` for every hook at once.
`./setup --host codex` (or `./setup --host auto` when `codex` is on your PATH) registers the routing hook, the contract Stop hook and the destructive-command check in `${CODEX_HOME:-~/.codex}/hooks.json`, each in its own matcher group: routing on `UserPromptSubmit` with a 5-second timeout, the contract hook on `Stop` with a 10-second timeout (as on Claude Code), the check on `PreToolUse` for `Bash` with a 10-second timeout. The same three switches, in the same order, turn them on and off on both hosts; Codex adds its own two, a hook's toggle in `/hooks` and `[features] hooks = false` in `config.toml` for every hook at once.

**You trust them once.** Codex runs a hook only after you trust it, and skips an untrusted one without saying so. Setup never writes that trust. It prints one line, "gstack added 2 Codex hook(s); trust them at Codex's startup prompt or in /hooks", and the next time you start Codex it shows "Hooks need review": pick "Trust all and continue", or review them one by one in `/hooks`. `gstack-doctor` has a `codex hooks` row saying whether `config.toml` holds a trust entry for each of gstack's hooks. It only checks that an entry exists; it never computes Codex's hash. Codex asks again when a hook's command or timeout changes, so the commands point at the stable runtime root (`${CODEX_HOME:-~/.codex}/skills/gstack/hosts/codex/hooks/`), and an upgrade that only changes the scripts behind them asks nothing. Codex also keys trust by a group's position in the file, so setup adds gstack's groups after yours and never reorders them. Evals and CI that run `codex exec` pass `--dangerously-bypass-hook-trust`.
**You trust them once.** Codex runs a hook only after you trust it, and skips an untrusted one without saying so. Setup never writes that trust. It prints one line, "gstack added 3 Codex hook(s); trust them at Codex's startup prompt or in /hooks", and the next time you start Codex it shows "Hooks need review": pick "Trust all and continue", or review them one by one in `/hooks`. `gstack-doctor` has a `codex hooks` row saying whether `config.toml` holds a trust entry for each of gstack's hooks. It only checks that an entry exists; it never computes Codex's hash. Codex asks again when a hook's command or timeout changes, so the commands point at the stable runtime root (`${CODEX_HOME:-~/.codex}/skills/gstack/hosts/codex/hooks/`), and an upgrade that only changes the scripts behind them asks nothing. Codex also keys trust by a group's position in the file, so setup adds gstack's groups after yours and never reorders them. Evals and CI that run `codex exec` pass `--dangerously-bypass-hook-trust`.

**Your own hooks stay as they are.** Codex 0.170 skips the whole `hooks.json` when it holds a key it does not know, your hooks included. So gstack adds no marker keys (it recognizes its groups by their command path), checks the new file against Codex's schema before moving it into place, and keeps the lock, the `hooks.json.bak.<ts>` backup and `gstack-settings-hook rollback` that `settings.json` has (`GSTACK_HOOKS_FORMAT=codex gstack-settings-hook rollback`). Setup leaves the file alone, prints why and carries on when the file does not parse or already fails Codex's schema, when `config.toml` does not parse, or when `config.toml` already declares one of gstack's hooks under `[hooks]` (Codex would run it twice). It also skips the hooks, saying why, when `codex` is missing or older than 0.170. `./setup --host codex --no-team` (plain `./setup --no-team` installs for Claude Code only, so it leaves the Codex hooks alone; `--host auto` with `codex` on your PATH also reaches them) and `gstack-uninstall` remove gstack's groups and nothing else. If one of your own hooks comes after gstack's group in the same event, removing gstack's group would shift yours to a new position and Codex would stop trusting it (`codex exec` skips an untrusted hook without asking). So gstack leaves the file unchanged and says so; delete gstack's group by hand and re-trust your hooks in `/hooks`.

**Routing on Codex** scans `${CODEX_HOME:-~/.codex}/skills`, names gstack's skills the way Codex does (`gstack-review`), and takes their triggers from the same skill in gstack's checkout, because Codex's copies keep only a name and a description. The line reads "this request fits the gstack-review skill. Read and follow the gstack-review skill before you start". A prompt that starts with `$` (a Codex skill mention) or `/` is never routed.

**The destructive-command check on Codex** runs the same `check-careful.sh`, but Codex has no "ask": its hook parser rejects `permissionDecision: "ask"` and lets the command run. So in an interactive Codex session, every command the check would ask about is blocked instead, and the reason tells the model to stop and ask you. If you want it, run it yourself by typing `!` and the command in Codex (Codex runs `!` commands without hooks), or turn the check off. In `codex exec`, and any session `gstack-session-kind` reports as headless or unattended, it is a warning and the command runs, as on Claude Code. The two deny shapes stay a deny everywhere. Codex already refuses some `rm -rf` commands by itself, before any hook runs.

**Contracts on Codex** use the same Stop hook, with the same one block per skill run, records and fail-open rules; Codex sends it the same `stop_hook_active` and `last_assistant_message` keys and takes the same block answer. The transcript is Codex's rollout file (`~/.codex/sessions/<yyyy>/<mm>/<dd>/rollout-*.jsonl`), and the checker tells it from a Claude Code transcript by what the file holds (a rollout starts with a `session_meta` record), never by its path. On Codex:

- A **load** is Codex injecting a `gstack-*` skill into the conversation as a `<skill>` message (the app server's skill input does this), or the model reading one with a shell command such as `cat ~/.codex/skills/gstack-review/SKILL.md`. `codex exec '$gstack-review ...'` passes the `$` mention as plain text, so in `exec` the skill loads only when the model reads it.
- A **dispatch** is a `spawn_agent` call that started an agent. One that failed (its result has no `agent_id`) doesn't count.
- **The contract** comes from gstack's own copy of the skill (`<gstack>/review/SKILL.md`), because Codex's installed `SKILL.md` keeps only its name and description.
- **/review's `specialists-dispatched` item is skipped**, because Codex doesn't run /review's specialist reviewers yet. The adversarial pass and the report are checked as on Claude Code, and the block text names `spawn_agent` and `wait_agent` where Claude Code's names an Agent dispatch.
- **A subagent's own rollout is never checked**: its first record names the parent thread.

**Reasoning effort.** gstack's workflows do best at high reasoning effort. Setup leaves your `config.toml` alone and prints a reminder; start Codex with `codex -c model_reasoning_effort=high` when you run them.

## Contracts
Expand Down Expand Up @@ -149,15 +157,15 @@ It blocks at most once per skill run. If the model stops again with the contract

### Records

Each Claude Code session has one record, `$GSTACK_STATE_ROOT/skill-contracts/<session id>.json`, written atomically (mode 0600) before any block. Each skill run in it has an outcome: `met`, `met_waived`, `blocked`, `met_after_block`, `miss` or `deferred` (stopped while its agents were still running, not yet checked; `report` re-checks it against its transcript). The hook's decisions also go to `$GSTACK_STATE_ROOT/hooks/skill-contract.log`.
Each session has one record, `$GSTACK_STATE_ROOT/skill-contracts/<session id>.json`, written atomically (mode 0600) before any block. Each skill run in it has an outcome: `met`, `met_waived`, `blocked`, `met_after_block`, `miss` or `deferred` (stopped while its agents were still running, not yet checked; `report` re-checks it against its transcript). The hook's decisions also go to `$GSTACK_STATE_ROOT/hooks/skill-contract.log`.

## Commands

### check

gstack-skill-contract check --transcript <path> --skill <name> [--skill-file <SKILL.md>] [--json]

A pure function of a Claude Code transcript (the JSONL a hook payload's `transcript_path` names, under `~/.claude/projects/`). The contract comes from the installed skill's frontmatter, or from `--skill-file`. It prints each item as met, missing or waived. The model never records anything itself, so evals can run the same check on their own transcripts.
A pure function of a Claude Code transcript or a Codex rollout (the JSONL a hook payload's `transcript_path` names, under `~/.claude/projects/` or `~/.codex/sessions/`). The contract comes from the installed skill's frontmatter (on Codex, gstack's own copy of the skill), or from `--skill-file`. It prints each item as met, missing or waived. The model never records anything itself, so evals can run the same check on their own transcripts.

### report

Expand Down Expand Up @@ -203,7 +211,7 @@ The stop goes through and nothing is recorded. Fix the named path: make `$GSTACK
<a id="exit-4-unknown-shape"></a>
### 4: unknown transcript shape (SKILL_CONTRACT_UNKNOWN_SHAPE)

The file is not a Claude Code transcript this checker understands (no user or assistant records, or message content that is neither text nor a block list), for example after a Claude Code format change. The Stop hook fails open with a state error line and adds one to `$GSTACK_STATE_ROOT/hooks/skill-contract.canary.json`. A rising canary count means the parser needs an update.
The file is not a transcript this checker understands: a Claude Code transcript with no user or assistant records, or message content that is neither text nor a block list, or a Codex rollout with no message records or a record shaped in a way the reader doesn't know. That usually means Claude Code or Codex changed its format. The Stop hook fails open with a state error line and adds one to `$GSTACK_STATE_ROOT/hooks/skill-contract.canary.json`. A rising canary count means the parser needs an update.

<a id="exit-5-no-contract"></a>
### 5: no contract (SKILL_CONTRACT_NO_CONTRACT)
Expand Down
Loading
Loading