@@ -1243,48 +1243,85 @@ describe("Tier EP — the execution protocol", () => {
12431243 * entrypoint a trusted host has.
12441244 *
12451245 * No session, no entry and no CLI: `executeInstalled` is the whole of the
1246- * seam, and what is proved is the stop itself. The document has already
1247- * printed something and is waiting on a real operation; its own installation
1248- * holds a cleanup gate, so the Stop cannot answer until that teardown has
1249- * been joined. What the journal keeps is the protocol's existing cancelled
1250- * Close — one of them, carrying a status and nothing else — and the
1251- * completion is not a success. Nothing is invented to stand in for the
1252- * answer the document never gave.
1246+ * seam, and what is proved is the stop itself. The document really has
1247+ * printed something — a prefix this row reads off `execution.output` before
1248+ * it asks for anything — and is then waiting on a real operation; its own
1249+ * installation holds a cleanup gate, so the Stop cannot answer until that
1250+ * teardown has been joined.
1251+ *
1252+ * What a stop owes that partial text is the discriminating part: the output
1253+ * channel closes with exactly what was emitted before the document stopped.
1254+ * A stop that threw the prefix away, or that closed the channel empty,
1255+ * passes every other assertion here and fails that one.
1256+ *
1257+ * What the journal keeps is the protocol's existing cancelled Close — one of
1258+ * them, carrying a status and nothing else — and the completion is not a
1259+ * success. Nothing is invented to stand in for the answer the document never
1260+ * gave, and asking again adds neither output nor terminal.
12531261 */
12541262 it ( "EP30: a host Stop reaches a running document, joins its cleanup and retains one cancelled close" , function * ( ) {
12551263 const stream = new InMemoryStream ( ) ;
12561264 const reached = withResolvers < void > ( ) ;
12571265 const waiting = withResolvers < void > ( ) ;
12581266 const releaseCleanup = withResolvers < void > ( ) ;
12591267 const order : string [ ] = [ ] ;
1268+ /** Body text the document renders before it reaches anything that blocks. */
1269+ const PREFIX = "PARTIAL-OUTPUT-BEFORE-STOP" ;
12601270
12611271 yield * scoped ( function * ( ) {
1262- const execution = yield * executeInstalled ( { ...inlineSource ( "<Waiting />\n" ) , stream } , [
1263- {
1264- * install ( ) : Operation < void > {
1265- // Registered first, so it unwinds last: the Stop below cannot
1266- // answer until this has been released and joined.
1267- yield * ensure ( function * ( ) : Operation < void > {
1268- order . push ( "cleanup:begin" ) ;
1269- yield * releaseCleanup . operation ;
1270- order . push ( "cleanup:end" ) ;
1271- } ) ;
1272- yield * registerComponents ( [
1273- {
1274- name : "Waiting" ,
1275- origin : "execution-protocol" ,
1276- props : { type : "object" , properties : { } , additionalProperties : false } ,
1277- * fn ( ) : Operation < string > {
1278- reached . resolve ( ) ;
1279- yield * waiting . operation ;
1280- return "unreachable" ;
1272+ const execution = yield * executeInstalled (
1273+ { ...inlineSource ( `${ PREFIX } \n\n<Waiting />\n` ) , stream } ,
1274+ [
1275+ {
1276+ * install ( ) : Operation < void > {
1277+ // Registered first, so it unwinds last: the Stop below cannot
1278+ // answer until this has been released and joined.
1279+ yield * ensure ( function * ( ) : Operation < void > {
1280+ order . push ( "cleanup:begin" ) ;
1281+ yield * releaseCleanup . operation ;
1282+ order . push ( "cleanup:end" ) ;
1283+ } ) ;
1284+ yield * registerComponents ( [
1285+ {
1286+ name : "Waiting" ,
1287+ origin : "execution-protocol" ,
1288+ props : { type : "object" , properties : { } , additionalProperties : false } ,
1289+ * fn ( ) : Operation < string > {
1290+ reached . resolve ( ) ;
1291+ yield * waiting . operation ;
1292+ return "unreachable" ;
1293+ } ,
12811294 } ,
1282- } ,
1283- ] ) ;
1295+ ] ) ;
1296+ } ,
12841297 } ,
1285- } ,
1286- ] ) ;
1298+ ] ,
1299+ ) ;
1300+
1301+ // Drained from the moment the execution exists, so what is read is what
1302+ // the document emitted rather than whatever was left in a buffer. The
1303+ // close value of this channel is the whole of it — full for a document
1304+ // that finished, partial for one that did not.
1305+ const chunks : string [ ] = [ ] ;
1306+ const closed = withResolvers < string > ( ) ;
1307+ yield * spawn ( function * ( ) {
1308+ const subscription = yield * execution . output ;
1309+ let next = yield * subscription . next ( ) ;
1310+ while ( ! next . done ) {
1311+ chunks . push ( next . value ) ;
1312+ next = yield * subscription . next ( ) ;
1313+ }
1314+ closed . resolve ( next . value ) ;
1315+ } ) ;
1316+
1317+ // The document has printed and is now blocked. Both are waited for, so
1318+ // the stop below happens at a known point rather than a timed one.
12871319 yield * reached . operation ;
1320+ for ( let round = 0 ; round < 200 && ! chunks . join ( "" ) . includes ( PREFIX ) ; round += 1 ) {
1321+ yield * sleep ( 1 ) ;
1322+ }
1323+ const observed = chunks . join ( "" ) ;
1324+ expect ( observed ) . toContain ( PREFIX ) ;
12881325
12891326 const stopped = withResolvers < Result < void > > ( ) ;
12901327 const asking = withResolvers < void > ( ) ;
@@ -1317,6 +1354,13 @@ describe("Tier EP — the execution protocol", () => {
13171354 const completion = yield * execution ;
13181355 expect ( completion . ok ) . toBe ( false ) ;
13191356
1357+ // And the text it had already printed is exactly what its output channel
1358+ // closed with. This is the assertion a stop that dropped the prefix, or
1359+ // closed the channel empty, does not survive.
1360+ const closure = yield * closed . operation ;
1361+ expect ( closure ) . toContain ( PREFIX ) ;
1362+ expect ( closure ) . toBe ( observed ) ;
1363+
13201364 // One cancelled root Close, carrying a status and nothing else.
13211365 const closes = stream . snapshot ( ) . filter ( ( event ) => event . type === "close" ) ;
13221366 const roots = closes . filter (
@@ -1330,14 +1374,17 @@ describe("Tier EP — the execution protocol", () => {
13301374 expect ( only . result . status ) . toBe ( "cancelled" ) ;
13311375 expect ( Object . keys ( only . result ) ) . toEqual ( [ "status" ] ) ;
13321376
1333- // Asked again, and read again: neither writes a second ending.
1377+ // Asked again, and read again: neither writes a second ending, and
1378+ // neither adds a byte to what was printed.
13341379 const again = yield * execution . stop ( ) ;
13351380 expect ( again . ok ) . toBe ( true ) ;
13361381 expect ( yield * execution ) . toEqual ( completion ) ;
13371382 expect (
13381383 stream . snapshot ( ) . filter ( ( event ) => event . type === "close" && event . coroutineId === "root" )
13391384 . length ,
13401385 ) . toBe ( 1 ) ;
1386+ expect ( chunks . join ( "" ) ) . toBe ( observed ) ;
1387+ expect ( yield * closed . operation ) . toBe ( closure ) ;
13411388 } ) ;
13421389 } ) ;
13431390
@@ -1406,7 +1453,7 @@ describe("Tier EP — the execution protocol", () => {
14061453 // refusal as its cause: the identity that matters is the run's, and the
14071454 // reason it gives is the reason the backend gave.
14081455 expect ( refused ) . toBeInstanceOf ( DurablePersistenceError ) ;
1409- expect ( ( refused as Error | undefined ) ?. cause ) . toBe ( appendCause ) ;
1456+ expect ( refused ?. cause ) . toBe ( appendCause ) ;
14101457
14111458 // The cleanup was attempted and joined even though the ending was not
14121459 // written, and nothing stood in for the terminal that was refused.
0 commit comments