pi-tuck 1.3.4 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +58 -125
  3. package/dist/index.d.ts +1 -4
  4. package/dist/index.js +49 -10
  5. package/dist/src/budget.d.ts +6 -15
  6. package/dist/src/budget.js +6 -11
  7. package/dist/src/build-id.d.ts +1 -1
  8. package/dist/src/build-id.js +1 -1
  9. package/dist/src/compaction.d.ts +12 -20
  10. package/dist/src/compaction.js +220 -202
  11. package/dist/src/content-observations.d.ts +24 -0
  12. package/dist/src/content-observations.js +122 -0
  13. package/dist/src/diagnostics.d.ts +1 -3
  14. package/dist/src/ledger.d.ts +6 -3
  15. package/dist/src/ledger.js +14 -7
  16. package/dist/src/llama-continuation.d.ts +8 -0
  17. package/dist/src/llama-continuation.js +179 -0
  18. package/dist/src/recall.js +8 -25
  19. package/dist/src/request-capacity.d.ts +11 -26
  20. package/dist/src/request-capacity.js +20 -57
  21. package/dist/src/run.d.ts +16 -7
  22. package/dist/src/run.js +53 -25
  23. package/dist/src/status.d.ts +2 -0
  24. package/dist/src/status.js +6 -5
  25. package/dist/src/tuck/core/format.js +1 -1
  26. package/dist/src/tuck/core/readable-content.d.ts +14 -0
  27. package/dist/src/tuck/core/readable-content.js +43 -0
  28. package/dist/src/tuck.d.ts +2 -1
  29. package/dist/src/tuck.js +92 -30
  30. package/index.ts +42 -10
  31. package/package.json +1 -1
  32. package/src/budget.ts +7 -12
  33. package/src/build-id.ts +1 -1
  34. package/src/compaction.ts +220 -193
  35. package/src/content-observations.ts +115 -0
  36. package/src/diagnostics.ts +1 -1
  37. package/src/ledger.ts +17 -10
  38. package/src/llama-continuation.ts +176 -0
  39. package/src/recall.ts +8 -20
  40. package/src/request-capacity.ts +18 -57
  41. package/src/run.ts +48 -22
  42. package/src/status.ts +8 -5
  43. package/src/tuck/core/format.ts +1 -1
  44. package/src/tuck/core/readable-content.ts +44 -0
  45. package/src/tuck.ts +79 -28
package/index.ts CHANGED
@@ -4,16 +4,14 @@ import { join } from "node:path";
4
4
  import { installCompaction } from "./src/compaction";
5
5
  import { validCutAt } from "./src/budget";
6
6
  import { notify } from "./src/notify";
7
+ import type { LlamaContinuation } from "./src/llama-continuation";
7
8
  import { cleanupLegacyFiles } from "./src/legacy-cleanup";
8
9
 
9
10
  export type { CompactionOptions } from "./src/compaction";
10
11
  export { installCompaction } from "./src/compaction";
11
12
  export { inspectIncident, readableIncident } from "./src/diagnostic-replay.js";
12
13
 
13
- /** pi-tuck as a Pi extension: compaction and history recall. `"cutAt"` in `pi-tuck.json` in Pi's agent folder (a number above 0 and at most
14
- * 1) fixes the cut point as a fraction of the input room; without it, the cut point adapts. */
15
- /** The oldest Pi with every hook and API pi-tuck uses, and the oldest its tests pass on (Pi 1.2 added the custom message that starts a
16
- * run to before_agent_start, which the prompt's cut counts). */
14
+ /** Pi 1.2 added the run's custom message to before_agent_start so preflight can count it. */
17
15
  export const PI_FLOOR = "1.2.0";
18
16
  /** Whether a Pi version is at or above the floor. A version that does not parse is taken as supported. */
