@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.
Files changed (218) hide show
  1. package/README.md +70 -194
  2. package/dist/cli.js +39149 -36991
  3. package/package.json +9 -2
  4. package/dist/__tests__/architecture-guard.test.js +0 -122
  5. package/dist/__tests__/arg-validator.test.js +0 -205
  6. package/dist/__tests__/ast-extractor.test.js +0 -203
  7. package/dist/__tests__/auto-bootstrap.test.js +0 -280
  8. package/dist/__tests__/background-indexer.test.js +0 -228
  9. package/dist/__tests__/blast-radius-engine.test.js +0 -200
  10. package/dist/__tests__/bridge-isolation.test.js +0 -37
  11. package/dist/__tests__/budget-enforcer.test.js +0 -53
  12. package/dist/__tests__/cfg-test-detection-perf.test.js +0 -82
  13. package/dist/__tests__/change-narrative.test.js +0 -190
  14. package/dist/__tests__/check-commit.test.js +0 -258
  15. package/dist/__tests__/checksum.test.js +0 -34
  16. package/dist/__tests__/commit-watcher.test.js +0 -154
  17. package/dist/__tests__/community-detection.test.js +0 -179
  18. package/dist/__tests__/community-tools.test.js +0 -299
  19. package/dist/__tests__/components.test.js +0 -449
  20. package/dist/__tests__/compression-log.test.js +0 -174
  21. package/dist/__tests__/compression-quality-monitor.test.js +0 -40
  22. package/dist/__tests__/config-healer.test.js +0 -165
  23. package/dist/__tests__/context-ledger.test.js +0 -58
  24. package/dist/__tests__/convention-detector.test.js +0 -99
  25. package/dist/__tests__/convention-learner.test.js +0 -86
  26. package/dist/__tests__/correction-detector.test.js +0 -330
  27. package/dist/__tests__/daemon-autostart-install.test.js +0 -283
  28. package/dist/__tests__/daemon-bridge.test.js +0 -222
  29. package/dist/__tests__/daemon-dashboard.test.js +0 -202
  30. package/dist/__tests__/daemon-registry.test.js +0 -240
  31. package/dist/__tests__/daemon-supervisor.test.js +0 -318
  32. package/dist/__tests__/daemon-version-check.test.js +0 -275
  33. package/dist/__tests__/decision-point-detector.test.js +0 -98
  34. package/dist/__tests__/deep-link.test.js +0 -143
  35. package/dist/__tests__/disallowed-tools.test.js +0 -115
  36. package/dist/__tests__/drift-tracker.test.js +0 -582
  37. package/dist/__tests__/durability-scorer.test.js +0 -152
  38. package/dist/__tests__/efficiency-tracker.test.js +0 -65
  39. package/dist/__tests__/enrich.test.js +0 -144
  40. package/dist/__tests__/entity-rewind.test.js +0 -248
  41. package/dist/__tests__/ephemeral.test.js +0 -111
  42. package/dist/__tests__/exploration-cost.test.js +0 -93
  43. package/dist/__tests__/fact-generator.test.js +0 -197
  44. package/dist/__tests__/file-l0-graph.test.js +0 -244
  45. package/dist/__tests__/file-logger.test.js +0 -82
  46. package/dist/__tests__/file-outline.test.js +0 -141
  47. package/dist/__tests__/file-read-protocol.test.js +0 -188
  48. package/dist/__tests__/format-encoder.test.js +0 -233
  49. package/dist/__tests__/git-attribution.test.js +0 -259
  50. package/dist/__tests__/graph-temporal-joiner.test.js +0 -219
  51. package/dist/__tests__/health-grade-enhanced.test.js +0 -138
  52. package/dist/__tests__/health-map-data.test.js +0 -173
  53. package/dist/__tests__/helpers/mcp-harness.js +0 -45
  54. package/dist/__tests__/helpers/mcp-harness.test.js +0 -68
  55. package/dist/__tests__/hook-dedup.test.js +0 -112
  56. package/dist/__tests__/hook-runner.test.js +0 -253
  57. package/dist/__tests__/indexer-cfg.test.js +0 -185
  58. package/dist/__tests__/indexer-cross-file.test.js +0 -172
  59. package/dist/__tests__/indexer-extraction.test.js +0 -245
  60. package/dist/__tests__/indexer-incremental.test.js +0 -232
  61. package/dist/__tests__/indexer-language-expansion.test.js +0 -165
  62. package/dist/__tests__/init-push.test.js +0 -131
  63. package/dist/__tests__/instruction-writer.test.js +0 -179
  64. package/dist/__tests__/intelligence-integration.test.js +0 -217
  65. package/dist/__tests__/intent-correlator.test.js +0 -175
  66. package/dist/__tests__/intent-detector.test.js +0 -235
  67. package/dist/__tests__/intent-encoder.test.js +0 -167
  68. package/dist/__tests__/java-build-tool-detection.test.js +0 -174
  69. package/dist/__tests__/layer3-sprint-q.test.js +0 -160
  70. package/dist/__tests__/layer3-sprint-r.test.js +0 -91
  71. package/dist/__tests__/layer3-sprint-s.test.js +0 -183
  72. package/dist/__tests__/layer3-sprint-t.test.js +0 -201
  73. package/dist/__tests__/layer3-sprint-u.test.js +0 -174
  74. package/dist/__tests__/layer4-sprint-ba2.test.js +0 -354
  75. package/dist/__tests__/layer4-sprint-ba4.test.js +0 -84
  76. package/dist/__tests__/layer4-sprint-vs.test.js +0 -105
  77. package/dist/__tests__/ledger-chains.test.js +0 -162
  78. package/dist/__tests__/lifecycle-machine.test.js +0 -226
  79. package/dist/__tests__/local-chat-provider.test.js +0 -170
  80. package/dist/__tests__/local-convention-detector.test.js +0 -308
  81. package/dist/__tests__/local-embeddings.test.js +0 -422
  82. package/dist/__tests__/local-graph.test.js +0 -540
  83. package/dist/__tests__/local-indexer.test.js +0 -228
  84. package/dist/__tests__/local-intelligence-l3.test.js +0 -332
  85. package/dist/__tests__/local-llm.test.js +0 -253
  86. package/dist/__tests__/local-mode-offline.test.js +0 -187
  87. package/dist/__tests__/local-mode-stats.test.js +0 -273
  88. package/dist/__tests__/local-mode-tui.test.js +0 -343
  89. package/dist/__tests__/local-parse.test.js +0 -199
  90. package/dist/__tests__/log-tailer.test.js +0 -208
  91. package/dist/__tests__/loop-breaker.test.js +0 -276
  92. package/dist/__tests__/loop-miner.test.js +0 -226
  93. package/dist/__tests__/mcp-config.test.js +0 -126
  94. package/dist/__tests__/mcp-content-json.test.js +0 -10
  95. package/dist/__tests__/mcp-envelope.test.js +0 -124
  96. package/dist/__tests__/metrics-store.test.js +0 -223
  97. package/dist/__tests__/native-watcher.test.js +0 -191
  98. package/dist/__tests__/navigation-hooks-agent-aware.test.js +0 -145
  99. package/dist/__tests__/negative-knowledge.test.js +0 -116
  100. package/dist/__tests__/network-boundary.test.js +0 -190
  101. package/dist/__tests__/network-firewall.test.js +0 -112
  102. package/dist/__tests__/nudge-invariants.test.js +0 -160
  103. package/dist/__tests__/nudge-v2.test.js +0 -225
  104. package/dist/__tests__/offline-rewind.test.js +0 -251
  105. package/dist/__tests__/open-threads.test.js +0 -89
  106. package/dist/__tests__/output-compressor.test.js +0 -93
  107. package/dist/__tests__/pending-violations.test.js +0 -112
  108. package/dist/__tests__/persistence-effectiveness.test.js +0 -143
  109. package/dist/__tests__/provider-factory.test.js +0 -42
  110. package/dist/__tests__/providers.test.js +0 -24
  111. package/dist/__tests__/proxy.test.js +0 -314
  112. package/dist/__tests__/query-router.test.js +0 -1018
  113. package/dist/__tests__/reasoning-quality-route.test.js +0 -138
  114. package/dist/__tests__/redactor.test.js +0 -120
  115. package/dist/__tests__/resource-monitor.test.js +0 -57
  116. package/dist/__tests__/response-envelope.test.js +0 -100
  117. package/dist/__tests__/risk-classifier.test.js +0 -101
  118. package/dist/__tests__/risk-signal-scope.test.js +0 -75
  119. package/dist/__tests__/rule-evaluator.test.js +0 -280
  120. package/dist/__tests__/scip-decoder.test.js +0 -49
  121. package/dist/__tests__/scip-downloader.test.js +0 -201
  122. package/dist/__tests__/scip-merger.test.js +0 -103
  123. package/dist/__tests__/search-index.test.js +0 -422
  124. package/dist/__tests__/semantic-enrichment.test.js +0 -360
  125. package/dist/__tests__/session-brief-builder.test.js +0 -187
  126. package/dist/__tests__/session-context.test.js +0 -221
  127. package/dist/__tests__/session-continuity.test.js +0 -144
  128. package/dist/__tests__/session-dedup.test.js +0 -74
  129. package/dist/__tests__/session-event-wiring.test.js +0 -206
  130. package/dist/__tests__/session-events.test.js +0 -149
  131. package/dist/__tests__/session-legend.test.js +0 -20
  132. package/dist/__tests__/session-persistence.test.js +0 -131
  133. package/dist/__tests__/session-resume-block.test.js +0 -107
  134. package/dist/__tests__/session-resume.test.js +0 -97
  135. package/dist/__tests__/session-summary-writer.test.js +0 -134
  136. package/dist/__tests__/shadow-ledger.test.js +0 -203
  137. package/dist/__tests__/shell-classifier.test.js +0 -151
  138. package/dist/__tests__/shell-compression-floor.test.js +0 -189
  139. package/dist/__tests__/shell-compression-v2.test.js +0 -339
  140. package/dist/__tests__/shell-compressor.test.js +0 -35
  141. package/dist/__tests__/shell-hooks.test.js +0 -128
  142. package/dist/__tests__/shell-strategies.test.js +0 -644
  143. package/dist/__tests__/shell-tee.test.js +0 -133
  144. package/dist/__tests__/signal-dedup.test.js +0 -158
  145. package/dist/__tests__/signal-reinforcer.test.js +0 -77
  146. package/dist/__tests__/signal-scorer.test.js +0 -251
  147. package/dist/__tests__/signal-show-store.test.js +0 -108
  148. package/dist/__tests__/smart-truncate.test.js +0 -215
  149. package/dist/__tests__/snapshot-v2.test.js +0 -113
  150. package/dist/__tests__/sprint-l1-local-mode.test.js +0 -130
  151. package/dist/__tests__/sprint-l10-boot.test.js +0 -220
  152. package/dist/__tests__/sprint-l9-offline-commands.test.js +0 -189
  153. package/dist/__tests__/sprint-q-persistent-context.test.js +0 -198
  154. package/dist/__tests__/sprint-s1-wiring.test.js +0 -215
  155. package/dist/__tests__/sprint-s2-wiring.test.js +0 -256
  156. package/dist/__tests__/sprint-s3-wiring.test.js +0 -195
  157. package/dist/__tests__/sprint-s4-wiring.test.js +0 -213
  158. package/dist/__tests__/sprint-s6-hooks.test.js +0 -222
  159. package/dist/__tests__/sprint-s7-persistent.test.js +0 -263
  160. package/dist/__tests__/sprint-s8-value.test.js +0 -167
  161. package/dist/__tests__/sprint-s9-behavioral.test.js +0 -179
  162. package/dist/__tests__/sprint3-intelligence.test.js +0 -297
  163. package/dist/__tests__/sprint5-mcp-server.test.js +0 -136
  164. package/dist/__tests__/startup-display.test.js +0 -302
  165. package/dist/__tests__/startup-log-file.test.js +0 -97
  166. package/dist/__tests__/stash-manager.test.js +0 -229
  167. package/dist/__tests__/state-detector.test.js +0 -92
  168. package/dist/__tests__/status-dashboard.test.js +0 -142
  169. package/dist/__tests__/temporal-facts.test.js +0 -292
  170. package/dist/__tests__/temporal-routes.test.js +0 -142
  171. package/dist/__tests__/test-detector.test.js +0 -174
  172. package/dist/__tests__/theme.test.js +0 -72
  173. package/dist/__tests__/timeline-agents.test.js +0 -122
  174. package/dist/__tests__/timeline-bootstrap.test.js +0 -176
  175. package/dist/__tests__/timeline-filters.test.js +0 -193
  176. package/dist/__tests__/timeline-markers.test.js +0 -151
  177. package/dist/__tests__/timeline-routes.test.js +0 -156
  178. package/dist/__tests__/timeline-store.test.js +0 -171
  179. package/dist/__tests__/token-counter.test.js +0 -86
  180. package/dist/__tests__/token-estimator.test.js +0 -96
  181. package/dist/__tests__/token-flow-api.test.js +0 -239
  182. package/dist/__tests__/token-flow-instrumentation.test.js +0 -437
  183. package/dist/__tests__/token-flow-persistence.test.js +0 -356
  184. package/dist/__tests__/token-flow-routes.test.js +0 -199
  185. package/dist/__tests__/token-flow.test.js +0 -695
  186. package/dist/__tests__/tool-clusters.test.js +0 -177
  187. package/dist/__tests__/transport-mux.test.js +0 -283
  188. package/dist/__tests__/turn-segmenter.test.js +0 -166
  189. package/dist/__tests__/uninstall.test.js +0 -141
  190. package/dist/__tests__/warm-start-policy.test.js +0 -271
  191. package/dist/__tests__/wire-cap-nudge.test.js +0 -77
  192. package/dist/__tests__/worker-pool.test.js +0 -101
  193. package/dist/ui/assets/index-7gl3mIuY.css +0 -1
  194. package/dist/ui/assets/index-CX4FCWGT.js +0 -10
  195. package/dist/ui/assets/rolldown-runtime-S-ySWqyJ.js +0 -1
  196. package/dist/ui/assets/vis-network-NIJHUFI3.js +0 -908
  197. package/dist/ui/fonts/jetbrains-mono-latin-400-normal.woff +0 -0
  198. package/dist/ui/icon-wordmark.png +0 -0
  199. package/dist/ui/icon-wordmark.svg +0 -30
  200. package/dist/ui/icon.png +0 -0
  201. package/dist/ui/icon.svg +0 -25
  202. package/dist/ui/index.html +0 -15
  203. package/dist/ui/prototype-sandbox/index.html +0 -257
  204. package/dist/ui/screenshots/activity.png +0 -0
  205. package/dist/ui/screenshots/code-base-intelligence.png +0 -0
  206. package/dist/ui/screenshots/dashboard.png +0 -0
  207. package/dist/ui/screenshots/project-memory.png +0 -0
  208. package/dist/ui/screenshots/reasoning-quality.png +0 -0
  209. package/dist/ui/screenshots/reasoning-session.png +0 -0
  210. package/dist/ui/screenshots/token-session.png +0 -0
  211. package/dist/ui/screenshots/token-trace-main.png +0 -0
  212. package/dist/ui/screenshots/token-turn.png +0 -0
  213. package/dist/ui/unerr-wordmark.png +0 -0
  214. package/dist/ui/unerr-wordmark.svg +0 -9
  215. package/dist/ui/unerr.png +0 -0
  216. package/dist/ui/unerr.svg +0 -25
  217. package/dist/ui/web-app-manifest-192x192.png +0 -0
  218. package/dist/ui/web-app-manifest-512x512.png +0 -0
