diff --git a/scripts/lib/dev-seed.test.ts b/scripts/lib/dev-seed.test.ts index 70f5ee1223..d68b0e4358 100644 --- a/scripts/lib/dev-seed.test.ts +++ b/scripts/lib/dev-seed.test.ts @@ -39,10 +39,12 @@ function createSchema( message_id TEXT PRIMARY KEY, thread_id TEXT NOT NULL, role TEXT NOT NULL, text TEXT NOT NULL, is_streaming INTEGER NOT NULL, attachments_json TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL)`); + // `sequence` (migration 008) is what the app orders activities by, ahead of + // created_at, so the copy's per-thread cap has to see it. database.exec(`CREATE TABLE projection_thread_activities ( activity_id TEXT PRIMARY KEY, thread_id TEXT NOT NULL, tone TEXT NOT NULL, kind TEXT NOT NULL, summary TEXT NOT NULL, payload_json TEXT NOT NULL, - created_at TEXT NOT NULL)`); + sequence INTEGER, created_at TEXT NOT NULL)`); database.exec(`CREATE TABLE projection_thread_sessions ( thread_id TEXT PRIMARY KEY, status TEXT NOT NULL, provider_name TEXT, active_turn_id TEXT, last_error TEXT, updated_at TEXT NOT NULL)`); @@ -155,8 +157,16 @@ function makeSource( ); for (let a = 0; a < activitiesPerThread; a += 1) { database - .prepare(`INSERT INTO projection_thread_activities VALUES (?,?,'neutral','tool','ran',?,?)`) - .run(`a-${threadId}-${String(a)}`, threadId, "{}", `2026-07-10T00:00:0${String(a)}.000Z`); + .prepare( + `INSERT INTO projection_thread_activities VALUES (?,?,'neutral','tool','ran',?,?,?)`, + ) + .run( + `a-${threadId}-${String(a)}`, + threadId, + "{}", + a, + `2026-07-10T00:00:0${String(a)}.000Z`, + ); } } database.exec("COMMIT"); @@ -375,6 +385,41 @@ describe("seedDevDatabase", () => { }); }); + // A burst of tool activity lands many rows on the same timestamp. Capping on + // created_at alone then cuts arbitrarily among them, and can keep exactly the + // rows the timeline treats as oldest. + it("caps activities by the order the app displays them", () => { + withDatabases(({ source, target }) => { + const sourceDatabase = new NodeSqlite.DatabaseSync(source); + sourceDatabase.exec("DELETE FROM projection_thread_activities"); + const tied = "2026-07-10T00:00:00.000Z"; + for (const sequence of [1, 2, 3, 4]) { + sourceDatabase + .prepare( + `INSERT INTO projection_thread_activities + VALUES (?, 't4', 'neutral', 'tool', 'ran', '{}', ?, ?)`, + ) + .run(`tie-${String(sequence)}`, sequence, tied); + } + sourceDatabase.close(); + + seedDevDatabase({ + sourceDbPath: source, + targetDbPath: target, + threadLimit: 1, + activityLimit: 2, + seededAt: SEEDED_AT, + }); + + const kept = query<{ activity_id: string }>( + target, + "SELECT activity_id FROM projection_thread_activities ORDER BY sequence DESC", + ).map((row) => row.activity_id); + // The two highest sequences, not whichever two the storage order yielded. + assert.deepStrictEqual(kept, ["tie-4", "tie-3"]); + }); + }); + // The delete mirrors the server's predicate, which spares rows carrying a // checkpoint. Without the `checkpoint_turn_count IS NULL` clause a checkpoint // row that happened to be pending would be destroyed along with the starts. diff --git a/scripts/lib/dev-seed.ts b/scripts/lib/dev-seed.ts index 925d3982b8..fa05af6489 100644 --- a/scripts/lib/dev-seed.ts +++ b/scripts/lib/dev-seed.ts @@ -553,7 +553,20 @@ export function seedDevDatabase(options: DevSeedOptions): DevSeedSummary { table: "projection_thread_activities", keyColumn: "thread_id", keys: threadIds, - perKeyLimit: { orderBy: "created_at", limit: options.activityLimit }, + // Ordered the way the app reads these back — `sequence` first, then + // `created_at` (ProjectionSnapshotQuery). Capping on `created_at` + // alone cuts arbitrarily among rows sharing a timestamp, which is the + // common case for a burst of tool activity: the "newest N" kept can be + // the oldest N in the order the timeline actually displays. + // + // `sequence` arrived in migration 008, so a source without it falls + // back to the timestamp — same drift tolerance as the rest of the copy. + perKeyLimit: { + orderBy: hasColumn(source, "projection_thread_activities", "sequence") + ? `"created_at" DESC, "sequence"` + : `"created_at"`, + limit: options.activityLimit, + }, }), ); const sessions = record(