principles-disciple 1.133.0 → 1.135.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.
- package/dist/core/schema/schema-definitions.js +3 -0
- package/dist/core/trajectory-types.d.ts +5 -0
- package/dist/core/trajectory.d.ts +1 -0
- package/dist/core/trajectory.js +29 -8
- package/dist/hooks/after-tool-call-helpers.d.ts +5 -0
- package/dist/hooks/after-tool-call-helpers.js +64 -0
- package/dist/hooks/llm.d.ts +8 -0
- package/dist/hooks/llm.js +30 -0
- package/dist/hooks/trajectory-evidence.js +6 -2
- package/dist/openclaw-sdk.d.ts +1 -0
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/dist/core/external-training-contract.d.ts +0 -276
- package/dist/core/external-training-contract.js +0 -269
|
@@ -49,6 +49,8 @@ export const SCHEMAS = {
|
|
|
49
49
|
empathy_signal_json TEXT NOT NULL,
|
|
50
50
|
blob_ref TEXT,
|
|
51
51
|
raw_excerpt TEXT,
|
|
52
|
+
stop_reason TEXT,
|
|
53
|
+
thinking_blocks_count INTEGER,
|
|
52
54
|
created_at TEXT NOT NULL
|
|
53
55
|
)`,
|
|
54
56
|
indexes: [
|
|
@@ -87,6 +89,7 @@ export const SCHEMAS = {
|
|
|
87
89
|
gfi_before REAL,
|
|
88
90
|
gfi_after REAL,
|
|
89
91
|
params_json TEXT NOT NULL,
|
|
92
|
+
result_preview TEXT,
|
|
90
93
|
created_at TEXT NOT NULL
|
|
91
94
|
)`,
|
|
92
95
|
indexes: [
|
|
@@ -28,6 +28,8 @@ export interface TrajectoryAssistantTurnInput {
|
|
|
28
28
|
sanitizedText: string;
|
|
29
29
|
usageJson: unknown;
|
|
30
30
|
empathySignalJson: unknown;
|
|
31
|
+
stopReason?: string | null;
|
|
32
|
+
thinkingBlocksCount?: number | null;
|
|
31
33
|
createdAt?: string;
|
|
32
34
|
}
|
|
33
35
|
export interface TrajectoryUserTurnInput {
|
|
@@ -50,6 +52,7 @@ export interface TrajectoryToolCallInput {
|
|
|
50
52
|
gfiBefore?: number | null;
|
|
51
53
|
gfiAfter?: number | null;
|
|
52
54
|
paramsJson?: unknown;
|
|
55
|
+
resultPreview?: string | null;
|
|
53
56
|
createdAt?: string;
|
|
54
57
|
}
|
|
55
58
|
export interface TrajectoryPainEventInput {
|
|
@@ -190,6 +193,8 @@ export interface AssistantTurnRecord {
|
|
|
190
193
|
rawText: string;
|
|
191
194
|
sanitizedText: string;
|
|
192
195
|
blobRef: string | null;
|
|
196
|
+
stopReason: string | null;
|
|
197
|
+
thinkingBlocksCount: number | null;
|
|
193
198
|
createdAt: string;
|
|
194
199
|
}
|
|
195
200
|
export interface CorrectionSampleRecord {
|
package/dist/core/trajectory.js
CHANGED
|
@@ -93,9 +93,9 @@ export class TrajectoryDatabase {
|
|
|
93
93
|
const result = this.db.prepare(`
|
|
94
94
|
INSERT INTO assistant_turns (
|
|
95
95
|
session_id, run_id, provider, model, raw_text, sanitized_text, usage_json,
|
|
96
|
-
empathy_signal_json, blob_ref, raw_excerpt, created_at
|
|
97
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
98
|
-
`).run(input.sessionId, input.runId, input.provider, input.model, rawStorage.inlineText, input.sanitizedText, safeJson(input.usageJson), safeJson(input.empathySignalJson), rawStorage.blobRef, rawStorage.excerpt, createdAt);
|
|
96
|
+
empathy_signal_json, blob_ref, raw_excerpt, stop_reason, thinking_blocks_count, created_at
|
|
97
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
98
|
+
`).run(input.sessionId, input.runId, input.provider, input.model, rawStorage.inlineText, input.sanitizedText, safeJson(input.usageJson), safeJson(input.empathySignalJson), rawStorage.blobRef, rawStorage.excerpt, input.stopReason ?? null, input.thinkingBlocksCount ?? null, createdAt);
|
|
99
99
|
return Number(result.lastInsertRowid);
|
|
100
100
|
});
|
|
101
101
|
}
|
|
@@ -120,9 +120,9 @@ export class TrajectoryDatabase {
|
|
|
120
120
|
const result = this.db.prepare(`
|
|
121
121
|
INSERT INTO tool_calls (
|
|
122
122
|
session_id, tool_name, outcome, duration_ms, exit_code, error_type, error_message,
|
|
123
|
-
gfi_before, gfi_after, params_json, created_at
|
|
124
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
125
|
-
`).run(input.sessionId, input.toolName, input.outcome, input.durationMs ?? null, input.exitCode ?? null, input.errorType ?? null, input.errorMessage ?? null, input.gfiBefore ?? null, input.gfiAfter ?? null, safeJson(input.paramsJson), createdAt);
|
|
123
|
+
gfi_before, gfi_after, params_json, result_preview, created_at
|
|
124
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
125
|
+
`).run(input.sessionId, input.toolName, input.outcome, input.durationMs ?? null, input.exitCode ?? null, input.errorType ?? null, input.errorMessage ?? null, input.gfiBefore ?? null, input.gfiAfter ?? null, safeJson(input.paramsJson), input.resultPreview ?? null, createdAt);
|
|
126
126
|
return Number(result.lastInsertRowid);
|
|
127
127
|
});
|
|
128
128
|
if (input.outcome === 'success') {
|
|
@@ -548,7 +548,8 @@ export class TrajectoryDatabase {
|
|
|
548
548
|
*/
|
|
549
549
|
listAssistantTurns(sessionId) {
|
|
550
550
|
const rows = this.db.prepare(`
|
|
551
|
-
SELECT id, session_id, run_id, provider, model, raw_text, sanitized_text, blob_ref,
|
|
551
|
+
SELECT id, session_id, run_id, provider, model, raw_text, sanitized_text, blob_ref,
|
|
552
|
+
stop_reason, thinking_blocks_count, created_at
|
|
552
553
|
FROM assistant_turns
|
|
553
554
|
WHERE session_id = ?
|
|
554
555
|
ORDER BY id ASC
|
|
@@ -562,6 +563,8 @@ export class TrajectoryDatabase {
|
|
|
562
563
|
rawText: this.restoreRawText(row.raw_text, row.blob_ref),
|
|
563
564
|
sanitizedText: String(row.sanitized_text ?? ''),
|
|
564
565
|
blobRef: row.blob_ref ? String(row.blob_ref) : null,
|
|
566
|
+
stopReason: row.stop_reason != null ? String(row.stop_reason) : null,
|
|
567
|
+
thinkingBlocksCount: row.thinking_blocks_count != null ? Number(row.thinking_blocks_count) : null,
|
|
565
568
|
createdAt: String(row.created_at),
|
|
566
569
|
}));
|
|
567
570
|
}
|
|
@@ -574,7 +577,7 @@ export class TrajectoryDatabase {
|
|
|
574
577
|
listToolCallsForSession(sessionId) {
|
|
575
578
|
const rows = this.db.prepare(`
|
|
576
579
|
SELECT id, tool_name, outcome, params_json, duration_ms, exit_code, error_type, error_message,
|
|
577
|
-
gfi_before, gfi_after, created_at
|
|
580
|
+
gfi_before, gfi_after, result_preview, created_at
|
|
578
581
|
FROM tool_calls
|
|
579
582
|
WHERE session_id = ?
|
|
580
583
|
ORDER BY id ASC
|
|
@@ -604,6 +607,7 @@ export class TrajectoryDatabase {
|
|
|
604
607
|
errorMessage: row.error_message ? String(row.error_message) : null,
|
|
605
608
|
gfiBefore: row.gfi_before != null ? Number(row.gfi_before) : null,
|
|
606
609
|
gfiAfter: row.gfi_after != null ? Number(row.gfi_after) : null,
|
|
610
|
+
resultPreview: row.result_preview != null ? String(row.result_preview) : null,
|
|
607
611
|
createdAt: String(row.created_at),
|
|
608
612
|
};
|
|
609
613
|
});
|
|
@@ -1085,6 +1089,23 @@ export class TrajectoryDatabase {
|
|
|
1085
1089
|
}
|
|
1086
1090
|
}
|
|
1087
1091
|
}
|
|
1092
|
+
// Trajectory enhancement: add stop_reason, thinking_blocks_count, result_preview
|
|
1093
|
+
const trajectoryEnhancementColumns = [
|
|
1094
|
+
{ table: 'assistant_turns', name: 'stop_reason', type: 'TEXT' },
|
|
1095
|
+
{ table: 'assistant_turns', name: 'thinking_blocks_count', type: 'INTEGER' },
|
|
1096
|
+
{ table: 'tool_calls', name: 'result_preview', type: 'TEXT' },
|
|
1097
|
+
];
|
|
1098
|
+
for (const col of trajectoryEnhancementColumns) {
|
|
1099
|
+
try {
|
|
1100
|
+
this.db.exec(`ALTER TABLE ${col.table} ADD COLUMN ${col.name} ${col.type}`);
|
|
1101
|
+
}
|
|
1102
|
+
catch (err) {
|
|
1103
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
1104
|
+
if (!message.includes('duplicate column name') && !message.includes('no column named')) {
|
|
1105
|
+
throw err;
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1088
1109
|
// PRI-406: Partial unique index on canonical_pain_id (non-null only) for dedup
|
|
1089
1110
|
this.db.exec(`
|
|
1090
1111
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_pain_events_canonical_pain_id
|
|
@@ -17,6 +17,11 @@ import { type SessionState } from '../core/session-tracker.js';
|
|
|
17
17
|
import { WorkspaceContext } from '../core/workspace-context.js';
|
|
18
18
|
import type { PluginHookAfterToolCallEvent } from '../openclaw-sdk.js';
|
|
19
19
|
import type { ToolCallOutcome, ToolCallObservation, PainAdmissionDecision } from './after-tool-call-types.js';
|
|
20
|
+
/**
|
|
21
|
+
* Extract a preview string from tool call result for diagnostic evidence.
|
|
22
|
+
* Pure function — no I/O, no side effects. ERR-001 / ERR-014 compliant.
|
|
23
|
+
*/
|
|
24
|
+
export declare function extractToolResultPreview(result: unknown): string | null;
|
|
20
25
|
/**
|
|
21
26
|
* Classify the outcome of a tool call event.
|
|
22
27
|
*
|
|
@@ -25,6 +25,68 @@ import { resolveSourceKind, buildToolFailureObservation } from './raw-observatio
|
|
|
25
25
|
import { evaluateEvidenceTriage } from './triage-adapter.js';
|
|
26
26
|
import { evaluateTriggerController } from '@principles/core/runtime-v2';
|
|
27
27
|
import { buildTrajectoryEvidence } from './trajectory-evidence.js';
|
|
28
|
+
const RESULT_PREVIEW_MAX_LENGTH = 500;
|
|
29
|
+
/**
|
|
30
|
+
* Extract a preview string from tool call result for diagnostic evidence.
|
|
31
|
+
* Pure function — no I/O, no side effects. ERR-001 / ERR-014 compliant.
|
|
32
|
+
*/
|
|
33
|
+
export function extractToolResultPreview(result) {
|
|
34
|
+
if (result === null || result === undefined)
|
|
35
|
+
return null;
|
|
36
|
+
try {
|
|
37
|
+
// String result: truncate directly
|
|
38
|
+
if (typeof result === 'string') {
|
|
39
|
+
return result.length > RESULT_PREVIEW_MAX_LENGTH
|
|
40
|
+
? `${result.slice(0, RESULT_PREVIEW_MAX_LENGTH - 3)}...`
|
|
41
|
+
: result;
|
|
42
|
+
}
|
|
43
|
+
// Object with content array (e.g., Anthropic content blocks)
|
|
44
|
+
if (typeof result === 'object' && !Array.isArray(result)) {
|
|
45
|
+
const obj = result;
|
|
46
|
+
if (Array.isArray(obj.content)) {
|
|
47
|
+
const textParts = [];
|
|
48
|
+
for (const block of obj.content) {
|
|
49
|
+
if (block && typeof block === 'object' && !Array.isArray(block)) {
|
|
50
|
+
const blockObj = block;
|
|
51
|
+
if (blockObj.type === 'text' && typeof blockObj.text === 'string') {
|
|
52
|
+
textParts.push(blockObj.text);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (textParts.length > 0) {
|
|
57
|
+
const joined = textParts.join('\n');
|
|
58
|
+
return joined.length > RESULT_PREVIEW_MAX_LENGTH
|
|
59
|
+
? `${joined.slice(0, RESULT_PREVIEW_MAX_LENGTH - 3)}...`
|
|
60
|
+
: joined;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// Object without content array: JSON.stringify with depth limiter (ERR-014)
|
|
64
|
+
const serialized = JSON.stringify(obj, (_key, value) => {
|
|
65
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
66
|
+
const keys = Object.keys(value);
|
|
67
|
+
if (keys.length > 10) {
|
|
68
|
+
const truncated = {};
|
|
69
|
+
for (const k of keys.slice(0, 10)) {
|
|
70
|
+
truncated[k] = value[k];
|
|
71
|
+
}
|
|
72
|
+
truncated['__truncated__'] = true;
|
|
73
|
+
return truncated;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return value;
|
|
77
|
+
}, 2);
|
|
78
|
+
if (serialized && serialized !== '{}') {
|
|
79
|
+
return serialized.length > RESULT_PREVIEW_MAX_LENGTH
|
|
80
|
+
? `${serialized.slice(0, RESULT_PREVIEW_MAX_LENGTH - 3)}...`
|
|
81
|
+
: serialized;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return '[result_preview_extraction_failed]';
|
|
88
|
+
}
|
|
89
|
+
}
|
|
28
90
|
// ── Stage 1: Classify ───────────────────────────────────────────────────────
|
|
29
91
|
/**
|
|
30
92
|
* Classify the outcome of a tool call event.
|
|
@@ -150,6 +212,7 @@ export function handleFrictionTrackingForFailure(sessionId, event, outcome, obse
|
|
|
150
212
|
gfiBefore,
|
|
151
213
|
gfiAfter: updatedState.currentGfi,
|
|
152
214
|
paramsJson: sanitizeToolParamsForEvidence(event.params, workspaceDir),
|
|
215
|
+
resultPreview: extractToolResultPreview(event.result),
|
|
153
216
|
});
|
|
154
217
|
return updatedState;
|
|
155
218
|
}
|
|
@@ -188,6 +251,7 @@ export function handleFrictionTrackingForSuccess(sessionId, event, outcome, obse
|
|
|
188
251
|
gfiBefore,
|
|
189
252
|
gfiAfter: resetState.currentGfi,
|
|
190
253
|
paramsJson: sanitizeToolParamsForEvidence(event.params, workspaceDir),
|
|
254
|
+
resultPreview: extractToolResultPreview(event.result),
|
|
191
255
|
});
|
|
192
256
|
wctx.eventLog.recordToolCall(sessionId, {
|
|
193
257
|
toolName: event.toolName,
|
package/dist/hooks/llm.d.ts
CHANGED
|
@@ -8,6 +8,14 @@ export interface EmpathySignal {
|
|
|
8
8
|
}
|
|
9
9
|
export declare function extractEmpathySignal(text: string): EmpathySignal;
|
|
10
10
|
export declare function isEmpathyAuditPayload(text: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Extract enhanced fields from lastAssistant (complete AssistantMessage) in hook payload.
|
|
13
|
+
* Pure function — no I/O, no side effects. ERR-001 compliant.
|
|
14
|
+
*/
|
|
15
|
+
export declare function extractAssistantEnhancedFields(lastAssistant: unknown): {
|
|
16
|
+
stopReason: string | null;
|
|
17
|
+
thinkingBlocksCount: number | null;
|
|
18
|
+
};
|
|
11
19
|
export declare function handleLlmOutput(event: PluginHookLlmOutputEvent, ctx: PluginHookAgentContext & {
|
|
12
20
|
workspaceDir?: string;
|
|
13
21
|
}): void;
|
package/dist/hooks/llm.js
CHANGED
|
@@ -124,6 +124,33 @@ export function isEmpathyAuditPayload(text) {
|
|
|
124
124
|
return true;
|
|
125
125
|
return false;
|
|
126
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Extract enhanced fields from lastAssistant (complete AssistantMessage) in hook payload.
|
|
129
|
+
* Pure function — no I/O, no side effects. ERR-001 compliant.
|
|
130
|
+
*/
|
|
131
|
+
export function extractAssistantEnhancedFields(lastAssistant) {
|
|
132
|
+
if (!lastAssistant || typeof lastAssistant !== 'object' || Array.isArray(lastAssistant)) {
|
|
133
|
+
return { stopReason: null, thinkingBlocksCount: null };
|
|
134
|
+
}
|
|
135
|
+
const obj = lastAssistant;
|
|
136
|
+
// stopReason: typeof guard
|
|
137
|
+
const stopReason = typeof obj.stopReason === 'string' ? obj.stopReason : null;
|
|
138
|
+
// thinkingBlocksCount: iterate content array, count thinking/redacted_thinking blocks
|
|
139
|
+
let thinkingBlocksCount = null;
|
|
140
|
+
if (Array.isArray(obj.content)) {
|
|
141
|
+
let count = 0;
|
|
142
|
+
for (const block of obj.content) {
|
|
143
|
+
if (block && typeof block === 'object' && !Array.isArray(block)) {
|
|
144
|
+
const blockType = block.type;
|
|
145
|
+
if (blockType === 'thinking' || blockType === 'redacted_thinking') {
|
|
146
|
+
count++;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
thinkingBlocksCount = count;
|
|
151
|
+
}
|
|
152
|
+
return { stopReason, thinkingBlocksCount };
|
|
153
|
+
}
|
|
127
154
|
export function handleLlmOutput(event, ctx) {
|
|
128
155
|
if (!ctx.workspaceDir || !ctx.sessionId)
|
|
129
156
|
return;
|
|
@@ -142,6 +169,7 @@ export function handleLlmOutput(event, ctx) {
|
|
|
142
169
|
return;
|
|
143
170
|
const text = event.assistantTexts.join('\n');
|
|
144
171
|
const signal = extractEmpathySignal(text);
|
|
172
|
+
const enhancedFields = extractAssistantEnhancedFields(event.lastAssistant);
|
|
145
173
|
const createdAt = new Date().toISOString();
|
|
146
174
|
let assistantTurnId = null;
|
|
147
175
|
try {
|
|
@@ -154,6 +182,8 @@ export function handleLlmOutput(event, ctx) {
|
|
|
154
182
|
sanitizedText: sanitizeAssistantText(text),
|
|
155
183
|
usageJson: event.usage || {},
|
|
156
184
|
empathySignalJson: signal,
|
|
185
|
+
stopReason: enhancedFields.stopReason,
|
|
186
|
+
thinkingBlocksCount: enhancedFields.thinkingBlocksCount,
|
|
157
187
|
createdAt,
|
|
158
188
|
});
|
|
159
189
|
}
|
|
@@ -41,9 +41,11 @@ export function buildTrajectoryEvidence(wctx, sessionId) {
|
|
|
41
41
|
if (evidence.length >= MAX_EVIDENCE_ENTRIES)
|
|
42
42
|
break;
|
|
43
43
|
const sanitizedNote = sanitizeAssistantText((turn.sanitizedText ?? '').slice(0, MAX_EVIDENCE_NOTE_CHARS));
|
|
44
|
+
// Enhanced: append truncation warning when stop_reason=length
|
|
45
|
+
const truncationWarning = turn.stopReason === 'length' ? ' [TRUNCATED: output cut off by length limit]' : '';
|
|
44
46
|
evidence.push({
|
|
45
47
|
sourceRef: `agent_turn:${turn.createdAt}`,
|
|
46
|
-
note: sanitizedNote,
|
|
48
|
+
note: sanitizedNote + truncationWarning,
|
|
47
49
|
});
|
|
48
50
|
}
|
|
49
51
|
}
|
|
@@ -62,7 +64,9 @@ export function buildTrajectoryEvidence(wctx, sessionId) {
|
|
|
62
64
|
for (const tc of failedToolCalls) {
|
|
63
65
|
if (evidence.length >= MAX_EVIDENCE_ENTRIES)
|
|
64
66
|
break;
|
|
65
|
-
|
|
67
|
+
// Enhanced: append resultPreview when available
|
|
68
|
+
const previewSuffix = tc.resultPreview ? ` | ${tc.resultPreview.slice(0, 200)}` : '';
|
|
69
|
+
const note = `Tool ${tc.toolName} failed: ${tc.errorType ?? 'unknown'} (exitCode: ${tc.exitCode ?? 'N/A'})${previewSuffix}`;
|
|
66
70
|
evidence.push({
|
|
67
71
|
sourceRef: `tool_call_failure:${tc.createdAt}`,
|
|
68
72
|
note: sanitizeAssistantText(note.slice(0, MAX_EVIDENCE_NOTE_CHARS)),
|
package/dist/openclaw-sdk.d.ts
CHANGED
package/openclaw.plugin.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"id": "principles-disciple",
|
|
3
3
|
"name": "Principles Disciple",
|
|
4
4
|
"description": "Evolutionary programming agent framework with strategic guardrails and reflection loops.",
|
|
5
|
-
"version": "1.
|
|
5
|
+
"version": "1.135.0",
|
|
6
6
|
"activation": {
|
|
7
7
|
"onCapabilities": [
|
|
8
8
|
"hook"
|
package/package.json
CHANGED
|
@@ -1,276 +0,0 @@
|
|
|
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;
|
|
@@ -1,269 +0,0 @@
|
|
|
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;
|