pi-context-kit 0.2.0 → 0.3.0
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/README.md +510 -68
- package/extensions/index.ts +234 -155
- package/package.json +21 -8
- package/src/config.ts +0 -9
- package/src/context.ts +0 -15
- package/src/maintain.ts +10 -13
- package/src/prune.ts +24 -26
- package/src/shake.ts +20 -11
- package/src/stale.ts +39 -44
- package/src/tool-calls.ts +22 -1
- package/CHANGELOG.md +0 -28
package/extensions/index.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
type Message,
|
|
4
|
-
type Model,
|
|
5
|
-
Type,
|
|
6
|
-
} from "@earendil-works/pi-ai";
|
|
1
|
+
import { type Api, type Message, type Model } from "@earendil-works/pi-ai";
|
|
2
|
+
import { Type } from "typebox";
|
|
7
3
|
import {
|
|
8
4
|
convertToLlm,
|
|
5
|
+
type CompactionEntryDraft,
|
|
9
6
|
type ExtensionAPI,
|
|
10
7
|
type ExtensionContext,
|
|
11
8
|
findCutPoint,
|
|
12
9
|
generateSummaryWithUsage,
|
|
13
10
|
getLatestCompactionEntry,
|
|
11
|
+
type ProjectedSessionEntry,
|
|
12
|
+
type SessionBeforeCompactEvent,
|
|
14
13
|
type SessionEntry,
|
|
15
14
|
SettingsManager,
|
|
16
15
|
} from "@earendil-works/pi-coding-agent";
|
|
@@ -35,7 +34,7 @@ import {
|
|
|
35
34
|
toDrafts,
|
|
36
35
|
} from "../src/context.ts";
|
|
37
36
|
import { handoffSummary } from "../src/handoff.ts";
|
|
38
|
-
import { planMaintenance } from "../src/maintain.ts";
|
|
37
|
+
import { type MaintenancePlan, planMaintenance } from "../src/maintain.ts";
|
|
39
38
|
import { requestNote } from "../src/note.ts";
|
|
40
39
|
import { readEntry, searchEntries } from "../src/recall.ts";
|
|
41
40
|
|
|
@@ -220,18 +219,128 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
220
219
|
: undefined;
|
|
221
220
|
};
|
|
222
221
|
|
|
222
|
+
/** Starts a background note shortly before the threshold, so compaction need not wait for one. */
|
|
223
|
+
const speculate = (
|
|
224
|
+
turn: Turn,
|
|
225
|
+
leafId: string,
|
|
226
|
+
keptEntryId: string | undefined,
|
|
227
|
+
messages: () => Message[],
|
|
228
|
+
) => {
|
|
229
|
+
const { ctx, config, model, limit, result } = turn;
|
|
230
|
+
if (!config.asyncEnabled || !keptEntryId) return;
|
|
231
|
+
if (result.tokens <= limit - speculationLead(limit)) return;
|
|
232
|
+
if (result.tokens > limit) return;
|
|
233
|
+
const budget = Math.floor(
|
|
234
|
+
limit * config.progressRatio - keptTokens(ctx, keptEntryId),
|
|
235
|
+
);
|
|
236
|
+
if (budget <= 0) return;
|
|
237
|
+
const context = messages();
|
|
238
|
+
const maxTokens = Math.min(model.maxTokens, budget);
|
|
239
|
+
background.start(
|
|
240
|
+
turn.current,
|
|
241
|
+
leafId,
|
|
242
|
+
(signal) =>
|
|
243
|
+
requestNote({
|
|
244
|
+
registry: ctx.modelRegistry,
|
|
245
|
+
model,
|
|
246
|
+
sessionId: ctx.sessionManager.getSessionId(),
|
|
247
|
+
messages: context,
|
|
248
|
+
reasoning: reasoning(),
|
|
249
|
+
signal,
|
|
250
|
+
maxTokens,
|
|
251
|
+
}),
|
|
252
|
+
(error) => warn(ctx, error, "A new note is written at compaction."),
|
|
253
|
+
);
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/** `"waiting"` means a background note is still being written for this threshold. */
|
|
257
|
+
const handoffAtTurn = async (turn: Turn) => {
|
|
258
|
+
const { ctx, model, limit, current, result } = turn;
|
|
259
|
+
const leafId = ctx.sessionManager.getLeafId();
|
|
260
|
+
if (!leafId) return undefined;
|
|
261
|
+
const messages = () =>
|
|
262
|
+
convertToLlm(projectedMessages(applyEdits(turn.entries, result.edits)));
|
|
263
|
+
const recent = recentStart(ctx, compactionSettings(model).keepRecentTokens);
|
|
264
|
+
speculate(turn, leafId, recent, messages);
|
|
265
|
+
if (result.tokens <= limit || !recent || failedEpoch === current)
|
|
266
|
+
return undefined;
|
|
267
|
+
const writing = background.writing(current);
|
|
268
|
+
if (writing && limit < piThreshold(model)) return "waiting" as const;
|
|
269
|
+
return tryHandoff(turn, recent, writing ? undefined : messages);
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const tryHandoff = async (
|
|
273
|
+
turn: Turn,
|
|
274
|
+
keptEntryId: string,
|
|
275
|
+
messages: (() => Message[]) | undefined,
|
|
276
|
+
) => {
|
|
277
|
+
const { ctx, current } = turn;
|
|
278
|
+
const signal = ctx.signal ?? new AbortController().signal;
|
|
279
|
+
try {
|
|
280
|
+
const compaction = await handoff({
|
|
281
|
+
ctx,
|
|
282
|
+
model: turn.model,
|
|
283
|
+
config: turn.config,
|
|
284
|
+
branch: turn.branch,
|
|
285
|
+
ready: await background.takeReady(current, turn.branch),
|
|
286
|
+
keptEntryId,
|
|
287
|
+
limit: turn.limit,
|
|
288
|
+
messages: messages?.(),
|
|
289
|
+
signal,
|
|
290
|
+
cancelBackground: () => background.cancel(),
|
|
291
|
+
});
|
|
292
|
+
if (compaction) {
|
|
293
|
+
background.cancel();
|
|
294
|
+
return compacted(turn.result, compaction);
|
|
295
|
+
}
|
|
296
|
+
} catch (error) {
|
|
297
|
+
if (!signal.aborted)
|
|
298
|
+
warn(ctx, error, "The remaining methods in methodOrder are tried.");
|
|
299
|
+
}
|
|
300
|
+
failedEpoch = current;
|
|
301
|
+
return undefined;
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
const softAtTurn = async (turn: Turn) => {
|
|
305
|
+
const { ctx, model, limit, current, result } = turn;
|
|
306
|
+
const due =
|
|
307
|
+
result.tokens > limit &&
|
|
308
|
+
limit < piThreshold(model) &&
|
|
309
|
+
turn.config.methodOrder.includes("soft") &&
|
|
310
|
+
softFailedEpoch !== current;
|
|
311
|
+
if (!due) return undefined;
|
|
312
|
+
try {
|
|
313
|
+
const compaction = await soft({
|
|
314
|
+
ctx,
|
|
315
|
+
model,
|
|
316
|
+
config: turn.config,
|
|
317
|
+
branch: turn.branch,
|
|
318
|
+
entries: applyEdits(turn.entries, result.edits),
|
|
319
|
+
limit,
|
|
320
|
+
});
|
|
321
|
+
if (compaction) return compacted(result, compaction);
|
|
322
|
+
} catch (error) {
|
|
323
|
+
if (!ctx.signal?.aborted)
|
|
324
|
+
warn(ctx, error, "Pi compacts at its own threshold.", "Pi's summary");
|
|
325
|
+
}
|
|
326
|
+
softFailedEpoch = current;
|
|
327
|
+
return undefined;
|
|
328
|
+
};
|
|
329
|
+
|
|
223
330
|
pi.on("turn_end", async (event, ctx) => {
|
|
224
331
|
const config = settings(ctx);
|
|
225
332
|
if (!config.enabled) return undefined;
|
|
333
|
+
const branch = ctx.sessionManager.getBranch();
|
|
334
|
+
const current = epoch(branch);
|
|
335
|
+
background.invalidate(current, branch);
|
|
226
336
|
const model = ctx.model;
|
|
337
|
+
// Without a model the threshold is infinite, so maintenance plans no edits.
|
|
338
|
+
if (!model) return undefined;
|
|
227
339
|
const limit = threshold(model, config);
|
|
228
340
|
const methods = config.methodOrder;
|
|
229
341
|
const entries = event.context.contextEntries;
|
|
230
|
-
const
|
|
342
|
+
const tokens = currentTokens(ctx, entries);
|
|
231
343
|
const edited = editedEntryIds(branch);
|
|
232
|
-
const tokens =
|
|
233
|
-
ctx.getContextUsage()?.tokens ??
|
|
234
|
-
contextTokens(projectedMessages(entries));
|
|
235
344
|
const plan = (shake: boolean) =>
|
|
236
345
|
planMaintenance({
|
|
237
346
|
entries,
|
|
@@ -241,161 +350,86 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
241
350
|
threshold: limit,
|
|
242
351
|
});
|
|
243
352
|
const shakeFirst = before(methods, "shake", "handoff");
|
|
244
|
-
|
|
245
|
-
|
|
353
|
+
const turn: Turn = {
|
|
354
|
+
ctx,
|
|
355
|
+
config,
|
|
356
|
+
model,
|
|
357
|
+
limit,
|
|
358
|
+
entries,
|
|
359
|
+
branch,
|
|
360
|
+
current,
|
|
361
|
+
result: plan(shakeFirst),
|
|
362
|
+
};
|
|
246
363
|
|
|
247
|
-
const leafId = ctx.sessionManager.getLeafId();
|
|
248
|
-
const current = epoch(branch);
|
|
249
|
-
background.invalidate(current, branch);
|
|
250
364
|
// Try ordered strategies at Pi's threshold before its native hook can compact.
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
if (
|
|
264
|
-
config.asyncEnabled &&
|
|
265
|
-
speculativeBudget > 0 &&
|
|
266
|
-
result.tokens > limit - speculationLead(limit) &&
|
|
267
|
-
result.tokens <= limit
|
|
268
|
-
) {
|
|
269
|
-
const context = messages();
|
|
270
|
-
const maxTokens = Math.min(model.maxTokens, speculativeBudget);
|
|
271
|
-
background.start(
|
|
272
|
-
current,
|
|
273
|
-
leafId,
|
|
274
|
-
(signal) =>
|
|
275
|
-
requestNote({
|
|
276
|
-
registry: ctx.modelRegistry,
|
|
277
|
-
model,
|
|
278
|
-
sessionId: ctx.sessionManager.getSessionId(),
|
|
279
|
-
messages: context,
|
|
280
|
-
reasoning: reasoning(),
|
|
281
|
-
signal,
|
|
282
|
-
maxTokens,
|
|
283
|
-
}),
|
|
284
|
-
(error) => warn(ctx, error, "A new note is written at compaction."),
|
|
285
|
-
);
|
|
286
|
-
}
|
|
287
|
-
if (own && keptEntryId && failedEpoch !== current) {
|
|
288
|
-
const writing = background.writing(current);
|
|
289
|
-
if (writing && limit < piThreshold(model)) return { entries: drafts() };
|
|
290
|
-
const signal = ctx.signal ?? new AbortController().signal;
|
|
291
|
-
try {
|
|
292
|
-
const compaction = await handoff({
|
|
293
|
-
ctx,
|
|
294
|
-
model,
|
|
295
|
-
config,
|
|
296
|
-
branch,
|
|
297
|
-
ready: await background.takeReady(current, branch),
|
|
298
|
-
keptEntryId,
|
|
299
|
-
limit,
|
|
300
|
-
messages: writing ? undefined : messages(),
|
|
301
|
-
signal,
|
|
302
|
-
cancelBackground: () => background.cancel(),
|
|
303
|
-
});
|
|
304
|
-
if (compaction) {
|
|
305
|
-
background.cancel();
|
|
306
|
-
return {
|
|
307
|
-
entries: [...drafts(), { type: "compaction", ...compaction }],
|
|
308
|
-
};
|
|
309
|
-
}
|
|
310
|
-
failedEpoch = current;
|
|
311
|
-
} catch (error) {
|
|
312
|
-
failedEpoch = current;
|
|
313
|
-
if (!signal.aborted)
|
|
314
|
-
warn(ctx, error, "The remaining methods in methodOrder are tried.");
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
const shakeAfterHandoff = !shakeFirst && before(methods, "shake", "soft");
|
|
319
|
-
if (tokens > limit && shakeAfterHandoff) result = plan(true);
|
|
320
|
-
const softDue =
|
|
321
|
-
model &&
|
|
322
|
-
result.tokens > limit &&
|
|
323
|
-
limit < piThreshold(model) &&
|
|
324
|
-
methods.includes("soft") &&
|
|
325
|
-
softFailedEpoch !== current;
|
|
326
|
-
if (softDue) {
|
|
327
|
-
try {
|
|
328
|
-
const compaction = await soft({
|
|
329
|
-
ctx,
|
|
330
|
-
model,
|
|
331
|
-
config,
|
|
332
|
-
branch,
|
|
333
|
-
entries: applyEdits(entries, result.edits),
|
|
334
|
-
limit,
|
|
335
|
-
});
|
|
336
|
-
if (compaction)
|
|
337
|
-
return {
|
|
338
|
-
entries: [...drafts(), { type: "compaction", ...compaction }],
|
|
339
|
-
};
|
|
340
|
-
} catch (error) {
|
|
341
|
-
if (!ctx.signal?.aborted)
|
|
342
|
-
warn(ctx, error, "Pi compacts at its own threshold.", "Pi's summary");
|
|
343
|
-
}
|
|
344
|
-
softFailedEpoch = current;
|
|
345
|
-
}
|
|
346
|
-
return result.edits.length > 0 ? { entries: drafts() } : undefined;
|
|
365
|
+
const handed = before(methods, "handoff", "soft")
|
|
366
|
+
? await handoffAtTurn(turn)
|
|
367
|
+
: undefined;
|
|
368
|
+
if (handed === "waiting") return persisted(turn.result, limit);
|
|
369
|
+
if (handed) return handed;
|
|
370
|
+
if (
|
|
371
|
+
turn.result.tokens > limit &&
|
|
372
|
+
!shakeFirst &&
|
|
373
|
+
before(methods, "shake", "soft")
|
|
374
|
+
)
|
|
375
|
+
turn.result = plan(true);
|
|
376
|
+
return (await softAtTurn(turn)) ?? persisted(turn.result, limit);
|
|
347
377
|
});
|
|
348
378
|
|
|
379
|
+
const handoffBeforeCompact = async (
|
|
380
|
+
event: SessionBeforeCompactEvent,
|
|
381
|
+
ctx: ExtensionContext,
|
|
382
|
+
model: Model<Api>,
|
|
383
|
+
config: ContextKitConfig,
|
|
384
|
+
) => {
|
|
385
|
+
const { preparation, branchEntries, customInstructions } = event;
|
|
386
|
+
const current = epoch(branchEntries);
|
|
387
|
+
background.invalidate(current, branchEntries);
|
|
388
|
+
const writing = !customInstructions && background.writing(current);
|
|
389
|
+
const compaction = await handoff({
|
|
390
|
+
ctx,
|
|
391
|
+
model,
|
|
392
|
+
config,
|
|
393
|
+
branch: branchEntries,
|
|
394
|
+
// A focused /compact needs a note written for that focus.
|
|
395
|
+
ready: customInstructions
|
|
396
|
+
? undefined
|
|
397
|
+
: await background.takeReady(current, branchEntries),
|
|
398
|
+
keptEntryId: preparation.firstKeptEntryId,
|
|
399
|
+
limit: Math.min(threshold(model, config), model.contextWindow),
|
|
400
|
+
// The live context no longer fits the model after an overflow.
|
|
401
|
+
messages:
|
|
402
|
+
event.reason === "overflow" || writing ? undefined : liveMessages(ctx),
|
|
403
|
+
focus: customInstructions,
|
|
404
|
+
signal: event.signal,
|
|
405
|
+
cancelBackground: () => background.cancel(),
|
|
406
|
+
});
|
|
407
|
+
return (
|
|
408
|
+
compaction && {
|
|
409
|
+
compaction: { ...compaction, tokensBefore: preparation.tokensBefore },
|
|
410
|
+
}
|
|
411
|
+
);
|
|
412
|
+
};
|
|
413
|
+
|
|
349
414
|
pi.on("session_before_compact", async (event, ctx) => {
|
|
350
415
|
const model = ctx.model;
|
|
351
416
|
const config = settings(ctx);
|
|
352
|
-
if (
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
!before(config.methodOrder, "handoff", "soft")
|
|
356
|
-
)
|
|
357
|
-
return undefined;
|
|
358
|
-
const { preparation, branchEntries, signal } = event;
|
|
359
|
-
const currentEpoch = epoch(branchEntries);
|
|
360
|
-
if (event.reason === "threshold" && failedEpoch === currentEpoch)
|
|
361
|
-
return undefined;
|
|
362
|
-
const limit = Math.min(threshold(model, config), model.contextWindow);
|
|
417
|
+
if (!model || !handoffEnabled(config)) return undefined;
|
|
418
|
+
const failed = failedEpoch === epoch(event.branchEntries);
|
|
419
|
+
if (event.reason === "threshold" && failed) return undefined;
|
|
363
420
|
try {
|
|
364
|
-
|
|
365
|
-
background.invalidate(current, branchEntries);
|
|
366
|
-
if (event.customInstructions) background.cancel();
|
|
367
|
-
const writing = !event.customInstructions && background.writing(current);
|
|
368
|
-
const compaction = await handoff({
|
|
369
|
-
ctx,
|
|
370
|
-
model,
|
|
371
|
-
config,
|
|
372
|
-
branch: branchEntries,
|
|
373
|
-
// A focused /compact needs a note written for that focus.
|
|
374
|
-
ready: event.customInstructions
|
|
375
|
-
? undefined
|
|
376
|
-
: await background.takeReady(current, branchEntries),
|
|
377
|
-
keptEntryId: preparation.firstKeptEntryId,
|
|
378
|
-
limit,
|
|
379
|
-
// The live context no longer fits the model after an overflow.
|
|
380
|
-
messages:
|
|
381
|
-
event.reason === "overflow" || writing
|
|
382
|
-
? undefined
|
|
383
|
-
: liveMessages(ctx),
|
|
384
|
-
focus: event.customInstructions,
|
|
385
|
-
signal,
|
|
386
|
-
cancelBackground: () => background.cancel(),
|
|
387
|
-
});
|
|
388
|
-
return (
|
|
389
|
-
compaction && {
|
|
390
|
-
compaction: { ...compaction, tokensBefore: preparation.tokensBefore },
|
|
391
|
-
}
|
|
392
|
-
);
|
|
421
|
+
return await handoffBeforeCompact(event, ctx, model, config);
|
|
393
422
|
} catch (error) {
|
|
394
|
-
if (!signal.aborted)
|
|
423
|
+
if (!event.signal.aborted)
|
|
424
|
+
warn(ctx, error, "Pi's summary is used instead.");
|
|
395
425
|
return undefined;
|
|
396
426
|
}
|
|
397
427
|
});
|
|
398
428
|
|
|
429
|
+
pi.on("session_tree", (_event, ctx) => {
|
|
430
|
+
const branch = ctx.sessionManager.getBranch();
|
|
431
|
+
background.invalidate(epoch(branch), branch);
|
|
432
|
+
});
|
|
399
433
|
pi.on("session_compact", () => background.cancel());
|
|
400
434
|
pi.on("session_shutdown", () => background.cancel());
|
|
401
435
|
|
|
@@ -403,8 +437,9 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
403
437
|
name: "recall",
|
|
404
438
|
label: "Recall",
|
|
405
439
|
description:
|
|
406
|
-
"Search or read the
|
|
407
|
-
"
|
|
440
|
+
"Search or read earlier text on the current session branch, including messages removed by compaction and tool outputs that were pruned or elided. " +
|
|
441
|
+
"Other branches are excluded. Images are represented only by MIME type. " +
|
|
442
|
+
"Pass `query` to list matching entries with their IDs, newest first. Pass `entryId` to read one entry's text, with pagination for large entries.",
|
|
408
443
|
parameters: Type.Object({
|
|
409
444
|
query: Type.Optional(
|
|
410
445
|
Type.String({ description: "Text to find, case-insensitive." }),
|
|
@@ -443,6 +478,50 @@ export default function contextKit(pi: ExtensionAPI): void {
|
|
|
443
478
|
});
|
|
444
479
|
}
|
|
445
480
|
|
|
481
|
+
/** One turn boundary's inputs and its current maintenance plan. */
|
|
482
|
+
interface Turn {
|
|
483
|
+
ctx: ExtensionContext;
|
|
484
|
+
config: ContextKitConfig;
|
|
485
|
+
model: Model<Api>;
|
|
486
|
+
limit: number;
|
|
487
|
+
entries: ProjectedSessionEntry[];
|
|
488
|
+
branch: SessionEntry[];
|
|
489
|
+
current: string | null;
|
|
490
|
+
result: MaintenancePlan;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// Without compaction, edits that leave the context above the threshold break the cache again next turn.
|
|
494
|
+
function persisted(result: MaintenancePlan, limit: number) {
|
|
495
|
+
return result.edits.length > 0 && result.tokens <= limit
|
|
496
|
+
? { entries: toDrafts(result.edits) }
|
|
497
|
+
: undefined;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function compacted(
|
|
501
|
+
result: MaintenancePlan,
|
|
502
|
+
compaction: Omit<CompactionEntryDraft, "type">,
|
|
503
|
+
) {
|
|
504
|
+
return {
|
|
505
|
+
entries: [
|
|
506
|
+
...toDrafts(result.edits),
|
|
507
|
+
{ type: "compaction" as const, ...compaction },
|
|
508
|
+
],
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function currentTokens(
|
|
513
|
+
ctx: ExtensionContext,
|
|
514
|
+
entries: readonly ProjectedSessionEntry[],
|
|
515
|
+
): number {
|
|
516
|
+
return (
|
|
517
|
+
ctx.getContextUsage()?.tokens ?? contextTokens(projectedMessages(entries))
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function handoffEnabled(config: ContextKitConfig): boolean {
|
|
522
|
+
return config.enabled && before(config.methodOrder, "handoff", "soft");
|
|
523
|
+
}
|
|
524
|
+
|
|
446
525
|
function liveMessages(ctx: ExtensionContext): Message[] {
|
|
447
526
|
return convertToLlm(ctx.sessionManager.buildSessionProjection().messages);
|
|
448
527
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-context-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Context maintenance for Pi: stale read replacement, pruning, handoff compaction with background notes, and recall.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,29 +23,42 @@
|
|
|
23
23
|
],
|
|
24
24
|
"files": [
|
|
25
25
|
"extensions",
|
|
26
|
-
"src"
|
|
27
|
-
"CHANGELOG.md"
|
|
26
|
+
"src"
|
|
28
27
|
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22.19.0"
|
|
30
|
+
},
|
|
29
31
|
"pi": {
|
|
30
32
|
"extensions": [
|
|
31
33
|
"./extensions/index.ts"
|
|
32
34
|
]
|
|
33
35
|
},
|
|
34
36
|
"scripts": {
|
|
35
|
-
"check": "
|
|
37
|
+
"check": "tsc --noEmit && npm run lint && vitest --run",
|
|
38
|
+
"lint": "node scripts/lint.ts",
|
|
36
39
|
"test": "vitest --run",
|
|
37
|
-
"release": "node scripts/release.ts prepare",
|
|
38
40
|
"prepublishOnly": "test -n \"$GITHUB_ACTIONS\" || test \"$(npm whoami)\" = hugolopes45 || { echo 'Publish only as the npm user hugolopes45.' >&2; exit 1; }; npm run check"
|
|
39
41
|
},
|
|
40
42
|
"peerDependencies": {
|
|
41
43
|
"@earendil-works/pi-ai": "*",
|
|
42
|
-
"@earendil-works/pi-coding-agent": "*"
|
|
44
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
45
|
+
"typebox": "*"
|
|
43
46
|
},
|
|
44
47
|
"devDependencies": {
|
|
45
|
-
"@earendil-works/pi-ai": "^1.
|
|
46
|
-
"@earendil-works/pi-coding-agent": "^1.
|
|
48
|
+
"@earendil-works/pi-ai": "^1.5.2",
|
|
49
|
+
"@earendil-works/pi-coding-agent": "^1.5.2",
|
|
47
50
|
"@types/node": "^26.6.4",
|
|
51
|
+
"eslint": "^10.14.0",
|
|
52
|
+
"typebox": "^1.3.27",
|
|
48
53
|
"typescript": "^7.0.2",
|
|
49
54
|
"vitest": "^5.0.3"
|
|
55
|
+
},
|
|
56
|
+
"overrides": {
|
|
57
|
+
"eslint": {
|
|
58
|
+
"minimatch": "^11.0.8"
|
|
59
|
+
},
|
|
60
|
+
"@eslint/config-array": {
|
|
61
|
+
"minimatch": "^11.0.8"
|
|
62
|
+
}
|
|
50
63
|
}
|
|
51
64
|
}
|
package/src/config.ts
CHANGED
|
@@ -12,8 +12,6 @@ export interface ContextKitConfig {
|
|
|
12
12
|
methodOrder: Method[];
|
|
13
13
|
/** Replace older reads of a file that was read again. */
|
|
14
14
|
supersedeReads: boolean;
|
|
15
|
-
/** Superseded reads are replaced only within this many newest tokens, to keep the prompt cache. */
|
|
16
|
-
staleSuffixTokens: number;
|
|
17
15
|
prune: PruneOptions & { enabled: boolean };
|
|
18
16
|
shake: ShakeOptions;
|
|
19
17
|
/** Pruning and shaking must reach this fraction of the threshold, or compaction runs instead. */
|
|
@@ -39,7 +37,6 @@ export const DEFAULT_CONFIG: ContextKitConfig = {
|
|
|
39
37
|
thresholdPercent: -1,
|
|
40
38
|
methodOrder: [...METHODS],
|
|
41
39
|
supersedeReads: true,
|
|
42
|
-
staleSuffixTokens: 8_000,
|
|
43
40
|
prune: {
|
|
44
41
|
enabled: true,
|
|
45
42
|
protectTokens: 40_000,
|
|
@@ -154,11 +151,6 @@ export function readConfig(raw: unknown): ConfigResult {
|
|
|
154
151
|
);
|
|
155
152
|
const methods = root.get("methodOrder", methodOrder, d.methodOrder);
|
|
156
153
|
const supersedeReads = root.get("supersedeReads", boolean, d.supersedeReads);
|
|
157
|
-
const staleSuffixTokens = root.get(
|
|
158
|
-
"staleSuffixTokens",
|
|
159
|
-
count,
|
|
160
|
-
d.staleSuffixTokens,
|
|
161
|
-
);
|
|
162
154
|
const progressRatio = root.get("progressRatio", ratio, d.progressRatio);
|
|
163
155
|
|
|
164
156
|
const p = root.section("prune");
|
|
@@ -188,7 +180,6 @@ export function readConfig(raw: unknown): ConfigResult {
|
|
|
188
180
|
thresholdPercent,
|
|
189
181
|
methodOrder: [...new Set(methods.filter(isMethod))],
|
|
190
182
|
supersedeReads,
|
|
191
|
-
staleSuffixTokens,
|
|
192
183
|
prune,
|
|
193
184
|
shake,
|
|
194
185
|
progressRatio,
|
package/src/context.ts
CHANGED
|
@@ -46,21 +46,6 @@ export function messageTokens(message: AgentMessage): number {
|
|
|
46
46
|
return estimateTokens(message);
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
/** Tokens from each entry through the end of the projected context. */
|
|
50
|
-
export function suffixTokens(
|
|
51
|
-
entries: readonly ProjectedSessionEntry[],
|
|
52
|
-
): number[] {
|
|
53
|
-
const suffix = new Array<number>(entries.length).fill(0);
|
|
54
|
-
let total = 0;
|
|
55
|
-
for (let i = entries.length - 1; i >= 0; i--) {
|
|
56
|
-
for (const message of entries[i]?.messages ?? []) {
|
|
57
|
-
if (message.role !== "system") total += estimateTokens(message);
|
|
58
|
-
}
|
|
59
|
-
suffix[i] = total;
|
|
60
|
-
}
|
|
61
|
-
return suffix;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
49
|
export function withContent(
|
|
65
50
|
message: EditableMessage,
|
|
66
51
|
content: ContextEditableContent,
|
package/src/maintain.ts
CHANGED
|
@@ -12,7 +12,7 @@ import { supersededReads } from "./stale.ts";
|
|
|
12
12
|
|
|
13
13
|
export type MaintenanceOptions = Pick<
|
|
14
14
|
ContextKitConfig,
|
|
15
|
-
"supersedeReads" | "
|
|
15
|
+
"supersedeReads" | "prune" | "progressRatio"
|
|
16
16
|
> & {
|
|
17
17
|
/** Undefined leaves the context unshaken. */
|
|
18
18
|
shake: ShakeOptions | undefined;
|
|
@@ -35,24 +35,19 @@ export interface MaintenancePlan {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* Plans context edits for one turn boundary. Any context edit
|
|
39
|
-
*
|
|
40
|
-
*
|
|
38
|
+
* Plans context edits for one turn boundary. Any context edit changes the prompt prefix, so the
|
|
39
|
+
* provider cache and, on Anthropic, the thinking blocks after the edit are lost. Edits are
|
|
40
|
+
* therefore made only above the compaction threshold, in one batch.
|
|
41
41
|
*/
|
|
42
42
|
export function planMaintenance(input: MaintenanceInput): MaintenancePlan {
|
|
43
43
|
const { options } = input;
|
|
44
44
|
const unchanged = { edits: [], tokens: input.tokens };
|
|
45
|
+
if (input.tokens <= input.threshold) return unchanged;
|
|
46
|
+
|
|
45
47
|
const stale = options.supersedeReads
|
|
46
|
-
? supersededReads(input.entries, input.edited
|
|
48
|
+
? supersededReads(input.entries, input.edited)
|
|
47
49
|
: [];
|
|
48
50
|
const afterStale = applyEdits(input.entries, stale);
|
|
49
|
-
const staleTokens = contextTokens(projectedMessages(afterStale));
|
|
50
|
-
|
|
51
|
-
if (input.tokens <= input.threshold) {
|
|
52
|
-
return stale.length > 0 && staleTokens <= input.threshold
|
|
53
|
-
? { edits: stale, tokens: staleTokens }
|
|
54
|
-
: unchanged;
|
|
55
|
-
}
|
|
56
51
|
|
|
57
52
|
const edited = new Set([
|
|
58
53
|
...input.edited,
|
|
@@ -71,7 +66,9 @@ export function planMaintenance(input: MaintenanceInput): MaintenancePlan {
|
|
|
71
66
|
return edits.length > 0 ? { edits, tokens: reducedTokens } : unchanged;
|
|
72
67
|
}
|
|
73
68
|
// Compaction runs next. Stale edits only shrink what it has to summarize.
|
|
74
|
-
return stale.length > 0
|
|
69
|
+
return stale.length > 0
|
|
70
|
+
? { edits: stale, tokens: contextTokens(projectedMessages(afterStale)) }
|
|
71
|
+
: unchanged;
|
|
75
72
|
}
|
|
76
73
|
|
|
77
74
|
function latestPerTarget(edits: readonly Edit[]): Edit[] {
|