@sixb/cli 0.0.1
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/LICENSE +21 -0
- package/README.md +275 -0
- package/dist/commands/access-token-utils.d.ts +9 -0
- package/dist/commands/access-token-utils.d.ts.map +1 -0
- package/dist/commands/api.d.ts +13 -0
- package/dist/commands/api.d.ts.map +1 -0
- package/dist/commands/app.d.ts +9 -0
- package/dist/commands/app.d.ts.map +1 -0
- package/dist/commands/atlas.d.ts +9 -0
- package/dist/commands/atlas.d.ts.map +1 -0
- package/dist/commands/auth.d.ts +8 -0
- package/dist/commands/auth.d.ts.map +1 -0
- package/dist/commands/build.d.ts +6 -0
- package/dist/commands/build.d.ts.map +1 -0
- package/dist/commands/check.d.ts +5 -0
- package/dist/commands/check.d.ts.map +1 -0
- package/dist/commands/db-migrate.d.ts +5 -0
- package/dist/commands/db-migrate.d.ts.map +1 -0
- package/dist/commands/dev.d.ts +12 -0
- package/dist/commands/dev.d.ts.map +1 -0
- package/dist/commands/init.d.ts +3 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/lake-check.d.ts +5 -0
- package/dist/commands/lake-check.d.ts.map +1 -0
- package/dist/commands/lake-cleanup.d.ts +16 -0
- package/dist/commands/lake-cleanup.d.ts.map +1 -0
- package/dist/commands/orchestrator.d.ts +6 -0
- package/dist/commands/orchestrator.d.ts.map +1 -0
- package/dist/commands/rules.d.ts +6 -0
- package/dist/commands/rules.d.ts.map +1 -0
- package/dist/commands/scheduler.d.ts +6 -0
- package/dist/commands/scheduler.d.ts.map +1 -0
- package/dist/commands/service-account.d.ts +14 -0
- package/dist/commands/service-account.d.ts.map +1 -0
- package/dist/commands/token.d.ts +14 -0
- package/dist/commands/token.d.ts.map +1 -0
- package/dist/commands/typegen.d.ts +5 -0
- package/dist/commands/typegen.d.ts.map +1 -0
- package/dist/commands/worker-group.d.ts +8 -0
- package/dist/commands/worker-group.d.ts.map +1 -0
- package/dist/commands/worker.d.ts +8 -0
- package/dist/commands/worker.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/lib/api-client.d.ts +26 -0
- package/dist/lib/api-client.d.ts.map +1 -0
- package/dist/lib/browser-topology.d.ts +62 -0
- package/dist/lib/browser-topology.d.ts.map +1 -0
- package/dist/lib/errors.d.ts +19 -0
- package/dist/lib/errors.d.ts.map +1 -0
- package/dist/lib/health.d.ts +25 -0
- package/dist/lib/health.d.ts.map +1 -0
- package/dist/lib/loadSixb.d.ts +49 -0
- package/dist/lib/loadSixb.d.ts.map +1 -0
- package/dist/lib/production-roles.d.ts +65 -0
- package/dist/lib/production-roles.d.ts.map +1 -0
- package/dist/lib/production.d.ts +29 -0
- package/dist/lib/production.d.ts.map +1 -0
- package/dist/lib/public-origin.d.ts +17 -0
- package/dist/lib/public-origin.d.ts.map +1 -0
- package/dist/lib/runtime.d.ts +76 -0
- package/dist/lib/runtime.d.ts.map +1 -0
- package/dist/lib/shareable-providers.d.ts +20 -0
- package/dist/lib/shareable-providers.d.ts.map +1 -0
- package/dist/lib/storage-migration.d.ts +40 -0
- package/dist/lib/storage-migration.d.ts.map +1 -0
- package/dist/lib/typegen.d.ts +5 -0
- package/dist/lib/typegen.d.ts.map +1 -0
- package/dist/lib/worker-registry.d.ts +30 -0
- package/dist/lib/worker-registry.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/ui/index.d.ts +164 -0
- package/dist/ui/index.d.ts.map +1 -0
- package/package.json +68 -0
- package/src/commands/access-token-utils.ts +63 -0
- package/src/commands/api.tsx +111 -0
- package/src/commands/app.tsx +91 -0
- package/src/commands/atlas.tsx +75 -0
- package/src/commands/auth.tsx +96 -0
- package/src/commands/build.tsx +90 -0
- package/src/commands/check.tsx +58 -0
- package/src/commands/db-migrate.tsx +27 -0
- package/src/commands/dev.tsx +141 -0
- package/src/commands/init.tsx +18 -0
- package/src/commands/lake-check.tsx +20 -0
- package/src/commands/lake-cleanup.tsx +63 -0
- package/src/commands/orchestrator.tsx +87 -0
- package/src/commands/rules.tsx +81 -0
- package/src/commands/scheduler.tsx +77 -0
- package/src/commands/service-account.tsx +304 -0
- package/src/commands/token.tsx +143 -0
- package/src/commands/typegen.tsx +18 -0
- package/src/commands/worker-group.tsx +120 -0
- package/src/commands/worker.tsx +96 -0
- package/src/index.tsx +390 -0
- package/src/lib/api-client.ts +132 -0
- package/src/lib/browser-topology.ts +292 -0
- package/src/lib/errors.ts +40 -0
- package/src/lib/health.ts +138 -0
- package/src/lib/loadSixb.ts +175 -0
- package/src/lib/production-roles.ts +48 -0
- package/src/lib/production.ts +104 -0
- package/src/lib/public-origin.ts +46 -0
- package/src/lib/runtime.ts +346 -0
- package/src/lib/shareable-providers.ts +67 -0
- package/src/lib/storage-migration.ts +120 -0
- package/src/lib/typegen.ts +12 -0
- package/src/lib/worker-registry.ts +191 -0
- package/src/ui/index.tsx +953 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { stat } from "node:fs/promises"
|
|
2
|
+
import { basename, dirname, resolve, sep } from "node:path"
|
|
3
|
+
import { type LoadedSixb, loadSixbFromEntry } from "./loadSixb"
|
|
4
|
+
import type { ProductionRole } from "./production-roles"
|
|
5
|
+
import { assertShareableProviders } from "./shareable-providers"
|
|
6
|
+
|
|
7
|
+
export interface RuntimeEntryOptions {
|
|
8
|
+
readonly entry?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ProductionRuntimeOptions extends RuntimeEntryOptions {
|
|
12
|
+
/**
|
|
13
|
+
* The role being started. Required so the provider guard cannot be skipped by
|
|
14
|
+
* forgetting to call it: every production role goes through this loader, and the
|
|
15
|
+
* role union in `shareable-providers` decides whether the guard applies.
|
|
16
|
+
*/
|
|
17
|
+
readonly role: ProductionRole
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ProductionPaths {
|
|
21
|
+
readonly projectRoot: string
|
|
22
|
+
readonly buildOutdir: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function resolveRuntimeEntry(options: RuntimeEntryOptions = {}): Promise<string> {
|
|
26
|
+
const sourceEntry = resolve("sixb.config.ts")
|
|
27
|
+
const defaultBuiltEntry = resolve(".sixb/dist/sixb.config.js")
|
|
28
|
+
|
|
29
|
+
if (options.entry) {
|
|
30
|
+
return resolve(options.entry)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const builtInfo = await stat(defaultBuiltEntry).catch(() => null)
|
|
34
|
+
return builtInfo ? defaultBuiltEntry : sourceEntry
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function loadProductionSixb(
|
|
38
|
+
options: ProductionRuntimeOptions
|
|
39
|
+
): Promise<{ entry: string; sixb: LoadedSixb; projectRoot: string; buildOutdir: string }> {
|
|
40
|
+
const entry = await resolveRuntimeEntry(options)
|
|
41
|
+
const sixb = await loadSixbFromEntry(entry)
|
|
42
|
+
assertShareableProviders(sixb, options.role)
|
|
43
|
+
const paths = await resolveProductionPaths(entry)
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
entry,
|
|
47
|
+
sixb,
|
|
48
|
+
projectRoot: paths.projectRoot,
|
|
49
|
+
buildOutdir: paths.buildOutdir,
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function resolveProductionPaths(entry: string): Promise<ProductionPaths> {
|
|
54
|
+
const resolvedEntry = resolve(entry)
|
|
55
|
+
const projectRoot = resolveProjectRoot(resolvedEntry)
|
|
56
|
+
const defaultBuildOutdir = resolve(projectRoot, ".sixb", "dist")
|
|
57
|
+
|
|
58
|
+
if (isDefaultBuildEntry(resolvedEntry)) {
|
|
59
|
+
return { projectRoot, buildOutdir: defaultBuildOutdir }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const entryDir = dirname(resolvedEntry)
|
|
63
|
+
if (await isCustomBuildOutdirEntry(resolvedEntry, entryDir)) {
|
|
64
|
+
return { projectRoot, buildOutdir: entryDir }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return { projectRoot, buildOutdir: defaultBuildOutdir }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function resolveProjectRoot(entry: string): string {
|
|
71
|
+
const resolvedEntry = resolve(entry)
|
|
72
|
+
const distMarker = `${sep}.sixb${sep}dist${sep}`
|
|
73
|
+
const distIndex = resolvedEntry.lastIndexOf(distMarker)
|
|
74
|
+
|
|
75
|
+
if (distIndex >= 0) {
|
|
76
|
+
return resolvedEntry.slice(0, distIndex)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return dirname(resolvedEntry)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function builtAppOutdir(buildOutdir: string): string {
|
|
83
|
+
return resolve(buildOutdir, "app")
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function builtAtlasOutdir(buildOutdir: string): string {
|
|
87
|
+
return resolve(buildOutdir, "atlas")
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function isDefaultBuildEntry(entry: string): boolean {
|
|
91
|
+
return entry.includes(`${sep}.sixb${sep}dist${sep}`)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function isCustomBuildOutdirEntry(entry: string, entryDir: string): Promise<boolean> {
|
|
95
|
+
if (basename(entry) !== "sixb.config.js") {
|
|
96
|
+
return false
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const hasBuiltAtlas = await stat(resolve(entryDir, "atlas"))
|
|
100
|
+
.then((info) => info.isDirectory())
|
|
101
|
+
.catch(() => false)
|
|
102
|
+
|
|
103
|
+
return hasBuiltAtlas
|
|
104
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One definition of a public origin, for every role that reads one.
|
|
3
|
+
*
|
|
4
|
+
* `SIXB_API_PUBLIC_ORIGIN` used to mean something different depending on who read it: the browser
|
|
5
|
+
* roles parsed it and refused anything that was not a bare origin, while the agent worker took any
|
|
6
|
+
* non-blank string and discovered the problem at its first request instead. The same value could
|
|
7
|
+
* therefore stop `sixb api` at startup and start `sixb worker agent`, which is the failure mode
|
|
8
|
+
* worth removing — a bad origin should be refused once, by the first role that reads it.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** An origin is scheme, host, and port, and nothing after them. */
|
|
12
|
+
export function normalizeOrigin(value: string, label: string): string {
|
|
13
|
+
let url: URL
|
|
14
|
+
try {
|
|
15
|
+
url = new URL(value)
|
|
16
|
+
} catch {
|
|
17
|
+
throw new Error(`[SixbCLI] Invalid ${label}: '${value}'.`)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
21
|
+
throw new Error(`[SixbCLI] ${label} must use http or https.`)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (url.pathname !== "/" || url.search || url.hash) {
|
|
25
|
+
throw new Error(`[SixbCLI] ${label} must be an origin, not a full URL.`)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return url.origin
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The origin a flag or the environment set, normalized. `null` when neither did, and a thrown
|
|
33
|
+
* error when one of them set something that is not an origin.
|
|
34
|
+
*/
|
|
35
|
+
export function configuredOrigin(
|
|
36
|
+
value: string | undefined,
|
|
37
|
+
envName: string,
|
|
38
|
+
label: string
|
|
39
|
+
): string | null {
|
|
40
|
+
const configured = nonblank(value) ?? nonblank(process.env[envName])
|
|
41
|
+
return configured === undefined ? null : normalizeOrigin(configured, label)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function nonblank(value: string | undefined): string | undefined {
|
|
45
|
+
return value?.trim() || undefined
|
|
46
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { appendFileSync } from "node:fs"
|
|
2
|
+
import { ActionWorker } from "@sixb/action-worker"
|
|
3
|
+
import { AgentWorker } from "@sixb/agent-worker"
|
|
4
|
+
import { migrateStorage } from "@sixb/core"
|
|
5
|
+
import { flushSixbErrors } from "@sixb/core/internal/error-reporting"
|
|
6
|
+
import { getProjectionDispatchDescriptors } from "@sixb/core/internal/projections"
|
|
7
|
+
import type { Worker } from "@sixb/core/internal/workers"
|
|
8
|
+
import { assertLakeDatasetDefinitionsCompatible } from "@sixb/core/lake-storage"
|
|
9
|
+
import {
|
|
10
|
+
type CompileRoutesDiagnostic,
|
|
11
|
+
compileRoutesWithDiagnostics,
|
|
12
|
+
OrchestratorWorker,
|
|
13
|
+
} from "@sixb/orchestrator"
|
|
14
|
+
import { PipelineWorker } from "@sixb/pipeline-worker"
|
|
15
|
+
import { ProjectionWorker } from "@sixb/projection-worker"
|
|
16
|
+
import { RulesWorker } from "@sixb/rules-worker"
|
|
17
|
+
import { SyncWorker } from "@sixb/sync-worker"
|
|
18
|
+
import { WorkflowWorker } from "@sixb/workflow-worker"
|
|
19
|
+
import type { LoadedSixb } from "./loadSixb"
|
|
20
|
+
|
|
21
|
+
export function waitForWorkerFailure(worker: Worker | null | undefined): Promise<never> {
|
|
22
|
+
return new Promise<never>((_resolve, reject) => {
|
|
23
|
+
worker?.wait().catch(reject)
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function stopQuietly(stopFn: (() => Promise<void>) | undefined | null): Promise<void> {
|
|
28
|
+
if (!stopFn) return
|
|
29
|
+
await stopFn().catch(() => {})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function closeProvider(provider: { close?(): void | Promise<void> }): Promise<void> {
|
|
33
|
+
await provider.close?.()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface RunningSixbRuntime {
|
|
37
|
+
readonly rulesWorker: RulesWorker | null
|
|
38
|
+
readonly syncWorker: SyncWorker | null
|
|
39
|
+
readonly actionWorker: ActionWorker | null
|
|
40
|
+
readonly agentWorker: AgentWorker | null
|
|
41
|
+
readonly projectionWorker: ProjectionWorker | null
|
|
42
|
+
readonly pipelineWorker: PipelineWorker | null
|
|
43
|
+
readonly workflowWorker: WorkflowWorker | null
|
|
44
|
+
readonly orchestratorWorker: OrchestratorWorker | null
|
|
45
|
+
readonly warnings: readonly string[]
|
|
46
|
+
waitForWorkerFailure(): Promise<never>
|
|
47
|
+
stop(): Promise<void>
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface StartSixbRuntimeOptions {
|
|
51
|
+
readonly cohostWorkers?: boolean
|
|
52
|
+
readonly agentApiBaseUrl?: string
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface RunningRulesRuntime {
|
|
56
|
+
readonly rulesWorker: RulesWorker | null
|
|
57
|
+
stop(): Promise<void>
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface RunningSchedulerRuntime {
|
|
61
|
+
stop(): Promise<void>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface RunningOrchestratorRuntime {
|
|
65
|
+
readonly orchestratorWorker: OrchestratorWorker | null
|
|
66
|
+
readonly warnings: readonly string[]
|
|
67
|
+
stop(): Promise<void>
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export async function migrateRuntimeStorage(sixb: LoadedSixb): Promise<void> {
|
|
71
|
+
await migrateStorage(sixb.storage)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function checkRuntimeLakeDefinitions(sixb: LoadedSixb): Promise<void> {
|
|
75
|
+
await assertLakeDatasetDefinitionsCompatible({
|
|
76
|
+
lakeStorage: sixb.lakeStorage,
|
|
77
|
+
definitions: sixb.listDatasets(),
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function stopSixbProviders(sixb: LoadedSixb): Promise<void> {
|
|
82
|
+
await stopQuietly(() => flushSixbErrors(sixb))
|
|
83
|
+
await stopQuietly(() => sixb.disconnectConnectors())
|
|
84
|
+
await stopQuietly(() => closeProvider(sixb.queues))
|
|
85
|
+
// Stop tracked outbox publication before closing the storage it claims from.
|
|
86
|
+
await stopQuietly(() => sixb.closeBroker())
|
|
87
|
+
// The final outbox drain can report delivery failures after the initial error flush.
|
|
88
|
+
await stopQuietly(() => flushSixbErrors(sixb))
|
|
89
|
+
await stopQuietly(() => closeProvider(sixb.storage))
|
|
90
|
+
await stopQuietly(() => closeProvider(sixb.lakeStorage))
|
|
91
|
+
await stopQuietly(() => closeProvider(sixb.blobStorage))
|
|
92
|
+
await stopQuietly(() => sixb.closeLogger())
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function startRulesRuntime(sixb: LoadedSixb): Promise<RunningRulesRuntime> {
|
|
96
|
+
let rulesWorker: RulesWorker | null = null
|
|
97
|
+
|
|
98
|
+
if (sixb.listRules().length > 0) {
|
|
99
|
+
rulesWorker = new RulesWorker(sixb)
|
|
100
|
+
await rulesWorker.start()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
rulesWorker,
|
|
105
|
+
async stop() {
|
|
106
|
+
await stopQuietly(() => rulesWorker?.stop() ?? Promise.resolve())
|
|
107
|
+
},
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function startSchedulerRuntime(sixb: LoadedSixb): Promise<RunningSchedulerRuntime> {
|
|
112
|
+
await sixb.startScheduler()
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
async stop() {
|
|
116
|
+
await stopQuietly(() => sixb.stopScheduler())
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export async function startOrchestratorRuntime(
|
|
122
|
+
sixb: LoadedSixb
|
|
123
|
+
): Promise<RunningOrchestratorRuntime> {
|
|
124
|
+
const projections = getProjectionDispatchDescriptors(sixb)
|
|
125
|
+
const { routes, diagnostics } = compileRoutesWithDiagnostics({
|
|
126
|
+
schedules: sixb.listSchedules(),
|
|
127
|
+
syncs: sixb.listSyncs(),
|
|
128
|
+
pipelines: sixb.listPipelines(),
|
|
129
|
+
projections,
|
|
130
|
+
workflows: sixb.workflows.list(),
|
|
131
|
+
})
|
|
132
|
+
const warnings = diagnostics.map(formatRouteDiagnosticWarning)
|
|
133
|
+
let orchestratorWorker: OrchestratorWorker | null = null
|
|
134
|
+
|
|
135
|
+
if (routes.size > 0) {
|
|
136
|
+
const projectionRuns = sixb.storage.projectionRuns
|
|
137
|
+
if (projections.length > 0 && !projectionRuns) {
|
|
138
|
+
throw new Error(
|
|
139
|
+
"[SixbCLI] Projection dispatch requires storage.projectionRuns for durable reconciliation."
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
const projectionDispatch =
|
|
143
|
+
projections.length > 0 && projectionRuns
|
|
144
|
+
? { lakeStorage: sixb.lakeStorage, projectionRuns }
|
|
145
|
+
: undefined
|
|
146
|
+
orchestratorWorker = new OrchestratorWorker({
|
|
147
|
+
projectId: sixb.id,
|
|
148
|
+
events: sixb.events,
|
|
149
|
+
queues: sixb.queues,
|
|
150
|
+
routes,
|
|
151
|
+
...(projectionDispatch ? { projectionDispatch } : {}),
|
|
152
|
+
})
|
|
153
|
+
await orchestratorWorker.start()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
orchestratorWorker,
|
|
158
|
+
warnings,
|
|
159
|
+
async stop() {
|
|
160
|
+
await stopQuietly(() => orchestratorWorker?.stop() ?? Promise.resolve())
|
|
161
|
+
},
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Starts the background runtimes owned by the CLI host.
|
|
167
|
+
*
|
|
168
|
+
* Startup order (consumers before producers):
|
|
169
|
+
* 1. RulesWorker (subscribes to ontology events)
|
|
170
|
+
* 2. ActionWorker (subscribes to events)
|
|
171
|
+
* 3. AgentWorker (claims from queues)
|
|
172
|
+
* 4. ProjectionWorker (claims from queues)
|
|
173
|
+
* 5. PipelineWorker (claims from queues)
|
|
174
|
+
* 6. WorkflowWorker (claims from queues)
|
|
175
|
+
* 7. SyncWorker (claims from queues)
|
|
176
|
+
* 8. Orchestrator (subscribes to events and enqueues jobs)
|
|
177
|
+
* 9. Scheduler (emits schedule.triggered)
|
|
178
|
+
*
|
|
179
|
+
* Shutdown order (producers before consumers):
|
|
180
|
+
* 1. Scheduler
|
|
181
|
+
* 2. Orchestrator (drains pending dispatches)
|
|
182
|
+
* 3. SyncWorker
|
|
183
|
+
* 4. WorkflowWorker
|
|
184
|
+
* 5. PipelineWorker
|
|
185
|
+
* 6. ProjectionWorker
|
|
186
|
+
* 7. AgentWorker
|
|
187
|
+
* 8. ActionWorker
|
|
188
|
+
* 9. RulesWorker (drains pending evaluations)
|
|
189
|
+
* 10. Runtime providers (connectors, broker)
|
|
190
|
+
*/
|
|
191
|
+
export async function startSixbRuntime(
|
|
192
|
+
sixb: LoadedSixb,
|
|
193
|
+
options: StartSixbRuntimeOptions = {}
|
|
194
|
+
): Promise<RunningSixbRuntime> {
|
|
195
|
+
await migrateRuntimeStorage(sixb)
|
|
196
|
+
await checkRuntimeLakeDefinitions(sixb)
|
|
197
|
+
|
|
198
|
+
let rulesRuntime: RunningRulesRuntime | null = null
|
|
199
|
+
let schedulerRuntime: RunningSchedulerRuntime | null = null
|
|
200
|
+
let orchestratorRuntime: RunningOrchestratorRuntime | null = null
|
|
201
|
+
let rulesWorker: RulesWorker | null = null
|
|
202
|
+
let syncWorker: SyncWorker | null = null
|
|
203
|
+
let actionWorker: ActionWorker | null = null
|
|
204
|
+
let agentWorker: AgentWorker | null = null
|
|
205
|
+
let pipelineWorker: PipelineWorker | null = null
|
|
206
|
+
let workflowWorker: WorkflowWorker | null = null
|
|
207
|
+
let orchestratorWorker: OrchestratorWorker | null = null
|
|
208
|
+
let projectionWorker: ProjectionWorker | null = null
|
|
209
|
+
const warnings: string[] = []
|
|
210
|
+
|
|
211
|
+
async function stop() {
|
|
212
|
+
await stopQuietly(() => schedulerRuntime?.stop() ?? Promise.resolve())
|
|
213
|
+
await stopQuietly(() => orchestratorRuntime?.stop() ?? Promise.resolve())
|
|
214
|
+
await stopQuietly(() => syncWorker?.stop() ?? Promise.resolve())
|
|
215
|
+
await stopQuietly(() => workflowWorker?.stop() ?? Promise.resolve())
|
|
216
|
+
await stopQuietly(() => pipelineWorker?.stop() ?? Promise.resolve())
|
|
217
|
+
await stopQuietly(() => projectionWorker?.stop() ?? Promise.resolve())
|
|
218
|
+
await stopQuietly(() => agentWorker?.stop() ?? Promise.resolve())
|
|
219
|
+
await stopQuietly(() => actionWorker?.stop() ?? Promise.resolve())
|
|
220
|
+
await stopQuietly(() => rulesRuntime?.stop() ?? Promise.resolve())
|
|
221
|
+
await stopSixbProviders(sixb)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
try {
|
|
225
|
+
rulesRuntime = await startRulesRuntime(sixb)
|
|
226
|
+
rulesWorker = rulesRuntime.rulesWorker
|
|
227
|
+
|
|
228
|
+
if (options.cohostWorkers) {
|
|
229
|
+
if (sixb.listActions().length > 0) {
|
|
230
|
+
actionWorker = new ActionWorker(sixb)
|
|
231
|
+
await actionWorker.start()
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (sixb.agents.list().length > 0 && sixb.storage.agents) {
|
|
235
|
+
agentWorker = new AgentWorker(sixb, {
|
|
236
|
+
apiBaseUrl: requireAgentApiBaseUrl(options.agentApiBaseUrl),
|
|
237
|
+
})
|
|
238
|
+
await agentWorker.start()
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const projectionCount =
|
|
242
|
+
sixb.listObjectProjections().length +
|
|
243
|
+
sixb.listLinkProjections().length +
|
|
244
|
+
sixb.listTelemetryProjections().length
|
|
245
|
+
if (projectionCount > 0) {
|
|
246
|
+
projectionWorker = new ProjectionWorker(sixb)
|
|
247
|
+
await projectionWorker.start()
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (sixb.listPipelines().length > 0) {
|
|
251
|
+
pipelineWorker = new PipelineWorker(sixb)
|
|
252
|
+
await pipelineWorker.start()
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (sixb.workflows.list().length > 0) {
|
|
256
|
+
workflowWorker = new WorkflowWorker(sixb)
|
|
257
|
+
await workflowWorker.start()
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (sixb.listSyncs().length > 0) {
|
|
261
|
+
syncWorker = new SyncWorker(sixb)
|
|
262
|
+
await syncWorker.start()
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
orchestratorRuntime = await startOrchestratorRuntime(sixb)
|
|
266
|
+
orchestratorWorker = orchestratorRuntime.orchestratorWorker
|
|
267
|
+
warnings.push(...orchestratorRuntime.warnings)
|
|
268
|
+
|
|
269
|
+
schedulerRuntime = await startSchedulerRuntime(sixb)
|
|
270
|
+
}
|
|
271
|
+
} catch (error) {
|
|
272
|
+
await stop()
|
|
273
|
+
throw error
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const activeWorkers = (): Worker[] => {
|
|
277
|
+
const workers: Worker[] = []
|
|
278
|
+
if (rulesWorker) workers.push(rulesWorker)
|
|
279
|
+
if (syncWorker) workers.push(syncWorker)
|
|
280
|
+
if (actionWorker) workers.push(actionWorker)
|
|
281
|
+
if (agentWorker) workers.push(agentWorker)
|
|
282
|
+
if (projectionWorker) workers.push(projectionWorker)
|
|
283
|
+
if (pipelineWorker) workers.push(pipelineWorker)
|
|
284
|
+
if (workflowWorker) workers.push(workflowWorker)
|
|
285
|
+
if (orchestratorWorker) workers.push(orchestratorWorker)
|
|
286
|
+
return workers
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return {
|
|
290
|
+
rulesWorker,
|
|
291
|
+
syncWorker,
|
|
292
|
+
actionWorker,
|
|
293
|
+
agentWorker,
|
|
294
|
+
projectionWorker,
|
|
295
|
+
pipelineWorker,
|
|
296
|
+
workflowWorker,
|
|
297
|
+
orchestratorWorker,
|
|
298
|
+
warnings,
|
|
299
|
+
waitForWorkerFailure() {
|
|
300
|
+
return Promise.race(activeWorkers().map(waitForWorkerFailure))
|
|
301
|
+
},
|
|
302
|
+
stop,
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function formatRouteDiagnosticWarning(diagnostic: CompileRoutesDiagnostic): string {
|
|
307
|
+
switch (diagnostic.type) {
|
|
308
|
+
case "workflow.schedule.input-required":
|
|
309
|
+
return `[Sixb] Workflow '${diagnostic.workflowId}' is scheduled but has non-empty input (${diagnostic.inputFields.join(", ")}); it was not auto-routed.`
|
|
310
|
+
case "schedule.reference.unknown":
|
|
311
|
+
return `[Sixb] ${diagnostic.consumerKind} '${diagnostic.consumerId}' references unknown schedule '${diagnostic.scheduleId}'; it was not auto-routed.`
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function requireAgentApiBaseUrl(value: string | undefined): string {
|
|
316
|
+
const trimmed = value?.trim()
|
|
317
|
+
if (!trimmed) {
|
|
318
|
+
throw new Error("[SixbCLI] Agent workers require an API public origin.")
|
|
319
|
+
}
|
|
320
|
+
return trimmed
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export async function runUntilSignal(onShutdown: () => Promise<void>): Promise<void> {
|
|
324
|
+
// Test-only readiness hook: e2e tests detect that a long-running role finished
|
|
325
|
+
// starting by watching for this marker. It avoids depending on Ink's rendered
|
|
326
|
+
// output, which is suppressed when stdout is not a TTY (e.g. under CI). Unset in
|
|
327
|
+
// production, so this is a no-op there.
|
|
328
|
+
const readyLog = process.env.SIXB_CLI_TEST_READY_LOG
|
|
329
|
+
if (readyLog) {
|
|
330
|
+
appendFileSync(readyLog, `${JSON.stringify({ type: "role:ready" })}\n`, "utf-8")
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
await new Promise<void>((resolvePromise) => {
|
|
334
|
+
let shuttingDown = false
|
|
335
|
+
|
|
336
|
+
const shutdown = async () => {
|
|
337
|
+
if (shuttingDown) return
|
|
338
|
+
shuttingDown = true
|
|
339
|
+
await onShutdown()
|
|
340
|
+
resolvePromise()
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
process.once("SIGINT", shutdown)
|
|
344
|
+
process.once("SIGTERM", shutdown)
|
|
345
|
+
})
|
|
346
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { Broker, Queues } from "@sixb/core"
|
|
2
|
+
import { SixbCliError } from "./errors"
|
|
3
|
+
import type { LoadedSixb } from "./loadSixb"
|
|
4
|
+
import { type ProductionRole, productionRoleFacts } from "./production-roles"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The two provider slots that declare whether they cross a process boundary.
|
|
8
|
+
*
|
|
9
|
+
* The storage slots are left out because whether a directory is a shared mount is not something
|
|
10
|
+
* the runtime can know, and a guess would either block a single-node deployment or give false
|
|
11
|
+
* confidence about a multi-replica one.
|
|
12
|
+
*/
|
|
13
|
+
const SLOTS = [
|
|
14
|
+
{
|
|
15
|
+
name: "queues",
|
|
16
|
+
get: (sixb: LoadedSixb): Broker | Queues => sixb.queues,
|
|
17
|
+
replacements: "@sixb/bullmq",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
name: "broker",
|
|
21
|
+
get: (sixb: LoadedSixb): Broker | Queues => sixb.broker,
|
|
22
|
+
replacements: "@sixb/redis or @sixb/nats",
|
|
23
|
+
},
|
|
24
|
+
] as const
|
|
25
|
+
|
|
26
|
+
function isProcessLocal(slot: (typeof SLOTS)[number], sixb: LoadedSixb): boolean {
|
|
27
|
+
return slot.get(sixb).scope === "process"
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ProcessLocalProvider {
|
|
31
|
+
/** The config slot: `broker` or `queues`. */
|
|
32
|
+
readonly slot: string
|
|
33
|
+
/** The configured class, so an operator can find it in `sixb.config.ts`. */
|
|
34
|
+
readonly configured: string
|
|
35
|
+
readonly replacements: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The same detection as {@link assertShareableProviders}, without the refusal. */
|
|
39
|
+
export function findProcessLocalProviders(sixb: LoadedSixb): readonly ProcessLocalProvider[] {
|
|
40
|
+
return SLOTS.filter((slot) => isProcessLocal(slot, sixb)).map((slot) => ({
|
|
41
|
+
slot: slot.name,
|
|
42
|
+
configured: slot.get(sixb).constructor?.name ?? "a process-local provider",
|
|
43
|
+
replacements: slot.replacements,
|
|
44
|
+
}))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Refuses to start a production role whose broker or queues cannot cross a process boundary.
|
|
49
|
+
*
|
|
50
|
+
* Deployed unchanged, the scaffolded in-memory providers give each role a private set of lanes:
|
|
51
|
+
* the orchestrator enqueues, the worker polls an empty queue, and nothing reports a problem.
|
|
52
|
+
* Throws rather than warns, because an inert deployment is indistinguishable from an idle one.
|
|
53
|
+
*/
|
|
54
|
+
export function assertShareableProviders(sixb: LoadedSixb, role: ProductionRole): void {
|
|
55
|
+
if (!productionRoleFacts(role).onEventPlane) return
|
|
56
|
+
|
|
57
|
+
for (const offender of findProcessLocalProviders(sixb)) {
|
|
58
|
+
throw new SixbCliError(
|
|
59
|
+
`[SixbCLI] \`sixb ${role}\` requires a ${offender.slot} provider that can be shared across ` +
|
|
60
|
+
`processes, but this project configures ${offender.configured}, which only works inside ` +
|
|
61
|
+
`one process. Jobs and events published here would be invisible to every other role.`,
|
|
62
|
+
{
|
|
63
|
+
remediation: `Use ${offender.replacements}, or keep the in-memory provider for \`sixb dev\` only.`,
|
|
64
|
+
}
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { migrateStorage } from "@sixb/core"
|
|
2
|
+
import { SixbCliError } from "./errors"
|
|
3
|
+
import type { LoadedSixb } from "./loadSixb"
|
|
4
|
+
import type { StorageSchemaRole } from "./production-roles"
|
|
5
|
+
|
|
6
|
+
/** Set to `1` to start a role without bringing the storage schema up to date. */
|
|
7
|
+
const SKIP_ENV_VAR = "SIXB_SKIP_MIGRATION"
|
|
8
|
+
|
|
9
|
+
export interface RoleStorageMigrationOptions {
|
|
10
|
+
readonly role: StorageSchemaRole
|
|
11
|
+
/** `--no-migrate`. Combines with `SIXB_SKIP_MIGRATION=1`; either one opts out. */
|
|
12
|
+
readonly noMigrate?: boolean
|
|
13
|
+
readonly env?: Record<string, string | undefined>
|
|
14
|
+
/**
|
|
15
|
+
* Called once, only when a migration is about to run. A schema change can take minutes, and
|
|
16
|
+
* a process that looks stuck is a process an operator restarts.
|
|
17
|
+
*/
|
|
18
|
+
readonly onStart?: () => void
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type RoleStorageMigrationOutcome =
|
|
22
|
+
/** Steps were applied; `applied` names them. */
|
|
23
|
+
| "migrated"
|
|
24
|
+
/** The schema was already at the latest version. */
|
|
25
|
+
| "current"
|
|
26
|
+
/** The configured storage exposes no migrators, so there is no schema to migrate. */
|
|
27
|
+
| "skipped"
|
|
28
|
+
/** The operator opted out with `--no-migrate` or `SIXB_SKIP_MIGRATION=1`. */
|
|
29
|
+
| "disabled"
|
|
30
|
+
|
|
31
|
+
export interface RoleStorageMigration {
|
|
32
|
+
readonly outcome: RoleStorageMigrationOutcome
|
|
33
|
+
/** Migration step ids that this process applied, in order. */
|
|
34
|
+
readonly applied: readonly string[]
|
|
35
|
+
/** One line for the role's startup panel, naming what ran, or `null` when nothing did. */
|
|
36
|
+
readonly summary: string | null
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Brings the storage schema up to date before a production role starts serving, so a forgotten
|
|
41
|
+
* `sixb db migrate` cannot surface as a missing column on the first request.
|
|
42
|
+
*
|
|
43
|
+
* Concurrent replicas are handled below this call: Postgres serializes migrators on a session
|
|
44
|
+
* advisory lock, and every adapter refuses a history it does not recognize, so late replicas
|
|
45
|
+
* no-op instead of racing. SQLite is the exception, and its error is translated rather than
|
|
46
|
+
* passed through raw.
|
|
47
|
+
*/
|
|
48
|
+
export async function migrateStorageForRole(
|
|
49
|
+
sixb: LoadedSixb,
|
|
50
|
+
options: RoleStorageMigrationOptions
|
|
51
|
+
): Promise<RoleStorageMigration> {
|
|
52
|
+
const { role } = options
|
|
53
|
+
const env = options.env ?? process.env
|
|
54
|
+
const skippedByEnv = env[SKIP_ENV_VAR] === "1"
|
|
55
|
+
|
|
56
|
+
// An opt-out says so out loud. Silence here is what made the old missing-migration
|
|
57
|
+
// failure so hard to place, and this is the path an operator chose deliberately.
|
|
58
|
+
if (options.noMigrate || skippedByEnv) {
|
|
59
|
+
const source = options.noMigrate ? "--no-migrate" : `${SKIP_ENV_VAR}=1`
|
|
60
|
+
return {
|
|
61
|
+
outcome: "disabled",
|
|
62
|
+
applied: [],
|
|
63
|
+
summary: `migration skipped (${source}); run \`sixb db migrate\` separately`,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
options.onStart?.()
|
|
68
|
+
const result = await runMigration(sixb, role)
|
|
69
|
+
|
|
70
|
+
if (result.status === "skipped") {
|
|
71
|
+
return { outcome: "skipped", applied: [], summary: null }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const applied = result.reports.flatMap((report) => report.applied)
|
|
75
|
+
|
|
76
|
+
if (applied.length === 0) {
|
|
77
|
+
return { outcome: "current", applied, summary: "schema up to date" }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
outcome: "migrated",
|
|
82
|
+
applied,
|
|
83
|
+
summary: `migrated: ${applied.join(", ")}`,
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function runMigration(sixb: LoadedSixb, role: StorageSchemaRole) {
|
|
88
|
+
try {
|
|
89
|
+
return await migrateStorage(sixb.storage)
|
|
90
|
+
} catch (error) {
|
|
91
|
+
throw isBusyDatabaseError(error) ? concurrentMigrationError(error, role) : error
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* SQLite has no cross-process migration lock, so two roles starting at the same moment
|
|
97
|
+
* on the same file collide. The raw `SQLITE_BUSY` says nothing about why or what to do,
|
|
98
|
+
* and it is the one concurrency case the layers below cannot serialize for us.
|
|
99
|
+
*/
|
|
100
|
+
function concurrentMigrationError(error: unknown, role: StorageSchemaRole): Error {
|
|
101
|
+
const detail = error instanceof Error ? error.message : String(error)
|
|
102
|
+
|
|
103
|
+
return new SixbCliError(
|
|
104
|
+
`[SixbCLI] \`sixb ${role}\` could not migrate storage because the database was locked ` +
|
|
105
|
+
`by another process: ${detail}. SQLite has no cross-process migration lock, so roles ` +
|
|
106
|
+
`starting together on the same file collide.`,
|
|
107
|
+
{
|
|
108
|
+
cause: error,
|
|
109
|
+
remediation:
|
|
110
|
+
"Run `sixb db migrate` once before starting the roles and start them with " +
|
|
111
|
+
"`--no-migrate`, or move to a storage provider that serializes migrations across " +
|
|
112
|
+
"processes.",
|
|
113
|
+
}
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function isBusyDatabaseError(error: unknown): boolean {
|
|
118
|
+
const message = error instanceof Error ? `${error.message} ${String(error.cause ?? "")}` : ""
|
|
119
|
+
return /SQLITE_BUSY|database is locked|database table is locked/i.test(message)
|
|
120
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { dirname, resolve } from "node:path"
|
|
2
|
+
import { generateOntologyTypeManifest } from "@sixb/core/internal/bootstrap"
|
|
3
|
+
|
|
4
|
+
export interface GenerateProjectTypesOptions {
|
|
5
|
+
readonly entry?: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export async function generateProjectTypes(options: GenerateProjectTypesOptions = {}) {
|
|
9
|
+
const entry = resolve(options.entry ?? "sixb.config.ts")
|
|
10
|
+
const projectRoot = dirname(entry)
|
|
11
|
+
return generateOntologyTypeManifest({ projectRoot })
|
|
12
|
+
}
|