Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(pi): mark only compactions that made a summary; save earlier text first

AdityaUmale40e5208b6863
diff --git a/server/drivers/pi.test.ts b/server/drivers/pi.test.ts
index 13ddc42eb8..c71004ec85 100644
--- a/server/drivers/pi.test.ts
+++ b/server/drivers/pi.test.ts
@@ -539,7 +539,20 @@ describe("PiDriver turns (fake CLI)", () => {
const second = await send("second", first.cursor);
expect(second.message).toBe("Standing rules.\n\nMemory: likes quiet hours.\n\nsecond");
// Each turn condensed the session once; the chat marks each one.
- expect(recorder.events.filter((e) => e.type === "context.compacted" && e.threadId === threadId)).toHaveLength(2);
+ const mine = recorder.events.filter((e) => e.threadId === threadId);
+ expect(mine.filter((e) => e.type === "context.compacted")).toHaveLength(2);
+ // Text streamed before the compaction is saved ahead of the marker, not joined to later text.
+ const before = mine.findIndex((e) => e.type === "item.completed" && (e as { text?: string }).text === "before");
+ expect(before).toBeGreaterThan(-1);
+ expect(before).toBeLessThan(mine.findIndex((e) => e.type === "context.compacted"));
+ });
+
+ it("adds no condensed marker when pi's compaction fails or is skipped", async () => {
+ await create("compaction", { FAKE_PI_COMPACTION_FAILS_ONLY: "1" });
+ const threadId = "t-pi-compaction-fails-" + randomUUID();
+ const { turnId } = await instance.adapter.sendTurn({ threadId, text: "first", system: "Standing rules." });
+ await recorder.until((e) => e.type === "turn.completed" && e.turnId === turnId);
+ expect(recorder.events.filter((e) => e.type === "context.compacted" && e.threadId === threadId)).toHaveLength(0);
});
it("keeps pi alive through post-run compaction recovery and re-establishes the full prompt", async () => {
diff --git a/server/drivers/pi.ts b/server/drivers/pi.ts
index 9a56f00ef8..3ef790fb44 100644
--- a/server/drivers/pi.ts
+++ b/server/drivers/pi.ts
@@ -734,7 +734,7 @@ export const PiDriver: ProviderDriver<PiConfig> = {
compactionObserved = true;
deletePromptSplitReceipt("pi", JSON.stringify([threadId, sessionFile]));
}
- if (evt.type.endsWith("compaction_end") && !(evt as { aborted?: boolean }).aborted && !(evt as { errorMessage?: string }).errorMessage) emit({ ...base(threadId, turnId), type: "context.compacted" });
+ if (evt.type.endsWith("compaction_end") && (evt as { result?: unknown }).result && !(evt as { aborted?: boolean }).aborted && !(evt as { errorMessage?: string }).errorMessage) { flushAssistantText(); emit({ ...base(threadId, turnId), type: "context.compacted" }); }
return;
}
case "message_end":
diff --git a/server/testing/fake-pi-cli.ts b/server/testing/fake-pi-cli.ts
index 9b9e3211e4..356f9fd586 100755
--- a/server/testing/fake-pi-cli.ts
+++ b/server/testing/fake-pi-cli.ts
@@ -133,11 +133,17 @@ const streamErrorTurn = () => {
const streamCompactionTurn = () => {
send({ type: "agent_start" });
send({ type: "turn_start" });
- // A failed attempt first (pi reports it with errorMessage, not aborted), then the one that succeeds.
+ send({ type: "message_update", usage: { input: 0, output: 0 }, assistantMessageEvent: { type: "text_delta", contentIndex: 0, delta: "before" } });
+ // A failed attempt (pi reports it with errorMessage, not aborted) and a skipped one (no result, no error, e.g. an
+ // expired credential); FAKE_PI_COMPACTION_FAILS_ONLY=1 stops there, otherwise one attempt succeeds with a summary.
send({ type: "compaction_start", reason: "threshold" });
send({ type: "compaction_end", reason: "threshold", result: undefined, aborted: false, willRetry: false, errorMessage: "Compaction failed" });
send({ type: "compaction_start", reason: "threshold" });
send({ type: "compaction_end", reason: "threshold", result: undefined, aborted: false, willRetry: false });
+ if (process.env.FAKE_PI_COMPACTION_FAILS_ONLY !== "1") {
+ send({ type: "compaction_start", reason: "threshold" });
+ send({ type: "compaction_end", reason: "threshold", result: { summary: "fake summary", firstKeptEntryId: "e1", tokensBefore: 100 }, aborted: false, willRetry: false });
+ }
send({ type: "message_update", usage: { input: 0, output: 0 }, assistantMessageEvent: { type: "text_delta", contentIndex: 0, delta: "compacted" } });
send({ type: "turn_end", message: { stopReason: "end_turn", usage: { input: 12, output: 3 } }, usage: { input: 12, output: 3 } });
send({ type: "agent_end" });