selftune 0.2.16 → 0.2.19

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 (91) hide show
  1. package/README.md +32 -22
  2. package/apps/local-dashboard/dist/assets/index-DnhnXQm6.js +60 -0
  3. package/apps/local-dashboard/dist/assets/index-_EcLywDg.css +1 -0
  4. package/apps/local-dashboard/dist/assets/vendor-table-BIiI3YhS.js +1 -0
  5. package/apps/local-dashboard/dist/assets/vendor-ui-CGEmUayx.js +12 -0
  6. package/apps/local-dashboard/dist/index.html +5 -5
  7. package/cli/selftune/alpha-upload/build-payloads.ts +14 -1
  8. package/cli/selftune/alpha-upload/client.ts +51 -1
  9. package/cli/selftune/alpha-upload/flush.ts +46 -5
  10. package/cli/selftune/alpha-upload/stage-canonical.ts +32 -10
  11. package/cli/selftune/alpha-upload-contract.ts +9 -0
  12. package/cli/selftune/constants.ts +92 -5
  13. package/cli/selftune/contribute/contribute.ts +30 -2
  14. package/cli/selftune/contribute/sanitize.ts +52 -5
  15. package/cli/selftune/contribution-config.ts +249 -0
  16. package/cli/selftune/contribution-relay.ts +177 -0
  17. package/cli/selftune/contribution-signals.ts +219 -0
  18. package/cli/selftune/contribution-staging.ts +147 -0
  19. package/cli/selftune/contributions.ts +532 -0
  20. package/cli/selftune/creator-contributions.ts +333 -0
  21. package/cli/selftune/dashboard-contract.ts +305 -1
  22. package/cli/selftune/dashboard-server.ts +47 -13
  23. package/cli/selftune/eval/family-overlap.ts +395 -0
  24. package/cli/selftune/eval/hooks-to-evals.ts +182 -28
  25. package/cli/selftune/eval/synthetic-evals.ts +298 -11
  26. package/cli/selftune/evolution/description-quality.ts +12 -11
  27. package/cli/selftune/evolution/evolve.ts +214 -51
  28. package/cli/selftune/evolution/validate-proposal.ts +9 -6
  29. package/cli/selftune/export.ts +2 -2
  30. package/cli/selftune/grading/grade-session.ts +20 -0
  31. package/cli/selftune/hooks/commit-track.ts +188 -0
  32. package/cli/selftune/hooks/prompt-log.ts +10 -1
  33. package/cli/selftune/hooks/session-stop.ts +2 -2
  34. package/cli/selftune/hooks/skill-eval.ts +15 -1
  35. package/cli/selftune/hooks/stdin-preview.ts +32 -0
  36. package/cli/selftune/index.ts +41 -5
  37. package/cli/selftune/ingestors/codex-rollout.ts +31 -35
  38. package/cli/selftune/ingestors/codex-wrapper.ts +32 -24
  39. package/cli/selftune/localdb/db.ts +2 -2
  40. package/cli/selftune/localdb/direct-write.ts +69 -6
  41. package/cli/selftune/localdb/queries.ts +1253 -37
  42. package/cli/selftune/localdb/schema.ts +66 -0
  43. package/cli/selftune/orchestrate.ts +32 -4
  44. package/cli/selftune/recover.ts +153 -0
  45. package/cli/selftune/repair/skill-usage.ts +363 -4
  46. package/cli/selftune/routes/actions.ts +35 -1
  47. package/cli/selftune/routes/analytics.ts +14 -0
  48. package/cli/selftune/routes/index.ts +1 -0
  49. package/cli/selftune/routes/overview.ts +150 -4
  50. package/cli/selftune/routes/skill-report.ts +648 -18
  51. package/cli/selftune/status.ts +81 -2
  52. package/cli/selftune/sync.ts +56 -2
  53. package/cli/selftune/trust-model.ts +66 -0
  54. package/cli/selftune/types.ts +80 -0
  55. package/cli/selftune/utils/skill-detection.ts +43 -0
  56. package/cli/selftune/utils/transcript.ts +210 -1
  57. package/cli/selftune/watchlist.ts +65 -0
  58. package/node_modules/@selftune/telemetry-contract/src/types.ts +11 -0
  59. package/package.json +1 -1
  60. package/packages/telemetry-contract/src/types.ts +11 -0
  61. package/packages/ui/src/components/ActivityTimeline.tsx +165 -150
  62. package/packages/ui/src/components/EvidenceViewer.tsx +335 -144
  63. package/packages/ui/src/components/EvolutionTimeline.tsx +58 -28
  64. package/packages/ui/src/components/OrchestrateRunsPanel.tsx +33 -16
  65. package/packages/ui/src/components/RecentActivityFeed.tsx +72 -41
  66. package/packages/ui/src/components/section-cards.tsx +12 -9
  67. package/packages/ui/src/primitives/card.tsx +1 -1
  68. package/skill/SKILL.md +40 -2
  69. package/skill/Workflows/AlphaUpload.md +4 -0
  70. package/skill/Workflows/Composability.md +64 -0
  71. package/skill/Workflows/Contribute.md +6 -3
  72. package/skill/Workflows/Contributions.md +97 -0
  73. package/skill/Workflows/CreatorContributions.md +74 -0
  74. package/skill/Workflows/Dashboard.md +31 -0
  75. package/skill/Workflows/Evals.md +57 -8
  76. package/skill/Workflows/Evolve.md +31 -13
  77. package/skill/Workflows/ExportCanonical.md +121 -0
  78. package/skill/Workflows/Hook.md +131 -0
  79. package/skill/Workflows/Ingest.md +7 -0
  80. package/skill/Workflows/Initialize.md +29 -9
  81. package/skill/Workflows/Orchestrate.md +27 -5
  82. package/skill/Workflows/Quickstart.md +94 -0
  83. package/skill/Workflows/Recover.md +84 -0
  84. package/skill/Workflows/RepairSkillUsage.md +95 -0
  85. package/skill/Workflows/Sync.md +18 -12
  86. package/skill/Workflows/Uninstall.md +82 -0
  87. package/skill/settings_snippet.json +11 -0
  88. package/apps/local-dashboard/dist/assets/index-BMIS6uUh.css +0 -2
  89. package/apps/local-dashboard/dist/assets/index-DOu3iLD9.js +0 -16
  90. package/apps/local-dashboard/dist/assets/vendor-table-pHbDxq36.js +0 -8
  91. package/apps/local-dashboard/dist/assets/vendor-ui-DIwlrGlb.js +0 -12
