@@ -8,12 +8,16 @@ import {
88 ProviderInstanceId ,
99 RunId ,
1010 ScheduledTaskId ,
11+ SECRET_REQUEST_UNSAVED_NOTE ,
1112 ThreadId ,
1213 TurnItemId ,
1314} from "@t3tools/contracts" ;
1415import * as DateTime from "effect/DateTime" ;
1516import * as Effect from "effect/Effect" ;
17+ import * as Fiber from "effect/Fiber" ;
1618import * as Layer from "effect/Layer" ;
19+ import * as Metric from "effect/Metric" ;
20+ import * as TestClock from "effect/testing/TestClock" ;
1721import * as NodeCrypto from "@effect/platform-node/NodeCrypto" ;
1822import { expect , it } from "vite-plus/test" ;
1923
@@ -655,3 +659,185 @@ it("readThread and sendToThread reach threads in other projects", async () => {
655659 expect ( stopped . code ) . toBe ( "parent_not_active" ) ;
656660 } ) . pipe ( Effect . provide ( layer ) , Effect . runPromise ) ;
657661} ) ;
662+
663+ it ( "requestSecret saves a pasted value to a file without opening a card" , async ( ) => {
664+ const projection = {
665+ thread : baseThread ( {
666+ threadId : parentThreadId ,
667+ title : "Parent" ,
668+ instanceId : parentInstanceId ,
669+ model : "gpt-5.4" ,
670+ } ) ,
671+ runs : [ { ...makeRun ( { id : activeRunId , ordinal : 1 , status : "running" } ) , rootNodeId : taskId } ] ,
672+ visibleTurnItems : [ ] ,
673+ runtimeRequests : [ ] ,
674+ messages : [ ] ,
675+ contextTransfers : [ ] ,
676+ subagents : [ ] ,
677+ updatedAt : now ,
678+ } as unknown as OrchestrationV2ThreadProjection ;
679+ const dispatched : Array < unknown > = [ ] ;
680+ const saved : Array < {
681+ readonly requestNamespace : string ;
682+ readonly requestKey : string ;
683+ readonly value : string ;
684+ } > = [ ] ;
685+ const file = "/home/user/.t3/userdata/secrets/files/0123456789abcdef0123456789abcdef" ;
686+
687+ const layer = layerOrchestratorService . pipe (
688+ Layer . provide (
689+ Layer . mergeAll (
690+ Layer . mock ( ThreadManagementService . ThreadManagementService ) ( {
691+ getThreadRecords : ( threadId ) =>
692+ threadId === parentThreadId
693+ ? Effect . succeed ( projection )
694+ : Effect . die ( `unexpected thread ${ threadId } ` ) ,
695+ getThreadShell : ( threadId ) =>
696+ Effect . succeed (
697+ threadId === parentThreadId
698+ ? ( projection . thread as unknown as OrchestrationV2ThreadShell )
699+ : null ,
700+ ) ,
701+ dispatch : ( command ) => Effect . sync ( ( ) => void dispatched . push ( command ) as never ) ,
702+ } satisfies Partial < ThreadManagementService . ThreadManagementService [ "Service" ] > ) ,
703+ ServerSettings . layerTest ( ) ,
704+ Layer . mock ( ProviderRegistry . ProviderRegistry ) ( {
705+ getProviders : Effect . succeed ( [ ] ) ,
706+ } satisfies Partial < ProviderRegistry . ProviderRegistry [ "Service" ] > ) ,
707+ Layer . mock ( ProjectService . ProjectService ) ( { } ) ,
708+ Layer . mock ( SecretRequests . SecretRequests ) ( {
709+ savePasted : ( input ) =>
710+ Effect . sync ( ( ) => {
711+ saved . push ( {
712+ requestNamespace : input . requestNamespace ,
713+ requestKey : input . requestKey ,
714+ value : input . value ,
715+ } ) ;
716+ return file ;
717+ } ) ,
718+ } ) ,
719+ Layer . mock ( RemoteDelegation . RemoteDelegation ) ( { } ) ,
720+ Layer . mock ( ScheduledTaskService . ScheduledTaskService ) ( { } ) ,
721+ Layer . mock ( ProviderAdapterRegistry . ProviderAdapterRegistryV2 ) ( { } ) ,
722+ NodeCrypto . layer ,
723+ ) ,
724+ ) ,
725+ ) ;
726+ const counted = Metric . snapshot . pipe (
727+ Effect . map ( ( snapshots ) => {
728+ const found = snapshots . find (
729+ ( snapshot ) =>
730+ snapshot . id === "t3_secret_requests_total" &&
731+ snapshot . attributes ?. status === "saved_pasted" ,
732+ ) ;
733+ return found ?. type === "Counter" ? Number ( found . state . count ) : 0 ;
734+ } ) ,
735+ ) ;
736+
737+ await Effect . gen ( function * ( ) {
738+ const service = yield * OrchestratorMcpService . OrchestratorMcpService ;
739+ const before = yield * counted ;
740+ const result = yield * service . requestSecret ( makeScope ( ) , {
741+ label : "Pixel PIN" ,
742+ reason : "Unlocks the test phone." ,
743+ value : "482916" ,
744+ clientRequestId : "pin-1" ,
745+ } ) ;
746+ expect ( result ) . toEqual ( { status : "saved" , file } ) ;
747+ expect ( JSON . stringify ( result ) ) . not . toContain ( "482916" ) ;
748+ expect ( saved ) . toEqual ( [
749+ {
750+ requestNamespace : "provider-session-mcp-orchestrator-detail" ,
751+ requestKey : "pin-1" ,
752+ value : "482916" ,
753+ } ,
754+ ] ) ;
755+ expect ( dispatched ) . toEqual ( [ ] ) ;
756+ expect ( ( yield * counted ) - before ) . toBe ( 1 ) ;
757+
758+ const asRef = yield * service
759+ . requestSecret ( makeScope ( ) , {
760+ label : "Pixel PIN" ,
761+ reason : "Unlocks the test phone." ,
762+ value : "482916" ,
763+ delivery : "secretRef" ,
764+ } )
765+ . pipe ( Effect . flip ) ;
766+ expect ( asRef . code ) . toBe ( "invalid_request" ) ;
767+ expect ( asRef . message ) . not . toContain ( "482916" ) ;
768+ expect ( saved ) . toHaveLength ( 1 ) ;
769+ expect ( dispatched ) . toEqual ( [ ] ) ;
770+ } ) . pipe ( Effect . provide ( layer ) , Effect . runPromise ) ;
771+ } ) ;
772+
773+ it ( "requestSecret returns the unsaved note for a declined, cancelled or timed-out card" , async ( ) => {
774+ const outcomes = [ "declined" , "cancelled" , "pending" ] as const ;
775+ const results = await Effect . forEach ( outcomes , ( outcome ) => {
776+ const dispatched : Array < { readonly type : string ; readonly turnItemId ?: string } > = [ ] ;
777+ const projection = ( ) => {
778+ const card = dispatched . find ( ( command ) => command . type === "secret_request.record" ) ;
779+ return {
780+ thread : baseThread ( {
781+ threadId : parentThreadId ,
782+ title : "Parent" ,
783+ instanceId : parentInstanceId ,
784+ model : "gpt-5.4" ,
785+ } ) ,
786+ runs : [
787+ { ...makeRun ( { id : activeRunId , ordinal : 1 , status : "running" } ) , rootNodeId : taskId } ,
788+ ] ,
789+ visibleTurnItems : [ ] ,
790+ turnItems :
791+ card === undefined
792+ ? [ ]
793+ : [ { type : "secret_request" , id : card . turnItemId , secretStatus : outcome } ] ,
794+ runtimeRequests : [ ] ,
795+ messages : [ ] ,
796+ contextTransfers : [ ] ,
797+ subagents : [ ] ,
798+ updatedAt : now ,
799+ } as unknown as OrchestrationV2ThreadProjection ;
800+ } ;
801+ const layer = layerOrchestratorService . pipe (
802+ Layer . provide (
803+ Layer . mergeAll (
804+ Layer . mock ( ThreadManagementService . ThreadManagementService ) ( {
805+ getThreadRecords : ( ) => Effect . sync ( projection ) ,
806+ getThreadShell : ( ) =>
807+ Effect . sync ( ( ) => projection ( ) . thread as unknown as OrchestrationV2ThreadShell ) ,
808+ dispatch : ( command ) => Effect . sync ( ( ) => void dispatched . push ( command ) as never ) ,
809+ } satisfies Partial < ThreadManagementService . ThreadManagementService [ "Service" ] > ) ,
810+ ServerSettings . layerTest ( ) ,
811+ Layer . mock ( ProviderRegistry . ProviderRegistry ) ( {
812+ getProviders : Effect . succeed ( [ ] ) ,
813+ } satisfies Partial < ProviderRegistry . ProviderRegistry [ "Service" ] > ) ,
814+ Layer . mock ( ProjectService . ProjectService ) ( { } ) ,
815+ Layer . mock ( SecretRequests . SecretRequests ) ( { } ) ,
816+ Layer . mock ( RemoteDelegation . RemoteDelegation ) ( { } ) ,
817+ Layer . mock ( ScheduledTaskService . ScheduledTaskService ) ( { } ) ,
818+ Layer . mock ( ProviderAdapterRegistry . ProviderAdapterRegistryV2 ) ( { } ) ,
819+ NodeCrypto . layer ,
820+ ) ,
821+ ) ,
822+ ) ;
823+ return Effect . gen ( function * ( ) {
824+ const service = yield * OrchestratorMcpService . OrchestratorMcpService ;
825+ const request = yield * service
826+ . requestSecret ( makeScope ( ) , {
827+ label : "Staging password" ,
828+ reason : "Signs in the QA account." ,
829+ delivery : "file" ,
830+ timeoutMs : 1_000 ,
831+ } )
832+ . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
833+ yield * TestClock . adjust ( "2 seconds" ) ;
834+ return yield * Fiber . join ( request ) ;
835+ } ) . pipe ( Effect . provide ( Layer . merge ( layer , TestClock . layer ( ) ) ) ) ;
836+ } ) . pipe ( Effect . runPromise ) ;
837+
838+ expect ( results ) . toEqual ( [
839+ { status : "declined" , note : SECRET_REQUEST_UNSAVED_NOTE . declined } ,
840+ { status : "cancelled" , note : SECRET_REQUEST_UNSAVED_NOTE . cancelled } ,
841+ { status : "timed_out" , note : SECRET_REQUEST_UNSAVED_NOTE . timed_out } ,
842+ ] ) ;
843+ } ) ;
0 commit comments