dsh-context-compression-improved 0.11.4 → 0.13.0

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 (330) hide show
  1. package/.agents/plans/improve/checklist.md +32 -0
  2. package/.agents/plans/improve/findings.md +102 -0
  3. package/.agents/plans/improve/spec.md +53 -0
  4. package/.agents/plans/improve/tasks.md +70 -0
  5. package/.gitattributes +2 -0
  6. package/.githooks/pre-push +37 -0
  7. package/.github/workflows/ci.yml +47 -0
  8. package/CHANGELOG.de.md +580 -0
  9. package/CHANGELOG.es.md +588 -0
  10. package/CHANGELOG.fr.md +576 -0
  11. package/CHANGELOG.it.md +577 -0
  12. package/CHANGELOG.ja.md +261 -0
  13. package/CHANGELOG.ko.md +254 -0
  14. package/CHANGELOG.md +582 -0
  15. package/CHANGELOG.pt.md +167 -0
  16. package/CHANGELOG.ru.md +554 -0
  17. package/CHANGELOG.zh.md +247 -0
  18. package/CONTRIBUTING.md +22 -0
  19. package/README.de.md +96 -0
  20. package/README.es.md +96 -0
  21. package/README.fr.md +96 -0
  22. package/README.it.md +96 -0
  23. package/README.ja.md +102 -0
  24. package/README.ko.md +102 -0
  25. package/README.md +121 -15
  26. package/README.pt.md +96 -0
  27. package/README.ru.md +96 -0
  28. package/README.zh.md +104 -14
  29. package/SECURITY.md +18 -0
  30. package/THIRD_PARTY_NOTICES.md +7 -25
  31. package/assets/board.png +0 -0
  32. package/assets/context.png +0 -0
  33. package/assets/dshfind-card.svg +33 -0
  34. package/docs/assets/context-compression-selector-profiles.jpg +0 -0
  35. package/docs/assets/context-compression-selector-settings.png +0 -0
  36. package/docs/compat-0.1.5-report.md +77 -0
  37. package/docs/installation.de.md +104 -0
  38. package/docs/installation.es.md +104 -0
  39. package/docs/installation.fr.md +104 -0
  40. package/docs/installation.it.md +104 -0
  41. package/docs/installation.ja.md +78 -0
  42. package/docs/installation.ko.md +78 -0
  43. package/docs/installation.md +103 -0
  44. package/docs/installation.ru.md +104 -0
  45. package/docs/installation.zh.md +100 -0
  46. package/docs/repair-log.md +657 -0
  47. package/docs/shallow-teaching-preset-template.md +46 -0
  48. package/eslint.config.js +30 -0
  49. package/package.json +112 -127
  50. package/packages/selector/LICENSE +21 -0
  51. package/packages/selector/README.md +26 -0
  52. package/packages/selector/README.zh.md +24 -0
  53. package/packages/selector/THIRD_PARTY_NOTICES.md +32 -0
  54. package/packages/selector/docs/history-tool-call-working-set-spec.md +112 -0
  55. package/packages/selector/docs/native-tool-result-selector-spec.md +34 -0
  56. package/packages/selector/docs/subagent-cache-reuse-spec.md +46 -0
  57. package/{dsh.plugin.json → packages/selector/dsh.plugin.json} +2 -2
  58. package/{lib → packages/selector/lib}/client.js +400 -0
  59. package/packages/selector/lib/style.css +350 -0
  60. package/packages/selector/package.json +180 -0
  61. package/packages/selector/screenshots.json +3 -0
  62. package/packages/selector/src/client/CompressionProfileControls.tsx +352 -0
  63. package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -0
  64. package/packages/selector/src/client/CompressionProfileSelector.tsx +89 -0
  65. package/packages/selector/src/client/CustomPolicyEditor.tsx +216 -0
  66. package/packages/selector/src/client/EstimatorControls.tsx +277 -0
  67. package/packages/selector/src/client/bridge-scope.ts +125 -0
  68. package/packages/selector/src/client/decode.ts +68 -0
  69. package/packages/selector/src/client/fab-pos.ts +71 -0
  70. package/packages/selector/src/client/guide-steps.ts +50 -0
  71. package/packages/selector/src/client/guide.tsx +368 -0
  72. package/packages/selector/src/client/index.ts +456 -0
  73. package/packages/selector/src/client/locales/de.ts +167 -0
  74. package/packages/selector/src/client/locales/es.ts +167 -0
  75. package/packages/selector/src/client/locales/fr.ts +167 -0
  76. package/packages/selector/src/client/locales/it.ts +167 -0
  77. package/packages/selector/src/client/locales/ja.ts +167 -0
  78. package/packages/selector/src/client/locales/ko.ts +167 -0
  79. package/packages/selector/src/client/locales/pt.ts +167 -0
  80. package/packages/selector/src/client/locales/ru.ts +167 -0
  81. package/packages/selector/src/client/locales.ts +334 -0
  82. package/packages/selector/src/client/monitor-fab.ts +445 -0
  83. package/packages/selector/src/client/preset-options.ts +88 -0
  84. package/packages/selector/src/client/savings-card.tsx +269 -0
  85. package/packages/selector/src/client/scope-face.ts +24 -0
  86. package/packages/selector/src/client/settings-section.tsx +171 -0
  87. package/packages/selector/src/compat/host-generation.ts +96 -0
  88. package/packages/selector/src/compat/llm-messages.ts +50 -0
  89. package/packages/selector/src/compat/settings-lease.ts +108 -0
  90. package/packages/selector/src/compat/tool-result.ts +91 -0
  91. package/packages/selector/src/css-modules.d.ts +6 -0
  92. package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -0
  93. package/packages/selector/src/estimator-catalog.ts +104 -0
  94. package/packages/selector/src/index.ts +909 -0
  95. package/packages/selector/src/invariant.ts +113 -0
  96. package/packages/selector/src/preset-overlay.ts +749 -0
  97. package/packages/selector/src/profiles.ts +554 -0
  98. package/packages/selector/src/pruner/content.ts +199 -0
  99. package/packages/selector/src/pruner/session.ts +97 -0
  100. package/packages/selector/src/pruner/state.ts +61 -0
  101. package/packages/selector/src/pruner/tuning.ts +23 -0
  102. package/packages/selector/src/pruner/types.ts +78 -0
  103. package/packages/selector/src/pruner.ts +3222 -0
  104. package/packages/selector/src/runtime/adaptive-cost.ts +205 -0
  105. package/packages/selector/src/runtime/audit.ts +321 -0
  106. package/packages/selector/src/runtime/config.ts +887 -0
  107. package/packages/selector/src/runtime/custom-policy.ts +300 -0
  108. package/packages/selector/src/runtime/deepseek-official-pricing.ts +298 -0
  109. package/packages/selector/src/runtime/deepseek-v4-vision-tokens.ts +254 -0
  110. package/packages/selector/src/runtime/display-prefs.ts +98 -0
  111. package/packages/selector/src/runtime/fold-router.ts +143 -0
  112. package/packages/selector/src/runtime/measurement.ts +444 -0
  113. package/packages/selector/src/runtime/monitor.ts +341 -0
  114. package/packages/selector/src/runtime/panel-command.ts +213 -0
  115. package/packages/selector/src/runtime/reducers.ts +1755 -0
  116. package/packages/selector/src/runtime/retrieve.ts +505 -0
  117. package/packages/selector/src/runtime/savings-integral.ts +117 -0
  118. package/packages/selector/src/runtime/savings-projection.ts +229 -0
  119. package/packages/selector/src/runtime/savings-verify.ts +446 -0
  120. package/packages/selector/src/runtime/savings.ts +526 -0
  121. package/packages/selector/src/runtime/sediment-card.ts +126 -0
  122. package/packages/selector/src/runtime/sediment-clock.ts +87 -0
  123. package/packages/selector/src/runtime/sediment-guard.ts +136 -0
  124. package/packages/selector/src/runtime/sediment-publish.ts +144 -0
  125. package/packages/selector/src/runtime/sediment-span.ts +170 -0
  126. package/packages/selector/src/runtime/sediment.ts +209 -0
  127. package/packages/selector/src/runtime/session-events.ts +30 -0
  128. package/packages/selector/src/runtime/session-file-decode.ts +523 -0
  129. package/packages/selector/src/runtime/shallow-tools.ts +342 -0
  130. package/packages/selector/src/runtime/stats-backfill.ts +541 -0
  131. package/packages/selector/src/runtime/stats-db.ts +492 -0
  132. package/packages/selector/src/runtime/stats-schema.ts +123 -0
  133. package/packages/selector/src/runtime/stats-sync.ts +307 -0
  134. package/packages/selector/src/runtime/summary-command.ts +127 -0
  135. package/packages/selector/src/runtime/tail-trim.ts +177 -0
  136. package/packages/selector/src/runtime/token-count.ts +72 -0
  137. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -0
  138. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -0
  139. package/packages/selector/src/runtime/tokenpilot/advisor.ts +419 -0
  140. package/packages/selector/src/runtime/tokenpilot/benefit.ts +200 -0
  141. package/packages/selector/src/runtime/tokenpilot/dedup.ts +82 -0
  142. package/packages/selector/src/runtime/tokenpilot/estimator.ts +151 -0
  143. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
  144. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
  145. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
  146. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
  147. package/packages/selector/src/runtime/tokenpilot/locator.ts +129 -0
  148. package/packages/selector/src/runtime/tokenpilot/read-state.ts +98 -0
  149. package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +303 -0
  150. package/packages/selector/src/runtime/tool-names.ts +42 -0
  151. package/packages/selector/src/runtime/toolclass.ts +103 -0
  152. package/packages/selector/src/runtime/triage/masks.ts +128 -0
  153. package/packages/selector/src/runtime/triage/shallow.ts +132 -0
  154. package/packages/selector/src/runtime/triage/signals.ts +167 -0
  155. package/packages/selector/src/runtime/triage/types.ts +125 -0
  156. package/packages/selector/src/runtime/types.ts +396 -0
  157. package/packages/selector/src/runtime/value.ts +48 -0
  158. package/packages/selector/src/settings-bridge.ts +175 -0
  159. package/packages/selector/src/tui-command.ts +99 -0
  160. package/packages/selector/tests/advisor-report.host.spec.ts +223 -0
  161. package/packages/selector/tests/auto-compact.client.spec.tsx +214 -0
  162. package/packages/selector/tests/built/client-artifact.spec.ts +56 -0
  163. package/packages/selector/tests/cache-prefix-audit.spec.ts +123 -0
  164. package/packages/selector/tests/code-skeleton.client.spec.ts +98 -0
  165. package/packages/selector/tests/compat/compat-narrow-waist.spec.ts +150 -0
  166. package/packages/selector/tests/custom-contract.client.spec.ts +226 -0
  167. package/packages/selector/tests/estimator-catalog.spec.ts +70 -0
  168. package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -0
  169. package/packages/selector/tests/estimator-route-registration.host.spec.ts +202 -0
  170. package/packages/selector/tests/fab-pos.client.spec.ts +47 -0
  171. package/packages/selector/tests/fixtures/sediment-pool/52adc205-cloud-5.orig.txt +231 -0
  172. package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-25.orig.txt +236 -0
  173. package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-38.orig.txt +220 -0
  174. package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-42.orig.txt +140 -0
  175. package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-cloud-53.orig.txt +117 -0
  176. package/packages/selector/tests/fixtures/sediment-pool/95fcba5d-local-10.orig.txt +67 -0
  177. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-45.orig.txt +119 -0
  178. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-49.orig.txt +119 -0
  179. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-63.orig.txt +131 -0
  180. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-75.orig.txt +196 -0
  181. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-76.orig.txt +71 -0
  182. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-85.orig.txt +126 -0
  183. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-86.orig.txt +86 -0
  184. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-87.orig.txt +50 -0
  185. package/packages/selector/tests/fixtures/sediment-pool/a7a767c0-cloud-96.orig.txt +102 -0
  186. package/packages/selector/tests/helpers/legacy-namespace.ts +35 -0
  187. package/packages/selector/tests/helpers/legacy-settings/index.ts +15 -0
  188. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.d.ts +41 -0
  189. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.js +647 -0
  190. package/packages/selector/tests/helpers/legacy-settings/legacy/redact.js +78 -0
  191. package/packages/selector/tests/helpers/legacy-settings/legacy/util-values.js +234 -0
  192. package/packages/selector/tests/host-preset-overlay.host.spec.ts +204 -0
  193. package/packages/selector/tests/monitor-fab.client.spec.ts +240 -0
  194. package/packages/selector/tests/preset-options-write.client.spec.ts +192 -0
  195. package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +200 -0
  196. package/packages/selector/tests/preset-overlay.host.spec.ts +246 -0
  197. package/packages/selector/tests/profiles.client.spec.tsx +436 -0
  198. package/packages/selector/tests/ptc-custom-write.client.spec.ts +86 -0
  199. package/packages/selector/tests/public/package-contract.client.spec.ts +91 -0
  200. package/packages/selector/tests/runtime/adaptive-cost.spec.ts +167 -0
  201. package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +230 -0
  202. package/packages/selector/tests/runtime/advisor-invariant.spec.ts +272 -0
  203. package/packages/selector/tests/runtime/advisor.spec.ts +226 -0
  204. package/packages/selector/tests/runtime/audit.spec.ts +275 -0
  205. package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -0
  206. package/packages/selector/tests/runtime/behavior-lock-profiles.spec.ts +128 -0
  207. package/packages/selector/tests/runtime/char-basis.spec.ts +30 -0
  208. package/packages/selector/tests/runtime/code-skeleton.spec.ts +152 -0
  209. package/packages/selector/tests/runtime/deepseek-official-pricing.spec.ts +186 -0
  210. package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -0
  211. package/packages/selector/tests/runtime/deepseek-v4-vision-tokens.spec.ts +122 -0
  212. package/packages/selector/tests/runtime/deprecated-preset-options.spec.ts +96 -0
  213. package/packages/selector/tests/runtime/fallback-diversity.spec.ts +75 -0
  214. package/packages/selector/tests/runtime/fixtures/profile-baseline.json +273 -0
  215. package/packages/selector/tests/runtime/fixtures/session-v4-dump.jsonl +46 -0
  216. package/packages/selector/tests/runtime/fixtures/tokenizer-golden.json +106 -0
  217. package/packages/selector/tests/runtime/fixtures/vision-golden.json +459 -0
  218. package/packages/selector/tests/runtime/fold-router.spec.ts +140 -0
  219. package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -0
  220. package/packages/selector/tests/runtime/fresh-gate.spec.ts +43 -0
  221. package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -0
  222. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
  223. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
  224. package/packages/selector/tests/runtime/jsonl-reducer.spec.ts +79 -0
  225. package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -0
  226. package/packages/selector/tests/runtime/monitor.spec.ts +354 -0
  227. package/packages/selector/tests/runtime/panel-command.spec.ts +101 -0
  228. package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -0
  229. package/packages/selector/tests/runtime/ptc-profile.spec.ts +133 -0
  230. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2795 -0
  231. package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -0
  232. package/packages/selector/tests/runtime/savings-accounting.spec.ts +291 -0
  233. package/packages/selector/tests/runtime/savings-integral.spec.ts +169 -0
  234. package/packages/selector/tests/runtime/savings-projection.spec.ts +89 -0
  235. package/packages/selector/tests/runtime/savings-verify.spec.ts +226 -0
  236. package/packages/selector/tests/runtime/savings.spec.ts +139 -0
  237. package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -0
  238. package/packages/selector/tests/runtime/sediment-card.spec.ts +111 -0
  239. package/packages/selector/tests/runtime/sediment-config.spec.ts +56 -0
  240. package/packages/selector/tests/runtime/sediment-guard.spec.ts +71 -0
  241. package/packages/selector/tests/runtime/sediment-masks.spec.ts +95 -0
  242. package/packages/selector/tests/runtime/sediment-orchestrator.spec.ts +166 -0
  243. package/packages/selector/tests/runtime/sediment-pool-share.spec.ts +65 -0
  244. package/packages/selector/tests/runtime/sediment-publish.spec.ts +142 -0
  245. package/packages/selector/tests/runtime/sediment-real-shapes.spec.ts +158 -0
  246. package/packages/selector/tests/runtime/sediment-settings.spec.ts +73 -0
  247. package/packages/selector/tests/runtime/sediment-signals.spec.ts +124 -0
  248. package/packages/selector/tests/runtime/sediment-span.spec.ts +92 -0
  249. package/packages/selector/tests/runtime/session-events.spec.ts +27 -0
  250. package/packages/selector/tests/runtime/session-file-decode.spec.ts +281 -0
  251. package/packages/selector/tests/runtime/settings-unwrap.spec.ts +58 -0
  252. package/packages/selector/tests/runtime/shallow-primitives.spec.ts +135 -0
  253. package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -0
  254. package/packages/selector/tests/runtime/source-kind-audit.spec.ts +79 -0
  255. package/packages/selector/tests/runtime/stats-backfill-scheduler.spec.ts +167 -0
  256. package/packages/selector/tests/runtime/stats-backfill.spec.ts +350 -0
  257. package/packages/selector/tests/runtime/stats-db.spec.ts +361 -0
  258. package/packages/selector/tests/runtime/stats-privacy.spec.ts +55 -0
  259. package/packages/selector/tests/runtime/stats-sync.spec.ts +278 -0
  260. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
  261. package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -0
  262. package/packages/selector/tests/runtime/tokenizer-golden.spec.ts +53 -0
  263. package/packages/selector/tests/runtime/tokenpilot/benefit.spec.ts +217 -0
  264. package/packages/selector/tests/runtime/tokenpilot/dedup.spec.ts +52 -0
  265. package/packages/selector/tests/runtime/tokenpilot/estimator.spec.ts +79 -0
  266. package/packages/selector/tests/runtime/tokenpilot/locator.spec.ts +76 -0
  267. package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +121 -0
  268. package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +82 -0
  269. package/packages/selector/tests/runtime/toolclass.spec.ts +156 -0
  270. package/packages/selector/tests/runtime/value.spec.ts +23 -0
  271. package/packages/selector/tests/savings-sessions.client.spec.tsx +140 -0
  272. package/packages/selector/tests/settings-seat.client.spec.ts +215 -0
  273. package/packages/selector/tests/standing-generation.host.spec.ts +729 -0
  274. package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +269 -0
  275. package/packages/selector/tests/support/cache-prefix-audit.ts +105 -0
  276. package/packages/selector/tests/support/mock-adapter.ts +37 -0
  277. package/packages/selector/tests/support/ui-primitives.tsx +34 -0
  278. package/packages/selector/tsconfig.json +11 -0
  279. package/packages/selector/tsdown.client.config.ts +102 -0
  280. package/packages/selector/tsdown.config.ts +20 -0
  281. package/pnpm-workspace.yaml +76 -0
  282. package/screenshots.json +6 -3
  283. package/scripts/capture-profile-baseline.mjs +42 -0
  284. package/scripts/cell-browser-e2e.mjs +354 -0
  285. package/scripts/generate-tokenizer-fixtures.py +81 -0
  286. package/scripts/generate-vision-fixtures.py +208 -0
  287. package/scripts/hosts.d.mts +3 -0
  288. package/scripts/hosts.mjs +81 -0
  289. package/scripts/packed-components-smoke.mjs +763 -0
  290. package/scripts/packed-install-e2e.mjs +1346 -0
  291. package/scripts/sediment-latency-report.mjs +63 -0
  292. package/scripts/sync-hosts.mjs +176 -0
  293. package/scripts/toolclass-corpus-replay.mjs +281 -0
  294. package/scripts/verify-release.mjs +342 -0
  295. package/tests/TEST_INVENTORY.md +41 -0
  296. package/tsconfig.base.json +18 -0
  297. package/tsconfig.json +7 -0
  298. package/tsconfig.tests.json +15 -0
  299. package/vitest.built.config.ts +9 -0
  300. package/vitest.config.ts +54 -0
  301. /package/{assets → packages/selector/assets}/deepseek-v4/LICENSE.DeepSeek-V4-Pro.txt +0 -0
  302. /package/{assets → packages/selector/assets}/deepseek-v4/manifest.json +0 -0
  303. /package/{assets → packages/selector/assets}/deepseek-v4/tokenizer.json +0 -0
  304. /package/{assets → packages/selector/assets}/deepseek-v4/tokenizer_config.json +0 -0
  305. /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/LICENSE.DeepSeek-V4-Flash-Vision-Exp.txt +0 -0
  306. /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/manifest.json +0 -0
  307. /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/tokenizer.json +0 -0
  308. /package/{assets → packages/selector/assets}/deepseek-v4-vision-exp/tokenizer_config.json +0 -0
  309. /package/{cordis.patch.yml → packages/selector/cordis.patch.yml} +0 -0
  310. /package/{lib → packages/selector/lib}/client.d.ts +0 -0
  311. /package/{lib → packages/selector/lib}/index.d.ts +0 -0
  312. /package/{lib → packages/selector/lib}/index.js +0 -0
  313. /package/{lib → packages/selector/lib}/invariant.d.ts +0 -0
  314. /package/{lib → packages/selector/lib}/invariant.js +0 -0
  315. /package/{lib → packages/selector/lib}/profiles.d.ts +0 -0
  316. /package/{lib → packages/selector/lib}/pruner.d.ts +0 -0
  317. /package/{lib → packages/selector/lib}/pruner.js +0 -0
  318. /package/{lib → packages/selector/lib}/savings-integral.js +0 -0
  319. /package/{lib → packages/selector/lib}/tail-trim.js +0 -0
  320. /package/{lib → packages/selector/lib}/tool-result.js +0 -0
  321. /package/{locale → packages/selector/locale}/de.json +0 -0
  322. /package/{locale → packages/selector/locale}/en.json +0 -0
  323. /package/{locale → packages/selector/locale}/es.json +0 -0
  324. /package/{locale → packages/selector/locale}/fr.json +0 -0
  325. /package/{locale → packages/selector/locale}/it.json +0 -0
  326. /package/{locale → packages/selector/locale}/ja.json +0 -0
  327. /package/{locale → packages/selector/locale}/ko.json +0 -0
  328. /package/{locale → packages/selector/locale}/pt.json +0 -0
  329. /package/{locale → packages/selector/locale}/ru.json +0 -0
  330. /package/{locale → packages/selector/locale}/zh.json +0 -0
