11import { ensureDir } from "@effectionx/fs" ;
2- import { exec , Stdio } from "@effectionx/process" ;
2+ import { Stdio } from "@effectionx/process" ;
33import {
44 capture ,
55 content ,
@@ -13,15 +13,7 @@ import {
1313import { Plugin } from "@executablemd/core/api" ;
1414import { command as xmdCommand , cwd , parseDuration , realpath } from "@executablemd/runtime" ;
1515import { Test } from "@executablemd/testing" ;
16- import {
17- createContext ,
18- ensure ,
19- type Operation ,
20- race ,
21- spawn ,
22- withResolvers ,
23- type Yielded ,
24- } from "effection" ;
16+ import { createContext , ensure , type Operation , type Yielded } from "effection" ;
2517import { randomUUID } from "node:crypto" ;
2618import { extname , join , resolve , sep } from "node:path" ;
2719import { fileURLToPath } from "node:url" ;
@@ -32,16 +24,15 @@ import {
3224 exitResult ,
3325 point ,
3426 region ,
35- response ,
3627 screenshotName ,
3728 size ,
3829 type Command ,
39- type Launch ,
4030 type Region ,
4131 type Size ,
4232} from "./lib/protocol.ts" ;
4333import { assertPinnedWorktree , prepareRun , root , type PreparedBuild } from "./lib/run.ts" ;
4434import { attachSidekick } from "./lib/sidekick.ts" ;
35+ import { connectDriver } from "./lib/driver-client.ts" ;
4536
4637type Execute = ( command : Command ) => Operation < unknown > ;
4738
@@ -131,68 +122,6 @@ const bench = size.partial().extend({
131122 allowAbsolute : z . boolean ( ) . default ( false ) ,
132123} ) ;
133124
134- function * connect ( options : Launch ) {
135- const script = fileURLToPath ( new URL ( "./driver.ts" , import . meta. url ) ) ;
136- const child = yield * exec ( "bun" , {
137- cwd : root ,
138- arguments : [ "--no-install" , script , JSON . stringify ( options ) ] ,
139- } ) ;
140- const decoder = new TextDecoder ( ) ;
141- let pending = "" ;
142- let pendingReply = withResolvers < z . infer < typeof response > > ( ) ;
143- let busy = false ;
144- let driverEnded = false ;
145- yield * spawn ( function * ( ) {
146- const stream = yield * child . stdout ;
147- while ( true ) {
148- const part = yield * stream . next ( ) ;
149- if ( part . done ) {
150- return ;
151- }
152- pending += decoder . decode ( part . value , { stream : true } ) ;
153- const lines = pending . split ( "\n" ) ;
154- pending = lines . pop ( ) ?? "" ;
155- for ( const line of lines ) {
156- pendingReply . resolve ( response . parse ( JSON . parse ( line ) ) ) ;
157- }
158- }
159- } ) ;
160- const ended = yield * spawn ( function * ( ) {
161- const { code, signal } = yield * child . join ( ) ;
162- driverEnded = true ;
163- return { error : `Driver exited (${ code ?? signal } ). Evidence: ${ options . directory } ` } ;
164- } ) ;
165- let stopRequested = false ;
166- function * close ( ) {
167- if ( ! driverEnded && ! stopRequested ) {
168- stopRequested = true ;
169- child . stdin . send ( `${ JSON . stringify ( { action : "stop" } ) } \n` ) ;
170- }
171- return yield * child . join ( ) ;
172- }
173- yield * ensure ( function * ( ) {
174- yield * close ( ) ;
175- } ) ;
176- const dispatch : Execute = function * ( command ) {
177- if ( busy ) {
178- throw new Error ( "ReplBench actions run one at a time" ) ;
179- }
180- busy = true ;
181- try {
182- pendingReply = withResolvers < z . infer < typeof response > > ( ) ;
183- child . stdin . send ( `${ JSON . stringify ( command ) } \n` ) ;
184- const result = yield * race ( [ pendingReply . operation , ended ] ) ;
185- if ( "error" in result ) {
186- throw new Error ( result . error ) ;
187- }
188- return result . value ;
189- } finally {
190- busy = false ;
191- }
192- } ;
193- return { dispatch, close } ;
194- }
195-
196125const Terminal : FunctionComponent = function * ( props , invocation ) {
197126 if ( ! invocation . hasContent ( ) ) {
198127 throw new Error ( "Use a paired <Terminal>" ) ;
@@ -357,7 +286,7 @@ const XmdRepl: FunctionComponent = function* (props, invocation) {
357286 } ,
358287 { at : "min" } ,
359288 ) ;
360- const { dispatch, close } = yield * connect ( launch ) ;
289+ const { dispatch, close } = yield * connectDriver ( launch ) ;
361290 yield * dispatch ( { action : "ready" } ) ;
362291 let sidekick : Yielded < ReturnType < typeof attachSidekick > > | undefined ;
363292 if ( attachment !== undefined ) {
0 commit comments