feat(server): request_secret can save a CLI token to a private file #24809
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 3 commits into
main fromrefs/pull/24809/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/secrets/SecretRequests.ts line 249OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -212,0 +229,21 @@229+ .getThreadRecords(input.threadId, ["turnItems"], {230+ turnItemTypes: ["secret_request"],231+ messageRoles: [],232+ })233+ .pipe(Effect.mapError((cause) => new SecretRequestError({ reason: "load_failed", cause })));234+ const item = records.turnItems.find((candidate) => candidate.id === input.turnItemId);235+ // Only a card that told the user the agent's commands can read the value may write it out.236+ if (237+ item?.type !== "secret_request" ||238+ item.secretStatus !== "saved" ||239+ item.delivery !== "file"240+ ) {241+ return yield* new SecretRequestError({ reason: "not_a_file_request" });242+ }243+ if (filesDir === undefined) return yield* new SecretRequestError({ reason: "store_failed" });244+ const ref = refFor(salt, input.threadId, item.id);245+ const file = path.join(filesDir, ref.slice(SECRET_REF_PREFIX.length));246+ return yield* Effect.gen(function* () {247+ // A retry with the same clientRequestId gets the file the first call wrote.248+ if (yield* fileSystem.exists(file).pipe(Effect.orElseSucceed(() => false))) return file;249+ const value = yield* consumeRef({ ref, projectId: records.thread.projectId });
- apps/server/src/secrets/SecretRequests.ts line 249OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -212,0 +229,21 @@229+ .getThreadRecords(input.threadId, ["turnItems"], {230+ turnItemTypes: ["secret_request"],231+ messageRoles: [],232+ })233+ .pipe(Effect.mapError((cause) => new SecretRequestError({ reason: "load_failed", cause })));234+ const item = records.turnItems.find((candidate) => candidate.id === input.turnItemId);235+ // Only a card that told the user the agent's commands can read the value may write it out.236+ if (237+ item?.type !== "secret_request" ||238+ item.secretStatus !== "saved" ||239+ item.delivery !== "file"240+ ) {241+ return yield* new SecretRequestError({ reason: "not_a_file_request" });242+ }243+ if (filesDir === undefined) return yield* new SecretRequestError({ reason: "store_failed" });244+ const ref = refFor(salt, input.threadId, item.id);245+ const file = path.join(filesDir, ref.slice(SECRET_REF_PREFIX.length));246+ return yield* Effect.gen(function* () {247+ // A retry with the same clientRequestId gets the file the first call wrote.248+ if (yield* fileSystem.exists(file).pipe(Effect.orElseSucceed(() => false))) return file;249+ const value = yield* consumeRef({ ref, projectId: records.thread.projectId });apps/server/src/secrets/SecretRequests.ts lines 307 to 332Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -236,6 +289,20 @@289+ const file = path.join(filesDir, name);290+ const { mtime } = yield* fileSystem.stat(file);291+ if (Option.exists(mtime, (time) => now - time.getTime() <= SECRET_REF_TTL_MS)) continue;292+ yield* fileSystem.remove(file);293+ removed += 1;294+ }295+ yield* Effect.annotateCurrentSpan({ "secret_request.expired_files_removed": removed });296+ }).pipe(297+ Effect.catchCause((cause) => Effect.logWarning("Could not sweep expired secret files", cause)),298+ Effect.withSpan("SecretRequests.sweepExpiredFiles"),299+ );300+ const sweep = Effect.andThen(sweepExpired, sweepExpiredFiles);301+ yield* sweep;302+ yield* sweep.pipe(236303 Effect.delay("1 hour"),237304 Effect.repeat(Schedule.spaced("1 hour")),238305 Effect.forkScoped,239306 );240307241- return SecretRequests.of({ answer, savedRef, consume });308+ return SecretRequests.of({ answer, savedRef, consume, saveToFile });
- apps/server/src/secrets/SecretRequests.ts line 274OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -210,0 +254,21 @@254+ const file = path.join(filesDir, ref.slice(SECRET_REF_PREFIX.length));255+ return yield* Effect.gen(function* () {256+ // A retry with the same clientRequestId gets the file the first call wrote.257+ if (yield* fileSystem.exists(file).pipe(Effect.orElseSucceed(() => false))) return file;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`;261+ yield* fileSystem.makeDirectory(filesDir, { recursive: true, mode: 0o700 }).pipe(262+ Effect.andThen(fileSystem.chmod(filesDir, 0o700)),263+ Effect.andThen(fileSystem.remove(partial, { force: true })),264+ // `wx` never follows or replaces an existing path.265+ Effect.andThen(266+ fileSystem.writeFile(partial, new TextEncoder().encode(value), {267+ flag: "wx",268+ mode: 0o600,269+ }),270+ ),271+ Effect.andThen(fileSystem.rename(partial, file)),272+ Effect.mapError((cause) => new SecretRequestError({ reason: "store_failed", cause })),273+ );274+ yield* removeLogged(storeName(ref));
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed