@@ -222,8 +222,8 @@ describe('gh-aw implement workflows', () => {
222222 expect ( continuationSection ( worker ) ) . toContain (
223223 '^<!-- squad:implement issue=([1-9][0-9]*) run=([1-9][0-9]*) -->$' ,
224224 ) ;
225- expect ( continuationSection ( worker ) ) . toContain (
226- ' issue number to equal the marker\ 's issue number' ,
225+ expect ( continuationSection ( worker ) ) . toMatch (
226+ / i s s u e \s + n u m b e r t o e q u a l t h e m a r k e r ' s i s s u e n u m b e r / ,
227227 ) ;
228228 } ) ;
229229
@@ -398,7 +398,7 @@ describe('gh-aw implement worker: retro-origin provenance enforcement', () => {
398398 } ) ) ) . toContain ( 'issue-number-not-numeric' ) ;
399399 } ) ;
400400
401- it ( 'leaves a manual dispatch and the merge continuation untouched ' , ( ) => {
401+ it ( 'leaves a manual dispatch untouched and validates merge continuation provenance ' , ( ) => {
402402 const manual = guard . evaluateImplementDispatchInputs ( {
403403 eventName : 'workflow_dispatch' ,
404404 issueNumber : '42' ,
@@ -407,9 +407,112 @@ describe('gh-aw implement worker: retro-origin provenance enforcement', () => {
407407 } ) ;
408408 expect ( manual . ok ) . toBe ( true ) ;
409409 expect ( manual . origin ) . toBe ( 'manual' ) ;
410- const merged = guard . evaluateImplementDispatchInputs ( { eventName : 'pull_request' } ) ;
410+ const merged = guard . evaluateImplementDispatchInputs ( {
411+ eventName : 'pull_request' ,
412+ pullRequestBody : 'Closes #42\n\n<!-- squad:implement issue=42 run=7 -->' ,
413+ pullRequestHeadRef : 'squad/implement-42-fix-the-widget' ,
414+ } ) ;
411415 expect ( merged . ok ) . toBe ( true ) ;
412- expect ( merged . enforced ) . toBe ( false ) ;
416+ expect ( merged . enforced ) . toBe ( true ) ;
417+ expect ( merged . origin ) . toBe ( 'merge-continuation' ) ;
418+ expect ( merged . issue_number ) . toBe ( 42 ) ;
419+ expect ( merged . run_id ) . toBe ( 7 ) ;
420+ } ) ;
421+
422+ it ( 'fails merge continuation closed on missing, duplicate, fenced, malformed, or mismatched evidence' , ( ) => {
423+ const evaluate = ( body : unknown , headRef : unknown ) => guard . evaluateImplementDispatchInputs ( {
424+ eventName : 'pull_request' ,
425+ pullRequestBody : body ,
426+ pullRequestHeadRef : headRef ,
427+ } ) ;
428+ expect ( kinds ( evaluate ( '' , 'squad/implement-42-fix' ) ) ) . toContain ( 'merge-provenance-marker-invalid' ) ;
429+ expect ( kinds ( evaluate ( [
430+ '<!-- squad:implement issue=42 run=7 -->' ,
431+ '<!-- squad:implement issue=42 run=8 -->' ,
432+ ] . join ( '\n' ) , 'squad/implement-42-fix' ) ) ) . toContain ( 'merge-provenance-marker-ambiguous' ) ;
433+ expect ( kinds ( evaluate ( [
434+ '```text' ,
435+ '<!-- squad:implement issue=42 run=7 -->' ,
436+ '```' ,
437+ ] . join ( '\n' ) , 'squad/implement-42-fix' ) ) ) . toContain ( 'merge-provenance-marker-invalid' ) ;
438+ expect ( kinds ( evaluate (
439+ 'prose <!-- squad:implement issue=42 run=7 -->' ,
440+ 'squad/implement-42-fix' ,
441+ ) ) ) . toContain ( 'merge-provenance-marker-invalid' ) ;
442+ expect ( kinds ( evaluate (
443+ '<!-- squad:implement issue=42 run=7 -->' ,
444+ 'squad/implement-43-fix' ,
445+ ) ) ) . toContain ( 'merge-provenance-issue-mismatch' ) ;
446+ expect ( kinds ( evaluate (
447+ '<!-- squad:implement issue=42 run=7 -->' ,
448+ 'squad/implement-42-Fix' ,
449+ ) ) ) . toContain ( 'merge-provenance-branch-invalid' ) ;
450+ expect ( kinds ( evaluate ( undefined , undefined ) ) ) . toEqual ( expect . arrayContaining ( [
451+ 'merge-provenance-body-unreadable' ,
452+ 'merge-provenance-branch-unreadable' ,
453+ ] ) ) ;
454+ } ) ;
455+
456+ it ( 'refuses merge dispatch output when runtime provenance no longer validates' , async ( ) => {
457+ const result = await guard . enforceImplementSafeOutputs ( {
458+ SQUAD_IMPLEMENT_EVENT_NAME : 'pull_request' ,
459+ SQUAD_IMPLEMENT_PULL_BODY : '<!-- squad:implement issue=42 run=7 -->' ,
460+ SQUAD_IMPLEMENT_PULL_HEAD_REF : 'squad/implement-43-forged' ,
461+ } as never , {
462+ readItems : ( ) => [ {
463+ type : 'dispatch_workflow' ,
464+ workflow_name : 'squad' ,
465+ inputs : { command : 'implement' , issue_number : '100' } ,
466+ } ] ,
467+ } ) ;
468+ expect ( result . ok ) . toBe ( false ) ;
469+ expect ( result . enforced ) . toBe ( true ) ;
470+ expect ( kinds ( result ) ) . toContain ( 'merge-provenance-issue-mismatch' ) ;
471+ } ) ;
472+
473+ it ( 'binds merge continuation to a successful same-repository implement run' , async ( ) => {
474+ const env = {
475+ GITHUB_REPOSITORY : REPO ,
476+ SQUAD_IMPLEMENT_DEFAULT_BRANCH : BRANCH ,
477+ SQUAD_IMPLEMENT_EVENT_NAME : 'pull_request' ,
478+ SQUAD_IMPLEMENT_PULL_BODY : '<!-- squad:implement issue=42 run=7 -->' ,
479+ SQUAD_IMPLEMENT_PULL_HEAD_REF : 'squad/implement-42-fix' ,
480+ SQUAD_IMPLEMENT_PULL_HEAD_REPOSITORY : REPO ,
481+ SQUAD_IMPLEMENT_PULL_CREATED_AT : '2026-09-14T18:00:30Z' ,
482+ } ;
483+ const trustedRun = {
484+ id : 7 ,
485+ path : guard . IMPLEMENT_WORKFLOW_PATH ,
486+ event : 'workflow_dispatch' ,
487+ status : 'completed' ,
488+ conclusion : 'success' ,
489+ head_branch : BRANCH ,
490+ repository : { full_name : REPO } ,
491+ head_repository : { full_name : REPO } ,
492+ run_started_at : '2026-09-14T18:00:00Z' ,
493+ updated_at : '2026-09-14T18:01:00Z' ,
494+ } ;
495+ const valid = await guard . validateImplementOrigin ( env , async ( ) => trustedRun ) ;
496+ expect ( valid . ok ) . toBe ( true ) ;
497+ expect ( valid . origin ) . toBe ( 'merge-continuation' ) ;
498+
499+ const fork = await guard . validateImplementOrigin ( {
500+ ...env ,
501+ SQUAD_IMPLEMENT_PULL_HEAD_REPOSITORY : 'attacker/fork' ,
502+ } , async ( ) => trustedRun ) ;
503+ expect ( kinds ( fork ) ) . toContain ( 'merge-provenance-head-repository-mismatch' ) ;
504+
505+ const copiedAfterRun = await guard . validateImplementOrigin ( {
506+ ...env ,
507+ SQUAD_IMPLEMENT_PULL_CREATED_AT : '2026-09-14T18:02:00Z' ,
508+ } , async ( ) => trustedRun ) ;
509+ expect ( kinds ( copiedAfterRun ) ) . toContain ( 'merge-provenance-run-window-mismatch' ) ;
510+
511+ const unrelatedRun = await guard . validateImplementOrigin ( env , async ( ) => ( {
512+ ...trustedRun ,
513+ path : '.github/workflows/other.lock.yml' ,
514+ } ) ) ;
515+ expect ( kinds ( unrelatedRun ) ) . toContain ( 'merge-provenance-run-untrusted' ) ;
413516 } ) ;
414517
415518 it ( 'refuses an unknown or half-declared origin instead of falling back to manual' , ( ) => {
@@ -763,13 +866,84 @@ describe('gh-aw implement worker: retro-origin provenance enforcement', () => {
763866 it ( 'wires the guard into a pre-agent step and the safe-outputs boundary' , ( ) => {
764867 expect ( worker ) . toContain ( 'shared/squad-retro-provenance.mjs' ) ;
765868 expect ( worker ) . toContain ( '--implement-inputs' ) ;
869+ expect ( worker ) . toContain ( 'Checkout trusted base for the pre-agent provenance guard' ) ;
870+ expect ( worker ) . toContain ( '.squad-pre-agent-trusted-base/.github/workflows/shared/squad-retro-provenance.mjs' ) ;
871+ expect ( worker ) . toContain ( 'SQUAD_IMPLEMENT_PULL_BODY' ) ;
872+ expect ( worker ) . toContain ( 'SQUAD_IMPLEMENT_PULL_HEAD_REF' ) ;
873+ expect ( worker ) . toContain ( 'SQUAD_IMPLEMENT_PULL_HEAD_REPOSITORY' ) ;
874+ expect ( worker ) . toContain ( 'SQUAD_IMPLEMENT_PULL_CREATED_AT' ) ;
766875 const steps = worker . match ( / ^ { 2 } s t e p s : \r ? \n ( [ \s \S ] * ?) ^ { 2 } c r e a t e - p u l l - r e q u e s t : / m) ?. [ 1 ] ?? '' ;
767876 expect ( steps ) . toContain ( 'enforceImplementSafeOutputs' ) ;
768877 expect ( steps ) . toContain ( 'core.setFailed' ) ;
769878 expect ( steps ) . toContain ( 'SQUAD_IMPLEMENT_AW_CONTEXT' ) ;
770879 expect ( steps ) . toContain ( 'SQUAD_IMPLEMENT_DEFAULT_BRANCH' ) ;
880+ expect ( steps ) . toContain ( 'SQUAD_IMPLEMENT_PULL_BODY' ) ;
881+ expect ( steps ) . toContain ( 'SQUAD_IMPLEMENT_PULL_HEAD_REF' ) ;
882+ expect ( steps ) . toContain ( 'SQUAD_IMPLEMENT_PULL_HEAD_REPOSITORY' ) ;
883+ expect ( steps ) . toContain ( 'SQUAD_IMPLEMENT_PULL_CREATED_AT' ) ;
771884 } ) ;
772885
886+ it ( 'compiles the merge gate before the agent and before safe-output processing' , ( ) => {
887+ const lock = compiledWorkerLock ( ) ;
888+ const preCheckout = lock . indexOf ( 'name: Checkout trusted base for the pre-agent provenance guard' ) ;
889+ const preGuard = lock . indexOf ( 'name: Validate dispatch inputs and declared origin' ) ;
890+ const agent = lock . indexOf ( 'name: Execute GitHub Copilot CLI' ) ;
891+ expect ( preCheckout ) . toBeGreaterThan ( - 1 ) ;
892+ expect ( preGuard ) . toBeGreaterThan ( preCheckout ) ;
893+ expect ( agent ) . toBeGreaterThan ( preGuard ) ;
894+ const preAgentContract = lock . slice ( preCheckout , agent ) ;
895+ const preGuardEnd = preAgentContract . indexOf ( 'name: Download container images' ) ;
896+ expect ( preGuardEnd ) . toBeGreaterThan ( - 1 ) ;
897+ const preGateSteps = preAgentContract . slice ( 0 , preGuardEnd ) ;
898+ expect ( preAgentContract ) . toContain ( 'ref: refs/heads/${{ github.event.repository.default_branch }}' ) ;
899+ expect ( preAgentContract ) . toContain ( 'persist-credentials: false' ) ;
900+ expect ( preAgentContract ) . toContain ( 'SQUAD_IMPLEMENT_PULL_BODY' ) ;
901+ expect ( preAgentContract ) . toContain ( 'SQUAD_IMPLEMENT_PULL_HEAD_REF' ) ;
902+ expect ( preAgentContract ) . toContain ( 'SQUAD_IMPLEMENT_PULL_HEAD_REPOSITORY' ) ;
903+ expect ( preAgentContract ) . toContain ( 'SQUAD_IMPLEMENT_PULL_CREATED_AT' ) ;
904+ expect ( preGateSteps ) . not . toMatch ( / \n \s + ( i f | c o n t i n u e - o n - e r r o r ) : / ) ;
905+
906+ const safeJob = safeOutputsJob ( lock ) ;
907+ const safeGuard = safeJob . indexOf ( 'name: Enforce implement provenance before any output' ) ;
908+ const process = safeJob . indexOf ( 'name: Process Safe Outputs' ) ;
909+ expect ( safeGuard ) . toBeGreaterThan ( - 1 ) ;
910+ expect ( process ) . toBeGreaterThan ( safeGuard ) ;
911+ expect ( safeJob . slice ( safeGuard , process ) ) . toContain ( 'SQUAD_IMPLEMENT_PULL_BODY' ) ;
912+ expect ( safeJob . slice ( safeGuard , process ) ) . toContain ( 'SQUAD_IMPLEMENT_PULL_HEAD_REF' ) ;
913+ expect ( safeJob . slice ( safeGuard , process ) ) . toContain ( 'SQUAD_IMPLEMENT_PULL_HEAD_REPOSITORY' ) ;
914+ expect ( safeJob . slice ( safeGuard , process ) ) . toContain ( 'SQUAD_IMPLEMENT_PULL_CREATED_AT' ) ;
915+
916+ } , 60000 ) ;
917+
918+ it ( 'detects mutations that remove either merge-provenance boundary' , ( ) => {
919+ const required = [
920+ 'Checkout trusted base for the pre-agent provenance guard' ,
921+ 'SQUAD_IMPLEMENT_PULL_BODY' ,
922+ 'SQUAD_IMPLEMENT_PULL_HEAD_REF' ,
923+ 'SQUAD_IMPLEMENT_PULL_HEAD_REPOSITORY' ,
924+ 'SQUAD_IMPLEMENT_PULL_CREATED_AT' ,
925+ '.squad-pre-agent-trusted-base/.github/workflows/shared/squad-retro-provenance.mjs' ,
926+ 'Enforce implement provenance before any output' ,
927+ ] ;
928+ const missing = ( text : string ) => required . filter ( value => ! text . includes ( value ) ) ;
929+ expect ( missing ( worker ) ) . toEqual ( [ ] ) ;
930+ for ( const value of required ) {
931+ expect (
932+ missing ( worker . replaceAll ( value , 'REMOVED' ) ) ,
933+ `removing ${ value } must break the source contract` ,
934+ ) . toContain ( value ) ;
935+ }
936+
937+ const compiled = compiledWorkerLock ( ) ;
938+ expect ( missing ( compiled ) ) . toEqual ( [ ] ) ;
939+ for ( const value of required ) {
940+ expect (
941+ missing ( compiled . replaceAll ( value , 'REMOVED' ) ) ,
942+ `removing ${ value } must break the compiled contract` ,
943+ ) . toContain ( value ) ;
944+ }
945+ } , 60000 ) ;
946+
773947 // -------------------------------------------------------------------------
774948 // FIDO blocker 2: the guard module was imported from GITHUB_WORKSPACE, whose
775949 // only checkout in this job is one gh-aw emits ONLY for `create_pull_request`
@@ -783,7 +957,7 @@ describe('gh-aw implement worker: retro-origin provenance enforcement', () => {
783957 const job = safeOutputsJob ( compiledWorkerLock ( ) ) ;
784958
785959 const trustedIndex = job . indexOf ( 'name: Checkout trusted base for the provenance guard' ) ;
786- const guardIndex = job . indexOf ( 'name: Enforce retro-origin provenance before any output' ) ;
960+ const guardIndex = job . indexOf ( 'name: Enforce implement provenance before any output' ) ;
787961 const processIndex = job . indexOf ( 'name: Process Safe Outputs' ) ;
788962 expect ( trustedIndex ) . toBeGreaterThan ( - 1 ) ;
789963 expect ( guardIndex ) . toBeGreaterThan ( trustedIndex ) ;
0 commit comments