dsh-context-compression-improved 0.8.0-beta.3 → 0.8.0-beta.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 (58) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/eslint.config.js +30 -30
  3. package/package.json +163 -163
  4. package/packages/selector/dsh.plugin.json +16 -16
  5. package/packages/selector/lib/client.d.ts +28 -0
  6. package/packages/selector/lib/client.js +631 -7
  7. package/packages/selector/lib/{advisor-state.js → config.js} +170 -65
  8. package/packages/selector/lib/index.d.ts +13 -1
  9. package/packages/selector/lib/index.js +306 -50
  10. package/packages/selector/lib/profiles.d.ts +15 -0
  11. package/packages/selector/lib/pruner.d.ts +65 -0
  12. package/packages/selector/lib/pruner.js +408 -5
  13. package/packages/selector/src/client/CompressionProfileControls.tsx +67 -0
  14. package/packages/selector/src/client/CompressionProfileSelector.tsx +2 -0
  15. package/packages/selector/src/client/decode.ts +57 -49
  16. package/packages/selector/src/client/index.ts +41 -0
  17. package/packages/selector/src/client/locales/de.ts +26 -0
  18. package/packages/selector/src/client/locales/es.ts +26 -0
  19. package/packages/selector/src/client/locales/fr.ts +26 -0
  20. package/packages/selector/src/client/locales/it.ts +26 -0
  21. package/packages/selector/src/client/locales/ja.ts +26 -0
  22. package/packages/selector/src/client/locales/ko.ts +26 -0
  23. package/packages/selector/src/client/locales/ru.ts +26 -0
  24. package/packages/selector/src/client/locales.ts +52 -0
  25. package/packages/selector/src/client/monitor-fab.ts +307 -0
  26. package/packages/selector/src/client/settings-section.tsx +16 -2
  27. package/packages/selector/src/index.ts +133 -60
  28. package/packages/selector/src/profiles.ts +431 -394
  29. package/packages/selector/src/pruner/state.ts +57 -50
  30. package/packages/selector/src/pruner.ts +241 -1
  31. package/packages/selector/src/runtime/audit.ts +303 -286
  32. package/packages/selector/src/runtime/config.ts +756 -706
  33. package/packages/selector/src/runtime/monitor.ts +149 -0
  34. package/packages/selector/src/runtime/summary-command.ts +127 -0
  35. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -188
  36. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -149
  37. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
  38. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
  39. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
  40. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
  41. package/packages/selector/src/runtime/types.ts +365 -346
  42. package/packages/selector/tests/auto-compact.client.spec.tsx +214 -213
  43. package/packages/selector/tests/code-skeleton.client.spec.ts +94 -93
  44. package/packages/selector/tests/custom-contract.client.spec.ts +223 -222
  45. package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -252
  46. package/packages/selector/tests/estimator-route-registration.host.spec.ts +8 -6
  47. package/packages/selector/tests/monitor-fab.client.spec.ts +117 -0
  48. package/packages/selector/tests/preset-options-write.client.spec.ts +188 -187
  49. package/packages/selector/tests/profiles.client.spec.tsx +436 -434
  50. package/packages/selector/tests/runtime/audit.spec.ts +275 -274
  51. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
  52. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
  53. package/packages/selector/tests/runtime/monitor.spec.ts +236 -0
  54. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2734 -2731
  55. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
  56. package/packages/selector/tests/settings-seat.client.spec.ts +1 -1
  57. package/scripts/packed-install-e2e.mjs +1 -1
  58. package/scripts/verify-release.mjs +2 -1