@@ -1,177 +0,0 @@
1
- /**
2
- * S7: Semantic Tool Clustering tests.
3
- *
4
- * Tests: cluster definitions, usage tracking, tool reordering.
5
- */
6
- import { describe, expect, it } from "vitest";
7
- import { TOOL_CLUSTERS, ToolUsageTracker, getToolCluster, reorderToolsByCluster, } from "../proxy/tool-clusters.js";
8
- // ── Cluster Definitions ──────────────────────────────────────────────
9
- describe("TOOL_CLUSTERS", () => {
10
- it("has 6 semantic clusters", () => {
11
- // ST-2: session-narrative cluster added (mark_* tools)
12
- expect(TOOL_CLUSTERS).toHaveLength(6);
13
- });
14
- it("clusters have unique IDs", () => {
15
- const ids = TOOL_CLUSTERS.map((c) => c.id);
16
- expect(new Set(ids).size).toBe(ids.length);
17
- });
18
- it("all tools appear in exactly one cluster", () => {
19
- const toolCounts = new Map();
20
- for (const cluster of TOOL_CLUSTERS) {
21
- for (const tool of cluster.tools) {
22
- toolCounts.set(tool, (toolCounts.get(tool) ?? 0) + 1);
23
- }
24
- }
25
- for (const [tool, count] of toolCounts) {
26
- expect(count, `${tool} appears in ${count} clusters`).toBe(1);
27
- }
28
- });
29
- it("navigation cluster has the most tools", () => {
30
- const nav = TOOL_CLUSTERS.find((c) => c.id === "navigation");
31
- expect(nav).toBeDefined();
32
- for (const cluster of TOOL_CLUSTERS) {
33
- if (cluster.id !== "navigation") {
34
- expect(nav.tools.length).toBeGreaterThanOrEqual(cluster.tools.length);
35
- }
36
- }
37
- });
38
- it("each cluster has trigger keywords", () => {
39
- for (const cluster of TOOL_CLUSTERS) {
40
- expect(cluster.triggerKeywords.length).toBeGreaterThan(0);
41
- }
42
- });
43
- });
44
- // ── getToolCluster ───────────────────────────────────────────────────
45
- describe("getToolCluster", () => {
46
- it("maps search_code to navigation", () => {
47
- expect(getToolCluster("search_code")).toBe("navigation");
48
- });
49
- it("maps file_outline to file-access", () => {
50
- expect(getToolCluster("file_outline")).toBe("file-access");
51
- });
52
- // Disabled: get_rules tool is disabled (no rules detected yet)
53
- // it("maps get_rules to quality", () => {
54
- // expect(getToolCluster("get_rules")).toBe("quality");
55
- // });
56
- it("maps record_fact to persistence", () => {
57
- expect(getToolCluster("record_fact")).toBe("persistence");
58
- });
59
- // Disabled: safety cluster removed (shadow ledger tools not exposed)
60
- // it("maps unerr_mark_working to safety", () => {
61
- // expect(getToolCluster("unerr_mark_working")).toBe("safety");
62
- // });
63
- it("returns undefined for unknown tool", () => {
64
- expect(getToolCluster("unknown_tool")).toBeUndefined();
65
- });
66
- });
67
- // ── ToolUsageTracker ─────────────────────────────────────────────────
68
- describe("ToolUsageTracker", () => {
69
- it("starts with zero call count", () => {
70
- const tracker = new ToolUsageTracker();
71
- expect(tracker.getCallCount()).toBe(0);
72
- });
73
- it("records tool calls", () => {
74
- const tracker = new ToolUsageTracker();
75
- tracker.record("search_code");
76
- tracker.record("get_entity");
77
- expect(tracker.getCallCount()).toBe(2);
78
- });
79
- it("tracks most recent cluster", () => {
80
- const tracker = new ToolUsageTracker();
81
- tracker.record("search_code");
82
- expect(tracker.getMostRecentCluster()).toBe("navigation");
83
- tracker.record("file_outline");
84
- expect(tracker.getMostRecentCluster()).toBe("file-access");
85
- });
86
- it("returns null for most recent cluster with no history", () => {
87
- const tracker = new ToolUsageTracker();
88
- expect(tracker.getMostRecentCluster()).toBeNull();
89
- });
90
- it("returns null for unknown tools in most recent cluster", () => {
91
- const tracker = new ToolUsageTracker();
92
- tracker.record("unknown_tool");
93
- expect(tracker.getMostRecentCluster()).toBeNull();
94
- });
95
- it("produces cluster scores with recency weighting", () => {
96
- const tracker = new ToolUsageTracker();
97
- tracker.record("search_code");
98
- tracker.record("file_outline");
99
- tracker.record("search_code");
100
- const scores = tracker.getClusterScores();
101
- // navigation should score higher (2 calls, more recent)
102
- expect(scores.get("navigation")).toBeGreaterThan(0);
103
- expect(scores.get("file-access")).toBeGreaterThan(0);
104
- expect(scores.get("navigation")).toBeGreaterThan(scores.get("file-access"));
105
- });
106
- it("respects maxHistory limit", () => {
107
- const tracker = new ToolUsageTracker(3);
108
- tracker.record("search_code");
109
- tracker.record("file_outline");
110
- tracker.record("get_rules");
111
- tracker.record("record_fact");
112
- expect(tracker.getCallCount()).toBe(3); // oldest dropped
113
- });
114
- });
115
- // ── reorderToolsByCluster ────────────────────────────────────────────
116
- describe("reorderToolsByCluster", () => {
117
- const mockTools = [
118
- { name: "get_entity" },
119
- { name: "file_outline" },
120
- { name: "get_rules" },
121
- { name: "record_fact" },
122
- { name: "unerr_mark_working" },
123
- { name: "search_code" },
124
- { name: "file_read" },
125
- ];
126
- it("returns all tools (never filters)", () => {
127
- const tracker = new ToolUsageTracker();
128
- const result = reorderToolsByCluster(mockTools, tracker);
129
- expect(result).toHaveLength(mockTools.length);
130
- });
131
- it("returns same tools with null tracker", () => {
132
- const result = reorderToolsByCluster(mockTools, null);
133
- expect(result).toHaveLength(mockTools.length);
134
- // All original tools present
135
- const names = result.map((t) => t.name);
136
- for (const tool of mockTools) {
137
- expect(names).toContain(tool.name);
138
- }
139
- });
140
- it("uses default cluster order with no usage data", () => {
141
- const tracker = new ToolUsageTracker();
142
- const result = reorderToolsByCluster(mockTools, tracker);
143
- const names = result.map((t) => t.name);
144
- // Navigation cluster tools should come first (default order)
145
- expect(names.indexOf("search_code")).toBeLessThan(names.indexOf("file_outline"));
146
- expect(names.indexOf("search_code")).toBeLessThan(names.indexOf("get_rules"));
147
- });
148
- it("prioritizes recently-used cluster", () => {
149
- const tracker = new ToolUsageTracker();
150
- // Use file-access tools heavily
151
- tracker.record("file_outline");
152
- tracker.record("file_read");
153
- tracker.record("file_outline");
154
- const result = reorderToolsByCluster(mockTools, tracker);
155
- const names = result.map((t) => t.name);
156
- // file-access tools should now come before navigation
157
- expect(names.indexOf("file_outline")).toBeLessThan(names.indexOf("search_code"));
158
- });
159
- it("includes unclustered tools at the end", () => {
160
- const toolsWithExtra = [
161
- ...mockTools,
162
- { name: "custom_tool_not_in_cluster" },
163
- ];
164
- const result = reorderToolsByCluster(toolsWithExtra, null);
165
- const names = result.map((t) => t.name);
166
- expect(names[names.length - 1]).toBe("custom_tool_not_in_cluster");
167
- });
168
- it("preserves tool object references", () => {
169
- const tools = [
170
- { name: "search_code", description: "search desc" },
171
- { name: "file_outline", description: "outline desc" },
172
- ];
173
- const result = reorderToolsByCluster(tools, null);
174
- const searchTool = result.find((t) => t.name === "search_code");
175
- expect(searchTool).toBe(tools[0]); // Same reference, not copy
176
- });
177
- });
@@ -1,283 +0,0 @@
1
- /**
2
- * Sprint 7.2: Transport Multiplexer tests (Unix domain socket multi-client).
3
- */
4
- import { existsSync, mkdtempSync } from "node:fs";
5
- import { createConnection } from "node:net";
6
- import { tmpdir } from "node:os";
7
- import { join } from "node:path";
8
- import { afterEach, describe, expect, it } from "vitest";
9
- import { TransportMux, } from "../proxy/transport-mux.js";
10
- function createTempDir() {
11
- return mkdtempSync(join(tmpdir(), "mux-"));
12
- }
13
- /** Send a JSON-RPC message over a socket and wait for response. */
14
- function sendMessage(sockPath, message) {
15
- return new Promise((resolve, reject) => {
16
- const socket = createConnection(sockPath);
17
- let buffer = "";
18
- socket.on("connect", () => {
19
- socket.write(`${JSON.stringify(message)}\n`);
20
- });
21
- socket.on("data", (data) => {
22
- buffer += data.toString();
23
- const newlineIdx = buffer.indexOf("\n");
24
- if (newlineIdx !== -1) {
25
- const line = buffer.slice(0, newlineIdx).trim();
26
- try {
27
- const response = JSON.parse(line);
28
- socket.destroy();
29
- resolve(response);
30
- }
31
- catch (err) {
32
- socket.destroy();
33
- reject(err);
34
- }
35
- }
36
- });
37
- socket.on("error", reject);
38
- // Timeout after 2s
39
- setTimeout(() => {
40
- socket.destroy();
41
- reject(new Error("Timeout waiting for response"));
42
- }, 2000);
43
- });
44
- }
45
- /** Wait for socket to become available. */
46
- function waitForSocket(sockPath, timeoutMs = 2000) {
47
- return new Promise((resolve, reject) => {
48
- const start = Date.now();
49
- const check = () => {
50
- if (existsSync(sockPath)) {
51
- resolve();
52
- return;
53
- }
54
- if (Date.now() - start > timeoutMs) {
55
- reject(new Error("Timeout waiting for socket"));
56
- return;
57
- }
58
- setTimeout(check, 50);
59
- };
60
- check();
61
- });
62
- }
63
- describe("TransportMux", () => {
64
- const muxInstances = [];
65
- afterEach(() => {
66
- for (const mux of muxInstances) {
67
- mux.stop();
68
- }
69
- muxInstances.length = 0;
70
- });
71
- it("starts and stops cleanly", async () => {
72
- const dir = createTempDir();
73
- const sockPath = join(dir, "proxy.sock");
74
- const mux = new TransportMux(sockPath);
75
- muxInstances.push(mux);
76
- mux.start();
77
- await waitForSocket(sockPath);
78
- expect(existsSync(sockPath)).toBe(true);
79
- mux.stop();
80
- expect(existsSync(sockPath)).toBe(false);
81
- });
82
- it("accepts a UDS client and routes messages", async () => {
83
- const dir = createTempDir();
84
- const sockPath = join(dir, "proxy.sock");
85
- const mux = new TransportMux(sockPath);
86
- muxInstances.push(mux);
87
- const receivedMessages = [];
88
- const handler = async (clientId, message) => {
89
- receivedMessages.push({ clientId, method: message.method });
90
- return {
91
- jsonrpc: "2.0",
92
- result: { tools: ["get_function"] },
93
- };
94
- };
95
- mux.setHandler(handler);
96
- mux.start();
97
- await waitForSocket(sockPath);
98
- const response = await sendMessage(sockPath, {
99
- jsonrpc: "2.0",
100
- id: 1,
101
- method: "tools/list",
102
- });
103
- expect(response.jsonrpc).toBe("2.0");
104
- expect(response.id).toBe(1);
105
- expect(response.result).toBeDefined();
106
- expect(receivedMessages).toHaveLength(1);
107
- expect(receivedMessages[0]?.method).toBe("tools/list");
108
- expect(receivedMessages[0]?.clientId).toMatch(/^uds-/);
109
- });
110
- it("handles tools/call with arguments", async () => {
111
- const dir = createTempDir();
112
- const sockPath = join(dir, "proxy.sock");
113
- const mux = new TransportMux(sockPath);
114
- muxInstances.push(mux);
115
- const handler = async (_clientId, message) => {
116
- const params = message.params;
117
- return {
118
- jsonrpc: "2.0",
119
- result: {
120
- content: [{ type: "text", text: `Called ${params.name}` }],
121
- },
122
- };
123
- };
124
- mux.setHandler(handler);
125
- mux.start();
126
- await waitForSocket(sockPath);
127
- const response = await sendMessage(sockPath, {
128
- jsonrpc: "2.0",
129
- id: 2,
130
- method: "tools/call",
131
- params: { name: "get_function", arguments: { key: "k1" } },
132
- });
133
- expect(response.id).toBe(2);
134
- const result = response.result;
135
- expect(result.content[0]?.text).toBe("Called get_function");
136
- });
137
- it("returns error for unknown methods", async () => {
138
- const dir = createTempDir();
139
- const sockPath = join(dir, "proxy.sock");
140
- const mux = new TransportMux(sockPath);
141
- muxInstances.push(mux);
142
- const handler = async () => {
143
- return {
144
- jsonrpc: "2.0",
145
- error: { code: -32601, message: "Method not found" },
146
- };
147
- };
148
- mux.setHandler(handler);
149
- mux.start();
150
- await waitForSocket(sockPath);
151
- const response = await sendMessage(sockPath, {
152
- jsonrpc: "2.0",
153
- id: 3,
154
- method: "unknown/method",
155
- });
156
- expect(response.error).toBeDefined();
157
- expect(response.error?.code).toBe(-32601);
158
- });
159
- it("returns parse error for invalid JSON", async () => {
160
- const dir = createTempDir();
161
- const sockPath = join(dir, "proxy.sock");
162
- const mux = new TransportMux(sockPath);
163
- muxInstances.push(mux);
164
- mux.setHandler(async () => ({
165
- jsonrpc: "2.0",
166
- result: {},
167
- }));
168
- mux.start();
169
- await waitForSocket(sockPath);
170
- // Send raw invalid JSON
171
- const response = await new Promise((resolve, reject) => {
172
- const socket = createConnection(sockPath);
173
- let buffer = "";
174
- socket.on("connect", () => {
175
- socket.write("not valid json\n");
176
- });
177
- socket.on("data", (data) => {
178
- buffer += data.toString();
179
- const idx = buffer.indexOf("\n");
180
- if (idx !== -1) {
181
- socket.destroy();
182
- resolve(JSON.parse(buffer.slice(0, idx)));
183
- }
184
- });
185
- socket.on("error", reject);
186
- setTimeout(() => {
187
- socket.destroy();
188
- reject(new Error("Timeout"));
189
- }, 2000);
190
- });
191
- expect(response.error).toBeDefined();
192
- expect(response.error?.code).toBe(-32700);
193
- });
194
- it("supports multiple concurrent clients", async () => {
195
- const dir = createTempDir();
196
- const sockPath = join(dir, "proxy.sock");
197
- const mux = new TransportMux(sockPath);
198
- muxInstances.push(mux);
199
- const clientIds = new Set();
200
- const handler = async (clientId) => {
201
- clientIds.add(clientId);
202
- return {
203
- jsonrpc: "2.0",
204
- result: { clientId },
205
- };
206
- };
207
- mux.setHandler(handler);
208
- mux.start();
209
- await waitForSocket(sockPath);
210
- // Send two messages concurrently from separate connections
211
- const [r1, r2] = await Promise.all([
212
- sendMessage(sockPath, { jsonrpc: "2.0", id: 1, method: "tools/list" }),
213
- sendMessage(sockPath, { jsonrpc: "2.0", id: 2, method: "tools/list" }),
214
- ]);
215
- expect(r1.result).toBeDefined();
216
- expect(r2.result).toBeDefined();
217
- // Each connection gets a unique client ID
218
- expect(clientIds.size).toBe(2);
219
- });
220
- it("tracks client count", async () => {
221
- const dir = createTempDir();
222
- const sockPath = join(dir, "proxy.sock");
223
- const mux = new TransportMux(sockPath);
224
- muxInstances.push(mux);
225
- mux.setHandler(async () => ({ jsonrpc: "2.0", result: {} }));
226
- mux.start();
227
- await waitForSocket(sockPath);
228
- expect(mux.clientCount).toBe(0);
229
- // Connect a client (it connects then disconnects after response)
230
- await sendMessage(sockPath, {
231
- jsonrpc: "2.0",
232
- id: 1,
233
- method: "tools/list",
234
- });
235
- // After sendMessage destroys socket, client should be cleaned up
236
- // Give event loop time to process the close
237
- await new Promise((r) => setTimeout(r, 100));
238
- expect(mux.clientCount).toBe(0);
239
- });
240
- it("cleans up stale socket file on start", async () => {
241
- const dir = createTempDir();
242
- const sockPath = join(dir, "proxy.sock");
243
- // Create a stale socket file
244
- const { writeFileSync } = await import("node:fs");
245
- writeFileSync(sockPath, "stale", "utf-8");
246
- expect(existsSync(sockPath)).toBe(true);
247
- const mux = new TransportMux(sockPath);
248
- muxInstances.push(mux);
249
- // Start should remove stale file and recreate
250
- mux.start();
251
- await waitForSocket(sockPath);
252
- // Should still work
253
- mux.setHandler(async () => ({
254
- jsonrpc: "2.0",
255
- result: { ok: true },
256
- }));
257
- const response = await sendMessage(sockPath, {
258
- jsonrpc: "2.0",
259
- id: 1,
260
- method: "tools/list",
261
- });
262
- expect(response.result).toBeDefined();
263
- });
264
- it("handles handler errors gracefully", async () => {
265
- const dir = createTempDir();
266
- const sockPath = join(dir, "proxy.sock");
267
- const mux = new TransportMux(sockPath);
268
- muxInstances.push(mux);
269
- mux.setHandler(async () => {
270
- throw new Error("Handler crashed");
271
- });
272
- mux.start();
273
- await waitForSocket(sockPath);
274
- const response = await sendMessage(sockPath, {
275
- jsonrpc: "2.0",
276
- id: 1,
277
- method: "tools/list",
278
- });
279
- expect(response.error).toBeDefined();
280
- expect(response.error?.code).toBe(-32603);
281
- expect(response.error?.message).toBe("Handler crashed");
282
- });
283
- });
@@ -1,166 +0,0 @@
1
- /**
2
- * ST-1: TurnSegmenter — pure, no I/O. Tests cover boundary detection
3
- * (first_call, idle_gap, stop_hook) and session isolation.
4
- */
5
- import { describe, expect, it, vi } from "vitest";
6
- import { TurnSegmenter, } from "../tracking/turn-segmenter.js";
7
- function makeEntry(sessionId, ts) {
8
- return { session_id: sessionId, ts };
9
- }
10
- describe("TurnSegmenter", () => {
11
- it("stamps the first entry as a new turn with confidence 'first_call'", () => {
12
- const seg = new TurnSegmenter();
13
- const e = makeEntry("s1", new Date("2026-05-12T10:00:00Z").toISOString());
14
- seg.observe(e);
15
- expect(e.turn_id).toMatch(/^[a-f0-9]{12}$/);
16
- expect(e.turn_confidence).toBe("first_call");
17
- });
18
- it("keeps subsequent entries within idle window in the same turn", () => {
19
- const seg = new TurnSegmenter({ idleGapMs: 20_000 });
20
- const e1 = makeEntry("s1", new Date("2026-05-12T10:00:00Z").toISOString());
21
- const e2 = makeEntry("s1", new Date("2026-05-12T10:00:15Z").toISOString());
22
- seg.observe(e1);
23
- seg.observe(e2);
24
- expect(e2.turn_id).toBe(e1.turn_id);
25
- expect(e2.turn_confidence).toBe("first_call");
26
- });
27
- it("opens a new turn after an idle gap and emits a close event", () => {
28
- const seg = new TurnSegmenter({ idleGapMs: 20_000 });
29
- const closes = [];
30
- seg.onTurnClose((e) => closes.push(e));
31
- const e1 = makeEntry("s1", new Date("2026-05-12T10:00:00Z").toISOString());
32
- const e2 = makeEntry("s1", new Date("2026-05-12T10:00:25Z").toISOString());
33
- seg.observe(e1);
34
- seg.observe(e2);
35
- expect(e2.turn_id).not.toBe(e1.turn_id);
36
- expect(e2.turn_confidence).toBe("idle_gap");
37
- expect(closes).toHaveLength(1);
38
- expect(closes[0]?.turn_id).toBe(e1.turn_id);
39
- expect(closes[0]?.reason).toBe("idle_gap");
40
- expect(closes[0]?.session_id).toBe("s1");
41
- });
42
- it("isolates turn ids across sessions", () => {
43
- const seg = new TurnSegmenter();
44
- const a = makeEntry("sa", new Date("2026-05-12T10:00:00Z").toISOString());
45
- const b = makeEntry("sb", new Date("2026-05-12T10:00:00Z").toISOString());
46
- seg.observe(a);
47
- seg.observe(b);
48
- expect(a.turn_id).toBeTypeOf("string");
49
- expect(b.turn_id).toBeTypeOf("string");
50
- expect(a.turn_id).not.toBe(b.turn_id);
51
- });
52
- it("closeTurn anchors an exact boundary; next entry is 'first_call'", () => {
53
- const seg = new TurnSegmenter();
54
- const closes = [];
55
- seg.onTurnClose((e) => closes.push(e));
56
- const e1 = makeEntry("s1", new Date("2026-05-12T10:00:00Z").toISOString());
57
- seg.observe(e1);
58
- seg.closeTurn("s1", "stop_hook");
59
- const e2 = makeEntry("s1", new Date("2026-05-12T10:00:05Z").toISOString());
60
- seg.observe(e2);
61
- expect(e2.turn_id).not.toBe(e1.turn_id);
62
- expect(e2.turn_confidence).toBe("first_call");
63
- expect(closes).toHaveLength(1);
64
- expect(closes[0]?.reason).toBe("stop_hook");
65
- });
66
- it("closeTurn with no open turn is a no-op", () => {
67
- const seg = new TurnSegmenter();
68
- const closes = [];
69
- seg.onTurnClose((e) => closes.push(e));
70
- seg.closeTurn("never-seen");
71
- expect(closes).toHaveLength(0);
72
- });
73
- it("getCurrentTurnId returns the active turn for the session", () => {
74
- const seg = new TurnSegmenter();
75
- expect(seg.getCurrentTurnId("s1")).toBeNull();
76
- const e = makeEntry("s1", new Date("2026-05-12T10:00:00Z").toISOString());
77
- seg.observe(e);
78
- expect(seg.getCurrentTurnId("s1")).toBe(e.turn_id);
79
- seg.closeTurn("s1");
80
- expect(seg.getCurrentTurnId("s1")).toBeNull();
81
- });
82
- it("onTurnClose returns an unsubscribe function", () => {
83
- const seg = new TurnSegmenter({ idleGapMs: 10_000 });
84
- const listener = vi.fn();
85
- const unsubscribe = seg.onTurnClose(listener);
86
- const e1 = makeEntry("s1", new Date("2026-05-12T10:00:00Z").toISOString());
87
- const e2 = makeEntry("s1", new Date("2026-05-12T10:00:20Z").toISOString());
88
- seg.observe(e1);
89
- seg.observe(e2);
90
- expect(listener).toHaveBeenCalledTimes(1);
91
- unsubscribe();
92
- const e3 = makeEntry("s1", new Date("2026-05-12T10:00:50Z").toISOString());
93
- seg.observe(e3);
94
- expect(listener).toHaveBeenCalledTimes(1);
95
- });
96
- it("survives a throwing listener without breaking other listeners", () => {
97
- const seg = new TurnSegmenter({ idleGapMs: 10_000 });
98
- const good = vi.fn();
99
- seg.onTurnClose(() => {
100
- throw new Error("boom");
101
- });
102
- seg.onTurnClose(good);
103
- const e1 = makeEntry("s1", new Date("2026-05-12T10:00:00Z").toISOString());
104
- const e2 = makeEntry("s1", new Date("2026-05-12T10:00:30Z").toISOString());
105
- seg.observe(e1);
106
- seg.observe(e2);
107
- expect(good).toHaveBeenCalledTimes(1);
108
- });
109
- it("reset clears all session state", () => {
110
- const seg = new TurnSegmenter();
111
- const e = makeEntry("s1", new Date("2026-05-12T10:00:00Z").toISOString());
112
- seg.observe(e);
113
- expect(seg.getCurrentTurnId("s1")).not.toBeNull();
114
- seg.reset();
115
- expect(seg.getCurrentTurnId("s1")).toBeNull();
116
- });
117
- });
118
- describe("ShadowLedger ↔ TurnSegmenter integration", () => {
119
- it("stamps recorded entries with turn_id and turn_confidence", async () => {
120
- const { mkdirSync, rmSync, readFileSync } = await import("node:fs");
121
- const { tmpdir } = await import("node:os");
122
- const { join } = await import("node:path");
123
- const { ShadowLedger } = await import("../tracking/shadow-ledger.js");
124
- const tempDir = join(tmpdir(), `unerr-ts-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
125
- const unerrDir = join(tempDir, ".unerr");
126
- mkdirSync(unerrDir, { recursive: true });
127
- try {
128
- const ledger = new ShadowLedger(unerrDir);
129
- const e1 = ledger.record("search_code", { query: "foo" }, { count: 3 }, "main", "deadbeef");
130
- const e2 = ledger.record("file_read", { file_path: "src/a.ts" }, { lines: 100 }, "main", "deadbeef");
131
- expect(e1.turn_id).toMatch(/^[a-f0-9]{12}$/);
132
- expect(e1.turn_confidence).toBe("first_call");
133
- expect(e2.turn_id).toBe(e1.turn_id);
134
- const filePath = join(unerrDir, "ledger", "shadow.jsonl");
135
- const lines = readFileSync(filePath, "utf-8")
136
- .trim()
137
- .split("\n")
138
- .map((l) => JSON.parse(l));
139
- expect(lines[0].turn_id).toBe(e1.turn_id);
140
- expect(lines[0].turn_confidence).toBe("first_call");
141
- }
142
- finally {
143
- rmSync(tempDir, { recursive: true, force: true });
144
- }
145
- });
146
- it("ledger.closeTurn() anchors a fresh first_call boundary on next record", async () => {
147
- const { mkdirSync, rmSync } = await import("node:fs");
148
- const { tmpdir } = await import("node:os");
149
- const { join } = await import("node:path");
150
- const { ShadowLedger } = await import("../tracking/shadow-ledger.js");
151
- const tempDir = join(tmpdir(), `unerr-ts-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
152
- const unerrDir = join(tempDir, ".unerr");
153
- mkdirSync(unerrDir, { recursive: true });
154
- try {
155
- const ledger = new ShadowLedger(unerrDir);
156
- const a = ledger.record("search_code", {}, {}, "main", "x");
157
- ledger.closeTurn("stop_hook");
158
- const b = ledger.record("file_read", {}, {}, "main", "x");
159
- expect(b.turn_id).not.toBe(a.turn_id);
160
- expect(b.turn_confidence).toBe("first_call");
161
- }
162
- finally {
163
- rmSync(tempDir, { recursive: true, force: true });
164
- }
165
- });
166
- });