Skip to content

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

fix(inbox): discard stale assistant answers

Sebastian Bc07be2695465
diff --git a/server/host/person-inbox.test.ts b/server/host/person-inbox.test.ts
index e6f10a3104..04581ca9a4 100644
--- a/server/host/person-inbox.test.ts
+++ b/server/host/person-inbox.test.ts
@@ -14,6 +14,96 @@ const ask = (botId: string, botName: string, threadId: string, kind: Notificatio
({ kind, botId, botName, threadId, title: `${botName} needs approval`, body: "Run: rm -rf ./build" });
const card = (requestId: string) => ({ title: "Run a command?", subtitle: "rm -rf ./build", options: ["Allow", "Deny"], requestId, tool: "Bash", requestType: "permission" as const });
+describe("pending assistant answers", () => {
+ const pending = async (destination: "bot" | "person" | "room" = "bot") => {
+ let started!: () => void, finish!: (answer: string) => void;
+ const generating = new Promise<void>((resolve) => { started = resolve; });
+ const result = new Promise<string>((resolve) => { finish = resolve; });
+ const f = fixture({ representative: { busy: async () => [], timeZone: () => "UTC", generate: async () => { started(); return result; } } });
+ if (destination === "person") f.said("scout-main", "@Marco are you free tomorrow?", PHILIPP);
+ else expect(f.team.pass("scout", destination === "room" ? "room-thread" : "philipp-private", "Marco", "Are you free tomorrow?").status).toBe(200);
+ await generating;
+ const inbox = f.inboxOf(MARCO).threadId;
+ const row = f.lines(inbox)[0]!;
+ const complete = async (answer = '{"answer":"Marco is free tomorrow."}') => {
+ finish(answer);
+ await new Promise<void>((resolve) => setImmediate(resolve));
+ };
+ return { f, inbox, row, complete };
+ };
+
+ it.each(['{"answer":"Marco is free tomorrow."}', '{"wait":true}'])("silently drops a result after the person replied (%s)", async (answer) => {
+ const { f, inbox, row, complete } = await pending();
+ expect(f.team.replyIn(session(MARCO.email), inbox, row.id, "I am busy tomorrow.", { sendId: "manual-1" })?.status).toBe(202);
+ await complete(answer);
+ expect(f.continued).toEqual([expect.objectContaining({ relay: expect.objectContaining({ kind: "reply" }), text: expect.stringContaining("I am busy tomorrow.") })]);
+ expect(f.lines(inbox)[0]!.inbox!.answers).toEqual([expect.objectContaining({ by: "person", text: "I am busy tomorrow." })]);
+ expect(f.notified).toEqual([]);
+ f.restart();
+ expect(f.team.inbox.waiting(inbox)).toEqual([]);
+ });
+
+ it("silently drops a result after the person's Chief relayed their answer", async () => {
+ const { f, inbox, row, complete } = await pending();
+ f.said(inbox, "Tell Philipp I am busy tomorrow.", MARCO);
+ expect(f.team.pass("marcos-chief", inbox, "Philipp", "I am busy tomorrow.").status).toBe(200);
+ await complete();
+ expect(f.continued).toHaveLength(1);
+ expect(f.lines(inbox).find((line) => line.id === row.id)!.inbox!.answers).toEqual([expect.objectContaining({ by: "person", text: "I am busy tomorrow." })]);
+ expect(f.notified).toEqual([]);
+ });
+
+ it.each(["person", "room"] as const)("does not send a stale answer to the asking %s", async (destination) => {
+ const { f, inbox, row, complete } = await pending(destination);
+ expect(f.team.replyIn(session(MARCO.email), inbox, row.id, "I am busy tomorrow.", {})?.status).toBe(202);
+ const before = f.lines(destination === "person" ? f.inboxOf(PHILIPP).threadId : "room-thread").slice();
+ const notified = f.notified.slice();
+ await complete();
+ expect(f.lines(destination === "person" ? f.inboxOf(PHILIPP).threadId : "room-thread")).toEqual(before);
+ expect(f.notified).toEqual(notified);
+ expect(f.lines(inbox)[0]!.inbox!.answers).toEqual([expect.objectContaining({ by: "person", text: "I am busy tomorrow." })]);
+ });
+
+ it.each(["done", "answered at origin", "forgotten", "deleted", "reassigned", "new starter", "stopped"])("silently drops a result when its inbox item is %s", async (change) => {
+ const { f, inbox, complete } = await pending();
+ const row = f.lines(inbox)[0]!;
+ if (change === "done") expect(f.team.inbox.markDone(key(MARCO.email), row.id)).toBe("done");
+ else if (change === "answered at origin") f.said("philipp-private", "I am busy tomorrow.", MARCO);
+ else if (change === "forgotten") f.team.inbox.forget(key(MARCO.email));
+ else if (change === "deleted") f.deleteThread(inbox);
+ else if (change === "reassigned") f.bots.find((bot) => bot.id === "marcos-chief")!.reportsTo = PHILIPP.email;
+ else if (change === "new starter") f.starters.set(inbox, key(PHILIPP.email));
+ else f.team.stop();
+ await complete();
+ expect(f.continued).toEqual([]);
+ expect(f.notified).toEqual([]);
+ expect(f.lines(inbox).find((line) => line.id === row.id)?.inbox?.answers).toBeUndefined();
+ });
+
+ it("preserves the person's correction made during the assistant answer's write event", async () => {
+ const { f, inbox, row, complete } = await pending();
+ let replied = false;
+ const unsubscribe = f.store.onChange((change) => {
+ if (change.type !== "message.patch" || change.message.id !== row.id || replied || change.message.inbox?.answers?.at(-1)?.by !== "assistant") return;
+ replied = true;
+ expect(f.team.replyIn(session(MARCO.email), inbox, row.id, "Actually, I am busy tomorrow.", { sendId: "correction-1" })?.status).toBe(202);
+ });
+ try {
+ await complete();
+ expect(replied).toBe(true);
+ expect(f.continued.map((one) => one.relay?.kind)).toEqual(["assistant", "reply"]);
+ expect(f.continued.at(-1)!.text).toContain("Actually, I am busy tomorrow.");
+ expect(f.lines(inbox)[0]!.inbox!.answers).toEqual([
+ expect.objectContaining({ by: "assistant", text: "Marco is free tomorrow." }),
+ expect.objectContaining({ by: "person", text: "Actually, I am busy tomorrow." }),
+ ]);
+ expect(f.notified).toEqual([]);
+ f.restart();
+ expect(f.team.inbox.view(key(MARCO.email))!.rows[0]!.answers?.at(-1)).toMatchObject({ by: "person", text: "Actually, I am busy tomorrow." });
+ } finally { unsubscribe(); }
+ });
+});
+
describe("asks routed to a person", () => {
it("copies an approval Marco owns into his inbox, sends the one notification from there, and answers the copy where it was asked", () => {
const f = fixture();
diff --git a/server/host/person-inbox.ts b/server/host/person-inbox.ts
index 3d0a2ddb9c..52d4021dd4 100644
--- a/server/host/person-inbox.ts
+++ b/server/host/person-inbox.ts
@@ -122,6 +122,7 @@ export function createPersonInbox(deps: PersonInboxDeps) {
const file = join(deps.dataDir, PERSON_INBOX_FILE);
let homes: Record<string, { botId: string; threadId: string }> = {};
let items: Item[] = [];
+ let stopped = false;
if (team) {
try {
const parsed = fileSchema.safeParse(JSON.parse(readFileSync(file, "utf8")));
@@ -316,7 +317,10 @@ export function createPersonInbox(deps: PersonInboxDeps) {
} catch (error) {
console.warn(`person inbox: the assistant could not answer: ${error instanceof Error ? error.message : String(error)}`);
}
+ if (stopped || !items.includes(item) || item.answered || item.done || settled(item)
+ || !deps.person(item.person) || personOf(item.threadId) !== item.person || deps.starters.get(item.threadId) !== item.person) return;
if (answer && answerItem(item, answer, "assistant").status === 200) {
+ if (line(item)?.inbox?.answers?.at(-1)?.by !== "assistant") return;
notifyItem(item, `${delivery.title} · your assistant answered`, delivery.notify, `${delivery.text} → ${answer}`);
} else notifyItem(item, delivery.title, delivery.notify, delivery.text);
}
@@ -512,7 +516,7 @@ export function createPersonInbox(deps: PersonInboxDeps) {
items = items.filter((item) => item.person !== person);
save();
},
- stop: unsubscribe,
+ stop: () => { stopped = true; unsubscribe(); },
};
}