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
6 changes: 3 additions & 3 deletions src/store-artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ export async function externalizeMessage(
const { storedPart, artifacts: nextArtifacts } = await externalizePart(
bindings,
part,
message.info.time.created,
message.info?.time?.created ?? 0,
);
artifacts.push(...nextArtifacts);
storedParts.push(storedPart);
Expand Down Expand Up @@ -509,7 +509,7 @@ export async function externalizeSession(
const { storedPart, artifacts: nextArtifacts } = await externalizePart(
bindings,
part,
message.info.time.created,
message.info?.time?.created ?? 0,
);
artifacts.push(...nextArtifacts);
storedParts.push(storedPart);
Expand Down Expand Up @@ -607,7 +607,7 @@ export function persistStoredSessionSync(
insertMessage.run(
message.info.id,
storedSession.sessionID,
message.info.time.created,
message.info?.time?.created ?? 0,
JSON.stringify(message.info),
);

Expand Down
12 changes: 9 additions & 3 deletions src/store-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export function searchByScan(
id: message.info.id,
type: message.info.role,
sessionID: session.sessionID,
timestamp: message.info.time.created,
timestamp: message.info?.time?.created ?? 0,
snippet: buildSnippet(blob, query),
content: blob,
sourceKind: 'message',
Expand Down Expand Up @@ -361,7 +361,7 @@ function insertMessageSearchRowsSync(deps: FtsDeps, session: NormalizedSession):
session.sessionID,
message.info.id,
message.info.role,
String(message.info.time.created),
String(message.info?.time?.created ?? 0),
content,
);
}
Expand All @@ -380,7 +380,13 @@ export function replaceMessageSearchRowSync(

db.prepare(
'INSERT INTO message_fts (session_id, message_id, role, created_at, content) VALUES (?, ?, ?, ?, ?)',
).run(sessionID, message.info.id, message.info.role, String(message.info.time.created), content);
).run(
sessionID,
message.info.id,
message.info.role,
String(message.info?.time?.created ?? 0),
content,
);
}

export function replaceSummarySearchRowsSync(deps: FtsDeps, sessionIDs?: string[]): void {
Expand Down
14 changes: 7 additions & 7 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1695,7 +1695,7 @@ export class SqliteLcmStore {
return issues.length > 0 ? { sessionID: session.sessionID, issues } : undefined;
}

const latestMessageCreated = archived.at(-1)?.info.time.created ?? 0;
const latestMessageCreated = archived.at(-1)?.info?.time?.created ?? 0;
const archivedSignature = this.buildArchivedSignature(archived);
const rootIDs = state ? parseJson<string[]>(state.root_node_ids_json) : [];
const roots = rootIDs
Expand Down Expand Up @@ -1848,7 +1848,7 @@ export class SqliteLcmStore {
return this.isMessageArchivedSync(
session.sessionID,
existing.info.id,
existing.info.time.created,
existing.info?.time?.created ?? 0,
);
}

Expand All @@ -1873,7 +1873,7 @@ export class SqliteLcmStore {
return this.isMessageArchivedSync(
session.sessionID,
message.info.id,
message.info.time.created,
message.info?.time?.created ?? 0,
);
}
case 'message.part.removed': {
Expand All @@ -1884,7 +1884,7 @@ export class SqliteLcmStore {
return this.isMessageArchivedSync(
session.sessionID,
message.info.id,
message.info.time.created,
message.info?.time?.created ?? 0,
);
}
default:
Expand Down Expand Up @@ -3619,7 +3619,7 @@ export class SqliteLcmStore {
for (const message of messages) {
hash.update(message.info.id);
hash.update(message.info.role);
hash.update(String(message.info.time.created));
hash.update(String(message.info?.time?.created ?? 0));
hash.update(guessMessageText(message, this.options.interop.ignoreToolPrefixes));
hash.update(JSON.stringify(listFiles(message)));
hash.update(JSON.stringify(this.listTools([message])));
Expand Down Expand Up @@ -3650,7 +3650,7 @@ export class SqliteLcmStore {
return [];
}

const latestMessageCreated = archivedMessages.at(-1)?.info.time.created ?? 0;
const latestMessageCreated = archivedMessages.at(-1)?.info?.time?.created ?? 0;
const archivedSignature = this.buildArchivedSignature(archivedMessages);
const state = safeQueryOne<SummaryStateRow>(
this.getDb().prepare('SELECT * FROM summary_state WHERE session_id = ?'),
Expand Down Expand Up @@ -3890,7 +3890,7 @@ export class SqliteLcmStore {
).run(
sessionID,
archivedMessages.length,
archivedMessages.at(-1)?.info.time.created ?? 0,
archivedMessages.at(-1)?.info?.time?.created ?? 0,
archivedSignature,
JSON.stringify(roots.map((node) => node.nodeID)),
now,
Expand Down
48 changes: 48 additions & 0 deletions tests/store-transform.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,54 @@ test('resume and describe skip stored messages with malformed metadata', async (
}
});

test('grep scan fallback skips stored messages with malformed metadata', async () => {
const workspace = makeWorkspace('lcm-grep-malformed-info');
const dbPath = path.join(workspace, '.lcm', 'lcm.db');
let store;
let db;

try {
store = new SqliteLcmStore(workspace, makeOptions());
await store.init();

await createSession(store, workspace, 's1', 1);
for (const [messageID, created, text] of [
['m1', 2, 'fallback => keep this result'],
['m2', 3, 'fallback => skip this result'],
]) {
await captureMessage(store, {
sessionID: 's1',
messageID,
created,
parts: [textPart('s1', messageID, `${messageID}-p`, text)],
});
}

db = new DatabaseSync(dbPath, {
enableForeignKeyConstraints: true,
timeout: 5000,
});
db.prepare('UPDATE messages SET info_json = ? WHERE session_id = ? AND message_id = ?').run(
JSON.stringify({ id: 'm2' }),
's1',
'm2',
);
db.close();
db = undefined;

const results = await store.grep({ query: '=>', sessionID: 's1', limit: 5 });

assert.deepEqual(
results.map((result) => result.id),
['m1'],
);
} finally {
db?.close();
store?.close();
await cleanupWorkspace(workspace);
}
});

test('capture ignores malformed message and part update events', async () => {
const workspace = makeWorkspace('lcm-capture-malformed-events');
let store;
Expand Down