@astrosheep/pi-context 0.5.0 → 0.6.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 +2 -1
- package/package.json +1 -1
- package/src/index.ts +34 -4
package/README.md
CHANGED
|
@@ -21,7 +21,8 @@ The extension composes Pi's public `session_before_compact` / `session_compact`
|
|
|
21
21
|
- **`new_context` tool** — the model requests a fresh context window. The extension waits for the current tool turn to end, compacts with a short deterministic reset message (old conversation is excluded from the new provider context but stays in the session), then sends exactly one hidden continuation turn.
|
|
22
22
|
- **`<context_window>` hint** — persisted as a visible custom message at session start and after each window reset (Codex-style: written once into history instead of re-injected per request). It carries the agent name, first/current/previous window IDs, and the 5 most recently updated notes. Within a window the note list goes stale, exactly like Codex's steady-state world-state diffing.
|
|
23
23
|
- **Low-budget guidance** — when remaining context first drops to 16,000 tokens or below, a `<context_window_guidance>` reminder is **persisted once per window** into history (TUI-visible, no extra turn; `sendMessage` safely defers to end of turn mid-stream). The in-flight request additionally gets one transient tail copy so the model sees it immediately. Text is static and appended, so the provider prefix cache survives. The exact remaining figure is one `get_context_remaining` call away. Note: Pi's built-in auto-compaction fires when remaining context falls below `reserveTokens` (default 16,384), so raise the reminder threshold above your `reserveTokens` or the reminder never precedes compaction.
|
|
24
|
-
- **
|
|
24
|
+
- **Auto-compact fallback** — Codex `auto_compact_fallback_prompt` parity, adapted to Pi's trigger points. On the first proactive `threshold` compaction in a window (post-run, agent still streaming), the extension cancels compaction once and steers in a note-taking turn ("write durable state with `notes_write_file` now"); the next threshold trigger performs the real reset and auto-continues, like Codex's mid-turn rollover. The pre-prompt threshold path (idle — cancelling would race the user prompt) and `overflow` recovery (cancelling would abandon Pi's one-shot retry) reset immediately without a fallback turn.
|
|
25
|
+
- **Runtime toggle** — `/pi-context off` disables hint injection, guidance, fallback turns, and reset-style compaction (Pi's default compaction, including `keepRecentTokens`, applies again). `/pi-context on` re-enables; a bare `/pi-context` reports the current state.
|
|
25
26
|
- **History tools** — the model searches pre-reset conversation with case-sensitive literal substring search, exactly like Codex's `history.*` namespace.
|
|
26
27
|
- **Notes tools** — persistent, session-scoped virtual files that survive window resets.
|
|
27
28
|
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -6,6 +6,7 @@ const STATE_TYPE = "pi-context/state";
|
|
|
6
6
|
const NOTE_TYPE = "pi-context/note";
|
|
7
7
|
const HINT_TYPE = "pi-context/hint";
|
|
8
8
|
const GUIDANCE_TYPE = "pi-context/guidance";
|
|
9
|
+
const FALLBACK_TYPE = "pi-context/fallback";
|
|
9
10
|
const RESET_MARKER_TYPE = "pi-context/reset-marker";
|
|
10
11
|
const CONTINUATION_TYPE = "pi-context/continuation";
|
|
11
12
|
const MAX_NOTE_BYTES = 1_000_000;
|
|
@@ -17,6 +18,10 @@ const REMINDER_THRESHOLD_TOKENS = 16_000;
|
|
|
17
18
|
const RESET_SUMMARY = "Context window reset. Prior session entries remain available only through the pi-context history tools.";
|
|
18
19
|
const CONTINUATION = "This is a fresh context window. Recover only the details needed to continue with history_* and notes_*; then continue the task.";
|
|
19
20
|
|
|
21
|
+
/** Codex auto_compact_fallback_prompt parity: one note-taking chance before an automatic reset. */
|
|
22
|
+
const FALLBACK_PROMPT =
|
|
23
|
+
"Context limit reached. This window is about to be reset. Write durable state with notes_write_file now: task state, decisions, open issues, next steps. Do not start new work. After this turn the window resets automatically; old conversation stays searchable through the history_* tools.";
|
|
24
|
+
|
|
20
25
|
type NoteFile = { text: string; createdAt: number; updatedAt: number };
|
|
21
26
|
type NoteOperation = {
|
|
22
27
|
op: "write" | "append";
|
|
@@ -277,6 +282,8 @@ export default function piContext(pi: ExtensionAPI) {
|
|
|
277
282
|
let rollover: "idle" | "requested" | "compacting" | "continued" = "idle";
|
|
278
283
|
let enabled = true;
|
|
279
284
|
let guidancePersistedInWindow: string | undefined;
|
|
285
|
+
let fallbackSentInWindow: string | undefined;
|
|
286
|
+
let continueAfterFallback = false;
|
|
280
287
|
|
|
281
288
|
/** Persist the context_window hint as a visible message (lands in history and the TUI), Codex-style. */
|
|
282
289
|
const persistHint = (ctx: ExtensionContext) => {
|
|
@@ -490,6 +497,22 @@ export default function piContext(pi: ExtensionAPI) {
|
|
|
490
497
|
if (!enabled) return undefined; // Default Pi compaction applies; keepRecentTokens is honored again.
|
|
491
498
|
// Never let an aborted or failed custom reset fall through to Pi's default summary.
|
|
492
499
|
if (event.signal.aborted) return { cancel: true };
|
|
500
|
+
// Codex auto_compact_fallback_prompt parity, adapted to Pi's trigger points:
|
|
501
|
+
// - threshold, post-run (agent still streaming): cancel once per window and steer a
|
|
502
|
+
// note-taking turn in; _runAutoCompaction then returns hasQueuedMessages() and the
|
|
503
|
+
// post-run loop delivers it via agent.continue(). Safe, intended path.
|
|
504
|
+
// - threshold, pre-prompt (idle): cancelling still sends the user prompt with an
|
|
505
|
+
// over-threshold context, and sendMessage would race _runAgentPrompt. Reset instead.
|
|
506
|
+
// - overflow: never cancel; that would abandon Pi's one-shot compact-and-retry recovery.
|
|
507
|
+
if (event.reason === "threshold" && !ctx.isIdle()) {
|
|
508
|
+
const windowId = currentWindowId(ctx);
|
|
509
|
+
if (fallbackSentInWindow !== windowId) {
|
|
510
|
+
fallbackSentInWindow = windowId;
|
|
511
|
+
continueAfterFallback = true;
|
|
512
|
+
pi.sendMessage({ customType: FALLBACK_TYPE, content: FALLBACK_PROMPT, display: true }, { triggerTurn: true });
|
|
513
|
+
return { cancel: true };
|
|
514
|
+
}
|
|
515
|
+
}
|
|
493
516
|
try {
|
|
494
517
|
pi.appendEntry(RESET_MARKER_TYPE, { version: 1, reason: event.reason, requested: rollover === "compacting" });
|
|
495
518
|
const markerId = ctx.sessionManager.getLeafId();
|
|
@@ -501,11 +524,18 @@ export default function piContext(pi: ExtensionAPI) {
|
|
|
501
524
|
});
|
|
502
525
|
|
|
503
526
|
pi.on("session_compact", (event, ctx) => {
|
|
504
|
-
if (!enabled)
|
|
527
|
+
if (!enabled) {
|
|
528
|
+
continueAfterFallback = false;
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
505
531
|
// Overflow retry is already continued once by Pi core. Sending another turn would duplicate it.
|
|
506
532
|
if (event.willRetry) return;
|
|
507
|
-
|
|
508
|
-
|
|
533
|
+
// Continue after our own rollover, and after a fallback reset (Codex rolls over
|
|
534
|
+
// mid-turn and keeps going). A user's manual /compact gets no continuation.
|
|
535
|
+
const shouldContinue = rollover === "compacting" || continueAfterFallback;
|
|
536
|
+
continueAfterFallback = false;
|
|
537
|
+
if (rollover === "compacting") rollover = "continued";
|
|
538
|
+
if (!shouldContinue) return;
|
|
509
539
|
pi.appendEntry(STATE_TYPE, { version: 1, lastResetEntryId: event.compactionEntry.id });
|
|
510
540
|
persistHint(ctx);
|
|
511
541
|
pi.sendMessage({ customType: CONTINUATION_TYPE, content: CONTINUATION, display: false }, { triggerTurn: true });
|
|
@@ -516,4 +546,4 @@ export default function piContext(pi: ExtensionAPI) {
|
|
|
516
546
|
});
|
|
517
547
|
}
|
|
518
548
|
|
|
519
|
-
export const internal = { MAX_NOTE_BYTES, NOTE_TYPE, HINT_TYPE, GUIDANCE_TYPE, RESET_MARKER_TYPE, RESET_SUMMARY, CONTINUATION, CONTEXT_WINDOW_OPEN_TAG, GUIDANCE_OPEN_TAG, REMINDER_THRESHOLD_TOKENS, lineRange, assertVirtualPath };
|
|
549
|
+
export const internal = { MAX_NOTE_BYTES, NOTE_TYPE, HINT_TYPE, GUIDANCE_TYPE, FALLBACK_TYPE, FALLBACK_PROMPT, RESET_MARKER_TYPE, RESET_SUMMARY, CONTINUATION, CONTEXT_WINDOW_OPEN_TAG, GUIDANCE_OPEN_TAG, REMINDER_THRESHOLD_TOKENS, lineRange, assertVirtualPath };
|