Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Teammate calls: a non-JSON 401 or 404 stays bad_answer; say why a name with two addresses was unreachable
Bugfixer 218e50ae42fe8
diff --git a/server/remote-teammates.test.ts b/server/remote-teammates.test.tsindex cf47f7ae87..b48b0f6db2 100644--- a/server/remote-teammates.test.ts+++ b/server/remote-teammates.test.ts@@ -168,10 +168,29 @@ describe("calling a teammate's Bot API", () => {expect(await readRemote(far, "t1")).toEqual({ ok: false, problem: "rate_limited", status: 429, retryAfterSeconds: 7 });next = { status: 202, body: "<html>proxy</html>" };expect(await readRemote(far, "t1")).toEqual({ ok: false, problem: "bad_answer", status: 202 });+ next = { status: 409, body: "busy" };+ expect(await readRemote(far, "t1")).toEqual({ ok: false, problem: "busy", status: 409 });+ });++ it("never reads an auth wall's or a proxy's page as the Bot API's wrong key or missing thread", async () => {+ let status = 401;+ const base = await listen((_req, res) => { res.writeHead(status, { "content-type": "text/html" }); res.end("<html>Sign in</html>"); });+ expect(await readRemote(teammate(base), "t1")).toEqual({ ok: false, problem: "bad_answer", status: 401 });+ status = 404;+ expect(await readRemote(teammate(base), "t1")).toEqual({ ok: false, problem: "bad_answer", status: 404 });+ });++ it("says why a name could not be reached when it has more than one address", async () => {+ const base = await listen((_req, res) => res.end("{}"));+ const closed = servers.pop()!;+ await new Promise<void>((done) => closed.close(() => done()));+ const result = await readRemote(teammate(base.replace("127.0.0.1", "localhost")), "t1");+ expect(result).toMatchObject({ ok: false, problem: "unreachable" });+ expect((result as { detail?: string }).detail).toMatch(/ECONNREFUSED/);});it("never connects to a name that resolves to a link-local or metadata address", async () => {- for (const address of ["169.254.169.254", "fe80::1", "fd00:ec2::254", "::ffff:169.254.169.254"]) expect(linkLocalAddress(address)).toBe(true);+ for (const address of ["169.254.169.254", "fe80::1", "fd00:ec2::254", "::ffff:169.254.169.254", "64:ff9b::a9fe:a9fe", "::a9fe:a9fe"]) expect(linkLocalAddress(address)).toBe(true);for (const address of ["127.0.0.1", "100.64.1.2", "192.168.1.5", "::1", "not an address"]) expect(linkLocalAddress(address)).toBe(false);const lookupWith = (answer: Array<{ address: string; family: number }>) => {const resolve = ((_host: string, options: { all?: boolean }, callback: (...args: unknown[]) => void) => {diff --git a/server/remote-teammates.ts b/server/remote-teammates.tsindex a7d92a611c..14a9bb5755 100644--- a/server/remote-teammates.ts+++ b/server/remote-teammates.ts@@ -37,6 +37,9 @@ const LINK_LOCAL_ADDRESSES = new BlockList();LINK_LOCAL_ADDRESSES.addSubnet("169.254.0.0", 16, "ipv4");LINK_LOCAL_ADDRESSES.addSubnet("fe80::", 10, "ipv6");LINK_LOCAL_ADDRESSES.addAddress("fd00:ec2::254", "ipv6");+// 169.254/16 inside IPv6: NAT64's 64:ff9b::/96 and the old IPv4-compatible ::/96.+LINK_LOCAL_ADDRESSES.addSubnet("64:ff9b::a9fe:0", 112, "ipv6");+LINK_LOCAL_ADDRESSES.addSubnet("::a9fe:0", 112, "ipv6");export function linkLocalAddress(address: string): boolean {const family = isIP(address);@@ -245,8 +248,7 @@ async function callRemote(teammate: RemoteTeammate, query: string, init: Request});} catch (error) {options.signal?.throwIfAborted();- // fetch says only "fetch failed"; why it failed (refused, no such name, a link-local address) is its cause.- const reason = error instanceof Error ? (error.cause instanceof Error ? error.cause.message : error.message) : undefined;+ const reason = failureReason(error);return { ok: false, problem: "unreachable", detail: timeout.aborted ? "no answer in time" : reason === undefined ? undefined : clip(reason, DETAIL_CHARS) };}let body: unknown;@@ -255,8 +257,9 @@ async function callRemote(teammate: RemoteTeammate, query: string, init: Request} catch {options.signal?.throwIfAborted();if (timeout.aborted) return { ok: false, problem: "unreachable", detail: "no answer in time" };- // Only a 200 or 202 must be the Bot API's JSON. A proxy's 502 page or a plain-text 429 is still that status.- if (response.status === 200 || response.status === 202) return { ok: false, problem: "bad_answer", status: response.status };+ // Busy, rate limited or failing means the same whatever the body: a proxy's 502 page, a plain-text 429. Any other+ // answer that isn't JSON (an auth wall's 401 page, a proxy's 404) is not the Bot API's, so says nothing of the key.+ if (response.status !== 409 && response.status !== 429 && response.status < 500) return { ok: false, problem: "bad_answer", status: response.status };body = undefined;}if (response.status === 200 || response.status === 202) {@@ -272,6 +275,16 @@ async function callRemote(teammate: RemoteTeammate, query: string, init: Request};}+/** fetch says only "fetch failed"; why (refused, no such name, a link-local address) is its cause. A name with more+ * than one address fails with an AggregateError whose message is empty: then its first error or its code says why. */+function failureReason(error: unknown): string | undefined {+ if (!(error instanceof Error)) return undefined;+ const cause = error.cause as (Error & { code?: unknown; errors?: unknown[] }) | undefined;+ if (!(cause instanceof Error)) return error.message;+ const first = cause.errors?.[0];+ return cause.message || (first instanceof Error ? first.message : undefined) || (typeof cause.code === "string" ? cause.code : undefined) || error.message;+}+const PROBLEM_BY_STATUS: Record<number, RemoteProblem> = {400: "bad_request", 401: "wrong_key", 403: "refused", 404: "no_thread", 409: "busy", 413: "bad_request", 415: "bad_request", 429: "rate_limited",};