dsh-context-compression-improved 0.7.0-beta.1 → 0.7.0

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 (83) hide show
  1. package/.github/workflows/ci.yml +39 -39
  2. package/CHANGELOG.ja.md +217 -220
  3. package/CHANGELOG.ko.md +211 -214
  4. package/CHANGELOG.md +401 -376
  5. package/CHANGELOG.zh.md +196 -199
  6. package/README.md +97 -97
  7. package/README.zh.md +86 -86
  8. package/package.json +1 -1
  9. package/packages/selector/dsh.plugin.json +1 -1
  10. package/packages/selector/lib/client.d.ts +44 -0
  11. package/packages/selector/lib/client.js +1733 -6
  12. package/packages/selector/lib/{intent-gate.js → config.js} +799 -136
  13. package/packages/selector/lib/index.d.ts +13 -1
  14. package/packages/selector/lib/index.js +204 -3
  15. package/packages/selector/lib/profiles.d.ts +6 -0
  16. package/packages/selector/lib/pruner.d.ts +6 -0
  17. package/packages/selector/lib/pruner.js +90 -258
  18. package/packages/selector/lib/style.css +88 -46
  19. package/packages/selector/src/client/CompressionProfileControls.tsx +293 -226
  20. package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -170
  21. package/packages/selector/src/client/CompressionProfileSelector.tsx +85 -83
  22. package/packages/selector/src/client/decode.ts +57 -53
  23. package/packages/selector/src/client/index.ts +307 -213
  24. package/packages/selector/src/client/locales/de.ts +141 -0
  25. package/packages/selector/src/client/locales/es.ts +141 -0
  26. package/packages/selector/src/client/locales/fr.ts +141 -0
  27. package/packages/selector/src/client/locales/it.ts +141 -0
  28. package/packages/selector/src/client/locales/ja.ts +141 -0
  29. package/packages/selector/src/client/locales/ko.ts +141 -0
  30. package/packages/selector/src/client/locales/ru.ts +141 -0
  31. package/packages/selector/src/client/locales.ts +282 -196
  32. package/packages/selector/src/client/monitor-fab.ts +336 -0
  33. package/packages/selector/src/client/savings-card.tsx +150 -0
  34. package/packages/selector/src/client/settings-section.tsx +140 -124
  35. package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -205
  36. package/packages/selector/src/index.ts +207 -5
  37. package/packages/selector/src/preset-overlay.ts +629 -629
  38. package/packages/selector/src/profiles.ts +431 -414
  39. package/packages/selector/src/pruner/content.ts +198 -198
  40. package/packages/selector/src/pruner/state.ts +57 -57
  41. package/packages/selector/src/pruner.ts +2759 -2665
  42. package/packages/selector/src/runtime/audit.ts +303 -303
  43. package/packages/selector/src/runtime/config.ts +756 -731
  44. package/packages/selector/src/runtime/monitor.ts +235 -0
  45. package/packages/selector/src/runtime/reducers.ts +1674 -1674
  46. package/packages/selector/src/runtime/retrieve.ts +479 -457
  47. package/packages/selector/src/runtime/savings.ts +337 -0
  48. package/packages/selector/src/runtime/summary-command.ts +133 -133
  49. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -233
  50. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -204
  51. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -53
  52. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -65
  53. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -125
  54. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -37
  55. package/packages/selector/src/runtime/tokenpilot/locator.ts +128 -128
  56. package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +306 -306
  57. package/packages/selector/src/runtime/types.ts +365 -358
  58. package/packages/selector/tests/built/client-artifact.spec.ts +61 -61
  59. package/packages/selector/tests/code-skeleton.client.spec.ts +2 -2
  60. package/packages/selector/tests/custom-contract.client.spec.ts +2 -2
  61. package/packages/selector/tests/estimator-channel.client.spec.tsx +2 -0
  62. package/packages/selector/tests/estimator-route-registration.host.spec.ts +194 -188
  63. package/packages/selector/tests/helpers/legacy-settings/index.ts +15 -15
  64. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.d.ts +41 -41
  65. package/packages/selector/tests/helpers/legacy-settings/legacy/provider.js +647 -647
  66. package/packages/selector/tests/helpers/legacy-settings/legacy/redact.js +78 -78
  67. package/packages/selector/tests/helpers/legacy-settings/legacy/util-values.js +234 -234
  68. package/packages/selector/tests/host-preset-overlay.host.spec.ts +203 -203
  69. package/packages/selector/tests/monitor-fab.client.spec.ts +138 -0
  70. package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -523
  71. package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -122
  72. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -62
  73. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -156
  74. package/packages/selector/tests/runtime/monitor.spec.ts +301 -0
  75. package/packages/selector/tests/runtime/savings.spec.ts +120 -0
  76. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -154
  77. package/packages/selector/tests/settings-seat.client.spec.ts +177 -165
  78. package/scripts/packed-components-smoke.mjs +734 -734
  79. package/scripts/packed-install-e2e.mjs +1 -1
  80. package/scripts/verify-release.mjs +1 -1
  81. package/tests/TEST_INVENTORY.md +41 -41
  82. package/tsconfig.tests.json +17 -17
  83. package/vitest.config.ts +57 -57
