| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- import { join } from "node:path";
- import { readFileSync } from "node:fs";
- import { describe, expect, it } from "vitest";
- import {
- createMessageId,
- createModelId,
- createModelProviderId,
- createPartId,
- createSessionId,
- type MessageId,
- type MessagePart,
- type MessageWithParts,
- type ToolState,
- } from "@zcode/contracts";
- import {
- buildMemoryExtractionPrompt,
- createMemoryExtractionScheduler,
- evaluateMemoryExtraction,
- type MemoryExtractionExecutionStatus,
- type MemoryExtractionSnapshot,
- } from "../src/memory/extraction.js";
- const SESSION_ID = createSessionId("memory-extraction");
- const PROVIDER_ID = createModelProviderId("test-provider");
- const MODEL_ID = createModelId("test-model");
- const WORKSPACE_ROOT = "/workspace";
- const MEMORY_ROOT = "/storage/memories/project/memory";
- describe("memory extraction prompt", () => {
- it("matches the frozen provider-visible fixture", () => {
- const timestamp = "2026-07-16T01:02:03.000Z";
- const fixture = readFileSync(
- new URL("./fixtures/memory/extraction-prompt.md", import.meta.url),
- "utf8",
- )
- .trimEnd()
- .replace("<TIMESTAMP>", timestamp);
- expect(
- buildMemoryExtractionPrompt({
- manifest: [
- {
- description: "Database tests must use the real database rather than mocks.",
- filePath: `${MEMORY_ROOT}/database-test-policy.md`,
- filename: "database-test-policy.md",
- mtimeMs: Date.parse(timestamp),
- type: "feedback",
- },
- ],
- messageCount: 2,
- }),
- ).toBe(fixture);
- });
- it("omits the existing-memory section when the manifest is empty", () => {
- const prompt = buildMemoryExtractionPrompt({ manifest: [], messageCount: 4 });
- expect(prompt).toContain("most recent ~4 messages");
- expect(prompt).not.toContain("## Existing memory files");
- });
- });
- describe("memory extraction gate", () => {
- it("requires one independent non-meta user text block with at least three words", () => {
- const cursor = userMessage("cursor", ["previous durable request"]);
- const shortBlocks = userMessage("short-blocks", ["one two", "three four"]);
- const meta = userMessage("meta", ["meta text has words"], { synthetic: true });
- const modelOnly = userMessage("model-only", ["hidden text has words"], {
- visibility: "model-only",
- });
- expect(
- evaluateMemoryExtraction(snapshot([cursor, shortBlocks, meta, modelOnly]), cursor.info.id),
- ).toEqual({ decision: "skip", messageCount: 3, reason: "no-user-prose" });
- const eligible = userMessage("eligible", ["one two", "one two three"]);
- expect(evaluateMemoryExtraction(snapshot([cursor, eligible]), cursor.info.id)).toEqual({
- decision: "run",
- messageCount: 1,
- });
- });
- it("skips after a contained Write or Edit regardless of tool status and filename extension", () => {
- const cursor = userMessage("cursor", ["previous durable request"]);
- const prose = userMessage("prose", ["save this durable preference"]);
- const statuses: ToolState[] = [
- { status: "pending", input: { file_path: join(MEMORY_ROOT, "pending.txt") }, raw: "" },
- {
- status: "error",
- input: { file_path: join(MEMORY_ROOT, "failed-without-extension") },
- error: "write failed",
- time: { start: 1, end: 2 },
- },
- ];
- for (const [index, state] of statuses.entries()) {
- const write = assistantToolMessage(`write-${index}`, index === 0 ? "Write" : "Edit", state);
- expect(evaluateMemoryExtraction(snapshot([cursor, prose, write]), cursor.info.id)).toEqual({
- decision: "skip",
- messageCount: 2,
- reason: "direct-memory-write",
- });
- }
- const outsideWrite = assistantToolMessage("outside", "Write", {
- status: "completed",
- input: { file_path: "/workspace/not-memory.md" },
- output: "ok",
- title: "write",
- metadata: {},
- time: { start: 1, end: 2 },
- });
- expect(
- evaluateMemoryExtraction(snapshot([cursor, prose, outsideWrite]), cursor.info.id),
- ).toEqual({ decision: "run", messageCount: 2 });
- });
- it("does not backscan direct writes when a non-empty cursor is missing", () => {
- const oldWrite = assistantToolMessage("old-write", "Write", {
- status: "completed",
- input: { file_path: join(MEMORY_ROOT, "old.md") },
- output: "ok",
- title: "write",
- metadata: {},
- time: { start: 1, end: 2 },
- });
- const prose = userMessage("new-prose", ["new durable preference here"]);
- expect(
- evaluateMemoryExtraction(snapshot([oldWrite, prose]), createMessageId("missing-cursor")),
- ).toEqual({ decision: "run", messageCount: 2 });
- });
- it("counts only conversation messages, not hidden announcement records", () => {
- const cursor = userMessage("cursor-hidden", ["already extracted"]);
- const announcement = hiddenReminder("env-announcement", "environment");
- const prose = userMessage("prose-hidden", ["remember the durable preference"]);
- const marker = hiddenReminder("interrupt", "interrupt_marker");
- // 隐藏公告不是对话消息,不计入 "~N messages";中断标记是普通 user 文本,计入。
- expect(
- evaluateMemoryExtraction(snapshot([cursor, announcement, prose, marker]), cursor.info.id),
- ).toEqual({ decision: "run", messageCount: 2 });
- });
- });
- describe("memory extraction scheduler", () => {
- it("reports pending work from schedule until the running extraction settles", async () => {
- const run = deferred<MemoryExtractionExecutionStatus>();
- const scheduler = createMemoryExtractionScheduler(async () => run.promise);
- expect(scheduler.hasPendingWork()).toBe(false);
- scheduler.schedule(snapshot([userMessage("pending", ["durable preference remains useful"])]));
- expect(scheduler.hasPendingWork()).toBe(true);
- run.resolve("success");
- await scheduler.drain();
- expect(scheduler.hasPendingWork()).toBe(false);
- });
- it("keeps one running extraction and replaces pending work with only the latest snapshot", async () => {
- const firstRun = deferred<MemoryExtractionExecutionStatus>();
- const executed: string[] = [];
- const scheduler = createMemoryExtractionScheduler(async ({ snapshot: input }) => {
- const id = input.durableMessages.at(-1)?.info.id;
- if (id) executed.push(id);
- if (id === createMessageId("a")) return firstRun.promise;
- return "success";
- });
- scheduler.schedule(snapshot([userMessage("a", ["alpha durable preference"])]));
- scheduler.schedule(snapshot([userMessage("b", ["beta durable preference"])]));
- scheduler.schedule(snapshot([userMessage("c", ["gamma durable preference"])]));
- await nextTask();
- expect(executed).toEqual([createMessageId("a")]);
- firstRun.resolve("success");
- await scheduler.drain();
- expect(executed).toEqual([createMessageId("a"), createMessageId("c")]);
- expect(scheduler.getCursor()).toBe(createMessageId("c"));
- });
- it.each(["success", "no-op"] as const)("advances the cursor after %s", async (status) => {
- const scheduler = createMemoryExtractionScheduler(async () => status);
- scheduler.schedule(snapshot([userMessage(status, ["durable preference to save"])]));
- await scheduler.drain();
- expect(scheduler.getCursor()).toBe(createMessageId(status));
- });
- it.each(["error", "aborted"] as const)("does not advance the cursor after %s", async (status) => {
- const scheduler = createMemoryExtractionScheduler(async () => status);
- scheduler.schedule(snapshot([userMessage(status, ["durable preference to save"])]));
- await scheduler.drain();
- expect(scheduler.getCursor()).toBeUndefined();
- });
- it("advances the cursor on direct-write and no-prose skips without executing the model callback", async () => {
- const executed: string[] = [];
- const scheduler = createMemoryExtractionScheduler(async ({ snapshot: input }) => {
- executed.push(input.durableMessages.at(-1)!.info.id);
- return "success";
- });
- const directWrite = assistantToolMessage("direct-write", "Write", {
- status: "error",
- input: { file_path: join(MEMORY_ROOT, "direct") },
- error: "failed",
- time: { start: 1, end: 2 },
- });
- scheduler.schedule(snapshot([directWrite]));
- await scheduler.drain();
- expect(scheduler.getCursor()).toBe(createMessageId("direct-write"));
- scheduler.schedule(
- snapshot([
- directWrite,
- userMessage("no-prose", ["one two", "three four"], { synthetic: false }),
- ]),
- );
- await scheduler.drain();
- expect(executed).toEqual([]);
- expect(scheduler.getCursor()).toBe(createMessageId("no-prose"));
- });
- it("does not advance on a thrown execution error and still runs the latest pending snapshot", async () => {
- const firstRun = deferred<void>();
- const executed: string[] = [];
- const scheduler = createMemoryExtractionScheduler(async ({ snapshot: input }) => {
- const id = input.durableMessages.at(-1)!.info.id;
- executed.push(id);
- if (id === createMessageId("throws")) {
- await firstRun.promise;
- throw new Error("model failed");
- }
- return "success";
- });
- scheduler.schedule(snapshot([userMessage("throws", ["first durable preference"])]));
- scheduler.schedule(snapshot([userMessage("latest", ["latest durable preference"])]));
- firstRun.resolve();
- await scheduler.drain();
- expect(executed).toEqual([createMessageId("throws"), createMessageId("latest")]);
- expect(scheduler.getCursor()).toBe(createMessageId("latest"));
- });
- it("does not strand a snapshot scheduled while the running extraction completes", async () => {
- const firstRun = deferred<MemoryExtractionExecutionStatus>();
- const executed: string[] = [];
- const scheduler = createMemoryExtractionScheduler(async ({ snapshot: input }) => {
- const id = input.durableMessages.at(-1)!.info.id;
- executed.push(id);
- if (id === createMessageId("first")) return firstRun.promise;
- return "success";
- });
- scheduler.schedule(snapshot([userMessage("first", ["first durable preference"])]));
- await nextTask();
- const scheduleDuringCleanup = new Promise<void>((resolve) => {
- void firstRun.promise.then(() => {
- queueMicrotask(() => {
- queueMicrotask(() => {
- scheduler.schedule(
- snapshot([userMessage("during-cleanup", ["latest durable preference"])]),
- );
- resolve();
- });
- });
- });
- });
- firstRun.resolve("success");
- await scheduleDuringCleanup;
- await scheduler.drain();
- expect(executed).toEqual([createMessageId("first"), createMessageId("during-cleanup")]);
- expect(scheduler.getCursor()).toBe(createMessageId("during-cleanup"));
- });
- it("does not advance the cursor for a snapshot acquisition error and still runs latest pending", async () => {
- const executed: string[] = [];
- const scheduler = createMemoryExtractionScheduler(async ({ snapshot: input }) => {
- executed.push(input.boundaryMessageId);
- return "success";
- });
- scheduler.schedule(Promise.reject(new Error("boundary missing")));
- scheduler.schedule(snapshot([userMessage("latest", ["latest durable preference"])]));
- await scheduler.drain();
- expect(executed).toEqual([createMessageId("latest")]);
- expect(scheduler.getCursor()).toBe(createMessageId("latest"));
- });
- it("aborts the running extraction, drops pending work, and rejects later schedules on shutdown", async () => {
- const runningSignal = deferred<AbortSignal>();
- const executed: MessageId[] = [];
- const scheduler = createMemoryExtractionScheduler(async ({ abortSignal, snapshot: input }) => {
- executed.push(input.boundaryMessageId);
- runningSignal.resolve(abortSignal);
- await waitForAbort(abortSignal);
- return "aborted";
- });
- scheduler.schedule(snapshot([userMessage("running", ["running durable preference"])]));
- const signal = await runningSignal.promise;
- scheduler.schedule(snapshot([userMessage("pending", ["pending durable preference"])]));
- scheduler.shutdown();
- await scheduler.drain();
- scheduler.schedule(snapshot([userMessage("after-close", ["later durable preference"])]));
- await nextTask();
- expect(signal.aborted).toBe(true);
- expect(executed).toEqual([createMessageId("running")]);
- expect(scheduler.getCursor()).toBeUndefined();
- });
- it("stops waiting for snapshot acquisition when shutdown starts", async () => {
- const snapshotGate = deferred<MemoryExtractionSnapshot>();
- let executeCount = 0;
- const scheduler = createMemoryExtractionScheduler(async () => {
- executeCount += 1;
- return "success";
- });
- scheduler.schedule(snapshotGate.promise);
- scheduler.shutdown();
- await scheduler.drain();
- expect(executeCount).toBe(0);
- expect(scheduler.getCursor()).toBeUndefined();
- });
- });
- function snapshot(messages: MessageWithParts[]): MemoryExtractionSnapshot {
- return {
- boundaryMessageId: messages.at(-1)!.info.id,
- durableMessages: messages,
- memoryRoot: MEMORY_ROOT,
- workingDirectory: WORKSPACE_ROOT,
- workspaceRoot: WORKSPACE_ROOT,
- };
- }
- function userMessage(
- id: string,
- textBlocks: string[],
- options: { synthetic?: boolean; visibility?: "model-only" | "user-visible" } = {},
- ): MessageWithParts {
- const messageId = createMessageId(id);
- return {
- info: {
- id: messageId,
- sessionID: SESSION_ID,
- role: "user",
- time: { created: 1 },
- agent: "zcode-agent",
- model: { providerID: PROVIDER_ID, modelID: MODEL_ID },
- synthetic: options.synthetic,
- visibility: options.visibility,
- },
- parts: textBlocks.map((text, index) => ({
- id: createPartId(`${id}-${index}`),
- sessionID: SESSION_ID,
- messageID: messageId,
- type: "text",
- text,
- })),
- };
- }
- function hiddenReminder(id: string, source: string): MessageWithParts {
- const message = userMessage(id, [`<system-reminder>${source}</system-reminder>`], {
- synthetic: true,
- visibility: "model-only",
- });
- message.info.semantics = {
- origin: "agent_runtime",
- kind: "system_reminder",
- source,
- uiVisibility: "hidden",
- providerVisibility: "visible",
- transcriptVisibility: "hidden",
- };
- // 与 buildRuntimeReminderMessage 写入的形状一致:类型记在 metadata.runtimeMessage,没有顶层 source。
- message.info.metadata = { runtimeMessage: { source }, source, visibility: "model-only" };
- return message;
- }
- function assistantToolMessage(
- id: string,
- tool: "Edit" | "Write",
- state: ToolState,
- ): MessageWithParts {
- const messageId = createMessageId(id);
- const parentId = createMessageId(`${id}-parent`);
- const part: MessagePart = {
- id: createPartId(`${id}-tool`),
- sessionID: SESSION_ID,
- messageID: messageId,
- type: "tool",
- callID: `${id}-call`,
- tool,
- state,
- };
- return {
- info: {
- id: messageId,
- sessionID: SESSION_ID,
- role: "assistant",
- time: { created: 1, completed: 2 },
- parentID: parentId,
- agent: "zcode-agent",
- cost: 0,
- mode: "build",
- modelID: MODEL_ID,
- path: { cwd: WORKSPACE_ROOT, root: WORKSPACE_ROOT },
- providerID: PROVIDER_ID,
- tokens: {
- cache: { read: 0, write: 0 },
- input: 0,
- output: 0,
- reasoning: 0,
- },
- },
- parts: [part],
- };
- }
- function deferred<T>() {
- let resolve!: (value: T | PromiseLike<T>) => void;
- const promise = new Promise<T>((settle) => {
- resolve = settle;
- });
- return { promise, resolve };
- }
- async function nextTask(): Promise<void> {
- await new Promise<void>((resolve) => setTimeout(resolve, 0));
- }
- async function waitForAbort(signal: AbortSignal): Promise<void> {
- if (signal.aborted) return;
- await new Promise<void>((resolve) => {
- signal.addEventListener("abort", () => resolve(), { once: true });
- });
- }
|