principles-disciple 1.7.6 → 1.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/dist/commands/context.js +5 -15
  2. package/dist/commands/evolution-status.js +2 -9
  3. package/dist/commands/export.js +61 -8
  4. package/dist/commands/nocturnal-review.d.ts +24 -0
  5. package/dist/commands/nocturnal-review.js +265 -0
  6. package/dist/commands/nocturnal-rollout.d.ts +27 -0
  7. package/dist/commands/nocturnal-rollout.js +671 -0
  8. package/dist/commands/nocturnal-train.d.ts +25 -0
  9. package/dist/commands/nocturnal-train.js +919 -0
  10. package/dist/commands/pain.js +8 -21
  11. package/dist/constants/tools.d.ts +2 -2
  12. package/dist/constants/tools.js +1 -1
  13. package/dist/core/adaptive-thresholds.d.ts +186 -0
  14. package/dist/core/adaptive-thresholds.js +300 -0
  15. package/dist/core/config.d.ts +2 -38
  16. package/dist/core/config.js +6 -61
  17. package/dist/core/event-log.d.ts +1 -2
  18. package/dist/core/event-log.js +0 -3
  19. package/dist/core/evolution-engine.js +1 -21
  20. package/dist/core/evolution-reducer.d.ts +7 -1
  21. package/dist/core/evolution-reducer.js +56 -4
  22. package/dist/core/evolution-types.d.ts +61 -9
  23. package/dist/core/evolution-types.js +31 -9
  24. package/dist/core/external-training-contract.d.ts +276 -0
  25. package/dist/core/external-training-contract.js +269 -0
  26. package/dist/core/local-worker-routing.d.ts +175 -0
  27. package/dist/core/local-worker-routing.js +525 -0
  28. package/dist/core/model-deployment-registry.d.ts +218 -0
  29. package/dist/core/model-deployment-registry.js +503 -0
  30. package/dist/core/model-training-registry.d.ts +295 -0
  31. package/dist/core/model-training-registry.js +475 -0
  32. package/dist/core/nocturnal-arbiter.d.ts +159 -0
  33. package/dist/core/nocturnal-arbiter.js +534 -0
  34. package/dist/core/nocturnal-candidate-scoring.d.ts +137 -0
  35. package/dist/core/nocturnal-candidate-scoring.js +266 -0
  36. package/dist/core/nocturnal-compliance.d.ts +175 -0
  37. package/dist/core/nocturnal-compliance.js +824 -0
  38. package/dist/core/nocturnal-dataset.d.ts +224 -0
  39. package/dist/core/nocturnal-dataset.js +443 -0
  40. package/dist/core/nocturnal-executability.d.ts +85 -0
  41. package/dist/core/nocturnal-executability.js +331 -0
  42. package/dist/core/nocturnal-export.d.ts +124 -0
  43. package/dist/core/nocturnal-export.js +275 -0
  44. package/dist/core/nocturnal-paths.d.ts +124 -0
  45. package/dist/core/nocturnal-paths.js +214 -0
  46. package/dist/core/nocturnal-trajectory-extractor.d.ts +242 -0
  47. package/dist/core/nocturnal-trajectory-extractor.js +307 -0
  48. package/dist/core/nocturnal-trinity.d.ts +311 -0
  49. package/dist/core/nocturnal-trinity.js +880 -0
  50. package/dist/core/paths.d.ts +6 -0
  51. package/dist/core/paths.js +6 -0
  52. package/dist/core/principle-training-state.d.ts +121 -0
  53. package/dist/core/principle-training-state.js +321 -0
  54. package/dist/core/promotion-gate.d.ts +238 -0
  55. package/dist/core/promotion-gate.js +529 -0
  56. package/dist/core/session-tracker.d.ts +10 -0
  57. package/dist/core/session-tracker.js +14 -0
  58. package/dist/core/shadow-observation-registry.d.ts +217 -0
  59. package/dist/core/shadow-observation-registry.js +308 -0
  60. package/dist/core/training-program.d.ts +233 -0
  61. package/dist/core/training-program.js +433 -0
  62. package/dist/core/trajectory.d.ts +95 -1
  63. package/dist/core/trajectory.js +220 -6
  64. package/dist/core/workspace-context.d.ts +0 -6
  65. package/dist/core/workspace-context.js +0 -12
  66. package/dist/hooks/bash-risk.d.ts +6 -6
  67. package/dist/hooks/bash-risk.js +8 -8
  68. package/dist/hooks/gate-block-helper.js +1 -1
  69. package/dist/hooks/gate.d.ts +1 -1
  70. package/dist/hooks/gate.js +2 -2
  71. package/dist/hooks/gfi-gate.d.ts +3 -3
  72. package/dist/hooks/gfi-gate.js +15 -14
  73. package/dist/hooks/pain.js +6 -9
  74. package/dist/hooks/progressive-trust-gate.d.ts +21 -49
  75. package/dist/hooks/progressive-trust-gate.js +51 -204
  76. package/dist/hooks/prompt.d.ts +11 -11
  77. package/dist/hooks/prompt.js +158 -72
  78. package/dist/hooks/subagent.js +43 -6
  79. package/dist/i18n/commands.js +8 -8
  80. package/dist/index.js +129 -28
  81. package/dist/service/evolution-worker.d.ts +42 -4
  82. package/dist/service/evolution-worker.js +321 -13
  83. package/dist/service/nocturnal-runtime.d.ts +183 -0
  84. package/dist/service/nocturnal-runtime.js +352 -0
  85. package/dist/service/nocturnal-service.d.ts +163 -0
  86. package/dist/service/nocturnal-service.js +787 -0
  87. package/dist/service/nocturnal-target-selector.d.ts +145 -0
  88. package/dist/service/nocturnal-target-selector.js +315 -0
  89. package/dist/service/phase3-input-filter.d.ts +2 -23
  90. package/dist/service/phase3-input-filter.js +3 -27
  91. package/dist/service/runtime-summary-service.d.ts +0 -10
  92. package/dist/service/runtime-summary-service.js +1 -54
  93. package/dist/tools/deep-reflect.js +2 -1
  94. package/dist/types/event-types.d.ts +2 -10
  95. package/dist/types/runtime-summary.d.ts +1 -8
  96. package/dist/types.d.ts +0 -3
  97. package/dist/types.js +0 -2
  98. package/openclaw.plugin.json +1 -1
  99. package/package.json +1 -1
  100. package/templates/langs/en/skills/pd-mentor/SKILL.md +5 -5
  101. package/templates/langs/zh/skills/pd-mentor/SKILL.md +5 -5
  102. package/templates/pain_settings.json +0 -6
  103. package/dist/commands/trust.d.ts +0 -4
  104. package/dist/commands/trust.js +0 -78
  105. package/dist/core/trust-engine.d.ts +0 -96
  106. package/dist/core/trust-engine.js +0 -286
