Skip to content

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

fix(prompt): keep the phone section out of the stable prompt half

Santhi Prakash322fa883c899
diff --git a/server/host/phones.test.ts b/server/host/phones.test.ts
index 013670f79b..ddf809be69 100644
--- a/server/host/phones.test.ts
+++ b/server/host/phones.test.ts
@@ -7,10 +7,12 @@ import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import type { CloudLendingTurn } from "../cloud-lending.ts";
+import { splitSessionPrompt, type PromptSplitReceipt } from "../drivers/prompt-split.ts";
import { PHONE_PLACE_PROMPT } from "../phones/instructions.ts";
import type { RequestAuth } from "../request-auth.ts";
import type { SessionRecord } from "../sessions.ts";
import type { BotRecord } from "../store.ts";
+import { buildSystemPrompt } from "../system-prompt.ts";
import { removeTempDir } from "../testing/cleanup.ts";
import { createPhones, type PhonesDeps } from "./phones.ts";
@@ -104,6 +106,34 @@ describe("where the person is", () => {
]);
expect(phones.promptParts(chief, { mounted: false, offered: false, turn: TURN, place: "phone" })).toEqual([{ id: "device", label: "Phone", text: "" }]);
});
+
+ it("keeps the phone section's sender-dependent text out of the stable half, so alternating senders do not resend the prompt", () => {
+ const { phones, send } = world();
+ let hook = 0;
+ // What index.ts hands buildSystemPrompt for one turn: the device integration is mounted only on the owner's own
+ // turn, so the "device" part carries phoneSystemPrompt there and phoneWithheldPrompt on a webhook's.
+ const promptFor = (sender: "person" | "webhook") => {
+ if (sender === "person") send(phones.sendIdFor(loopback));
+ else send(`webhook-send-${++hook}`, { automation: "webhook", origin: { kind: "outside" } });
+ const mounted = Boolean(phones.integration(chief, TURN));
+ return buildSystemPrompt("You are Kiwi.", "", phones.promptParts(chief, {
+ mounted, offered: true, turn: TURN, place: phones.place(THREAD, sender === "person"),
+ }));
+ };
+ let receipt: PromptSplitReceipt | null = null;
+ const full: boolean[] = [];
+ const firstTwo: { stable: string; volatile: string }[] = [];
+ for (let turn = 0; turn < 12; turn++) {
+ const built = promptFor(turn % 2 === 0 ? "person" : "webhook");
+ if (turn < 2) firstTwo.push({ stable: built.stable, volatile: built.volatile });
+ const split = splitSessionPrompt(built.stable, built.volatile, receipt, built.text, `turn ${turn}`, false, 8);
+ receipt = split.receipt;
+ full.push(split.text.startsWith(built.text));
+ }
+ expect(full.map((sent) => (sent ? "F" : ".")).join("")).toBe("F........F..");
+ expect(firstTwo[0]!.volatile).not.toBe(firstTwo[1]!.volatile);
+ expect(firstTwo[0]!.stable).toBe(firstTwo[1]!.stable);
+ });
});
describe("whose phones", () => {
diff --git a/server/system-prompt.test.ts b/server/system-prompt.test.ts
index d1b991ff20..51938ef065 100644
--- a/server/system-prompt.test.ts
+++ b/server/system-prompt.test.ts
@@ -162,14 +162,16 @@ describe("buildSystemPrompt", () => {
expect(built.stable).toBe(`You are Kiwi. Set yourself up.${WEBHOOK_PROMPT}${REPLY_HABITS_PROMPT}`);
});
- it("keeps the phone part stable and where the person is volatile, so a turn from the phone keeps the cached prefix", () => {
- const at = (place: string) => buildSystemPrompt("You are Kiwi.", "", [
- { id: "device", label: "Phone", text: " The phone." },
+ it("keeps the phone part and where the person is volatile, so a turn from the phone keeps the cached prefix", () => {
+ const at = (device: string, place: string) => buildSystemPrompt("You are Kiwi.", "", [
+ { id: "device", label: "Phone", text: device },
{ id: "device-place", label: "Where the person is", text: place },
]);
- expect(at(" On the phone.").stable).toBe(`You are Kiwi. The phone.${REPLY_HABITS_PROMPT}`);
- expect(at(" At the computer.").stable).toBe(at(" On the phone.").stable);
- expect(at(" On the phone.").volatile).toBe(" On the phone.");
+ // The phone part's text follows the turn's sender (may-use vs withheld), so it rides the volatile half with the place.
+ expect(at(" The phone.", " On the phone.").stable).toBe(`You are Kiwi.${REPLY_HABITS_PROMPT}`);
+ expect(at(" The phone.", " At the computer.").stable).toBe(at(" The phone.", " On the phone.").stable);
+ expect(at(" Not available here.", " On the phone.").stable).toBe(at(" The phone.", " On the phone.").stable);
+ expect(at(" The phone.", " On the phone.").volatile).toBe(" The phone. On the phone.");
});
it("has an empty volatile half when nothing mid-conversation is present", () => {
diff --git a/server/system-prompt.ts b/server/system-prompt.ts
index b55c843a36..b45f4342ed 100644
--- a/server/system-prompt.ts
+++ b/server/system-prompt.ts
@@ -47,7 +47,7 @@ export function heavyPrompt(sections: readonly PromptSection[]): boolean {
// runs of a bot and not others) and an automation run's reply rule ("automation-reply"), which otherwise makes every
// routine run write a new cached prefix.
const VOLATILE_SECTIONS = new Set(["memory", "mentions", "outstanding", "teammate-questions", "long-waits", "recent", "availability", "active-routines",
- "plan", "assignment", "routine-execution", "automation-reply", "skill-instructions", "playbooks", "device-place", "files", "tool-bridge"]);
+ "plan", "assignment", "routine-execution", "automation-reply", "skill-instructions", "playbooks", "device", "device-place", "files", "tool-bridge"]);
/** In the stable half, so it is not sent again whenever availability changes, but no part of what a per-thread process is
* keyed on: a warm process gets a changed roster as a note instead of a relaunch (SendTurnInput.systemRoster). */
const ROSTER_SECTION = "roster";