skillar 0.0.0-stage → 0.2.0

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 (114) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +194 -2
  3. package/dist/analysis/finding.js +190 -0
  4. package/dist/analysis/tree.js +157 -0
  5. package/dist/analysis/usage.js +394 -0
  6. package/dist/api/flows.js +285 -0
  7. package/dist/api/routes.js +192 -0
  8. package/dist/api/security.js +86 -0
  9. package/dist/api/sse.js +55 -0
  10. package/dist/api/static.js +33 -0
  11. package/dist/catalog/fields.js +159 -0
  12. package/dist/catalog/index.js +133 -0
  13. package/dist/catalog/scanner.js +204 -0
  14. package/dist/catalog/uri.js +43 -0
  15. package/dist/catalog/watcher.js +59 -0
  16. package/dist/cli/config-writer.js +255 -0
  17. package/dist/cli/doctor.js +268 -0
  18. package/dist/cli/eval.js +64 -0
  19. package/dist/cli/export-import.js +68 -0
  20. package/dist/cli/init.js +272 -0
  21. package/dist/cli/main.js +165 -0
  22. package/dist/cli/open.js +24 -0
  23. package/dist/cli/serve.js +147 -0
  24. package/dist/config.js +92 -0
  25. package/dist/dashboard/assets/app.js +354 -0
  26. package/dist/dashboard/assets/chunk-5DFWNJC2.js +1 -0
  27. package/dist/dashboard/assets/chunk-S6B2HJQA.js +17 -0
  28. package/dist/dashboard/assets/styles.css +542 -0
  29. package/dist/dashboard/index.html +15 -0
  30. package/dist/fs-watchdog.js +35 -0
  31. package/dist/herdr/client.js +205 -0
  32. package/dist/herdr/sync.js +342 -0
  33. package/dist/ingest/attribution.js +80 -0
  34. package/dist/ingest/hooks.js +168 -0
  35. package/dist/ingest/identity.js +4 -0
  36. package/dist/ingest/mcp-recorder.js +41 -0
  37. package/dist/ingest/otlp-traces.js +106 -0
  38. package/dist/ingest/otlp.js +314 -0
  39. package/dist/ingest/pipeline.js +119 -0
  40. package/dist/ingest/redact.js +36 -0
  41. package/dist/ingest/runtimes.js +25 -0
  42. package/dist/mcp/dispatch.js +54 -0
  43. package/dist/mcp/http.js +112 -0
  44. package/dist/mcp/legacy.js +61 -0
  45. package/dist/mcp/pagination.js +17 -0
  46. package/dist/mcp/protocol.js +121 -0
  47. package/dist/mcp/skills-extension.js +77 -0
  48. package/dist/mcp/stdio-proxy.js +108 -0
  49. package/dist/mcp/tools.js +59 -0
  50. package/dist/packs/claude-code.js +176 -0
  51. package/dist/packs/codex.js +207 -0
  52. package/dist/packs/cursor.js +97 -0
  53. package/dist/packs/gajae.js +7 -0
  54. package/dist/packs/gemini.js +90 -0
  55. package/dist/packs/json-config.js +55 -0
  56. package/dist/packs/omo.js +154 -0
  57. package/dist/packs/openclaw.js +132 -0
  58. package/dist/packs/registry.js +13 -0
  59. package/dist/search/fts.js +61 -0
  60. package/dist/search/fusion.js +50 -0
  61. package/dist/search/guidance.js +7 -0
  62. package/dist/search/index.js +74 -0
  63. package/dist/search/lexical.js +69 -0
  64. package/dist/server.js +267 -0
  65. package/dist/store/automation.js +18 -0
  66. package/dist/store/db.js +134 -0
  67. package/dist/store/execution.js +157 -0
  68. package/dist/store/export.js +269 -0
  69. package/dist/store/index.js +14 -0
  70. package/dist/store/migrations/002_tracing.sql +94 -0
  71. package/dist/store/migrations/003_tracing_indexes.sql +11 -0
  72. package/dist/store/migrations/004_load_reconciliation.sql +50 -0
  73. package/dist/store/migrations/005_execution_boundaries.sql +6 -0
  74. package/dist/store/migrations/006_recording_order.sql +72 -0
  75. package/dist/store/queries.js +643 -0
  76. package/dist/store/schema.sql +96 -0
  77. package/dist/store/workflow.js +250 -0
  78. package/dist/store/writes.js +315 -0
  79. package/dist/types.js +1 -0
  80. package/dist/version.js +19 -0
  81. package/dist/workflow/compare.js +68 -0
  82. package/dist/workflow/routes.js +100 -0
  83. package/dist/workflow/spec.js +40 -0
  84. package/docs/execution-ownership.md +98 -0
  85. package/docs/mcp-conformance.md +156 -0
  86. package/docs/skill-tracing.md +294 -0
  87. package/package.json +48 -4
  88. package/packs/claude-code/README.md +5 -0
  89. package/packs/claude-code/settings.template.json +34 -0
  90. package/packs/codex/README.md +8 -0
  91. package/packs/codex/config.template.toml +12 -0
  92. package/packs/cursor/hooks.example.json +8 -0
  93. package/packs/cursor/mcp.example.json +8 -0
  94. package/packs/gajae/extension/discovery.md +5 -0
  95. package/packs/gajae/extension/index.js +52 -0
  96. package/packs/gemini/settings.example.json +16 -0
  97. package/packs/hook-post.mjs +37 -0
  98. package/packs/omo/extension/discovery.md +5 -0
  99. package/packs/omo/extension/index.js +52 -0
  100. package/packs/openclaw/.codex-plugin/plugin.json +11 -0
  101. package/packs/openclaw/.mcp.json +12 -0
  102. package/packs/openclaw/hooks/skillar-discovery/HOOK.md +8 -0
  103. package/packs/openclaw/hooks/skillar-discovery/handler.js +30 -0
  104. package/packs/openclaw/index.mjs +55 -0
  105. package/packs/openclaw/openclaw.plugin.json +22 -0
  106. package/packs/openclaw/package.json +11 -0
  107. package/packs/openclaw/skillar.json +1 -0
  108. package/src/store/migrations/002_tracing.sql +94 -0
  109. package/src/store/migrations/003_tracing_indexes.sql +11 -0
  110. package/src/store/migrations/004_load_reconciliation.sql +50 -0
  111. package/src/store/migrations/005_execution_boundaries.sql +6 -0
  112. package/src/store/migrations/006_recording_order.sql +72 -0
  113. package/src/store/schema.sql +96 -0
  114. package/src/store/task-types.json +15 -0