@@ -1,523 +1,523 @@
1
- /** Auto Compact threshold settings schema and History linkage formula. */
2
-
3
- import { describe, expect, it } from 'vitest'
4
- import {
5
- AUTO_COMPACT_THRESHOLD_LIMITS,
6
- ContextCompressionSettingsSchema,
7
- DEFAULT_CUSTOM_COMPRESSION_POLICY,
8
- isValidAutoCompactThresholdPercent,
9
- parseContextCompressionSettings,
10
- resolveConfig,
11
- resolvePolicy,
12
- } from '../../src/pruner.ts'
13
- import {
14
- AUTO_COMPACT_THRESHOLD_LIMITS as BROWSER_LIMITS,
15
- decodeAutoCompactSettings as browserDecodeAutoCompact,
16
- isValidAutoCompactThresholdPercent as browserIsValid,
17
- } from '../../src/profiles.ts'
18
- import { decodeSettings as browserDecodeSettings } from '../../src/client/decode.ts'
19
-
20
- const BASE_SETTINGS = {
21
- profile: 'balanced' as const,
22
- custom: structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY),
23
- }
24
-
25
- describe('autoCompact settings validation', () => {
26
- it('defaults legacy settings without an autoCompact section to 80%', () => {
27
- const settings = parseContextCompressionSettings(structuredClone(BASE_SETTINGS))
28
- expect(settings.autoCompact).toEqual({ thresholdPercent: 80 })
29
- })
30
-
31
- it('accepts any integer threshold from 50 through 90, including non-quick values', () => {
32
- for (const thresholdPercent of [50, 69, 70, 73, 80, 82, 85, 90]) {
33
- const settings = parseContextCompressionSettings({
34
- ...structuredClone(BASE_SETTINGS),
35
- autoCompact: { thresholdPercent },
36
- })
37
- expect(settings.autoCompact.thresholdPercent).toBe(thresholdPercent)
38
- }
39
- })
40
-
41
- it.each([
42
- 49, 91, 0, -1, 100, 72.5, 80.5, Number.NaN, Number.POSITIVE_INFINITY,
43
- ])('rejects the out-of-range or fractional value %s', (thresholdPercent) => {
44
- expect(() => parseContextCompressionSettings({
45
- ...structuredClone(BASE_SETTINGS),
46
- autoCompact: { thresholdPercent },
47
- })).toThrow()
48
- expect(isValidAutoCompactThresholdPercent(thresholdPercent)).toBe(false)
49
- })
50
-
51
- it('rejects malformed autoCompact sections and unknown keys', () => {
52
- for (const autoCompact of [
53
- { thresholdPercent: '80' },
54
- { thresholdPercent: null },
55
- {},
56
- { thresholdPercent: 80, extra: true },
57
- null,
58
- ]) {
59
- expect(() => parseContextCompressionSettings({
60
- ...structuredClone(BASE_SETTINGS),
61
- autoCompact,
62
- })).toThrow()
63
- }
64
- expect(() => parseContextCompressionSettings({
65
- ...structuredClone(BASE_SETTINGS),
66
- autoCompact: { thresholdPercent: 80 },
67
- unrelated: true,
68
- })).toThrow(/unknown key/u)
69
- })
70
-
71
- it('publishes one shared validation contract for UI, persistence, and runtime', () => {
72
- expect(AUTO_COMPACT_THRESHOLD_LIMITS).toEqual({
73
- min: 50,
74
- max: 90,
75
- step: 1,
76
- default: 80,
77
- })
78
- expect(isValidAutoCompactThresholdPercent(73)).toBe(true)
79
- expect(isValidAutoCompactThresholdPercent(80)).toBe(true)
80
- expect(isValidAutoCompactThresholdPercent('73')).toBe(false)
81
- expect(isValidAutoCompactThresholdPercent(undefined)).toBe(false)
82
- })
83
-
84
- it('keeps the browser-safe mirror of the contract identical to the runtime contract', () => {
85
- expect(BROWSER_LIMITS).toEqual(AUTO_COMPACT_THRESHOLD_LIMITS)
86
- for (const value of [50, 73, 80, 90, 49, 91, 72.5, '73', null]) {
87
- expect(browserIsValid(value)).toBe(isValidAutoCompactThresholdPercent(value))
88
- }
89
- })
90
-
91
- it('keeps the full settings document browser/runtime parity', () => {
92
- const documents = [
93
- ['legacy without autoCompact', { profile: 'off', custom: structuredClone(BASE_SETTINGS.custom) }],
94
- ['complete document', {
95
- profile: 'off',
96
- custom: structuredClone(BASE_SETTINGS.custom),
97
- autoCompact: { thresholdPercent: 73 },
98
- }],
99
- ['top-level unknown key', {
100
- profile: 'off',
101
- custom: structuredClone(BASE_SETTINGS.custom),
102
- autoCompact: { thresholdPercent: 80 },
103
- unrelated: true,
104
- }],
105
- ['null document', null],
106
- ['array document', [{ profile: 'off' }]],
107
- ['primitive document', 42],
108
- ['empty object', {}],
109
- ['missing profile', { custom: structuredClone(BASE_SETTINGS.custom) }],
110
- ['invalid profile', { profile: 'turbo', custom: structuredClone(BASE_SETTINGS.custom) }],
111
- ['missing custom', { profile: 'off' }],
112
- ['malformed autoCompact', {
113
- profile: 'off',
114
- custom: structuredClone(BASE_SETTINGS.custom),
115
- autoCompact: { thresholdPercent: '80' },
116
- }],
117
- ['null profile', { profile: null, custom: structuredClone(BASE_SETTINGS.custom) }],
118
- ['null custom', { profile: 'off', custom: null }],
119
- ['both sections null', { profile: null, custom: null }],
120
- ['own-property undefined profile', { profile: undefined, custom: structuredClone(BASE_SETTINGS.custom) }],
121
- ['own-property undefined custom', { profile: 'off', custom: undefined }],
122
- ['profile off plus malformed autoCompact', {
123
- profile: 'off',
124
- custom: structuredClone(BASE_SETTINGS.custom),
125
- autoCompact: { thresholdPercent: 400 },
126
- }],
127
- ['codeSkeleton enabled', {
128
- profile: 'off',
129
- custom: structuredClone(BASE_SETTINGS.custom),
130
- codeSkeleton: { enabled: true },
131
- }],
132
- ['codeSkeleton disabled', {
133
- profile: 'off',
134
- custom: structuredClone(BASE_SETTINGS.custom),
135
- codeSkeleton: { enabled: false },
136
- }],
137
- ['malformed codeSkeleton section', {
138
- profile: 'off',
139
- custom: structuredClone(BASE_SETTINGS.custom),
140
- codeSkeleton: { enabled: 'on' },
141
- }],
142
- ['empty codeSkeleton section', {
143
- profile: 'off',
144
- custom: structuredClone(BASE_SETTINGS.custom),
145
- codeSkeleton: {},
146
- }],
147
- ['codeSkeleton with extra key', {
148
- profile: 'off',
149
- custom: structuredClone(BASE_SETTINGS.custom),
150
- codeSkeleton: { enabled: true, extra: true },
151
- }],
152
- ['null codeSkeleton section', {
153
- profile: 'off',
154
- custom: structuredClone(BASE_SETTINGS.custom),
155
- codeSkeleton: null,
156
- }],
157
- ['custom with wrong version', { profile: 'off', custom: { ...structuredClone(BASE_SETTINGS.custom), version: 99 } }],
158
- ] as const
159
- for (const [label, document] of documents) {
160
- const runtimeAccepts = (() => {
161
- try {
162
- parseContextCompressionSettings(structuredClone(document))
163
- return true
164
- } catch {
165
- return false
166
- }
167
- })()
168
- const browserDecoded = browserDecodeSettings(structuredClone(document))
169
- expect(browserDecoded !== undefined, label).toBe(runtimeAccepts)
170
- // When both accept, the decoded values must agree on the COMPLETE
171
- // canonical document — profile, custom, and autoCompact alike.
172
- if (runtimeAccepts && browserDecoded !== undefined) {
173
- const runtimeDecoded = parseContextCompressionSettings(structuredClone(document))
174
- expect(browserDecoded, label).toEqual(runtimeDecoded)
175
- }
176
- }
177
- })
178
-
179
- it('enforces the same plain-record boundary without erasing prototypes', () => {
180
- class SettingsDocument {
181
- profile = 'off' as const
182
- custom = structuredClone(BASE_SETTINGS.custom)
183
- autoCompact = { thresholdPercent: 73 }
184
- }
185
-
186
- class AutoCompactDocument {
187
- thresholdPercent = 73
188
- }
189
-
190
- const customPrototypeDocument = (): Record<string, unknown> => {
191
- const custom = structuredClone(BASE_SETTINGS.custom) as unknown as Record<string, unknown>
192
- Object.setPrototypeOf(custom, { inheritedMarker: true })
193
- return {
194
- profile: 'off',
195
- custom,
196
- autoCompact: { thresholdPercent: 73 },
197
- }
198
- }
199
-
200
- const invalidFactories: ReadonlyArray<readonly [string, () => unknown]> = [
201
- ['Date top-level document', () => new Date(0)],
202
- ['class top-level document', () => new SettingsDocument()],
203
- ['class autoCompact section', () => ({
204
- ...structuredClone(BASE_SETTINGS),
205
- autoCompact: new AutoCompactDocument(),
206
- })],
207
- ['custom-prototype Custom section', customPrototypeDocument],
208
- ]
209
-
210
- for (const [label, createDocument] of invalidFactories) {
211
- expect(() => ContextCompressionSettingsSchema(createDocument() as never), `host: ${label}`).toThrow()
212
- expect(() => parseContextCompressionSettings(createDocument()), `runtime: ${label}`).toThrow()
213
- expect(browserDecodeSettings(createDocument()), `browser: ${label}`).toBeUndefined()
214
- }
215
-
216
- const nullPrototypeDocument = Object.assign(Object.create(null) as Record<string, unknown>, {
217
- profile: 'off',
218
- custom: structuredClone(BASE_SETTINGS.custom),
219
- autoCompact: { thresholdPercent: 73 },
220
- })
221
- const hostDecoded = ContextCompressionSettingsSchema(nullPrototypeDocument as never)
222
- const runtimeDecoded = parseContextCompressionSettings(nullPrototypeDocument)
223
- const browserDecoded = browserDecodeSettings(nullPrototypeDocument)
224
- expect(hostDecoded).toEqual(runtimeDecoded)
225
- expect(browserDecoded).toEqual(runtimeDecoded)
226
- })
227
-
228
- it('canonicalizes legacy Custom v1 documents identically on both ends', () => {
229
- const legacyV1 = {
230
- version: 1,
231
- unit: 'tokens',
232
- fresh: { enabled: true, trigger: 8_192, target: 3_072 },
233
- aggregate: { enabled: true, trigger: 32_768, target: 12_288 },
234
- history: {
235
- enabled: true,
236
- trigger: 500_000,
237
- keepRecentTurns: 4,
238
- keepRecent: 64_000,
239
- minReclaim: 96_000,
240
- },
241
- prefixPolicy: 'pressure-break',
242
- }
243
- const runtimeDecoded = parseContextCompressionSettings({
244
- profile: 'custom',
245
- custom: structuredClone(legacyV1),
246
- })
247
- const browserDecoded = browserDecodeSettings({
248
- profile: 'custom',
249
- custom: structuredClone(legacyV1),
250
- })
251
- expect(browserDecoded).toEqual(runtimeDecoded)
252
- // Canonicalization is part of the shared contract: both ends upgrade the
253
- // legacy document to the same complete v3 policy.
254
- const custom = runtimeDecoded.custom
255
- if (custom.version !== 3) throw new Error('runtime did not canonicalize the legacy document to v3')
256
- expect(custom.version).toBe(3)
257
- expect(custom.history.keepRecentToolCalls).toBe(10)
258
- expect(custom.history.keepRecentTokens).toBe(64_000)
259
- expect(custom.tailTrim).toEqual({ enabled: false, trigger: 700_000 })
260
- })
261
-
262
- it('decodes the browser autoCompact section with exactly the runtime strictness', () => {
263
- for (const [section, expected] of [
264
- [undefined, { thresholdPercent: 80 }],
265
- [{ thresholdPercent: 73 }, { thresholdPercent: 73 }],
266
- [{ thresholdPercent: 50 }, { thresholdPercent: 50 }],
267
- [{ thresholdPercent: 90 }, { thresholdPercent: 90 }],
268
- [null, undefined],
269
- [[80], undefined],
270
- [80, undefined],
271
- ['80', undefined],
272
- [{}, undefined],
273
- [{ thresholdPercent: 80, extra: true }, undefined],
274
- [{ thresholdPercent: null }, undefined],
275
- [{ thresholdPercent: '73' }, undefined],
276
- [{ thresholdPercent: 91 }, undefined],
277
- [{ thresholdPercent: 72.5 }, undefined],
278
- [{ percent: 80 }, undefined],
279
- ] as const) {
280
- expect(browserDecodeAutoCompact(section), JSON.stringify(section)).toEqual(expected)
281
- // The runtime schema agrees on accept/reject for the whole section.
282
- const accepted = (() => {
283
- try {
284
- parseContextCompressionSettings({ ...structuredClone(BASE_SETTINGS), ...(section === undefined ? {} : { autoCompact: section }) })
285
- return true
286
- } catch {
287
- return false
288
- }
289
- })()
290
- expect(accepted, JSON.stringify(section)).toBe(expected !== undefined)
291
- }
292
- })
293
- })
294
-
295
- describe('Auto Compact History linkage formula', () => {
296
- it('restores the exact current preset numbers at the 80% default on a 1M window', () => {
297
- for (const [profile, expected] of [
298
- ['balanced', { h: 500_000, m: 96_000 }],
299
- ['adaptive', { h: 500_000, m: 96_000 }],
300
- ['savings', { h: 400_000, m: 128_000 }],
301
- ['cache-strict', { h: 600_000, m: 128_000 }],
302
- ] as const) {
303
- const policy = resolvePolicy(resolveConfig(), profile, undefined, {
304
- contextWindowTokens: 1_000_000,
305
- autoCompactThresholdPercent: 80,
306
- })
307
- expect(policy.historyTriggerTokens, `${profile} trigger`).toBe(expected.h)
308
- expect(policy.historyMinReclaimTokens, `${profile} min reclaim`).toBe(expected.m)
309
- expect(policy.historyKeepRecentTokens, `${profile} tail`).toBe(64_000)
310
- expect(policy.autoCompactTokens).toBe(800_000)
311
- expect(policy.microDeadlineTokens).toBe(700_000)
312
- }
313
- })
314
-
315
- it('scales H/M/K and the micro deadline for arbitrary thresholds', () => {
316
- const balanced73 = resolvePolicy(resolveConfig(), 'balanced', undefined, {
317
- contextWindowTokens: 1_000_000,
318
- autoCompactThresholdPercent: 73,
319
- })
320
- expect(balanced73.autoCompactTokens).toBe(730_000)
321
- expect(balanced73.microDeadlineTokens).toBe(638_750)
322
- expect(balanced73.historyTriggerTokens).toBe(456_250)
323
- expect(balanced73.historyMinReclaimTokens).toBe(87_600)
324
- expect(balanced73.historyKeepRecentTokens).toBe(58_400)
325
-
326
- const savings70 = resolvePolicy(resolveConfig(), 'savings', undefined, {
327
- contextWindowTokens: 1_000_000,
328
- autoCompactThresholdPercent: 70,
329
- })
330
- expect(savings70.autoCompactTokens).toBe(700_000)
331
- expect(savings70.microDeadlineTokens).toBe(612_500)
332
- expect(savings70.historyTriggerTokens).toBe(350_000)
333
- expect(savings70.historyMinReclaimTokens).toBe(112_000)
334
- expect(savings70.historyKeepRecentTokens).toBe(56_000)
335
-
336
- const cacheStrict85 = resolvePolicy(resolveConfig(), 'cache-strict', undefined, {
337
- contextWindowTokens: 1_000_000,
338
- autoCompactThresholdPercent: 85,
339
- })
340
- expect(cacheStrict85.autoCompactTokens).toBe(850_000)
341
- expect(cacheStrict85.microDeadlineTokens).toBe(743_750)
342
- expect(cacheStrict85.historyTriggerTokens).toBe(637_500)
343
- expect(cacheStrict85.historyMinReclaimTokens).toBe(136_000)
344
- expect(cacheStrict85.historyKeepRecentTokens).toBe(68_000)
345
- })
346
-
347
- it('matches the native engine float evaluation order on rounding-edge pairs', () => {
348
- // compaction-basic computes floor(window * (p / 100)); integer-first
349
- // division differs by one token on exactly these pairs.
350
- for (const [thresholdPercent, contextWindowTokens, expected] of [
351
- [57, 200_000, 113_999],
352
- [58, 200_000, 115_999],
353
- [57, 1_000_000, 570_000],
354
- ] as const) {
355
- const policy = resolvePolicy(resolveConfig(), 'balanced', undefined, {
356
- contextWindowTokens,
357
- autoCompactThresholdPercent: thresholdPercent,
358
- })
359
- expect(policy.autoCompactTokens, `${String(thresholdPercent)}@${String(contextWindowTokens)}`)
360
- .toBe(expected)
361
- expect(policy.autoCompactTokens)
362
- .toBe(Math.floor(contextWindowTokens * (thresholdPercent / 100)))
363
- }
364
- })
365
-
366
- it('keeps every watermark floor-based and exact on odd context windows', () => {
367
- const contextWindow = 999_983
368
- const policy = resolvePolicy(resolveConfig(), 'balanced', undefined, {
369
- contextWindowTokens: contextWindow,
370
- autoCompactThresholdPercent: 73,
371
- })
372
- const autoTokens = Math.floor(contextWindow * (73 / 100))
373
- expect(policy.autoCompactTokens).toBe(autoTokens)
374
- expect(policy.microDeadlineTokens).toBe(Math.floor(autoTokens * 0.875))
375
- expect(policy.historyTriggerTokens).toBe(Math.floor(0.625 * autoTokens))
376
- expect(policy.historyMinReclaimTokens).toBe(Math.floor(0.12 * autoTokens))
377
- expect(policy.historyKeepRecentTokens).toBe(Math.floor(0.08 * autoTokens))
378
- })
379
-
380
- it('covers the 50% and 90% hard boundaries', () => {
381
- const low = resolvePolicy(resolveConfig(), 'balanced', undefined, {
382
- contextWindowTokens: 1_000_000,
383
- autoCompactThresholdPercent: 50,
384
- })
385
- expect(low.autoCompactTokens).toBe(500_000)
386
- expect(low.microDeadlineTokens).toBe(437_500)
387
- const high = resolvePolicy(resolveConfig(), 'balanced', undefined, {
388
- contextWindowTokens: 1_000_000,
389
- autoCompactThresholdPercent: 90,
390
- })
391
- expect(high.autoCompactTokens).toBe(900_000)
392
- expect(high.microDeadlineTokens).toBe(787_500)
393
- })
394
-
395
- it('never links Custom, Off, or Native profiles', () => {
396
- const custom = resolvePolicy(resolveConfig(), 'custom', structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY), {
397
- contextWindowTokens: 1_000_000,
398
- autoCompactThresholdPercent: 73,
399
- })
400
- expect(custom.historyTriggerTokens).toBe(500_000)
401
- expect(custom.autoCompactTokens).toBeUndefined()
402
- expect(custom.microDeadlineTokens).toBeUndefined()
403
- for (const profile of ['off', 'native'] as const) {
404
- const policy = resolvePolicy(resolveConfig(), profile, undefined, {
405
- contextWindowTokens: 1_000_000,
406
- autoCompactThresholdPercent: 73,
407
- })
408
- expect(policy.autoCompactTokens).toBeUndefined()
409
- expect(policy.microDeadlineTokens).toBeUndefined()
410
- expect(policy.historyMode).toBe('disabled')
411
- }
412
- })
413
-
414
- it('falls back to the fixed preset numbers without a resolved context window', () => {
415
- const policy = resolvePolicy(resolveConfig(), 'balanced', undefined, { autoCompactThresholdPercent: 73 })
416
- expect(policy.historyTriggerTokens).toBe(500_000)
417
- expect(policy.historyMinReclaimTokens).toBe(96_000)
418
- expect(policy.historyKeepRecentTokens).toBe(64_000)
419
- expect(policy.autoCompactTokens).toBeUndefined()
420
- expect(policy.microDeadlineTokens).toBeUndefined()
421
- })
422
-
423
- it('rejects an out-of-range deployment-config threshold percent', () => {
424
- for (const autoCompactThresholdPercent of [49, 91, 72.5]) {
425
- expect(() => resolveConfig({ autoCompactThresholdPercent }), String(autoCompactThresholdPercent))
426
- .toThrow(/autoCompactThresholdPercent/u)
427
- }
428
- expect(resolveConfig({ autoCompactThresholdPercent: 73 }).autoCompactThresholdPercent).toBe(73)
429
- })
430
-
431
- it('ignores linkage when the context window is zero, fractional, or negative', () => {
432
- for (const contextWindowTokens of [0, -1_000_000, 999_999.5]) {
433
- const policy = resolvePolicy(resolveConfig(), 'balanced', undefined, {
434
- contextWindowTokens,
435
- autoCompactThresholdPercent: 73,
436
- })
437
- expect(policy.historyTriggerTokens).toBe(500_000)
438
- expect(policy.autoCompactTokens).toBeUndefined()
439
- expect(policy.microDeadlineTokens).toBeUndefined()
440
- }
441
- })
442
-
443
- it('ignores linkage when the threshold is missing or invalid', () => {
444
- for (const autoCompactThresholdPercent of [undefined, 49, 91, 72.5]) {
445
- const policy = resolvePolicy(resolveConfig(), 'balanced', undefined, {
446
- contextWindowTokens: 1_000_000,
447
- ...(autoCompactThresholdPercent === undefined ? {} : { autoCompactThresholdPercent }),
448
- })
449
- expect(policy.historyTriggerTokens).toBe(500_000)
450
- expect(policy.microDeadlineTokens).toBeUndefined()
451
- }
452
- })
453
-
454
- it('keeps explicit deployment overrides ahead of linkage values', () => {
455
- const config = resolveConfig({ historyTriggerTokens: 123_456, historyMinReclaimTokens: 12_345 })
456
- const policy = resolvePolicy(config, 'balanced', undefined, {
457
- contextWindowTokens: 1_000_000,
458
- autoCompactThresholdPercent: 73,
459
- })
460
- expect(policy.historyTriggerTokens).toBe(123_456)
461
- expect(policy.historyMinReclaimTokens).toBe(12_345)
462
- expect(policy.historyKeepRecentTokens).toBe(58_400)
463
- })
464
-
465
- it('keeps the 10-call working set and Fresh/Aggregate budgets independent of the threshold', () => {
466
- for (const autoCompactThresholdPercent of [50, 73, 90]) {
467
- const policy = resolvePolicy(resolveConfig(), 'balanced', undefined, {
468
- contextWindowTokens: 1_000_000,
469
- autoCompactThresholdPercent,
470
- })
471
- expect(policy.historyKeepRecentToolCalls).toBe(10)
472
- expect(policy.freshTriggerTokens).toBe(8_192)
473
- expect(policy.freshTargetTokens).toBe(3_072)
474
- expect(policy.aggregateTriggerTokens).toBe(32_768)
475
- expect(policy.aggregateTargetTokens).toBe(12_288)
476
- }
477
- })
478
- })
479
-
480
- describe('codeSkeleton settings validation', () => {
481
- it('defaults legacy settings without a codeSkeleton section to off', () => {
482
- const settings = parseContextCompressionSettings(structuredClone(BASE_SETTINGS))
483
- expect(settings.codeSkeleton).toEqual({ enabled: false })
484
- })
485
-
486
- it('accepts an explicit boolean enabled flag on either side', () => {
487
- for (const enabled of [true, false]) {
488
- const settings = parseContextCompressionSettings({
489
- ...structuredClone(BASE_SETTINGS),
490
- codeSkeleton: { enabled },
491
- })
492
- expect(settings.codeSkeleton).toEqual({ enabled })
493
- }
494
- })
495
-
496
- it('rejects malformed codeSkeleton sections', () => {
497
- for (const codeSkeleton of [
498
- { enabled: 'true' },
499
- { enabled: 1 },
500
- {},
501
- { enabled: true, extra: true },
502
- null,
503
- 'on',
504
- ]) {
505
- expect(() => parseContextCompressionSettings({
506
- ...structuredClone(BASE_SETTINGS),
507
- codeSkeleton,
508
- })).toThrow()
509
- }
510
- })
511
-
512
- it('mirrors the browser decode for legacy, valid, and malformed documents', () => {
513
- const legacy = structuredClone(BASE_SETTINGS)
514
- const enabled = { ...structuredClone(BASE_SETTINGS), codeSkeleton: { enabled: true } }
515
- const malformed = { ...structuredClone(BASE_SETTINGS), codeSkeleton: { enabled: 'on' } }
516
- expect(browserDecodeSettings(structuredClone(legacy)))
517
- .toEqual(parseContextCompressionSettings(structuredClone(legacy)))
518
- expect(browserDecodeSettings(structuredClone(enabled)))
519
- .toEqual(parseContextCompressionSettings(structuredClone(enabled) as never))
520
- expect(browserDecodeSettings(structuredClone(malformed))).toBeUndefined()
521
- expect(() => parseContextCompressionSettings(structuredClone(malformed))).toThrow()
522
- })
523
- })
1
+ /** Auto Compact threshold settings schema and History linkage formula. */
2
+
3
+ import { describe, expect, it } from 'vitest'
4
+ import {
5
+ AUTO_COMPACT_THRESHOLD_LIMITS,
6
+ ContextCompressionSettingsSchema,
7
+ DEFAULT_CUSTOM_COMPRESSION_POLICY,
8
+ isValidAutoCompactThresholdPercent,
9
+ parseContextCompressionSettings,
10
+ resolveConfig,
11
+ resolvePolicy,
12
+ } from '../../src/pruner.ts'
13
+ import {
14
+ AUTO_COMPACT_THRESHOLD_LIMITS as BROWSER_LIMITS,
15
+ decodeAutoCompactSettings as browserDecodeAutoCompact,
16
+ isValidAutoCompactThresholdPercent as browserIsValid,
17
+ } from '../../src/profiles.ts'
18
+ import { decodeSettings as browserDecodeSettings } from '../../src/client/decode.ts'
19
+
20
+ const BASE_SETTINGS = {
21
+ profile: 'balanced' as const,
22
+ custom: structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY),
23
+ }
24
+
25
+ describe('autoCompact settings validation', () => {
26
+ it('defaults legacy settings without an autoCompact section to 80%', () => {
27
+ const settings = parseContextCompressionSettings(structuredClone(BASE_SETTINGS))
28
+ expect(settings.autoCompact).toEqual({ thresholdPercent: 80 })
29
+ })
30
+
31
+ it('accepts any integer threshold from 50 through 90, including non-quick values', () => {
32
+ for (const thresholdPercent of [50, 69, 70, 73, 80, 82, 85, 90]) {
33
+ const settings = parseContextCompressionSettings({
34
+ ...structuredClone(BASE_SETTINGS),
35
+ autoCompact: { thresholdPercent },
36
+ })
37
+ expect(settings.autoCompact.thresholdPercent).toBe(thresholdPercent)
38
+ }
39
+ })
40
+
41
+ it.each([
42
+ 49, 91, 0, -1, 100, 72.5, 80.5, Number.NaN, Number.POSITIVE_INFINITY,
43
+ ])('rejects the out-of-range or fractional value %s', (thresholdPercent) => {
44
+ expect(() => parseContextCompressionSettings({
45
+ ...structuredClone(BASE_SETTINGS),
46
+ autoCompact: { thresholdPercent },
47
+ })).toThrow()
48
+ expect(isValidAutoCompactThresholdPercent(thresholdPercent)).toBe(false)
49
+ })
50
+
51
+ it('rejects malformed autoCompact sections and unknown keys', () => {
52
+ for (const autoCompact of [
53
+ { thresholdPercent: '80' },
54
+ { thresholdPercent: null },
55
+ {},
56
+ { thresholdPercent: 80, extra: true },
57
+ null,
58
+ ]) {
59
+ expect(() => parseContextCompressionSettings({
60
+ ...structuredClone(BASE_SETTINGS),
61
+ autoCompact,
62
+ })).toThrow()
63
+ }
64
+ expect(() => parseContextCompressionSettings({
65
+ ...structuredClone(BASE_SETTINGS),
66
+ autoCompact: { thresholdPercent: 80 },
67
+ unrelated: true,
68
+ })).toThrow(/unknown key/u)
69
+ })
70
+
71
+ it('publishes one shared validation contract for UI, persistence, and runtime', () => {
72
+ expect(AUTO_COMPACT_THRESHOLD_LIMITS).toEqual({
73
+ min: 50,
74
+ max: 90,
75
+ step: 1,
76
+ default: 80,
77
+ })
78
+ expect(isValidAutoCompactThresholdPercent(73)).toBe(true)
79
+ expect(isValidAutoCompactThresholdPercent(80)).toBe(true)
80
+ expect(isValidAutoCompactThresholdPercent('73')).toBe(false)
81
+ expect(isValidAutoCompactThresholdPercent(undefined)).toBe(false)
82
+ })
83
+
84
+ it('keeps the browser-safe mirror of the contract identical to the runtime contract', () => {
85
+ expect(BROWSER_LIMITS).toEqual(AUTO_COMPACT_THRESHOLD_LIMITS)
86
+ for (const value of [50, 73, 80, 90, 49, 91, 72.5, '73', null]) {
87
+ expect(browserIsValid(value)).toBe(isValidAutoCompactThresholdPercent(value))
88
+ }
89
+ })
90
+
91
+ it('keeps the full settings document browser/runtime parity', () => {
92
+ const documents = [
93
+ ['legacy without autoCompact', { profile: 'off', custom: structuredClone(BASE_SETTINGS.custom) }],
94
+ ['complete document', {
95
+ profile: 'off',
96
+ custom: structuredClone(BASE_SETTINGS.custom),
97
+ autoCompact: { thresholdPercent: 73 },
98
+ }],
99
+ ['top-level unknown key', {
100
+ profile: 'off',
101
+ custom: structuredClone(BASE_SETTINGS.custom),
102
+ autoCompact: { thresholdPercent: 80 },
103
+ unrelated: true,
104
+ }],
105
+ ['null document', null],
106
+ ['array document', [{ profile: 'off' }]],
107
+ ['primitive document', 42],
108
+ ['empty object', {}],
109
+ ['missing profile', { custom: structuredClone(BASE_SETTINGS.custom) }],
110
+ ['invalid profile', { profile: 'turbo', custom: structuredClone(BASE_SETTINGS.custom) }],
111
+ ['missing custom', { profile: 'off' }],
112
+ ['malformed autoCompact', {
113
+ profile: 'off',
114
+ custom: structuredClone(BASE_SETTINGS.custom),
115
+ autoCompact: { thresholdPercent: '80' },
116
+ }],
117
+ ['null profile', { profile: null, custom: structuredClone(BASE_SETTINGS.custom) }],
118
+ ['null custom', { profile: 'off', custom: null }],
119
+ ['both sections null', { profile: null, custom: null }],
120
+ ['own-property undefined profile', { profile: undefined, custom: structuredClone(BASE_SETTINGS.custom) }],
121
+ ['own-property undefined custom', { profile: 'off', custom: undefined }],
122
+ ['profile off plus malformed autoCompact', {
123
+ profile: 'off',
124
+ custom: structuredClone(BASE_SETTINGS.custom),
125
+ autoCompact: { thresholdPercent: 400 },
126
+ }],
127
+ ['codeSkeleton enabled', {
128
+ profile: 'off',
129
+ custom: structuredClone(BASE_SETTINGS.custom),
130
+ codeSkeleton: { enabled: true },
131
+ }],
132
+ ['codeSkeleton disabled', {
133
+ profile: 'off',
134
+ custom: structuredClone(BASE_SETTINGS.custom),
135
+ codeSkeleton: { enabled: false },
136
+ }],
137
+ ['malformed codeSkeleton section', {
138
+ profile: 'off',
139
+ custom: structuredClone(BASE_SETTINGS.custom),
140
+ codeSkeleton: { enabled: 'on' },
141
+ }],
142
+ ['empty codeSkeleton section', {
143
+ profile: 'off',
144
+ custom: structuredClone(BASE_SETTINGS.custom),
145
+ codeSkeleton: {},
146
+ }],
147
+ ['codeSkeleton with extra key', {
148
+ profile: 'off',
149
+ custom: structuredClone(BASE_SETTINGS.custom),
150
+ codeSkeleton: { enabled: true, extra: true },
151
+ }],
152
+ ['null codeSkeleton section', {
153
+ profile: 'off',
154
+ custom: structuredClone(BASE_SETTINGS.custom),
155
+ codeSkeleton: null,
156
+ }],
157
+ ['custom with wrong version', { profile: 'off', custom: { ...structuredClone(BASE_SETTINGS.custom), version: 99 } }],
158
+ ] as const
159
+ for (const [label, document] of documents) {
160
+ const runtimeAccepts = (() => {
161
+ try {
162
+ parseContextCompressionSettings(structuredClone(document))
163
+ return true
164
+ } catch {
165
+ return false
166
+ }
167
+ })()
168
+ const browserDecoded = browserDecodeSettings(structuredClone(document))
169
+ expect(browserDecoded !== undefined, label).toBe(runtimeAccepts)
170
+ // When both accept, the decoded values must agree on the COMPLETE
171
+ // canonical document — profile, custom, and autoCompact alike.
172
+ if (runtimeAccepts && browserDecoded !== undefined) {
173
+ const runtimeDecoded = parseContextCompressionSettings(structuredClone(document))
174
+ expect(browserDecoded, label).toEqual(runtimeDecoded)
175
+ }
176
+ }
177
+ })
178
+
179
+ it('enforces the same plain-record boundary without erasing prototypes', () => {
180
+ class SettingsDocument {
181
+ profile = 'off' as const
182
+ custom = structuredClone(BASE_SETTINGS.custom)
183
+ autoCompact = { thresholdPercent: 73 }
184
+ }
185
+
186
+ class AutoCompactDocument {
187
+ thresholdPercent = 73
188
+ }
189
+
190
+ const customPrototypeDocument = (): Record<string, unknown> => {
191
+ const custom = structuredClone(BASE_SETTINGS.custom) as unknown as Record<string, unknown>
192
+ Object.setPrototypeOf(custom, { inheritedMarker: true })
193
+ return {
194
+ profile: 'off',
195
+ custom,
196
+ autoCompact: { thresholdPercent: 73 },
197
+ }
198
+ }
199
+
200
+ const invalidFactories: ReadonlyArray<readonly [string, () => unknown]> = [
201
+ ['Date top-level document', () => new Date(0)],
202
+ ['class top-level document', () => new SettingsDocument()],
203
+ ['class autoCompact section', () => ({
204
+ ...structuredClone(BASE_SETTINGS),
205
+ autoCompact: new AutoCompactDocument(),
206
+ })],
207
+ ['custom-prototype Custom section', customPrototypeDocument],
208
+ ]
209
+
210
+ for (const [label, createDocument] of invalidFactories) {
211
+ expect(() => ContextCompressionSettingsSchema(createDocument() as never), `host: ${label}`).toThrow()
212
+ expect(() => parseContextCompressionSettings(createDocument()), `runtime: ${label}`).toThrow()
213
+ expect(browserDecodeSettings(createDocument()), `browser: ${label}`).toBeUndefined()
214
+ }
215
+
216
+ const nullPrototypeDocument = Object.assign(Object.create(null) as Record<string, unknown>, {
217
+ profile: 'off',
218
+ custom: structuredClone(BASE_SETTINGS.custom),
219
+ autoCompact: { thresholdPercent: 73 },
220
+ })
221
+ const hostDecoded = ContextCompressionSettingsSchema(nullPrototypeDocument as never)
222
+ const runtimeDecoded = parseContextCompressionSettings(nullPrototypeDocument)
223
+ const browserDecoded = browserDecodeSettings(nullPrototypeDocument)
224
+ expect(hostDecoded).toEqual(runtimeDecoded)
225
+ expect(browserDecoded).toEqual(runtimeDecoded)
226
+ })
227
+
228
+ it('canonicalizes legacy Custom v1 documents identically on both ends', () => {
229
+ const legacyV1 = {
230
+ version: 1,
231
+ unit: 'tokens',
232
+ fresh: { enabled: true, trigger: 8_192, target: 3_072 },
233
+ aggregate: { enabled: true, trigger: 32_768, target: 12_288 },
234
+ history: {
235
+ enabled: true,
236
+ trigger: 500_000,
237
+ keepRecentTurns: 4,
238
+ keepRecent: 64_000,
239
+ minReclaim: 96_000,
240
+ },
241
+ prefixPolicy: 'pressure-break',
242
+ }
243
+ const runtimeDecoded = parseContextCompressionSettings({
244
+ profile: 'custom',
245
+ custom: structuredClone(legacyV1),
246
+ })
247
+ const browserDecoded = browserDecodeSettings({
248
+ profile: 'custom',
249
+ custom: structuredClone(legacyV1),
250
+ })
251
+ expect(browserDecoded).toEqual(runtimeDecoded)
252
+ // Canonicalization is part of the shared contract: both ends upgrade the
253
+ // legacy document to the same complete v3 policy.
254
+ const custom = runtimeDecoded.custom
255
+ if (custom.version !== 3) throw new Error('runtime did not canonicalize the legacy document to v3')
256
+ expect(custom.version).toBe(3)
257
+ expect(custom.history.keepRecentToolCalls).toBe(10)
258
+ expect(custom.history.keepRecentTokens).toBe(64_000)
259
+ expect(custom.tailTrim).toEqual({ enabled: false, trigger: 700_000 })
260
+ })
261
+
262
+ it('decodes the browser autoCompact section with exactly the runtime strictness', () => {
263
+ for (const [section, expected] of [
264
+ [undefined, { thresholdPercent: 80 }],
265
+ [{ thresholdPercent: 73 }, { thresholdPercent: 73 }],
266
+ [{ thresholdPercent: 50 }, { thresholdPercent: 50 }],
267
+ [{ thresholdPercent: 90 }, { thresholdPercent: 90 }],
268
+ [null, undefined],
269
+ [[80], undefined],
270
+ [80, undefined],
271
+ ['80', undefined],
272
+ [{}, undefined],
273
+ [{ thresholdPercent: 80, extra: true }, undefined],
274
+ [{ thresholdPercent: null }, undefined],
275
+ [{ thresholdPercent: '73' }, undefined],
276
+ [{ thresholdPercent: 91 }, undefined],
277
+ [{ thresholdPercent: 72.5 }, undefined],
278
+ [{ percent: 80 }, undefined],
279
+ ] as const) {
280
+ expect(browserDecodeAutoCompact(section), JSON.stringify(section)).toEqual(expected)
281
+ // The runtime schema agrees on accept/reject for the whole section.
282
+ const accepted = (() => {
283
+ try {
284
+ parseContextCompressionSettings({ ...structuredClone(BASE_SETTINGS), ...(section === undefined ? {} : { autoCompact: section }) })
285
+ return true
286
+ } catch {
287
+ return false
288
+ }
289
+ })()
290
+ expect(accepted, JSON.stringify(section)).toBe(expected !== undefined)
291
+ }
292
+ })
293
+ })
294
+
295
+ describe('Auto Compact History linkage formula', () => {
296
+ it('restores the exact current preset numbers at the 80% default on a 1M window', () => {
297
+ for (const [profile, expected] of [
298
+ ['balanced', { h: 500_000, m: 96_000 }],
299
+ ['adaptive', { h: 500_000, m: 96_000 }],
300
+ ['savings', { h: 400_000, m: 128_000 }],
301
+ ['cache-strict', { h: 600_000, m: 128_000 }],
302
+ ] as const) {
303
+ const policy = resolvePolicy(resolveConfig(), profile, undefined, {
304
+ contextWindowTokens: 1_000_000,
305
+ autoCompactThresholdPercent: 80,
306
+ })
307
+ expect(policy.historyTriggerTokens, `${profile} trigger`).toBe(expected.h)
308
+ expect(policy.historyMinReclaimTokens, `${profile} min reclaim`).toBe(expected.m)
309
+ expect(policy.historyKeepRecentTokens, `${profile} tail`).toBe(64_000)
310
+ expect(policy.autoCompactTokens).toBe(800_000)
311
+ expect(policy.microDeadlineTokens).toBe(700_000)
312
+ }
313
+ })
314
+
315
+ it('scales H/M/K and the micro deadline for arbitrary thresholds', () => {
316
+ const balanced73 = resolvePolicy(resolveConfig(), 'balanced', undefined, {
317
+ contextWindowTokens: 1_000_000,
318
+ autoCompactThresholdPercent: 73,
319
+ })
320
+ expect(balanced73.autoCompactTokens).toBe(730_000)
321
+ expect(balanced73.microDeadlineTokens).toBe(638_750)
322
+ expect(balanced73.historyTriggerTokens).toBe(456_250)
323
+ expect(balanced73.historyMinReclaimTokens).toBe(87_600)
324
+ expect(balanced73.historyKeepRecentTokens).toBe(58_400)
325
+
326
+ const savings70 = resolvePolicy(resolveConfig(), 'savings', undefined, {
327
+ contextWindowTokens: 1_000_000,
328
+ autoCompactThresholdPercent: 70,
329
+ })
330
+ expect(savings70.autoCompactTokens).toBe(700_000)
331
+ expect(savings70.microDeadlineTokens).toBe(612_500)
332
+ expect(savings70.historyTriggerTokens).toBe(350_000)
333
+ expect(savings70.historyMinReclaimTokens).toBe(112_000)
334
+ expect(savings70.historyKeepRecentTokens).toBe(56_000)
335
+
336
+ const cacheStrict85 = resolvePolicy(resolveConfig(), 'cache-strict', undefined, {
337
+ contextWindowTokens: 1_000_000,
338
+ autoCompactThresholdPercent: 85,
339
+ })
340
+ expect(cacheStrict85.autoCompactTokens).toBe(850_000)
341
+ expect(cacheStrict85.microDeadlineTokens).toBe(743_750)
342
+ expect(cacheStrict85.historyTriggerTokens).toBe(637_500)
343
+ expect(cacheStrict85.historyMinReclaimTokens).toBe(136_000)
344
+ expect(cacheStrict85.historyKeepRecentTokens).toBe(68_000)
345
+ })
346
+
347
+ it('matches the native engine float evaluation order on rounding-edge pairs', () => {
348
+ // compaction-basic computes floor(window * (p / 100)); integer-first
349
+ // division differs by one token on exactly these pairs.
350
+ for (const [thresholdPercent, contextWindowTokens, expected] of [
351
+ [57, 200_000, 113_999],
352
+ [58, 200_000, 115_999],
353
+ [57, 1_000_000, 570_000],
354
+ ] as const) {
355
+ const policy = resolvePolicy(resolveConfig(), 'balanced', undefined, {
356
+ contextWindowTokens,
357
+ autoCompactThresholdPercent: thresholdPercent,
358
+ })
359
+ expect(policy.autoCompactTokens, `${String(thresholdPercent)}@${String(contextWindowTokens)}`)
360
+ .toBe(expected)
361
+ expect(policy.autoCompactTokens)
362
+ .toBe(Math.floor(contextWindowTokens * (thresholdPercent / 100)))
363
+ }
364
+ })
365
+
366
+ it('keeps every watermark floor-based and exact on odd context windows', () => {
367
+ const contextWindow = 999_983
368
+ const policy = resolvePolicy(resolveConfig(), 'balanced', undefined, {
369
+ contextWindowTokens: contextWindow,
370
+ autoCompactThresholdPercent: 73,
371
+ })
372
+ const autoTokens = Math.floor(contextWindow * (73 / 100))
373
+ expect(policy.autoCompactTokens).toBe(autoTokens)
374
+ expect(policy.microDeadlineTokens).toBe(Math.floor(autoTokens * 0.875))
375
+ expect(policy.historyTriggerTokens).toBe(Math.floor(0.625 * autoTokens))
376
+ expect(policy.historyMinReclaimTokens).toBe(Math.floor(0.12 * autoTokens))
377
+ expect(policy.historyKeepRecentTokens).toBe(Math.floor(0.08 * autoTokens))
378
+ })
379
+
380
+ it('covers the 50% and 90% hard boundaries', () => {
381
+ const low = resolvePolicy(resolveConfig(), 'balanced', undefined, {
382
+ contextWindowTokens: 1_000_000,
383
+ autoCompactThresholdPercent: 50,
384
+ })
385
+ expect(low.autoCompactTokens).toBe(500_000)
386
+ expect(low.microDeadlineTokens).toBe(437_500)
387
+ const high = resolvePolicy(resolveConfig(), 'balanced', undefined, {
388
+ contextWindowTokens: 1_000_000,
389
+ autoCompactThresholdPercent: 90,
390
+ })
391
+ expect(high.autoCompactTokens).toBe(900_000)
392
+ expect(high.microDeadlineTokens).toBe(787_500)
393
+ })
394
+
395
+ it('never links Custom, Off, or Native profiles', () => {
396
+ const custom = resolvePolicy(resolveConfig(), 'custom', structuredClone(DEFAULT_CUSTOM_COMPRESSION_POLICY), {
397
+ contextWindowTokens: 1_000_000,
398
+ autoCompactThresholdPercent: 73,
399
+ })
400
+ expect(custom.historyTriggerTokens).toBe(500_000)
401
+ expect(custom.autoCompactTokens).toBeUndefined()
402
+ expect(custom.microDeadlineTokens).toBeUndefined()
403
+ for (const profile of ['off', 'native'] as const) {
404
+ const policy = resolvePolicy(resolveConfig(), profile, undefined, {
405
+ contextWindowTokens: 1_000_000,
406
+ autoCompactThresholdPercent: 73,
407
+ })
408
+ expect(policy.autoCompactTokens).toBeUndefined()
409
+ expect(policy.microDeadlineTokens).toBeUndefined()
410
+ expect(policy.historyMode).toBe('disabled')
411
+ }
412
+ })
413
+
414
+ it('falls back to the fixed preset numbers without a resolved context window', () => {
415
+ const policy = resolvePolicy(resolveConfig(), 'balanced', undefined, { autoCompactThresholdPercent: 73 })
416
+ expect(policy.historyTriggerTokens).toBe(500_000)
417
+ expect(policy.historyMinReclaimTokens).toBe(96_000)
418
+ expect(policy.historyKeepRecentTokens).toBe(64_000)
419
+ expect(policy.autoCompactTokens).toBeUndefined()
420
+ expect(policy.microDeadlineTokens).toBeUndefined()
421
+ })
422
+
423
+ it('rejects an out-of-range deployment-config threshold percent', () => {
424
+ for (const autoCompactThresholdPercent of [49, 91, 72.5]) {
425
+ expect(() => resolveConfig({ autoCompactThresholdPercent }), String(autoCompactThresholdPercent))
426
+ .toThrow(/autoCompactThresholdPercent/u)
427
+ }
428
+ expect(resolveConfig({ autoCompactThresholdPercent: 73 }).autoCompactThresholdPercent).toBe(73)
429
+ })
430
+
431
+ it('ignores linkage when the context window is zero, fractional, or negative', () => {
432
+ for (const contextWindowTokens of [0, -1_000_000, 999_999.5]) {
433
+ const policy = resolvePolicy(resolveConfig(), 'balanced', undefined, {
434
+ contextWindowTokens,
435
+ autoCompactThresholdPercent: 73,
436
+ })
437
+ expect(policy.historyTriggerTokens).toBe(500_000)
438
+ expect(policy.autoCompactTokens).toBeUndefined()
439
+ expect(policy.microDeadlineTokens).toBeUndefined()
440
+ }
441
+ })
442
+
443
+ it('ignores linkage when the threshold is missing or invalid', () => {
444
+ for (const autoCompactThresholdPercent of [undefined, 49, 91, 72.5]) {
445
+ const policy = resolvePolicy(resolveConfig(), 'balanced', undefined, {
446
+ contextWindowTokens: 1_000_000,
447
+ ...(autoCompactThresholdPercent === undefined ? {} : { autoCompactThresholdPercent }),
448
+ })
449
+ expect(policy.historyTriggerTokens).toBe(500_000)
450
+ expect(policy.microDeadlineTokens).toBeUndefined()
451
+ }
452
+ })
453
+
454
+ it('keeps explicit deployment overrides ahead of linkage values', () => {
455
+ const config = resolveConfig({ historyTriggerTokens: 123_456, historyMinReclaimTokens: 12_345 })
456
+ const policy = resolvePolicy(config, 'balanced', undefined, {
457
+ contextWindowTokens: 1_000_000,
458
+ autoCompactThresholdPercent: 73,
459
+ })
460
+ expect(policy.historyTriggerTokens).toBe(123_456)
461
+ expect(policy.historyMinReclaimTokens).toBe(12_345)
462
+ expect(policy.historyKeepRecentTokens).toBe(58_400)
463
+ })
464
+
465
+ it('keeps the 10-call working set and Fresh/Aggregate budgets independent of the threshold', () => {
466
+ for (const autoCompactThresholdPercent of [50, 73, 90]) {
467
+ const policy = resolvePolicy(resolveConfig(), 'balanced', undefined, {
468
+ contextWindowTokens: 1_000_000,
469
+ autoCompactThresholdPercent,
470
+ })
471
+ expect(policy.historyKeepRecentToolCalls).toBe(10)
472
+ expect(policy.freshTriggerTokens).toBe(8_192)
473
+ expect(policy.freshTargetTokens).toBe(3_072)
474
+ expect(policy.aggregateTriggerTokens).toBe(32_768)
475
+ expect(policy.aggregateTargetTokens).toBe(12_288)
476
+ }
477
+ })
478
+ })
479
+
480
+ describe('codeSkeleton settings validation', () => {
481
+ it('defaults legacy settings without a codeSkeleton section to off', () => {
482
+ const settings = parseContextCompressionSettings(structuredClone(BASE_SETTINGS))
483
+ expect(settings.codeSkeleton).toEqual({ enabled: false })
484
+ })
485
+
486
+ it('accepts an explicit boolean enabled flag on either side', () => {
487
+ for (const enabled of [true, false]) {
488
+ const settings = parseContextCompressionSettings({
489
+ ...structuredClone(BASE_SETTINGS),
490
+ codeSkeleton: { enabled },
491
+ })
492
+ expect(settings.codeSkeleton).toEqual({ enabled })
493
+ }
494
+ })
495
+
496
+ it('rejects malformed codeSkeleton sections', () => {
497
+ for (const codeSkeleton of [
498
+ { enabled: 'true' },
499
+ { enabled: 1 },
500
+ {},
501
+ { enabled: true, extra: true },
502
+ null,
503
+ 'on',
504
+ ]) {
505
+ expect(() => parseContextCompressionSettings({
506
+ ...structuredClone(BASE_SETTINGS),
507
+ codeSkeleton,
508
+ })).toThrow()
509
+ }
510
+ })
511
+
512
+ it('mirrors the browser decode for legacy, valid, and malformed documents', () => {
513
+ const legacy = structuredClone(BASE_SETTINGS)
514
+ const enabled = { ...structuredClone(BASE_SETTINGS), codeSkeleton: { enabled: true } }
515
+ const malformed = { ...structuredClone(BASE_SETTINGS), codeSkeleton: { enabled: 'on' } }
516
+ expect(browserDecodeSettings(structuredClone(legacy)))
517
+ .toEqual(parseContextCompressionSettings(structuredClone(legacy)))
518
+ expect(browserDecodeSettings(structuredClone(enabled)))
519
+ .toEqual(parseContextCompressionSettings(structuredClone(enabled) as never))
520
+ expect(browserDecodeSettings(structuredClone(malformed))).toBeUndefined()
521
+ expect(() => parseContextCompressionSettings(structuredClone(malformed))).toThrow()
522
+ })
523
+ })