Skip to content

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

fix(waits): recheck handed-on work right before storing its wait

Rutvij97a2acb1d16235
diff --git a/server/routes/waits.test.ts b/server/routes/waits.test.ts
index 2f75e9a550..fd8026a5b2 100644
--- a/server/routes/waits.test.ts
+++ b/server/routes/waits.test.ts
@@ -233,6 +233,20 @@ describe("the waits route", () => {
expect(await call("POST", "/api/internal/waits", FILE_WAIT, { sourceRefusal, assignmentId: () => (reads++ === 0 ? "a1" : undefined) })).toMatchObject({ status: 403, payload: { error: WAIT_MESSAGES.notPerson } });
expect(waits.create).not.toHaveBeenCalled();
});
+ it("has the manager ask once more, right before storing, whether the handed-on work still runs", async () => {
+ waits.create.mockClear();
+ let current: string | undefined = "a1";
+ await call("POST", "/api/internal/waits", FILE_WAIT, { assignmentId: () => current });
+ const { stillAllowed } = waits.create.mock.calls[0]![0];
+ expect(stillAllowed?.()).toBeNull();
+ current = undefined;
+ expect(stillAllowed?.()).toBe(WAIT_MESSAGES.notPerson);
+ current = "a2";
+ expect(stillAllowed?.()).toBe(WAIT_MESSAGES.notPerson);
+ waits.create.mockClear();
+ await call("POST", "/api/internal/waits", FILE_WAIT);
+ expect(waits.create.mock.calls[0]![0]).not.toHaveProperty("stillAllowed");
+ });
it("offers files and programs only on the bot's own computer", () => {
const own = { cloudHome: false, hosted: false, guest: false, place: null };
for (const place of [null, "local", "browser"] as const) expect(hostWaitRefusal({ ...own, place })).toBeNull();
diff --git a/server/routes/waits.ts b/server/routes/waits.ts
index 1bc7f7b565..e4ed99c5d5 100644
--- a/server/routes/waits.ts
+++ b/server/routes/waits.ts
@@ -178,7 +178,11 @@ export interface WaitRouteContext {
elsewhere?: (name: string) => { where: string } | { hint: string } | null;
}
-/** `sleeps`: the home is a personal My Cloud, whose plan sleeps when idle; a Team Cloud never sleeps. */
+/**
+ * Routes for listing, making and stopping a bot's waits. `sleeps`: the home is a personal My Cloud, whose plan
+ * sleeps when idle; a Team Cloud never sleeps. Work a bot handed on is read once, before the body, and must still
+ * be running after the body is read and again right before the wait is stored.
+ */
export function createWaitRoutes(deps: { waits: Pick<WaitManager, "create" | "list" | "cancel">; home?: () => string; cloudHome?: boolean; sleeps?: boolean }) {
const home = deps.home ?? homedir;
return async (ctx: WaitRouteContext): Promise<typeof PASS | void> => {
@@ -239,7 +243,7 @@ export function createWaitRoutes(deps: { waits: Pick<WaitManager, "create" | "li
condition,
...(request.followUp ? { followUp: request.followUp } : {}),
hours: request.hours,
- ...(assignmentId ? { assignmentId } : {}),
+ ...(assignmentId ? { assignmentId, stillAllowed: () => (ctx.assignmentId?.() === assignmentId ? null : WAIT_MESSAGES.notPerson) } : {}),
});
if (result.state === "refused") return void json(res, result.status, { error: result.error });
if (result.state === "already") return void json(res, 200, { text: result.text });
diff --git a/server/waits.test.ts b/server/waits.test.ts
index beb34c6307..24daf3adbd 100644
--- a/server/waits.test.ts
+++ b/server/waits.test.ts
@@ -108,6 +108,23 @@ describe("WaitManager", () => {
expect(await handed("a1")).toMatchObject({ state: "created" });
});
+ it("stores no handed-on wait when the work ends while its first check is still running", async () => {
+ const h = harness();
+ let open!: () => void, running = true;
+ h.pageGate = new Promise((resolve) => { open = resolve; });
+ const pending = h.manager.create({ botId: "bot-1", threadId: "t-1", label: "the page", condition: { kind: "url_contains", url: "https://example.com/", text: "done" }, assignmentId: "a1", stillAllowed: () => (running ? null : "ended") });
+ await vi.waitFor(() => expect(h.pageCalls).toBe(1));
+ running = false;
+ open();
+ expect(await pending).toEqual({ state: "refused", status: 403, error: "ended" });
+ expect(h.manager.list("bot-1")).toEqual([]);
+ expect(h.showCard).not.toHaveBeenCalled();
+ h.pageGate = null;
+ const kept = await h.manager.create({ botId: "bot-1", threadId: "t-1", label: "the report", condition: { kind: "file_exists", path: "/r.txt" }, assignmentId: "a2", stillAllowed: () => null });
+ expect(kept).toMatchObject({ state: "created", wait: { assignmentId: "a2" } });
+ expect(h.waitOf((kept as { wait: WaitRecord }).wait.id)).not.toHaveProperty("stillAllowed");
+ });
+
it("lets handed-on work set a new wait once its wait has ended, even before the ending is posted", async () => {
const h = harness();
const handed = () => h.manager.create({ botId: "bot-1", threadId: "t-1", label: "the report", condition: { kind: "file_exists", path: "/r.txt" }, assignmentId: "a1" });
diff --git a/server/waits.ts b/server/waits.ts
index a8a8c44ebd..3384821e97 100644
--- a/server/waits.ts
+++ b/server/waits.ts
@@ -48,11 +48,15 @@ export interface WaitRecord {
/** Decided and saved first; posted once its chat is free. */
ending?: WaitNotice;
}
-export interface NewWait { botId: string; threadId: string; label: string; condition: NewWaitCondition; followUp?: string; hours?: number; assignmentId?: string }
+export interface NewWait {
+ botId: string; threadId: string; label: string; condition: NewWaitCondition; followUp?: string; hours?: number; assignmentId?: string;
+ /** Asked again right before the wait is stored, after the checks `create` awaits; a refusal text stores nothing, so work stopped meanwhile leaves no wait behind. Never saved. */
+ stillAllowed?: () => string | null;
+}
export type CreateWaitResult =
| { state: "created"; wait: WaitRecord; note?: string }
| { state: "already"; text: string }
- | { state: "refused"; status: 400 | 409; error: string };
+ | { state: "refused"; status: 400 | 403 | 409; error: string };
export interface WaitProbes { page(url: string): Promise<PageResult>; stamp(path: string): Promise<FileStamp | null>; processes(): Promise<ProcessEntry[]> }
export interface WaitManagerOptions {
file?: string; now?: () => number; probes?: Partial<WaitProbes>;
@@ -175,6 +179,11 @@ export class WaitManager {
}
}
+ /**
+ * Makes a wait once its first check shows the condition is not met yet. Limits are checked before
+ * and after that check, and `input.stillAllowed` last of all, since the check can take seconds and
+ * the work that asked for the wait can end meanwhile.
+ */
async create(input: NewWait): Promise<CreateWaitResult> {
const full = this.limitRefusal(input);
if (full) return full;
@@ -186,6 +195,8 @@ export class WaitManager {
if (verdict.met) return { state: "already", text: `${describeCondition(condition)} already. Nothing was set up: tell the person now.` };
const crowded = this.limitRefusal(input);
if (crowded) return crowded;
+ const ended = input.stillAllowed?.();
+ if (ended) return { state: "refused", status: 403, error: ended };
const now = this.now();
const wait: WaitRecord = {
id: randomUUID(),