diff --git a/.github/workflows/native-artifacts.yml b/.github/workflows/native-artifacts.yml index 89b83dfd7..dd0ef4fde 100644 --- a/.github/workflows/native-artifacts.yml +++ b/.github/workflows/native-artifacts.yml @@ -7,35 +7,60 @@ on: description: Source commit to build; defaults to the calling workflow commit. type: string default: "" + reuse-verified: + description: Reuse this week's verified payload when all native build and proof inputs match. + type: boolean + default: false workflow_dispatch: permissions: contents: read jobs: - unix: - uses: ./.github/workflows/native-unix.yml - with: - ref: ${{ inputs.ref || github.sha }} - windows: - uses: ./.github/workflows/native-windows.yml - with: - ref: ${{ inputs.ref || github.sha }} - musl: - uses: ./.github/workflows/native-musl.yml - with: - ref: ${{ inputs.ref || github.sha }} - bundle: - needs: [unix, windows, musl] + prepare: runs-on: ubuntu-24.04 + outputs: + cache-key: ${{ steps.key.outputs.key }} + cache-hit: ${{ steps.restore.outputs.cache-hit }} steps: - - name: Download verified platform payloads - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + - name: Checkout native inputs + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: ${{ inputs.ref || github.sha }} + persist-credentials: false + - name: Identify native build and proof inputs + id: key + shell: bash + run: | + set -euo pipefail + # Include filenames and modes, not only file contents. Windows proofs + # bundle MCP helpers, including their schema and workbench dependencies. + source_hash="$(git ls-tree -r -z HEAD -- \ + .gitattributes .cargo .npmrc .pnpmfile.cjs package.json pnpm-workspace.yaml \ + .github/actions/setup-tools .github/scripts \ + .github/workflows/native-artifacts.yml \ + .github/workflows/native-unix.yml \ + .github/workflows/native-windows.yml \ + .github/workflows/native-musl.yml \ + sdk/typescript/.npmrc sdk/typescript/.pnpmfile.cjs \ + sdk/typescript/package.json sdk/typescript/pnpm-lock.yaml \ + sdk/typescript/pnpm-workspace.yaml \ + sdk/typescript/tsconfig.json sdk/typescript/tsconfig.ci.json \ + plugins/codex-security/native plugins/codex-security/mcp-app \ + plugins/codex-security/schemas plugins/codex-security/scripts \ + plugins/codex-security/shared | sha256sum)" + # Hosted runner images can change without a source change. + printf 'key=native-universal-v1-%s-%s\n' "$(date -u +%G-%V)" "${source_hash%% *}" >> "$GITHUB_OUTPUT" + - name: Restore verified native payload + if: inputs.reuse-verified + id: restore + continue-on-error: true + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: - pattern: native-platform-*-${{ inputs.ref || github.sha }} - merge-multiple: true path: native - - name: Upload universal native payload + key: ${{ steps.key.outputs.key }} + - name: Upload reused native payload for this commit + if: steps.restore.outputs.cache-hit == 'true' uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: native-universal-${{ inputs.ref || github.sha }} @@ -43,3 +68,67 @@ jobs: if-no-files-found: error overwrite: true retention-days: 7 + unix: + needs: prepare + if: needs.prepare.outputs.cache-hit != 'true' + uses: ./.github/workflows/native-unix.yml + with: + ref: ${{ inputs.ref || github.sha }} + windows: + needs: prepare + if: needs.prepare.outputs.cache-hit != 'true' + uses: ./.github/workflows/native-windows.yml + with: + ref: ${{ inputs.ref || github.sha }} + musl: + needs: prepare + if: needs.prepare.outputs.cache-hit != 'true' + uses: ./.github/workflows/native-musl.yml + with: + ref: ${{ inputs.ref || github.sha }} + bundle: + needs: [prepare, unix, windows, musl] + if: ${{ !cancelled() }} + runs-on: ubuntu-24.04 + steps: + - name: Require a verified native payload + shell: bash + env: + PREPARE_RESULT: ${{ needs.prepare.result }} + CACHE_HIT: ${{ needs.prepare.outputs.cache-hit }} + UNIX_RESULT: ${{ needs.unix.result }} + WINDOWS_RESULT: ${{ needs.windows.result }} + MUSL_RESULT: ${{ needs.musl.result }} + run: | + set -euo pipefail + [[ "$PREPARE_RESULT" == success ]] + if [[ "$CACHE_HIT" != true ]]; then + [[ "$UNIX_RESULT" == success && "$WINDOWS_RESULT" == success && "$MUSL_RESULT" == success ]] + fi + - name: Download verified platform payloads + if: needs.prepare.outputs.cache-hit != 'true' + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + pattern: native-platform-*-${{ inputs.ref || github.sha }} + merge-multiple: true + path: native + - name: Upload universal native payload + if: needs.prepare.outputs.cache-hit != 'true' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: native-universal-${{ inputs.ref || github.sha }} + path: native/ + if-no-files-found: error + overwrite: true + retention-days: 7 + - name: Save verified native payload from main + if: >- + needs.prepare.outputs.cache-hit != 'true' && + github.ref == 'refs/heads/main' && + (github.event_name == 'push' || github.event_name == 'schedule') && + (inputs.ref == '' || inputs.ref == github.sha) + continue-on-error: true + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: native + key: ${{ needs.prepare.outputs.cache-key }} diff --git a/.github/workflows/native-windows.yml b/.github/workflows/native-windows.yml index c7ad3b39f..ce7a01df9 100644 --- a/.github/workflows/native-windows.yml +++ b/.github/workflows/native-windows.yml @@ -43,6 +43,9 @@ jobs: with: architecture: ${{ matrix.arch }} compile: true + cache-dependency-path: | + sdk/typescript/pnpm-lock.yaml + plugins/codex-security/mcp-app/pnpm-lock.yaml - name: Install helper dependencies working-directory: . run: pnpm --dir plugins/codex-security/mcp-app install --frozen-lockfile diff --git a/.github/workflows/node-ci.yml b/.github/workflows/node-ci.yml index 0898eec6f..918dc42b1 100644 --- a/.github/workflows/node-ci.yml +++ b/.github/workflows/node-ci.yml @@ -146,6 +146,8 @@ jobs: native: needs: classify-changes uses: ./.github/workflows/native-artifacts.yml + with: + reuse-verified: true container-validate: needs: [classify-changes, native] diff --git a/sdk/typescript/tests-ts/container-release-workflow.test.ts b/sdk/typescript/tests-ts/container-release-workflow.test.ts index dee5ed142..f3e6f520f 100644 --- a/sdk/typescript/tests-ts/container-release-workflow.test.ts +++ b/sdk/typescript/tests-ts/container-release-workflow.test.ts @@ -223,12 +223,16 @@ test("builds and downloads native payloads from the same source as the image", ( "${{ inputs.ref || github.sha }}", ); } - expect(native.jobs["bundle"]!.steps![0]?.with?.["pattern"]).toBe( - "native-platform-*-${{ inputs.ref || github.sha }}", - ); - expect(native.jobs["bundle"]!.steps![1]?.with?.["name"]).toBe( - "native-universal-${{ inputs.ref || github.sha }}", - ); + expect( + native.jobs["bundle"]!.steps!.find( + (step) => step.name === "Download verified platform payloads", + )?.with?.["pattern"], + ).toBe("native-platform-*-${{ inputs.ref || github.sha }}"); + expect( + native.jobs["bundle"]!.steps!.find( + (step) => step.name === "Upload universal native payload", + )?.with?.["name"], + ).toBe("native-universal-${{ inputs.ref || github.sha }}"); for (const job of ["publish-platform", "manifest"]) { expect(release.jobs[job]!.steps![0]?.with?.["ref"]).toBe( "${{ needs.authorize.outputs.sha }}", diff --git a/sdk/typescript/tests-ts/native-artifact-cache.test.ts b/sdk/typescript/tests-ts/native-artifact-cache.test.ts new file mode 100644 index 000000000..604e9e43c --- /dev/null +++ b/sdk/typescript/tests-ts/native-artifact-cache.test.ts @@ -0,0 +1,213 @@ +import { spawnSync } from "node:child_process"; +import { mkdtempSync, readFileSync, renameSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { expect, test } from "bun:test"; +import { git } from "./git-fixture.js"; +import { bashCommand, writeSource } from "./support/shell.js"; + +interface Step { + name?: string; + id?: string; + uses?: string; + run?: string; + if?: string; + with?: Record; + "continue-on-error"?: boolean; +} + +function workflow(name: string) { + return Bun.YAML.parse( + readFileSync( + new URL(`../../../.github/workflows/${name}.yml`, import.meta.url), + "utf8", + ), + ) as { + on: { workflow_call?: { inputs: Record } }; + jobs: Record< + string, + { + needs?: string | string[]; + if?: string; + with?: Record; + steps?: Step[]; + } + >; + }; +} + +const native = workflow("native-artifacts"); +const prepare = native.jobs["prepare"]!; +const keyScript = prepare.steps!.find((step) => step.id === "key")!.run!; + +test("native reuse follows committed build inputs, filenames and the qualification week", () => { + const root = mkdtempSync(join(tmpdir(), "native-cache-")); + const repository = join(root, "repository"); + const output = join(root, "output"); + const source = "plugins/codex-security/native/src/lib.rs"; + const helper = "plugins/codex-security/mcp-app/src/native.ts"; + writeSource(repository, source, "native source\n"); + writeSource(repository, helper, "helper source\n"); + writeSource(repository, "README.md", "documentation\n"); + git(repository, "init", "-q"); + const commit = () => { + git(repository, "add", "."); + git(repository, "commit", "-qm", "fixture"); + }; + const key = (week = "2026-43") => { + writeSource(root, "output", ""); + const result = spawnSync( + bashCommand(), + [ + "--noprofile", + "--norc", + "-c", + `date() { printf '%s\\n' "$TEST_WEEK"; }\n${keyScript}`, + ], + { + cwd: repository, + encoding: "utf8", + env: { ...process.env, GITHUB_OUTPUT: output, TEST_WEEK: week }, + }, + ); + expect(result.status, result.stderr).toBe(0); + return readFileSync(output, "utf8").trim(); + }; + try { + commit(); + const initial = key(); + expect(initial).toMatch(/^key=native-universal-v1-2026-43-[a-f0-9]{64}$/); + writeSource(repository, "README.md", "updated documentation\n"); + commit(); + expect(key()).toBe(initial); + + writeSource(repository, source, "updated native source\n"); + commit(); + const changedNative = key(); + expect(changedNative).not.toBe(initial); + + writeSource(repository, helper, "updated helper source\n"); + commit(); + const changedHelper = key(); + expect(changedHelper).not.toBe(changedNative); + + renameSync( + join(repository, source), + join(repository, "plugins/codex-security/native/src/renamed.rs"), + ); + commit(); + expect(key()).not.toBe(changedHelper); + expect(key("2026-44")).not.toBe(key()); + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); + +test("only exact hits replace fresh native builds and cache failures fall back to proofs", () => { + const restore = prepare.steps!.find((step) => step.id === "restore")!; + expect(restore["continue-on-error"]).toBe(true); + expect(restore.with).not.toHaveProperty("restore-keys"); + const upload = prepare.steps!.find((step) => + step.uses?.startsWith("actions/upload-artifact@"), + )!; + expect(upload.if).toBe("steps.restore.outputs.cache-hit == 'true'"); + expect(upload.with?.["if-no-files-found"]).toBe("error"); + expect(upload.with?.["name"]).toBe( + "native-universal-${{ inputs.ref || github.sha }}", + ); + for (const platform of ["unix", "windows", "musl"]) { + expect(native.jobs[platform]).toMatchObject({ + needs: "prepare", + if: "needs.prepare.outputs.cache-hit != 'true'", + }); + } +}); + +test("only a successful fresh matrix can save a main-branch cache", () => { + const bundle = native.jobs["bundle"]!; + expect(bundle.needs).toEqual(["prepare", "unix", "windows", "musl"]); + expect(bundle.if).toBe("${{ !cancelled() }}"); + const save = bundle.steps!.find((step) => + step.uses?.startsWith("actions/cache/save@"), + )!; + expect(save.if).toBe( + "needs.prepare.outputs.cache-hit != 'true' && github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'schedule') && (inputs.ref == '' || inputs.ref == github.sha)", + ); + expect(save["continue-on-error"]).toBe(true); +}); + +test.each([ + { + cache: "true", + prepare: "success", + unix: "skipped", + windows: "skipped", + musl: "skipped", + passes: true, + }, + { + cache: "false", + prepare: "success", + unix: "success", + windows: "success", + musl: "success", + passes: true, + }, + { + cache: "true", + prepare: "failure", + unix: "skipped", + windows: "skipped", + musl: "skipped", + passes: false, + }, + { + cache: "false", + prepare: "success", + unix: "success", + windows: "failure", + musl: "success", + passes: false, + }, + { + cache: "", + prepare: "success", + unix: "success", + windows: "success", + musl: "skipped", + passes: false, + }, +])("native completion checks the cache or every fresh proof: %j", (result) => { + const guard = native.jobs["bundle"]!.steps![0]!.run!; + const checked = spawnSync(bashCommand(), ["-c", guard], { + encoding: "utf8", + env: { + ...process.env, + CACHE_HIT: result.cache, + PREPARE_RESULT: result.prepare, + UNIX_RESULT: result.unix, + WINDOWS_RESULT: result.windows, + MUSL_RESULT: result.musl, + }, + }); + expect(checked.status === 0, checked.stderr).toBe(result.passes); +}); + +test("source CI opts in while release and standalone qualification remain fresh", () => { + expect(native.on.workflow_call?.inputs["reuse-verified"]?.default).toBe( + false, + ); + expect(workflow("node-ci").jobs["native"]?.with?.["reuse-verified"]).toBe( + true, + ); + for (const caller of [ + "node-release", + "container-release", + "container-ci", + "test-quality", + ]) { + expect( + workflow(caller).jobs["native"]?.with?.["reuse-verified"], + ).toBeUndefined(); + } +});