pi-tuck 1.11.1 → 1.12.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/CHANGELOG.md +20 -0
- package/README.md +15 -3
- package/dist/src/build-id.d.ts +1 -1
- package/dist/src/build-id.js +1 -1
- package/dist/src/compaction.d.ts +15 -0
- package/dist/src/compaction.js +67 -11
- package/dist/src/kept-reads.js +4 -1
- package/package.json +1 -1
- package/src/build-id.ts +1 -1
- package/src/compaction.ts +55 -9
- package/src/kept-reads.ts +4 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,26 @@
|
|
|
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.12.0]
|
|
7
|
+
|
|
8
|
+
### Changed
|
|
9
|
+
|
|
10
|
+
- Reply cap: on a Chat Completions reasoning model, each request (other than the act-now turn) asks for at most twice the largest reply
|
|
11
|
+
this model wrote on the branch, finished or length-stopped, at least 32,768 tokens, and never more than `maxTokens` or what Pi sends.
|
|
12
|
+
A large `maxTokens` no longer needs a hand-set lower value. A thinking-only stop at this cap is a full-cap stop: the act-now turn
|
|
13
|
+
follows, and the stopped output raises the next cap. The `llamaContinuation` model keeps its configured cap.
|
|
14
|
+
- Thinking level: when Pi sends `reasoning_effort` only in the chat template arguments and the server lists thinking levels, pi-tuck
|
|
15
|
+
also sends it at the top level.
|
|
16
|
+
- Lean line: off for a model whose `models.json` input and cache-read prices are both 0. `cacheSensitiveModels` still turns it off for
|
|
17
|
+
any model.
|
|
18
|
+
|
|
19
|
+
## [1.11.2]
|
|
20
|
+
|
|
21
|
+
### Fixed
|
|
22
|
+
|
|
23
|
+
- Kept prompt outputs: when a user prompt names a command (for example "Run: telex read NAME") and the agent runs it as
|
|
24
|
+
`cd DIR && telex read NAME`, the output is now carried whole in the summary, as it already was without the `cd` prefix.
|
|
25
|
+
|
|
6
26
|
## [1.11.1]
|
|
7
27
|
|
|
8
28
|
### Fixed
|
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ pi install npm:pi-tuck
|
|
|
12
12
|
|
|
13
13
|
Start a new Pi process after installing or updating. pi-tuck handles automatic compaction and `/compact` by default. Run `/tuck` to see the loaded version, cut history, and active cut policy.
|
|
14
14
|
|
|
15
|
-
This README describes pi-tuck 1.
|
|
15
|
+
This README describes pi-tuck 1.12.0. To pin a published release, use a versioned source such as `pi install npm:pi-tuck@1.12.0` or `pi install git:github.com/galyavin/pi-tuck@v1.12.0`. To uninstall, run `pi remove` with the source you installed.
|
|
16
16
|
|
|
17
17
|
## Compaction and recall
|
|
18
18
|
|
|
@@ -38,6 +38,14 @@ Read the original by the hit's `entryId`, or use `{"index": 212}` to follow a `#
|
|
|
38
38
|
|
|
39
39
|
Pi-tuck needs a supported Pi version and a nonvirtual model with a valid context window. Setting `"compaction": { "enabled": false }` in Pi's settings disables automatic compaction and makes pi-tuck stand down, including for `/compact` and recall. `/tuck` reports why a loaded compactor is standing down. Capacity notices go to stderr in headless runs.
|
|
40
40
|
|
|
41
|
+
## Long runs
|
|
42
|
+
|
|
43
|
+
Three Pi agents worked for 72 hours on a hard research codebase, on one local model with a 272k window (later 230k). That came to 4,877 model requests and over 560M tokens of context.
|
|
44
|
+
|
|
45
|
+
Each time an agent filled its window, pi-tuck cut it back with no model call: 23 times in all. After a cut, pi-tuck kept about 20k tokens of conversation; with the system prompt and tools, the context was about 40k. Every agent kept working after every cut.
|
|
46
|
+
|
|
47
|
+
The run showed us a few rough edges; the [case study](docs/case-study-long-run.md) lists each one and what changed. We keep watching long runs like this one.
|
|
48
|
+
|
|
41
49
|
## Footer
|
|
42
50
|
|
|
43
51
|
In Pi's terminal UI, pi-tuck replaces Pi's footer. Under the folder and git branch, the bar fills toward pi-tuck's trim point, the number `/tuck` reports: a full bar means pi-tuck trims. Next to it are the context size and the trim point, the trim count once pi-tuck has trimmed, the cost when it is above $0, and the model with its thinking level. Other extensions' statuses stay below. The bar turns yellow at 85% of the trim point. A narrow terminal shows only the bar and the two sizes. Right after a trim, before the next reply, the size is the trim's own estimate, shown with `~`. When pi-tuck is not trimming for the model, the line shows the context against the window. `/tuck` shows the trim history.
|
|
@@ -58,7 +66,7 @@ Once per Pi start, in the background, pi-tuck reads the server's own model list
|
|
|
58
66
|
Optional settings go in `pi-tuck.json` in Pi's agent directory (`~/.pi/agent` by default, or `PI_CODING_AGENT_DIR`). Restart Pi to apply changes. The input room starts at the model's context window minus Pi's `compaction.reserveTokens`. Invalid JSON uses defaults. An invalid field uses its own default, with a notice when compaction is active.
|
|
59
67
|
|
|
60
68
|
- **`cutAt`** is a fraction above 0 and at most 1. By default pi-tuck cuts at the room, less a 2% margin. An explicit value fixes a fraction of the room, subject to the same margin even at `1`.
|
|
61
|
-
- **`cacheSensitiveModels`** lists exact `provider/model` identifiers. For these models, the cut point is the full room, taking precedence over `cutAt`. This delays early cuts that would discard a reusable prompt prefix. It also turns off the lean line for these models
|
|
69
|
+
- **`cacheSensitiveModels`** lists exact `provider/model` identifiers. For these models, the cut point is the full room, taking precedence over `cutAt`. This delays early cuts that would discard a reusable prompt prefix. It also turns off the lean line for these models. The lean line is also off, with no setting, for a model whose `models.json` input and cache-read prices are both 0 (a free or local model), where a carried token costs nothing.
|
|
62
70
|
- **`footer`** set to `false` keeps Pi's own footer.
|
|
63
71
|
- **`llamaContinuation`** opts one reasoning model into a single extra request after a thinking-only response exhausts the full configured output allowance. In Pi's `models.json`, configure that model on a llama.cpp provider using the `openai-completions` API. Verify that the runtime and template support `continue_final_message: "reasoning_content"` and report reused prompt tokens through `prompt_progress`. Pi-tuck requires the first cache report to match the prefix within 15 seconds. The positive integer `extraTokens` caps the additional allowance within the remaining context. The positive integer `deadlineMs` covers both requests.
|
|
64
72
|
|
|
@@ -75,7 +83,7 @@ Optional settings go in `pi-tuck.json` in Pi's agent directory (`~/.pi/agent` by
|
|
|
75
83
|
|
|
76
84
|
## Output exhaustion and small windows
|
|
77
85
|
|
|
78
|
-
- Pi-tuck preserves thinking-only stops in session history. At the model's full output allowance, it reports exhaustion if the reply remains unfinished. Below that allowance, it retries once, re-armed as Pi's overflow recovery is. After any length stop on the branch, pi-tuck trims so the next reply gets twice that stop's output (at most `maxTokens` and half the window). When such a stop is not retried, pi-tuck queues one more user turn asking the model to act: "Your last reply used the whole output budget on thinking. Act now: make the next tool call or give the answer, with brief reasoning." The stopped reply stays saved but leaves that request's context. On a Chat Completions model, that request alone goes out at the lowest thinking effort the server lists (pi-tuck reads the list at session start) and with at most 20,480 output tokens; a stop at that cap counts as a full-cap stop. At most two such turns go out in a row until a reply finishes, and only when the request fits and no message is already queued; if the second also stops, the notice below stands. A stop at the full `maxTokens` while still thinking names the setting and `models.json`; a lower thinking level usually helps more than a bigger cap.
|
|
86
|
+
- Pi-tuck preserves thinking-only stops in session history. At the model's full output allowance, it reports exhaustion if the reply remains unfinished. Below that allowance, it retries once, re-armed as Pi's overflow recovery is. After any length stop on the branch, pi-tuck trims so the next reply gets twice that stop's output (at most `maxTokens` and half the window). When such a stop is not retried, pi-tuck queues one more user turn asking the model to act: "Your last reply used the whole output budget on thinking. Act now: make the next tool call or give the answer, with brief reasoning." The stopped reply stays saved but leaves that request's context. On a Chat Completions model, that request alone goes out at the lowest thinking effort the server lists (pi-tuck reads the list at session start) and with at most 20,480 output tokens; a stop at that cap counts as a full-cap stop. At most two such turns go out in a row until a reply finishes, and only when the request fits and no message is already queued; if the second also stops, the notice below stands. A stop at the full `maxTokens` while still thinking names the setting and `models.json`; a lower thinking level usually helps more than a bigger cap. On a Chat Completions reasoning model, every other request asks for at most twice the largest reply this model has written on the branch (finished or length-stopped), at least 32,768 tokens and never more than `maxTokens` or what Pi sends, so a large `maxTokens` needs no hand-set lower value; a thinking-only stop at that cap takes the act-now path above, and its output raises the next cap. The continuation model (`llamaContinuation`) keeps its configured cap. When Pi sends a thinking level only inside the chat template arguments and the server lists thinking levels, pi-tuck also sends it at the top level, so no per-level sampling setting is needed.
|
|
79
87
|
- Pi-tuck uses the context window declared in Pi's model configuration. A length stop alone does not establish a smaller window. If the server has a smaller effective limit, set that limit in Pi's model configuration.
|
|
80
88
|
- Pi's compaction reserve sets the cut target. For larger requests, pi-tuck estimates reply needs from observed output on the current branch for the active model, bounded by the reserve and model output limit. A cut above the room aims to leave 3,584 reply tokens within those bounds; where no cut can, the request goes out as before. If cuts cannot leave enough room for the protected prompt and a reply, try a shorter prompt, fewer tools, or a larger window. Pi-tuck's checks use estimates. Pi handles recognized provider overflows, and silent truncation can go undetected.
|
|
81
89
|
|
|
@@ -126,6 +134,10 @@ See [Contributing](CONTRIBUTING.md) for stress tests and [the changelog](CHANGEL
|
|
|
126
134
|
|
|
127
135
|
The [benchmark index](benchmark/README.md) records the models, versions, plans, and scope of each result.
|
|
128
136
|
|
|
137
|
+
## Author
|
|
138
|
+
|
|
139
|
+
Rustem Galyavin · [galyavin.com](https://galyavin.com)
|
|
140
|
+
|
|
129
141
|
## Credits and license
|
|
130
142
|
|
|
131
143
|
MIT. pi-tuck grew out of [pi-vcc](https://github.com/sting8k/pi-vcc) and includes code from [pi-blackhole](https://github.com/k0valik/pi-blackhole) and [Pi](https://github.com/earendil-works/pi). See [NOTICE](NOTICE).
|
package/dist/src/build-id.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const BUILD_ID = "
|
|
1
|
+
export declare const BUILD_ID = "210d6264761e797d386437e785ce52781a630ff250d80b12160f987b5ba346ca";
|
package/dist/src/build-id.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const BUILD_ID = "
|
|
1
|
+
export const BUILD_ID = "210d6264761e797d386437e785ce52781a630ff250d80b12160f987b5ba346ca";
|
package/dist/src/compaction.d.ts
CHANGED
|
@@ -20,6 +20,21 @@ export declare function actNowPayload(payload: unknown, effort: string | undefin
|
|
|
20
20
|
payload: Record<string, unknown>;
|
|
21
21
|
cap?: number;
|
|
22
22
|
} | undefined;
|
|
23
|
+
/** Zero tuning (1.12.0): the reply cap a reasoning request asks for, never above what Pi sent. A reply this model wrote on the branch is
|
|
24
|
+
* evidence of the room it needs, so the cap is twice the largest one (finished or length-stopped), at least 32,768. A thinking-only stop at
|
|
25
|
+
* this cap takes the act-now path; its saved output then raises the next cap. Read from the log, so a restart or /tree keeps it. */
|
|
26
|
+
export declare const AUTO_CAP_FLOOR = 32768;
|
|
27
|
+
export declare function autoReplyCap(branch: readonly SessionEntry[], model: {
|
|
28
|
+
provider: string;
|
|
29
|
+
api: string;
|
|
30
|
+
id: string;
|
|
31
|
+
}): number;
|
|
32
|
+
/** Lower the reply cap fields to `cap` (when given) and copy a template-only reasoning effort to the top level (when the server listed
|
|
33
|
+
* efforts). `cap` in the result is set only when it lowered what Pi sent: a stop there is a full-cap stop. Undefined when nothing changes. */
|
|
34
|
+
export declare function zeroTuningPayload(payload: unknown, cap: number | undefined, mirrorEffort: boolean): {
|
|
35
|
+
payload: Record<string, unknown>;
|
|
36
|
+
cap?: number;
|
|
37
|
+
} | undefined;
|
|
23
38
|
/** Act-now turns on the branch since its last finished reply (stopReason "stop" or "toolUse"). Read from the log, so a restart, /tree
|
|
24
39
|
* or fork keeps the count; a length stop with content (a cut-off tool call) does not reset it. Operator messages do not reset it. */
|
|
25
40
|
export declare function actNowsInARow(branch: readonly SessionEntry[]): number;
|
package/dist/src/compaction.js
CHANGED
|
@@ -98,6 +98,48 @@ export function actNowPayload(payload, effort) {
|
|
|
98
98
|
}
|
|
99
99
|
return { payload: out, cap };
|
|
100
100
|
}
|
|
101
|
+
/** Zero tuning (1.12.0): the reply cap a reasoning request asks for, never above what Pi sent. A reply this model wrote on the branch is
|
|
102
|
+
* evidence of the room it needs, so the cap is twice the largest one (finished or length-stopped), at least 32,768. A thinking-only stop at
|
|
103
|
+
* this cap takes the act-now path; its saved output then raises the next cap. Read from the log, so a restart or /tree keeps it. */
|
|
104
|
+
export const AUTO_CAP_FLOOR = 32_768;
|
|
105
|
+
export function autoReplyCap(branch, model) {
|
|
106
|
+
let largest = 0;
|
|
107
|
+
for (const entry of branch) {
|
|
108
|
+
if (entry.type !== "message" || entry.message.role !== "assistant")
|
|
109
|
+
continue;
|
|
110
|
+
const message = entry.message;
|
|
111
|
+
if (message.model !== model.id || message.provider !== model.provider || message.api !== model.api)
|
|
112
|
+
continue;
|
|
113
|
+
if (message.stopReason !== "stop" && message.stopReason !== "toolUse" && message.stopReason !== "length")
|
|
114
|
+
continue;
|
|
115
|
+
const output = message.usage?.output;
|
|
116
|
+
if (typeof output === "number" && Number.isFinite(output) && output > largest)
|
|
117
|
+
largest = output;
|
|
118
|
+
}
|
|
119
|
+
return Math.max(AUTO_CAP_FLOOR, Math.ceil(2 * largest));
|
|
120
|
+
}
|
|
121
|
+
/** Lower the reply cap fields to `cap` (when given) and copy a template-only reasoning effort to the top level (when the server listed
|
|
122
|
+
* efforts). `cap` in the result is set only when it lowered what Pi sent: a stop there is a full-cap stop. Undefined when nothing changes. */
|
|
123
|
+
export function zeroTuningPayload(payload, cap, mirrorEffort) {
|
|
124
|
+
const body = payload;
|
|
125
|
+
if (!body || typeof body !== "object" || !Array.isArray(body.messages))
|
|
126
|
+
return undefined;
|
|
127
|
+
const out = { ...body };
|
|
128
|
+
let changed = false, lowered;
|
|
129
|
+
if (cap !== undefined)
|
|
130
|
+
for (const field of ["max_tokens", "max_completion_tokens"]) {
|
|
131
|
+
if (typeof out[field] === "number" && out[field] > cap) {
|
|
132
|
+
out[field] = lowered = cap;
|
|
133
|
+
changed = true;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const kwargs = out.chat_template_kwargs;
|
|
137
|
+
if (mirrorEffort && out.reasoning_effort === undefined && kwargs && typeof kwargs === "object" && typeof kwargs.reasoning_effort === "string") {
|
|
138
|
+
out.reasoning_effort = kwargs.reasoning_effort;
|
|
139
|
+
changed = true;
|
|
140
|
+
}
|
|
141
|
+
return changed ? { payload: out, cap: lowered } : undefined;
|
|
142
|
+
}
|
|
101
143
|
/** At most this many act-now turns in a row; after that a stall gets the cap-stop notice until a reply finishes (stop or toolUse). */
|
|
102
144
|
const ACT_NOW_LIMIT = 2;
|
|
103
145
|
/** Act-now turns on the branch since its last finished reply (stopReason "stop" or "toolUse"). Read from the log, so a restart, /tree
|
|
@@ -395,19 +437,30 @@ export async function installCompaction(pi, options = {}) {
|
|
|
395
437
|
if (ctx.model && `${ctx.model.provider}/${ctx.model.id}` !== options.continuationModel)
|
|
396
438
|
run?.rearmLengthRetry();
|
|
397
439
|
};
|
|
398
|
-
// The act-now request asks for a short act, so it goes out at the server's lowest effort with a smaller cap.
|
|
399
|
-
// at it is a full-cap stop, not a squeezed one.
|
|
400
|
-
|
|
440
|
+
// The act-now request asks for a short act, so it goes out at the server's lowest effort with a smaller cap. Other requests get the
|
|
441
|
+
// zero-tuning cap (reasoning models) and effort mirror. The cap pi-tuck set is kept: a stop at it is a full-cap stop, not a squeezed one.
|
|
442
|
+
// The continuation model keeps its configured cap: its continuation only follows a stop at exactly that cap.
|
|
443
|
+
let ownCap;
|
|
401
444
|
pi.on("before_provider_request", async (event, ctx) => {
|
|
445
|
+
ownCap = undefined;
|
|
402
446
|
const model = ctx.model;
|
|
403
|
-
if (model?.api !== "openai-completions"
|
|
404
|
-
|
|
447
|
+
if (model?.api !== "openai-completions")
|
|
448
|
+
return undefined;
|
|
449
|
+
const body = event.payload;
|
|
450
|
+
if (body && typeof body === "object" && body.continue_final_message !== undefined)
|
|
405
451
|
return undefined;
|
|
406
|
-
}
|
|
407
452
|
await pendingCheck;
|
|
408
|
-
const
|
|
409
|
-
|
|
410
|
-
|
|
453
|
+
const efforts = serverEfforts.get(`${model.provider}/${model.id}`);
|
|
454
|
+
if (actNowPayload(event.payload, undefined)) {
|
|
455
|
+
const rewritten = actNowPayload(event.payload, lowestEffort(efforts));
|
|
456
|
+
ownCap = rewritten.cap;
|
|
457
|
+
return rewritten.payload;
|
|
458
|
+
}
|
|
459
|
+
const cap = model.reasoning && `${model.provider}/${model.id}` !== options.continuationModel
|
|
460
|
+
? autoReplyCap(ctx.sessionManager.getBranch(), model) : undefined;
|
|
461
|
+
const rewritten = zeroTuningPayload(event.payload, cap, efforts !== undefined);
|
|
462
|
+
ownCap = rewritten?.cap;
|
|
463
|
+
return rewritten?.payload;
|
|
411
464
|
});
|
|
412
465
|
// The act-now turn is pi-tuck's own, not the operator's: it grants no retry.
|
|
413
466
|
pi.on("message_start", (event, ctx) => { if (event.message.role === "user" && textOf(event.message.content) !== ACT_NOW)
|
|
@@ -494,10 +547,13 @@ export async function installCompaction(pi, options = {}) {
|
|
|
494
547
|
};
|
|
495
548
|
// Lean line (lean.ts): once the branch has a cut and a measured growth rate, a cut also becomes due at K + sqrt(2(K + R)·δ / h), the
|
|
496
549
|
// economic order quantity of the context, when that is below the line above. Never later than it, and admission is unchanged: a cut it
|
|
497
|
-
// asks for is opportunistic, so one that cannot be planned is no cut. Off where the thrash line is off
|
|
550
|
+
// asks for is opportunistic, so one that cannot be planned is no cut. Off where the thrash line is off, and on a free model (input and
|
|
551
|
+
// cache-read prices both 0, 1.12.0): there a held token costs nothing, so an early cut only spends prefill time.
|
|
498
552
|
const leanLimit = (ctx) => {
|
|
499
553
|
if (options.cutAt !== undefined || options.requestReserve || !ctx.model || preservePrefix(ctx))
|
|
500
554
|
return undefined;
|
|
555
|
+
if (!ctx.model.cost?.input && !ctx.model.cost?.cacheRead)
|
|
556
|
+
return undefined;
|
|
501
557
|
return leanLine(ctx.sessionManager.getBranch());
|
|
502
558
|
};
|
|
503
559
|
// A cut the lean line makes due inside the band is sized by that line, not by the tenth-of-the-room rule for opportunistic cuts.
|
|
@@ -1288,7 +1344,7 @@ export async function installCompaction(pi, options = {}) {
|
|
|
1288
1344
|
if (event.message.role !== "assistant" || !ctx.model)
|
|
1289
1345
|
return;
|
|
1290
1346
|
const stoppedOutput = event.message.usage?.output;
|
|
1291
|
-
if (stoppedOutput >= (
|
|
1347
|
+
if (stoppedOutput >= (ownCap ?? ctx.model.maxTokens)) {
|
|
1292
1348
|
return { entries: event.entries, continue: false };
|
|
1293
1349
|
}
|
|
1294
1350
|
// Pi's supported boundary drafts commit before finishTurn honors `continue`. Omitting the selected failed response also prevents
|
package/dist/src/kept-reads.js
CHANGED
|
@@ -91,7 +91,10 @@ export function promptOutputCandidates(entries, indices, resolvable) {
|
|
|
91
91
|
for (const block of message.content) {
|
|
92
92
|
if (block.type !== "toolCall" || block.name !== "bash" || typeof block.arguments?.command !== "string")
|
|
93
93
|
continue;
|
|
94
|
-
const command = block.arguments.command
|
|
94
|
+
const command = block.arguments.command;
|
|
95
|
+
// A leading "cd DIR &&" is not what the prompt names; classifyBash already recognizes it.
|
|
96
|
+
const rest = classifyBash(command).directory === undefined ? command : command.slice(command.indexOf("&&") + 2);
|
|
97
|
+
const lead = words(rest.split(/[|;&<>]|\d>/u)[0]);
|
|
95
98
|
if (lead.includes(" ") && prompt.includes(lead))
|
|
96
99
|
named.set(block.id, command);
|
|
97
100
|
}
|
package/package.json
CHANGED
package/src/build-id.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const BUILD_ID = "
|
|
1
|
+
export const BUILD_ID = "210d6264761e797d386437e785ce52781a630ff250d80b12160f987b5ba346ca";
|
package/src/compaction.ts
CHANGED
|
@@ -97,6 +97,38 @@ export function actNowPayload(payload: unknown, effort: string | undefined): { p
|
|
|
97
97
|
}
|
|
98
98
|
return { payload: out, cap };
|
|
99
99
|
}
|
|
100
|
+
/** Zero tuning (1.12.0): the reply cap a reasoning request asks for, never above what Pi sent. A reply this model wrote on the branch is
|
|
101
|
+
* evidence of the room it needs, so the cap is twice the largest one (finished or length-stopped), at least 32,768. A thinking-only stop at
|
|
102
|
+
* this cap takes the act-now path; its saved output then raises the next cap. Read from the log, so a restart or /tree keeps it. */
|
|
103
|
+
export const AUTO_CAP_FLOOR = 32_768;
|
|
104
|
+
export function autoReplyCap(branch: readonly SessionEntry[], model: { provider: string; api: string; id: string }): number {
|
|
105
|
+
let largest = 0;
|
|
106
|
+
for (const entry of branch) {
|
|
107
|
+
if (entry.type !== "message" || entry.message.role !== "assistant") continue;
|
|
108
|
+
const message = entry.message;
|
|
109
|
+
if (message.model !== model.id || message.provider !== model.provider || message.api !== model.api) continue;
|
|
110
|
+
if (message.stopReason !== "stop" && message.stopReason !== "toolUse" && message.stopReason !== "length") continue;
|
|
111
|
+
const output = message.usage?.output;
|
|
112
|
+
if (typeof output === "number" && Number.isFinite(output) && output > largest) largest = output;
|
|
113
|
+
}
|
|
114
|
+
return Math.max(AUTO_CAP_FLOOR, Math.ceil(2 * largest));
|
|
115
|
+
}
|
|
116
|
+
/** Lower the reply cap fields to `cap` (when given) and copy a template-only reasoning effort to the top level (when the server listed
|
|
117
|
+
* efforts). `cap` in the result is set only when it lowered what Pi sent: a stop there is a full-cap stop. Undefined when nothing changes. */
|
|
118
|
+
export function zeroTuningPayload(payload: unknown, cap: number | undefined, mirrorEffort: boolean): { payload: Record<string, unknown>; cap?: number } | undefined {
|
|
119
|
+
const body = payload as Record<string, unknown> | null;
|
|
120
|
+
if (!body || typeof body !== "object" || !Array.isArray(body.messages)) return undefined;
|
|
121
|
+
const out: Record<string, unknown> = { ...body };
|
|
122
|
+
let changed = false, lowered: number | undefined;
|
|
123
|
+
if (cap !== undefined) for (const field of ["max_tokens", "max_completion_tokens"]) {
|
|
124
|
+
if (typeof out[field] === "number" && (out[field] as number) > cap) { out[field] = lowered = cap; changed = true; }
|
|
125
|
+
}
|
|
126
|
+
const kwargs = out.chat_template_kwargs as Record<string, unknown> | undefined;
|
|
127
|
+
if (mirrorEffort && out.reasoning_effort === undefined && kwargs && typeof kwargs === "object" && typeof kwargs.reasoning_effort === "string") {
|
|
128
|
+
out.reasoning_effort = kwargs.reasoning_effort; changed = true;
|
|
129
|
+
}
|
|
130
|
+
return changed ? { payload: out, cap: lowered } : undefined;
|
|
131
|
+
}
|
|
100
132
|
/** At most this many act-now turns in a row; after that a stall gets the cap-stop notice until a reply finishes (stop or toolUse). */
|
|
101
133
|
const ACT_NOW_LIMIT = 2;
|
|
102
134
|
/** Act-now turns on the branch since its last finished reply (stopReason "stop" or "toolUse"). Read from the log, so a restart, /tree
|
|
@@ -367,16 +399,28 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
|
|
|
367
399
|
const rearmLengthRetry = (ctx: ExtensionContext) => {
|
|
368
400
|
if (ctx.model && `${ctx.model.provider}/${ctx.model.id}` !== options.continuationModel) run?.rearmLengthRetry();
|
|
369
401
|
};
|
|
370
|
-
// The act-now request asks for a short act, so it goes out at the server's lowest effort with a smaller cap.
|
|
371
|
-
// at it is a full-cap stop, not a squeezed one.
|
|
372
|
-
|
|
402
|
+
// The act-now request asks for a short act, so it goes out at the server's lowest effort with a smaller cap. Other requests get the
|
|
403
|
+
// zero-tuning cap (reasoning models) and effort mirror. The cap pi-tuck set is kept: a stop at it is a full-cap stop, not a squeezed one.
|
|
404
|
+
// The continuation model keeps its configured cap: its continuation only follows a stop at exactly that cap.
|
|
405
|
+
let ownCap: number | undefined;
|
|
373
406
|
pi.on("before_provider_request", async (event, ctx) => {
|
|
407
|
+
ownCap = undefined;
|
|
374
408
|
const model = ctx.model;
|
|
375
|
-
if (model?.api !== "openai-completions"
|
|
409
|
+
if (model?.api !== "openai-completions") return undefined;
|
|
410
|
+
const body = event.payload as Record<string, unknown> | null;
|
|
411
|
+
if (body && typeof body === "object" && body.continue_final_message !== undefined) return undefined;
|
|
376
412
|
await pendingCheck;
|
|
377
|
-
const
|
|
378
|
-
|
|
379
|
-
|
|
413
|
+
const efforts = serverEfforts.get(`${model.provider}/${model.id}`);
|
|
414
|
+
if (actNowPayload(event.payload, undefined)) {
|
|
415
|
+
const rewritten = actNowPayload(event.payload, lowestEffort(efforts))!;
|
|
416
|
+
ownCap = rewritten.cap;
|
|
417
|
+
return rewritten.payload;
|
|
418
|
+
}
|
|
419
|
+
const cap = model.reasoning && `${model.provider}/${model.id}` !== options.continuationModel
|
|
420
|
+
? autoReplyCap(ctx.sessionManager.getBranch(), model) : undefined;
|
|
421
|
+
const rewritten = zeroTuningPayload(event.payload, cap, efforts !== undefined);
|
|
422
|
+
ownCap = rewritten?.cap;
|
|
423
|
+
return rewritten?.payload;
|
|
380
424
|
});
|
|
381
425
|
// The act-now turn is pi-tuck's own, not the operator's: it grants no retry.
|
|
382
426
|
pi.on("message_start", (event, ctx) => { if (event.message.role === "user" && textOf(event.message.content) !== ACT_NOW) rearmLengthRetry(ctx); });
|
|
@@ -454,9 +498,11 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
|
|
|
454
498
|
};
|
|
455
499
|
// Lean line (lean.ts): once the branch has a cut and a measured growth rate, a cut also becomes due at K + sqrt(2(K + R)·δ / h), the
|
|
456
500
|
// economic order quantity of the context, when that is below the line above. Never later than it, and admission is unchanged: a cut it
|
|
457
|
-
// asks for is opportunistic, so one that cannot be planned is no cut. Off where the thrash line is off
|
|
501
|
+
// asks for is opportunistic, so one that cannot be planned is no cut. Off where the thrash line is off, and on a free model (input and
|
|
502
|
+
// cache-read prices both 0, 1.12.0): there a held token costs nothing, so an early cut only spends prefill time.
|
|
458
503
|
const leanLimit = (ctx: ExtensionContext): number | undefined => {
|
|
459
504
|
if (options.cutAt !== undefined || options.requestReserve || !ctx.model || preservePrefix(ctx)) return undefined;
|
|
505
|
+
if (!ctx.model.cost?.input && !ctx.model.cost?.cacheRead) return undefined;
|
|
460
506
|
return leanLine(ctx.sessionManager.getBranch());
|
|
461
507
|
};
|
|
462
508
|
// A cut the lean line makes due inside the band is sized by that line, not by the tenth-of-the-room rule for opportunistic cuts.
|
|
@@ -1139,7 +1185,7 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
|
|
|
1139
1185
|
// Native Pi does not compact-and-retry output that reached the catalogue cap.
|
|
1140
1186
|
if (event.message.role !== "assistant" || !ctx.model) return;
|
|
1141
1187
|
const stoppedOutput = event.message.usage?.output;
|
|
1142
|
-
if (stoppedOutput >= (
|
|
1188
|
+
if (stoppedOutput >= (ownCap ?? ctx.model.maxTokens)) {
|
|
1143
1189
|
return { entries: event.entries, continue: false };
|
|
1144
1190
|
}
|
|
1145
1191
|
// Pi's supported boundary drafts commit before finishTurn honors `continue`. Omitting the selected failed response also prevents
|
package/src/kept-reads.ts
CHANGED
|
@@ -70,7 +70,10 @@ export function promptOutputCandidates(entries: readonly (SessionEntry | Session
|
|
|
70
70
|
if (message.role !== "assistant" || prompt === undefined || isDiscardedResponse(message)) continue;
|
|
71
71
|
for (const block of message.content) {
|
|
72
72
|
if (block.type !== "toolCall" || block.name !== "bash" || typeof (block.arguments as { command?: unknown })?.command !== "string") continue;
|
|
73
|
-
const command = (block.arguments as { command: string }).command
|
|
73
|
+
const command = (block.arguments as { command: string }).command;
|
|
74
|
+
// A leading "cd DIR &&" is not what the prompt names; classifyBash already recognizes it.
|
|
75
|
+
const rest = classifyBash(command).directory === undefined ? command : command.slice(command.indexOf("&&") + 2);
|
|
76
|
+
const lead = words(rest.split(/[|;&<>]|\d>/u)[0]!);
|
|
74
77
|
if (lead.includes(" ") && prompt.includes(lead)) named.set(block.id, command);
|
|
75
78
|
}
|
|
76
79
|
prompt = undefined;
|