@@ -3068,146 +3068,250 @@ describe("direct submission attachment rights", () => {
30683068 } ) ;
30693069} ) ;
30703070
3071+ function * controlledSupervision ( outcome : string ) {
3072+ const entered = withResolvers < void > ( ) ;
3073+ const releaseBody = withResolvers < void > ( ) ;
3074+ const cleaning = withResolvers < void > ( ) ;
3075+ const releaseCleanup = withResolvers < void > ( ) ;
3076+ const following = withResolvers < void > ( ) ;
3077+ const methods : string [ ] = [ ] ;
3078+ const resultDirectory = yield * useTemporary ( "xmd-supervision-result-" ) ;
3079+ const resultFile = join ( resultDirectory , "result.json" ) ;
3080+ let cleaned = false ;
3081+ const one = yield * world (
3082+ {
3083+ entryInstallation : {
3084+ * install ( ) {
3085+ yield * Execution . around ( {
3086+ * document ( [ request ] , next ) {
3087+ yield * ensure ( function * ( ) {
3088+ cleaning . resolve ( ) ;
3089+ yield * releaseCleanup . operation ;
3090+ if ( outcome === "completed" ) {
3091+ yield * writeTextFile ( resultFile , JSON . stringify ( { status : "hold" , calls : 1 } ) ) ;
3092+ }
3093+ cleaned = true ;
3094+ } ) ;
3095+ entered . resolve ( ) ;
3096+ yield * releaseBody . operation ;
3097+ yield * next ( request ) ;
3098+ } ,
3099+ } ) ;
3100+ } ,
3101+ } ,
3102+ } ,
3103+ ( ) => countWaitRequests ( methods , ( ) => following . resolve ( ) ) ,
3104+ ) ;
3105+ return {
3106+ one,
3107+ entered,
3108+ releaseBody,
3109+ cleaning,
3110+ releaseCleanup,
3111+ following,
3112+ methods,
3113+ resultFile,
3114+ get cleaned ( ) {
3115+ return cleaned ;
3116+ } ,
3117+ } ;
3118+ }
3119+
3120+ type ControlledSupervision =
3121+ ReturnType < typeof controlledSupervision > extends Operation < infer T > ? T : never ;
3122+
3123+ function withControlledSupervision (
3124+ outcome : string ,
3125+ body : ( controlled : ControlledSupervision ) => Operation < void > ,
3126+ ) {
3127+ return scoped ( function * ( ) {
3128+ const controlled = yield * controlledSupervision ( outcome ) ;
3129+ try {
3130+ yield * body ( controlled ) ;
3131+ } finally {
3132+ // These barriers must open before scope teardown joins the producer and entry.
3133+ controlled . releaseBody . resolve ( ) ;
3134+ controlled . releaseCleanup . resolve ( ) ;
3135+ }
3136+ } ) ;
3137+ }
3138+
30713139describe ( "Passive workflow supervision" , ( ) => {
30723140 for ( const outcome of [ "completed" , "failed" ] ) {
30733141 it ( `waits without polling for a ${ outcome } entry and joined cleanup` , function * ( ) {
3074- const entered = withResolvers < void > ( ) ;
3075- const releaseBody = withResolvers < void > ( ) ;
3076- const cleaning = withResolvers < void > ( ) ;
3077- const releaseCleanup = withResolvers < void > ( ) ;
3078- const following = withResolvers < void > ( ) ;
3079- const progressed = withResolvers < void > ( ) ;
3080- const progress : string [ ] = [ ] ;
3081- const methods : string [ ] = [ ] ;
3082- const resultDirectory = yield * useTemporary ( "xmd-supervision-result-" ) ;
3083- const resultFile = join ( resultDirectory , "result.json" ) ;
3084- let cleaned = false ;
3085- yield * ensure ( ( ) => {
3142+ yield * withControlledSupervision ( outcome , function * ( controlled ) {
3143+ const {
3144+ one,
3145+ entered,
3146+ releaseBody,
3147+ cleaning,
3148+ releaseCleanup,
3149+ following,
3150+ methods,
3151+ resultFile,
3152+ } = controlled ;
3153+ const progressed = withResolvers < void > ( ) ;
3154+ const progress : string [ ] = [ ] ;
3155+ const attached = attachResult . parse (
3156+ yield * requestAttachment ( one . path , { method : "attach" , cwd : one . descriptor . cwd } ) ,
3157+ ) ;
3158+ const source =
3159+ outcome === "completed"
3160+ ? "<Output>The controlled step result.</Output>"
3161+ : '<Fail message="controlled step failure" />' ;
3162+ one . held . fake . script ( { reply : fills ( source ) + "\n<REPL.Submit />" } ) ;
3163+ one . held . fake . script ( { reply : "<Output>The exchange settled.</Output>" } ) ;
3164+ const producer = yield * spawn ( ( ) =>
3165+ requestAttachment ( one . path , {
3166+ method : "prompt" ,
3167+ attachment : attached . attachment ,
3168+ text : "Execute one controlled step." ,
3169+ } ) ,
3170+ ) ;
3171+ yield * race ( [
3172+ entered . operation ,
3173+ producer ,
3174+ deadline ( "The original controlled entry did not start" ) ,
3175+ ] ) ;
3176+ const reference = one . dispatch . current ?. snapshot . message ;
3177+ if ( reference === undefined ) {
3178+ throw new Error ( "The step has no acknowledged message" ) ;
3179+ }
3180+ const mark = methods . length ;
3181+ let returned = false ;
3182+ const waiting = yield * spawn ( function * ( ) {
3183+ const result = yield * waitDocument ( one , 1 , reference , {
3184+ resultFile,
3185+ resultSchema : {
3186+ type : "object" ,
3187+ properties : { status : { const : "hold" } , calls : { const : 1 } } ,
3188+ required : [ "status" , "calls" ] ,
3189+ additionalProperties : false ,
3190+ } ,
3191+ progress ( text ) {
3192+ progress . push ( text ) ;
3193+ if ( progress . join ( "" ) . includes ( "[entry 1] evaluating" ) ) {
3194+ progressed . resolve ( ) ;
3195+ }
3196+ } ,
3197+ } ) ;
3198+ returned = true ;
3199+ return result ;
3200+ } ) ;
3201+ yield * race ( [
3202+ following . operation ,
3203+ waiting ,
3204+ deadline ( "The wait document did not subscribe" ) ,
3205+ ] ) ;
3206+ yield * race ( [
3207+ progressed . operation ,
3208+ waiting ,
3209+ deadline ( "Progress did not arrive before completion" ) ,
3210+ ] ) ;
3211+ expect ( returned ) . toBe ( false ) ;
30863212 releaseBody . resolve ( ) ;
3213+ yield * cleaning . operation ;
3214+ expect ( returned ) . toBe ( false ) ;
3215+ expect ( controlled . cleaned ) . toBe ( false ) ;
30873216 releaseCleanup . resolve ( ) ;
3217+ const result = yield * race ( [ waiting , deadline ( "The document did not receive completion" ) ] ) ;
3218+ yield * producer ;
3219+ expect ( controlled . cleaned ) . toBe ( true ) ;
3220+ expect ( result . status ) . toBe ( outcome ) ;
3221+ expect ( result . joinedExchange ) . toBe ( true ) ;
3222+ expect ( result . exchangeOutcome ) . toBe ( "completed" ) ;
3223+ expect ( progress . join ( "" ) ) . toContain ( "exchange completed" ) ;
3224+ expect ( progress . join ( "" ) ) . not . toContain ( "The exchange settled." ) ;
3225+ expect ( progress . join ( "" ) . length ) . toBeLessThan ( 1500 ) ;
3226+ if ( outcome === "completed" ) {
3227+ expect ( result . read ) . toEqual ( { entry : 1 , view : "journal" } ) ;
3228+ expect ( result . resultStatus ) . toBe ( "readable" ) ;
3229+ expect ( result . result ) . toEqual ( { status : "hold" , calls : 1 } ) ;
3230+ }
3231+ if ( outcome === "failed" ) {
3232+ expect ( result . diagnostic ) . toContain ( "controlled step failure" ) ;
3233+ }
3234+ expect ( methods . slice ( mark ) ) . toEqual ( [ "hello" , "message" , "hello" , "entry" ] ) ;
3235+ expect ( one . held . fake . prompts ) . toHaveLength ( one . baselineTurns + 2 ) ;
3236+ const lateMark = methods . length ;
3237+ const late = yield * waitDocument ( one , 1 , reference ) ;
3238+ expect ( late . status ) . toBe ( outcome ) ;
3239+ expect ( methods . slice ( lateMark ) ) . toEqual ( [ "hello" , "message" , "hello" , "entry" ] ) ;
3240+ expect ( one . held . fake . prompts ) . toHaveLength ( one . baselineTurns + 2 ) ;
3241+ console . log (
3242+ JSON . stringify ( {
3243+ kind : "wait-document-result" ,
3244+ outcome,
3245+ cleaned : controlled . cleaned ,
3246+ requests : methods . slice ( mark ) ,
3247+ statusPolls : 0 ,
3248+ observerRoleCalls : 0 ,
3249+ alreadyCompletedRead : late . status ,
3250+ } ) ,
3251+ ) ;
30883252 } ) ;
3089- const one = yield * world (
3090- {
3091- entryInstallation : {
3092- * install ( ) {
3093- yield * Execution . around ( {
3094- * document ( [ request ] , next ) {
3095- yield * ensure ( function * ( ) {
3096- cleaning . resolve ( ) ;
3097- yield * releaseCleanup . operation ;
3098- if ( outcome === "completed" ) {
3099- yield * writeTextFile (
3100- resultFile ,
3101- JSON . stringify ( { status : "hold" , calls : 1 } ) ,
3102- ) ;
3103- }
3104- cleaned = true ;
3105- } ) ;
3106- entered . resolve ( ) ;
3107- yield * releaseBody . operation ;
3108- yield * next ( request ) ;
3109- } ,
3110- } ) ;
3111- } ,
3112- } ,
3113- } ,
3114- ( ) => countWaitRequests ( methods , ( ) => following . resolve ( ) ) ,
3115- ) ;
3116- const attached = attachResult . parse (
3117- yield * requestAttachment ( one . path , { method : "attach" , cwd : one . descriptor . cwd } ) ,
3118- ) ;
3119- const source =
3120- outcome === "completed"
3121- ? "<Output>The controlled step result.</Output>"
3122- : '<Fail message="controlled step failure" />' ;
3123- one . held . fake . script ( { reply : fills ( source ) + "\n<REPL.Submit />" } ) ;
3124- one . held . fake . script ( { reply : "<Output>The exchange settled.</Output>" } ) ;
3125- const producer = yield * spawn ( ( ) =>
3126- requestAttachment ( one . path , {
3127- method : "prompt" ,
3128- attachment : attached . attachment ,
3129- text : "Execute one controlled step." ,
3130- } ) ,
3131- ) ;
3132- yield * race ( [
3133- entered . operation ,
3134- producer ,
3135- deadline ( "The original controlled entry did not start" ) ,
3136- ] ) ;
3137- const reference = one . dispatch . current ?. snapshot . message ;
3138- if ( reference === undefined ) {
3139- throw new Error ( "The step has no acknowledged message" ) ;
3140- }
3141- const mark = methods . length ;
3142- let returned = false ;
3143- const waiting = yield * spawn ( function * ( ) {
3144- const result = yield * waitDocument ( one , 1 , reference , {
3145- resultFile,
3146- resultSchema : {
3147- type : "object" ,
3148- properties : { status : { const : "hold" } , calls : { const : 1 } } ,
3149- required : [ "status" , "calls" ] ,
3150- additionalProperties : false ,
3151- } ,
3152- progress ( text ) {
3153- progress . push ( text ) ;
3154- if ( progress . join ( "" ) . includes ( "[entry 1] evaluating" ) ) {
3155- progressed . resolve ( ) ;
3156- }
3157- } ,
3253+ } ) ;
3254+ }
3255+
3256+ it ( "exposes an early supervisor failure after joining held entry cleanup" , function * ( ) {
3257+ const ready = withResolvers < ControlledSupervision > ( ) ;
3258+ const joined = withResolvers < unknown > ( ) ;
3259+ let initiating : unknown ;
3260+ const owner = yield * spawn ( function * ( ) {
3261+ try {
3262+ yield * withControlledSupervision ( "completed" , function * ( controlled ) {
3263+ ready . resolve ( controlled ) ;
3264+ const { one } = controlled ;
3265+ const attached = attachResult . parse (
3266+ yield * requestAttachment ( one . path , { method : "attach" , cwd : one . descriptor . cwd } ) ,
3267+ ) ;
3268+ one . held . fake . script ( {
3269+ reply : fills ( "<Output>The held entry.</Output>" ) + "\n<REPL.Submit />" ,
3270+ } ) ;
3271+ yield * spawn ( ( ) =>
3272+ requestAttachment ( one . path , {
3273+ method : "prompt" ,
3274+ attachment : attached . attachment ,
3275+ text : "Hold the entry through supervisor failure." ,
3276+ } ) ,
3277+ ) ;
3278+ yield * controlled . entered . operation ;
3279+ try {
3280+ yield * waitDocument ( one , 1 , undefined , {
3281+ document : join ( one . home , "missing-supervisor.md" ) ,
3282+ } ) ;
3283+ } catch ( error ) {
3284+ initiating = error ;
3285+ throw error ;
3286+ }
31583287 } ) ;
3159- returned = true ;
3160- return result ;
3161- } ) ;
3162- yield * race ( [ following . operation , waiting , deadline ( "The wait document did not subscribe" ) ] ) ;
3163- yield * race ( [
3164- progressed . operation ,
3165- waiting ,
3166- deadline ( "Progress did not arrive before completion" ) ,
3167- ] ) ;
3168- expect ( returned ) . toBe ( false ) ;
3169- releaseBody . resolve ( ) ;
3170- yield * cleaning . operation ;
3171- expect ( returned ) . toBe ( false ) ;
3172- expect ( cleaned ) . toBe ( false ) ;
3173- releaseCleanup . resolve ( ) ;
3174- const result = yield * race ( [ waiting , deadline ( "The document did not receive completion" ) ] ) ;
3175- yield * producer ;
3176- expect ( cleaned ) . toBe ( true ) ;
3177- expect ( result . status ) . toBe ( outcome ) ;
3178- expect ( result . joinedExchange ) . toBe ( true ) ;
3179- expect ( result . exchangeOutcome ) . toBe ( "completed" ) ;
3180- expect ( progress . join ( "" ) ) . toContain ( "exchange completed" ) ;
3181- expect ( progress . join ( "" ) ) . not . toContain ( "The exchange settled." ) ;
3182- expect ( progress . join ( "" ) . length ) . toBeLessThan ( 1500 ) ;
3183- if ( outcome === "completed" ) {
3184- expect ( result . read ) . toEqual ( { entry : 1 , view : "journal" } ) ;
3185- expect ( result . resultStatus ) . toBe ( "readable" ) ;
3186- expect ( result . result ) . toEqual ( { status : "hold" , calls : 1 } ) ;
3187- }
3188- if ( outcome === "failed" ) {
3189- expect ( result . diagnostic ) . toContain ( "controlled step failure" ) ;
3288+ } catch ( error ) {
3289+ joined . resolve ( error ) ;
31903290 }
3191- expect ( methods . slice ( mark ) ) . toEqual ( [ "hello" , "message" , "hello" , "entry" ] ) ;
3192- expect ( one . held . fake . prompts ) . toHaveLength ( one . baselineTurns + 2 ) ;
3193- const lateMark = methods . length ;
3194- const late = yield * waitDocument ( one , 1 , reference ) ;
3195- expect ( late . status ) . toBe ( outcome ) ;
3196- expect ( methods . slice ( lateMark ) ) . toEqual ( [ "hello" , "message" , "hello" , "entry" ] ) ;
3197- expect ( one . held . fake . prompts ) . toHaveLength ( one . baselineTurns + 2 ) ;
3198- console . log (
3199- JSON . stringify ( {
3200- kind : "wait-document-result" ,
3201- outcome,
3202- cleaned,
3203- requests : methods . slice ( mark ) ,
3204- statusPolls : 0 ,
3205- observerRoleCalls : 0 ,
3206- alreadyCompletedRead : late . status ,
3207- } ) ,
3208- ) ;
32093291 } ) ;
3210- }
3292+ const controlled = yield * ready . operation ;
3293+ const settled = yield * race ( [
3294+ ( function * ( ) {
3295+ yield * joined . operation ;
3296+ return true ;
3297+ } ) ( ) ,
3298+ ( function * ( ) {
3299+ yield * sleep ( 5000 ) ;
3300+ return false ;
3301+ } ) ( ) ,
3302+ ] ) ;
3303+ // Release from outside the owner so a broken ordering reports an assertion instead of hanging.
3304+ controlled . releaseBody . resolve ( ) ;
3305+ controlled . releaseCleanup . resolve ( ) ;
3306+ yield * owner ;
3307+ expect ( settled ) . toBe ( true ) ;
3308+ expect ( yield * joined . operation ) . toBe ( initiating ) ;
3309+ expect ( initiating ) . toBeInstanceOf ( Error ) ;
3310+ if ( initiating instanceof Error ) {
3311+ expect ( initiating . message ) . toContain ( "missing-supervisor.md" ) ;
3312+ }
3313+ expect ( controlled . cleaned ) . toBe ( true ) ;
3314+ } ) ;
32113315
32123316 it ( "detaching a passive follow leaves the held producer running" , function * ( ) {
32133317 const following = withResolvers < void > ( ) ;
0 commit comments