@@ -105,6 +105,39 @@ export default {
105105 sharingModel: 'private',
106106 fields: { title: { type: 'text', label: 'Title' } },
107107 }],
108+ // A host plugin that writes through every console channel from a tool-call
109+ // interceptor — the seam a host audits its calls through. Inert for the
110+ // #7915 case below, which makes no tool call. FX_HOLD_FOR_CALL is set only by
111+ // the console describe: it holds the boot (and so serve's boot-quiet window)
112+ // open until a call has been served, so the call provably lands INSIDE the
113+ // window instead of racing it.
114+ plugins: [{
115+ name: 'fx.console-probe',
116+ version: '1.0.0',
117+ init: async () => {
118+ console.log('[fx-boot-chatter] from init');
119+ },
120+ start: async (ctx) => {
121+ let served;
122+ const firstCall = new Promise((resolveCall) => { served = resolveCall; });
123+ ctx.getService('mcp').setToolInterceptor(async (call, next) => {
124+ const tag = call.transport + ':' + call.toolName;
125+ console.log('[fx-call] console.log ' + tag);
126+ console.info('[fx-call] console.info ' + tag);
127+ console.debug('[fx-call] console.debug ' + tag);
128+ console.warn('[fx-call] console.warn ' + tag);
129+ console.error('[fx-call] console.error ' + tag);
130+ process.stdout.write('[fx-call] process.stdout.write ' + tag + String.fromCharCode(10));
131+ served();
132+ return next();
133+ });
134+ if (process.env.FX_HOLD_FOR_CALL === '1') {
135+ ctx.hook('kernel:ready', async () => {
136+ await Promise.race([firstCall, new Promise((r) => setTimeout(r, 30000))]);
137+ });
138+ }
139+ },
140+ }],
108141};
109142` ;
110143
@@ -304,90 +337,91 @@ function parseFrame(line: string): Record<string, unknown> | undefined {
304337 }
305338}
306339
307- describe ( '#7915: a stdio MCP boot writes nothing but protocol frames to stdout' , ( ) => {
308- beforeAll ( async ( ) => {
309- // Build prerequisite first: the spawns below resolve `serve` from `dist/`.
310- requireBuiltCli ( RUN_JS_RESOLVES_FROM_DIST ) ;
311-
312- dir = mkdtempSync ( join ( tmpdir ( ) , 'mcp-stdout-purity-e2e-' ) ) ;
313- writeFileSync ( join ( dir , 'objectstack.config.ts' ) , CONFIG , 'utf8' ) ;
314- writeFileSync (
315- join ( dir , 'package.json' ) ,
316- JSON . stringify ( { name : 'mcp-stdout-purity-e2e-fixture' , private : true , type : 'module' } , null , 2 ) ,
317- 'utf8' ,
340+ // Shared by both describes below: one minting boot, one key, one database.
341+ beforeAll ( async ( ) => {
342+ // Build prerequisite first: the spawns below resolve `serve` from `dist/`.
343+ requireBuiltCli ( RUN_JS_RESOLVES_FROM_DIST ) ;
344+
345+ dir = mkdtempSync ( join ( tmpdir ( ) , 'mcp-stdout-purity-e2e-' ) ) ;
346+ writeFileSync ( join ( dir , 'objectstack.config.ts' ) , CONFIG , 'utf8' ) ;
347+ writeFileSync (
348+ join ( dir , 'package.json' ) ,
349+ JSON . stringify ( { name : 'mcp-stdout-purity-e2e-fixture' , private : true , type : 'module' } , null , 2 ) ,
350+ 'utf8' ,
351+ ) ;
352+
353+ // Boot 1 — mint a real key through the product route, against a FILE db so
354+ // boot 2 sees the same row (`:memory:` would not survive the restart).
355+ const first = await boot ( { OS_DATABASE_URL : join ( dir , 'probe.db' ) } , / S e r v e r i s r e a d y / ) ;
356+ const base = `http://localhost:${ first . port } /api/v1` ;
357+
358+ // ⭐ #15898 — the fifth and last file of the family #15653 repaired.
359+ // `boot()`'s `child.on('exit')` handler feeds the READINESS promise ONLY — it
360+ // returns early once `settled` — so a death AFTER readiness is invisible to
361+ // it, and the two requests below used to reach vitest as a bare
362+ // `TypeError: fetch failed` with no exit code, no stdout and no stderr: the
363+ // unattributable failure #15545 recorded once and could not diagnose.
364+ // `probeThroughChild()` is where the child's fate is read instead; its own
365+ // section in `helpers/serve-process.ts` carries the measurement, the bound
366+ // and the fences (⛔ no skip, no quarantine, no timeout bump).
367+ //
368+ // Read at THROW time, so a failure carries what the child printed on its way
369+ // down rather than the buffer as it stood at ready.
370+ const transcript = ( ) =>
371+ `\n--- child stdout ---\n${ first . stdout ( ) } \n--- child stderr ---\n${ first . stderr ( ) } ` ;
372+ const probe = < T > ( what : string , request : ( ) => Promise < T > ) : Promise < T > =>
373+ probeThroughChild (
374+ {
375+ child : first . child ,
376+ transcript,
377+ label : 'serve-stdio-stdout-purity' ,
378+ what : `${ what } on port ${ first . port } ` ,
379+ } ,
380+ request ,
318381 ) ;
319382
320- // Boot 1 — mint a real key through the product route, against a FILE db so
321- // boot 2 sees the same row (`:memory:` would not survive the restart).
322- const first = await boot ( { OS_DATABASE_URL : join ( dir , 'probe.db' ) } , / S e r v e r i s r e a d y / ) ;
323- const base = `http://localhost:${ first . port } /api/v1` ;
324-
325- // ⭐ #15898 — the fifth and last file of the family #15653 repaired.
326- // `boot()`'s `child.on('exit')` handler feeds the READINESS promise ONLY — it
327- // returns early once `settled` — so a death AFTER readiness is invisible to
328- // it, and the two requests below used to reach vitest as a bare
329- // `TypeError: fetch failed` with no exit code, no stdout and no stderr: the
330- // unattributable failure #15545 recorded once and could not diagnose.
331- // `probeThroughChild()` is where the child's fate is read instead; its own
332- // section in `helpers/serve-process.ts` carries the measurement, the bound
333- // and the fences (⛔ no skip, no quarantine, no timeout bump).
334- //
335- // Read at THROW time, so a failure carries what the child printed on its way
336- // down rather than the buffer as it stood at ready.
337- const transcript = ( ) =>
338- `\n--- child stdout ---\n${ first . stdout ( ) } \n--- child stderr ---\n${ first . stderr ( ) } ` ;
339- const probe = < T > ( what : string , request : ( ) => Promise < T > ) : Promise < T > =>
340- probeThroughChild (
341- {
342- child : first . child ,
343- transcript,
344- label : 'serve-stdio-stdout-purity' ,
345- what : `${ what } on port ${ first . port } ` ,
346- } ,
347- request ,
348- ) ;
349-
350- // ⛔ The body read is INSIDE the thunk and cannot throw: a connection torn
351- // down mid-body rejects out of `res.json()` rather than out of `fetch()`, and
352- // the guard reads any throw as a transport failure — so an ASSERTION must
353- // never live in here or a wrong answer would be reported as a dropped socket.
354- const signIn = await probe ( 'the sign-in probe' , async ( ) => {
355- const res = await fetch ( `${ base } /auth/sign-in/email` , {
356- method : 'POST' ,
357- headers : { 'content-type' : 'application/json' } ,
358- // `serve --dev` seeds this admin on an empty DB.
359- body : JSON . stringify ( { email : 'admin@objectos.ai' , password : 'admin123' } ) ,
360- } ) ;
361- let body : unknown = null ;
362- try { body = await res . json ( ) ; } catch { /* non-JSON error body */ }
363- return { status : res . status , body } ;
383+ // ⛔ The body read is INSIDE the thunk and cannot throw: a connection torn
384+ // down mid-body rejects out of `res.json()` rather than out of `fetch()`, and
385+ // the guard reads any throw as a transport failure — so an ASSERTION must
386+ // never live in here or a wrong answer would be reported as a dropped socket.
387+ const signIn = await probe ( 'the sign-in probe' , async ( ) => {
388+ const res = await fetch ( `${ base } /auth/sign-in/email` , {
389+ method : 'POST' ,
390+ headers : { 'content-type' : 'application/json' } ,
391+ // `serve --dev` seeds this admin on an empty DB.
392+ body : JSON . stringify ( { email : 'admin@objectos.ai' , password : 'admin123' } ) ,
364393 } ) ;
365- expect ( signIn . status ) . toBe ( 200 ) ;
366- const token = ( signIn . body as { token ?: string } | null ) ?. token ;
367- expect ( token ) . toBeTruthy ( ) ;
368-
369- const minted = await probe ( 'the key-mint probe' , async ( ) => {
370- const res = await fetch ( ` ${ base } /keys` , {
371- method : 'POST' ,
372- headers : { 'content-type' : 'application/json' , authorization : `Bearer ${ token } ` } ,
373- body : JSON . stringify ( { name : 'mcp-stdout-purity-e2e' } ) ,
374- } ) ;
375- let body : unknown = null ;
376- try { body = await res . json ( ) ; } catch { /* non-JSON error body */ }
377- return { status : res . status , body } ;
394+ let body : unknown = null ;
395+ try { body = await res . json ( ) ; } catch { /* non-JSON error body */ }
396+ return { status : res . status , body } ;
397+ } ) ;
398+ expect ( signIn . status ) . toBe ( 200 ) ;
399+ const token = ( signIn . body as { token ?: string } | null ) ?. token ;
400+ expect ( token ) . toBeTruthy ( ) ;
401+
402+ const minted = await probe ( 'the key-mint probe' , async ( ) => {
403+ const res = await fetch ( ` ${ base } /keys` , {
404+ method : 'POST' ,
405+ headers : { 'content-type' : 'application/ json' , authorization : `Bearer ${ token } ` } ,
406+ body : JSON . stringify ( { name : 'mcp-stdout-purity-e2e' } ) ,
378407 } ) ;
379- expect ( minted . status ) . toBe ( 201 ) ;
380- apiKey = String ( ( minted . body as { data : { key : string } } ) . data . key ) ;
381- expect ( apiKey . startsWith ( 'osk_' ) ) . toBe ( true ) ;
408+ let body : unknown = null ;
409+ try { body = await res . json ( ) ; } catch { /* non-JSON error body */ }
410+ return { status : res . status , body } ;
411+ } ) ;
412+ expect ( minted . status ) . toBe ( 201 ) ;
413+ apiKey = String ( ( minted . body as { data : { key : string } } ) . data . key ) ;
414+ expect ( apiKey . startsWith ( 'osk_' ) ) . toBe ( true ) ;
382415
383- await stop ( first . child ) ;
384- } , 240_000 ) ;
416+ await stop ( first . child ) ;
417+ } , 240_000 ) ;
385418
386- afterAll ( async ( ) => {
387- for ( const child of children ) await stop ( child ) ;
388- if ( dir ) rmSync ( dir , { recursive : true , force : true } ) ;
389- } , 60_000 ) ;
419+ afterAll ( async ( ) => {
420+ for ( const child of children ) await stop ( child ) ;
421+ if ( dir ) rmSync ( dir , { recursive : true , force : true } ) ;
422+ } , 60_000 ) ;
390423
424+ describe ( '#7915: a stdio MCP boot writes nothing but protocol frames to stdout' , ( ) => {
391425 it ( 'keeps stdout free of every non-frame byte, and keeps the diagnostics on stderr' , async ( ) => {
392426 // Wait for the banner's LAST line, not for `[MCP] Server started`. The
393427 // transport attaches inside `runtime.start()`, which the banner follows —
@@ -495,3 +529,158 @@ describe('#7915: a stdio MCP boot writes nothing but protocol frames to stdout',
495529 await stop ( booted . child ) ;
496530 } , 240_000 ) ;
497531} ) ;
532+
533+ /**
534+ * #32942 — with the stdio MCP transport on, what a host plugin writes to the
535+ * console while SERVING a call reaches stderr, never stdout.
536+ *
537+ * ## The defect, measured on `origin/main` before the fix
538+ *
539+ * A stdio client writes `initialize` at spawn and its first `tools/call` the
540+ * moment that is answered. The transport attaches inside `runtime.start()`, so
541+ * those calls are served while `serve`'s boot-quiet window is still open — and
542+ * the window dropped `console.log` / `console.debug` outright and filtered
543+ * `process.stdout.write` (so `console.info` too) down to kernel-logger records.
544+ * A host interceptor's audit line therefore reached NEITHER stream, while
545+ * `console.warn` / `console.error` (stderr) and the same plugin over HTTP were
546+ * fine. A plugin that logs from a timer after the banner never showed it: the
547+ * defect is the window, not the redirect.
548+ *
549+ * ## What is pinned
550+ *
551+ * 1. under stdio, a call served inside the window — `FX_HOLD_FOR_CALL` holds
552+ * the boot open until it is, and the line is asserted to precede the
553+ * banner — puts every console channel's line on stderr;
554+ * 2. stdout carries nothing but JSON-RPC frames, every line parsed;
555+ * 3. the window still holds back the boot's own chatter up to the claim
556+ * (`[fx-boot-chatter]` from `init()`), on both streams;
557+ * 4. CONTROL: an HTTP boot (no stdio claim) keeps its window as before — the
558+ * boot chatter held back, a post-boot HTTP call's lines on stderr, stdout
559+ * empty.
560+ *
561+ * `OS_LOG_LEVEL: 'warn'` on both boots: the #7915 case runs at `info`, which is
562+ * a verbose level, and at a verbose level `serve` never opens the window — the
563+ * defect would be unreachable.
564+ */
565+ describe ( '#32942: a stdio call\'s console output reaches stderr, never stdout, even inside the boot-quiet window' , ( ) => {
566+ /** Every console channel the interceptor writes through, as it tags them. */
567+ const CHANNELS = [ 'console.log' , 'console.info' , 'console.debug' , 'console.warn' , 'console.error' , 'process.stdout.write' ] ;
568+
569+ it ( 'stdio: the call\'s lines are on stderr before the banner, stdout is frames only, boot chatter still held' , async ( ) => {
570+ const child = spawn ( process . execPath , [ CLI , 'serve' , '-p' , KERNEL_ASSIGNED_PORT , '--dev' ] , {
571+ cwd : dir ,
572+ stdio : [ 'pipe' , 'pipe' , 'pipe' ] ,
573+ env : childEnv ( {
574+ NO_COLOR : '1' ,
575+ OS_LOG_LEVEL : 'warn' ,
576+ OS_DISABLE_CONSOLE : '1' ,
577+ OS_SECRET_KEY : E2E_SECRET_KEY ,
578+ NODE_ENV : undefined ,
579+ OS_DATABASE_URL : join ( dir , 'probe.db' ) ,
580+ OS_MCP_STDIO_ENABLED : 'true' ,
581+ OS_MCP_STDIO_API_KEY : apiKey ,
582+ FX_HOLD_FOR_CALL : '1' ,
583+ } ) ,
584+ } ) as ChildProcessWithoutNullStreams ;
585+ children . push ( child ) ;
586+
587+ let stdout = '' ;
588+ let stderr = '' ;
589+ let called = false ;
590+ const send = ( msg : unknown ) => child . stdin . write ( `${ JSON . stringify ( msg ) } \n` ) ;
591+ const ready = new Promise < boolean > ( ( settle ) => {
592+ const timer = setTimeout ( ( ) => settle ( false ) , 150_000 ) ;
593+ child . stdout . on ( 'data' , ( d ) => {
594+ stdout += String ( d ) ;
595+ // The SDK client's shape: the first call goes out the moment the
596+ // `initialize` answer is read — the banner has not printed yet.
597+ if ( ! called && stdout . split ( '\n' ) . slice ( 0 , - 1 ) . some ( ( line ) => parseFrame ( line ) ?. id === 1 ) ) {
598+ called = true ;
599+ send ( { jsonrpc : '2.0' , method : 'notifications/initialized' } ) ;
600+ send ( { jsonrpc : '2.0' , id : 2 , method : 'tools/call' , params : { name : 'list_objects' , arguments : { } } } ) ;
601+ }
602+ } ) ;
603+ child . stderr . on ( 'data' , ( d ) => {
604+ stderr += String ( d ) ;
605+ if ( / P r e s s C t r l \+ C t o s t o p / . test ( stderr ) ) { clearTimeout ( timer ) ; settle ( true ) ; }
606+ } ) ;
607+ child . on ( 'exit' , ( ) => { clearTimeout ( timer ) ; settle ( false ) ; } ) ;
608+ } ) ;
609+ // AT SPAWN, as an MCP client does.
610+ send ( {
611+ jsonrpc : '2.0' ,
612+ id : 1 ,
613+ method : 'initialize' ,
614+ params : { protocolVersion : '2024-11-05' , capabilities : { } , clientInfo : { name : 'objectstack-e2e' , version : '0.0.0' } } ,
615+ } ) ;
616+ const booted = await ready ;
617+ await new Promise ( ( r ) => setTimeout ( r , 750 ) ) ;
618+ const seen = `\n--- stdout ---\n${ stdout . slice ( 0 , 4000 ) } \n--- stderr ---\n${ stderr . slice ( - 6000 ) } ` ;
619+ expect ( booted , `serve never printed its banner${ seen } ` ) . toBe ( true ) ;
620+
621+ // The call was answered on the channel.
622+ const lines = stdout . split ( '\n' ) . slice ( 0 , - 1 ) . filter ( ( line ) => line . length > 0 ) ;
623+ const answer = lines . map ( parseFrame ) . find ( ( msg ) => msg ?. id === 2 ) ;
624+ expect ( answer , `list_objects was never answered${ seen } ` ) . toBeTruthy ( ) ;
625+ expect ( answer ! . error , `list_objects answered an error${ seen } ` ) . toBeUndefined ( ) ;
626+
627+ // 2. stdout: every line a JSON-RPC frame.
628+ expect ( lines . filter ( ( line ) => parseFrame ( line ) === undefined ) , `stdout carries non-frame lines${ seen } ` ) . toEqual ( [ ] ) ;
629+
630+ // 1. every channel's line is on stderr — and ahead of the banner, so it was
631+ // written while the window was open rather than after it closed.
632+ const banner = stderr . indexOf ( 'Server is ready' ) ;
633+ for ( const channel of CHANNELS ) {
634+ const at = stderr . indexOf ( `[fx-call] ${ channel } stdio:list_objects` ) ;
635+ expect ( at , `the ${ channel } line of the stdio call is missing from stderr${ seen } ` ) . toBeGreaterThanOrEqual ( 0 ) ;
636+ expect ( at , `the ${ channel } line landed after the banner, so the call missed the window${ seen } ` ) . toBeLessThan ( banner ) ;
637+ }
638+
639+ // 3. the boot's own chatter, before the transport claimed stdout, is still held back.
640+ expect ( stderr + stdout , `the window no longer holds back boot chatter${ seen } ` ) . not . toContain ( '[fx-boot-chatter]' ) ;
641+
642+ await stop ( child ) ;
643+ } , 240_000 ) ;
644+
645+ it ( 'CONTROL — HTTP: the window holds boot chatter as before, a call\'s lines reach stderr, stdout stays empty' , async ( ) => {
646+ const booted = await boot (
647+ { OS_LOG_LEVEL : 'warn' , OS_DATABASE_URL : join ( dir , 'probe.db' ) } ,
648+ / P r e s s C t r l \+ C t o s t o p / ,
649+ ) ;
650+ const url = `http://localhost:${ booted . port } /api/v1/mcp` ;
651+ const call = await probeThroughChild (
652+ {
653+ child : booted . child ,
654+ transcript : ( ) => `\n--- child stdout ---\n${ booted . stdout ( ) } \n--- child stderr ---\n${ booted . stderr ( ) } ` ,
655+ label : 'serve-stdio-stdout-purity' ,
656+ what : `the HTTP list_objects call on port ${ booted . port } ` ,
657+ } ,
658+ async ( ) => {
659+ const res = await fetch ( url , {
660+ method : 'POST' ,
661+ headers : {
662+ 'content-type' : 'application/json' ,
663+ accept : 'application/json, text/event-stream' ,
664+ authorization : `Bearer ${ apiKey } ` ,
665+ } ,
666+ body : JSON . stringify ( { jsonrpc : '2.0' , id : 2 , method : 'tools/call' , params : { name : 'list_objects' , arguments : { } } } ) ,
667+ } ) ;
668+ let text = '' ;
669+ try { text = await res . text ( ) ; } catch { /* torn-down body */ }
670+ return { status : res . status , text } ;
671+ } ,
672+ ) ;
673+ await new Promise ( ( r ) => setTimeout ( r , 750 ) ) ;
674+ const seen = `\n--- HTTP ${ call . status } ${ call . text . slice ( 0 , 400 ) } \n--- stdout ---\n${ booted . stdout ( ) . slice ( 0 , 2000 ) } \n--- stderr ---\n${ booted . stderr ( ) . slice ( - 6000 ) } ` ;
675+ expect ( call . status , `the HTTP call failed${ seen } ` ) . toBe ( 200 ) ;
676+
677+ const stderr = booted . stderr ( ) ;
678+ for ( const channel of CHANNELS ) {
679+ expect ( stderr , `the ${ channel } line of the HTTP call is missing from stderr${ seen } ` ) . toContain ( `[fx-call] ${ channel } http:list_objects` ) ;
680+ }
681+ expect ( booted . stdout ( ) , `an HTTP boot wrote to stdout${ seen } ` ) . toBe ( '' ) ;
682+ expect ( stderr + booted . stdout ( ) , `the window no longer holds back boot chatter${ seen } ` ) . not . toContain ( '[fx-boot-chatter]' ) ;
683+
684+ await stop ( booted . child ) ;
685+ } , 240_000 ) ;
686+ } ) ;
0 commit comments