pi-tuck 1.10.0 → 1.10.1
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 +2 -2
- package/dist/src/build-id.d.ts +1 -1
- package/dist/src/build-id.js +1 -1
- package/dist/src/compaction.d.ts +4 -1
- package/dist/src/compaction.js +19 -7
- package/dist/src/diagnostics.d.ts +1 -1
- package/dist/src/epochs.d.ts +22 -0
- package/dist/src/epochs.js +52 -0
- package/dist/src/lean.js +13 -23
- package/dist/src/thrash.js +10 -7
- package/package.json +1 -1
- package/src/build-id.ts +1 -1
- package/src/compaction.ts +16 -6
- package/src/epochs.ts +52 -0
- package/src/lean.ts +12 -19
- package/src/thrash.ts +7 -3
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.10.1]
|
|
7
|
+
|
|
8
|
+
### Fixed
|
|
9
|
+
|
|
10
|
+
- Lean line: a cut planned when a prompt arrives, or by the request guard, is saved after the first reply that carried it. The lean line
|
|
11
|
+
took that drop in prompt size as negative growth, which pulled the line toward what the cut kept and could trigger cut after cut. Each
|
|
12
|
+
reply now counts under the cut it was sent with, and the line waits for a reply after a fresh cut. Re-reads after a cut count the same way.
|
|
13
|
+
- Act-now turn: the limit of two in a row now holds until a reply finishes (stop or tool use); a reply cut off in the middle of a tool call
|
|
14
|
+
no longer resets it. The count is read from the session, so it survives a restart and follows `/tree`. No act-now turn is sent when the
|
|
15
|
+
operator has already queued a message.
|
|
16
|
+
|
|
6
17
|
## [1.10.0]
|
|
7
18
|
|
|
8
19
|
### Added
|
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.10.
|
|
15
|
+
This README describes pi-tuck 1.10.1. To pin a published release, use a versioned source such as `pi install npm:pi-tuck@1.10.1` or `pi install git:github.com/galyavin/pi-tuck@v1.10.1`. To uninstall, run `pi remove` with the source you installed.
|
|
16
16
|
|
|
17
17
|
## Compaction and recall
|
|
18
18
|
|
|
@@ -75,7 +75,7 @@ Optional settings go in `pi-tuck.json` in Pi's agent directory (`~/.pi/agent` by
|
|
|
75
75
|
|
|
76
76
|
## Output exhaustion and small windows
|
|
77
77
|
|
|
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. At most two such turns go out in a row, and only when the request fits; 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.
|
|
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. 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.
|
|
79
79
|
- 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
80
|
- 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
81
|
|
package/dist/src/build-id.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const BUILD_ID = "
|
|
1
|
+
export declare const BUILD_ID = "6582ec23dfaf8ff94cbd7a5979c69569e3156024d0fc636de44b748f2ccab8ec";
|
package/dist/src/build-id.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const BUILD_ID = "
|
|
1
|
+
export const BUILD_ID = "6582ec23dfaf8ff94cbd7a5979c69569e3156024d0fc636de44b748f2ccab8ec";
|
package/dist/src/compaction.d.ts
CHANGED
|
@@ -12,8 +12,11 @@ export declare const overflowNotice: (contextWindow: number, bound: number, lear
|
|
|
12
12
|
export declare const capStopNotice: (model: string, maxTokens: number, file: string) => string;
|
|
13
13
|
/** The follow-up turn sent after a reply that spent its whole output on thinking, with no text or tool call. */
|
|
14
14
|
export declare const ACT_NOW = "Your last reply used the whole output budget on thinking. Act now: make the next tool call or give the answer, with brief reasoning.";
|
|
15
|
-
/** At most this many act-now turns in a row; after that a stall gets the cap-stop notice until a reply
|
|
15
|
+
/** 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). */
|
|
16
16
|
export declare const ACT_NOW_LIMIT = 2;
|
|
17
|
+
/** Act-now turns on the branch since its last finished reply (stopReason "stop" or "toolUse"). Read from the log, so a restart, /tree
|
|
18
|
+
* 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. */
|
|
19
|
+
export declare function actNowsInARow(branch: readonly SessionEntry[]): number;
|
|
17
20
|
export declare const INCIDENT_HINT = "\npi-tuck: something went wrong - run /tuck-incident next to record it for a bug report.";
|
|
18
21
|
/** The most a seed or an overflow raise may set the calibration factor to. A paired reply's own ratio is not bounded by it. */
|
|
19
22
|
export declare const CALIBRATION_CEILING = 1.5;
|
package/dist/src/compaction.js
CHANGED
|
@@ -69,8 +69,24 @@ export const capStopNotice = (model, maxTokens, file) => `pi-tuck: ${model} used
|
|
|
69
69
|
"The unfinished response is saved in the session.";
|
|
70
70
|
/** The follow-up turn sent after a reply that spent its whole output on thinking, with no text or tool call. */
|
|
71
71
|
export const ACT_NOW = "Your last reply used the whole output budget on thinking. Act now: make the next tool call or give the answer, with brief reasoning.";
|
|
72
|
-
/** At most this many act-now turns in a row; after that a stall gets the cap-stop notice until a reply
|
|
72
|
+
/** 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). */
|
|
73
73
|
export const ACT_NOW_LIMIT = 2;
|
|
74
|
+
/** Act-now turns on the branch since its last finished reply (stopReason "stop" or "toolUse"). Read from the log, so a restart, /tree
|
|
75
|
+
* 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. */
|
|
76
|
+
export function actNowsInARow(branch) {
|
|
77
|
+
let count = 0;
|
|
78
|
+
for (let i = branch.length - 1; i >= 0; i--) {
|
|
79
|
+
const entry = branch[i];
|
|
80
|
+
if (entry.type !== "message")
|
|
81
|
+
continue;
|
|
82
|
+
const message = entry.message;
|
|
83
|
+
if (message.role === "assistant" && (message.stopReason === "stop" || message.stopReason === "toolUse"))
|
|
84
|
+
break;
|
|
85
|
+
if (message.role === "user" && textOf(message.content) === ACT_NOW)
|
|
86
|
+
count++;
|
|
87
|
+
}
|
|
88
|
+
return count;
|
|
89
|
+
}
|
|
74
90
|
/** Pi's models.json, shown with ~ for the home folder. */
|
|
75
91
|
function modelsPath() {
|
|
76
92
|
try {
|
|
@@ -346,13 +362,9 @@ export async function installCompaction(pi, options = {}) {
|
|
|
346
362
|
// The act-now turn is pi-tuck's own, not the operator's: it grants no retry.
|
|
347
363
|
pi.on("message_start", (event, ctx) => { if (event.message.role === "user" && textOf(event.message.content) !== ACT_NOW)
|
|
348
364
|
rearmLengthRetry(ctx); });
|
|
349
|
-
// Act-now turns sent since the last reply that was not a stall.
|
|
350
|
-
let actNows = 0;
|
|
351
365
|
pi.on("message_end", (event, ctx) => {
|
|
352
366
|
if (event.message.role !== "assistant")
|
|
353
367
|
return;
|
|
354
|
-
if (event.message.stopReason !== "error" && event.message.stopReason !== "aborted" && !(event.message.content && thinkingOnlyLength(event.message)))
|
|
355
|
-
actNows = 0;
|
|
356
368
|
if (event.message.stopReason !== "error" && event.message.stopReason !== "length")
|
|
357
369
|
rearmLengthRetry(ctx);
|
|
358
370
|
lengthStop = event.message.stopReason === "length" ? {
|
|
@@ -1149,7 +1161,8 @@ export async function installCompaction(pi, options = {}) {
|
|
|
1149
1161
|
// After a stall that is not retried: leave the stalled reply out of the context (it stays saved, with its output usage, so the next
|
|
1150
1162
|
// request keeps the raised reply room) and queue one act-now turn, if that request fits as it stands. Otherwise the notice stands.
|
|
1151
1163
|
const actNow = (event, ctx, result) => {
|
|
1152
|
-
|
|
1164
|
+
// Queued operator input already becomes the next turn; an act-now behind it would arrive after an answer and misstate the stall.
|
|
1165
|
+
if (event.context.pendingMessages.length > 0 || actNowsInARow(ctx.sessionManager.getBranch()) >= ACT_NOW_LIMIT || !ctx.model)
|
|
1153
1166
|
return undefined;
|
|
1154
1167
|
const edit = { type: "context_edit", targetId: event.messageEntryId, replacement: null };
|
|
1155
1168
|
// The cap path returns before taking a pending cut: preview it in place, and take it only if the turn is queued.
|
|
@@ -1178,7 +1191,6 @@ export async function installCompaction(pi, options = {}) {
|
|
|
1178
1191
|
}
|
|
1179
1192
|
if (untaken)
|
|
1180
1193
|
entries = [...(atBoundary(event, ctx, "run-end", false)?.entries ?? event.entries), edit];
|
|
1181
|
-
actNows++;
|
|
1182
1194
|
markBoundary(entries);
|
|
1183
1195
|
return { entries, continue: false };
|
|
1184
1196
|
};
|
|
@@ -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 = "6582ec23dfaf8ff94cbd7a5979c69569e3156024d0fc636de44b748f2ccab8ec";
|
|
4
4
|
export interface Receipt {
|
|
5
5
|
id: string;
|
|
6
6
|
build: string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
/** A reply whose prompt is at least this share below the previous reply's prompt was sent with a new cut. */
|
|
3
|
+
export declare const EPOCH_DROP = 0.05;
|
|
4
|
+
/** The prompt a reply was sent with (input + cache read + cache write), or undefined for replies with no usable usage. */
|
|
5
|
+
export declare const promptTokens: (entry: SessionEntry) => number | undefined;
|
|
6
|
+
export interface CutEpochs {
|
|
7
|
+
/** The cut epoch in effect at each branch entry: replies carry the epoch of the context they were sent with. 0 before any cut. */
|
|
8
|
+
epoch: number[];
|
|
9
|
+
/** True at the first reply with usable usage in each epoch after the first: its prompt is what that cut left. */
|
|
10
|
+
opens: boolean[];
|
|
11
|
+
/** Epoch steps, one per cut. */
|
|
12
|
+
cuts: number;
|
|
13
|
+
/** Compaction entries on the branch. */
|
|
14
|
+
compactions: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Places each reply under the cut it was sent with. A cut planned at prompt time or by the request guard is carried by requests at once,
|
|
18
|
+
* but its compaction entry is saved only at the next boundary, after the reply that carried it. So a cut shows in the log either as a
|
|
19
|
+
* compaction entry before the first reply that carries it, or as a prompt drop (EPOCH_DROP) at that reply with the entry after it. A
|
|
20
|
+
* compaction entry steps the epoch only when no drop has stepped it since the previous compaction entry, so one cut is one step.
|
|
21
|
+
*/
|
|
22
|
+
export declare function cutEpochs(branch: readonly SessionEntry[]): CutEpochs;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { isDiscardedResponse } from "./protocol.js";
|
|
2
|
+
/** A reply whose prompt is at least this share below the previous reply's prompt was sent with a new cut. */
|
|
3
|
+
export const EPOCH_DROP = 0.05;
|
|
4
|
+
/** The prompt a reply was sent with (input + cache read + cache write), or undefined for replies with no usable usage. */
|
|
5
|
+
export const promptTokens = (entry) => {
|
|
6
|
+
if (entry.type !== "message" || entry.message.role !== "assistant" || isDiscardedResponse(entry.message))
|
|
7
|
+
return undefined;
|
|
8
|
+
const message = entry.message;
|
|
9
|
+
if (message.stopReason === "error" || message.stopReason === "aborted")
|
|
10
|
+
return undefined;
|
|
11
|
+
const u = message.usage;
|
|
12
|
+
const total = (u?.input ?? 0) + (u?.cacheRead ?? 0) + (u?.cacheWrite ?? 0);
|
|
13
|
+
return Number.isFinite(total) && total > 0 ? total : undefined;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Places each reply under the cut it was sent with. A cut planned at prompt time or by the request guard is carried by requests at once,
|
|
17
|
+
* but its compaction entry is saved only at the next boundary, after the reply that carried it. So a cut shows in the log either as a
|
|
18
|
+
* compaction entry before the first reply that carries it, or as a prompt drop (EPOCH_DROP) at that reply with the entry after it. A
|
|
19
|
+
* compaction entry steps the epoch only when no drop has stepped it since the previous compaction entry, so one cut is one step.
|
|
20
|
+
*/
|
|
21
|
+
export function cutEpochs(branch) {
|
|
22
|
+
const epoch = [], opens = [];
|
|
23
|
+
let current = 0, compactions = 0, previous, dropped = false, open = false;
|
|
24
|
+
for (const entry of branch) {
|
|
25
|
+
if (entry.type === "compaction") {
|
|
26
|
+
compactions++;
|
|
27
|
+
if (dropped)
|
|
28
|
+
dropped = false;
|
|
29
|
+
else {
|
|
30
|
+
current++;
|
|
31
|
+
previous = undefined;
|
|
32
|
+
open = true;
|
|
33
|
+
}
|
|
34
|
+
epoch.push(current);
|
|
35
|
+
opens.push(false);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const prompt = promptTokens(entry);
|
|
39
|
+
if (prompt !== undefined && previous !== undefined && prompt < previous * (1 - EPOCH_DROP)) {
|
|
40
|
+
current++;
|
|
41
|
+
dropped = true;
|
|
42
|
+
open = true;
|
|
43
|
+
}
|
|
44
|
+
epoch.push(current);
|
|
45
|
+
opens.push(prompt !== undefined && open && current > 0);
|
|
46
|
+
if (prompt !== undefined) {
|
|
47
|
+
previous = prompt;
|
|
48
|
+
open = false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return { epoch, opens, cuts: current, compactions };
|
|
52
|
+
}
|
package/dist/src/lean.js
CHANGED
|
@@ -1,45 +1,35 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { cutEpochs, promptTokens } from "./epochs.js";
|
|
2
2
|
import { rereadsAfterCuts } from "./thrash.js";
|
|
3
3
|
/** Holding cost of a context token per request, as a share of its fresh price: a cached re-send costs about a tenth. */
|
|
4
4
|
export const LEAN_HOLDING = 0.1;
|
|
5
5
|
/** Growth samples (consecutive replies with no cut between) needed before the growth rate counts. */
|
|
6
6
|
export const LEAN_MIN_SAMPLES = 4;
|
|
7
|
-
const promptTokens = (entry) => {
|
|
8
|
-
if (entry.type !== "message" || entry.message.role !== "assistant" || isDiscardedResponse(entry.message))
|
|
9
|
-
return undefined;
|
|
10
|
-
const message = entry.message;
|
|
11
|
-
if (message.stopReason === "error" || message.stopReason === "aborted")
|
|
12
|
-
return undefined;
|
|
13
|
-
const u = message.usage;
|
|
14
|
-
const total = (u?.input ?? 0) + (u?.cacheRead ?? 0) + (u?.cacheWrite ?? 0);
|
|
15
|
-
return Number.isFinite(total) && total > 0 ? total : undefined;
|
|
16
|
-
};
|
|
17
7
|
/** The quantities the lean line is built from, read from the branch's recorded usage only. `kept` (K): the prompt of the first reply after
|
|
18
8
|
* the latest cut, what that cut left (system, summary, kept turns, carried files). `growth` (δ): the mean rise of the prompt between
|
|
19
9
|
* consecutive replies with no cut between. `rereads` (R): the mean tokens per cut of unchanged files re-read soon after it (thrash.ts). */
|
|
20
10
|
export function leanInputs(branch) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
11
|
+
// Epochs place each reply under the cut it was sent with (epochs.ts): growth is measured only inside an epoch, never across a cut.
|
|
12
|
+
const { epoch, opens, compactions } = cutEpochs(branch);
|
|
13
|
+
let kept = 0, previous, previousEpoch = -1, sum = 0, samples = 0;
|
|
14
|
+
branch.forEach((entry, i) => {
|
|
15
|
+
if (entry.type === "compaction" && epoch[i] !== previousEpoch && previousEpoch >= 0) {
|
|
16
|
+
kept = 0;
|
|
24
17
|
previous = undefined;
|
|
25
|
-
afterCut = true;
|
|
26
|
-
continue;
|
|
27
18
|
}
|
|
28
19
|
const prompt = promptTokens(entry);
|
|
29
20
|
if (prompt === undefined)
|
|
30
|
-
|
|
31
|
-
if (
|
|
21
|
+
return;
|
|
22
|
+
if (opens[i])
|
|
32
23
|
kept = prompt;
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
if (previous !== undefined) {
|
|
24
|
+
else if (previous !== undefined && epoch[i] === previousEpoch) {
|
|
36
25
|
sum += prompt - previous;
|
|
37
26
|
samples++;
|
|
38
27
|
}
|
|
39
28
|
previous = prompt;
|
|
40
|
-
|
|
29
|
+
previousEpoch = epoch[i];
|
|
30
|
+
});
|
|
41
31
|
const { cuts, tokens } = rereadsAfterCuts(branch);
|
|
42
|
-
return { kept, growth: samples ? sum / samples : 0, rereads: cuts ? tokens / cuts : 0, samples, cuts };
|
|
32
|
+
return { kept, growth: samples ? sum / samples : 0, rereads: cuts ? tokens / cuts : 0, samples, cuts: compactions };
|
|
43
33
|
}
|
|
44
34
|
/**
|
|
45
35
|
* The lean cut line: the economic order quantity of a context. Each cut costs a fresh re-send of what it keeps (K) and the re-reads it causes
|
package/dist/src/thrash.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isDiscardedResponse } from "./protocol.js";
|
|
2
|
+
import { cutEpochs } from "./epochs.js";
|
|
2
3
|
import { pathKey } from "./path-key.js";
|
|
3
4
|
import { isTextOnly, textOf } from "./tuck/core/content.js";
|
|
4
5
|
import { extractPath } from "./tuck/core/tool-args.js";
|
|
@@ -8,20 +9,22 @@ export const THRASH_WINDOW_REPLIES = 3;
|
|
|
8
9
|
export const THRASH_BAR = 0.5;
|
|
9
10
|
/** The branch's cuts and the re-reads that followed them (as `thrashing` defines a re-read), with their text in tokens at 4 characters each. */
|
|
10
11
|
export function rereadsAfterCuts(branch) {
|
|
11
|
-
|
|
12
|
+
// Each reply counts under the cut it was sent with (epochs.ts), so the reply that first carries a cut opens that cut's window.
|
|
13
|
+
const { epoch, cuts } = cutEpochs(branch);
|
|
14
|
+
let current = 0, replies = 0, rereads = 0, chars = 0;
|
|
12
15
|
const pending = new Map(), last = new Map();
|
|
13
|
-
for (const entry of branch) {
|
|
14
|
-
if (entry.type === "compaction") {
|
|
15
|
-
cuts++;
|
|
16
|
-
replies = 0;
|
|
17
|
-
continue;
|
|
18
|
-
}
|
|
16
|
+
for (const [i, entry] of branch.entries()) {
|
|
19
17
|
if (entry.type !== "message")
|
|
20
18
|
continue;
|
|
21
19
|
const message = entry.message;
|
|
22
20
|
if (message.role === "assistant") {
|
|
23
21
|
if (isDiscardedResponse(message))
|
|
24
22
|
continue;
|
|
23
|
+
if (epoch[i] !== current) {
|
|
24
|
+
current = epoch[i];
|
|
25
|
+
replies = 0;
|
|
26
|
+
}
|
|
27
|
+
const cuts = current;
|
|
25
28
|
replies++;
|
|
26
29
|
for (const block of message.content) {
|
|
27
30
|
if (block.type !== "toolCall" || block.name !== "read" || !block.arguments || typeof block.arguments !== "object")
|
package/package.json
CHANGED
package/src/build-id.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const BUILD_ID = "
|
|
1
|
+
export const BUILD_ID = "6582ec23dfaf8ff94cbd7a5979c69569e3156024d0fc636de44b748f2ccab8ec";
|
package/src/compaction.ts
CHANGED
|
@@ -73,8 +73,21 @@ export const capStopNotice = (model: string, maxTokens: number, file: string) =>
|
|
|
73
73
|
"The unfinished response is saved in the session.";
|
|
74
74
|
/** The follow-up turn sent after a reply that spent its whole output on thinking, with no text or tool call. */
|
|
75
75
|
export const ACT_NOW = "Your last reply used the whole output budget on thinking. Act now: make the next tool call or give the answer, with brief reasoning.";
|
|
76
|
-
/** At most this many act-now turns in a row; after that a stall gets the cap-stop notice until a reply
|
|
76
|
+
/** 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). */
|
|
77
77
|
export const ACT_NOW_LIMIT = 2;
|
|
78
|
+
/** Act-now turns on the branch since its last finished reply (stopReason "stop" or "toolUse"). Read from the log, so a restart, /tree
|
|
79
|
+
* 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. */
|
|
80
|
+
export function actNowsInARow(branch: readonly SessionEntry[]): number {
|
|
81
|
+
let count = 0;
|
|
82
|
+
for (let i = branch.length - 1; i >= 0; i--) {
|
|
83
|
+
const entry = branch[i]!;
|
|
84
|
+
if (entry.type !== "message") continue;
|
|
85
|
+
const message = entry.message;
|
|
86
|
+
if (message.role === "assistant" && (message.stopReason === "stop" || message.stopReason === "toolUse")) break;
|
|
87
|
+
if (message.role === "user" && textOf(message.content) === ACT_NOW) count++;
|
|
88
|
+
}
|
|
89
|
+
return count;
|
|
90
|
+
}
|
|
78
91
|
/** Pi's models.json, shown with ~ for the home folder. */
|
|
79
92
|
function modelsPath(): string {
|
|
80
93
|
try {
|
|
@@ -325,11 +338,8 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
|
|
|
325
338
|
};
|
|
326
339
|
// The act-now turn is pi-tuck's own, not the operator's: it grants no retry.
|
|
327
340
|
pi.on("message_start", (event, ctx) => { if (event.message.role === "user" && textOf(event.message.content) !== ACT_NOW) rearmLengthRetry(ctx); });
|
|
328
|
-
// Act-now turns sent since the last reply that was not a stall.
|
|
329
|
-
let actNows = 0;
|
|
330
341
|
pi.on("message_end", (event, ctx) => {
|
|
331
342
|
if (event.message.role !== "assistant") return;
|
|
332
|
-
if (event.message.stopReason !== "error" && event.message.stopReason !== "aborted" && !(event.message.content && thinkingOnlyLength(event.message))) actNows = 0;
|
|
333
343
|
if (event.message.stopReason !== "error" && event.message.stopReason !== "length") rearmLengthRetry(ctx);
|
|
334
344
|
lengthStop = event.message.stopReason === "length" ? {
|
|
335
345
|
kind: event.message.content && thinkingOnlyLength(event.message) ? "thinking" : "output",
|
|
@@ -1026,7 +1036,8 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
|
|
|
1026
1036
|
// After a stall that is not retried: leave the stalled reply out of the context (it stays saved, with its output usage, so the next
|
|
1027
1037
|
// request keeps the raised reply room) and queue one act-now turn, if that request fits as it stands. Otherwise the notice stands.
|
|
1028
1038
|
const actNow = (event: TurnEndEvent, ctx: ExtensionContext, result: { entries: readonly SessionBoundaryDraft[] } | undefined) => {
|
|
1029
|
-
|
|
1039
|
+
// Queued operator input already becomes the next turn; an act-now behind it would arrive after an answer and misstate the stall.
|
|
1040
|
+
if (event.context.pendingMessages.length > 0 || actNowsInARow(ctx.sessionManager.getBranch()) >= ACT_NOW_LIMIT || !ctx.model) return undefined;
|
|
1030
1041
|
const edit: SessionBoundaryDraft = { type: "context_edit", targetId: event.messageEntryId, replacement: null };
|
|
1031
1042
|
// The cap path returns before taking a pending cut: preview it in place, and take it only if the turn is queued.
|
|
1032
1043
|
const untaken = !result || result.entries === event.entries;
|
|
@@ -1044,7 +1055,6 @@ export async function installCompaction(pi: ExtensionAPI, options: CompactionOpt
|
|
|
1044
1055
|
} finally { restore?.(); }
|
|
1045
1056
|
try { pi.sendUserMessage(ACT_NOW, { deliverAs: "followUp" }); } catch { return undefined; }
|
|
1046
1057
|
if (untaken) entries = [...(atBoundary(event, ctx, "run-end", false)?.entries ?? event.entries), edit];
|
|
1047
|
-
actNows++;
|
|
1048
1058
|
markBoundary(entries);
|
|
1049
1059
|
return { entries, continue: false };
|
|
1050
1060
|
};
|
package/src/epochs.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { isDiscardedResponse } from "./protocol";
|
|
3
|
+
|
|
4
|
+
/** A reply whose prompt is at least this share below the previous reply's prompt was sent with a new cut. */
|
|
5
|
+
export const EPOCH_DROP = 0.05;
|
|
6
|
+
|
|
7
|
+
/** The prompt a reply was sent with (input + cache read + cache write), or undefined for replies with no usable usage. */
|
|
8
|
+
export const promptTokens = (entry: SessionEntry): number | undefined => {
|
|
9
|
+
if (entry.type !== "message" || entry.message.role !== "assistant" || isDiscardedResponse(entry.message)) return undefined;
|
|
10
|
+
const message = entry.message;
|
|
11
|
+
if (message.stopReason === "error" || message.stopReason === "aborted") return undefined;
|
|
12
|
+
const u = message.usage;
|
|
13
|
+
const total = (u?.input ?? 0) + (u?.cacheRead ?? 0) + (u?.cacheWrite ?? 0);
|
|
14
|
+
return Number.isFinite(total) && total > 0 ? total : undefined;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export interface CutEpochs {
|
|
18
|
+
/** The cut epoch in effect at each branch entry: replies carry the epoch of the context they were sent with. 0 before any cut. */
|
|
19
|
+
epoch: number[];
|
|
20
|
+
/** True at the first reply with usable usage in each epoch after the first: its prompt is what that cut left. */
|
|
21
|
+
opens: boolean[];
|
|
22
|
+
/** Epoch steps, one per cut. */
|
|
23
|
+
cuts: number;
|
|
24
|
+
/** Compaction entries on the branch. */
|
|
25
|
+
compactions: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Places each reply under the cut it was sent with. A cut planned at prompt time or by the request guard is carried by requests at once,
|
|
30
|
+
* but its compaction entry is saved only at the next boundary, after the reply that carried it. So a cut shows in the log either as a
|
|
31
|
+
* compaction entry before the first reply that carries it, or as a prompt drop (EPOCH_DROP) at that reply with the entry after it. A
|
|
32
|
+
* compaction entry steps the epoch only when no drop has stepped it since the previous compaction entry, so one cut is one step.
|
|
33
|
+
*/
|
|
34
|
+
export function cutEpochs(branch: readonly SessionEntry[]): CutEpochs {
|
|
35
|
+
const epoch: number[] = [], opens: boolean[] = [];
|
|
36
|
+
let current = 0, compactions = 0, previous: number | undefined, dropped = false, open = false;
|
|
37
|
+
for (const entry of branch) {
|
|
38
|
+
if (entry.type === "compaction") {
|
|
39
|
+
compactions++;
|
|
40
|
+
if (dropped) dropped = false;
|
|
41
|
+
else { current++; previous = undefined; open = true; }
|
|
42
|
+
epoch.push(current); opens.push(false);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const prompt = promptTokens(entry);
|
|
46
|
+
if (prompt !== undefined && previous !== undefined && prompt < previous * (1 - EPOCH_DROP)) { current++; dropped = true; open = true; }
|
|
47
|
+
epoch.push(current);
|
|
48
|
+
opens.push(prompt !== undefined && open && current > 0);
|
|
49
|
+
if (prompt !== undefined) { previous = prompt; open = false; }
|
|
50
|
+
}
|
|
51
|
+
return { epoch, opens, cuts: current, compactions };
|
|
52
|
+
}
|
package/src/lean.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import {
|
|
2
|
+
import { cutEpochs, promptTokens } from "./epochs";
|
|
3
3
|
import { rereadsAfterCuts } from "./thrash";
|
|
4
4
|
|
|
5
5
|
/** Holding cost of a context token per request, as a share of its fresh price: a cached re-send costs about a tenth. */
|
|
@@ -7,31 +7,24 @@ export const LEAN_HOLDING = 0.1;
|
|
|
7
7
|
/** Growth samples (consecutive replies with no cut between) needed before the growth rate counts. */
|
|
8
8
|
export const LEAN_MIN_SAMPLES = 4;
|
|
9
9
|
|
|
10
|
-
const promptTokens = (entry: SessionEntry): number | undefined => {
|
|
11
|
-
if (entry.type !== "message" || entry.message.role !== "assistant" || isDiscardedResponse(entry.message)) return undefined;
|
|
12
|
-
const message = entry.message;
|
|
13
|
-
if (message.stopReason === "error" || message.stopReason === "aborted") return undefined;
|
|
14
|
-
const u = message.usage;
|
|
15
|
-
const total = (u?.input ?? 0) + (u?.cacheRead ?? 0) + (u?.cacheWrite ?? 0);
|
|
16
|
-
return Number.isFinite(total) && total > 0 ? total : undefined;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
10
|
export interface LeanInputs { kept: number; growth: number; rereads: number; samples: number; cuts: number }
|
|
20
11
|
/** The quantities the lean line is built from, read from the branch's recorded usage only. `kept` (K): the prompt of the first reply after
|
|
21
12
|
* the latest cut, what that cut left (system, summary, kept turns, carried files). `growth` (δ): the mean rise of the prompt between
|
|
22
13
|
* consecutive replies with no cut between. `rereads` (R): the mean tokens per cut of unchanged files re-read soon after it (thrash.ts). */
|
|
23
14
|
export function leanInputs(branch: readonly SessionEntry[]): LeanInputs {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
15
|
+
// Epochs place each reply under the cut it was sent with (epochs.ts): growth is measured only inside an epoch, never across a cut.
|
|
16
|
+
const { epoch, opens, compactions } = cutEpochs(branch);
|
|
17
|
+
let kept = 0, previous: number | undefined, previousEpoch = -1, sum = 0, samples = 0;
|
|
18
|
+
branch.forEach((entry, i) => {
|
|
19
|
+
if (entry.type === "compaction" && epoch[i] !== previousEpoch && previousEpoch >= 0) { kept = 0; previous = undefined; }
|
|
27
20
|
const prompt = promptTokens(entry);
|
|
28
|
-
if (prompt === undefined)
|
|
29
|
-
if (
|
|
30
|
-
if (previous !== undefined) { sum += prompt - previous; samples++; }
|
|
31
|
-
previous = prompt;
|
|
32
|
-
}
|
|
21
|
+
if (prompt === undefined) return;
|
|
22
|
+
if (opens[i]) kept = prompt;
|
|
23
|
+
else if (previous !== undefined && epoch[i] === previousEpoch) { sum += prompt - previous; samples++; }
|
|
24
|
+
previous = prompt; previousEpoch = epoch[i];
|
|
25
|
+
});
|
|
33
26
|
const { cuts, tokens } = rereadsAfterCuts(branch);
|
|
34
|
-
return { kept, growth: samples ? sum / samples : 0, rereads: cuts ? tokens / cuts : 0, samples, cuts };
|
|
27
|
+
return { kept, growth: samples ? sum / samples : 0, rereads: cuts ? tokens / cuts : 0, samples, cuts: compactions };
|
|
35
28
|
}
|
|
36
29
|
|
|
37
30
|
/**
|
package/src/thrash.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { isDiscardedResponse } from "./protocol";
|
|
3
|
+
import { cutEpochs } from "./epochs";
|
|
3
4
|
import { pathKey } from "./path-key";
|
|
4
5
|
import { isTextOnly, textOf } from "./tuck/core/content";
|
|
5
6
|
import { extractPath } from "./tuck/core/tool-args";
|
|
@@ -18,14 +19,17 @@ export const THRASH_BAR = 0.5;
|
|
|
18
19
|
export interface Rereads { cuts: number; rereads: number; tokens: number }
|
|
19
20
|
/** The branch's cuts and the re-reads that followed them (as `thrashing` defines a re-read), with their text in tokens at 4 characters each. */
|
|
20
21
|
export function rereadsAfterCuts(branch: readonly SessionEntry[]): Rereads {
|
|
21
|
-
|
|
22
|
+
// Each reply counts under the cut it was sent with (epochs.ts), so the reply that first carries a cut opens that cut's window.
|
|
23
|
+
const { epoch, cuts } = cutEpochs(branch);
|
|
24
|
+
let current = 0, replies = 0, rereads = 0, chars = 0;
|
|
22
25
|
const pending = new Map<string, { key: string; cut: number; soon: boolean }>(), last = new Map<string, { cut: number; text: string }>();
|
|
23
|
-
for (const entry of branch) {
|
|
24
|
-
if (entry.type === "compaction") { cuts++; replies = 0; continue; }
|
|
26
|
+
for (const [i, entry] of branch.entries()) {
|
|
25
27
|
if (entry.type !== "message") continue;
|
|
26
28
|
const message = entry.message;
|
|
27
29
|
if (message.role === "assistant") {
|
|
28
30
|
if (isDiscardedResponse(message)) continue;
|
|
31
|
+
if (epoch[i] !== current) { current = epoch[i]; replies = 0; }
|
|
32
|
+
const cuts = current;
|
|
29
33
|
replies++;
|
|
30
34
|
for (const block of message.content) {
|
|
31
35
|
if (block.type !== "toolCall" || block.name !== "read" || !block.arguments || typeof block.arguments !== "object") continue;
|