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,69 @@
|
|
|
1
|
+
export const normalize = (value) => value.normalize("NFKC").toLowerCase().replace(/\s+/g, " ").trim();
|
|
2
|
+
const STOPWORDS = new Set(("a an the and or to of for in on with from by is are be it this that do please use using when how me my can could would should not only then after before need want help " +
|
|
3
|
+
"해줘 해주세요 알려줘 작성해줘 만들어줘 확인해줘 검토해줘 필요 없어 있습니다 합니다 작업 사용 통해 대한 위한 관련 현재 다음 먼저 실제 하나 것 수 및 또는 하고 그리고 방법 " +
|
|
4
|
+
// Bare particles left over when a Latin/digit token carries a Korean particle ("api가", "19로").
|
|
5
|
+
"은 는 이 가 을 를 의 도 로 와 과 에").split(" "));
|
|
6
|
+
export function stem(word) {
|
|
7
|
+
if (/^[가-힣]+$/.test(word) && word.length > 2) {
|
|
8
|
+
const stripped = word.replace(/(?:해주세요|해줘|하기|하게|하는|하면서|하려는|으로|에서|에게|에는|까지|부터|처럼|보다|은|는|을|를|이|가|과|와|의|도|로)$/, "");
|
|
9
|
+
return stripped.length >= 2 ? stripped : word;
|
|
10
|
+
}
|
|
11
|
+
return /^[a-z]{5,}s$/.test(word) && !word.endsWith("ss") ? word.slice(0, -1) : word;
|
|
12
|
+
}
|
|
13
|
+
/** Raw word tokens of normalized text, split where Hangul meets Latin/digits ("api가" -> "api", "가"). */
|
|
14
|
+
export function words(text) {
|
|
15
|
+
return (normalize(text).match(/[\p{L}\p{N}]+/gu) ?? [])
|
|
16
|
+
.flatMap(word => word.split(/(?<=[가-힣])(?=[a-z0-9])|(?<=[a-z0-9])(?=[가-힣])/));
|
|
17
|
+
}
|
|
18
|
+
export const isStopword = (word) => STOPWORDS.has(word);
|
|
19
|
+
export function terms(text) {
|
|
20
|
+
return [...new Set(words(text).filter(word => !STOPWORDS.has(word)).map(stem).filter(word => !STOPWORDS.has(word)))];
|
|
21
|
+
}
|
|
22
|
+
export function keywordsOf(entry) {
|
|
23
|
+
return [...entry.examples, ...entry.aliasesKo, ...(entry.whenToUse ? terms(entry.whenToUse) : [])].map(normalize).filter(Boolean);
|
|
24
|
+
}
|
|
25
|
+
export function buildLexical(entries) {
|
|
26
|
+
const frequency = new Map();
|
|
27
|
+
const rows = entries.map(entry => {
|
|
28
|
+
const name = normalize(entry.name), keywords = keywordsOf(entry);
|
|
29
|
+
const weights = new Map();
|
|
30
|
+
for (const [field, weight] of [[entry.description, 1], [keywords.join(" "), 4], [name, 8]]) {
|
|
31
|
+
for (const term of terms(field))
|
|
32
|
+
weights.set(term, weight);
|
|
33
|
+
}
|
|
34
|
+
for (const term of weights.keys())
|
|
35
|
+
frequency.set(term, (frequency.get(term) ?? 0) + 1);
|
|
36
|
+
return { uri: entry.uri, name, keywords, nameTerms: new Set(terms(name)), weights };
|
|
37
|
+
});
|
|
38
|
+
const size = rows.length;
|
|
39
|
+
return {
|
|
40
|
+
search(query) {
|
|
41
|
+
const text = normalize(query);
|
|
42
|
+
if (!text)
|
|
43
|
+
return [];
|
|
44
|
+
const queryTerms = terms(text);
|
|
45
|
+
const hits = [];
|
|
46
|
+
for (const { uri, name, keywords, nameTerms, weights } of rows) {
|
|
47
|
+
const anchor = name === text ? 2 : keywords.includes(text) ? 1 : 0;
|
|
48
|
+
let score = anchor === 2 ? 1000 : anchor === 1 ? 300 : 0;
|
|
49
|
+
let anchored = anchor > 0;
|
|
50
|
+
const matched = [];
|
|
51
|
+
for (const word of queryTerms) {
|
|
52
|
+
const weight = weights.get(word);
|
|
53
|
+
if (!weight)
|
|
54
|
+
continue;
|
|
55
|
+
matched.push(word);
|
|
56
|
+
anchored ||= nameTerms.has(word) || keywords.includes(word);
|
|
57
|
+
const rarity = Math.log(1 + (size + .5) / ((frequency.get(word) ?? 0) + .5));
|
|
58
|
+
score += weight * rarity;
|
|
59
|
+
}
|
|
60
|
+
if (!score || (queryTerms.length > 1 && matched.length === 1 && !anchored))
|
|
61
|
+
continue;
|
|
62
|
+
// Prefer coverage without letting unmatched narrative details erase a result.
|
|
63
|
+
score *= 1 + matched.length / Math.max(queryTerms.length, 1);
|
|
64
|
+
hits.push({ uri, score, matched, anchor });
|
|
65
|
+
}
|
|
66
|
+
return hits.sort((a, b) => b.score - a.score || (a.uri < b.uri ? -1 : a.uri > b.uri ? 1 : 0));
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { dbPath } from "./config.js";
|
|
2
|
+
import { appendFileSync, mkdirSync, renameSync, statSync } from "node:fs";
|
|
3
|
+
import { stat } from "node:fs/promises";
|
|
4
|
+
import { createServer } from "node:http";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
import express from "express";
|
|
7
|
+
import { registerRoutes } from "./api/routes.js";
|
|
8
|
+
import { authGate, deny, localOnly, requireJson } from "./api/security.js";
|
|
9
|
+
import { createSseHub } from "./api/sse.js";
|
|
10
|
+
import { registerStatic } from "./api/static.js";
|
|
11
|
+
import { createCatalog } from "./catalog/index.js";
|
|
12
|
+
import { ensureSkillarHome, ensureToken, resolveSkillarHome, resolveHome } from "./config.js";
|
|
13
|
+
import { createFsWatchdog } from "./fs-watchdog.js";
|
|
14
|
+
import { startHerdrSync } from "./herdr/sync.js";
|
|
15
|
+
import { createIngestDeps } from "./ingest/pipeline.js";
|
|
16
|
+
import { registerMcp } from "./mcp/http.js";
|
|
17
|
+
import { packageVersion } from "./mcp/tools.js";
|
|
18
|
+
import { createSearchIndex } from "./search/index.js";
|
|
19
|
+
import { openDb, selfCheck } from "./store/db.js";
|
|
20
|
+
import { createStore } from "./store/index.js";
|
|
21
|
+
import { assertNodeVersion, assertPlatform, SkillarStartupError } from "./version.js";
|
|
22
|
+
export const HOST = "127.0.0.1";
|
|
23
|
+
export const LOG_MAX_BYTES = 5 * 1024 * 1024;
|
|
24
|
+
const DAY_MS = 86_400_000;
|
|
25
|
+
export function createFileLogger(path, maxBytes = LOG_MAX_BYTES) {
|
|
26
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
27
|
+
return (msg) => {
|
|
28
|
+
try {
|
|
29
|
+
if ((statSync(path, { throwIfNoEntry: false })?.size ?? 0) >= maxBytes)
|
|
30
|
+
renameSync(path, `${path}.1`);
|
|
31
|
+
appendFileSync(path, `${new Date().toISOString()} ${msg}\n`);
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
process.stderr.write(`skillar: cannot write ${path}: ${error.message}\n${msg}\n`);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function listen(server, port) {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
const onError = (error) => {
|
|
41
|
+
server.off("listening", onListening);
|
|
42
|
+
reject(error.code === "EADDRINUSE" ? new SkillarStartupError("port_in_use", `${HOST}:${port} is already in use`) : error);
|
|
43
|
+
};
|
|
44
|
+
const onListening = () => {
|
|
45
|
+
server.off("error", onError);
|
|
46
|
+
resolve(server.address().port);
|
|
47
|
+
};
|
|
48
|
+
server.once("error", onError);
|
|
49
|
+
server.once("listening", onListening);
|
|
50
|
+
server.listen(port, HOST);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
export async function startServer(config, deps = {}) {
|
|
54
|
+
assertNodeVersion();
|
|
55
|
+
assertPlatform();
|
|
56
|
+
const skillarHome = deps.skillarHome ?? resolveSkillarHome(process.env);
|
|
57
|
+
if (deps.skillarHome === undefined)
|
|
58
|
+
ensureSkillarHome(process.env);
|
|
59
|
+
else
|
|
60
|
+
mkdirSync(skillarHome, { recursive: true, mode: 0o700 });
|
|
61
|
+
const log = deps.log ?? createFileLogger(join(skillarHome, "logs", "daemon.log"));
|
|
62
|
+
const token = ensureToken(skillarHome);
|
|
63
|
+
const version = packageVersion();
|
|
64
|
+
const db = openDb(dbPath(skillarHome));
|
|
65
|
+
const cleanup = [];
|
|
66
|
+
let store;
|
|
67
|
+
try {
|
|
68
|
+
selfCheck(db);
|
|
69
|
+
store = deps.openStore ? deps.openStore() : createStore(db, { config, skillarHome, home: resolveHome(process.env) });
|
|
70
|
+
const ownStore = store;
|
|
71
|
+
cleanup.push(() => ownStore.close());
|
|
72
|
+
if (deps.openStore)
|
|
73
|
+
cleanup.push(() => db.close());
|
|
74
|
+
const herdr = (process.env.SKILLAR_HERDR ?? process.env.GISOOL_HERDR) === "off" ? undefined : startHerdrSync(db, { log });
|
|
75
|
+
if (herdr)
|
|
76
|
+
cleanup.push(() => herdr.close());
|
|
77
|
+
let linkQueued = false;
|
|
78
|
+
const linkSessions = new Set();
|
|
79
|
+
let herdrClosed = false;
|
|
80
|
+
cleanup.push(() => { herdrClosed = true; });
|
|
81
|
+
const catalog = createCatalog({ roots: config.roots, watch: true, log });
|
|
82
|
+
cleanup.push(() => catalog.close());
|
|
83
|
+
await catalog.refresh();
|
|
84
|
+
const search = createSearchIndex(db);
|
|
85
|
+
search.rebuild(catalog.list());
|
|
86
|
+
ownStore.upsertSkills(catalog.list());
|
|
87
|
+
// Skills deleted while the daemon was down, or from a root removed from the config, never produce a delta.
|
|
88
|
+
const present = new Set(catalog.list().map((s) => s.uri));
|
|
89
|
+
const stale = db.prepare("SELECT uri FROM skills WHERE removed_at IS NULL").all().map((r) => r.uri).filter((u) => !present.has(u));
|
|
90
|
+
if (stale.length)
|
|
91
|
+
ownStore.markRemoved(stale);
|
|
92
|
+
catalog.onDelta((delta) => {
|
|
93
|
+
try {
|
|
94
|
+
search.apply(delta);
|
|
95
|
+
ownStore.upsertSkills([...delta.added, ...delta.changed]);
|
|
96
|
+
if (delta.removed.length)
|
|
97
|
+
ownStore.markRemoved(delta.removed);
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
log(`applying catalog delta failed: ${error.message}`);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
const sessionRowStmt = db.prepare("SELECT runtime, started_at FROM sessions WHERE id = ?");
|
|
104
|
+
const lookupRow = (id) => {
|
|
105
|
+
const meta = sessionRowStmt.get(id);
|
|
106
|
+
if (!meta)
|
|
107
|
+
return undefined;
|
|
108
|
+
return ownStore.sessions({ from: meta.started_at, to: meta.started_at + 1, runtime: [meta.runtime] }).find((r) => r.id === id);
|
|
109
|
+
};
|
|
110
|
+
const sse = createSseHub(lookupRow, log);
|
|
111
|
+
cleanup.push(() => sse.close());
|
|
112
|
+
const ingestDeps = createIngestDeps(ownStore, {
|
|
113
|
+
captureLevel: config.captureLevel,
|
|
114
|
+
publish: (change) => sse.notify(change.sessionId),
|
|
115
|
+
sessionChanged: (sessionId) => {
|
|
116
|
+
// Ingest batches are synchronous; coalesce their notifications into one linking pass.
|
|
117
|
+
if (!herdr)
|
|
118
|
+
return;
|
|
119
|
+
linkSessions.add(sessionId);
|
|
120
|
+
if (linkQueued)
|
|
121
|
+
return;
|
|
122
|
+
linkQueued = true;
|
|
123
|
+
queueMicrotask(() => {
|
|
124
|
+
linkQueued = false;
|
|
125
|
+
if (herdrClosed)
|
|
126
|
+
return;
|
|
127
|
+
try {
|
|
128
|
+
const touched = [...linkSessions];
|
|
129
|
+
linkSessions.clear();
|
|
130
|
+
herdr.linkSessions(touched);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
log(`herdr linking failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
},
|
|
137
|
+
log: (message, error) => log(`ingest: ${message}: ${error instanceof Error ? error.message : String(error)}`),
|
|
138
|
+
});
|
|
139
|
+
const wd = deps.fsWatchdog ?? {};
|
|
140
|
+
const watchdog = createFsWatchdog({
|
|
141
|
+
probe: wd.probe ?? (() => stat(skillarHome)),
|
|
142
|
+
intervalMs: wd.intervalMs ?? 15_000,
|
|
143
|
+
timeoutMs: wd.timeoutMs ?? 30_000,
|
|
144
|
+
onStall: wd.onStall ?? ((stalledMs) => {
|
|
145
|
+
log(`filesystem requests stalled for ${stalledMs} ms; exiting so the service manager restarts skillar`);
|
|
146
|
+
// process.exit() can hang joining libuv workers during native cleanup after an fs stall.
|
|
147
|
+
process.kill(process.pid, "SIGKILL");
|
|
148
|
+
}),
|
|
149
|
+
});
|
|
150
|
+
let boundPort = config.port;
|
|
151
|
+
const app = express();
|
|
152
|
+
app.disable("x-powered-by");
|
|
153
|
+
// Routes match exactly as authGate classifies them; `/API/...` must not reach an `/api/...` handler.
|
|
154
|
+
app.set("case sensitive routing", true);
|
|
155
|
+
app.use(localOnly(() => boundPort));
|
|
156
|
+
// Authenticate before parsing so unauthenticated callers cannot make the daemon buffer bodies or bump reject counters.
|
|
157
|
+
app.use(authGate(token));
|
|
158
|
+
const smallJson = express.json({ limit: "1mb" });
|
|
159
|
+
const otlpJson = express.json({ limit: "8mb" });
|
|
160
|
+
app.use((req, res, next) => {
|
|
161
|
+
// The MCP route owns parsing so malformed JSON receives a protocol error.
|
|
162
|
+
if (req.path === "/mcp")
|
|
163
|
+
return next();
|
|
164
|
+
const parser = req.path.startsWith("/ingest/otlp/") ? otlpJson : smallJson;
|
|
165
|
+
parser(req, res, (error) => {
|
|
166
|
+
if (!error)
|
|
167
|
+
return next();
|
|
168
|
+
const type = error.type;
|
|
169
|
+
if (type === "entity.too.large") {
|
|
170
|
+
ownStore.countIngest("rejected", "payload_too_large");
|
|
171
|
+
return deny(res, 413, "payload_too_large");
|
|
172
|
+
}
|
|
173
|
+
if (req.path.startsWith("/ingest/"))
|
|
174
|
+
ownStore.countIngest("rejected", "bad_json");
|
|
175
|
+
deny(res, 400, "bad_json");
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
app.use(requireJson((req) => {
|
|
179
|
+
if (req.path.startsWith("/ingest/"))
|
|
180
|
+
ownStore.countIngest("rejected", "unsupported_media_type");
|
|
181
|
+
}));
|
|
182
|
+
registerRoutes(app, { store: ownStore, catalog, ingest: ingestDeps, sse, version, log, fsHealth: () => watchdog.health() });
|
|
183
|
+
registerMcp(app, { catalog, search, store: ownStore, ingest: ingestDeps, token });
|
|
184
|
+
registerStatic(app, { token, ...(deps.dashboardDir ? { dir: deps.dashboardDir } : {}) });
|
|
185
|
+
app.use((_req, res) => deny(res, 404, "not_found"));
|
|
186
|
+
app.use((error, req, res, _next) => {
|
|
187
|
+
const status = error.status;
|
|
188
|
+
if (status === 400)
|
|
189
|
+
return deny(res, 400, "bad_request");
|
|
190
|
+
// serve-static raises 403 for malformed paths (e.g. encoded traversal); nothing is served, so report not_found.
|
|
191
|
+
if (status === 403 || status === 404)
|
|
192
|
+
return deny(res, 404, "not_found");
|
|
193
|
+
if (status !== undefined && status > 400 && status < 500)
|
|
194
|
+
return deny(res, status, "request_rejected");
|
|
195
|
+
log(`${req.method} ${req.path} failed: ${error.stack ?? String(error)}`);
|
|
196
|
+
if (!res.headersSent)
|
|
197
|
+
deny(res, 500, "internal_error");
|
|
198
|
+
});
|
|
199
|
+
const server = createServer(app);
|
|
200
|
+
boundPort = await listen(server, config.port);
|
|
201
|
+
let retentionTimer;
|
|
202
|
+
cleanup.push(() => watchdog.stop());
|
|
203
|
+
if (config.retentionDays !== null) {
|
|
204
|
+
const days = config.retentionDays;
|
|
205
|
+
const runRetention = () => {
|
|
206
|
+
try {
|
|
207
|
+
log(`retention removed ${ownStore.retention(days)} sessions older than ${days} days`);
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
log(`retention failed: ${error.message}`);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
runRetention();
|
|
214
|
+
retentionTimer = setInterval(runRetention, DAY_MS);
|
|
215
|
+
retentionTimer.unref();
|
|
216
|
+
}
|
|
217
|
+
let closing;
|
|
218
|
+
const onSignal = (signal) => {
|
|
219
|
+
log(`received ${signal}, shutting down`);
|
|
220
|
+
void close().then(() => process.exit(0));
|
|
221
|
+
};
|
|
222
|
+
const handleSignals = deps.handleSignals ?? true;
|
|
223
|
+
if (handleSignals) {
|
|
224
|
+
process.once("SIGINT", onSignal);
|
|
225
|
+
process.once("SIGTERM", onSignal);
|
|
226
|
+
}
|
|
227
|
+
const close = () => {
|
|
228
|
+
closing ??= (async () => {
|
|
229
|
+
if (handleSignals) {
|
|
230
|
+
process.off("SIGINT", onSignal);
|
|
231
|
+
process.off("SIGTERM", onSignal);
|
|
232
|
+
}
|
|
233
|
+
if (retentionTimer)
|
|
234
|
+
clearInterval(retentionTimer);
|
|
235
|
+
sse.close();
|
|
236
|
+
server.closeAllConnections();
|
|
237
|
+
await new Promise((resolve) => server.close(() => resolve()));
|
|
238
|
+
for (const fn of cleanup.reverse()) {
|
|
239
|
+
try {
|
|
240
|
+
fn();
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
log(`shutdown step failed: ${error.message}`);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
log("skillar stopped");
|
|
247
|
+
})();
|
|
248
|
+
return closing;
|
|
249
|
+
};
|
|
250
|
+
log(`skillar ${version} listening on http://${HOST}:${boundPort}`);
|
|
251
|
+
return { port: boundPort, host: HOST, close };
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
for (const fn of cleanup.reverse()) {
|
|
255
|
+
try {
|
|
256
|
+
fn();
|
|
257
|
+
}
|
|
258
|
+
catch {
|
|
259
|
+
// Startup already failed; the original error is the one worth reporting.
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (!store)
|
|
263
|
+
db.close();
|
|
264
|
+
log(`startup failed: ${error.message}`);
|
|
265
|
+
throw error;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const AUTOMATION_RULES = [
|
|
2
|
+
// Codex TUI opens a side conversation per thread to name it.
|
|
3
|
+
{ id: "codex_title", label: "Codex thread title", test: /^\s*Generate a concise, single-line task title\b/ },
|
|
4
|
+
// Herdr asks an agent to recap a pane: the prompt is a JSON object that starts with previousRecap.
|
|
5
|
+
{ id: "herdr_recap", label: "Herdr pane recap", test: /^\s*\{\s*"previousRecap"\s*:/ },
|
|
6
|
+
// OpenClaw scheduled jobs prefix the prompt with the cron job id and name.
|
|
7
|
+
{ id: "cron_job", label: "Scheduled job", test: /^\s*\[cron:[0-9a-f-]{36} / },
|
|
8
|
+
// OmO memory reflection runs open with the reflection run file.
|
|
9
|
+
{ id: "memory_reflection", label: "Memory reflection", test: /^\s*<file name="[^"]*\/runtime\/reflection\// },
|
|
10
|
+
// Memory consolidation ("dreaming") turns fragments into a diary entry.
|
|
11
|
+
{ id: "memory_dream", label: "Memory dream", test: /^\s*Write a dream diary entry from these memory fragments/ },
|
|
12
|
+
];
|
|
13
|
+
export const NO_ACTIVITY = "no_activity";
|
|
14
|
+
export function automationOf(firstPrompt) {
|
|
15
|
+
if (!firstPrompt)
|
|
16
|
+
return undefined;
|
|
17
|
+
return AUTOMATION_RULES.find((rule) => rule.test.test(firstPrompt))?.id;
|
|
18
|
+
}
|
package/dist/store/db.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { DatabaseSync } from "node:sqlite";
|
|
4
|
+
import { SkillarStartupError } from "../version.js";
|
|
5
|
+
export const SCHEMA_VERSION = 6;
|
|
6
|
+
/** migrations[i] upgrades user_version i -> i + 1. */
|
|
7
|
+
function migrations() {
|
|
8
|
+
return [
|
|
9
|
+
readFileSync(new URL("./schema.sql", import.meta.url), "utf8"),
|
|
10
|
+
readFileSync(new URL("./migrations/002_tracing.sql", import.meta.url), "utf8"),
|
|
11
|
+
readFileSync(new URL("./migrations/003_tracing_indexes.sql", import.meta.url), "utf8"),
|
|
12
|
+
readFileSync(new URL("./migrations/004_load_reconciliation.sql", import.meta.url), "utf8"),
|
|
13
|
+
readFileSync(new URL("./migrations/005_execution_boundaries.sql", import.meta.url), "utf8"),
|
|
14
|
+
readFileSync(new URL("./migrations/006_recording_order.sql", import.meta.url), "utf8"),
|
|
15
|
+
];
|
|
16
|
+
}
|
|
17
|
+
function userVersion(db) {
|
|
18
|
+
const row = db.prepare("PRAGMA user_version").get();
|
|
19
|
+
return row?.user_version ?? 0;
|
|
20
|
+
}
|
|
21
|
+
export function openDb(path) {
|
|
22
|
+
if (path !== ":memory:")
|
|
23
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
24
|
+
const db = new DatabaseSync(path);
|
|
25
|
+
db.exec("PRAGMA journal_mode = WAL");
|
|
26
|
+
db.exec("PRAGMA busy_timeout = 5000");
|
|
27
|
+
db.exec("PRAGMA foreign_keys = ON");
|
|
28
|
+
migrate(db);
|
|
29
|
+
mergeSplitSessions(db);
|
|
30
|
+
return db;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Folds sessions that one runtime recorded twice under different id columns (before upsertSession matched either column,
|
|
34
|
+
* Codex OTel rows carried only conversation_id while its MCP rows carried runtime_session_id) into the earliest row.
|
|
35
|
+
* Idempotent: a database without such pairs is left unchanged.
|
|
36
|
+
*/
|
|
37
|
+
export function mergeSplitSessions(db) {
|
|
38
|
+
const pairs = db.prepare(`SELECT d.id AS dup, (
|
|
39
|
+
SELECT k.id FROM sessions k WHERE k.runtime = d.runtime AND k.id <> d.id
|
|
40
|
+
AND (k.runtime_session_id IN (d.runtime_session_id, d.conversation_id) OR k.conversation_id IN (d.runtime_session_id, d.conversation_id))
|
|
41
|
+
AND (k.started_at < d.started_at OR (k.started_at = d.started_at AND k.id < d.id))
|
|
42
|
+
ORDER BY k.started_at, k.id LIMIT 1) AS keep
|
|
43
|
+
FROM sessions d WHERE COALESCE(d.runtime_session_id, d.conversation_id) IS NOT NULL`).all();
|
|
44
|
+
const merges = pairs.filter((p) => p.keep !== null);
|
|
45
|
+
if (merges.length === 0)
|
|
46
|
+
return 0;
|
|
47
|
+
// Resolve chains (c -> b -> a) to their root so every duplicate lands on the earliest row.
|
|
48
|
+
const parent = new Map(merges.map((m) => [m.dup, m.keep]));
|
|
49
|
+
const root = (id) => (parent.has(id) ? root(parent.get(id)) : id);
|
|
50
|
+
db.exec("BEGIN IMMEDIATE");
|
|
51
|
+
try {
|
|
52
|
+
const moveEvents = db.prepare("UPDATE events SET session_id = ? WHERE session_id = ?");
|
|
53
|
+
// A pane link holds a foreign key to the duplicate, and a load candidate on both rows is one candidate.
|
|
54
|
+
const movePanes = db.prepare("UPDATE pane_sessions SET session_id = ? WHERE session_id = ?");
|
|
55
|
+
const moveCandidates = db.prepare("UPDATE OR IGNORE load_candidates SET session_id = ? WHERE session_id = ?");
|
|
56
|
+
const dropCandidates = db.prepare("DELETE FROM load_candidates WHERE session_id = ?");
|
|
57
|
+
const absorb = db.prepare(`UPDATE sessions SET
|
|
58
|
+
runtime_session_id = COALESCE(runtime_session_id, (SELECT runtime_session_id FROM sessions WHERE id = ?)),
|
|
59
|
+
conversation_id = COALESCE(conversation_id, (SELECT conversation_id FROM sessions WHERE id = ?)),
|
|
60
|
+
client_name = COALESCE(client_name, (SELECT client_name FROM sessions WHERE id = ?)),
|
|
61
|
+
cwd = COALESCE(cwd, (SELECT cwd FROM sessions WHERE id = ?)),
|
|
62
|
+
first_prompt = COALESCE(first_prompt, (SELECT first_prompt FROM sessions WHERE id = ?)),
|
|
63
|
+
ended_at = MAX(COALESCE(ended_at, 0), COALESCE((SELECT ended_at FROM sessions WHERE id = ?), 0))
|
|
64
|
+
WHERE id = ?`);
|
|
65
|
+
const drop = db.prepare("DELETE FROM sessions WHERE id = ?");
|
|
66
|
+
for (const { dup } of merges) {
|
|
67
|
+
const keep = root(dup);
|
|
68
|
+
moveEvents.run(keep, dup);
|
|
69
|
+
movePanes.run(keep, dup);
|
|
70
|
+
moveCandidates.run(keep, dup);
|
|
71
|
+
dropCandidates.run(dup);
|
|
72
|
+
absorb.run(dup, dup, dup, dup, dup, dup, keep);
|
|
73
|
+
drop.run(dup);
|
|
74
|
+
}
|
|
75
|
+
db.exec("UPDATE sessions SET ended_at = NULL WHERE ended_at = 0");
|
|
76
|
+
db.exec("COMMIT");
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
db.exec("ROLLBACK");
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
return merges.length;
|
|
83
|
+
}
|
|
84
|
+
export function migrate(db) {
|
|
85
|
+
const steps = migrations();
|
|
86
|
+
let version = userVersion(db);
|
|
87
|
+
while (version < steps.length) {
|
|
88
|
+
const sql = steps[version];
|
|
89
|
+
db.exec("BEGIN IMMEDIATE");
|
|
90
|
+
try {
|
|
91
|
+
db.exec(sql);
|
|
92
|
+
db.exec(`PRAGMA user_version = ${version + 1}`);
|
|
93
|
+
db.exec("COMMIT");
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
db.exec("ROLLBACK");
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
version = userVersion(db);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Proves FTS5 with the trigram tokenizer works and matches Korean substrings.
|
|
104
|
+
* Uses a TEMP table on the given connection (or a fresh in-memory DB) and drops it afterwards.
|
|
105
|
+
*/
|
|
106
|
+
export function selfCheck(db) {
|
|
107
|
+
const conn = db ?? new DatabaseSync(":memory:");
|
|
108
|
+
let ok = false;
|
|
109
|
+
let detail = "";
|
|
110
|
+
try {
|
|
111
|
+
conn.exec("DROP TABLE IF EXISTS temp.skillar_selfcheck");
|
|
112
|
+
conn.exec("CREATE VIRTUAL TABLE temp.skillar_selfcheck USING fts5(body, tokenize = 'trigram')");
|
|
113
|
+
conn.prepare("INSERT INTO temp.skillar_selfcheck(body) VALUES (?)").run("커밋 메시지 작성해줘");
|
|
114
|
+
const row = conn.prepare("SELECT count(*) AS n FROM temp.skillar_selfcheck WHERE skillar_selfcheck MATCH ?").get('"메시지"');
|
|
115
|
+
ok = (row?.n ?? 0) === 1;
|
|
116
|
+
if (!ok)
|
|
117
|
+
detail = "trigram match for '메시지' returned no rows";
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
detail = error instanceof Error ? error.message : String(error);
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
try {
|
|
124
|
+
conn.exec("DROP TABLE IF EXISTS temp.skillar_selfcheck");
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
// Cleanup failure is irrelevant once the outcome is known; the original failure is reported below.
|
|
128
|
+
}
|
|
129
|
+
if (!db)
|
|
130
|
+
conn.close();
|
|
131
|
+
}
|
|
132
|
+
if (!ok)
|
|
133
|
+
throw new SkillarStartupError("fts5_trigram_unavailable", detail);
|
|
134
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { assembleSession } from "../analysis/tree.js";
|
|
2
|
+
import { parseJson } from "./writes.js";
|
|
3
|
+
/** Local recording cuts resolve equal timestamps, not the runtime's unrecorded causal order. */
|
|
4
|
+
export function executionEventScope(db, flow, warningSessionIds = [], includeSpanCut = true) {
|
|
5
|
+
const from = flow.registered_ms ?? 0;
|
|
6
|
+
const to = flow.ended_ms;
|
|
7
|
+
const candidates = executionCandidates(db, flow);
|
|
8
|
+
const sessionIds = JSON.stringify([...new Set(candidates.links.flatMap((link) => link.sessionId === null ? [] : [link.sessionId]))]);
|
|
9
|
+
const candidateIds = JSON.stringify([...new Set([...candidates.links.flatMap((link) => link.sessionId === null ? [] : [link.sessionId]),
|
|
10
|
+
...warningSessionIds])]);
|
|
11
|
+
const cut = db.prepare(`SELECT start_event_ids_json, end_event_ids_json, end_sessions_json, end_spans_json,
|
|
12
|
+
start_order_domain, start_order, end_order_domain, end_order FROM flows WHERE id = ?`).get(flow.id);
|
|
13
|
+
const startPath = cut.start_order_domain === null ? null : `$."${cut.start_order_domain}"`;
|
|
14
|
+
const endPath = cut.end_order_domain === null ? null : `$."${cut.end_order_domain}"`;
|
|
15
|
+
const startOrder = cut.start_order;
|
|
16
|
+
const rows = db.prepare(`WITH execution_events AS (
|
|
17
|
+
SELECT e.id, e.session_id, e.ts, e.rowid AS recording_order, e.recording_orders_json FROM events e
|
|
18
|
+
WHERE e.session_id IN (SELECT value FROM json_each(?)) AND e.ts BETWEEN ? AND ?
|
|
19
|
+
UNION
|
|
20
|
+
SELECT e.id, e.session_id, e.ts, e.rowid AS recording_order, e.recording_orders_json FROM load_candidates c
|
|
21
|
+
CROSS JOIN events e ON e.id = c.load_id
|
|
22
|
+
WHERE c.session_id IN (SELECT value FROM json_each(?)) AND c.ts BETWEEN ? AND ?
|
|
23
|
+
AND e.kind = 'load' AND e.source = 'mcp'
|
|
24
|
+
) SELECT id, session_id, ts, json_extract(recording_orders_json, ?) AS start_position,
|
|
25
|
+
json_extract(recording_orders_json, ?) AS end_position FROM execution_events
|
|
26
|
+
ORDER BY ts, recording_order`).all(sessionIds, from, to ?? Number.MAX_SAFE_INTEGER, candidateIds, from, to ?? Number.MAX_SAFE_INTEGER, startPath, endPath);
|
|
27
|
+
const legacyEnds = new Set(parseJson(cut.end_event_ids_json, []));
|
|
28
|
+
const owned = rows.filter((row) => (row.ts > from || (cut.start_order !== null && row.start_position !== null && row.start_position > cut.start_order)) &&
|
|
29
|
+
(to === null || row.ts < to || (cut.end_order !== null
|
|
30
|
+
? row.end_position !== null && row.end_position <= cut.end_order : legacyEnds.has(row.id))));
|
|
31
|
+
let unavailable = cut.start_order === null || (to !== null && cut.end_order === null) ||
|
|
32
|
+
rows.some((row) => (row.ts === from && row.start_position === null) || (row.ts === to && row.end_position === null));
|
|
33
|
+
const spanCut = includeSpanCut && to !== null && cut.end_order !== null
|
|
34
|
+
? executionBoundarySpans(db, { sessionIds, from, at: to, path: endPath, position: cut.end_order }) : null;
|
|
35
|
+
if (spanCut?.unavailable)
|
|
36
|
+
unavailable = true;
|
|
37
|
+
const sessionEnds = cut.end_sessions_json === null ? null : parseJson(cut.end_sessions_json, {});
|
|
38
|
+
if (sessionEnds !== null && to !== null && cut.end_order !== null) {
|
|
39
|
+
const ends = db.prepare(`SELECT id, json_extract(end_orders_json, ?) AS position FROM sessions
|
|
40
|
+
WHERE id IN (SELECT value FROM json_each(?)) AND ended_at = ?`).all(endPath, sessionIds, to);
|
|
41
|
+
for (const end of ends) {
|
|
42
|
+
if (Object.hasOwn(sessionEnds, end.id))
|
|
43
|
+
continue;
|
|
44
|
+
if (end.position === null)
|
|
45
|
+
unavailable = true;
|
|
46
|
+
else if (end.position <= cut.end_order)
|
|
47
|
+
sessionEnds[end.id] = to;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const activity = new Map();
|
|
51
|
+
for (const row of owned) {
|
|
52
|
+
const times = activity.get(row.session_id);
|
|
53
|
+
if (times)
|
|
54
|
+
times.last = row.ts;
|
|
55
|
+
else
|
|
56
|
+
activity.set(row.session_id, { first: row.ts, last: row.ts });
|
|
57
|
+
}
|
|
58
|
+
const links = candidates.links.flatMap(({ startedAt, ...link }) => {
|
|
59
|
+
if (link.sessionId === null)
|
|
60
|
+
return [link];
|
|
61
|
+
const times = activity.get(link.sessionId);
|
|
62
|
+
if (!times && !(to === null && startedAt !== null && startedAt > from))
|
|
63
|
+
return [];
|
|
64
|
+
if (startedAt === null)
|
|
65
|
+
return [];
|
|
66
|
+
// Reconciliation can refresh link observations; activity bounds must not drift with those updates.
|
|
67
|
+
return [{ ...link, firstSeenMs: Math.max(from, Math.min(startedAt, times?.first ?? startedAt)),
|
|
68
|
+
lastSeenMs: Math.min(to ?? Number.MAX_SAFE_INTEGER, Math.max(startedAt, times?.last ?? startedAt)) }];
|
|
69
|
+
});
|
|
70
|
+
return { from, to, paneIds: candidates.paneIds, links, eventIds: owned.map((row) => row.id),
|
|
71
|
+
priorEventIds: startOrder === null ? parseJson(cut.start_event_ids_json, []) :
|
|
72
|
+
rows.filter((row) => row.ts === from && row.start_position !== null && row.start_position <= startOrder).map((row) => row.id),
|
|
73
|
+
sessionEnds,
|
|
74
|
+
spanKeys: spanCut?.keys ?? (cut.end_spans_json === null ? null :
|
|
75
|
+
parseJson(cut.end_spans_json, []).map((key) => JSON.stringify(key))),
|
|
76
|
+
boundary: unavailable ? "order_unavailable" : "recorded" };
|
|
77
|
+
}
|
|
78
|
+
/** Derive possible membership before reading events; activity refines it, never defines its query population. */
|
|
79
|
+
export function executionCandidates(db, flow) {
|
|
80
|
+
const from = flow.registered_ms ?? 0;
|
|
81
|
+
const to = flow.ended_ms ?? Number.MAX_SAFE_INTEGER;
|
|
82
|
+
const paneIds = db.prepare(`SELECT id, created_ms FROM herdr_panes
|
|
83
|
+
WHERE workspace_id = ? AND (? IS NULL OR tab_id = ?)
|
|
84
|
+
AND COALESCE(created_ms, ?) <= ? AND COALESCE(closed_ms, ?) >= ?
|
|
85
|
+
UNION SELECT fs.pane_id, p.created_ms FROM flow_steps fs LEFT JOIN herdr_panes p ON p.id = fs.pane_id
|
|
86
|
+
WHERE fs.flow_id = ? ORDER BY created_ms, id`)
|
|
87
|
+
.all(flow.workspace_id, flow.tab_id, flow.tab_id, from, to, to, from, flow.id).map((p) => p.id);
|
|
88
|
+
if (!paneIds.length)
|
|
89
|
+
return { paneIds, links: [] };
|
|
90
|
+
const links = db.prepare(`SELECT ps.pane_id AS paneId, ps.session_id AS sessionId, s.started_at AS startedAt,
|
|
91
|
+
ps.agent_session_value AS agentSessionValue, ps.source,
|
|
92
|
+
MIN(MAX(COALESCE(ps.first_seen_ms, ?), ?), ?) AS firstSeenMs,
|
|
93
|
+
MAX(MIN(COALESCE(ps.last_seen_ms, ?), ?), ?) AS lastSeenMs
|
|
94
|
+
FROM pane_sessions ps LEFT JOIN sessions s ON s.id = ps.session_id
|
|
95
|
+
WHERE ps.pane_id IN (SELECT value FROM json_each(?)) AND (
|
|
96
|
+
ps.session_id IS NOT NULL OR ps.first_seen_ms BETWEEN ? AND ?)
|
|
97
|
+
ORDER BY ps.first_seen_ms, ps.agent_session_value`)
|
|
98
|
+
.all(from, from, to, to, to, from, JSON.stringify(paneIds), from, to);
|
|
99
|
+
return { paneIds, links };
|
|
100
|
+
}
|
|
101
|
+
/** Resolve the persisted scalar against eventual members, never snapshot unrelated historical spans. */
|
|
102
|
+
function executionBoundarySpans(db, cut) {
|
|
103
|
+
// An endpoint span can link to an interior event, not only an event at the endpoint.
|
|
104
|
+
const rows = db.prepare(`WITH session AS (
|
|
105
|
+
SELECT id, runtime, runtime_session_id, conversation_id FROM sessions WHERE id IN (SELECT value FROM json_each(?))
|
|
106
|
+
), matched AS (
|
|
107
|
+
SELECT o.trace_id, o.span_id FROM session s CROSS JOIN otel_spans o
|
|
108
|
+
WHERE o.conversation_id IN (s.runtime_session_id, s.conversation_id) AND o.runtime = s.runtime
|
|
109
|
+
UNION
|
|
110
|
+
SELECT o.trace_id, o.span_id FROM session s JOIN events e ON e.session_id = s.id AND e.ts BETWEEN ? AND ? CROSS JOIN otel_spans o
|
|
111
|
+
WHERE o.call_id = e.call_id AND o.runtime = s.runtime
|
|
112
|
+
AND (o.conversation_id IS NULL OR o.conversation_id IN (s.runtime_session_id, s.conversation_id))
|
|
113
|
+
UNION
|
|
114
|
+
SELECT o.trace_id, o.span_id FROM session s JOIN events e ON e.session_id = s.id AND e.ts BETWEEN ? AND ? CROSS JOIN otel_spans o
|
|
115
|
+
WHERE o.trace_id = json_extract(e.data_json, '$.otelTraceId') AND o.span_id = e.span_id AND o.runtime = s.runtime
|
|
116
|
+
AND (o.conversation_id IS NULL OR o.conversation_id IN (s.runtime_session_id, s.conversation_id))
|
|
117
|
+
) SELECT o.trace_id, o.span_id, json_extract(o.recording_orders_json, ?) AS position
|
|
118
|
+
FROM matched m JOIN otel_spans o ON o.trace_id = m.trace_id AND o.span_id = m.span_id
|
|
119
|
+
WHERE o.start_ms IS NULL OR o.start_ms = ? ORDER BY o.trace_id, o.span_id`)
|
|
120
|
+
.all(cut.sessionIds, cut.from, cut.at, cut.from, cut.at, cut.path, cut.at);
|
|
121
|
+
return { keys: rows.filter((row) => row.position !== null && row.position <= cut.position)
|
|
122
|
+
.map((row) => JSON.stringify([row.trace_id, row.span_id])), unavailable: rows.some((row) => row.position === null) };
|
|
123
|
+
}
|
|
124
|
+
/** Keep turn identity from the full assembly, but never carry observations across execution boundaries. */
|
|
125
|
+
export function scopeSession(session, scope) {
|
|
126
|
+
const { from, to } = scope;
|
|
127
|
+
const end = to ?? Number.MAX_SAFE_INTEGER;
|
|
128
|
+
const owned = new Set(scope.eventIds);
|
|
129
|
+
const inside = (event) => owned.has(event.id);
|
|
130
|
+
const turns = session.turns.flatMap((turn, index) => {
|
|
131
|
+
const events = turn.events.filter(inside);
|
|
132
|
+
if (!events.length)
|
|
133
|
+
return [];
|
|
134
|
+
const original = new Map(events.map((e) => [e.id, e]));
|
|
135
|
+
const endedAt = Math.min(end, session.turns[index + 1]?.startedAt ?? session.endedAt ?? (to ?? turn.endedAt));
|
|
136
|
+
const startedAt = Math.max(from, turn.startedAt);
|
|
137
|
+
// Reassemble activations from in-window events: the same skill may be activated again in a continuing turn.
|
|
138
|
+
// An internal boundary preserves identity when the real prompt preceded registration; it never leaves here.
|
|
139
|
+
const scoped = assembleSession({ id: session.id, runtime: session.runtime, startedAt, endedAt, spans: [],
|
|
140
|
+
events: [{ id: `execution-boundary:${turn.id}`, ts: startedAt, kind: "prompt", source: "hook",
|
|
141
|
+
turnKey: turn.id, callId: null, spanId: null, skillUri: null, text: null, textLength: null, data: {} },
|
|
142
|
+
...events.map((e) => ({ ...e, turnKey: turn.id }))] }).turns[0];
|
|
143
|
+
if (!scoped)
|
|
144
|
+
return [];
|
|
145
|
+
const restore = (event) => {
|
|
146
|
+
const source = original.get(event.id);
|
|
147
|
+
return source ? { ...event, turnKey: source.turnKey, basis: source.basis } : event;
|
|
148
|
+
};
|
|
149
|
+
return [{ ...scoped, basis: turn.basis, events: scoped.events.filter((e) => original.has(e.id)).map(restore),
|
|
150
|
+
skillSpans: scoped.skillSpans.map((span) => ({ ...span, activation: restore(span.activation), events: span.events.map(restore) })) }];
|
|
151
|
+
});
|
|
152
|
+
// An execution end clips analysis, never observed lifecycle. A later session end must not rewrite A.
|
|
153
|
+
const endedAt = session.endedAt !== null && (session.endedAt < end ||
|
|
154
|
+
(session.endedAt === end && scope.sessionEnds?.[session.id] === end)) ? session.endedAt : null;
|
|
155
|
+
return { ...session, endedAt,
|
|
156
|
+
executionWindow: { from, to }, turns, unassignedEvents: session.unassignedEvents.filter(inside) };
|
|
157
|
+
}
|