Skip to content

Commit d1cadbc

Browse files
MrPickeringclaude
andcommitted
Codex as a supported host, at parity with Claude Code
Host adapter: project.host.kind claude|codex (per-run --host override; studio host selector). Codex packets run `codex exec` read-only and ephemeral, with the prompt on stdin and the answer read from -o; CODEX_API_KEY/OPENAI_API_KEY are stripped so packets use the ChatGPT login, as ANTHROPIC_API_KEY is for Claude. Guardrails under Codex reuse guard.mjs decisions: project hooks in .codex/hooks.json (PreToolUse on Bash and apply_patch, PostToolUse on apply_patch) with apply_patch parsing (update/add/delete/move targets) and the pre-edit blocking-rule gate applied to patches in memory; project command rules in .codex/rules/weaver.rules (forbidden author commands, history writes, installs). `weaver hooks install --host claude|codex|both`; .codex/** is protected and sealed. Doctor reports guardrails per host, including Codex hook trust (via the app-server hooks API) and login. docs/codex-host.md records the Codex facts and their sources. Verified on the real host (codex-cli 0.160.1, book instruction files removed, sandbox fully open, hooks trusted): 11/11 attack prompts left the book unchanged. Codex attempted each one and the guards denied it (author commands plain and disguised, git commit, rules edits by tool and shell, removing or disabling hooks, author-page API, adding em dashes against a blocking rule, writing an approve script, reinstalling the tool). 167/167 tests; consumer install passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Session-Id: 61e7c924-7a69-4dda-83e6-6e95ffcb326b Agent: claude-code
1 parent 4a03142 commit d1cadbc

26 files changed

Lines changed: 1109 additions & 89 deletions

‎README.md‎

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ These are requirements, not suggestions:
2828
history and can be undone. `weaver init` sets this up for you.
2929
2. **Weaver is installed as a package, never copied into your book.** The AI
3030
that works on your book cannot change the tool.
31-
3. **You work with an AI that can run commands,** such as Claude Code. Chat
31+
3. **You work with an AI that can run commands,** such as Claude Code or local Codex. Chat
3232
windows that can't run Weaver aren't supported.
3333
4. **Your original files are never changed.** Import reads them; export writes
3434
new files.
@@ -41,23 +41,25 @@ book, and it tells you in plain words what to fix.
4141
The AI edits prose; the author decides. Author commands (`approve`, `reject`,
4242
`undo`, `rule`, `proposal`, `state:accept`, `release`, `import`, and `guard seal`) refuse in
4343
Claude Code and Codex shells. The author page still works when an AI launches it.
44-
Claude Code edit and shell hooks block protected settings and records, author
44+
Claude Code and Codex edit and shell hooks block protected settings and records, author
4545
commands, Git history/config writes, tool installation changes, and requests to
4646
the local author page. Ordinary prose edits, notes, checks, and reads stay available.
4747

4848
`weaver init` and `weaver hooks install` install the hooks and permission rules.
49-
The rules use `Bash(npx weaver approve:*)` and the equivalent forms for every
49+
Claude's rules use `Bash(npx weaver approve:*)` and the equivalent forms for every
5050
author command (bare `weaver`, `npx`, `npx --no-install`, and `npx.cmd`).
5151
`Edit(/.claude/**)` and `Edit(/quality/**)` are anchored to the book's project
5252
settings, apply to all built-in edit tools, and leave reads available. These use
5353
the documented [Claude Code permission syntax](https://code.claude.com/docs/en/permissions).
5454
Pre-hooks return the documented [deny decision](https://code.claude.com/docs/en/hooks#pretooluse-decision-control).
55-
Permission bypass is disabled in the installed project settings.
55+
Claude permission bypass is disabled in the installed project settings.
56+
Codex uses project hooks and forbidden command-prefix rules; see [Hosts](#hosts)
57+
for the required one-time trust step.
5658

5759
At hand-over, PickBits runs `weaver guard seal` from an author shell. It records
5860
CRLF-normalized SHA-256 fingerprints in `.weaver/guard.json` and commits that
5961
seal with an approval trailer. It covers both book Claude settings files
60-
(including an absent local file), `CLAUDE.md`, `AGENTS.md`, `project.json`, and
62+
(including an absent local file), `.codex/**`, `CLAUDE.md`, `AGENTS.md`, `project.json`, and
6163
every file under `quality/`. Rulings live separately in `continuity/rulings.json`
6264
and do not invalidate the seal. Intentional settings changes require review and
6365
resealing; sealing commits only the seal, leaving other pending work untouched.
@@ -271,7 +273,7 @@ style findings are listed by ID in `check` and `approve` output.
271273

272274
## Claude Code hooks
273275

274-
Run `weaver hooks install --root ./my-book` to merge Weaver's PreToolUse and
276+
Run `weaver hooks install --host claude --root ./my-book` to merge Weaver's PreToolUse and
275277
PostToolUse hooks into `.claude/settings.json`. The pre-hook supplies the
276278
editorial rules and rule messages before scene edits, and the post-hook checks
277279
the edited scene and blocks only when a blocking rule fires. Hooks fail open on
@@ -327,7 +329,7 @@ npx weaver state:accept --through 3-2 --root ./my-book
327329
npx weaver facts build --root ./my-book
328330
```
329331

330-
For an interactive Claude Code session, print one packet and submit its answer:
332+
For an interactive Claude Code or Codex session, print one packet and submit its answer:
331333

332334
```bash
333335
npx weaver packet state --scene 1-1 --root ./my-book
@@ -340,11 +342,89 @@ quotes from elsewhere are rejected, including after the single repair round.
340342
`continuity/facts.json` is deterministic derived state: it keeps facts in scene
341343
order and records the first scene that establishes each normalized value.
342344

343-
The host defaults to Claude Code (`claude -p --output-format text`) and can be
344-
set in `project.json` with `host.command`, `host.args`, `host.concurrency`, and
345-
`host.timeout_seconds`. Weaver removes `ANTHROPIC_API_KEY` from the child
346-
environment, so host runs use the user's Claude subscription rather than
347-
silently billing an API key. A missing host is a doctor warning, not a block.
345+
## Hosts
346+
347+
Claude Code and Codex are supported for local book work, including local Codex
348+
sessions in the ChatGPT desktop app. The AI edits prose; the author approves,
349+
rejects, rules on findings, accepts story state, and releases the book.
350+
351+
Claude Code remains the default (`claude -p --output-format text`). Choose
352+
Codex in `project.json`:
353+
354+
```json
355+
{
356+
"hosts": ["claude", "codex"],
357+
"host": {
358+
"kind": "codex",
359+
"reasoning_effort": "medium",
360+
"timeout_seconds": 600,
361+
"concurrency": 2,
362+
"args": []
363+
}
364+
}
365+
```
366+
367+
`hosts` lists the hosts the author uses; it defaults to both. Doctor blocks
368+
missing or inactive guards only for those hosts. `host.kind` selects the packet
369+
runner. Override it for one call with `--host claude` or `--host codex` on
370+
`run --book --run`, `review`, `repair`, or `state bootstrap`. The Jobs page has
371+
the same selector, initially set to the project choice. An override does not
372+
change the project file. When switching kinds, it uses that host's executable
373+
and arguments instead of carrying the other host's command across.
374+
375+
`host.command` can select a custom executable. Claude's `args` replace its
376+
default arguments; Codex's `args` are extra `exec` options, such as
377+
`["--model", "a-model-you-can-use"]`. Codex receives the packet on stdin, runs
378+
read-only with `--ephemeral` and `--skip-git-repo-check`, and writes its final
379+
answer with `-o` into a temporary directory. Weaver consumes only that file,
380+
cleans it after the attempt, and uses the same validation, repair, and retry
381+
pipeline for either host. Reasoning effort defaults to `medium`; the chosen
382+
model must support the configured level.
383+
384+
Weaver strips `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, and `CODEX_API_KEY` from host
385+
processes. Codex packet calls select the OpenAI provider and require ChatGPT
386+
login. Custom provider credential settings are outside this supported login
387+
path; do not configure API providers for Weaver packets.
388+
389+
For a fresh machine:
390+
391+
1. Install Node.js 22 or newer, Git, Weaver, and the host CLI(s) you use. Run
392+
`claude` and finish sign-in for Claude Code. Run `codex login`, choose
393+
ChatGPT, and verify with `codex login status` for Codex.
394+
2. Create the book with `weaver init <folder>`, which installs both sets of
395+
guards. For an existing book, run `npx weaver hooks install --host both`
396+
from its folder. Use `--host claude` or `--host codex` to repair just one.
397+
The installer merges unrelated configuration and is safe to repeat.
398+
3. For Claude Code, open the book folder, accept its workspace trust prompt,
399+
and restart an existing session so it loads `.claude/settings.json`.
400+
4. For Codex, open a **local** session in the book folder and trust the folder.
401+
Type `/hooks`, review and trust each Weaver hook, then restart Codex to load
402+
the project command rules. New or changed hook definitions require review
403+
again. `AGENTS.md` is discovered automatically; Weaver leaves it unchanged.
404+
5. Run `npx weaver doctor`. On Windows, interactive writing needs a writable
405+
workspace: use `codex --sandbox workspace-write` if doctor reports read-only
406+
mode. Packet calls stay read-only. PickBits seals the installed book settings
407+
at hand-over with `npx weaver guard seal` from the author's own terminal.
408+
409+
Claude uses pre/post edit and shell hooks plus permission denials. Codex uses
410+
`.codex/hooks.json` and `.codex/rules/weaver.rules`. Its patch handler checks
411+
every file and move destination, reconstructs scene edits in memory, and uses
412+
the same blocking-rule regression comparison as Claude. Remaining blocking
413+
findings and advisory voice warnings are returned after editing. If a patch
414+
cannot be reconstructed, that rule check fails open; path guards still run.
415+
416+
Codex 0.160.1 supports project command rules, so **no global installation is
417+
needed**. The optional `--user-rules` flag explicitly also installs the rules
418+
under `$CODEX_HOME/rules/weaver.rules` (normally `~/.codex/rules`). Those global
419+
rules affect other projects too. Normal installation never changes either
420+
host's global configuration or trusts hooks for you.
421+
422+
These are pattern-based guardrails running as the same operating-system user,
423+
not an isolation boundary. Shell disguises, other tools, or direct changes can
424+
get around them. Doctor, the settings seal (including `.codex/**`), and approved
425+
history make covered changes visible. Codex's local hooks are not supported in
426+
Work Cloud with local computer access; use a local Codex session for this
427+
workflow. See [verified Codex CLI facts and limits](docs/codex-host.md).
348428

349429
## Your own checks
350430

@@ -435,7 +515,7 @@ settings-seal checks.
435515
| `reject` | Save and restore one chapter's pending edits |
436516
| `undo` | Revert the most recent approval with a new Git commit |
437517
| `history` | Show approvals and undos (`--json` supported) |
438-
| `hooks install` | Install or merge Claude Code scene-edit hooks |
518+
| `hooks install` | Install or merge Claude Code and Codex guards (both by default) |
439519
| `state:status` | Show current and stale derived-state records |
440520
| `state bootstrap` | Build story-so-far packets, optionally run them through the host |
441521
| `state:accept` | Stamp reviewed narrative state through a scene |

‎contracts/project.schema.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@
3030
"reader_state_path": { "type": "string", "minLength": 1 },
3131
"outline_scene_root": { "type": "string", "minLength": 1 },
3232
"require_current_state_for_release": { "type": "boolean" },
33+
"hosts": { "type": "array", "items": { "enum": ["claude", "codex"] }, "uniqueItems": true, "default": ["claude", "codex"] },
3334
"host": {
3435
"type": "object",
3536
"additionalProperties": false,
3637
"properties": {
38+
"kind": { "enum": ["claude", "codex"], "default": "claude" },
39+
"reasoning_effort": { "type": "string", "pattern": "^[a-z]+$", "default": "medium" },
3740
"command": { "type": "string", "minLength": 1 },
3841
"args": { "type": "array", "items": { "type": "string" } },
3942
"concurrency": { "type": "integer", "minimum": 1, "maximum": 4 },

‎docs/WORKFLOW.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,16 @@ The reusable role prompt is in `prompts/architect.md`.
106106

107107
## 3. Draft
108108

109-
Install the Claude Code hooks once for this book so style rules run around AI
109+
Install both hosts' guards once for this book so style rules run around AI
110110
prose edits:
111111

112112
```bash
113-
weaver hooks install --root ./my-book
113+
weaver hooks install --host both --root ./my-book
114114
```
115115

116+
For Codex, trust the book folder and review and trust Weaver's hooks with
117+
`/hooks`, then restart the local session. See [Hosts](../README.md#hosts).
118+
116119
The pre-hook supplies the editorial rules before an edit. The post-hook checks
117120
the edited scene and reports or blocks deterministic findings. Hooks fail open,
118121
so a hook problem never interrupts an edit; the rules gate in `check` and

‎docs/codex-host.md‎

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# Codex host contract
2+
3+
Verified for Weaver V1-D on 2026-10-13 against installed `codex-cli 0.160.1`.
4+
This records the documentation and local probes used for the implementation.
5+
The tests use synthetic executable fixtures; no test needs a real account.
6+
7+
## 1. Hooks, payloads, decisions, and trust
8+
9+
Codex discovers JSON hook files beside active config layers: user
10+
`$CODEX_HOME/hooks.json` (default `~/.codex/hooks.json`) and project
11+
`<book>/.codex/hooks.json`. Inline `[hooks]` in the corresponding `config.toml`
12+
is another supported representation. Multiple sources merge. Project layers
13+
require workspace trust; each non-managed hook then requires trust of its
14+
current definition hash through `/hooks`.
15+
16+
Weaver installs this shape, with separate shell and patch handlers:
17+
18+
```json
19+
{
20+
"hooks": {
21+
"PreToolUse": [{
22+
"matcher": "^apply_patch$",
23+
"hooks": [{ "type": "command", "command": "npx --no-install weaver hook pre --host codex" }]
24+
}],
25+
"PostToolUse": [{
26+
"matcher": "^apply_patch$",
27+
"hooks": [{ "type": "command", "command": "npx --no-install weaver hook post --host codex" }]
28+
}]
29+
}
30+
}
31+
```
32+
33+
The shell pre-hook matches `^Bash$` and invokes `weaver hook shell --host codex`.
34+
These are synchronous hooks: background hooks cannot deny an action.
35+
36+
Input is one JSON object on stdin. Shared fields are `session_id`,
37+
`transcript_path` (nullable), `cwd`, `hook_event_name`, and `model`.
38+
Event-specific fields documented by OpenAI:
39+
40+
| Event | Additional input |
41+
| --- | --- |
42+
| `SessionStart` | `source`: startup, resume, clear, compact |
43+
| `SessionEnd` | `reason`: currently other |
44+
| `PreToolUse` | `turn_id`, `tool_name`, `tool_use_id`, `tool_input` |
45+
| `PermissionRequest` | `turn_id`, `tool_name`, `tool_input`; optional description inside tool input |
46+
| `PostToolUse` | The pre-tool fields plus `tool_response` |
47+
| `PreCompact`, `PostCompact` | `turn_id`, `trigger`: manual or auto |
48+
| `UserPromptSubmit` | `turn_id`, `prompt` |
49+
| `SubagentStart` | `turn_id`, `agent_id`, `agent_type` |
50+
| `SubagentStop` | `turn_id`, `agent_id`, `agent_type`, `agent_transcript_path`, `stop_hook_active`, `last_assistant_message` |
51+
| `Stop` | `turn_id`, `stop_hook_active`, `last_assistant_message` |
52+
| `Interrupt` | `turn_id` |
53+
54+
`permission_mode` is also present for session start, the three tool events,
55+
prompt submit, subagent start/stop, stop, and interrupt. Weaver uses only
56+
pre/post tool events. Shell/unified exec input is normalized to
57+
`tool_name: "Bash"`, `tool_input.command: "the shell command"`. Patches use
58+
`tool_name: "apply_patch"`, `tool_input.command: "*** Begin Patch\n..."`.
59+
`Edit` and `Write` are matcher aliases; the payload still says `apply_patch`.
60+
61+
To deny before execution, Weaver exits zero and writes:
62+
63+
```json
64+
{
65+
"hookSpecificOutput": {
66+
"hookEventName": "PreToolUse",
67+
"permissionDecision": "deny",
68+
"permissionDecisionReason": "That is the author's decision."
69+
}
70+
}
71+
```
72+
73+
Codex also supports `decision: "block"` with `reason`, or exit 2 with the
74+
reason on stderr. `additionalContext` inside the event-specific output sends
75+
context to the model. A post-tool block supplies feedback but cannot undo the
76+
completed edit. Codex rejects `suppressOutput` on a pre-tool response, so the
77+
Codex adapter does not return Claude's compatibility field.
78+
79+
Source: [OpenAI Hooks documentation](https://learn.chatgpt.com/docs/hooks).
80+
Local evidence: `codex features list` reported `hooks stable true`;
81+
`codex app-server generate-json-schema --out <temp>` enumerated all twelve
82+
events and trust statuses `managed`, `untrusted`, `trusted`, `modified`.
83+
A temporary `CODEX_HOME`, trusted synthetic book, and `hooks/list` RPC returned
84+
the project hook with `source: "project"`, its source path and hash, and
85+
`trustStatus: "untrusted"`. This confirms that workspace trust alone is not
86+
hook trust. Doctor asks this API instead of reverse-engineering the trust file.
87+
The probe never approves or changes a hook.
88+
89+
## 2. Command rules
90+
91+
Project rules are supported: `<book>/.codex/rules/*.rules`, beside the trusted
92+
project config layer. User rules live under `$CODEX_HOME/rules/*.rules`.
93+
`prefix_rule` decisions are exactly `allow`, `prompt`, and `forbidden`;
94+
the most restrictive matching decision wins. These command policies primarily
95+
control execution outside the sandbox; the shell hook is the first guard.
96+
97+
Source: [OpenAI Rules documentation](https://developers.openai.com/codex/rules).
98+
Local evidence: `codex exec --help` describes `--ignore-rules` as skipping user
99+
and project rules. `codex execpolicy check --rules <generated-weaver.rules>`
100+
returned `forbidden` for `git commit`, `npx --no-install weaver approve`, and
101+
`npm install`; `git status` matched no forbidden rule. This exercised the
102+
installed CLI's Starlark parser, including union patterns.
103+
104+
No user rules installation is needed. `hooks install --user-rules` is an
105+
explicit optional global copy; it never edits the user's `config.toml`.
106+
107+
## 3. Packet invocation
108+
109+
The adapter supplies the full prompt on stdin, conceptually:
110+
111+
```text
112+
codex exec [host.args...] --sandbox read-only --ephemeral --skip-git-repo-check -c model_reasoning_effort=medium -c model_provider=openai -c forced_login_method=chatgpt -o <temporary-answer-file> -
113+
```
114+
115+
The final `-` selects stdin as the prompt. The final answer comes only from the
116+
output file, even if stdout contains logs or other text. Each attempt gets a
117+
new temporary directory; cleanup follows success, failure, or timeout.
118+
`--ephemeral` prevents session rollout persistence, not every operational log.
119+
`host.reasoning_effort`, `args`, and `timeout_seconds` configure each call.
120+
The model must support the selected effort. `--output-schema` exists but is
121+
not required: Weaver's packets include Markdown and fenced JSON and already
122+
have validation and a repair round.
123+
124+
Sources: installed `codex exec --help`,
125+
[non-interactive mode](https://developers.openai.com/codex/noninteractive), and
126+
[configuration reference](https://learn.chatgpt.com/docs/config-file/config-reference).
127+
The fake Codex executable checks the flags, reads stdin, emits stdout noise,
128+
writes `-o`, and supports malformed answers and real process timeouts.
129+
130+
Weaver does not pass `--ignore-rules` or `--dangerously-bypass-hook-trust`.
131+
The latter exists in 0.160.1 but bypasses the author's trust step; it is not a
132+
substitute for installing and trusting the guards.
133+
134+
## 4. Authentication and billing
135+
136+
`CODEX_API_KEY` overrides saved authentication for `codex exec`. `OPENAI_API_KEY`
137+
is the conventional OpenAI key variable and can supply API-key login or a
138+
configured provider. Both are removed, case-insensitively, along with
139+
`ANTHROPIC_API_KEY`. Packet calls pin `model_provider=openai` and
140+
`forced_login_method=chatgpt` to require the supported subscription login.
141+
An author with stored API authentication must sign in with ChatGPT first.
142+
143+
Custom model providers can name **any** environment variable in `env_key`, or
144+
use `experimental_bearer_token`/command-backed credentials. There is no finite
145+
list of custom provider secret names to strip. Arbitrary API/gateway provider
146+
configuration is outside Weaver's supported subscription path; environment
147+
stripping alone cannot guarantee billing behavior for custom configurations.
148+
`CODEX_HOME` selects config/auth storage; it is deliberately preserved so the
149+
author's existing ChatGPT login remains available.
150+
151+
Sources: [non-interactive authentication](https://developers.openai.com/codex/noninteractive#authenticate-in-automation),
152+
[authentication](https://developers.openai.com/codex/auth), and
153+
[provider configuration](https://learn.chatgpt.com/docs/config-file/config-reference).
154+
Local `codex login status` reported ChatGPT login. No credentials were read or
155+
printed and no real model request was needed for the probes.
156+
157+
## Limits and Windows
158+
159+
Local hooks apply to local Codex sessions, including local desktop work.
160+
OpenAI explicitly excludes local hook files from Work Cloud with local
161+
computer access; Weaver cannot provide this protection on that surface.
162+
Use a local session. See [hook deployment limits](https://learn.chatgpt.com/docs/hooks).
163+
164+
On Windows, doctor identifies a configured `read-only` sandbox and explains
165+
how to start interactive writing with `codex --sandbox workspace-write`.
166+
The documentation does not establish that a particular Windows sandbox
167+
backend universally breaks file edits in 0.160.1; Weaver does not invent such
168+
a diagnosis or recommend disabling the sandbox. Current Windows docs discuss
169+
MXC support that depends on OS capabilities and newer CLI versions; those
170+
instructions are not assumed to work on 0.160.1. See
171+
[Windows sandbox documentation](https://developers.openai.com/codex/windows).
172+
173+
Pattern matching and same-user hooks can be bypassed. Unsupported tools,
174+
hook errors, or an unmatchable patch can escape checks. The settings seal,
175+
doctor, and approved history are the audit trail. Doctor blocks hosts listed
176+
in `project.hosts` if their required guards are absent, disabled, or (for
177+
Codex) untrusted/unverifiable. Other hosts are reported without blocking.

0 commit comments

Comments
 (0)