aiden-runtime 4.1.1 → 4.1.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.
Files changed (55) hide show
  1. package/README.md +78 -26
  2. package/dist/cli/v4/aidenCLI.js +159 -9
  3. package/dist/cli/v4/callbacks.js +5 -2
  4. package/dist/cli/v4/chatSession.js +525 -15
  5. package/dist/cli/v4/commands/auth.js +6 -3
  6. package/dist/cli/v4/commands/help.js +4 -0
  7. package/dist/cli/v4/commands/index.js +10 -1
  8. package/dist/cli/v4/commands/reloadSoul.js +37 -0
  9. package/dist/cli/v4/commands/update.js +102 -0
  10. package/dist/cli/v4/defaultSoul.js +68 -2
  11. package/dist/cli/v4/display.js +28 -10
  12. package/dist/cli/v4/doctor.js +112 -0
  13. package/dist/cli/v4/doctorLiveness.js +65 -10
  14. package/dist/cli/v4/promotionPrompt.js +202 -0
  15. package/dist/cli/v4/providerBootSelector.js +144 -0
  16. package/dist/cli/v4/sessionSummaryGate.js +66 -0
  17. package/dist/cli/v4/toolPreview.js +139 -0
  18. package/dist/core/v4/aidenAgent.js +91 -29
  19. package/dist/core/v4/capabilities.js +89 -0
  20. package/dist/core/v4/contextCompressor.js +25 -8
  21. package/dist/core/v4/distillationIndex.js +167 -0
  22. package/dist/core/v4/distillationStore.js +98 -0
  23. package/dist/core/v4/logger/logger.js +40 -9
  24. package/dist/core/v4/promotionCandidates.js +234 -0
  25. package/dist/core/v4/promptBuilder.js +145 -1
  26. package/dist/core/v4/sessionDistiller.js +405 -0
  27. package/dist/core/v4/skillMining/skillMiner.js +43 -6
  28. package/dist/core/v4/skillOutcomeTracker.js +323 -0
  29. package/dist/core/v4/subsystemHealth.js +143 -0
  30. package/dist/core/v4/update/executeInstall.js +233 -0
  31. package/dist/core/version.js +1 -1
  32. package/dist/moat/memoryGuard.js +111 -0
  33. package/dist/moat/skillTeacher.js +14 -5
  34. package/dist/providers/v4/chatCompletionsAdapter.js +9 -0
  35. package/dist/providers/v4/errors.js +20 -4
  36. package/dist/providers/v4/modelDefaults.js +65 -0
  37. package/dist/providers/v4/registry.js +9 -2
  38. package/dist/providers/v4/runtimeResolver.js +6 -0
  39. package/dist/tools/v4/index.js +57 -1
  40. package/dist/tools/v4/memory/memoryRemove.js +57 -2
  41. package/dist/tools/v4/memory/sessionSummary.js +151 -0
  42. package/dist/tools/v4/sessions/recallSession.js +163 -0
  43. package/dist/tools/v4/sessions/sessionSearch.js +5 -1
  44. package/dist/tools/v4/system/_psHelpers.js +55 -0
  45. package/dist/tools/v4/system/aidenSelfUpdate.js +162 -0
  46. package/dist/tools/v4/system/appClose.js +79 -0
  47. package/dist/tools/v4/system/appLaunch.js +92 -0
  48. package/dist/tools/v4/system/clipboardRead.js +54 -0
  49. package/dist/tools/v4/system/clipboardWrite.js +84 -0
  50. package/dist/tools/v4/system/mediaKey.js +78 -0
  51. package/dist/tools/v4/system/osProcessList.js +99 -0
  52. package/dist/tools/v4/system/screenshot.js +106 -0
  53. package/dist/tools/v4/system/volumeSet.js +157 -0
  54. package/package.json +4 -1
  55. package/skills/system_control.md +135 -69
