principles-disciple 1.7.6 → 1.7.8

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 (106) hide show
  1. package/dist/commands/context.js +5 -15
  2. package/dist/commands/evolution-status.js +2 -9
  3. package/dist/commands/export.js +61 -8
  4. package/dist/commands/nocturnal-review.d.ts +24 -0
  5. package/dist/commands/nocturnal-review.js +265 -0
  6. package/dist/commands/nocturnal-rollout.d.ts +27 -0
  7. package/dist/commands/nocturnal-rollout.js +671 -0
  8. package/dist/commands/nocturnal-train.d.ts +25 -0
  9. package/dist/commands/nocturnal-train.js +919 -0
  10. package/dist/commands/pain.js +8 -21
  11. package/dist/constants/tools.d.ts +2 -2
  12. package/dist/constants/tools.js +1 -1
  13. package/dist/core/adaptive-thresholds.d.ts +186 -0
  14. package/dist/core/adaptive-thresholds.js +300 -0
  15. package/dist/core/config.d.ts +2 -38
  16. package/dist/core/config.js +6 -61
  17. package/dist/core/event-log.d.ts +1 -2
  18. package/dist/core/event-log.js +0 -3
  19. package/dist/core/evolution-engine.js +1 -21
  20. package/dist/core/evolution-reducer.d.ts +7 -1
  21. package/dist/core/evolution-reducer.js +56 -4
  22. package/dist/core/evolution-types.d.ts +61 -9
  23. package/dist/core/evolution-types.js +31 -9
  24. package/dist/core/external-training-contract.d.ts +276 -0
  25. package/dist/core/external-training-contract.js +269 -0
  26. package/dist/core/local-worker-routing.d.ts +175 -0
  27. package/dist/core/local-worker-routing.js +525 -0
  28. package/dist/core/model-deployment-registry.d.ts +218 -0
  29. package/dist/core/model-deployment-registry.js +503 -0
  30. package/dist/core/model-training-registry.d.ts +295 -0
  31. package/dist/core/model-training-registry.js +475 -0
  32. package/dist/core/nocturnal-arbiter.d.ts +159 -0
  33. package/dist/core/nocturnal-arbiter.js +534 -0
  34. package/dist/core/nocturnal-candidate-scoring.d.ts +137 -0
  35. package/dist/core/nocturnal-candidate-scoring.js +266 -0
  36. package/dist/core/nocturnal-compliance.d.ts +175 -0
  37. package/dist/core/nocturnal-compliance.js +824 -0
  38. package/dist/core/nocturnal-dataset.d.ts +224 -0
  39. package/dist/core/nocturnal-dataset.js +443 -0
  40. package/dist/core/nocturnal-executability.d.ts +85 -0
  41. package/dist/core/nocturnal-executability.js +331 -0
  42. package/dist/core/nocturnal-export.d.ts +124 -0
  43. package/dist/core/nocturnal-export.js +275 -0
  44. package/dist/core/nocturnal-paths.d.ts +124 -0
  45. package/dist/core/nocturnal-paths.js +214 -0
  46. package/dist/core/nocturnal-trajectory-extractor.d.ts +242 -0
  47. package/dist/core/nocturnal-trajectory-extractor.js +307 -0
  48. package/dist/core/nocturnal-trinity.d.ts +311 -0
  49. package/dist/core/nocturnal-trinity.js +880 -0
  50. package/dist/core/paths.d.ts +6 -0
  51. package/dist/core/paths.js +6 -0
  52. package/dist/core/principle-training-state.d.ts +121 -0
  53. package/dist/core/principle-training-state.js +321 -0
  54. package/dist/core/promotion-gate.d.ts +238 -0
  55. package/dist/core/promotion-gate.js +529 -0
  56. package/dist/core/session-tracker.d.ts +10 -0
  57. package/dist/core/session-tracker.js +14 -0
  58. package/dist/core/shadow-observation-registry.d.ts +217 -0
  59. package/dist/core/shadow-observation-registry.js +308 -0
  60. package/dist/core/training-program.d.ts +233 -0
  61. package/dist/core/training-program.js +433 -0
  62. package/dist/core/trajectory.d.ts +95 -1
  63. package/dist/core/trajectory.js +220 -6
  64. package/dist/core/workspace-context.d.ts +0 -6
  65. package/dist/core/workspace-context.js +0 -12
  66. package/dist/hooks/bash-risk.d.ts +6 -6
  67. package/dist/hooks/bash-risk.js +8 -8
  68. package/dist/hooks/gate-block-helper.js +1 -1
  69. package/dist/hooks/gate.d.ts +1 -1
  70. package/dist/hooks/gate.js +2 -2
  71. package/dist/hooks/gfi-gate.d.ts +3 -3
  72. package/dist/hooks/gfi-gate.js +15 -14
  73. package/dist/hooks/pain.js +6 -9
  74. package/dist/hooks/progressive-trust-gate.d.ts +21 -49
  75. package/dist/hooks/progressive-trust-gate.js +51 -204
  76. package/dist/hooks/prompt.d.ts +11 -11
  77. package/dist/hooks/prompt.js +158 -72
  78. package/dist/hooks/subagent.js +43 -6
  79. package/dist/i18n/commands.js +8 -8
  80. package/dist/index.js +129 -28
  81. package/dist/service/evolution-worker.d.ts +42 -4
  82. package/dist/service/evolution-worker.js +321 -13
  83. package/dist/service/nocturnal-runtime.d.ts +183 -0
  84. package/dist/service/nocturnal-runtime.js +352 -0
  85. package/dist/service/nocturnal-service.d.ts +163 -0
  86. package/dist/service/nocturnal-service.js +787 -0
  87. package/dist/service/nocturnal-target-selector.d.ts +145 -0
  88. package/dist/service/nocturnal-target-selector.js +315 -0
  89. package/dist/service/phase3-input-filter.d.ts +2 -23
  90. package/dist/service/phase3-input-filter.js +3 -27
  91. package/dist/service/runtime-summary-service.d.ts +0 -10
  92. package/dist/service/runtime-summary-service.js +1 -54
  93. package/dist/tools/deep-reflect.js +2 -1
  94. package/dist/types/event-types.d.ts +2 -10
  95. package/dist/types/runtime-summary.d.ts +1 -8
  96. package/dist/types.d.ts +0 -3
  97. package/dist/types.js +0 -2
  98. package/openclaw.plugin.json +1 -1
  99. package/package.json +1 -1
  100. package/templates/langs/en/skills/pd-mentor/SKILL.md +5 -5
  101. package/templates/langs/zh/skills/pd-mentor/SKILL.md +5 -5
  102. package/templates/pain_settings.json +0 -6
  103. package/dist/commands/trust.d.ts +0 -4
  104. package/dist/commands/trust.js +0 -78
  105. package/dist/core/trust-engine.d.ts +0 -96
  106. package/dist/core/trust-engine.js +0 -286
