dsh-context-compression-improved 0.6.5 → 0.7.0-beta.2

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 (70) hide show
  1. package/.github/workflows/ci.yml +39 -39
  2. package/CHANGELOG.ja.md +217 -211
  3. package/CHANGELOG.ko.md +211 -205
  4. package/CHANGELOG.md +392 -350
  5. package/CHANGELOG.zh.md +196 -190
  6. package/README.md +97 -96
  7. package/README.zh.md +86 -85
  8. package/eslint.config.js +76 -30
  9. package/package.json +3 -1
  10. package/packages/selector/dsh.plugin.json +1 -1
  11. package/packages/selector/lib/client.js +747 -3
  12. package/packages/selector/lib/index.js +92 -1
  13. package/packages/selector/lib/{advisor-state.js → intent-gate.js} +91 -3
  14. package/packages/selector/lib/profiles.d.ts +9 -0
  15. package/packages/selector/lib/pruner.d.ts +59 -0
  16. package/packages/selector/lib/pruner.js +410 -8
  17. package/packages/selector/src/client/decode.ts +53 -49
  18. package/packages/selector/src/client/index.ts +30 -0
  19. package/packages/selector/src/client/locales/de.ts +98 -0
  20. package/packages/selector/src/client/locales/es.ts +98 -0
  21. package/packages/selector/src/client/locales/fr.ts +98 -0
  22. package/packages/selector/src/client/locales/it.ts +98 -0
  23. package/packages/selector/src/client/locales/ja.ts +98 -0
  24. package/packages/selector/src/client/locales/ko.ts +98 -0
  25. package/packages/selector/src/client/locales/ru.ts +98 -0
  26. package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -205
  27. package/packages/selector/src/index.ts +418 -411
  28. package/packages/selector/src/preset-overlay.ts +629 -626
  29. package/packages/selector/src/profiles.ts +414 -394
  30. package/packages/selector/src/pruner/content.ts +198 -198
  31. package/packages/selector/src/pruner/state.ts +57 -50
  32. package/packages/selector/src/pruner.ts +2665 -2421
  33. package/packages/selector/src/runtime/audit.ts +303 -286
  34. package/packages/selector/src/runtime/config.ts +731 -706
  35. package/packages/selector/src/runtime/reducers.ts +1674 -1674
  36. package/packages/selector/src/runtime/summary-command.ts +133 -0
  37. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -188
  38. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -149
  39. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
  40. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
  41. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
  42. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
  43. package/packages/selector/src/runtime/tokenpilot/locator.ts +128 -128
  44. package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +306 -303
  45. package/packages/selector/src/runtime/types.ts +358 -346
  46. package/packages/selector/tests/auto-compact.client.spec.tsx +214 -213
  47. package/packages/selector/tests/built/client-artifact.spec.ts +61 -59
  48. package/packages/selector/tests/code-skeleton.client.spec.ts +94 -93
  49. package/packages/selector/tests/custom-contract.client.spec.ts +223 -222
  50. package/packages/selector/tests/estimator-channel.client.spec.tsx +252 -252
  51. package/packages/selector/tests/preset-options-write.client.spec.ts +188 -187
  52. package/packages/selector/tests/profiles.client.spec.tsx +436 -434
  53. package/packages/selector/tests/runtime/audit.spec.ts +275 -274
  54. package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -523
  55. package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -122
  56. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
  57. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
  58. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +5 -0
  59. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
  60. package/packages/selector/tests/standing-generation.host.spec.ts +15 -35
  61. package/scripts/packed-components-smoke.mjs +734 -732
  62. package/scripts/packed-install-e2e.mjs +1275 -1273
  63. package/scripts/verify-release.mjs +318 -317
  64. package/tests/TEST_INVENTORY.md +41 -41
  65. package/tsconfig.tests.json +17 -15
  66. package/vitest.config.ts +57 -43
  67. package/.agents-stash-064.patch +0 -39
  68. package/_dsh_devdeps.json +0 -50
  69. package/_verify064/package/package.json +0 -162
  70. package/_verify064/package/packages/selector/src/client/index.ts +0 -232