@@ -0,0 +1,405 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) 2026 Shiva Deore (Taracod).
4
+ * Licensed under AGPL-3.0. See LICENSE for details.
5
+ *
6
+ * Aiden — local-first agent.
7
+ */
8
+ /**
9
+ * core/v4/sessionDistiller.ts — Phase v4.1.2-memory-AB.
10
+ *
11
+ * Replaces the lossy 5-bullet auxiliary summary with a structured
12
+ * SessionDistillation:
13
+ *
14
+ * - bullets[] (5 bullets, back-compat with MEMORY.md `## Recent sessions`)
15
+ * - decisions[] (higher-fidelity than bullets)
16
+ * - open_items[] (unfinished work, useful for next session)
17
+ * - keywords[] (for future retrieval ranking — Phase C)
18
+ * - files_touched[] (DETERMINISTIC — derived from tool-call result payloads)
19
+ * - tools_used[] (DETERMINISTIC — counted from tool-call trace names)
20
+ * - schema_version (always 1; reserved for future migrations)
21
+ * - exit_path (which exit caused the distillation: quit/sigint/etc.)
22
+ * - partial (set true when LLM JSON parse falls back to bullets-only)
23
+ *
24
+ * Source-of-truth split:
25
+ * - Programmatic fields (files_touched, tools_used) → trace inspection.
26
+ * - Semantic fields (bullets, decisions, open_items, keywords) → single
27
+ * auxiliary-LLM call with strict-then-lenient JSON parsing.
28
+ *
29
+ * Phase A's CLI ChatSession owns the per-session HonestyTraceEntry[]
30
+ * accumulator and passes it here. The auxiliary call sees the full
31
+ * message history (not the trace — the trace is purely for programmatic
32
+ * field derivation).
33
+ */
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.TOOL_RESULT_TRUNCATION = exports.SESSION_DISTILLATION_SCHEMA_VERSION = void 0;
36
+ exports.deriveProgrammaticFields = deriveProgrammaticFields;
37
+ exports.parseLLMDistillation = parseLLMDistillation;
38
+ exports.filterMessagesForDistillation = filterMessagesForDistillation;
39
+ exports.distillSession = distillSession;
40
+ // ── Public surface ───────────────────────────────────────────────────────
41
+ exports.SESSION_DISTILLATION_SCHEMA_VERSION = 1;
42
+ // ── Programmatic field derivation ─────────────────────────────────────────
43
+ /**
44
+ * Tools whose result payload SHOULD contain a `path` field naming the
45
+ * file they touched. Used to populate `files_touched`.
46
+ *
47
+ * Curated rather than "any tool with a path in its result" because
48
+ * read-only tools (`file_read`, `file_list`) shouldn't count as
49
+ * "touched" — only mutating ops do.
50
+ */
51
+ const FILE_TOUCH_TOOLS = new Set([
52
+ 'file_write',
53
+ 'file_patch',
54
+ 'file_create',
55
+ 'file_delete',
56
+ 'memory_add', // writes MEMORY.md / USER.md
57
+ 'memory_remove',
58
+ 'memory_replace',
59
+ 'session_summary', // writes MEMORY.md
60
+ ]);
61
+ /**
62
+ * Extract programmatic fields from the accumulated tool trace. Pure
63
+ * function — no I/O.
64
+ */
65
+ function deriveProgrammaticFields(trace) {
66
+ // tools_used: count by name, sorted by count desc, name asc.
67
+ const counts = new Map();
68
+ for (const e of trace) {
69
+ counts.set(e.name, (counts.get(e.name) ?? 0) + 1);
70
+ }
71
+ const tools_used = Array.from(counts.entries())
72
+ .map(([name, count]) => ({ name, count }))
73
+ .sort((a, b) => b.count - a.count || a.name.localeCompare(b.name));
74
+ // files_touched: unique paths from mutating tool results.
75
+ // Each entry's `result` may be { success, path, ... } or { path: ... }
76
+ // depending on the tool. We accept either shape.
77
+ const paths = new Set();
78
+ for (const e of trace) {
79
+ if (e.error)
80
+ continue; // failed tool — don't credit
81
+ if (!FILE_TOUCH_TOOLS.has(e.name))
82
+ continue;
83
+ const candidate = extractPath(e.result);
84
+ if (candidate)
85
+ paths.add(candidate);
86
+ }
87
+ const files_touched = Array.from(paths).sort();
88
+ return { files_touched, tools_used };
89
+ }
90
+ function extractPath(result) {
91
+ if (!result || typeof result !== 'object')
92
+ return null;
93
+ // Top-level path field — most write tools.
94
+ const top = result.path;
95
+ if (typeof top === 'string' && top.length > 0)
96
+ return top;
97
+ // Nested under .result (some adapters wrap output).
98
+ const inner = result.result;
99
+ if (inner && typeof inner === 'object') {
100
+ const innerPath = inner.path;
101
+ if (typeof innerPath === 'string' && innerPath.length > 0)
102
+ return innerPath;
103
+ }
104
+ return null;
105
+ }
106
+ // ── LLM extraction ────────────────────────────────────────────────────────
107
+ /**
108
+ * Strict-then-lenient parser for the auxiliary LLM's distillation JSON.
109
+ *
110
+ * Strict path: parse as JSON, validate shape, return all four semantic
111
+ * fields. Lenient path (only when strict fails): try to extract a
112
+ * bullets array from a malformed body (codepath shared with slice2's
113
+ * parseSessionBulletsResponse fallback), set the other three fields to
114
+ * empty arrays, and signal `partial: true` to the caller.
115
+ *
116
+ * Pure function — no I/O. Caller decides what to do with `partial`.
117
+ */
118
+ function parseLLMDistillation(raw) {
119
+ const trimmed = raw.trim();
120
+ if (!trimmed) {
121
+ return { bullets: [], decisions: [], open_items: [], keywords: [], partial: true };
122
+ }
123
+ // Strict path.
124
+ const strict = tryStrictParse(trimmed);
125
+ if (strict)
126
+ return { ...strict, partial: false };
127
+ // Lenient: scan for a JSON object embedded in prose (some models
128
+ // prefix "Here is the JSON:\n{...}"). Trim to the first '{' through
129
+ // the last '}' and retry.
130
+ const first = trimmed.indexOf('{');
131
+ const last = trimmed.lastIndexOf('}');
132
+ if (first >= 0 && last > first) {
133
+ const inner = trimmed.slice(first, last + 1);
134
+ const second = tryStrictParse(inner);
135
+ if (second)
136
+ return { ...second, partial: false };
137
+ }
138
+ // Bullets-only fallback — recover what we can. Tries a bare bullet
139
+ // list ("- ...", "* ...", numbered lines) or a JSON-array fragment.
140
+ const fallbackBullets = recoverBullets(trimmed);
141
+ return {
142
+ bullets: fallbackBullets,
143
+ decisions: [],
144
+ open_items: [],
145
+ keywords: [],
146
+ partial: true,
147
+ };
148
+ }
149
+ function tryStrictParse(s) {
150
+ try {
151
+ const obj = JSON.parse(s);
152
+ if (!obj || typeof obj !== 'object' || Array.isArray(obj))
153
+ return null;
154
+ const o = obj;
155
+ const bullets = toStringArray(o.bullets);
156
+ const decisions = toStringArray(o.decisions);
157
+ const open_items = toStringArray(o.open_items ?? o.openItems);
158
+ const keywords = toStringArray(o.keywords);
159
+ if (bullets.length === 0 && decisions.length === 0 && open_items.length === 0) {
160
+ return null; // nothing useful — let the lenient path try
161
+ }
162
+ return { bullets, decisions, open_items, keywords };
163
+ }
164
+ catch {
165
+ return null;
166
+ }
167
+ }
168
+ function toStringArray(v) {
169
+ if (!Array.isArray(v))
170
+ return [];
171
+ return v
172
+ .filter((x) => typeof x === 'string')
173
+ .map((x) => x.trim())
174
+ .filter((x) => x.length > 0);
175
+ }
176
+ function recoverBullets(raw) {
177
+ // Strategy 1: bullet-prefixed lines.
178
+ const lines = raw.split(/\r?\n/);
179
+ const bulleted = lines
180
+ .map((l) => l.replace(/^\s*(?:[-*•]|\d+\.)\s+/, '').trim())
181
+ .filter((l, i, arr) => l.length > 0 && /^\s*(?:[-*•]|\d+\.)\s+/.test(lines[i] ?? ''));
182
+ if (bulleted.length > 0)
183
+ return bulleted.slice(0, 5);
184
+ // Strategy 2: a JSON array of strings, with or without the object wrapper.
185
+ const arrMatch = raw.match(/\[\s*"[\s\S]*?"\s*\]/);
186
+ if (arrMatch) {
187
+ try {
188
+ const arr = JSON.parse(arrMatch[0]);
189
+ return toStringArray(arr).slice(0, 5);
190
+ }
191
+ catch { /* fall through */ }
192
+ }
193
+ return [];
194
+ }
195
+ const DEFAULT_TIMEOUT_MS = 4000;
196
+ /**
197
+ * Phase v4.1.2-bug-Y: max chars of tool-result content surfaced to the
198
+ * auxiliary LLM. Covers typical error messages + JSON-payload heads
199
+ * without bloating the prompt with full tool-output dumps. User and
200
+ * assistant TEXT are never truncated — user intent must survive in
201
+ * full. Widen this only after eval shows truncation eating signal.
202
+ */
203
+ exports.TOOL_RESULT_TRUNCATION = 200;
204
+ /**
205
+ * Pure: filter + format the conversation history into the transcript
206
+ * the auxiliary LLM sees. Phase v4.1.2-bug-Y root-cause fix:
207
+ *
208
+ * The previous distiller dumped chatSession.history verbatim,
209
+ * including the giant `role: 'system'` block PromptBuilder
210
+ * constructs (SOUL.md identity, MEMORY.md, USER.md, Runtime slot,
211
+ * Capabilities boilerplate, tool-catalog descriptions, personality
212
+ * overlay, execution-discipline notes). Weak summarizer models
213
+ * latched onto this longest-coherent-block in context as the
214
+ * session topic, returning bullets like "I'm Aiden, a local-first
215
+ * AI agent built by Taracod" regardless of what the user and
216
+ * assistant actually discussed.
217
+ *
218
+ * This filter drops ALL `role: 'system'` messages and emits the
219
+ * remaining traffic as role-tagged lines:
220
+ *
221
+ * [USER] full user message verbatim
222
+ * [ASSISTANT] assistant text (if non-empty)
223
+ * [TOOL:name] {args}
224
+ * [TOOL:name] → result-payload, truncated to TOOL_RESULT_TRUNCATION
225
+ *
226
+ * Tool results carry their tool name (resolved via toolCallId →
227
+ * call-name map walked through preceding assistant turns) so the
228
+ * model can correlate tool intent with output. Empty messages are
229
+ * dropped entirely. Multi-line content within a message is
230
+ * preserved.
231
+ */
232
+ function filterMessagesForDistillation(messages) {
233
+ /** Per-toolCallId → toolName, populated as we walk assistant turns. */
234
+ const callNames = new Map();
235
+ const lines = [];
236
+ for (const m of messages) {
237
+ if (m.role === 'system')
238
+ continue; // entire boilerplate source — dropped
239
+ if (m.role === 'user') {
240
+ const text = m.content.trim();
241
+ if (text.length === 0)
242
+ continue;
243
+ lines.push(`[USER] ${text}`);
244
+ continue;
245
+ }
246
+ if (m.role === 'assistant') {
247
+ // Emit assistant text only if non-empty — avoid empty `[ASSISTANT]`
248
+ // placeholder for tool-only turns.
249
+ const text = (m.content ?? '').trim();
250
+ if (text.length > 0)
251
+ lines.push(`[ASSISTANT] ${text}`);
252
+ // Tool calls: cache the id → name pair so the matching tool
253
+ // result downstream can render with its tool name. Emit the
254
+ // call line in original order.
255
+ if (m.toolCalls && m.toolCalls.length > 0) {
256
+ for (const tc of m.toolCalls) {
257
+ callNames.set(tc.id, tc.name);
258
+ const argsStr = compactArgs(tc.arguments);
259
+ lines.push(`[TOOL:${tc.name}] ${argsStr}`);
260
+ }
261
+ }
262
+ continue;
263
+ }
264
+ if (m.role === 'tool') {
265
+ const name = callNames.get(m.toolCallId) ?? 'unknown';
266
+ const truncated = truncateForTranscript(m.content);
267
+ lines.push(`[TOOL:${name}] → ${truncated}`);
268
+ continue;
269
+ }
270
+ }
271
+ return lines.join('\n');
272
+ }
273
+ /**
274
+ * Compact tool-call args into a one-line representation. JSON shape
275
+ * preserved; large strings get truncated alongside everything else
276
+ * to keep the transcript focused on intent, not full payloads.
277
+ */
278
+ function compactArgs(args) {
279
+ if (!args || Object.keys(args).length === 0)
280
+ return '{}';
281
+ try {
282
+ return truncateForTranscript(JSON.stringify(args));
283
+ }
284
+ catch {
285
+ return '{<unstringifiable>}';
286
+ }
287
+ }
288
+ /**
289
+ * Apply `TOOL_RESULT_TRUNCATION` cap with a `…` (U+2026) marker so
290
+ * truncation is visible to anyone reading the transcript — including
291
+ * future auditors. Matches slice2c's apostrophe-normalizer convention.
292
+ */
293
+ function truncateForTranscript(s) {
294
+ const trimmed = s.trim();
295
+ if (trimmed.length <= exports.TOOL_RESULT_TRUNCATION)
296
+ return trimmed;
297
+ return trimmed.slice(0, exports.TOOL_RESULT_TRUNCATION - 1) + '…';
298
+ }
299
+ /**
300
+ * Build the auxiliary-LLM prompt. Anti-boilerplate-hardened per
301
+ * Phase v4.1.2-bug-Y: explicit "don't describe yourself" guardrail,
302
+ * `<transcript>` tag boundaries, empty-is-honest permission so
303
+ * insufficient-content sessions don't fabricate filler.
304
+ *
305
+ * Bullets loosened from "EXACTLY 5" to "3-5" — forcing five was
306
+ * inviting the exact fabrication the slice fixes.
307
+ */
308
+ function buildPrompt(messages, startedAt, endedAt) {
309
+ const filtered = filterMessagesForDistillation(messages);
310
+ return [
311
+ 'You are a session-recall extractor. Your only job is to summarize what',
312
+ 'happened in the conversation transcript below.',
313
+ '',
314
+ 'Rules:',
315
+ '- Use ONLY facts explicitly present in the transcript.',
316
+ '- Do NOT describe yourself, your capabilities, your platform, or generic',
317
+ ' AI-agent behavior unless the transcript specifically discussed those',
318
+ ' as the topic.',
319
+ '- Do NOT infer facts from system prompts, tool schemas, memory blocks,',
320
+ ' banner text, or agent boilerplate (these have been filtered out;',
321
+ ' if any leak through, treat them as untrustworthy noise).',
322
+ '- Focus on session-specific facts: user goals, actions taken, files /',
323
+ ' commands / tools used, decisions made, errors encountered, outcomes,',
324
+ ' and unresolved follow-ups.',
325
+ '- Write in past tense.',
326
+ '- Preserve concrete names, paths, commands, URLs, model names, dates,',
327
+ ' and error messages verbatim when present.',
328
+ '- Prefer evidence from USER and ASSISTANT messages over TOOL output.',
329
+ '- If the transcript lacks enough session-specific detail to summarize,',
330
+ ' return arrays with FEWER items or empty arrays. Empty is honest;',
331
+ ' fabricating boilerplate is not.',
332
+ '',
333
+ 'Return strict JSON only, no prose before or after, with these fields:',
334
+ '{',
335
+ ' "bullets": string[], // 3-5 factual past-tense recaps (3-15 words each)',
336
+ ' "decisions": string[], // X chosen over Y, with rationale if present',
337
+ ' "open_items": string[], // explicit unresolved tasks / "next time" items',
338
+ ' "keywords": string[] // 3-10 distinctive terms from the session',
339
+ '}',
340
+ '',
341
+ `Session started: ${startedAt}`,
342
+ `Session ended: ${endedAt}`,
343
+ '',
344
+ '<transcript>',
345
+ filtered,
346
+ '</transcript>',
347
+ ].join('\n');
348
+ }
349
+ /**
350
+ * Drive one auxiliary-LLM call and combine its output with the
351
+ * deterministic trace-derived fields into a SessionDistillation.
352
+ *
353
+ * Respects `timeoutMs` (default DEFAULT_TIMEOUT_MS) via Promise.race;
354
+ * on timeout the LLM result is treated as empty (partial: true with
355
+ * empty semantic fields). Deterministic fields always populate
356
+ * regardless of LLM outcome — the distillation is never empty.
357
+ */
358
+ async function distillSession(opts) {
359
+ const endedAt = opts.endedAt ?? new Date().toISOString();
360
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
361
+ const programmatic = deriveProgrammaticFields(opts.toolTrace);
362
+ // Run the auxiliary call under a hard timeout. The race resolves
363
+ // with `{timedOut: true}` if the LLM doesn't return in time — we
364
+ // record that as a partial distillation.
365
+ const prompt = buildPrompt(opts.messages, opts.startedAt, endedAt);
366
+ const llmRaw = await Promise.race([
367
+ opts.auxiliaryClient
368
+ .call({ purpose: 'session_summary', prompt, maxTokens: 800 })
369
+ .then((r) => ({ ok: true, content: r.content ?? '' }))
370
+ .catch((e) => ({ ok: false, error: e })),
371
+ new Promise((resolve) => {
372
+ setTimeout(() => resolve({ ok: false, error: new Error(`auxiliary call timed out after ${timeoutMs}ms`), timedOut: true }), timeoutMs);
373
+ }),
374
+ ]);
375
+ let semantic;
376
+ if (llmRaw.ok) {
377
+ semantic = parseLLMDistillation(llmRaw.content);
378
+ }
379
+ else {
380
+ semantic = {
381
+ bullets: [],
382
+ decisions: [],
383
+ open_items: [],
384
+ keywords: [],
385
+ partial: true,
386
+ };
387
+ }
388
+ const dist = {
389
+ schema_version: exports.SESSION_DISTILLATION_SCHEMA_VERSION,
390
+ session_id: opts.sessionId,
391
+ started_at: opts.startedAt,
392
+ ended_at: endedAt,
393
+ exit_path: opts.exitPath,
394
+ user_turns: opts.userTurns,
395
+ bullets: semantic.bullets,
396
+ decisions: semantic.decisions,
397
+ open_items: semantic.open_items,
398
+ keywords: semantic.keywords,
399
+ files_touched: programmatic.files_touched,
400
+ tools_used: programmatic.tools_used,
401
+ };
402
+ if (semantic.partial)
403
+ dist.partial = true;
404
+ return dist;
405
+ }
@@ -101,6 +101,7 @@ class SkillMiner {
101
101
  this.auxiliaryClient = opts.auxiliaryClient;
102
102
  this.sessionCap = opts.sessionCap ?? exports.SESSION_SKILL_LIMIT;
103
103
  this.skeletonOnly = opts.skeletonOnly ?? false;
104
+ this.healthTracker = opts.healthTracker;
104
105
  }