@@ -0,0 +1,503 @@
1
+ /**
2
+ * Model Deployment Registry — Worker Profile → Checkpoint Binding & Routing Control
3
+ * ===============================================================================
4
+ *
5
+ * PURPOSE: Establish auditable, reversible bindings between worker profiles and
6
+ * trained model checkpoints so that routing decisions are code-governed and
7
+ * rollback-safe.
8
+ *
9
+ * ARCHITECTURE:
10
+ * - Registry file: {stateDir}/.state/nocturnal/deployment-registry.json
11
+ * - File locking on all write operations
12
+ * - Immutable deployment records — rollback uses previousCheckpointId
13
+ * - Tight integration with model-training-registry for checkpoint validation
14
+ *
15
+ * PROFILE CONSTRAINTS (Phase 5 only):
16
+ * - local-reader → must bind a checkpoint whose targetModelFamily is a "reader" family
17
+ * - local-editor → must bind a checkpoint whose targetModelFamily is an "editor" family
18
+ * - No other profiles are accepted
19
+ *
20
+ * BINDING RULES:
21
+ * - Only a deployable checkpoint can be bound
22
+ * - The checkpoint's targetModelFamily must satisfy the profile's family constraint
23
+ * - binding sets routingEnabled = false; enableRoutingForProfile() must be called explicitly
24
+ * - rollbackDeployment() returns to previousCheckpointId (if any)
25
+ *
26
+ * DESIGN CONSTRAINTS:
27
+ * - No actual task routing execution (Phase 5 only)
28
+ * - No automatic promotion or failover
29
+ * - Registry is append-only for deployments; rollback creates a new binding
30
+ */
31
+ import * as fs from 'fs';
32
+ import * as path from 'path';
33
+ import * as crypto from 'crypto';
34
+ import { withLock } from '../utils/file-lock.js';
35
+ import { getCheckpoint, isCheckpointDeployable, } from './model-training-registry.js';
36
+ import { getPromotionState } from './promotion-gate.js';
37
+ // ---------------------------------------------------------------------------
38
+ // Constants
39
+ // ---------------------------------------------------------------------------
40
+ const REGISTRY_FILE = '.state/nocturnal/deployment-registry.json';
41
+ /**
42
+ * The set of valid Phase 5 worker profile names.
43
+ */
44
+ export const SUPPORTED_PROFILES = ['local-reader', 'local-editor'];
45
+ // ---------------------------------------------------------------------------
46
+ // Profile–Family Constraint System
47
+ // ---------------------------------------------------------------------------
48
+ /**
49
+ * Known model family name prefixes/suffixes recognized as "reader" families.
50
+ * Any targetModelFamily string containing one of these tokens is considered a reader family.
51
+ *
52
+ * This is a first-iteration heuristic. Real systems may use explicit tag registries.
53
+ */
54
+ const READER_FAMILY_KEYWORDS = ['reader', 'read', 'claude-haiku', 'qwen-lite', 'phi-mini'];
55
+ /**
56
+ * Known model family name prefixes/suffixes recognized as "editor" families.
57
+ * Any targetModelFamily string containing one of these tokens is considered an editor family.
58
+ */
59
+ const EDITOR_FAMILY_KEYWORDS = ['editor', 'edit', 'code', 'claude-sonnet', 'gpt-4o-mini'];
60
+ /**
61
+ * Determine whether a target model family name qualifies as a "reader" family.
62
+ * Returns true if any known reader keyword appears in the family name (case-insensitive).
63
+ */
64
+ function isReaderFamily(targetModelFamily) {
65
+ const lower = targetModelFamily.toLowerCase();
66
+ return READER_FAMILY_KEYWORDS.some((kw) => lower.includes(kw));
67
+ }
68
+ /**
69
+ * Determine whether a target model family name qualifies as an "editor" family.
70
+ * Returns true if any known editor keyword appears in the family name (case-insensitive).
71
+ */
72
+ function isEditorFamily(targetModelFamily) {
73
+ const lower = targetModelFamily.toLowerCase();
74
+ return EDITOR_FAMILY_KEYWORDS.some((kw) => lower.includes(kw));
75
+ }
76
+ /**
77
+ * Validate that a given targetModelFamily satisfies a worker's family constraint.
78
+ *
79
+ * @param profile - The worker profile requesting the binding
80
+ * @param targetModelFamily - The checkpoint's target model family
81
+ * @throws Error if the family is incompatible with the profile
82
+ */
83
+ function validateProfileFamilyConstraint(profile, targetModelFamily) {
84
+ if (profile === 'local-reader') {
85
+ if (!isReaderFamily(targetModelFamily)) {
86
+ throw new Error(`Family constraint violated: profile "${profile}" requires a reader-family checkpoint ` +
87
+ `but checkpoint targets "${targetModelFamily}". ` +
88
+ `Reader families must contain one of: ${READER_FAMILY_KEYWORDS.join(', ')}. ` +
89
+ `If you are deploying a new model family, update the READER_FAMILY_KEYWORDS or ` +
90
+ `EDITOR_FAMILY_KEYWORDS constants in model-deployment-registry.ts.`);
91
+ }
92
+ }
93
+ else if (profile === 'local-editor') {
94
+ if (!isEditorFamily(targetModelFamily)) {
95
+ throw new Error(`Family constraint violated: profile "${profile}" requires an editor-family checkpoint ` +
96
+ `but checkpoint targets "${targetModelFamily}". ` +
97
+ `Editor families must contain one of: ${EDITOR_FAMILY_KEYWORDS.join(', ')}. ` +
98
+ `If you are deploying a new model family, update the EDITOR_FAMILY_KEYWORDS constant ` +
99
+ `in model-deployment-registry.ts.`);
100
+ }
101
+ }
102
+ }
103
+ // ---------------------------------------------------------------------------
104
+ // Registry Path
105
+ // ---------------------------------------------------------------------------
106
+ function getRegistryPath(stateDir) {
107
+ return path.join(stateDir, REGISTRY_FILE);
108
+ }
109
+ /**
110
+ * Ensure the registry directory exists.
111
+ */
112
+ function ensureRegistryDir(stateDir) {
113
+ const registryPath = getRegistryPath(stateDir);
114
+ const dir = path.dirname(registryPath);
115
+ if (!fs.existsSync(dir)) {
116
+ fs.mkdirSync(dir, { recursive: true });
117
+ }
118
+ }
119
+ // ---------------------------------------------------------------------------
120
+ // File Operations
121
+ // ---------------------------------------------------------------------------
122
+ /**
123
+ * Read the registry from disk. Returns empty registry if missing.
124
+ * Throws if the file exists but contains invalid JSON — fail-closed
125
+ * to prevent silent data loss when a corrupt registry would otherwise
126
+ * be overwritten on the next write.
127
+ */
128
+ function readRegistry(stateDir) {
129
+ const registryPath = getRegistryPath(stateDir);
130
+ if (!fs.existsSync(registryPath)) {
131
+ return { deployments: [] };
132
+ }
133
+ try {
134
+ const content = fs.readFileSync(registryPath, 'utf-8');
135
+ const registry = JSON.parse(content);
136
+ // Validate required structure — fail closed on malformed registry
137
+ if (!Array.isArray(registry.deployments)) {
138
+ throw new Error(`Corrupt deployment registry at "${registryPath}": missing or invalid "deployments" field`);
139
+ }
140
+ return registry;
141
+ }
142
+ catch (err) {
143
+ if (err instanceof SyntaxError || err instanceof Error) {
144
+ throw new Error(`Failed to read deployment registry from "${registryPath}": ${err.message}`);
145
+ }
146
+ throw err;
147
+ }
148
+ }
149
+ /**
150
+ * Write the registry to disk atomically.
151
+ * Caller must hold the registry lock.
152
+ */
153
+ function writeRegistry(stateDir, registry) {
154
+ ensureRegistryDir(stateDir);
155
+ const registryPath = getRegistryPath(stateDir);
156
+ const tmpPath = `${registryPath}.tmp`;
157
+ fs.writeFileSync(tmpPath, JSON.stringify(registry, null, 2), 'utf-8');
158
+ fs.renameSync(tmpPath, registryPath);
159
+ }
160
+ /**
161
+ * Execute a read-modify-write under an exclusive file lock.
162
+ */
163
+ function withDeploymentRegistryLock(stateDir, fn) {
164
+ const registryPath = getRegistryPath(stateDir);
165
+ return withLock(registryPath, () => {
166
+ const registry = readRegistry(stateDir);
167
+ return fn(registry);
168
+ });
169
+ }
170
+ // ---------------------------------------------------------------------------
171
+ // Profile Validation
172
+ // ---------------------------------------------------------------------------
173
+ /**
174
+ * Validate that a worker profile name is supported in Phase 5.
175
+ *
176
+ * @throws Error if the profile is not in SUPPORTED_PROFILES
177
+ */
178
+ export function assertSupportedProfile(profile) {
179
+ if (!SUPPORTED_PROFILES.includes(profile)) {
180
+ throw new Error(`Unsupported worker profile: "${profile}". ` +
181
+ `Phase 5 only supports: ${SUPPORTED_PROFILES.join(', ')}. ` +
182
+ `Do not add new profiles in Phase 5.`);
183
+ }
184
+ }
185
+ // ---------------------------------------------------------------------------
186
+ // Promotion Gate Integration (Phase 7)
187
+ // ---------------------------------------------------------------------------
188
+ /**
189
+ * Check if a checkpoint has passed the promotion gate and can be deployed.
190
+ *
191
+ * This function checks:
192
+ * 1. The checkpoint has an eval summary attached (lineage complete)
193
+ * 2. The promotion state is 'shadow_ready' or 'promotable' (gate passed)
194
+ *
195
+ * @param stateDir - Workspace state directory
196
+ * @param checkpointId - Checkpoint to verify
197
+ * @returns true if the checkpoint can be deployed, false otherwise
198
+ */
199
+ export function hasPassedPromotionGate(stateDir, checkpointId) {
200
+ const checkpoint = getCheckpoint(stateDir, checkpointId);
201
+ if (!checkpoint)
202
+ return false;
203
+ // Must have eval summary attached
204
+ if (!checkpoint.lastEvalSummaryRef)
205
+ return false;
206
+ // Check promotion state
207
+ const state = getPromotionState(stateDir, checkpointId);
208
+ return state === 'shadow_ready' || state === 'promotable';
209
+ }
210
+ /**
211
+ * Assert that a checkpoint has passed the promotion gate.
212
+ * Throws if the checkpoint cannot be deployed.
213
+ *
214
+ * @param stateDir - Workspace state directory
215
+ * @param checkpointId - Checkpoint to verify
216
+ * @throws Error if the checkpoint has not passed the promotion gate
217
+ */
218
+ export function assertPromotionGatePassed(stateDir, checkpointId) {
219
+ if (!hasPassedPromotionGate(stateDir, checkpointId)) {
220
+ throw new Error(`Checkpoint "${checkpointId}" has not passed the promotion gate. ` +
221
+ `A checkpoint must pass the promotion gate (state: shadow_ready or promotable) ` +
222
+ `before it can be bound to a worker profile. ` +
223
+ `Ensure the promotion gate has been evaluated and approved.`);
224
+ }
225
+ }
226
+ // ---------------------------------------------------------------------------
227
+ // Core Operations
228
+ // ---------------------------------------------------------------------------
229
+ /**
230
+ * Bind a checkpoint to a worker profile, creating or updating the deployment record.
231
+ *
232
+ * BINDING RULE (fail-closed):
233
+ * Only a checkpoint that is marked deployable in the training registry may be bound.
234
+ *
235
+ * PROFILE-FAMILY CONSTRAINT:
236
+ * The checkpoint's targetModelFamily must satisfy the profile's family keyword constraint.
237
+ * See: validateProfileFamilyConstraint()
238
+ *
239
+ * @param stateDir - Workspace state directory
240
+ * @param workerProfile - Target worker profile (local-reader | local-editor)
241
+ * @param checkpointId - Checkpoint to bind (must be deployable)
242
+ * @param note - Optional human-readable note
243
+ * @returns The new or updated Deployment record
244
+ *
245
+ * @throws Error if checkpoint is not found or not deployable
246
+ * @throws Error if checkpoint's targetModelFamily violates profile constraints
247
+ */
248
+ export function bindCheckpointToWorkerProfile(stateDir, workerProfile, checkpointId, note) {
249
+ assertSupportedProfile(workerProfile);
250
+ return withDeploymentRegistryLock(stateDir, (registry) => {
251
+ const now = new Date().toISOString();
252
+ // --- Validate checkpoint exists and is deployable ---
253
+ const checkpoint = getCheckpoint(stateDir, checkpointId);
254
+ if (!checkpoint) {
255
+ throw new Error(`bindCheckpointToWorkerProfile failed: checkpoint "${checkpointId}" not found ` +
256
+ `in training registry. Ensure the checkpoint has been registered via ` +
257
+ `registerCheckpoint() in model-training-registry.ts first.`);
258
+ }
259
+ if (!isCheckpointDeployable(stateDir, checkpointId)) {
260
+ throw new Error(`bindCheckpointToWorkerProfile failed: checkpoint "${checkpointId}" is not deployable. ` +
261
+ `Only checkpoints that have passed evaluation may be bound to a worker profile. ` +
262
+ `Use markCheckpointDeployable() in model-training-registry.ts after a successful eval.`);
263
+ }
264
+ // --- Phase 7: Validate promotion gate has passed ---
265
+ assertPromotionGatePassed(stateDir, checkpointId);
266
+ // --- Validate profile-family constraint ---
267
+ validateProfileFamilyConstraint(workerProfile, checkpoint.targetModelFamily);
268
+ // --- Find existing deployment for this profile (if any) ---
269
+ const existingIdx = registry.deployments.findIndex((d) => d.workerProfile === workerProfile);
270
+ const deploymentId = existingIdx >= 0
271
+ ? registry.deployments[existingIdx].deploymentId
272
+ : crypto.randomUUID();
273
+ const previousCheckpointId = existingIdx >= 0
274
+ ? registry.deployments[existingIdx].activeCheckpointId ?? null
275
+ : null;
276
+ const newDeployment = {
277
+ deploymentId,
278
+ workerProfile,
279
+ targetModelFamily: checkpoint.targetModelFamily,
280
+ activeCheckpointId: checkpointId,
281
+ // When re-binding (updating checkpoint), the old active becomes previous
282
+ previousCheckpointId,
283
+ // routingEnabled starts false — must be explicitly enabled
284
+ routingEnabled: false,
285
+ deployedAt: existingIdx >= 0
286
+ ? registry.deployments[existingIdx].deployedAt
287
+ : now,
288
+ updatedAt: now,
289
+ note: note ?? (existingIdx >= 0 ? registry.deployments[existingIdx].note : undefined),
290
+ };
291
+ if (existingIdx >= 0) {
292
+ registry.deployments[existingIdx] = newDeployment;
293
+ }
294
+ else {
295
+ registry.deployments.push(newDeployment);
296
+ }
297
+ writeRegistry(stateDir, registry);
298
+ return newDeployment;
299
+ });
300
+ }
301
+ /**
302
+ * Retrieve the deployment record for a worker profile.
303
+ *
304
+ * @returns Deployment if found, null otherwise
305
+ */
306
+ export function getDeployment(stateDir, workerProfile) {
307
+ assertSupportedProfile(workerProfile);
308
+ const registry = readRegistry(stateDir);
309
+ return registry.deployments.find((d) => d.workerProfile === workerProfile) ?? null;
310
+ }
311
+ /**
312
+ * List all deployments, optionally filtered.
313
+ *
314
+ * @param stateDir - Workspace state directory
315
+ * @param filter - Optional filter criteria
316
+ */
317
+ export function listDeployments(stateDir, filter) {
318
+ const registry = readRegistry(stateDir);
319
+ let deployments = registry.deployments;
320
+ if (filter?.workerProfile) {
321
+ deployments = deployments.filter((d) => d.workerProfile === filter.workerProfile);
322
+ }
323
+ if (filter?.routingEnabled !== undefined) {
324
+ deployments = deployments.filter((d) => d.routingEnabled === filter.routingEnabled);
325
+ }
326
+ return deployments.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());
327
+ }
328
+ /**
329
+ * Enable routing for a worker profile.
330
+ *
331
+ * PRECONDITIONS (fail-closed):
332
+ * 1. A deployment record must exist for this profile
333
+ * 2. activeCheckpointId must not be null
334
+ *
335
+ * @throws Error if no deployment exists
336
+ * @throws Error if activeCheckpointId is null (nothing to route to)
337
+ */
338
+ export function enableRoutingForProfile(stateDir, workerProfile) {
339
+ assertSupportedProfile(workerProfile);
340
+ return withDeploymentRegistryLock(stateDir, (registry) => {
341
+ const idx = registry.deployments.findIndex((d) => d.workerProfile === workerProfile);
342
+ if (idx === -1) {
343
+ throw new Error(`enableRoutingForProfile failed: no deployment found for profile "${workerProfile}". ` +
344
+ `Bind a checkpoint first using bindCheckpointToWorkerProfile().`);
345
+ }
346
+ const deployment = registry.deployments[idx];
347
+ if (!deployment.activeCheckpointId) {
348
+ throw new Error(`enableRoutingForProfile failed: deployment for "${workerProfile}" has no ` +
349
+ `active checkpoint (activeCheckpointId is null). ` +
350
+ `Bind a checkpoint before enabling routing.`);
351
+ }
352
+ // Double-check the active checkpoint is still deployable
353
+ if (!isCheckpointDeployable(stateDir, deployment.activeCheckpointId)) {
354
+ throw new Error(`enableRoutingForProfile failed: active checkpoint "${deployment.activeCheckpointId}" ` +
355
+ `for profile "${workerProfile}" is no longer marked deployable. ` +
356
+ `Revoke deployment or bind a new checkpoint before enabling routing.`);
357
+ }
358
+ registry.deployments[idx] = {
359
+ ...deployment,
360
+ routingEnabled: true,
361
+ updatedAt: new Date().toISOString(),
362
+ };
363
+ writeRegistry(stateDir, registry);
364
+ return registry.deployments[idx];
365
+ });
366
+ }
367
+ /**
368
+ * Disable routing for a worker profile.
369
+ * This is always safe — it does not unbind the checkpoint.
370
+ *
371
+ * @throws Error if no deployment exists for the profile
372
+ */
373
+ export function disableRoutingForProfile(stateDir, workerProfile) {
374
+ assertSupportedProfile(workerProfile);
375
+ return withDeploymentRegistryLock(stateDir, (registry) => {
376
+ const idx = registry.deployments.findIndex((d) => d.workerProfile === workerProfile);
377
+ if (idx === -1) {
378
+ throw new Error(`disableRoutingForProfile failed: no deployment found for profile "${workerProfile}".`);
379
+ }
380
+ registry.deployments[idx] = {
381
+ ...registry.deployments[idx],
382
+ routingEnabled: false,
383
+ updatedAt: new Date().toISOString(),
384
+ };
385
+ writeRegistry(stateDir, registry);
386
+ return registry.deployments[idx];
387
+ });
388
+ }
389
+ /**
390
+ * Roll back the deployment for a worker profile to its previous checkpoint.
391
+ *
392
+ * ROLLBACK RULE:
393
+ * - Can only roll back if previousCheckpointId is not null
394
+ * - Sets activeCheckpointId = previousCheckpointId
395
+ * - The old activeCheckpointId becomes the new previousCheckpointId
396
+ * - routingEnabled is set to false (must be re-enabled explicitly)
397
+ *
398
+ * @throws Error if no deployment exists
399
+ * @throws Error if no previous checkpoint is available
400
+ */
401
+ export function rollbackDeployment(stateDir, workerProfile, note) {
402
+ assertSupportedProfile(workerProfile);
403
+ return withDeploymentRegistryLock(stateDir, (registry) => {
404
+ const idx = registry.deployments.findIndex((d) => d.workerProfile === workerProfile);
405
+ if (idx === -1) {
406
+ throw new Error(`rollbackDeployment failed: no deployment found for profile "${workerProfile}".`);
407
+ }
408
+ const deployment = registry.deployments[idx];
409
+ if (!deployment.previousCheckpointId) {
410
+ throw new Error(`rollbackDeployment failed: no previous checkpoint available for profile ` +
411
+ `"${workerProfile}". The current deployment has no rollback target. ` +
412
+ `(activeCheckpointId="${deployment.activeCheckpointId}", ` +
413
+ `previousCheckpointId=null)`);
414
+ }
415
+ // Verify the rollback target checkpoint still exists and is deployable
416
+ const rollbackTarget = getCheckpoint(stateDir, deployment.previousCheckpointId);
417
+ if (!rollbackTarget) {
418
+ throw new Error(`rollbackDeployment failed: previous checkpoint "${deployment.previousCheckpointId}" ` +
419
+ `no longer exists in the training registry. Cannot roll back to a deleted checkpoint.`);
420
+ }
421
+ if (!isCheckpointDeployable(stateDir, deployment.previousCheckpointId)) {
422
+ throw new Error(`rollbackDeployment failed: previous checkpoint "${deployment.previousCheckpointId}" ` +
423
+ `is no longer deployable. Roll back to a passing checkpoint or re-bind a new one.`);
424
+ }
425
+ const now = new Date().toISOString();
426
+ // Chain the rollback: the old active becomes the new previous
427
+ const newPreviousCheckpointId = deployment.activeCheckpointId;
428
+ const rolledBack = {
429
+ ...deployment,
430
+ activeCheckpointId: deployment.previousCheckpointId,
431
+ previousCheckpointId: newPreviousCheckpointId,
432
+ routingEnabled: false, // Always disable routing after rollback — must re-enable
433
+ updatedAt: now,
434
+ note: note ?? `Rollback to ${deployment.previousCheckpointId}`,
435
+ };
436
+ registry.deployments[idx] = rolledBack;
437
+ writeRegistry(stateDir, registry);
438
+ return rolledBack;
439
+ });
440
+ }
441
+ // ---------------------------------------------------------------------------
442
+ // Read-Only Query Helpers
443
+ // ---------------------------------------------------------------------------
444
+ /**
445
+ * Check whether a worker profile currently has an enabled deployment
446
+ * with an active checkpoint that is still deployable.
447
+ *
448
+ * GOVERNANCE: Even if routing was previously enabled, a checkpoint that
449
+ * has been revoked (marked non-deployable via markCheckpointDeployable(false))
450
+ * must not be used for routing. This prevents routing traffic to a
451
+ * checkpoint that has been superseded or failed re-evaluation.
452
+ */
453
+ export function isRoutingEnabledForProfile(stateDir, workerProfile) {
454
+ const deployment = getDeployment(stateDir, workerProfile);
455
+ if (!deployment?.routingEnabled)
456
+ return false;
457
+ if (!deployment.activeCheckpointId)
458
+ return false;
459
+ // Re-check deployability on every routing decision — checkpoint may have been revoked
460
+ return isCheckpointDeployable(stateDir, deployment.activeCheckpointId);
461
+ }
462
+ /**
463
+ * Get the active checkpoint ID for a worker profile.
464
+ * Returns null if no deployment or no active checkpoint.
465
+ */
466
+ export function getActiveCheckpointForProfile(stateDir, workerProfile) {
467
+ const deployment = getDeployment(stateDir, workerProfile);
468
+ return deployment?.activeCheckpointId ?? null;
469
+ }
470
+ /**
471
+ * Get the full deployment record with lineage context.
472
+ * Returns null if no deployment exists.
473
+ *
474
+ * Lineage includes: deployment record, active checkpoint, parent training run, eval summary.
475
+ */
476
+ export function getDeploymentLineage(stateDir, workerProfile) {
477
+ const deployment = getDeployment(stateDir, workerProfile);
478
+ if (!deployment)
479
+ return null;
480
+ const activeCheckpoint = deployment.activeCheckpointId
481
+ ? getCheckpoint(stateDir, deployment.activeCheckpointId)
482
+ : null;
483
+ return { deployment, activeCheckpoint };
484
+ }
485
+ /**
486
+ * Get the complete deployment registry (for debugging/admin purposes).
487
+ */
488
+ export function getFullDeploymentRegistry(stateDir) {
489
+ return readRegistry(stateDir);
490
+ }
491
+ /**
492
+ * Compute stats for the deployment registry.
493
+ */
494
+ export function getDeploymentRegistryStats(stateDir) {
495
+ const registry = readRegistry(stateDir);
496
+ const deployments = registry.deployments;
497
+ return {
498
+ totalDeployments: deployments.length,
499
+ activeDeployments: deployments.filter((d) => d.routingEnabled).length,
500
+ profilesWithBindings: new Set(deployments.map((d) => d.workerProfile)).size,
501
+ profilesWithRoutingEnabled: deployments.filter((d) => d.routingEnabled).length,
502
+ };
503
+ }