dsh-context-compression-improved 0.2.3 → 0.2.5

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 (149) hide show
  1. package/.gitattributes +2 -1
  2. package/.githooks/pre-push +37 -0
  3. package/.github/workflows/ci.yml +39 -39
  4. package/CHANGELOG.ja.md +169 -39
  5. package/CHANGELOG.ko.md +167 -39
  6. package/CHANGELOG.md +333 -135
  7. package/CHANGELOG.zh.md +152 -39
  8. package/CONTRIBUTING.md +22 -22
  9. package/README.ja.md +85 -104
  10. package/README.ko.md +85 -103
  11. package/README.md +94 -112
  12. package/README.zh.md +85 -101
  13. package/THIRD_PARTY_NOTICES.md +14 -14
  14. package/docs/compat-0.1.5-report.md +77 -0
  15. package/docs/installation.ja.md +78 -76
  16. package/docs/installation.ko.md +78 -76
  17. package/docs/installation.md +103 -76
  18. package/docs/installation.zh.md +100 -76
  19. package/docs/repair-log.md +657 -582
  20. package/eslint.config.js +30 -30
  21. package/package.json +163 -142
  22. package/packages/selector/README.md +26 -31
  23. package/packages/selector/README.zh.md +24 -31
  24. package/packages/selector/THIRD_PARTY_NOTICES.md +32 -38
  25. package/packages/selector/cordis.patch.yml +27 -19
  26. package/packages/selector/dsh.plugin.json +2 -2
  27. package/packages/selector/lib/client.d.ts +320 -228
  28. package/packages/selector/lib/client.js +3254 -1462
  29. package/packages/selector/lib/config.js +1713 -842
  30. package/packages/selector/lib/index.d.ts +46 -38
  31. package/packages/selector/lib/index.js +1099 -645
  32. package/packages/selector/lib/invariant.js +70 -70
  33. package/packages/selector/lib/pruner.d.ts +904 -684
  34. package/packages/selector/lib/pruner.js +5800 -3807
  35. package/packages/selector/lib/style.css +88 -46
  36. package/packages/selector/lib/tail-trim.js +145 -136
  37. package/packages/selector/package.json +18 -19
  38. package/packages/selector/screenshots.json +3 -6
  39. package/packages/selector/src/client/CompressionProfileControls.tsx +293 -229
  40. package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -170
  41. package/packages/selector/src/client/CompressionProfileSelector.tsx +85 -79
  42. package/packages/selector/src/client/EstimatorControls.tsx +277 -281
  43. package/packages/selector/src/client/decode.ts +57 -49
  44. package/packages/selector/src/client/index.ts +249 -111
  45. package/packages/selector/src/client/locales/de.ts +141 -0
  46. package/packages/selector/src/client/locales/es.ts +141 -0
  47. package/packages/selector/src/client/locales/fr.ts +141 -0
  48. package/packages/selector/src/client/locales/it.ts +141 -0
  49. package/packages/selector/src/client/locales/ja.ts +141 -0
  50. package/packages/selector/src/client/locales/ko.ts +141 -0
  51. package/packages/selector/src/client/locales/ru.ts +141 -0
  52. package/packages/selector/src/client/locales.ts +282 -198
  53. package/packages/selector/src/client/monitor-fab.ts +336 -0
  54. package/packages/selector/src/client/preset-options.ts +88 -70
  55. package/packages/selector/src/client/savings-card.tsx +150 -0
  56. package/packages/selector/src/client/settings-section.tsx +140 -126
  57. package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -210
  58. package/packages/selector/src/index.ts +671 -327
  59. package/packages/selector/src/invariant.ts +113 -113
  60. package/packages/selector/src/preset-overlay.ts +60 -1
  61. package/packages/selector/src/profiles.ts +431 -342
  62. package/packages/selector/src/pruner/content.ts +18 -5
  63. package/packages/selector/src/pruner/state.ts +57 -43
  64. package/packages/selector/src/pruner/types.ts +23 -5
  65. package/packages/selector/src/pruner.ts +2736 -2144
  66. package/packages/selector/src/runtime/adaptive-cost.ts +23 -12
  67. package/packages/selector/src/runtime/audit.ts +303 -215
  68. package/packages/selector/src/runtime/config.ts +756 -613
  69. package/packages/selector/src/runtime/measurement.ts +436 -403
  70. package/packages/selector/src/runtime/monitor.ts +235 -0
  71. package/packages/selector/src/runtime/reducers.ts +1115 -97
  72. package/packages/selector/src/runtime/retrieve.ts +479 -457
  73. package/packages/selector/src/runtime/savings.ts +337 -0
  74. package/packages/selector/src/runtime/session-events.ts +30 -17
  75. package/packages/selector/src/runtime/summary-command.ts +127 -0
  76. package/packages/selector/src/runtime/tail-trim.ts +166 -166
  77. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -0
  78. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -0
  79. package/packages/selector/src/runtime/tokenpilot/advisor.ts +419 -0
  80. package/packages/selector/src/runtime/tokenpilot/benefit.ts +200 -0
  81. package/packages/selector/src/runtime/tokenpilot/dedup.ts +1 -1
  82. package/packages/selector/src/runtime/tokenpilot/estimator.ts +151 -183
  83. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
  84. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
  85. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
  86. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
  87. package/packages/selector/src/runtime/tokenpilot/locator.ts +1 -1
  88. package/packages/selector/src/runtime/tokenpilot/read-state.ts +23 -2
  89. package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +303 -0
  90. package/packages/selector/src/runtime/toolclass.ts +103 -0
  91. package/packages/selector/src/runtime/types.ts +365 -309
  92. package/packages/selector/tests/advisor-report.host.spec.ts +223 -0
  93. package/packages/selector/tests/auto-compact.client.spec.tsx +214 -226
  94. package/packages/selector/tests/built/client-artifact.spec.ts +59 -51
  95. package/packages/selector/tests/code-skeleton.client.spec.ts +91 -88
  96. package/packages/selector/tests/custom-contract.client.spec.ts +205 -202
  97. package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -247
  98. package/packages/selector/tests/estimator-route-registration.host.spec.ts +194 -176
  99. package/packages/selector/tests/host-preset-overlay.host.spec.ts +206 -204
  100. package/packages/selector/tests/monitor-fab.client.spec.ts +138 -0
  101. package/packages/selector/tests/preset-options-write.client.spec.ts +207 -181
  102. package/packages/selector/tests/preset-overlay-loader.e2e.host.spec.ts +201 -196
  103. package/packages/selector/tests/preset-overlay.host.spec.ts +246 -243
  104. package/packages/selector/tests/profiles.client.spec.tsx +436 -434
  105. package/packages/selector/tests/public/package-contract.client.spec.ts +78 -33
  106. package/packages/selector/tests/runtime/adaptive-cost.spec.ts +7 -7
  107. package/packages/selector/tests/runtime/advice-never-withholds.host.spec.ts +232 -0
  108. package/packages/selector/tests/runtime/advisor-invariant.spec.ts +272 -0
  109. package/packages/selector/tests/runtime/advisor.spec.ts +226 -0
  110. package/packages/selector/tests/runtime/audit.spec.ts +275 -129
  111. package/packages/selector/tests/runtime/char-basis.spec.ts +30 -0
  112. package/packages/selector/tests/runtime/code-skeleton.spec.ts +14 -3
  113. package/packages/selector/tests/runtime/deprecated-preset-options.spec.ts +96 -0
  114. package/packages/selector/tests/runtime/frequency-longstrings.spec.ts +74 -0
  115. package/packages/selector/tests/runtime/html-reducer.spec.ts +212 -0
  116. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
  117. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
  118. package/packages/selector/tests/runtime/line-mapping.spec.ts +153 -0
  119. package/packages/selector/tests/runtime/monitor.spec.ts +301 -0
  120. package/packages/selector/tests/runtime/prose-reducers.spec.ts +133 -0
  121. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2724 -2531
  122. package/packages/selector/tests/runtime/read-input-cap.spec.ts +33 -0
  123. package/packages/selector/tests/runtime/savings.spec.ts +120 -0
  124. package/packages/selector/tests/runtime/search-reducer.spec.ts +110 -0
  125. package/packages/selector/tests/runtime/session-events.spec.ts +27 -27
  126. package/packages/selector/tests/runtime/sidechannel.spec.ts +241 -0
  127. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
  128. package/packages/selector/tests/runtime/toc-and-bundled.spec.ts +159 -0
  129. package/packages/selector/tests/runtime/tokenpilot/benefit.spec.ts +217 -0
  130. package/packages/selector/tests/runtime/tokenpilot/estimator.spec.ts +79 -56
  131. package/packages/selector/tests/runtime/tokenpilot/profile-baseline.spec.ts +116 -100
  132. package/packages/selector/tests/runtime/tokenpilot/read-state.spec.ts +24 -0
  133. package/packages/selector/tests/runtime/toolclass.spec.ts +156 -0
  134. package/packages/selector/tests/settings-seat.client.spec.ts +130 -0
  135. package/packages/selector/tests/standing-generation.host.spec.ts +697 -631
  136. package/packages/selector/tests/subagent-cache-reuse.host.spec.ts +253 -250
  137. package/pnpm-workspace.yaml +66 -19
  138. package/scripts/capture-profile-baseline.mjs +42 -0
  139. package/scripts/generate-tokenizer-fixtures.py +81 -81
  140. package/scripts/generate-vision-fixtures.py +208 -208
  141. package/scripts/{packed-components-smoke.ts → packed-components-smoke.mjs} +732 -713
  142. package/scripts/{packed-install-e2e.ts → packed-install-e2e.mjs} +1273 -1072
  143. package/scripts/toolclass-corpus-replay.mjs +281 -0
  144. package/scripts/{verify-release.ts → verify-release.mjs} +318 -300
  145. package/tests/TEST_INVENTORY.md +41 -42
  146. package/scripts/capture-profile-baseline.ts +0 -80
  147. package/tsconfig.scripts.json +0 -13
  148. /package/{packages/selector/assets/screenshots → docs/assets}/context-compression-selector-profiles.jpg +0 -0
  149. /package/{packages/selector/assets/screenshots → docs/assets}/context-compression-selector-settings.png +0 -0
