Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Fix closed threads still showing as waiting on the person (#7079)
Ooi Pin Junf38101192fc9
diff --git a/server/host/person-waits.test.ts b/server/host/person-waits.test.tsindex 9f1e5adf81..236c1072b4 100644--- a/server/host/person-waits.test.ts+++ b/server/host/person-waits.test.ts@@ -96,6 +96,29 @@ describe("a conversation resting on the person", () => {expect(waits.waiting().map((wait) => wait.kind)).toEqual(["sign-in"]);});+ it("drops a closed thread's copied question from the inbox wait, and the same card waits again when it reopens", () => {+ const { store, waits, known, bot, thread, task } = fixture();+ const asked = store.appendMessage(thread, {+ role: "bot", kind: "options", text: "Which?",+ card: { title: "Which?", subtitle: "", options: [], requestId: "r1", requestType: "question" },+ automation: {+ routineId: "routine-1", runId: "run-1", name: "Weekly report", trigger: "schedule",+ executionThreadId: "exec-1", scheduledFor: 1, needsYou: true, copyOf: "original-card",+ },+ });+ expect(waits.waiting().map((wait) => wait.messageId)).toContain(asked.id);+ expect(task()).toMatchObject({ activity: "waiting-on-you", busy: false });+ store.setTaskClosedBy(bot.id, thread, { botId: bot.id, name: bot.name, at: Date.now() });+ expect(waits.waiting().map((wait) => wait.messageId)).not.toContain(asked.id);+ expect(task()).toMatchObject({ activity: "idle", busy: false });+ known.add(thread);+ waits.boot();+ expect(waits.waiting().map((wait) => wait.messageId)).not.toContain(asked.id);+ expect(task()).toMatchObject({ activity: "idle", busy: false });+ store.setTaskClosedBy(bot.id, thread, null);+ expect(waits.waiting().map((wait) => wait.messageId)).toContain(asked.id);+ });+it("shows a card asked in a room on the room's thread", () => {const { store, bot } = fixture();const room = store.createGroup("Launch", [bot.id]);diff --git a/server/host/person-waits.ts b/server/host/person-waits.tsindex 9eeae8d659..2d418affe9 100644--- a/server/host/person-waits.ts+++ b/server/host/person-waits.ts@@ -37,6 +37,8 @@ export function createPersonWaits(deps: PersonWaitsDeps) {/** What this conversation rests on: a card older than the Needs you window has been left, and no longer asks. */const restsOn = (threadId: string): PersonWait | undefined => {+ const owner = store.botByThread(threadId);+ if (owner && store.taskByThread(owner.id, threadId)?.closedBy) return undefined;const wait = restingOnPerson(store.activePath(threadId), Boolean(store.groupByThread(threadId)));return wait && now() - wait.at < NEEDS_YOU_WINDOW_MS ? wait : undefined;};@@ -111,6 +113,9 @@ export function createPersonWaits(deps: PersonWaitsDeps) {withCards.delete(change.threadId);resting.delete(change.threadId);} else if (change.type === "bot") {+ if (change.closedByChanged && change.threadId && withCards.has(change.threadId)) {+ refresh(change.threadId);+ }for (const [threadId, wait] of resting) if (store.taskByThread(change.botId, threadId)) show(threadId, wait);}});diff --git a/server/host/place-inbox.test.ts b/server/host/place-inbox.test.tsindex b86fab74c7..89cc0b3fa2 100644--- a/server/host/place-inbox.test.ts+++ b/server/host/place-inbox.test.ts@@ -25,6 +25,7 @@ function setup(over: Partial<PlaceInboxDeps> = {}, dataDir = mkdtempSync(join(tmlet serial = 0;const said = new Map<string, Said[]>();const cards: OpenCardRow[] = [];+ const closed = new Set<string>();const sent: Notification[] = [];const handed = new Map<string, string>();const guests = new Map<string, string>();@@ -37,7 +38,7 @@ function setup(over: Partial<PlaceInboxDeps> = {}, dataDir = mkdtempSync(join(tmconst store = {bot: (id: string) => bots[id],botByThread: (threadId: string) => (threadId.startsWith("eli") || threadId === "guest-chat" ? bots.eli : threadId.startsWith("mira") ? bots.mira : undefined),- taskByThread: (_botId: string, threadId: string) => (titles[threadId] ? { threadId, title: titles[threadId] } : undefined),+ taskByThread: (_botId: string, threadId: string) => (titles[threadId] ? { threadId, title: titles[threadId], ...(closed.has(threadId) ? { closedBy: { botId: "eli", name: "Eli", at: 1 } } : {}) } : undefined),groupByThread: (threadId: string) => rooms[threadId],appendMessage: (threadId: string, line: Omit<Said, "id" | "at">) => {const posted = { ...line, id: `m${++serial}`, at: clock };@@ -64,7 +65,7 @@ function setup(over: Partial<PlaceInboxDeps> = {}, dataDir = mkdtempSync(join(tmcards.push(row);return row;};- return { inbox, store, said, cards, sent, handed, guests, bots, listeners, ask, dataDir, tick: (ms: number) => { clock += ms; } };+ return { inbox, store, said, cards, closed, sent, handed, guests, bots, listeners, ask, dataDir, tick: (ms: number) => { clock += ms; } };}describe("finished work reaches the inbox once", () => {@@ -212,6 +213,20 @@ describe("the inbox of a place that is no team's", () => {expect(f.inbox.view(OWNER).rows.map((row) => [row.id, row.origin?.threadId])).toEqual([["copy-q", "eli-pricing"]]);});+ it("leaves a closed Results chat's copied question out of the inbox, and lists it again when the thread reopens", () => {+ const f = setup({ sees: (_auth, threadId) => threadId !== "eli-launch" });+ const original = f.ask("eli-launch", "q");+ f.cards.push({ ...original, threadId: "eli-pricing", id: "copy-q", at: original.at + 1, copyOf: original.id });+ expect(f.inbox.view(OWNER).rows.map((row) => [row.id, row.origin?.threadId])).toEqual([["copy-q", "eli-pricing"]]);+ expect(f.inbox.count(OWNER)).toBe(1);+ f.closed.add("eli-pricing");+ expect(f.inbox.view(OWNER).rows).toEqual([]);+ expect(f.inbox.count(OWNER)).toBe(0);+ f.closed.delete("eli-pricing");+ expect(f.inbox.view(OWNER).rows.map((row) => [row.id, row.origin?.threadId])).toEqual([["copy-q", "eli-pricing"]]);+ expect(f.inbox.count(OWNER)).toBe(1);+ });+it("drops a question the person wrote past, unless a running turn still waits on it", () => {const live = new Set<string>();const f = setup({ live: (_threadId, requestId) => live.has(requestId) });diff --git a/server/host/place-inbox.ts b/server/host/place-inbox.tsindex 3da4ebbdcd..88aea60f34 100644--- a/server/host/place-inbox.ts+++ b/server/host/place-inbox.ts@@ -155,7 +155,9 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {try { card = row.card ? JSON.parse(row.card) as OptionCardData : null; } catch { continue; }if (!card || !personWait({ id: row.id, at: row.at, kind: "options", card }) || !deps.sees(auth, row.threadId) || !deps.mayAnswer(auth, row.threadId, card.requestId!)) continue;if (overtaken(row.threadId, row.id) && !deps.live(row.threadId, card.requestId!)) continue;- const bot = (row.fromBotId ? store.bot(row.fromBotId) : undefined) ?? store.botByThread(row.threadId);+ const owner = store.botByThread(row.threadId);+ if (owner && store.taskByThread(owner.id, row.threadId)?.closedBy) continue;+ const bot = (row.fromBotId ? store.bot(row.fromBotId) : undefined) ?? owner;if (!bot) continue;const group = store.groupByThread(row.threadId);const guest = deps.guestOf(row.threadId);diff --git a/server/routes/internal-threads.test.ts b/server/routes/internal-threads.test.tsindex 9b0e4f6734..d6a2a1e01d 100644--- a/server/routes/internal-threads.test.ts+++ b/server/routes/internal-threads.test.ts@@ -10,7 +10,12 @@ const task = (threadId: string, title: string, openedBy?: unknown) => ({ threadIconst handoff = { id: "h-42", rootId: "h-1", parentId: "h-1", threadId: "t-ops-new", botId: "bot-ops", status: "queued" } as HandoffView;const tasks: Record<string, TaskRecord[]> = {"bot-lead": [task("t-lead", "Lead main")],- "bot-ops": [task("t-ops-busy", "Weekly report"), task("t-ops-new", "Export CSV", { botId: "bot-lead", name: "Lead", at: 5 })],+ "bot-ops": [+ task("t-ops-busy", "Weekly report"),+ task("t-ops-new", "Export CSV", { botId: "bot-lead", name: "Lead", at: 5 }),+ { ...task("t-ops-closed", "Closed question"), activity: "waiting-on-you", closedBy: { botId: ops.id, name: ops.name, at: 2 } },+ { ...task("t-ops-open", "Open question"), activity: "waiting-on-you" },+ ],};async function listThreads(from: BotRecord) {@@ -40,6 +45,12 @@ describe("list_threads on a queued handoff", () => {expect(running).toMatchObject({ state: "running" });expect(running).not.toHaveProperty("waiting");});++ it("reports a closed thread as closed even when its activity still says it waits on the person", async () => {+ const rows = await listThreads(ops);+ expect(rows.find((thread) => thread.threadId === "t-ops-closed")).toMatchObject({ state: "closed" });+ expect(rows.find((thread) => thread.threadId === "t-ops-open")).toMatchObject({ state: "waiting-on-you" });+ });});describe("list_threads past the first 100", () => {diff --git a/server/routes/internal-threads.ts b/server/routes/internal-threads.tsindex 87ec4561a3..1bce86e815 100644--- a/server/routes/internal-threads.ts+++ b/server/routes/internal-threads.ts@@ -43,7 +43,7 @@ export function createInternalThreadRoutes(deps: {}const rows: ThreadRow[] = [];const stateOf = (bot: BotRecord, task: TaskRecord) => {- if (task.activity === "waiting-on-you" || coordinator.awaitingAnswer(task.threadId)) return "waiting-on-you" as const;+ if ((!task.closedBy && task.activity === "waiting-on-you") || coordinator.awaitingAnswer(task.threadId)) return "waiting-on-you" as const;if (deps.threadBusy(bot.id, task.threadId)) return "running" as const;if (coordinator.parkedDirect(task.threadId)) return "waiting-on-you" as const;if (deps.queuedThreadPosition(bot.id, task.threadId) !== null) return "queued" as const;diff --git a/server/store.ts b/server/store.tsindex e345bd4272..bfe059373d 100644--- a/server/store.ts+++ b/server/store.ts@@ -127,7 +127,7 @@ export type StoreChange =| { type: "message.patch"; threadId: string; message: Message }| { type: "thread"; threadId: string; activeLeafId: string }| { type: "thread.deleted"; threadId: string }- | { type: "bot"; botId: string; /** Only this thread of the bot changed, besides the bot's own fields. */ threadId?: string }+ | { type: "bot"; botId: string; /** Only this thread of the bot changed, besides the bot's own fields. */ threadId?: string; closedByChanged?: true }| { type: "bot.deleted"; botId: string }/** The fleet or the rooms were reordered as a whole; the broadcast carries every id in its new place. */| { type: "bots.order" } | { type: "groups.order" }@@ -2192,7 +2192,7 @@ export class Store {if (closedBy) task.closedBy = structuredClone(closedBy);else delete task.closedBy;});- this.emit({ type: "bot", botId });+ this.emit({ type: "bot", botId, threadId, closedByChanged: true });return task;}