@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,223 +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
- describe("MetricsStore", () => {
7
- let dir;
8
- beforeEach(() => {
9
- dir = join(os.tmpdir(), `unerr-metrics-${Date.now()}-${Math.random()}`);
10
- mkdirSync(dir, { recursive: true });
11
- });
12
- afterEach(() => {
13
- closeMetricsStore(dir);
14
- rmSync(dir, { recursive: true, force: true });
15
- });
16
- it("creates metrics.db on first open and is idempotent", () => {
17
- openMetricsStore(dir);
18
- expect(existsSync(join(dir, "metrics.db"))).toBe(true);
19
- // Re-opening returns the same instance (no error)
20
- expect(openMetricsStore(dir)).toBe(openMetricsStore(dir));
21
- });
22
- it("inserts + reads compression events with monotonic id", () => {
23
- const s = openMetricsStore(dir);
24
- const id1 = s.insertCompression({
25
- ts: Date.now(),
26
- ts_iso: new Date().toISOString(),
27
- command: "ls",
28
- category: "log_text",
29
- confidence: 0.9,
30
- raw_bytes: 1024,
31
- compressed_bytes: 512,
32
- saved_pct: 50,
33
- omni_fallback: 0,
34
- tee_file: null,
35
- });
36
- const id2 = s.insertCompression({
37
- ts: Date.now() + 1,
38
- ts_iso: new Date().toISOString(),
39
- command: "ps",
40
- category: "tabular",
41
- confidence: 0.95,
42
- raw_bytes: 2048,
43
- compressed_bytes: 256,
44
- saved_pct: 87.5,
45
- omni_fallback: 0,
46
- tee_file: ".unerr/tee/foo.txt",
47
- });
48
- expect(id2).toBeGreaterThan(id1);
49
- const recent = s.recentCompression(10);
50
- expect(recent).toHaveLength(2);
51
- // ORDER BY id DESC — newest first
52
- expect(recent[0]?.command).toBe("ps");
53
- expect(recent[1]?.command).toBe("ls");
54
- });
55
- it("supports id > lastSeen polling for compression", () => {
56
- const s = openMetricsStore(dir);
57
- for (let i = 0; i < 3; i++) {
58
- s.insertCompression({
59
- ts: Date.now() + i,
60
- ts_iso: new Date().toISOString(),
61
- command: `cmd-${i}`,
62
- category: "log_text",
63
- confidence: 0.9,
64
- raw_bytes: 100,
65
- compressed_bytes: 50,
66
- saved_pct: 50,
67
- omni_fallback: 0,
68
- tee_file: null,
69
- });
70
- }
71
- const all = s.compressionSince(0);
72
- expect(all).toHaveLength(3);
73
- expect(all.map((r) => r.command)).toEqual(["cmd-0", "cmd-1", "cmd-2"]);
74
- const afterFirst = s.compressionSince(all[0].id);
75
- expect(afterFirst).toHaveLength(2);
76
- expect(afterFirst[0]?.command).toBe("cmd-1");
77
- });
78
- it("inserts + reads file_read events", () => {
79
- const s = openMetricsStore(dir);
80
- s.insertFileRead({
81
- ts: Date.now(),
82
- ts_iso: new Date().toISOString(),
83
- file: "src/foo.ts",
84
- mode: "outline",
85
- total_lines: 500,
86
- returned_lines: 30,
87
- saved_pct: 94,
88
- entity: null,
89
- token_estimate: 200,
90
- });
91
- const all = s.fileReadsSince(0);
92
- expect(all).toHaveLength(1);
93
- expect(all[0]?.file).toBe("src/foo.ts");
94
- expect(all[0]?.mode).toBe("outline");
95
- });
96
- it("inserts + reads token_flow events; filters by session", () => {
97
- const s = openMetricsStore(dir);
98
- s.insertTokenFlow({
99
- ts: Date.now(),
100
- ts_iso: new Date().toISOString(),
101
- session_id: "s-1",
102
- pid: 1234,
103
- turn: 1,
104
- mechanism: "graph_query",
105
- tool: "search_code",
106
- tokens_without: 1000,
107
- tokens_with: 200,
108
- tokens_saved: 800,
109
- detail: JSON.stringify({ query: "foo" }),
110
- });
111
- s.insertTokenFlow({
112
- ts: Date.now() + 1,
113
- ts_iso: new Date().toISOString(),
114
- session_id: "s-2",
115
- pid: 1234,
116
- turn: 1,
117
- mechanism: "shell_compression",
118
- tool: null,
119
- tokens_without: 5000,
120
- tokens_with: 500,
121
- tokens_saved: 4500,
122
- detail: null,
123
- });
124
- expect(s.allTokenFlow()).toHaveLength(2);
125
- expect(s.tokenFlowBySession("s-1")).toHaveLength(1);
126
- expect(s.tokenFlowBySession("s-2")[0]?.mechanism).toBe("shell_compression");
127
- });
128
- it("upserts session_history (one row per session_id)", () => {
129
- const s = openMetricsStore(dir);
130
- s.upsertSessionHistory({
131
- session_id: "s-1",
132
- started_at: "2026-05-12T10:00:00Z",
133
- ended_at: "2026-05-12T10:30:00Z",
134
- duration_ms: 1_800_000,
135
- tool_calls: 50,
136
- tokens_saved: 12000,
137
- tokens_processed: 50000,
138
- efficiency: 24,
139
- dollars_saved: 0.15,
140
- model_id: "claude-opus-4-7",
141
- entity_count: 30,
142
- agent_name: "claude-code",
143
- token_flow_summary: null,
144
- });
145
- // Second upsert for the same session_id should update, not append.
146
- s.upsertSessionHistory({
147
- session_id: "s-1",
148
- started_at: "2026-05-12T10:00:00Z",
149
- ended_at: "2026-05-12T11:00:00Z",
150
- duration_ms: 3_600_000,
151
- tool_calls: 80,
152
- tokens_saved: 20000,
153
- tokens_processed: 80000,
154
- efficiency: 25,
155
- dollars_saved: 0.25,
156
- model_id: "claude-opus-4-7",
157
- entity_count: 45,
158
- agent_name: "claude-code",
159
- token_flow_summary: JSON.stringify({ top_mechanism: "graph_query" }),
160
- });
161
- const all = s.allSessionHistory();
162
- expect(all).toHaveLength(1);
163
- expect(all[0]?.tool_calls).toBe(80);
164
- expect(all[0]?.duration_ms).toBe(3_600_000);
165
- });
166
- it("upserts session_summary and reads by id", () => {
167
- const s = openMetricsStore(dir);
168
- s.upsertSessionSummary({
169
- session_id: "sum-1",
170
- written_at: "2026-05-12T10:30:00Z",
171
- started_at: "2026-05-12T10:00:00Z",
172
- ended_at: "2026-05-12T10:30:00Z",
173
- duration_ms: 1_800_000,
174
- tool_calls: 50,
175
- chains: 5,
176
- files_modified: JSON.stringify(["a.ts", "b.ts"]),
177
- entities_touched: JSON.stringify(["fooFn", "barClass"]),
178
- tools_used: JSON.stringify({ search_code: 10, file_read: 20 }),
179
- feature_areas: JSON.stringify(["intelligence"]),
180
- facts_recorded: 2,
181
- facts_surfaced: JSON.stringify(["f-1"]),
182
- revert_count: 0,
183
- rot_score: 0.1,
184
- token_estimate: 50000,
185
- branch: "main",
186
- });
187
- const r = s.sessionSummary("sum-1");
188
- expect(r?.session_id).toBe("sum-1");
189
- expect(r?.chains).toBe(5);
190
- expect(JSON.parse(r.files_modified)).toEqual(["a.ts", "b.ts"]);
191
- expect(s.sessionSummary("never-existed")).toBeNull();
192
- });
193
- it("reset() empties tables and resets autoincrement", () => {
194
- const s = openMetricsStore(dir);
195
- s.insertCompression({
196
- ts: Date.now(),
197
- ts_iso: new Date().toISOString(),
198
- command: "x",
199
- category: "log_text",
200
- confidence: 1,
201
- raw_bytes: 1,
202
- compressed_bytes: 1,
203
- saved_pct: 0,
204
- omni_fallback: 0,
205
- tee_file: null,
206
- });
207
- s.reset();
208
- expect(s.recentCompression(10)).toEqual([]);
209
- const firstAfterReset = s.insertCompression({
210
- ts: Date.now(),
211
- ts_iso: new Date().toISOString(),
212
- command: "y",
213
- category: "log_text",
214
- confidence: 1,
215
- raw_bytes: 1,
216
- compressed_bytes: 1,
217
- saved_pct: 0,
218
- omni_fallback: 0,
219
- tee_file: null,
220
- });
221
- expect(firstAfterReset).toBe(1);
222
- });
223
- });
@@ -1,191 +0,0 @@
1
- /**
2
- * Native watcher tests — @parcel/watcher integration with debounced events.
3
- */
4
- import { mkdirSync, realpathSync, rmSync, unlinkSync, writeFileSync, } from "node:fs";
5
- import { tmpdir } from "node:os";
6
- import { join } from "node:path";
7
- import { afterEach, beforeEach, describe, expect, it } from "vitest";
8
- import { createNativeWatcher, } from "../tracking/native-watcher.js";
9
- let tempDir;
10
- let watcher = null;
11
- function makeTempDir() {
12
- const raw = join(tmpdir(), `unerr-watcher-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
13
- mkdirSync(raw, { recursive: true });
14
- return realpathSync(raw);
15
- }
16
- /**
17
- * Wait until the predicate matches at least one event, or timeout.
18
- * Returns all collected events at the time the predicate was satisfied.
19
- */
20
- function waitForMatch(collected, predicate, timeoutMs = 3000) {
21
- return new Promise((resolve) => {
22
- const start = Date.now();
23
- const check = () => {
24
- if (collected.some(predicate)) {
25
- resolve([...collected]);
26
- return;
27
- }
28
- if (Date.now() - start > timeoutMs) {
29
- resolve([...collected]);
30
- return;
31
- }
32
- setTimeout(check, 20);
33
- };
34
- check();
35
- });
36
- }
37
- function delay(ms) {
38
- return new Promise((resolve) => setTimeout(resolve, ms));
39
- }
40
- /**
41
- * Start watcher and drain the initial FSEvents directory-creation event
42
- * that fires when subscribing to a newly-created temp directory.
43
- */
44
- async function startAndDrain(w, events) {
45
- await w.start();
46
- await delay(500);
47
- events.length = 0;
48
- }
49
- beforeEach(() => {
50
- tempDir = makeTempDir();
51
- });
52
- afterEach(async () => {
53
- if (watcher?.isRunning()) {
54
- await watcher.stop();
55
- }
56
- watcher = null;
57
- try {
58
- rmSync(tempDir, { recursive: true, force: true });
59
- }
60
- catch {
61
- /* ignore */
62
- }
63
- });
64
- describe("NativeWatcher", () => {
65
- it("can be created and started without errors", async () => {
66
- const events = [];
67
- watcher = createNativeWatcher({
68
- projectRoot: tempDir,
69
- onEvents: (batch) => events.push(...batch),
70
- });
71
- await watcher.start();
72
- expect(watcher.isRunning()).toBe(true);
73
- await watcher.stop();
74
- expect(watcher.isRunning()).toBe(false);
75
- });
76
- it("detects file creation", async () => {
77
- const events = [];
78
- watcher = createNativeWatcher({
79
- projectRoot: tempDir,
80
- debounceMs: 30,
81
- onEvents: (batch) => events.push(...batch),
82
- });
83
- await startAndDrain(watcher, events);
84
- writeFileSync(join(tempDir, "new-file.txt"), "hello");
85
- const result = await waitForMatch(events, (e) => e.path.includes("new-file.txt"));
86
- const createEvent = result.find((e) => e.path.includes("new-file.txt"));
87
- expect(createEvent).toBeDefined();
88
- expect(createEvent?.type).toBe("create");
89
- });
90
- it("detects file modification", async () => {
91
- const filePath = join(tempDir, "existing.txt");
92
- writeFileSync(filePath, "initial content");
93
- const events = [];
94
- watcher = createNativeWatcher({
95
- projectRoot: tempDir,
96
- debounceMs: 30,
97
- onEvents: (batch) => events.push(...batch),
98
- });
99
- await startAndDrain(watcher, events);
100
- writeFileSync(filePath, "updated content");
101
- const result = await waitForMatch(events, (e) => e.path.includes("existing.txt"));
102
- const updateEvent = result.find((e) => e.path.includes("existing.txt"));
103
- expect(updateEvent).toBeDefined();
104
- expect(["create", "update"]).toContain(updateEvent?.type);
105
- });
106
- it("detects file deletion", async () => {
107
- const filePath = join(tempDir, "to-delete.txt");
108
- writeFileSync(filePath, "will be deleted");
109
- const events = [];
110
- watcher = createNativeWatcher({
111
- projectRoot: tempDir,
112
- debounceMs: 30,
113
- onEvents: (batch) => events.push(...batch),
114
- });
115
- await startAndDrain(watcher, events);
116
- unlinkSync(filePath);
117
- const result = await waitForMatch(events, (e) => e.path.includes("to-delete.txt"));
118
- const deleteEvent = result.find((e) => e.path.includes("to-delete.txt"));
119
- expect(deleteEvent).toBeDefined();
120
- expect(deleteEvent?.type).toBe("delete");
121
- });
122
- it("debounces rapid writes into a single batch", async () => {
123
- const batches = [];
124
- watcher = createNativeWatcher({
125
- projectRoot: tempDir,
126
- debounceMs: 150,
127
- onEvents: (batch) => batches.push([...batch]),
128
- });
129
- await watcher.start();
130
- await delay(500);
131
- batches.length = 0;
132
- for (let i = 0; i < 5; i++) {
133
- writeFileSync(join(tempDir, `rapid-${i}.txt`), `content-${i}`);
134
- }
135
- await delay(500);
136
- expect(batches.length).toBeLessThanOrEqual(2);
137
- const allEvents = batches.flat();
138
- expect(allEvents.length).toBeGreaterThanOrEqual(1);
139
- });
140
- it("ignores .git directory changes", async () => {
141
- const gitDir = join(tempDir, ".git");
142
- mkdirSync(gitDir, { recursive: true });
143
- const events = [];
144
- watcher = createNativeWatcher({
145
- projectRoot: tempDir,
146
- debounceMs: 30,
147
- onEvents: (batch) => events.push(...batch),
148
- });
149
- await startAndDrain(watcher, events);
150
- writeFileSync(join(gitDir, "HEAD"), "ref: refs/heads/main");
151
- writeFileSync(join(tempDir, "tracked.txt"), "should see this");
152
- const result = await waitForMatch(events, (e) => e.path.includes("tracked.txt"));
153
- const gitEvents = result.filter((e) => e.path.includes("/.git/") || e.path.endsWith("/.git"));
154
- expect(gitEvents.length).toBe(0);
155
- const trackedEvent = result.find((e) => e.path.includes("tracked.txt"));
156
- expect(trackedEvent).toBeDefined();
157
- });
158
- it("ignores node_modules directory changes", async () => {
159
- const nmDir = join(tempDir, "node_modules", "some-pkg");
160
- mkdirSync(nmDir, { recursive: true });
161
- const events = [];
162
- watcher = createNativeWatcher({
163
- projectRoot: tempDir,
164
- debounceMs: 30,
165
- onEvents: (batch) => events.push(...batch),
166
- });
167
- await startAndDrain(watcher, events);
168
- writeFileSync(join(nmDir, "index.js"), "module.exports = {}");
169
- writeFileSync(join(tempDir, "src-file.ts"), "export const x = 1;");
170
- const result = await waitForMatch(events, (e) => e.path.includes("src-file.ts"));
171
- const nmEvents = result.filter((e) => e.path.includes("node_modules"));
172
- expect(nmEvents.length).toBe(0);
173
- const srcEvent = result.find((e) => e.path.includes("src-file.ts"));
174
- expect(srcEvent).toBeDefined();
175
- });
176
- it("stops cleanly and flushes pending events", async () => {
177
- const events = [];
178
- watcher = createNativeWatcher({
179
- projectRoot: tempDir,
180
- debounceMs: 5000,
181
- onEvents: (batch) => events.push(...batch),
182
- });
183
- await startAndDrain(watcher, events);
184
- writeFileSync(join(tempDir, "pending.txt"), "content");
185
- await delay(300);
186
- await watcher.stop();
187
- expect(watcher.isRunning()).toBe(false);
188
- const pendingEvent = events.find((e) => e.path.includes("pending.txt"));
189
- expect(pendingEvent).toBeDefined();
190
- });
191
- });
@@ -1,145 +0,0 @@
1
- import { beforeEach, describe, expect, it } from "vitest";
2
- import { resetHookDedup } from "../hooks/hook-dedup.js";
3
- import { runPostReadHook, runPreEditHook, runPreReadHook, } from "../hooks/navigation-hooks.js";
4
- /**
5
- * Tests for agent-aware navigation hook behavior.
6
- *
7
- * Claude Code hooks detect via `hook_event_name` field.
8
- * Cursor hooks detect via `tool_name` + `cwd` fields (no hook_event_name).
9
- * Cline hooks detect via `tool` + `params` + `event` fields.
10
- *
11
- * The key behavioral difference: Read-before-Edit warnings are Claude Code only.
12
- */
13
- // ── Helpers ──────────────────────────────────────────────────────────
14
- function claudeCodePayload(toolInput) {
15
- return JSON.stringify({
16
- hook_event_name: "PreToolUse",
17
- tool_input: toolInput,
18
- });
19
- }
20
- function cursorPayload(toolInput) {
21
- return JSON.stringify({
22
- tool_name: "Read",
23
- tool_input: toolInput,
24
- cwd: "/project",
25
- });
26
- }
27
- function clinePayload(path) {
28
- return JSON.stringify({
29
- tool: "read_file",
30
- params: { path },
31
- event: "pre_tool",
32
- });
33
- }
34
- // ── preReadHook: Claude Code ─────────────────────────────────────────
35
- describe("preReadHook — Claude Code", () => {
36
- it("passthrough for targeted Read (offset/limit)", () => {
37
- const result = JSON.parse(runPreReadHook(claudeCodePayload({ file_path: "src/foo.ts", offset: 10, limit: 20 })));
38
- // Empty object = passthrough (no nudge)
39
- expect(result).toEqual({});
40
- });
41
- it("nudges for full-file Read (no offset/limit)", () => {
42
- const result = JSON.parse(runPreReadHook(claudeCodePayload({ file_path: "src/foo.ts" })));
43
- const msg = result.hookSpecificOutput?.systemMessage ?? "";
44
- expect(msg).toContain("ONLY for the Edit workflow");
45
- expect(msg).toContain("offset/limit");
46
- expect(msg).toContain("file_read");
47
- });
48
- it("still nudges for non-code files (preRead has no isCodeFile gate)", () => {
49
- const result = JSON.parse(runPreReadHook(claudeCodePayload({ file_path: "README.md" })));
50
- const msg = result.hookSpecificOutput?.systemMessage ?? "";
51
- expect(msg).toContain("file_read");
52
- });
53
- });
54
- // ── preReadHook: Non-Claude Code (Cursor) ────────────────────────────
55
- describe("preReadHook — Cursor (non-Claude Code)", () => {
56
- it("nudges toward file_read (no Edit workflow mention)", () => {
57
- const result = JSON.parse(runPreReadHook(cursorPayload({ file_path: "src/foo.ts" })));
58
- // Cursor adapter uses `agent_message` at root level for pre-tool-use nudges
59
- const msg = result.agent_message ?? "";
60
- // Should NOT mention Edit workflow or offset/limit requirement
61
- expect(msg).not.toContain("ONLY for the Edit workflow");
62
- // Should mention file_read as the preferred tool
63
- expect(msg).toContain("file_read");
64
- });
65
- it("still nudges even with offset/limit (Cursor doesn't need targeted Read)", () => {
66
- const result = JSON.parse(runPreReadHook(cursorPayload({ file_path: "src/foo.ts", offset: 10, limit: 20 })));
67
- // Cursor adapter uses `agent_message` for pre-tool-use nudges
68
- const msg = result.agent_message ?? "";
69
- // Non-Claude Code: always nudge toward file_read, even with offset/limit
70
- expect(msg).toContain("file_read");
71
- });
72
- });
73
- // ── preEditHook: Claude Code ─────────────────────────────────────────
74
- describe("preEditHook — Claude Code", () => {
75
- it("includes Read prerequisite warning", () => {
76
- const result = JSON.parse(runPreEditHook(claudeCodePayload({
77
- file_path: "src/foo.ts",
78
- old_string: "const x = 1",
79
- new_string: "const x = 2",
80
- })));
81
- const msg = result.hookSpecificOutput?.systemMessage ?? "";
82
- expect(msg).toContain("CRITICAL: Edit REQUIRES built-in Read");
83
- expect(msg).toContain("file_read (MCP) does NOT satisfy this");
84
- });
85
- it("includes blast radius warning for signature changes", () => {
86
- const result = JSON.parse(runPreEditHook(claudeCodePayload({
87
- file_path: "src/foo.ts",
88
- old_string: "export function doSomething(x: number)",
89
- new_string: "export function doSomething(x: string)",
90
- })));
91
- const msg = result.hookSpecificOutput?.systemMessage ?? "";
92
- expect(msg).toContain("CRITICAL: Edit REQUIRES built-in Read");
93
- expect(msg).toContain("function/class signature");
94
- expect(msg).toContain("get_references");
95
- });
96
- });
97
- // ── preEditHook: Non-Claude Code (Cursor) ────────────────────────────
98
- describe("preEditHook — Cursor (non-Claude Code)", () => {
99
- it("does NOT include Read prerequisite warning", () => {
100
- const stdin = JSON.stringify({
101
- tool_name: "Edit",
102
- tool_input: {
103
- file_path: "src/foo.ts",
104
- old_string: "const x = 1",
105
- new_string: "const x = 2",
106
- },
107
- cwd: "/project",
108
- });
109
- const result = JSON.parse(runPreEditHook(stdin));
110
- // Cursor adapter uses `agent_message` at root level for pre-tool-use nudges
111
- const msg = result.agent_message ?? "";
112
- // Should NOT mention Edit requires Read
113
- expect(msg).not.toContain("CRITICAL: Edit REQUIRES built-in Read");
114
- expect(msg).not.toContain("file_read (MCP) does NOT satisfy this");
115
- // Should still mention get_references for blast radius
116
- expect(msg).toContain("get_references");
117
- });
118
- });
119
- // ── postReadHook: Claude Code vs Cursor ──────────────────────────────
120
- describe("postReadHook — agent-aware enrichment", () => {
121
- beforeEach(() => {
122
- resetHookDedup();
123
- });
124
- it("Claude Code: mentions Edit workflow", () => {
125
- const result = JSON.parse(runPostReadHook(claudeCodePayload({ file_path: "src/post-read-cc.ts" })));
126
- const msg = result.hookSpecificOutput?.additionalContext ?? "";
127
- expect(msg).toContain("Edit needs built-in Read first");
128
- expect(msg).toContain("file_read");
129
- });
130
- it("Cursor: generic file_read suggestion (no Edit mention)", () => {
131
- // Cursor postToolUse payload includes tool_output to distinguish from preToolUse
132
- // Use a unique file path to avoid dedup collision with the Claude Code test above
133
- const stdin = JSON.stringify({
134
- tool_name: "Read",
135
- tool_input: { file_path: "src/post-read-cursor.ts" },
136
- tool_output: "file contents here",
137
- cwd: "/project",
138
- });
139
- const result = JSON.parse(runPostReadHook(stdin));
140
- // Cursor adapter uses `additional_context` at root level for post-tool-use enrichment
141
- const msg = result.additional_context ?? "";
142
- expect(msg).not.toContain("Edit needs built-in Read first");
143
- expect(msg).toContain("file_read");
144
- });
145
- });
@@ -1,116 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { detectAntiPatterns, detectInstableEntities, } from "../intelligence/negative-knowledge.js";
3
- describe("detectAntiPatterns", () => {
4
- it("detects modified-then-reverted pattern", () => {
5
- const entries = [
6
- {
7
- id: "target",
8
- ts: "2026-04-30T10:00:00Z",
9
- tool: "sync_local_diff",
10
- args_summary: { files: ["src/ok.ts"] },
11
- result_summary: {},
12
- },
13
- {
14
- id: "bad1",
15
- ts: "2026-04-30T10:01:00Z",
16
- tool: "sync_local_diff",
17
- args_summary: { files: ["src/bad.ts"] },
18
- result_summary: {},
19
- },
20
- {
21
- id: "bad2",
22
- ts: "2026-04-30T10:02:00Z",
23
- tool: "sync_local_diff",
24
- args_summary: { files: ["src/worse.ts"] },
25
- result_summary: {},
26
- },
27
- {
28
- id: "rewind",
29
- ts: "2026-04-30T10:05:00Z",
30
- tool: "revert_to_working_state",
31
- args_summary: {},
32
- result_summary: { rewind_target_id: "target" },
33
- },
34
- ];
35
- const corrections = detectAntiPatterns(entries, "rewind");
36
- expect(corrections.length).toBe(2);
37
- expect(corrections[0]?.entityKey).toBe("src/bad.ts");
38
- expect(corrections[0]?.pattern).toBe("modified-then-reverted");
39
- expect(corrections[1]?.entityKey).toBe("src/worse.ts");
40
- });
41
- it("returns empty when rewind not found", () => {
42
- const entries = [
43
- {
44
- id: "e1",
45
- ts: "2026-04-30T10:00:00Z",
46
- tool: "sync_local_diff",
47
- args_summary: { files: ["a.ts"] },
48
- result_summary: {},
49
- },
50
- ];
51
- expect(detectAntiPatterns(entries, "nonexistent")).toEqual([]);
52
- });
53
- it("returns empty when no target entry found", () => {
54
- const entries = [
55
- {
56
- id: "rewind",
57
- ts: "2026-04-30T10:00:00Z",
58
- tool: "revert_to_working_state",
59
- args_summary: {},
60
- result_summary: { rewind_target_id: "missing" },
61
- },
62
- ];
63
- expect(detectAntiPatterns(entries, "rewind")).toEqual([]);
64
- });
65
- });
66
- describe("detectInstableEntities", () => {
67
- it("detects entities modified 3+ times rapidly", () => {
68
- const now = Date.now();
69
- const entries = [
70
- {
71
- id: "e1",
72
- ts: new Date(now - 5 * 60000).toISOString(),
73
- tool: "sync_local_diff",
74
- args_summary: { files: ["src/flaky.ts"] },
75
- result_summary: {},
76
- },
77
- {
78
- id: "e2",
79
- ts: new Date(now - 3 * 60000).toISOString(),
80
- tool: "sync_local_diff",
81
- args_summary: { files: ["src/flaky.ts"] },
82
- result_summary: {},
83
- },
84
- {
85
- id: "e3",
86
- ts: new Date(now - 1 * 60000).toISOString(),
87
- tool: "sync_local_diff",
88
- args_summary: { files: ["src/flaky.ts"] },
89
- result_summary: {},
90
- },
91
- ];
92
- const corrections = detectInstableEntities(entries);
93
- expect(corrections.length).toBe(1);
94
- expect(corrections[0]?.entityKey).toBe("src/flaky.ts");
95
- expect(corrections[0]?.pattern).toBe("rapid-modification");
96
- });
97
- it("ignores stable entities", () => {
98
- const entries = [
99
- {
100
- id: "e1",
101
- ts: "2026-04-01T10:00:00Z",
102
- tool: "sync_local_diff",
103
- args_summary: { files: ["src/stable.ts"] },
104
- result_summary: {},
105
- },
106
- {
107
- id: "e2",
108
- ts: "2026-04-15T10:00:00Z",
109
- tool: "sync_local_diff",
110
- args_summary: { files: ["src/stable.ts"] },
111
- result_summary: {},
112
- },
113
- ];
114
- expect(detectInstableEntities(entries)).toEqual([]);
115
- });
116
- });