Skip to content

#1 Persistent memory for AI coding agents based on real-world benchmarks

fix: cap session-start/subagent-start hook latency (#221) (#271)

Rohit Ghumare1ff5849d9c88
diff --git a/plugin/scripts/session-start.mjs b/plugin/scripts/session-start.mjs
index 23b989d8..9e573e24 100755
--- a/plugin/scripts/session-start.mjs
+++ b/plugin/scripts/session-start.mjs
@@ -8,6 +8,8 @@ function isSdkChildContext(payload) {
const INJECT_CONTEXT = process.env["AGENTMEMORY_INJECT_CONTEXT"] === "true";
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
+const INJECT_TIMEOUT_MS = 1500;
+const REGISTER_TIMEOUT_MS = 800;
function authHeaders() {
const h = { "Content-Type": "application/json" };
if (SECRET) h["Authorization"] = `Bearer ${SECRET}`;
@@ -25,18 +27,29 @@ async function main() {
if (isSdkChildContext(data)) return;
const sessionId = data.session_id || `ses_${Date.now().toString(36)}`;
const project = data.cwd || process.cwd();
+ const url = `${REST_URL}/agentmemory/session/start`;
+ const init = {
+ method: "POST",
+ headers: authHeaders(),
+ body: JSON.stringify({
+ sessionId,
+ project,
+ cwd: project
+ })
+ };
+ if (!INJECT_CONTEXT) {
+ fetch(url, {
+ ...init,
+ signal: AbortSignal.timeout(REGISTER_TIMEOUT_MS)
+ }).catch(() => {});
+ return;
+ }
try {
- const res = await fetch(`${REST_URL}/agentmemory/session/start`, {
- method: "POST",
- headers: authHeaders(),
- body: JSON.stringify({
- sessionId,
- project,
- cwd: project
- }),
- signal: AbortSignal.timeout(5e3)
+ const res = await fetch(url, {
+ ...init,
+ signal: AbortSignal.timeout(INJECT_TIMEOUT_MS)
});
- if (INJECT_CONTEXT && res.ok) {
+ if (res.ok) {
const result = await res.json();
if (result.context) process.stdout.write(result.context);
}
diff --git a/plugin/scripts/subagent-start.mjs b/plugin/scripts/subagent-start.mjs
index 648745de..db143459 100755
--- a/plugin/scripts/subagent-start.mjs
+++ b/plugin/scripts/subagent-start.mjs
@@ -7,6 +7,7 @@ function isSdkChildContext(payload) {
}
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
+const TIMEOUT_MS = 800;
function authHeaders() {
const h = { "Content-Type": "application/json" };
if (SECRET) h["Authorization"] = `Bearer ${SECRET}`;
@@ -23,24 +24,22 @@ 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: "subagent_start",
- sessionId,
- project: data.cwd || process.cwd(),
- cwd: data.cwd || process.cwd(),
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
- data: {
- agent_id: data.agent_id,
- agent_type: data.agent_type
- }
- }),
- signal: AbortSignal.timeout(2e3)
- });
- } catch {}
+ fetch(`${REST_URL}/agentmemory/observe`, {
+ method: "POST",
+ headers: authHeaders(),
+ body: JSON.stringify({
+ hookType: "subagent_start",
+ sessionId,
+ project: data.cwd || process.cwd(),
+ cwd: data.cwd || process.cwd(),
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
+ data: {
+ agent_id: data.agent_id,
+ agent_type: data.agent_type
+ }
+ }),
+ signal: AbortSignal.timeout(TIMEOUT_MS)
+ }).catch(() => {});
}
main();
diff --git a/src/hooks/session-start.ts b/src/hooks/session-start.ts
index 814e01f9..a6cefe41 100644
--- a/src/hooks/session-start.ts
+++ b/src/hooks/session-start.ts
@@ -1,5 +1,7 @@
#!/usr/bin/env node
+// Inlined from ./sdk-guard so each hook bundles to a single self-contained
+// .mjs (matches the pattern used by every other hook entry in tsdown.config).
function isSdkChildContext(payload: unknown): boolean {
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
if (!payload || typeof payload !== "object") return false;
@@ -18,6 +20,13 @@ const INJECT_CONTEXT = process.env["AGENTMEMORY_INJECT_CONTEXT"] === "true";
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
+// When the server is unreachable a 5s timeout multiplies hard under
+// concurrent fan-out (Slack bots, multi-agent harnesses) and becomes a
+// positive feedback loop that OOM-kills iii-engine (#221). Cap tight on
+// both paths and skip the await entirely when the response is unused.
+const INJECT_TIMEOUT_MS = 1500;
+const REGISTER_TIMEOUT_MS = 800;
+
function authHeaders(): Record<string, string> {
const h: Record<string, string> = { "Content-Type": "application/json" };
if (SECRET) h["Authorization"] = `Bearer ${SECRET}`;
@@ -43,18 +52,30 @@ async function main() {
(data.session_id as string) || `ses_${Date.now().toString(36)}`;
const project = (data.cwd as string) || process.cwd();
+ const url = `${REST_URL}/agentmemory/session/start`;
+ const init: RequestInit = {
+ method: "POST",
+ headers: authHeaders(),
+ body: JSON.stringify({ sessionId, project, cwd: project }),
+ };
+
+ if (!INJECT_CONTEXT) {
+ // Pure telemetry path: caller never reads the response, so don't
+ // block on it. AbortSignal.timeout caps the wait the event loop
+ // gives the pending socket before exit.
+ fetch(url, {
+ ...init,
+ signal: AbortSignal.timeout(REGISTER_TIMEOUT_MS),
+ }).catch(() => {});
+ return;
+ }
+
try {
- const res = await fetch(`${REST_URL}/agentmemory/session/start`, {
- method: "POST",
- headers: authHeaders(),
- body: JSON.stringify({ sessionId, project, cwd: project }),
- signal: AbortSignal.timeout(5000),
+ const res = await fetch(url, {
+ ...init,
+ signal: AbortSignal.timeout(INJECT_TIMEOUT_MS),
});
-
- // Only write context to stdout when the user has explicitly opted
- // into injection. Registering the session is cheap and doesn't touch
- // Claude Code's input token window.
- if (INJECT_CONTEXT && res.ok) {
+ if (res.ok) {
const result = (await res.json()) as { context?: string };
if (result.context) {
process.stdout.write(result.context);
diff --git a/src/hooks/subagent-start.ts b/src/hooks/subagent-start.ts
index 24f1c45f..3f730adb 100644
--- a/src/hooks/subagent-start.ts
+++ b/src/hooks/subagent-start.ts
@@ -1,5 +1,7 @@
#!/usr/bin/env node
+// Inlined from ./sdk-guard so each hook bundles to a single self-contained
+// .mjs (matches the pattern used by every other hook entry in tsdown.config).
function isSdkChildContext(payload: unknown): boolean {
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
if (!payload || typeof payload !== "object") return false;
@@ -9,6 +11,12 @@ function isSdkChildContext(payload: unknown): boolean {
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
+// Passive telemetry only — nothing reads the response, so the previous
+// `await` was pure latency. Tightened from 2000ms to a defensive cap so a
+// slow/unreachable server can't stack onto every concurrent subagent
+// startup (#221).
+const TIMEOUT_MS = 800;
+
function authHeaders(): Record<string, string> {
const h: Record<string, string> = { "Content-Type": "application/json" };
if (SECRET) h["Authorization"] = `Bearer ${SECRET}`;
@@ -32,26 +40,22 @@ async function main() {
const sessionId = (data.session_id as string) || "unknown";
- try {
- await fetch(`${REST_URL}/agentmemory/observe`, {
- method: "POST",
- headers: authHeaders(),
- body: JSON.stringify({
- hookType: "subagent_start",
- sessionId,
- project: data.cwd || process.cwd(),
- cwd: data.cwd || process.cwd(),
- timestamp: new Date().toISOString(),
- data: {
- agent_id: data.agent_id,
- agent_type: data.agent_type,
- },
- }),
- signal: AbortSignal.timeout(2000),
- });
- } catch {
- // fire and forget
- }
+ fetch(`${REST_URL}/agentmemory/observe`, {
+ method: "POST",
+ headers: authHeaders(),
+ body: JSON.stringify({
+ hookType: "subagent_start",
+ sessionId,
+ project: data.cwd || process.cwd(),
+ cwd: data.cwd || process.cwd(),
+ timestamp: new Date().toISOString(),
+ data: {
+ agent_id: data.agent_id,
+ agent_type: data.agent_type,
+ },
+ }),
+ signal: AbortSignal.timeout(TIMEOUT_MS),
+ }).catch(() => {});
}
main();