principles-disciple 1.28.0 → 1.28.2
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 +4 -4
- package/package.json +4 -4
- package/scripts/validate-live-path.ts +18 -18
- package/src/commands/context.ts +1 -0
- package/src/commands/disable-impl.ts +2 -0
- package/src/commands/evolution-status.ts +2 -0
- package/src/commands/focus.ts +2 -0
- package/src/commands/nocturnal-train.ts +4 -6
- package/src/commands/pain.ts +9 -11
- package/src/commands/pd-reflect.ts +1 -1
- package/src/commands/principle-rollback.ts +1 -0
- package/src/commands/rollback-impl.ts +1 -0
- package/src/core/adaptive-thresholds.ts +1 -0
- package/src/core/bootstrap-rules.ts +3 -3
- package/src/core/dictionary.ts +1 -0
- package/src/core/empathy-keyword-matcher.ts +1 -0
- package/src/core/event-log.ts +2 -0
- package/src/core/evolution-engine.ts +1 -0
- package/src/core/external-training-contract.ts +1 -0
- package/src/core/focus-history.ts +3 -0
- package/src/core/init.ts +1 -0
- package/src/core/merge-gate-audit.ts +1 -1
- package/src/core/nocturnal-arbiter.ts +3 -0
- package/src/core/nocturnal-candidate-scoring.ts +131 -0
- package/src/core/nocturnal-compliance.ts +1 -0
- package/src/core/nocturnal-dataset.ts +1 -0
- package/src/core/nocturnal-executability.ts +1 -0
- package/src/core/nocturnal-reasoning-deriver.ts +338 -0
- package/src/core/nocturnal-rule-implementation-validator.ts +1 -0
- package/src/core/nocturnal-trinity.ts +457 -18
- package/src/core/pain-context-extractor.ts +2 -3
- package/src/core/pain.ts +1 -0
- package/src/core/pd-task-reconciler.ts +1 -0
- package/src/core/pd-task-service.ts +1 -0
- package/src/core/principle-internalization/deprecated-readiness.ts +1 -0
- package/src/core/principle-internalization/principle-lifecycle-service.ts +1 -0
- package/src/core/principle-tree-migration.ts +3 -4
- package/src/core/replay-engine.ts +4 -0
- package/src/core/risk-calculator.ts +1 -0
- package/src/core/rule-host.ts +2 -0
- package/src/core/session-tracker.ts +2 -0
- package/src/core/thinking-models.ts +1 -0
- package/src/core/thinking-os-parser.ts +3 -3
- package/src/core/trajectory.ts +4 -0
- package/src/hooks/bash-risk.ts +1 -1
- package/src/hooks/gfi-gate.ts +1 -1
- package/src/hooks/lifecycle-routing.ts +1 -0
- package/src/hooks/pain.ts +2 -1
- package/src/hooks/prompt.ts +37 -2
- package/src/hooks/subagent.ts +1 -1
- package/src/hooks/trajectory-collector.ts +1 -0
- package/src/http/principles-console-route.ts +2 -0
- package/src/index.ts +1 -1
- package/src/service/central-database.ts +2 -0
- package/src/service/central-sync-service.ts +1 -0
- package/src/service/control-ui-query-service.ts +2 -0
- package/src/service/event-log-auditor.ts +2 -0
- package/src/service/evolution-worker.ts +2 -1
- package/src/service/health-query-service.ts +20 -6
- package/src/service/nocturnal-runtime.ts +4 -0
- package/src/service/runtime-summary-service.ts +5 -0
- package/src/service/subagent-workflow/deep-reflect-workflow-manager.ts +1 -0
- package/src/service/subagent-workflow/nocturnal-workflow-manager.ts +2 -1
- package/src/service/subagent-workflow/subagent-error-utils.ts +1 -0
- package/src/service/subagent-workflow/workflow-manager-base.ts +1 -0
- package/src/tools/critique-prompt.ts +1 -0
- package/src/utils/io.ts +1 -0
- 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
|
@@ -150,6 +150,7 @@ export const deepReflectWorkflowSpec: SubagentWorkflowSpec<DeepReflectResult> =
|
|
|
150
150
|
};
|
|
151
151
|
},
|
|
152
152
|
|
|
153
|
+
// eslint-disable-next-line complexity -- complexity 13, refactor candidate
|
|
153
154
|
async persistResult(ctx: WorkflowPersistContext<DeepReflectResult>): Promise<void> {
|
|
154
155
|
const { result, metadata, workspaceDir } = ctx;
|
|
155
156
|
|
|
@@ -36,7 +36,6 @@ import {
|
|
|
36
36
|
} from '../nocturnal-service.js';
|
|
37
37
|
import { type TrinityStageFailure, type TrinityResult } from '../../core/nocturnal-trinity.js';
|
|
38
38
|
import type { TrinityRuntimeAdapter } from '../../core/nocturnal-trinity.js';
|
|
39
|
-
import type { NocturnalSessionSnapshot } from '../../core/nocturnal-trajectory-extractor.js';
|
|
40
39
|
import type { RecentPainContext } from '../evolution-worker.js';
|
|
41
40
|
import * as fs from 'fs';
|
|
42
41
|
import * as path from 'path';
|
|
@@ -244,6 +243,7 @@ export class NocturnalWorkflowManager implements WorkflowManager {
|
|
|
244
243
|
// #213: Wrap fire-and-forget Promise with .catch() to prevent
|
|
245
244
|
// unhandled promise rejections if anything throws outside the try-catch
|
|
246
245
|
// (e.g., during parameter construction or environment errors).
|
|
246
|
+
// eslint-disable-next-line complexity -- complexity 15, refactor candidate
|
|
247
247
|
Promise.resolve().then(async () => {
|
|
248
248
|
try {
|
|
249
249
|
const result = await executeNocturnalReflectionAsync(
|
|
@@ -652,6 +652,7 @@ export class NocturnalWorkflowManager implements WorkflowManager {
|
|
|
652
652
|
* Derives stage events from TrinityResult.telemetry and TrinityResult.failures.
|
|
653
653
|
* Always records _start event for each stage that ran, plus _complete or _failed based on outcome.
|
|
654
654
|
*/
|
|
655
|
+
// eslint-disable-next-line complexity -- complexity 12, refactor candidate
|
|
655
656
|
private recordStageEvents(workflowId: string, result: TrinityResult): void {
|
|
656
657
|
const { telemetry, failures } = result;
|
|
657
658
|
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*
|
|
6
6
|
* Callers should suppress warnings for these errors — they are not real failures.
|
|
7
7
|
*/
|
|
8
|
+
// eslint-disable-next-line complexity -- complexity 13, refactor candidate
|
|
8
9
|
export function isExpectedSubagentError(err: unknown): boolean {
|
|
9
10
|
const msg = String(err);
|
|
10
11
|
return (
|
|
@@ -449,6 +449,7 @@ export abstract class WorkflowManagerBase implements WorkflowManager {
|
|
|
449
449
|
}
|
|
450
450
|
}
|
|
451
451
|
|
|
452
|
+
// eslint-disable-next-line complexity -- complexity 11, slightly over threshold
|
|
452
453
|
async sweepExpiredWorkflows(maxAgeMs?: number): Promise<number> {
|
|
453
454
|
const ttl = maxAgeMs ?? this.defaultTtlMs;
|
|
454
455
|
const expired = this.store.getExpiredWorkflows(ttl);
|
package/src/utils/io.ts
CHANGED
|
@@ -2,6 +2,7 @@ import * as path from 'path';
|
|
|
2
2
|
import * as fs from 'fs';
|
|
3
3
|
import { resolvePdPath } from '../core/paths.js';
|
|
4
4
|
|
|
5
|
+
// eslint-disable-next-line complexity -- complexity 12, refactor candidate
|
|
5
6
|
export function normalizePath(filePath: string, projectDir: string): string {
|
|
6
7
|
if (!filePath) return '';
|
|
7
8
|
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
rankCandidates,
|
|
6
6
|
runTournament,
|
|
7
7
|
DEFAULT_SCORING_WEIGHTS,
|
|
8
|
+
validateCandidateDiversity,
|
|
8
9
|
} from '../../src/core/nocturnal-candidate-scoring.js';
|
|
9
10
|
import type { DreamerCandidate, PhilosopherJudgment } from '../../src/core/nocturnal-trinity.js';
|
|
10
11
|
import type { ThresholdValues } from '../../src/core/adaptive-thresholds.js';
|
|
@@ -398,3 +399,134 @@ describe('DEFAULT_SCORING_WEIGHTS', () => {
|
|
|
398
399
|
}
|
|
399
400
|
});
|
|
400
401
|
});
|
|
402
|
+
|
|
403
|
+
// ---------------------------------------------------------------------------
|
|
404
|
+
// Tests: validateCandidateDiversity
|
|
405
|
+
// ---------------------------------------------------------------------------
|
|
406
|
+
|
|
407
|
+
describe('validateCandidateDiversity', () => {
|
|
408
|
+
it('passes when candidates have 2+ distinct risk levels and low keyword overlap', () => {
|
|
409
|
+
const candidates: DreamerCandidate[] = [
|
|
410
|
+
makeCandidate({ candidateIndex: 0, riskLevel: 'low', betterDecision: 'Read config.json to verify settings' }),
|
|
411
|
+
makeCandidate({ candidateIndex: 1, riskLevel: 'high', betterDecision: 'Refactor the entire authentication module from scratch' }),
|
|
412
|
+
];
|
|
413
|
+
const result = validateCandidateDiversity(candidates);
|
|
414
|
+
expect(result.diversityCheckPassed).toBe(true);
|
|
415
|
+
expect(result.riskLevelDiversity).toBe(true);
|
|
416
|
+
expect(result.keywordOverlapPassed).toBe(true);
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it('fails when all candidates have the same risk level', () => {
|
|
420
|
+
const candidates: DreamerCandidate[] = [
|
|
421
|
+
makeCandidate({ candidateIndex: 0, riskLevel: 'low', betterDecision: 'Read file A to check settings' }),
|
|
422
|
+
makeCandidate({ candidateIndex: 1, riskLevel: 'low', betterDecision: 'Review file completely different approach' }),
|
|
423
|
+
makeCandidate({ candidateIndex: 2, riskLevel: 'low', betterDecision: 'Inspect another unique diagnostic method' }),
|
|
424
|
+
];
|
|
425
|
+
const result = validateCandidateDiversity(candidates);
|
|
426
|
+
expect(result.diversityCheckPassed).toBe(false);
|
|
427
|
+
expect(result.riskLevelDiversity).toBe(false);
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it('fails when candidate pair has keyword overlap > 0.8', () => {
|
|
431
|
+
const candidates: DreamerCandidate[] = [
|
|
432
|
+
makeCandidate({ candidateIndex: 0, riskLevel: 'low', betterDecision: 'Review the authentication configuration file before making any changes to the system' }),
|
|
433
|
+
makeCandidate({ candidateIndex: 1, riskLevel: 'high', betterDecision: 'Review the authentication configuration file before making any changes to the system' }),
|
|
434
|
+
];
|
|
435
|
+
const result = validateCandidateDiversity(candidates);
|
|
436
|
+
expect(result.diversityCheckPassed).toBe(false);
|
|
437
|
+
expect(result.keywordOverlapPassed).toBe(false);
|
|
438
|
+
expect(result.maxOverlapScore).toBeGreaterThan(0.8);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it('passes for single candidate', () => {
|
|
442
|
+
const candidates: DreamerCandidate[] = [
|
|
443
|
+
makeCandidate({ candidateIndex: 0, riskLevel: 'low' }),
|
|
444
|
+
];
|
|
445
|
+
const result = validateCandidateDiversity(candidates);
|
|
446
|
+
expect(result.diversityCheckPassed).toBe(true);
|
|
447
|
+
expect(result.details).toContain('Single candidate');
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
it('passes for empty array', () => {
|
|
451
|
+
const result = validateCandidateDiversity([]);
|
|
452
|
+
expect(result.diversityCheckPassed).toBe(true);
|
|
453
|
+
expect(result.details).toContain('No candidates');
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
it('passes when candidates lack riskLevel (graceful degradation)', () => {
|
|
457
|
+
const candidates: DreamerCandidate[] = [
|
|
458
|
+
makeCandidate({ candidateIndex: 0, betterDecision: 'Read config.json to verify settings' }),
|
|
459
|
+
makeCandidate({ candidateIndex: 1, betterDecision: 'Refactor the entire authentication module from scratch' }),
|
|
460
|
+
];
|
|
461
|
+
// No riskLevel on any candidate - should pass (no risk levels to check)
|
|
462
|
+
const result = validateCandidateDiversity(candidates);
|
|
463
|
+
expect(result.diversityCheckPassed).toBe(true);
|
|
464
|
+
expect(result.riskLevelDiversity).toBe(true);
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
it('fails when some candidates have riskLevel but fewer than 2 distinct values', () => {
|
|
468
|
+
const candidates: DreamerCandidate[] = [
|
|
469
|
+
makeCandidate({ candidateIndex: 0, riskLevel: 'medium', betterDecision: 'Read config.json to verify settings' }),
|
|
470
|
+
makeCandidate({ candidateIndex: 1, betterDecision: 'Refactor the entire authentication module from scratch' }),
|
|
471
|
+
];
|
|
472
|
+
// Only 1 candidate has riskLevel, so only 1 distinct value → fail
|
|
473
|
+
const result = validateCandidateDiversity(candidates);
|
|
474
|
+
expect(result.diversityCheckPassed).toBe(false);
|
|
475
|
+
expect(result.riskLevelDiversity).toBe(false);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
it('uses max(|A|, |B|) as denominator for keyword overlap', () => {
|
|
479
|
+
// Short text A, long text B - overlap should use max as denominator
|
|
480
|
+
const candidates: DreamerCandidate[] = [
|
|
481
|
+
makeCandidate({ candidateIndex: 0, riskLevel: 'low', betterDecision: 'review authentication configuration' }),
|
|
482
|
+
makeCandidate({ candidateIndex: 1, riskLevel: 'high', betterDecision: 'review authentication configuration before proceeding with changes to the deployment pipeline infrastructure' }),
|
|
483
|
+
];
|
|
484
|
+
const result = validateCandidateDiversity(candidates);
|
|
485
|
+
// "review", "authentication", "configuration" overlap in both
|
|
486
|
+
// Set A = {review, authentication, configuration} = 3
|
|
487
|
+
// Set B = {review, authentication, configuration, before, proceeding, with, changes, deployment, pipeline, infrastructure} = 10
|
|
488
|
+
// intersection = 3, max(3, 10) = 10, overlap = 3/10 = 0.3
|
|
489
|
+
expect(result.maxOverlapScore).toBeLessThanOrEqual(0.4);
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
it('ignores words <= 3 characters in keyword overlap', () => {
|
|
493
|
+
const candidates: DreamerCandidate[] = [
|
|
494
|
+
makeCandidate({ candidateIndex: 0, riskLevel: 'low', betterDecision: 'the and but for' }),
|
|
495
|
+
makeCandidate({ candidateIndex: 1, riskLevel: 'high', betterDecision: 'the and but for' }),
|
|
496
|
+
];
|
|
497
|
+
// All words are <= 3 chars, so no keywords extracted → overlap = 0
|
|
498
|
+
const result = validateCandidateDiversity(candidates);
|
|
499
|
+
expect(result.keywordOverlapPassed).toBe(true);
|
|
500
|
+
expect(result.maxOverlapScore).toBe(0);
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
it('never throws on malformed input', () => {
|
|
504
|
+
// Undefined candidates
|
|
505
|
+
expect(() => validateCandidateDiversity(undefined as unknown as DreamerCandidate[])).not.toThrow();
|
|
506
|
+
// Null candidates
|
|
507
|
+
expect(() => validateCandidateDiversity(null as unknown as DreamerCandidate[])).not.toThrow();
|
|
508
|
+
// Candidates with undefined fields
|
|
509
|
+
expect(() => validateCandidateDiversity([
|
|
510
|
+
{ candidateIndex: 0 } as DreamerCandidate,
|
|
511
|
+
])).not.toThrow();
|
|
512
|
+
// Mixed valid and malformed
|
|
513
|
+
expect(() => validateCandidateDiversity([
|
|
514
|
+
makeCandidate({ candidateIndex: 0, riskLevel: 'low' }),
|
|
515
|
+
{ candidateIndex: 1 } as DreamerCandidate,
|
|
516
|
+
])).not.toThrow();
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it('returns correct maxOverlapScore rounded to 2 decimal places', () => {
|
|
520
|
+
const candidates: DreamerCandidate[] = [
|
|
521
|
+
makeCandidate({ candidateIndex: 0, riskLevel: 'low', betterDecision: 'Review configuration settings before deployment' }),
|
|
522
|
+
makeCandidate({ candidateIndex: 1, riskLevel: 'high', betterDecision: 'Review configuration settings before deployment testing' }),
|
|
523
|
+
];
|
|
524
|
+
const result = validateCandidateDiversity(candidates);
|
|
525
|
+
// Verify the maxOverlapScore is a number with at most 2 decimal places
|
|
526
|
+
const decimalPart = result.maxOverlapScore.toString().split('.')[1];
|
|
527
|
+
if (decimalPart) {
|
|
528
|
+
expect(decimalPart.length).toBeLessThanOrEqual(2);
|
|
529
|
+
}
|
|
530
|
+
expect(typeof result.maxOverlapScore).toBe('number');
|
|
531
|
+
});
|
|
532
|
+
});
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
deriveReasoningChain,
|
|
4
|
+
deriveDecisionPoints,
|
|
5
|
+
deriveContextualFactors,
|
|
6
|
+
} from '../../src/core/nocturnal-reasoning-deriver.js';
|
|
7
|
+
import type {
|
|
8
|
+
NocturnalAssistantTurn,
|
|
9
|
+
NocturnalToolCall,
|
|
10
|
+
NocturnalUserTurn,
|
|
11
|
+
NocturnalSessionSnapshot,
|
|
12
|
+
} from '../../src/core/nocturnal-trajectory-extractor.js';
|
|
13
|
+
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// Test Fixtures
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
function makeAssistantTurn(overrides: Partial<NocturnalAssistantTurn> = {}): NocturnalAssistantTurn {
|
|
19
|
+
return {
|
|
20
|
+
turnIndex: 0,
|
|
21
|
+
sanitizedText: 'I will read the file to understand the structure before making changes.',
|
|
22
|
+
model: 'gpt-4',
|
|
23
|
+
createdAt: '2026-04-12T10:00:00.000Z',
|
|
24
|
+
...overrides,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
// deriveReasoningChain
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
describe('deriveReasoningChain', () => {
|
|
33
|
+
it('extracts thinking content from <thinking> tags', () => {
|
|
34
|
+
const turns = [makeAssistantTurn({
|
|
35
|
+
sanitizedText: '<thinking>planning the approach</thinking>Now I will proceed.',
|
|
36
|
+
})];
|
|
37
|
+
const result = deriveReasoningChain(turns);
|
|
38
|
+
expect(result).toHaveLength(1);
|
|
39
|
+
expect(result[0].thinkingContent).toBe('planning the approach');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('returns empty thinkingContent when no <thinking> tags present', () => {
|
|
43
|
+
const turns = [makeAssistantTurn({
|
|
44
|
+
sanitizedText: 'I will just read the file and proceed.',
|
|
45
|
+
})];
|
|
46
|
+
const result = deriveReasoningChain(turns);
|
|
47
|
+
expect(result[0].thinkingContent).toBe('');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('detects uncertainty markers', () => {
|
|
51
|
+
const turns = [makeAssistantTurn({
|
|
52
|
+
sanitizedText: 'let me verify the output. not sure if this is correct.',
|
|
53
|
+
})];
|
|
54
|
+
const result = deriveReasoningChain(turns);
|
|
55
|
+
expect(result[0].uncertaintyMarkers).toEqual(
|
|
56
|
+
expect.arrayContaining([
|
|
57
|
+
expect.stringContaining('let me verify'),
|
|
58
|
+
expect.stringContaining('not sure if'),
|
|
59
|
+
]),
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('detects all 3 uncertainty patterns', () => {
|
|
64
|
+
const turns = [makeAssistantTurn({
|
|
65
|
+
sanitizedText: 'let me check the file. I should probably review this. not sure whether this works.',
|
|
66
|
+
})];
|
|
67
|
+
const result = deriveReasoningChain(turns);
|
|
68
|
+
expect(result[0].uncertaintyMarkers.length).toBeGreaterThanOrEqual(3);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('computes high confidence signal', () => {
|
|
72
|
+
// Use text rich in thinking model patterns to trigger high activation
|
|
73
|
+
const richText = 'Let me plan this carefully. I need to analyze the structure first. ' +
|
|
74
|
+
'The approach should consider multiple perspectives. Let me think step by step. ' +
|
|
75
|
+
'I should verify my understanding. Breaking this down into parts helps. ' +
|
|
76
|
+
'Consider the constraints and requirements carefully.';
|
|
77
|
+
const turns = [makeAssistantTurn({ sanitizedText: richText })];
|
|
78
|
+
const result = deriveReasoningChain(turns);
|
|
79
|
+
// The exact signal depends on thinking model definitions, but it should be one of the three
|
|
80
|
+
expect(['high', 'medium', 'low']).toContain(result[0].confidenceSignal);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('computes low confidence signal', () => {
|
|
84
|
+
const turns = [makeAssistantTurn({
|
|
85
|
+
sanitizedText: 'Just a simple text with no particular patterns.',
|
|
86
|
+
})];
|
|
87
|
+
const result = deriveReasoningChain(turns);
|
|
88
|
+
expect(result[0].confidenceSignal).toBe('low');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('returns empty array for empty input', () => {
|
|
92
|
+
expect(deriveReasoningChain([])).toEqual([]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('returns empty array for null input', () => {
|
|
96
|
+
expect(deriveReasoningChain(null as any)).toEqual([]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('handles multiple turns correctly', () => {
|
|
100
|
+
const turns = [
|
|
101
|
+
makeAssistantTurn({ turnIndex: 0, sanitizedText: 'First turn.' }),
|
|
102
|
+
makeAssistantTurn({ turnIndex: 1, sanitizedText: '<thinking>Second turn thinking</thinking>' }),
|
|
103
|
+
makeAssistantTurn({ turnIndex: 2, sanitizedText: 'let me check this. Third turn.' }),
|
|
104
|
+
];
|
|
105
|
+
const result = deriveReasoningChain(turns);
|
|
106
|
+
expect(result).toHaveLength(3);
|
|
107
|
+
expect(result.map(r => r.turnIndex)).toEqual([0, 1, 2]);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('handles empty sanitizedText gracefully', () => {
|
|
111
|
+
const turns = [makeAssistantTurn({ sanitizedText: '' })];
|
|
112
|
+
const result = deriveReasoningChain(turns);
|
|
113
|
+
expect(result[0].thinkingContent).toBe('');
|
|
114
|
+
expect(result[0].uncertaintyMarkers).toEqual([]);
|
|
115
|
+
expect(result[0].confidenceSignal).toBe('low');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('extracts thinking content from multiline tags', () => {
|
|
119
|
+
const turns = [makeAssistantTurn({
|
|
120
|
+
sanitizedText: '<thinking>\nStep 1: analyze\nStep 2: plan\n</thinking>',
|
|
121
|
+
})];
|
|
122
|
+
const result = deriveReasoningChain(turns);
|
|
123
|
+
expect(result[0].thinkingContent).toContain('Step 1: analyze');
|
|
124
|
+
expect(result[0].thinkingContent).toContain('Step 2: plan');
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
// Test Fixtures (Plan 02)
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
|
|
132
|
+
function makeToolCall(overrides: Partial<NocturnalToolCall> = {}): NocturnalToolCall {
|
|
133
|
+
return {
|
|
134
|
+
toolName: 'edit',
|
|
135
|
+
outcome: 'success',
|
|
136
|
+
filePath: 'src/index.ts',
|
|
137
|
+
durationMs: 150,
|
|
138
|
+
exitCode: 0,
|
|
139
|
+
errorType: null,
|
|
140
|
+
errorMessage: null,
|
|
141
|
+
createdAt: '2026-04-12T10:01:00.000Z',
|
|
142
|
+
...overrides,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function makeUserTurn(overrides: Partial<NocturnalUserTurn> = {}): NocturnalUserTurn {
|
|
147
|
+
return {
|
|
148
|
+
turnIndex: 0,
|
|
149
|
+
correctionDetected: false,
|
|
150
|
+
correctionCue: null,
|
|
151
|
+
createdAt: '2026-04-12T10:00:30.000Z',
|
|
152
|
+
...overrides,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function makeSnapshot(overrides: Partial<NocturnalSessionSnapshot> = {}): NocturnalSessionSnapshot {
|
|
157
|
+
return {
|
|
158
|
+
sessionId: 'test-session-001',
|
|
159
|
+
startedAt: '2026-04-12T10:00:00.000Z',
|
|
160
|
+
updatedAt: '2026-04-12T10:05:00.000Z',
|
|
161
|
+
assistantTurns: [],
|
|
162
|
+
userTurns: [],
|
|
163
|
+
toolCalls: [],
|
|
164
|
+
painEvents: [],
|
|
165
|
+
gateBlocks: [],
|
|
166
|
+
stats: {
|
|
167
|
+
totalAssistantTurns: 0,
|
|
168
|
+
totalToolCalls: 0,
|
|
169
|
+
totalPainEvents: 0,
|
|
170
|
+
totalGateBlocks: 0,
|
|
171
|
+
failureCount: 0,
|
|
172
|
+
},
|
|
173
|
+
...overrides,
|
|
174
|
+
} as NocturnalSessionSnapshot;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ---------------------------------------------------------------------------
|
|
178
|
+
// deriveDecisionPoints
|
|
179
|
+
// ---------------------------------------------------------------------------
|
|
180
|
+
|
|
181
|
+
describe('deriveDecisionPoints', () => {
|
|
182
|
+
it('extracts beforeContext from preceding assistant turn', () => {
|
|
183
|
+
const turns = [makeAssistantTurn({
|
|
184
|
+
sanitizedText: 'I will analyze the code structure before making changes to ensure correctness.',
|
|
185
|
+
createdAt: '2026-04-12T10:00:00.000Z',
|
|
186
|
+
})];
|
|
187
|
+
const toolCalls = [makeToolCall({ createdAt: '2026-04-12T10:01:00.000Z' })];
|
|
188
|
+
const result = deriveDecisionPoints(turns, toolCalls);
|
|
189
|
+
expect(result).toHaveLength(1);
|
|
190
|
+
expect(result[0].beforeContext).toBe('I will analyze the code structure before making changes to ensure correctness.');
|
|
191
|
+
expect(result[0].toolName).toBe('edit');
|
|
192
|
+
expect(result[0].outcome).toBe('success');
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('extracts afterReflection on failure outcome', () => {
|
|
196
|
+
const turns = [
|
|
197
|
+
makeAssistantTurn({ createdAt: '2026-04-12T10:00:00.000Z', sanitizedText: 'before' }),
|
|
198
|
+
makeAssistantTurn({ createdAt: '2026-04-12T10:02:00.000Z', sanitizedText: 'After the failure I need to reconsider the approach and try a different method' }),
|
|
199
|
+
];
|
|
200
|
+
const toolCalls = [makeToolCall({
|
|
201
|
+
outcome: 'failure',
|
|
202
|
+
createdAt: '2026-04-12T10:01:00.000Z',
|
|
203
|
+
})];
|
|
204
|
+
const result = deriveDecisionPoints(turns, toolCalls);
|
|
205
|
+
expect(result[0].afterReflection).toBe('After the failure I need to reconsider the approach and try a different method');
|
|
206
|
+
expect(result[0].outcome).toBe('failure');
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('omits afterReflection on success outcome', () => {
|
|
210
|
+
const turns = [makeAssistantTurn({ createdAt: '2026-04-12T10:00:00.000Z' })];
|
|
211
|
+
const toolCalls = [makeToolCall({ outcome: 'success', createdAt: '2026-04-12T10:01:00.000Z' })];
|
|
212
|
+
const result = deriveDecisionPoints(turns, toolCalls);
|
|
213
|
+
expect(result[0].afterReflection).toBeUndefined();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('returns empty array for empty toolCalls', () => {
|
|
217
|
+
expect(deriveDecisionPoints([makeAssistantTurn()], [])).toEqual([]);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('returns empty beforeContext when no assistant turns', () => {
|
|
221
|
+
const toolCalls = [makeToolCall()];
|
|
222
|
+
const result = deriveDecisionPoints([], toolCalls);
|
|
223
|
+
expect(result).toHaveLength(1);
|
|
224
|
+
expect(result[0].beforeContext).toBe('');
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it('computes confidenceDelta on failure', () => {
|
|
228
|
+
const beforeText = 'Let me plan this carefully. I need to analyze the structure first. ' +
|
|
229
|
+
'The approach should consider multiple perspectives. Let me think step by step. ' +
|
|
230
|
+
'I should verify my understanding. Breaking this down into parts helps.';
|
|
231
|
+
const turns = [
|
|
232
|
+
makeAssistantTurn({ createdAt: '2026-04-12T10:00:00.000Z', sanitizedText: beforeText }),
|
|
233
|
+
makeAssistantTurn({ createdAt: '2026-04-12T10:02:00.000Z', sanitizedText: 'ok' }),
|
|
234
|
+
];
|
|
235
|
+
const toolCalls = [makeToolCall({ outcome: 'failure', createdAt: '2026-04-12T10:01:00.000Z' })];
|
|
236
|
+
const result = deriveDecisionPoints(turns, toolCalls);
|
|
237
|
+
// confidenceDelta should be computed (defined) when both before and after turns exist
|
|
238
|
+
expect(result[0].confidenceDelta).toBeDefined();
|
|
239
|
+
expect(typeof result[0].confidenceDelta).toBe('number');
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('matches by createdAt timestamp not turnIndex', () => {
|
|
243
|
+
const turns = [
|
|
244
|
+
makeAssistantTurn({ turnIndex: 2, createdAt: '2026-04-12T10:00:00.000Z', sanitizedText: 'turn index 2' }),
|
|
245
|
+
makeAssistantTurn({ turnIndex: 0, createdAt: '2026-04-12T09:59:00.000Z', sanitizedText: 'turn index 0' }),
|
|
246
|
+
];
|
|
247
|
+
const toolCalls = [makeToolCall({ createdAt: '2026-04-12T10:01:00.000Z' })];
|
|
248
|
+
const result = deriveDecisionPoints(turns, toolCalls);
|
|
249
|
+
// Should pick turnIndex 2 (closest before tool call by timestamp)
|
|
250
|
+
expect(result[0].beforeContext).toBe('turn index 2');
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
// ---------------------------------------------------------------------------
|
|
255
|
+
// deriveContextualFactors
|
|
256
|
+
// ---------------------------------------------------------------------------
|
|
257
|
+
|
|
258
|
+
describe('deriveContextualFactors', () => {
|
|
259
|
+
it('computes all four factors from a rich snapshot', () => {
|
|
260
|
+
const snapshot = makeSnapshot({
|
|
261
|
+
toolCalls: [
|
|
262
|
+
makeToolCall({ toolName: 'read', outcome: 'success', createdAt: '2026-04-12T10:00:00.000Z' }),
|
|
263
|
+
makeToolCall({ toolName: 'edit', outcome: 'success', createdAt: '2026-04-12T10:00:01.000Z' }),
|
|
264
|
+
makeToolCall({ toolName: 'edit', outcome: 'failure', createdAt: '2026-04-12T10:00:02.000Z' }),
|
|
265
|
+
],
|
|
266
|
+
userTurns: [makeUserTurn({ correctionDetected: true })],
|
|
267
|
+
});
|
|
268
|
+
const result = deriveContextualFactors(snapshot);
|
|
269
|
+
expect(result.fileStructureKnown).toBe(true);
|
|
270
|
+
expect(result.errorHistoryPresent).toBe(true);
|
|
271
|
+
expect(result.userGuidanceAvailable).toBe(true);
|
|
272
|
+
expect(result.timePressure).toBe(true);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('fileStructureKnown: true when read precedes write', () => {
|
|
276
|
+
const snapshot = makeSnapshot({
|
|
277
|
+
toolCalls: [
|
|
278
|
+
makeToolCall({ toolName: 'read', createdAt: '2026-04-12T10:00:00.000Z' }),
|
|
279
|
+
makeToolCall({ toolName: 'edit', createdAt: '2026-04-12T10:00:01.000Z' }),
|
|
280
|
+
],
|
|
281
|
+
});
|
|
282
|
+
expect(deriveContextualFactors(snapshot).fileStructureKnown).toBe(true);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('fileStructureKnown: false when write has no preceding read', () => {
|
|
286
|
+
const snapshot = makeSnapshot({
|
|
287
|
+
toolCalls: [
|
|
288
|
+
makeToolCall({ toolName: 'edit', createdAt: '2026-04-12T10:00:00.000Z' }),
|
|
289
|
+
makeToolCall({ toolName: 'read', createdAt: '2026-04-12T10:00:01.000Z' }),
|
|
290
|
+
],
|
|
291
|
+
});
|
|
292
|
+
expect(deriveContextualFactors(snapshot).fileStructureKnown).toBe(false);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('fileStructureKnown: false with only write tools', () => {
|
|
296
|
+
const snapshot = makeSnapshot({
|
|
297
|
+
toolCalls: [makeToolCall({ toolName: 'edit' })],
|
|
298
|
+
});
|
|
299
|
+
expect(deriveContextualFactors(snapshot).fileStructureKnown).toBe(false);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('errorHistoryPresent: true when any tool call failed', () => {
|
|
303
|
+
const snapshot = makeSnapshot({
|
|
304
|
+
toolCalls: [
|
|
305
|
+
makeToolCall({ outcome: 'success' }),
|
|
306
|
+
makeToolCall({ outcome: 'failure' }),
|
|
307
|
+
],
|
|
308
|
+
});
|
|
309
|
+
expect(deriveContextualFactors(snapshot).errorHistoryPresent).toBe(true);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it('errorHistoryPresent: false when all outcomes are success', () => {
|
|
313
|
+
const snapshot = makeSnapshot({
|
|
314
|
+
toolCalls: [makeToolCall({ outcome: 'success' })],
|
|
315
|
+
});
|
|
316
|
+
expect(deriveContextualFactors(snapshot).errorHistoryPresent).toBe(false);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it('userGuidanceAvailable: true when correction detected', () => {
|
|
320
|
+
const snapshot = makeSnapshot({
|
|
321
|
+
userTurns: [makeUserTurn({ correctionDetected: true })],
|
|
322
|
+
});
|
|
323
|
+
expect(deriveContextualFactors(snapshot).userGuidanceAvailable).toBe(true);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('userGuidanceAvailable: false without corrections', () => {
|
|
327
|
+
const snapshot = makeSnapshot({
|
|
328
|
+
userTurns: [makeUserTurn({ correctionDetected: false })],
|
|
329
|
+
});
|
|
330
|
+
expect(deriveContextualFactors(snapshot).userGuidanceAvailable).toBe(false);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it('timePressure: true when majority of gaps < 2 seconds', () => {
|
|
334
|
+
const snapshot = makeSnapshot({
|
|
335
|
+
toolCalls: [
|
|
336
|
+
makeToolCall({ createdAt: '2026-04-12T10:00:00.000Z' }),
|
|
337
|
+
makeToolCall({ createdAt: '2026-04-12T10:00:01.000Z' }),
|
|
338
|
+
makeToolCall({ createdAt: '2026-04-12T10:00:01.500Z' }),
|
|
339
|
+
],
|
|
340
|
+
});
|
|
341
|
+
expect(deriveContextualFactors(snapshot).timePressure).toBe(true);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it('timePressure: false when gaps are large', () => {
|
|
345
|
+
const snapshot = makeSnapshot({
|
|
346
|
+
toolCalls: [
|
|
347
|
+
makeToolCall({ createdAt: '2026-04-12T10:00:00.000Z' }),
|
|
348
|
+
makeToolCall({ createdAt: '2026-04-12T10:00:10.000Z' }),
|
|
349
|
+
makeToolCall({ createdAt: '2026-04-12T10:00:20.000Z' }),
|
|
350
|
+
],
|
|
351
|
+
});
|
|
352
|
+
expect(deriveContextualFactors(snapshot).timePressure).toBe(false);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it('returns all-false defaults for null snapshot', () => {
|
|
356
|
+
expect(deriveContextualFactors(null as any)).toEqual({
|
|
357
|
+
fileStructureKnown: false,
|
|
358
|
+
errorHistoryPresent: false,
|
|
359
|
+
userGuidanceAvailable: false,
|
|
360
|
+
timePressure: false,
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it('returns all-false defaults for empty snapshot', () => {
|
|
365
|
+
expect(deriveContextualFactors(makeSnapshot())).toEqual({
|
|
366
|
+
fileStructureKnown: false,
|
|
367
|
+
errorHistoryPresent: false,
|
|
368
|
+
userGuidanceAvailable: false,
|
|
369
|
+
timePressure: false,
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
});
|