@@ -1,252 +1,253 @@
1
- // @vitest-environment jsdom
2
-
3
- import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
4
- import { useSyncExternalStore } from 'react'
5
- import { afterEach, describe, expect, it, vi } from 'vitest'
6
- import {
7
- ContextCompressionSettingsSection,
8
- type CompressionProfileSelectorProps,
9
- type ContextCompressionSettings,
10
- } from '../src/client/CompressionProfileSelector.tsx'
11
- import { en } from '../src/client/locales.ts'
12
-
13
- afterEach(() => {
14
- cleanup()
15
- vi.unstubAllGlobals()
16
- })
17
-
18
- interface SettingsScopeSnapshot<T> {
19
- status: 'loading' | 'ready' | 'unavailable'
20
- value: T | undefined
21
- base: T | undefined
22
- user: T | undefined
23
- revision: number | undefined
24
- writable: boolean
25
- mode: 'host' | 'memory'
26
- }
27
-
28
- interface SnapshotStore<T> {
29
- readonly getSnapshot: () => T
30
- readonly subscribe: (listener: () => void) => () => void
31
- }
32
-
33
- function createSnapshotStore<T>(initial: T): SnapshotStore<T> {
34
- const snapshot = structuredClone(initial)
35
- const listeners = new Set<() => void>()
36
- return {
37
- getSnapshot: () => snapshot,
38
- subscribe(listener) {
39
- listeners.add(listener)
40
- return () => { listeners.delete(listener) }
41
- },
42
- }
43
- }
44
-
45
- function bindSnapshotSelector<T>(store: SnapshotStore<T>) {
46
- return function useSnapshotSelector<U>(selector: (snapshot: T) => U): U {
47
- return useSyncExternalStore(
48
- store.subscribe,
49
- () => selector(store.getSnapshot()),
50
- () => selector(store.getSnapshot()),
51
- )
52
- }
53
- }
54
-
55
- const DEFAULT_CUSTOM = {
56
- version: 1,
57
- unit: 'tokens',
58
- fresh: { enabled: true, trigger: 4_096, target: 2_048 },
59
- aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
60
- history: {
61
- enabled: true,
62
- trigger: 32_768,
63
- keepRecentTurns: 2,
64
- keepRecent: 16_384,
65
- minReclaim: 8_192,
66
- },
67
- prefixPolicy: 'pressure-break',
68
- } as const satisfies ContextCompressionSettings['custom']
69
-
70
- const t = (key: string) => en[key as keyof typeof en] ?? key
71
-
72
- const PROVIDER_LABEL = 'Provider'
73
- const CHANNEL_LABEL = 'Channel'
74
- const MODEL_LABEL = 'Model'
75
- const API_KEY_LABEL = 'API key (write-only, never echoed)'
76
- const BASE_URL_LABEL = 'Endpoint base URL (/v1)'
77
-
78
- const CATALOG = {
79
- ok: true,
80
- providers: [
81
- {
82
- id: 'local-35b',
83
- name: 'local-35b',
84
- models: [{ id: 'Qwen3.6-35B-A3B', name: 'Qwen3.6-35B-A3B' }, { id: 'Qwen38-27B', name: 'Qwen38-27B' }],
85
- },
86
- { id: 'deepseek-official', name: 'DeepSeek', models: [{ id: 'deepseek-flash', name: 'DeepSeek-V41-Flash' }] },
87
- ],
88
- selection: { provider: 'deepseek-official', model: 'deepseek-flash' },
89
- }
90
-
91
- function mountEstimator(
92
- presetOptions: ContextCompressionSettings['presetOptions'],
93
- options: { readonly catalog?: unknown, readonly profile?: ContextCompressionSettings['profile'] } = {},
94
- ) {
95
- const state = createSnapshotStore<SettingsScopeSnapshot<ContextCompressionSettings>>({
96
- status: 'ready',
97
- value: {
98
- profile: options.profile ?? 'tokenpilot-inspired',
99
- custom: structuredClone(DEFAULT_CUSTOM),
100
- autoCompact: { thresholdPercent: 80 },
101
- codeSkeleton: { enabled: false },
102
- presetOptions: presetOptions ?? {},
103
- },
104
- base: undefined,
105
- user: undefined,
106
- revision: 0,
107
- writable: true,
108
- mode: 'host',
109
- })
110
- const savePresetOptions = vi.fn(() => Promise.resolve())
111
- if (options.catalog !== undefined) {
112
- vi.stubGlobal('fetch', vi.fn(() => Promise.resolve({
113
- ok: true,
114
- json: () => Promise.resolve(options.catalog),
115
- })))
116
- }
117
- render(<ContextCompressionSettingsSection {...({
118
- useCompression: bindSnapshotSelector(state),
119
- select: vi.fn(() => Promise.resolve()),
120
- saveCustom: vi.fn(() => Promise.resolve()),
121
- resetCustom: vi.fn(() => Promise.resolve()),
122
- saveAutoCompact: vi.fn(() => Promise.resolve()),
123
- saveCodeSkeleton: vi.fn(() => Promise.resolve()),
124
- savePresetOptions,
125
- t,
126
- } as unknown as CompressionProfileSelectorProps)} />)
127
- return { savePresetOptions }
128
- }
129
-
130
- describe('estimator channel card', () => {
131
- it('host mode fills comboboxes from the catalog, keeps custom typing, and asks for no API key', async () => {
132
- const { savePresetOptions } = mountEstimator({ estimatorMode: 'host' }, { catalog: CATALOG })
133
-
134
- // Provider/model are always comboboxes (dsh-perm-gate receiver pattern):
135
- // a catalog pick fills the field, and the same input accepts a custom id.
136
- const provider = screen.getByLabelText<HTMLInputElement>(PROVIDER_LABEL)
137
- const model = screen.getByLabelText<HTMLInputElement>(MODEL_LABEL)
138
- await waitFor(() => {
139
- const providerOptions = document.querySelectorAll('#estimator-provider-options option')
140
- expect([...providerOptions].map(option => (option as HTMLOptionElement).value))
141
- .toEqual(['local-35b', 'deepseek-official'])
142
- expect(provider.getAttribute('list')).toBe('estimator-provider-options')
143
- expect(model.getAttribute('list')).toBe('estimator-model-options')
144
- })
145
- expect(screen.getByText(/Effective route: deepseek-official \/ deepseek-flash/)).not.toBeNull()
146
- expect(screen.getByText(/no API key is needed/)).not.toBeNull()
147
-
148
- // No credential input and no base-URL field on this channel.
149
- expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
150
- expect(screen.queryByLabelText(BASE_URL_LABEL)).toBeNull()
151
- expect(document.querySelectorAll('input[type="password"]').length).toBe(0)
152
-
153
- // A datalist pick fires change without blur: it commits immediately.
154
- fireEvent.change(provider, { target: { value: 'local-35b' } })
155
- await waitFor(() => {
156
- expect(savePresetOptions).toHaveBeenCalledWith({ estimatorProvider: 'local-35b' })
157
- })
158
- // Free typing only commits on blur, so mid-edit keystrokes never save.
159
- fireEvent.change(provider, { target: { value: 'my-custom-group' } })
160
- expect(savePresetOptions).not.toHaveBeenCalledWith({ estimatorProvider: 'my-custom-group' })
161
- fireEvent.blur(provider)
162
- await waitFor(() => {
163
- expect(savePresetOptions).toHaveBeenCalledWith({ estimatorProvider: 'my-custom-group' })
164
- })
165
- })
166
-
167
- it('offers the catalog models of the chosen provider and commits one', async () => {
168
- const { savePresetOptions } = mountEstimator(
169
- { estimatorMode: 'host', estimatorProvider: 'local-35b' },
170
- { catalog: CATALOG },
171
- )
172
- // Query before the catalog lands: once the datalist has options, the
173
- // label's textContent includes them and getByLabelText's exact match
174
- // would no longer see a bare "Model".
175
- const model = screen.getByLabelText<HTMLInputElement>(MODEL_LABEL)
176
- expect(model.value).toBe('')
177
- await waitFor(() => {
178
- const modelOptions = document.querySelectorAll('#estimator-model-options option')
179
- expect([...modelOptions].map(option => (option as HTMLOptionElement).value))
180
- .toEqual(['Qwen3.6-35B-A3B', 'Qwen38-27B'])
181
- })
182
- expect(screen.getByText(/Effective route: local-35b/)).not.toBeNull()
183
-
184
- fireEvent.change(model, { target: { value: 'Qwen38-27B' } })
185
- await waitFor(() => {
186
- expect(savePresetOptions).toHaveBeenCalledWith({ estimatorModel: 'Qwen38-27B' })
187
- })
188
- })
189
-
190
- it('still accepts custom names — without a credential field — when no catalog is served', async () => {
191
- vi.stubGlobal('fetch', vi.fn(() => Promise.reject(new Error('no route'))))
192
- const { savePresetOptions } = mountEstimator({ estimatorMode: 'host' })
193
-
194
- const provider = screen.getByLabelText<HTMLInputElement>(PROVIDER_LABEL)
195
- // The model field stays present and editable without a served catalog.
196
- expect(screen.getByLabelText<HTMLInputElement>(MODEL_LABEL)).toBeTruthy()
197
- expect(document.querySelectorAll('#estimator-provider-options option').length).toBe(0)
198
- expect(document.querySelectorAll('#estimator-model-options option').length).toBe(0)
199
- fireEvent.change(provider, { target: { value: 'manual-group' } })
200
- fireEvent.blur(provider)
201
- await waitFor(() => {
202
- expect(savePresetOptions).toHaveBeenCalledWith({ estimatorProvider: 'manual-group' })
203
- })
204
- expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
205
- expect(screen.getByText(/no API key is needed/)).not.toBeNull()
206
- expect(screen.getByText(/not determined yet/)).not.toBeNull()
207
- })
208
-
209
- it('keeps the endpoint and key fields on the direct channel alone', () => {
210
- mountEstimator({ estimatorMode: 'direct' })
211
-
212
- expect(screen.getByLabelText(BASE_URL_LABEL)).not.toBeNull()
213
- expect(screen.getByLabelText(MODEL_LABEL).tagName).toBe('INPUT')
214
- expect(screen.getByLabelText(API_KEY_LABEL)).not.toBeNull()
215
- expect(document.querySelectorAll('input[type="password"]').length).toBe(1)
216
- expect(screen.queryByLabelText(PROVIDER_LABEL)).toBeNull()
217
- expect(screen.queryByText(/no API key is needed/)).toBeNull()
218
- })
219
-
220
- it('renders no channel controls while the estimator is off', () => {
221
- mountEstimator({})
222
- expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
223
- expect(screen.queryByLabelText(BASE_URL_LABEL)).toBeNull()
224
- expect(screen.queryByLabelText(PROVIDER_LABEL)).toBeNull()
225
- })
226
-
227
- // Regression guard for the first real-machine report: the card used to render
228
- // nothing at all off tokenpilot-inspired, which reads as a missing feature.
229
- it('keeps the estimator section, and names the unlocking profile, off tokenpilot-inspired', () => {
230
- mountEstimator({}, { profile: 'balanced' })
231
-
232
- // The heading and its stable anchor survive, so the panel is still findable
233
- // where the reader last saw it.
234
- expect(document.getElementById('context-compression-estimator-title')?.textContent)
235
- .toBe('Estimator (optional)')
236
- // The notice names the current profile and the profile that unlocks the card.
237
- const notice = screen.getByText(/ships only with the TokenPilot-inspired profile/)
238
- expect(notice.textContent).toContain('“Balanced”')
239
- expect(notice.textContent).toContain('Select TokenPilot-inspired')
240
- // Gating semantics are unchanged: no channel control exists here.
241
- expect(screen.queryByLabelText(CHANNEL_LABEL)).toBeNull()
242
- expect(screen.queryByLabelText(PROVIDER_LABEL)).toBeNull()
243
- expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
244
- expect(document.querySelectorAll('input[list]').length).toBe(0)
245
- })
246
-
247
- it('drops the inactive notice once tokenpilot-inspired owns the selector', () => {
248
- mountEstimator({})
249
- expect(screen.queryByText(/ships only with the TokenPilot-inspired profile/)).toBeNull()
250
- expect(screen.getByLabelText(CHANNEL_LABEL)).not.toBeNull()
251
- })
252
- })
1
+ // @vitest-environment jsdom
2
+
3
+ import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
4
+ import { useSyncExternalStore } from 'react'
5
+ import { afterEach, describe, expect, it, vi } from 'vitest'
6
+ import {
7
+ ContextCompressionSettingsSection,
8
+ type CompressionProfileSelectorProps,
9
+ type ContextCompressionSettings,
10
+ } from '../src/client/CompressionProfileSelector.tsx'
11
+ import { en } from '../src/client/locales.ts'
12
+
13
+ afterEach(() => {
14
+ cleanup()
15
+ vi.unstubAllGlobals()
16
+ })
17
+
18
+ interface SettingsScopeSnapshot<T> {
19
+ status: 'loading' | 'ready' | 'unavailable'
20
+ value: T | undefined
21
+ base: T | undefined
22
+ user: T | undefined
23
+ revision: number | undefined
24
+ writable: boolean
25
+ mode: 'host' | 'memory'
26
+ }
27
+
28
+ interface SnapshotStore<T> {
29
+ readonly getSnapshot: () => T
30
+ readonly subscribe: (listener: () => void) => () => void
31
+ }
32
+
33
+ function createSnapshotStore<T>(initial: T): SnapshotStore<T> {
34
+ const snapshot = structuredClone(initial)
35
+ const listeners = new Set<() => void>()
36
+ return {
37
+ getSnapshot: () => snapshot,
38
+ subscribe(listener) {
39
+ listeners.add(listener)
40
+ return () => { listeners.delete(listener) }
41
+ },
42
+ }
43
+ }
44
+
45
+ function bindSnapshotSelector<T>(store: SnapshotStore<T>) {
46
+ return function useSnapshotSelector<U>(selector: (snapshot: T) => U): U {
47
+ return useSyncExternalStore(
48
+ store.subscribe,
49
+ () => selector(store.getSnapshot()),
50
+ () => selector(store.getSnapshot()),
51
+ )
52
+ }
53
+ }
54
+
55
+ const DEFAULT_CUSTOM = {
56
+ version: 1,
57
+ unit: 'tokens',
58
+ fresh: { enabled: true, trigger: 4_096, target: 2_048 },
59
+ aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
60
+ history: {
61
+ enabled: true,
62
+ trigger: 32_768,
63
+ keepRecentTurns: 2,
64
+ keepRecent: 16_384,
65
+ minReclaim: 8_192,
66
+ },
67
+ prefixPolicy: 'pressure-break',
68
+ } as const satisfies ContextCompressionSettings['custom']
69
+
70
+ const t = (key: string) => en[key as keyof typeof en] ?? key
71
+
72
+ const PROVIDER_LABEL = 'Provider'
73
+ const CHANNEL_LABEL = 'Channel'
74
+ const MODEL_LABEL = 'Model'
75
+ const API_KEY_LABEL = 'API key (write-only, never echoed)'
76
+ const BASE_URL_LABEL = 'Endpoint base URL (/v1)'
77
+
78
+ const CATALOG = {
79
+ ok: true,
80
+ providers: [
81
+ {
82
+ id: 'local-35b',
83
+ name: 'local-35b',
84
+ models: [{ id: 'Qwen3.6-35B-A3B', name: 'Qwen3.6-35B-A3B' }, { id: 'Qwen38-27B', name: 'Qwen38-27B' }],
85
+ },
86
+ { id: 'deepseek-official', name: 'DeepSeek', models: [{ id: 'deepseek-flash', name: 'DeepSeek-V41-Flash' }] },
87
+ ],
88
+ selection: { provider: 'deepseek-official', model: 'deepseek-flash' },
89
+ }
90
+
91
+ function mountEstimator(
92
+ presetOptions: ContextCompressionSettings['presetOptions'],
93
+ options: { readonly catalog?: unknown, readonly profile?: ContextCompressionSettings['profile'] } = {},
94
+ ) {
95
+ const state = createSnapshotStore<SettingsScopeSnapshot<ContextCompressionSettings>>({
96
+ status: 'ready',
97
+ value: {
98
+ profile: options.profile ?? 'tokenpilot-inspired',
99
+ custom: structuredClone(DEFAULT_CUSTOM),
100
+ autoCompact: { thresholdPercent: 80 },
101
+ codeSkeleton: { enabled: false },
102
+ intentSummary: { enabled: false },
103
+ presetOptions: presetOptions ?? {},
104
+ },
105
+ base: undefined,
106
+ user: undefined,
107
+ revision: 0,
108
+ writable: true,
109
+ mode: 'host',
110
+ })
111
+ const savePresetOptions = vi.fn(() => Promise.resolve())
112
+ if (options.catalog !== undefined) {
113
+ vi.stubGlobal('fetch', vi.fn(() => Promise.resolve({
114
+ ok: true,
115
+ json: () => Promise.resolve(options.catalog),
116
+ })))
117
+ }
118
+ render(<ContextCompressionSettingsSection {...({
119
+ useCompression: bindSnapshotSelector(state),
120
+ select: vi.fn(() => Promise.resolve()),
121
+ saveCustom: vi.fn(() => Promise.resolve()),
122
+ resetCustom: vi.fn(() => Promise.resolve()),
123
+ saveAutoCompact: vi.fn(() => Promise.resolve()),
124
+ saveCodeSkeleton: vi.fn(() => Promise.resolve()),
125
+ savePresetOptions,
126
+ t,
127
+ } as unknown as CompressionProfileSelectorProps)} />)
128
+ return { savePresetOptions }
129
+ }
130
+
131
+ describe('estimator channel card', () => {
132
+ it('host mode fills comboboxes from the catalog, keeps custom typing, and asks for no API key', async () => {
133
+ const { savePresetOptions } = mountEstimator({ estimatorMode: 'host' }, { catalog: CATALOG })
134
+
135
+ // Provider/model are always comboboxes (dsh-perm-gate receiver pattern):
136
+ // a catalog pick fills the field, and the same input accepts a custom id.
137
+ const provider = screen.getByLabelText<HTMLInputElement>(PROVIDER_LABEL)
138
+ const model = screen.getByLabelText<HTMLInputElement>(MODEL_LABEL)
139
+ await waitFor(() => {
140
+ const providerOptions = document.querySelectorAll('#estimator-provider-options option')
141
+ expect([...providerOptions].map(option => (option as HTMLOptionElement).value))
142
+ .toEqual(['local-35b', 'deepseek-official'])
143
+ expect(provider.getAttribute('list')).toBe('estimator-provider-options')
144
+ expect(model.getAttribute('list')).toBe('estimator-model-options')
145
+ })
146
+ expect(screen.getByText(/Effective route: deepseek-official \/ deepseek-flash/)).not.toBeNull()
147
+ expect(screen.getByText(/no API key is needed/)).not.toBeNull()
148
+
149
+ // No credential input and no base-URL field on this channel.
150
+ expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
151
+ expect(screen.queryByLabelText(BASE_URL_LABEL)).toBeNull()
152
+ expect(document.querySelectorAll('input[type="password"]').length).toBe(0)
153
+
154
+ // A datalist pick fires change without blur: it commits immediately.
155
+ fireEvent.change(provider, { target: { value: 'local-35b' } })
156
+ await waitFor(() => {
157
+ expect(savePresetOptions).toHaveBeenCalledWith({ estimatorProvider: 'local-35b' })
158
+ })
159
+ // Free typing only commits on blur, so mid-edit keystrokes never save.
160
+ fireEvent.change(provider, { target: { value: 'my-custom-group' } })
161
+ expect(savePresetOptions).not.toHaveBeenCalledWith({ estimatorProvider: 'my-custom-group' })
162
+ fireEvent.blur(provider)
163
+ await waitFor(() => {
164
+ expect(savePresetOptions).toHaveBeenCalledWith({ estimatorProvider: 'my-custom-group' })
165
+ })
166
+ })
167
+
168
+ it('offers the catalog models of the chosen provider and commits one', async () => {
169
+ const { savePresetOptions } = mountEstimator(
170
+ { estimatorMode: 'host', estimatorProvider: 'local-35b' },
171
+ { catalog: CATALOG },
172
+ )
173
+ // Query before the catalog lands: once the datalist has options, the
174
+ // label's textContent includes them and getByLabelText's exact match
175
+ // would no longer see a bare "Model".
176
+ const model = screen.getByLabelText<HTMLInputElement>(MODEL_LABEL)
177
+ expect(model.value).toBe('')
178
+ await waitFor(() => {
179
+ const modelOptions = document.querySelectorAll('#estimator-model-options option')
180
+ expect([...modelOptions].map(option => (option as HTMLOptionElement).value))
181
+ .toEqual(['Qwen3.6-35B-A3B', 'Qwen38-27B'])
182
+ })
183
+ expect(screen.getByText(/Effective route: local-35b/)).not.toBeNull()
184
+
185
+ fireEvent.change(model, { target: { value: 'Qwen38-27B' } })
186
+ await waitFor(() => {
187
+ expect(savePresetOptions).toHaveBeenCalledWith({ estimatorModel: 'Qwen38-27B' })
188
+ })
189
+ })
190
+
191
+ it('still accepts custom names — without a credential field — when no catalog is served', async () => {
192
+ vi.stubGlobal('fetch', vi.fn(() => Promise.reject(new Error('no route'))))
193
+ const { savePresetOptions } = mountEstimator({ estimatorMode: 'host' })
194
+
195
+ const provider = screen.getByLabelText<HTMLInputElement>(PROVIDER_LABEL)
196
+ // The model field stays present and editable without a served catalog.
197
+ expect(screen.getByLabelText<HTMLInputElement>(MODEL_LABEL)).toBeTruthy()
198
+ expect(document.querySelectorAll('#estimator-provider-options option').length).toBe(0)
199
+ expect(document.querySelectorAll('#estimator-model-options option').length).toBe(0)
200
+ fireEvent.change(provider, { target: { value: 'manual-group' } })
201
+ fireEvent.blur(provider)
202
+ await waitFor(() => {
203
+ expect(savePresetOptions).toHaveBeenCalledWith({ estimatorProvider: 'manual-group' })
204
+ })
205
+ expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
206
+ expect(screen.getByText(/no API key is needed/)).not.toBeNull()
207
+ expect(screen.getByText(/not determined yet/)).not.toBeNull()
208
+ })
209
+
210
+ it('keeps the endpoint and key fields on the direct channel alone', () => {
211
+ mountEstimator({ estimatorMode: 'direct' })
212
+
213
+ expect(screen.getByLabelText(BASE_URL_LABEL)).not.toBeNull()
214
+ expect(screen.getByLabelText(MODEL_LABEL).tagName).toBe('INPUT')
215
+ expect(screen.getByLabelText(API_KEY_LABEL)).not.toBeNull()
216
+ expect(document.querySelectorAll('input[type="password"]').length).toBe(1)
217
+ expect(screen.queryByLabelText(PROVIDER_LABEL)).toBeNull()
218
+ expect(screen.queryByText(/no API key is needed/)).toBeNull()
219
+ })
220
+
221
+ it('renders no channel controls while the estimator is off', () => {
222
+ mountEstimator({})
223
+ expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
224
+ expect(screen.queryByLabelText(BASE_URL_LABEL)).toBeNull()
225
+ expect(screen.queryByLabelText(PROVIDER_LABEL)).toBeNull()
226
+ })
227
+
228
+ // Regression guard for the first real-machine report: the card used to render
229
+ // nothing at all off tokenpilot-inspired, which reads as a missing feature.
230
+ it('keeps the estimator section, and names the unlocking profile, off tokenpilot-inspired', () => {
231
+ mountEstimator({}, { profile: 'balanced' })
232
+
233
+ // The heading and its stable anchor survive, so the panel is still findable
234
+ // where the reader last saw it.
235
+ expect(document.getElementById('context-compression-estimator-title')?.textContent)
236
+ .toBe('Estimator (optional)')
237
+ // The notice names the current profile and the profile that unlocks the card.
238
+ const notice = screen.getByText(/ships only with the TokenPilot-inspired profile/)
239
+ expect(notice.textContent).toContain('“Balanced”')
240
+ expect(notice.textContent).toContain('Select TokenPilot-inspired')
241
+ // Gating semantics are unchanged: no channel control exists here.
242
+ expect(screen.queryByLabelText(CHANNEL_LABEL)).toBeNull()
243
+ expect(screen.queryByLabelText(PROVIDER_LABEL)).toBeNull()
244
+ expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
245
+ expect(document.querySelectorAll('input[list]').length).toBe(0)
246
+ })
247
+
248
+ it('drops the inactive notice once tokenpilot-inspired owns the selector', () => {
249
+ mountEstimator({})
250
+ expect(screen.queryByText(/ships only with the TokenPilot-inspired profile/)).toBeNull()
251
+ expect(screen.getByLabelText(CHANNEL_LABEL)).not.toBeNull()
252
+ })
253
+ })
@@ -17,6 +17,8 @@ const CATALOG_ROUTE = '/api/dsh-context-compression-improved/estimator-catalog'
17
17
  const LEGACY_ROUTE = '/endpoint/dsh-context-compression-improved/estimator-catalog'
