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, 14:30 UTC
fix(task-cache): the hook test waits for every flusher it starts (#26148)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 27, 2026 at 15:48 UTC
4cb5f108ff8f64a2fc59ed4d92cccbd2a6e6da2c
1 file changed
+80
-32
infra/task-cache/src/runHook.test.ts
+80
-32
@@ -1,28 +1,17 @@
1
// @effect-diagnostics nodeBuiltinImport:off - exercises the real native runner and local HTTP collector.
2
import * as NodeChildProcess from "node:child_process";
3
+import * as NodeDiagnosticsChannel from "node:diagnostics_channel";
4
import * as NodeFSP from "node:fs/promises";
5
import * as NodeHttp from "node:http";
6
import * as NodeOS from "node:os";
7
import * as NodePath from "node:path";
7
-import { expect, it } from "vite-plus/test";
8
+import { expect, it, vi } from "vite-plus/test";
9
+import startRunHook from "./runHook.ts";
10
11
it("uses one machine hook across fresh worktrees without modifying Vite+", async () => {
12
const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-hook-"));
13
const unrelated = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "unrelated-project-"));
14
const reports: { authorization: string | undefined; body: string }[] = [];
13
- let firstExit!: () => void;
14
- const firstExited = new Promise<void>((resolve) => {
15
- firstExit = resolve;
16
- });
17
- let completedReports = 0;
18
- let notified: (() => void) | undefined;
19
- let firstDisconnected = false;
20
- const report = (count: number) =>
21
- completedReports >= count
22
- ? Promise.resolve()
23
- : new Promise<void>((resolve) => {
24
- notified = resolve;
25
- });
15
const server = NodeHttp.createServer(async (request, response) => {
16
if (!request.url?.endsWith("/telemetry")) {
17
response.writeHead(404).end();
@@ -31,14 +20,7 @@ it("uses one machine hook across fresh worktrees without modifying Vite+", async
20
let body = "";
21
for await (const chunk of request) body += chunk;
22
reports.push({ authorization: request.headers.authorization, body });
34
- if (reports.length === 1) {
35
- await firstExited;
36
- firstDisconnected = response.destroyed;
37
- }
23
response.writeHead(204).end();
39
- completedReports++;
40
- notified?.();
41
- notified = undefined;
24
});
25
try {
26
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
@@ -110,7 +92,7 @@ it("uses one machine hook across fresh worktrees without modifying Vite+", async
92
}),
93
);
94
const originalEntrypoint = await NodeFSP.readFile(entrypoint, "utf8");
113
- const run = (task: string, cwd = root, ci = false, cache = "--cache") =>
95
+ const run = (task: string, cwd = root, cache = "--cache") =>
96
new Promise<number | null>((resolve, reject) => {
97
const child = NodeChildProcess.spawn(
98
process.execPath,
@@ -120,7 +102,8 @@ it("uses one machine hook across fresh worktrees without modifying Vite+", async
102
env: {
103
...process.env,
104
HOME: root,
123
- GITHUB_ACTIONS: ci ? "true" : undefined,
105
+ // Flush inline so no detached flusher can write under root while it is removed.
106
+ GITHUB_ACTIONS: "true",
107
T3_TASK_CACHE_TELEMETRY: undefined,
108
T3_TASK_CACHE_HOST: undefined,
109
VITE_CACHE_PATH: undefined,
@@ -139,32 +122,30 @@ it("uses one machine hook across fresh worktrees without modifying Vite+", async
122
});
123
child.once("error", reject);
124
child.once("close", (code) => {
142
- firstExit();
125
if (code !== 0 && task !== "fail") reject(new Error(stderr));
126
else resolve(code);
127
});
128
});
129
expect(await run("build")).toBe(0);
148
- await report(1);
130
+ expect(reports).toHaveLength(1);
131
expect(await run("--last-details")).toBe(0);
132
expect(
133
JSON.parse(await NodeFSP.readFile(NodePath.join(root, "environment.json"), "utf8")),
134
).toMatchObject({ url: null });
153
- expect(firstDisconnected).toBe(false);
135
expect(await run("build")).toBe(0);
155
- await report(2);
136
+ expect(reports).toHaveLength(2);
137
expect(await run("fail")).toBe(7);
157
- await report(3);
138
+ expect(reports).toHaveLength(3);
139
const secondWorktree = NodePath.join(root, "second-worktree");
140
await NodeFSP.mkdir(secondWorktree);
141
for (const file of ["package.json", ".task-cache.json", "vite.config.ts", "build.cjs"])
142
await NodeFSP.copyFile(NodePath.join(root, file), NodePath.join(secondWorktree, file));
143
expect(await run("build", secondWorktree)).toBe(0);
163
- await report(4);
164
- expect(await run("build", secondWorktree, true)).toBe(0);
165
- await report(5);
144
+ expect(reports).toHaveLength(4);
145
+ expect(await run("build", secondWorktree)).toBe(0);
146
+ expect(reports).toHaveLength(5);
147
expect(await NodeFSP.readFile(entrypoint, "utf8")).toBe(originalEntrypoint);
167
- expect(await run("nested", root, false, "--no-cache")).toBe(0);
148
+ expect(await run("nested", root, "--no-cache")).toBe(0);
149
expect(await run("build", nested)).toBe(0);
150
expect(reports).toHaveLength(5);
151
expect(
@@ -187,6 +168,73 @@ it("uses one machine hook across fresh worktrees without modifying Vite+", async
168
}
169
});
170
171
+it("sends telemetry from a detached flusher that the task runner does not wait for", async () => {
172
+ const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-flusher-"));
173
+ const reports: string[] = [];
174
+ let finishReturned!: () => void;
175
+ const returned = new Promise<void>((resolve) => {
176
+ finishReturned = resolve;
177
+ });
178
+ let disconnected = false;
179
+ const server = NodeHttp.createServer(async (request, response) => {
180
+ let body = "";
181
+ for await (const chunk of request) body += chunk;
182
+ reports.push(body);
183
+ await returned;
184
+ disconnected = response.destroyed;
185
+ response.writeHead(204).end();
186
+ });
187
+ // The hook detaches and unrefs its flusher, so only this channel lets cleanup wait for it.
188
+ const flushers: Promise<void>[] = [];
189
+ const onSpawn = (message: unknown) => {
190
+ const { process: flusher } = message as { process: NodeChildProcess.ChildProcess };
191
+ flushers.push(new Promise((resolve) => flusher.once("close", () => resolve())));
192
+ };
193
+ NodeDiagnosticsChannel.subscribe("child_process", onSpawn);
194
+ try {
195
+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
196
+ const address = server.address();
197
+ if (!address || typeof address === "string") throw new Error("Missing HTTP address");
198
+ const endpoint = `http://127.0.0.1:${address.port}/projects/test`;
199
+ await NodeFSP.writeFile(NodePath.join(root, ".task-cache.json"), JSON.stringify({ endpoint }));
200
+ await NodeFSP.mkdir(NodePath.join(root, ".config/t3-task-cache"), { recursive: true });
201
+ await NodeFSP.writeFile(
202
+ NodePath.join(root, ".config/t3-task-cache/credentials.json"),
203
+ JSON.stringify({ [endpoint]: { name: "maria", token: "secret" } }),
204
+ );
205
+ vi.stubEnv("HOME", root);
206
+ vi.stubEnv("GITHUB_ACTIONS", undefined);
207
+ vi.stubEnv("T3_TASK_CACHE_TELEMETRY", undefined);
208
+ vi.stubEnv("VITE_CACHE_PATH", undefined);
209
+ const finish = startRunHook({ root });
210
+ const summary = NodePath.join(root, "node_modules/.vite/task-cache/run/last-summary.json");
211
+ await NodeFSP.mkdir(NodePath.dirname(summary), { recursive: true });
212
+ await NodeFSP.writeFile(
213
+ summary,
214
+ JSON.stringify({
215
+ tasks: [{ package_name: "fixture", task_name: "build", result: { Spawned: {} } }],
216
+ }),
217
+ );
218
+ await finish?.();
219
+ finishReturned();
220
+ await Promise.all(flushers);
221
+ expect(flushers).toHaveLength(1);
222
+ expect(reports.map((report) => JSON.parse(report).tasks)).toEqual([
223
+ [{ package: "fixture", task: "build", source: "executed", savedMs: 0 }],
224
+ ]);
225
+ expect(disconnected).toBe(false);
226
+ } finally {
227
+ finishReturned();
228
+ await Promise.all(flushers);
229
+ NodeDiagnosticsChannel.unsubscribe("child_process", onSpawn);
230
+ vi.unstubAllEnvs();
231
+ await new Promise<void>((resolve, reject) =>
232
+ server.close((error) => (error ? reject(error) : resolve())),
233
+ );
234
+ await NodeFSP.rm(root, { recursive: true, force: true });
235
+ }
236
+});
237
+
238
it("rejects invalid setup credentials before installing anything and never prints the token", async () => {
239
const home = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-setup-"));
240
const token = "private-credential-that-must-not-appear-in-output";