@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,695 +0,0 @@
|
|
|
1
|
-
import { existsSync, 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 { closeMetricsStore, openMetricsStore, } from "../tracking/metrics-store.js";
|
|
6
|
-
import { TokenFlowWriter, aggregateByMechanism, aggregateSession, readTokenFlowEvents, } from "../tracking/token-flow.js";
|
|
7
|
-
describe("token-flow", () => {
|
|
8
|
-
let tmpDir;
|
|
9
|
-
let unerrDir;
|
|
10
|
-
let dbPath;
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
tmpDir = join(os.tmpdir(), `unerr-tokenflow-test-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`);
|
|
13
|
-
unerrDir = join(tmpDir, ".unerr");
|
|
14
|
-
mkdirSync(unerrDir, { recursive: true });
|
|
15
|
-
dbPath = join(unerrDir, "metrics.db");
|
|
16
|
-
});
|
|
17
|
-
afterEach(() => {
|
|
18
|
-
closeMetricsStore(unerrDir);
|
|
19
|
-
rmSync(tmpDir, { recursive: true, force: true });
|
|
20
|
-
});
|
|
21
|
-
function makeInput(overrides) {
|
|
22
|
-
return {
|
|
23
|
-
session_id: "test-session-001",
|
|
24
|
-
turn: 1,
|
|
25
|
-
mechanism: "graph_query",
|
|
26
|
-
tool: "get_callers",
|
|
27
|
-
tokens_without: 5000,
|
|
28
|
-
tokens_with: 1800,
|
|
29
|
-
tokens_saved: 3200,
|
|
30
|
-
...overrides,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
// ── Writer Tests ──────────────────────────────────────────────────
|
|
34
|
-
describe("TokenFlowWriter", () => {
|
|
35
|
-
it("creates metrics.db on first write", () => {
|
|
36
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
37
|
-
writer.record(makeInput());
|
|
38
|
-
expect(existsSync(dbPath)).toBe(true);
|
|
39
|
-
});
|
|
40
|
-
it("writes events with auto-populated fields", () => {
|
|
41
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
42
|
-
writer.record(makeInput());
|
|
43
|
-
const [event] = readTokenFlowEvents(unerrDir);
|
|
44
|
-
expect(event).toBeDefined();
|
|
45
|
-
expect(event.id).toBeGreaterThan(0);
|
|
46
|
-
expect(event.ts).toMatch(/^\d{4}-\d{2}-\d{2}T/);
|
|
47
|
-
expect(event.pid).toBe(process.pid);
|
|
48
|
-
expect(event.session_id).toBe("test-session-001");
|
|
49
|
-
expect(event.turn).toBe(1);
|
|
50
|
-
expect(event.mechanism).toBe("graph_query");
|
|
51
|
-
expect(event.tool).toBe("get_callers");
|
|
52
|
-
expect(event.tokens_without).toBe(5000);
|
|
53
|
-
expect(event.tokens_with).toBe(1800);
|
|
54
|
-
expect(event.tokens_saved).toBe(3200);
|
|
55
|
-
});
|
|
56
|
-
it("assigns monotonic IDs across multiple writes", () => {
|
|
57
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
58
|
-
writer.record(makeInput({ turn: 1 }));
|
|
59
|
-
writer.record(makeInput({ turn: 2 }));
|
|
60
|
-
writer.record(makeInput({ turn: 3 }));
|
|
61
|
-
const events = readTokenFlowEvents(unerrDir);
|
|
62
|
-
expect(events).toHaveLength(3);
|
|
63
|
-
const ids = events.map((e) => e.id);
|
|
64
|
-
expect(ids).toEqual([...ids].sort((a, b) => a - b));
|
|
65
|
-
expect(new Set(ids).size).toBe(3);
|
|
66
|
-
});
|
|
67
|
-
it("records detail metadata when provided", () => {
|
|
68
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
69
|
-
writer.record(makeInput({ detail: { counterfactual: "file_read", depth: 3 } }));
|
|
70
|
-
const [event] = readTokenFlowEvents(unerrDir);
|
|
71
|
-
expect(event.detail).toEqual({ counterfactual: "file_read", depth: 3 });
|
|
72
|
-
});
|
|
73
|
-
it("records events with null tool (shell compression)", () => {
|
|
74
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
75
|
-
writer.record(makeInput({
|
|
76
|
-
mechanism: "shell_compression",
|
|
77
|
-
tool: null,
|
|
78
|
-
turn: 0,
|
|
79
|
-
tokens_without: 3400,
|
|
80
|
-
tokens_with: 800,
|
|
81
|
-
tokens_saved: 2600,
|
|
82
|
-
}));
|
|
83
|
-
const [event] = readTokenFlowEvents(unerrDir);
|
|
84
|
-
expect(event.tool).toBeNull();
|
|
85
|
-
expect(event.mechanism).toBe("shell_compression");
|
|
86
|
-
});
|
|
87
|
-
it("getSessionEvents returns in-memory buffer", () => {
|
|
88
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
89
|
-
writer.record(makeInput({ turn: 1 }));
|
|
90
|
-
writer.record(makeInput({ turn: 2 }));
|
|
91
|
-
const events = writer.getSessionEvents();
|
|
92
|
-
expect(events).toHaveLength(2);
|
|
93
|
-
expect(events[0].turn).toBe(1);
|
|
94
|
-
expect(events[1].turn).toBe(2);
|
|
95
|
-
});
|
|
96
|
-
it("getSessionTokensSaved computes running total", () => {
|
|
97
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
98
|
-
writer.record(makeInput({ tokens_saved: 3200 }));
|
|
99
|
-
writer.record(makeInput({ tokens_saved: 1200 }));
|
|
100
|
-
writer.record(makeInput({ tokens_saved: 480 }));
|
|
101
|
-
expect(writer.getSessionTokensSaved()).toBe(4880);
|
|
102
|
-
});
|
|
103
|
-
it("getSessionEfficiency computes correct percentage", () => {
|
|
104
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
105
|
-
writer.record(makeInput({ tokens_without: 10000, tokens_saved: 7000 }));
|
|
106
|
-
writer.record(makeInput({ tokens_without: 5000, tokens_saved: 2000 }));
|
|
107
|
-
// Total without: 15000, total saved: 9000 → 60%
|
|
108
|
-
expect(writer.getSessionEfficiency()).toBe(60);
|
|
109
|
-
});
|
|
110
|
-
it("getSessionEfficiency returns 0 when no events", () => {
|
|
111
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
112
|
-
expect(writer.getSessionEfficiency()).toBe(0);
|
|
113
|
-
});
|
|
114
|
-
it("handles zero-savings events", () => {
|
|
115
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
116
|
-
writer.record(makeInput({
|
|
117
|
-
tokens_without: 100,
|
|
118
|
-
tokens_with: 100,
|
|
119
|
-
tokens_saved: 0,
|
|
120
|
-
}));
|
|
121
|
-
expect(writer.getSessionTokensSaved()).toBe(0);
|
|
122
|
-
expect(writer.getSessionEfficiency()).toBe(0);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
// ── Reader Tests ──────────────────────────────────────────────────
|
|
126
|
-
describe("readTokenFlowEvents", () => {
|
|
127
|
-
it("returns empty array when file does not exist", () => {
|
|
128
|
-
const events = readTokenFlowEvents(unerrDir);
|
|
129
|
-
expect(events).toEqual([]);
|
|
130
|
-
});
|
|
131
|
-
it("reads all events from JSONL file", () => {
|
|
132
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
133
|
-
writer.record(makeInput({ turn: 1 }));
|
|
134
|
-
writer.record(makeInput({ turn: 2 }));
|
|
135
|
-
writer.record(makeInput({ turn: 3 }));
|
|
136
|
-
const events = readTokenFlowEvents(unerrDir);
|
|
137
|
-
expect(events).toHaveLength(3);
|
|
138
|
-
});
|
|
139
|
-
it("filters by session_id", () => {
|
|
140
|
-
const writer1 = new TokenFlowWriter(unerrDir, "session-a");
|
|
141
|
-
writer1.record(makeInput({ session_id: "session-a" }));
|
|
142
|
-
writer1.record(makeInput({ session_id: "session-a" }));
|
|
143
|
-
const writer2 = new TokenFlowWriter(unerrDir, "session-b");
|
|
144
|
-
writer2.record(makeInput({ session_id: "session-b" }));
|
|
145
|
-
const eventsA = readTokenFlowEvents(unerrDir, {
|
|
146
|
-
session_id: "session-a",
|
|
147
|
-
});
|
|
148
|
-
expect(eventsA).toHaveLength(2);
|
|
149
|
-
const eventsB = readTokenFlowEvents(unerrDir, {
|
|
150
|
-
session_id: "session-b",
|
|
151
|
-
});
|
|
152
|
-
expect(eventsB).toHaveLength(1);
|
|
153
|
-
});
|
|
154
|
-
it("filters by mechanism", () => {
|
|
155
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
156
|
-
writer.record(makeInput({ mechanism: "graph_query" }));
|
|
157
|
-
writer.record(makeInput({ mechanism: "shell_compression" }));
|
|
158
|
-
writer.record(makeInput({ mechanism: "format_encoding" }));
|
|
159
|
-
writer.record(makeInput({ mechanism: "graph_query" }));
|
|
160
|
-
const graphEvents = readTokenFlowEvents(unerrDir, {
|
|
161
|
-
mechanism: "graph_query",
|
|
162
|
-
});
|
|
163
|
-
expect(graphEvents).toHaveLength(2);
|
|
164
|
-
});
|
|
165
|
-
it("skips malformed detail JSON gracefully", () => {
|
|
166
|
-
// SQLite stores detail as a TEXT column; if a row ever has a corrupt
|
|
167
|
-
// JSON payload (e.g. external migration), the reader returns it but
|
|
168
|
-
// simply leaves `detail` undefined rather than throwing.
|
|
169
|
-
const store = openMetricsStore(unerrDir);
|
|
170
|
-
store.insertTokenFlow({
|
|
171
|
-
ts: Date.now(),
|
|
172
|
-
ts_iso: new Date().toISOString(),
|
|
173
|
-
session_id: "s1",
|
|
174
|
-
pid: 1,
|
|
175
|
-
turn: 1,
|
|
176
|
-
mechanism: "graph_query",
|
|
177
|
-
tool: "t",
|
|
178
|
-
tokens_without: 100,
|
|
179
|
-
tokens_with: 50,
|
|
180
|
-
tokens_saved: 50,
|
|
181
|
-
detail: "{not valid json",
|
|
182
|
-
});
|
|
183
|
-
store.insertTokenFlow({
|
|
184
|
-
ts: Date.now(),
|
|
185
|
-
ts_iso: new Date().toISOString(),
|
|
186
|
-
session_id: "s1",
|
|
187
|
-
pid: 1,
|
|
188
|
-
turn: 2,
|
|
189
|
-
mechanism: "graph_query",
|
|
190
|
-
tool: "t",
|
|
191
|
-
tokens_without: 200,
|
|
192
|
-
tokens_with: 100,
|
|
193
|
-
tokens_saved: 100,
|
|
194
|
-
detail: null,
|
|
195
|
-
});
|
|
196
|
-
expect(() => readTokenFlowEvents(unerrDir)).not.toThrow();
|
|
197
|
-
const events = readTokenFlowEvents(unerrDir);
|
|
198
|
-
expect(events).toHaveLength(2);
|
|
199
|
-
});
|
|
200
|
-
});
|
|
201
|
-
// ── Aggregation Tests ─────────────────────────────────────────────
|
|
202
|
-
describe("aggregateSession", () => {
|
|
203
|
-
it("computes correct session totals", () => {
|
|
204
|
-
const writer = new TokenFlowWriter(unerrDir, "s1");
|
|
205
|
-
writer.record(makeInput({
|
|
206
|
-
session_id: "s1",
|
|
207
|
-
turn: 1,
|
|
208
|
-
tokens_without: 5000,
|
|
209
|
-
tokens_with: 1800,
|
|
210
|
-
tokens_saved: 3200,
|
|
211
|
-
}));
|
|
212
|
-
writer.record(makeInput({
|
|
213
|
-
session_id: "s1",
|
|
214
|
-
turn: 2,
|
|
215
|
-
tokens_without: 2000,
|
|
216
|
-
tokens_with: 800,
|
|
217
|
-
tokens_saved: 1200,
|
|
218
|
-
}));
|
|
219
|
-
writer.record(makeInput({
|
|
220
|
-
session_id: "s1",
|
|
221
|
-
turn: 3,
|
|
222
|
-
tokens_without: 1000,
|
|
223
|
-
tokens_with: 520,
|
|
224
|
-
tokens_saved: 480,
|
|
225
|
-
}));
|
|
226
|
-
const summary = aggregateSession(writer.getSessionEvents(), "s1");
|
|
227
|
-
expect(summary.session_id).toBe("s1");
|
|
228
|
-
expect(summary.total_turns).toBe(3);
|
|
229
|
-
expect(summary.total_tokens_without).toBe(8000);
|
|
230
|
-
expect(summary.total_tokens_with).toBe(3120);
|
|
231
|
-
expect(summary.total_tokens_saved).toBe(4880);
|
|
232
|
-
expect(summary.efficiency_pct).toBe(61);
|
|
233
|
-
});
|
|
234
|
-
it("filters to correct session when multiple sessions present", () => {
|
|
235
|
-
const events = [
|
|
236
|
-
{
|
|
237
|
-
id: 1,
|
|
238
|
-
ts: "t1",
|
|
239
|
-
session_id: "s1",
|
|
240
|
-
pid: 1,
|
|
241
|
-
turn: 1,
|
|
242
|
-
mechanism: "graph_query",
|
|
243
|
-
tool: "t",
|
|
244
|
-
tokens_without: 100,
|
|
245
|
-
tokens_with: 50,
|
|
246
|
-
tokens_saved: 50,
|
|
247
|
-
},
|
|
248
|
-
{
|
|
249
|
-
id: 2,
|
|
250
|
-
ts: "t2",
|
|
251
|
-
session_id: "s2",
|
|
252
|
-
pid: 1,
|
|
253
|
-
turn: 1,
|
|
254
|
-
mechanism: "graph_query",
|
|
255
|
-
tool: "t",
|
|
256
|
-
tokens_without: 200,
|
|
257
|
-
tokens_with: 100,
|
|
258
|
-
tokens_saved: 100,
|
|
259
|
-
},
|
|
260
|
-
{
|
|
261
|
-
id: 3,
|
|
262
|
-
ts: "t3",
|
|
263
|
-
session_id: "s1",
|
|
264
|
-
pid: 1,
|
|
265
|
-
turn: 2,
|
|
266
|
-
mechanism: "graph_query",
|
|
267
|
-
tool: "t",
|
|
268
|
-
tokens_without: 300,
|
|
269
|
-
tokens_with: 150,
|
|
270
|
-
tokens_saved: 150,
|
|
271
|
-
},
|
|
272
|
-
];
|
|
273
|
-
const s1Summary = aggregateSession(events, "s1");
|
|
274
|
-
expect(s1Summary.total_tokens_saved).toBe(200);
|
|
275
|
-
expect(s1Summary.total_turns).toBe(2);
|
|
276
|
-
const s2Summary = aggregateSession(events, "s2");
|
|
277
|
-
expect(s2Summary.total_tokens_saved).toBe(100);
|
|
278
|
-
expect(s2Summary.total_turns).toBe(1);
|
|
279
|
-
});
|
|
280
|
-
it("builds mechanism breakdown with percentages", () => {
|
|
281
|
-
const events = [
|
|
282
|
-
{
|
|
283
|
-
id: 1,
|
|
284
|
-
ts: "t1",
|
|
285
|
-
session_id: "s1",
|
|
286
|
-
pid: 1,
|
|
287
|
-
turn: 1,
|
|
288
|
-
mechanism: "graph_query",
|
|
289
|
-
tool: "get_callers",
|
|
290
|
-
tokens_without: 5000,
|
|
291
|
-
tokens_with: 1800,
|
|
292
|
-
tokens_saved: 3200,
|
|
293
|
-
},
|
|
294
|
-
{
|
|
295
|
-
id: 2,
|
|
296
|
-
ts: "t2",
|
|
297
|
-
session_id: "s1",
|
|
298
|
-
pid: 1,
|
|
299
|
-
turn: 2,
|
|
300
|
-
mechanism: "shell_compression",
|
|
301
|
-
tool: null,
|
|
302
|
-
tokens_without: 2000,
|
|
303
|
-
tokens_with: 800,
|
|
304
|
-
tokens_saved: 1200,
|
|
305
|
-
},
|
|
306
|
-
{
|
|
307
|
-
id: 3,
|
|
308
|
-
ts: "t3",
|
|
309
|
-
session_id: "s1",
|
|
310
|
-
pid: 1,
|
|
311
|
-
turn: 3,
|
|
312
|
-
mechanism: "graph_query",
|
|
313
|
-
tool: "search_code",
|
|
314
|
-
tokens_without: 1000,
|
|
315
|
-
tokens_with: 600,
|
|
316
|
-
tokens_saved: 400,
|
|
317
|
-
},
|
|
318
|
-
];
|
|
319
|
-
const summary = aggregateSession(events, "s1");
|
|
320
|
-
expect(summary.by_mechanism.graph_query).toBeDefined();
|
|
321
|
-
expect(summary.by_mechanism.graph_query.tokens_saved).toBe(3600);
|
|
322
|
-
expect(summary.by_mechanism.graph_query.event_count).toBe(2);
|
|
323
|
-
expect(summary.by_mechanism.shell_compression).toBeDefined();
|
|
324
|
-
expect(summary.by_mechanism.shell_compression.tokens_saved).toBe(1200);
|
|
325
|
-
expect(summary.by_mechanism.shell_compression.event_count).toBe(1);
|
|
326
|
-
// Percentages should sum close to 100
|
|
327
|
-
const totalPct = Object.values(summary.by_mechanism).reduce((sum, m) => sum + m.pct_of_total, 0);
|
|
328
|
-
expect(totalPct).toBeCloseTo(100, 0);
|
|
329
|
-
});
|
|
330
|
-
it("identifies top turns sorted by tokens_saved", () => {
|
|
331
|
-
const events = [
|
|
332
|
-
{
|
|
333
|
-
id: 1,
|
|
334
|
-
ts: "t1",
|
|
335
|
-
session_id: "s1",
|
|
336
|
-
pid: 1,
|
|
337
|
-
turn: 1,
|
|
338
|
-
mechanism: "graph_query",
|
|
339
|
-
tool: "search_code",
|
|
340
|
-
tokens_without: 1000,
|
|
341
|
-
tokens_with: 800,
|
|
342
|
-
tokens_saved: 200,
|
|
343
|
-
},
|
|
344
|
-
{
|
|
345
|
-
id: 2,
|
|
346
|
-
ts: "t2",
|
|
347
|
-
session_id: "s1",
|
|
348
|
-
pid: 1,
|
|
349
|
-
turn: 2,
|
|
350
|
-
mechanism: "graph_query",
|
|
351
|
-
tool: "get_callers",
|
|
352
|
-
tokens_without: 5000,
|
|
353
|
-
tokens_with: 1000,
|
|
354
|
-
tokens_saved: 4000,
|
|
355
|
-
},
|
|
356
|
-
{
|
|
357
|
-
id: 3,
|
|
358
|
-
ts: "t3",
|
|
359
|
-
session_id: "s1",
|
|
360
|
-
pid: 1,
|
|
361
|
-
turn: 3,
|
|
362
|
-
mechanism: "shell_compression",
|
|
363
|
-
tool: null,
|
|
364
|
-
tokens_without: 3000,
|
|
365
|
-
tokens_with: 1200,
|
|
366
|
-
tokens_saved: 1800,
|
|
367
|
-
},
|
|
368
|
-
{
|
|
369
|
-
id: 4,
|
|
370
|
-
ts: "t4",
|
|
371
|
-
session_id: "s1",
|
|
372
|
-
pid: 1,
|
|
373
|
-
turn: 2,
|
|
374
|
-
mechanism: "format_encoding",
|
|
375
|
-
tool: "get_callers",
|
|
376
|
-
tokens_without: 800,
|
|
377
|
-
tokens_with: 500,
|
|
378
|
-
tokens_saved: 300,
|
|
379
|
-
},
|
|
380
|
-
];
|
|
381
|
-
const summary = aggregateSession(events, "s1");
|
|
382
|
-
expect(summary.top_turns[0].turn).toBe(2);
|
|
383
|
-
expect(summary.top_turns[0].tokens_saved).toBe(4300);
|
|
384
|
-
expect(summary.top_turns[0].tool).toBe("get_callers");
|
|
385
|
-
expect(summary.top_turns[0].primary_mechanism).toBe("graph_query");
|
|
386
|
-
expect(summary.top_turns[1].turn).toBe(3);
|
|
387
|
-
expect(summary.top_turns[1].tokens_saved).toBe(1800);
|
|
388
|
-
});
|
|
389
|
-
it("limits top_turns to 5", () => {
|
|
390
|
-
const events = [];
|
|
391
|
-
for (let i = 1; i <= 10; i++) {
|
|
392
|
-
events.push({
|
|
393
|
-
id: i,
|
|
394
|
-
ts: `t${i}`,
|
|
395
|
-
session_id: "s1",
|
|
396
|
-
pid: 1,
|
|
397
|
-
turn: i,
|
|
398
|
-
mechanism: "graph_query",
|
|
399
|
-
tool: `tool_${i}`,
|
|
400
|
-
tokens_without: i * 1000,
|
|
401
|
-
tokens_with: i * 100,
|
|
402
|
-
tokens_saved: i * 900,
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
const summary = aggregateSession(events, "s1");
|
|
406
|
-
expect(summary.top_turns).toHaveLength(5);
|
|
407
|
-
expect(summary.top_turns[0].turn).toBe(10);
|
|
408
|
-
});
|
|
409
|
-
it("returns zero efficiency when no events for session", () => {
|
|
410
|
-
const summary = aggregateSession([], "nonexistent");
|
|
411
|
-
expect(summary.total_turns).toBe(0);
|
|
412
|
-
expect(summary.total_tokens_saved).toBe(0);
|
|
413
|
-
expect(summary.efficiency_pct).toBe(0);
|
|
414
|
-
expect(summary.top_turns).toHaveLength(0);
|
|
415
|
-
});
|
|
416
|
-
it("handles multiple operations within same turn", () => {
|
|
417
|
-
const events = [
|
|
418
|
-
{
|
|
419
|
-
id: 1,
|
|
420
|
-
ts: "t1",
|
|
421
|
-
session_id: "s1",
|
|
422
|
-
pid: 1,
|
|
423
|
-
turn: 3,
|
|
424
|
-
mechanism: "graph_query",
|
|
425
|
-
tool: "get_callers",
|
|
426
|
-
tokens_without: 4800,
|
|
427
|
-
tokens_with: 1800,
|
|
428
|
-
tokens_saved: 3000,
|
|
429
|
-
},
|
|
430
|
-
{
|
|
431
|
-
id: 2,
|
|
432
|
-
ts: "t2",
|
|
433
|
-
session_id: "s1",
|
|
434
|
-
pid: 1,
|
|
435
|
-
turn: 3,
|
|
436
|
-
mechanism: "format_encoding",
|
|
437
|
-
tool: "get_callers",
|
|
438
|
-
tokens_without: 1800,
|
|
439
|
-
tokens_with: 1480,
|
|
440
|
-
tokens_saved: 320,
|
|
441
|
-
},
|
|
442
|
-
{
|
|
443
|
-
id: 3,
|
|
444
|
-
ts: "t3",
|
|
445
|
-
session_id: "s1",
|
|
446
|
-
pid: 1,
|
|
447
|
-
turn: 3,
|
|
448
|
-
mechanism: "session_dedup",
|
|
449
|
-
tool: "get_callers",
|
|
450
|
-
tokens_without: 480,
|
|
451
|
-
tokens_with: 0,
|
|
452
|
-
tokens_saved: 480,
|
|
453
|
-
},
|
|
454
|
-
];
|
|
455
|
-
const summary = aggregateSession(events, "s1");
|
|
456
|
-
expect(summary.total_turns).toBe(1);
|
|
457
|
-
expect(summary.total_tokens_saved).toBe(3800);
|
|
458
|
-
const turn3 = summary.top_turns[0];
|
|
459
|
-
expect(turn3.turn).toBe(3);
|
|
460
|
-
expect(turn3.tokens_saved).toBe(3800);
|
|
461
|
-
expect(turn3.primary_mechanism).toBe("graph_query");
|
|
462
|
-
});
|
|
463
|
-
});
|
|
464
|
-
describe("aggregateByMechanism", () => {
|
|
465
|
-
it("aggregates across all sessions", () => {
|
|
466
|
-
const events = [
|
|
467
|
-
{
|
|
468
|
-
id: 1,
|
|
469
|
-
ts: "t1",
|
|
470
|
-
session_id: "s1",
|
|
471
|
-
pid: 1,
|
|
472
|
-
turn: 1,
|
|
473
|
-
mechanism: "graph_query",
|
|
474
|
-
tool: "t",
|
|
475
|
-
tokens_without: 100,
|
|
476
|
-
tokens_with: 50,
|
|
477
|
-
tokens_saved: 50,
|
|
478
|
-
},
|
|
479
|
-
{
|
|
480
|
-
id: 2,
|
|
481
|
-
ts: "t2",
|
|
482
|
-
session_id: "s2",
|
|
483
|
-
pid: 1,
|
|
484
|
-
turn: 1,
|
|
485
|
-
mechanism: "graph_query",
|
|
486
|
-
tool: "t",
|
|
487
|
-
tokens_without: 200,
|
|
488
|
-
tokens_with: 100,
|
|
489
|
-
tokens_saved: 100,
|
|
490
|
-
},
|
|
491
|
-
{
|
|
492
|
-
id: 3,
|
|
493
|
-
ts: "t3",
|
|
494
|
-
session_id: "s1",
|
|
495
|
-
pid: 1,
|
|
496
|
-
turn: 2,
|
|
497
|
-
mechanism: "shell_compression",
|
|
498
|
-
tool: null,
|
|
499
|
-
tokens_without: 300,
|
|
500
|
-
tokens_with: 100,
|
|
501
|
-
tokens_saved: 200,
|
|
502
|
-
},
|
|
503
|
-
];
|
|
504
|
-
const breakdown = aggregateByMechanism(events);
|
|
505
|
-
expect(breakdown.graph_query.tokens_saved).toBe(150);
|
|
506
|
-
expect(breakdown.graph_query.event_count).toBe(2);
|
|
507
|
-
expect(breakdown.shell_compression.tokens_saved).toBe(200);
|
|
508
|
-
expect(breakdown.shell_compression.event_count).toBe(1);
|
|
509
|
-
});
|
|
510
|
-
it("computes correct percentages", () => {
|
|
511
|
-
const events = [
|
|
512
|
-
{
|
|
513
|
-
id: 1,
|
|
514
|
-
ts: "t1",
|
|
515
|
-
session_id: "s1",
|
|
516
|
-
pid: 1,
|
|
517
|
-
turn: 1,
|
|
518
|
-
mechanism: "graph_query",
|
|
519
|
-
tool: "t",
|
|
520
|
-
tokens_without: 1000,
|
|
521
|
-
tokens_with: 250,
|
|
522
|
-
tokens_saved: 750,
|
|
523
|
-
},
|
|
524
|
-
{
|
|
525
|
-
id: 2,
|
|
526
|
-
ts: "t2",
|
|
527
|
-
session_id: "s1",
|
|
528
|
-
pid: 1,
|
|
529
|
-
turn: 2,
|
|
530
|
-
mechanism: "shell_compression",
|
|
531
|
-
tool: null,
|
|
532
|
-
tokens_without: 500,
|
|
533
|
-
tokens_with: 250,
|
|
534
|
-
tokens_saved: 250,
|
|
535
|
-
},
|
|
536
|
-
];
|
|
537
|
-
const breakdown = aggregateByMechanism(events);
|
|
538
|
-
expect(breakdown.graph_query.pct_of_total).toBe(75);
|
|
539
|
-
expect(breakdown.shell_compression.pct_of_total).toBe(25);
|
|
540
|
-
});
|
|
541
|
-
it("returns empty record for empty events", () => {
|
|
542
|
-
const breakdown = aggregateByMechanism([]);
|
|
543
|
-
expect(Object.keys(breakdown)).toHaveLength(0);
|
|
544
|
-
});
|
|
545
|
-
});
|
|
546
|
-
// ── High-volume insert Tests ──────────────────────────────────────
|
|
547
|
-
// (Replaces the prior "JSONL rotation" test — SQLite doesn't trim rows,
|
|
548
|
-
// it indexes them.)
|
|
549
|
-
describe("high-volume writes", () => {
|
|
550
|
-
it("persists 2200 events without loss", () => {
|
|
551
|
-
const writer = new TokenFlowWriter(unerrDir, "test-session-001");
|
|
552
|
-
for (let i = 0; i < 2200; i++) {
|
|
553
|
-
writer.record(makeInput({ turn: i, tokens_saved: i }));
|
|
554
|
-
}
|
|
555
|
-
const events = readTokenFlowEvents(unerrDir);
|
|
556
|
-
expect(events).toHaveLength(2200);
|
|
557
|
-
// Ordered by id ASC — the last inserted event is the last in the array.
|
|
558
|
-
expect(events[events.length - 1].turn).toBe(2199);
|
|
559
|
-
});
|
|
560
|
-
});
|
|
561
|
-
// ── Cross-Process Tests ───────────────────────────────────────────
|
|
562
|
-
describe("cross-process coordination", () => {
|
|
563
|
-
it("events from different PIDs coexist in same store", () => {
|
|
564
|
-
// Simulate events written by two child processes by inserting via
|
|
565
|
-
// the metrics-store directly with custom pid values.
|
|
566
|
-
const store = openMetricsStore(unerrDir);
|
|
567
|
-
store.insertTokenFlow({
|
|
568
|
-
ts: Date.parse("2026-01-01T00:00:00Z"),
|
|
569
|
-
ts_iso: "2026-01-01T00:00:00Z",
|
|
570
|
-
session_id: "s1",
|
|
571
|
-
pid: 12345,
|
|
572
|
-
turn: 1,
|
|
573
|
-
mechanism: "graph_query",
|
|
574
|
-
tool: "get_callers",
|
|
575
|
-
tokens_without: 5000,
|
|
576
|
-
tokens_with: 1800,
|
|
577
|
-
tokens_saved: 3200,
|
|
578
|
-
detail: null,
|
|
579
|
-
});
|
|
580
|
-
store.insertTokenFlow({
|
|
581
|
-
ts: Date.parse("2026-01-01T00:00:01Z"),
|
|
582
|
-
ts_iso: "2026-01-01T00:00:01Z",
|
|
583
|
-
session_id: "s1",
|
|
584
|
-
pid: 54321,
|
|
585
|
-
turn: 0,
|
|
586
|
-
mechanism: "shell_compression",
|
|
587
|
-
tool: null,
|
|
588
|
-
tokens_without: 3400,
|
|
589
|
-
tokens_with: 800,
|
|
590
|
-
tokens_saved: 2600,
|
|
591
|
-
detail: null,
|
|
592
|
-
});
|
|
593
|
-
const events = readTokenFlowEvents(unerrDir, { session_id: "s1" });
|
|
594
|
-
expect(events).toHaveLength(2);
|
|
595
|
-
const summary = aggregateSession(events, "s1");
|
|
596
|
-
expect(summary.total_tokens_saved).toBe(5800);
|
|
597
|
-
expect(summary.by_mechanism.graph_query.tokens_saved).toBe(3200);
|
|
598
|
-
expect(summary.by_mechanism.shell_compression.tokens_saved).toBe(2600);
|
|
599
|
-
});
|
|
600
|
-
it("session_id filtering isolates concurrent sessions", () => {
|
|
601
|
-
const store = openMetricsStore(unerrDir);
|
|
602
|
-
const baseTs = Date.parse("2026-01-01T00:00:00Z");
|
|
603
|
-
const insert = (sessionId, pid, turn, without, saved, offsetSec) => {
|
|
604
|
-
store.insertTokenFlow({
|
|
605
|
-
ts: baseTs + offsetSec * 1000,
|
|
606
|
-
ts_iso: new Date(baseTs + offsetSec * 1000).toISOString(),
|
|
607
|
-
session_id: sessionId,
|
|
608
|
-
pid,
|
|
609
|
-
turn,
|
|
610
|
-
mechanism: "graph_query",
|
|
611
|
-
tool: "t",
|
|
612
|
-
tokens_without: without,
|
|
613
|
-
tokens_with: without - saved,
|
|
614
|
-
tokens_saved: saved,
|
|
615
|
-
detail: null,
|
|
616
|
-
});
|
|
617
|
-
};
|
|
618
|
-
insert("session-a", 100, 1, 100, 50, 0);
|
|
619
|
-
insert("session-b", 200, 1, 200, 100, 1);
|
|
620
|
-
insert("session-a", 100, 2, 300, 150, 2);
|
|
621
|
-
const summaryA = aggregateSession(readTokenFlowEvents(unerrDir), "session-a");
|
|
622
|
-
const summaryB = aggregateSession(readTokenFlowEvents(unerrDir), "session-b");
|
|
623
|
-
expect(summaryA.total_tokens_saved).toBe(200);
|
|
624
|
-
expect(summaryA.total_turns).toBe(2);
|
|
625
|
-
expect(summaryB.total_tokens_saved).toBe(100);
|
|
626
|
-
expect(summaryB.total_turns).toBe(1);
|
|
627
|
-
});
|
|
628
|
-
});
|
|
629
|
-
// ── All Mechanism Types ───────────────────────────────────────────
|
|
630
|
-
describe("all mechanism types", () => {
|
|
631
|
-
it("handles all 7 mechanism types", () => {
|
|
632
|
-
const writer = new TokenFlowWriter(unerrDir, "s1");
|
|
633
|
-
const mechanisms = [
|
|
634
|
-
"graph_query",
|
|
635
|
-
"session_dedup",
|
|
636
|
-
"shell_compression",
|
|
637
|
-
"format_encoding",
|
|
638
|
-
"smart_truncation",
|
|
639
|
-
"file_read",
|
|
640
|
-
"behavior_automation",
|
|
641
|
-
];
|
|
642
|
-
for (const [i, mechanism] of mechanisms.entries()) {
|
|
643
|
-
writer.record(makeInput({
|
|
644
|
-
session_id: "s1",
|
|
645
|
-
turn: i + 1,
|
|
646
|
-
mechanism,
|
|
647
|
-
tokens_saved: (i + 1) * 100,
|
|
648
|
-
tokens_without: (i + 1) * 200,
|
|
649
|
-
tokens_with: (i + 1) * 100,
|
|
650
|
-
}));
|
|
651
|
-
}
|
|
652
|
-
const summary = aggregateSession(writer.getSessionEvents(), "s1");
|
|
653
|
-
expect(Object.keys(summary.by_mechanism)).toHaveLength(7);
|
|
654
|
-
for (const mech of mechanisms) {
|
|
655
|
-
expect(summary.by_mechanism[mech]).toBeDefined();
|
|
656
|
-
}
|
|
657
|
-
});
|
|
658
|
-
});
|
|
659
|
-
// ── Performance Smoke Test ────────────────────────────────────────
|
|
660
|
-
describe("performance", () => {
|
|
661
|
-
it("record() completes in under 1ms per write", () => {
|
|
662
|
-
const writer = new TokenFlowWriter(unerrDir, "perf-test");
|
|
663
|
-
const iterations = 100;
|
|
664
|
-
const start = performance.now();
|
|
665
|
-
for (let i = 0; i < iterations; i++) {
|
|
666
|
-
writer.record(makeInput({ turn: i }));
|
|
667
|
-
}
|
|
668
|
-
const elapsed = performance.now() - start;
|
|
669
|
-
const perWrite = elapsed / iterations;
|
|
670
|
-
expect(perWrite).toBeLessThan(1);
|
|
671
|
-
});
|
|
672
|
-
it("aggregateSession handles 100 events in under 5ms", () => {
|
|
673
|
-
const events = [];
|
|
674
|
-
for (let i = 0; i < 100; i++) {
|
|
675
|
-
events.push({
|
|
676
|
-
id: i,
|
|
677
|
-
ts: new Date().toISOString(),
|
|
678
|
-
session_id: "s1",
|
|
679
|
-
pid: 1,
|
|
680
|
-
turn: Math.floor(i / 2) + 1,
|
|
681
|
-
mechanism: i % 2 === 0 ? "graph_query" : "shell_compression",
|
|
682
|
-
tool: i % 2 === 0 ? "get_callers" : null,
|
|
683
|
-
tokens_without: 1000 + i * 10,
|
|
684
|
-
tokens_with: 500 + i * 5,
|
|
685
|
-
tokens_saved: 500 + i * 5,
|
|
686
|
-
});
|
|
687
|
-
}
|
|
688
|
-
const start = performance.now();
|
|
689
|
-
const summary = aggregateSession(events, "s1");
|
|
690
|
-
const elapsed = performance.now() - start;
|
|
691
|
-
expect(elapsed).toBeLessThan(5);
|
|
692
|
-
expect(summary.total_turns).toBe(50);
|
|
693
|
-
});
|
|
694
|
-
});
|
|
695
|
-
});
|