@@ -1,181 +1,207 @@
1
- // @vitest-environment jsdom
2
-
3
- import { describe, expect, it } from 'vitest'
4
- import { apply } from '../src/client/index.ts'
5
- import type { CompressionSelectorInjected } from '../src/client/CompressionProfileSelector.tsx'
6
- import type { ContextCompressionSettings } from '../src/profiles.ts'
7
-
8
- /**
9
- * `settingsScope.set(field, value)` replaces the value at that field, so the
10
- * write path is asserted through the document it stores: writing the bare
11
- * patch is what silently deleted `estimatorMode` (and every sibling override)
12
- * whenever the user touched a second field of the TokenPilot-inspired card.
13
- */
14
-
15
- type ScopeFace = {
16
- getSnapshot: () => unknown
17
- subscribe: (listener: () => void) => () => void
18
- set: (field: string, value: unknown) => Promise<void>
19
- unset: (field: string) => Promise<void>
20
- }
21
-
22
- function createScopeStub(initial: ContextCompressionSettings, commit = true): {
23
- scope: ScopeFace
24
- writes: { field: string, value: unknown }[]
25
- section: () => Record<string, unknown>
26
- sectionOf: (key: string) => unknown
27
- } {
28
- let revision = 1
29
- let value: Record<string, unknown> = structuredClone(initial) as unknown as Record<string, unknown>
30
- const writes: { field: string, value: unknown }[] = []
31
- const listeners = new Set<() => void>()
32
- const scope: ScopeFace = {
33
- getSnapshot: () => ({
34
- status: 'ready',
35
- value: value as unknown as ContextCompressionSettings,
36
- base: undefined,
37
- user: undefined,
38
- revision,
39
- writable: true,
40
- mode: 'host',
41
- }),
42
- subscribe: (listener: () => void) => {
43
- listeners.add(listener)
44
- return () => { listeners.delete(listener) }
45
- },
46
- set: (field: string, fieldValue: unknown) => {
47
- writes.push({ field, value: fieldValue })
48
- // The Host applies `set` AT the path: the section value is replaced.
49
- if (commit) {
50
- value = { ...value, [field]: structuredClone(fieldValue) }
51
- revision += 1
52
- for (const listener of listeners) listener()
53
- }
54
- return Promise.resolve()
55
- },
56
- unset: (field: string) => {
57
- writes.push({ field, value: undefined })
58
- if (commit) {
59
- delete value[field]
60
- revision += 1
61
- for (const listener of listeners) listener()
62
- }
63
- return Promise.resolve()
64
- },
65
- }
66
- return {
67
- scope,
68
- writes,
69
- section: () => value,
70
- sectionOf: (key: string) => value[key],
71
- }
72
- }
73
-
74
- const DEFAULT_CUSTOM = {
75
- version: 1,
76
- unit: 'tokens',
77
- fresh: { enabled: true, trigger: 4_096, target: 2_048 },
78
- aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
79
- history: {
80
- enabled: true,
81
- trigger: 32_768,
82
- keepRecentTurns: 2,
83
- keepRecent: 16_384,
84
- minReclaim: 8_192,
85
- },
86
- prefixPolicy: 'pressure-break',
87
- } as const satisfies ContextCompressionSettings['custom']
88
-
89
- function bindInjected(
90
- presetOptions: ContextCompressionSettings['presetOptions'],
91
- commit = true,
92
- ): {
93
- injected: CompressionSelectorInjected
94
- writes: { field: string, value: unknown }[]
95
- sectionOf: (key: string) => unknown
96
- } {
97
- const settings: ContextCompressionSettings = {
98
- profile: 'tokenpilot-inspired',
99
- custom: structuredClone(DEFAULT_CUSTOM),
100
- autoCompact: { thresholdPercent: 80 },
101
- codeSkeleton: { enabled: false },
102
- ...(presetOptions === undefined ? {} : { presetOptions }),
103
- }
104
- const stub = createScopeStub(settings, commit)
105
- let options: Record<string, unknown> | undefined
106
- const ctx = {
107
- effect: (factory: () => unknown) => { factory(); return () => {} },
108
- locale: { register: () => {}, bind: () => (key: string) => key },
109
- settingsScope: { bind: () => stub.scope },
110
- slots: {
111
- inject: (_slot: string, factory: () => (() => void) | Generator<() => void>) => {
112
- const result = factory()
113
- return typeof result === 'function' ? result : () => {}
114
- },
115
- register: (registerOptions: Record<string, unknown>) => {
116
- options = registerOptions
117
- return () => {}
118
- },
119
- },
120
- }
121
- apply(ctx as never)
122
- if (options === undefined) throw new Error('the settings card never registered')
123
- const factory = options['inject'] as () => CompressionSelectorInjected
124
- return { injected: factory(), writes: stub.writes, sectionOf: stub.sectionOf }
125
- }
126
-
127
- describe('presetOptions writes preserve sibling fields', () => {
128
- it('keeps the estimator channel when a provider is chosen afterwards', async () => {
129
- const { injected, sectionOf } = bindInjected({ estimatorMode: 'host' })
130
- await injected.savePresetOptions({ estimatorProvider: 'local-35b' })
131
- expect(sectionOf('presetOptions')).toEqual({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
132
- })
133
-
134
- it('writes the merged document rather than the bare patch', async () => {
135
- const { injected, writes } = bindInjected({ estimatorMode: 'direct', estimatorBaseUrl: 'http://192.168.100.242:8200' })
136
- await injected.savePresetOptions({ estimatorModel: 'Qwen3.6-35B-A3B' })
137
- expect(writes).toEqual([{
138
- field: 'presetOptions',
139
- value: {
140
- estimatorMode: 'direct',
141
- estimatorBaseUrl: 'http://192.168.100.242:8200',
142
- estimatorModel: 'Qwen3.6-35B-A3B',
143
- },
144
- }])
145
- })
146
-
147
- it('keeps every unrelated override across successive edits', async () => {
148
- const { injected, sectionOf } = bindInjected({
149
- estimatorMode: 'direct',
150
- dedupeToolResults: false,
151
- readState: false,
152
- })
153
- await injected.savePresetOptions({ estimatorModel: 'Qwen3.6-35B-A3B' })
154
- await injected.savePresetOptions({ estimatorProvider: 'local-35b' })
155
- expect(sectionOf('presetOptions')).toEqual({
156
- estimatorMode: 'direct',
157
- dedupeToolResults: false,
158
- readState: false,
159
- estimatorModel: 'Qwen3.6-35B-A3B',
160
- estimatorProvider: 'local-35b',
161
- })
162
- })
163
-
164
- it('clears exactly the field a patch names with undefined', async () => {
165
- const { injected, sectionOf } = bindInjected({ estimatorMode: 'direct', estimatorApiKey: 'sk-stored' })
166
- await injected.savePresetOptions({ estimatorApiKey: undefined })
167
- expect(sectionOf('presetOptions')).toEqual({ estimatorMode: 'direct' })
168
- })
169
-
170
- it('writes nothing when the patch already matches the stored section', async () => {
171
- const { injected, writes } = bindInjected({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
172
- await injected.savePresetOptions({ estimatorMode: 'host' })
173
- expect(writes).toEqual([])
174
- })
175
-
176
- it('reports a save the Host did not commit', async () => {
177
- const { injected } = bindInjected({ estimatorMode: 'host' }, false)
178
- await expect(injected.savePresetOptions({ estimatorProvider: 'local-35b' }))
179
- .rejects.toThrow(/were not saved/)
180
- })
181
- })
1
+ // @vitest-environment jsdom
2
+
3
+ import { describe, expect, it } from 'vitest'
4
+ import { apply } from '../src/client/index.ts'
5
+ import type { CompressionSelectorInjected } from '../src/client/CompressionProfileSelector.tsx'
6
+ import type { ContextCompressionSettings } from '../src/profiles.ts'
7
+
8
+ /**
9
+ * The settings transport applies a `set` op AT its path, so the write path is
10
+ * asserted through the ops it submits: a whole-section write is what silently
11
+ * deleted `estimatorMode` (and every sibling override) whenever the user
12
+ * touched a second field of the TokenPilot-inspired card.
13
+ */
14
+
15
+ type PathOp = { op: 'set', path: string[], value: unknown } | { op: 'unset', path: string[] }
16
+
17
+ interface ScopeStub {
18
+ readonly writes: PathOp[][]
19
+ readonly snapshot: () => Record<string, unknown>
20
+ }
21
+
22
+ /** Emulate the Host settings service: apply ops at their path, then bump the revision. */
23
+ function createScopeStub(initial: ContextCompressionSettings, commit = true): ScopeStub {
24
+ let revision = 1
25
+ let value: Record<string, unknown> = structuredClone(initial) as unknown as Record<string, unknown>
26
+ const writes: PathOp[][] = []
27
+ const listeners = new Set<() => void>()
28
+ const applyOps = (ops: readonly PathOp[]): void => {
29
+ for (const op of ops) {
30
+ const [head, ...rest] = op.path
31
+ if (head === undefined) continue
32
+ if (rest.length === 0) {
33
+ if (op.op === 'set') value = { ...value, [head]: op.value }
34
+ else delete value[head]
35
+ continue
36
+ }
37
+ const child = { ...(value[head] as Record<string, unknown> | undefined) }
38
+ const leaf = rest[rest.length - 1] as string
39
+ if (op.op === 'set') child[leaf] = op.value
40
+ else delete child[leaf]
41
+ value = { ...value, [head]: child }
42
+ }
43
+ revision += 1
44
+ for (const listener of listeners) listener()
45
+ }
46
+ return {
47
+ writes,
48
+ snapshot: () => value,
49
+ // The scope face the injection factory binds.
50
+ scope: {
51
+ getSnapshot: () => ({
52
+ status: 'ready',
53
+ value: value as unknown as ContextCompressionSettings,
54
+ base: undefined,
55
+ user: undefined,
56
+ revision,
57
+ writable: true,
58
+ mode: 'host',
59
+ }),
60
+ subscribe: (listener: () => void) => {
61
+ listeners.add(listener)
62
+ return () => { listeners.delete(listener) }
63
+ },
64
+ set: (field: string, fieldValue: unknown) => {
65
+ const ops: PathOp[] = [{ op: 'set', path: [field], value: fieldValue }]
66
+ writes.push(ops)
67
+ applyOps(ops)
68
+ return Promise.resolve()
69
+ },
70
+ unset: (field: string) => {
71
+ const ops: PathOp[] = [{ op: 'unset', path: [field] }]
72
+ writes.push(ops)
73
+ applyOps(ops)
74
+ return Promise.resolve()
75
+ },
76
+ mutate: (ops: readonly PathOp[]) => {
77
+ writes.push([...ops])
78
+ // A refused or lost write changes no document, so the revision the
79
+ // confirmation read fences on never moves.
80
+ if (commit) applyOps(ops)
81
+ return Promise.resolve()
82
+ },
83
+ },
84
+ } as ScopeStub & { scope: unknown }
85
+ }
86
+
87
+ const DEFAULT_CUSTOM = {
88
+ version: 1,
89
+ unit: 'tokens',
90
+ fresh: { enabled: true, trigger: 4_096, target: 2_048 },
91
+ aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
92
+ history: {
93
+ enabled: true,
94
+ trigger: 32_768,
95
+ keepRecentTurns: 2,
96
+ keepRecent: 16_384,
97
+ minReclaim: 8_192,
98
+ },
99
+ prefixPolicy: 'pressure-break',
100
+ } as const satisfies ContextCompressionSettings['custom']
101
+
102
+ function bindInjected(
103
+ presetOptions: ContextCompressionSettings['presetOptions'],
104
+ commit = true,
105
+ ): {
106
+ injected: CompressionSelectorInjected
107
+ writes: PathOp[][]
108
+ snapshot: () => Record<string, unknown>
109
+ } {
110
+ const settings: ContextCompressionSettings = {
111
+ profile: 'tokenpilot-inspired',
112
+ custom: structuredClone(DEFAULT_CUSTOM),
113
+ autoCompact: { thresholdPercent: 80 },
114
+ codeSkeleton: { enabled: false },
115
+ intentSummary: { enabled: false },
116
+ ...(presetOptions === undefined ? {} : { presetOptions }),
117
+ }
118
+ const stub = createScopeStub(settings, commit)
119
+ let options: Record<string, unknown> | undefined
120
+ const ctx = {
121
+ slots: {
122
+ inject: (_slot: string, factory: () => (() => void) | Generator<() => void>) => {
123
+ const result = factory()
124
+ return typeof result === 'function' ? result : () => {}
125
+ },
126
+ register: (registerOptions: Record<string, unknown>) => {
127
+ // The R4 overlay registration rides the same slots service; only the
128
+ // settings card carries the inject factory these tests drive.
129
+ if (registerOptions.name === 'settings.section') options = registerOptions
130
+ return () => {}
131
+ },
132
+ },
133
+ // apply consumes the services through their declarative-inject faces
134
+ // (ctx.locale / ctx.settingsScope), the same shape cordis binds on the host.
135
+ locale: { bind: () => (key: string) => key, register: () => {} },
136
+ settingsScope: { bind: () => (stub as unknown as { scope: unknown }).scope },
137
+ }
138
+ apply(ctx as never)
139
+ if (options === undefined) throw new Error('the settings card never registered')
140
+ const factory = options['inject'] as () => CompressionSelectorInjected
141
+ return { injected: factory(), writes: stub.writes, snapshot: stub.snapshot }
142
+ }
143
+
144
+ describe('presetOptions writes are path-addressed', () => {
145
+ it('keeps the estimator channel when a provider is chosen afterwards', async () => {
146
+ const { injected, writes, snapshot } = bindInjected({ estimatorMode: 'host' })
147
+ await injected.savePresetOptions({ estimatorProvider: 'local-35b' })
148
+ expect(snapshot()['presetOptions']).toEqual({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
149
+ expect(writes).toEqual([[{ op: 'set', path: ['presetOptions', 'estimatorProvider'], value: 'local-35b' }]])
150
+ })
151
+
152
+ it('treats a retired review-gate patch as a no-op instead of a write', async () => {
153
+ const { injected, writes, snapshot } = bindInjected({ estimatorMode: 'host' })
154
+ // The patch surface no longer carries these keys, and the decoders accept
155
+ // them only so a legacy document keeps loading. Cast past the removed type
156
+ // so the test pins the RUNTIME behaviour: nothing is written.
157
+ await injected.savePresetOptions({
158
+ reviewMode: true,
159
+ reviewTimeoutTurns: 8,
160
+ cacheHitDiscountAlpha: 0.2,
161
+ reviewHighImpactTokens: 6000,
162
+ } as unknown as Parameters<typeof injected.savePresetOptions>[0])
163
+ expect(writes).toEqual([])
164
+ expect(snapshot()['presetOptions']).toEqual({ estimatorMode: 'host' })
165
+ })
166
+
167
+ it('never replaces the whole section, so sibling overrides survive', async () => {
168
+ const { injected, writes, snapshot } = bindInjected({
169
+ estimatorMode: 'direct',
170
+ estimatorBaseUrl: 'http://192.168.100.242:8200',
171
+ dedupeToolResults: false,
172
+ readState: false,
173
+ })
174
+ await injected.savePresetOptions({ estimatorModel: 'Qwen3.6-35B-A3B' })
175
+ await injected.savePresetOptions({ estimatorProvider: 'local-35b' })
176
+ expect(snapshot()['presetOptions']).toEqual({
177
+ estimatorMode: 'direct',
178
+ estimatorBaseUrl: 'http://192.168.100.242:8200',
179
+ dedupeToolResults: false,
180
+ readState: false,
181
+ estimatorModel: 'Qwen3.6-35B-A3B',
182
+ estimatorProvider: 'local-35b',
183
+ })
184
+ for (const batch of writes) {
185
+ for (const op of batch) expect(op.path.length).toBe(2)
186
+ }
187
+ })
188
+
189
+ it('clears exactly the field a patch names with undefined', async () => {
190
+ const { injected, writes, snapshot } = bindInjected({ estimatorMode: 'direct', estimatorApiKey: 'sk-stored' })
191
+ await injected.savePresetOptions({ estimatorApiKey: undefined })
192
+ expect(snapshot()['presetOptions']).toEqual({ estimatorMode: 'direct' })
193
+ expect(writes).toEqual([[{ op: 'unset', path: ['presetOptions', 'estimatorApiKey'] }]])
194
+ })
195
+
196
+ it('writes nothing when the patch already matches the stored section', async () => {
197
+ const { injected, writes } = bindInjected({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
198
+ await injected.savePresetOptions({ estimatorMode: 'host' })
199
+ expect(writes).toEqual([])
200
+ })
201
+
202
+ it('reports a save the Host did not commit', async () => {
203
+ const { injected } = bindInjected({ estimatorMode: 'host' }, false)
204
+ await expect(injected.savePresetOptions({ estimatorProvider: 'local-35b' }))
205
+ .rejects.toThrow(/were not saved/)
206
+ })
207
+ })