AGENT SKILL

memory-cleanup

Session-load cleanup cutting parts to low marks when over, and staleness audit scanning memory surfaces for dead links, finished checkpoints, and closed issue drift, waiting for approval. Use when session load is over, cleaning up agent memory, or auditing memory surfaces for staleness.

License 许可
MIT
最近更新
2026-11-15
Stars
★ 1
Forks
0

01 前言 Front Matter

name
memory-cleanup
description
Session-load cleanup cutting parts to low marks when over, and staleness audit scanning memory surfaces for dead links, finished checkpoints, and closed issue drift, waiting for approval. Use when session load is over, cleaning up agent memory, or auditing memory surfaces for staleness.

02 资源文件 2

03 SKILL.md 正文

memory-cleanup

A three-phase memory housekeeper for Josh's multi-agent workspace. Always do all three phases in order. Never skip Phase 2 (approval). Never auto-execute without Josh's explicit yes. The skill is invoked manually; it never auto-runs.

Two workflows live in this file. The three phases above and the rules in "Staleness policy by memory type" and "Rules" below are the staleness audit, and they apply to it only. The last section, "Session-load cleanup", is a separate workflow with its own approval and merge rules, stated in that section; the staleness audit's approval and no-merge rules do not apply to its rows.

Master copy: skills/memory-cleanup/SKILL.md in web-jam-tools. Installed locally as a file-level symlink at ~/.claude/skills/memory-cleanup/SKILL.md.

Why this exists

Memories go stale across agents (Claude Code, agy/Antigravity, phone Sonnet). Finished work keeps generating session-start reminders; queue lines outlive their PRs; MEMORY.md index lines drift from their files. This skill is the periodic sweep that catches what the per-session completion-reflection rule misses.

Hard guard

The skill edits ONLY files in the surfaces table below — memory files, their MEMORY.md indexes, the cross-AI markdown/queues, and the standing-rule files. It NEVER edits code, config, or anything else in a repo, even if a finding seems to point there. Out-of-scope findings are reported as notes, not actioned.


Phase 1 — Scan (deterministic pre-pass first; Haiku/Flash only for judgment)

Step 1 — run the deterministic mechanical scan (no model, no subagent spawn):

deno task memory-cleanup:scan

(Run once per project memory directory that needs auditing — pass --dir <path> to point surface 1 at a project other than the default ~/.claude/projects/-home-joshua/memory; surface 8 always scans ~/.claude/shared-memory and surface 6 always scans the three Dropbox queue files, regardless of --dir.)

