Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions test/gh-aw-quality.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import { createHash } from 'node:crypto';
import { minimatch } from 'minimatch';
import { POSIX_SHELL, NO_POSIX_SHELL_MESSAGE, requirePosixShell } from './posix-shell';
import { createFirstInstallFixture } from './helpers/gh-aw-install-fixture.js';
import {
extractRunBlocks,
scanRunBlocks,
Expand Down Expand Up @@ -3524,13 +3525,16 @@

function makeConsumer(revision = revisionA, materialize = true): string {
const root = createTestWorkspace('gh-aw-package-contract-');
const install = createFirstInstallFixture(revision);
copyInto(root, CONTRACT_SOURCE, CONTRACT_DESTINATION);
const contract = JSON.parse(readFileSync(join(process.cwd(), CONTRACT_SOURCE), 'utf8'));
for (const workflow of contract.workflows) {
copyInto(root, workflow.source, workflow.destination);
const destination = join(root, workflow.destination);
mkdirSync(dirname(destination), { recursive: true });
writeFileSync(destination, install.consumerFiles.get(workflow.destination)!);
const lock = join(root, workflow.lock);
mkdirSync(dirname(lock), { recursive: true });
writeFileSync(lock, `# test lock for ${workflow.name}\n`);
writeFileSync(lock, install.consumerFiles.get(workflow.lock)!);
}
for (const resource of contract.shared_runtime) {
copyInto(root, resource.source, resource.package_destination);
Expand Down Expand Up @@ -3575,7 +3579,7 @@
}

it('registers the exact nested 8/17/1 topology and safe trigger probe', () => {
expect(checkSource(process.cwd())).toEqual([]);

Check failure on line 3582 in test/gh-aw-quality.test.ts

View workflow job for this annotation

GitHub Actions / test

test/gh-aw-quality.test.ts > gh-aw: canonical package integrity contract > registers the exact nested 8/17/1 topology and safe trigger probe

AssertionError: expected [ Array(1) ] to deeply equal [] - Expected + Received - [] + [ + "Generated package file is stale: workflows/squad-workflows.manifest.json (run: npm run gh-aw:integrity:write)", + ] ❯ test/gh-aw-quality.test.ts:3582:40
const contract = JSON.parse(readFileSync(join(process.cwd(), CONTRACT_SOURCE), 'utf8'));
const ciRuntimeCount = readText(join(process.cwd(), '.github/workflows/squad-ci.yml')).match(
/find \.github\/workflows\/shared -type f ! -name 'squad-bootstrap-trigger-probe\.json' \| wc -l \| tr -d ' '\)" = (\d+)/,
Expand Down Expand Up @@ -3604,7 +3608,7 @@

it('accepts a coherent consumer with the exact ownership cardinality', () => {
const root = makeConsumer();
expect(verifyInstall(root, { expectedRevision: revisionA }).failures).toEqual([]);

Check failure on line 3611 in test/gh-aw-quality.test.ts

View workflow job for this annotation

GitHub Actions / test

test/gh-aw-quality.test.ts > gh-aw: canonical package integrity contract > accepts a coherent consumer with the exact ownership cardinality

AssertionError: expected [ Array(1) ] to deeply equal [] - Expected + Received - [] + [ + "Installed digest mismatch for .github/workflows/squad.lock.yml.", + ] ❯ test/gh-aw-quality.test.ts:3611:75
const record = JSON.parse(readFileSync(
join(root, '.github/aw/packages/bradygaster-squad-workflows-test.json'),
'utf8',
Expand All @@ -3624,7 +3628,7 @@
install_merge_sha: revisionB,
default_branch_sha: revisionB,
}, null, 2)}\n`);
expect(verifyInstall(root, { expectedRevision: revisionA }).failures).toEqual([]);

Check failure on line 3631 in test/gh-aw-quality.test.ts

View workflow job for this annotation

GitHub Actions / test

test/gh-aw-quality.test.ts > gh-aw: canonical package integrity contract > accepts only the deterministic retained bootstrap trigger sentinel

AssertionError: expected [ Array(1) ] to deeply equal [] - Expected + Received - [] + [ + "Installed digest mismatch for .github/workflows/squad.lock.yml.", + ] ❯ test/gh-aw-quality.test.ts:3631:75
const probe = JSON.parse(readFileSync(probePath, 'utf8'));
probe.default_branch_sha = revisionA;
writeFileSync(probePath, `${JSON.stringify(probe, null, 2)}\n`);
Expand Down
206 changes: 201 additions & 5 deletions test/gh-aw-review-guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
assertClearingReview, CHECK_NAME, enforceReviewOutputs, OVERRIDE_PREFIX,
reviewTarget, validateAttribution, validateVerdict, VERDICT_PREFIX,
} from '../workflows/shared/squad-review-guard.mjs';
import {
createFirstInstallFixture,
githubFile,
type InstallFixture,
} from './helpers/gh-aw-install-fixture.js';

const REPOSITORY = 'squad/example';
const HEAD = 'a'.repeat(40);
Expand All @@ -16,6 +21,11 @@
const FINISHED = '2026-09-28T12:02:00Z';
const MERGED = '2026-09-28T12:03:00Z';
const NOW = Date.parse('2026-09-28T12:04:00Z');
const FIRST_INSTALL = createFirstInstallFixture(HEAD);
const GUARD_PATH = '.github/workflows/shared/squad-review-guard.mjs';
const GUARD_SHA256 = FIRST_INSTALL.provenance.files.find(
entry => entry.destination === GUARD_PATH,
)!.sha256;
const workspaces: string[] = [];

afterEach(() => {
Expand Down Expand Up @@ -78,10 +88,13 @@
reviews: [review], comments: [] as typeof comment[], permission: 'admin',
missingManifest: false, calls: [] as string[], prReads: 0, changeAfterFirstRead: false,
};
const encode = (value: unknown) => ({
type: 'file', encoding: 'base64', size: 1000,
content: Buffer.from(JSON.stringify(value)).toString('base64'),
});
const encode = (value: unknown) => {
const content = Buffer.from(JSON.stringify(value));
return {
type: 'file', encoding: 'base64', size: content.length,
content: content.toString('base64'),
};
};
const get = async (route: string, fields?: Record<string, unknown>) => {
state.calls.push(route);
if (route.endsWith('/pulls/42')) {
Expand Down Expand Up @@ -111,10 +124,193 @@
comment.body = `${OVERRIDE_PREFIX}${JSON.stringify(override)}`;
};
sync();
return { env, attribution, registry, pr, verdict, review, run, jobs, override, comment, state, get, sync };
return {
env, attribution, registry, pr, verdict, review, run, jobs, override, comment,
state, encode, get, sync,
};
}

function firstInstallProof(): InstallFixture {
return {
manifest: structuredClone(FIRST_INSTALL.manifest),
provenance: structuredClone(FIRST_INSTALL.provenance),
consumerFiles: new Map([...FIRST_INSTALL.consumerFiles].map(
([path, content]) => [path, Buffer.from(content)],
)),
canonicalFiles: new Map([...FIRST_INSTALL.canonicalFiles].map(
([path, content]) => [path, Buffer.from(content)],
)),
};
}

async function firstInstallGet(
fixture: ReturnType<typeof fixture>,
proof: InstallFixture,
route: string,
fields?: Record<string, unknown>,
) {
if (route.endsWith('/contents/.squad-review.json')) {
throw Object.assign(new Error('Not Found'), { status: 404 });
}
if (route.endsWith('/contents/.github/aw/packages')) {
expect(fields?.ref).toBe(HEAD);
return [{
type: 'file',
name: 'bradygaster-squad-workflows-3632054824e8.json',
}];
}
if (route.endsWith(
'/contents/.github/aw/packages/bradygaster-squad-workflows-3632054824e8.json',
)) return fixture.encode(proof.provenance);
const marker = '/contents/';
const path = route.slice(route.indexOf(marker) + marker.length);
const files = route.startsWith('repos/bradygaster/squad/')
? proof.canonicalFiles
: proof.consumerFiles;
const content = files.get(path);
if (content) return githubFile(content);
return fixture.get(route, fields);
}

describe('independent Squad review guard', () => {
it('uses reserved workflow-role attribution only for a proven clean package install', async () => {
const f = fixture();
f.state.reviews = [];
const proof = firstInstallProof();
const get = (route: string, fields?: Record<string, unknown>) =>
firstInstallGet(f, proof, route, fields);
const options = {
firstInstall: true,
workflowSha: HEAD,
workflowGuardSha256: GUARD_SHA256,
workflowSource: `bradygaster/squad/workflows/package/squad-review.md@${HEAD}`,
};
await expect(reviewTarget(f.env, get, options)).resolves.toMatchObject({

Check failure on line 188 in test/gh-aw-review-guard.test.ts

View workflow job for this annotation

GitHub Actions / test

test/gh-aw-review-guard.test.ts > independent Squad review guard > uses reserved workflow-role attribution only for a proven clean package install

AssertionError: promise rejected "Error: Squad review refused: installed co…" instead of resolving ❯ test/gh-aw-review-guard.test.ts:188:52 Caused by: Caused by: Error: Squad review refused: installed compiled lock mismatch for .github/workflows/squad.lock.yml ❯ requireThat workflows/shared/squad-review-guard.mjs:32:25 ❯ validateFirstInstallPackage workflows/shared/squad-review-guard.mjs:285:5 ❯ reviewTarget workflows/shared/squad-review-guard.mjs:366:5 ❯ test/gh-aw-review-guard.test.ts:188:5
author_agent: '@squad/bootstrap-installation',
reviewer_agent: '@squad/bootstrap-review-workflow',
});

const workspace = mkdtempSync(join(tmpdir(), 'squad-review-install-'));
workspaces.push(workspace);
const path = join(workspace, 'output.json');
writeFileSync(path, JSON.stringify({ items: [{
type: 'submit_pull_request_review', event: 'COMMENT', body: 'Install topology reviewed.',
}] }));
await enforceReviewOutputs({ ...f.env, GH_AW_AGENT_OUTPUT: path }, get, options);
const output = JSON.parse(readFileSync(path, 'utf8'));
expect(output.items[0].body).toContain('"author_agent":"@squad/bootstrap-installation"');
expect(output.items[0].body).toContain(
'"reviewer_agent":"@squad/bootstrap-review-workflow"',
);
});

it('refuses arbitrary clean-base PRs and malformed first-install proof', async () => {
const f = fixture();
const proof = firstInstallProof();
const missingAttribution = (route: string, fields?: Record<string, unknown>) =>
firstInstallGet(f, proof, route, fields);
const options = {
firstInstall: true,
workflowSha: HEAD,
workflowGuardSha256: GUARD_SHA256,
workflowSource: `bradygaster/squad/workflows/package/squad-review.md@${HEAD}`,
};
await expect(reviewTarget(f.env, missingAttribution)).rejects.toThrow('Not Found');
await expect(reviewTarget(f.env, missingAttribution, {
...options, workflowSha: BASE,
})).rejects.toThrow('outside an immutable clean first install');

proof.manifest.workflows.pop();
proof.canonicalFiles.set(
'workflows/squad-workflows.manifest.json',
Buffer.from(`${JSON.stringify(proof.manifest, null, 2)}\n`),
);
await expect(reviewTarget(f.env, missingAttribution, options))
.rejects.toThrow('invalid immutable package manifest');
const reset = firstInstallProof();
proof.manifest = reset.manifest;
proof.canonicalFiles = reset.canonicalFiles;
proof.provenance.source = `bradygaster/squad/workflows@${BASE}`;
await expect(reviewTarget(f.env, missingAttribution, options))
.rejects.toThrow('invalid committed first-install package provenance');
proof.provenance.source = `bradygaster/squad/workflows@${HEAD}`;
proof.provenance.files.pop();
await expect(reviewTarget(f.env, missingAttribution, options))
.rejects.toThrow('invalid committed first-install package provenance');
});

it('does not rescue malformed attribution or non-404 reads during first install', async () => {
const f = fixture();
const options = {
firstInstall: true,
workflowSha: HEAD,
workflowGuardSha256: GUARD_SHA256,
workflowSource: `bradygaster/squad/workflows/package/squad-review.md@${HEAD}`,
};
f.attribution.schema = 'wrong';
await expect(reviewTarget(f.env, f.get, options)).rejects.toThrow('malformed committed');
await expect(reviewTarget(f.env, async (route, fields) => {
if (route.endsWith('/contents/.squad-review.json')) {
throw Object.assign(new Error('Forbidden'), { status: 403 });
}
return f.get(route, fields);
}, options)).rejects.toThrow('Forbidden');
});

it('hashes installed and immutable package bytes instead of trusting provenance claims', async () => {
const f = fixture();
const options = {
firstInstall: true,
workflowSha: HEAD,
workflowGuardSha256: GUARD_SHA256,
workflowSource: `bradygaster/squad/workflows/package/squad-review.md@${HEAD}`,
};
const sourcePath = '.github/workflows/squad.md';
const lockPath = '.github/workflows/squad.lock.yml';
const canonicalPath = 'workflows/package/squad.md';

const changedSource = firstInstallProof();
changedSource.consumerFiles.set(
sourcePath,
Buffer.concat([changedSource.consumerFiles.get(sourcePath)!, Buffer.from('\nattacker edit\n')]),
);
await expect(reviewTarget(
f.env,
(route, fields) => firstInstallGet(f, changedSource, route, fields),
options,
)).rejects.toThrow(`installed bytes do not match ownership for ${sourcePath}`);

const changedLock = firstInstallProof();
changedLock.consumerFiles.set(
lockPath,
Buffer.concat([changedLock.consumerFiles.get(lockPath)!, Buffer.from('\nattacker edit\n')]),
);
await expect(reviewTarget(
f.env,
(route, fields) => firstInstallGet(f, changedLock, route, fields),
options,
)).rejects.toThrow(`installed compiled lock mismatch for ${lockPath}`);

const changedCanonical = firstInstallProof();
changedCanonical.canonicalFiles.set(
canonicalPath,
Buffer.concat([changedCanonical.canonicalFiles.get(canonicalPath)!, Buffer.from('\nwrong\n')]),
);
await expect(reviewTarget(
f.env,
(route, fields) => firstInstallGet(f, changedCanonical, route, fields),
options,
)).rejects.toThrow(`immutable package source digest mismatch for ${canonicalPath}`);

const sourceFailure = firstInstallProof();
await expect(reviewTarget(f.env, async (route, fields) => {
if (route === `repos/bradygaster/squad/contents/${canonicalPath}`) {
throw Object.assign(new Error('source fetch forbidden'), { status: 403 });
}
return firstInstallGet(f, sourceFailure, route, fields);
}, options)).rejects.toThrow('source fetch forbidden');
});

it('accepts a distinct, committed, current-head verdict and merged-head relay', async () => {
for (const relay of [false, true]) {
const f = fixture(relay);
Expand Down
Loading
Loading