@sixb/cli 0.2.5 → 0.2.7

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.
Files changed (67) hide show
  1. package/dist/commands/api.d.ts.map +1 -1
  2. package/dist/commands/app.d.ts.map +1 -1
  3. package/dist/commands/check.d.ts +4 -0
  4. package/dist/commands/check.d.ts.map +1 -1
  5. package/dist/commands/db-migrate.d.ts +2 -0
  6. package/dist/commands/db-migrate.d.ts.map +1 -1
  7. package/dist/commands/deploy.d.ts +0 -20
  8. package/dist/commands/deploy.d.ts.map +1 -1
  9. package/dist/commands/env.d.ts.map +1 -1
  10. package/dist/commands/worker-group.d.ts.map +1 -1
  11. package/dist/commands/worker.d.ts.map +1 -1
  12. package/dist/lib/command-line.d.ts +16 -16
  13. package/dist/lib/command-line.d.ts.map +1 -1
  14. package/dist/lib/deploy-release.d.ts.map +1 -1
  15. package/dist/lib/deploy-source.d.ts +2 -0
  16. package/dist/lib/deploy-source.d.ts.map +1 -1
  17. package/dist/lib/env-config.d.ts.map +1 -1
  18. package/dist/lib/role-lifecycle.d.ts +8 -1
  19. package/dist/lib/role-lifecycle.d.ts.map +1 -1
  20. package/dist/lib/storage-migration.d.ts +7 -0
  21. package/dist/lib/storage-migration.d.ts.map +1 -1
  22. package/dist/lib/worker-registry.d.ts +1 -0
  23. package/dist/lib/worker-registry.d.ts.map +1 -1
  24. package/dist/runner/computer-connection.d.ts.map +1 -1
  25. package/dist/tui/app.d.ts +2 -0
  26. package/dist/tui/app.d.ts.map +1 -1
  27. package/dist/tui/moves.d.ts +62 -0
  28. package/dist/tui/moves.d.ts.map +1 -0
  29. package/dist/tui/parts.d.ts.map +1 -1
  30. package/dist/tui/settings.d.ts +2 -0
  31. package/dist/tui/settings.d.ts.map +1 -1
  32. package/dist/tui/transcript.d.ts +13 -6
  33. package/dist/tui/transcript.d.ts.map +1 -1
  34. package/dist/ui/deploy-screens.d.ts +1 -26
  35. package/dist/ui/deploy-screens.d.ts.map +1 -1
  36. package/dist/ui/screens.d.ts +8 -0
  37. package/dist/ui/screens.d.ts.map +1 -1
  38. package/package.json +21 -21
  39. package/src/commands/agent.ts +1 -1
  40. package/src/commands/api.ts +20 -10
  41. package/src/commands/app.ts +15 -7
  42. package/src/commands/check.ts +87 -49
  43. package/src/commands/db-migrate.ts +25 -1
  44. package/src/commands/deploy.ts +16 -186
  45. package/src/commands/dev-runner.ts +1 -1
  46. package/src/commands/env.ts +15 -5
  47. package/src/commands/worker-group.ts +36 -9
  48. package/src/commands/worker.ts +27 -9
  49. package/src/index.tsx +11 -27
  50. package/src/lib/command-line.ts +19 -53
  51. package/src/lib/deploy-release.ts +3 -4
  52. package/src/lib/deploy-source.ts +15 -0
  53. package/src/lib/env-config.ts +1 -3
  54. package/src/lib/role-lifecycle.ts +48 -8
  55. package/src/lib/storage-migration.ts +33 -1
  56. package/src/lib/worker-registry.ts +4 -1
  57. package/src/runner/computer-connection.ts +7 -1
  58. package/src/tui/app.tsx +183 -38
  59. package/src/tui/moves.ts +221 -0
  60. package/src/tui/parts.ts +7 -0
  61. package/src/tui/settings.ts +5 -0
  62. package/src/tui/transcript.tsx +50 -14
  63. package/src/ui/deploy-screens.ts +11 -112
  64. package/src/ui/screens.ts +23 -0
  65. package/dist/lib/github-ci.d.ts +0 -49
  66. package/dist/lib/github-ci.d.ts.map +0 -1
  67. package/src/lib/github-ci.ts +0 -262
