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,88 +1,91 @@
1
- import { describe, expect, it, vi } from 'vitest'
2
- import type { ClientContext, SettingsScopeSnapshot } from '@deepseek-ai/dsh-client-runtime/client'
3
- import { DEFAULT_CUSTOM_COMPRESSION_POLICY } from '../src/profiles.ts'
4
- import { apply } from '../src/client/index.ts'
5
- import type {
6
- CompressionSelectorInjected,
7
- ContextCompressionSettings,
8
- } from '../src/client/CompressionProfileSelector.tsx'
9
-
10
- vi.mock('@deepseek-ai/dsh-client-ui-primitives', () => ({
11
- IconChevronDownOutline14: () => null,
12
- Menu: () => null,
13
- }))
14
-
15
- describe('codeSkeleton confirm-on-write client contract', () => {
16
- it('resolves only when the host confirms the toggled value and rejects silent drops', async () => {
17
- let revision = 3
18
- let commit = false
19
- let flipEnabled = false
20
- let value: ContextCompressionSettings = {
21
- profile: 'balanced',
22
- custom: structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY),
23
- autoCompact: { thresholdPercent: 80 },
24
- codeSkeleton: { enabled: false },
25
- }
26
- const snapshot = (): SettingsScopeSnapshot<ContextCompressionSettings> => ({
27
- status: 'ready',
28
- value,
29
- base: undefined,
30
- user: undefined,
31
- revision,
32
- writable: true,
33
- mode: 'host',
34
- })
35
- const scope = {
36
- getSnapshot: snapshot,
37
- subscribe: vi.fn(() => () => {}),
38
- set: vi.fn(async (field: string, next: unknown) => {
39
- if (!commit) return
40
- revision += 1
41
- if (field === 'codeSkeleton' && flipEnabled) {
42
- const accepted = structuredClone(next as ContextCompressionSettings['codeSkeleton'])
43
- accepted.enabled = !accepted.enabled
44
- value = { ...value, codeSkeleton: accepted }
45
- return
46
- }
47
- value = { ...value, [field]: next }
48
- }),
49
- unset: vi.fn(async () => {}),
50
- }
51
- let injected: (() => CompressionSelectorInjected) | undefined
52
- const ctx = {
53
- effect: (install: () => unknown) => { install() },
54
- locale: { register: vi.fn(() => () => {}) },
55
- settingsScope: { bind: vi.fn(() => scope) },
56
- slots: {
57
- inject: (_slot: string, install: () => unknown) => { install() },
58
- register: (registration: { inject: () => CompressionSelectorInjected }) => {
59
- injected = registration.inject
60
- return () => {}
61
- },
62
- },
63
- } as unknown as ClientContext
64
-
65
- apply(ctx)
66
- const actions = injected?.()
67
- expect(actions).toBeDefined()
68
- if (actions === undefined) throw new Error('selector injection was not registered')
69
-
70
- // A silent host drop leaves the revision untouched: the write must reject.
71
- await expect(actions.saveCodeSkeleton(true)).rejects.toThrow('were not saved')
72
-
73
- // A committed write resolves and persists the exact section payload.
74
- commit = true
75
- await expect(actions.saveCodeSkeleton(true)).resolves.toBeUndefined()
76
- expect(scope.set).toHaveBeenLastCalledWith('codeSkeleton', { enabled: true })
77
- expect(value.codeSkeleton).toEqual({ enabled: true })
78
-
79
- // A host that flips the toggled bit fails the acceptance check.
80
- flipEnabled = true
81
- await expect(actions.saveCodeSkeleton(false)).rejects.toThrow('were not saved')
82
-
83
- flipEnabled = false
84
- await expect(actions.saveCodeSkeleton(false)).resolves.toBeUndefined()
85
- expect(scope.set).toHaveBeenLastCalledWith('codeSkeleton', { enabled: false })
86
- expect(value.codeSkeleton).toEqual({ enabled: false })
87
- })
88
- })
1
+ import { describe, expect, it, vi } from 'vitest'
2
+ import type { Context as ClientContext } from '@deepseek-ai/cordis'
3
+ import type { SettingsScopeSnapshot } from '@deepseek-ai/dsh-client-ui-settings/client'
4
+ import { DEFAULT_CUSTOM_COMPRESSION_POLICY } from '../src/profiles.ts'
5
+ import { apply } from '../src/client/index.ts'
6
+ import type {
7
+ CompressionSelectorInjected,
8
+ ContextCompressionSettings,
9
+ } from '../src/client/CompressionProfileSelector.tsx'
10
+
11
+ vi.mock('@deepseek-ai/dsh-client-ui-primitives', () => ({
12
+ IconChevronDownOutline14: () => null,
13
+ Menu: () => null,
14
+ }))
15
+
16
+ describe('codeSkeleton confirm-on-write client contract', () => {
17
+ it('resolves only when the host confirms the toggled value and rejects silent drops', async () => {
18
+ let revision = 3
19
+ let commit = false
20
+ let flipEnabled = false
21
+ let value: ContextCompressionSettings = {
22
+ profile: 'balanced',
23
+ custom: structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY),
24
+ autoCompact: { thresholdPercent: 80 },
25
+ codeSkeleton: { enabled: false },
26
+ intentSummary: { enabled: false },
27
+ }
28
+ const snapshot = (): SettingsScopeSnapshot<ContextCompressionSettings> => ({
29
+ status: 'ready',
30
+ value,
31
+ base: undefined,
32
+ user: undefined,
33
+ revision,
34
+ writable: true,
35
+ mode: 'host',
36
+ })
37
+ const scope = {
38
+ getSnapshot: snapshot,
39
+ subscribe: vi.fn(() => () => {}),
40
+ set: vi.fn(async (field: string, next: unknown) => {
41
+ if (!commit) return
42
+ revision += 1
43
+ if (field === 'codeSkeleton' && flipEnabled) {
44
+ const accepted = structuredClone(next as ContextCompressionSettings['codeSkeleton'])
45
+ accepted.enabled = !accepted.enabled
46
+ value = { ...value, codeSkeleton: accepted }
47
+ return
48
+ }
49
+ value = { ...value, [field]: next }
50
+ }),
51
+ unset: vi.fn(async () => {}),
52
+ }
53
+ let injected: (() => CompressionSelectorInjected) | undefined
54
+ const ctx = {
55
+ effect: (install: () => unknown) => { install() },
56
+ locale: { register: vi.fn(() => () => {}) },
57
+ settingsScope: { bind: vi.fn(() => scope) },
58
+ slots: {
59
+ inject: (_slot: string, install: () => unknown) => { install() },
60
+ register: (registration: { name?: string, inject?: () => CompressionSelectorInjected }) => {
61
+ // The R4 overlay registration carries no inject face; keep the card's.
62
+ if (registration.name === 'settings.section' && typeof registration.inject === 'function') injected = registration.inject
63
+ return () => {}
64
+ },
65
+ },
66
+ } as unknown as ClientContext
67
+
68
+ apply(ctx)
69
+ const actions = injected?.()
70
+ expect(actions).toBeDefined()
71
+ if (actions === undefined) throw new Error('selector injection was not registered')
72
+
73
+ // A silent host drop leaves the revision untouched: the write must reject.
74
+ await expect(actions.saveCodeSkeleton(true)).rejects.toThrow('were not saved')
75
+
76
+ // A committed write resolves and persists the exact section payload.
77
+ commit = true
78
+ await expect(actions.saveCodeSkeleton(true)).resolves.toBeUndefined()
79
+ expect(scope.set).toHaveBeenLastCalledWith('codeSkeleton', { enabled: true })
80
+ expect(value.codeSkeleton).toEqual({ enabled: true })
81
+
82
+ // A host that flips the toggled bit fails the acceptance check.
83
+ flipEnabled = true
84
+ await expect(actions.saveCodeSkeleton(false)).rejects.toThrow('were not saved')
85
+
86
+ flipEnabled = false
87
+ await expect(actions.saveCodeSkeleton(false)).resolves.toBeUndefined()
88
+ expect(scope.set).toHaveBeenLastCalledWith('codeSkeleton', { enabled: false })
89
+ expect(value.codeSkeleton).toEqual({ enabled: false })
90
+ })
91
+ })
@@ -1,202 +1,205 @@
1
- import { describe, expect, it, vi } from 'vitest'
2
- import type { ClientContext, SettingsScopeSnapshot } from '@deepseek-ai/dsh-client-runtime/client'
3
- import {
4
- DEFAULT_CUSTOM_COMPRESSION_POLICY,
5
- isCustomCompressionPolicy,
6
- } from '../src/profiles.ts'
7
- import { apply } from '../src/client/index.ts'
8
- import { decodeSettings } from '../src/client/decode.ts'
9
- import type {
10
- CompressionSelectorInjected,
11
- ContextCompressionSettings,
12
- } from '../src/client/CompressionProfileSelector.tsx'
13
-
14
- vi.mock('@deepseek-ai/dsh-client-ui-primitives', () => ({
15
- IconChevronDownOutline14: () => null,
16
- Menu: () => null,
17
- }))
18
-
19
- const CUSTOM = {
20
- version: 1,
21
- unit: 'tokens',
22
- fresh: { enabled: true, trigger: 4_096, target: 2_048 },
23
- aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
24
- history: {
25
- enabled: true,
26
- trigger: 32_768,
27
- keepRecentTurns: 2,
28
- keepRecent: 16_384,
29
- minReclaim: 8_192,
30
- },
31
- prefixPolicy: 'pressure-break',
32
- } as const satisfies ContextCompressionSettings['custom']
33
-
34
- describe('context compression browser contract', () => {
35
- it('accepts legacy v1 and defaults new edits to v3 with TailTrim disabled', () => {
36
- expect(isCustomCompressionPolicy(CUSTOM)).toBe(true)
37
- expect(DEFAULT_CUSTOM_COMPRESSION_POLICY).toMatchObject({
38
- version: 3,
39
- tailTrim: { enabled: false, trigger: 700_000 },
40
- })
41
- expect(isCustomCompressionPolicy(DEFAULT_CUSTOM_COMPRESSION_POLICY)).toBe(true)
42
- })
43
-
44
- it('fails the browser narrowing for unknown or invalid Custom fields', () => {
45
- expect(isCustomCompressionPolicy({
46
- ...structuredClone(CUSTOM),
47
- tailTrim: { enabled: true },
48
- })).toBe(false)
49
- expect(isCustomCompressionPolicy({
50
- ...structuredClone(CUSTOM),
51
- fresh: { enabled: true, trigger: 100, target: 100 },
52
- })).toBe(false)
53
- for (const trigger of [0, -1]) {
54
- expect(isCustomCompressionPolicy({
55
- ...structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY),
56
- tailTrim: { enabled: true, trigger },
57
- })).toBe(false)
58
- }
59
- expect(isCustomCompressionPolicy({
60
- ...structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY),
61
- unit: 'context-percent',
62
- tailTrim: { enabled: true, trigger: 101 },
63
- })).toBe(false)
64
- })
65
-
66
- it('decodes legacy settings to the default threshold and rejects malformed sections', async () => {
67
- // Legacy documents stored before autoCompact existed inherit 80%.
68
- const legacy = decodeSettings({ profile: 'balanced', custom: structuredClone(CUSTOM) })
69
- expect(legacy).toMatchObject({ autoCompact: { thresholdPercent: 80 } })
70
- // A present-but-invalid threshold fails the whole decode.
71
- expect(decodeSettings({
72
- profile: 'balanced',
73
- custom: structuredClone(CUSTOM),
74
- autoCompact: { thresholdPercent: 91 },
75
- })).toBeUndefined()
76
- })
77
-
78
- it('reports a recovered no-op write only on the selector boundary', async () => {
79
- let revision = 7
80
- let commit = false
81
- let rewriteCustom = false
82
- let rewriteReset = false
83
- let dropTailTrim = false
84
- let downgradeVersion = false
85
- let value: ContextCompressionSettings = {
86
- profile: 'balanced',
87
- custom: structuredClone(CUSTOM),
88
- autoCompact: { thresholdPercent: 80 },
89
- codeSkeleton: { enabled: false },
90
- }
91
- const snapshot = (): SettingsScopeSnapshot<ContextCompressionSettings> => ({
92
- status: 'ready',
93
- value,
94
- base: undefined,
95
- user: undefined,
96
- revision,
97
- writable: true,
98
- mode: 'host',
99
- })
100
- const scope = {
101
- getSnapshot: snapshot,
102
- subscribe: vi.fn(() => () => {}),
103
- set: vi.fn(async (field: string, next: unknown) => {
104
- if (!commit) return
105
- revision += 1
106
- if (field === 'custom' && rewriteCustom) {
107
- const accepted = structuredClone(next as ContextCompressionSettings['custom'])
108
- accepted.fresh.trigger += 1
109
- value = { ...value, custom: accepted }
110
- return
111
- }
112
- if (field === 'custom' && rewriteReset) {
113
- const accepted = structuredClone(next as ContextCompressionSettings['custom'])
114
- accepted.fresh.trigger += 1
115
- value = { ...value, custom: accepted }
116
- return
117
- }
118
- if (field === 'custom' && dropTailTrim) {
119
- const accepted = structuredClone(next as ContextCompressionSettings['custom'])
120
- if (accepted.version === 3) {
121
- accepted.tailTrim = { enabled: false, trigger: 700_000 }
122
- }
123
- value = { ...value, custom: accepted }
124
- return
125
- }
126
- if (field === 'custom' && downgradeVersion) {
127
- value = { ...value, custom: structuredClone(CUSTOM) }
128
- return
129
- }
130
- value = { ...value, [field]: next }
131
- }),
132
- unset: vi.fn(async () => {
133
- if (!commit) return
134
- revision += 1
135
- const custom: ContextCompressionSettings['custom'] = structuredClone(CUSTOM)
136
- if (rewriteReset) {
137
- custom.fresh.trigger = 8_192
138
- custom.fresh.target = 4_096
139
- }
140
- value = { ...value, custom }
141
- }),
142
- }
143
- let injected: (() => CompressionSelectorInjected) | undefined
144
- const ctx = {
145
- effect: (install: () => unknown) => { install() },
146
- locale: { register: vi.fn(() => () => {}) },
147
- settingsScope: { bind: vi.fn(() => scope) },
148
- slots: {
149
- inject: (_slot: string, install: () => unknown) => { install() },
150
- register: (registration: { inject: () => CompressionSelectorInjected }) => {
151
- injected = registration.inject
152
- return () => {}
153
- },
154
- },
155
- } as unknown as ClientContext
156
-
157
- apply(ctx)
158
- const actions = injected?.()
159
- expect(actions).toBeDefined()
160
- if (actions === undefined) throw new Error('selector injection was not registered')
161
- await expect(actions.select('native')).rejects.toThrow('were not saved')
162
-
163
- commit = true
164
- await expect(actions.select('native')).resolves.toBeUndefined()
165
- expect(value.profile).toBe('native')
166
-
167
- const requested: ContextCompressionSettings['custom'] = structuredClone(CUSTOM)
168
- requested.fresh.trigger = 8_192
169
- requested.fresh.target = 4_096
170
- rewriteCustom = true
171
- await expect(actions.saveCustom(requested)).rejects.toThrow('were not saved')
172
-
173
- rewriteCustom = false
174
- const requestedV3 = structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY)
175
- if (requestedV3.version !== 3) throw new Error('Custom TailTrim persistence fixture requires policy v3')
176
- requestedV3.tailTrim = { enabled: true, trigger: 49_152 }
177
- dropTailTrim = true
178
- await expect(actions.saveCustom(requestedV3)).rejects.toThrow('were not saved')
179
-
180
- dropTailTrim = false
181
- downgradeVersion = true
182
- await expect(actions.saveCustom(requestedV3)).rejects.toThrow('were not saved')
183
-
184
- downgradeVersion = false
185
- rewriteReset = true
186
- await expect(actions.resetCustom()).rejects.toThrow('were not saved')
187
-
188
- rewriteReset = false
189
- await expect(actions.resetCustom()).resolves.toBeUndefined()
190
- expect(scope.set).toHaveBeenLastCalledWith('custom', DEFAULT_CUSTOM_COMPRESSION_POLICY)
191
- expect(scope.unset).not.toHaveBeenCalled()
192
-
193
- // The Auto Compact threshold shares the same confirm-on-write boundary:
194
- // committed writes resolve, and a silent host drop rejects.
195
- commit = false
196
- await expect(actions.saveAutoCompact(73)).rejects.toThrow('were not saved')
197
- commit = true
198
- await expect(actions.saveAutoCompact(73)).resolves.toBeUndefined()
199
- expect(scope.set).toHaveBeenLastCalledWith('autoCompact', { thresholdPercent: 73 })
200
- expect(value.autoCompact).toEqual({ thresholdPercent: 73 })
201
- })
202
- })
1
+ import { describe, expect, it, vi } from 'vitest'
2
+ import type { Context as ClientContext } from '@deepseek-ai/cordis'
3
+ import type { SettingsScopeSnapshot } from '@deepseek-ai/dsh-client-ui-settings/client'
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
+ IconChevronDownOutline14: () => 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 = (): SettingsScopeSnapshot<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
+ settingsScope: { bind: vi.fn(() => scope) },
150
+ slots: {
151
+ inject: (_slot: string, install: () => unknown) => { install() },
152
+ register: (registration: { name?: string, inject?: () => CompressionSelectorInjected }) => {
153
+ // The R4 overlay registration carries no inject face; keep the card's.
154
+ if (registration.name === 'settings.section' && typeof registration.inject === 'function') injected = registration.inject
155
+ return () => {}
156
+ },
157
+ },
158
+ } as unknown as ClientContext
159
+
160
+ apply(ctx)
161
+ const actions = injected?.()
162
+ expect(actions).toBeDefined()
163
+ if (actions === undefined) throw new Error('selector injection was not registered')
164
+ await expect(actions.select('native')).rejects.toThrow('were not saved')
165
+
166
+ commit = true
167
+ await expect(actions.select('native')).resolves.toBeUndefined()
168
+ expect(value.profile).toBe('native')
169
+
170
+ const requested: ContextCompressionSettings['custom'] = structuredClone(CUSTOM)
171
+ requested.fresh.trigger = 8_192
172
+ requested.fresh.target = 4_096
173
+ rewriteCustom = true
174
+ await expect(actions.saveCustom(requested)).rejects.toThrow('were not saved')
175
+
176
+ rewriteCustom = false
177
+ const requestedV3 = structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY)
178
+ if (requestedV3.version !== 3) throw new Error('Custom TailTrim persistence fixture requires policy v3')
179
+ requestedV3.tailTrim = { enabled: true, trigger: 49_152 }
180
+ dropTailTrim = true
181
+ await expect(actions.saveCustom(requestedV3)).rejects.toThrow('were not saved')
182
+
183
+ dropTailTrim = false
184
+ downgradeVersion = true
185
+ await expect(actions.saveCustom(requestedV3)).rejects.toThrow('were not saved')
186
+
187
+ downgradeVersion = false
188
+ rewriteReset = true
189
+ await expect(actions.resetCustom()).rejects.toThrow('were not saved')
190
+
191
+ rewriteReset = false
192
+ await expect(actions.resetCustom()).resolves.toBeUndefined()
193
+ expect(scope.set).toHaveBeenLastCalledWith('custom', DEFAULT_CUSTOM_COMPRESSION_POLICY)
194
+ expect(scope.unset).not.toHaveBeenCalled()
195
+
196
+ // The Auto Compact threshold shares the same confirm-on-write boundary:
197
+ // committed writes resolve, and a silent host drop rejects.
198
+ commit = false
199
+ await expect(actions.saveAutoCompact(73)).rejects.toThrow('were not saved')
200
+ commit = true
201
+ await expect(actions.saveAutoCompact(73)).resolves.toBeUndefined()
202
+ expect(scope.set).toHaveBeenLastCalledWith('autoCompact', { thresholdPercent: 73 })
203
+ expect(value.autoCompact).toEqual({ thresholdPercent: 73 })
204
+ })
205
+ })