Skip to content

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

fix(coordinate-bots): another run's late answer is not this run's refusal

Flex Builder2cb41575049a
diff --git a/server/host/refused-handoffs.test.ts b/server/host/refused-handoffs.test.ts
index 65a527e0fc..ef19a60681 100644
--- a/server/host/refused-handoffs.test.ts
+++ b/server/host/refused-handoffs.test.ts
@@ -84,6 +84,27 @@ describe("a routine's refused hand-off", () => {
expect(later, "the run that started since never made that call").toBeNull();
});
+ it("keeps a refusal when another run's delayed refusal for the same bot settles after it", () => {
+ const { refused, lines, now } = fixture({ writer: "p_olive", asked: "p_mara" });
+ now(r1);
+ refused.settled("t", rooms, ["Bookkeeping"], 403, r1);
+ now(r1);
+ refused.settled("t", rooms, ["Bookkeeping"], 403, r2);
+ expect(refused.take("t"), "the run that was refused still says so").toBe(SAID);
+ expect(lines.map((line) => line.to), "and its writer still hears it, once").toEqual(["p_olive"]);
+ expect(refused.take("t"), "the run that no longer owns the conversation says nothing").toBeNull();
+ expect(lines).toHaveLength(1);
+ });
+
+ it("keeps a refusal when another run's delayed success for the same bot settles after it", () => {
+ const { refused, lines, now } = fixture({ writer: "p_olive", asked: "p_mara" });
+ now(r1);
+ refused.settled("t", rooms, ["Bookkeeping"], 403, r1);
+ refused.settled("t", rooms, ["books"], 202, r2);
+ expect(refused.take("t"), "another run reaching Bookkeeping did not reach it for this one").toBe(SAID);
+ expect(lines.map((line) => line.to)).toEqual(["p_olive"]);
+ });
+
it("tells who wrote the routine in their inbox when the bot's failures reach someone else, once for the same words", () => {
const { refused, lines } = fixture({ writer: "p_olive", asked: "p_mara" });
refused.settled("t", rooms, ["Bookkeeping"], 403, r1);
diff --git a/server/host/refused-handoffs.ts b/server/host/refused-handoffs.ts
index a541eaaa5d..697f9687e6 100644
--- a/server/host/refused-handoffs.ts
+++ b/server/host/refused-handoffs.ts
@@ -7,7 +7,9 @@ import type { Delivery } from "./person-inbox.ts";
/** The run that made a call, as it was before the call went out: a call can outlive it. */
type Run = { id: string; routineId: string; botId: string };
-type Refused = { target: RosterMember; from: RosterMember; run: string };
+type Refused = { target: RosterMember; from: RosterMember };
+/** Who each run was refused by, per run and per bot: another run's late answer is never this run's. */
+type Refusals = Map<string, Map<string, Refused>>;
export interface RefusedHandoffDeps {
bots(): readonly RosterMember[];
@@ -28,7 +30,7 @@ export interface RefusedHandoffDeps {
}
export function createRefusedHandoffs(deps: RefusedHandoffDeps) {
- const refused = new Map<string, Map<string, Refused>>();
+ const refused = new Map<string, Refusals>();
const toldWriter = new Map<string, string>();
function words(threadId: string, seen: readonly Refused[]): string {
@@ -41,18 +43,24 @@ export function createRefusedHandoffs(deps: RefusedHandoffDeps) {
/** A coordinate_bots call came back: remembers the bots the run that made it could not reach, and forgets one it has reached since. */
settled(threadId: string, from: RosterMember, named: readonly string[], status: number, run: Run | undefined): void {
if (!run) return;
- const seen = refused.get(threadId) ?? new Map<string, Refused>();
+ // The run must still hold this conversation: another run's answer is not this one's.
+ if (deps.runOf(threadId)?.id !== run.id) return;
+ const byRun = refused.get(threadId) ?? new Map<string, Map<string, Refused>>();
+ const mine = byRun.get(run.id) ?? new Map<string, Refused>();
for (const raw of named) {
const target = status >= 400 ? unreachableTeammate(deps.bots(), from, raw, (bot) => deps.hides?.(from, bot) === true) : null;
- if (target) seen.set(target.id, { target, from, run: run.id });
- if (status < 400) for (const [id, was] of seen) if (namesTeammate(was.target, raw)) seen.delete(id);
+ if (target) mine.set(target.id, { target, from });
+ if (status < 400) for (const [id, was] of mine) if (namesTeammate(was.target, raw)) mine.delete(id);
}
- if (seen.size) refused.set(threadId, seen);
+ if (mine.size) byRun.set(run.id, mine);
+ else byRun.delete(run.id);
+ if (byRun.size) refused.set(threadId, byRun);
else refused.delete(threadId);
},
/** The run in this conversation is ending well: what it must say instead, once; null when nothing was refused, or another run was. */
take(threadId: string): string | null {
- const run = deps.runOf(threadId), seen = [...refused.get(threadId)?.values() ?? []].filter((was) => was.run === run?.id);
+ const run = deps.runOf(threadId), byRun = refused.get(threadId), seen = [...(run ? byRun?.get(run.id)?.values() ?? [] : [])];
+ // Another run's records here belong to a run that no longer holds the thread, so they can never be said.
refused.delete(threadId);
if (!seen.length) return null;
const said = words(threadId, seen);