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
61 changes: 27 additions & 34 deletions src/services/api/memory-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,44 +89,37 @@ export async function handleListMemories(
timeline = [...keptMemories, ...promptsWithType];
}

const linkedPairs = new Map<string, { memory: any; prompt: any }>();
const standalone: any[] = [];
for (const item of timeline) {
if (item.type === "memory" && item.linkedPromptId) {
if (!linkedPairs.has(item.linkedPromptId)) {
linkedPairs.set(item.linkedPromptId, { memory: item, prompt: null });
} else {
linkedPairs.get(item.linkedPromptId)!.memory = item;
}
} else if (item.type === "prompt" && item.linkedMemoryId) {
if (!linkedPairs.has(item.id)) {
linkedPairs.set(item.id, { memory: null, prompt: item });
} else {
linkedPairs.get(item.id)!.prompt = item;
}
} else {
standalone.push(item);
}
}
// Pair by the prompt row's linked_memory_id. Capture writes it for every
// host. A memory's metadata.promptId holds the host message ID for newer
// captures, so it matches a prompt row ID only on older rows.
const memoriesById = new Map<string, any>();
for (const item of timeline) if (item.type === "memory") memoriesById.set(item.id, item);
const promptsById = new Map<string, any>();
for (const item of timeline) if (item.type === "prompt") promptsById.set(item.id, item);

