@unerr-ai/unerr 0.1.6 → 0.1.7
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 +70 -194
- package/dist/cli.js +39149 -36991
- package/package.json +9 -2
- package/dist/__tests__/architecture-guard.test.js +0 -122
- package/dist/__tests__/arg-validator.test.js +0 -205
- package/dist/__tests__/ast-extractor.test.js +0 -203
- package/dist/__tests__/auto-bootstrap.test.js +0 -280
- package/dist/__tests__/background-indexer.test.js +0 -228
- package/dist/__tests__/blast-radius-engine.test.js +0 -200
- package/dist/__tests__/bridge-isolation.test.js +0 -37
- package/dist/__tests__/budget-enforcer.test.js +0 -53
- package/dist/__tests__/cfg-test-detection-perf.test.js +0 -82
- package/dist/__tests__/change-narrative.test.js +0 -190
- package/dist/__tests__/check-commit.test.js +0 -258
- package/dist/__tests__/checksum.test.js +0 -34
- package/dist/__tests__/commit-watcher.test.js +0 -154
- package/dist/__tests__/community-detection.test.js +0 -179
- package/dist/__tests__/community-tools.test.js +0 -299
- package/dist/__tests__/components.test.js +0 -449
- package/dist/__tests__/compression-log.test.js +0 -174
- package/dist/__tests__/compression-quality-monitor.test.js +0 -40
- package/dist/__tests__/config-healer.test.js +0 -165
- package/dist/__tests__/context-ledger.test.js +0 -58
- package/dist/__tests__/convention-detector.test.js +0 -99
- package/dist/__tests__/convention-learner.test.js +0 -86
- package/dist/__tests__/correction-detector.test.js +0 -330
- package/dist/__tests__/daemon-autostart-install.test.js +0 -283
- package/dist/__tests__/daemon-bridge.test.js +0 -222
- package/dist/__tests__/daemon-dashboard.test.js +0 -202
- package/dist/__tests__/daemon-registry.test.js +0 -240
- package/dist/__tests__/daemon-supervisor.test.js +0 -318
- package/dist/__tests__/daemon-version-check.test.js +0 -275
- package/dist/__tests__/decision-point-detector.test.js +0 -98
- package/dist/__tests__/deep-link.test.js +0 -143
- package/dist/__tests__/disallowed-tools.test.js +0 -115
- package/dist/__tests__/drift-tracker.test.js +0 -582
- package/dist/__tests__/durability-scorer.test.js +0 -152
- package/dist/__tests__/efficiency-tracker.test.js +0 -65
- package/dist/__tests__/enrich.test.js +0 -144
- package/dist/__tests__/entity-rewind.test.js +0 -248
- package/dist/__tests__/ephemeral.test.js +0 -111
- package/dist/__tests__/exploration-cost.test.js +0 -93
- package/dist/__tests__/fact-generator.test.js +0 -197
- package/dist/__tests__/file-l0-graph.test.js +0 -244
- package/dist/__tests__/file-logger.test.js +0 -82
- package/dist/__tests__/file-outline.test.js +0 -141
- package/dist/__tests__/file-read-protocol.test.js +0 -188
- package/dist/__tests__/format-encoder.test.js +0 -233
- package/dist/__tests__/git-attribution.test.js +0 -259
- package/dist/__tests__/graph-temporal-joiner.test.js +0 -219
- package/dist/__tests__/health-grade-enhanced.test.js +0 -138
- package/dist/__tests__/health-map-data.test.js +0 -173
- package/dist/__tests__/helpers/mcp-harness.js +0 -45
- package/dist/__tests__/helpers/mcp-harness.test.js +0 -68
- package/dist/__tests__/hook-dedup.test.js +0 -112
- package/dist/__tests__/hook-runner.test.js +0 -253
- package/dist/__tests__/indexer-cfg.test.js +0 -185
- package/dist/__tests__/indexer-cross-file.test.js +0 -172
- package/dist/__tests__/indexer-extraction.test.js +0 -245
- package/dist/__tests__/indexer-incremental.test.js +0 -232
- package/dist/__tests__/indexer-language-expansion.test.js +0 -165
- package/dist/__tests__/init-push.test.js +0 -131
- package/dist/__tests__/instruction-writer.test.js +0 -179
- package/dist/__tests__/intelligence-integration.test.js +0 -217
- package/dist/__tests__/intent-correlator.test.js +0 -175
- package/dist/__tests__/intent-detector.test.js +0 -235
- package/dist/__tests__/intent-encoder.test.js +0 -167
- package/dist/__tests__/java-build-tool-detection.test.js +0 -174
- package/dist/__tests__/layer3-sprint-q.test.js +0 -160
- package/dist/__tests__/layer3-sprint-r.test.js +0 -91
- package/dist/__tests__/layer3-sprint-s.test.js +0 -183
- package/dist/__tests__/layer3-sprint-t.test.js +0 -201
- package/dist/__tests__/layer3-sprint-u.test.js +0 -174
- package/dist/__tests__/layer4-sprint-ba2.test.js +0 -354
- package/dist/__tests__/layer4-sprint-ba4.test.js +0 -84
- package/dist/__tests__/layer4-sprint-vs.test.js +0 -105
- package/dist/__tests__/ledger-chains.test.js +0 -162
- package/dist/__tests__/lifecycle-machine.test.js +0 -226
- package/dist/__tests__/local-chat-provider.test.js +0 -170
- package/dist/__tests__/local-convention-detector.test.js +0 -308
- package/dist/__tests__/local-embeddings.test.js +0 -422
- package/dist/__tests__/local-graph.test.js +0 -540
- package/dist/__tests__/local-indexer.test.js +0 -228
- package/dist/__tests__/local-intelligence-l3.test.js +0 -332
- package/dist/__tests__/local-llm.test.js +0 -253
- package/dist/__tests__/local-mode-offline.test.js +0 -187
- package/dist/__tests__/local-mode-stats.test.js +0 -273
- package/dist/__tests__/local-mode-tui.test.js +0 -343
- package/dist/__tests__/local-parse.test.js +0 -199
- package/dist/__tests__/log-tailer.test.js +0 -208
- package/dist/__tests__/loop-breaker.test.js +0 -276
- package/dist/__tests__/loop-miner.test.js +0 -226
- package/dist/__tests__/mcp-config.test.js +0 -126
- package/dist/__tests__/mcp-content-json.test.js +0 -10
- package/dist/__tests__/mcp-envelope.test.js +0 -124
- package/dist/__tests__/metrics-store.test.js +0 -223
- package/dist/__tests__/native-watcher.test.js +0 -191
- package/dist/__tests__/navigation-hooks-agent-aware.test.js +0 -145
- package/dist/__tests__/negative-knowledge.test.js +0 -116
- package/dist/__tests__/network-boundary.test.js +0 -190
- package/dist/__tests__/network-firewall.test.js +0 -112
- package/dist/__tests__/nudge-invariants.test.js +0 -160
- package/dist/__tests__/nudge-v2.test.js +0 -225
- package/dist/__tests__/offline-rewind.test.js +0 -251
- package/dist/__tests__/open-threads.test.js +0 -89
- package/dist/__tests__/output-compressor.test.js +0 -93
- package/dist/__tests__/pending-violations.test.js +0 -112
- package/dist/__tests__/persistence-effectiveness.test.js +0 -143
- package/dist/__tests__/provider-factory.test.js +0 -42
- package/dist/__tests__/providers.test.js +0 -24
- package/dist/__tests__/proxy.test.js +0 -314
- package/dist/__tests__/query-router.test.js +0 -1018
- package/dist/__tests__/reasoning-quality-route.test.js +0 -138
- package/dist/__tests__/redactor.test.js +0 -120
- package/dist/__tests__/resource-monitor.test.js +0 -57
- package/dist/__tests__/response-envelope.test.js +0 -100
- package/dist/__tests__/risk-classifier.test.js +0 -101
- package/dist/__tests__/risk-signal-scope.test.js +0 -75
- package/dist/__tests__/rule-evaluator.test.js +0 -280
- package/dist/__tests__/scip-decoder.test.js +0 -49
- package/dist/__tests__/scip-downloader.test.js +0 -201
- package/dist/__tests__/scip-merger.test.js +0 -103
- package/dist/__tests__/search-index.test.js +0 -422
- package/dist/__tests__/semantic-enrichment.test.js +0 -360
- package/dist/__tests__/session-brief-builder.test.js +0 -187
- package/dist/__tests__/session-context.test.js +0 -221
- package/dist/__tests__/session-continuity.test.js +0 -144
- package/dist/__tests__/session-dedup.test.js +0 -74
- package/dist/__tests__/session-event-wiring.test.js +0 -206
- package/dist/__tests__/session-events.test.js +0 -149
- package/dist/__tests__/session-legend.test.js +0 -20
- package/dist/__tests__/session-persistence.test.js +0 -131
- package/dist/__tests__/session-resume-block.test.js +0 -107
- package/dist/__tests__/session-resume.test.js +0 -97
- package/dist/__tests__/session-summary-writer.test.js +0 -134
- package/dist/__tests__/shadow-ledger.test.js +0 -203
- package/dist/__tests__/shell-classifier.test.js +0 -151
- package/dist/__tests__/shell-compression-floor.test.js +0 -189
- package/dist/__tests__/shell-compression-v2.test.js +0 -339
- package/dist/__tests__/shell-compressor.test.js +0 -35
- package/dist/__tests__/shell-hooks.test.js +0 -128
- package/dist/__tests__/shell-strategies.test.js +0 -644
- package/dist/__tests__/shell-tee.test.js +0 -133
- package/dist/__tests__/signal-dedup.test.js +0 -158
- package/dist/__tests__/signal-reinforcer.test.js +0 -77
- package/dist/__tests__/signal-scorer.test.js +0 -251
- package/dist/__tests__/signal-show-store.test.js +0 -108
- package/dist/__tests__/smart-truncate.test.js +0 -215
- package/dist/__tests__/snapshot-v2.test.js +0 -113
- package/dist/__tests__/sprint-l1-local-mode.test.js +0 -130
- package/dist/__tests__/sprint-l10-boot.test.js +0 -220
- package/dist/__tests__/sprint-l9-offline-commands.test.js +0 -189
- package/dist/__tests__/sprint-q-persistent-context.test.js +0 -198
- package/dist/__tests__/sprint-s1-wiring.test.js +0 -215
- package/dist/__tests__/sprint-s2-wiring.test.js +0 -256
- package/dist/__tests__/sprint-s3-wiring.test.js +0 -195
- package/dist/__tests__/sprint-s4-wiring.test.js +0 -213
- package/dist/__tests__/sprint-s6-hooks.test.js +0 -222
- package/dist/__tests__/sprint-s7-persistent.test.js +0 -263
- package/dist/__tests__/sprint-s8-value.test.js +0 -167
- package/dist/__tests__/sprint-s9-behavioral.test.js +0 -179
- package/dist/__tests__/sprint3-intelligence.test.js +0 -297
- package/dist/__tests__/sprint5-mcp-server.test.js +0 -136
- package/dist/__tests__/startup-display.test.js +0 -302
- package/dist/__tests__/startup-log-file.test.js +0 -97
- package/dist/__tests__/stash-manager.test.js +0 -229
- package/dist/__tests__/state-detector.test.js +0 -92
- package/dist/__tests__/status-dashboard.test.js +0 -142
- package/dist/__tests__/temporal-facts.test.js +0 -292
- package/dist/__tests__/temporal-routes.test.js +0 -142
- package/dist/__tests__/test-detector.test.js +0 -174
- package/dist/__tests__/theme.test.js +0 -72
- package/dist/__tests__/timeline-agents.test.js +0 -122
- package/dist/__tests__/timeline-bootstrap.test.js +0 -176
- package/dist/__tests__/timeline-filters.test.js +0 -193
- package/dist/__tests__/timeline-markers.test.js +0 -151
- package/dist/__tests__/timeline-routes.test.js +0 -156
- package/dist/__tests__/timeline-store.test.js +0 -171
- package/dist/__tests__/token-counter.test.js +0 -86
- package/dist/__tests__/token-estimator.test.js +0 -96
- package/dist/__tests__/token-flow-api.test.js +0 -239
- package/dist/__tests__/token-flow-instrumentation.test.js +0 -437
- package/dist/__tests__/token-flow-persistence.test.js +0 -356
- package/dist/__tests__/token-flow-routes.test.js +0 -199
- package/dist/__tests__/token-flow.test.js +0 -695
- package/dist/__tests__/tool-clusters.test.js +0 -177
- package/dist/__tests__/transport-mux.test.js +0 -283
- package/dist/__tests__/turn-segmenter.test.js +0 -166
- package/dist/__tests__/uninstall.test.js +0 -141
- package/dist/__tests__/warm-start-policy.test.js +0 -271
- package/dist/__tests__/wire-cap-nudge.test.js +0 -77
- package/dist/__tests__/worker-pool.test.js +0 -101
- package/dist/ui/assets/index-7gl3mIuY.css +0 -1
- package/dist/ui/assets/index-CX4FCWGT.js +0 -10
- package/dist/ui/assets/rolldown-runtime-S-ySWqyJ.js +0 -1
- package/dist/ui/assets/vis-network-NIJHUFI3.js +0 -908
- package/dist/ui/fonts/jetbrains-mono-latin-400-normal.woff +0 -0
- package/dist/ui/icon-wordmark.png +0 -0
- package/dist/ui/icon-wordmark.svg +0 -30
- package/dist/ui/icon.png +0 -0
- package/dist/ui/icon.svg +0 -25
- package/dist/ui/index.html +0 -15
- package/dist/ui/prototype-sandbox/index.html +0 -257
- package/dist/ui/screenshots/activity.png +0 -0
- package/dist/ui/screenshots/code-base-intelligence.png +0 -0
- package/dist/ui/screenshots/dashboard.png +0 -0
- package/dist/ui/screenshots/project-memory.png +0 -0
- package/dist/ui/screenshots/reasoning-quality.png +0 -0
- package/dist/ui/screenshots/reasoning-session.png +0 -0
- package/dist/ui/screenshots/token-session.png +0 -0
- package/dist/ui/screenshots/token-trace-main.png +0 -0
- package/dist/ui/screenshots/token-turn.png +0 -0
- package/dist/ui/unerr-wordmark.png +0 -0
- package/dist/ui/unerr-wordmark.svg +0 -9
- package/dist/ui/unerr.png +0 -0
- package/dist/ui/unerr.svg +0 -25
- package/dist/ui/web-app-manifest-192x192.png +0 -0
- package/dist/ui/web-app-manifest-512x512.png +0 -0
|
@@ -1,356 +0,0 @@
|
|
|
1
|
-
import { mkdirSync, rmSync } from "node:fs";
|
|
2
|
-
import os from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
5
|
-
import { appendSessionHistory, readSessionHistory, } from "../tracking/session-history.js";
|
|
6
|
-
import { formatSessionReceipt, } from "../tracking/session-receipt.js";
|
|
7
|
-
import { TokenFlowWriter, aggregateSession, } from "../tracking/token-flow.js";
|
|
8
|
-
describe("token-flow-persistence", () => {
|
|
9
|
-
let tmpDir;
|
|
10
|
-
let unerrDir;
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
tmpDir = join(os.tmpdir(), `unerr-tfp-test-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`);
|
|
13
|
-
unerrDir = join(tmpDir, ".unerr");
|
|
14
|
-
mkdirSync(unerrDir, { recursive: true });
|
|
15
|
-
});
|
|
16
|
-
afterEach(() => {
|
|
17
|
-
rmSync(tmpDir, { recursive: true, force: true });
|
|
18
|
-
});
|
|
19
|
-
// ── TF-C.1 + TF-C.2: Session history persistence ─────────────────
|
|
20
|
-
describe("session history with tokenFlowSummary", () => {
|
|
21
|
-
it("persists tokenFlowSummary in session history entries", () => {
|
|
22
|
-
const entry = {
|
|
23
|
-
sessionId: "test-sess-001",
|
|
24
|
-
startedAt: "2026-01-01T00:00:00Z",
|
|
25
|
-
endedAt: "2026-01-01T00:23:00Z",
|
|
26
|
-
durationMs: 23 * 60_000,
|
|
27
|
-
toolCalls: 15,
|
|
28
|
-
tokensSaved: 51600,
|
|
29
|
-
tokensProcessed: 80200,
|
|
30
|
-
efficiency: 64,
|
|
31
|
-
dollarsSaved: 1.55,
|
|
32
|
-
modelId: "unknown",
|
|
33
|
-
entityCount: 0,
|
|
34
|
-
tokenFlowSummary: {
|
|
35
|
-
by_mechanism: {
|
|
36
|
-
graph_query: { tokens_saved: 38400, event_count: 8 },
|
|
37
|
-
shell_compression: { tokens_saved: 4200, event_count: 3 },
|
|
38
|
-
format_encoding: { tokens_saved: 1600, event_count: 5 },
|
|
39
|
-
},
|
|
40
|
-
top_mechanism: "graph_query",
|
|
41
|
-
efficiency_pct: 64,
|
|
42
|
-
total_tokens_saved: 51600,
|
|
43
|
-
total_tokens_delivered: 28800,
|
|
44
|
-
},
|
|
45
|
-
};
|
|
46
|
-
appendSessionHistory(unerrDir, entry);
|
|
47
|
-
const entries = readSessionHistory(unerrDir);
|
|
48
|
-
expect(entries).toHaveLength(1);
|
|
49
|
-
expect(entries[0].tokenFlowSummary).toBeDefined();
|
|
50
|
-
expect(entries[0].tokenFlowSummary.top_mechanism).toBe("graph_query");
|
|
51
|
-
expect(entries[0].tokenFlowSummary.by_mechanism.graph_query.tokens_saved).toBe(38400);
|
|
52
|
-
expect(entries[0].tokenFlowSummary.total_tokens_saved).toBe(51600);
|
|
53
|
-
});
|
|
54
|
-
it("reads entries without tokenFlowSummary (backward compatibility)", () => {
|
|
55
|
-
const entry = {
|
|
56
|
-
sessionId: "old-session",
|
|
57
|
-
startedAt: "2026-01-01T00:00:00Z",
|
|
58
|
-
endedAt: "2026-01-01T00:10:00Z",
|
|
59
|
-
durationMs: 600_000,
|
|
60
|
-
toolCalls: 5,
|
|
61
|
-
tokensSaved: 10000,
|
|
62
|
-
tokensProcessed: 20000,
|
|
63
|
-
efficiency: 50,
|
|
64
|
-
dollarsSaved: 0.3,
|
|
65
|
-
modelId: "unknown",
|
|
66
|
-
entityCount: 0,
|
|
67
|
-
};
|
|
68
|
-
appendSessionHistory(unerrDir, entry);
|
|
69
|
-
const entries = readSessionHistory(unerrDir);
|
|
70
|
-
expect(entries).toHaveLength(1);
|
|
71
|
-
expect(entries[0].tokenFlowSummary).toBeUndefined();
|
|
72
|
-
});
|
|
73
|
-
it("aggregates multiple sessions with tokenFlowSummary", () => {
|
|
74
|
-
for (let i = 0; i < 3; i++) {
|
|
75
|
-
appendSessionHistory(unerrDir, {
|
|
76
|
-
sessionId: `session-${i}`,
|
|
77
|
-
startedAt: new Date().toISOString(),
|
|
78
|
-
endedAt: new Date().toISOString(),
|
|
79
|
-
durationMs: 60_000,
|
|
80
|
-
toolCalls: 5,
|
|
81
|
-
tokensSaved: 1000 * (i + 1),
|
|
82
|
-
tokensProcessed: 5000,
|
|
83
|
-
efficiency: 20 * (i + 1),
|
|
84
|
-
dollarsSaved: 0.1 * (i + 1),
|
|
85
|
-
modelId: "unknown",
|
|
86
|
-
entityCount: 0,
|
|
87
|
-
tokenFlowSummary: {
|
|
88
|
-
by_mechanism: {
|
|
89
|
-
graph_query: { tokens_saved: 800 * (i + 1), event_count: 3 },
|
|
90
|
-
shell_compression: {
|
|
91
|
-
tokens_saved: 200 * (i + 1),
|
|
92
|
-
event_count: 1,
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
top_mechanism: "graph_query",
|
|
96
|
-
efficiency_pct: 20 * (i + 1),
|
|
97
|
-
total_tokens_saved: 1000 * (i + 1),
|
|
98
|
-
total_tokens_delivered: 4000,
|
|
99
|
-
},
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
const entries = readSessionHistory(unerrDir);
|
|
103
|
-
expect(entries).toHaveLength(3);
|
|
104
|
-
const totalGraphSaved = entries.reduce((s, e) => s + (e.tokenFlowSummary?.by_mechanism.graph_query?.tokens_saved ?? 0), 0);
|
|
105
|
-
expect(totalGraphSaved).toBe(800 + 1600 + 2400);
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
// ── TF-C.4: Session receipt formatting ────────────────────────────
|
|
109
|
-
describe("session receipt", () => {
|
|
110
|
-
function makeSummary() {
|
|
111
|
-
const writer = new TokenFlowWriter(unerrDir, "receipt-test");
|
|
112
|
-
writer.record({
|
|
113
|
-
session_id: "receipt-test",
|
|
114
|
-
turn: 1,
|
|
115
|
-
mechanism: "graph_query",
|
|
116
|
-
tool: "get_callers",
|
|
117
|
-
tokens_without: 5000,
|
|
118
|
-
tokens_with: 1800,
|
|
119
|
-
tokens_saved: 3200,
|
|
120
|
-
});
|
|
121
|
-
writer.record({
|
|
122
|
-
session_id: "receipt-test",
|
|
123
|
-
turn: 2,
|
|
124
|
-
mechanism: "shell_compression",
|
|
125
|
-
tool: null,
|
|
126
|
-
tokens_without: 2000,
|
|
127
|
-
tokens_with: 800,
|
|
128
|
-
tokens_saved: 1200,
|
|
129
|
-
});
|
|
130
|
-
writer.record({
|
|
131
|
-
session_id: "receipt-test",
|
|
132
|
-
turn: 3,
|
|
133
|
-
mechanism: "format_encoding",
|
|
134
|
-
tool: "get_callers",
|
|
135
|
-
tokens_without: 1800,
|
|
136
|
-
tokens_with: 1480,
|
|
137
|
-
tokens_saved: 320,
|
|
138
|
-
});
|
|
139
|
-
return aggregateSession(writer.getSessionEvents(), "receipt-test");
|
|
140
|
-
}
|
|
141
|
-
it("produces formatted receipt with all sections", () => {
|
|
142
|
-
const summary = makeSummary();
|
|
143
|
-
const receipt = formatSessionReceipt({
|
|
144
|
-
summary,
|
|
145
|
-
durationMs: 23 * 60_000,
|
|
146
|
-
toolCalls: 15,
|
|
147
|
-
weeklyTokensSaved: 312_000,
|
|
148
|
-
weeklySessions: 7,
|
|
149
|
-
});
|
|
150
|
-
expect(receipt).toContain("unerr");
|
|
151
|
-
expect(receipt).toContain("session receipt");
|
|
152
|
-
expect(receipt).toContain("23 minutes");
|
|
153
|
-
expect(receipt).toContain("15");
|
|
154
|
-
expect(receipt).toContain("graph_query");
|
|
155
|
-
expect(receipt).toContain("shell_compression");
|
|
156
|
-
expect(receipt).toContain("312.0K");
|
|
157
|
-
});
|
|
158
|
-
it("receipt includes efficiency percentage", () => {
|
|
159
|
-
const summary = makeSummary();
|
|
160
|
-
const receipt = formatSessionReceipt({
|
|
161
|
-
summary,
|
|
162
|
-
durationMs: 60_000,
|
|
163
|
-
toolCalls: 3,
|
|
164
|
-
});
|
|
165
|
-
expect(receipt).toContain(`${summary.efficiency_pct}%`);
|
|
166
|
-
});
|
|
167
|
-
it("receipt includes most efficient turn", () => {
|
|
168
|
-
const summary = makeSummary();
|
|
169
|
-
const receipt = formatSessionReceipt({
|
|
170
|
-
summary,
|
|
171
|
-
durationMs: 60_000,
|
|
172
|
-
toolCalls: 3,
|
|
173
|
-
});
|
|
174
|
-
expect(receipt).toContain("get_callers");
|
|
175
|
-
});
|
|
176
|
-
it("receipt handles empty session gracefully", () => {
|
|
177
|
-
const emptySummary = {
|
|
178
|
-
session_id: "empty",
|
|
179
|
-
total_turns: 0,
|
|
180
|
-
total_tokens_without: 0,
|
|
181
|
-
total_tokens_with: 0,
|
|
182
|
-
total_tokens_saved: 0,
|
|
183
|
-
efficiency_pct: 0,
|
|
184
|
-
by_mechanism: {},
|
|
185
|
-
top_turns: [],
|
|
186
|
-
};
|
|
187
|
-
const receipt = formatSessionReceipt({
|
|
188
|
-
summary: emptySummary,
|
|
189
|
-
durationMs: 0,
|
|
190
|
-
toolCalls: 0,
|
|
191
|
-
});
|
|
192
|
-
expect(receipt).toContain("0%");
|
|
193
|
-
});
|
|
194
|
-
it("receipt includes box-drawing characters", () => {
|
|
195
|
-
const summary = makeSummary();
|
|
196
|
-
const receipt = formatSessionReceipt({
|
|
197
|
-
summary,
|
|
198
|
-
durationMs: 60_000,
|
|
199
|
-
toolCalls: 3,
|
|
200
|
-
});
|
|
201
|
-
expect(receipt).toContain("┌");
|
|
202
|
-
expect(receipt).toContain("└");
|
|
203
|
-
expect(receipt).toContain("│");
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
// ── TF-C.3: WeeklyStats tokensByMechanism ────────────────────────
|
|
207
|
-
describe("weekly stats mechanism breakdown", () => {
|
|
208
|
-
it("formatStatsReport includes mechanism breakdown when present", async () => {
|
|
209
|
-
const { formatStatsReport } = await import("../tracking/weekly-accumulator.js");
|
|
210
|
-
const stats = {
|
|
211
|
-
version: 1,
|
|
212
|
-
weekly: {
|
|
213
|
-
weekStart: "2026-01-06",
|
|
214
|
-
sessions: 5,
|
|
215
|
-
tokensSaved: 100_000,
|
|
216
|
-
dollarsSaved: 3.0,
|
|
217
|
-
toolCalls: 75,
|
|
218
|
-
violationsCaught: 2,
|
|
219
|
-
chokepointWarnings: 1,
|
|
220
|
-
correctionsApplied: 0,
|
|
221
|
-
blastRadiusComputed: 3,
|
|
222
|
-
avgEfficiency: 65,
|
|
223
|
-
avgLatencyP50: 2.1,
|
|
224
|
-
tokensByMechanism: {
|
|
225
|
-
graph_query: 74000,
|
|
226
|
-
shell_compression: 12000,
|
|
227
|
-
format_encoding: 8000,
|
|
228
|
-
smart_truncation: 6000,
|
|
229
|
-
},
|
|
230
|
-
},
|
|
231
|
-
allTime: {
|
|
232
|
-
firstSessionDate: "2026-01-01T00:00:00Z",
|
|
233
|
-
totalSessions: 20,
|
|
234
|
-
totalTokensSaved: 500_000,
|
|
235
|
-
totalDollarsSaved: 15.0,
|
|
236
|
-
totalViolationsCaught: 10,
|
|
237
|
-
},
|
|
238
|
-
lastUpdated: new Date().toISOString(),
|
|
239
|
-
};
|
|
240
|
-
const report = formatStatsReport(stats);
|
|
241
|
-
expect(report).toContain("By mechanism:");
|
|
242
|
-
expect(report).toContain("graph_query");
|
|
243
|
-
expect(report).toContain("shell_compression");
|
|
244
|
-
expect(report).toContain("format_encoding");
|
|
245
|
-
expect(report).toContain("█");
|
|
246
|
-
});
|
|
247
|
-
it("formatStatsReport omits mechanism section when empty", async () => {
|
|
248
|
-
const { formatStatsReport } = await import("../tracking/weekly-accumulator.js");
|
|
249
|
-
const stats = {
|
|
250
|
-
version: 1,
|
|
251
|
-
weekly: {
|
|
252
|
-
weekStart: "2026-01-06",
|
|
253
|
-
sessions: 1,
|
|
254
|
-
tokensSaved: 1000,
|
|
255
|
-
dollarsSaved: 0.03,
|
|
256
|
-
toolCalls: 5,
|
|
257
|
-
violationsCaught: 0,
|
|
258
|
-
chokepointWarnings: 0,
|
|
259
|
-
correctionsApplied: 0,
|
|
260
|
-
blastRadiusComputed: 0,
|
|
261
|
-
avgEfficiency: 50,
|
|
262
|
-
avgLatencyP50: 1.5,
|
|
263
|
-
},
|
|
264
|
-
allTime: {
|
|
265
|
-
firstSessionDate: "2026-01-01T00:00:00Z",
|
|
266
|
-
totalSessions: 1,
|
|
267
|
-
totalTokensSaved: 1000,
|
|
268
|
-
totalDollarsSaved: 0.03,
|
|
269
|
-
totalViolationsCaught: 0,
|
|
270
|
-
},
|
|
271
|
-
lastUpdated: new Date().toISOString(),
|
|
272
|
-
};
|
|
273
|
-
const report = formatStatsReport(stats);
|
|
274
|
-
expect(report).not.toContain("By mechanism:");
|
|
275
|
-
});
|
|
276
|
-
});
|
|
277
|
-
// ── End-to-End: Token Flow → Summary → Receipt ───────────────────
|
|
278
|
-
describe("end-to-end flow", () => {
|
|
279
|
-
it("events → aggregation → session history → receipt", () => {
|
|
280
|
-
const writer = new TokenFlowWriter(unerrDir, "e2e-session");
|
|
281
|
-
writer.record({
|
|
282
|
-
session_id: "e2e-session",
|
|
283
|
-
turn: 1,
|
|
284
|
-
mechanism: "graph_query",
|
|
285
|
-
tool: "search_code",
|
|
286
|
-
tokens_without: 5000,
|
|
287
|
-
tokens_with: 1800,
|
|
288
|
-
tokens_saved: 3200,
|
|
289
|
-
});
|
|
290
|
-
writer.record({
|
|
291
|
-
session_id: "e2e-session",
|
|
292
|
-
turn: 2,
|
|
293
|
-
mechanism: "graph_query",
|
|
294
|
-
tool: "get_entity",
|
|
295
|
-
tokens_without: 3000,
|
|
296
|
-
tokens_with: 2400,
|
|
297
|
-
tokens_saved: 600,
|
|
298
|
-
});
|
|
299
|
-
writer.record({
|
|
300
|
-
session_id: "e2e-session",
|
|
301
|
-
turn: 3,
|
|
302
|
-
mechanism: "graph_query",
|
|
303
|
-
tool: "get_callers",
|
|
304
|
-
tokens_without: 6800,
|
|
305
|
-
tokens_with: 2000,
|
|
306
|
-
tokens_saved: 4800,
|
|
307
|
-
});
|
|
308
|
-
writer.record({
|
|
309
|
-
session_id: "e2e-session",
|
|
310
|
-
turn: 4,
|
|
311
|
-
mechanism: "shell_compression",
|
|
312
|
-
tool: null,
|
|
313
|
-
tokens_without: 2000,
|
|
314
|
-
tokens_with: 800,
|
|
315
|
-
tokens_saved: 1200,
|
|
316
|
-
});
|
|
317
|
-
const summary = aggregateSession(writer.getSessionEvents(), "e2e-session");
|
|
318
|
-
expect(summary.total_tokens_saved).toBe(9800);
|
|
319
|
-
expect(summary.by_mechanism.graph_query.tokens_saved).toBe(8600);
|
|
320
|
-
const topMech = Object.entries(summary.by_mechanism).sort(([, a], [, b]) => b.tokens_saved - a.tokens_saved)[0];
|
|
321
|
-
appendSessionHistory(unerrDir, {
|
|
322
|
-
sessionId: "e2e-session",
|
|
323
|
-
startedAt: new Date().toISOString(),
|
|
324
|
-
endedAt: new Date().toISOString(),
|
|
325
|
-
durationMs: 300_000,
|
|
326
|
-
toolCalls: 4,
|
|
327
|
-
tokensSaved: summary.total_tokens_saved,
|
|
328
|
-
tokensProcessed: summary.total_tokens_without,
|
|
329
|
-
efficiency: summary.efficiency_pct,
|
|
330
|
-
dollarsSaved: 0,
|
|
331
|
-
modelId: "unknown",
|
|
332
|
-
entityCount: 0,
|
|
333
|
-
tokenFlowSummary: {
|
|
334
|
-
by_mechanism: Object.fromEntries(Object.entries(summary.by_mechanism).map(([k, v]) => [
|
|
335
|
-
k,
|
|
336
|
-
{ tokens_saved: v.tokens_saved, event_count: v.event_count },
|
|
337
|
-
])),
|
|
338
|
-
top_mechanism: topMech?.[0] ?? "none",
|
|
339
|
-
efficiency_pct: summary.efficiency_pct,
|
|
340
|
-
total_tokens_saved: summary.total_tokens_saved,
|
|
341
|
-
total_tokens_delivered: summary.total_tokens_with,
|
|
342
|
-
},
|
|
343
|
-
});
|
|
344
|
-
const history = readSessionHistory(unerrDir);
|
|
345
|
-
expect(history).toHaveLength(1);
|
|
346
|
-
expect(history[0].tokenFlowSummary.top_mechanism).toBe("graph_query");
|
|
347
|
-
const receipt = formatSessionReceipt({
|
|
348
|
-
summary,
|
|
349
|
-
durationMs: 300_000,
|
|
350
|
-
toolCalls: 4,
|
|
351
|
-
});
|
|
352
|
-
expect(receipt).toContain("graph_query");
|
|
353
|
-
expect(receipt).toContain("shell_compression");
|
|
354
|
-
});
|
|
355
|
-
});
|
|
356
|
-
});
|
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Integration tests for token-flow API routes.
|
|
3
|
-
* Tests all 6 endpoints against real data in `.unerr/metrics.db`
|
|
4
|
-
* (token_flow_events table).
|
|
5
|
-
*/
|
|
6
|
-
import { join } from "node:path";
|
|
7
|
-
import { beforeAll, describe, expect, it } from "vitest";
|
|
8
|
-
import { createTokenFlowRoutes } from "../server/routes/token-flow.js";
|
|
9
|
-
import { readTokenFlowEvents } from "../tracking/token-flow.js";
|
|
10
|
-
const unerrDir = join(process.cwd(), ".unerr");
|
|
11
|
-
// Count raw events for validation — driven by the same reader the routes use.
|
|
12
|
-
// The routes strip "persistent_memory" events before aggregating (see
|
|
13
|
-
// stripPersistentMemory in src/server/routes/token-flow.ts), so we match that
|
|
14
|
-
// filter here to keep totals comparable.
|
|
15
|
-
function countRawEvents() {
|
|
16
|
-
const events = readTokenFlowEvents(unerrDir).filter((e) => e.mechanism !== "persistent_memory");
|
|
17
|
-
const sessions = new Set();
|
|
18
|
-
let saved = 0;
|
|
19
|
-
let without = 0;
|
|
20
|
-
let with_ = 0;
|
|
21
|
-
for (const e of events) {
|
|
22
|
-
sessions.add(e.session_id);
|
|
23
|
-
saved += e.tokens_saved;
|
|
24
|
-
without += e.tokens_without;
|
|
25
|
-
with_ += e.tokens_with;
|
|
26
|
-
}
|
|
27
|
-
return { total: events.length, sessions, saved, without, with_ };
|
|
28
|
-
}
|
|
29
|
-
const hasData = countRawEvents().total > 0;
|
|
30
|
-
async function fetchRoute(app, path) {
|
|
31
|
-
const res = await app.fetch(new Request(`http://localhost${path}`));
|
|
32
|
-
return {
|
|
33
|
-
status: res.status,
|
|
34
|
-
json: (await res.json()),
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
describe.skipIf(!hasData)("token-flow API routes", () => {
|
|
38
|
-
let app;
|
|
39
|
-
let raw;
|
|
40
|
-
beforeAll(() => {
|
|
41
|
-
app = createTokenFlowRoutes({ unerrDir, getTokenFlowWriter: () => null });
|
|
42
|
-
raw = countRawEvents();
|
|
43
|
-
});
|
|
44
|
-
describe("GET /global", () => {
|
|
45
|
-
it("returns aggregate KPIs across all sessions", async () => {
|
|
46
|
-
const { status, json } = await fetchRoute(app, "/global");
|
|
47
|
-
expect(status).toBe(200);
|
|
48
|
-
const d = json.data;
|
|
49
|
-
expect(d.total_sessions).toBe(raw.sessions.size);
|
|
50
|
-
expect(d.event_count).toBe(raw.total);
|
|
51
|
-
expect(d.total_tokens_saved).toBe(raw.saved);
|
|
52
|
-
expect(d.total_tokens_without).toBe(raw.without);
|
|
53
|
-
expect(d.total_tokens_with).toBe(raw.with_);
|
|
54
|
-
expect(d.efficiency_pct).toBe(Math.round((raw.saved / raw.without) * 100));
|
|
55
|
-
});
|
|
56
|
-
it("mechanism percentages sum to ~100", async () => {
|
|
57
|
-
const { json } = await fetchRoute(app, "/global");
|
|
58
|
-
const mechs = json.data
|
|
59
|
-
.by_mechanism;
|
|
60
|
-
const pctSum = Object.values(mechs).reduce((s, m) => s + m.pct_of_total, 0);
|
|
61
|
-
expect(pctSum).toBeGreaterThanOrEqual(98);
|
|
62
|
-
expect(pctSum).toBeLessThanOrEqual(102);
|
|
63
|
-
});
|
|
64
|
-
it("mechanism event counts sum to total", async () => {
|
|
65
|
-
const { json } = await fetchRoute(app, "/global");
|
|
66
|
-
const mechs = json.data
|
|
67
|
-
.by_mechanism;
|
|
68
|
-
const evtSum = Object.values(mechs).reduce((s, m) => s + m.event_count, 0);
|
|
69
|
-
expect(evtSum).toBe(raw.total);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
describe("GET /sessions", () => {
|
|
73
|
-
it("lists all unique sessions sorted by recency", async () => {
|
|
74
|
-
const { status, json } = await fetchRoute(app, "/sessions");
|
|
75
|
-
expect(status).toBe(200);
|
|
76
|
-
const sessions = json.data;
|
|
77
|
-
expect(sessions.length).toBe(raw.sessions.size);
|
|
78
|
-
// Event counts sum to total
|
|
79
|
-
const evtSum = sessions.reduce((s, x) => s + x.event_count, 0);
|
|
80
|
-
expect(evtSum).toBe(raw.total);
|
|
81
|
-
// Saved sums match
|
|
82
|
-
const savedSum = sessions.reduce((s, x) => s + x.total_saved, 0);
|
|
83
|
-
expect(savedSum).toBe(raw.saved);
|
|
84
|
-
// Sorted descending by last_ts
|
|
85
|
-
for (let i = 1; i < sessions.length; i++) {
|
|
86
|
-
expect(sessions[i].last_ts <= sessions[i - 1].last_ts).toBe(true);
|
|
87
|
-
}
|
|
88
|
-
// Each session has mechanisms
|
|
89
|
-
for (const s of sessions) {
|
|
90
|
-
expect(s.mechanisms.length).toBeGreaterThan(0);
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
describe("GET /session", () => {
|
|
95
|
-
it("returns current session summary when no session_id param", async () => {
|
|
96
|
-
const { status, json } = await fetchRoute(app, "/session");
|
|
97
|
-
expect(status).toBe(200);
|
|
98
|
-
// With no writer, it picks the most recent session from disk
|
|
99
|
-
const d = json.data;
|
|
100
|
-
// Could be null if aggregation yields 0 (unlikely with real data)
|
|
101
|
-
if (d) {
|
|
102
|
-
expect(d.session_id).toBeDefined();
|
|
103
|
-
expect(typeof d.total_tokens_saved).toBe("number");
|
|
104
|
-
expect(typeof d.efficiency_pct).toBe("number");
|
|
105
|
-
expect(d.by_mechanism).toBeDefined();
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
it("returns specific session when session_id param provided", async () => {
|
|
109
|
-
// Get first session ID from /sessions
|
|
110
|
-
const { json: sessionsJson } = await fetchRoute(app, "/sessions");
|
|
111
|
-
const sessions = sessionsJson.data;
|
|
112
|
-
const targetId = sessions[sessions.length - 1].session_id; // oldest session
|
|
113
|
-
const expectedEvents = sessions[sessions.length - 1].event_count;
|
|
114
|
-
const { status, json } = await fetchRoute(app, `/session?session_id=${targetId}`);
|
|
115
|
-
expect(status).toBe(200);
|
|
116
|
-
const d = json.data;
|
|
117
|
-
expect(d).not.toBeNull();
|
|
118
|
-
expect(d.session_id).toBe(targetId);
|
|
119
|
-
expect(d.event_count).toBe(expectedEvents);
|
|
120
|
-
expect(typeof d.total_tokens_saved).toBe("number");
|
|
121
|
-
expect(d.total_tokens_saved).toBeGreaterThan(0);
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
describe("GET /cumulative", () => {
|
|
125
|
-
it("returns monotonically increasing cumulative data", async () => {
|
|
126
|
-
const { json: sessionsJson } = await fetchRoute(app, "/sessions");
|
|
127
|
-
const sessions = sessionsJson.data;
|
|
128
|
-
const targetId = sessions[0].session_id;
|
|
129
|
-
const { status, json } = await fetchRoute(app, `/cumulative?session_id=${targetId}`);
|
|
130
|
-
expect(status).toBe(200);
|
|
131
|
-
const turns = json.data;
|
|
132
|
-
expect(turns.length).toBeGreaterThan(0);
|
|
133
|
-
// Monotonically non-decreasing
|
|
134
|
-
for (let i = 1; i < turns.length; i++) {
|
|
135
|
-
expect(turns[i].cumulative_tokens_saved).toBeGreaterThanOrEqual(turns[i - 1].cumulative_tokens_saved);
|
|
136
|
-
}
|
|
137
|
-
// Last cumulative equals total_saved
|
|
138
|
-
expect(turns[turns.length - 1].cumulative_tokens_saved).toBe(json.total_saved);
|
|
139
|
-
// Each turn has events and tools array
|
|
140
|
-
for (const t of turns) {
|
|
141
|
-
expect(t.event_count).toBeGreaterThan(0);
|
|
142
|
-
expect(Array.isArray(t.tools)).toBe(true);
|
|
143
|
-
expect(typeof t.tokens_saved_this_turn).toBe("number");
|
|
144
|
-
expect(typeof t.mechanisms_this_turn).toBe("object");
|
|
145
|
-
expect(typeof t.cumulative_by_mechanism).toBe("object");
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
describe("GET /events", () => {
|
|
150
|
-
it("filters by session_id and turn", async () => {
|
|
151
|
-
const { json: sessionsJson } = await fetchRoute(app, "/sessions");
|
|
152
|
-
const sessions = sessionsJson.data;
|
|
153
|
-
const targetId = sessions[0].session_id;
|
|
154
|
-
const { status, json } = await fetchRoute(app, `/events?session_id=${targetId}&turn=0`);
|
|
155
|
-
expect(status).toBe(200);
|
|
156
|
-
const evts = json.data;
|
|
157
|
-
expect(evts.length).toBeGreaterThan(0);
|
|
158
|
-
for (const e of evts) {
|
|
159
|
-
expect(e.session_id).toBe(targetId);
|
|
160
|
-
expect(e.turn).toBe(0);
|
|
161
|
-
expect(typeof e.tokens_saved).toBe("number");
|
|
162
|
-
expect(typeof e.tokens_without).toBe("number");
|
|
163
|
-
expect(typeof e.tokens_with).toBe("number");
|
|
164
|
-
expect(typeof e.mechanism).toBe("string");
|
|
165
|
-
expect(typeof e.ts).toBe("string");
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
it("filters by mechanism", async () => {
|
|
169
|
-
const { status, json } = await fetchRoute(app, "/events?mechanism=graph_query");
|
|
170
|
-
expect(status).toBe(200);
|
|
171
|
-
const evts = json.data;
|
|
172
|
-
for (const e of evts) {
|
|
173
|
-
expect(e.mechanism).toBe("graph_query");
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
describe("cross-endpoint consistency", () => {
|
|
178
|
-
it("global totals match sum of all session totals", async () => {
|
|
179
|
-
const { json: globalJson } = await fetchRoute(app, "/global");
|
|
180
|
-
const g = globalJson.data;
|
|
181
|
-
const { json: sessionsJson } = await fetchRoute(app, "/sessions");
|
|
182
|
-
const sessions = sessionsJson.data;
|
|
183
|
-
const sessionSavedSum = sessions.reduce((s, x) => s + x.total_saved, 0);
|
|
184
|
-
expect(sessionSavedSum).toBe(g.total_tokens_saved);
|
|
185
|
-
});
|
|
186
|
-
it("session detail matches session list entry", async () => {
|
|
187
|
-
const { json: sessionsJson } = await fetchRoute(app, "/sessions");
|
|
188
|
-
const sessions = sessionsJson.data;
|
|
189
|
-
for (const s of sessions) {
|
|
190
|
-
const { json } = await fetchRoute(app, `/session?session_id=${s.session_id}`);
|
|
191
|
-
const d = json.data;
|
|
192
|
-
if (d) {
|
|
193
|
-
expect(d.event_count).toBe(s.event_count);
|
|
194
|
-
expect(d.total_tokens_saved).toBe(s.total_saved);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
});
|