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.
- package/LICENSE +22 -0
- package/README.md +194 -2
- package/dist/analysis/finding.js +190 -0
- package/dist/analysis/tree.js +157 -0
- package/dist/analysis/usage.js +394 -0
- package/dist/api/flows.js +285 -0
- package/dist/api/routes.js +192 -0
- package/dist/api/security.js +86 -0
- package/dist/api/sse.js +55 -0
- package/dist/api/static.js +33 -0
- package/dist/catalog/fields.js +159 -0
- package/dist/catalog/index.js +133 -0
- package/dist/catalog/scanner.js +204 -0
- package/dist/catalog/uri.js +43 -0
- package/dist/catalog/watcher.js +59 -0
- package/dist/cli/config-writer.js +255 -0
- package/dist/cli/doctor.js +268 -0
- package/dist/cli/eval.js +64 -0
- package/dist/cli/export-import.js +68 -0
- package/dist/cli/init.js +272 -0
- package/dist/cli/main.js +165 -0
- package/dist/cli/open.js +24 -0
- package/dist/cli/serve.js +147 -0
- package/dist/config.js +92 -0
- package/dist/dashboard/assets/app.js +354 -0
- package/dist/dashboard/assets/chunk-5DFWNJC2.js +1 -0
- package/dist/dashboard/assets/chunk-S6B2HJQA.js +17 -0
- package/dist/dashboard/assets/styles.css +542 -0
- package/dist/dashboard/index.html +15 -0
- package/dist/fs-watchdog.js +35 -0
- package/dist/herdr/client.js +205 -0
- package/dist/herdr/sync.js +342 -0
- package/dist/ingest/attribution.js +80 -0
- package/dist/ingest/hooks.js +168 -0
- package/dist/ingest/identity.js +4 -0
- package/dist/ingest/mcp-recorder.js +41 -0
- package/dist/ingest/otlp-traces.js +106 -0
- package/dist/ingest/otlp.js +314 -0
- package/dist/ingest/pipeline.js +119 -0
- package/dist/ingest/redact.js +36 -0
- package/dist/ingest/runtimes.js +25 -0
- package/dist/mcp/dispatch.js +54 -0
- package/dist/mcp/http.js +112 -0
- package/dist/mcp/legacy.js +61 -0
- package/dist/mcp/pagination.js +17 -0
- package/dist/mcp/protocol.js +121 -0
- package/dist/mcp/skills-extension.js +77 -0
- package/dist/mcp/stdio-proxy.js +108 -0
- package/dist/mcp/tools.js +59 -0
- package/dist/packs/claude-code.js +176 -0
- package/dist/packs/codex.js +207 -0
- package/dist/packs/cursor.js +97 -0
- package/dist/packs/gajae.js +7 -0
- package/dist/packs/gemini.js +90 -0
- package/dist/packs/json-config.js +55 -0
- package/dist/packs/omo.js +154 -0
- package/dist/packs/openclaw.js +132 -0
- package/dist/packs/registry.js +13 -0
- package/dist/search/fts.js +61 -0
- package/dist/search/fusion.js +50 -0
- package/dist/search/guidance.js +7 -0
- package/dist/search/index.js +74 -0
- package/dist/search/lexical.js +69 -0
- package/dist/server.js +267 -0
- package/dist/store/automation.js +18 -0
- package/dist/store/db.js +134 -0
- package/dist/store/execution.js +157 -0
- package/dist/store/export.js +269 -0
- package/dist/store/index.js +14 -0
- package/dist/store/migrations/002_tracing.sql +94 -0
- package/dist/store/migrations/003_tracing_indexes.sql +11 -0
- package/dist/store/migrations/004_load_reconciliation.sql +50 -0
- package/dist/store/migrations/005_execution_boundaries.sql +6 -0
- package/dist/store/migrations/006_recording_order.sql +72 -0
- package/dist/store/queries.js +643 -0
- package/dist/store/schema.sql +96 -0
- package/dist/store/workflow.js +250 -0
- package/dist/store/writes.js +315 -0
- package/dist/types.js +1 -0
- package/dist/version.js +19 -0
- package/dist/workflow/compare.js +68 -0
- package/dist/workflow/routes.js +100 -0
- package/dist/workflow/spec.js +40 -0
- package/docs/execution-ownership.md +98 -0
- package/docs/mcp-conformance.md +156 -0
- package/docs/skill-tracing.md +294 -0
- package/package.json +48 -4
- package/packs/claude-code/README.md +5 -0
- package/packs/claude-code/settings.template.json +34 -0
- package/packs/codex/README.md +8 -0
- package/packs/codex/config.template.toml +12 -0
- package/packs/cursor/hooks.example.json +8 -0
- package/packs/cursor/mcp.example.json +8 -0
- package/packs/gajae/extension/discovery.md +5 -0
- package/packs/gajae/extension/index.js +52 -0
- package/packs/gemini/settings.example.json +16 -0
- package/packs/hook-post.mjs +37 -0
- package/packs/omo/extension/discovery.md +5 -0
- package/packs/omo/extension/index.js +52 -0
- package/packs/openclaw/.codex-plugin/plugin.json +11 -0
- package/packs/openclaw/.mcp.json +12 -0
- package/packs/openclaw/hooks/skillar-discovery/HOOK.md +8 -0
- package/packs/openclaw/hooks/skillar-discovery/handler.js +30 -0
- package/packs/openclaw/index.mjs +55 -0
- package/packs/openclaw/openclaw.plugin.json +22 -0
- package/packs/openclaw/package.json +11 -0
- package/packs/openclaw/skillar.json +1 -0
- package/src/store/migrations/002_tracing.sql +94 -0
- package/src/store/migrations/003_tracing_indexes.sql +11 -0
- package/src/store/migrations/004_load_reconciliation.sql +50 -0
- package/src/store/migrations/005_execution_boundaries.sql +6 -0
- package/src/store/migrations/006_recording_order.sql +72 -0
- package/src/store/schema.sql +96 -0
- package/src/store/task-types.json +15 -0
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
import { isSkillarMcpData } from "../ingest/identity.js";
|
|
2
|
+
import { flowTree, sessionTree } from "./tree.js";
|
|
3
|
+
/** "Used the skill?" per skill span, computed on read from reported events only; an unknown is `null`, never 0. */
|
|
4
|
+
/** The measurements a model call can report. A runtime that omits one measured nothing, which is never a zero. */
|
|
5
|
+
export const TOKEN_KEYS = ["inputTokens", "outputTokens", "cacheReadTokens", "cacheCreationTokens", "cacheWriteTokens",
|
|
6
|
+
"reasoningTokens", "totalTokens"];
|
|
7
|
+
const KOREAN_CORRECTIONS = ["아니", "다시", "틀렸", "잘못", "그게 아니", "왜 안"];
|
|
8
|
+
const ENGLISH_CORRECTIONS = ["no,", "wrong", "again", "that's not", "undo", "revert"];
|
|
9
|
+
function escapeRegex(text) {
|
|
10
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
11
|
+
}
|
|
12
|
+
/** "again" must not fire inside "against"; "no," carries its own right-hand boundary. */
|
|
13
|
+
const ENGLISH_PATTERNS = ENGLISH_CORRECTIONS.map((token) => [
|
|
14
|
+
token,
|
|
15
|
+
new RegExp(`${/^[a-z0-9]/.test(token) ? "(?<![a-z0-9])" : ""}${escapeRegex(token)}${/[a-z0-9]$/.test(token) ? "(?![a-z0-9])" : ""}`, "i"),
|
|
16
|
+
]);
|
|
17
|
+
function numberOf(value) {
|
|
18
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
19
|
+
}
|
|
20
|
+
function stringOf(value) {
|
|
21
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
22
|
+
}
|
|
23
|
+
/** Case-insensitive word-list match; typographic apostrophes are folded so "that’s not" reads as "that's not". */
|
|
24
|
+
export function correctionMatches(text) {
|
|
25
|
+
const normalized = text.replace(/[\u2018\u2019]/g, "'").toLowerCase();
|
|
26
|
+
const matched = [];
|
|
27
|
+
for (const token of KOREAN_CORRECTIONS)
|
|
28
|
+
if (normalized.includes(token))
|
|
29
|
+
matched.push(token);
|
|
30
|
+
for (const [token, pattern] of ENGLISH_PATTERNS)
|
|
31
|
+
if (pattern.test(normalized))
|
|
32
|
+
matched.push(token);
|
|
33
|
+
return matched;
|
|
34
|
+
}
|
|
35
|
+
function correctionOf(next) {
|
|
36
|
+
if (next === null)
|
|
37
|
+
return { status: "unknown", label: "heuristic", matched: [], reason: "no_next_prompt" };
|
|
38
|
+
// Capture level metadata (and a runtime that reports no prompt text) leaves nothing to match on.
|
|
39
|
+
if (next.text === null)
|
|
40
|
+
return { status: "unknown", label: "heuristic", matched: [], reason: "text_not_captured" };
|
|
41
|
+
const matched = correctionMatches(next.text);
|
|
42
|
+
return { status: matched.length > 0 ? "correction" : "no_correction", label: "heuristic", matched, reason: null };
|
|
43
|
+
}
|
|
44
|
+
/** The first prompt of a later turn in the same session; turns without a prompt event (Codex spans) are skipped. */
|
|
45
|
+
export function nextPromptAfterTurn(session, turnIndex) {
|
|
46
|
+
for (let i = turnIndex + 1; i < session.turns.length; i += 1) {
|
|
47
|
+
const prompt = session.turns[i]?.events.find((e) => e.kind === "prompt");
|
|
48
|
+
if (prompt)
|
|
49
|
+
return prompt;
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
function callTokens(data) {
|
|
54
|
+
const measured = {};
|
|
55
|
+
const unknownCalls = {};
|
|
56
|
+
for (const key of TOKEN_KEYS) {
|
|
57
|
+
const value = numberOf(data[key]);
|
|
58
|
+
measured[key] = value ?? null;
|
|
59
|
+
unknownCalls[key] = value === undefined ? 1 : 0;
|
|
60
|
+
}
|
|
61
|
+
return { inputIncludesCache: data.inputIncludesCache === true, calls: 1, ...measured, unknownCalls };
|
|
62
|
+
}
|
|
63
|
+
/** An unreported measurement adds nothing: the sum covers the calls that reported it, unknownCalls counts the rest. */
|
|
64
|
+
function sumTokens(a, b) {
|
|
65
|
+
const measured = {};
|
|
66
|
+
const unknownCalls = {};
|
|
67
|
+
for (const key of TOKEN_KEYS) {
|
|
68
|
+
const left = a[key];
|
|
69
|
+
const right = b[key];
|
|
70
|
+
measured[key] = left === null ? right : right === null ? left : left + right;
|
|
71
|
+
unknownCalls[key] = a.unknownCalls[key] + b.unknownCalls[key];
|
|
72
|
+
}
|
|
73
|
+
return { inputIncludesCache: a.inputIncludesCache, calls: a.calls + b.calls, ...measured, unknownCalls };
|
|
74
|
+
}
|
|
75
|
+
function mergeTokens(into, buckets) {
|
|
76
|
+
for (const bucket of buckets) {
|
|
77
|
+
const current = into.get(bucket.inputIncludesCache);
|
|
78
|
+
into.set(bucket.inputIncludesCache, current ? sumTokens(current, bucket) : bucket);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function sortedTokens(buckets) {
|
|
82
|
+
return [...buckets.values()].sort((a, b) => Number(a.inputIncludesCache) - Number(b.inputIncludesCache));
|
|
83
|
+
}
|
|
84
|
+
function skillDirHint(span) {
|
|
85
|
+
const path = stringOf(span.activation.data.path);
|
|
86
|
+
if (path === undefined)
|
|
87
|
+
return null;
|
|
88
|
+
const cut = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
|
|
89
|
+
return cut > 0 ? path.slice(0, cut) : null;
|
|
90
|
+
}
|
|
91
|
+
function readFileBelongsTo(event, span) {
|
|
92
|
+
const skillUri = event.skillUri ?? stringOf(event.data.skillUri) ?? null;
|
|
93
|
+
if (skillUri === null)
|
|
94
|
+
return false;
|
|
95
|
+
if (span.uri !== null)
|
|
96
|
+
return skillUri === span.uri;
|
|
97
|
+
return skillUri.split("/").at(-2) === span.name;
|
|
98
|
+
}
|
|
99
|
+
const COMMAND_KEYS = new Set(["command", "full_command", "bash_command", "cmd", "argv", "script", "shell_command"]);
|
|
100
|
+
/**
|
|
101
|
+
* The strings an argument blob can hold. A JSON value under a command key (and any text that is not JSON)
|
|
102
|
+
* is a command line; every other JSON string is one path argument and keeps its spaces and quotes as data.
|
|
103
|
+
*/
|
|
104
|
+
function collectArgStrings(text) {
|
|
105
|
+
let parsed;
|
|
106
|
+
try {
|
|
107
|
+
parsed = JSON.parse(text);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return [{ text, command: true }];
|
|
111
|
+
}
|
|
112
|
+
const out = [];
|
|
113
|
+
const walk = (value, command) => {
|
|
114
|
+
if (typeof value === "string")
|
|
115
|
+
out.push({ text: value, command });
|
|
116
|
+
else if (Array.isArray(value))
|
|
117
|
+
for (const item of value)
|
|
118
|
+
walk(item, command);
|
|
119
|
+
else if (value !== null && typeof value === "object") {
|
|
120
|
+
for (const [key, item] of Object.entries(value))
|
|
121
|
+
walk(item, command || COMMAND_KEYS.has(key));
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
walk(parsed, false);
|
|
125
|
+
return out.length > 0 ? out : [{ text, command: true }];
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Shell-like split of a command line: whitespace separates, single and double quotes group and are dropped.
|
|
129
|
+
* A backslash escapes a separator, a quote or itself; anywhere else it stays, so `C:\skills\x` survives intact.
|
|
130
|
+
*/
|
|
131
|
+
function shellTokens(text) {
|
|
132
|
+
const tokens = [];
|
|
133
|
+
let current = "";
|
|
134
|
+
let open = false;
|
|
135
|
+
let quote = null;
|
|
136
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
137
|
+
const char = text[i];
|
|
138
|
+
if (quote === null && /\s/.test(char)) {
|
|
139
|
+
if (open)
|
|
140
|
+
tokens.push(current);
|
|
141
|
+
current = "";
|
|
142
|
+
open = false;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (quote === null && (char === '"' || char === "'")) {
|
|
146
|
+
quote = char;
|
|
147
|
+
open = true;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (quote === char) {
|
|
151
|
+
quote = null;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const next = text[i + 1];
|
|
155
|
+
if (char === "\\" && quote !== "'" && next !== undefined && (/\s/.test(next) || next === '"' || next === "'" || next === "\\")) {
|
|
156
|
+
current += next;
|
|
157
|
+
open = true;
|
|
158
|
+
i += 1;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
current += char;
|
|
162
|
+
open = true;
|
|
163
|
+
}
|
|
164
|
+
if (open)
|
|
165
|
+
tokens.push(current);
|
|
166
|
+
return tokens;
|
|
167
|
+
}
|
|
168
|
+
function normalizePath(path) {
|
|
169
|
+
return path.replace(/\\+/g, "/").replace(/\/+$/, "");
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Whole-token comparison, never a substring: `/skills/db-query` does not reference `/skills/db-query'backup`.
|
|
173
|
+
* `insideOnly` drops the bare directory itself, which is what naming a directory on a command line is.
|
|
174
|
+
*/
|
|
175
|
+
function tokenReferencesDir(token, dir, insideOnly) {
|
|
176
|
+
const path = normalizePath(token);
|
|
177
|
+
const target = normalizePath(dir);
|
|
178
|
+
if (path === "" || target === "")
|
|
179
|
+
return false;
|
|
180
|
+
if (path.startsWith(`${target}/`))
|
|
181
|
+
return true;
|
|
182
|
+
const rooted = target.startsWith("/") || /^[A-Za-z]:\//.test(target);
|
|
183
|
+
if (!rooted && path.includes(`/${target}/`))
|
|
184
|
+
return true;
|
|
185
|
+
if (insideOnly)
|
|
186
|
+
return false;
|
|
187
|
+
return path === target || (!rooted && path.endsWith(`/${target}`));
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Native skills leave no MCP trail, so a tool argument naming a file inside the skill directory is counted as a read.
|
|
191
|
+
* The skill name stands in as a relative directory only while the activation reported no directory of its own.
|
|
192
|
+
*/
|
|
193
|
+
function argsReferenceSkillDir(event, span, dirHint) {
|
|
194
|
+
if (event.text === null)
|
|
195
|
+
return false;
|
|
196
|
+
const target = dirHint ?? span.name;
|
|
197
|
+
for (const { text, command } of collectArgStrings(event.text)) {
|
|
198
|
+
for (const token of command ? shellTokens(text) : [text]) {
|
|
199
|
+
if (tokenReferencesDir(token, target, command))
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
function supportingReadsOf(span, turn) {
|
|
206
|
+
const reads = [];
|
|
207
|
+
const start = turn.events.findIndex((e) => e.id === span.activation.id);
|
|
208
|
+
// Row order, not timestamps, decides "after the activation": events can share a millisecond.
|
|
209
|
+
for (const event of turn.events.slice(start < 0 ? turn.events.length : start + 1)) {
|
|
210
|
+
if (event.kind === "read_file" && readFileBelongsTo(event, span)) {
|
|
211
|
+
reads.push({ eventId: event.id, kind: "read_file", uri: event.skillUri ?? stringOf(event.data.uri) ?? null, label: "reported" });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (span.activation.kind !== "native_skill")
|
|
215
|
+
return reads;
|
|
216
|
+
const dirHint = skillDirHint(span);
|
|
217
|
+
for (const event of span.events) {
|
|
218
|
+
if (event.kind !== "tool_call" || isSkillarMcpData(event.data))
|
|
219
|
+
continue;
|
|
220
|
+
if (argsReferenceSkillDir(event, span, dirHint)) {
|
|
221
|
+
reads.push({ eventId: event.id, kind: "tool_call", uri: null, label: "heuristic" });
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return reads;
|
|
225
|
+
}
|
|
226
|
+
const SHELL_TOOLS = new Set(["exec_command", "Bash", "shell", "local_shell"]);
|
|
227
|
+
/** Only Codex names its code-running wrapper `exec`; elsewhere the name carries no wrapper meaning. */
|
|
228
|
+
function activityKind(event, runtime) {
|
|
229
|
+
const toolName = stringOf(event.data.toolName);
|
|
230
|
+
if (runtime === "codex" && toolName === "exec")
|
|
231
|
+
return "unresolved_exec";
|
|
232
|
+
return toolName !== undefined && SHELL_TOOLS.has(toolName) ? "command" : "other";
|
|
233
|
+
}
|
|
234
|
+
/** Usage of one skill span: `turn` supplies the events outside the span (MCP reads), `nextPrompt` the correction guess. */
|
|
235
|
+
export function spanUsage(span, ctx) {
|
|
236
|
+
let followThrough = 0;
|
|
237
|
+
let commands = 0;
|
|
238
|
+
let unresolvedExec = 0;
|
|
239
|
+
const results = { success: 0, failure: 0, unknown: 0 };
|
|
240
|
+
let skillToolCalls = 0;
|
|
241
|
+
let toolFailures = 0;
|
|
242
|
+
let llmCalls = 0;
|
|
243
|
+
let costSum = 0;
|
|
244
|
+
let costUnknownCalls = 0;
|
|
245
|
+
let reported = 0;
|
|
246
|
+
let single = 0;
|
|
247
|
+
let coActive = 0;
|
|
248
|
+
const buckets = new Map();
|
|
249
|
+
for (const event of span.events) {
|
|
250
|
+
if (event.attribution === "reported")
|
|
251
|
+
reported += 1;
|
|
252
|
+
else if (event.attribution === "co_active")
|
|
253
|
+
coActive += 1;
|
|
254
|
+
else if (event.attribution === "single")
|
|
255
|
+
single += 1;
|
|
256
|
+
if (event.kind === "tool_call") {
|
|
257
|
+
if (isSkillarMcpData(event.data))
|
|
258
|
+
skillToolCalls += 1;
|
|
259
|
+
else {
|
|
260
|
+
followThrough += 1;
|
|
261
|
+
const kind = activityKind(event, ctx.runtime);
|
|
262
|
+
if (kind === "command")
|
|
263
|
+
commands += 1;
|
|
264
|
+
else if (kind === "unresolved_exec")
|
|
265
|
+
unresolvedExec += 1;
|
|
266
|
+
results[event.data.success === true ? "success" : event.data.success === false ? "failure" : "unknown"] += 1;
|
|
267
|
+
}
|
|
268
|
+
if (event.data.success === false)
|
|
269
|
+
toolFailures += 1;
|
|
270
|
+
}
|
|
271
|
+
if (event.kind === "llm_call") {
|
|
272
|
+
llmCalls += 1;
|
|
273
|
+
const cost = numberOf(event.data.costUsd);
|
|
274
|
+
if (cost === undefined)
|
|
275
|
+
costUnknownCalls += 1;
|
|
276
|
+
else
|
|
277
|
+
costSum += cost;
|
|
278
|
+
mergeTokens(buckets, [callTokens(event.data)]);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const total = span.events.length;
|
|
282
|
+
return {
|
|
283
|
+
turnId: ctx.turn.id, spanId: span.id, skill: span.skill, name: span.name, uri: span.uri,
|
|
284
|
+
activatedAt: span.startedAt, durationMs: Math.max(0, span.endedAt - span.startedAt),
|
|
285
|
+
followThrough, activity: { version: 1, observations: followThrough, commands, unresolvedExec, results,
|
|
286
|
+
workExecutions: unresolvedExec > 0 ? null : followThrough },
|
|
287
|
+
skillToolCalls, toolFailures, supportingReads: supportingReadsOf(span, ctx.turn), llmCalls,
|
|
288
|
+
costUsd: llmCalls > 0 && costUnknownCalls === 0 ? costSum : null, costUnknownCalls,
|
|
289
|
+
tokens: sortedTokens(buckets), nextPromptCorrection: correctionOf(ctx.nextPrompt),
|
|
290
|
+
attribution: { reported, single, coActive, total, coActiveShare: total > 0 ? coActive / total : null },
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
export function sessionUsage(session) {
|
|
294
|
+
const spans = [];
|
|
295
|
+
session.turns.forEach((turn, index) => {
|
|
296
|
+
const nextPrompt = nextPromptAfterTurn(session, index);
|
|
297
|
+
for (const span of turn.skillSpans)
|
|
298
|
+
spans.push(spanUsage(span, { turn, nextPrompt, runtime: session.runtime }));
|
|
299
|
+
});
|
|
300
|
+
return { sessionId: session.id, runtime: session.runtime, spans };
|
|
301
|
+
}
|
|
302
|
+
/** Per-skill totals across any set of spans. Cost stays null while a single contributing model call reported none. */
|
|
303
|
+
export function aggregateUsage(spans) {
|
|
304
|
+
const bySkill = new Map();
|
|
305
|
+
for (const usage of spans) {
|
|
306
|
+
const acc = bySkill.get(usage.skill) ?? {
|
|
307
|
+
skill: usage.skill, name: usage.name, uri: usage.uri, spans: 0, spansWithoutFollowThrough: 0,
|
|
308
|
+
followThrough: 0, skillToolCalls: 0, toolFailures: 0, supportingReads: 0, heuristicReads: 0,
|
|
309
|
+
durationMs: 0, llmCalls: 0, costSum: 0, costUnknownCalls: 0, tokens: new Map(),
|
|
310
|
+
corrections: { correction: 0, no_correction: 0, unknown: 0 }, reported: 0, single: 0, coActive: 0, total: 0,
|
|
311
|
+
commands: 0, unresolvedExec: 0, results: { success: 0, failure: 0, unknown: 0 }, workExecutions: 0,
|
|
312
|
+
};
|
|
313
|
+
acc.uri = acc.uri ?? usage.uri;
|
|
314
|
+
acc.spans += 1;
|
|
315
|
+
if (usage.followThrough === 0)
|
|
316
|
+
acc.spansWithoutFollowThrough += 1;
|
|
317
|
+
acc.followThrough += usage.followThrough;
|
|
318
|
+
acc.skillToolCalls += usage.skillToolCalls;
|
|
319
|
+
acc.toolFailures += usage.toolFailures;
|
|
320
|
+
acc.supportingReads += usage.supportingReads.length;
|
|
321
|
+
acc.heuristicReads += usage.supportingReads.filter((r) => r.label === "heuristic").length;
|
|
322
|
+
acc.durationMs += usage.durationMs;
|
|
323
|
+
acc.llmCalls += usage.llmCalls;
|
|
324
|
+
acc.costSum += usage.costUsd ?? 0;
|
|
325
|
+
acc.costUnknownCalls += usage.costUnknownCalls;
|
|
326
|
+
mergeTokens(acc.tokens, usage.tokens);
|
|
327
|
+
acc.corrections[usage.nextPromptCorrection.status] += 1;
|
|
328
|
+
acc.reported += usage.attribution.reported;
|
|
329
|
+
acc.single += usage.attribution.single;
|
|
330
|
+
acc.coActive += usage.attribution.coActive;
|
|
331
|
+
acc.total += usage.attribution.total;
|
|
332
|
+
acc.commands += usage.activity.commands;
|
|
333
|
+
acc.unresolvedExec += usage.activity.unresolvedExec;
|
|
334
|
+
acc.results.success += usage.activity.results.success;
|
|
335
|
+
acc.results.failure += usage.activity.results.failure;
|
|
336
|
+
acc.results.unknown += usage.activity.results.unknown;
|
|
337
|
+
acc.workExecutions = acc.workExecutions === null || usage.activity.workExecutions === null ? null
|
|
338
|
+
: acc.workExecutions + usage.activity.workExecutions;
|
|
339
|
+
bySkill.set(usage.skill, acc);
|
|
340
|
+
}
|
|
341
|
+
return [...bySkill.values()]
|
|
342
|
+
.map((acc) => ({
|
|
343
|
+
skill: acc.skill, name: acc.name, uri: acc.uri, spans: acc.spans, spansWithoutFollowThrough: acc.spansWithoutFollowThrough,
|
|
344
|
+
followThrough: acc.followThrough, skillToolCalls: acc.skillToolCalls, toolFailures: acc.toolFailures,
|
|
345
|
+
supportingReads: acc.supportingReads, heuristicReads: acc.heuristicReads, durationMs: acc.durationMs,
|
|
346
|
+
llmCalls: acc.llmCalls, costUsd: acc.llmCalls > 0 && acc.costUnknownCalls === 0 ? acc.costSum : null,
|
|
347
|
+
costUnknownCalls: acc.costUnknownCalls, tokens: sortedTokens(acc.tokens), corrections: acc.corrections,
|
|
348
|
+
attribution: { reported: acc.reported, single: acc.single, coActive: acc.coActive, total: acc.total,
|
|
349
|
+
coActiveShare: acc.total > 0 ? acc.coActive / acc.total : null },
|
|
350
|
+
activity: { version: 1, observations: acc.followThrough, commands: acc.commands, unresolvedExec: acc.unresolvedExec,
|
|
351
|
+
results: acc.results, workExecutions: acc.workExecutions },
|
|
352
|
+
}))
|
|
353
|
+
.sort((a, b) => a.skill.localeCompare(b.skill));
|
|
354
|
+
}
|
|
355
|
+
/** Usage of every session linked to one flow (or to the herdr workspace acting as one). */
|
|
356
|
+
export function flowUsage(store, flowOrWorkspaceId) {
|
|
357
|
+
const flow = flowTree(store, flowOrWorkspaceId);
|
|
358
|
+
if (!flow)
|
|
359
|
+
return undefined;
|
|
360
|
+
const sessions = [];
|
|
361
|
+
for (const tab of flow.tabs) {
|
|
362
|
+
for (const pane of tab.panes) {
|
|
363
|
+
for (const link of pane.sessions) {
|
|
364
|
+
if (link.session === null || sessions.some((s) => s.sessionId === link.session?.id))
|
|
365
|
+
continue;
|
|
366
|
+
sessions.push(sessionUsage(link.session));
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return { id: flow.id, sessions, skills: aggregateUsage(sessions.flatMap((s) => s.spans)) };
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Usage of the sessions in a time range. Every eligible session is analysed and aggregated; an optional
|
|
374
|
+
* `detailLimit` shortens only the per-session list and then says so through `truncated` and `sessionCount`.
|
|
375
|
+
*/
|
|
376
|
+
export function rangeUsage(store, opts) {
|
|
377
|
+
const filter = { from: opts.from, to: opts.to, ...(opts.automated !== undefined ? { automated: opts.automated } : {}) };
|
|
378
|
+
const detailLimit = opts.detailLimit === undefined ? undefined : Math.max(0, Math.floor(opts.detailLimit));
|
|
379
|
+
const sessions = [];
|
|
380
|
+
const spans = [];
|
|
381
|
+
let sessionCount = 0;
|
|
382
|
+
for (const row of store.sessions(filter)) {
|
|
383
|
+
const tree = sessionTree(store, row.id);
|
|
384
|
+
if (!tree)
|
|
385
|
+
continue;
|
|
386
|
+
const usage = sessionUsage(tree);
|
|
387
|
+
sessionCount += 1;
|
|
388
|
+
spans.push(...usage.spans);
|
|
389
|
+
if (detailLimit === undefined || sessions.length < detailLimit)
|
|
390
|
+
sessions.push(usage);
|
|
391
|
+
}
|
|
392
|
+
return { from: opts.from, to: opts.to, sessionCount, truncated: sessions.length < sessionCount, sessions,
|
|
393
|
+
skills: aggregateUsage(spans) };
|
|
394
|
+
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { aggregateFindings, analyzeSession, loadedDigests } from "../analysis/finding.js";
|
|
2
|
+
import { flowTree, sessionTree } from "../analysis/tree.js";
|
|
3
|
+
import { aggregateUsage, rangeUsage, sessionUsage } from "../analysis/usage.js";
|
|
4
|
+
import { compareFlow } from "../workflow/compare.js";
|
|
5
|
+
import { deny } from "./security.js";
|
|
6
|
+
/** A herdr workspace with no registered flow is addressed as `ws:<workspaceId>`, a registered flow by its own id. */
|
|
7
|
+
export const WORKSPACE_PREFIX = "ws:";
|
|
8
|
+
const MAX_LIMIT = 100;
|
|
9
|
+
const DEFAULT_LIMIT = 20;
|
|
10
|
+
const MAX_DETAIL = 100;
|
|
11
|
+
const DEFAULT_DETAIL = 20;
|
|
12
|
+
const DAY_MS = 86_400_000;
|
|
13
|
+
const DEFAULT_RANGE_DAYS = 7;
|
|
14
|
+
// /api/skills/usage analyses every session of its range, so the range itself is what bounds the work.
|
|
15
|
+
const MAX_RANGE_DAYS = 366;
|
|
16
|
+
class BadQuery extends Error {
|
|
17
|
+
code;
|
|
18
|
+
constructor(code, message) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.code = code;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function handle(log, fn) {
|
|
24
|
+
return (req, res) => {
|
|
25
|
+
try {
|
|
26
|
+
fn(req, res);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (error instanceof BadQuery)
|
|
30
|
+
return deny(res, 400, error.code, { message: error.message });
|
|
31
|
+
log(`${req.method} ${req.path} failed: ${error.stack ?? String(error)}`);
|
|
32
|
+
deny(res, 500, "internal_error");
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function one(value) {
|
|
37
|
+
if (value === undefined)
|
|
38
|
+
return undefined;
|
|
39
|
+
return Array.isArray(value) ? value.map(String).join(",") : String(value);
|
|
40
|
+
}
|
|
41
|
+
function intOf(q, key, range) {
|
|
42
|
+
const raw = one(q[key]);
|
|
43
|
+
if (raw === undefined || raw === "")
|
|
44
|
+
return undefined;
|
|
45
|
+
const n = Number(raw);
|
|
46
|
+
if (!Number.isInteger(n) || (range && (n < range[0] || n > range[1]))) {
|
|
47
|
+
throw new BadQuery("bad_filter", `${key} must be an integer${range ? ` in ${range[0]}..${range[1]}` : ""}`);
|
|
48
|
+
}
|
|
49
|
+
return n;
|
|
50
|
+
}
|
|
51
|
+
function automatedOf(q) {
|
|
52
|
+
const raw = one(q.automated);
|
|
53
|
+
if (raw === undefined || raw === "")
|
|
54
|
+
return "exclude";
|
|
55
|
+
if (raw !== "include" && raw !== "only")
|
|
56
|
+
throw new BadQuery("bad_filter", "automated must be include or only");
|
|
57
|
+
return raw;
|
|
58
|
+
}
|
|
59
|
+
/** from/to are milliseconds, half-open [from, to), as everywhere else in the API. */
|
|
60
|
+
function rangeOf(q, now) {
|
|
61
|
+
const to = intOf(q, "to") ?? now;
|
|
62
|
+
const from = intOf(q, "from") ?? to - DEFAULT_RANGE_DAYS * DAY_MS;
|
|
63
|
+
if (from > to)
|
|
64
|
+
throw new BadQuery("bad_filter", "from must not be after to");
|
|
65
|
+
if (to - from > MAX_RANGE_DAYS * DAY_MS)
|
|
66
|
+
throw new BadQuery("bad_filter", `the range must not exceed ${MAX_RANGE_DAYS} days`);
|
|
67
|
+
return { from, to };
|
|
68
|
+
}
|
|
69
|
+
/** Opaque to callers, and tied to the sort key: the ordering time plus the id that breaks its ties. */
|
|
70
|
+
function encodeCursor(row) {
|
|
71
|
+
return Buffer.from(`${row.orderMs}:${row.id}`, "utf8").toString("base64url");
|
|
72
|
+
}
|
|
73
|
+
function decodeCursor(raw) {
|
|
74
|
+
if (raw === undefined || raw === "")
|
|
75
|
+
return undefined;
|
|
76
|
+
const text = Buffer.from(raw, "base64url").toString("utf8");
|
|
77
|
+
const cut = text.indexOf(":");
|
|
78
|
+
const orderMs = cut > 0 ? Number(text.slice(0, cut)) : Number.NaN;
|
|
79
|
+
const id = cut > 0 ? text.slice(cut + 1) : "";
|
|
80
|
+
if (!Number.isInteger(orderMs) || id === "")
|
|
81
|
+
throw new BadQuery("bad_cursor", "cursor is not one this endpoint issued");
|
|
82
|
+
return { orderMs, id };
|
|
83
|
+
}
|
|
84
|
+
function keepSession(sessionId, automation, mode) {
|
|
85
|
+
if (mode === "include")
|
|
86
|
+
return true;
|
|
87
|
+
const automated = automation[sessionId] !== undefined;
|
|
88
|
+
return mode === "only" ? automated : !automated;
|
|
89
|
+
}
|
|
90
|
+
function entryOf(row, automation, mode) {
|
|
91
|
+
const links = row.links.filter((link) => link.sessionId === null || keepSession(link.sessionId, automation, mode));
|
|
92
|
+
const sessionIds = [...new Set(links.flatMap((link) => (link.sessionId === null ? [] : [link.sessionId])))];
|
|
93
|
+
let reported = 0;
|
|
94
|
+
let inferred = 0;
|
|
95
|
+
let unlinked = 0;
|
|
96
|
+
for (const link of links) {
|
|
97
|
+
if (link.sessionId === null || link.source === "unlinked")
|
|
98
|
+
unlinked += 1;
|
|
99
|
+
else if (link.source === "inferred:cwd_time")
|
|
100
|
+
inferred += 1;
|
|
101
|
+
else
|
|
102
|
+
reported += 1;
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
id: row.kind === "workspace" ? `${WORKSPACE_PREFIX}${row.workspaceId}` : row.id,
|
|
106
|
+
kind: row.kind, workspaceId: row.workspaceId, tabId: row.tabId, label: row.label, registered: row.kind === "flow",
|
|
107
|
+
spec: row.spec, startedMs: row.startedMs, endedMs: row.endedMs,
|
|
108
|
+
paneIds: row.paneIds, sessionIds,
|
|
109
|
+
counts: { panes: row.paneIds.length, sessions: sessionIds.length },
|
|
110
|
+
attribution: { reported, inferred, unlinked },
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/** The finding of one session under the API's names: `loadedSkills` is `usedSkills`, native activations included. */
|
|
114
|
+
function findingDto(finding) {
|
|
115
|
+
return {
|
|
116
|
+
sessionId: finding.sessionId,
|
|
117
|
+
turns: finding.turns.map(({ loadedSkills, ...turn }) => ({ ...turn, usedSkills: loadedSkills })),
|
|
118
|
+
aggregate: finding.aggregate,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function sessionIdsOf(tree) {
|
|
122
|
+
return tree.tabs.flatMap((tab) => tab.panes.flatMap((pane) => pane.sessions.flatMap((link) => (link.sessionId === null ? [] : [link.sessionId]))));
|
|
123
|
+
}
|
|
124
|
+
/** Drops the links to sessions the automated filter excludes; the pane stays, and the report says what was left out. */
|
|
125
|
+
function filterTree(tree, automation, mode) {
|
|
126
|
+
const excluded = new Map();
|
|
127
|
+
const tabs = tree.tabs.map((tab) => ({
|
|
128
|
+
...tab,
|
|
129
|
+
panes: tab.panes.map((pane) => ({
|
|
130
|
+
...pane,
|
|
131
|
+
sessions: pane.sessions.filter((link) => {
|
|
132
|
+
if (link.sessionId === null || keepSession(link.sessionId, automation, mode))
|
|
133
|
+
return true;
|
|
134
|
+
excluded.set(link.sessionId, { sessionId: link.sessionId, reason: automation[link.sessionId] ?? "not_automated" });
|
|
135
|
+
return false;
|
|
136
|
+
}),
|
|
137
|
+
})),
|
|
138
|
+
}));
|
|
139
|
+
return { tree: { ...tree, tabs }, excluded };
|
|
140
|
+
}
|
|
141
|
+
function sessionsOf(tree) {
|
|
142
|
+
const sessions = new Map((tree.sessions ?? []).map((session) => [session.id, session]));
|
|
143
|
+
for (const tab of tree.tabs) {
|
|
144
|
+
for (const pane of tab.panes) {
|
|
145
|
+
for (const link of pane.sessions)
|
|
146
|
+
if (link.session !== null)
|
|
147
|
+
sessions.set(link.session.id, link.session);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return [...sessions.values()];
|
|
151
|
+
}
|
|
152
|
+
export function registerFlowRoutes(app, deps) {
|
|
153
|
+
const { store, log } = deps;
|
|
154
|
+
/**
|
|
155
|
+
* Registered flows and the herdr workspaces without one, newest first. The page is bounded by `limit`, so the
|
|
156
|
+
* optional from/to filter the ordering time rather than capping the work.
|
|
157
|
+
*/
|
|
158
|
+
app.get("/api/flows", handle(log, (req, res) => {
|
|
159
|
+
const automated = automatedOf(req.query);
|
|
160
|
+
const limit = intOf(req.query, "limit", [1, MAX_LIMIT]) ?? DEFAULT_LIMIT;
|
|
161
|
+
const from = intOf(req.query, "from");
|
|
162
|
+
const to = intOf(req.query, "to");
|
|
163
|
+
if (from !== undefined && to !== undefined && from > to)
|
|
164
|
+
throw new BadQuery("bad_filter", "from must not be after to");
|
|
165
|
+
const cursor = decodeCursor(one(req.query.cursor));
|
|
166
|
+
// One row beyond the page says whether a next page exists, without counting the whole list.
|
|
167
|
+
const rows = store.listFlows({ limit: limit + 1, automated, ...(from !== undefined ? { from } : {}), ...(to !== undefined ? { to } : {}), ...(cursor ? { cursor } : {}) });
|
|
168
|
+
const page = rows.slice(0, limit);
|
|
169
|
+
const last = page.at(-1);
|
|
170
|
+
const automation = automated === "include" ? {} :
|
|
171
|
+
store.sessionAutomation(page.flatMap((row) => row.links.flatMap((link) => (link.sessionId === null ? [] : [link.sessionId]))));
|
|
172
|
+
res.json({
|
|
173
|
+
generatedAt: Date.now(), from: from ?? null, to: to ?? null, limit, automated,
|
|
174
|
+
flows: page.map((row) => entryOf(row, automation, automated)),
|
|
175
|
+
nextCursor: rows.length > limit && last ? encodeCursor(last) : null,
|
|
176
|
+
});
|
|
177
|
+
}));
|
|
178
|
+
/** One flow: the tree (todo 6), per-turn finding (7), per-span usage (8) and the spec comparison (9). */
|
|
179
|
+
app.get("/api/flows/:id", handle(log, (req, res) => {
|
|
180
|
+
const automated = automatedOf(req.query);
|
|
181
|
+
const ref = String(req.params.id);
|
|
182
|
+
const id = ref.startsWith(WORKSPACE_PREFIX) ? ref.slice(WORKSPACE_PREFIX.length) : ref;
|
|
183
|
+
const eventScope = ref === "outside" ? undefined : store.flowEventScope(id);
|
|
184
|
+
let full;
|
|
185
|
+
if (ref === "outside") {
|
|
186
|
+
const to = intOf(req.query, "to") ?? Date.now() + 1;
|
|
187
|
+
const from = intOf(req.query, "from") ?? to - DEFAULT_RANGE_DAYS * DAY_MS;
|
|
188
|
+
if (from > to)
|
|
189
|
+
throw new BadQuery("bad_filter", "from must not be after to");
|
|
190
|
+
const limit = intOf(req.query, "limit", [1, MAX_LIMIT]) ?? MAX_LIMIT;
|
|
191
|
+
full = { id: "outside", workspaceId: "", label: "outside herdr", registered: false, tabs: [],
|
|
192
|
+
sessions: store.outsideHerdrSessions({ from, to, limit, automated }).flatMap(({ id: sessionId }) => {
|
|
193
|
+
const session = sessionTree(store, sessionId);
|
|
194
|
+
return session ? [session] : [];
|
|
195
|
+
}) };
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
full = flowTree(store, id, eventScope);
|
|
199
|
+
}
|
|
200
|
+
if (!full)
|
|
201
|
+
return deny(res, 404, "flow_not_found", { message: `no flow or herdr workspace ${ref}` });
|
|
202
|
+
// Classify the shared execution population, including explicitly mapped panes on other tabs.
|
|
203
|
+
const comparisonSessionIds = full.registered ? store.flowSessionIds(full.id, eventScope) : [];
|
|
204
|
+
const automation = automated === "include" ? {} : store.sessionAutomation([...sessionIdsOf(full), ...comparisonSessionIds]);
|
|
205
|
+
const flow = full.registered ? store.getFlow(full.id) : undefined;
|
|
206
|
+
const overlaps = flow ? sessionsOf(full).map((session) => ({
|
|
207
|
+
sessionId: session.id, startedBefore: session.startedAt < flow.registeredMs,
|
|
208
|
+
continuesAfter: flow.endedMs !== undefined && (session.endedAt === null || session.endedAt > flow.endedMs),
|
|
209
|
+
})).filter((session) => session.startedBefore || session.continuesAfter) : [];
|
|
210
|
+
const { tree, excluded } = filterTree(full, automation, automated);
|
|
211
|
+
for (const sessionId of comparisonSessionIds) {
|
|
212
|
+
if (keepSession(sessionId, automation, automated))
|
|
213
|
+
continue;
|
|
214
|
+
excluded.set(sessionId, { sessionId, reason: automation[sessionId] ?? "not_automated" });
|
|
215
|
+
}
|
|
216
|
+
const sessions = sessionsOf(tree);
|
|
217
|
+
const digestOf = loadedDigests(store);
|
|
218
|
+
const findings = sessions.map((session) => analyzeSession(session, digestOf));
|
|
219
|
+
const usages = sessions.map((session) => sessionUsage(session));
|
|
220
|
+
const spec = flow?.spec;
|
|
221
|
+
const warningSessions = [...sessions.map((session) => session.id), ...comparisonSessionIds.filter((id) => !excluded.has(id))];
|
|
222
|
+
const unattributedLoads = flow ? store.flowUnattributedLoads(flow.id, warningSessions, eventScope) : store.reconcileLoads({
|
|
223
|
+
readOnly: true,
|
|
224
|
+
from: (ref === "outside" ? intOf(req.query, "from") ?? Date.now() - DEFAULT_RANGE_DAYS * DAY_MS
|
|
225
|
+
: sessions.length === 0 ? 0 : Math.min(...sessions.map((session) => session.startedAt))),
|
|
226
|
+
to: (ref === "outside" ? intOf(req.query, "to") ?? Date.now() + 1 : Date.now() + 1),
|
|
227
|
+
sessionIds: warningSessions,
|
|
228
|
+
});
|
|
229
|
+
res.json({
|
|
230
|
+
generatedAt: Date.now(),
|
|
231
|
+
id: ref === "outside" ? "outside" : tree.registered ? tree.id : `${WORKSPACE_PREFIX}${tree.workspaceId}`,
|
|
232
|
+
kind: ref === "outside" ? "outside_herdr" : tree.registered ? "flow" : "workspace",
|
|
233
|
+
workspaceId: tree.workspaceId, label: tree.label, registered: tree.registered,
|
|
234
|
+
flow: flow ? { registeredMs: flow.registeredMs, endedMs: flow.endedMs ?? null, tabId: flow.tabId ?? null } : null,
|
|
235
|
+
spec: spec ? { id: spec.id, name: spec.name, version: spec.version } : null,
|
|
236
|
+
automated, tree,
|
|
237
|
+
executionScope: eventScope ? { from: eventScope.from, to: eventScope.to, boundary: eventScope.boundary,
|
|
238
|
+
overlaps: overlaps.filter((session) => keepSession(session.sessionId, automation, automated)),
|
|
239
|
+
label: eventScope.boundary === "recorded"
|
|
240
|
+
? "Events scoped by timestamp; persisted recording order resolves equal-time boundaries independently of membership discovery. Delayed interior evidence can still arrive. Session ends are observed separately."
|
|
241
|
+
: "Some boundary ordering is unavailable. Unproven equal-time activity is excluded here; workspace history retains it." } : null,
|
|
242
|
+
finding: { id: tree.id, sessions: findings.map(findingDto), aggregate: aggregateFindings(findings.flatMap((f) => f.turns)) },
|
|
243
|
+
usage: { id: tree.id, sessions: usages, skills: aggregateUsage(usages.flatMap((u) => u.spans)) },
|
|
244
|
+
comparison: tree.registered ? compareFlow(store, tree.id, { excludeSessions: [...excluded.keys()],
|
|
245
|
+
...(eventScope ? { scope: eventScope } : {}) }) ?? null : null,
|
|
246
|
+
excludedSessions: [...excluded.values()],
|
|
247
|
+
unattributedLoads,
|
|
248
|
+
});
|
|
249
|
+
}));
|
|
250
|
+
/** The same finding and usage analysis for one session, whether or not herdr knows a pane for it. */
|
|
251
|
+
app.get("/api/sessions/:id/skill-report", handle(log, (req, res) => {
|
|
252
|
+
const automated = automatedOf(req.query);
|
|
253
|
+
const id = String(req.params.id);
|
|
254
|
+
const unattributedLoads = store.reconcileLoads({ sessionIds: [id], readOnly: true });
|
|
255
|
+
const tree = sessionTree(store, id);
|
|
256
|
+
if (!tree)
|
|
257
|
+
return deny(res, 404, "session_not_found", { message: `no session ${id}` });
|
|
258
|
+
const automation = store.sessionAutomation([id])[id];
|
|
259
|
+
if (!keepSession(id, automation === undefined ? {} : { [id]: automation }, automated)) {
|
|
260
|
+
return deny(res, 404, "automated_excluded", {
|
|
261
|
+
message: automated === "only"
|
|
262
|
+
? `session ${id} was not automated; drop automated=only to read it`
|
|
263
|
+
: `session ${id} was automated (${automation}); pass automated=include to read it`,
|
|
264
|
+
automated: automation ?? null,
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
res.json({
|
|
268
|
+
generatedAt: Date.now(), sessionId: tree.id, runtime: tree.runtime, startedAt: tree.startedAt, endedAt: tree.endedAt,
|
|
269
|
+
automated: automation ?? null, tree,
|
|
270
|
+
finding: findingDto(analyzeSession(tree, loadedDigests(store))), usage: sessionUsage(tree),
|
|
271
|
+
unattributedLoads,
|
|
272
|
+
});
|
|
273
|
+
}));
|
|
274
|
+
/** Per-skill usage over a time range; `sessions` is the detail list `detail` bounds, `skills` covers them all. */
|
|
275
|
+
app.get("/api/skills/usage", handle(log, (req, res) => {
|
|
276
|
+
const automated = automatedOf(req.query);
|
|
277
|
+
const { from, to } = rangeOf(req.query, Date.now());
|
|
278
|
+
const detailLimit = intOf(req.query, "detail", [0, MAX_DETAIL]) ?? DEFAULT_DETAIL;
|
|
279
|
+
const usage = rangeUsage(store, { from, to, detailLimit, ...(automated === "exclude" ? {} : { automated }) });
|
|
280
|
+
res.json({
|
|
281
|
+
generatedAt: Date.now(), from, to, automated, detailLimit,
|
|
282
|
+
sessionCount: usage.sessionCount, truncated: usage.truncated, skills: usage.skills, sessions: usage.sessions,
|
|
283
|
+
});
|
|
284
|
+
}));
|
|
285
|
+
}
|