dsh-context-compression-improved 0.8.0-beta.3 → 0.8.0-beta.4

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 (52) hide show
  1. package/CHANGELOG.md +7 -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 +6 -0
  6. package/packages/selector/lib/client.js +106 -7
  7. package/packages/selector/lib/index.js +92 -1
  8. package/packages/selector/lib/{advisor-state.js → intent-gate.js} +91 -3
  9. package/packages/selector/lib/profiles.d.ts +9 -0
  10. package/packages/selector/lib/pruner.d.ts +59 -0
  11. package/packages/selector/lib/pruner.js +408 -5
  12. package/packages/selector/src/client/CompressionProfileControls.tsx +34 -0
  13. package/packages/selector/src/client/CompressionProfileSelector.tsx +1 -0
  14. package/packages/selector/src/client/decode.ts +53 -49
  15. package/packages/selector/src/client/index.ts +4 -0
  16. package/packages/selector/src/client/locales/de.ts +5 -0
  17. package/packages/selector/src/client/locales/es.ts +5 -0
  18. package/packages/selector/src/client/locales/fr.ts +5 -0
  19. package/packages/selector/src/client/locales/it.ts +5 -0
  20. package/packages/selector/src/client/locales/ja.ts +5 -0
  21. package/packages/selector/src/client/locales/ko.ts +5 -0
  22. package/packages/selector/src/client/locales/ru.ts +5 -0
  23. package/packages/selector/src/client/locales.ts +10 -0
  24. package/packages/selector/src/client/settings-section.tsx +9 -2
  25. package/packages/selector/src/index.ts +7 -0
  26. package/packages/selector/src/profiles.ts +414 -394
  27. package/packages/selector/src/pruner/state.ts +57 -50
  28. package/packages/selector/src/pruner.ts +241 -1
  29. package/packages/selector/src/runtime/audit.ts +303 -286
  30. package/packages/selector/src/runtime/config.ts +731 -706
  31. package/packages/selector/src/runtime/summary-command.ts +127 -0
  32. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -188
  33. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -149
  34. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
  35. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
  36. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
  37. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
  38. package/packages/selector/src/runtime/types.ts +358 -346
  39. package/packages/selector/tests/auto-compact.client.spec.tsx +214 -213
  40. package/packages/selector/tests/code-skeleton.client.spec.ts +94 -93
  41. package/packages/selector/tests/custom-contract.client.spec.ts +223 -222
  42. package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -252
  43. package/packages/selector/tests/preset-options-write.client.spec.ts +188 -187
  44. package/packages/selector/tests/profiles.client.spec.tsx +436 -434
  45. package/packages/selector/tests/runtime/audit.spec.ts +275 -274
  46. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
  47. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
  48. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2734 -2731
  49. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
  50. package/packages/selector/tests/settings-seat.client.spec.ts +1 -1
  51. package/scripts/packed-install-e2e.mjs +1 -1
  52. package/scripts/verify-release.mjs +2 -1
