Imported from wl1650918245/codex-memory-guard (
SKILL.md). Install upstream withnpx skills add wl1650918245/codex-memory-guard. Copyright stays with the author.
Codex Memory Guard
Keep tasks small enough to finish before compaction when practical. Persist confirmed critical state when it appears, keep the recovery checkpoint compact, and preserve a raw transcript reference as the final fallback.
This Skill does not prevent compaction or promise lossless retention of every chat detail. It makes confirmed critical decisions, constraints, verified lessons, blockers, and execution state durable, reviewable, and traceable.
Workflow
- Apply the task-boundary state machine when the current goal is unfinished. Compare the new input with the current goal before estimating its duration.
- Inspect the target project for existing
AGENTS.md, memory files, and.codex/hooks.json. - Run
scripts/initialize-memory-system.ps1for the target root. It creates all four memory layers when missing and preserves existing content. - Run
scripts/install.ps1only after the user authorizes global installation. It installs the Hooks and safely upserts a marked policy block in the user's globalAGENTS.mdwithout replacing other rules. - Tell the user to review and trust the new handlers through
/hooks; never claim they are active before trust is confirmed. - Verify with
scripts/test.ps1in an isolated temporary directory. The test covers multi-task and legacy HANDOFF validation, bounded post-HANDOFF transcript delta review, legacy pending compatibility, scope isolation, ready and recovery-required resume paths, pending closure and cleanup, installation, and scoped uninstall. - After a real compaction, inspect
memory/inbox/pending-*.jsonand the shortHANDOFF.md; review the bounded transcript delta after HANDOFF's recorded last write before closure, then widen transcript search only when the checkpoint is insufficient or conflicting.
Task boundary policy
Use these states: ACTIVE, BOUNDARY_CANDIDATE, CONTINUE_CURRENT, SWITCH_RECOMMENDED, and COMPLETED.
For each new input while the state is ACTIVE:
- Identify the single current goal from the conversation and short
HANDOFF.mdwhen present. - Decide whether the input directly advances that goal.
- If it does not, decide whether it is still a goal-related clarification, objection, correction, implementation adjustment, same-deliverable addition, or result check.
- If neither applies and the input can open an independent discussion or action direction, enter
BOUNDARY_CANDIDATEand give one lightweight reminder. Do this even when the new topic might end within one or two turns; do not require the user to label it as a test.
Treat the following as additional boundary evidence:
- the project or working directory changes;
- the final deliverable or acceptance criteria change;
- two unrelated current goals would be needed in
HANDOFF.md; - the unfinished current task would be displaced by another multi-turn task;
- the new work has materially different permissions, risks, or side effects.
Ask whether the user wants a separate Codex task; do not create one without an explicit request. If the user stays, enter CONTINUE_CURRENT for that boundary and do not repeat the reminder unless a distinct boundary appears. If the user chooses a separate task, enter SWITCH_RECOMMENDED. When the current goal is closed, enter COMPLETED and do not warn merely because the next topic differs.
The short-tangent exemption applies only when the tangent is directly related to the current goal. A short but clearly unrelated topic is still a boundary candidate. Do not persist routine state labels; record only a boundary choice needed for reliable resume. For the normative transition table and examples, read references/task-boundary-state-machine.md when implementing, auditing, or testing boundary behavior.
Memory policy
- Treat the transcript as evidence, not default model context.
- Write confirmed critical information when it appears; do not wait for compaction. Explicit phrases such as "remember this", "this is final", "use this from now on", or equivalent require immediate persistence and a read-back confirmation with the destination path.
- Critical information includes confirmed decisions, user constraints, acceptance criteria, verified lessons, active blockers, exact state required to resume, and facts whose loss would cause rework or duplicate side effects.
- Keep exactly one
HANDOFF.mdper project. It contains only started, unfinished work that must survive compaction or a window change; the complete backlog belongs in GitHub Issues or the project's chosen tracker. - Do not impose a fixed active-task count. For each active task keep a short title, one or two sentences of background, current verified state, one next action, and optional Issue/Spec/report pointer, up to three key files, and the latest relevant Session ID as an evidence locator only. Target 1000-2000 Chinese characters and enforce a 3000-character hard limit.
- Before editing
HANDOFF.md, reread the latest file and update only the matching task entry. Read back after writing; if the file changed concurrently, reread and merge instead of overwriting other tasks. Remove completed, cancelled, or no-longer-resumable tasks promptly. - Use
DECISIONS.mdfor confirmed decisions with source and invalidation conditions. - Use
LESSONS.mdonly for practices verified in real work. - Route unconfirmed observations to
memory/YYYY-MM-DD.md. - Create all four layers, but load only
HANDOFF.mdby default. Read other layers only when the current question needs them. - Never store credentials, tokens, or secrets in memory artifacts.
- Preserve conflicting history and mark supersession; do not silently rewrite it.
- Durable entries use explicit lifecycle states when applicable:
active,superseded,archived, orrevoked. Onlyactiveentries participate in default recovery. - Read back every critical-memory write and verify content, source, scope, status, and invalidation or review conditions.
Hook behavior
PreCompactvalidatesHANDOFF.mddeterministically before writing the pending record. The record includes layout, active-task count, structure and length status, issues, last-write time, SHA-256, project scope, session and transcript evidence pointers, andrecovery_required. It does not copy transcript contents or perform semantic summarization.- Hooks are opt-in per initialized project: if no ancestor contains
MEMORY_WORKFLOW.md, they return without creating files. SessionStartmatchingcompactverifies project root, session, HANDOFF path and transcript pointer before trusting the record. It reads HANDOFF first, then reviews only transcript entries after the recorded HANDOFFlast_write_atthrough the pre-compaction turn so a structurally valid but semantically stale checkpoint cannot silently skip last-minute confirmed state. A failed checkpoint or conflicting delta may require a wider targeted transcript repair.- After recovery, run
scripts/close-pending.ps1with the actual sources used. Schema v4 closure requires-DeltaCheckedand records the delta turn count, critical updates found, whether HANDOFF changed,processed_at, repair status, resolved issues and final HANDOFF SHA-256. Legacy schema v3awaiting_extractionrecords remain compatible.scripts/cleanup-pending.ps1is dry-run by default and may delete only processed records older than its explicit retention threshold when-Applyis supplied. - Keep both hooks synchronous. The default installation timeout is 10 seconds because they perform local small-file operations only.
- The command hook does not perform semantic extraction itself. Critical state should already be written through when confirmed; the deterministic pre-check and post-compaction closure are the fallback.
Inspect the bounded post-HANDOFF transcript delta:
powershell -NoProfile -ExecutionPolicy Bypass -File scripts/get-transcript-delta.ps1 -PendingPath C:\path\to\pending.json
The reader returns only user/assistant messages after the recorded HANDOFF write and through pending capture time, capped at 12 messages and 12000 characters by default. If truncated is true, continue with a targeted search before closure.
Deliberate boundaries
Keep this Skill a lightweight, deterministic task-continuity guard rather than turning it into a full memory platform.
- The bounded transcript delta is a narrow stale-checkpoint safeguard. The Skill does not continuously infer semantic freshness from Git history, remote releases, issue trackers, file meaning, or every conversation turn; those probes add latency and still cannot prove that a checkpoint is semantically complete.
- Long-term semantic recall through Codex memories, OpenViking, Trellis, vector databases, hybrid search, cloud storage, or custom MCP servers has been considered but is not implemented or required. Those systems solve broader cross-session retrieval and may be layered alongside this Skill when their privacy, operating cost, failure modes, and retrieval quality are acceptable.
- Do not treat recalled backend content as confirmed project truth. HANDOFF remains the small resumable execution checkpoint; confirmed decisions and verified lessons remain reviewable project files; transcript and Git remain evidence.
- Preserve offline degradation: if an optional memory backend is unavailable, the core HANDOFF, pending record, bounded transcript review, and closure workflow must still work.
There is currently no backend adapter contract. Do not imply that installing this Skill enables semantic search, vector storage, cross-project recall, or automatic knowledge extraction.
Validate a checkpoint:
powershell -NoProfile -ExecutionPolicy Bypass -File scripts/validate-handoff.ps1 -Path C:\path\to\project\HANDOFF.md
Close a processed recovery record:
powershell -NoProfile -ExecutionPolicy Bypass -File scripts/close-pending.ps1 -PendingPath C:\path\to\pending.json -SourcesUsed HANDOFF.md,'bounded transcript delta' -DeltaChecked -DeltaTurnCount 4 -CriticalUpdatesFound 1 -HandoffUpdatedFromDelta
Preview cleanup of processed records older than 14 days:
powershell -NoProfile -ExecutionPolicy Bypass -File scripts/cleanup-pending.ps1 -ProjectRoot C:\path\to\project
Commands
Initialize one project:
powershell -NoProfile -ExecutionPolicy Bypass -File scripts/initialize-memory-system.ps1 -Root C:\path\to\project -SingleProject
Initialize every immediate child project:
powershell -NoProfile -ExecutionPolicy Bypass -File scripts/initialize-memory-system.ps1 -Root C:\path\to\projects
Install global hooks and optionally initialize a project:
powershell -NoProfile -ExecutionPolicy Bypass -File scripts/install.ps1 -ProjectRoot C:\path\to\project
Remove only this Skill's global hooks:
powershell -NoProfile -ExecutionPolicy Bypass -File scripts/uninstall.ps1
Uninstall also removes only the marked codex-memory-guard block from global AGENTS.md; it preserves all other global rules and project memory files.
For schemas and acceptance checks, read references/memory-contract.md.
