pi-dynamic-context-pruning 0.1.10 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -5
- package/index.ts +17 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,11 +27,23 @@ and why it hasn't been built yet.
|
|
|
27
27
|
key wins when replaying history, so a decision can be pruned, restored, and
|
|
28
28
|
re-pruned any number of times.
|
|
29
29
|
- **Recomputed every call, not on a timer.** All three automatic strategies run
|
|
30
|
-
fresh on every `context` event, over the current message array.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
fresh on every `context` event, over the current message array. Pi 0.99's
|
|
31
|
+
`context` hook intentionally receives the mutable conversation without system
|
|
32
|
+
messages; Pi restores its system prompt and tool state after the handler. The
|
|
33
|
+
extension does not try to rebuild that request-only envelope.
|
|
34
|
+
- **Canonical picker input.** `/prune` uses Pi's canonical session projection when
|
|
35
|
+
available, so context omissions and replacements, branch selection, and tool
|
|
36
|
+
pairing match the provider-visible conversation. Older hosts retain the raw
|
|
37
|
+
message-entry fallback.
|
|
38
|
+
- **Nested calls stay metadata.** Pi records bounded `nestedCalls` metadata on a
|
|
39
|
+
parent tool result, but nested results are not transcript messages. Strategies
|
|
40
|
+
count and prune only the parent transcript tool result; they never materialize
|
|
41
|
+
child entries, double-count child output, or infer complete history from a
|
|
42
|
+
truncated/incomplete record.
|
|
43
|
+
- **Persisted decisions remain branch-local.** Nothing is pruned "once and
|
|
44
|
+
forgotten"; if a tool result would no longer qualify (e.g. it's now within the
|
|
45
|
+
protected recency window), it isn't proposed again, but an already-applied,
|
|
46
|
+
persisted decision still applies until explicitly restored.
|
|
35
47
|
|
|
36
48
|
## Strategies
|
|
37
49
|
|
package/index.ts
CHANGED
|
@@ -51,6 +51,8 @@ export interface MinimalToolResultMessage {
|
|
|
51
51
|
toolCallId: string;
|
|
52
52
|
toolName: string;
|
|
53
53
|
content: (MinimalTextContent | MinimalImageContent)[];
|
|
54
|
+
/** Bounded nested-call metadata; child calls have no transcript results to prune. */
|
|
55
|
+
nestedCalls?: unknown;
|
|
54
56
|
isError: boolean;
|
|
55
57
|
timestamp?: number;
|
|
56
58
|
[key: string]: unknown;
|
|
@@ -114,13 +116,16 @@ export function normalizeSessionProjection(value: unknown): MinimalSessionProjec
|
|
|
114
116
|
});
|
|
115
117
|
}
|
|
116
118
|
|
|
119
|
+
// Entries carry the provenance that Pi uses to apply omissions and
|
|
120
|
+
// replacements. Validate the optional flattened field for compatibility, but
|
|
121
|
+
// derive the messages used by pruning from entries so stale/invented records
|
|
122
|
+
// cannot bypass canonical context edits.
|
|
117
123
|
if ("messages" in value && (!Array.isArray(value.messages) || !value.messages.every(isMinimalMessage))) return undefined;
|
|
118
|
-
|
|
119
|
-
return { entries, messages };
|
|
124
|
+
return { entries, messages: entries.flatMap((entry) => entry.messages) };
|
|
120
125
|
}
|
|
121
126
|
|
|
122
127
|
export function sessionProjectionToMessages(projection: MinimalSessionProjection): MinimalMessage[] {
|
|
123
|
-
return projection.
|
|
128
|
+
return projection.entries.flatMap((entry) => entry.messages);
|
|
124
129
|
}
|
|
125
130
|
|
|
126
131
|
// ============================================================================
|
|
@@ -1598,7 +1603,13 @@ export interface ToolCallOccurrence {
|
|
|
1598
1603
|
isError: boolean;
|
|
1599
1604
|
}
|
|
1600
1605
|
|
|
1601
|
-
/**
|
|
1606
|
+
/**
|
|
1607
|
+
* Collect every transcript assistant toolCall block that has a matching
|
|
1608
|
+
* transcript toolResult message, in message order. `nestedCalls` is deliberately
|
|
1609
|
+
* not expanded: Pi keeps only bounded call metadata there and does not record
|
|
1610
|
+
* child results as transcript messages, so expanding it would double-count or
|
|
1611
|
+
* invent prune targets.
|
|
1612
|
+
*/
|
|
1602
1613
|
export function collectCompletedToolCallOccurrences(messages: MinimalMessage[]): ToolCallOccurrence[] {
|
|
1603
1614
|
const resultIndexById = new Map<string, number>();
|
|
1604
1615
|
const isErrorById = new Map<string, boolean>();
|
|
@@ -2318,6 +2329,8 @@ export function buildPrunableItems(
|
|
|
2318
2329
|
const items: PrunableItem[] = [];
|
|
2319
2330
|
for (const occurrence of collectCompletedToolCallOccurrences(messages)) {
|
|
2320
2331
|
const resultMessage = messages[occurrence.resultIndex] as MinimalToolResultMessage;
|
|
2332
|
+
// Only provider-visible transcript content is economically attributable;
|
|
2333
|
+
// bounded nested-call metadata has no child result content to estimate.
|
|
2321
2334
|
const estimatedTokens = estimateTokensForContent(resultMessage.content);
|
|
2322
2335
|
const activeDecision = activeByToolCallId.get(occurrence.toolCallId);
|
|
2323
2336
|
const status: PrunableItemStatus = activeDecision
|