@@ -0,0 +1,643 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { automationOf, NO_ACTIVITY } from "./automation.js";
3
+ import { executionEventScope } from "./execution.js";
4
+ import { nameFromUri, parseJson, readIngestCounters, storeError } from "./writes.js";
5
+ const DAY_MS = 86_400_000;
6
+ const WEEK_MS = 7 * DAY_MS;
7
+ const RUNTIMES = ["claude-code", "codex", "gemini", "cursor", "omo", "gajae", "openclaw", "unknown"];
8
+ let rulesCache;
9
+ /** tsc does not copy JSON into dist, so the rules are read beside the compiled module first, then from src/store. */
10
+ export function taskTypeRules() {
11
+ if (rulesCache)
12
+ return rulesCache;
13
+ const candidates = [new URL("./task-types.json", import.meta.url), new URL("../../src/store/task-types.json", import.meta.url)];
14
+ const found = candidates.find((u) => existsSync(u));
15
+ if (!found)
16
+ throw storeError("task_types_missing", "task-types.json not found next to the store module or in src/store");
17
+ rulesCache = JSON.parse(readFileSync(found, "utf8"));
18
+ return rulesCache;
19
+ }
20
+ function tokens(s) {
21
+ return s.toLowerCase().split(/[^\p{L}\p{N}]+/u).filter(Boolean);
22
+ }
23
+ function keywordInText(keyword, text) {
24
+ const k = keyword.toLowerCase();
25
+ if (/^[\x20-\x7e]+$/.test(k)) {
26
+ const escaped = k.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
27
+ return new RegExp(`(^|[^a-z0-9])${escaped}($|[^a-z0-9])`).test(text);
28
+ }
29
+ return text.includes(k);
30
+ }
31
+ function skillMatches(rule, skill) {
32
+ const tags = skill.tags.map((t) => t.toLowerCase());
33
+ if (rule.anyTags?.some((t) => tags.includes(t.toLowerCase())))
34
+ return true;
35
+ if (skill.groupId && rule.anyGroup?.some((g) => g.toLowerCase() === skill.groupId.toLowerCase()))
36
+ return true;
37
+ const nameTokens = tokens(skill.name);
38
+ const name = skill.name.toLowerCase();
39
+ return rule.anyNameKeywords?.some((k) => nameTokens.includes(k.toLowerCase()) || (/[-_ ]/.test(k) && name.includes(k.toLowerCase()))) ?? false;
40
+ }
41
+ export function classifyTaskType(skill, prompt, rules = taskTypeRules()) {
42
+ if (skill) {
43
+ const hit = rules.find((r) => skillMatches(r, skill));
44
+ if (hit)
45
+ return hit.taskType;
46
+ if (skill.groupId)
47
+ return skill.groupId;
48
+ }
49
+ if (prompt) {
50
+ const text = prompt.toLowerCase();
51
+ const hit = rules.find((r) => r.anyPromptKeywords?.some((k) => keywordInText(k, text)));
52
+ if (hit)
53
+ return hit.taskType;
54
+ }
55
+ return "unclassified";
56
+ }
57
+ /** Sums the buckets a runtime reports without double counting: Codex counts cached tokens inside inputTokens, Claude Code does not. */
58
+ function llmTokens(data) {
59
+ const n = (value) => (typeof value === "number" && Number.isFinite(value) ? value : 0);
60
+ if (typeof data.totalTokens === "number")
61
+ return data.totalTokens;
62
+ const base = n(data.inputTokens) + n(data.outputTokens);
63
+ return data.inputIncludesCache === true ? base : base + n(data.cacheReadTokens) + n(data.cacheCreationTokens);
64
+ }
65
+ const fmtCache = new Map();
66
+ const WEEKDAYS = { Sun: 0, Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6 };
67
+ export function localDowHour(ts, tz) {
68
+ let fmt = fmtCache.get(tz);
69
+ if (!fmt) {
70
+ try {
71
+ fmt = new Intl.DateTimeFormat("en-US", { timeZone: tz, weekday: "short", hour: "numeric", hourCycle: "h23" });
72
+ }
73
+ catch {
74
+ throw storeError("invalid_tz", `unknown time zone: ${tz}`);
75
+ }
76
+ fmtCache.set(tz, fmt);
77
+ }
78
+ let dow = 0;
79
+ let hour = 0;
80
+ for (const p of fmt.formatToParts(ts)) {
81
+ if (p.type === "weekday")
82
+ dow = WEEKDAYS[p.value] ?? 0;
83
+ else if (p.type === "hour")
84
+ hour = Number(p.value) % 24;
85
+ }
86
+ return { dow, hour };
87
+ }
88
+ function candidatesOf(data) {
89
+ return Array.isArray(data.candidates) ? data.candidates : [];
90
+ }
91
+ /** Adoption precedence: loaded > exposed_only > no_match > aborted (error after a search) > not_used. */
92
+ export function adoptionState(events) {
93
+ if (events.some((e) => e.kind === "load"))
94
+ return "loaded";
95
+ const searches = events.filter((e) => e.kind === "search");
96
+ if (searches.some((e) => candidatesOf(e.data).length >= 1))
97
+ return "exposed_only";
98
+ if (searches.some((e) => e.data.abstained !== undefined && e.data.abstained !== null))
99
+ return "no_match";
100
+ const firstSearch = events.findIndex((e) => e.kind === "search");
101
+ if (firstSearch >= 0 && events.slice(firstSearch + 1).some((e) => e.kind === "error"))
102
+ return "aborted";
103
+ return "not_used";
104
+ }
105
+ function isoWeekStart(ts) {
106
+ const d = new Date(ts);
107
+ const mondayOffset = (d.getUTCDay() + 6) % 7;
108
+ return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()) - mondayOffset * DAY_MS;
109
+ }
110
+ function isoWeekLabel(weekStart) {
111
+ const thursday = new Date(weekStart + 3 * DAY_MS);
112
+ const year = thursday.getUTCFullYear();
113
+ const week1 = isoWeekStart(Date.UTC(year, 0, 4));
114
+ const week = 1 + Math.round((weekStart - week1) / WEEK_MS);
115
+ return `${year}-W${String(week).padStart(2, "0")}`;
116
+ }
117
+ function shortDigest(d) {
118
+ const s = typeof d === "string" ? d : "";
119
+ return s.replace(/^sha256[:-]/, "").slice(0, 12);
120
+ }
121
+ /** The automation id of a session, or undefined when a person drove it: the rule matching its first prompt, else
122
+ * NO_ACTIVITY for a session that showed no prompt and no skill activity at all (e.g. a thread a daemon reopened). */
123
+ function automationIdOf(firstPrompt, active) {
124
+ return automationOf(firstPrompt) ?? (active ? undefined : NO_ACTIVITY);
125
+ }
126
+ export function createQueries(ctx) {
127
+ const { db } = ctx;
128
+ const defaultTz = () => ctx.opts.tz ?? Intl.DateTimeFormat().resolvedOptions().timeZone;
129
+ function skillMeta() {
130
+ const rows = db.prepare("SELECT uri, name, tags_json, group_id FROM skills").all();
131
+ return new Map(rows.map((r) => [r.uri, { uri: r.uri, name: r.name, tags: parseJson(r.tags_json, []), ...(r.group_id ? { groupId: r.group_id } : {}) }]));
132
+ }
133
+ function metaFor(skills, uri) {
134
+ return skills.get(uri) ?? { uri, name: nameFromUri(uri), tags: [] };
135
+ }
136
+ function loadSessions(f, ignoreCellFilter = false) {
137
+ const where = [];
138
+ const params = [];
139
+ if (f.from !== undefined) {
140
+ where.push("started_at >= ?");
141
+ params.push(f.from);
142
+ }
143
+ if (f.to !== undefined) {
144
+ where.push("started_at < ?");
145
+ params.push(f.to);
146
+ }
147
+ if (f.runtime && f.runtime.length > 0) {
148
+ where.push(`runtime IN (${f.runtime.map(() => "?").join(",")})`);
149
+ params.push(...f.runtime);
150
+ }
151
+ const clause = where.length ? `WHERE ${where.join(" AND ")}` : "";
152
+ const rows = db.prepare(`SELECT id, runtime, started_at, ended_at, first_prompt FROM sessions ${clause}`).all(...params);
153
+ // Model calls and tool runs say nothing about adoption state and outnumber every other kind, so sessions are built without them.
154
+ const events = db.prepare(`SELECT * FROM events WHERE session_id IN (SELECT id FROM sessions ${clause}) AND kind NOT IN ('llm_call', 'tool_call')
155
+ ORDER BY ts, rowid`).all(...params);
156
+ const bySession = new Map();
157
+ for (const e of events) {
158
+ const list = bySession.get(e.session_id);
159
+ if (list)
160
+ list.push(e);
161
+ else
162
+ bySession.set(e.session_id, [e]);
163
+ }
164
+ const tz = f.tz ?? defaultTz();
165
+ const out = [];
166
+ for (const r of rows) {
167
+ const evs = bySession.get(r.id) ?? [];
168
+ const parsed = evs.map((e) => ({ kind: e.kind, data: parseJson(e.data_json, {}) }));
169
+ const loaded = [];
170
+ const exposed = [];
171
+ let firstTop;
172
+ const texts = [];
173
+ evs.forEach((e, i) => {
174
+ const data = parsed[i].data;
175
+ if (e.kind === "load") {
176
+ const uri = e.skill_uri ?? (typeof data.uri === "string" ? data.uri : undefined);
177
+ if (uri && !loaded.includes(uri))
178
+ loaded.push(uri);
179
+ }
180
+ else if (e.kind === "search") {
181
+ const cands = [...candidatesOf(data)].sort((a, b) => a.rank - b.rank);
182
+ if (firstTop === undefined && cands[0])
183
+ firstTop = cands[0].uri;
184
+ for (const c of cands)
185
+ if (!exposed.includes(c.uri))
186
+ exposed.push(c.uri);
187
+ if (typeof data.query === "string")
188
+ texts.push(data.query);
189
+ }
190
+ else if (e.kind === "prompt" && e.text)
191
+ texts.push(e.text);
192
+ });
193
+ const attributed = evs.find((e) => e.attribution_method !== "none");
194
+ const info = {
195
+ id: r.id, runtime: r.runtime, startedAt: r.started_at, events: evs, loaded, exposed, state: adoptionState(parsed),
196
+ attribution: attributed?.attribution_method ?? "none", texts,
197
+ ...(r.ended_at !== null ? { endedAt: r.ended_at } : {}),
198
+ ...(r.first_prompt !== null ? { firstPrompt: r.first_prompt } : {}),
199
+ ...(firstTop !== undefined ? { firstTop } : {}),
200
+ };
201
+ // A session with no prompt and no skill activity (e.g. a thread the Codex daemon reopened) tells nothing about usage.
202
+ const active = loaded.length > 0 || exposed.length > 0
203
+ || evs.some((e) => e.kind === "prompt" || e.kind === "native_skill" || e.kind === "read_file");
204
+ const automated = automationIdOf(info.firstPrompt, active);
205
+ if (automated !== undefined)
206
+ info.automated = automated;
207
+ if (f.automated === undefined && automated !== undefined)
208
+ continue;
209
+ if (f.automated === "only" && automated === undefined)
210
+ continue;
211
+ if (f.state && f.state.length > 0 && !f.state.includes(info.state))
212
+ continue;
213
+ if (f.skill && f.skill.length > 0 && !f.skill.some((u) => loaded.includes(u) || exposed.includes(u)))
214
+ continue;
215
+ if (f.q) {
216
+ const q = f.q.toLowerCase();
217
+ if (![info.firstPrompt ?? "", ...texts].some((t) => t.toLowerCase().includes(q)))
218
+ continue;
219
+ }
220
+ if (!ignoreCellFilter && (f.dow !== undefined || f.hour !== undefined)) {
221
+ const local = localDowHour(r.started_at, tz);
222
+ if (f.dow !== undefined && local.dow !== f.dow)
223
+ continue;
224
+ if (f.hour !== undefined) {
225
+ const span = f.hourSpan ?? 1;
226
+ if (local.hour < f.hour || local.hour >= f.hour + span)
227
+ continue;
228
+ }
229
+ }
230
+ out.push(info);
231
+ }
232
+ return out;
233
+ }
234
+ function sessionSkills(s) {
235
+ if (s.loaded.length > 0)
236
+ return s.loaded;
237
+ return s.firstTop ? [s.firstTop] : [];
238
+ }
239
+ const queries = {
240
+ treeSessionData(sessionId, scope) {
241
+ const session = db.prepare(`SELECT id, runtime, started_at AS startedAt, ended_at AS endedAt
242
+ FROM sessions WHERE id = ?`).get(sessionId);
243
+ if (!session)
244
+ return undefined;
245
+ const rows = db.prepare(`SELECT id, ts, kind, source, turn_key AS turnKey, call_id AS callId, span_id AS spanId,
246
+ skill_uri AS skillUri, text, text_length AS textLength, data_json FROM events WHERE session_id = ? ORDER BY ts, rowid`)
247
+ .all(sessionId);
248
+ // Broker traces can differ from the turn trace. Scope call ids to this session's conversation (when supplied),
249
+ // and use the log's exact trace/span pair rather than treating a trace id as a turn id.
250
+ const spans = db.prepare(`WITH session AS (
251
+ SELECT id, runtime, runtime_session_id, conversation_id FROM sessions WHERE id = ?
252
+ ), matched AS (
253
+ SELECT o.trace_id, o.span_id FROM session s CROSS JOIN otel_spans o
254
+ WHERE o.conversation_id IN (s.runtime_session_id, s.conversation_id) AND o.runtime = s.runtime
255
+ UNION
256
+ SELECT o.trace_id, o.span_id FROM session s JOIN events e ON e.session_id = s.id CROSS JOIN otel_spans o
257
+ WHERE o.call_id = e.call_id AND o.runtime = s.runtime
258
+ AND (o.conversation_id IS NULL OR o.conversation_id IN (s.runtime_session_id, s.conversation_id))
259
+ UNION
260
+ SELECT o.trace_id, o.span_id FROM session s JOIN events e ON e.session_id = s.id CROSS JOIN otel_spans o
261
+ WHERE o.trace_id = json_extract(e.data_json, '$.otelTraceId') AND o.span_id = e.span_id AND o.runtime = s.runtime
262
+ AND (o.conversation_id IS NULL OR o.conversation_id IN (s.runtime_session_id, s.conversation_id))
263
+ ) SELECT o.trace_id AS traceId, o.span_id AS spanId, o.name, o.runtime, o.received_at AS receivedAt,
264
+ o.parent_span_id AS parentSpanId, o.conversation_id AS conversationId, o.turn_key AS turnKey,
265
+ o.call_id AS callId, o.start_ms AS startMs, o.end_ms AS endMs
266
+ FROM matched m JOIN otel_spans o ON o.trace_id = m.trace_id AND o.span_id = m.span_id
267
+ ORDER BY o.start_ms, o.trace_id, o.span_id`).all(sessionId);
268
+ const owned = scope ? new Set(scope.eventIds) : undefined;
269
+ const prior = scope ? new Set(scope.priorEventIds) : undefined;
270
+ const spanKeys = scope?.spanKeys ? new Set(scope.spanKeys) : undefined;
271
+ // Retain earlier context for continuing turn identity, but never let B's prompts reassign A's calls.
272
+ const events = rows.filter((row) => !scope || row.ts < scope.from ||
273
+ (row.ts === scope.from && row.kind === "prompt" && prior?.has(row.id)) || owned?.has(row.id));
274
+ return { ...session, endedAt: scope?.sessionEnds?.[sessionId] ?? session.endedAt,
275
+ events: events.map(({ data_json, ...row }) => ({ ...row, data: parseJson(data_json, {}) })),
276
+ spans: spans.filter((s) => scope?.to == null || (s.startMs !== null && s.startMs < scope.to)
277
+ || spanKeys?.has(JSON.stringify([s.traceId, s.spanId])))
278
+ .map((s) => ({ traceId: s.traceId, spanId: s.spanId, name: s.name, runtime: s.runtime, receivedAt: s.receivedAt,
279
+ ...(s.parentSpanId !== null ? { parentSpanId: s.parentSpanId } : {}), ...(s.conversationId !== null ? { conversationId: s.conversationId } : {}),
280
+ ...(s.turnKey !== null ? { turnKey: s.turnKey } : {}), ...(s.callId !== null ? { callId: s.callId } : {}),
281
+ ...(s.startMs !== null ? { startMs: s.startMs } : {}), ...(s.endMs !== null ? { endMs: s.endMs } : {}) })) };
282
+ },
283
+ treeFlowData(flowOrWorkspaceId, scope) {
284
+ const flow = db.prepare("SELECT id, workspace_id, tab_id, label, registered_ms, ended_ms FROM flows WHERE id = ?").get(flowOrWorkspaceId);
285
+ const workspaceId = flow?.workspace_id ?? flowOrWorkspaceId;
286
+ const workspace = db.prepare("SELECT id, label FROM herdr_workspaces WHERE id = ?").get(workspaceId);
287
+ if (!workspace)
288
+ return undefined;
289
+ if (flow) {
290
+ const { paneIds, links } = scope ?? executionEventScope(db, flow);
291
+ const panes = paneIds.flatMap((id) => {
292
+ const pane = db.prepare(`SELECT id, tab_id AS tabId, label, created_ms AS createdMs, closed_ms AS closedMs
293
+ FROM herdr_panes WHERE id = ?`).get(id);
294
+ return pane ? [pane] : [];
295
+ });
296
+ const tabIds = [...new Set(panes.map((p) => p.tabId))];
297
+ const tabs = tabIds.flatMap((id) => {
298
+ const tab = db.prepare(`SELECT id, label, created_ms AS createdMs, closed_ms AS closedMs FROM herdr_tabs WHERE id = ?`)
299
+ .get(id);
300
+ return tab ? [tab] : [];
301
+ });
302
+ return { id: flow.id, workspaceId, label: flow.label ?? workspace.label, registered: true, tabs, panes, links };
303
+ }
304
+ const tabId = null;
305
+ const tabs = db.prepare(`SELECT id, label, created_ms AS createdMs, closed_ms AS closedMs FROM herdr_tabs
306
+ WHERE workspace_id = ? AND (? IS NULL OR id = ?) ORDER BY created_ms, id`).all(workspaceId, tabId, tabId);
307
+ const panes = db.prepare(`SELECT id, tab_id AS tabId, label, created_ms AS createdMs, closed_ms AS closedMs FROM herdr_panes
308
+ WHERE workspace_id = ? AND (? IS NULL OR tab_id = ?) ORDER BY created_ms, id`).all(workspaceId, tabId, tabId);
309
+ const links = db.prepare(`SELECT ps.pane_id AS paneId, ps.session_id AS sessionId, ps.agent_session_value AS agentSessionValue,
310
+ ps.source, ps.first_seen_ms AS firstSeenMs, ps.last_seen_ms AS lastSeenMs
311
+ FROM pane_sessions ps JOIN herdr_panes p ON p.id = ps.pane_id
312
+ WHERE p.workspace_id = ? AND (? IS NULL OR p.tab_id = ?)
313
+ ORDER BY ps.first_seen_ms, ps.agent_session_value`).all(workspaceId, tabId, tabId);
314
+ return { id: workspaceId, workspaceId, label: workspace.label, registered: false, tabs, panes, links };
315
+ },
316
+ /**
317
+ * The automation id of each automated session among the ids given; a session a person drove is absent from the
318
+ * result. The read APIs filter whole flows with it without loading every session of a range, so it reads the
319
+ * same columns loadSessions classifies on, bounded by the id list and chunked under SQLite's parameter limit.
320
+ */
321
+ sessionAutomation(ids) {
322
+ const out = {};
323
+ const unique = [...new Set(ids)];
324
+ for (let i = 0; i < unique.length; i += 400) {
325
+ const chunk = unique.slice(i, i + 400);
326
+ const holes = chunk.map(() => "?").join(",");
327
+ const rows = db.prepare(`SELECT id, first_prompt FROM sessions WHERE id IN (${holes})`).all(...chunk);
328
+ const evs = db.prepare(`SELECT session_id, kind, skill_uri, data_json FROM events
329
+ WHERE session_id IN (${holes}) AND kind IN ('prompt', 'native_skill', 'read_file', 'load', 'search')`).all(...chunk);
330
+ const active = new Set();
331
+ for (const e of evs) {
332
+ const data = e.kind === "load" || e.kind === "search" ? parseJson(e.data_json, {}) : {};
333
+ const counts = e.kind === "search" ? candidatesOf(data).length > 0
334
+ : e.kind === "load" ? e.skill_uri !== null || typeof data.uri === "string"
335
+ : true;
336
+ if (counts)
337
+ active.add(e.session_id);
338
+ }
339
+ for (const row of rows) {
340
+ const automated = automationIdOf(row.first_prompt ?? undefined, active.has(row.id));
341
+ if (automated !== undefined)
342
+ out[row.id] = automated;
343
+ }
344
+ }
345
+ return out;
346
+ },
347
+ outsideHerdrSessions({ from, to, limit, automated }) {
348
+ const kept = [];
349
+ const cap = Math.max(1, Math.min(100, Math.floor(limit)));
350
+ let cursor;
351
+ // Classify before the result cap: a page of automated sessions must not hide older human sessions.
352
+ // Each read and classification is bounded; no session tree or full event population is loaded here.
353
+ for (;;) {
354
+ const rows = db.prepare(`SELECT * FROM (
355
+ SELECT s.id, MAX(s.started_at, COALESCE(s.ended_at, s.started_at),
356
+ COALESCE((SELECT MAX(e.ts) FROM events e WHERE e.session_id = s.id), s.started_at)) AS activityMs
357
+ FROM sessions s WHERE NOT EXISTS (
358
+ SELECT 1 FROM pane_sessions ps WHERE ps.session_id = s.id AND COALESCE(ps.source, '') <> 'unlinked')
359
+ ) WHERE activityMs >= ? AND activityMs < ?
360
+ ${cursor ? "AND (activityMs < ? OR (activityMs = ? AND id > ?))" : ""}
361
+ ORDER BY activityMs DESC, id LIMIT 100`).all(from, to, ...(cursor ? [cursor.activityMs, cursor.activityMs, cursor.id] : []));
362
+ const automation = automated === "include" ? {} : queries.sessionAutomation(rows.map((row) => row.id));
363
+ for (const row of rows) {
364
+ const isAutomated = automation[row.id] !== undefined;
365
+ if (automated === "include" || (automated === "only" ? isAutomated : !isAutomated))
366
+ kept.push(row);
367
+ if (kept.length === cap)
368
+ return kept;
369
+ }
370
+ if (rows.length < 100)
371
+ return kept;
372
+ cursor = rows.at(-1);
373
+ }
374
+ },
375
+ outsideHerdrSessionIds({ from, to, limit }) {
376
+ return db.prepare(`SELECT s.id FROM sessions s WHERE s.started_at >= ? AND s.started_at < ?
377
+ AND NOT EXISTS (SELECT 1 FROM pane_sessions ps WHERE ps.session_id = s.id)
378
+ ORDER BY s.started_at DESC, s.id LIMIT ?`).all(from, to, Math.max(1, Math.min(100, Math.floor(limit)))).map((s) => s.id);
379
+ },
380
+ sessions(f) {
381
+ return loadSessions(f)
382
+ .sort((a, b) => b.startedAt - a.startedAt)
383
+ .map((s) => ({
384
+ id: s.id, runtime: s.runtime, startedAt: s.startedAt, skills: sessionSkills(s), state: s.state, attribution: s.attribution,
385
+ ...(s.endedAt !== undefined ? { endedAt: s.endedAt } : {}),
386
+ ...(s.firstPrompt !== undefined ? { firstPrompt: s.firstPrompt } : {}),
387
+ ...(s.automated !== undefined ? { automated: s.automated } : {}),
388
+ }));
389
+ },
390
+ trace(sessionId) {
391
+ const evs = db.prepare("SELECT * FROM events WHERE session_id = ? ORDER BY ts, rowid").all(sessionId);
392
+ const skills = skillMeta();
393
+ const loadsByTrace = new Map();
394
+ for (const e of evs) {
395
+ if (e.kind !== "load" || !e.trace_id || !e.skill_uri)
396
+ continue;
397
+ const list = loadsByTrace.get(e.trace_id) ?? [];
398
+ list.push({ ts: e.ts, uri: e.skill_uri });
399
+ loadsByTrace.set(e.trace_id, list);
400
+ }
401
+ const loadRow = db.prepare("SELECT uri, manifest_digest FROM loads WHERE load_id = ?");
402
+ return evs.map((e) => {
403
+ const data = parseJson(e.data_json, {});
404
+ switch (e.kind) {
405
+ case "prompt": {
406
+ const textLength = e.text_length ?? e.text?.length;
407
+ const summary = e.text ? e.text.slice(0, 200) : `(prompt not captured${textLength !== undefined ? `, ${textLength} chars` : ""})`;
408
+ return { ts: e.ts, kind: e.kind, summary, detail: { kind: "prompt", ...(e.text !== null ? { text: e.text } : {}), ...(textLength !== undefined ? { textLength } : {}) } };
409
+ }
410
+ case "search": {
411
+ const traceId = e.trace_id ?? (typeof data.traceId === "string" ? data.traceId : "");
412
+ const later = (loadsByTrace.get(traceId) ?? []).filter((l) => l.ts >= e.ts).map((l) => l.uri);
413
+ const cands = candidatesOf(data).map((c) => ({ ...c, chosen: later.includes(c.uri) }));
414
+ const chosen = cands.filter((c) => c.chosen).map((c) => c.uri);
415
+ const query = typeof data.query === "string" ? data.query : "";
416
+ const abstained = data.abstained;
417
+ const summary = abstained
418
+ ? `search "${query}" abstained (${abstained.reason}) in ${Number(data.elapsedMs ?? 0)} ms`
419
+ : `search "${query}" -> ${cands.length} candidates in ${Number(data.elapsedMs ?? 0)} ms`;
420
+ return { ts: e.ts, kind: e.kind, summary, detail: { ...data, kind: "search", traceId, chosen, candidates: cands } };
421
+ }
422
+ case "load": {
423
+ const loadId = e.load_id ?? (typeof data.loadId === "string" ? data.loadId : "");
424
+ const stored = loadId ? loadRow.get(loadId) : undefined;
425
+ const uri = e.skill_uri ?? stored?.uri ?? (typeof data.uri === "string" ? data.uri : "");
426
+ const manifestDigest = typeof data.manifestDigest === "string" ? data.manifestDigest : stored?.manifest_digest ?? "";
427
+ return { ts: e.ts, kind: e.kind, summary: `load ${metaFor(skills, uri).name} (${uri}) @${shortDigest(manifestDigest)}`,
428
+ detail: { kind: "load", uri, loadId, manifestDigest } };
429
+ }
430
+ case "read_file": {
431
+ const uri = typeof data.uri === "string" ? data.uri : "";
432
+ const bytes = Number(data.bytes ?? data.size ?? 0);
433
+ const skillUri = e.skill_uri ?? (typeof data.skillUri === "string" ? data.skillUri : "");
434
+ return { ts: e.ts, kind: e.kind, summary: `read ${uri} (${bytes} bytes)`, detail: { kind: "read_file", skillUri, uri, bytes } };
435
+ }
436
+ case "native_skill": {
437
+ const name = typeof data.name === "string" ? data.name : e.skill_uri ? nameFromUri(e.skill_uri) : "unknown";
438
+ const via = typeof data.via === "string" ? data.via : "tool";
439
+ return { ts: e.ts, kind: e.kind, summary: `native skill ${name} via ${via}`, detail: { kind: "native_skill", name, via } };
440
+ }
441
+ case "llm_call": {
442
+ const model = typeof data.model === "string" ? data.model : "unknown";
443
+ return { ts: e.ts, kind: e.kind, summary: `model call: ${model}, ${llmTokens(data)} tokens`,
444
+ detail: { ...data, kind: "llm_call", inputIncludesCache: data.inputIncludesCache === true } };
445
+ }
446
+ case "tool_call": {
447
+ const toolName = typeof data.toolName === "string" ? data.toolName : "unknown";
448
+ const durationMs = typeof data.durationMs === "number" ? data.durationMs : undefined;
449
+ const outcome = data.success === true ? "ok" : data.success === false ? "failed" : "unknown";
450
+ return { ts: e.ts, kind: e.kind, summary: `tool: ${toolName} (${durationMs ?? "?"} ms, ${outcome})`,
451
+ detail: { ...data, kind: "tool_call", toolName,
452
+ ...(e.tool_use_id !== null ? { toolUseId: e.tool_use_id } : {}), ...(e.call_id !== null ? { callId: e.call_id } : {}),
453
+ ...(e.text !== null ? { args: e.text } : {}), ...(e.text_length !== null ? { argsLength: e.text_length } : {}) } };
454
+ }
455
+ case "error": {
456
+ const code = typeof data.code === "string" ? data.code : "error";
457
+ const message = typeof data.message === "string" ? data.message : e.text ?? "";
458
+ return { ts: e.ts, kind: e.kind, summary: `error ${code}${message ? `: ${message.slice(0, 200)}` : ""}`, detail: { kind: "error", code, message } };
459
+ }
460
+ default:
461
+ return { ts: e.ts, kind: e.kind, summary: e.kind === "session_start" ? "session started" : "session ended", detail: { kind: e.kind } };
462
+ }
463
+ });
464
+ },
465
+ flow(f, unit, top = 7) {
466
+ const limit = Math.min(15, Math.max(1, Math.floor(top)));
467
+ const sessions = loadSessions(f);
468
+ const skills = skillMeta();
469
+ const paths = [];
470
+ for (const s of sessions) {
471
+ const primary = s.loaded[0] ?? s.firstTop;
472
+ const task = classifyTaskType(primary ? metaFor(skills, primary) : undefined, s.firstPrompt ?? s.texts[0]);
473
+ const skillList = unit === "all_loads" && s.loaded.length > 0 ? s.loaded : primary ? [primary] : ["(none)"];
474
+ for (const sk of skillList)
475
+ paths.push({ session: s.id, task, skill: sk, state: s.state });
476
+ }
477
+ const OTHER = "__other__";
478
+ const otherMembers = [new Set(), new Set(), new Set()];
479
+ const keep = (values, column) => {
480
+ const sessionsBy = new Map();
481
+ paths.forEach((p, i) => {
482
+ const v = values[i];
483
+ const set = sessionsBy.get(v) ?? new Set();
484
+ set.add(p.session);
485
+ sessionsBy.set(v, set);
486
+ });
487
+ const ranked = [...sessionsBy.entries()].sort((a, b) => b[1].size - a[1].size || a[0].localeCompare(b[0]));
488
+ const map = new Map();
489
+ ranked.forEach(([v], i) => {
490
+ if (i < limit)
491
+ map.set(v, v);
492
+ else {
493
+ map.set(v, OTHER);
494
+ otherMembers[column].add(v);
495
+ }
496
+ });
497
+ return map;
498
+ };
499
+ const taskMap = keep(paths.map((p) => p.task), 0);
500
+ const skillMap = keep(paths.map((p) => p.skill), 1);
501
+ const stateMap = keep(paths.map((p) => p.state), 2);
502
+ const mapped = paths.map((p) => ({ session: p.session, state: p.state, task: taskMap.get(p.task), skill: skillMap.get(p.skill), stateNode: stateMap.get(p.state) }));
503
+ const countLinks = () => {
504
+ const left = new Map();
505
+ const right = new Map();
506
+ const add = (m, k, s) => { const set = m.get(k) ?? new Set(); set.add(s); m.set(k, set); };
507
+ for (const p of mapped) {
508
+ add(left, `${p.task}\u0000${p.skill}\u0000${p.state}`, p.session);
509
+ add(right, `${p.skill}\u0000${p.stateNode}\u0000${p.state}`, p.session);
510
+ }
511
+ return { left, right };
512
+ };
513
+ const before = countLinks();
514
+ for (const p of mapped) {
515
+ if (p.skill === OTHER)
516
+ continue;
517
+ const weakLeft = (before.left.get(`${p.task}\u0000${p.skill}\u0000${p.state}`)?.size ?? 0) < 2;
518
+ const weakRight = (before.right.get(`${p.skill}\u0000${p.stateNode}\u0000${p.state}`)?.size ?? 0) < 2;
519
+ if (weakLeft || weakRight) {
520
+ otherMembers[1].add(p.skill);
521
+ p.skill = OTHER;
522
+ }
523
+ }
524
+ const after = countLinks();
525
+ const nodeId = (column, v) => `${["t", "s", "a"][column]}:${v}`;
526
+ const nodeSessions = new Map();
527
+ const touch = (column, v, s) => {
528
+ const id = nodeId(column, v);
529
+ const n = nodeSessions.get(id) ?? { column, value: v, sessions: new Set() };
530
+ n.sessions.add(s);
531
+ nodeSessions.set(id, n);
532
+ };
533
+ for (const p of mapped) {
534
+ touch(0, p.task, p.session);
535
+ touch(1, p.skill, p.session);
536
+ touch(2, p.stateNode, p.session);
537
+ }
538
+ const label = (column, v) => v === OTHER ? `Other (${otherMembers[column].size})` : column === 1 ? (v === "(none)" ? "(no skill)" : metaFor(skills, v).name) : v;
539
+ const nodes = [...nodeSessions.entries()]
540
+ .sort((a, b) => a[1].column - b[1].column || Number(a[1].value === OTHER) - Number(b[1].value === OTHER) || b[1].sessions.size - a[1].sessions.size)
541
+ .map(([id, n]) => ({ id, column: n.column, label: label(n.column, n.value), count: n.sessions.size, ...(n.value === OTHER ? { other: true } : {}) }));
542
+ const links = [];
543
+ for (const [k, set] of after.left) {
544
+ const [task, skill, state] = k.split("\u0000");
545
+ links.push({ source: nodeId(0, task), target: nodeId(1, skill), count: set.size, state });
546
+ }
547
+ for (const [k, set] of after.right) {
548
+ const [skill, stateNode, state] = k.split("\u0000");
549
+ links.push({ source: nodeId(1, skill), target: nodeId(2, stateNode), count: set.size, state });
550
+ }
551
+ links.sort((a, b) => b.count - a.count);
552
+ return { unit, columns: ["task_type", "skill", "state"], nodes, links, sessionCount: sessions.length };
553
+ },
554
+ heatmap(f, tz, weekStart, bucketHours) {
555
+ const buckets = 24 / bucketHours;
556
+ const grid = Array.from({ length: 7 }, () => new Array(buckets).fill(0));
557
+ for (const s of loadSessions(f, true)) {
558
+ const { dow, hour } = localDowHour(s.startedAt, tz);
559
+ grid[dow][Math.floor(hour / bucketHours)] += 1;
560
+ }
561
+ const cells = [];
562
+ for (let i = 0; i < 7; i++) {
563
+ const dow = (weekStart + i) % 7;
564
+ for (let b = 0; b < buckets; b++)
565
+ cells.push({ dow, hour: b * bucketHours, count: grid[dow][b] });
566
+ }
567
+ const rowTotals = grid.map((row) => row.reduce((a, n) => a + n, 0));
568
+ const colTotals = Array.from({ length: buckets }, (_, b) => grid.reduce((a, row) => a + row[b], 0));
569
+ return { tz, weekStart, bucketHours, cells, rowTotals, colTotals };
570
+ },
571
+ skillWeek(f) {
572
+ const lastWeek = isoWeekStart(f.to !== undefined ? f.to - 1 : ctx.now());
573
+ const starts = Array.from({ length: 8 }, (_, i) => lastWeek - (7 - i) * WEEK_MS);
574
+ const from = starts[0];
575
+ const to = lastWeek + WEEK_MS;
576
+ const where = ["kind = 'load'", "skill_uri IS NOT NULL", "ts >= ?", "ts < ?"];
577
+ const params = [from, to];
578
+ if (f.runtime && f.runtime.length > 0) {
579
+ where.push(`runtime IN (${f.runtime.map(() => "?").join(",")})`);
580
+ params.push(...f.runtime);
581
+ }
582
+ if (f.skill && f.skill.length > 0) {
583
+ where.push(`skill_uri IN (${f.skill.map(() => "?").join(",")})`);
584
+ params.push(...f.skill);
585
+ }
586
+ const rows = db.prepare(`SELECT skill_uri, ts FROM events WHERE ${where.join(" AND ")}`).all(...params);
587
+ const skills = skillMeta();
588
+ const counts = new Map();
589
+ for (const r of rows) {
590
+ const idx = Math.floor((r.ts - from) / WEEK_MS);
591
+ const arr = counts.get(r.skill_uri) ?? new Array(8).fill(0);
592
+ arr[idx] += 1;
593
+ counts.set(r.skill_uri, arr);
594
+ }
595
+ const out = [...counts.entries()]
596
+ .map(([uri, c]) => ({ uri, name: metaFor(skills, uri).name, counts: c }))
597
+ .sort((a, b) => b.counts.reduce((x, y) => x + y, 0) - a.counts.reduce((x, y) => x + y, 0) || a.name.localeCompare(b.name));
598
+ return { weeks: starts.map(isoWeekLabel), rows: out };
599
+ },
600
+ stats(f = {}) {
601
+ const attributionRate = Object.fromEntries(RUNTIMES.map((r) => [r, null]));
602
+ const mcp = db.prepare(`SELECT runtime, COUNT(*) AS total, SUM(CASE WHEN session_id != 'unattributed' THEN 1 ELSE 0 END) AS attributed
603
+ FROM events WHERE source = 'mcp' GROUP BY runtime`).all();
604
+ for (const r of mcp)
605
+ attributionRate[r.runtime] = r.total > 0 ? r.attributed / r.total : null;
606
+ const where = ["source = 'mcp'", "session_id = 'unattributed'", "kind IN ('search', 'load')"];
607
+ const params = [];
608
+ if (f.from !== undefined) {
609
+ where.push("ts >= ?");
610
+ params.push(f.from);
611
+ }
612
+ if (f.to !== undefined) {
613
+ where.push("ts < ?");
614
+ params.push(f.to);
615
+ }
616
+ if (f.runtime && f.runtime.length > 0) {
617
+ where.push(`runtime IN (${f.runtime.map(() => "?").join(",")})`);
618
+ params.push(...f.runtime);
619
+ }
620
+ const orphan = db.prepare(`SELECT kind, COUNT(*) AS n FROM events WHERE ${where.join(" AND ")} GROUP BY kind`).all(...params);
621
+ const unattributed = { searches: orphan.find((o) => o.kind === "search")?.n ?? 0, loads: orphan.find((o) => o.kind === "load")?.n ?? 0 };
622
+ const automatedSessions = loadSessions({ ...f, automated: "only" }).length;
623
+ const searches = db.prepare("SELECT data_json FROM events WHERE kind = 'search'").all();
624
+ const abstained = searches.filter((s) => parseJson(s.data_json, {}).abstained).length;
625
+ const cutoff = ctx.now() - 56 * DAY_MS;
626
+ const unused = db.prepare(`SELECT uri FROM skills WHERE removed_at IS NULL AND uri NOT IN
627
+ (SELECT skill_uri FROM events WHERE kind = 'load' AND skill_uri IS NOT NULL AND ts >= ?) ORDER BY name`).all(cutoff);
628
+ const last = db.prepare("SELECT MAX(ts) AS ts FROM events").get();
629
+ const byRuntime = db.prepare("SELECT runtime, MAX(ts) AS ts FROM events GROUP BY runtime").all();
630
+ return {
631
+ attributionRate,
632
+ abstainRate: searches.length > 0 ? abstained / searches.length : 0,
633
+ unusedSkills: unused.map((u) => u.uri),
634
+ ...(last.ts !== null ? { lastEventTs: last.ts } : {}),
635
+ lastEventByRuntime: Object.fromEntries(byRuntime.map((r) => [r.runtime, r.ts])),
636
+ ingest: readIngestCounters(db),
637
+ unattributed,
638
+ automatedSessions,
639
+ };
640
+ },
641
+ };
642
+ return queries;
643
+ }