|
9 | 9 | */ |
10 | 10 |
|
11 | 11 | import { afterAll, describe, it, expect } from 'vitest'; |
12 | | -import { cpSync, readFileSync, existsSync, mkdirSync, mkdtempSync, writeFileSync, rmSync } from 'node:fs'; |
| 12 | +import { chmodSync, cpSync, readFileSync, existsSync, mkdirSync, mkdtempSync, writeFileSync, rmSync } from 'node:fs'; |
13 | 13 | import { join, dirname } from 'node:path'; |
14 | 14 | import { tmpdir } from 'node:os'; |
15 | 15 | import { execFileSync, execSync, spawnSync } from 'node:child_process'; |
| 16 | +import { createHash } from 'node:crypto'; |
16 | 17 | import { minimatch } from 'minimatch'; |
17 | 18 | import { POSIX_SHELL, NO_POSIX_SHELL_MESSAGE, requirePosixShell } from './posix-shell'; |
18 | 19 | import { |
@@ -167,6 +168,40 @@ function extractImports(frontmatter: string): string[] { |
167 | 168 | return imports; |
168 | 169 | } |
169 | 170 |
|
| 171 | +/** Extract the resources list from frontmatter. */ |
| 172 | +function extractResources(frontmatter: string): string[] { |
| 173 | + const resources: string[] = []; |
| 174 | + const lines = frontmatter.split('\n'); |
| 175 | + let inResources = false; |
| 176 | + |
| 177 | + for (const line of lines) { |
| 178 | + if (line.match(/^resources:\s*$/)) { |
| 179 | + inResources = true; |
| 180 | + continue; |
| 181 | + } |
| 182 | + |
| 183 | + if (inResources) { |
| 184 | + const itemMatch = line.match(/^\s+-\s+(.+)$/); |
| 185 | + if (itemMatch) { |
| 186 | + resources.push(itemMatch[1].trim()); |
| 187 | + } else if (line.match(/^\S/)) { |
| 188 | + break; |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return resources; |
| 194 | +} |
| 195 | + |
| 196 | +function extractNamedStepScript(frontmatter: string, stepName: string): string { |
| 197 | + const escapedName = stepName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 198 | + const match = frontmatter.match( |
| 199 | + new RegExp(`^ - name: ${escapedName}\\n shell: bash\\n run: \\|\\n([\\s\\S]*?)(?=^ - name:|\\n\\S)`, 'm'), |
| 200 | + ); |
| 201 | + if (!match) throw new Error(`No bash step named "${stepName}" found`); |
| 202 | + return match[1].split('\n').map(line => line.replace(/^ {6}/, '')).join('\n').trim(); |
| 203 | +} |
| 204 | + |
170 | 205 | function extractWorkflowDispatchInputs(frontmatter: string): Record<string, Record<string, string>> { |
171 | 206 | const inputs: Record<string, Record<string, string>> = {}; |
172 | 207 | const lines = frontmatter.split('\n'); |
@@ -568,6 +603,111 @@ describe('gh-aw: shared component imports', () => { |
568 | 603 | expect(imports.length).toBeGreaterThan(0); |
569 | 604 | }); |
570 | 605 |
|
| 606 | + describe('#1982: experimental gh-aw resource delivery probe', () => { |
| 607 | + const frontmatter = extractFrontmatter(SQUAD_WORKFLOW); |
| 608 | + const probeResource = 'shared/squad-gh-aw-resource-probe.txt'; |
| 609 | + const probePath = join(WORKFLOWS_DIR, probeResource); |
| 610 | + const expectedDigest = 'dba0c331c0b0fda06539bd9245dda72bd9c36cca6962726147b03168c1d97a73'; |
| 611 | + const stepName = 'Assert experimental gh-aw resource delivery'; |
| 612 | + |
| 613 | + it('declares the plaintext probe on the top-level direct-install workflow only', () => { |
| 614 | + expect(extractResources(frontmatter)).toEqual([probeResource]); |
| 615 | + expect(existsSync(probePath)).toBe(true); |
| 616 | + expect(readText(probePath)).toContain('Experiment: bradygaster/squad#1982'); |
| 617 | + expect(readText(probePath)).toContain('no executable or runtime behavior'); |
| 618 | + |
| 619 | + for (const sharedFile of [ |
| 620 | + 'squad.md', |
| 621 | + 'squad-cast-validator.md', |
| 622 | + 'squad-planning-ontology.md', |
| 623 | + 'squad-planning-policy.md', |
| 624 | + ]) { |
| 625 | + expect(readText(join(SHARED_DIR, sharedFile))).not.toMatch(/^resources:/m); |
| 626 | + } |
| 627 | + }); |
| 628 | + |
| 629 | + it('pins the exact plaintext bytes and checks missing, unreadable, and mismatched resources', () => { |
| 630 | + const actualDigest = createHash('sha256').update(readFileSync(probePath)).digest('hex'); |
| 631 | + const script = extractNamedStepScript(frontmatter, stepName); |
| 632 | + |
| 633 | + expect(actualDigest).toBe(expectedDigest); |
| 634 | + expect(script).toContain(`expected_sha256="${expectedDigest}"`); |
| 635 | + expect(script).toContain('if [ ! -f "$probe" ]'); |
| 636 | + expect(script).toContain('if [ ! -r "$probe" ]'); |
| 637 | + expect(script).toContain('Experimental gh-aw resource probe SHA-256 mismatch'); |
| 638 | + }); |
| 639 | + |
| 640 | + it('fails closed before the agent for a missing or modified installed probe', () => { |
| 641 | + const script = extractNamedStepScript(frontmatter, stepName); |
| 642 | + const bash = process.platform === 'win32' ? requirePosixShell() : 'bash'; |
| 643 | + const workspace = createTestWorkspace('gh-aw-resource-probe-'); |
| 644 | + const installedProbe = join(workspace, '.github', 'workflows', probeResource); |
| 645 | + mkdirSync(dirname(installedProbe), { recursive: true }); |
| 646 | + |
| 647 | + const missing = spawnSync(bash, ['-c', script], { |
| 648 | + cwd: workspace, |
| 649 | + encoding: 'utf8', |
| 650 | + env: { ...process.env, GITHUB_WORKSPACE: workspace, RUNNER_TEMP: workspace }, |
| 651 | + }); |
| 652 | + expect(missing.status).not.toBe(0); |
| 653 | + expect(missing.stderr).toContain('Experimental gh-aw resource probe is missing'); |
| 654 | + |
| 655 | + writeFileSync(installedProbe, 'tampered\n'); |
| 656 | + const modified = spawnSync(bash, ['-c', script], { |
| 657 | + cwd: workspace, |
| 658 | + encoding: 'utf8', |
| 659 | + env: { ...process.env, GITHUB_WORKSPACE: workspace, RUNNER_TEMP: workspace }, |
| 660 | + }); |
| 661 | + expect(modified.status).not.toBe(0); |
| 662 | + expect(modified.stderr).toContain('Experimental gh-aw resource probe SHA-256 mismatch'); |
| 663 | + |
| 664 | + cpSync(probePath, installedProbe); |
| 665 | + const verified = spawnSync(bash, ['-c', script], { |
| 666 | + cwd: workspace, |
| 667 | + encoding: 'utf8', |
| 668 | + env: { ...process.env, GITHUB_WORKSPACE: workspace, RUNNER_TEMP: workspace }, |
| 669 | + }); |
| 670 | + expect(verified.status).toBe(0); |
| 671 | + expect(verified.stdout).toContain('Experimental gh-aw resource delivery verified'); |
| 672 | + }); |
| 673 | + |
| 674 | + it.skipIf( |
| 675 | + process.platform === 'win32' || |
| 676 | + typeof process.getuid !== 'function' || |
| 677 | + process.getuid() === 0, |
| 678 | + )('fails closed before the agent for an unreadable installed probe', () => { |
| 679 | + const script = extractNamedStepScript(frontmatter, stepName); |
| 680 | + const workspace = createTestWorkspace('gh-aw-resource-probe-unreadable-'); |
| 681 | + const installedProbe = join(workspace, '.github', 'workflows', probeResource); |
| 682 | + const bash = 'bash'; |
| 683 | + mkdirSync(dirname(installedProbe), { recursive: true }); |
| 684 | + cpSync(probePath, installedProbe); |
| 685 | + |
| 686 | + try { |
| 687 | + chmodSync(installedProbe, 0o000); |
| 688 | + const permissionCheck = spawnSync( |
| 689 | + bash, |
| 690 | + ['-c', '[ ! -r "$1" ]', 'bash', installedProbe], |
| 691 | + { encoding: 'utf8' }, |
| 692 | + ); |
| 693 | + expect( |
| 694 | + permissionCheck.status, |
| 695 | + 'fixture must be unreadable to Bash before exercising the workflow branch', |
| 696 | + ).toBe(0); |
| 697 | + |
| 698 | + const unreadable = spawnSync(bash, ['-c', script], { |
| 699 | + cwd: workspace, |
| 700 | + encoding: 'utf8', |
| 701 | + env: { ...process.env, GITHUB_WORKSPACE: workspace, RUNNER_TEMP: workspace }, |
| 702 | + }); |
| 703 | + expect(unreadable.status).not.toBe(0); |
| 704 | + expect(unreadable.stderr).toContain('Experimental gh-aw resource probe is not readable'); |
| 705 | + } finally { |
| 706 | + chmodSync(installedProbe, 0o600); |
| 707 | + } |
| 708 | + }); |
| 709 | + }); |
| 710 | + |
571 | 711 | it('each imported file exists in workflows/shared/', () => { |
572 | 712 | for (const importPath of imports) { |
573 | 713 | const fullPath = join(WORKFLOWS_DIR, importPath); |
@@ -947,7 +1087,10 @@ describe('gh-aw: prompt budget & planning import regression', () => { |
947 | 1087 | // moved from agent-transcribed prompt text into a deterministic pre-agent runner. |
948 | 1088 | // The runner also emits a machine-readable factual failure record. The Cast skill |
949 | 1089 | // shrank, and the ambient prompt remains below its independently enforced 40 KB cap. |
950 | | - const SOURCE_GROWTH_BUDGET_KB = 194; |
| 1090 | + // Raised 194 -> 196 KB by #1982's gh-aw-only resource assertion. The deterministic |
| 1091 | + // step runs before the agent and does not enter its prompt; the separate ambient |
| 1092 | + // prompt budget remains the authoritative delivered-context guard. |
| 1093 | + const SOURCE_GROWTH_BUDGET_KB = 196; |
951 | 1094 | const SOURCE_GROWTH_BUDGET_BYTES = SOURCE_GROWTH_BUDGET_KB * 1024; |
952 | 1095 |
|
953 | 1096 | it('squad-planning-ontology.md is in the imports list', () => { |
|
0 commit comments