dsh-context-compression-improved 0.8.0-beta.3 → 0.8.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/eslint.config.js +30 -30
- package/package.json +163 -163
- package/packages/selector/dsh.plugin.json +16 -16
- package/packages/selector/lib/client.d.ts +6 -0
- package/packages/selector/lib/client.js +106 -7
- package/packages/selector/lib/index.js +92 -1
- package/packages/selector/lib/{advisor-state.js → intent-gate.js} +91 -3
- package/packages/selector/lib/profiles.d.ts +9 -0
- package/packages/selector/lib/pruner.d.ts +59 -0
- package/packages/selector/lib/pruner.js +408 -5
- package/packages/selector/src/client/CompressionProfileControls.tsx +34 -0
- package/packages/selector/src/client/CompressionProfileSelector.tsx +1 -0
- package/packages/selector/src/client/decode.ts +53 -49
- package/packages/selector/src/client/index.ts +4 -0
- package/packages/selector/src/client/locales/de.ts +5 -0
- package/packages/selector/src/client/locales/es.ts +5 -0
- package/packages/selector/src/client/locales/fr.ts +5 -0
- package/packages/selector/src/client/locales/it.ts +5 -0
- package/packages/selector/src/client/locales/ja.ts +5 -0
- package/packages/selector/src/client/locales/ko.ts +5 -0
- package/packages/selector/src/client/locales/ru.ts +5 -0
- package/packages/selector/src/client/locales.ts +10 -0
- package/packages/selector/src/client/settings-section.tsx +9 -2
- package/packages/selector/src/index.ts +7 -0
- package/packages/selector/src/profiles.ts +414 -394
- package/packages/selector/src/pruner/state.ts +57 -50
- package/packages/selector/src/pruner.ts +241 -1
- package/packages/selector/src/runtime/audit.ts +303 -286
- package/packages/selector/src/runtime/config.ts +731 -706
- package/packages/selector/src/runtime/summary-command.ts +127 -0
- package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -188
- package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -149
- package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
- package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
- package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
- package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
- package/packages/selector/src/runtime/types.ts +358 -346
- package/packages/selector/tests/auto-compact.client.spec.tsx +214 -213
- package/packages/selector/tests/code-skeleton.client.spec.ts +94 -93
- package/packages/selector/tests/custom-contract.client.spec.ts +223 -222
- package/packages/selector/tests/estimator-channel.client.spec.tsx +253 -252
- package/packages/selector/tests/preset-options-write.client.spec.ts +188 -187
- package/packages/selector/tests/profiles.client.spec.tsx +436 -434
- package/packages/selector/tests/runtime/audit.spec.ts +275 -274
- package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
- package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
- package/packages/selector/tests/runtime/public/public-runtime.spec.ts +2734 -2731
- package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
- package/packages/selector/tests/settings-seat.client.spec.ts +1 -1
- package/scripts/packed-install-e2e.mjs +1 -1
- package/scripts/verify-release.mjs +2 -1
|
@@ -1,394 +1,414 @@
|
|
|
1
|
-
/** Public context-compression choices shared by the Host schema and browser selector. */
|
|
2
|
-
export const COMPRESSION_PROFILES = [
|
|
3
|
-
'off',
|
|
4
|
-
'native',
|
|
5
|
-
'balanced',
|
|
6
|
-
'cache-strict',
|
|
7
|
-
'savings',
|
|
8
|
-
'adaptive',
|
|
9
|
-
'tokenpilot-inspired',
|
|
10
|
-
'custom',
|
|
11
|
-
] as const
|
|
12
|
-
|
|
13
|
-
/** One supported context-compression profile. */
|
|
14
|
-
export type CompressionProfile = typeof COMPRESSION_PROFILES[number]
|
|
15
|
-
|
|
16
|
-
/** Single canonical unit stored by a version-1 browser Custom document. */
|
|
17
|
-
export type CustomCompressionUnit = 'tokens' | 'context-percent'
|
|
18
|
-
|
|
19
|
-
/** Whether Custom History may routinely rewrite an already-sent prefix. */
|
|
20
|
-
export type CustomPrefixPolicy = 'preserve' | 'pressure-break'
|
|
21
|
-
|
|
22
|
-
/** Browser representation of one independently enabled Fresh or Aggregate budget. */
|
|
23
|
-
export interface CustomCompressionBudget {
|
|
24
|
-
enabled: boolean
|
|
25
|
-
trigger: number
|
|
26
|
-
target: number
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** Legacy browser representation of the Custom History working set. */
|
|
30
|
-
export interface LegacyCustomHistoryPolicy {
|
|
31
|
-
enabled: boolean
|
|
32
|
-
trigger: number
|
|
33
|
-
keepRecentTurns: number
|
|
34
|
-
keepRecent: number
|
|
35
|
-
minReclaim: number
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Browser representation of Custom History tool-call and token-tail protection. */
|
|
39
|
-
export interface CustomHistoryPolicy {
|
|
40
|
-
enabled: boolean
|
|
41
|
-
trigger: number
|
|
42
|
-
keepRecentToolCalls: number
|
|
43
|
-
keepRecentTokens: number
|
|
44
|
-
minReclaim: number
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Browser representation of the Custom-only Experimental TailTrim gate. */
|
|
48
|
-
export interface CustomTailTrimPolicy {
|
|
49
|
-
enabled: boolean
|
|
50
|
-
trigger: number
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
interface CustomCompressionPolicyCommon<HistoryPolicy> {
|
|
54
|
-
unit: CustomCompressionUnit
|
|
55
|
-
fresh: CustomCompressionBudget
|
|
56
|
-
aggregate: CustomCompressionBudget
|
|
57
|
-
history: HistoryPolicy
|
|
58
|
-
prefixPolicy: CustomPrefixPolicy
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** Legacy public Custom document; accepted and normalized before editing. */
|
|
62
|
-
export interface CustomCompressionPolicyV1 extends CustomCompressionPolicyCommon<LegacyCustomHistoryPolicy> {
|
|
63
|
-
version: 1
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** Legacy Custom document with a default-disabled TailTrim stage. */
|
|
67
|
-
export interface CustomCompressionPolicyV2 extends CustomCompressionPolicyCommon<LegacyCustomHistoryPolicy> {
|
|
68
|
-
version: 2
|
|
69
|
-
tailTrim: CustomTailTrimPolicy
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Public Custom document with tool-call working-set protection. */
|
|
73
|
-
export interface CustomCompressionPolicyV3 extends CustomCompressionPolicyCommon<CustomHistoryPolicy> {
|
|
74
|
-
version: 3
|
|
75
|
-
tailTrim: CustomTailTrimPolicy
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Exact public Custom document accepted by the Host and browser boundary. */
|
|
79
|
-
export type CustomCompressionPolicy = CustomCompressionPolicyV1 | CustomCompressionPolicyV2 | CustomCompressionPolicyV3
|
|
80
|
-
|
|
81
|
-
/** Browser-safe mirror of the Host's Balanced-equivalent Custom default. */
|
|
82
|
-
export const DEFAULT_CUSTOM_COMPRESSION_POLICY: CustomCompressionPolicyV3 = {
|
|
83
|
-
version: 3,
|
|
84
|
-
unit: 'tokens',
|
|
85
|
-
fresh: { enabled: true, trigger: 8_192, target: 3_072 },
|
|
86
|
-
aggregate: { enabled: true, trigger: 32_768, target: 12_288 },
|
|
87
|
-
history: {
|
|
88
|
-
enabled: true,
|
|
89
|
-
trigger: 500_000,
|
|
90
|
-
keepRecentToolCalls: 10,
|
|
91
|
-
keepRecentTokens: 64_000,
|
|
92
|
-
minReclaim: 96_000,
|
|
93
|
-
},
|
|
94
|
-
prefixPolicy: 'pressure-break',
|
|
95
|
-
tailTrim: { enabled: false, trigger: 700_000 },
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** User-tunable Auto Compact coordination preferences. */
|
|
99
|
-
export interface AutoCompactSettings {
|
|
100
|
-
thresholdPercent: number
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Orthogonal code-skeleton reducer gate, mirrored browser-safe from the
|
|
105
|
-
* runtime: independent of every profile, default off.
|
|
106
|
-
*/
|
|
107
|
-
export interface CodeSkeletonSettings {
|
|
108
|
-
enabled: boolean
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
if (
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return undefined
|
|
187
|
-
}
|
|
188
|
-
const
|
|
189
|
-
if (
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
return undefined
|
|
199
|
-
}
|
|
200
|
-
const
|
|
201
|
-
if (
|
|
202
|
-
&& (typeof
|
|
203
|
-
||
|
|
204
|
-
return undefined
|
|
205
|
-
}
|
|
206
|
-
const
|
|
207
|
-
if (
|
|
208
|
-
&& (typeof
|
|
209
|
-
||
|
|
210
|
-
return undefined
|
|
211
|
-
}
|
|
212
|
-
const
|
|
213
|
-
if (
|
|
214
|
-
&& (typeof
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if (
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
if (
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
*/
|
|
301
|
-
export
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
||
|
|
330
|
-
|
|
331
|
-
if (value.
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
value.
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
||
|
|
348
|
-
||
|
|
349
|
-
||
|
|
350
|
-
|
|
351
|
-
if (value.
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
return
|
|
394
|
-
|
|
1
|
+
/** Public context-compression choices shared by the Host schema and browser selector. */
|
|
2
|
+
export const COMPRESSION_PROFILES = [
|
|
3
|
+
'off',
|
|
4
|
+
'native',
|
|
5
|
+
'balanced',
|
|
6
|
+
'cache-strict',
|
|
7
|
+
'savings',
|
|
8
|
+
'adaptive',
|
|
9
|
+
'tokenpilot-inspired',
|
|
10
|
+
'custom',
|
|
11
|
+
] as const
|
|
12
|
+
|
|
13
|
+
/** One supported context-compression profile. */
|
|
14
|
+
export type CompressionProfile = typeof COMPRESSION_PROFILES[number]
|
|
15
|
+
|
|
16
|
+
/** Single canonical unit stored by a version-1 browser Custom document. */
|
|
17
|
+
export type CustomCompressionUnit = 'tokens' | 'context-percent'
|
|
18
|
+
|
|
19
|
+
/** Whether Custom History may routinely rewrite an already-sent prefix. */
|
|
20
|
+
export type CustomPrefixPolicy = 'preserve' | 'pressure-break'
|
|
21
|
+
|
|
22
|
+
/** Browser representation of one independently enabled Fresh or Aggregate budget. */
|
|
23
|
+
export interface CustomCompressionBudget {
|
|
24
|
+
enabled: boolean
|
|
25
|
+
trigger: number
|
|
26
|
+
target: number
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Legacy browser representation of the Custom History working set. */
|
|
30
|
+
export interface LegacyCustomHistoryPolicy {
|
|
31
|
+
enabled: boolean
|
|
32
|
+
trigger: number
|
|
33
|
+
keepRecentTurns: number
|
|
34
|
+
keepRecent: number
|
|
35
|
+
minReclaim: number
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Browser representation of Custom History tool-call and token-tail protection. */
|
|
39
|
+
export interface CustomHistoryPolicy {
|
|
40
|
+
enabled: boolean
|
|
41
|
+
trigger: number
|
|
42
|
+
keepRecentToolCalls: number
|
|
43
|
+
keepRecentTokens: number
|
|
44
|
+
minReclaim: number
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Browser representation of the Custom-only Experimental TailTrim gate. */
|
|
48
|
+
export interface CustomTailTrimPolicy {
|
|
49
|
+
enabled: boolean
|
|
50
|
+
trigger: number
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface CustomCompressionPolicyCommon<HistoryPolicy> {
|
|
54
|
+
unit: CustomCompressionUnit
|
|
55
|
+
fresh: CustomCompressionBudget
|
|
56
|
+
aggregate: CustomCompressionBudget
|
|
57
|
+
history: HistoryPolicy
|
|
58
|
+
prefixPolicy: CustomPrefixPolicy
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Legacy public Custom document; accepted and normalized before editing. */
|
|
62
|
+
export interface CustomCompressionPolicyV1 extends CustomCompressionPolicyCommon<LegacyCustomHistoryPolicy> {
|
|
63
|
+
version: 1
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Legacy Custom document with a default-disabled TailTrim stage. */
|
|
67
|
+
export interface CustomCompressionPolicyV2 extends CustomCompressionPolicyCommon<LegacyCustomHistoryPolicy> {
|
|
68
|
+
version: 2
|
|
69
|
+
tailTrim: CustomTailTrimPolicy
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Public Custom document with tool-call working-set protection. */
|
|
73
|
+
export interface CustomCompressionPolicyV3 extends CustomCompressionPolicyCommon<CustomHistoryPolicy> {
|
|
74
|
+
version: 3
|
|
75
|
+
tailTrim: CustomTailTrimPolicy
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Exact public Custom document accepted by the Host and browser boundary. */
|
|
79
|
+
export type CustomCompressionPolicy = CustomCompressionPolicyV1 | CustomCompressionPolicyV2 | CustomCompressionPolicyV3
|
|
80
|
+
|
|
81
|
+
/** Browser-safe mirror of the Host's Balanced-equivalent Custom default. */
|
|
82
|
+
export const DEFAULT_CUSTOM_COMPRESSION_POLICY: CustomCompressionPolicyV3 = {
|
|
83
|
+
version: 3,
|
|
84
|
+
unit: 'tokens',
|
|
85
|
+
fresh: { enabled: true, trigger: 8_192, target: 3_072 },
|
|
86
|
+
aggregate: { enabled: true, trigger: 32_768, target: 12_288 },
|
|
87
|
+
history: {
|
|
88
|
+
enabled: true,
|
|
89
|
+
trigger: 500_000,
|
|
90
|
+
keepRecentToolCalls: 10,
|
|
91
|
+
keepRecentTokens: 64_000,
|
|
92
|
+
minReclaim: 96_000,
|
|
93
|
+
},
|
|
94
|
+
prefixPolicy: 'pressure-break',
|
|
95
|
+
tailTrim: { enabled: false, trigger: 700_000 },
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** User-tunable Auto Compact coordination preferences. */
|
|
99
|
+
export interface AutoCompactSettings {
|
|
100
|
+
thresholdPercent: number
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Orthogonal code-skeleton reducer gate, mirrored browser-safe from the
|
|
105
|
+
* runtime: independent of every profile, default off.
|
|
106
|
+
*/
|
|
107
|
+
export interface CodeSkeletonSettings {
|
|
108
|
+
enabled: boolean
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Orthogonal turn-tail intent-summary gate, mirrored browser-safe from the
|
|
113
|
+
* runtime: independent of every profile, default off.
|
|
114
|
+
*/
|
|
115
|
+
export interface IntentSummarySettings {
|
|
116
|
+
enabled: boolean
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Decode the persisted codeSkeleton section with exactly the runtime schema's
|
|
121
|
+
* strictness: absent means the lossless off default; present values must be a
|
|
122
|
+
* plain object carrying only a boolean `enabled`. Anything else is invalid,
|
|
123
|
+
* never silently coerced.
|
|
124
|
+
*/
|
|
125
|
+
export function decodeCodeSkeletonSettings(value: unknown): CodeSkeletonSettings | undefined {
|
|
126
|
+
if (value === undefined) return { enabled: false }
|
|
127
|
+
if (!isPlainRecord(value)) return undefined
|
|
128
|
+
const keys = Object.keys(value)
|
|
129
|
+
if (keys.length !== 1 || keys[0] !== 'enabled') return undefined
|
|
130
|
+
const enabled = (value as Record<string, unknown>).enabled
|
|
131
|
+
return typeof enabled === 'boolean' ? { enabled } : undefined
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Browser-safe mirror of the runtime intentSummary section (absent inherits off). */
|
|
135
|
+
export function decodeIntentSummarySettings(value: unknown): IntentSummarySettings | undefined {
|
|
136
|
+
if (value === undefined) return { enabled: false }
|
|
137
|
+
if (!isPlainRecord(value)) return undefined
|
|
138
|
+
const keys = Object.keys(value)
|
|
139
|
+
if (keys.length !== 1 || keys[0] !== 'enabled') return undefined
|
|
140
|
+
const enabled = (value as Record<string, unknown>).enabled
|
|
141
|
+
return typeof enabled === 'boolean' ? { enabled } : undefined
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Browser-safe mirror of the runtime presetOptions section. */
|
|
145
|
+
export interface PresetOptionsSettings {
|
|
146
|
+
readonly dedupeToolResults?: boolean
|
|
147
|
+
readonly summaryLocator?: boolean
|
|
148
|
+
readonly prefixStabilizer?: boolean
|
|
149
|
+
readonly readState?: boolean
|
|
150
|
+
readonly estimatorMode?: '' | 'host' | 'direct'
|
|
151
|
+
readonly estimatorProvider?: string
|
|
152
|
+
readonly estimatorModel?: string
|
|
153
|
+
readonly estimatorBaseUrl?: string
|
|
154
|
+
readonly estimatorApiKey?: string
|
|
155
|
+
readonly estimatorTimeoutMs?: number
|
|
156
|
+
/** Advisory advisor channel; `''` (the default) keeps the advisor off. */
|
|
157
|
+
readonly advisorMode?: '' | 'host' | 'direct'
|
|
158
|
+
readonly advisorTimeoutMs?: number
|
|
159
|
+
readonly advisorRefreshTurns?: number
|
|
160
|
+
readonly advisorScoreThreshold?: number
|
|
161
|
+
readonly advisorSampleLimit?: number
|
|
162
|
+
readonly advisorMinTokens?: number
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Browser mirror of the runtime presetOptions section: absent inherits the
|
|
167
|
+
* preset defaults (decodes to `undefined`); present values must be a plain
|
|
168
|
+
* object carrying only the known keys with valid types.
|
|
169
|
+
*/
|
|
170
|
+
export function decodePresetOptionsSettings(value: unknown): PresetOptionsSettings | undefined {
|
|
171
|
+
if (value === undefined) return undefined
|
|
172
|
+
if (!isPlainRecord(value)) return undefined
|
|
173
|
+
const allowed = new Set([
|
|
174
|
+
'dedupeToolResults', 'summaryLocator', 'prefixStabilizer', 'readState', 'estimatorMode',
|
|
175
|
+
'estimatorProvider', 'estimatorModel', 'estimatorBaseUrl', 'estimatorApiKey', 'estimatorTimeoutMs',
|
|
176
|
+
// Legacy keys of the retired review gate: still ACCEPTED so an existing
|
|
177
|
+
// settings document keeps decoding (rejecting them would report the whole
|
|
178
|
+
// card unreadable), and ignored — nothing reads them any more.
|
|
179
|
+
'reviewMode', 'reviewTimeoutTurns', 'cacheHitDiscountAlpha', 'reviewHighImpactTokens',
|
|
180
|
+
'advisorMode', 'advisorTimeoutMs', 'advisorRefreshTurns', 'advisorScoreThreshold', 'advisorSampleLimit',
|
|
181
|
+
'advisorMinTokens',
|
|
182
|
+
])
|
|
183
|
+
if (Object.keys(value).some(key => !allowed.has(key))) return undefined
|
|
184
|
+
for (const key of ['dedupeToolResults', 'summaryLocator', 'prefixStabilizer', 'readState'] as const) {
|
|
185
|
+
const entry = value[key]
|
|
186
|
+
if (entry !== undefined && typeof entry !== 'boolean') return undefined
|
|
187
|
+
}
|
|
188
|
+
const estimatorMode = value.estimatorMode
|
|
189
|
+
if (estimatorMode !== undefined && estimatorMode !== '' && estimatorMode !== 'host' && estimatorMode !== 'direct') {
|
|
190
|
+
return undefined
|
|
191
|
+
}
|
|
192
|
+
const advisorMode = value.advisorMode
|
|
193
|
+
if (advisorMode !== undefined && advisorMode !== '' && advisorMode !== 'host' && advisorMode !== 'direct') {
|
|
194
|
+
return undefined
|
|
195
|
+
}
|
|
196
|
+
for (const key of ['estimatorProvider', 'estimatorModel', 'estimatorBaseUrl', 'estimatorApiKey'] as const) {
|
|
197
|
+
const entry = value[key]
|
|
198
|
+
if (entry !== undefined && typeof entry !== 'string') return undefined
|
|
199
|
+
}
|
|
200
|
+
const estimatorTimeoutMs = value.estimatorTimeoutMs
|
|
201
|
+
if (estimatorTimeoutMs !== undefined
|
|
202
|
+
&& (typeof estimatorTimeoutMs !== 'number' || !Number.isSafeInteger(estimatorTimeoutMs)
|
|
203
|
+
|| estimatorTimeoutMs < 100 || estimatorTimeoutMs > 60_000)) {
|
|
204
|
+
return undefined
|
|
205
|
+
}
|
|
206
|
+
const advisorTimeoutMs = value.advisorTimeoutMs
|
|
207
|
+
if (advisorTimeoutMs !== undefined
|
|
208
|
+
&& (typeof advisorTimeoutMs !== 'number' || !Number.isSafeInteger(advisorTimeoutMs)
|
|
209
|
+
|| advisorTimeoutMs < 100 || advisorTimeoutMs > 60_000)) {
|
|
210
|
+
return undefined
|
|
211
|
+
}
|
|
212
|
+
const advisorRefreshTurns = value.advisorRefreshTurns
|
|
213
|
+
if (advisorRefreshTurns !== undefined
|
|
214
|
+
&& (typeof advisorRefreshTurns !== 'number' || !Number.isSafeInteger(advisorRefreshTurns)
|
|
215
|
+
|| advisorRefreshTurns < 1)) {
|
|
216
|
+
return undefined
|
|
217
|
+
}
|
|
218
|
+
const advisorScoreThreshold = value.advisorScoreThreshold
|
|
219
|
+
if (advisorScoreThreshold !== undefined
|
|
220
|
+
&& (typeof advisorScoreThreshold !== 'number' || !Number.isFinite(advisorScoreThreshold)
|
|
221
|
+
|| advisorScoreThreshold <= 0 || advisorScoreThreshold >= 1)) {
|
|
222
|
+
return undefined
|
|
223
|
+
}
|
|
224
|
+
const advisorSampleLimit = value.advisorSampleLimit
|
|
225
|
+
if (advisorSampleLimit !== undefined
|
|
226
|
+
&& (typeof advisorSampleLimit !== 'number' || !Number.isSafeInteger(advisorSampleLimit)
|
|
227
|
+
|| advisorSampleLimit < 1 || advisorSampleLimit > 64)) {
|
|
228
|
+
return undefined
|
|
229
|
+
}
|
|
230
|
+
const advisorMinTokens = value.advisorMinTokens
|
|
231
|
+
if (advisorMinTokens !== undefined
|
|
232
|
+
&& (typeof advisorMinTokens !== 'number' || !Number.isSafeInteger(advisorMinTokens) || advisorMinTokens < 1)) {
|
|
233
|
+
return undefined
|
|
234
|
+
}
|
|
235
|
+
const decoded: {
|
|
236
|
+
-readonly [K in keyof PresetOptionsSettings]: PresetOptionsSettings[K]
|
|
237
|
+
} = {}
|
|
238
|
+
if (value.dedupeToolResults !== undefined) decoded.dedupeToolResults = value.dedupeToolResults as boolean
|
|
239
|
+
if (value.summaryLocator !== undefined) decoded.summaryLocator = value.summaryLocator as boolean
|
|
240
|
+
if (value.prefixStabilizer !== undefined) decoded.prefixStabilizer = value.prefixStabilizer as boolean
|
|
241
|
+
if (value.readState !== undefined) decoded.readState = value.readState as boolean
|
|
242
|
+
if (estimatorMode !== undefined) decoded.estimatorMode = estimatorMode as '' | 'host' | 'direct'
|
|
243
|
+
if (value.estimatorProvider !== undefined) decoded.estimatorProvider = value.estimatorProvider as string
|
|
244
|
+
if (value.estimatorModel !== undefined) decoded.estimatorModel = value.estimatorModel as string
|
|
245
|
+
if (value.estimatorBaseUrl !== undefined) decoded.estimatorBaseUrl = value.estimatorBaseUrl as string
|
|
246
|
+
if (value.estimatorApiKey !== undefined) decoded.estimatorApiKey = value.estimatorApiKey as string
|
|
247
|
+
if (estimatorTimeoutMs !== undefined) decoded.estimatorTimeoutMs = estimatorTimeoutMs as number
|
|
248
|
+
if (advisorMode !== undefined) decoded.advisorMode = advisorMode as '' | 'host' | 'direct'
|
|
249
|
+
if (advisorTimeoutMs !== undefined) decoded.advisorTimeoutMs = advisorTimeoutMs as number
|
|
250
|
+
if (advisorRefreshTurns !== undefined) decoded.advisorRefreshTurns = advisorRefreshTurns as number
|
|
251
|
+
if (advisorScoreThreshold !== undefined) decoded.advisorScoreThreshold = advisorScoreThreshold as number
|
|
252
|
+
if (advisorSampleLimit !== undefined) decoded.advisorSampleLimit = advisorSampleLimit as number
|
|
253
|
+
if (advisorMinTokens !== undefined) decoded.advisorMinTokens = advisorMinTokens as number
|
|
254
|
+
return decoded
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* The one threshold contract shared by the UI, the persisted settings, and the
|
|
259
|
+
* runtime resolver; mirrored browser-safe from the runtime package.
|
|
260
|
+
*/
|
|
261
|
+
export const AUTO_COMPACT_THRESHOLD_LIMITS = Object.freeze({
|
|
262
|
+
min: 50,
|
|
263
|
+
max: 90,
|
|
264
|
+
step: 1,
|
|
265
|
+
default: 80,
|
|
266
|
+
} as const)
|
|
267
|
+
|
|
268
|
+
/** Narrow one unknown value to a valid Auto Compact threshold percent. */
|
|
269
|
+
export function isValidAutoCompactThresholdPercent(value: unknown): value is number {
|
|
270
|
+
return typeof value === 'number'
|
|
271
|
+
&& Number.isSafeInteger(value)
|
|
272
|
+
&& value >= AUTO_COMPACT_THRESHOLD_LIMITS.min
|
|
273
|
+
&& value <= AUTO_COMPACT_THRESHOLD_LIMITS.max
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Accept JSON-object records while rejecting class instances and exotic prototypes. */
|
|
277
|
+
export function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
|
278
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false
|
|
279
|
+
const prototype = Object.getPrototypeOf(value) as object | null
|
|
280
|
+
return prototype === Object.prototype || prototype === null
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Decode the persisted autoCompact section with exactly the runtime schema's
|
|
285
|
+
* strictness: absent means the 80% default; present values must be a plain
|
|
286
|
+
* object carrying only a valid `thresholdPercent`. Anything else is invalid,
|
|
287
|
+
* never silently coerced.
|
|
288
|
+
*/
|
|
289
|
+
export function decodeAutoCompactSettings(value: unknown): AutoCompactSettings | undefined {
|
|
290
|
+
if (value === undefined) return { thresholdPercent: AUTO_COMPACT_THRESHOLD_LIMITS.default }
|
|
291
|
+
if (!isPlainRecord(value)) return undefined
|
|
292
|
+
const keys = Object.keys(value)
|
|
293
|
+
if (keys.length !== 1 || keys[0] !== 'thresholdPercent') return undefined
|
|
294
|
+
const thresholdPercent = (value as Record<string, unknown>).thresholdPercent
|
|
295
|
+
return isValidAutoCompactThresholdPercent(thresholdPercent)
|
|
296
|
+
? { thresholdPercent }
|
|
297
|
+
: undefined
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Durable settings section owned by this package. */
|
|
301
|
+
export interface ContextCompressionSettings {
|
|
302
|
+
/** Default profile captured when each Session first reaches the pruner. */
|
|
303
|
+
profile: CompressionProfile
|
|
304
|
+
/** Complete canonical policy captured with `profile` when the runtime first observes a Session. */
|
|
305
|
+
custom: CustomCompressionPolicy
|
|
306
|
+
/** Auto Compact trigger captured with `profile` when the runtime first observes a Session. */
|
|
307
|
+
autoCompact: AutoCompactSettings
|
|
308
|
+
/** Code-skeleton reducer gate captured independently of `profile`. */
|
|
309
|
+
codeSkeleton: CodeSkeletonSettings
|
|
310
|
+
/** Turn-tail intent-summary gate captured independently of `profile`. */
|
|
311
|
+
intentSummary: IntentSummarySettings
|
|
312
|
+
/** Optional tokenpilot-inspired sub-capability overrides (presence-validated only). */
|
|
313
|
+
presetOptions?: PresetOptionsSettings
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Narrow an unknown settings value to a complete supported Custom policy.
|
|
318
|
+
* @param value - Candidate settings value received from the Host or edited locally.
|
|
319
|
+
* @returns Whether the value is a relation-valid Custom policy.
|
|
320
|
+
*/
|
|
321
|
+
export function isCustomCompressionPolicy(value: unknown): value is CustomCompressionPolicy {
|
|
322
|
+
if (!hasExactKeys(
|
|
323
|
+
value,
|
|
324
|
+
value !== null && typeof value === 'object' && 'version' in value && value.version === 1
|
|
325
|
+
? ['version', 'unit', 'fresh', 'aggregate', 'history', 'prefixPolicy']
|
|
326
|
+
: ['version', 'unit', 'fresh', 'aggregate', 'history', 'prefixPolicy', 'tailTrim'],
|
|
327
|
+
)) return false
|
|
328
|
+
if ((value.version !== 1 && value.version !== 2 && value.version !== 3)
|
|
329
|
+
|| (value.unit !== 'tokens' && value.unit !== 'context-percent')) return false
|
|
330
|
+
if (value.prefixPolicy !== 'preserve' && value.prefixPolicy !== 'pressure-break') return false
|
|
331
|
+
if (!isBudget(value.fresh) || !isBudget(value.aggregate)) return false
|
|
332
|
+
const modernHistory = value.version === 3
|
|
333
|
+
if (!hasExactKeys(
|
|
334
|
+
value.history,
|
|
335
|
+
modernHistory
|
|
336
|
+
? ['enabled', 'trigger', 'keepRecentToolCalls', 'keepRecentTokens', 'minReclaim']
|
|
337
|
+
: ['enabled', 'trigger', 'keepRecentTurns', 'keepRecent', 'minReclaim'],
|
|
338
|
+
)) return false
|
|
339
|
+
if (typeof value.history.enabled !== 'boolean'
|
|
340
|
+
|| typeof value.history.trigger !== 'number'
|
|
341
|
+
|| typeof value.history.minReclaim !== 'number') return false
|
|
342
|
+
const recent = modernHistory
|
|
343
|
+
? value.history.keepRecentTokens
|
|
344
|
+
: value.history.keepRecent
|
|
345
|
+
const calls = modernHistory ? value.history.keepRecentToolCalls : value.history.keepRecentTurns
|
|
346
|
+
if (typeof recent !== 'number'
|
|
347
|
+
|| typeof calls !== 'number'
|
|
348
|
+
|| !Number.isSafeInteger(calls)
|
|
349
|
+
|| calls < 0) return false
|
|
350
|
+
let tailTrimTrigger: number | undefined
|
|
351
|
+
if (value.version !== 1) {
|
|
352
|
+
const tailTrim = value.tailTrim
|
|
353
|
+
if (!hasExactKeys(tailTrim, ['enabled', 'trigger'])
|
|
354
|
+
|| typeof tailTrim.enabled !== 'boolean'
|
|
355
|
+
|| typeof tailTrim.trigger !== 'number') return false
|
|
356
|
+
tailTrimTrigger = tailTrim.trigger
|
|
357
|
+
}
|
|
358
|
+
const measured = [
|
|
359
|
+
value.fresh.trigger, value.fresh.target,
|
|
360
|
+
value.aggregate.trigger, value.aggregate.target,
|
|
361
|
+
value.history.trigger, recent, value.history.minReclaim,
|
|
362
|
+
...tailTrimTrigger === undefined ? [] : [tailTrimTrigger],
|
|
363
|
+
]
|
|
364
|
+
if (!measured.every(entry => typeof entry === 'number' && Number.isFinite(entry))) return false
|
|
365
|
+
if (value.fresh.trigger <= 0 || value.fresh.target <= 0
|
|
366
|
+
|| value.aggregate.trigger <= 0 || value.aggregate.target <= 0
|
|
367
|
+
|| value.history.trigger <= 0 || recent < 0
|
|
368
|
+
|| value.history.minReclaim <= 0
|
|
369
|
+
|| (tailTrimTrigger !== undefined && tailTrimTrigger <= 0)) return false
|
|
370
|
+
if (value.unit === 'tokens' && !measured.every(Number.isSafeInteger)) return false
|
|
371
|
+
if (value.unit === 'context-percent' && !measured.every(entry => entry <= 100)) return false
|
|
372
|
+
return value.fresh.target < value.fresh.trigger
|
|
373
|
+
&& value.aggregate.target < value.aggregate.trigger
|
|
374
|
+
&& value.history.minReclaim <= value.history.trigger
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function isBudget(value: unknown): value is CustomCompressionBudget {
|
|
378
|
+
return hasExactKeys(value, ['enabled', 'trigger', 'target'])
|
|
379
|
+
&& typeof value.enabled === 'boolean'
|
|
380
|
+
&& typeof value.trigger === 'number'
|
|
381
|
+
&& typeof value.target === 'number'
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Canonicalize one validated Custom document to version 3, mirroring the
|
|
386
|
+
* runtime's `canonicalizeCustomPolicy` exactly so the browser and runtime
|
|
387
|
+
* boundaries hand the SAME complete document to the UI and the policy
|
|
388
|
+
* resolver: legacy v1/v2 History working sets upgrade to the 10-call default,
|
|
389
|
+
* and a v1 document gains the default-disabled TailTrim stage.
|
|
390
|
+
*/
|
|
391
|
+
export function canonicalizeCustomPolicy(policy: CustomCompressionPolicy): CustomCompressionPolicyV3 {
|
|
392
|
+
if (policy.version === 3) return structuredClone(policy)
|
|
393
|
+
return {
|
|
394
|
+
version: 3,
|
|
395
|
+
unit: policy.unit,
|
|
396
|
+
fresh: structuredClone(policy.fresh),
|
|
397
|
+
aggregate: structuredClone(policy.aggregate),
|
|
398
|
+
history: {
|
|
399
|
+
enabled: policy.history.enabled,
|
|
400
|
+
trigger: policy.history.trigger,
|
|
401
|
+
keepRecentToolCalls: 10,
|
|
402
|
+
keepRecentTokens: policy.history.keepRecent,
|
|
403
|
+
minReclaim: policy.history.minReclaim,
|
|
404
|
+
},
|
|
405
|
+
prefixPolicy: policy.prefixPolicy,
|
|
406
|
+
tailTrim: policy.version === 1 ? { enabled: false, trigger: 700_000 } : structuredClone(policy.tailTrim),
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function hasExactKeys(value: unknown, expected: readonly string[]): value is Record<string, unknown> {
|
|
411
|
+
if (!isPlainRecord(value)) return false
|
|
412
|
+
const keys = Object.keys(value)
|
|
413
|
+
return keys.length === expected.length && keys.every(key => expected.includes(key))
|
|
414
|
+
}
|