principles-disciple 1.7.5 → 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.
- package/dist/commands/context.js +5 -15
- package/dist/commands/evolution-status.js +29 -48
- package/dist/commands/export.js +61 -8
- package/dist/commands/nocturnal-review.d.ts +24 -0
- package/dist/commands/nocturnal-review.js +265 -0
- package/dist/commands/nocturnal-rollout.d.ts +27 -0
- package/dist/commands/nocturnal-rollout.js +671 -0
- package/dist/commands/nocturnal-train.d.ts +25 -0
- package/dist/commands/nocturnal-train.js +919 -0
- package/dist/commands/pain.js +8 -21
- package/dist/config/defaults/runtime.d.ts +40 -0
- package/dist/config/defaults/runtime.js +44 -0
- package/dist/config/errors.d.ts +84 -0
- package/dist/config/errors.js +94 -0
- package/dist/config/index.d.ts +7 -0
- package/dist/config/index.js +7 -0
- package/dist/constants/diagnostician.d.ts +0 -4
- package/dist/constants/diagnostician.js +0 -4
- package/dist/constants/tools.d.ts +2 -2
- package/dist/constants/tools.js +1 -1
- package/dist/core/adaptive-thresholds.d.ts +186 -0
- package/dist/core/adaptive-thresholds.js +300 -0
- package/dist/core/config.d.ts +2 -38
- package/dist/core/config.js +6 -61
- package/dist/core/control-ui-db.d.ts +27 -0
- package/dist/core/control-ui-db.js +18 -0
- package/dist/core/event-log.d.ts +1 -2
- package/dist/core/event-log.js +0 -3
- package/dist/core/evolution-engine.js +1 -21
- package/dist/core/evolution-reducer.d.ts +7 -1
- package/dist/core/evolution-reducer.js +56 -4
- package/dist/core/evolution-types.d.ts +61 -9
- package/dist/core/evolution-types.js +31 -9
- package/dist/core/external-training-contract.d.ts +276 -0
- package/dist/core/external-training-contract.js +269 -0
- package/dist/core/local-worker-routing.d.ts +175 -0
- package/dist/core/local-worker-routing.js +525 -0
- package/dist/core/model-deployment-registry.d.ts +218 -0
- package/dist/core/model-deployment-registry.js +503 -0
- package/dist/core/model-training-registry.d.ts +295 -0
- package/dist/core/model-training-registry.js +475 -0
- package/dist/core/nocturnal-arbiter.d.ts +159 -0
- package/dist/core/nocturnal-arbiter.js +534 -0
- package/dist/core/nocturnal-candidate-scoring.d.ts +137 -0
- package/dist/core/nocturnal-candidate-scoring.js +266 -0
- package/dist/core/nocturnal-compliance.d.ts +175 -0
- package/dist/core/nocturnal-compliance.js +824 -0
- package/dist/core/nocturnal-dataset.d.ts +224 -0
- package/dist/core/nocturnal-dataset.js +443 -0
- package/dist/core/nocturnal-executability.d.ts +85 -0
- package/dist/core/nocturnal-executability.js +331 -0
- package/dist/core/nocturnal-export.d.ts +124 -0
- package/dist/core/nocturnal-export.js +275 -0
- package/dist/core/nocturnal-paths.d.ts +124 -0
- package/dist/core/nocturnal-paths.js +214 -0
- package/dist/core/nocturnal-trajectory-extractor.d.ts +242 -0
- package/dist/core/nocturnal-trajectory-extractor.js +307 -0
- package/dist/core/nocturnal-trinity.d.ts +311 -0
- package/dist/core/nocturnal-trinity.js +880 -0
- package/dist/core/path-resolver.js +2 -1
- package/dist/core/paths.d.ts +6 -0
- package/dist/core/paths.js +6 -0
- package/dist/core/principle-training-state.d.ts +121 -0
- package/dist/core/principle-training-state.js +321 -0
- package/dist/core/promotion-gate.d.ts +238 -0
- package/dist/core/promotion-gate.js +529 -0
- package/dist/core/session-tracker.d.ts +10 -0
- package/dist/core/session-tracker.js +14 -0
- package/dist/core/shadow-observation-registry.d.ts +217 -0
- package/dist/core/shadow-observation-registry.js +308 -0
- package/dist/core/training-program.d.ts +233 -0
- package/dist/core/training-program.js +433 -0
- package/dist/core/trajectory.d.ts +155 -1
- package/dist/core/trajectory.js +292 -8
- package/dist/core/workspace-context.d.ts +0 -6
- package/dist/core/workspace-context.js +0 -12
- package/dist/hooks/bash-risk.d.ts +57 -0
- package/dist/hooks/bash-risk.js +137 -0
- package/dist/hooks/edit-verification.d.ts +62 -0
- package/dist/hooks/edit-verification.js +256 -0
- package/dist/hooks/gate-block-helper.d.ts +44 -0
- package/dist/hooks/gate-block-helper.js +119 -0
- package/dist/hooks/gate.d.ts +18 -0
- package/dist/hooks/gate.js +62 -751
- package/dist/hooks/gfi-gate.d.ts +40 -0
- package/dist/hooks/gfi-gate.js +113 -0
- package/dist/hooks/pain.js +6 -9
- package/dist/hooks/progressive-trust-gate.d.ts +51 -0
- package/dist/hooks/progressive-trust-gate.js +89 -0
- package/dist/hooks/prompt.d.ts +11 -11
- package/dist/hooks/prompt.js +167 -77
- package/dist/hooks/subagent.js +43 -6
- package/dist/hooks/thinking-checkpoint.d.ts +37 -0
- package/dist/hooks/thinking-checkpoint.js +51 -0
- package/dist/http/principles-console-route.js +13 -3
- package/dist/i18n/commands.js +8 -8
- package/dist/index.js +129 -28
- package/dist/service/central-database.js +2 -1
- package/dist/service/control-ui-query-service.d.ts +1 -1
- package/dist/service/control-ui-query-service.js +3 -3
- package/dist/service/evolution-query-service.d.ts +1 -1
- package/dist/service/evolution-query-service.js +5 -5
- package/dist/service/evolution-worker.d.ts +52 -4
- package/dist/service/evolution-worker.js +328 -16
- package/dist/service/nocturnal-runtime.d.ts +183 -0
- package/dist/service/nocturnal-runtime.js +352 -0
- package/dist/service/nocturnal-service.d.ts +163 -0
- package/dist/service/nocturnal-service.js +787 -0
- package/dist/service/nocturnal-target-selector.d.ts +145 -0
- package/dist/service/nocturnal-target-selector.js +315 -0
- package/dist/service/phase3-input-filter.d.ts +48 -12
- package/dist/service/phase3-input-filter.js +84 -18
- package/dist/service/runtime-summary-service.d.ts +34 -10
- package/dist/service/runtime-summary-service.js +87 -48
- package/dist/tools/deep-reflect.js +2 -1
- package/dist/types/event-types.d.ts +4 -10
- package/dist/types/runtime-summary.d.ts +47 -0
- package/dist/types/runtime-summary.js +1 -0
- package/dist/types.d.ts +0 -3
- package/dist/types.js +0 -2
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/templates/langs/en/skills/pd-mentor/SKILL.md +5 -5
- package/templates/langs/zh/skills/pd-mentor/SKILL.md +5 -5
- package/templates/pain_settings.json +0 -6
- package/dist/commands/trust.d.ts +0 -4
- package/dist/commands/trust.js +0 -78
- package/dist/core/trust-engine.d.ts +0 -96
- package/dist/core/trust-engine.js +0 -286
|
@@ -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;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local Worker Routing Policy — Task Classification and Routing Decisions
|
|
3
|
+
* ======================================================================
|
|
4
|
+
*
|
|
5
|
+
* PURPOSE: Provide an explainable, testable policy that decides whether a given
|
|
6
|
+
* task can be delegated to a local-worker profile (local-reader or local-editor)
|
|
7
|
+
* or must stay on the main agent.
|
|
8
|
+
*
|
|
9
|
+
* ARCHITECTURE:
|
|
10
|
+
* - This module is POLICY ONLY — it makes routing decisions but does NOT execute them
|
|
11
|
+
* - The main agent (or a delegation hook in a future phase) is responsible for
|
|
12
|
+
* actually routing the task based on the RoutingDecision returned here
|
|
13
|
+
* - All decisions are deterministic and based on structured input fields
|
|
14
|
+
* - No model inference, no learning, no dynamic adaptation
|
|
15
|
+
*
|
|
16
|
+
* TASK CLASSIFICATION TAXONOMY:
|
|
17
|
+
* reader_eligible — clearly suitable for local-reader
|
|
18
|
+
* editor_eligible — clearly suitable for local-editor
|
|
19
|
+
* high_entropy_disallowed — high-complexity tasks that must stay on main agent
|
|
20
|
+
* risk_disallowed — tasks with destructive or high-risk signals
|
|
21
|
+
* ambiguous_scope — tasks that are unclear and need main-agent judgment
|
|
22
|
+
* deployment_unavailable — no enabled deployment exists for the target profile
|
|
23
|
+
*
|
|
24
|
+
* FAIL-CLOSED PRINCIPLE:
|
|
25
|
+
* - When in doubt → stay_main
|
|
26
|
+
* - Unclear intent → stay_main
|
|
27
|
+
* - High complexity → stay_main
|
|
28
|
+
* - Any risk signal → stay_main
|
|
29
|
+
* - No enabled deployment → stay_main
|
|
30
|
+
*
|
|
31
|
+
* DESIGN CONSTRAINTS:
|
|
32
|
+
* - No actual task execution
|
|
33
|
+
* - No automatic learning or route optimization
|
|
34
|
+
* - No Trinity or adaptive threshold logic
|
|
35
|
+
* - Routing decisions are fully explainable (return `reason` + `blockers[]`)
|
|
36
|
+
*/
|
|
37
|
+
import type { WorkerProfile } from './model-deployment-registry.js';
|
|
38
|
+
/**
|
|
39
|
+
* The input contract for a routing decision.
|
|
40
|
+
* All fields are optional — the classifier handles missing data gracefully
|
|
41
|
+
* by treating it as ambiguous (stay_main).
|
|
42
|
+
*/
|
|
43
|
+
export interface RoutingInput {
|
|
44
|
+
/**
|
|
45
|
+
* A short label or name for the task intent.
|
|
46
|
+
* E.g., "read_file", "edit_config", "debug_memory_leak", "design_system"
|
|
47
|
+
*/
|
|
48
|
+
taskIntent?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Natural-language description of the task.
|
|
51
|
+
* The classifier examines this for keywords indicating complexity/risk.
|
|
52
|
+
*/
|
|
53
|
+
taskDescription?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Specific tools requested or implied by the task.
|
|
56
|
+
* These are examined for risk signals (e.g., bash, rm, git push).
|
|
57
|
+
*/
|
|
58
|
+
requestedTools?: string[];
|
|
59
|
+
/**
|
|
60
|
+
* Specific files involved or targeted.
|
|
61
|
+
* Examined for risk-path indicators (e.g., .git/, node_modules, production configs).
|
|
62
|
+
*/
|
|
63
|
+
requestedFiles?: string[];
|
|
64
|
+
/**
|
|
65
|
+
* Shape of expected output.
|
|
66
|
+
* E.g., "json", "markdown", "one_line", "full_report"
|
|
67
|
+
*/
|
|
68
|
+
expectedOutputShape?: string;
|
|
69
|
+
/**
|
|
70
|
+
* Explicit risk signals detected in the task.
|
|
71
|
+
* E.g., ["destructive", "production", "irreversible", "large_scale"]
|
|
72
|
+
* Any non-empty riskSignals → automatic stay_main.
|
|
73
|
+
*/
|
|
74
|
+
riskSignals?: string[];
|
|
75
|
+
/**
|
|
76
|
+
* Complexity hints for the task.
|
|
77
|
+
* E.g., ["multi_step", "cross_file", "ambiguous", "requires_planning"]
|
|
78
|
+
*/
|
|
79
|
+
complexityHints?: string[];
|
|
80
|
+
/**
|
|
81
|
+
* Target worker profile for routing consideration.
|
|
82
|
+
* If omitted, both profiles are evaluated and the best match is returned.
|
|
83
|
+
*/
|
|
84
|
+
targetProfile?: WorkerProfile;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The result of a routing classification decision.
|
|
88
|
+
* Always includes a `reason` and a `blockers` list for full explainability.
|
|
89
|
+
*/
|
|
90
|
+
export interface RoutingDecision {
|
|
91
|
+
/**
|
|
92
|
+
* The routing verdict.
|
|
93
|
+
* - `route_local` — the task may be delegated to `targetProfile`
|
|
94
|
+
* - `stay_main` — the task must remain on the main agent
|
|
95
|
+
*/
|
|
96
|
+
decision: 'route_local' | 'stay_main';
|
|
97
|
+
/**
|
|
98
|
+
* Which profile the task should be routed to (if decision === 'route_local').
|
|
99
|
+
* Null if decision === 'stay_main'.
|
|
100
|
+
*/
|
|
101
|
+
targetProfile: WorkerProfile | null;
|
|
102
|
+
/**
|
|
103
|
+
* The task classification category that led to this decision.
|
|
104
|
+
*/
|
|
105
|
+
classification: 'reader_eligible' | 'editor_eligible' | 'high_entropy_disallowed' | 'risk_disallowed' | 'ambiguous_scope' | 'profile_mismatch' | 'deployment_unavailable';
|
|
106
|
+
/**
|
|
107
|
+
* Human-readable explanation of the routing decision.
|
|
108
|
+
* Must be specific enough that a developer can understand why a task was accepted/rejected.
|
|
109
|
+
*/
|
|
110
|
+
reason: string;
|
|
111
|
+
/**
|
|
112
|
+
* List of specific reasons that blocked routing (if decision === 'stay_main').
|
|
113
|
+
* Empty if decision === 'route_local'.
|
|
114
|
+
*/
|
|
115
|
+
blockers: string[];
|
|
116
|
+
/**
|
|
117
|
+
* Whether a deployment check was performed and whether it passed.
|
|
118
|
+
* Useful for diagnostics when deployment_unavailable is the classification.
|
|
119
|
+
*/
|
|
120
|
+
deploymentCheck: {
|
|
121
|
+
performed: boolean;
|
|
122
|
+
profileAvailable: boolean;
|
|
123
|
+
routingEnabled: boolean;
|
|
124
|
+
/** Whether the active checkpoint is currently marked as deployable in the training registry. */
|
|
125
|
+
checkpointDeployable: boolean;
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* The active checkpoint ID that would be used for routing (if decision === 'route_local').
|
|
129
|
+
* This is the checkpoint from the deployment registry.
|
|
130
|
+
* Null if decision === 'stay_main' or if no checkpoint is active.
|
|
131
|
+
*
|
|
132
|
+
* USE FOR SHADOW OBSERVATIONS:
|
|
133
|
+
* When routing in shadow mode (checkpoint is in shadow_ready state),
|
|
134
|
+
* the caller should record a shadow observation using this checkpoint ID.
|
|
135
|
+
*/
|
|
136
|
+
activeCheckpointId: string | null;
|
|
137
|
+
/**
|
|
138
|
+
* The promotion state of the active checkpoint.
|
|
139
|
+
* Indicates whether this is a regular deployment or a shadow rollout.
|
|
140
|
+
* Useful for determining whether to record shadow observations.
|
|
141
|
+
*/
|
|
142
|
+
activeCheckpointState?: 'promotable' | 'shadow_ready' | 'candidate_only';
|
|
143
|
+
/**
|
|
144
|
+
* Deprecated: runtime shadow observations are now recorded from real
|
|
145
|
+
* subagent lifecycle hooks instead of from classifyTask().
|
|
146
|
+
*/
|
|
147
|
+
shadowObservationId?: string;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Classify a task and produce a routing decision.
|
|
151
|
+
*
|
|
152
|
+
* This is the main entry point for routing policy evaluation.
|
|
153
|
+
* It:
|
|
154
|
+
* 1. Classifies the task kind based on keywords and heuristics
|
|
155
|
+
* 2. Checks deployment availability for the target profile
|
|
156
|
+
* 3. Returns a fully explainable RoutingDecision
|
|
157
|
+
*
|
|
158
|
+
* @param input - The routing input describing the task
|
|
159
|
+
* @param stateDir - Workspace state directory (for deployment registry lookup)
|
|
160
|
+
* @returns RoutingDecision with classification, reason, blockers, and routing verdict
|
|
161
|
+
*/
|
|
162
|
+
export declare function classifyTask(input: RoutingInput, stateDir: string): RoutingDecision;
|
|
163
|
+
/**
|
|
164
|
+
* Convenience: check if a specific profile can handle a task.
|
|
165
|
+
* Equivalent to calling classifyTask with targetProfile set.
|
|
166
|
+
*/
|
|
167
|
+
export declare function canRouteToProfile(input: RoutingInput, stateDir: string, profile: WorkerProfile): boolean;
|
|
168
|
+
/**
|
|
169
|
+
* Check if any local worker routing is currently enabled for any profile.
|
|
170
|
+
*/
|
|
171
|
+
export declare function isAnyLocalRoutingEnabled(stateDir: string): boolean;
|
|
172
|
+
/**
|
|
173
|
+
* List all profiles that currently have routing enabled.
|
|
174
|
+
*/
|
|
175
|
+
export declare function listEnabledProfiles(stateDir: string): WorkerProfile[];
|