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.
Files changed (54) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +2 -2
  3. package/dist/src/budget.d.ts +0 -2
  4. package/dist/src/budget.js +1 -3
  5. package/dist/src/build-id.d.ts +1 -1
  6. package/dist/src/build-id.js +1 -1
  7. package/dist/src/compaction.d.ts +1 -4
  8. package/dist/src/compaction.js +46 -18
  9. package/dist/src/content-observations.d.ts +0 -6
  10. package/dist/src/content-observations.js +5 -5
  11. package/dist/src/diagnostic-replay.js +6 -6
  12. package/dist/src/diagnostics.d.ts +0 -1
  13. package/dist/src/diagnostics.js +2 -3
  14. package/dist/src/history-image.d.ts +0 -2
  15. package/dist/src/history-image.js +1 -1
  16. package/dist/src/kept-reads.d.ts +3 -0
  17. package/dist/src/kept-reads.js +51 -0
  18. package/dist/src/omissions.d.ts +3 -6
  19. package/dist/src/omissions.js +3 -10
  20. package/dist/src/recall.js +2 -2
  21. package/dist/src/thrash.d.ts +0 -6
  22. package/dist/src/thrash.js +1 -1
  23. package/dist/src/tuck/core/format.d.ts +2 -0
  24. package/dist/src/tuck/core/format.js +3 -0
  25. package/dist/src/tuck/core/token-estimate.d.ts +2 -8
  26. package/dist/src/tuck/core/token-estimate.js +3 -9
  27. package/dist/src/tuck/core/tool-args.d.ts +0 -1
  28. package/dist/src/tuck/core/tool-args.js +1 -1
  29. package/dist/src/tuck/core/transcript.d.ts +3 -2
  30. package/dist/src/tuck/cut.d.ts +0 -1
  31. package/dist/src/tuck/cut.js +0 -3
  32. package/dist/src/tuck.d.ts +2 -2
  33. package/dist/src/tuck.js +16 -13
  34. package/package.json +1 -1
  35. package/src/budget.ts +1 -4
  36. package/src/build-id.ts +1 -1
  37. package/src/compaction.ts +42 -17
  38. package/src/content-observations.ts +5 -5
  39. package/src/diagnostic-replay.ts +7 -6
  40. package/src/diagnostics.ts +2 -3
  41. package/src/history-image.ts +1 -1
  42. package/src/kept-reads.ts +40 -0
  43. package/src/omissions.ts +3 -10
  44. package/src/recall.ts +3 -3
  45. package/src/thrash.ts +1 -7
  46. package/src/tuck/core/format.ts +3 -0
  47. package/src/tuck/core/token-estimate.ts +4 -20
  48. package/src/tuck/core/tool-args.ts +1 -1
  49. package/src/tuck/core/transcript.ts +2 -2
  50. package/src/tuck/cut.ts +0 -4
  51. package/src/tuck.ts +16 -13
  52. package/dist/src/tuck/core/render-entries.d.ts +0 -6
  53. package/dist/src/tuck/core/render-entries.js +0 -1
  54. 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
- ): TokenEstimateCalibration => {
11
+ ): { charsPerToken: number } => {
22
12
  if (!sourceTokens || sourceTokens <= 0 || sourceChars <= 0) {
23
- return { mode: "heuristic", charsPerToken: DEFAULT_CHARS_PER_TOKEN };
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 { mode: "heuristic", charsPerToken: DEFAULT_CHARS_PER_TOKEN };
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 = (
@@ -1,4 +1,4 @@
1
- export const PATH_KEYS = ["path", "file_path", "filePath", "file"] as const;
1
+ const PATH_KEYS = ["path", "file_path", "filePath", "file"] as const;
2
2
 
3
3
  export const extractPath = (args: Record<string, unknown>): string | null => {
4
4
  for (const key of PATH_KEYS) {
@@ -15,8 +15,8 @@ export const smallOriginalArguments = (value: unknown, maxCharacters = MAX_PRODU
15
15
  };
16
16
 
17
17
  interface RecordFields {
18
- sourceTime?: import("./source-time").SourceTime;
19
- sourceTimes?: ({ sourceIndex: number } & import("./source-time").SourceTime)[];
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
- /** `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
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, keptUserTurns: 0, keepFallbackToCompactAll: false };
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
- assertNativeToolRounds(floorProjection);
318
- const after = measure(floorProjection);
319
- return { compaction: { summary: floorSummary, firstKeptEntryId: entries[floorBoundary]?.id ?? "", tokensBefore: input.tokensBefore,
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
- // Reads and searches share the lead rank, newest result first, and one
541
- // allowance. Keep L2's independent four-item caps and whole-item skips.
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,6 +0,0 @@
1
- export interface RenderedEntry {
2
- index: number;
3
- role: string;
4
- summary: string;
5
- files?: string[];
6
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,6 +0,0 @@
1
- export interface RenderedEntry {
2
- index: number;
3
- role: string;
4
- summary: string;
5
- files?: string[];
6
- }