Files
rohitg00--agentmemory/test/stop-hook-recursion-guard.test.ts
Rohit Ghumare 5e63846b29 fix(hooks): break Stop-hook infinite recursion via agent-sdk fallback
Reported: a user with no provider API key and AGENTMEMORY_AUTO_COMPRESS=false
(which they believed protected them) hit unbounded recursion — Stop hook
POSTs /agentmemory/summarize, handler calls provider.summarize(), agent-sdk
provider spawns @anthropic-ai/claude-agent-sdk query(), which creates a full
CC-style child session that reads ~/.claude/settings.json, registers the
same plugin hooks, and fires its own Stop -> another child -> loop. ~579
ghost 'entrypoint: sdk-ts' sessions accumulated in a few minutes, draining
Claude Pro tokens.

#149 only added a stderr warning. AGENTMEMORY_AUTO_COMPRESS gated /compress
but never /summarize, so users who followed the warning's implied guidance
still got hit. Fix the loop at every layer:

1. config.ts detectProvider
   - Treat empty-string provider keys (ANTHROPIC_API_KEY=) as unset; they
     previously passed the truthiness check identically to a real key.
   - Stop defaulting to agent-sdk. When no key is set, return a 'noop'
     provider config and warn. Agent-sdk fallback now requires an explicit
     AGENTMEMORY_ALLOW_AGENT_SDK=true opt-in with a loud second warning.

2. providers/noop.ts (new) + providers/index.ts
   - NoopProvider implements MemoryProvider and returns empty strings for
     compress and summarize so callers can detect .name === 'noop' and
     short-circuit without spawning anything.
   - Add ProviderType 'noop' and wire it through createBaseProvider.

3. providers/agent-sdk.ts
   - Before spawning query(), check process.env.AGENTMEMORY_SDK_CHILD === '1'
     and return '' instead of recursing. Set the env var to '1' before the
     spawn so any child process (including the Agent SDK session's hooks)
     inherits it.

4. hooks/sdk-guard.ts (new) + all 12 hook scripts
   - Shared isSdkChildContext(payload) checks both AGENTMEMORY_SDK_CHILD=1
     and payload.entrypoint === 'sdk-ts' (CC writes this into the stdin
     jsonl for SDK-spawned sessions). Every hook script now bails early
     when that returns true, so even if one guard layer fails the others
     break the loop.

5. functions/summarize.ts
   - Short-circuit with {success:false, error:'no_provider'} when
     provider.name === 'noop' — never reach .summarize().
   - Treat an empty provider response as empty_provider_response instead
     of trying to parse it.

Tests: 74 files / 819 tests pass (+7 new in stop-hook-recursion-guard.test.ts).
Defense in depth means any ONE of the five layers breaks the loop.
2026-04-22 10:57:57 +01:00

58 lines
1.9 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { isSdkChildContext } from "../src/hooks/sdk-guard.js";
import { NoopProvider } from "../src/providers/noop.js";
describe("isSdkChildContext — Stop hook recursion guard", () => {
const originalEnv = process.env.AGENTMEMORY_SDK_CHILD;
beforeEach(() => {
delete process.env.AGENTMEMORY_SDK_CHILD;
});
afterEach(() => {
if (originalEnv === undefined) {
delete process.env.AGENTMEMORY_SDK_CHILD;
} else {
process.env.AGENTMEMORY_SDK_CHILD = originalEnv;
}
});
it("returns true when AGENTMEMORY_SDK_CHILD=1 is in env", () => {
process.env.AGENTMEMORY_SDK_CHILD = "1";
expect(isSdkChildContext({})).toBe(true);
});
it("returns true when payload.entrypoint === 'sdk-ts'", () => {
expect(isSdkChildContext({ entrypoint: "sdk-ts" })).toBe(true);
});
it("returns false for a normal CC payload", () => {
expect(isSdkChildContext({ entrypoint: "cli", session_id: "s1" })).toBe(false);
});
it("returns false when payload is null / undefined / non-object", () => {
expect(isSdkChildContext(null)).toBe(false);
expect(isSdkChildContext(undefined)).toBe(false);
expect(isSdkChildContext("not-an-object")).toBe(false);
expect(isSdkChildContext(42)).toBe(false);
});
it("env marker wins over payload shape", () => {
process.env.AGENTMEMORY_SDK_CHILD = "1";
expect(isSdkChildContext({ entrypoint: "cli" })).toBe(true);
});
});
describe("NoopProvider — no-op fallback when no LLM key present", () => {
it("reports name 'noop' so callers can detect it and short-circuit", () => {
const p = new NoopProvider();
expect(p.name).toBe("noop");
});
it("returns empty string for compress and summarize", async () => {
const p = new NoopProvider();
await expect(p.compress()).resolves.toBe("");
await expect(p.summarize()).resolves.toBe("");
});
});