principles-disciple 1.28.0 → 1.28.1
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 +1 -1
- package/package.json +4 -4
- package/scripts/validate-live-path.ts +18 -18
- package/src/commands/nocturnal-train.ts +4 -6
- package/src/commands/pain.ts +8 -11
- package/src/commands/pd-reflect.ts +1 -1
- package/src/core/bootstrap-rules.ts +3 -3
- package/src/core/merge-gate-audit.ts +1 -1
- package/src/core/nocturnal-candidate-scoring.ts +131 -0
- package/src/core/nocturnal-reasoning-deriver.ts +337 -0
- package/src/core/nocturnal-trinity.ts +454 -18
- package/src/core/pain-context-extractor.ts +1 -3
- package/src/core/principle-tree-migration.ts +2 -4
- package/src/core/thinking-os-parser.ts +3 -3
- package/src/hooks/bash-risk.ts +1 -1
- package/src/hooks/gfi-gate.ts +1 -1
- package/src/hooks/pain.ts +1 -1
- package/src/hooks/prompt.ts +36 -2
- package/src/hooks/subagent.ts +1 -1
- package/src/index.ts +1 -1
- package/src/service/evolution-worker.ts +1 -1
- package/src/service/health-query-service.ts +15 -6
- package/src/service/subagent-workflow/nocturnal-workflow-manager.ts +0 -1
- package/tests/core/nocturnal-candidate-scoring.test.ts +132 -0
- package/tests/core/nocturnal-reasoning-deriver.test.ts +372 -0
- package/tests/core/nocturnal-trinity.test.ts +791 -0
package/openclaw.plugin.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "principles-disciple",
|
|
3
|
-
"version": "1.28.
|
|
3
|
+
"version": "1.28.1",
|
|
4
4
|
"description": "Native OpenClaw plugin for Principles Disciple",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/bundle.js",
|
|
@@ -40,14 +40,14 @@
|
|
|
40
40
|
"@testing-library/react": "^16.3.0",
|
|
41
41
|
"@types/better-sqlite3": "^7.6.13",
|
|
42
42
|
"@types/micromatch": "^4.0.10",
|
|
43
|
-
"@types/node": "^25.
|
|
43
|
+
"@types/node": "^25.6.0",
|
|
44
44
|
"@types/react": "^19.2.2",
|
|
45
45
|
"@types/react-dom": "^19.2.2",
|
|
46
46
|
"@types/ws": "^8.5.13",
|
|
47
47
|
"@typescript-eslint/eslint-plugin": "^8.58.0",
|
|
48
48
|
"@typescript-eslint/parser": "^8.58.0",
|
|
49
49
|
"@vitest/coverage-v8": "^4.1.0",
|
|
50
|
-
"esbuild": "^0.
|
|
50
|
+
"esbuild": "^0.28.0",
|
|
51
51
|
"eslint": "^10.1.0",
|
|
52
52
|
"jsdom": "^29.0.1",
|
|
53
53
|
"typescript": "^6.0.2",
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
66
|
"@sinclair/typebox": "^0.34.48",
|
|
67
|
-
"better-sqlite3": "^12.
|
|
67
|
+
"better-sqlite3": "^12.9.0",
|
|
68
68
|
"lucide-react": "^1.7.0",
|
|
69
69
|
"micromatch": "^4.0.8",
|
|
70
70
|
"react": "^19.2.0",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
* WORKSPACE_DIR - Optional workspace directory (defaults to process.cwd())
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
|
+
import * as Database from 'better-sqlite3';
|
|
23
24
|
import * as fs from 'fs';
|
|
24
25
|
import * as path from 'path';
|
|
25
26
|
|
|
@@ -33,7 +34,6 @@ const LOCK_STALE_MS = 30_000;
|
|
|
33
34
|
const WORKSPACE_DIR = process.env.WORKSPACE_DIR || process.cwd();
|
|
34
35
|
const STATE_DIR = path.join(WORKSPACE_DIR, '.state');
|
|
35
36
|
const QUEUE_PATH = path.join(STATE_DIR, 'EVOLUTION_QUEUE');
|
|
36
|
-
const QUEUE_LOCK_PATH = QUEUE_PATH + LOCK_SUFFIX;
|
|
37
37
|
const LEDGER_PATH = path.join(STATE_DIR, 'principle_training_state.json');
|
|
38
38
|
const DB_PATH = path.join(STATE_DIR, 'subagent_workflows.db');
|
|
39
39
|
|
|
@@ -80,17 +80,14 @@ async function acquireLockAsync(filePath: string, options: {
|
|
|
80
80
|
baseRetryDelayMs?: number;
|
|
81
81
|
lockStaleMs?: number;
|
|
82
82
|
} = {}): Promise<LockContext> {
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
lockStaleMs: LOCK_STALE_MS,
|
|
88
|
-
...options,
|
|
89
|
-
};
|
|
83
|
+
const lockSuffix = options.lockSuffix ?? LOCK_SUFFIX;
|
|
84
|
+
const maxRetries = options.maxRetries ?? LOCK_MAX_RETRIES;
|
|
85
|
+
const baseRetryDelayMs = options.baseRetryDelayMs ?? LOCK_RETRY_DELAY_MS;
|
|
86
|
+
const lockStaleMs = options.lockStaleMs ?? LOCK_STALE_MS;
|
|
90
87
|
const { pid } = process;
|
|
91
|
-
const lockPath = filePath +
|
|
88
|
+
const lockPath = filePath + lockSuffix;
|
|
92
89
|
|
|
93
|
-
for (let attempt = 0; attempt <
|
|
90
|
+
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
94
91
|
try {
|
|
95
92
|
// Check if lock file exists and is stale
|
|
96
93
|
if (fs.existsSync(lockPath)) {
|
|
@@ -100,11 +97,11 @@ async function acquireLockAsync(filePath: string, options: {
|
|
|
100
97
|
const lockAge = Date.now() - lockStats.mtimeMs;
|
|
101
98
|
|
|
102
99
|
// Clean up stale lock
|
|
103
|
-
if (lockAge >
|
|
100
|
+
if (lockAge > lockStaleMs) {
|
|
104
101
|
fs.unlinkSync(lockPath);
|
|
105
102
|
} else if (lockPid !== pid) {
|
|
106
103
|
// Lock held by another process
|
|
107
|
-
await new Promise(resolve => setTimeout(resolve,
|
|
104
|
+
await new Promise(resolve => setTimeout(resolve, baseRetryDelayMs));
|
|
108
105
|
continue;
|
|
109
106
|
}
|
|
110
107
|
}
|
|
@@ -125,17 +122,20 @@ async function acquireLockAsync(filePath: string, options: {
|
|
|
125
122
|
},
|
|
126
123
|
};
|
|
127
124
|
} catch (error: unknown) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
125
|
+
const err = error as { code?: string };
|
|
126
|
+
if (err.code === 'EEXIST') {
|
|
127
|
+
if (attempt < maxRetries - 1) {
|
|
128
|
+
await new Promise(resolve => setTimeout(resolve, baseRetryDelayMs));
|
|
131
129
|
continue;
|
|
132
130
|
}
|
|
133
131
|
}
|
|
134
|
-
|
|
132
|
+
const lockError = new Error(`Failed to acquire lock for ${filePath}: ${String(error)}`);
|
|
133
|
+
lockError.cause = error;
|
|
134
|
+
throw lockError;
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
throw new Error(`Failed to acquire lock for ${filePath} after ${
|
|
138
|
+
throw new Error(`Failed to acquire lock for ${filePath} after ${maxRetries} attempts`);
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
function releaseLock(ctx: LockContext): void {
|
|
@@ -225,7 +225,6 @@ function listNocturnalWorkflows(): WorkflowRow[] {
|
|
|
225
225
|
return [];
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
-
const Database = require('better-sqlite3');
|
|
229
228
|
const db = new Database(DB_PATH, { readonly: true });
|
|
230
229
|
const rows = db.prepare(`
|
|
231
230
|
SELECT workflow_id, workflow_type, state, metadata_json, created_at
|
|
@@ -279,6 +278,7 @@ async function main() {
|
|
|
279
278
|
const verbose = process.argv.includes('--verbose');
|
|
280
279
|
|
|
281
280
|
// 1. Check bootstrapped rules
|
|
281
|
+
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
282
282
|
let rules: LedgerRule[];
|
|
283
283
|
try {
|
|
284
284
|
rules = loadBootstrappedRules();
|
|
@@ -30,6 +30,7 @@ import type { PluginCommandContext, PluginCommandResult } from '../openclaw-sdk.
|
|
|
30
30
|
import {
|
|
31
31
|
type TrainerBackendKind,
|
|
32
32
|
type HardwareTier,
|
|
33
|
+
type TrainingExperimentResult,
|
|
33
34
|
} from '../core/external-training-contract.js';
|
|
34
35
|
import {
|
|
35
36
|
TrainingProgram,
|
|
@@ -270,14 +271,11 @@ Hardware tiers:
|
|
|
270
271
|
// This closes the gap in the create-experiment -> trainer -> import-result chain.
|
|
271
272
|
// NOTE: This blocks until training completes (could be minutes).
|
|
272
273
|
if (runNow) {
|
|
273
|
-
|
|
274
|
-
const {spec} = createResult;
|
|
275
274
|
const baseDir = TRAINER_SCRIPTS_DIR;
|
|
276
275
|
const scriptPath = path.join(baseDir, 'main.py');
|
|
277
276
|
const specPath = path.join(baseDir, `experiment-${spec.experimentId}.json`);
|
|
278
|
-
|
|
279
|
-
const
|
|
280
|
-
const resultFilePath = path.join(outputDir, `result-${spec.experimentId}.json`);
|
|
277
|
+
|
|
278
|
+
const resultFilePath = path.join(spec.outputDir, `result-${spec.experimentId}.json`);
|
|
281
279
|
|
|
282
280
|
// Write spec file
|
|
283
281
|
const specDir = path.dirname(specPath);
|
|
@@ -287,7 +285,7 @@ Hardware tiers:
|
|
|
287
285
|
fs.writeFileSync(specPath, JSON.stringify(spec, null, 2), 'utf-8');
|
|
288
286
|
|
|
289
287
|
|
|
290
|
-
let trainerResult!:
|
|
288
|
+
let trainerResult!: TrainingExperimentResult;
|
|
291
289
|
|
|
292
290
|
try {
|
|
293
291
|
if (spec.backend === 'dry-run') {
|
package/src/commands/pain.ts
CHANGED
|
@@ -127,20 +127,17 @@ export function handlePainCommand(ctx: PluginCommandContext): PluginCommandResul
|
|
|
127
127
|
|
|
128
128
|
// Determine Mental Mode (aligned with prompt.ts logic)
|
|
129
129
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
else if (gfi >= 40) mentalMode = '🤝 CONCILIATORY';
|
|
138
|
-
else mentalMode = '⚡ EFFICIENT';
|
|
139
|
-
}
|
|
130
|
+
const mentalMode = isZh
|
|
131
|
+
? gfi >= 70 ? '🚑 救赎模式 (HUMBLE_RECOVERY)'
|
|
132
|
+
: gfi >= 40 ? '🤝 安抚模式 (CONCILIATORY)'
|
|
133
|
+
: '⚡ 高效模式 (EFFICIENT)'
|
|
134
|
+
: gfi >= 70 ? '🚑 HUMBLE_RECOVERY'
|
|
135
|
+
: gfi >= 40 ? '🤝 CONCILIATORY'
|
|
136
|
+
: '⚡ EFFICIENT';
|
|
140
137
|
|
|
141
138
|
// Determine health status based on GFI
|
|
142
139
|
|
|
143
|
-
let healthLabel
|
|
140
|
+
let healthLabel: string;
|
|
144
141
|
let suggestionText = '';
|
|
145
142
|
|
|
146
143
|
if (isZh) {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* This command must operate on an explicitly resolved active workspace.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { PluginCommandDefinition, PluginCommandContext, PluginCommandResult, OpenClawPluginApi } from '../openclaw-sdk.js';
|
|
8
|
+
import type { PluginCommandDefinition, PluginCommandContext, PluginCommandResult, OpenClawPluginApi } from '../openclaw-sdk.js';
|
|
9
9
|
import { acquireQueueLock, EVOLUTION_QUEUE_LOCK_SUFFIX } from '../service/evolution-worker.js';
|
|
10
10
|
import * as fs from 'fs';
|
|
11
11
|
import * as path from 'path';
|
|
@@ -30,7 +30,7 @@ export interface BootstrapResult {
|
|
|
30
30
|
* @returns Array of principle IDs sorted by observedViolationCount (descending)
|
|
31
31
|
* @throws Error if no deterministic principles found
|
|
32
32
|
*/
|
|
33
|
-
export function selectPrinciplesForBootstrap(stateDir: string, limit
|
|
33
|
+
export function selectPrinciplesForBootstrap(stateDir: string, limit = 3): string[] {
|
|
34
34
|
// Load training store to get evaluability and violation data
|
|
35
35
|
const store = loadStore(stateDir);
|
|
36
36
|
|
|
@@ -76,7 +76,7 @@ export function selectPrinciplesForBootstrap(stateDir: string, limit: number = 3
|
|
|
76
76
|
* @returns Array of results indicating created or skipped status
|
|
77
77
|
* @throws Error if no deterministic principles found
|
|
78
78
|
*/
|
|
79
|
-
export function bootstrapRules(stateDir: string, limit
|
|
79
|
+
export function bootstrapRules(stateDir: string, limit = 3): BootstrapResult[] {
|
|
80
80
|
// Select principles for bootstrap
|
|
81
81
|
const selectedPrincipleIds = selectPrinciplesForBootstrap(stateDir, limit);
|
|
82
82
|
|
|
@@ -107,7 +107,7 @@ export function bootstrapRules(stateDir: string, limit: number = 3): BootstrapRe
|
|
|
107
107
|
|
|
108
108
|
// Create stub rule
|
|
109
109
|
const now = new Date().toISOString();
|
|
110
|
-
|
|
110
|
+
createRule(stateDir, {
|
|
111
111
|
id: ruleId,
|
|
112
112
|
version: 1,
|
|
113
113
|
name: `Stub bootstrap rule for ${principleId}`,
|
|
@@ -364,7 +364,7 @@ function validateSingleReplayReport(reportPath: string): ReplayValidationCategor
|
|
|
364
364
|
return 'missing_evidence_summary';
|
|
365
365
|
}
|
|
366
366
|
|
|
367
|
-
const evidenceSummary =
|
|
367
|
+
const evidenceSummary = parsed.evidenceSummary;
|
|
368
368
|
if (parsed.overallDecision === 'pass' && evidenceSummary.totalSamples === 0) {
|
|
369
369
|
return 'unsupported_pass';
|
|
370
370
|
}
|
|
@@ -116,6 +116,23 @@ export const DEFAULT_SCORING_WEIGHTS: ScoringWeights = {
|
|
|
116
116
|
confidence: 0.15,
|
|
117
117
|
};
|
|
118
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Result of diversity validation on Dreamer candidates.
|
|
121
|
+
* Soft enforcement: result is informational, never gates the pipeline.
|
|
122
|
+
*/
|
|
123
|
+
export interface DiversityValidationResult {
|
|
124
|
+
/** Whether candidates passed diversity checks */
|
|
125
|
+
diversityCheckPassed: boolean;
|
|
126
|
+
/** Whether at least 2 distinct risk levels were present */
|
|
127
|
+
riskLevelDiversity: boolean;
|
|
128
|
+
/** Whether no candidate pair exceeded keyword overlap threshold */
|
|
129
|
+
keywordOverlapPassed: boolean;
|
|
130
|
+
/** Highest pairwise keyword overlap score (for telemetry) */
|
|
131
|
+
maxOverlapScore: number;
|
|
132
|
+
/** Human-readable summary of check results */
|
|
133
|
+
details: string;
|
|
134
|
+
}
|
|
135
|
+
|
|
119
136
|
// ---------------------------------------------------------------------------
|
|
120
137
|
// Scoring Logic
|
|
121
138
|
// ---------------------------------------------------------------------------
|
|
@@ -232,6 +249,120 @@ export function checkThresholds(
|
|
|
232
249
|
return [failedThresholds.length === 0, failedThresholds];
|
|
233
250
|
}
|
|
234
251
|
|
|
252
|
+
/**
|
|
253
|
+
* Validate that Dreamer candidates are strategically diverse.
|
|
254
|
+
*
|
|
255
|
+
* DIVER-03: Checks risk level diversity (Set.size >= 2 when candidates >= 2)
|
|
256
|
+
* and keyword overlap similarity (reject if intersection / max(|A|, |B|) > 0.8
|
|
257
|
+
* for words > 3 chars per D-05).
|
|
258
|
+
*
|
|
259
|
+
* This is SOFT enforcement: returns a result, never throws.
|
|
260
|
+
* Pipeline continues regardless of diversityCheckPassed value.
|
|
261
|
+
*
|
|
262
|
+
* @param candidates - Dreamer candidates to validate
|
|
263
|
+
* @returns DiversityValidationResult with pass/fail details
|
|
264
|
+
*/
|
|
265
|
+
export function validateCandidateDiversity(
|
|
266
|
+
candidates: DreamerCandidate[],
|
|
267
|
+
): DiversityValidationResult {
|
|
268
|
+
// Edge cases: empty, null, or single candidate always passes
|
|
269
|
+
if (!candidates || candidates.length <= 1) {
|
|
270
|
+
return {
|
|
271
|
+
diversityCheckPassed: true,
|
|
272
|
+
riskLevelDiversity: true,
|
|
273
|
+
keywordOverlapPassed: true,
|
|
274
|
+
maxOverlapScore: 0,
|
|
275
|
+
details: candidates?.length === 1
|
|
276
|
+
? 'Single candidate — diversity check not applicable'
|
|
277
|
+
: 'No candidates to validate',
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Check 1: Risk level diversity (D-05)
|
|
282
|
+
const riskLevels = new Set(
|
|
283
|
+
candidates
|
|
284
|
+
.map(c => c.riskLevel)
|
|
285
|
+
.filter((r): r is "low" | "medium" | "high" => typeof r === 'string')
|
|
286
|
+
);
|
|
287
|
+
// If NO candidates have riskLevel, skip risk diversity check (graceful degradation)
|
|
288
|
+
const riskLevelDiversity = riskLevels.size === 0 || riskLevels.size >= 2;
|
|
289
|
+
|
|
290
|
+
// Check 2: Keyword overlap (D-05: intersection / max(|A|, |B|) for words > 3 chars)
|
|
291
|
+
let maxOverlapScore = 0;
|
|
292
|
+
let keywordOverlapPassed = true;
|
|
293
|
+
|
|
294
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
295
|
+
for (let j = i + 1; j < candidates.length; j++) {
|
|
296
|
+
const overlap = computeKeywordOverlap(
|
|
297
|
+
candidates[i].betterDecision ?? '',
|
|
298
|
+
candidates[j].betterDecision ?? '',
|
|
299
|
+
);
|
|
300
|
+
if (overlap > maxOverlapScore) {
|
|
301
|
+
maxOverlapScore = overlap;
|
|
302
|
+
}
|
|
303
|
+
if (overlap > 0.8) {
|
|
304
|
+
keywordOverlapPassed = false;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const diversityCheckPassed = riskLevelDiversity && keywordOverlapPassed;
|
|
310
|
+
|
|
311
|
+
// Build details string
|
|
312
|
+
const parts: string[] = [];
|
|
313
|
+
if (!riskLevelDiversity) {
|
|
314
|
+
parts.push(`Risk levels not diverse (found: ${[...riskLevels].join(', ') || 'none'})`);
|
|
315
|
+
}
|
|
316
|
+
if (!keywordOverlapPassed) {
|
|
317
|
+
parts.push(`Keyword overlap too high (max: ${maxOverlapScore.toFixed(2)})`);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
diversityCheckPassed,
|
|
322
|
+
riskLevelDiversity,
|
|
323
|
+
keywordOverlapPassed,
|
|
324
|
+
maxOverlapScore: Math.round(maxOverlapScore * 100) / 100,
|
|
325
|
+
details: diversityCheckPassed
|
|
326
|
+
? 'Diversity check passed'
|
|
327
|
+
: parts.join('; '),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Compute keyword overlap between two strings.
|
|
333
|
+
* Algorithm: intersection / max(|A|, |B|) for words > 3 chars (per D-05).
|
|
334
|
+
* Returns value between 0 and 1.
|
|
335
|
+
*/
|
|
336
|
+
function computeKeywordOverlap(textA: string, textB: string): number {
|
|
337
|
+
const wordsA = extractKeywords(textA);
|
|
338
|
+
const wordsB = extractKeywords(textB);
|
|
339
|
+
|
|
340
|
+
if (wordsA.length === 0 && wordsB.length === 0) return 0;
|
|
341
|
+
if (wordsA.length === 0 || wordsB.length === 0) return 0;
|
|
342
|
+
|
|
343
|
+
const setA = new Set(wordsA);
|
|
344
|
+
const setB = new Set(wordsB);
|
|
345
|
+
|
|
346
|
+
let intersection = 0;
|
|
347
|
+
for (const word of setA) {
|
|
348
|
+
if (setB.has(word)) intersection++;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const denominator = Math.max(setA.size, setB.size);
|
|
352
|
+
return denominator === 0 ? 0 : intersection / denominator;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Extract keywords from text: words > 3 characters, lowercased.
|
|
357
|
+
*/
|
|
358
|
+
function extractKeywords(text: string): string[] {
|
|
359
|
+
if (!text) return [];
|
|
360
|
+
return text
|
|
361
|
+
.toLowerCase()
|
|
362
|
+
.split(/[^a-z0-9]+/)
|
|
363
|
+
.filter(w => w.length > 3);
|
|
364
|
+
}
|
|
365
|
+
|
|
235
366
|
/**
|
|
236
367
|
* Score and rank all candidates deterministically.
|
|
237
368
|
*
|