-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.ts
More file actions
579 lines (521 loc) · 23.9 KB
/
Copy pathschema.ts
File metadata and controls
579 lines (521 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import { homedir } from 'node:os'
import { isAbsolute, join } from 'node:path'
import { z } from 'zod'
import { KubernetesEnvironmentConfigSchema } from '../environments/connection-registry.js'
// The five workflow-state roles the factory drives an issue through. Each is
// configured either by name (config.linear.states.<role>, resolved to a
// workspace UUID at startup) or by explicit UUID (config.stateIds.<role>).
export const FACTORY_STATE_ROLES = [
'readyForAgent',
'agentImplementing',
'inPlanning',
'done',
'humanReview',
] as const
export type FactoryStateRole = (typeof FACTORY_STATE_ROLES)[number]
// A mapping from factory roles to a team's workflow-state NAMES (e.g.
// readyForAgent -> "Ready for Agent", or "To Do" for a team that names it
// differently). All optional: unset roles fall back to global/explicit config.
const linearRoleNamesSchema = z.object({
readyForAgent: z.string().optional(),
agentImplementing: z.string().optional(),
inPlanning: z.string().optional(),
done: z.string().optional(),
humanReview: z.string().optional(),
}).default({})
const subscriptionSchema = z.object({
teams: z.array(z.string()).default([]),
projects: z.array(z.string()).default([]),
labels: z.array(z.string()).default([]),
assignees: z.array(z.string()).default([]),
}).default({})
const liveSubscriptionSchema = z.object({
transport: z.enum(['subscribe-and-poll', 'subscribe', 'poll']).default('subscribe-and-poll'),
pollIntervalMs: z.number().int().min(50).default(5_000),
eventLimit: z.number().int().min(1).max(1_000).default(1_000),
replaySkewMarginMs: z.number().int().min(0).default(60_000),
}).default({})
const dispatchSchema = z.object({
errorCooldownMs: z.number().int().min(0).default(60_000),
maxAttempts: z.number().int().min(1).max(5).default(2),
}).default({})
const loopSchema = z.object({
maxIterations: z.number().int().min(1).max(5).default(3),
maxConsecutiveFailures: z.number().int().min(1).max(5).default(3),
heartbeatPath: z.string().min(1).default('/tmp/factory-run/factory-loop-heartbeat.json'),
registryPath: z.string().min(1).default('/tmp/factory-run/factory-loop-registry.json'),
heartbeatStaleMs: z.number().int().min(1_000).default(60_000),
}).default({})
const triageSchema = z.object({
maxImplementers: z.number().int().min(1).max(6).default(2),
}).default({})
const workspaceReposSchema = z.object({
// Compact, single-source repo config. Most setups only need these: `names`
// is the label/repo list, and byLabel + clonePaths + subscription.labels are
// derived from them at parse time (see the transform below).
// byLabel[name] = overrides[name] ?? `${org}/${name}`
// clonePaths[repo] = `${cloneRoot}/${repoName}`
org: z.string().optional(),
names: z.array(z.string()).optional(),
overrides: z.record(z.string(), z.string()).default({}),
// Explicit forms remain supported as an escape hatch and are merged over the
// derived maps (explicit entries win). byLabel is optional now that it can be
// derived from `names`.
byLabel: z.record(z.string(), z.string()).default({}),
byProject: z.record(z.string(), z.string()).default({}),
keywordRules: z.array(z.object({ pattern: z.string(), repo: z.string() })).default([]),
default: z.string().optional(),
// Legacy/node-local repo checkout inputs accepted by the composed schema.
// WorkspaceConfig strips them; FactoryConfig uses them to preserve #369.
cloneRoot: z.string().optional(),
clonePaths: z.record(z.string(), z.string()).default({}),
})
const modelsSchema = z.object({
implementer: z.string().optional(),
reviewer: z.string().optional(),
triage: z.string().optional(),
// The PR babysitter defaults to sonnet — it shepherds an already-open PR
// (CI/conflicts/comments) rather than authoring from scratch, so the
// mid-tier model is the deliberate default rather than the implementer/
// reviewer's unset (inherit) behavior.
babysitter: z.string().default('sonnet'),
}).default({})
// Which agent CLI backs each spawn role. `models.*` picks the model; this picks
// the CLI. Restricted to the two spawn capabilities actually wired into
// capabilityCli (internal-fleet-client.ts) — spawn:opencode / spawn:gemini would
// resolve to an undefined CLI and are intentionally excluded until mapped.
// Defaults preserve today's behavior (codex implements, claude reviews/babysits)
// so existing configs are unaffected unless a role is set explicitly. The map is
// named agentCapabilities rather than `capabilities` because the node config
// already owns a top-level `capabilities` (advertised node capability list).
const spawnCapabilitySchema = z.enum(['spawn:codex', 'spawn:claude'])
const agentCapabilitiesSchema = z.object({
implementer: spawnCapabilitySchema.default('spawn:codex'),
reviewer: spawnCapabilitySchema.default('spawn:claude'),
babysitter: spawnCapabilitySchema.default('spawn:claude'),
}).default({})
const slackSchema = z.object({
channel: z.string(),
style: z.literal('threaded-summarized').default('threaded-summarized'),
botUserId: z.string().default('U0B2596R7EZ'),
// Slack user IDs to mention when an agent needs a human decision. Keeping
// this explicit avoids guessing that a Linear assignee name is also a Slack
// identity, while still making parked questions immediately actionable.
stakeholderUserIds: z.array(z.string().min(1)).default([]),
staleAfterMs: z.number().int().min(1_000).default(10 * 60_000),
conversationCoalesceMs: z.number().int().min(0).max(60_000).default(750),
}).optional()
const babysitterSchema = z.object({
enabled: z.boolean().default(false),
}).default({})
const reportingSchema = z.object({
enabled: z.boolean().default(true),
instanceName: z.string().trim().min(1).max(256).optional(),
outboxPath: z.string().min(1).optional(),
batchSize: z.number().int().min(1).max(100).default(100),
requestTimeoutMs: z.number().int().min(100).max(60_000).default(15_000),
}).default({})
const previewServiceSchema = z.object({
/** Local HTTP port the repository's development server listens on. */
port: z.number().int().min(1).max(65_535),
/** Consecutive node-local ports Factory may allocate for concurrent issues. */
portSpan: z.number().int().min(1).max(1_000).optional(),
/** Optional stable tailnet HTTPS port; otherwise Factory allocates one. */
httpsPort: z.number().int().min(1).max(65_535).optional(),
/** Foreground command Factory supervises for the issue-lifetime preview. */
startCommand: z.string().trim().min(1),
}).superRefine((service, ctx) => {
if (service.port + (service.portSpan ?? 100) - 1 > 65_535) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['portSpan'],
message: 'preview service port range must end at or below 65535',
})
}
})
// Tailscale Serve is deliberately the only initial provider. Unlike Funnel,
// Serve is tailnet-only and keeps the tailnet's grants/ACLs in the request path.
// Making the access mode a literal prevents a config typo from silently
// publishing an unauthenticated URL to Slack or GitHub.
const previewSchema = z.object({
provider: z.literal('tailscale-serve').default('tailscale-serve'),
access: z.literal('tailnet').default('tailnet'),
services: z.record(z.string(), previewServiceSchema).default({}),
tailscaleBinary: z.string().trim().min(1).default('tailscale'),
registryPath: z.string().min(1).default('~/.factory/tailscale-previews.json'),
httpsPortRange: z.tuple([
z.number().int().min(1).max(65_535),
z.number().int().min(1).max(65_535),
]).default([10_000, 10_999]),
}).superRefine((preview, ctx) => {
if (preview.httpsPortRange[0] > preview.httpsPortRange[1]) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['httpsPortRange'],
message: 'preview.httpsPortRange start must be less than or equal to end',
})
}
}).optional()
const githubSchema = z.object({
// Controls the credential identity used when Factory creates pull requests.
// `auto` preserves the compatibility behavior: prefer the connected
// workspace GitHub App, then use the operator's local `gh` authentication
// when the app write path is unavailable.
identity: z.enum(['app', 'user', 'auto']).default('auto'),
}).default({})
const verificationSchema = z.object({
/** Default-on: an auto-merge must have a green live-stack verification verdict. */
enabled: z.boolean().default(true),
descriptorPath: z.string().trim().min(1).default('.factory/verification-stack.yaml'),
maxConcurrentEnvironments: z.number().int().min(1).max(20).default(2),
maxRunTimeoutMs: z.number().int().min(1_000).max(2 * 60 * 60_000).default(30 * 60_000),
maxEnvironmentTtlMs: z.number().int().min(1_000).max(24 * 60 * 60_000).default(60 * 60_000),
maxTeardownTimeoutMs: z.number().int().min(1_000).max(30 * 60_000).default(5 * 60_000),
}).strict().default({})
// The factory owns its workflow-state NAME conventions; consumers (e.g. pear)
// don't hand-configure them. These names let the factory resolve a role from a
// synced record that carries state.name but no state.id (sparse-sync fallback).
// A workspace that names states differently can override via config.
const DEFAULT_LINEAR_STATE_NAMES = {
readyForAgent: 'Ready for Agent',
agentImplementing: 'Agent Implementing',
done: 'Done',
inPlanning: 'In Planning',
humanReview: 'In Human Review',
}
const linearSchema = z.object({
states: linearRoleNamesSchema.default(DEFAULT_LINEAR_STATE_NAMES),
statesByTeam: z.record(z.string(), linearRoleNamesSchema).default({}),
teamIds: z.record(z.string(), z.string()).default({}),
}).default({ states: DEFAULT_LINEAR_STATE_NAMES, statesByTeam: {}, teamIds: {} })
const stateIdsSchema = z.object({
readyForAgent: z.string().optional(),
agentImplementing: z.string().optional(),
done: z.string().optional(),
inPlanning: z.string().optional(),
humanReview: z.string().optional(),
}).default({})
const safetySchema = z.object({
requireTitlePrefix: z.string().min(1).default('[factory-e2e]'),
requireLabel: z.string().default('factory'),
requireTeamKey: z.string().min(1).default('AR'),
}).default({})
const environmentsSchema = z.object({
kubernetes: KubernetesEnvironmentConfigSchema.optional(),
}).strict().default({})
const WorkspaceConfigObjectSchema = z.object({
// Optional. When omitted, the CLI derives the workspace from the cloud session
// via `resolveActiveWorkspace()` (returns the active `relayfileWorkspaceId`),
// falling back to the SDK's built-in default. Set it only to pin a non-active
// workspace. See resolveFactoryWorkspace() in relayfile-cloud-mount-client.ts.
workspaceId: z.string().optional(),
// Optional exact root of this workspace's single Relayfile mirror. When
// omitted Factory reads Relayfile's existing registration. This is a
// workspace-scoped escape hatch, never a request to re-home per checkout.
localMountRoot: z.string()
.trim()
.min(1)
.refine(isAbsolute, 'localMountRoot must be an absolute path')
.optional(),
subscription: subscriptionSchema,
liveSubscription: liveSubscriptionSchema,
dispatch: dispatchSchema,
loop: loopSchema,
triage: triageSchema,
repos: workspaceReposSchema,
batchSize: z.number().int().min(1).max(5).default(5),
models: modelsSchema,
agentCapabilities: agentCapabilitiesSchema,
slack: slackSchema,
// Linear remains the default whenever its issue sub-root is connected. When
// omitted, the orchestrator probes that sub-root once and falls back to
// GitHub-native issue lifecycle handling only when Linear is absent.
issueSource: z.enum(['linear', 'github']).optional(),
mergePolicy: z.enum(['never', 'on-green-with-review']).default('never'),
// Opt-in PR babysitter. When enabled, a sonnet agent is spawned once the
// implementer's PR opens (webhook-driven, see the orchestrator) and shepherds
// it — addressing review comments, resolving conflicts, and fixing CI — until
// it is green, then transitions the issue to the `human-review` terminal state
// instead of jumping straight to `done`. Default off preserves the legacy
// PR-open -> done behavior.
babysitter: babysitterSchema,
// Authenticated run progress is a Cloud product feature, not anonymous
// analytics. It defaults on for real CLI sessions and remains no-op when no
// Cloud account is available; delivery failure never changes orchestration.
reporting: reportingSchema,
preview: previewSchema,
github: githubSchema,
verification: verificationSchema,
// Which Linear state an issue lands in once the agents finish and the PR is
// open. `human-review` parks it for operator review (Done is reserved for the
// actual merge); `done` is the legacy behavior. Only honored when the
// `humanReview` role resolves to a state — otherwise it falls back to `done`.
terminalState: z.enum(['done', 'human-review']).default('human-review'),
// Dynamic, workspace-agnostic Linear configuration. Nothing about state names
// or UUIDs is hardcoded — customers map the factory's semantic roles to
// whatever their teams call those states, and the names are resolved to UUIDs
// at startup against /linear/states (see resolveFactoryStates).
//
// `states` is the workspace-wide default mapping; `statesByTeam.<TEAM>`
// overrides individual roles for teams that name their states differently
// (resolution per issue uses the issue's team, falling back to `states`).
linear: linearSchema,
// Explicit workflow-state UUIDs. Lowest-precedence fallback / single-team
// escape hatch for setups that prefer pinning ids over name resolution; any
// role resolved by name (per-team or global) takes precedence. Populated in
// place once resolution runs, so the orchestrator always sees concrete UUIDs.
stateIds: stateIdsSchema,
safety: safetySchema,
environments: environmentsSchema,
})
const NodeConfigObjectSchema = z.object({
workspaceId: z.string().optional(),
capabilities: z.array(z.string()).default([]),
cloneRoot: z.string().optional(),
clonePaths: z.record(z.string(), z.string()).default({}),
dryRun: z.boolean().default(false),
factoryLoopHeartbeatPath: z.string().min(1).optional(),
factoryLoopRegistryPath: z.string().min(1).optional(),
preview: previewSchema,
})
const FactoryConfigObjectSchema = WorkspaceConfigObjectSchema.merge(NodeConfigObjectSchema)
export const WorkspaceConfigSchema = WorkspaceConfigObjectSchema.transform((cfg) => normalizeWorkspaceConfig(cfg))
export const NodeConfigSchema = NodeConfigObjectSchema.transform((cfg) => normalizeNodeConfig(cfg))
export const FactoryConfigSchema = FactoryConfigObjectSchema.transform((cfg) => normalizeFactoryConfig(cfg))
function normalizeWorkspaceConfig(cfg: z.infer<typeof WorkspaceConfigObjectSchema>) {
const resolved = resolveRepos(cfg.repos, cfg.repos.cloneRoot)
const labels = resolveSubscriptionLabels(cfg.subscription.labels, cfg.repos.names ?? [])
const {
cloneRoot: _legacyCloneRoot,
clonePaths: _legacyClonePaths,
...repos
} = cfg.repos
return {
...cfg,
subscription: { ...cfg.subscription, labels },
preview: normalizePreviewConfig(cfg.preview),
repos: {
...repos,
byLabel: resolved.byLabel,
byProject: resolved.byProject,
keywordRules: resolved.keywordRules,
...(resolved.defaultRepo !== undefined ? { default: resolved.defaultRepo } : {}),
},
}
}
function normalizeNodeConfig(cfg: z.infer<typeof NodeConfigObjectSchema>) {
const cloneRoot = cfg.cloneRoot === undefined
? undefined
: expandLeadingTilde(cfg.cloneRoot, 'cloneRoot')
return {
...cfg,
cloneRoot,
clonePaths: expandClonePaths(cfg.clonePaths),
factoryLoopHeartbeatPath: cfg.factoryLoopHeartbeatPath,
factoryLoopRegistryPath: cfg.factoryLoopRegistryPath,
preview: normalizePreviewConfig(cfg.preview),
}
}
function normalizeFactoryConfig(cfg: z.infer<typeof FactoryConfigObjectSchema>) {
const topLevelCloneRoot = cfg.cloneRoot === undefined
? undefined
: expandLeadingTilde(cfg.cloneRoot, 'cloneRoot')
const legacyCloneRoot = cfg.repos.cloneRoot === undefined
? undefined
: expandLeadingTilde(cfg.repos.cloneRoot, 'repos.cloneRoot')
const cloneRoot = topLevelCloneRoot ?? legacyCloneRoot
const explicitClonePaths = {
...expandClonePaths(cfg.repos.clonePaths, 'repos.clonePaths'),
...expandClonePaths(cfg.clonePaths),
}
const resolved = resolveRepos(cfg.repos, cloneRoot, explicitClonePaths)
const labels = resolveSubscriptionLabels(cfg.subscription.labels, cfg.repos.names ?? [])
const heartbeatPath = cfg.factoryLoopHeartbeatPath ?? cfg.loop.heartbeatPath
const registryPath = cfg.factoryLoopRegistryPath ?? cfg.loop.registryPath
return {
...cfg,
cloneRoot,
clonePaths: resolved.clonePaths,
factoryLoopHeartbeatPath: heartbeatPath,
factoryLoopRegistryPath: registryPath,
subscription: { ...cfg.subscription, labels },
loop: {
...cfg.loop,
heartbeatPath,
registryPath,
},
preview: normalizePreviewConfig(cfg.preview),
repos: {
...(cfg.repos.org !== undefined ? { org: cfg.repos.org } : {}),
...(cfg.repos.names !== undefined ? { names: cfg.repos.names } : {}),
byLabel: resolved.byLabel,
byProject: resolved.byProject,
keywordRules: resolved.keywordRules,
clonePaths: resolved.clonePaths,
...(resolved.defaultRepo !== undefined ? { default: resolved.defaultRepo } : {}),
},
}
}
function resolveRepos(
repos: z.infer<typeof workspaceReposSchema>,
cloneRoot?: string,
explicitClonePaths: Record<string, string> = repos.clonePaths,
) {
const { org, names, overrides, byLabel, byProject, keywordRules, default: defaultRepo } = repos
const repoNames = names ?? []
// Derive byLabel from `names` (label === repo name): overrides[name] wins,
// else `${org}/${name}` when an org is set, else the bare name. Explicit
// byLabel entries are merged last so they always win.
const derivedByLabel: Record<string, string> = {}
for (const name of repoNames) {
derivedByLabel[name] = overrides[name] ?? (org ? `${org}/${name}` : name)
}
const resolvedByLabel = { ...derivedByLabel, ...byLabel }
// Derive clonePaths as `${cloneRoot}/${repoName}` for every routed repo.
// Explicit clonePaths entries win.
const derivedClonePaths: Record<string, string> = {}
if (cloneRoot) {
const root = expandLeadingTilde(cloneRoot, 'cloneRoot').replace(/\/+$/u, '')
for (const repo of Object.values(resolvedByLabel)) {
const repoName = repo.includes('/') ? repo.slice(repo.lastIndexOf('/') + 1) : repo
derivedClonePaths[repo] = `${root}/${repoName}`
}
}
const resolvedClonePaths = {
...derivedClonePaths,
...expandClonePaths(explicitClonePaths),
}
return {
byLabel: resolvedByLabel,
byProject,
keywordRules,
clonePaths: resolvedClonePaths,
defaultRepo,
}
}
function expandClonePaths(
clonePaths: Record<string, string>,
field = 'clonePaths',
): Record<string, string> {
return Object.fromEntries(Object.entries(clonePaths).map(([repo, clonePath]) => [
repo,
expandLeadingTilde(clonePath, `${field}[${JSON.stringify(repo)}]`),
]))
}
/** Expand the shell-like home shorthand that Node's filesystem APIs do not. */
export function expandLeadingTilde(value: string, field = 'path'): string {
if (value === '~') return homedir()
if (value.startsWith('~/')) return join(homedir(), value.slice(2))
if (value.startsWith('~')) {
throw new Error(`${field} does not support ~user expansion; use ~ or ~/ instead`)
}
return value
}
function resolveSubscriptionLabels(labels: string[], repoNames: string[]): string[] {
return labels.length > 0 ? labels : repoNames
}
export interface LoadedFactoryConfig {
workspaceConfig: WorkspaceConfig
nodeConfig: NodeConfig
factoryConfig: FactoryConfig
}
export function loadFactoryConfig(input: unknown): LoadedFactoryConfig {
const record = asConfigRecord(input)
const hasSplit = Object.prototype.hasOwnProperty.call(record, 'workspaceConfig') ||
Object.prototype.hasOwnProperty.call(record, 'nodeConfig')
if (hasSplit) {
if (!Object.prototype.hasOwnProperty.call(record, 'workspaceConfig')) {
throw new Error('split factory config requires workspaceConfig')
}
if (!Object.prototype.hasOwnProperty.call(record, 'nodeConfig')) {
throw new Error('split factory config requires nodeConfig')
}
return normalizeLoadedConfig(combineSplitConfigInput(record.workspaceConfig, record.nodeConfig))
}
return normalizeLoadedConfig(record.factoryConfig ?? input)
}
function normalizeLoadedConfig(input: unknown): LoadedFactoryConfig {
const factoryConfig = FactoryConfigSchema.parse(input)
const workspaceConfig = WorkspaceConfigSchema.parse(factoryConfig)
const nodeConfig = NodeConfigSchema.parse({
workspaceId: factoryConfig.workspaceId,
capabilities: factoryConfig.capabilities,
cloneRoot: factoryConfig.cloneRoot,
clonePaths: factoryConfig.clonePaths,
dryRun: factoryConfig.dryRun,
factoryLoopHeartbeatPath: factoryConfig.loop.heartbeatPath,
factoryLoopRegistryPath: factoryConfig.loop.registryPath,
preview: factoryConfig.preview,
})
return { workspaceConfig, nodeConfig, factoryConfig }
}
function combineSplitConfigInput(workspaceInput: unknown, nodeInput: unknown): Record<string, unknown> {
const workspace = asConfigRecord(workspaceInput)
const node = asConfigRecord(nodeInput)
const workspaceRepos = asOptionalConfigRecord(workspace.repos)
assertCompatibleWorkspaceIds(workspace.workspaceId, node.workspaceId)
// Validate tilde syntax in both split halves before node-local values take
// precedence, so an overridden ~user path is never silently accepted.
validateClonePathSyntax(workspaceRepos, 'workspaceConfig.repos')
validateClonePathSyntax(node, 'nodeConfig')
const workspacePreview = asOptionalConfigRecord(workspace.preview)
const nodePreview = asOptionalConfigRecord(node.preview)
const hasPreview = workspace.preview !== undefined || node.preview !== undefined
return {
...workspace,
...node,
...(hasPreview ? {
preview: {
...workspacePreview,
...nodePreview,
services: {
...asOptionalConfigRecord(workspacePreview.services),
...asOptionalConfigRecord(nodePreview.services),
},
},
} : {}),
repos: {
...workspaceRepos,
cloneRoot: node.cloneRoot ?? workspaceRepos.cloneRoot,
clonePaths: node.clonePaths ?? workspaceRepos.clonePaths,
},
}
}
function normalizePreviewConfig<T extends z.infer<typeof previewSchema>>(preview: T): T {
if (!preview) return preview
return {
...preview,
registryPath: expandLeadingTilde(preview.registryPath, 'preview.registryPath'),
} as T
}
function validateClonePathSyntax(input: Record<string, unknown>, field: string): void {
if (typeof input.cloneRoot === 'string') expandLeadingTilde(input.cloneRoot, `${field}.cloneRoot`)
const clonePaths = asOptionalConfigRecord(input.clonePaths)
for (const [repo, clonePath] of Object.entries(clonePaths)) {
if (typeof clonePath === 'string') {
expandLeadingTilde(clonePath, `${field}.clonePaths[${JSON.stringify(repo)}]`)
}
}
}
function assertCompatibleWorkspaceIds(workspaceId: unknown, nodeWorkspaceId: unknown): void {
if (
typeof workspaceId === 'string' &&
typeof nodeWorkspaceId === 'string' &&
workspaceId !== nodeWorkspaceId
) {
throw new Error(`split factory config workspaceId mismatch: workspaceConfig=${workspaceId} nodeConfig=${nodeWorkspaceId}`)
}
}
function asConfigRecord(value: unknown): Record<string, unknown> {
if (value && typeof value === 'object' && !Array.isArray(value)) return value as Record<string, unknown>
throw new Error('factory config must be a JSON object')
}
function asOptionalConfigRecord(value: unknown): Record<string, unknown> {
if (value === undefined) return {}
return asConfigRecord(value)
}
export type WorkspaceConfig = z.infer<typeof WorkspaceConfigSchema>
export type NodeConfig = z.infer<typeof NodeConfigSchema>
export type FactoryConfig = z.infer<typeof FactoryConfigSchema>
export type PreviewConfig = NonNullable<FactoryConfig['preview']>
export type PreviewServiceConfig = PreviewConfig['services'][string]