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
14 changes: 14 additions & 0 deletions packages/core/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,20 @@ const MIGRATIONS: string[] = [
);
CREATE INDEX IF NOT EXISTS idx_import_history_project ON import_history(project_id);
`,
`
-- Version 20: Purge worker boilerplate from temporal messages.
-- Legacy gateway/plugin worker calls (distillation observer, curator,
-- consolidation, reflector, eval) stored their full system prompts
-- (containing entire conversation transcripts, up to 1.6MB each) as
-- temporal messages. These pollute FTS search results by matching
-- virtually any domain keyword. Safe to delete: their actual output
-- (distillations, knowledge entries) is stored in dedicated tables.
DELETE FROM temporal_messages WHERE content LIKE '%You are a memory observer.%'
OR content LIKE '%You are a long-term memory curator.%'
OR content LIKE '%You are a long-term memory curator performing a consolidation pass.%'
OR content LIKE '%You are a memory reflector.%'
OR content LIKE '%You are evaluating distillation quality.%';
`,
];

/** Return the resolved path of the SQLite database file. */
Expand Down
14 changes: 8 additions & 6 deletions packages/core/src/temporal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ export function markDistilled(ids: string[]) {
.run(...ids);
}

// Only searches undistilled messages — distilled content is already represented
// in distillation search results and would duplicate/dilute temporal hits.
// LIKE-based fallback for when FTS5 fails unexpectedly.
function searchLike(input: {
pid: string;
Expand All @@ -186,8 +188,8 @@ function searchLike(input: {
const conditions = terms.map(() => "LOWER(content) LIKE ?").join(" AND ");
const likeParams = terms.map((t) => `%${t}%`);
const query = input.sessionID
? `SELECT * FROM temporal_messages WHERE project_id = ? AND session_id = ? AND ${conditions} ORDER BY created_at DESC LIMIT ?`
: `SELECT * FROM temporal_messages WHERE project_id = ? AND ${conditions} ORDER BY created_at DESC LIMIT ?`;
? `SELECT * FROM temporal_messages WHERE project_id = ? AND session_id = ? AND distilled = 0 AND ${conditions} ORDER BY created_at DESC LIMIT ?`
: `SELECT * FROM temporal_messages WHERE project_id = ? AND distilled = 0 AND ${conditions} ORDER BY created_at DESC LIMIT ?`;
const params = input.sessionID
? [input.pid, input.sessionID, ...likeParams, input.limit]
: [input.pid, ...likeParams, input.limit];
Expand All @@ -208,11 +210,11 @@ export function search(input: {
const ftsSQL = input.sessionID
? `SELECT m.* FROM temporal_fts f
CROSS JOIN temporal_messages m ON m.rowid = f.rowid
WHERE f.content MATCH ? AND m.project_id = ? AND m.session_id = ?
WHERE f.content MATCH ? AND m.project_id = ? AND m.session_id = ? AND m.distilled = 0
ORDER BY rank LIMIT ?`
: `SELECT m.* FROM temporal_fts f
CROSS JOIN temporal_messages m ON m.rowid = f.rowid
WHERE f.content MATCH ? AND m.project_id = ?
WHERE f.content MATCH ? AND m.project_id = ? AND m.distilled = 0
ORDER BY rank LIMIT ?`;

try {
Expand Down Expand Up @@ -251,11 +253,11 @@ export function searchScored(input: {
const ftsSQL = input.sessionID
? `SELECT m.*, rank FROM temporal_fts f
CROSS JOIN temporal_messages m ON m.rowid = f.rowid
WHERE f.content MATCH ? AND m.project_id = ? AND m.session_id = ?
WHERE f.content MATCH ? AND m.project_id = ? AND m.session_id = ? AND m.distilled = 0
ORDER BY rank LIMIT ?`
: `SELECT m.*, rank FROM temporal_fts f
CROSS JOIN temporal_messages m ON m.rowid = f.rowid
WHERE f.content MATCH ? AND m.project_id = ?
WHERE f.content MATCH ? AND m.project_id = ? AND m.distilled = 0
ORDER BY rank LIMIT ?`;

try {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("db", () => {
const row = db().query("SELECT version FROM schema_version").get() as {
version: number;
};
expect(row.version).toBe(19);
expect(row.version).toBe(20);
});

test("distillation_fts virtual table exists", () => {
Expand Down
Loading