105
106
  /** Test/reset hook. */
106
107
  _resetForTests() {
@@ -133,14 +134,32 @@ class SkillMiner {
133
134
  return { status: 'opt-out' };
134
135
  }
135
136
  }
136
- // Fingerprint + dedup.
137
+ // Fingerprint + dedup. Phase v4.1.2-slice3: wrap the store reads
138
+ // so disk-level failures (read of pending list, read of rejected
139
+ // list) surface to the health tracker. We re-throw to preserve
140
+ // existing crash-on-disk-error semantics — the surface is purely
141
+ // observational.
137
142
  const fpEntries = obs.trace.map((e) => ({ name: e.name, args: e.args }));
138
143
  const fingerprint = (0, traceFingerprint_1.traceFingerprint)(fpEntries);
139
- const pending = await this.store.list();
144
+ let pending;
145
+ try {
146
+ pending = await this.store.list();
147
+ }
148
+ catch (e) {
149
+ this.healthTracker?.recordFailure(e);
150
+ throw e;
151
+ }
140
152
  if (pending.some((c) => c.fingerprint === fingerprint)) {
141
153
  return { status: 'dedup-pending' };
142
154
  }
143
- const rejected = await this.store.loadRejected();
155
+ let rejected;
156
+ try {
157
+ rejected = await this.store.loadRejected();
158
+ }
159
+ catch (e) {
160
+ this.healthTracker?.recordFailure(e);
161
+ throw e;
162
+ }
144
163
  if (rejected.has(fingerprint)) {
145
164
  return { status: 'dedup-rejected' };
146
165
  }