@@ -65,12 +65,18 @@ export async function runEnvPull(options: {
65
65
  }
66
66
 
67
67
  const { config, source } = await requireSource(cwd)
68
- const token = options.tokenFile === undefined ? undefined : await readToken(options.tokenFile)
68
+ // A server: it reads with its token file, unless the source reads with the server's identity.
69
+ const server = options.tokenFile !== undefined
70
+ const token =
71
+ options.tokenFile !== undefined && source.serverNeedsToken !== false
72
+ ? await readToken(options.tokenFile)
73
+ : undefined
69
74
  const pulled = await pullEnv({
70
75
  cwd,
71
76
  config,
72
77
  source,
73
78
  env,
79
+ server,
74
80
  ...(token === undefined ? {} : { token }),
75
81
  force: options.force === true,
76
82
  })
@@ -91,19 +97,23 @@ async function pullEnv(input: {
91
97
  readonly config: EnvConfig
92
98
  readonly source: EnvSource
93
99
  readonly env: EnvName
100
+ readonly server: boolean
94
101
  readonly token?: string
95
102
  readonly force?: boolean
96
103
  }): Promise<PullResult> {
97
- const { config, source, env, token } = input
104
+ const { config, source, env, server, token } = input
98
105
  const path = join(input.cwd, ".env")
99
106
 
100
107
  let values: Readonly<Record<string, string>>
101
108
  try {
102
- values = await source.read(env, token === undefined ? {} : { token })
109
+ values = await source.read(
110
+ env,
111
+ server ? { server, ...(token === undefined ? {} : { token }) } : {}
112
+ )
103
113
  } catch (error) {
104
114
  // A server riding out an outage, so it can still take a hotfix. A refusal never reaches this:
105
115
  // a revoked token must stop the deploy.
106
- const riding = token !== undefined && error instanceof EnvSourceUnreachableError
116
+ const riding = server && error instanceof EnvSourceUnreachableError
107
117
  if (riding && (await isCompleteGeneratedEnv(path, config, env))) {
108
118
  return { kept: true, reason: errorMessage(error) }
109
119
  }
@@ -192,7 +202,7 @@ export async function pullIntoThisProcess(
192
202
  config: EnvConfig,
193
203
  source: EnvSource
194
204
  ): Promise<void> {
195
- const pulled = await pullEnv({ cwd, config, source, env: "dev" })
205
+ const pulled = await pullEnv({ cwd, config, source, env: "dev", server: false })
196
206
  if (pulled.kept) return
197
207
 
198
208
  const localPath = join(cwd, ".env.local")
@@ -1,6 +1,10 @@
1
1
  import { type LoadedSixbHost, loadSixbFromEntry } from "../lib/loadSixb"
2
2
  import { resolveRuntimeEntry } from "../lib/production"
3
- import { runUntilSignal, stopQuietly } from "../lib/role-lifecycle"
3
+ import { resolveDrainTimeoutMs, runUntilSignal, stopQuietly } from "../lib/role-lifecycle"
4
+
5
+ // Leaves 10 s inside the default 70 s kill timeout for abort finalization and provider shutdown.
6
+ const DEFAULT_WORKER_DRAIN_TIMEOUT_MS = 60_000
7
+
4
8
  import { recordApiPublicOrigin, stopSixbProviders, waitForWorkerFailure } from "../lib/runtime"
5
9
  import { assertShareableProviders } from "../lib/shareable-providers"
6
10
  import { migrateStorageForRole } from "../lib/storage-migration"
@@ -57,8 +61,18 @@ export async function runWorkerGroup(options: WorkerGroupOptions = {}) {
57
61
  let sixb: LoadedSixbHost | null = null
58
62
  let workers: QueueWorkerProcess[] = []
59
63
 
60
- async function stopWorkersAndProviders() {
61
- await Promise.all(workers.map((worker) => stopQuietly(() => worker.stop())))
64
+ async function stopWorkersAndProviders(drainTimeoutMs?: number) {
65
+ await Promise.all(
66
+ workers.map((worker) =>
67
+ stopQuietly(async () => {
68
+ if (drainTimeoutMs !== undefined) {
69
+ await worker.drain(drainTimeoutMs)
70
+ } else {
71
+ await worker.stop()
72
+ }
73
+ })
74
+ )
75
+ )
62
76
  if (sixb) {
63
77
  await stopSixbProviders(sixb)
64
78
  }
@@ -127,12 +141,25 @@ export async function runWorkerGroup(options: WorkerGroupOptions = {}) {
127
141
  )
128
142
 
129
143
  await Promise.race([
130
- runUntilSignal(async () => {
131
- app.stop()
132
- printStopping("worker-group")
133
- await stopWorkersAndProviders()
134
- sixb = null
135
- }),
144
+ runUntilSignal(
145
+ async (signal) => {
146
+ app.stop()
147
+ printStopping("worker-group")
148
+ const drainTimeoutMs =
149
+ signal === "SIGTERM"
150
+ ? resolveDrainTimeoutMs(DEFAULT_WORKER_DRAIN_TIMEOUT_MS)
151
+ : undefined
152
+ await stopWorkersAndProviders(drainTimeoutMs)
153
+ sixb = null
154
+ },
155
+ {
156
+ onInterrupt: () => {
157
+ for (const worker of workers) {
158
+ void stopQuietly(() => worker.stop())
159
+ }
160
+ },
161
+ }
162
+ ),
136
163
  ...workers.map(waitForWorkerFailure),
137
164
  ])
138
165
  } catch (error) {
@@ -1,7 +1,11 @@
1
1
  import { SixbCliError } from "../lib/errors"
2
2
  import { type LoadedSixbHost, loadSixbFromEntry } from "../lib/loadSixb"
3
3
  import { resolveRuntimeEntry } from "../lib/production"
4
- import { runUntilSignal, stopQuietly } from "../lib/role-lifecycle"
4
+ import { resolveDrainTimeoutMs, runUntilSignal, stopQuietly } from "../lib/role-lifecycle"
5
+
6
+ // Leaves 10 s inside the default 70 s kill timeout for abort finalization and provider shutdown.
7
+ const DEFAULT_WORKER_DRAIN_TIMEOUT_MS = 60_000
8
+
5
9
  import { recordApiPublicOrigin, stopSixbProviders, waitForWorkerFailure } from "../lib/runtime"
6
10
  import { assertShareableProviders } from "../lib/shareable-providers"
7
11
  import { migrateStorageForRole } from "../lib/storage-migration"
@@ -97,15 +101,29 @@ export async function runWorker(options: WorkerOptions = {}) {
97
101
  )
98
102
 
99
103
  await Promise.race([
100
- runUntilSignal(async () => {
101
- app.stop()
102
- printStopping("worker")
103
- await stopQuietly(() => worker?.stop() ?? Promise.resolve())
104
- if (sixb) {
105
- await stopSixbProviders(sixb)
104
+ runUntilSignal(
105
+ async (signal) => {
106
+ app.stop()
107
+ printStopping("worker")
108
+ if (signal === "SIGTERM") {
109
+ const timeoutMs = resolveDrainTimeoutMs(DEFAULT_WORKER_DRAIN_TIMEOUT_MS)
110
+ await stopQuietly(async () => {
111
+ await worker?.drain(timeoutMs)
112
+ })
113
+ } else {
114
+ await stopQuietly(() => worker?.stop() ?? Promise.resolve())
115
+ }
116
+ if (sixb) {
117
+ await stopSixbProviders(sixb)
118
+ }
119
+ sixb = null
120
+ },
121
+ {
122
+ onInterrupt: () => {
123
+ void stopQuietly(() => worker?.stop() ?? Promise.resolve())
124
+ },
106
125
  }
107
- sixb = null
108
- }),
126
+ ),
109
127
  waitForWorkerFailure(worker),
110
128
  ])
111
129
  } catch (error) {
package/src/index.tsx CHANGED
@@ -333,7 +333,11 @@ async function main(): Promise<void> {
333
333
 
334
334
  case "check": {
335
335
  const { runCheck } = await import("./commands/check")
336
- await runCheck({ entry: getFlag("entry") })
336
+ await runCheck({
337
+ entry: getFlag("entry"),
338
+ migrate: hasFlag("migrate"),
339
+ lake: hasFlag("lake"),
340
+ })
337
341
  break
338
342
  }
339
343
 
@@ -380,7 +384,7 @@ async function main(): Promise<void> {
380
384
 
381
385
  case "db:migrate": {
382
386
  const { runDbMigrate } = await import("./commands/db-migrate")
383
- await runDbMigrate({ entry: getFlag("entry") })
387
+ await runDbMigrate({ entry: getFlag("entry"), check: hasFlag("check") })
384
388
  break
385
389
  }
386
390
 
@@ -445,7 +449,11 @@ async function main(): Promise<void> {
445
449
 
446
450
  case "deploy": {
447
451
  const { runDeploy } = await import("./commands/deploy")
448
- await runDeploy({ dryRun: hasFlag("dry-run"), json: hasFlag("json"), ref: getFlag("ref") })
452
+ await runDeploy({
453
+ dryRun: hasFlag("dry-run"),
454
+ json: hasFlag("json"),
455
+ ref: getFlag("ref"),
456
+ })
449
457
  break
450
458
  }
451
459
 
@@ -465,36 +473,12 @@ async function main(): Promise<void> {
465
473
  break
466
474
  }
467
475
 
468
- case "deploy:setup": {
469
- const { runDeploySetup } = await import("./commands/deploy")
470
- await runDeploySetup({ admin: getFlag("admin"), key: getFlag("key") })
471
- break
472
- }
473
-
474
476
  case "deploy:check": {
475
477
  const { runDeployCheck } = await import("./commands/deploy")
476
478
  await runDeployCheck({ json: hasFlag("json") })
477
479
  break
478
480
  }
479
481
 
480
- case "deploy:ci": {
481
- const { runDeployCi } = await import("./commands/deploy")
482
- await runDeployCi({ branch: getFlag("branch") })
483
- break
484
- }
485
-
486
- case "deploy:access:list":
487
- case "deploy:access:add":
488
- case "deploy:access:remove": {
489
- const { runDeployAccess } = await import("./commands/deploy")
490
- await runDeployAccess({
491
- action: parsed.id.slice("deploy:access:".length) as "list" | "add" | "remove",
492
- value: positionals[0],
493
- json: hasFlag("json"),
494
- })
495
- break
496
- }
497
-
498
482
  case "deploy:restart":
499
483
  case "deploy:start":
500
484
  case "deploy:stop": {
@@ -140,6 +140,16 @@ export const CLI_OPTION_DEFINITIONS = {
140
140
  summary: "Branch, tag, or commit to deploy (default: HEAD)",
141
141
  kind: "string",
142
142
  },
143
+ migrate: {
144
+ syntax: "--migrate",
145
+ summary: "Bring the storage schema up to date first, as a deploy does",
146
+ kind: "boolean",
147
+ },
148
+ lake: {
149
+ syntax: "--lake",
150
+ summary: "Check the lake's dataset definitions too",
151
+ kind: "boolean",
152
+ },
143
153
  follow: { syntax: "--follow", summary: "Keep printing new log lines", kind: "boolean" },
144
154
  env: {
145
155
  syntax: "--env <dev|prod>",
@@ -166,26 +176,16 @@ export const CLI_OPTION_DEFINITIONS = {
166
176
  summary: "Replace a hand-written .env, losing what the source doesn't have",
167
177
  kind: "boolean",
168
178
  },
169
- admin: {
170
- syntax: "--admin <login>",
171
- summary: "Login with sudo that prepares the server (default: root)",
172
- kind: "string",
173
- },
174
- key: {
175
- syntax: "--key <path>",
176
- summary: "Public key to authorize for deploys (default: the one SSH uses)",
177
- kind: "string",
178
- },
179
- branch: {
180
- syntax: "--branch <name>",
181
- summary: "Branch whose pushes deploy (default: the repository's default branch)",
182
- kind: "string",
183
- },
184
179
  tail: {
185
180
  syntax: "--tail <lines>",
186
181
  summary: "Recent log lines to print (default: 100)",
187
182
  kind: "string",
188
183
  },
184
+ check: {
185
+ syntax: "--check",
186
+ summary: "Only report pending migrations, exiting with 3 when there are some",
187
+ kind: "boolean",
188
+ },
189
189
  "dry-run": {
190
190
  syntax: "--dry-run",
191
191
  summary: "Show what would change without changing it",
@@ -252,12 +252,7 @@ export type LocalCommandId =
252
252
  | "deploy:restart"
253
253
  | "deploy:start"
254
254
  | "deploy:stop"
255
- | "deploy:setup"
256
255
  | "deploy:check"
257
- | "deploy:ci"
258
- | "deploy:access:list"
259
- | "deploy:access:add"
260
- | "deploy:access:remove"
261
256
  | "env:init"
262
257
  | "env:pull"
263
258
  | "env:set"
@@ -542,7 +537,9 @@ const commandTree: readonly CommandNode[] = [
542
537
  maximumPositionals: Number.POSITIVE_INFINITY,
543
538
  rootHelpLabel: "worker-group [types...]",
544
539
  }),
545
- command("check", "Validate project configuration and health", { options: ["entry"] }),
540
+ command("check", "Validate project configuration and health", {
541
+ options: ["entry", "migrate", "lake"],
542
+ }),
546
543
  command("run", "Run a script against the project, here or in production", {
547
544
  usage: "sixb run [--deployed] [--yes] [--entry <path>] <script> [args...]",
548
545
  options: ["entry", "deployed", "yes"],
@@ -583,7 +580,7 @@ const commandTree: readonly CommandNode[] = [
583
580
  ),
584
581
  command("db:migrate", "Run database migrations ahead of a role", {
585
582
  path: ["db", "migrate"],
586
- options: ["entry"],
583
+ options: ["entry", "check"],
587
584
  rootHelpLabel: "db migrate",
588
585
  }),
589
586
  ],
@@ -617,18 +614,10 @@ const commandTree: readonly CommandNode[] = [
617
614
  rootHelpLabel: "deploy ...",
618
615
  }),
619
616
  children: [
620
- command("deploy:setup", "Prepare the server and project for deploys", {
621
- path: ["deploy", "setup"],
622
- options: ["admin", "key"],
623
- }),
624
617
  command("deploy:check", "Check what stands between you and a working deploy", {
625
618
  path: ["deploy", "check"],
626
619
  options: ["json"],
627
620
  }),
628
- command("deploy:ci", "Deploy from GitHub Actions on every push", {
629
- path: ["deploy", "ci"],
630
- options: ["branch"],
631
- }),
632
621
  command("deploy:status", "Show what the deployment runs, and which commit", {
633
622
  path: ["deploy", "status"],
634
623
  options: ["json"],
@@ -646,29 +635,6 @@ const commandTree: readonly CommandNode[] = [
646
635
  maximumPositionals: 1,
647
636
  })
648
637
  ),
649
- {
650
- name: "access",
651
- summary: "Manage the keys that may deploy and operate the deployment",
652
- usage: "sixb deploy access <list|add|remove>",
653
- children: [
654
- command("deploy:access:list", "List the authorized keys", {
655
- path: ["deploy", "access", "list"],
656
- options: ["json"],
657
- }),
658
- command("deploy:access:add", "Authorize a key: a .pub file, its text, or github:<user>", {
659
- path: ["deploy", "access", "add"],
660
- usage: "sixb deploy access add <key>",
661
- minimumPositionals: 1,
662
- maximumPositionals: 1,
663
- }),
664
- command("deploy:access:remove", "Revoke the keys with this fingerprint or comment", {
665
- path: ["deploy", "access", "remove"],
666
- usage: "sixb deploy access remove <fingerprint|comment>",
667
- minimumPositionals: 1,
668
- maximumPositionals: 1,
669
- }),
670
- ],
671
- },
672
638
  ],
673
639
  },
674
640
  {
@@ -101,10 +101,9 @@ export function buildDeployRelease(
101
101
  steps: {
102
102
  ...(options.envFromSource ? { env: sixb("env", "pull", "--env", "prod") } : {}),
103
103
  build: [sixb("build")],
104
- // The order `docs/deployment` gives. `check` probes every provider, so a broker or queue
105
- // the services cannot reach fails the deploy instead of passing for healthy; it needs the
106
- // schema migrated first.
107
- beforeStart: [sixb("db", "migrate"), sixb("check"), sixb("lake", "check")],
104
+ // One project load: migrate, then probe every provider, so a broker or queue the services
105
+ // cannot reach fails the deploy instead of passing for healthy, then the lake's datasets.
106
+ beforeStart: [sixb("check", "--migrate", "--lake")],
108
107
  },
109
108
  services: [
110
109
  ...enabled.map((service) => sixbService(config, service, env, http)),
@@ -48,6 +48,21 @@ export async function packSource(projectDir: string, ref = "HEAD"): Promise<Pack
48
48
  }
49
49
  }
50
50
 
51
+ /** The packages a project prints with Chromium through. */
52
+ const CHROMIUM_PACKAGES = ["playwright", "playwright-core"]
53
+
54
+ /** Whether the project depends on Playwright, so its server needs Chromium's libraries. */
55
+ export async function usesChromium(projectDir: string): Promise<boolean> {
56
+ const manifest = (await Bun.file(join(projectDir, "package.json"))
57
+ .json()
58
+ .catch(() => null)) as {
59
+ readonly dependencies?: Readonly<Record<string, string>>
60
+ readonly optionalDependencies?: Readonly<Record<string, string>>
61
+ } | null
62
+ const dependencies = { ...manifest?.dependencies, ...manifest?.optionalDependencies }
63
+ return CHROMIUM_PACKAGES.some((name) => name in dependencies)
64
+ }
65
+
51
66
  /**
52
67
  * The Bun version the project pins with `packageManager` (`"bun@1.4.2"`) in its `package.json` or
53
68
  * the nearest one above it, up to the repository root; else the Bun running this CLI. Read from the
@@ -50,9 +50,7 @@ function validateEnvConfig(value: unknown): EnvConfig {
50
50
 
51
51
  function isEnvSource(value: unknown): boolean {
52
52
  const candidate = value as Record<string, unknown> | null
53
- return ["names", "read", "write", "createServerToken"].every(
54
- (method) => typeof candidate?.[method] === "function"
55
- )
53
+ return ["names", "read", "write"].every((method) => typeof candidate?.[method] === "function")
56
54
  }
57
55
 
58
56
  /** The names `env` must have a value for and doesn't. An empty value counts as none. */
@@ -1,14 +1,35 @@
1
1
  import { appendFileSync } from "node:fs"
2
2
 
3
3
  // Kept apart from `runtime.ts`, which imports every worker package: `app` serves static assets for
4
- // as long as it runs and must not hold that module graph to reuse these two helpers.
4
+ // as long as it runs and must not hold that module graph to reuse these helpers.
5
+
6
+ export type ShutdownSignal = "SIGINT" | "SIGTERM"
7
+
8
+ export interface RunUntilSignalOptions {
9
+ /** Called if `SIGINT` arrives while a `SIGTERM` drain is already in progress. */
10
+ readonly onInterrupt?: () => void
11
+ }
5
12
 
6
13
  export async function stopQuietly(stopFn: (() => Promise<void>) | undefined | null): Promise<void> {
7
14
  if (!stopFn) return
8
15
  await stopFn().catch(() => {})
9
16
  }
10
17
 
11
- export async function runUntilSignal(onShutdown: () => Promise<void>): Promise<void> {
18
+ /** Reads an optional millisecond drain bound from `SIXB_DRAIN_TIMEOUT_MS`. */
19
+ export function resolveDrainTimeoutMs(fallbackMs: number): number {
20
+ const raw = process.env.SIXB_DRAIN_TIMEOUT_MS?.trim()
21
+ if (!raw) return fallbackMs
22
+ const parsed = Number(raw)
23
+ if (!Number.isSafeInteger(parsed) || parsed < 0) {
24
+ throw new Error("[SixbCLI] SIXB_DRAIN_TIMEOUT_MS must be a non-negative integer.")
25
+ }
26
+ return parsed
27
+ }
28
+
29
+ export async function runUntilSignal(
30
+ onShutdown: (signal: ShutdownSignal) => Promise<void>,
31
+ options: RunUntilSignalOptions = {}
32
+ ): Promise<void> {
12
33
  // Test-only readiness hook: e2e tests detect that a long-running role finished
13
34
  // starting by watching for this marker, rather than on the wording of what the
14
35
  // role prints. Unset in production, so this is a no-op there.
@@ -20,14 +41,33 @@ export async function runUntilSignal(onShutdown: () => Promise<void>): Promise<v
20
41
  await new Promise<void>((resolvePromise) => {
21
42
  let shuttingDown = false
22
43
 
23
- const shutdown = async () => {
24
- if (shuttingDown) return
44
+ const handle = (signal: ShutdownSignal) => {
45
+ // The SIGTERM listener stays until the shutdown ends: without one, a repeated SIGTERM takes
46
+ // the default action and kills the role mid-drain. When a deployment stops, systemd and the
47
+ // deploy supervisor each send one.
48
+ if (shuttingDown) {
49
+ if (signal === "SIGINT") {
50
+ process.off("SIGINT", onSigint)
51
+ options.onInterrupt?.()
52
+ }
53
+ return
54
+ }
55
+
25
56
  shuttingDown = true
26
- await onShutdown()
27
- resolvePromise()
57
+ if (signal === "SIGINT") process.off("SIGINT", onSigint)
58
+
59
+ void Promise.resolve()
60
+ .then(() => onShutdown(signal))
61
+ .finally(() => {
62
+ process.off("SIGINT", onSigint)
63
+ process.off("SIGTERM", onSigterm)
64
+ resolvePromise()
65
+ })
28
66
  }
29
67
 
30
- process.once("SIGINT", shutdown)
31
- process.once("SIGTERM", shutdown)
68
+ const onSigint = () => handle("SIGINT")
69
+ const onSigterm = () => handle("SIGTERM")
70
+ process.on("SIGINT", onSigint)
71
+ process.on("SIGTERM", onSigterm)
32
72
  })
33
73
  }
@@ -1,4 +1,9 @@
1
- import { migrateStorage } from "@sixb/core"
1
+ import {
2
+ isMigrationCapableStorage,
3
+ type MigrationStatus,
4
+ migrateStorage,
5
+ type Storage,
6
+ } from "@sixb/core"
2
7
  import { SixbCliError } from "./errors"
3
8
  import type { LoadedSixbHost } from "./loadSixb"
4
9
  import type { StorageSchemaRole } from "./production-roles"
@@ -92,6 +97,33 @@ export async function migrateStorageForRole(
92
97
  }
93
98
  }
94
99
 
100
+ /**
101
+ * The storage adapters `sixb db migrate` would bring up to date, read without touching the
102
+ * schema. A state a migration does not resolve, such as a history this build does not know,
103
+ * throws: migrating would fail too.
104
+ */
105
+ export async function findPendingMigrations(storage: Storage): Promise<readonly MigrationStatus[]> {
106
+ if (!isMigrationCapableStorage(storage)) return []
107
+
108
+ const statuses = await Promise.all(storage.migrators.map((migrator) => migrator.status()))
109
+ const pending: MigrationStatus[] = []
110
+
111
+ for (const status of statuses) {
112
+ if (status.state === "current") continue
113
+
114
+ if (status.state === "pending" || status.state === "uninitialized") {
115
+ pending.push(status)
116
+ continue
117
+ }
118
+
119
+ throw new SixbCliError(`[SixbCLI] ${status.adapterId} cannot be migrated: ${status.state}.`, {
120
+ remediation: status.reason,
121
+ })
122
+ }
123
+
124
+ return pending
125
+ }
126
+
95
127
  async function runMigration(sixb: LoadedSixbHost, role: StorageSchemaRole) {
96
128
  try {
97
129
  return await migrateStorage(sixb.storage, {
@@ -24,7 +24,10 @@ export const WORKER_TYPES = [
24
24
  export type WorkerType = (typeof WORKER_TYPES)[number]
25
25
 
26
26
  export type WorkerConcurrency = Readonly<Partial<Record<WorkerType, number>>>
27
- export type QueueWorkerProcess = Worker & { readonly concurrency: number }
27
+ export type QueueWorkerProcess = Worker & {
28
+ readonly concurrency: number
29
+ drain(timeoutMs?: number): Promise<boolean>
30
+ }
28
31
 
29
32
  export interface WorkerCreationOptions {
30
33
  readonly agentApiBaseUrl?: string
@@ -74,6 +74,12 @@ export class ComputerConnection {
74
74
  this.onEvent({ type: "error", message: errorMessage(error) })
75
75
  )
76
76
  },
77
+ // A conversation moved to a folder here with a turn waiting runs at once, not at renewal.
78
+ onThreadMoved: (event) => {
79
+ void this.route(event.threadId).catch((error: unknown) =>
80
+ this.onEvent({ type: "error", message: errorMessage(error) })
81
+ )
82
+ },
77
83
  })
78
84
  }
79
85
 
@@ -173,7 +179,7 @@ export class ComputerConnection {
173
179
  }
174
180
 
175
181
  /** Each folder's runner would read the thread to learn it isn't theirs: read it once. */
176
- private async route(threadId: string, runId: string): Promise<void> {
182
+ private async route(threadId: string, runId?: string): Promise<void> {
177
183
  const { data: thread } = await getAgentThread({
178
184
  client: this.connection.client,
179
185
  path: { threadId },