{"id":"add-mnemon","name":"add-mnemon","summary":"mnemonを使って永続的なグラフベースのメモリを追加しましょう。エージェントは応答前に過去のコンテキストを思い出し、各ターン後に洞察を記憶します。","body":"# Add Mnemon — Persistent Memory\n\nInstalls [mnemon](https://github.com/mnemon-dev/mnemon) in the agent container image. On each container start, `mnemon setup` registers Claude Code hooks that surface relevant memory before the agent responds and store new insights after each turn. Memory is written to the per-agent-group `.claude/` mount and survives container restarts.\n\n## Provider Compatibility\n\nmnemon hooks fire only under `--target claude-code`. Use this skill on agent groups that run the default Claude provider. The provider is the materialized `provider` key in each group's `container.json` (absent or `claude` = default Claude provider). Confirm it before applying:\n\n```bash\ngrep -H '\"provider\"' groups/*/container.json 2>/dev/null   # no match, or \"provider\": \"claude\" = Claude\n```\n\nIf a group sets a different provider (e.g. `\"provider\": \"opencode\"`), it spawns its own process and never invokes the `claude` CLI, so the hooks registered by `mnemon setup` do not run for that group.\n\n## Phase 1: Pre-flight\n\n### Check if already applied\n\n```bash\ngrep -q 'MNEMON_VERSION' container/Dockerfile && echo \"Already applied\" || echo \"Not applied\"\n```\n\nIf already applied, re-run Phase 2 anyway — every step is idempotent and skips work that is already in place — then continue to Phase 3 (Verify).\n\n### Check latest mnemon version\n\n```bash\ncurl -fsSL https://api.github.com/repos/mnemon-dev/mnemon/releases/latest | grep '\"tag_name\"'\n```\n\nNote the version (e.g. `v0.1.1`) — use it as `MNEMON_VERSION` in the next step.\n\n## Phase 2: Apply Changes\n\n### 1. Dockerfile — install mnemon binary\n\nInsert the mnemon block immediately above the `# ---- Bun runtime` section of `container/Dockerfile` (skip if `grep -q 'MNEMON_VERSION' container/Dockerfile` already matches):\n\n```dockerfile\n# ---- mnemon — persistent agent memory ----------------------------------------\nARG MNEMON_VERSION=0.1.1\nRUN ARCH=$(dpkg --print-architecture) && \\\n    curl -fsSL \"https://github.com/mnemon-dev/mnemon/releases/download/v${MNEMON_VERSION}/mnemon_${MNEMON_VERSION}_linux_${ARCH}.tar.gz\" \\\n    | tar -xz -C /usr/local/bin mnemon && \\\n    chmod +x /usr/local/bin/mnemon\n\nENV MNEMON_DATA_DIR=/home/node/.claude/mnemon\n```\n\n`MNEMON_DATA_DIR` points into the per-agent-group `.claude/` mount, so memory persists across container restarts.\n\n### 2. Entrypoint — run mnemon setup on each container start\n\n`mnemon setup` is idempotent. Run it once per `container/entrypoint.sh`. First check whether the line is already present:\n\n```bash\ngrep -q 'mnemon setup' container/entrypoint.sh && echo \"Already wired\" || echo \"Wire it\"\n```\n\nIf it prints `Wire it`, add the setup call right after `set -e`, before the `cat` that captures stdin, so the result looks like:\n\n```bash\n#!/bin/bash\n# NanoClaw agent container entrypoint.\n#\n# ...existing header comment...\n\nset -e\n\nmnemon setup --target claude-code --yes --global >/dev/stderr 2>&1\n\ncat > /tmp/input.json\n\nexec bun run /app/src/index.ts < /tmp/input.json\n```\n\n`>/dev/stderr 2>&1` routes all mnemon output to stderr (docker logs) so it doesn't interfere with the JSON stdin handshake between host and agent-runner.\n\n### 3. Copy the integration tests\n\nBoth reach-ins are into container build/runtime files that aren't importable or typed (a GitHub-release binary in the Dockerfile, a shell line in the entrypoint), so structural tests guard them. Copy them into the host test tree:\n\n```bash\ncp .claude/skills/add-mnemon/mnemon-dockerfile.test.ts src/mnemon-dockerfile.test.ts\ncp .claude/skills/add-mnemon/mnemon-entrypoint.test.ts src/mnemon-entrypoint.test.ts\npnpm exec vitest run src/mnemon-dockerfile.test.ts src/mnemon-entrypoint.test.ts\n```\n\n`mnemon-dockerfile.test.ts` asserts the `MNEMON_VERSION` ARG and `MNEMON_DATA_DIR` ENV are present (red if the install layer is dropped on an upgrade). `mnemon-entrypoint.test.ts` asserts the entrypoint invokes `mnemon setup --target claude-code` (red if the wiring is removed).\n\n### 4. Rebuild and smoke-test the image\n\n```bash\n./container/build.sh\ndocker run --rm --entrypoint mnemon nanoclaw-agent:latest --version\n```\n\n## Phase 3: Restart and Verify\n\n### Restart the service\n\nRun from your NanoClaw project root:\n\n```bash\nsource setup/lib/install-slug.sh\nsystemctl --user restart $(systemd_unit)              # Linux\n# launchctl kickstart -k gui/$(id -u)/$(launchd_label)   # macOS\n```\n\n### Confirm mnemon hooks are registered\n\nAfter the next container starts, check that setup ran:\n\n```bash\ndocker logs $(docker ps --filter label=nanoclaw-session --format \"{{.Names}}\" | head -1) 2>&1 | grep -i mnemon\n```\n\nThen inspect the hooks inside the running container:\n\n```bash\ndocker exec $(docker ps --filter label=nanoclaw-session --format \"{{.Names}}\" | head -1) \\\n  cat /home/node/.claude/settings.json | grep -A5 mnemon\n```\n\n### Test memory recall\n\nHave a conversation with the agent, then start a new session and reference something from the earlier one. Mnemon should surface the relevant context automatically without you restating it.\n\n## Memory Storage\n\nMnemon writes to `/home/node/.claude/mnemon/` inside the container, which maps to the per-agent-group `.claude/` directory on the host. To find the exact host path:\n\n```bash\ndocker inspect $(docker ps --filter label=nanoclaw-session --format \"{{.Names}}\" | head -1) \\\n  --format '{{range .Mounts}}{{if eq .Destination \"/home/node/.claude\"}}{{.Source}}{{end}}{{end}}'\n```\n\nTo reset all memory for an agent, stop the container and delete the `mnemon/` subdirectory from that host path.\n\n## Troubleshooting\n\n### `mnemon: command not found` in container\n\nThe image wasn't rebuilt after adding the Dockerfile layer. Run `./container/build.sh` and restart.\n\n### Memory not persisting across restarts\n\nVerify `MNEMON_DATA_DIR` resolves to a mounted path (not an in-container ephemeral directory):\n\n```bash\ndocker exec <container> sh -c 'ls -la $MNEMON_DATA_DIR'\n```\n\nIf the directory is empty after conversations, the mount is missing or the path is wrong. Check the host mount with the `docker inspect` command above.\n\n### Agent not using past memory\n\n`mnemon setup` writes hooks into `/home/node/.claude/settings.json`. Verify:\n\n```bash\ndocker exec <container> cat /home/node/.claude/settings.json\n```\n\nIf the hooks are absent, `mnemon setup` may have failed silently. Check container startup logs for errors from mnemon.\n\n### Setup fails at container start\n\nRun setup manually inside a running container to see the full error:\n\n```bash\ndocker exec -it <container> mnemon setup --target claude-code --yes --global\n```","author":"@nanocoai","ownerProfile":null,"authorContacts":null,"sourceUrl":"https://github.com/nanocoai/nanoclaw/tree/main/.claude/skills/add-mnemon","license":"MIT","category":null,"lang":"en","tokens":1660,"stars":0,"calls30d":1,"claimed":false,"visibility":"public","origin":"crawler","version":"0.1.0","createdAt":"2026-08-22","updatedAt":"2026-08-22","files":[{"path":"mnemon-dockerfile.test.ts","size":1278,"sha256":"d1663ee7949b8ba5515034f8acadad111d48650d997e8e32529c132bc264f6e6"},{"path":"mnemon-entrypoint.test.ts","size":985,"sha256":"6755b83295ec6382dc7564609b42807c6970856ecc87be033868aedbcfb1877f"},{"path":"REMOVE.md","size":1900,"sha256":"28b10cf91bc103dc38dab39b8818c6e9869feffdbdc10f20f1fe6c3fd116bd07"}],"requires":{"mcp":[],"tools":[]},"safety":{"flags":[{"code":"net.endpoints","kind":"exfiltration","excerpt":"api.github.com","message":"bundled scripts reach 1 external host(s)","severity":"warn"}],"scannedAt":"2026-08-22","hasScripts":true,"networkEndpoints":["api.github.com"]}}