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.
@@ -0,0 +1,337 @@
1
+ /**
2
+ * Nocturnal Reasoning Deriver — Runtime Reasoning Signal Extraction
3
+ * ==============================================================
4
+ *
5
+ * PURPOSE: Derive structured reasoning signals from existing snapshot data
6
+ * without any snapshot schema changes. Pure functions, zero dependencies.
7
+ *
8
+ * THREE FUNCTIONS:
9
+ * - deriveReasoningChain: Extract thinking content, uncertainty, confidence from assistant turns
10
+ * - deriveDecisionPoints: Extract before/after context per tool call (Plan 02)
11
+ * - deriveContextualFactors: Compute contextual factors from snapshot (Plan 02)
12
+ */
13
+
14
+ import type { NocturnalAssistantTurn, NocturnalToolCall, NocturnalUserTurn, NocturnalSessionSnapshot } from './nocturnal-trajectory-extractor.js';
15
+ import { detectThinkingModelMatches, listThinkingModels } from './thinking-models.js';
16
+
17
+ // ---------------------------------------------------------------------------
18
+ // Shared helpers
19
+ // ---------------------------------------------------------------------------
20
+
21
+ /** Parse an ISO 8601 timestamp, returning NaN for invalid formats. */
22
+ function parseTs(ts: string): number {
23
+ // ISO 8601 strings without Z suffix or offset are treated as local time.
24
+ // Log a warning for ambiguous formats (missing timezone indicator).
25
+ if (
26
+ typeof ts === 'string' &&
27
+ !ts.endsWith('Z') &&
28
+ !ts.includes('+') &&
29
+ ts.includes('-', 4)
30
+ ) {
31
+ // Looks like an ISO date but no timezone — could be ambiguous
32
+ const bare = ts.slice(0, 10);
33
+ if (/^\d{4}-\d{2}-\d{2}$/.test(bare)) {
34
+ // Plain YYYY-MM-DD without time or Z — definitely ambiguous
35
+ console.warn(`[Deriver] Timestamp missing timezone: "${ts}"`);
36
+ }
37
+ }
38
+ return Date.parse(ts);
39
+ }
40
+
41
+ // ---------------------------------------------------------------------------
42
+ // Shared types (used across all three derive functions)
43
+ // ---------------------------------------------------------------------------
44
+
45
+ export interface DerivedReasoningSignal {
46
+ turnIndex: number;
47
+ thinkingContent: string;
48
+ uncertaintyMarkers: string[];
49
+ confidenceSignal: "high" | "medium" | "low";
50
+ }
51
+
52
+ export interface DerivedDecisionPoint {
53
+ toolName: string;
54
+ outcome: "success" | "failure" | "blocked";
55
+ beforeContext: string;
56
+ afterReflection?: string;
57
+ confidenceDelta?: number;
58
+ }
59
+
60
+ export interface DerivedContextualFactors {
61
+ fileStructureKnown: boolean;
62
+ errorHistoryPresent: boolean;
63
+ userGuidanceAvailable: boolean;
64
+ timePressure: boolean;
65
+ }
66
+
67
+ // ---------------------------------------------------------------------------
68
+ // Constants
69
+ // ---------------------------------------------------------------------------
70
+
71
+ const UNCERTAINTY_PATTERNS: RegExp[] = [
72
+ /let me (check|verify|confirm|understand)/gi,
73
+ /I should (first|probably|maybe)/gi,
74
+ /not sure (if|whether|about)/gi,
75
+ ];
76
+
77
+ const THINKING_TAG_REGEX = /<thinking>([\s\S]*?)<\/thinking>/g;
78
+
79
+ // ---------------------------------------------------------------------------
80
+ // Helpers
81
+ // ---------------------------------------------------------------------------
82
+
83
+ /**
84
+ * Compute thinking model activation ratio for text.
85
+ * Uses detectThinkingModelMatches() from thinking-models.ts.
86
+ * Returns 0-1, rounded to 2 decimal places.
87
+ */
88
+ function computeThinkingModelActivation(text: string): number {
89
+ if (!text || text.trim().length === 0) return 0;
90
+ const matches = detectThinkingModelMatches(text);
91
+ const totalModels = listThinkingModels().length;
92
+ if (totalModels === 0) return 0;
93
+ return Math.round((matches.length / totalModels) * 100) / 100;
94
+ }
95
+
96
+ /**
97
+ * Map activation ratio (0-1) to confidence signal.
98
+ * Thresholds: high > 0.6, medium 0.3-0.6, low < 0.3
99
+ */
100
+ function mapConfidenceSignal(activation: number): "high" | "medium" | "low" {
101
+ if (activation > 0.6) return "high";
102
+ if (activation >= 0.3) return "medium";
103
+ return "low";
104
+ }
105
+
106
+ // ---------------------------------------------------------------------------
107
+ // deriveReasoningChain (DERIV-01)
108
+ // ---------------------------------------------------------------------------
109
+
110
+ /**
111
+ * Extract thinking content, uncertainty markers, and confidence signal
112
+ * from each assistant turn in the snapshot.
113
+ *
114
+ * DERIV-01: Returns one DerivedReasoningSignal per assistant turn.
115
+ * Empty input returns empty array. Never throws.
116
+ */
117
+ export function deriveReasoningChain(assistantTurns: NocturnalAssistantTurn[]): DerivedReasoningSignal[] {
118
+ if (!assistantTurns || assistantTurns.length === 0) return [];
119
+
120
+ return assistantTurns.map(turn => {
121
+ const text = turn.sanitizedText ?? '';
122
+
123
+ // Extract all <thinking> content blocks (multiple blocks per turn possible)
124
+ const thinkingMatches = [...text.matchAll(THINKING_TAG_REGEX)];
125
+ const thinkingContent = thinkingMatches.map(m => m[1].trim()).join('\n');
126
+
127
+ // Detect uncertainty markers (collect all unique matches across 3 patterns)
128
+ const uncertaintyMarkers: string[] = [];
129
+ for (const pattern of UNCERTAINTY_PATTERNS) {
130
+ // Reset lastIndex to avoid g-flag state issues
131
+ pattern.lastIndex = 0;
132
+ const matches = text.match(pattern);
133
+ if (matches) {
134
+ for (const m of matches) {
135
+ if (!uncertaintyMarkers.includes(m)) {
136
+ uncertaintyMarkers.push(m);
137
+ }
138
+ }
139
+ }
140
+ }
141
+
142
+ // Confidence signal: only meaningful when <thinking> content exists.
143
+ // Without thinking tags we cannot extract a genuine reasoning trace, so
144
+ // we fall back to 'low' rather than misleading the downstream pipeline
145
+ // with activation derived from non-thinking patterns in the response text.
146
+ let confidenceSignal: "high" | "medium" | "low";
147
+ if (thinkingContent.length === 0) {
148
+ confidenceSignal = 'low';
149
+ } else {
150
+ const activation = computeThinkingModelActivation(text);
151
+ confidenceSignal = mapConfidenceSignal(activation);
152
+ }
153
+
154
+ return {
155
+ turnIndex: turn.turnIndex,
156
+ thinkingContent,
157
+ uncertaintyMarkers,
158
+ confidenceSignal,
159
+ };
160
+ });
161
+ }
162
+
163
+ // ---------------------------------------------------------------------------
164
+ // Helpers (Plan 02)
165
+ // ---------------------------------------------------------------------------
166
+
167
+ /**
168
+ * Convert confidence signal to numeric value for delta computation.
169
+ * high=1, medium=0.5, low=0
170
+ */
171
+ function confidenceToNumber(signal: "high" | "medium" | "low"): number {
172
+ switch (signal) {
173
+ case "high": return 1;
174
+ case "medium": return 0.5;
175
+ case "low": return 0;
176
+ }
177
+ }
178
+
179
+ // ---------------------------------------------------------------------------
180
+ // deriveDecisionPoints (DERIV-02)
181
+ // ---------------------------------------------------------------------------
182
+
183
+ /**
184
+ * Extract before-context and after-reflection for each tool call.
185
+ *
186
+ * DERIV-02: For each tool call, find the assistant turn immediately before it
187
+ * (by createdAt timestamp) and extract last 500 chars as beforeContext.
188
+ * On failure outcome, find the next assistant turn and extract first 300 chars
189
+ * as afterReflection. Compute confidence delta between before/after.
190
+ *
191
+ * Empty inputs return empty array. Never throws.
192
+ */
193
+ export function deriveDecisionPoints(
194
+ assistantTurns: NocturnalAssistantTurn[],
195
+ toolCalls: NocturnalToolCall[],
196
+ ): DerivedDecisionPoint[] {
197
+ if (!toolCalls || toolCalls.length === 0) return [];
198
+ if (!assistantTurns || assistantTurns.length === 0) {
199
+ // Return decision points with empty beforeContext when no assistant turns
200
+ return toolCalls.map(tc => ({
201
+ toolName: tc.toolName,
202
+ outcome: tc.outcome,
203
+ beforeContext: '',
204
+ }));
205
+ }
206
+
207
+ // Sort assistant turns by createdAt for binary search
208
+ const sortedTurns = [...assistantTurns].sort(
209
+ (a, b) => parseTs(a.createdAt) - parseTs(b.createdAt)
210
+ );
211
+
212
+ // Binary search: find rightmost assistant turn with createdAt < tcTime
213
+ const findBeforeTurn = (tcTime: number): NocturnalAssistantTurn | undefined => {
214
+ let lo = 0, hi = sortedTurns.length - 1, result: NocturnalAssistantTurn | undefined;
215
+ while (lo <= hi) {
216
+ const mid = (lo + hi) >>> 1;
217
+ if (parseTs(sortedTurns[mid].createdAt) < tcTime) {
218
+ result = sortedTurns[mid];
219
+ lo = mid + 1;
220
+ } else {
221
+ hi = mid - 1;
222
+ }
223
+ }
224
+ return result;
225
+ };
226
+
227
+ return toolCalls.map(tc => {
228
+ const tcTime = parseTs(tc.createdAt);
229
+ const beforeTurn = findBeforeTurn(tcTime);
230
+
231
+ const beforeContext = beforeTurn
232
+ ? beforeTurn.sanitizedText.slice(-500)
233
+ : '';
234
+
235
+ // On failure, find next assistant turn after tool call
236
+ let afterReflection: string | undefined;
237
+ let confidenceDelta: number | undefined;
238
+
239
+ if (tc.outcome === 'failure') {
240
+ const afterTurn = sortedTurns.find(
241
+ turn => parseTs(turn.createdAt) > tcTime
242
+ );
243
+ if (afterTurn) {
244
+ afterReflection = afterTurn.sanitizedText.slice(0, 300);
245
+ }
246
+
247
+ // Compute confidence delta if both before and after turns exist
248
+ if (beforeTurn && afterTurn) {
249
+ const beforeConfidence = confidenceToNumber(
250
+ mapConfidenceSignal(computeThinkingModelActivation(beforeTurn.sanitizedText))
251
+ );
252
+ const afterConfidence = confidenceToNumber(
253
+ mapConfidenceSignal(computeThinkingModelActivation(afterTurn.sanitizedText))
254
+ );
255
+ confidenceDelta = Math.round((afterConfidence - beforeConfidence) * 100) / 100;
256
+ }
257
+ }
258
+
259
+ const result: DerivedDecisionPoint = {
260
+ toolName: tc.toolName,
261
+ outcome: tc.outcome,
262
+ beforeContext,
263
+ };
264
+ if (afterReflection !== undefined) result.afterReflection = afterReflection;
265
+ if (confidenceDelta !== undefined) result.confidenceDelta = confidenceDelta;
266
+ return result;
267
+ });
268
+ }
269
+
270
+ // ---------------------------------------------------------------------------
271
+ // deriveContextualFactors (DERIV-03)
272
+ // ---------------------------------------------------------------------------
273
+
274
+ /**
275
+ * Compute contextual factors from session snapshot data.
276
+ *
277
+ * DERIV-03: Four boolean factors indicating the environment
278
+ * the agent was operating in. All derived from existing snapshot
279
+ * fields -- no schema changes.
280
+ *
281
+ * Empty/missing data returns all-false defaults. Never throws.
282
+ */
283
+ export function deriveContextualFactors(
284
+ snapshot: NocturnalSessionSnapshot,
285
+ ): DerivedContextualFactors {
286
+ const defaults: DerivedContextualFactors = {
287
+ fileStructureKnown: false,
288
+ errorHistoryPresent: false,
289
+ userGuidanceAvailable: false,
290
+ timePressure: false,
291
+ };
292
+
293
+ if (!snapshot) return defaults;
294
+
295
+ const { toolCalls = [], userTurns = [] } = snapshot;
296
+
297
+ // fileStructureKnown: any Read tool precedes any Write tool in chronological order
298
+ let fileStructureKnown = false;
299
+ const isReadTool = (name: string) => /^(read|grep|search|find|inspect|look)/i.test(name);
300
+ const isWriteTool = (name: string) => /^(edit|write|create|delete|remove|move|rename)/i.test(name);
301
+ let hasSeenRead = false;
302
+ for (const tc of toolCalls) {
303
+ if (isReadTool(tc.toolName)) hasSeenRead = true;
304
+ if (isWriteTool(tc.toolName) && hasSeenRead) {
305
+ fileStructureKnown = true;
306
+ break;
307
+ }
308
+ }
309
+
310
+ // errorHistoryPresent: any tool call with outcome === 'failure'
311
+ const errorHistoryPresent = toolCalls.some(tc => tc.outcome === 'failure');
312
+
313
+ // userGuidanceAvailable: any user turn with correctionDetected === true
314
+ const userGuidanceAvailable = (userTurns || []).some(ut => ut.correctionDetected === true);
315
+
316
+ // timePressure: >50% of consecutive tool call pairs have < 2s gap
317
+ let timePressure = false;
318
+ if (toolCalls.length >= 2) {
319
+ const sorted = [...toolCalls].sort(
320
+ (a, b) => parseTs(a.createdAt) - parseTs(b.createdAt)
321
+ );
322
+ let rapidGaps = 0;
323
+ for (let i = 0; i < sorted.length - 1; i++) {
324
+ const gap = parseTs(sorted[i + 1].createdAt) - parseTs(sorted[i].createdAt);
325
+ if (gap < 2000) rapidGaps++;
326
+ }
327
+ const totalPairs = sorted.length - 1;
328
+ timePressure = rapidGaps / totalPairs > 0.5;
329
+ }
330
+
331
+ return {
332
+ fileStructureKnown,
333
+ errorHistoryPresent,
334
+ userGuidanceAvailable,
335
+ timePressure,
336
+ };
337
+ }