Skip to content

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

fix(hooks): back-fill prompts from Claude Code transcripts too (#1366)

Dmitrii Zhukov0b3515d3000a
diff --git a/plugin/scripts/session-end.mjs b/plugin/scripts/session-end.mjs
index 0f860c0d..8b57637f 100755
--- a/plugin/scripts/session-end.mjs
+++ b/plugin/scripts/session-end.mjs
@@ -39,6 +39,23 @@ function isSdkChildContext(payload) {
if (!payload || typeof payload !== "object") return false;
return payload.entrypoint === "sdk-ts";
}
+const HARNESS_TEXT = /^(?:<(?:command-name|command-message|command-args|local-command-stdout|local-command-stderr|local-command-caveat|bash-input|bash-stdout|bash-stderr|task-notification|system-reminder|ci-monitor-event|cross-session-message|scheduled-task)(?=[\s>])|\[Request interrupted)/;
+function isUserTurn(msg) {
+ if (msg.isSidechain || msg.isMeta || msg.isCompactSummary) return false;
+ return msg.role === "user" || msg.type === "user" || msg.message?.role === "user";
+}
+function promptText(raw) {
+ const m = raw.match(/<user_query>\n?([\s\S]*?)\n?<\/user_query>/);
+ const text = (m ? m[1] : raw).trim();
+ return HARNESS_TEXT.test(text) ? "" : text;
+}
+function turnTexts(content) {
+ if (typeof content === "string") return [content];
+ if (!Array.isArray(content)) return [];
+ const texts = [];
+ for (const block of content) if (block?.type === "text" && typeof block.text === "string") texts.push(block.text);
+ return texts;
+}
function extractTranscriptPrompts(data) {
const path = data.transcript_path;
if (typeof path !== "string" || !path.endsWith(".jsonl")) return [];
@@ -61,16 +78,20 @@ function extractTranscriptPrompts(data) {
if (prompts.length >= 50) return prompts;
const match = msg.content.match(/<USER_REQUEST>\n?([\s\S]*?)\n?<\/USER_REQUEST>/);
const text = (match ? match[1] : msg.content).trim();
- if (text) prompts.push(text.slice(0, 8e3));
+ if (text) prompts.push({ prompt: text.slice(0, 8e3) });
continue;
}
- if (msg.role !== "user") continue;
- for (const block of msg.message?.content ?? []) {
+ if (!isUserTurn(msg)) continue;
+ const texts = turnTexts(msg.message?.content).map(promptText).filter(Boolean);
+ const promptId = typeof msg.promptId === "string" ? msg.promptId : void 0;
+ const timestamp = typeof msg.timestamp === "string" ? msg.timestamp : void 0;
+ for (const text of promptId ? [texts.join("\n\n")].filter(Boolean) : texts) {
if (prompts.length >= 50) return prompts;
- if (block.type !== "text" || typeof block.text !== "string") continue;
- const m = block.text.match(/<user_query>\n?([\s\S]*?)\n?<\/user_query>/);
- const text = (m ? m[1] : block.text).trim();
- if (text) prompts.push(text.slice(0, 8e3));
+ prompts.push({
+ prompt: text.slice(0, 8e3),
+ promptId,
+ timestamp
+ });
}
}
return prompts;
@@ -93,17 +114,17 @@ async function main() {
const cwd = hookCwd(data) || process.cwd();
const project = resolveProject(cwd);
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
- await Promise.allSettled(transcriptPrompts.map((prompt, index) => captureObservation(withEventId({
+ await Promise.allSettled(transcriptPrompts.map(({ prompt, promptId, timestamp: at }, index) => captureObservation(withEventId({
hookType: "prompt_submit",
sessionId,
project,
cwd,
- timestamp,
+ timestamp: at ?? timestamp,
data: {
prompt,
backfill: true
}
- }, {}, {
+ }, promptId ? { prompt_id: promptId } : {}, {
source: "transcript",
transcript: data.transcript_path,
index,
diff --git a/src/hooks/session-end.ts b/src/hooks/session-end.ts
index ecd8043b..f39a9b6a 100644
--- a/src/hooks/session-end.ts
+++ b/src/hooks/session-end.ts
@@ -9,7 +9,51 @@ function isSdkChildContext(payload: unknown): boolean {
return (payload as { entrypoint?: unknown }).entrypoint === "sdk-ts";
}
-function extractTranscriptPrompts(data: Record<string, unknown>): string[] {
+type TranscriptBlock = { type?: string; text?: string };
+type TranscriptLine = {
+ role?: string;
+ type?: string;
+ source?: string;
+ content?: unknown;
+ isSidechain?: boolean;
+ isMeta?: boolean;
+ isCompactSummary?: boolean;
+ promptId?: string;
+ timestamp?: string;
+ message?: { role?: string; content?: string | TranscriptBlock[] };
+};
+
+type TranscriptPrompt = { prompt: string; promptId?: string; timestamp?: string };
+
+const HARNESS_TEXT =
+ /^(?:<(?:command-name|command-message|command-args|local-command-stdout|local-command-stderr|local-command-caveat|bash-input|bash-stdout|bash-stderr|task-notification|system-reminder|ci-monitor-event|cross-session-message|scheduled-task)(?=[\s>])|\[Request interrupted)/;
+
+function isUserTurn(msg: TranscriptLine): boolean {
+ if (msg.isSidechain || msg.isMeta || msg.isCompactSummary) return false;
+ return (
+ msg.role === "user" || msg.type === "user" || msg.message?.role === "user"
+ );
+}
+
+function promptText(raw: string): string {
+ const m = raw.match(/<user_query>\n?([\s\S]*?)\n?<\/user_query>/);
+ const text = (m ? m[1] : raw).trim();
+ return HARNESS_TEXT.test(text) ? "" : text;
+}
+
+function turnTexts(content: string | TranscriptBlock[] | undefined): string[] {
+ if (typeof content === "string") return [content];
+ if (!Array.isArray(content)) return [];
+ const texts: string[] = [];
+ for (const block of content) {
+ if (block?.type === "text" && typeof block.text === "string") {
+ texts.push(block.text);
+ }
+ }
+ return texts;
+}
+
+function extractTranscriptPrompts(data: Record<string, unknown>): TranscriptPrompt[] {
const path = data.transcript_path;
if (typeof path !== "string" || !path.endsWith(".jsonl")) return [];
let raw: string;
@@ -18,16 +62,10 @@ function extractTranscriptPrompts(data: Record<string, unknown>): string[] {
} catch {
return [];
}
- const prompts: string[] = [];
+ const prompts: TranscriptPrompt[] = [];
for (const line of raw.split("\n")) {
if (!line.trim()) continue;
- let msg: {
- role?: string;
- type?: string;
- source?: string;
- content?: unknown;
- message?: { content?: Array<{ type?: string; text?: string }> };
- };
+ let msg: TranscriptLine;
try {
msg = JSON.parse(line);
} catch {
@@ -37,16 +75,16 @@ function extractTranscriptPrompts(data: Record<string, unknown>): string[] {
if (prompts.length >= 50) return prompts;
const match = msg.content.match(/<USER_REQUEST>\n?([\s\S]*?)\n?<\/USER_REQUEST>/);
const text = (match ? match[1] : msg.content).trim();
- if (text) prompts.push(text.slice(0, 8000));
+ if (text) prompts.push({ prompt: text.slice(0, 8000) });
continue;
}
- if (msg.role !== "user") continue;
- for (const block of msg.message?.content ?? []) {
+ if (!isUserTurn(msg)) continue;
+ const texts = turnTexts(msg.message?.content).map(promptText).filter(Boolean);
+ const promptId = typeof msg.promptId === "string" ? msg.promptId : undefined;
+ const timestamp = typeof msg.timestamp === "string" ? msg.timestamp : undefined;
+ for (const text of promptId ? [texts.join("\n\n")].filter(Boolean) : texts) {
if (prompts.length >= 50) return prompts;
- if (block.type !== "text" || typeof block.text !== "string") continue;
- const m = block.text.match(/<user_query>\n?([\s\S]*?)\n?<\/user_query>/);
- const text = (m ? m[1] : block.text).trim();
- if (text) prompts.push(text.slice(0, 8000));
+ prompts.push({ prompt: text.slice(0, 8000), promptId, timestamp });
}
}
return prompts;
@@ -77,11 +115,11 @@ async function main() {
const project = resolveProject(cwd);
const timestamp = new Date().toISOString();
await Promise.allSettled(
- transcriptPrompts.map((prompt, index) =>
+ transcriptPrompts.map(({ prompt, promptId, timestamp: at }, index) =>
captureObservation(
withEventId(
- { hookType: "prompt_submit", sessionId, project, cwd, timestamp, data: { prompt, backfill: true } },
- {},
+ { hookType: "prompt_submit", sessionId, project, cwd, timestamp: at ?? timestamp, data: { prompt, backfill: true } },
+ promptId ? { prompt_id: promptId } : {},
{ source: "transcript", transcript: data.transcript_path, index, prompt },
{ stable: true },
),
diff --git a/test/session-end-transcript.test.ts b/test/session-end-transcript.test.ts
index 9433b515..c9f452e8 100644
--- a/test/session-end-transcript.test.ts
+++ b/test/session-end-transcript.test.ts
@@ -9,9 +9,12 @@ let server: Server;
let port: number;
const posts: Array<{ path: string; body: Record<string, unknown> }> = [];
-function runHook(payload: Record<string, unknown>): Promise<number> {
+function runHook(
+ payload: Record<string, unknown>,
+ script = "plugin/scripts/session-end.mjs",
+): Promise<number> {
return new Promise((resolve) => {
- const child = spawn("node", ["plugin/scripts/session-end.mjs"], {
+ const child = spawn("node", [script], {
env: { ...process.env, AGENTMEMORY_URL: `http://127.0.0.1:${port}` },
});
child.on("exit", (code) => resolve(code ?? 1));
@@ -104,6 +107,147 @@ describe("session-end transcript prompt backfill", () => {
}
});
+ it("posts prompts from a Claude Code transcript, skipping tool results", async () => {
+ posts.length = 0;
+ const transcript = join(dir, "t-cc.jsonl");
+ writeFileSync(
+ transcript,
+ [
+ JSON.stringify({
+ type: "user",
+ message: { role: "user", content: "bare claude code prompt" },
+ }),
+ JSON.stringify({
+ message: { role: "user", content: "nested role only" },
+ }),
+ JSON.stringify({
+ type: "assistant",
+ message: { role: "assistant", content: [{ type: "text", text: "answer" }] },
+ }),
+ JSON.stringify({
+ type: "user",
+ message: {
+ role: "user",
+ content: [
+ {
+ type: "tool_result",
+ content: [{ type: "text", text: "tool output, not a prompt" }],
+ },
+ ],
+ },
+ }),
+ JSON.stringify({
+ type: "user",
+ message: { role: "user", content: "follow-up after the tool" },
+ }),
+ JSON.stringify({
+ type: "user",
+ isSidechain: true,
+ message: { role: "user", content: "subagent instruction" },
+ }),
+ "",
+ ].join("\n"),
+ );
+
+ const code = await runHook({
+ session_id: "ses_cc",
+ hook_event_name: "sessionEnd",
+ reason: "completed",
+ transcript_path: transcript,
+ });
+ expect(code).toBe(0);
+
+ const observes = posts.filter((p) => p.path.includes("/observe"));
+ expect(observes.map((p) => (p.body.data as { prompt: string }).prompt)).toEqual([
+ "bare claude code prompt",
+ "nested role only",
+ "follow-up after the tool",
+ ]);
+ });
+
+ it("skips Claude Code records the user did not type", async () => {
+ posts.length = 0;
+ const transcript = join(dir, "t-cc-harness.jsonl");
+ const userLine = (content: string, extra: Record<string, unknown> = {}) =>
+ JSON.stringify({ type: "user", ...extra, message: { role: "user", content } });
+ writeFileSync(
+ transcript,
+ [
+ userLine("typed by the user"),
+ userLine("Base directory for this skill: /tmp/skill", { isMeta: true }),
+ userLine("This session is being continued from a previous conversation.", {
+ isCompactSummary: true,
+ }),
+ userLine("<command-name>/clear</command-name>"),
+ userLine("<command-message>review</command-message>"),
+ userLine("<local-command-stdout>done</local-command-stdout>"),
+ userLine("<local-command-caveat>Caveat: generated locally</local-command-caveat>"),
+ userLine("<task-notification><task-id>b1</task-id></task-notification>"),
+ userLine("<system-reminder>context</system-reminder>"),
+ userLine("<ci-monitor-event>build passed</ci-monitor-event>"),
+ userLine("[Request interrupted by user]"),
+ userLine("<bash-input>export API_TOKEN=secret && ls</bash-input>"),
+ userLine("<bash-stdout>file.txt</bash-stdout><bash-stderr></bash-stderr>"),
+ userLine("<bash-stderr>ls: no such file</bash-stderr>"),
+ userLine('<cross-session-message from="other-session">status?</cross-session-message>'),
+ userLine('<scheduled-task name="daily-check">run the check</scheduled-task>'),
+ "",
+ ].join("\n"),
+ );
+
+ await runHook({
+ session_id: "ses_cc_harness",
+ hook_event_name: "sessionEnd",
+ reason: "completed",
+ transcript_path: transcript,
+ });
+
+ const observes = posts.filter((p) => p.path.includes("/observe"));
+ expect(observes.map((p) => (p.body.data as { prompt: string }).prompt)).toEqual([
+ "typed by the user",
+ ]);
+ });
+
+ it("gives a backfilled Claude Code prompt the event id of its live capture", async () => {
+ posts.length = 0;
+ const transcript = join(dir, "t-cc-live.jsonl");
+ writeFileSync(
+ transcript,
+ [
+ JSON.stringify({
+ type: "user",
+ promptId: "8f1d2c3b-0000-4000-8000-000000000001",
+ timestamp: "2026-10-03T10:00:00.000Z",
+ message: { role: "user", content: "same prompt twice" },
+ }),
+ "",
+ ].join("\n"),
+ );
+
+ await runHook(
+ {
+ session_id: "ses_cc_live",
+ hook_event_name: "UserPromptSubmit",
+ prompt: "same prompt twice",
+ prompt_id: "8f1d2c3b-0000-4000-8000-000000000001",
+ transcript_path: transcript,
+ },
+ "plugin/scripts/prompt-submit.mjs",
+ );
+ await runHook({
+ session_id: "ses_cc_live",
+ hook_event_name: "sessionEnd",
+ reason: "completed",
+ transcript_path: transcript,
+ });
+
+ const observes = posts.filter((p) => p.path.includes("/observe"));
+ expect(observes).toHaveLength(2);
+ expect(observes[0].body.eventId).toBeTruthy();
+ expect(observes[1].body.eventId).toBe(observes[0].body.eventId);
+ expect(observes[1].body.timestamp).toBe("2026-10-03T10:00:00.000Z");
+ });
+
it("caps backfill at 50 prompts even within a single transcript record", async () => {
posts.length = 0;
const transcript = join(dir, "t-cap.jsonl");