principles-disciple 1.11.0 → 1.13.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/CHANGELOG.md +26 -0
- package/README.md +66 -0
- package/esbuild.config.js +1 -1
- package/openclaw.plugin.json +4 -4
- package/package.json +2 -3
- package/run-nocturnal.mjs +30 -0
- package/scripts/db-migrate.mjs +170 -0
- package/scripts/sync-plugin.mjs +250 -6
- package/src/commands/archive-impl.ts +136 -0
- package/src/commands/capabilities.ts +4 -2
- package/src/commands/context.ts +5 -1
- package/src/commands/disable-impl.ts +151 -0
- package/src/commands/evolution-status.ts +64 -19
- package/src/commands/export.ts +8 -6
- package/src/commands/focus.ts +8 -20
- package/src/commands/nocturnal-review.ts +5 -7
- package/src/commands/nocturnal-rollout.ts +1 -12
- package/src/commands/nocturnal-train.ts +21 -47
- package/src/commands/pain.ts +10 -5
- package/src/commands/principle-rollback.ts +4 -2
- package/src/commands/promote-impl.ts +274 -0
- package/src/commands/rollback-impl.ts +234 -0
- package/src/commands/rollback.ts +6 -3
- package/src/commands/samples.ts +2 -0
- package/src/commands/thinking-os.ts +3 -4
- package/src/commands/workflow-debug.ts +2 -1
- package/src/config/errors.ts +1 -0
- package/src/core/AGENTS.md +34 -0
- package/src/core/adaptive-thresholds.ts +4 -3
- package/src/core/code-implementation-storage.ts +241 -0
- package/src/core/config.ts +5 -2
- package/src/core/control-ui-db.ts +29 -10
- package/src/core/detection-funnel.ts +12 -7
- package/src/core/diagnostician-task-store.ts +156 -0
- package/src/core/dictionary.ts +4 -4
- package/src/core/empathy-keyword-matcher.ts +7 -3
- package/src/core/empathy-types.ts +13 -2
- package/src/core/event-log.ts +14 -6
- package/src/core/evolution-engine.ts +27 -31
- package/src/core/evolution-logger.ts +3 -2
- package/src/core/evolution-reducer.ts +110 -31
- package/src/core/evolution-types.ts +10 -0
- package/src/core/external-training-contract.ts +1 -0
- package/src/core/focus-history.ts +38 -24
- package/src/core/hygiene/tracker.ts +10 -6
- package/src/core/init.ts +5 -2
- package/src/core/migration.ts +3 -3
- package/src/core/model-deployment-registry.ts +6 -4
- package/src/core/model-training-registry.ts +5 -3
- package/src/core/nocturnal-arbiter.ts +13 -14
- package/src/core/nocturnal-artifact-lineage.ts +117 -0
- package/src/core/nocturnal-artificer.ts +257 -0
- package/src/core/nocturnal-candidate-scoring.ts +4 -2
- package/src/core/nocturnal-compliance.ts +67 -19
- package/src/core/nocturnal-dataset.ts +95 -2
- package/src/core/nocturnal-executability.ts +2 -3
- package/src/core/nocturnal-export.ts +6 -3
- package/src/core/nocturnal-rule-implementation-validator.ts +245 -0
- package/src/core/nocturnal-trajectory-extractor.ts +10 -3
- package/src/core/nocturnal-trinity.ts +319 -61
- package/src/core/pain-context-extractor.ts +29 -15
- package/src/core/pain.ts +7 -5
- package/src/core/path-resolver.ts +16 -15
- package/src/core/paths.ts +2 -1
- package/src/core/pd-task-reconciler.ts +463 -0
- package/src/core/pd-task-service.ts +42 -0
- package/src/core/pd-task-store.ts +77 -0
- package/src/core/pd-task-types.ts +128 -0
- package/src/core/principle-internalization/deprecated-readiness.ts +91 -0
- package/src/core/principle-internalization/internalization-routing-policy.ts +208 -0
- package/src/core/principle-internalization/lifecycle-metrics.ts +149 -0
- package/src/core/principle-internalization/lifecycle-read-model.ts +243 -0
- package/src/core/principle-internalization/lifecycle-refresh.ts +11 -0
- package/src/core/principle-internalization/principle-lifecycle-service.ts +167 -0
- package/src/core/principle-training-state.ts +95 -370
- package/src/core/principle-tree-ledger.ts +733 -0
- package/src/core/principle-tree-migration.ts +195 -0
- package/src/core/profile.ts +3 -1
- package/src/core/promotion-gate.ts +14 -18
- package/src/core/replay-engine.ts +562 -0
- package/src/core/risk-calculator.ts +6 -4
- package/src/core/rule-host-helpers.ts +39 -0
- package/src/core/rule-host-types.ts +82 -0
- package/src/core/rule-host.ts +245 -0
- package/src/core/rule-implementation-runtime.ts +38 -0
- package/src/core/schema/db-types.ts +16 -0
- package/src/core/schema/index.ts +26 -0
- package/src/core/schema/migration-runner.ts +207 -0
- package/src/core/schema/migrations/001-init-trajectory.ts +211 -0
- package/src/core/schema/migrations/002-init-central.ts +122 -0
- package/src/core/schema/migrations/003-init-workflow.ts +55 -0
- package/src/core/schema/migrations/004-add-thinking-and-gfi.ts +74 -0
- package/src/core/schema/migrations/index.ts +31 -0
- package/src/core/schema/schema-definitions.ts +650 -0
- package/src/core/session-tracker.ts +6 -4
- package/src/core/shadow-observation-registry.ts +6 -3
- package/src/core/system-logger.ts +2 -2
- package/src/core/thinking-models.ts +182 -46
- package/src/core/thinking-os-parser.ts +156 -0
- package/src/core/training-program.ts +7 -7
- package/src/core/trajectory.ts +42 -36
- package/src/core/workspace-context.ts +77 -11
- package/src/core/workspace-dir-validation.ts +152 -0
- package/src/hooks/AGENTS.md +31 -0
- package/src/hooks/bash-risk.ts +3 -1
- package/src/hooks/edit-verification.ts +9 -5
- package/src/hooks/gate-block-helper.ts +5 -1
- package/src/hooks/gate.ts +152 -5
- package/src/hooks/gfi-gate.ts +9 -2
- package/src/hooks/lifecycle-routing.ts +124 -0
- package/src/hooks/lifecycle.ts +12 -12
- package/src/hooks/llm.ts +17 -109
- package/src/hooks/message-sanitize.ts +5 -3
- package/src/hooks/pain.ts +19 -15
- package/src/hooks/progressive-trust-gate.ts +7 -1
- package/src/hooks/prompt.ts +169 -60
- package/src/hooks/subagent.ts +5 -4
- package/src/hooks/thinking-checkpoint.ts +2 -0
- package/src/hooks/trajectory-collector.ts +15 -12
- package/src/http/principles-console-route.ts +31 -68
- package/src/i18n/commands.ts +2 -2
- package/src/index.ts +130 -40
- package/src/service/central-database.ts +131 -43
- package/src/service/central-health-service.ts +47 -0
- package/src/service/central-overview-service.ts +135 -0
- package/src/service/central-sync-service.ts +87 -0
- package/src/service/control-ui-query-service.ts +46 -36
- package/src/service/event-log-auditor.ts +261 -0
- package/src/service/evolution-query-service.ts +23 -22
- package/src/service/evolution-worker.ts +565 -261
- package/src/service/health-query-service.ts +213 -36
- package/src/service/nocturnal-runtime.ts +8 -4
- package/src/service/nocturnal-service.ts +503 -59
- package/src/service/nocturnal-target-selector.ts +5 -7
- package/src/service/runtime-summary-service.ts +2 -1
- package/src/service/subagent-workflow/deep-reflect-workflow-manager.ts +25 -336
- package/src/service/subagent-workflow/dynamic-timeout.ts +30 -0
- package/src/service/subagent-workflow/empathy-observer-workflow-manager.ts +48 -386
- package/src/service/subagent-workflow/index.ts +2 -0
- package/src/service/subagent-workflow/nocturnal-workflow-manager.ts +169 -284
- package/src/service/subagent-workflow/runtime-direct-driver.ts +114 -16
- package/src/service/subagent-workflow/subagent-error-utils.ts +25 -0
- package/src/service/subagent-workflow/types.ts +9 -4
- package/src/service/subagent-workflow/workflow-manager-base.ts +573 -0
- package/src/service/subagent-workflow/workflow-store.ts +71 -11
- package/src/service/trajectory-service.ts +2 -1
- package/src/tools/critique-prompt.ts +1 -1
- package/src/tools/deep-reflect.ts +175 -209
- package/src/tools/model-index.ts +2 -1
- package/src/types/event-types.ts +2 -2
- package/src/types/principle-tree-schema.ts +29 -23
- package/src/utils/file-lock.ts +5 -3
- package/src/utils/io.ts +5 -2
- package/src/utils/nlp.ts +5 -46
- package/src/utils/node-vm-polyfill.ts +11 -0
- package/src/utils/plugin-logger.ts +2 -0
- package/src/utils/retry.ts +572 -0
- package/src/utils/subagent-probe.ts +1 -1
- package/templates/langs/en/core/AGENTS.md +0 -13
- package/templates/langs/en/core/SOUL.md +1 -31
- package/templates/langs/en/core/TOOLS.md +0 -4
- package/templates/langs/en/principles/THINKING_OS.md +77 -0
- package/templates/langs/en/skills/admin/SKILL.md +0 -1
- package/templates/langs/en/skills/evolution-framework-update/SKILL.md +1 -1
- package/templates/langs/en/skills/pd-diagnostician/SKILL.md +18 -5
- package/templates/langs/zh/core/AGENTS.md +0 -22
- package/templates/langs/zh/core/SOUL.md +1 -31
- package/templates/langs/zh/core/TOOLS.md +0 -4
- package/templates/langs/zh/principles/THINKING_OS.md +77 -0
- package/templates/langs/zh/skills/admin/SKILL.md +0 -1
- package/templates/langs/zh/skills/evolution-framework-update/SKILL.md +1 -1
- package/templates/langs/zh/skills/pd-diagnostician/SKILL.md +25 -4
- package/tests/commands/evolution-status.test.ts +119 -0
- package/tests/commands/implementation-lifecycle.test.ts +362 -0
- package/tests/core/code-implementation-storage.test.ts +398 -0
- package/tests/core/evolution-reducer.detector-metadata.test.ts +28 -28
- package/tests/core/nocturnal-artifact-lineage.test.ts +53 -0
- package/tests/core/nocturnal-artificer.test.ts +241 -0
- package/tests/core/nocturnal-compliance-p-principles.test.ts +133 -0
- package/tests/core/nocturnal-rule-implementation-validator.test.ts +127 -0
- package/tests/core/pd-task-store.test.ts +126 -0
- package/tests/core/principle-internalization/deprecated-readiness.test.ts +193 -0
- package/tests/core/principle-internalization/internalization-routing-policy.test.ts +212 -0
- package/tests/core/principle-internalization/lifecycle-metrics.test.ts +350 -0
- package/tests/core/principle-internalization/principle-lifecycle-service.test.ts +211 -0
- package/tests/core/principle-training-state.test.ts +228 -1
- package/tests/core/principle-tree-ledger.test.ts +423 -0
- package/tests/core/regression-v1-9-1.test.ts +265 -0
- package/tests/core/replay-engine.test.ts +234 -0
- package/tests/core/rule-host-helpers.test.ts +120 -0
- package/tests/core/rule-host.test.ts +389 -0
- package/tests/core/rule-implementation-runtime.test.ts +64 -0
- package/tests/core/workspace-context.test.ts +53 -0
- package/tests/core/workspace-dir-validation.test.ts +272 -0
- package/tests/hooks/gate-rule-host-pipeline.test.ts +385 -0
- package/tests/hooks/pain.test.ts +74 -10
- package/tests/hooks/prompt.test.ts +63 -1
- package/tests/integration/principle-lifecycle.e2e.test.ts +197 -0
- package/tests/integration/tool-hooks-workspace-dir.e2e.test.ts +211 -0
- package/tests/service/data-endpoints-regression.test.ts +834 -0
- package/tests/service/evolution-worker.test.ts +0 -123
- package/tests/service/nocturnal-service-code-candidate.test.ts +330 -0
- package/tests/utils/nlp.test.ts +1 -19
- package/tests/utils/retry.test.ts +327 -0
- package/ui/src/App.tsx +1 -1
- package/ui/src/api.ts +4 -0
- package/ui/src/charts.tsx +366 -0
- package/ui/src/components/WorkspaceConfig.tsx +107 -75
- package/ui/src/i18n/ui.ts +89 -31
- package/ui/src/pages/EvolutionPage.tsx +1 -1
- package/ui/src/pages/OverviewPage.tsx +441 -81
- package/ui/src/pages/ThinkingModelsPage.tsx +287 -69
- package/ui/src/styles.css +43 -0
- package/ui/src/types.ts +17 -1
- package/src/agents/nocturnal-dreamer.md +0 -152
- package/src/agents/nocturnal-philosopher.md +0 -138
- package/src/agents/nocturnal-reflector.md +0 -126
- package/src/agents/nocturnal-scribe.md +0 -164
- package/templates/workspace/.principles/00-kernel.md +0 -51
- package/templates/workspace/.principles/DECISION_POLICY.json +0 -44
- package/templates/workspace/.principles/PRINCIPLES.md +0 -20
- package/templates/workspace/.principles/PROFILE.json +0 -54
- package/templates/workspace/.principles/PROFILE.schema.json +0 -56
- package/templates/workspace/.principles/THINKING_OS.md +0 -64
- package/templates/workspace/.principles/THINKING_OS_ARCHIVE.md +0 -7
- package/templates/workspace/.principles/THINKING_OS_CANDIDATES.md +0 -9
- package/templates/workspace/.principles/models/_INDEX.md +0 -27
- package/templates/workspace/.principles/models/first_principles.md +0 -62
- package/templates/workspace/.principles/models/marketing_4p.md +0 -52
- package/templates/workspace/.principles/models/porter_five.md +0 -63
- package/templates/workspace/.principles/models/swot.md +0 -60
- package/templates/workspace/.principles/models/user_story_map.md +0 -63
- package/templates/workspace/.state/WORKBOARD.json +0 -4
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as os from 'os';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import { WorkspaceContext } from '../../src/core/workspace-context.js';
|
|
6
|
+
import { createImplementationAssetDir, getImplementationAssetRoot } from '../../src/core/code-implementation-storage.js';
|
|
7
|
+
import {
|
|
8
|
+
loadLedger,
|
|
9
|
+
saveLedger,
|
|
10
|
+
type LedgerPrinciple,
|
|
11
|
+
type LedgerRule,
|
|
12
|
+
} from '../../src/core/principle-tree-ledger.js';
|
|
13
|
+
import { handlePromoteImplCommand } from '../../src/commands/promote-impl.js';
|
|
14
|
+
import { handleDisableImplCommand } from '../../src/commands/disable-impl.js';
|
|
15
|
+
import { handleArchiveImplCommand } from '../../src/commands/archive-impl.js';
|
|
16
|
+
import { handleRollbackImplCommand } from '../../src/commands/rollback-impl.js';
|
|
17
|
+
import type { PluginCommandContext } from '../../src/openclaw-sdk.js';
|
|
18
|
+
import type { Implementation } from '../../src/types/principle-tree-schema.js';
|
|
19
|
+
import { TrajectoryDatabase, TrajectoryRegistry } from '../../src/core/trajectory.js';
|
|
20
|
+
import { registerSample } from '../../src/core/nocturnal-dataset.js';
|
|
21
|
+
import { safeRmDir } from '../test-utils.js';
|
|
22
|
+
|
|
23
|
+
function makePrinciple(): LedgerPrinciple {
|
|
24
|
+
return {
|
|
25
|
+
id: 'P-1',
|
|
26
|
+
version: 1,
|
|
27
|
+
text: 'Prefer explicit principle-backed implementations',
|
|
28
|
+
triggerPattern: 'principle',
|
|
29
|
+
action: 'use implementations safely',
|
|
30
|
+
status: 'active',
|
|
31
|
+
priority: 'P1',
|
|
32
|
+
scope: 'general',
|
|
33
|
+
evaluability: 'deterministic',
|
|
34
|
+
valueScore: 0,
|
|
35
|
+
adherenceRate: 0,
|
|
36
|
+
painPreventedCount: 0,
|
|
37
|
+
derivedFromPainIds: [],
|
|
38
|
+
ruleIds: ['R-1'],
|
|
39
|
+
conflictsWithPrincipleIds: [],
|
|
40
|
+
createdAt: '2026-04-08T00:00:00.000Z',
|
|
41
|
+
updatedAt: '2026-04-08T00:00:00.000Z',
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function makeRule(implementationIds: string[]): LedgerRule {
|
|
46
|
+
return {
|
|
47
|
+
id: 'R-1',
|
|
48
|
+
version: 1,
|
|
49
|
+
name: 'Guard implementation lifecycle',
|
|
50
|
+
description: 'Tracks implementation state transitions',
|
|
51
|
+
type: 'gate',
|
|
52
|
+
triggerCondition: 'lifecycle',
|
|
53
|
+
enforcement: 'warn',
|
|
54
|
+
action: 'record state safely',
|
|
55
|
+
principleId: 'P-1',
|
|
56
|
+
status: 'implemented',
|
|
57
|
+
coverageRate: 0,
|
|
58
|
+
falsePositiveRate: 0,
|
|
59
|
+
implementationPath: undefined,
|
|
60
|
+
testPath: undefined,
|
|
61
|
+
createdAt: '2026-04-08T00:00:00.000Z',
|
|
62
|
+
updatedAt: '2026-04-08T00:00:00.000Z',
|
|
63
|
+
implementationIds,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function makeImplementation(
|
|
68
|
+
id: string,
|
|
69
|
+
lifecycleState: Implementation['lifecycleState'],
|
|
70
|
+
overrides: Partial<Implementation> = {}
|
|
71
|
+
): Implementation {
|
|
72
|
+
return {
|
|
73
|
+
id,
|
|
74
|
+
ruleId: 'R-1',
|
|
75
|
+
type: 'code',
|
|
76
|
+
path: path.join('virtual', `${id}.js`),
|
|
77
|
+
version: '1.0.0',
|
|
78
|
+
coversCondition: 'lifecycle',
|
|
79
|
+
coveragePercentage: 100,
|
|
80
|
+
lifecycleState,
|
|
81
|
+
createdAt: '2026-04-08T00:00:00.000Z',
|
|
82
|
+
updatedAt: '2026-04-08T00:00:00.000Z',
|
|
83
|
+
...overrides,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function makeCommandContext(workspaceDir: string, args: string): PluginCommandContext {
|
|
88
|
+
return {
|
|
89
|
+
channel: 'test',
|
|
90
|
+
isAuthorizedSender: true,
|
|
91
|
+
commandBody: args,
|
|
92
|
+
args,
|
|
93
|
+
config: { workspaceDir, language: 'en' },
|
|
94
|
+
workspaceDir,
|
|
95
|
+
sessionId: 'session-1',
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function writePassingReplayReport(stateDir: string, implementationId: string): void {
|
|
100
|
+
const assetRoot = getImplementationAssetRoot(stateDir, implementationId);
|
|
101
|
+
const reportPath = path.join(assetRoot, 'replays', '2026-04-08T00-00-00.000Z.json');
|
|
102
|
+
fs.writeFileSync(
|
|
103
|
+
reportPath,
|
|
104
|
+
JSON.stringify(
|
|
105
|
+
{
|
|
106
|
+
overallDecision: 'pass',
|
|
107
|
+
replayResults: {
|
|
108
|
+
painNegative: { total: 1, passed: 1, failed: 0, details: [] },
|
|
109
|
+
successPositive: { total: 1, passed: 1, failed: 0, details: [] },
|
|
110
|
+
principleAnchor: { total: 1, passed: 1, failed: 0, details: [] },
|
|
111
|
+
},
|
|
112
|
+
blockers: [],
|
|
113
|
+
generatedAt: '2026-04-08T00:00:00.000Z',
|
|
114
|
+
implementationId,
|
|
115
|
+
sampleFingerprints: ['fp-1'],
|
|
116
|
+
},
|
|
117
|
+
null,
|
|
118
|
+
2
|
|
119
|
+
),
|
|
120
|
+
'utf-8'
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
describe('implementation lifecycle commands', () => {
|
|
125
|
+
let tempDir: string;
|
|
126
|
+
let workspaceDir: string;
|
|
127
|
+
let stateDir: string;
|
|
128
|
+
let trajectory: TrajectoryDatabase;
|
|
129
|
+
|
|
130
|
+
beforeEach(() => {
|
|
131
|
+
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-impl-cmd-'));
|
|
132
|
+
workspaceDir = path.join(tempDir, 'workspace');
|
|
133
|
+
stateDir = path.join(tempDir, '.state');
|
|
134
|
+
fs.mkdirSync(workspaceDir, { recursive: true });
|
|
135
|
+
fs.mkdirSync(stateDir, { recursive: true });
|
|
136
|
+
fs.writeFileSync(
|
|
137
|
+
path.join(workspaceDir, 'PROFILE.json'),
|
|
138
|
+
JSON.stringify({ risk_paths: ['src/**'] }, null, 2),
|
|
139
|
+
'utf-8',
|
|
140
|
+
);
|
|
141
|
+
trajectory = new TrajectoryDatabase({ workspaceDir });
|
|
142
|
+
|
|
143
|
+
WorkspaceContext.clearCache();
|
|
144
|
+
const workspaceContext = WorkspaceContext.fromHookContext({ workspaceDir, stateDir });
|
|
145
|
+
vi.spyOn(WorkspaceContext, 'fromHookContext').mockReturnValue(workspaceContext);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
afterEach(() => {
|
|
149
|
+
try {
|
|
150
|
+
trajectory.dispose();
|
|
151
|
+
} catch {
|
|
152
|
+
// Best effort cleanup.
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
TrajectoryRegistry.dispose(workspaceDir);
|
|
156
|
+
} catch {
|
|
157
|
+
// Best effort cleanup.
|
|
158
|
+
}
|
|
159
|
+
vi.restoreAllMocks();
|
|
160
|
+
WorkspaceContext.clearCache();
|
|
161
|
+
safeRmDir(tempDir);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
function seedLedger(implementations: Implementation[]): void {
|
|
165
|
+
saveLedger(stateDir, {
|
|
166
|
+
trainingStore: {
|
|
167
|
+
'P-1': {
|
|
168
|
+
principleId: 'P-1',
|
|
169
|
+
evaluability: 'deterministic',
|
|
170
|
+
applicableOpportunityCount: 1,
|
|
171
|
+
observedViolationCount: 0,
|
|
172
|
+
complianceRate: 1,
|
|
173
|
+
violationTrend: 0,
|
|
174
|
+
generatedSampleCount: 0,
|
|
175
|
+
approvedSampleCount: 0,
|
|
176
|
+
includedTrainRunIds: [],
|
|
177
|
+
deployedCheckpointIds: [],
|
|
178
|
+
internalizationStatus: 'internalized',
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
tree: {
|
|
182
|
+
principles: {
|
|
183
|
+
'P-1': makePrinciple(),
|
|
184
|
+
},
|
|
185
|
+
rules: {
|
|
186
|
+
'R-1': makeRule(implementations.map((impl) => impl.id)),
|
|
187
|
+
},
|
|
188
|
+
implementations: Object.fromEntries(implementations.map((impl) => [impl.id, impl])),
|
|
189
|
+
metrics: {},
|
|
190
|
+
lastUpdated: '2026-04-08T00:00:00.000Z',
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function seedReplaySample(sessionId: string, artifactId: string): void {
|
|
196
|
+
trajectory.recordSession({
|
|
197
|
+
sessionId,
|
|
198
|
+
startedAt: '2026-04-08T00:00:00.000Z',
|
|
199
|
+
});
|
|
200
|
+
trajectory.recordToolCall({
|
|
201
|
+
sessionId,
|
|
202
|
+
toolName: 'write',
|
|
203
|
+
outcome: 'blocked',
|
|
204
|
+
errorMessage: 'risky write requires approval',
|
|
205
|
+
paramsJson: { filePath: 'src/app.ts' },
|
|
206
|
+
createdAt: '2026-04-08T00:01:00.000Z',
|
|
207
|
+
});
|
|
208
|
+
trajectory.recordPainEvent({
|
|
209
|
+
sessionId,
|
|
210
|
+
source: 'gate',
|
|
211
|
+
score: 75,
|
|
212
|
+
reason: 'risky write requires approval',
|
|
213
|
+
createdAt: '2026-04-08T00:01:01.000Z',
|
|
214
|
+
});
|
|
215
|
+
trajectory.recordGateBlock({
|
|
216
|
+
sessionId,
|
|
217
|
+
toolName: 'write',
|
|
218
|
+
filePath: 'src/app.ts',
|
|
219
|
+
reason: 'risky write requires approval',
|
|
220
|
+
planStatus: 'NONE',
|
|
221
|
+
createdAt: '2026-04-08T00:01:02.000Z',
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const artifactPath = path.join(stateDir, 'nocturnal', 'samples', `${artifactId}.json`);
|
|
225
|
+
fs.mkdirSync(path.dirname(artifactPath), { recursive: true });
|
|
226
|
+
fs.writeFileSync(
|
|
227
|
+
artifactPath,
|
|
228
|
+
JSON.stringify(
|
|
229
|
+
{
|
|
230
|
+
artifactId,
|
|
231
|
+
sessionId,
|
|
232
|
+
principleId: 'P-1',
|
|
233
|
+
sourceSnapshotRef: `snapshot-${sessionId}`,
|
|
234
|
+
badDecision: 'Retried the write without checking approval requirements',
|
|
235
|
+
betterDecision: 'Check the approval requirements before writing',
|
|
236
|
+
rationale: 'Riskiest paths should not be written without a plan',
|
|
237
|
+
createdAt: '2026-04-08T00:02:00.000Z',
|
|
238
|
+
},
|
|
239
|
+
null,
|
|
240
|
+
2,
|
|
241
|
+
),
|
|
242
|
+
'utf-8',
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
registerSample(
|
|
246
|
+
workspaceDir,
|
|
247
|
+
{
|
|
248
|
+
artifactId,
|
|
249
|
+
sessionId,
|
|
250
|
+
principleId: 'P-1',
|
|
251
|
+
sourceSnapshotRef: `snapshot-${sessionId}`,
|
|
252
|
+
badDecision: 'Retried the write without checking approval requirements',
|
|
253
|
+
betterDecision: 'Check the approval requirements before writing',
|
|
254
|
+
rationale: 'Riskiest paths should not be written without a plan',
|
|
255
|
+
createdAt: '2026-04-08T00:02:00.000Z',
|
|
256
|
+
},
|
|
257
|
+
artifactPath,
|
|
258
|
+
null,
|
|
259
|
+
'pain-negative',
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
it('promote preserves the legacy training store while activating the candidate', () => {
|
|
264
|
+
seedLedger([makeImplementation('IMPL-CAND', 'candidate')]);
|
|
265
|
+
createImplementationAssetDir(stateDir, 'IMPL-CAND', '1.0.0');
|
|
266
|
+
writePassingReplayReport(stateDir, 'IMPL-CAND');
|
|
267
|
+
|
|
268
|
+
const result = handlePromoteImplCommand(makeCommandContext(workspaceDir, 'IMPL-CAND'));
|
|
269
|
+
|
|
270
|
+
expect(result.text).toContain('Implementation promoted');
|
|
271
|
+
expect(loadLedger(stateDir).tree.implementations['IMPL-CAND'].lifecycleState).toBe('active');
|
|
272
|
+
|
|
273
|
+
const raw = JSON.parse(
|
|
274
|
+
fs.readFileSync(path.join(stateDir, 'principle_training_state.json'), 'utf-8')
|
|
275
|
+
) as Record<string, unknown>;
|
|
276
|
+
expect(raw['P-1']).toBeDefined();
|
|
277
|
+
expect(raw._tree).toBeDefined();
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('eval generates a replay report for a candidate implementation before promotion', () => {
|
|
281
|
+
seedLedger([makeImplementation('IMPL-CAND', 'candidate')]);
|
|
282
|
+
createImplementationAssetDir(stateDir, 'IMPL-CAND', '1.0.0', {
|
|
283
|
+
entrySource: [
|
|
284
|
+
'export const meta = {',
|
|
285
|
+
' name: "risky-write-guard",',
|
|
286
|
+
' version: "1.0.0",',
|
|
287
|
+
' ruleId: "R-1",',
|
|
288
|
+
' coversCondition: "risky write",',
|
|
289
|
+
'};',
|
|
290
|
+
'export function evaluate(input, helpers) {',
|
|
291
|
+
" if (helpers.isRiskPath() && helpers.getToolName() === 'write' && helpers.getPlanStatus() !== 'READY') {",
|
|
292
|
+
" return { decision: 'requireApproval', matched: true, reason: 'plan required' };",
|
|
293
|
+
' }',
|
|
294
|
+
" return { decision: 'allow', matched: false, reason: 'not-applicable' };",
|
|
295
|
+
'}',
|
|
296
|
+
].join('\n'),
|
|
297
|
+
});
|
|
298
|
+
seedReplaySample('session-eval', 'artifact-eval');
|
|
299
|
+
|
|
300
|
+
const result = handlePromoteImplCommand(makeCommandContext(workspaceDir, 'eval IMPL-CAND'));
|
|
301
|
+
|
|
302
|
+
expect(result.text).toContain('Replay Evaluation Report');
|
|
303
|
+
expect(result.text).toContain('Overall Decision: [PASS]');
|
|
304
|
+
expect(
|
|
305
|
+
fs.readdirSync(path.join(getImplementationAssetRoot(stateDir, 'IMPL-CAND'), 'replays')).length,
|
|
306
|
+
).toBeGreaterThan(0);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it('disable preserves the legacy training store while recording disable metadata', () => {
|
|
310
|
+
seedLedger([makeImplementation('IMPL-ACTIVE', 'active')]);
|
|
311
|
+
|
|
312
|
+
const result = handleDisableImplCommand(
|
|
313
|
+
makeCommandContext(workspaceDir, 'IMPL-ACTIVE --reason "manual disable"')
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
expect(result.text).toContain('Implementation disabled');
|
|
317
|
+
const updated = loadLedger(stateDir).tree.implementations['IMPL-ACTIVE'];
|
|
318
|
+
expect(updated.lifecycleState).toBe('disabled');
|
|
319
|
+
expect(updated.disabledReason).toBe('manual disable');
|
|
320
|
+
|
|
321
|
+
const raw = JSON.parse(
|
|
322
|
+
fs.readFileSync(path.join(stateDir, 'principle_training_state.json'), 'utf-8')
|
|
323
|
+
) as Record<string, unknown>;
|
|
324
|
+
expect(raw['P-1']).toBeDefined();
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it('archive preserves the legacy training store while archiving a candidate', () => {
|
|
328
|
+
seedLedger([makeImplementation('IMPL-CAND', 'candidate')]);
|
|
329
|
+
|
|
330
|
+
const result = handleArchiveImplCommand(makeCommandContext(workspaceDir, 'IMPL-CAND'));
|
|
331
|
+
|
|
332
|
+
expect(result.text).toContain('Implementation archived');
|
|
333
|
+
expect(loadLedger(stateDir).tree.implementations['IMPL-CAND'].lifecycleState).toBe('archived');
|
|
334
|
+
|
|
335
|
+
const raw = JSON.parse(
|
|
336
|
+
fs.readFileSync(path.join(stateDir, 'principle_training_state.json'), 'utf-8')
|
|
337
|
+
) as Record<string, unknown>;
|
|
338
|
+
expect(raw['P-1']).toBeDefined();
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it('rollback preserves the legacy training store while restoring the previous active implementation', () => {
|
|
342
|
+
seedLedger([
|
|
343
|
+
makeImplementation('IMPL-CURRENT', 'active', { previousActive: 'IMPL-PREV' }),
|
|
344
|
+
makeImplementation('IMPL-PREV', 'disabled'),
|
|
345
|
+
]);
|
|
346
|
+
|
|
347
|
+
const result = handleRollbackImplCommand(
|
|
348
|
+
makeCommandContext(workspaceDir, 'IMPL-CURRENT --reason "rollback"')
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
expect(result.text).toContain('Rollback complete');
|
|
352
|
+
|
|
353
|
+
const ledger = loadLedger(stateDir);
|
|
354
|
+
expect(ledger.tree.implementations['IMPL-CURRENT'].lifecycleState).toBe('disabled');
|
|
355
|
+
expect(ledger.tree.implementations['IMPL-PREV'].lifecycleState).toBe('active');
|
|
356
|
+
|
|
357
|
+
const raw = JSON.parse(
|
|
358
|
+
fs.readFileSync(path.join(stateDir, 'principle_training_state.json'), 'utf-8')
|
|
359
|
+
) as Record<string, unknown>;
|
|
360
|
+
expect(raw['P-1']).toBeDefined();
|
|
361
|
+
});
|
|
362
|
+
});
|