Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ case "memory_your_tool": {
```

### Hook Scripts
Hook scripts in `src/hooks/` are standalone Node.js scripts (no iii-sdk import). They read JSON from stdin, make HTTP calls to the REST API, and exit. Always use `try/catch` with `AbortSignal.timeout()` for best-effort calls.
Hook scripts in `src/hooks/` are standalone Node.js scripts (no iii-sdk import). They read JSON from stdin, make HTTP calls to the REST API, and exit. There are two patterns depending on whether Claude Code consumes the script's stdout:

- **Context-injecting hooks** (`pre-tool-use`, `pre-compact`, `session-start`) write recalled context to stdout for Claude Code to inject. These MUST use `try/catch` with `await fetch(..., { signal: AbortSignal.timeout(N) })` — the script has to wait for the response before exiting, and the timeout is the only bound on hang time.
- **Telemetry-only hooks** (`notification`, `post-tool-failure`, `post-tool-use`, `prompt-submit`, `stop`, `session-end`, `subagent-start`, `subagent-stop`, `task-completed`) write nothing to stdout. These MUST use fire-and-forget `fetch(..., { signal: AbortSignal.timeout(N) }).catch(() => {})` paired with `setTimeout(() => process.exit(0), 500).unref()`. The unawaited fetch dispatches the request; the unref'd `setTimeout` force-exits the process after the request has been flushed to the local daemon's socket buffer (~500ms is enough for single-request hooks; use 1500ms for multi-request hooks like `stop` and `session-end` so all fetches have time to start, especially when `AGENTMEMORY_URL` points to a remote daemon). Without the `setTimeout` Node keeps the event loop alive waiting for any in-flight fetch to settle, which means the hook still blocks Claude Code's next-prompt boundary for up to the AbortSignal duration — exactly the bug fire-and-forget is meant to fix.

## Coding Standards

Expand Down
37 changes: 18 additions & 19 deletions plugin/scripts/notification.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,24 @@ async function main() {
if (isSdkChildContext(data)) return;
if (data.notification_type !== "permission_prompt") return;
const sessionId = data.session_id || "unknown";
try {
await fetch(`${REST_URL}/agentmemory/observe`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
hookType: "notification",
sessionId,
project: resolveProject(data.cwd),
cwd: data.cwd || process.cwd(),
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
data: {
notification_type: data.notification_type,
title: data.title,
message: data.message
}
}),
signal: AbortSignal.timeout(2e3)
});
} catch {}
fetch(`${REST_URL}/agentmemory/observe`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
hookType: "notification",
sessionId,
project: resolveProject(data.cwd),
cwd: data.cwd || process.cwd(),
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
data: {
notification_type: data.notification_type,
title: data.title,
message: data.message
}
}),
signal: AbortSignal.timeout(2e3)
}).catch(() => {});
setTimeout(() => process.exit(0), 500).unref();
}
main();

Expand Down
37 changes: 18 additions & 19 deletions plugin/scripts/post-tool-failure.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,24 @@ async function main() {
if (isSdkChildContext(data)) return;
if (data.is_interrupt) return;
const sessionId = data.session_id || "unknown";
try {
await fetch(`${REST_URL}/agentmemory/observe`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
hookType: "post_tool_failure",
sessionId,
project: resolveProject(data.cwd),
cwd: data.cwd || process.cwd(),
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
data: {
tool_name: data.tool_name,
tool_input: typeof data.tool_input === "string" ? data.tool_input.slice(0, 4e3) : JSON.stringify(data.tool_input ?? "").slice(0, 4e3),
error: typeof data.error === "string" ? data.error.slice(0, 4e3) : JSON.stringify(data.error ?? "").slice(0, 4e3)
}
}),
signal: AbortSignal.timeout(3e3)
});
} catch {}
fetch(`${REST_URL}/agentmemory/observe`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
hookType: "post_tool_failure",
sessionId,
project: resolveProject(data.cwd),
cwd: data.cwd || process.cwd(),
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
data: {
tool_name: data.tool_name,
tool_input: typeof data.tool_input === "string" ? data.tool_input.slice(0, 4e3) : JSON.stringify(data.tool_input ?? "").slice(0, 4e3),
error: typeof data.error === "string" ? data.error.slice(0, 4e3) : JSON.stringify(data.error ?? "").slice(0, 4e3)
}
}),
signal: AbortSignal.timeout(3e3)
}).catch(() => {});
setTimeout(() => process.exit(0), 500).unref();
}
main();

Expand Down
39 changes: 19 additions & 20 deletions plugin/scripts/post-tool-use.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,25 @@ async function main() {
if (isSdkChildContext(data)) return;
const sessionId = data.session_id || "unknown";
const { imageData, cleanOutput } = extractImageData(data.tool_response ?? data.tool_output);
try {
await fetch(`${REST_URL}/agentmemory/observe`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
hookType: "post_tool_use",
sessionId,
project: resolveProject(data.cwd),
cwd: data.cwd || process.cwd(),
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
data: {
tool_name: data.tool_name,
tool_input: data.tool_input,
tool_output: truncate(cleanOutput, 8e3),
...imageData ? { image_data: imageData } : {}
}
}),
signal: AbortSignal.timeout(3e3)
});
} catch {}
fetch(`${REST_URL}/agentmemory/observe`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
hookType: "post_tool_use",
sessionId,
project: resolveProject(data.cwd),
cwd: data.cwd || process.cwd(),
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
data: {
tool_name: data.tool_name,
tool_input: data.tool_input,
tool_output: truncate(cleanOutput, 8e3),
...imageData ? { image_data: imageData } : {}
}
}),
signal: AbortSignal.timeout(3e3)
}).catch(() => {});
setTimeout(() => process.exit(0), 500).unref();
}
function isBase64Image(val) {
return typeof val === "string" && (val.startsWith("data:image/") || val.startsWith("iVBORw0KGgo") || val.startsWith("/9j/"));
Expand Down
29 changes: 14 additions & 15 deletions plugin/scripts/prompt-submit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,20 @@ async function main() {
}
if (isSdkChildContext(data)) return;
const sessionId = data.session_id || "unknown";
try {
await fetch(`${REST_URL}/agentmemory/observe`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
hookType: "prompt_submit",
sessionId,
project: resolveProject(data.cwd),
cwd: data.cwd || process.cwd(),
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
data: { prompt: data.prompt }
}),
signal: AbortSignal.timeout(3e3)
});
} catch {}
fetch(`${REST_URL}/agentmemory/observe`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
hookType: "prompt_submit",
sessionId,
project: resolveProject(data.cwd),
cwd: data.cwd || process.cwd(),
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
data: { prompt: data.prompt }
}),
signal: AbortSignal.timeout(3e3)
}).catch(() => {});
setTimeout(() => process.exit(0), 500).unref();
}
main();

Expand Down
57 changes: 25 additions & 32 deletions plugin/scripts/session-end.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,35 @@ async function main() {
}
if (isSdkChildContext(data)) return;
const sessionId = data.session_id || "unknown";
try {
await fetch(`${REST_URL}/agentmemory/session/end`, {
fetch(`${REST_URL}/agentmemory/session/end`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({ sessionId }),
signal: AbortSignal.timeout(3e4)
}).catch(() => {});
if (process.env["CONSOLIDATION_ENABLED"] === "true") {
fetch(`${REST_URL}/agentmemory/crystals/auto`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({ sessionId }),
signal: AbortSignal.timeout(3e4)
});
} catch {}
if (process.env["CONSOLIDATION_ENABLED"] === "true") {
try {
await fetch(`${REST_URL}/agentmemory/crystals/auto`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({ olderThanDays: 0 }),
signal: AbortSignal.timeout(6e4)
});
} catch {}
try {
await fetch(`${REST_URL}/agentmemory/consolidate-pipeline`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
tier: "all",
force: true
}),
signal: AbortSignal.timeout(12e4)
});
} catch {}
}
if (process.env["CLAUDE_MEMORY_BRIDGE"] === "true") try {
await fetch(`${REST_URL}/agentmemory/claude-bridge/sync`, {
body: JSON.stringify({ olderThanDays: 0 }),
signal: AbortSignal.timeout(6e4)
}).catch(() => {});
fetch(`${REST_URL}/agentmemory/consolidate-pipeline`, {
method: "POST",
headers: authHeaders(),
signal: AbortSignal.timeout(3e4)
});
} catch {}
body: JSON.stringify({
tier: "all",
force: true
}),
signal: AbortSignal.timeout(12e4)
}).catch(() => {});
}
if (process.env["CLAUDE_MEMORY_BRIDGE"] === "true") fetch(`${REST_URL}/agentmemory/claude-bridge/sync`, {
method: "POST",
headers: authHeaders(),
signal: AbortSignal.timeout(3e4)
}).catch(() => {});
setTimeout(() => process.exit(0), 1500).unref();
}
main();

Expand Down
29 changes: 13 additions & 16 deletions plugin/scripts/stop.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,19 @@ async function main() {
}
if (isSdkChildContext(data)) return;
const sessionId = data.session_id || "unknown";
try {
await fetch(`${REST_URL}/agentmemory/summarize`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({ sessionId }),
signal: AbortSignal.timeout(12e4)
});
} catch {}
try {
await fetch(`${REST_URL}/agentmemory/session/end`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({ sessionId }),
signal: AbortSignal.timeout(5e3)
});
} catch {}
fetch(`${REST_URL}/agentmemory/summarize`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({ sessionId }),
signal: AbortSignal.timeout(12e4)
}).catch(() => {});
fetch(`${REST_URL}/agentmemory/session/end`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({ sessionId }),
signal: AbortSignal.timeout(5e3)
}).catch(() => {});
setTimeout(() => process.exit(0), 1500).unref();
}
main();

Expand Down
1 change: 1 addition & 0 deletions plugin/scripts/subagent-start.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async function main() {
}),
signal: AbortSignal.timeout(TIMEOUT_MS)
}).catch(() => {});
setTimeout(() => process.exit(0), 500).unref();
}
main();

Expand Down
37 changes: 18 additions & 19 deletions plugin/scripts/subagent-stop.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,24 @@ async function main() {
if (isSdkChildContext(data)) return;
const sessionId = data.session_id || "unknown";
const lastMsg = typeof data.last_assistant_message === "string" ? data.last_assistant_message.slice(0, 4e3) : "";
try {
await fetch(`${REST_URL}/agentmemory/observe`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
hookType: "subagent_stop",
sessionId,
project: resolveProject(data.cwd),
cwd: data.cwd || process.cwd(),
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
data: {
agent_id: data.agent_id,
agent_type: data.agent_type,
last_message: lastMsg
}
}),
signal: AbortSignal.timeout(2e3)
});
} catch {}
fetch(`${REST_URL}/agentmemory/observe`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
hookType: "subagent_stop",
sessionId,
project: resolveProject(data.cwd),
cwd: data.cwd || process.cwd(),
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
data: {
agent_id: data.agent_id,
agent_type: data.agent_type,
last_message: lastMsg
}
}),
signal: AbortSignal.timeout(2e3)
}).catch(() => {});
setTimeout(() => process.exit(0), 500).unref();
}
main();

Expand Down
41 changes: 20 additions & 21 deletions plugin/scripts/task-completed.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,26 @@ async function main() {
}
if (isSdkChildContext(data)) return;
const sessionId = data.session_id || "unknown";
try {
await fetch(`${REST_URL}/agentmemory/observe`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
hookType: "task_completed",
sessionId,
project: resolveProject(data.cwd),
cwd: data.cwd || process.cwd(),
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
data: {
task_id: data.task_id,
task_subject: data.task_subject,
task_description: typeof data.task_description === "string" ? data.task_description.slice(0, 2e3) : "",
teammate_name: data.teammate_name,
team_name: data.team_name
}
}),
signal: AbortSignal.timeout(2e3)
});
} catch {}
fetch(`${REST_URL}/agentmemory/observe`, {
method: "POST",
headers: authHeaders(),
body: JSON.stringify({
hookType: "task_completed",
sessionId,
project: resolveProject(data.cwd),
cwd: data.cwd || process.cwd(),
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
data: {
task_id: data.task_id,
task_subject: data.task_subject,
task_description: typeof data.task_description === "string" ? data.task_description.slice(0, 2e3) : "",
teammate_name: data.teammate_name,
team_name: data.team_name
}
}),
signal: AbortSignal.timeout(2e3)
}).catch(() => {});
setTimeout(() => process.exit(0), 500).unref();
}
main();

Expand Down
Loading