Cap activities by the order the app displays them

Auditing the fixture against the real migrations (the method that found
the missing checkpoint column) turned up a second gap: the test schema
omitted `projection_thread_activities.sequence`, and the copy's per-thread
cap ordered on `created_at` alone.

The app reads these back ordered by `sequence` first, then `created_at`
(ProjectionSnapshotQuery). A burst of tool activity lands many rows on
one timestamp, so capping on the timestamp alone cuts arbitrarily among
them — demonstrated with four tied rows where "keep the newest two" kept
the two the timeline treats as oldest.

Ordered to match, falling back to the timestamp when the source predates
migration 008. Fixture gained the column, and the regression test fails
against the old ordering.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5 committed 2026-07-26 20:38:49 -07:00
1 parent a37db1a837
commit dc375f336d
2 files changed
+62 -4

No files matched your search

+48 -3
View File
@@ -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.
+14 -1
View File
@@ -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(