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 } $ / ;
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 (
@@ -210,6 +222,43 @@ const make = Effect.gen(function* () {
210222 return decoded . value . value ;
211223 } ) ;
212224
225+ const saveToFile : SecretRequests [ "Service" ] [ "saveToFile" ] = Effect . fn (
226+ "SecretRequests.saveToFile" ,
227+ ) ( function * ( input ) {
228+ const records = yield * threadManagement
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 } ) ;
250+ yield * fileSystem . makeDirectory ( filesDir , { recursive : true , mode : 0o700 } ) . pipe (
251+ Effect . andThen ( fileSystem . chmod ( filesDir , 0o700 ) ) ,
252+ // `wx` never follows or replaces an existing path.
253+ Effect . andThen (
254+ fileSystem . writeFile ( file , new TextEncoder ( ) . encode ( value ) , { flag : "wx" , mode : 0o600 } ) ,
255+ ) ,
256+ Effect . mapError ( ( cause ) => new SecretRequestError ( { reason : "store_failed" , cause } ) ) ,
257+ ) ;
258+ return file ;
259+ } ) . pipe ( consumeLock . withPermits ( 1 ) ) ;
260+ } ) ;
261+
213262 const sweepExpired = Effect . gen ( function * ( ) {
214263 if ( store . directory === undefined ) return ;
215264 const now = yield * Clock . currentTimeMillis ;
@@ -231,14 +280,32 @@ const make = Effect.gen(function* () {
231280 Effect . catchCause ( ( cause ) => Effect . logWarning ( "Could not sweep expired secret refs" , cause ) ) ,
232281 Effect . withSpan ( "SecretRequests.sweepExpired" ) ,
233282 ) ;
234- yield * sweepExpired ;
235- yield * sweepExpired . pipe (
283+ const sweepExpiredFiles = Effect . gen ( function * ( ) {
284+ if ( filesDir === undefined || ! ( yield * fileSystem . exists ( filesDir ) ) ) return ;
285+ const now = yield * Clock . currentTimeMillis ;
286+ let removed = 0 ;
287+ for ( const name of yield * fileSystem . readDirectory ( filesDir ) ) {
288+ if ( ! FILE_NAME_PATTERN . test ( name ) ) continue ;
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 ) ;
240307
241- return SecretRequests . of ( { answer, savedRef, consume } ) ;
308+ return SecretRequests . of ( { answer, savedRef, consume, saveToFile } ) ;
242309} ) ;
243310
244311export const layer = Layer . effect ( SecretRequests , make ) ;
0 commit comments