@@ -65,6 +65,7 @@ import { AppError } from '../../../kernel/errors.ts';
6565import { makeIosSession } from '../../../__tests__/test-utils/session-factories.ts' ;
6666import { HEAL_COMPLETE_SENTINEL } from '../../session-script-writer.ts' ;
6767import { parseReplayScriptDetailed } from '../../../replay/script.ts' ;
68+ import type { DaemonRequest } from '../../types.ts' ;
6869import {
6970 baseReplayRequest as baseReq ,
7071 writeReplayFile ,
@@ -512,6 +513,108 @@ test('BLOCKER 2b/2c: a close whose commit FAILS (no-clobber) keeps the session f
512513 expect ( promoted . actions . filter ( ( a ) => a . command === 'close' ) ) . toHaveLength ( 1 ) ;
513514} ) ;
514515
516+ // --- #1258: `--force`/`--overwrite` on `--save-script`. ---
517+
518+ test ( '#1258: close --save-script --force overwrites an existing COMPLETE healed .ad instead of refusing' , async ( ) => {
519+ const { root, sessionStore, sessionName, logPath, leaseRegistry } = setup (
520+ 'agent-device-repair-transaction-force-overwrite-' ,
521+ ) ;
522+ makeCompleteRepairSession ( sessionStore , sessionName , root ) ;
523+ // A prior COMPLETE (sentinel-marked) healed artifact already sits at the
524+ // default path — `--force` must overwrite it instead of refusing.
525+ fs . writeFileSync (
526+ path . join ( root , 'flow.healed.ad' ) ,
527+ `context platform=ios device="x"\nclick id="old"\n${ HEAL_COMPLETE_SENTINEL } \n` ,
528+ ) ;
529+
530+ const closeResponse = await handleCloseCommand ( {
531+ req : {
532+ token : 't' ,
533+ session : sessionName ,
534+ command : 'close' ,
535+ positionals : [ ] ,
536+ flags : { force : true } ,
537+ } ,
538+ sessionName,
539+ logPath,
540+ sessionStore,
541+ leaseRegistry,
542+ } ) ;
543+
544+ expect ( closeResponse . ok ) . toBe ( true ) ;
545+ expect ( sessionStore . get ( sessionName ) ) . toBeUndefined ( ) ;
546+ const healedPath = path . join ( root , 'flow.healed.ad' ) ;
547+ if ( closeResponse . ok ) expect ( closeResponse . data ?. savedScript ) . toBe ( healedPath ) ;
548+ const script = fs . readFileSync ( healedPath , 'utf8' ) ;
549+ expect ( script ) . toContain ( HEAL_COMPLETE_SENTINEL ) ;
550+ const parsed = parseReplayScriptDetailed ( script ) ;
551+ // The new repair's own actions replaced the stale prior artifact — the old
552+ // content is gone, never merged/appended.
553+ expect ( parsed . actions . some ( ( a ) => a . positionals [ 0 ] === 'id="old"' ) ) . toBe ( false ) ;
554+ expect ( parsed . actions . map ( ( a ) => a . command ) ) . toEqual ( [ 'open' , 'press' , 'close' ] ) ;
555+ } ) ;
556+
557+ test ( '#1258 arm-time preflight: an existing --save-script target rejects BEFORE any step runs' , async ( ) => {
558+ const { root, sessionStore, sessionName, logPath } = setup (
559+ 'agent-device-repair-transaction-arm-preflight-' ,
560+ ) ;
561+ const filePath = writeReplayFile ( root , [ 'open "Demo" --relaunch' , 'close' ] ) ;
562+ // The default healed sibling already exists — a prior repair, or an
563+ // unrelated stale file sitting at that path.
564+ fs . writeFileSync ( path . join ( root , 'flow.healed.ad' ) , 'context platform=ios device="x"\n' ) ;
565+
566+ const spy : DaemonRequest [ ] = [ ] ;
567+ const invoke = makeRecordingReplayInvoke ( { sessionStore, sessionName, spy } ) ;
568+
569+ const result = await runReplayScriptFile ( {
570+ req : baseReq ( { positionals : [ filePath ] , flags : { saveScript : true } } ) ,
571+ sessionName,
572+ logPath,
573+ sessionStore,
574+ invoke,
575+ } ) ;
576+
577+ expect ( result . ok ) . toBe ( false ) ;
578+ if ( ! result . ok ) {
579+ expect ( result . error . code ) . toBe ( 'COMMAND_FAILED' ) ;
580+ expect ( result . error . message ) . toMatch ( / a l r e a d y e x i s t s / ) ;
581+ }
582+ // Not a single step dispatched — the preflight fired BEFORE the loop, not
583+ // merely at the end (publish time). Even `open` never ran.
584+ expect ( spy ) . toHaveLength ( 0 ) ;
585+ // The session was never armed (no boundary stamped) either — the whole
586+ // repair-arm side effect is skipped, not just the dispatch.
587+ expect ( sessionStore . get ( sessionName ) ?. saveScriptBoundary ) . toBeUndefined ( ) ;
588+ } ) ;
589+
590+ test ( '#1258: --force skips the arm-time preflight and the replay proceeds despite the existing target' , async ( ) => {
591+ const { root, sessionStore, sessionName, logPath } = setup (
592+ 'agent-device-repair-transaction-arm-preflight-force-' ,
593+ ) ;
594+ const filePath = writeReplayFile ( root , [ 'open "Demo" --relaunch' , 'close' ] ) ;
595+ fs . writeFileSync ( path . join ( root , 'flow.healed.ad' ) , 'context platform=ios device="x"\n' ) ;
596+
597+ const spy : DaemonRequest [ ] = [ ] ;
598+ const invoke = makeRecordingReplayInvoke ( { sessionStore, sessionName, spy } ) ;
599+
600+ const result = await runReplayScriptFile ( {
601+ req : baseReq ( { positionals : [ filePath ] , flags : { saveScript : true , force : true } } ) ,
602+ sessionName,
603+ logPath,
604+ sessionStore,
605+ invoke,
606+ } ) ;
607+
608+ expect ( result . ok ) . toBe ( true ) ;
609+ // `open` dispatched — the preflight did not block it. The terminal `close`
610+ // is skipped while repair-armed (existing, unrelated behavior), so `open`
611+ // is the only step this minimal script actually dispatches.
612+ expect ( spy . map ( ( r ) => r . command ) ) . toEqual ( [ 'open' ] ) ;
613+ // `force` is persisted on the session from arm time, so a LATER commit
614+ // (e.g. a bare `close`, or teardown) still honors the overwrite.
615+ expect ( sessionStore . get ( sessionName ) ?. saveScriptForce ) . toBe ( true ) ;
616+ } ) ;
617+
515618test ( 'BLOCKER 2 (new): a repair close whose PLATFORM close fails never commits a healed .ad claiming a successful close' , async ( ) => {
516619 const { root, sessionStore, sessionName, logPath, leaseRegistry } = setup (
517620 'agent-device-repair-transaction-platform-close-fail-' ,
0 commit comments