Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Sign-in resume: an app sign-in after a refusal the status never read is a new sign-in; no sign-in told from an inconclusive read
laijsg1c4700ee96f40
diff --git a/server/harness/registry.test.ts b/server/harness/registry.test.tsindex ae98e1a86c..55a596d07e 100644--- a/server/harness/registry.test.ts+++ b/server/harness/registry.test.ts@@ -250,6 +250,10 @@ describe("ProviderRegistry", () => {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;+ authenticated = false;+ await read();await registry.dispose("a");await registry.load({ a: { driver: "fake" } });authenticated = true;diff --git a/server/harness/registry.ts b/server/harness/registry.tsindex 2ebc6dfe77..81083edad9 100644--- a/server/harness/registry.ts+++ b/server/harness/registry.ts@@ -642,7 +642,7 @@ export class ProviderRegistry {const kept = this.lastAuth.get(instance.instanceId), knew = kept ? kept.authenticated : before?.authenticated;const newest = !kept || kept.read < begun;if (newest && !inconclusive && snapshot.state === "available" && typeof snapshot.authenticated === "boolean") this.lastAuth.set(instance.instanceId, { authenticated: snapshot.authenticated, read: begun });- if (newest && knew === false && snapshot.state === "available" && snapshot.authenticated === true) {+ if (newest && !inconclusive && knew === false && snapshot.state === "available" && snapshot.authenticated === true) {for (const listener of this.signedInListeners) listener(instance.instanceId);}}diff --git a/server/host/sign-in-resume.test.ts b/server/host/sign-in-resume.test.tsindex e35fccb5ca..2f808ced68 100644--- a/server/host/sign-in-resume.test.ts+++ b/server/host/sign-in-resume.test.ts@@ -411,11 +411,15 @@ describe("createSignInResume", () => {await resume.resumeSaved();expect(w.sent.map(({ message }) => message.id)).toEqual([kai.asked.id]);expect(w.noticeTool("t-kai", kai.notice.id)?.nameCode).toBe("chat.notice.signedInAgain");- writeFileSync(file, "[1, 2]");- expect(() => createSignInResume(w.deps)).not.toThrow();+ const logged: string[] = [];+ for (const damaged of ["[1, 2]", "{not json"]) {+ writeFileSync(file, damaged);+ expect(() => createSignInResume({ ...w.deps, log: (line) => logged.push(line) })).not.toThrow();+ }+ expect(logged).toEqual([expect.stringContaining("not an object"), expect.stringContaining("could not read the saved file")]);});- it("takes the app's own report of a sign-in the status read already found for the same sign-in", async () => {+ function hostWorld() {const directory = mkdtempSync(join(tmpdir(), "omb-sign-in-resume-"));scratch.push(directory);const w = world();@@ -431,18 +435,31 @@ describe("createSignInResume", () => {onSignedIn: (listener) => { listeners.push(listener); },sawSignedIn: () => { const known = readSignedIn; readSignedIn = true; return known; },},- startTurn: async (_botId, text, opts) => { w.sent.push({ wait: { threadId: opts.threadId } as SignInWait, message: { text } as Message }); },+ startTurn: async (_botId, text, opts) => {+ const sent = w.row(opts.threadId, { role: "user", kind: "text", text });+ w.sent.push({ wait: { threadId: opts.threadId } as SignInWait, message: sent });+ return sent;+ },replyTarget: () => undefined,});+ const statusRead = () => {+ readSignedIn = true;+ listeners.forEach((listener) => listener("claude"));+ };+ return { w, host, takeover, statusRead };+ }++ 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);- readSignedIn = true;- listeners.forEach((listener) => listener("claude"));+ statusRead();await vi.waitFor(() => expect(w.sent).toHaveLength(1));w.tick(5);- const resent = w.row("t-kai", { role: "user", kind: "text", text: "Draft the plan" });- await host.waiting({ ...kai.wait, messageId: resent.id, failedAt: 1_015 });+ 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));@@ -450,6 +467,17 @@ describe("createSignInResume", () => {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);+ const kai = w.stuck("t-kai");+ host.waiting(kai.wait);+ w.tick(10);+ host.appSignedIn("claude");+ await vi.waitFor(() => 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 () => {const directory = mkdtempSync(join(tmpdir(), "omb-sign-in-resume-"));scratch.push(directory);diff --git a/server/host/sign-in-resume.ts b/server/host/sign-in-resume.tsindex 6c83caee78..3f22c3fa4f 100644--- a/server/host/sign-in-resume.ts+++ b/server/host/sign-in-resume.ts@@ -87,14 +87,23 @@ export function createSignInResume(deps: SignInResumeDeps) {};if (deps.file) {+ let saved: unknown;try {- const saved = JSON.parse(readFileSync(deps.file, "utf8")) as unknown;- const rows = (key: keyof Saved) => saved && typeof saved === "object" && Array.isArray((saved as Saved)[key]) ? (saved as Record<string, unknown[]>)[key]! : [];+ saved = JSON.parse(readFileSync(deps.file, "utf8"));+ } catch (error) {+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") log(`sign-in resume: could not read the saved file: ${error instanceof Error ? error.message : String(error)}`);+ }+ if (saved !== undefined && (!saved || typeof saved !== "object" || Array.isArray(saved))) log("sign-in resume: the saved file is not an object; left out");+ else if (saved) {+ const rows = (key: keyof Saved) => {+ const value = (saved as Record<string, unknown>)[key];+ return Array.isArray(value) ? value : [];+ };waiting = rows("waiting").filter(savedWait);notices = rows("notices").filter(savedNotice);const dropped = rows("waiting").length + rows("notices").length - waiting.length - notices.length;if (dropped) log(`sign-in resume: left out ${dropped} unreadable saved rows`);- } catch {}+ }prune();}@@ -267,22 +276,40 @@ export function createSignInResumeHost(deps: Omit<SignInResumeDeps, "file" | "siconst stands = () => Boolean(instance) && registry.get(instanceId) === instance;return stands() && snapshot?.state === "available" && snapshot.authenticated === true ? stands : null;},- 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 } : {}),- }),+ 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);+ },});+ 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(signedIn);+ registry.onSignedIn((instanceId) => {+ statusToldAt.set(instanceId, now());+ signedIn(instanceId);+ });void Promise.resolve(deps.startupRead).then(() => resume.resumeSaved());- /** A sign-in made in the app is one sign-in with the status read that finds it, whichever comes first. */+ /** 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. */const appSignedIn = (instanceId: string): void => {- if (registry.sawSignedIn(instanceId)) deps.takeover.signedIn(instanceId);+ const told = registry.sawSignedIn(instanceId) && (statusToldAt.get(instanceId) ?? -Infinity) > (refusedAt.get(instanceId) ?? -Infinity);+ if (told) deps.takeover.signedIn(instanceId);else signedIn(instanceId);};- return { ...resume, signedIn, appSignedIn };+ 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);+ };+ return { ...resume, waiting, signedIn, appSignedIn };}