principles-disciple 1.72.0 → 1.73.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/openclaw.plugin.json +10 -5
- package/package.json +17 -19
- package/scripts/acceptance-test.mjs +16 -73
- package/scripts/sync-plugin.mjs +382 -77
- package/src/commands/archive-impl.ts +2 -1
- package/src/commands/capabilities.ts +2 -2
- package/src/commands/context.ts +2 -2
- package/src/commands/disable-impl.ts +2 -1
- package/src/commands/evolution-status.ts +16 -16
- package/src/commands/export.ts +12 -67
- package/src/commands/pain.ts +91 -1
- package/src/commands/principle-rollback.ts +2 -1
- package/src/commands/promote-impl.ts +7 -43
- package/src/commands/rollback-impl.ts +2 -1
- package/src/commands/rollback.ts +2 -1
- package/src/commands/samples.ts +2 -1
- package/src/commands/thinking-os.ts +2 -1
- package/src/config/errors.ts +18 -2
- package/src/constants/diagnostician.ts +2 -2
- package/src/constants/tools.ts +2 -1
- package/src/core/__tests__/focus-history.test.ts +210 -0
- package/src/core/config.ts +1 -1
- package/src/core/confirm-first-gate.ts +255 -0
- package/src/core/correction-cue-learner.ts +2 -136
- package/src/core/correction-types.ts +16 -88
- package/src/core/dictionary.ts +19 -20
- package/src/core/empathy-keyword-matcher.ts +17 -289
- package/src/core/empathy-types.ts +18 -229
- package/src/core/event-log.ts +38 -132
- package/src/core/evolution-reducer.ts +21 -2
- package/src/core/evolution-types.ts +76 -464
- package/src/core/file-store.ts +80 -0
- package/src/core/focus-history.ts +228 -955
- package/src/core/local-worker-routing.ts +34 -314
- package/src/core/merge-gate-audit.ts +0 -195
- package/src/core/pain-diagnostic-gate.ts +154 -0
- package/src/core/pain-signal.ts +21 -138
- package/src/core/pain.ts +15 -88
- package/src/core/pd-task-reconciler.ts +26 -115
- package/src/core/pd-task-service.ts +9 -9
- package/src/core/pd-task-types.ts +23 -127
- package/src/core/principle-compiler/__tests__/compiler-replay-gate.test.ts +174 -0
- package/src/core/principle-compiler/code-validator.ts +15 -42
- package/src/core/principle-compiler/compiler.ts +100 -15
- package/src/core/principle-compiler/index.ts +5 -2
- package/src/core/principle-compiler/template-generator.ts +4 -104
- package/src/core/principle-injection.ts +10 -202
- package/src/core/principle-internalization/filesystem-lifecycle-datasource.ts +42 -0
- package/src/core/principle-internalization/lifecycle-read-model.ts +39 -242
- package/src/core/principle-internalization/principle-lifecycle-service.ts +12 -10
- package/src/core/principle-tree-ledger-adapter.ts +145 -0
- package/src/core/principle-tree-ledger.ts +8 -6
- package/src/core/reflection/reflection-context.ts +14 -109
- package/src/core/replay-engine.ts +8 -500
- package/src/core/rule-host-helpers.ts +5 -35
- package/src/core/rule-host-types.ts +10 -82
- package/src/core/rule-host.ts +6 -63
- package/src/core/runtime-v2-prompt-activation-reader.ts +231 -0
- package/src/core/session-tracker.ts +87 -101
- package/src/core/shadow-observation-registry.ts +19 -48
- package/src/core/trajectory.ts +3 -1
- package/src/core/workflow-funnel-loader.ts +62 -68
- package/src/core/workspace-context.ts +46 -0
- package/src/core/workspace-dir-service.ts +1 -1
- package/src/core/workspace-dir-validation.ts +18 -9
- package/src/hooks/AGENTS.md +1 -1
- package/src/hooks/gate-block-helper.ts +46 -44
- package/src/hooks/gate.ts +207 -7
- package/src/hooks/lifecycle.ts +30 -32
- package/src/hooks/llm.ts +60 -32
- package/src/hooks/pain.ts +297 -103
- package/src/hooks/prompt.ts +459 -439
- package/src/hooks/subagent.ts +2 -29
- package/src/i18n/commands.ts +2 -10
- package/src/index.ts +95 -85
- package/src/openclaw-sdk.ts +311 -0
- package/src/service/central-database.ts +8 -4
- package/src/service/evolution-queue-migration.ts +2 -1
- package/src/service/evolution-worker.ts +163 -1786
- package/src/service/internalization-trigger-adapter.ts +302 -0
- package/src/service/keyword-optimization-service.ts +4 -4
- package/src/service/monitoring-query-service.ts +1 -215
- package/src/service/queue-io.ts +60 -331
- package/src/service/runtime-summary-service.ts +59 -16
- package/src/service/subagent-workflow/index.ts +0 -41
- package/src/service/subagent-workflow/types.ts +9 -120
- package/src/service/subagent-workflow/workflow-store.ts +2 -119
- package/src/service/workflow-watchdog.ts +0 -43
- package/src/types/event-payload.ts +16 -74
- package/src/types/event-types.ts +39 -547
- package/src/types/hygiene-types.ts +7 -30
- package/src/types/principle-tree-schema.ts +20 -222
- package/src/types/queue.ts +15 -70
- package/src/types/runtime-summary.ts +5 -49
- package/src/utils/io.ts +10 -0
- package/src/utils/retry.ts +1 -1
- package/src/utils/shadow-fingerprint.ts +2 -2
- package/src/utils/workspace-resolver.ts +50 -0
- package/templates/langs/en/core/AGENTS.md +2 -2
- package/templates/langs/en/core/BOOT.md +1 -1
- package/templates/langs/en/core/HEARTBEAT.md +2 -2
- package/templates/langs/en/skills/ai-sprint-orchestration/references/agent-registry.json +1 -72
- package/templates/langs/en/skills/ai-sprint-orchestration/references/specs/bugfix-complex-template.json +6 -6
- package/templates/langs/en/skills/ai-sprint-orchestration/references/specs/feature-complex-template.json +6 -6
- package/templates/langs/en/skills/ai-sprint-orchestration/references/specs/workflow-validation-minimal-verify.json +2 -12
- package/templates/langs/en/skills/ai-sprint-orchestration/references/specs/workflow-validation-minimal.json +2 -12
- package/templates/langs/en/skills/ai-sprint-orchestration/runtime/.gitignore +2 -2
- package/templates/langs/en/skills/ai-sprint-orchestration/scripts/run.mjs +51 -15
- package/templates/langs/en/skills/evolve-task/SKILL.md +1 -1
- package/templates/langs/en/skills/pd-cli-operator/SKILL.md +67 -0
- package/templates/langs/en/skills/pd-diagnostician/SKILL.md +1 -1
- package/templates/langs/en/skills/pd-mentor/SKILL.md +1 -1
- package/templates/langs/en/skills/pd-pain-signal/SKILL.md +17 -39
- package/templates/langs/en/skills/pd-runtime-v2/SKILL.md +61 -0
- package/templates/langs/zh/core/AGENTS.md +2 -2
- package/templates/langs/zh/core/BOOT.md +1 -1
- package/templates/langs/zh/core/HEARTBEAT.md +2 -2
- package/templates/langs/zh/skills/ai-sprint-orchestration/references/agent-registry.json +1 -72
- package/templates/langs/zh/skills/ai-sprint-orchestration/references/specs/bugfix-complex-template.json +6 -6
- package/templates/langs/zh/skills/ai-sprint-orchestration/references/specs/feature-complex-template.json +6 -6
- package/templates/langs/zh/skills/ai-sprint-orchestration/references/specs/nocturnal-trinity-quality-enhancement.json +8 -8
- package/templates/langs/zh/skills/ai-sprint-orchestration/references/specs/workflow-validation-minimal-verify.json +2 -12
- package/templates/langs/zh/skills/ai-sprint-orchestration/references/specs/workflow-validation-minimal.json +2 -12
- package/templates/langs/zh/skills/ai-sprint-orchestration/runtime/.gitignore +2 -2
- package/templates/langs/zh/skills/ai-sprint-orchestration/scripts/run.mjs +51 -15
- package/templates/langs/zh/skills/ai-sprint-orchestration/test/run.test.mjs +21 -5
- package/templates/langs/zh/skills/evolve-task/SKILL.md +2 -2
- package/templates/langs/zh/skills/pd-cli-operator/SKILL.md +67 -0
- package/templates/langs/zh/skills/pd-diagnostician/SKILL.md +1 -1
- package/templates/langs/zh/skills/pd-mentor/SKILL.md +1 -1
- package/templates/langs/zh/skills/pd-pain-signal/SKILL.md +17 -38
- package/templates/langs/zh/skills/pd-runtime-v2/SKILL.md +61 -0
- package/tests/build-artifacts.test.ts +1 -3
- package/tests/commands/evolution-status.test.ts +0 -118
- package/tests/core/bootstrap-rules.test.ts +1 -1
- package/tests/core/config.test.ts +1 -1
- package/tests/core/event-log.test.ts +35 -0
- package/tests/core/evolution-engine.test.ts +610 -0
- package/tests/core/file-store.test.ts +102 -0
- package/tests/core/focus-history.test.ts +203 -11
- package/tests/core/merge-gate-audit.test.ts +2 -169
- package/tests/core/model-deployment-registry.test.ts +7 -1
- package/tests/core/model-training-registry.test.ts +19 -0
- package/tests/core/observability.test.ts +0 -1
- package/tests/core/pain-diagnostic-gate.test.ts +498 -0
- package/tests/core/pain.test.ts +0 -1
- package/tests/core/principle-internalization/deprecated-readiness.test.ts +2 -2
- package/tests/core/principle-internalization/lifecycle-metrics.test.ts +2 -2
- package/tests/core/principle-internalization/{internalization-routing-policy.test.ts → lifecycle-routing-policy.test.ts} +6 -6
- package/tests/core/principle-internalization/lineage-source-retired.test.ts +56 -0
- package/tests/core/principle-internalization/principle-lifecycle-service.test.ts +1 -23
- package/tests/core/principle-tree-ledger-adapter.test.ts +253 -0
- package/tests/core/reflection-context.test.ts +0 -14
- package/tests/core/replay-engine.test.ts +127 -215
- package/tests/core/rule-host-helpers.test.ts +2 -2
- package/tests/core/rule-implementation-runtime.test.ts +0 -27
- package/tests/core/workflow-funnel-loader.test.ts +162 -0
- package/tests/core/workspace-dir-validation.test.ts +8 -1
- package/tests/core-anti-growth.test.ts +192 -0
- package/tests/hook-workspace-nextaction-contract.test.ts +42 -0
- package/tests/hooks/confirm-first-gate.test.ts +333 -0
- package/tests/hooks/gate-auto-correct-shadow.test.ts +310 -0
- package/tests/hooks/gate-auto-correct.test.ts +665 -0
- package/tests/hooks/gate-rule-host-pipeline.test.ts +2 -1
- package/tests/hooks/pain.test.ts +269 -12
- package/tests/hooks/prompt-characterization.test.ts +500 -0
- package/tests/hooks/prompt-size-guard.test.ts +32 -17
- package/tests/hooks/runtime-v2-prompt-activation.test.ts +869 -0
- package/tests/index.test.ts +94 -1
- package/tests/integration/auto-entry-gate.test.ts +248 -0
- package/tests/integration/internalization-trigger-guard.test.ts +69 -0
- package/tests/integration/m8-legacy-paths.test.ts +63 -0
- package/tests/integration/runtime-v2-pain-guard.test.ts +125 -0
- package/tests/plugin-config-resolution-cutover.test.ts +359 -0
- package/tests/runtime-v2-discovery-guard.test.ts +154 -0
- package/tests/service/central-database.test.ts +457 -0
- package/tests/service/evolution-worker.correction-observer.test.ts +173 -0
- package/tests/service/evolution-worker.timeout.test.ts +11 -129
- package/tests/service/internalization-trigger-adapter.test.ts +251 -0
- package/tests/service/monitoring-query-service.test.ts +1 -47
- package/tests/service/queue-io.test.ts +1 -62
- package/tests/service/runtime-summary-service.test.ts +3 -1
- package/tests/service/workflow-watchdog.test.ts +0 -91
- package/tests/utils/file-lock.test.ts +5 -3
- package/tests/utils/session-key.test.ts +52 -0
- package/tests/utils/subagent-probe.test.ts +48 -1
- package/vitest.config.ts +4 -11
- package/.planning/codebase/ARCHITECTURE.md +0 -157
- package/.planning/codebase/CONCERNS.md +0 -145
- package/.planning/codebase/CONVENTIONS.md +0 -148
- package/.planning/codebase/INTEGRATIONS.md +0 -81
- package/.planning/codebase/STACK.md +0 -87
- package/.planning/codebase/STRUCTURE.md +0 -193
- package/.planning/codebase/TESTING.md +0 -243
- package/.planning/phases/01-basic-visualization/01-GAP-CLOSURE-VERIFICATION.md +0 -113
- package/docs/COMMAND_REFERENCE.md +0 -76
- package/docs/COMMAND_REFERENCE_EN.md +0 -79
- package/scripts/build-web.mjs +0 -46
- package/scripts/diagnose-nocturnal.mjs +0 -537
- package/scripts/seed-nocturnal-scenarios.mjs +0 -384
- package/src/commands/nocturnal-review.ts +0 -322
- package/src/commands/nocturnal-rollout.ts +0 -790
- package/src/commands/nocturnal-train.ts +0 -986
- package/src/commands/pd-reflect.ts +0 -88
- package/src/core/adaptive-thresholds.ts +0 -478
- package/src/core/diagnostician-task-store.ts +0 -192
- package/src/core/nocturnal-arbiter.ts +0 -715
- package/src/core/nocturnal-artifact-lineage.ts +0 -116
- package/src/core/nocturnal-artificer.ts +0 -257
- package/src/core/nocturnal-candidate-scoring.ts +0 -530
- package/src/core/nocturnal-compliance.ts +0 -1146
- package/src/core/nocturnal-dataset.ts +0 -763
- package/src/core/nocturnal-executability.ts +0 -428
- package/src/core/nocturnal-export.ts +0 -499
- package/src/core/nocturnal-paths.ts +0 -240
- package/src/core/nocturnal-reasoning-deriver.ts +0 -343
- package/src/core/nocturnal-rule-implementation-validator.ts +0 -246
- package/src/core/nocturnal-snapshot-contract.ts +0 -99
- package/src/core/nocturnal-trajectory-extractor.ts +0 -512
- package/src/core/nocturnal-trinity-types.ts +0 -218
- package/src/core/nocturnal-trinity.ts +0 -2680
- package/src/core/principle-internalization/deprecated-readiness.ts +0 -93
- package/src/core/principle-internalization/internalization-routing-policy.ts +0 -208
- package/src/core/principle-internalization/lifecycle-metrics.ts +0 -152
- package/src/http/principles-console-route.ts +0 -709
- package/src/service/central-health-service.ts +0 -49
- package/src/service/central-overview-service.ts +0 -138
- package/src/service/control-ui-query-service.ts +0 -900
- package/src/service/cooldown-strategy.ts +0 -97
- package/src/service/evolution-pain-context.ts +0 -79
- package/src/service/evolution-query-service.ts +0 -407
- package/src/service/health-query-service.ts +0 -1038
- package/src/service/nocturnal-config.ts +0 -214
- package/src/service/nocturnal-runtime.ts +0 -734
- package/src/service/nocturnal-service.ts +0 -1605
- package/src/service/nocturnal-target-selector.ts +0 -545
- package/src/service/sleep-cycle.ts +0 -157
- package/src/service/startup-reconciler.ts +0 -112
- package/src/service/subagent-workflow/correction-observer-types.ts +0 -82
- package/src/service/subagent-workflow/correction-observer-workflow-manager.ts +0 -250
- package/src/service/subagent-workflow/deep-reflect-workflow-manager.ts +0 -1
- package/src/service/subagent-workflow/dynamic-timeout.ts +0 -30
- package/src/service/subagent-workflow/empathy-observer-workflow-manager.ts +0 -268
- package/src/service/subagent-workflow/nocturnal-workflow-manager.ts +0 -795
- package/src/service/subagent-workflow/runtime-direct-driver.ts +0 -268
- package/src/service/subagent-workflow/workflow-manager-base.ts +0 -580
- package/src/tools/write-pain-flag.ts +0 -215
- package/tests/commands/nocturnal-review.test.ts +0 -448
- package/tests/commands/nocturnal-train.test.ts +0 -97
- package/tests/commands/pd-reflect.test.ts +0 -49
- package/tests/core/adaptive-thresholds.test.ts +0 -261
- package/tests/core/nocturnal-arbiter.test.ts +0 -559
- package/tests/core/nocturnal-artifact-lineage.test.ts +0 -53
- package/tests/core/nocturnal-artificer.test.ts +0 -241
- package/tests/core/nocturnal-candidate-scoring.test.ts +0 -532
- package/tests/core/nocturnal-compliance-p-principles.test.ts +0 -133
- package/tests/core/nocturnal-compliance.test.ts +0 -646
- package/tests/core/nocturnal-dataset.test.ts +0 -892
- package/tests/core/nocturnal-e2e.test.ts +0 -234
- package/tests/core/nocturnal-executability.test.ts +0 -357
- package/tests/core/nocturnal-export.test.ts +0 -517
- package/tests/core/nocturnal-reasoning-deriver.test.ts +0 -372
- package/tests/core/nocturnal-reviewed-subset-comparison.test.ts +0 -428
- package/tests/core/nocturnal-rule-implementation-validator.test.ts +0 -127
- package/tests/core/nocturnal-snapshot-contract.test.ts +0 -121
- package/tests/core/nocturnal-trajectory-extractor.test.ts +0 -634
- package/tests/core/nocturnal-trinity.test.ts +0 -2053
- package/tests/core/pain-auto-repair.test.ts +0 -96
- package/tests/core/pain-integration.test.ts +0 -510
- package/tests/fixtures/nocturnal-reviewed-subset.json +0 -183
- package/tests/http/principles-console-route.test.ts +0 -162
- package/tests/integration/chaos-resilience.test.ts +0 -348
- package/tests/integration/empathy-workflow-integration.test.ts +0 -626
- package/tests/integration/pain-diagnostician-loop.e2e.test.ts +0 -380
- package/tests/service/control-ui-query-service.test.ts +0 -121
- package/tests/service/cooldown-strategy.test.ts +0 -164
- package/tests/service/data-endpoints-regression.test.ts +0 -834
- package/tests/service/empathy-observer-workflow-manager.test.ts +0 -175
- package/tests/service/evolution-worker.nocturnal.test.ts +0 -601
- package/tests/service/nocturnal-runtime-hardening.test.ts +0 -118
- package/tests/service/nocturnal-runtime.test.ts +0 -473
- package/tests/service/nocturnal-service-code-candidate.test.ts +0 -330
- package/tests/service/nocturnal-target-selector.test.ts +0 -615
- package/tests/service/startup-reconciler.test.ts +0 -148
- package/tests/tools/write-pain-flag.test.ts +0 -358
- package/ui/src/App.tsx +0 -45
- package/ui/src/api.ts +0 -220
- package/ui/src/charts.tsx +0 -955
- package/ui/src/components/ErrorState.tsx +0 -6
- package/ui/src/components/Loading.tsx +0 -13
- package/ui/src/components/ProtectedRoute.tsx +0 -12
- package/ui/src/components/Shell.tsx +0 -91
- package/ui/src/components/WorkspaceConfig.tsx +0 -178
- package/ui/src/components/index.ts +0 -5
- package/ui/src/context/auth.tsx +0 -80
- package/ui/src/context/theme.tsx +0 -66
- package/ui/src/hooks/useAutoRefresh.ts +0 -39
- package/ui/src/i18n/ui.ts +0 -473
- package/ui/src/main.tsx +0 -16
- package/ui/src/pages/EvolutionPage.tsx +0 -333
- package/ui/src/pages/FeedbackPage.tsx +0 -138
- package/ui/src/pages/GateMonitorPage.tsx +0 -136
- package/ui/src/pages/LoginPage.tsx +0 -89
- package/ui/src/pages/OverviewPage.tsx +0 -599
- package/ui/src/pages/SamplesPage.tsx +0 -174
- package/ui/src/pages/ThinkingModelsPage.tsx +0 -702
- package/ui/src/styles.css +0 -2020
- package/ui/src/types.ts +0 -384
- package/ui/src/utils/format.ts +0 -15
|
@@ -1,1605 +0,0 @@
|
|
|
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
|
-
|
|
31
|
-
import * as fs from 'fs';
|
|
32
|
-
import * as path from 'path';
|
|
33
|
-
import { randomUUID } from 'crypto';
|
|
34
|
-
import type { RecentPainContext } from './subagent-workflow/types.js';
|
|
35
|
-
import type { PluginLogger } from '../openclaw-sdk.js';
|
|
36
|
-
import {
|
|
37
|
-
createNocturnalTrajectoryExtractor,
|
|
38
|
-
computeThinkingModelDelta,
|
|
39
|
-
type NocturnalSessionSnapshot,
|
|
40
|
-
} from '../core/nocturnal-trajectory-extractor.js';
|
|
41
|
-
import {
|
|
42
|
-
NocturnalTargetSelector,
|
|
43
|
-
type NocturnalSelectionResult,
|
|
44
|
-
type SkipReason,
|
|
45
|
-
} from './nocturnal-target-selector.js';
|
|
46
|
-
import {
|
|
47
|
-
parseAndValidateArtifact,
|
|
48
|
-
validateTrinityDraft,
|
|
49
|
-
type NocturnalArtifact,
|
|
50
|
-
type ArbiterResult,
|
|
51
|
-
} from '../core/nocturnal-arbiter.js';
|
|
52
|
-
import {
|
|
53
|
-
draftToArtifact,
|
|
54
|
-
runTrinity,
|
|
55
|
-
runTrinityAsync,
|
|
56
|
-
DEFAULT_TRINITY_CONFIG,
|
|
57
|
-
type TrinityConfig,
|
|
58
|
-
type TrinityResult,
|
|
59
|
-
type TrinityDraftArtifact,
|
|
60
|
-
type TrinityRuntimeAdapter,
|
|
61
|
-
} from '../core/nocturnal-trinity.js';
|
|
62
|
-
import {
|
|
63
|
-
validateExecutability,
|
|
64
|
-
type BoundedAction,
|
|
65
|
-
} from '../core/nocturnal-executability.js';
|
|
66
|
-
import {
|
|
67
|
-
adjustThresholdsFromSignals,
|
|
68
|
-
type ThresholdSignals,
|
|
69
|
-
} from '../core/adaptive-thresholds.js';
|
|
70
|
-
import {
|
|
71
|
-
parseArtificerOutput,
|
|
72
|
-
resolveArtificerTargetRule,
|
|
73
|
-
shouldRunArtificer,
|
|
74
|
-
type ArtificerOutput,
|
|
75
|
-
type ArtificerTargetRuleResolution,
|
|
76
|
-
} from '../core/nocturnal-artificer.js';
|
|
77
|
-
import { validateRuleImplementationCandidate } from '../core/nocturnal-rule-implementation-validator.js';
|
|
78
|
-
import { refreshPrincipleLifecycle } from '../core/principle-internalization/lifecycle-refresh.js';
|
|
79
|
-
import {
|
|
80
|
-
createImplementationAssetDir,
|
|
81
|
-
deleteImplementationAssetDir,
|
|
82
|
-
getImplementationAssetRoot,
|
|
83
|
-
type CodeImplementationLineageMetadata,
|
|
84
|
-
} from '../core/code-implementation-storage.js';
|
|
85
|
-
import {
|
|
86
|
-
appendCandidateArtifactLineageRecord,
|
|
87
|
-
appendArtifactLineageRecord,
|
|
88
|
-
} from '../core/nocturnal-artifact-lineage.js';
|
|
89
|
-
import {
|
|
90
|
-
createImplementation,
|
|
91
|
-
deleteImplementation,
|
|
92
|
-
} from '../core/principle-tree-ledger.js';
|
|
93
|
-
import {
|
|
94
|
-
checkPreflight,
|
|
95
|
-
recordRunStart,
|
|
96
|
-
recordRunEnd,
|
|
97
|
-
type IdleCheckResult,
|
|
98
|
-
type PreflightCheckResult,
|
|
99
|
-
} from './nocturnal-runtime.js';
|
|
100
|
-
import { loadNocturnalConfig } from './nocturnal-config.js';
|
|
101
|
-
import { atomicWriteFileSync } from '../utils/io.js';
|
|
102
|
-
import { NocturnalPathResolver } from '../core/nocturnal-paths.js';
|
|
103
|
-
import { registerSample } from '../core/nocturnal-dataset.js';
|
|
104
|
-
import { getPrincipleState, setPrincipleState } from '../core/principle-training-state.js';
|
|
105
|
-
import type { Implementation } from '../types/principle-tree-schema.js';
|
|
106
|
-
import { validateNocturnalSnapshotIngress } from '../core/nocturnal-snapshot-contract.js';
|
|
107
|
-
import { EventLogService } from '../core/event-log.js';
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
// ---------------------------------------------------------------------------
|
|
111
|
-
// #251: Sync trainingStore sample counts after registration
|
|
112
|
-
// ---------------------------------------------------------------------------
|
|
113
|
-
|
|
114
|
-
function incrementGeneratedSampleCount(stateDir: string, principleId: string): void {
|
|
115
|
-
try {
|
|
116
|
-
const state = getPrincipleState(stateDir, principleId);
|
|
117
|
-
state.generatedSampleCount += 1;
|
|
118
|
-
setPrincipleState(stateDir, state);
|
|
119
|
-
} catch (err) {
|
|
120
|
-
|
|
121
|
-
console.warn(`[nocturnal-service] Failed to sync generatedSampleCount for ${principleId}:`, err instanceof Error ? err.stack : err);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// ---------------------------------------------------------------------------
|
|
126
|
-
// Types
|
|
127
|
-
// ---------------------------------------------------------------------------
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Result of a complete nocturnal reflection run.
|
|
131
|
-
*/
|
|
132
|
-
export interface NocturnalRunResult {
|
|
133
|
-
/** Whether the run produced an approved artifact */
|
|
134
|
-
success: boolean;
|
|
135
|
-
/** The approved artifact (if success === true) */
|
|
136
|
-
artifact?: NocturnalArtifact & { boundedAction?: BoundedAction };
|
|
137
|
-
/** Skip reason (if success === false because nothing to do) */
|
|
138
|
-
skipReason?: SkipReason;
|
|
139
|
-
/** Whether the selector found no target */
|
|
140
|
-
noTargetSelected: boolean;
|
|
141
|
-
/** Whether the reflector rejected or artifact failed validation */
|
|
142
|
-
validationFailed: boolean;
|
|
143
|
-
/** Validation failure reasons */
|
|
144
|
-
validationFailures: string[];
|
|
145
|
-
/** Snapshot used for reflection */
|
|
146
|
-
snapshot?: NocturnalSessionSnapshot;
|
|
147
|
-
/** Diagnostics from each pipeline stage */
|
|
148
|
-
diagnostics: NocturnalRunDiagnostics;
|
|
149
|
-
/** Trinity telemetry (if Trinity was used) */
|
|
150
|
-
trinityTelemetry?: TrinityResult['telemetry'];
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Diagnostics from each pipeline stage.
|
|
155
|
-
*/
|
|
156
|
-
export interface NocturnalRunDiagnostics {
|
|
157
|
-
/** Pre-flight check result */
|
|
158
|
-
preflight: PreflightCheckResult | null;
|
|
159
|
-
/** Selection result */
|
|
160
|
-
selection: NocturnalSelectionResult | null;
|
|
161
|
-
/** Idle check result */
|
|
162
|
-
idle: IdleCheckResult | null;
|
|
163
|
-
/** Whether Trinity chain was attempted */
|
|
164
|
-
trinityAttempted: boolean;
|
|
165
|
-
/** Trinity result (if trinityAttempted === true) */
|
|
166
|
-
trinityResult: TrinityResult | null;
|
|
167
|
-
/** Which chain mode was used */
|
|
168
|
-
chainModeUsed: 'trinity' | 'single-reflector' | null;
|
|
169
|
-
/** Arbiter validation result */
|
|
170
|
-
arbiterResult: ArbiterResult | null;
|
|
171
|
-
/** Executability validation result (if arbiter passed) */
|
|
172
|
-
executabilityResult: { executable: boolean; failures: string[] } | null;
|
|
173
|
-
/** Whether artifact was persisted */
|
|
174
|
-
persisted: boolean;
|
|
175
|
-
/** Persistence path (if persisted) */
|
|
176
|
-
persistedPath?: string;
|
|
177
|
-
/** Code-candidate sidecar diagnostics */
|
|
178
|
-
artificer: NocturnalArtificerDiagnostics;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export interface NocturnalArtificerDiagnostics {
|
|
182
|
-
status: 'skipped' | 'validation_failed' | 'persisted_candidate';
|
|
183
|
-
reason?:
|
|
184
|
-
| 'behavioral_artifact_unavailable'
|
|
185
|
-
| 'no_deterministic_rule'
|
|
186
|
-
| 'insufficient_signal_density'
|
|
187
|
-
| 'missing_scribe_input'
|
|
188
|
-
| 'parse_failed'
|
|
189
|
-
| 'rule_mismatch'
|
|
190
|
-
| 'validator_rejected'
|
|
191
|
-
| 'persistence_failed';
|
|
192
|
-
ruleResolution: ArtificerTargetRuleResolution | null;
|
|
193
|
-
validationFailures: string[];
|
|
194
|
-
implementationId?: string;
|
|
195
|
-
artifactId?: string;
|
|
196
|
-
ruleId?: string;
|
|
197
|
-
persistedPath?: string;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Configuration for the nocturnal service.
|
|
202
|
-
*/
|
|
203
|
-
export interface NocturnalServiceOptions {
|
|
204
|
-
/**
|
|
205
|
-
* Whether to skip the reflector (for testing arbiter/executability in isolation).
|
|
206
|
-
* Default: false (reflector runs normally).
|
|
207
|
-
*/
|
|
208
|
-
skipReflector?: boolean;
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Override the reflector output (for testing).
|
|
212
|
-
* If provided, this JSON string is used instead of calling the stub reflector.
|
|
213
|
-
*/
|
|
214
|
-
reflectorOutputOverride?: string;
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Override idle check (for testing).
|
|
218
|
-
* If provided, this result is used instead of calling checkWorkspaceIdle.
|
|
219
|
-
*/
|
|
220
|
-
idleCheckOverride?: IdleCheckResult;
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Trinity chain configuration.
|
|
224
|
-
* Default: { useTrinity: true, maxCandidates: 3, useStubs: false }
|
|
225
|
-
*/
|
|
226
|
-
trinityConfig?: Partial<TrinityConfig>;
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Runtime adapter for real subagent execution.
|
|
230
|
-
* When provided, Trinity stages are invoked via the adapter's async methods.
|
|
231
|
-
* Ignored when trinityConfig.useStubs is true.
|
|
232
|
-
*/
|
|
233
|
-
runtimeAdapter?: TrinityRuntimeAdapter;
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Override the Trinity result (for testing).
|
|
237
|
-
* If provided, this result is used instead of running the Trinity chain.
|
|
238
|
-
*/
|
|
239
|
-
trinityResultOverride?: TrinityResult;
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Recent pain context from the evolution queue.
|
|
243
|
-
* When provided, the target selector uses it for ranking bias and diagnostics enrichment.
|
|
244
|
-
* This threads recent pain signals into sleep_reflection targeting without merging task kinds.
|
|
245
|
-
*/
|
|
246
|
-
painContext?: RecentPainContext;
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Override the principleId (skip Selector stage).
|
|
250
|
-
* When provided with snapshotOverride, the Selector stage is skipped and the provided
|
|
251
|
-
* principleId and snapshot are used directly for Trinity execution.
|
|
252
|
-
* This unifies NocturnalWorkflowManager with executeNocturnalReflectionAsync.
|
|
253
|
-
*/
|
|
254
|
-
principleIdOverride?: string;
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Override the snapshot (skip Selector stage).
|
|
258
|
-
* Must be provided together with principleIdOverride to skip Selector.
|
|
259
|
-
*/
|
|
260
|
-
snapshotOverride?: NocturnalSessionSnapshot;
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Override the Artificer JSON output (for testing).
|
|
264
|
-
* When omitted, a deterministic local candidate is synthesized.
|
|
265
|
-
*/
|
|
266
|
-
artificerOutputOverride?: string;
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Logger for diagnostic output.
|
|
270
|
-
* When provided, warnings are logged via logger.warn instead of console.warn.
|
|
271
|
-
*/
|
|
272
|
-
logger?: PluginLogger;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// ---------------------------------------------------------------------------
|
|
276
|
-
// Stub Reflector (Phase 2 — no real subagent calls)
|
|
277
|
-
// ---------------------------------------------------------------------------
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* STUB REFLECTOR — Phase 2 MVP only.
|
|
281
|
-
*
|
|
282
|
-
* This does NOT call a real subagent. Instead, it generates a plausible
|
|
283
|
-
* artifact for testing purposes. The artifact structure is correct and
|
|
284
|
-
* passes arbiter validation, but the content is synthetic.
|
|
285
|
-
*
|
|
286
|
-
* In Phase 3, this will be replaced with real subagent invocation.
|
|
287
|
-
*/
|
|
288
|
-
function invokeStubReflector(
|
|
289
|
-
snapshot: NocturnalSessionSnapshot,
|
|
290
|
-
principleId: string
|
|
291
|
-
): string {
|
|
292
|
-
const artifactId = randomUUID();
|
|
293
|
-
const now = new Date().toISOString();
|
|
294
|
-
|
|
295
|
-
// #256: Build artifact from actual event content, not just stats counts.
|
|
296
|
-
// Previously the stub only checked stats.failureCount/painEvents/gateBlocks > 0
|
|
297
|
-
// and emitted the same template artifact regardless of what actually happened.
|
|
298
|
-
// Now we examine the actual event data to generate targeted reflections.
|
|
299
|
-
|
|
300
|
-
const hasGateBlocks = (snapshot.stats.totalGateBlocks ?? 0) > 0;
|
|
301
|
-
const hasPain = snapshot.stats.totalPainEvents > 0;
|
|
302
|
-
const hasFailures = (snapshot.stats.failureCount ?? 0) > 0;
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
let badDecision: string;
|
|
306
|
-
|
|
307
|
-
let betterDecision: string;
|
|
308
|
-
|
|
309
|
-
let rationale: string;
|
|
310
|
-
|
|
311
|
-
if (hasGateBlocks && snapshot.gateBlocks.length > 0) {
|
|
312
|
-
// Use actual gate block content
|
|
313
|
-
|
|
314
|
-
const block = snapshot.gateBlocks[0];
|
|
315
|
-
const tool = block.toolName ?? 'a tool';
|
|
316
|
-
const file = block.filePath ? ` on ${block.filePath}` : '';
|
|
317
|
-
badDecision = `Attempted to invoke ${tool}${file} without satisfying the gate requirements`;
|
|
318
|
-
betterDecision = `Review the gate block reason "${block.reason ?? 'unspecified'}" and resolve the blocking condition before retrying`;
|
|
319
|
-
rationale = `Gate blocks exist for a reason — bypassing them without understanding the underlying constraint risks unintended consequences. The block on ${tool}${file} indicates the operation exceeded allowed thresholds for the current evolution tier.`;
|
|
320
|
-
} else if (hasPain && snapshot.painEvents.length > 0) {
|
|
321
|
-
// Use actual pain event content
|
|
322
|
-
|
|
323
|
-
const pain = snapshot.painEvents[0];
|
|
324
|
-
const painSource = pain.source ?? 'unknown';
|
|
325
|
-
const painReason = pain.reason ? `: ${pain.reason}` : '';
|
|
326
|
-
badDecision = `Continued operating despite ${painSource} pain signal (score ${pain.score ?? 'unknown'})${painReason}`;
|
|
327
|
-
betterDecision = `Pause and analyze the ${painSource} signal — the pain indicates accumulated friction that should be diagnosed before proceeding`;
|
|
328
|
-
rationale = `Pain signals from ${painSource} are early warnings of systemic issues. Score ${pain.score ?? 'N/A'} indicates ${((pain.score ?? 0) >= 70) ? 'severe' : ((pain.score ?? 0) >= 40) ? 'moderate' : 'mild'} friction that should be addressed before continuing operations.`;
|
|
329
|
-
} else if (hasFailures && snapshot.toolCalls.length > 0) {
|
|
330
|
-
// Use actual tool failure content
|
|
331
|
-
const failedCall = snapshot.toolCalls.find(tc => tc.outcome === 'failure');
|
|
332
|
-
if (failedCall) {
|
|
333
|
-
const tool = failedCall.toolName ?? 'a tool';
|
|
334
|
-
const file = failedCall.filePath ? ` on ${failedCall.filePath}` : '';
|
|
335
|
-
const error = failedCall.errorMessage ? ` — ${failedCall.errorMessage}` : '';
|
|
336
|
-
badDecision = `Retried ${tool}${file} after failure without first diagnosing the root cause${error}`;
|
|
337
|
-
betterDecision = `Examine the error details (${failedCall.errorType ?? 'unknown type'}${error ? error : ''}) and verify preconditions before attempting ${tool} again`;
|
|
338
|
-
rationale = `Tool failures are opportunities for learning. The ${tool} failure${file} with error type ${failedCall.errorType ?? 'unknown'} suggests a gap in precondition checking or error handling that should be addressed to prevent recurrence.`;
|
|
339
|
-
} else {
|
|
340
|
-
badDecision = `Retried a failing operation without diagnosing the root cause of the failure`;
|
|
341
|
-
betterDecision = `Based on the evidence from the error logs, let me first check the actual source code to understand the precondition before retrying`;
|
|
342
|
-
rationale = `Diagnosing failures before retry prevents repeated failures and respects the cost of each action attempt`;
|
|
343
|
-
}
|
|
344
|
-
} else {
|
|
345
|
-
// Fallback — no specific signal content available
|
|
346
|
-
badDecision = `Proceeded with an operation without verifying preconditions or checking for conflicting changes`;
|
|
347
|
-
betterDecision = `Let me first understand the current state of the codebase by reading the relevant files before making any changes`;
|
|
348
|
-
rationale = `Verifying preconditions and current state prevents errors and ensures actions are appropriate for the actual situation`;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
// Compute design-alignment reflection quality metrics
|
|
352
|
-
const thinkingModelDelta = computeThinkingModelDelta(badDecision, betterDecision);
|
|
353
|
-
// Stub reflectors don't have an improved snapshot, so planningRatioGain is 0
|
|
354
|
-
const planningRatioGain = 0;
|
|
355
|
-
|
|
356
|
-
const artifact = {
|
|
357
|
-
artifactId,
|
|
358
|
-
sessionId: snapshot.sessionId,
|
|
359
|
-
principleId,
|
|
360
|
-
sourceSnapshotRef: `snapshot-${snapshot.sessionId}-${Date.now()}`,
|
|
361
|
-
badDecision,
|
|
362
|
-
betterDecision,
|
|
363
|
-
rationale,
|
|
364
|
-
createdAt: now,
|
|
365
|
-
thinkingModelDelta,
|
|
366
|
-
planningRatioGain,
|
|
367
|
-
};
|
|
368
|
-
|
|
369
|
-
return JSON.stringify(artifact);
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
// ---------------------------------------------------------------------------
|
|
373
|
-
// Artifact Persistence
|
|
374
|
-
// ---------------------------------------------------------------------------
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Persist an approved artifact to the samples directory.
|
|
378
|
-
* Returns the absolute path where the artifact was saved.
|
|
379
|
-
*/
|
|
380
|
-
function persistArtifact(
|
|
381
|
-
workspaceDir: string,
|
|
382
|
-
artifact: NocturnalArtifact & { boundedAction?: BoundedAction }
|
|
383
|
-
): string {
|
|
384
|
-
const artifactPath = NocturnalPathResolver.samplePath(workspaceDir, artifact.artifactId);
|
|
385
|
-
|
|
386
|
-
const sampleRecord = {
|
|
387
|
-
...artifact,
|
|
388
|
-
status: 'approved' as const,
|
|
389
|
-
boundedAction: artifact.boundedAction,
|
|
390
|
-
persistedAt: new Date().toISOString(),
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
// Ensure directory exists
|
|
394
|
-
const dir = path.dirname(artifactPath);
|
|
395
|
-
if (!fs.existsSync(dir)) {
|
|
396
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
atomicWriteFileSync(artifactPath, JSON.stringify(sampleRecord, null, 2));
|
|
400
|
-
return artifactPath;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
function buildPainRefs(snapshot: NocturnalSessionSnapshot): string[] {
|
|
404
|
-
return snapshot.painEvents.map(
|
|
405
|
-
(painEvent) =>
|
|
406
|
-
`pain:${painEvent.source}:${painEvent.createdAt}:${(painEvent.reason ?? '').trim()}`
|
|
407
|
-
);
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
function buildGateBlockRefs(snapshot: NocturnalSessionSnapshot): string[] {
|
|
411
|
-
return snapshot.gateBlocks.map(
|
|
412
|
-
(gateBlock) =>
|
|
413
|
-
`gate:${gateBlock.toolName}:${gateBlock.createdAt}:${gateBlock.reason.trim()}`
|
|
414
|
-
);
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
function buildDefaultArtificerOutput(
|
|
420
|
-
ruleId: string,
|
|
421
|
-
artifact: NocturnalArtifact,
|
|
422
|
-
sourceSnapshotRef: string,
|
|
423
|
-
sourcePainIds: string[],
|
|
424
|
-
sourceGateBlockIds: string[]
|
|
425
|
-
): ArtificerOutput {
|
|
426
|
-
return {
|
|
427
|
-
ruleId,
|
|
428
|
-
implementationType: 'code',
|
|
429
|
-
candidateSource: [
|
|
430
|
-
'export const meta = {',
|
|
431
|
-
` name: ${JSON.stringify(`nocturnal-${ruleId.toLowerCase()}`)},`,
|
|
432
|
-
' version: "1.0.0",',
|
|
433
|
-
` ruleId: ${JSON.stringify(ruleId)},`,
|
|
434
|
-
` coversCondition: ${JSON.stringify(artifact.betterDecision)},`,
|
|
435
|
-
'};',
|
|
436
|
-
'',
|
|
437
|
-
'export function evaluate(input, helpers) {',
|
|
438
|
-
' const riskPath = helpers.isRiskPath();',
|
|
439
|
-
' const toolName = helpers.getToolName();',
|
|
440
|
-
' const planStatus = helpers.getPlanStatus();',
|
|
441
|
-
" if (riskPath && toolName === 'write' && planStatus !== 'READY') {",
|
|
442
|
-
' return {',
|
|
443
|
-
" decision: 'requireApproval',",
|
|
444
|
-
' matched: true,',
|
|
445
|
-
` reason: ${JSON.stringify(artifact.rationale)},`,
|
|
446
|
-
' };',
|
|
447
|
-
' }',
|
|
448
|
-
' return {',
|
|
449
|
-
" decision: 'allow',",
|
|
450
|
-
' matched: false,',
|
|
451
|
-
" reason: 'not-applicable',",
|
|
452
|
-
' };',
|
|
453
|
-
'}',
|
|
454
|
-
].join('\n'),
|
|
455
|
-
helperUsage: ['isRiskPath', 'getToolName', 'getPlanStatus'],
|
|
456
|
-
expectedDecision: 'requireApproval',
|
|
457
|
-
rationale: artifact.rationale,
|
|
458
|
-
lineage: {
|
|
459
|
-
artifactKind: 'rule-implementation-candidate',
|
|
460
|
-
sourceSnapshotRef,
|
|
461
|
-
sourcePainIds,
|
|
462
|
-
sourceGateBlockIds,
|
|
463
|
-
},
|
|
464
|
-
};
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
function persistCodeCandidate(
|
|
470
|
-
workspaceDir: string,
|
|
471
|
-
stateDir: string,
|
|
472
|
-
artifact: NocturnalArtifact,
|
|
473
|
-
selectedPrincipleId: string,
|
|
474
|
-
selectedSessionId: string,
|
|
475
|
-
parsedArtificer: ArtificerOutput
|
|
476
|
-
): NocturnalArtificerDiagnostics {
|
|
477
|
-
const implementationId = `IMPL-${randomUUID()}`;
|
|
478
|
-
const artifactId = `artifact-${randomUUID()}`;
|
|
479
|
-
const now = new Date().toISOString();
|
|
480
|
-
const assetRoot = getImplementationAssetRoot(stateDir, implementationId);
|
|
481
|
-
const entryPath = path.join(assetRoot, 'entry.js');
|
|
482
|
-
const lineage: CodeImplementationLineageMetadata = {
|
|
483
|
-
principleId: selectedPrincipleId,
|
|
484
|
-
ruleId: parsedArtificer.ruleId,
|
|
485
|
-
sourceSnapshotRef: artifact.sourceSnapshotRef,
|
|
486
|
-
sourcePainIds: [...parsedArtificer.lineage.sourcePainIds],
|
|
487
|
-
sourceGateBlockIds: [...parsedArtificer.lineage.sourceGateBlockIds],
|
|
488
|
-
sourceSessionId: selectedSessionId,
|
|
489
|
-
artificerArtifactId: artifactId,
|
|
490
|
-
};
|
|
491
|
-
|
|
492
|
-
const implementation: Implementation = {
|
|
493
|
-
id: implementationId,
|
|
494
|
-
ruleId: parsedArtificer.ruleId,
|
|
495
|
-
type: 'code',
|
|
496
|
-
path: entryPath,
|
|
497
|
-
version: now,
|
|
498
|
-
coversCondition: parsedArtificer.rationale,
|
|
499
|
-
coveragePercentage: 0,
|
|
500
|
-
lifecycleState: 'candidate',
|
|
501
|
-
createdAt: now,
|
|
502
|
-
updatedAt: now,
|
|
503
|
-
};
|
|
504
|
-
|
|
505
|
-
try {
|
|
506
|
-
createImplementation(stateDir, implementation);
|
|
507
|
-
createImplementationAssetDir(stateDir, implementationId, now, {
|
|
508
|
-
entrySource: parsedArtificer.candidateSource,
|
|
509
|
-
lineage,
|
|
510
|
-
});
|
|
511
|
-
appendCandidateArtifactLineageRecord(workspaceDir, {
|
|
512
|
-
artifactId,
|
|
513
|
-
principleId: selectedPrincipleId,
|
|
514
|
-
ruleId: parsedArtificer.ruleId,
|
|
515
|
-
sessionId: selectedSessionId,
|
|
516
|
-
sourceSnapshotRef: artifact.sourceSnapshotRef,
|
|
517
|
-
sourcePainIds: lineage.sourcePainIds,
|
|
518
|
-
sourceGateBlockIds: lineage.sourceGateBlockIds,
|
|
519
|
-
storagePath: assetRoot,
|
|
520
|
-
implementationId,
|
|
521
|
-
createdAt: now,
|
|
522
|
-
});
|
|
523
|
-
try {
|
|
524
|
-
refreshPrincipleLifecycle(workspaceDir, stateDir);
|
|
525
|
-
} catch (err) {
|
|
526
|
-
console.warn('[nocturnal-service] Lifecycle refresh failed after code candidate persistence:', err instanceof Error ? err.stack : err);
|
|
527
|
-
}
|
|
528
|
-
// PD-FUNNEL-2.3: Emit nocturnal_code_candidate_created event
|
|
529
|
-
try {
|
|
530
|
-
const eventLog = EventLogService.get(stateDir, undefined);
|
|
531
|
-
eventLog.recordNocturnalCodeCandidateCreated({
|
|
532
|
-
implementationId,
|
|
533
|
-
artifactId,
|
|
534
|
-
ruleId: parsedArtificer.ruleId,
|
|
535
|
-
persistedPath: assetRoot,
|
|
536
|
-
});
|
|
537
|
-
} catch (evErr) {
|
|
538
|
-
console.warn(`[nocturnal-service] Failed to record nocturnal_code_candidate_created: ${String(evErr)}`);
|
|
539
|
-
}
|
|
540
|
-
return {
|
|
541
|
-
status: 'persisted_candidate',
|
|
542
|
-
ruleResolution: {
|
|
543
|
-
status: 'selected',
|
|
544
|
-
ruleId: parsedArtificer.ruleId,
|
|
545
|
-
reason: 'evidence-winner',
|
|
546
|
-
scores: [],
|
|
547
|
-
},
|
|
548
|
-
validationFailures: [],
|
|
549
|
-
implementationId,
|
|
550
|
-
artifactId,
|
|
551
|
-
ruleId: parsedArtificer.ruleId,
|
|
552
|
-
persistedPath: assetRoot,
|
|
553
|
-
};
|
|
554
|
-
} catch (error: unknown) {
|
|
555
|
-
deleteImplementationAssetDir(stateDir, implementationId);
|
|
556
|
-
try {
|
|
557
|
-
deleteImplementation(stateDir, implementationId);
|
|
558
|
-
} catch {
|
|
559
|
-
// Best effort cleanup to avoid leaving a half-created candidate discoverable.
|
|
560
|
-
}
|
|
561
|
-
return {
|
|
562
|
-
status: 'validation_failed',
|
|
563
|
-
reason: 'persistence_failed',
|
|
564
|
-
ruleResolution: {
|
|
565
|
-
status: 'selected',
|
|
566
|
-
ruleId: parsedArtificer.ruleId,
|
|
567
|
-
reason: 'evidence-winner',
|
|
568
|
-
scores: [],
|
|
569
|
-
},
|
|
570
|
-
validationFailures: [String(error)],
|
|
571
|
-
ruleId: parsedArtificer.ruleId,
|
|
572
|
-
};
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
function maybePersistArtificerCandidate(
|
|
579
|
-
workspaceDir: string,
|
|
580
|
-
stateDir: string,
|
|
581
|
-
selectedPrincipleId: string,
|
|
582
|
-
selectedSessionId: string,
|
|
583
|
-
snapshot: NocturnalSessionSnapshot,
|
|
584
|
-
artifact: NocturnalArtifact,
|
|
585
|
-
options: NocturnalServiceOptions
|
|
586
|
-
): NocturnalArtificerDiagnostics {
|
|
587
|
-
const ruleResolution = resolveArtificerTargetRule(
|
|
588
|
-
stateDir,
|
|
589
|
-
selectedPrincipleId,
|
|
590
|
-
snapshot
|
|
591
|
-
);
|
|
592
|
-
|
|
593
|
-
if (ruleResolution.status !== 'selected') {
|
|
594
|
-
return {
|
|
595
|
-
status: 'skipped',
|
|
596
|
-
reason: 'no_deterministic_rule',
|
|
597
|
-
ruleResolution,
|
|
598
|
-
validationFailures: [],
|
|
599
|
-
};
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
// #219: Detect fallback data source and warn about potential signal inaccuracy
|
|
603
|
-
const validationFailures: string[] = [];
|
|
604
|
-
if (snapshot._dataSource === 'pain_context_fallback') {
|
|
605
|
-
validationFailures.push('fallback_snapshot: stats derived from pain context only (trajectory extractor failed) - signal counts may be undercounted');
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
if (!shouldRunArtificer(snapshot, ruleResolution)) {
|
|
609
|
-
return {
|
|
610
|
-
status: 'skipped',
|
|
611
|
-
reason: 'insufficient_signal_density',
|
|
612
|
-
ruleResolution,
|
|
613
|
-
validationFailures,
|
|
614
|
-
ruleId: ruleResolution.ruleId,
|
|
615
|
-
};
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
if (!artifact.betterDecision || !artifact.rationale) {
|
|
619
|
-
return {
|
|
620
|
-
status: 'skipped',
|
|
621
|
-
reason: 'missing_scribe_input',
|
|
622
|
-
ruleResolution,
|
|
623
|
-
validationFailures: [],
|
|
624
|
-
ruleId: ruleResolution.ruleId,
|
|
625
|
-
};
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
const sourcePainIds = buildPainRefs(snapshot);
|
|
629
|
-
const sourceGateBlockIds = buildGateBlockRefs(snapshot);
|
|
630
|
-
const parsedArtificer =
|
|
631
|
-
options.artificerOutputOverride !== undefined
|
|
632
|
-
? parseArtificerOutput(options.artificerOutputOverride)
|
|
633
|
-
: buildDefaultArtificerOutput(
|
|
634
|
-
ruleResolution.ruleId,
|
|
635
|
-
artifact,
|
|
636
|
-
artifact.sourceSnapshotRef,
|
|
637
|
-
sourcePainIds,
|
|
638
|
-
sourceGateBlockIds
|
|
639
|
-
);
|
|
640
|
-
|
|
641
|
-
if (!parsedArtificer) {
|
|
642
|
-
return {
|
|
643
|
-
status: 'validation_failed',
|
|
644
|
-
reason: 'parse_failed',
|
|
645
|
-
ruleResolution,
|
|
646
|
-
validationFailures: ['Artificer output could not be parsed.'],
|
|
647
|
-
ruleId: ruleResolution.ruleId,
|
|
648
|
-
};
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
if (parsedArtificer.ruleId !== ruleResolution.ruleId) {
|
|
652
|
-
return {
|
|
653
|
-
status: 'validation_failed',
|
|
654
|
-
reason: 'rule_mismatch',
|
|
655
|
-
ruleResolution,
|
|
656
|
-
validationFailures: [
|
|
657
|
-
`Resolved rule ${ruleResolution.ruleId} did not match candidate rule ${parsedArtificer.ruleId}.`,
|
|
658
|
-
],
|
|
659
|
-
ruleId: ruleResolution.ruleId,
|
|
660
|
-
};
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
const validation = validateRuleImplementationCandidate(parsedArtificer.candidateSource);
|
|
664
|
-
if (!validation.passed) {
|
|
665
|
-
return {
|
|
666
|
-
status: 'validation_failed',
|
|
667
|
-
reason: 'validator_rejected',
|
|
668
|
-
ruleResolution,
|
|
669
|
-
validationFailures: validation.failures.map((failure) => failure.message),
|
|
670
|
-
ruleId: ruleResolution.ruleId,
|
|
671
|
-
};
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
const persisted = persistCodeCandidate(
|
|
675
|
-
workspaceDir,
|
|
676
|
-
stateDir,
|
|
677
|
-
artifact,
|
|
678
|
-
selectedPrincipleId,
|
|
679
|
-
selectedSessionId,
|
|
680
|
-
parsedArtificer
|
|
681
|
-
);
|
|
682
|
-
return {
|
|
683
|
-
...persisted,
|
|
684
|
-
ruleResolution,
|
|
685
|
-
};
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
// ---------------------------------------------------------------------------
|
|
689
|
-
// Main Orchestrator
|
|
690
|
-
// ---------------------------------------------------------------------------
|
|
691
|
-
|
|
692
|
-
/**
|
|
693
|
-
* Execute a complete nocturnal reflection run.
|
|
694
|
-
*
|
|
695
|
-
* Pipeline:
|
|
696
|
-
* 1. Pre-flight check (idle + cooldown + quota)
|
|
697
|
-
* 2. Target selection (principle + violating session)
|
|
698
|
-
* 3. Trajectory snapshot extraction
|
|
699
|
-
* 4. Reflector (stub) → JSON artifact
|
|
700
|
-
* 5. Arbiter validation
|
|
701
|
-
* 6. Executability check
|
|
702
|
-
* 7. Artifact persistence
|
|
703
|
-
* 8. Cooldown recording
|
|
704
|
-
*
|
|
705
|
-
* @param workspaceDir - Workspace directory
|
|
706
|
-
* @param stateDir - State directory
|
|
707
|
-
* @param options - Service configuration options
|
|
708
|
-
* @returns NocturnalRunResult
|
|
709
|
-
*/
|
|
710
|
-
export function executeNocturnalReflection(
|
|
711
|
-
workspaceDir: string,
|
|
712
|
-
stateDir: string,
|
|
713
|
-
options: NocturnalServiceOptions = {}
|
|
714
|
-
): NocturnalRunResult {
|
|
715
|
-
// Use provided logger or fallback to console
|
|
716
|
-
const logger = options.logger;
|
|
717
|
-
|
|
718
|
-
const warn = logger?.warn?.bind(logger) ?? console.warn.bind(console);
|
|
719
|
-
|
|
720
|
-
const diagnostics: NocturnalRunDiagnostics = {
|
|
721
|
-
preflight: null,
|
|
722
|
-
selection: null,
|
|
723
|
-
idle: null,
|
|
724
|
-
trinityAttempted: false,
|
|
725
|
-
trinityResult: null,
|
|
726
|
-
chainModeUsed: null,
|
|
727
|
-
arbiterResult: null,
|
|
728
|
-
executabilityResult: null,
|
|
729
|
-
persisted: false,
|
|
730
|
-
artificer: {
|
|
731
|
-
status: 'skipped',
|
|
732
|
-
ruleResolution: null,
|
|
733
|
-
validationFailures: [],
|
|
734
|
-
},
|
|
735
|
-
};
|
|
736
|
-
|
|
737
|
-
// -------------------------------------------------------------------------
|
|
738
|
-
// Step 1: Pre-flight check
|
|
739
|
-
// -------------------------------------------------------------------------
|
|
740
|
-
const preflight = checkPreflight(
|
|
741
|
-
workspaceDir,
|
|
742
|
-
stateDir,
|
|
743
|
-
undefined, // principleId
|
|
744
|
-
undefined, // trajectoryLastActivityAt
|
|
745
|
-
options.idleCheckOverride,
|
|
746
|
-
!!options.idleCheckOverride // skip cooldown/quota gates for manual/test triggers
|
|
747
|
-
);
|
|
748
|
-
diagnostics.preflight = preflight;
|
|
749
|
-
|
|
750
|
-
if (!preflight.canRun) {
|
|
751
|
-
return {
|
|
752
|
-
success: false,
|
|
753
|
-
noTargetSelected: true,
|
|
754
|
-
skipReason: 'preflight_blocked',
|
|
755
|
-
validationFailed: false,
|
|
756
|
-
validationFailures: [],
|
|
757
|
-
diagnostics,
|
|
758
|
-
};
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
// -------------------------------------------------------------------------
|
|
762
|
-
// Step 2: Target selection
|
|
763
|
-
// -------------------------------------------------------------------------
|
|
764
|
-
const extractor = createNocturnalTrajectoryExtractor(workspaceDir, stateDir);
|
|
765
|
-
const selector = new NocturnalTargetSelector(workspaceDir, stateDir, extractor, {
|
|
766
|
-
idleCheckOverride: options.idleCheckOverride,
|
|
767
|
-
recentPainContext: options.painContext,
|
|
768
|
-
});
|
|
769
|
-
|
|
770
|
-
const selection = selector.select();
|
|
771
|
-
diagnostics.selection = selection;
|
|
772
|
-
|
|
773
|
-
if (selection.decision === 'skip') {
|
|
774
|
-
return {
|
|
775
|
-
success: false,
|
|
776
|
-
noTargetSelected: true,
|
|
777
|
-
skipReason: selection.skipReason,
|
|
778
|
-
validationFailed: false,
|
|
779
|
-
validationFailures: [],
|
|
780
|
-
diagnostics,
|
|
781
|
-
};
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
const { selectedPrincipleId, selectedSessionId } = selection;
|
|
785
|
-
|
|
786
|
-
// -------------------------------------------------------------------------
|
|
787
|
-
// Step 3: Trajectory snapshot extraction
|
|
788
|
-
// -------------------------------------------------------------------------
|
|
789
|
-
if (!selectedPrincipleId || !selectedSessionId) {
|
|
790
|
-
return {
|
|
791
|
-
success: false,
|
|
792
|
-
noTargetSelected: true,
|
|
793
|
-
validationFailed: false,
|
|
794
|
-
validationFailures: [],
|
|
795
|
-
diagnostics,
|
|
796
|
-
};
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
const snapshot = extractor.getNocturnalSessionSnapshot(selectedSessionId);
|
|
800
|
-
if (!snapshot) {
|
|
801
|
-
return {
|
|
802
|
-
success: false,
|
|
803
|
-
noTargetSelected: true,
|
|
804
|
-
skipReason: 'insufficient_snapshot_data',
|
|
805
|
-
validationFailed: false,
|
|
806
|
-
validationFailures: [],
|
|
807
|
-
diagnostics,
|
|
808
|
-
};
|
|
809
|
-
}
|
|
810
|
-
diagnostics.idle = { isIdle: true, mostRecentActivityAt: 0, idleForMs: 0, userActiveSessions: 0, abandonedSessionIds: [], trajectoryGuardrailConfirmsIdle: true, reason: 'preflight passed' };
|
|
811
|
-
|
|
812
|
-
// -------------------------------------------------------------------------
|
|
813
|
-
// Step 4: Record run start (begin cooldown window)
|
|
814
|
-
// -------------------------------------------------------------------------
|
|
815
|
-
// Note: We use a sync approximation here since this is called from sync context
|
|
816
|
-
// The async version would be used in real worker integration
|
|
817
|
-
const config = loadNocturnalConfig(stateDir);
|
|
818
|
-
void recordRunStart(stateDir, selectedPrincipleId, config.cooldown_ms).catch((err) => {
|
|
819
|
-
warn(`[nocturnal-service] Failed to record run start: ${String(err)}`);
|
|
820
|
-
});
|
|
821
|
-
|
|
822
|
-
// -------------------------------------------------------------------------
|
|
823
|
-
// Step 5: Artifact generation (Trinity or single-reflector)
|
|
824
|
-
// -------------------------------------------------------------------------
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
let trinityArtifact: TrinityDraftArtifact | null = null;
|
|
828
|
-
let trinityResult: TrinityResult | null = null;
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
let rawJson: string;
|
|
832
|
-
|
|
833
|
-
if (options.skipReflector) {
|
|
834
|
-
// Caller provided explicit artifact — used for testing arbiter/executability
|
|
835
|
-
if (!options.reflectorOutputOverride) {
|
|
836
|
-
return {
|
|
837
|
-
success: false,
|
|
838
|
-
noTargetSelected: false,
|
|
839
|
-
validationFailed: true,
|
|
840
|
-
validationFailures: ['skipReflector is true but no reflectorOutputOverride provided'],
|
|
841
|
-
diagnostics,
|
|
842
|
-
};
|
|
843
|
-
}
|
|
844
|
-
rawJson = options.reflectorOutputOverride;
|
|
845
|
-
} else if (options.trinityResultOverride) {
|
|
846
|
-
// Testing override — use provided Trinity result
|
|
847
|
-
trinityResult = options.trinityResultOverride;
|
|
848
|
-
diagnostics.trinityAttempted = true;
|
|
849
|
-
diagnostics.trinityResult = trinityResult;
|
|
850
|
-
diagnostics.chainModeUsed = trinityResult.success ? 'trinity' : 'single-reflector';
|
|
851
|
-
|
|
852
|
-
if (!trinityResult.success) {
|
|
853
|
-
// Trinity failed — fail closed (same semantics as production)
|
|
854
|
-
const failures = trinityResult.failures.map((f) => `${f.stage}: ${f.reason}`);
|
|
855
|
-
void recordRunEnd(stateDir, 'failed', { reason: `Trinity override failed: ${failures.join('; ')}` }).catch((err) => {
|
|
856
|
-
warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
857
|
-
});
|
|
858
|
-
// Emit threshold signals: malformed Trinity override is a strong signal
|
|
859
|
-
adjustThresholdsFromSignals(stateDir, {
|
|
860
|
-
malformedRate: 1.0,
|
|
861
|
-
arbiterRejectRate: 0.0,
|
|
862
|
-
executabilityRejectRate: 0.0,
|
|
863
|
-
qualityDelta: 0.0,
|
|
864
|
-
});
|
|
865
|
-
return {
|
|
866
|
-
success: false,
|
|
867
|
-
noTargetSelected: false,
|
|
868
|
-
validationFailed: true,
|
|
869
|
-
validationFailures: [`Trinity override failed: ${failures.join('; ')}`],
|
|
870
|
-
snapshot,
|
|
871
|
-
diagnostics,
|
|
872
|
-
};
|
|
873
|
-
} else {
|
|
874
|
-
// Validate Trinity draft
|
|
875
|
-
const draftValidation = validateTrinityDraft(trinityResult.artifact);
|
|
876
|
-
if (!draftValidation.valid) {
|
|
877
|
-
const {failures} = draftValidation;
|
|
878
|
-
void recordRunEnd(stateDir, 'failed', { reason: `Trinity draft invalid: ${failures.join('; ')}` }).catch((err) => {
|
|
879
|
-
warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
880
|
-
});
|
|
881
|
-
// Emit threshold signals: malformed draft content is a strong signal
|
|
882
|
-
adjustThresholdsFromSignals(stateDir, {
|
|
883
|
-
malformedRate: 1.0,
|
|
884
|
-
arbiterRejectRate: 0.0,
|
|
885
|
-
executabilityRejectRate: 0.0,
|
|
886
|
-
qualityDelta: 0.0,
|
|
887
|
-
});
|
|
888
|
-
return {
|
|
889
|
-
success: false,
|
|
890
|
-
noTargetSelected: false,
|
|
891
|
-
validationFailed: true,
|
|
892
|
-
validationFailures: failures,
|
|
893
|
-
snapshot,
|
|
894
|
-
diagnostics,
|
|
895
|
-
};
|
|
896
|
-
}
|
|
897
|
-
trinityArtifact = trinityResult.artifact!;
|
|
898
|
-
// Convert Trinity draft to arbiter-compatible artifact
|
|
899
|
-
const artifactData = draftToArtifact(trinityArtifact);
|
|
900
|
-
rawJson = JSON.stringify(artifactData);
|
|
901
|
-
}
|
|
902
|
-
} else {
|
|
903
|
-
// Normal execution: try Trinity first, fall back to single-reflector
|
|
904
|
-
const trinityConfig: TrinityConfig = {
|
|
905
|
-
...DEFAULT_TRINITY_CONFIG,
|
|
906
|
-
...options.trinityConfig,
|
|
907
|
-
stateDir, // Enable threshold loading/persistence
|
|
908
|
-
};
|
|
909
|
-
|
|
910
|
-
// If useStubs=false but no runtimeAdapter provided in sync context,
|
|
911
|
-
// fall back to stub behavior (graceful degradation).
|
|
912
|
-
// For real async execution, use executeNocturnalReflectionAsync with a runtimeAdapter.
|
|
913
|
-
const effectiveConfig: TrinityConfig = trinityConfig.useTrinity && !trinityConfig.useStubs && !options.runtimeAdapter
|
|
914
|
-
? { ...trinityConfig, useStubs: true }
|
|
915
|
-
: trinityConfig;
|
|
916
|
-
|
|
917
|
-
if (effectiveConfig.useTrinity) {
|
|
918
|
-
diagnostics.trinityAttempted = true;
|
|
919
|
-
trinityResult = runTrinity({ snapshot, principleId: selectedPrincipleId, config: effectiveConfig });
|
|
920
|
-
diagnostics.trinityResult = trinityResult;
|
|
921
|
-
diagnostics.chainModeUsed = trinityResult.success ? 'trinity' : 'single-reflector';
|
|
922
|
-
|
|
923
|
-
if (trinityResult.success) {
|
|
924
|
-
// Validate Trinity draft
|
|
925
|
-
const draftValidation = validateTrinityDraft(trinityResult.artifact);
|
|
926
|
-
if (!draftValidation.valid) {
|
|
927
|
-
// Trinity draft invalid — fail closed
|
|
928
|
-
const {failures} = draftValidation;
|
|
929
|
-
void recordRunEnd(stateDir, 'failed', { reason: `Trinity draft invalid: ${failures.join('; ')}` }).catch((err) => {
|
|
930
|
-
warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
931
|
-
});
|
|
932
|
-
// Emit threshold signals: malformed draft content is a strong signal
|
|
933
|
-
adjustThresholdsFromSignals(stateDir, {
|
|
934
|
-
malformedRate: 1.0,
|
|
935
|
-
arbiterRejectRate: 0.0,
|
|
936
|
-
executabilityRejectRate: 0.0,
|
|
937
|
-
qualityDelta: 0.0,
|
|
938
|
-
});
|
|
939
|
-
return {
|
|
940
|
-
success: false,
|
|
941
|
-
noTargetSelected: false,
|
|
942
|
-
validationFailed: true,
|
|
943
|
-
validationFailures: failures,
|
|
944
|
-
snapshot,
|
|
945
|
-
diagnostics,
|
|
946
|
-
};
|
|
947
|
-
}
|
|
948
|
-
trinityArtifact = trinityResult.artifact!;
|
|
949
|
-
// Convert Trinity draft to arbiter-compatible artifact
|
|
950
|
-
const artifactData = draftToArtifact(trinityArtifact);
|
|
951
|
-
rawJson = JSON.stringify(artifactData);
|
|
952
|
-
} else {
|
|
953
|
-
// Trinity failed — fail closed (do NOT fall back to single-reflector)
|
|
954
|
-
// Phase 6 requirement: malformed Trinity stage output fails closed
|
|
955
|
-
const failures = trinityResult.failures.map((f) => `${f.stage}: ${f.reason}`);
|
|
956
|
-
void recordRunEnd(stateDir, 'failed', { reason: `Trinity chain failed: ${failures.join('; ')}` }).catch((err) => {
|
|
957
|
-
warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
958
|
-
});
|
|
959
|
-
// Emit threshold signals: malformed Trinity is the strongest signal for tightening schema threshold
|
|
960
|
-
adjustThresholdsFromSignals(stateDir, {
|
|
961
|
-
malformedRate: 1.0,
|
|
962
|
-
arbiterRejectRate: 0.0,
|
|
963
|
-
executabilityRejectRate: 0.0,
|
|
964
|
-
qualityDelta: 0.0,
|
|
965
|
-
});
|
|
966
|
-
return {
|
|
967
|
-
success: false,
|
|
968
|
-
noTargetSelected: false,
|
|
969
|
-
validationFailed: true,
|
|
970
|
-
validationFailures: [`Trinity chain failed: ${failures.join('; ')}`],
|
|
971
|
-
snapshot,
|
|
972
|
-
diagnostics,
|
|
973
|
-
};
|
|
974
|
-
}
|
|
975
|
-
} else {
|
|
976
|
-
// Trinity disabled — use single-reflector directly
|
|
977
|
-
rawJson = invokeStubReflector(snapshot, selectedPrincipleId);
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
// -------------------------------------------------------------------------
|
|
982
|
-
// Step 6: Arbiter validation
|
|
983
|
-
// -------------------------------------------------------------------------
|
|
984
|
-
// #256: Use 0 for thinkingModelDeltaMin — Trinity chain (Dreamer→Philosopher→Scribe)
|
|
985
|
-
// already ensures quality. A delta of 0 is valid when both bad and better decisions
|
|
986
|
-
// show equally well-reasoned thinking (the Scribe's job is to contrast decisions,
|
|
987
|
-
// not to make one sound more "cognitive" than the other).
|
|
988
|
-
const arbiterResult = parseAndValidateArtifact(rawJson, {
|
|
989
|
-
expectedPrincipleId: selectedPrincipleId,
|
|
990
|
-
expectedSessionId: selectedSessionId,
|
|
991
|
-
qualityThresholds: {
|
|
992
|
-
thinkingModelDeltaMin: 0,
|
|
993
|
-
planningRatioGainMin: -0.5,
|
|
994
|
-
},
|
|
995
|
-
});
|
|
996
|
-
diagnostics.arbiterResult = arbiterResult;
|
|
997
|
-
|
|
998
|
-
if (!arbiterResult.passed || !arbiterResult.artifact) {
|
|
999
|
-
const failures = arbiterResult.failures.map((f) => f.reason);
|
|
1000
|
-
void recordRunEnd(stateDir, 'failed', { reason: failures.join('; ') }).catch((err) => {
|
|
1001
|
-
warn(`[nocturnal-service] Failed to record run end (arbiter failed): ${String(err)}`);
|
|
1002
|
-
});
|
|
1003
|
-
// Emit threshold signals: arbiter rejection indicates principle alignment issues
|
|
1004
|
-
adjustThresholdsFromSignals(stateDir, {
|
|
1005
|
-
malformedRate: 0.0,
|
|
1006
|
-
arbiterRejectRate: 1.0,
|
|
1007
|
-
executabilityRejectRate: 0.0,
|
|
1008
|
-
qualityDelta: 0.0,
|
|
1009
|
-
});
|
|
1010
|
-
return {
|
|
1011
|
-
success: false,
|
|
1012
|
-
noTargetSelected: false,
|
|
1013
|
-
validationFailed: true,
|
|
1014
|
-
validationFailures: failures,
|
|
1015
|
-
diagnostics,
|
|
1016
|
-
};
|
|
1017
|
-
}
|
|
1018
|
-
|
|
1019
|
-
// -------------------------------------------------------------------------
|
|
1020
|
-
// Step 7: Executability check
|
|
1021
|
-
// -------------------------------------------------------------------------
|
|
1022
|
-
const execResult = validateExecutability(arbiterResult.artifact);
|
|
1023
|
-
|
|
1024
|
-
if (!execResult.executable) {
|
|
1025
|
-
const failures = execResult.failures.map((f) => f.reason);
|
|
1026
|
-
void recordRunEnd(stateDir, 'failed', { reason: failures.join('; ') }).catch((err) => {
|
|
1027
|
-
warn(`[nocturnal-service] Failed to record run end (executability failed): ${String(err)}`);
|
|
1028
|
-
});
|
|
1029
|
-
// Emit threshold signals: executability rejection indicates action quality issues
|
|
1030
|
-
adjustThresholdsFromSignals(stateDir, {
|
|
1031
|
-
malformedRate: 0.0,
|
|
1032
|
-
arbiterRejectRate: 0.0,
|
|
1033
|
-
executabilityRejectRate: 1.0,
|
|
1034
|
-
qualityDelta: 0.0,
|
|
1035
|
-
});
|
|
1036
|
-
return {
|
|
1037
|
-
success: false,
|
|
1038
|
-
noTargetSelected: false,
|
|
1039
|
-
validationFailed: true,
|
|
1040
|
-
validationFailures: failures,
|
|
1041
|
-
diagnostics,
|
|
1042
|
-
};
|
|
1043
|
-
}
|
|
1044
|
-
diagnostics.executabilityResult = { executable: true, failures: [] };
|
|
1045
|
-
|
|
1046
|
-
// -------------------------------------------------------------------------
|
|
1047
|
-
// Step 8: Persist artifact
|
|
1048
|
-
// -------------------------------------------------------------------------
|
|
1049
|
-
const artifactWithBoundedAction = {
|
|
1050
|
-
...arbiterResult.artifact,
|
|
1051
|
-
boundedAction: execResult.boundedAction,
|
|
1052
|
-
};
|
|
1053
|
-
|
|
1054
|
-
let persistedPath: string;
|
|
1055
|
-
try {
|
|
1056
|
-
persistedPath = persistArtifact(workspaceDir, artifactWithBoundedAction);
|
|
1057
|
-
diagnostics.persisted = true;
|
|
1058
|
-
diagnostics.persistedPath = persistedPath;
|
|
1059
|
-
// PD-FUNNEL-2.3: Emit nocturnal_artifact_persisted event
|
|
1060
|
-
try {
|
|
1061
|
-
const eventLog = EventLogService.get(stateDir, undefined);
|
|
1062
|
-
eventLog.recordNocturnalArtifactPersisted({
|
|
1063
|
-
artifactId: artifactWithBoundedAction.artifactId,
|
|
1064
|
-
principleId: artifactWithBoundedAction.principleId,
|
|
1065
|
-
persistedPath,
|
|
1066
|
-
});
|
|
1067
|
-
} catch (evErr) {
|
|
1068
|
-
console.warn(`[nocturnal-service] Failed to record nocturnal_artifact_persisted: ${String(evErr)}`);
|
|
1069
|
-
}
|
|
1070
|
-
} catch (err) {
|
|
1071
|
-
void recordRunEnd(stateDir, 'failed', { reason: `persistence error: ${String(err)}` }).catch((e) => {
|
|
1072
|
-
warn(`[nocturnal-service] Failed to record run end (persistence failed): ${String(e)}`);
|
|
1073
|
-
});
|
|
1074
|
-
return {
|
|
1075
|
-
success: false,
|
|
1076
|
-
noTargetSelected: false,
|
|
1077
|
-
validationFailed: true,
|
|
1078
|
-
validationFailures: [`Failed to persist artifact: ${String(err)}`],
|
|
1079
|
-
snapshot,
|
|
1080
|
-
diagnostics,
|
|
1081
|
-
};
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
|
-
// -------------------------------------------------------------------------
|
|
1085
|
-
// Step 8b: Register in dataset lineage store (Phase 3 review gate)
|
|
1086
|
-
// -------------------------------------------------------------------------
|
|
1087
|
-
// Approved artifacts must enter the dataset registry so they can be reviewed
|
|
1088
|
-
// before export. Without this, new samples never appear in the review queue.
|
|
1089
|
-
try {
|
|
1090
|
-
const regResult = registerSample(workspaceDir, arbiterResult.artifact, persistedPath, null);
|
|
1091
|
-
if (regResult.isNew) {
|
|
1092
|
-
incrementGeneratedSampleCount(stateDir, arbiterResult.artifact.principleId);
|
|
1093
|
-
}
|
|
1094
|
-
} catch (err) {
|
|
1095
|
-
// Non-fatal: artifact is persisted, registry is secondary.
|
|
1096
|
-
// Log but don't fail the run.
|
|
1097
|
-
warn(`[nocturnal-service] Failed to register sample in dataset registry: ${String(err)}`);
|
|
1098
|
-
}
|
|
1099
|
-
|
|
1100
|
-
try {
|
|
1101
|
-
appendArtifactLineageRecord(workspaceDir, {
|
|
1102
|
-
artifactKind: 'behavioral-sample',
|
|
1103
|
-
artifactId: arbiterResult.artifact.artifactId,
|
|
1104
|
-
principleId: selectedPrincipleId,
|
|
1105
|
-
ruleId: null,
|
|
1106
|
-
sessionId: selectedSessionId,
|
|
1107
|
-
sourceSnapshotRef: arbiterResult.artifact.sourceSnapshotRef,
|
|
1108
|
-
sourcePainIds: buildPainRefs(snapshot),
|
|
1109
|
-
sourceGateBlockIds: buildGateBlockRefs(snapshot),
|
|
1110
|
-
storagePath: persistedPath,
|
|
1111
|
-
implementationId: null,
|
|
1112
|
-
createdAt: arbiterResult.artifact.createdAt,
|
|
1113
|
-
});
|
|
1114
|
-
} catch (err) {
|
|
1115
|
-
warn(`[nocturnal-service] Failed to append behavioral artifact lineage: ${String(err)}`);
|
|
1116
|
-
}
|
|
1117
|
-
|
|
1118
|
-
diagnostics.artificer = maybePersistArtificerCandidate(
|
|
1119
|
-
workspaceDir,
|
|
1120
|
-
stateDir,
|
|
1121
|
-
selectedPrincipleId,
|
|
1122
|
-
selectedSessionId,
|
|
1123
|
-
snapshot,
|
|
1124
|
-
arbiterResult.artifact,
|
|
1125
|
-
options
|
|
1126
|
-
);
|
|
1127
|
-
|
|
1128
|
-
// -------------------------------------------------------------------------
|
|
1129
|
-
// Step 9: Record run success
|
|
1130
|
-
// -------------------------------------------------------------------------
|
|
1131
|
-
void recordRunEnd(stateDir, 'success', { sampleCount: 1 }).catch((err) => {
|
|
1132
|
-
warn(`[nocturnal-service] Failed to record run end (success): ${String(err)}`);
|
|
1133
|
-
});
|
|
1134
|
-
|
|
1135
|
-
// -------------------------------------------------------------------------
|
|
1136
|
-
// Step 10: Adaptive threshold adjustment based on run signals
|
|
1137
|
-
// -------------------------------------------------------------------------
|
|
1138
|
-
// Compute signals from this run's outcomes and adjust thresholds if needed
|
|
1139
|
-
const malformedRate = trinityResult && !trinityResult.success ? 1.0 : 0.0;
|
|
1140
|
-
const arbiterRejectRate = !arbiterResult.passed ? 1.0 : 0.0;
|
|
1141
|
-
const executabilityRejectRate = !execResult.executable ? 1.0 : 0.0;
|
|
1142
|
-
// qualityDelta requires reviewed-subset comparison infrastructure (Phase 7+)
|
|
1143
|
-
const qualityDelta = 0.0;
|
|
1144
|
-
|
|
1145
|
-
const signals: ThresholdSignals = {
|
|
1146
|
-
malformedRate,
|
|
1147
|
-
arbiterRejectRate,
|
|
1148
|
-
executabilityRejectRate,
|
|
1149
|
-
qualityDelta,
|
|
1150
|
-
};
|
|
1151
|
-
|
|
1152
|
-
// Apply threshold adjustments based on run signals (fire-and-forget, non-blocking)
|
|
1153
|
-
// Note: adjustThresholdsFromSignals is synchronous, so no .catch() needed
|
|
1154
|
-
adjustThresholdsFromSignals(stateDir, signals);
|
|
1155
|
-
|
|
1156
|
-
return {
|
|
1157
|
-
success: true,
|
|
1158
|
-
artifact: artifactWithBoundedAction,
|
|
1159
|
-
noTargetSelected: false,
|
|
1160
|
-
validationFailed: false,
|
|
1161
|
-
validationFailures: [],
|
|
1162
|
-
snapshot,
|
|
1163
|
-
diagnostics,
|
|
1164
|
-
trinityTelemetry: trinityResult?.telemetry,
|
|
1165
|
-
};
|
|
1166
|
-
}
|
|
1167
|
-
|
|
1168
|
-
// ---------------------------------------------------------------------------
|
|
1169
|
-
// Convenience function for async contexts (e.g., worker integration)
|
|
1170
|
-
// ---------------------------------------------------------------------------
|
|
1171
|
-
|
|
1172
|
-
/**
|
|
1173
|
-
* Async wrapper for executeNocturnalReflection.
|
|
1174
|
-
* When runtimeAdapter is provided in options, uses runTrinityAsync for real subagent execution.
|
|
1175
|
-
* Otherwise falls back to synchronous executeNocturnalReflection.
|
|
1176
|
-
*/
|
|
1177
|
-
export async function executeNocturnalReflectionAsync(
|
|
1178
|
-
workspaceDir: string,
|
|
1179
|
-
stateDir: string,
|
|
1180
|
-
options: NocturnalServiceOptions = {}
|
|
1181
|
-
): Promise<NocturnalRunResult> {
|
|
1182
|
-
// If no runtime adapter and no trinityConfig.override, use sync path
|
|
1183
|
-
if (!options.runtimeAdapter && !options.trinityConfig?.useStubs) {
|
|
1184
|
-
// Sync path with default config (useStubs=false but no adapter = fail)
|
|
1185
|
-
// Fall through to sync wrapper
|
|
1186
|
-
return Promise.resolve(executeNocturnalReflection(workspaceDir, stateDir, options));
|
|
1187
|
-
}
|
|
1188
|
-
|
|
1189
|
-
// If runtime adapter is provided, use async Trinity path
|
|
1190
|
-
if (options.runtimeAdapter) {
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
return executeNocturnalReflectionWithAdapter(workspaceDir, stateDir, options);
|
|
1194
|
-
}
|
|
1195
|
-
|
|
1196
|
-
// Sync path (useStubs=true or other sync options)
|
|
1197
|
-
return Promise.resolve(executeNocturnalReflection(workspaceDir, stateDir, options));
|
|
1198
|
-
}
|
|
1199
|
-
|
|
1200
|
-
/**
|
|
1201
|
-
* Execute nocturnal reflection with real Trinity runtime adapter (async).
|
|
1202
|
-
* This handles the full pipeline with async Trinity stage execution.
|
|
1203
|
-
*/
|
|
1204
|
-
async function executeNocturnalReflectionWithAdapter(
|
|
1205
|
-
workspaceDir: string,
|
|
1206
|
-
stateDir: string,
|
|
1207
|
-
options: NocturnalServiceOptions
|
|
1208
|
-
): Promise<NocturnalRunResult> {
|
|
1209
|
-
// Use provided logger or fallback to console
|
|
1210
|
-
const logger = options.logger;
|
|
1211
|
-
|
|
1212
|
-
const warn = logger?.warn?.bind(logger) ?? console.warn.bind(console);
|
|
1213
|
-
|
|
1214
|
-
const diagnostics: NocturnalRunDiagnostics = {
|
|
1215
|
-
preflight: null,
|
|
1216
|
-
selection: null,
|
|
1217
|
-
idle: null,
|
|
1218
|
-
trinityAttempted: false,
|
|
1219
|
-
trinityResult: null,
|
|
1220
|
-
chainModeUsed: null,
|
|
1221
|
-
arbiterResult: null,
|
|
1222
|
-
executabilityResult: null,
|
|
1223
|
-
persisted: false,
|
|
1224
|
-
artificer: {
|
|
1225
|
-
status: 'skipped',
|
|
1226
|
-
ruleResolution: null,
|
|
1227
|
-
validationFailures: [],
|
|
1228
|
-
},
|
|
1229
|
-
};
|
|
1230
|
-
|
|
1231
|
-
// Step 1: Pre-flight check
|
|
1232
|
-
const preflight = checkPreflight(
|
|
1233
|
-
workspaceDir,
|
|
1234
|
-
stateDir,
|
|
1235
|
-
undefined,
|
|
1236
|
-
undefined,
|
|
1237
|
-
options.idleCheckOverride,
|
|
1238
|
-
!!options.idleCheckOverride // skip cooldown/quota gates for manual/test triggers
|
|
1239
|
-
);
|
|
1240
|
-
diagnostics.preflight = preflight;
|
|
1241
|
-
|
|
1242
|
-
if (!preflight.canRun) {
|
|
1243
|
-
return {
|
|
1244
|
-
success: false,
|
|
1245
|
-
noTargetSelected: true,
|
|
1246
|
-
skipReason: 'preflight_blocked',
|
|
1247
|
-
validationFailed: false,
|
|
1248
|
-
validationFailures: [],
|
|
1249
|
-
diagnostics
|
|
1250
|
-
};
|
|
1251
|
-
}
|
|
1252
|
-
|
|
1253
|
-
// Step 2: Target selection (or use override to skip)
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
let selectedPrincipleId: string | undefined;
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
let selectedSessionId: string | undefined;
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
let snapshot: NocturnalSessionSnapshot | null = null;
|
|
1263
|
-
|
|
1264
|
-
if (options.principleIdOverride && options.snapshotOverride) {
|
|
1265
|
-
const snapshotValidation = validateNocturnalSnapshotIngress(options.snapshotOverride);
|
|
1266
|
-
if (snapshotValidation.status !== 'valid' || !snapshotValidation.snapshot) {
|
|
1267
|
-
return {
|
|
1268
|
-
success: false,
|
|
1269
|
-
skipReason: 'insufficient_snapshot_data',
|
|
1270
|
-
noTargetSelected: true,
|
|
1271
|
-
validationFailed: true,
|
|
1272
|
-
validationFailures: snapshotValidation.reasons.length > 0
|
|
1273
|
-
? snapshotValidation.reasons
|
|
1274
|
-
: ['invalid snapshot override'],
|
|
1275
|
-
snapshot: undefined,
|
|
1276
|
-
diagnostics,
|
|
1277
|
-
trinityTelemetry: undefined,
|
|
1278
|
-
};
|
|
1279
|
-
}
|
|
1280
|
-
|
|
1281
|
-
// Skip Selector: use provided principleId and snapshot directly
|
|
1282
|
-
selectedPrincipleId = options.principleIdOverride;
|
|
1283
|
-
selectedSessionId = snapshotValidation.snapshot.sessionId;
|
|
1284
|
-
|
|
1285
|
-
snapshot = snapshotValidation.snapshot;
|
|
1286
|
-
// Calculate violation density from snapshot stats for meaningful diagnostics
|
|
1287
|
-
const snapStats = snapshotValidation.snapshot.stats;
|
|
1288
|
-
const totalToolCalls = snapStats?.totalToolCalls ?? 0;
|
|
1289
|
-
const failureCount = snapStats?.failureCount ?? 0;
|
|
1290
|
-
const violationDensity = totalToolCalls > 0 ? failureCount / totalToolCalls : 0;
|
|
1291
|
-
diagnostics.selection = {
|
|
1292
|
-
decision: 'selected',
|
|
1293
|
-
selectedPrincipleId,
|
|
1294
|
-
selectedSessionId,
|
|
1295
|
-
skipReason: undefined,
|
|
1296
|
-
diagnostics: {
|
|
1297
|
-
totalEvaluablePrinciples: 1, // We provided one principle via override
|
|
1298
|
-
filteredByCooldown: 0,
|
|
1299
|
-
passedPrinciples: [selectedPrincipleId],
|
|
1300
|
-
violatingSessionCount: 1, // The session we're using
|
|
1301
|
-
selectedSessionViolationDensity: violationDensity,
|
|
1302
|
-
selectedPrincipleScore: 100, // Override means high priority
|
|
1303
|
-
scoringBreakdown: { override: 100 },
|
|
1304
|
-
idleCheckPassed: true,
|
|
1305
|
-
cooldownCheckPassed: true,
|
|
1306
|
-
quotaCheckPassed: true,
|
|
1307
|
-
},
|
|
1308
|
-
};
|
|
1309
|
-
diagnostics.idle = {
|
|
1310
|
-
isIdle: true,
|
|
1311
|
-
mostRecentActivityAt: 0,
|
|
1312
|
-
idleForMs: 0,
|
|
1313
|
-
userActiveSessions: 0,
|
|
1314
|
-
abandonedSessionIds: [],
|
|
1315
|
-
trajectoryGuardrailConfirmsIdle: true,
|
|
1316
|
-
reason: 'selector skipped (override provided)',
|
|
1317
|
-
};
|
|
1318
|
-
} else {
|
|
1319
|
-
// Normal Selector path
|
|
1320
|
-
const extractor = createNocturnalTrajectoryExtractor(workspaceDir, stateDir);
|
|
1321
|
-
const selector = new NocturnalTargetSelector(workspaceDir, stateDir, extractor, {
|
|
1322
|
-
idleCheckOverride: options.idleCheckOverride,
|
|
1323
|
-
recentPainContext: options.painContext,
|
|
1324
|
-
});
|
|
1325
|
-
|
|
1326
|
-
const selection = selector.select();
|
|
1327
|
-
diagnostics.selection = selection;
|
|
1328
|
-
|
|
1329
|
-
if (selection.decision === 'skip') {
|
|
1330
|
-
return {
|
|
1331
|
-
success: false,
|
|
1332
|
-
noTargetSelected: true,
|
|
1333
|
-
skipReason: selection.skipReason,
|
|
1334
|
-
validationFailed: false,
|
|
1335
|
-
validationFailures: [],
|
|
1336
|
-
diagnostics,
|
|
1337
|
-
};
|
|
1338
|
-
}
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
selectedPrincipleId = selection.selectedPrincipleId;
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
selectedSessionId = selection.selectedSessionId;
|
|
1346
|
-
|
|
1347
|
-
if (!selectedPrincipleId || !selectedSessionId) {
|
|
1348
|
-
return {
|
|
1349
|
-
success: false,
|
|
1350
|
-
noTargetSelected: true,
|
|
1351
|
-
validationFailed: false,
|
|
1352
|
-
validationFailures: [],
|
|
1353
|
-
diagnostics,
|
|
1354
|
-
};
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1357
|
-
snapshot = extractor.getNocturnalSessionSnapshot(selectedSessionId);
|
|
1358
|
-
if (!snapshot) {
|
|
1359
|
-
return {
|
|
1360
|
-
success: false,
|
|
1361
|
-
noTargetSelected: true,
|
|
1362
|
-
skipReason: 'insufficient_snapshot_data',
|
|
1363
|
-
validationFailed: false,
|
|
1364
|
-
validationFailures: [],
|
|
1365
|
-
diagnostics,
|
|
1366
|
-
};
|
|
1367
|
-
}
|
|
1368
|
-
diagnostics.idle = { isIdle: true, mostRecentActivityAt: 0, idleForMs: 0, userActiveSessions: 0, abandonedSessionIds: [], trajectoryGuardrailConfirmsIdle: true, reason: 'preflight passed' };
|
|
1369
|
-
}
|
|
1370
|
-
|
|
1371
|
-
// Step 3: Record run start
|
|
1372
|
-
const config = loadNocturnalConfig(stateDir);
|
|
1373
|
-
void recordRunStart(stateDir, selectedPrincipleId, config.cooldown_ms).catch((err) => {
|
|
1374
|
-
warn(`[nocturnal-service] Failed to record run start: ${String(err)}`);
|
|
1375
|
-
});
|
|
1376
|
-
|
|
1377
|
-
// Step 4: Trinity execution via adapter (async)
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
let trinityArtifact: TrinityDraftArtifact | null = null;
|
|
1381
|
-
let trinityResult: TrinityResult | null = null;
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
let rawJson: string;
|
|
1385
|
-
|
|
1386
|
-
if (options.skipReflector) {
|
|
1387
|
-
if (!options.reflectorOutputOverride) {
|
|
1388
|
-
return {
|
|
1389
|
-
success: false,
|
|
1390
|
-
noTargetSelected: false,
|
|
1391
|
-
validationFailed: true,
|
|
1392
|
-
validationFailures: ['skipReflector is true but no reflectorOutputOverride provided'],
|
|
1393
|
-
diagnostics,
|
|
1394
|
-
};
|
|
1395
|
-
}
|
|
1396
|
-
rawJson = options.reflectorOutputOverride;
|
|
1397
|
-
} else if (options.trinityResultOverride) {
|
|
1398
|
-
trinityResult = options.trinityResultOverride;
|
|
1399
|
-
diagnostics.trinityAttempted = true;
|
|
1400
|
-
diagnostics.trinityResult = trinityResult;
|
|
1401
|
-
diagnostics.chainModeUsed = trinityResult.success ? 'trinity' : 'single-reflector';
|
|
1402
|
-
|
|
1403
|
-
if (!trinityResult.success) {
|
|
1404
|
-
const failures = trinityResult.failures.map((f) => `${f.stage}: ${f.reason}`);
|
|
1405
|
-
void recordRunEnd(stateDir, 'failed', { reason: `Trinity override failed: ${failures.join('; ')}` }).catch((err) => {
|
|
1406
|
-
warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
1407
|
-
});
|
|
1408
|
-
adjustThresholdsFromSignals(stateDir, { malformedRate: 1.0, arbiterRejectRate: 0.0, executabilityRejectRate: 0.0, qualityDelta: 0.0 });
|
|
1409
|
-
return { success: false, noTargetSelected: false, validationFailed: true, validationFailures: [`Trinity override failed: ${failures.join('; ')}`], snapshot, diagnostics };
|
|
1410
|
-
}
|
|
1411
|
-
trinityArtifact = trinityResult.artifact!;
|
|
1412
|
-
const artifactData = draftToArtifact(trinityArtifact);
|
|
1413
|
-
rawJson = JSON.stringify(artifactData);
|
|
1414
|
-
} else {
|
|
1415
|
-
const trinityConfig: TrinityConfig = {
|
|
1416
|
-
...DEFAULT_TRINITY_CONFIG,
|
|
1417
|
-
...options.trinityConfig,
|
|
1418
|
-
runtimeAdapter: options.runtimeAdapter,
|
|
1419
|
-
stateDir,
|
|
1420
|
-
};
|
|
1421
|
-
|
|
1422
|
-
if (trinityConfig.useTrinity) {
|
|
1423
|
-
diagnostics.trinityAttempted = true;
|
|
1424
|
-
trinityResult = await runTrinityAsync({ snapshot, principleId: selectedPrincipleId, config: trinityConfig });
|
|
1425
|
-
diagnostics.trinityResult = trinityResult;
|
|
1426
|
-
diagnostics.chainModeUsed = trinityResult.success ? 'trinity' : 'single-reflector';
|
|
1427
|
-
|
|
1428
|
-
if (trinityResult.success) {
|
|
1429
|
-
const draftValidation = validateTrinityDraft(trinityResult.artifact);
|
|
1430
|
-
if (!draftValidation.valid) {
|
|
1431
|
-
const {failures} = draftValidation;
|
|
1432
|
-
void recordRunEnd(stateDir, 'failed', { reason: `Trinity draft invalid: ${failures.join('; ')}` }).catch((err) => {
|
|
1433
|
-
warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
1434
|
-
});
|
|
1435
|
-
adjustThresholdsFromSignals(stateDir, { malformedRate: 1.0, arbiterRejectRate: 0.0, executabilityRejectRate: 0.0, qualityDelta: 0.0 });
|
|
1436
|
-
return { success: false, noTargetSelected: false, validationFailed: true, validationFailures: failures, snapshot, diagnostics };
|
|
1437
|
-
}
|
|
1438
|
-
trinityArtifact = trinityResult.artifact!;
|
|
1439
|
-
const artifactData = draftToArtifact(trinityArtifact);
|
|
1440
|
-
rawJson = JSON.stringify(artifactData);
|
|
1441
|
-
} else {
|
|
1442
|
-
const failures = trinityResult.failures.map((f) => `${f.stage}: ${f.reason}`);
|
|
1443
|
-
void recordRunEnd(stateDir, 'failed', { reason: `Trinity chain failed: ${failures.join('; ')}` }).catch((err) => {
|
|
1444
|
-
warn(`[nocturnal-service] Failed to record run end: ${String(err)}`);
|
|
1445
|
-
});
|
|
1446
|
-
adjustThresholdsFromSignals(stateDir, { malformedRate: 1.0, arbiterRejectRate: 0.0, executabilityRejectRate: 0.0, qualityDelta: 0.0 });
|
|
1447
|
-
return { success: false, noTargetSelected: false, validationFailed: true, validationFailures: [`Trinity chain failed: ${failures.join('; ')}`], snapshot, diagnostics };
|
|
1448
|
-
}
|
|
1449
|
-
} else {
|
|
1450
|
-
rawJson = invokeStubReflector(snapshot, selectedPrincipleId);
|
|
1451
|
-
}
|
|
1452
|
-
}
|
|
1453
|
-
|
|
1454
|
-
// Step 5: Arbiter validation
|
|
1455
|
-
// #256: Use 0 for thinkingModelDeltaMin — Trinity chain already ensures quality
|
|
1456
|
-
const arbiterResult = parseAndValidateArtifact(rawJson, {
|
|
1457
|
-
expectedPrincipleId: selectedPrincipleId,
|
|
1458
|
-
expectedSessionId: selectedSessionId,
|
|
1459
|
-
qualityThresholds: {
|
|
1460
|
-
thinkingModelDeltaMin: 0,
|
|
1461
|
-
planningRatioGainMin: -0.5,
|
|
1462
|
-
},
|
|
1463
|
-
});
|
|
1464
|
-
diagnostics.arbiterResult = arbiterResult;
|
|
1465
|
-
|
|
1466
|
-
if (!arbiterResult.passed || !arbiterResult.artifact) {
|
|
1467
|
-
const failures = arbiterResult.failures.map((f) => f.reason);
|
|
1468
|
-
void recordRunEnd(stateDir, 'failed', { reason: failures.join('; ') }).catch((err) => {
|
|
1469
|
-
warn(`[nocturnal-service] Failed to record run end (arbiter failed): ${String(err)}`);
|
|
1470
|
-
});
|
|
1471
|
-
adjustThresholdsFromSignals(stateDir, { malformedRate: 0.0, arbiterRejectRate: 1.0, executabilityRejectRate: 0.0, qualityDelta: 0.0 });
|
|
1472
|
-
return { success: false, noTargetSelected: false, validationFailed: true, validationFailures: failures, diagnostics };
|
|
1473
|
-
}
|
|
1474
|
-
|
|
1475
|
-
// Step 6: Executability check
|
|
1476
|
-
const execResult = validateExecutability(arbiterResult.artifact);
|
|
1477
|
-
if (!execResult.executable) {
|
|
1478
|
-
const failures = execResult.failures.map((f) => f.reason);
|
|
1479
|
-
void recordRunEnd(stateDir, 'failed', { reason: failures.join('; ') }).catch((err) => {
|
|
1480
|
-
warn(`[nocturnal-service] Failed to record run end (executability failed): ${String(err)}`);
|
|
1481
|
-
});
|
|
1482
|
-
adjustThresholdsFromSignals(stateDir, { malformedRate: 0.0, arbiterRejectRate: 0.0, executabilityRejectRate: 1.0, qualityDelta: 0.0 });
|
|
1483
|
-
return { success: false, noTargetSelected: false, validationFailed: true, validationFailures: failures, diagnostics };
|
|
1484
|
-
}
|
|
1485
|
-
diagnostics.executabilityResult = { executable: true, failures: [] };
|
|
1486
|
-
|
|
1487
|
-
// Step 7: Persist artifact
|
|
1488
|
-
const artifactWithBoundedAction = { ...arbiterResult.artifact, boundedAction: execResult.boundedAction };
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
let persistedPath: string;
|
|
1492
|
-
try {
|
|
1493
|
-
persistedPath = persistArtifact(workspaceDir, artifactWithBoundedAction);
|
|
1494
|
-
diagnostics.persisted = true;
|
|
1495
|
-
diagnostics.persistedPath = persistedPath;
|
|
1496
|
-
} catch (err) {
|
|
1497
|
-
void recordRunEnd(stateDir, 'failed', { reason: `persistence error: ${String(err)}` }).catch((e) => {
|
|
1498
|
-
warn(`[nocturnal-service] Failed to record run end (persistence failed): ${String(e)}`);
|
|
1499
|
-
});
|
|
1500
|
-
return { success: false, noTargetSelected: false, validationFailed: true, validationFailures: [`Failed to persist artifact: ${String(err)}`], snapshot, diagnostics };
|
|
1501
|
-
}
|
|
1502
|
-
|
|
1503
|
-
// Step 8: Register in dataset lineage
|
|
1504
|
-
try {
|
|
1505
|
-
const regResult = registerSample(workspaceDir, arbiterResult.artifact, persistedPath, null);
|
|
1506
|
-
if (regResult.isNew) {
|
|
1507
|
-
incrementGeneratedSampleCount(stateDir, arbiterResult.artifact.principleId);
|
|
1508
|
-
}
|
|
1509
|
-
} catch (err) {
|
|
1510
|
-
warn(`[nocturnal-service] Failed to register sample in dataset registry: ${String(err)}`);
|
|
1511
|
-
}
|
|
1512
|
-
|
|
1513
|
-
try {
|
|
1514
|
-
appendArtifactLineageRecord(workspaceDir, {
|
|
1515
|
-
artifactKind: 'behavioral-sample',
|
|
1516
|
-
artifactId: arbiterResult.artifact.artifactId,
|
|
1517
|
-
principleId: selectedPrincipleId,
|
|
1518
|
-
ruleId: null,
|
|
1519
|
-
sessionId: selectedSessionId,
|
|
1520
|
-
sourceSnapshotRef: arbiterResult.artifact.sourceSnapshotRef,
|
|
1521
|
-
sourcePainIds: buildPainRefs(snapshot),
|
|
1522
|
-
sourceGateBlockIds: buildGateBlockRefs(snapshot),
|
|
1523
|
-
storagePath: persistedPath,
|
|
1524
|
-
implementationId: null,
|
|
1525
|
-
createdAt: arbiterResult.artifact.createdAt,
|
|
1526
|
-
});
|
|
1527
|
-
} catch (err) {
|
|
1528
|
-
warn(`[nocturnal-service] Failed to append behavioral artifact lineage: ${String(err)}`);
|
|
1529
|
-
}
|
|
1530
|
-
|
|
1531
|
-
diagnostics.artificer = maybePersistArtificerCandidate(
|
|
1532
|
-
workspaceDir,
|
|
1533
|
-
stateDir,
|
|
1534
|
-
selectedPrincipleId,
|
|
1535
|
-
selectedSessionId,
|
|
1536
|
-
snapshot,
|
|
1537
|
-
arbiterResult.artifact,
|
|
1538
|
-
options
|
|
1539
|
-
);
|
|
1540
|
-
|
|
1541
|
-
// Step 9: Record run success
|
|
1542
|
-
void recordRunEnd(stateDir, 'success', { sampleCount: 1 }).catch((err) => {
|
|
1543
|
-
warn(`[nocturnal-service] Failed to record run end (success): ${String(err)}`);
|
|
1544
|
-
});
|
|
1545
|
-
|
|
1546
|
-
// Step 10: Adaptive threshold adjustment
|
|
1547
|
-
const malformedRate = trinityResult && !trinityResult.success ? 1.0 : 0.0;
|
|
1548
|
-
const arbiterRejectRate = !arbiterResult.passed ? 1.0 : 0.0;
|
|
1549
|
-
const executabilityRejectRate = !execResult.executable ? 1.0 : 0.0;
|
|
1550
|
-
const qualityDelta = 0.0;
|
|
1551
|
-
adjustThresholdsFromSignals(stateDir, { malformedRate, arbiterRejectRate, executabilityRejectRate, qualityDelta });
|
|
1552
|
-
|
|
1553
|
-
return {
|
|
1554
|
-
success: true,
|
|
1555
|
-
artifact: artifactWithBoundedAction,
|
|
1556
|
-
noTargetSelected: false,
|
|
1557
|
-
validationFailed: false,
|
|
1558
|
-
validationFailures: [],
|
|
1559
|
-
snapshot,
|
|
1560
|
-
diagnostics,
|
|
1561
|
-
trinityTelemetry: trinityResult?.telemetry,
|
|
1562
|
-
};
|
|
1563
|
-
}
|
|
1564
|
-
|
|
1565
|
-
// ---------------------------------------------------------------------------
|
|
1566
|
-
// Query: List approved artifacts
|
|
1567
|
-
// ---------------------------------------------------------------------------
|
|
1568
|
-
|
|
1569
|
-
/**
|
|
1570
|
-
* List all approved nocturnal artifacts for a workspace.
|
|
1571
|
-
* Returns artifacts sorted by createdAt (newest first).
|
|
1572
|
-
*/
|
|
1573
|
-
export function listApprovedNocturnalArtifacts(
|
|
1574
|
-
workspaceDir: string
|
|
1575
|
-
): (NocturnalArtifact & { persistedAt: string; boundedAction?: BoundedAction })[] {
|
|
1576
|
-
const samplePaths = NocturnalPathResolver.listApprovedSamples(workspaceDir);
|
|
1577
|
-
const artifacts: (NocturnalArtifact & { persistedAt: string; boundedAction?: BoundedAction })[] = [];
|
|
1578
|
-
|
|
1579
|
-
for (const samplePath of samplePaths) {
|
|
1580
|
-
try {
|
|
1581
|
-
const content = fs.readFileSync(samplePath, 'utf-8');
|
|
1582
|
-
const sample = JSON.parse(content);
|
|
1583
|
-
if (sample.status === 'approved' && sample.artifactId) {
|
|
1584
|
-
artifacts.push({
|
|
1585
|
-
artifactId: sample.artifactId,
|
|
1586
|
-
sessionId: sample.sessionId,
|
|
1587
|
-
principleId: sample.principleId,
|
|
1588
|
-
sourceSnapshotRef: sample.sourceSnapshotRef || '',
|
|
1589
|
-
badDecision: sample.badDecision,
|
|
1590
|
-
betterDecision: sample.betterDecision,
|
|
1591
|
-
rationale: sample.rationale,
|
|
1592
|
-
createdAt: sample.createdAt,
|
|
1593
|
-
persistedAt: sample.persistedAt || new Date().toISOString(),
|
|
1594
|
-
boundedAction: sample.boundedAction,
|
|
1595
|
-
});
|
|
1596
|
-
}
|
|
1597
|
-
} catch {
|
|
1598
|
-
// Skip malformed files
|
|
1599
|
-
}
|
|
1600
|
-
}
|
|
1601
|
-
|
|
1602
|
-
// Sort by createdAt descending
|
|
1603
|
-
artifacts.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
|
|
1604
|
-
return artifacts;
|
|
1605
|
-
}
|