@@ -845,6 +845,210 @@ test("agent calls run through the injected session factory and resume replays th
845845 }
846846} ) ;
847847
848+ type ReplayAcceptanceVerdict = "rejected" | "missing" | "malformed" ;
849+
850+ type ReplayAcceptanceAgent = {
851+ state : unknown ;
852+ replayed ?: unknown ;
853+ invocation ?: {
854+ admissionState ?: unknown ;
855+ executionState ?: unknown ;
856+ outcome ?: unknown ;
857+ } ;
858+ acceptance ?: { status ?: unknown } ;
859+ error ?: unknown ;
860+ resultArtifact ?: unknown ;
861+ resultRef ?: unknown ;
862+ } ;
863+
864+ type ReplayAcceptanceFixture = {
865+ resumed : {
866+ content : Array < { type : string ; text : string } > ;
867+ details : { runId ?: unknown } ;
868+ } ;
869+ agents : ReplayAcceptanceAgent [ ] ;
870+ runDir : string ;
871+ sessionCreations : number ;
872+ } ;
873+
874+ let replayAcceptanceFixtureId = 0 ;
875+
876+ function tamperedAcceptance ( verdict : ReplayAcceptanceVerdict ) {
877+ if ( verdict === "rejected" ) {
878+ return {
879+ acceptance : {
880+ criteria : [ { id : "tests" , status : "rejected" , evidence : [ "command" ] } ] ,
881+ } ,
882+ } ;
883+ }
884+ if ( verdict === "missing" ) return { } ;
885+ return {
886+ acceptance : {
887+ criteria : [
888+ {
889+ id : `\u001b[31m${ "x" . repeat ( 3_000 ) } \u001b[0m` ,
890+ status : "rejected" ,
891+ evidence : [ "command" ] ,
892+ } ,
893+ ] ,
894+ } ,
895+ } ;
896+ }
897+
898+ async function runTamperedAcceptanceReplay (
899+ verdict : ReplayAcceptanceVerdict ,
900+ ) : Promise < ReplayAcceptanceFixture > {
901+ let sessionCreations = 0 ;
902+ const factory : WorkflowAgentSessionFactory = async ( options ) => {
903+ sessionCreations ++ ;
904+ const structuredTool = options ?. customTools ?. find (
905+ ( tool ) => tool . name === "structured_output" ,
906+ ) ;
907+ assert . ok ( structuredTool ) ;
908+ const session = fakeAgentSession ( "acceptance replay output" ) ;
909+ const existingTools = session . getAllTools ( ) ;
910+ session . getAllTools = ( ) => [
911+ ...existingTools ,
912+ {
913+ name : structuredTool . name ,
914+ description : structuredTool . description ,
915+ parameters : structuredTool . parameters ,
916+ promptGuidelines : structuredTool . promptGuidelines ,
917+ sourceInfo : {
918+ path : "<custom:structured_output>" ,
919+ source : "custom" ,
920+ scope : "temporary" ,
921+ origin : "top-level" ,
922+ } ,
923+ } ,
924+ ] ;
925+ session . getActiveToolNames = ( ) => [
926+ ...existingTools . map ( ( tool ) => tool . name ) ,
927+ structuredTool . name ,
928+ ] ;
929+ session . getToolDefinition = ( name ) =>
930+ name === structuredTool . name ? structuredTool : undefined ;
931+ const prompt = session . prompt . bind ( session ) ;
932+ session . prompt = async ( ) => {
933+ await structuredTool . execute (
934+ "acceptance-replay" ,
935+ {
936+ acceptance : {
937+ criteria : [
938+ { id : "tests" , status : "accepted" , evidence : [ "command" ] } ,
939+ ] ,
940+ } ,
941+ } ,
942+ new AbortController ( ) . signal ,
943+ ( ) => { } ,
944+ ctx ,
945+ ) ;
946+ await prompt ( "fixture prompt" ) ;
947+ } ;
948+ return { session } ;
949+ } ;
950+ __setWorkflowTestAgentSessionFactory ( factory ) ;
951+
952+ const fixtureId = ++ replayAcceptanceFixtureId ;
953+ const script =
954+ `export const meta = { name: "acceptance-replay-${ fixtureId } " };\n` +
955+ 'const r = await agent("verify the fixture", { agent_type: "reviewer", acceptance: { criteria: [{ id: "tests", description: "Focused tests pass", requiredEvidence: ["command"] }] } });\n' +
956+ "return { ok: r.ok, error: r.error };" ;
957+
958+ try {
959+ const first = ( await workflow . execute (
960+ `e2e-acceptance-replay-source-${ fixtureId } ` ,
961+ { script, wait : true } ,
962+ undefined ,
963+ undefined ,
964+ ctx ,
965+ ) ) as { details : { runId ?: unknown } } ;
966+ const sourceDir = runDirFor ( first . details . runId ) ;
967+ const journalPath = join ( sourceDir , "journal.json" ) ;
968+ const journal = JSON . parse ( readFileSync ( journalPath , "utf8" ) ) as {
969+ entries : Array < Record < string , unknown > > ;
970+ } ;
971+ assert . equal ( journal . entries . length , 1 ) ;
972+ if ( verdict === "missing" ) delete journal . entries [ 0 ] ! . structured ;
973+ else journal . entries [ 0 ] ! . structured = tamperedAcceptance ( verdict ) ;
974+ writeFileSync ( journalPath , JSON . stringify ( journal ) ) ;
975+
976+ const resumed = ( await workflow . execute (
977+ `e2e-acceptance-replay-resume-${ fixtureId } ` ,
978+ {
979+ script,
980+ resume_from_run_id : String ( first . details . runId ) ,
981+ wait : true ,
982+ } ,
983+ undefined ,
984+ undefined ,
985+ ctx ,
986+ ) ) as ReplayAcceptanceFixture [ "resumed" ] ;
987+ const persisted = readWorkflowJson ( resumed . details . runId ) ;
988+ return {
989+ resumed,
990+ agents : persisted . agents as ReplayAcceptanceAgent [ ] ,
991+ runDir : runDirFor ( resumed . details . runId ) ,
992+ sessionCreations,
993+ } ;
994+ } finally {
995+ __setWorkflowTestAgentSessionFactory ( undefined ) ;
996+ }
997+ }
998+
999+ const replayAcceptanceVerdicts = [
1000+ "rejected" ,
1001+ "missing" ,
1002+ "malformed" ,
1003+ ] as const satisfies readonly ReplayAcceptanceVerdict [ ] ;
1004+
1005+ // Keep the three projections independent: the script-visible result, the
1006+ // persisted lifecycle record, and success-only filesystem side effects.
1007+ test ( "replay acceptance verdicts preserve the script return contract" , async ( ) => {
1008+ for ( const verdict of replayAcceptanceVerdicts ) {
1009+ const fixture = await runTamperedAcceptanceReplay ( verdict ) ;
1010+ const projected = fixture . resumed . content
1011+ . map ( ( entry ) => entry . text )
1012+ . join ( "\n" ) ;
1013+ assert . match ( projected , / " o k " \s * : \s * f a l s e / ) ;
1014+ assert . match ( projected , new RegExp ( `Acceptance ${ verdict } ` ) ) ;
1015+ if ( verdict === "malformed" ) {
1016+ const error = projected . match ( / A c c e p t a n c e m a l f o r m e d [ ^ \n ] * / ) ?. [ 0 ] ?? "" ;
1017+ assert . ok ( [ ...error ] . length <= 2_000 ) ;
1018+ assert . ok ( ! / [ \u0000 - \u001f \u007f - \u009f ] / . test ( error ) ) ;
1019+ }
1020+ }
1021+ } ) ;
1022+
1023+ test ( "replay acceptance verdicts persist fail-closed runtime records" , async ( ) => {
1024+ for ( const verdict of replayAcceptanceVerdicts ) {
1025+ const { agents, sessionCreations } =
1026+ await runTamperedAcceptanceReplay ( verdict ) ;
1027+ assert . equal ( sessionCreations , 1 ) ;
1028+ assert . equal ( agents . length , 1 ) ;
1029+ assert . equal ( agents [ 0 ] ?. state , "error" ) ;
1030+ assert . equal ( agents [ 0 ] ?. replayed , undefined ) ;
1031+ assert . equal ( agents [ 0 ] ?. invocation ?. admissionState , "rejected" ) ;
1032+ assert . equal ( agents [ 0 ] ?. invocation ?. executionState , "settled" ) ;
1033+ assert . equal ( agents [ 0 ] ?. invocation ?. outcome , "error" ) ;
1034+ assert . equal ( agents [ 0 ] ?. acceptance ?. status , verdict ) ;
1035+ assert . match ( String ( agents [ 0 ] ?. error ) , new RegExp ( `Acceptance ${ verdict } ` ) ) ;
1036+ assert . equal ( agents [ 0 ] ?. resultArtifact , undefined ) ;
1037+ assert . equal ( agents [ 0 ] ?. resultRef , undefined ) ;
1038+ }
1039+ } ) ;
1040+
1041+ test ( "replay acceptance verdicts do not create success filesystem side effects" , async ( ) => {
1042+ for ( const verdict of replayAcceptanceVerdicts ) {
1043+ const { runDir } = await runTamperedAcceptanceReplay ( verdict ) ;
1044+ assert . equal (
1045+ existsSync ( join ( runDir , "agent-results/agent-0001.json" ) ) ,
1046+ false ,
1047+ ) ;
1048+ assert . equal ( existsSync ( join ( runDir , "journal.json" ) ) , false ) ;
1049+ }
1050+ } ) ;
1051+
8481052test ( "oversized authoritative agent results fail without a success record" , async ( ) => {
8491053 const factory : WorkflowAgentSessionFactory = async ( options ) => {
8501054 const structuredTool = options ?. customTools ?. find (
0 commit comments