@@ -0,0 +1,188 @@
1
+ #!/usr/bin/env bun
2
+ /**
3
+ * PostToolUse hook: commit-track.ts
4
+ *
5
+ * Detects git commits in Bash tool output and records commit SHA, title,
6
+ * branch, and session ID for session-to-commit traceability.
7
+ *
8
+ * Fail-open: exits 0 on all errors. Never blocks the host agent.
9
+ */
10
+
11
+ import { execSync } from "node:child_process";
12
+
13
+ import type { PostToolUsePayload } from "../types.js";
14
+
15
+ // -- Regex patterns (pre-compiled at module load) ----------------------------
16
+
17
+ /** Matches git commands that produce commits. */
18
+ const GIT_COMMIT_CMD_RE = /\bgit\s+(commit|merge|cherry-pick|revert)\b/;
19
+
20
+ /**
21
+ * Matches the standard git commit output format: [branch SHA] title
22
+ * Supports optional parenthetical like (root-commit).
23
+ * Branch names can contain word chars, slashes, dots, hyphens, plus signs.
24
+ */
25
+ const COMMIT_OUTPUT_RE = /\[([\w/.+-]+)(?:\s+\([^)]+\))?\s+([a-f0-9]{7,40})\]\s+(.+)/;
26
+
27
+ // -- Pure extraction functions (exported for testability) ---------------------
28
+
29
+ /** Check if a command string contains a git commit/merge/cherry-pick/revert. */
30
+ export function containsGitCommitCommand(command: string): boolean {
31
+ return GIT_COMMIT_CMD_RE.test(command);
32
+ }
33
+
34
+ /** Extract commit SHA from git output. */
35
+ export function parseCommitSha(output: string): string | undefined {
36
+ const match = output.match(COMMIT_OUTPUT_RE);
37
+ return match ? match[2] : undefined;
38
+ }
39
+
40
+ /** Extract commit title from git output. */
41
+ export function parseCommitTitle(output: string): string | undefined {
42
+ const match = output.match(COMMIT_OUTPUT_RE);
43
+ return match ? match[3].trim() : undefined;
44
+ }
45
+
46
+ /** Extract branch name from git output. */
47
+ export function parseBranchFromOutput(output: string): string | undefined {
48
+ const match = output.match(COMMIT_OUTPUT_RE);
49
+ return match ? match[1] : undefined;
50
+ }
51
+
52
+ /** Scrub credentials from a remote URL. Returns undefined for empty input. */
53
+ export function scrubRemoteUrl(rawUrl: string): string | undefined {
54
+ if (!rawUrl) return undefined;
55
+ try {
56
+ const parsed = new URL(rawUrl);
57
+ parsed.username = "";
58
+ parsed.password = "";
59
+ return `${parsed.protocol}//${parsed.host}${parsed.pathname}`;
60
+ } catch {
61
+ // SSH or non-URL format — safe as-is
62
+ return rawUrl;
63
+ }
64
+ }
65
+
66
+ // -- Commit tracking record shape --------------------------------------------
67
+
68
+ export interface CommitTrackRecord {
69
+ session_id: string;
70
+ commit_sha: string;
71
+ commit_title?: string;
72
+ branch?: string;
73
+ repo_remote?: string;
74
+ timestamp: string;
75
+ }
76
+
77
+ // -- Core processing logic ---------------------------------------------------
78
+
79
+ /**
80
+ * Process a PostToolUse payload for git commit tracking.
81
+ * Returns the record that was written, or null if skipped.
82
+ * Exported for testability.
83
+ */
84
+ export async function processCommitTrack(
85
+ payload: PostToolUsePayload,
86
+ ): Promise<CommitTrackRecord | null> {
87
+ // Fast-path: only care about Bash tool
88
+ if (payload.tool_name !== "Bash") return null;
89
+
90
+ // Fast-path: if cwd is known and not inside a git repo, skip
91
+ const cwd = payload.cwd ?? "";
92
+ if (cwd) {
93
+ try {
94
+ execSync("git rev-parse --is-inside-work-tree", {
95
+ cwd,
96
+ timeout: 1000,
97
+ stdio: ["ignore", "ignore", "ignore"],
98
+ });
99
+ } catch {
100
+ return null; // Not inside a git repo
101
+ }
102
+ }
103
+
104
+ // Fast-path: check if the command is a git commit-producing operation
105
+ const command = typeof payload.tool_input?.command === "string" ? payload.tool_input.command : "";
106
+ if (!containsGitCommitCommand(command)) return null;
107
+
108
+ // Extract stdout from tool_response
109
+ const response = payload.tool_response ?? {};
110
+ const stdout = typeof response.stdout === "string" ? response.stdout : "";
111
+ if (!stdout) return null;
112
+
113
+ // Parse commit SHA — if we can't find one, nothing to track
114
+ const commitSha = parseCommitSha(stdout);
115
+ if (!commitSha) return null;
116
+
117
+ const commitTitle = parseCommitTitle(stdout);
118
+ const outputBranch = parseBranchFromOutput(stdout);
119
+ const sessionId = payload.session_id;
120
+ if (!sessionId) return null; // No session ID — skip insert (fail-open)
121
+
122
+ // Try to get branch from git if not parsed from output
123
+ let branch = outputBranch;
124
+ if (!branch && cwd) {
125
+ try {
126
+ branch =
127
+ execSync("git rev-parse --abbrev-ref HEAD", {
128
+ cwd,
129
+ timeout: 3000,
130
+ stdio: ["ignore", "pipe", "ignore"],
131
+ })
132
+ .toString()
133
+ .trim() || undefined;
134
+ } catch {
135
+ /* not a git repo or git not available */
136
+ }
137
+ }
138
+
139
+ // Try to get remote URL (scrub credentials)
140
+ let repoRemote: string | undefined;
141
+ if (cwd) {
142
+ try {
143
+ const rawRemote =
144
+ execSync("git remote get-url origin", {
145
+ cwd,
146
+ timeout: 3000,
147
+ stdio: ["ignore", "pipe", "ignore"],
148
+ })
149
+ .toString()
150
+ .trim() || undefined;
151
+ if (rawRemote) {
152
+ repoRemote = scrubRemoteUrl(rawRemote);
153
+ }
154
+ } catch {
155
+ /* no remote configured */
156
+ }
157
+ }
158
+
159
+ const record: CommitTrackRecord = {
160
+ session_id: sessionId,
161
+ commit_sha: commitSha,
162
+ commit_title: commitTitle,
163
+ branch,
164
+ repo_remote: repoRemote,
165
+ timestamp: new Date().toISOString(),
166
+ };
167
+
168
+ // Write to SQLite (dynamic import to reduce hook startup cost)
169
+ try {
170
+ const { writeCommitTracking } = await import("../localdb/direct-write.js");
171
+ writeCommitTracking(record);
172
+ } catch {
173
+ /* hooks must never block */
174
+ }
175
+
176
+ return record;
177
+ }
178
+
179
+ // --- stdin main (only when executed directly, not when imported) ---
180
+ if (import.meta.main) {
181
+ try {
182
+ const payload: PostToolUsePayload = JSON.parse(await Bun.stdin.text());
183
+ await processCommitTrack(payload);
184
+ } catch {
185
+ // silent — hooks must never block Claude
186
+ }
187
+ process.exit(0);
188
+ }
@@ -226,7 +226,16 @@ export async function processPrompt(
226
226
  // --- stdin main (only when executed directly, not when imported) ---
227
227
  if (import.meta.main) {
228
228
  try {
229
- const payload: PromptSubmitPayload = JSON.parse(await Bun.stdin.text());
229
+ const { readStdinWithPreview } = await import("./stdin-preview.js");
230
+ const { preview, full } = await readStdinWithPreview();
231
+
232
+ // Fast-path: prompt-log only handles UserPromptSubmit events.
233
+ // If the keyword is absent from the first 4 KiB we can skip JSON.parse entirely.
234
+ if (!preview.includes('"UserPromptSubmit"')) {
235
+ process.exit(0);
236
+ }
237
+
238
+ const payload: PromptSubmitPayload = JSON.parse(full);
230
239
  await processPrompt(payload);
231
240
  } catch {
232
241
  // silent — hooks must never block Claude
@@ -11,7 +11,7 @@
11
11
  import { execSync } from "node:child_process";
12
12
  import { readFileSync } from "node:fs";
13
13
 
14
- import { CANONICAL_LOG, ORCHESTRATE_LOCK, TELEMETRY_LOG } from "../constants.js";
14
+ import { CANONICAL_LOG, getOrchestrateLockPath, TELEMETRY_LOG } from "../constants.js";
15
15
  import {
16
16
  appendCanonicalRecords,
17
17
  buildCanonicalExecutionFact,
@@ -47,7 +47,7 @@ function hasFreshOrchestrateLock(lockPath: string): boolean {
47
47
  * Returns true if a process was spawned, false otherwise.
48
48
  */
49
49
  export async function maybeSpawnReactiveOrchestrate(
50
- lockPath: string = ORCHESTRATE_LOCK,
50
+ lockPath: string = getOrchestrateLockPath(),
51
51
  deps: ReactiveSpawnDeps = {},
52
52
  ): Promise<boolean> {
53
53
  try {
@@ -359,7 +359,21 @@ async function processSkillToolUse(
359
359
  // --- stdin main (only when executed directly, not when imported) ---
360
360
  if (import.meta.main) {
361
361
  try {
362
- const payload: PostToolUsePayload = JSON.parse(await Bun.stdin.text());
362
+ const { readStdinWithPreview } = await import("./stdin-preview.js");
363
+ const { preview, full } = await readStdinWithPreview();
364
+
365
+ // Fast-path: skill-eval only handles PostToolUse events.
366
+ if (!preview.includes('"PostToolUse"')) {
367
+ process.exit(0);
368
+ }
369
+
370
+ // Secondary fast-path: only Read and Skill tools are relevant.
371
+ // Most PostToolUse events are for Bash/Write/Edit — skip those entirely.
372
+ if (!preview.includes('"Read"') && !preview.includes('"Skill"')) {
373
+ process.exit(0);
374
+ }
375
+
376
+ const payload: PostToolUsePayload = JSON.parse(full);
363
377
  await processToolUse(payload);
364
378
  } catch {
365
379
  // silent — hooks must never block Claude
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Shared stdin preview utility for hook fast-path optimization.
3
+ *
4
+ * Reads all of stdin once, then exposes a small preview slice so callers
5
+ * can do cheap `.includes()` keyword checks before paying for JSON.parse().
6
+ * When the keyword is absent the hook can exit in <1ms.
7
+ */
8
+
9
+ const STDIN_PREVIEW_BYTES = 4096;
10
+
11
+ /**
12
+ * Read stdin and return both a preview slice and the full text.
13
+ *
14
+ * Bun's `stdin.text()` consumes the entire stream in one call, so this is
15
+ * not a true streaming preview. The win comes from avoiding `JSON.parse()`
16
+ * entirely when the preview slice already proves the payload is irrelevant.
17
+ *
18
+ * @param previewBytes Number of leading characters to expose in `preview`.
19
+ * Defaults to 4096, which comfortably covers the
20
+ * envelope fields (`hook_event_name`, `tool_name`, etc.)
21
+ * of any Claude Code hook payload.
22
+ */
23
+ export async function readStdinWithPreview(previewBytes: number = STDIN_PREVIEW_BYTES): Promise<{
24
+ preview: string;
25
+ full: string;
26
+ }> {
27
+ const raw = await Bun.stdin.text();
28
+ return {
29
+ preview: raw.slice(0, previewBytes),
30
+ full: raw,
31
+ };
32
+ }
@@ -6,7 +6,7 @@
6
6
  * selftune ingest <agent> — Ingest agent sessions (claude, codex, opencode, openclaw, wrap-codex)
7
7
  * selftune grade [mode] — Grade skill sessions (auto, baseline)
8
8
  * selftune evolve [target] — Evolve skill descriptions (body, rollback)
9
- * selftune eval <action> — Evaluation tools (generate, unit-test, import, composability)
9
+ * selftune eval <action> — Evaluation tools (generate, unit-test, import, composability, family-overlap)
10
10
  * selftune sync — Sync source-truth telemetry across supported agents
11
11
  * selftune orchestrate — Run autonomous core loop (sync → status → evolve → watch)
12
12
  * selftune init — Initialize agent identity and config
@@ -19,11 +19,14 @@
19
19
  * selftune cron — Scheduling & automation (setup, list, remove)
20
20
  * selftune badge — Generate skill health badges for READMEs
21
21
  * selftune contribute — Export anonymized skill data for community
22
+ * selftune contributions — Manage creator-directed sharing preferences
23
+ * selftune creator-contributions — Manage creator-side contribution configs
22
24
  * selftune workflows — Discover and manage multi-skill workflows
23
25
  * selftune quickstart — Guided onboarding: init, ingest, status, and suggestions
24
26
  * selftune repair-skill-usage — Rebuild trustworthy skill usage from transcripts
25
- * selftune export — Export SQLite data to JSONL files
27
+ * selftune export — Export SQLite data to JSONL snapshots
26
28
  * selftune export-canonical — Export canonical telemetry for downstream ingestion
29
+ * selftune recover — Recover SQLite from legacy/exported JSONL
27
30
  * selftune telemetry — Manage anonymous usage analytics (status, enable, disable)
28
31
  * selftune alpha <subcommand> — Alpha program management (upload)
29
32
  * selftune hook <name> — Run a hook by name (prompt-log, session-stop, etc.)
@@ -46,7 +49,7 @@ Commands:
46
49
  ingest <agent> Ingest agent sessions (claude, codex, opencode, openclaw, wrap-codex)
47
50
  grade [mode] Grade skill sessions (auto, baseline)
48
51
  evolve [target] Evolve skill descriptions (body, rollback)
49
- eval <action> Evaluation tools (generate, unit-test, import, composability)
52
+ eval <action> Evaluation tools (generate, unit-test, import, composability, family-overlap)
50
53
  sync Sync source-truth telemetry across supported agents
51
54
  orchestrate Run autonomous core loop (sync → status → evolve → watch)
52
55
  init Initialize agent identity and config
@@ -59,11 +62,14 @@ Commands:
59
62
  cron Scheduling & automation (setup, list, remove)
60
63
  badge Generate skill health badges for READMEs
61
64
  contribute Export anonymized skill data for community
65
+ contributions Manage creator-directed sharing preferences
66
+ creator-contributions Manage creator-side contribution configs
62
67
  workflows Discover and manage multi-skill workflows
63
68
  quickstart Guided onboarding: init, ingest, status, and suggestions
64
69
  repair-skill-usage Rebuild trustworthy skill usage from transcripts
65
- export Export SQLite data to JSONL files
70
+ export Export SQLite data to JSONL snapshots
66
71
  export-canonical Export canonical telemetry for downstream ingestion
72
+ recover Recover SQLite from legacy/exported JSONL
67
73
  alpha <subcommand> Alpha program management (upload)
68
74
  telemetry Manage anonymous usage analytics (status, enable, disable)
69
75
  hook <name> Run a hook by name (prompt-log, session-stop, etc.)
@@ -254,6 +260,7 @@ Actions:
254
260
  unit-test Run or generate skill unit tests
255
261
  import Import SkillsBench task corpus as eval entries
256
262
  composability Analyze skill co-occurrence conflicts
263
+ family-overlap Detect sibling-skill overlap and consolidation pressure
257
264
 
258
265
  Run 'selftune eval <action> --help' for action-specific options.`);
259
266
  process.exit(0);
@@ -341,6 +348,17 @@ Run 'selftune eval <action> --help' for action-specific options.`);
341
348
  console.log(JSON.stringify(report, null, 2));
342
349
  break;
343
350
  }
351
+ case "family-overlap": {
352
+ if (process.argv[2] === "--help" || process.argv[2] === "-h") {
353
+ console.log(
354
+ "selftune eval family-overlap --prefix <family-> | --skills <a,b,c> [--parent-skill <name>] [--min-overlap 0.3] [--min-shared 2]",
355
+ );
356
+ process.exit(0);
357
+ }
358
+ const { cliMain } = await import("./eval/family-overlap.js");
359
+ await cliMain();
360
+ break;
361
+ }
344
362
  default:
345
363
  throw new CLIError(
346
364
  `Unknown eval action: ${sub}`,
@@ -368,6 +386,16 @@ Run 'selftune eval <action> --help' for action-specific options.`);
368
386
  await cliMain();
369
387
  break;
370
388
  }
389
+ case "contributions": {
390
+ const { cliMain } = await import("./contributions.js");
391
+ await cliMain();
392
+ break;
393
+ }
394
+ case "creator-contributions": {
395
+ const { cliMain } = await import("./creator-contributions.js");
396
+ await cliMain();
397
+ break;
398
+ }
371
399
  case "watch": {
372
400
  const { cliMain } = await import("./monitoring/watch.js");
373
401
  await cliMain();
@@ -527,11 +555,14 @@ Run 'selftune cron <subcommand> --help' for subcommand-specific options.`);
527
555
  throw new CLIError(`Invalid arguments: ${message}`, "INVALID_FLAG", "selftune export --help");
528
556
  }
529
557
  if (values.help) {
530
- console.log(`selftune export — Export SQLite data to JSONL files
558
+ console.log(`selftune export — Export SQLite data to JSONL snapshots
531
559
 
532
560
  Usage:
533
561
  selftune export [tables...] [options]
534
562
 
563
+ Use this for portability, debugging, contribute flows, or explicit recovery
564
+ snapshots. Normal runtime reads and writes stay in SQLite.
565
+
535
566
  Tables (default: all):
536
567
  telemetry Session telemetry records
537
568
  skills Skill usage records
@@ -570,6 +601,11 @@ Options:
570
601
  cliMain();
571
602
  break;
572
603
  }
604
+ case "recover": {
605
+ const { cliMain } = await import("./recover.js");
606
+ cliMain();
607
+ break;
608
+ }
573
609
  case "orchestrate": {
574
610
  const { cliMain } = await import("./orchestrate.js");
575
611
  await cliMain();
@@ -52,9 +52,9 @@ import type {
52
52
  import { handleCLIError } from "../utils/cli-error.js";
53
53
  import { loadMarker, saveMarker } from "../utils/jsonl.js";
54
54
  import { extractActionableQueryText } from "../utils/query-filter.js";
55
+ import { getInternalPromptTargetSkill, isWrappedNonUserPart } from "../utils/skill-detection.js";
55
56
  import {
56
57
  classifySkillPath,
57
- containsWholeSkillMention,
58
58
  extractExplicitSkillMentions,
59
59
  extractSkillNamesFromInstructions,
60
60
  extractSkillNamesFromPathReferences,
@@ -228,6 +228,15 @@ export function parseRolloutFile(path: string, skillNames: Set<string>): ParsedR
228
228
  let observedCwd: string | undefined;
229
229
  const sessionSkillNames = new Set(skillNames);
230
230
  let hasActionablePrompt = false;
231
+ const markSkillTriggered = (skillName: string, evidence: "explicit" | "inferred"): void => {
232
+ if (!skillsTriggered.includes(skillName)) {
233
+ skillsTriggered.push(skillName);
234
+ }
235
+ const existingEvidence = skillEvidence.get(skillName);
236
+ if (existingEvidence !== "explicit") {
237
+ skillEvidence.set(skillName, evidence);
238
+ }
239
+ };
231
240
  const rememberSessionSkillNames = (text: unknown): void => {
232
241
  if (typeof text !== "string" || !text) return;
233
242
  for (const skillName of extractSkillNamesFromInstructions(text, sessionSkillNames)) {
@@ -240,33 +249,23 @@ export function parseRolloutFile(path: string, skillNames: Set<string>): ParsedR
240
249
  sessionSkillNames.add(skillName);
241
250
  }
242
251
  };
243
- const detectTriggeredSkills = (text: unknown): void => {
244
- if (typeof text !== "string" || !text) return;
245
- for (const skillName of sessionSkillNames) {
246
- if (containsWholeSkillMention(text, skillName) && !skillsTriggered.includes(skillName)) {
247
- skillsTriggered.push(skillName);
248
- }
249
- if (containsWholeSkillMention(text, skillName) && !skillEvidence.has(skillName)) {
250
- skillEvidence.set(skillName, "inferred");
251
- }
252
- }
253
- };
254
252
  const detectExplicitPromptSkillMentions = (text: unknown): void => {
255
253
  if (typeof text !== "string" || !text) return;
256
- for (const skillName of extractExplicitSkillMentions(text, sessionSkillNames)) {
257
- if (!skillsTriggered.includes(skillName)) {
258
- skillsTriggered.push(skillName);
259
- }
260
- skillEvidence.set(skillName, "explicit");
254
+ if (isWrappedNonUserPart(text)) return;
255
+ const actionableText = extractActionableQueryText(text) ?? text;
256
+ const internalTargetSkill = getInternalPromptTargetSkill(actionableText, sessionSkillNames);
257
+ if (internalTargetSkill) {
258
+ markSkillTriggered(internalTargetSkill, "explicit");
259
+ return;
260
+ }
261
+ for (const skillName of extractExplicitSkillMentions(actionableText, sessionSkillNames)) {
262
+ markSkillTriggered(skillName, "explicit");
261
263
  }
262
264
  };
263
265
  const detectExplicitSkillReads = (text: unknown): void => {
264
266
  if (typeof text !== "string" || !text) return;
265
267
  for (const skillName of extractSkillNamesFromPathReferences(text, sessionSkillNames)) {
266
- if (!skillsTriggered.includes(skillName)) {
267
- skillsTriggered.push(skillName);
268
- }
269
- skillEvidence.set(skillName, "explicit");
268
+ markSkillTriggered(skillName, "explicit");
270
269
  }
271
270
  };
272
271
  const rememberPromptCandidate = (value: unknown): void => {
@@ -352,27 +351,26 @@ export function parseRolloutFile(path: string, skillNames: Set<string>): ParsedR
352
351
  if (itemType === "function_call") {
353
352
  const fnName = (payload.name as string) ?? "function_call";
354
353
  toolCalls[fnName] = (toolCalls[fnName] ?? 0) + 1;
355
- // Check for skill mentions in function arguments
354
+ // Only path-based skill references count as triggers here.
356
355
  detectExplicitSkillReads(payload.arguments);
357
- detectTriggeredSkills(payload.arguments);
358
356
  } else if (itemType === "agent_reasoning") {
359
357
  toolCalls.reasoning = (toolCalls.reasoning ?? 0) + 1;
360
- detectTriggeredSkills(payload.text);
361
358
  } else if (itemType === "message") {
362
- const content = Array.isArray(payload.content)
359
+ const parts = Array.isArray(payload.content)
363
360
  ? payload.content
364
361
  .map((part) =>
365
362
  typeof part === "object" && part
366
363
  ? (((part as Record<string, unknown>).text as string | undefined) ?? "")
367
364
  : "",
368
365
  )
369
- .join("\n")
370
- : "";
366
+ .filter(Boolean)
367
+ : [];
368
+ const content = parts.join("\n");
371
369
  rememberSessionSkillNames(content);
372
- if ((payload.role as string) === "assistant") {
373
- detectTriggeredSkills(content);
374
- } else if ((payload.role as string) === "user") {
375
- detectExplicitPromptSkillMentions(content);
370
+ if ((payload.role as string) === "user") {
371
+ for (const part of parts) {
372
+ detectExplicitPromptSkillMentions(part);
373
+ }
376
374
  }
377
375
  }
378
376
  } else if (etype === "turn.started") {
@@ -410,10 +408,8 @@ export function parseRolloutFile(path: string, skillNames: Set<string>): ParsedR
410
408
  }
411
409
 
412
410
  // Detect skill names in text content on completed events
413
- const textContent = ((item.text as string) ?? "") + ((item.command as string) ?? "");
414
- detectExplicitSkillReads(textContent);
415
- if (etype === "item.completed") {
416
- detectTriggeredSkills(textContent);
411
+ if (itemType === "command_execution") {
412
+ detectExplicitSkillReads(item.command);
417
413
  }
418
414
  } else if (etype === "error") {
419
415
  errors += 1;
@@ -41,11 +41,13 @@ import type {
41
41
  SessionTelemetryRecord,
42
42
  SkillUsageRecord,
43
43
  } from "../types.js";
44
+ import { extractActionableQueryText } from "../utils/query-filter.js";
45
+ import { getInternalPromptTargetSkill, isWrappedNonUserPart } from "../utils/skill-detection.js";
44
46
  import {
45
47
  classifySkillPath,
46
- containsWholeSkillMention,
47
48
  extractExplicitSkillMentions,
48
49
  extractSkillNamesFromInstructions,
50
+ extractSkillNamesFromPathReferences,
49
51
  findInstalledSkillNames,
50
52
  findInstalledSkillPath,
51
53
  findRepositorySkillDirs,
@@ -112,26 +114,34 @@ export function parseJsonlStream(lines: string[], skillNames: Set<string>): Pars
112
114
  let outputTokens = 0;
113
115
  const agentMessages: string[] = [];
114
116
  const sessionSkillNames = new Set(skillNames);
117
+ const markSkillTriggered = (skillName: string): void => {
118
+ if (!skillsTriggered.includes(skillName)) {
119
+ skillsTriggered.push(skillName);
120
+ }
121
+ };
115
122
  const rememberSessionSkillNames = (text: unknown): void => {
116
123
  if (typeof text !== "string" || !text) return;
117
124
  for (const skillName of extractSkillNamesFromInstructions(text, sessionSkillNames)) {
118
125
  sessionSkillNames.add(skillName);
119
126
  }
120
127
  };
121
- const detectTriggeredSkills = (text: unknown): void => {
128
+ const detectExplicitSkillReads = (text: unknown): void => {
122
129
  if (typeof text !== "string" || !text) return;
123
- for (const skillName of sessionSkillNames) {
124
- if (containsWholeSkillMention(text, skillName) && !skillsTriggered.includes(skillName)) {
125
- skillsTriggered.push(skillName);
126
- }
130
+ for (const skillName of extractSkillNamesFromPathReferences(text, sessionSkillNames)) {
131
+ markSkillTriggered(skillName);
127
132
  }
128
133
  };
129
134
  const detectExplicitPromptSkillMentions = (text: unknown): void => {
130
135
  if (typeof text !== "string" || !text) return;
131
- for (const skillName of extractExplicitSkillMentions(text, sessionSkillNames)) {
132
- if (!skillsTriggered.includes(skillName)) {
133
- skillsTriggered.push(skillName);
134
- }
136
+ if (isWrappedNonUserPart(text)) return;
137
+ const actionableText = extractActionableQueryText(text) ?? text;
138
+ const internalTargetSkill = getInternalPromptTargetSkill(actionableText, sessionSkillNames);
139
+ if (internalTargetSkill) {
140
+ markSkillTriggered(internalTargetSkill);
141
+ return;
142
+ }
143
+ for (const skillName of extractExplicitSkillMentions(actionableText, sessionSkillNames)) {
144
+ markSkillTriggered(skillName);
135
145
  }
136
146
  };
137
147
 
@@ -187,40 +197,38 @@ export function parseJsonlStream(lines: string[], skillNames: Set<string>): Pars
187
197
  } else if (itemType === "agent_message") {
188
198
  const text = (item.text as string) ?? "";
189
199
  if (text) agentMessages.push(text.slice(0, 500));
190
- detectTriggeredSkills(text);
191
200
  } else if (itemType === "reasoning") {
192
201
  toolCalls.reasoning = (toolCalls.reasoning ?? 0) + 1;
193
202
  }
194
203
  }
195
204
 
196
- // Detect skill names in text on completed events (whole-word match)
197
- const textContent = ((item.text as string) ?? "") + ((item.command as string) ?? "");
198
- if (etype === "item.completed") {
199
- detectTriggeredSkills(textContent);
205
+ if (etype === "item.completed" && itemType === "command_execution") {
206
+ detectExplicitSkillReads(item.command);
200
207
  }
201
208
  } else if (etype === "response_item") {
202
209
  const payload = (event.payload as Record<string, unknown>) ?? {};
203
210
  const itemType = (payload.type as string) ?? "";
204
211
  if (itemType === "function_call") {
205
- detectTriggeredSkills(payload.arguments);
212
+ detectExplicitSkillReads(payload.arguments);
206
213
  } else if (itemType === "message") {
207
- const content = Array.isArray(payload.content)
214
+ const parts = Array.isArray(payload.content)
208
215
  ? payload.content
209
216
  .map((part) =>
210
217
  typeof part === "object" && part
211
218
  ? (((part as Record<string, unknown>).text as string | undefined) ?? "")
212
219
  : "",
213
220
  )
214
- .join("\n")
215
- : "";
221
+ .filter(Boolean)
222
+ : [];
223
+ const content = parts.join("\n");
216
224
  rememberSessionSkillNames(content);
217
- if ((payload.role as string) === "assistant") {
218
- detectTriggeredSkills(content);
219
- } else if ((payload.role as string) === "user") {
220
- detectExplicitPromptSkillMentions(content);
225
+ if ((payload.role as string) === "user") {
226
+ for (const part of parts) {
227
+ detectExplicitPromptSkillMentions(part);
228
+ }
221
229
  }
222
230
  } else if (itemType === "agent_reasoning") {
223
- detectTriggeredSkills(payload.text);
231
+ detectExplicitSkillReads(payload.text);
224
232
  }
225
233
  } else if (etype === "error") {
226
234
  errors += 1;
@@ -54,7 +54,7 @@ export function openDb(dbPath: string = DB_PATH): Database {
54
54
  const msg = err instanceof Error ? err.message : String(err);
55
55
  if (msg.includes("duplicate column")) continue; // expected on subsequent runs
56
56
  throw new Error(
57
- `Schema migration failed: ${msg}. Export first with 'selftune export', then remove '${dbPath}' and rerun 'selftune sync --force' or 'selftune dashboard'.`,
57
+ `Schema migration failed: ${msg}. Export first with 'selftune export', then remove '${dbPath}' and rerun 'selftune sync --force'. If you need legacy/export JSONL backfill, run 'selftune recover --full --force'.`,
58
58
  );
59
59
  }
60
60
  }
@@ -67,7 +67,7 @@ export function openDb(dbPath: string = DB_PATH): Database {
67
67
  const msg = err instanceof Error ? err.message : String(err);
68
68
  if (msg.includes("already exists")) continue; // expected on subsequent runs
69
69
  throw new Error(
70
- `Schema index creation failed: ${msg}. Export first with 'selftune export', then remove '${dbPath}' and rerun 'selftune sync --force' or 'selftune dashboard'.`,
70
+ `Schema index creation failed: ${msg}. Export first with 'selftune export', then remove '${dbPath}' and rerun 'selftune sync --force'. If you need legacy/export JSONL backfill, run 'selftune recover --full --force'.`,
71
71
  );
72
72
  }
73
73
  }