@@ -0,0 +1,123 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import {
3
+ assessReusablePrefix,
4
+ buildSafeCacheAuditRecord,
5
+ fingerprintStablePrefix,
6
+ validateCacheUsage,
7
+ type StablePrefixEnvelope,
8
+ } from './support/cache-prefix-audit.js'
9
+
10
+ const parentEnvelope = (): StablePrefixEnvelope => ({
11
+ provider: 'deepseek',
12
+ model: 'deepseek-v4-flash',
13
+ system: ['stable-system'],
14
+ tools: [
15
+ { name: 'alpha', description: 'first', parameters: { type: 'object' } },
16
+ { name: 'beta', description: 'second', parameters: { type: 'object' } },
17
+ ],
18
+ messages: [
19
+ { role: 'user', content: 'completed parent turn' },
20
+ { role: 'assistant', content: 'completed response' },
21
+ ],
22
+ })
23
+
24
+ describe('parent/child cache-prefix characterization', () => {
25
+ it('accepts an identical fork prefix and computes the same fingerprint', () => {
26
+ const parent = parentEnvelope()
27
+ const child = structuredClone(parent)
28
+ const result = assessReusablePrefix({ mode: 'fork', inheritsParentContext: true, parent, child })
29
+
30
+ expect(result).toEqual({ eligible: true, reason: 'identical-fork-prefix' })
31
+ expect(fingerprintStablePrefix(parent)).toBe(fingerprintStablePrefix(child))
32
+ expect(fingerprintStablePrefix(parent)).toMatch(/^[a-f0-9]{64}$/u)
33
+ })
34
+
35
+ it('rejects spawn because it does not inherit parent messages', () => {
36
+ const parent = parentEnvelope()
37
+ const result = assessReusablePrefix({
38
+ mode: 'spawn',
39
+ inheritsParentContext: false,
40
+ parent,
41
+ child: { ...structuredClone(parent), messages: [] },
42
+ })
43
+
44
+ expect(result).toEqual({ eligible: false, reason: 'spawn-does-not-inherit' })
45
+ })
46
+
47
+ it.each([
48
+ ['system', (child: StablePrefixEnvelope) => { child.system = ['different-persona'] }],
49
+ ['tools', (child: StablePrefixEnvelope) => { child.tools.reverse() }],
50
+ ['provider', (child: StablePrefixEnvelope) => { child.provider = 'another-provider' }],
51
+ ['model', (child: StablePrefixEnvelope) => { child.model = 'another-model' }],
52
+ ['messages', (child: StablePrefixEnvelope) => { child.messages[0] = { role: 'user', content: 'different-history' } }],
53
+ ])('rejects a %s mismatch', (_field, mutate) => {
54
+ const parent = parentEnvelope()
55
+ const child = structuredClone(parent)
56
+ mutate(child)
57
+
58
+ expect(assessReusablePrefix({ mode: 'fork', inheritsParentContext: true, parent, child })).toEqual({
59
+ eligible: false,
60
+ reason: 'stable-prefix-mismatch',
61
+ })
62
+ })
63
+
64
+ it('emits only allowlisted evidence fields', () => {
65
+ const secret = 'sk-do-not-record-this'
66
+ const toolResult = 'private tool result'
67
+ const record = buildSafeCacheAuditRecord({
68
+ fingerprint: fingerprintStablePrefix(parentEnvelope()),
69
+ estimatedSharedPrefixTokens: 128,
70
+ parentSessionId: 'parent-id',
71
+ childSessionId: 'child-id',
72
+ mode: 'fork',
73
+ eligible: true,
74
+ reason: 'identical-fork-prefix',
75
+ cacheReadTokens: 96,
76
+ cacheMissTokens: 32,
77
+ observedPromptTokens: 128,
78
+ })
79
+ const serialized = JSON.stringify(record)
80
+
81
+ expect(Object.keys(record).sort()).toEqual([
82
+ 'auditVersion',
83
+ 'cacheMissTokens',
84
+ 'cacheReadTokens',
85
+ 'childSessionId',
86
+ 'confirmationStatus',
87
+ 'eligible',
88
+ 'estimatedSharedPrefixTokens',
89
+ 'fingerprint',
90
+ 'mode',
91
+ 'observedPromptTokens',
92
+ 'parentSessionId',
93
+ 'reason',
94
+ ])
95
+ expect(serialized).not.toContain(secret)
96
+ expect(serialized).not.toContain(toolResult)
97
+ expect(record.confirmationStatus).toBe('confirmed')
98
+ })
99
+
100
+ it('validates official cache usage arithmetic', () => {
101
+ expect(validateCacheUsage({ cacheReadTokens: 96, cacheMissTokens: 32, observedPromptTokens: 128 })).toBe(true)
102
+ expect(validateCacheUsage({ cacheReadTokens: 95, cacheMissTokens: 32, observedPromptTokens: 128 })).toBe(false)
103
+ })
104
+
105
+ it('marks missing official cache fields as unconfirmed without inventing zeros', () => {
106
+ const record = buildSafeCacheAuditRecord({
107
+ fingerprint: fingerprintStablePrefix(parentEnvelope()),
108
+ estimatedSharedPrefixTokens: 128,
109
+ parentSessionId: 'parent-id',
110
+ childSessionId: 'child-id',
111
+ mode: 'fork',
112
+ eligible: true,
113
+ reason: 'identical-fork-prefix',
114
+ })
115
+
116
+ expect(record).toMatchObject({
117
+ cacheReadTokens: null,
118
+ cacheMissTokens: null,
119
+ observedPromptTokens: null,
120
+ confirmationStatus: 'unconfirmed',
121
+ })
122
+ })
123
+ })
@@ -0,0 +1,98 @@
1
+ // @vitest-environment jsdom
2
+
3
+ import { describe, expect, it, vi } from 'vitest'
4
+ import { DEFAULT_CUSTOM_COMPRESSION_POLICY } from '../src/profiles.ts'
5
+ import { apply } from '../src/client/index.ts'
6
+ import type { CompressionSelectorInjected } from '../src/client/CompressionProfileSelector.tsx'
7
+ import type { ContextCompressionSettings } from '../src/profiles.ts'
8
+
9
+ /**
10
+ * 0.1.7 contract: `codeSkeleton` rides the entry config as part of the volatile
11
+ * `settings` whole-object field. Every write commits the full doc through
12
+ * `configForms.set('settings', doc)`; the confirm-on-write semantics are
13
+ * unchanged — a host that silently drops (or flips) the section fails the
14
+ * acceptance check on the post-commit snapshot.
15
+ */
16
+
17
+ describe('codeSkeleton confirm-on-write client contract', () => {
18
+ it('resolves only when the host confirms the toggled value and rejects silent drops', async () => {
19
+ let revision = 3
20
+ let commit = false
21
+ let flipEnabled = false
22
+ let value: ContextCompressionSettings = {
23
+ profile: 'balanced',
24
+ custom: structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY),
25
+ autoCompact: { thresholdPercent: 80 },
26
+ codeSkeleton: { enabled: false },
27
+ intentSummary: { enabled: false },
28
+ }
29
+ const form = {
30
+ getSnapshot: vi.fn(() => ({
31
+ status: 'ready' as const,
32
+ value: { settings: value },
33
+ base: undefined,
34
+ user: undefined,
35
+ revision,
36
+ writable: true,
37
+ mode: 'host' as const,
38
+ })),
39
+ subscribe: vi.fn(() => () => {}),
40
+ set: vi.fn(async (_field: string, next: unknown) => {
41
+ if (!commit) return false
42
+ revision += 1
43
+ const doc = structuredClone(next) as ContextCompressionSettings
44
+ if (flipEnabled) {
45
+ const accepted = structuredClone(doc.codeSkeleton)
46
+ accepted.enabled = !accepted.enabled
47
+ value = { ...doc, codeSkeleton: accepted }
48
+ return true
49
+ }
50
+ value = doc
51
+ return true
52
+ }),
53
+ unset: vi.fn(async () => false),
54
+ }
55
+ let injected: (() => CompressionSelectorInjected) | undefined
56
+ const ctx = {
57
+ effect: (install: () => unknown) => { install() },
58
+ locale: { register: vi.fn(() => () => {}) },
59
+ configForms: { get: vi.fn(() => form) },
60
+ // Scoped sub-inject seam: host resolves configForms and fires the arm
61
+ inject: (services: string[], cb: (service: unknown) => void) => {
62
+ if (services.includes('configForms')) cb(ctx.configForms)
63
+ },
64
+ slots: {
65
+ inject: (_slot: string, install: () => unknown) => { install() },
66
+ register: (registration: { name?: string, inject?: () => CompressionSelectorInjected }) => {
67
+ // The R4 overlay registration carries no inject face; keep the card's.
68
+ if (registration.name === 'settings.section' && typeof registration.inject === 'function') injected = registration.inject
69
+ return () => {}
70
+ },
71
+ },
72
+ } as unknown as Parameters<typeof apply>[0]
73
+
74
+ apply(ctx)
75
+ const actions = injected?.()
76
+ expect(actions).toBeDefined()
77
+ if (actions === undefined) throw new Error('selector injection was not registered')
78
+
79
+ // A silent host drop leaves the revision untouched: the write must reject.
80
+ await expect(actions.saveCodeSkeleton(true)).rejects.toThrow('were not saved')
81
+
82
+ // A committed write resolves and persists the exact section payload under
83
+ // the volatile `settings` field.
84
+ commit = true
85
+ await expect(actions.saveCodeSkeleton(true)).resolves.toBeUndefined()
86
+ expect(form.set).toHaveBeenLastCalledWith('settings', expect.objectContaining({ codeSkeleton: { enabled: true } }))
87
+ expect(value.codeSkeleton).toEqual({ enabled: true })
88
+
89
+ // A host that flips the toggled bit fails the acceptance check.
90
+ flipEnabled = true
91
+ await expect(actions.saveCodeSkeleton(false)).rejects.toThrow('were not saved')
92
+
93
+ flipEnabled = false
94
+ await expect(actions.saveCodeSkeleton(false)).resolves.toBeUndefined()
95
+ expect(form.set).toHaveBeenLastCalledWith('settings', expect.objectContaining({ codeSkeleton: { enabled: false } }))
96
+ expect(value.codeSkeleton).toEqual({ enabled: false })
97
+ })
98
+ })
@@ -0,0 +1,150 @@
1
+ /**
2
+ * compat narrow-waist unit tests: tool-result triple view, legacy source-kind
3
+ * escape, generation probe, and the settings lease no-op on modern hosts.
4
+ */
5
+ import { describe, expect, it } from 'vitest'
6
+ import { viewToolResult, compressionMessageSource } from '../../src/compat/tool-result.ts'
7
+ import { detectHostGeneration, resetHostGenerationCache, noteResolvedSettingsService, hostGenerationSnapshot } from '../../src/compat/host-generation.ts'
8
+ import { acquireSettingsLease } from '../../src/compat/settings-lease.ts'
9
+
10
+ const MODERN_TOOL_RESULT = {
11
+ content: [{ type: 'text', text: 'hello' }, { type: 'text', text: 'world' }],
12
+ isError: true,
13
+ toolCallId: 'call-modern',
14
+ }
15
+
16
+ const LEGACY_TOOL_RESULT = {
17
+ content: [{
18
+ type: 'tool-result',
19
+ content: [{ type: 'text', text: 'legacy body' }],
20
+ isError: true,
21
+ }],
22
+ source: { callId: 'call-legacy' },
23
+ }
24
+
25
+ describe('viewToolResult (three-axis envelope adapter)', () => {
26
+ it('reads the flat modern shape', () => {
27
+ const view = viewToolResult(MODERN_TOOL_RESULT, 'modern')
28
+ expect(view.blocks).toHaveLength(2)
29
+ expect(view.isError).toBe(true)
30
+ expect(view.callId).toBe('call-modern')
31
+ })
32
+
33
+ it('reads the enveloped legacy shape (strict signature overrides the flag)', () => {
34
+ const view = viewToolResult(LEGACY_TOOL_RESULT, 'modern')
35
+ expect(view.blocks).toEqual([{ type: 'text', text: 'legacy body' }])
36
+ expect(view.isError).toBe(true)
37
+ expect(view.callId).toBe('call-legacy')
38
+ })
39
+
40
+ it('honors the legacy flag when the signature check is inconclusive', () => {
41
+ const loose = {
42
+ content: [{ something: 'else' }],
43
+ source: { callId: 'call-loose' },
44
+ }
45
+ const view = viewToolResult(loose, 'legacy')
46
+ expect(view.blocks).toEqual([{ something: 'else' }])
47
+ expect(view.callId).toBe('call-loose')
48
+ })
49
+
50
+ it('keeps the modern axes on an inconclusive modern message', () => {
51
+ const view = viewToolResult(MODERN_TOOL_RESULT, 'legacy')
52
+ expect(view.blocks).toEqual(MODERN_TOOL_RESULT.content)
53
+ expect(view.isError).toBe(true)
54
+ expect(view.callId).toBe('call-modern')
55
+ })
56
+
57
+ it('never throws on absent fields', () => {
58
+ const view = viewToolResult({}, 'modern')
59
+ expect(view.blocks).toEqual([])
60
+ expect(view.isError).toBe(false)
61
+ expect(view.callId).toBeUndefined()
62
+ })
63
+ })
64
+
65
+ describe('compressionMessageSource (legacy kind escape)', () => {
66
+ it('returns the per-producer kind on modern hosts', () => {
67
+ expect(compressionMessageSource('modern')).toEqual({ kind: 'dsh-context-compression' })
68
+ })
69
+
70
+ it('escapes to the shared plugin kind on legacy hosts', () => {
71
+ const source = compressionMessageSource('legacy')
72
+ expect(source.kind).toBe('plugin')
73
+ expect((source as { plugin?: string }).plugin).toBe('dsh-context-compression-improved')
74
+ })
75
+ })
76
+
77
+ describe('detectHostGeneration (capability probe)', () => {
78
+ it('classifies a register-capable settings service as legacy', () => {
79
+ resetHostGenerationCache()
80
+ expect(detectHostGeneration({ get: (name) => (name === 'settings' ? { register: () => ({}) } : undefined) })).toBe('legacy')
81
+ })
82
+
83
+ it('classifies a register-less settings service as modern', () => {
84
+ resetHostGenerationCache()
85
+ expect(detectHostGeneration({ get: (name) => (name === 'settings' ? { describe: () => [] } : undefined) })).toBe('modern')
86
+ })
87
+
88
+ it('defaults to modern when the settings service is absent', () => {
89
+ resetHostGenerationCache()
90
+ expect(detectHostGeneration({ get: () => undefined })).toBe('modern')
91
+ resetHostGenerationCache()
92
+ })
93
+
94
+ it('does not cache the provisional verdict taken while the service is absent', () => {
95
+ // Measured on the real 0.1.0-rc.2 cell: rows compose before the host
96
+ // settings service, and the old probe cached that absence as 'modern'
97
+ // forever — the legacy lease arm went dead on the line it was built for.
98
+ resetHostGenerationCache()
99
+ expect(detectHostGeneration({ get: () => undefined })).toBe('modern')
100
+ // Later evidence (the settings inject callback) must still flip it.
101
+ expect(noteResolvedSettingsService({ register: () => ({}) })).toBe('legacy')
102
+ })
103
+
104
+ it('noteResolvedSettingsService overrides even a definitive earlier verdict', () => {
105
+ resetHostGenerationCache()
106
+ expect(detectHostGeneration({ get: () => ({ describe: () => [] }) })).toBe('modern')
107
+ expect(noteResolvedSettingsService({ register: () => ({}) })).toBe('legacy')
108
+ expect(hostGenerationSnapshot()).toBe('legacy')
109
+ })
110
+
111
+ it('hostGenerationSnapshot defaults to modern without any evidence', () => {
112
+ resetHostGenerationCache()
113
+ expect(hostGenerationSnapshot()).toBe('modern')
114
+ })
115
+ })
116
+
117
+ describe('acquireSettingsLease', () => {
118
+ it('is a no-op face on modern hosts (register absent, P1-13)', async () => {
119
+ resetHostGenerationCache()
120
+ const effects: Array<{ name?: string }> = []
121
+ const ctx = {
122
+ effect: (factory: () => unknown, name?: string) => { effects.push(name === undefined ? {} : { name }); void factory },
123
+ settings: { describe: () => [] },
124
+ } as unknown as Parameters<typeof acquireSettingsLease>[0]
125
+ const face = acquireSettingsLease(ctx, 'context-compression', {})
126
+ expect(face.get()).toBeUndefined()
127
+ await expect(face.update({ marker: 1 })).resolves.toBe(false)
128
+ })
129
+
130
+ it('registers once and reads/writes through the host service on legacy hosts', async () => {
131
+ const registrations: string[] = []
132
+ const stored = new Map<string, Record<string, unknown>>()
133
+ const settings = {
134
+ register: (ns: string) => { registrations.push(ns); return { get: () => stored.get(ns) } },
135
+ get: (ns: string) => stored.get(ns),
136
+ update: async (ns: string, patch: Record<string, unknown>) => { stored.set(ns, patch) },
137
+ }
138
+ const effects: Array<() => void> = []
139
+ const ctx = {
140
+ effect: (factory: () => () => void) => { effects.push(factory()) },
141
+ settings,
142
+ } as unknown as Parameters<typeof acquireSettingsLease>[0]
143
+ const face = acquireSettingsLease(ctx, 'context-compression', {})
144
+ expect(registrations).toEqual(['context-compression'])
145
+ await expect(face.update({ profile: 'balanced' })).resolves.toBe(true)
146
+ expect(face.get()).toEqual({ profile: 'balanced' })
147
+ // Disposal must not throw when this was the only owner.
148
+ for (const dispose of effects) dispose()
149
+ })
150
+ })
@@ -0,0 +1,226 @@
1
+ import { describe, expect, it, vi } from 'vitest'
2
+ import type { Context as ClientContext } from '@deepseek-ai/cordis'
3
+ import type { ScopeSnapshot } from '../src/client/scope-face.ts'
4
+ import {
5
+ DEFAULT_CUSTOM_COMPRESSION_POLICY,
6
+ isCustomCompressionPolicy,
7
+ } from '../src/profiles.ts'
8
+ import { apply } from '../src/client/index.ts'
9
+ import { decodeSettings } from '../src/client/decode.ts'
10
+ import type {
11
+ CompressionSelectorInjected,
12
+ ContextCompressionSettings,
13
+ } from '../src/client/CompressionProfileSelector.tsx'
14
+
15
+ vi.mock('@deepseek-ai/dsh-client-ui-primitives', () => ({
16
+ IconChevronDownOutlineMedium: () => null,
17
+ Menu: () => null,
18
+ }))
19
+
20
+ const CUSTOM = {
21
+ version: 1,
22
+ unit: 'tokens',
23
+ fresh: { enabled: true, trigger: 4_096, target: 2_048 },
24
+ aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
25
+ history: {
26
+ enabled: true,
27
+ trigger: 32_768,
28
+ keepRecentTurns: 2,
29
+ keepRecent: 16_384,
30
+ minReclaim: 8_192,
31
+ },
32
+ prefixPolicy: 'pressure-break',
33
+ } as const satisfies ContextCompressionSettings['custom']
34
+
35
+ describe('context compression browser contract', () => {
36
+ it('accepts legacy v1 and defaults new edits to v3 with TailTrim disabled', () => {
37
+ expect(isCustomCompressionPolicy(CUSTOM)).toBe(true)
38
+ expect(DEFAULT_CUSTOM_COMPRESSION_POLICY).toMatchObject({
39
+ version: 3,
40
+ tailTrim: { enabled: false, trigger: 700_000 },
41
+ })
42
+ expect(isCustomCompressionPolicy(DEFAULT_CUSTOM_COMPRESSION_POLICY)).toBe(true)
43
+ })
44
+
45
+ it('fails the browser narrowing for unknown or invalid Custom fields', () => {
46
+ expect(isCustomCompressionPolicy({
47
+ ...structuredClone(CUSTOM),
48
+ tailTrim: { enabled: true },
49
+ })).toBe(false)
50
+ expect(isCustomCompressionPolicy({
51
+ ...structuredClone(CUSTOM),
52
+ fresh: { enabled: true, trigger: 100, target: 100 },
53
+ })).toBe(false)
54
+ for (const trigger of [0, -1]) {
55
+ expect(isCustomCompressionPolicy({
56
+ ...structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY),
57
+ tailTrim: { enabled: true, trigger },
58
+ })).toBe(false)
59
+ }
60
+ expect(isCustomCompressionPolicy({
61
+ ...structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY),
62
+ unit: 'context-percent',
63
+ tailTrim: { enabled: true, trigger: 101 },
64
+ })).toBe(false)
65
+ })
66
+
67
+ it('decodes legacy settings to the default threshold and rejects malformed sections', async () => {
68
+ // Legacy documents stored before autoCompact existed inherit 80%.
69
+ const legacy = decodeSettings({ profile: 'balanced', custom: structuredClone(CUSTOM) })
70
+ expect(legacy).toMatchObject({ autoCompact: { thresholdPercent: 80 } })
71
+ // A present-but-invalid threshold fails the whole decode.
72
+ expect(decodeSettings({
73
+ profile: 'balanced',
74
+ custom: structuredClone(CUSTOM),
75
+ autoCompact: { thresholdPercent: 91 },
76
+ })).toBeUndefined()
77
+ })
78
+
79
+ it('reports a recovered no-op write only on the selector boundary', async () => {
80
+ let revision = 7
81
+ let commit = false
82
+ let rewriteCustom = false
83
+ let rewriteReset = false
84
+ let dropTailTrim = false
85
+ let downgradeVersion = false
86
+ let value: ContextCompressionSettings = {
87
+ profile: 'balanced',
88
+ custom: structuredClone(CUSTOM),
89
+ autoCompact: { thresholdPercent: 80 },
90
+ codeSkeleton: { enabled: false },
91
+ intentSummary: { enabled: false },
92
+ }
93
+ const snapshot = (): ScopeSnapshot<ContextCompressionSettings> => ({
94
+ status: 'ready',
95
+ value,
96
+ base: undefined,
97
+ user: undefined,
98
+ revision,
99
+ writable: true,
100
+ mode: 'host',
101
+ })
102
+ const scope = {
103
+ getSnapshot: snapshot,
104
+ subscribe: vi.fn(() => () => {}),
105
+ set: vi.fn(async (field: string, next: unknown) => {
106
+ if (!commit) return
107
+ revision += 1
108
+ if (field === 'custom' && rewriteCustom) {
109
+ const accepted = structuredClone(next as ContextCompressionSettings['custom'])
110
+ accepted.fresh.trigger += 1
111
+ value = { ...value, custom: accepted }
112
+ return
113
+ }
114
+ if (field === 'custom' && rewriteReset) {
115
+ const accepted = structuredClone(next as ContextCompressionSettings['custom'])
116
+ accepted.fresh.trigger += 1
117
+ value = { ...value, custom: accepted }
118
+ return
119
+ }
120
+ if (field === 'custom' && dropTailTrim) {
121
+ const accepted = structuredClone(next as ContextCompressionSettings['custom'])
122
+ if (accepted.version === 3) {
123
+ accepted.tailTrim = { enabled: false, trigger: 700_000 }
124
+ }
125
+ value = { ...value, custom: accepted }
126
+ return
127
+ }
128
+ if (field === 'custom' && downgradeVersion) {
129
+ value = { ...value, custom: structuredClone(CUSTOM) }
130
+ return
131
+ }
132
+ value = { ...value, [field]: next }
133
+ }),
134
+ unset: vi.fn(async () => {
135
+ if (!commit) return
136
+ revision += 1
137
+ const custom: ContextCompressionSettings['custom'] = structuredClone(CUSTOM)
138
+ if (rewriteReset) {
139
+ custom.fresh.trigger = 8_192
140
+ custom.fresh.target = 4_096
141
+ }
142
+ value = { ...value, custom }
143
+ }),
144
+ }
145
+ let injected: (() => CompressionSelectorInjected) | undefined
146
+ const ctx = {
147
+ effect: (install: () => unknown) => { install() },
148
+ locale: { register: vi.fn(() => () => {}) },
149
+ // 0.1.7: the plugin commits the whole doc through configForms; the fake
150
+ // routes each changed top-level field back onto the old scope stub so
151
+ // the rewrite/drop semantics below stay observable per field.
152
+ inject: (services: string[], cb: (service: unknown) => void) => {
153
+ if (services.includes('configForms')) cb(ctx.configForms)
154
+ },
155
+ configForms: {
156
+ get: vi.fn(() => ({
157
+ getSnapshot: () => ({ ...snapshot(), value: { settings: value } }),
158
+ subscribe: scope.subscribe,
159
+ set: async (_field: string, doc: unknown) => {
160
+ const next = doc as unknown as Record<string, unknown>
161
+ for (const key of Object.keys(next)) {
162
+ if (JSON.stringify((value as unknown as Record<string, unknown>)[key]) !== JSON.stringify(next[key])) {
163
+ await scope.set(key, (next as Record<string, unknown>)[key])
164
+ }
165
+ }
166
+ return true
167
+ },
168
+ unset: scope.unset,
169
+ })),
170
+ },
171
+ slots: {
172
+ inject: (_slot: string, install: () => unknown) => { install() },
173
+ register: (registration: { name?: string, inject?: () => CompressionSelectorInjected }) => {
174
+ // The R4 overlay registration carries no inject face; keep the card's.
175
+ if (registration.name === 'settings.section' && typeof registration.inject === 'function') injected = registration.inject
176
+ return () => {}
177
+ },
178
+ },
179
+ } as unknown as ClientContext
180
+
181
+ apply(ctx)
182
+ const actions = injected?.()
183
+ expect(actions).toBeDefined()
184
+ if (actions === undefined) throw new Error('selector injection was not registered')
185
+ await expect(actions.select('native')).rejects.toThrow('were not saved')
186
+
187
+ commit = true
188
+ await expect(actions.select('native')).resolves.toBeUndefined()
189
+ expect(value.profile).toBe('native')
190
+
191
+ const requested: ContextCompressionSettings['custom'] = structuredClone(CUSTOM)
192
+ requested.fresh.trigger = 8_192
193
+ requested.fresh.target = 4_096
194
+ rewriteCustom = true
195
+ await expect(actions.saveCustom(requested)).rejects.toThrow('were not saved')
196
+
197
+ rewriteCustom = false
198
+ const requestedV3 = structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY)
199
+ if (requestedV3.version !== 3) throw new Error('Custom TailTrim persistence fixture requires policy v3')
200
+ requestedV3.tailTrim = { enabled: true, trigger: 49_152 }
201
+ dropTailTrim = true
202
+ await expect(actions.saveCustom(requestedV3)).rejects.toThrow('were not saved')
203
+
204
+ dropTailTrim = false
205
+ downgradeVersion = true
206
+ await expect(actions.saveCustom(requestedV3)).rejects.toThrow('were not saved')
207
+
208
+ downgradeVersion = false
209
+ rewriteReset = true
210
+ await expect(actions.resetCustom()).rejects.toThrow('were not saved')
211
+
212
+ rewriteReset = false
213
+ await expect(actions.resetCustom()).resolves.toBeUndefined()
214
+ expect(scope.set).toHaveBeenLastCalledWith('custom', DEFAULT_CUSTOM_COMPRESSION_POLICY)
215
+ expect(scope.unset).not.toHaveBeenCalled()
216
+
217
+ // The Auto Compact threshold shares the same confirm-on-write boundary:
218
+ // committed writes resolve, and a silent host drop rejects.
219
+ commit = false
220
+ await expect(actions.saveAutoCompact(73)).rejects.toThrow('were not saved')
221
+ commit = true
222
+ await expect(actions.saveAutoCompact(73)).resolves.toBeUndefined()
223
+ expect(scope.set).toHaveBeenLastCalledWith('autoCompact', { thresholdPercent: 73 })
224
+ expect(value.autoCompact).toEqual({ thresholdPercent: 73 })
225
+ })
226
+ })
@@ -0,0 +1,70 @@
1
+ /** Estimator catalog projection: never throws, selection precedence, inline errors. */
2
+
3
+ import { describe, expect, it } from 'vitest'
4
+ import {
5
+ buildEstimatorCatalog, resolveHostRoute,
6
+ type EstimatorCatalogDeps,
7
+ } from '../src/estimator-catalog.ts'
8
+
9
+ describe('resolveHostRoute', () => {
10
+ it('prefers explicit overrides over the host default selection', () => {
11
+ expect(resolveHostRoute({
12
+ overrideProvider: 'p1',
13
+ overrideModel: 'm1',
14
+ currentSelection: () => ({ provider: 'p2', model: 'm2' }),
15
+ })).toEqual({ provider: 'p1', model: 'm1' })
16
+ })
17
+
18
+ it('falls back to the host default selection when overrides are empty', () => {
19
+ expect(resolveHostRoute({
20
+ overrideProvider: '',
21
+ overrideModel: '',
22
+ currentSelection: () => ({ provider: 'p2', model: 'm2' }),
23
+ })).toEqual({ provider: 'p2', model: 'm2' })
24
+ })
25
+
26
+ it('fills missing override fields from the selection, per field', () => {
27
+ expect(resolveHostRoute({ overrideProvider: 'p1', currentSelection: () => ({ model: 'm2' }) }))
28
+ .toEqual({ provider: 'p1', model: 'm2' })
29
+ })
30
+
31
+ it('reports undefined when neither source yields a complete route', () => {
32
+ expect(resolveHostRoute({ currentSelection: () => ({ provider: 'p' }) })).toBeUndefined()
33
+ expect(resolveHostRoute({})).toBeUndefined()
34
+ })
35
+ })
36
+
37
+ describe('buildEstimatorCatalog', () => {
38
+ it('lists every provider with its models and never throws on one broken group', async () => {
39
+ const llm = {
40
+ listProviders: () => [{ id: 'deepseek', name: 'DeepSeek' }, { id: 'broken', name: 'Broken' }],
41
+ listModels: async (providerId: string) => {
42
+ if (providerId === 'broken') throw new Error('adapter offline')
43
+ return [{ id: 'm1', name: 'Model 1' }]
44
+ },
45
+ }
46
+ const catalog = await buildEstimatorCatalog({ llm })
47
+ expect(catalog.providers).toHaveLength(2)
48
+ expect(catalog.providers[0]).toEqual({ id: 'deepseek', name: 'DeepSeek', models: [{ id: 'm1', name: 'Model 1' }] })
49
+ expect(catalog.providers[1]?.error).toBe('adapter offline')
50
+ })
51
+
52
+ it('uses route discovery when listModels is absent or silent', async () => {
53
+ const llm: NonNullable<EstimatorCatalogDeps['llm']> = {
54
+ listProviders: () => [{ id: 'self-hosted', name: 'Self hosted' }],
55
+ listConfigurableProviders: () => [{ provider: 'self-hosted', settingsNs: 'llm.self-hosted' }],
56
+ discoverModels: async () => [{ id: 'm9' }],
57
+ }
58
+ const catalog = await buildEstimatorCatalog({ llm })
59
+ expect(catalog.providers[0]?.models).toEqual([{ id: 'm9', name: 'm9' }])
60
+ expect(catalog.providers[0]?.error).toBeUndefined()
61
+ })
62
+
63
+ it('returns the host selection even when the llm service is unavailable', async () => {
64
+ const catalog = await buildEstimatorCatalog({
65
+ currentSelection: () => ({ provider: 'p', model: 'm' }),
66
+ })
67
+ expect(catalog.providers).toEqual([])
68
+ expect(catalog.selection).toEqual({ provider: 'p', model: 'm' })
69
+ })
70
+ })