pi-context-kit 0.2.0 → 0.3.0
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 +510 -68
- package/extensions/index.ts +234 -155
- package/package.json +21 -8
- package/src/config.ts +0 -9
- package/src/context.ts +0 -15
- package/src/maintain.ts +10 -13
- package/src/prune.ts +24 -26
- package/src/shake.ts +20 -11
- package/src/stale.ts +39 -44
- package/src/tool-calls.ts +22 -1
- package/CHANGELOG.md +0 -28
package/src/prune.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import type { ProjectedSessionEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
editableMessage,
|
|
5
|
-
messageTokens,
|
|
6
|
-
withContent,
|
|
7
|
-
} from "./context.ts";
|
|
8
|
-
import { toolCallsById } from "./tool-calls.ts";
|
|
2
|
+
import { type Edit, messageTokens, withContent } from "./context.ts";
|
|
3
|
+
import { newestToolResults, type ToolResult } from "./tool-calls.ts";
|
|
9
4
|
|
|
10
5
|
export interface PruneOptions {
|
|
11
6
|
/** Newest tool-output tokens that are never pruned. */
|
|
@@ -22,41 +17,44 @@ export function pruneToolOutputs(
|
|
|
22
17
|
edited: ReadonlySet<string>,
|
|
23
18
|
options: PruneOptions,
|
|
24
19
|
): Edit[] {
|
|
25
|
-
const calls = toolCallsById(entries);
|
|
26
20
|
const edits: Edit[] = [];
|
|
27
21
|
let seen = 0;
|
|
28
22
|
let savings = 0;
|
|
29
23
|
|
|
30
|
-
for (
|
|
31
|
-
const
|
|
32
|
-
const message = entry && editableMessage(entry);
|
|
33
|
-
if (!entry || message?.role !== "toolResult") continue;
|
|
34
|
-
const tokens = messageTokens(message);
|
|
24
|
+
for (const result of newestToolResults(entries)) {
|
|
25
|
+
const tokens = messageTokens(result.message);
|
|
35
26
|
const protectedByWindow = seen < options.protectTokens;
|
|
36
27
|
seen += tokens;
|
|
37
|
-
const id = entry.sourceEntry.id;
|
|
38
28
|
if (
|
|
39
29
|
protectedByWindow ||
|
|
40
30
|
tokens < options.minTokens ||
|
|
41
|
-
edited.has(id) ||
|
|
42
|
-
isSkillFile(
|
|
31
|
+
edited.has(result.id) ||
|
|
32
|
+
isSkillFile(result.args)
|
|
43
33
|
) {
|
|
44
34
|
continue;
|
|
45
35
|
}
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
},
|
|
51
|
-
];
|
|
52
|
-
const saved = tokens - messageTokens(withContent(message, content));
|
|
53
|
-
if (saved <= 0) continue;
|
|
54
|
-
savings += saved;
|
|
55
|
-
edits.push({ targetId: id, content });
|
|
36
|
+
const edit = pruneEdit(result, tokens);
|
|
37
|
+
if (!edit) continue;
|
|
38
|
+
savings += edit.saved;
|
|
39
|
+
edits.push(edit.edit);
|
|
56
40
|
}
|
|
57
41
|
return savings >= options.minSavings ? edits.reverse() : [];
|
|
58
42
|
}
|
|
59
43
|
|
|
44
|
+
function pruneEdit(
|
|
45
|
+
{ id, message }: ToolResult,
|
|
46
|
+
tokens: number,
|
|
47
|
+
): { edit: Edit; saved: number } | undefined {
|
|
48
|
+
const content = [
|
|
49
|
+
{
|
|
50
|
+
type: "text" as const,
|
|
51
|
+
text: `[Output pruned: about ${tokens} tokens. Call recall with entryId "${id}" to read it.]`,
|
|
52
|
+
},
|
|
53
|
+
];
|
|
54
|
+
const saved = tokens - messageTokens(withContent(message, content));
|
|
55
|
+
return saved > 0 ? { edit: { targetId: id, content }, saved } : undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
60
58
|
function isSkillFile(args: Record<string, unknown> | undefined): boolean {
|
|
61
59
|
const path = args?.path;
|
|
62
60
|
return typeof path === "string" && /(^|[\\/])SKILL\.md$/.test(path);
|
package/src/shake.ts
CHANGED
|
@@ -26,7 +26,8 @@ const BLOCK =
|
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Elides large blocks and images from older messages. Tool calls and thinking stay intact,
|
|
29
|
-
* so every tool call keeps its result
|
|
29
|
+
* so every tool call keeps its result. Signatures stay valid, but Anthropic drops every thinking
|
|
30
|
+
* block after the first changed message.
|
|
30
31
|
*/
|
|
31
32
|
export function shake(
|
|
32
33
|
entries: readonly ProjectedSessionEntry[],
|
|
@@ -43,22 +44,30 @@ export function shake(
|
|
|
43
44
|
for (const message of entry.messages) {
|
|
44
45
|
if (message.role !== "system") seen += messageTokens(message);
|
|
45
46
|
}
|
|
46
|
-
const
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (content === undefined) continue;
|
|
51
|
-
const saved =
|
|
52
|
-
messageTokens(message) - messageTokens(withContent(message, content));
|
|
53
|
-
if (saved <= 0) continue;
|
|
54
|
-
savings += saved;
|
|
55
|
-
edits.push({ targetId: entry.sourceEntry.id, content });
|
|
47
|
+
const edit = isProtected ? undefined : shakeEdit(entry, options);
|
|
48
|
+
if (!edit) continue;
|
|
49
|
+
savings += edit.saved;
|
|
50
|
+
edits.push(edit.edit);
|
|
56
51
|
}
|
|
57
52
|
return savings >= options.minSavings && edits.length > 0
|
|
58
53
|
? edits.reverse()
|
|
59
54
|
: [];
|
|
60
55
|
}
|
|
61
56
|
|
|
57
|
+
function shakeEdit(
|
|
58
|
+
entry: ProjectedSessionEntry,
|
|
59
|
+
options: ShakeOptions,
|
|
60
|
+
): { edit: Edit; saved: number } | undefined {
|
|
61
|
+
const message = editableMessage(entry);
|
|
62
|
+
if (!message || message.role === "user") return undefined;
|
|
63
|
+
const targetId = entry.sourceEntry.id;
|
|
64
|
+
const content = shakeContent(message, targetId, options);
|
|
65
|
+
if (content === undefined) return undefined;
|
|
66
|
+
const saved =
|
|
67
|
+
messageTokens(message) - messageTokens(withContent(message, content));
|
|
68
|
+
return saved > 0 ? { edit: { targetId, content }, saved } : undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
62
71
|
function shakeContent(
|
|
63
72
|
message: EditableMessage,
|
|
64
73
|
id: string,
|
package/src/stale.ts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
import type { ToolResultMessage } from "@earendil-works/pi-ai";
|
|
2
2
|
import type { ProjectedSessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
editableMessage,
|
|
6
|
-
messageTokens,
|
|
7
|
-
suffixTokens,
|
|
8
|
-
withContent,
|
|
9
|
-
} from "./context.ts";
|
|
10
|
-
import { toolCallsById } from "./tool-calls.ts";
|
|
3
|
+
import { type Edit, messageTokens, withContent } from "./context.ts";
|
|
4
|
+
import { newestToolResults, type ToolResult } from "./tool-calls.ts";
|
|
11
5
|
|
|
12
6
|
export const SUPERSEDED_NOTICE = "[Superseded by a newer read of this file]";
|
|
13
7
|
|
|
@@ -26,62 +20,63 @@ interface NewerReads {
|
|
|
26
20
|
/**
|
|
27
21
|
* Replaces read results that a newer read of the same file makes redundant.
|
|
28
22
|
* A newer full read covers every older read; a newer ranged read covers only the same range;
|
|
29
|
-
* a failed read covers only older failed reads.
|
|
30
|
-
* within `suffixLimit` tokens.
|
|
23
|
+
* a failed read covers only older failed reads.
|
|
31
24
|
*/
|
|
32
25
|
export function supersededReads(
|
|
33
26
|
entries: readonly ProjectedSessionEntry[],
|
|
34
27
|
edited: ReadonlySet<string>,
|
|
35
|
-
suffixLimit: number,
|
|
36
28
|
): Edit[] {
|
|
37
|
-
const calls = toolCallsById(entries);
|
|
38
|
-
const suffix = suffixTokens(entries);
|
|
39
29
|
const newer = new Map<string, NewerReads>();
|
|
40
30
|
const edits: Edit[] = [];
|
|
41
31
|
|
|
42
|
-
for (
|
|
43
|
-
const
|
|
44
|
-
const message = entry && editableMessage(entry);
|
|
45
|
-
if (!entry || message?.role !== "toolResult" || message.toolName !== "read")
|
|
46
|
-
continue;
|
|
47
|
-
const key = readKey(calls.get(message.toolCallId)?.arguments);
|
|
32
|
+
for (const result of newestToolResults(entries)) {
|
|
33
|
+
const key = result.message.toolName === "read" && readKey(result.args);
|
|
48
34
|
if (!key) continue;
|
|
49
|
-
|
|
50
35
|
const seen = newer.get(key.path) ?? {
|
|
51
36
|
anyRead: false,
|
|
52
37
|
fullSuccess: false,
|
|
53
38
|
successRanges: new Set<string>(),
|
|
54
39
|
};
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
? seen.anyRead
|
|
59
|
-
: seen.fullSuccess ||
|
|
60
|
-
(key.range !== undefined && seen.successRanges.has(key.range));
|
|
61
|
-
|
|
62
|
-
if (
|
|
63
|
-
superseded &&
|
|
64
|
-
!edited.has(entry.sourceEntry.id) &&
|
|
65
|
-
(suffix[i] ?? Number.POSITIVE_INFINITY) <= suffixLimit
|
|
66
|
-
) {
|
|
67
|
-
const content = [{ type: "text" as const, text: SUPERSEDED_NOTICE }];
|
|
68
|
-
if (
|
|
69
|
-
messageTokens(withContent(message, content)) < messageTokens(message)
|
|
70
|
-
) {
|
|
71
|
-
edits.push({ targetId: entry.sourceEntry.id, content });
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
seen.anyRead = true;
|
|
76
|
-
if (readCoversOutput) {
|
|
77
|
-
if (key.range === undefined) seen.fullSuccess = true;
|
|
78
|
-
else seen.successRanges.add(key.range);
|
|
40
|
+
if (!edited.has(result.id) && isSuperseded(result, key, seen)) {
|
|
41
|
+
const edit = supersededEdit(result);
|
|
42
|
+
if (edit) edits.push(edit);
|
|
79
43
|
}
|
|
44
|
+
recordRead(seen, result, key);
|
|
80
45
|
newer.set(key.path, seen);
|
|
81
46
|
}
|
|
82
47
|
return edits.reverse();
|
|
83
48
|
}
|
|
84
49
|
|
|
50
|
+
function isSuperseded(
|
|
51
|
+
{ message }: ToolResult,
|
|
52
|
+
key: ReadKey,
|
|
53
|
+
seen: NewerReads,
|
|
54
|
+
): boolean {
|
|
55
|
+
if (message.isError) return seen.anyRead;
|
|
56
|
+
return (
|
|
57
|
+
seen.fullSuccess ||
|
|
58
|
+
(key.range !== undefined && seen.successRanges.has(key.range))
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function supersededEdit({ id, message }: ToolResult): Edit | undefined {
|
|
63
|
+
const content = [{ type: "text" as const, text: SUPERSEDED_NOTICE }];
|
|
64
|
+
return messageTokens(withContent(message, content)) < messageTokens(message)
|
|
65
|
+
? { targetId: id, content }
|
|
66
|
+
: undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function recordRead(
|
|
70
|
+
seen: NewerReads,
|
|
71
|
+
{ message }: ToolResult,
|
|
72
|
+
key: ReadKey,
|
|
73
|
+
): void {
|
|
74
|
+
seen.anyRead = true;
|
|
75
|
+
if (message.isError || isTruncatedRead(message.content)) return;
|
|
76
|
+
if (key.range === undefined) seen.fullSuccess = true;
|
|
77
|
+
else seen.successRanges.add(key.range);
|
|
78
|
+
}
|
|
79
|
+
|
|
85
80
|
function isTruncatedRead(content: ToolResultMessage["content"]): boolean {
|
|
86
81
|
return content.some(
|
|
87
82
|
(block) =>
|
package/src/tool-calls.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import type { ToolCall } from "@earendil-works/pi-ai";
|
|
2
2
|
import type { ProjectedSessionEntry } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { type EditableMessage, editableMessage } from "./context.ts";
|
|
4
|
+
|
|
5
|
+
export interface ToolResult {
|
|
6
|
+
id: string;
|
|
7
|
+
message: Extract<EditableMessage, { role: "toolResult" }>;
|
|
8
|
+
args: Record<string, unknown> | undefined;
|
|
9
|
+
}
|
|
3
10
|
|
|
4
11
|
/** Tool calls in the projected context, by tool call ID. */
|
|
5
|
-
|
|
12
|
+
function toolCallsById(
|
|
6
13
|
entries: readonly ProjectedSessionEntry[],
|
|
7
14
|
): Map<string, ToolCall> {
|
|
8
15
|
const calls = new Map<string, ToolCall>();
|
|
@@ -16,3 +23,17 @@ export function toolCallsById(
|
|
|
16
23
|
}
|
|
17
24
|
return calls;
|
|
18
25
|
}
|
|
26
|
+
|
|
27
|
+
/** Editable tool results, newest first, with the arguments of their tool call. */
|
|
28
|
+
export function* newestToolResults(
|
|
29
|
+
entries: readonly ProjectedSessionEntry[],
|
|
30
|
+
): Generator<ToolResult> {
|
|
31
|
+
const calls = toolCallsById(entries);
|
|
32
|
+
for (let i = entries.length - 1; i >= 0; i--) {
|
|
33
|
+
const entry = entries[i];
|
|
34
|
+
const message = entry && editableMessage(entry);
|
|
35
|
+
if (!entry || message?.role !== "toolResult") continue;
|
|
36
|
+
const args = calls.get(message.toolCallId)?.arguments;
|
|
37
|
+
yield { id: entry.sourceEntry.id, message, args };
|
|
38
|
+
}
|
|
39
|
+
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## Unreleased
|
|
4
|
-
|
|
5
|
-
- Send handoff note requests with the session's thinking level and tool choice, so Anthropic reuses the cached conversation instead of re-billing it; reject notes that call a tool.
|
|
6
|
-
- `soft` now writes Pi's summary at `thresholdTokens` or `thresholdPercent`, so an earlier threshold compacts even when no handoff note is available. Before, Pi compacted only at its own threshold.
|
|
7
|
-
|
|
8
|
-
## 0.2.0
|
|
9
|
-
|
|
10
|
-
- Keep older read results when a newer full or ranged read is truncated, count the replaced read in the stale suffix budget, and preserve user-role messages during shaking.
|
|
11
|
-
- Bound recall regex searches in a worker and propagate cancellation; keep regex syntax errors and result paging.
|
|
12
|
-
- Reject length-truncated or oversized handoff notes, validate retained-context budgets, and use only ready background notes during native, manual, and overflow compaction.
|
|
13
|
-
- Apply the configured strategy order at both custom and Pi thresholds, including while a background note is unfinished; scale the background-note lead with small thresholds.
|
|
14
|
-
- Preserve projected retained history after earlier compactions and context edits; keep Pi's token estimate when maintenance makes no edits.
|
|
15
|
-
- Clarify measured provider behavior, fallback paths, and metadata-only image recall.
|
|
16
|
-
|
|
17
|
-
- `contextKit` settings, named after the `compaction.*` settings of oh-my-pi: `enabled`, `thresholdTokens`, `thresholdPercent`, `methodOrder`, `asyncEnabled`, `supersedeReads`, and the prune and shake budgets.
|
|
18
|
-
- An earlier threshold compacts at the end of a turn, without interrupting the run.
|
|
19
|
-
- The background note starts one eighth of the threshold early, capped at 32,000 tokens.
|
|
20
|
-
- By default, a handoff note is tried before shaking.
|
|
21
|
-
- A failed threshold note is not retried until the next compaction.
|
|
22
|
-
|
|
23
|
-
## 0.1.0
|
|
24
|
-
|
|
25
|
-
- Superseded read replacement at the end of each turn.
|
|
26
|
-
- Pruning of old tool outputs and elision of old blocks and images above the compaction threshold.
|
|
27
|
-
- Handoff compaction from the live context, with a background note near the threshold.
|
|
28
|
-
- `recall` tool for the full session history.
|