@herbertgao/pi-extensions 2026.8.14 → 2026.8.15

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 (231) hide show
  1. package/README.md +16 -13
  2. package/THIRD_PARTY_NOTICES.md +0 -1
  3. package/node_modules/@czottmann/pi-automode/CHANGELOG.md +8 -1
  4. package/node_modules/@czottmann/pi-automode/README.md +4 -0
  5. package/node_modules/@czottmann/pi-automode/extensions/auto-mode/classifier.ts +71 -9
  6. package/node_modules/@czottmann/pi-automode/package.json +1 -1
  7. package/node_modules/@herbertgao/pi-bark/LICENSE +21 -0
  8. package/node_modules/@herbertgao/pi-bark/README.md +44 -0
  9. package/node_modules/@herbertgao/pi-bark/assets/pi-icon.png +0 -0
  10. package/node_modules/@herbertgao/pi-bark/package.json +53 -0
  11. package/node_modules/@herbertgao/pi-bark/src/index.ts +178 -0
  12. package/node_modules/@juicesharp/rpiv-ask-user-question/package.json +2 -2
  13. package/node_modules/@narumitw/pi-btw/README.md +125 -88
  14. package/node_modules/@narumitw/pi-btw/dist/index.ts +140 -45
  15. package/node_modules/@narumitw/pi-btw/dist/index.ts.map +2 -2
  16. package/node_modules/@narumitw/pi-btw/package.json +5 -8
  17. package/node_modules/@narumitw/pi-btw/src/btw.ts +31 -26
  18. package/node_modules/@narumitw/pi-btw/src/fullscreen-ui.ts +106 -17
  19. package/node_modules/@narumitw/pi-btw/src/menu.ts +34 -2
  20. package/node_modules/@narumitw/pi-btw/src/settings.ts +16 -0
  21. package/node_modules/@tifan/pi-handoff/README.md +36 -13
  22. package/node_modules/@tifan/pi-handoff/package.json +2 -2
  23. package/node_modules/@tifan/pi-handoff/src/index.ts +160 -4
  24. package/node_modules/@tifan/pi-recap/README.md +1 -1
  25. package/node_modules/@tifan/pi-recap/package.json +2 -2
  26. package/node_modules/@tifan/pi-recap/src/index.ts +15 -0
  27. package/node_modules/@tifan/pi-rename/README.md +42 -21
  28. package/node_modules/@tifan/pi-rename/package.json +1 -1
  29. package/node_modules/@tifan/pi-rename/src/index.ts +62 -11
  30. package/node_modules/@tifan/pi-rename/src/language.ts +18 -0
  31. package/node_modules/@tifan/pi-rename/src/models.ts +69 -13
  32. package/node_modules/@tifan/pi-rename/src/naming.ts +36 -7
  33. package/node_modules/@tifan/pi-rename/src/sanitize.ts +22 -14
  34. package/node_modules/pi-lens/CHANGELOG.md +320 -0
  35. package/node_modules/pi-lens/config/dependency-cruiser-eager-allowlist.json +79 -0
  36. package/node_modules/pi-lens/dist/clients/actionable-warnings-logger.js +16 -1
  37. package/node_modules/pi-lens/dist/clients/ast-grep-client.js +13 -6
  38. package/node_modules/pi-lens/dist/clients/availability-probe-flight.js +12 -0
  39. package/node_modules/pi-lens/dist/clients/biome-client.js +7 -0
  40. package/node_modules/pi-lens/dist/clients/blocker-freshness.js +20 -0
  41. package/node_modules/pi-lens/dist/clients/bounded-telemetry.js +56 -19
  42. package/node_modules/pi-lens/dist/clients/build-identity.js +95 -0
  43. package/node_modules/pi-lens/dist/clients/bus-publish.js +3 -0
  44. package/node_modules/pi-lens/dist/clients/cache/rule-cache.js +2 -1
  45. package/node_modules/pi-lens/dist/clients/cache-observability.js +2 -1
  46. package/node_modules/pi-lens/dist/clients/cascade-logger.js +15 -1
  47. package/node_modules/pi-lens/dist/clients/dead-code-client.js +14 -11
  48. package/node_modules/pi-lens/dist/clients/degradation-ledger.js +56 -4
  49. package/node_modules/pi-lens/dist/clients/demoted-finding-render.js +14 -0
  50. package/node_modules/pi-lens/dist/clients/dependency-checker.js +20 -3
  51. package/node_modules/pi-lens/dist/clients/deps/minimatch.js +1 -1
  52. package/node_modules/pi-lens/dist/clients/dispatch/collect-later-tier.js +39 -0
  53. package/node_modules/pi-lens/dist/clients/dispatch/dispatcher.js +150 -9
  54. package/node_modules/pi-lens/dist/clients/dispatch/fact-store.js +231 -9
  55. package/node_modules/pi-lens/dist/clients/dispatch/integration.js +205 -100
  56. package/node_modules/pi-lens/dist/clients/dispatch/pending-runner-findings.js +87 -0
  57. package/node_modules/pi-lens/dist/clients/dispatch/plan.js +21 -5
  58. package/node_modules/pi-lens/dist/clients/dispatch/runners/ast-grep-napi.js +174 -55
  59. package/node_modules/pi-lens/dist/clients/dispatch/runners/eslint.js +5 -1
  60. package/node_modules/pi-lens/dist/clients/dispatch/runners/helm-lint.js +16 -3
  61. package/node_modules/pi-lens/dist/clients/dispatch/runners/helm-render.js +54 -9
  62. package/node_modules/pi-lens/dist/clients/dispatch/runners/index.js +1 -3
  63. package/node_modules/pi-lens/dist/clients/dispatch/runners/lsp.js +37 -1
  64. package/node_modules/pi-lens/dist/clients/dispatch/runners/markdownlint.js +4 -1
  65. package/node_modules/pi-lens/dist/clients/dispatch/runners/oxlint.js +11 -4
  66. package/node_modules/pi-lens/dist/clients/dispatch/runners/psscriptanalyzer.js +4 -0
  67. package/node_modules/pi-lens/dist/clients/dispatch/runners/rust-clippy.js +8 -3
  68. package/node_modules/pi-lens/dist/clients/dispatch/runners/utils/runner-helpers.js +76 -25
  69. package/node_modules/pi-lens/dist/clients/dispatch/runners/utils/spawn-outcome.js +4 -0
  70. package/node_modules/pi-lens/dist/clients/dispatch/runners/utils/tool-failure.js +19 -7
  71. package/node_modules/pi-lens/dist/clients/dispatch/runners/utils/toolchain-availability.js +7 -13
  72. package/node_modules/pi-lens/dist/clients/dispatch/runners/yaml-rule-parser.js +51 -17
  73. package/node_modules/pi-lens/dist/clients/event-loop-monitor.js +66 -1
  74. package/node_modules/pi-lens/dist/clients/file-utils.js +157 -7
  75. package/node_modules/pi-lens/dist/clients/finding-delivery-gate.js +5 -0
  76. package/node_modules/pi-lens/dist/clients/formatters.js +28 -3
  77. package/node_modules/pi-lens/dist/clients/git-guard.js +197 -47
  78. package/node_modules/pi-lens/dist/clients/git-tracked-ignore.js +32 -1
  79. package/node_modules/pi-lens/dist/clients/govulncheck-client.js +5 -1
  80. package/node_modules/pi-lens/dist/clients/gzip-stage-write.js +7 -0
  81. package/node_modules/pi-lens/dist/clients/installer/index.js +131 -34
  82. package/node_modules/pi-lens/dist/clients/installer/managed-tool-refresh.js +3 -1
  83. package/node_modules/pi-lens/dist/clients/instance-reaper.js +4 -13
  84. package/node_modules/pi-lens/dist/clients/instance-registry-lock.js +145 -0
  85. package/node_modules/pi-lens/dist/clients/instance-registry.js +432 -122
  86. package/node_modules/pi-lens/dist/clients/jscpd-client.js +6 -1
  87. package/node_modules/pi-lens/dist/clients/language-profile.js +4 -2
  88. package/node_modules/pi-lens/dist/clients/latency-logger.js +19 -1
  89. package/node_modules/pi-lens/dist/clients/lens-flag-registry.js +8 -0
  90. package/node_modules/pi-lens/dist/clients/lens-map.js +3 -1
  91. package/node_modules/pi-lens/dist/clients/lsp/cascade-tier.js +4 -0
  92. package/node_modules/pi-lens/dist/clients/lsp/client.js +516 -31
  93. package/node_modules/pi-lens/dist/clients/lsp/config.js +25 -2
  94. package/node_modules/pi-lens/dist/clients/lsp/diagnostic-binding.js +9 -4
  95. package/node_modules/pi-lens/dist/clients/lsp/index.js +509 -182
  96. package/node_modules/pi-lens/dist/clients/lsp/jvm-runtime.js +5 -0
  97. package/node_modules/pi-lens/dist/clients/lsp/launch.js +36 -8
  98. package/node_modules/pi-lens/dist/clients/lsp/pending-aux-coverage.js +116 -4
  99. package/node_modules/pi-lens/dist/clients/lsp/server.js +142 -13
  100. package/node_modules/pi-lens/dist/clients/lsp/session-roots.js +99 -0
  101. package/node_modules/pi-lens/dist/clients/lsp/workspace-diagnostics-cache.js +16 -6
  102. package/node_modules/pi-lens/dist/clients/lsp/workspace-sweep-hold.js +32 -23
  103. package/node_modules/pi-lens/dist/clients/lsp-mutation.js +16 -2
  104. package/node_modules/pi-lens/dist/clients/mcp/analyze.js +12 -6
  105. package/node_modules/pi-lens/dist/clients/mcp/session.js +14 -1
  106. package/node_modules/pi-lens/dist/clients/memory-sampler.js +23 -1
  107. package/node_modules/pi-lens/dist/clients/message-end-attribution.js +30 -0
  108. package/node_modules/pi-lens/dist/clients/metrics-history.js +21 -4
  109. package/node_modules/pi-lens/dist/clients/opaque-mutation-scan.js +232 -18
  110. package/node_modules/pi-lens/dist/clients/package-manager.js +6 -18
  111. package/node_modules/pi-lens/dist/clients/path-keyed-map.js +14 -1
  112. package/node_modules/pi-lens/dist/clients/path-utils.js +32 -0
  113. package/node_modules/pi-lens/dist/clients/pipeline.js +0 -8
  114. package/node_modules/pi-lens/dist/clients/process-singletons.js +210 -0
  115. package/node_modules/pi-lens/dist/clients/project-diagnostics/runner-adapters/madge.js +2 -1
  116. package/node_modules/pi-lens/dist/clients/project-diagnostics/scanner.js +10 -4
  117. package/node_modules/pi-lens/dist/clients/project-snapshot.js +33 -11
  118. package/node_modules/pi-lens/dist/clients/read-guard-logger.js +24 -2
  119. package/node_modules/pi-lens/dist/clients/read-guard.js +74 -7
  120. package/node_modules/pi-lens/dist/clients/recent-touches.js +17 -5
  121. package/node_modules/pi-lens/dist/clients/review-graph/builder.js +639 -62
  122. package/node_modules/pi-lens/dist/clients/review-graph/tsconfig-paths.js +5 -1
  123. package/node_modules/pi-lens/dist/clients/review-graph-logger.js +21 -1
  124. package/node_modules/pi-lens/dist/clients/runtime-coordinator.js +68 -0
  125. package/node_modules/pi-lens/dist/clients/runtime-session.js +30 -12
  126. package/node_modules/pi-lens/dist/clients/runtime-tool-call.js +15 -0
  127. package/node_modules/pi-lens/dist/clients/runtime-tool-result.js +106 -7
  128. package/node_modules/pi-lens/dist/clients/runtime-turn.js +242 -27
  129. package/node_modules/pi-lens/dist/clients/safe-spawn.js +158 -18
  130. package/node_modules/pi-lens/dist/clients/security-scan-client.js +48 -17
  131. package/node_modules/pi-lens/dist/clients/session-lifecycle.js +238 -42
  132. package/node_modules/pi-lens/dist/clients/sg-runner.js +27 -15
  133. package/node_modules/pi-lens/dist/clients/shared-checkout-guard.js +338 -0
  134. package/node_modules/pi-lens/dist/clients/spawn-output-cap.js +37 -0
  135. package/node_modules/pi-lens/dist/clients/spawn-timeout-cooldown.js +15 -1
  136. package/node_modules/pi-lens/dist/clients/startup-scan.js +6 -7
  137. package/node_modules/pi-lens/dist/clients/startup-timing.js +69 -4
  138. package/node_modules/pi-lens/dist/clients/string-utils.js +20 -0
  139. package/node_modules/pi-lens/dist/clients/test-runner-client.js +419 -88
  140. package/node_modules/pi-lens/dist/clients/tool-policy.js +1 -1
  141. package/node_modules/pi-lens/dist/clients/tree-sitter-client.js +11 -5
  142. package/node_modules/pi-lens/dist/clients/tree-sitter-logger.js +17 -1
  143. package/node_modules/pi-lens/dist/clients/warm-attach.js +8 -10
  144. package/node_modules/pi-lens/dist/clients/word-index-logger.js +13 -1
  145. package/node_modules/pi-lens/dist/clients/word-index-store.js +448 -0
  146. package/node_modules/pi-lens/dist/clients/word-index.js +582 -104
  147. package/node_modules/pi-lens/dist/clients/workspace-topology.js +17 -0
  148. package/node_modules/pi-lens/dist/clients/zizmor-config.js +26 -6
  149. package/node_modules/pi-lens/dist/index.js +11243 -6651
  150. package/node_modules/pi-lens/dist/mcp/build-staleness.js +8 -7
  151. package/node_modules/pi-lens/dist/mcp/cli.js +3 -1
  152. package/node_modules/pi-lens/dist/mcp/server.js +6 -1
  153. package/node_modules/pi-lens/dist/tools/lens-diagnostics.js +4 -0
  154. package/node_modules/pi-lens/dist/tools/lsp-diagnostics.js +14 -5
  155. package/node_modules/pi-lens/dist/tools/lsp-navigation.js +3 -1
  156. package/node_modules/pi-lens/docs/agent-guide.md +4 -2
  157. package/node_modules/pi-lens/docs/ast-grep_rules_catalog.md +41 -10
  158. package/node_modules/pi-lens/docs/astplayground.md +26 -17
  159. package/node_modules/pi-lens/package.json +5 -1
  160. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/c-no-malloc-free-test.yml +20 -0
  161. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/cpp-no-malloc-free-test.yml +25 -0
  162. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/cpp-no-printf-test.yml +19 -0
  163. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/go-no-fmt-println-test.yml +19 -0
  164. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/go-no-panic-in-lib-test.yml +17 -0
  165. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/go-no-underscore-func-name-test.yml +18 -0
  166. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/go-prefer-errors-is-test.yml +15 -0
  167. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/go-prefer-string-builder-test.yml +21 -0
  168. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/hyphenated-svg-attribute-test.yml +11 -0
  169. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/kotlin-no-lateinit-test.yml +17 -0
  170. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/kotlin-no-nullable-boolean-test.yml +15 -0
  171. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/kotlin-no-println-test.yml +19 -0
  172. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/kotlin-no-unnecessary-let-test.yml +7 -0
  173. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/kotlin-no-unsafe-cast-test.yml +7 -0
  174. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/kotlin-no-var-test.yml +7 -0
  175. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/kotlin-prefer-data-class-test.yml +25 -0
  176. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/kotlin-prefer-is-empty-test.yml +11 -0
  177. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/no-important-test.yml +21 -0
  178. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/no-raw-types-test.yml +30 -0
  179. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/no-string-concat-in-loop-test.yml +100 -0
  180. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/no-system-out-println-test.yml +7 -0
  181. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/prefer-string-is-empty-test.yml +7 -0
  182. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/python-optional-type-test.yml +9 -0
  183. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/reducible-list-comprehension-test.yml +11 -0
  184. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/ruby-symbol-to-proc-candidate-test.yml +10 -0
  185. package/node_modules/pi-lens/rules/ast-grep-rules/rule-tests/rust-no-chars-enumerate-test.yml +7 -0
  186. package/node_modules/pi-lens/rules/ast-grep-rules/rules/c-no-malloc-free.yml +39 -0
  187. package/node_modules/pi-lens/rules/ast-grep-rules/rules/cpp-no-malloc-free.yml +45 -0
  188. package/node_modules/pi-lens/rules/ast-grep-rules/rules/cpp-no-printf.yml +41 -0
  189. package/node_modules/pi-lens/rules/ast-grep-rules/rules/go-no-fmt-println.yml +43 -0
  190. package/node_modules/pi-lens/rules/ast-grep-rules/rules/go-no-panic-in-lib.yml +41 -0
  191. package/node_modules/pi-lens/rules/ast-grep-rules/rules/go-no-underscore-func-name.yml +35 -0
  192. package/node_modules/pi-lens/rules/ast-grep-rules/rules/go-prefer-errors-is.yml +40 -0
  193. package/node_modules/pi-lens/rules/ast-grep-rules/rules/go-prefer-string-builder.yml +43 -0
  194. package/node_modules/pi-lens/rules/ast-grep-rules/rules/hyphenated-svg-attribute.yml +32 -0
  195. package/node_modules/pi-lens/rules/ast-grep-rules/rules/kotlin-no-lateinit.yml +37 -0
  196. package/node_modules/pi-lens/rules/ast-grep-rules/rules/kotlin-no-nullable-boolean.yml +33 -0
  197. package/node_modules/pi-lens/rules/ast-grep-rules/rules/kotlin-no-println.yml +42 -0
  198. package/node_modules/pi-lens/rules/ast-grep-rules/rules/kotlin-no-unnecessary-let.yml +28 -0
  199. package/node_modules/pi-lens/rules/ast-grep-rules/rules/kotlin-no-unsafe-cast.yml +28 -0
  200. package/node_modules/pi-lens/rules/ast-grep-rules/rules/kotlin-no-var.yml +35 -0
  201. package/node_modules/pi-lens/rules/ast-grep-rules/rules/kotlin-prefer-data-class.yml +66 -0
  202. package/node_modules/pi-lens/rules/ast-grep-rules/rules/kotlin-prefer-is-empty.yml +35 -0
  203. package/node_modules/pi-lens/rules/ast-grep-rules/rules/no-important.yml +21 -0
  204. package/node_modules/pi-lens/rules/ast-grep-rules/rules/no-raw-types.yml +33 -0
  205. package/node_modules/pi-lens/rules/ast-grep-rules/rules/no-string-concat-in-loop.yml +90 -0
  206. package/node_modules/pi-lens/rules/ast-grep-rules/rules/no-system-out-println.yml +16 -0
  207. package/node_modules/pi-lens/rules/ast-grep-rules/rules/prefer-string-is-empty.yml +11 -0
  208. package/node_modules/pi-lens/rules/ast-grep-rules/rules/python-optional-type.yml +22 -0
  209. package/node_modules/pi-lens/rules/ast-grep-rules/rules/reducible-list-comprehension.yml +29 -0
  210. package/node_modules/pi-lens/rules/ast-grep-rules/rules/ruby-symbol-to-proc-candidate.yml +25 -0
  211. package/node_modules/pi-lens/rules/ast-grep-rules/rules/rust-no-chars-enumerate.yml +19 -0
  212. package/node_modules/pi-mcp-adapter/CHANGELOG.md +15 -0
  213. package/node_modules/pi-mcp-adapter/README.md +2 -2
  214. package/node_modules/pi-mcp-adapter/commands.ts +1 -1
  215. package/node_modules/pi-mcp-adapter/dist/types.d.ts +1 -1
  216. package/node_modules/pi-mcp-adapter/mcp-auth-flow.ts +105 -32
  217. package/node_modules/pi-mcp-adapter/package.json +1 -1
  218. package/node_modules/pi-mcp-adapter/proxy-modes.ts +22 -12
  219. package/node_modules/pi-mcp-adapter/server-manager.ts +6 -0
  220. package/node_modules/pi-mcp-adapter/types.ts +1 -1
  221. package/node_modules/pi-web-access/CHANGELOG.md +20 -0
  222. package/node_modules/pi-web-access/README.md +15 -2
  223. package/node_modules/pi-web-access/extract.ts +57 -12
  224. package/node_modules/pi-web-access/github-extract.ts +47 -3
  225. package/node_modules/pi-web-access/index.ts +1 -1
  226. package/node_modules/pi-web-access/package.json +1 -1
  227. package/node_modules/pi-web-access/page-query.ts +61 -8
  228. package/package.json +16 -16
  229. package/node_modules/@tifan/pi-titlebar-spinner/README.md +0 -24
  230. package/node_modules/@tifan/pi-titlebar-spinner/package.json +0 -39
  231. package/node_modules/@tifan/pi-titlebar-spinner/src/index.ts +0 -49