19
17
  export function piSupported(version: string): boolean {
@@ -35,24 +33,58 @@ export default async function (pi: ExtensionAPI) {
35
33
  return;
36
34
  }
37
35
  const settings = readSettings(join(getAgentDir(), "pi-tuck.json"));
38
- const handle = await installCompaction(pi, { cutAt: settings.cutAt });
36
+ const handle = await installCompaction(pi, { cutAt: settings.cutAt, cacheSensitiveModels: settings.cacheSensitiveModels, continuationModel: settings.llamaContinuation?.model });
37
+ const continuation = settings.llamaContinuation;
38
+ let continuationError: string | undefined;
39
+ if (continuation) {
40
+ try {
41
+ const [{ getApiProvider }, { llamaContinuationStream }] = await Promise.all([
42
+ import("@earendil-works/pi-ai/compat"), import("./src/llama-continuation"),
43
+ ]);
44
+ const native = getApiProvider("openai-completions")?.streamSimple;
45
+ if (!native) throw new Error("Pi's Chat Completions provider is unavailable");
46
+ pi.registerProvider(continuation.model.split("/")[0]!, {
47
+ api: "openai-completions",
48
+ streamSimple: llamaContinuationStream(continuation, native),
49
+ });
50
+ } catch (error) {
51
+ continuationError = `pi-tuck: native continuation unavailable: ${String(error)}`;
52
+ }
53
+ }
39
54
  let said = false;
40
55
  pi.on("session_start", async (_event, ctx) => {
41
56
  await cleanupLegacyFiles(ctx);
42
57
  if (handle.state?.(ctx).kind === "standingDown") return;
58
+ if (continuationError) notify(ctx, continuationError, "warning");
43
59
  if (settings.error && !said) {
44
- said = notify(ctx, `pi-tuck: ${settings.error}; using the defaults.`, "warning");
60
+ said = notify(ctx, `pi-tuck: ${settings.error}`, "warning");
45
61
  }
46
62
  });
47
63
  }
48
64
 