18
18
  const SAVINGS_ROUTE = '/api/dsh-context-compression-improved/savings'
19
19
  const SAVINGS_LEGACY_ROUTE = '/endpoint/dsh-context-compression-improved/savings'
20
+ const MONITOR_ROUTE = '/api/dsh-context-compression-improved/monitor'
21
+ const MONITOR_LEGACY_ROUTE = '/endpoint/dsh-context-compression-improved/monitor'
20
22
 
21
23
  interface RegisteredRoute {
22
24
  kind: string
@@ -106,7 +108,7 @@ describe('estimator catalog route registration', () => {
106
108
  apply(runtime, { estimatorCatalogRoute: true })
107
109
  await settle()
108
110
 
109
- expect(routes.map(route => route.path)).toEqual([LEGACY_ROUTE, CATALOG_ROUTE, SAVINGS_LEGACY_ROUTE, SAVINGS_ROUTE])
111
+ expect(routes.map(route => route.path)).toEqual([LEGACY_ROUTE, CATALOG_ROUTE, SAVINGS_LEGACY_ROUTE, SAVINGS_ROUTE, MONITOR_LEGACY_ROUTE, MONITOR_ROUTE])
110
112
  expect(routes.every(route => route.kind === 'exact')).toBe(true)
111
113
  })
112
114
 
@@ -122,7 +124,7 @@ describe('estimator catalog route registration', () => {
122
124
  await mountWebServer(runtime, routes)
123
125
  await settle()
124
126
 
125
- expect(routes.map(route => route.path)).toEqual([LEGACY_ROUTE, CATALOG_ROUTE, SAVINGS_LEGACY_ROUTE, SAVINGS_ROUTE])
127
+ expect(routes.map(route => route.path)).toEqual([LEGACY_ROUTE, CATALOG_ROUTE, SAVINGS_LEGACY_ROUTE, SAVINGS_ROUTE, MONITOR_LEGACY_ROUTE, MONITOR_ROUTE])
126
128
  })
127
129
 
128
130
  it('registers nothing twice when the estimator services arrive later', async () => {
@@ -176,9 +178,9 @@ describe('estimator catalog route registration', () => {
176
178
  expect(JSON.parse(String(response.body))).toMatchObject({ ok: true, providers: [] })
177
179
  })
178
180
 
179
- it('without the estimator opt-in only the always-on savings snapshot route registers', async () => {
180
- // savings 路由是有意常开的只读面(纯 token 聚合,无会话内容);estimator
181
- // catalog 才是 opt-in 行。默认面 = 恰好两条 savings 前缀。
181
+ it('without the estimator opt-in only the always-on read routes register (savings + monitor)', async () => {
182
+ // savings 与 monitor 路由是有意常开的只读面(纯 token 聚合与折叠状态,无会话内容);estimator
183
+ // catalog 才是 opt-in 行。默认面 = 恰好两条 savings 前缀 + 两条 monitor 前缀。
182
184
  const routes: RegisteredRoute[] = []
183
185
  const runtime = new Context()
184
186
  ctx = runtime
@@ -187,6 +189,6 @@ describe('estimator catalog route registration', () => {
187
189
  apply(runtime, { presetOverlay: false })
188
190
  await settle()
189
191
 
190
- expect(routes.map(route => route.path)).toEqual([SAVINGS_LEGACY_ROUTE, SAVINGS_ROUTE])
192
+ expect(routes.map(route => route.path)).toEqual([SAVINGS_LEGACY_ROUTE, SAVINGS_ROUTE, MONITOR_LEGACY_ROUTE, MONITOR_ROUTE])
191
193
  })
192
194
  })