Skip to content

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

Sign-in resume: every sign-in made in the app runs a pass; it only marks the registry so its status read does not tell it again

laijsg1ce1d4026a51e
diff --git a/server/harness/registry.test.ts b/server/harness/registry.test.ts
index 55a596d07e..e341b55516 100644
--- a/server/harness/registry.test.ts
+++ b/server/harness/registry.test.ts
@@ -244,14 +244,15 @@ describe("ProviderRegistry", () => {
};
await read();
authenticated = true;
- expect(registry.sawSignedIn("a"), "no read had found it signed in yet").toBe(false);
+ registry.sawSignedIn("a");
await read();
expect(heard, "the app told this sign-in itself").toEqual([]);
- expect(registry.sawSignedIn("a"), "the app reporting it again is the same sign-in").toBe(true);
authenticated = false;
await read();
- expect(registry.sawSignedIn("a"), "a read found it signed out since: a new sign-in").toBe(false);
authenticated = true;
+ await read();
+ expect(heard, "signed out and in again since: a new sign-in").toEqual(["a"]);
+ heard.length = 0;
authenticated = false;
await read();
await registry.dispose("a");
diff --git a/server/harness/registry.ts b/server/harness/registry.ts
index 81083edad9..ff1f4ffc57 100644
--- a/server/harness/registry.ts
+++ b/server/harness/registry.ts
@@ -655,12 +655,9 @@ export class ProviderRegistry {
this.signedInListeners.add(listener);
}
- /** A sign-in the app made and tells itself: the next read that finds it signed in tells nothing new. True when a read
- * had already found it signed in, so it was told then. */
- sawSignedIn(instanceId: InstanceId): boolean {
- const known = this.lastAuth.get(instanceId)?.authenticated === true;
+ /** A sign-in the app made and tells itself: the next read that finds it signed in tells nothing new. */
+ sawSignedIn(instanceId: InstanceId): void {
this.lastAuth.set(instanceId, { authenticated: true, read: ++this.readsBegun });
- return known;
}
private saveStatus(): void {
diff --git a/server/host/sign-in-resume.test.ts b/server/host/sign-in-resume.test.ts
index 2f808ced68..62c5f3a201 100644
--- a/server/host/sign-in-resume.test.ts
+++ b/server/host/sign-in-resume.test.ts
@@ -423,7 +423,7 @@ describe("createSignInResume", () => {
const directory = mkdtempSync(join(tmpdir(), "omb-sign-in-resume-"));
scratch.push(directory);
const w = world();
- let readSignedIn = false;
+ const sawSignedIn = vi.fn();
const listeners: Array<(instanceId: string) => void> = [];
const instance = { snapshot: async () => ({ state: "available" as const, authenticated: true }) };
const takeover = { signedIn: vi.fn() };
@@ -433,7 +433,7 @@ describe("createSignInResume", () => {
forgetSnapshots: () => {},
get: () => instance as never,
onSignedIn: (listener) => { listeners.push(listener); },
- sawSignedIn: () => { const known = readSignedIn; readSignedIn = true; return known; },
+ sawSignedIn,
},
startTurn: async (_botId, text, opts) => {
const sent = w.row(opts.threadId, { role: "user", kind: "text", text });
@@ -442,40 +442,19 @@ describe("createSignInResume", () => {
},
replyTarget: () => undefined,
});
- const statusRead = () => {
- readSignedIn = true;
- listeners.forEach((listener) => listener("claude"));
- };
- return { w, host, takeover, statusRead };
+ return { w, host, takeover, sawSignedIn, listeners };
}
- it("takes the app's own report of a sign-in the status read already found for the same sign-in", async () => {
- const { w, host, takeover, statusRead } = hostWorld();
- const kai = w.stuck("t-kai");
- host.waiting(kai.wait);
- w.tick(10);
- statusRead();
- await vi.waitFor(() => expect(w.sent).toHaveLength(1));
- w.tick(5);
- const resent = w.sent[0]!.message;
- w.row("t-kai", { tool: { name: "error: Not logged in · Please run /login", ok: false, setup: true } });
- await host.waiting({ ...kai.wait, messageId: resent.id, failedAt: w.deps.now!() });
- w.tick(10);
- host.appSignedIn("claude");
- await new Promise((resolve) => setTimeout(resolve, 10));
- expect(w.sent, "the app's report is the sign-in the status read already found").toHaveLength(1);
- expect(takeover.signedIn).toHaveBeenCalledTimes(2);
- });
-
- it("takes the app's sign-in for a new one when a message was refused after the status read last found it signed in", async () => {
- const { w, host, statusRead } = hostWorld();
- statusRead();
- w.tick(10);
+ it("runs a pass for a sign-in made in the app, and marks it so the status read that finds it tells nothing more", async () => {
+ const { w, host, takeover, sawSignedIn } = hostWorld();
const kai = w.stuck("t-kai");
- host.waiting(kai.wait);
+ host.waiting(kai.wait); host.noticed(kai.noticed);
w.tick(10);
host.appSignedIn("claude");
- await vi.waitFor(() => expect(w.sent.map(({ message }) => message.text)).toEqual(["Draft the plan"]));
+ expect(sawSignedIn).toHaveBeenCalledWith("claude");
+ expect(takeover.signedIn).toHaveBeenCalledWith("claude");
+ await vi.waitFor(() => expect(w.noticeTool("t-kai", kai.notice.id)?.nameCode).toBe("chat.notice.signedInAgain"));
+ expect(w.sent.map(({ message }) => message.text)).toEqual(["Draft the plan"]);
});
it("after a restart, asks the AI once and sends what was left waiting", async () => {
diff --git a/server/host/sign-in-resume.ts b/server/host/sign-in-resume.ts
index 3f22c3fa4f..b08fbd52ba 100644
--- a/server/host/sign-in-resume.ts
+++ b/server/host/sign-in-resume.ts
@@ -276,40 +276,22 @@ export function createSignInResumeHost(deps: Omit<SignInResumeDeps, "file" | "si
const stands = () => Boolean(instance) && registry.get(instanceId) === instance;
return stands() && snapshot?.state === "available" && snapshot.authenticated === true ? stands : null;
},
- retry: async (wait, message) => {
- const sent = await deps.startTurn(wait.botId, message.text ?? "", {
- threadId: wait.threadId, editedMessageId: message.id, dataContext: message.dataContext,
- ...(message.replyToId ? { replyTo: deps.replyTarget(wait.threadId, message.replyToId) } : {}),
- ...(wait.trigger ? { trigger: wait.trigger } : {}), ...(message.sender ? { sender: message.sender } : {}),
- });
- const id = (sent as { id?: unknown } | null | undefined)?.id;
- if (typeof id === "string") sentAgain.add(id);
- },
+ retry: (wait, message) => deps.startTurn(wait.botId, message.text ?? "", {
+ threadId: wait.threadId, editedMessageId: message.id, dataContext: message.dataContext,
+ ...(message.replyToId ? { replyTo: deps.replyTarget(wait.threadId, message.replyToId) } : {}),
+ ...(wait.trigger ? { trigger: wait.trigger } : {}), ...(message.sender ? { sender: message.sender } : {}),
+ }),
});
- const now = deps.now ?? Date.now;
- /** The messages a pass sent again: one refused again says nothing new about the AI's sign-in. */
- const sentAgain = new Set<string>();
- const statusToldAt = new Map<string, number>();
- const refusedAt = new Map<string, number>();
const signedIn = (instanceId: string): void => {
deps.takeover.signedIn(instanceId);
void resume.signedIn(instanceId);
};
- registry.onSignedIn((instanceId) => {
- statusToldAt.set(instanceId, now());
- signedIn(instanceId);
- });
+ registry.onSignedIn(signedIn);
void Promise.resolve(deps.startupRead).then(() => resume.resumeSaved());
- /** A sign-in made in the app is the one a status read already told only when that read came after the last refusal of a
- * message the person sent; a message sent again and refused again does not count. */
+ /** A sign-in made in the app is a sign-in, and the status read that later finds it does not tell it again. */
const appSignedIn = (instanceId: string): void => {
- const told = registry.sawSignedIn(instanceId) && (statusToldAt.get(instanceId) ?? -Infinity) > (refusedAt.get(instanceId) ?? -Infinity);
- if (told) deps.takeover.signedIn(instanceId);
- else signedIn(instanceId);
- };
- const waiting = (wait: SignInWait) => {
- if (!sentAgain.has(wait.messageId)) refusedAt.set(wait.selection.instanceId, Math.max(refusedAt.get(wait.selection.instanceId) ?? -Infinity, wait.failedAt));
- return resume.waiting(wait);
+ registry.sawSignedIn(instanceId);
+ signedIn(instanceId);
};
- return { ...resume, waiting, signedIn, appSignedIn };
+ return { ...resume, signedIn, appSignedIn };
}