@@ -0,0 +1,242 @@
1
+ /**
2
+ * Nocturnal Trajectory Extractor — Structured Session Snapshot API
3
+ * ==============================================================
4
+ *
5
+ * PURPOSE: Provide minimal necessary structured trajectory extraction
6
+ * for the nocturnal reflection pipeline. NOT a general-purpose data mirror.
7
+ *
8
+ * DESIGN PRINCIPLES:
9
+ * - Uses sanitized text ONLY — never raw_text or blob payloads
10
+ * - Two distinct query paths:
11
+ * 1. Analytics query (listRecentNocturnalCandidateSessions) — for target selection
12
+ * 2. Runtime query (getNocturnalSessionSnapshot) — for sample generation
13
+ * - All snapshots are self-contained and principle-relevant metadata-rich
14
+ *
15
+ * WHAT THIS MODULE DOES:
16
+ * - List recent sessions with metadata relevant to nocturnal target selection
17
+ * - Extract structured session snapshots for a selected violating session
18
+ *
19
+ * WHAT THIS MODULE DOES NOT DO:
20
+ * - NO snapshot database cloning
21
+ * - NO full trajectory export
22
+ * - NO raw text exposure
23
+ * - NO target selection logic
24
+ * - NO sample generation
25
+ *
26
+ * ARTIFACT OUTPUTS go to:
27
+ * .state/nocturnal/samples/ ← structured JSON artifacts
28
+ *
29
+ * FILE: {stateDir}/nocturnal/snapshots/ (cached snapshots if needed, optional)
30
+ */
31
+ import { TrajectoryDatabase } from './trajectory.js';
32
+ /**
33
+ * Minimal sanitized assistant turn for nocturnal snapshot.
34
+ * Contains ONLY sanitizedText — raw_text is never exposed.
35
+ */
36
+ export interface NocturnalAssistantTurn {
37
+ turnIndex: number;
38
+ sanitizedText: string;
39
+ model: string;
40
+ createdAt: string;
41
+ }
42
+ /**
43
+ * Minimal sanitized user turn for nocturnal snapshot.
44
+ * Contains only derived cues — NO raw user text.
45
+ */
46
+ export interface NocturnalUserTurn {
47
+ turnIndex: number;
48
+ correctionDetected: boolean;
49
+ correctionCue: string | null;
50
+ createdAt: string;
51
+ }
52
+ /**
53
+ * Tool call event for nocturnal snapshot.
54
+ */
55
+ export interface NocturnalToolCall {
56
+ toolName: string;
57
+ outcome: 'success' | 'failure' | 'blocked';
58
+ filePath: string | null;
59
+ durationMs: number | null;
60
+ exitCode: number | null;
61
+ errorType: string | null;
62
+ errorMessage: string | null;
63
+ createdAt: string;
64
+ }
65
+ /**
66
+ * Pain signal for nocturnal snapshot.
67
+ */
68
+ export interface NocturnalPainEvent {
69
+ source: string;
70
+ score: number;
71
+ severity: string | null;
72
+ reason: string | null;
73
+ createdAt: string;
74
+ }
75
+ /**
76
+ * Gate block event for nocturnal snapshot.
77
+ */
78
+ export interface NocturnalGateBlock {
79
+ toolName: string;
80
+ filePath: string | null;
81
+ reason: string;
82
+ planStatus: string | null;
83
+ createdAt: string;
84
+ }
85
+ /**
86
+ * A structured nocturnal session snapshot.
87
+ * Contains all information needed for a reflector to generate decision-point samples.
88
+ *
89
+ * GUARANTEES:
90
+ * - NO raw_text exposed
91
+ * - NO blob references resolved
92
+ * - All text is sanitized or derived-cue only
93
+ * - Self-contained (principle-relevant metadata included)
94
+ */
95
+ export interface NocturnalSessionSnapshot {
96
+ sessionId: string;
97
+ startedAt: string;
98
+ updatedAt: string;
99
+ assistantTurns: NocturnalAssistantTurn[];
100
+ userTurns: NocturnalUserTurn[];
101
+ toolCalls: NocturnalToolCall[];
102
+ painEvents: NocturnalPainEvent[];
103
+ gateBlocks: NocturnalGateBlock[];
104
+ /**
105
+ * Summary statistics for quick triage.
106
+ */
107
+ stats: {
108
+ totalAssistantTurns: number;
109
+ totalToolCalls: number;
110
+ totalPainEvents: number;
111
+ totalGateBlocks: number;
112
+ failureCount: number;
113
+ };
114
+ }
115
+ /**
116
+ * Summary entry for session listing (used by nocturnal target selector).
117
+ * Lightweight — only identification and basic metadata, no turns.
118
+ */
119
+ export interface NocturnalSessionSummary {
120
+ sessionId: string;
121
+ startedAt: string;
122
+ updatedAt: string;
123
+ /** Number of assistant turns (for relevance scoring) */
124
+ assistantTurnCount: number;
125
+ /** Number of tool calls (for violation signal density) */
126
+ toolCallCount: number;
127
+ /** Number of pain events (for pain signal density) */
128
+ painEventCount: number;
129
+ /** Number of gate blocks (for constraint violation evidence) */
130
+ gateBlockCount: number;
131
+ /** Number of failed tool calls (for violation signal) */
132
+ failureCount: number;
133
+ }
134
+ /**
135
+ * Options for listing recent nocturnal candidate sessions.
136
+ */
137
+ export interface ListNocturnalSessionsOptions {
138
+ /** Maximum number of sessions to return (default: 20) */
139
+ limit?: number;
140
+ /** Only return sessions updated after this date */
141
+ dateFrom?: string;
142
+ /** Only return sessions updated before this date */
143
+ dateTo?: string;
144
+ /** Minimum tool call count threshold (default: 1) */
145
+ minToolCalls?: number;
146
+ }
147
+ /**
148
+ * Nocturnal Trajectory Extractor.
149
+ *
150
+ * Provides sanitized, structured access to session data for the nocturnal
151
+ * reflection pipeline. All queries return sanitized text only.
152
+ *
153
+ * This class is a thin, focused wrapper around TrajectoryDatabase.
154
+ * It does NOT cache snapshots or maintain its own state.
155
+ */
156
+ export declare class NocturnalTrajectoryExtractor {
157
+ private readonly trajectory;
158
+ constructor(trajectory: TrajectoryDatabase);
159
+ /**
160
+ * List recent sessions suitable for nocturnal target selection.
161
+ *
162
+ * ANALYTICS QUERY — used by nocturnal target selector to find candidate sessions.
163
+ *
164
+ * @param options - Query options
165
+ * @returns Lightweight session summaries ordered by most recently updated
166
+ */
167
+ listRecentNocturnalCandidateSessions(options?: ListNocturnalSessionsOptions): NocturnalSessionSummary[];
168
+ /**
169
+ * Get a full structured snapshot for a specific session.
170
+ *
171
+ * RUNTIME QUERY — used by nocturnal service after target selection.
172
+ *
173
+ * SECURITY GUARANTEES:
174
+ * - Only sanitizedText from assistant turns (never raw_text)
175
+ * - Only correction cues from user turns (never raw user text)
176
+ * - Tool calls with outcome and error info (no raw parameters)
177
+ * - Pain events with score and reason (no raw event data)
178
+ * - Gate blocks with tool/reason info (no file content)
179
+ *
180
+ * @param sessionId - Session ID to snapshot
181
+ * @returns Full structured snapshot, or null if session not found
182
+ */
183
+ getNocturnalSessionSnapshot(sessionId: string): NocturnalSessionSnapshot | null;
184
+ }
185
+ /**
186
+ * Creates a NocturnalTrajectoryExtractor from a workspace directory.
187
+ *
188
+ * USAGE:
189
+ * const extractor = createNocturnalTrajectoryExtractor(workspaceDir);
190
+ * const sessions = extractor.listRecentNocturnalCandidateSessions({ limit: 10 });
191
+ * const snapshot = extractor.getNocturnalSessionSnapshot(sessionId);
192
+ */
193
+ export declare function createNocturnalTrajectoryExtractor(workspaceDir: string, _stateDir?: string): NocturnalTrajectoryExtractor;
194
+ /**
195
+ * List recent sessions for nocturnal target selection.
196
+ * Convenience wrapper around NocturnalTrajectoryExtractor.
197
+ */
198
+ export declare function listNocturnalCandidateSessions(trajectory: TrajectoryDatabase, options?: ListNocturnalSessionsOptions): NocturnalSessionSummary[];
199
+ /**
200
+ * Get a session snapshot for nocturnal reflection.
201
+ * Convenience wrapper around NocturnalTrajectoryExtractor.
202
+ */
203
+ export declare function getNocturnalSessionSnapshot(trajectory: TrajectoryDatabase, sessionId: string): NocturnalSessionSnapshot | null;
204
+ /**
205
+ * Compute thinking model activation for a text.
206
+ * Returns 0-1 ratio of matched thinking models to total available models.
207
+ *
208
+ * @param text - Text to analyze
209
+ * @returns Activation ratio (0-1)
210
+ */
211
+ export declare function computeThinkingModelActivation(text: string): number;
212
+ /**
213
+ * Compute planning ratio from a session snapshot.
214
+ * Planning ratio = write operations preceded immediately by a read tool / total write operations.
215
+ * A higher ratio indicates more careful planning behavior (reading before writing).
216
+ *
217
+ * Only the immediately preceding tool is checked — each write needs its own
218
+ * preceding read to count as planned. This prevents a single read from satisfying
219
+ * multiple writes in sequence.
220
+ *
221
+ * @param snapshot - Session snapshot to analyze
222
+ * @returns Planning ratio (0-1), or 0 if no write operations
223
+ */
224
+ export declare function computePlanningRatio(snapshot: NocturnalSessionSnapshot): number;
225
+ /**
226
+ * Compute thinking model delta between original and improved decisions.
227
+ * Positive delta means the improved decision uses more thinking models.
228
+ *
229
+ * @param originalText - Original (bad) decision text
230
+ * @param improvedText - Improved (better) decision text
231
+ * @returns Delta in thinking model activation (-1 to 1)
232
+ */
233
+ export declare function computeThinkingModelDelta(originalText: string, improvedText: string): number;
234
+ /**
235
+ * Compute planning ratio gain between original and improved snapshots.
236
+ * Positive gain means the improved behavior has better planning (more reads before writes).
237
+ *
238
+ * @param originalSnapshot - Original session snapshot
239
+ * @param improvedSnapshot - Improved session snapshot
240
+ * @returns Planning ratio gain (-1 to 1)
241
+ */
242
+ export declare function computePlanningRatioGain(originalSnapshot: NocturnalSessionSnapshot, improvedSnapshot: NocturnalSessionSnapshot): number;
@@ -0,0 +1,307 @@
1
+ /**
2
+ * Nocturnal Trajectory Extractor — Structured Session Snapshot API
3
+ * ==============================================================
4
+ *
5
+ * PURPOSE: Provide minimal necessary structured trajectory extraction
6
+ * for the nocturnal reflection pipeline. NOT a general-purpose data mirror.
7
+ *
8
+ * DESIGN PRINCIPLES:
9
+ * - Uses sanitized text ONLY — never raw_text or blob payloads
10
+ * - Two distinct query paths:
11
+ * 1. Analytics query (listRecentNocturnalCandidateSessions) — for target selection
12
+ * 2. Runtime query (getNocturnalSessionSnapshot) — for sample generation
13
+ * - All snapshots are self-contained and principle-relevant metadata-rich
14
+ *
15
+ * WHAT THIS MODULE DOES:
16
+ * - List recent sessions with metadata relevant to nocturnal target selection
17
+ * - Extract structured session snapshots for a selected violating session
18
+ *
19
+ * WHAT THIS MODULE DOES NOT DO:
20
+ * - NO snapshot database cloning
21
+ * - NO full trajectory export
22
+ * - NO raw text exposure
23
+ * - NO target selection logic
24
+ * - NO sample generation
25
+ *
26
+ * ARTIFACT OUTPUTS go to:
27
+ * .state/nocturnal/samples/ ← structured JSON artifacts
28
+ *
29
+ * FILE: {stateDir}/nocturnal/snapshots/ (cached snapshots if needed, optional)
30
+ */
31
+ import { TrajectoryRegistry } from './trajectory.js';
32
+ import { detectThinkingModelMatches, listThinkingModels } from './thinking-models.js';
33
+ // ---------------------------------------------------------------------------
34
+ // Core Extractor
35
+ // ---------------------------------------------------------------------------
36
+ /**
37
+ * Nocturnal Trajectory Extractor.
38
+ *
39
+ * Provides sanitized, structured access to session data for the nocturnal
40
+ * reflection pipeline. All queries return sanitized text only.
41
+ *
42
+ * This class is a thin, focused wrapper around TrajectoryDatabase.
43
+ * It does NOT cache snapshots or maintain its own state.
44
+ */
45
+ export class NocturnalTrajectoryExtractor {
46
+ trajectory;
47
+ constructor(trajectory) {
48
+ this.trajectory = trajectory;
49
+ }
50
+ /**
51
+ * List recent sessions suitable for nocturnal target selection.
52
+ *
53
+ * ANALYTICS QUERY — used by nocturnal target selector to find candidate sessions.
54
+ *
55
+ * @param options - Query options
56
+ * @returns Lightweight session summaries ordered by most recently updated
57
+ */
58
+ listRecentNocturnalCandidateSessions(options = {}) {
59
+ const limit = options.limit ?? 20;
60
+ const minToolCalls = options.minToolCalls ?? 1;
61
+ // Get recent sessions from trajectory DB
62
+ const sessions = this.trajectory.listRecentSessions({
63
+ limit: limit * 3, // Over-fetch to allow filtering
64
+ dateFrom: options.dateFrom,
65
+ dateTo: options.dateTo,
66
+ });
67
+ if (sessions.length === 0) {
68
+ return [];
69
+ }
70
+ // For each session, get counts
71
+ // We batch these by fetching tool_calls count per session
72
+ const summaries = [];
73
+ for (const session of sessions) {
74
+ if (summaries.length >= limit)
75
+ break;
76
+ const toolCalls = this.trajectory.listToolCallsForSession(session.sessionId);
77
+ const painEvents = this.trajectory.listPainEventsForSession(session.sessionId);
78
+ const gateBlocks = this.trajectory.listGateBlocksForSession(session.sessionId);
79
+ // Filter by minimum tool calls threshold
80
+ if (toolCalls.length < minToolCalls) {
81
+ continue;
82
+ }
83
+ const failureCount = toolCalls.filter((tc) => tc.outcome === 'failure').length;
84
+ summaries.push({
85
+ sessionId: session.sessionId,
86
+ startedAt: session.startedAt,
87
+ updatedAt: session.updatedAt,
88
+ assistantTurnCount: 0, // Not readily available without extra query
89
+ toolCallCount: toolCalls.length,
90
+ painEventCount: painEvents.length,
91
+ gateBlockCount: gateBlocks.length,
92
+ failureCount,
93
+ });
94
+ }
95
+ return summaries;
96
+ }
97
+ /**
98
+ * Get a full structured snapshot for a specific session.
99
+ *
100
+ * RUNTIME QUERY — used by nocturnal service after target selection.
101
+ *
102
+ * SECURITY GUARANTEES:
103
+ * - Only sanitizedText from assistant turns (never raw_text)
104
+ * - Only correction cues from user turns (never raw user text)
105
+ * - Tool calls with outcome and error info (no raw parameters)
106
+ * - Pain events with score and reason (no raw event data)
107
+ * - Gate blocks with tool/reason info (no file content)
108
+ *
109
+ * @param sessionId - Session ID to snapshot
110
+ * @returns Full structured snapshot, or null if session not found
111
+ */
112
+ getNocturnalSessionSnapshot(sessionId) {
113
+ // Verify session exists — must use a large enough limit to cover all candidates,
114
+ // not just the single most-recent session (which would cause false negatives when
115
+ // the selector targets an older session).
116
+ const sessions = this.trajectory.listRecentSessions({ limit: 1000 });
117
+ const sessionExists = sessions.some((s) => s.sessionId === sessionId);
118
+ if (!sessionExists) {
119
+ // Session might not be in trajectory DB — try to get basic info
120
+ // If no data at all, return null for fail-safe
121
+ return null;
122
+ }
123
+ // Fetch all turn data
124
+ const assistantTurns = this.trajectory.listAssistantTurns(sessionId);
125
+ const userTurns = this.trajectory.listUserTurnsForSession(sessionId);
126
+ const toolCalls = this.trajectory.listToolCallsForSession(sessionId);
127
+ const painEvents = this.trajectory.listPainEventsForSession(sessionId);
128
+ const gateBlocks = this.trajectory.listGateBlocksForSession(sessionId);
129
+ // Map to sanitized structures
130
+ // SECURITY: Only sanitizedText from assistant turns
131
+ const sanitizedAssistantTurns = assistantTurns.map((turn, index) => ({
132
+ turnIndex: index,
133
+ sanitizedText: turn.sanitizedText,
134
+ model: turn.model,
135
+ createdAt: turn.createdAt,
136
+ }));
137
+ // SECURITY: Only derived cues from user turns
138
+ const sanitizedUserTurns = userTurns.map((turn) => ({
139
+ turnIndex: turn.turnIndex,
140
+ correctionDetected: turn.correctionDetected,
141
+ correctionCue: turn.correctionCue,
142
+ createdAt: turn.createdAt,
143
+ }));
144
+ // Tool calls — include outcome and error info but not raw params
145
+ const nocturnalToolCalls = toolCalls.map((tc) => ({
146
+ toolName: tc.toolName,
147
+ outcome: tc.outcome,
148
+ filePath: tc.filePath,
149
+ durationMs: tc.durationMs,
150
+ exitCode: tc.exitCode,
151
+ errorType: tc.errorType,
152
+ errorMessage: tc.errorMessage,
153
+ createdAt: tc.createdAt,
154
+ }));
155
+ // Pain events — score and reason only
156
+ const nocturnalPainEvents = painEvents.map((pe) => ({
157
+ source: pe.source,
158
+ score: pe.score,
159
+ severity: pe.severity,
160
+ reason: pe.reason,
161
+ createdAt: pe.createdAt,
162
+ }));
163
+ // Gate blocks — tool and reason only
164
+ const nocturnalGateBlocks = gateBlocks.map((gb) => ({
165
+ toolName: gb.toolName,
166
+ filePath: gb.filePath,
167
+ reason: gb.reason,
168
+ planStatus: gb.planStatus,
169
+ createdAt: gb.createdAt,
170
+ }));
171
+ // Compute summary stats
172
+ const failureCount = toolCalls.filter((tc) => tc.outcome === 'failure').length;
173
+ // Get session metadata (use trajectory data)
174
+ const sessionMeta = sessions.find((s) => s.sessionId === sessionId);
175
+ return {
176
+ sessionId,
177
+ startedAt: sessionMeta?.startedAt ?? new Date(0).toISOString(),
178
+ updatedAt: sessionMeta?.updatedAt ?? new Date(0).toISOString(),
179
+ assistantTurns: sanitizedAssistantTurns,
180
+ userTurns: sanitizedUserTurns,
181
+ toolCalls: nocturnalToolCalls,
182
+ painEvents: nocturnalPainEvents,
183
+ gateBlocks: nocturnalGateBlocks,
184
+ stats: {
185
+ totalAssistantTurns: sanitizedAssistantTurns.length,
186
+ totalToolCalls: nocturnalToolCalls.length,
187
+ totalPainEvents: nocturnalPainEvents.length,
188
+ totalGateBlocks: nocturnalGateBlocks.length,
189
+ failureCount,
190
+ },
191
+ };
192
+ }
193
+ }
194
+ // ---------------------------------------------------------------------------
195
+ // Factory
196
+ // ---------------------------------------------------------------------------
197
+ /**
198
+ * Creates a NocturnalTrajectoryExtractor from a workspace directory.
199
+ *
200
+ * USAGE:
201
+ * const extractor = createNocturnalTrajectoryExtractor(workspaceDir);
202
+ * const sessions = extractor.listRecentNocturnalCandidateSessions({ limit: 10 });
203
+ * const snapshot = extractor.getNocturnalSessionSnapshot(sessionId);
204
+ */
205
+ export function createNocturnalTrajectoryExtractor(workspaceDir, _stateDir // Ignored; kept for API compatibility with existing call sites
206
+ ) {
207
+ // Use the registry to get or create the TrajectoryDatabase instance
208
+ const trajectory = TrajectoryRegistry.get(workspaceDir);
209
+ return new NocturnalTrajectoryExtractor(trajectory);
210
+ }
211
+ // ---------------------------------------------------------------------------
212
+ // Direct module helpers (for cases where you already have TrajectoryDatabase)
213
+ // ---------------------------------------------------------------------------
214
+ /**
215
+ * List recent sessions for nocturnal target selection.
216
+ * Convenience wrapper around NocturnalTrajectoryExtractor.
217
+ */
218
+ export function listNocturnalCandidateSessions(trajectory, options = {}) {
219
+ return new NocturnalTrajectoryExtractor(trajectory).listRecentNocturnalCandidateSessions(options);
220
+ }
221
+ /**
222
+ * Get a session snapshot for nocturnal reflection.
223
+ * Convenience wrapper around NocturnalTrajectoryExtractor.
224
+ */
225
+ export function getNocturnalSessionSnapshot(trajectory, sessionId) {
226
+ return new NocturnalTrajectoryExtractor(trajectory).getNocturnalSessionSnapshot(sessionId);
227
+ }
228
+ // ---------------------------------------------------------------------------
229
+ // Reflection Quality Metrics
230
+ // ---------------------------------------------------------------------------
231
+ /**
232
+ * Compute thinking model activation for a text.
233
+ * Returns 0-1 ratio of matched thinking models to total available models.
234
+ *
235
+ * @param text - Text to analyze
236
+ * @returns Activation ratio (0-1)
237
+ */
238
+ export function computeThinkingModelActivation(text) {
239
+ if (!text || text.trim().length === 0)
240
+ return 0;
241
+ const matches = detectThinkingModelMatches(text);
242
+ const totalModels = listThinkingModels().length;
243
+ return Math.round((matches.length / totalModels) * 100) / 100;
244
+ }
245
+ /**
246
+ * Compute planning ratio from a session snapshot.
247
+ * Planning ratio = write operations preceded immediately by a read tool / total write operations.
248
+ * A higher ratio indicates more careful planning behavior (reading before writing).
249
+ *
250
+ * Only the immediately preceding tool is checked — each write needs its own
251
+ * preceding read to count as planned. This prevents a single read from satisfying
252
+ * multiple writes in sequence.
253
+ *
254
+ * @param snapshot - Session snapshot to analyze
255
+ * @returns Planning ratio (0-1), or 0 if no write operations
256
+ */
257
+ export function computePlanningRatio(snapshot) {
258
+ const toolCalls = snapshot.toolCalls;
259
+ let totalWrites = 0;
260
+ let writesWithPrecedingRead = 0;
261
+ for (let i = 0; i < toolCalls.length; i++) {
262
+ const tc = toolCalls[i];
263
+ const isWriteTool = /^(edit|write|create|delete|remove|move|rename)/i.test(tc.toolName);
264
+ if (isWriteTool) {
265
+ totalWrites++;
266
+ // Check only the immediately preceding tool
267
+ if (i > 0) {
268
+ const prevTc = toolCalls[i - 1];
269
+ const isReadTool = /^(read|grep|search|find|inspect|look)/i.test(prevTc.toolName);
270
+ if (isReadTool) {
271
+ writesWithPrecedingRead++;
272
+ }
273
+ }
274
+ }
275
+ }
276
+ if (totalWrites === 0)
277
+ return 0;
278
+ return Math.round((writesWithPrecedingRead / totalWrites) * 100) / 100;
279
+ }
280
+ /**
281
+ * Compute thinking model delta between original and improved decisions.
282
+ * Positive delta means the improved decision uses more thinking models.
283
+ *
284
+ * @param originalText - Original (bad) decision text
285
+ * @param improvedText - Improved (better) decision text
286
+ * @returns Delta in thinking model activation (-1 to 1)
287
+ */
288
+ export function computeThinkingModelDelta(originalText, improvedText) {
289
+ const originalActivation = computeThinkingModelActivation(originalText);
290
+ const improvedActivation = computeThinkingModelActivation(improvedText);
291
+ const delta = improvedActivation - originalActivation;
292
+ return Math.round(delta * 100) / 100;
293
+ }
294
+ /**
295
+ * Compute planning ratio gain between original and improved snapshots.
296
+ * Positive gain means the improved behavior has better planning (more reads before writes).
297
+ *
298
+ * @param originalSnapshot - Original session snapshot
299
+ * @param improvedSnapshot - Improved session snapshot
300
+ * @returns Planning ratio gain (-1 to 1)
301
+ */
302
+ export function computePlanningRatioGain(originalSnapshot, improvedSnapshot) {
303
+ const originalRatio = computePlanningRatio(originalSnapshot);
304
+ const improvedRatio = computePlanningRatio(improvedSnapshot);
305
+ const gain = improvedRatio - originalRatio;
306
+ return Math.round(gain * 100) / 100;
307
+ }