@@ -5,6 +5,7 @@ import * as Queue from "effect/Queue";
55import * as Schema from "effect/Schema" ;
66import * as Stdio from "effect/Stdio" ;
77import * as Stream from "effect/Stream" ;
8+ import * as TestClock from "effect/testing/TestClock" ;
89
910import * as NodeServices from "@effect/platform-node/NodeServices" ;
1011import { assert , it } from "@effect/vitest" ;
@@ -644,4 +645,49 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
644645 assert . notInclude ( error . message , secret ) ;
645646 } ) ,
646647 ) ;
648+
649+ it . effect ( "fails pending requests when input ends while the process is still running" , ( ) =>
650+ Effect . gen ( function * ( ) {
651+ const { stdio, input, output } = yield * makeInMemoryStdio ( ) ;
652+ const transport = yield * CodexProtocol . makeCodexAppServerPatchedProtocol ( {
653+ stdio,
654+ terminationError : Effect . never ,
655+ } ) ;
656+ const pending = yield * transport . request ( "thread/read" , { } ) . pipe ( Effect . forkScoped ) ;
657+ yield * Queue . take ( output ) ;
658+
659+ yield * Queue . end ( input ) ;
660+ yield * TestClock . adjust ( "249 millis" ) ;
661+ assert . isUndefined ( pending . pollUnsafe ( ) ) ;
662+ yield * TestClock . adjust ( "1 millis" ) ;
663+
664+ const failure = yield * Fiber . join ( pending ) . pipe (
665+ Effect . match ( {
666+ onFailure : ( error ) => error ,
667+ onSuccess : ( ) => assert . fail ( "Expected the pending request to fail" ) ,
668+ } ) ,
669+ ) ;
670+ assert . instanceOf ( failure , CodexError . CodexAppServerInputStreamEndedError ) ;
671+ } ) ,
672+ ) ;
673+
674+ it . effect ( "reports a process exit that follows input end" , ( ) =>
675+ Effect . gen ( function * ( ) {
676+ const { stdio, input } = yield * makeInMemoryStdio ( ) ;
677+ const exitError = new CodexError . CodexAppServerProcessExitedError ( { code : 1 , pid : 123 } ) ;
678+ const exited = yield * Deferred . make < CodexError . CodexAppServerError > ( ) ;
679+ const termination = yield * Deferred . make < CodexError . CodexAppServerError > ( ) ;
680+ yield * CodexProtocol . makeCodexAppServerPatchedProtocol ( {
681+ stdio,
682+ terminationError : Deferred . await ( exited ) ,
683+ onTermination : ( error ) => Deferred . succeed ( termination , error ) . pipe ( Effect . asVoid ) ,
684+ } ) ;
685+
686+ yield * Queue . end ( input ) ;
687+ yield * TestClock . adjust ( "100 millis" ) ;
688+ yield * Deferred . succeed ( exited , exitError ) ;
689+
690+ assert . strictEqual ( yield * Deferred . await ( termination ) , exitError ) ;
691+ } ) ,
692+ ) ;
647693} ) ;
0 commit comments