@devries-cameron20 / t3code Mirror · read-only / commits / c6fd4ad1e4
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 22:00 UTC

fix(mobile): check for server updates without GitHub's API rate limit (#17900)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Jorge David committed Oct 20, 2026 at 01:06 UTC c6fd4ad1e40e68b63dc7690c9b6bdbc7a75a0d7f
3 files changed +131 -40
apps/mobile/src/features/settings/environment-maintenance.test.ts
+88 -25
@@ -159,46 +159,109 @@ describe("environment maintenance access", () => {
159
160 describe("environment release checks", () => {
161 const signal = new AbortController().signal;
162 + const feed = (...tags: ReadonlyArray<string>) =>
163 + new Response(
164 + `<feed><id>tag:github.com,2008:https://github.com/pingdotgg/t3code/releases</id>${tags
165 + .map((tag) => `<entry><id>tag:github.com,2008:Repository/1/${tag}</id></entry>`)
166 + .join("")}</feed>`,
167 + );
168
163 - it("keeps stable hosts on stable releases and ignores drafts", async () => {
169 + it("checks stable updates as JSON without using the anonymous API quota", async () => {
170 vi.stubGlobal(
171 "fetch",
166 - vi
167 - .fn()
168 - .mockImplementation(async () =>
169 - Response.json([
170 - { tag_name: "v2.0.0-nightly.20260923.1" },
171 - { tag_name: "v1.2.0", draft: true },
172 - { tag_name: "v1.1.0" },
173 - ]),
174 - ),
172 + vi.fn(async (url: string, init?: RequestInit) =>
173 + url === "https://github.com/pingdotgg/t3code/releases/latest" &&
174 + new Headers(init?.headers).get("Accept") === "application/json"
175 + ? Response.json({ tag_name: "v1.1.0" })
176 + : new Response(null, { status: 403 }),
177 + ),
178 );
179 expect(await findEnvironmentUpdate("1.0.0", signal)).toBe("1.1.0");
180 expect(await findEnvironmentUpdate("1.1.0", signal)).toBeNull();
181 expect(await findEnvironmentUpdate("1.3.0", signal)).toBeNull();
182 });
183
181 - it("walks release pages to find the host's channel", async () => {
182 - const fetchMock = vi
183 - .fn()
184 - .mockResolvedValueOnce(
185 - Response.json(Array.from({ length: 100 }, () => ({ tag_name: "v2.0.0" }))),
186 - )
187 - .mockResolvedValueOnce(Response.json([{ tag_name: "v1.0.0-preview.20260923.2" }]));
188 - vi.stubGlobal("fetch", fetchMock);
184 + it("keeps nightly and preview hosts on their channel without using the anonymous API quota", async () => {
185 + vi.stubGlobal(
186 + "fetch",
187 + vi.fn(async (url: string) =>
188 + url === "https://github.com/pingdotgg/t3code/releases.atom"
189 + ? feed(
190 + "desktop-preview",
191 + "v1.0.0-preview.20260924.3",
192 + "v1.0.0-nightly.20260924.2",
193 + "v1.0.0",
194 + "v1.0.0-nightly.20260923.1",
195 + )
196 + : new Response(null, { status: 403 }),
197 + ),
198 + );
199 + expect(await findEnvironmentUpdate("1.0.0-nightly.20260923.1", signal)).toBe(
200 + "1.0.0-nightly.20260924.2",
201 + );
202 + expect(await findEnvironmentUpdate("1.0.0-nightly.20260924.2", signal)).toBeNull();
203 + expect(await findEnvironmentUpdate("1.0.0-preview.20260923.1", signal)).toBe(
204 + "1.0.0-preview.20260924.3",
205 + );
206 + expect(await findEnvironmentUpdate("1.0.0-preview.20260925.1", signal)).toBeNull();
207 + });
208 +
209 + it("finds a published preview beyond the ten-entry feed and first API page", async () => {
210 + const nightlies = Array.from({ length: 10 }, (_, i) => `v1.0.0-nightly.20260930.${i}`);
211 + vi.stubGlobal(
212 + "fetch",
213 + vi.fn(async (url: string) => {
214 + if (url === "https://github.com/pingdotgg/t3code/releases.atom") return feed(...nightlies);
215 + if (url === "https://api.github.com/repos/pingdotgg/t3code/releases?per_page=100&page=1") {
216 + return Response.json(Array.from({ length: 100 }, () => ({ tag_name: "v2.0.0" })));
217 + }
218 + if (url === "https://api.github.com/repos/pingdotgg/t3code/releases?per_page=100&page=2") {
219 + return Response.json([
220 + { tag_name: "v1.0.0-preview.20260925.1", draft: true },
221 + { tag_name: "v1.0.0-preview.20260924.3" },
222 + ]);
223 + }
224 + throw new Error(`Unexpected release request: ${url}`);
225 + }),
226 + );
227 expect(await findEnvironmentUpdate("1.0.0-preview.20260923.1", signal)).toBe(
190 - "1.0.0-preview.20260923.2",
228 + "1.0.0-preview.20260924.3",
229 );
192 - expect(fetchMock.mock.calls[1]?.[0]).toContain("page=2");
193 - expect(fetchMock.mock.calls[1]?.[1]).toEqual({ signal });
230 });
231
232 it("reports failed checks instead of claiming the server is current", async () => {
197 - vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(null, { status: 403 })));
198 - await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow("403");
199 - vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json([])));
200 - await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow("No stable release");
233 + for (const status of [403, 429]) {
234 + vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(null, { status })));
235 + await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow(String(status));
236 + }
237 + vi.stubGlobal(
238 + "fetch",
239 + vi.fn(async (url: string) =>
240 + url.endsWith(".atom") ? feed("v1.0.0") : Response.json([{ tag_name: "v1.0.0" }]),
241 + ),
242 + );
243 + await expect(findEnvironmentUpdate("1.0.0-nightly.20260923.1", signal)).rejects.toThrow(
244 + "No nightly release",
245 + );
246 vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json({ error: "invalid" })));
247 await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow();
248 });
249 +
250 + it.each(["1.0.0", "1.0.0-nightly.20260923.1", "1.0.0-preview.20260923.1"])(
251 + "cancels the release request for %s, including the API fallback",
252 + async (currentVersion) => {
253 + const controller = new AbortController();
254 + const abort = new DOMException("Update check cancelled", "AbortError");
255 + vi.stubGlobal(
256 + "fetch",
257 + vi.fn(async (url: string, init?: RequestInit) => {
258 + if (currentVersion.includes("preview") && url.endsWith(".atom")) return feed("v1.0.0");
259 + controller.abort(abort);
260 + init?.signal?.throwIfAborted();
261 + return new Response(null, { status: 500 });
262 + }),
263 + );
264 + await expect(findEnvironmentUpdate(currentVersion, controller.signal)).rejects.toBe(abort);
265 + },
266 + );
267 });
apps/mobile/src/features/settings/environment-maintenance.ts
+42 -15
@@ -6,6 +6,7 @@ import {
6 type ServerProvider,
7 } from "@t3tools/contracts";
8 import {
9 + CLI_RELEASES_PAGE_URL,
10 cliReleaseChannelOf,
11 cliReleaseIndexPageUrl,
12 newestCliReleaseVersion,
@@ -45,24 +46,50 @@ export function canUpdateEnvironmentProvider(provider: ServerProvider) {
46 );
47 }
48
48 -const Releases = Schema.Array(
49 - Schema.Struct({
50 - tag_name: Schema.String,
51 - draft: Schema.optionalKey(Schema.Boolean),
52 - }),
53 -);
54 -const decodeReleases = Schema.decodeUnknownSync(Releases);
49 +const Release = Schema.Struct({
50 + tag_name: Schema.String,
51 + draft: Schema.optionalKey(Schema.Boolean),
52 +});
53 +const decodeRelease = Schema.decodeUnknownSync(Release);
54 +const decodeReleases = Schema.decodeUnknownSync(Schema.Array(Release));
55 +
56 +async function fetchReleases(url: string, accept: string, signal: AbortSignal) {
57 + const response = await fetch(url, { signal, headers: { Accept: accept } });
58 + if (!response.ok) throw new Error(`Could not check releases (${response.status}). Try again.`);
59 + return response;
60 +}
61
62 export async function findEnvironmentUpdate(currentVersion: string, signal: AbortSignal) {
63 const channel = cliReleaseChannelOf(currentVersion);
58 - for (let page = 1; ; page++) {
59 - const response = await fetch(cliReleaseIndexPageUrl(page), { signal });
60 - if (!response.ok) throw new Error(`Could not check releases (${response.status}). Try again.`);
64 + // Public release pages avoid the anonymous API quota shared by devices on the same network.
65 + const response = await fetchReleases(
66 + channel === "stable" ? `${CLI_RELEASES_PAGE_URL}/latest` : `${CLI_RELEASES_PAGE_URL}.atom`,
67 + channel === "stable" ? "application/json" : "application/atom+xml",
68 + signal,
69 + );
70 + const releases =
71 + channel === "stable"
72 + ? [decodeRelease(await response.json())]
73 + : Array.from(
74 + (await response.text()).matchAll(
75 + /<id>tag:github\.com,2008:Repository\/\d+\/([^<]+)<\/id>/g,
76 + ),
77 + ([, tag_name = ""]) => ({ tag_name }),
78 + );
79 + let version = newestCliReleaseVersion(releases, channel);
80 +
81 + // The feed only includes ten releases, so later nightlies can displace a published preview.
82 + for (let page = 1; version === undefined; page++) {
83 + const response = await fetchReleases(
84 + cliReleaseIndexPageUrl(page),
85 + "application/vnd.github+json",
86 + signal,
87 + );
88 const releases = decodeReleases(await response.json());
62 - const version = newestCliReleaseVersion(releases, channel);
63 - if (version !== undefined) {
64 - return compareSemverVersions(version, currentVersion) > 0 ? version : null;
65 - }
66 - if (releases.length < 100) throw new Error(`No ${channel} release was found.`);
89 + version = newestCliReleaseVersion(releases, channel);
90 + if (releases.length < 100) break;
91 }
92 +
93 + if (version === undefined) throw new Error(`No ${channel} release was found.`);
94 + return compareSemverVersions(version, currentVersion) > 0 ? version : null;
95 }
packages/shared/src/cliRelease.ts
+1
@@ -1,4 +1,5 @@
1 const CLI_RELEASE_REPOSITORY = "pingdotgg/t3code";
2 +export const CLI_RELEASES_PAGE_URL = `https://github.com/${CLI_RELEASE_REPOSITORY}/releases`;
3 export const CLI_RELEASE_CHECKSUMS_FILE = "SHA256SUMS";
4 export const CLI_RELEASE_BASE_URL_ENV = "T3CODE_RELEASE_BASE_URL";
5