49
- function readSettings(path: string): { cutAt?: number; error?: string } {
65
+ type Settings = { cutAt?: number; cacheSensitiveModels?: string[]; llamaContinuation?: LlamaContinuation; error?: string };
66
+ const modelRef = (value: unknown) => typeof value === "string" && /^[^/]+\/.+/.test(value);
67
+ function readSettings(path: string): Settings {
50
68
  let text: string;
51
69
  try { text = readFileSync(path, "utf8"); } catch { return {}; }
70
+ const cutAtHint = `${path} should be a JSON object like {"cutAt": 0.8}, with cutAt a number above 0 and at most 1`;
52
71
  try {
53
72
  const value = JSON.parse(text);
54
- if (value && typeof value === "object" &&
55
- (value.cutAt === undefined || validCutAt(value.cutAt))) return { cutAt: value.cutAt };
73
+ if (value && typeof value === "object" && !Array.isArray(value)) {
74
+ const settings: Settings = {};
75
+ const warnings: string[] = [];
76
+ if (value.cutAt === undefined || validCutAt(value.cutAt)) settings.cutAt = value.cutAt;
77
+ else warnings.push(`${cutAtHint}; using the default cut point.`);
78
+ const models = value.cacheSensitiveModels;
79
+ if (Array.isArray(models) && models.every(modelRef)) settings.cacheSensitiveModels = models;
80
+ else if (models !== undefined) warnings.push(`${path} cacheSensitiveModels must be an array of provider/model identifiers; ignoring that setting.`);
81
+ const c = value.llamaContinuation;
82
+ if (c && modelRef(c.model) && Number.isSafeInteger(c.extraTokens) && c.extraTokens > 0 &&
83
+ Number.isSafeInteger(c.deadlineMs) && c.deadlineMs > 0 && c.deadlineMs <= 2_147_483_647) settings.llamaContinuation = c;
84
+ else if (c !== undefined) warnings.push(`${path} llamaContinuation requires a provider/model identifier in model, a positive safe integer extraTokens, and an integer deadlineMs from 1 to 2147483647; native continuation is off.`);
85
+ if (warnings.length) settings.error = warnings.join(" ");
86
+ return settings;
87
+ }
56
88
  } catch { /* reported below */ }
57
- return { error: `${path} should be a JSON object like {"cutAt": 0.8}, with cutAt a number above 0 and at most 1` };
89
+ return { error: `${cutAtHint}; using the defaults.` };
58
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-tuck",
3
- "version": "1.3.4",
3
+ "version": "1.4.1",
4
4
  "description": "Deterministic compaction for Pi with no model call, plus exact recall of earlier history.",
5
5
  "keywords": [
6
6
  "pi-package",
package/src/budget.ts CHANGED
@@ -19,20 +19,15 @@ export interface ToolShape { name: string; description: string; parameters: unkn
19
19
  export const TRIGGER_MARGIN_FRACTION = 0.02;
20
20
  /** Whether `value` is a cut point: a fraction of the room above 0 and at most 1. */
21
21
  export const validCutAt = (value: unknown): value is number => typeof value === "number" && value > 0 && value <= 1;
22
- /**
23
- * The one owner of the band: the limit a cut is triggered at and preferably planned to. An adaptive default may raise the cut point above
24
- * 60% using the last cut's conversation size and current system size, both estimates, so they are converted to the room's units by the factor. Explicit cut points use the fraction alone. The margin caps both.
25
- */
26
- /** The initial default cut point: 60% of the room before a usable previous cut. Measured against 1 with paired replays (no answer loss) and billed
27
- * input under automatic and explicit prompt caching (about 17% less at 128k, 10-14% at 32k). */
28
- export const DEFAULT_CUT_AT = 0.6;
29
- /** Preferred native carry, in the planner's estimate: never more than the
30
- * keep-recent cut offers or the tail amount the adaptive band assumes. */
22
+ /** Trigger at the room for native parity of post-cut headroom. */
23
+ export const DEFAULT_CUT_AT = 1;
24
+ /** Preferred native carry, in the planner's estimate, bounded by the keep-recent offer and the existing tail limit. */
31
25
  export const carriedTailTokens = (offeredTokens: number): number => Math.min(offeredTokens, MAX_TAIL_TOKENS / 2);
32
- export const bandOf = (room: number, cutAt = 1, adaptive?: { system: number; tokensAfter: number; factor: number }): number => {
26
+ /** The one owner of the band: the trigger and preferred planning limit, capped by the margin. */
27
+ export const bandOf = (room: number, cutAt = DEFAULT_CUT_AT): number => {
33
28
  const positive = Math.max(0, room);
34
29
  const base = Math.floor(positive * cutAt);
35
- return Math.min(room - Math.ceil(positive * TRIGGER_MARGIN_FRACTION), adaptive ? Math.max(base, Math.ceil((adaptive.system + adaptive.tokensAfter + carriedTailTokens(MAX_TAIL_TOKENS)) * adaptive.factor)) : base);
30
+ return Math.min(room - Math.ceil(positive * TRIGGER_MARGIN_FRACTION), base);
36
31
  };
37
32
 
38
33
  /** The cut's one carried-context grant, in the planner's estimate. Combines
@@ -115,7 +110,7 @@ export type Decision<D extends object = {}> =
115
110
  | { kind: "cut"; target: Target; plan: Extract<Plan<D>, { draft: true }> }
116
111
  | { kind: "fault"; capacity: boolean; message: string };
117
112
  export interface CutInput<D extends object = {}> {
118
- /** The band adapts by itself (no explicit cut point), so a cut that isn't due must also free a tenth of the room. */
113
+ /** With no explicit cut point, a cut that isn't due must also free a tenth of the room. */
119
114
  usefulOnly?: boolean;
120
115
  /** Saved conversation, measured by the planner's own function. */
121
116
  before: number;
package/src/build-id.ts CHANGED
@@ -1 +1 @@
1
- export const BUILD_ID = "6a84733ab75ecf863fe77c6dd6d0a91602a8a0d353a785c3fdb34585f42637a2";
1
+ export const BUILD_ID = "8493bab7ad20ff2063558de74a92bcfeeb11f48536bb513d9dd442b03fdaf3ad";