The best way to control Claude Code, Codex and more from any computer or phone
fix(infra): task-cache telemetry no longer strands an event queued during a flush (#25653)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 26, 2026 at 15:04 UTC
e2d3ded436f7b32c07aeb9bf7b4738bfef58156d
3 files changed
+69
-7
infra/task-cache/src/runHook.test.ts
+1
-1
@@ -162,7 +162,7 @@ it("uses one machine hook across fresh worktrees without modifying Vite+", async
162
expect(await run("build", secondWorktree)).toBe(0);
163
await report(4);
164
expect(await run("build", secondWorktree, true)).toBe(0);
165
- expect(completedReports).toBe(5);
165
+ await report(5);
166
expect(await NodeFSP.readFile(entrypoint, "utf8")).toBe(originalEntrypoint);
167
expect(await run("nested", root, false, "--no-cache")).toBe(0);
168
expect(await run("build", nested)).toBe(0);
infra/task-cache/src/telemetry.test.ts
+42
@@ -5,6 +5,11 @@ import * as NodePath from "node:path";
5
import { expect, it, vi } from "vite-plus/test";
6
import { flushTelemetry, observeSummaries, summarize } from "./telemetry.ts";
7
8
+vi.mock("node:fs", async (importOriginal) => {
9
+ const fs = await importOriginal<typeof import("node:fs")>();
10
+ return { ...fs, rmdirSync: vi.fn(fs.rmdirSync) };
11
+});
12
+
13
const task = (result: unknown) => ({
14
package_name: "@team/server",
15
task_name: "typecheck",
@@ -160,3 +165,40 @@ it("drops revoked credentials and continues delivering other queued endpoints",
165
NodeFS.rmSync(home, { recursive: true, force: true });
166
}
167
});
168
+
169
+it("delivers an event queued while the previous flusher releases its lock", async () => {
170
+ const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "cache-handoff-"));
171
+ const endpoint = "https://cache.example/projects/t3code";
172
+ try {
173
+ const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry");
174
+ const state = NodePath.join(home, ".config/t3-task-cache");
175
+ NodeFS.mkdirSync(queue, { recursive: true });
176
+ NodeFS.mkdirSync(state, { recursive: true });
177
+ NodeFS.writeFileSync(
178
+ NodePath.join(state, "credentials.json"),
179
+ JSON.stringify({ [endpoint]: { name: "alice", token: "secret-token" } }),
180
+ );
181
+ using request = vi
182
+ .spyOn(globalThis, "fetch")
183
+ .mockImplementation(async () => new Response(null, { status: 204 }));
184
+ const rmdirSync = vi.mocked(NodeFS.rmdirSync);
185
+ const release = rmdirSync.getMockImplementation()!;
186
+ let competing: Promise<void> | undefined;
187
+ // The next command queues its event after this flusher's last scan, and its own flusher
188
+ // gives up because the lock is still held.
189
+ rmdirSync.mockImplementationOnce((path, options) => {
190
+ NodeFS.writeFileSync(
191
+ NodePath.join(queue, "late.json"),
192
+ JSON.stringify({ endpoint, event: { id: "late" } }),
193
+ );
194
+ competing = flushTelemetry(home, {});
195
+ release(path, options);
196
+ });
197
+ await flushTelemetry(home, {});
198
+ await competing;
199
+ expect(request).toHaveBeenCalledOnce();
200
+ expect(NodeFS.readdirSync(queue)).toEqual([]);
201
+ } finally {
202
+ NodeFS.rmSync(home, { recursive: true, force: true });
203
+ }
204
+});
infra/task-cache/src/telemetry.ts
+26
-6
@@ -161,6 +161,28 @@ export function observeSummaries(
161
162
export async function flushTelemetry(home = NodeOS.homedir(), env = process.env) {
163
const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry");
164
+ const deadline = Date.now() + 15_000;
165
+ const skipped = new Set<string>();
166
+ // A flusher that finds the lock taken exits and leaves its event to the holder. Re-check after
167
+ // releasing, or an event queued after the holder's last scan waits for the next command.
168
+ try {
169
+ while (
170
+ (await drainTelemetry(queue, home, env, deadline, skipped)) &&
171
+ Date.now() < deadline &&
172
+ NodeFS.readdirSync(queue).some((entry) => entry.endsWith(".json") && !skipped.has(entry))
173
+ );
174
+ } catch {
175
+ /* Leave unsent events for a later invocation. */
176
+ }
177
+}
178
+
179
+async function drainTelemetry(
180
+ queue: string,
181
+ home: string,
182
+ env: NodeJS.ProcessEnv,
183
+ deadline: number,
184
+ skipped: Set<string>,
185
+) {
186
const lock = NodePath.join(queue, "flush.lock");
187
try {
188
NodeFS.mkdirSync(queue, { recursive: true, mode: 0o700 });
@@ -171,7 +193,7 @@ export async function flushTelemetry(home = NodeOS.homedir(), env = process.env)
193
}
194
NodeFS.mkdirSync(lock);
195
} catch {
174
- return;
196
+ return false;
197
}
198
try {
199
const files = NodeFS.readdirSync(queue)
@@ -188,14 +210,12 @@ export async function flushTelemetry(home = NodeOS.homedir(), env = process.env)
210
const credentials = NodeFS.existsSync(credentialsFile)
211
? JSON.parse(NodeFS.readFileSync(credentialsFile, "utf8"))
212
: {};
191
- const deadline = Date.now() + 15_000;
192
- const skipped = new Set<string>();
213
// Re-scan between sends so a concurrent command's final summary joins this flush.
214
for (let sent = 0; sent < 100 && Date.now() < deadline;) {
215
const file = NodeFS.readdirSync(queue)
216
.filter((entry) => entry.endsWith(".json") && !skipped.has(entry))
217
.sort()[0];
198
- if (!file) break;
218
+ if (!file) return true;
219
const path = NodePath.join(queue, file);
220
const { endpoint, event } = JSON.parse(NodeFS.readFileSync(path, "utf8"));
221
const url = new URL(endpoint);
@@ -229,7 +249,7 @@ export async function flushTelemetry(home = NodeOS.homedir(), env = process.env)
249
headers: { Authorization: `Bearer ${env.ACTIONS_ID_TOKEN_REQUEST_TOKEN}` },
250
signal: AbortSignal.timeout(3000),
251
});
232
- if (!response.ok) return;
252
+ if (!response.ok) return false;
253
const token = (await response.json()) as { value: string };
254
authorization = `Bearer ${token.value}`;
255
} else {
@@ -246,7 +266,7 @@ export async function flushTelemetry(home = NodeOS.homedir(), env = process.env)
266
});
267
await response.body?.cancel();
268
if (response.ok || [400, 401, 403, 413].includes(response.status)) NodeFS.unlinkSync(path);
249
- else return;
269
+ else return false;
270
}
271
} catch {
272
/* Leave unsent events for a later invocation. */