Skip to content

Commit 98c920b

Browse files
t3dotggclaude
andcommitted
fix(server): keep a file request's secret until its file is complete
A write failure after the stored value was used up lost the secret, and the saved card could not take a replacement. saveToFile now reads the value without using it, writes a .partial file and renames it into place, and only then deletes the stored copy. A retry after a failed write succeeds, and a crash mid-write never leaves a truncated file at the final path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1eb9b1c commit 98c920b

2 files changed

Lines changed: 51 additions & 5 deletions

File tree

‎apps/server/src/secrets/SecretRequests.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,35 @@ it.effect("a file request moves its value to an owner-only file, once", () =>
391391
}).pipe(Effect.provide(NodeServices.layer)),
392392
);
393393

394+
it.effect("a failed file write keeps the value, so the request can retry", () =>
395+
Effect.gen(function* () {
396+
const fs = yield* FileSystem.FileSystem;
397+
const path = yield* Path.Path;
398+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "secret-files-test-" });
399+
yield* withService(
400+
({ service }) =>
401+
Effect.gen(function* () {
402+
yield* service.answer({
403+
threadId,
404+
turnItemId,
405+
answer: { type: "save", secret: "cf_token" },
406+
});
407+
const ref = Option.getOrThrow(yield* service.savedRef({ threadId, turnItemId }));
408+
const name = ref.slice("secret-ref:".length);
409+
// A non-empty directory where the partial file goes makes the write fail.
410+
const partial = path.join(directory, "files", `${name}.partial`);
411+
yield* fs.makeDirectory(path.join(partial, "block"), { recursive: true });
412+
const failed = yield* service.saveToFile({ threadId, turnItemId }).pipe(Effect.flip);
413+
assert.equal(failed.reason, "store_failed");
414+
yield* fs.remove(partial, { recursive: true });
415+
const file = yield* service.saveToFile({ threadId, turnItemId });
416+
assert.equal(yield* fs.readFileString(file), "cf_token");
417+
}),
418+
{ delivery: "file", directory },
419+
);
420+
}).pipe(Effect.provide(NodeServices.layer)),
421+
);
422+
394423
it.effect("a secret the card promised to keep from the agent is never written to a file", () =>
395424
withService(({ service, stored }) =>
396425
Effect.gen(function* () {

‎apps/server/src/secrets/SecretRequests.ts‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const storeName = (ref: SecretRef) => `secret-request-${ref.slice(SECRET_REF_PRE
3232
const REF_PATTERN = /^secret-ref:[0-9a-f]{32}$/;
3333
const SECRET_REF_TTL_MS = 24 * 60 * 60 * 1000;
3434
const STORE_NAME_PATTERN = /^(secret-request-[0-9a-f]{32})\.bin$/;
35-
const FILE_NAME_PATTERN = /^[0-9a-f]{32}$/;
35+
const FILE_NAME_PATTERN = /^[0-9a-f]{32}(\.partial)?$/;
3636

3737
const refFor = (salt: string, threadId: ThreadId, turnItemId: string) =>
3838
SecretRef.make(
@@ -195,7 +195,8 @@ const make = Effect.gen(function* () {
195195
Effect.withSpan("SecretRequests.consume"),
196196
);
197197

198-
const consumeRef = Effect.fn("SecretRequests.consumeRef")(function* (input: {
198+
/** A live ref's value, left in the store. */
199+
const readRef = Effect.fn("SecretRequests.readRef")(function* (input: {
199200
readonly ref: SecretRef;
200201
readonly projectId: ProjectId;
201202
}) {
@@ -214,12 +215,20 @@ const make = Effect.gen(function* () {
214215
yield* removeLogged(storeName(input.ref));
215216
return yield* new SecretRequestError({ reason: "ref_expired" });
216217
}
218+
return decoded.value.value;
219+
});
220+
221+
const consumeRef = Effect.fn("SecretRequests.consumeRef")(function* (input: {
222+
readonly ref: SecretRef;
223+
readonly projectId: ProjectId;
224+
}) {
225+
const value = yield* readRef(input);
217226
// One use: the value moves into whatever consumed it. If it cannot be
218227
// deleted, it is not handed out, so a ref is never used twice.
219228
if (!(yield* removeLogged(storeName(input.ref)))) {
220229
return yield* new SecretRequestError({ reason: "consume_failed" });
221230
}
222-
return decoded.value.value;
231+
return value;
223232
});
224233

225234
const saveToFile: SecretRequests["Service"]["saveToFile"] = Effect.fn(
@@ -246,15 +255,23 @@ const make = Effect.gen(function* () {
246255
return yield* Effect.gen(function* () {
247256
// A retry with the same clientRequestId gets the file the first call wrote.
248257
if (yield* fileSystem.exists(file).pipe(Effect.orElseSucceed(() => false))) return file;
249-
const value = yield* consumeRef({ ref, projectId: records.thread.projectId });
258+
// The stored value goes only after the file is complete, so a failed write can retry.
259+
const value = yield* readRef({ ref, projectId: records.thread.projectId });
260+
const partial = `${file}.partial`;
250261
yield* fileSystem.makeDirectory(filesDir, { recursive: true, mode: 0o700 }).pipe(
251262
Effect.andThen(fileSystem.chmod(filesDir, 0o700)),
263+
Effect.andThen(fileSystem.remove(partial, { force: true })),
252264
// `wx` never follows or replaces an existing path.
253265
Effect.andThen(
254-
fileSystem.writeFile(file, new TextEncoder().encode(value), { flag: "wx", mode: 0o600 }),
266+
fileSystem.writeFile(partial, new TextEncoder().encode(value), {
267+
flag: "wx",
268+
mode: 0o600,
269+
}),
255270
),
271+
Effect.andThen(fileSystem.rename(partial, file)),
256272
Effect.mapError((cause) => new SecretRequestError({ reason: "store_failed", cause })),
257273
);
274+
yield* removeLogged(storeName(ref));
258275
return file;
259276
}).pipe(consumeLock.withPermits(1));
260277
});

0 commit comments

Comments
 (0)