@unerr-ai/unerr 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/dist/cli.js +37236 -35793
- package/package.json +6 -1
- package/dist/behaviors/agent-llm-bridge.js +0 -166
- package/dist/behaviors/architecture-guard.js +0 -256
- package/dist/behaviors/auto-doc.js +0 -247
- package/dist/behaviors/cascade-guard.js +0 -289
- package/dist/behaviors/change-narrative.js +0 -270
- package/dist/behaviors/convention-drift.js +0 -290
- package/dist/behaviors/framework.js +0 -235
- package/dist/behaviors/guard-formatter.js +0 -44
- package/dist/behaviors/incomplete-work.js +0 -270
- package/dist/behaviors/loop-breaker.js +0 -300
- package/dist/behaviors/session-continuity.js +0 -208
- package/dist/commands/branches.js +0 -97
- package/dist/commands/check-commit.js +0 -225
- package/dist/commands/compress-output.js +0 -64
- package/dist/commands/config-verify.js +0 -243
- package/dist/commands/daemon.js +0 -905
- package/dist/commands/dashboard.js +0 -52
- package/dist/commands/debug.js +0 -200
- package/dist/commands/enrich.js +0 -184
- package/dist/commands/exec.js +0 -233
- package/dist/commands/gain.js +0 -156
- package/dist/commands/hook.js +0 -88
- package/dist/commands/index.js +0 -88
- package/dist/commands/init.js +0 -74
- package/dist/commands/install.js +0 -505
- package/dist/commands/learn.js +0 -116
- package/dist/commands/manifest.js +0 -193
- package/dist/commands/rewind.js +0 -103
- package/dist/commands/serve.js +0 -19
- package/dist/commands/setup-wizard.js +0 -414
- package/dist/commands/skills.js +0 -64
- package/dist/commands/stats.js +0 -20
- package/dist/commands/status.js +0 -654
- package/dist/commands/timeline.js +0 -139
- package/dist/commands/uninstall.js +0 -230
- package/dist/components/App.js +0 -109
- package/dist/components/Banner.js +0 -12
- package/dist/components/ConfirmPrompt.js +0 -25
- package/dist/components/DriftSummary.js +0 -23
- package/dist/components/GradeBadge.js +0 -15
- package/dist/components/HealthCard.js +0 -18
- package/dist/components/InkSpinner.js +0 -22
- package/dist/components/InputBox.js +0 -17
- package/dist/components/KeyValue.js +0 -13
- package/dist/components/MessageList.js +0 -14
- package/dist/components/ProgressBar.js +0 -26
- package/dist/components/Section.js +0 -16
- package/dist/components/SessionSummaryCard.js +0 -73
- package/dist/components/StartupDisplay.js +0 -24
- package/dist/components/StatusDashboard.js +0 -57
- package/dist/components/StatusLine.js +0 -8
- package/dist/components/StepLine.js +0 -22
- package/dist/components/Theme.js +0 -20
- package/dist/components/ToolProgress.js +0 -8
- package/dist/components/ViolationList.js +0 -21
- package/dist/components/render.js +0 -13
- package/dist/config/agent-registry.js +0 -237
- package/dist/config/claude-settings-hooks.js +0 -304
- package/dist/config/hook-installer.js +0 -65
- package/dist/config/instruction-writer.js +0 -388
- package/dist/config/mcp-config-writer.js +0 -266
- package/dist/config/settings.js +0 -174
- package/dist/config/tool-detector.js +0 -42
- package/dist/config/value-surfacing.js +0 -119
- package/dist/core/context-assembly.js +0 -108
- package/dist/core/conversation.js +0 -33
- package/dist/core/local-chat-provider.js +0 -475
- package/dist/core/provider-factory.js +0 -55
- package/dist/core/providers.js +0 -90
- package/dist/core/query-engine.js +0 -174
- package/dist/daemon/api.js +0 -312
- package/dist/daemon/autostart.js +0 -119
- package/dist/daemon/bootstrap.js +0 -39
- package/dist/daemon/client.js +0 -164
- package/dist/daemon/detect-ci.js +0 -81
- package/dist/daemon/platform-linux.js +0 -146
- package/dist/daemon/platform-macos.js +0 -134
- package/dist/daemon/platform-windows.js +0 -116
- package/dist/daemon/process-manager.js +0 -299
- package/dist/daemon/protocol.js +0 -23
- package/dist/daemon/registry.js +0 -270
- package/dist/daemon/settings-schema.js +0 -72
- package/dist/daemon/system-health.js +0 -134
- package/dist/daemon/version-checker.js +0 -262
- package/dist/daemon/warm-start.js +0 -223
- package/dist/entrypoints/cli.js +0 -1043
- package/dist/entrypoints/daemon.js +0 -380
- package/dist/entrypoints/repl.js +0 -147
- package/dist/hooks/adapters/claude-code.js +0 -90
- package/dist/hooks/adapters/cline.js +0 -100
- package/dist/hooks/adapters/cursor.js +0 -98
- package/dist/hooks/hook-dedup.js +0 -79
- package/dist/hooks/hook-runner.js +0 -113
- package/dist/hooks/navigation-hooks.js +0 -175
- package/dist/hooks/prompt-hooks.js +0 -63
- package/dist/hooks/shell-hooks.js +0 -47
- package/dist/ignore.js +0 -111
- package/dist/intelligence/approach-suggester.js +0 -61
- package/dist/intelligence/ast-extractor.js +0 -2615
- package/dist/intelligence/ast-worker.js +0 -34
- package/dist/intelligence/background-indexer.js +0 -121
- package/dist/intelligence/blast-radius.js +0 -200
- package/dist/intelligence/community-detection.js +0 -691
- package/dist/intelligence/community-detector.js +0 -184
- package/dist/intelligence/computation-scheduler.js +0 -75
- package/dist/intelligence/confidence-propagation.js +0 -47
- package/dist/intelligence/convention-detector.js +0 -242
- package/dist/intelligence/convention-learner.js +0 -205
- package/dist/intelligence/convention-matcher.js +0 -205
- package/dist/intelligence/cozo-schema.js +0 -376
- package/dist/intelligence/decision-point-detector.js +0 -90
- package/dist/intelligence/deep-dive-tools.js +0 -586
- package/dist/intelligence/durability-scorer.js +0 -84
- package/dist/intelligence/exploration-cost.js +0 -204
- package/dist/intelligence/exploration-pattern-tracker.js +0 -61
- package/dist/intelligence/fact-generator.js +0 -322
- package/dist/intelligence/facts-schema.js +0 -90
- package/dist/intelligence/file-intelligence.js +0 -59
- package/dist/intelligence/graph-holder.js +0 -220
- package/dist/intelligence/graph-temporal-joiner.js +0 -238
- package/dist/intelligence/health-grade.js +0 -423
- package/dist/intelligence/health-grader.js +0 -200
- package/dist/intelligence/health-map-data.js +0 -259
- package/dist/intelligence/import-symbols.js +0 -136
- package/dist/intelligence/incremental-indexer.js +0 -658
- package/dist/intelligence/indexer/centrality.js +0 -62
- package/dist/intelligence/indexer/cfg-context.js +0 -95
- package/dist/intelligence/indexer/confidence.js +0 -34
- package/dist/intelligence/indexer/cross-file-resolver.js +0 -104
- package/dist/intelligence/indexer/edge-repair.js +0 -89
- package/dist/intelligence/indexer/entity-key.js +0 -17
- package/dist/intelligence/indexer/export-map.js +0 -132
- package/dist/intelligence/indexer/git-cochange.js +0 -128
- package/dist/intelligence/indexer/graph-patch.js +0 -147
- package/dist/intelligence/indexer/incremental.js +0 -78
- package/dist/intelligence/indexer/ingest.js +0 -160
- package/dist/intelligence/indexer/language-detect.js +0 -226
- package/dist/intelligence/indexer/metadata.js +0 -63
- package/dist/intelligence/indexer/mutation-tracker.js +0 -79
- package/dist/intelligence/indexer/orchestrator.js +0 -155
- package/dist/intelligence/indexer/plugin-interface.js +0 -31
- package/dist/intelligence/indexer/plugins/csharp.js +0 -440
- package/dist/intelligence/indexer/plugins/go.js +0 -335
- package/dist/intelligence/indexer/plugins/java.js +0 -370
- package/dist/intelligence/indexer/plugins/python.js +0 -358
- package/dist/intelligence/indexer/plugins/regex-fallback.js +0 -82
- package/dist/intelligence/indexer/plugins/ruby.js +0 -290
- package/dist/intelligence/indexer/plugins/rust.js +0 -484
- package/dist/intelligence/indexer/plugins/tier2-generic.js +0 -310
- package/dist/intelligence/indexer/plugins/typescript.js +0 -456
- package/dist/intelligence/indexer/resource-monitor.js +0 -93
- package/dist/intelligence/indexer/scip/decoder.js +0 -253
- package/dist/intelligence/indexer/scip/detector.js +0 -232
- package/dist/intelligence/indexer/scip/downloader.js +0 -427
- package/dist/intelligence/indexer/scip/fallback.js +0 -34
- package/dist/intelligence/indexer/scip/merger.js +0 -109
- package/dist/intelligence/indexer/scip/orchestrator.js +0 -433
- package/dist/intelligence/indexer/scip/runner.js +0 -98
- package/dist/intelligence/indexer/snapshot.js +0 -66
- package/dist/intelligence/indexer/test-detector.js +0 -196
- package/dist/intelligence/indexer/watch-integration.js +0 -61
- package/dist/intelligence/indexer/worker.js +0 -85
- package/dist/intelligence/local-convention-detector.js +0 -437
- package/dist/intelligence/local-embeddings.js +0 -190
- package/dist/intelligence/local-graph.js +0 -1946
- package/dist/intelligence/local-indexer.js +0 -1575
- package/dist/intelligence/local-llm.js +0 -163
- package/dist/intelligence/local-rule-generator.js +0 -154
- package/dist/intelligence/local-snapshot.js +0 -213
- package/dist/intelligence/negative-knowledge.js +0 -103
- package/dist/intelligence/persistent-db.js +0 -85
- package/dist/intelligence/query-router.js +0 -2556
- package/dist/intelligence/risk-classifier.js +0 -116
- package/dist/intelligence/rule-evaluator.js +0 -380
- package/dist/intelligence/rule-generator.js +0 -49
- package/dist/intelligence/search-index.js +0 -173
- package/dist/intelligence/semantic/docstring-extractor.js +0 -67
- package/dist/intelligence/semantic/embedding-store.js +0 -52
- package/dist/intelligence/semantic/enrichment-orchestrator.js +0 -48
- package/dist/intelligence/semantic/git-message-miner.js +0 -114
- package/dist/intelligence/semantic/identifier-tokenizer.js +0 -51
- package/dist/intelligence/semantic/node2vec-embeddings.js +0 -71
- package/dist/intelligence/semantic/node2vec-walks.js +0 -103
- package/dist/intelligence/semantic/path-domain-inference.js +0 -112
- package/dist/intelligence/semantic/similarity-engine.js +0 -60
- package/dist/intelligence/semantic/tfidf-vectors.js +0 -88
- package/dist/intelligence/session-brief-builder.js +0 -159
- package/dist/intelligence/session-context.js +0 -221
- package/dist/intelligence/session-health-monitor.js +0 -211
- package/dist/intelligence/session-narrative.js +0 -197
- package/dist/intelligence/session-pattern-analyzer.js +0 -218
- package/dist/intelligence/signal-scorer.js +0 -390
- package/dist/intelligence/signal-show-store.js +0 -182
- package/dist/intelligence/smart-truncate.js +0 -158
- package/dist/intelligence/subgraph-cache.js +0 -88
- package/dist/intelligence/temporal-facts.js +0 -494
- package/dist/intelligence/token-estimator.js +0 -100
- package/dist/intelligence/tool-injector.js +0 -87
- package/dist/intelligence/tree-sitter-loader.js +0 -71
- package/dist/intelligence/worker-pool.js +0 -116
- package/dist/proxy/arg-validator.js +0 -79
- package/dist/proxy/auto-bootstrap.js +0 -167
- package/dist/proxy/bridge.js +0 -147
- package/dist/proxy/budget-enforcer.js +0 -70
- package/dist/proxy/compression-quality-monitor.js +0 -160
- package/dist/proxy/compression-stats.js +0 -51
- package/dist/proxy/context-rot-detector.js +0 -137
- package/dist/proxy/drift-detector.js +0 -139
- package/dist/proxy/efficiency-tracker.js +0 -79
- package/dist/proxy/fact-ranking.js +0 -154
- package/dist/proxy/format-encoder.js +0 -266
- package/dist/proxy/http-transport.js +0 -90
- package/dist/proxy/lifecycle-actor.js +0 -55
- package/dist/proxy/lifecycle-machine.js +0 -187
- package/dist/proxy/log-tailer.js +0 -265
- package/dist/proxy/model-pricing.js +0 -98
- package/dist/proxy/network-firewall.js +0 -141
- package/dist/proxy/nudge-state.js +0 -93
- package/dist/proxy/output-compressor.js +0 -185
- package/dist/proxy/pid-lock.js +0 -291
- package/dist/proxy/proxy-context.js +0 -11
- package/dist/proxy/proxy.js +0 -2633
- package/dist/proxy/response-enrichment.js +0 -32
- package/dist/proxy/response-envelope.js +0 -313
- package/dist/proxy/session-dedup.js +0 -82
- package/dist/proxy/session-legend.js +0 -30
- package/dist/proxy/session-persistence.js +0 -210
- package/dist/proxy/session-resume.js +0 -94
- package/dist/proxy/session-stats.js +0 -513
- package/dist/proxy/shell-classifier.js +0 -1346
- package/dist/proxy/shell-compression-log.js +0 -93
- package/dist/proxy/shell-compressor.js +0 -390
- package/dist/proxy/shell-graph-boost.js +0 -202
- package/dist/proxy/shell-monitor-map.js +0 -18
- package/dist/proxy/shell-stats.js +0 -54
- package/dist/proxy/shell-strategies/cloud.js +0 -215
- package/dist/proxy/shell-strategies/diff.js +0 -159
- package/dist/proxy/shell-strategies/error-diagnostic.js +0 -796
- package/dist/proxy/shell-strategies/filter-dsl.js +0 -358
- package/dist/proxy/shell-strategies/git-status.js +0 -177
- package/dist/proxy/shell-strategies/key-value.js +0 -193
- package/dist/proxy/shell-strategies/log-text.js +0 -154
- package/dist/proxy/shell-strategies/omni.js +0 -188
- package/dist/proxy/shell-strategies/progress.js +0 -55
- package/dist/proxy/shell-strategies/redact.js +0 -76
- package/dist/proxy/shell-strategies/structured.js +0 -241
- package/dist/proxy/shell-strategies/tabular.js +0 -243
- package/dist/proxy/shell-strategies/test-results-types.js +0 -13
- package/dist/proxy/shell-strategies/test-results.js +0 -784
- package/dist/proxy/shell-strategies/tree-paths.js +0 -144
- package/dist/proxy/shell-strategies/yaml.js +0 -182
- package/dist/proxy/shell-tee.js +0 -111
- package/dist/proxy/signal-dedup.js +0 -171
- package/dist/proxy/startup-renderer.js +0 -158
- package/dist/proxy/task-token-display.js +0 -38
- package/dist/proxy/token-counter.js +0 -61
- package/dist/proxy/tool-clusters.js +0 -273
- package/dist/proxy/tool-definitions.js +0 -525
- package/dist/proxy/transport-mux.js +0 -229
- package/dist/proxy/wire-cap.js +0 -268
- package/dist/rules/developer.mozilla.org.json +0 -9
- package/dist/rules/github.com.json +0 -21
- package/dist/schemas/api/skills.js +0 -19
- package/dist/schemas/common/errors.js +0 -7
- package/dist/schemas/common/headers.js +0 -5
- package/dist/schemas/entities/edge.js +0 -25
- package/dist/schemas/entities/entity.js +0 -22
- package/dist/schemas/entities/rule.js +0 -18
- package/dist/schemas/index.js +0 -14
- package/dist/server/event-bus.js +0 -59
- package/dist/server/http.js +0 -156
- package/dist/server/middleware.js +0 -70
- package/dist/server/routes/drift.js +0 -97
- package/dist/server/routes/intelligence.js +0 -1217
- package/dist/server/routes/reasoning-quality.js +0 -444
- package/dist/server/routes/session.js +0 -86
- package/dist/server/routes/stream.js +0 -120
- package/dist/server/routes/system.js +0 -73
- package/dist/server/routes/temporal.js +0 -170
- package/dist/server/routes/timeline.js +0 -232
- package/dist/server/routes/token-flow.js +0 -403
- package/dist/skills/effectiveness-tracker.js +0 -93
- package/dist/skills/local-pack.js +0 -380
- package/dist/skills/resolver.js +0 -495
- package/dist/state-detector.js +0 -83
- package/dist/timeline/intent-detector.js +0 -263
- package/dist/timeline/loop-miner.js +0 -140
- package/dist/timeline/open-threads.js +0 -49
- package/dist/timeline/signal-reinforcer.js +0 -62
- package/dist/timeline/timeline-bootstrap.js +0 -151
- package/dist/timeline/timeline-store.js +0 -618
- package/dist/tools/coding/bash.js +0 -49
- package/dist/tools/coding/file-edit.js +0 -72
- package/dist/tools/coding/file-outline.js +0 -227
- package/dist/tools/coding/file-read-protocol.js +0 -425
- package/dist/tools/coding/file-read.js +0 -35
- package/dist/tools/coding/file-write.js +0 -43
- package/dist/tools/coding/glob-tool.js +0 -109
- package/dist/tools/coding/grep.js +0 -162
- package/dist/tools/coding/index.js +0 -27
- package/dist/tools/intelligence/index.js +0 -269
- package/dist/tools/intelligence/record-fact.js +0 -48
- package/dist/tools/intelligence/timeline-markers.js +0 -130
- package/dist/tools/registry.js +0 -47
- package/dist/tools/types.js +0 -8
- package/dist/tracking/auto-snapshot-triggers.js +0 -246
- package/dist/tracking/branch-context.js +0 -115
- package/dist/tracking/branch-snapshot.js +0 -217
- package/dist/tracking/causal-bridge.js +0 -317
- package/dist/tracking/circuit-breaker.js +0 -147
- package/dist/tracking/commit-watcher.js +0 -114
- package/dist/tracking/context-ledger.js +0 -119
- package/dist/tracking/correction-detector.js +0 -324
- package/dist/tracking/drift-tracker.js +0 -874
- package/dist/tracking/durability-tracker.js +0 -94
- package/dist/tracking/entity-rewind.js +0 -200
- package/dist/tracking/file-hash-state.js +0 -114
- package/dist/tracking/git-attribution.js +0 -132
- package/dist/tracking/git-trailers.js +0 -171
- package/dist/tracking/intelligence-counter.js +0 -46
- package/dist/tracking/intent-correlator.js +0 -202
- package/dist/tracking/intent-encoder.js +0 -52
- package/dist/tracking/intent-token-tracker.js +0 -159
- package/dist/tracking/ledger-archiver.js +0 -94
- package/dist/tracking/ledger-chains.js +0 -245
- package/dist/tracking/metrics-store.js +0 -361
- package/dist/tracking/native-watcher.js +0 -131
- package/dist/tracking/offline-rewind.js +0 -295
- package/dist/tracking/pending-violations.js +0 -74
- package/dist/tracking/persistence-effectiveness.js +0 -167
- package/dist/tracking/prompt-durability.js +0 -202
- package/dist/tracking/quality-signals.js +0 -213
- package/dist/tracking/redactor.js +0 -73
- package/dist/tracking/rewind-engine.js +0 -161
- package/dist/tracking/session-history.js +0 -128
- package/dist/tracking/session-receipt.js +0 -88
- package/dist/tracking/session-summary-writer.js +0 -157
- package/dist/tracking/shadow-ledger.js +0 -321
- package/dist/tracking/stash-manager.js +0 -258
- package/dist/tracking/timeline-fork.js +0 -213
- package/dist/tracking/timeline.js +0 -69
- package/dist/tracking/token-flow.js +0 -276
- package/dist/tracking/turn-segmenter.js +0 -122
- package/dist/tracking/weekly-accumulator.js +0 -179
- package/dist/tracking/working-snapshots.js +0 -188
- package/dist/tracking/workspace-manifest.js +0 -176
- package/dist/transport/http.js +0 -102
- package/dist/utils/counterfactual.js +0 -65
- package/dist/utils/deep-link.js +0 -34
- package/dist/utils/detect.js +0 -193
- package/dist/utils/exec.js +0 -73
- package/dist/utils/file-logger.js +0 -87
- package/dist/utils/format-error.js +0 -29
- package/dist/utils/git.js +0 -181
- package/dist/utils/log.js +0 -57
- package/dist/utils/logger.js +0 -35
- package/dist/utils/mcp-content-json.js +0 -8
- package/dist/utils/session-logger.js +0 -154
- package/dist/utils/startup-log.js +0 -512
- package/dist/utils/ui.js +0 -56
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shadow Ledger Archiver (ST-6).
|
|
3
|
-
*
|
|
4
|
-
* Splits `.unerr/ledger/shadow.jsonl` into:
|
|
5
|
-
* - kept : entries with `ts >= now - retainMs` (rewritten in place)
|
|
6
|
-
* - archived : older entries gzipped into
|
|
7
|
-
* `.unerr/ledger/archive/<YYYY-MM-DD>.jsonl.gz`
|
|
8
|
-
*
|
|
9
|
-
* Daily archive keeps recent context fast (small JSONL) without losing
|
|
10
|
-
* history — the gzip files are still grep-able with `zcat`. Derived facts in
|
|
11
|
-
* `timeline.db` / `facts.db` are NOT touched; archival only affects the raw
|
|
12
|
-
* append-only log.
|
|
13
|
-
*/
|
|
14
|
-
import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync, } from "node:fs";
|
|
15
|
-
import { join } from "node:path";
|
|
16
|
-
import { gzipSync } from "node:zlib";
|
|
17
|
-
const DEFAULT_RETAIN_MS = 7 * 24 * 60 * 60_000;
|
|
18
|
-
/**
|
|
19
|
-
* Run an archive pass over `<unerrDir>/ledger/shadow.jsonl`. Safe to run while
|
|
20
|
-
* the proxy is appending: we read the snapshot, rewrite atomically via a
|
|
21
|
-
* temp file + rename. Concurrent appends in the small window between read and
|
|
22
|
-
* rename are preserved by reading the *tail* of the file after rewrite and
|
|
23
|
-
* re-appending any new lines.
|
|
24
|
-
*/
|
|
25
|
-
export function archiveShadowLedger(unerrDir, opts = {}) {
|
|
26
|
-
const ledgerDir = join(unerrDir, "ledger");
|
|
27
|
-
const filePath = join(ledgerDir, "shadow.jsonl");
|
|
28
|
-
const archiveDir = join(ledgerDir, "archive");
|
|
29
|
-
if (!existsSync(filePath)) {
|
|
30
|
-
return { archived: 0, kept: 0, archivePath: null };
|
|
31
|
-
}
|
|
32
|
-
const retainMs = opts.retainMs ?? DEFAULT_RETAIN_MS;
|
|
33
|
-
const now = opts.nowMs ?? Date.now();
|
|
34
|
-
const cutoff = now - retainMs;
|
|
35
|
-
const raw = readFileSync(filePath, "utf-8");
|
|
36
|
-
const initialBytes = Buffer.byteLength(raw);
|
|
37
|
-
const lines = raw.split("\n").filter((l) => l.trim().length > 0);
|
|
38
|
-
const keep = [];
|
|
39
|
-
const archive = [];
|
|
40
|
-
for (const line of lines) {
|
|
41
|
-
let entryTs = Number.NaN;
|
|
42
|
-
try {
|
|
43
|
-
const obj = JSON.parse(line);
|
|
44
|
-
if (typeof obj.ts === "string")
|
|
45
|
-
entryTs = Date.parse(obj.ts);
|
|
46
|
-
}
|
|
47
|
-
catch {
|
|
48
|
-
// Unparseable lines are kept — recovery code further down the stack
|
|
49
|
-
// already deals with them.
|
|
50
|
-
keep.push(line);
|
|
51
|
-
continue;
|
|
52
|
-
}
|
|
53
|
-
if (Number.isFinite(entryTs) && entryTs < cutoff) {
|
|
54
|
-
archive.push(line);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
keep.push(line);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (archive.length === 0) {
|
|
61
|
-
return { archived: 0, kept: keep.length, archivePath: null };
|
|
62
|
-
}
|
|
63
|
-
mkdirSync(archiveDir, { recursive: true });
|
|
64
|
-
const dateStamp = new Date(now).toISOString().slice(0, 10);
|
|
65
|
-
const archivePath = join(archiveDir, `${dateStamp}.jsonl.gz`);
|
|
66
|
-
const payload = `${archive.join("\n")}\n`;
|
|
67
|
-
const gz = gzipSync(Buffer.from(payload, "utf-8"));
|
|
68
|
-
if (existsSync(archivePath)) {
|
|
69
|
-
// Same-day archive already exists — concatenate as a separate gzip stream.
|
|
70
|
-
// gzip handles concatenated streams correctly when read with `zcat`.
|
|
71
|
-
appendFileSync(archivePath, gz);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
writeFileSync(archivePath, gz);
|
|
75
|
-
}
|
|
76
|
-
// Rewrite shadow.jsonl atomically. We snapshot the file size before write
|
|
77
|
-
// and after, then re-append any bytes that landed between the two.
|
|
78
|
-
const tmpPath = `${filePath}.tmp-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
79
|
-
const kept = `${keep.join("\n")}${keep.length > 0 ? "\n" : ""}`;
|
|
80
|
-
writeFileSync(tmpPath, kept, "utf-8");
|
|
81
|
-
// Re-read the original to capture any lines appended during our work.
|
|
82
|
-
const afterRaw = readFileSync(filePath, "utf-8");
|
|
83
|
-
const afterBytes = Buffer.byteLength(afterRaw);
|
|
84
|
-
if (afterBytes > initialBytes) {
|
|
85
|
-
const tail = afterRaw.slice(raw.length);
|
|
86
|
-
appendFileSync(tmpPath, tail);
|
|
87
|
-
}
|
|
88
|
-
renameSync(tmpPath, filePath);
|
|
89
|
-
return {
|
|
90
|
-
archived: archive.length,
|
|
91
|
-
kept: keep.length,
|
|
92
|
-
archivePath,
|
|
93
|
-
};
|
|
94
|
-
}
|
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Ledger Chain Analyzer — causality-aware history from shadow ledger.
|
|
3
|
-
*
|
|
4
|
-
* Layer 9 PI-3: Transforms flat ledger entries into:
|
|
5
|
-
* 1. Correlation chains (grouped tool calls forming one intent)
|
|
6
|
-
* 2. Entity history (what happened to a specific entity across sessions)
|
|
7
|
-
* 3. Session timelines (grouped by session, summarized by feature area)
|
|
8
|
-
* 4. Revert pattern detection (chains that led to reverted code)
|
|
9
|
-
*
|
|
10
|
-
* All operations are read-only against the existing shadow.jsonl.
|
|
11
|
-
* No mutations to ledger data — this is an analysis layer.
|
|
12
|
-
*/
|
|
13
|
-
// ── Chain Extraction ─────────────────────────────────────────────────
|
|
14
|
-
/**
|
|
15
|
-
* Extract correlation chains from raw ledger entries.
|
|
16
|
-
* A chain is a root entry + all entries correlated to it.
|
|
17
|
-
*/
|
|
18
|
-
export function extractChains(entries) {
|
|
19
|
-
const rootMap = new Map();
|
|
20
|
-
for (const entry of entries) {
|
|
21
|
-
const rootId = entry.correlation_id ?? entry.id;
|
|
22
|
-
const group = rootMap.get(rootId);
|
|
23
|
-
if (group) {
|
|
24
|
-
group.push(entry);
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
rootMap.set(rootId, [entry]);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
const chains = [];
|
|
31
|
-
for (const [rootId, chainEntries] of rootMap) {
|
|
32
|
-
if (chainEntries.length === 0)
|
|
33
|
-
continue;
|
|
34
|
-
const sorted = chainEntries.sort((a, b) => new Date(a.ts).getTime() - new Date(b.ts).getTime());
|
|
35
|
-
const first = sorted[0];
|
|
36
|
-
const last = sorted[sorted.length - 1];
|
|
37
|
-
const entities = extractEntitiesFromChain(sorted);
|
|
38
|
-
const tools = [...new Set(sorted.map((e) => e.tool))];
|
|
39
|
-
const featureArea = first.feature_area ?? inferFeatureArea(sorted);
|
|
40
|
-
const outcome = classifyChainOutcome(sorted);
|
|
41
|
-
chains.push({
|
|
42
|
-
root_id: rootId,
|
|
43
|
-
session_id: first.session_id,
|
|
44
|
-
branch: first.branch,
|
|
45
|
-
started_at: first.ts,
|
|
46
|
-
ended_at: last.ts,
|
|
47
|
-
duration_ms: new Date(last.ts).getTime() - new Date(first.ts).getTime(),
|
|
48
|
-
entries: sorted,
|
|
49
|
-
entities_touched: entities,
|
|
50
|
-
tools_used: tools,
|
|
51
|
-
feature_area: featureArea,
|
|
52
|
-
outcome,
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
return chains.sort((a, b) => new Date(b.started_at).getTime() - new Date(a.started_at).getTime());
|
|
56
|
-
}
|
|
57
|
-
// ── Entity History ───────────────────────────────────────────────────
|
|
58
|
-
/**
|
|
59
|
-
* Get chains that touched a specific entity (file path or entity key).
|
|
60
|
-
*/
|
|
61
|
-
export function getEntityHistory(entries, entityKey, limit = 20) {
|
|
62
|
-
const chains = extractChains(entries);
|
|
63
|
-
return chains
|
|
64
|
-
.filter((chain) => chain.entities_touched.some((e) => e === entityKey || e.startsWith(entityKey)))
|
|
65
|
-
.slice(0, limit);
|
|
66
|
-
}
|
|
67
|
-
// ── Revert Pattern Detection ─────────────────────────────────────────
|
|
68
|
-
/**
|
|
69
|
-
* Detect tool sequences that frequently lead to reverts.
|
|
70
|
-
* A revert is detected when a chain contains "unerr_revert_to_working_state"
|
|
71
|
-
* or when the same entity is modified multiple times in a session (thrashing).
|
|
72
|
-
*/
|
|
73
|
-
export function getRevertPatterns(entries, minFrequency = 2) {
|
|
74
|
-
const chains = extractChains(entries);
|
|
75
|
-
const sequenceMap = new Map();
|
|
76
|
-
for (const chain of chains) {
|
|
77
|
-
const seqKey = chain.tools_used.slice(0, 4).join(" → ");
|
|
78
|
-
const existing = sequenceMap.get(seqKey);
|
|
79
|
-
const isRevert = chain.outcome === "reverted";
|
|
80
|
-
if (existing) {
|
|
81
|
-
existing.count++;
|
|
82
|
-
if (isRevert)
|
|
83
|
-
existing.revertCount++;
|
|
84
|
-
existing.lengths.push(chain.entries.length);
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
sequenceMap.set(seqKey, {
|
|
88
|
-
count: 1,
|
|
89
|
-
revertCount: isRevert ? 1 : 0,
|
|
90
|
-
lengths: [chain.entries.length],
|
|
91
|
-
exampleId: chain.root_id,
|
|
92
|
-
tools: chain.tools_used.slice(0, 4),
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
const patterns = [];
|
|
97
|
-
for (const [pattern, data] of sequenceMap) {
|
|
98
|
-
if (data.count < minFrequency)
|
|
99
|
-
continue;
|
|
100
|
-
if (data.revertCount === 0)
|
|
101
|
-
continue;
|
|
102
|
-
const avgLength = data.lengths.reduce((a, b) => a + b, 0) / data.lengths.length;
|
|
103
|
-
patterns.push({
|
|
104
|
-
pattern,
|
|
105
|
-
tool_sequence: data.tools,
|
|
106
|
-
frequency: data.count,
|
|
107
|
-
revert_rate: data.revertCount / data.count,
|
|
108
|
-
avg_chain_length: Math.round(avgLength * 10) / 10,
|
|
109
|
-
example_chain_id: data.exampleId,
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
return patterns.sort((a, b) => b.revert_rate - a.revert_rate);
|
|
113
|
-
}
|
|
114
|
-
// ── Session Timeline ─────────────────────────────────────────────────
|
|
115
|
-
/**
|
|
116
|
-
* Get session summaries from ledger entries, most recent first.
|
|
117
|
-
*/
|
|
118
|
-
export function getSessionTimeline(entries, count = 5) {
|
|
119
|
-
const sessionMap = new Map();
|
|
120
|
-
for (const entry of entries) {
|
|
121
|
-
const group = sessionMap.get(entry.session_id);
|
|
122
|
-
if (group) {
|
|
123
|
-
group.push(entry);
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
sessionMap.set(entry.session_id, [entry]);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
const summaries = [];
|
|
130
|
-
for (const [sessionId, sessionEntries] of sessionMap) {
|
|
131
|
-
if (sessionEntries.length === 0)
|
|
132
|
-
continue;
|
|
133
|
-
const sorted = sessionEntries.sort((a, b) => new Date(a.ts).getTime() - new Date(b.ts).getTime());
|
|
134
|
-
const first = sorted[0];
|
|
135
|
-
const last = sorted[sorted.length - 1];
|
|
136
|
-
const chains = extractChains(sorted);
|
|
137
|
-
const files = new Set();
|
|
138
|
-
const entities = new Set();
|
|
139
|
-
const toolCounts = {};
|
|
140
|
-
const featureAreas = new Set();
|
|
141
|
-
let factsRecorded = 0;
|
|
142
|
-
let revertCount = 0;
|
|
143
|
-
for (const entry of sorted) {
|
|
144
|
-
toolCounts[entry.tool] = (toolCounts[entry.tool] ?? 0) + 1;
|
|
145
|
-
if (entry.tool === "record_fact")
|
|
146
|
-
factsRecorded++;
|
|
147
|
-
const filePath = extractFilePath(entry);
|
|
148
|
-
if (filePath)
|
|
149
|
-
files.add(filePath);
|
|
150
|
-
const entityKeys = extractEntityKeys(entry);
|
|
151
|
-
for (const key of entityKeys)
|
|
152
|
-
entities.add(key);
|
|
153
|
-
if (entry.feature_area)
|
|
154
|
-
featureAreas.add(entry.feature_area);
|
|
155
|
-
}
|
|
156
|
-
for (const chain of chains) {
|
|
157
|
-
if (chain.outcome === "reverted")
|
|
158
|
-
revertCount++;
|
|
159
|
-
}
|
|
160
|
-
summaries.push({
|
|
161
|
-
session_id: sessionId,
|
|
162
|
-
started_at: first.ts,
|
|
163
|
-
ended_at: last.ts,
|
|
164
|
-
duration_ms: new Date(last.ts).getTime() - new Date(first.ts).getTime(),
|
|
165
|
-
tool_calls: sorted.length,
|
|
166
|
-
chains: chains.length,
|
|
167
|
-
files_modified: [...files],
|
|
168
|
-
entities_touched: [...entities],
|
|
169
|
-
tools_used: toolCounts,
|
|
170
|
-
feature_areas: [...featureAreas],
|
|
171
|
-
facts_recorded: factsRecorded,
|
|
172
|
-
revert_count: revertCount,
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
return summaries
|
|
176
|
-
.sort((a, b) => new Date(b.started_at).getTime() - new Date(a.started_at).getTime())
|
|
177
|
-
.slice(0, count);
|
|
178
|
-
}
|
|
179
|
-
// ── Internal Helpers ─────────────────────────────────────────────────
|
|
180
|
-
function extractEntitiesFromChain(entries) {
|
|
181
|
-
const entities = new Set();
|
|
182
|
-
for (const entry of entries) {
|
|
183
|
-
const keys = extractEntityKeys(entry);
|
|
184
|
-
for (const key of keys)
|
|
185
|
-
entities.add(key);
|
|
186
|
-
const filePath = extractFilePath(entry);
|
|
187
|
-
if (filePath)
|
|
188
|
-
entities.add(filePath);
|
|
189
|
-
}
|
|
190
|
-
return [...entities];
|
|
191
|
-
}
|
|
192
|
-
function extractEntityKeys(entry) {
|
|
193
|
-
const keys = [];
|
|
194
|
-
const args = entry.args_summary;
|
|
195
|
-
if (typeof args.key === "string" && args.key.length > 0) {
|
|
196
|
-
keys.push(args.key);
|
|
197
|
-
}
|
|
198
|
-
if (typeof args.entity === "string" && args.entity.length > 0) {
|
|
199
|
-
keys.push(args.entity);
|
|
200
|
-
}
|
|
201
|
-
if (typeof args.subject === "string" && args.subject.length > 0) {
|
|
202
|
-
keys.push(args.subject);
|
|
203
|
-
}
|
|
204
|
-
return keys;
|
|
205
|
-
}
|
|
206
|
-
function extractFilePath(entry) {
|
|
207
|
-
const args = entry.args_summary;
|
|
208
|
-
if (typeof args.file_path === "string")
|
|
209
|
-
return args.file_path;
|
|
210
|
-
if (typeof args.path === "string")
|
|
211
|
-
return args.path;
|
|
212
|
-
if (typeof args.key === "string" && args.key.includes("/")) {
|
|
213
|
-
return args.key.includes("::") ? args.key.split("::")[0] : args.key;
|
|
214
|
-
}
|
|
215
|
-
return null;
|
|
216
|
-
}
|
|
217
|
-
function inferFeatureArea(entries) {
|
|
218
|
-
for (const entry of entries) {
|
|
219
|
-
if (entry.feature_area)
|
|
220
|
-
return entry.feature_area;
|
|
221
|
-
}
|
|
222
|
-
const paths = entries
|
|
223
|
-
.map(extractFilePath)
|
|
224
|
-
.filter((p) => p !== null);
|
|
225
|
-
if (paths.length === 0)
|
|
226
|
-
return null;
|
|
227
|
-
const segments = paths[0]?.split("/");
|
|
228
|
-
if (segments && segments.length >= 2) {
|
|
229
|
-
return segments.slice(0, 2).join("/");
|
|
230
|
-
}
|
|
231
|
-
return null;
|
|
232
|
-
}
|
|
233
|
-
function classifyChainOutcome(entries) {
|
|
234
|
-
const hasRevert = entries.some((e) => e.tool === "unerr_revert_to_working_state" ||
|
|
235
|
-
e.result_summary?.reverted === true);
|
|
236
|
-
if (hasRevert)
|
|
237
|
-
return "reverted";
|
|
238
|
-
const hasModify = entries.some((e) => e.tool === "sync_local_diff" || e.result_summary?.modified === true);
|
|
239
|
-
if (hasModify)
|
|
240
|
-
return "modified";
|
|
241
|
-
const hasCommit = entries.some((e) => e.commit_sha != null && e.commit_sha.length > 0);
|
|
242
|
-
if (hasCommit)
|
|
243
|
-
return "survived";
|
|
244
|
-
return "unknown";
|
|
245
|
-
}
|
|
@@ -1,361 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Metrics Store — `.unerr/metrics.db` (better-sqlite3, WAL).
|
|
3
|
-
*
|
|
4
|
-
* Single source of truth for time-series metric streams that were previously
|
|
5
|
-
* append-only JSONL files:
|
|
6
|
-
* - compression_events (was logs/compression.jsonl)
|
|
7
|
-
* - file_read_events (was logs/file-reads.jsonl)
|
|
8
|
-
* - token_flow_events (was logs/token-flow.jsonl)
|
|
9
|
-
* - session_history (was state/session-history.jsonl)
|
|
10
|
-
* - session_summaries (was sessions/{session_id}.jsonl)
|
|
11
|
-
*
|
|
12
|
-
* Why SQLite + WAL:
|
|
13
|
-
* - autoincrement `id` gives a global monotonic counter usable by the
|
|
14
|
-
* log-tailer (poll `SELECT ... WHERE id > $lastSeen`)
|
|
15
|
-
* - cross-process writes serialize through WAL — no flock dance
|
|
16
|
-
* - dashboard aggregations (`SUM/GROUP BY`) become microseconds instead
|
|
17
|
-
* of "re-parse a 2 MB JSONL on every poll"
|
|
18
|
-
*
|
|
19
|
-
* Singleton per cwd: openMetricsStore() caches the handle so writers and
|
|
20
|
-
* readers share one prepared-statement set per process.
|
|
21
|
-
*/
|
|
22
|
-
import { mkdirSync } from "node:fs";
|
|
23
|
-
import { join } from "node:path";
|
|
24
|
-
import Database from "better-sqlite3";
|
|
25
|
-
// ── Store ─────────────────────────────────────────────────────────────
|
|
26
|
-
const SCHEMA = `
|
|
27
|
-
CREATE TABLE IF NOT EXISTS compression_events (
|
|
28
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
29
|
-
ts INTEGER NOT NULL,
|
|
30
|
-
ts_iso TEXT NOT NULL,
|
|
31
|
-
command TEXT NOT NULL,
|
|
32
|
-
category TEXT NOT NULL,
|
|
33
|
-
confidence REAL NOT NULL,
|
|
34
|
-
raw_bytes INTEGER NOT NULL,
|
|
35
|
-
compressed_bytes INTEGER NOT NULL,
|
|
36
|
-
saved_pct REAL NOT NULL,
|
|
37
|
-
omni_fallback INTEGER NOT NULL,
|
|
38
|
-
tee_file TEXT
|
|
39
|
-
);
|
|
40
|
-
CREATE INDEX IF NOT EXISTS idx_compression_ts ON compression_events(ts);
|
|
41
|
-
CREATE INDEX IF NOT EXISTS idx_compression_category ON compression_events(category);
|
|
42
|
-
|
|
43
|
-
CREATE TABLE IF NOT EXISTS file_read_events (
|
|
44
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
45
|
-
ts INTEGER NOT NULL,
|
|
46
|
-
ts_iso TEXT NOT NULL,
|
|
47
|
-
file TEXT NOT NULL,
|
|
48
|
-
mode TEXT NOT NULL,
|
|
49
|
-
total_lines INTEGER NOT NULL,
|
|
50
|
-
returned_lines INTEGER NOT NULL,
|
|
51
|
-
saved_pct REAL NOT NULL,
|
|
52
|
-
entity TEXT,
|
|
53
|
-
token_estimate INTEGER
|
|
54
|
-
);
|
|
55
|
-
CREATE INDEX IF NOT EXISTS idx_file_read_ts ON file_read_events(ts);
|
|
56
|
-
|
|
57
|
-
CREATE TABLE IF NOT EXISTS token_flow_events (
|
|
58
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
59
|
-
ts INTEGER NOT NULL,
|
|
60
|
-
ts_iso TEXT NOT NULL,
|
|
61
|
-
session_id TEXT NOT NULL,
|
|
62
|
-
pid INTEGER NOT NULL,
|
|
63
|
-
turn INTEGER NOT NULL,
|
|
64
|
-
mechanism TEXT NOT NULL,
|
|
65
|
-
tool TEXT,
|
|
66
|
-
tokens_without INTEGER NOT NULL,
|
|
67
|
-
tokens_with INTEGER NOT NULL,
|
|
68
|
-
tokens_saved INTEGER NOT NULL,
|
|
69
|
-
detail TEXT
|
|
70
|
-
);
|
|
71
|
-
CREATE INDEX IF NOT EXISTS idx_token_flow_ts ON token_flow_events(ts);
|
|
72
|
-
CREATE INDEX IF NOT EXISTS idx_token_flow_session ON token_flow_events(session_id);
|
|
73
|
-
CREATE INDEX IF NOT EXISTS idx_token_flow_mechanism ON token_flow_events(mechanism);
|
|
74
|
-
|
|
75
|
-
CREATE TABLE IF NOT EXISTS session_history (
|
|
76
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
77
|
-
session_id TEXT NOT NULL UNIQUE,
|
|
78
|
-
started_at TEXT NOT NULL,
|
|
79
|
-
ended_at TEXT NOT NULL,
|
|
80
|
-
duration_ms INTEGER NOT NULL,
|
|
81
|
-
tool_calls INTEGER NOT NULL,
|
|
82
|
-
tokens_saved INTEGER NOT NULL,
|
|
83
|
-
tokens_processed INTEGER NOT NULL,
|
|
84
|
-
efficiency REAL NOT NULL,
|
|
85
|
-
dollars_saved REAL NOT NULL,
|
|
86
|
-
model_id TEXT NOT NULL,
|
|
87
|
-
entity_count INTEGER NOT NULL,
|
|
88
|
-
agent_name TEXT,
|
|
89
|
-
token_flow_summary TEXT
|
|
90
|
-
);
|
|
91
|
-
CREATE INDEX IF NOT EXISTS idx_session_history_ended ON session_history(ended_at);
|
|
92
|
-
|
|
93
|
-
CREATE TABLE IF NOT EXISTS session_summaries (
|
|
94
|
-
session_id TEXT PRIMARY KEY,
|
|
95
|
-
written_at TEXT NOT NULL,
|
|
96
|
-
started_at TEXT NOT NULL,
|
|
97
|
-
ended_at TEXT NOT NULL,
|
|
98
|
-
duration_ms INTEGER NOT NULL,
|
|
99
|
-
tool_calls INTEGER NOT NULL,
|
|
100
|
-
chains INTEGER NOT NULL,
|
|
101
|
-
files_modified TEXT NOT NULL,
|
|
102
|
-
entities_touched TEXT NOT NULL,
|
|
103
|
-
tools_used TEXT NOT NULL,
|
|
104
|
-
feature_areas TEXT NOT NULL,
|
|
105
|
-
facts_recorded INTEGER NOT NULL,
|
|
106
|
-
facts_surfaced TEXT NOT NULL,
|
|
107
|
-
revert_count INTEGER NOT NULL,
|
|
108
|
-
rot_score REAL NOT NULL,
|
|
109
|
-
token_estimate INTEGER NOT NULL,
|
|
110
|
-
branch TEXT NOT NULL
|
|
111
|
-
);
|
|
112
|
-
CREATE INDEX IF NOT EXISTS idx_session_summaries_ended ON session_summaries(ended_at);
|
|
113
|
-
|
|
114
|
-
CREATE TABLE IF NOT EXISTS meta (
|
|
115
|
-
key TEXT PRIMARY KEY,
|
|
116
|
-
value TEXT NOT NULL
|
|
117
|
-
);
|
|
118
|
-
`;
|
|
119
|
-
const SCHEMA_VERSION = "1";
|
|
120
|
-
export class MetricsStore {
|
|
121
|
-
db;
|
|
122
|
-
stmt;
|
|
123
|
-
constructor(dbPath) {
|
|
124
|
-
this.db = new Database(dbPath);
|
|
125
|
-
this.db.pragma("journal_mode = WAL");
|
|
126
|
-
this.db.pragma("synchronous = NORMAL");
|
|
127
|
-
this.db.pragma("foreign_keys = ON");
|
|
128
|
-
this.db.exec(SCHEMA);
|
|
129
|
-
this.db
|
|
130
|
-
.prepare("INSERT OR IGNORE INTO meta(key, value) VALUES (?, ?)")
|
|
131
|
-
.run("schema_version", SCHEMA_VERSION);
|
|
132
|
-
this.stmt = {
|
|
133
|
-
insertCompression: this.db.prepare(`
|
|
134
|
-
INSERT INTO compression_events
|
|
135
|
-
(ts, ts_iso, command, category, confidence, raw_bytes, compressed_bytes,
|
|
136
|
-
saved_pct, omni_fallback, tee_file)
|
|
137
|
-
VALUES (@ts, @ts_iso, @command, @category, @confidence, @raw_bytes,
|
|
138
|
-
@compressed_bytes, @saved_pct, @omni_fallback, @tee_file)
|
|
139
|
-
`),
|
|
140
|
-
insertFileRead: this.db.prepare(`
|
|
141
|
-
INSERT INTO file_read_events
|
|
142
|
-
(ts, ts_iso, file, mode, total_lines, returned_lines, saved_pct,
|
|
143
|
-
entity, token_estimate)
|
|
144
|
-
VALUES (@ts, @ts_iso, @file, @mode, @total_lines, @returned_lines,
|
|
145
|
-
@saved_pct, @entity, @token_estimate)
|
|
146
|
-
`),
|
|
147
|
-
insertTokenFlow: this.db.prepare(`
|
|
148
|
-
INSERT INTO token_flow_events
|
|
149
|
-
(ts, ts_iso, session_id, pid, turn, mechanism, tool,
|
|
150
|
-
tokens_without, tokens_with, tokens_saved, detail)
|
|
151
|
-
VALUES (@ts, @ts_iso, @session_id, @pid, @turn, @mechanism, @tool,
|
|
152
|
-
@tokens_without, @tokens_with, @tokens_saved, @detail)
|
|
153
|
-
`),
|
|
154
|
-
upsertSessionHistory: this.db.prepare(`
|
|
155
|
-
INSERT INTO session_history
|
|
156
|
-
(session_id, started_at, ended_at, duration_ms, tool_calls, tokens_saved,
|
|
157
|
-
tokens_processed, efficiency, dollars_saved, model_id, entity_count,
|
|
158
|
-
agent_name, token_flow_summary)
|
|
159
|
-
VALUES (@session_id, @started_at, @ended_at, @duration_ms, @tool_calls,
|
|
160
|
-
@tokens_saved, @tokens_processed, @efficiency, @dollars_saved,
|
|
161
|
-
@model_id, @entity_count, @agent_name, @token_flow_summary)
|
|
162
|
-
ON CONFLICT(session_id) DO UPDATE SET
|
|
163
|
-
ended_at = excluded.ended_at,
|
|
164
|
-
duration_ms = excluded.duration_ms,
|
|
165
|
-
tool_calls = excluded.tool_calls,
|
|
166
|
-
tokens_saved = excluded.tokens_saved,
|
|
167
|
-
tokens_processed = excluded.tokens_processed,
|
|
168
|
-
efficiency = excluded.efficiency,
|
|
169
|
-
dollars_saved = excluded.dollars_saved,
|
|
170
|
-
entity_count = excluded.entity_count,
|
|
171
|
-
token_flow_summary = excluded.token_flow_summary
|
|
172
|
-
`),
|
|
173
|
-
upsertSessionSummary: this.db.prepare(`
|
|
174
|
-
INSERT INTO session_summaries
|
|
175
|
-
(session_id, written_at, started_at, ended_at, duration_ms, tool_calls,
|
|
176
|
-
chains, files_modified, entities_touched, tools_used, feature_areas,
|
|
177
|
-
facts_recorded, facts_surfaced, revert_count, rot_score, token_estimate,
|
|
178
|
-
branch)
|
|
179
|
-
VALUES (@session_id, @written_at, @started_at, @ended_at, @duration_ms,
|
|
180
|
-
@tool_calls, @chains, @files_modified, @entities_touched,
|
|
181
|
-
@tools_used, @feature_areas, @facts_recorded, @facts_surfaced,
|
|
182
|
-
@revert_count, @rot_score, @token_estimate, @branch)
|
|
183
|
-
ON CONFLICT(session_id) DO UPDATE SET
|
|
184
|
-
written_at = excluded.written_at,
|
|
185
|
-
ended_at = excluded.ended_at,
|
|
186
|
-
duration_ms = excluded.duration_ms,
|
|
187
|
-
tool_calls = excluded.tool_calls,
|
|
188
|
-
chains = excluded.chains,
|
|
189
|
-
files_modified = excluded.files_modified,
|
|
190
|
-
entities_touched = excluded.entities_touched,
|
|
191
|
-
tools_used = excluded.tools_used,
|
|
192
|
-
feature_areas = excluded.feature_areas,
|
|
193
|
-
facts_recorded = excluded.facts_recorded,
|
|
194
|
-
facts_surfaced = excluded.facts_surfaced,
|
|
195
|
-
revert_count = excluded.revert_count,
|
|
196
|
-
rot_score = excluded.rot_score,
|
|
197
|
-
token_estimate = excluded.token_estimate,
|
|
198
|
-
branch = excluded.branch
|
|
199
|
-
`),
|
|
200
|
-
recentCompression: this.db.prepare(`
|
|
201
|
-
SELECT * FROM compression_events ORDER BY id DESC LIMIT @limit
|
|
202
|
-
`),
|
|
203
|
-
recentFileReads: this.db.prepare(`
|
|
204
|
-
SELECT * FROM file_read_events ORDER BY id DESC LIMIT @limit
|
|
205
|
-
`),
|
|
206
|
-
compressionSince: this.db.prepare(`
|
|
207
|
-
SELECT * FROM compression_events WHERE id > @lastId ORDER BY id ASC LIMIT @limit
|
|
208
|
-
`),
|
|
209
|
-
fileReadsSince: this.db.prepare(`
|
|
210
|
-
SELECT * FROM file_read_events WHERE id > @lastId ORDER BY id ASC LIMIT @limit
|
|
211
|
-
`),
|
|
212
|
-
tokenFlowSince: this.db.prepare(`
|
|
213
|
-
SELECT * FROM token_flow_events WHERE id > @lastId ORDER BY id ASC LIMIT @limit
|
|
214
|
-
`),
|
|
215
|
-
tokenFlowAll: this.db.prepare(`
|
|
216
|
-
SELECT * FROM token_flow_events ORDER BY id ASC
|
|
217
|
-
`),
|
|
218
|
-
tokenFlowBySession: this.db.prepare(`
|
|
219
|
-
SELECT * FROM token_flow_events WHERE session_id = @sessionId ORDER BY id ASC
|
|
220
|
-
`),
|
|
221
|
-
allSessionHistory: this.db.prepare(`
|
|
222
|
-
SELECT * FROM session_history ORDER BY ended_at ASC
|
|
223
|
-
`),
|
|
224
|
-
sessionSummaryById: this.db.prepare(`
|
|
225
|
-
SELECT * FROM session_summaries WHERE session_id = @sessionId
|
|
226
|
-
`),
|
|
227
|
-
allSessionSummaries: this.db.prepare(`
|
|
228
|
-
SELECT * FROM session_summaries ORDER BY ended_at DESC
|
|
229
|
-
`),
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
// ── Writes ──────────────────────────────────────────────────────────
|
|
233
|
-
insertCompression(row) {
|
|
234
|
-
return Number(this.stmt.insertCompression.run(row).lastInsertRowid);
|
|
235
|
-
}
|
|
236
|
-
insertFileRead(row) {
|
|
237
|
-
return Number(this.stmt.insertFileRead.run(row).lastInsertRowid);
|
|
238
|
-
}
|
|
239
|
-
insertTokenFlow(row) {
|
|
240
|
-
return Number(this.stmt.insertTokenFlow.run(row).lastInsertRowid);
|
|
241
|
-
}
|
|
242
|
-
upsertSessionHistory(row) {
|
|
243
|
-
this.stmt.upsertSessionHistory.run(row);
|
|
244
|
-
}
|
|
245
|
-
upsertSessionSummary(row) {
|
|
246
|
-
this.stmt.upsertSessionSummary.run(row);
|
|
247
|
-
}
|
|
248
|
-
// ── Reads ───────────────────────────────────────────────────────────
|
|
249
|
-
recentCompression(limit) {
|
|
250
|
-
return this.stmt.recentCompression.all({ limit });
|
|
251
|
-
}
|
|
252
|
-
recentFileReads(limit) {
|
|
253
|
-
return this.stmt.recentFileReads.all({ limit });
|
|
254
|
-
}
|
|
255
|
-
/** Poll API used by the log-tailer. */
|
|
256
|
-
compressionSince(lastId, limit = 500) {
|
|
257
|
-
return this.stmt.compressionSince.all({
|
|
258
|
-
lastId,
|
|
259
|
-
limit,
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
fileReadsSince(lastId, limit = 500) {
|
|
263
|
-
return this.stmt.fileReadsSince.all({
|
|
264
|
-
lastId,
|
|
265
|
-
limit,
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
tokenFlowSince(lastId, limit = 500) {
|
|
269
|
-
return this.stmt.tokenFlowSince.all({
|
|
270
|
-
lastId,
|
|
271
|
-
limit,
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
allTokenFlow() {
|
|
275
|
-
return this.stmt.tokenFlowAll.all({});
|
|
276
|
-
}
|
|
277
|
-
tokenFlowBySession(sessionId) {
|
|
278
|
-
return this.stmt.tokenFlowBySession.all({
|
|
279
|
-
sessionId,
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
allSessionHistory() {
|
|
283
|
-
return this.stmt.allSessionHistory.all({});
|
|
284
|
-
}
|
|
285
|
-
sessionSummary(sessionId) {
|
|
286
|
-
return (this.stmt.sessionSummaryById.get({ sessionId }) ??
|
|
287
|
-
null);
|
|
288
|
-
}
|
|
289
|
-
allSessionSummaries() {
|
|
290
|
-
return this.stmt.allSessionSummaries.all({});
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* Current max id in each of the three polled tables — used by the
|
|
294
|
-
* log-tailer to skip events that already existed when it started.
|
|
295
|
-
* `COALESCE(MAX(id), 0)` keeps the call O(1) on an indexed PK.
|
|
296
|
-
*/
|
|
297
|
-
lastIds() {
|
|
298
|
-
const c = this.db
|
|
299
|
-
.prepare("SELECT COALESCE(MAX(id), 0) AS id FROM compression_events")
|
|
300
|
-
.get();
|
|
301
|
-
const f = this.db
|
|
302
|
-
.prepare("SELECT COALESCE(MAX(id), 0) AS id FROM file_read_events")
|
|
303
|
-
.get();
|
|
304
|
-
const t = this.db
|
|
305
|
-
.prepare("SELECT COALESCE(MAX(id), 0) AS id FROM token_flow_events")
|
|
306
|
-
.get();
|
|
307
|
-
return { compression: c.id, fileRead: f.id, tokenFlow: t.id };
|
|
308
|
-
}
|
|
309
|
-
// ── Lifecycle ───────────────────────────────────────────────────────
|
|
310
|
-
close() {
|
|
311
|
-
this.db.close();
|
|
312
|
-
}
|
|
313
|
-
/** Test-only — wipe every metric table. */
|
|
314
|
-
reset() {
|
|
315
|
-
this.db.exec(`
|
|
316
|
-
DELETE FROM compression_events;
|
|
317
|
-
DELETE FROM file_read_events;
|
|
318
|
-
DELETE FROM token_flow_events;
|
|
319
|
-
DELETE FROM session_history;
|
|
320
|
-
DELETE FROM session_summaries;
|
|
321
|
-
DELETE FROM sqlite_sequence WHERE name IN
|
|
322
|
-
('compression_events','file_read_events','token_flow_events','session_history');
|
|
323
|
-
`);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
// ── Per-cwd singleton ────────────────────────────────────────────────
|
|
327
|
-
const instances = new Map();
|
|
328
|
-
/**
|
|
329
|
-
* Open (or reuse) the metrics store for a given unerr directory.
|
|
330
|
-
* `.unerr/metrics.db` is created on first call; the schema bootstrap is
|
|
331
|
-
* idempotent (CREATE TABLE IF NOT EXISTS).
|
|
332
|
-
*/
|
|
333
|
-
export function openMetricsStore(unerrDir) {
|
|
334
|
-
let store = instances.get(unerrDir);
|
|
335
|
-
if (!store) {
|
|
336
|
-
mkdirSync(unerrDir, { recursive: true });
|
|
337
|
-
store = new MetricsStore(join(unerrDir, "metrics.db"));
|
|
338
|
-
instances.set(unerrDir, store);
|
|
339
|
-
}
|
|
340
|
-
return store;
|
|
341
|
-
}
|
|
342
|
-
/** Test-only — close + drop the cached instance for an `unerrDir`. */
|
|
343
|
-
export function closeMetricsStore(unerrDir) {
|
|
344
|
-
const store = instances.get(unerrDir);
|
|
345
|
-
if (store) {
|
|
346
|
-
store.close();
|
|
347
|
-
instances.delete(unerrDir);
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
/** Close every cached instance — called on process shutdown. */
|
|
351
|
-
export function closeAllMetricsStores() {
|
|
352
|
-
for (const [dir, store] of instances) {
|
|
353
|
-
try {
|
|
354
|
-
store.close();
|
|
355
|
-
}
|
|
356
|
-
catch {
|
|
357
|
-
/* best effort */
|
|
358
|
-
}
|
|
359
|
-
instances.delete(dir);
|
|
360
|
-
}
|
|
361
|
-
}
|