diff --git a/.github/workflows/check-omm-artin-native.yml b/.github/workflows/check-omm-artin-native.yml new file mode 100644 index 000000000..e5f3b5d22 --- /dev/null +++ b/.github/workflows/check-omm-artin-native.yml @@ -0,0 +1,144 @@ +name: MathGraph OMM fixed Artin native qualification +on: + push: + branches: ['feature/check-omm-claim-bridge-v1-20261105'] + paths: + - '.github/workflows/check-omm-artin-native.yml' + - 'integrations/mathgraph-check-github/omm-v1/**' + - 'integrations/mathgraph-check-github/mention-artin-control-v1.mjs' + workflow_dispatch: +permissions: + contents: read +jobs: + frozen-source-preflight: + runs-on: ubuntu-24.04 + timeout-minutes: 8 + strategy: + fail-fast: false + matrix: + pr: [239, 240] + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 + with: + node-version: '24' + - name: Check exact public PR heads and source bytes + env: + GITHUB_TOKEN: ${{ github.token }} + run: node integrations/mathgraph-check-github/omm-v1/artin-preflight.mjs '${{ matrix.pr }}' "$RUNNER_TEMP/omm-artin-${{ matrix.pr }}" + - name: Carry authenticated, public, frozen inputs into isolated job + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 + with: + name: omm-artin-bound-input-${{ matrix.pr }} + path: ${{ runner.temp }}/omm-artin-${{ matrix.pr }}/ + if-no-files-found: error + retention-days: 14 + + native-original-target-replay: + needs: frozen-source-preflight + runs-on: ubuntu-24.04 + timeout-minutes: 45 + strategy: + fail-fast: false + matrix: + pr: [239, 240] + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 + with: + node-version: '24' + - name: Obtain exact-head public inputs from separate preflight job + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 + with: + name: omm-artin-bound-input-${{ matrix.pr }} + path: ${{ runner.temp }}/omm-artin-${{ matrix.pr }} + - name: Acquire checked Lean 4.28.0 distribution + run: | + set -euo pipefail + mkdir -p "$RUNNER_TEMP/lean-428" + curl --proto '=https' --tlsv1.2 -fL --retry 2 https://github.com/leanprover/lean4/releases/download/v4.28.0/lean-4.28.0-linux.tar.zst -o "$RUNNER_TEMP/lean-428.tar.zst" + echo "ceb3a3f844f7aebf63245e2b51c28d5b0ed38942c19f93cf3febd520302160bd $RUNNER_TEMP/lean-428.tar.zst" | sha256sum -c - + tar --zstd -xf "$RUNNER_TEMP/lean-428.tar.zst" --strip-components=1 -C "$RUNNER_TEMP/lean-428" + echo "$RUNNER_TEMP/lean-428/bin" >> "$GITHUB_PATH" + - name: Build pinned Mathlib environment with no App or LLM secrets + working-directory: ${{ runner.temp }}/omm-artin-${{ matrix.pr }} + run: | + set -euo pipefail + printf '%s\n' 'leanprover/lean4:v4.28.0' > lean-toolchain + cat > lakefile.lean <<'LEAN' + import Lake + open Lake DSL + package MathGraphArtinControls where + require mathlib from git "https://github.com/leanprover-community/mathlib4.git" @ "8f9d9cff6bd728b17a24e163c9402775d9e6a365" + lean_lib Candidate where + LEAN + lake update + node --input-type=module -e "import fs from 'node:fs';const d=JSON.parse(fs.readFileSync('lake-manifest.json'));if(d.packages.find(p=>p.name==='mathlib')?.rev!=='8f9d9cff6bd728b17a24e163c9402775d9e6a365')throw Error('MATHLIB_REVISION_MISMATCH')" + lake exe cache get + - name: Replay candidate, original statement consumer and axiom audit + env: + LEAN_NUM_THREADS: '1' + run: node integrations/mathgraph-check-github/omm-v1/artin-native-cli.mjs "$RUNNER_TEMP/omm-artin-${{ matrix.pr }}" + - name: Preserve evidence, negative controls and unknowns + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 + with: + name: omm-artin-native-${{ matrix.pr }} + path: | + ${{ runner.temp }}/omm-artin-${{ matrix.pr }}/request.json + ${{ runner.temp }}/omm-artin-${{ matrix.pr }}/published-exercise.lean.txt + ${{ runner.temp }}/omm-artin-${{ matrix.pr }}/candidate.lean.txt + ${{ runner.temp }}/omm-artin-${{ matrix.pr }}/lake-manifest.json + ${{ runner.temp }}/omm-artin-${{ matrix.pr }}/output/* + retention-days: 30 + if-no-files-found: warn + + publish-first-party-app-check: + needs: native-original-target-replay + runs-on: ubuntu-24.04 + timeout-minutes: 10 + concurrency: + group: mathgraph-omm-owned-artin-app-publication-${{ matrix.pr }} + cancel-in-progress: false + strategy: + fail-fast: false + matrix: + pr: [239, 240] + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 + with: + node-version: '24' + - name: Load no-secret qualified native receipt for this exact workflow run + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 + with: + name: omm-artin-native-${{ matrix.pr }} + path: ${{ runner.temp }}/omm-app-${{ matrix.pr }} + - name: Mint Check-only token from existing registered MathGraph App + id: app-token + uses: actions/create-github-app-token@fee1f7d63c2ff003460e3d139729b119787bc349 + with: + app-id: 5308912 + private-key: ${{ secrets.MATHGRAPH_CHECK_APP_PRIVATE_KEY }} + owner: metalogiclabs + repositories: mathgraph + permission-checks: write + permission-pull-requests: read + - name: Verify original Artin obligation then publish neutral, exact-head App Check + env: + MATHGRAPH_APP_INSTALLATION_TOKEN: ${{ steps.app-token.outputs.token }} + run: node integrations/mathgraph-check-github/omm-v1/app-owned-publish.mjs '${{ matrix.pr }}' "$RUNNER_TEMP/omm-app-${{ matrix.pr }}" + - name: Record registered-App readback if available + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 + with: + name: omm-app-publication-${{ matrix.pr }} + path: ${{ runner.temp }}/omm-app-${{ matrix.pr }}/output/app-publish-receipt.json + if-no-files-found: warn + retention-days: 30 diff --git a/.github/workflows/check-omm-claim-bridge.yml b/.github/workflows/check-omm-claim-bridge.yml new file mode 100644 index 000000000..a21b79a06 --- /dev/null +++ b/.github/workflows/check-omm-claim-bridge.yml @@ -0,0 +1,31 @@ +name: MathGraph OMM claim bridge — additive qualification +on: + push: + branches: [feature/check-omm-claim-bridge-v1-20261105] + pull_request: + paths: + - 'integrations/mathgraph-check-github/omm-v1/**' + - '.github/workflows/check-omm-claim-bridge.yml' +permissions: + contents: read +jobs: + contracts: + runs-on: ubuntu-24.04 + timeout-minutes: 15 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + with: + persist-credentials: false + fetch-depth: 0 + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 + with: + node-version: '24' + - name: OMM source/receipt/report RED-GREEN controls + run: node --test integrations/mathgraph-check-github/omm-v1/*.test.mjs + - name: Existing MathGraph Check contract regression + run: node --test integrations/mathgraph-check-github/*.test.mjs integrations/mathgraph-check-github/pr-pilot-v1/*.test.mjs integrations/mathgraph-check-github/cloudflare/*.test.mjs + - name: Existing L4YAML runtime and page unchanged from selected pilot + run: | + git diff --exit-code b5bf21db259c595dfdd731bdec8837c4d165578a HEAD -- \ + integrations/mathgraph-check-github/pr-pilot-v1 \ + integrations/mathgraph-check-github/cloudflare diff --git a/.github/workflows/check-omm-intake-lifecycle.yml b/.github/workflows/check-omm-intake-lifecycle.yml new file mode 100644 index 000000000..f86e68f38 --- /dev/null +++ b/.github/workflows/check-omm-intake-lifecycle.yml @@ -0,0 +1,45 @@ +name: MathGraph OMM exact-head App lifecycle — first-party qualification +on: + push: + branches: [feature/check-omm-claim-bridge-v1-20261105] + paths: + - '.github/workflows/check-omm-intake-lifecycle.yml' + - 'integrations/mathgraph-check-github/omm-v1/**' +permissions: + contents: read +jobs: + publish-owned-unknown-and-withdraw-previous: + if: >- + github.repository == 'metalogiclabs/mathgraph' && + github.ref == 'refs/heads/feature/check-omm-claim-bridge-v1-20261105' && + github.actor_id == 32909488 + runs-on: ubuntu-24.04 + timeout-minutes: 10 + concurrency: + group: mathgraph-omm-intake-lifecycle-pr270 + cancel-in-progress: false + queue: max + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 + with: + node-version: '24' + - name: Verify fail-closed transition contract before any App credentials + run: node --test integrations/mathgraph-check-github/omm-v1/lifecycle-check.test.mjs + - name: Mint Check-only token for installed MathGraph GitHub App + id: app-token + uses: actions/create-github-app-token@fee1f7d63c2ff003460e3d139729b119787bc349 + with: + app-id: 5308912 + private-key: ${{ secrets.MATHGRAPH_CHECK_APP_PRIVATE_KEY }} + owner: metalogiclabs + repositories: mathgraph + permission-checks: write + permission-pull-requests: read + - name: Independent PR270 SHA readback, predecessor withdrawal and UNKNOWN App Check + env: + MATHGRAPH_APP_INSTALLATION_TOKEN: ${{ steps.app-token.outputs.token }} + GITHUB_ACTOR_ID: ${{ github.actor_id }} + run: node integrations/mathgraph-check-github/omm-v1/lifecycle-check.mjs diff --git a/.github/workflows/check-omm-native-sandbox-smoke.yml b/.github/workflows/check-omm-native-sandbox-smoke.yml new file mode 100644 index 000000000..b71d194e8 --- /dev/null +++ b/.github/workflows/check-omm-native-sandbox-smoke.yml @@ -0,0 +1,52 @@ +name: MathGraph OMM unprivileged Lean sandbox — owned controls only +on: + push: + branches: [feature/check-omm-claim-bridge-v1-20261105] + paths: + - '.github/workflows/check-omm-native-sandbox-smoke.yml' + - 'integrations/mathgraph-check-github/omm-v1/sandbox-*.mjs' +permissions: + contents: read +jobs: + no-secrets-no-network-owned-fixtures: + if: >- + github.repository == 'metalogiclabs/mathgraph' && + github.ref == 'refs/heads/feature/check-omm-claim-bridge-v1-20261105' && + github.actor_id == 32909488 + runs-on: ubuntu-24.04 + timeout-minutes: 25 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 + with: + node-version: '24' + - name: Verify isolation policy and success/failure classification before container build + run: node --test integrations/mathgraph-check-github/omm-v1/sandbox-*.test.mjs + - name: Obtain SHA-256-verified Lean 4.28.0 runtime without private credentials + run: | + set -euo pipefail + mkdir -p "$RUNNER_TEMP/omm-lean-image/lean" + curl --proto '=https' --tlsv1.2 -fL --retry 2 https://github.com/leanprover/lean4/releases/download/v4.28.0/lean-4.28.0-linux.tar.zst -o "$RUNNER_TEMP/omm-lean-428.tar.zst" + echo "ceb3a3f844f7aebf63245e2b51c28d5b0ed38942c19f93cf3febd520302160bd $RUNNER_TEMP/omm-lean-428.tar.zst" | sha256sum -c - + tar --zstd -xf "$RUNNER_TEMP/omm-lean-428.tar.zst" --strip-components=1 -C "$RUNNER_TEMP/omm-lean-image/lean" + - name: Build distinct unprivileged image from existing read-only Lean Dockerfile + run: | + set -euo pipefail + docker build --pull -t mathgraph-lean-sandbox:omm-v1 -f integrations/mathgraph-check-github/Dockerfile.native-demo "$RUNNER_TEMP/omm-lean-image" + docker image inspect --format '{{.Id}}' mathgraph-lean-sandbox:omm-v1 > "$RUNNER_TEMP/omm-sandbox-image-id.txt" + - name: Execute owned positive/negative Lean fixtures and real filesystem/network isolation probe + env: + GITHUB_ACTOR_ID: ${{ github.actor_id }} + run: node integrations/mathgraph-check-github/omm-v1/sandbox-smoke.mjs + - name: Retain bounded smoke receipt, environment identity and negative controls + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 + with: + name: omm-native-sandbox-smoke-${{ github.run_id }} + path: | + ${{ runner.temp }}/omm-sandbox-smoke.json + ${{ runner.temp }}/omm-sandbox-image-id.txt + if-no-files-found: warn + retention-days: 30 diff --git a/.github/workflows/check-omm-owned-optin.yml b/.github/workflows/check-omm-owned-optin.yml new file mode 100644 index 000000000..4f4bc66e8 --- /dev/null +++ b/.github/workflows/check-omm-owned-optin.yml @@ -0,0 +1,148 @@ +name: MathGraph OMM real owner-comment → fixed native → registered App (qualification only) +on: + push: + branches: ['feature/check-omm-claim-bridge-v1-20261105'] + paths: + - '.github/workflows/check-omm-owned-optin.yml' + - 'integrations/mathgraph-check-github/omm-v1/owned-comment*.mjs' + - 'integrations/mathgraph-check-github/omm-v1/command.mjs' +permissions: + contents: read +jobs: + authenticate-owned-comment-and-frozen-source: + if: >- + github.repository == 'metalogiclabs/mathgraph' && + github.ref == 'refs/heads/feature/check-omm-claim-bridge-v1-20261105' && + github.actor_id == 32909488 + runs-on: ubuntu-24.04 + timeout-minutes: 10 + permissions: + contents: read + issues: read + pull-requests: read + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 + with: + node-version: '24' + - name: Verify owner comment and publication-gate contracts before native work + run: node --test integrations/mathgraph-check-github/omm-v1/owned-comment*.test.mjs + - name: Re-read actual first-party comment 6645637811 and frozen PR239 before any Lean + env: + GITHUB_TOKEN: ${{ github.token }} + GITHUB_ACTOR_ID: ${{ github.actor_id }} + run: node integrations/mathgraph-check-github/omm-v1/owned-comment-cli.mjs preflight "$RUNNER_TEMP/omm-owned-artin-239" + - name: Preserve independently fetched owned request and exact frozen source + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 + with: + name: omm-owned-comment-source-239 + path: ${{ runner.temp }}/omm-owned-artin-239/ + if-no-files-found: error + retention-days: 14 + + credentialless-fixed-artin-replay: + needs: authenticate-owned-comment-and-frozen-source + runs-on: ubuntu-24.04 + timeout-minutes: 45 + permissions: + contents: read + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 + with: + node-version: '24' + - name: Obtain same-run admission and source (no App or model credentials) + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 + with: + name: omm-owned-comment-source-239 + path: ${{ runner.temp }}/omm-owned-artin-239 + - name: Acquire byte-pinned Lean 4.28.0 runtime + run: | + set -euo pipefail + mkdir -p "$RUNNER_TEMP/lean-428" + curl --proto '=https' --tlsv1.2 -fL --retry 2 https://github.com/leanprover/lean4/releases/download/v4.28.0/lean-4.28.0-linux.tar.zst -o "$RUNNER_TEMP/lean-428.tar.zst" + echo "ceb3a3f844f7aebf63245e2b51c28d5b0ed38942c19f93cf3febd520302160bd $RUNNER_TEMP/lean-428.tar.zst" | sha256sum -c - + tar --zstd -xf "$RUNNER_TEMP/lean-428.tar.zst" --strip-components=1 -C "$RUNNER_TEMP/lean-428" + echo "$RUNNER_TEMP/lean-428/bin" >> "$GITHUB_PATH" + - name: Materialize exactly pinned Mathlib environment without private credentials + working-directory: ${{ runner.temp }}/omm-owned-artin-239 + run: | + set -euo pipefail + printf '%s\n' 'leanprover/lean4:v4.28.0' > lean-toolchain + cat > lakefile.lean <<'LEAN' + import Lake + open Lake DSL + package MathGraphArtinControls where + require mathlib from git "https://github.com/leanprover-community/mathlib4.git" @ "8f9d9cff6bd728b17a24e163c9402775d9e6a365" + lean_lib Candidate where + LEAN + lake update + node --input-type=module -e "import fs from 'node:fs';const d=JSON.parse(fs.readFileSync('lake-manifest.json'));if(d.packages.find(p=>p.name==='mathlib')?.rev!=='8f9d9cff6bd728b17a24e163c9402775d9e6a365')throw Error('MATHLIB_REVISION_MISMATCH')" + lake exe cache get + - name: Execute registered Artin candidate, original-target consumer and axiom audit + env: + LEAN_NUM_THREADS: '1' + run: node integrations/mathgraph-check-github/omm-v1/artin-native-cli.mjs "$RUNNER_TEMP/omm-owned-artin-239" + - name: Preserve source admission, native trace, receipts and bounded unknowns + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 + with: + name: omm-owned-comment-native-239 + path: | + ${{ runner.temp }}/omm-owned-artin-239/admission.json + ${{ runner.temp }}/omm-owned-artin-239/request.json + ${{ runner.temp }}/omm-owned-artin-239/published-exercise.lean.txt + ${{ runner.temp }}/omm-owned-artin-239/candidate.lean.txt + ${{ runner.temp }}/omm-owned-artin-239/lake-manifest.json + ${{ runner.temp }}/omm-owned-artin-239/output/* + if-no-files-found: warn + retention-days: 30 + + publish-one-neutral-owned-optin-check: + needs: credentialless-fixed-artin-replay + runs-on: ubuntu-24.04 + timeout-minutes: 12 + permissions: + contents: read + concurrency: + group: mathgraph-omm-owned-optin-pr239 + cancel-in-progress: false + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 + with: + node-version: '24' + - name: Download exact same-run native evidence into the credentialed verifier + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 + with: + name: omm-owned-comment-native-239 + path: ${{ runner.temp }}/omm-owned-publish-239 + - name: Mint restricted registered MathGraph App token (not exposed to native job) + id: app-token + uses: actions/create-github-app-token@fee1f7d63c2ff003460e3d139729b119787bc349 + with: + app-id: 5308912 + private-key: ${{ secrets.MATHGRAPH_CHECK_APP_PRIVATE_KEY }} + owner: metalogiclabs + repositories: mathgraph + permission-checks: write + permission-pull-requests: read + - name: Re-authenticate owner comment and independently requalify native bytes before neutral App write + env: + MATHGRAPH_APP_INSTALLATION_TOKEN: ${{ steps.app-token.outputs.token }} + GITHUB_ACTOR_ID: ${{ github.actor_id }} + run: node integrations/mathgraph-check-github/omm-v1/owned-comment-cli.mjs publish "$RUNNER_TEMP/omm-owned-publish-239" + - name: Preserve genuine registered-App readback + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 + with: + name: omm-owned-comment-app-readback-239 + path: ${{ runner.temp }}/omm-owned-publish-239/output/owned-optin-app-receipt.json + if-no-files-found: warn + retention-days: 30 diff --git a/integrations/mathgraph-check-github/omm-v1/README.md b/integrations/mathgraph-check-github/omm-v1/README.md new file mode 100644 index 000000000..c031b94f5 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/README.md @@ -0,0 +1,56 @@ +# MathGraph Check for Open Math Model — claim bridge V1 + +**Status:** the frozen first-party Artin native replay and registered-App transport are **WARRANTED_BOUNDED** within their declared source and environment; general OMM community intake is **UNKNOWN**. There is no enabled community OMM `/check` or `/fix` command, arbitrary Lean evaluation, or research-intent certificate. + +This additive adapter can turn a trusted, exact-source native Lean replay into the shared MathGraph Check/Passport-style neutral report. It distinguishes: + +1. **Lean candidate compilation:** the exact candidate was compiled in the pinned environment; compilation alone is not an axiom audit. +2. **Original-target consumer:** an operator-authored, hash-pinned consumer compiled with standard axioms, was rejected by Lean in this replay, or is UNKNOWN. Rejection does not prove semantic inequivalence. +3. **Formal-statement equivalence:** UNKNOWN in this adapter until a separately authenticated, same-context SemantEq result is integrated. +4. **Candidate proof necessity and paper-to-Lean intended meaning:** both UNKNOWN, including on consumer acceptance. +5. **Independent evidence warrant:** UNKNOWN until a trusted GitHub/Actions artifact chain is verified externally. + +The bridge expects an `omm.request.v1` formed from authenticated GitHub PR metadata and **actual fetched candidate source bytes**; a separately admitted `omm.claim-contract.v1`; and an `omm.native-receipt.v1`. Contract and receipt exact-byte SHA-256 pins must be admitted by the trusted Check controller, not the PR, an AI answer, or an arbitrary JSON field. The native output includes both pinned source hashes, the consumer and candidate declaration names, the Mathlib Git revision, tracked toolchain hash, native exit codes and separate candidate/consumer axiom audit. The controlled release stage independently re-derives the qualified source identities and artifacts before App publication; this does not confer authority over a paper's intended meaning. + +The optional workflow `check-omm-artin-native.yml` performs a fixed, first-party native Lean cohort only: + +- PR #239: an Artin proof with an added `CommGroup` hypothesis. Candidate Lean compilation can succeed, but an original `Group` target consumer should reject. The result is **NOT_ESTABLISHED_IN_THIS_REPLAY**, not a proof of mathematical inequivalence. +- PR #240: the repaired candidate under the original `Group` assumption. The exact original-target consumer and separate `#print axioms` checks should accept. The result is **CONSUMER_ACCEPTED**, never an intended-claim warrant. + +GitHub preflight pins both public PR head/base identities, the exact `ArtinGenerators.lean` bytes and the externally published exercise. A separate job executes only those pre-registered sources with Lean 4.28.0 and a pinned Mathlib revision, without GitHub App or LLM credentials. CI stores source, manifest, trace, assessment, neutral report **and a deterministic GitHub App Check-run preview** for each fixed case. The preview reuses the existing `advisory-publisher-v1` schema, remains `conclusion: neutral`, and never transmits credentials or publishes a Check by itself. Workflow outcome and artifacts, not this README, are the authority for whether the native cohort actually passed. + +An additional, strictly first-party publisher stage runs **only after both native jobs pass**. It downloads artifacts from that same workflow run, independently recomputes all manifest hashes, exact Artin source identities and OMM assessment, verifies the GitHub App Check payload against the native receipt, and rechecks each exact owned PR head immediately before and after Check publication. It uses the existing registered MathGraph App's private key in a **separate job**, mints a narrowly scoped token with Checks write/Pull Requests read, and independently reads the posted neutral Check back from GitHub. Duplicate same-App checks are reconciled instead of repeatedly created. This does not enable community webhook intake. A missing installation, permission or secret must leave App publication **UNKNOWN**, even while the native research controls remain qualified. + +The [registered-App qualification run](https://github.com/metalogiclabs/mathgraph/actions/runs/41916425178) passed and independently updated the original neutral Check IDs on [PR239](https://github.com/metalogiclabs/mathgraph/runs/126876654761) and [PR240](https://github.com/metalogiclabs/mathgraph/runs/126876680794), rather than creating duplicates. This proves bounded transport for the frozen source cohort, not universal proof review. + +For an *independent* stale-source control, `check-omm-intake-lifecycle.yml` operates only on the owned draft PR270 and publishes a distinct **OMM intake (advisory)** App Check. It reports UNKNOWN for every proof/intent axis; no theorem is examined. On successive owned pushes, it checks exact live head and App identity and explicitly supersedes the immediately preceding Check, with independent readback. The workflow's actual runs and Check bodies, not its existence, determine whether revocation has passed in production. + +`optin-admission.mjs` is a **non-publishing, operator-only source gate** for exact OMM requests. The intended public OMM syntax is `@MathGraph /check`; previously qualified `@MathGraph -claim` is a retained legacy alias. Both pass the same owner, PR and source-identity checks, with a maximum result of `SOURCE_ADMITTED_ONLY`; native receipt, original target, mathematical intent and independent warrant remain UNKNOWN, and repairs/publication are denied. There is **no live issue-comment route for `/check`** yet. It deliberately does not compete with the separate default-branch demo already handling `@MathGraph -check` and `@MathGraph -fix`. + +The OMM command boundary is deliberately narrow: + +| Exact comment | OMM claim-audit admission | +|---|---| +| `@MathGraph /check` | Canonical request; source admission only | +| `@MathGraph -claim` | Legacy request; source admission only | +| `@MathGraph -check`, `@MathGraph -fix` | Remain with the existing generic demo, **not** OMM | +| `@MathGraph /explain`, `@MathGraph /fix` | Reserved for future capabilities; **not enabled** | + +Trailing instructions, multiline comments, altered spellings and whitespace are not commands. This parser only recognizes intent; it does not itself activate a listener, authorize external Lean, or establish a mathematical claim. + +The separate `check-omm-owned-optin.yml` is a **push-triggered first-party qualification only**, not a GitHub `issue_comment` listener. It authenticates the unchanged historical `-claim` owner comment `6645637811` on frozen PR239 directly from GitHub, passes only that fixed source into a credentialless Lean 4.28.0/Mathlib native replay, then has a separate App-token job independently re-fetch the comment/source and requalify the full native receipt before creating a distinct neutral `MathGraph Check / OMM owner opt-in (advisory)` Check. The same admission code now also recognizes canonical `/check`, though **no live `/check` comment has been routed through this three-job workflow**. Edits or stale source fail closed, and its qualification scope explicitly leaves formal equivalence, paper intent, warrant and repairs UNKNOWN/DENIED. A successful run does not authorize general intake; a production comment listener would have to reside on the default branch after a separate security and release decision. + +The first real owner [comment](https://github.com/metalogiclabs/mathgraph/pull/239#issuecomment-6645637811) was replayed through all three jobs in [run 41939111450](https://github.com/metalogiclabs/mathgraph/actions/runs/41939111450); independent GitHub readback confirmed [neutral App Check 126956089184](https://github.com/metalogiclabs/mathgraph/runs/126956089184), the exact frozen PR239 head, original-target consumer `NOT_ESTABLISHED_IN_THIS_REPLAY`, and no mathematical-intent warrant. The prior [run 41938543754](https://github.com/metalogiclabs/mathgraph/actions/runs/41938543754) is retained as a failed publisher attempt: an API wrapper discarded the POST method/body. Its corrected transport has regression tests. An actual retry of the successful publisher job ([run 41939111450, attempt 2](https://github.com/metalogiclabs/mathgraph/actions/runs/41939111450/attempts/2)) returned `UNCHANGED` for **the same Check ID**, and independent GitHub GET still returned exactly one matching App Check. This qualifies idempotence for this one first-party source/request, not all future comment events or cross-commit revocation. + +Untrusted Lean source must not be invoked by the production Worker or a credentialed job: Lean elaboration executes macros. Before introducing community intake, qualify a separate offline, unprivileged native sandbox and an authenticated native receipt-publisher boundary, with no GitHub App/model credentials in the sandbox. + +The separate experimental `check-omm-native-sandbox-smoke.yml` exercises **two fixed, first-party Lean fixtures only** in a distinct Docker image built from the existing Lean 4.28.0 Dockerfile with verified Lean distribution bytes. The native process receives a single read-only source file, `--network none`, read-only root filesystem, a small no-execute tmpfs, non-root UID 65534, no Linux capabilities, no-new-privileges, and CPU/memory/PID/time/output limits. Each invocation has a unique generated container name and independently forces removal of that exact container even after a client timeout; failed cleanup returns UNKNOWN. A separate container probe tests read-only mount/root, non-root UID and absent default network route. Positive compilation and negative rejection are kept distinct; failure or resource exhaustion remains UNKNOWN. The smoke receipt carries first-party source digests, runtime image identity in the Action artifact, observed constraints and explicit denial of community execution. Passing this smoke **does not** qualify arbitrary adversarial Lean source or a secure multi-tenant execution service; the base container image is not yet digest-pinned, and the trust boundary needs independent adversarial and deployment review. + +No existing L4YAML checking method, live runtime, `/pilot` page, examples, installation policy, price cap or model route is modified by this change. No automatic OMM repair is enabled. To test locally: + +```sh +node --test integrations/mathgraph-check-github/omm-v1/*.test.mjs +``` + +The owned PR270's immediately preceding stale-head withdrawal was already requalified in [run 41922969172](https://github.com/metalogiclabs/mathgraph/actions/runs/41922969172); the real first-party request and same-Check idempotence above are also complete. The remaining decisive gates are a **trusted default-branch comment trigger**, separately authenticated community claim-owner/intent authority, digest-pinned adversarially qualified sandbox, bounded costs and permissions, broader historical stale-Check/comment-deletion withdrawal, authenticated community artifact admission and precise intent/claim-source mapping. Existing generic Lean work in [MathGraph PR #241](https://github.com/metalogiclabs/mathgraph/pull/241) is operator-only and not already a safe webhook executor; it remains independent until qualified transport is implemented. + diff --git a/integrations/mathgraph-check-github/omm-v1/app-check.mjs b/integrations/mathgraph-check-github/omm-v1/app-check.mjs new file mode 100644 index 000000000..876ccbe45 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/app-check.mjs @@ -0,0 +1,31 @@ +// OMM projection to the *existing* MathGraph Check GitHub App Check-run schema. +// This is a pure payload preview. Only the authenticated App publisher may +// create the Check run after independently verifying immutable evidence pins, +// exact live head and installation permissions. No PR code executes here. +import {assessOmmClaim} from './claim-admission.mjs'; +import {formatOmmClaimReport,ommClaimView} from './report.mjs'; +import {buildAdvisoryCheck} from '../advisory-publisher-v1.mjs'; + +export function buildOmmAppCheckPreview({request,expectedHeadSha,runUrl,...nativeInputs}={}){ + if(request?.command!=='check')throw Error('OMM_CHECK_ONLY'); + const assessment=assessOmmClaim({request,...nativeInputs}); + const view=ommClaimView(assessment,request); + const qualified=assessment.status==='NATIVE_RECEIPT_BOUND'; + const results=[ + {id:'candidate-Lean-build',status:qualified&&view.candidate_proof==='LEAN_COMPILED'?'OBSERVED':'UNKNOWN'}, + {id:'original-claim-consumer',status:qualified&&view.original_target==='CONSUMER_ACCEPTED'?'OBSERVED': + qualified&&view.original_target==='NOT_ESTABLISHED_IN_THIS_REPLAY'?'BLOCKED':'UNKNOWN'}, + {id:'paper-to-Lean-intent',status:'UNKNOWN'}, + {id:'independent-evidence-attestation',status:'UNKNOWN'} + ]; + const generic={source:request,status:'UNKNOWN',permission:'ADVISORY_ONLY', + scope:'GITHUB_CHECK_RUN_OBSERVATION_ONLY',results, + evidence:results.filter(r=>r.status==='OBSERVED')}; + const base=buildAdvisoryCheck(generic,{repository:request.repository,sha:expectedHeadSha}); + const detail=formatOmmClaimReport(assessment,request,runUrl); + return {assessment,payload:{...base,name:'MathGraph Check / OMM claim (advisory)', + external_id:`mathgraph-omm-claim-v1:${request.repository}:${request.number}:${expectedHeadSha}`, + output:{title:'OMM: '+view.title+' (advisory)', + summary:[base.output.summary,detail, + 'Publication and exact-head App identity require separate GitHub readback. This Check conveys bounded native observations, not an independent mathematical-intent warrant.'].join('\n\n').slice(0,65000)}}}; +} diff --git a/integrations/mathgraph-check-github/omm-v1/app-check.test.mjs b/integrations/mathgraph-check-github/omm-v1/app-check.test.mjs new file mode 100644 index 000000000..d71b4d46e --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/app-check.test.mjs @@ -0,0 +1,56 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {createHash} from 'node:crypto'; +import {buildOmmAppCheckPreview} from './app-check.mjs'; + +const hash=s=>createHash('sha256').update(s).digest('hex'); +const request={schema:'mathgraph.omm.request.v1',repository:'metalogiclabs/mathgraph', + number:239,head_sha:'a'.repeat(40),base_sha:'b'.repeat(40),candidate_source_sha256:'c'.repeat(64),command:'check'}; +const contract={schema:'mathgraph.omm.claim-contract.v1',repository:request.repository,number:request.number, + head_sha:request.head_sha,base_sha:request.base_sha,reference_sha256:'d'.repeat(64), + candidate_sha256:request.candidate_source_sha256,consumer_sha256:'e'.repeat(64), + consumer_name:'Original.target',candidate_name:'Candidate.theorem',mathlib_git:'f'.repeat(40),lean_toolchain_sha256:'1'.repeat(64)}; +const receipt={schema:'mathgraph.omm.native-receipt.v1',repository:request.repository,number:request.number, + head_sha:request.head_sha,base_sha:request.base_sha,native:{execution_status:'EXECUTED', + source_reference_sha256:contract.reference_sha256,candidate_sha256:contract.candidate_sha256, + consumer_sha256:contract.consumer_sha256,consumer_name:contract.consumer_name, + candidate_declaration_name:contract.candidate_name,mathlib_git:contract.mathlib_git, + lean_toolchain_sha256:contract.lean_toolchain_sha256,candidate_source_build:'LEAN_ACCEPTED', + original_target_status:'NATIVE_CONSUMER_ACCEPTED',candidate_axioms:['propext'],axioms:['propext'], + trace:[{unit:'Candidate',exit_code:0},{unit:'Consumer',exit_code:0},{unit:'Audit',exit_code:0}]}}; +function input(r=receipt){const contractBytes=JSON.stringify(contract),receiptBytes=JSON.stringify(r);return { + request,contractBytes,receiptBytes,expectedContractSha256:hash(contractBytes),expectedReceiptSha256:hash(receiptBytes), + expectedHeadSha:request.head_sha,runUrl:'https://github.com/metalogiclabs/mathgraph/actions/runs/41914046962'};} + +test('OMM Check shares neutral GitHub App formatting and never means a mathematical warrant',()=>{ + const {payload,assessment}=buildOmmAppCheckPreview(input()); + assert.equal(assessment.original_target,'CONSUMER_ACCEPTED'); + assert.equal(payload.head_sha,request.head_sha); + assert.equal(payload.conclusion,'neutral'); + assert.equal(payload.status,'completed'); + assert.match(payload.name,/MathGraph Check.*OMM/); + assert.match(payload.output.summary,/Original.target consumer.*Accepted by native Lean replay/i); + assert.match(payload.output.summary,/Human mathematical intent.*UNKNOWN/i); + assert.match(payload.output.summary,/Independent evidence warrant.*UNKNOWN/i); + assert.equal(payload.output.title.includes('success'),false); + assert.doesNotMatch(payload.output.summary,/PREVIEW ONLY|No live GitHub Check/i); +}); + +test('strengthened premise reports bounded failure without converting it to unsound proof rejection',()=>{ + const r=structuredClone(receipt);r.native.trace=r.native.trace.slice(0,2); + r.native.trace[1].exit_code=1;r.native.axioms=null;r.native.original_target_status='UNKNOWN'; + const {payload,assessment}=buildOmmAppCheckPreview(input(r)); + assert.equal(assessment.candidate_proof,'LEAN_COMPILED'); + assert.equal(assessment.original_target,'NOT_ESTABLISHED_IN_THIS_REPLAY'); + assert.equal(payload.conclusion,'neutral'); + assert.match(payload.output.summary,/NOT mathematical inequivalence/); +}); + +test('source staleness and falsified receipt keep an informational UNKNOWN, not a green Check',()=>{ + const unknown=buildOmmAppCheckPreview({...input(),expectedReceiptSha256:'0'.repeat(64)}); + assert.equal(unknown.assessment.status,'UNKNOWN'); + assert.equal(unknown.payload.conclusion,'neutral'); + assert.match(unknown.payload.output.summary,/Check incomplete/); + assert.throws(()=>buildOmmAppCheckPreview({...input(),expectedHeadSha:'2'.repeat(40)}),/STALE_SOURCE_REFUSED/); + assert.throws(()=>buildOmmAppCheckPreview({...input(),request:{...request,command:'fix'}}),/OMM_CHECK_ONLY/); +}); diff --git a/integrations/mathgraph-check-github/omm-v1/app-owned-publish.mjs b/integrations/mathgraph-check-github/omm-v1/app-owned-publish.mjs new file mode 100644 index 000000000..ba6cbb643 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/app-owned-publish.mjs @@ -0,0 +1,158 @@ +// One explicitly owned Artin cohort only. The publish job receives an App +// installation token, never a Lean executable or source from an arbitrary PR. +import {createHash} from 'node:crypto'; +import {readFile,writeFile} from 'node:fs/promises'; +import {join,resolve} from 'node:path'; +import {pathToFileURL} from 'node:url'; +import {artinStrongSource,artinRepairSource,bindPublishedArtin} from '../mention-artin-control-v1.mjs'; +import {assessOmmClaim} from './claim-admission.mjs'; +import {buildOmmAppCheckPreview} from './app-check.mjs'; + +const SHA40=/^[a-f0-9]{40}$/; +const SHA256=/^[a-f0-9]{64}$/; +const digest=s=>createHash('sha256').update(s).digest('hex'); +const REPO='metalogiclabs/mathgraph',APP_ID=5308912; +const NAME='MathGraph Check / OMM claim (advisory)'; +const PROFILES={ + 239:{head:'2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17',base:'dfd87d1e921953badedcfae21ade76bb38f5c04a',source:artinStrongSource,expected:'NOT_ESTABLISHED_IN_THIS_REPLAY'}, + 240:{head:'06f09cd5aaa866365ceb6540cd06389bd37a727c',base:'2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17',source:artinRepairSource,expected:'CONSUMER_ACCEPTED'} +}; +const artifactNames=['claim-contract.json','native-receipt.json','assessment.json','native-trace.json','report.md','app-check-preview.json']; +const requirePin=(condition,why)=>{if(!condition)throw Error(why);}; + +/** Reconstruct the Check payload independently of PR-authored claims and the saved preview. */ +export async function loadFrozenArtinPreview({directory,number,runId}={}){ + const p=PROFILES[number]; + if(!p||typeof directory!=='string'||!directory.startsWith('/')|| + !/^[1-9][0-9]*$/.test(String(runId??'')))throw Error('UNREGISTERED_RELEASE_INPUT'); + const dir=resolve(directory); + const read=async name=>{const bytes=await readFile(join(dir,name),'utf8'); + if(bytes.length>2_000_000)throw Error('ARTIFACT_TOO_LARGE');return bytes;}; + const [requestRaw,source,published,manifestRaw]=await Promise.all([ + read('request.json'),read('candidate.lean.txt'),read('published-exercise.lean.txt'),read('output/manifest.json')]); + const request=JSON.parse(requestRaw),manifest=JSON.parse(manifestRaw); + const original=bindPublishedArtin(published); + requirePin(request.schema==='mathgraph.omm.request.v1'&&request.repository===REPO&& + request.number===number&&request.command==='check'&&request.head_sha===p.head&& + request.base_sha===p.base&&request.candidate_source_sha256===digest(source)&& + source===p.source,'FROZEN_SOURCE_IDENTITY_MISMATCH'); + const runUrl=`https://github.com/${REPO}/actions/runs/${runId}`; + requirePin(manifest.schema==='mathgraph.omm.artin-qualification-manifest.v1'&& + manifest.repository===REPO&&manifest.number===number&&manifest.source_head===p.head&& + manifest.base_sha===p.base&&manifest.candidate_source_sha256===digest(source)&& + manifest.published_source_sha256===original.source_sha256&&manifest.run_url===runUrl&& + manifest.release==='QUALIFICATION_ONLY'&&manifest.evidence_warrant==='UNKNOWN', + 'ARTIFACT_PIN_MISMATCH'); + const actualNames=Object.keys(manifest.artifact_sha256??{}); + requirePin(actualNames.length===artifactNames.length&& + actualNames.slice().sort().join('|')===artifactNames.slice().sort().join('|'), + 'ARTIFACT_PIN_MISMATCH'); + const artifactPairs=await Promise.all(artifactNames.map(async name=>[name,await read('output/'+name)])); + for(const [name,bytes] of artifactPairs)requirePin(manifest.artifact_sha256[name]===digest(bytes),'ARTIFACT_PIN_MISMATCH'); + const files=Object.fromEntries(artifactPairs); + requirePin(manifest.claim_contract_sha256===digest(files['claim-contract.json'])&& + manifest.native_receipt_sha256===digest(files['native-receipt.json']), + 'ARTIFACT_PIN_MISMATCH'); + const inputs={request,contractBytes:files['claim-contract.json'], + expectedContractSha256:manifest.claim_contract_sha256,receiptBytes:files['native-receipt.json'], + expectedReceiptSha256:manifest.native_receipt_sha256}; + const assessment=assessOmmClaim(inputs); + requirePin(assessment.status==='NATIVE_RECEIPT_BOUND'&& + assessment.original_target===p.expected&&assessment.intent_fidelity==='UNKNOWN'&& + assessment.evidence_warrant==='UNKNOWN'&& + JSON.stringify(assessment)===files['assessment.json'],'NATIVE_RECEIPT_REQUALIFICATION_FAILED'); + const trace=JSON.parse(files['native-trace.json']); + const receipt=JSON.parse(files['native-receipt.json']); + requirePin(Array.isArray(trace)&&trace.length===receipt.native.trace.length&& + trace.every((e,i)=>e.unit===receipt.native.trace[i].unit&& + e.exit_code===receipt.native.trace[i].exit_code)&& + trace[0].source_sha256===digest(source)&& + trace[1].source_sha256===assessment.consumer_sha256,'NATIVE_TRACE_DIVERGED'); + // An intact trace file can still describe a different object handoff. + const [candidate,consumer,audit]=trace; + requirePin(SHA256.test(candidate.produced_olean_sha256??'')&& + consumer.imported_olean_sha256===candidate.produced_olean_sha256&& + (!audit||(SHA256.test(consumer.produced_olean_sha256??'')&& + audit.imported_olean_sha256===consumer.produced_olean_sha256)), + 'NATIVE_OLEAN_HANDOFF_UNBOUND'); + const preview=buildOmmAppCheckPreview({...inputs,expectedHeadSha:p.head,runUrl}); + requirePin(JSON.stringify(preview.payload)===files['app-check-preview.json']&& + preview.payload.conclusion==='neutral','APP_PREVIEW_REQUALIFICATION_FAILED'); + return {request,assessment,payload:preview.payload}; +} + +/** Explicit first-party exact-head transport; `write` defaults to false. */ +export async function reconcileOwnedOmmCheck({number,baseSha,payload,api,write=false}={}){ + const p=PROFILES[number]; + const external=`mathgraph-omm-claim-v1:${REPO}:${number}:${p?.head??''}`; + if(!p||baseSha!==p.base||typeof api!=='function'|| + payload?.name!==NAME||payload.head_sha!==p.head||!SHA40.test(payload.head_sha)|| + payload.external_id!==external||payload.status!=='completed'|| + payload.conclusion!=='neutral'|| + typeof payload.output?.summary!=='string'|| + !payload.output.summary.includes('Human mathematical intent | UNKNOWN')|| + !payload.output.summary.includes('Independent evidence warrant | UNKNOWN')) + throw Error('UNQUALIFIED_OMM_APP_PAYLOAD'); + const root='/repos/'+REPO; + const exactHead=async()=>{ + const observed=await api(root+'/pulls/'+number); + if(observed.number!==number||observed.state!=='open'|| + observed.head?.sha!==p.head||observed.base?.sha!==p.base|| + observed.head?.repo?.full_name!==REPO||observed.base?.repo?.full_name!==REPO) + throw Error('STALE_PR_HEAD'); + }; + await exactHead(); + const list=await api(root+'/commits/'+p.head+'/check-runs?check_name='+ + encodeURIComponent(NAME)+'&filter=all&per_page=100'); + if(!Array.isArray(list?.check_runs)||!Number.isSafeInteger(list.total_count)|| + list.total_count>100||list.total_count>list.check_runs.length) + throw Error('APP_CHECK_RECONCILIATION_INCOMPLETE'); + const own=list.check_runs.filter(x=>x.app?.id===APP_ID&&x.name===NAME&&x.external_id===external); + if(own.length>1)throw Error('DUPLICATE_OWNED_OMM_CHECKS'); + if(own.length===1&&(!Number.isSafeInteger(own[0].id)||own[0].head_sha!==p.head)) + throw Error('OWNED_OMM_CHECK_IDENTITY_MISMATCH'); + await exactHead(); + if(!write)return {mode:'PREVIEW',existing:own.length}; + let result,mode; + if(own.length===1){ + const old=own[0]; + if(old.status==='completed'&&old.conclusion==='neutral'&&old.output?.summary===payload.output.summary){ + result=old;mode='UNCHANGED'; + }else{ + result=await api(root+'/check-runs/'+old.id,{method:'PATCH',body:{status:'completed', + conclusion:'neutral',external_id:external,output:payload.output}});mode='UPDATED'; + } + }else{ + result=await api(root+'/check-runs',{method:'POST',body:payload});mode='CREATED'; + } + if(!Number.isSafeInteger(result?.id))throw Error('APP_WRITE_IDENTITY_MISMATCH'); + const verified=await api(root+'/check-runs/'+result.id); + if(verified?.app?.id!==APP_ID||verified.head_sha!==p.head|| + verified.name!==NAME||verified.external_id!==external||verified.status!=='completed'|| + verified.conclusion!=='neutral'||verified.output?.summary!==payload.output.summary) + throw Error('APP_INDEPENDENT_READBACK_MISMATCH'); + await exactHead(); + return {mode,check_run_id:result.id,app_id:APP_ID,repository:REPO,number,head_sha:p.head}; +} + +if(process.argv[1]&&import.meta.url===pathToFileURL(resolve(process.argv[1])).href){ + try{ + const number=Number(process.argv[2]),directory=process.argv[3],runId=process.env.GITHUB_RUN_ID; + const token=process.env.MATHGRAPH_APP_INSTALLATION_TOKEN; + if(process.argv.length!==4||process.env.GITHUB_REPOSITORY!==REPO|| + !runId||!token||token.length<10)throw Error('QUALIFICATION_RELEASE_CONTEXT_MISSING'); + const {request,payload}=await loadFrozenArtinPreview({number,directory,runId}); + const api=async(path,{method='GET',body}={})=>{ + if(!path.startsWith('/repos/'+REPO+'/'))throw Error('APP_API_SURFACE_REFUSED'); + const response=await fetch('https://api.github.com'+path,{method, + headers:{Accept:'application/vnd.github+json','X-GitHub-Api-Version':'2022-11-28', + Authorization:'Bearer '+token,'Content-Type':'application/json'}, + body:body===undefined?undefined:JSON.stringify(body),signal:AbortSignal.timeout(20000)}); + if(!response.ok)throw Error('GITHUB_APP_HTTP_'+response.status); + return response.json(); + }; + const result=await reconcileOwnedOmmCheck({number,baseSha:request.base_sha,payload,api,write:true}); + await writeFile(join(resolve(directory),'output','app-publish-receipt.json'),JSON.stringify(result,null,2)+'\n',{flag:'wx'}); + console.log('OMM_OWNED_APP_CHECK_READBACK '+JSON.stringify(result)); + }catch(e){console.error('OMM_OWNED_APP_CHECK_UNKNOWN:'+String(e.message));process.exitCode=1;} +} diff --git a/integrations/mathgraph-check-github/omm-v1/app-owned-publish.test.mjs b/integrations/mathgraph-check-github/omm-v1/app-owned-publish.test.mjs new file mode 100644 index 000000000..19c56f64b --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/app-owned-publish.test.mjs @@ -0,0 +1,134 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {mkdtemp,writeFile,readFile,rm} from 'node:fs/promises'; +import {tmpdir} from 'node:os'; +import {join} from 'node:path'; +import {createHash} from 'node:crypto'; +import {artinStrongSource,artinRepairSource} from '../mention-artin-control-v1.mjs'; +import {qualifyArtinDirectory} from './artin-native-cli.mjs'; +import {loadFrozenArtinPreview,reconcileOwnedOmmCheck} from './app-owned-publish.mjs'; + +const sha=s=>createHash('sha256').update(s).digest('hex'); +const runId='41914895950'; +const base='dfd87d1e921953badedcfae21ade76bb38f5c04a'; +const head='2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17'; +const published=`import Mathlib + +open Function Fintype Subgroup Ideal Polynomial Submodule Zsqrtd +open scoped BigOperators + + + +/-Informal Statement + +Let $H$ be the subgroup generated by two elements $a, b$ of a group $G$. Prove that if $a b=b a$, then $H$ is an abelian group. +-/ + +theorem Artin_exercise_2_2_9 {G : Type*} [Group G] {a b : G} + (h : a * b = b * a) : + ∀ x y : closure {x | x = a ∨ x = b}, x * y = y * x := by + sorry +`; +const candidateOlean=sha('candidate object'); +const consumerOlean=sha('consumer object'); +async function setup(number=239){ + const source=number===239?artinStrongSource:artinRepairSource; + const directory=await mkdtemp(join(tmpdir(),'omm-app-publish-')); + const request={schema:'mathgraph.omm.request.v1',repository:'metalogiclabs/mathgraph',number, + command:'check',head_sha:number===239?head:'06f09cd5aaa866365ceb6540cd06389bd37a727c', + base_sha:number===239?base:head,candidate_source_sha256:sha(source)}; + await writeFile(join(directory,'request.json'),JSON.stringify(request)); + await writeFile(join(directory,'candidate.lean.txt'),source); + await writeFile(join(directory,'published-exercise.lean.txt'),published); + await writeFile(join(directory,'lean-toolchain'),'leanprover/lean4:v4.28.0\n'); + await qualifyArtinDirectory({directory,mathlibGit:'8f9d9cff6bd728b17a24e163c9402775d9e6a365', + runUrl:`https://github.com/metalogiclabs/mathgraph/actions/runs/${runId}`, + run:async unit=>{ + if(unit==='Candidate')return {code:0,output:'',produced_olean_sha256:candidateOlean}; + if(unit==='Consumer')return number===239 + ?{code:1,output:'type mismatch',imported_olean_sha256:candidateOlean} + :{code:0,output:'',imported_olean_sha256:candidateOlean,produced_olean_sha256:consumerOlean}; + return {code:0,output:"'Artin_exercise_2_2_9' depends on axioms: [propext]\n'MathGraphConsumer.original_target' depends on axioms: [propext]", + imported_olean_sha256:consumerOlean}; + }}); + return directory; +} +test('release gate re-derives App payload and all hashes from frozen native artifacts',async()=>{ + const directory=await setup(); + try{ + const loaded=await loadFrozenArtinPreview({directory,number:239,runId}); + assert.equal(loaded.payload.conclusion,'neutral'); + assert.match(loaded.payload.output.summary,/Human mathematical intent \| UNKNOWN/); + assert.equal(loaded.assessment.original_target,'NOT_ESTABLISHED_IN_THIS_REPLAY'); + const file=join(directory,'output','app-check-preview.json'); + await writeFile(file,(await readFile(file,'utf8')).replace('neutral','success')); + await assert.rejects(loadFrozenArtinPreview({directory,number:239,runId}),/ARTIFACT_PIN_MISMATCH/); + }finally{await rm(directory,{recursive:true,force:true});} +}); +test('release gate refuses broken object handoffs even with matching manifest hashes',async()=>{ + for(const fault of ['missing','candidate mismatch','audit mismatch','consumer source mismatch']){ + const number=fault==='audit mismatch'?240:239; + const directory=await setup(number); + try{ + const artifact=join(directory,'output','native-trace.json'); + const manifestPath=join(directory,'output','manifest.json'); + const input={directory,number,runId}; + await loadFrozenArtinPreview(input); + const trace=JSON.parse(await readFile(artifact,'utf8')); + if(fault==='missing')delete trace[0].produced_olean_sha256; + if(fault==='candidate mismatch')trace[1].imported_olean_sha256=consumerOlean; + if(fault==='audit mismatch')trace[2].imported_olean_sha256=candidateOlean; + if(fault==='consumer source mismatch')trace[1].source_sha256=sha('other consumer source'); + const traceBytes=JSON.stringify(trace); + const manifest=JSON.parse(await readFile(manifestPath,'utf8')); + manifest.artifact_sha256['native-trace.json']=sha(traceBytes); + await writeFile(artifact,traceBytes); + await writeFile(manifestPath,JSON.stringify(manifest)); + const error=fault==='consumer source mismatch'?/NATIVE_TRACE_DIVERGED/:/NATIVE_OLEAN_HANDOFF_UNBOUND/; + await assert.rejects(loadFrozenArtinPreview(input),error,fault); + }finally{await rm(directory,{recursive:true,force:true});} + } +}); +function client({stale=false,existing=null,changeAfter=false}={}){ + const calls=[]; + const record={id:777,head_sha:head,name:'MathGraph Check / OMM claim (advisory)', + conclusion:'neutral',status:'completed',external_id:`mathgraph-omm-claim-v1:metalogiclabs/mathgraph:239:${head}`, + app:{id:5308912}}; + const state={count:0,check:existing}; + return {calls,api:async(path,{method='GET',body}={})=>{ + calls.push({path,method,body}); + if(path.endsWith('/pulls/239')){ + state.count++; + return {number:239,state:'open',head:{sha:stale||changeAfter&&state.count>2?'0'.repeat(40):head,repo:{full_name:'metalogiclabs/mathgraph'}}, + base:{sha:base,repo:{full_name:'metalogiclabs/mathgraph'}}}; + } + if(path.includes('/commits/')&&path.includes('/check-runs?'))return {total_count:state.check?1:0,check_runs:state.check?[state.check]:[]}; + if(path.endsWith('/check-runs')&&method==='POST'){ + state.check={...record,...body,output:body.output};return state.check; + } + if(path.endsWith('/check-runs/777')&&method==='GET')return state.check; + if(path.endsWith('/check-runs/777')&&method==='PATCH'){ + state.check={...state.check,...body};return state.check; + } + throw Error('UNEXPECTED_API_PATH:'+path); + }}; +} +const payload={name:'MathGraph Check / OMM claim (advisory)',head_sha:head,status:'completed',conclusion:'neutral', + external_id:`mathgraph-omm-claim-v1:metalogiclabs/mathgraph:239:${head}`, + output:{title:'OMM: original target not established (advisory)', + summary:'| Human mathematical intent | UNKNOWN |\n| Independent evidence warrant | UNKNOWN |'}}; +test('only exact live owned PR head can receive a neutral registered-App Check',async()=>{ + const c=client();const preview=await reconcileOwnedOmmCheck({number:239,baseSha:base,payload,api:c.api}); + assert.equal(preview.mode,'PREVIEW');assert.equal(c.calls.some(x=>x.method==='POST'),false); + const p=await reconcileOwnedOmmCheck({number:239,baseSha:base,payload,api:c.api,write:true}); + assert.equal(p.mode,'CREATED');assert.equal(p.check_run_id,777); + assert.equal(c.calls.some(x=>x.path.endsWith('/check-runs')&&x.method==='POST'),true); + const q=await reconcileOwnedOmmCheck({number:239,baseSha:base,payload,api:c.api,write:true}); + assert.equal(q.mode,'UNCHANGED'); +}); +test('stale head and attempts to report a green success are refused without any mutation',async()=>{ + const c=client({stale:true}); + await assert.rejects(reconcileOwnedOmmCheck({number:239,baseSha:base,payload,api:c.api,write:true}),/STALE_PR_HEAD/); + await assert.rejects(reconcileOwnedOmmCheck({number:239,baseSha:base,payload:{...payload,conclusion:'success'},api:c.api,write:true}),/UNQUALIFIED_OMM_APP_PAYLOAD/); + assert.equal(c.calls.some(x=>x.method==='POST'||x.method==='PATCH'),false); +}); diff --git a/integrations/mathgraph-check-github/omm-v1/artin-native-cli.mjs b/integrations/mathgraph-check-github/omm-v1/artin-native-cli.mjs new file mode 100644 index 000000000..fab27b468 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/artin-native-cli.mjs @@ -0,0 +1,148 @@ +// Qualification-only native wrapper for two exact first-party Artin PR heads. +// The credentialless job executes only the pre-registered source bytes. +import {createHash} from 'node:crypto'; +import {readFile,lstat,mkdir,writeFile} from 'node:fs/promises'; +import {execFile} from 'node:child_process'; +import {promisify} from 'node:util'; +import {join,resolve} from 'node:path'; +import {pathToFileURL} from 'node:url'; +import {runControlledArtinCase} from './artin-native.mjs'; +import {assessOmmClaim} from './claim-admission.mjs'; +import {formatOmmClaimReport} from './report.mjs'; +import {buildOmmAppCheckPreview} from './app-check.mjs'; + +const exec=promisify(execFile); +const sha=s=>createHash('sha256').update(s).digest('hex'); +const artifactErrors=new Set(['NATIVE_OLEAN_PREEXISTS','NATIVE_OLEAN_MISSING', + 'NATIVE_OLEAN_CHANGED','NATIVE_OLEAN_IMPORT_MISMATCH','NATIVE_OLEAN_UNEXPECTED_PART']); + +export async function qualifyArtinDirectory({directory,mathlibGit,runUrl,run}={}){ + if(typeof directory!=='string'||!directory.startsWith('/')||typeof run!=='function')throw Error('INVALID_NATIVE_QUALIFICATION_DIRECTORY'); + const dir=resolve(directory); + const [requestBytes,publishedSource,source,toolchain]=await Promise.all([ + readFile(join(dir,'request.json'),'utf8'),readFile(join(dir,'published-exercise.lean.txt'),'utf8'), + readFile(join(dir,'candidate.lean.txt'),'utf8'),readFile(join(dir,'lean-toolchain'),'utf8')]); + const request=JSON.parse(requestBytes); + const raw=[]; + const auditedRun=async(unit,code)=>{ + const began=performance.now(); + try{ + const result=await run(unit,code); + raw.push({unit,source_sha256:sha(code),exit_code:result?.code??null, + stdout_stderr:String(result?.output??''), + ...(result?.produced_olean_sha256?{produced_olean_sha256:result.produced_olean_sha256}:{}), + ...(result?.imported_olean_sha256?{imported_olean_sha256:result.imported_olean_sha256}:{}), + wall_clock_ms:Math.round(performance.now()-began)}); + return result; + }catch(e){raw.push({unit,source_sha256:sha(code),exit_code:null, + error:artifactErrors.has(e?.message)?e.message:'NATIVE_EXECUTION_FAILED',wall_clock_ms:Math.round(performance.now()-began)});throw e;} + }; + const {contract,receipt}=await runControlledArtinCase({publishedSource,request,source,mathlibGit, + leanToolchainSha256:sha(toolchain),run:auditedRun}); + const contractBytes=JSON.stringify(contract),receiptBytes=JSON.stringify(receipt); + const assessment=assessOmmClaim({request,contractBytes,expectedContractSha256:sha(contractBytes), + receiptBytes,expectedReceiptSha256:sha(receiptBytes)}); + const report=formatOmmClaimReport(assessment,request,runUrl); + const preview=buildOmmAppCheckPreview({request,contractBytes,expectedContractSha256:sha(contractBytes), + receiptBytes,expectedReceiptSha256:sha(receiptBytes),expectedHeadSha:request.head_sha,runUrl}); + if(JSON.stringify(preview.assessment)!==JSON.stringify(assessment))throw Error('APP_PREVIEW_ASSESSMENT_DIVERGED'); + const expected=request.number===239?'NOT_ESTABLISHED_IN_THIS_REPLAY':'CONSUMER_ACCEPTED'; + const dirOut=join(dir,'output');await mkdir(dirOut,{recursive:false}); + const files={ + 'claim-contract.json':contractBytes,'native-receipt.json':receiptBytes, + 'assessment.json':JSON.stringify(assessment),'native-trace.json':JSON.stringify(raw), + 'report.md':report,'app-check-preview.json':JSON.stringify(preview.payload) + }; + await Promise.all(Object.entries(files).map(([name,contents])=>writeFile(join(dirOut,name),contents,{flag:'wx'}))); + const manifest={schema:'mathgraph.omm.artin-qualification-manifest.v1', + repository:request.repository,number:request.number,source_head:request.head_sha,base_sha:request.base_sha, + candidate_source_sha256:sha(source),published_source_sha256:sha(publishedSource), + claim_contract_sha256:sha(contractBytes),native_receipt_sha256:sha(receiptBytes), + artifact_sha256:Object.fromEntries(Object.entries(files).map(([name,contents])=>[name,sha(contents)])), + run_url:runUrl,release:'QUALIFICATION_ONLY',evidence_warrant:'UNKNOWN'}; + await writeFile(join(dirOut,'manifest.json'),JSON.stringify(manifest,null,2)+'\n',{flag:'wx'}); + if(assessment.status!=='NATIVE_RECEIPT_BOUND'||assessment.original_target!==expected) + throw Error('ARTIN_COHORT_NOT_QUALIFIED'); + return assessment; +} + +// Each qualification has a fresh runner and compiles only its registered units. +export function createArtinNativeRunner(directory,runLake){ + const build=join(directory,'.lake/build/lib/lean'); + const olean=unit=>join(build,unit+'.olean'); + const compiled=new Map(); + const env={PATH:process.env.PATH,HOME:process.env.HOME,LEAN_NUM_THREADS:'1'}; + const lake=runLake??(args=>exec('lake',args,{cwd:directory,env,timeout:120000,maxBuffer:512000})); + const statOlean=path=>lstat(path).catch(error=>{ + if(error.code==='ENOENT')return null; + throw error; + }); + // These fixed sources use legacy Lean files and produce no split olean parts. + const verifyLegacyOlean=async path=>{ + if(await statOlean(path+'.server')||await statOlean(path+'.private')) + throw Error('NATIVE_OLEAN_UNEXPECTED_PART'); + }; + const digestOlean=async path=>{ + await verifyLegacyOlean(path); + const stat=await statOlean(path); + if(!stat?.isFile())throw Error('NATIVE_OLEAN_MISSING'); + return sha(await readFile(path)); + }; + const verifyCompiled=async()=>{ + for(const [unit,digest] of compiled){ + if(await digestOlean(olean(unit))!==digest)throw Error('NATIVE_OLEAN_CHANGED'); + } + }; + return async(unit,code)=>{ + if(!['Candidate','Consumer','Audit'].includes(unit))throw Error('UNREGISTERED_NATIVE_UNIT'); + const file=join(directory,unit+'.lean'); + const output=unit==='Audit'?null:olean(unit); + const dependency={Consumer:'Candidate',Audit:'Consumer'}[unit]; + if(output){ + await mkdir(build,{recursive:true}); + if(await statOlean(output))throw Error('NATIVE_OLEAN_PREEXISTS'); + await verifyLegacyOlean(output); + } + await writeFile(file,code,{flag:'wx',mode:0o600}); + let imported; + if(dependency){ + imported=compiled.get(dependency); + if(!imported)throw Error('NATIVE_OLEAN_MISSING'); + await verifyCompiled(); + let deps; + try{deps=await lake(['env','lean','--deps',file]);} + catch{throw Error('NATIVE_OLEAN_IMPORT_MISMATCH');} + const paths=deps.stdout.split(/\r?\n/).filter(Boolean).map(path=>resolve(directory,path)); + if(!paths.includes(olean(dependency)))throw Error('NATIVE_OLEAN_IMPORT_MISMATCH'); + } + let result; + try{ + const r=await lake(['env','lean',...(output?['-o',output]:[]),file]); + result={code:0,output:r.stdout+r.stderr}; + }catch(e){ + if(!Number.isInteger(e.code))throw Error('NATIVE_ENVIRONMENT_UNAVAILABLE'); + result={code:e.code,output:String(e.stdout??'')+String(e.stderr??'')}; + } + await verifyCompiled(); + if(imported)result.imported_olean_sha256=imported; + if(result.code===0&&output){ + result.produced_olean_sha256=await digestOlean(output); + compiled.set(unit,result.produced_olean_sha256); + } + return result; + }; +} + +if(process.argv[1]&&import.meta.url===pathToFileURL(resolve(process.argv[1])).href){ + const dir=process.argv[2]; + try{ + if(!dir||process.argv.length!==3||process.env.GITHUB_REPOSITORY!=='metalogiclabs/mathgraph'|| + !/^[1-9][0-9]*$/.test(process.env.GITHUB_RUN_ID??''))throw Error('QUALIFICATION_CONTEXT_REQUIRED'); + const {stdout}=await exec('git',['-C',join(resolve(dir),'.lake/packages/mathlib'),'rev-parse','HEAD'],{timeout:10000}); + const assessment=await qualifyArtinDirectory({directory:dir,mathlibGit:stdout.trim(), + runUrl:`https://github.com/metalogiclabs/mathgraph/actions/runs/${process.env.GITHUB_RUN_ID}`, + run:createArtinNativeRunner(resolve(dir))}); + console.log(JSON.stringify({status:assessment.status,original_target:assessment.original_target, + candidate_proof:assessment.candidate_proof,intent_fidelity:assessment.intent_fidelity})); + }catch(e){console.error('OMM_ARTIN_QUALIFICATION_INCOMPLETE:'+String(e.message));process.exitCode=1;} +} diff --git a/integrations/mathgraph-check-github/omm-v1/artin-native-cli.test.mjs b/integrations/mathgraph-check-github/omm-v1/artin-native-cli.test.mjs new file mode 100644 index 000000000..e95643fef --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/artin-native-cli.test.mjs @@ -0,0 +1,155 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {mkdtemp,writeFile,readFile,rm,mkdir} from 'node:fs/promises'; +import {tmpdir} from 'node:os'; +import {join} from 'node:path'; +import {createHash} from 'node:crypto'; +import {artinStrongSource,artinRepairSource} from '../mention-artin-control-v1.mjs'; +import {qualifyArtinDirectory,createArtinNativeRunner} from './artin-native-cli.mjs'; + +const sha=x=>createHash('sha256').update(x).digest('hex'); +const published=`import Mathlib + +open Function Fintype Subgroup Ideal Polynomial Submodule Zsqrtd +open scoped BigOperators + + + +/-Informal Statement + +Let $H$ be the subgroup generated by two elements $a, b$ of a group $G$. Prove that if $a b=b a$, then $H$ is an abelian group. +-/ + +theorem Artin_exercise_2_2_9 {G : Type*} [Group G] {a b : G} + (h : a * b = b * a) : + ∀ x y : closure {x | x = a ∨ x = b}, x * y = y * x := by + sorry +`; +const request=(number,source)=>({schema:'mathgraph.omm.request.v1',repository:'metalogiclabs/mathgraph',number, + head_sha:number===239?'2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17':'06f09cd5aaa866365ceb6540cd06389bd37a727c', + base_sha:number===239?'dfd87d1e921953badedcfae21ade76bb38f5c04a':'2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17', + candidate_source_sha256:sha(source),command:'check'}); +async function setup(number,source){ + const directory=await mkdtemp(join(tmpdir(),'omm-artin-test-')); + await writeFile(join(directory,'published-exercise.lean.txt'),published); + await writeFile(join(directory,'candidate.lean.txt'),source); + await writeFile(join(directory,'request.json'),JSON.stringify(request(number,source))); + await writeFile(join(directory,'lean-toolchain'),'leanprover/lean4:v4.28.0\n'); + return directory; +} +const mathlib='8f9d9cff6bd728b17a24e163c9402775d9e6a365'; +const url='https://github.com/metalogiclabs/mathgraph/actions/runs/98765'; + +test('bounded weakened Artin trial writes a report, contracts and immutable evidence manifest',async()=>{ + const directory=await setup(239,artinStrongSource); + try{ + const result=await qualifyArtinDirectory({directory,mathlibGit:mathlib,runUrl:url, + run:async unit=>unit==='Candidate'?{code:0,output:''}:{code:1,output:'type mismatch'}}); + assert.equal(result.original_target,'NOT_ESTABLISHED_IN_THIS_REPLAY'); + const report=await readFile(join(directory,'output','report.md'),'utf8'); + assert.match(report,/original target not established/i); + const receipt=JSON.parse(await readFile(join(directory,'output','native-receipt.json'))); + assert.equal(receipt.number,239); + const manifest=JSON.parse(await readFile(join(directory,'output','manifest.json'))); + assert.equal(manifest.source_head,request(239,artinStrongSource).head_sha); + assert.equal(manifest.native_receipt_sha256,sha(JSON.stringify(receipt))); + const preview=JSON.parse(await readFile(join(directory,'output','app-check-preview.json'))); + assert.equal(preview.conclusion,'neutral'); + assert.match(preview.output.summary,/NOT_ESTABLISHED_IN_THIS_REPLAY|not established in this replay/i); + assert.equal(manifest.artifact_sha256['app-check-preview.json'],sha(JSON.stringify(preview))); + }finally{await rm(directory,{recursive:true,force:true});} +}); + +test('frozen repaired Artin trial writes a positive consumer, not an intent warrant',async()=>{ + const directory=await setup(240,artinRepairSource); + try{ + const result=await qualifyArtinDirectory({directory,mathlibGit:mathlib,runUrl:url, + run:async unit=>({code:0,output:unit==='Audit'?"'Artin_exercise_2_2_9' depends on axioms: [propext]\n'MathGraphConsumer.original_target' depends on axioms: [propext]":''})}); + assert.equal(result.original_target,'CONSUMER_ACCEPTED'); + assert.equal(result.intent_fidelity,'UNKNOWN'); + const report=await readFile(join(directory,'output','report.md'),'utf8'); + assert.match(report,/Human mathematical intent \| UNKNOWN/); + const preview=JSON.parse(await readFile(join(directory,'output','app-check-preview.json'))); + assert.equal(preview.conclusion,'neutral'); + assert.match(preview.output.summary,/Human mathematical intent \| UNKNOWN/); + }finally{await rm(directory,{recursive:true,force:true});} +}); + +test('source edit before qualification is blocked before Lean is invoked',async()=>{ + const directory=await setup(239,artinStrongSource+'\n-- injected content'); + try{ + let calls=0; + await assert.rejects(qualifyArtinDirectory({directory,mathlibGit:mathlib,runUrl:url, + run:async()=>{calls++;return {code:0,output:''};}})); + assert.equal(calls,0); + }finally{await rm(directory,{recursive:true,force:true});} +}); + +test('native runner binds each direct import to the newly compiled artifact',async()=>{ + const directory=await mkdtemp(join(tmpdir(),'omm-artin-artifact-')); + try{ + const build=join(directory,'.lake/build/lib/lean'); + const candidate=join(build,'Candidate.olean'); + const consumer=join(build,'Consumer.olean'); + const calls=[]; + const run=createArtinNativeRunner(directory,async args=>{ + calls.push(args); + if(args.includes('--deps')){ + return {stdout:(args.at(-1).endsWith('Consumer.lean')?candidate:consumer)+'\n',stderr:''}; + } + if(args.includes('-o'))await writeFile(args[args.indexOf('-o')+1],args.at(-1).endsWith('Candidate.lean')?'candidate bytes':'consumer bytes'); + return {stdout:'',stderr:''}; + }); + assert.equal((await run('Candidate','candidate')).produced_olean_sha256,sha('candidate bytes')); + assert.equal((await run('Consumer','consumer')).imported_olean_sha256,sha('candidate bytes')); + assert.equal((await run('Audit','audit')).imported_olean_sha256,sha('consumer bytes')); + assert.equal(calls.filter(args=>args.includes('--deps')).length,2); + }finally{await rm(directory,{recursive:true,force:true});} +}); + +test('native runner refuses stale outputs, missing outputs, and shadowed imports',async()=>{ + for(const fault of ['stale','missing','shadow','replacement','extra-part']){ + const directory=await mkdtemp(join(tmpdir(),'omm-artin-artifact-')); + try{ + const build=join(directory,'.lake/build/lib/lean'); + const candidate=join(build,'Candidate.olean'); + const shadow=join(directory,'Candidate.olean'); + await mkdir(build,{recursive:true}); + if(fault==='stale')await writeFile(candidate,'stale'); + await writeFile(shadow,'shadow'); + let compilations=0; + const run=createArtinNativeRunner(directory,async args=>{ + if(args.includes('--deps'))return {stdout:(fault==='shadow'?shadow:candidate)+'\n',stderr:''}; + compilations++; + if(fault!=='missing')await writeFile(args[args.indexOf('-o')+1],'fresh artifact'); + return {stdout:'',stderr:''}; + }); + if(fault==='stale'||fault==='missing'){ + await assert.rejects(run('Candidate','candidate'),/NATIVE_OLEAN_(PREEXISTS|MISSING)/); + assert.equal(compilations,fault==='stale'?0:1); + }else{ + await run('Candidate','candidate'); + if(fault==='replacement')await writeFile(candidate,'replacement'); + if(fault==='extra-part')await writeFile(candidate+'.server','unexpected part'); + await assert.rejects(run('Consumer','consumer'),/NATIVE_OLEAN_(IMPORT_MISMATCH|CHANGED|UNEXPECTED_PART)/); + assert.equal(compilations,1); + } + }finally{await rm(directory,{recursive:true,force:true});} + } +}); + +test('artifact binding failure remains an execution failure in the qualification trace',async()=>{ + const directory=await setup(239,artinStrongSource); + try{ + const artifactSha=sha('candidate bytes'); + await assert.rejects(qualifyArtinDirectory({directory,mathlibGit:mathlib,runUrl:url, + run:async unit=>{ + if(unit==='Candidate')return {code:0,output:'',produced_olean_sha256:artifactSha}; + throw Error('NATIVE_OLEAN_IMPORT_MISMATCH'); + }}),/ARTIN_COHORT_NOT_QUALIFIED/); + const trace=JSON.parse(await readFile(join(directory,'output','native-trace.json'))); + assert.equal(trace[0].produced_olean_sha256,artifactSha); + assert.equal(trace[1].exit_code,null); + assert.equal(trace[1].error,'NATIVE_OLEAN_IMPORT_MISMATCH'); + }finally{await rm(directory,{recursive:true,force:true});} +}); diff --git a/integrations/mathgraph-check-github/omm-v1/artin-native.mjs b/integrations/mathgraph-check-github/omm-v1/artin-native.mjs new file mode 100644 index 000000000..788d93f00 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/artin-native.mjs @@ -0,0 +1,76 @@ +// First-party fixed-source Artin cohort for OMM qualification only. No GitHub +// webhook, model request or arbitrary Lean source may invoke this runner. +import {createHash} from 'node:crypto'; +import {artinStrongSource,artinRepairSource,artinTarget,bindPublishedArtin} from '../mention-artin-control-v1.mjs'; + +const sha=s=>createHash('sha256').update(s).digest('hex'); +const REPO='metalogiclabs/mathgraph'; +const MATHLIB='8f9d9cff6bd728b17a24e163c9402775d9e6a365'; +const TOOLCHAIN=sha('leanprover/lean4:v4.28.0\n'); +const CANDIDATE='Artin_exercise_2_2_9'; +const CONSUMER='MathGraphConsumer.original_target'; +const PRS=Object.freeze({ + 239:{head:'2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17',base:'dfd87d1e921953badedcfae21ade76bb38f5c04a',source:artinStrongSource}, + 240:{head:'06f09cd5aaa866365ceb6540cd06389bd37a727c',base:'2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17',source:artinRepairSource} +}); + +const consumerSource='import Candidate\n\nopen Subgroup\n\n'+ + artinTarget.replace('theorem Artin_exercise_2_2_9','theorem '+CONSUMER)+ + ' := by\n exact Artin_exercise_2_2_9 h\n'; +const auditSource=`import Consumer\n#print axioms ${CANDIDATE}\n#print axioms ${CONSUMER}\n`; + +function readAxioms(output,name){ + const lines=String(output??'').split(/\r?\n/).map(s=>s.trim()); + const matching=lines.filter(line=>line.startsWith(`'${name}' `)); + if(matching.length!==1)return null; + if(matching[0]===`'${name}' does not depend on any axioms`)return []; + const prefix=`'${name}' depends on axioms: [`; + if(!matching[0].startsWith(prefix)||!matching[0].endsWith(']'))return null; + const raw=matching[0].slice(prefix.length,-1); + return raw?raw.split(',').map(x=>x.trim()):[]; +} + +const STANDARD=new Set(['propext','Classical.choice','Quot.sound']); +const clean=axioms=>Array.isArray(axioms)&&new Set(axioms).size===axioms.length&&axioms.every(x=>STANDARD.has(x)); + +/** The source and PR commits are predeclared first-party controls, never user input. */ +export async function runControlledArtinCase({publishedSource,request,source,mathlibGit,leanToolchainSha256,run}={}){ + const profile=PRS[request?.number]; + if(request?.schema!=='mathgraph.omm.request.v1'||request?.repository!==REPO||request.command!=='check'|| + !profile||request.head_sha!==profile.head||request.base_sha!==profile.base|| + source!==profile.source||request.candidate_source_sha256!==sha(source)|| + mathlibGit!==MATHLIB||leanToolchainSha256!==TOOLCHAIN||typeof run!=='function') + throw Error('UNREGISTERED_ARTIN_SOURCE_OR_ENVIRONMENT'); + const published=bindPublishedArtin(publishedSource); + const contract={schema:'mathgraph.omm.claim-contract.v1',repository:REPO,number:request.number, + head_sha:profile.head,base_sha:profile.base,reference_sha256:published.source_sha256, + candidate_sha256:sha(source),consumer_sha256:sha(consumerSource), + consumer_name:CONSUMER,candidate_name:CANDIDATE,mathlib_git:MATHLIB, + lean_toolchain_sha256:TOOLCHAIN}; + const trace=[];let auditOutput='';let executionFailed=false; + const execute=async(unit,content)=>{ + try{ + const r=await run(unit,content); + if(!r||!Number.isInteger(r.code)||r.code<0||typeof r.output!=='string')throw Error('INVALID_NATIVE_RESULT'); + trace.push({unit,exit_code:r.code});return r; + }catch{executionFailed=true;trace.push({unit,exit_code:null,execution_failed:true});return null;} + }; + const candidate=await execute('Candidate',source); + let consumer=null; + if(candidate?.code===0)consumer=await execute('Consumer',consumerSource); + if(consumer?.code===0){const audit=await execute('Audit',auditSource);auditOutput=audit?.output??'';} + const candidateAxioms=readAxioms(auditOutput,CANDIDATE),axioms=readAxioms(auditOutput,CONSUMER); + const accepted=trace.length===3&&trace.every(x=>x.exit_code===0)&& + clean(candidateAxioms)&&clean(axioms); + const native={execution_status:executionFailed?'RUN_FAILED':'EXECUTED', + source_reference_sha256:contract.reference_sha256,candidate_sha256:contract.candidate_sha256, + consumer_sha256:contract.consumer_sha256,consumer_name:CONSUMER, + candidate_declaration_name:CANDIDATE,mathlib_git:MATHLIB,lean_toolchain_sha256:TOOLCHAIN, + candidate_source_build:candidate?.code===0?'LEAN_ACCEPTED':'UNKNOWN', + original_target_status:accepted?'NATIVE_CONSUMER_ACCEPTED':'UNKNOWN', + candidate_axioms:clean(candidateAxioms)?candidateAxioms:null, + axioms:clean(axioms)?axioms:null,trace}; + const receipt={schema:'mathgraph.omm.native-receipt.v1',repository:REPO,number:request.number, + head_sha:profile.head,base_sha:profile.base,native}; + return {contract,receipt}; +} diff --git a/integrations/mathgraph-check-github/omm-v1/artin-native.test.mjs b/integrations/mathgraph-check-github/omm-v1/artin-native.test.mjs new file mode 100644 index 000000000..87b1e1e8c --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/artin-native.test.mjs @@ -0,0 +1,85 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {createHash} from 'node:crypto'; +import {artinStrongSource,artinRepairSource} from '../mention-artin-control-v1.mjs'; +import {runControlledArtinCase} from './artin-native.mjs'; +import {assessOmmClaim} from './claim-admission.mjs'; + +const published=`import Mathlib + +open Function Fintype Subgroup Ideal Polynomial Submodule Zsqrtd +open scoped BigOperators + + + +/-Informal Statement + +Let $H$ be the subgroup generated by two elements $a, b$ of a group $G$. Prove that if $a b=b a$, then $H$ is an abelian group. +-/ + +theorem Artin_exercise_2_2_9 {G : Type*} [Group G] {a b : G} + (h : a * b = b * a) : + ∀ x y : closure {x | x = a ∨ x = b}, x * y = y * x := by + sorry +`; +const sha=x=>createHash('sha256').update(x).digest('hex'); +const mathlib='8f9d9cff6bd728b17a24e163c9402775d9e6a365'; +const toolchainSha=sha('leanprover/lean4:v4.28.0\n'); +const strong={schema:'mathgraph.omm.request.v1',repository:'metalogiclabs/mathgraph',number:239, + head_sha:'2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17',base_sha:'dfd87d1e921953badedcfae21ade76bb38f5c04a', + candidate_source_sha256:sha(artinStrongSource),command:'check'}; +const repaired={...strong,number:240,head_sha:'06f09cd5aaa866365ceb6540cd06389bd37a727c', + base_sha:strong.head_sha,candidate_source_sha256:sha(artinRepairSource)}; +const initial={publishedSource:published,mathlibGit:mathlib,leanToolchainSha256:toolchainSha}; +const withPins=({contract,receipt},request)=>{ + const contractBytes=JSON.stringify(contract),receiptBytes=JSON.stringify(receipt); + return assessOmmClaim({request,contractBytes,expectedContractSha256:sha(contractBytes), + receiptBytes,expectedReceiptSha256:sha(receiptBytes)}); +}; + +test('published Artin theorem binds original Group claim; stronger CommGroup source compiles but consumer rejects',async()=>{ + const calls=[]; + const result=await runControlledArtinCase({...initial,request:strong,source:artinStrongSource, + run:async(unit,content)=>{calls.push([unit,content]);return unit==='Candidate'?{code:0,output:''}:{code:1,output:'type mismatch'};}}); + assert.deepEqual(calls.map(x=>x[0]),['Candidate','Consumer']); + assert.match(calls[1][1],/\[Group G\]/); + assert.match(calls[1][1],/MathGraphConsumer\.original_target/); + const a=withPins(result,strong); + assert.equal(a.status,'NATIVE_RECEIPT_BOUND'); + assert.equal(a.candidate_proof,'LEAN_COMPILED'); + assert.equal(a.original_target,'NOT_ESTABLISHED_IN_THIS_REPLAY'); + assert.equal(a.intent_fidelity,'UNKNOWN'); +}); + +test('original Group target, separate consumer and candidate axiom audit can qualify one positive replay',async()=>{ + const calls=[]; + const result=await runControlledArtinCase({...initial,request:repaired,source:artinRepairSource, + run:async(unit,content)=>{calls.push([unit,content]);return {code:0,output:unit==='Audit'?"'Artin_exercise_2_2_9' depends on axioms: [propext]\n'MathGraphConsumer.original_target' depends on axioms: [propext]":''};}}); + assert.deepEqual(calls.map(x=>x[0]),['Candidate','Consumer','Audit']); + assert.match(calls[2][1],/#print axioms MathGraphConsumer\.original_target/); + assert.equal(withPins(result,repaired).original_target,'CONSUMER_ACCEPTED'); + assert.equal(withPins(result,repaired).candidate_dependence,'UNKNOWN'); +}); + +test('missing axiom outputs and native runner errors cannot create a success',async()=>{ + for(const outcome of ['missing','error']){ + const result=await runControlledArtinCase({...initial,request:repaired,source:artinRepairSource, + run:async(unit)=>{ + if(outcome==='error'&&unit==='Consumer')throw Error('process failed to start'); + return {code:0,output:''}; + }}); + const a=withPins(result,repaired); + assert.notEqual(a.original_target,'CONSUMER_ACCEPTED'); + assert.equal(a.evidence_warrant,'UNKNOWN'); + } +}); + +test('unsupported PR heads and source content are rejected before execution',async()=>{ + for(const changed of [{request:{...strong,head_sha:'a'.repeat(40)}}, + {source:artinStrongSource+'\n#eval 1'}]){ + let invoked=0; + await assert.rejects(()=>runControlledArtinCase({...initial,request:strong,source:artinStrongSource,...changed, + run:async()=>{invoked++;return {code:0,output:''};}})); + assert.equal(invoked,0); + } +}); diff --git a/integrations/mathgraph-check-github/omm-v1/artin-preflight.mjs b/integrations/mathgraph-check-github/omm-v1/artin-preflight.mjs new file mode 100644 index 000000000..5fea8b0a5 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/artin-preflight.mjs @@ -0,0 +1,74 @@ +// Authenticated, fixed-cohort Artin input binding for the OMM *research* +// workflow. Never accepts an arbitrary PR number, source path or Lean program. +import {createHash} from 'node:crypto'; +import {mkdir,writeFile} from 'node:fs/promises'; +import {resolve,join} from 'node:path'; +import {pathToFileURL} from 'node:url'; +import {artinStrongSource,artinRepairSource,bindPublishedArtin} from '../mention-artin-control-v1.mjs'; + +const digest=s=>createHash('sha256').update(s).digest('hex'); +const REPO='metalogiclabs/mathgraph'; +const ORIGINAL='https://raw.githubusercontent.com/marcusm117/ProofNet-Verified/14879cb23e078d122adecd9578dd26a7304f753b/data/archive/proofnet-verified-4.28/proofnet-1.lean'; +const FILE='mathgraph-demo/ArtinGenerators.lean'; +const PROFILES=Object.freeze({ + 239:{head:'2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17',base:'dfd87d1e921953badedcfae21ade76bb38f5c04a', + candidate:artinStrongSource,source_hash:'e45efb73729fc98e45e66d216e792e70f67f8c43d4c12cbb753ac410b950e4d9', + changed:['mathgraph-demo/ArtinGenerators.lean:added','mathgraph-demo/README.md:added']}, + 240:{head:'06f09cd5aaa866365ceb6540cd06389bd37a727c',base:'2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17', + candidate:artinRepairSource,source_hash:'7153ebbaef33070b5b25ee3e8f3ba6dd35c6868f7a5dac07867255635ed85557', + changed:['mathgraph-demo/ArtinGenerators.lean:modified']} +}); + +async function getBody(fetcher,url,parseJson){ + const response=await fetcher(url,{headers:{Accept:'application/vnd.github+json','User-Agent':'MathGraph-OMM-Bounded-Qualification'}}); + if(!response?.ok)throw Error('TRUSTED_SOURCE_FETCH_UNAVAILABLE'); + const raw=await response.text(); + if(raw.length>1_000_000)throw Error('TRUSTED_SOURCE_SIZE_EXCEEDED'); + try{return parseJson?JSON.parse(raw):raw;}catch{throw Error('TRUSTED_SOURCE_JSON_INVALID');} +} + +export async function checkPublishedArtinInput({number,fetch:fetcher=globalThis.fetch,verifyReference=bindPublishedArtin}={}){ + if(!Number.isInteger(number)||!PROFILES[number])throw Error('UNREGISTERED_ARTIN_PR'); + if(typeof fetcher!=='function'||typeof verifyReference!=='function')throw Error('TRUSTED_SOURCE_FETCH_UNAVAILABLE'); + const profile=PROFILES[number]; + const root=`https://api.github.com/repos/${REPO}`; + const pull=await getBody(fetcher,`${root}/pulls/${number}`,true); + if(pull?.number!==number||pull?.state!=='open'||pull?.head?.sha!==profile.head|| + pull?.base?.sha!==profile.base||pull?.head?.repo?.full_name!==REPO|| + pull?.base?.repo?.full_name!==REPO||pull?.changed_files!==profile.changed.length) + throw Error('PR_IDENTITY_CHANGED'); + const files=await getBody(fetcher,`${root}/pulls/${number}/files?per_page=100`,true); + if(!Array.isArray(files)||files.length!==profile.changed.length|| + files.map(f=>`${f.filename}:${f.status}`).sort().join('|')!==profile.changed.slice().sort().join('|')) + throw Error('PR_DIFF_SURFACE_CHANGED'); + const source=await getBody(fetcher,`https://raw.githubusercontent.com/${REPO}/${profile.head}/${FILE}`,false); + if(digest(profile.candidate)!==profile.source_hash||source!==profile.candidate||digest(source)!==profile.source_hash) + throw Error('SOURCE_BYTES_CHANGED'); + const publishedSource=await getBody(fetcher,ORIGINAL,false); + try{if(!verifyReference(publishedSource))throw Error('PUBLISHED_TARGET_CHANGED');} + catch(e){if(e.message==='PUBLISHED_TARGET_CHANGED')throw e;throw Error('PUBLISHED_TARGET_CHANGED');} + return {request:{schema:'mathgraph.omm.request.v1',repository:REPO,number,command:'check', + head_sha:profile.head,base_sha:profile.base,candidate_source_sha256:profile.source_hash}, + source,publishedSource}; +} + +if(process.argv[1]&&import.meta.url===pathToFileURL(resolve(process.argv[1])).href){ + try{ + const number=Number(process.argv[2]),directory=process.argv[3]; + if(process.argv.length!==4||!directory?.startsWith('/')|| + process.env.GITHUB_REPOSITORY!==REPO|| + !/^[1-9][0-9]*$/.test(process.env.GITHUB_RUN_ID??'')|| + !process.env.GITHUB_TOKEN)throw Error('QUALIFICATION_CONTEXT_REQUIRED'); + const authorizedFetch=(url,opts)=>fetch(url,{...opts,headers:{...opts?.headers, + ...(url.startsWith(`https://api.github.com/repos/${REPO}/`)?{Authorization:`Bearer ${process.env.GITHUB_TOKEN}`}:{})}}); + const {request,source,publishedSource}=await checkPublishedArtinInput({number,fetch:authorizedFetch}); + const dir=resolve(directory); + await mkdir(dir,{recursive:false}); + await Promise.all([ + writeFile(join(dir,'request.json'),JSON.stringify(request,null,2)+'\n',{flag:'wx'}), + writeFile(join(dir,'candidate.lean.txt'),source,{flag:'wx'}), + writeFile(join(dir,'published-exercise.lean.txt'),publishedSource,{flag:'wx'}) + ]); + console.log(`OMM_ARTIN_PREFLIGHT_BOUND:${number}:${request.head_sha}`); + }catch(e){console.error('OMM_ARTIN_PREFLIGHT_UNKNOWN:'+String(e.message));process.exitCode=1;} +} diff --git a/integrations/mathgraph-check-github/omm-v1/artin-preflight.test.mjs b/integrations/mathgraph-check-github/omm-v1/artin-preflight.test.mjs new file mode 100644 index 000000000..0f711f9e0 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/artin-preflight.test.mjs @@ -0,0 +1,51 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {artinStrongSource,artinRepairSource} from '../mention-artin-control-v1.mjs'; +import {checkPublishedArtinInput} from './artin-preflight.mjs'; + +const reference='published-exercise'; +const sample=(n=239)=>({ + number:n,state:'open',changed_files:n===239?2:1, + head:{sha:n===239?'2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17':'06f09cd5aaa866365ceb6540cd06389bd37a727c',repo:{full_name:'metalogiclabs/mathgraph'}}, + base:{sha:n===239?'dfd87d1e921953badedcfae21ade76bb38f5c04a':'2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17',repo:{full_name:'metalogiclabs/mathgraph'}} +}); +function fixture(n=239,{mutate,source}={}){ + const pull=sample(n);mutate?.(pull); + const files=n===239?[{filename:'mathgraph-demo/ArtinGenerators.lean',status:'added'},{filename:'mathgraph-demo/README.md',status:'added'}]:[{filename:'mathgraph-demo/ArtinGenerators.lean',status:'modified'}]; + const inputs=[]; + const fetch=async(url)=>{ + inputs.push(url); + let body; + if(url.endsWith(`/pulls/${n}`))body=JSON.stringify(pull); + else if(url.endsWith(`/pulls/${n}/files?per_page=100`))body=JSON.stringify(files); + else if(url.includes('/ProofNet-Verified/'))body=reference; + else if(url.includes('/ArtinGenerators.lean'))body=source??(n===239?artinStrongSource:artinRepairSource); + else throw Error('UNEXPECTED_URL'); + return {ok:true,text:async()=>body}; + }; + return {fetch,inputs}; +} +test('both fixed public Artin controls are pinned to authenticated PR identity and exact candidate bytes',async()=>{ + for(const n of [239,240]){ + const {fetch,inputs}=fixture(n); + const p=await checkPublishedArtinInput({number:n,fetch, + verifyReference:reference==='published-exercise'?()=>true:undefined}); + assert.equal(p.request.number,n); + assert.equal(p.request.head_sha,sample(n).head.sha); + assert.equal(p.source,n===239?artinStrongSource:artinRepairSource); + assert.equal(p.publishedSource,reference); + assert.equal(inputs.length,4); + } +}); +test('changed head, foreign source or unexpected changed files all fail closed',async()=>{ + const changed=fixture(239,{mutate:p=>{p.head.sha='1'.repeat(40)}}); + await assert.rejects(checkPublishedArtinInput({number:239,fetch:changed.fetch,verifyReference:()=>true}),/PR_IDENTITY_CHANGED/); + const candidate=fixture(240,{source:artinStrongSource}); + await assert.rejects(checkPublishedArtinInput({number:240,fetch:candidate.fetch,verifyReference:()=>true}),/SOURCE_BYTES_CHANGED/); + const {fetch}=fixture(239); + await assert.rejects(checkPublishedArtinInput({number:238,fetch,verifyReference:()=>true}),/UNREGISTERED_ARTIN_PR/); +}); +test('unverified published original never reaches native qualification',async()=>{ + const {fetch}=fixture(239); + await assert.rejects(checkPublishedArtinInput({number:239,fetch,verifyReference:()=>false}),/PUBLISHED_TARGET_CHANGED/); +}); diff --git a/integrations/mathgraph-check-github/omm-v1/claim-admission.mjs b/integrations/mathgraph-check-github/omm-v1/claim-admission.mjs new file mode 100644 index 000000000..1a0c2dde0 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/claim-admission.mjs @@ -0,0 +1,84 @@ +// Bounded OMM evidence bridge. This module never executes source or authenticates +// GitHub itself: the caller must obtain request identities and both independent +// artifact pins from the trusted Check controller, not PR-controlled material. +import {createHash} from 'node:crypto'; + +const SHA40=/^[a-f0-9]{40}$/; +const SHA256=/^[a-f0-9]{64}$/; +const LEAN_NAME=/^[A-Za-z_][A-Za-z0-9_'.]{0,150}$/; +const STANDARD_AXIOMS=new Set(['propext','Classical.choice','Quot.sound']); + +const digest=s=>createHash('sha256').update(s).digest('hex'); +const same=(left,right,keys)=>keys.every(key=>left?.[key]===right?.[key]); +const cleanAxioms=xs=>Array.isArray(xs)&&xs.every(x=>typeof x==='string'&&STANDARD_AXIOMS.has(x))&&new Set(xs).size===xs.length; +const validExit=step=>step&&Number.isInteger(step.exit_code)&&step.execution_failed!==true; + +function unknown(reason){return {schema:'mathgraph.omm.claim-assessment.v1',status:'UNKNOWN',reason, + candidate_proof:'UNKNOWN',original_target:'UNKNOWN',formal_statement_relation:'UNKNOWN', + intent_fidelity:'UNKNOWN',evidence_warrant:'UNKNOWN',candidate_dependence:'UNKNOWN', + candidate_axiom_status:'UNKNOWN',repair_permission:'DENIED'};} + +function pinnedJson(bytes,pin){ + if(typeof bytes!=='string'||bytes.length>2_000_000||!SHA256.test(pin??'')||digest(bytes)!==pin)return null; + try{return JSON.parse(bytes);}catch{return null;} +} + +/** Only a bounded native-replay observation; never paper-to-Lean intent warrant. */ +export function assessOmmClaim({request,contractBytes,expectedContractSha256,receiptBytes,expectedReceiptSha256}={}){ + if(request?.schema!=='mathgraph.omm.request.v1'||request.command!=='check'|| + !Number.isSafeInteger(request.number)||request.number<1|| + !/^[a-zA-Z0-9_.-]+\/[a-zA-Z0-9_.-]+$/.test(request.repository??'')|| + !SHA40.test(request.head_sha??'')||!SHA40.test(request.base_sha??'')|| + !SHA256.test(request.candidate_source_sha256??''))return unknown('REQUEST_NOT_AUTHENTICATED_OR_NOT_SUPPORTED'); + + const contract=pinnedJson(contractBytes,expectedContractSha256); + if(!contract||contract.schema!=='mathgraph.omm.claim-contract.v1'|| + !same(contract,request,['repository','number','head_sha','base_sha'])|| + !SHA256.test(contract.reference_sha256??'')|| + !SHA256.test(contract.candidate_sha256??'')|| + !SHA256.test(contract.consumer_sha256??'')|| + !LEAN_NAME.test(contract.consumer_name??'')||!LEAN_NAME.test(contract.candidate_name??'')|| + !SHA40.test(contract.mathlib_git??'')|| + !SHA256.test(contract.lean_toolchain_sha256??'')|| + contract.candidate_sha256!==request.candidate_source_sha256) + return unknown('TRUSTED_CLAIM_CONTRACT_MISSING_OR_SOURCE_CHANGED'); + + const receipt=pinnedJson(receiptBytes,expectedReceiptSha256); + if(!receipt||receipt.schema!=='mathgraph.omm.native-receipt.v1'|| + !same(receipt,request,['repository','number','head_sha','base_sha'])) + return unknown('NATIVE_RECEIPT_MISSING_OR_CONTEXT_CHANGED'); + + const n=receipt.native; + if(!n||!same(n,contract,['candidate_sha256','consumer_sha256','consumer_name','mathlib_git','lean_toolchain_sha256'])|| + n.candidate_declaration_name!==contract.candidate_name|| + n.source_reference_sha256!==contract.reference_sha256||n.execution_status!=='EXECUTED'|| + !Array.isArray(n.trace)||n.trace.length<2||n.trace.length>3|| + n.trace[0]?.unit!=='Candidate'||n.trace[1]?.unit!=='Consumer'|| + n.trace.some(step=>!step||typeof step.unit!=='string'||step.execution_failed===true)) + return unknown('NATIVE_EXECUTION_OR_ENVIRONMENT_NOT_QUALIFIED'); + + if(!validExit(n.trace[0])||n.trace[0].exit_code!==0||n.candidate_source_build!=='LEAN_ACCEPTED') + return unknown('CANDIDATE_NOT_COMPILED'); + + let originalTarget='UNKNOWN'; + if(validExit(n.trace[1])&&n.trace[1].exit_code!==0&&n.original_target_status!=='NATIVE_CONSUMER_ACCEPTED') + originalTarget='NOT_ESTABLISHED_IN_THIS_REPLAY'; + else if(n.trace.length===3&&n.trace[2]?.unit==='Audit'&&validExit(n.trace[2])&& + n.trace[1].exit_code===0&&n.trace[2].exit_code===0&& + n.original_target_status==='NATIVE_CONSUMER_ACCEPTED'&& + cleanAxioms(n.axioms)&&cleanAxioms(n.candidate_axioms)) + originalTarget='CONSUMER_ACCEPTED'; + + return {schema:'mathgraph.omm.claim-assessment.v1',status:'NATIVE_RECEIPT_BOUND', + candidate_proof:'LEAN_COMPILED',original_target:originalTarget, + // SemantEq and source-to-paper authority are distinct future admissions. + formal_statement_relation:'UNKNOWN',intent_fidelity:'UNKNOWN',evidence_warrant:'UNKNOWN', + candidate_dependence:'UNKNOWN',candidate_axiom_status:cleanAxioms(n.candidate_axioms)?'STANDARD_ONLY':'UNKNOWN', + repair_permission:'DENIED',repository:request.repository,number:request.number, + head_sha:request.head_sha,base_sha:request.base_sha, + reference_sha256:contract.reference_sha256,candidate_sha256:contract.candidate_sha256, + consumer_sha256:contract.consumer_sha256,contract_sha256:expectedContractSha256, + native_receipt_sha256:expectedReceiptSha256, + reason:originalTarget==='NOT_ESTABLISHED_IN_THIS_REPLAY'?'ORIGINAL_TARGET_CONSUMER_REJECTED': + originalTarget==='CONSUMER_ACCEPTED'?'NATIVE_ORIGINAL_TARGET_REPLAY_ACCEPTED':'ORIGINAL_TARGET_AUDIT_INCOMPLETE'}; +} diff --git a/integrations/mathgraph-check-github/omm-v1/claim-admission.test.mjs b/integrations/mathgraph-check-github/omm-v1/claim-admission.test.mjs new file mode 100644 index 000000000..d1e9a84cd --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/claim-admission.test.mjs @@ -0,0 +1,120 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {createHash} from 'node:crypto'; +import {assessOmmClaim} from './claim-admission.mjs'; + +const hash = bytes => createHash('sha256').update(bytes).digest('hex'); +const original = 'a'.repeat(64), candidate = 'b'.repeat(64), consumer = 'c'.repeat(64); +const head = 'd'.repeat(40), base = 'e'.repeat(40), mathlib = 'f'.repeat(40); +const toolchain = '9'.repeat(64); +const request = {schema: 'mathgraph.omm.request.v1', repository: 'metalogiclabs/mathgraph', number: 300, + head_sha: head, base_sha: base, candidate_source_sha256: candidate, command: 'check'}; +const contract = {schema: 'mathgraph.omm.claim-contract.v1', repository: request.repository, + number: request.number, head_sha: head, base_sha: base, + reference_sha256: original, candidate_sha256: candidate, consumer_sha256: consumer, + consumer_name: 'MathGraphConsumer.original_target', + candidate_name: 'Proposed.candidate', + mathlib_git: mathlib, lean_toolchain_sha256: toolchain}; +const native = {execution_status: 'EXECUTED', source_reference_sha256: original, + candidate_sha256: candidate, consumer_sha256: consumer, + consumer_name: contract.consumer_name, mathlib_git: mathlib, + candidate_declaration_name: contract.candidate_name, + lean_toolchain_sha256: toolchain, candidate_source_build: 'LEAN_ACCEPTED', + original_target_status: 'NATIVE_CONSUMER_ACCEPTED', axioms: ['propext'], + candidate_axioms: ['propext'], + trace: [{unit: 'Candidate', exit_code: 0}, {unit: 'Consumer', exit_code: 0}, + {unit: 'Audit', exit_code: 0}]}; +const receipt = {schema: 'mathgraph.omm.native-receipt.v1', + repository: request.repository, number: request.number, head_sha: head, base_sha: base, native}; +const args = (c=contract, r=receipt, req=request) => { + const contractBytes = JSON.stringify(c), receiptBytes = JSON.stringify(r); + return {request: req, contractBytes, expectedContractSha256: hash(contractBytes), + receiptBytes, expectedReceiptSha256: hash(receiptBytes)}; +}; + +test('native compilation and an original-target consumer are separate, bounded observations', () => { + const result = assessOmmClaim(args()); + assert.equal(result.status, 'NATIVE_RECEIPT_BOUND'); + assert.equal(result.candidate_proof, 'LEAN_COMPILED'); + assert.equal(result.original_target, 'CONSUMER_ACCEPTED'); + assert.equal(result.formal_statement_relation, 'UNKNOWN'); + assert.equal(result.intent_fidelity, 'UNKNOWN'); + assert.equal(result.evidence_warrant, 'UNKNOWN'); + assert.equal(result.candidate_dependence, 'UNKNOWN'); + assert.equal(result.candidate_axiom_status, 'STANDARD_ONLY'); + assert.equal(result.repair_permission, 'DENIED'); + assert.equal(result.head_sha, head); +}); + +test('a compiling but weakened candidate does not inherit original-target acceptance', () => { + const failed = structuredClone(receipt); + failed.native.original_target_status = 'UNKNOWN'; + failed.native.axioms = null; + failed.native.trace = [{unit:'Candidate',exit_code:0}, {unit:'Consumer',exit_code:1}]; + const result = assessOmmClaim(args(contract, failed)); + assert.equal(result.candidate_proof, 'LEAN_COMPILED'); + assert.equal(result.original_target, 'NOT_ESTABLISHED_IN_THIS_REPLAY'); + assert.equal(result.formal_statement_relation, 'UNKNOWN'); + assert.equal(result.intent_fidelity, 'UNKNOWN'); + assert.equal(result.repair_permission, 'DENIED'); +}); + +test('stale GitHub head or changed source sha invalidates native admission', () => { + for (const change of [{head_sha:'1'.repeat(40)}, {candidate_source_sha256:'2'.repeat(64)}]) { + const result = assessOmmClaim(args(contract, receipt, {...request, ...change})); + assert.equal(result.status, 'UNKNOWN'); + assert.equal(result.candidate_proof, 'UNKNOWN'); + assert.equal(result.original_target, 'UNKNOWN'); + } +}); + +test('no trusted contract or independently pinned artifact means UNKNOWN, even if payload says PASS', () => { + const a = args(); + for (const patch of [ + {expectedContractSha256: '0'.repeat(64)}, + {expectedReceiptSha256: '0'.repeat(64)}, + {expectedContractSha256: null}, + {receiptBytes: '{not json'}, + ]) { + const result = assessOmmClaim({...a, ...patch}); + assert.equal(result.status, 'UNKNOWN'); + assert.equal(result.evidence_warrant, 'UNKNOWN'); + } +}); + +test('native tool/environment errors never become Lean proof rejection or original-target failures', () => { + const failed = structuredClone(receipt); + failed.native.execution_status = 'ENVIRONMENT_UNAVAILABLE'; + failed.native.trace = [{unit:'Candidate',exit_code:null,execution_failed:true}]; + const result = assessOmmClaim(args(contract, failed)); + assert.equal(result.candidate_proof, 'UNKNOWN'); + assert.equal(result.original_target, 'UNKNOWN'); + assert.equal(result.status, 'UNKNOWN'); +}); + +test('a clean consumer must have the expected separate axiom audit', () => { + for (const change of [{trace:native.trace.slice(0,2)}, + {axioms:['sorryAx']}, {candidate_axioms:['uncheckedAx']}, + {consumer_name:'Other.consumer'}, {mathlib_git:'0'.repeat(40)}]) { + const changed = structuredClone(receipt); Object.assign(changed.native, change); + const result = assessOmmClaim(args(contract, changed)); + assert.notEqual(result.original_target, 'CONSUMER_ACCEPTED'); + assert.equal(result.evidence_warrant, 'UNKNOWN'); + } +}); + +test('a native consumer without a bound candidate theorem or candidate axiom audit cannot be promoted', () => { + for (const change of [{candidate_declaration_name:'Imported.unrelated'}, + {candidate_axioms:null}, {candidate_axioms:['sorryAx']}]) { + const changed = structuredClone(receipt); Object.assign(changed.native, change); + const result = assessOmmClaim(args(contract, changed)); + assert.notEqual(result.original_target, 'CONSUMER_ACCEPTED'); + assert.equal(result.candidate_dependence, 'UNKNOWN'); + } +}); + +test('OMM method is read-only: fix commands cannot authorize model repair', () => { + const result = assessOmmClaim(args(contract, receipt, {...request,command:'fix'})); + assert.equal(result.status, 'UNKNOWN'); + assert.equal(result.repair_permission, 'DENIED'); +}); diff --git a/integrations/mathgraph-check-github/omm-v1/command.mjs b/integrations/mathgraph-check-github/omm-v1/command.mjs new file mode 100644 index 000000000..c105baa7f --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/command.mjs @@ -0,0 +1,4 @@ +// Exact OMM claim-audit request syntax. Legacy demo -check/-fix own a +// different route, while /fix and /explain are not OMM release capabilities. +export const isOmmClaimCheckCommand=body=> + body==='@MathGraph /check'||body==='@MathGraph -claim'; diff --git a/integrations/mathgraph-check-github/omm-v1/lifecycle-check.mjs b/integrations/mathgraph-check-github/omm-v1/lifecycle-check.mjs new file mode 100644 index 000000000..dac88da42 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/lifecycle-check.mjs @@ -0,0 +1,130 @@ +// First-party OMM App-transport lifecycle probe. This intentionally does NOT +// admit a mathematical claim or execute Lean. It independently tests that a +// registered App's neutral, UNKNOWN observation is bound to one live PR SHA +// and that a predecessor is explicitly superseded after an owned push. +import {readFile} from 'node:fs/promises'; +import {resolve} from 'node:path'; +import {pathToFileURL} from 'node:url'; + +const REPO='metalogiclabs/mathgraph'; +const BRANCH='feature/check-omm-claim-bridge-v1-20261105'; +const NUMBER=270,APP_ID=5308912; +const NAME='MathGraph Check / OMM intake (advisory)'; +const SHA40=/^[a-f0-9]{40}$/; +const RUN_URL=/^https:\/\/github\.com\/metalogiclabs\/mathgraph\/actions\/runs\/[1-9][0-9]*$/; +const assert=(x,reason)=>{if(!x)throw Error(reason);}; +const root='/repos/'+REPO; +const external=head=>`mathgraph-omm-intake-v1:${REPO}:${NUMBER}:${head}`; +const marker=head=>``; + +export function buildOmmIntakeCheck({head,runUrl}={}){ + assert(SHA40.test(head??'')&&RUN_URL.test(runUrl??''),'INVALID_OMM_LIFECYCLE_SOURCE'); + return {name:NAME,head_sha:head,external_id:external(head),status:'completed',conclusion:'neutral', + output:{title:'OMM claim intake: evidence not admitted (UNKNOWN)', + summary:[`Revision: \`${head}\``,`Source: [controlled owned draft PR #270](https://github.com/${REPO}/pull/${NUMBER}).`, + '| Question | Result |','|---|---|', + '| Claim-owner authority | NOT_ADMITTED |','| Native original-target replay | UNKNOWN |', + '| Mathematical intent | UNKNOWN |','| Independent evidence warrant | UNKNOWN |', + '',`[Controlled lifecycle run](${runUrl}). This is an App transport and stale-source control only; no Lean claim was checked, no proof repaired, and no community input was executed.`].join('\n')} + }; +} + +function withdrawnOutput(previous,newHead,runUrl){ + const oldHead=previous.head_sha; + return {title:'OMM intake: previous revision superseded (UNKNOWN)', + summary:[marker(newHead),`SUPERSEDED: the observation for \`${oldHead}\` is withdrawn for the current PR revision \`${newHead}\`.`, + 'It never established a mathematical claim. Do not apply its evidence or conclusion to the newer source.', + `[Withdrawal run](${runUrl}).`,`
Original bounded observation\n\n${previous.output?.summary??'No prior summary'}\n\n
`].join('\n\n').slice(0,60000)}; +} + +export function admitOmmLifecyclePush(event,env={}){ + const ref='refs/heads/'+BRANCH; + assert(env.GITHUB_EVENT_NAME==='push'&&env.GITHUB_REPOSITORY===REPO&& + env.GITHUB_REF===ref&&event?.ref===ref&&event.repository?.full_name===REPO&& + env.GITHUB_ACTOR_ID==='32909488'&&event.sender?.id===32909488&& + SHA40.test(event.before??'')&&SHA40.test(event.after??'')&& + env.GITHUB_SHA===event.after&&/^[1-9][0-9]*$/.test(env.GITHUB_RUN_ID??''), + 'OWNED_EVENT_REQUIRED'); + return {head:event.after,before:event.before,runId:env.GITHUB_RUN_ID}; +} + +/** The App token is limited to checks:write, pull_requests:read on our repo. */ +export async function reconcileOwnedOmmLifecycle({number,head,before,runUrl,api,write=false}={}){ + assert(number===NUMBER&&SHA40.test(head??'')&&SHA40.test(before??'')&& + before!==head&&typeof api==='function'&&RUN_URL.test(runUrl??''),'OWNED_LIFECYCLE_ONLY'); + const live=async()=>{ + const pr=await api(root+'/pulls/'+NUMBER); + assert(pr?.number===NUMBER&&pr.state==='open'&&pr.head?.sha===head&& + pr.head?.ref===BRANCH&&pr.head?.repo?.full_name===REPO&& + pr.base?.repo?.full_name===REPO,'STALE_PR_HEAD'); + }; + const owned=async sha=>{ + const path=root+'/commits/'+sha+'/check-runs?check_name='+encodeURIComponent(NAME)+'&filter=all&per_page=100'; + const d=await api(path); + assert(Array.isArray(d?.check_runs)&&Number.isSafeInteger(d.total_count)&& + d.total_count>=0&&d.total_count<=100&&d.total_count===d.check_runs.length,'CHECK_VISIBILITY_INCOMPLETE'); + const matching=d.check_runs.filter(x=>x.name===NAME); + assert(matching.every(x=>x.app?.id===APP_ID),'FOREIGN_APP_CHECK_NAME_COLLISION'); + assert(matching.every(x=>x.external_id===external(sha)&&x.head_sha===sha&& + Number.isSafeInteger(x.id))&&matching.length<=1,'CHECK_IDENTITY_COLLISION'); + return matching[0]??null; + }; + await live(); + const prior=await owned(before),current=await owned(head); + const payload=buildOmmIntakeCheck({head,runUrl}); + await live(); + if(!write)return {current:{mode:current?'EXISTS':'ABSENT'},previous:{mode:prior?'EXISTS':'ABSENT'},head,before}; + let previous={mode:'ABSENT'}; + if(prior){ + const output=withdrawnOutput(prior,head,runUrl); + const isWithdrawn=prior.output?.summary?.includes(marker(head))&& + prior.status==='completed'&&prior.conclusion==='neutral'; + let patched=prior; + if(!isWithdrawn){ + patched=await api(root+'/check-runs/'+prior.id,{method:'PATCH',body:{status:'completed', + conclusion:'neutral',external_id:external(before),output}}); + } + const reread=await api(root+'/check-runs/'+prior.id); + assert(patched?.id===prior.id&&reread?.id===prior.id&&reread.app?.id===APP_ID&& + reread.head_sha===before&&reread.name===NAME&&reread.external_id===external(before)&& + reread.status==='completed'&&reread.conclusion==='neutral'&& + reread.output?.summary?.includes(marker(head)),'APP_WITHDRAWAL_READBACK_MISMATCH'); + previous={mode:isWithdrawn?'ALREADY_WITHDRAWN':'WITHDRAWN',check_run_id:prior.id,head:before}; + } + await live(); + let result,mode; + if(current){ + if(current.status==='completed'&¤t.conclusion==='neutral'&& + current.output?.summary===payload.output.summary){result=current;mode='UNCHANGED';} + else{result=await api(root+'/check-runs/'+current.id,{method:'PATCH',body:{status:'completed', + conclusion:'neutral',external_id:external(head),output:payload.output}});mode='UPDATED';} + }else{result=await api(root+'/check-runs',{method:'POST',body:payload});mode='CREATED';} + assert(Number.isSafeInteger(result?.id),'APP_CHECK_WRITE_UNCONFIRMED'); + const reread=await api(root+'/check-runs/'+result.id); + assert(reread?.app?.id===APP_ID&&reread.name===NAME&&reread.head_sha===head&& + reread.external_id===external(head)&&reread.status==='completed'&& + reread.conclusion==='neutral'&&reread.output?.summary===payload.output.summary, + 'APP_CHECK_READBACK_MISMATCH'); + await live(); + return {current:{mode,check_run_id:result.id,head},previous,app_id:APP_ID,number,repository:REPO}; +} + +if(process.argv[1]&&import.meta.url===pathToFileURL(resolve(process.argv[1])).href){ + try{ + const event=JSON.parse(await readFile(process.env.GITHUB_EVENT_PATH,'utf8')); + const {head,before,runId}=admitOmmLifecyclePush(event,process.env); + const token=process.env.MATHGRAPH_APP_INSTALLATION_TOKEN; + assert(process.argv.length===2&&typeof token==='string'&&token.length>10,'APP_CONTEXT_MISSING'); + const api=async(path,{method='GET',body}={})=>{ + assert(path.startsWith(root+'/'),'APP_API_SURFACE_REFUSED'); + const response=await fetch('https://api.github.com'+path,{method,redirect:'error', + headers:{Accept:'application/vnd.github+json','X-GitHub-Api-Version':'2022-11-28', + Authorization:'Bearer '+token,'Content-Type':'application/json'}, + body:body===undefined?undefined:JSON.stringify(body),signal:AbortSignal.timeout(20000)}); + assert(response.ok,'GITHUB_APP_HTTP_'+response.status);return response.json(); + }; + const result=await reconcileOwnedOmmLifecycle({number:NUMBER,head,before, + runUrl:`https://github.com/${REPO}/actions/runs/${runId}`,api,write:true}); + console.log('OMM_OWNED_LIFECYCLE_READBACK '+JSON.stringify(result)); + }catch(e){console.error('OMM_OWNED_LIFECYCLE_UNKNOWN:'+String(e.message));process.exitCode=1;} +} diff --git a/integrations/mathgraph-check-github/omm-v1/lifecycle-check.test.mjs b/integrations/mathgraph-check-github/omm-v1/lifecycle-check.test.mjs new file mode 100644 index 000000000..84ff107fd --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/lifecycle-check.test.mjs @@ -0,0 +1,107 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { + buildOmmIntakeCheck, reconcileOwnedOmmLifecycle, admitOmmLifecyclePush +} from './lifecycle-check.mjs'; + +const repo='metalogiclabs/mathgraph'; +const branch='feature/check-omm-claim-bridge-v1-20261105'; +const a='a'.repeat(40),b='b'.repeat(40),c='c'.repeat(40); +const name='MathGraph Check / OMM intake (advisory)'; + +function stateApi({live=a,foreign=false,moveAt=Infinity,full=true}={}){ + const state={live,checks:new Map(),calls:[],next:701,prReads:0}; + const root='/repos/'+repo; + const api=async(path,{method='GET',body}={})=>{ + state.calls.push({path,method,body}); + if(path===root+'/pulls/270'){ + state.prReads++; + if(state.prReads>=moveAt)state.live=c; + return {number:270,state:'open',head:{sha:state.live,ref:branch,repo:{full_name:repo}},base:{repo:{full_name:repo}}}; + } + const found=path.match(/\/commits\/([a-f0-9]{40})\/check-runs\?/); + if(found){ + const list=state.checks.get(found[1])??[]; + if(foreign&&found[1]===state.live)return {total_count:1,check_runs:[{...list[0],id:800,app:{id:111},name,external_id:`mathgraph-omm-intake-v1:${repo}:270:${state.live}`}]}; + return {total_count:full?list.length:list.length+1,check_runs:list}; + } + if(path===root+'/check-runs'&&method==='POST'){ + const r={...body,id:state.next++,app:{id:5308912}}; + state.checks.set(body.head_sha,[...(state.checks.get(body.head_sha)??[]),r]);return r; + } + const checkId=path.match(/\/check-runs\/(\d+)$/); + if(checkId){ + const entries=[...state.checks.values()].flat();const check=entries.find(x=>x.id===Number(checkId[1])); + if(!check)throw Error('CHECK_NOT_FOUND'); + if(method==='PATCH'){Object.assign(check,body);return check;} + return check; + } + throw Error('UNEXPECTED_API_'+method+':'+path); + }; + return {state,api}; +} + +const runUrl='https://github.com/metalogiclabs/mathgraph/actions/runs/41916425178'; + +test('owned exact-head UNKNOWN check is created; head change explicitly supersedes old check',async()=>{ + const {state,api}=stateApi(); + const first=await reconcileOwnedOmmLifecycle({number:270,head:a,before:c,runUrl,api,write:true}); + assert.equal(first.current.mode,'CREATED'); + assert.equal(first.previous.mode,'ABSENT'); + assert.equal(state.checks.get(a)[0].app.id,5308912); + assert.equal(state.checks.get(a)[0].conclusion,'neutral'); + assert.match(state.checks.get(a)[0].output.summary,/Mathematical intent \| UNKNOWN/); + state.live=b; + const second=await reconcileOwnedOmmLifecycle({number:270,head:b,before:a,runUrl,api,write:true}); + assert.equal(second.previous.mode,'WITHDRAWN'); + assert.equal(second.current.mode,'CREATED'); + assert.match(state.checks.get(a)[0].output.summary,/SUPERSEDED/); + assert.match(state.checks.get(a)[0].output.summary,new RegExp(b)); + assert.equal(state.checks.get(b)[0].conclusion,'neutral'); + const rerun=await reconcileOwnedOmmLifecycle({number:270,head:b,before:a,runUrl,api,write:true}); + assert.equal(rerun.previous.mode,'ALREADY_WITHDRAWN'); + assert.equal(rerun.current.mode,'UNCHANGED'); + assert.equal(state.checks.get(b).length,1); +}); + +test('changed live PR refuses to publish a Check for an obsolete SHA',async()=>{ + const {state,api}=stateApi({live:b}); + await assert.rejects(reconcileOwnedOmmLifecycle({number:270,head:a,before:c,runUrl,api,write:true}),/STALE_PR_HEAD/); + assert.equal(state.calls.some(x=>x.method!=='GET'),false); +}); + +test('a foreign App with the same Check name cannot be overwritten',async()=>{ + const {state,api}=stateApi({foreign:true}); + await assert.rejects(reconcileOwnedOmmLifecycle({number:270,head:a,before:c,runUrl,api,write:true}),/FOREIGN_APP_CHECK_NAME_COLLISION/); + assert.equal(state.calls.some(x=>x.method!=='GET'),false); +}); + +test('incomplete check-list visibility refuses publication',async()=>{ + const {state,api}=stateApi({full:false}); + await assert.rejects(reconcileOwnedOmmLifecycle({number:270,head:a,before:c,runUrl,api,write:true}),/CHECK_VISIBILITY_INCOMPLETE/); + assert.equal(state.calls.some(x=>x.method!=='GET'),false); +}); + +test('head moving after preflight cannot receive a new Check',async()=>{ + const {state,api}=stateApi({moveAt:2}); + await assert.rejects(reconcileOwnedOmmLifecycle({number:270,head:a,before:c,runUrl,api,write:true}),/STALE_PR_HEAD/); + assert.equal(state.calls.some(x=>x.method==='POST'),false); +}); + +test('wrong owned PR and attempts to encode a passing claim are rejected',async()=>{ + const {api}=stateApi(); + await assert.rejects(reconcileOwnedOmmLifecycle({number:239,head:a,before:c,runUrl,api,write:true}),/OWNED_LIFECYCLE_ONLY/); + const p=buildOmmIntakeCheck({head:a,runUrl}); + assert.equal(p.status,'completed');assert.equal(p.conclusion,'neutral'); + assert.equal(p.external_id,`mathgraph-omm-intake-v1:${repo}:270:${a}`); + assert.doesNotMatch(p.output.summary,/WARRANTED/); +}); + +test('CLI event admission requires exact owned push source and actor',()=>{ + const event={ref:'refs/heads/'+branch,before:a,after:b,repository:{full_name:repo},sender:{id:32909488}}; + const env={GITHUB_EVENT_NAME:'push',GITHUB_REPOSITORY:repo,GITHUB_REF:event.ref,GITHUB_SHA:b,GITHUB_ACTOR_ID:'32909488',GITHUB_RUN_ID:'41916425178'}; + assert.deepEqual(admitOmmLifecyclePush(event,env),{head:b,before:a,runId:'41916425178'}); + assert.throws(()=>admitOmmLifecyclePush({...event,sender:{id:999}},env),/OWNED_EVENT_REQUIRED/); + assert.throws(()=>admitOmmLifecyclePush(event,{...env,GITHUB_ACTOR_ID:'999'}),/OWNED_EVENT_REQUIRED/); + assert.throws(()=>admitOmmLifecyclePush(event,{...env,GITHUB_SHA:c}),/OWNED_EVENT_REQUIRED/); +}); diff --git a/integrations/mathgraph-check-github/omm-v1/optin-admission.mjs b/integrations/mathgraph-check-github/omm-v1/optin-admission.mjs new file mode 100644 index 000000000..ee79838a4 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/optin-admission.mjs @@ -0,0 +1,55 @@ +// An operator-only, non-publishing OMM opt-in source gate. It authenticates +// the human request and source boundary before any native work. A command +// cannot supply its own theorem, native evidence, model prompt or proof. +// No live issue_comment workflow is activated by this module alone. +import {checkPublishedArtinInput} from './artin-preflight.mjs'; +import {isOmmClaimCheckCommand} from './command.mjs'; + +const REPO='metalogiclabs/mathgraph',USER_ID=32909488; +const SHA40=/^[a-f0-9]{40}$/; +const root='/repos/'+REPO; +const assert=(ok,error)=>{if(!ok)throw Error(error);}; + +export function parseOwnedOmmOptin(event,env={}){ + if(env.GITHUB_EVENT_NAME!=='issue_comment'||env.GITHUB_REPOSITORY!==REPO|| + env.GITHUB_RUN_ATTEMPT!=='1'||event?.action!=='created'|| + event.repository?.full_name!==REPO||event.sender?.id!==USER_ID|| + event.sender?.type!=='User'||event.comment?.user?.id!==USER_ID|| + event.comment.user?.type!=='User'||!isOmmClaimCheckCommand(event.comment?.body)|| + !Number.isSafeInteger(event.comment?.id)||event.comment.id<1|| + !event.issue?.pull_request||!Number.isSafeInteger(event.issue?.number)||event.issue.number<1)return null; + return {number:event.issue.number,commentId:event.comment.id}; +} + +/** Admit identity only. A full native claim receipt and independent intent remain UNKNOWN. */ +export async function admitOwnedOmmSource({event,env,api,qualifyRegistered=checkPublishedArtinInput}={}){ + const input=parseOwnedOmmOptin(event,env); + assert(input&&typeof api==='function'&&typeof qualifyRegistered==='function','UNQUALIFIED_OMM_OPTIN'); + const comment=await api(root+'/issues/comments/'+input.commentId); + assert(comment?.id===input.commentId&&comment.body===event.comment.body&& + comment.user?.id===USER_ID&&comment.issue_url==='https://api.github.com'+root+'/issues/'+input.number&& + typeof comment.created_at==='string'&&comment.created_at===event.comment.created_at&& + comment.updated_at===event.comment.updated_at,'COMMENT_IDENTITY_CHANGED'); + const original=await api(root+'/pulls/'+input.number); + const sourceOkay=p=>p?.number===input.number&&p.state==='open'&& + SHA40.test(p.head?.sha??'')&&SHA40.test(p.base?.sha??'')&& + p.head.repo?.full_name===REPO&&p.base.repo?.full_name===REPO; + assert(sourceOkay(original),'FIRST_PARTY_SOURCE_REQUIRED'); + const registration=await qualifyRegistered({number:input.number}); + const contract=registration?.request; + assert(contract?.schema==='mathgraph.omm.request.v1'&&contract.repository===REPO&& + contract.number===input.number&&contract.command==='check'&& + contract.head_sha===original.head.sha&&contract.base_sha===original.base.sha&& + /^[a-f0-9]{64}$/.test(contract.candidate_source_sha256??''), + 'QUALIFIED_SOURCE_IDENTITY_MISMATCH'); + const current=await api(root+'/pulls/'+input.number); + assert(sourceOkay(current)&¤t.head.sha===original.head.sha&& + current.base.sha===original.base.sha,'SOURCE_HEAD_MOVED'); + return {schema:'mathgraph.omm.optin-admission.v1',status:'SOURCE_ADMITTED_ONLY', + repository:REPO,number:input.number,comment_id:input.commentId, + head_sha:contract.head_sha,base_sha:contract.base_sha, + source_sha256:contract.candidate_source_sha256, + native_receipt:'UNKNOWN',original_target:'UNKNOWN', + intent_fidelity:'UNKNOWN',evidence_warrant:'UNKNOWN', + repair_permission:'DENIED',publication_permission:'DENIED'}; +} diff --git a/integrations/mathgraph-check-github/omm-v1/optin-admission.test.mjs b/integrations/mathgraph-check-github/omm-v1/optin-admission.test.mjs new file mode 100644 index 000000000..722abeaca --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/optin-admission.test.mjs @@ -0,0 +1,101 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {parseOwnedOmmOptin,admitOwnedOmmSource} from './optin-admission.mjs'; + +const repo='metalogiclabs/mathgraph'; +const head='2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17'; +const base='dfd87d1e921953badedcfae21ade76bb38f5c04a'; +const userId=32909488; +const command='@MathGraph -claim'; +const event=()=>({action:'created',repository:{full_name:repo},sender:{id:userId,type:'User'}, + comment:{id:555,body:command,user:{id:userId,type:'User'},created_at:'2026-11-05T00:00:00Z',updated_at:'2026-11-05T00:00:00Z'}, + issue:{number:239,pull_request:{url:'https://api.github.com/repos/'+repo+'/pulls/239'}}}); +const env=()=>({GITHUB_EVENT_NAME:'issue_comment',GITHUB_REPOSITORY:repo,GITHUB_RUN_ATTEMPT:'1'}); +const qualified=()=>({request:{schema:'mathgraph.omm.request.v1',repository:repo,number:239,command:'check',head_sha:head, + base_sha:base,candidate_source_sha256:'e45efb73729fc98e45e66d216e792e70f67f8c43d4c12cbb753ac410b950e4d9'}}); + +function client({moved=false,edited=false,foreign=false,body=command}={}){ + const calls=[],state={count:0}; + const api=async path=>{ + calls.push(path); + if(path==='/repos/'+repo+'/issues/comments/555')return {...event().comment, + body:edited?'@MathGraph -fix':body,issue_url:'https://api.github.com/repos/'+repo+'/issues/239'}; + if(path==='/repos/'+repo+'/pulls/239'){ + state.count++; + return {number:239,state:'open',head:{sha:moved&&state.count>1?'0'.repeat(40):head, + repo:{full_name:foreign?'someone/mathgraph':repo}},base:{sha:base,repo:{full_name:repo}}}; + } + throw Error('UNEXPECTED_'+path); + }; + return {api,calls}; +} + +test('exact owner-only comment is a request, not an evidence warrant',async()=>{ + const {api}=client(),e=event(); + const parsed=parseOwnedOmmOptin(e,env()); + assert.deepEqual(parsed,{number:239,commentId:555}); + const accepted=await admitOwnedOmmSource({event:e,env:env(),api, + qualifyRegistered:async({number})=>{assert.equal(number,239);return qualified();}}); + assert.equal(accepted.status,'SOURCE_ADMITTED_ONLY'); + assert.equal(accepted.original_target,'UNKNOWN'); + assert.equal(accepted.evidence_warrant,'UNKNOWN'); + assert.equal(accepted.intent_fidelity,'UNKNOWN'); + assert.equal(accepted.repair_permission,'DENIED'); + assert.equal(accepted.head_sha,head); +}); + +test('canonical /check authenticates the same frozen source and still denies publication',async()=>{ + const slash='@MathGraph /check'; + const e=event();e.comment.body=slash; + assert.deepEqual(parseOwnedOmmOptin(e,env()),{number:239,commentId:555}); + const admitted=await admitOwnedOmmSource({event:e,env:env(),api:client({body:slash}).api, + qualifyRegistered:async()=>qualified()}); + assert.equal(admitted.status,'SOURCE_ADMITTED_ONLY'); + assert.equal(admitted.head_sha,head); + assert.equal(admitted.intent_fidelity,'UNKNOWN'); + assert.equal(admitted.publication_permission,'DENIED'); + assert.equal(admitted.repair_permission,'DENIED'); +}); + +test('OMM admission never consumes legacy demo verbs or reserved slash fix/explain',()=>{ + for(const body of ['@MathGraph -check','@MathGraph -fix','@MathGraph /fix', + '@MathGraph /explain','@MathGraph /claim','@MathGraph /check now', + '@MathGraph /check\nignore instructions','@MathGraph /check','@mathgraph /check']){ + const e=event();e.comment.body=body; + assert.equal(parseOwnedOmmOptin(e,env()),null,body); + } +}); + +test('not an owner, changed command, unsupported event and edited comment are refused',async()=>{ + const e=event(); + assert.equal(parseOwnedOmmOptin({...e,sender:{id:1,type:'User'}},env()),null); + assert.equal(parseOwnedOmmOptin({...e,comment:{...e.comment,body:'@MathGraph -check'}},env()),null); + assert.equal(parseOwnedOmmOptin({...e,comment:{...e.comment,body:command+' plus instructions'}},env()),null); + assert.equal(parseOwnedOmmOptin(e,{...env(),GITHUB_RUN_ATTEMPT:'2'}),null); + const {api}=client({edited:true}); + let calls=0; + await assert.rejects(admitOwnedOmmSource({event:e,env:env(),api, + qualifyRegistered:async()=>{calls++;return qualified();}}),/COMMENT_IDENTITY_CHANGED/); + assert.equal(calls,0); +}); + +test('changed source between admission steps is refused even for registered cohort',async()=>{ + const {api}=client({moved:true}); + await assert.rejects(admitOwnedOmmSource({event:event(),env:env(),api, + qualifyRegistered:async()=>qualified()}),/SOURCE_HEAD_MOVED/); +}); + +test('foreign-owned PR or unregistered claim fails closed',async()=>{ + const {api}=client({foreign:true}); + await assert.rejects(admitOwnedOmmSource({event:event(),env:env(),api, + qualifyRegistered:async()=>qualified()}),/FIRST_PARTY_SOURCE_REQUIRED/); + const normal=client(); + await assert.rejects(admitOwnedOmmSource({event:event(),env:env(),api:normal.api, + qualifyRegistered:async()=>{throw Error('UNREGISTERED_ARTIN_PR');}}),/UNREGISTERED_ARTIN_PR/); +}); + +test('a forged qualification response cannot bind a different claim or base',async()=>{ + const {api}=client(); + await assert.rejects(admitOwnedOmmSource({event:event(),env:env(),api, + qualifyRegistered:async()=>({request:{...qualified().request,number:240}})}),/QUALIFIED_SOURCE_IDENTITY_MISMATCH/); +}); diff --git a/integrations/mathgraph-check-github/omm-v1/owned-comment-cli.mjs b/integrations/mathgraph-check-github/omm-v1/owned-comment-cli.mjs new file mode 100644 index 000000000..06759cb33 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/owned-comment-cli.mjs @@ -0,0 +1,134 @@ +// Controlled Actions bridge: a real owned comment is re-read before native +// execution and again before/after Check publication. No public event listener. +import {createHash} from 'node:crypto'; +import {mkdir,readFile,writeFile} from 'node:fs/promises'; +import {join,resolve} from 'node:path'; +import {pathToFileURL} from 'node:url'; +import {checkPublishedArtinInput} from './artin-preflight.mjs'; +import {admitOwnedComment,verifyOwnedAdmission} from './owned-comment.mjs'; +import {loadFrozenArtinPreview} from './app-owned-publish.mjs'; +import {buildOwnedCommentCheck,reconcileOwnedCommentCheck} from './owned-comment-publish.mjs'; + +const REPO='metalogiclabs/mathgraph',NUMBER=239,OWNER_ID=32909488,COMMENT_ID=6645637811; +const BRANCH='refs/heads/feature/check-omm-claim-bridge-v1-20261105'; +const SOURCE_SHA='e45efb73729fc98e45e66d216e792e70f67f8c43d4c12cbb753ac410b950e4d9'; +const sha=s=>createHash('sha256').update(s).digest('hex'); +const assert=(ok,reason)=>{if(!ok)throw Error(reason);}; + +/** Explicit HTTP method + least-privilege path boundary for our App publisher. */ +export function makeOwnedGitHubApi({token,commentId=COMMENT_ID,allowWrite=false,fetcher=globalThis.fetch}={}){ + assert(typeof token==='string'&&token.length>10&&typeof fetcher==='function', + 'OWNED_AUTHENTICATED_READ_CONTEXT_REQUIRED'); + assert(Number.isSafeInteger(commentId)&&commentId>0,'OWNED_AUTHENTICATED_READ_CONTEXT_REQUIRED'); + const root=`/repos/${REPO}`; + const read=path=>path===root+'/issues/comments/'+commentId|| + path===root+'/pulls/'+NUMBER|| + path.startsWith(root+`/commits/2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17/check-runs?check_name=`)|| + new RegExp('^'+root+'/check-runs/[1-9][0-9]*$').test(path); + const writable=(method,path)=>allowWrite&& + (method==='POST'&&path===root+'/check-runs'|| + method==='PATCH'&&new RegExp('^'+root+'/check-runs/[1-9][0-9]*$').test(path)); + return async(path,{method='GET',body}={})=>{ + assert(typeof path==='string'&&path.startsWith(root+'/')&& + (method==='GET'&&body===undefined&&read(path)||writable(method,path)), + 'OWNED_API_SURFACE_REFUSED'); + const response=await fetcher('https://api.github.com'+path,{method,redirect:'error', + headers:{Accept:'application/vnd.github+json','X-GitHub-Api-Version':'2022-11-28', + Authorization:'Bearer '+token,'User-Agent':'MathGraph-OMM-Owned-Claim', + 'Content-Type':'application/json'}, + body:body===undefined?undefined:JSON.stringify(body),signal:AbortSignal.timeout(20000)}); + assert(response.ok,'OWNED_GITHUB_'+method+'_'+response.status); + return response.json(); + }; +} + +export async function prepareOwnedCommentInputs({directory,number,commentId,api, + qualifyRegistered,commandBody}={}){ + assert(typeof directory==='string'&&directory.startsWith('/')&& + number===NUMBER&&Number.isSafeInteger(commentId)&&commentId>0&& + typeof qualifyRegistered==='function'&& + (commandBody===undefined||commandBody==='@MathGraph /check'), + 'OWNED_SOURCE_ADMISSION_CONTEXT'); + const bundle=await qualifyRegistered({number}); + const admission=await admitOwnedComment({number,commentId,api,commandBody, + qualifyRegistered:async()=>bundle}); + assert(bundle.request.candidate_source_sha256===SOURCE_SHA&& + typeof bundle.source==='string'&&sha(bundle.source)===SOURCE_SHA&& + typeof bundle.publishedSource==='string'&&bundle.publishedSource.length>10, + 'OWNED_SOURCE_BYTES_MISMATCH'); + const dir=resolve(directory); + await mkdir(dir,{recursive:false}); + await Promise.all([ + writeFile(join(dir,'admission.json'),JSON.stringify(admission,null,2)+'\n',{flag:'wx'}), + writeFile(join(dir,'request.json'),JSON.stringify(bundle.request,null,2)+'\n',{flag:'wx'}), + writeFile(join(dir,'candidate.lean.txt'),bundle.source,{flag:'wx'}), + writeFile(join(dir,'published-exercise.lean.txt'),bundle.publishedSource,{flag:'wx'}) + ]); + return admission; +} + +/** Bind a separately requalified native receipt to the authenticated request. */ +export function checkOwnedNativeBinding({admission,request,assessment, + expectedCommentId=COMMENT_ID}={}){ + const valid=admission?.schema==='mathgraph.omm.owned-comment-admission.v1'&& + admission.status==='SOURCE_ADMITTED_ONLY'&&admission.repository===REPO&& + admission.number===NUMBER&&admission.comment_id===expectedCommentId&& + admission.head_sha===request?.head_sha&&admission.base_sha===request?.base_sha&& + admission.source_sha256===request?.candidate_source_sha256&& + admission.native_receipt==='UNKNOWN'&&admission.original_target==='UNKNOWN'&& + admission.intent_fidelity==='UNKNOWN'&&admission.evidence_warrant==='UNKNOWN'&& + admission.repair_permission==='DENIED'&&admission.publication_permission==='DENIED'&& + assessment?.schema==='mathgraph.omm.claim-assessment.v1'&& + assessment.status==='NATIVE_RECEIPT_BOUND'&& + assessment.repository===admission.repository&&assessment.number===NUMBER&& + assessment.head_sha===request.head_sha&&assessment.base_sha===request.base_sha&& + assessment.candidate_sha256===request.candidate_source_sha256&& + assessment.candidate_proof==='LEAN_COMPILED'&& + assessment.original_target==='NOT_ESTABLISHED_IN_THIS_REPLAY'&& + assessment.formal_statement_relation==='UNKNOWN'&& + assessment.intent_fidelity==='UNKNOWN'&&assessment.evidence_warrant==='UNKNOWN'&& + assessment.candidate_dependence==='UNKNOWN'; + assert(valid,'OWNED_NATIVE_BOUNDARY_MISMATCH'); + return true; +} + +export async function publishOwnedComment({directory,runId,api,qualifyRegistered}={}){ + assert(typeof directory==='string'&&directory.startsWith('/')&& + /^[1-9][0-9]*$/.test(String(runId??'')),'OWNED_PUBLISH_CONTEXT_REQUIRED'); + const admission=JSON.parse(await readFile(join(resolve(directory),'admission.json'),'utf8')); + const verifyAdmission=async()=>verifyOwnedAdmission(admission,{api,qualifyRegistered}); + await verifyAdmission(); + const {request,assessment}=await loadFrozenArtinPreview({directory,number:NUMBER,runId}); + checkOwnedNativeBinding({admission,request,assessment}); + const runUrl=`https://github.com/${REPO}/actions/runs/${runId}`; + buildOwnedCommentCheck({admission,assessment,runUrl}); + const result=await reconcileOwnedCommentCheck({admission,assessment,runUrl,api,verifyAdmission,write:true}); + await writeFile(join(resolve(directory),'output','owned-optin-app-receipt.json'), + JSON.stringify(result,null,2)+'\n',{flag:'wx'}); + return result; +} + +if(process.argv[1]&&import.meta.url===pathToFileURL(resolve(process.argv[1])).href){ + try{ + const mode=process.argv[2],directory=process.argv[3]; + assert(process.argv.length===4&&['preflight','publish'].includes(mode)&& + directory?.startsWith('/')&&process.env.GITHUB_REPOSITORY===REPO&& + process.env.GITHUB_EVENT_NAME==='push'&&process.env.GITHUB_REF===BRANCH&& + process.env.GITHUB_ACTOR_ID===String(OWNER_ID)&& + /^[1-9][0-9]*$/.test(process.env.GITHUB_RUN_ID??''), + 'OWNED_QUALIFICATION_WORKFLOW_CONTEXT_REQUIRED'); + const token=mode==='preflight'?process.env.GITHUB_TOKEN: + process.env.MATHGRAPH_APP_INSTALLATION_TOKEN; + assert(typeof token==='string'&&token.length>10,'OWNED_AUTHENTICATED_READ_CONTEXT_REQUIRED'); + const api=makeOwnedGitHubApi({token,allowWrite:mode==='publish'}); + const authorizedFetch=(url,opts)=>fetch(url,{...opts,redirect:'error', + headers:{...opts?.headers, + ...(url.startsWith(`https://api.github.com/repos/${REPO}/`)?{Authorization:'Bearer '+token}:{})}, + signal:AbortSignal.timeout(20000)}); + const qualifyRegistered=({number})=>checkPublishedArtinInput({number,fetch:authorizedFetch}); + const result=mode==='preflight'? + await prepareOwnedCommentInputs({directory,number:NUMBER,commentId:COMMENT_ID,api,qualifyRegistered}): + await publishOwnedComment({directory,runId:process.env.GITHUB_RUN_ID,api,qualifyRegistered}); + console.log('OMM_OWNED_OPTIN_'+mode.toUpperCase()+' '+JSON.stringify(result)); + }catch(e){console.error('OMM_OWNED_OPTIN_UNKNOWN:'+String(e.message));process.exitCode=1;} +} diff --git a/integrations/mathgraph-check-github/omm-v1/owned-comment-cli.test.mjs b/integrations/mathgraph-check-github/omm-v1/owned-comment-cli.test.mjs new file mode 100644 index 000000000..0974a88ca --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/owned-comment-cli.test.mjs @@ -0,0 +1,102 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {mkdtemp,readFile,rm} from 'node:fs/promises'; +import {tmpdir} from 'node:os'; +import {join} from 'node:path'; +import {artinStrongSource} from '../mention-artin-control-v1.mjs'; + +const repo='metalogiclabs/mathgraph',number=239,commentId=6645637811,owner=32909488; +const head='2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17'; +const base='dfd87d1e921953badedcfae21ade76bb38f5c04a'; +const source='e45efb73729fc98e45e66d216e792e70f67f8c43d4c12cbb753ac410b950e4d9'; +const timestamp='2026-11-05T00:00:20Z'; +const qualified=()=>({request:{schema:'mathgraph.omm.request.v1',repository:repo,number,command:'check', + head_sha:head,base_sha:base,candidate_source_sha256:source}, + source:artinStrongSource,publishedSource:'published verified Artin exercise'}); +function api(path){ + if(path===`/repos/${repo}/issues/comments/${commentId}`) + return {id:commentId,body:'@MathGraph -claim',user:{id:owner,type:'User'}, + created_at:timestamp,updated_at:timestamp,issue_url:`https://api.github.com/repos/${repo}/issues/${number}`}; + if(path===`/repos/${repo}/pulls/${number}`) + return {number,state:'open',user:{id:owner,type:'User'}, + head:{sha:head,repo:{full_name:repo}},base:{sha:base,repo:{full_name:repo}}}; + throw Error('UNEXPECTED_'+path); +} +const subject=()=>import('./owned-comment-cli.mjs'); + +test('App Check creation uses a real HTTP POST with JSON rather than GET on the checks collection',async()=>{ + const {makeOwnedGitHubApi}=await subject(); + const requests=[]; + const api=makeOwnedGitHubApi({token:'test-token-with-no-real-access',allowWrite:true, + fetcher:async(url,opts)=>{requests.push({url,opts});return {ok:true,status:201, + json:async()=>({id:991})};}}); + const p={name:'MathGraph Check / OMM owner opt-in (advisory)',conclusion:'neutral'}; + assert.deepEqual(await api(`/repos/${repo}/check-runs`,{method:'POST',body:p}),{id:991}); + assert.equal(requests.length,1); + assert.equal(requests[0].url,`https://api.github.com/repos/${repo}/check-runs`); + assert.equal(requests[0].opts.method,'POST'); + assert.equal(requests[0].opts.body,JSON.stringify(p)); + assert.equal(requests[0].opts.headers['Content-Type'],'application/json'); + await api(`/repos/${repo}/check-runs/991`,{method:'PATCH',body:{conclusion:'neutral'}}); + assert.equal(requests[1].opts.method,'PATCH'); + await api(`/repos/${repo}/pulls/239`); + assert.equal(requests[2].opts.method,'GET'); +}); + +test('preflight API cannot write and publisher cannot mutate any path except exact Check endpoints',async()=>{ + const {makeOwnedGitHubApi}=await subject(); + let calls=0; + const fetcher=async()=>{calls++;return {ok:true,json:async()=>({})};}; + const read=makeOwnedGitHubApi({token:'test-token-with-no-real-access',fetcher}); + await assert.rejects(read(`/repos/${repo}/check-runs`,{method:'POST',body:{}}),/OWNED_API_SURFACE_REFUSED/); + const write=makeOwnedGitHubApi({token:'test-token-with-no-real-access',fetcher,allowWrite:true}); + await assert.rejects(write(`/repos/${repo}/issues/comments/1`,{method:'POST',body:{}}),/OWNED_API_SURFACE_REFUSED/); + await assert.rejects(write('/repos/foreign/repo/check-runs',{method:'POST',body:{}}),/OWNED_API_SURFACE_REFUSED/); + assert.equal(calls,0); +}); + +test('real source admission writes a run-local admission and actual fixed Lean source',async()=>{ + const {prepareOwnedCommentInputs}=await subject(); + const root=await mkdtemp(join(tmpdir(),'omm-owned-comment-')); + const dir=join(root,'artin'); + try{ + const admission=await prepareOwnedCommentInputs({directory:dir,number,commentId, + api:async path=>api(path),qualifyRegistered:async()=>qualified()}); + assert.equal(admission.comment_id,commentId); + assert.equal(admission.status,'SOURCE_ADMITTED_ONLY'); + assert.deepEqual(JSON.parse(await readFile(join(dir,'admission.json'),'utf8')),admission); + assert.deepEqual(JSON.parse(await readFile(join(dir,'request.json'),'utf8')),qualified().request); + assert.equal(await readFile(join(dir,'candidate.lean.txt'),'utf8'),artinStrongSource); + assert.equal(await readFile(join(dir,'published-exercise.lean.txt'),'utf8'),qualified().publishedSource); + }finally{await rm(root,{recursive:true,force:true});} +}); + +test('preflight refuses a qualified manifest that does not match candidate source bytes',async()=>{ + const {prepareOwnedCommentInputs}=await subject(); + const root=await mkdtemp(join(tmpdir(),'omm-owned-comment-')); + try{await assert.rejects(prepareOwnedCommentInputs({directory:join(root,'artin'),number,commentId, + api:async path=>api(path),qualifyRegistered:async()=>({...qualified(),source:'wrong source'})}), + /OWNED_SOURCE_BYTES_MISMATCH/); + }finally{await rm(root,{recursive:true,force:true});} +}); + +test('artifact binding requires exact admission, registered PR and native assessment',async()=>{ + const {checkOwnedNativeBinding}=await subject(); + const admission={schema:'mathgraph.omm.owned-comment-admission.v1',status:'SOURCE_ADMITTED_ONLY', + repository:repo,number,comment_id:commentId,head_sha:head,base_sha:base,source_sha256:source, + comment_created_at:timestamp,comment_updated_at:timestamp, + native_receipt:'UNKNOWN',original_target:'UNKNOWN',intent_fidelity:'UNKNOWN', + evidence_warrant:'UNKNOWN',repair_permission:'DENIED',publication_permission:'DENIED'}; + const assessment={schema:'mathgraph.omm.claim-assessment.v1',status:'NATIVE_RECEIPT_BOUND', + repository:repo,number,head_sha:head,base_sha:base,candidate_sha256:source, + candidate_proof:'LEAN_COMPILED',original_target:'NOT_ESTABLISHED_IN_THIS_REPLAY', + formal_statement_relation:'UNKNOWN',intent_fidelity:'UNKNOWN',evidence_warrant:'UNKNOWN', + candidate_dependence:'UNKNOWN',native_receipt_sha256:'a'.repeat(64)}; + assert.equal(checkOwnedNativeBinding({admission,request:qualified().request,assessment}),true); + for(const request of [{...qualified().request,base_sha:'0'.repeat(40)}, + {...qualified().request,candidate_source_sha256:'f'.repeat(64)}]) + assert.throws(()=>checkOwnedNativeBinding({admission,request,assessment}),/OWNED_NATIVE_BOUNDARY_MISMATCH/); + for(const modified of [{original_target:'CONSUMER_ACCEPTED'},{evidence_warrant:'WARRANTED'}]) + assert.throws(()=>checkOwnedNativeBinding({admission,request:qualified().request, + assessment:{...assessment,...modified}}),/OWNED_NATIVE_BOUNDARY_MISMATCH/); +}); diff --git a/integrations/mathgraph-check-github/omm-v1/owned-comment-publish.mjs b/integrations/mathgraph-check-github/omm-v1/owned-comment-publish.mjs new file mode 100644 index 000000000..9cea39401 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/owned-comment-publish.mjs @@ -0,0 +1,113 @@ +// An independently requalified, *first-party* neutral App Check for one real +// owner comment on frozen PR239. Not a production issue_comment listener. +const REPO='metalogiclabs/mathgraph',NUMBER=239,OWNER=32909488,APP_ID=5308912; +const HEAD='2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17'; +const BASE='dfd87d1e921953badedcfae21ade76bb38f5c04a'; +const SOURCE='e45efb73729fc98e45e66d216e792e70f67f8c43d4c12cbb753ac410b950e4d9'; +const NAME='MathGraph Check / OMM owner opt-in (advisory)'; +const SLASH_NAME='MathGraph Check / OMM /check (advisory)'; +const SHA256=/^[a-f0-9]{64}$/; +const root='/repos/'+REPO; +const fail=(ok)=>{if(!ok)throw Error('OWNED_OPTIN_NOT_QUALIFIED');}; +const channelValid=channel=>channel==='legacy'||channel==='slash-v1'; +const nameFor=channel=>channel==='legacy'?NAME:SLASH_NAME; +const external=(id,channel)=>channel==='legacy'? + `mathgraph-omm-owner-optin-v1:${REPO}:${NUMBER}:${HEAD}:${id}`: + `mathgraph-omm-owned-slash-v1:${REPO}:${NUMBER}:${HEAD}`; + +export function buildOwnedCommentCheck({admission,assessment,runUrl,channel='legacy'}={}){ + fail(channelValid(channel)&&admission?.schema==='mathgraph.omm.owned-comment-admission.v1'&& + admission.status==='SOURCE_ADMITTED_ONLY'&&admission.repository===REPO&& + admission.number===NUMBER&&Number.isSafeInteger(admission.comment_id)&&admission.comment_id>0&& + admission.head_sha===HEAD&&admission.base_sha===BASE&&admission.source_sha256===SOURCE&& + admission.native_receipt==='UNKNOWN'&&admission.original_target==='UNKNOWN'&& + admission.intent_fidelity==='UNKNOWN'&&admission.evidence_warrant==='UNKNOWN'&& + admission.publication_permission==='DENIED'&&admission.repair_permission==='DENIED'&& + /^20\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ$/.test(admission.comment_created_at??'')&& + admission.comment_created_at===admission.comment_updated_at&& + assessment?.schema==='mathgraph.omm.claim-assessment.v1'&& + assessment.status==='NATIVE_RECEIPT_BOUND'&&assessment.repository===REPO&& + assessment.number===NUMBER&&assessment.head_sha===HEAD&&assessment.base_sha===BASE&& + assessment.candidate_sha256===SOURCE&&assessment.candidate_proof==='LEAN_COMPILED'&& + assessment.original_target==='NOT_ESTABLISHED_IN_THIS_REPLAY'&& + assessment.formal_statement_relation==='UNKNOWN'&&assessment.intent_fidelity==='UNKNOWN'&& + assessment.evidence_warrant==='UNKNOWN'&&assessment.candidate_dependence==='UNKNOWN'&& + SHA256.test(assessment.native_receipt_sha256??'')&& + /^https:\/\/github\.com\/metalogiclabs\/mathgraph\/actions\/runs\/[1-9][0-9]*$/.test(runUrl??'')); + return {name:nameFor(channel),head_sha:HEAD,external_id:external(admission.comment_id,channel), + status:'completed',conclusion:'neutral',output:{ + title:'Owner opt-in: native original target not established (advisory)', + summary:[`Authenticated first-party [owner request](https://github.com/${REPO}/pull/${NUMBER}#issuecomment-${admission.comment_id}) (`+ + `comment ID ${admission.comment_id}; created ${admission.comment_created_at}).`, + `Source: [frozen PR239](https://github.com/${REPO}/pull/${NUMBER}) at commit \`${HEAD}\`; candidate SHA-256 \`${SOURCE}\`.`, + `Native receipt SHA-256: \`${assessment.native_receipt_sha256}\`; [qualification run](${runUrl}).`, + '| Question | Observation |','|---|---|', + '| Original request identity | SOURCE_ADMITTED_ONLY |', + '| Candidate Lean compilation | LEAN_COMPILED |', + '| Original-target consumer | NOT_ESTABLISHED_IN_THIS_REPLAY |', + '| Formal-statement relation | UNKNOWN |', + '| Mathematical intent | UNKNOWN |', + '| Independent evidence warrant | UNKNOWN |', + '| Repair permission | DENIED |', + '', channel==='legacy'? + 'This is a push-triggered, frozen first-party qualification, not an active issue_comment listener or a community verification service. A failing original-target consumer does not prove mathematical inequivalence. The evidence is source- and run-specific, not a proof of paper-to-Lean fidelity.': + 'Triggered by a real, authenticated first-party issue_comment listener; limited to frozen PR239 and one owner. No community verification or mathematical repair capability is enabled. A failing original-target consumer does not prove mathematical inequivalence. The evidence is source- and run-specific, not a proof of paper-to-Lean fidelity.'].join('\n')} + }; +} + +/** Only this registered App, one comment and exact owned PR head may publish. */ +export async function reconcileOwnedCommentCheck({admission,assessment,runUrl,api,verifyAdmission,write=false, + channel='legacy'}={}){ + const payload=buildOwnedCommentCheck({admission,assessment,runUrl,channel}); + fail(typeof api==='function'&&typeof verifyAdmission==='function'); + const confirmAdmission=async()=>{ + const result=await verifyAdmission(admission); + fail(JSON.stringify(result)===JSON.stringify(admission)); + }; + const confirmSource=async()=>{ + const pr=await api(root+'/pulls/'+NUMBER); + if(pr?.number!==NUMBER||pr.state!=='open'||pr.user?.id!==OWNER|| + pr.head?.sha!==HEAD||pr.base?.sha!==BASE|| + pr.head?.repo?.full_name!==REPO||pr.base?.repo?.full_name!==REPO) + throw Error('STALE_PR_HEAD'); + }; + await confirmAdmission();await confirmSource(); + const name=nameFor(channel),externalId=external(admission.comment_id,channel); + const path=root+'/commits/'+HEAD+'/check-runs?check_name='+encodeURIComponent(name)+'&filter=all&per_page=100'; + const list=await api(path); + if(!Array.isArray(list?.check_runs)||!Number.isSafeInteger(list.total_count)|| + list.total_count!==list.check_runs.length||list.total_count>100) + throw Error('APP_CHECK_RECONCILIATION_INCOMPLETE'); + const matching=list.check_runs.filter(x=>x.name===name); + if(matching.some(x=>x.app?.id!==APP_ID))throw Error('FOREIGN_APP_CHECK_NAME_COLLISION'); + if(matching.length>1)throw Error('DUPLICATE_OWNED_OMM_CHECK'); + if(matching.length===1&&(matching[0].head_sha!==HEAD|| + matching[0].external_id!==externalId|| + !Number.isSafeInteger(matching[0].id)))throw Error('OWNED_CHECK_IDENTITY_CHANGED'); + await confirmAdmission();await confirmSource(); + if(!write)return {mode:'PREVIEW',existing:matching.length}; + let result,mode; + if(matching.length){ + const old=matching[0]; + if(old.status==='completed'&&old.conclusion==='neutral'&& + old.output?.title===payload.output.title&&old.output?.summary===payload.output.summary){ + result=old;mode='UNCHANGED'; + }else{ + result=await api(root+'/check-runs/'+old.id,{method:'PATCH',body:{ + status:'completed',conclusion:'neutral',external_id:payload.external_id,output:payload.output}}); + mode='UPDATED'; + } + }else{result=await api(root+'/check-runs',{method:'POST',body:payload});mode='CREATED';} + if(!Number.isSafeInteger(result?.id))throw Error('APP_CHECK_WRITE_UNCONFIRMED'); + const observed=await api(root+'/check-runs/'+result.id); + if(observed?.id!==result.id||observed.app?.id!==APP_ID||observed.name!==name|| + observed.head_sha!==HEAD||observed.external_id!==payload.external_id|| + observed.status!=='completed'||observed.conclusion!=='neutral'|| + observed.output?.title!==payload.output.title||observed.output?.summary!==payload.output.summary) + throw Error('APP_CHECK_READBACK_MISMATCH'); + await confirmAdmission();await confirmSource(); + return {mode,number:NUMBER,comment_id:admission.comment_id,app_id:APP_ID, + check_run_id:result.id,head_sha:HEAD,source_sha256:SOURCE, + original_target:'NOT_ESTABLISHED_IN_THIS_REPLAY',intent_fidelity:'UNKNOWN', + evidence_warrant:'UNKNOWN'}; +} diff --git a/integrations/mathgraph-check-github/omm-v1/owned-comment-publish.test.mjs b/integrations/mathgraph-check-github/omm-v1/owned-comment-publish.test.mjs new file mode 100644 index 000000000..03ee5d79f --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/owned-comment-publish.test.mjs @@ -0,0 +1,103 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; + +const repo='metalogiclabs/mathgraph',owner=32909488,number=239,commentId=6650111234; +const head='2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17'; +const base='dfd87d1e921953badedcfae21ade76bb38f5c04a'; +const admission={schema:'mathgraph.omm.owned-comment-admission.v1',status:'SOURCE_ADMITTED_ONLY', + repository:repo,number,comment_id:commentId,comment_created_at:'2026-11-05T00:35:12Z', + comment_updated_at:'2026-11-05T00:35:12Z',head_sha:head,base_sha:base, + source_sha256:'e45efb73729fc98e45e66d216e792e70f67f8c43d4c12cbb753ac410b950e4d9', + native_receipt:'UNKNOWN',original_target:'UNKNOWN',intent_fidelity:'UNKNOWN', + evidence_warrant:'UNKNOWN',repair_permission:'DENIED',publication_permission:'DENIED'}; +const assessment={schema:'mathgraph.omm.claim-assessment.v1',status:'NATIVE_RECEIPT_BOUND', + number,repository:repo,head_sha:head,base_sha:base,candidate_proof:'LEAN_COMPILED', + original_target:'NOT_ESTABLISHED_IN_THIS_REPLAY',formal_statement_relation:'UNKNOWN', + intent_fidelity:'UNKNOWN',evidence_warrant:'UNKNOWN',candidate_dependence:'UNKNOWN', + native_receipt_sha256:'a'.repeat(64),candidate_sha256:admission.source_sha256}; +const runUrl=`https://github.com/${repo}/actions/runs/41930000111`; +const subject=()=>import('./owned-comment-publish.mjs'); + +function client({foreignApp=false,duplicate=false,moved=false}={}){ + const calls=[];let numberOfPrGets=0,check=null,created=0; + const appCheck=()=>({id:900123,app:{id:foreignApp?1234:5308912}, + name:check.name,head_sha:check.head_sha,external_id:check.external_id, + status:check.status,conclusion:check.conclusion,output:check.output}); + const api=async(path,{method='GET',body}={})=>{ + calls.push({path,method}); + if(path===`/repos/${repo}/pulls/${number}`){numberOfPrGets++; + return {number,state:'open',user:{id:owner,type:'User'}, + head:{sha:moved&&numberOfPrGets>1?'0'.repeat(40):head,repo:{full_name:repo}}, + base:{sha:base,repo:{full_name:repo}}};} + if(path.startsWith(`/repos/${repo}/commits/${head}/check-runs?`)) + return {total_count:check?(duplicate?2:1):0, + check_runs:check?(duplicate?[appCheck(),{...appCheck(),id:900124}]:[appCheck()]):[]}; + if(path===`/repos/${repo}/check-runs`&&method==='POST'){ + check=body;created++;return appCheck();} + if(path===`/repos/${repo}/check-runs/900123`&&method==='PATCH'){ + check={...check,...body};return appCheck();} + if(path===`/repos/${repo}/check-runs/900123`&&method==='GET')return appCheck(); + throw Error('UNEXPECTED_'+path+'_'+method); + }; + return {api,calls,getCreated:()=>created,seed:payload=>{check=payload;}}; +} + +test('owner command + pinned native negative consumer projects only a neutral advisory',async()=>{ + const {buildOwnedCommentCheck}=await subject(); + const p=buildOwnedCommentCheck({admission,assessment,runUrl}); + assert.equal(p.name,'MathGraph Check / OMM owner opt-in (advisory)'); + assert.equal(p.head_sha,head); + assert.equal(p.status,'completed'); + assert.equal(p.conclusion,'neutral'); + assert.match(p.external_id,/^mathgraph-omm-owner-optin-v1:/); + assert.match(p.output.summary,/issuecomment-6650111234/); + assert.match(p.output.summary,/NOT_ESTABLISHED_IN_THIS_REPLAY/); + assert.match(p.output.summary,/Mathematical intent \| UNKNOWN/); + assert.match(p.output.summary,/Independent evidence warrant \| UNKNOWN/); + assert.match(p.output.summary,/not an active issue_comment listener/); +}); + +test('unqualified or promoted claims never form an App payload',async()=>{ + const {buildOwnedCommentCheck}=await subject(); + for(const change of [{head_sha:'0'.repeat(40)},{status:'WARRANTED'}, + {publication_permission:'ALLOWED'}, {comment_id:0}]) + assert.throws(()=>buildOwnedCommentCheck({admission:{...admission,...change},assessment,runUrl}), + /OWNED_OPTIN_NOT_QUALIFIED/); + for(const change of [{intent_fidelity:'WARRANTED'},{evidence_warrant:'WARRANTED'}, + {original_target:'CONSUMER_ACCEPTED'},{candidate_sha256:'0'.repeat(64)}]) + assert.throws(()=>buildOwnedCommentCheck({admission,assessment:{...assessment,...change},runUrl}), + /OWNED_OPTIN_NOT_QUALIFIED/); +}); + +test('App write is neutral, exact-head, independently reread, idempotent',async()=>{ + const {reconcileOwnedCommentCheck}=await subject(); + const c=client();let rechecks=0; + const args={admission,assessment,runUrl,api:c.api,verifyAdmission:async()=>{rechecks++;return admission;}}; + const a=await reconcileOwnedCommentCheck({...args,write:true}); + assert.equal(a.mode,'CREATED');assert.equal(a.app_id,5308912); + assert.equal(a.check_run_id,900123);assert.equal(c.getCreated(),1); + assert.equal(rechecks>=2,true); + const b=await reconcileOwnedCommentCheck({...args,write:true}); + assert.equal(b.mode,'UNCHANGED');assert.equal(c.getCreated(),1); +}); + +test('non-owning App collision, duplicate Check and stale source fail closed before write',async()=>{ + const {buildOwnedCommentCheck,reconcileOwnedCommentCheck}=await subject(); + const payload=buildOwnedCommentCheck({admission,assessment,runUrl}); + for(const config of [{foreignApp:true},{duplicate:true},{moved:true}]){ + const c=client(config);if(!config.moved)c.seed(payload); + await assert.rejects(reconcileOwnedCommentCheck({admission,assessment,runUrl,api:c.api, + verifyAdmission:async()=>admission,write:true}), + /FOREIGN_APP_CHECK_NAME_COLLISION|DUPLICATE_OWNED_OMM_CHECK|STALE_PR_HEAD/); + assert.equal(c.getCreated(),0); + } +}); + +test('comment/source withdrawing during the publisher gate prevents App creation',async()=>{ + const {reconcileOwnedCommentCheck}=await subject(); + const c=client();let i=0; + await assert.rejects(reconcileOwnedCommentCheck({admission,assessment,runUrl,api:c.api, + verifyAdmission:async()=>{if(++i>1)throw Error('COMMENT_CHANGED');return admission;},write:true}), + /COMMENT_CHANGED/); + assert.equal(c.getCreated(),0); +}); diff --git a/integrations/mathgraph-check-github/omm-v1/owned-comment.mjs b/integrations/mathgraph-check-github/omm-v1/owned-comment.mjs new file mode 100644 index 000000000..2e73db42c --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/owned-comment.mjs @@ -0,0 +1,68 @@ +// Qualification-only: authenticate a REAL first-party GitHub comment by ID. +// This is not an issue_comment event handler; it never grants execution, +// publication, semantic warrant or repair permission on its own. +import {checkPublishedArtinInput} from './artin-preflight.mjs'; +import {isOmmClaimCheckCommand} from './command.mjs'; + +const REPO='metalogiclabs/mathgraph',NUMBER=239,OWNER=32909488; +const SHA40=/^[a-f0-9]{40}$/,SHA256=/^[a-f0-9]{64}$/; +const root='/repos/'+REPO; +const requireQualified=(ok,reason)=>{if(!ok)throw Error(reason);}; + +const sourceValid=p=>p?.number===NUMBER&&p.state==='open'&& + p.user?.id===OWNER&&p.user.type==='User'&& + SHA40.test(p.head?.sha??'')&&SHA40.test(p.base?.sha??'')&& + p.head?.repo?.full_name===REPO&&p.base?.repo?.full_name===REPO; +const commentValid=(c,id,commandBody)=>c?.id===id&&isOmmClaimCheckCommand(c.body)&& + (commandBody===undefined||c.body===commandBody)&& + c.user?.id===OWNER&&c.user?.type==='User'&& + c.issue_url===`https://api.github.com${root}/issues/${NUMBER}`&& + /^20\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ$/.test(c.created_at??'')&& + c.updated_at===c.created_at; + +/** Treat one exact owner-authored GitHub comment as a source request only. */ +export async function admitOwnedComment({number,commentId,api,commandBody, + qualifyRegistered=checkPublishedArtinInput}={}){ + requireQualified(number===NUMBER&&Number.isSafeInteger(commentId)&&commentId>0&& + typeof api==='function'&&typeof qualifyRegistered==='function'&& + (commandBody===undefined||isOmmClaimCheckCommand(commandBody)), + 'UNREGISTERED_OWNED_REQUEST'); + const path=root+'/issues/comments/'+commentId; + const initialComment=await api(path); + requireQualified(commentValid(initialComment,commentId,commandBody),'OWNED_COMMENT_IDENTITY_UNQUALIFIED'); + const initialPr=await api(root+'/pulls/'+NUMBER); + requireQualified(sourceValid(initialPr),'FIRST_PARTY_PR_REQUIRED'); + const registration=await qualifyRegistered({number:NUMBER}); + const request=registration?.request; + requireQualified(request?.schema==='mathgraph.omm.request.v1'&& + request.repository===REPO&&request.number===NUMBER&&request.command==='check'&& + request.head_sha===initialPr.head.sha&&request.base_sha===initialPr.base.sha&& + SHA256.test(request.candidate_source_sha256??''),'QUALIFIED_SOURCE_IDENTITY_MISMATCH'); + const lastComment=await api(path); + requireQualified(commentValid(lastComment,commentId,commandBody)&& + lastComment.created_at===initialComment.created_at&& + lastComment.updated_at===initialComment.updated_at,'COMMENT_CHANGED'); + const lastPr=await api(root+'/pulls/'+NUMBER); + requireQualified(sourceValid(lastPr)&&lastPr.head.sha===initialPr.head.sha&& + lastPr.base.sha===initialPr.base.sha,'SOURCE_HEAD_MOVED'); + return {schema:'mathgraph.omm.owned-comment-admission.v1',status:'SOURCE_ADMITTED_ONLY', + repository:REPO,number:NUMBER,comment_id:commentId, + comment_created_at:initialComment.created_at,comment_updated_at:initialComment.updated_at, + head_sha:request.head_sha,base_sha:request.base_sha, + source_sha256:request.candidate_source_sha256, + native_receipt:'UNKNOWN',original_target:'UNKNOWN',intent_fidelity:'UNKNOWN', + evidence_warrant:'UNKNOWN',repair_permission:'DENIED',publication_permission:'DENIED'}; +} + +/** Re-read live source/comment and compare *every* admitted field before use. */ +export async function verifyOwnedAdmission(admission,options={}){ + requireQualified(admission?.schema==='mathgraph.omm.owned-comment-admission.v1'&& + admission.number===NUMBER&&Number.isSafeInteger(admission.comment_id), + 'OWNED_ADMISSION_RECHECK_MISMATCH'); + let fresh; + try{fresh=await admitOwnedComment({...options,number:NUMBER,commentId:admission.comment_id});} + catch{throw Error('OWNED_ADMISSION_RECHECK_MISMATCH');} + requireQualified(JSON.stringify(admission)===JSON.stringify(fresh), + 'OWNED_ADMISSION_RECHECK_MISMATCH'); + return fresh; +} diff --git a/integrations/mathgraph-check-github/omm-v1/owned-comment.test.mjs b/integrations/mathgraph-check-github/omm-v1/owned-comment.test.mjs new file mode 100644 index 000000000..f9c9a1b81 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/owned-comment.test.mjs @@ -0,0 +1,110 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; + +const repo='metalogiclabs/mathgraph',owner=32909488,number=239,commentId=6650111234; +const head='2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17'; +const base='dfd87d1e921953badedcfae21ade76bb38f5c04a'; +const source='e45efb73729fc98e45e66d216e792e70f67f8c43d4c12cbb753ac410b950e4d9'; +const timestamp='2026-11-05T00:35:12Z'; +const qualified=()=>({request:{schema:'mathgraph.omm.request.v1',repository:repo,number, + command:'check',head_sha:head,base_sha:base,candidate_source_sha256:source}}); + +function client({foreignComment=false,edited=false,wrongPr=false,headMoved=false, + baseMoved=false,ownerChanged=false,deleted=false,body='@MathGraph -claim'}={}){ + const calls=[];let c=0,p=0; + const api=async path=>{ + calls.push(path); + if(path===`/repos/${repo}/issues/comments/${commentId}`){ + c++;if(deleted&&c>1)throw Error('HTTP_404'); + return {id:commentId,body:edited&&c>1?'@MathGraph -fix':body, + user:{id:foreignComment?9:owner,type:'User'}, + created_at:timestamp,updated_at:edited&&c>1?'2026-11-05T00:36:00Z':timestamp, + issue_url:`https://api.github.com/repos/${repo}/issues/${wrongPr?240:number}`}; + } + if(path===`/repos/${repo}/pulls/${number}`){ + p++;return {number,state:'open',user:{id:ownerChanged?10:owner,type:'User'}, + head:{sha:headMoved&&p>1?'0'.repeat(40):head,repo:{full_name:repo}}, + base:{sha:baseMoved&&p>1?'0'.repeat(40):base,repo:{full_name:repo}}}; + } + throw Error(`UNEXPECTED_${path}`); + }; + return {api,calls}; +} + +async function subject(){return import('./owned-comment.mjs');} + +test('real owned comment gates only the registered frozen Artin source and never warrants intent',async()=>{ + const {admitOwnedComment}=await subject(); + const {api,calls}=client(); + const admission=await admitOwnedComment({number,commentId,api, + qualifyRegistered:async({number:n})=>{assert.equal(n,239);return qualified();}}); + assert.equal(admission.schema,'mathgraph.omm.owned-comment-admission.v1'); + assert.equal(admission.status,'SOURCE_ADMITTED_ONLY'); + assert.equal(admission.comment_id,commentId); + assert.equal(admission.head_sha,head); + assert.equal(admission.base_sha,base); + assert.equal(admission.source_sha256,source); + for(const field of ['native_receipt','original_target','intent_fidelity','evidence_warrant']) + assert.equal(admission[field],'UNKNOWN'); + assert.equal(admission.publication_permission,'DENIED'); + assert.equal(admission.repair_permission,'DENIED'); + assert.deepEqual(calls,[`/repos/${repo}/issues/comments/${commentId}`, + `/repos/${repo}/pulls/${number}`,`/repos/${repo}/issues/comments/${commentId}`, + `/repos/${repo}/pulls/${number}`]); +}); + +test('a genuine slash /check comment passes identical owner/source gates',async()=>{ + const {admitOwnedComment}=await subject(); + const {api}=client({body:'@MathGraph /check'}); + const admission=await admitOwnedComment({number,commentId,api, + qualifyRegistered:async()=>qualified()}); + assert.equal(admission.status,'SOURCE_ADMITTED_ONLY'); + assert.equal(admission.comment_id,commentId); + assert.equal(admission.source_sha256,source); + assert.equal(admission.evidence_warrant,'UNKNOWN'); + assert.equal(admission.repair_permission,'DENIED'); +}); + +test('cannot admit unsupported PR or arbitrary invalid IDs',async()=>{ + const {admitOwnedComment}=await subject(); + const {api}=client(); + await assert.rejects(admitOwnedComment({number:240,commentId,api}),/UNREGISTERED_OWNED_REQUEST/); + await assert.rejects(admitOwnedComment({number,commentId:0,api}),/UNREGISTERED_OWNED_REQUEST/); + await assert.rejects(admitOwnedComment({number,commentId:'123',api}),/UNREGISTERED_OWNED_REQUEST/); +}); + +test('foreign author, wrong issue, wrong PR owner or changed source are rejected',async()=>{ + const {admitOwnedComment}=await subject(); + for(const config of [{foreignComment:true},{wrongPr:true},{ownerChanged:true}]){ + const {api}=client(config); + await assert.rejects(admitOwnedComment({number,commentId,api, + qualifyRegistered:async()=>qualified()}),/OWNED_COMMENT_IDENTITY_UNQUALIFIED|FIRST_PARTY_PR_REQUIRED/); + } + const {api}=client(); + await assert.rejects(admitOwnedComment({number,commentId,api, + qualifyRegistered:async()=>({request:{...qualified().request,head_sha:'0'.repeat(40)}})}), + /QUALIFIED_SOURCE_IDENTITY_MISMATCH/); +}); + +test('recheck refuses edited/deleted owner comment and head/base movement',async()=>{ + const {admitOwnedComment}=await subject(); + for(const config of [{edited:true},{deleted:true},{headMoved:true},{baseMoved:true}]){ + const {api}=client(config); + await assert.rejects(admitOwnedComment({number,commentId,api, + qualifyRegistered:async()=>qualified()}), + /COMMENT_CHANGED|SOURCE_HEAD_MOVED|HTTP_404/); + } +}); + +test('saved admission is independently rederived; tampering is rejected',async()=>{ + const {admitOwnedComment,verifyOwnedAdmission}=await subject(); + const admission=await admitOwnedComment({number,commentId,api:client().api, + qualifyRegistered:async()=>qualified()}); + assert.deepEqual(await verifyOwnedAdmission(admission,{api:client().api, + qualifyRegistered:async()=>qualified()}),admission); + for(const change of [{head_sha:'1'.repeat(40)},{comment_id:commentId+1}, + {publication_permission:'ALLOWED'},{evidence_warrant:'WARRANTED'}]){ + await assert.rejects(verifyOwnedAdmission({...admission,...change},{api:client().api, + qualifyRegistered:async()=>qualified()}),/OWNED_ADMISSION_RECHECK_MISMATCH|UNREGISTERED_OWNED_REQUEST/); + } +}); diff --git a/integrations/mathgraph-check-github/omm-v1/report.mjs b/integrations/mathgraph-check-github/omm-v1/report.mjs new file mode 100644 index 000000000..4f930aedf --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/report.mjs @@ -0,0 +1,65 @@ +// Neutral OMM Passport projection. The caller supplies the source-bound +// assessment; this view is not an independent evidence authenticator. +const REPO=/^[a-zA-Z0-9_.-]+\/[a-zA-Z0-9_.-]+$/; +const SHA40=/^[a-f0-9]{40}$/; +const SHA256=/^[a-f0-9]{64}$/; +const RUN=/^https:\/\/github\.com\/metalogiclabs\/mathgraph\/actions\/runs\/[1-9][0-9]*$/; + +function validateRequest(r){ + if(!REPO.test(r?.repository??'')||!Number.isSafeInteger(r.number)||r.number<1|| + !SHA40.test(r.head_sha??'')||!SHA40.test(r.base_sha??''))throw Error('INVALID_OMM_REPORT_SOURCE'); +} +const compiled=s=>s==='LEAN_COMPILED'?'Compiled':'UNKNOWN'; +const target=s=>({CONSUMER_ACCEPTED:'Accepted by native Lean replay', + NOT_ESTABLISHED_IN_THIS_REPLAY:'Not established in this replay',UNKNOWN:'UNKNOWN'}[s]??'UNKNOWN'); + +export function ommClaimView(assessment,request){ + validateRequest(request); + const observed=assessment?.status==='NATIVE_RECEIPT_BOUND'&&assessment.head_sha!==undefined&& + assessment.head_sha===request.head_sha&&assessment.base_sha===request.base_sha; + // An unrelated or stale artifact cannot be rendered as an observed success. + const proof=observed?assessment.candidate_proof:'UNKNOWN'; + const original=observed?assessment.original_target:'UNKNOWN'; + const title=original==='CONSUMER_ACCEPTED'?'Original-target consumer accepted': + original==='NOT_ESTABLISHED_IN_THIS_REPLAY'?'Original target not established in this replay':'Check incomplete'; + return {title,summary:proof==='LEAN_COMPILED'?'Lean candidate compiled; the original target is assessed separately.': + 'The evidence is incomplete or stale. No native proof acceptance is claimed.', + candidate_proof:proof,original_target:original,formal_statement_relation:'UNKNOWN', + candidate_axiom_status:observed&&assessment.candidate_axiom_status==='STANDARD_ONLY'?'STANDARD_ONLY':'UNKNOWN', + candidate_dependence:'UNKNOWN',intent_fidelity:'UNKNOWN',evidence_warrant:'UNKNOWN', + scope:'One source- and receipt-pinned native consumer replay. Formal statement equivalence: UNKNOWN. Human mathematical intent: UNKNOWN. This is NOT mathematical inequivalence, proof necessity, or acceptance of an author-specified claim.', + fix:{available:false,reason:'No automatic repair is qualified for OMM Lean claims.'}, + next:original==='NOT_ESTABLISHED_IN_THIS_REPLAY'?'Inspect the original-target consumer and exact source; seek independent claim approval before proposing a repair.': + original==='CONSUMER_ACCEPTED'?'Have an independent reviewer approve that the formal target represents the intended mathematics.': + 'Recheck the current revision with a separately authorized claim contract and complete native evidence.'}; +} + +export function formatOmmClaimReport(assessment,request,runUrl){ + const v=ommClaimView(assessment,request); + if(!RUN.test(runUrl??''))throw Error('INVALID_EVIDENCE_LINK'); + const observed=assessment?.status==='NATIVE_RECEIPT_BOUND'&&assessment.head_sha===request.head_sha&&assessment.base_sha===request.base_sha; + const pins=observed?[ + ['Trusted original source',assessment.reference_sha256], + ['Candidate source',assessment.candidate_sha256], + ['Consumer source',assessment.consumer_sha256], + ['Claim contract',assessment.contract_sha256], + ['Native receipt',assessment.native_receipt_sha256] + ].filter(([,v])=>SHA256.test(v??'')):[]; + return ['### MathGraph Check · Open Math Model', + `**${v.title}**`, + `[${request.repository}#${request.number}](https://github.com/${request.repository}/pull/${request.number})`, + `Head: \`${request.head_sha}\` · Base: \`${request.base_sha}\``, + '| Assurance axis | Observation |','|---|---|', + `| Lean candidate | ${compiled(v.candidate_proof)} |`, + `| Candidate axioms | ${v.candidate_axiom_status==='STANDARD_ONLY'?'Standard only':'UNKNOWN'} |`, + `| Original-target consumer | ${target(v.original_target)} |`, + '| Candidate proof necessity | UNKNOWN |', + '| Formal-statement relation | UNKNOWN |', + '| Human mathematical intent | UNKNOWN |', + '| Independent evidence warrant | UNKNOWN |', + '',v.scope,'',`**Next:** ${v.next}`, + '',v.fix.reason, + ...(pins.length?['',...pins.map(([name,value])=>`${name} SHA-256: \`${value}\``)]:[]), + '',`[Execution and full evidence](${runUrl})` + ].join('\n'); +} diff --git a/integrations/mathgraph-check-github/omm-v1/report.test.mjs b/integrations/mathgraph-check-github/omm-v1/report.test.mjs new file mode 100644 index 000000000..7cba947be --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/report.test.mjs @@ -0,0 +1,49 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {formatOmmClaimReport,ommClaimView} from './report.mjs'; + +const request = {repository:'metalogiclabs/mathgraph',number:300,command:'check',head_sha:'a'.repeat(40),base_sha:'b'.repeat(40)}; +const receipt = {status:'NATIVE_RECEIPT_BOUND',head_sha:request.head_sha,base_sha:request.base_sha,candidate_proof:'LEAN_COMPILED', + original_target:'NOT_ESTABLISHED_IN_THIS_REPLAY',formal_statement_relation:'UNKNOWN', + intent_fidelity:'UNKNOWN',evidence_warrant:'UNKNOWN',candidate_dependence:'UNKNOWN',candidate_axiom_status:'STANDARD_ONLY',repair_permission:'DENIED', + reference_sha256:'c'.repeat(64),candidate_sha256:'d'.repeat(64),consumer_sha256:'e'.repeat(64), + contract_sha256:'f'.repeat(64),native_receipt_sha256:'1'.repeat(64)}; +const url='https://github.com/metalogiclabs/mathgraph/actions/runs/123'; + +test('accepted Lean proof and rejected original target appear as distinct scoped observations',()=>{ + const report=formatOmmClaimReport(receipt,request,url); + assert.match(report,/Lean candidate.*Compiled/i); + assert.match(report,/original.target.*not established in this replay/i); + assert.match(report,/Formal.statement.*UNKNOWN/i); + assert.match(report,/Human.*intent.*UNKNOWN/i); + assert.match(report,/Candidate axioms.*Standard only/i); + assert.match(report,/Candidate proof necessity.*UNKNOWN/i); + assert.match(report,/NOT.*mathematical inequivalence/i); + assert.match(report,/No automatic repair/i); + assert.match(report,/metalogiclabs\/mathgraph#300/); + assert.match(report,/c{64}/); + assert.match(report,/1{64}/); +}); + +test('native consumer accepted never implies the paper meaning is verified',()=>{ + const v=ommClaimView({...receipt,original_target:'CONSUMER_ACCEPTED'},request); + assert.match(v.title,/original.target consumer accepted/i); + assert.match(v.scope,/intent.*UNKNOWN/i); + assert.equal(v.fix.available,false); +}); + +test('missing evidence yields neutral UNKNOWN without a green headline or repair action',()=>{ + const assessment={status:'UNKNOWN',reason:'TRUSTED_CLAIM_CONTRACT_MISSING_OR_SOURCE_CHANGED', + candidate_proof:'UNKNOWN',original_target:'UNKNOWN',intent_fidelity:'UNKNOWN', + formal_statement_relation:'UNKNOWN',evidence_warrant:'UNKNOWN'}; + const report=formatOmmClaimReport(assessment,request,url); + assert.match(report,/Check incomplete/); + assert.doesNotMatch(report,/Original.target consumer accepted/); + assert.doesNotMatch(report,/use .*fix/i); + assert.match(report,/current revision/i); +}); + +test('report fails closed on unsafe repository identities and artifact URLs',()=>{ + assert.throws(()=>formatOmmClaimReport(receipt,{...request,repository:'bad/owner\n@everyone'},url)); + assert.throws(()=>formatOmmClaimReport(receipt,request,'javascript:alert(1)')); +}); diff --git a/integrations/mathgraph-check-github/omm-v1/sandbox-native.mjs b/integrations/mathgraph-check-github/omm-v1/sandbox-native.mjs new file mode 100644 index 000000000..edbfadccf --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/sandbox-native.mjs @@ -0,0 +1,80 @@ +// Candidate community Lean execution boundary: first-party smoke only. +// Never run this in a credentialed GitHub App / model job. The container gets +// one read-only Lean source file, a tiny tmpfs and zero host/network access. +// Compiling Lean code does not establish a user's intended mathematical claim. +import {createHash} from 'node:crypto'; +import {execFile} from 'node:child_process'; +import {promisify} from 'node:util'; +import {mkdtemp,writeFile,rm} from 'node:fs/promises'; +import {tmpdir} from 'node:os'; +import {join,normalize,sep} from 'node:path'; + +const exec=promisify(execFile); +const IMAGE='mathgraph-lean-sandbox:omm-v1'; +const digest=s=>createHash('sha256').update(s).digest('hex'); +const assert=(yes,code)=>{if(!yes)throw Error(code);}; + +export function sandboxDockerArgs(options={}){ + assert(Object.keys(options).length===1&&typeof options.sourcePath==='string', + 'UNSUPPORTED_SANDBOX_OPTION'); + const sourcePath=options.sourcePath,base=tmpdir()+sep+'omm-lean-'; + assert(sourcePath.startsWith(base)&&sourcePath===normalize(sourcePath)&& + /^[-a-zA-Z0-9_]+$/.test(sourcePath.slice(base.length).split(sep)[0]??'')&& + sourcePath.slice(base.length).split(sep).length===2&& + sourcePath.endsWith(sep+'Submission.lean')&&!sourcePath.includes(','), + 'UNSAFE_SANDBOX_SOURCE'); + const nonce=sourcePath.slice(base.length).split(sep)[0]; + return ['run','--rm','--name','mathgraph-omm-'+nonce,'--pull','never', + '--network','none','--ipc','none','--read-only', + '--cap-drop','ALL','--security-opt','no-new-privileges', + '--pids-limit','32','--memory','512m','--memory-swap','512m','--cpus','1', + '--user','65534:65534','--ulimit','nofile=128:128', + '--tmpfs','/tmp:rw,noexec,nosuid,nodev,size=32m,mode=1777', + '--mount',`type=bind,src=${sourcePath},dst=/work/Submission.lean,readonly`, + '--workdir','/work', + '--env','HOME=/tmp','--env','LEAN_NUM_THREADS=1', + IMAGE,'-o','/tmp/Submission.olean','/work/Submission.lean']; +} + +/** An exec timeout kills the Docker CLI, not necessarily the daemon container. */ +export async function reapIsolatedContainer({name,run=exec}={}){ + assert(typeof name==='string'&&/^mathgraph-omm-[-A-Za-z0-9_]+$/.test(name)&& + typeof run==='function','UNSAFE_SANDBOX_REAP_TARGET'); + try{ + await run('docker',['rm','-f',name],{timeout:5000,maxBuffer:131072, + env:{PATH:process.env.PATH??'/usr/bin:/bin'}}); + }catch(e){ + const message=String(e.stderr??'')+' '+String(e.message??''); + if(!/No such container:/i.test(message))throw Error('SANDBOX_CLEANUP_UNCONFIRMED'); + } +} + +export async function runIsolatedLeanSource({source,run=exec}={}){ + assert(typeof source==='string'&&Buffer.byteLength(source,'utf8')>0&& + Buffer.byteLength(source,'utf8')<=32768,'SOURCE_SIZE_REFUSED'); + assert(!source.includes('\u0000'),'SOURCE_FORMAT_REFUSED'); + assert(typeof run==='function','INVALID_NATIVE_RUNNER'); + const directory=await mkdtemp(join(tmpdir(),'omm-lean-')); + const sourcePath=join(directory,'Submission.lean'),sourceSha=digest(source); + try{ + await writeFile(sourcePath,source,{mode:0o444,flag:'wx'}); + const args=sandboxDockerArgs({sourcePath}); + const opts={timeout:60000,maxBuffer:262144,env:{PATH:process.env.PATH??'/usr/bin:/bin'}}; + const name=args[args.indexOf('--name')+1]; + let outcome; + try{ + const result=await run('docker',args,opts); + outcome={status:'LEAN_ACCEPTED',exit_code:0,source_sha256:sourceSha, + output:String(result.stdout??'').slice(0,131072)+String(result.stderr??'').slice(0,131072)}; + }catch(e){ + const code=Number.isInteger(e.code)?e.code:null; + outcome={status:code===1?'LEAN_REJECTED_THIS_REPLAY':'EXECUTION_UNKNOWN', + exit_code:code,source_sha256:sourceSha, + output:(String(e.stdout??'')+String(e.stderr??'')).slice(0,131072), + reason:code===1?'LEAN_EXITED_NONZERO':'RUNTIME_INCOMPLETE_OR_RESOURCE_BOUND'}; + } + try{await reapIsolatedContainer({name,run});} + catch{return {...outcome,status:'EXECUTION_UNKNOWN',reason:'SANDBOX_CLEANUP_UNCONFIRMED'};} + return outcome; + }finally{await rm(directory,{recursive:true,force:true});} +} diff --git a/integrations/mathgraph-check-github/omm-v1/sandbox-native.test.mjs b/integrations/mathgraph-check-github/omm-v1/sandbox-native.test.mjs new file mode 100644 index 000000000..06a4a9478 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/sandbox-native.test.mjs @@ -0,0 +1,93 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {join} from 'node:path'; +import {tmpdir} from 'node:os'; +import {sandboxDockerArgs,runIsolatedLeanSource} from './sandbox-native.mjs'; + +const candidate=join(tmpdir(),'omm-lean-abc123','Submission.lean'); + +test('Docker boundary has one read-only input, no network, no privileges and hard resource caps',()=>{ + const args=sandboxDockerArgs({sourcePath:candidate}); + assert.deepEqual(args.slice(0,2),['run','--rm']); + assert.match(args[args.indexOf('--name')+1],/^mathgraph-omm-[-A-Za-z0-9_]+$/); + assert.deepEqual(args.slice(-3),['-o','/tmp/Submission.olean','/work/Submission.lean']); + const joined=args.join(' '); + for(const item of ['--network none','--read-only','--cap-drop ALL','--security-opt no-new-privileges', + '--pids-limit 32','--memory 512m','--memory-swap 512m','--cpus 1', + '--user 65534:65534','--pull never','--ipc none', + 'type=bind,src='+candidate+',dst=/work/Submission.lean,readonly'])assert.ok(joined.includes(item),item); + assert.equal(joined.includes('GITHUB_TOKEN'),false); + assert.equal(joined.includes('/var/run/docker.sock'),false); + assert.equal(joined.includes(',dst=/work,readonly'),false); + assert.equal(args.filter(x=>x==='--mount').length,1); +}); + +test('arbitrary mount path, escape, noncanonical source and image override cannot cross the boundary',()=>{ + for(const bad of ['/etc/passwd',join(tmpdir(),'other','Submission.lean'), + join(tmpdir(),'omm-lean-abc123','../Submission.lean'), + candidate+',dst=/etc/passwd'])assert.throws(()=>sandboxDockerArgs({sourcePath:bad}),/UNSAFE_SANDBOX_SOURCE/); + assert.throws(()=>sandboxDockerArgs({sourcePath:candidate,image:'ubuntu:latest'}),/UNSUPPORTED_SANDBOX_OPTION/); +}); + +test('isolated runner keeps host credentials out and reports positive and negative native exits separately',async()=>{ + const invocations=[]; + const run=async(command,args,opts)=>{ + invocations.push({command,args,opts}); + return {stdout:'Lean output',stderr:''}; + }; + const accepted=await runIsolatedLeanSource({source:'import Init\nexample : True := by trivial\n',run}); + assert.equal(accepted.status,'LEAN_ACCEPTED'); + assert.equal(accepted.exit_code,0); + assert.equal(invocations.length,2); + assert.equal(invocations[0].command,'docker'); + assert.deepEqual(invocations[1].args.slice(0,2),['rm','-f']); + assert.equal(invocations[1].args[2],invocations[0].args[invocations[0].args.indexOf('--name')+1]); + assert.deepEqual(Object.keys(invocations[0].opts.env),['PATH']); + assert.ok(invocations[0].opts.timeout<=60000); + assert.ok(invocations[0].opts.maxBuffer<=262144); + assert.equal(accepted.source_sha256.length,64); + const failed=await runIsolatedLeanSource({source:'example : False := by trivial\n',run:async(_command,args)=>{ + if(args[0]==='run')throw Object.assign(Error('LEAN_EXIT_1'),{code:1,stdout:'type mismatch',stderr:''}); + return {stdout:'',stderr:''}; + }}); + assert.equal(failed.status,'LEAN_REJECTED_THIS_REPLAY'); + assert.equal(failed.exit_code,1); + const resource=await runIsolatedLeanSource({source:'example : True := by trivial\n',run:async(_command,args)=>{ + if(args[0]==='run')throw Object.assign(Error('OOM'),{code:137}); + return {stdout:'',stderr:''}; + }}); + assert.equal(resource.status,'EXECUTION_UNKNOWN'); + assert.equal(resource.exit_code,137); +}); + +test('execution timeout is UNKNOWN and forcibly reaps the exact task container name',async()=>{ + const calls=[]; + const r=await runIsolatedLeanSource({source:'example : True := by trivial\n', + run:async(_command,args)=>{ + calls.push(args); + if(args[0]==='run')throw Object.assign(Error('timeout'),{code:'ETIMEDOUT'}); + return {stdout:'',stderr:''}; + }}); + assert.equal(r.status,'EXECUTION_UNKNOWN'); + assert.equal(r.exit_code,null); + assert.equal(calls.length,2); + assert.deepEqual(calls[1],['rm','-f',calls[0][calls[0].indexOf('--name')+1]]); +}); + +test('unconfirmed container cleanup cannot be reported as accepted Lean execution',async()=>{ + let count=0; + const r=await runIsolatedLeanSource({source:'example : True := by trivial\n',run:async()=>{ + if(count++===0)return {stdout:'ok',stderr:''}; + throw Object.assign(Error('daemon unavailable'),{code:125,stderr:'permission denied'}); + }}); + assert.equal(r.status,'EXECUTION_UNKNOWN'); + assert.equal(r.reason,'SANDBOX_CLEANUP_UNCONFIRMED'); +}); + +test('over-budget source bytes and NULs are rejected before Docker is invoked',async()=>{ + let count=0;const run=async()=>{count++;return {stdout:'',stderr:''};}; + await assert.rejects(runIsolatedLeanSource({source:'x'.repeat(32769),run}),/SOURCE_SIZE_REFUSED/); + await assert.rejects(runIsolatedLeanSource({source:'x\u0000y',run}),/SOURCE_FORMAT_REFUSED/); + await assert.rejects(runIsolatedLeanSource({source:'',run}),/SOURCE_SIZE_REFUSED/); + assert.equal(count,0); +}); diff --git a/integrations/mathgraph-check-github/omm-v1/sandbox-smoke.mjs b/integrations/mathgraph-check-github/omm-v1/sandbox-smoke.mjs new file mode 100644 index 000000000..06103d339 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/sandbox-smoke.mjs @@ -0,0 +1,70 @@ +// No-secret, owned-fixture-only native isolation experiment. It does not +// accept PR input or provide a community sandbox service. +import {execFile} from 'node:child_process'; +import {promisify} from 'node:util'; +import {mkdtemp,writeFile,rm} from 'node:fs/promises'; +import {join,resolve} from 'node:path'; +import {tmpdir} from 'node:os'; +import {pathToFileURL} from 'node:url'; +import {sandboxDockerArgs,runIsolatedLeanSource,reapIsolatedContainer} from './sandbox-native.mjs'; + +const exec=promisify(execFile); +const ensure=(okay,reason)=>{if(!okay)throw Error(reason);}; +const positive='import Init\nexample : 2 + 2 = 4 := by decide\n'; +const negative='import Init\nexample : False := by trivial\n'; + +export async function probeSandboxBoundary({run=exec}={}){ + const directory=await mkdtemp(join(tmpdir(),'omm-lean-')); + const sourcePath=join(directory,'Submission.lean'); + try{ + await writeFile(sourcePath,positive,{mode:0o444,flag:'wx'}); + const args=sandboxDockerArgs({sourcePath}); + const imageAt=args.indexOf('mathgraph-lean-sandbox:omm-v1'); + ensure(imageAt>0,'SANDBOX_IMAGE_MISSING'); + const name=args[args.indexOf('--name')+1]; + const shell=[...args.slice(0,imageAt),'--entrypoint','/bin/sh',args[imageAt],'-c', + 'set -eu; test -r /work/Submission.lean; test "$(id -u)" -eq 65534; '+ + '! (echo denied > /work/Submission.lean); '+ + '! (echo denied > /etc/mathgraph-sandbox-violation); '+ + '! grep -q "00000000" /proc/net/route; '+ + 'echo OMM_BOUNDARY_OBSERVED']; + try{ + const result=await run('docker',shell,{timeout:20000,maxBuffer:262144, + env:{PATH:process.env.PATH??'/usr/bin:/bin'}}); + ensure(result.stdout.includes('OMM_BOUNDARY_OBSERVED'),'SANDBOX_GUARD_FAILED'); + return {read_only:true,no_default_route:true,uid:65534}; + }finally{await reapIsolatedContainer({name,run});} + }finally{await rm(directory,{recursive:true,force:true});} +} + +export async function qualifySandboxSmoke({native=runIsolatedLeanSource,probe=probeSandboxBoundary}={}){ + const [p,n]=[await native({source:positive}),await native({source:negative})]; + const guard=await probe(); + ensure(p?.status==='LEAN_ACCEPTED'&&p.exit_code===0&& + n?.status==='LEAN_REJECTED_THIS_REPLAY'&&n.exit_code===1&& + guard?.read_only===true&&guard.no_default_route===true&&guard.uid===65534, + 'SANDBOX_SMOKE_FAILED'); + return {schema:'mathgraph.omm.sandbox-smoke.v1',status:'SANDBOX_SMOKE_OBSERVED', + candidate_compilation:'OBSERVED',negative_control:'REJECTED_THIS_REPLAY', + first_party_positive_sha256:p.source_sha256,first_party_negative_sha256:n.source_sha256, + isolation:{...guard,network_mode:'none',host_source_mount:'SINGLE_READ_ONLY_FILE', + image:'mathgraph-lean-sandbox:omm-v1',memory:'512m',pids:32,cpus:1}, + intent_fidelity:'UNKNOWN',community_execution_permission:'DENIED', + independent_evidence_warrant:'UNKNOWN'}; +} + +if(process.argv[1]&&import.meta.url===pathToFileURL(resolve(process.argv[1])).href){ + try{ + ensure(process.argv.length===2&&process.env.GITHUB_REPOSITORY==='metalogiclabs/mathgraph'&& + process.env.GITHUB_EVENT_NAME==='push'&&process.env.GITHUB_ACTOR_ID==='32909488'&& + /^[a-f0-9]{40}$/.test(process.env.GITHUB_SHA??'')&& + typeof process.env.RUNNER_TEMP==='string'&&process.env.RUNNER_TEMP.startsWith('/')&& + !process.env.OPENAI_API_KEY&&!process.env.MATHGRAPH_APP_INSTALLATION_TOKEN, + 'UNTRUSTED_SMOKE_CONTEXT'); + const report=await qualifySandboxSmoke(); + report.controller_sha=process.env.GITHUB_SHA; + report.run_url=`https://github.com/metalogiclabs/mathgraph/actions/runs/${process.env.GITHUB_RUN_ID}`; + await writeFile(join(process.env.RUNNER_TEMP,'omm-sandbox-smoke.json'),JSON.stringify(report,null,2)+'\n',{flag:'wx'}); + console.log('OMM_SANDBOX_SMOKE_READBACK '+JSON.stringify(report)); + }catch(e){console.error('OMM_SANDBOX_SMOKE_UNKNOWN:'+String(e.message));process.exitCode=1;} +} diff --git a/integrations/mathgraph-check-github/omm-v1/sandbox-smoke.test.mjs b/integrations/mathgraph-check-github/omm-v1/sandbox-smoke.test.mjs new file mode 100644 index 000000000..e03290e55 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/sandbox-smoke.test.mjs @@ -0,0 +1,45 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {qualifySandboxSmoke,probeSandboxBoundary} from './sandbox-smoke.mjs'; + +const ok={status:'LEAN_ACCEPTED',exit_code:0,source_sha256:'a'.repeat(64)}; +const bad={status:'LEAN_REJECTED_THIS_REPLAY',exit_code:1,source_sha256:'b'.repeat(64)}; + +test('two immutable native controls plus actual isolation probe form bounded smoke evidence',async()=>{ + const sources=[]; + const result=await qualifySandboxSmoke({native:async({source})=>{sources.push(source);return sources.length===1?ok:bad;}, + probe:async()=>({read_only:true,no_default_route:true,uid:65534})}); + assert.equal(result.status,'SANDBOX_SMOKE_OBSERVED'); + assert.equal(result.candidate_compilation,'OBSERVED'); + assert.equal(result.negative_control,'REJECTED_THIS_REPLAY'); + assert.equal(result.intent_fidelity,'UNKNOWN'); + assert.equal(result.community_execution_permission,'DENIED'); + assert.equal(sources.length,2); + assert.match(sources[1],/False/); +}); + +test('missing runtime, wrong native outcomes and isolation guard failures refuse promotion',async()=>{ + for(const [positive,negative,probe] of [ + [bad,bad,{read_only:true,no_default_route:true,uid:65534}], + [ok,ok,{read_only:true,no_default_route:true,uid:65534}], + [ok,bad,{read_only:false,no_default_route:true,uid:65534}], + [ok,bad,{read_only:true,no_default_route:false,uid:65534}], + [ok,bad,{read_only:true,no_default_route:true,uid:0}] + ]){ + let count=0; + await assert.rejects(qualifySandboxSmoke({native:async()=>count++===0?positive:negative, + probe:async()=>probe}),/SANDBOX_SMOKE_FAILED/); + } +}); + +test('filesystem/network guard reaps its exact container after its shell probe',async()=>{ + const calls=[]; + const observed=await probeSandboxBoundary({run:async(cmd,args)=>{ + calls.push({cmd,args}); + if(args[0]==='run')return {stdout:'OMM_BOUNDARY_OBSERVED\n',stderr:''}; + return {stdout:'',stderr:''}; + }}); + assert.deepEqual(observed,{read_only:true,no_default_route:true,uid:65534}); + assert.equal(calls.length,2); + assert.deepEqual(calls[1].args,['rm','-f',calls[0].args[calls[0].args.indexOf('--name')+1]]); +}); diff --git a/integrations/mathgraph-check-github/omm-v1/scope-contract-research.mjs b/integrations/mathgraph-check-github/omm-v1/scope-contract-research.mjs new file mode 100644 index 000000000..9ea426940 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/scope-contract-research.mjs @@ -0,0 +1,89 @@ +// Offline OMM research-only scope comparison. Neither the scope labels nor the +// expected digest pins may be supplied by the candidate/model. An independent +// controller must authenticate the original requirement owner, obtain the +// actual elaborated Lean type, attest the ID-to-type mapping, and hold both +// expected digests. The inputs here are annotations, not native type receipts. +// This module does none of those upstream tasks and is NOT wired to the App. +import {createHash} from 'node:crypto'; + +const SHA40 = /^[a-f0-9]{40}$/; +const SHA256 = /^[a-f0-9]{64}$/; +const REPOSITORY = /^[a-zA-Z0-9_.-]+\/[a-zA-Z0-9_.-]+$/; +const REQUIREMENT_ID = /^[a-z0-9][a-z0-9-]{0,63}$/; +const DECLARATION = /^[A-Za-z_][A-Za-z0-9_'.]{0,150}$/; +const PATH_SEGMENT = /^[A-Za-z0-9_.-]+$/; + +function unknown(reason) { + return { + schema: 'mathgraph.omm.declared-scope-assessment.research.v1', + status: 'UNKNOWN', declared_scope: 'UNKNOWN', + uncovered_requirement_ids: [], intent_fidelity: 'UNKNOWN', + proof_status: 'UNKNOWN', reason, + }; +} + +function pinnedJson(bytes, expected) { + if (typeof bytes !== 'string' || bytes.length > 100_000 || + !SHA256.test(expected ?? '') || + createHash('sha256').update(bytes).digest('hex') !== expected) return null; + try { return JSON.parse(bytes); } catch { return null; } +} + +function validSource(value) { + const path = value?.source_path; + return value && REPOSITORY.test(value.repository ?? '') && + SHA40.test(value.head_sha ?? '') && SHA256.test(value.source_sha256 ?? '') && + typeof path === 'string' && path.length <= 240 && path.split('/').every( + segment => segment !== '.' && segment !== '..' && PATH_SEGMENT.test(segment)); +} + +function sameSource(a, b) { + return a.repository === b.repository && a.head_sha === b.head_sha && + a.source_sha256 === b.source_sha256 && a.source_path === b.source_path; +} + +function validIds(ids, allowEmpty = false) { + return Array.isArray(ids) && ids.length <= 32 && + (allowEmpty || ids.length > 0) && + ids.every(id => typeof id === 'string' && REQUIREMENT_ID.test(id)) && + new Set(ids).size === ids.length; +} + +/** + * Report only a syntactic difference between two independently pinned, + * externally attested ID sets. A no-gap result is never human-intent approval. + */ +export function assessDeclaredScope({currentSource, contractBytes, + expectedContractSha256, typeAnnotationBytes, expectedTypeAnnotationSha256} = {}) { + if (!validSource(currentSource)) return unknown('SOURCE_IDENTITY_NOT_AUTHENTICATED'); + + const contract = pinnedJson(contractBytes, expectedContractSha256); + if (!contract || contract.schema !== 'mathgraph.omm.declared-scope-contract.research.v1' || + !validSource(contract) || !sameSource(contract, currentSource) || + !validIds(contract.requirement_ids)) + return unknown('REQUIREMENT_CONTRACT_MISSING_OR_SOURCE_CHANGED'); + + const receipt = pinnedJson(typeAnnotationBytes, expectedTypeAnnotationSha256); + if (!receipt || receipt.schema !== 'mathgraph.omm.type-scope-annotation.research.v1' || + !validSource(receipt) || !sameSource(receipt, currentSource) || + !DECLARATION.test(receipt.declaration ?? '') || + !SHA256.test(receipt.type_sha256 ?? '') || + !validIds(receipt.attested_scope_ids, true) || + receipt.attested_scope_ids.some(id => !contract.requirement_ids.includes(id))) + return unknown('TYPE_SCOPE_ANNOTATION_MISSING_OR_SOURCE_CHANGED'); + + const covered = new Set(receipt.attested_scope_ids); + const uncovered = contract.requirement_ids.filter(id => !covered.has(id)); + return { + schema: 'mathgraph.omm.declared-scope-assessment.research.v1', + status: 'PINNED_SCOPE_ANNOTATIONS_COMPARED', + declared_scope: uncovered.length ? 'DECLARED_GAP' : 'NO_DECLARED_GAP', + uncovered_requirement_ids: uncovered, + intent_fidelity: 'UNKNOWN', proof_status: 'UNKNOWN', + contract_sha256: expectedContractSha256, + type_annotation_sha256: expectedTypeAnnotationSha256, + source_sha256: currentSource.source_sha256, + type_sha256: receipt.type_sha256, + reason: 'OPERATOR_LABELS_ONLY_NOT_NATIVE_TYPE_OR_SEMANTIC_WARRANT', + }; +} diff --git a/integrations/mathgraph-check-github/omm-v1/scope-contract-research.test.mjs b/integrations/mathgraph-check-github/omm-v1/scope-contract-research.test.mjs new file mode 100644 index 000000000..815413dd1 --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/scope-contract-research.test.mjs @@ -0,0 +1,128 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {createHash} from 'node:crypto'; +import {assessDeclaredScope as assess} from './scope-contract-research.mjs'; +const sha = value => createHash('sha256').update(value).digest('hex'); + +const currentSource = { + // Synthetic IDs/bytes. These scenarios are inspired by the public Mathathon + // texts, but do not replay or qualify any of their Lean declarations. + repository: 'metalogiclabs/synthetic-omm-control', + head_sha: 'd'.repeat(40), + source_sha256: 'a'.repeat(64), + source_path: 'fixtures/example.lean', +}; + +const requirements = (ids) => ({ + schema: 'mathgraph.omm.declared-scope-contract.research.v1', + ...currentSource, + requirement_ids: ids, +}); +const target = (ids) => ({ + schema: 'mathgraph.omm.type-scope-annotation.research.v1', + ...currentSource, + declaration: 'Mathathon.example', + type_sha256: 'b'.repeat(64), + attested_scope_ids: ids, +}); +const call = (req, typeReceipt, source=currentSource) => { + const contractBytes = JSON.stringify(req); + const typeAnnotationBytes = typeReceipt === null ? null : JSON.stringify(typeReceipt); + return assess({ + currentSource: source, + contractBytes, + expectedContractSha256: sha(contractBytes), + typeAnnotationBytes, + expectedTypeAnnotationSha256: typeAnnotationBytes === null ? null : sha(typeAnnotationBytes), + }); +}; + +test('P127-shaped: equality-only target leaves source-requested bijection uncovered', () => { + const result = call(requirements(['count-equality', 'natural-bijection']), target(['count-equality'])); + assert.equal(result.status, 'PINNED_SCOPE_ANNOTATIONS_COMPARED'); + assert.equal(result.declared_scope, 'DECLARED_GAP'); + assert.deepEqual(result.uncovered_requirement_ids, ['natural-bijection']); + assert.equal(result.intent_fidelity, 'UNKNOWN'); + assert.equal(result.proof_status, 'UNKNOWN'); +}); + +test('P127-shaped: a separately pinned narrower request no longer declares the bijection gap', () => { + const result = call(requirements(['count-equality']), target(['count-equality'])); + assert.equal(result.declared_scope, 'NO_DECLARED_GAP'); + assert.deepEqual(result.uncovered_requirement_ids, []); + assert.equal(result.intent_fidelity, 'UNKNOWN'); +}); + +test('P11-shaped: existential density does not attest explicit c or cutoff', () => { + const actual = call(requirements(['positive-density', 'explicit-c', 'explicit-cutoff']), + target(['positive-density'])); + assert.equal(actual.declared_scope, 'DECLARED_GAP'); + assert.deepEqual(actual.uncovered_requirement_ids, ['explicit-c', 'explicit-cutoff']); + const narrowed = call(requirements(['positive-density']), target(['positive-density'])); + assert.equal(narrowed.declared_scope, 'NO_DECLARED_GAP'); +}); + +test('P123-shaped: a high-level result match is not a proof or intent certificate', () => { + const result = call(requirements(['schur-five-160']), target(['schur-five-160'])); + assert.equal(result.declared_scope, 'NO_DECLARED_GAP'); + assert.equal(result.intent_fidelity, 'UNKNOWN'); + assert.equal(result.proof_status, 'UNKNOWN'); +}); + +test('missing type annotation is UNKNOWN, not a proven mismatch', () => { + const result = call(requirements(['count-equality']), null); + assert.equal(result.status, 'UNKNOWN'); + assert.equal(result.declared_scope, 'UNKNOWN'); +}); + +test('source movement or comment-only edits invalidate an old receipt', () => { + const req = requirements(['count-equality']); + const rec = target(['count-equality']); + for (const source of [ + {...currentSource, head_sha: 'c'.repeat(40)}, + {...currentSource, source_sha256: sha('same theorem; edited comment')}, + {...currentSource, source_path: 'fixtures/shadowed.lean'}, + ]) { + const result = call(req, rec, source); + assert.equal(result.status, 'UNKNOWN'); + assert.equal(result.declared_scope, 'UNKNOWN'); + } +}); + +test('changed type annotations with old externally held pin are not admitted', () => { + const req = requirements(['natural-bijection']); + const rec = target(['count-equality']); + const args = { + currentSource, + contractBytes: JSON.stringify(req), + expectedContractSha256: sha(JSON.stringify(req)), + typeAnnotationBytes: JSON.stringify({...rec, attested_scope_ids: ['natural-bijection']}), + expectedTypeAnnotationSha256: sha(JSON.stringify(rec)), + }; + const result = assess(args); + assert.equal(result.status, 'UNKNOWN'); + assert.equal(result.declared_scope, 'UNKNOWN'); +}); + +test('duplicate or ill-formed requirement IDs fail closed', () => { + for (const ids of [['count-equality', 'count-equality'], ['count equality'], []]) { + const result = call(requirements(ids), target(['count-equality'])); + assert.equal(result.status, 'UNKNOWN'); + } +}); + +test('ambiguous or traversing source paths fail closed', () => { + const req = requirements(['count-equality']); + const rec = target(['count-equality']); + for (const source_path of ['../example.lean', '/fixtures/example.lean', 'fixtures//example.lean']) { + assert.equal(call({...req, source_path}, {...rec, source_path}, + {...currentSource, source_path}).status, 'UNKNOWN'); + } +}); + +test('a fully claimed type scope still cannot be promoted to human-intent verified', () => { + const result = call(requirements(['a', 'b']), target(['a', 'b'])); + assert.equal(result.declared_scope, 'NO_DECLARED_GAP'); + assert.equal(result.intent_fidelity, 'UNKNOWN'); + assert.equal(result.proof_status, 'UNKNOWN'); +}); diff --git a/integrations/mathgraph-check-github/omm-v1/slash-owned-cli.mjs b/integrations/mathgraph-check-github/omm-v1/slash-owned-cli.mjs new file mode 100644 index 000000000..8ac95980a --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/slash-owned-cli.mjs @@ -0,0 +1,91 @@ +// Exact first-party /check issue_comment bridge; frozen PR239 source only. +// No community intake, model invocation or repair. App credentials are only +// present in the final publisher job, never the Lean process. +import {readFile,writeFile} from 'node:fs/promises'; +import {join,resolve} from 'node:path'; +import {pathToFileURL} from 'node:url'; +import {checkPublishedArtinInput} from './artin-preflight.mjs'; +import {verifyOwnedAdmission} from './owned-comment.mjs'; +import {makeOwnedGitHubApi,prepareOwnedCommentInputs,checkOwnedNativeBinding} from './owned-comment-cli.mjs'; +import {loadFrozenArtinPreview} from './app-owned-publish.mjs'; +import {reconcileOwnedCommentCheck} from './owned-comment-publish.mjs'; + +const REPO='metalogiclabs/mathgraph',NUMBER=239,OWNER=32909488; +const BODY='@MathGraph /check'; +const assert=(valid,reason)=>{if(!valid)throw Error(reason);}; +const isTime=t=>/^20\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ$/.test(t??''); + +/** Validate the runner's event; the API source and comment are re-read later. */ +export function validateOwnedSlashEvent({event,actorId,runAttempt}={}){ + const comment=event?.comment; + assert(event?.action==='created'&&event.repository?.full_name===REPO&& + event.sender?.id===OWNER&&event.sender?.type==='User'&& + event.issue?.number===NUMBER&& + event.issue?.pull_request?.url===`https://api.github.com/repos/${REPO}/pulls/${NUMBER}`&& + Number.isSafeInteger(comment?.id)&&comment.id>0&&comment.body===BODY&& + comment.user?.id===OWNER&&comment.user.type==='User'&& + comment.issue_url===`https://api.github.com/repos/${REPO}/issues/${NUMBER}`&& + isTime(comment.created_at)&&comment.updated_at===comment.created_at&& + actorId===String(OWNER)&&runAttempt==='1','OWNED_SLASH_EVENT_REFUSED'); + return comment.id; +} + +export async function prepareOwnedSlash({directory,event,actorId,runAttempt,api, + qualifyRegistered}={}){ + const commentId=validateOwnedSlashEvent({event,actorId,runAttempt}); + const admission=await prepareOwnedCommentInputs({directory,number:NUMBER,commentId, + api,qualifyRegistered,commandBody:BODY}); + assert(admission.comment_created_at===event.comment.created_at,'OWNED_EVENT_API_MISMATCH'); + return admission; +} + +export async function publishOwnedSlash({directory,event,actorId,runAttempt,api, + qualifyRegistered,runId}={}){ + const commentId=validateOwnedSlashEvent({event,actorId,runAttempt}); + assert(typeof directory==='string'&&directory.startsWith('/')&& + /^[1-9][0-9]*$/.test(String(runId??'')),'OWNED_SLASH_PUBLISH_CONTEXT'); + const admission=JSON.parse(await readFile(join(resolve(directory),'admission.json'),'utf8')); + assert(admission.comment_id===commentId&& + admission.comment_created_at===event.comment.created_at,'OWNED_EVENT_API_MISMATCH'); + const verifyAdmission=async()=>verifyOwnedAdmission(admission,{number:NUMBER,api, + qualifyRegistered,commandBody:BODY}); + await verifyAdmission(); + const {request,assessment}=await loadFrozenArtinPreview({directory,number:NUMBER,runId}); + checkOwnedNativeBinding({admission,request,assessment,expectedCommentId:commentId}); + const runUrl=`https://github.com/${REPO}/actions/runs/${runId}`; + const result=await reconcileOwnedCommentCheck({admission,assessment,runUrl, + api,verifyAdmission,write:true,channel:'slash-v1'}); + await writeFile(join(resolve(directory),'output','owned-slash-app-receipt.json'), + JSON.stringify(result,null,2)+'\n',{flag:'wx'}); + return result; +} + +if(process.argv[1]&&import.meta.url===pathToFileURL(resolve(process.argv[1])).href){ + try{ + const mode=process.argv[2],directory=process.argv[3]; + assert(process.argv.length===4&&['preflight','publish'].includes(mode)&& + directory?.startsWith('/')&&process.env.GITHUB_REPOSITORY===REPO&& + process.env.GITHUB_EVENT_NAME==='issue_comment'&& + typeof process.env.GITHUB_EVENT_PATH==='string'&& + /^[1-9][0-9]*$/.test(process.env.GITHUB_RUN_ID??''), + 'OWNED_SLASH_WORKFLOW_CONTEXT_REQUIRED'); + const event=JSON.parse(await readFile(process.env.GITHUB_EVENT_PATH,'utf8')); + const commentId=validateOwnedSlashEvent({event,actorId:process.env.GITHUB_ACTOR_ID, + runAttempt:process.env.GITHUB_RUN_ATTEMPT}); + const token=mode==='preflight'?process.env.GITHUB_TOKEN: + process.env.MATHGRAPH_APP_INSTALLATION_TOKEN; + const api=makeOwnedGitHubApi({token,commentId,allowWrite:mode==='publish'}); + const authorizedFetch=(url,opts)=>fetch(url,{...opts,redirect:'error', + headers:{...opts?.headers, + ...(url.startsWith(`https://api.github.com/repos/${REPO}/`)? + {Authorization:'Bearer '+token}:{})},signal:AbortSignal.timeout(20000)}); + const qualifyRegistered=({number})=>checkPublishedArtinInput({number,fetch:authorizedFetch}); + const result=mode==='preflight'? + await prepareOwnedSlash({directory,event,actorId:process.env.GITHUB_ACTOR_ID, + runAttempt:process.env.GITHUB_RUN_ATTEMPT,api,qualifyRegistered}): + await publishOwnedSlash({directory,event,actorId:process.env.GITHUB_ACTOR_ID, + runAttempt:process.env.GITHUB_RUN_ATTEMPT,api,qualifyRegistered, + runId:process.env.GITHUB_RUN_ID}); + console.log('OMM_OWNED_SLASH_'+mode.toUpperCase()+' '+JSON.stringify(result)); + }catch(e){console.error('OMM_OWNED_SLASH_UNKNOWN:'+String(e.message));process.exitCode=1;} +} diff --git a/integrations/mathgraph-check-github/omm-v1/slash-owned-cli.test.mjs b/integrations/mathgraph-check-github/omm-v1/slash-owned-cli.test.mjs new file mode 100644 index 000000000..b9864dc2b --- /dev/null +++ b/integrations/mathgraph-check-github/omm-v1/slash-owned-cli.test.mjs @@ -0,0 +1,110 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {readFile,mkdtemp,rm} from 'node:fs/promises'; +import {tmpdir} from 'node:os'; +import {join} from 'node:path'; +import {artinStrongSource} from '../mention-artin-control-v1.mjs'; + +const owner=32909488,commentId=6657771222,repo='metalogiclabs/mathgraph'; +const event={action:'created',repository:{full_name:repo},sender:{id:owner,type:'User'}, + issue:{number:239,pull_request:{url:`https://api.github.com/repos/${repo}/pulls/239`}}, + comment:{id:commentId,body:'@MathGraph /check',user:{id:owner,type:'User'}, + created_at:'2026-11-05T01:15:20Z',updated_at:'2026-11-05T01:15:20Z', + issue_url:`https://api.github.com/repos/${repo}/issues/239`}}; + +test('only a newly created exact slash command by the registered first-party owner enters the listener',async()=>{ + const {validateOwnedSlashEvent}=await import('./slash-owned-cli.mjs'); + assert.equal(validateOwnedSlashEvent({event,actorId:String(owner),runAttempt:'1'}),commentId); + for(const modified of [ + {...event,action:'edited'}, + {...event,repository:{full_name:'other/repo'}}, + {...event,issue:{...event.issue,number:240}}, + {...event,issue:{number:239}}, + {...event,sender:{id:42,type:'User'}}, + {...event,comment:{...event.comment,body:'@MathGraph -claim'}}, + {...event,comment:{...event.comment,body:'@MathGraph /check extra'}}, + {...event,comment:{...event.comment,user:{id:12,type:'User'}}}, + {...event,comment:{...event.comment,updated_at:'2026-11-05T01:16:20Z'}}, + {...event,comment:{...event.comment,id:'6657771222'}} + ])assert.throws(()=>validateOwnedSlashEvent({event:modified,actorId:String(owner),runAttempt:'1'}), + /OWNED_SLASH_EVENT_REFUSED/); + assert.throws(()=>validateOwnedSlashEvent({event,actorId:String(owner),runAttempt:'2'}),/OWNED_SLASH_EVENT_REFUSED/); + assert.throws(()=>validateOwnedSlashEvent({event,actorId:'9',runAttempt:'1'}),/OWNED_SLASH_EVENT_REFUSED/); +}); + +test('slash event binds dynamically selected comment without expanding the GitHub API read surface',async()=>{ + const {makeOwnedGitHubApi}=await import('./owned-comment-cli.mjs'); + const requested=[]; + const api=makeOwnedGitHubApi({token:'test-token-with-no-real-access',commentId,fetcher:async(url)=>{ + requested.push(url);return {ok:true,json:async()=>({id:commentId})};}}); + await api(`/repos/${repo}/issues/comments/${commentId}`); + await assert.rejects(api(`/repos/${repo}/issues/comments/6645637811`),/OWNED_API_SURFACE_REFUSED/); + await assert.rejects(api(`/repos/${repo}/issues/comments/${commentId+1}`),/OWNED_API_SURFACE_REFUSED/); + assert.equal(requested.length,1); +}); + +test('preflight reauthenticates actual slash bytes and freezes exact source without arbitrary execution',async()=>{ + const {prepareOwnedSlash}=await import('./slash-owned-cli.mjs'); + const root=await mkdtemp(join(tmpdir(),'omm-owned-slash-')); + const head='2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17'; + const base='dfd87d1e921953badedcfae21ade76bb38f5c04a'; + const qualified=()=>({request:{schema:'mathgraph.omm.request.v1',repository:repo,number:239, + command:'check',head_sha:head,base_sha:base, + candidate_source_sha256:'e45efb73729fc98e45e66d216e792e70f67f8c43d4c12cbb753ac410b950e4d9'}, + source:artinStrongSource,publishedSource:'published verified Artin exercise'}); + const api=async path=>{ + if(path===`/repos/${repo}/issues/comments/${commentId}`)return event.comment; + if(path===`/repos/${repo}/pulls/239`)return {number:239,state:'open',user:{id:owner,type:'User'}, + head:{sha:head,repo:{full_name:repo}},base:{sha:base,repo:{full_name:repo}}}; + throw Error('UNEXPECTED_API_PATH'); + }; + try{ + const admission=await prepareOwnedSlash({directory:join(root,'accepted'),event, + actorId:String(owner),runAttempt:'1',api,qualifyRegistered:async()=>qualified()}); + assert.equal(admission.comment_id,commentId); + assert.equal(admission.intent_fidelity,'UNKNOWN'); + assert.equal(await readFile(join(root,'accepted','candidate.lean.txt'),'utf8'),artinStrongSource); + await assert.rejects(prepareOwnedSlash({directory:join(root,'mismatch'),event, + actorId:String(owner),runAttempt:'1',api:async path=>path.includes('/issues/comments/')? + {...event.comment,created_at:'2026-11-05T01:15:30Z',updated_at:'2026-11-05T01:15:30Z'}:api(path), + qualifyRegistered:async()=>qualified()}),/OWNED_EVENT_API_MISMATCH/); + await assert.rejects(prepareOwnedSlash({directory:join(root,'legacy'),event, + actorId:String(owner),runAttempt:'1',api:async path=>path.includes('/issues/comments/')? + {...event.comment,body:'@MathGraph -claim'}:api(path),qualifyRegistered:async()=>qualified()}), + /OWNED_COMMENT_IDENTITY_UNQUALIFIED/); + }finally{await rm(root,{recursive:true,force:true});} +}); + +test('slash Check has distinct identity, binds the comment, and remains neutral/UNKNOWN',async()=>{ + const {buildOwnedCommentCheck}=await import('./owned-comment-publish.mjs'); + const admission={schema:'mathgraph.omm.owned-comment-admission.v1',status:'SOURCE_ADMITTED_ONLY', + repository:repo,number:239,comment_id:commentId,comment_created_at:'2026-11-05T01:15:20Z', + comment_updated_at:'2026-11-05T01:15:20Z', + head_sha:'2f6aaba0d71f1c0b4c0db58618a8981ccd59dc17', + base_sha:'dfd87d1e921953badedcfae21ade76bb38f5c04a', + source_sha256:'e45efb73729fc98e45e66d216e792e70f67f8c43d4c12cbb753ac410b950e4d9', + native_receipt:'UNKNOWN',original_target:'UNKNOWN',intent_fidelity:'UNKNOWN', + evidence_warrant:'UNKNOWN',repair_permission:'DENIED',publication_permission:'DENIED'}; + const assessment={schema:'mathgraph.omm.claim-assessment.v1',status:'NATIVE_RECEIPT_BOUND', + repository:repo,number:239,head_sha:admission.head_sha,base_sha:admission.base_sha, + candidate_sha256:admission.source_sha256,candidate_proof:'LEAN_COMPILED', + original_target:'NOT_ESTABLISHED_IN_THIS_REPLAY',formal_statement_relation:'UNKNOWN', + intent_fidelity:'UNKNOWN',evidence_warrant:'UNKNOWN',candidate_dependence:'UNKNOWN', + native_receipt_sha256:'a'.repeat(64)}; + const url=`https://github.com/${repo}/actions/runs/41999990001`; + const previous=buildOwnedCommentCheck({admission,assessment,runUrl:url}); + const next=buildOwnedCommentCheck({admission,assessment,runUrl:url,channel:'slash-v1'}); + assert.equal(previous.name,'MathGraph Check / OMM owner opt-in (advisory)'); + assert.match(previous.output.summary,/not an active issue_comment listener/); + assert.equal(next.name,'MathGraph Check / OMM \/check (advisory)'); + assert.notEqual(next.external_id,previous.external_id); + assert.equal(next.status,'completed');assert.equal(next.conclusion,'neutral'); + assert.match(next.output.summary,/issuecomment-6657771222/); + assert.match(next.output.summary,/issue_comment listener/); + assert.match(next.output.summary,/intent \| UNKNOWN/); + assert.match(next.output.summary,/repair permission \| DENIED/i); + assert.doesNotMatch(next.output.summary,/not an active issue_comment listener/); + assert.throws(()=>buildOwnedCommentCheck({admission,assessment,runUrl:url,channel:'arbitrary'}), + /OWNED_OPTIN_NOT_QUALIFIED/); +}); +