Skip to content

Commit 769d7c4

Browse files
authored
Merge pull request #187 from rohitg00/fix/stop-hook-recursion-loop
fix(hooks): break Stop-hook infinite recursion via agent-sdk fallback
2 parents a654dd7 + 766aaed commit 769d7c4

32 files changed

Lines changed: 400 additions & 29 deletions

plugin/scripts/notification.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env node
22
//#region src/hooks/notification.ts
3+
function isSdkChildContext(payload) {
4+
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
5+
if (!payload || typeof payload !== "object") return false;
6+
return payload.entrypoint === "sdk-ts";
7+
}
38
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
49
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
510
function authHeaders() {
@@ -16,6 +21,7 @@ async function main() {
1621
} catch {
1722
return;
1823
}
24+
if (isSdkChildContext(data)) return;
1925
if (data.notification_type !== "permission_prompt") return;
2026
const sessionId = data.session_id || "unknown";
2127
try {

plugin/scripts/post-tool-failure.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env node
22
//#region src/hooks/post-tool-failure.ts
3+
function isSdkChildContext(payload) {
4+
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
5+
if (!payload || typeof payload !== "object") return false;
6+
return payload.entrypoint === "sdk-ts";
7+
}
38
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
49
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
510
function authHeaders() {
@@ -16,6 +21,7 @@ async function main() {
1621
} catch {
1722
return;
1823
}
24+
if (isSdkChildContext(data)) return;
1925
if (data.is_interrupt) return;
2026
const sessionId = data.session_id || "unknown";
2127
try {

plugin/scripts/post-tool-use.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env node
22
//#region src/hooks/post-tool-use.ts
3+
function isSdkChildContext(payload) {
4+
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
5+
if (!payload || typeof payload !== "object") return false;
6+
return payload.entrypoint === "sdk-ts";
7+
}
38
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
49
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
510
function authHeaders() {
@@ -16,6 +21,7 @@ async function main() {
1621
} catch {
1722
return;
1823
}
24+
if (isSdkChildContext(data)) return;
1925
const sessionId = data.session_id || "unknown";
2026
const { imageData, cleanOutput } = extractImageData(data.tool_output);
2127
try {

plugin/scripts/pre-compact.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env node
22
//#region src/hooks/pre-compact.ts
3+
function isSdkChildContext(payload) {
4+
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
5+
if (!payload || typeof payload !== "object") return false;
6+
return payload.entrypoint === "sdk-ts";
7+
}
38
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
49
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
510
function authHeaders() {
@@ -16,6 +21,7 @@ async function main() {
1621
} catch {
1722
return;
1823
}
24+
if (isSdkChildContext(data)) return;
1925
const sessionId = data.session_id || "unknown";
2026
const project = data.cwd || process.cwd();
2127
if (process.env["CLAUDE_MEMORY_BRIDGE"] === "true") try {

plugin/scripts/pre-tool-use.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env node
22
//#region src/hooks/pre-tool-use.ts
3+
function isSdkChildContext(payload) {
4+
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
5+
if (!payload || typeof payload !== "object") return false;
6+
return payload.entrypoint === "sdk-ts";
7+
}
38
const INJECT_CONTEXT = process.env["AGENTMEMORY_INJECT_CONTEXT"] === "true";
49
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
510
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
@@ -18,6 +23,7 @@ async function main() {
1823
} catch {
1924
return;
2025
}
26+
if (isSdkChildContext(data)) return;
2127
const toolName = data.tool_name;
2228
if (!toolName) return;
2329
if (![

plugin/scripts/prompt-submit.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env node
22
//#region src/hooks/prompt-submit.ts
3+
function isSdkChildContext(payload) {
4+
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
5+
if (!payload || typeof payload !== "object") return false;
6+
return payload.entrypoint === "sdk-ts";
7+
}
38
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
49
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
510
function authHeaders() {
@@ -16,6 +21,7 @@ async function main() {
1621
} catch {
1722
return;
1823
}
24+
if (isSdkChildContext(data)) return;
1925
const sessionId = data.session_id || "unknown";
2026
try {
2127
await fetch(`${REST_URL}/agentmemory/observe`, {

plugin/scripts/session-end.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env node
22
//#region src/hooks/session-end.ts
3+
function isSdkChildContext(payload) {
4+
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
5+
if (!payload || typeof payload !== "object") return false;
6+
return payload.entrypoint === "sdk-ts";
7+
}
38
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
49
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
510
function authHeaders() {
@@ -16,6 +21,7 @@ async function main() {
1621
} catch {
1722
return;
1823
}
24+
if (isSdkChildContext(data)) return;
1925
const sessionId = data.session_id || "unknown";
2026
try {
2127
await fetch(`${REST_URL}/agentmemory/session/end`, {

plugin/scripts/session-start.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env node
22
//#region src/hooks/session-start.ts
3+
function isSdkChildContext(payload) {
4+
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
5+
if (!payload || typeof payload !== "object") return false;
6+
return payload.entrypoint === "sdk-ts";
7+
}
38
const INJECT_CONTEXT = process.env["AGENTMEMORY_INJECT_CONTEXT"] === "true";
49
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
510
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
@@ -17,6 +22,7 @@ async function main() {
1722
} catch {
1823
return;
1924
}
25+
if (isSdkChildContext(data)) return;
2026
const sessionId = data.session_id || `ses_${Date.now().toString(36)}`;
2127
const project = data.cwd || process.cwd();
2228
try {

plugin/scripts/stop.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env node
22
//#region src/hooks/stop.ts
3+
function isSdkChildContext(payload) {
4+
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
5+
if (!payload || typeof payload !== "object") return false;
6+
return payload.entrypoint === "sdk-ts";
7+
}
38
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
49
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
510
function authHeaders() {
@@ -16,6 +21,7 @@ async function main() {
1621
} catch {
1722
return;
1823
}
24+
if (isSdkChildContext(data)) return;
1925
const sessionId = data.session_id || "unknown";
2026
try {
2127
await fetch(`${REST_URL}/agentmemory/summarize`, {

plugin/scripts/subagent-start.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env node
22
//#region src/hooks/subagent-start.ts
3+
function isSdkChildContext(payload) {
4+
if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true;
5+
if (!payload || typeof payload !== "object") return false;
6+
return payload.entrypoint === "sdk-ts";
7+
}
38
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
49
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
510
function authHeaders() {
@@ -16,6 +21,7 @@ async function main() {
1621
} catch {
1722
return;
1823
}
24+
if (isSdkChildContext(data)) return;
1925
const sessionId = data.session_id || "unknown";
2026
try {
2127
await fetch(`${REST_URL}/agentmemory/observe`, {

0 commit comments

Comments
 (0)