pi-context-guard-jev 0.2.0 → 0.3.2
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 +111 -409
- package/docs/CACHE.md +5 -1
- package/docs/DESIGN.md +60 -24
- package/docs/REFERENCE.md +164 -0
- package/docs/RESULTS.md +146 -0
- package/docs/TESTING.md +29 -10
- package/package.json +2 -2
- package/src/checkpoint.ts +39 -16
- package/src/config.ts +8 -1
- package/src/images.ts +89 -0
- package/src/index.ts +87 -34
- package/src/items.ts +13 -14
- package/src/process.ts +45 -0
- package/src/recall.ts +44 -11
- package/src/render.ts +2 -0
- package/src/run.ts +71 -11
- package/src/size.ts +114 -0
- package/src/stats.ts +50 -18
- package/src/types.ts +2 -0
package/src/recall.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* or limited to a line window, truncated like Pi's `read` tool
|
|
2
|
+
* What the `recall` tool returns: the original tool output (with its images), optionally filtered by a
|
|
3
|
+
* regex or limited to a line window, truncated like Pi's `read` tool; or an omitted exchange as a
|
|
4
|
+
* transcript (images in its user messages attached, tool-result images named by their entry id).
|
|
4
5
|
*/
|
|
5
6
|
import vm from "node:vm";
|
|
6
7
|
import { MARKER } from "./render.ts";
|
|
8
|
+
import { isBranchSteering } from "./run.ts";
|
|
7
9
|
import { type Block, type SourceEntryLike, textOf } from "./types.ts";
|
|
8
10
|
|
|
9
11
|
export const RECALL_MAX_LINES = 2_000;
|
|
@@ -15,6 +17,9 @@ export const RECALL_MATCH_CHARS = 4_000;
|
|
|
15
17
|
/** Wall-clock limit for filtering by `pattern` (ms); stops catastrophic backtracking. */
|
|
16
18
|
export const RECALL_PATTERN_TIMEOUT_MS = 1_000;
|
|
17
19
|
|
|
20
|
+
/** Most images one recall returns (an exchange's user messages can hold more; the transcript says so). */
|
|
21
|
+
export const RECALL_MAX_IMAGES = 6;
|
|
22
|
+
|
|
18
23
|
export interface RecallParams {
|
|
19
24
|
entryId: string;
|
|
20
25
|
pattern?: string;
|
|
@@ -27,14 +32,30 @@ type RecallBranchEntry = { id?: string; type?: string; message?: { role?: string
|
|
|
27
32
|
/**
|
|
28
33
|
* The original text behind a context-guard stub: a tool result's output, or, for the user prompt of an
|
|
29
34
|
* omitted exchange, the whole exchange from the branch (prompt, assistant texts, tool calls and results,
|
|
30
|
-
* custom messages) up to the next
|
|
35
|
+
* custom messages, steering messages) up to the next prompt.
|
|
31
36
|
*/
|
|
32
37
|
export function originalOutput(entry: SourceEntryLike | undefined, entryId: string, branch: readonly RecallBranchEntry[] = [], omittedIds?: readonly string[]): string {
|
|
38
|
+
return originalContent(entry, entryId, branch, omittedIds).text;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const imagesOf = (content: unknown): Block[] => (Array.isArray(content) ? (content as Block[]).filter((b) => b?.type === "image") : []);
|
|
42
|
+
|
|
43
|
+
/** `originalOutput` plus the images that go with it: a tool result's images, or those of an exchange's user messages. */
|
|
44
|
+
export function originalContent(
|
|
45
|
+
entry: SourceEntryLike | undefined,
|
|
46
|
+
entryId: string,
|
|
47
|
+
branch: readonly RecallBranchEntry[] = [],
|
|
48
|
+
omittedIds?: readonly string[],
|
|
49
|
+
): { text: string; images: Block[] } {
|
|
33
50
|
const message = entry?.type === "message" ? entry.message : undefined;
|
|
34
|
-
if (message?.role === "toolResult") return textOf(message);
|
|
51
|
+
if (message?.role === "toolResult") return { text: textOf(message), images: imagesOf(message.content) };
|
|
35
52
|
if (message?.role === "user") {
|
|
36
53
|
const start = branch.findIndex((e) => e.id === entryId);
|
|
37
|
-
if (start >= 0)
|
|
54
|
+
if (start >= 0) {
|
|
55
|
+
const images: Block[] = [];
|
|
56
|
+
const text = exchangeTranscript(branch, start, omittedIds, images);
|
|
57
|
+
return { text, images };
|
|
58
|
+
}
|
|
38
59
|
}
|
|
39
60
|
throw new Error(`No tool result or omitted exchange with entry id ${entryId}. Use the id from a "${MARKER}" note.`);
|
|
40
61
|
}
|
|
@@ -57,16 +78,17 @@ export function omittedIdsFor(branch: readonly (RecallBranchEntry & { data?: unk
|
|
|
57
78
|
/**
|
|
58
79
|
* Plain-text transcript of the exchange that starts at branch[start] (a user message): with
|
|
59
80
|
* `omittedIds`, exactly the prompt and those entries (what the omission removed); otherwise everything up
|
|
60
|
-
* to the next user message.
|
|
81
|
+
* to the next user message that is not a steering message. Images: a tool result's are named by its entry
|
|
82
|
+
* id (recall it to see them); a user message's are appended to `images` (up to RECALL_MAX_IMAGES).
|
|
61
83
|
*/
|
|
62
|
-
export function exchangeTranscript(branch: readonly RecallBranchEntry[], start: number, omittedIds?: readonly string[]): string {
|
|
84
|
+
export function exchangeTranscript(branch: readonly RecallBranchEntry[], start: number, omittedIds?: readonly string[], images: Block[] = []): string {
|
|
63
85
|
const out: string[] = [];
|
|
64
86
|
const listed = omittedIds ? new Set(omittedIds) : undefined;
|
|
65
87
|
for (let i = start; i < branch.length; i++) {
|
|
66
88
|
const e = branch[i];
|
|
67
89
|
if (listed) {
|
|
68
90
|
if (i > start && !listed.has(e.id ?? "")) continue;
|
|
69
|
-
} else if (i > start && e.type === "message" && e.message?.role === "user") break;
|
|
91
|
+
} else if (i > start && e.type === "message" && e.message?.role === "user" && !isBranchSteering(branch, i)) break;
|
|
70
92
|
if (e.type === "custom_message") {
|
|
71
93
|
out.push(`## custom message (${e.customType ?? "custom"})`, textOf({ role: "custom", content: (e.content ?? "") as string }));
|
|
72
94
|
continue;
|
|
@@ -74,15 +96,26 @@ export function exchangeTranscript(branch: readonly RecallBranchEntry[], start:
|
|
|
74
96
|
if (e.type !== "message" || !e.message) continue;
|
|
75
97
|
const m = e.message;
|
|
76
98
|
const content = m.content as string | Block[];
|
|
77
|
-
|
|
78
|
-
|
|
99
|
+
const pictures = imagesOf(content);
|
|
100
|
+
if (m.role === "user") {
|
|
101
|
+
out.push("## user", textOf({ role: "user", content }));
|
|
102
|
+
if (pictures.length > 0) {
|
|
103
|
+
const room = Math.max(0, RECALL_MAX_IMAGES - images.length);
|
|
104
|
+
images.push(...pictures.slice(0, room));
|
|
105
|
+
const shown = Math.min(room, pictures.length);
|
|
106
|
+
out.push(shown === pictures.length ? `[${pictures.length} image(s), attached below]` : `[${pictures.length} image(s), ${shown} attached below]`);
|
|
107
|
+
}
|
|
108
|
+
} else if (m.role === "assistant") {
|
|
79
109
|
out.push("## assistant");
|
|
80
110
|
const text = textOf({ role: "assistant", content });
|
|
81
111
|
if (text.trim()) out.push(text);
|
|
82
112
|
if (Array.isArray(content)) {
|
|
83
113
|
for (const block of content) if (block.type === "toolCall") out.push(`[tool call] ${String(block.name)} ${JSON.stringify(block.arguments ?? {})}`);
|
|
84
114
|
}
|
|
85
|
-
} else if (m.role === "toolResult")
|
|
115
|
+
} else if (m.role === "toolResult") {
|
|
116
|
+
out.push(`## tool result (${m.toolName ?? "tool"})`, textOf({ role: "toolResult", content }));
|
|
117
|
+
if (pictures.length > 0) out.push(`[${pictures.length} image(s): recall({"entryId":"${e.id}"}) shows them]`);
|
|
118
|
+
}
|
|
86
119
|
}
|
|
87
120
|
return out.join("\n");
|
|
88
121
|
}
|
package/src/render.ts
CHANGED
|
@@ -8,6 +8,8 @@ import type { Candidate } from "./run.ts";
|
|
|
8
8
|
|
|
9
9
|
/** Every replacement starts with this; it also marks results as already distilled. */
|
|
10
10
|
export const MARKER = "[context-guard]";
|
|
11
|
+
/** Name of our recall tool. Its outputs are never judged mid-run: the model just asked for them. */
|
|
12
|
+
export const RECALL_TOOL = "recall";
|
|
11
13
|
|
|
12
14
|
export const formatChars = (chars: number): string =>
|
|
13
15
|
chars >= 1_000_000 ? `${(chars / 1_000_000).toFixed(1)}M` : chars >= 1_000 ? `${(chars / 1_000).toFixed(1)}k` : String(chars);
|
package/src/run.ts
CHANGED
|
@@ -61,25 +61,86 @@ export const HISTORY_FIRST_REQUEST_LIMIT = 1_000;
|
|
|
61
61
|
/** Question text used when the prompt that started the run has no text (e.g. only an image). */
|
|
62
62
|
export const NO_TEXT_QUESTION = "[the user sent only an image]";
|
|
63
63
|
|
|
64
|
+
const hasUser = (entry: ProjectedEntryLike): boolean => entry.messages.some((m) => m.role === "user");
|
|
65
|
+
|
|
66
|
+
/** An assistant message that handed over to tools (not stopped by an abort or error). */
|
|
67
|
+
export const endsInToolCall = (message: { stopReason?: unknown; content?: unknown }): boolean =>
|
|
68
|
+
message.stopReason !== "aborted" && message.stopReason !== "error" && Array.isArray(message.content) && message.content.some((b) => b?.type === "toolCall");
|
|
69
|
+
|
|
70
|
+
/** Index of the entry holding the last user message (prompt or steering message), or -1. */
|
|
71
|
+
export function lastUserIndex(entries: readonly ProjectedEntryLike[], before = entries.length): number {
|
|
72
|
+
let index = Math.min(before, entries.length) - 1;
|
|
73
|
+
while (index >= 0 && !hasUser(entries[index])) index--;
|
|
74
|
+
return index;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A user message sent while the agent was still working: the message before it (custom notices and
|
|
79
|
+
* omitted entries aside) is a tool result, or an assistant message with tool calls that did not stop on an
|
|
80
|
+
* abort or error. It continues the run. (A prompt typed after aborting a running tool looks the same and
|
|
81
|
+
* is treated as steering too: it usually redirects the same work.)
|
|
82
|
+
*/
|
|
83
|
+
export function isSteering(entries: readonly ProjectedEntryLike[], index: number): boolean {
|
|
84
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
85
|
+
const message = entries[i].messages.at(-1);
|
|
86
|
+
if (!message || message.role === "custom") continue;
|
|
87
|
+
if (message.role === "toolResult") return true;
|
|
88
|
+
if (message.role === "assistant") return endsInToolCall(message);
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
type BranchMessageEntry = { type?: string; message?: { role?: string; stopReason?: unknown; content?: unknown } };
|
|
95
|
+
|
|
96
|
+
/** `isSteering` on raw branch entries: is the user message at branch[index] a steering message? */
|
|
97
|
+
export function isBranchSteering(branch: readonly BranchMessageEntry[], index: number): boolean {
|
|
98
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
99
|
+
const e = branch[i];
|
|
100
|
+
if (e.type !== "message" || !e.message) continue;
|
|
101
|
+
if (e.message.role === "toolResult") return true;
|
|
102
|
+
if (e.message.role === "assistant") return endsInToolCall(e.message);
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
|
|
64
108
|
/**
|
|
65
|
-
*
|
|
66
|
-
* during
|
|
67
|
-
*
|
|
109
|
+
* Index of the entry holding the user message that starts the current run, or -1. Steering messages
|
|
110
|
+
* (sent during the run) do not start a run: the span goes back to the prompt the agent started from, so
|
|
111
|
+
* tool results from before a steering message are still judged (against the prompt and every steering
|
|
112
|
+
* message, see `runQuestion`).
|
|
68
113
|
*/
|
|
69
|
-
/** Index of the entry holding the user message that starts the current run, or -1. */
|
|
70
114
|
export function findRunStart(entries: readonly ProjectedEntryLike[]): number {
|
|
71
|
-
let start = entries
|
|
72
|
-
while (start
|
|
115
|
+
let start = lastUserIndex(entries);
|
|
116
|
+
while (start > 0 && isSteering(entries, start)) {
|
|
117
|
+
const previous = lastUserIndex(entries, start);
|
|
118
|
+
if (previous < 0) break;
|
|
119
|
+
start = previous;
|
|
120
|
+
}
|
|
73
121
|
return start;
|
|
74
122
|
}
|
|
75
123
|
|
|
76
|
-
/** The
|
|
124
|
+
/** The text of the user message at `start`. */
|
|
77
125
|
export function questionAt(entries: readonly ProjectedEntryLike[], start: number): string {
|
|
78
126
|
const questionMessage = entries[start].messages.findLast((m) => m.role === "user");
|
|
79
127
|
const questionText = questionMessage ? textOf(questionMessage).trim() : "";
|
|
80
128
|
return questionText ? textOf(questionMessage as MessageLike) : NO_TEXT_QUESTION;
|
|
81
129
|
}
|
|
82
130
|
|
|
131
|
+
export const STEERING_PREFIX = "[The user added during the run]";
|
|
132
|
+
|
|
133
|
+
/** The run's question: the prompt at `start`, then every steering message of the run in order. */
|
|
134
|
+
export function runQuestion(entries: readonly ProjectedEntryLike[], start: number): string {
|
|
135
|
+
const parts = [questionAt(entries, start)];
|
|
136
|
+
for (let i = start + 1; i < entries.length; i++) {
|
|
137
|
+
if (!hasUser(entries[i])) continue;
|
|
138
|
+
const text = textOf(entries[i].messages.findLast((m) => m.role === "user") as MessageLike).trim();
|
|
139
|
+
parts.push(`${STEERING_PREFIX} ${text || NO_TEXT_QUESTION}`);
|
|
140
|
+
}
|
|
141
|
+
return parts.join("\n\n");
|
|
142
|
+
}
|
|
143
|
+
|
|
83
144
|
export interface ToolCallInfo {
|
|
84
145
|
name: string;
|
|
85
146
|
args: Record<string, unknown> | undefined;
|
|
@@ -140,16 +201,15 @@ export function candidateOf(
|
|
|
140
201
|
}
|
|
141
202
|
|
|
142
203
|
/**
|
|
143
|
-
* The run starts after the
|
|
144
|
-
* during
|
|
145
|
-
* left for later (they stay unpruned, which is the safe direction).
|
|
204
|
+
* The run starts after the user message that started the agent (see `findRunStart`: steering messages
|
|
205
|
+
* sent during the run belong to it).
|
|
146
206
|
*
|
|
147
207
|
* `skip`: entry ids not to collect.
|
|
148
208
|
*/
|
|
149
209
|
export function collectRun(entries: readonly ProjectedEntryLike[], options: CollectOptions, skip?: ReadonlySet<string>): RunInfo | undefined {
|
|
150
210
|
const start = findRunStart(entries);
|
|
151
211
|
if (start < 0) return undefined;
|
|
152
|
-
const question =
|
|
212
|
+
const question = runQuestion(entries, start);
|
|
153
213
|
const span = entries.slice(start + 1);
|
|
154
214
|
const { toolCalls, assistantTexts } = indexSpan(span);
|
|
155
215
|
const answer = assistantTexts.at(-1)?.text ?? "";
|
package/src/size.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Size estimates for model context, in "text characters": the unit tool output is measured in, so the
|
|
3
|
+
* break-even rule (checkpoint.ts → paysOff) compares like with like.
|
|
4
|
+
*
|
|
5
|
+
* Blocks that are not plain text must not be counted by their serialized length:
|
|
6
|
+
* - images are base64 (a 1280×960 JPEG is ~110k characters) but cost about w×h/750 tokens on Anthropic
|
|
7
|
+
* (measured on a real Opus session: 1280×960 → ~1.6k tokens), so they are counted by their pixels, at
|
|
8
|
+
* 4 characters per token (Pi's own chars/4 estimate, so the footer shows image savings as tokens);
|
|
9
|
+
* - thinking blocks carry an encrypted signature that grows with the hidden reasoning; it is counted at
|
|
10
|
+
* half its length (measured: ~0.25 tokens per signature character vs ~0.4 per character of tool text).
|
|
11
|
+
*/
|
|
12
|
+
import { Buffer } from "node:buffer";
|
|
13
|
+
import type { Block } from "./types.ts";
|
|
14
|
+
|
|
15
|
+
/** Text characters per image token: Pi's chars/4 estimate (tool output itself runs at ~2.5–3). */
|
|
16
|
+
export const IMAGE_CHARS_PER_TOKEN = 4;
|
|
17
|
+
/** Anthropic's image token estimate: width × height / 750. */
|
|
18
|
+
export const IMAGE_PIXELS_PER_TOKEN = 750;
|
|
19
|
+
/** Tokens assumed for an image whose size cannot be read (about a 1.15-megapixel image). */
|
|
20
|
+
export const DEFAULT_IMAGE_TOKENS = 1_600;
|
|
21
|
+
/** Upper bound for one image (a 2576-pixel long edge at 4:3). */
|
|
22
|
+
export const MAX_IMAGE_TOKENS = 6_700;
|
|
23
|
+
/** Base64 characters decoded to find the image header (JPEG headers can sit behind EXIF data). */
|
|
24
|
+
const HEADER_BASE64_CHARS = 65_536;
|
|
25
|
+
|
|
26
|
+
/** Width and height from the header of a PNG, JPEG, GIF or WebP image, or undefined. */
|
|
27
|
+
export function imageSize(bytes: Uint8Array): { width: number; height: number } | undefined {
|
|
28
|
+
const b = bytes;
|
|
29
|
+
const u16be = (i: number) => (b[i] << 8) | b[i + 1];
|
|
30
|
+
const u16le = (i: number) => b[i] | (b[i + 1] << 8);
|
|
31
|
+
const u32be = (i: number) => ((b[i] << 24) >>> 0) + (b[i + 1] << 16) + (b[i + 2] << 8) + b[i + 3];
|
|
32
|
+
if (b.length >= 24 && b[0] === 0x89 && b[1] === 0x50 && b[2] === 0x4e && b[3] === 0x47) return { width: u32be(16), height: u32be(20) };
|
|
33
|
+
if (b.length >= 10 && b[0] === 0x47 && b[1] === 0x49 && b[2] === 0x46) return { width: u16le(6), height: u16le(8) };
|
|
34
|
+
if (b.length >= 30 && b[0] === 0x52 && b[1] === 0x49 && b[2] === 0x46 && b[3] === 0x46 && b[8] === 0x57 && b[9] === 0x45 && b[10] === 0x42 && b[11] === 0x50) {
|
|
35
|
+
const chunk = String.fromCharCode(b[12], b[13], b[14], b[15]);
|
|
36
|
+
if (chunk === "VP8X") return { width: 1 + (b[24] | (b[25] << 8) | (b[26] << 16)), height: 1 + (b[27] | (b[28] << 8) | (b[29] << 16)) };
|
|
37
|
+
if (chunk === "VP8L") return { width: 1 + (u16le(21) & 0x3fff), height: 1 + (((b[22] >> 6) | (b[23] << 2) | (b[24] << 10)) & 0x3fff) };
|
|
38
|
+
if (chunk === "VP8 ") return { width: u16le(26) & 0x3fff, height: u16le(28) & 0x3fff };
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
if (b.length >= 4 && b[0] === 0xff && b[1] === 0xd8) {
|
|
42
|
+
let i = 2;
|
|
43
|
+
while (i + 9 < b.length) {
|
|
44
|
+
if (b[i] !== 0xff) {
|
|
45
|
+
i++;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const marker = b[i + 1];
|
|
49
|
+
if (marker === 0xff) {
|
|
50
|
+
i++;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
// SOF0–SOF15 except DHT (C4), JPG (C8) and DAC (CC).
|
|
54
|
+
if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) return { width: u16be(i + 7), height: u16be(i + 5) };
|
|
55
|
+
if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {
|
|
56
|
+
i += 2;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
i += 2 + u16be(i + 2);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ImageInfo {
|
|
66
|
+
width?: number;
|
|
67
|
+
height?: number;
|
|
68
|
+
/** Estimated input tokens. */
|
|
69
|
+
tokens: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const imageInfoCache = new WeakMap<object, ImageInfo>();
|
|
73
|
+
|
|
74
|
+
/** Size (when the header is readable) and estimated input tokens of an image block (`{ type: "image", data: <base64> }`). */
|
|
75
|
+
export function imageInfo(block: Block): ImageInfo {
|
|
76
|
+
const cached = imageInfoCache.get(block);
|
|
77
|
+
if (cached) return cached;
|
|
78
|
+
let info: ImageInfo = { tokens: DEFAULT_IMAGE_TOKENS };
|
|
79
|
+
if (typeof block.data === "string") {
|
|
80
|
+
const head = block.data.slice(0, HEADER_BASE64_CHARS);
|
|
81
|
+
const size = imageSize(Buffer.from(head.slice(0, head.length - (head.length % 4)), "base64"));
|
|
82
|
+
if (size && size.width > 0 && size.height > 0) {
|
|
83
|
+
info = { ...size, tokens: Math.min(MAX_IMAGE_TOKENS, Math.max(1, Math.ceil((size.width * size.height) / IMAGE_PIXELS_PER_TOKEN))) };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
imageInfoCache.set(block, info);
|
|
87
|
+
return info;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Estimated input tokens of an image block. */
|
|
91
|
+
export const imageTokens = (block: Block): number => imageInfo(block).tokens;
|
|
92
|
+
|
|
93
|
+
/** Estimated size of one content block in text characters. */
|
|
94
|
+
export function blockChars(block: Block): number {
|
|
95
|
+
switch (block.type) {
|
|
96
|
+
case "text":
|
|
97
|
+
return typeof block.text === "string" ? block.text.length : 0;
|
|
98
|
+
case "image":
|
|
99
|
+
return imageTokens(block) * IMAGE_CHARS_PER_TOKEN;
|
|
100
|
+
case "thinking": {
|
|
101
|
+
const thinking = typeof block.thinking === "string" ? block.thinking.length : 0;
|
|
102
|
+
const signature = typeof block.thinkingSignature === "string" ? block.thinkingSignature.length : 0;
|
|
103
|
+
return thinking + Math.ceil(signature / 2);
|
|
104
|
+
}
|
|
105
|
+
case "toolCall":
|
|
106
|
+
return String(block.name ?? "").length + JSON.stringify(block.arguments ?? {}).length;
|
|
107
|
+
default:
|
|
108
|
+
return JSON.stringify(block).length;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Estimated size of message content in text characters. */
|
|
113
|
+
export const contentChars = (content: string | readonly Block[] | undefined): number =>
|
|
114
|
+
typeof content === "string" ? content.length : (content ?? []).reduce((n, block) => n + blockChars(block), 0);
|
package/src/stats.ts
CHANGED
|
@@ -3,11 +3,16 @@
|
|
|
3
3
|
* `/tree` navigation, reloads and compaction:
|
|
4
4
|
*
|
|
5
5
|
* - active savings: for every tool result in the current model context whose projected text
|
|
6
|
-
* is one of our replacements, original
|
|
7
|
-
* -
|
|
6
|
+
* is one of our replacements, original size minus replacement size (size.ts: images by pixels);
|
|
7
|
+
* - lifetime savings: summed from our `context-guard` records on the active branch. Compaction drops
|
|
8
|
+
* earlier edits from the context, so after one the footer shows both (`🛡 0 · Σ−79k`);
|
|
9
|
+
* - Jev usage: summed from the same records.
|
|
8
10
|
*/
|
|
11
|
+
import { imagesRemoved } from "./images.ts";
|
|
9
12
|
import { formatChars, MARKER } from "./render.ts";
|
|
10
13
|
import { EXCHANGE_STUB_PREFIX } from "./items.ts";
|
|
14
|
+
import { isBranchSteering } from "./run.ts";
|
|
15
|
+
import { contentChars } from "./size.ts";
|
|
11
16
|
import { type MessageLike, type ProjectedEntryLike, textOf } from "./types.ts";
|
|
12
17
|
|
|
13
18
|
export const CUSTOM_TYPE = "context-guard";
|
|
@@ -26,7 +31,7 @@ export interface RunRecordData {
|
|
|
26
31
|
timedOut: boolean;
|
|
27
32
|
results: {
|
|
28
33
|
/** Absent in records that only had large outputs. */
|
|
29
|
-
kind?: "large" | "small" | "exchange";
|
|
34
|
+
kind?: "large" | "small" | "exchange" | "image";
|
|
30
35
|
/** Omitted exchanges: the entry ids that got `replacement: null`. */
|
|
31
36
|
omitted?: string[];
|
|
32
37
|
entryId: string;
|
|
@@ -44,12 +49,19 @@ export interface RunRecordData {
|
|
|
44
49
|
export interface GuardStats {
|
|
45
50
|
/** Tool results currently distilled or omitted in model context (large and small outputs). */
|
|
46
51
|
distilledResults: number;
|
|
52
|
+
/** Tool results whose images are currently removed from model context. */
|
|
53
|
+
imageResults: number;
|
|
47
54
|
/** Old exchanges currently omitted from model context. */
|
|
48
55
|
omittedExchanges: number;
|
|
49
56
|
/** Characters currently kept out of model context. */
|
|
50
57
|
savedChars: number;
|
|
51
58
|
/** Rough token equivalent (Pi's own estimate: chars / 4). */
|
|
52
59
|
savedTokens: number;
|
|
60
|
+
/** Characters saved by every record on the branch, including edits a compaction has since dropped. */
|
|
61
|
+
lifetimeChars: number;
|
|
62
|
+
lifetimeTokens: number;
|
|
63
|
+
/** A compaction came after a record that saved something: `lifetime` is worth showing. */
|
|
64
|
+
compacted: boolean;
|
|
53
65
|
runs: number;
|
|
54
66
|
/** Mid-run checkpoints among `runs`. */
|
|
55
67
|
checkpoints: number;
|
|
@@ -62,8 +74,23 @@ export const estimateTokens = (chars: number): number => Math.round(chars / 4);
|
|
|
62
74
|
|
|
63
75
|
type StatsBranchEntry = { type?: string; id?: string; customType?: string; data?: unknown; message?: MessageLike };
|
|
64
76
|
|
|
77
|
+
const messageChars = (message: MessageLike): number => contentChars(message.content);
|
|
78
|
+
|
|
65
79
|
export function computeStats(projection: readonly ProjectedEntryLike[], branch: readonly StatsBranchEntry[]): GuardStats {
|
|
66
|
-
const stats: GuardStats = {
|
|
80
|
+
const stats: GuardStats = {
|
|
81
|
+
distilledResults: 0,
|
|
82
|
+
imageResults: 0,
|
|
83
|
+
omittedExchanges: 0,
|
|
84
|
+
savedChars: 0,
|
|
85
|
+
savedTokens: 0,
|
|
86
|
+
lifetimeChars: 0,
|
|
87
|
+
lifetimeTokens: 0,
|
|
88
|
+
compacted: false,
|
|
89
|
+
runs: 0,
|
|
90
|
+
checkpoints: 0,
|
|
91
|
+
requests: 0,
|
|
92
|
+
costUsd: 0,
|
|
93
|
+
};
|
|
67
94
|
// Pi keeps omitted entries in the projection with `messages: []`: count what the model sees.
|
|
68
95
|
const visibleMessages = new Map(projection.map((entry) => [entry.sourceEntry.id, entry.messages.length]));
|
|
69
96
|
for (const entry of projection) {
|
|
@@ -72,26 +99,26 @@ export function computeStats(projection: readonly ProjectedEntryLike[], branch:
|
|
|
72
99
|
if (entry.messages.length === 1 && projected?.role === "user" && raw && textOf(projected).startsWith(EXCHANGE_STUB_PREFIX) && !textOf(raw).startsWith(MARKER)) {
|
|
73
100
|
// An omitted exchange: its raw entries up to the next user message that the model no longer sees.
|
|
74
101
|
const start = branch.findIndex((e) => e.id === entry.sourceEntry.id);
|
|
75
|
-
let before =
|
|
102
|
+
let before = messageChars(raw);
|
|
76
103
|
for (let i = start + 1; start >= 0 && i < branch.length; i++) {
|
|
77
104
|
const e = branch[i];
|
|
78
|
-
if (e.type === "message" && e.message?.role === "user") break;
|
|
79
|
-
if (e.message && e.id && (visibleMessages.get(e.id) ?? 0) === 0) before +=
|
|
105
|
+
if (e.type === "message" && e.message?.role === "user" && !isBranchSteering(branch, i)) break;
|
|
106
|
+
if (e.message && e.id && (visibleMessages.get(e.id) ?? 0) === 0) before += messageChars(e.message);
|
|
80
107
|
}
|
|
81
108
|
stats.omittedExchanges++;
|
|
82
|
-
stats.savedChars += Math.max(0, before -
|
|
109
|
+
stats.savedChars += Math.max(0, before - messageChars(projected));
|
|
83
110
|
continue;
|
|
84
111
|
}
|
|
85
112
|
if (entry.messages.length !== 1 || projected?.role !== "toolResult" || !raw) continue;
|
|
86
|
-
|
|
87
|
-
if (
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
stats.distilledResults++;
|
|
91
|
-
stats.savedChars += Math.max(0, before.length - now.length);
|
|
113
|
+
if (!textOf(projected).startsWith(MARKER) || textOf(raw).startsWith(MARKER)) continue;
|
|
114
|
+
if (imagesRemoved(projected, raw)) stats.imageResults++;
|
|
115
|
+
else stats.distilledResults++;
|
|
116
|
+
stats.savedChars += Math.max(0, messageChars(raw) - messageChars(projected));
|
|
92
117
|
}
|
|
93
118
|
stats.savedTokens = estimateTokens(stats.savedChars);
|
|
119
|
+
let saving = false;
|
|
94
120
|
for (const entry of branch) {
|
|
121
|
+
if (entry.type === "compaction" && saving) stats.compacted = true;
|
|
95
122
|
if (entry.type !== "custom" || entry.customType !== CUSTOM_TYPE) continue;
|
|
96
123
|
const data = entry.data as Partial<RunRecordData> | undefined;
|
|
97
124
|
if (!data || data.v !== 1) continue;
|
|
@@ -99,8 +126,11 @@ export function computeStats(projection: readonly ProjectedEntryLike[], branch:
|
|
|
99
126
|
if (data.phase === "mid-run") stats.checkpoints++;
|
|
100
127
|
stats.requests += data.requests ?? 0;
|
|
101
128
|
stats.costUsd += data.costUsd ?? 0;
|
|
129
|
+
stats.lifetimeChars += Math.max(0, data.savedChars ?? 0);
|
|
130
|
+
if ((data.savedChars ?? 0) > 0) saving = true;
|
|
102
131
|
stats.last = data as RunRecordData;
|
|
103
132
|
}
|
|
133
|
+
stats.lifetimeTokens = estimateTokens(stats.lifetimeChars);
|
|
104
134
|
return stats;
|
|
105
135
|
}
|
|
106
136
|
|
|
@@ -117,10 +147,12 @@ export function formatStatus(stats: GuardStats, state: StatusState, color: Color
|
|
|
117
147
|
if (state === "off") return color("dim", `${icon} guard off`);
|
|
118
148
|
if (state === "busy") return color("accent", `${icon} distilling…`);
|
|
119
149
|
if (state === "problem") return color("warning", `${icon} ${problem ?? "guard unavailable"}`);
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
150
|
+
// After a compaction dropped earlier edits: "· Σ−79k", every saving of the session so far.
|
|
151
|
+
const lifetime = stats.compacted && stats.lifetimeTokens > stats.savedTokens ? color("dim", ` · Σ−${formatTokens(stats.lifetimeTokens)}`) : "";
|
|
152
|
+
if (stats.savedTokens === 0) return `${color("dim", `${icon} ${lifetime ? "0" : "0 saved"}`)}${lifetime}`;
|
|
153
|
+
const count = stats.distilledResults + stats.imageResults + stats.omittedExchanges;
|
|
154
|
+
// "🛡 −4.2k · 1": ≈ tokens kept out of context · edited items.
|
|
155
|
+
return `${color("success", `${icon} −${formatTokens(stats.savedTokens)}`)}${color("dim", ` · ${count}`)}${lifetime}`;
|
|
124
156
|
}
|
|
125
157
|
|
|
126
158
|
export const formatCost = (usd: number): string => (usd === 0 ? "$0" : usd < 0.01 ? `$${usd.toFixed(4)}` : `$${usd.toFixed(2)}`);
|
package/src/types.ts
CHANGED
|
@@ -16,6 +16,8 @@ export interface MessageLike {
|
|
|
16
16
|
toolName?: string;
|
|
17
17
|
toolCallId?: string;
|
|
18
18
|
isError?: boolean;
|
|
19
|
+
/** Assistant messages: why the model stopped ("toolUse", "stop", "aborted", "error", …). */
|
|
20
|
+
stopReason?: string;
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
export interface SourceEntryLike {
|