Skip to content

Commit cea58c4

Browse files
ashx-jclaude
andcommitted
fix(server): nightly starts on databases a preview build migrated
v0.1.2 previews recorded peer links as migrations 68-70, the ids main later used for ShellStopRequestIndex and ThreadMessageSearch, so newer builds refused those databases at startup. Forget that preview history when no peer links are stored so main's migrations run. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f0dd86f commit cea58c4

3 files changed

Lines changed: 124 additions & 1 deletion

File tree

‎apps/server/src/persistence/Migrations.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import * as Migrator from "effect/sql/Migrator";
22
import * as Effect from "effect/Effect";
33
import * as SqlClient from "effect/sql/SqlClient";
4-
import { reconcileV2PreviewMigration } from "./reconcileV2PreviewMigration.ts";
4+
import {
5+
reconcilePeerLinksPreviewMigrations,
6+
reconcileV2PreviewMigration,
7+
} from "./reconcileV2PreviewMigration.ts";
58

69
import Migration0001 from "./Migrations/001_OrchestrationEvents.ts";
710
import Migration0002 from "./Migrations/002_OrchestrationCommandReceipts.ts";
@@ -205,6 +208,7 @@ export const runMigrations = Effect.fn("runMigrations")(function* ({
205208
toMigrationInclusive === undefined || toMigrationInclusive >= FIRST_V2_MIGRATION_ID;
206209
const previewMigrations = includesV2 ? yield* reconcileV2PreviewMigration() : [];
207210
if (includesV2) {
211+
yield* reconcilePeerLinksPreviewMigrations();
208212
yield* assertV2MigrationsCanRun(toMigrationInclusive);
209213
}
210214
const executedMigrations = [

‎apps/server/src/persistence/reconcileV2PreviewMigration.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,76 @@ describe("V2 preview upgrade", () => {
158158
);
159159
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
160160
);
161+
162+
// Mirrors v0.1.2-preview.20261022.3511's 068-070; main never shipped these files.
163+
const seedPeerLinksPreview = Effect.gen(function* () {
164+
const sql = yield* SqlClient.SqlClient;
165+
yield* runMigrations({ toMigrationInclusive: 67 });
166+
yield* Migrator.make({})({
167+
loader: Migrator.fromRecord({
168+
"68_PeerLinks": sql`
169+
CREATE TABLE peer_environment_links (
170+
environment_id TEXT PRIMARY KEY, secret_name TEXT NOT NULL, label TEXT NOT NULL,
171+
urls_json TEXT NOT NULL, access TEXT NOT NULL, linked_at TEXT NOT NULL,
172+
expires_at TEXT NOT NULL, last_reached_at TEXT, last_error TEXT
173+
)
174+
`,
175+
"69_PeerLinkFence": sql`
176+
ALTER TABLE peer_environment_links ADD COLUMN fenced INTEGER NOT NULL DEFAULT 0
177+
`,
178+
"70_WaitingLinkRequestIndex": sql`
179+
CREATE INDEX orchestration_v2_projection_turn_items_waiting_link_idx
180+
ON orchestration_v2_projection_turn_items(thread_id, updated_at DESC, turn_item_id DESC)
181+
WHERE type = 'link_request' AND status = 'waiting'
182+
`,
183+
}),
184+
});
185+
});
186+
187+
it.effect("upgrades a peer links preview without stored links", () =>
188+
Effect.gen(function* () {
189+
const sql = yield* SqlClient.SqlClient;
190+
yield* seedPeerLinksPreview;
191+
assert.deepStrictEqual(yield* runMigrations(), [
192+
[68, "ShellStopRequestIndex"],
193+
[69, "ThreadMessageSearch"],
194+
]);
195+
assert.deepStrictEqual(yield* runMigrations(), []);
196+
const history = yield* sql<{ readonly migration_id: number; readonly name: string }>`
197+
SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id
198+
`;
199+
assert.deepStrictEqual(
200+
history.map((row) => [row.migration_id, row.name] as const),
201+
migrationManifest,
202+
);
203+
assert.deepStrictEqual(
204+
yield* sql`
205+
SELECT name FROM sqlite_master WHERE name IN (
206+
'peer_environment_links', 'orchestration_v2_projection_turn_items_waiting_link_idx'
207+
)
208+
`,
209+
[],
210+
);
211+
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
212+
);
213+
214+
it.effect("refuses a peer links preview with stored links without modifying it", () =>
215+
Effect.gen(function* () {
216+
const sql = yield* SqlClient.SqlClient;
217+
yield* seedPeerLinksPreview;
218+
yield* sql`
219+
INSERT INTO peer_environment_links
220+
(environment_id, secret_name, label, urls_json, access, linked_at, expires_at, fenced)
221+
VALUES ('peer', 'peer-secret', 'Laptop', '[]', 'full', '2026-10-22', '2026-11-22', 1)
222+
`;
223+
const history = yield* sql`SELECT * FROM effect_sql_migrations ORDER BY migration_id`;
224+
const links = yield* sql`SELECT * FROM peer_environment_links`;
225+
assert.ok(Exit.isFailure(yield* Effect.exit(runMigrations())));
226+
assert.deepStrictEqual(
227+
yield* sql`SELECT * FROM effect_sql_migrations ORDER BY migration_id`,
228+
history,
229+
);
230+
assert.deepStrictEqual(yield* sql`SELECT * FROM peer_environment_links`, links);
231+
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
232+
);
161233
});

‎apps/server/src/persistence/reconcileV2PreviewMigration.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,50 @@ export const reconcileV2PreviewMigration = Effect.fn("reconcileV2PreviewMigratio
5353
}),
5454
);
5555
});
56+
57+
const PEER_LINKS_PREVIEW_HISTORY = [
58+
[68, "PeerLinks"],
59+
[69, "PeerLinkFence"],
60+
[70, "WaitingLinkRequestIndex"],
61+
] as const;
62+
63+
// v0.1.2 previews recorded peer links as 68-70 before main took 68/69. Forget that history so
64+
// main's 68+ run and later peer link migrations can recreate the schema. Stored links block this
65+
// so they are never dropped silently; unlinking them in the preview build clears the way.
66+
export const reconcilePeerLinksPreviewMigrations = Effect.fn("reconcilePeerLinksPreviewMigrations")(
67+
function* () {
68+
const sql = yield* SqlClient.SqlClient;
69+
return yield* sql.withTransaction(
70+
Effect.gen(function* () {
71+
const tables = yield* sql`
72+
SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'effect_sql_migrations'
73+
`;
74+
if (tables.length === 0) return;
75+
const history = yield* sql<{ readonly migration_id: number; readonly name: string }>`
76+
SELECT migration_id, name FROM effect_sql_migrations
77+
WHERE migration_id >= 68 ORDER BY migration_id
78+
`;
79+
const isPreviewHistory =
80+
history.length === PEER_LINKS_PREVIEW_HISTORY.length &&
81+
history.every(
82+
(row, index) =>
83+
row.migration_id === PEER_LINKS_PREVIEW_HISTORY[index]?.[0] &&
84+
row.name === PEER_LINKS_PREVIEW_HISTORY[index]?.[1],
85+
);
86+
if (!isPreviewHistory) return;
87+
const links = yield* sql`SELECT 1 FROM peer_environment_links LIMIT 1`;
88+
if (links.length > 0) {
89+
return yield* new Migrator.MigrationError({
90+
kind: "BadState",
91+
message:
92+
"This database was migrated by a v0.1.2 preview build and still has peer environment links, which this build cannot keep. Start the preview build, unlink every linked environment, then start this build again.",
93+
});
94+
}
95+
yield* sql`DROP INDEX IF EXISTS orchestration_v2_projection_turn_items_waiting_link_idx`;
96+
yield* sql`DROP TABLE peer_environment_links`;
97+
yield* sql`DELETE FROM effect_sql_migrations WHERE migration_id >= 68`;
98+
yield* Effect.log("Removed empty peer links schema left by a preview build");
99+
}),
100+
);
101+
},
102+
);

0 commit comments

Comments
 (0)