Skip to content

Commit 91e44dc

Browse files
Grivnclaude
andcommitted
NanoClaw skill: add lifecycle hook injection (prime/remind/nudge/compact)
Add phases 2d and 2e to /add-mnemon skill: four hook scripts at container/hooks/mnemon/ (SessionStart, UserPromptSubmit, Stop, PreCompact), Dockerfile COPY directive, and settings.json hook registration in container-runner.ts. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f36a883 commit 91e44dc

1 file changed

Lines changed: 99 additions & 4 deletions

File tree

internal/setup/assets/nanoclaw/SKILL.md

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,99 @@ Adapt the mount syntax to match the existing pattern in `container-runner.ts` (i
104104

105105
**Important**: The `mkdirSync` call ensures the per-group mnemon directory exists on the host before the container starts, preventing mount failures.
106106

107+
### 2d. Add lifecycle hook scripts
108+
109+
Create `container/hooks/mnemon/` with four shell scripts. These run inside the container at Claude Code lifecycle events to actively drive memory operations.
110+
111+
**File**: `container/hooks/mnemon/prime.sh`
112+
113+
```bash
114+
#!/bin/bash
115+
# mnemon SessionStart hook — report memory stats on session init.
116+
STATS=$(mnemon status 2>/dev/null)
117+
if [ -n "$STATS" ]; then
118+
INSIGHTS=$(echo "$STATS" | sed -n 's/.*"total_insights": *\([0-9]*\).*/\1/p' | head -1)
119+
EDGES=$(echo "$STATS" | sed -n 's/.*"edge_count": *\([0-9]*\).*/\1/p' | head -1)
120+
echo "[mnemon] Memory active (${INSIGHTS:-0} insights, ${EDGES:-0} edges)."
121+
else
122+
echo "[mnemon] Memory active."
123+
fi
124+
```
125+
126+
**File**: `container/hooks/mnemon/user_prompt.sh`
127+
128+
```bash
129+
#!/bin/bash
130+
# mnemon UserPromptSubmit hook — remind agent to evaluate recall/remember.
131+
echo "[mnemon] Evaluate: recall needed? After responding, evaluate: remember needed?"
132+
```
133+
134+
**File**: `container/hooks/mnemon/stop.sh`
135+
136+
```bash
137+
#!/bin/bash
138+
# mnemon Stop hook — remind agent to consider remember after responding.
139+
INPUT=$(cat)
140+
MSG=$(echo "$INPUT" | jq -r '.last_assistant_message // ""' 2>/dev/null)
141+
if echo "$MSG" | grep -qiE "mnemon remember|sub-agent.*remember|Stored.*imp="; then
142+
exit 0
143+
fi
144+
echo "[mnemon] Consider: does this exchange warrant a remember sub-agent?"
145+
```
146+
147+
**File**: `container/hooks/mnemon/compact.sh`
148+
149+
```bash
150+
#!/bin/bash
151+
# mnemon PreCompact hook — save key insights before context compaction.
152+
echo "[mnemon] Context compaction starting. Review this session and remember the most valuable insights (up to 5) before context is compressed. Delegate to Task sub-agents now."
153+
```
154+
155+
Make all scripts executable: `chmod +x container/hooks/mnemon/*.sh`
156+
157+
### 2e. Copy hooks into container and register in settings.json
158+
159+
**File**: `container/Dockerfile`
160+
161+
Add after the mnemon binary install block:
162+
163+
```dockerfile
164+
# Copy mnemon hook scripts
165+
COPY hooks/mnemon/ /app/hooks/mnemon/
166+
RUN chmod +x /app/hooks/mnemon/*.sh
167+
```
168+
169+
**File**: `src/container-runner.ts`
170+
171+
In the block where `settings.json` is created for each group session (look for `writeFileSync` with `settings.json`), merge mnemon hooks into the settings object:
172+
173+
```typescript
174+
// Register mnemon lifecycle hooks
175+
const mnemonHooks = {
176+
SessionStart: [{
177+
hooks: [{ type: 'command', command: '/app/hooks/mnemon/prime.sh' }]
178+
}],
179+
UserPromptSubmit: [{
180+
hooks: [{ type: 'command', command: '/app/hooks/mnemon/user_prompt.sh' }]
181+
}],
182+
Stop: [{
183+
hooks: [{ type: 'command', command: '/app/hooks/mnemon/stop.sh' }]
184+
}],
185+
PreCompact: [{
186+
hooks: [{ type: 'command', command: '/app/hooks/mnemon/compact.sh' }]
187+
}],
188+
};
189+
190+
// Merge into existing settings.hooks (preserve any existing hooks)
191+
const existingHooks = settings.hooks || {};
192+
for (const [event, entries] of Object.entries(mnemonHooks)) {
193+
existingHooks[event] = [...(existingHooks[event] || []), ...entries];
194+
}
195+
settings.hooks = existingHooks;
196+
```
197+
198+
Adapt this to match the existing settings.json construction pattern in `container-runner.ts`.
199+
107200
---
108201

109202
## Phase 3: Setup
@@ -154,8 +247,10 @@ Adapt the mount syntax to match the existing pattern in `container-runner.ts` (i
154247

155248
To remove mnemon from your NanoClaw installation:
156249

157-
1. Remove from Dockerfile: delete the `ARG MNEMON_VERSION` and `RUN ... mnemon` block
250+
1. Remove from Dockerfile: delete the `ARG MNEMON_VERSION` + `RUN ... mnemon` block and the `COPY hooks/mnemon/` line
158251
2. Remove container skill: `rm -rf container/skills/mnemon/`
159-
3. Remove volume mounts from `src/container-runner.ts`: delete the mnemon mount blocks
160-
4. Rebuild: `./container/build.sh`
161-
5. (Optional) Remove data: `rm -rf ~/.mnemon/data/`
252+
3. Remove hook scripts: `rm -rf container/hooks/mnemon/`
253+
4. Remove volume mounts from `src/container-runner.ts`: delete the mnemon mount blocks
254+
5. Remove hooks registration from `src/container-runner.ts`: delete the mnemon hooks merge in settings.json
255+
6. Rebuild: `./container/build.sh`
256+
7. (Optional) Remove data: `rm -rf ~/.mnemon/data/`

0 commit comments

Comments
 (0)