pi-tuck 1.0.7 → 1.0.8
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/CHANGELOG.md +11 -0
- package/README.md +17 -17
- package/dist/src/build-id.d.ts +1 -1
- package/dist/src/build-id.js +1 -1
- package/dist/src/clip.d.ts +14 -0
- package/dist/src/clip.js +52 -0
- package/dist/src/compaction.js +84 -40
- package/dist/src/diagnostics.d.ts +1 -1
- package/dist/src/diagnostics.js +1 -1
- package/package.json +1 -1
- package/src/build-id.ts +1 -1
- package/src/clip.ts +47 -0
- package/src/compaction.ts +84 -48
- package/src/diagnostics.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to pi-tuck. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
|
|
4
4
|
[Semantic Versioning](https://semver.org/).
|
|
5
5
|
|
|
6
|
+
## [1.0.8] - 2026-11-01
|
|
7
|
+
|
|
8
|
+
### Fixed
|
|
9
|
+
|
|
10
|
+
- A fresh tool result too large for the room no longer refuses the run. The request carries the result's head and tail instead, with a
|
|
11
|
+
`tuck_recall` locator to the rest; a round of parallel results shares one clip budget. The clip ends with its run.
|
|
12
|
+
- A prompt right after a cut is no longer refused when Pi has nothing to compact for it. When a run ends at or above the cut band, pi-tuck
|
|
13
|
+
cuts there itself, so the next prompt starts below the band. A run-end cut that cannot be planned is no fault.
|
|
14
|
+
- Gates: on 29 replayed recordings, no task lost and no request refused, where 1.0.6 refused 8; live at a 32k window (60 tasks), no dead task
|
|
15
|
+
and no mid-run refusal, and 46 resolved, as 1.0.7 ([`benchmark/RESULTS-V108.md`](benchmark/RESULTS-V108.md)).
|
|
16
|
+
|
|
6
17
|
## [1.0.7] - 2026-11-01
|
|
7
18
|
|
|
8
19
|
### Changed
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# pi-tuck
|
|
2
2
|
|
|
3
|
-
**Compaction for [Pi](https://pi.dev) with no model call, and a history the agent can search by keyword
|
|
3
|
+
**Compaction for [Pi](https://pi.dev) with no model call, and a history the agent can search by keyword, and on Apple silicon by meaning, all on your machine.** In our live run, a cut took a median 0.09 s. It costs no tokens. The same session always gives the same summary. And nothing is lost: the agent can read back anything earlier, word for word, and on Apple silicon it can find it by meaning.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
pi install npm:pi-tuck
|
|
@@ -17,14 +17,13 @@ Pi's built-in compaction asks a model to write a summary. pi-tuck builds the sum
|
|
|
17
17
|
| Context carried after a cut | 29,200 tokens | 27,800 tokens (about the same) |
|
|
18
18
|
| Same session, same summary | Not guaranteed, since a model writes it | Yes |
|
|
19
19
|
| Can the agent get dropped detail back | No recall tool in Pi 1.4.0. The full history stays in the session file, but the agent has no tool made to search it | Yes. `tuck_recall` reads any original exactly |
|
|
20
|
-
| Search history by meaning | No | Yes, with `tuck_search`, on macOS with Apple silicon. It runs locally, and no text leaves your machine. Elsewhere
|
|
20
|
+
| Search history by meaning | No | Yes, with `tuck_search`, on macOS with Apple silicon. It runs locally on your Mac's GPU, and no text leaves your machine. Elsewhere there is no `tuck_search`, and `tuck_recall` searches by keyword |
|
|
21
21
|
| Can the summary invent history | It can, since a model writes it | No, it holds only recorded text |
|
|
22
22
|
| What the summary leaves out | Whatever the model dropped | Tool output only; the agent reads it back exactly |
|
|
23
23
|
| Billed input at the default 60% cut point (simulated) | Not measured | 10–18% less than pi-tuck cutting at the full room. This is not a comparison with the built-in |
|
|
24
|
-
|
|
|
25
|
-
| Answer quality against Pi's compaction prompts on a frontier model | | **Untested.** |
|
|
24
|
+
| Tasks resolved at a 32k window (150 SWE-bench Verified tasks, one model, pi-tuck 1.0.5) | 94 of 150 | 120 of 150 (+17.3 points, 95% lower bound +10.7). Limits below the table |
|
|
26
25
|
|
|
27
|
-
The time and context rows are live runs with a 131k window: 27 cuts per arm over 5 chains, interim.
|
|
26
|
+
The time and context rows are live runs with a 131k window: 27 cuts per arm over 5 chains, interim. The 32k row is one model, one window and five chains. Observation masking, a third approach, resolved 117 of the same 150 tasks (pi-tuck +2.0 points, lower bound −2.7: no difference shown). In the 32k run only pi-tuck was held to the 16,384-token room: Pi's built-in compaction sent a request over it in 97 of 150 tasks. Every number comes from a file in this repository. Conditions, sample sizes and the full limits are in [Benchmarks](#benchmarks).
|
|
28
27
|
|
|
29
28
|
## Ask your history
|
|
30
29
|
|
|
@@ -46,7 +45,7 @@ It gets back the original passage, with a pointer such as `#212`. It reads the w
|
|
|
46
45
|
|
|
47
46
|
That is `tuck_recall`. The agent answers from the original text, not from a model's memory of it.
|
|
48
47
|
|
|
49
|
-
*This example is an illustration. It is not a recorded session.* The call shapes are real. **Semantic search has no measured quality numbers yet.** A test of it is pre-registered in [`benchmark/PREREG-RECALL.md`](benchmark/PREREG-RECALL.md); it has no result yet. The benchmarks below measure cut time, cost and answer retention after cuts, not how well `tuck_search` finds a passage.
|
|
48
|
+
*This example is an illustration. It is not a recorded session.* The call shapes are real. **Semantic search has no measured quality numbers yet.** A test of it is pre-registered in [`benchmark/PREREG-RECALL.md`](benchmark/PREREG-RECALL.md); it has no result yet. The benchmarks below measure cut time, cost and answer retention after cuts, not how well `tuck_search` finds a passage. `tuck_search` exists only on macOS with Apple silicon. Elsewhere the agent has `tuck_recall` and its keyword search, and the example above would not find its match.
|
|
50
49
|
|
|
51
50
|
## Install
|
|
52
51
|
|
|
@@ -56,7 +55,7 @@ pi install npm:pi-tuck
|
|
|
56
55
|
|
|
57
56
|
Then start a new Pi session. That is all. pi-tuck takes over Pi's compaction, both automatic and `/compact`. You change no setting.
|
|
58
57
|
|
|
59
|
-
- Needs Pi 1.1 or later (tested on 1.1.0 and 1.4.0), on macOS or Linux.
|
|
58
|
+
- Needs Pi 1.1 or later (tested on 1.1.0 and 1.4.0), on macOS or Linux. Full features on macOS with Apple silicon; elsewhere, compaction and word recall.
|
|
60
59
|
- On macOS with Apple silicon, the first session also downloads a local search model in the background (about 390 MB, once). Until it is ready, search works by keyword. See [Semantic search](#semantic-search).
|
|
61
60
|
- Prefer GitHub? `pi install git:github.com/galyavin/pi-tuck@v1.0.7`.
|
|
62
61
|
- To pin a version, add it: `pi install npm:pi-tuck@1.0.7`.
|
|
@@ -77,9 +76,9 @@ The summary is plain text, one line per record, such as `#12 user @2026-10-30T14
|
|
|
77
76
|
The agent gets two tools to get anything back:
|
|
78
77
|
|
|
79
78
|
- **`tuck_recall`** finds earlier text by keyword. It reads any original message or tool result exactly, by the index shown in a summary. It can also write an original out to a file.
|
|
80
|
-
- **`tuck_search`** finds earlier history related to a question, even when the wording differs. It combines keyword hits with local semantic search and returns the original text with pointers for `tuck_recall`.
|
|
79
|
+
- **`tuck_search`** (macOS with Apple silicon only) finds earlier history related to a question, even when the wording differs. It combines keyword hits with local semantic search and returns the original text with pointers for `tuck_recall`.
|
|
81
80
|
|
|
82
|
-
|
|
81
|
+
On Apple silicon, both tools cost about 1,050 tokens of tool description per request. Elsewhere only `tuck_recall` is offered.
|
|
83
82
|
|
|
84
83
|
| Command | Does |
|
|
85
84
|
|---|---|
|
|
@@ -90,15 +89,15 @@ Both tools cost about 1,050 tokens of tool description per request.
|
|
|
90
89
|
|
|
91
90
|
## What is proven, and what is not
|
|
92
91
|
|
|
93
|
-
**Measured so far
|
|
92
|
+
**Measured so far.** In the live run at a 131k window, a pi-tuck cut took a median 0.09 s, where Pi's built-in compaction with a local model writing the summary took a median 269 s. A cut calls no model, so it costs no tokens. After a cut, pi-tuck carried about as many tokens as the built-in did, not fewer. In simulation, cutting at 60% of the room instead of 100% saved billed input for pi-tuck itself, and answers held in replay. At a 32k window, in one suite of 150 SWE-bench Verified tasks with one model, pi-tuck 1.0.5 resolved 120 tasks and Pi's built-in compaction 94 (+17.3 points, 95% lower bound +10.7). Observation masking resolved 117 (pi-tuck +2.0 points, lower bound −2.7: no difference shown). The limits of that result are in [Benchmarks](#benchmarks).
|
|
94
93
|
|
|
95
|
-
**Not
|
|
94
|
+
**Not claimed.** This README does not say that pi-tuck is as good as, or better than, Pi's built-in compaction at a 128k window or in any other setting than the one above. The tests were pre-registered before any result came in. The plans, and every result, including those that did not favour pi-tuck, are in [`benchmark/`](benchmark).
|
|
96
95
|
|
|
97
|
-
The
|
|
96
|
+
The pre-registered plan has three parts:
|
|
98
97
|
|
|
99
98
|
1. **A live run** at a 128k window on a local model, with pi-tuck 1.0.1. Ten chains of 30 SWE-bench Verified tasks per arm, with Pi's built-in compaction in one arm and pi-tuck in the other. Official grading.
|
|
100
|
-
2. **A paired replay with probe questions.** The same cuts, made by pi-tuck and by Pi's summarize prompts
|
|
101
|
-
3. **A 32k suite**, where tasks are bigger than the room, so cuts must summarise work in progress.
|
|
99
|
+
2. **A paired replay with probe questions.** The same cuts, made by pi-tuck and by Pi's summarize prompts, then questions about facts that fell before the cut. Pre-registered rule: the claim holds only if pi-tuck is no more than 3 points worse than the better compactor, with a 95% bound. The comparators are named in [`benchmark/PINS.md`](benchmark/PINS.md).
|
|
100
|
+
3. **A 32k suite**, where tasks are bigger than the room, so cuts must summarise work in progress. Its result is the 32k row above.
|
|
102
101
|
|
|
103
102
|
The plan: [`benchmark/PREREG.md`](benchmark/PREREG.md), [`benchmark/PREREG-32K.md`](benchmark/PREREG-32K.md), [`benchmark/PREREG-TERRA.md`](benchmark/PREREG-TERRA.md), and every change after the freeze in [`benchmark/DEVIATIONS.md`](benchmark/DEVIATIONS.md).
|
|
104
103
|
|
|
@@ -106,7 +105,7 @@ Known limits today:
|
|
|
106
105
|
|
|
107
106
|
- pi-tuck's summary does not explain why things happened. It holds the words that were said, not a model's reading of them. The agent must look things up. Each look-up costs a tool call.
|
|
108
107
|
- Small windows: an upstream that spends far more context on tool schemas than others (some OpenRouter upstreams do) can leave a small input room too little space, and pi-tuck then refuses to cut rather than overflow. This is what made 1.0.1 refuse at a 32k window with Pi's default 16,384 reserve. Since 1.0.4 the notice says when the provider is counting far above pi-tuck's estimate. In 1.0.4, after an oversize tool result or a "Nothing to compact" error from Pi, a session could keep refusing later prompts. 1.0.5 fixes both. Pin a different upstream, lower the reserve, or use a larger window; see [Small windows](#small-windows).
|
|
109
|
-
- Semantic search
|
|
108
|
+
- Semantic search (`tuck_search`) needs a Mac with Metal (Apple silicon). Elsewhere, compaction and `tuck_recall`'s word search work, there is no `tuck_search`, and nothing is downloaded.
|
|
110
109
|
|
|
111
110
|
## Benchmarks
|
|
112
111
|
|
|
@@ -114,6 +113,7 @@ Each row says what was measured, under which conditions, and where the data is.
|
|
|
114
113
|
|
|
115
114
|
| What | Result | Conditions | Source |
|
|
116
115
|
|---|---|---|---|
|
|
116
|
+
| Tasks resolved, 32k window | Pi's built-in compaction 94 of 150; pi-tuck 120 of 150 (+17.3 points, 95% lower bound +10.7); observation masking 117 of 150 (pi-tuck +2.0, lower bound −2.7: no difference shown). Turns per task: 9.4 (built-in), 24.0 (pi-tuck), 11.9 (masking). Tokens per session: 6.10 M, 6.39 M, 7.02 M. Spend per arm for the five sessions: $0.87, $0.55, $0.94 | pi-tuck 1.0.5 (commit `aa30824`, the 1.0.5 code; 1.0.6 and 1.0.7 are not measured). DeepSeek V4.1 Flash pinned to one upstream, window 32,768, reserve 16,384, 5 chains of 30 SWE-bench Verified tasks, paired by task, one-sided bootstrap over tasks. **Limits:** only pi-tuck is held to the 16,384-token room (the built-in sent a request over it in 97 of 150 tasks, masking in 73, so the window is nominal for them). The difference by chain is +10.0, −3.3, +50.0, +16.7 and +13.3 points; leaving out chain t03 it is +9.2. The bootstrap resamples tasks, not chains. The pi-tuck arm ran at a different time from the others. One model, one window. pi-tuck refused to cut mid-run in 5 tasks, where one fresh tool result was larger than the room; they count as unresolved. In the same setup pi-tuck 1.0.3 resolved 72 of 150, with 52 tasks lost because an earlier refusal carried forward; 1.0.5 fixes that | [`benchmark/RESULTS-32K-ACCEPT.md`](benchmark/RESULTS-32K-ACCEPT.md) |
|
|
117
117
|
| Cut time, live | pi-tuck median 0.087 s (90th percentile 0.205 s); Pi's built-in median 268.8 s (90th percentile 421.6 s). Context carried after a cut, median: 27,751 against 29,241 tokens | **Interim.** pi-tuck 1.0.1, Pi 1.4.0, SWE-bench Verified chains of 30 tasks, 131,072-token window, reply reserve 32,768, local model `mac-studio/main` writing Pi's summaries. 27 cuts per arm, 5 chains. The built-in time depends on that model and on server load. pi-tuck's time is an upper bound read from the session file | [`benchmark/RESULTS-INTERIM.md`](benchmark/RESULTS-INTERIM.md) section 1 |
|
|
118
118
|
| Cut time, paired replay | pi-tuck median 0.062 s (range 0.005 to 0.48 s); local-model summarizer 193.4 s (77.3 to 413.3 s); frontier summarizer 96.1 s (39.1 to 267.8 s; measured on the retired model `openai/gpt-6-astra`; feeds no test). Context carried after a cut, median: 28,006 (pi-tuck), 26,424 (local), 25,555 (frontier) tokens | **Interim.** The same recorded sessions cut at the same positions. pi-tuck: 10 recordings, 52 cuts. Summarizers: 6 recordings, 28 and 29 cuts. pi-tuck's time leaves out the background search index | [`benchmark/RESULTS-INTERIM.md`](benchmark/RESULTS-INTERIM.md) section 2 |
|
|
119
119
|
| Billed input, cutting at 60% of the room instead of 100% | −17.9% at 128k, −14.0% at 32k with automatic prompt caching; −17.0% and −9.6% with explicit cache breakpoints. Cuts go from 6 to 12 (128k) and from 84 to 157 (32k); none frees under a tenth of the room | Simulated billing, not provider bills. Prompt caching priced. Build `1bda64b` (the default shipped in 1.0.3). 5 recorded live sessions. 128k = 131,072 window, reserve 32,768. 32k = 32,768 window, reserve 16,384 | [`rnd/replay32/RESULT-lean.md`](rnd/replay32/RESULT-lean.md) |
|
|
@@ -139,7 +139,7 @@ pi-tuck needs none. Two are optional. Put them in `pi-tuck.json` in Pi's agent f
|
|
|
139
139
|
```
|
|
140
140
|
|
|
141
141
|
- **`cutAt`** (above 0, at most 1; default 0.6) is the part of the input room a request may fill before pi-tuck cuts. At 0.6, each cut leaves room to grow, so fewer tokens are carried and billed per request. `{ "cutAt": 1 }` cuts at the full room. A lower value cuts earlier and smaller. The whole room still decides which request pi-tuck refuses.
|
|
142
|
-
- **`semanticRecall`** (`false` turns semantic search off). Nothing is then downloaded, and `tuck_search` uses keywords only.
|
|
142
|
+
- **`semanticRecall`** (`false` turns semantic search off, on Apple silicon). Nothing is then downloaded, and `tuck_search` uses keywords only.
|
|
143
143
|
|
|
144
144
|
A bad file is reported at session start, and pi-tuck uses the defaults.
|
|
145
145
|
|
|
@@ -161,7 +161,7 @@ If you reach the model through OpenRouter, you can also pin one upstream, so tha
|
|
|
161
161
|
|
|
162
162
|
## Semantic search
|
|
163
163
|
|
|
164
|
-
Semantic search is on by default on macOS with Apple silicon. It runs entirely on your Mac: an embedding model, [EmbeddingGemma-2](https://huggingface.co/google/embeddinggemma-2), served by [llama.cpp](https://github.com/ggml-org/llama.cpp) on your Mac's GPU (Metal)
|
|
164
|
+
Semantic search is on by default on macOS with Apple silicon. It runs entirely on your Mac: an embedding model, [EmbeddingGemma-2](https://huggingface.co/google/embeddinggemma-2), served by [llama.cpp](https://github.com/ggml-org/llama.cpp) on your Mac's GPU (Metal). No text leaves your machine. Elsewhere (Linux, Intel Macs) there is no `tuck_search`, and nothing is downloaded; compaction and `tuck_recall` work. On a Mac where Metal fails to start, semantic recall goes off with the reason "Semantic recall needs Metal; word recall still works".
|
|
165
165
|
|
|
166
166
|
The first time a session starts, pi-tuck downloads what it needs, once, about 390 MB, in the background. Keyword search works meanwhile:
|
|
167
167
|
|
package/dist/src/build-id.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const BUILD_ID = "
|
|
1
|
+
export declare const BUILD_ID = "52dc04a544dfbc3cdf7e9dccd5f192350d56889968cf2bd4bd176405ab65a29a";
|
package/dist/src/build-id.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const BUILD_ID = "
|
|
1
|
+
export const BUILD_ID = "52dc04a544dfbc3cdf7e9dccd5f192350d56889968cf2bd4bd176405ab65a29a";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ToolResultMessage } from "@earendil-works/pi-ai";
|
|
2
|
+
/** What a request carries in place of one fresh tool result that the room cannot hold: its head and tail, and where the rest is. */
|
|
3
|
+
export interface Clip {
|
|
4
|
+
toolCallId: string;
|
|
5
|
+
content: ToolResultMessage["content"];
|
|
6
|
+
}
|
|
7
|
+
/** The round's text results within `budget` tokens together: each result smaller than an equal share of what is left stays whole, and the
|
|
8
|
+
* rest are clipped to that share, keeping the most head and tail characters that fit it. None when the round already fits, or when a share
|
|
9
|
+
* cannot hold even the locator. A round of one result gets the whole budget. Deterministic: the same round and budget always clip the same way. */
|
|
10
|
+
export declare function clipFor(results: readonly ToolResultMessage[], entryIdOf: (toolCallId: string) => string | undefined, budget: number): Clip[];
|
|
11
|
+
/** The messages with each clipped result replaced; the same array when nothing is clipped. */
|
|
12
|
+
export declare const applyClips: <M extends {
|
|
13
|
+
role: string;
|
|
14
|
+
}>(messages: M[], clips: ReadonlyMap<string, Clip>) => M[];
|
package/dist/src/clip.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { conversationTokens } from "./budget.js";
|
|
2
|
+
import { MAX_RECALL_CHARACTERS } from "./recall.js";
|
|
3
|
+
/** The text `tuck_recall {entryId, offset}` reads from a text-only result; any other result is not clipped. */
|
|
4
|
+
const textOf = (message) => message.content.every(block => block.type === "text")
|
|
5
|
+
? message.content.map(block => block.type === "text" ? block.text : "").join("\n") : undefined;
|
|
6
|
+
const clipped = (text, keep, entryId) => {
|
|
7
|
+
const head = Math.ceil(keep / 2), tail = keep - head;
|
|
8
|
+
return [{ type: "text", text: text.slice(0, head) +
|
|
9
|
+
`\n[pi-tuck: characters ${head}-${text.length - tail} of ${text.length} are not shown, to fit the context window; read them with ` +
|
|
10
|
+
`tuck_recall {"entryId":"${entryId}","offset":${head}}, up to ${MAX_RECALL_CHARACTERS} characters a call]\n` + text.slice(text.length - tail) }];
|
|
11
|
+
};
|
|
12
|
+
/** The round's text results within `budget` tokens together: each result smaller than an equal share of what is left stays whole, and the
|
|
13
|
+
* rest are clipped to that share, keeping the most head and tail characters that fit it. None when the round already fits, or when a share
|
|
14
|
+
* cannot hold even the locator. A round of one result gets the whole budget. Deterministic: the same round and budget always clip the same way. */
|
|
15
|
+
export function clipFor(results, entryIdOf, budget) {
|
|
16
|
+
const tokens = (message) => conversationTokens([message]);
|
|
17
|
+
const round = results.flatMap(result => {
|
|
18
|
+
const text = textOf(result), entryId = entryIdOf(result.toolCallId);
|
|
19
|
+
return text === undefined || !entryId ? [] : [{ result, text, entryId, size: tokens(result) }];
|
|
20
|
+
})
|
|
21
|
+
.sort((a, b) => a.size - b.size);
|
|
22
|
+
if (round.reduce((sum, item) => sum + item.size, 0) <= budget)
|
|
23
|
+
return [];
|
|
24
|
+
const clips = [];
|
|
25
|
+
let left = budget;
|
|
26
|
+
for (const [at, { result, text, entryId, size }] of round.entries()) {
|
|
27
|
+
const share = Math.floor(left / (round.length - at));
|
|
28
|
+
if (size <= share) {
|
|
29
|
+
left -= size;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const fits = (keep) => tokens({ ...result, content: clipped(text, keep, entryId) }) <= share;
|
|
33
|
+
if (!fits(0))
|
|
34
|
+
return [];
|
|
35
|
+
let low = 0, high = text.length;
|
|
36
|
+
while (low < high) {
|
|
37
|
+
const mid = Math.ceil((low + high) / 2);
|
|
38
|
+
if (fits(mid))
|
|
39
|
+
low = mid;
|
|
40
|
+
else
|
|
41
|
+
high = mid - 1;
|
|
42
|
+
}
|
|
43
|
+
const content = clipped(text, low, entryId);
|
|
44
|
+
clips.push({ toolCallId: result.toolCallId, content });
|
|
45
|
+
left -= tokens({ ...result, content });
|
|
46
|
+
}
|
|
47
|
+
return clips;
|
|
48
|
+
}
|
|
49
|
+
/** The messages with each clipped result replaced; the same array when nothing is clipped. */
|
|
50
|
+
export const applyClips = (messages, clips) => !clips.size ? messages
|
|
51
|
+
: messages.map(message => message.role === "toolResult" && clips.has(message.toolCallId)
|
|
52
|
+
? { ...message, content: clips.get(message.toolCallId).content } : message);
|
package/dist/src/compaction.js
CHANGED
|
@@ -5,6 +5,7 @@ import { exportSource } from "./source-export.js";
|
|
|
5
5
|
import { isDeepStrictEqual } from "node:util";
|
|
6
6
|
import { assertNativeToolRounds } from "./protocol.js";
|
|
7
7
|
import { projectedMessages } from "./omissions.js";
|
|
8
|
+
import { applyClips, clipFor } from "./clip.js";
|
|
8
9
|
import { DEFAULT_CUT_AT, bandOf, chooseCut, conversationTokens, decideCut, gateCut, incomingEventTokens, incomingTokens, measurableConversationTokens, planningReserve, projectedConversationTokens, requestTokens, systemPart, validCutAt } from "./budget.js";
|
|
9
10
|
import { isContextOverflow } from "@earendil-works/pi-ai";
|
|
10
11
|
import { convertToLlm, SessionManager, SettingsManager } from "@earendil-works/pi-coding-agent";
|
|
@@ -269,7 +270,7 @@ export async function installCompaction(pi, options = {}) {
|
|
|
269
270
|
learn(ratio);
|
|
270
271
|
});
|
|
271
272
|
pi.on("agent_start", () => { admittedRun = true; pending = undefined; phase = "idle"; });
|
|
272
|
-
pi.on("agent_settled", () => { admittedRun = false; pending = undefined; phase = "idle"; forgetFailure(); });
|
|
273
|
+
pi.on("agent_settled", () => { admittedRun = false; pending = undefined; phase = "idle"; forgetFailure(); clips.clear(); });
|
|
273
274
|
const active = () => options.active?.() ?? true;
|
|
274
275
|
const settingsFor = () => options.settings ?? SettingsManager.inMemory(pi.getSettings());
|
|
275
276
|
// The standalone default reserves the model's catalogue maximum output: a transport may omit the output cap, and then only that
|
|
@@ -298,10 +299,10 @@ export async function installCompaction(pi, options = {}) {
|
|
|
298
299
|
};
|
|
299
300
|
const fits = (tokens, limit, ctx) => corrected(tokens, ctx) < limit;
|
|
300
301
|
// The saved conversation and a cut's quantities are counted by the one function the guard counts with (strict, as the context hook is).
|
|
301
|
-
const measureFor = (ctx) => (messages) => projectedConversationTokens(messages, ctx, options.recallPolicy);
|
|
302
|
+
const measureFor = (ctx) => (messages) => projectedConversationTokens(applyClips(messages, clips), ctx, options.recallPolicy);
|
|
302
303
|
// The planner counts with the same function, except that it does not fail on a summary in another format: a cut can replace one
|
|
303
304
|
// (an operator's /compact repairs a session whose saved summary is foreign). Identical to the strict one for this compactor's summaries.
|
|
304
|
-
const plannerMeasureFor = (ctx) => (messages) => measurableConversationTokens(messages, ctx, options.recallPolicy);
|
|
305
|
+
const plannerMeasureFor = (ctx) => (messages) => measurableConversationTokens(applyClips(messages, clips), ctx, options.recallPolicy);
|
|
305
306
|
const hostTokens = (ctx) => options.hostMessages ? conversationTokens(options.hostMessages(ctx)) : 0;
|
|
306
307
|
const toolShapes = (names) => {
|
|
307
308
|
const selected = new Set(names);
|
|
@@ -315,6 +316,9 @@ export async function installCompaction(pi, options = {}) {
|
|
|
315
316
|
let pendingCut;
|
|
316
317
|
let failure;
|
|
317
318
|
let pendingBoundaryCommit = false;
|
|
319
|
+
// Fresh tool results the room cannot hold, by call id: each request of the run carries the clip in their place; the session keeps them
|
|
320
|
+
// whole. A clip ends with its run like any failure would: the next run plans from the session as it is, where the result is no longer fresh.
|
|
321
|
+
const clips = new Map();
|
|
318
322
|
const fail = (error, ctx) => {
|
|
319
323
|
recorder.stop("native-attempt-failed");
|
|
320
324
|
const notify = !failure && error instanceof CapacityError;
|
|
@@ -495,7 +499,7 @@ export async function installCompaction(pi, options = {}) {
|
|
|
495
499
|
}
|
|
496
500
|
});
|
|
497
501
|
pi.on("session_before_tree", (event) => event.preparation.userWantsSummary ? { cancel: true } : undefined);
|
|
498
|
-
pi.on("session_start", () => { recorder.stop("session-transition"); proposalId = undefined; failure = undefined; admittedRun = false; pendingBoundaryCommit = false; identity = ""; factor = 1; pending = undefined; phase = "idle"; });
|
|
502
|
+
pi.on("session_start", () => { clips.clear(); recorder.stop("session-transition"); proposalId = undefined; failure = undefined; admittedRun = false; pendingBoundaryCommit = false; identity = ""; factor = 1; pending = undefined; phase = "idle"; });
|
|
499
503
|
// The cut before a run. Pi 1.4 counts a run as started before before_agent_start, and ctx.compact() first aborts and waits for an
|
|
500
504
|
// idle session, so a cut asked for there would wait on its own run. A prompt's cut is therefore made on `input`, while the session
|
|
501
505
|
// is idle. before_agent_start decides only for a run no input started (one a custom message starts) and cuts only if the session is
|
|
@@ -571,30 +575,31 @@ export async function installCompaction(pi, options = {}) {
|
|
|
571
575
|
fail(error, ctx);
|
|
572
576
|
}
|
|
573
577
|
});
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
578
|
+
// A cut at a public boundary, drafted against the boundary's own preview: "tool-loop" between the rounds of a run, "run-end" once its
|
|
579
|
+
// last reply is in. A run-end cut is made only at or above the band, as a tool-loop one, so the next prompt starts from below it: an
|
|
580
|
+
// input cut is bound by Pi's keepRecentTokens, and a large prompt right after a cut could leave it nothing to cut. A run-end cut that
|
|
581
|
+
// cannot be planned is no fault: the run is over, and the next prompt decides afresh. Only a tool-loop boundary has fresh results to clip.
|
|
582
|
+
const boundaryCut = (event, ctx, reason) => {
|
|
583
|
+
access();
|
|
584
|
+
// Earlier boundary drafts have a public preview but no original-source compiler
|
|
585
|
+
// contract. Preserve them and leave admission to the final guard.
|
|
586
|
+
if (event.entries.length || !isDeepStrictEqual(event.context.contextMessages, ctx.sessionManager.buildSessionProjection().messages))
|
|
587
|
+
return;
|
|
588
|
+
if (!ctx.model)
|
|
589
|
+
return;
|
|
590
|
+
const { response, room, band } = limits(ctx);
|
|
591
|
+
const measure = measureFor(ctx), saved = event.context.contextMessages;
|
|
592
|
+
// One snapshot per decision. Messages queued for the next request (steering, follow-ups) are in that request but not in the
|
|
593
|
+
// saved conversation. Pi's peek returns the steering queue, else the follow-up queue; a follow-up is not in the very next
|
|
594
|
+
// request while tools still run, so it can be counted early: conservative, and nothing else would cut at that later boundary.
|
|
595
|
+
const system = systemPart(ctx.getSystemPrompt(), toolShapes(pi.getActiveTools()), saved, ctx.getSystemPrompt());
|
|
596
|
+
const host = hostTokens(ctx);
|
|
597
|
+
const queued = event.context.pendingMessages.length ? conversationTokens(event.context.pendingMessages) : 0;
|
|
598
|
+
const mandatory = system + host + queued;
|
|
599
|
+
const previousSummary = saved.find(message => message.role === "compactionSummary")?.summary;
|
|
600
|
+
const decide = () => {
|
|
595
601
|
const before = measure(saved);
|
|
596
|
-
|
|
597
|
-
const decision = decideCut({
|
|
602
|
+
return decideCut({
|
|
598
603
|
total: before + mandatory, mandatory, before, room, band, fits: (tokens, limit) => fits(tokens, limit, ctx),
|
|
599
604
|
unreduced: "Tool-loop preflight compaction did not reduce context within the estimated request budget",
|
|
600
605
|
plan: (target) => {
|
|
@@ -612,14 +617,44 @@ export async function installCompaction(pi, options = {}) {
|
|
|
612
617
|
return { draft: true, after: measure(messages), mode: draft.mode, compaction: draft.compaction, reserve };
|
|
613
618
|
},
|
|
614
619
|
});
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
620
|
+
};
|
|
621
|
+
let decision = decide();
|
|
622
|
+
// Only a request that would be refused is clipped. A fresh result is kept whole by every cut (it has not reached the model yet), so
|
|
623
|
+
// a round larger than the room refuses the run. In their place the request carries the results' heads and tails, together at most
|
|
624
|
+
// half of what the room leaves the conversation, each with a tuck_recall locator to its rest. When even that is refused the refusal stands.
|
|
625
|
+
if (decision.kind === "fault" && event.toolResults) {
|
|
626
|
+
const branch = ctx.sessionManager.getBranch();
|
|
627
|
+
const entryIdOf = (toolCallId) => branch.findLast(entry => entry.type === "message" && entry.message.role === "toolResult" && entry.message.toolCallId === toolCallId)?.id;
|
|
628
|
+
const round = clipFor(event.toolResults, entryIdOf, Math.floor((room - mandatory) / 2));
|
|
629
|
+
if (round.length) {
|
|
630
|
+
for (const clip of round)
|
|
631
|
+
clips.set(clip.toolCallId, clip);
|
|
632
|
+
const clippedDecision = decide();
|
|
633
|
+
if (clippedDecision.kind === "fault")
|
|
634
|
+
for (const clip of round)
|
|
635
|
+
clips.delete(clip.toolCallId);
|
|
636
|
+
else
|
|
637
|
+
decision = clippedDecision;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
const before = measure(saved);
|
|
641
|
+
if (decision.kind === "fault" && reason === "tool-loop")
|
|
642
|
+
throw new (decision.capacity ? CapacityError : Refusal)(decision.message);
|
|
643
|
+
if (decision.kind !== "cut")
|
|
644
|
+
return;
|
|
645
|
+
const compaction = captureDraft(decision.plan.compaction, ctx, decision.plan.reserve, reason, before, true, false, previousSummary, { response, host, mandatory, target: decision.target, margin: room - band });
|
|
646
|
+
return { entries: [{ type: "compaction", summary: compaction.summary,
|
|
647
|
+
firstKeptEntryId: compaction.firstKeptEntryId, details: compaction.details }] };
|
|
648
|
+
};
|
|
649
|
+
pi.on("turn_end", (event, ctx) => {
|
|
650
|
+
try {
|
|
651
|
+
if (!active() || !admittedRun || event.outcome !== "completed" || ctx.signal?.aborted ||
|
|
652
|
+
!event.context.canContinue || !event.toolResults.length || !settingsFor().getCompactionSettings(ctx.model).enabled)
|
|
618
653
|
return;
|
|
619
|
-
const
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
654
|
+
const result = boundaryCut(event, ctx, "tool-loop");
|
|
655
|
+
if (result)
|
|
656
|
+
pendingBoundaryCommit = true;
|
|
657
|
+
return result;
|
|
623
658
|
}
|
|
624
659
|
catch (error) {
|
|
625
660
|
fail(error, ctx);
|
|
@@ -630,7 +665,7 @@ export async function installCompaction(pi, options = {}) {
|
|
|
630
665
|
if (!active())
|
|
631
666
|
return;
|
|
632
667
|
access();
|
|
633
|
-
return { messages: projectedMessages(event.messages, ctx, options.recallPolicy) };
|
|
668
|
+
return { messages: applyClips(projectedMessages(event.messages, ctx, options.recallPolicy), clips) };
|
|
634
669
|
}
|
|
635
670
|
catch (error) {
|
|
636
671
|
try {
|
|
@@ -665,12 +700,21 @@ export async function installCompaction(pi, options = {}) {
|
|
|
665
700
|
fail(new (event.fromExtension ? CommitFault : Error)(event.errorMessage ?? "Compaction failed"), ctx);
|
|
666
701
|
});
|
|
667
702
|
pi.on("agent_before_settle", (event, ctx) => {
|
|
668
|
-
if (
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
fail(new CommitFault("Native tool-loop boundary draft remained unconfirmed at error settlement"), ctx);
|
|
672
|
-
else
|
|
703
|
+
if (pendingBoundaryCommit) {
|
|
704
|
+
if (event.outcome === "error")
|
|
705
|
+
return void fail(new CommitFault("Native tool-loop boundary draft remained unconfirmed at error settlement"), ctx);
|
|
673
706
|
pendingBoundaryCommit = false;
|
|
707
|
+
}
|
|
708
|
+
// Pi commits these entries before the run settles.
|
|
709
|
+
try {
|
|
710
|
+
if (!active() || !admittedRun || event.outcome !== "completed" || ctx.signal?.aborted || failure ||
|
|
711
|
+
!settingsFor().getCompactionSettings(ctx.model).enabled)
|
|
712
|
+
return;
|
|
713
|
+
return boundaryCut(event, ctx, "run-end");
|
|
714
|
+
}
|
|
715
|
+
catch (error) {
|
|
716
|
+
fail(error, ctx);
|
|
717
|
+
}
|
|
674
718
|
});
|
|
675
719
|
pi.on("context_with_system", (event, ctx) => {
|
|
676
720
|
// Reaching the next request boundary proves native commit returned, not durable storage.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type RecallPolicy } from './recall.js';
|
|
2
2
|
import type { SessionEntry } from '@earendil-works/pi-coding-agent';
|
|
3
|
-
export declare const DIAGNOSTIC_BUILD = "
|
|
3
|
+
export declare const DIAGNOSTIC_BUILD = "52dc04a544dfbc3cdf7e9dccd5f192350d56889968cf2bd4bd176405ab65a29a";
|
|
4
4
|
export interface Receipt {
|
|
5
5
|
id: string;
|
|
6
6
|
build: string;
|
package/dist/src/diagnostics.js
CHANGED
|
@@ -8,7 +8,7 @@ import { createRecall } from './recall.js';
|
|
|
8
8
|
export const DIAGNOSTIC_BUILD = BUILD_ID;
|
|
9
9
|
export function attachReceipt(draft, reason, reserveTokens, contextWindow, resolved = {}) {
|
|
10
10
|
const details = draft.details && typeof draft.details === 'object' ? draft.details : {};
|
|
11
|
-
const receipt = { id: randomUUID(), build: DIAGNOSTIC_BUILD, reason, selectedView: typeof details.view === 'string' ? details.view : 'deterministic-selected-draft', boundary: draft.firstKeptEntryId, settings: { draftReservedTokens: reserveTokens, contextWindow, ...resolved }, before: { value: draft.tokensBefore, unit: 'estimated-input-tokens', scope: reason === 'tool-loop' ? 'public-conversation' : 'native-preparation' }, after: { value: typeof details.tokensAfter === 'number' ? details.tokensAfter : null, unit: 'estimated-input-tokens', scope: 'draft-preview' } };
|
|
11
|
+
const receipt = { id: randomUUID(), build: DIAGNOSTIC_BUILD, reason, selectedView: typeof details.view === 'string' ? details.view : 'deterministic-selected-draft', boundary: draft.firstKeptEntryId, settings: { draftReservedTokens: reserveTokens, contextWindow, ...resolved }, before: { value: draft.tokensBefore, unit: 'estimated-input-tokens', scope: reason === 'tool-loop' || reason === 'run-end' ? 'public-conversation' : 'native-preparation' }, after: { value: typeof details.tokensAfter === 'number' ? details.tokensAfter : null, unit: 'estimated-input-tokens', scope: 'draft-preview' } };
|
|
12
12
|
return { ...draft, details: { ...details, diagnosticReceipt: receipt } };
|
|
13
13
|
}
|
|
14
14
|
export function receiptId(entry) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-tuck",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "Deterministic compaction for Pi: compact summaries that keep the dialogue and the files touched, plus history recall by keyword and by meaning, all local.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
package/src/build-id.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const BUILD_ID = "
|
|
1
|
+
export const BUILD_ID = "52dc04a544dfbc3cdf7e9dccd5f192350d56889968cf2bd4bd176405ab65a29a";
|
package/src/clip.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ToolResultMessage } from "@earendil-works/pi-ai";
|
|
2
|
+
import { conversationTokens, type Messages } from "./budget.js";
|
|
3
|
+
import { MAX_RECALL_CHARACTERS } from "./recall.js";
|
|
4
|
+
|
|
5
|
+
/** What a request carries in place of one fresh tool result that the room cannot hold: its head and tail, and where the rest is. */
|
|
6
|
+
export interface Clip { toolCallId: string; content: ToolResultMessage["content"] }
|
|
7
|
+
|
|
8
|
+
/** The text `tuck_recall {entryId, offset}` reads from a text-only result; any other result is not clipped. */
|
|
9
|
+
const textOf = (message: ToolResultMessage) => message.content.every(block => block.type === "text")
|
|
10
|
+
? message.content.map(block => block.type === "text" ? block.text : "").join("\n") : undefined;
|
|
11
|
+
|
|
12
|
+
const clipped = (text: string, keep: number, entryId: string): ToolResultMessage["content"] => {
|
|
13
|
+
const head = Math.ceil(keep / 2), tail = keep - head;
|
|
14
|
+
return [{ type: "text", text: text.slice(0, head) +
|
|
15
|
+
`\n[pi-tuck: characters ${head}-${text.length - tail} of ${text.length} are not shown, to fit the context window; read them with ` +
|
|
16
|
+
`tuck_recall {"entryId":"${entryId}","offset":${head}}, up to ${MAX_RECALL_CHARACTERS} characters a call]\n` + text.slice(text.length - tail) }];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** The round's text results within `budget` tokens together: each result smaller than an equal share of what is left stays whole, and the
|
|
20
|
+
* rest are clipped to that share, keeping the most head and tail characters that fit it. None when the round already fits, or when a share
|
|
21
|
+
* cannot hold even the locator. A round of one result gets the whole budget. Deterministic: the same round and budget always clip the same way. */
|
|
22
|
+
export function clipFor(results: readonly ToolResultMessage[], entryIdOf: (toolCallId: string) => string | undefined, budget: number): Clip[] {
|
|
23
|
+
const tokens = (message: ToolResultMessage) => conversationTokens([message] as Messages);
|
|
24
|
+
const round = results.flatMap(result => { const text = textOf(result), entryId = entryIdOf(result.toolCallId);
|
|
25
|
+
return text === undefined || !entryId ? [] : [{ result, text, entryId, size: tokens(result) }]; })
|
|
26
|
+
.sort((a, b) => a.size - b.size);
|
|
27
|
+
if (round.reduce((sum, item) => sum + item.size, 0) <= budget) return [];
|
|
28
|
+
const clips: Clip[] = [];
|
|
29
|
+
let left = budget;
|
|
30
|
+
for (const [at, { result, text, entryId, size }] of round.entries()) {
|
|
31
|
+
const share = Math.floor(left / (round.length - at));
|
|
32
|
+
if (size <= share) { left -= size; continue; }
|
|
33
|
+
const fits = (keep: number) => tokens({ ...result, content: clipped(text, keep, entryId) }) <= share;
|
|
34
|
+
if (!fits(0)) return [];
|
|
35
|
+
let low = 0, high = text.length;
|
|
36
|
+
while (low < high) { const mid = Math.ceil((low + high) / 2); if (fits(mid)) low = mid; else high = mid - 1; }
|
|
37
|
+
const content = clipped(text, low, entryId);
|
|
38
|
+
clips.push({ toolCallId: result.toolCallId, content });
|
|
39
|
+
left -= tokens({ ...result, content });
|
|
40
|
+
}
|
|
41
|
+
return clips;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The messages with each clipped result replaced; the same array when nothing is clipped. */
|
|
45
|
+
export const applyClips = <M extends { role: string }>(messages: M[], clips: ReadonlyMap<string, Clip>): M[] => !clips.size ? messages
|
|
46
|
+
: messages.map(message => message.role === "toolResult" && clips.has((message as unknown as ToolResultMessage).toolCallId)
|
|
47
|
+
? { ...message, content: clips.get((message as unknown as ToolResultMessage).toolCallId)!.content } : message);
|
package/src/compaction.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { exportSource } from "./source-export.js";
|
|
|
5
5
|
import { isDeepStrictEqual } from "node:util";
|
|
6
6
|
import { assertNativeToolRounds } from "./protocol.js";
|
|
7
7
|
import { projectedMessages } from "./omissions.js";
|
|
8
|
+
import { applyClips, clipFor, type Clip } from "./clip.js";
|
|
8
9
|
import { DEFAULT_CUT_AT, bandOf, chooseCut, conversationTokens, decideCut, gateCut, incomingEventTokens, incomingTokens, measurableConversationTokens, planningReserve, projectedConversationTokens, requestTokens, systemPart, validCutAt,
|
|
9
10
|
type AgentMessage, type Messages, type Plan, type Target, type ToolShape } from "./budget.js";
|
|
10
11
|
import { isContextOverflow } from "@earendil-works/pi-ai";
|
|
@@ -240,7 +241,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
|
|
|
240
241
|
if (input > 0 && Number.isSafeInteger(input) && Number.isFinite(ratio)) learn(ratio);
|
|
241
242
|
});
|
|
242
243
|
pi.on("agent_start", () => { admittedRun = true; pending = undefined; phase = "idle"; });
|
|
243
|
-
pi.on("agent_settled", () => { admittedRun = false; pending = undefined; phase = "idle"; forgetFailure(); });
|
|
244
|
+
pi.on("agent_settled", () => { admittedRun = false; pending = undefined; phase = "idle"; forgetFailure(); clips.clear(); });
|
|
244
245
|
const active = () => options.active?.() ?? true;
|
|
245
246
|
const settingsFor = () => options.settings ?? SettingsManager.inMemory(pi.getSettings());
|
|
246
247
|
// The standalone default reserves the model's catalogue maximum output: a transport may omit the output cap, and then only that
|
|
@@ -270,10 +271,10 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
|
|
|
270
271
|
};
|
|
271
272
|
const fits = (tokens: number, limit: number, ctx: ExtensionContext) => corrected(tokens, ctx) < limit;
|
|
272
273
|
// The saved conversation and a cut's quantities are counted by the one function the guard counts with (strict, as the context hook is).
|
|
273
|
-
const measureFor = (ctx: ExtensionContext) => (messages: Messages) => projectedConversationTokens(messages, ctx, options.recallPolicy);
|
|
274
|
+
const measureFor = (ctx: ExtensionContext) => (messages: Messages) => projectedConversationTokens(applyClips(messages, clips), ctx, options.recallPolicy);
|
|
274
275
|
// The planner counts with the same function, except that it does not fail on a summary in another format: a cut can replace one
|
|
275
276
|
// (an operator's /compact repairs a session whose saved summary is foreign). Identical to the strict one for this compactor's summaries.
|
|
276
|
-
const plannerMeasureFor = (ctx: ExtensionContext) => (messages: Messages) => measurableConversationTokens(messages, ctx, options.recallPolicy);
|
|
277
|
+
const plannerMeasureFor = (ctx: ExtensionContext) => (messages: Messages) => measurableConversationTokens(applyClips(messages, clips), ctx, options.recallPolicy);
|
|
277
278
|
const hostTokens = (ctx: ExtensionContext) => options.hostMessages ? conversationTokens(options.hostMessages(ctx)) : 0;
|
|
278
279
|
const toolShapes = (names: Iterable<string>): ToolShape[] => {
|
|
279
280
|
const selected = new Set(names);
|
|
@@ -287,6 +288,9 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
|
|
|
287
288
|
let pendingCut: { mandatory: number; before: number; host: number; opportunistic: boolean; proposed: boolean } | undefined;
|
|
288
289
|
let failure: Error | undefined;
|
|
289
290
|
let pendingBoundaryCommit = false;
|
|
291
|
+
// Fresh tool results the room cannot hold, by call id: each request of the run carries the clip in their place; the session keeps them
|
|
292
|
+
// whole. A clip ends with its run like any failure would: the next run plans from the session as it is, where the result is no longer fresh.
|
|
293
|
+
const clips = new Map<string, Clip>();
|
|
290
294
|
const fail = (error: unknown, ctx: ExtensionContext) => {
|
|
291
295
|
recorder.stop("native-attempt-failed");
|
|
292
296
|
const notify = !failure && error instanceof CapacityError;
|
|
@@ -423,7 +427,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
|
|
|
423
427
|
}
|
|
424
428
|
});
|
|
425
429
|
pi.on("session_before_tree", (event) => event.preparation.userWantsSummary ? { cancel: true } : undefined);
|
|
426
|
-
pi.on("session_start", () => { recorder.stop("session-transition"); proposalId = undefined; failure = undefined; admittedRun = false; pendingBoundaryCommit = false; identity = ""; factor = 1; pending = undefined; phase = "idle"; });
|
|
430
|
+
pi.on("session_start", () => { clips.clear(); recorder.stop("session-transition"); proposalId = undefined; failure = undefined; admittedRun = false; pendingBoundaryCommit = false; identity = ""; factor = 1; pending = undefined; phase = "idle"; });
|
|
427
431
|
// The cut before a run. Pi 1.4 counts a run as started before before_agent_start, and ctx.compact() first aborts and waits for an
|
|
428
432
|
// idle session, so a cut asked for there would wait on its own run. A prompt's cut is therefore made on `input`, while the session
|
|
429
433
|
// is idle. before_agent_start decides only for a run no input started (one a custom message starts) and cuts only if the session is
|
|
@@ -483,57 +487,82 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
|
|
|
483
487
|
else await preflight(ctx, incomingEventTokens(event), event.systemPrompt, event.systemPromptOptions.selectedTools);
|
|
484
488
|
} catch (error) { fail(error, ctx); }
|
|
485
489
|
});
|
|
490
|
+
// A cut at a public boundary, drafted against the boundary's own preview: "tool-loop" between the rounds of a run, "run-end" once its
|
|
491
|
+
// last reply is in. A run-end cut is made only at or above the band, as a tool-loop one, so the next prompt starts from below it: an
|
|
492
|
+
// input cut is bound by Pi's keepRecentTokens, and a large prompt right after a cut could leave it nothing to cut. A run-end cut that
|
|
493
|
+
// cannot be planned is no fault: the run is over, and the next prompt decides afresh. Only a tool-loop boundary has fresh results to clip.
|
|
494
|
+
const boundaryCut = (event: { entries: readonly unknown[]; context: { contextMessages: Messages; pendingMessages: readonly unknown[] }; toolResults?: Parameters<typeof clipFor>[0] }, ctx: ExtensionContext, reason: "tool-loop" | "run-end") => {
|
|
495
|
+
access();
|
|
496
|
+
// Earlier boundary drafts have a public preview but no original-source compiler
|
|
497
|
+
// contract. Preserve them and leave admission to the final guard.
|
|
498
|
+
if (event.entries.length || !isDeepStrictEqual(event.context.contextMessages,
|
|
499
|
+
ctx.sessionManager.buildSessionProjection().messages)) return;
|
|
500
|
+
if (!ctx.model) return;
|
|
501
|
+
const { response, room, band } = limits(ctx);
|
|
502
|
+
const measure = measureFor(ctx), saved = event.context.contextMessages;
|
|
503
|
+
// One snapshot per decision. Messages queued for the next request (steering, follow-ups) are in that request but not in the
|
|
504
|
+
// saved conversation. Pi's peek returns the steering queue, else the follow-up queue; a follow-up is not in the very next
|
|
505
|
+
// request while tools still run, so it can be counted early: conservative, and nothing else would cut at that later boundary.
|
|
506
|
+
const system = systemPart(ctx.getSystemPrompt(), toolShapes(pi.getActiveTools()), saved, ctx.getSystemPrompt());
|
|
507
|
+
const host = hostTokens(ctx);
|
|
508
|
+
const queued = event.context.pendingMessages.length ? conversationTokens(event.context.pendingMessages as Messages) : 0;
|
|
509
|
+
const mandatory = system + host + queued;
|
|
510
|
+
const previousSummary = saved.find(message => message.role === "compactionSummary")?.summary;
|
|
511
|
+
const decide = () => {
|
|
512
|
+
const before = measure(saved);
|
|
513
|
+
return decideCut({
|
|
514
|
+
total: before + mandatory, mandatory, before, room, band, fits: (tokens, limit) => fits(tokens, limit, ctx),
|
|
515
|
+
unreduced: "Tool-loop preflight compaction did not reduce context within the estimated request budget",
|
|
516
|
+
plan: (target): Plan<{ compaction: NonNullable<ReturnType<typeof tuckObservedDraft>["compaction"]>; reserve: number }> => {
|
|
517
|
+
const reserve = planReserve(target, room, mandatory, ctx);
|
|
518
|
+
const draft = tuckObservedDraft(tuck, { branchEntries: ctx.sessionManager.getBranch(), tokensBefore: before, previousSummary }, ctx, reserve, plannerMeasureFor(ctx), options.recallPolicy, true);
|
|
519
|
+
if (!draft.compaction) return { draft: false, capacityRejected: Boolean(draft.capacityRejected), reason: draft.unavailable };
|
|
520
|
+
const header = ctx.sessionManager.getHeader();
|
|
521
|
+
if (!header) throw new Error("Missing native session header at tool-loop boundary");
|
|
522
|
+
const preview = SessionManager.inMemory(ctx.cwd, undefined, [header, ...ctx.sessionManager.getBranch()]);
|
|
523
|
+
preview.appendCompaction(draft.compaction.summary, draft.compaction.firstKeptEntryId, before, draft.compaction.details, true);
|
|
524
|
+
const messages = preview.buildSessionProjection().messages;
|
|
525
|
+
assertNativeToolRounds(messages);
|
|
526
|
+
return { draft: true, after: measure(messages), mode: draft.mode, compaction: draft.compaction, reserve };
|
|
527
|
+
},
|
|
528
|
+
});
|
|
529
|
+
};
|
|
530
|
+
let decision = decide();
|
|
531
|
+
// Only a request that would be refused is clipped. A fresh result is kept whole by every cut (it has not reached the model yet), so
|
|
532
|
+
// a round larger than the room refuses the run. In their place the request carries the results' heads and tails, together at most
|
|
533
|
+
// half of what the room leaves the conversation, each with a tuck_recall locator to its rest. When even that is refused the refusal stands.
|
|
534
|
+
if (decision.kind === "fault" && event.toolResults) {
|
|
535
|
+
const branch = ctx.sessionManager.getBranch();
|
|
536
|
+
const entryIdOf = (toolCallId: string) => branch.findLast(entry => entry.type === "message" && entry.message.role === "toolResult" && entry.message.toolCallId === toolCallId)?.id;
|
|
537
|
+
const round = clipFor(event.toolResults, entryIdOf, Math.floor((room - mandatory) / 2));
|
|
538
|
+
if (round.length) {
|
|
539
|
+
for (const clip of round) clips.set(clip.toolCallId, clip);
|
|
540
|
+
const clippedDecision = decide();
|
|
541
|
+
if (clippedDecision.kind === "fault") for (const clip of round) clips.delete(clip.toolCallId); else decision = clippedDecision;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
const before = measure(saved);
|
|
545
|
+
if (decision.kind === "fault" && reason === "tool-loop") throw new (decision.capacity ? CapacityError : Refusal)(decision.message);
|
|
546
|
+
if (decision.kind !== "cut") return;
|
|
547
|
+
const compaction = captureDraft(decision.plan.compaction, ctx, decision.plan.reserve, reason, before, true, false, previousSummary,
|
|
548
|
+
{ response, host, mandatory, target: decision.target, margin: room - band });
|
|
549
|
+
return { entries: [{ type: "compaction" as const, summary: compaction.summary,
|
|
550
|
+
firstKeptEntryId: compaction.firstKeptEntryId, details: compaction.details }] };
|
|
551
|
+
};
|
|
486
552
|
pi.on("turn_end", (event, ctx) => {
|
|
487
553
|
try {
|
|
488
554
|
if (!active() || !admittedRun || event.outcome !== "completed" || ctx.signal?.aborted ||
|
|
489
555
|
!event.context.canContinue || !event.toolResults.length || !settingsFor().getCompactionSettings(ctx.model).enabled) return;
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
if (event.entries.length || !isDeepStrictEqual(event.context.contextMessages,
|
|
494
|
-
ctx.sessionManager.buildSessionProjection().messages)) return;
|
|
495
|
-
if (!ctx.model) return;
|
|
496
|
-
const { response, room, band } = limits(ctx);
|
|
497
|
-
const measure = measureFor(ctx), saved = event.context.contextMessages;
|
|
498
|
-
// One snapshot per decision. Messages queued for the next request (steering, follow-ups) are in that request but not in the
|
|
499
|
-
// saved conversation. Pi's peek returns the steering queue, else the follow-up queue; a follow-up is not in the very next
|
|
500
|
-
// request while tools still run, so it can be counted early: conservative, and nothing else would cut at that later boundary.
|
|
501
|
-
const system = systemPart(ctx.getSystemPrompt(), toolShapes(pi.getActiveTools()), saved, ctx.getSystemPrompt());
|
|
502
|
-
const host = hostTokens(ctx);
|
|
503
|
-
const queued = event.context.pendingMessages.length ? conversationTokens(event.context.pendingMessages as Messages) : 0;
|
|
504
|
-
const mandatory = system + host + queued;
|
|
505
|
-
const before = measure(saved);
|
|
506
|
-
const previousSummary = saved.find(message => message.role === "compactionSummary")?.summary;
|
|
507
|
-
const decision = decideCut({
|
|
508
|
-
total: before + mandatory, mandatory, before, room, band, fits: (tokens, limit) => fits(tokens, limit, ctx),
|
|
509
|
-
unreduced: "Tool-loop preflight compaction did not reduce context within the estimated request budget",
|
|
510
|
-
plan: (target): Plan<{ compaction: NonNullable<ReturnType<typeof tuckObservedDraft>["compaction"]>; reserve: number }> => {
|
|
511
|
-
const reserve = planReserve(target, room, mandatory, ctx);
|
|
512
|
-
const draft = tuckObservedDraft(tuck, { branchEntries: ctx.sessionManager.getBranch(), tokensBefore: before, previousSummary }, ctx, reserve, plannerMeasureFor(ctx), options.recallPolicy, true);
|
|
513
|
-
if (!draft.compaction) return { draft: false, capacityRejected: Boolean(draft.capacityRejected), reason: draft.unavailable };
|
|
514
|
-
const header = ctx.sessionManager.getHeader();
|
|
515
|
-
if (!header) throw new Error("Missing native session header at tool-loop boundary");
|
|
516
|
-
const preview = SessionManager.inMemory(ctx.cwd, undefined, [header, ...ctx.sessionManager.getBranch()]);
|
|
517
|
-
preview.appendCompaction(draft.compaction.summary, draft.compaction.firstKeptEntryId, before, draft.compaction.details, true);
|
|
518
|
-
const messages = preview.buildSessionProjection().messages;
|
|
519
|
-
assertNativeToolRounds(messages);
|
|
520
|
-
return { draft: true, after: measure(messages), mode: draft.mode, compaction: draft.compaction, reserve };
|
|
521
|
-
},
|
|
522
|
-
});
|
|
523
|
-
if (decision.kind === "fault") throw new (decision.capacity ? CapacityError : Refusal)(decision.message);
|
|
524
|
-
if (decision.kind !== "cut") return;
|
|
525
|
-
const compaction = captureDraft(decision.plan.compaction, ctx, decision.plan.reserve, "tool-loop", before, true, false, previousSummary,
|
|
526
|
-
{ response, host, mandatory, target: decision.target, margin: room - band });
|
|
527
|
-
pendingBoundaryCommit = true;
|
|
528
|
-
return { entries: [{ type: "compaction" as const, summary: compaction.summary,
|
|
529
|
-
firstKeptEntryId: compaction.firstKeptEntryId, details: compaction.details }] };
|
|
556
|
+
const result = boundaryCut(event, ctx, "tool-loop");
|
|
557
|
+
if (result) pendingBoundaryCommit = true;
|
|
558
|
+
return result;
|
|
530
559
|
} catch (error) { fail(error, ctx); }
|
|
531
560
|
});
|
|
532
561
|
pi.on("context", (event, ctx) => {
|
|
533
562
|
try {
|
|
534
563
|
if (!active()) return;
|
|
535
564
|
access();
|
|
536
|
-
return { messages: projectedMessages(event.messages, ctx, options.recallPolicy) };
|
|
565
|
+
return { messages: applyClips(projectedMessages(event.messages, ctx, options.recallPolicy), clips) };
|
|
537
566
|
} catch (error) { try { fail(error, ctx); } finally { return { messages: [] }; } }
|
|
538
567
|
});
|
|
539
568
|
pi.on("session_compact_failed", (event, ctx) => {
|
|
@@ -554,9 +583,16 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
|
|
|
554
583
|
if (active() && !event.aborted) fail(new (event.fromExtension ? CommitFault : Error)(event.errorMessage ?? "Compaction failed"), ctx);
|
|
555
584
|
});
|
|
556
585
|
pi.on("agent_before_settle", (event, ctx) => {
|
|
557
|
-
if (
|
|
558
|
-
|
|
559
|
-
|
|
586
|
+
if (pendingBoundaryCommit) {
|
|
587
|
+
if (event.outcome === "error") return void fail(new CommitFault("Native tool-loop boundary draft remained unconfirmed at error settlement"), ctx);
|
|
588
|
+
pendingBoundaryCommit = false;
|
|
589
|
+
}
|
|
590
|
+
// Pi commits these entries before the run settles.
|
|
591
|
+
try {
|
|
592
|
+
if (!active() || !admittedRun || event.outcome !== "completed" || ctx.signal?.aborted || failure ||
|
|
593
|
+
!settingsFor().getCompactionSettings(ctx.model).enabled) return;
|
|
594
|
+
return boundaryCut(event, ctx, "run-end");
|
|
595
|
+
} catch (error) { fail(error, ctx); }
|
|
560
596
|
});
|
|
561
597
|
pi.on("context_with_system", (event, ctx) => {
|
|
562
598
|
// Reaching the next request boundary proves native commit returned, not durable storage.
|
package/src/diagnostics.ts
CHANGED
|
@@ -10,7 +10,7 @@ export const DIAGNOSTIC_BUILD = BUILD_ID;
|
|
|
10
10
|
export interface Receipt { id: string; build: string; reason: string; selectedView: string; boundary: string; settings: { draftReservedTokens: number; contextWindow: number; responseReserveTokens?: number; hostInputTokens?: number; mandatoryInputTokens?: number; target?: 'band' | 'room'; triggerMarginTokens?: number; cutAt?: number; calibrationFactor?: number; calibrationWindow?: number[]; calibrationIdentity?: string; activeInputCeilingTokens?: number | null }; before: { value: number; unit: 'estimated-input-tokens'; scope: 'native-preparation' | 'public-conversation' }; after: { value: number | null; unit: 'estimated-input-tokens'; scope: 'draft-preview' }; }
|
|
11
11
|
export function attachReceipt<T extends { details?: unknown; firstKeptEntryId: string; tokensBefore: number }>(draft: T, reason: string, reserveTokens: number, contextWindow: number, resolved: Partial<Receipt['settings']> = {}): T & { details: Record<string, unknown> & { diagnosticReceipt: Receipt } } {
|
|
12
12
|
const details = draft.details && typeof draft.details === 'object' ? draft.details as Record<string, unknown> : {};
|
|
13
|
-
const receipt: Receipt = { id: randomUUID(), build: DIAGNOSTIC_BUILD, reason, selectedView: typeof details.view === 'string' ? details.view : 'deterministic-selected-draft', boundary: draft.firstKeptEntryId, settings: { draftReservedTokens: reserveTokens, contextWindow, ...resolved }, before: { value: draft.tokensBefore, unit: 'estimated-input-tokens', scope: reason === 'tool-loop' ? 'public-conversation' : 'native-preparation' }, after: { value: typeof details.tokensAfter === 'number' ? details.tokensAfter : null, unit: 'estimated-input-tokens', scope: 'draft-preview' } };
|
|
13
|
+
const receipt: Receipt = { id: randomUUID(), build: DIAGNOSTIC_BUILD, reason, selectedView: typeof details.view === 'string' ? details.view : 'deterministic-selected-draft', boundary: draft.firstKeptEntryId, settings: { draftReservedTokens: reserveTokens, contextWindow, ...resolved }, before: { value: draft.tokensBefore, unit: 'estimated-input-tokens', scope: reason === 'tool-loop' || reason === 'run-end' ? 'public-conversation' : 'native-preparation' }, after: { value: typeof details.tokensAfter === 'number' ? details.tokensAfter : null, unit: 'estimated-input-tokens', scope: 'draft-preview' } };
|
|
14
14
|
return { ...draft, details: { ...details, diagnosticReceipt: receipt } };
|
|
15
15
|
}
|
|
16
16
|
export function receiptId(entry: { details?: unknown }): string | undefined {
|