const sortedTimeline: any[] = [];
const pairValues = Array.from(linkedPairs.values());
const pairs = pairValues
.filter((p) => p.memory && p.prompt)
.sort((a, b) => b.memory.createdAt - a.memory.createdAt);
const paired = new Set<any>();
const units: Array<{ createdAt: number; items: any[] }> = [];
const addPair = (memory: any, prompt: any) => {
paired.add(memory);
paired.add(prompt);
memory.linkedPromptId = prompt.id;
units.push({ createdAt: memory.createdAt, items: [memory, prompt] });
};
for (const prompt of promptsById.values()) {
const memory = prompt.linkedMemoryId && memoriesById.get(prompt.linkedMemoryId);
if (memory && !paired.has(memory)) addPair(memory, prompt);
}
for (const memory of memoriesById.values()) {
const prompt = memory.linkedPromptId && promptsById.get(memory.linkedPromptId);
if (!paired.has(memory) && prompt && !paired.has(prompt)) addPair(memory, prompt);
}
// A memory or prompt whose counterpart is missing (linked prompt deleted,
// or prompt capture off) must still show up in the timeline, unlinked.
for (const pair of pairValues) {
if (pair.memory && !pair.prompt) standalone.push(pair.memory);
else if (pair.prompt && !pair.memory) standalone.push(pair.prompt);
}
for (const pair of pairs) {
sortedTimeline.push(pair.memory);
sortedTimeline.push(pair.prompt);
for (const item of timeline) {
if (!paired.has(item)) units.push({ createdAt: item.createdAt, items: [item] });
}
standalone.sort((a, b) => b.createdAt - a.createdAt);
sortedTimeline.push(...standalone);
timeline = sortedTimeline;
units.sort((a, b) => b.createdAt - a.createdAt);
timeline = units.flatMap((unit) => unit.items);

const total = timeline.length;
const totalPages = Math.ceil(total / pageSize);
Expand Down
13 changes: 7 additions & 6 deletions src/services/api/memory-write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,17 @@ export async function handleDeleteMemory(
for (const shard of shards) {
const memory = await getMemoryRow(shard, id);
if (memory) {
let deletedPrompt = false;
if (cascade) {
const metadata = safeJSONParse(memory.metadata);
const linkedPromptId = metadata?.promptId;
// Older memories store the prompt row ID in metadata.promptId. Newer
// ones store the host message ID, so also follow the prompt's link.
const linkedPromptId = safeJSONParse(memory.metadata)?.promptId;
if (linkedPromptId) await userPromptManager.deletePrompt(linkedPromptId);
const linkedCount = await userPromptManager.deletePromptsLinkedToMemory(id);
deletedPrompt = !!linkedPromptId || linkedCount > 0;
}
if (await deleteMemoryRow(shard, id)) await decrementShardCount(shard.id);
return {
success: true,
data: { deletedPrompt: cascade && !!safeJSONParse(memory.metadata)?.promptId },
};
return { success: true, data: { deletedPrompt } };
}
}
return { success: false, error: "Memory not found" };
Expand Down
6 changes: 6 additions & 0 deletions src/services/storage/user-prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ export class UserPromptManager {
await db.run(`DELETE FROM user_prompts WHERE id = ?`, [promptId]);
}

/** Deletes the prompts whose linked_memory_id is `memoryId`; returns how many went. */
async deletePromptsLinkedToMemory(memoryId: string): Promise<number> {
const db = await this.ready();
return db.run(`DELETE FROM user_prompts WHERE linked_memory_id = ?`, [memoryId]);
}

async markAsCaptured(promptId: string): Promise<void> {
const db = await this.ready();
await db.run(`UPDATE user_prompts SET captured = 1 WHERE id = ?`, [promptId]);
Expand Down
145 changes: 145 additions & 0 deletions tests/memory-timeline-orphan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,111 @@ describe("memory timeline listing", () => {
expect(ids.indexOf("mem_linked")).toBeLessThan(ids.indexOf(promptId));
});

it("lists the newest turns first when memories store the host message ID", async () => {
baseDir = mkdtempSync(join(tmpdir(), "timeline-newest-"));

const { CONFIG } = await import("../src/config.js");
CONFIG.storagePath = baseDir;

const { ensureTursoReady } = await import("../src/services/turso/ready.js");
await ensureTursoReady();

const { tursoConnectionManager } = await import("../src/services/turso/connection-manager.js");
const { tursoShardManager } = await import("../src/services/turso/shard-manager.js");
const { tursoVectorSearch } = await import("../src/services/turso/vector-search.js");
const { handleListMemories } = await import("../src/services/api-handlers.js");
const { userPromptManager } =
await import("../src/services/user-prompt/user-prompt-manager.js");

const vector = new Float32Array(CONFIG.embeddingDimensions);
vector[0] = 1;
const scopeHash = "a1b2c3d4e5f67890";
const containerTag = `opencode_project_${scopeHash}`;
const shard = await tursoShardManager.createShard("project", scopeHash, 0);
const db = await tursoConnectionManager.getConnection(shard.dbPath);
const now = Date.now();

// An old pair whose memory stores the OMMS prompt row ID.
const oldPrompt = await userPromptManager.savePrompt("s", "msg-old", "C:/proj", "old prompt");
await userPromptManager.markAsCaptured(oldPrompt);
await tursoVectorSearch.insertVector(db, {
id: "mem_old",
content: "Old memory",
vector,
containerTag,
metadata: JSON.stringify({ promptId: oldPrompt }),
createdAt: now - 60_000,
updatedAt: now - 60_000,
});
await userPromptManager.linkMemoryToPrompt(oldPrompt, "mem_old");

// A new pair: capture now stores the host message ID as promptId.
const newPrompt = await userPromptManager.savePrompt("s", "msg-new", "C:/proj", "new prompt");
await userPromptManager.markAsCaptured(newPrompt);
await tursoVectorSearch.insertVector(db, {
id: "mem_new",
content: "New memory",
vector,
containerTag,
metadata: JSON.stringify({ promptId: "msg-new" }),
createdAt: now,
updatedAt: now,
});
await userPromptManager.linkMemoryToPrompt(newPrompt, "mem_new");

const result = await handleListMemories(undefined, 1, 2, true);
expect(result.success).toBe(true);
const items = (result.data as { items: Array<{ id: string; linkedPromptId?: string }> }).items;
expect(items.map((i) => i.id)).toEqual(["mem_new", newPrompt]);
expect(items[0].linkedPromptId).toBe(newPrompt);
});

it("places an unlinked newer prompt above older pairs", async () => {
baseDir = mkdtempSync(join(tmpdir(), "timeline-standalone-"));

const { CONFIG } = await import("../src/config.js");
CONFIG.storagePath = baseDir;

const { ensureTursoReady } = await import("../src/services/turso/ready.js");
await ensureTursoReady();

const { tursoConnectionManager } = await import("../src/services/turso/connection-manager.js");
const { tursoShardManager } = await import("../src/services/turso/shard-manager.js");
const { tursoVectorSearch } = await import("../src/services/turso/vector-search.js");
const { handleListMemories } = await import("../src/services/api-handlers.js");
const { userPromptManager } =
await import("../src/services/user-prompt/user-prompt-manager.js");

const vector = new Float32Array(CONFIG.embeddingDimensions);
vector[0] = 1;
const scopeHash = "a1b2c3d4e5f67890";
const containerTag = `opencode_project_${scopeHash}`;
const shard = await tursoShardManager.createShard("project", scopeHash, 0);
const db = await tursoConnectionManager.getConnection(shard.dbPath);

const pairedPrompt = await userPromptManager.savePrompt("s", "m1", "C:/proj", "paired");
await userPromptManager.markAsCaptured(pairedPrompt);
await tursoVectorSearch.insertVector(db, {
id: "mem_paired",
content: "Paired memory",
vector,
containerTag,
metadata: JSON.stringify({ promptId: pairedPrompt }),
createdAt: Date.now() - 60_000,
updatedAt: Date.now() - 60_000,
});
await userPromptManager.linkMemoryToPrompt(pairedPrompt, "mem_paired");

// Saved after the pair, so its createdAt is newer. No memory came from it.
const lonePrompt = await userPromptManager.savePrompt("s", "m2", "C:/proj", "no memory");
await userPromptManager.markAsCaptured(lonePrompt);

const result = await handleListMemories(undefined, 1, 20, true);
expect(result.success).toBe(true);
const ids = (result.data as { items: Array<{ id: string }> }).items.map((i) => i.id);
expect(ids).toEqual([lonePrompt, "mem_paired", pairedPrompt]);
});

it("filters the timeline by memory keyword and keeps only linked prompts", async () => {
baseDir = mkdtempSync(join(tmpdir(), "timeline-keyword-"));

Expand Down Expand Up @@ -165,4 +270,44 @@ describe("memory timeline listing", () => {
expect(ids).not.toContain("mem_other");
expect(ids).not.toContain(otherPrompt);
});

it("cascade delete removes the prompt linked to the memory", async () => {
baseDir = mkdtempSync(join(tmpdir(), "timeline-cascade-"));

const { CONFIG } = await import("../src/config.js");
CONFIG.storagePath = baseDir;

const { ensureTursoReady } = await import("../src/services/turso/ready.js");
await ensureTursoReady();

const { tursoConnectionManager } = await import("../src/services/turso/connection-manager.js");
const { tursoShardManager } = await import("../src/services/turso/shard-manager.js");
const { tursoVectorSearch } = await import("../src/services/turso/vector-search.js");
const { handleDeleteMemory } = await import("../src/services/api-handlers.js");
const { userPromptManager } =
await import("../src/services/user-prompt/user-prompt-manager.js");

const vector = new Float32Array(CONFIG.embeddingDimensions);
vector[0] = 1;
const scopeHash = "a1b2c3d4e5f67890";
const shard = await tursoShardManager.createShard("project", scopeHash, 0);
const db = await tursoConnectionManager.getConnection(shard.dbPath);

const promptId = await userPromptManager.savePrompt("s", "msg-host", "C:/proj", "prompt");
await userPromptManager.markAsCaptured(promptId);
await tursoVectorSearch.insertVector(db, {
id: "mem_cascade",
content: "Memory",
vector,
containerTag: `opencode_project_${scopeHash}`,
metadata: JSON.stringify({ promptId: "msg-host" }),
createdAt: Date.now(),
updatedAt: Date.now(),
});
await userPromptManager.linkMemoryToPrompt(promptId, "mem_cascade");

const result = await handleDeleteMemory("mem_cascade", true);
expect(result).toEqual({ success: true, data: { deletedPrompt: true } });
expect(await userPromptManager.getPromptById(promptId)).toBeNull();
});
});
6 changes: 6 additions & 0 deletions tests/web-api-memory-characterisation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const originals = {
getPromptsByIds: userPromptManager.getPromptsByIds,
getCapturedPrompts: userPromptManager.getCapturedPrompts,
deletePrompt: userPromptManager.deletePrompt,
deletePromptsLinkedToMemory: userPromptManager.deletePromptsLinkedToMemory,
};

function record(name: string, args: unknown[]): void {
Expand Down Expand Up @@ -74,6 +75,10 @@ beforeEach(() => {
userPromptManager.deletePrompt = (async (...args: unknown[]) => {
record("deletePrompt", args);
}) as typeof userPromptManager.deletePrompt;
userPromptManager.deletePromptsLinkedToMemory = (async (...args: unknown[]) => {
record("deletePromptsLinkedToMemory", args);
return 0;
}) as typeof userPromptManager.deletePromptsLinkedToMemory;
});

afterEach(async () => {
Expand All @@ -86,6 +91,7 @@ afterEach(async () => {
getPromptsByIds: originals.getPromptsByIds,
getCapturedPrompts: originals.getCapturedPrompts,
deletePrompt: originals.deletePrompt,
deletePromptsLinkedToMemory: originals.deletePromptsLinkedToMemory,
});
await closeTursoAndInvalidateCaches();
for (const folder of folders.splice(0)) await cleanupTursoTestDirectory(folder);
Expand Down