dsh-context-compression-improved 0.1.1
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/LICENSE +21 -0
- package/README.md +26 -0
- package/README.zh.md +26 -0
- package/THIRD_PARTY_NOTICES.md +38 -0
- package/assets/deepseek-v4/LICENSE.DeepSeek-V4-Pro.txt +21 -0
- package/assets/deepseek-v4/manifest.json +23 -0
- package/assets/deepseek-v4/tokenizer.json +267359 -0
- package/assets/deepseek-v4/tokenizer_config.json +34 -0
- package/assets/deepseek-v4-vision-exp/LICENSE.DeepSeek-V4-Flash-Vision-Exp.txt +21 -0
- package/assets/deepseek-v4-vision-exp/manifest.json +22 -0
- package/assets/deepseek-v4-vision-exp/tokenizer.json +267359 -0
- package/assets/deepseek-v4-vision-exp/tokenizer_config.json +34 -0
- package/assets/screenshots/context-compression-selector-profiles.jpg +0 -0
- package/assets/screenshots/context-compression-selector-settings.png +0 -0
- package/cordis.patch.yml +19 -0
- package/dsh.plugin.json +16 -0
- package/lib/client.d.ts +229 -0
- package/lib/client.js +1462 -0
- package/lib/config.js +842 -0
- package/lib/index.d.ts +38 -0
- package/lib/index.js +645 -0
- package/lib/invariant.d.ts +10 -0
- package/lib/invariant.js +70 -0
- package/lib/pruner.d.ts +684 -0
- package/lib/pruner.js +3807 -0
- package/lib/tail-trim.js +136 -0
- package/package.json +115 -0
- package/screenshots.json +6 -0
package/lib/pruner.d.ts
ADDED
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
import z from "@deepseek-ai/schemastery";
|
|
2
|
+
import { Context, Service } from "@deepseek-ai/cordis";
|
|
3
|
+
import { CallId, ContentBlock } from "@deepseek-ai/dsh-llm";
|
|
4
|
+
import { Session } from "@deepseek-ai/dsh-session";
|
|
5
|
+
import { TokenMeasurement } from "@deepseek-ai/dsh-token-meter";
|
|
6
|
+
//#region src/runtime/tokenpilot/dedup.d.ts
|
|
7
|
+
/** Entry stored per first-seen content hash. */
|
|
8
|
+
interface DedupeTableEntry {
|
|
9
|
+
/** First surface seq carrying this canonical content. */
|
|
10
|
+
readonly seq: number;
|
|
11
|
+
/** Session source reference of the first occurrence. */
|
|
12
|
+
readonly sourceRef: string;
|
|
13
|
+
/** Tool name of the first occurrence. */
|
|
14
|
+
readonly toolName: string;
|
|
15
|
+
/** Original full-text code points of the first occurrence. */
|
|
16
|
+
readonly originalChars: number;
|
|
17
|
+
}
|
|
18
|
+
/** Per-session dedup index with insertion-order eviction. */
|
|
19
|
+
declare class DedupeTable {
|
|
20
|
+
private readonly maxEntries;
|
|
21
|
+
private readonly entries;
|
|
22
|
+
constructor(maxEntries?: number);
|
|
23
|
+
/** Look up the first occurrence for one canonical hash, if any. */
|
|
24
|
+
get(hash: string): DedupeTableEntry | undefined;
|
|
25
|
+
/** Record a first occurrence; existing hashes only refresh insertion order. */
|
|
26
|
+
record(hash: string, entry: DedupeTableEntry): void;
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/runtime/types.d.ts
|
|
30
|
+
/** User-facing mixed strategy profile. */
|
|
31
|
+
declare const COMPRESSION_PROFILES: readonly ["off", "native", "balanced", "cache-strict", "savings", "adaptive", "tokenpilot-inspired", "custom"];
|
|
32
|
+
/** Public compression strategy selected for one Session. */
|
|
33
|
+
type CompressionProfile = typeof COMPRESSION_PROFILES[number];
|
|
34
|
+
/** When historical tool results may be aged for one Session policy. */
|
|
35
|
+
type HistoryMode = 'disabled' | 'routine' | 'capacity-pressure' | 'adaptive';
|
|
36
|
+
/** Canonical unit stored by one versioned Custom policy. */
|
|
37
|
+
type CustomCompressionUnit = 'tokens' | 'context-percent';
|
|
38
|
+
/** Whether routine History may rewrite a previously sent Harness prefix. */
|
|
39
|
+
type CustomPrefixPolicy = 'preserve' | 'pressure-break';
|
|
40
|
+
/** One independently selectable Custom Fresh or Aggregate stage. */
|
|
41
|
+
interface CustomCompressionBudget {
|
|
42
|
+
enabled: boolean;
|
|
43
|
+
trigger: number;
|
|
44
|
+
target: number;
|
|
45
|
+
}
|
|
46
|
+
/** Legacy Custom History gate and turn/token working-set protection. */
|
|
47
|
+
interface LegacyCustomHistoryPolicy {
|
|
48
|
+
enabled: boolean;
|
|
49
|
+
trigger: number;
|
|
50
|
+
keepRecentTurns: number;
|
|
51
|
+
keepRecent: number;
|
|
52
|
+
minReclaim: number;
|
|
53
|
+
}
|
|
54
|
+
/** Custom History gate and recent tool-call/token working-set protection. */
|
|
55
|
+
interface CustomHistoryPolicy {
|
|
56
|
+
enabled: boolean;
|
|
57
|
+
trigger: number;
|
|
58
|
+
keepRecentToolCalls: number;
|
|
59
|
+
keepRecentTokens: number;
|
|
60
|
+
minReclaim: number;
|
|
61
|
+
}
|
|
62
|
+
/** Custom-only experimental TailTrim gate. */
|
|
63
|
+
interface CustomTailTrimPolicy {
|
|
64
|
+
enabled: boolean;
|
|
65
|
+
trigger: number;
|
|
66
|
+
}
|
|
67
|
+
/** TokenPilot-inspired preset sub-capability switches (only injected for `tokenpilot-inspired`). */
|
|
68
|
+
interface PresetOptions {
|
|
69
|
+
/** Reject replacements whose text is not smaller than the original (G1). */
|
|
70
|
+
readonly noNetSavingsGuard: boolean;
|
|
71
|
+
/** Permanently exempt recovery content from further reduction (G2). */
|
|
72
|
+
readonly skipReductionRecovery: boolean;
|
|
73
|
+
/** Replace byte-identical repeated tool results with first-occurrence pointers (A1). */
|
|
74
|
+
readonly dedupeToolResults: boolean;
|
|
75
|
+
/** Append Exact Sources locator blocks after Auto Compact completes (A2). */
|
|
76
|
+
readonly summaryLocator: boolean;
|
|
77
|
+
/** Volatile-line demotion, deterministic tool ordering, and prefix fingerprint audit (S1/S2). */
|
|
78
|
+
readonly prefixStabilizer: boolean;
|
|
79
|
+
/** Fresh/superseded read-state classification with clustered omission markers (R2/R3). */
|
|
80
|
+
readonly readState: boolean;
|
|
81
|
+
/** Optional estimator channel; `''` keeps every estimator consumer on rule-only fallbacks (E1/E2). */
|
|
82
|
+
readonly estimator: {
|
|
83
|
+
readonly mode: '' | 'host' | 'direct';
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** Common user-authored Custom stages shared by persisted policy versions. */
|
|
87
|
+
interface CustomCompressionPolicyFields<HistoryPolicy> {
|
|
88
|
+
unit: CustomCompressionUnit;
|
|
89
|
+
fresh: CustomCompressionBudget;
|
|
90
|
+
aggregate: CustomCompressionBudget;
|
|
91
|
+
history: HistoryPolicy;
|
|
92
|
+
prefixPolicy: CustomPrefixPolicy;
|
|
93
|
+
}
|
|
94
|
+
/** Legacy R4 Custom policy, accepted without migration. */
|
|
95
|
+
interface CustomCompressionPolicyV1 extends CustomCompressionPolicyFields<LegacyCustomHistoryPolicy> {
|
|
96
|
+
version: 1;
|
|
97
|
+
}
|
|
98
|
+
/** R5 Custom policy with an explicit default-off TailTrim stage. */
|
|
99
|
+
interface CustomCompressionPolicyV2 extends CustomCompressionPolicyFields<LegacyCustomHistoryPolicy> {
|
|
100
|
+
version: 2;
|
|
101
|
+
tailTrim: CustomTailTrimPolicy;
|
|
102
|
+
}
|
|
103
|
+
/** Custom policy with tool-call working-set protection. */
|
|
104
|
+
interface CustomCompressionPolicyV3 extends CustomCompressionPolicyFields<CustomHistoryPolicy> {
|
|
105
|
+
version: 3;
|
|
106
|
+
tailTrim: CustomTailTrimPolicy;
|
|
107
|
+
}
|
|
108
|
+
/** Strict persisted Custom policy union. */
|
|
109
|
+
type CustomCompressionPolicy = CustomCompressionPolicyV1 | CustomCompressionPolicyV2 | CustomCompressionPolicyV3;
|
|
110
|
+
/** User-tunable Auto Compact coordination preferences. */
|
|
111
|
+
interface AutoCompactSettings {
|
|
112
|
+
/** Routed-context percentage that triggers model-driven Auto Compact. */
|
|
113
|
+
thresholdPercent: number;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Orthogonal evidence-based code-skeleton reducer gate. Independent of every
|
|
117
|
+
* profile: when enabled, fresh oversized source-code results may take the
|
|
118
|
+
* `hypa-code-skeleton` reducer before the head/tail fallbacks.
|
|
119
|
+
*/
|
|
120
|
+
interface CodeSkeletonSettings {
|
|
121
|
+
enabled: boolean;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Persisted sub-capability overrides for the `tokenpilot-inspired` preset.
|
|
125
|
+
* Absent fields inherit the preset defaults; the section is only meaningful
|
|
126
|
+
* while the resolved profile is `tokenpilot-inspired`.
|
|
127
|
+
*/
|
|
128
|
+
interface PresetOptionsSettings {
|
|
129
|
+
readonly dedupeToolResults?: boolean;
|
|
130
|
+
readonly summaryLocator?: boolean;
|
|
131
|
+
readonly prefixStabilizer?: boolean;
|
|
132
|
+
readonly readState?: boolean;
|
|
133
|
+
readonly estimatorMode?: '' | 'host' | 'direct';
|
|
134
|
+
/**
|
|
135
|
+
* Estimator endpoint fields. Persisted-settings only: they never enter the
|
|
136
|
+
* frozen CompressionPolicy, which is emitted verbatim by policy-resolved
|
|
137
|
+
* audits, so the API key cannot leak into logs.
|
|
138
|
+
*/
|
|
139
|
+
readonly estimatorProvider?: string;
|
|
140
|
+
readonly estimatorModel?: string;
|
|
141
|
+
readonly estimatorBaseUrl?: string;
|
|
142
|
+
readonly estimatorApiKey?: string;
|
|
143
|
+
readonly estimatorTimeoutMs?: number;
|
|
144
|
+
}
|
|
145
|
+
/** Durable global preference exposed through `ctx.settings`. */
|
|
146
|
+
interface ContextCompressionSettings {
|
|
147
|
+
/** Default strategy snapped when a Session first reaches the pruner. */
|
|
148
|
+
profile: CompressionProfile;
|
|
149
|
+
/** Versioned Custom policy snapped with `profile` for a newly observed Session. */
|
|
150
|
+
custom: CustomCompressionPolicy;
|
|
151
|
+
/** Auto Compact trigger preference snapped with `profile` for a newly observed Session. */
|
|
152
|
+
autoCompact: AutoCompactSettings;
|
|
153
|
+
/** Code-skeleton reducer gate snapped independently of `profile`. */
|
|
154
|
+
codeSkeleton: CodeSkeletonSettings;
|
|
155
|
+
/** Optional tokenpilot-inspired sub-capability overrides; absent inherits preset defaults. */
|
|
156
|
+
presetOptions?: PresetOptionsSettings;
|
|
157
|
+
}
|
|
158
|
+
/** Token-gated policy with character fields limited to reducer candidate shape. */
|
|
159
|
+
interface ToolResultPruneConfig {
|
|
160
|
+
/** Composition fallback when Host settings are unavailable. Defaults to `balanced`. */
|
|
161
|
+
profile?: CompressionProfile;
|
|
162
|
+
/** Native fallback leading Unicode code points. Defaults to `4096`. */
|
|
163
|
+
headChars?: number;
|
|
164
|
+
/** Native fallback trailing Unicode code points. Defaults to `1024`. */
|
|
165
|
+
tailChars?: number;
|
|
166
|
+
/** Native original-content token trigger. Profile default when omitted. */
|
|
167
|
+
nativeTriggerTokens?: number;
|
|
168
|
+
/** Native replacement token target. Profile default when omitted. */
|
|
169
|
+
nativeTargetTokens?: number;
|
|
170
|
+
/** Fresh-result exact-token trigger. Profile default when omitted. */
|
|
171
|
+
freshTriggerTokens?: number;
|
|
172
|
+
/** Maximum fresh-result exact-token replacement size. Profile default when omitted. */
|
|
173
|
+
freshTargetTokens?: number;
|
|
174
|
+
/** Combined completed-step token pressure that starts aggregate reduction. */
|
|
175
|
+
aggregateTriggerTokens?: number;
|
|
176
|
+
/** Aggregate token target after completed-step pressure exceeds its trigger. */
|
|
177
|
+
aggregateTargetTokens?: number;
|
|
178
|
+
/** Total live tool-result tokens that permit historical aging. Profile default when omitted. */
|
|
179
|
+
historyTriggerTokens?: number;
|
|
180
|
+
/** Recent completed agent tool calls protected from historical aging. Profile default when omitted. */
|
|
181
|
+
historyKeepRecentToolCalls?: number;
|
|
182
|
+
/** Recent tool-result token tail protected in addition to tool calls. Profile default when omitted. */
|
|
183
|
+
historyKeepRecentTokens?: number;
|
|
184
|
+
/** Minimum reclaim required before historical aging is worth a cache break. Profile default when omitted. */
|
|
185
|
+
historyMinReclaimTokens?: number;
|
|
186
|
+
/**
|
|
187
|
+
* Auto Compact threshold percent frozen into this deployment by the preset
|
|
188
|
+
* overlay generation (50–90 integer). When present it supersedes the live
|
|
189
|
+
* Host setting so one generation never splits Auto Compact and micro
|
|
190
|
+
* compact across two thresholds.
|
|
191
|
+
*/
|
|
192
|
+
autoCompactThresholdPercent?: number;
|
|
193
|
+
/** Optional tokenpilot-inspired sub-capability overrides (deploy-level). */
|
|
194
|
+
presetOptions?: PresetOptionsSettings;
|
|
195
|
+
}
|
|
196
|
+
/** Resolved per-profile behavior. */
|
|
197
|
+
interface CompressionPolicy {
|
|
198
|
+
readonly profile: CompressionProfile;
|
|
199
|
+
/** Whether this Session may use the selector's native-style head/middle/tail reducer. */
|
|
200
|
+
readonly nativeToolResultEnabled: boolean;
|
|
201
|
+
readonly freshEnabled: boolean;
|
|
202
|
+
readonly aggregateEnabled: boolean;
|
|
203
|
+
readonly historyMode: HistoryMode;
|
|
204
|
+
readonly nativeTriggerTokens: number;
|
|
205
|
+
readonly nativeTargetTokens: number;
|
|
206
|
+
readonly freshTriggerTokens: number;
|
|
207
|
+
readonly freshTargetTokens: number;
|
|
208
|
+
readonly aggregateTriggerTokens: number;
|
|
209
|
+
readonly aggregateTargetTokens: number;
|
|
210
|
+
readonly historyTriggerTokens: number;
|
|
211
|
+
readonly historyKeepRecentToolCalls: number;
|
|
212
|
+
readonly historyKeepRecentTokens: number;
|
|
213
|
+
readonly historyMinReclaimTokens: number;
|
|
214
|
+
/**
|
|
215
|
+
* Auto Compact token watermark `A = floor(C × a)` when the standard-profile
|
|
216
|
+
* History linkage resolved for this Session; absent for Custom, Off, Native,
|
|
217
|
+
* or unresolved routed capacity.
|
|
218
|
+
*/
|
|
219
|
+
readonly autoCompactTokens?: number;
|
|
220
|
+
/**
|
|
221
|
+
* Micro-compact last-chance watermark `D = floor(A × 0.875)`. Absent when
|
|
222
|
+
* {@link autoCompactTokens} is absent; capacity-pressure gates fall back to
|
|
223
|
+
* the fixed 0.7 routed-context ratio in that case.
|
|
224
|
+
*/
|
|
225
|
+
readonly microDeadlineTokens?: number;
|
|
226
|
+
/** Present for Custom v3; standard profiles and legacy Custom policies carry no TailTrim policy. */
|
|
227
|
+
readonly tailTrim?: {
|
|
228
|
+
readonly enabled: boolean;
|
|
229
|
+
readonly triggerTokens: number;
|
|
230
|
+
};
|
|
231
|
+
/**
|
|
232
|
+
* TokenPilot-inspired sub-capability switches. Present only for the
|
|
233
|
+
* `tokenpilot-inspired` preset; every other profile stays byte-identical.
|
|
234
|
+
*/
|
|
235
|
+
readonly presetOptions?: PresetOptions;
|
|
236
|
+
}
|
|
237
|
+
/** Validated, detached, deeply immutable configuration. */
|
|
238
|
+
interface ResolvedConfig {
|
|
239
|
+
readonly profile: CompressionProfile;
|
|
240
|
+
readonly headChars: number;
|
|
241
|
+
readonly tailChars: number;
|
|
242
|
+
readonly nativeTriggerTokens?: number;
|
|
243
|
+
readonly nativeTargetTokens?: number;
|
|
244
|
+
readonly freshTriggerTokens?: number;
|
|
245
|
+
readonly freshTargetTokens?: number;
|
|
246
|
+
readonly aggregateTriggerTokens?: number;
|
|
247
|
+
readonly aggregateTargetTokens?: number;
|
|
248
|
+
readonly historyTriggerTokens?: number;
|
|
249
|
+
readonly historyKeepRecentToolCalls?: number;
|
|
250
|
+
readonly historyKeepRecentTokens?: number;
|
|
251
|
+
readonly historyMinReclaimTokens?: number;
|
|
252
|
+
/**
|
|
253
|
+
* Auto Compact threshold percent frozen into this deployment by the preset
|
|
254
|
+
* overlay generation (50-90 integer). Supersedes the live Host setting.
|
|
255
|
+
*/
|
|
256
|
+
readonly autoCompactThresholdPercent?: number;
|
|
257
|
+
/** Optional tokenpilot-inspired sub-capability overrides resolved from the deployment config. */
|
|
258
|
+
readonly presetOptions?: PresetOptionsSettings;
|
|
259
|
+
}
|
|
260
|
+
/** Why a pruning pass runs. */
|
|
261
|
+
type PruneStage = 'fresh' | 'pressure';
|
|
262
|
+
/** Optional control over one pruning pass. */
|
|
263
|
+
interface PruneSessionOptions {
|
|
264
|
+
/** `fresh` only reduces never-before-seen oversized results; `pressure` may age older results too. */
|
|
265
|
+
stage?: PruneStage;
|
|
266
|
+
/** Routed model context capacity used only to resolve a context-percent Custom snapshot. */
|
|
267
|
+
contextWindowTokens?: number;
|
|
268
|
+
/** Proposed turn at the pre-step boundary. Used with `freshStep` to freeze keep/reduce decisions. */
|
|
269
|
+
freshTurn?: number;
|
|
270
|
+
/** The immediately preceding completed step whose tool results have not yet entered a model request. */
|
|
271
|
+
freshStep?: number;
|
|
272
|
+
}
|
|
273
|
+
/** Cited source event and size accounting for one landed surface replacement. */
|
|
274
|
+
interface PrunedEntry {
|
|
275
|
+
/** Current surface event shadowed by this replacement. */
|
|
276
|
+
readonly originalSeq: number;
|
|
277
|
+
/** Root full-fidelity source event used in the recovery reference. */
|
|
278
|
+
readonly sourceSeq: number;
|
|
279
|
+
/** Newly appended compressed tool-result event. */
|
|
280
|
+
readonly replacementSeq: number;
|
|
281
|
+
/** Tool call shared by the original and replacement. */
|
|
282
|
+
readonly callId: CallId;
|
|
283
|
+
/** Reducer or aging strategy that produced the replacement. */
|
|
284
|
+
readonly reducer: string;
|
|
285
|
+
/** Pass stage that landed the replacement. */
|
|
286
|
+
readonly stage: PruneStage;
|
|
287
|
+
/** Original deterministic pressure cost. */
|
|
288
|
+
readonly charsBefore: number;
|
|
289
|
+
/** Replacement deterministic pressure cost. */
|
|
290
|
+
readonly charsAfter: number;
|
|
291
|
+
/** Authoritative exact canonical content tokens before replacement. */
|
|
292
|
+
readonly tokensBefore: number;
|
|
293
|
+
/** Authoritative exact canonical content tokens after replacement. */
|
|
294
|
+
readonly tokensAfter: number;
|
|
295
|
+
}
|
|
296
|
+
/** Aggregate outcome of one stable-surface pruning pass. */
|
|
297
|
+
interface PruneResult {
|
|
298
|
+
/** Replacements in landing order. */
|
|
299
|
+
readonly pruned: readonly PrunedEntry[];
|
|
300
|
+
/** Total deterministic pressure cost removed across replacements. */
|
|
301
|
+
readonly charsRemoved: number;
|
|
302
|
+
/** Authoritative exact canonical content tokens removed. */
|
|
303
|
+
readonly tokensRemoved: number;
|
|
304
|
+
}
|
|
305
|
+
//#endregion
|
|
306
|
+
//#region src/runtime/tokenpilot/estimator.d.ts
|
|
307
|
+
/** Per-session estimator failure bookkeeping for exponential backoff. */
|
|
308
|
+
interface EstimatorFailures {
|
|
309
|
+
failures: number;
|
|
310
|
+
cooldownUntil: number;
|
|
311
|
+
}
|
|
312
|
+
//#endregion
|
|
313
|
+
//#region src/pruner/state.d.ts
|
|
314
|
+
/** Mutable per-session state bag used inside {@link ToolResultPruner}. */
|
|
315
|
+
interface PrunerState {
|
|
316
|
+
/** Resolved immutable deployment configuration. */
|
|
317
|
+
readonly config: ResolvedConfig;
|
|
318
|
+
/** Complete canonical setting document frozen when each Session first reaches this root service. */
|
|
319
|
+
readonly sessionSettings: WeakMap<Session, ContextCompressionSettings>;
|
|
320
|
+
/** Original result seqs whose first-exposure KEEP/REDUCE decision has committed. */
|
|
321
|
+
readonly firstExposure: WeakMap<Session, Set<number>>;
|
|
322
|
+
/** Result seqs permanently exempt from further reduction (recovery outputs and registered equivalents). */
|
|
323
|
+
readonly recoveryExemptions: WeakMap<Session, Set<number>>;
|
|
324
|
+
/** Per-session canonical-content hash index backing tokenpilot-inspired dedupe. */
|
|
325
|
+
readonly dedupeTables: WeakMap<Session, DedupeTable>;
|
|
326
|
+
/** Advisory estimator verdicts consumed by the read-state classification. */
|
|
327
|
+
readonly estimatorVerdicts: WeakMap<Session, Map<number, boolean>>;
|
|
328
|
+
/** Per-session estimator failure backoff state. */
|
|
329
|
+
readonly estimatorFailures: WeakMap<Session, EstimatorFailures>;
|
|
330
|
+
/** Runtime prerequisite warnings deduplicated per Session and failure key. */
|
|
331
|
+
readonly warnedFailures: WeakMap<Session, Set<string>>;
|
|
332
|
+
/** Last Adaptive postflight attempt emitted per Session; keeps diagnostics bounded and independent. */
|
|
333
|
+
readonly postflightDiagnostics: WeakMap<Session, string>;
|
|
334
|
+
/** Current pre-step chain identity, shared by this producer and downstream compaction-basic. */
|
|
335
|
+
readonly activeRequestBoundaries: WeakMap<Session, object>;
|
|
336
|
+
/** Boundary identity that already attempted one fully preflighted TailTrim publication. */
|
|
337
|
+
readonly tailTrimBoundaryAttempts: WeakMap<Session, object>;
|
|
338
|
+
/** Last effective policy audit key emitted for each Session. */
|
|
339
|
+
readonly policyResolutionAudits: WeakMap<Session, string>;
|
|
340
|
+
}
|
|
341
|
+
//#endregion
|
|
342
|
+
//#region src/runtime/custom-policy.d.ts
|
|
343
|
+
/** Canonical Custom document accepted by Host settings and the runtime resolver. */
|
|
344
|
+
declare const CustomCompressionPolicySchema: z<CustomCompressionPolicy>;
|
|
345
|
+
/** Balanced-equivalent Custom policy stored as one token-canonical document. */
|
|
346
|
+
declare const DEFAULT_CUSTOM_COMPRESSION_POLICY: CustomCompressionPolicyV3;
|
|
347
|
+
/** Routed model facts needed only by context-percent Custom documents. */
|
|
348
|
+
interface CustomPolicyResolutionOptions {
|
|
349
|
+
/** Positive resolved shared model context capacity. */
|
|
350
|
+
readonly contextWindowTokens?: number;
|
|
351
|
+
/**
|
|
352
|
+
* Frozen Auto Compact threshold percent. Standard profiles use it to link
|
|
353
|
+
* History watermarks to the Auto Compact level; Custom resolution ignores it
|
|
354
|
+
* because Custom stays explicit-token manual.
|
|
355
|
+
*/
|
|
356
|
+
readonly autoCompactThresholdPercent?: number;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Resolve one validated Custom document to the same token policy used by public presets.
|
|
360
|
+
* @param value - untrusted or typed Custom settings value.
|
|
361
|
+
* @param options - routed model capacity for context-percent documents.
|
|
362
|
+
* @returns a detached deeply immutable effective token policy.
|
|
363
|
+
*/
|
|
364
|
+
declare function resolveCustomPolicy(value: CustomCompressionPolicy, options?: CustomPolicyResolutionOptions): CompressionPolicy;
|
|
365
|
+
//#endregion
|
|
366
|
+
//#region src/runtime/config.d.ts
|
|
367
|
+
/** Settings namespace shared by the Host service and browser selector. */
|
|
368
|
+
declare const CONTEXT_COMPRESSION_SETTINGS_NAMESPACE = "context-compression";
|
|
369
|
+
/** Fixed native fallback marker. */
|
|
370
|
+
declare const PRUNE_MARKER = "\n\n[... tool result middle pruned ...]\n\n";
|
|
371
|
+
/**
|
|
372
|
+
* The one Auto Compact threshold contract shared by the settings UI, the
|
|
373
|
+
* persisted settings schema, and the runtime resolver. Every integer in the
|
|
374
|
+
* range is valid and entered directly in the UI.
|
|
375
|
+
*/
|
|
376
|
+
declare const AUTO_COMPACT_THRESHOLD_LIMITS: {
|
|
377
|
+
readonly min: 50;
|
|
378
|
+
readonly max: 90;
|
|
379
|
+
readonly step: 1;
|
|
380
|
+
readonly default: 80;
|
|
381
|
+
};
|
|
382
|
+
/** Narrow one untrusted value to a valid Auto Compact threshold percent. */
|
|
383
|
+
declare function isValidAutoCompactThresholdPercent(value: unknown): value is number;
|
|
384
|
+
/**
|
|
385
|
+
* Parse one settings document with the persisted-section semantics: `undefined`
|
|
386
|
+
* inherits the defaults (an absent section), while `null` is an explicitly
|
|
387
|
+
* invalid document and must never silently become the default policy.
|
|
388
|
+
*/
|
|
389
|
+
declare function parseContextCompressionSettings(value: unknown): ContextCompressionSettings;
|
|
390
|
+
/** Settings schema used by the user-facing profile selector. */
|
|
391
|
+
declare const ContextCompressionSettingsSchema: z<ContextCompressionSettings>;
|
|
392
|
+
/** Low-friction defaults; token budgets live in resolved profile policy. */
|
|
393
|
+
declare const DEFAULTS: ResolvedConfig;
|
|
394
|
+
/**
|
|
395
|
+
* Count Unicode code points without splitting surrogate pairs.
|
|
396
|
+
* @param text - text whose code points are counted.
|
|
397
|
+
* @returns the number of Unicode code points.
|
|
398
|
+
*/
|
|
399
|
+
declare function codePointLength(text: string): number;
|
|
400
|
+
/**
|
|
401
|
+
* Test whether a settings value names a supported compression profile.
|
|
402
|
+
* @param value - untrusted settings value.
|
|
403
|
+
* @returns whether the value is a supported compression profile.
|
|
404
|
+
*/
|
|
405
|
+
declare function isCompressionProfile(value: unknown): value is CompressionProfile;
|
|
406
|
+
/**
|
|
407
|
+
* Resolve and validate plugin configuration.
|
|
408
|
+
* @param config - optional composition overrides.
|
|
409
|
+
* @returns a detached, deeply immutable configuration snapshot.
|
|
410
|
+
*/
|
|
411
|
+
declare function resolveConfig(config?: ToolResultPruneConfig): ResolvedConfig;
|
|
412
|
+
/**
|
|
413
|
+
* Resolve one public profile into a complete mixed-strategy policy.
|
|
414
|
+
* @param config - validated composition configuration.
|
|
415
|
+
* @param profile - profile frozen for the target Session.
|
|
416
|
+
* @param custom - versioned Custom document used only by the `custom` profile.
|
|
417
|
+
* @param options - routed capacity and the frozen Auto Compact threshold used
|
|
418
|
+
* to resolve context-percent Custom values and standard-profile linkage.
|
|
419
|
+
* @returns the effective deterministic compression policy.
|
|
420
|
+
*/
|
|
421
|
+
declare function resolvePolicy(config: ResolvedConfig, profile: CompressionProfile, custom?: CustomCompressionPolicy, options?: CustomPolicyResolutionOptions): CompressionPolicy;
|
|
422
|
+
//#endregion
|
|
423
|
+
//#region src/runtime/reducers.d.ts
|
|
424
|
+
/** Deterministic, evidence-backed reducers for fresh tool results. */
|
|
425
|
+
/** Input shared by every fresh-result reducer. */
|
|
426
|
+
interface ReducerInput {
|
|
427
|
+
readonly toolName: string;
|
|
428
|
+
readonly argumentsText: string;
|
|
429
|
+
readonly text: string;
|
|
430
|
+
readonly budgetChars: number;
|
|
431
|
+
readonly sourceRef: string;
|
|
432
|
+
readonly isError: boolean;
|
|
433
|
+
/**
|
|
434
|
+
* Orthogonal user gate for the `hypa-code-skeleton` candidate. Absent or
|
|
435
|
+
* false keeps source-code content on its existing head/tail reducers.
|
|
436
|
+
*/
|
|
437
|
+
readonly codeSkeleton?: boolean;
|
|
438
|
+
}
|
|
439
|
+
/** One verified reducer candidate. */
|
|
440
|
+
interface ReducerOutput {
|
|
441
|
+
readonly text: string;
|
|
442
|
+
readonly reducer: string;
|
|
443
|
+
readonly lossy: boolean;
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Select a reducer from verified tool, command, and content evidence.
|
|
447
|
+
* @param input - original result text, recovery source, and output budget.
|
|
448
|
+
* @returns a verified candidate, or `null` when every reducer fails open.
|
|
449
|
+
*/
|
|
450
|
+
declare function reduceFreshToolResult(input: ReducerInput): ReducerOutput | null;
|
|
451
|
+
/**
|
|
452
|
+
* Build a recoverable placeholder for an old tool result.
|
|
453
|
+
* @param input - tool identity, source reference, size, status, and retained evidence.
|
|
454
|
+
* @returns a lossy placeholder that cites the immutable source event.
|
|
455
|
+
*/
|
|
456
|
+
declare function historicalPlaceholder(input: {
|
|
457
|
+
readonly toolName: string;
|
|
458
|
+
readonly sourceRef: string;
|
|
459
|
+
readonly charsBefore: number;
|
|
460
|
+
readonly isError: boolean;
|
|
461
|
+
readonly text: string;
|
|
462
|
+
readonly compact?: boolean;
|
|
463
|
+
}): ReducerOutput;
|
|
464
|
+
/**
|
|
465
|
+
* Validate shrinkage, budget, recovery, and error retention.
|
|
466
|
+
* @param input - original reducer input and its safety requirements.
|
|
467
|
+
* @param output - candidate reduced text and reducer metadata.
|
|
468
|
+
* @returns whether the candidate is safe to land.
|
|
469
|
+
*/
|
|
470
|
+
declare function verifyReduction(input: ReducerInput, output: ReducerOutput): boolean;
|
|
471
|
+
/**
|
|
472
|
+
* Strip ANSI, collapse carriage-return progress redraws, and fold exact repeats.
|
|
473
|
+
* @param text - raw terminal output.
|
|
474
|
+
* @returns normalized terminal text.
|
|
475
|
+
*/
|
|
476
|
+
declare function normalizeTerminalText(text: string): string;
|
|
477
|
+
//#endregion
|
|
478
|
+
//#region src/runtime/token-count.d.ts
|
|
479
|
+
/** Token counts owned by the standalone compression runtime. */
|
|
480
|
+
/** Exact count of one canonical value under a pinned tokenizer artifact. */
|
|
481
|
+
interface ExactTokenizerTokenCount {
|
|
482
|
+
readonly kind: 'exact-tokenizer';
|
|
483
|
+
readonly tokens: number;
|
|
484
|
+
readonly tokenizerId: string;
|
|
485
|
+
readonly tokenizerRevision: string;
|
|
486
|
+
}
|
|
487
|
+
/** A value that the bundled tokenizer cannot safely count. */
|
|
488
|
+
interface UnavailableTokenCount {
|
|
489
|
+
readonly kind: 'unavailable';
|
|
490
|
+
readonly reason: string;
|
|
491
|
+
}
|
|
492
|
+
/** Best-effort estimate carrying a conservative upper value when available. */
|
|
493
|
+
interface TokenizerEstimateTokenCount {
|
|
494
|
+
readonly kind: 'tokenizer-estimate';
|
|
495
|
+
readonly tokens: number;
|
|
496
|
+
readonly upperBoundTokens: number;
|
|
497
|
+
readonly estimatorId: string;
|
|
498
|
+
readonly estimatorRevision: string;
|
|
499
|
+
readonly calibration?: Readonly<{
|
|
500
|
+
readonly sampleCount: number;
|
|
501
|
+
readonly conservativeMarginTokens: number;
|
|
502
|
+
}>;
|
|
503
|
+
}
|
|
504
|
+
/** Exact, conservative request estimate, or an explicit refusal. */
|
|
505
|
+
type TokenCount = ExactTokenizerTokenCount | TokenizerEstimateTokenCount | UnavailableTokenCount;
|
|
506
|
+
//#endregion
|
|
507
|
+
//#region src/runtime/measurement.d.ts
|
|
508
|
+
/** Request identity retained only when every dimension is publicly known. */
|
|
509
|
+
interface ProviderMeasurementKey {
|
|
510
|
+
readonly provider: string;
|
|
511
|
+
readonly baseUrlClass: string;
|
|
512
|
+
readonly apiRoute: string;
|
|
513
|
+
readonly modelId: string;
|
|
514
|
+
readonly requestTemplateRevision: string;
|
|
515
|
+
readonly tokenizerRevision: string;
|
|
516
|
+
readonly modality: string;
|
|
517
|
+
}
|
|
518
|
+
/** Rich request observation used by Adaptive when a future public API supplies it. */
|
|
519
|
+
interface ObservedPromptUsage {
|
|
520
|
+
readonly attemptId: string;
|
|
521
|
+
readonly providerRequestOrdinal: number;
|
|
522
|
+
readonly startedAtMs: number;
|
|
523
|
+
readonly completedAtMs: number;
|
|
524
|
+
readonly measurement: TokenCount;
|
|
525
|
+
readonly observedPromptTokens: number;
|
|
526
|
+
readonly observedOutputTokens?: number;
|
|
527
|
+
readonly responseModelId?: string;
|
|
528
|
+
readonly cacheStatus?: 'complete' | 'unknown';
|
|
529
|
+
readonly cacheReadTokens?: number;
|
|
530
|
+
readonly cacheMissTokens?: number;
|
|
531
|
+
readonly key?: ProviderMeasurementKey;
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Intrinsic-grid diagnostic attached to nodes whose count estimates images.
|
|
535
|
+
* It reports ONLY the official block
|
|
536
|
+
* arithmetic evaluated on the attachment's intrinsic dimensions at the two
|
|
537
|
+
* alignment-padding extremes (compress-pad 0 and 3). It is NOT a request-token
|
|
538
|
+
* bound: the adapter may still re-project the image (per-route pixel-budget
|
|
539
|
+
* or image-detail overrides, byte-cap reprojection), which can move the real
|
|
540
|
+
* count below the diagnostic minimum. It never participates in exact gates,
|
|
541
|
+
* rewrite proofs, or any lossy decision.
|
|
542
|
+
*/
|
|
543
|
+
interface IntrinsicImageBlockDiagnostic {
|
|
544
|
+
readonly paddingMinimumTokens: number;
|
|
545
|
+
readonly paddingMaximumTokens: number;
|
|
546
|
+
}
|
|
547
|
+
/** One same-revision surface node with exact, estimated, or unavailable count. */
|
|
548
|
+
interface MeasuredTokenSurfaceNode {
|
|
549
|
+
readonly seq: number;
|
|
550
|
+
readonly count: TokenCount;
|
|
551
|
+
/** Intrinsic-grid diagnostic when usable image dimensions were available. */
|
|
552
|
+
readonly intrinsicImageBlockEstimate?: IntrinsicImageBlockDiagnostic;
|
|
553
|
+
}
|
|
554
|
+
/** Compression view derived only from published Session and TokenMeter methods. */
|
|
555
|
+
interface CompactionTokenView extends TokenMeasurement {
|
|
556
|
+
readonly providerRoute?: string;
|
|
557
|
+
readonly modelId?: string;
|
|
558
|
+
readonly measuredNodes: readonly MeasuredTokenSurfaceNode[];
|
|
559
|
+
readonly currentSurface: TokenCount;
|
|
560
|
+
/** Sum of per-node intrinsic padding minima; a diagnostic, not a token bound. */
|
|
561
|
+
readonly intrinsicImageBlockEstimateTokens: number;
|
|
562
|
+
readonly latestEnvelopeKey?: ProviderMeasurementKey;
|
|
563
|
+
readonly lastCompletedUsage?: ObservedPromptUsage;
|
|
564
|
+
countCanonicalText(text: string): TokenCount;
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Capture one route-bound view without calling patched Harness methods.
|
|
568
|
+
* Official `measure()` remains authoritative for request pressure; the bundled
|
|
569
|
+
* tokenizer supplies exact canonical content counts used by safe rewrites.
|
|
570
|
+
*/
|
|
571
|
+
declare function measureForCompaction(ctx: Context, session: Session): CompactionTokenView;
|
|
572
|
+
//#endregion
|
|
573
|
+
//#region src/pruner.d.ts
|
|
574
|
+
declare module '@deepseek-ai/cordis' {
|
|
575
|
+
interface Context {
|
|
576
|
+
toolResultPruner: ToolResultPruner;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
/** Mixed deterministic selector behind the existing `ctx.toolResultPruner` seam. */
|
|
580
|
+
declare class ToolResultPruner extends Service {
|
|
581
|
+
static inject: string[];
|
|
582
|
+
static Config: z<ToolResultPruneConfig>;
|
|
583
|
+
/** Consolidated per-session mutable state. */
|
|
584
|
+
readonly state: PrunerState;
|
|
585
|
+
constructor(ctx: Context, config?: ToolResultPruneConfig);
|
|
586
|
+
/**
|
|
587
|
+
* Measure text content in Unicode code points; non-text blocks cost zero.
|
|
588
|
+
* @param blocks - tool-result content to measure.
|
|
589
|
+
* @returns total Unicode code points across text blocks.
|
|
590
|
+
*/
|
|
591
|
+
measureContent(blocks: readonly ContentBlock[]): number;
|
|
592
|
+
/**
|
|
593
|
+
* Apply the configured native head/middle/tail transform.
|
|
594
|
+
* @param blocks - original tool-result content.
|
|
595
|
+
* @returns reduced content, or `null` when no reduction is required.
|
|
596
|
+
*/
|
|
597
|
+
pruneContent(blocks: readonly ContentBlock[]): ContentBlock[] | null;
|
|
598
|
+
/**
|
|
599
|
+
* Run one stable-surface pass. `fresh` is invoked before every request and
|
|
600
|
+
* only reduces original oversized results. `pressure` is called by
|
|
601
|
+
* compaction-basic and may additionally age old results at one high-water.
|
|
602
|
+
* @param session - session whose current tool-result surface may be rewritten.
|
|
603
|
+
* @param options - pass stage and optional completed-step coordinates.
|
|
604
|
+
* @returns landed replacements and aggregate Unicode-code-point savings.
|
|
605
|
+
*/
|
|
606
|
+
pruneSession(session: Session, options?: PruneSessionOptions): PruneResult;
|
|
607
|
+
private activeSettings;
|
|
608
|
+
/**
|
|
609
|
+
* TokenPilot-inspired A2: replace the compaction summary checkpoint node
|
|
610
|
+
* with the same summary plus an Exact Sources locator block. Fails open:
|
|
611
|
+
* any unresolved shape (no trace, no checkpoint node, already annotated)
|
|
612
|
+
* leaves the summary untouched.
|
|
613
|
+
*/
|
|
614
|
+
private attachSummaryLocator;
|
|
615
|
+
/**
|
|
616
|
+
* TokenPilot-inspired E1: sample oversized historical reads and ask the
|
|
617
|
+
* auxiliary estimator whether their file state is still likely to be
|
|
618
|
+
* referenced. Fire-and-forget: never awaited on the pruning chain, failures
|
|
619
|
+
* back off exponentially per Session, verdicts only extend the rule-only
|
|
620
|
+
* superseded classification.
|
|
621
|
+
*/
|
|
622
|
+
private postflightEstimatorPass;
|
|
623
|
+
private activePolicy;
|
|
624
|
+
private contextWindowForRequest;
|
|
625
|
+
private runRequestBoundary;
|
|
626
|
+
/** Resolve historical-aging authority without accepting caller-supplied elevation. */
|
|
627
|
+
private historyAllowed;
|
|
628
|
+
/**
|
|
629
|
+
* Match the compaction-basic pressure gate using public durable data. The
|
|
630
|
+
* frozen Auto Compact deadline `D = floor(A x 0.875)` replaces the legacy
|
|
631
|
+
* fixed 0.7 ratio once the standard-profile linkage resolved; without
|
|
632
|
+
* linkage the 0.7 ratio is the documented fallback and reproduces the
|
|
633
|
+
* previous behavior.
|
|
634
|
+
*/
|
|
635
|
+
private capacityPressureActive;
|
|
636
|
+
/** Emit one bounded, independently correlatable postflight cost diagnostic per completed attempt. */
|
|
637
|
+
private logAdaptivePostflight;
|
|
638
|
+
/** Decide one already-planned History batch from adjacent request-level facts only. */
|
|
639
|
+
private adaptiveHistoryAllowed;
|
|
640
|
+
private decisions;
|
|
641
|
+
/**
|
|
642
|
+
* TokenPilot-style skipReduction: recovery tool output is permanently exempt
|
|
643
|
+
* from every reduction pass so retrieved content can never enter a
|
|
644
|
+
* compress-restore-oscillation loop. A call-name match covers the built-in
|
|
645
|
+
* recovery tool; the per-session set admits future recovery paths.
|
|
646
|
+
*/
|
|
647
|
+
private isRecoveryExempt;
|
|
648
|
+
/** Register a result seq as permanently exempt from further reduction. */
|
|
649
|
+
private grantRecoveryExemption;
|
|
650
|
+
private decideFreshStep;
|
|
651
|
+
private snapshot;
|
|
652
|
+
private planNative;
|
|
653
|
+
/**
|
|
654
|
+
* TokenPilot-inspired A1: replace a byte-identical repeat of an earlier
|
|
655
|
+
* oversized tool result with a pointer to its first occurrence. The first
|
|
656
|
+
* occurrence's hash is always recorded so later repeats can point at the
|
|
657
|
+
* append-only original event even after the surface copy is reduced.
|
|
658
|
+
*/
|
|
659
|
+
private planDedupe;
|
|
660
|
+
private planFresh;
|
|
661
|
+
private planAggregate;
|
|
662
|
+
/** Preserve bounded diagnostic evidence whenever an all-text error is reduced. */
|
|
663
|
+
private planErrorEvidence;
|
|
664
|
+
private planHistoricalAging;
|
|
665
|
+
private protectedHistoryResultSeqs;
|
|
666
|
+
/** Select the newest completed tool calls and token tail for History-derived stages. */
|
|
667
|
+
private protectedHistoryCandidateSeqs;
|
|
668
|
+
/** Atomically replace at most one oldest safe completed tool-call group. */
|
|
669
|
+
private landOldestTailTrimGroup;
|
|
670
|
+
private reserveTailTrimBoundaryAttempt;
|
|
671
|
+
private uniqueAppendRoot;
|
|
672
|
+
private plan;
|
|
673
|
+
private land;
|
|
674
|
+
private landAll;
|
|
675
|
+
private hasRecoveryTool;
|
|
676
|
+
private auditHistoryEvaluation;
|
|
677
|
+
private auditComponent;
|
|
678
|
+
private auditFailure;
|
|
679
|
+
private auditPublicationFailure;
|
|
680
|
+
private warnExactUnavailable;
|
|
681
|
+
private warnOnce;
|
|
682
|
+
}
|
|
683
|
+
//#endregion
|
|
684
|
+
export { AUTO_COMPACT_THRESHOLD_LIMITS, type AutoCompactSettings, COMPRESSION_PROFILES, CONTEXT_COMPRESSION_SETTINGS_NAMESPACE, type CodeSkeletonSettings, type CompactionTokenView, type CompressionPolicy, type CompressionProfile, type ContextCompressionSettings, ContextCompressionSettingsSchema, type CustomCompressionBudget, type CustomCompressionPolicy, CustomCompressionPolicySchema, type CustomCompressionPolicyV1, type CustomCompressionPolicyV2, type CustomCompressionPolicyV3, type CustomCompressionUnit, type CustomHistoryPolicy, type CustomPolicyResolutionOptions, type CustomPrefixPolicy, type CustomTailTrimPolicy, DEFAULTS, DEFAULT_CUSTOM_COMPRESSION_POLICY, type HistoryMode, type MeasuredTokenSurfaceNode, PRUNE_MARKER, type PruneResult, type PruneSessionOptions, type PruneStage, type PrunedEntry, type ResolvedConfig, type ToolResultPruneConfig, ToolResultPruner, ToolResultPruner as default, codePointLength, historicalPlaceholder, isCompressionProfile, isValidAutoCompactThresholdPercent, measureForCompaction, normalizeTerminalText, parseContextCompressionSettings, reduceFreshToolResult, resolveConfig, resolveCustomPolicy, resolvePolicy, verifyReduction };
|