@juicesharp/rpiv-btw 2.13.4 → 2.13.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.
- package/README.md +5 -5
- package/btw-budget.ts +18 -6
- package/btw.ts +30 -11
- package/docs/context-model.md +13 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -51,22 +51,22 @@ pick, but Pi needs an active model with working credentials (`/login`).
|
|
|
51
51
|
- **Nothing leaks into the main chat** — the answer is drawn in an overlay, never
|
|
52
52
|
emitted as an agent message, never written to the transcript, never written to disk.
|
|
53
53
|
- **The side question already knows your work** — it is handed a read-only clone
|
|
54
|
-
of the current
|
|
54
|
+
of the main agent's current context, so you do not re-explain context.
|
|
55
55
|
- **Follow-ups have their own thread** — every `/btw` turn in a session is replayed
|
|
56
56
|
into the next one, so the side conversation remembers itself.
|
|
57
57
|
- **`Esc` cancels only the side question** — cancelling it never interrupts what
|
|
58
58
|
the main session is doing.
|
|
59
59
|
- **Survives `/new`, `/fork`, `/resume`, `/reload`** — history is held in the
|
|
60
60
|
running Pi process and clears when Pi exits.
|
|
61
|
-
- **Correct after compaction** — the
|
|
62
|
-
|
|
63
|
-
stale view.
|
|
61
|
+
- **Correct after compaction** — the clone is the compacted context the main
|
|
62
|
+
agent sees, not the full history, and it is rebuilt whenever the conversation is
|
|
63
|
+
compacted or re-branched, so a later `/btw` never answers off a stale view.
|
|
64
64
|
- **No tools, plain text** — a side question cannot edit a file or run a command.
|
|
65
65
|
|
|
66
66
|
## Reference
|
|
67
67
|
|
|
68
68
|
- [Context model](https://github.com/juicesharp/rpiv-mono/blob/main/packages/rpiv-btw/docs/context-model.md) — exactly what each `/btw` call
|
|
69
|
-
sends to the model: the
|
|
69
|
+
sends to the model: the context snapshot and its invalidation rules, session
|
|
70
70
|
history threading, the cross-session question hint, and the no-pollution
|
|
71
71
|
guarantees.
|
|
72
72
|
- [Architecture](https://github.com/juicesharp/rpiv-mono/blob/main/packages/rpiv-btw/docs/architecture.md) — modules, registered command and
|
package/btw-budget.ts
CHANGED
|
@@ -95,10 +95,11 @@ export function capHistory(history: BtwTurn[], budget = BTW_HISTORY_TOKEN_BUDGET
|
|
|
95
95
|
// ---------------------------------------------------------------------------
|
|
96
96
|
|
|
97
97
|
export interface FitBranchInput {
|
|
98
|
-
/**
|
|
98
|
+
/** The main agent's context entries from the snapshot (compaction summary + kept +
|
|
99
|
+
* newer; findCutPoint/getLastAssistantUsage contract). */
|
|
99
100
|
entries: SessionEntry[];
|
|
100
|
-
/** Cached
|
|
101
|
-
* Returned by reference on the fast path (byte-identical prefix). */
|
|
101
|
+
/** Cached main-agent context messages (`branchToContextMessages`) — the same context
|
|
102
|
+
* the usage anchor measured. Returned by reference on the fast path (byte-identical prefix). */
|
|
102
103
|
messages: Message[];
|
|
103
104
|
model: Model<Api>;
|
|
104
105
|
systemPrompt: string;
|
|
@@ -218,9 +219,10 @@ function trimBranch(entries: SessionEntry[], keepRecentTokens: number): { messag
|
|
|
218
219
|
const startIdx = forwardScanToTurnStart(entries, cut.firstKeptEntryIndex);
|
|
219
220
|
if (startIdx < 0) return { messages: null }; // no turn-start in the kept suffix → stub the cache
|
|
220
221
|
const keptEntries = entries.slice(startIdx);
|
|
221
|
-
//
|
|
222
|
-
//
|
|
223
|
-
//
|
|
222
|
+
// After a cut, convert entry by entry (UNFILTERED sessionEntryToContextMessages) so a
|
|
223
|
+
// head compaction/branch summary survives, matching the main agent's post-compact
|
|
224
|
+
// context. Pi >= 1.0 context edits are not re-applied to the kept suffix; what is sent
|
|
225
|
+
// is exactly what is estimated, so the budget still holds.
|
|
224
226
|
const trimmed = convertToLlm(keptEntries.flatMap((e) => sessionEntryToContextMessages(e)));
|
|
225
227
|
return { messages: trimmed };
|
|
226
228
|
}
|
|
@@ -307,6 +309,16 @@ function truncateToFit(result: Message[], budget: number): boolean {
|
|
|
307
309
|
result[target.mi] = { ...msg, content };
|
|
308
310
|
}
|
|
309
311
|
}
|
|
312
|
+
} else if (msg.role === "system") {
|
|
313
|
+
// Pi ≥ 1.1 passes system messages through convertToLlm; content is a string or text parts.
|
|
314
|
+
if (typeof msg.content === "string") {
|
|
315
|
+
result[target.mi] = { ...msg, content: `${msg.content.slice(0, keepChars)}${marker}` };
|
|
316
|
+
} else {
|
|
317
|
+
const content = [...msg.content];
|
|
318
|
+
const part = content[target.ci];
|
|
319
|
+
content[target.ci] = { ...part, text: `${part.text.slice(0, keepChars)}${marker}` };
|
|
320
|
+
result[target.mi] = { ...msg, content };
|
|
321
|
+
}
|
|
310
322
|
} else if (msg.role === "assistant") {
|
|
311
323
|
const content = [...msg.content];
|
|
312
324
|
const part = content[target.ci];
|
package/btw.ts
CHANGED
|
@@ -11,6 +11,8 @@ import { readFileSync } from "node:fs";
|
|
|
11
11
|
import { fileURLToPath } from "node:url";
|
|
12
12
|
import type { AssistantMessage, Message, StopReason, UserMessage } from "@earendil-works/pi-ai";
|
|
13
13
|
import {
|
|
14
|
+
buildContextEntries,
|
|
15
|
+
buildSessionContext,
|
|
14
16
|
convertToLlm,
|
|
15
17
|
type ExtensionAPI,
|
|
16
18
|
type ExtensionCommandContext,
|
|
@@ -71,11 +73,30 @@ interface BtwState {
|
|
|
71
73
|
snapshots: Map<string, { messages: Message[]; entries: SessionEntry[] }>;
|
|
72
74
|
}
|
|
73
75
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
/**
|
|
77
|
+
* Serialize a branch into the main agent's model context — the single branch→LLM
|
|
78
|
+
* conversion seam. The branch is the one session view every supported Pi exposes
|
|
79
|
+
* to extensions (`getBranch()`; the read-only session manager gained context
|
|
80
|
+
* methods only in later releases), so /btw reads it and serializes it with Pi's
|
|
81
|
+
* own exported `buildSessionContext`: after a compaction that is the compaction
|
|
82
|
+
* summary plus the kept and newer messages, and on Pi >= 1.0 context edits are
|
|
83
|
+
* applied — exactly the context the main model receives and the usage anchor
|
|
84
|
+
* meters. Filtering the branch's raw messages instead re-sent every summarized
|
|
85
|
+
* message (#274).
|
|
86
|
+
*/
|
|
87
|
+
export function branchToContextMessages(branch: SessionEntry[]): Message[] {
|
|
88
|
+
return convertToLlm(buildSessionContext(branch).messages);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The /btw snapshot: the main agent's context messages, plus its context entries
|
|
92
|
+
* (compaction summary + kept + newer, via Pi's `buildContextEntries`) for the
|
|
93
|
+
* anchor/cut primitives. */
|
|
94
|
+
export function readContextSnapshot(sessionManager: ExtensionContext["sessionManager"]): {
|
|
95
|
+
messages: Message[];
|
|
96
|
+
entries: SessionEntry[];
|
|
97
|
+
} {
|
|
98
|
+
const branch = sessionManager.getBranch() as SessionEntry[];
|
|
99
|
+
return { messages: branchToContextMessages(branch), entries: buildContextEntries(branch) };
|
|
79
100
|
}
|
|
80
101
|
|
|
81
102
|
// ---------------------------------------------------------------------------
|
|
@@ -172,12 +193,11 @@ export type BtwExecResult =
|
|
|
172
193
|
| { kind: "error"; error: string; stopReason?: StopReason }
|
|
173
194
|
| { kind: "aborted"; stopReason: StopReason };
|
|
174
195
|
|
|
175
|
-
function
|
|
196
|
+
function readSnapshot(ctx: ExtensionContext): { messages: Message[]; entries: SessionEntry[] } {
|
|
176
197
|
const cached = getSnapshot(ctx);
|
|
177
198
|
if (cached) return cached;
|
|
178
199
|
// Cold start (no message_end fired yet) — fall back to a single live read.
|
|
179
|
-
|
|
180
|
-
return { messages: branchToMessages(branch), entries: branch };
|
|
200
|
+
return readContextSnapshot(ctx.sessionManager);
|
|
181
201
|
}
|
|
182
202
|
|
|
183
203
|
export interface BtwBuiltContext {
|
|
@@ -197,7 +217,7 @@ export function buildBtwMessages(
|
|
|
197
217
|
// ctx.model is non-null here — executeBtw returns early on !model before calling.
|
|
198
218
|
const model = ctx.model!;
|
|
199
219
|
const history = getSessionHistory(ctx);
|
|
200
|
-
const { messages, entries } =
|
|
220
|
+
const { messages, entries } = readSnapshot(ctx);
|
|
201
221
|
const systemPrompt = buildSystemPrompt();
|
|
202
222
|
const fitInput = { entries, messages, model, systemPrompt, question: userMessage };
|
|
203
223
|
|
|
@@ -360,8 +380,7 @@ export function registerMessageEndSnapshot(pi: ExtensionAPI): void {
|
|
|
360
380
|
const msg = event.message;
|
|
361
381
|
if (msg.role !== "assistant") return;
|
|
362
382
|
if ((msg as AssistantMessage).stopReason === "toolUse") return;
|
|
363
|
-
|
|
364
|
-
setSnapshot(ctx, { messages: branchToMessages(branch), entries: branch });
|
|
383
|
+
setSnapshot(ctx, readContextSnapshot(ctx.sessionManager));
|
|
365
384
|
});
|
|
366
385
|
}
|
|
367
386
|
|
package/docs/context-model.md
CHANGED
|
@@ -13,22 +13,28 @@ lighter-weight side model.
|
|
|
13
13
|
| --- | --- |
|
|
14
14
|
| Model | `ctx.model`, with credentials from `ctx.modelRegistry.getApiKeyAndHeaders(model)` |
|
|
15
15
|
| System prompt | the bundled `prompts/btw-system.txt`, plus the cross-session hint appendix |
|
|
16
|
-
| Messages | `[...
|
|
16
|
+
| Messages | `[...main-agent context clone, ...this-session /btw turns, your question]` |
|
|
17
17
|
| Tools | `[]` — none, always |
|
|
18
18
|
| Abort signal | a fresh `AbortController` owned by `/btw`, never `ctx.signal` |
|
|
19
19
|
|
|
20
|
-
## The
|
|
20
|
+
## The context clone
|
|
21
21
|
|
|
22
|
-
The first block of messages is a read-only clone of
|
|
23
|
-
so you do not have to re-explain what you have been working on.
|
|
22
|
+
The first block of messages is a read-only clone of the main agent's current
|
|
23
|
+
context, so you do not have to re-explain what you have been working on. It is the
|
|
24
|
+
same context Pi sends the main model: after a compaction, the compaction summary
|
|
25
|
+
plus the kept and newer messages, not the full history the summary replaced. On
|
|
26
|
+
Pi 1.0 and later it also reflects context edits.
|
|
24
27
|
|
|
25
|
-
- The `message_end` hook takes the snapshot: it reads
|
|
26
|
-
|
|
28
|
+
- The `message_end` hook takes the snapshot: it reads the branch
|
|
29
|
+
(`ctx.sessionManager.getBranch()`, the session view every supported Pi gives
|
|
30
|
+
extensions) and serializes it with Pi's own `buildSessionContext`, then
|
|
31
|
+
`convertToLlm`. Pi's `buildContextEntries` gives the matching entries that
|
|
32
|
+
budgeting measures.
|
|
27
33
|
- It snapshots **only** on `role === "assistant"` messages, and **only** when
|
|
28
34
|
`stopReason !== "toolUse"` — so mid-tool-call states never become the context.
|
|
29
35
|
- Snapshots are cached per session, keyed by `ctx.sessionManager.getSessionFile()`,
|
|
30
36
|
falling back to `memory:<sessionId>` when there is no session file.
|
|
31
|
-
- Cold start: if no `message_end` has fired yet, `/btw` reads the
|
|
37
|
+
- Cold start: if no `message_end` has fired yet, `/btw` reads the context live
|
|
32
38
|
instead of using a cache.
|
|
33
39
|
|
|
34
40
|
### Invalidation
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juicesharp/rpiv-btw",
|
|
3
|
-
"version": "2.13.
|
|
3
|
+
"version": "2.13.5",
|
|
4
4
|
"description": "Pi extension. The /btw slash command, for putting a one-off side question to the same primary model without polluting the main conversation.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|