principles-disciple 1.7.6 → 1.7.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/context.js +5 -15
- package/dist/commands/evolution-status.js +2 -9
- 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/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/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/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 +95 -1
- package/dist/core/trajectory.js +220 -6
- package/dist/core/workspace-context.d.ts +0 -6
- package/dist/core/workspace-context.js +0 -12
- package/dist/hooks/bash-risk.d.ts +6 -6
- package/dist/hooks/bash-risk.js +8 -8
- package/dist/hooks/gate-block-helper.js +1 -1
- package/dist/hooks/gate.d.ts +1 -1
- package/dist/hooks/gate.js +2 -2
- package/dist/hooks/gfi-gate.d.ts +3 -3
- package/dist/hooks/gfi-gate.js +15 -14
- package/dist/hooks/pain.js +6 -9
- package/dist/hooks/progressive-trust-gate.d.ts +21 -49
- package/dist/hooks/progressive-trust-gate.js +51 -204
- package/dist/hooks/prompt.d.ts +11 -11
- package/dist/hooks/prompt.js +158 -72
- package/dist/hooks/subagent.js +43 -6
- package/dist/i18n/commands.js +8 -8
- package/dist/index.js +129 -28
- package/dist/service/evolution-worker.d.ts +42 -4
- package/dist/service/evolution-worker.js +321 -13
- 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 +2 -23
- package/dist/service/phase3-input-filter.js +3 -27
- package/dist/service/runtime-summary-service.d.ts +0 -10
- package/dist/service/runtime-summary-service.js +1 -54
- package/dist/tools/deep-reflect.js +2 -1
- package/dist/types/event-types.d.ts +2 -10
- package/dist/types/runtime-summary.d.ts +1 -8
- 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,787 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nocturnal Service — Trinity Reflection Pipeline Orchestrator
|
|
3
|
+
* ============================================================
|
|
4
|
+
*
|
|
5
|
+
* PURPOSE: Orchestrate the complete nocturnal reflection pipeline:
|
|
6
|
+
* 1. Workspace idle check
|
|
7
|
+
* 2. Target selection (principle + session)
|
|
8
|
+
* 3. Trajectory snapshot extraction
|
|
9
|
+
* 4. Trinity artifact generation (Dreamer -> Philosopher -> Scribe)
|
|
10
|
+
* OR single-reflector fallback (if Trinity disabled or fails)
|
|
11
|
+
* 5. Arbiter validation
|
|
12
|
+
* 6. Executability check
|
|
13
|
+
* 7. Artifact persistence
|
|
14
|
+
* 8. Cooldown recording
|
|
15
|
+
*
|
|
16
|
+
* DESIGN CONSTRAINTS (Phase 6):
|
|
17
|
+
* - Trinity is configurable (useTrinity flag)
|
|
18
|
+
* - Single-reflector fallback preserved if Trinity fails
|
|
19
|
+
* - All stage I/O is structured JSON contracts
|
|
20
|
+
* - Any malformed stage output fails the entire chain closed
|
|
21
|
+
* - Final artifact still passes arbiter + executability validation
|
|
22
|
+
* - Telemetry records chain mode, stage outcomes, candidate counts
|
|
23
|
+
* - No real training export (Phase 3+ only)
|
|
24
|
+
* - No auto-deployment
|
|
25
|
+
* - Approved artifacts go to .state/nocturnal/samples/{artifactId}.json
|
|
26
|
+
* - Cooldown recorded via nocturnal-runtime.ts
|
|
27
|
+
*
|
|
28
|
+
* THIS IS THE MAIN ORCHESTRATOR — all other nocturnal modules are called from here.
|
|
29
|
+
*/
|
|
30
|
+
import * as fs from 'fs';
|
|
31
|
+
import * as path from 'path';
|
|
32
|
+
import { randomUUID } from 'crypto';
|
|
33
|
+
import { createNocturnalTrajectoryExtractor, computeThinkingModelDelta, } from '../core/nocturnal-trajectory-extractor.js';
|
|
34
|
+
import { NocturnalTargetSelector, } from './nocturnal-target-selector.js';
|
|
35
|
+
import { parseAndValidateArtifact, validateTrinityDraft, } from '../core/nocturnal-arbiter.js';
|
|
36
|
+
import { draftToArtifact, runTrinity, runTrinityAsync, DEFAULT_TRINITY_CONFIG, } from '../core/nocturnal-trinity.js';
|
|
37
|
+
import { validateExecutability, } from '../core/nocturnal-executability.js';
|
|
38
|
+
import { adjustThresholdsFromSignals, } from '../core/adaptive-thresholds.js';
|
|
39
|
+
import { checkPreflight, recordRunStart, recordRunEnd, } from './nocturnal-runtime.js';
|
|
40
|
+
import { NocturnalPathResolver } from '../core/nocturnal-paths.js';
|
|
41
|
+
import { registerSample } from '../core/nocturnal-dataset.js';
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
// Stub Reflector (Phase 2 — no real subagent calls)
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
/**
|
|
46
|
+
* STUB REFLECTOR — Phase 2 MVP only.
|
|
47
|
+
*
|
|
48
|
+
* This does NOT call a real subagent. Instead, it generates a plausible
|
|
49
|
+
* artifact for testing purposes. The artifact structure is correct and
|
|
50
|
+
* passes arbiter validation, but the content is synthetic.
|
|
51
|
+
*
|
|
52
|
+
* In Phase 3, this will be replaced with real subagent invocation.
|
|
53
|
+
*/
|
|
54
|
+
function invokeStubReflector(snapshot, principleId) {
|
|
55
|
+
const artifactId = randomUUID();
|
|
56
|
+
const now = new Date().toISOString();
|
|
57
|
+
// Build a plausible bad/better decision pair based on available snapshot data.
|
|
58
|
+
// This is synthetic — real reflection would come from subagent analysis.
|
|
59
|
+
const hasFailures = snapshot.stats.failureCount > 0;
|
|
60
|
+
const hasPain = snapshot.stats.totalPainEvents > 0;
|
|
61
|
+
const hasGateBlocks = snapshot.stats.totalGateBlocks > 0;
|
|
62
|
+
// Detect what kind of signal is available and craft appropriate artifact
|
|
63
|
+
let badDecision;
|
|
64
|
+
let betterDecision;
|
|
65
|
+
let rationale;
|
|
66
|
+
if (hasGateBlocks) {
|
|
67
|
+
badDecision = `Proceeded with a tool call despite receiving a gate block, bypassing the safety check`;
|
|
68
|
+
betterDecision = `Read the blocked operation documentation and obtained proper authorization before retrying the operation`;
|
|
69
|
+
rationale = `Respecting gate blocks prevents unintended system modifications and ensures alignment with operational constraints`;
|
|
70
|
+
}
|
|
71
|
+
else if (hasPain) {
|
|
72
|
+
badDecision = `Continued executing operations without pausing to address accumulated pain signals`;
|
|
73
|
+
betterDecision = `Let me stop and reconsider when pain signals accumulate — the error tells us something needs fixing first`;
|
|
74
|
+
rationale = `Pain signals indicate accumulated friction or error conditions that should be addressed before continuing`;
|
|
75
|
+
}
|
|
76
|
+
else if (hasFailures) {
|
|
77
|
+
badDecision = `Retried a failing operation without diagnosing the root cause of the failure`;
|
|
78
|
+
betterDecision = `Based on the evidence from the error logs, let me first check the actual source code to understand the precondition before retrying`;
|
|
79
|
+
rationale = `Diagnosing failures before retry prevents repeated failures and respects the cost of each action attempt`;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
badDecision = `Proceeded with an operation without verifying preconditions or checking for conflicting changes`;
|
|
83
|
+
betterDecision = `Let me first understand the current state of the codebase by reading the relevant files before making any changes`;
|
|
84
|
+
rationale = `Verifying preconditions and current state prevents errors and ensures actions are appropriate for the actual situation`;
|
|
85
|
+
}
|
|
86
|
+
// Compute design-alignment reflection quality metrics
|
|
87
|
+
const thinkingModelDelta = computeThinkingModelDelta(badDecision, betterDecision);
|
|
88
|
+
// Stub reflectors don't have an improved snapshot, so planningRatioGain is 0
|
|
89
|
+
const planningRatioGain = 0;
|
|
90
|
+
const artifact = {
|
|
91
|
+
artifactId,
|
|
92
|
+
sessionId: snapshot.sessionId,
|
|
93
|
+
principleId,
|
|
94
|
+
sourceSnapshotRef: `snapshot-${snapshot.sessionId}-${Date.now()}`,
|
|
95
|
+
badDecision,
|
|
96
|
+
betterDecision,
|
|
97
|
+
rationale,
|
|
98
|
+
createdAt: now,
|
|
99
|
+
thinkingModelDelta,
|
|
100
|
+
planningRatioGain,
|
|
101
|
+
};
|
|
102
|
+
return JSON.stringify(artifact);
|
|
103
|
+
}
|
|
104
|
+
// ---------------------------------------------------------------------------
|
|
105
|
+
// Artifact Persistence
|
|
106
|
+
// ---------------------------------------------------------------------------
|
|
107
|
+
/**
|
|
108
|
+
* Persist an approved artifact to the samples directory.
|
|
109
|
+
* Returns the absolute path where the artifact was saved.
|
|
110
|
+
*/
|
|
111
|
+
function persistArtifact(workspaceDir, artifact) {
|
|
112
|
+
const artifactPath = NocturnalPathResolver.samplePath(workspaceDir, artifact.artifactId);
|
|
113
|
+
const sampleRecord = {
|
|
114
|
+
...artifact,
|
|
115
|
+
status: 'approved',
|
|
116
|
+
boundedAction: artifact.boundedAction,
|
|
117
|
+
persistedAt: new Date().toISOString(),
|
|
118
|
+
};
|
|
119
|
+
// Ensure directory exists
|
|
120
|
+
const dir = path.dirname(artifactPath);
|
|
121
|
+
if (!fs.existsSync(dir)) {
|
|
122
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
123
|
+
}
|
|
124
|
+
fs.writeFileSync(artifactPath, JSON.stringify(sampleRecord, null, 2), 'utf-8');
|
|
125
|
+
return artifactPath;
|
|
126
|
+
}
|
|
127
|
+
// ---------------------------------------------------------------------------
|
|
128
|
+
// Main Orchestrator
|
|
129
|
+
// ---------------------------------------------------------------------------
|
|
130
|
+
/**
|
|
131
|
+
* Execute a complete nocturnal reflection run.
|
|
132
|
+
*
|
|
133
|
+
* Pipeline:
|
|
134
|
+
* 1. Pre-flight check (idle + cooldown + quota)
|
|
135
|
+
* 2. Target selection (principle + violating session)
|
|
136
|
+
* 3. Trajectory snapshot extraction
|
|
137
|
+
* 4. Reflector (stub) → JSON artifact
|
|
138
|
+
* 5. Arbiter validation
|
|
139
|
+
* 6. Executability check
|
|
140
|
+
* 7. Artifact persistence
|
|
141
|
+
* 8. Cooldown recording
|
|
142
|
+
*
|
|
143
|
+
* @param workspaceDir - Workspace directory
|
|
144
|
+
* @param stateDir - State directory
|
|
145
|
+
* @param options - Service configuration options
|
|
146
|
+
* @returns NocturnalRunResult
|
|
147
|
+
*/
|
|
148
|
+
export function executeNocturnalReflection(workspaceDir, stateDir, options = {}) {
|
|
149
|
+
const diagnostics = {
|
|
150
|
+
preflight: null,
|
|
151
|
+
selection: null,
|
|
152
|
+
idle: null,
|
|
153
|
+
trinityAttempted: false,
|
|
154
|
+
trinityResult: null,
|
|
155
|
+
chainModeUsed: null,
|
|
156
|
+
arbiterResult: null,
|
|
157
|
+
executabilityResult: null,
|
|
158
|
+
persisted: false,
|
|
159
|
+
};
|
|
160
|
+
// -------------------------------------------------------------------------
|
|
161
|
+
// Step 1: Pre-flight check
|
|
162
|
+
// -------------------------------------------------------------------------
|
|
163
|
+
const preflight = checkPreflight(workspaceDir, stateDir, undefined, // principleId
|
|
164
|
+
undefined, // trajectoryLastActivityAt
|
|
165
|
+
options.idleCheckOverride);
|
|
166
|
+
diagnostics.preflight = preflight;
|
|
167
|
+
if (!preflight.canRun) {
|
|
168
|
+
return {
|
|
169
|
+
success: false,
|
|
170
|
+
noTargetSelected: false,
|
|
171
|
+
validationFailed: false,
|
|
172
|
+
validationFailures: [],
|
|
173
|
+
diagnostics,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
// -------------------------------------------------------------------------
|
|
177
|
+
// Step 2: Target selection
|
|
178
|
+
// -------------------------------------------------------------------------
|
|
179
|
+
const extractor = createNocturnalTrajectoryExtractor(workspaceDir, stateDir);
|
|
180
|
+
const selector = new NocturnalTargetSelector(workspaceDir, stateDir, extractor, {
|
|
181
|
+
idleCheckOverride: options.idleCheckOverride,
|
|
182
|
+
recentPainContext: options.painContext,
|
|
183
|
+
});
|
|
184
|
+
const selection = selector.select();
|
|
185
|
+
diagnostics.selection = selection;
|
|
186
|
+
if (selection.decision === 'skip') {
|
|
187
|
+
return {
|
|
188
|
+
success: false,
|
|
189
|
+
noTargetSelected: true,
|
|
190
|
+
skipReason: selection.skipReason,
|
|
191
|
+
validationFailed: false,
|
|
192
|
+
validationFailures: [],
|
|
193
|
+
diagnostics,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
const { selectedPrincipleId, selectedSessionId } = selection;
|
|
197
|
+
// -------------------------------------------------------------------------
|
|
198
|
+
// Step 3: Trajectory snapshot extraction
|
|
199
|
+
// -------------------------------------------------------------------------
|
|
200
|
+
if (!selectedPrincipleId || !selectedSessionId) {
|
|
201
|
+
return {
|
|
202
|
+
success: false,
|
|
203
|
+
noTargetSelected: true,
|
|
204
|
+
validationFailed: false,
|
|
205
|
+
validationFailures: [],
|
|
206
|
+
diagnostics,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
const snapshot = extractor.getNocturnalSessionSnapshot(selectedSessionId);
|
|
210
|
+
if (!snapshot) {
|
|
211
|
+
return {
|
|
212
|
+
success: false,
|
|
213
|
+
noTargetSelected: true,
|
|
214
|
+
skipReason: 'insufficient_snapshot_data',
|
|
215
|
+
validationFailed: false,
|
|
216
|
+
validationFailures: [],
|
|
217
|
+
diagnostics,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
diagnostics.idle = { isIdle: true, mostRecentActivityAt: 0, idleForMs: 0, activeSessionCount: 0, abandonedSessionIds: [], trajectoryGuardrailConfirmsIdle: true, reason: 'preflight passed' };
|
|
221
|
+
// -------------------------------------------------------------------------
|
|
222
|
+
// Step 4: Record run start (begin cooldown window)
|
|
223
|
+
// -------------------------------------------------------------------------
|
|
224
|
+
// Note: We use a sync approximation here since this is called from sync context
|
|
225
|
+
// The async version would be used in real worker integration
|
|
226
|
+
void recordRunStart(stateDir, selectedPrincipleId).catch((err) => {
|
|
227
|
+
console.warn(`[nocturnal-service] Failed to record run start: ${String(err)}`);
|
|
228
|
+
});
|
|
229
|
+
// -------------------------------------------------------------------------
|
|
230
|
+
// Step 5: Artifact generation (Trinity or single-reflector)
|
|
231
|
+
// -------------------------------------------------------------------------
|
|
232
|
+
let trinityArtifact = null;
|
|
233
|
+
let trinityResult = null;
|
|
234
|
+
let rawJson;
|
|
235
|
+
let chainModeUsed = 'single-reflector';
|
|
236
|
+
if (options.skipReflector) {
|
|
237
|
+
// Caller provided explicit artifact — used for testing arbiter/executability
|
|
238
|
+
if (!options.reflectorOutputOverride) {
|
|
239
|
+
return {
|
|
240
|
+
success: false,
|
|
241
|
+
noTargetSelected: false,
|
|
242
|
+
validationFailed: true,
|
|
243
|
+
validationFailures: ['skipReflector is true but no reflectorOutputOverride provided'],
|
|
244
|
+
diagnostics,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
rawJson = options.reflectorOutputOverride;
|
|
248
|
+
}
|
|
249
|
+
else if (options.trinityResultOverride) {
|
|
250
|
+
// Testing override — use provided Trinity result
|
|
251
|
+
trinityResult = options.trinityResultOverride;
|
|
252
|
+
diagnostics.trinityAttempted = true;
|
|
253
|
+
diagnostics.trinityResult = trinityResult;
|
|
254
|
+
diagnostics.chainModeUsed = trinityResult.success ? 'trinity' : 'single-reflector';
|
|
255
|
+
chainModeUsed = trinityResult.success ? 'trinity' : 'single-reflector';
|
|
256
|
+
if (!trinityResult.success) {
|
|
257
|
+
// Trinity failed — fail closed (same semantics as production)
|
|
258
|
+
const failures = trinityResult.failures.map((f) => `${f.stage}: ${f.reason}`);
|
|
259
|
+
void recordRunEnd(stateDir, 'failed', { reason: `Trinity override failed: ${failures.join('; ')}` }).catch((err) => {
|
|
260
|
+
console.warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
261
|
+
});
|
|
262
|
+
// Emit threshold signals: malformed Trinity override is a strong signal
|
|
263
|
+
adjustThresholdsFromSignals(stateDir, {
|
|
264
|
+
malformedRate: 1.0,
|
|
265
|
+
arbiterRejectRate: 0.0,
|
|
266
|
+
executabilityRejectRate: 0.0,
|
|
267
|
+
qualityDelta: 0.0,
|
|
268
|
+
});
|
|
269
|
+
return {
|
|
270
|
+
success: false,
|
|
271
|
+
noTargetSelected: false,
|
|
272
|
+
validationFailed: true,
|
|
273
|
+
validationFailures: [`Trinity override failed: ${failures.join('; ')}`],
|
|
274
|
+
snapshot,
|
|
275
|
+
diagnostics,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
// Validate Trinity draft
|
|
280
|
+
const draftValidation = validateTrinityDraft(trinityResult.artifact);
|
|
281
|
+
if (!draftValidation.valid) {
|
|
282
|
+
const failures = draftValidation.failures;
|
|
283
|
+
void recordRunEnd(stateDir, 'failed', { reason: `Trinity draft invalid: ${failures.join('; ')}` }).catch((err) => {
|
|
284
|
+
console.warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
285
|
+
});
|
|
286
|
+
// Emit threshold signals: malformed draft content is a strong signal
|
|
287
|
+
adjustThresholdsFromSignals(stateDir, {
|
|
288
|
+
malformedRate: 1.0,
|
|
289
|
+
arbiterRejectRate: 0.0,
|
|
290
|
+
executabilityRejectRate: 0.0,
|
|
291
|
+
qualityDelta: 0.0,
|
|
292
|
+
});
|
|
293
|
+
return {
|
|
294
|
+
success: false,
|
|
295
|
+
noTargetSelected: false,
|
|
296
|
+
validationFailed: true,
|
|
297
|
+
validationFailures: failures,
|
|
298
|
+
snapshot,
|
|
299
|
+
diagnostics,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
trinityArtifact = trinityResult.artifact;
|
|
303
|
+
// Convert Trinity draft to arbiter-compatible artifact
|
|
304
|
+
const artifactData = draftToArtifact(trinityArtifact);
|
|
305
|
+
rawJson = JSON.stringify(artifactData);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
// Normal execution: try Trinity first, fall back to single-reflector
|
|
310
|
+
const trinityConfig = {
|
|
311
|
+
...DEFAULT_TRINITY_CONFIG,
|
|
312
|
+
...options.trinityConfig,
|
|
313
|
+
stateDir, // Enable threshold loading/persistence
|
|
314
|
+
};
|
|
315
|
+
// If useStubs=false but no runtimeAdapter provided in sync context,
|
|
316
|
+
// fall back to stub behavior (graceful degradation).
|
|
317
|
+
// For real async execution, use executeNocturnalReflectionAsync with a runtimeAdapter.
|
|
318
|
+
const effectiveConfig = trinityConfig.useTrinity && !trinityConfig.useStubs && !options.runtimeAdapter
|
|
319
|
+
? { ...trinityConfig, useStubs: true }
|
|
320
|
+
: trinityConfig;
|
|
321
|
+
if (effectiveConfig.useTrinity) {
|
|
322
|
+
diagnostics.trinityAttempted = true;
|
|
323
|
+
trinityResult = runTrinity({ snapshot, principleId: selectedPrincipleId, config: effectiveConfig });
|
|
324
|
+
diagnostics.trinityResult = trinityResult;
|
|
325
|
+
diagnostics.chainModeUsed = trinityResult.success ? 'trinity' : 'single-reflector';
|
|
326
|
+
chainModeUsed = trinityResult.success ? 'trinity' : 'single-reflector';
|
|
327
|
+
if (trinityResult.success) {
|
|
328
|
+
// Validate Trinity draft
|
|
329
|
+
const draftValidation = validateTrinityDraft(trinityResult.artifact);
|
|
330
|
+
if (!draftValidation.valid) {
|
|
331
|
+
// Trinity draft invalid — fail closed
|
|
332
|
+
const failures = draftValidation.failures;
|
|
333
|
+
void recordRunEnd(stateDir, 'failed', { reason: `Trinity draft invalid: ${failures.join('; ')}` }).catch((err) => {
|
|
334
|
+
console.warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
335
|
+
});
|
|
336
|
+
// Emit threshold signals: malformed draft content is a strong signal
|
|
337
|
+
adjustThresholdsFromSignals(stateDir, {
|
|
338
|
+
malformedRate: 1.0,
|
|
339
|
+
arbiterRejectRate: 0.0,
|
|
340
|
+
executabilityRejectRate: 0.0,
|
|
341
|
+
qualityDelta: 0.0,
|
|
342
|
+
});
|
|
343
|
+
return {
|
|
344
|
+
success: false,
|
|
345
|
+
noTargetSelected: false,
|
|
346
|
+
validationFailed: true,
|
|
347
|
+
validationFailures: failures,
|
|
348
|
+
snapshot,
|
|
349
|
+
diagnostics,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
trinityArtifact = trinityResult.artifact;
|
|
353
|
+
// Convert Trinity draft to arbiter-compatible artifact
|
|
354
|
+
const artifactData = draftToArtifact(trinityArtifact);
|
|
355
|
+
rawJson = JSON.stringify(artifactData);
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
// Trinity failed — fail closed (do NOT fall back to single-reflector)
|
|
359
|
+
// Phase 6 requirement: malformed Trinity stage output fails closed
|
|
360
|
+
const failures = trinityResult.failures.map((f) => `${f.stage}: ${f.reason}`);
|
|
361
|
+
void recordRunEnd(stateDir, 'failed', { reason: `Trinity chain failed: ${failures.join('; ')}` }).catch((err) => {
|
|
362
|
+
console.warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
363
|
+
});
|
|
364
|
+
// Emit threshold signals: malformed Trinity is the strongest signal for tightening schema threshold
|
|
365
|
+
adjustThresholdsFromSignals(stateDir, {
|
|
366
|
+
malformedRate: 1.0,
|
|
367
|
+
arbiterRejectRate: 0.0,
|
|
368
|
+
executabilityRejectRate: 0.0,
|
|
369
|
+
qualityDelta: 0.0,
|
|
370
|
+
});
|
|
371
|
+
return {
|
|
372
|
+
success: false,
|
|
373
|
+
noTargetSelected: false,
|
|
374
|
+
validationFailed: true,
|
|
375
|
+
validationFailures: [`Trinity chain failed: ${failures.join('; ')}`],
|
|
376
|
+
snapshot,
|
|
377
|
+
diagnostics,
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
else {
|
|
382
|
+
// Trinity disabled — use single-reflector directly
|
|
383
|
+
rawJson = invokeStubReflector(snapshot, selectedPrincipleId);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
// -------------------------------------------------------------------------
|
|
387
|
+
// Step 6: Arbiter validation
|
|
388
|
+
// -------------------------------------------------------------------------
|
|
389
|
+
const arbiterResult = parseAndValidateArtifact(rawJson, {
|
|
390
|
+
expectedPrincipleId: selectedPrincipleId,
|
|
391
|
+
expectedSessionId: selectedSessionId,
|
|
392
|
+
qualityThresholds: {
|
|
393
|
+
thinkingModelDeltaMin: 0.01,
|
|
394
|
+
planningRatioGainMin: -0.5,
|
|
395
|
+
},
|
|
396
|
+
});
|
|
397
|
+
diagnostics.arbiterResult = arbiterResult;
|
|
398
|
+
if (!arbiterResult.passed || !arbiterResult.artifact) {
|
|
399
|
+
const failures = arbiterResult.failures.map((f) => f.reason);
|
|
400
|
+
void recordRunEnd(stateDir, 'failed', { reason: failures.join('; ') }).catch((err) => {
|
|
401
|
+
console.warn(`[nocturnal-service] Failed to record run end (arbiter failed): ${String(err)}`);
|
|
402
|
+
});
|
|
403
|
+
// Emit threshold signals: arbiter rejection indicates principle alignment issues
|
|
404
|
+
adjustThresholdsFromSignals(stateDir, {
|
|
405
|
+
malformedRate: 0.0,
|
|
406
|
+
arbiterRejectRate: 1.0,
|
|
407
|
+
executabilityRejectRate: 0.0,
|
|
408
|
+
qualityDelta: 0.0,
|
|
409
|
+
});
|
|
410
|
+
return {
|
|
411
|
+
success: false,
|
|
412
|
+
noTargetSelected: false,
|
|
413
|
+
validationFailed: true,
|
|
414
|
+
validationFailures: failures,
|
|
415
|
+
diagnostics,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
// -------------------------------------------------------------------------
|
|
419
|
+
// Step 7: Executability check
|
|
420
|
+
// -------------------------------------------------------------------------
|
|
421
|
+
const execResult = validateExecutability(arbiterResult.artifact);
|
|
422
|
+
if (!execResult.executable) {
|
|
423
|
+
const failures = execResult.failures.map((f) => f.reason);
|
|
424
|
+
void recordRunEnd(stateDir, 'failed', { reason: failures.join('; ') }).catch((err) => {
|
|
425
|
+
console.warn(`[nocturnal-service] Failed to record run end (executability failed): ${String(err)}`);
|
|
426
|
+
});
|
|
427
|
+
// Emit threshold signals: executability rejection indicates action quality issues
|
|
428
|
+
adjustThresholdsFromSignals(stateDir, {
|
|
429
|
+
malformedRate: 0.0,
|
|
430
|
+
arbiterRejectRate: 0.0,
|
|
431
|
+
executabilityRejectRate: 1.0,
|
|
432
|
+
qualityDelta: 0.0,
|
|
433
|
+
});
|
|
434
|
+
return {
|
|
435
|
+
success: false,
|
|
436
|
+
noTargetSelected: false,
|
|
437
|
+
validationFailed: true,
|
|
438
|
+
validationFailures: failures,
|
|
439
|
+
diagnostics,
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
diagnostics.executabilityResult = { executable: true, failures: [] };
|
|
443
|
+
// -------------------------------------------------------------------------
|
|
444
|
+
// Step 8: Persist artifact
|
|
445
|
+
// -------------------------------------------------------------------------
|
|
446
|
+
const artifactWithBoundedAction = {
|
|
447
|
+
...arbiterResult.artifact,
|
|
448
|
+
boundedAction: execResult.boundedAction,
|
|
449
|
+
};
|
|
450
|
+
let persistedPath;
|
|
451
|
+
try {
|
|
452
|
+
persistedPath = persistArtifact(workspaceDir, artifactWithBoundedAction);
|
|
453
|
+
diagnostics.persisted = true;
|
|
454
|
+
diagnostics.persistedPath = persistedPath;
|
|
455
|
+
}
|
|
456
|
+
catch (err) {
|
|
457
|
+
void recordRunEnd(stateDir, 'failed', { reason: `persistence error: ${String(err)}` }).catch((e) => {
|
|
458
|
+
console.warn(`[nocturnal-service] Failed to record run end (persistence failed): ${String(e)}`);
|
|
459
|
+
});
|
|
460
|
+
return {
|
|
461
|
+
success: false,
|
|
462
|
+
noTargetSelected: false,
|
|
463
|
+
validationFailed: true,
|
|
464
|
+
validationFailures: [`Failed to persist artifact: ${String(err)}`],
|
|
465
|
+
snapshot,
|
|
466
|
+
diagnostics,
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
// -------------------------------------------------------------------------
|
|
470
|
+
// Step 8b: Register in dataset lineage store (Phase 3 review gate)
|
|
471
|
+
// -------------------------------------------------------------------------
|
|
472
|
+
// Approved artifacts must enter the dataset registry so they can be reviewed
|
|
473
|
+
// before export. Without this, new samples never appear in the review queue.
|
|
474
|
+
try {
|
|
475
|
+
registerSample(workspaceDir, arbiterResult.artifact, persistedPath, null);
|
|
476
|
+
}
|
|
477
|
+
catch (err) {
|
|
478
|
+
// Non-fatal: artifact is persisted, registry is secondary.
|
|
479
|
+
// Log but don't fail the run.
|
|
480
|
+
console.warn(`[nocturnal-service] Failed to register sample in dataset registry: ${String(err)}`);
|
|
481
|
+
}
|
|
482
|
+
// -------------------------------------------------------------------------
|
|
483
|
+
// Step 9: Record run success
|
|
484
|
+
// -------------------------------------------------------------------------
|
|
485
|
+
void recordRunEnd(stateDir, 'success', { sampleCount: 1 }).catch((err) => {
|
|
486
|
+
console.warn(`[nocturnal-service] Failed to record run end (success): ${String(err)}`);
|
|
487
|
+
});
|
|
488
|
+
// -------------------------------------------------------------------------
|
|
489
|
+
// Step 10: Adaptive threshold adjustment based on run signals
|
|
490
|
+
// -------------------------------------------------------------------------
|
|
491
|
+
// Compute signals from this run's outcomes and adjust thresholds if needed
|
|
492
|
+
const malformedRate = trinityResult && !trinityResult.success ? 1.0 : 0.0;
|
|
493
|
+
const arbiterRejectRate = !arbiterResult.passed ? 1.0 : 0.0;
|
|
494
|
+
const executabilityRejectRate = !execResult.executable ? 1.0 : 0.0;
|
|
495
|
+
// qualityDelta requires reviewed-subset comparison infrastructure (Phase 7+)
|
|
496
|
+
const qualityDelta = 0.0;
|
|
497
|
+
const signals = {
|
|
498
|
+
malformedRate,
|
|
499
|
+
arbiterRejectRate,
|
|
500
|
+
executabilityRejectRate,
|
|
501
|
+
qualityDelta,
|
|
502
|
+
};
|
|
503
|
+
// Apply threshold adjustments based on run signals (fire-and-forget, non-blocking)
|
|
504
|
+
// Note: adjustThresholdsFromSignals is synchronous, so no .catch() needed
|
|
505
|
+
adjustThresholdsFromSignals(stateDir, signals);
|
|
506
|
+
return {
|
|
507
|
+
success: true,
|
|
508
|
+
artifact: artifactWithBoundedAction,
|
|
509
|
+
noTargetSelected: false,
|
|
510
|
+
validationFailed: false,
|
|
511
|
+
validationFailures: [],
|
|
512
|
+
snapshot,
|
|
513
|
+
diagnostics,
|
|
514
|
+
trinityTelemetry: trinityResult?.telemetry,
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
// ---------------------------------------------------------------------------
|
|
518
|
+
// Convenience function for async contexts (e.g., worker integration)
|
|
519
|
+
// ---------------------------------------------------------------------------
|
|
520
|
+
/**
|
|
521
|
+
* Async wrapper for executeNocturnalReflection.
|
|
522
|
+
* When runtimeAdapter is provided in options, uses runTrinityAsync for real subagent execution.
|
|
523
|
+
* Otherwise falls back to synchronous executeNocturnalReflection.
|
|
524
|
+
*/
|
|
525
|
+
export async function executeNocturnalReflectionAsync(workspaceDir, stateDir, options = {}) {
|
|
526
|
+
// If no runtime adapter and no trinityConfig.override, use sync path
|
|
527
|
+
if (!options.runtimeAdapter && !options.trinityConfig?.useStubs) {
|
|
528
|
+
// Sync path with default config (useStubs=false but no adapter = fail)
|
|
529
|
+
// Fall through to sync wrapper
|
|
530
|
+
return Promise.resolve(executeNocturnalReflection(workspaceDir, stateDir, options));
|
|
531
|
+
}
|
|
532
|
+
// If runtime adapter is provided, use async Trinity path
|
|
533
|
+
if (options.runtimeAdapter) {
|
|
534
|
+
return executeNocturnalReflectionWithAdapter(workspaceDir, stateDir, options);
|
|
535
|
+
}
|
|
536
|
+
// Sync path (useStubs=true or other sync options)
|
|
537
|
+
return Promise.resolve(executeNocturnalReflection(workspaceDir, stateDir, options));
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Execute nocturnal reflection with real Trinity runtime adapter (async).
|
|
541
|
+
* This handles the full pipeline with async Trinity stage execution.
|
|
542
|
+
*/
|
|
543
|
+
async function executeNocturnalReflectionWithAdapter(workspaceDir, stateDir, options) {
|
|
544
|
+
const diagnostics = {
|
|
545
|
+
preflight: null,
|
|
546
|
+
selection: null,
|
|
547
|
+
idle: null,
|
|
548
|
+
trinityAttempted: false,
|
|
549
|
+
trinityResult: null,
|
|
550
|
+
chainModeUsed: null,
|
|
551
|
+
arbiterResult: null,
|
|
552
|
+
executabilityResult: null,
|
|
553
|
+
persisted: false,
|
|
554
|
+
};
|
|
555
|
+
// Step 1: Pre-flight check
|
|
556
|
+
const preflight = checkPreflight(workspaceDir, stateDir, undefined, undefined, options.idleCheckOverride);
|
|
557
|
+
diagnostics.preflight = preflight;
|
|
558
|
+
if (!preflight.canRun) {
|
|
559
|
+
return { success: false, noTargetSelected: false, validationFailed: false, validationFailures: [], diagnostics };
|
|
560
|
+
}
|
|
561
|
+
// Step 2: Target selection
|
|
562
|
+
const extractor = createNocturnalTrajectoryExtractor(workspaceDir, stateDir);
|
|
563
|
+
const selector = new NocturnalTargetSelector(workspaceDir, stateDir, extractor, {
|
|
564
|
+
idleCheckOverride: options.idleCheckOverride,
|
|
565
|
+
recentPainContext: options.painContext,
|
|
566
|
+
});
|
|
567
|
+
const selection = selector.select();
|
|
568
|
+
diagnostics.selection = selection;
|
|
569
|
+
if (selection.decision === 'skip') {
|
|
570
|
+
return {
|
|
571
|
+
success: false,
|
|
572
|
+
noTargetSelected: true,
|
|
573
|
+
skipReason: selection.skipReason,
|
|
574
|
+
validationFailed: false,
|
|
575
|
+
validationFailures: [],
|
|
576
|
+
diagnostics,
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
const { selectedPrincipleId, selectedSessionId } = selection;
|
|
580
|
+
if (!selectedPrincipleId || !selectedSessionId) {
|
|
581
|
+
return {
|
|
582
|
+
success: false,
|
|
583
|
+
noTargetSelected: true,
|
|
584
|
+
validationFailed: false,
|
|
585
|
+
validationFailures: [],
|
|
586
|
+
diagnostics,
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
const snapshot = extractor.getNocturnalSessionSnapshot(selectedSessionId);
|
|
590
|
+
if (!snapshot) {
|
|
591
|
+
return {
|
|
592
|
+
success: false,
|
|
593
|
+
noTargetSelected: true,
|
|
594
|
+
skipReason: 'insufficient_snapshot_data',
|
|
595
|
+
validationFailed: false,
|
|
596
|
+
validationFailures: [],
|
|
597
|
+
diagnostics,
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
diagnostics.idle = { isIdle: true, mostRecentActivityAt: 0, idleForMs: 0, activeSessionCount: 0, abandonedSessionIds: [], trajectoryGuardrailConfirmsIdle: true, reason: 'preflight passed' };
|
|
601
|
+
// Step 3: Record run start
|
|
602
|
+
void recordRunStart(stateDir, selectedPrincipleId).catch((err) => {
|
|
603
|
+
console.warn(`[nocturnal-service] Failed to record run start: ${String(err)}`);
|
|
604
|
+
});
|
|
605
|
+
// Step 4: Trinity execution via adapter (async)
|
|
606
|
+
let trinityArtifact = null;
|
|
607
|
+
let trinityResult = null;
|
|
608
|
+
let rawJson;
|
|
609
|
+
let chainModeUsed = 'single-reflector';
|
|
610
|
+
if (options.skipReflector) {
|
|
611
|
+
if (!options.reflectorOutputOverride) {
|
|
612
|
+
return {
|
|
613
|
+
success: false,
|
|
614
|
+
noTargetSelected: false,
|
|
615
|
+
validationFailed: true,
|
|
616
|
+
validationFailures: ['skipReflector is true but no reflectorOutputOverride provided'],
|
|
617
|
+
diagnostics,
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
rawJson = options.reflectorOutputOverride;
|
|
621
|
+
}
|
|
622
|
+
else if (options.trinityResultOverride) {
|
|
623
|
+
trinityResult = options.trinityResultOverride;
|
|
624
|
+
diagnostics.trinityAttempted = true;
|
|
625
|
+
diagnostics.trinityResult = trinityResult;
|
|
626
|
+
diagnostics.chainModeUsed = trinityResult.success ? 'trinity' : 'single-reflector';
|
|
627
|
+
chainModeUsed = trinityResult.success ? 'trinity' : 'single-reflector';
|
|
628
|
+
if (!trinityResult.success) {
|
|
629
|
+
const failures = trinityResult.failures.map((f) => `${f.stage}: ${f.reason}`);
|
|
630
|
+
void recordRunEnd(stateDir, 'failed', { reason: `Trinity override failed: ${failures.join('; ')}` }).catch((err) => {
|
|
631
|
+
console.warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
632
|
+
});
|
|
633
|
+
adjustThresholdsFromSignals(stateDir, { malformedRate: 1.0, arbiterRejectRate: 0.0, executabilityRejectRate: 0.0, qualityDelta: 0.0 });
|
|
634
|
+
return { success: false, noTargetSelected: false, validationFailed: true, validationFailures: [`Trinity override failed: ${failures.join('; ')}`], snapshot, diagnostics };
|
|
635
|
+
}
|
|
636
|
+
trinityArtifact = trinityResult.artifact;
|
|
637
|
+
const artifactData = draftToArtifact(trinityArtifact);
|
|
638
|
+
rawJson = JSON.stringify(artifactData);
|
|
639
|
+
}
|
|
640
|
+
else {
|
|
641
|
+
const trinityConfig = {
|
|
642
|
+
...DEFAULT_TRINITY_CONFIG,
|
|
643
|
+
...options.trinityConfig,
|
|
644
|
+
runtimeAdapter: options.runtimeAdapter,
|
|
645
|
+
stateDir,
|
|
646
|
+
};
|
|
647
|
+
if (trinityConfig.useTrinity) {
|
|
648
|
+
diagnostics.trinityAttempted = true;
|
|
649
|
+
trinityResult = await runTrinityAsync({ snapshot, principleId: selectedPrincipleId, config: trinityConfig });
|
|
650
|
+
diagnostics.trinityResult = trinityResult;
|
|
651
|
+
diagnostics.chainModeUsed = trinityResult.success ? 'trinity' : 'single-reflector';
|
|
652
|
+
chainModeUsed = trinityResult.success ? 'trinity' : 'single-reflector';
|
|
653
|
+
if (trinityResult.success) {
|
|
654
|
+
const draftValidation = validateTrinityDraft(trinityResult.artifact);
|
|
655
|
+
if (!draftValidation.valid) {
|
|
656
|
+
const failures = draftValidation.failures;
|
|
657
|
+
void recordRunEnd(stateDir, 'failed', { reason: `Trinity draft invalid: ${failures.join('; ')}` }).catch((err) => {
|
|
658
|
+
console.warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
659
|
+
});
|
|
660
|
+
adjustThresholdsFromSignals(stateDir, { malformedRate: 1.0, arbiterRejectRate: 0.0, executabilityRejectRate: 0.0, qualityDelta: 0.0 });
|
|
661
|
+
return { success: false, noTargetSelected: false, validationFailed: true, validationFailures: failures, snapshot, diagnostics };
|
|
662
|
+
}
|
|
663
|
+
trinityArtifact = trinityResult.artifact;
|
|
664
|
+
const artifactData = draftToArtifact(trinityArtifact);
|
|
665
|
+
rawJson = JSON.stringify(artifactData);
|
|
666
|
+
}
|
|
667
|
+
else {
|
|
668
|
+
const failures = trinityResult.failures.map((f) => `${f.stage}: ${f.reason}`);
|
|
669
|
+
void recordRunEnd(stateDir, 'failed', { reason: `Trinity chain failed: ${failures.join('; ')}` }).catch((err) => {
|
|
670
|
+
console.warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
671
|
+
});
|
|
672
|
+
adjustThresholdsFromSignals(stateDir, { malformedRate: 1.0, arbiterRejectRate: 0.0, executabilityRejectRate: 0.0, qualityDelta: 0.0 });
|
|
673
|
+
return { success: false, noTargetSelected: false, validationFailed: true, validationFailures: [`Trinity chain failed: ${failures.join('; ')}`], snapshot, diagnostics };
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
else {
|
|
677
|
+
rawJson = invokeStubReflector(snapshot, selectedPrincipleId);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
// Step 5: Arbiter validation
|
|
681
|
+
const arbiterResult = parseAndValidateArtifact(rawJson, {
|
|
682
|
+
expectedPrincipleId: selectedPrincipleId,
|
|
683
|
+
expectedSessionId: selectedSessionId,
|
|
684
|
+
qualityThresholds: {
|
|
685
|
+
thinkingModelDeltaMin: 0.01,
|
|
686
|
+
planningRatioGainMin: -0.5,
|
|
687
|
+
},
|
|
688
|
+
});
|
|
689
|
+
diagnostics.arbiterResult = arbiterResult;
|
|
690
|
+
if (!arbiterResult.passed || !arbiterResult.artifact) {
|
|
691
|
+
const failures = arbiterResult.failures.map((f) => f.reason);
|
|
692
|
+
void recordRunEnd(stateDir, 'failed', { reason: failures.join('; ') }).catch((err) => {
|
|
693
|
+
console.warn(`[nocturnal-service] Failed to record run end (arbiter failed): ${String(err)}`);
|
|
694
|
+
});
|
|
695
|
+
adjustThresholdsFromSignals(stateDir, { malformedRate: 0.0, arbiterRejectRate: 1.0, executabilityRejectRate: 0.0, qualityDelta: 0.0 });
|
|
696
|
+
return { success: false, noTargetSelected: false, validationFailed: true, validationFailures: failures, diagnostics };
|
|
697
|
+
}
|
|
698
|
+
// Step 6: Executability check
|
|
699
|
+
const execResult = validateExecutability(arbiterResult.artifact);
|
|
700
|
+
if (!execResult.executable) {
|
|
701
|
+
const failures = execResult.failures.map((f) => f.reason);
|
|
702
|
+
void recordRunEnd(stateDir, 'failed', { reason: failures.join('; ') }).catch((err) => {
|
|
703
|
+
console.warn(`[nocturnal-service] Failed to record run end (executability failed): ${String(err)}`);
|
|
704
|
+
});
|
|
705
|
+
adjustThresholdsFromSignals(stateDir, { malformedRate: 0.0, arbiterRejectRate: 0.0, executabilityRejectRate: 1.0, qualityDelta: 0.0 });
|
|
706
|
+
return { success: false, noTargetSelected: false, validationFailed: true, validationFailures: failures, diagnostics };
|
|
707
|
+
}
|
|
708
|
+
diagnostics.executabilityResult = { executable: true, failures: [] };
|
|
709
|
+
// Step 7: Persist artifact
|
|
710
|
+
const artifactWithBoundedAction = { ...arbiterResult.artifact, boundedAction: execResult.boundedAction };
|
|
711
|
+
let persistedPath;
|
|
712
|
+
try {
|
|
713
|
+
persistedPath = persistArtifact(workspaceDir, artifactWithBoundedAction);
|
|
714
|
+
diagnostics.persisted = true;
|
|
715
|
+
diagnostics.persistedPath = persistedPath;
|
|
716
|
+
}
|
|
717
|
+
catch (err) {
|
|
718
|
+
void recordRunEnd(stateDir, 'failed', { reason: `persistence error: ${String(err)}` }).catch((e) => {
|
|
719
|
+
console.warn(`[nocturnal-service] Failed to record run end (persistence failed): ${String(e)}`);
|
|
720
|
+
});
|
|
721
|
+
return { success: false, noTargetSelected: false, validationFailed: true, validationFailures: [`Failed to persist artifact: ${String(err)}`], snapshot, diagnostics };
|
|
722
|
+
}
|
|
723
|
+
// Step 8: Register in dataset lineage
|
|
724
|
+
try {
|
|
725
|
+
registerSample(workspaceDir, arbiterResult.artifact, persistedPath, null);
|
|
726
|
+
}
|
|
727
|
+
catch (err) {
|
|
728
|
+
console.warn(`[nocturnal-service] Failed to register sample in dataset registry: ${String(err)}`);
|
|
729
|
+
}
|
|
730
|
+
// Step 9: Record run success
|
|
731
|
+
void recordRunEnd(stateDir, 'success', { sampleCount: 1 }).catch((err) => {
|
|
732
|
+
console.warn(`[nocturnal-service] Failed to record run end (success): ${String(err)}`);
|
|
733
|
+
});
|
|
734
|
+
// Step 10: Adaptive threshold adjustment
|
|
735
|
+
const malformedRate = trinityResult && !trinityResult.success ? 1.0 : 0.0;
|
|
736
|
+
const arbiterRejectRate = !arbiterResult.passed ? 1.0 : 0.0;
|
|
737
|
+
const executabilityRejectRate = !execResult.executable ? 1.0 : 0.0;
|
|
738
|
+
const qualityDelta = 0.0;
|
|
739
|
+
adjustThresholdsFromSignals(stateDir, { malformedRate, arbiterRejectRate, executabilityRejectRate, qualityDelta });
|
|
740
|
+
return {
|
|
741
|
+
success: true,
|
|
742
|
+
artifact: artifactWithBoundedAction,
|
|
743
|
+
noTargetSelected: false,
|
|
744
|
+
validationFailed: false,
|
|
745
|
+
validationFailures: [],
|
|
746
|
+
snapshot,
|
|
747
|
+
diagnostics,
|
|
748
|
+
trinityTelemetry: trinityResult?.telemetry,
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
// ---------------------------------------------------------------------------
|
|
752
|
+
// Query: List approved artifacts
|
|
753
|
+
// ---------------------------------------------------------------------------
|
|
754
|
+
/**
|
|
755
|
+
* List all approved nocturnal artifacts for a workspace.
|
|
756
|
+
* Returns artifacts sorted by createdAt (newest first).
|
|
757
|
+
*/
|
|
758
|
+
export function listApprovedNocturnalArtifacts(workspaceDir) {
|
|
759
|
+
const samplePaths = NocturnalPathResolver.listApprovedSamples(workspaceDir);
|
|
760
|
+
const artifacts = [];
|
|
761
|
+
for (const samplePath of samplePaths) {
|
|
762
|
+
try {
|
|
763
|
+
const content = fs.readFileSync(samplePath, 'utf-8');
|
|
764
|
+
const sample = JSON.parse(content);
|
|
765
|
+
if (sample.status === 'approved' && sample.artifactId) {
|
|
766
|
+
artifacts.push({
|
|
767
|
+
artifactId: sample.artifactId,
|
|
768
|
+
sessionId: sample.sessionId,
|
|
769
|
+
principleId: sample.principleId,
|
|
770
|
+
sourceSnapshotRef: sample.sourceSnapshotRef || '',
|
|
771
|
+
badDecision: sample.badDecision,
|
|
772
|
+
betterDecision: sample.betterDecision,
|
|
773
|
+
rationale: sample.rationale,
|
|
774
|
+
createdAt: sample.createdAt,
|
|
775
|
+
persistedAt: sample.persistedAt || new Date().toISOString(),
|
|
776
|
+
boundedAction: sample.boundedAction,
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
catch {
|
|
781
|
+
// Skip malformed files
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
// Sort by createdAt descending
|
|
785
|
+
artifacts.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
|
|
786
|
+
return artifacts;
|
|
787
|
+
}
|