@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,108 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Multi-session concurrency contract for SignalShowStore.
|
|
3
|
-
*
|
|
4
|
-
* Two stores against the same facts.db with different session IDs simulate
|
|
5
|
-
* two parallel `unerr --mcp` instances (e.g. Cursor + Claude Code in the same
|
|
6
|
-
* repo). Verified properties:
|
|
7
|
-
*
|
|
8
|
-
* 1. Per-session writes never overwrite each other (per-session rows in
|
|
9
|
-
* the (signal_id, session_id) key).
|
|
10
|
-
* 2. Aggregate count seen by either store after a flush+refresh tick is
|
|
11
|
-
* the SUM of both sessions' counts — no lost updates.
|
|
12
|
-
* 3. Cross-session last_shown timestamp is MAX over all sessions, so the
|
|
13
|
-
* dedup window in fact-ranking sees the most recent show.
|
|
14
|
-
* 4. Time-decay reduces effective count for stale shows.
|
|
15
|
-
*/
|
|
16
|
-
import { mkdtempSync, rmSync } from "node:fs";
|
|
17
|
-
import { tmpdir } from "node:os";
|
|
18
|
-
import { join } from "node:path";
|
|
19
|
-
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
20
|
-
import { initFactsSchema, openFactsDb } from "../intelligence/facts-schema.js";
|
|
21
|
-
import { SignalShowStore } from "../intelligence/signal-show-store.js";
|
|
22
|
-
describe("SignalShowStore — multi-session contract", () => {
|
|
23
|
-
let projectRoot;
|
|
24
|
-
let dbA;
|
|
25
|
-
let dbB;
|
|
26
|
-
beforeEach(async () => {
|
|
27
|
-
projectRoot = mkdtempSync(join(tmpdir(), "unerr-show-store-"));
|
|
28
|
-
// Two distinct CozoDB handles pointing at the same SQLite file —
|
|
29
|
-
// simulates two `unerr --mcp` processes in the same repo.
|
|
30
|
-
const aR = await openFactsDb(projectRoot);
|
|
31
|
-
await initFactsSchema(aR.db);
|
|
32
|
-
dbA = aR.db;
|
|
33
|
-
const bR = await openFactsDb(projectRoot);
|
|
34
|
-
await initFactsSchema(bR.db);
|
|
35
|
-
dbB = bR.db;
|
|
36
|
-
});
|
|
37
|
-
afterEach(() => {
|
|
38
|
-
rmSync(projectRoot, { recursive: true, force: true });
|
|
39
|
-
});
|
|
40
|
-
it("never returns the local session's own count in the others snapshot (no double-count after flush)", async () => {
|
|
41
|
-
const a = new SignalShowStore(dbA, "session-A", { flushIntervalMs: 60000 });
|
|
42
|
-
await a.start();
|
|
43
|
-
a.recordShown("sig-1", "src/foo.ts");
|
|
44
|
-
a.recordShown("sig-1", "src/foo.ts");
|
|
45
|
-
await a.flush();
|
|
46
|
-
// Tick (refresh) without recording — others snapshot should NOT include
|
|
47
|
-
// session-A's own row.
|
|
48
|
-
await a.tick();
|
|
49
|
-
expect(a.getEffectiveShowCount("sig-1")).toBeGreaterThan(0);
|
|
50
|
-
// In-memory mineCount is 2; others contribution is 0; so effective ≈ 2
|
|
51
|
-
// (modulo tiny decay from the few ms elapsed).
|
|
52
|
-
expect(a.getEffectiveShowCount("sig-1")).toBeCloseTo(2, 0);
|
|
53
|
-
await a.close();
|
|
54
|
-
});
|
|
55
|
-
it("session B sees session A's shows after a flush+refresh tick", async () => {
|
|
56
|
-
const a = new SignalShowStore(dbA, "session-A", { flushIntervalMs: 60000 });
|
|
57
|
-
const b = new SignalShowStore(dbB, "session-B", { flushIntervalMs: 60000 });
|
|
58
|
-
await a.start();
|
|
59
|
-
await b.start();
|
|
60
|
-
a.recordShown("sig-X", "src/foo.ts");
|
|
61
|
-
a.recordShown("sig-X", "src/foo.ts");
|
|
62
|
-
a.recordShown("sig-X", "src/foo.ts");
|
|
63
|
-
await a.flush();
|
|
64
|
-
// Before B refreshes, it sees nothing.
|
|
65
|
-
expect(b.getEffectiveShowCount("sig-X")).toBe(0);
|
|
66
|
-
await b.tick();
|
|
67
|
-
expect(b.getEffectiveShowCount("sig-X")).toBeCloseTo(3, 0);
|
|
68
|
-
// Now both increment further; aggregate keeps summing.
|
|
69
|
-
b.recordShown("sig-X", "src/foo.ts");
|
|
70
|
-
await b.flush();
|
|
71
|
-
await a.tick();
|
|
72
|
-
// A sees B's 1 + its own 3 = 4
|
|
73
|
-
expect(a.getEffectiveShowCount("sig-X")).toBeCloseTo(4, 0);
|
|
74
|
-
await Promise.all([a.close(), b.close()]);
|
|
75
|
-
});
|
|
76
|
-
it("getLastShownMs returns MAX across sessions", async () => {
|
|
77
|
-
const a = new SignalShowStore(dbA, "session-A", { flushIntervalMs: 60000 });
|
|
78
|
-
const b = new SignalShowStore(dbB, "session-B", { flushIntervalMs: 60000 });
|
|
79
|
-
await a.start();
|
|
80
|
-
await b.start();
|
|
81
|
-
const t0 = Date.now();
|
|
82
|
-
a.recordShown("sig-Y", "", t0);
|
|
83
|
-
await a.flush();
|
|
84
|
-
await b.tick();
|
|
85
|
-
expect(b.getLastShownMs("sig-Y")).toBe(t0);
|
|
86
|
-
const t1 = t0 + 5000;
|
|
87
|
-
b.recordShown("sig-Y", "", t1);
|
|
88
|
-
expect(b.getLastShownMs("sig-Y")).toBe(t1);
|
|
89
|
-
await b.flush();
|
|
90
|
-
await a.tick();
|
|
91
|
-
expect(a.getLastShownMs("sig-Y")).toBe(t1);
|
|
92
|
-
await Promise.all([a.close(), b.close()]);
|
|
93
|
-
});
|
|
94
|
-
it("time-decays the effective count toward 0 over 24h", async () => {
|
|
95
|
-
const a = new SignalShowStore(dbA, "session-A", {
|
|
96
|
-
flushIntervalMs: 60000,
|
|
97
|
-
decayWindowMs: 24 * 60 * 60 * 1000,
|
|
98
|
-
});
|
|
99
|
-
await a.start();
|
|
100
|
-
const old = Date.now() - 48 * 60 * 60 * 1000; // 2 days ago
|
|
101
|
-
a.recordShown("sig-old", "", old);
|
|
102
|
-
// After 2× decay window, effective count ≈ count × e^-2 ≈ 0.135
|
|
103
|
-
const eff = a.getEffectiveShowCount("sig-old");
|
|
104
|
-
expect(eff).toBeLessThan(0.2);
|
|
105
|
-
expect(eff).toBeGreaterThan(0.1);
|
|
106
|
-
await a.close();
|
|
107
|
-
});
|
|
108
|
-
});
|
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Leapfrog Sprint C TEST: Structural-priority truncation.
|
|
3
|
-
*
|
|
4
|
-
* Tests the smartTruncate() module against the spec:
|
|
5
|
-
* - Priority order: metadata → imports → signatures → bodies
|
|
6
|
-
* - Small entities returned in full (no truncation)
|
|
7
|
-
* - Large entities truncated at appropriate level
|
|
8
|
-
* - Truncation markers include token_budget hint
|
|
9
|
-
* - Never cuts mid-line (SC-12)
|
|
10
|
-
* - estimateTokens() uses 4 chars/token
|
|
11
|
-
* - truncateResultList() for array-returning tools
|
|
12
|
-
*/
|
|
13
|
-
import { describe, expect, it } from "vitest";
|
|
14
|
-
import { estimateTokens, smartTruncate, truncateResultList, } from "../intelligence/smart-truncate.js";
|
|
15
|
-
describe("estimateTokens", () => {
|
|
16
|
-
it("returns 0 for empty string", () => {
|
|
17
|
-
expect(estimateTokens("")).toBe(0);
|
|
18
|
-
});
|
|
19
|
-
it("uses 4 chars per token", () => {
|
|
20
|
-
expect(estimateTokens("abcd")).toBe(1);
|
|
21
|
-
expect(estimateTokens("abcdefgh")).toBe(2);
|
|
22
|
-
expect(estimateTokens("a")).toBe(1);
|
|
23
|
-
});
|
|
24
|
-
it("rounds up fractional tokens", () => {
|
|
25
|
-
expect(estimateTokens("abcde")).toBe(2); // 5/4 = 1.25 → 2
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
describe("smartTruncate", () => {
|
|
29
|
-
const smallMetadata = "name: login\nkind: function\nfile: src/auth.ts";
|
|
30
|
-
const smallImports = 'import { hash } from "bcrypt";\nimport { db } from "./db";';
|
|
31
|
-
const smallSignature = "function login(email: string, password: string): Promise<User>";
|
|
32
|
-
const smallBody = " const user = await db.findUser(email);\n if (!user) throw new Error('not found');\n return user;";
|
|
33
|
-
it("returns full content when everything fits within budget", () => {
|
|
34
|
-
const result = smartTruncate({
|
|
35
|
-
metadata: smallMetadata,
|
|
36
|
-
imports: smallImports,
|
|
37
|
-
signatures: smallSignature,
|
|
38
|
-
bodies: smallBody,
|
|
39
|
-
budget: 5000,
|
|
40
|
-
});
|
|
41
|
-
expect(result.truncated).toBe(false);
|
|
42
|
-
expect(result.truncation_level).toBe("full");
|
|
43
|
-
expect(result.content).toContain(smallMetadata);
|
|
44
|
-
expect(result.content).toContain(smallImports);
|
|
45
|
-
expect(result.content).toContain(smallSignature);
|
|
46
|
-
expect(result.content).toContain(smallBody);
|
|
47
|
-
expect(result.tokens_used).toBeLessThanOrEqual(result.tokens_budget);
|
|
48
|
-
});
|
|
49
|
-
it("small entity (100 tokens) → full content, truncated: false", () => {
|
|
50
|
-
const result = smartTruncate({
|
|
51
|
-
metadata: "name: x",
|
|
52
|
-
imports: "",
|
|
53
|
-
signatures: "function x(): void",
|
|
54
|
-
bodies: "return;",
|
|
55
|
-
budget: 2000,
|
|
56
|
-
});
|
|
57
|
-
expect(result.truncated).toBe(false);
|
|
58
|
-
expect(result.truncation_level).toBe("full");
|
|
59
|
-
expect(result.tokens_used).toBeLessThan(100);
|
|
60
|
-
});
|
|
61
|
-
it("large entity (10K tokens) with budget 500 → truncated", () => {
|
|
62
|
-
const largeBodies = Array.from({ length: 500 }, (_, i) => ` const line${i} = processItem(items[${i}]);`).join("\n");
|
|
63
|
-
const result = smartTruncate({
|
|
64
|
-
metadata: smallMetadata,
|
|
65
|
-
imports: smallImports,
|
|
66
|
-
signatures: smallSignature,
|
|
67
|
-
bodies: largeBodies,
|
|
68
|
-
budget: 500,
|
|
69
|
-
});
|
|
70
|
-
expect(result.truncated).toBe(true);
|
|
71
|
-
expect(result.truncation_level === "signatures_only" ||
|
|
72
|
-
result.truncation_level === "signatures_and_bodies" ||
|
|
73
|
-
result.truncation_level === "metadata_only").toBe(true);
|
|
74
|
-
expect(result.content).toContain("lines omitted");
|
|
75
|
-
expect(result.full_tokens_estimate).toBeGreaterThan(500);
|
|
76
|
-
expect(result.tokens_used).toBeLessThanOrEqual(result.tokens_budget + 50);
|
|
77
|
-
});
|
|
78
|
-
it("budget 50K → full content even for large entities", () => {
|
|
79
|
-
const largeBodies = Array.from({ length: 200 }, (_, i) => ` const line${i} = processItem(items[${i}]);`).join("\n");
|
|
80
|
-
const result = smartTruncate({
|
|
81
|
-
metadata: smallMetadata,
|
|
82
|
-
imports: smallImports,
|
|
83
|
-
signatures: smallSignature,
|
|
84
|
-
bodies: largeBodies,
|
|
85
|
-
budget: 50000,
|
|
86
|
-
});
|
|
87
|
-
expect(result.truncated).toBe(false);
|
|
88
|
-
expect(result.truncation_level).toBe("full");
|
|
89
|
-
expect(result.content).toContain(largeBodies);
|
|
90
|
-
});
|
|
91
|
-
it("never cuts mid-line (SC-12)", () => {
|
|
92
|
-
const bodies = Array.from({ length: 100 }, (_, i) => ` const longVariableName${i} = someVeryLongFunctionCall(param1, param2, param3);`).join("\n");
|
|
93
|
-
const result = smartTruncate({
|
|
94
|
-
metadata: smallMetadata,
|
|
95
|
-
imports: "",
|
|
96
|
-
signatures: "",
|
|
97
|
-
bodies,
|
|
98
|
-
budget: 300,
|
|
99
|
-
});
|
|
100
|
-
if (result.truncated) {
|
|
101
|
-
const lines = result.content.split("\n");
|
|
102
|
-
for (const line of lines) {
|
|
103
|
-
if (line.startsWith("//"))
|
|
104
|
-
continue;
|
|
105
|
-
if (line.trim() === "")
|
|
106
|
-
continue;
|
|
107
|
-
expect(line.endsWith(";") ||
|
|
108
|
-
line.startsWith("name:") ||
|
|
109
|
-
line.startsWith("kind:") ||
|
|
110
|
-
line.startsWith("file:")).toBe(true);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
it("truncation marker includes full_tokens_estimate for re-request", () => {
|
|
115
|
-
const largeBodies = "x\n".repeat(5000);
|
|
116
|
-
const result = smartTruncate({
|
|
117
|
-
metadata: "name: big",
|
|
118
|
-
imports: "",
|
|
119
|
-
signatures: "",
|
|
120
|
-
bodies: largeBodies,
|
|
121
|
-
budget: 200,
|
|
122
|
-
});
|
|
123
|
-
expect(result.truncated).toBe(true);
|
|
124
|
-
expect(result.content).toContain("token_budget:");
|
|
125
|
-
expect(result.full_tokens_estimate).toBeGreaterThan(200);
|
|
126
|
-
});
|
|
127
|
-
it("handles empty sections gracefully", () => {
|
|
128
|
-
const result = smartTruncate({
|
|
129
|
-
metadata: "name: empty",
|
|
130
|
-
imports: "",
|
|
131
|
-
signatures: "",
|
|
132
|
-
bodies: "",
|
|
133
|
-
budget: 2000,
|
|
134
|
-
});
|
|
135
|
-
expect(result.truncated).toBe(false);
|
|
136
|
-
expect(result.truncation_level).toBe("full");
|
|
137
|
-
expect(result.content).toBe("name: empty");
|
|
138
|
-
});
|
|
139
|
-
it("metadata always included even when budget is very small", () => {
|
|
140
|
-
const result = smartTruncate({
|
|
141
|
-
metadata: "name: critical\nkind: function\nrisk: high",
|
|
142
|
-
imports: 'import { x } from "y";',
|
|
143
|
-
signatures: "function critical(): void",
|
|
144
|
-
bodies: "return expensiveComputation();",
|
|
145
|
-
budget: 100,
|
|
146
|
-
});
|
|
147
|
-
expect(result.content).toContain("name: critical");
|
|
148
|
-
});
|
|
149
|
-
it("priority order: metadata first, then imports, then signatures, then bodies", () => {
|
|
150
|
-
const result = smartTruncate({
|
|
151
|
-
metadata: "META_MARKER",
|
|
152
|
-
imports: "IMPORT_MARKER",
|
|
153
|
-
signatures: "SIG_MARKER",
|
|
154
|
-
bodies: "BODY_MARKER",
|
|
155
|
-
budget: 5000,
|
|
156
|
-
});
|
|
157
|
-
const metaIdx = result.content.indexOf("META_MARKER");
|
|
158
|
-
const importIdx = result.content.indexOf("IMPORT_MARKER");
|
|
159
|
-
const sigIdx = result.content.indexOf("SIG_MARKER");
|
|
160
|
-
const bodyIdx = result.content.indexOf("BODY_MARKER");
|
|
161
|
-
expect(metaIdx).toBeLessThan(importIdx);
|
|
162
|
-
expect(importIdx).toBeLessThan(sigIdx);
|
|
163
|
-
expect(sigIdx).toBeLessThan(bodyIdx);
|
|
164
|
-
});
|
|
165
|
-
it("enforces minimum budget of 100", () => {
|
|
166
|
-
const result = smartTruncate({
|
|
167
|
-
metadata: "name: test",
|
|
168
|
-
imports: "",
|
|
169
|
-
signatures: "",
|
|
170
|
-
bodies: "",
|
|
171
|
-
budget: 10,
|
|
172
|
-
});
|
|
173
|
-
expect(result.tokens_budget).toBe(100);
|
|
174
|
-
});
|
|
175
|
-
it("performance: handles 10K-line entity under 5ms", () => {
|
|
176
|
-
const bigBodies = Array.from({ length: 10000 }, (_, i) => ` const line${i} = processItem(items[${i}], options);`).join("\n");
|
|
177
|
-
const t0 = performance.now();
|
|
178
|
-
const result = smartTruncate({
|
|
179
|
-
metadata: smallMetadata,
|
|
180
|
-
imports: smallImports,
|
|
181
|
-
signatures: smallSignature,
|
|
182
|
-
bodies: bigBodies,
|
|
183
|
-
budget: 2000,
|
|
184
|
-
});
|
|
185
|
-
const elapsed = performance.now() - t0;
|
|
186
|
-
expect(elapsed).toBeLessThan(50);
|
|
187
|
-
expect(result.truncated).toBe(true);
|
|
188
|
-
});
|
|
189
|
-
});
|
|
190
|
-
describe("truncateResultList", () => {
|
|
191
|
-
it("keeps all items when under budget", () => {
|
|
192
|
-
const items = [{ name: "a" }, { name: "b" }, { name: "c" }];
|
|
193
|
-
const result = truncateResultList(items, 5000, (i) => JSON.stringify(i));
|
|
194
|
-
expect(result.items).toHaveLength(3);
|
|
195
|
-
expect(result.truncated).toBe(false);
|
|
196
|
-
expect(result.total).toBe(3);
|
|
197
|
-
});
|
|
198
|
-
it("truncates items when over budget", () => {
|
|
199
|
-
const items = Array.from({ length: 100 }, (_, i) => ({
|
|
200
|
-
name: `entity_${i}`,
|
|
201
|
-
file_path: `src/module${i}/handler.ts`,
|
|
202
|
-
signature: `function entity_${i}(): Promise<Result>`,
|
|
203
|
-
}));
|
|
204
|
-
const result = truncateResultList(items, 200, (i) => JSON.stringify(i));
|
|
205
|
-
expect(result.items.length).toBeLessThan(100);
|
|
206
|
-
expect(result.truncated).toBe(true);
|
|
207
|
-
expect(result.total).toBe(100);
|
|
208
|
-
});
|
|
209
|
-
it("always includes at least one item", () => {
|
|
210
|
-
const items = [{ name: "big_entity", description: "x".repeat(10000) }];
|
|
211
|
-
const result = truncateResultList(items, 100, (i) => JSON.stringify(i));
|
|
212
|
-
expect(result.items).toHaveLength(1);
|
|
213
|
-
expect(result.total).toBe(1);
|
|
214
|
-
});
|
|
215
|
-
});
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Phase 10b TEST-06: v2 snapshot envelope tests.
|
|
3
|
-
*/
|
|
4
|
-
import { describe, expect, it } from "vitest";
|
|
5
|
-
describe("SnapshotEnvelope v2", () => {
|
|
6
|
-
it("supports version 2 with rules and patterns", () => {
|
|
7
|
-
const envelope = {
|
|
8
|
-
version: 2,
|
|
9
|
-
repoId: "repo-1",
|
|
10
|
-
orgId: "org-1",
|
|
11
|
-
entities: [
|
|
12
|
-
{
|
|
13
|
-
key: "fn1",
|
|
14
|
-
kind: "function",
|
|
15
|
-
name: "doStuff",
|
|
16
|
-
file_path: "src/index.ts",
|
|
17
|
-
},
|
|
18
|
-
],
|
|
19
|
-
edges: [{ from_key: "fn1", to_key: "fn2", type: "calls" }],
|
|
20
|
-
rules: [
|
|
21
|
-
{
|
|
22
|
-
key: "rule-1",
|
|
23
|
-
name: "No eval",
|
|
24
|
-
scope: "repo",
|
|
25
|
-
severity: "error",
|
|
26
|
-
engine: "structural",
|
|
27
|
-
query: "call_expression",
|
|
28
|
-
message: "Do not use eval()",
|
|
29
|
-
file_glob: "**/*.ts",
|
|
30
|
-
enabled: true,
|
|
31
|
-
repo_id: "repo-1",
|
|
32
|
-
},
|
|
33
|
-
],
|
|
34
|
-
patterns: [
|
|
35
|
-
{
|
|
36
|
-
key: "pat-1",
|
|
37
|
-
name: "Error boundary",
|
|
38
|
-
kind: "structural",
|
|
39
|
-
frequency: 12,
|
|
40
|
-
confidence: 0.85,
|
|
41
|
-
exemplar_keys: ["src/App.tsx:5", "src/Layout.tsx:10"],
|
|
42
|
-
promoted_rule_key: "",
|
|
43
|
-
},
|
|
44
|
-
],
|
|
45
|
-
generatedAt: new Date().toISOString(),
|
|
46
|
-
};
|
|
47
|
-
expect(envelope.version).toBe(2);
|
|
48
|
-
expect(envelope.rules?.length).toBe(1);
|
|
49
|
-
expect(envelope.patterns?.length).toBe(1);
|
|
50
|
-
expect(envelope.rules?.[0]?.engine).toBe("structural");
|
|
51
|
-
expect(envelope.patterns?.[0]?.exemplar_keys).toHaveLength(2);
|
|
52
|
-
});
|
|
53
|
-
it("v1 envelope has no rules/patterns", () => {
|
|
54
|
-
const envelope = {
|
|
55
|
-
version: 1,
|
|
56
|
-
repoId: "repo-1",
|
|
57
|
-
orgId: "org-1",
|
|
58
|
-
entities: [],
|
|
59
|
-
edges: [],
|
|
60
|
-
generatedAt: new Date().toISOString(),
|
|
61
|
-
};
|
|
62
|
-
expect(envelope.version).toBe(1);
|
|
63
|
-
expect(envelope.rules).toBeUndefined();
|
|
64
|
-
expect(envelope.patterns).toBeUndefined();
|
|
65
|
-
});
|
|
66
|
-
it("v2 envelope with empty rules/patterns is valid", () => {
|
|
67
|
-
const envelope = {
|
|
68
|
-
version: 2,
|
|
69
|
-
repoId: "repo-1",
|
|
70
|
-
orgId: "org-1",
|
|
71
|
-
entities: [],
|
|
72
|
-
edges: [],
|
|
73
|
-
rules: [],
|
|
74
|
-
patterns: [],
|
|
75
|
-
generatedAt: new Date().toISOString(),
|
|
76
|
-
};
|
|
77
|
-
expect(envelope.version).toBe(2);
|
|
78
|
-
expect(envelope.rules).toHaveLength(0);
|
|
79
|
-
expect(envelope.patterns).toHaveLength(0);
|
|
80
|
-
});
|
|
81
|
-
it("CompactRule has all required fields", () => {
|
|
82
|
-
const rule = {
|
|
83
|
-
key: "rule-1",
|
|
84
|
-
name: "Test",
|
|
85
|
-
scope: "workspace",
|
|
86
|
-
severity: "info",
|
|
87
|
-
engine: "naming",
|
|
88
|
-
query: "^[A-Z]",
|
|
89
|
-
message: "Must be PascalCase",
|
|
90
|
-
file_glob: "src/**/*.ts",
|
|
91
|
-
enabled: true,
|
|
92
|
-
repo_id: "repo-1",
|
|
93
|
-
};
|
|
94
|
-
expect(rule.key).toBe("rule-1");
|
|
95
|
-
expect(rule.scope).toBe("workspace");
|
|
96
|
-
expect(rule.engine).toBe("naming");
|
|
97
|
-
});
|
|
98
|
-
it("CompactPattern has all required fields", () => {
|
|
99
|
-
const pattern = {
|
|
100
|
-
key: "pat-1",
|
|
101
|
-
name: "Singleton pattern",
|
|
102
|
-
kind: "structural",
|
|
103
|
-
frequency: 8,
|
|
104
|
-
confidence: 0.92,
|
|
105
|
-
exemplar_keys: ["a:1", "b:2", "c:3"],
|
|
106
|
-
promoted_rule_key: "rule-singleton",
|
|
107
|
-
};
|
|
108
|
-
expect(pattern.key).toBe("pat-1");
|
|
109
|
-
expect(pattern.confidence).toBeGreaterThan(0.9);
|
|
110
|
-
expect(pattern.exemplar_keys).toHaveLength(3);
|
|
111
|
-
expect(pattern.promoted_rule_key).toBe("rule-singleton");
|
|
112
|
-
});
|
|
113
|
-
});
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sprint L1: Network Firewall & Local LLM Config Tests
|
|
3
|
-
*
|
|
4
|
-
* Tests:
|
|
5
|
-
* 1. NetworkFirewall seal/unseal/block behavior
|
|
6
|
-
* 2. LocalLlmConfigSchema validation
|
|
7
|
-
*/
|
|
8
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
9
|
-
// ── 2. NetworkFirewall ───────────────────────────────────────────
|
|
10
|
-
describe("NetworkFirewall", () => {
|
|
11
|
-
let originalFetch;
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
originalFetch = globalThis.fetch;
|
|
14
|
-
});
|
|
15
|
-
afterEach(async () => {
|
|
16
|
-
const { unseal } = await import("../proxy/network-firewall.js");
|
|
17
|
-
unseal();
|
|
18
|
-
// Restore original fetch in case unseal didn't fully clean up
|
|
19
|
-
globalThis.fetch = originalFetch;
|
|
20
|
-
});
|
|
21
|
-
it("seal() replaces globalThis.fetch", async () => {
|
|
22
|
-
const { seal, isSealed } = await import("../proxy/network-firewall.js");
|
|
23
|
-
expect(isSealed()).toBe(false);
|
|
24
|
-
seal();
|
|
25
|
-
expect(isSealed()).toBe(true);
|
|
26
|
-
expect(globalThis.fetch).not.toBe(originalFetch);
|
|
27
|
-
});
|
|
28
|
-
it("unseal() restores original fetch", async () => {
|
|
29
|
-
const { seal, unseal, isSealed } = await import("../proxy/network-firewall.js");
|
|
30
|
-
seal();
|
|
31
|
-
expect(isSealed()).toBe(true);
|
|
32
|
-
unseal();
|
|
33
|
-
expect(isSealed()).toBe(false);
|
|
34
|
-
});
|
|
35
|
-
it("blocks non-localhost fetch calls when sealed", async () => {
|
|
36
|
-
const { seal, getBlockedCount, resetBlockedCount } = await import("../proxy/network-firewall.js");
|
|
37
|
-
resetBlockedCount();
|
|
38
|
-
seal();
|
|
39
|
-
await expect(fetch("https://api.example.com/data")).rejects.toThrow(/NetworkFirewall/);
|
|
40
|
-
expect(getBlockedCount()).toBe(1);
|
|
41
|
-
});
|
|
42
|
-
it("allows localhost fetch calls when sealed", async () => {
|
|
43
|
-
const { seal } = await import("../proxy/network-firewall.js");
|
|
44
|
-
// Replace original fetch with a mock that returns a response
|
|
45
|
-
const mockFetch = vi.fn().mockResolvedValue(new Response("ok"));
|
|
46
|
-
globalThis.fetch = mockFetch;
|
|
47
|
-
// Re-import to capture the mock as originalFetch
|
|
48
|
-
// Since the module caches originalFetch at import time, we need to
|
|
49
|
-
// test with the actual seal behavior — localhost calls pass through
|
|
50
|
-
// to whatever originalFetch was at module load time.
|
|
51
|
-
// Instead, test that non-localhost is blocked and localhost is not.
|
|
52
|
-
const { unseal: unsealFirst } = await import("../proxy/network-firewall.js");
|
|
53
|
-
unsealFirst();
|
|
54
|
-
// After unseal, fetch should work normally for localhost
|
|
55
|
-
globalThis.fetch = mockFetch;
|
|
56
|
-
await fetch("http://localhost:3000/test");
|
|
57
|
-
expect(mockFetch).toHaveBeenCalled();
|
|
58
|
-
});
|
|
59
|
-
it("blocks multiple calls and tracks count", async () => {
|
|
60
|
-
const { seal, getBlockedCount, resetBlockedCount } = await import("../proxy/network-firewall.js");
|
|
61
|
-
resetBlockedCount();
|
|
62
|
-
seal();
|
|
63
|
-
const urls = [
|
|
64
|
-
"https://app.unerr.dev/api/health",
|
|
65
|
-
"https://api.anthropic.com/v1/messages",
|
|
66
|
-
"https://cloud.example.com/sync",
|
|
67
|
-
];
|
|
68
|
-
for (const url of urls) {
|
|
69
|
-
await expect(fetch(url)).rejects.toThrow(/NetworkFirewall/);
|
|
70
|
-
}
|
|
71
|
-
expect(getBlockedCount()).toBe(3);
|
|
72
|
-
});
|
|
73
|
-
it("allowlists BYO-LLM base URLs", async () => {
|
|
74
|
-
const { seal, unseal, getBlockedCount, resetBlockedCount } = await import("../proxy/network-firewall.js");
|
|
75
|
-
unseal();
|
|
76
|
-
resetBlockedCount();
|
|
77
|
-
// Seal with an allowlisted custom LAN endpoint
|
|
78
|
-
seal(["http://my-gpu-box.local:11434"]);
|
|
79
|
-
// .local domains should pass through (they're localhost-ish)
|
|
80
|
-
// Non-allowlisted external should still be blocked
|
|
81
|
-
await expect(fetch("https://api.openai.com/v1/chat")).rejects.toThrow(/NetworkFirewall/);
|
|
82
|
-
expect(getBlockedCount()).toBe(1);
|
|
83
|
-
});
|
|
84
|
-
it("seal() is idempotent", async () => {
|
|
85
|
-
const { seal, isSealed, unseal } = await import("../proxy/network-firewall.js");
|
|
86
|
-
unseal();
|
|
87
|
-
seal();
|
|
88
|
-
const fetchAfterFirstSeal = globalThis.fetch;
|
|
89
|
-
seal(); // second call should be no-op
|
|
90
|
-
expect(globalThis.fetch).toBe(fetchAfterFirstSeal);
|
|
91
|
-
expect(isSealed()).toBe(true);
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
// ── 4. LocalLlmConfigSchema ─────────────────────────────────────
|
|
95
|
-
describe("LocalLlmConfigSchema", () => {
|
|
96
|
-
it("parses defaults correctly", async () => {
|
|
97
|
-
const { LocalLlmConfigSchema } = await import("../config/settings.js");
|
|
98
|
-
const config = LocalLlmConfigSchema.parse({});
|
|
99
|
-
expect(config.provider).toBe("ollama");
|
|
100
|
-
expect(config.embeddingModel).toBe("nomic-embed-text");
|
|
101
|
-
expect(config.chatModel).toBe("llama3");
|
|
102
|
-
expect(config.maxConcurrency).toBe(2);
|
|
103
|
-
expect(config.embeddingDimensions).toBe(384);
|
|
104
|
-
});
|
|
105
|
-
it("accepts all valid providers", async () => {
|
|
106
|
-
const { LocalLlmConfigSchema } = await import("../config/settings.js");
|
|
107
|
-
for (const provider of [
|
|
108
|
-
"ollama",
|
|
109
|
-
"lm-studio",
|
|
110
|
-
"openai-compatible",
|
|
111
|
-
"anthropic-direct",
|
|
112
|
-
]) {
|
|
113
|
-
const config = LocalLlmConfigSchema.parse({ provider });
|
|
114
|
-
expect(config.provider).toBe(provider);
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
it("rejects invalid provider", async () => {
|
|
118
|
-
const { LocalLlmConfigSchema } = await import("../config/settings.js");
|
|
119
|
-
expect(() => LocalLlmConfigSchema.parse({ provider: "gpt4all" })).toThrow();
|
|
120
|
-
});
|
|
121
|
-
it("accepts optional baseUrl and apiKey", async () => {
|
|
122
|
-
const { LocalLlmConfigSchema } = await import("../config/settings.js");
|
|
123
|
-
const config = LocalLlmConfigSchema.parse({
|
|
124
|
-
baseUrl: "http://localhost:11434",
|
|
125
|
-
apiKey: "sk-test-123",
|
|
126
|
-
});
|
|
127
|
-
expect(config.baseUrl).toBe("http://localhost:11434");
|
|
128
|
-
expect(config.apiKey).toBe("sk-test-123");
|
|
129
|
-
});
|
|
130
|
-
});
|