@vellumai/assistant 0.8.12-dev.202606161836.2f5810e → 0.8.12-dev.202606162008.e73dbc9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node_modules/@vellumai/gateway-client/src/index.ts +17 -6
- package/node_modules/@vellumai/gateway-client/src/outbound-contract.ts +119 -0
- package/node_modules/@vellumai/gateway-client/src/types.ts +15 -84
- package/package.json +1 -1
- package/src/__tests__/config-loader-backfill.test.ts +18 -0
- package/src/config/loader.ts +4 -0
- package/src/config/schemas/platform.ts +0 -8
- package/src/daemon/orphan-reaper.test.ts +0 -19
- package/src/daemon/orphan-reaper.ts +2 -24
- package/src/memory/db-init.ts +5 -17
- package/src/memory/migrations/__tests__/run-migrations.test.ts +52 -0
- package/src/memory/migrations/run-migrations.ts +41 -0
|
@@ -19,20 +19,26 @@ export * from "./gateway-ipc-contracts.js";
|
|
|
19
19
|
|
|
20
20
|
export { ipcCall, IpcCallError, PersistentIpcClient } from "./ipc-client.js";
|
|
21
21
|
|
|
22
|
+
// Outbound delivery contract (daemon → gateway) — Zod schemas + derived types
|
|
23
|
+
export {
|
|
24
|
+
ApprovalActionOptionSchema,
|
|
25
|
+
ApprovalUIMetadataSchema,
|
|
26
|
+
AttachmentMetadataSchema,
|
|
27
|
+
ChannelDeliveryResultSchema,
|
|
28
|
+
ChannelReplyPayloadSchema,
|
|
29
|
+
PermissionRequestDetailsSchema,
|
|
30
|
+
} from "./outbound-contract.js";
|
|
31
|
+
|
|
22
32
|
export type {
|
|
23
33
|
ApprovalActionOption,
|
|
24
34
|
ApprovalUIMetadata,
|
|
25
35
|
AttachmentMetadata,
|
|
26
36
|
ChannelDeliveryResult,
|
|
27
37
|
ChannelReplyPayload,
|
|
28
|
-
IpcRequest,
|
|
29
|
-
IpcResponse,
|
|
30
|
-
Logger,
|
|
31
38
|
PermissionRequestDetails,
|
|
32
|
-
} from "./
|
|
33
|
-
|
|
34
|
-
export { noopLogger } from "./types.js";
|
|
39
|
+
} from "./outbound-contract.js";
|
|
35
40
|
|
|
41
|
+
// Inbound contract (gateway → daemon) — Zod schemas + derived types
|
|
36
42
|
export {
|
|
37
43
|
CommandIntentSchema,
|
|
38
44
|
RuntimeInboundPayloadSchema,
|
|
@@ -44,3 +50,8 @@ export type {
|
|
|
44
50
|
RuntimeInboundPayload,
|
|
45
51
|
SourceMetadata,
|
|
46
52
|
} from "./inbound-contract.js";
|
|
53
|
+
|
|
54
|
+
// IPC, logger, and utility types
|
|
55
|
+
export type { IpcRequest, IpcResponse, Logger } from "./types.js";
|
|
56
|
+
|
|
57
|
+
export { noopLogger } from "./types.js";
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Daemon → gateway outbound delivery contract.
|
|
3
|
+
*
|
|
4
|
+
* Zod schemas defining the wire format for channel replies delivered from
|
|
5
|
+
* the daemon to the gateway via `POST /deliver/{channel}`. Both services
|
|
6
|
+
* import from here so the contract is enforced at compile time.
|
|
7
|
+
*
|
|
8
|
+
* The daemon constructs these payloads in `deliverChannelReply()` and
|
|
9
|
+
* `deliverApprovalPrompt()`; the gateway validates and dispatches them
|
|
10
|
+
* to the target channel provider.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
// Attachment metadata
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
|
|
19
|
+
export const AttachmentMetadataSchema = z.object({
|
|
20
|
+
id: z.string(),
|
|
21
|
+
filename: z.string(),
|
|
22
|
+
mimeType: z.string(),
|
|
23
|
+
sizeBytes: z.number(),
|
|
24
|
+
kind: z.string(),
|
|
25
|
+
data: z.string().optional(),
|
|
26
|
+
thumbnailData: z.string().optional(),
|
|
27
|
+
fileBacked: z.boolean().optional(),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
export type AttachmentMetadata = z.infer<typeof AttachmentMetadataSchema>;
|
|
31
|
+
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// Approval UI types
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
export const ApprovalActionOptionSchema = z.object({
|
|
37
|
+
id: z.string(),
|
|
38
|
+
label: z.string(),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export type ApprovalActionOption = z.infer<typeof ApprovalActionOptionSchema>;
|
|
42
|
+
|
|
43
|
+
export const PermissionRequestDetailsSchema = z.object({
|
|
44
|
+
toolName: z.string(),
|
|
45
|
+
riskLevel: z.string(),
|
|
46
|
+
toolInput: z.record(z.string(), z.unknown()),
|
|
47
|
+
requesterIdentifier: z.string().optional(),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
export type PermissionRequestDetails = z.infer<
|
|
51
|
+
typeof PermissionRequestDetailsSchema
|
|
52
|
+
>;
|
|
53
|
+
|
|
54
|
+
export const ApprovalUIMetadataSchema = z.object({
|
|
55
|
+
requestId: z.string(),
|
|
56
|
+
actions: z.array(ApprovalActionOptionSchema),
|
|
57
|
+
plainTextFallback: z.string(),
|
|
58
|
+
permissionDetails: PermissionRequestDetailsSchema.optional(),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
export type ApprovalUIMetadata = z.infer<typeof ApprovalUIMetadataSchema>;
|
|
62
|
+
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
// Channel reply payload — the full outbound wire format
|
|
65
|
+
// ---------------------------------------------------------------------------
|
|
66
|
+
|
|
67
|
+
export const ChannelReplyPayloadSchema = z.object({
|
|
68
|
+
chatId: z.string(),
|
|
69
|
+
text: z.string().optional(),
|
|
70
|
+
/** Pre-formatted Block Kit blocks for Slack delivery. */
|
|
71
|
+
blocks: z.array(z.unknown()).optional(),
|
|
72
|
+
assistantId: z.string().optional(),
|
|
73
|
+
attachments: z.array(AttachmentMetadataSchema).optional(),
|
|
74
|
+
approval: ApprovalUIMetadataSchema.optional(),
|
|
75
|
+
chatAction: z.literal("typing").optional(),
|
|
76
|
+
/**
|
|
77
|
+
* When true, deliver via `chat.postEphemeral` so only the target `user`
|
|
78
|
+
* sees the message.
|
|
79
|
+
*/
|
|
80
|
+
ephemeral: z.boolean().optional(),
|
|
81
|
+
/** Slack user ID — required when `ephemeral` is true. */
|
|
82
|
+
user: z.string().optional(),
|
|
83
|
+
/** When provided, update an existing message instead of posting a new one. */
|
|
84
|
+
messageTs: z.string().optional(),
|
|
85
|
+
/** When true, auto-generate Block Kit blocks from text via textToBlocks(). */
|
|
86
|
+
useBlocks: z.boolean().optional(),
|
|
87
|
+
/** When provided, add or remove an emoji reaction on a message. */
|
|
88
|
+
reaction: z
|
|
89
|
+
.object({
|
|
90
|
+
action: z.enum(["add", "remove"]),
|
|
91
|
+
name: z.string(),
|
|
92
|
+
messageTs: z.string(),
|
|
93
|
+
})
|
|
94
|
+
.optional(),
|
|
95
|
+
/** When provided, set or clear the Slack Assistants API thread status. */
|
|
96
|
+
assistantThreadStatus: z
|
|
97
|
+
.object({
|
|
98
|
+
channel: z.string(),
|
|
99
|
+
threadTs: z.string(),
|
|
100
|
+
status: z.string(),
|
|
101
|
+
/** Serialized to Slack as `loading_messages`. */
|
|
102
|
+
loadingMessages: z.array(z.string()).optional(),
|
|
103
|
+
})
|
|
104
|
+
.optional(),
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
export type ChannelReplyPayload = z.infer<typeof ChannelReplyPayloadSchema>;
|
|
108
|
+
|
|
109
|
+
// ---------------------------------------------------------------------------
|
|
110
|
+
// Channel delivery result — gateway response
|
|
111
|
+
// ---------------------------------------------------------------------------
|
|
112
|
+
|
|
113
|
+
export const ChannelDeliveryResultSchema = z.object({
|
|
114
|
+
ok: z.boolean(),
|
|
115
|
+
/** The message timestamp returned by the delivery endpoint. */
|
|
116
|
+
ts: z.string().optional(),
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
export type ChannelDeliveryResult = z.infer<typeof ChannelDeliveryResultSchema>;
|
|
@@ -4,92 +4,23 @@
|
|
|
4
4
|
* Type definitions for assistant-to-gateway communication. These are
|
|
5
5
|
* intentionally decoupled from the assistant's internal types so the
|
|
6
6
|
* package can be consumed without importing assistant internals.
|
|
7
|
+
*
|
|
8
|
+
* HTTP delivery types (ChannelReplyPayload, ApprovalUIMetadata, etc.)
|
|
9
|
+
* are defined as Zod schemas in `outbound-contract.ts` and re-exported
|
|
10
|
+
* from the barrel `index.ts`. This file retains only IPC and utility
|
|
11
|
+
* types that don't cross an HTTP wire boundary.
|
|
7
12
|
*/
|
|
8
13
|
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
kind: string;
|
|
20
|
-
data?: string;
|
|
21
|
-
thumbnailData?: string;
|
|
22
|
-
fileBacked?: boolean;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** An action option presented to the user in an approval prompt. */
|
|
26
|
-
export interface ApprovalActionOption {
|
|
27
|
-
id: string;
|
|
28
|
-
label: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Tool-permission-specific details carried alongside the approval payload.
|
|
33
|
-
* Channels that support rich UI (e.g. Slack Block Kit) use these fields
|
|
34
|
-
* to render a detailed permission request card.
|
|
35
|
-
*/
|
|
36
|
-
export interface PermissionRequestDetails {
|
|
37
|
-
toolName: string;
|
|
38
|
-
riskLevel: string;
|
|
39
|
-
toolInput: Record<string, unknown>;
|
|
40
|
-
requesterIdentifier?: string;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Metadata attached to gateway callback payloads for rendering approval
|
|
45
|
-
* UI and routing decisions back to the correct pending interaction.
|
|
46
|
-
*/
|
|
47
|
-
export interface ApprovalUIMetadata {
|
|
48
|
-
requestId: string;
|
|
49
|
-
actions: ApprovalActionOption[];
|
|
50
|
-
plainTextFallback: string;
|
|
51
|
-
permissionDetails?: PermissionRequestDetails;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** Payload for a channel reply delivered via the gateway. */
|
|
55
|
-
export interface ChannelReplyPayload {
|
|
56
|
-
chatId: string;
|
|
57
|
-
text?: string;
|
|
58
|
-
/** Pre-formatted Block Kit blocks for Slack delivery. */
|
|
59
|
-
blocks?: unknown[];
|
|
60
|
-
assistantId?: string;
|
|
61
|
-
attachments?: AttachmentMetadata[];
|
|
62
|
-
approval?: ApprovalUIMetadata;
|
|
63
|
-
chatAction?: "typing";
|
|
64
|
-
/**
|
|
65
|
-
* When true, deliver via `chat.postEphemeral` so only the target `user`
|
|
66
|
-
* sees the message.
|
|
67
|
-
*/
|
|
68
|
-
ephemeral?: boolean;
|
|
69
|
-
/** Slack user ID — required when `ephemeral` is true. */
|
|
70
|
-
user?: string;
|
|
71
|
-
/** When provided, update an existing message instead of posting a new one. */
|
|
72
|
-
messageTs?: string;
|
|
73
|
-
/** When true, auto-generate Block Kit blocks from text via textToBlocks(). */
|
|
74
|
-
useBlocks?: boolean;
|
|
75
|
-
/** When provided, add or remove an emoji reaction on a message. */
|
|
76
|
-
reaction?: { action: "add" | "remove"; name: string; messageTs: string };
|
|
77
|
-
/** When provided, set or clear the Slack Assistants API thread status. */
|
|
78
|
-
assistantThreadStatus?: {
|
|
79
|
-
channel: string;
|
|
80
|
-
threadTs: string;
|
|
81
|
-
status: string;
|
|
82
|
-
/** Serialized to Slack as `loading_messages`. */
|
|
83
|
-
loadingMessages?: string[];
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** Result from a channel delivery attempt. */
|
|
88
|
-
export interface ChannelDeliveryResult {
|
|
89
|
-
ok: boolean;
|
|
90
|
-
/** The message timestamp returned by the delivery endpoint. */
|
|
91
|
-
ts?: string;
|
|
92
|
-
}
|
|
14
|
+
// Re-export outbound delivery types for backward compatibility — consumers
|
|
15
|
+
// that import from "./types.js" continue to work.
|
|
16
|
+
export type {
|
|
17
|
+
ApprovalActionOption,
|
|
18
|
+
ApprovalUIMetadata,
|
|
19
|
+
AttachmentMetadata,
|
|
20
|
+
ChannelDeliveryResult,
|
|
21
|
+
ChannelReplyPayload,
|
|
22
|
+
PermissionRequestDetails,
|
|
23
|
+
} from "./outbound-contract.js";
|
|
93
24
|
|
|
94
25
|
// ---------------------------------------------------------------------------
|
|
95
26
|
// IPC types
|
package/package.json
CHANGED
|
@@ -417,6 +417,24 @@ describe("loadConfig startup behavior", () => {
|
|
|
417
417
|
expect(raw.memory?.jobs?.workerConcurrency).toBe(4);
|
|
418
418
|
});
|
|
419
419
|
|
|
420
|
+
test("strips daemon.reapOrphanedSubprocesses from existing user configs", () => {
|
|
421
|
+
// `daemon.reapOrphanedSubprocesses` is a deprecated opt-in flag: the
|
|
422
|
+
// orphan-subprocess reaper runs by default whenever the daemon is PID 1 on
|
|
423
|
+
// Linux. Existing configs that have it written to disk should load cleanly
|
|
424
|
+
// with the field silently stripped.
|
|
425
|
+
writeConfig({
|
|
426
|
+
provider: "anthropic",
|
|
427
|
+
daemon: { reapOrphanedSubprocesses: true, standaloneRecording: false },
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
loadConfig();
|
|
431
|
+
|
|
432
|
+
const raw = JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
|
|
433
|
+
expect(raw.daemon?.reapOrphanedSubprocesses).toBeUndefined();
|
|
434
|
+
// Sibling fields under daemon are preserved
|
|
435
|
+
expect(raw.daemon?.standaloneRecording).toBe(false);
|
|
436
|
+
});
|
|
437
|
+
|
|
420
438
|
test("still writes a default config on first launch when file is absent", () => {
|
|
421
439
|
// Discoverability: when no config.json exists, write one populated with
|
|
422
440
|
// all schema defaults so users can see and edit available options.
|
package/src/config/loader.ts
CHANGED
|
@@ -362,6 +362,10 @@ const DEPRECATED_FIELDS: Record<string, string> = {
|
|
|
362
362
|
"per-lane concurrency caps (slowLlmConcurrency, fastConcurrency, " +
|
|
363
363
|
"embedConcurrency) instead of a single batch size. " +
|
|
364
364
|
"The field will be removed from your config file.",
|
|
365
|
+
"daemon.reapOrphanedSubprocesses":
|
|
366
|
+
"daemon.reapOrphanedSubprocesses has been removed. The daemon now reaps " +
|
|
367
|
+
"orphaned subprocesses automatically whenever it runs as PID 1 on Linux. " +
|
|
368
|
+
"The field will be removed from your config file.",
|
|
365
369
|
};
|
|
366
370
|
|
|
367
371
|
/**
|
|
@@ -87,14 +87,6 @@ export const DaemonConfigSchema = z
|
|
|
87
87
|
.describe(
|
|
88
88
|
"Whether the daemon records conversations even when no client is connected",
|
|
89
89
|
),
|
|
90
|
-
reapOrphanedSubprocesses: z
|
|
91
|
-
.boolean({
|
|
92
|
-
error: "daemon.reapOrphanedSubprocesses must be a boolean",
|
|
93
|
-
})
|
|
94
|
-
.default(false)
|
|
95
|
-
.describe(
|
|
96
|
-
"Whether the daemon, when running as PID 1 in a container, periodically reaps orphaned subprocesses that reparented to it. Off by default while the behavior is being validated.",
|
|
97
|
-
),
|
|
98
90
|
})
|
|
99
91
|
.describe("Background daemon process configuration");
|
|
100
92
|
|
|
@@ -21,7 +21,6 @@ mock.module("../util/logger.js", () => ({
|
|
|
21
21
|
}));
|
|
22
22
|
|
|
23
23
|
const { parseProcStat, selectReapable } = await import("./orphan-reaper.js");
|
|
24
|
-
const { DaemonConfigSchema } = await import("../config/schemas/platform.js");
|
|
25
24
|
|
|
26
25
|
describe("parseProcStat", () => {
|
|
27
26
|
test("parses a normal stat line", () => {
|
|
@@ -89,24 +88,6 @@ describe("selectReapable", () => {
|
|
|
89
88
|
});
|
|
90
89
|
});
|
|
91
90
|
|
|
92
|
-
describe("daemon.reapOrphanedSubprocesses gate", () => {
|
|
93
|
-
test("defaults to off so the reaper is opt-in", () => {
|
|
94
|
-
// GIVEN a daemon config with the reaper flag unspecified
|
|
95
|
-
// WHEN it is parsed with schema defaults
|
|
96
|
-
const parsed = DaemonConfigSchema.parse({});
|
|
97
|
-
// THEN the reaper is disabled unless explicitly turned on
|
|
98
|
-
expect(parsed.reapOrphanedSubprocesses).toBe(false);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
test("honors an explicit opt-in", () => {
|
|
102
|
-
// GIVEN a daemon config that explicitly enables the reaper
|
|
103
|
-
// WHEN it is parsed
|
|
104
|
-
const parsed = DaemonConfigSchema.parse({ reapOrphanedSubprocesses: true });
|
|
105
|
-
// THEN the flag is respected
|
|
106
|
-
expect(parsed.reapOrphanedSubprocesses).toBe(true);
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
|
|
110
91
|
// ── Integration: real reparented orphan on Linux ────────────────────────────
|
|
111
92
|
const itLinux = process.platform === "linux" ? test : test.skip;
|
|
112
93
|
|
|
@@ -26,9 +26,7 @@
|
|
|
26
26
|
* macOS dev, or if an init such as tini is ever placed above the daemon),
|
|
27
27
|
* orphans reparent to that init and are reaped there, so there is nothing for
|
|
28
28
|
* this to do. Because the daemon is PID 1, orphans already reparent to it and
|
|
29
|
-
* `PR_SET_CHILD_SUBREAPER` is unnecessary.
|
|
30
|
-
* `daemon.reapOrphanedSubprocesses` config flag (default off) so the behavior
|
|
31
|
-
* can be enabled per workspace for validation before becoming the default.
|
|
29
|
+
* `PR_SET_CHILD_SUBREAPER` is unnecessary.
|
|
32
30
|
*
|
|
33
31
|
* References:
|
|
34
32
|
* - libuv reaps its own children by pid on SIGCHLD (`uv__wait_children`):
|
|
@@ -42,7 +40,6 @@
|
|
|
42
40
|
import { readdirSync, readFileSync } from "node:fs";
|
|
43
41
|
import { dlopen, FFIType, ptr } from "bun:ffi";
|
|
44
42
|
|
|
45
|
-
import { getConfigReadOnly } from "../config/loader.js";
|
|
46
43
|
import { getLogger } from "../util/logger.js";
|
|
47
44
|
|
|
48
45
|
const log = getLogger("orphan-reaper");
|
|
@@ -192,22 +189,9 @@ function reapScan(): void {
|
|
|
192
189
|
}
|
|
193
190
|
}
|
|
194
191
|
|
|
195
|
-
/**
|
|
196
|
-
* Read the opt-in gate from workspace config (`daemon.reapOrphanedSubprocesses`),
|
|
197
|
-
* tolerating a missing or malformed config so startup never fails on it.
|
|
198
|
-
*/
|
|
199
|
-
function isReaperEnabled(): boolean {
|
|
200
|
-
try {
|
|
201
|
-
return getConfigReadOnly().daemon.reapOrphanedSubprocesses;
|
|
202
|
-
} catch {
|
|
203
|
-
return false;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
192
|
/**
|
|
208
193
|
* Start the periodic orphan reaper. No-op unless the daemon is PID 1 on Linux
|
|
209
|
-
* (otherwise reparented orphans are reaped by the real init)
|
|
210
|
-
* `daemon.reapOrphanedSubprocesses` config gate is enabled.
|
|
194
|
+
* (otherwise reparented orphans are reaped by the real init).
|
|
211
195
|
*/
|
|
212
196
|
export function startOrphanReaper(): void {
|
|
213
197
|
if (scanTimer) return;
|
|
@@ -218,12 +202,6 @@ export function startOrphanReaper(): void {
|
|
|
218
202
|
);
|
|
219
203
|
return;
|
|
220
204
|
}
|
|
221
|
-
if (!isReaperEnabled()) {
|
|
222
|
-
log.info(
|
|
223
|
-
"Orphan reaper not started: daemon.reapOrphanedSubprocesses is disabled",
|
|
224
|
-
);
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
205
|
if (!initWaitpid()) return;
|
|
228
206
|
seenLastScan = new Set();
|
|
229
207
|
scanTimer = setInterval(reapScan, SCAN_INTERVAL_MS);
|
package/src/memory/db-init.ts
CHANGED
|
@@ -230,6 +230,7 @@ import {
|
|
|
230
230
|
runLateMigrations,
|
|
231
231
|
validateMigrationState,
|
|
232
232
|
} from "./migrations/index.js";
|
|
233
|
+
import { runMigrationSteps } from "./migrations/run-migrations.js";
|
|
233
234
|
|
|
234
235
|
// ---------------------------------------------------------------------------
|
|
235
236
|
// Test DB template — run migrations once, reuse across test files
|
|
@@ -524,25 +525,12 @@ export function initializeDb(): void {
|
|
|
524
525
|
|
|
525
526
|
// Run each migration step, catching and logging individual failures so one
|
|
526
527
|
// broken migration doesn't prevent independent later ones from succeeding.
|
|
527
|
-
const
|
|
528
|
-
for (const step of migrationSteps) {
|
|
529
|
-
try {
|
|
530
|
-
log.debug({ migration: step.name }, `Starting migration: ${step.name}`);
|
|
531
|
-
step(database);
|
|
532
|
-
log.debug({ migration: step.name }, `Migration succeeded: ${step.name}`);
|
|
533
|
-
} catch (err) {
|
|
534
|
-
failures.push(step.name);
|
|
535
|
-
log.error(
|
|
536
|
-
{ err, migration: step.name },
|
|
537
|
-
`Migration failed: ${step.name}`,
|
|
538
|
-
);
|
|
539
|
-
}
|
|
540
|
-
}
|
|
528
|
+
const { failed } = runMigrationSteps(database, migrationSteps);
|
|
541
529
|
|
|
542
|
-
if (
|
|
530
|
+
if (failed.length > 0) {
|
|
543
531
|
log.error(
|
|
544
|
-
{ failedMigrations:
|
|
545
|
-
`DB initialization completed with ${
|
|
532
|
+
{ failedMigrations: failed, count: failed.length },
|
|
533
|
+
`DB initialization completed with ${failed.length} failed migration(s)`,
|
|
546
534
|
);
|
|
547
535
|
}
|
|
548
536
|
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { describe, expect, test } from "bun:test";
|
|
3
|
+
|
|
4
|
+
import { drizzle } from "drizzle-orm/bun-sqlite";
|
|
5
|
+
|
|
6
|
+
import * as schema from "../../schema.js";
|
|
7
|
+
import { type MigrationStep, runMigrationSteps } from "../run-migrations.js";
|
|
8
|
+
|
|
9
|
+
function createTestDb() {
|
|
10
|
+
const sqlite = new Database(":memory:");
|
|
11
|
+
sqlite.exec("PRAGMA journal_mode=WAL");
|
|
12
|
+
sqlite.exec("PRAGMA foreign_keys = ON");
|
|
13
|
+
return drizzle(sqlite, { schema });
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe("runMigrationSteps — checkpointing", () => {
|
|
17
|
+
// Skipped: documents the post-checkpointing contract and fails against the
|
|
18
|
+
// current behavior, which re-runs every forward step on every boot. Promote
|
|
19
|
+
// to `test` when true checkpointing of initial migrations lands.
|
|
20
|
+
test.skip("does not re-execute a step that was already applied on a prior run", () => {
|
|
21
|
+
/**
|
|
22
|
+
* Booting an already-migrated database must not re-run forward migration
|
|
23
|
+
* steps. With true checkpointing each step's body executes at most once
|
|
24
|
+
* across boots; without it every step re-runs on every startup — the
|
|
25
|
+
* ~200-step re-probe that floors daemon startup at ~30s.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
// GIVEN three dummy migration steps that count how often their body runs
|
|
29
|
+
const calls = { a: 0, b: 0, c: 0 };
|
|
30
|
+
const steps: MigrationStep[] = [
|
|
31
|
+
function dummyMigrationA() {
|
|
32
|
+
calls.a++;
|
|
33
|
+
},
|
|
34
|
+
function dummyMigrationB() {
|
|
35
|
+
calls.b++;
|
|
36
|
+
},
|
|
37
|
+
function dummyMigrationC() {
|
|
38
|
+
calls.c++;
|
|
39
|
+
},
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
// AND a database that has already been migrated once
|
|
43
|
+
const db = createTestDb();
|
|
44
|
+
runMigrationSteps(db, steps);
|
|
45
|
+
|
|
46
|
+
// WHEN the same steps run again against the already-migrated database
|
|
47
|
+
runMigrationSteps(db, steps);
|
|
48
|
+
|
|
49
|
+
// THEN no step body executes a second time
|
|
50
|
+
expect(calls).toEqual({ a: 1, b: 1, c: 1 });
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { getLogger } from "../../util/logger.js";
|
|
2
|
+
import type { DrizzleDb } from "../db-connection.js";
|
|
3
|
+
|
|
4
|
+
const log = getLogger("db-init");
|
|
5
|
+
|
|
6
|
+
/** A single forward migration step, identified for logging by its `.name`. */
|
|
7
|
+
export type MigrationStep = (database: DrizzleDb) => void;
|
|
8
|
+
|
|
9
|
+
export interface MigrationRunResult {
|
|
10
|
+
/** Steps whose body threw. */
|
|
11
|
+
failed: string[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Run the ordered list of forward migration steps against the database.
|
|
16
|
+
*
|
|
17
|
+
* Individual step failures are caught and logged so one broken migration does
|
|
18
|
+
* not prevent independent later ones from succeeding.
|
|
19
|
+
*/
|
|
20
|
+
export function runMigrationSteps(
|
|
21
|
+
database: DrizzleDb,
|
|
22
|
+
steps: MigrationStep[],
|
|
23
|
+
): MigrationRunResult {
|
|
24
|
+
const failed: string[] = [];
|
|
25
|
+
|
|
26
|
+
for (const step of steps) {
|
|
27
|
+
try {
|
|
28
|
+
log.debug({ migration: step.name }, `Starting migration: ${step.name}`);
|
|
29
|
+
step(database);
|
|
30
|
+
log.debug({ migration: step.name }, `Migration succeeded: ${step.name}`);
|
|
31
|
+
} catch (err) {
|
|
32
|
+
failed.push(step.name);
|
|
33
|
+
log.error(
|
|
34
|
+
{ err, migration: step.name },
|
|
35
|
+
`Migration failed: ${step.name}`,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return { failed };
|
|
41
|
+
}
|