principles-disciple 1.7.6 → 1.8.0

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,276 @@
1
+ /**
2
+ * External Training Contract — Normalized Experiment Spec and Result Schema
3
+ * ========================================================================
4
+ *
5
+ * PURPOSE: Define the stable contract between the plugin and external trainer
6
+ * backends. The plugin produces a constrained experiment specification that an
7
+ * external trainer consumes. The trainer returns a normalized result that the
8
+ * plugin can register, evaluate, and gate for rollout.
9
+ *
10
+ * ARCHITECTURE:
11
+ * - Plugin is responsible for creating the experiment spec
12
+ * - Plugin is responsible for validating the trainer result
13
+ * - Plugin is responsible for registering lineage (train run → checkpoint → eval)
14
+ * - Plugin is responsible for invoking benchmark evaluation
15
+ * - Plugin is responsible for invoking promotion gate logic
16
+ * - Plugin is responsible for binding deployment only after gate approval
17
+ *
18
+ * DESIGN CONSTRAINTS:
19
+ * - ORPO-first: trainingMode must be 'orpo' for production runs
20
+ * - No real training inside the plugin
21
+ * - No direct deployment promotion from trainer output
22
+ * - No direct trainer writes to review/eval/deployment state
23
+ * - Backend-pluggable: same contract works for all backends
24
+ *
25
+ * CONTRACT GOALS:
26
+ * - support ORPO training for approved nocturnal exports
27
+ * - support multiple backend implementations behind one schema
28
+ * - preserve dataset / config / checkpoint lineage
29
+ * - remain valid on consumer hardware
30
+ * - fail closed when inputs are incomplete or inconsistent
31
+ */
32
+ /**
33
+ * Allowed backend identifiers.
34
+ *
35
+ * - `peft-trl-orpo`: primary reference implementation using PEFT + TRL ORPO
36
+ * - `unsloth-orpo`: compatible accelerated implementation using Unsloth
37
+ * - `dry-run`: validates paths/spec/environment only, no real training
38
+ */
39
+ export type TrainerBackendKind = 'peft-trl-orpo' | 'unsloth-orpo' | 'dry-run';
40
+ /**
41
+ * Hardware tier for training.
42
+ *
43
+ * - `consumer-gpu`: RTX 4090 24GB or equivalent (production target)
44
+ * - `small-gpu`: 8GB-16GB VRAM (compatibility target)
45
+ * - `cpu-experimental`: CPU-only experimental runs (dry-run or tiny models only)
46
+ */
47
+ export type HardwareTier = 'consumer-gpu' | 'small-gpu' | 'cpu-experimental';
48
+ /**
49
+ * Worker profiles supported for training.
50
+ *
51
+ * Phase 7 first rollout: `local-reader` only.
52
+ * `local-editor` requires explicit human approval to enable.
53
+ */
54
+ export type TrainableWorkerProfile = 'local-reader' | 'local-editor';
55
+ /**
56
+ * Training mode — Phase 7 production is ORPO-only.
57
+ */
58
+ export type TrainingMode = 'orpo';
59
+ /**
60
+ * Hyperparameters for ORPO training.
61
+ */
62
+ export interface TrainingHyperparameters {
63
+ learningRate: number;
64
+ batchSize: number;
65
+ gradientAccumulation: number;
66
+ loraRank: number;
67
+ loraAlpha: number;
68
+ loraDropout: number;
69
+ warmupRatio: number;
70
+ maxSteps: number;
71
+ maxSeqLength: number;
72
+ }
73
+ /**
74
+ * Budget constraints for a training experiment.
75
+ */
76
+ export interface TrainingBudget {
77
+ maxWallClockMinutes: number;
78
+ maxTrainTokens?: number;
79
+ }
80
+ /**
81
+ * Expected artifact from a successful training run.
82
+ */
83
+ export interface ExpectedArtifact {
84
+ checkpointName: string;
85
+ adapterFormat: 'peft-adapter';
86
+ }
87
+ /**
88
+ * The experiment specification sent to an external trainer.
89
+ * This defines WHAT to train, not HOW to train (backend-specific).
90
+ */
91
+ export interface TrainingExperimentSpec {
92
+ /** Unique identifier for this experiment */
93
+ experimentId: string;
94
+ /** Which backend to use */
95
+ backend: TrainerBackendKind;
96
+ /** Training mode — only 'orpo' is supported in Phase 7 */
97
+ trainingMode: TrainingMode;
98
+ /** Target worker profile for this experiment */
99
+ targetWorkerProfile: TrainableWorkerProfile;
100
+ /** Target model family to train */
101
+ targetModelFamily: string;
102
+ /** Hardware tier for this experiment */
103
+ hardwareTier: HardwareTier;
104
+ /** Reference to the ORPO export providing training data */
105
+ datasetExportId: string;
106
+ datasetExportPath: string;
107
+ /** Fingerprint of the dataset for lineage verification */
108
+ datasetFingerprint: string;
109
+ /** Reference to the benchmark export for eval */
110
+ benchmarkExportId: string;
111
+ /** Output directory for checkpoint artifacts */
112
+ outputDir: string;
113
+ /** Fingerprint of the training configuration */
114
+ configFingerprint: string;
115
+ /** Hash of the training code/contract version */
116
+ codeHash: string;
117
+ /** Training hyperparameters */
118
+ hyperparameters: TrainingHyperparameters;
119
+ /** Budget constraints */
120
+ budget: TrainingBudget;
121
+ /** Expected artifact from training */
122
+ expectedArtifact: ExpectedArtifact;
123
+ }
124
+ /**
125
+ * Training metrics recorded by the backend.
126
+ */
127
+ export interface TrainingMetrics {
128
+ wallClockMinutes: number;
129
+ finalLoss?: number;
130
+ tokensSeen?: number;
131
+ }
132
+ /**
133
+ * Artifact produced by a successful training run.
134
+ */
135
+ export interface TrainingArtifact {
136
+ adapterFormat: 'peft-adapter';
137
+ artifactPath: string;
138
+ }
139
+ /**
140
+ * Status of a training experiment.
141
+ */
142
+ export type ExperimentStatus = 'completed' | 'failed' | 'dry_run';
143
+ /**
144
+ * The result returned by an external trainer after execution.
145
+ * This defines the output contract — all backends must return the same shape.
146
+ */
147
+ export interface TrainingExperimentResult {
148
+ /** Experiment ID (must match the spec's experimentId) */
149
+ experimentId: string;
150
+ /** Which backend was used */
151
+ backend: TrainerBackendKind;
152
+ /** Final status of the experiment */
153
+ status: ExperimentStatus;
154
+ /** Registered training run ID (plugin-side) */
155
+ trainRunId?: string;
156
+ /** Registered checkpoint ID (plugin-side) */
157
+ checkpointId?: string;
158
+ /** Checkpoint reference string (for lineage) */
159
+ checkpointRef?: string;
160
+ /** Target worker profile */
161
+ targetWorkerProfile: TrainableWorkerProfile;
162
+ /** Target model family */
163
+ targetModelFamily: string;
164
+ /** Dataset fingerprint (for lineage verification) */
165
+ datasetFingerprint: string;
166
+ /** Config fingerprint (for lineage verification) */
167
+ configFingerprint: string;
168
+ /** Code hash (for lineage verification) */
169
+ codeHash: string;
170
+ /** Training metrics */
171
+ metrics?: TrainingMetrics;
172
+ /** Produced artifact (only if status === 'completed') */
173
+ artifact?: TrainingArtifact;
174
+ /** Failure reason (only if status === 'failed') */
175
+ failureReason?: string;
176
+ /** ISO-8601 creation timestamp */
177
+ createdAt: string;
178
+ }
179
+ /**
180
+ * Validation error for trainer result verification.
181
+ */
182
+ export interface ValidationError {
183
+ field: string;
184
+ expected: string;
185
+ actual: string;
186
+ reason: string;
187
+ }
188
+ /**
189
+ * Result of validating a trainer result against the experiment spec.
190
+ */
191
+ export interface ValidationResult {
192
+ valid: boolean;
193
+ errors: ValidationError[];
194
+ }
195
+ /**
196
+ * Validate that a trainer result matches the experiment spec.
197
+ *
198
+ * FAILS CLOSED on any mismatch — a checkpoint with invalid lineage must not
199
+ * be registered or promoted.
200
+ *
201
+ * Validation rules:
202
+ * 1. experimentId must match
203
+ * 2. backend must match
204
+ * 3. targetWorkerProfile must match
205
+ * 4. targetModelFamily must match
206
+ * 5. datasetFingerprint must match
207
+ * 6. configFingerprint must match
208
+ * 7. codeHash must match
209
+ * 8. dry-run must not produce a deployable checkpoint
210
+ *
211
+ * @param spec - The original experiment spec
212
+ * @param result - The trainer result to validate
213
+ * @returns ValidationResult indicating pass/fail and any errors
214
+ */
215
+ export declare function validateTrainerResult(spec: TrainingExperimentSpec, result: TrainingExperimentResult): ValidationResult;
216
+ /**
217
+ * Generate a fingerprint for a configuration object.
218
+ * Used for configFingerprint in the experiment spec.
219
+ */
220
+ export declare function computeConfigFingerprint(config: Partial<TrainingHyperparameters>): string;
221
+ /**
222
+ * Generate a fingerprint for a dataset export.
223
+ * Used for datasetFingerprint in the experiment spec.
224
+ *
225
+ * Combines file content hash with sampleCount to detect:
226
+ * - Content changes (file modified/replaced)
227
+ * - Sample count changes (different export)
228
+ *
229
+ * If the file cannot be read, falls back to path+count hash (legacy behavior).
230
+ */
231
+ export declare function computeDatasetFingerprint(exportPath: string, sampleCount: number): string;
232
+ /**
233
+ * Generate a code hash for the training contract version.
234
+ * Used for codeHash in the experiment spec.
235
+ *
236
+ * Hashes the actual contract source file content so any change to the
237
+ * contract produces a different hash, ensuring lineage integrity.
238
+ *
239
+ * Falls back to version string + timestamp if source cannot be read.
240
+ */
241
+ export declare function computeCodeHash(): string;
242
+ /**
243
+ * Generate a new experiment ID.
244
+ */
245
+ export declare function generateExperimentId(): string;
246
+ /**
247
+ * Validate that a hardware tier is appropriate for the backend.
248
+ *
249
+ * @param backend - The backend being used
250
+ * @param tier - The hardware tier
251
+ * @throws Error if the combination is not supported
252
+ */
253
+ export declare function validateHardwareTier(backend: TrainerBackendKind, tier: HardwareTier): void;
254
+ /**
255
+ * Get the default hardware tier for a backend.
256
+ */
257
+ export declare function getDefaultHardwareTier(backend: TrainerBackendKind): HardwareTier;
258
+ /**
259
+ * Valid model family patterns for local-reader profile.
260
+ * Used for family validation in the training contract.
261
+ */
262
+ export declare const READER_FAMILY_PATTERNS: string[];
263
+ /**
264
+ * Valid model family patterns for local-editor profile.
265
+ * Used for family validation in the training contract.
266
+ */
267
+ export declare const EDITOR_FAMILY_PATTERNS: string[];
268
+ /**
269
+ * Check if a model family is valid for a worker profile.
270
+ */
271
+ export declare function isValidModelFamilyForProfile(family: string, profile: TrainableWorkerProfile): boolean;
272
+ /**
273
+ * Phase 7 first rollout is limited to local-reader.
274
+ * This flag controls whether local-editor is allowed.
275
+ */
276
+ export declare const LOCAL_EDITOR_ENABLED = false;
@@ -0,0 +1,269 @@
1
+ /**
2
+ * External Training Contract — Normalized Experiment Spec and Result Schema
3
+ * ========================================================================
4
+ *
5
+ * PURPOSE: Define the stable contract between the plugin and external trainer
6
+ * backends. The plugin produces a constrained experiment specification that an
7
+ * external trainer consumes. The trainer returns a normalized result that the
8
+ * plugin can register, evaluate, and gate for rollout.
9
+ *
10
+ * ARCHITECTURE:
11
+ * - Plugin is responsible for creating the experiment spec
12
+ * - Plugin is responsible for validating the trainer result
13
+ * - Plugin is responsible for registering lineage (train run → checkpoint → eval)
14
+ * - Plugin is responsible for invoking benchmark evaluation
15
+ * - Plugin is responsible for invoking promotion gate logic
16
+ * - Plugin is responsible for binding deployment only after gate approval
17
+ *
18
+ * DESIGN CONSTRAINTS:
19
+ * - ORPO-first: trainingMode must be 'orpo' for production runs
20
+ * - No real training inside the plugin
21
+ * - No direct deployment promotion from trainer output
22
+ * - No direct trainer writes to review/eval/deployment state
23
+ * - Backend-pluggable: same contract works for all backends
24
+ *
25
+ * CONTRACT GOALS:
26
+ * - support ORPO training for approved nocturnal exports
27
+ * - support multiple backend implementations behind one schema
28
+ * - preserve dataset / config / checkpoint lineage
29
+ * - remain valid on consumer hardware
30
+ * - fail closed when inputs are incomplete or inconsistent
31
+ */
32
+ import * as crypto from 'crypto';
33
+ import * as fs from 'fs';
34
+ import { fileURLToPath } from 'url';
35
+ // ---------------------------------------------------------------------------
36
+ // Contract Validation
37
+ // ---------------------------------------------------------------------------
38
+ /**
39
+ * Validate that a trainer result matches the experiment spec.
40
+ *
41
+ * FAILS CLOSED on any mismatch — a checkpoint with invalid lineage must not
42
+ * be registered or promoted.
43
+ *
44
+ * Validation rules:
45
+ * 1. experimentId must match
46
+ * 2. backend must match
47
+ * 3. targetWorkerProfile must match
48
+ * 4. targetModelFamily must match
49
+ * 5. datasetFingerprint must match
50
+ * 6. configFingerprint must match
51
+ * 7. codeHash must match
52
+ * 8. dry-run must not produce a deployable checkpoint
53
+ *
54
+ * @param spec - The original experiment spec
55
+ * @param result - The trainer result to validate
56
+ * @returns ValidationResult indicating pass/fail and any errors
57
+ */
58
+ export function validateTrainerResult(spec, result) {
59
+ const errors = [];
60
+ // Rule 1: experimentId must match
61
+ if (spec.experimentId !== result.experimentId) {
62
+ errors.push({
63
+ field: 'experimentId',
64
+ expected: spec.experimentId,
65
+ actual: result.experimentId,
66
+ reason: 'Trainer result experimentId does not match the experiment spec',
67
+ });
68
+ }
69
+ // Rule 2: backend must match
70
+ if (spec.backend !== result.backend) {
71
+ errors.push({
72
+ field: 'backend',
73
+ expected: spec.backend,
74
+ actual: result.backend,
75
+ reason: 'Trainer result backend does not match the experiment spec',
76
+ });
77
+ }
78
+ // Rule 3: targetWorkerProfile must match
79
+ if (spec.targetWorkerProfile !== result.targetWorkerProfile) {
80
+ errors.push({
81
+ field: 'targetWorkerProfile',
82
+ expected: spec.targetWorkerProfile,
83
+ actual: result.targetWorkerProfile,
84
+ reason: 'Trainer result targetWorkerProfile does not match the experiment spec',
85
+ });
86
+ }
87
+ // Rule 4: targetModelFamily must match
88
+ if (spec.targetModelFamily !== result.targetModelFamily) {
89
+ errors.push({
90
+ field: 'targetModelFamily',
91
+ expected: spec.targetModelFamily,
92
+ actual: result.targetModelFamily,
93
+ reason: 'Trainer result targetModelFamily does not match the experiment spec',
94
+ });
95
+ }
96
+ // Rule 5: datasetFingerprint must match
97
+ if (spec.datasetFingerprint !== result.datasetFingerprint) {
98
+ errors.push({
99
+ field: 'datasetFingerprint',
100
+ expected: spec.datasetFingerprint,
101
+ actual: result.datasetFingerprint,
102
+ reason: 'Dataset fingerprint mismatch — possible dataset tampering or wrong export used',
103
+ });
104
+ }
105
+ // Rule 6: configFingerprint must match
106
+ if (spec.configFingerprint !== result.configFingerprint) {
107
+ errors.push({
108
+ field: 'configFingerprint',
109
+ expected: spec.configFingerprint,
110
+ actual: result.configFingerprint,
111
+ reason: 'Config fingerprint mismatch — training config may have changed since spec was created',
112
+ });
113
+ }
114
+ // Rule 7: codeHash must match
115
+ if (spec.codeHash !== result.codeHash) {
116
+ errors.push({
117
+ field: 'codeHash',
118
+ expected: spec.codeHash,
119
+ actual: result.codeHash,
120
+ reason: 'Code hash mismatch — training code or contract version may have changed',
121
+ });
122
+ }
123
+ // Rule 8: dry-run must not produce a deployable checkpoint
124
+ if (spec.backend === 'dry-run') {
125
+ if (result.status === 'completed' && result.artifact) {
126
+ errors.push({
127
+ field: 'artifact',
128
+ expected: 'no artifact for dry-run',
129
+ actual: 'artifact present',
130
+ reason: 'Dry-run backend must not produce a deployable checkpoint',
131
+ });
132
+ }
133
+ }
134
+ return {
135
+ valid: errors.length === 0,
136
+ errors,
137
+ };
138
+ }
139
+ // ---------------------------------------------------------------------------
140
+ // Spec Creation Helpers
141
+ // ---------------------------------------------------------------------------
142
+ /**
143
+ * Generate a fingerprint for a configuration object.
144
+ * Used for configFingerprint in the experiment spec.
145
+ */
146
+ export function computeConfigFingerprint(config) {
147
+ const normalized = JSON.stringify(config, Object.keys(config).sort());
148
+ return crypto.createHash('sha256').update(normalized).digest('hex').slice(0, 16);
149
+ }
150
+ /**
151
+ * Generate a fingerprint for a dataset export.
152
+ * Used for datasetFingerprint in the experiment spec.
153
+ *
154
+ * Combines file content hash with sampleCount to detect:
155
+ * - Content changes (file modified/replaced)
156
+ * - Sample count changes (different export)
157
+ *
158
+ * If the file cannot be read, falls back to path+count hash (legacy behavior).
159
+ */
160
+ export function computeDatasetFingerprint(exportPath, sampleCount) {
161
+ let contentHash;
162
+ try {
163
+ const content = fs.readFileSync(exportPath, 'utf-8');
164
+ contentHash = crypto.createHash('sha256').update(content, 'utf8').digest('hex').slice(0, 16);
165
+ }
166
+ catch {
167
+ // Fallback: include path in hash so different paths still differ
168
+ // (even if files don't exist during spec creation)
169
+ const fallbackContent = `${exportPath}:${sampleCount}`;
170
+ return crypto.createHash('sha256').update(fallbackContent).digest('hex').slice(0, 16);
171
+ }
172
+ // Combine content hash with sample count for additional safety
173
+ const combined = `${contentHash}:${sampleCount}`;
174
+ return crypto.createHash('sha256').update(combined).digest('hex').slice(0, 16);
175
+ }
176
+ /**
177
+ * Generate a code hash for the training contract version.
178
+ * Used for codeHash in the experiment spec.
179
+ *
180
+ * Hashes the actual contract source file content so any change to the
181
+ * contract produces a different hash, ensuring lineage integrity.
182
+ *
183
+ * Falls back to version string + timestamp if source cannot be read.
184
+ */
185
+ export function computeCodeHash() {
186
+ try {
187
+ // Hash the actual contract source file content using ESM-safe resolution
188
+ const sourcePath = fileURLToPath(import.meta.url);
189
+ const sourceContent = fs.readFileSync(sourcePath, 'utf-8');
190
+ // Include only the relevant contract definitions (first 500 lines)
191
+ // to avoid hash changes from comments/timestamps
192
+ const relevantContent = sourceContent.split('\n').slice(0, 500).join('\n');
193
+ return crypto.createHash('sha256').update(relevantContent).digest('hex').slice(0, 16);
194
+ }
195
+ catch {
196
+ // Fallback if source cannot be read (should not happen in normal operation)
197
+ // Use a deterministic version string — NOT Date.now() — so the hash is stable
198
+ const fallback = 'nocturnal-phase7-v1:deterministic-fallback';
199
+ return crypto.createHash('sha256').update(fallback).digest('hex').slice(0, 16);
200
+ }
201
+ }
202
+ /**
203
+ * Generate a new experiment ID.
204
+ */
205
+ export function generateExperimentId() {
206
+ return crypto.randomUUID();
207
+ }
208
+ // ---------------------------------------------------------------------------
209
+ // Hardware Tier Helpers
210
+ // ---------------------------------------------------------------------------
211
+ /**
212
+ * Validate that a hardware tier is appropriate for the backend.
213
+ *
214
+ * @param backend - The backend being used
215
+ * @param tier - The hardware tier
216
+ * @throws Error if the combination is not supported
217
+ */
218
+ export function validateHardwareTier(backend, tier) {
219
+ // cpu-experimental is only allowed for dry-run
220
+ if (tier === 'cpu-experimental' && backend !== 'dry-run') {
221
+ throw new Error(`Hardware tier 'cpu-experimental' is only allowed for 'dry-run' backend. ` +
222
+ `For real training on GPU, use 'consumer-gpu' or 'small-gpu'.`);
223
+ }
224
+ }
225
+ /**
226
+ * Get the default hardware tier for a backend.
227
+ */
228
+ export function getDefaultHardwareTier(backend) {
229
+ if (backend === 'dry-run') {
230
+ return 'cpu-experimental';
231
+ }
232
+ return 'consumer-gpu';
233
+ }
234
+ // ---------------------------------------------------------------------------
235
+ // Constants
236
+ // ---------------------------------------------------------------------------
237
+ /**
238
+ * Valid model family patterns for local-reader profile.
239
+ * Used for family validation in the training contract.
240
+ */
241
+ export const READER_FAMILY_PATTERNS = [
242
+ 'reader', 'read', 'claude-haiku', 'qwen-lite', 'phi-mini',
243
+ 'gpt-4o-mini', 'gpt-4o-nano',
244
+ ];
245
+ /**
246
+ * Valid model family patterns for local-editor profile.
247
+ * Used for family validation in the training contract.
248
+ */
249
+ export const EDITOR_FAMILY_PATTERNS = [
250
+ 'editor', 'edit', 'code', 'claude-sonnet', 'gpt-4o-mini',
251
+ ];
252
+ /**
253
+ * Check if a model family is valid for a worker profile.
254
+ */
255
+ export function isValidModelFamilyForProfile(family, profile) {
256
+ const lower = family.toLowerCase();
257
+ if (profile === 'local-reader') {
258
+ return READER_FAMILY_PATTERNS.some((p) => lower.includes(p));
259
+ }
260
+ if (profile === 'local-editor') {
261
+ return EDITOR_FAMILY_PATTERNS.some((p) => lower.includes(p));
262
+ }
263
+ return false;
264
+ }
265
+ /**
266
+ * Phase 7 first rollout is limited to local-reader.
267
+ * This flag controls whether local-editor is allowed.
268
+ */
269
+ export const LOCAL_EDITOR_ENABLED = false;