# AGENT_GUIDE.md Reference for AI coding assistants (Claude Code, Codex, and similar agents) consuming `@shakystar/memorize`. Humans do not need to read this file end-to-end; the README is the human entry point. This file assumes you already ran the setup steps in [guides/AI_SETUP.md](./guides/AI_SETUP.md). It exists so you can look up the full behaviour, flags, idempotency guarantees, and failure modes of every command without guessing. Ship location: the repo root (`https://github.com/shakystar/memorize/blob/main/AGENT_GUIDE.md`) and inside the npm tarball (listed in `package.json#files`). --- ## Ground rule: memorize is the single source of truth **Do not duplicate memorize state in your own memory system.** If you maintain your own long-term memory (Claude Code `MEMORY.md`, Cursor saved context, etc.): - OK: a one-line note "memorize is installed in this project; always query it for state." - Not OK: recording the project id, current tasks, handoff text, rule contents, decisions, or any other data memorize tracks. Reason: your memory cannot follow memorize's state when the user runs `project setup` again, wipes `~/.memorize/`, or the project id is otherwise regenerated. A stale pointer in your memory will diverge silently from reality and produce wrong answers. Always ask memorize at session start: ```sh npx @shakystar/memorize task resume # full startup payload as JSON npx @shakystar/memorize project show # bound project entity npx @shakystar/memorize doctor --json # health + install state ``` Treat any information in your own memory that overlaps with these outputs as a cache that must be re-validated, not as authoritative. ## Mental model 1. **Event log** is the source of truth. Every task, handoff, checkpoint, rule, decision, and conflict is an append-only event under `.memorize//events/YYYY-MM-DD.ndjson`. 2. **Projection** is a derived cache rebuilt from the event log. Safe to delete; `memorize projection rebuild` regenerates it. 3. **Startup payload** is a small bundle built from the projection and rendered per-agent (`claude` vs `codex` formats differ). 4. **Install hooks** wire your agent runtime: - Claude Code: `.claude/settings.local.json` (per-project) registers `SessionStart`, `PostToolUse` (capture), `PostCompact`, `SessionEnd`. - Codex: `~/.codex/hooks.json` (global per-user; the handler no-ops when cwd is not a memorize-bound project) registers `SessionStart`, `PostToolUse` (capture), and `PostCompact` (consolidation boundary). Codex has no SessionEnd / Shutdown hook. In both cases the SessionStart hook calls `memorize hook SessionStart` and the output is injected as `hookSpecificOutput.additionalContext`. 5. **Session lifecycle** is owned by memorize, not by per-turn hooks. - `SessionStart` mints a new session, claims a task (best-effort), and reaps any prior abandoned pointers in the same cwd. - Heartbeat events fire from every memorize CLI call; they keep the session's `lastSeenAt` fresh. - Claude `SessionEnd` writes `session.completed` and unlinks the cwd pointer when the agent exits cleanly. - For Ctrl+C, crashes, and Codex (which has no end-hook), the next `SessionStart` in the same cwd reaps stale pointers (older than 30 min by default; tunable via `MEMORIZE_STALE_SESSION_MS`) and emits `session.abandoned`. Run `memorize session reap` to force a sweep at any time. 6. **Handoffs are agent-initiated.** The `Stop` hook used to auto-write a handoff at every assistant turn, which conflated "turn end" with "session end." Now agents call `memorize handoff create ...` only when they actually want to hand off control or summarize their work for the next agent. Treat handoffs as intentional artifacts, not automatic ones. You interact with memorize through the CLI; never hand-edit `.memorize/` files. --- ## File layout (under `MEMORIZE_ROOT`) ``` / ├── profile/ │ └── bindings.json # path → projectId (walk-up resolved) └── projects/ └── / ├── project.json # projection (rebuilt from events) ├── memory-index.json # derived summary index ├── events/ │ └── YYYY-MM-DD.ndjson # append-only, integrity-checked ├── tasks/.json ├── workstreams/.json ├── handoffs/.json ├── checkpoints/.json ├── decisions/.json ├── rules/.json ├── conflicts/.json ├── topics/.md └── sync/ └── remote.json # sync state ``` Defaults: `MEMORIZE_ROOT` env overrides the location; if unset, memorize uses `/.memorize`. Set `MEMORIZE_ROOT` when running tests or in CI to isolate state. In the user's project directory, a small `.memorize/` may also appear for per-project runtime state (current session, bootstrap files). The `.memorize/` directory should be listed in the project's `.gitignore`; `memorize doctor` warns if it is not. --- ## Binding resolution `resolveProjectIdForPath(cwd)` walks upward: `cwd → parent → … → /`. The nearest ancestor that was previously bound wins. This means: - `memorize task list` from `~/work/myproj/src/components` resolves to the `~/work/myproj` binding. - Nested bindings (a bound project directory containing another bound project directory) resolve to the closer one. - If no ancestor is bound, commands that require a project fail with `No project bound to current directory.` --- ## Commands Flags marked `boolean` accept `--flag` (no value). Flags marked `single` accept `--flag value` or `--flag=value`. Flags marked `multi` may be repeated and collected into an array. ### `memorize doctor [--json]` Runs a sequence of checks on the current project and integration. Human-readable by default; `--json` emits a stable shape: ```json { "status": "ok | warn | error", "checks": [ { "id": "...", "label": "...", "status": "ok|warn|error", "message": "...", "fix": "suggested command or action" } ], "issues": [ { "id": "...", "severity": "warn|error", "fix": "..." } ], "version": "1" } ``` Common checks: project binding, required directories, consolidation health (observations pending consolidation plus when/where/how the last consolidation attempt ended; warns when a backlog is stuck), git redaction risk (`.memorize/` in `.gitignore`), install state. Exit code: `1` when status is not `ok`. Use `--json` for scripting. ### `memorize update` Upgrades the global CLI to the latest published version, then refreshes every existing integration on this machine with the new binary: - Re-installs Codex hooks (`~/.codex/hooks.json`) when memorize entries are already present. - Re-installs Claude hooks for every known project that already has them (never installs fresh into a project that has not opted in). - Re-imports changed context files (`CLAUDE.md`, `AGENTS.md`, `GEMINI.md`, `.cursorrules`, `.cursor/rules`) for every bound project. Idempotent: unchanged files emit nothing; changed files upsert the same rule in place. Already up to date: the npm step is skipped, but the refresh still runs. `update` therefore doubles as a repair command when integrations drift after a manual change. Data is never deleted: the event store is append-only. A removed context file leaves its previously imported rule intact. **Internal flags (not for direct use):** - `--post-only`: refresh-only re-exec entry point called by the upgraded binary after `npm install -g` completes. The parent process exits and the new binary owns the machine-wide refresh. - `--check`: detached registry probe spawned by `SessionStart`. Writes the latest available version to `~/.memorize/update-check.json` without installing anything. **Failure modes:** - No global install found: prints guidance and exits 1. - Registry unreachable: exits 1. - `npm install -g` fails: exit code propagated, refresh skipped. - Per-project refresh failure: reported at the end with exit 1; the loop continues past individual failures so other projects still run. **Session-start notice:** when a newer version is cached in `update-check.json`, one line is appended to the startup context (`memorize vX.Y.Z available, run memorize update`). The background check is throttled to once per 24 h, runs detached, and never blocks session start or auto-installs anything. ### `memorize consolidate [--session ] [--boundary