pi-dynamic-context-pruning 0.1.10 → 0.1.12
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/README.md +23 -43
- package/index.ts +2133 -1976
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import * as fs from "node:fs/promises";
|
|
2
2
|
import * as path from "node:path";
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
getAgentDir,
|
|
6
|
+
type ExtensionAPI,
|
|
7
|
+
type ExtensionContext,
|
|
8
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
9
|
|
|
6
10
|
// ============================================================================
|
|
7
11
|
// Types (kept intentionally minimal/structural so pure helpers stay testable
|
|
@@ -9,118 +13,140 @@ import { getAgentDir, type ExtensionAPI, type ExtensionContext } from "@earendil
|
|
|
9
13
|
// ============================================================================
|
|
10
14
|
|
|
11
15
|
export interface MinimalTextContent {
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
type: "text";
|
|
17
|
+
text: string;
|
|
14
18
|
}
|
|
15
19
|
|
|
16
20
|
export interface MinimalImageContent {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
type: "image";
|
|
22
|
+
data: string;
|
|
23
|
+
mimeType: string;
|
|
20
24
|
}
|
|
21
25
|
|
|
22
26
|
export interface MinimalToolCallContent {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
type: "toolCall";
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
arguments: Record<string, unknown>;
|
|
31
|
+
[key: string]: unknown;
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
export type MinimalAssistantContentBlock =
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
| MinimalTextContent
|
|
36
|
+
| { type: "thinking"; thinking: string; [key: string]: unknown }
|
|
37
|
+
| MinimalToolCallContent;
|
|
34
38
|
|
|
35
39
|
export interface MinimalUserMessage {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
role: "user";
|
|
41
|
+
content: string | (MinimalTextContent | MinimalImageContent)[];
|
|
42
|
+
timestamp?: number;
|
|
43
|
+
[key: string]: unknown;
|
|
40
44
|
}
|
|
41
45
|
|
|
42
46
|
export interface MinimalAssistantMessage {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
role: "assistant";
|
|
48
|
+
content: MinimalAssistantContentBlock[];
|
|
49
|
+
timestamp?: number;
|
|
50
|
+
[key: string]: unknown;
|
|
47
51
|
}
|
|
48
52
|
|
|
49
53
|
export interface MinimalToolResultMessage {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
role: "toolResult";
|
|
55
|
+
toolCallId: string;
|
|
56
|
+
toolName: string;
|
|
57
|
+
content: (MinimalTextContent | MinimalImageContent)[];
|
|
58
|
+
/** Bounded nested-call metadata; child calls have no transcript results to prune. */
|
|
59
|
+
nestedCalls?: unknown;
|
|
60
|
+
isError: boolean;
|
|
61
|
+
timestamp?: number;
|
|
62
|
+
[key: string]: unknown;
|
|
57
63
|
}
|
|
58
64
|
|
|
59
65
|
export interface MinimalOtherMessage {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
66
|
+
role: Exclude<string, "user" | "assistant" | "toolResult">;
|
|
67
|
+
timestamp?: number;
|
|
68
|
+
[key: string]: unknown;
|
|
63
69
|
}
|
|
64
70
|
|
|
65
|
-
export type MinimalMessage =
|
|
71
|
+
export type MinimalMessage =
|
|
72
|
+
| MinimalUserMessage
|
|
73
|
+
| MinimalAssistantMessage
|
|
74
|
+
| MinimalToolResultMessage
|
|
75
|
+
| MinimalOtherMessage;
|
|
66
76
|
|
|
67
77
|
export interface MinimalSessionEntry {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
78
|
+
type: string;
|
|
79
|
+
id: string;
|
|
80
|
+
parentId: string | null;
|
|
81
|
+
timestamp: string;
|
|
82
|
+
message?: MinimalMessage;
|
|
83
|
+
customType?: string;
|
|
84
|
+
data?: unknown;
|
|
85
|
+
[key: string]: unknown;
|
|
76
86
|
}
|
|
77
87
|
|
|
78
88
|
export interface MinimalProjectedSessionEntry {
|
|
79
|
-
|
|
80
|
-
|
|
89
|
+
sourceEntry: MinimalSessionEntry;
|
|
90
|
+
messages: MinimalMessage[];
|
|
81
91
|
}
|
|
82
92
|
|
|
83
93
|
export interface MinimalSessionProjection {
|
|
84
|
-
|
|
85
|
-
|
|
94
|
+
entries: MinimalProjectedSessionEntry[];
|
|
95
|
+
messages?: MinimalMessage[];
|
|
86
96
|
}
|
|
87
97
|
|
|
88
98
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
89
|
-
|
|
99
|
+
return typeof value === "object" && value !== null;
|
|
90
100
|
}
|
|
91
101
|
|
|
92
102
|
function isMinimalMessage(value: unknown): value is MinimalMessage {
|
|
93
|
-
|
|
103
|
+
return isRecord(value) && typeof value.role === "string" && value.role.length > 0;
|
|
94
104
|
}
|
|
95
105
|
|
|
96
106
|
function isProjectionSourceEntry(value: unknown): value is MinimalSessionEntry {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
107
|
+
return (
|
|
108
|
+
isRecord(value) &&
|
|
109
|
+
typeof value.type === "string" &&
|
|
110
|
+
value.type.length > 0 &&
|
|
111
|
+
typeof value.id === "string" &&
|
|
112
|
+
value.id.length > 0
|
|
113
|
+
);
|
|
102
114
|
}
|
|
103
115
|
|
|
104
116
|
export function normalizeSessionProjection(value: unknown): MinimalSessionProjection | undefined {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
117
|
+
if (!isRecord(value) || !Array.isArray(value.entries)) return undefined;
|
|
118
|
+
|
|
119
|
+
const entries: MinimalProjectedSessionEntry[] = [];
|
|
120
|
+
for (const candidate of value.entries) {
|
|
121
|
+
if (
|
|
122
|
+
!isRecord(candidate) ||
|
|
123
|
+
!isProjectionSourceEntry(candidate.sourceEntry) ||
|
|
124
|
+
!Array.isArray(candidate.messages)
|
|
125
|
+
)
|
|
126
|
+
return undefined;
|
|
127
|
+
if (!candidate.messages.every(isMinimalMessage)) return undefined;
|
|
128
|
+
entries.push({
|
|
129
|
+
sourceEntry: candidate.sourceEntry,
|
|
130
|
+
messages: candidate.messages,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Entries carry the provenance that Pi uses to apply omissions and
|
|
135
|
+
// replacements. Validate the optional flattened field for compatibility, but
|
|
136
|
+
// derive the messages used by pruning from entries so stale/invented records
|
|
137
|
+
// cannot bypass canonical context edits.
|
|
138
|
+
if (
|
|
139
|
+
"messages" in value &&
|
|
140
|
+
(!Array.isArray(value.messages) || !value.messages.every(isMinimalMessage))
|
|
141
|
+
)
|
|
142
|
+
return undefined;
|
|
143
|
+
return { entries, messages: entries.flatMap((entry) => entry.messages) };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function sessionProjectionToMessages(
|
|
147
|
+
projection: MinimalSessionProjection,
|
|
148
|
+
): MinimalMessage[] {
|
|
149
|
+
return projection.entries.flatMap((entry) => entry.messages);
|
|
124
150
|
}
|
|
125
151
|
|
|
126
152
|
// ============================================================================
|
|
@@ -180,30 +206,30 @@ const DEFAULT_MID_LOOP_BREAK_EVEN_THRESHOLD = 1;
|
|
|
180
206
|
|
|
181
207
|
/** Tool names never pruned by default: orchestration/subagent-style tools and todo-like state. */
|
|
182
208
|
const DEFAULT_PROTECTED_TOOL_NAMES = [
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
209
|
+
"todo",
|
|
210
|
+
"task",
|
|
211
|
+
"subagent",
|
|
212
|
+
"agent",
|
|
213
|
+
"skill",
|
|
214
|
+
"oracle",
|
|
215
|
+
"librarian",
|
|
216
|
+
"contrarian",
|
|
217
|
+
"code_reviewer",
|
|
218
|
+
"gnosis",
|
|
219
|
+
"triage_comments",
|
|
194
220
|
];
|
|
195
221
|
|
|
196
222
|
/** File globs never pruned by default: secrets/credential-shaped paths. */
|
|
197
223
|
const DEFAULT_PROTECTED_PATH_GLOBS = [
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
224
|
+
"**/.env",
|
|
225
|
+
"**/.env.*",
|
|
226
|
+
"**/*.pem",
|
|
227
|
+
"**/*.key",
|
|
228
|
+
"**/id_rsa*",
|
|
229
|
+
"**/id_ed25519*",
|
|
230
|
+
"**/secrets/**",
|
|
231
|
+
"**/*.p12",
|
|
232
|
+
"**/*.pfx",
|
|
207
233
|
];
|
|
208
234
|
|
|
209
235
|
const DEFAULT_MIN_CHARS_SAVED = 200;
|
|
@@ -219,251 +245,243 @@ const ERROR_PURGE_STRATEGY_ID = "error-purge";
|
|
|
219
245
|
// ============================================================================
|
|
220
246
|
|
|
221
247
|
export interface PruneProtections {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
248
|
+
/** Tool names (case-insensitive) whose calls are never pruned. */
|
|
249
|
+
toolNames: string[];
|
|
250
|
+
/** Glob patterns matched against string arguments; matches are never pruned. */
|
|
251
|
+
pathGlobs: string[];
|
|
252
|
+
/** Number of most recent conversational turns left untouched. */
|
|
253
|
+
recentTurns: number;
|
|
228
254
|
}
|
|
229
255
|
|
|
230
256
|
export interface PruneThresholds {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
257
|
+
/**
|
|
258
|
+
* Minimum raw text characters a fresh AUTOMATIC prune proposal must remove
|
|
259
|
+
* (original text chars minus placeholder chars) to be considered at all
|
|
260
|
+
* (pe-qdzb). Enforced by `runDynamicContextPruningPipeline` BEFORE the
|
|
261
|
+
* net-benefit gate (see `PruneGateConfig`/`gate`, which uses break-even token
|
|
262
|
+
* math and is a separate, later check): text-only candidates below this floor
|
|
263
|
+
* are dropped up front and never reach gate/batch consideration. Image-bearing
|
|
264
|
+
* tool-result candidates bypass this raw-text floor (images receive no
|
|
265
|
+
* synthetic character count) and proceed to the token-based gate. Manual
|
|
266
|
+
* prunes and already-persisted/replayed decisions always bypass this floor,
|
|
267
|
+
* same as the gate. A value of 0 disables the floor (no filtering).
|
|
268
|
+
*/
|
|
269
|
+
minCharsSaved: number;
|
|
244
270
|
}
|
|
245
271
|
|
|
246
272
|
/** Per-strategy toggles/config (pe-u8gd, pe-qs8j). Each strategy can be disabled independently. */
|
|
247
273
|
export interface StrategiesConfig {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
274
|
+
dedupe: { enabled: boolean };
|
|
275
|
+
errorPurge: {
|
|
276
|
+
enabled: boolean;
|
|
277
|
+
/** A tool call's input is only eligible for purging once it is older than this many turns. */
|
|
278
|
+
minTurnsOld: number;
|
|
279
|
+
};
|
|
280
|
+
/** Superseded file-ops strategy (pe-qs8j): stale read/write/edit outputs replaced by a placeholder. */
|
|
281
|
+
supersededFileOps: { enabled: boolean };
|
|
256
282
|
}
|
|
257
283
|
|
|
258
284
|
/** Net-benefit gate operating mode (pe-s2ho). */
|
|
259
285
|
export type GateMode = "on" | "off" | "always-apply";
|
|
260
286
|
|
|
261
287
|
/**
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
* the same threshold today.
|
|
266
|
-
*
|
|
267
|
-
* pe-c5n9 evidence (turn-END definition, since superseded -- see below):
|
|
268
|
-
* the representative-corpus benchmark showed mid_loop candidates carrying
|
|
269
|
-
* ~all realized net benefit while idle candidates carried ~none. That would
|
|
270
|
-
* have argued for a stricter idle default. However, the only live runtime
|
|
271
|
-
* caller (the `context` event handler below) did not yet perform real
|
|
272
|
-
* mid-loop/idle detection at the time, so no default change was made.
|
|
273
|
-
*
|
|
274
|
-
* pe-zy4s update (2026-07-08): real runtime mid_loop/idle detection is now
|
|
275
|
-
* wired through the `context` event handler (`classifyAgentStateFromMessages`),
|
|
276
|
-
* using a turn-START definition (idle = this is the first LLM call of a turn;
|
|
277
|
-
* mid_loop = the agent already made a call or received a tool result this
|
|
278
|
-
* turn) -- see that function's doc comment for the exact runtime-observable
|
|
279
|
-
* semantics. The benchmark's candidate labeling was aligned to this SAME
|
|
280
|
-
* definition (retiring the old turn-END definition above, which was never
|
|
281
|
-
* runtime-observable), and the state-split was re-derived on the
|
|
282
|
-
* representative corpus (~/.the-last-harness/agent/sessions, 460 candidates)
|
|
283
|
-
* under it.
|
|
284
|
-
*
|
|
285
|
-
* The result REVERSES the earlier (turn-END) finding: at r=0.1 the
|
|
286
|
-
* turn-START "idle" population (calls made at the start of a turn) carries
|
|
287
|
-
* essentially ALL the realized net benefit (idle-optimal T=22, total realized
|
|
288
|
-
* net benefit ~20,594 token-units), while "mid_loop" carries essentially NONE
|
|
289
|
-
* (mid_loop-optimal T=1, total realized net benefit ~0.0 on this corpus). This is
|
|
290
|
-
* the OPPOSITE of what the pe-c5n9 evidence (measured under the old,
|
|
291
|
-
* non-runtime-observable turn-END definition) suggested.
|
|
292
|
-
*
|
|
293
|
-
* Per the pe-zy4s decision rule (stricter idle default only if idle itself is
|
|
294
|
-
* shown to be ~worthless): idle is NOT worthless here -- it is the ONLY state
|
|
295
|
-
* carrying value at r=0.1 -- so the idle default stays at
|
|
296
|
-
* DEFAULT_BREAK_EVEN_THRESHOLD. The mid_loop default, by the same benchmark,
|
|
297
|
-
* is now DEFAULT_MID_LOOP_BREAK_EVEN_THRESHOLD (=1): the measured candidates
|
|
298
|
-
* did not justify a more permissive threshold at r=0.1. Mid_loop pruning can
|
|
299
|
-
* still amortize over enough subsequent calls; state alone does not establish
|
|
300
|
-
* cache warmth or profitability. Both defaults remain config-overridable via
|
|
301
|
-
* gate.breakEvenThresholdByState.
|
|
288
|
+
* Whether the agent is waiting on the user (`idle`) or already iterating
|
|
289
|
+
* (`mid_loop`). The `context` handler classifies this via
|
|
290
|
+
* `classifyAgentStateFromMessages`. Gate defaults are idle 22 and mid_loop 1.
|
|
302
291
|
*/
|
|
303
292
|
export type AgentState = "idle" | "mid_loop";
|
|
304
293
|
|
|
305
294
|
export interface PruneGateConfig {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
295
|
+
/** "on": gate rejects new prunes above threshold. "off": gate is bypassed
|
|
296
|
+
* entirely (no cost modelling, everything applies). "always-apply": cost
|
|
297
|
+
* is still modelled (for stats/observability) but nothing is ever rejected. */
|
|
298
|
+
mode: GateMode;
|
|
299
|
+
/** Fraction of full price still paid for cached ( r ). Default 0.1. */
|
|
300
|
+
cachedPriceRatio: number;
|
|
301
|
+
/**
|
|
302
|
+
* Default break-even threshold (max future calls to amortize a cache
|
|
303
|
+
* bust) used when no state-specific override applies. Recalibrated from
|
|
304
|
+
* the representative-corpus benchmark; see DEFAULT_BREAK_EVEN_THRESHOLD
|
|
305
|
+
* for the full rationale and ratio-sensitivity caveats.
|
|
306
|
+
*/
|
|
307
|
+
breakEvenThreshold: number;
|
|
308
|
+
/** Per-state overrides. Defaults: idle 22, mid_loop 1. */
|
|
309
|
+
breakEvenThresholdByState: { idle: number; mid_loop: number };
|
|
321
310
|
}
|
|
322
311
|
|
|
323
312
|
export interface DynamicContextPruningConfig {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
313
|
+
enabled: boolean;
|
|
314
|
+
protections: PruneProtections;
|
|
315
|
+
thresholds: PruneThresholds;
|
|
316
|
+
gate: PruneGateConfig;
|
|
317
|
+
strategies: StrategiesConfig;
|
|
329
318
|
}
|
|
330
319
|
|
|
331
320
|
export function defaultConfig(): DynamicContextPruningConfig {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
321
|
+
return {
|
|
322
|
+
enabled: true,
|
|
323
|
+
protections: {
|
|
324
|
+
toolNames: [...DEFAULT_PROTECTED_TOOL_NAMES],
|
|
325
|
+
pathGlobs: [...DEFAULT_PROTECTED_PATH_GLOBS],
|
|
326
|
+
recentTurns: DEFAULT_RECENT_TURNS,
|
|
327
|
+
},
|
|
328
|
+
thresholds: {
|
|
329
|
+
minCharsSaved: DEFAULT_MIN_CHARS_SAVED,
|
|
330
|
+
},
|
|
331
|
+
strategies: {
|
|
332
|
+
dedupe: { enabled: true },
|
|
333
|
+
errorPurge: { enabled: true, minTurnsOld: DEFAULT_ERROR_PURGE_MIN_TURNS_OLD },
|
|
334
|
+
supersededFileOps: { enabled: true },
|
|
335
|
+
},
|
|
336
|
+
gate: {
|
|
337
|
+
mode: "on",
|
|
338
|
+
cachedPriceRatio: DEFAULT_CACHED_PRICE_RATIO,
|
|
339
|
+
breakEvenThreshold: DEFAULT_BREAK_EVEN_THRESHOLD,
|
|
340
|
+
breakEvenThresholdByState: {
|
|
341
|
+
idle: DEFAULT_BREAK_EVEN_THRESHOLD,
|
|
342
|
+
mid_loop: DEFAULT_MID_LOOP_BREAK_EVEN_THRESHOLD,
|
|
343
|
+
},
|
|
344
|
+
},
|
|
345
|
+
};
|
|
354
346
|
}
|
|
355
347
|
|
|
356
348
|
function asStringArray(value: unknown, fallback: string[]): string[] {
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
349
|
+
if (!Array.isArray(value)) return fallback;
|
|
350
|
+
const out = value.filter((item): item is string => typeof item === "string" && item.length > 0);
|
|
351
|
+
return out.length > 0 ? out : fallback;
|
|
360
352
|
}
|
|
361
353
|
|
|
362
354
|
function asNonNegativeInt(value: unknown, fallback: number): number {
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
355
|
+
const parsed = typeof value === "number" ? value : Number(value);
|
|
356
|
+
if (!Number.isFinite(parsed) || parsed < 0) return fallback;
|
|
357
|
+
return Math.floor(parsed);
|
|
366
358
|
}
|
|
367
359
|
|
|
368
360
|
function asNonNegativeNumber(value: unknown, fallback: number): number {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
361
|
+
const parsed = typeof value === "number" ? value : Number(value);
|
|
362
|
+
if (!Number.isFinite(parsed) || parsed < 0) return fallback;
|
|
363
|
+
return parsed;
|
|
372
364
|
}
|
|
373
365
|
|
|
374
366
|
function asRatio(value: unknown, fallback: number): number {
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
367
|
+
const parsed = typeof value === "number" ? value : Number(value);
|
|
368
|
+
if (!Number.isFinite(parsed) || parsed < 0 || parsed > 1) return fallback;
|
|
369
|
+
return parsed;
|
|
378
370
|
}
|
|
379
371
|
|
|
380
372
|
function asGateMode(value: unknown, fallback: GateMode): GateMode {
|
|
381
|
-
|
|
373
|
+
return value === "on" || value === "off" || value === "always-apply" ? value : fallback;
|
|
382
374
|
}
|
|
383
375
|
|
|
384
376
|
function asBoolean(value: unknown, fallback: boolean): boolean {
|
|
385
|
-
|
|
377
|
+
return typeof value === "boolean" ? value : fallback;
|
|
386
378
|
}
|
|
387
379
|
|
|
388
380
|
/** Merge a partially-shaped parsed config (e.g. from disk) onto defaults. Never throws. */
|
|
389
381
|
export function normalizeConfig(parsed: unknown): DynamicContextPruningConfig {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
382
|
+
const defaults = defaultConfig();
|
|
383
|
+
if (!parsed || typeof parsed !== "object") return defaults;
|
|
384
|
+
const raw = parsed as Record<string, unknown>;
|
|
385
|
+
const rawProtections = (
|
|
386
|
+
raw.protections && typeof raw.protections === "object" ? raw.protections : {}
|
|
387
|
+
) as Record<string, unknown>;
|
|
388
|
+
const rawThresholds = (
|
|
389
|
+
raw.thresholds && typeof raw.thresholds === "object" ? raw.thresholds : {}
|
|
390
|
+
) as Record<string, unknown>;
|
|
391
|
+
const rawGate = (raw.gate && typeof raw.gate === "object" ? raw.gate : {}) as Record<
|
|
392
|
+
string,
|
|
393
|
+
unknown
|
|
394
|
+
>;
|
|
395
|
+
const rawGateByState = (
|
|
396
|
+
rawGate.breakEvenThresholdByState && typeof rawGate.breakEvenThresholdByState === "object"
|
|
397
|
+
? rawGate.breakEvenThresholdByState
|
|
398
|
+
: {}
|
|
399
|
+
) as Record<string, unknown>;
|
|
400
|
+
const rawStrategies = (
|
|
401
|
+
raw.strategies && typeof raw.strategies === "object" ? raw.strategies : {}
|
|
402
|
+
) as Record<string, unknown>;
|
|
403
|
+
const rawDedupe = (
|
|
404
|
+
rawStrategies.dedupe && typeof rawStrategies.dedupe === "object" ? rawStrategies.dedupe : {}
|
|
405
|
+
) as Record<string, unknown>;
|
|
406
|
+
const rawErrorPurge = (
|
|
407
|
+
rawStrategies.errorPurge && typeof rawStrategies.errorPurge === "object"
|
|
408
|
+
? rawStrategies.errorPurge
|
|
409
|
+
: {}
|
|
410
|
+
) as Record<string, unknown>;
|
|
411
|
+
const rawSupersededFileOps = (
|
|
412
|
+
rawStrategies.supersededFileOps && typeof rawStrategies.supersededFileOps === "object"
|
|
413
|
+
? rawStrategies.supersededFileOps
|
|
414
|
+
: {}
|
|
415
|
+
) as Record<string, unknown>;
|
|
416
|
+
|
|
417
|
+
return {
|
|
418
|
+
enabled: typeof raw.enabled === "boolean" ? raw.enabled : defaults.enabled,
|
|
419
|
+
protections: {
|
|
420
|
+
toolNames: asStringArray(rawProtections.toolNames, defaults.protections.toolNames),
|
|
421
|
+
pathGlobs: asStringArray(rawProtections.pathGlobs, defaults.protections.pathGlobs),
|
|
422
|
+
recentTurns: asNonNegativeInt(rawProtections.recentTurns, defaults.protections.recentTurns),
|
|
423
|
+
},
|
|
424
|
+
thresholds: {
|
|
425
|
+
minCharsSaved: asNonNegativeInt(
|
|
426
|
+
rawThresholds.minCharsSaved,
|
|
427
|
+
defaults.thresholds.minCharsSaved,
|
|
428
|
+
),
|
|
429
|
+
},
|
|
430
|
+
strategies: {
|
|
431
|
+
dedupe: { enabled: asBoolean(rawDedupe.enabled, defaults.strategies.dedupe.enabled) },
|
|
432
|
+
errorPurge: {
|
|
433
|
+
enabled: asBoolean(rawErrorPurge.enabled, defaults.strategies.errorPurge.enabled),
|
|
434
|
+
minTurnsOld: asNonNegativeInt(
|
|
435
|
+
rawErrorPurge.minTurnsOld,
|
|
436
|
+
defaults.strategies.errorPurge.minTurnsOld,
|
|
437
|
+
),
|
|
438
|
+
},
|
|
439
|
+
supersededFileOps: {
|
|
440
|
+
enabled: asBoolean(
|
|
441
|
+
rawSupersededFileOps.enabled,
|
|
442
|
+
defaults.strategies.supersededFileOps.enabled,
|
|
443
|
+
),
|
|
444
|
+
},
|
|
445
|
+
},
|
|
446
|
+
gate: {
|
|
447
|
+
mode: asGateMode(rawGate.mode, defaults.gate.mode),
|
|
448
|
+
cachedPriceRatio: asRatio(rawGate.cachedPriceRatio, defaults.gate.cachedPriceRatio),
|
|
449
|
+
breakEvenThreshold: asNonNegativeNumber(
|
|
450
|
+
rawGate.breakEvenThreshold,
|
|
451
|
+
defaults.gate.breakEvenThreshold,
|
|
452
|
+
),
|
|
453
|
+
breakEvenThresholdByState: {
|
|
454
|
+
idle: asNonNegativeNumber(
|
|
455
|
+
rawGateByState.idle,
|
|
456
|
+
defaults.gate.breakEvenThresholdByState.idle,
|
|
457
|
+
),
|
|
458
|
+
mid_loop: asNonNegativeNumber(
|
|
459
|
+
rawGateByState.mid_loop,
|
|
460
|
+
defaults.gate.breakEvenThresholdByState.mid_loop,
|
|
461
|
+
),
|
|
462
|
+
},
|
|
463
|
+
},
|
|
464
|
+
};
|
|
447
465
|
}
|
|
448
466
|
|
|
449
467
|
function getConfigPath(): string {
|
|
450
|
-
|
|
468
|
+
return path.join(getAgentDir(), "extensions", CONFIG_FILE_NAME);
|
|
451
469
|
}
|
|
452
470
|
|
|
453
471
|
async function readConfig(): Promise<DynamicContextPruningConfig> {
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
472
|
+
try {
|
|
473
|
+
const raw = await fs.readFile(getConfigPath(), "utf8");
|
|
474
|
+
return normalizeConfig(JSON.parse(raw));
|
|
475
|
+
} catch {
|
|
476
|
+
return defaultConfig();
|
|
477
|
+
}
|
|
460
478
|
}
|
|
461
479
|
|
|
462
480
|
async function writeConfig(config: DynamicContextPruningConfig): Promise<void> {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
481
|
+
const configPath = getConfigPath();
|
|
482
|
+
const payload = { ...config, updatedAt: new Date().toISOString() };
|
|
483
|
+
await fs.mkdir(path.dirname(configPath), { recursive: true });
|
|
484
|
+
await fs.writeFile(configPath, `${JSON.stringify(payload, null, 2)}\n`, "utf8");
|
|
467
485
|
}
|
|
468
486
|
|
|
469
487
|
// ============================================================================
|
|
@@ -472,19 +490,21 @@ async function writeConfig(config: DynamicContextPruningConfig): Promise<void> {
|
|
|
472
490
|
// ============================================================================
|
|
473
491
|
|
|
474
492
|
export function estimateTokensForText(text: string): number {
|
|
475
|
-
|
|
476
|
-
|
|
493
|
+
if (!text) return 0;
|
|
494
|
+
return Math.max(0, Math.ceil(text.length / 4));
|
|
477
495
|
}
|
|
478
496
|
|
|
479
|
-
export function estimateTokensForContent(
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
497
|
+
export function estimateTokensForContent(
|
|
498
|
+
content: string | (MinimalTextContent | MinimalImageContent)[],
|
|
499
|
+
): number {
|
|
500
|
+
if (typeof content === "string") return estimateTokensForText(content);
|
|
501
|
+
if (!Array.isArray(content)) return 0;
|
|
502
|
+
let total = 0;
|
|
503
|
+
for (const block of content) {
|
|
504
|
+
if (block.type === "text") total += estimateTokensForText(block.text);
|
|
505
|
+
else if (block.type === "image") total += 1200; // rough fixed estimate, mirrors context-inspector's convention
|
|
506
|
+
}
|
|
507
|
+
return total;
|
|
488
508
|
}
|
|
489
509
|
|
|
490
510
|
export type TokenEstimator = (text: string) => number;
|
|
@@ -515,37 +535,45 @@ export type TokenEstimator = (text: string) => number;
|
|
|
515
535
|
* call is a turn's last one, so that definition was never runtime-observable.)
|
|
516
536
|
*/
|
|
517
537
|
export function classifyAgentStateFromMessages(messages: MinimalMessage[]): AgentState {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
538
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
539
|
+
const role = messages[i].role;
|
|
540
|
+
if (role === "user") return "idle";
|
|
541
|
+
if (role === "assistant" || role === "toolResult") return "mid_loop";
|
|
542
|
+
// Any other role (e.g. "system") is skipped: keep walking further back.
|
|
543
|
+
}
|
|
544
|
+
return "idle";
|
|
525
545
|
}
|
|
526
546
|
|
|
527
547
|
/** Resolve which break-even threshold applies for a given agent state (pe-s2ho state-conditioning hook). */
|
|
528
|
-
export function resolveBreakEvenThreshold(
|
|
529
|
-
|
|
530
|
-
|
|
548
|
+
export function resolveBreakEvenThreshold(
|
|
549
|
+
gateConfig: PruneGateConfig,
|
|
550
|
+
agentState: AgentState,
|
|
551
|
+
): number {
|
|
552
|
+
const override = gateConfig.breakEvenThresholdByState[agentState];
|
|
553
|
+
return typeof override === "number" && Number.isFinite(override)
|
|
554
|
+
? override
|
|
555
|
+
: gateConfig.breakEvenThreshold;
|
|
531
556
|
}
|
|
532
557
|
|
|
533
558
|
/** Estimate the token cost of a single message (all content kinds), for tail/context-size accounting. */
|
|
534
559
|
function estimateMessageTokens(message: MinimalMessage, estimateTokens: TokenEstimator): number {
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
560
|
+
if (message.role === "user" || message.role === "toolResult") {
|
|
561
|
+
const content = (message as MinimalUserMessage | MinimalToolResultMessage).content;
|
|
562
|
+
return estimateTokensForContent(
|
|
563
|
+
content as string | (MinimalTextContent | MinimalImageContent)[],
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
if (message.role === "assistant") {
|
|
567
|
+
let total = 0;
|
|
568
|
+
for (const block of (message as MinimalAssistantMessage).content) {
|
|
569
|
+
if (block.type === "text") total += estimateTokens(block.text);
|
|
570
|
+
else if (block.type === "thinking") total += estimateTokens(block.thinking);
|
|
571
|
+
else if (block.type === "toolCall")
|
|
572
|
+
total += estimateTokens(JSON.stringify(block.arguments ?? {}));
|
|
573
|
+
}
|
|
574
|
+
return total;
|
|
575
|
+
}
|
|
576
|
+
return 0;
|
|
549
577
|
}
|
|
550
578
|
|
|
551
579
|
/**
|
|
@@ -555,16 +583,16 @@ function estimateMessageTokens(message: MinimalMessage, estimateTokens: TokenEst
|
|
|
555
583
|
* `fromIndex` (e.g. the very last message, or beyond) degrades gracefully.
|
|
556
584
|
*/
|
|
557
585
|
export function estimateTailTokens(
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
586
|
+
messages: MinimalMessage[],
|
|
587
|
+
fromIndex: number,
|
|
588
|
+
estimateTokens: TokenEstimator = estimateTokensForText,
|
|
561
589
|
): number {
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
590
|
+
let total = 0;
|
|
591
|
+
const start = Math.max(0, fromIndex);
|
|
592
|
+
for (let i = start; i < messages.length; i++) {
|
|
593
|
+
total += estimateMessageTokens(messages[i], estimateTokens);
|
|
594
|
+
}
|
|
595
|
+
return total;
|
|
568
596
|
}
|
|
569
597
|
|
|
570
598
|
// ============================================================================
|
|
@@ -572,66 +600,73 @@ export function estimateTailTokens(
|
|
|
572
600
|
// ============================================================================
|
|
573
601
|
|
|
574
602
|
function globToRegExp(glob: string): RegExp {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
export function isProtectedToolName(
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
603
|
+
let pattern = "^";
|
|
604
|
+
for (let i = 0; i < glob.length; i++) {
|
|
605
|
+
const char = glob[i];
|
|
606
|
+
if (char === "*") {
|
|
607
|
+
if (glob[i + 1] === "*") {
|
|
608
|
+
i++;
|
|
609
|
+
if (glob[i + 1] === "/") {
|
|
610
|
+
pattern += "(?:.*/)?";
|
|
611
|
+
i++;
|
|
612
|
+
} else {
|
|
613
|
+
pattern += ".*";
|
|
614
|
+
}
|
|
615
|
+
} else {
|
|
616
|
+
pattern += "[^/]*";
|
|
617
|
+
}
|
|
618
|
+
} else if (char === "?") {
|
|
619
|
+
pattern += "[^/]";
|
|
620
|
+
} else if ("\\^$.|?*+()[]{}".includes(char)) {
|
|
621
|
+
pattern += `\\${char}`;
|
|
622
|
+
} else {
|
|
623
|
+
pattern += char;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
pattern += "$";
|
|
627
|
+
return new RegExp(pattern);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
export function isProtectedToolName(
|
|
631
|
+
toolName: string | undefined,
|
|
632
|
+
protections: PruneProtections,
|
|
633
|
+
): boolean {
|
|
634
|
+
if (!toolName) return false;
|
|
635
|
+
const normalized = toolName.trim().toLowerCase();
|
|
636
|
+
return protections.toolNames.some((name) => name.trim().toLowerCase() === normalized);
|
|
606
637
|
}
|
|
607
638
|
|
|
608
639
|
export function isProtectedPath(value: string, protections: PruneProtections): boolean {
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
640
|
+
if (!value) return false;
|
|
641
|
+
const normalized = value.trim();
|
|
642
|
+
if (!normalized) return false;
|
|
643
|
+
return protections.pathGlobs.some((glob) => globToRegExp(glob).test(normalized));
|
|
613
644
|
}
|
|
614
645
|
|
|
615
646
|
/** Shallow-collect string values from a tool-call arguments object, for path-glob matching. */
|
|
616
|
-
export function collectArgStringValues(
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
647
|
+
export function collectArgStringValues(
|
|
648
|
+
args: Record<string, unknown> | undefined,
|
|
649
|
+
depth = 2,
|
|
650
|
+
): string[] {
|
|
651
|
+
if (!args || typeof args !== "object") return [];
|
|
652
|
+
const out: string[] = [];
|
|
653
|
+
const visit = (value: unknown, remainingDepth: number) => {
|
|
654
|
+
if (typeof value === "string") {
|
|
655
|
+
out.push(value);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
if (remainingDepth <= 0) return;
|
|
659
|
+
if (Array.isArray(value)) {
|
|
660
|
+
for (const item of value) visit(item, remainingDepth - 1);
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
if (value && typeof value === "object") {
|
|
664
|
+
for (const item of Object.values(value as Record<string, unknown>))
|
|
665
|
+
visit(item, remainingDepth - 1);
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
visit(args, depth);
|
|
669
|
+
return out;
|
|
635
670
|
}
|
|
636
671
|
|
|
637
672
|
/**
|
|
@@ -640,19 +675,22 @@ export function collectArgStringValues(args: Record<string, unknown> | undefined
|
|
|
640
675
|
*
|
|
641
676
|
* A "turn" starts at each user message. Protects the last `recentTurns` turns.
|
|
642
677
|
*/
|
|
643
|
-
export function computeRecencyBoundaryIndex(
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
678
|
+
export function computeRecencyBoundaryIndex(
|
|
679
|
+
messages: MinimalMessage[],
|
|
680
|
+
recentTurns: number,
|
|
681
|
+
): number {
|
|
682
|
+
if (recentTurns <= 0) return messages.length;
|
|
683
|
+
const turnStarts: number[] = [];
|
|
684
|
+
messages.forEach((message, index) => {
|
|
685
|
+
if (message.role === "user") turnStarts.push(index);
|
|
686
|
+
});
|
|
687
|
+
if (turnStarts.length === 0) return 0;
|
|
688
|
+
const startIdx = Math.max(0, turnStarts.length - recentTurns);
|
|
689
|
+
return turnStarts[startIdx] ?? 0;
|
|
652
690
|
}
|
|
653
691
|
|
|
654
692
|
export function isWithinRecencyWindow(index: number, boundaryIndex: number): boolean {
|
|
655
|
-
|
|
693
|
+
return index >= boundaryIndex;
|
|
656
694
|
}
|
|
657
695
|
|
|
658
696
|
/**
|
|
@@ -661,11 +699,11 @@ export function isWithinRecencyWindow(index: number, boundaryIndex: number): boo
|
|
|
661
699
|
* message at the very end of the conversation has 0 turns elapsed.
|
|
662
700
|
*/
|
|
663
701
|
export function computeTurnsElapsedSince(messages: MinimalMessage[], index: number): number {
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
702
|
+
let count = 0;
|
|
703
|
+
for (let i = index + 1; i < messages.length; i++) {
|
|
704
|
+
if (messages[i].role === "user") count++;
|
|
705
|
+
}
|
|
706
|
+
return count;
|
|
669
707
|
}
|
|
670
708
|
|
|
671
709
|
// ============================================================================
|
|
@@ -673,9 +711,9 @@ export function computeTurnsElapsedSince(messages: MinimalMessage[], index: numb
|
|
|
673
711
|
// ============================================================================
|
|
674
712
|
|
|
675
713
|
export interface ToolCallPairIndices {
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
714
|
+
assistantIndex?: number;
|
|
715
|
+
toolCallBlockIndex?: number;
|
|
716
|
+
resultIndex?: number;
|
|
679
717
|
}
|
|
680
718
|
|
|
681
719
|
/**
|
|
@@ -686,37 +724,42 @@ export interface ToolCallPairIndices {
|
|
|
686
724
|
* pass it to `findToolCallPairIndices` and friends to avoid an O(n) rescan
|
|
687
725
|
* per decision (pe-e0zd).
|
|
688
726
|
*/
|
|
689
|
-
export function buildToolCallPairIndex(
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
727
|
+
export function buildToolCallPairIndex(
|
|
728
|
+
messages: MinimalMessage[],
|
|
729
|
+
): Map<string, ToolCallPairIndices> {
|
|
730
|
+
const index = new Map<string, ToolCallPairIndices>();
|
|
731
|
+
const getOrCreate = (toolCallId: string): ToolCallPairIndices => {
|
|
732
|
+
let pair = index.get(toolCallId);
|
|
733
|
+
if (!pair) {
|
|
734
|
+
pair = {};
|
|
735
|
+
index.set(toolCallId, pair);
|
|
736
|
+
}
|
|
737
|
+
return pair;
|
|
738
|
+
};
|
|
739
|
+
messages.forEach((message, messageIndex) => {
|
|
740
|
+
if (
|
|
741
|
+
message.role === "assistant" &&
|
|
742
|
+
Array.isArray((message as MinimalAssistantMessage).content)
|
|
743
|
+
) {
|
|
744
|
+
const content = (message as MinimalAssistantMessage).content;
|
|
745
|
+
const seenInThisMessage = new Set<string>();
|
|
746
|
+
content.forEach((block, blockIndex) => {
|
|
747
|
+
if (block.type !== "toolCall") return;
|
|
748
|
+
// Mirror findToolCallPairIndices' content.findIndex: within a single
|
|
749
|
+
// message, the FIRST matching block wins (duplicate ids in one
|
|
750
|
+
// message's content are pathological but must resolve identically).
|
|
751
|
+
if (seenInThisMessage.has(block.id)) return;
|
|
752
|
+
seenInThisMessage.add(block.id);
|
|
753
|
+
const pair = getOrCreate(block.id);
|
|
754
|
+
pair.assistantIndex = messageIndex;
|
|
755
|
+
pair.toolCallBlockIndex = blockIndex;
|
|
756
|
+
});
|
|
757
|
+
} else if (message.role === "toolResult") {
|
|
758
|
+
const pair = getOrCreate((message as MinimalToolResultMessage).toolCallId);
|
|
759
|
+
pair.resultIndex = messageIndex;
|
|
760
|
+
}
|
|
761
|
+
});
|
|
762
|
+
return index;
|
|
720
763
|
}
|
|
721
764
|
|
|
722
765
|
/**
|
|
@@ -726,125 +769,33 @@ export function buildToolCallPairIndex(messages: MinimalMessage[]): Map<string,
|
|
|
726
769
|
* `messages`; omitting it preserves the original O(n) full-scan behavior.
|
|
727
770
|
*/
|
|
728
771
|
export function findToolCallPairIndices(
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
772
|
+
messages: MinimalMessage[],
|
|
773
|
+
toolCallId: string,
|
|
774
|
+
pairIndex?: Map<string, ToolCallPairIndices>,
|
|
732
775
|
): ToolCallPairIndices {
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
}
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
return undefined;
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
function buildProjectedEntryIdToMessageIndexMap(
|
|
760
|
-
entries: MinimalSessionEntry[],
|
|
761
|
-
messages: MinimalMessage[],
|
|
762
|
-
projection: MinimalSessionProjection,
|
|
763
|
-
): Map<string, number> {
|
|
764
|
-
const map = new Map<string, number>();
|
|
765
|
-
const entryIds = new Set(entries.map((entry) => entry.id));
|
|
766
|
-
let cursor = 0;
|
|
767
|
-
|
|
768
|
-
for (const projectedEntry of projection.entries) {
|
|
769
|
-
let firstMessageIndex: number | undefined;
|
|
770
|
-
for (const projectedMessage of projectedEntry.messages) {
|
|
771
|
-
let messageIndex = messages.indexOf(projectedMessage, cursor);
|
|
772
|
-
if (messageIndex < cursor) messageIndex = -1;
|
|
773
|
-
|
|
774
|
-
if (messageIndex < 0 && typeof projectedMessage.timestamp === "number") {
|
|
775
|
-
messageIndex = messages.findIndex(
|
|
776
|
-
(candidate, index) =>
|
|
777
|
-
index >= cursor && candidate.role === projectedMessage.role && candidate.timestamp === projectedMessage.timestamp,
|
|
778
|
-
);
|
|
779
|
-
}
|
|
780
|
-
if (messageIndex < 0) continue;
|
|
781
|
-
firstMessageIndex ??= messageIndex;
|
|
782
|
-
cursor = messageIndex + 1;
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
if (firstMessageIndex !== undefined && entryIds.has(projectedEntry.sourceEntry.id)) {
|
|
786
|
-
map.set(projectedEntry.sourceEntry.id, firstMessageIndex);
|
|
787
|
-
}
|
|
788
|
-
}
|
|
789
|
-
return map;
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
export function buildEntryIdToMessageIndexMap(
|
|
793
|
-
entries: MinimalSessionEntry[],
|
|
794
|
-
messages: MinimalMessage[],
|
|
795
|
-
projection?: MinimalSessionProjection,
|
|
796
|
-
): Map<string, number> {
|
|
797
|
-
if (projection) return buildProjectedEntryIdToMessageIndexMap(entries, messages, projection);
|
|
798
|
-
|
|
799
|
-
// Older runtimes expose only raw branch entries; preserve their positional
|
|
800
|
-
// correlation and use role/timestamp only when the counts diverge.
|
|
801
|
-
const producing = entries.filter((entry) =>
|
|
802
|
-
entry.type === "message" || entry.type === "custom_message" || entry.type === "branch_summary" || entry.type === "compaction",
|
|
803
|
-
);
|
|
804
|
-
const length = Math.min(producing.length, messages.length);
|
|
805
|
-
const map = new Map<string, number>();
|
|
806
|
-
for (let i = 0; i < length; i++) {
|
|
807
|
-
const entry = producing[i];
|
|
808
|
-
const expectedRole = expectedRoleForEntry(entry);
|
|
809
|
-
const candidate = messages[i];
|
|
810
|
-
// Guard against silent misalignment (e.g. compaction/branch-summary shifted
|
|
811
|
-
// the projection): only trust the positional zip when roles line up,
|
|
812
|
-
// otherwise leave it unset so callers fall back to role+timestamp matching.
|
|
813
|
-
if (expectedRole && candidate?.role !== expectedRole) continue;
|
|
814
|
-
map.set(entry.id, i);
|
|
815
|
-
}
|
|
816
|
-
return map;
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
/** Last-resort correlation fallback: match by role + timestamp when order-zip counts diverge. */
|
|
820
|
-
export function findMessageIndexByRoleAndTimestamp(
|
|
821
|
-
entry: MinimalSessionEntry,
|
|
822
|
-
messages: MinimalMessage[],
|
|
823
|
-
): number | undefined {
|
|
824
|
-
const message = entry.message;
|
|
825
|
-
if (!message || typeof message.role !== "string" || typeof message.timestamp !== "number") return undefined;
|
|
826
|
-
const index = messages.findIndex((m) => m.role === message.role && m.timestamp === message.timestamp);
|
|
827
|
-
return index >= 0 ? index : undefined;
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
/** Resolve a session entry id using projection provenance, then legacy role+timestamp fallback. */
|
|
831
|
-
export function resolveMessageIndexForEntry(
|
|
832
|
-
entryId: string,
|
|
833
|
-
entries: MinimalSessionEntry[],
|
|
834
|
-
messages: MinimalMessage[],
|
|
835
|
-
projection?: MinimalSessionProjection,
|
|
836
|
-
): number | undefined {
|
|
837
|
-
const zipMap = buildEntryIdToMessageIndexMap(entries, messages, projection);
|
|
838
|
-
const zipped = zipMap.get(entryId);
|
|
839
|
-
if (zipped !== undefined) return zipped;
|
|
840
|
-
|
|
841
|
-
// An empty projected contribution is an explicit omission/replacement state;
|
|
842
|
-
// only a validated projection may make that context-edit-aware decision.
|
|
843
|
-
if (projection?.entries.some((entry) => entry.sourceEntry.id === entryId && entry.messages.length === 0)) return undefined;
|
|
844
|
-
|
|
845
|
-
const entry = entries.find((candidate) => candidate.id === entryId);
|
|
846
|
-
if (!entry) return undefined;
|
|
847
|
-
return findMessageIndexByRoleAndTimestamp(entry, messages);
|
|
776
|
+
if (pairIndex) return pairIndex.get(toolCallId) ?? {};
|
|
777
|
+
const pair: ToolCallPairIndices = {};
|
|
778
|
+
messages.forEach((message, index) => {
|
|
779
|
+
if (
|
|
780
|
+
message.role === "assistant" &&
|
|
781
|
+
Array.isArray((message as MinimalAssistantMessage).content)
|
|
782
|
+
) {
|
|
783
|
+
const content = (message as MinimalAssistantMessage).content;
|
|
784
|
+
const blockIndex = content.findIndex(
|
|
785
|
+
(block) => block.type === "toolCall" && block.id === toolCallId,
|
|
786
|
+
);
|
|
787
|
+
if (blockIndex >= 0) {
|
|
788
|
+
pair.assistantIndex = index;
|
|
789
|
+
pair.toolCallBlockIndex = blockIndex;
|
|
790
|
+
}
|
|
791
|
+
} else if (
|
|
792
|
+
message.role === "toolResult" &&
|
|
793
|
+
(message as MinimalToolResultMessage).toolCallId === toolCallId
|
|
794
|
+
) {
|
|
795
|
+
pair.resultIndex = index;
|
|
796
|
+
}
|
|
797
|
+
});
|
|
798
|
+
return pair;
|
|
848
799
|
}
|
|
849
800
|
|
|
850
801
|
// ============================================================================
|
|
@@ -854,37 +805,37 @@ export function resolveMessageIndexForEntry(
|
|
|
854
805
|
export type PruneTargetKind = "tool_result_content" | "tool_call_input";
|
|
855
806
|
|
|
856
807
|
/**
|
|
857
|
-
* Where a decision came from.
|
|
858
|
-
*
|
|
808
|
+
* Where a decision came from. Manual decisions from the `/prune` picker
|
|
809
|
+
* always bypass the net-benefit gate: user intent wins.
|
|
859
810
|
* Absent/unspecified defaults to "auto".
|
|
860
811
|
*/
|
|
861
812
|
export type PruneDecisionSource = "auto" | "manual";
|
|
862
813
|
|
|
863
814
|
/** What a strategy proposes. The pipeline decides whether/how to apply it. */
|
|
864
815
|
export interface ProposedPrune {
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
816
|
+
strategyId: string;
|
|
817
|
+
toolCallId: string;
|
|
818
|
+
kind: PruneTargetKind;
|
|
819
|
+
reason: string;
|
|
820
|
+
placeholder?: string;
|
|
821
|
+
/** Defaults to "auto"; set to "manual" for user-initiated prunes that must bypass the gate. */
|
|
822
|
+
source?: PruneDecisionSource;
|
|
872
823
|
}
|
|
873
824
|
|
|
874
825
|
/** What actually gets persisted once the pipeline applies a proposal. */
|
|
875
826
|
export interface PruneDecisionRecord {
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
827
|
+
idempotencyKey: string;
|
|
828
|
+
strategyId: string;
|
|
829
|
+
correlation: { type: "toolCallId"; toolCallId: string } | { type: "entryId"; entryId: string };
|
|
830
|
+
kind: PruneTargetKind;
|
|
831
|
+
reason: string;
|
|
832
|
+
placeholder?: string;
|
|
833
|
+
createdAt: string;
|
|
834
|
+
source: PruneDecisionSource;
|
|
884
835
|
}
|
|
885
836
|
|
|
886
837
|
export function buildIdempotencyKey(proposal: ProposedPrune): string {
|
|
887
|
-
|
|
838
|
+
return `${proposal.strategyId}:${proposal.kind}:${proposal.toolCallId}`;
|
|
888
839
|
}
|
|
889
840
|
|
|
890
841
|
/**
|
|
@@ -896,53 +847,59 @@ export function buildIdempotencyKey(proposal: ProposedPrune): string {
|
|
|
896
847
|
* `runDynamicContextPruningPipeline`).
|
|
897
848
|
*/
|
|
898
849
|
function buildDecisionTargetKey(decision: PruneDecisionRecord): string {
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
850
|
+
const correlationId =
|
|
851
|
+
decision.correlation.type === "toolCallId"
|
|
852
|
+
? decision.correlation.toolCallId
|
|
853
|
+
: decision.correlation.entryId;
|
|
854
|
+
return `${decision.kind}:${decision.correlation.type}:${correlationId}`;
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
export function proposalToDecisionRecord(
|
|
858
|
+
proposal: ProposedPrune,
|
|
859
|
+
createdAt: string = new Date().toISOString(),
|
|
860
|
+
): PruneDecisionRecord {
|
|
861
|
+
return {
|
|
862
|
+
idempotencyKey: buildIdempotencyKey(proposal),
|
|
863
|
+
strategyId: proposal.strategyId,
|
|
864
|
+
correlation: { type: "toolCallId", toolCallId: proposal.toolCallId },
|
|
865
|
+
kind: proposal.kind,
|
|
866
|
+
reason: proposal.reason,
|
|
867
|
+
placeholder: proposal.placeholder,
|
|
868
|
+
createdAt,
|
|
869
|
+
source: proposal.source ?? "auto",
|
|
870
|
+
};
|
|
914
871
|
}
|
|
915
872
|
|
|
916
873
|
/** Parse a persisted CustomEntry payload back into a decision record. Returns undefined if malformed. */
|
|
917
874
|
export function parseDecisionRecord(data: unknown): PruneDecisionRecord | undefined {
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
875
|
+
if (!data || typeof data !== "object") return undefined;
|
|
876
|
+
const raw = data as Record<string, unknown>;
|
|
877
|
+
if (typeof raw.idempotencyKey !== "string" || !raw.idempotencyKey) return undefined;
|
|
878
|
+
if (typeof raw.strategyId !== "string") return undefined;
|
|
879
|
+
if (raw.kind !== "tool_result_content" && raw.kind !== "tool_call_input") return undefined;
|
|
880
|
+
if (typeof raw.reason !== "string") return undefined;
|
|
881
|
+
if (typeof raw.createdAt !== "string") return undefined;
|
|
882
|
+
const source: PruneDecisionSource = raw.source === "manual" ? "manual" : "auto";
|
|
883
|
+
|
|
884
|
+
const rawCorrelation = raw.correlation as Record<string, unknown> | undefined;
|
|
885
|
+
let correlation: PruneDecisionRecord["correlation"] | undefined;
|
|
886
|
+
if (rawCorrelation?.type === "toolCallId" && typeof rawCorrelation.toolCallId === "string") {
|
|
887
|
+
correlation = { type: "toolCallId", toolCallId: rawCorrelation.toolCallId };
|
|
888
|
+
} else if (rawCorrelation?.type === "entryId" && typeof rawCorrelation.entryId === "string") {
|
|
889
|
+
correlation = { type: "entryId", entryId: rawCorrelation.entryId };
|
|
890
|
+
}
|
|
891
|
+
if (!correlation) return undefined;
|
|
892
|
+
|
|
893
|
+
return {
|
|
894
|
+
idempotencyKey: raw.idempotencyKey,
|
|
895
|
+
strategyId: raw.strategyId,
|
|
896
|
+
correlation,
|
|
897
|
+
kind: raw.kind,
|
|
898
|
+
reason: raw.reason,
|
|
899
|
+
placeholder: typeof raw.placeholder === "string" ? raw.placeholder : undefined,
|
|
900
|
+
createdAt: raw.createdAt,
|
|
901
|
+
source,
|
|
902
|
+
};
|
|
946
903
|
}
|
|
947
904
|
|
|
948
905
|
// ============================================================================
|
|
@@ -964,37 +921,49 @@ export function parseDecisionRecord(data: unknown): PruneDecisionRecord | undefi
|
|
|
964
921
|
* mutate or delete anything.
|
|
965
922
|
*/
|
|
966
923
|
export interface RestoreRecord {
|
|
967
|
-
|
|
968
|
-
|
|
924
|
+
idempotencyKey: string;
|
|
925
|
+
createdAt: string;
|
|
969
926
|
}
|
|
970
927
|
|
|
971
|
-
export function buildRestoreRecord(
|
|
972
|
-
|
|
928
|
+
export function buildRestoreRecord(
|
|
929
|
+
idempotencyKey: string,
|
|
930
|
+
createdAt: string = new Date().toISOString(),
|
|
931
|
+
): RestoreRecord {
|
|
932
|
+
return { idempotencyKey, createdAt };
|
|
973
933
|
}
|
|
974
934
|
|
|
975
935
|
/** Parse a persisted CustomEntry payload back into a restore record. Returns undefined if malformed. */
|
|
976
936
|
export function parseRestoreRecord(data: unknown): RestoreRecord | undefined {
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
937
|
+
if (!data || typeof data !== "object") return undefined;
|
|
938
|
+
const raw = data as Record<string, unknown>;
|
|
939
|
+
if (typeof raw.idempotencyKey !== "string" || !raw.idempotencyKey) return undefined;
|
|
940
|
+
if (typeof raw.createdAt !== "string") return undefined;
|
|
941
|
+
return { idempotencyKey: raw.idempotencyKey, createdAt: raw.createdAt };
|
|
982
942
|
}
|
|
983
943
|
|
|
984
944
|
/** Build the manual-prune proposal a /prune picker "prune" action emits. Always source: "manual" (gate bypass). */
|
|
985
|
-
export function buildManualPruneProposal(
|
|
986
|
-
|
|
945
|
+
export function buildManualPruneProposal(
|
|
946
|
+
toolCallId: string,
|
|
947
|
+
reason: string = "manual prune via /prune",
|
|
948
|
+
): ProposedPrune {
|
|
949
|
+
return {
|
|
950
|
+
strategyId: MANUAL_STRATEGY_ID,
|
|
951
|
+
toolCallId,
|
|
952
|
+
kind: "tool_result_content",
|
|
953
|
+
reason,
|
|
954
|
+
source: "manual",
|
|
955
|
+
};
|
|
987
956
|
}
|
|
988
957
|
|
|
989
958
|
export interface PruneTombstoneState {
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
959
|
+
/** Decisions whose most recent event (decision vs restore) for their idempotencyKey is a decision. */
|
|
960
|
+
activeDecisions: PruneDecisionRecord[];
|
|
961
|
+
/** idempotencyKeys of `activeDecisions`, for pipeline seen/known-key checks. */
|
|
962
|
+
activeIdempotencyKeys: Set<string>;
|
|
963
|
+
/** idempotencyKeys whose most recent event is a restore: currently tombstoned/inactive. */
|
|
964
|
+
restoredKeys: Set<string>;
|
|
965
|
+
/** Most-recently-seen decision record per key, regardless of current tombstone state (for /prune history display). */
|
|
966
|
+
lastDecisionByKey: Map<string, PruneDecisionRecord>;
|
|
998
967
|
}
|
|
999
968
|
|
|
1000
969
|
/**
|
|
@@ -1006,43 +975,42 @@ export interface PruneTombstoneState {
|
|
|
1006
975
|
* re-persisting a just-restored decision) and by the /prune picker (to show
|
|
1007
976
|
* accurate status and support prune/restore/re-prune).
|
|
1008
977
|
*
|
|
1009
|
-
* Idempotent
|
|
1010
|
-
* the same entries any number of times yields identical results.
|
|
978
|
+
* Idempotent: replaying the same entries any number of times yields identical results.
|
|
1011
979
|
*/
|
|
1012
980
|
export function resolvePruneTombstoneState(entries: MinimalSessionEntry[]): PruneTombstoneState {
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
981
|
+
const lastDecisionByKey = new Map<string, PruneDecisionRecord>();
|
|
982
|
+
const lastEventKindByKey = new Map<string, "decision" | "restore">();
|
|
983
|
+
|
|
984
|
+
for (const entry of entries) {
|
|
985
|
+
if (entry.type !== "custom") continue;
|
|
986
|
+
if (entry.customType === DECISION_ENTRY_TYPE) {
|
|
987
|
+
const record = parseDecisionRecord(entry.data);
|
|
988
|
+
if (!record) continue;
|
|
989
|
+
lastDecisionByKey.set(record.idempotencyKey, record);
|
|
990
|
+
lastEventKindByKey.set(record.idempotencyKey, "decision");
|
|
991
|
+
} else if (entry.customType === RESTORE_ENTRY_TYPE) {
|
|
992
|
+
const record = parseRestoreRecord(entry.data);
|
|
993
|
+
if (!record) continue;
|
|
994
|
+
lastEventKindByKey.set(record.idempotencyKey, "restore");
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
const activeDecisions: PruneDecisionRecord[] = [];
|
|
999
|
+
const activeIdempotencyKeys = new Set<string>();
|
|
1000
|
+
const restoredKeys = new Set<string>();
|
|
1001
|
+
for (const [key, kind] of lastEventKindByKey) {
|
|
1002
|
+
if (kind === "decision") {
|
|
1003
|
+
const record = lastDecisionByKey.get(key);
|
|
1004
|
+
if (record) {
|
|
1005
|
+
activeDecisions.push(record);
|
|
1006
|
+
activeIdempotencyKeys.add(key);
|
|
1007
|
+
}
|
|
1008
|
+
} else {
|
|
1009
|
+
restoredKeys.add(key);
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
return { activeDecisions, activeIdempotencyKeys, restoredKeys, lastDecisionByKey };
|
|
1046
1014
|
}
|
|
1047
1015
|
|
|
1048
1016
|
// ============================================================================
|
|
@@ -1050,30 +1018,31 @@ export function resolvePruneTombstoneState(entries: MinimalSessionEntry[]): Prun
|
|
|
1050
1018
|
// ============================================================================
|
|
1051
1019
|
|
|
1052
1020
|
export function buildPlaceholderText(reason: string, charsRemoved: number): string {
|
|
1053
|
-
|
|
1021
|
+
return `[pruned by ${EXTENSION_ID}: ${reason} (${charsRemoved} chars removed)]`;
|
|
1054
1022
|
}
|
|
1055
1023
|
|
|
1056
1024
|
function contentTextLength(content: (MinimalTextContent | MinimalImageContent)[]): number {
|
|
1057
|
-
|
|
1025
|
+
return content.reduce((sum, block) => (block.type === "text" ? sum + block.text.length : sum), 0);
|
|
1058
1026
|
}
|
|
1059
1027
|
|
|
1060
1028
|
/** Return true when a decision targets a tool result containing one or more images. */
|
|
1061
1029
|
function isImageBearingToolResultTarget(
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1030
|
+
messages: MinimalMessage[],
|
|
1031
|
+
decision: PruneDecisionRecord,
|
|
1032
|
+
pairIndex?: Map<string, ToolCallPairIndices>,
|
|
1065
1033
|
): boolean {
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1034
|
+
if (decision.kind !== "tool_result_content" || decision.correlation.type !== "toolCallId")
|
|
1035
|
+
return false;
|
|
1036
|
+
const pair = findToolCallPairIndices(messages, decision.correlation.toolCallId, pairIndex);
|
|
1037
|
+
if (pair.resultIndex === undefined) return false;
|
|
1038
|
+
const target = messages[pair.resultIndex];
|
|
1039
|
+
if (target?.role !== "toolResult") return false;
|
|
1040
|
+
return (target as MinimalToolResultMessage).content.some((block) => block.type === "image");
|
|
1072
1041
|
}
|
|
1073
1042
|
|
|
1074
1043
|
export interface ApplyResult {
|
|
1075
|
-
|
|
1076
|
-
|
|
1044
|
+
applied: boolean;
|
|
1045
|
+
charsRemoved: number;
|
|
1077
1046
|
}
|
|
1078
1047
|
|
|
1079
1048
|
/**
|
|
@@ -1083,91 +1052,106 @@ export interface ApplyResult {
|
|
|
1083
1052
|
* Gracefully no-ops when the target is absent (e.g. after compaction).
|
|
1084
1053
|
*/
|
|
1085
1054
|
export function applyPruneDecision(
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1055
|
+
messages: MinimalMessage[],
|
|
1056
|
+
decision: PruneDecisionRecord,
|
|
1057
|
+
pairIndex?: Map<string, ToolCallPairIndices>,
|
|
1089
1058
|
): ApplyResult {
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
}
|
|
1132
|
-
|
|
1133
|
-
function resolveToolNameForPair(
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1059
|
+
if (decision.correlation.type !== "toolCallId") {
|
|
1060
|
+
// v1 only ever targets tool call/result pairs; entryId correlation is
|
|
1061
|
+
// reserved for future strategies and intentionally not applied yet.
|
|
1062
|
+
return { applied: false, charsRemoved: 0 };
|
|
1063
|
+
}
|
|
1064
|
+
const pair = findToolCallPairIndices(messages, decision.correlation.toolCallId, pairIndex);
|
|
1065
|
+
|
|
1066
|
+
if (decision.kind === "tool_result_content") {
|
|
1067
|
+
if (pair.resultIndex === undefined) return { applied: false, charsRemoved: 0 };
|
|
1068
|
+
const resultMessage = messages[pair.resultIndex] as MinimalToolResultMessage;
|
|
1069
|
+
const charsRemoved = contentTextLength(resultMessage.content);
|
|
1070
|
+
const placeholder = decision.placeholder ?? buildPlaceholderText(decision.reason, charsRemoved);
|
|
1071
|
+
messages[pair.resultIndex] = {
|
|
1072
|
+
...resultMessage,
|
|
1073
|
+
content: [{ type: "text", text: placeholder }],
|
|
1074
|
+
};
|
|
1075
|
+
return { applied: true, charsRemoved };
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
if (decision.kind === "tool_call_input") {
|
|
1079
|
+
if (pair.assistantIndex === undefined || pair.toolCallBlockIndex === undefined) {
|
|
1080
|
+
return { applied: false, charsRemoved: 0 };
|
|
1081
|
+
}
|
|
1082
|
+
if (pair.resultIndex === undefined) return { applied: false, charsRemoved: 0 };
|
|
1083
|
+
const resultMessage = messages[pair.resultIndex] as MinimalToolResultMessage;
|
|
1084
|
+
// Only errored-call inputs may ever be redacted (ticket invariant).
|
|
1085
|
+
if (!resultMessage.isError) return { applied: false, charsRemoved: 0 };
|
|
1086
|
+
|
|
1087
|
+
const assistantMessage = messages[pair.assistantIndex] as MinimalAssistantMessage;
|
|
1088
|
+
const block = assistantMessage.content[pair.toolCallBlockIndex] as MinimalToolCallContent;
|
|
1089
|
+
const charsRemoved = JSON.stringify(block.arguments ?? {}).length;
|
|
1090
|
+
const newContent = assistantMessage.content.slice();
|
|
1091
|
+
newContent[pair.toolCallBlockIndex] = {
|
|
1092
|
+
...block,
|
|
1093
|
+
arguments: { pruned: true, reason: decision.reason },
|
|
1094
|
+
};
|
|
1095
|
+
messages[pair.assistantIndex] = { ...assistantMessage, content: newContent };
|
|
1096
|
+
return { applied: true, charsRemoved };
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
return { applied: false, charsRemoved: 0 };
|
|
1100
|
+
}
|
|
1101
|
+
|
|
1102
|
+
function resolveToolNameForPair(
|
|
1103
|
+
pair: ToolCallPairIndices,
|
|
1104
|
+
messages: MinimalMessage[],
|
|
1105
|
+
): string | undefined {
|
|
1106
|
+
if (pair.resultIndex !== undefined) {
|
|
1107
|
+
return (messages[pair.resultIndex] as MinimalToolResultMessage).toolName;
|
|
1108
|
+
}
|
|
1109
|
+
if (pair.assistantIndex !== undefined && pair.toolCallBlockIndex !== undefined) {
|
|
1110
|
+
const assistantMessage = messages[pair.assistantIndex] as MinimalAssistantMessage;
|
|
1111
|
+
const block = assistantMessage.content[pair.toolCallBlockIndex] as
|
|
1112
|
+
| MinimalToolCallContent
|
|
1113
|
+
| undefined;
|
|
1114
|
+
return block?.name;
|
|
1115
|
+
}
|
|
1116
|
+
return undefined;
|
|
1143
1117
|
}
|
|
1144
1118
|
|
|
1145
1119
|
function resolveArgStringsForPair(pair: ToolCallPairIndices, messages: MinimalMessage[]): string[] {
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1120
|
+
if (pair.assistantIndex === undefined || pair.toolCallBlockIndex === undefined) return [];
|
|
1121
|
+
const assistantMessage = messages[pair.assistantIndex] as MinimalAssistantMessage;
|
|
1122
|
+
const block = assistantMessage.content[pair.toolCallBlockIndex] as
|
|
1123
|
+
| MinimalToolCallContent
|
|
1124
|
+
| undefined;
|
|
1125
|
+
return collectArgStringValues(block?.arguments);
|
|
1150
1126
|
}
|
|
1151
1127
|
|
|
1152
1128
|
export function isDecisionProtected(
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1129
|
+
decision: PruneDecisionRecord,
|
|
1130
|
+
messages: MinimalMessage[],
|
|
1131
|
+
protections: PruneProtections,
|
|
1132
|
+
recencyBoundaryIndex: number,
|
|
1133
|
+
pairIndex?: Map<string, ToolCallPairIndices>,
|
|
1158
1134
|
): boolean {
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1135
|
+
if (decision.correlation.type !== "toolCallId") return true; // no safe way to check protections yet
|
|
1136
|
+
const pair = findToolCallPairIndices(messages, decision.correlation.toolCallId, pairIndex);
|
|
1137
|
+
if (
|
|
1138
|
+
pair.resultIndex !== undefined &&
|
|
1139
|
+
isWithinRecencyWindow(pair.resultIndex, recencyBoundaryIndex)
|
|
1140
|
+
)
|
|
1141
|
+
return true;
|
|
1142
|
+
if (
|
|
1143
|
+
pair.assistantIndex !== undefined &&
|
|
1144
|
+
isWithinRecencyWindow(pair.assistantIndex, recencyBoundaryIndex)
|
|
1145
|
+
)
|
|
1146
|
+
return true;
|
|
1163
1147
|
|
|
1164
|
-
|
|
1165
|
-
|
|
1148
|
+
const toolName = resolveToolNameForPair(pair, messages);
|
|
1149
|
+
if (isProtectedToolName(toolName, protections)) return true;
|
|
1166
1150
|
|
|
1167
|
-
|
|
1168
|
-
|
|
1151
|
+
const argStrings = resolveArgStringsForPair(pair, messages);
|
|
1152
|
+
if (argStrings.some((value) => isProtectedPath(value, protections))) return true;
|
|
1169
1153
|
|
|
1170
|
-
|
|
1154
|
+
return false;
|
|
1171
1155
|
}
|
|
1172
1156
|
|
|
1173
1157
|
// ============================================================================
|
|
@@ -1175,10 +1159,10 @@ export function isDecisionProtected(
|
|
|
1175
1159
|
// ============================================================================
|
|
1176
1160
|
|
|
1177
1161
|
export interface DecisionSavingsEstimate {
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1162
|
+
/** Earliest message index this decision changes; used for cache-bust tail sizing. */
|
|
1163
|
+
position: number;
|
|
1164
|
+
/** Estimated tokens removed by this decision, via the shared estimator. */
|
|
1165
|
+
tokensRemoved: number;
|
|
1182
1166
|
}
|
|
1183
1167
|
|
|
1184
1168
|
/**
|
|
@@ -1188,45 +1172,49 @@ export interface DecisionSavingsEstimate {
|
|
|
1188
1172
|
* (mirrors `applyPruneDecision`'s no-op conditions).
|
|
1189
1173
|
*/
|
|
1190
1174
|
export function estimateDecisionSavings(
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1175
|
+
messages: MinimalMessage[],
|
|
1176
|
+
decision: PruneDecisionRecord,
|
|
1177
|
+
estimateTokens: TokenEstimator = estimateTokensForText,
|
|
1178
|
+
pairIndex?: Map<string, ToolCallPairIndices>,
|
|
1195
1179
|
): DecisionSavingsEstimate | undefined {
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1180
|
+
if (decision.correlation.type !== "toolCallId") return undefined;
|
|
1181
|
+
const pair = findToolCallPairIndices(messages, decision.correlation.toolCallId, pairIndex);
|
|
1182
|
+
|
|
1183
|
+
// Apply against a shallow copy of the array (not the message objects) so
|
|
1184
|
+
// the caller's messages/content are never mutated by this preview. Indices
|
|
1185
|
+
// in `pairIndex` (built from `messages`) remain valid against `preview`
|
|
1186
|
+
// since slice() preserves length/order.
|
|
1187
|
+
const preview = messages.slice();
|
|
1188
|
+
const result = applyPruneDecision(preview, decision, pairIndex);
|
|
1189
|
+
if (!result.applied) return undefined;
|
|
1190
|
+
|
|
1191
|
+
if (decision.kind === "tool_result_content") {
|
|
1192
|
+
if (pair.resultIndex === undefined) return undefined;
|
|
1193
|
+
const before = messages[pair.resultIndex] as MinimalToolResultMessage;
|
|
1194
|
+
const after = preview[pair.resultIndex] as MinimalToolResultMessage;
|
|
1195
|
+
const tokensBefore = estimateTokensForContent(before.content);
|
|
1196
|
+
const tokensAfter = estimateTokensForContent(after.content);
|
|
1197
|
+
return { position: pair.resultIndex, tokensRemoved: Math.max(0, tokensBefore - tokensAfter) };
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
if (decision.kind === "tool_call_input") {
|
|
1201
|
+
if (pair.assistantIndex === undefined || pair.toolCallBlockIndex === undefined)
|
|
1202
|
+
return undefined;
|
|
1203
|
+
const beforeBlock = (messages[pair.assistantIndex] as MinimalAssistantMessage).content[
|
|
1204
|
+
pair.toolCallBlockIndex
|
|
1205
|
+
] as MinimalToolCallContent;
|
|
1206
|
+
const afterBlock = (preview[pair.assistantIndex] as MinimalAssistantMessage).content[
|
|
1207
|
+
pair.toolCallBlockIndex
|
|
1208
|
+
] as MinimalToolCallContent;
|
|
1209
|
+
const tokensBefore = estimateTokens(JSON.stringify(beforeBlock.arguments ?? {}));
|
|
1210
|
+
const tokensAfter = estimateTokens(JSON.stringify(afterBlock.arguments ?? {}));
|
|
1211
|
+
return {
|
|
1212
|
+
position: pair.assistantIndex,
|
|
1213
|
+
tokensRemoved: Math.max(0, tokensBefore - tokensAfter),
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
return undefined;
|
|
1230
1218
|
}
|
|
1231
1219
|
|
|
1232
1220
|
/**
|
|
@@ -1241,41 +1229,42 @@ export function estimateDecisionSavings(
|
|
|
1241
1229
|
* (negative savings) is reported as 0 chars saved, not a negative number.
|
|
1242
1230
|
*/
|
|
1243
1231
|
export function estimateDecisionCharsSaved(
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1232
|
+
messages: MinimalMessage[],
|
|
1233
|
+
decision: PruneDecisionRecord,
|
|
1234
|
+
pairIndex?: Map<string, ToolCallPairIndices>,
|
|
1247
1235
|
): number | undefined {
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1236
|
+
if (decision.correlation.type !== "toolCallId") return undefined;
|
|
1237
|
+
const pair = findToolCallPairIndices(messages, decision.correlation.toolCallId, pairIndex);
|
|
1238
|
+
|
|
1239
|
+
// Same non-mutating preview approach as `estimateDecisionSavings`.
|
|
1240
|
+
const preview = messages.slice();
|
|
1241
|
+
const result = applyPruneDecision(preview, decision, pairIndex);
|
|
1242
|
+
if (!result.applied) return undefined;
|
|
1243
|
+
|
|
1244
|
+
if (decision.kind === "tool_result_content") {
|
|
1245
|
+
if (pair.resultIndex === undefined) return undefined;
|
|
1246
|
+
const before = messages[pair.resultIndex] as MinimalToolResultMessage;
|
|
1247
|
+
const after = preview[pair.resultIndex] as MinimalToolResultMessage;
|
|
1248
|
+
const charsBefore = contentTextLength(before.content);
|
|
1249
|
+
const charsAfter = contentTextLength(after.content);
|
|
1250
|
+
return Math.max(0, charsBefore - charsAfter);
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
if (decision.kind === "tool_call_input") {
|
|
1254
|
+
if (pair.assistantIndex === undefined || pair.toolCallBlockIndex === undefined)
|
|
1255
|
+
return undefined;
|
|
1256
|
+
const beforeBlock = (messages[pair.assistantIndex] as MinimalAssistantMessage).content[
|
|
1257
|
+
pair.toolCallBlockIndex
|
|
1258
|
+
] as MinimalToolCallContent;
|
|
1259
|
+
const afterBlock = (preview[pair.assistantIndex] as MinimalAssistantMessage).content[
|
|
1260
|
+
pair.toolCallBlockIndex
|
|
1261
|
+
] as MinimalToolCallContent;
|
|
1262
|
+
const charsBefore = JSON.stringify(beforeBlock.arguments ?? {}).length;
|
|
1263
|
+
const charsAfter = JSON.stringify(afterBlock.arguments ?? {}).length;
|
|
1264
|
+
return Math.max(0, charsBefore - charsAfter);
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
return undefined;
|
|
1279
1268
|
}
|
|
1280
1269
|
|
|
1281
1270
|
/**
|
|
@@ -1293,44 +1282,47 @@ export function estimateDecisionCharsSaved(
|
|
|
1293
1282
|
* calls needed to amortize the one-time cache bust via the recurring saving.
|
|
1294
1283
|
*/
|
|
1295
1284
|
export interface CacheCostModelInput {
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1285
|
+
tailTokensAfterEarliestChange: number;
|
|
1286
|
+
tokensRemoved: number;
|
|
1287
|
+
cachedPriceRatio: number;
|
|
1299
1288
|
}
|
|
1300
1289
|
|
|
1301
1290
|
export interface CacheCostModelResult {
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1291
|
+
penalty: number;
|
|
1292
|
+
recurringSaving: number;
|
|
1293
|
+
/** Infinity when there is a penalty but no recurring saving to amortize it. */
|
|
1294
|
+
breakEvenCalls: number;
|
|
1306
1295
|
}
|
|
1307
1296
|
|
|
1308
1297
|
export function computeCacheCostModel(input: CacheCostModelInput): CacheCostModelResult {
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1298
|
+
const r = Number.isFinite(input.cachedPriceRatio)
|
|
1299
|
+
? Math.min(1, Math.max(0, input.cachedPriceRatio))
|
|
1300
|
+
: DEFAULT_CACHED_PRICE_RATIO;
|
|
1301
|
+
const tailTokens = Math.max(0, input.tailTokensAfterEarliestChange);
|
|
1302
|
+
const tokensRemoved = Math.max(0, input.tokensRemoved);
|
|
1303
|
+
const penalty = (1 - r) * tailTokens;
|
|
1304
|
+
const recurringSaving = r * tokensRemoved;
|
|
1305
|
+
const breakEvenCalls =
|
|
1306
|
+
recurringSaving > 0 ? penalty / recurringSaving : penalty > 0 ? Infinity : 0;
|
|
1307
|
+
return { penalty, recurringSaving, breakEvenCalls };
|
|
1316
1308
|
}
|
|
1317
1309
|
|
|
1318
1310
|
/** A fresh, automatic (non-manual, non-persisted) decision pending the net-benefit gate. */
|
|
1319
1311
|
export interface GateCandidate {
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1312
|
+
decision: PruneDecisionRecord;
|
|
1313
|
+
position: number;
|
|
1314
|
+
tokensRemoved: number;
|
|
1323
1315
|
}
|
|
1324
1316
|
|
|
1325
1317
|
export interface NetBenefitGateResult {
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1318
|
+
accepted: PruneDecisionRecord[];
|
|
1319
|
+
rejected: PruneDecisionRecord[];
|
|
1320
|
+
mode: GateMode;
|
|
1321
|
+
threshold: number;
|
|
1322
|
+
earliestPosition: number | undefined;
|
|
1323
|
+
tailTokensAfterEarliestChange: number;
|
|
1324
|
+
totalTokensRemoved: number;
|
|
1325
|
+
cost: CacheCostModelResult | undefined;
|
|
1334
1326
|
}
|
|
1335
1327
|
|
|
1336
1328
|
/**
|
|
@@ -1345,73 +1337,77 @@ export interface NetBenefitGateResult {
|
|
|
1345
1337
|
* `runDynamicContextPruningPipeline`).
|
|
1346
1338
|
*/
|
|
1347
1339
|
export function evaluateNetBenefitGate(
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1340
|
+
candidates: GateCandidate[],
|
|
1341
|
+
messages: MinimalMessage[],
|
|
1342
|
+
gateConfig: PruneGateConfig,
|
|
1343
|
+
agentState: AgentState = "idle",
|
|
1344
|
+
estimateTokens: TokenEstimator = estimateTokensForText,
|
|
1353
1345
|
): NetBenefitGateResult {
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1346
|
+
const threshold = resolveBreakEvenThreshold(gateConfig, agentState);
|
|
1347
|
+
|
|
1348
|
+
if (candidates.length === 0) {
|
|
1349
|
+
return {
|
|
1350
|
+
accepted: [],
|
|
1351
|
+
rejected: [],
|
|
1352
|
+
mode: gateConfig.mode,
|
|
1353
|
+
threshold,
|
|
1354
|
+
earliestPosition: undefined,
|
|
1355
|
+
tailTokensAfterEarliestChange: 0,
|
|
1356
|
+
totalTokensRemoved: 0,
|
|
1357
|
+
cost: undefined,
|
|
1358
|
+
};
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
if (gateConfig.mode === "off") {
|
|
1362
|
+
// Gate fully bypassed: no cost modelling performed at all.
|
|
1363
|
+
return {
|
|
1364
|
+
accepted: candidates.map((c) => c.decision),
|
|
1365
|
+
rejected: [],
|
|
1366
|
+
mode: "off",
|
|
1367
|
+
threshold,
|
|
1368
|
+
earliestPosition: undefined,
|
|
1369
|
+
tailTokensAfterEarliestChange: 0,
|
|
1370
|
+
totalTokensRemoved: candidates.reduce((sum, c) => sum + c.tokensRemoved, 0),
|
|
1371
|
+
cost: undefined,
|
|
1372
|
+
};
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
const earliestPosition = Math.min(...candidates.map((c) => c.position));
|
|
1376
|
+
const tailTokensAfterEarliestChange = estimateTailTokens(
|
|
1377
|
+
messages,
|
|
1378
|
+
earliestPosition,
|
|
1379
|
+
estimateTokens,
|
|
1380
|
+
);
|
|
1381
|
+
const totalTokensRemoved = candidates.reduce((sum, c) => sum + c.tokensRemoved, 0);
|
|
1382
|
+
const cost = computeCacheCostModel({
|
|
1383
|
+
tailTokensAfterEarliestChange,
|
|
1384
|
+
tokensRemoved: totalTokensRemoved,
|
|
1385
|
+
cachedPriceRatio: gateConfig.cachedPriceRatio,
|
|
1386
|
+
});
|
|
1387
|
+
|
|
1388
|
+
if (gateConfig.mode === "always-apply" || cost.breakEvenCalls <= threshold) {
|
|
1389
|
+
return {
|
|
1390
|
+
accepted: candidates.map((c) => c.decision),
|
|
1391
|
+
rejected: [],
|
|
1392
|
+
mode: gateConfig.mode,
|
|
1393
|
+
threshold,
|
|
1394
|
+
earliestPosition,
|
|
1395
|
+
tailTokensAfterEarliestChange,
|
|
1396
|
+
totalTokensRemoved,
|
|
1397
|
+
cost,
|
|
1398
|
+
};
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
return {
|
|
1402
|
+
accepted: [],
|
|
1403
|
+
rejected: candidates.map((c) => c.decision),
|
|
1404
|
+
mode: gateConfig.mode,
|
|
1405
|
+
threshold,
|
|
1406
|
+
earliestPosition,
|
|
1407
|
+
tailTokensAfterEarliestChange,
|
|
1408
|
+
totalTokensRemoved,
|
|
1409
|
+
cost,
|
|
1410
|
+
};
|
|
1415
1411
|
}
|
|
1416
1412
|
|
|
1417
1413
|
// ============================================================================
|
|
@@ -1420,106 +1416,118 @@ export function evaluateNetBenefitGate(
|
|
|
1420
1416
|
// ============================================================================
|
|
1421
1417
|
|
|
1422
1418
|
export interface PruneStatsRecord {
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1419
|
+
idempotencyKey: string;
|
|
1420
|
+
strategyId: string;
|
|
1421
|
+
tokensRemoved: number;
|
|
1422
|
+
appliedAt: string;
|
|
1427
1423
|
}
|
|
1428
1424
|
|
|
1429
1425
|
export function buildStatsRecord(
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1426
|
+
decision: PruneDecisionRecord,
|
|
1427
|
+
tokensRemoved: number,
|
|
1428
|
+
appliedAt: string = new Date().toISOString(),
|
|
1433
1429
|
): PruneStatsRecord {
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1430
|
+
return {
|
|
1431
|
+
idempotencyKey: decision.idempotencyKey,
|
|
1432
|
+
strategyId: decision.strategyId,
|
|
1433
|
+
tokensRemoved: Math.max(0, tokensRemoved),
|
|
1434
|
+
appliedAt,
|
|
1435
|
+
};
|
|
1440
1436
|
}
|
|
1441
1437
|
|
|
1442
1438
|
/** Parse a persisted stats CustomEntry payload back into a record. Returns undefined if malformed. */
|
|
1443
1439
|
export function parseStatsRecord(data: unknown): PruneStatsRecord | undefined {
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1440
|
+
if (!data || typeof data !== "object") return undefined;
|
|
1441
|
+
const raw = data as Record<string, unknown>;
|
|
1442
|
+
if (typeof raw.idempotencyKey !== "string" || !raw.idempotencyKey) return undefined;
|
|
1443
|
+
if (typeof raw.strategyId !== "string") return undefined;
|
|
1444
|
+
if (typeof raw.tokensRemoved !== "number" || !Number.isFinite(raw.tokensRemoved))
|
|
1445
|
+
return undefined;
|
|
1446
|
+
if (typeof raw.appliedAt !== "string") return undefined;
|
|
1447
|
+
return {
|
|
1448
|
+
idempotencyKey: raw.idempotencyKey,
|
|
1449
|
+
strategyId: raw.strategyId,
|
|
1450
|
+
tokensRemoved: raw.tokensRemoved,
|
|
1451
|
+
appliedAt: raw.appliedAt,
|
|
1452
|
+
};
|
|
1451
1453
|
}
|
|
1452
1454
|
|
|
1453
1455
|
export interface StrategyStats {
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
+
tokensRemoved: number;
|
|
1457
|
+
pruneCount: number;
|
|
1456
1458
|
}
|
|
1457
1459
|
|
|
1458
1460
|
export interface CumulativePruneStats {
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1461
|
+
totalTokensRemoved: number;
|
|
1462
|
+
totalPruneCount: number;
|
|
1463
|
+
byStrategy: Record<string, StrategyStats>;
|
|
1462
1464
|
}
|
|
1463
1465
|
|
|
1464
1466
|
export function emptyCumulativeStats(): CumulativePruneStats {
|
|
1465
|
-
|
|
1467
|
+
return { totalTokensRemoved: 0, totalPruneCount: 0, byStrategy: {} };
|
|
1466
1468
|
}
|
|
1467
1469
|
|
|
1468
1470
|
/** Fold a single stats record into cumulative stats (pure; returns a new object). */
|
|
1469
|
-
export function foldStatsRecord(
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1471
|
+
export function foldStatsRecord(
|
|
1472
|
+
stats: CumulativePruneStats,
|
|
1473
|
+
record: PruneStatsRecord,
|
|
1474
|
+
): CumulativePruneStats {
|
|
1475
|
+
const existing = stats.byStrategy[record.strategyId] ?? { tokensRemoved: 0, pruneCount: 0 };
|
|
1476
|
+
return {
|
|
1477
|
+
totalTokensRemoved: stats.totalTokensRemoved + record.tokensRemoved,
|
|
1478
|
+
totalPruneCount: stats.totalPruneCount + 1,
|
|
1479
|
+
byStrategy: {
|
|
1480
|
+
...stats.byStrategy,
|
|
1481
|
+
[record.strategyId]: {
|
|
1482
|
+
tokensRemoved: existing.tokensRemoved + record.tokensRemoved,
|
|
1483
|
+
pruneCount: existing.pruneCount + 1,
|
|
1484
|
+
},
|
|
1485
|
+
},
|
|
1486
|
+
};
|
|
1482
1487
|
}
|
|
1483
1488
|
|
|
1484
1489
|
export interface RebuiltStatsState {
|
|
1485
|
-
|
|
1486
|
-
|
|
1490
|
+
stats: CumulativePruneStats;
|
|
1491
|
+
seenKeys: Set<string>;
|
|
1487
1492
|
}
|
|
1488
1493
|
|
|
1489
1494
|
/**
|
|
1490
1495
|
* Rebuild cumulative stats from persisted CustomEntry records on a branch.
|
|
1491
|
-
* Idempotent
|
|
1492
|
-
* replayed entries for the same idempotencyKey collapse to a single fold.
|
|
1496
|
+
* Idempotent: duplicate or replayed entries for the same idempotencyKey collapse to a single fold.
|
|
1493
1497
|
*/
|
|
1494
1498
|
export function rebuildStatsStateFromEntries(entries: MinimalSessionEntry[]): RebuiltStatsState {
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1499
|
+
let stats = emptyCumulativeStats();
|
|
1500
|
+
const seenKeys = new Set<string>();
|
|
1501
|
+
for (const entry of entries) {
|
|
1502
|
+
if (entry.type !== "custom" || entry.customType !== STATS_ENTRY_TYPE) continue;
|
|
1503
|
+
const record = parseStatsRecord(entry.data);
|
|
1504
|
+
if (!record) continue;
|
|
1505
|
+
if (seenKeys.has(record.idempotencyKey)) continue;
|
|
1506
|
+
seenKeys.add(record.idempotencyKey);
|
|
1507
|
+
stats = foldStatsRecord(stats, record);
|
|
1508
|
+
}
|
|
1509
|
+
return { stats, seenKeys };
|
|
1506
1510
|
}
|
|
1507
1511
|
|
|
1508
1512
|
/** Live (non-persisted) snapshot of raw vs effective (post-prune) context size for a single call. */
|
|
1509
1513
|
export interface ContextSizeSnapshot {
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1514
|
+
rawTokens: number;
|
|
1515
|
+
effectiveTokens: number;
|
|
1516
|
+
tokensSavedThisCall: number;
|
|
1513
1517
|
}
|
|
1514
1518
|
|
|
1515
1519
|
export function computeContextSizeSnapshot(
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1520
|
+
rawMessages: MinimalMessage[],
|
|
1521
|
+
effectiveMessages: MinimalMessage[],
|
|
1522
|
+
estimateTokens: TokenEstimator = estimateTokensForText,
|
|
1519
1523
|
): ContextSizeSnapshot {
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1524
|
+
const rawTokens = estimateTailTokens(rawMessages, 0, estimateTokens);
|
|
1525
|
+
const effectiveTokens = estimateTailTokens(effectiveMessages, 0, estimateTokens);
|
|
1526
|
+
return {
|
|
1527
|
+
rawTokens,
|
|
1528
|
+
effectiveTokens,
|
|
1529
|
+
tokensSavedThisCall: Math.max(0, rawTokens - effectiveTokens),
|
|
1530
|
+
};
|
|
1523
1531
|
}
|
|
1524
1532
|
|
|
1525
1533
|
// ============================================================================
|
|
@@ -1527,31 +1535,31 @@ export function computeContextSizeSnapshot(
|
|
|
1527
1535
|
// ============================================================================
|
|
1528
1536
|
|
|
1529
1537
|
export interface StrategyProposeInput {
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1538
|
+
messages: MinimalMessage[];
|
|
1539
|
+
protections: PruneProtections;
|
|
1540
|
+
estimateTokens: TokenEstimator;
|
|
1541
|
+
/**
|
|
1542
|
+
* Full config, so strategies can read their own enable flag and any
|
|
1543
|
+
* strategy-specific settings (e.g. error-purge's `minTurnsOld`).
|
|
1544
|
+
* Protection/recency filtering is handled centrally by the pipeline
|
|
1545
|
+
* (see `isDecisionProtected`); strategies do not need to re-check it.
|
|
1546
|
+
*/
|
|
1547
|
+
config: DynamicContextPruningConfig;
|
|
1548
|
+
/**
|
|
1549
|
+
* Session working directory (pe-qs8j), used to normalize relative file
|
|
1550
|
+
* paths (e.g. from read/write/edit tool args) against an absolute base so
|
|
1551
|
+
* the same file referenced via different relative/absolute spellings is
|
|
1552
|
+
* recognized as the same path. Optional/backward-compatible: undefined
|
|
1553
|
+
* when the caller doesn't have a cwd available (e.g. older callers/tests);
|
|
1554
|
+
* strategies that need it should degrade gracefully (best-effort relative
|
|
1555
|
+
* normalization only) rather than throw.
|
|
1556
|
+
*/
|
|
1557
|
+
cwd?: string;
|
|
1550
1558
|
}
|
|
1551
1559
|
|
|
1552
1560
|
export interface PruneStrategy {
|
|
1553
|
-
|
|
1554
|
-
|
|
1561
|
+
id: string;
|
|
1562
|
+
propose(input: StrategyProposeInput): ProposedPrune[];
|
|
1555
1563
|
}
|
|
1556
1564
|
|
|
1557
1565
|
// ----------------------------------------------------------------------
|
|
@@ -1568,66 +1576,74 @@ export interface PruneStrategy {
|
|
|
1568
1576
|
* original key order.
|
|
1569
1577
|
*/
|
|
1570
1578
|
export function canonicalizeArguments(value: unknown): unknown {
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
+
if (Array.isArray(value)) return value.map((item) => canonicalizeArguments(item));
|
|
1580
|
+
if (value && typeof value === "object") {
|
|
1581
|
+
const obj = value as Record<string, unknown>;
|
|
1582
|
+
const sorted: Record<string, unknown> = {};
|
|
1583
|
+
for (const key of Object.keys(obj).sort()) sorted[key] = canonicalizeArguments(obj[key]);
|
|
1584
|
+
return sorted;
|
|
1585
|
+
}
|
|
1586
|
+
return value;
|
|
1579
1587
|
}
|
|
1580
1588
|
|
|
1581
1589
|
/** Canonical JSON string for a tool call's arguments (see `canonicalizeArguments`). */
|
|
1582
1590
|
export function canonicalizeArgumentsJSON(args: unknown): string {
|
|
1583
|
-
|
|
1591
|
+
return JSON.stringify(canonicalizeArguments(args ?? {}));
|
|
1584
1592
|
}
|
|
1585
1593
|
|
|
1586
1594
|
/** Dedup key: identical tool name (case-insensitive) + canonicalized args JSON. */
|
|
1587
1595
|
export function buildDedupeKey(toolName: string, args: unknown): string {
|
|
1588
|
-
|
|
1596
|
+
return `${toolName.trim().toLowerCase()}::${canonicalizeArgumentsJSON(args)}`;
|
|
1589
1597
|
}
|
|
1590
1598
|
|
|
1591
1599
|
/** Exported for /prune picker item building (pe-8re9) and direct unit testing. */
|
|
1592
1600
|
export interface ToolCallOccurrence {
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
}
|
|
1600
|
-
|
|
1601
|
-
/**
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1601
|
+
toolCallId: string;
|
|
1602
|
+
toolName: string;
|
|
1603
|
+
arguments: Record<string, unknown> | undefined;
|
|
1604
|
+
assistantIndex: number;
|
|
1605
|
+
resultIndex: number;
|
|
1606
|
+
isError: boolean;
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
/**
|
|
1610
|
+
* Collect every transcript assistant toolCall block that has a matching
|
|
1611
|
+
* transcript toolResult message, in message order. `nestedCalls` is deliberately
|
|
1612
|
+
* not expanded: Pi keeps only bounded call metadata there and does not record
|
|
1613
|
+
* child results as transcript messages, so expanding it would double-count or
|
|
1614
|
+
* invent prune targets.
|
|
1615
|
+
*/
|
|
1616
|
+
export function collectCompletedToolCallOccurrences(
|
|
1617
|
+
messages: MinimalMessage[],
|
|
1618
|
+
): ToolCallOccurrence[] {
|
|
1619
|
+
const resultIndexById = new Map<string, number>();
|
|
1620
|
+
const isErrorById = new Map<string, boolean>();
|
|
1621
|
+
messages.forEach((message, index) => {
|
|
1622
|
+
if (message.role === "toolResult") {
|
|
1623
|
+
const result = message as MinimalToolResultMessage;
|
|
1624
|
+
resultIndexById.set(result.toolCallId, index);
|
|
1625
|
+
isErrorById.set(result.toolCallId, result.isError);
|
|
1626
|
+
}
|
|
1627
|
+
});
|
|
1628
|
+
|
|
1629
|
+
const occurrences: ToolCallOccurrence[] = [];
|
|
1630
|
+
messages.forEach((message, assistantIndex) => {
|
|
1631
|
+
if (message.role !== "assistant") return;
|
|
1632
|
+
for (const block of (message as MinimalAssistantMessage).content) {
|
|
1633
|
+
if (block.type !== "toolCall") continue;
|
|
1634
|
+
const resultIndex = resultIndexById.get(block.id);
|
|
1635
|
+
if (resultIndex === undefined) continue; // still in flight; nothing to dedupe/purge yet
|
|
1636
|
+
occurrences.push({
|
|
1637
|
+
toolCallId: block.id,
|
|
1638
|
+
toolName: block.name,
|
|
1639
|
+
arguments: block.arguments,
|
|
1640
|
+
assistantIndex,
|
|
1641
|
+
resultIndex,
|
|
1642
|
+
isError: isErrorById.get(block.id) ?? false,
|
|
1643
|
+
});
|
|
1644
|
+
}
|
|
1645
|
+
});
|
|
1646
|
+
return occurrences;
|
|
1631
1647
|
}
|
|
1632
1648
|
|
|
1633
1649
|
// ----------------------------------------------------------------------
|
|
@@ -1636,40 +1652,40 @@ export function collectCompletedToolCallOccurrences(messages: MinimalMessage[]):
|
|
|
1636
1652
|
|
|
1637
1653
|
/** Placeholder text for a duplicate tool result whose content was replaced. */
|
|
1638
1654
|
export function buildDedupePlaceholder(toolName: string, newestToolCallId: string): string {
|
|
1639
|
-
|
|
1655
|
+
return `[pruned by ${EXTENSION_ID}: duplicate ${toolName} call result; see newest occurrence (call ${newestToolCallId})]`;
|
|
1640
1656
|
}
|
|
1641
1657
|
|
|
1642
1658
|
export const dedupeStrategy: PruneStrategy = {
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1659
|
+
id: DEDUPE_STRATEGY_ID,
|
|
1660
|
+
propose(input: StrategyProposeInput): ProposedPrune[] {
|
|
1661
|
+
if (input.config.strategies.dedupe.enabled === false) return [];
|
|
1662
|
+
|
|
1663
|
+
const occurrences = collectCompletedToolCallOccurrences(input.messages);
|
|
1664
|
+
const groups = new Map<string, ToolCallOccurrence[]>();
|
|
1665
|
+
for (const occurrence of occurrences) {
|
|
1666
|
+
const key = buildDedupeKey(occurrence.toolName, occurrence.arguments);
|
|
1667
|
+
const group = groups.get(key);
|
|
1668
|
+
if (group) group.push(occurrence);
|
|
1669
|
+
else groups.set(key, [occurrence]);
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
const proposals: ProposedPrune[] = [];
|
|
1673
|
+
for (const group of groups.values()) {
|
|
1674
|
+
if (group.length < 2) continue;
|
|
1675
|
+
// Occurrences are collected in message order, so the last one is newest.
|
|
1676
|
+
const newest = group[group.length - 1];
|
|
1677
|
+
for (const occurrence of group.slice(0, -1)) {
|
|
1678
|
+
proposals.push({
|
|
1679
|
+
strategyId: DEDUPE_STRATEGY_ID,
|
|
1680
|
+
toolCallId: occurrence.toolCallId,
|
|
1681
|
+
kind: "tool_result_content",
|
|
1682
|
+
reason: `duplicate ${occurrence.toolName} call; see result of call ${newest.toolCallId}`,
|
|
1683
|
+
placeholder: buildDedupePlaceholder(occurrence.toolName, newest.toolCallId),
|
|
1684
|
+
});
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
return proposals;
|
|
1688
|
+
},
|
|
1673
1689
|
};
|
|
1674
1690
|
|
|
1675
1691
|
// ----------------------------------------------------------------------
|
|
@@ -1677,27 +1693,27 @@ export const dedupeStrategy: PruneStrategy = {
|
|
|
1677
1693
|
// ----------------------------------------------------------------------
|
|
1678
1694
|
|
|
1679
1695
|
export const errorPurgeStrategy: PruneStrategy = {
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1696
|
+
id: ERROR_PURGE_STRATEGY_ID,
|
|
1697
|
+
propose(input: StrategyProposeInput): ProposedPrune[] {
|
|
1698
|
+
if (input.config.strategies.errorPurge.enabled === false) return [];
|
|
1699
|
+
const minTurnsOld = input.config.strategies.errorPurge.minTurnsOld;
|
|
1700
|
+
|
|
1701
|
+
const occurrences = collectCompletedToolCallOccurrences(input.messages);
|
|
1702
|
+
const proposals: ProposedPrune[] = [];
|
|
1703
|
+
for (const occurrence of occurrences) {
|
|
1704
|
+
if (!occurrence.isError) continue;
|
|
1705
|
+
const turnsElapsed = computeTurnsElapsedSince(input.messages, occurrence.assistantIndex);
|
|
1706
|
+
// Strictly older than `minTurnsOld` turns (e.g. default 4: eligible at 5+, not at exactly 4).
|
|
1707
|
+
if (turnsElapsed <= minTurnsOld) continue;
|
|
1708
|
+
proposals.push({
|
|
1709
|
+
strategyId: ERROR_PURGE_STRATEGY_ID,
|
|
1710
|
+
toolCallId: occurrence.toolCallId,
|
|
1711
|
+
kind: "tool_call_input",
|
|
1712
|
+
reason: `errored ${occurrence.toolName} call input older than ${minTurnsOld} turns`,
|
|
1713
|
+
});
|
|
1714
|
+
}
|
|
1715
|
+
return proposals;
|
|
1716
|
+
},
|
|
1701
1717
|
};
|
|
1702
1718
|
|
|
1703
1719
|
// ----------------------------------------------------------------------
|
|
@@ -1751,10 +1767,12 @@ type FileOpKind = "read" | "write" | "edit";
|
|
|
1751
1767
|
const FILE_OP_TOOL_NAMES: ReadonlySet<string> = new Set(["read", "write", "edit"]);
|
|
1752
1768
|
|
|
1753
1769
|
/** Extract a file path from tool-call arguments, checking common arg names defensively. */
|
|
1754
|
-
export function extractFileOpPathArg(
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1770
|
+
export function extractFileOpPathArg(
|
|
1771
|
+
args: Record<string, unknown> | undefined,
|
|
1772
|
+
): string | undefined {
|
|
1773
|
+
if (!args || typeof args !== "object") return undefined;
|
|
1774
|
+
const raw = (args.path ?? args.file_path ?? args.filePath) as unknown;
|
|
1775
|
+
return typeof raw === "string" && raw.trim().length > 0 ? raw : undefined;
|
|
1758
1776
|
}
|
|
1759
1777
|
|
|
1760
1778
|
/**
|
|
@@ -1766,32 +1784,39 @@ export function extractFileOpPathArg(args: Record<string, unknown> | undefined):
|
|
|
1766
1784
|
* spellings of the same file will NOT be unified in that case.
|
|
1767
1785
|
*/
|
|
1768
1786
|
export function normalizeFileOpsPath(rawPath: string, cwd: string | undefined): string {
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1787
|
+
const trimmed = rawPath.trim();
|
|
1788
|
+
if (!trimmed) return trimmed;
|
|
1789
|
+
const absoluteOrRelative =
|
|
1790
|
+
cwd && !path.isAbsolute(trimmed) ? path.resolve(cwd, trimmed) : trimmed;
|
|
1791
|
+
return path.normalize(absoluteOrRelative);
|
|
1773
1792
|
}
|
|
1774
1793
|
|
|
1775
1794
|
/** A read's line range. `end === undefined` means the read continues through end-of-file. */
|
|
1776
1795
|
export interface FileReadRange {
|
|
1777
|
-
|
|
1778
|
-
|
|
1796
|
+
start: number;
|
|
1797
|
+
end: number | undefined;
|
|
1779
1798
|
}
|
|
1780
1799
|
|
|
1781
1800
|
/** Compute a read's line range from its `offset`/`limit` args (see pi's `read` tool: 1-indexed offset, line-count limit). */
|
|
1782
1801
|
export function computeFileReadRange(args: Record<string, unknown> | undefined): FileReadRange {
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1802
|
+
const offsetRaw = args?.offset;
|
|
1803
|
+
const limitRaw = args?.limit;
|
|
1804
|
+
const offset =
|
|
1805
|
+
typeof offsetRaw === "number" && Number.isFinite(offsetRaw) && offsetRaw > 0
|
|
1806
|
+
? offsetRaw
|
|
1807
|
+
: undefined;
|
|
1808
|
+
const limit =
|
|
1809
|
+
typeof limitRaw === "number" && Number.isFinite(limitRaw) && limitRaw > 0
|
|
1810
|
+
? limitRaw
|
|
1811
|
+
: undefined;
|
|
1812
|
+
const start = offset ?? 1;
|
|
1813
|
+
const end = limit !== undefined ? start + limit - 1 : undefined;
|
|
1814
|
+
return { start, end };
|
|
1790
1815
|
}
|
|
1791
1816
|
|
|
1792
1817
|
/** True when a range represents a full-file read (starts at line 1, no limit). */
|
|
1793
1818
|
export function isFullFileReadRange(range: FileReadRange): boolean {
|
|
1794
|
-
|
|
1819
|
+
return range.start === 1 && range.end === undefined;
|
|
1795
1820
|
}
|
|
1796
1821
|
|
|
1797
1822
|
/**
|
|
@@ -1808,188 +1833,211 @@ export function isFullFileReadRange(range: FileReadRange): boolean {
|
|
|
1808
1833
|
* - Otherwise (both bounded), `later` covers `earlier` iff later.end >= earlier.end.
|
|
1809
1834
|
*/
|
|
1810
1835
|
export function readRangeCovers(later: FileReadRange, earlier: FileReadRange): boolean {
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1836
|
+
if (later.start > earlier.start) return false;
|
|
1837
|
+
if (later.end === undefined) return true;
|
|
1838
|
+
if (earlier.end === undefined) return false;
|
|
1839
|
+
return later.end >= earlier.end;
|
|
1815
1840
|
}
|
|
1816
1841
|
|
|
1817
1842
|
interface FileOpOccurrence {
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
}
|
|
1826
|
-
|
|
1827
|
-
function collectFileOpOccurrences(
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
}
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1843
|
+
toolCallId: string;
|
|
1844
|
+
kind: FileOpKind;
|
|
1845
|
+
normalizedPath: string;
|
|
1846
|
+
range: FileReadRange | undefined;
|
|
1847
|
+
isError: boolean;
|
|
1848
|
+
resultTextLength: number;
|
|
1849
|
+
assistantIndex: number;
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
function collectFileOpOccurrences(
|
|
1853
|
+
messages: MinimalMessage[],
|
|
1854
|
+
cwd: string | undefined,
|
|
1855
|
+
): FileOpOccurrence[] {
|
|
1856
|
+
const occurrences: FileOpOccurrence[] = [];
|
|
1857
|
+
for (const occurrence of collectCompletedToolCallOccurrences(messages)) {
|
|
1858
|
+
const kind = occurrence.toolName.trim().toLowerCase();
|
|
1859
|
+
if (!FILE_OP_TOOL_NAMES.has(kind)) continue;
|
|
1860
|
+
const rawPath = extractFileOpPathArg(occurrence.arguments);
|
|
1861
|
+
if (!rawPath) continue; // can't safely correlate without a path
|
|
1862
|
+
const normalizedPath = normalizeFileOpsPath(rawPath, cwd);
|
|
1863
|
+
if (!normalizedPath) continue;
|
|
1864
|
+
const resultMessage = messages[occurrence.resultIndex] as MinimalToolResultMessage;
|
|
1865
|
+
occurrences.push({
|
|
1866
|
+
toolCallId: occurrence.toolCallId,
|
|
1867
|
+
kind: kind as FileOpKind,
|
|
1868
|
+
normalizedPath,
|
|
1869
|
+
range: kind === "read" ? computeFileReadRange(occurrence.arguments) : undefined,
|
|
1870
|
+
isError: occurrence.isError,
|
|
1871
|
+
resultTextLength: contentTextLength(resultMessage.content),
|
|
1872
|
+
assistantIndex: occurrence.assistantIndex,
|
|
1873
|
+
});
|
|
1874
|
+
}
|
|
1875
|
+
return occurrences;
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
function buildFileChangedPlaceholder(
|
|
1879
|
+
normalizedPath: string,
|
|
1880
|
+
supersedingKind: FileOpKind,
|
|
1881
|
+
supersedingToolCallId: string,
|
|
1882
|
+
): string {
|
|
1883
|
+
return `[pruned by ${EXTENSION_ID}: file has since changed — ${normalizedPath} was modified by a later ${supersedingKind} (call ${supersedingToolCallId})]`;
|
|
1884
|
+
}
|
|
1885
|
+
|
|
1886
|
+
function buildSupersededReadPlaceholder(
|
|
1887
|
+
normalizedPath: string,
|
|
1888
|
+
supersedingToolCallId: string,
|
|
1889
|
+
): string {
|
|
1890
|
+
return `[pruned by ${EXTENSION_ID}: superseded by newer read of ${normalizedPath} (call ${supersedingToolCallId})]`;
|
|
1891
|
+
}
|
|
1892
|
+
|
|
1893
|
+
function buildSupersededWritePlaceholder(
|
|
1894
|
+
normalizedPath: string,
|
|
1895
|
+
supersedingKind: FileOpKind,
|
|
1896
|
+
supersedingToolCallId: string,
|
|
1897
|
+
): string {
|
|
1898
|
+
return `[pruned by ${EXTENSION_ID}: superseded by newer ${supersedingKind} of ${normalizedPath} (call ${supersedingToolCallId})]`;
|
|
1860
1899
|
}
|
|
1861
1900
|
|
|
1862
1901
|
export const supersededFileOpsStrategy: PruneStrategy = {
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1902
|
+
id: SUPERSEDED_FILE_OPS_STRATEGY_ID,
|
|
1903
|
+
propose(input: StrategyProposeInput): ProposedPrune[] {
|
|
1904
|
+
if (input.config.strategies.supersededFileOps.enabled === false) return [];
|
|
1905
|
+
|
|
1906
|
+
const occurrences = collectFileOpOccurrences(input.messages, input.cwd);
|
|
1907
|
+
const byPath = new Map<string, FileOpOccurrence[]>();
|
|
1908
|
+
for (const occurrence of occurrences) {
|
|
1909
|
+
const group = byPath.get(occurrence.normalizedPath);
|
|
1910
|
+
if (group) group.push(occurrence);
|
|
1911
|
+
else byPath.set(occurrence.normalizedPath, [occurrence]);
|
|
1912
|
+
}
|
|
1913
|
+
|
|
1914
|
+
const proposals: ProposedPrune[] = [];
|
|
1915
|
+
for (const group of byPath.values()) {
|
|
1916
|
+
// `group` is already in message/chronological order because
|
|
1917
|
+
// `collectCompletedToolCallOccurrences` iterates messages in order.
|
|
1918
|
+
//
|
|
1919
|
+
// Single reverse pass (pe-e0zd): instead of `group.slice(i + 1).find(...)`
|
|
1920
|
+
// per index (O(k^2) allocations+scans for a group of size k), walk the
|
|
1921
|
+
// group back-to-front and maintain:
|
|
1922
|
+
// - `nearestLaterSuccessfulWrite`: the nearest (chronologically first)
|
|
1923
|
+
// later successful write/edit, updated as we pass one going backward.
|
|
1924
|
+
// This is exactly equivalent to `later.find(non-read && !isError)`
|
|
1925
|
+
// because `later` preserves chronological order and `.find` returns
|
|
1926
|
+
// the first (nearest) match.
|
|
1927
|
+
// - `nearestCoveringReads`: later reads not yet known-redundant, nearest
|
|
1928
|
+
// first. A read R dominates (renders redundant) any later-tracked read
|
|
1929
|
+
// E once `readRangeCovers(R.range, E.range)` holds, because coverage is
|
|
1930
|
+
// transitive and R is nearer than E — so R already satisfies anything
|
|
1931
|
+
// E would have satisfied, and is checked first anyway. Dominated reads
|
|
1932
|
+
// are dropped on insert, which keeps this list small in the common case
|
|
1933
|
+
// (e.g. monotonically-widening re-reads of the same file) while still
|
|
1934
|
+
// producing byte-for-byte the same first-match result as the original
|
|
1935
|
+
// forward `.find`.
|
|
1936
|
+
let nearestLaterSuccessfulWrite: FileOpOccurrence | undefined;
|
|
1937
|
+
const nearestCoveringReads: FileOpOccurrence[] = [];
|
|
1938
|
+
|
|
1939
|
+
for (let i = group.length - 1; i >= 0; i--) {
|
|
1940
|
+
const occurrence = group[i];
|
|
1941
|
+
// Never supersede the newest operation for this path (rule: "never
|
|
1942
|
+
// supersede the newest op for a path") — but still fold it into the
|
|
1943
|
+
// tracking state below so earlier occurrences can see it as "later".
|
|
1944
|
+
if (i !== group.length - 1) {
|
|
1945
|
+
if (occurrence.kind === "read") {
|
|
1946
|
+
// Rule 3 takes priority: any later successful write/edit makes
|
|
1947
|
+
// this read stale outright, regardless of read-vs-read coverage.
|
|
1948
|
+
if (nearestLaterSuccessfulWrite) {
|
|
1949
|
+
if (occurrence.resultTextLength > 0) {
|
|
1950
|
+
proposals.push({
|
|
1951
|
+
strategyId: SUPERSEDED_FILE_OPS_STRATEGY_ID,
|
|
1952
|
+
toolCallId: occurrence.toolCallId,
|
|
1953
|
+
kind: "tool_result_content",
|
|
1954
|
+
reason: `${occurrence.normalizedPath} has since been modified (see ${nearestLaterSuccessfulWrite.kind} call ${nearestLaterSuccessfulWrite.toolCallId})`,
|
|
1955
|
+
placeholder: buildFileChangedPlaceholder(
|
|
1956
|
+
occurrence.normalizedPath,
|
|
1957
|
+
nearestLaterSuccessfulWrite.kind,
|
|
1958
|
+
nearestLaterSuccessfulWrite.toolCallId,
|
|
1959
|
+
),
|
|
1960
|
+
});
|
|
1961
|
+
}
|
|
1962
|
+
} else if (occurrence.range !== undefined) {
|
|
1963
|
+
// Rules 1+2: superseded by a later read whose range covers this one's.
|
|
1964
|
+
const occurrenceRange = occurrence.range;
|
|
1965
|
+
const laterCoveringRead = nearestCoveringReads.find(
|
|
1966
|
+
(candidate) =>
|
|
1967
|
+
candidate.range !== undefined &&
|
|
1968
|
+
readRangeCovers(candidate.range, occurrenceRange),
|
|
1969
|
+
);
|
|
1970
|
+
if (laterCoveringRead && occurrence.resultTextLength > 0) {
|
|
1971
|
+
proposals.push({
|
|
1972
|
+
strategyId: SUPERSEDED_FILE_OPS_STRATEGY_ID,
|
|
1973
|
+
toolCallId: occurrence.toolCallId,
|
|
1974
|
+
kind: "tool_result_content",
|
|
1975
|
+
reason: `superseded by newer read of ${occurrence.normalizedPath} (call ${laterCoveringRead.toolCallId})`,
|
|
1976
|
+
placeholder: buildSupersededReadPlaceholder(
|
|
1977
|
+
occurrence.normalizedPath,
|
|
1978
|
+
laterCoveringRead.toolCallId,
|
|
1979
|
+
),
|
|
1980
|
+
});
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
} else if (nearestLaterSuccessfulWrite) {
|
|
1984
|
+
// occurrence.kind is "write" or "edit": rule 4 — superseded only by a
|
|
1985
|
+
// LATER *successful* write/edit (an errored later write/edit never
|
|
1986
|
+
// supersedes; reads never supersede writes/edits).
|
|
1987
|
+
if (occurrence.resultTextLength > 0) {
|
|
1988
|
+
proposals.push({
|
|
1989
|
+
strategyId: SUPERSEDED_FILE_OPS_STRATEGY_ID,
|
|
1990
|
+
toolCallId: occurrence.toolCallId,
|
|
1991
|
+
kind: "tool_result_content",
|
|
1992
|
+
reason: `superseded by newer ${nearestLaterSuccessfulWrite.kind} of ${occurrence.normalizedPath} (call ${nearestLaterSuccessfulWrite.toolCallId})`,
|
|
1993
|
+
placeholder: buildSupersededWritePlaceholder(
|
|
1994
|
+
occurrence.normalizedPath,
|
|
1995
|
+
nearestLaterSuccessfulWrite.kind,
|
|
1996
|
+
nearestLaterSuccessfulWrite.toolCallId,
|
|
1997
|
+
),
|
|
1998
|
+
});
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
|
|
2003
|
+
// Fold this occurrence into the tracking state for the next (earlier) iteration.
|
|
2004
|
+
if (occurrence.kind !== "read") {
|
|
2005
|
+
if (!occurrence.isError) nearestLaterSuccessfulWrite = occurrence;
|
|
2006
|
+
} else if (occurrence.range !== undefined) {
|
|
2007
|
+
const occurrenceRange = occurrence.range;
|
|
2008
|
+
for (let j = nearestCoveringReads.length - 1; j >= 0; j--) {
|
|
2009
|
+
const existing = nearestCoveringReads[j];
|
|
2010
|
+
if (existing.range !== undefined && readRangeCovers(occurrenceRange, existing.range)) {
|
|
2011
|
+
nearestCoveringReads.splice(j, 1);
|
|
2012
|
+
}
|
|
2013
|
+
}
|
|
2014
|
+
nearestCoveringReads.unshift(occurrence);
|
|
2015
|
+
}
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
return proposals;
|
|
2019
|
+
},
|
|
1976
2020
|
};
|
|
1977
2021
|
|
|
1978
2022
|
/**
|
|
1979
2023
|
* Strategies PROPOSE prunes; only this pipeline ever APPLIES them.
|
|
1980
2024
|
*/
|
|
1981
|
-
export const STRATEGIES: PruneStrategy[] = [
|
|
2025
|
+
export const STRATEGIES: PruneStrategy[] = [
|
|
2026
|
+
dedupeStrategy,
|
|
2027
|
+
errorPurgeStrategy,
|
|
2028
|
+
supersededFileOpsStrategy,
|
|
2029
|
+
];
|
|
1982
2030
|
|
|
1983
2031
|
function collectProposals(input: StrategyProposeInput): ProposedPrune[] {
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
2032
|
+
const proposals: ProposedPrune[] = [];
|
|
2033
|
+
for (const strategy of STRATEGIES) {
|
|
2034
|
+
try {
|
|
2035
|
+
proposals.push(...strategy.propose(input));
|
|
2036
|
+
} catch {
|
|
2037
|
+
// A misbehaving strategy must never break the pipeline or corrupt context.
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
2040
|
+
return proposals;
|
|
1993
2041
|
}
|
|
1994
2042
|
|
|
1995
2043
|
// ============================================================================
|
|
@@ -1997,42 +2045,38 @@ function collectProposals(input: StrategyProposeInput): ProposedPrune[] {
|
|
|
1997
2045
|
// ============================================================================
|
|
1998
2046
|
|
|
1999
2047
|
export interface PipelineInput {
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
*/
|
|
2021
|
-
agentState?: AgentState;
|
|
2022
|
-
/** Session working directory (pe-qs8j), forwarded to strategies for path normalization. */
|
|
2023
|
-
cwd?: string;
|
|
2048
|
+
messages: MinimalMessage[];
|
|
2049
|
+
config: DynamicContextPruningConfig;
|
|
2050
|
+
persistedDecisions: PruneDecisionRecord[];
|
|
2051
|
+
/** Idempotency keys already known/persisted; used to avoid re-emitting appendEntry calls. */
|
|
2052
|
+
knownIdempotencyKeys: ReadonlySet<string>;
|
|
2053
|
+
/**
|
|
2054
|
+
* Idempotency keys most-recently restored/undone via the /prune picker
|
|
2055
|
+
* (pe-8re9; see `resolvePruneTombstoneState`). Fresh AUTOMATIC proposals
|
|
2056
|
+
* matching one of these keys are dropped so a restored decision does not
|
|
2057
|
+
* silently re-apply on the very next context event. Manual re-prunes
|
|
2058
|
+
* always use a fresh idempotencyKey namespaced by "manual" (see
|
|
2059
|
+
* `buildManualPruneProposal`), so they are unaffected by this filter and
|
|
2060
|
+
* always work regardless of tombstone state. Optional/back-compat: an
|
|
2061
|
+
* absent set behaves as if nothing was ever restored.
|
|
2062
|
+
*/
|
|
2063
|
+
restoredIdempotencyKeys?: ReadonlySet<string>;
|
|
2064
|
+
/** Omitted defaults to idle. The context handler passes the classified state. */
|
|
2065
|
+
agentState?: AgentState;
|
|
2066
|
+
/** Session working directory (pe-qs8j), forwarded to strategies for path normalization. */
|
|
2067
|
+
cwd?: string;
|
|
2024
2068
|
}
|
|
2025
2069
|
|
|
2026
2070
|
export interface PipelineResult {
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2071
|
+
messages: MinimalMessage[];
|
|
2072
|
+
/** Decisions that were applied this call and are not yet persisted; caller should appendEntry these once. */
|
|
2073
|
+
newlyAppliedDecisions: PruneDecisionRecord[];
|
|
2074
|
+
/** Stats records to persist 1:1 alongside `newlyAppliedDecisions` (same idempotencyKey, same order). */
|
|
2075
|
+
newlyAppliedStats: PruneStatsRecord[];
|
|
2076
|
+
/** Live (non-persisted) snapshot of raw vs effective context size for this call. */
|
|
2077
|
+
contextSizeSnapshot: ContextSizeSnapshot;
|
|
2078
|
+
/** Net-benefit gate outcome for this call's fresh automatic proposals (debug/stats consumers). */
|
|
2079
|
+
gate: NetBenefitGateResult;
|
|
2036
2080
|
}
|
|
2037
2081
|
|
|
2038
2082
|
/**
|
|
@@ -2046,212 +2090,224 @@ export interface PipelineResult {
|
|
|
2046
2090
|
* happened) and manual decisions always bypass the gate (user intent wins).
|
|
2047
2091
|
*/
|
|
2048
2092
|
export function runDynamicContextPruningPipeline(input: PipelineInput): PipelineResult {
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2093
|
+
if (!input.config.enabled) {
|
|
2094
|
+
return {
|
|
2095
|
+
messages: input.messages,
|
|
2096
|
+
newlyAppliedDecisions: [],
|
|
2097
|
+
newlyAppliedStats: [],
|
|
2098
|
+
contextSizeSnapshot: computeContextSizeSnapshot(input.messages, input.messages),
|
|
2099
|
+
gate: {
|
|
2100
|
+
accepted: [],
|
|
2101
|
+
rejected: [],
|
|
2102
|
+
mode: input.config.gate.mode,
|
|
2103
|
+
threshold: resolveBreakEvenThreshold(input.config.gate, input.agentState ?? "idle"),
|
|
2104
|
+
earliestPosition: undefined,
|
|
2105
|
+
tailTokensAfterEarliestChange: 0,
|
|
2106
|
+
totalTokensRemoved: 0,
|
|
2107
|
+
cost: undefined,
|
|
2108
|
+
},
|
|
2109
|
+
};
|
|
2110
|
+
}
|
|
2111
|
+
|
|
2112
|
+
const agentState = input.agentState ?? "idle";
|
|
2113
|
+
const messages = input.messages.map((message) => ({ ...message }));
|
|
2114
|
+
const recencyBoundaryIndex = computeRecencyBoundaryIndex(
|
|
2115
|
+
messages,
|
|
2116
|
+
input.config.protections.recentTurns,
|
|
2117
|
+
);
|
|
2118
|
+
// Built once and reused across all protection/savings/apply checks below so
|
|
2119
|
+
// each per-decision lookup is O(1) instead of an O(n) rescan of `messages`
|
|
2120
|
+
// (pe-e0zd). Positions are stable across the apply loop below (and against
|
|
2121
|
+
// `input.messages`) because decisions only ever replace a message's
|
|
2122
|
+
// content at its existing index, never insert/remove/reorder messages.
|
|
2123
|
+
const pairIndex = buildToolCallPairIndex(messages);
|
|
2124
|
+
|
|
2125
|
+
const proposals = collectProposals({
|
|
2126
|
+
messages,
|
|
2127
|
+
protections: input.config.protections,
|
|
2128
|
+
estimateTokens: estimateTokensForText,
|
|
2129
|
+
config: input.config,
|
|
2130
|
+
cwd: input.cwd,
|
|
2131
|
+
});
|
|
2132
|
+
|
|
2133
|
+
const seenKeys = new Set(input.persistedDecisions.map((decision) => decision.idempotencyKey));
|
|
2134
|
+
const restoredKeys = input.restoredIdempotencyKeys ?? new Set<string>();
|
|
2135
|
+
const freshDecisions = proposals
|
|
2136
|
+
.filter((proposal) => !seenKeys.has(buildIdempotencyKey(proposal)))
|
|
2137
|
+
.filter((proposal) => !restoredKeys.has(buildIdempotencyKey(proposal)))
|
|
2138
|
+
.map((proposal) => proposalToDecisionRecord(proposal));
|
|
2139
|
+
|
|
2140
|
+
// De-dup fresh proposals against each other too (multiple strategies could
|
|
2141
|
+
// theoretically propose the same idempotency key in one call).
|
|
2142
|
+
const dedupedFresh: PruneDecisionRecord[] = [];
|
|
2143
|
+
const freshKeys = new Set<string>();
|
|
2144
|
+
for (const decision of freshDecisions) {
|
|
2145
|
+
if (freshKeys.has(decision.idempotencyKey)) continue;
|
|
2146
|
+
freshKeys.add(decision.idempotencyKey);
|
|
2147
|
+
dedupedFresh.push(decision);
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2150
|
+
// Manual decisions (source: "manual") always bypass the net-benefit gate.
|
|
2151
|
+
// Only fresh *automatic* proposals are ever gated; gating happens here
|
|
2152
|
+
// against the pre-mutation `messages` clone, before the apply loop below
|
|
2153
|
+
// mutates anything.
|
|
2154
|
+
const freshManual = dedupedFresh.filter((decision) => decision.source === "manual");
|
|
2155
|
+
const freshAutomaticUnfiltered = dedupedFresh.filter((decision) => decision.source !== "manual");
|
|
2156
|
+
|
|
2157
|
+
// Minimum-size floor (pe-qdzb): drop fresh automatic proposals whose
|
|
2158
|
+
// character savings (original content chars minus placeholder chars) fall
|
|
2159
|
+
// below `thresholds.minCharsSaved`, BEFORE gate evaluation so dropped
|
|
2160
|
+
// candidates never consume batch/earliest-position gate consideration.
|
|
2161
|
+
// Applies to automatic proposals only; manual (`freshManual` above) and
|
|
2162
|
+
// already-persisted decisions (`input.persistedDecisions`, applied later)
|
|
2163
|
+
// bypass this floor entirely, same as the net-benefit gate. Tool-result
|
|
2164
|
+
// candidates containing images also bypass this raw-text floor because
|
|
2165
|
+
// images have no character count; their token savings still go through the
|
|
2166
|
+
// existing net-benefit gate. A floor of 0 (the config default before
|
|
2167
|
+
// pe-c5n9's DEFAULT_MIN_CHARS_SAVED constant was introduced) is a no-op:
|
|
2168
|
+
// every non-negative delta clears it.
|
|
2169
|
+
const minCharsSaved = input.config.thresholds.minCharsSaved;
|
|
2170
|
+
const freshAutomatic =
|
|
2171
|
+
minCharsSaved <= 0
|
|
2172
|
+
? freshAutomaticUnfiltered
|
|
2173
|
+
: freshAutomaticUnfiltered.filter((decision) => {
|
|
2174
|
+
if (isImageBearingToolResultTarget(messages, decision, pairIndex)) return true;
|
|
2175
|
+
const charsSaved = estimateDecisionCharsSaved(messages, decision, pairIndex);
|
|
2176
|
+
return charsSaved !== undefined && charsSaved >= minCharsSaved;
|
|
2177
|
+
});
|
|
2178
|
+
|
|
2179
|
+
// Collapse fresh AUTOMATIC candidates that target the same (kind,
|
|
2180
|
+
// correlation) down to a single decision (pe-j7sb). Two strategies (e.g.
|
|
2181
|
+
// `dedupe` and `superseded-file-ops`) can independently propose a decision
|
|
2182
|
+
// for the exact same tool result; their idempotencyKeys differ (each
|
|
2183
|
+
// embeds its own strategyId) so the self-dedup above does not catch this,
|
|
2184
|
+
// and letting both through would double-count the same tokensRemoved in
|
|
2185
|
+
// the net-benefit gate and double-apply/double-credit in the apply loop
|
|
2186
|
+
// below. The winner is chosen by a DETERMINISTIC strategy priority derived
|
|
2187
|
+
// from `STRATEGIES` array order: earlier entries in `STRATEGIES` win. Only
|
|
2188
|
+
// the winning decision's placeholder/reason is what ends up gated/applied/
|
|
2189
|
+
// persisted. Manual decisions (already filtered out above) and persisted
|
|
2190
|
+
// decisions are never collapsed here — only fresh automatic overlap causes
|
|
2191
|
+
// the double-count this call.
|
|
2192
|
+
const strategyPriority = new Map(STRATEGIES.map((strategy, index) => [strategy.id, index]));
|
|
2193
|
+
const collapsedByTarget = new Map<string, PruneDecisionRecord>();
|
|
2194
|
+
for (const decision of freshAutomatic) {
|
|
2195
|
+
const targetKey = buildDecisionTargetKey(decision);
|
|
2196
|
+
const existing = collapsedByTarget.get(targetKey);
|
|
2197
|
+
if (!existing) {
|
|
2198
|
+
collapsedByTarget.set(targetKey, decision);
|
|
2199
|
+
continue;
|
|
2200
|
+
}
|
|
2201
|
+
const existingPriority = strategyPriority.get(existing.strategyId) ?? Number.POSITIVE_INFINITY;
|
|
2202
|
+
const candidatePriority = strategyPriority.get(decision.strategyId) ?? Number.POSITIVE_INFINITY;
|
|
2203
|
+
if (candidatePriority < existingPriority) collapsedByTarget.set(targetKey, decision);
|
|
2204
|
+
}
|
|
2205
|
+
const collapsedFreshAutomatic = Array.from(collapsedByTarget.values());
|
|
2206
|
+
|
|
2207
|
+
// Fresh automatic candidates must also be excluded from gating (not just
|
|
2208
|
+
// from the apply loop) when their target is already claimed by a decision
|
|
2209
|
+
// that will apply earlier in `allCandidates` — i.e. a persisted decision or
|
|
2210
|
+
// a fresh manual decision. Persisted decisions come from a DIFFERENT prior
|
|
2211
|
+
// call, so a fresh proposal from a different strategy on the same
|
|
2212
|
+
// toolCallId+kind has a distinct idempotencyKey and slips past `seenKeys`
|
|
2213
|
+
// above; likewise a fresh manual decision has `source: "manual"` and is
|
|
2214
|
+
// filtered out of `freshAutomatic` but still claims its target. Without
|
|
2215
|
+
// this exclusion, `evaluateNetBenefitGate` below would double-count that
|
|
2216
|
+
// target's tokensRemoved even though the apply-loop guard further down
|
|
2217
|
+
// correctly skips applying the fresh candidate — corrupting the gate's
|
|
2218
|
+
// net-benefit math (and potentially flipping ACCEPT/REJECT) even though no
|
|
2219
|
+
// double-apply occurs. Only decisions with `correlation.type ===
|
|
2220
|
+
// "toolCallId"` are applyable targets worth tracking here; anything else is
|
|
2221
|
+
// ignored safely (it cannot collide with a toolCallId-keyed target).
|
|
2222
|
+
const claimedTargetKeys = new Set<string>();
|
|
2223
|
+
for (const decision of [...input.persistedDecisions, ...freshManual]) {
|
|
2224
|
+
if (decision.correlation.type !== "toolCallId") continue;
|
|
2225
|
+
claimedTargetKeys.add(buildDecisionTargetKey(decision));
|
|
2226
|
+
}
|
|
2227
|
+
|
|
2228
|
+
const savingsByKey = new Map<string, number>();
|
|
2229
|
+
const gateCandidates: GateCandidate[] = [];
|
|
2230
|
+
for (const decision of collapsedFreshAutomatic) {
|
|
2231
|
+
if (claimedTargetKeys.has(buildDecisionTargetKey(decision))) continue; // already claimed by a persisted/manual decision applying earlier
|
|
2232
|
+
if (
|
|
2233
|
+
isDecisionProtected(
|
|
2234
|
+
decision,
|
|
2235
|
+
messages,
|
|
2236
|
+
input.config.protections,
|
|
2237
|
+
recencyBoundaryIndex,
|
|
2238
|
+
pairIndex,
|
|
2239
|
+
)
|
|
2240
|
+
)
|
|
2241
|
+
continue;
|
|
2242
|
+
const estimate = estimateDecisionSavings(messages, decision, estimateTokensForText, pairIndex);
|
|
2243
|
+
if (!estimate || estimate.tokensRemoved <= 0) continue;
|
|
2244
|
+
savingsByKey.set(decision.idempotencyKey, estimate.tokensRemoved);
|
|
2245
|
+
gateCandidates.push({
|
|
2246
|
+
decision,
|
|
2247
|
+
position: estimate.position,
|
|
2248
|
+
tokensRemoved: estimate.tokensRemoved,
|
|
2249
|
+
});
|
|
2250
|
+
}
|
|
2251
|
+
|
|
2252
|
+
const gate = evaluateNetBenefitGate(
|
|
2253
|
+
gateCandidates,
|
|
2254
|
+
messages,
|
|
2255
|
+
input.config.gate,
|
|
2256
|
+
agentState,
|
|
2257
|
+
estimateTokensForText,
|
|
2258
|
+
);
|
|
2259
|
+
|
|
2260
|
+
const allCandidates = [...input.persistedDecisions, ...freshManual, ...gate.accepted];
|
|
2261
|
+
const newlyAppliedDecisions: PruneDecisionRecord[] = [];
|
|
2262
|
+
const newlyAppliedStats: PruneStatsRecord[] = [];
|
|
2263
|
+
|
|
2264
|
+
// Guards against double-apply/double-count when more than one candidate in
|
|
2265
|
+
// `allCandidates` targets the same (kind, correlation) — e.g. a persisted
|
|
2266
|
+
// decision from a different strategy and a fresh automatic decision (from
|
|
2267
|
+
// a strategy that started proposing overlap only after the persisted one
|
|
2268
|
+
// was written) both targeting the same toolCallId+kind in this SAME call.
|
|
2269
|
+
// `applyPruneDecision` unconditionally re-applies its placeholder even when
|
|
2270
|
+
// the target is already a placeholder and reports `applied: true`, so
|
|
2271
|
+
// without this guard the second candidate would silently overwrite the
|
|
2272
|
+
// first's placeholder AND still be credited full stats/newlyAppliedDecisions
|
|
2273
|
+
// (pe-j7sb). The pre-gate collapse above already prevents this among fresh
|
|
2274
|
+
// automatic candidates; this guard additionally covers persisted-vs-fresh
|
|
2275
|
+
// (and any other) overlap within a single call.
|
|
2276
|
+
const appliedTargetKeys = new Set<string>();
|
|
2277
|
+
for (const decision of allCandidates) {
|
|
2278
|
+
if (
|
|
2279
|
+
isDecisionProtected(
|
|
2280
|
+
decision,
|
|
2281
|
+
messages,
|
|
2282
|
+
input.config.protections,
|
|
2283
|
+
recencyBoundaryIndex,
|
|
2284
|
+
pairIndex,
|
|
2285
|
+
)
|
|
2286
|
+
)
|
|
2287
|
+
continue;
|
|
2288
|
+
const targetKey = buildDecisionTargetKey(decision);
|
|
2289
|
+
if (appliedTargetKeys.has(targetKey)) continue; // already pruned by an earlier candidate this call
|
|
2290
|
+
const result = applyPruneDecision(messages, decision, pairIndex);
|
|
2291
|
+
if (!result.applied) continue; // graceful no-op: target absent/already safe
|
|
2292
|
+
appliedTargetKeys.add(targetKey);
|
|
2293
|
+
if (!input.knownIdempotencyKeys.has(decision.idempotencyKey)) {
|
|
2294
|
+
newlyAppliedDecisions.push(decision);
|
|
2295
|
+
const tokensRemoved =
|
|
2296
|
+
savingsByKey.get(decision.idempotencyKey) ??
|
|
2297
|
+
estimateDecisionSavings(input.messages, decision, estimateTokensForText, pairIndex)
|
|
2298
|
+
?.tokensRemoved ??
|
|
2299
|
+
0;
|
|
2300
|
+
newlyAppliedStats.push(buildStatsRecord(decision, tokensRemoved));
|
|
2301
|
+
}
|
|
2302
|
+
}
|
|
2303
|
+
|
|
2304
|
+
const contextSizeSnapshot = computeContextSizeSnapshot(
|
|
2305
|
+
input.messages,
|
|
2306
|
+
messages,
|
|
2307
|
+
estimateTokensForText,
|
|
2308
|
+
);
|
|
2309
|
+
|
|
2310
|
+
return { messages, newlyAppliedDecisions, newlyAppliedStats, contextSizeSnapshot, gate };
|
|
2255
2311
|
}
|
|
2256
2312
|
|
|
2257
2313
|
// ============================================================================
|
|
@@ -2259,46 +2315,48 @@ export function rebuildDecisionStateFromEntries(entries: MinimalSessionEntry[]):
|
|
|
2259
2315
|
// ============================================================================
|
|
2260
2316
|
|
|
2261
2317
|
function resolveSessionProjection(ctx: ExtensionContext): MinimalSessionProjection | undefined {
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2318
|
+
try {
|
|
2319
|
+
const sessionManager = ctx.sessionManager as unknown as {
|
|
2320
|
+
buildSessionProjection?: () => unknown;
|
|
2321
|
+
};
|
|
2322
|
+
const buildProjection = sessionManager.buildSessionProjection;
|
|
2323
|
+
if (typeof buildProjection !== "function") return undefined;
|
|
2324
|
+
return normalizeSessionProjection(buildProjection.call(sessionManager));
|
|
2325
|
+
} catch {
|
|
2326
|
+
return undefined;
|
|
2327
|
+
}
|
|
2270
2328
|
}
|
|
2271
2329
|
|
|
2272
2330
|
export function sessionEntriesToMessages(
|
|
2273
|
-
|
|
2274
|
-
|
|
2331
|
+
entries: MinimalSessionEntry[],
|
|
2332
|
+
projection?: MinimalSessionProjection,
|
|
2275
2333
|
): MinimalMessage[] {
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2334
|
+
if (projection) return sessionProjectionToMessages(projection);
|
|
2335
|
+
const messages: MinimalMessage[] = [];
|
|
2336
|
+
for (const entry of entries) {
|
|
2337
|
+
if (entry.type === "message" && entry.message) messages.push(entry.message);
|
|
2338
|
+
}
|
|
2339
|
+
return messages;
|
|
2282
2340
|
}
|
|
2283
2341
|
|
|
2284
2342
|
/** Truncated, canonicalized single-line digest of a tool call's arguments, for compact picker display. */
|
|
2285
2343
|
export function buildArgsDigest(args: unknown, maxLength = 80): string {
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2344
|
+
const json = canonicalizeArgumentsJSON(args);
|
|
2345
|
+
if (json.length <= maxLength) return json;
|
|
2346
|
+
return `${json.slice(0, Math.max(0, maxLength - 1))}\u2026`;
|
|
2289
2347
|
}
|
|
2290
2348
|
|
|
2291
2349
|
export type PrunableItemStatus = "active" | "pruned" | "restored";
|
|
2292
2350
|
|
|
2293
2351
|
export interface PrunableItem {
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2352
|
+
toolCallId: string;
|
|
2353
|
+
toolName: string;
|
|
2354
|
+
argsDigest: string;
|
|
2355
|
+
/** Estimated tokens of the ORIGINAL (unpruned) tool result content, i.e. what pruning would save / restoring would bring back. */
|
|
2356
|
+
estimatedTokens: number;
|
|
2357
|
+
status: PrunableItemStatus;
|
|
2358
|
+
/** Set only when status === "pruned": the currently-active decision pruning this result. */
|
|
2359
|
+
activeDecision?: PruneDecisionRecord;
|
|
2302
2360
|
}
|
|
2303
2361
|
|
|
2304
2362
|
/**
|
|
@@ -2310,53 +2368,58 @@ export interface PrunableItem {
|
|
|
2310
2368
|
* usage in the extension wiring).
|
|
2311
2369
|
*/
|
|
2312
2370
|
export function buildPrunableItems(
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2371
|
+
messages: MinimalMessage[],
|
|
2372
|
+
activeByToolCallId: Map<string, PruneDecisionRecord>,
|
|
2373
|
+
restoredToolCallIds: ReadonlySet<string>,
|
|
2374
|
+
_estimateTokens: TokenEstimator = estimateTokensForText,
|
|
2317
2375
|
): PrunableItem[] {
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2376
|
+
const items: PrunableItem[] = [];
|
|
2377
|
+
for (const occurrence of collectCompletedToolCallOccurrences(messages)) {
|
|
2378
|
+
const resultMessage = messages[occurrence.resultIndex] as MinimalToolResultMessage;
|
|
2379
|
+
// Only provider-visible transcript content is economically attributable;
|
|
2380
|
+
// bounded nested-call metadata has no child result content to estimate.
|
|
2381
|
+
const estimatedTokens = estimateTokensForContent(resultMessage.content);
|
|
2382
|
+
const activeDecision = activeByToolCallId.get(occurrence.toolCallId);
|
|
2383
|
+
const status: PrunableItemStatus = activeDecision
|
|
2384
|
+
? "pruned"
|
|
2385
|
+
: restoredToolCallIds.has(occurrence.toolCallId)
|
|
2386
|
+
? "restored"
|
|
2387
|
+
: "active";
|
|
2388
|
+
items.push({
|
|
2389
|
+
toolCallId: occurrence.toolCallId,
|
|
2390
|
+
toolName: occurrence.toolName,
|
|
2391
|
+
argsDigest: buildArgsDigest(occurrence.arguments),
|
|
2392
|
+
estimatedTokens,
|
|
2393
|
+
status,
|
|
2394
|
+
activeDecision,
|
|
2395
|
+
});
|
|
2396
|
+
}
|
|
2397
|
+
return items;
|
|
2338
2398
|
}
|
|
2339
2399
|
|
|
2340
2400
|
/** Build a Map<toolCallId, decision> from active decisions, restricted to tool_result_content kind. First match per toolCallId wins. */
|
|
2341
|
-
export function buildActiveResultDecisionMap(
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2401
|
+
export function buildActiveResultDecisionMap(
|
|
2402
|
+
activeDecisions: PruneDecisionRecord[],
|
|
2403
|
+
): Map<string, PruneDecisionRecord> {
|
|
2404
|
+
const map = new Map<string, PruneDecisionRecord>();
|
|
2405
|
+
for (const decision of activeDecisions) {
|
|
2406
|
+
if (decision.kind !== "tool_result_content") continue;
|
|
2407
|
+
if (decision.correlation.type !== "toolCallId") continue;
|
|
2408
|
+
if (!map.has(decision.correlation.toolCallId))
|
|
2409
|
+
map.set(decision.correlation.toolCallId, decision);
|
|
2410
|
+
}
|
|
2411
|
+
return map;
|
|
2349
2412
|
}
|
|
2350
2413
|
|
|
2351
2414
|
/** Single-line label for a /prune picker option row. */
|
|
2352
2415
|
export function formatPrunableItemOption(item: PrunableItem): string {
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2416
|
+
const statusLabel =
|
|
2417
|
+
item.status === "pruned"
|
|
2418
|
+
? `pruned by ${item.activeDecision?.strategyId ?? "?"}${item.activeDecision?.source === "manual" ? " (manual)" : ""}`
|
|
2419
|
+
: item.status === "restored"
|
|
2420
|
+
? "restored (prunable again)"
|
|
2421
|
+
: "not pruned";
|
|
2422
|
+
return `${item.toolName} ${item.argsDigest} \u2014 ~${item.estimatedTokens} tok \u2014 ${statusLabel}`;
|
|
2360
2423
|
}
|
|
2361
2424
|
|
|
2362
2425
|
/**
|
|
@@ -2367,29 +2430,40 @@ export function formatPrunableItemOption(item: PrunableItem): string {
|
|
|
2367
2430
|
* Do NOT recover a selection via `formatPrunableItemOption(item) === choice` or
|
|
2368
2431
|
* `options.indexOf(choice)` on re-derived labels — always match against these rows' `label`.
|
|
2369
2432
|
*/
|
|
2370
|
-
export function buildPrunableItemPickerOptions(
|
|
2371
|
-
|
|
2433
|
+
export function buildPrunableItemPickerOptions(
|
|
2434
|
+
items: PrunableItem[],
|
|
2435
|
+
): Array<{ label: string; item: PrunableItem }> {
|
|
2436
|
+
return items.map((item, index) => ({
|
|
2437
|
+
label: `${index + 1}) ${formatPrunableItemOption(item)}`,
|
|
2438
|
+
item,
|
|
2439
|
+
}));
|
|
2372
2440
|
}
|
|
2373
2441
|
|
|
2374
2442
|
/** Multi-line detail text for a /prune confirm dialog, describing the item and (when pruning) the predicted cache-bust cost. */
|
|
2375
2443
|
export function formatPrunableItemDetail(item: PrunableItem, cost?: CacheCostModelResult): string {
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2444
|
+
const lines = [
|
|
2445
|
+
`Tool: ${item.toolName}`,
|
|
2446
|
+
`Args: ${item.argsDigest}`,
|
|
2447
|
+
`Estimated tokens: ${item.estimatedTokens}`,
|
|
2448
|
+
];
|
|
2449
|
+
if (item.status === "pruned" && item.activeDecision) {
|
|
2450
|
+
lines.push(
|
|
2451
|
+
`Currently pruned by "${item.activeDecision.strategyId}": ${item.activeDecision.reason}`,
|
|
2452
|
+
);
|
|
2453
|
+
}
|
|
2454
|
+
if (cost) {
|
|
2455
|
+
lines.push(
|
|
2456
|
+
`Predicted cache-bust cost: penalty \u2248${cost.penalty.toFixed(0)} tok, recurring saving \u2248${cost.recurringSaving.toFixed(0)} tok/call, break-even \u2248${Number.isFinite(cost.breakEvenCalls) ? cost.breakEvenCalls.toFixed(1) : "\u221e"} calls.`,
|
|
2457
|
+
"Manual prunes always bypass the net-benefit gate, so this preview is informational only.",
|
|
2458
|
+
);
|
|
2459
|
+
}
|
|
2460
|
+
return lines.join("\n");
|
|
2387
2461
|
}
|
|
2388
2462
|
|
|
2389
2463
|
/** Plain-text report used both by the non-UI /prune fallback and as a debugging aid. */
|
|
2390
2464
|
export function formatPrunableItemsReport(items: PrunableItem[]): string[] {
|
|
2391
|
-
|
|
2392
|
-
|
|
2465
|
+
if (items.length === 0) return ["No prunable tool results found in the current model context."];
|
|
2466
|
+
return items.map((item, index) => `${index + 1}. ${formatPrunableItemOption(item)}`);
|
|
2393
2467
|
}
|
|
2394
2468
|
|
|
2395
2469
|
// ============================================================================
|
|
@@ -2399,415 +2473,498 @@ export function formatPrunableItemsReport(items: PrunableItem[]): string[] {
|
|
|
2399
2473
|
export type StrategyKey = "dedupe" | "errorPurge" | "supersededFileOps";
|
|
2400
2474
|
|
|
2401
2475
|
const STRATEGY_ALIASES: Record<string, StrategyKey> = {
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2476
|
+
dedupe: "dedupe",
|
|
2477
|
+
"error-purge": "errorPurge",
|
|
2478
|
+
errorpurge: "errorPurge",
|
|
2479
|
+
error_purge: "errorPurge",
|
|
2480
|
+
errorPurge: "errorPurge",
|
|
2481
|
+
"superseded-file-ops": "supersededFileOps",
|
|
2482
|
+
supersededfileops: "supersededFileOps",
|
|
2483
|
+
superseded_file_ops: "supersededFileOps",
|
|
2484
|
+
supersededFileOps: "supersededFileOps",
|
|
2411
2485
|
};
|
|
2412
2486
|
|
|
2413
2487
|
const STRATEGY_DISPLAY_NAMES: Record<StrategyKey, string> = {
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2488
|
+
dedupe: "dedupe",
|
|
2489
|
+
errorPurge: "error-purge",
|
|
2490
|
+
supersededFileOps: "superseded-file-ops",
|
|
2417
2491
|
};
|
|
2418
2492
|
|
|
2419
2493
|
export type ContextPruningSubcommand =
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2494
|
+
| { kind: "help" }
|
|
2495
|
+
| { kind: "status" }
|
|
2496
|
+
| { kind: "stats" }
|
|
2497
|
+
| { kind: "enabled"; value: boolean }
|
|
2498
|
+
| { kind: "toggle" }
|
|
2499
|
+
| { kind: "strategy"; strategy: StrategyKey; value: boolean }
|
|
2500
|
+
| { kind: "gate"; mode: GateMode }
|
|
2501
|
+
| { kind: "unknown"; raw: string };
|
|
2428
2502
|
|
|
2429
2503
|
/** Parse `/context-pruning <...>` arguments into a typed subcommand. Pure; never throws. */
|
|
2430
2504
|
export function parseContextPruningArgs(rawArgs: string): ContextPruningSubcommand {
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2505
|
+
const tokens = rawArgs.trim().split(/\s+/).filter(Boolean);
|
|
2506
|
+
if (tokens.length === 0) return { kind: "help" };
|
|
2507
|
+
const [head, ...rest] = tokens;
|
|
2508
|
+
const cmd = head.toLowerCase();
|
|
2509
|
+
|
|
2510
|
+
switch (cmd) {
|
|
2511
|
+
case "help":
|
|
2512
|
+
case "-h":
|
|
2513
|
+
case "--help":
|
|
2514
|
+
return { kind: "help" };
|
|
2515
|
+
case "status":
|
|
2516
|
+
return { kind: "status" };
|
|
2517
|
+
case "stats":
|
|
2518
|
+
return { kind: "stats" };
|
|
2519
|
+
case "on":
|
|
2520
|
+
return { kind: "enabled", value: true };
|
|
2521
|
+
case "off":
|
|
2522
|
+
return { kind: "enabled", value: false };
|
|
2523
|
+
case "toggle":
|
|
2524
|
+
return { kind: "toggle" };
|
|
2525
|
+
case "strategy": {
|
|
2526
|
+
const [nameRaw, valueRaw] = rest;
|
|
2527
|
+
const key = nameRaw
|
|
2528
|
+
? (STRATEGY_ALIASES[nameRaw.toLowerCase()] ?? STRATEGY_ALIASES[nameRaw])
|
|
2529
|
+
: undefined;
|
|
2530
|
+
const value = valueRaw?.toLowerCase();
|
|
2531
|
+
if (!key || (value !== "on" && value !== "off")) return { kind: "unknown", raw: rawArgs };
|
|
2532
|
+
return { kind: "strategy", strategy: key, value: value === "on" };
|
|
2533
|
+
}
|
|
2534
|
+
case "gate": {
|
|
2535
|
+
const modeRaw = rest[0]?.toLowerCase();
|
|
2536
|
+
if (modeRaw === "on" || modeRaw === "off" || modeRaw === "always-apply")
|
|
2537
|
+
return { kind: "gate", mode: modeRaw };
|
|
2538
|
+
return { kind: "unknown", raw: rawArgs };
|
|
2539
|
+
}
|
|
2540
|
+
default:
|
|
2541
|
+
return { kind: "unknown", raw: rawArgs };
|
|
2542
|
+
}
|
|
2466
2543
|
}
|
|
2467
2544
|
|
|
2468
2545
|
export function contextPruningUsage(): string {
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2546
|
+
return [
|
|
2547
|
+
"Usage: /context-pruning <status|stats|on|off|toggle|strategy <name> on|off|gate <on|off|always-apply>>",
|
|
2548
|
+
"",
|
|
2549
|
+
" status Show enabled state, strategies, protections, and context size.",
|
|
2550
|
+
" stats Show cumulative tokens saved per strategy.",
|
|
2551
|
+
" on | off | toggle Enable/disable dynamic context pruning entirely.",
|
|
2552
|
+
" strategy <name> on|off Toggle one strategy: dedupe | error-purge | superseded-file-ops.",
|
|
2553
|
+
" gate on|off|always-apply Control the net-benefit gate: reject/bypass/model-only.",
|
|
2554
|
+
].join("\n");
|
|
2478
2555
|
}
|
|
2479
2556
|
|
|
2480
2557
|
export interface ConfigMutationResult {
|
|
2481
|
-
|
|
2482
|
-
|
|
2558
|
+
config: DynamicContextPruningConfig;
|
|
2559
|
+
message: string;
|
|
2483
2560
|
}
|
|
2484
2561
|
|
|
2485
2562
|
/** Apply a config-mutating subcommand ((enabled|toggle|strategy|gate)) to a config. Pure; returns a new config object. */
|
|
2486
2563
|
export function applyContextPruningConfigMutation(
|
|
2487
|
-
|
|
2488
|
-
|
|
2564
|
+
config: DynamicContextPruningConfig,
|
|
2565
|
+
subcommand: Extract<
|
|
2566
|
+
ContextPruningSubcommand,
|
|
2567
|
+
{ kind: "enabled" | "toggle" | "strategy" | "gate" }
|
|
2568
|
+
>,
|
|
2489
2569
|
): ConfigMutationResult {
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2570
|
+
if (subcommand.kind === "enabled") {
|
|
2571
|
+
return {
|
|
2572
|
+
config: { ...config, enabled: subcommand.value },
|
|
2573
|
+
message: `Dynamic context pruning ${subcommand.value ? "enabled" : "disabled"}.`,
|
|
2574
|
+
};
|
|
2575
|
+
}
|
|
2576
|
+
if (subcommand.kind === "toggle") {
|
|
2577
|
+
const value = !config.enabled;
|
|
2578
|
+
return {
|
|
2579
|
+
config: { ...config, enabled: value },
|
|
2580
|
+
message: `Dynamic context pruning ${value ? "enabled" : "disabled"}.`,
|
|
2581
|
+
};
|
|
2582
|
+
}
|
|
2583
|
+
if (subcommand.kind === "strategy") {
|
|
2584
|
+
const strategies = {
|
|
2585
|
+
...config.strategies,
|
|
2586
|
+
[subcommand.strategy]: {
|
|
2587
|
+
...config.strategies[subcommand.strategy],
|
|
2588
|
+
enabled: subcommand.value,
|
|
2589
|
+
},
|
|
2590
|
+
};
|
|
2591
|
+
return {
|
|
2592
|
+
config: { ...config, strategies },
|
|
2593
|
+
message: `Strategy "${STRATEGY_DISPLAY_NAMES[subcommand.strategy]}" ${subcommand.value ? "enabled" : "disabled"}.`,
|
|
2594
|
+
};
|
|
2595
|
+
}
|
|
2596
|
+
return {
|
|
2597
|
+
config: { ...config, gate: { ...config.gate, mode: subcommand.mode } },
|
|
2598
|
+
message: `Net-benefit gate set to "${subcommand.mode}".`,
|
|
2599
|
+
};
|
|
2514
2600
|
}
|
|
2515
2601
|
|
|
2516
2602
|
export interface StatusReportInput {
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2603
|
+
config: DynamicContextPruningConfig;
|
|
2604
|
+
contextUsage?: { tokens: number | null; contextWindow: number; percent: number | null };
|
|
2605
|
+
lastSnapshot?: ContextSizeSnapshot;
|
|
2520
2606
|
}
|
|
2521
2607
|
|
|
2522
2608
|
/** Human-readable `/context-pruning status` report lines. Pure; testable with fixture inputs. */
|
|
2523
2609
|
export function formatStatusReport(input: StatusReportInput): string[] {
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2610
|
+
const { config, contextUsage, lastSnapshot } = input;
|
|
2611
|
+
const lines: string[] = [];
|
|
2612
|
+
lines.push(`Dynamic context pruning: ${config.enabled ? "ENABLED" : "disabled"}`);
|
|
2613
|
+
lines.push(
|
|
2614
|
+
`Gate: mode=${config.gate.mode} thresholds={idle:${resolveBreakEvenThreshold(config.gate, "idle")}, mid_loop:${resolveBreakEvenThreshold(config.gate, "mid_loop")}} cachedPriceRatio=${config.gate.cachedPriceRatio}`,
|
|
2615
|
+
);
|
|
2616
|
+
lines.push("Strategies:");
|
|
2617
|
+
lines.push(` dedupe: ${config.strategies.dedupe.enabled ? "on" : "off"}`);
|
|
2618
|
+
lines.push(
|
|
2619
|
+
` error-purge: ${config.strategies.errorPurge.enabled ? "on" : "off"} (minTurnsOld=${config.strategies.errorPurge.minTurnsOld})`,
|
|
2620
|
+
);
|
|
2621
|
+
lines.push(
|
|
2622
|
+
` superseded-file-ops: ${config.strategies.supersededFileOps.enabled ? "on" : "off"}`,
|
|
2623
|
+
);
|
|
2624
|
+
lines.push(
|
|
2625
|
+
`Protections: recentTurns=${config.protections.recentTurns}, protectedTools=${config.protections.toolNames.length}, protectedPathGlobs=${config.protections.pathGlobs.length}`,
|
|
2626
|
+
);
|
|
2627
|
+
if (lastSnapshot) {
|
|
2628
|
+
lines.push(
|
|
2629
|
+
`Last call: raw=${lastSnapshot.rawTokens} tok, effective=${lastSnapshot.effectiveTokens} tok, saved=${lastSnapshot.tokensSavedThisCall} tok`,
|
|
2630
|
+
);
|
|
2631
|
+
} else {
|
|
2632
|
+
lines.push(
|
|
2633
|
+
"Last call: no context snapshot yet (nothing has run through the pipeline this session).",
|
|
2634
|
+
);
|
|
2635
|
+
}
|
|
2636
|
+
if (contextUsage) {
|
|
2637
|
+
const percent = contextUsage.percent != null ? ` (${contextUsage.percent.toFixed(1)}%)` : "";
|
|
2638
|
+
lines.push(
|
|
2639
|
+
`Current context usage: ${contextUsage.tokens ?? "unknown"} / ${contextUsage.contextWindow} tokens${percent}`,
|
|
2640
|
+
);
|
|
2641
|
+
}
|
|
2642
|
+
return lines;
|
|
2551
2643
|
}
|
|
2552
2644
|
|
|
2553
2645
|
/** Human-readable `/context-pruning stats` report lines. Pure; testable with fixture inputs. */
|
|
2554
2646
|
export function formatStatsReport(stats: CumulativePruneStats): string[] {
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2647
|
+
const lines: string[] = [
|
|
2648
|
+
`Total tokens saved: ${stats.totalTokensRemoved} across ${stats.totalPruneCount} prune(s).`,
|
|
2649
|
+
];
|
|
2650
|
+
const strategyIds = Object.keys(stats.byStrategy).sort();
|
|
2651
|
+
if (strategyIds.length === 0) {
|
|
2652
|
+
lines.push("No prunes recorded yet.");
|
|
2653
|
+
return lines;
|
|
2654
|
+
}
|
|
2655
|
+
for (const id of strategyIds) {
|
|
2656
|
+
const s = stats.byStrategy[id];
|
|
2657
|
+
lines.push(` ${id}: ${s.tokensRemoved} tok saved across ${s.pruneCount} prune(s)`);
|
|
2658
|
+
}
|
|
2659
|
+
return lines;
|
|
2566
2660
|
}
|
|
2567
2661
|
|
|
2568
2662
|
// ============================================================================
|
|
2569
2663
|
// Extension wiring
|
|
2570
2664
|
// ============================================================================
|
|
2571
2665
|
|
|
2572
|
-
function notify(
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2666
|
+
function notify(
|
|
2667
|
+
ctx: ExtensionContext,
|
|
2668
|
+
message: string,
|
|
2669
|
+
level: "info" | "warning" | "error" = "info",
|
|
2670
|
+
): void {
|
|
2671
|
+
if (ctx.hasUI) {
|
|
2672
|
+
ctx.ui.notify(message, level);
|
|
2673
|
+
return;
|
|
2674
|
+
}
|
|
2675
|
+
console.error(message);
|
|
2578
2676
|
}
|
|
2579
2677
|
|
|
2580
2678
|
export const __testing = {
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2679
|
+
globToRegExp,
|
|
2680
|
+
getConfigPath,
|
|
2681
|
+
readConfig,
|
|
2682
|
+
writeConfig,
|
|
2585
2683
|
};
|
|
2586
2684
|
|
|
2587
2685
|
export default function dynamicContextPruningExtension(pi: ExtensionAPI) {
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2686
|
+
let config: DynamicContextPruningConfig = defaultConfig();
|
|
2687
|
+
let persistedDecisions: PruneDecisionRecord[] = [];
|
|
2688
|
+
let knownIdempotencyKeys = new Set<string>();
|
|
2689
|
+
let restoredIdempotencyKeys = new Set<string>();
|
|
2690
|
+
let lastDecisionByKey = new Map<string, PruneDecisionRecord>();
|
|
2691
|
+
let cumulativeStats: CumulativePruneStats = emptyCumulativeStats();
|
|
2692
|
+
let knownStatsKeys = new Set<string>();
|
|
2693
|
+
let lastContextSizeSnapshot: ContextSizeSnapshot | undefined;
|
|
2694
|
+
|
|
2695
|
+
const rebuildState = (ctx: ExtensionContext) => {
|
|
2696
|
+
const branchEntries = ctx.sessionManager.getBranch() as unknown as MinimalSessionEntry[];
|
|
2697
|
+
const tombstoneState = resolvePruneTombstoneState(branchEntries);
|
|
2698
|
+
persistedDecisions = tombstoneState.activeDecisions;
|
|
2699
|
+
knownIdempotencyKeys = tombstoneState.activeIdempotencyKeys;
|
|
2700
|
+
restoredIdempotencyKeys = tombstoneState.restoredKeys;
|
|
2701
|
+
lastDecisionByKey = tombstoneState.lastDecisionByKey;
|
|
2702
|
+
const rebuiltStats = rebuildStatsStateFromEntries(branchEntries);
|
|
2703
|
+
cumulativeStats = rebuiltStats.stats;
|
|
2704
|
+
knownStatsKeys = rebuiltStats.seenKeys;
|
|
2705
|
+
};
|
|
2706
|
+
|
|
2707
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
2708
|
+
config = await readConfig();
|
|
2709
|
+
rebuildState(ctx);
|
|
2710
|
+
});
|
|
2711
|
+
|
|
2712
|
+
pi.on("session_tree", async (_event, ctx) => {
|
|
2713
|
+
rebuildState(ctx);
|
|
2714
|
+
});
|
|
2715
|
+
|
|
2716
|
+
pi.on("context", async (event, ctx) => {
|
|
2717
|
+
if (!config.enabled) return undefined;
|
|
2718
|
+
|
|
2719
|
+
const result = runDynamicContextPruningPipeline({
|
|
2720
|
+
messages: event.messages as unknown as MinimalMessage[],
|
|
2721
|
+
config,
|
|
2722
|
+
persistedDecisions,
|
|
2723
|
+
knownIdempotencyKeys,
|
|
2724
|
+
restoredIdempotencyKeys,
|
|
2725
|
+
// Real mid-loop/idle agent-state detection (pe-zy4s): derived straight from
|
|
2726
|
+
// the message payload via classifyAgentStateFromMessages -- see its doc
|
|
2727
|
+
// comment for the exact runtime-observable definition.
|
|
2728
|
+
agentState: classifyAgentStateFromMessages(event.messages as unknown as MinimalMessage[]),
|
|
2729
|
+
cwd: ctx.sessionManager.getCwd(),
|
|
2730
|
+
});
|
|
2731
|
+
|
|
2732
|
+
for (const decision of result.newlyAppliedDecisions) {
|
|
2733
|
+
if (knownIdempotencyKeys.has(decision.idempotencyKey)) continue;
|
|
2734
|
+
knownIdempotencyKeys.add(decision.idempotencyKey);
|
|
2735
|
+
persistedDecisions.push(decision);
|
|
2736
|
+
lastDecisionByKey.set(decision.idempotencyKey, decision);
|
|
2737
|
+
try {
|
|
2738
|
+
pi.appendEntry(DECISION_ENTRY_TYPE, decision);
|
|
2739
|
+
} catch {
|
|
2740
|
+
// Persisting a decision for a call that never completes (abort/retry)
|
|
2741
|
+
// is harmless: pruning is recomputed fresh on every call. Swallow.
|
|
2742
|
+
}
|
|
2743
|
+
}
|
|
2744
|
+
|
|
2745
|
+
for (const statRecord of result.newlyAppliedStats) {
|
|
2746
|
+
if (knownStatsKeys.has(statRecord.idempotencyKey)) continue;
|
|
2747
|
+
knownStatsKeys.add(statRecord.idempotencyKey);
|
|
2748
|
+
cumulativeStats = foldStatsRecord(cumulativeStats, statRecord);
|
|
2749
|
+
try {
|
|
2750
|
+
pi.appendEntry(STATS_ENTRY_TYPE, statRecord);
|
|
2751
|
+
} catch {
|
|
2752
|
+
// Same reasoning as decision persistence above: safe to drop on abort/retry.
|
|
2753
|
+
}
|
|
2754
|
+
}
|
|
2755
|
+
|
|
2756
|
+
lastContextSizeSnapshot = result.contextSizeSnapshot;
|
|
2757
|
+
return { messages: result.messages as never };
|
|
2758
|
+
});
|
|
2759
|
+
|
|
2760
|
+
/** Persist + apply a manual prune decision immediately (accounting included), bypassing the net-benefit gate. */
|
|
2761
|
+
const persistManualPrune = (
|
|
2762
|
+
messages: MinimalMessage[],
|
|
2763
|
+
toolCallId: string,
|
|
2764
|
+
): { decision: PruneDecisionRecord; tokensRemoved: number } => {
|
|
2765
|
+
const proposal = buildManualPruneProposal(toolCallId);
|
|
2766
|
+
const decision = proposalToDecisionRecord(proposal);
|
|
2767
|
+
const estimate = estimateDecisionSavings(messages, decision, estimateTokensForText);
|
|
2768
|
+
const tokensRemoved = estimate?.tokensRemoved ?? 0;
|
|
2769
|
+
const statRecord = buildStatsRecord(decision, tokensRemoved);
|
|
2770
|
+
|
|
2771
|
+
pi.appendEntry(DECISION_ENTRY_TYPE, decision);
|
|
2772
|
+
pi.appendEntry(STATS_ENTRY_TYPE, statRecord);
|
|
2773
|
+
|
|
2774
|
+
persistedDecisions = [
|
|
2775
|
+
...persistedDecisions.filter((d) => d.idempotencyKey !== decision.idempotencyKey),
|
|
2776
|
+
decision,
|
|
2777
|
+
];
|
|
2778
|
+
knownIdempotencyKeys.add(decision.idempotencyKey);
|
|
2779
|
+
restoredIdempotencyKeys.delete(decision.idempotencyKey);
|
|
2780
|
+
lastDecisionByKey.set(decision.idempotencyKey, decision);
|
|
2781
|
+
if (!knownStatsKeys.has(statRecord.idempotencyKey)) {
|
|
2782
|
+
knownStatsKeys.add(statRecord.idempotencyKey);
|
|
2783
|
+
cumulativeStats = foldStatsRecord(cumulativeStats, statRecord);
|
|
2784
|
+
}
|
|
2785
|
+
|
|
2786
|
+
return { decision, tokensRemoved };
|
|
2787
|
+
};
|
|
2788
|
+
|
|
2789
|
+
/** Persist a restore (tombstone) for a currently-active decision. Idempotent: safe to call more than once. */
|
|
2790
|
+
const persistRestore = (idempotencyKey: string): void => {
|
|
2791
|
+
const record = buildRestoreRecord(idempotencyKey);
|
|
2792
|
+
pi.appendEntry(RESTORE_ENTRY_TYPE, record);
|
|
2793
|
+
persistedDecisions = persistedDecisions.filter((d) => d.idempotencyKey !== idempotencyKey);
|
|
2794
|
+
knownIdempotencyKeys.delete(idempotencyKey);
|
|
2795
|
+
restoredIdempotencyKeys.add(idempotencyKey);
|
|
2796
|
+
};
|
|
2797
|
+
|
|
2798
|
+
pi.registerCommand("prune", {
|
|
2799
|
+
description:
|
|
2800
|
+
"Review, manually prune, and restore prunable tool results (interactive picker in TUI/RPC)",
|
|
2801
|
+
handler: async (_args, ctx) => {
|
|
2802
|
+
const branchEntries = ctx.sessionManager.getBranch() as unknown as MinimalSessionEntry[];
|
|
2803
|
+
const projection = resolveSessionProjection(ctx);
|
|
2804
|
+
const messages = sessionEntriesToMessages(branchEntries, projection);
|
|
2805
|
+
const activeByToolCallId = buildActiveResultDecisionMap(persistedDecisions);
|
|
2806
|
+
const restoredToolCallIds = new Set(
|
|
2807
|
+
Array.from(lastDecisionByKey.values())
|
|
2808
|
+
.filter(
|
|
2809
|
+
(d) =>
|
|
2810
|
+
d.correlation.type === "toolCallId" && restoredIdempotencyKeys.has(d.idempotencyKey),
|
|
2811
|
+
)
|
|
2812
|
+
.map((d) => (d.correlation as { type: "toolCallId"; toolCallId: string }).toolCallId),
|
|
2813
|
+
);
|
|
2814
|
+
const items = buildPrunableItems(messages, activeByToolCallId, restoredToolCallIds);
|
|
2815
|
+
|
|
2816
|
+
if (!ctx.hasUI) {
|
|
2817
|
+
notify(
|
|
2818
|
+
ctx,
|
|
2819
|
+
[
|
|
2820
|
+
"/prune: no interactive UI available. Prunable tool results in the current model context:",
|
|
2821
|
+
"",
|
|
2822
|
+
...formatPrunableItemsReport(items),
|
|
2823
|
+
].join("\n"),
|
|
2824
|
+
"info",
|
|
2825
|
+
);
|
|
2826
|
+
return;
|
|
2827
|
+
}
|
|
2828
|
+
|
|
2829
|
+
if (items.length === 0) {
|
|
2830
|
+
notify(ctx, "No prunable tool results found in the current model context.", "info");
|
|
2831
|
+
return;
|
|
2832
|
+
}
|
|
2833
|
+
|
|
2834
|
+
const doneLabel = "Done";
|
|
2835
|
+
while (true) {
|
|
2836
|
+
const latestBranch = ctx.sessionManager.getBranch() as unknown as MinimalSessionEntry[];
|
|
2837
|
+
const latestProjection = resolveSessionProjection(ctx);
|
|
2838
|
+
const latestMessages = sessionEntriesToMessages(latestBranch, latestProjection);
|
|
2839
|
+
const latestActive = buildActiveResultDecisionMap(persistedDecisions);
|
|
2840
|
+
const latestRestoredToolCallIds = new Set(
|
|
2841
|
+
Array.from(lastDecisionByKey.values())
|
|
2842
|
+
.filter(
|
|
2843
|
+
(d) =>
|
|
2844
|
+
d.correlation.type === "toolCallId" &&
|
|
2845
|
+
restoredIdempotencyKeys.has(d.idempotencyKey),
|
|
2846
|
+
)
|
|
2847
|
+
.map((d) => (d.correlation as { type: "toolCallId"; toolCallId: string }).toolCallId),
|
|
2848
|
+
);
|
|
2849
|
+
const latestItems = buildPrunableItems(
|
|
2850
|
+
latestMessages,
|
|
2851
|
+
latestActive,
|
|
2852
|
+
latestRestoredToolCallIds,
|
|
2853
|
+
);
|
|
2854
|
+
if (latestItems.length === 0) {
|
|
2855
|
+
notify(ctx, "No prunable tool results found in the current model context.", "info");
|
|
2856
|
+
return;
|
|
2857
|
+
}
|
|
2858
|
+
|
|
2859
|
+
// Labels may collide (e.g. duplicate tool calls with identical args/tokens/status), so each
|
|
2860
|
+
// option is disambiguated with a stable row-number prefix and the selection is recovered by
|
|
2861
|
+
// matching that unique label back to its own row — never by indexOf on re-derived base labels.
|
|
2862
|
+
const pickerOptions = buildPrunableItemPickerOptions(latestItems);
|
|
2863
|
+
const options = pickerOptions.map((option) => option.label);
|
|
2864
|
+
const choice = await ctx.ui.select(
|
|
2865
|
+
"/prune \u2014 select a tool result to prune or restore",
|
|
2866
|
+
[...options, doneLabel],
|
|
2867
|
+
);
|
|
2868
|
+
if (!choice || choice === doneLabel) return;
|
|
2869
|
+
|
|
2870
|
+
const item = pickerOptions.find((option) => option.label === choice)?.item;
|
|
2871
|
+
if (!item) continue;
|
|
2872
|
+
|
|
2873
|
+
if (item.status === "pruned" && item.activeDecision) {
|
|
2874
|
+
const confirmed = await ctx.ui.confirm(
|
|
2875
|
+
"Restore this tool result?",
|
|
2876
|
+
formatPrunableItemDetail(item),
|
|
2877
|
+
);
|
|
2878
|
+
if (confirmed) {
|
|
2879
|
+
persistRestore(item.activeDecision.idempotencyKey);
|
|
2880
|
+
notify(
|
|
2881
|
+
ctx,
|
|
2882
|
+
`Restored ${item.toolName} result; it will show its original content on the next call.`,
|
|
2883
|
+
"info",
|
|
2884
|
+
);
|
|
2885
|
+
}
|
|
2886
|
+
continue;
|
|
2887
|
+
}
|
|
2888
|
+
|
|
2889
|
+
// active or restored: offer a (re-)prune, with a predicted cache-bust cost preview.
|
|
2890
|
+
const previewProposal = buildManualPruneProposal(item.toolCallId);
|
|
2891
|
+
const previewDecision = proposalToDecisionRecord(previewProposal);
|
|
2892
|
+
const estimate = estimateDecisionSavings(
|
|
2893
|
+
latestMessages,
|
|
2894
|
+
previewDecision,
|
|
2895
|
+
estimateTokensForText,
|
|
2896
|
+
);
|
|
2897
|
+
const cost = estimate
|
|
2898
|
+
? computeCacheCostModel({
|
|
2899
|
+
tailTokensAfterEarliestChange: estimateTailTokens(
|
|
2900
|
+
latestMessages,
|
|
2901
|
+
estimate.position,
|
|
2902
|
+
estimateTokensForText,
|
|
2903
|
+
),
|
|
2904
|
+
tokensRemoved: estimate.tokensRemoved,
|
|
2905
|
+
cachedPriceRatio: config.gate.cachedPriceRatio,
|
|
2906
|
+
})
|
|
2907
|
+
: undefined;
|
|
2908
|
+
|
|
2909
|
+
const confirmed = await ctx.ui.confirm(
|
|
2910
|
+
`Prune ${item.toolName} result?`,
|
|
2911
|
+
formatPrunableItemDetail(item, cost),
|
|
2912
|
+
);
|
|
2913
|
+
if (confirmed) {
|
|
2914
|
+
persistManualPrune(latestMessages, item.toolCallId);
|
|
2915
|
+
notify(ctx, `Pruned ${item.toolName} result; it will apply on the next call.`, "info");
|
|
2916
|
+
}
|
|
2917
|
+
}
|
|
2918
|
+
},
|
|
2919
|
+
});
|
|
2920
|
+
|
|
2921
|
+
pi.registerCommand("context-pruning", {
|
|
2922
|
+
description:
|
|
2923
|
+
"Inspect and control dynamic context pruning: status, stats, on/off, strategy, gate",
|
|
2924
|
+
handler: async (args, ctx) => {
|
|
2925
|
+
const subcommand = parseContextPruningArgs(args);
|
|
2926
|
+
|
|
2927
|
+
if (subcommand.kind === "help") {
|
|
2928
|
+
notify(ctx, contextPruningUsage(), "info");
|
|
2929
|
+
return;
|
|
2930
|
+
}
|
|
2931
|
+
if (subcommand.kind === "unknown") {
|
|
2932
|
+
notify(
|
|
2933
|
+
ctx,
|
|
2934
|
+
`Unknown /context-pruning subcommand: "${subcommand.raw}".\n\n${contextPruningUsage()}`,
|
|
2935
|
+
"error",
|
|
2936
|
+
);
|
|
2937
|
+
return;
|
|
2938
|
+
}
|
|
2939
|
+
if (subcommand.kind === "status") {
|
|
2940
|
+
const usage = ctx.getContextUsage();
|
|
2941
|
+
notify(
|
|
2942
|
+
ctx,
|
|
2943
|
+
formatStatusReport({
|
|
2944
|
+
config,
|
|
2945
|
+
contextUsage: usage,
|
|
2946
|
+
lastSnapshot: lastContextSizeSnapshot,
|
|
2947
|
+
}).join("\n"),
|
|
2948
|
+
"info",
|
|
2949
|
+
);
|
|
2950
|
+
return;
|
|
2951
|
+
}
|
|
2952
|
+
if (subcommand.kind === "stats") {
|
|
2953
|
+
notify(ctx, formatStatsReport(cumulativeStats).join("\n"), "info");
|
|
2954
|
+
return;
|
|
2955
|
+
}
|
|
2956
|
+
|
|
2957
|
+
const { config: nextConfig, message } = applyContextPruningConfigMutation(config, subcommand);
|
|
2958
|
+
config = nextConfig;
|
|
2959
|
+
try {
|
|
2960
|
+
await writeConfig(config);
|
|
2961
|
+
} catch {
|
|
2962
|
+
// Config is still updated in-memory for this session even if the write fails
|
|
2963
|
+
// (e.g. read-only filesystem); surface the change but note persistence failed.
|
|
2964
|
+
notify(ctx, `${message} (warning: failed to persist config to disk)`, "warning");
|
|
2965
|
+
return;
|
|
2966
|
+
}
|
|
2967
|
+
notify(ctx, message, "info");
|
|
2968
|
+
},
|
|
2969
|
+
});
|
|
2813
2970
|
}
|