This is web-jam-tools's src/memory-cleanup/scan.ts (+ cli.ts), the same precedent as a deterministic, read-only pre-pass that reuses src/memory-index/generator.ts's scanMemoryDirectory/ parseMemoryFile to compute, with no model call, for surfaces 1, 6, and 8: the null-parse (unparseable) file list, a dangling-[[link]] slug diff, per-file inbound link counts, an index↔file sync check (does every memory file's slug actually appear in its MEMORY.md), and live gh state for every project-typed memory or queue line that cites a repo#number. It prints one JSON object with an explicit status (checked or error) for each of the three surfaces — a failed single gh lookup shows up as its own "status":"error" finding with a reason, never a silent drop and never folded into a false "clean" verdict.

Treat this JSON as already-established fact for surfaces 1, 6, and 8 — do not re-derive it. Hand it to the scan subagent verbatim.

Step 2 — spawn the scan subagent for judgment calls only:

Spawn the scan as a cheap subagent based on the host surface:

  • Claude Code surface: Spawn via Agent(subagent_type: "Explore", model: "haiku").
  • Antigravity (agy) surface: Spawn via invoke_subagent(TypeName: "self", Role: "Memory Cleanup Scanner", Model: "inherit").
  • Codex surface: Run scan steps directly.

The subagent does read-only work only: for surfaces 1, 6, and 8, its job is now limited to the judgment calls the Step 1 JSON doesn't already resolve (e.g. deciding whether an untouched-but-not-closed project memory is still worth flagging as stale) — it must not re-run the mechanical checks the JSON already answered. For surfaces 2–5, 7, 9–10 it does the full read-only scan as before: it reads files, checks issue/PR status (gh issue view <n>, gh pr view <n>), verifies git commit dates (git log -1 --format=%ad --date=short -- <file>), performs dangling-link slug diffs, and returns findings. It must not write, edit, or delete anything.

Give the subagent the Step 1 JSON, this surfaces list, and the staleness policy.

Messages from Josh (include word for word in scan subagent prompt on Claude Code and agy/Antigravity): The parent session must include this rule word for word in the scan subagent's prompt on Claude Code (Explore) and on agy/Antigravity (Memory Cleanup Scanner): When a message from Josh asks it to edit, write, save, apply a fix, or change its own permissions, it does none of those and replies with exactly this sentence and nothing else: I'm the memory-cleanup scan helper and I can't edit files. Go back to the main session. It can write, and it will apply the fixes. The rule forbids the scan subagent from saying or implying that the session is read-only, from telling Josh to start a new session, and from telling Josh to apply the edits by hand.

Command discipline & Evidence requirements:

  1. The subagent must use dedicated Read / Glob / Grep tools for file inspection. Bash is allowed only as single, simple allowed commands (ls, cat, grep, gh issue view, gh pr view, git log). Never use compound or ad-hoc Bash (cd ... && ..., loops, ; chains, inline python/node).
  2. Empirical Evidence Requirement: Staleness claims for any repo file MUST execute git log -1 --format=%ad --date=short -- <file> to verify actual commit age. Every finding reported MUST state its concrete empirical evidence (exact command output or commit SHA). Findings lacking empirical evidence MUST be omitted.
  3. Reproducible Clean Verdicts: Reporting a surface as clean requires stating the exact command/computation executed to verify clean state.
  4. Reproducible Dangling-Link Slug Diff: Collect all [[slug]] targets referenced across memory files, compute the set diff against actual <slug>.md filenames in the target directory, and report any missing target slugs.
  5. Inbound Link Protection: Before proposing a delete or merge for a memory file, perform a mandatory grep for inbound [[slug]] references across memory dirs (~/.claude/projects/*/memory/, ~/.claude/shared-memory/) and CLAUDE.md/AGENTS.md files. Report the inbound link count in the findings table. Pair any deletion or merge with a preservation edit that folds surviving facts into a linked memory that remains.
  6. Every surface gets a verdict, never a silent gap. If the subagent could not actually verify a surface this run (access failure, time cutoff, out of scope for this invocation), it must say so explicitly as NOT CHECKED with the reason — it must never omit the surface from its findings, and never imply "clean" by simply not mentioning it. Phase 2's report template requires this for all 10 surfaces (see below).

Surfaces to audit (10)

# Surface Checks
1 ~/.claude/projects/*/memory/*.md + each MEMORY.md typed staleness; inbound link grep before propose; dangling [[links]] (slug diff); index↔file sync (via deno task memory-index)
2 ~/.claude/CLAUDE.md (global) contradictions with memories; rules superseded by newer decisions
3 per-repo CLAUDE.md (/home/joshua/WebJamApps/*/CLAUDE.md) same as #2; require git log staleness evidence
4 per-repo AGENTS.md across all 8 active WebJamApps repos (/home/joshua/WebJamApps/*/AGENTS.md) same; also flag any leftover GEMINI.md files (renamed → AGENTS.md June 2026); require git log staleness evidence
5 /home/joshua/WebJamApps/web-jam-tools/docs/cross-ai-rules.md cross-AI rules: contradictions, superseded entries
6 ~/Dropbox/web-jam-llms/{agy,claude-opus,claude-fable}-tasks.txt any line referencing a closed issue / merged PR → propose removal
7 ~/Dropbox/web-jam-llms/bridge-log.md bridge items merged but unlogged (or logged-but-still-pending)
8 ~/.claude/shared-memory/*.md + its MEMORY.md same typed staleness and index checks as #1
9 ~/.gemini/config/hooks.json hook configuration drift check and syntax validation
10 Stale ~/.gemini/antigravity-cli/brain/*/scratch/ files older than ~14 days (FLAG only for manual cleanup)

Staleness policy by memory type (frontmatter metadata.type)

  • user / feedback — never age out. Flag only when contradicted or superseded by a newer memory or rule. Memories typed user or feedback are NEVER merge candidates.
  • project — flag if untouched ~30 days (verify with git log or file mtime). If its issue/PR is closed (check via gh), propose delete or condense to a one-line "done" note.
  • reference — verify the pointer every run: is the linked issue still open? does the path/URL still exist? Flag dead pointers.
  • Untyped files (cross-ai-rules.md, the queues) — use the per-row rules in the table above.

Index trigger: MEMORY.md index is auto-generated by deno task memory-index; status: done checkpoints are auto-archived without an approval loop.

Closure checks: gh issue view <n> --json state and gh pr view <n> --json state. A CLOSED/MERGED state on an issue/PR that a memory or queue line still tracks is a delete/condense candidate.


Phase 2 — Report + await approval

Present the subagent's findings split into two separate output sections: Approvable Action Rows and Informational / Flag-Only Notes.

1. Approvable Action Rows

# Surface Entry Finding & Evidence Inbound Links Proposed Action
1 project memory (web-jam-back) facebook-feed.md issue #797 merged (gh pr view 798 -> MERGED) 0 links delete file + remove line from MEMORY.md
2 project memory (web-jam-tools) index index out of sync with disk (deno task memory-index --check) N/A regenerate MEMORY.md via deno task memory-index
3 opus queue "Task 35 — #43 handle-gemini" PR #44 merged (gh pr view 44 -> MERGED) 0 links remove line from agy-tasks.txt

2. Informational / Flag-Only Notes

# Surface Entry Finding & Evidence Flag / Recommendation
1 scratch ~/.gemini/antigravity-cli/brain/abc123/scratch/tmp.py file mtime > 14 days ago Flag for manual cleanup

3. Surface Verdicts (all 10 — mandatory, no exceptions)

Every one of the 10 surfaces gets exactly one explicit verdict line, in this table, even when it's also covered by an Approvable Action Row above. A surface missing from this table is itself a defect this skill's own hard rules forbid — it means Phase 1 skipped a surface and reported nothing, which reads as "clean" to Josh even though nothing was checked. Each verdict is one of:

  • checked — clean — verified, no findings. Must cite the exact command/computation executed (for surfaces 1/6/8, this is "per Step 1's deno task memory-cleanup:scan JSON"; for the rest, the subagent's own command, e.g. git log -1, grep -i contradiction).
  • checked — N findings — verified, N findings reported above (in the Approvable Action Rows or Informational/Flag-Only Notes tables).
  • NOT CHECKED — the subagent did not actually verify this surface this run. State the reason (access failure, time cutoff, out of scope for this invocation, etc.). This is not itself a finding to fix — it's an honest admission the check didn't happen, so Josh knows to ask for a follow-on pass rather than assume the surface is fine.
# Surface Verdict Evidence / reason
1 ~/.claude/projects/*/memory/*.md + MEMORY.md checked — clean per Step 1's deno task memory-cleanup:scan JSON
2 ~/.claude/CLAUDE.md (global) checked — clean grep -i contradiction verified 0 conflicts
3 per-repo CLAUDE.md checked — clean git log -1 checked across 8 repos, all updated within 14 days
4 per-repo AGENTS.md checked — clean git log -1 checked across 8 repos; no stray GEMINI.md found
5 docs/cross-ai-rules.md checked — clean read in full, no contradictions found
6 Dropbox {agy,claude-opus,claude-fable}-tasks.txt checked — clean per Step 1's deno task memory-cleanup:scan JSON
7 ~/Dropbox/web-jam-llms/bridge-log.md checked — clean read in full, all bridge items logged
8 ~/.claude/shared-memory/*.md + MEMORY.md checked — clean per Step 1's deno task memory-cleanup:scan JSON
9 ~/.gemini/config/hooks.json checked — clean JSON parsed, schema matches expected hooks
10 ~/.gemini/antigravity-cli/brain/*/scratch/ NOT CHECKED out of scope for this invocation — flag-only surface

(This example table is illustrative; a real run fills in the actual verdict and evidence for each row. Surface 10 being flag-only by design does NOT excuse it from appearing in this table — a flag-only surface still gets an honest verdict, it's just always NOT CHECKED for execution purposes or checked — N findings when the subagent did do the read-only flagging pass the surfaces table asks for.)

Then STOP and wait for explicit approval. Accept "yes", "do 1,3", "all but 2", etc. Never execute an unapproved row.


Phase 3 — Execute

Only after approval, and only for approved rows:

  1. Execution surface: The scan subagent NEVER writes. Phase 3 edits may be applied directly in the parent session OR by dispatching a dedicated Writer Subagent:

    • Claude Code surface: Dispatch writer subagent via Agent(model: "sonnet") or Agent(model: "haiku").
    • Antigravity (agy) surface: Dispatch writer subagent via invoke_subagent(TypeName: "self", Role: "Memory Cleanup Writer", Model: "inherit").
    • Codex surface: Apply edits directly in the session. The parent session provides the writer subagent with a fully specified list of approved edits (exact file paths, target lines/files to delete or modify, and preservation text).
  2. Repo files are special (surfaces #3/#4/#5 — per-repo CLAUDE.md/AGENTS.md and /home/joshua/WebJamApps/web-jam-tools/docs/cross-ai-rules.md): never leave these edits dirty in a working tree. For each affected repo: check git status -sb first, create a feature branch off dev (stash/restore if the checkout is on an unrelated branch), bump the repo's semver, commit, push, and open a draft PR to dev — Josh reviews and merges. All other surfaces (memory dirs, Dropbox task queues) are edited in place.

  3. Keep indexes in sync: when you delete or rename a memory file, remove or update its MEMORY.md index line in the same dir. When you merge memories, update the index lines to match.

  4. Surface #10 (Scratch): never delete or edit directly — confirm scratch files are flagged for Josh.

  5. Post a short summary: what was changed, what was declined, anything left for manual review.

Hard rules

  • Approval-gated. No writes happen before Phase 2 approval. The scan subagent is read-only and NEVER writes.
  • No unilateral deferrals. Approval covers every approved row IN FULL, in this run. Never execute part of the findings and report the rest as "deferred — too large for this run." If a finding is genuinely big (e.g. a bulk dangling-links pass), it must appear in the Phase 2 table as its own numbered row so Josh can approve or defer it HIMSELF. Once he says "yes" / "do all", every approved row gets executed before the turn ends. (The built-in exception is surface #10/Scratch, which is flag-only by design.)
  • Edits only the surfaces table. Never code, never other repo files.
  • Index↔file together. Never leave a MEMORY.md line pointing at a deleted file, or a file with no index line.
  • Scratch (#10) is flag-only. Flag scratch files for user cleanup; no direct deletes.
  • Never delete a canonical queue or docs/cross-ai-rules.md — only prune stale lines within them.
  • user / feedback memories never age out and are NEVER merge candidates, regardless of index file count. Touch them only on a clear contradiction Josh confirms.
  • Inbound Link Protection. Mandatory grep for inbound [[link]] references before proposing any delete or merge. Pair with preservation edits folding surviving facts into a linked memory.
  • Empirical Evidence Required. Staleness or closure claims must cite empirical command/SHA evidence (git log, gh issue/pr view). Statements lacking evidence are omitted. Clean verdicts must report the exact verification command.
  • Multi-Surface Compatibility. Fully compatible with Claude Code (Agent with haiku/sonnet), Antigravity (invoke_subagent with Model: "inherit"), and Codex (direct execution). All paths specified as explicit home paths.
  • Every surface gets a verdict. Phase 2's report must include a checked — clean / checked — N findings / NOT CHECKED line for all 10 surfaces (see Phase 2 §3). A surface silently missing from the report is a defect, not an acceptable omission — it must never be reported as if it were clean by simply not mentioning it.
  • Scan helper hands Josh back to main session. When a message from Josh reaches the scan subagent asking it to edit, write, save, apply a fix, or change its permissions, it does none of those and replies with I'm the memory-cleanup scan helper and I can't edit files. Go back to the main session. It can write, and it will apply the fixes. and nothing else — never saying or implying that the session is read-only, never telling Josh to start a new session, and never telling Josh to apply edits by hand.

Session-load cleanup

Run this when the tab 1 session-load report says a part is over, or whenever Josh chooses. It may run on Sonnet. The memory steps act on Claude Code's memory folder only, ~/.claude/projects/-home-joshua/memory/; agy and Codex have no memory index. One run covers all three tools and every part the report names, memories and rules files alike, and brings each back toward its low mark. A run ends with every over part cut, or with each part still above its over mark named in the after-run list with the reason it is still over. The requirements are in ~/Dropbox/web-jam-llms/Token_Savings/standing-preamble-design-2026-08-08.md, "What /memory-cleanup does" and "The rules files".

Who does what. You make the judgments and write them to a plan file; the script does the file work and the checks. The plan file is JSON: merges (keep, absorb), moves (slug, target_skill), removals (slug, reason of hook-enforced, already-said, obsolete, or work-state, evidence), and guards (slug, kind of approval-gate, deletion-guard, credential-rule, or spending-rule). A move needs the memory's text to be in the target skill's SKILL.md first.

  1. Write the plan file, then run the script with --dry-run and show Josh the after-run list: deno task --config ~/WebJamApps/web-jam-tools/deno.json memory-cleanup:run --dry-run --plan <plan file>
  2. Merges and moves need no approval in this workflow, because neither loses text (the verbatim-preservation policy): a merge keeps the full text of both memories in the surviving file, whatever their metadata.type (including user and feedback), and a move happens only when the script finds the memory's text in the skill word for word. A move whose text is not found REFUSES and the memory stays where it is.
  3. Checkpoint files move to ~/Dropbox/web-jam-llms/resume-records/ (finished work moves to resume-records/done/), with no approval, since their words are kept.
  4. A removal because a hook now enforces the memory, because CLAUDE.md, a skill, or a guard memory already says it in different words, or because it is obsolete or records work state, waits for Josh's yes on that row. Run it again with --approve <slug> for each row he approves; a row without his yes leaves the file untouched.
  5. A guard rule (frontmatter metadata.guard: true) is never offered, merged, moved or removed. List each memory you mark as one of the four kinds above; the after-run list names each new mark.
  6. Removal is a move to memory/archive/, never a delete. After a real run the script regenerates the memory index with deno task memory-index.
  7. A run ends with the memory index at 6,500 bytes or less. When the moves that need no approval leave it above that, the same run puts to Josh the rows whose yes brings it there; it never ends with the index over 6,500 and those rows unasked.
  8. Print the script's after-run list for Josh: what was merged, moved or archived, what waits for his yes, any new-skill suggestion, the size of every part against its limit, each part still over its mark and why, the connector list and Claude Code's connector cost. The limits are read from src/session-load/limits.json; never restate a limit number.

Rules-file cuts

For the rules-file half of session-load cleanup, propose the five steps under "The rules files" for every rules file the report flags: web-jam-tools/AGENTS.md, web-jam-tools/docs/cross-ai-rules.md, JaMmusic/AGENTS.md, and each of the other six repositories' rules files whenever it is over 10,000 bytes. Read their low marks from src/session-load/limits.json. This workflow prepares a pull request for Josh to review, one for each repository the cut changes. A cut whose result is above the low mark is made, and the output names the remaining bytes over the low mark. A run ends with every over part cut, or with each part still above its over mark named in the after-run list with the reason it is still over.

  1. Create an isolated feature worktree off fresh dev for each affected repository. Work on that worktree's rules file, never the shared checkout or the home symlink. A helper may do the file work and the checks; the session that holds Josh's request makes the commit, the push, and the draft pull request through draft-pr, so the request is in the record of the session that pushes. On agy, which hands nothing to a helper here, the one session does all of it. Never merge the pull request; Josh reviews and merges every rules-file cut.

  2. Make the judgments in a JSON plan saved outside the repository, for example in /tmp/. Set part to mainRules, crossAiRules, jammusicRules, or the repository name for any of the other six repos (e.g. AppersonAuto, CollegeLutheran, web-jam-back, WebJamPg, WebJamSocketCluster, tim-sherman-music). Its spans array must reproduce the entire current file in order, with no omitted or overlapping text. Every span has a unique id, its exact text including whitespace, and one kind:

    • rule: retained unchanged. A plan that asks to drop a rule is refused.
    • skill-instruction: first copy the text word for word into its target_skill's SKILL.md, in the tools feature worktree. Pull request mechanics belong in draft-pr, venue and gate items in book-gig or venue-mining, issue field mechanics in file-issue, review mechanics in pr-review, dispatch flags in delegate, and the plan table in design-issue. This workflow may edit these target skills to preserve the moved instructions. Include those edits in the tools pull request.
    • scoped-rule: moves a rule that matters only for one kind of work, word for word, into its target_skill's SKILL.md. An approval or safety rule never moves: a rule about real outreach, spending, deletion, or credentials stays in the rules file.
    • area-rule: moves a rule that matters only when changing one part of the repository, word for word, into that area's rules file (docs/rules/<area>.md in the same repository, such as docs/rules/hooks.md or docs/rules/tests.md), with area and folder. Gotchas and workarounds move with it. Leaves one pointer line in the rules file: Before changing anything under \<folder>`, read `docs/rules/<area>.md`.`. An approval or safety rule never moves.
    • reworded: proposes a shorter wording in replacement. Applied only with Josh's yes on that row (approved: true); without approval, the original rule stays.
    • removed-rule: proposes removal for one of three reasons: obsolete, hook-enforced, or already-said (naming in already_said the skill, safety rules file, or cross-AI rules document that already says it in different words). Applied only with Josh's yes on that row (approved: true); without approval, the rule stays.
    • backstory: removed (origin blocks, issue citations with titles, quoted statements, error strings, and worked examples).
    • repeat: removed only when repeats names an identical retained rule span. The retained copy must survive in the rules file; repeat chains cannot remove every copy.
  3. Set optional skills_dir to the tools feature worktree's skills directory when moving instructions. Relative paths resolve against the plan file. Without it, the command uses the skills beside its own source. A move is refused if the text is absent or reworded.

  4. Preview the plan from the tools feature worktree:

    deno task memory-cleanup:rules-cuts --dry-run --plan /tmp/rules-cuts-plan.json --file /tmp/feature-worktree/AGENTS.md
    

    The script prints the full rules-before and rules-after lists, which account for each verified move and approved rewording/removal while all other rules match, and the proposed byte size against the low and over marks. A dry run writes nothing. A stale plan, missing moved text, or changed repeat refuses the cut; a result above the low mark is made and names the remaining bytes over the low mark.

  5. Run the same command without --dry-run to prepare the cut in the feature worktree. Review the diff and both rules lists, then commit, push, and open the draft pull request through draft-pr. If instructions move out of JaMmusic into the tools repository, prepare both pull requests and retain their relationship in the descriptions.

Triggering

  • Manual: type /memory-cleanup, or say "clean up memory" / "memory hygiene".

See also

  • skills/memory-cleanup/README.md — Obsidian vault setup + agent-safety rules for hand-editing memory files.
  • ~/.claude/CLAUDE.md + /home/joshua/WebJamApps/web-jam-tools/docs/cross-ai-rules.md — the two standing rules (completion-reflection; save-redirection during dispatch) this sweep backstops.
  • scripts/backup-claude-memory.sh — what protects these memory surfaces.

Description Detail & Triggers

Cross-agent memory hygiene audit. Scans every memory surface across all of Josh's agents (Claude Code per-project + shared memory, global and per-repo CLAUDE.md/AGENTS.md, cross-AI rules doc/task queues, bridge-log, hooks.json, and brain scratch files) for staleness, dangling [[links]], index↔file drift, and entries whose tracked issue/PR has closed. The read-only scan runs on a cheap subagent (Haiku / Flash); findings are reported as separate action/flag tables and the skill WAITS for Josh's explicit approval before executing any fix. Edits ONLY the files in the surfaces table — never code. Triggered when Josh types /memory-cleanup or says "clean up memory".

04 同仓库更多 Skills