@@ -5,8 +5,10 @@ import { describe, expect, it } from "@effect/vitest";
55import * as Deferred from "effect/Deferred" ;
66import * as Effect from "effect/Effect" ;
77import * as Fiber from "effect/Fiber" ;
8+ import * as TestClock from "effect/testing/TestClock" ;
89import { vi } from "vite-plus/test" ;
910
11+ import { makeIncomingDecisionShadow } from "./IncomingDecisionShadow.ts" ;
1012import { mcpToolSession , runAcpMcpCliFastPath , runAcpMcpStdioBridge } from "./AcpMcpStdioBridge.ts" ;
1113
1214function makeHarness ( responder : ( request : Request ) => Promise < Response > | Response ) {
@@ -39,6 +41,149 @@ function pendingFetch(started: Deferred.Deferred<AbortSignal | undefined>) {
3941}
4042
4143describe ( "AcpMcpStdioBridge" , ( ) => {
44+ it . effect (
45+ "shadows only matching tool responses before delivery and preserves the delivered prefix" ,
46+ ( ) =>
47+ Effect . gen ( function * ( ) {
48+ const input = new NodeStream . PassThrough ( ) ;
49+ const written : string [ ] = [ ] ;
50+ let decisions = 0 ;
51+ const result = { content : [ { type : "text" , text : "original evidence\n" . repeat ( 30 ) } ] } ;
52+ const incomingDecision = yield * makeIncomingDecisionShadow (
53+ JSON . stringify ( { mode : "shadow" , port : 8009 , model : "test-candidate" , device : "metal" } ) ,
54+ async ( ) => {
55+ decisions ++ ;
56+ expect ( written ) . toHaveLength ( 2 ) ;
57+ return Response . json ( {
58+ model : "test-candidate" ,
59+ answers : {
60+ "0" : {
61+ type : "choice" ,
62+ choice : "drop" ,
63+ confidence : 1 ,
64+ probabilities : { keep : 0 , drop : 1 , abstain : 0 } ,
65+ } ,
66+ } ,
67+ usage : { input_tokens : 1 , output_tokens : 1 } ,
68+ } ) ;
69+ } ,
70+ ) ;
71+ using stderr = vi . spyOn ( process . stderr , "write" ) . mockReturnValue ( true ) ;
72+ const bridge = runAcpMcpStdioBridge ( {
73+ endpoint : "http://127.0.0.1:1/mcp" ,
74+ authorization : "Bearer bridge-test" ,
75+ input,
76+ output : { write : ( chunk ) => written . push ( chunk ) } ,
77+ incomingDecision,
78+ fetchImplementation : async ( _url , init ) => {
79+ const request = JSON . parse ( String ( init ?. body ) ) ;
80+ return Response . json ( [
81+ { jsonrpc : "2.0" , method : "notifications/progress" , params : { unchanged : true } } ,
82+ { jsonrpc : "2.0" , id : 999 , result } ,
83+ { jsonrpc : "2.0" , id : request . id , result } ,
84+ ] ) ;
85+ } ,
86+ } ) ;
87+ input . end ( '{"jsonrpc":"2.0","id":1,"method":"tools/call"}\n' ) ;
88+ yield * bridge ;
89+ expect ( decisions ) . toBe ( 1 ) ;
90+ expect ( written ) . toEqual ( [
91+ JSON . stringify ( {
92+ jsonrpc : "2.0" ,
93+ method : "notifications/progress" ,
94+ params : { unchanged : true } ,
95+ } ) + "\n" ,
96+ JSON . stringify ( { jsonrpc : "2.0" , id : 999 , result } ) + "\n" ,
97+ JSON . stringify ( { jsonrpc : "2.0" , id : 1 , result } ) + "\n" ,
98+ ] ) ;
99+ expect ( stderr ) . toHaveBeenCalledWith ( expect . stringContaining ( '"applied":false' ) ) ;
100+ expect ( stderr . mock . calls . flat ( ) . join ( "" ) ) . not . toContain ( "original evidence" ) ;
101+ } ) ,
102+ ) ;
103+
104+ it . effect . each ( [ "decision" , "malformed" , "timeout" ] as const ) (
105+ "preserves the provider prefix through %s selection" ,
106+ ( outcome ) =>
107+ Effect . gen ( function * ( ) {
108+ const input = new NodeStream . PassThrough ( ) ;
109+ const written : string [ ] = [ ] ;
110+ const started = yield * Deferred . make < void > ( ) ;
111+ const first = `${ "é evidence " . repeat ( 30 ) } \n\n` ;
112+ const result = {
113+ meta : { x : 1 } ,
114+ content : [
115+ { type : "text" , text : first + "remove\n\n" , extra : 2 } ,
116+ { type : "text" , text : "留める" } ,
117+ ] ,
118+ } ;
119+ const incomingDecision = yield * makeIncomingDecisionShadow (
120+ JSON . stringify ( { mode : "shadow" , port : 8009 , model : "test" , device : "metal" } ) ,
121+ async ( ) => {
122+ if ( outcome === "timeout" ) {
123+ Deferred . doneUnsafe ( started , Effect . void ) ;
124+ return new Promise < Response > ( ( ) => { } ) ;
125+ }
126+ return Response . json ( {
127+ model : "test" ,
128+ answers : Object . fromEntries (
129+ [ "keep" , "drop" , "abstain" ] . map ( ( choice , id ) => [
130+ outcome === "malformed" && id === 1 ? "unknown-block" : String ( id ) ,
131+ {
132+ type : "choice" ,
133+ choice,
134+ confidence : 0.5 ,
135+ probabilities : { keep : 0.3 , drop : 0.3 , abstain : 0.4 } ,
136+ } ,
137+ ] ) ,
138+ ) ,
139+ usage : { input_tokens : 1 , output_tokens : 1 } ,
140+ } ) ;
141+ } ,
142+ "test-select" ,
143+ ) ;
144+ using _stderr = vi . spyOn ( process . stderr , "write" ) . mockReturnValue ( true ) ;
145+ const bridge = runAcpMcpStdioBridge ( {
146+ endpoint : "http://127.0.0.1:1/mcp" ,
147+ authorization : "test" ,
148+ input,
149+ output : { write : ( chunk ) => written . push ( chunk ) } ,
150+ incomingDecision,
151+ fetchImplementation : async ( _url , init ) => {
152+ const request = JSON . parse ( String ( init ?. body ) ) ;
153+ return Response . json ( [
154+ { jsonrpc : "2.0" , method : "prefix" , params : { text : "prior" } } ,
155+ { jsonrpc : "2.0" , id : request . id , result, extra : true } ,
156+ ] ) ;
157+ } ,
158+ } ) ;
159+ input . end ( '{"jsonrpc":"2.0","id":1,"method":"tools/call"}\n' ) ;
160+ const fiber = yield * Effect . forkChild ( bridge ) ;
161+ if ( outcome === "timeout" ) {
162+ yield * Deferred . await ( started ) ;
163+ yield * TestClock . adjust ( "1 second" ) ;
164+ }
165+ yield * Fiber . join ( fiber ) ;
166+ expect ( written ) . toEqual ( [
167+ JSON . stringify ( { jsonrpc : "2.0" , method : "prefix" , params : { text : "prior" } } ) + "\n" ,
168+ JSON . stringify ( {
169+ jsonrpc : "2.0" ,
170+ id : 1 ,
171+ result :
172+ outcome === "decision"
173+ ? {
174+ ...result ,
175+ content : [ { ...result . content [ 0 ] , text : first } , result . content [ 1 ] ] ,
176+ }
177+ : result ,
178+ extra : true ,
179+ } ) + "\n" ,
180+ ] ) ;
181+ if ( outcome === "timeout" ) {
182+ expect ( _stderr ) . toHaveBeenCalledWith ( expect . stringContaining ( '"elapsedMs":1000' ) ) ;
183+ }
184+ } ) ,
185+ ) ;
186+
42187 it ( "prints structured results once in the terminal while retaining other content and errors" , async ( ) => {
43188 const originalEnvironment = process . env ;
44189 using _environment = {
0 commit comments