11import { afterAll , describe , expect , it } from 'vitest' ;
22import {
33 cpSync ,
4+ chmodSync ,
5+ existsSync ,
46 mkdirSync ,
57 mkdtempSync ,
68 readFileSync ,
@@ -12,6 +14,7 @@ import { createHash } from 'node:crypto';
1214import { dirname , join , resolve } from 'node:path' ;
1315import { tmpdir } from 'node:os' ;
1416import { fileURLToPath } from 'node:url' ;
17+ import { parse } from 'yaml' ;
1518import {
1619 BOOTSTRAP_BRANCH ,
1720 BOOTSTRAP_ISSUE_MARKER ,
@@ -375,6 +378,48 @@ function validateFixture(
375378 ) ;
376379}
377380
381+ function submissionFixture ( ) {
382+ const fixture = createFixture ( ) ;
383+ const targetBytes = 40_843 ;
384+ const remaining = targetBytes - Buffer . byteLength ( JSON . stringify ( fixture . payload ) ) ;
385+ const rationale = ' Repository evidence informs this proposed specialist; human review is required before activation.' ;
386+ fixture . payload . pr_body += rationale . repeat ( Math . ceil ( remaining / rationale . length ) ) . slice ( 0 , remaining ) ;
387+ const text = JSON . stringify ( fixture . payload ) ;
388+ expect ( Buffer . byteLength ( text ) ) . toBe ( targetBytes ) ;
389+ writeFileSync ( fixture . payloadPath , text ) ;
390+ const envelope = createBootstrapPayloadEnvelope ( text ) as Record < string , string > ;
391+ expect ( envelope . payload_chunk_count ) . toBe ( '7' ) ;
392+ const envelopePath = join ( fixture . root , 'envelope.json' ) ;
393+ writeFileSync ( envelopePath , JSON . stringify ( envelope ) ) ;
394+ const capture = join ( fixture . root , 'calls.jsonl' ) ;
395+ const proxy = join ( fixture . root , 'bin/safeoutputs' ) ;
396+ write ( fixture . root , 'bin/safeoutputs' , `#!${ process . execPath }
397+ const fs = require('node:fs');
398+ const args = process.argv.slice(2);
399+ if (JSON.stringify(args) !== JSON.stringify(['materialize_bootstrap', '.'])) process.exit(2);
400+ const input = JSON.parse(fs.readFileSync(0, 'utf8'));
401+ if (Object.values(input).some(value => typeof value !== 'string' || Buffer.byteLength(value) > 10240)) process.exit(3);
402+ fs.appendFileSync(process.env.CAPTURE, JSON.stringify(input) + '\\n');
403+ if (process.env.PROXY_FAIL === '1') { console.error('proxy rejected request'); process.exit(4); }
404+ console.log('tool accepted');
405+ ` ) ;
406+ chmodSync ( proxy , 0o700 ) ;
407+ const env = {
408+ ...process . env ,
409+ PATH : `${ join ( fixture . root , 'bin' ) } :${ process . env . PATH } ` ,
410+ CAPTURE : capture ,
411+ GITHUB_WORKSPACE : fixture . root ,
412+ GITHUB_REPOSITORY : 'octo/example' ,
413+ DEFAULT_BRANCH : 'main' ,
414+ } ;
415+ const submit = ( overrides = { } ) => spawnSync ( process . execPath , [
416+ VALIDATOR , '--submit-envelope' , envelopePath ,
417+ '--root' , fixture . root , '--payload' , fixture . payloadPath ,
418+ '--repository' , 'octo/example' , '--default-branch' , 'main' , '--link-mode' , 'placeholder' ,
419+ ] , { env : { ...env , ...overrides } , encoding : 'utf8' } ) ;
420+ return { ...fixture , text, envelope, envelopePath, capture, proxy, env, submit } ;
421+ }
422+
378423function compileWorkflow ( source = WORKFLOW ) : string {
379424 const root = mkdtempSync ( join ( tmpdir ( ) , 'gh-aw-bootstrap-compile-' ) ) ;
380425 workspaces . push ( root ) ;
@@ -601,12 +646,8 @@ describe('automatic Squad bootstrap workflow', () => {
601646 } ) ;
602647
603648 it ( 'fails closed for every chunk transport corruption class' , ( ) => {
604- const payloadText = JSON . stringify ( {
605- shared : 'Cast and research stay in one validated payload.' ,
606- content : 'x' . repeat ( PAYLOAD_CHUNK_BYTES * 2 ) ,
607- } ) ;
608- const valid = createBootstrapPayloadEnvelope ( payloadText ) as Record < string , string > ;
609- expect ( Number ( valid . payload_chunk_count ) ) . toBeGreaterThanOrEqual ( 3 ) ;
649+ const fixture = submissionFixture ( ) ;
650+ const valid = fixture . envelope ;
610651
611652 const mutations : Array < [ string , ( envelope : Record < string , string > ) => void , RegExp ] > = [
612653 [ 'missing' , ( envelope ) => delete envelope . payload_chunk_00 , / p a y l o a d _ c h u n k _ 0 0 i s m i s s i n g / ] ,
@@ -653,6 +694,8 @@ describe('automatic Squad bootstrap workflow', () => {
653694 / d e c o d e d l e n g t h d o e s n o t m a t c h | l e n g t h m i s m a t c h / ,
654695 ] ,
655696 [ 'hash mismatch' , ( envelope ) => { envelope . payload_sha256 = '0' . repeat ( 64 ) ; } , / S H A - 2 5 6 m i s m a t c h / ] ,
697+ [ 'noncanonical count' , ( envelope ) => { envelope . payload_chunk_count = '07' ; } , / c a n o n i c a l d e c i m a l / ] ,
698+ [ 'invalid slot' , ( envelope ) => { envelope . payload_chunk_16 = '16:QQ==' ; } , / n o t a v a l i d f i x e d c h u n k s l o t / ] ,
656699 [
657700 'trailing data' ,
658701 ( envelope ) => {
@@ -680,7 +723,74 @@ describe('automatic Squad bootstrap workflow', () => {
680723 ( ) => reconstructBootstrapPayload ( envelope ) ,
681724 `${ name } corruption must fail closed` ,
682725 ) . toThrow ( error ) ;
726+ writeFileSync ( fixture . envelopePath , JSON . stringify ( envelope ) ) ;
727+ const result = fixture . submit ( ) ;
728+ expect ( result . status , name ) . not . toBe ( 0 ) ;
729+ expect ( result . stderr , name ) . toMatch ( error ) ;
730+ expect ( existsSync ( fixture . capture ) , `${ name } must not invoke safeoutputs` ) . toBe ( false ) ;
731+ }
732+ } ) ;
733+
734+ it ( 'submits seven chunks through the authenticated runner without printing or transcribing them' , ( ) => {
735+ const fixture = submissionFixture ( ) ;
736+ cpSync ( resolve ( ROOT , 'workflows/shared' ) , join ( fixture . root , '.github/workflows/shared' ) , { recursive : true } ) ;
737+ write ( fixture . root , '.github/workflows/squad-bootstrap-payload.json' , fixture . text ) ;
738+ const config = parse ( WORKFLOW . split ( '---' ) [ 1 ] ) ;
739+ const prepare = config [ 'pre-agent-steps' ] . find (
740+ ( step : { name : string } ) => step . name === 'Prepare authenticated bootstrap validator' ,
741+ ) ;
742+ const setup = spawnSync ( 'bash' , [ '-c' , prepare . run ] , { env : fixture . env , encoding : 'utf8' } ) ;
743+ expect ( setup . status , setup . stderr ) . toBe ( 0 ) ;
744+ const result = spawnSync (
745+ join ( fixture . root , '.github/workflows/run-squad-bootstrap-validator' ) ,
746+ [ ] ,
747+ { env : fixture . env , encoding : 'utf8' } ,
748+ ) ;
749+ expect ( result . status , result . stderr ) . toBe ( 0 ) ;
750+ expect ( result . stdout ) . toBe ( 'Squad bootstrap validation passed; materialize_bootstrap submitted.\n' ) ;
751+ const calls = readFileSync ( fixture . capture , 'utf8' ) . trim ( ) . split ( '\n' ) ;
752+ expect ( calls ) . toHaveLength ( 1 ) ;
753+ expect ( JSON . parse ( calls [ 0 ] ) ) . toEqual ( fixture . envelope ) ;
754+ expect ( reconstructBootstrapPayload ( JSON . parse ( calls [ 0 ] ) ) ) . toBe ( fixture . text ) ;
755+ expect ( result . stdout . length ) . toBeLessThan ( 100 ) ;
756+ expect ( result . stderr ) . toBe ( '' ) ;
757+ } ) ;
758+
759+ it ( 'rejects schema, repository, Cast, and stale payload mutations before submitting' , ( ) => {
760+ for ( const mutate of [
761+ ( payload : ReturnType < typeof createFixture > [ 'payload' ] ) => { payload . schema_version = '2' ; } ,
762+ ( payload : ReturnType < typeof createFixture > [ 'payload' ] ) => { payload . repository = 'other/repo' ; } ,
763+ ( payload : ReturnType < typeof createFixture > [ 'payload' ] ) => { payload . default_branch = 'other' ; } ,
764+ ( payload : ReturnType < typeof createFixture > [ 'payload' ] ) => { payload . files [ 0 ] . content += '\nchanged' ; } ,
765+ ] ) {
766+ const fixture = submissionFixture ( ) ;
767+ mutate ( fixture . payload ) ;
768+ const text = JSON . stringify ( fixture . payload ) ;
769+ writeFileSync ( fixture . payloadPath , text ) ;
770+ writeFileSync ( fixture . envelopePath , JSON . stringify ( createBootstrapPayloadEnvelope ( text ) ) ) ;
771+ const result = fixture . submit ( ) ;
772+ expect ( result . status ) . not . toBe ( 0 ) ;
773+ expect ( result . stderr ) . toContain ( 'Squad bootstrap validation failed' ) ;
774+ expect ( existsSync ( fixture . capture ) ) . toBe ( false ) ;
683775 }
776+ const fixture = submissionFixture ( ) ;
777+ writeFileSync ( fixture . payloadPath , `${ fixture . text } \n` ) ;
778+ expect ( fixture . submit ( ) . stderr ) . toContain ( 'does not match the generated payload bytes' ) ;
779+ expect ( existsSync ( fixture . capture ) ) . toBe ( false ) ;
780+ } ) ;
781+
782+ it ( 'fails explicitly without retries when the mounted tool fails or is unavailable' , ( ) => {
783+ const fixture = submissionFixture ( ) ;
784+ const failed = fixture . submit ( { PROXY_FAIL : '1' } ) ;
785+ expect ( failed . status ) . not . toBe ( 0 ) ;
786+ expect ( failed . stderr ) . toContain ( 'proxy rejected request' ) ;
787+ expect ( failed . stderr ) . toContain ( 'Do not retry' ) ;
788+ expect ( readFileSync ( fixture . capture , 'utf8' ) . trim ( ) . split ( '\n' ) ) . toHaveLength ( 1 ) ;
789+ rmSync ( fixture . proxy ) ;
790+ const unavailable = fixture . submit ( { PATH : join ( fixture . root , 'bin' ) } ) ;
791+ expect ( unavailable . status ) . not . toBe ( 0 ) ;
792+ expect ( unavailable . stderr ) . toContain ( 'ENOENT' ) ;
793+ expect ( unavailable . stdout ) . toBe ( '' ) ;
684794 } ) ;
685795
686796 it ( 'enforces encoder total-size and fixed chunk-count bounds' , ( ) => {
@@ -738,6 +848,10 @@ describe('automatic Squad bootstrap workflow', () => {
738848 expect ( lock ) . toContain ( '"payload_sha256"' ) ;
739849 expect ( lock ) . not . toMatch ( / " m a t e r i a l i z e - b o o t s t r a p " : \{ " i n p u t s " : \{ " p a y l o a d " : / ) ;
740850 expect ( lock ) . toContain ( 'reconstructBootstrapPayload(items[0])' ) ;
851+ expect ( lock ) . toContain ( 'mcp-cli' ) ;
852+ expect ( lock ) . toContain ( '--submit-envelope' ) ;
853+ expect ( parse ( lock ) . jobs . agent . env . DEFAULT_BRANCH ) . toBe ( '${{ github.event.repository.default_branch }}' ) ;
854+ expect ( parse ( WORKFLOW . split ( '---' ) [ 1 ] ) . tools [ 'cli-proxy' ] ) . toBe ( true ) ;
741855 expect ( lock ) . toContain ( 'SQUAD_BOOTSTRAP_INSTALL_SHA: ${{ github.sha }}' ) ;
742856 expect ( lock ) . toContain ( 'SQUAD_BOOTSTRAP_REPOSITORY: ${{ github.repository }}' ) ;
743857 expect ( lock ) . toContain ( 'SQUAD_BOOTSTRAP_RUN_ID: ${{ github.run_id }}' ) ;
@@ -762,6 +876,16 @@ describe('automatic Squad bootstrap workflow', () => {
762876 ) ;
763877 } , 180000 ) ;
764878
879+ it ( 'detects a compiled mutation that removes the file-backed safe-output CLI' , ( ) => {
880+ const assertMounted = ( lock : string ) => {
881+ const agent = parse ( lock ) . jobs . agent ;
882+ expect ( agent . steps . some ( ( step : { id ?: string } ) => step . id === 'mount-mcp-clis' ) ) . toBe ( true ) ;
883+ expect ( lock ) . toContain ( 'GH_AW_MCP_CLI_SERVERS=\'["safeoutputs"]\'' ) ;
884+ } ;
885+ assertMounted ( compileWorkflow ( ) ) ;
886+ expect ( ( ) => assertMounted ( compileWorkflow ( WORKFLOW . replace ( ' cli-proxy: true\n' , '' ) ) ) ) . toThrow ( ) ;
887+ } , 180000 ) ;
888+
765889 it ( 'declares deterministic output bounds and paginated recovery checks' , ( ) => {
766890 expect ( WORKFLOW ) . toContain ( 'max: 1' ) ;
767891 expect ( WORKFLOW ) . toContain ( "state: 'all'" ) ;
0 commit comments