Skip to content

Commit 001bf3c

Browse files
fix(task-cache): isolate setup and protect credential updates
1 parent 35e7342 commit 001bf3c

11 files changed

Lines changed: 362 additions & 30 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ on:
88

99
permissions:
1010
contents: read
11-
id-token: write
1211

1312
concurrency:
1413
group: ci-${{ github.event.pull_request.number || github.sha }}
@@ -60,6 +59,9 @@ jobs:
6059
# file, docs included. The `Check` job at the end of this file passes only
6160
# when every other job passes or was skipped by `changes`.
6261
lint:
62+
permissions:
63+
contents: read
64+
id-token: write
6365
name: Lint
6466
runs-on: blacksmith-8vcpu-ubuntu-2404
6567
timeout-minutes: 10
@@ -109,6 +111,9 @@ jobs:
109111
run: vp check
110112

111113
build:
114+
permissions:
115+
contents: read
116+
id-token: write
112117
name: Build and Typecheck
113118
needs: changes
114119
if: ${{ !cancelled() && (needs.changes.outputs.build != 'false' || needs.changes.outputs.typecheck != 'false') }}
@@ -198,6 +203,9 @@ jobs:
198203
# dependency layers. The concurrency limit stays at the default 4 so peak load
199204
# per runner is unchanged.
200205
test:
206+
permissions:
207+
contents: read
208+
id-token: write
201209
name: Test
202210
needs: changes
203211
if: ${{ !cancelled() && needs.changes.outputs.test != 'false' }}
@@ -279,6 +287,9 @@ jobs:
279287
# started last and ran alone for about 40s after everything else finished.
280288
# release.yml mirrors this test, test_web, and test_server split.
281289
test_web:
290+
permissions:
291+
contents: read
292+
id-token: write
282293
name: Test Web
283294
needs: changes
284295
if: ${{ !cancelled() && needs.changes.outputs.test_web != 'false' }}
@@ -334,6 +345,9 @@ jobs:
334345
# The shards are the longest jobs, so they gate themselves instead of waiting
335346
# ~15s for `changes`: a shard the change cannot affect stops after checkout.
336347
test_server:
348+
permissions:
349+
contents: read
350+
id-token: write
337351
name: Test Server ${{ matrix.shard }}
338352
runs-on: blacksmith-4vcpu-ubuntu-2404
339353
timeout-minutes: 10
@@ -496,6 +510,9 @@ jobs:
496510
run: vp run lint:mobile
497511

498512
release_smoke:
513+
permissions:
514+
contents: read
515+
id-token: write
499516
name: Release Smoke
500517
needs: changes
501518
if: ${{ !cancelled() && needs.changes.outputs.release_smoke != 'false' }}

