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
106 changes: 106 additions & 0 deletions src/mcp/rest-proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const DEFAULT_URL = "http://localhost:3111";
const HEALTH_PROBE_TIMEOUT_MS = 500;
const CALL_TIMEOUT_MS = 15_000;
const LOCAL_MODE_TTL_MS = 30_000;

export interface ProxyHandle {
mode: "proxy";
baseUrl: string;
call: (path: string, init?: RequestInit) => Promise<unknown>;
}

export interface LocalHandle {
mode: "local";
}

export type Handle = ProxyHandle | LocalHandle;

let cached: Handle | null = null;
let cachedAt = 0;
let probeInFlight: Promise<Handle> | null = null;
Comment on lines +18 to +20

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
fd -t f 'package.json' -d 2 --exec sh -c 'echo "== $0 =="; jq ".engines // {}" "$0"'

Repository: rohitg00/agentmemory

Length of output: 210


🏁 Script executed:

#!/bin/bash
# Find all package.json files
fd -t f 'package.json' -x cat {} \; | head -50

Repository: rohitg00/agentmemory

Length of output: 1547


🏁 Script executed:

#!/bin/bash
# Get the full package.json to see engines field
find . -name "package.json" -type f -exec cat {} \; | jq '.engines // "no engines specified"'

Repository: rohitg00/agentmemory

Length of output: 113


🏁 Script executed:

#!/bin/bash
# Read rest-proxy.ts file
wc -l src/mcp/rest-proxy.ts

Repository: rohitg00/agentmemory

Length of output: 88


🏁 Script executed:

#!/bin/bash
# Read the entire rest-proxy.ts file
cat -n src/mcp/rest-proxy.ts

Repository: rohitg00/agentmemory

Length of output: 2970


🏁 Script executed:

#!/bin/bash
# Search for issue `#159` reference in the codebase
rg "#159|issue.*159|159.*issue" --type ts --type js --type md -i

Repository: rohitg00/agentmemory

Length of output: 173


🏁 Script executed:

#!/bin/bash
# Read the test file to understand issue `#159` context
cat -n test/mcp-standalone-proxy.test.ts | head -100

Repository: rohitg00/agentmemory

Length of output: 4788


🏁 Script executed:

#!/bin/bash
# Continue reading the test file to see more tests
cat -n test/mcp-standalone-proxy.test.ts | sed -n '100,150p'

Repository: rohitg00/agentmemory

Length of output: 915


Resolved handle is cached permanently — no recovery from startup-order or server bouncing.

Once the first probe resolves, the cache is never invalidated. If the server was down at MCP startup but brought up later, MCP stays on local KV forever (writes never reach the server). If the server goes down after being probed as up, every call still hits proxy first, fails, and silently falls back to local; once it's back up, there's no re-sync and any memories written during the outage live only in local KV.

This is the split-data scenario issue #159 addresses. Consider invalidating cached when a proxy call throws (so the next resolveHandle() re-probes), and/or adding a short TTL on the local-mode cache so a later-started server is eventually discovered.

♻️ Sketch: invalidate on proxy failure + short local-mode TTL
-let cached: Handle | null = null;
+let cached: Handle | null = null;
+let cachedAt = 0;
+const LOCAL_MODE_TTL_MS = 30_000;
+
+export function invalidateHandle(): void {
+  cached = null;
+}
 ...
 export async function resolveHandle(): Promise<Handle> {
-  if (cached) return cached;
+  const now = new Date().toISOString();
+  if (cached) {
+    if (cached.mode === "local" && /* elapsed check */ ) {
+      cached = null;
+    } else {
+      return cached;
+    }
+  }
   if (probeInFlight) return probeInFlight;
 ...
-      cached = handle;
+      cached = handle;
+      cachedAt = now;
       return handle;
     }
     const local: LocalHandle = { mode: "local" };
-    cached = local;
+    cached = local;
+    cachedAt = now;
     return local;

Then call invalidateHandle() from handleToolCall's proxy-failure catch in standalone.ts.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/mcp/rest-proxy.ts` around lines 17 - 18, The cached Handle (cached) is
never invalidated causing permanent local-mode after an initial probe; update
resolveHandle()/probe logic to allow re-probing by adding an invalidateHandle()
that clears cached and probeInFlight (or set a short TTL on cached to
auto-expire local-mode), and call invalidateHandle() from the proxy-failure
catch in handleToolCall (in standalone.ts) so any proxy error clears the cache
and triggers a fresh probe on the next resolveHandle() attempt; ensure
probeInFlight handling prevents duplicate probes while allowing recovery after
failures.


function baseUrl(): string {
return (process.env["AGENTMEMORY_URL"] || DEFAULT_URL).replace(/\/+$/, "");
}

function authHeader(): Record<string, string> {
const secret = process.env["AGENTMEMORY_SECRET"];
return secret ? { authorization: `Bearer ${secret}` } : {};
}

async function probe(url: string): Promise<boolean> {
try {
const res = await fetch(`${url}/agentmemory/livez`, {
method: "GET",
headers: authHeader(),
signal: AbortSignal.timeout(HEALTH_PROBE_TIMEOUT_MS),
});
return res.ok;
} catch {
return false;
}
}

export function invalidateHandle(): void {
cached = null;
cachedAt = 0;
}

export async function resolveHandle(): Promise<Handle> {
const now = Date.now();
if (cached) {
if (cached.mode === "local" && now - cachedAt >= LOCAL_MODE_TTL_MS) {
cached = null;
cachedAt = 0;
} else {
return cached;
}
}
if (probeInFlight) return probeInFlight;
const url = baseUrl();
probeInFlight = (async () => {
const up = await probe(url);
if (up) {
const handle: ProxyHandle = {
mode: "proxy",
baseUrl: url,
call: async (path, init) => {
const res = await fetch(`${url}${path}`, {
...init,
headers: {
"content-type": "application/json",
...authHeader(),
...(init?.headers as Record<string, string> | undefined),
},
signal: AbortSignal.timeout(CALL_TIMEOUT_MS),
});
if (!res.ok) {
throw new Error(
`${init?.method || "GET"} ${path} -> ${res.status} ${res.statusText}`,
);
}
const text = await res.text();
return text ? JSON.parse(text) : null;
},
};
cached = handle;
cachedAt = Date.now();
return handle;
}
const local: LocalHandle = { mode: "local" };
cached = local;
cachedAt = Date.now();
return local;
})();
try {
return await probeInFlight;
} finally {
probeInFlight = null;
}
}

export function resetHandleForTests(): void {
cached = null;
cachedAt = 0;
probeInFlight = null;
}
Loading
Loading