@@ -1,20 +1,73 @@
1
1
  import { normalizeMapKey } from "../path-utils.js";
2
+ let capacityEvictionReporter;
3
+ export function setFactStoreEvictionReporter(reporter) {
4
+ capacityEvictionReporter = reporter;
5
+ }
6
+ export function getFactStoreEvictionReporter() {
7
+ return capacityEvictionReporter;
8
+ }
9
+ // #2240: the dispatch store is module-scope in integration.ts, so it lives for
10
+ // the whole process, and a review-graph project walk seeds facts for EVERY file
11
+ // it visits. Nothing removed an entry except the next dispatch of that same
12
+ // path, so a several-hundred-file batch retained one entry per distinct path
13
+ // until the heap ran out. Bound the record count the way widget-state.ts bounds
14
+ // its file records.
15
+ const MAX_FILE_FACT_RECORDS = 1024;
16
+ // Keep enough content for 1024 typical source files averaging 64 KiB, while
17
+ // preventing a small number of generated or vendored files from retaining
18
+ // hundreds of MiB for the process lifetime (#2247).
19
+ const MAX_FILE_FACT_CONTENT_BYTES = 64 * 1024 * 1024;
2
20
  export class FactStore {
21
+ subject;
22
+ // Insertion order is eviction order: the oldest-used record is evicted first.
3
23
  fileFacts = new Map();
4
24
  sessionFacts = new Map();
25
+ // Pin refcount per file. A file is exempt from capacity eviction while its
26
+ // count is > 0. Refcounted, not a Set, so two overlapping dispatches of the
27
+ // same file both hold the pin until BOTH settle (#2243 item 2).
28
+ pinnedFiles = new Map();
29
+ // Running UTF-8 byte total for file.content only. Maintaining it at every
30
+ // mutation keeps getFileFact/setFileFact O(1) amortized; never scan the map
31
+ // to decide whether the byte budget is exceeded.
32
+ retainedContentBytes = 0;
33
+ // Subset of retainedContentBytes held by currently pinned files (#2247
34
+ // review F2). Maintained incrementally the same way — a scan over
35
+ // `pinnedFiles` looks small in isolation, but `evictColdFileFacts` calls it
36
+ // on EVERY unpinned insert for as long as pinned bytes stay over budget, so
37
+ // a scan there would re-walk a large pinned file's full string on every
38
+ // single write admitted during that window. Keeping a running total avoids
39
+ // reintroducing exactly the scan `retainedContentBytes` exists to avoid.
40
+ pinnedContentBytesTotal = 0;
41
+ /**
42
+ * @param subject Discriminates this store's capacity-eviction telemetry
43
+ * from every other production FactStore's (#2243 review round 3, F1).
44
+ * Production callers pass a label naming the store (e.g. `"dispatch"`,
45
+ * `"runtime-session-call-graph"`); the default is only for call sites
46
+ * that never wired a reporter and tests that don't care.
47
+ */
48
+ constructor(subject = "session-fact-store") {
49
+ this.subject = subject;
50
+ }
5
51
  // All file-keyed methods normalize the path internally via normalizeMapKey().
6
52
  // Callers always pass raw/resolved paths — normalization is not their concern.
7
53
  getFileFact(filePath, factId) {
8
- return this.fileFacts.get(normalizeMapKey(filePath))?.get(factId);
54
+ return this.touchFileFacts(normalizeMapKey(filePath))?.get(factId);
9
55
  }
10
56
  setFileFact(filePath, factId, value) {
11
57
  const key = normalizeMapKey(filePath);
12
- let facts = this.fileFacts.get(key);
58
+ let facts = this.touchFileFacts(key);
13
59
  if (!facts) {
14
60
  facts = new Map();
15
61
  this.fileFacts.set(key, facts);
16
62
  }
63
+ if (factId === "file.content") {
64
+ const delta = this.contentBytes(value) - this.contentBytes(facts.get(factId));
65
+ this.retainedContentBytes += delta;
66
+ if (this.pinnedFiles.has(key))
67
+ this.pinnedContentBytesTotal += delta;
68
+ }
17
69
  facts.set(factId, value);
70
+ this.evictColdFileFacts();
18
71
  }
19
72
  hasFileFact(filePath, factId) {
20
73
  return this.fileFacts.get(normalizeMapKey(filePath))?.has(factId) ?? false;
@@ -26,20 +79,186 @@ export class FactStore {
26
79
  const facts = this.fileFacts.get(key);
27
80
  if (!facts)
28
81
  return;
82
+ if (factId === "file.content") {
83
+ const dropped = this.contentBytes(facts.get(factId));
84
+ this.retainedContentBytes -= dropped;
85
+ if (this.pinnedFiles.has(key))
86
+ this.pinnedContentBytesTotal -= dropped;
87
+ }
29
88
  facts.delete(factId);
30
89
  if (facts.size === 0)
31
90
  this.fileFacts.delete(key);
32
91
  }
33
- /** Clear facts for one specific file only. Use at the start of each per-file dispatch call.
92
+ /** Clear facts for one specific file only, and mark the file's dispatch as
93
+ * begun. Use at the start of each per-file dispatch call.
34
94
  * Preserves facts for other files computed in the same turn.
35
- * Normalizes filePath internally — callers pass raw paths. */
95
+ * Normalizes filePath internally — callers pass raw paths.
96
+ * Pins the file against capacity eviction until the matching
97
+ * {@link endDispatchFor} runs in the dispatch's settle path (#2243 item 2).
98
+ * Every `clearFileFactsFor` MUST be paired with an `endDispatchFor`. */
36
99
  clearFileFactsFor(filePath) {
37
- this.fileFacts.delete(normalizeMapKey(filePath));
100
+ const key = normalizeMapKey(filePath);
101
+ this.deleteFileFactsRecord(key);
102
+ this.pinFile(key);
38
103
  }
39
- /** Clear all file facts across all paths. Reserve for explicit full resets only —
40
- * do NOT use in the normal per-file dispatch path. */
41
- clearFileFacts() {
42
- this.fileFacts.clear();
104
+ /** Pin a file against capacity eviction for the duration of a dispatch,
105
+ * WITHOUT clearing its existing facts. Pairs with {@link endDispatchFor}
106
+ * exactly like {@link clearFileFactsFor} does.
107
+ *
108
+ * #2243 review round 3 (F5): `clearFileFactsFor`'s clear+re-derive is
109
+ * right for a dispatch driven by a fresh edit, but the debounced
110
+ * ast-grep warning scan runs ~2s AFTER the inline dispatch for that same
111
+ * edit already re-derived every fact for this exact file — a second
112
+ * clear there bought nothing but ~51ms of redundant re-parsing across
113
+ * all five providers (measured; a warm-cache read is ~1ms). Use this
114
+ * where the facts are very likely already fresh: `runProviders` still
115
+ * re-derives any fact this file is missing (a genuine miss, e.g. the
116
+ * fact was evicted meanwhile), so a read never depends on staleness. */
117
+ beginDispatchFor(filePath) {
118
+ this.pinFile(normalizeMapKey(filePath));
119
+ }
120
+ /** Release the pin a {@link clearFileFactsFor} or {@link beginDispatchFor}
121
+ * took for one file's dispatch. Call once per matching call, from the
122
+ * dispatch's finally/settle path, so the pin set tracks dispatches
123
+ * actually in flight rather than the last N files touched (#2243 item 2). */
124
+ endDispatchFor(filePath) {
125
+ this.unpinFile(normalizeMapKey(filePath));
126
+ this.evictColdFileFacts();
127
+ }
128
+ /** Clear one file's facts WITHOUT pinning. For sequential single-store scans
129
+ * (project-diagnostics) that own their store and race no concurrent walk:
130
+ * pinning there would exempt every scanned file from the capacity cap and
131
+ * defeat it. Normalizes filePath internally. */
132
+ dropFileFacts(filePath) {
133
+ this.deleteFileFactsRecord(normalizeMapKey(filePath));
134
+ }
135
+ /** LRU touch: re-inserting an existing record moves it to the end of the
136
+ * eviction order, so a record still in use is not the next victim. */
137
+ touchFileFacts(key) {
138
+ const facts = this.fileFacts.get(key);
139
+ if (!facts)
140
+ return undefined;
141
+ this.fileFacts.delete(key);
142
+ this.fileFacts.set(key, facts);
143
+ return facts;
144
+ }
145
+ pinFile(key) {
146
+ const count = this.pinnedFiles.get(key) ?? 0;
147
+ // Transition from unpinned to pinned: this file's content joins the
148
+ // pinned subset. A refcount bump past 1 (an already-pinned file, two
149
+ // overlapping dispatches) does not — it is already counted.
150
+ if (count === 0) {
151
+ this.pinnedContentBytesTotal += this.contentBytes(this.fileFacts.get(key)?.get("file.content"));
152
+ }
153
+ this.pinnedFiles.set(key, count + 1);
154
+ }
155
+ unpinFile(key) {
156
+ const count = this.pinnedFiles.get(key);
157
+ if (count === undefined)
158
+ return;
159
+ if (count <= 1) {
160
+ this.pinnedFiles.delete(key);
161
+ // Transition from pinned to unpinned: this file's content leaves the
162
+ // pinned subset.
163
+ this.pinnedContentBytesTotal -= this.contentBytes(this.fileFacts.get(key)?.get("file.content"));
164
+ }
165
+ else {
166
+ this.pinnedFiles.set(key, count - 1);
167
+ }
168
+ }
169
+ /** Drop least-recently-used records past either cap, skipping the files whose
170
+ * dispatch pinned them. Pinned content counts toward the byte total, but is
171
+ * never evicted before endDispatchFor. Pins survive until clearAll.
172
+ *
173
+ * #2247 review F2: a leaked (or several overlapping) pin(s) can put pinned
174
+ * bytes over budget ON THEIR OWN. Evicting every unpinned record can never
175
+ * bring the total back under budget in that state — before this guard, the
176
+ * loop below evicted each newly inserted unpinned record on the very next
177
+ * call (it was always the sole remaining unpinned key), so unpinned writes
178
+ * never retained anything: a silent, permanent collapse for the rest of
179
+ * the process. Once byte pressure is unpinned-bytes-driven-only, stop
180
+ * evicting and admit unpinned inserts as-is — best-effort under pin
181
+ * pressure — and record the state distinctly so it is visible instead of
182
+ * inferred from "the store stopped retaining anything". */
183
+ evictColdFileFacts() {
184
+ if (!this.overCapacity())
185
+ return;
186
+ for (const key of this.fileFacts.keys()) {
187
+ if (this.pinnedFiles.has(key))
188
+ continue;
189
+ const axis = this.capacityAxis();
190
+ if (axis === "bytes" &&
191
+ this.pinnedContentBytesTotal > MAX_FILE_FACT_CONTENT_BYTES) {
192
+ this.reportPinnedOverBudget();
193
+ return;
194
+ }
195
+ this.deleteFileFactsRecord(key);
196
+ this.reportCapacityEviction(key, axis);
197
+ if (!this.overCapacity())
198
+ return;
199
+ }
200
+ }
201
+ overCapacity() {
202
+ return (this.fileFacts.size > MAX_FILE_FACT_RECORDS ||
203
+ this.retainedContentBytes > MAX_FILE_FACT_CONTENT_BYTES);
204
+ }
205
+ capacityAxis() {
206
+ return this.fileFacts.size > MAX_FILE_FACT_RECORDS ? "count" : "bytes";
207
+ }
208
+ contentBytes(value) {
209
+ return typeof value === "string" ? Buffer.byteLength(value, "utf8") : 0;
210
+ }
211
+ deleteFileFactsRecord(key) {
212
+ const facts = this.fileFacts.get(key);
213
+ if (!facts)
214
+ return;
215
+ const dropped = this.contentBytes(facts.get("file.content"));
216
+ this.retainedContentBytes -= dropped;
217
+ if (this.pinnedFiles.has(key))
218
+ this.pinnedContentBytesTotal -= dropped;
219
+ this.fileFacts.delete(key);
220
+ }
221
+ // #2243 item 4: the cap silently drops a fact a live dispatch may read back
222
+ // as "" (dispatcher.ts reads `file.content` with `?? ""`). Emit through the
223
+ // injected reporter so the drop is visible in the ledger instead of being
224
+ // inferred from a downstream symptom. integration.ts routes this to
225
+ // `recordDegradationOnce` keyed on THIS store's `subject` (#2243 review
226
+ // round 3, F1), so the ledger's own per-kind+subject dedupe yields exactly
227
+ // ONE record per session PER STORE (re-arming at session_start when the
228
+ // ledger resets) — no latch or generation compare here, so fact-store
229
+ // keeps no session-scoped state to forget to re-arm.
230
+ reportCapacityEviction(evictedKey, axis) {
231
+ capacityEvictionReporter?.(this.subject, axis, axis === "count"
232
+ ? `file-fact store axis=count exceeded ${MAX_FILE_FACT_RECORDS} records; evicted least-recently-used fact for ${evictedKey}`
233
+ : `file-fact store axis=bytes exceeded ${MAX_FILE_FACT_CONTENT_BYTES} retained content bytes; evicted least-recently-used fact for ${evictedKey}`);
234
+ }
235
+ // #2247 review F2: pinned bytes alone exceeding budget is not an eviction —
236
+ // nothing is dropped — so it is reported through a distinct axis rather
237
+ // than reusing `reportCapacityEviction`'s "evicted least-recently-used
238
+ // fact for X" language, which would misdescribe what happened.
239
+ reportPinnedOverBudget() {
240
+ const pinnedBytes = this.pinnedContentBytesTotal;
241
+ capacityEvictionReporter?.(this.subject, "pinned-over-budget", `file-fact store pinned content bytes (${pinnedBytes}) exceed the ` +
242
+ `${MAX_FILE_FACT_CONTENT_BYTES}-byte budget; unpinned inserts are ` +
243
+ `admitted without eviction until a pin releases`);
244
+ }
245
+ /** Running UTF-8 byte total for retained `file.content` values. Exposed so
246
+ * tests can crosscheck it against a fresh sum over retained records
247
+ * (#2247 review F3) — the running total is maintained incrementally on
248
+ * every insert/delete path rather than recomputed, so a subtraction
249
+ * dropped from one of those paths would otherwise drift silently. */
250
+ getRetainedContentBytes() {
251
+ return this.retainedContentBytes;
252
+ }
253
+ /** Running UTF-8 byte total for the pinned SUBSET of retained
254
+ * `file.content` values. Exposed so tests can crosscheck it against a
255
+ * fresh sum over `pinnedFiles ∩ fileFacts` (#2247 review F4) — like
256
+ * `retainedContentBytes`, it is maintained incrementally on the pin
257
+ * 0→1 transition, the unpin 1→0 transition, content overwrite/delete,
258
+ * and `clearAll`, so a drop from any one of those paths would
259
+ * otherwise drift silently instead of failing loudly. */
260
+ getPinnedContentBytes() {
261
+ return this.pinnedContentBytesTotal;
43
262
  }
44
263
  getSessionFact(factId) {
45
264
  return this.sessionFacts.get(factId);
@@ -54,5 +273,8 @@ export class FactStore {
54
273
  clearAll() {
55
274
  this.fileFacts.clear();
56
275
  this.sessionFacts.clear();
276
+ this.pinnedFiles.clear();
277
+ this.retainedContentBytes = 0;
278
+ this.pinnedContentBytesTotal = 0;
57
279
  }
58
280
  }