‎infra/task-cache/scripts/install-ci.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,20 @@ const { endpoint } = JSON.parse(
2121
);
2222
const githubEnv = process.env.GITHUB_ENV;
2323
if (!githubEnv) throw new Error("Missing GitHub Actions environment file");
24+
const directory = process.env.RUNNER_TEMP;
25+
if (!directory) throw new Error("Missing GitHub Actions temporary directory");
26+
const shellSetup = NodePath.join(directory, "t3-task-cache.sh");
27+
const quote = (value: string) => "'" + value.replaceAll("'", "'\"'\"'") + "'";
2428
const preload = NodeURL.pathToFileURL(NodePath.join(root, "infra/task-cache/src/preload.mjs")).href;
25-
await NodeFSP.appendFile(
26-
githubEnv,
27-
`VP_REMOTE_CACHE_URL=${endpoint}\nNODE_OPTIONS=${process.env.NODE_OPTIONS || ""} --import=${preload}\n`,
29+
// GitHub prohibits NODE_OPTIONS in GITHUB_ENV; bash exports it to task subprocesses instead.
30+
await NodeFSP.writeFile(
31+
shellSetup,
32+
[
33+
process.env.BASH_ENV && process.env.BASH_ENV !== shellSetup
34+
? `. ${quote(process.env.BASH_ENV)}`
35+
: "",
36+
`export NODE_OPTIONS="\${NODE_OPTIONS:+$NODE_OPTIONS }"${quote(`--import=${preload}`)}`,
37+
"",
38+
].join("\n"),
2839
);
40+
await NodeFSP.appendFile(githubEnv, `VP_REMOTE_CACHE_URL=${endpoint}\nBASH_ENV=${shellSetup}\n`);

‎infra/task-cache/scripts/install.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ const prompt = NodeReadlinePromises.createInterface({
3131
let endpoint = process.env.T3_CACHE_SETUP_URL || defaultEndpoint;
3232
let name = process.env.T3_CACHE_SETUP_NAME || credentials[endpoint]?.name;
3333
let token = process.env.T3_CACHE_SETUP_TOKEN || credentials[endpoint]?.token;
34+
const initialEndpoint = endpoint;
35+
const initialName = name;
3436
try {
3537
if (!nonInteractive) {
3638
endpoint = (await prompt.question(`Cache URL [${endpoint}]: `)).trim() || endpoint;
3739
name = (await prompt.question(`Developer name${name ? ` [${name}]` : ""}: `)).trim() || name;
3840
prompt.close();
39-
if (!token || endpoint !== defaultEndpoint || name !== credentials[endpoint]?.name) {
41+
if (!token || endpoint !== initialEndpoint || name !== initialName) {
4042
if (!process.stdin.isTTY)
4143
throw new Error("Use T3_CACHE_SETUP_TOKEN for non-interactive setup.");
4244
process.stdout.write("Credential token (hidden): ");
@@ -186,14 +188,22 @@ await NodeFSP.writeFile(
186188
{ mode: 0o600 },
187189
);
188190
const sourceLine = '. "$HOME/.config/t3-task-cache/env"';
189-
for (const file of [".profile", ".bashrc", ".bash_profile", ".zshenv", ".zshrc"]) {
191+
for (const file of [".profile", ".bashrc", ".bash_profile", ".bash_login", ".zshenv", ".zshrc"]) {
190192
const path = NodePath.join(home, file);
191193
let original = "";
192194
try {
193195
original = await NodeFSP.readFile(path, "utf8");
194196
} catch (error) {
195197
if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) throw error;
196198
}
199+
if (file === ".bash_profile" || file === ".bash_login") {
200+
if (!original) continue;
201+
// Earlier installers created a profile that masks the user’s existing login profile.
202+
if (file === ".bash_profile" && original.trim() === sourceLine) {
203+
await NodeFSP.unlink(path);
204+
continue;
205+
}
206+
}
197207
if (!original.split("\n").includes(sourceLine))
198208
await NodeFSP.appendFile(path, `\n${sourceLine}\n`);
199209
}

‎infra/task-cache/scripts/smoke.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ const credential = JSON.parse(
2020
const url = new URL(endpoint);
2121
url.username = credential.name;
2222
url.password = credential.token;
23-
const env = { ...process.env, VP_REMOTE_CACHE: "read-write", VP_REMOTE_CACHE_URL: url.href };
23+
const env = {
24+
...process.env,
25+
T3_TASK_CACHE_TELEMETRY: "off",
26+
VP_REMOTE_CACHE: "read-write",
27+
VP_REMOTE_CACHE_URL: url.href,
28+
};
2429
const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-cache-probe-"));
2530
const name = `t3-cache-probe-${NodeCrypto.randomUUID()}`;
2631

‎infra/task-cache/src/Cache.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,37 @@ it.effect("aborts the upstream deadline before disposing it on interruption", ()
109109
);
110110
}),
111111
);
112+
113+
it.effect("gives telemetry a rate-limit budget separate from artifact downloads", () => {
114+
const limit = vi.fn(async (_options: { key: string }) => ({ success: true }));
115+
return Effect.gen(function* () {
116+
const cache = yield* Cache.Cache;
117+
yield* cache.handle(new Request("https://cache.example/projects/t3code/telemetry"), {
118+
waitUntil: () => {},
119+
});
120+
yield* cache.handle(
121+
new Request(
122+
"https://cache.example/projects/t3code/blob/12345678-1234-1234-1234-123456789abc",
123+
{ method: "POST" },
124+
),
125+
{ waitUntil: () => {} },
126+
);
127+
expect(limit.mock.calls).toEqual([[{ key: "t3code:telemetry" }], [{ key: "t3code:blob" }]]);
128+
}).pipe(
129+
Effect.provide(
130+
Cache.layer.pipe(
131+
Layer.provide([
132+
Layer.succeed(Cache.Bindings, {
133+
readLimit: { limit },
134+
} as unknown as Cache.Bindings["Service"]),
135+
Admission.layer,
136+
Layer.succeed(Savings.Savings, {
137+
record: () => Effect.void,
138+
emitTotals: () => Effect.void,
139+
cleanup: Effect.void,
140+
}),
141+
]),
142+
),
143+
),
144+
);
145+
});

‎infra/task-cache/src/Cache.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const make = Effect.gen(function* () {
8585
const allowed = yield* protocol(() =>
8686
(writing ? bindings.storeLimit : bindings.readLimit).limit({
8787
key: route
88-
? `t3code:${writing ? "store" : operation === "fetch" ? "fetch" : "blob"}`
88+
? `t3code:${writing ? "store" : operation === "fetch" || operation === "telemetry" ? operation : "blob"}`
8989
: "unknown",
9090
}),
9191
);

‎infra/task-cache/src/WriterRegistry.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,57 @@ it.effect("writes credentials for the supplied deployed worker endpoint", () =>
101101
expect(credential.name).toBe("maria");
102102
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
103103
);
104+
105+
it.effect("refuses a concurrent registry update without changing committed credentials", () =>
106+
Effect.gen(function* () {
107+
const fs = yield* FileSystem.FileSystem;
108+
const home = yield* fs.makeTempDirectoryScoped();
109+
const state = `${home}/.config/t3-task-cache`;
110+
yield* fs.makeDirectory(`${state}/writers.lock`, { recursive: true });
111+
yield* fs.writeFileString(`${state}/writers.json`, '{"maria":"digest"}');
112+
const result = yield* WriterRegistry.WriterRegistry.pipe(
113+
Effect.flatMap((registry) => registry.update("theo", false, "https://cache.example")),
114+
Effect.provide(
115+
WriterRegistry.layer.pipe(
116+
Layer.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ HOME: home }))),
117+
),
118+
),
119+
Effect.exit,
120+
);
121+
expect(Exit.isFailure(result)).toBe(true);
122+
expect(yield* fs.readFileString(`${state}/writers.json`)).toBe('{"maria":"digest"}');
123+
expect(yield* fs.exists(`${state}/theo.json`)).toBe(false);
124+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
125+
);
126+
127+
it.effect("keeps the committed registry intact if a replacement write fails", () =>
128+
Effect.gen(function* () {
129+
const fs = yield* FileSystem.FileSystem;
130+
const home = yield* fs.makeTempDirectoryScoped();
131+
const state = `${home}/.config/t3-task-cache`;
132+
yield* fs.makeDirectory(state, { recursive: true });
133+
const original = '{"maria":"digest"}';
134+
yield* fs.writeFileString(`${state}/writers.json`, original);
135+
const result = yield* WriterRegistry.WriterRegistry.pipe(
136+
Effect.flatMap((registry) => registry.update("theo", false, "https://cache.example")),
137+
Effect.provide(
138+
WriterRegistry.layer.pipe(
139+
Layer.provide([
140+
ConfigProvider.layer(ConfigProvider.fromUnknown({ HOME: home })),
141+
Layer.succeed(FileSystem.FileSystem, {
142+
...fs,
143+
writeFileString: (path) =>
144+
fs
145+
.writeFileString(path, "partial")
146+
.pipe(Effect.andThen(Effect.die("disk write failed"))),
147+
}),
148+
]),
149+
),
150+
),
151+
Effect.exit,
152+
);
153+
expect(Exit.isFailure(result)).toBe(true);
154+
expect(yield* fs.readFileString(`${state}/writers.json`)).toBe(original);
155+
expect(yield* fs.exists(`${state}/writers.lock`)).toBe(false);
156+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
157+
);

‎infra/task-cache/src/WriterRegistry.ts‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,17 @@ const make = Effect.gen(function* () {
5454

5555
const save = Effect.fn("TaskCache.save")(function* (name: string, value: string) {
5656
yield* fs.makeDirectory(state, { recursive: true, mode: 0o700 });
57-
const file = `${state}/${name}.json`;
58-
yield* fs.writeFileString(file, value + "\n", { mode: 0o600 });
59-
yield* fs.chmod(file, 0o600);
57+
yield* Effect.scoped(
58+
Effect.gen(function* () {
59+
const directory = yield* fs.makeTempDirectoryScoped({
60+
directory: state,
61+
prefix: ".writer-",
62+
});
63+
const temporary = `${directory}/${name}.json`;
64+
yield* fs.writeFileString(temporary, value + "\n", { mode: 0o600 });
65+
yield* fs.rename(temporary, `${state}/${name}.json`);
66+
}),
67+
);
6068
});
6169

6270
const updateWriter = Effect.fn("TaskCache.updateWriter")(function* (
@@ -67,16 +75,35 @@ const make = Effect.gen(function* () {
6775
if (!isWriterName(name)) return yield* new ManagementError({ message: "Invalid writer name." });
6876
if (!revoke && !endpoint)
6977
return yield* new ManagementError({ message: "A deployed cache endpoint is required." });
70-
const exists = yield* fs.exists(`${state}/writers.json`);
71-
const writers = { ...(exists || revoke ? yield* readWriters() : {}) };
72-
const token = hex(yield* crypto.randomBytes(32));
73-
if (revoke) delete writers[name];
74-
else {
75-
writers[name] = hex(yield* crypto.digest("SHA-256", new TextEncoder().encode(token)));
76-
}
77-
yield* save("writers", yield* encodeWriters(writers));
78-
if (!revoke && endpoint) yield* save(name, yield* encodeCredential({ endpoint, name, token }));
79-
return revoke ? undefined : `${state}/${name}.json`;
78+
return yield* Effect.scoped(
79+
Effect.gen(function* () {
80+
yield* fs.makeDirectory(state, { recursive: true, mode: 0o700 });
81+
// Exclusive creation coordinates separate management processes as well as fibers.
82+
yield* Effect.acquireRelease(
83+
fs.makeDirectory(`${state}/writers.lock`).pipe(
84+
Effect.mapError(
85+
() =>
86+
new ManagementError({
87+
message:
88+
"Writer registry is locked by another update. Retry after it completes; remove writers.lock only if that process has stopped.",
89+
}),
90+
),
91+
),
92+
() => fs.remove(`${state}/writers.lock`, { recursive: true }).pipe(Effect.orDie),
93+
);
94+
const exists = yield* fs.exists(`${state}/writers.json`);
95+
const writers = { ...(exists || revoke ? yield* readWriters() : {}) };
96+
const token = hex(yield* crypto.randomBytes(32));
97+
if (revoke) delete writers[name];
98+
else {
99+
writers[name] = hex(yield* crypto.digest("SHA-256", new TextEncoder().encode(token)));
100+
}
101+
yield* save("writers", yield* encodeWriters(writers));
102+
if (!revoke && endpoint)
103+
yield* save(name, yield* encodeCredential({ endpoint, name, token }));
104+
return revoke ? undefined : `${state}/${name}.json`;
105+
}),
106+
);
80107
});
81108
return WriterRegistry.of({ read: readWriters, update: updateWriter });
82109
});

‎infra/task-cache/src/nativeHook.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,26 @@ it("rejects an unknown native entrypoint before changing it", () => {
1616
"Unsupported Vite+ entrypoint",
1717
);
1818
});
19+
20+
it("leaves an unsupported CLI runnable when the machine preload is enabled", async () => {
21+
const fs = await import("node:fs/promises");
22+
const os = await import("node:os");
23+
const path = await import("node:path");
24+
const { promisify } = await import("node:util");
25+
const { execFile } = await import("node:child_process");
26+
const root = await fs.mkdtemp(path.join(os.tmpdir(), "cache-preload-"));
27+
try {
28+
const directory = path.join(root, "vite-plus/dist");
29+
await fs.mkdir(directory, { recursive: true });
30+
await fs.writeFile(path.join(root, "package.json"), '{"type":"module"}');
31+
await fs.writeFile(path.join(directory, "bin.js"), 'console.log("unmodified CLI");');
32+
const result = await promisify(execFile)(
33+
process.execPath,
34+
["--import", new URL("./preload.mjs", import.meta.url).href, path.join(directory, "bin.js")],
35+
{ env: { ...process.env, NODE_OPTIONS: "" } },
36+
);
37+
expect(result.stdout.trim()).toBe("unmodified CLI");
38+
} finally {
39+
await fs.rm(root, { recursive: true, force: true });
40+
}
41+
});

‎infra/task-cache/src/preload.mjs‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@ if (
1111
load(url, context, nextLoad) {
1212
const loaded = nextLoad(url, context);
1313
if (!url.endsWith("/vite-plus/dist/bin.js")) return loaded;
14-
const source =
15-
typeof loaded.source === "string" ? loaded.source : Buffer.from(loaded.source).toString();
16-
return {
17-
...loaded,
18-
source: patchNativeCli(source).replace(
19-
'from "./run-hook.js"',
20-
`from ${JSON.stringify(hookUrl)}`,
21-
),
22-
};
14+
try {
15+
const source =
16+
typeof loaded.source === "string" ? loaded.source : Buffer.from(loaded.source).toString();
17+
return {
18+
...loaded,
19+
source: patchNativeCli(source).replace(
20+
'from "./run-hook.js"',
21+
`from ${JSON.stringify(hookUrl)}`,
22+
),
23+
};
24+
} catch {
25+
process.stderr.write(
26+
"Task cache hook does not support this Vite+ version; running without telemetry.\n",
27+
);
28+
return loaded;
29+
}
2330
},
2431
});
2532
}

0 commit comments

Comments
 (0)