@@ -1,187 +1,188 @@
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
- * 0.1.7 contract: the compression document rides the entry config as ONE
10
- * volatile whole-object field, so every write commits the full document
11
- * through `configForms.set('settings', doc)`. The sibling-safety that the old
12
- * path-addressed settings service provided is now enforced CLIENT-side:
13
- * `planPresetOptionsOps` is still computed against the current doc and applied
14
- * locally before the commit, so a second field write never erases the first.
15
- */
16
-
17
- interface FormStub {
18
- form: Record<string, unknown>
19
- /** Whole-doc commits the fake Host accepted. */
20
- readonly commits: Record<string, unknown>[]
21
- /** Whole-doc writes the fake Host refused. */
22
- readonly refused: Record<string, unknown>[]
23
- readonly snapshot: () => Record<string, unknown>
24
- }
25
-
26
- function createFormStub(initial: ContextCompressionSettings, commit = true): FormStub {
27
- let revision = 1
28
- let value: Record<string, unknown> = { settings: structuredClone(initial) as unknown as Record<string, unknown> }
29
- const commits: Record<string, unknown>[] = []
30
- const refused: Record<string, unknown>[] = []
31
- const listeners = new Set<() => void>()
32
- return {
33
- commits,
34
- refused,
35
- snapshot: () => structuredClone(value.settings as Record<string, unknown>),
36
- form: {
37
- getSnapshot: () => ({
38
- status: 'ready' as const,
39
- value: value as unknown as Record<string, unknown>,
40
- revision,
41
- writable: true,
42
- base: undefined,
43
- user: undefined,
44
- mode: 'host' as const,
45
- }),
46
- subscribe: (listener: () => void) => {
47
- listeners.add(listener)
48
- return () => { listeners.delete(listener) }
49
- },
50
- set: (_field: string, doc: unknown) => {
51
- if (!commit) {
52
- refused.push(doc as Record<string, unknown>)
53
- return Promise.resolve(false)
54
- }
55
- value = { settings: structuredClone(doc) as unknown as Record<string, unknown> }
56
- revision += 1
57
- commits.push({ settings: structuredClone(doc) as unknown as Record<string, unknown> })
58
- for (const listener of listeners) listener()
59
- return Promise.resolve(true)
60
- },
61
- unset: () => Promise.resolve(false),
62
- },
63
- } as FormStub & { form: Record<string, unknown> }
64
- }
65
-
66
- const DEFAULT_CUSTOM = {
67
- version: 1,
68
- unit: 'tokens',
69
- fresh: { enabled: true, trigger: 4_096, target: 2_048 },
70
- aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
71
- history: {
72
- enabled: true,
73
- trigger: 16_384,
74
- keepRecentTurns: 2,
75
- keepRecent: 16_384,
76
- minReclaim: 8_192,
77
- },
78
- prefixPolicy: 'pressure-break',
79
- } as const satisfies ContextCompressionSettings['custom']
80
-
81
- function bindInjected(
82
- presetOptions: ContextCompressionSettings['presetOptions'],
83
- commit = true,
84
- ): {
85
- injected: CompressionSelectorInjected
86
- stub: FormStub
87
- } {
88
- const settings: ContextCompressionSettings = {
89
- profile: 'tokenpilot-inspired',
90
- custom: structuredClone(DEFAULT_CUSTOM),
91
- autoCompact: { thresholdPercent: 80 },
92
- codeSkeleton: { enabled: false },
93
- ...(presetOptions === undefined ? {} : { presetOptions }),
94
- }
95
- const stub = createFormStub(settings, commit)
96
- let options: Record<string, unknown> | undefined
97
- const ctx = {
98
- slots: {
99
- inject: (_slot: string, factory: () => (() => void) | Generator<() => void>) => {
100
- const result = factory()
101
- return typeof result === 'function' ? result : () => {}
102
- },
103
- register: (registerOptions: Record<string, unknown>) => {
104
- // The R4 overlay registration rides the same slots service; only the
105
- // settings card carries the inject factory these tests drive.
106
- if (registerOptions.name === 'settings.section') options = registerOptions
107
- return () => {}
108
- },
109
- },
110
- // apply consumes the services through their declarative-inject faces
111
- // (ctx.locale / ctx.configForms), the same shape cordis binds on the host.
112
- locale: { bind: () => (key: string) => key, register: () => {} },
113
- configForms: { get: () => (stub.form as unknown as { getSnapshot(): unknown }) },
114
- }
115
- apply(ctx as never)
116
- if (options === undefined) throw new Error('the settings card never registered')
117
- const factory = options['inject'] as () => CompressionSelectorInjected
118
- return { injected: factory(), stub }
119
- }
120
-
121
- describe('presetOptions writes commit the whole doc through configForms', () => {
122
- it('keeps the estimator channel when a provider is chosen afterwards', async () => {
123
- const { injected, stub } = bindInjected({ estimatorMode: 'host' })
124
- await injected.savePresetOptions({ estimatorProvider: 'local-35b' })
125
- expect(stub.snapshot()['presetOptions']).toEqual({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
126
- expect(stub.commits.length).toBe(1)
127
- })
128
-
129
- it('treats a retired review-gate patch as a no-op instead of a write', async () => {
130
- const { injected, stub } = bindInjected({ estimatorMode: 'host' })
131
- // The patch surface no longer carries these keys, and the decoders accept
132
- // them only so a legacy document keeps loading. Cast past the removed type
133
- // so the test pins the RUNTIME behaviour: nothing is written.
134
- await injected.savePresetOptions({
135
- reviewMode: true,
136
- reviewTimeoutTurns: 8,
137
- cacheHitDiscountAlpha: 0.2,
138
- reviewHighImpactTokens: 6000,
139
- } as unknown as Parameters<typeof injected.savePresetOptions>[0])
140
- expect(stub.commits.length).toBe(0)
141
- expect(stub.snapshot()['presetOptions']).toEqual({ estimatorMode: 'host' })
142
- })
143
-
144
- it('never loses sibling overrides across whole-doc commits', async () => {
145
- const { injected, stub } = bindInjected({
146
- estimatorMode: 'direct',
147
- estimatorBaseUrl: 'http://192.168.100.242:8200',
148
- dedupeToolResults: false,
149
- readState: false,
150
- })
151
- await injected.savePresetOptions({ estimatorModel: 'Qwen3.6-35B-A3B' })
152
- await injected.savePresetOptions({ estimatorProvider: 'local-35b' })
153
- expect(stub.snapshot()['presetOptions']).toEqual({
154
- estimatorMode: 'direct',
155
- estimatorBaseUrl: 'http://192.168.100.242:8200',
156
- dedupeToolResults: false,
157
- readState: false,
158
- estimatorModel: 'Qwen3.6-35B-A3B',
159
- estimatorProvider: 'local-35b',
160
- })
161
- // Two user actions, two whole-doc commits.
162
- expect(stub.commits.length).toBe(2)
163
- for (const doc of stub.commits) {
164
- // Each commit carries the complete document under the volatile field.
165
- expect(Object.keys(doc)).toEqual(['settings'])
166
- }
167
- })
168
-
169
- it('clears exactly the field a patch names with undefined', async () => {
170
- const { injected, stub } = bindInjected({ estimatorMode: 'direct', estimatorApiKey: 'sk-stored' })
171
- await injected.savePresetOptions({ estimatorApiKey: undefined })
172
- expect(stub.snapshot()['presetOptions']).toEqual({ estimatorMode: 'direct' })
173
- })
174
-
175
- it('writes nothing when the patch already matches the stored section', async () => {
176
- const { injected, stub } = bindInjected({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
177
- await injected.savePresetOptions({ estimatorMode: 'host' })
178
- expect(stub.commits.length).toBe(0)
179
- expect(stub.refused.length).toBe(0)
180
- })
181
-
182
- it('reports a save the Host did not commit', async () => {
183
- const { injected } = bindInjected({ estimatorMode: 'host' }, false)
184
- await expect(injected.savePresetOptions({ estimatorProvider: 'local-35b' }))
185
- .rejects.toThrow(/were not saved/)
186
- })
187
- })
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
+ * 0.1.7 contract: the compression document rides the entry config as ONE
10
+ * volatile whole-object field, so every write commits the full document
11
+ * through `configForms.set('settings', doc)`. The sibling-safety that the old
12
+ * path-addressed settings service provided is now enforced CLIENT-side:
13
+ * `planPresetOptionsOps` is still computed against the current doc and applied
14
+ * locally before the commit, so a second field write never erases the first.
15
+ */
16
+
17
+ interface FormStub {
18
+ form: Record<string, unknown>
19
+ /** Whole-doc commits the fake Host accepted. */
20
+ readonly commits: Record<string, unknown>[]
21
+ /** Whole-doc writes the fake Host refused. */
22
+ readonly refused: Record<string, unknown>[]
23
+ readonly snapshot: () => Record<string, unknown>
24
+ }
25
+
26
+ function createFormStub(initial: ContextCompressionSettings, commit = true): FormStub {
27
+ let revision = 1
28
+ let value: Record<string, unknown> = { settings: structuredClone(initial) as unknown as Record<string, unknown> }
29
+ const commits: Record<string, unknown>[] = []
30
+ const refused: Record<string, unknown>[] = []
31
+ const listeners = new Set<() => void>()
32
+ return {
33
+ commits,
34
+ refused,
35
+ snapshot: () => structuredClone(value.settings as Record<string, unknown>),
36
+ form: {
37
+ getSnapshot: () => ({
38
+ status: 'ready' as const,
39
+ value: value as unknown as Record<string, unknown>,
40
+ revision,
41
+ writable: true,
42
+ base: undefined,
43
+ user: undefined,
44
+ mode: 'host' as const,
45
+ }),
46
+ subscribe: (listener: () => void) => {
47
+ listeners.add(listener)
48
+ return () => { listeners.delete(listener) }
49
+ },
50
+ set: (_field: string, doc: unknown) => {
51
+ if (!commit) {
52
+ refused.push(doc as Record<string, unknown>)
53
+ return Promise.resolve(false)
54
+ }
55
+ value = { settings: structuredClone(doc) as unknown as Record<string, unknown> }
56
+ revision += 1
57
+ commits.push({ settings: structuredClone(doc) as unknown as Record<string, unknown> })
58
+ for (const listener of listeners) listener()
59
+ return Promise.resolve(true)
60
+ },
61
+ unset: () => Promise.resolve(false),
62
+ },
63
+ } as FormStub & { form: Record<string, unknown> }
64
+ }
65
+
66
+ const DEFAULT_CUSTOM = {
67
+ version: 1,
68
+ unit: 'tokens',
69
+ fresh: { enabled: true, trigger: 4_096, target: 2_048 },
70
+ aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
71
+ history: {
72
+ enabled: true,
73
+ trigger: 16_384,
74
+ keepRecentTurns: 2,
75
+ keepRecent: 16_384,
76
+ minReclaim: 8_192,
77
+ },
78
+ prefixPolicy: 'pressure-break',
79
+ } as const satisfies ContextCompressionSettings['custom']
80
+
81
+ function bindInjected(
82
+ presetOptions: ContextCompressionSettings['presetOptions'],
83
+ commit = true,
84
+ ): {
85
+ injected: CompressionSelectorInjected
86
+ stub: FormStub
87
+ } {
88
+ const settings: ContextCompressionSettings = {
89
+ profile: 'tokenpilot-inspired',
90
+ custom: structuredClone(DEFAULT_CUSTOM),
91
+ autoCompact: { thresholdPercent: 80 },
92
+ codeSkeleton: { enabled: false },
93
+ intentSummary: { enabled: false },
94
+ ...(presetOptions === undefined ? {} : { presetOptions }),
95
+ }
96
+ const stub = createFormStub(settings, commit)
97
+ let options: Record<string, unknown> | undefined
98
+ const ctx = {
99
+ slots: {
100
+ inject: (_slot: string, factory: () => (() => void) | Generator<() => void>) => {
101
+ const result = factory()
102
+ return typeof result === 'function' ? result : () => {}
103
+ },
104
+ register: (registerOptions: Record<string, unknown>) => {
105
+ // The R4 overlay registration rides the same slots service; only the
106
+ // settings card carries the inject factory these tests drive.
107
+ if (registerOptions.name === 'settings.section') options = registerOptions
108
+ return () => {}
109
+ },
110
+ },
111
+ // apply consumes the services through their declarative-inject faces
112
+ // (ctx.locale / ctx.configForms), the same shape cordis binds on the host.
113
+ locale: { bind: () => (key: string) => key, register: () => {} },
114
+ configForms: { get: () => (stub.form as unknown as { getSnapshot(): unknown }) },
115
+ }
116
+ apply(ctx as never)
117
+ if (options === undefined) throw new Error('the settings card never registered')
118
+ const factory = options['inject'] as () => CompressionSelectorInjected
119
+ return { injected: factory(), stub }
120
+ }
121
+
122
+ describe('presetOptions writes commit the whole doc through configForms', () => {
123
+ it('keeps the estimator channel when a provider is chosen afterwards', async () => {
124
+ const { injected, stub } = bindInjected({ estimatorMode: 'host' })
125
+ await injected.savePresetOptions({ estimatorProvider: 'local-35b' })
126
+ expect(stub.snapshot()['presetOptions']).toEqual({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
127
+ expect(stub.commits.length).toBe(1)
128
+ })
129
+
130
+ it('treats a retired review-gate patch as a no-op instead of a write', async () => {
131
+ const { injected, stub } = bindInjected({ estimatorMode: 'host' })
132
+ // The patch surface no longer carries these keys, and the decoders accept
133
+ // them only so a legacy document keeps loading. Cast past the removed type
134
+ // so the test pins the RUNTIME behaviour: nothing is written.
135
+ await injected.savePresetOptions({
136
+ reviewMode: true,
137
+ reviewTimeoutTurns: 8,
138
+ cacheHitDiscountAlpha: 0.2,
139
+ reviewHighImpactTokens: 6000,
140
+ } as unknown as Parameters<typeof injected.savePresetOptions>[0])
141
+ expect(stub.commits.length).toBe(0)
142
+ expect(stub.snapshot()['presetOptions']).toEqual({ estimatorMode: 'host' })
143
+ })
144
+
145
+ it('never loses sibling overrides across whole-doc commits', async () => {
146
+ const { injected, stub } = bindInjected({
147
+ estimatorMode: 'direct',
148
+ estimatorBaseUrl: 'http://192.168.100.242:8200',
149
+ dedupeToolResults: false,
150
+ readState: false,
151
+ })
152
+ await injected.savePresetOptions({ estimatorModel: 'Qwen3.6-35B-A3B' })
153
+ await injected.savePresetOptions({ estimatorProvider: 'local-35b' })
154
+ expect(stub.snapshot()['presetOptions']).toEqual({
155
+ estimatorMode: 'direct',
156
+ estimatorBaseUrl: 'http://192.168.100.242:8200',
157
+ dedupeToolResults: false,
158
+ readState: false,
159
+ estimatorModel: 'Qwen3.6-35B-A3B',
160
+ estimatorProvider: 'local-35b',
161
+ })
162
+ // Two user actions, two whole-doc commits.
163
+ expect(stub.commits.length).toBe(2)
164
+ for (const doc of stub.commits) {
165
+ // Each commit carries the complete document under the volatile field.
166
+ expect(Object.keys(doc)).toEqual(['settings'])
167
+ }
168
+ })
169
+
170
+ it('clears exactly the field a patch names with undefined', async () => {
171
+ const { injected, stub } = bindInjected({ estimatorMode: 'direct', estimatorApiKey: 'sk-stored' })
172
+ await injected.savePresetOptions({ estimatorApiKey: undefined })
173
+ expect(stub.snapshot()['presetOptions']).toEqual({ estimatorMode: 'direct' })
174
+ })
175
+
176
+ it('writes nothing when the patch already matches the stored section', async () => {
177
+ const { injected, stub } = bindInjected({ estimatorMode: 'host', estimatorProvider: 'local-35b' })
178
+ await injected.savePresetOptions({ estimatorMode: 'host' })
179
+ expect(stub.commits.length).toBe(0)
180
+ expect(stub.refused.length).toBe(0)
181
+ })
182
+
183
+ it('reports a save the Host did not commit', async () => {
184
+ const { injected } = bindInjected({ estimatorMode: 'host' }, false)
185
+ await expect(injected.savePresetOptions({ estimatorProvider: 'local-35b' }))
186
+ .rejects.toThrow(/were not saved/)
187
+ })
188
+ })