@@ -163,7 +182,17 @@ class SkillMiner {
163
182
  };
164
183
  let skill = (0, proposalBuilder_1.draft)(obs.trace, ctx);
165
184
  if (!this.skeletonOnly) {
166
- skill = await (0, extractorPrompt_1.refine)(skill, { client: this.auxiliaryClient });
185
+ // Phase v4.1.2-slice3: refine is an LLM call that historically
186
+ // crashed the whole turn when it threw. Wrap and surface; on
187
+ // failure fall back to the skeleton so mining still produces
188
+ // something rather than nothing.
189
+ try {
190
+ skill = await (0, extractorPrompt_1.refine)(skill, { client: this.auxiliaryClient });
191
+ }
192
+ catch (e) {
193
+ this.healthTracker?.recordFailure(e);
194
+ // Skeleton retained; carry on.
195
+ }
167
196
  }
168
197
  // Final validation — must round-trip through parseSkillContent
169
198
  // (the canonical loader parser). If it doesn't, drop the
@@ -171,7 +200,8 @@ class SkillMiner {
171
200
  try {
172
201
  (0, skillSpec_1.parseSkillContent)(skill);
173
202
  }
174
- catch {
203
+ catch (e) {
204
+ this.healthTracker?.recordFailure(e);
175
205
  return { status: 'invalid-skill', confidence };
176
206
  }
177
207
  const candidate = {
@@ -183,8 +213,15 @@ class SkillMiner {
183
213
  candidateConfidence: confidence,
184
214
  skillContent: skill,
185
215
  };
186
- await this.store.append(candidate);
216
+ try {
217
+ await this.store.append(candidate);
218
+ }
219
+ catch (e) {
220
+ this.healthTracker?.recordFailure(e);
221
+ throw e;
222
+ }
187
223
  this.perSessionCount.set(obs.sessionId, this.countForSession(obs.sessionId) + 1);
224
+ this.healthTracker?.recordSuccess();
188
225
  return { status: 'queued', candidate, confidence };
189
226
  }
190
227
  }