Keep seeded development state internally consistent
This commit is contained in:
3 files changed
+125
-26
No files matched your search
+14
-7
@@ -166,16 +166,23 @@ const copyAttachments = Effect.fn("devSeed.copyAttachments")(function* (input: {
|
||||
return 0;
|
||||
}
|
||||
|
||||
yield* fileSystem.makeDirectory(input.targetDir, { recursive: true });
|
||||
// Attachments are decoration on a dev fixture: one unreadable file should
|
||||
// leave the rest of the seed intact rather than fail a completed copy.
|
||||
const copied = yield* Effect.forEach(matches, (entry) =>
|
||||
fileSystem.copyFile(path.join(input.sourceDir, entry), path.join(input.targetDir, entry)).pipe(
|
||||
Effect.as(1),
|
||||
Effect.orElseSucceed(() => 0),
|
||||
// leave the rest of the seed intact rather than fail a completed database
|
||||
// copy. The directory itself is subject to the same rule.
|
||||
return yield* fileSystem.makeDirectory(input.targetDir, { recursive: true }).pipe(
|
||||
Effect.andThen(
|
||||
Effect.forEach(matches, (entry) =>
|
||||
fileSystem
|
||||
.copyFile(path.join(input.sourceDir, entry), path.join(input.targetDir, entry))
|
||||
.pipe(
|
||||
Effect.as(1),
|
||||
Effect.orElseSucceed(() => 0),
|
||||
),
|
||||
),
|
||||
),
|
||||
Effect.map((copied) => copied.reduce((total, one) => total + one, 0)),
|
||||
Effect.orElseSucceed(() => 0),
|
||||
);
|
||||
return copied.reduce((total, one) => total + one, 0);
|
||||
});
|
||||
|
||||
const devSeedCli = Command.make("dev-seed", {
|
||||
|
||||
@@ -324,6 +324,11 @@ describe("seedDevDatabase", () => {
|
||||
assert.equal(session.status, "stopped");
|
||||
assert.isNull(session.active_turn_id);
|
||||
}
|
||||
const [threadsWithLatestTurn] = query<{ count: number }>(
|
||||
target,
|
||||
"SELECT COUNT(*) count FROM projection_threads WHERE latest_turn_id IS NOT NULL",
|
||||
);
|
||||
assert.equal(threadsWithLatestTurn?.count, 0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -437,6 +442,35 @@ describe("seedDevDatabase", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("uses activity id as the final cap tiebreaker", () => {
|
||||
withDatabases(({ source, target }) => {
|
||||
const sourceDatabase = new NodeSqlite.DatabaseSync(source);
|
||||
sourceDatabase.exec("DELETE FROM projection_thread_activities");
|
||||
const insert = sourceDatabase.prepare(
|
||||
`INSERT INTO projection_thread_activities
|
||||
VALUES (?, 't4', 'neutral', 'tool', 'ran', '{}', 7, ?)`,
|
||||
);
|
||||
const tied = "2026-07-10T00:00:00.000Z";
|
||||
insert.run("tie-a", tied);
|
||||
insert.run("tie-z", tied);
|
||||
sourceDatabase.close();
|
||||
|
||||
seedDevDatabase({
|
||||
sourceDbPath: source,
|
||||
targetDbPath: target,
|
||||
threadLimit: 1,
|
||||
activityLimit: 1,
|
||||
seededAt: SEEDED_AT,
|
||||
});
|
||||
|
||||
const kept = query<{ activity_id: string }>(
|
||||
target,
|
||||
"SELECT activity_id FROM projection_thread_activities",
|
||||
);
|
||||
assert.deepStrictEqual(kept, [{ activity_id: "tie-z" }]);
|
||||
});
|
||||
});
|
||||
|
||||
// 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.
|
||||
@@ -650,6 +684,35 @@ describe("seedDevDatabase", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("falls back to user messages when stored recency is missing", () => {
|
||||
withDatabases(({ source, target }) => {
|
||||
const sourceDatabase = new NodeSqlite.DatabaseSync(source);
|
||||
sourceDatabase.exec("UPDATE projection_threads SET latest_user_message_at = NULL");
|
||||
sourceDatabase
|
||||
.prepare(
|
||||
`UPDATE projection_thread_messages
|
||||
SET created_at = '2099-01-01T00:00:00.000Z'
|
||||
WHERE thread_id = 't0' AND role = 'user'`,
|
||||
)
|
||||
.run();
|
||||
sourceDatabase.close();
|
||||
|
||||
seedDevDatabase({
|
||||
sourceDbPath: source,
|
||||
targetDbPath: target,
|
||||
threadLimit: 1,
|
||||
activityLimit: 10,
|
||||
seededAt: SEEDED_AT,
|
||||
});
|
||||
|
||||
const threads = query<{ thread_id: string }>(
|
||||
target,
|
||||
"SELECT thread_id FROM projection_threads",
|
||||
);
|
||||
assert.deepStrictEqual(threads, [{ thread_id: "t0" }]);
|
||||
});
|
||||
});
|
||||
|
||||
// Every statement keyed on thread ids must cost exactly one bound variable
|
||||
// per thread: the limit *is* SQLite's ceiling, so a single extra binding
|
||||
// makes the maximum fail — after the target has already been emptied. Runs
|
||||
|
||||
+48
-19
@@ -411,31 +411,51 @@ export function seedDevDatabase(options: DevSeedOptions): DevSeedSummary {
|
||||
}
|
||||
|
||||
try {
|
||||
// Threads the user actually touched most recently. Mirrors the sidebar's own
|
||||
// ordering (packages/client-runtime/src/state/threadSort.ts). Both recency
|
||||
// columns arrived in later migrations, so an older source database is read
|
||||
// with whichever of them it actually has — the rest of the copy tolerates
|
||||
// schema drift, and this query must too.
|
||||
const recencyColumns = ["latest_user_message_at", "updated_at", "created_at"].filter((column) =>
|
||||
hasColumn(source, "projection_threads", column),
|
||||
);
|
||||
// Threads the user actually touched most recently. Mirrors the sidebar's
|
||||
// own ordering (packages/client-runtime/src/state/threadSort.ts), including
|
||||
// its user-message scan when the materialized timestamp is null. Both
|
||||
// recency columns arrived in later migrations, so an older source database
|
||||
// is read with whichever inputs it actually has.
|
||||
const recencyExpressions: Array<string> = [];
|
||||
if (hasColumn(source, "projection_threads", "latest_user_message_at")) {
|
||||
recencyExpressions.push('threads."latest_user_message_at"');
|
||||
}
|
||||
if (
|
||||
hasTable(source, "projection_thread_messages") &&
|
||||
["thread_id", "role", "created_at"].every((column) =>
|
||||
hasColumn(source, "projection_thread_messages", column),
|
||||
)
|
||||
) {
|
||||
recencyExpressions.push(`(
|
||||
SELECT MAX(messages."created_at")
|
||||
FROM projection_thread_messages AS messages
|
||||
WHERE messages."thread_id" = threads."thread_id"
|
||||
AND messages."role" = 'user'
|
||||
)`);
|
||||
}
|
||||
for (const column of ["updated_at", "created_at"]) {
|
||||
if (hasColumn(source, "projection_threads", column)) {
|
||||
recencyExpressions.push(`threads."${column}"`);
|
||||
}
|
||||
}
|
||||
// SQLite's COALESCE requires at least two arguments, so a source down to a
|
||||
// single recency column — the pre-migration-017 case this filter exists to
|
||||
// support — would throw rather than degrade. `created_at` is NOT NULL from
|
||||
// migration 005 on, so there is always at least one.
|
||||
const recencyOrder =
|
||||
recencyColumns.length > 1
|
||||
? `COALESCE(${recencyColumns.join(", ")})`
|
||||
: (recencyColumns[0] ?? "rowid");
|
||||
recencyExpressions.length > 1
|
||||
? `COALESCE(${recencyExpressions.join(", ")})`
|
||||
: (recencyExpressions[0] ?? "threads.rowid");
|
||||
const activeFilters = ["deleted_at", "archived_at"]
|
||||
.filter((column) => hasColumn(source, "projection_threads", column))
|
||||
.map((column) => `${column} IS NULL`);
|
||||
.map((column) => `threads."${column}" IS NULL`);
|
||||
const threadIds = (
|
||||
source
|
||||
.prepare(
|
||||
`SELECT thread_id FROM projection_threads
|
||||
`SELECT threads."thread_id" AS thread_id
|
||||
FROM projection_threads AS threads
|
||||
${activeFilters.length > 0 ? `WHERE ${activeFilters.join(" AND ")}` : ""}
|
||||
ORDER BY ${recencyOrder} DESC
|
||||
ORDER BY ${recencyOrder} DESC, threads."thread_id" DESC
|
||||
LIMIT ?`,
|
||||
)
|
||||
.all(options.threadLimit) as Array<{ thread_id: string }>
|
||||
@@ -512,7 +532,13 @@ export function seedDevDatabase(options: DevSeedOptions): DevSeedSummary {
|
||||
keyColumn: "thread_id",
|
||||
keys: threadIds,
|
||||
// Approvals are not copied (see below), so the badge must not claim any.
|
||||
overrides: { pending_approval_count: 0, pending_user_input_count: 0 },
|
||||
// Sessions are copied as stopped with no active turn, matching the
|
||||
// thread.session-set projection by clearing its latest turn pointer.
|
||||
overrides: {
|
||||
latest_turn_id: null,
|
||||
pending_approval_count: 0,
|
||||
pending_user_input_count: 0,
|
||||
},
|
||||
}),
|
||||
);
|
||||
// row_id is an AUTOINCREMENT surrogate; let the target assign its own.
|
||||
@@ -553,8 +579,8 @@ export function seedDevDatabase(options: DevSeedOptions): DevSeedSummary {
|
||||
table: "projection_thread_activities",
|
||||
keyColumn: "thread_id",
|
||||
keys: threadIds,
|
||||
// Ordered the way the app reads these back — `sequence` first, then
|
||||
// `created_at` (ProjectionSnapshotQuery). Any other key order can keep
|
||||
// Ordered the way the app reads these back — `sequence`, `created_at`,
|
||||
// then `activity_id` (ProjectionSnapshotQuery). Any other key order can keep
|
||||
// rows the timeline treats as oldest: timestamps tie within a burst of
|
||||
// tool activity, and can even disagree with `sequence` outright when
|
||||
// events arrive with skewed clocks. Descending, a NULL `sequence`
|
||||
@@ -564,8 +590,8 @@ export function seedDevDatabase(options: DevSeedOptions): DevSeedSummary {
|
||||
// back to the timestamp — same drift tolerance as the rest of the copy.
|
||||
perKeyLimit: {
|
||||
orderBy: hasColumn(source, "projection_thread_activities", "sequence")
|
||||
? `"sequence" DESC, "created_at"`
|
||||
: `"created_at"`,
|
||||
? `"sequence" DESC, "created_at" DESC, "activity_id"`
|
||||
: `"created_at" DESC, "activity_id"`,
|
||||
limit: options.activityLimit,
|
||||
},
|
||||
}),
|
||||
@@ -580,6 +606,9 @@ export function seedDevDatabase(options: DevSeedOptions): DevSeedSummary {
|
||||
// No agent process is attached in the copy. A carried-over "running"
|
||||
// status with an active turn renders a thread that spins forever, and
|
||||
// ProviderSessionReaper skips reaping anything with an active turn.
|
||||
// "ready"/"idle" are live-session completion signals to agent
|
||||
// awareness, so preserving those would publish historical fixtures as
|
||||
// newly completed work. Every copied session is intentionally stopped.
|
||||
overrides: { status: "stopped", active_turn_id: null, last_error: null },
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user