11// Answers stay in the secret store behind one-use refs; orchestration records only request status.
22// Consuming tools keep values out of transcripts, projections, clients and model context.
3+ // A `file` request instead moves its value to an owner-only file for the agent's own shell.
34import {
45 CommandId ,
56 SecretRef ,
@@ -17,6 +18,7 @@ import * as Effect from "effect/Effect";
1718import * as FileSystem from "effect/FileSystem" ;
1819import * as Layer from "effect/Layer" ;
1920import * as Option from "effect/Option" ;
21+ import * as Path from "effect/Path" ;
2022import * as Schedule from "effect/Schedule" ;
2123import * as Schema from "effect/Schema" ;
2224import * as Semaphore from "effect/Semaphore" ;
@@ -30,6 +32,7 @@ const storeName = (ref: SecretRef) => `secret-request-${ref.slice(SECRET_REF_PRE
3032const REF_PATTERN = / ^ s e c r e t - r e f : [ 0 - 9 a - f ] { 32 } $ / ;
3133const SECRET_REF_TTL_MS = 24 * 60 * 60 * 1000 ;
3234const STORE_NAME_PATTERN = / ^ ( s e c r e t - r e q u e s t - [ 0 - 9 a - f ] { 32 } ) \. b i n $ / ;
35+ const FILE_NAME_PATTERN = / ^ [ 0 - 9 a - f ] { 32 } ( \. p a r t i a l ) ? $ / ;
3336
3437const refFor = ( salt : string , threadId : ThreadId , turnItemId : string ) =>
3538 SecretRef . make (
@@ -57,13 +60,21 @@ export class SecretRequests extends Context.Service<
5760 readonly ref : SecretRef ;
5861 readonly projectId : ProjectId ;
5962 } ) => Effect . Effect < string , SecretRequestError > ;
63+ /** Moves a saved `file` request's value to an owner-only file and returns its path. */
64+ readonly saveToFile : ( input : {
65+ readonly threadId : ThreadId ;
66+ readonly turnItemId : string ;
67+ } ) => Effect . Effect < string , SecretRequestError > ;
6068 }
6169> ( ) ( "t3/secrets/SecretRequests" ) { }
6270
6371const make = Effect . gen ( function * ( ) {
6472 const store = yield * ServerSecretStore . ServerSecretStore ;
6573 const fileSystem = yield * FileSystem . FileSystem ;
74+ const path = yield * Path . Path ;
6675 const threadManagement = yield * ThreadManagementService . ThreadManagementService ;
76+ // Beside the store's own files so the same owner-only directory and cleanup rules apply.
77+ const filesDir = store . directory === undefined ? undefined : path . join ( store . directory , "files" ) ;
6778
6879 const salt = Buffer . from (
6980 yield * store . getOrCreateRandom ( "secret-request-salt" , 32 ) . pipe ( Effect . orDie ) ,
@@ -124,6 +135,7 @@ const make = Effect.gen(function* () {
124135 label : item . label ,
125136 reason : item . reason ,
126137 ...( item . placeholder === undefined ? { } : { placeholder : item . placeholder } ) ,
138+ ...( item . delivery === undefined ? { } : { delivery : item . delivery } ) ,
127139 secretStatus,
128140 } )
129141 . pipe (
@@ -183,7 +195,8 @@ const make = Effect.gen(function* () {
183195 Effect . withSpan ( "SecretRequests.consume" ) ,
184196 ) ;
185197
186- 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 : {
187200 readonly ref : SecretRef ;
188201 readonly projectId : ProjectId ;
189202 } ) {
@@ -202,12 +215,72 @@ const make = Effect.gen(function* () {
202215 yield * removeLogged ( storeName ( input . ref ) ) ;
203216 return yield * new SecretRequestError ( { reason : "ref_expired" } ) ;
204217 }
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 ) ;
205226 // One use: the value moves into whatever consumed it. If it cannot be
206227 // deleted, it is not handed out, so a ref is never used twice.
207228 if ( ! ( yield * removeLogged ( storeName ( input . ref ) ) ) ) {
208229 return yield * new SecretRequestError ( { reason : "consume_failed" } ) ;
209230 }
210- return decoded . value . value ;
231+ return value ;
232+ } ) ;
233+
234+ const saveToFile : SecretRequests [ "Service" ] [ "saveToFile" ] = Effect . fn (
235+ "SecretRequests.saveToFile" ,
236+ ) ( function * ( input ) {
237+ const records = yield * threadManagement
238+ . getThreadRecords ( input . threadId , [ "turnItems" ] , {
239+ turnItemTypes : [ "secret_request" ] ,
240+ messageRoles : [ ] ,
241+ } )
242+ . pipe ( Effect . mapError ( ( cause ) => new SecretRequestError ( { reason : "load_failed" , cause } ) ) ) ;
243+ const item = records . turnItems . find ( ( candidate ) => candidate . id === input . turnItemId ) ;
244+ // Only a card that told the user the agent's commands can read the value may write it out.
245+ if (
246+ item ?. type !== "secret_request" ||
247+ item . secretStatus !== "saved" ||
248+ item . delivery !== "file"
249+ ) {
250+ return yield * new SecretRequestError ( { reason : "not_a_file_request" } ) ;
251+ }
252+ if ( filesDir === undefined ) return yield * new SecretRequestError ( { reason : "store_failed" } ) ;
253+ const ref = refFor ( salt , input . threadId , item . id ) ;
254+ const file = path . join ( filesDir , ref . slice ( SECRET_REF_PREFIX . length ) ) ;
255+ return yield * Effect . gen ( function * ( ) {
256+ // One use: the file counts as delivered only once the stored copy is gone. A retry with the
257+ // same clientRequestId gets the file the first call wrote and finishes that removal.
258+ const removeStored = removeLogged ( storeName ( ref ) ) . pipe (
259+ Effect . flatMap ( ( removed ) =>
260+ removed ? Effect . succeed ( file ) : new SecretRequestError ( { reason : "consume_failed" } ) ,
261+ ) ,
262+ ) ;
263+ if ( yield * fileSystem . exists ( file ) . pipe ( Effect . orElseSucceed ( ( ) => false ) ) ) {
264+ return yield * removeStored ;
265+ }
266+ // The stored value goes only after the file is complete, so a failed write can retry.
267+ const value = yield * readRef ( { ref, projectId : records . thread . projectId } ) ;
268+ const partial = `${ file } .partial` ;
269+ yield * fileSystem . makeDirectory ( filesDir , { recursive : true , mode : 0o700 } ) . pipe (
270+ Effect . andThen ( fileSystem . chmod ( filesDir , 0o700 ) ) ,
271+ Effect . andThen ( fileSystem . remove ( partial , { force : true } ) ) ,
272+ // `wx` never follows or replaces an existing path.
273+ Effect . andThen (
274+ fileSystem . writeFile ( partial , new TextEncoder ( ) . encode ( value ) , {
275+ flag : "wx" ,
276+ mode : 0o600 ,
277+ } ) ,
278+ ) ,
279+ Effect . andThen ( fileSystem . rename ( partial , file ) ) ,
280+ Effect . mapError ( ( cause ) => new SecretRequestError ( { reason : "store_failed" , cause } ) ) ,
281+ ) ;
282+ return yield * removeStored ;
283+ } ) . pipe ( consumeLock . withPermits ( 1 ) ) ;
211284 } ) ;
212285
213286 const sweepExpired = Effect . gen ( function * ( ) {
@@ -231,14 +304,32 @@ const make = Effect.gen(function* () {
231304 Effect . catchCause ( ( cause ) => Effect . logWarning ( "Could not sweep expired secret refs" , cause ) ) ,
232305 Effect . withSpan ( "SecretRequests.sweepExpired" ) ,
233306 ) ;
234- yield * sweepExpired ;
235- yield * sweepExpired . pipe (
307+ const sweepExpiredFiles = Effect . gen ( function * ( ) {
308+ if ( filesDir === undefined || ! ( yield * fileSystem . exists ( filesDir ) ) ) return ;
309+ const now = yield * Clock . currentTimeMillis ;
310+ let removed = 0 ;
311+ for ( const name of yield * fileSystem . readDirectory ( filesDir ) ) {
312+ if ( ! FILE_NAME_PATTERN . test ( name ) ) continue ;
313+ const file = path . join ( filesDir , name ) ;
314+ const { mtime } = yield * fileSystem . stat ( file ) ;
315+ if ( Option . exists ( mtime , ( time ) => now - time . getTime ( ) <= SECRET_REF_TTL_MS ) ) continue ;
316+ yield * fileSystem . remove ( file ) ;
317+ removed += 1 ;
318+ }
319+ yield * Effect . annotateCurrentSpan ( { "secret_request.expired_files_removed" : removed } ) ;
320+ } ) . pipe (
321+ Effect . catchCause ( ( cause ) => Effect . logWarning ( "Could not sweep expired secret files" , cause ) ) ,
322+ Effect . withSpan ( "SecretRequests.sweepExpiredFiles" ) ,
323+ ) ;
324+ const sweep = Effect . andThen ( sweepExpired , sweepExpiredFiles ) ;
325+ yield * sweep ;
326+ yield * sweep . pipe (
236327 Effect . delay ( "1 hour" ) ,
237328 Effect . repeat ( Schedule . spaced ( "1 hour" ) ) ,
238329 Effect . forkScoped ,
239330 ) ;
240331
241- return SecretRequests . of ( { answer, savedRef, consume } ) ;
332+ return SecretRequests . of ( { answer, savedRef, consume, saveToFile } ) ;
242333} ) ;
243334
244335export const layer = Layer . effect ( SecretRequests , make ) ;
0 commit comments