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/src/tuck.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
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
|
-
import { parseRecords, serializeTranscript, splitSummary, type TranscriptRecord } from "./tuck/core/transcript";
|
|
4
|
+
import { parseRecords, serializeTranscript, serializeRecord, splitSummary, TRANSCRIPT_HEADER, type TranscriptRecord } from "./tuck/core/transcript";
|
|
5
5
|
import { DIALOGUE_SUFFIX } from "./tuck/core/format";
|
|
6
|
+
import { carriedTailTokens, carriedTarget, conversationTokens } from "./budget";
|
|
7
|
+
import { omissionRecord, userPrefixRecords } from "./tuck/core/brief";
|
|
6
8
|
import { isTextOnly, textOf } from "./tuck/core/content";
|
|
7
|
-
import {
|
|
9
|
+
import { MAX_ACTION_LINES, MAX_LEDGER_LINES, actionItems, actionRecords, ledgerItems, ledgerRecords } from "./ledger.js";
|
|
8
10
|
import type { Message } from "@earendil-works/pi-ai";
|
|
9
11
|
import { convertToLlm, SessionManager, type ExtensionContext,
|
|
10
12
|
type SessionBeforeCompactEvent, type SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
@@ -14,8 +16,11 @@ import { ATTEMPT_NOTICE, createRecall, type RecallPolicy,
|
|
|
14
16
|
type AgentMessage = Parameters<typeof convertToLlm>[0][number];
|
|
15
17
|
type Cut = import("./tuck/cut").OwnCutResult;
|
|
16
18
|
/** Counts a projection of the conversation exactly as the triggers, the post-cut checks and the guard do (budget.ts). */
|
|
17
|
-
export type Measure = (messages: AgentMessage[]) => number
|
|
18
|
-
/** The
|
|
19
|
+
export type Measure = ((messages: AgentMessage[]) => number) & {
|
|
20
|
+
/** The same per-message estimate, including any temporary native result clip. */
|
|
21
|
+
messageTokens?: (message: AgentMessage) => number;
|
|
22
|
+
};
|
|
23
|
+
/** Diagnostic description of the single ranked compiler result. Never a decision input. */
|
|
19
24
|
export type DraftMode = "dialogue" | "shrunk";
|
|
20
25
|
type Draft = { compaction: Pick<Extract<SessionEntry, { type: "compaction" }>, "summary" | "firstKeptEntryId" | "tokensBefore" | "details">; after: number; mode: DraftMode; unavailable?: never; capacityRejected?: never }
|
|
21
26
|
// The actual floor projection lets the caller measure it without the fresh results it may clip.
|
|
@@ -30,12 +35,9 @@ export interface Tuck {
|
|
|
30
35
|
applyTailBudget(entries: SessionEntry[], cut: Extract<Cut, { ok: true }>, options: { charsPerToken: number; maxTokens?: number; oversizedFactor?: number }): Cut;
|
|
31
36
|
calibrateCharsPerToken(chars: number, tokens: number): { charsPerToken: number };
|
|
32
37
|
buildGlobalIndexById(entries: readonly SessionEntry[]): Map<string, number>;
|
|
33
|
-
/**
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
ranking: { maxBriefChars: number; mandatorySourceIndices?: ReadonlySet<number> } }): string;
|
|
37
|
-
/** The whole pre-cut dialogue view; "" offers none, and the shrunk view is used. */
|
|
38
|
-
compileDialogue(input: Parameters<Tuck["compileShrunkDialogue"]>[0]): string;
|
|
38
|
+
/** Fill the lower ranks once within the remaining serialized amount. */
|
|
39
|
+
compileDialogue(input: { messages: AgentMessage[]; sourceIndices: (number | undefined)[]; sourceEntryIds?: readonly string[]; sourceTimes?: readonly SourceTime[];
|
|
40
|
+
ranking: { maxBriefChars: number; mandatorySourceIndices?: ReadonlySet<number> } }): ReturnType<typeof compileDialogue>;
|
|
39
41
|
prepareKeywordQuery(query: string): PreparedKeywordQuery;
|
|
40
42
|
searchEntriesDetailed(entries: SearchHit[], messages: Message[], query: string,
|
|
41
43
|
tuning: { relativeFloor: number; cap: number }): {
|
|
@@ -45,19 +47,10 @@ export interface Tuck {
|
|
|
45
47
|
import { buildOwnCut, applyTailBudget, MAX_TAIL_TOKENS, OVERSIZED_TAIL_FACTOR } from "./tuck/cut";
|
|
46
48
|
import { calibrateCharsPerToken, estimateSourceMessageChars } from "./tuck/core/token-estimate";
|
|
47
49
|
import { buildGlobalIndexById } from "./tuck/core/global-indices";
|
|
48
|
-
import {
|
|
50
|
+
import { compileDialogue } from "./tuck/core/summarize";
|
|
49
51
|
import { prepareKeywordQuery } from "./tuck/core/keyword-query";
|
|
50
52
|
import { searchEntriesDetailed } from "./tuck/core/search-entries";
|
|
51
|
-
export const tuck: Tuck = { buildOwnCut, applyTailBudget, calibrateCharsPerToken, buildGlobalIndexById,
|
|
52
|
-
/** The one historical allowance: at most 25% of remaining estimated room, for both views.
|
|
53
|
-
* The whole dialogue is offered only within it (plus actual preview/headroom guards);
|
|
54
|
-
* otherwise the same dialogue is shrunk to fit it. The 4-character conversion matches pinned Pi's
|
|
55
|
-
* summary estimator, not a provider-token guarantee. Mandatory sources (current input,
|
|
56
|
-
* initiating prompt, file ledger) are already charged in base. */
|
|
57
|
-
export function historicalAllowance(window: number, reserve: number, base: number, before: number): number {
|
|
58
|
-
const free = Math.max(0, window - reserve - base - 1);
|
|
59
|
-
return Math.max(0, Math.min(Math.floor(free / 4), before - base - 1)) * 4;
|
|
60
|
-
}
|
|
53
|
+
export const tuck: Tuck = { buildOwnCut, applyTailBudget, calibrateCharsPerToken, buildGlobalIndexById, compileDialogue, prepareKeywordQuery, searchEntriesDetailed };
|
|
61
54
|
/** Uses pi-tuck's own cut, calibrated maximum tail and dialogue compiler; Pi owns persistence and continuation. */
|
|
62
55
|
export function tuckDraft(tuck: Tuck, event: SessionBeforeCompactEvent, ctx: ExtensionContext, reserveTokens: number, measure: Measure, policy: RecallPolicy = {}, retainCurrentInput = true, operatorManual = false, newPrompt = false) {
|
|
63
56
|
return tuckObservedDraft(tuck, { branchEntries: event.branchEntries, tokensBefore: event.preparation.tokensBefore, previousSummary: event.preparation.previousSummary, operatorManual, newPrompt }, ctx, reserveTokens, measure, policy, retainCurrentInput);
|
|
@@ -85,6 +78,8 @@ export function tuckObservedDraft(tuck: Tuck, input: { branchEntries: SessionEnt
|
|
|
85
78
|
const chars = (calibration.ok ? calibration.messages.reduce((n, m) => n + estimateSourceMessageChars(m), 0) : 0)
|
|
86
79
|
+ (input.previousSummary?.length ?? 0);
|
|
87
80
|
const { charsPerToken } = tuck.calibrateCharsPerToken(chars, input.tokensBefore);
|
|
81
|
+
// The existing keep-recent rule offers a native suffix. The budget owner
|
|
82
|
+
// bounds its rank and base price below, in the planner's estimate.
|
|
88
83
|
// The planner's reserve includes mandatory input (system, host and incoming prompt), so this is half of the target room *after* that
|
|
89
84
|
// fixed input, not half of the whole room. Current/fresh-source floors below may still require an explicit rejection.
|
|
90
85
|
const inputRoom = Math.floor(Math.max(0, (ctx.model?.contextWindow ?? 0) - reserveTokens - 1) / 2);
|
|
@@ -148,8 +143,7 @@ export function tuckObservedDraft(tuck: Tuck, input: { branchEntries: SessionEnt
|
|
|
148
143
|
const current = entries[userPosition];
|
|
149
144
|
const userContent = current?.type === "message" && current.message.role === "user" ? current.message.content : undefined;
|
|
150
145
|
const textOnly = isTextOnly(userContent);
|
|
151
|
-
//
|
|
152
|
-
// source; when a native edit omitted it, nothing is quoted and no later user source is promoted in its place.
|
|
146
|
+
// Anchor the actual first source only; a native omission never promotes a later prompt.
|
|
153
147
|
const firstUser = branch.find(entry => entry.type === "message" && entry.message.role === "user");
|
|
154
148
|
const firstUserPosition = entries.findIndex(entry => entry.id === firstUser?.id && entry.type === "message");
|
|
155
149
|
const selectedKeptId = cut.firstKeptEntryId;
|
|
@@ -164,173 +158,330 @@ export function tuckObservedDraft(tuck: Tuck, input: { branchEntries: SessionEnt
|
|
|
164
158
|
|
|
165
159
|
if (cut.messages.length !== cut.selectedIds.length || (!cut.compactAll && !entries.some((e) => e.id === cut.firstKeptEntryId)))
|
|
166
160
|
throw new Error("pi-tuck returned an invalid source boundary");
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
//
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
const initiating = firstUserPosition >= 0 && firstUserPosition < boundary && entries[firstUserPosition]!.id !== protectedUser?.id ? entries[firstUserPosition] : undefined;
|
|
195
|
-
const quote = (entry: SessionEntry, budget: number, label: string): TranscriptRecord[] => {
|
|
196
|
-
const content = entry.type === "message" && entry.message.role === "user" ? entry.message.content : undefined;
|
|
197
|
-
if (!isTextOnly(content)) return [];
|
|
198
|
-
const text = textOf(content!), index = indices.get(entry.id);
|
|
199
|
-
if (index === undefined) throw new Error("Missing quoted user source index");
|
|
200
|
-
const quoted = head(text, budget), sourceTimes = [{ sourceIndex: index, ...resolveSourceTime(entry) }];
|
|
201
|
-
if (!text.trim() || !quoted.length) return [];
|
|
202
|
-
return [{ kind: "excerpt", role: "user", sourceIndex: index, text: quoted, sourceTimes }, ...(quoted.length < text.length ? [{ kind: "omission" as const, role: "user" as const,
|
|
203
|
-
sourceIndex: index, reason: `${label} continues: characters ${quoted.length}-${text.length} omitted; read them with tuck_recall {"index":${index},"offset":${quoted.length}}`, sourceTimes }] : [])];
|
|
204
|
-
};
|
|
205
|
-
// Both ends are anchored, in this order: the heads of the newest user turns (at most a quarter of the budget), the head of the first prompt (the
|
|
206
|
-
// goal of a long session), then extensions of the newest turns, then the rest of the first prompt. Heads come first so steps after a task can't
|
|
207
|
-
// use up the budget the task turn needs; the first prompt's head comes before any extension so steering turns can't use up the goal's.
|
|
208
|
-
const quotesFor = (budget: number, headSize: number, withFirst: boolean): TranscriptRecord[] => {
|
|
209
|
-
const turns: { entry: SessionEntry; text: string; take: number }[] = [];
|
|
210
|
-
// Only answered turns: a prompt that was refused and never got a reply was never taken up, and quoting it would spend the budget for nothing.
|
|
211
|
-
let answered = false;
|
|
161
|
+
const budgetCut = cut.budgetCut;
|
|
162
|
+
let preferredBoundary = cut.compactAll ? entries.length : entries.findIndex(entry => entry.id === cut.firstKeptEntryId);
|
|
163
|
+
let floorBoundary = freshRound ? lastResponse : entries.length;
|
|
164
|
+
if (freshBash >= 0) floorBoundary = Math.min(floorBoundary, freshBash);
|
|
165
|
+
if (userPosition >= 0 && (!textOnly || userPosition > lastResponse)) floorBoundary = Math.min(floorBoundary, userPosition);
|
|
166
|
+
// One fill owns the summary and the contiguous native suffix. Protocol/current-input
|
|
167
|
+
// floors are representation constraints; every other source competes by rank.
|
|
168
|
+
return rankedFill();
|
|
169
|
+
|
|
170
|
+
function rankedFill(): Draft {
|
|
171
|
+
const model = ctx.model;
|
|
172
|
+
if (!model) return { unavailable: "pi-tuck needs a model to measure the cut" };
|
|
173
|
+
const indices = tuck.buildGlobalIndexById(ctx.sessionManager.getEntries());
|
|
174
|
+
const header = ctx.sessionManager.getHeader();
|
|
175
|
+
if (!header) throw new Error("Missing native session header");
|
|
176
|
+
const before = measure(projected.messages), room = Math.max(0, model.contextWindow - reserveTokens - 1);
|
|
177
|
+
const minimumOpeningLength = 80, openingLength = Math.max(80, Math.floor(Math.sqrt(room * 4)));
|
|
178
|
+
const positions = new Map(entries.map((entry, at) => [entry.id, at]));
|
|
179
|
+
const originals = tuck.buildOwnCut(entries.filter(entry => entry.type !== "compaction"), 0);
|
|
180
|
+
if (!originals.ok) return { unavailable: "No eligible original prefix to compile" };
|
|
181
|
+
const byId = new Map(entries.map(entry => [entry.id, entry]));
|
|
182
|
+
const sourceIndices = originals.selectedIds.map(id => indices.get(id));
|
|
183
|
+
const rows = originals.selectedIds.map((id, at) => ({ id, position: positions.get(id)!, index: sourceIndices[at], message: originals.messages[at] as AgentMessage }));
|
|
184
|
+
const users = rows.filter(row => row.message.role === "user");
|
|
185
|
+
const userTexts = new Map(users.map(row => [row.index, textOf((row.message as Extract<AgentMessage, { role: "user" }>).content)]));
|
|
186
|
+
let replied = false;
|
|
187
|
+
const answered: SessionEntry[] = [];
|
|
212
188
|
for (let at = entries.length - 1; at >= 0; at--) {
|
|
213
189
|
const entry = entries[at]!;
|
|
214
|
-
if (entry.type
|
|
215
|
-
if (entry.
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const text = textOf(entry.message.content);
|
|
220
|
-
if (text.trim()) turns.push({ entry, text, take: 0 });
|
|
190
|
+
if (entry.type !== "message") continue;
|
|
191
|
+
if (entry.message.role === "assistant" && !isDiscardedResponse(entry.message) && entry.message.content.length) { replied = true; continue; }
|
|
192
|
+
if (entry.message.role !== "user") continue;
|
|
193
|
+
if (replied && textOf(entry.message.content).length) answered.push(entry);
|
|
194
|
+
replied = false;
|
|
221
195
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
return { ledger, messages, base: measure(messages), choice };
|
|
257
|
-
};
|
|
258
|
-
const fits = (base: number) => Boolean(model) && base + reserveTokens < model!.contextWindow && base < before;
|
|
259
|
-
// When room is tight the newest turns win over the first prompt, which in a long-running session is often a finished task. The floor gives way
|
|
260
|
-
// in this order: the trail and ledger to their headings, the extensions (the budget halved), then the heads, the first prompt's among them
|
|
261
|
-
// (300 characters, 2,000 for the first prompt; then 100 and 667), then everything but the first prompt (as before user turns were quoted),
|
|
262
|
-
// and only then every quote. Only a floor with no quote but the current input can refuse the cut.
|
|
263
|
-
// The budget grows with the room: 15% of it, at least the 12,000 characters a first prompt always had. A long-running session's task can be tens
|
|
264
|
-
// of thousands of characters, and the room is what can afford it.
|
|
265
|
-
const taskBudget = Math.max(MAX_TASK_CONTRACT_CHARACTERS, Math.floor(Math.max(0, (model?.contextWindow ?? 0) - reserveTokens) * charsPerToken * 0.15));
|
|
266
|
-
const budgets = [taskBudget, taskBudget / 2, taskBudget / 4, taskBudget / 8, 1500].map(Math.floor).filter((b, at, all) => b >= 1500 && all.indexOf(b) === at);
|
|
267
|
-
const choices = [{ head: 300, budgets }, { head: 100, budgets: [1500, 600] }]
|
|
268
|
-
.flatMap(({ head, budgets }) => budgets.map(budget => ({ budget, head, first: true }))).concat([{ budget: MAX_TASK_CONTRACT_CHARACTERS, head: 0, first: true }, { budget: 0, head: 0, first: false }]);
|
|
269
|
-
// When even the smallest floor cannot fit, no step of the ladder can: refuse at once instead of building every preview.
|
|
270
|
-
const smallest = floorAt(0, choices[choices.length - 1]!);
|
|
271
|
-
let floor = smallest;
|
|
272
|
-
if (fits(smallest.base)) for (const choice of choices) {
|
|
273
|
-
floor = floorAt(Math.max(items.length, actions.length), choice);
|
|
274
|
-
if (fits(floor.base)) break;
|
|
275
|
-
if (items.length || actions.length) { floor = floorAt(0, choice); if (fits(floor.base)) break; }
|
|
276
|
-
}
|
|
277
|
-
const { ledger, base } = floor;
|
|
278
|
-
const kept = [...mandatory, ...quotesFor(floor.choice.budget, floor.choice.head, floor.choice.first)];
|
|
279
|
-
if (!model || !fits(base))
|
|
280
|
-
return { unavailable: "pi-tuck's mandatory context cannot fit or reduce the request budget", capacityRejected: Boolean(model && base + reserveTokens >= model.contextWindow), floorMessages: floor.messages };
|
|
281
|
-
const maxBriefChars = historicalAllowance(model.contextWindow, reserveTokens, base, before);
|
|
282
|
-
const compileInput = { messages, sourceIndices, sourceEntryIds: originalCut.selectedIds, sourceTimes: originalCut.selectedIds.map(id => resolveSourceTime(entryById.get(id)!)), receiptContext,
|
|
283
|
-
ranking: { maxBriefChars, mandatorySourceIndices: new Set(kept.flatMap(r => r.sourceIndex === undefined ? [] : [r.sourceIndex])) },
|
|
284
|
-
};
|
|
285
|
-
// Both views use the same parser, mandatory insertion, native preview and guard, and end with the same closing section.
|
|
286
|
-
const assemble = (compiled: string) => {
|
|
287
|
-
if (typeof compiled !== "string") throw new Error("pi-tuck returned an invalid summary");
|
|
288
|
-
const split = compiled ? splitSummary(compiled) : { body: "", version: 2 as const };
|
|
289
|
-
if (!split) throw new Error("pi-tuck returned an invalid summary");
|
|
290
|
-
const records = parseRecords(split.body, split.version);
|
|
291
|
-
for (const record of kept) {
|
|
292
|
-
const insertion = records.findIndex(row => row.sourceIndex !== undefined && row.sourceIndex > record.sourceIndex!);
|
|
293
|
-
records.splice(insertion < 0 ? records.length : insertion, 0, record);
|
|
196
|
+
const anchors = [...new Map([...answered.slice(0, 8), ...(firstUserPosition >= 0 ? [entries[firstUserPosition]!] : [])].map(entry => [entry.id, entry])).values()];
|
|
197
|
+
const failedCalls = new Set(entries.flatMap(entry => entry.type === "message" && entry.message.role === "toolResult" && entry.message.isError ? [entry.message.toolCallId] : []));
|
|
198
|
+
const detailsAt = (boundary: number) => ({ compactor: "pi-tuck", version: 2, adapter: "standalone",
|
|
199
|
+
keptUserTurns: users.reduce((n, row) => n + Number(row.position >= boundary), 0),
|
|
200
|
+
...(boundary === preferredBoundary && budgetCut ? { budgetCut } : {}) });
|
|
201
|
+
const preview = (summary: string, boundary: number) => {
|
|
202
|
+
const manager = SessionManager.inMemory(ctx.cwd, undefined, [header, ...branch]);
|
|
203
|
+
manager.appendCompaction(summary, entries[boundary]?.id ?? "", input.tokensBefore, detailsAt(boundary), true);
|
|
204
|
+
return manager.buildSessionProjection().messages;
|
|
205
|
+
};
|
|
206
|
+
// Price the native suffix once. The measure's additive offset and Pi's
|
|
207
|
+
// summary envelope are calibrated once, rather than replaying a session
|
|
208
|
+
// for each opening, ledger line or tail boundary considered by the fill.
|
|
209
|
+
const rawPositions = new Map(branch.map((entry, at) => [entry.id, at]));
|
|
210
|
+
const native = new Float64Array(branch.length + 1);
|
|
211
|
+
for (const entry of projected.entries) {
|
|
212
|
+
if (entry.sourceEntry.type === "compaction") continue;
|
|
213
|
+
const at = rawPositions.get(entry.sourceEntry.id);
|
|
214
|
+
if (at !== undefined) for (const message of entry.messages)
|
|
215
|
+
native[at] += measure.messageTokens ? measure.messageTokens(message) : conversationTokens([message]);
|
|
216
|
+
}
|
|
217
|
+
for (let at = branch.length - 1; at >= 0; at--) native[at] += native[at + 1]!;
|
|
218
|
+
const nativeAt = (boundary: number) => boundary === entries.length ? 0 : native[rawPositions.get(entries[boundary]!.id)!]!;
|
|
219
|
+
const tailTokens = carriedTailTokens(nativeAt(preferredBoundary));
|
|
220
|
+
// Move only the older end, to the newest whole protocol-safe entries
|
|
221
|
+
// within the shared tail size. Required current/fresh native sources
|
|
222
|
+
// stay whole even when their floor exceeds that preferred amount.
|
|
223
|
+
while (preferredBoundary < floorBoundary && nativeAt(preferredBoundary) > tailTokens) {
|
|
224
|
+
preferredBoundary++;
|
|
225
|
+
while (preferredBoundary < floorBoundary) {
|
|
226
|
+
const entry = entries[preferredBoundary]!;
|
|
227
|
+
if ((entry.type === "message" && entry.message.role !== "toolResult") || entry.type === "custom_message" || entry.type === "branch_summary") break;
|
|
228
|
+
preferredBoundary++;
|
|
229
|
+
}
|
|
294
230
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const
|
|
299
|
-
const
|
|
231
|
+
const envelope = convertToLlm([{ role: "compactionSummary", summary: "", tokensBefore: 0, timestamp: 0 }]);
|
|
232
|
+
const wrapperCharacters = envelope.reduce((n, message) => n + textOf(message.content).length, 0);
|
|
233
|
+
const summaryTokens = (characters: number) => Math.ceil((characters + wrapperCharacters) / 4);
|
|
234
|
+
const lineCost = (records: readonly TranscriptRecord[]) => records.reduce((n, record) => n + serializeRecord(record).length + 1, 0);
|
|
235
|
+
const frameLength = (cost: number, count: number) => TRANSCRIPT_HEADER.length + DIALOGUE_SUFFIX.length + cost + (count ? 0 : 1);
|
|
236
|
+
const render = (records: TranscriptRecord[]) => (serializeTranscript(records) || TRANSCRIPT_HEADER + "\n") + DIALOGUE_SUFFIX;
|
|
237
|
+
type Unit = { position: number; index: number; count: number };
|
|
238
|
+
const units: Record<"user" | "assistant" | "toolResult", Unit[]> = { user: [], assistant: [], toolResult: [] };
|
|
239
|
+
for (const row of rows) {
|
|
240
|
+
if (row.index === undefined) continue;
|
|
241
|
+
const message = row.message;
|
|
242
|
+
if (message.role === "user" && textOf(message.content).length) units.user.push({ position: row.position, index: row.index, count: 1 });
|
|
243
|
+
else if (message.role === "toolResult") units.toolResult.push({ position: row.position, index: row.index, count: 1 });
|
|
244
|
+
else if (message.role === "assistant" && !isDiscardedResponse(message)) {
|
|
245
|
+
const count = message.content.filter(part => part.type === "text" && part.text.trim()).length;
|
|
246
|
+
if (count) units.assistant.push({ position: row.position, index: row.index, count });
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const prefixes = Object.fromEntries(Object.entries(units).map(([role, list]) => {
|
|
250
|
+
const end = new Int32Array(entries.length + 1), count = new Int32Array(list.length + 1);
|
|
251
|
+
let seen = 0;
|
|
252
|
+
for (let at = 0; at <= entries.length; at++) {
|
|
253
|
+
while (seen < list.length && list[seen]!.position < at) seen++;
|
|
254
|
+
end[at] = seen;
|
|
255
|
+
}
|
|
256
|
+
list.forEach((unit, at) => count[at + 1] = count[at]! + unit.count);
|
|
257
|
+
return [role, { end, count }];
|
|
258
|
+
})) as Record<keyof typeof units, { end: Int32Array; count: Int32Array }>;
|
|
259
|
+
const quoteCache = new Map<string, { records: TranscriptRecord[]; cost: number; whole: boolean }>();
|
|
260
|
+
const quote = (entry: SessionEntry, take: number) => {
|
|
261
|
+
const key = `${entry.id}:${take}`;
|
|
262
|
+
let cached = quoteCache.get(key);
|
|
263
|
+
if (cached) return cached;
|
|
264
|
+
const index = indices.get(entry.id);
|
|
265
|
+
const text = entry.type === "message" && entry.message.role === "user" ? textOf(entry.message.content) : "";
|
|
266
|
+
const sourceTimes = index === undefined ? undefined : [{ sourceIndex: index, ...resolveSourceTime(entry) }];
|
|
267
|
+
const records: TranscriptRecord[] = index === undefined ? [] : take === Infinity
|
|
268
|
+
? [{ kind: "excerpt", role: "user", sourceIndex: index, text, sourceTimes }]
|
|
269
|
+
: userPrefixRecords({ kind: "user", text, originalText: text, startsSource: true, sourceIndex: index }, undefined, take).map(record => ({ ...record, sourceTimes }));
|
|
270
|
+
cached = { records, cost: lineCost(records), whole: take >= text.length };
|
|
271
|
+
quoteCache.set(key, cached);
|
|
272
|
+
return cached;
|
|
273
|
+
};
|
|
274
|
+
const highAt = (boundary: number, length: number) => {
|
|
275
|
+
const kept: TranscriptRecord[] = [], selected = new Set<number>(), whole = new Set<number>();
|
|
276
|
+
let cost = 0;
|
|
277
|
+
const add = (entry: SessionEntry, take: number) => {
|
|
278
|
+
const quoted = quote(entry, take), index = indices.get(entry.id);
|
|
279
|
+
kept.push(...quoted.records); cost += quoted.cost;
|
|
280
|
+
if (index !== undefined && quoted.records.length) { selected.add(index); if (quoted.whole) whole.add(index); }
|
|
281
|
+
};
|
|
282
|
+
if (userPosition >= 0 && userPosition < boundary && textOnly) add(current!, Infinity);
|
|
283
|
+
for (const entry of anchors) if (entry.id !== current?.id && positions.get(entry.id)! < boundary) add(entry, length);
|
|
284
|
+
const omissions: TranscriptRecord[] = [];
|
|
285
|
+
for (const role of ["user", "assistant", "toolResult"] as const) {
|
|
286
|
+
const list = units[role], prefix = prefixes[role];
|
|
287
|
+
let first = 0, last = prefix.end[boundary]! - 1;
|
|
288
|
+
if (role === "user") {
|
|
289
|
+
while (first <= last && selected.has(list[first]!.index)) first++;
|
|
290
|
+
while (last >= first && selected.has(list[last]!.index)) last--;
|
|
291
|
+
}
|
|
292
|
+
if (last < first) continue;
|
|
293
|
+
const a = list[first]!, b = list[last]!;
|
|
294
|
+
let count = prefix.count[last + 1]! - prefix.count[first]!;
|
|
295
|
+
if (role === "user") for (const index of whole) if (index >= a.index && index <= b.index) count--;
|
|
296
|
+
if (count) omissions.push(omissionRecord(role, count, [a.index, b.index], false));
|
|
297
|
+
}
|
|
298
|
+
const rows = [...kept, ...omissions], totalCost = cost + lineCost(omissions);
|
|
299
|
+
return { kept, whole, omissions, rows, cost: totalCost, length: frameLength(totalCost, rows.length) };
|
|
300
|
+
};
|
|
301
|
+
const calibrationHigh = highAt(floorBoundary, minimumOpeningLength);
|
|
302
|
+
const floorSummary = render(calibrationHigh.rows), floorProjection = preview(floorSummary, floorBoundary);
|
|
303
|
+
const offset = measure(floorProjection) - nativeAt(floorBoundary) - summaryTokens(floorSummary.length);
|
|
304
|
+
const price = (characters: number, boundary: number) => offset + nativeAt(boundary) + summaryTokens(characters);
|
|
305
|
+
const listCache = new Map<number, { files: ReturnType<typeof ledgerItems>; actions: ReturnType<typeof actionItems>; records: TranscriptRecord[] }>();
|
|
306
|
+
const listsAt = (boundary: number) => {
|
|
307
|
+
const cached = listCache.get(boundary);
|
|
308
|
+
if (cached) return cached;
|
|
309
|
+
const compacted = entries.slice(0, boundary), resolves = (entry: SessionEntry) => readable(entry) !== undefined;
|
|
310
|
+
const files = ledgerItems(compacted, indices, resolves, failedCalls), actions = actionItems(compacted, indices, resolves, failedCalls);
|
|
311
|
+
const lists = { files, actions, records: [...actionRecords(actions), ...ledgerRecords(files)] };
|
|
312
|
+
listCache.set(boundary, lists);
|
|
313
|
+
return lists;
|
|
314
|
+
};
|
|
315
|
+
const preferredHigh = highAt(preferredBoundary, openingLength), preferredLists = listsAt(preferredBoundary);
|
|
316
|
+
const fixedRowsCost = preferredHigh.cost + lineCost(preferredLists.records);
|
|
317
|
+
const baseCharacters = frameLength(fixedRowsCost, preferredHigh.rows.length + preferredLists.records.length);
|
|
318
|
+
const base = price(baseCharacters, preferredBoundary);
|
|
319
|
+
const target = carriedTarget(model.contextWindow, reserveTokens, base, before);
|
|
320
|
+
// The old tail rule supplies an ordering boundary only. Its recent
|
|
321
|
+
// suffix follows the openings, and is charged to the same total grant.
|
|
322
|
+
const boundaries = [floorBoundary];
|
|
323
|
+
for (let at = floorBoundary - 1; at >= preferredBoundary; at--) {
|
|
324
|
+
const entry = entries[at]!;
|
|
325
|
+
if ((entry.type === "message" && entry.message.role !== "toolResult") || entry.type === "custom_message" || entry.type === "branch_summary") boundaries.push(at);
|
|
326
|
+
}
|
|
327
|
+
boundaries.reverse();
|
|
328
|
+
const tailFor = (length: number) => {
|
|
329
|
+
// Longest suffix first: the first fit is the preferred rank. Most
|
|
330
|
+
// cuts keep that entire suffix, so no other boundaries need pricing.
|
|
331
|
+
for (const boundary of boundaries) {
|
|
332
|
+
const high = highAt(boundary, length);
|
|
333
|
+
if (price(high.length, boundary) <= target) return { boundary, high };
|
|
334
|
+
}
|
|
335
|
+
return undefined;
|
|
336
|
+
};
|
|
337
|
+
let selected = tailFor(minimumOpeningLength);
|
|
338
|
+
if (!selected) return { unavailable: "pi-tuck's mandatory context cannot fit or reduce the request budget",
|
|
339
|
+
capacityRejected: price(calibrationHigh.length, floorBoundary) + reserveTokens >= model.contextWindow,
|
|
340
|
+
floorMessages: floorProjection };
|
|
341
|
+
let low = minimumOpeningLength, high = openingLength;
|
|
342
|
+
while (low < high) {
|
|
343
|
+
const middle = Math.ceil((low + high) / 2);
|
|
344
|
+
if (tailFor(middle)) low = middle; else high = middle - 1;
|
|
345
|
+
}
|
|
346
|
+
selected = tailFor(low)!;
|
|
347
|
+
let { boundary, high: protectedView } = selected;
|
|
348
|
+
const sourceRows = rows.filter(row => row.position < boundary);
|
|
349
|
+
const lists = listsAt(boundary);
|
|
350
|
+
let ledger: TranscriptRecord[] = lists.records;
|
|
351
|
+
// List caps are existing ranking policy; only the carried target is a
|
|
352
|
+
// spending limit. Candidate list sizes have fixed, cached line prices.
|
|
353
|
+
if (price(frameLength(protectedView.cost + lineCost(ledger), protectedView.rows.length + ledger.length), boundary) > target) {
|
|
354
|
+
ledger = [];
|
|
355
|
+
for (let keep = 0; keep <= Math.min(Math.max(MAX_LEDGER_LINES, MAX_ACTION_LINES), Math.max(lists.files.length, lists.actions.length)); keep++) {
|
|
356
|
+
const candidate = [...actionRecords(lists.actions, keep), ...ledgerRecords(lists.files, keep)];
|
|
357
|
+
if (price(frameLength(protectedView.cost + lineCost(candidate), protectedView.rows.length + candidate.length), boundary) <= target) ledger = candidate;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
const mandatory = new Set([...protectedView.whole].filter(index => {
|
|
361
|
+
const row = users.find(row => row.index === index);
|
|
362
|
+
return row?.message.role === "user" && isTextOnly(row.message.content);
|
|
363
|
+
}));
|
|
364
|
+
const fixed = [...protectedView.kept, ...ledger], fixedCost = lineCost(fixed);
|
|
365
|
+
const maxSummaryCharacters = Math.floor((target - offset - nativeAt(boundary)) * 4) - wrapperCharacters;
|
|
366
|
+
const maxBriefChars = Math.max(0, maxSummaryCharacters - fixedCost);
|
|
367
|
+
const compileInput = { messages: sourceRows.map(row => row.message), sourceIndices: sourceRows.map(row => row.index),
|
|
368
|
+
sourceEntryIds: sourceRows.map(row => row.id), sourceTimes: sourceRows.map(row => resolveSourceTime(byId.get(row.id)!)),
|
|
369
|
+
ranking: { maxBriefChars, mandatorySourceIndices: mandatory } };
|
|
370
|
+
const imageLabels = new Map<number | undefined, Set<string>>();
|
|
371
|
+
for (const row of sourceRows) if (row.message.role === "user" && Array.isArray(row.message.content))
|
|
372
|
+
imageLabels.set(row.index, new Set(row.message.content.flatMap(part => part.type === "image" ? [`[image: ${part.mimeType}]`] : [])));
|
|
373
|
+
const compiledBoundary = boundary;
|
|
374
|
+
const assemble = (compiled: string, at = compiledBoundary) => {
|
|
375
|
+
const split = compiled ? splitSummary(compiled) : undefined;
|
|
376
|
+
if (compiled && !split) throw new Error("pi-tuck returned an invalid summary");
|
|
377
|
+
const inputRecords = split ? parseRecords(split.body, split.version) : protectedView.omissions;
|
|
378
|
+
const expanded = new Set(protectedView.kept.filter(record => record.kind === "excerpt" && inputRecords.some(row =>
|
|
379
|
+
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));
|
|
380
|
+
const anchored = new Set(protectedView.kept.map(record => record.sourceIndex));
|
|
381
|
+
const moved = new Set(rows.filter(row => row.position >= at && row.position < compiledBoundary).map(row => row.index));
|
|
382
|
+
const records = [...inputRecords.filter(record => {
|
|
383
|
+
if (record.sourceIndex !== undefined && moved.has(record.sourceIndex)) return false;
|
|
384
|
+
if (record.kind === "excerpt" && record.role === "user" && imageLabels.get(record.sourceIndex)?.has(record.text)) return true;
|
|
385
|
+
return record.role !== "user" || record.sourceIndex === undefined || !anchored.has(record.sourceIndex) || expanded.has(record.sourceIndex);
|
|
386
|
+
}), ...protectedView.kept.filter(record => !expanded.has(record.sourceIndex) && !moved.has(record.sourceIndex))];
|
|
387
|
+
// Mixed user turns keep their text opening, while their image
|
|
388
|
+
// metadata remains in the ordinary lower-ranked dialogue lane.
|
|
389
|
+
const wholeUser = new Set(records.filter(record => record.kind === "excerpt" && record.role === "user" && record.text === userTexts.get(record.sourceIndex)).map(record => record.sourceIndex));
|
|
390
|
+
const wholeAssistant = new Map<number, number>();
|
|
391
|
+
for (const record of records) if (record.kind === "excerpt" && record.role === "assistant" && !record.span && record.sourceIndex !== undefined)
|
|
392
|
+
wholeAssistant.set(record.sourceIndex, (wholeAssistant.get(record.sourceIndex) ?? 0) + 1);
|
|
393
|
+
for (let pos = records.length - 1; pos >= 0; pos--) {
|
|
394
|
+
const record = records[pos]!;
|
|
395
|
+
if (record.kind !== "omission" || !record.sourceRange) continue;
|
|
396
|
+
if (record.role === "user") {
|
|
397
|
+
const missing = units.user.filter(unit => unit.position < at && unit.index >= record.sourceRange!.start && unit.index <= record.sourceRange!.end && !wholeUser.has(unit.index));
|
|
398
|
+
if (!missing.length) records.splice(pos, 1);
|
|
399
|
+
else records[pos] = omissionRecord("user", missing.length, [missing[0]!.index, missing.at(-1)!.index], false);
|
|
400
|
+
} else if (at !== compiledBoundary) {
|
|
401
|
+
const role = record.role;
|
|
402
|
+
if (role === "assistant" || role === "toolResult") {
|
|
403
|
+
const missing = units[role].filter(unit => unit.position < at && unit.index >= record.sourceRange!.start && unit.index <= record.sourceRange!.end &&
|
|
404
|
+
(role === "toolResult" || unit.count > (wholeAssistant.get(unit.index) ?? 0)));
|
|
405
|
+
const count = missing.reduce((n, unit) => n + unit.count - (role === "assistant" ? wholeAssistant.get(unit.index) ?? 0 : 0), 0);
|
|
406
|
+
if (!count) records.splice(pos, 1);
|
|
407
|
+
else records[pos] = omissionRecord(role, count, [missing[0]!.index, missing.at(-1)!.index], false);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
orderByOriginalSource(records, sourceIndices, originals.selectedIds);
|
|
412
|
+
const summary = render([...records, ...ledger]);
|
|
413
|
+
return { summary, after: price(summary.length, at), records };
|
|
414
|
+
};
|
|
415
|
+
const compiled = tuck.compileDialogue(compileInput);
|
|
416
|
+
let offer = assemble(compiled.text);
|
|
417
|
+
// Only capacity left after all higher ranks can extend the native tail
|
|
418
|
+
// beyond its preferred rank boundary. No old allocator is consulted.
|
|
419
|
+
const liveStartId = calibration.ok ? calibration.selectedIds[0] : undefined;
|
|
420
|
+
const liveStart = liveStartId === undefined ? preferredBoundary : positions.get(liveStartId)!;
|
|
421
|
+
if (boundary > liveStart) {
|
|
422
|
+
const recordCosts = new Float64Array(entries.length + 1), recordCounts = new Int32Array(entries.length + 1);
|
|
423
|
+
const sourcePositions = new Map(rows.map(row => [row.index, row.position]));
|
|
424
|
+
const wholeUsers = new Set(offer.records.filter(record => record.kind === "excerpt" && record.role === "user" &&
|
|
425
|
+
record.text === userTexts.get(record.sourceIndex)).map(record => record.sourceIndex));
|
|
426
|
+
const wholeAssistants = new Map<number, number>();
|
|
427
|
+
for (const record of offer.records) {
|
|
428
|
+
const at = record.sourceIndex === undefined ? undefined : sourcePositions.get(record.sourceIndex);
|
|
429
|
+
if (at !== undefined) { recordCosts[at + 1] += lineCost([record]); recordCounts[at + 1]++; }
|
|
430
|
+
if (record.kind === "excerpt" && record.role === "assistant" && !record.span && record.sourceIndex !== undefined)
|
|
431
|
+
wholeAssistants.set(record.sourceIndex, (wholeAssistants.get(record.sourceIndex) ?? 0) + 1);
|
|
432
|
+
}
|
|
433
|
+
for (let at = 1; at <= entries.length; at++) {
|
|
434
|
+
recordCosts[at] += recordCosts[at - 1]!; recordCounts[at] += recordCounts[at - 1]!;
|
|
435
|
+
}
|
|
436
|
+
// A tail extension removes source records and shortens at most three
|
|
437
|
+
// counted ranges. Price those changes from prefix sums; render only the
|
|
438
|
+
// winning suffix, rather than parsing/serializing the dialogue per step.
|
|
439
|
+
const ranges = offer.records.flatMap(record => {
|
|
440
|
+
if (record.kind !== "omission" || !record.sourceRange || !["user", "assistant", "toolResult"].includes(record.role ?? "")) return [];
|
|
441
|
+
const role = record.role as keyof typeof units;
|
|
442
|
+
const missing = units[role].flatMap(unit => {
|
|
443
|
+
const count = role === "user" ? Number(!wholeUsers.has(unit.index)) : unit.count - (role === "assistant" ? wholeAssistants.get(unit.index) ?? 0 : 0);
|
|
444
|
+
return count && unit.position < compiledBoundary && unit.index >= record.sourceRange!.start && unit.index <= record.sourceRange!.end ? [{ ...unit, count }] : [];
|
|
445
|
+
});
|
|
446
|
+
const ends = new Int32Array(entries.length + 1), counts = new Int32Array(missing.length + 1);
|
|
447
|
+
let seen = 0;
|
|
448
|
+
for (let at = 0; at <= entries.length; at++) {
|
|
449
|
+
while (seen < missing.length && missing[seen]!.position < at) seen++;
|
|
450
|
+
ends[at] = seen;
|
|
451
|
+
}
|
|
452
|
+
missing.forEach((unit, at) => counts[at + 1] = counts[at]! + unit.count);
|
|
453
|
+
return [{ role, missing, ends, counts, cost: lineCost([record]) }];
|
|
454
|
+
});
|
|
455
|
+
const baseCost = lineCost(offer.records) + lineCost(ledger), baseCount = offer.records.length + ledger.length;
|
|
456
|
+
const extensionPrice = (at: number) => {
|
|
457
|
+
let cost = baseCost - (recordCosts[compiledBoundary]! - recordCosts[at]!);
|
|
458
|
+
let count = baseCount - (recordCounts[compiledBoundary]! - recordCounts[at]!);
|
|
459
|
+
for (const range of ranges) {
|
|
460
|
+
const end = range.ends[at]!;
|
|
461
|
+
cost -= range.cost;
|
|
462
|
+
if (end) cost += lineCost([omissionRecord(range.role, range.counts[end]!, [range.missing[0]!.index, range.missing[end - 1]!.index], false)]);
|
|
463
|
+
else count--;
|
|
464
|
+
}
|
|
465
|
+
return price(frameLength(cost, count), at);
|
|
466
|
+
};
|
|
467
|
+
for (let at = boundary - 1; at >= liveStart; at--) {
|
|
468
|
+
const entry = entries[at]!;
|
|
469
|
+
if (entry.type !== "message" && entry.type !== "custom_message" && entry.type !== "branch_summary") continue;
|
|
470
|
+
if (entry.type === "message" && entry.message.role === "toolResult") continue;
|
|
471
|
+
if (extensionPrice(at) <= target) boundary = at;
|
|
472
|
+
else break;
|
|
473
|
+
}
|
|
474
|
+
if (boundary !== compiledBoundary) offer = assemble(compiled.text, boundary);
|
|
475
|
+
}
|
|
476
|
+
const projection = preview(offer.summary, boundary);
|
|
300
477
|
assertNativeToolRounds(projection);
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
const dialogueFits = dialogue.length <= maxBriefChars;
|
|
309
|
-
const candidate = dialogue && (dialogueFits || input.operatorManual) ? assemble(dialogue) : undefined;
|
|
310
|
-
const offer = candidate && dialogueFits && candidate.after < before && candidate.after + reserveTokens + headroom < model.contextWindow ? candidate : undefined;
|
|
311
|
-
// Manual no-work preserves native originals; final request admission still owns full overhead.
|
|
312
|
-
if (!offer && input.operatorManual && candidate && candidate.after >= before &&
|
|
313
|
-
before + reserveTokens < model.contextWindow)
|
|
314
|
-
return { unavailable: "Manual compaction preserved original dialogue: the complete-dialogue view does not reduce context" };
|
|
315
|
-
const { summary, after } = offer ?? assemble(tuck.compileShrunkDialogue(compileInput));
|
|
316
|
-
// Draft reduction concerns conversation. Final request admission owns the effective system/tools.
|
|
317
|
-
if (after + reserveTokens >= model.contextWindow || after >= before)
|
|
318
|
-
return { unavailable: "pi-tuck's draft does not reduce context within the request budget", capacityRejected: after + reserveTokens >= model.contextWindow, floorMessages: floor.messages };
|
|
319
|
-
const mode: DraftMode = offer ? "dialogue" : "shrunk";
|
|
320
|
-
return { compaction: { summary, firstKeptEntryId: cut.firstKeptEntryId, tokensBefore: input.tokensBefore, details: { ...details, tokensAfter: after, view: offer ? "complete-dialogue" : "shrunk-dialogue", mode } }, after, mode };
|
|
321
|
-
};
|
|
322
|
-
const preferred = compileAtCut(cut);
|
|
323
|
-
if (preferred.mode === "dialogue") return preferred;
|
|
324
|
-
if (!preferred.compaction && !preferred.capacityRejected) return preferred;
|
|
325
|
-
const preferredBoundary = cut.compactAll ? entries.length : entries.findIndex(entry => entry.id === cut.firstKeptEntryId);
|
|
326
|
-
let floorBoundary = freshRound ? lastResponse : entries.length;
|
|
327
|
-
if (freshBash >= 0) floorBoundary = Math.min(floorBoundary, freshBash);
|
|
328
|
-
if (userPosition >= 0 && (!textOnly || userPosition > lastResponse)) floorBoundary = Math.min(floorBoundary, userPosition);
|
|
329
|
-
if (floorBoundary <= preferredBoundary) return preferred;
|
|
330
|
-
const prefix = tuck.buildOwnCut(entries.slice(0, floorBoundary), 0);
|
|
331
|
-
if (!prefix.ok) return preferred;
|
|
332
|
-
const retried = compileAtCut({ ...cutBefore(cut, floorBoundary, prefix), budgetCut: undefined });
|
|
333
|
-
return preferred.compaction && retried.mode !== "dialogue" ? preferred : retried;
|
|
478
|
+
const after = measure(projection);
|
|
479
|
+
if (after + reserveTokens >= model.contextWindow || after >= before)
|
|
480
|
+
return { unavailable: "pi-tuck's draft does not reduce context within the request budget", capacityRejected: after + reserveTokens >= model.contextWindow, floorMessages: floorProjection };
|
|
481
|
+
const mode: DraftMode = compiled.step === "whole" ? "dialogue" : "shrunk";
|
|
482
|
+
return { compaction: { summary: offer.summary, firstKeptEntryId: entries[boundary]?.id ?? "", tokensBefore: input.tokensBefore,
|
|
483
|
+
details: { ...detailsAt(boundary), tokensAfter: after, carriedTarget: target, view: mode === "dialogue" ? "complete-dialogue" : "shrunk-dialogue", mode } }, after, mode };
|
|
484
|
+
}
|
|
334
485
|
|
|
335
486
|
}
|
|
336
487
|
|