Skip to content

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

fix: address CodeRabbit review on session replay

Rohit Ghumare50817adb8a99
diff --git a/README.md b/README.md
index 820d20f8..aa3e22fd 100644
--- a/README.md
+++ b/README.md
@@ -800,7 +800,7 @@ Create `~/.agentmemory/.env`:
<h2 id="api"><picture><source media="(prefers-color-scheme: dark)" srcset="assets/tags/light/section-api.svg"><img src="assets/tags/section-api.svg" alt="API" height="32" /></picture></h2>
-104 endpoints on port `3111`. The REST API binds to `127.0.0.1` by default. Protected endpoints require `Authorization: Bearer <secret>` when `AGENTMEMORY_SECRET` is set, and mesh sync endpoints require `AGENTMEMORY_SECRET` on both peers.
+107 endpoints on port `3111`. The REST API binds to `127.0.0.1` by default. Protected endpoints require `Authorization: Bearer <secret>` when `AGENTMEMORY_SECRET` is set, and mesh sync endpoints require `AGENTMEMORY_SECRET` on both peers.
<details>
<summary>Key endpoints</summary>
diff --git a/src/cli.ts b/src/cli.ts
index 9bd31593..598810d1 100644
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -830,6 +830,7 @@ async function runImportJsonl(): Promise<void> {
method: "POST",
headers,
body: JSON.stringify(body),
+ signal: AbortSignal.timeout(120_000),
});
const json = (await res.json()) as {
success?: boolean;
@@ -851,7 +852,11 @@ async function runImportJsonl(): Promise<void> {
}
} catch (err) {
spinner.stop("failed");
- p.log.error(err instanceof Error ? err.message : String(err));
+ if (err instanceof Error && err.name === "TimeoutError") {
+ p.log.error("import timed out after 2 minutes");
+ } else {
+ p.log.error(err instanceof Error ? err.message : String(err));
+ }
process.exit(1);
}
}
diff --git a/src/functions/replay.ts b/src/functions/replay.ts
index b575b4ad..8010a4f7 100644
--- a/src/functions/replay.ts
+++ b/src/functions/replay.ts
@@ -1,6 +1,6 @@
import { homedir } from "node:os";
import { lstat, readFile, readdir } from "node:fs/promises";
-import { resolve, join, isAbsolute } from "node:path";
+import { resolve, join } from "node:path";
import type { ISdk } from "iii-sdk";
import type {
CompressedObservation,
@@ -14,18 +14,20 @@ import { projectTimeline, type Timeline } from "../replay/timeline.js";
import { safeAudit } from "./audit.js";
import { logger } from "../logger.js";
-const SENSITIVE_PATH_TERMS = [
- "secret",
- "credential",
- "private_key",
- ".env",
- "id_rsa",
- "token",
+const SENSITIVE_PATH_PATTERNS: RegExp[] = [
+ /(^|[\\/_.-])secret([\\/_.-]|s?$)/i,
+ /(^|[\\/_.-])credentials?([\\/_.-]|$)/i,
+ /(^|[\\/_.-])private[_-]?key([\\/_.-]|$)/i,
+ /(^|[\\/])\.env(\.[\w-]+)?$/i,
+ /(^|[\\/_.-])id_rsa([\\/_.-]|$)/i,
+ /(^|[\\/])auth[_-]?token([\\/_.-]|$)/i,
+ /(^|[\\/])bearer[_-]?token([\\/_.-]|$)/i,
+ /(^|[\\/])access[_-]?token([\\/_.-]|$)/i,
+ /(^|[\\/])api[_-]?token([\\/_.-]|$)/i,
];
-function isSensitive(path: string): boolean {
- const lower = path.toLowerCase();
- return SENSITIVE_PATH_TERMS.some((t) => lower.includes(t));
+export function isSensitive(path: string): boolean {
+ return SENSITIVE_PATH_PATTERNS.some((re) => re.test(path));
}
async function isSymlink(path: string): Promise<boolean> {
@@ -143,7 +145,10 @@ export function registerReplayFunctions(sdk: ISdk, kv: StateKV): void {
if (typeof rawPath !== "string" || rawPath.length === 0) {
return { success: false, error: "path must be a non-empty string" };
}
- const abs = isAbsolute(rawPath) ? resolve(rawPath) : resolve(rawPath);
+ const expanded = rawPath.startsWith("~")
+ ? join(homedir(), rawPath.slice(1))
+ : rawPath;
+ const abs = resolve(expanded);
if (isSensitive(abs)) {
return { success: false, error: "refusing to process sensitive-looking path" };
}
@@ -175,6 +180,7 @@ export function registerReplayFunctions(sdk: ISdk, kv: StateKV): void {
let observationCount = 0;
for (const file of files) {
+ if (isSensitive(file)) continue;
if (await isSymlink(file)) continue;
let text: string;
try {
@@ -191,7 +197,19 @@ export function registerReplayFunctions(sdk: ISdk, kv: StateKV): void {
if (parsed.observations.length === 0) continue;
const existing = await kv.get<Session>(KV.sessions, parsed.sessionId);
- if (!existing) {
+ if (existing) {
+ existing.observationCount =
+ (existing.observationCount || 0) + parsed.observations.length;
+ if (parsed.endedAt > (existing.endedAt || "")) {
+ existing.endedAt = parsed.endedAt;
+ }
+ if (existing.status === "active") existing.status = "completed";
+ const existingTags = existing.tags || [];
+ if (!existingTags.includes("jsonl-import")) {
+ existing.tags = [...existingTags, "jsonl-import"];
+ }
+ await kv.set(KV.sessions, existing.id, existing);
+ } else {
const session: Session = {
id: parsed.sessionId,
project: parsed.project,
@@ -205,10 +223,12 @@ export function registerReplayFunctions(sdk: ISdk, kv: StateKV): void {
await kv.set(KV.sessions, session.id, session);
}
- for (const obs of parsed.observations) {
- await kv.set(KV.observations(parsed.sessionId), obs.id, obs);
- observationCount++;
- }
+ await Promise.all(
+ parsed.observations.map((obs) =>
+ kv.set(KV.observations(parsed.sessionId), obs.id, obs),
+ ),
+ );
+ observationCount += parsed.observations.length;
sessionIds.push(parsed.sessionId);
}
diff --git a/src/index.ts b/src/index.ts
index 8965ea0a..a5bd630e 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -318,7 +318,7 @@ async function main() {
`[agentmemory] Ready. ${embeddingProvider ? "Triple-stream (BM25+Vector+Graph)" : "BM25+Graph"} search active.`,
);
console.log(
- `[agentmemory] Endpoints: 104 REST + 44 MCP tools + 6 MCP resources + 3 MCP prompts`,
+ `[agentmemory] Endpoints: 107 REST + 44 MCP tools + 6 MCP resources + 3 MCP prompts`,
);
const viewerPort = config.restPort + 2;
diff --git a/src/replay/jsonl-parser.ts b/src/replay/jsonl-parser.ts
index 3e8487a1..390ecc48 100644
--- a/src/replay/jsonl-parser.ts
+++ b/src/replay/jsonl-parser.ts
@@ -169,12 +169,13 @@ export function parseJsonlText(text: string, fallbackSessionId?: string): Parsed
if (obs.sessionId === "imported") obs.sessionId = effectiveSessionId;
}
+ const nowIso = new Date().toISOString();
return {
sessionId: effectiveSessionId,
project: deriveProject(cwd),
cwd: cwd || process.cwd(),
- startedAt: firstTs || new Date().toISOString(),
- endedAt: lastTs || new Date().toISOString(),
+ startedAt: firstTs || nowIso,
+ endedAt: lastTs || nowIso,
observations,
};
}
diff --git a/src/triggers/api.ts b/src/triggers/api.ts
index a5da2b72..5dc4b897 100644
--- a/src/triggers/api.ts
+++ b/src/triggers/api.ts
@@ -369,7 +369,7 @@ export function registerApiTriggers(
body: { error: "path must be a non-empty string" },
};
}
- payload.path = body.path;
+ payload.path = body.path.trim();
}
if (body.maxFiles !== undefined) {
if (!Number.isInteger(body.maxFiles) || (body.maxFiles as number) < 1) {
diff --git a/src/version.ts b/src/version.ts
index bf1e842e..0e6b4f0b 100644
--- a/src/version.ts
+++ b/src/version.ts
@@ -1 +1 @@
-export const VERSION: "0.3.0" | "0.4.0" | "0.5.0" | "0.6.0" | "0.6.1" | "0.7.0" | "0.7.2" | "0.7.3" | "0.7.4" | "0.7.5" | "0.7.6" | "0.8.1" | "0.8.2" | "0.8.3" | "0.8.4" | "0.8.5" | "0.8.6" | "0.8.7" | "0.8.8" | "0.8.9" | "0.8.10" | "0.8.11" | "0.8.12" | "0.8.13" = "0.8.13";
+export const VERSION: "0.3.0" | "0.4.0" | "0.5.0" | "0.6.0" | "0.6.1" | "0.7.0" | "0.7.2" | "0.7.3" | "0.7.4" | "0.7.5" | "0.7.6" | "0.7.9" | "0.8.0" | "0.8.1" | "0.8.2" | "0.8.3" | "0.8.4" | "0.8.5" | "0.8.6" | "0.8.7" | "0.8.8" | "0.8.9" | "0.8.10" | "0.8.11" | "0.8.12" | "0.8.13" = "0.8.13";
diff --git a/src/viewer/index.html b/src/viewer/index.html
index f1769b82..f38fd0e2 100644
--- a/src/viewer/index.html
+++ b/src/viewer/index.html
@@ -578,6 +578,7 @@
.empty-state p { font-size: 14px; font-family: var(--font-body); font-style: italic; }
.loading { color: var(--ink-faint); padding: 20px; text-align: center; font-style: italic; font-family: var(--font-body); }
+ .empty { color: var(--ink-muted); padding: 24px; text-align: center; font-family: var(--font-body); font-style: italic; border: 1px dashed var(--border); }
.replay-controls { display: flex; align-items: center; gap: 6px; padding: 10px 0; flex-wrap: wrap; font-family: var(--font-ui); font-size: 12px; }
.replay-controls button { padding: 4px 10px; border: 1px solid var(--border); background: var(--bg); color: var(--ink); cursor: pointer; font-family: var(--font-ui); font-size: 12px; }
@@ -965,6 +966,9 @@
}
function switchTab(tab) {
+ if (state.activeTab === 'replay' && tab !== 'replay' && typeof stopReplayTimer === 'function') {
+ stopReplayTimer();
+ }
state.activeTab = tab;
document.querySelectorAll('.tab-bar button').forEach(function(b) {
b.classList.toggle('active', b.dataset.tab === tab);
diff --git a/test/replay-sensitive.test.ts b/test/replay-sensitive.test.ts
new file mode 100644
index 00000000..6550dc7b
--- /dev/null
+++ b/test/replay-sensitive.test.ts
@@ -0,0 +1,21 @@
+import { describe, expect, it } from "vitest";
+import { isSensitive } from "../src/functions/replay.js";
+
+describe("isSensitive path guard", () => {
+ it("blocks .env and common secret filenames", () => {
+ expect(isSensitive("/Users/x/project/.env")).toBe(true);
+ expect(isSensitive("/Users/x/project/.env.local")).toBe(true);
+ expect(isSensitive("/tmp/credentials.json")).toBe(true);
+ expect(isSensitive("/home/alice/.ssh/id_rsa")).toBe(true);
+ expect(isSensitive("/srv/app/secret.key")).toBe(true);
+ expect(isSensitive("/srv/app/access_token.txt")).toBe(true);
+ expect(isSensitive("/srv/app/private_key.pem")).toBe(true);
+ });
+
+ it("does not false-positive on project names containing substrings", () => {
+ expect(isSensitive("/Users/dev/jsonwebtoken-demo/transcript.jsonl")).toBe(false);
+ expect(isSensitive("/repos/secrethandshake-lib/a.jsonl")).toBe(false);
+ expect(isSensitive("/opt/tokeniser/out.jsonl")).toBe(false);
+ expect(isSensitive("/Users/alice/.claude/projects/myapp/abc.jsonl")).toBe(false);
+ });
+});
diff --git a/test/replay.test.ts b/test/replay.test.ts
index af8fd35e..570ffd84 100644
--- a/test/replay.test.ts
+++ b/test/replay.test.ts
@@ -93,4 +93,14 @@ describe("projectTimeline", () => {
const tl = projectTimeline(parsed.observations);
expect(tl.events.some((e) => e.kind === "tool_error")).toBe(true);
});
+
+ it("uses one shared fallback timestamp when metadata missing", () => {
+ const text = JSON.stringify({
+ type: "user",
+ message: { role: "user", content: [{ type: "text", text: "hi" }] },
+ });
+ const out = parseJsonlText(text);
+ expect(out.startedAt).toBe(out.endedAt);
+ });
});
+