# Codex Integration

`llm-wiki-kit` installs command hooks into the Codex hook configuration, usually `~/.codex/hooks.json`.

The command target is:

```bash
llm-wiki hook codex <EventName>
```

The installed hook stores the Node executable and the absolute package binary path. A system-wide npm install usually points at:

```bash
/usr/lib/node_modules/llm-wiki-kit/bin/llm-wiki.js
```

User-local fallback installs may instead point under `$HOME/.local/lib/node_modules`.

Handled events:

- `SessionStart`
- `UserPromptSubmit`
- `PreToolUse`
- `PostToolUse`
- `PreCompact`
- `PostCompact`
- `SubagentStop`
- `Stop`

Expected behavior:

- In an explicitly initialized project, `SessionStart` may recover queue-worthy stale turn state, perform a cached npm update notice check for npm installs, report managed-template or maintenance drift, and inject functional compact context. It does not bootstrap a workspace, refresh templates, remove legacy files, migrate layouts, or apply lifecycle changes. The context still uses `llm-wiki/wiki/memory.md`, `llm-wiki/wiki/index.md`, relevant wiki/search state, operating rules, maintenance signals, and passive runtime update status.
- `UserPromptSubmit` recovers queue-worthy stale turn state, searches project wiki pages with MiniSearch or substring fallback, expands one-hop wikilinks, redacts context fields, performs the same cached update notice check, and injects the smallest useful functional compact context set. Verbose `llm-wiki context` can explain `why selected`, `rankReason`, `matchedFields`, and `evidenceRefs`, but hook context keeps those details compact. Update notice cache is scoped by npm command, and maintenance reminders are shown only for wiki/maintenance prompts, approved items, explicit durable requests, recovered items, or batch review threshold pressure.
- Hook-visible language is selected from the current real user prompt first. Korean prompts get Korean guidance, English prompts get English guidance. Codex synthetic plan execution prompts such as `Implement the plan.` are treated as neutral, so they reuse remembered session language or project `preferredLanguage` instead of forcing English. If no prompt language is clear, Codex falls back to local `CLAUDE.md`/`AGENTS.md` language signals, then English.
- `PreToolUse` records redacted tool summaries without blocking tool calls.
- `PostToolUse` records redacted tool summaries in a turn buffer.
- `PreCompact` classifies the current turn before compaction. Non-durable turns stay in transient runtime state; explicit durable requests, structured durable conclusions with required evidence, and durable wiki/procedure updates write a chunked live Q&A checkpoint; explicit/structured durable candidates write a maintenance queue item only when no durable wiki/procedure update is detected. It never opens `transcript_path`; the checkpoint is built only from bounded, redacted turn state captured by earlier hook events. Compaction is not blocked; if checkpoint storage fails, the hook records a transient compact recovery packet for the next legal context-injection event.
- `PostCompact` keeps the compact summary transient and prepares any pending compact recovery packet. It does not write `wiki/context/` notes or return `hookSpecificOutput.additionalContext`, because Codex `PostCompact` only supports common output fields.
- In the default `answer-first` mode, `SubagentStop` does not create live Q&A, query, decision, or maintenance files. `Stop` appends chunked live Q&A only for explicit durable requests, structured durable conclusions with required evidence, or durable wiki/procedure updates and does not auto-create `wiki/queries/` or `wiki/decisions/`.
- If the captured user question explicitly asked to record durable knowledge, or the turn contains a structured durable conclusion, and no durable wiki/procedure update is detected, `Stop` queues a pending maintenance item for batch review. `wiki/queries/` updates are episodic and do not suppress this queue. Queue items carry `signal_level` and may carry safe `evidence_refs` candidates from actual repo files and verification commands. The lifecycle keeps candidates pending for batch review, skips stale low-signal items, and archives old reviewed items, but it never creates durable wiki pages automatically or auto-approves promotion. Prompt-time reminders are limited to explicit durable requests, recovered state, maintenance prompts, approved items, or batch thresholds.
- `Stop` clears the per-session turn buffer after recording. `SubagentStop` leaves the parent turn buffer available for the final stop event.
- Provider events without a stable session or conversation identifier fail closed for transient continuity: they do not read or write a shared fallback turn, compact, language, or recovery record.

Hook payloads are stored as small redacted event envelopes rather than full transcripts. Context output is also redacted field by field before it is returned to Codex. Functional compact context is a presentation policy, not a feature reduction: Codex still receives the wiki memory, search, maintenance, and passive update signals needed for the hook workflow.

For handoff or retrieval verification, use `llm-wiki export --workspace <project> --format all` and `llm-wiki eval --workspace <project>`. The generated `llms.txt`/`llms-full.txt`/`llm-wiki.json` files are redacted durable manifests, not raw transcripts.

Managed-template refresh is explicit through `post-update` or `install`; routine hooks only report drift.
Set `LLM_WIKI_KIT_UPDATE_NOTICE=0` only while suppressing the cached passive runtime update status.

Set `LLM_WIKI_KIT_PRECOMPACT_ENFORCEMENT=off` to suppress pre-compact failure warnings; `limited` and `soft` both keep compaction moving and emit a non-blocking warning when checkpoint/queue preservation fails. Transcript-tail capture is not supported.

Run these after install:

```bash
llm-wiki status --workspace /path/to/project
llm-wiki doctor --workspace /path/to/project
```

If Codex reports untrusted hooks, trust the hook through Codex's own trust flow or run a trusted automation profile that explicitly accepts the hook source. Restart Codex after hook installation or update.
