@diegopetrucci/pi-extensions 0.1.74 → 0.1.76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +2 -0
- package/README.md +9 -4
- package/extensions/agent-workflow-audit/index.ts +1398 -1009
- package/extensions/agent-workflow-audit/package.json +1 -1
- package/extensions/annotate-git-diff/README.md +3 -1
- package/extensions/annotate-git-diff/clipboard.ts +109 -100
- package/extensions/annotate-git-diff/git.ts +853 -767
- package/extensions/annotate-git-diff/glimpseui.d.ts +87 -78
- package/extensions/annotate-git-diff/index.ts +712 -671
- package/extensions/annotate-git-diff/package.json +1 -1
- package/extensions/annotate-git-diff/prompt.ts +45 -45
- package/extensions/annotate-git-diff/quiet-glimpse.ts +161 -155
- package/extensions/annotate-git-diff/review-server.ts +337 -311
- package/extensions/annotate-git-diff/types.ts +132 -132
- package/extensions/annotate-git-diff/ui.ts +27 -27
- package/extensions/annotate-git-diff/watch.ts +74 -74
- package/extensions/annotate-git-diff/web/app.js +1947 -1884
- package/extensions/annotate-git-diff/web/index.html +5 -6
- package/extensions/annotate-last-message/README.md +1 -1
- package/extensions/annotate-last-message/glimpseui.d.ts +87 -78
- package/extensions/annotate-last-message/index.ts +210 -206
- package/extensions/annotate-last-message/package.json +1 -1
- package/extensions/annotate-last-message/prompt.ts +87 -66
- package/extensions/annotate-last-message/quiet-glimpse.ts +161 -155
- package/extensions/annotate-last-message/session.ts +94 -89
- package/extensions/annotate-last-message/theme.ts +199 -193
- package/extensions/annotate-last-message/types.ts +23 -23
- package/extensions/annotate-last-message/ui.ts +27 -25
- package/extensions/annotate-last-message/web/app.js +4 -7
- package/extensions/annotate-last-message/web/md-renderer.js +1 -1
- package/extensions/brrr/README.md +1 -1
- package/extensions/brrr/index.ts +213 -191
- package/extensions/brrr/package.json +1 -1
- package/extensions/claude-fast/README.md +5 -79
- package/extensions/claude-fast/index.ts +31 -289
- package/extensions/claude-fast/package.json +2 -3
- package/extensions/code-reviewer/README.md +1 -1
- package/extensions/code-reviewer/index.ts +1598 -1350
- package/extensions/code-reviewer/package.json +1 -1
- package/extensions/confirm-destructive/README.md +1 -1
- package/extensions/confirm-destructive/index.ts +55 -51
- package/extensions/confirm-destructive/package.json +1 -1
- package/extensions/context-cap/index.ts +312 -304
- package/extensions/context-cap/package.json +1 -1
- package/extensions/context-inspector/index.ts +1211 -1064
- package/extensions/context-inspector/package.json +1 -1
- package/extensions/contrarian/index.ts +1854 -1735
- package/extensions/contrarian/package.json +1 -1
- package/extensions/dirty-repo-guard/index.ts +41 -41
- package/extensions/dynamic-context-pruning/README.md +6 -38
- package/extensions/dynamic-context-pruning/index.ts +2124 -1980
- package/extensions/dynamic-context-pruning/package.json +1 -1
- package/extensions/fast/README.md +50 -12
- package/extensions/fast/index.ts +513 -317
- package/extensions/fast/package.json +1 -1
- package/extensions/git-footer/README.md +1 -1
- package/extensions/git-footer/index.ts +458 -411
- package/extensions/git-footer/package.json +2 -3
- package/extensions/gnosis/index.ts +180 -130
- package/extensions/gnosis/package.json +1 -1
- package/extensions/inline-bash/index.ts +88 -88
- package/extensions/inline-bash/package.json +1 -1
- package/extensions/librarian/index.ts +1669 -1419
- package/extensions/librarian/package.json +1 -1
- package/extensions/minimal-footer/index.ts +994 -1012
- package/extensions/minimal-footer/openai-usage.ts +195 -190
- package/extensions/notify/index.ts +222 -230
- package/extensions/notify/package.json +1 -1
- package/extensions/openai-fast/README.md +5 -73
- package/extensions/openai-fast/index.ts +32 -272
- package/extensions/openai-fast/package.json +2 -3
- package/extensions/oracle/index.ts +1731 -1625
- package/extensions/oracle/package.json +1 -1
- package/extensions/permission-gate/index.ts +658 -608
- package/extensions/permission-gate/package.json +1 -1
- package/extensions/permission-gate/powershell-safety.ts +840 -793
- package/extensions/project-mcp-json/.pi-fleet-tested-version +1 -0
- package/extensions/project-mcp-json/README.md +199 -0
- package/extensions/project-mcp-json/config.ts +564 -0
- package/extensions/project-mcp-json/index.ts +119 -0
- package/extensions/project-mcp-json/package.json +38 -0
- package/extensions/quiet-tools/index.ts +282 -264
- package/extensions/review/README.md +2 -2
- package/extensions/review/index.ts +1745 -1872
- package/extensions/review/package.json +1 -1
- package/extensions/todo/index.ts +284 -265
- package/extensions/triage-comments/README.md +2 -2
- package/extensions/triage-comments/index.ts +2294 -1882
- package/extensions/triage-comments/package.json +1 -1
- package/package.json +6 -3
- package/assets/social-preview.png +0 -0
- package/extensions/claude-fast/claude-fast.example.json +0 -4
- package/extensions/openai-fast/openai-fast.example.json +0 -4
|
@@ -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,123 +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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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;
|
|
59
63
|
}
|
|
60
64
|
|
|
61
65
|
export interface MinimalOtherMessage {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
role: Exclude<string, "user" | "assistant" | "toolResult">;
|
|
67
|
+
timestamp?: number;
|
|
68
|
+
[key: string]: unknown;
|
|
65
69
|
}
|
|
66
70
|
|
|
67
|
-
export type MinimalMessage =
|
|
71
|
+
export type MinimalMessage =
|
|
72
|
+
| MinimalUserMessage
|
|
73
|
+
| MinimalAssistantMessage
|
|
74
|
+
| MinimalToolResultMessage
|
|
75
|
+
| MinimalOtherMessage;
|
|
68
76
|
|
|
69
77
|
export interface MinimalSessionEntry {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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;
|
|
78
86
|
}
|
|
79
87
|
|
|
80
88
|
export interface MinimalProjectedSessionEntry {
|
|
81
|
-
|
|
82
|
-
|
|
89
|
+
sourceEntry: MinimalSessionEntry;
|
|
90
|
+
messages: MinimalMessage[];
|
|
83
91
|
}
|
|
84
92
|
|
|
85
93
|
export interface MinimalSessionProjection {
|
|
86
|
-
|
|
87
|
-
|
|
94
|
+
entries: MinimalProjectedSessionEntry[];
|
|
95
|
+
messages?: MinimalMessage[];
|
|
88
96
|
}
|
|
89
97
|
|
|
90
98
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
91
|
-
|
|
99
|
+
return typeof value === "object" && value !== null;
|
|
92
100
|
}
|
|
93
101
|
|
|
94
102
|
function isMinimalMessage(value: unknown): value is MinimalMessage {
|
|
95
|
-
|
|
103
|
+
return isRecord(value) && typeof value.role === "string" && value.role.length > 0;
|
|
96
104
|
}
|
|
97
105
|
|
|
98
106
|
function isProjectionSourceEntry(value: unknown): value is MinimalSessionEntry {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
+
);
|
|
104
114
|
}
|
|
105
115
|
|
|
106
116
|
export function normalizeSessionProjection(value: unknown): MinimalSessionProjection | undefined {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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);
|
|
129
150
|
}
|
|
130
151
|
|
|
131
152
|
// ============================================================================
|
|
@@ -185,30 +206,30 @@ const DEFAULT_MID_LOOP_BREAK_EVEN_THRESHOLD = 1;
|
|
|
185
206
|
|
|
186
207
|
/** Tool names never pruned by default: orchestration/subagent-style tools and todo-like state. */
|
|
187
208
|
const DEFAULT_PROTECTED_TOOL_NAMES = [
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
209
|
+
"todo",
|
|
210
|
+
"task",
|
|
211
|
+
"subagent",
|
|
212
|
+
"agent",
|
|
213
|
+
"skill",
|
|
214
|
+
"oracle",
|
|
215
|
+
"librarian",
|
|
216
|
+
"contrarian",
|
|
217
|
+
"code_reviewer",
|
|
218
|
+
"gnosis",
|
|
219
|
+
"triage_comments",
|
|
199
220
|
];
|
|
200
221
|
|
|
201
222
|
/** File globs never pruned by default: secrets/credential-shaped paths. */
|
|
202
223
|
const DEFAULT_PROTECTED_PATH_GLOBS = [
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
224
|
+
"**/.env",
|
|
225
|
+
"**/.env.*",
|
|
226
|
+
"**/*.pem",
|
|
227
|
+
"**/*.key",
|
|
228
|
+
"**/id_rsa*",
|
|
229
|
+
"**/id_ed25519*",
|
|
230
|
+
"**/secrets/**",
|
|
231
|
+
"**/*.p12",
|
|
232
|
+
"**/*.pfx",
|
|
212
233
|
];
|
|
213
234
|
|
|
214
235
|
const DEFAULT_MIN_CHARS_SAVED = 200;
|
|
@@ -224,251 +245,243 @@ const ERROR_PURGE_STRATEGY_ID = "error-purge";
|
|
|
224
245
|
// ============================================================================
|
|
225
246
|
|
|
226
247
|
export interface PruneProtections {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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;
|
|
233
254
|
}
|
|
234
255
|
|
|
235
256
|
export interface PruneThresholds {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
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;
|
|
249
270
|
}
|
|
250
271
|
|
|
251
272
|
/** Per-strategy toggles/config (pe-u8gd, pe-qs8j). Each strategy can be disabled independently. */
|
|
252
273
|
export interface StrategiesConfig {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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 };
|
|
261
282
|
}
|
|
262
283
|
|
|
263
284
|
/** Net-benefit gate operating mode (pe-s2ho). */
|
|
264
285
|
export type GateMode = "on" | "off" | "always-apply";
|
|
265
286
|
|
|
266
287
|
/**
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
* the same threshold today.
|
|
271
|
-
*
|
|
272
|
-
* pe-c5n9 evidence (turn-END definition, since superseded -- see below):
|
|
273
|
-
* the representative-corpus benchmark showed mid_loop candidates carrying
|
|
274
|
-
* ~all realized net benefit while idle candidates carried ~none. That would
|
|
275
|
-
* have argued for a stricter idle default. However, the only live runtime
|
|
276
|
-
* caller (the `context` event handler below) did not yet perform real
|
|
277
|
-
* mid-loop/idle detection at the time, so no default change was made.
|
|
278
|
-
*
|
|
279
|
-
* pe-zy4s update (2026-07-08): real runtime mid_loop/idle detection is now
|
|
280
|
-
* wired through the `context` event handler (`classifyAgentStateFromMessages`),
|
|
281
|
-
* using a turn-START definition (idle = this is the first LLM call of a turn;
|
|
282
|
-
* mid_loop = the agent already made a call or received a tool result this
|
|
283
|
-
* turn) -- see that function's doc comment for the exact runtime-observable
|
|
284
|
-
* semantics. The benchmark's candidate labeling was aligned to this SAME
|
|
285
|
-
* definition (retiring the old turn-END definition above, which was never
|
|
286
|
-
* runtime-observable), and the state-split was re-derived on the
|
|
287
|
-
* representative corpus (~/.the-last-harness/agent/sessions, 460 candidates)
|
|
288
|
-
* under it.
|
|
289
|
-
*
|
|
290
|
-
* The result REVERSES the earlier (turn-END) finding: at r=0.1 the
|
|
291
|
-
* turn-START "idle" population (calls made at the start of a turn) carries
|
|
292
|
-
* essentially ALL the realized net benefit (idle-optimal T=22, total realized
|
|
293
|
-
* net benefit ~20,594 token-units), while "mid_loop" carries essentially NONE
|
|
294
|
-
* (mid_loop-optimal T=1, total realized net benefit ~0.0 on this corpus). This is
|
|
295
|
-
* the OPPOSITE of what the pe-c5n9 evidence (measured under the old,
|
|
296
|
-
* non-runtime-observable turn-END definition) suggested.
|
|
297
|
-
*
|
|
298
|
-
* Per the pe-zy4s decision rule (stricter idle default only if idle itself is
|
|
299
|
-
* shown to be ~worthless): idle is NOT worthless here -- it is the ONLY state
|
|
300
|
-
* carrying value at r=0.1 -- so the idle default stays at
|
|
301
|
-
* DEFAULT_BREAK_EVEN_THRESHOLD. The mid_loop default, by the same benchmark,
|
|
302
|
-
* is now DEFAULT_MID_LOOP_BREAK_EVEN_THRESHOLD (=1): the measured candidates
|
|
303
|
-
* did not justify a more permissive threshold at r=0.1. Mid_loop pruning can
|
|
304
|
-
* still amortize over enough subsequent calls; state alone does not establish
|
|
305
|
-
* cache warmth or profitability. Both defaults remain config-overridable via
|
|
306
|
-
* 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.
|
|
307
291
|
*/
|
|
308
292
|
export type AgentState = "idle" | "mid_loop";
|
|
309
293
|
|
|
310
294
|
export interface PruneGateConfig {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
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 };
|
|
326
310
|
}
|
|
327
311
|
|
|
328
312
|
export interface DynamicContextPruningConfig {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
313
|
+
enabled: boolean;
|
|
314
|
+
protections: PruneProtections;
|
|
315
|
+
thresholds: PruneThresholds;
|
|
316
|
+
gate: PruneGateConfig;
|
|
317
|
+
strategies: StrategiesConfig;
|
|
334
318
|
}
|
|
335
319
|
|
|
336
320
|
export function defaultConfig(): DynamicContextPruningConfig {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
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
|
+
};
|
|
359
346
|
}
|
|
360
347
|
|
|
361
348
|
function asStringArray(value: unknown, fallback: string[]): string[] {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
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;
|
|
365
352
|
}
|
|
366
353
|
|
|
367
354
|
function asNonNegativeInt(value: unknown, fallback: number): number {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
355
|
+
const parsed = typeof value === "number" ? value : Number(value);
|
|
356
|
+
if (!Number.isFinite(parsed) || parsed < 0) return fallback;
|
|
357
|
+
return Math.floor(parsed);
|
|
371
358
|
}
|
|
372
359
|
|
|
373
360
|
function asNonNegativeNumber(value: unknown, fallback: number): number {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
361
|
+
const parsed = typeof value === "number" ? value : Number(value);
|
|
362
|
+
if (!Number.isFinite(parsed) || parsed < 0) return fallback;
|
|
363
|
+
return parsed;
|
|
377
364
|
}
|
|
378
365
|
|
|
379
366
|
function asRatio(value: unknown, fallback: number): number {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
367
|
+
const parsed = typeof value === "number" ? value : Number(value);
|
|
368
|
+
if (!Number.isFinite(parsed) || parsed < 0 || parsed > 1) return fallback;
|
|
369
|
+
return parsed;
|
|
383
370
|
}
|
|
384
371
|
|
|
385
372
|
function asGateMode(value: unknown, fallback: GateMode): GateMode {
|
|
386
|
-
|
|
373
|
+
return value === "on" || value === "off" || value === "always-apply" ? value : fallback;
|
|
387
374
|
}
|
|
388
375
|
|
|
389
376
|
function asBoolean(value: unknown, fallback: boolean): boolean {
|
|
390
|
-
|
|
377
|
+
return typeof value === "boolean" ? value : fallback;
|
|
391
378
|
}
|
|
392
379
|
|
|
393
380
|
/** Merge a partially-shaped parsed config (e.g. from disk) onto defaults. Never throws. */
|
|
394
381
|
export function normalizeConfig(parsed: unknown): DynamicContextPruningConfig {
|
|
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
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
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
|
+
};
|
|
452
465
|
}
|
|
453
466
|
|
|
454
467
|
function getConfigPath(): string {
|
|
455
|
-
|
|
468
|
+
return path.join(getAgentDir(), "extensions", CONFIG_FILE_NAME);
|
|
456
469
|
}
|
|
457
470
|
|
|
458
471
|
async function readConfig(): Promise<DynamicContextPruningConfig> {
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
472
|
+
try {
|
|
473
|
+
const raw = await fs.readFile(getConfigPath(), "utf8");
|
|
474
|
+
return normalizeConfig(JSON.parse(raw));
|
|
475
|
+
} catch {
|
|
476
|
+
return defaultConfig();
|
|
477
|
+
}
|
|
465
478
|
}
|
|
466
479
|
|
|
467
480
|
async function writeConfig(config: DynamicContextPruningConfig): Promise<void> {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
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");
|
|
472
485
|
}
|
|
473
486
|
|
|
474
487
|
// ============================================================================
|
|
@@ -477,19 +490,21 @@ async function writeConfig(config: DynamicContextPruningConfig): Promise<void> {
|
|
|
477
490
|
// ============================================================================
|
|
478
491
|
|
|
479
492
|
export function estimateTokensForText(text: string): number {
|
|
480
|
-
|
|
481
|
-
|
|
493
|
+
if (!text) return 0;
|
|
494
|
+
return Math.max(0, Math.ceil(text.length / 4));
|
|
482
495
|
}
|
|
483
496
|
|
|
484
|
-
export function estimateTokensForContent(
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
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;
|
|
493
508
|
}
|
|
494
509
|
|
|
495
510
|
export type TokenEstimator = (text: string) => number;
|
|
@@ -520,37 +535,45 @@ export type TokenEstimator = (text: string) => number;
|
|
|
520
535
|
* call is a turn's last one, so that definition was never runtime-observable.)
|
|
521
536
|
*/
|
|
522
537
|
export function classifyAgentStateFromMessages(messages: MinimalMessage[]): AgentState {
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
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";
|
|
530
545
|
}
|
|
531
546
|
|
|
532
547
|
/** Resolve which break-even threshold applies for a given agent state (pe-s2ho state-conditioning hook). */
|
|
533
|
-
export function resolveBreakEvenThreshold(
|
|
534
|
-
|
|
535
|
-
|
|
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;
|
|
536
556
|
}
|
|
537
557
|
|
|
538
558
|
/** Estimate the token cost of a single message (all content kinds), for tail/context-size accounting. */
|
|
539
559
|
function estimateMessageTokens(message: MinimalMessage, estimateTokens: TokenEstimator): number {
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
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;
|
|
554
577
|
}
|
|
555
578
|
|
|
556
579
|
/**
|
|
@@ -560,16 +583,16 @@ function estimateMessageTokens(message: MinimalMessage, estimateTokens: TokenEst
|
|
|
560
583
|
* `fromIndex` (e.g. the very last message, or beyond) degrades gracefully.
|
|
561
584
|
*/
|
|
562
585
|
export function estimateTailTokens(
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
586
|
+
messages: MinimalMessage[],
|
|
587
|
+
fromIndex: number,
|
|
588
|
+
estimateTokens: TokenEstimator = estimateTokensForText,
|
|
566
589
|
): number {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
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;
|
|
573
596
|
}
|
|
574
597
|
|
|
575
598
|
// ============================================================================
|
|
@@ -577,66 +600,73 @@ export function estimateTailTokens(
|
|
|
577
600
|
// ============================================================================
|
|
578
601
|
|
|
579
602
|
function globToRegExp(glob: string): RegExp {
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
export function isProtectedToolName(
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
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);
|
|
611
637
|
}
|
|
612
638
|
|
|
613
639
|
export function isProtectedPath(value: string, protections: PruneProtections): boolean {
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
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));
|
|
618
644
|
}
|
|
619
645
|
|
|
620
646
|
/** Shallow-collect string values from a tool-call arguments object, for path-glob matching. */
|
|
621
|
-
export function collectArgStringValues(
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
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;
|
|
640
670
|
}
|
|
641
671
|
|
|
642
672
|
/**
|
|
@@ -645,19 +675,22 @@ export function collectArgStringValues(args: Record<string, unknown> | undefined
|
|
|
645
675
|
*
|
|
646
676
|
* A "turn" starts at each user message. Protects the last `recentTurns` turns.
|
|
647
677
|
*/
|
|
648
|
-
export function computeRecencyBoundaryIndex(
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
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;
|
|
657
690
|
}
|
|
658
691
|
|
|
659
692
|
export function isWithinRecencyWindow(index: number, boundaryIndex: number): boolean {
|
|
660
|
-
|
|
693
|
+
return index >= boundaryIndex;
|
|
661
694
|
}
|
|
662
695
|
|
|
663
696
|
/**
|
|
@@ -666,11 +699,11 @@ export function isWithinRecencyWindow(index: number, boundaryIndex: number): boo
|
|
|
666
699
|
* message at the very end of the conversation has 0 turns elapsed.
|
|
667
700
|
*/
|
|
668
701
|
export function computeTurnsElapsedSince(messages: MinimalMessage[], index: number): number {
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
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;
|
|
674
707
|
}
|
|
675
708
|
|
|
676
709
|
// ============================================================================
|
|
@@ -678,9 +711,9 @@ export function computeTurnsElapsedSince(messages: MinimalMessage[], index: numb
|
|
|
678
711
|
// ============================================================================
|
|
679
712
|
|
|
680
713
|
export interface ToolCallPairIndices {
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
714
|
+
assistantIndex?: number;
|
|
715
|
+
toolCallBlockIndex?: number;
|
|
716
|
+
resultIndex?: number;
|
|
684
717
|
}
|
|
685
718
|
|
|
686
719
|
/**
|
|
@@ -691,37 +724,42 @@ export interface ToolCallPairIndices {
|
|
|
691
724
|
* pass it to `findToolCallPairIndices` and friends to avoid an O(n) rescan
|
|
692
725
|
* per decision (pe-e0zd).
|
|
693
726
|
*/
|
|
694
|
-
export function buildToolCallPairIndex(
|
|
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
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
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;
|
|
725
763
|
}
|
|
726
764
|
|
|
727
765
|
/**
|
|
@@ -731,125 +769,33 @@ export function buildToolCallPairIndex(messages: MinimalMessage[]): Map<string,
|
|
|
731
769
|
* `messages`; omitting it preserves the original O(n) full-scan behavior.
|
|
732
770
|
*/
|
|
733
771
|
export function findToolCallPairIndices(
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
772
|
+
messages: MinimalMessage[],
|
|
773
|
+
toolCallId: string,
|
|
774
|
+
pairIndex?: Map<string, ToolCallPairIndices>,
|
|
737
775
|
): ToolCallPairIndices {
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
return undefined;
|
|
762
|
-
}
|
|
763
|
-
|
|
764
|
-
function buildProjectedEntryIdToMessageIndexMap(
|
|
765
|
-
entries: MinimalSessionEntry[],
|
|
766
|
-
messages: MinimalMessage[],
|
|
767
|
-
projection: MinimalSessionProjection,
|
|
768
|
-
): Map<string, number> {
|
|
769
|
-
const map = new Map<string, number>();
|
|
770
|
-
const entryIds = new Set(entries.map((entry) => entry.id));
|
|
771
|
-
let cursor = 0;
|
|
772
|
-
|
|
773
|
-
for (const projectedEntry of projection.entries) {
|
|
774
|
-
let firstMessageIndex: number | undefined;
|
|
775
|
-
for (const projectedMessage of projectedEntry.messages) {
|
|
776
|
-
let messageIndex = messages.indexOf(projectedMessage, cursor);
|
|
777
|
-
if (messageIndex < cursor) messageIndex = -1;
|
|
778
|
-
|
|
779
|
-
if (messageIndex < 0 && typeof projectedMessage.timestamp === "number") {
|
|
780
|
-
messageIndex = messages.findIndex(
|
|
781
|
-
(candidate, index) =>
|
|
782
|
-
index >= cursor && candidate.role === projectedMessage.role && candidate.timestamp === projectedMessage.timestamp,
|
|
783
|
-
);
|
|
784
|
-
}
|
|
785
|
-
if (messageIndex < 0) continue;
|
|
786
|
-
firstMessageIndex ??= messageIndex;
|
|
787
|
-
cursor = messageIndex + 1;
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
if (firstMessageIndex !== undefined && entryIds.has(projectedEntry.sourceEntry.id)) {
|
|
791
|
-
map.set(projectedEntry.sourceEntry.id, firstMessageIndex);
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
return map;
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
export function buildEntryIdToMessageIndexMap(
|
|
798
|
-
entries: MinimalSessionEntry[],
|
|
799
|
-
messages: MinimalMessage[],
|
|
800
|
-
projection?: MinimalSessionProjection,
|
|
801
|
-
): Map<string, number> {
|
|
802
|
-
if (projection) return buildProjectedEntryIdToMessageIndexMap(entries, messages, projection);
|
|
803
|
-
|
|
804
|
-
// Older runtimes expose only raw branch entries; preserve their positional
|
|
805
|
-
// correlation and use role/timestamp only when the counts diverge.
|
|
806
|
-
const producing = entries.filter((entry) =>
|
|
807
|
-
entry.type === "message" || entry.type === "custom_message" || entry.type === "branch_summary" || entry.type === "compaction",
|
|
808
|
-
);
|
|
809
|
-
const length = Math.min(producing.length, messages.length);
|
|
810
|
-
const map = new Map<string, number>();
|
|
811
|
-
for (let i = 0; i < length; i++) {
|
|
812
|
-
const entry = producing[i];
|
|
813
|
-
const expectedRole = expectedRoleForEntry(entry);
|
|
814
|
-
const candidate = messages[i];
|
|
815
|
-
// Guard against silent misalignment (e.g. compaction/branch-summary shifted
|
|
816
|
-
// the projection): only trust the positional zip when roles line up,
|
|
817
|
-
// otherwise leave it unset so callers fall back to role+timestamp matching.
|
|
818
|
-
if (expectedRole && candidate?.role !== expectedRole) continue;
|
|
819
|
-
map.set(entry.id, i);
|
|
820
|
-
}
|
|
821
|
-
return map;
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
/** Last-resort correlation fallback: match by role + timestamp when order-zip counts diverge. */
|
|
825
|
-
export function findMessageIndexByRoleAndTimestamp(
|
|
826
|
-
entry: MinimalSessionEntry,
|
|
827
|
-
messages: MinimalMessage[],
|
|
828
|
-
): number | undefined {
|
|
829
|
-
const message = entry.message;
|
|
830
|
-
if (!message || typeof message.role !== "string" || typeof message.timestamp !== "number") return undefined;
|
|
831
|
-
const index = messages.findIndex((m) => m.role === message.role && m.timestamp === message.timestamp);
|
|
832
|
-
return index >= 0 ? index : undefined;
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
/** Resolve a session entry id using projection provenance, then legacy role+timestamp fallback. */
|
|
836
|
-
export function resolveMessageIndexForEntry(
|
|
837
|
-
entryId: string,
|
|
838
|
-
entries: MinimalSessionEntry[],
|
|
839
|
-
messages: MinimalMessage[],
|
|
840
|
-
projection?: MinimalSessionProjection,
|
|
841
|
-
): number | undefined {
|
|
842
|
-
const zipMap = buildEntryIdToMessageIndexMap(entries, messages, projection);
|
|
843
|
-
const zipped = zipMap.get(entryId);
|
|
844
|
-
if (zipped !== undefined) return zipped;
|
|
845
|
-
|
|
846
|
-
// An empty projected contribution is an explicit omission/replacement state;
|
|
847
|
-
// only a validated projection may make that context-edit-aware decision.
|
|
848
|
-
if (projection?.entries.some((entry) => entry.sourceEntry.id === entryId && entry.messages.length === 0)) return undefined;
|
|
849
|
-
|
|
850
|
-
const entry = entries.find((candidate) => candidate.id === entryId);
|
|
851
|
-
if (!entry) return undefined;
|
|
852
|
-
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;
|
|
853
799
|
}
|
|
854
800
|
|
|
855
801
|
// ============================================================================
|
|
@@ -859,37 +805,37 @@ export function resolveMessageIndexForEntry(
|
|
|
859
805
|
export type PruneTargetKind = "tool_result_content" | "tool_call_input";
|
|
860
806
|
|
|
861
807
|
/**
|
|
862
|
-
* Where a decision came from.
|
|
863
|
-
*
|
|
808
|
+
* Where a decision came from. Manual decisions from the `/prune` picker
|
|
809
|
+
* always bypass the net-benefit gate: user intent wins.
|
|
864
810
|
* Absent/unspecified defaults to "auto".
|
|
865
811
|
*/
|
|
866
812
|
export type PruneDecisionSource = "auto" | "manual";
|
|
867
813
|
|
|
868
814
|
/** What a strategy proposes. The pipeline decides whether/how to apply it. */
|
|
869
815
|
export interface ProposedPrune {
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
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;
|
|
877
823
|
}
|
|
878
824
|
|
|
879
825
|
/** What actually gets persisted once the pipeline applies a proposal. */
|
|
880
826
|
export interface PruneDecisionRecord {
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
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;
|
|
889
835
|
}
|
|
890
836
|
|
|
891
837
|
export function buildIdempotencyKey(proposal: ProposedPrune): string {
|
|
892
|
-
|
|
838
|
+
return `${proposal.strategyId}:${proposal.kind}:${proposal.toolCallId}`;
|
|
893
839
|
}
|
|
894
840
|
|
|
895
841
|
/**
|
|
@@ -901,53 +847,59 @@ export function buildIdempotencyKey(proposal: ProposedPrune): string {
|
|
|
901
847
|
* `runDynamicContextPruningPipeline`).
|
|
902
848
|
*/
|
|
903
849
|
function buildDecisionTargetKey(decision: PruneDecisionRecord): string {
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
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
|
+
};
|
|
919
871
|
}
|
|
920
872
|
|
|
921
873
|
/** Parse a persisted CustomEntry payload back into a decision record. Returns undefined if malformed. */
|
|
922
874
|
export function parseDecisionRecord(data: unknown): PruneDecisionRecord | undefined {
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
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
|
+
};
|
|
951
903
|
}
|
|
952
904
|
|
|
953
905
|
// ============================================================================
|
|
@@ -969,37 +921,49 @@ export function parseDecisionRecord(data: unknown): PruneDecisionRecord | undefi
|
|
|
969
921
|
* mutate or delete anything.
|
|
970
922
|
*/
|
|
971
923
|
export interface RestoreRecord {
|
|
972
|
-
|
|
973
|
-
|
|
924
|
+
idempotencyKey: string;
|
|
925
|
+
createdAt: string;
|
|
974
926
|
}
|
|
975
927
|
|
|
976
|
-
export function buildRestoreRecord(
|
|
977
|
-
|
|
928
|
+
export function buildRestoreRecord(
|
|
929
|
+
idempotencyKey: string,
|
|
930
|
+
createdAt: string = new Date().toISOString(),
|
|
931
|
+
): RestoreRecord {
|
|
932
|
+
return { idempotencyKey, createdAt };
|
|
978
933
|
}
|
|
979
934
|
|
|
980
935
|
/** Parse a persisted CustomEntry payload back into a restore record. Returns undefined if malformed. */
|
|
981
936
|
export function parseRestoreRecord(data: unknown): RestoreRecord | undefined {
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
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 };
|
|
987
942
|
}
|
|
988
943
|
|
|
989
944
|
/** Build the manual-prune proposal a /prune picker "prune" action emits. Always source: "manual" (gate bypass). */
|
|
990
|
-
export function buildManualPruneProposal(
|
|
991
|
-
|
|
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
|
+
};
|
|
992
956
|
}
|
|
993
957
|
|
|
994
958
|
export interface PruneTombstoneState {
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
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>;
|
|
1003
967
|
}
|
|
1004
968
|
|
|
1005
969
|
/**
|
|
@@ -1011,43 +975,42 @@ export interface PruneTombstoneState {
|
|
|
1011
975
|
* re-persisting a just-restored decision) and by the /prune picker (to show
|
|
1012
976
|
* accurate status and support prune/restore/re-prune).
|
|
1013
977
|
*
|
|
1014
|
-
* Idempotent
|
|
1015
|
-
* the same entries any number of times yields identical results.
|
|
978
|
+
* Idempotent: replaying the same entries any number of times yields identical results.
|
|
1016
979
|
*/
|
|
1017
980
|
export function resolvePruneTombstoneState(entries: MinimalSessionEntry[]): PruneTombstoneState {
|
|
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
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
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 };
|
|
1051
1014
|
}
|
|
1052
1015
|
|
|
1053
1016
|
// ============================================================================
|
|
@@ -1055,30 +1018,31 @@ export function resolvePruneTombstoneState(entries: MinimalSessionEntry[]): Prun
|
|
|
1055
1018
|
// ============================================================================
|
|
1056
1019
|
|
|
1057
1020
|
export function buildPlaceholderText(reason: string, charsRemoved: number): string {
|
|
1058
|
-
|
|
1021
|
+
return `[pruned by ${EXTENSION_ID}: ${reason} (${charsRemoved} chars removed)]`;
|
|
1059
1022
|
}
|
|
1060
1023
|
|
|
1061
1024
|
function contentTextLength(content: (MinimalTextContent | MinimalImageContent)[]): number {
|
|
1062
|
-
|
|
1025
|
+
return content.reduce((sum, block) => (block.type === "text" ? sum + block.text.length : sum), 0);
|
|
1063
1026
|
}
|
|
1064
1027
|
|
|
1065
1028
|
/** Return true when a decision targets a tool result containing one or more images. */
|
|
1066
1029
|
function isImageBearingToolResultTarget(
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1030
|
+
messages: MinimalMessage[],
|
|
1031
|
+
decision: PruneDecisionRecord,
|
|
1032
|
+
pairIndex?: Map<string, ToolCallPairIndices>,
|
|
1070
1033
|
): boolean {
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
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");
|
|
1077
1041
|
}
|
|
1078
1042
|
|
|
1079
1043
|
export interface ApplyResult {
|
|
1080
|
-
|
|
1081
|
-
|
|
1044
|
+
applied: boolean;
|
|
1045
|
+
charsRemoved: number;
|
|
1082
1046
|
}
|
|
1083
1047
|
|
|
1084
1048
|
/**
|
|
@@ -1088,91 +1052,106 @@ export interface ApplyResult {
|
|
|
1088
1052
|
* Gracefully no-ops when the target is absent (e.g. after compaction).
|
|
1089
1053
|
*/
|
|
1090
1054
|
export function applyPruneDecision(
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1055
|
+
messages: MinimalMessage[],
|
|
1056
|
+
decision: PruneDecisionRecord,
|
|
1057
|
+
pairIndex?: Map<string, ToolCallPairIndices>,
|
|
1094
1058
|
): ApplyResult {
|
|
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
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
}
|
|
1137
|
-
|
|
1138
|
-
function resolveToolNameForPair(
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
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;
|
|
1148
1117
|
}
|
|
1149
1118
|
|
|
1150
1119
|
function resolveArgStringsForPair(pair: ToolCallPairIndices, messages: MinimalMessage[]): string[] {
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
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);
|
|
1155
1126
|
}
|
|
1156
1127
|
|
|
1157
1128
|
export function isDecisionProtected(
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1129
|
+
decision: PruneDecisionRecord,
|
|
1130
|
+
messages: MinimalMessage[],
|
|
1131
|
+
protections: PruneProtections,
|
|
1132
|
+
recencyBoundaryIndex: number,
|
|
1133
|
+
pairIndex?: Map<string, ToolCallPairIndices>,
|
|
1163
1134
|
): boolean {
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
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;
|
|
1168
1147
|
|
|
1169
|
-
|
|
1170
|
-
|
|
1148
|
+
const toolName = resolveToolNameForPair(pair, messages);
|
|
1149
|
+
if (isProtectedToolName(toolName, protections)) return true;
|
|
1171
1150
|
|
|
1172
|
-
|
|
1173
|
-
|
|
1151
|
+
const argStrings = resolveArgStringsForPair(pair, messages);
|
|
1152
|
+
if (argStrings.some((value) => isProtectedPath(value, protections))) return true;
|
|
1174
1153
|
|
|
1175
|
-
|
|
1154
|
+
return false;
|
|
1176
1155
|
}
|
|
1177
1156
|
|
|
1178
1157
|
// ============================================================================
|
|
@@ -1180,10 +1159,10 @@ export function isDecisionProtected(
|
|
|
1180
1159
|
// ============================================================================
|
|
1181
1160
|
|
|
1182
1161
|
export interface DecisionSavingsEstimate {
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
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;
|
|
1187
1166
|
}
|
|
1188
1167
|
|
|
1189
1168
|
/**
|
|
@@ -1193,45 +1172,49 @@ export interface DecisionSavingsEstimate {
|
|
|
1193
1172
|
* (mirrors `applyPruneDecision`'s no-op conditions).
|
|
1194
1173
|
*/
|
|
1195
1174
|
export function estimateDecisionSavings(
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1175
|
+
messages: MinimalMessage[],
|
|
1176
|
+
decision: PruneDecisionRecord,
|
|
1177
|
+
estimateTokens: TokenEstimator = estimateTokensForText,
|
|
1178
|
+
pairIndex?: Map<string, ToolCallPairIndices>,
|
|
1200
1179
|
): DecisionSavingsEstimate | undefined {
|
|
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
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
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;
|
|
1235
1218
|
}
|
|
1236
1219
|
|
|
1237
1220
|
/**
|
|
@@ -1246,41 +1229,42 @@ export function estimateDecisionSavings(
|
|
|
1246
1229
|
* (negative savings) is reported as 0 chars saved, not a negative number.
|
|
1247
1230
|
*/
|
|
1248
1231
|
export function estimateDecisionCharsSaved(
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1232
|
+
messages: MinimalMessage[],
|
|
1233
|
+
decision: PruneDecisionRecord,
|
|
1234
|
+
pairIndex?: Map<string, ToolCallPairIndices>,
|
|
1252
1235
|
): number | undefined {
|
|
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
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
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;
|
|
1284
1268
|
}
|
|
1285
1269
|
|
|
1286
1270
|
/**
|
|
@@ -1298,44 +1282,47 @@ export function estimateDecisionCharsSaved(
|
|
|
1298
1282
|
* calls needed to amortize the one-time cache bust via the recurring saving.
|
|
1299
1283
|
*/
|
|
1300
1284
|
export interface CacheCostModelInput {
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1285
|
+
tailTokensAfterEarliestChange: number;
|
|
1286
|
+
tokensRemoved: number;
|
|
1287
|
+
cachedPriceRatio: number;
|
|
1304
1288
|
}
|
|
1305
1289
|
|
|
1306
1290
|
export interface CacheCostModelResult {
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1291
|
+
penalty: number;
|
|
1292
|
+
recurringSaving: number;
|
|
1293
|
+
/** Infinity when there is a penalty but no recurring saving to amortize it. */
|
|
1294
|
+
breakEvenCalls: number;
|
|
1311
1295
|
}
|
|
1312
1296
|
|
|
1313
1297
|
export function computeCacheCostModel(input: CacheCostModelInput): CacheCostModelResult {
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
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 };
|
|
1321
1308
|
}
|
|
1322
1309
|
|
|
1323
1310
|
/** A fresh, automatic (non-manual, non-persisted) decision pending the net-benefit gate. */
|
|
1324
1311
|
export interface GateCandidate {
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1312
|
+
decision: PruneDecisionRecord;
|
|
1313
|
+
position: number;
|
|
1314
|
+
tokensRemoved: number;
|
|
1328
1315
|
}
|
|
1329
1316
|
|
|
1330
1317
|
export interface NetBenefitGateResult {
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
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;
|
|
1339
1326
|
}
|
|
1340
1327
|
|
|
1341
1328
|
/**
|
|
@@ -1350,73 +1337,77 @@ export interface NetBenefitGateResult {
|
|
|
1350
1337
|
* `runDynamicContextPruningPipeline`).
|
|
1351
1338
|
*/
|
|
1352
1339
|
export function evaluateNetBenefitGate(
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1340
|
+
candidates: GateCandidate[],
|
|
1341
|
+
messages: MinimalMessage[],
|
|
1342
|
+
gateConfig: PruneGateConfig,
|
|
1343
|
+
agentState: AgentState = "idle",
|
|
1344
|
+
estimateTokens: TokenEstimator = estimateTokensForText,
|
|
1358
1345
|
): NetBenefitGateResult {
|
|
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
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
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
|
+
};
|
|
1420
1411
|
}
|
|
1421
1412
|
|
|
1422
1413
|
// ============================================================================
|
|
@@ -1425,106 +1416,118 @@ export function evaluateNetBenefitGate(
|
|
|
1425
1416
|
// ============================================================================
|
|
1426
1417
|
|
|
1427
1418
|
export interface PruneStatsRecord {
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1419
|
+
idempotencyKey: string;
|
|
1420
|
+
strategyId: string;
|
|
1421
|
+
tokensRemoved: number;
|
|
1422
|
+
appliedAt: string;
|
|
1432
1423
|
}
|
|
1433
1424
|
|
|
1434
1425
|
export function buildStatsRecord(
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1426
|
+
decision: PruneDecisionRecord,
|
|
1427
|
+
tokensRemoved: number,
|
|
1428
|
+
appliedAt: string = new Date().toISOString(),
|
|
1438
1429
|
): PruneStatsRecord {
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1430
|
+
return {
|
|
1431
|
+
idempotencyKey: decision.idempotencyKey,
|
|
1432
|
+
strategyId: decision.strategyId,
|
|
1433
|
+
tokensRemoved: Math.max(0, tokensRemoved),
|
|
1434
|
+
appliedAt,
|
|
1435
|
+
};
|
|
1445
1436
|
}
|
|
1446
1437
|
|
|
1447
1438
|
/** Parse a persisted stats CustomEntry payload back into a record. Returns undefined if malformed. */
|
|
1448
1439
|
export function parseStatsRecord(data: unknown): PruneStatsRecord | undefined {
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
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
|
+
};
|
|
1456
1453
|
}
|
|
1457
1454
|
|
|
1458
1455
|
export interface StrategyStats {
|
|
1459
|
-
|
|
1460
|
-
|
|
1456
|
+
tokensRemoved: number;
|
|
1457
|
+
pruneCount: number;
|
|
1461
1458
|
}
|
|
1462
1459
|
|
|
1463
1460
|
export interface CumulativePruneStats {
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1461
|
+
totalTokensRemoved: number;
|
|
1462
|
+
totalPruneCount: number;
|
|
1463
|
+
byStrategy: Record<string, StrategyStats>;
|
|
1467
1464
|
}
|
|
1468
1465
|
|
|
1469
1466
|
export function emptyCumulativeStats(): CumulativePruneStats {
|
|
1470
|
-
|
|
1467
|
+
return { totalTokensRemoved: 0, totalPruneCount: 0, byStrategy: {} };
|
|
1471
1468
|
}
|
|
1472
1469
|
|
|
1473
1470
|
/** Fold a single stats record into cumulative stats (pure; returns a new object). */
|
|
1474
|
-
export function foldStatsRecord(
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
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
|
+
};
|
|
1487
1487
|
}
|
|
1488
1488
|
|
|
1489
1489
|
export interface RebuiltStatsState {
|
|
1490
|
-
|
|
1491
|
-
|
|
1490
|
+
stats: CumulativePruneStats;
|
|
1491
|
+
seenKeys: Set<string>;
|
|
1492
1492
|
}
|
|
1493
1493
|
|
|
1494
1494
|
/**
|
|
1495
1495
|
* Rebuild cumulative stats from persisted CustomEntry records on a branch.
|
|
1496
|
-
* Idempotent
|
|
1497
|
-
* 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.
|
|
1498
1497
|
*/
|
|
1499
1498
|
export function rebuildStatsStateFromEntries(entries: MinimalSessionEntry[]): RebuiltStatsState {
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
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 };
|
|
1511
1510
|
}
|
|
1512
1511
|
|
|
1513
1512
|
/** Live (non-persisted) snapshot of raw vs effective (post-prune) context size for a single call. */
|
|
1514
1513
|
export interface ContextSizeSnapshot {
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1514
|
+
rawTokens: number;
|
|
1515
|
+
effectiveTokens: number;
|
|
1516
|
+
tokensSavedThisCall: number;
|
|
1518
1517
|
}
|
|
1519
1518
|
|
|
1520
1519
|
export function computeContextSizeSnapshot(
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1520
|
+
rawMessages: MinimalMessage[],
|
|
1521
|
+
effectiveMessages: MinimalMessage[],
|
|
1522
|
+
estimateTokens: TokenEstimator = estimateTokensForText,
|
|
1524
1523
|
): ContextSizeSnapshot {
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
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
|
+
};
|
|
1528
1531
|
}
|
|
1529
1532
|
|
|
1530
1533
|
// ============================================================================
|
|
@@ -1532,31 +1535,31 @@ export function computeContextSizeSnapshot(
|
|
|
1532
1535
|
// ============================================================================
|
|
1533
1536
|
|
|
1534
1537
|
export interface StrategyProposeInput {
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
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;
|
|
1555
1558
|
}
|
|
1556
1559
|
|
|
1557
1560
|
export interface PruneStrategy {
|
|
1558
|
-
|
|
1559
|
-
|
|
1561
|
+
id: string;
|
|
1562
|
+
propose(input: StrategyProposeInput): ProposedPrune[];
|
|
1560
1563
|
}
|
|
1561
1564
|
|
|
1562
1565
|
// ----------------------------------------------------------------------
|
|
@@ -1573,34 +1576,34 @@ export interface PruneStrategy {
|
|
|
1573
1576
|
* original key order.
|
|
1574
1577
|
*/
|
|
1575
1578
|
export function canonicalizeArguments(value: unknown): unknown {
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
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;
|
|
1584
1587
|
}
|
|
1585
1588
|
|
|
1586
1589
|
/** Canonical JSON string for a tool call's arguments (see `canonicalizeArguments`). */
|
|
1587
1590
|
export function canonicalizeArgumentsJSON(args: unknown): string {
|
|
1588
|
-
|
|
1591
|
+
return JSON.stringify(canonicalizeArguments(args ?? {}));
|
|
1589
1592
|
}
|
|
1590
1593
|
|
|
1591
1594
|
/** Dedup key: identical tool name (case-insensitive) + canonicalized args JSON. */
|
|
1592
1595
|
export function buildDedupeKey(toolName: string, args: unknown): string {
|
|
1593
|
-
|
|
1596
|
+
return `${toolName.trim().toLowerCase()}::${canonicalizeArgumentsJSON(args)}`;
|
|
1594
1597
|
}
|
|
1595
1598
|
|
|
1596
1599
|
/** Exported for /prune picker item building (pe-8re9) and direct unit testing. */
|
|
1597
1600
|
export interface ToolCallOccurrence {
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1601
|
+
toolCallId: string;
|
|
1602
|
+
toolName: string;
|
|
1603
|
+
arguments: Record<string, unknown> | undefined;
|
|
1604
|
+
assistantIndex: number;
|
|
1605
|
+
resultIndex: number;
|
|
1606
|
+
isError: boolean;
|
|
1604
1607
|
}
|
|
1605
1608
|
|
|
1606
1609
|
/**
|
|
@@ -1610,35 +1613,37 @@ export interface ToolCallOccurrence {
|
|
|
1610
1613
|
* child results as transcript messages, so expanding it would double-count or
|
|
1611
1614
|
* invent prune targets.
|
|
1612
1615
|
*/
|
|
1613
|
-
export function collectCompletedToolCallOccurrences(
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
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;
|
|
1642
1647
|
}
|
|
1643
1648
|
|
|
1644
1649
|
// ----------------------------------------------------------------------
|
|
@@ -1647,40 +1652,40 @@ export function collectCompletedToolCallOccurrences(messages: MinimalMessage[]):
|
|
|
1647
1652
|
|
|
1648
1653
|
/** Placeholder text for a duplicate tool result whose content was replaced. */
|
|
1649
1654
|
export function buildDedupePlaceholder(toolName: string, newestToolCallId: string): string {
|
|
1650
|
-
|
|
1655
|
+
return `[pruned by ${EXTENSION_ID}: duplicate ${toolName} call result; see newest occurrence (call ${newestToolCallId})]`;
|
|
1651
1656
|
}
|
|
1652
1657
|
|
|
1653
1658
|
export const dedupeStrategy: PruneStrategy = {
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
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
|
+
},
|
|
1684
1689
|
};
|
|
1685
1690
|
|
|
1686
1691
|
// ----------------------------------------------------------------------
|
|
@@ -1688,27 +1693,27 @@ export const dedupeStrategy: PruneStrategy = {
|
|
|
1688
1693
|
// ----------------------------------------------------------------------
|
|
1689
1694
|
|
|
1690
1695
|
export const errorPurgeStrategy: PruneStrategy = {
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
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
|
+
},
|
|
1712
1717
|
};
|
|
1713
1718
|
|
|
1714
1719
|
// ----------------------------------------------------------------------
|
|
@@ -1762,10 +1767,12 @@ type FileOpKind = "read" | "write" | "edit";
|
|
|
1762
1767
|
const FILE_OP_TOOL_NAMES: ReadonlySet<string> = new Set(["read", "write", "edit"]);
|
|
1763
1768
|
|
|
1764
1769
|
/** Extract a file path from tool-call arguments, checking common arg names defensively. */
|
|
1765
|
-
export function extractFileOpPathArg(
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
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;
|
|
1769
1776
|
}
|
|
1770
1777
|
|
|
1771
1778
|
/**
|
|
@@ -1777,32 +1784,39 @@ export function extractFileOpPathArg(args: Record<string, unknown> | undefined):
|
|
|
1777
1784
|
* spellings of the same file will NOT be unified in that case.
|
|
1778
1785
|
*/
|
|
1779
1786
|
export function normalizeFileOpsPath(rawPath: string, cwd: string | undefined): string {
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
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);
|
|
1784
1792
|
}
|
|
1785
1793
|
|
|
1786
1794
|
/** A read's line range. `end === undefined` means the read continues through end-of-file. */
|
|
1787
1795
|
export interface FileReadRange {
|
|
1788
|
-
|
|
1789
|
-
|
|
1796
|
+
start: number;
|
|
1797
|
+
end: number | undefined;
|
|
1790
1798
|
}
|
|
1791
1799
|
|
|
1792
1800
|
/** Compute a read's line range from its `offset`/`limit` args (see pi's `read` tool: 1-indexed offset, line-count limit). */
|
|
1793
1801
|
export function computeFileReadRange(args: Record<string, unknown> | undefined): FileReadRange {
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
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 };
|
|
1801
1815
|
}
|
|
1802
1816
|
|
|
1803
1817
|
/** True when a range represents a full-file read (starts at line 1, no limit). */
|
|
1804
1818
|
export function isFullFileReadRange(range: FileReadRange): boolean {
|
|
1805
|
-
|
|
1819
|
+
return range.start === 1 && range.end === undefined;
|
|
1806
1820
|
}
|
|
1807
1821
|
|
|
1808
1822
|
/**
|
|
@@ -1819,188 +1833,211 @@ export function isFullFileReadRange(range: FileReadRange): boolean {
|
|
|
1819
1833
|
* - Otherwise (both bounded), `later` covers `earlier` iff later.end >= earlier.end.
|
|
1820
1834
|
*/
|
|
1821
1835
|
export function readRangeCovers(later: FileReadRange, earlier: FileReadRange): boolean {
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
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;
|
|
1826
1840
|
}
|
|
1827
1841
|
|
|
1828
1842
|
interface FileOpOccurrence {
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
}
|
|
1837
|
-
|
|
1838
|
-
function collectFileOpOccurrences(
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
}
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
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})]`;
|
|
1871
1899
|
}
|
|
1872
1900
|
|
|
1873
1901
|
export const supersededFileOpsStrategy: PruneStrategy = {
|
|
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
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
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
|
+
},
|
|
1987
2020
|
};
|
|
1988
2021
|
|
|
1989
2022
|
/**
|
|
1990
2023
|
* Strategies PROPOSE prunes; only this pipeline ever APPLIES them.
|
|
1991
2024
|
*/
|
|
1992
|
-
export const STRATEGIES: PruneStrategy[] = [
|
|
2025
|
+
export const STRATEGIES: PruneStrategy[] = [
|
|
2026
|
+
dedupeStrategy,
|
|
2027
|
+
errorPurgeStrategy,
|
|
2028
|
+
supersededFileOpsStrategy,
|
|
2029
|
+
];
|
|
1993
2030
|
|
|
1994
2031
|
function collectProposals(input: StrategyProposeInput): ProposedPrune[] {
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
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;
|
|
2004
2041
|
}
|
|
2005
2042
|
|
|
2006
2043
|
// ============================================================================
|
|
@@ -2008,42 +2045,38 @@ function collectProposals(input: StrategyProposeInput): ProposedPrune[] {
|
|
|
2008
2045
|
// ============================================================================
|
|
2009
2046
|
|
|
2010
2047
|
export interface PipelineInput {
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
*/
|
|
2032
|
-
agentState?: AgentState;
|
|
2033
|
-
/** Session working directory (pe-qs8j), forwarded to strategies for path normalization. */
|
|
2034
|
-
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;
|
|
2035
2068
|
}
|
|
2036
2069
|
|
|
2037
2070
|
export interface PipelineResult {
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
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;
|
|
2047
2080
|
}
|
|
2048
2081
|
|
|
2049
2082
|
/**
|
|
@@ -2057,212 +2090,224 @@ export interface PipelineResult {
|
|
|
2057
2090
|
* happened) and manual decisions always bypass the gate (user intent wins).
|
|
2058
2091
|
*/
|
|
2059
2092
|
export function runDynamicContextPruningPipeline(input: PipelineInput): PipelineResult {
|
|
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
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
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 };
|
|
2266
2311
|
}
|
|
2267
2312
|
|
|
2268
2313
|
// ============================================================================
|
|
@@ -2270,46 +2315,48 @@ export function rebuildDecisionStateFromEntries(entries: MinimalSessionEntry[]):
|
|
|
2270
2315
|
// ============================================================================
|
|
2271
2316
|
|
|
2272
2317
|
function resolveSessionProjection(ctx: ExtensionContext): MinimalSessionProjection | undefined {
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
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
|
+
}
|
|
2281
2328
|
}
|
|
2282
2329
|
|
|
2283
2330
|
export function sessionEntriesToMessages(
|
|
2284
|
-
|
|
2285
|
-
|
|
2331
|
+
entries: MinimalSessionEntry[],
|
|
2332
|
+
projection?: MinimalSessionProjection,
|
|
2286
2333
|
): MinimalMessage[] {
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
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;
|
|
2293
2340
|
}
|
|
2294
2341
|
|
|
2295
2342
|
/** Truncated, canonicalized single-line digest of a tool call's arguments, for compact picker display. */
|
|
2296
2343
|
export function buildArgsDigest(args: unknown, maxLength = 80): string {
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2344
|
+
const json = canonicalizeArgumentsJSON(args);
|
|
2345
|
+
if (json.length <= maxLength) return json;
|
|
2346
|
+
return `${json.slice(0, Math.max(0, maxLength - 1))}\u2026`;
|
|
2300
2347
|
}
|
|
2301
2348
|
|
|
2302
2349
|
export type PrunableItemStatus = "active" | "pruned" | "restored";
|
|
2303
2350
|
|
|
2304
2351
|
export interface PrunableItem {
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
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;
|
|
2313
2360
|
}
|
|
2314
2361
|
|
|
2315
2362
|
/**
|
|
@@ -2321,55 +2368,58 @@ export interface PrunableItem {
|
|
|
2321
2368
|
* usage in the extension wiring).
|
|
2322
2369
|
*/
|
|
2323
2370
|
export function buildPrunableItems(
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2371
|
+
messages: MinimalMessage[],
|
|
2372
|
+
activeByToolCallId: Map<string, PruneDecisionRecord>,
|
|
2373
|
+
restoredToolCallIds: ReadonlySet<string>,
|
|
2374
|
+
_estimateTokens: TokenEstimator = estimateTokensForText,
|
|
2328
2375
|
): PrunableItem[] {
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
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;
|
|
2351
2398
|
}
|
|
2352
2399
|
|
|
2353
2400
|
/** Build a Map<toolCallId, decision> from active decisions, restricted to tool_result_content kind. First match per toolCallId wins. */
|
|
2354
|
-
export function buildActiveResultDecisionMap(
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
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;
|
|
2362
2412
|
}
|
|
2363
2413
|
|
|
2364
2414
|
/** Single-line label for a /prune picker option row. */
|
|
2365
2415
|
export function formatPrunableItemOption(item: PrunableItem): string {
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
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}`;
|
|
2373
2423
|
}
|
|
2374
2424
|
|
|
2375
2425
|
/**
|
|
@@ -2380,29 +2430,40 @@ export function formatPrunableItemOption(item: PrunableItem): string {
|
|
|
2380
2430
|
* Do NOT recover a selection via `formatPrunableItemOption(item) === choice` or
|
|
2381
2431
|
* `options.indexOf(choice)` on re-derived labels — always match against these rows' `label`.
|
|
2382
2432
|
*/
|
|
2383
|
-
export function buildPrunableItemPickerOptions(
|
|
2384
|
-
|
|
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
|
+
}));
|
|
2385
2440
|
}
|
|
2386
2441
|
|
|
2387
2442
|
/** Multi-line detail text for a /prune confirm dialog, describing the item and (when pruning) the predicted cache-bust cost. */
|
|
2388
2443
|
export function formatPrunableItemDetail(item: PrunableItem, cost?: CacheCostModelResult): string {
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
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");
|
|
2400
2461
|
}
|
|
2401
2462
|
|
|
2402
2463
|
/** Plain-text report used both by the non-UI /prune fallback and as a debugging aid. */
|
|
2403
2464
|
export function formatPrunableItemsReport(items: PrunableItem[]): string[] {
|
|
2404
|
-
|
|
2405
|
-
|
|
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)}`);
|
|
2406
2467
|
}
|
|
2407
2468
|
|
|
2408
2469
|
// ============================================================================
|
|
@@ -2412,415 +2473,498 @@ export function formatPrunableItemsReport(items: PrunableItem[]): string[] {
|
|
|
2412
2473
|
export type StrategyKey = "dedupe" | "errorPurge" | "supersededFileOps";
|
|
2413
2474
|
|
|
2414
2475
|
const STRATEGY_ALIASES: Record<string, StrategyKey> = {
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
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",
|
|
2424
2485
|
};
|
|
2425
2486
|
|
|
2426
2487
|
const STRATEGY_DISPLAY_NAMES: Record<StrategyKey, string> = {
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2488
|
+
dedupe: "dedupe",
|
|
2489
|
+
errorPurge: "error-purge",
|
|
2490
|
+
supersededFileOps: "superseded-file-ops",
|
|
2430
2491
|
};
|
|
2431
2492
|
|
|
2432
2493
|
export type ContextPruningSubcommand =
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
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 };
|
|
2441
2502
|
|
|
2442
2503
|
/** Parse `/context-pruning <...>` arguments into a typed subcommand. Pure; never throws. */
|
|
2443
2504
|
export function parseContextPruningArgs(rawArgs: string): ContextPruningSubcommand {
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
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
|
+
}
|
|
2479
2543
|
}
|
|
2480
2544
|
|
|
2481
2545
|
export function contextPruningUsage(): string {
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
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");
|
|
2491
2555
|
}
|
|
2492
2556
|
|
|
2493
2557
|
export interface ConfigMutationResult {
|
|
2494
|
-
|
|
2495
|
-
|
|
2558
|
+
config: DynamicContextPruningConfig;
|
|
2559
|
+
message: string;
|
|
2496
2560
|
}
|
|
2497
2561
|
|
|
2498
2562
|
/** Apply a config-mutating subcommand ((enabled|toggle|strategy|gate)) to a config. Pure; returns a new config object. */
|
|
2499
2563
|
export function applyContextPruningConfigMutation(
|
|
2500
|
-
|
|
2501
|
-
|
|
2564
|
+
config: DynamicContextPruningConfig,
|
|
2565
|
+
subcommand: Extract<
|
|
2566
|
+
ContextPruningSubcommand,
|
|
2567
|
+
{ kind: "enabled" | "toggle" | "strategy" | "gate" }
|
|
2568
|
+
>,
|
|
2502
2569
|
): ConfigMutationResult {
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
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
|
+
};
|
|
2527
2600
|
}
|
|
2528
2601
|
|
|
2529
2602
|
export interface StatusReportInput {
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2603
|
+
config: DynamicContextPruningConfig;
|
|
2604
|
+
contextUsage?: { tokens: number | null; contextWindow: number; percent: number | null };
|
|
2605
|
+
lastSnapshot?: ContextSizeSnapshot;
|
|
2533
2606
|
}
|
|
2534
2607
|
|
|
2535
2608
|
/** Human-readable `/context-pruning status` report lines. Pure; testable with fixture inputs. */
|
|
2536
2609
|
export function formatStatusReport(input: StatusReportInput): string[] {
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
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;
|
|
2564
2643
|
}
|
|
2565
2644
|
|
|
2566
2645
|
/** Human-readable `/context-pruning stats` report lines. Pure; testable with fixture inputs. */
|
|
2567
2646
|
export function formatStatsReport(stats: CumulativePruneStats): string[] {
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
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;
|
|
2579
2660
|
}
|
|
2580
2661
|
|
|
2581
2662
|
// ============================================================================
|
|
2582
2663
|
// Extension wiring
|
|
2583
2664
|
// ============================================================================
|
|
2584
2665
|
|
|
2585
|
-
function notify(
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
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);
|
|
2591
2676
|
}
|
|
2592
2677
|
|
|
2593
2678
|
export const __testing = {
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2679
|
+
globToRegExp,
|
|
2680
|
+
getConfigPath,
|
|
2681
|
+
readConfig,
|
|
2682
|
+
writeConfig,
|
|
2598
2683
|
};
|
|
2599
2684
|
|
|
2600
2685
|
export default function dynamicContextPruningExtension(pi: ExtensionAPI) {
|
|
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
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
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
|
+
});
|
|
2826
2970
|
}
|