@@ -1,93 +1,94 @@
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
- }
28
- const form = {
29
- getSnapshot: vi.fn(() => ({
30
- status: 'ready' as const,
31
- value: { settings: value },
32
- base: undefined,
33
- user: undefined,
34
- revision,
35
- writable: true,
36
- mode: 'host' as const,
37
- })),
38
- subscribe: vi.fn(() => () => {}),
39
- set: vi.fn(async (_field: string, next: unknown) => {
40
- if (!commit) return false
41
- revision += 1
42
- const doc = structuredClone(next) as ContextCompressionSettings
43
- if (flipEnabled) {
44
- const accepted = structuredClone(doc.codeSkeleton)
45
- accepted.enabled = !accepted.enabled
46
- value = { ...doc, codeSkeleton: accepted }
47
- return true
48
- }
49
- value = doc
50
- return true
51
- }),
52
- unset: vi.fn(async () => false),
53
- }
54
- let injected: (() => CompressionSelectorInjected) | undefined
55
- const ctx = {
56
- effect: (install: () => unknown) => { install() },
57
- locale: { register: vi.fn(() => () => {}) },
58
- configForms: { get: vi.fn(() => form) },
59
- slots: {
60
- inject: (_slot: string, install: () => unknown) => { install() },
61
- register: (registration: { inject?: () => CompressionSelectorInjected }) => {
62
- // The R4 overlay registration carries no inject face; keep the card's.
63
- if (typeof registration.inject === 'function') injected = registration.inject
64
- return () => {}
65
- },
66
- },
67
- } as unknown as Parameters<typeof apply>[0]
68
-
69
- apply(ctx)
70
- const actions = injected?.()
71
- expect(actions).toBeDefined()
72
- if (actions === undefined) throw new Error('selector injection was not registered')
73
-
74
- // A silent host drop leaves the revision untouched: the write must reject.
75
- await expect(actions.saveCodeSkeleton(true)).rejects.toThrow('were not saved')
76
-
77
- // A committed write resolves and persists the exact section payload under
78
- // the volatile `settings` field.
79
- commit = true
80
- await expect(actions.saveCodeSkeleton(true)).resolves.toBeUndefined()
81
- expect(form.set).toHaveBeenLastCalledWith('settings', expect.objectContaining({ codeSkeleton: { enabled: true } }))
82
- expect(value.codeSkeleton).toEqual({ enabled: true })
83
-
84
- // A host that flips the toggled bit fails the acceptance check.
85
- flipEnabled = true
86
- await expect(actions.saveCodeSkeleton(false)).rejects.toThrow('were not saved')
87
-
88
- flipEnabled = false
89
- await expect(actions.saveCodeSkeleton(false)).resolves.toBeUndefined()
90
- expect(form.set).toHaveBeenLastCalledWith('settings', expect.objectContaining({ codeSkeleton: { enabled: false } }))
91
- expect(value.codeSkeleton).toEqual({ enabled: false })
92
- })
93
- })
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
+ slots: {
61
+ inject: (_slot: string, install: () => unknown) => { install() },
62
+ register: (registration: { inject?: () => CompressionSelectorInjected }) => {
63
+ // The R4 overlay registration carries no inject face; keep the card's.
64
+ if (typeof registration.inject === 'function') injected = registration.inject
65
+ return () => {}
66
+ },
67
+ },
68
+ } as unknown as Parameters<typeof apply>[0]
69
+
70
+ apply(ctx)
71
+ const actions = injected?.()
72
+ expect(actions).toBeDefined()
73
+ if (actions === undefined) throw new Error('selector injection was not registered')
74
+
75
+ // A silent host drop leaves the revision untouched: the write must reject.
76
+ await expect(actions.saveCodeSkeleton(true)).rejects.toThrow('were not saved')
77
+
78
+ // A committed write resolves and persists the exact section payload under
79
+ // the volatile `settings` field.
80
+ commit = true
81
+ await expect(actions.saveCodeSkeleton(true)).resolves.toBeUndefined()
82
+ expect(form.set).toHaveBeenLastCalledWith('settings', expect.objectContaining({ codeSkeleton: { enabled: true } }))
83
+ expect(value.codeSkeleton).toEqual({ enabled: true })
84
+
85
+ // A host that flips the toggled bit fails the acceptance check.
86
+ flipEnabled = true
87
+ await expect(actions.saveCodeSkeleton(false)).rejects.toThrow('were not saved')
88
+
89
+ flipEnabled = false
90
+ await expect(actions.saveCodeSkeleton(false)).resolves.toBeUndefined()
91
+ expect(form.set).toHaveBeenLastCalledWith('settings', expect.objectContaining({ codeSkeleton: { enabled: false } }))
92
+ expect(value.codeSkeleton).toEqual({ enabled: false })
93
+ })
94
+ })
@@ -1,222 +1,223 @@
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
- }
92
- const snapshot = (): ScopeSnapshot<ContextCompressionSettings> => ({
93
- status: 'ready',
94
- value,
95
- base: undefined,
96
- user: undefined,
97
- revision,
98
- writable: true,
99
- mode: 'host',
100
- })
101
- const scope = {
102
- getSnapshot: snapshot,
103
- subscribe: vi.fn(() => () => {}),
104
- set: vi.fn(async (field: string, next: unknown) => {
105
- if (!commit) return
106
- revision += 1
107
- if (field === 'custom' && rewriteCustom) {
108
- const accepted = structuredClone(next as ContextCompressionSettings['custom'])
109
- accepted.fresh.trigger += 1
110
- value = { ...value, custom: accepted }
111
- return
112
- }
113
- if (field === 'custom' && rewriteReset) {
114
- const accepted = structuredClone(next as ContextCompressionSettings['custom'])
115
- accepted.fresh.trigger += 1
116
- value = { ...value, custom: accepted }
117
- return
118
- }
119
- if (field === 'custom' && dropTailTrim) {
120
- const accepted = structuredClone(next as ContextCompressionSettings['custom'])
121
- if (accepted.version === 3) {
122
- accepted.tailTrim = { enabled: false, trigger: 700_000 }
123
- }
124
- value = { ...value, custom: accepted }
125
- return
126
- }
127
- if (field === 'custom' && downgradeVersion) {
128
- value = { ...value, custom: structuredClone(CUSTOM) }
129
- return
130
- }
131
- value = { ...value, [field]: next }
132
- }),
133
- unset: vi.fn(async () => {
134
- if (!commit) return
135
- revision += 1
136
- const custom: ContextCompressionSettings['custom'] = structuredClone(CUSTOM)
137
- if (rewriteReset) {
138
- custom.fresh.trigger = 8_192
139
- custom.fresh.target = 4_096
140
- }
141
- value = { ...value, custom }
142
- }),
143
- }
144
- let injected: (() => CompressionSelectorInjected) | undefined
145
- const ctx = {
146
- effect: (install: () => unknown) => { install() },
147
- locale: { register: vi.fn(() => () => {}) },
148
- // 0.1.7: the plugin commits the whole doc through configForms; the fake
149
- // routes each changed top-level field back onto the old scope stub so
150
- // the rewrite/drop semantics below stay observable per field.
151
- configForms: {
152
- get: vi.fn(() => ({
153
- getSnapshot: () => ({ ...snapshot(), value: { settings: value } }),
154
- subscribe: scope.subscribe,
155
- set: async (_field: string, doc: unknown) => {
156
- const next = doc as unknown as Record<string, unknown>
157
- for (const key of Object.keys(next)) {
158
- if (JSON.stringify((value as unknown as Record<string, unknown>)[key]) !== JSON.stringify(next[key])) {
159
- await scope.set(key, (next as Record<string, unknown>)[key])
160
- }
161
- }
162
- return true
163
- },
164
- unset: scope.unset,
165
- })),
166
- },
167
- slots: {
168
- inject: (_slot: string, install: () => unknown) => { install() },
169
- register: (registration: { inject?: () => CompressionSelectorInjected }) => {
170
- // The R4 overlay registration carries no inject face; keep the card's.
171
- if (typeof registration.inject === 'function') injected = registration.inject
172
- return () => {}
173
- },
174
- },
175
- } as unknown as ClientContext
176
-
177
- apply(ctx)
178
- const actions = injected?.()
179
- expect(actions).toBeDefined()
180
- if (actions === undefined) throw new Error('selector injection was not registered')
181
- await expect(actions.select('native')).rejects.toThrow('were not saved')
182
-
183
- commit = true
184
- await expect(actions.select('native')).resolves.toBeUndefined()
185
- expect(value.profile).toBe('native')
186
-
187
- const requested: ContextCompressionSettings['custom'] = structuredClone(CUSTOM)
188
- requested.fresh.trigger = 8_192
189
- requested.fresh.target = 4_096
190
- rewriteCustom = true
191
- await expect(actions.saveCustom(requested)).rejects.toThrow('were not saved')
192
-
193
- rewriteCustom = false
194
- const requestedV3 = structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY)
195
- if (requestedV3.version !== 3) throw new Error('Custom TailTrim persistence fixture requires policy v3')
196
- requestedV3.tailTrim = { enabled: true, trigger: 49_152 }
197
- dropTailTrim = true
198
- await expect(actions.saveCustom(requestedV3)).rejects.toThrow('were not saved')
199
-
200
- dropTailTrim = false
201
- downgradeVersion = true
202
- await expect(actions.saveCustom(requestedV3)).rejects.toThrow('were not saved')
203
-
204
- downgradeVersion = false
205
- rewriteReset = true
206
- await expect(actions.resetCustom()).rejects.toThrow('were not saved')
207
-
208
- rewriteReset = false
209
- await expect(actions.resetCustom()).resolves.toBeUndefined()
210
- expect(scope.set).toHaveBeenLastCalledWith('custom', DEFAULT_CUSTOM_COMPRESSION_POLICY)
211
- expect(scope.unset).not.toHaveBeenCalled()
212
-
213
- // The Auto Compact threshold shares the same confirm-on-write boundary:
214
- // committed writes resolve, and a silent host drop rejects.
215
- commit = false
216
- await expect(actions.saveAutoCompact(73)).rejects.toThrow('were not saved')
217
- commit = true
218
- await expect(actions.saveAutoCompact(73)).resolves.toBeUndefined()
219
- expect(scope.set).toHaveBeenLastCalledWith('autoCompact', { thresholdPercent: 73 })
220
- expect(value.autoCompact).toEqual({ thresholdPercent: 73 })
221
- })
222
- })
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
+ configForms: {
153
+ get: vi.fn(() => ({
154
+ getSnapshot: () => ({ ...snapshot(), value: { settings: value } }),
155
+ subscribe: scope.subscribe,
156
+ set: async (_field: string, doc: unknown) => {
157
+ const next = doc as Record<string, unknown>
158
+ for (const key of Object.keys(next)) {
159
+ if (JSON.stringify((value as unknown as Record<string, unknown>)[key]) !== JSON.stringify(next[key])) {
160
+ await scope.set(key, (next)[key])
161
+ }
162
+ }
163
+ return true
164
+ },
165
+ unset: scope.unset,
166
+ })),
167
+ },
168
+ slots: {
169
+ inject: (_slot: string, install: () => unknown) => { install() },
170
+ register: (registration: { inject?: () => CompressionSelectorInjected }) => {
171
+ // The R4 overlay registration carries no inject face; keep the card's.
172
+ if (typeof registration.inject === 'function') injected = registration.inject
173
+ return () => {}
174
+ },
175
+ },
176
+ } as unknown as ClientContext
177
+
178
+ apply(ctx)
179
+ const actions = injected?.()
180
+ expect(actions).toBeDefined()
181
+ if (actions === undefined) throw new Error('selector injection was not registered')
182
+ await expect(actions.select('native')).rejects.toThrow('were not saved')
183
+
184
+ commit = true
185
+ await expect(actions.select('native')).resolves.toBeUndefined()
186
+ expect(value.profile).toBe('native')
187
+
188
+ const requested: ContextCompressionSettings['custom'] = structuredClone(CUSTOM)
189
+ requested.fresh.trigger = 8_192
190
+ requested.fresh.target = 4_096
191
+ rewriteCustom = true
192
+ await expect(actions.saveCustom(requested)).rejects.toThrow('were not saved')
193
+
194
+ rewriteCustom = false
195
+ const requestedV3 = structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY)
196
+ if (requestedV3.version !== 3) throw new Error('Custom TailTrim persistence fixture requires policy v3')
197
+ requestedV3.tailTrim = { enabled: true, trigger: 49_152 }
198
+ dropTailTrim = true
199
+ await expect(actions.saveCustom(requestedV3)).rejects.toThrow('were not saved')
200
+
201
+ dropTailTrim = false
202
+ downgradeVersion = true
203
+ await expect(actions.saveCustom(requestedV3)).rejects.toThrow('were not saved')
204
+
205
+ downgradeVersion = false
206
+ rewriteReset = true
207
+ await expect(actions.resetCustom()).rejects.toThrow('were not saved')
208
+
209
+ rewriteReset = false
210
+ await expect(actions.resetCustom()).resolves.toBeUndefined()
211
+ expect(scope.set).toHaveBeenLastCalledWith('custom', DEFAULT_CUSTOM_COMPRESSION_POLICY)
212
+ expect(scope.unset).not.toHaveBeenCalled()
213
+
214
+ // The Auto Compact threshold shares the same confirm-on-write boundary:
215
+ // committed writes resolve, and a silent host drop rejects.
216
+ commit = false
217
+ await expect(actions.saveAutoCompact(73)).rejects.toThrow('were not saved')
218
+ commit = true
219
+ await expect(actions.saveAutoCompact(73)).resolves.toBeUndefined()
220
+ expect(scope.set).toHaveBeenLastCalledWith('autoCompact', { thresholdPercent: 73 })
221
+ expect(value.autoCompact).toEqual({ thresholdPercent: 73 })
222
+ })
223
+ })