pi-tuck 1.0.16 → 1.0.18
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 +75 -0
- package/README.md +7 -4
- package/dist/src/budget.d.ts +14 -17
- package/dist/src/budget.js +22 -29
- package/dist/src/build-id.d.ts +1 -1
- package/dist/src/build-id.js +1 -1
- package/dist/src/compaction.js +113 -20
- package/dist/src/diagnostics.d.ts +1 -1
- package/dist/src/history-tools.d.ts +16 -0
- package/dist/src/history-tools.js +59 -0
- package/dist/src/ledger.d.ts +0 -2
- package/dist/src/ledger.js +0 -2
- package/dist/src/omissions.d.ts +1 -1
- package/dist/src/omissions.js +88 -6
- package/dist/src/tuck/core/brief.d.ts +6 -3
- package/dist/src/tuck/core/brief.js +27 -9
- package/dist/src/tuck/core/filter-noise.d.ts +1 -1
- package/dist/src/tuck/core/filter-noise.js +1 -3
- package/dist/src/tuck/core/normalize.d.ts +2 -5
- package/dist/src/tuck/core/normalize.js +15 -33
- package/dist/src/tuck/core/shrink.d.ts +6 -14
- package/dist/src/tuck/core/shrink.js +162 -102
- package/dist/src/tuck/core/summarize.d.ts +3 -14
- package/dist/src/tuck/core/summarize.js +9 -20
- package/dist/src/tuck/core/transcript.d.ts +5 -0
- package/dist/src/tuck/core/transcript.js +27 -4
- package/dist/src/tuck/types.d.ts +1 -1
- package/dist/src/tuck.d.ts +9 -17
- package/dist/src/tuck.js +384 -212
- package/package.json +1 -1
- package/src/budget.ts +24 -26
- package/src/build-id.ts +1 -1
- package/src/compaction.ts +102 -21
- package/src/history-tools.ts +57 -0
- package/src/ledger.ts +0 -2
- package/src/omissions.ts +64 -7
- package/src/tuck/core/brief.ts +25 -10
- package/src/tuck/core/filter-noise.ts +1 -3
- package/src/tuck/core/normalize.ts +11 -27
- package/src/tuck/core/shrink.ts +141 -91
- package/src/tuck/core/summarize.ts +9 -24
- package/src/tuck/core/transcript.ts +26 -4
- package/src/tuck/types.ts +3 -3
- package/src/tuck.ts +336 -185
package/dist/src/tuck.js
CHANGED
|
@@ -1,30 +1,23 @@
|
|
|
1
1
|
import { resolveSourceTime } from "./tuck/core/source-time.js";
|
|
2
2
|
import { orderByOriginalSource } from "./tuck/core/source-order.js";
|
|
3
3
|
import { assertNativeToolRounds, isDiscardedResponse } from "./protocol.js";
|
|
4
|
-
import { parseRecords, serializeTranscript, splitSummary } from "./tuck/core/transcript.js";
|
|
4
|
+
import { parseRecords, serializeTranscript, serializeRecord, splitSummary, TRANSCRIPT_HEADER } from "./tuck/core/transcript.js";
|
|
5
5
|
import { DIALOGUE_SUFFIX } from "./tuck/core/format.js";
|
|
6
|
+
import { carriedTailTokens, carriedTarget, conversationTokens } from "./budget.js";
|
|
7
|
+
import { omissionRecord, userPrefixRecords } from "./tuck/core/brief.js";
|
|
6
8
|
import { isTextOnly, textOf } from "./tuck/core/content.js";
|
|
7
|
-
import {
|
|
8
|
-
import { SessionManager } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { MAX_ACTION_LINES, MAX_LEDGER_LINES, actionItems, actionRecords, ledgerItems, ledgerRecords } from "./ledger.js";
|
|
10
|
+
import { convertToLlm, SessionManager } from "@earendil-works/pi-coding-agent";
|
|
9
11
|
import { ATTEMPT_NOTICE, createRecall } from "./recall.js";
|
|
10
12
|
/** Includes JSON escaping, hit metadata and continuation, not just excerpt text. */
|
|
11
13
|
export const MAX_RANKED_RESPONSE_CHARACTERS = 16000;
|
|
12
14
|
import { buildOwnCut, applyTailBudget, MAX_TAIL_TOKENS, OVERSIZED_TAIL_FACTOR } from "./tuck/cut.js";
|
|
13
15
|
import { calibrateCharsPerToken, estimateSourceMessageChars } from "./tuck/core/token-estimate.js";
|
|
14
16
|
import { buildGlobalIndexById } from "./tuck/core/global-indices.js";
|
|
15
|
-
import {
|
|
17
|
+
import { compileDialogue } from "./tuck/core/summarize.js";
|
|
16
18
|
import { prepareKeywordQuery } from "./tuck/core/keyword-query.js";
|
|
17
19
|
import { searchEntriesDetailed } from "./tuck/core/search-entries.js";
|
|
18
|
-
export const tuck = { buildOwnCut, applyTailBudget, calibrateCharsPerToken, buildGlobalIndexById,
|
|
19
|
-
/** The one historical allowance: at most 25% of remaining estimated room, for both views.
|
|
20
|
-
* The whole dialogue is offered only within it (plus actual preview/headroom guards);
|
|
21
|
-
* otherwise the same dialogue is shrunk to fit it. The 4-character conversion matches pinned Pi's
|
|
22
|
-
* summary estimator, not a provider-token guarantee. Mandatory sources (current input,
|
|
23
|
-
* initiating prompt, file ledger) are already charged in base. */
|
|
24
|
-
export function historicalAllowance(window, reserve, base, before) {
|
|
25
|
-
const free = Math.max(0, window - reserve - base - 1);
|
|
26
|
-
return Math.max(0, Math.min(Math.floor(free / 4), before - base - 1)) * 4;
|
|
27
|
-
}
|
|
20
|
+
export const tuck = { buildOwnCut, applyTailBudget, calibrateCharsPerToken, buildGlobalIndexById, compileDialogue, prepareKeywordQuery, searchEntriesDetailed };
|
|
28
21
|
/** Uses pi-tuck's own cut, calibrated maximum tail and dialogue compiler; Pi owns persistence and continuation. */
|
|
29
22
|
export function tuckDraft(tuck, event, ctx, reserveTokens, measure, policy = {}, retainCurrentInput = true, operatorManual = false, newPrompt = false) {
|
|
30
23
|
return tuckObservedDraft(tuck, { branchEntries: event.branchEntries, tokensBefore: event.preparation.tokensBefore, previousSummary: event.preparation.previousSummary, operatorManual, newPrompt }, ctx, reserveTokens, measure, policy, retainCurrentInput);
|
|
@@ -53,6 +46,8 @@ export function tuckObservedDraft(tuck, input, ctx, reserveTokens, measure, poli
|
|
|
53
46
|
const chars = (calibration.ok ? calibration.messages.reduce((n, m) => n + estimateSourceMessageChars(m), 0) : 0)
|
|
54
47
|
+ (input.previousSummary?.length ?? 0);
|
|
55
48
|
const { charsPerToken } = tuck.calibrateCharsPerToken(chars, input.tokensBefore);
|
|
49
|
+
// The existing keep-recent rule offers a native suffix. The budget owner
|
|
50
|
+
// bounds its rank and base price below, in the planner's estimate.
|
|
56
51
|
// The planner's reserve includes mandatory input (system, host and incoming prompt), so this is half of the target room *after* that
|
|
57
52
|
// fixed input, not half of the whole room. Current/fresh-source floors below may still require an explicit rejection.
|
|
58
53
|
const inputRoom = Math.floor(Math.max(0, (ctx.model?.contextWindow ?? 0) - reserveTokens - 1) / 2);
|
|
@@ -120,8 +115,7 @@ export function tuckObservedDraft(tuck, input, ctx, reserveTokens, measure, poli
|
|
|
120
115
|
const current = entries[userPosition];
|
|
121
116
|
const userContent = current?.type === "message" && current.message.role === "user" ? current.message.content : undefined;
|
|
122
117
|
const textOnly = isTextOnly(userContent);
|
|
123
|
-
//
|
|
124
|
-
// source; when a native edit omitted it, nothing is quoted and no later user source is promoted in its place.
|
|
118
|
+
// Anchor the actual first source only; a native omission never promotes a later prompt.
|
|
125
119
|
const firstUser = branch.find(entry => entry.type === "message" && entry.message.role === "user");
|
|
126
120
|
const firstUserPosition = entries.findIndex(entry => entry.id === firstUser?.id && entry.type === "message");
|
|
127
121
|
const selectedKeptId = cut.firstKeptEntryId;
|
|
@@ -136,217 +130,395 @@ export function tuckObservedDraft(tuck, input, ctx, reserveTokens, measure, poli
|
|
|
136
130
|
}
|
|
137
131
|
if (cut.messages.length !== cut.selectedIds.length || (!cut.compactAll && !entries.some((e) => e.id === cut.firstKeptEntryId)))
|
|
138
132
|
throw new Error("pi-tuck returned an invalid source boundary");
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
133
|
+
const budgetCut = cut.budgetCut;
|
|
134
|
+
let preferredBoundary = cut.compactAll ? entries.length : entries.findIndex(entry => entry.id === cut.firstKeptEntryId);
|
|
135
|
+
let floorBoundary = freshRound ? lastResponse : entries.length;
|
|
136
|
+
if (freshBash >= 0)
|
|
137
|
+
floorBoundary = Math.min(floorBoundary, freshBash);
|
|
138
|
+
if (userPosition >= 0 && (!textOnly || userPosition > lastResponse))
|
|
139
|
+
floorBoundary = Math.min(floorBoundary, userPosition);
|
|
140
|
+
// One fill owns the summary and the contiguous native suffix. Protocol/current-input
|
|
141
|
+
// floors are representation constraints; every other source competes by rank.
|
|
142
|
+
return rankedFill();
|
|
143
|
+
function rankedFill() {
|
|
144
|
+
const model = ctx.model;
|
|
145
|
+
if (!model)
|
|
146
|
+
return { unavailable: "pi-tuck needs a model to measure the cut" };
|
|
150
147
|
const indices = tuck.buildGlobalIndexById(ctx.sessionManager.getEntries());
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const
|
|
156
|
-
|
|
148
|
+
const header = ctx.sessionManager.getHeader();
|
|
149
|
+
if (!header)
|
|
150
|
+
throw new Error("Missing native session header");
|
|
151
|
+
const before = measure(projected.messages), room = Math.max(0, model.contextWindow - reserveTokens - 1);
|
|
152
|
+
const minimumOpeningLength = 80, openingLength = Math.max(80, Math.floor(Math.sqrt(room * 4)));
|
|
153
|
+
const positions = new Map(entries.map((entry, at) => [entry.id, at]));
|
|
154
|
+
const originals = tuck.buildOwnCut(entries.filter(entry => entry.type !== "compaction"), 0);
|
|
155
|
+
if (!originals.ok)
|
|
157
156
|
return { unavailable: "No eligible original prefix to compile" };
|
|
158
|
-
const
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
157
|
+
const byId = new Map(entries.map(entry => [entry.id, entry]));
|
|
158
|
+
const sourceIndices = originals.selectedIds.map(id => indices.get(id));
|
|
159
|
+
const rows = originals.selectedIds.map((id, at) => ({ id, position: positions.get(id), index: sourceIndices[at], message: originals.messages[at] }));
|
|
160
|
+
const users = rows.filter(row => row.message.role === "user");
|
|
161
|
+
const userTexts = new Map(users.map(row => [row.index, textOf(row.message.content)]));
|
|
162
|
+
let replied = false;
|
|
163
|
+
const answered = [];
|
|
164
|
+
for (let at = entries.length - 1; at >= 0; at--) {
|
|
165
|
+
const entry = entries[at];
|
|
166
|
+
if (entry.type !== "message")
|
|
167
|
+
continue;
|
|
168
|
+
if (entry.message.role === "assistant" && !isDiscardedResponse(entry.message) && entry.message.content.length) {
|
|
169
|
+
replied = true;
|
|
170
|
+
continue;
|
|
168
171
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
return [{ kind: "excerpt", role: "user", sourceIndex: index, text: quoted, sourceTimes }, ...(quoted.length < text.length ? [{ kind: "omission", role: "user",
|
|
185
|
-
sourceIndex: index, reason: `${label} continues: characters ${quoted.length}-${text.length} omitted; read them with tuck_recall {"index":${index},"offset":${quoted.length}}`, sourceTimes }] : [])];
|
|
172
|
+
if (entry.message.role !== "user")
|
|
173
|
+
continue;
|
|
174
|
+
if (replied && textOf(entry.message.content).length)
|
|
175
|
+
answered.push(entry);
|
|
176
|
+
replied = false;
|
|
177
|
+
}
|
|
178
|
+
const anchors = [...new Map([...answered.slice(0, 8), ...(firstUserPosition >= 0 ? [entries[firstUserPosition]] : [])].map(entry => [entry.id, entry])).values()];
|
|
179
|
+
const failedCalls = new Set(entries.flatMap(entry => entry.type === "message" && entry.message.role === "toolResult" && entry.message.isError ? [entry.message.toolCallId] : []));
|
|
180
|
+
const detailsAt = (boundary) => ({ compactor: "pi-tuck", version: 2, adapter: "standalone",
|
|
181
|
+
keptUserTurns: users.reduce((n, row) => n + Number(row.position >= boundary), 0),
|
|
182
|
+
...(boundary === preferredBoundary && budgetCut ? { budgetCut } : {}) });
|
|
183
|
+
const preview = (summary, boundary) => {
|
|
184
|
+
const manager = SessionManager.inMemory(ctx.cwd, undefined, [header, ...branch]);
|
|
185
|
+
manager.appendCompaction(summary, entries[boundary]?.id ?? "", input.tokensBefore, detailsAt(boundary), true);
|
|
186
|
+
return manager.buildSessionProjection().messages;
|
|
186
187
|
};
|
|
187
|
-
//
|
|
188
|
-
//
|
|
189
|
-
//
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
turns.length = 0;
|
|
213
|
-
let left = budget, heads = Math.floor(budget / 4);
|
|
214
|
-
for (const turn of turns) {
|
|
215
|
-
if (heads <= 0)
|
|
188
|
+
// Price the native suffix once. The measure's additive offset and Pi's
|
|
189
|
+
// summary envelope are calibrated once, rather than replaying a session
|
|
190
|
+
// for each opening, ledger line or tail boundary considered by the fill.
|
|
191
|
+
const rawPositions = new Map(branch.map((entry, at) => [entry.id, at]));
|
|
192
|
+
const native = new Float64Array(branch.length + 1);
|
|
193
|
+
for (const entry of projected.entries) {
|
|
194
|
+
if (entry.sourceEntry.type === "compaction")
|
|
195
|
+
continue;
|
|
196
|
+
const at = rawPositions.get(entry.sourceEntry.id);
|
|
197
|
+
if (at !== undefined)
|
|
198
|
+
for (const message of entry.messages)
|
|
199
|
+
native[at] += measure.messageTokens ? measure.messageTokens(message) : conversationTokens([message]);
|
|
200
|
+
}
|
|
201
|
+
for (let at = branch.length - 1; at >= 0; at--)
|
|
202
|
+
native[at] += native[at + 1];
|
|
203
|
+
const nativeAt = (boundary) => boundary === entries.length ? 0 : native[rawPositions.get(entries[boundary].id)];
|
|
204
|
+
const tailTokens = carriedTailTokens(nativeAt(preferredBoundary));
|
|
205
|
+
// Move only the older end, to the newest whole protocol-safe entries
|
|
206
|
+
// within the shared tail size. Required current/fresh native sources
|
|
207
|
+
// stay whole even when their floor exceeds that preferred amount.
|
|
208
|
+
while (preferredBoundary < floorBoundary && nativeAt(preferredBoundary) > tailTokens) {
|
|
209
|
+
preferredBoundary++;
|
|
210
|
+
while (preferredBoundary < floorBoundary) {
|
|
211
|
+
const entry = entries[preferredBoundary];
|
|
212
|
+
if ((entry.type === "message" && entry.message.role !== "toolResult") || entry.type === "custom_message" || entry.type === "branch_summary")
|
|
216
213
|
break;
|
|
217
|
-
|
|
218
|
-
heads -= turn.take;
|
|
219
|
-
left -= turn.take;
|
|
214
|
+
preferredBoundary++;
|
|
220
215
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
216
|
+
}
|
|
217
|
+
const envelope = convertToLlm([{ role: "compactionSummary", summary: "", tokensBefore: 0, timestamp: 0 }]);
|
|
218
|
+
const wrapperCharacters = envelope.reduce((n, message) => n + textOf(message.content).length, 0);
|
|
219
|
+
const summaryTokens = (characters) => Math.ceil((characters + wrapperCharacters) / 4);
|
|
220
|
+
const lineCost = (records) => records.reduce((n, record) => n + serializeRecord(record).length + 1, 0);
|
|
221
|
+
const frameLength = (cost, count) => TRANSCRIPT_HEADER.length + DIALOGUE_SUFFIX.length + cost + (count ? 0 : 1);
|
|
222
|
+
const render = (records) => (serializeTranscript(records) || TRANSCRIPT_HEADER + "\n") + DIALOGUE_SUFFIX;
|
|
223
|
+
const units = { user: [], assistant: [], toolResult: [] };
|
|
224
|
+
for (const row of rows) {
|
|
225
|
+
if (row.index === undefined)
|
|
226
|
+
continue;
|
|
227
|
+
const message = row.message;
|
|
228
|
+
if (message.role === "user" && textOf(message.content).length)
|
|
229
|
+
units.user.push({ position: row.position, index: row.index, count: 1 });
|
|
230
|
+
else if (message.role === "toolResult")
|
|
231
|
+
units.toolResult.push({ position: row.position, index: row.index, count: 1 });
|
|
232
|
+
else if (message.role === "assistant" && !isDiscardedResponse(message)) {
|
|
233
|
+
const count = message.content.filter(part => part.type === "text" && part.text.trim()).length;
|
|
234
|
+
if (count)
|
|
235
|
+
units.assistant.push({ position: row.position, index: row.index, count });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
const prefixes = Object.fromEntries(Object.entries(units).map(([role, list]) => {
|
|
239
|
+
const end = new Int32Array(entries.length + 1), count = new Int32Array(list.length + 1);
|
|
240
|
+
let seen = 0;
|
|
241
|
+
for (let at = 0; at <= entries.length; at++) {
|
|
242
|
+
while (seen < list.length && list[seen].position < at)
|
|
243
|
+
seen++;
|
|
244
|
+
end[at] = seen;
|
|
230
245
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
246
|
+
list.forEach((unit, at) => count[at + 1] = count[at] + unit.count);
|
|
247
|
+
return [role, { end, count }];
|
|
248
|
+
}));
|
|
249
|
+
const quoteCache = new Map();
|
|
250
|
+
const quote = (entry, take) => {
|
|
251
|
+
const key = `${entry.id}:${take}`;
|
|
252
|
+
let cached = quoteCache.get(key);
|
|
253
|
+
if (cached)
|
|
254
|
+
return cached;
|
|
255
|
+
const index = indices.get(entry.id);
|
|
256
|
+
const text = entry.type === "message" && entry.message.role === "user" ? textOf(entry.message.content) : "";
|
|
257
|
+
const sourceTimes = index === undefined ? undefined : [{ sourceIndex: index, ...resolveSourceTime(entry) }];
|
|
258
|
+
const records = index === undefined ? [] : take === Infinity
|
|
259
|
+
? [{ kind: "excerpt", role: "user", sourceIndex: index, text, sourceTimes }]
|
|
260
|
+
: userPrefixRecords({ kind: "user", text, originalText: text, startsSource: true, sourceIndex: index }, undefined, take).map(record => ({ ...record, sourceTimes }));
|
|
261
|
+
cached = { records, cost: lineCost(records), whole: take >= text.length };
|
|
262
|
+
quoteCache.set(key, cached);
|
|
263
|
+
return cached;
|
|
236
264
|
};
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
265
|
+
const highAt = (boundary, length) => {
|
|
266
|
+
const kept = [], selected = new Set(), whole = new Set();
|
|
267
|
+
let cost = 0;
|
|
268
|
+
const add = (entry, take) => {
|
|
269
|
+
const quoted = quote(entry, take), index = indices.get(entry.id);
|
|
270
|
+
kept.push(...quoted.records);
|
|
271
|
+
cost += quoted.cost;
|
|
272
|
+
if (index !== undefined && quoted.records.length) {
|
|
273
|
+
selected.add(index);
|
|
274
|
+
if (quoted.whole)
|
|
275
|
+
whole.add(index);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
if (userPosition >= 0 && userPosition < boundary && textOnly)
|
|
279
|
+
add(current, Infinity);
|
|
280
|
+
for (const entry of anchors)
|
|
281
|
+
if (entry.id !== current?.id && positions.get(entry.id) < boundary)
|
|
282
|
+
add(entry, length);
|
|
283
|
+
const omissions = [];
|
|
284
|
+
for (const role of ["user", "assistant", "toolResult"]) {
|
|
285
|
+
const list = units[role], prefix = prefixes[role];
|
|
286
|
+
let first = 0, last = prefix.end[boundary] - 1;
|
|
287
|
+
if (role === "user") {
|
|
288
|
+
while (first <= last && selected.has(list[first].index))
|
|
289
|
+
first++;
|
|
290
|
+
while (last >= first && selected.has(list[last].index))
|
|
291
|
+
last--;
|
|
292
|
+
}
|
|
293
|
+
if (last < first)
|
|
294
|
+
continue;
|
|
295
|
+
const a = list[first], b = list[last];
|
|
296
|
+
let count = prefix.count[last + 1] - prefix.count[first];
|
|
297
|
+
if (role === "user")
|
|
298
|
+
for (const index of whole)
|
|
299
|
+
if (index >= a.index && index <= b.index)
|
|
300
|
+
count--;
|
|
301
|
+
if (count)
|
|
302
|
+
omissions.push(omissionRecord(role, count, [a.index, b.index], false));
|
|
303
|
+
}
|
|
304
|
+
const rows = [...kept, ...omissions], totalCost = cost + lineCost(omissions);
|
|
305
|
+
return { kept, whole, omissions, rows, cost: totalCost, length: frameLength(totalCost, rows.length) };
|
|
253
306
|
};
|
|
254
|
-
const
|
|
255
|
-
const
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
307
|
+
const calibrationHigh = highAt(floorBoundary, minimumOpeningLength);
|
|
308
|
+
const floorSummary = render(calibrationHigh.rows), floorProjection = preview(floorSummary, floorBoundary);
|
|
309
|
+
const offset = measure(floorProjection) - nativeAt(floorBoundary) - summaryTokens(floorSummary.length);
|
|
310
|
+
const price = (characters, boundary) => offset + nativeAt(boundary) + summaryTokens(characters);
|
|
311
|
+
const listCache = new Map();
|
|
312
|
+
const listsAt = (boundary) => {
|
|
313
|
+
const cached = listCache.get(boundary);
|
|
314
|
+
if (cached)
|
|
315
|
+
return cached;
|
|
316
|
+
const compacted = entries.slice(0, boundary), resolves = (entry) => readable(entry) !== undefined;
|
|
317
|
+
const files = ledgerItems(compacted, indices, resolves, failedCalls), actions = actionItems(compacted, indices, resolves, failedCalls);
|
|
318
|
+
const lists = { files, actions, records: [...actionRecords(actions), ...ledgerRecords(files)] };
|
|
319
|
+
listCache.set(boundary, lists);
|
|
320
|
+
return lists;
|
|
260
321
|
};
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
// The
|
|
267
|
-
//
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
if (
|
|
281
|
-
|
|
282
|
-
if (fits(floor.base))
|
|
283
|
-
break;
|
|
284
|
-
}
|
|
322
|
+
const preferredHigh = highAt(preferredBoundary, openingLength), preferredLists = listsAt(preferredBoundary);
|
|
323
|
+
const fixedRowsCost = preferredHigh.cost + lineCost(preferredLists.records);
|
|
324
|
+
const baseCharacters = frameLength(fixedRowsCost, preferredHigh.rows.length + preferredLists.records.length);
|
|
325
|
+
const base = price(baseCharacters, preferredBoundary);
|
|
326
|
+
const target = carriedTarget(model.contextWindow, reserveTokens, base, before);
|
|
327
|
+
// The old tail rule supplies an ordering boundary only. Its recent
|
|
328
|
+
// suffix follows the openings, and is charged to the same total grant.
|
|
329
|
+
const boundaries = [floorBoundary];
|
|
330
|
+
for (let at = floorBoundary - 1; at >= preferredBoundary; at--) {
|
|
331
|
+
const entry = entries[at];
|
|
332
|
+
if ((entry.type === "message" && entry.message.role !== "toolResult") || entry.type === "custom_message" || entry.type === "branch_summary")
|
|
333
|
+
boundaries.push(at);
|
|
334
|
+
}
|
|
335
|
+
boundaries.reverse();
|
|
336
|
+
const tailFor = (length) => {
|
|
337
|
+
// Longest suffix first: the first fit is the preferred rank. Most
|
|
338
|
+
// cuts keep that entire suffix, so no other boundaries need pricing.
|
|
339
|
+
for (const boundary of boundaries) {
|
|
340
|
+
const high = highAt(boundary, length);
|
|
341
|
+
if (price(high.length, boundary) <= target)
|
|
342
|
+
return { boundary, high };
|
|
285
343
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
344
|
+
return undefined;
|
|
345
|
+
};
|
|
346
|
+
let selected = tailFor(minimumOpeningLength);
|
|
347
|
+
if (!selected)
|
|
348
|
+
return { unavailable: "pi-tuck's mandatory context cannot fit or reduce the request budget",
|
|
349
|
+
capacityRejected: price(calibrationHigh.length, floorBoundary) + reserveTokens >= model.contextWindow,
|
|
350
|
+
floorMessages: floorProjection };
|
|
351
|
+
let low = minimumOpeningLength, high = openingLength;
|
|
352
|
+
while (low < high) {
|
|
353
|
+
const middle = Math.ceil((low + high) / 2);
|
|
354
|
+
if (tailFor(middle))
|
|
355
|
+
low = middle;
|
|
356
|
+
else
|
|
357
|
+
high = middle - 1;
|
|
358
|
+
}
|
|
359
|
+
selected = tailFor(low);
|
|
360
|
+
let { boundary, high: protectedView } = selected;
|
|
361
|
+
const sourceRows = rows.filter(row => row.position < boundary);
|
|
362
|
+
const lists = listsAt(boundary);
|
|
363
|
+
let ledger = lists.records;
|
|
364
|
+
// List caps are existing ranking policy; only the carried target is a
|
|
365
|
+
// spending limit. Candidate list sizes have fixed, cached line prices.
|
|
366
|
+
if (price(frameLength(protectedView.cost + lineCost(ledger), protectedView.rows.length + ledger.length), boundary) > target) {
|
|
367
|
+
ledger = [];
|
|
368
|
+
for (let keep = 0; keep <= Math.min(Math.max(MAX_LEDGER_LINES, MAX_ACTION_LINES), Math.max(lists.files.length, lists.actions.length)); keep++) {
|
|
369
|
+
const candidate = [...actionRecords(lists.actions, keep), ...ledgerRecords(lists.files, keep)];
|
|
370
|
+
if (price(frameLength(protectedView.cost + lineCost(candidate), protectedView.rows.length + candidate.length), boundary) <= target)
|
|
371
|
+
ledger = candidate;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
const mandatory = new Set([...protectedView.whole].filter(index => {
|
|
375
|
+
const row = users.find(row => row.index === index);
|
|
376
|
+
return row?.message.role === "user" && isTextOnly(row.message.content);
|
|
377
|
+
}));
|
|
378
|
+
const fixed = [...protectedView.kept, ...ledger], fixedCost = lineCost(fixed);
|
|
379
|
+
const maxSummaryCharacters = Math.floor((target - offset - nativeAt(boundary)) * 4) - wrapperCharacters;
|
|
380
|
+
const maxBriefChars = Math.max(0, maxSummaryCharacters - fixedCost);
|
|
381
|
+
const compileInput = { messages: sourceRows.map(row => row.message), sourceIndices: sourceRows.map(row => row.index),
|
|
382
|
+
sourceEntryIds: sourceRows.map(row => row.id), sourceTimes: sourceRows.map(row => resolveSourceTime(byId.get(row.id))),
|
|
383
|
+
ranking: { maxBriefChars, mandatorySourceIndices: mandatory } };
|
|
384
|
+
const imageLabels = new Map();
|
|
385
|
+
for (const row of sourceRows)
|
|
386
|
+
if (row.message.role === "user" && Array.isArray(row.message.content))
|
|
387
|
+
imageLabels.set(row.index, new Set(row.message.content.flatMap(part => part.type === "image" ? [`[image: ${part.mimeType}]`] : [])));
|
|
388
|
+
const compiledBoundary = boundary;
|
|
389
|
+
const assemble = (compiled, at = compiledBoundary) => {
|
|
390
|
+
const split = compiled ? splitSummary(compiled) : undefined;
|
|
391
|
+
if (compiled && !split)
|
|
299
392
|
throw new Error("pi-tuck returned an invalid summary");
|
|
300
|
-
const
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
393
|
+
const inputRecords = split ? parseRecords(split.body, split.version) : protectedView.omissions;
|
|
394
|
+
const expanded = new Set(protectedView.kept.filter(record => record.kind === "excerpt" && inputRecords.some(row => row.kind === "excerpt" && row.role === "user" && row.sourceIndex === record.sourceIndex && row.text.startsWith(record.text) && row.text.length >= record.text.length)).map(record => record.sourceIndex));
|
|
395
|
+
const anchored = new Set(protectedView.kept.map(record => record.sourceIndex));
|
|
396
|
+
const moved = new Set(rows.filter(row => row.position >= at && row.position < compiledBoundary).map(row => row.index));
|
|
397
|
+
const records = [...inputRecords.filter(record => {
|
|
398
|
+
if (record.sourceIndex !== undefined && moved.has(record.sourceIndex))
|
|
399
|
+
return false;
|
|
400
|
+
if (record.kind === "excerpt" && record.role === "user" && imageLabels.get(record.sourceIndex)?.has(record.text))
|
|
401
|
+
return true;
|
|
402
|
+
return record.role !== "user" || record.sourceIndex === undefined || !anchored.has(record.sourceIndex) || expanded.has(record.sourceIndex);
|
|
403
|
+
}), ...protectedView.kept.filter(record => !expanded.has(record.sourceIndex) && !moved.has(record.sourceIndex))];
|
|
404
|
+
// Mixed user turns keep their text opening, while their image
|
|
405
|
+
// metadata remains in the ordinary lower-ranked dialogue lane.
|
|
406
|
+
const wholeUser = new Set(records.filter(record => record.kind === "excerpt" && record.role === "user" && record.text === userTexts.get(record.sourceIndex)).map(record => record.sourceIndex));
|
|
407
|
+
const wholeAssistant = new Map();
|
|
408
|
+
for (const record of records)
|
|
409
|
+
if (record.kind === "excerpt" && record.role === "assistant" && !record.span && record.sourceIndex !== undefined)
|
|
410
|
+
wholeAssistant.set(record.sourceIndex, (wholeAssistant.get(record.sourceIndex) ?? 0) + 1);
|
|
411
|
+
for (let pos = records.length - 1; pos >= 0; pos--) {
|
|
412
|
+
const record = records[pos];
|
|
413
|
+
if (record.kind !== "omission" || !record.sourceRange)
|
|
414
|
+
continue;
|
|
415
|
+
if (record.role === "user") {
|
|
416
|
+
const missing = units.user.filter(unit => unit.position < at && unit.index >= record.sourceRange.start && unit.index <= record.sourceRange.end && !wholeUser.has(unit.index));
|
|
417
|
+
if (!missing.length)
|
|
418
|
+
records.splice(pos, 1);
|
|
419
|
+
else
|
|
420
|
+
records[pos] = omissionRecord("user", missing.length, [missing[0].index, missing.at(-1).index], false);
|
|
421
|
+
}
|
|
422
|
+
else if (at !== compiledBoundary) {
|
|
423
|
+
const role = record.role;
|
|
424
|
+
if (role === "assistant" || role === "toolResult") {
|
|
425
|
+
const missing = units[role].filter(unit => unit.position < at && unit.index >= record.sourceRange.start && unit.index <= record.sourceRange.end &&
|
|
426
|
+
(role === "toolResult" || unit.count > (wholeAssistant.get(unit.index) ?? 0)));
|
|
427
|
+
const count = missing.reduce((n, unit) => n + unit.count - (role === "assistant" ? wholeAssistant.get(unit.index) ?? 0 : 0), 0);
|
|
428
|
+
if (!count)
|
|
429
|
+
records.splice(pos, 1);
|
|
430
|
+
else
|
|
431
|
+
records[pos] = omissionRecord(role, count, [missing[0].index, missing.at(-1).index], false);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
304
434
|
}
|
|
305
|
-
orderByOriginalSource(records, sourceIndices,
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
const summary = serializeTranscript(records) + DIALOGUE_SUFFIX;
|
|
309
|
-
const projection = preview(summary);
|
|
310
|
-
assertNativeToolRounds(projection);
|
|
311
|
-
return { summary, after: measure(projection) };
|
|
435
|
+
orderByOriginalSource(records, sourceIndices, originals.selectedIds);
|
|
436
|
+
const summary = render([...records, ...ledger]);
|
|
437
|
+
return { summary, after: price(summary.length, at), records };
|
|
312
438
|
};
|
|
313
|
-
const
|
|
314
|
-
|
|
315
|
-
//
|
|
316
|
-
//
|
|
317
|
-
const
|
|
318
|
-
const
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
439
|
+
const compiled = tuck.compileDialogue(compileInput);
|
|
440
|
+
let offer = assemble(compiled.text);
|
|
441
|
+
// Only capacity left after all higher ranks can extend the native tail
|
|
442
|
+
// beyond its preferred rank boundary. No old allocator is consulted.
|
|
443
|
+
const liveStartId = calibration.ok ? calibration.selectedIds[0] : undefined;
|
|
444
|
+
const liveStart = liveStartId === undefined ? preferredBoundary : positions.get(liveStartId);
|
|
445
|
+
if (boundary > liveStart) {
|
|
446
|
+
const recordCosts = new Float64Array(entries.length + 1), recordCounts = new Int32Array(entries.length + 1);
|
|
447
|
+
const sourcePositions = new Map(rows.map(row => [row.index, row.position]));
|
|
448
|
+
const wholeUsers = new Set(offer.records.filter(record => record.kind === "excerpt" && record.role === "user" &&
|
|
449
|
+
record.text === userTexts.get(record.sourceIndex)).map(record => record.sourceIndex));
|
|
450
|
+
const wholeAssistants = new Map();
|
|
451
|
+
for (const record of offer.records) {
|
|
452
|
+
const at = record.sourceIndex === undefined ? undefined : sourcePositions.get(record.sourceIndex);
|
|
453
|
+
if (at !== undefined) {
|
|
454
|
+
recordCosts[at + 1] += lineCost([record]);
|
|
455
|
+
recordCounts[at + 1]++;
|
|
456
|
+
}
|
|
457
|
+
if (record.kind === "excerpt" && record.role === "assistant" && !record.span && record.sourceIndex !== undefined)
|
|
458
|
+
wholeAssistants.set(record.sourceIndex, (wholeAssistants.get(record.sourceIndex) ?? 0) + 1);
|
|
459
|
+
}
|
|
460
|
+
for (let at = 1; at <= entries.length; at++) {
|
|
461
|
+
recordCosts[at] += recordCosts[at - 1];
|
|
462
|
+
recordCounts[at] += recordCounts[at - 1];
|
|
463
|
+
}
|
|
464
|
+
// A tail extension removes source records and shortens at most three
|
|
465
|
+
// counted ranges. Price those changes from prefix sums; render only the
|
|
466
|
+
// winning suffix, rather than parsing/serializing the dialogue per step.
|
|
467
|
+
const ranges = offer.records.flatMap(record => {
|
|
468
|
+
if (record.kind !== "omission" || !record.sourceRange || !["user", "assistant", "toolResult"].includes(record.role ?? ""))
|
|
469
|
+
return [];
|
|
470
|
+
const role = record.role;
|
|
471
|
+
const missing = units[role].flatMap(unit => {
|
|
472
|
+
const count = role === "user" ? Number(!wholeUsers.has(unit.index)) : unit.count - (role === "assistant" ? wholeAssistants.get(unit.index) ?? 0 : 0);
|
|
473
|
+
return count && unit.position < compiledBoundary && unit.index >= record.sourceRange.start && unit.index <= record.sourceRange.end ? [{ ...unit, count }] : [];
|
|
474
|
+
});
|
|
475
|
+
const ends = new Int32Array(entries.length + 1), counts = new Int32Array(missing.length + 1);
|
|
476
|
+
let seen = 0;
|
|
477
|
+
for (let at = 0; at <= entries.length; at++) {
|
|
478
|
+
while (seen < missing.length && missing[seen].position < at)
|
|
479
|
+
seen++;
|
|
480
|
+
ends[at] = seen;
|
|
481
|
+
}
|
|
482
|
+
missing.forEach((unit, at) => counts[at + 1] = counts[at] + unit.count);
|
|
483
|
+
return [{ role, missing, ends, counts, cost: lineCost([record]) }];
|
|
484
|
+
});
|
|
485
|
+
const baseCost = lineCost(offer.records) + lineCost(ledger), baseCount = offer.records.length + ledger.length;
|
|
486
|
+
const extensionPrice = (at) => {
|
|
487
|
+
let cost = baseCost - (recordCosts[compiledBoundary] - recordCosts[at]);
|
|
488
|
+
let count = baseCount - (recordCounts[compiledBoundary] - recordCounts[at]);
|
|
489
|
+
for (const range of ranges) {
|
|
490
|
+
const end = range.ends[at];
|
|
491
|
+
cost -= range.cost;
|
|
492
|
+
if (end)
|
|
493
|
+
cost += lineCost([omissionRecord(range.role, range.counts[end], [range.missing[0].index, range.missing[end - 1].index], false)]);
|
|
494
|
+
else
|
|
495
|
+
count--;
|
|
496
|
+
}
|
|
497
|
+
return price(frameLength(cost, count), at);
|
|
498
|
+
};
|
|
499
|
+
for (let at = boundary - 1; at >= liveStart; at--) {
|
|
500
|
+
const entry = entries[at];
|
|
501
|
+
if (entry.type !== "message" && entry.type !== "custom_message" && entry.type !== "branch_summary")
|
|
502
|
+
continue;
|
|
503
|
+
if (entry.type === "message" && entry.message.role === "toolResult")
|
|
504
|
+
continue;
|
|
505
|
+
if (extensionPrice(at) <= target)
|
|
506
|
+
boundary = at;
|
|
507
|
+
else
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
510
|
+
if (boundary !== compiledBoundary)
|
|
511
|
+
offer = assemble(compiled.text, boundary);
|
|
512
|
+
}
|
|
513
|
+
const projection = preview(offer.summary, boundary);
|
|
514
|
+
assertNativeToolRounds(projection);
|
|
515
|
+
const after = measure(projection);
|
|
327
516
|
if (after + reserveTokens >= model.contextWindow || after >= before)
|
|
328
|
-
return { unavailable: "pi-tuck's draft does not reduce context within the request budget", capacityRejected: after + reserveTokens >= model.contextWindow, floorMessages:
|
|
329
|
-
const mode =
|
|
330
|
-
return { compaction: { summary
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
if (preferred.mode === "dialogue")
|
|
334
|
-
return preferred;
|
|
335
|
-
if (!preferred.compaction && !preferred.capacityRejected)
|
|
336
|
-
return preferred;
|
|
337
|
-
const preferredBoundary = cut.compactAll ? entries.length : entries.findIndex(entry => entry.id === cut.firstKeptEntryId);
|
|
338
|
-
let floorBoundary = freshRound ? lastResponse : entries.length;
|
|
339
|
-
if (freshBash >= 0)
|
|
340
|
-
floorBoundary = Math.min(floorBoundary, freshBash);
|
|
341
|
-
if (userPosition >= 0 && (!textOnly || userPosition > lastResponse))
|
|
342
|
-
floorBoundary = Math.min(floorBoundary, userPosition);
|
|
343
|
-
if (floorBoundary <= preferredBoundary)
|
|
344
|
-
return preferred;
|
|
345
|
-
const prefix = tuck.buildOwnCut(entries.slice(0, floorBoundary), 0);
|
|
346
|
-
if (!prefix.ok)
|
|
347
|
-
return preferred;
|
|
348
|
-
const retried = compileAtCut({ ...cutBefore(cut, floorBoundary, prefix), budgetCut: undefined });
|
|
349
|
-
return preferred.compaction && retried.mode !== "dialogue" ? preferred : retried;
|
|
517
|
+
return { unavailable: "pi-tuck's draft does not reduce context within the request budget", capacityRejected: after + reserveTokens >= model.contextWindow, floorMessages: floorProjection };
|
|
518
|
+
const mode = compiled.step === "whole" ? "dialogue" : "shrunk";
|
|
519
|
+
return { compaction: { summary: offer.summary, firstKeptEntryId: entries[boundary]?.id ?? "", tokensBefore: input.tokensBefore,
|
|
520
|
+
details: { ...detailsAt(boundary), tokensAfter: after, carriedTarget: target, view: mode === "dialogue" ? "complete-dialogue" : "shrunk-dialogue", mode } }, after, mode };
|
|
521
|
+
}
|
|
350
522
|
}
|
|
351
523
|
/** Numeric references use the original session index, then intersect with readable current-branch entries. */
|
|
352
524
|
export function tuckRecall(tuck, entries, branch, input, policy = {}) {
|