pi-tuck 1.10.2 → 1.10.5
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 +24 -0
- package/README.md +2 -2
- package/dist/src/budget.d.ts +0 -2
- package/dist/src/budget.js +1 -3
- package/dist/src/build-id.d.ts +1 -1
- package/dist/src/build-id.js +1 -1
- package/dist/src/compaction.d.ts +1 -4
- package/dist/src/compaction.js +46 -18
- package/dist/src/content-observations.d.ts +0 -6
- package/dist/src/content-observations.js +5 -5
- package/dist/src/diagnostic-replay.js +6 -6
- package/dist/src/diagnostics.d.ts +0 -1
- package/dist/src/diagnostics.js +2 -3
- package/dist/src/history-image.d.ts +0 -2
- package/dist/src/history-image.js +1 -1
- package/dist/src/kept-reads.d.ts +3 -0
- package/dist/src/kept-reads.js +51 -0
- package/dist/src/omissions.d.ts +3 -6
- package/dist/src/omissions.js +3 -10
- package/dist/src/recall.js +2 -2
- package/dist/src/thrash.d.ts +0 -6
- package/dist/src/thrash.js +1 -1
- package/dist/src/tuck/core/format.d.ts +2 -0
- package/dist/src/tuck/core/format.js +3 -0
- package/dist/src/tuck/core/token-estimate.d.ts +2 -8
- package/dist/src/tuck/core/token-estimate.js +3 -9
- package/dist/src/tuck/core/tool-args.d.ts +0 -1
- package/dist/src/tuck/core/tool-args.js +1 -1
- package/dist/src/tuck/core/transcript.d.ts +3 -2
- package/dist/src/tuck/cut.d.ts +0 -1
- package/dist/src/tuck/cut.js +0 -3
- package/dist/src/tuck.d.ts +2 -2
- package/dist/src/tuck.js +16 -13
- package/package.json +1 -1
- package/src/budget.ts +1 -4
- package/src/build-id.ts +1 -1
- package/src/compaction.ts +42 -17
- package/src/content-observations.ts +5 -5
- package/src/diagnostic-replay.ts +7 -6
- package/src/diagnostics.ts +2 -3
- package/src/history-image.ts +1 -1
- package/src/kept-reads.ts +40 -0
- package/src/omissions.ts +3 -10
- package/src/recall.ts +3 -3
- package/src/thrash.ts +1 -7
- package/src/tuck/core/format.ts +3 -0
- package/src/tuck/core/token-estimate.ts +4 -20
- package/src/tuck/core/tool-args.ts +1 -1
- package/src/tuck/core/transcript.ts +2 -2
- package/src/tuck/cut.ts +0 -4
- package/src/tuck.ts +16 -13
- package/dist/src/tuck/core/render-entries.d.ts +0 -6
- package/dist/src/tuck/core/render-entries.js +0 -1
- package/src/tuck/core/render-entries.ts +0 -6
|
@@ -2,39 +2,23 @@ export const DEFAULT_CHARS_PER_TOKEN = 4;
|
|
|
2
2
|
export const MIN_CHARS_PER_TOKEN = 2;
|
|
3
3
|
export const MAX_CHARS_PER_TOKEN = 6;
|
|
4
4
|
|
|
5
|
-
export type TokenEstimateMode = "heuristic" | "calibrated";
|
|
6
|
-
|
|
7
|
-
export interface TokenEstimateCalibration {
|
|
8
|
-
mode: TokenEstimateMode;
|
|
9
|
-
charsPerToken: number;
|
|
10
|
-
sourceChars?: number;
|
|
11
|
-
sourceTokens?: number;
|
|
12
|
-
rawCharsPerToken?: number;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
5
|
const clamp = (value: number, min: number, max: number): number =>
|
|
16
6
|
Math.min(max, Math.max(min, value));
|
|
17
7
|
|
|
18
8
|
export const calibrateCharsPerToken = (
|
|
19
9
|
sourceChars: number,
|
|
20
10
|
sourceTokens: number | undefined,
|
|
21
|
-
):
|
|
11
|
+
): { charsPerToken: number } => {
|
|
22
12
|
if (!sourceTokens || sourceTokens <= 0 || sourceChars <= 0) {
|
|
23
|
-
return {
|
|
13
|
+
return { charsPerToken: DEFAULT_CHARS_PER_TOKEN };
|
|
24
14
|
}
|
|
25
15
|
|
|
26
16
|
const rawCharsPerToken = sourceChars / sourceTokens;
|
|
27
17
|
if (!Number.isFinite(rawCharsPerToken) || rawCharsPerToken <= 0) {
|
|
28
|
-
return {
|
|
18
|
+
return { charsPerToken: DEFAULT_CHARS_PER_TOKEN };
|
|
29
19
|
}
|
|
30
20
|
|
|
31
|
-
return {
|
|
32
|
-
mode: "calibrated",
|
|
33
|
-
charsPerToken: clamp(rawCharsPerToken, MIN_CHARS_PER_TOKEN, MAX_CHARS_PER_TOKEN),
|
|
34
|
-
sourceChars,
|
|
35
|
-
sourceTokens,
|
|
36
|
-
rawCharsPerToken,
|
|
37
|
-
};
|
|
21
|
+
return { charsPerToken: clamp(rawCharsPerToken, MIN_CHARS_PER_TOKEN, MAX_CHARS_PER_TOKEN) };
|
|
38
22
|
};
|
|
39
23
|
|
|
40
24
|
export const estimateTokensFromChars = (
|
|
@@ -15,8 +15,8 @@ export const smallOriginalArguments = (value: unknown, maxCharacters = MAX_PRODU
|
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
interface RecordFields {
|
|
18
|
-
sourceTime?:
|
|
19
|
-
sourceTimes?: ({ sourceIndex: number } &
|
|
18
|
+
sourceTime?: SourceTime;
|
|
19
|
+
sourceTimes?: ({ sourceIndex: number } & SourceTime)[];
|
|
20
20
|
sourceEntryId?: string;
|
|
21
21
|
role?: "user" | "assistant" | "custom" | "bashExecution" | "toolResult";
|
|
22
22
|
sourceIndex?: number;
|
package/src/tuck/cut.ts
CHANGED
|
@@ -48,7 +48,6 @@ export type OwnCutResult =
|
|
|
48
48
|
selectedIds: string[];
|
|
49
49
|
firstKeptEntryId: string;
|
|
50
50
|
compactAll: boolean;
|
|
51
|
-
keptUserTurns: number;
|
|
52
51
|
keepFallbackToCompactAll: boolean;
|
|
53
52
|
budgetCut?: BudgetCutKind;
|
|
54
53
|
}
|
|
@@ -87,7 +86,6 @@ export function buildOwnCut(branchEntries: any[], keepUserTurns = 1): OwnCutResu
|
|
|
87
86
|
selectedIds: liveMessages.map((e) => e.entry.id),
|
|
88
87
|
firstKeptEntryId: "",
|
|
89
88
|
compactAll: true,
|
|
90
|
-
keptUserTurns: 0,
|
|
91
89
|
keepFallbackToCompactAll,
|
|
92
90
|
});
|
|
93
91
|
|
|
@@ -111,7 +109,6 @@ export function buildOwnCut(branchEntries: any[], keepUserTurns = 1): OwnCutResu
|
|
|
111
109
|
selectedIds: liveMessages.slice(0, cutIdx).map((e) => e.entry.id),
|
|
112
110
|
firstKeptEntryId: liveMessages[cutIdx].entry.id,
|
|
113
111
|
compactAll: false,
|
|
114
|
-
keptUserTurns: userIndices.length - targetUserIdx,
|
|
115
112
|
keepFallbackToCompactAll: false,
|
|
116
113
|
};
|
|
117
114
|
}
|
|
@@ -168,7 +165,6 @@ export const applyTailBudget = (
|
|
|
168
165
|
selectedIds: live.slice(0, idx).map((m) => m.entry.id),
|
|
169
166
|
firstKeptEntryId: live[idx].entry.id,
|
|
170
167
|
compactAll: false,
|
|
171
|
-
keptUserTurns: live.slice(idx).filter((m) => m.message.role === "user").length,
|
|
172
168
|
keepFallbackToCompactAll: false,
|
|
173
169
|
budgetCut,
|
|
174
170
|
});
|
package/src/tuck.ts
CHANGED
|
@@ -2,12 +2,12 @@ import { resolveSourceTime, type SourceTime } from "./tuck/core/source-time";
|
|
|
2
2
|
import { orderByOriginalSource } from "./tuck/core/source-order";
|
|
3
3
|
import { assertNativeToolRounds, isDiscardedResponse } from "./protocol.js";
|
|
4
4
|
import { parseRecords, serializeTranscript, serializeRecord, splitSummary, TRANSCRIPT_HEADER, type TranscriptRecord } from "./tuck/core/transcript";
|
|
5
|
-
import { closingSuffix, DIALOGUE_SUFFIX, workingFileBlock, WORKING_FILES_HEADER, WORKING_FILES_INSTRUCTION, CLOSING_INSTRUCTION } from "./tuck/core/format";
|
|
5
|
+
import { closingSuffix, DEEPEST_SUFFIX, DIALOGUE_SUFFIX, workingFileBlock, WORKING_FILES_HEADER, WORKING_FILES_INSTRUCTION, CLOSING_INSTRUCTION } from "./tuck/core/format";
|
|
6
6
|
import { carriedTailTokens, carriedTarget, conversationTokens } from "./budget";
|
|
7
7
|
import { omissionRecord, userPrefixRecords } from "./tuck/core/brief";
|
|
8
8
|
import { isTextOnly, textOf } from "./tuck/core/content";
|
|
9
9
|
import { MAX_ACTION_LINES, MAX_LEDGER_LINES, actionItems, actionRecords, ledgerItems, ledgerRecords } from "./ledger.js";
|
|
10
|
-
import { keptReadCandidates } from "./kept-reads.js";
|
|
10
|
+
import { keptReadCandidates, promptOutputCandidates } from "./kept-reads.js";
|
|
11
11
|
import { workingSetCandidates, type WorkingFile } from "./working-set.js";
|
|
12
12
|
import { keptSearchCandidates } from "./kept-searches.js";
|
|
13
13
|
import { fileObservations, thinkingRecords } from "./content-observations";
|
|
@@ -61,8 +61,8 @@ export function tuckDraft(tuck: Tuck, event: SessionBeforeCompactEvent, ctx: Ext
|
|
|
61
61
|
return tuckObservedDraft(tuck, { branchEntries: event.branchEntries, tokensBefore: event.preparation.tokensBefore, previousSummary: event.preparation.previousSummary, newPrompt, protectedPromptEntryId,
|
|
62
62
|
keepRecentTokens: event.preparation.settings?.keepRecentTokens ?? keepRecentTokens }, ctx, reserveTokens, measure, policy, retainCurrentInput);
|
|
63
63
|
}
|
|
64
|
-
/** Shared compiler observes canonical branch and current token estimate; no native preparation utility.
|
|
65
|
-
|
|
64
|
+
/** Shared compiler observes canonical branch and current token estimate; no native preparation utility.
|
|
65
|
+
* `newPrompt`: the cut is planned for a run that has not begun (a prompt Pi has not appended yet, an operator's manual cut, Pi's own cut before
|
|
66
66
|
* a run a custom message starts); the session has moved on from an ended run.
|
|
67
67
|
* `reRenderSummary`: the cut is due, so its summary may be re-rendered smaller (see the `no_live_messages` branch below). */
|
|
68
68
|
export function tuckObservedDraft(tuck: Tuck, input: { branchEntries: SessionEntry[]; tokensBefore: number; previousSummary?: string; newPrompt?: boolean; reRenderSummary?: boolean; protectedPromptEntryId?: string; deepest?: boolean; keepRecentTokens?: number }, ctx: ExtensionContext,
|
|
@@ -100,12 +100,11 @@ export function tuckObservedDraft(tuck: Tuck, input: { branchEntries: SessionEnt
|
|
|
100
100
|
// made smaller. Inside the band a cut is opportunistic, and re-rendering there shrinks the summary and costs history for nothing (replays
|
|
101
101
|
// s04-B task 13, s08-B task 23: a 13,008-token first request became 3,336), so an opportunistic cut refuses as it did before.
|
|
102
102
|
else if (input.reRenderSummary && cut.reason === "no_live_messages" && entries.some(entry => entry.type === "compaction"))
|
|
103
|
-
cut = { ok: true, messages: [], selectedIds: [], firstKeptEntryId: "", compactAll: true,
|
|
103
|
+
cut = { ok: true, messages: [], selectedIds: [], firstKeptEntryId: "", compactAll: true, keepFallbackToCompactAll: false };
|
|
104
104
|
if (!cut.ok) return { unavailable: `pi-tuck cannot select a cut: ${cut.reason}` };
|
|
105
105
|
const cutBefore = (cut: Extract<Cut, { ok: true }>, boundary: number, prefix: Extract<Cut, { ok: true }>): Extract<Cut, { ok: true }> => ({
|
|
106
106
|
...cut, messages: prefix.messages, selectedIds: prefix.selectedIds,
|
|
107
107
|
firstKeptEntryId: entries[boundary]?.id ?? "", compactAll: boundary === entries.length, keepFallbackToCompactAll: false,
|
|
108
|
-
keptUserTurns: entries.slice(boundary).filter(entry => entry.type === "message" && entry.message.role === "user").length,
|
|
109
108
|
});
|
|
110
109
|
// The latest call's trailing receipts have not reached a subsequent response.
|
|
111
110
|
// A preferred tail size must not erase this first exposure when the whole
|
|
@@ -314,9 +313,13 @@ export function tuckObservedDraft(tuck: Tuck, input: { branchEntries: SessionEnt
|
|
|
314
313
|
// The last admission attempt and thinking-only retry share this exact floor: whole protected prompts, the smallest
|
|
315
314
|
// existing summary representation, and the required fresh/native suffix. No historical fill or room target spends reply space.
|
|
316
315
|
if (input.deepest) {
|
|
317
|
-
|
|
318
|
-
const
|
|
319
|
-
|
|
316
|
+
// The floor carries the outputs prompts asked for (capped at 8,000 characters) and closes without claiming lists it does not have.
|
|
317
|
+
const outputs = promptOutputCandidates([header, ...entries.slice(0, floorBoundary)], indices, entry => readable(entry) !== undefined);
|
|
318
|
+
const summary = (serializeTranscript([...calibrationHigh.rows, ...outputs]) || TRANSCRIPT_HEADER + "\n") + DEEPEST_SUFFIX;
|
|
319
|
+
const projection = preview(summary, floorBoundary);
|
|
320
|
+
assertNativeToolRounds(projection);
|
|
321
|
+
const after = measure(projection);
|
|
322
|
+
return { compaction: { summary, firstKeptEntryId: entries[floorBoundary]?.id ?? "", tokensBefore: input.tokensBefore,
|
|
320
323
|
details: { ...detailsAt(floorBoundary), tokensAfter: after, carriedTarget: after, view: "shrunk-dialogue", mode: "shrunk", deepest: true } }, after, mode: "shrunk" };
|
|
321
324
|
}
|
|
322
325
|
const offset = measure(floorProjection) - nativeAt(floorBoundary) - summaryTokens(floorSummary.length);
|
|
@@ -537,10 +540,10 @@ export function tuckObservedDraft(tuck: Tuck, input: { branchEntries: SessionEnt
|
|
|
537
540
|
const keptResultCap = Math.min(Math.floor(0.20 * maxSummaryCharacters), 24000);
|
|
538
541
|
const counts = new Map<string, number>();
|
|
539
542
|
const prefix = [header, ...entries.slice(0, boundary)];
|
|
540
|
-
//
|
|
541
|
-
// allowance
|
|
542
|
-
const candidates = [...keptReadCandidates(prefix, indices, resolvable, carriedKeys), ...keptSearchCandidates(prefix, indices, resolvable)]
|
|
543
|
-
.sort((a, b) => b.sourceIndex! - a.sourceIndex! || Number(a.title === "Kept-search") - Number(b.title === "Kept-search"));
|
|
543
|
+
// Outputs a user prompt asked for lead (a message-driven agent's tasks); reads and searches follow, newest result first. One
|
|
544
|
+
// allowance; L2's independent four-item caps and whole-item skips.
|
|
545
|
+
const candidates = [...promptOutputCandidates(prefix, indices, resolvable), ...[...keptReadCandidates(prefix, indices, resolvable, carriedKeys), ...keptSearchCandidates(prefix, indices, resolvable)]
|
|
546
|
+
.sort((a, b) => b.sourceIndex! - a.sourceIndex! || Number(a.title === "Kept-search") - Number(b.title === "Kept-search"))];
|
|
544
547
|
for (const record of candidates) {
|
|
545
548
|
if ((counts.get(record.title!) ?? 0) === 4) continue;
|
|
546
549
|
const extra = lineCost([record]);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|