diff --git a/.github/workflows/discussion-draft.lock.yml b/.github/workflows/discussion-draft.lock.yml index e336e8de..872920b8 100644 --- a/.github/workflows/discussion-draft.lock.yml +++ b/.github/workflows/discussion-draft.lock.yml @@ -1,5 +1,5 @@ -# gh-aw-metadata: {"schema_version":"v4","frontmatter_hash":"7fd31a0e1074ecf9917cb3b4e812be63a00d1aec07cc5bc6d5ed98f40d9cbc73","body_hash":"e55e7fae6d5aa41a4f8265903890ddb3474f9a49ffc1854e7cbc423d2077c30b","compiler_version":"v0.93.5","strict":true,"agent_id":"copilot","agent_model":"gpt-6.1-sol","engine_versions":{"copilot":"1.0.101"}} -# gh-aw-manifest: {"version":1,"secrets":["GH_AW_DEFAULT_OTLP_ENDPOINT","GH_AW_DEFAULT_OTLP_HEADERS","GH_AW_GITHUB_MCP_SERVER_TOKEN","GH_AW_GITHUB_TOKEN","GITHUB_TOKEN"],"actions":[{"repo":"actions/checkout","sha":"3d3c42e5aac5ba805825da76410c181273ba90b1","version":"v7.0.1"},{"repo":"actions/checkout","sha":"d23441a48e516b6c34aea4fa41551a30e30af803","version":"v6.1.0"},{"repo":"actions/download-artifact","sha":"9000827ccba6bdab643e8b6fd33ac0654aef8333","version":"v8.0.2"},{"repo":"actions/github-script","sha":"3a2844b7e9c422d3c10d287c895573f7108da1b3","version":"v9.0.0"},{"repo":"actions/setup-node","sha":"949feb2413d6458794dcd2491c4babbbce0c15c1","version":"v7.1.0"},{"repo":"actions/upload-artifact","sha":"cf430e030ddbb5b0abf93d22962f4752f3646cd9","version":"v7.0.2"},{"repo":"github/gh-aw/actions/setup","sha":"32ecafbc061c9af57e9361942099e381102ae304","version":"32ecafbc061c9af57e9361942099e381102ae304"}],"containers":[{"image":"ghcr.io/github/gh-aw-firewall/agent:0.28.70","digest":"sha256:b15ab48cbbab02904eb9bbd359a398db59690eebbea1e6934d47ed2bb407dd34","pinned_image":"ghcr.io/github/gh-aw-firewall/agent:0.28.70@sha256:b15ab48cbbab02904eb9bbd359a398db59690eebbea1e6934d47ed2bb407dd34"},{"image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.28.70","digest":"sha256:9ac8f9c7294e892c916de3da9d2588dfd8cd4931e5c39b00143266d7ed9fd52a","pinned_image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.28.70@sha256:9ac8f9c7294e892c916de3da9d2588dfd8cd4931e5c39b00143266d7ed9fd52a"},{"image":"ghcr.io/github/gh-aw-firewall/squid:0.28.70","digest":"sha256:b5bc53f4becfd96e926e63c83831f1b3c9512f713c83161d2b8c9b7204b3e917","pinned_image":"ghcr.io/github/gh-aw-firewall/squid:0.28.70@sha256:b5bc53f4becfd96e926e63c83831f1b3c9512f713c83161d2b8c9b7204b3e917"},{"image":"ghcr.io/github/gh-aw-mcpg:v0.4.37","digest":"sha256:9c7f242f6dec2d7d7b55d4c9e07872d59b85377c6cb3de46c915a5d1ffb91442","pinned_image":"ghcr.io/github/gh-aw-mcpg:v0.4.37@sha256:9c7f242f6dec2d7d7b55d4c9e07872d59b85377c6cb3de46c915a5d1ffb91442"},{"image":"ghcr.io/github/gh-aw-node","digest":"sha256:23746305eeded20dc2726ab18bb081962d66ee3b2e09a54291afd1869ab3ef6d","pinned_image":"ghcr.io/github/gh-aw-node@sha256:23746305eeded20dc2726ab18bb081962d66ee3b2e09a54291afd1869ab3ef6d"},{"image":"ghcr.io/github/github-mcp-server:v2.0.3","digest":"sha256:250bc6ce70788207608ef74cef4397230ff2e8481218da74d6093e4bd9101132","pinned_image":"ghcr.io/github/github-mcp-server:v2.0.3@sha256:250bc6ce70788207608ef74cef4397230ff2e8481218da74d6093e4bd9101132"}],"mcp_servers":[{"name":"github","tools":["get_discussion","get_discussion_comments","get_file_contents","issue_read","list_issues","list_pull_requests","pull_request_read","search_issues","search_pull_requests"]},{"name":"safeoutputs","tools":["noop","report_incomplete","report_missing_data"]}],"threat_detection":{"mode":"enabled"},"admission":false} +# gh-aw-metadata: {"schema_version":"v4","frontmatter_hash":"b3e5c051b1bfaff6a729a15c438248e7d4f2f32e783c8cfec7d47d321009367f","body_hash":"e55e7fae6d5aa41a4f8265903890ddb3474f9a49ffc1854e7cbc423d2077c30b","compiler_version":"v0.93.5","strict":true,"agent_id":"copilot","agent_model":"gpt-6.1-sol","engine_versions":{"copilot":"1.0.101"}} +# gh-aw-manifest: {"version":1,"secrets":["GH_AW_DEFAULT_OTLP_ENDPOINT","GH_AW_DEFAULT_OTLP_HEADERS","GH_AW_GITHUB_MCP_SERVER_TOKEN","GH_AW_GITHUB_TOKEN","GITHUB_TOKEN"],"actions":[{"repo":"actions/checkout","sha":"3d3c42e5aac5ba805825da76410c181273ba90b1","version":"v7.0.1"},{"repo":"actions/checkout","sha":"d23441a48e516b6c34aea4fa41551a30e30af803","version":"d23441a48e516b6c34aea4fa41551a30e30af803"},{"repo":"actions/download-artifact","sha":"9000827ccba6bdab643e8b6fd33ac0654aef8333","version":"9000827ccba6bdab643e8b6fd33ac0654aef8333"},{"repo":"actions/github-script","sha":"3a2844b7e9c422d3c10d287c895573f7108da1b3","version":"3a2844b7e9c422d3c10d287c895573f7108da1b3"},{"repo":"actions/setup-node","sha":"949feb2413d6458794dcd2491c4babbbce0c15c1","version":"v7.1.0"},{"repo":"actions/upload-artifact","sha":"cf430e030ddbb5b0abf93d22962f4752f3646cd9","version":"cf430e030ddbb5b0abf93d22962f4752f3646cd9"},{"repo":"github/gh-aw/actions/setup","sha":"32ecafbc061c9af57e9361942099e381102ae304","version":"32ecafbc061c9af57e9361942099e381102ae304"}],"containers":[{"image":"ghcr.io/github/gh-aw-firewall/agent:0.28.70","digest":"sha256:b15ab48cbbab02904eb9bbd359a398db59690eebbea1e6934d47ed2bb407dd34","pinned_image":"ghcr.io/github/gh-aw-firewall/agent:0.28.70@sha256:b15ab48cbbab02904eb9bbd359a398db59690eebbea1e6934d47ed2bb407dd34"},{"image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.28.70","digest":"sha256:9ac8f9c7294e892c916de3da9d2588dfd8cd4931e5c39b00143266d7ed9fd52a","pinned_image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.28.70@sha256:9ac8f9c7294e892c916de3da9d2588dfd8cd4931e5c39b00143266d7ed9fd52a"},{"image":"ghcr.io/github/gh-aw-firewall/squid:0.28.70","digest":"sha256:b5bc53f4becfd96e926e63c83831f1b3c9512f713c83161d2b8c9b7204b3e917","pinned_image":"ghcr.io/github/gh-aw-firewall/squid:0.28.70@sha256:b5bc53f4becfd96e926e63c83831f1b3c9512f713c83161d2b8c9b7204b3e917"},{"image":"ghcr.io/github/gh-aw-mcpg:v0.4.37","digest":"sha256:9c7f242f6dec2d7d7b55d4c9e07872d59b85377c6cb3de46c915a5d1ffb91442","pinned_image":"ghcr.io/github/gh-aw-mcpg:v0.4.37@sha256:9c7f242f6dec2d7d7b55d4c9e07872d59b85377c6cb3de46c915a5d1ffb91442"},{"image":"ghcr.io/github/gh-aw-node","digest":"sha256:23746305eeded20dc2726ab18bb081962d66ee3b2e09a54291afd1869ab3ef6d","pinned_image":"ghcr.io/github/gh-aw-node@sha256:23746305eeded20dc2726ab18bb081962d66ee3b2e09a54291afd1869ab3ef6d"},{"image":"ghcr.io/github/github-mcp-server:v2.0.3","digest":"sha256:250bc6ce70788207608ef74cef4397230ff2e8481218da74d6093e4bd9101132","pinned_image":"ghcr.io/github/github-mcp-server:v2.0.3@sha256:250bc6ce70788207608ef74cef4397230ff2e8481218da74d6093e4bd9101132"}],"mcp_servers":[{"name":"github","tools":["get_discussion","get_discussion_comments","get_file_contents","issue_read","list_issues","list_pull_requests","pull_request_read","search_issues","search_pull_requests"]},{"name":"safeoutputs","tools":["noop","report_incomplete","report_missing_data"]}],"threat_detection":{"mode":"enabled"},"admission":false} # This file was automatically generated by gh-aw (v0.93.5). DO NOT EDIT. To debug this workflow, load the skill at https://github.com/github/gh-aw/blob/main/debug.md # # To update this file, edit the corresponding .md file and run: @@ -23,10 +23,14 @@ # # Custom actions used: # - actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 +# - actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # - actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 +# - actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # - actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 +# - actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # - actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 # - actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 +# - actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # - actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 # - github/gh-aw/actions/setup@32ecafbc061c9af57e9361942099e381102ae304 # 32ecafbc061c9af57e9361942099e381102ae304 # @@ -301,7 +305,7 @@ jobs: GH_AW_GITHUB_RUN_ID: ${{ github.run_id }} GH_AW_GITHUB_WORKSPACE: ${{ github.workspace }} GH_AW_PROMPT_CONTENT_0000: "\n" - GH_AW_PROMPT_CONTENT_0001: "\nTools: report_missing_data, noop(max:2)\n" + GH_AW_PROMPT_CONTENT_0001: "\nTools: report_missing_data, noop(max:3)\n" GH_AW_PROMPT_CONTENT_0002: "\n" GH_AW_PROMPT_CONTENT_0003: "\nThe following GitHub context information is available for this workflow:\n{{#if github.actor}}\n- **actor**: __GH_AW_GITHUB_ACTOR__\n{{/if}}\n{{#if github.repository}}\n- **repository**: __GH_AW_GITHUB_REPOSITORY__\n{{/if}}\n{{#if github.workspace}}\n- **workspace**: __GH_AW_GITHUB_WORKSPACE__\n{{/if}}\n{{#if github.event.issue.number || (github.aw.context.item_type == 'issue' && github.aw.context.item_number)}}\n- **issue-number**: #__GH_AW_EXPR_7C248226__\n{{/if}}\n{{#if github.event.discussion.number || (github.aw.context.item_type == 'discussion' && github.aw.context.item_number)}}\n- **discussion-number**: #__GH_AW_EXPR_C19C384F__\n{{/if}}\n{{#if github.event.pull_request.number || (github.aw.context.item_type == 'pull_request' && github.aw.context.item_number)}}\n- **pull-request-number**: #__GH_AW_EXPR_76DF9333__\n{{/if}}\n{{#if github.event.comment.id || github.aw.context.comment_id}}\n- **comment-id**: __GH_AW_EXPR_77C1A4D2__\n{{/if}}\n{{#if github.run_id}}\n- **workflow-run-id**: __GH_AW_GITHUB_RUN_ID__\n{{/if}}\n\n\n" GH_AW_PROMPT_CONTENT_0004: "\n" @@ -570,6 +574,13 @@ jobs: env: GH_AW_SKILL_DIR: ".github/skills" run: bash "${RUNNER_TEMP}/gh-aw/actions/restore_inline_skills.sh" + - env: + GH_AW_SAFE_OUTPUTS: ${{ steps.set-runtime-paths.outputs.GH_AW_SAFE_OUTPUTS }} + id: require_candidate_detection + name: Require candidate threat detection + run: "const fs = require(\"node:fs\");\nconst path = require(\"node:path\");\nfs.mkdirSync(path.dirname(process.env.GH_AW_SAFE_OUTPUTS), { recursive: true });\nfs.appendFileSync(process.env.GH_AW_SAFE_OUTPUTS, JSON.stringify({\n type: \"noop\",\n message: \"This read-only workflow requires threat detection of its candidate artifact before sealing.\"\n}) + \"\\n\");\n" + shell: node {0} + - name: Download container images run: bash "${RUNNER_TEMP}/gh-aw/actions/download_docker_images.sh" ghcr.io/github/gh-aw-firewall/agent:0.28.70@sha256:b15ab48cbbab02904eb9bbd359a398db59690eebbea1e6934d47ed2bb407dd34 ghcr.io/github/gh-aw-firewall/api-proxy:0.28.70@sha256:9ac8f9c7294e892c916de3da9d2588dfd8cd4931e5c39b00143266d7ed9fd52a ghcr.io/github/gh-aw-firewall/squid:0.28.70@sha256:b5bc53f4becfd96e926e63c83831f1b3c9512f713c83161d2b8c9b7204b3e917 ghcr.io/github/gh-aw-mcpg:v0.4.37@sha256:9c7f242f6dec2d7d7b55d4c9e07872d59b85377c6cb3de46c915a5d1ffb91442 ghcr.io/github/gh-aw-node@sha256:23746305eeded20dc2726ab18bb081962d66ee3b2e09a54291afd1869ab3ef6d ghcr.io/github/github-mcp-server:v2.0.3@sha256:250bc6ce70788207608ef74cef4397230ff2e8481218da74d6093e4bd9101132 - name: Prepare Safe Outputs Directories @@ -582,7 +593,7 @@ jobs: env: GH_AW_FILE_ROOT: "${{ runner.temp }}/gh-aw" GH_AW_FILE_CONFIG: "{\"files\":[{\"path\":\"safeoutputs/config.json\",\"content_env\":\"GH_AW_SAFE_OUTPUTS_CONFIG\"}]}" - GH_AW_SAFE_OUTPUTS_CONFIG: "{\"noop\":{\"max\":2,\"report-as-issue\":\"false\"},\"report_incomplete\":{},\"report_missing_data\":{}}" + GH_AW_SAFE_OUTPUTS_CONFIG: "{\"noop\":{\"max\":3,\"report-as-issue\":\"false\"},\"report_incomplete\":{},\"report_missing_data\":{}}" with: script: | const path = require('path'); @@ -1284,7 +1295,7 @@ jobs: uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 env: GH_AW_AGENT_OUTPUT: ${{ steps.setup-agent-output-env.outputs.GH_AW_AGENT_OUTPUT }} - GH_AW_NOOP_MAX: "2" + GH_AW_NOOP_MAX: "3" GH_AW_WORKFLOW_NAME: "Prepare discussion draft" GH_AW_WORKFLOW_SOURCE_URL: "${{ github.server_url }}/${{ github.repository }}/blob/${{ github.ref_name }}/.github/workflows/discussion-draft.md" GH_AW_ENGINE_ID: "copilot" @@ -1305,25 +1316,6 @@ jobs: setupGlobals(core, github, context, exec, io, getOctokit); const { main } = require(path.join(actionsDir, 'handle_noop_message.cjs')); await main(); - - name: Log detection run - id: detection_runs - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_AGENT_OUTPUT: ${{ steps.setup-agent-output-env.outputs.GH_AW_AGENT_OUTPUT }} - GH_AW_WORKFLOW_NAME: "Prepare discussion draft" - GH_AW_WORKFLOW_SOURCE_URL: "${{ github.server_url }}/${{ github.repository }}/blob/${{ github.ref_name }}/.github/workflows/discussion-draft.md" - GH_AW_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} - GH_AW_DETECTION_CONCLUSION: ${{ needs.detection.outputs.detection_conclusion }} - GH_AW_DETECTION_REASON: ${{ needs.detection.outputs.detection_reason }} - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const path = require('path'); - const actionsDir = path.join(process.env.RUNNER_TEMP, 'gh-aw', 'actions'); - const { setupGlobals } = require(path.join(actionsDir, 'setup_globals.cjs')); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require(path.join(actionsDir, 'handle_detection_runs.cjs')); - await main(); - name: Record incomplete id: report_incomplete uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 @@ -1598,6 +1590,33 @@ jobs: if: always() && steps.detection_guard.outputs.run_detection == 'true' run: | bash "${RUNNER_TEMP}/gh-aw/actions/prepare_threat_detection_files.sh" + - if: always() && steps.detection_guard.outputs.run_detection == 'true' + uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 + with: + persist-credentials: false + ref: ${{ github.sha }} + - name: Download candidate for detection + if: always() && steps.detection_guard.outputs.run_detection == 'true' + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 + with: + name: typechat-discussion-candidate-${{ github.run_attempt }} + path: ${{ runner.temp }}/typechat-detection-candidate + - name: Stage the exact candidate for detection + id: stage_candidate + if: always() && steps.detection_guard.outputs.run_detection == 'true' + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 + env: + TYPECHAT_DETECTION_DIRECTORY: /tmp/gh-aw/threat-detection + with: + github-token: ${{ github.token }} + retries: 0 + script: | + const path = require("node:path"); + const { pathToFileURL } = require("node:url"); + const helper = await import(pathToFileURL(path.join(process.env.GITHUB_WORKSPACE, "tools", "discussion-draft.mjs")).href); + const directory = path.join(process.env.RUNNER_TEMP, "typechat-detection-candidate"); + const hash = helper.stageCandidateForDetection(directory, process.env.TYPECHAT_DETECTION_DIRECTORY); + core.setOutput("sha256", hash); - name: Setup threat detection id: detection_setup if: always() && steps.detection_guard.outputs.run_detection == 'true' @@ -1606,7 +1625,7 @@ jobs: WORKFLOW_NAME: "Prepare discussion draft" WORKFLOW_DESCRIPTION: "Prepare a read-only issue or discussion draft for explicit human approval." HAS_PATCH: ${{ needs.agent.outputs.has_patch }} - GH_AW_DETECTION_CONTINUE_ON_ERROR: "true" + GH_AW_DETECTION_CONTINUE_ON_ERROR: "false" GH_AW_DETECTION_SKIP_PROMPT_SUMMARY: "true" with: script: | @@ -1636,7 +1655,6 @@ jobs: - name: Install threat-detect binary id: threat_detect_install if: always() && steps.detection_guard.outputs.run_detection == 'true' - continue-on-error: true run: | bash "${RUNNER_TEMP}/gh-aw/actions/install_threat_detect_binary.sh" v0.5.3 --artifact-base-url https://github.com/github/gh-aw-threat-detection/releases/download --sha256-amd64 7cfab3bd02717968a0584ddd3790cff22550059e36d6f656c4f35e9d27851009 --sha256-arm64 6f355949af98e7b5527a958ac11431bd7867db45c0191b94c3a205c662181cc6 - name: Execute threat detection with AWF @@ -1677,7 +1695,7 @@ jobs: WORKFLOW_NAME: "Prepare discussion draft" WORKFLOW_DESCRIPTION: "Prepare a read-only issue or discussion draft for explicit human approval." HAS_PATCH: ${{ needs.agent.outputs.has_patch }} - GH_AW_DETECTION_CONTINUE_ON_ERROR: "true" + GH_AW_DETECTION_CONTINUE_ON_ERROR: "false" run: | mkdir -p "/tmp/gh-aw/threat-detection" evidence_tmp="/tmp/gh-aw/threat-detection/execution.json.tmp" @@ -1744,6 +1762,33 @@ jobs: mkdir -p /tmp/gh-aw/threat-detection/sandbox/firewall if [ -d /tmp/gh-aw/sandbox/firewall/logs ]; then mkdir -p /tmp/gh-aw/threat-detection/sandbox/firewall/logs && cp -r /tmp/gh-aw/sandbox/firewall/logs/. /tmp/gh-aw/threat-detection/sandbox/firewall/logs/; fi if [ -d /tmp/gh-aw/sandbox/firewall/audit ]; then mkdir -p /tmp/gh-aw/threat-detection/sandbox/firewall/audit && cp -r /tmp/gh-aw/sandbox/firewall/audit/. /tmp/gh-aw/threat-detection/sandbox/firewall/audit/; fi + - name: Record the candidate scan + id: candidate_scan + if: success() && steps.stage_candidate.outcome == 'success' && steps.detection_agentic_execution.outcome == 'success' + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 + env: + TYPECHAT_CANDIDATE_SHA256: ${{ steps.stage_candidate.outputs.sha256 }} + TYPECHAT_SCAN_OUTCOME: ${{ steps.detection_agentic_execution.outcome }} + with: + github-token: ${{ github.token }} + retries: 0 + script: | + const path = require("node:path"); + const { pathToFileURL } = require("node:url"); + const helper = await import(pathToFileURL(path.join(process.env.GITHUB_WORKSPACE, "tools", "discussion-draft.mjs")).href); + const scan = helper.createCandidateScan(process.env.TYPECHAT_CANDIDATE_SHA256, process.env.TYPECHAT_SCAN_OUTCOME, context.runId, process.env.GITHUB_RUN_ATTEMPT); + const directory = path.join(process.env.RUNNER_TEMP, "typechat-candidate-scan"); + helper.writeJson(directory, "candidate-scan.json", scan); + core.setOutput("path", path.join(directory, "candidate-scan.json")); + - name: Upload candidate scan binding + if: success() && steps.candidate_scan.outcome == 'success' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 + with: + if-no-files-found: error + name: typechat-discussion-scan-${{ github.run_attempt }} + overwrite: false + path: ${{ steps.candidate_scan.outputs.path }} + retention-days: ${{ vars.GH_AW_DEFAULT_ARTIFACT_RETENTION_DAYS || '7' }} - name: Redact secrets in detection logs id: redact_detection_logs if: always() && steps.detection_guard.outputs.run_detection == 'true' @@ -1826,12 +1871,11 @@ jobs: - name: Conclude threat detection id: detection_conclusion if: always() && steps.detection_guard.outputs.run_detection == 'false' || (always() && steps.detection_guard.outputs.run_detection == 'true' && steps.redact_detection_logs.outcome == 'success' && steps.redact_detection_artifacts.outcome == 'success') - continue-on-error: true env: RUN_DETECTION: ${{ steps.detection_guard.outputs.run_detection }} DETECTION_AGENTIC_EXECUTION_OUTCOME: ${{ steps.detection_agentic_execution.outcome }} THREAT_DETECT_INSTALL_OUTCOME: ${{ steps.threat_detect_install.outcome }} - GH_AW_DETECTION_CONTINUE_ON_ERROR: "true" + GH_AW_DETECTION_CONTINUE_ON_ERROR: "false" run: | bash "${RUNNER_TEMP}/gh-aw/actions/conclude_threat_detection.sh" /tmp/gh-aw/threat-detection/detection_result.json - name: Upload skipped detection execution evidence @@ -2090,7 +2134,7 @@ jobs: GH_AW_ALLOWED_DOMAINS: "api.snapcraft.io,archive.ubuntu.com,azure.archive.ubuntu.com,crl.geotrust.com,crl.globalsign.com,crl.identrust.com,crl.sectigo.com,crl.thawte.com,crl.usertrust.com,crl.verisign.com,crl3.digicert.com,crl4.digicert.com,crls.ssl.com,json-schema.org,json.schemastore.org,keyserver.ubuntu.com,ocsp.digicert.com,ocsp.geotrust.com,ocsp.globalsign.com,ocsp.identrust.com,ocsp.sectigo.com,ocsp.ssl.com,ocsp.thawte.com,ocsp.usertrust.com,ocsp.verisign.com,packagecloud.io,packages.cloud.google.com,packages.microsoft.com,ppa.launchpad.net,s.symcb.com,s.symcd.com,security.ubuntu.com,ts-crl.ws.symantec.com,ts-ocsp.ws.symantec.com,www.googleapis.com" GITHUB_SERVER_URL: ${{ github.server_url }} GITHUB_API_URL: ${{ github.api_url }} - GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG: "{\"noop\":{\"max\":2,\"report-as-issue\":\"false\"},\"report_incomplete\":{},\"report_missing_data\":{}}" + GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG: "{\"noop\":{\"max\":3,\"report-as-issue\":\"false\"},\"report_incomplete\":{},\"report_missing_data\":{}}" GH_AW_GITHUB_TOKEN_SOURCE: "github_actions" with: github-token: ${{ secrets.GITHUB_TOKEN }} @@ -2119,7 +2163,7 @@ jobs: - agent - detection - prepare - if: "${{ needs.detection.result == 'success' && needs.detection.outputs.detection_success == 'true' && (needs.agent.result == 'success' && needs.detection.result == 'success') }}" + if: "${{ needs.detection.result == 'success' && needs.detection.outputs.detection_success == 'true' && (needs.agent.result == 'success' && needs.detection.result == 'success' && needs.detection.outputs.run_detection == 'true' && needs.detection.outputs.detection_conclusion == 'success') }}" runs-on: ubuntu-latest # Permissions for the seal job (workflow permissions default to none). permissions: @@ -2153,6 +2197,11 @@ jobs: with: name: typechat-discussion-candidate-${{ github.run_attempt }} path: ${{ runner.temp }}/typechat-seal-candidate + - name: Download candidate scan binding + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: typechat-discussion-scan-${{ github.run_attempt }} + path: ${{ runner.temp }}/typechat-seal-scan - name: Validate and seal the draft id: seal uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 @@ -2164,7 +2213,8 @@ jobs: const { pathToFileURL } = require("node:url"); const helper = await import(pathToFileURL(path.join(process.env.GITHUB_WORKSPACE, "tools", "discussion-draft.mjs")).href); const prepared = helper.readJson(path.join(process.env.RUNNER_TEMP, "typechat-seal-context"), "context.json", helper.MAX_CONTEXT_BYTES); - const candidate = helper.readJson(path.join(process.env.RUNNER_TEMP, "typechat-seal-candidate"), "discussion-draft.json", helper.MAX_DRAFT_BYTES); + const scan = helper.readJson(path.join(process.env.RUNNER_TEMP, "typechat-seal-scan"), "candidate-scan.json", 1024); + const candidate = helper.readDetectedDraft(path.join(process.env.RUNNER_TEMP, "typechat-seal-candidate"), scan, context.runId, process.env.GITHUB_RUN_ATTEMPT); const bundle = await helper.sealDraft(github, context, process.env, prepared, candidate); const directory = path.join(process.env.RUNNER_TEMP, "typechat-sealed-draft"); helper.writeJson(directory, "sealed-draft.json", bundle); diff --git a/.github/workflows/discussion-draft.md b/.github/workflows/discussion-draft.md index 8e48a58f..1360971a 100644 --- a/.github/workflows/discussion-draft.md +++ b/.github/workflows/discussion-draft.md @@ -26,6 +26,68 @@ permissions: copilot-requests: write imports: - shared/read-only-analysis.md +safe-outputs: + report-missing-tool: false + noop: + # One trusted scan signal plus the two existing model no-ops. + max: 3 + report-as-issue: false + threat-detection: + continue-on-error: false + report-as-issue: false + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 + with: + ref: ${{ github.sha }} + persist-credentials: false + - name: Download candidate for detection + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: typechat-discussion-candidate-${{ github.run_attempt }} + path: ${{ runner.temp }}/typechat-detection-candidate + - name: Stage the exact candidate for detection + id: stage_candidate + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + env: + TYPECHAT_DETECTION_DIRECTORY: /tmp/gh-aw/threat-detection + with: + github-token: ${{ github.token }} + retries: 0 + script: | + const path = require("node:path"); + const { pathToFileURL } = require("node:url"); + const helper = await import(pathToFileURL(path.join(process.env.GITHUB_WORKSPACE, "tools", "discussion-draft.mjs")).href); + const directory = path.join(process.env.RUNNER_TEMP, "typechat-detection-candidate"); + const hash = helper.stageCandidateForDetection(directory, process.env.TYPECHAT_DETECTION_DIRECTORY); + core.setOutput("sha256", hash); + post-steps: + - name: Record the candidate scan + id: candidate_scan + if: success() && steps.stage_candidate.outcome == 'success' && steps.detection_agentic_execution.outcome == 'success' + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + env: + TYPECHAT_CANDIDATE_SHA256: ${{ steps.stage_candidate.outputs.sha256 }} + TYPECHAT_SCAN_OUTCOME: ${{ steps.detection_agentic_execution.outcome }} + with: + github-token: ${{ github.token }} + retries: 0 + script: | + const path = require("node:path"); + const { pathToFileURL } = require("node:url"); + const helper = await import(pathToFileURL(path.join(process.env.GITHUB_WORKSPACE, "tools", "discussion-draft.mjs")).href); + const scan = helper.createCandidateScan(process.env.TYPECHAT_CANDIDATE_SHA256, process.env.TYPECHAT_SCAN_OUTCOME, context.runId, process.env.GITHUB_RUN_ATTEMPT); + const directory = path.join(process.env.RUNNER_TEMP, "typechat-candidate-scan"); + helper.writeJson(directory, "candidate-scan.json", scan); + core.setOutput("path", path.join(directory, "candidate-scan.json")); + - name: Upload candidate scan binding + if: success() && steps.candidate_scan.outcome == 'success' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: typechat-discussion-scan-${{ github.run_attempt }} + path: ${{ steps.candidate_scan.outputs.path }} + if-no-files-found: error + overwrite: false + retention-days: 7 checkout: false network: defaults timeout-minutes: 60 @@ -88,7 +150,10 @@ jobs: seal: name: Seal discussion draft needs: [prepare, agent, detection] - if: needs.agent.result == 'success' && needs.detection.result == 'success' + if: >- + needs.agent.result == 'success' && needs.detection.result == 'success' && + needs.detection.outputs.run_detection == 'true' && + needs.detection.outputs.detection_conclusion == 'success' runs-on: ubuntu-latest timeout-minutes: 10 permissions: @@ -112,6 +177,11 @@ jobs: with: name: typechat-discussion-candidate-${{ github.run_attempt }} path: ${{ runner.temp }}/typechat-seal-candidate + - name: Download candidate scan binding + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: typechat-discussion-scan-${{ github.run_attempt }} + path: ${{ runner.temp }}/typechat-seal-scan - name: Validate and seal the draft id: seal uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 @@ -123,7 +193,8 @@ jobs: const { pathToFileURL } = require("node:url"); const helper = await import(pathToFileURL(path.join(process.env.GITHUB_WORKSPACE, "tools", "discussion-draft.mjs")).href); const prepared = helper.readJson(path.join(process.env.RUNNER_TEMP, "typechat-seal-context"), "context.json", helper.MAX_CONTEXT_BYTES); - const candidate = helper.readJson(path.join(process.env.RUNNER_TEMP, "typechat-seal-candidate"), "discussion-draft.json", helper.MAX_DRAFT_BYTES); + const scan = helper.readJson(path.join(process.env.RUNNER_TEMP, "typechat-seal-scan"), "candidate-scan.json", 1024); + const candidate = helper.readDetectedDraft(path.join(process.env.RUNNER_TEMP, "typechat-seal-candidate"), scan, context.runId, process.env.GITHUB_RUN_ATTEMPT); const bundle = await helper.sealDraft(github, context, process.env, prepared, candidate); const directory = path.join(process.env.RUNNER_TEMP, "typechat-sealed-draft"); helper.writeJson(directory, "sealed-draft.json", bundle); @@ -158,6 +229,21 @@ steps: artifact-ids: ${{ needs.prepare.outputs.context_artifact_id }} merge-multiple: true path: .typechat-draft +pre-agent-steps: + # The pinned detection guard only sees native output types, not file artifacts. + - name: Require candidate threat detection + id: require_candidate_detection + env: + GH_AW_SAFE_OUTPUTS: ${{ steps.set-runtime-paths.outputs.GH_AW_SAFE_OUTPUTS }} + shell: node {0} + run: | + const fs = require("node:fs"); + const path = require("node:path"); + fs.mkdirSync(path.dirname(process.env.GH_AW_SAFE_OUTPUTS), { recursive: true }); + fs.appendFileSync(process.env.GH_AW_SAFE_OUTPUTS, JSON.stringify({ + type: "noop", + message: "This read-only workflow requires threat detection of its candidate artifact before sealing." + }) + "\n"); post-steps: - name: Verify candidate draft file if: success() diff --git a/AUTOMATION.md b/AUTOMATION.md index f9e7ada4..12d38c0a 100644 --- a/AUTOMATION.md +++ b/AUTOMATION.md @@ -466,6 +466,8 @@ The workflow check also runs the contract tests in `tools/` (`npm ci --ignore-sc by `npm test`), including checks that the runtime pin matches the compilation command, generated drafting jobs cannot write to issues or pull requests, and the separate publisher has only its target-specific comment permission. +The discussion tests execute the compiled detection guard with Bash (Git Bash +on Windows), including a candidate with no model-emitted safe outputs or patch. The assessment itself remains report-only and does not change existing maintenance and deployment workflows. The separate @@ -516,11 +518,27 @@ recorded requirements, suggested criteria, and a readiness recommendation. Thread context is bounded at 1 MiB and 1,000 comments, including replies; incomplete or oversized context is an error, not silent truncation. -After successful agent execution and threat detection, a separate trusted job -validates the structured output and downloads the **original** context again. -It checks for changes, renders the comment, and seals its exact bytes and -provenance. Model output cannot choose the publishing target, owner, workflow -identity, or approval record. +Before model execution, a trusted step adds a read-only no-op to the native +safe-output stream. This makes the pinned runtime's detection guard run even +when the model writes only the candidate file and emits no safe outputs or patch. +The no-op limit reserves one slot for that signal in addition to the two existing +model no-ops; no-op issue reporting remains disabled. + +The detector downloads the attempt-specific, immutable candidate artifact. +Trusted code adds its complete JSON text to the detector's agent-output input, +preserving any native safe outputs. This includes file-only candidates, not just +text the model sends to safe-output tools. Sealing requires an executed, +successful scan with a successful verdict; skipped scans, warnings, and failures +cannot authorize sealing. + +After detection, a separate trusted job downloads that same candidate artifact +and checks its SHA-256 against an immutable scan-binding artifact from the same +run and attempt. The binding records the bytes staged for scanning and the +detector step's successful execution; the job's final verdict must also succeed. +The sealing job validates the structured output and downloads the **original** +context again. It checks for changes, renders the comment, and seals its exact +bytes and provenance. Model output cannot choose the publishing target, owner, +workflow identity, or approval record. The [publisher](./.github/workflows/publish-discussion-draft.yml) runs no model. Its Issue job has `issues: write`; its Discussion job has `discussions: write`. @@ -548,7 +566,7 @@ Publishing invokes no model. A **1,000-credit implementation budget** is a future owner-approved implementation-stage setting only; this pilot enables neither that spending nor a build command. -Context, candidate, and sealed draft artifacts are retained for seven days. +Context, candidate, scan-binding, and sealed draft artifacts are retained for seven days. Approval validity ends at the earlier of artifact expiry or seven days after artifact creation. Artifacts and Actions summaries follow repository visibility; they are not a private channel and must not contain secrets or sensitive data. @@ -559,6 +577,13 @@ must permit that retention; an upload or retention check failure prevents the write. The resulting receipt is also retained for 30 days. Do not delete publication runs or intent artifacts while the associated draft remains valid. +Intent names are keyed by the sealed draft's artifact ID, not the publishing run +or attempt; the intent document still records both. Each history check makes one +repository-artifact request filtered by that exact name and reads at most two +matches. Only one match can be the current upload, so a second match always blocks +another write. Missing, malformed, or incomplete history fails closed. This avoids +enumerating unrelated workflow runs while finding intents across runs and reruns. + An existing exact comment from the verified Actions bot makes a retry a no-op. A prior intent without a confirmed matching comment blocks another write, including reruns and new publish commands for that artifact. A timeout may mean diff --git a/tools/discussion-draft.mjs b/tools/discussion-draft.mjs index 07c740e9..ae032167 100644 --- a/tools/discussion-draft.mjs +++ b/tools/discussion-draft.mjs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +import { isUtf8 } from "node:buffer"; import { createHash } from "node:crypto"; import fs from "node:fs"; import path from "node:path"; @@ -561,15 +562,21 @@ export async function sealDraft(github, context, env, prepared, candidate, clock return bundle; } -export function readJson(directory, filename, maximum) { +function readArtifact(directory, filename, maximum) { const entries = fs.readdirSync(directory); requireValue(entries.length === 1 && entries[0] === filename, "Unexpected files in the downloaded artifact."); const file = path.join(directory, filename); const info = fs.lstatSync(file); requireValue(info.isFile() && !info.isSymbolicLink() && info.size > 0 && info.size <= maximum, "The artifact must contain one bounded regular JSON file."); + const bytes = fs.readFileSync(file); + requireValue(isUtf8(bytes), "The artifact must contain UTF-8 JSON."); + return bytes.toString("utf8"); +} + +function parseJson(text) { try { - return JSON.parse(fs.readFileSync(file, "utf8")); + return JSON.parse(text); } catch (error) { if (error instanceof SyntaxError) { @@ -579,6 +586,41 @@ export function readJson(directory, filename, maximum) { } } +export function readJson(directory, filename, maximum) { + return parseJson(readArtifact(directory, filename, maximum)); +} + +export function stageCandidateForDetection(directory, detectionDirectory) { + const text = readArtifact(directory, "discussion-draft.json", MAX_DRAFT_BYTES); + validateDraft(parseJson(text)); + const outputPath = path.join(detectionDirectory, "agent_output.json"); + const output = fs.existsSync(outputPath) ? parseJson(fs.readFileSync(outputPath, "utf8")) : { items: [] }; + requireValue(output && Array.isArray(output.items), "The detector's agent output is malformed."); + output.items.push({ type: "discussion-draft", content: text }); + fs.mkdirSync(detectionDirectory, { recursive: true }); + fs.writeFileSync(outputPath, JSON.stringify(output) + "\n"); + return digest(text); +} + +export function createCandidateScan(hash, outcome, runId, attempt) { + requireValue(typeof hash === "string" && SHA256.test(hash), "Missing candidate threat-scan hash."); + requireValue(outcome === "success", "The candidate threat scan did not execute successfully."); + return { + version: 1, sha256: hash, outcome, + runId: integer(Number(runId), "Invalid candidate scan run ID."), + attempt: integer(Number(attempt), "Invalid candidate scan attempt."), + }; +} + +export function readDetectedDraft(directory, scan, runId, attempt) { + exactKeys(scan, ["version", "sha256", "outcome", "runId", "attempt"], "Missing or malformed candidate threat-scan binding."); + requireValue(equal(scan, createCandidateScan(scan.sha256, scan.outcome, runId, attempt)), + "The candidate threat-scan binding belongs to a different run or attempt."); + const text = readArtifact(directory, "discussion-draft.json", MAX_DRAFT_BYTES); + requireValue(digest(text) === scan.sha256, "The candidate differs from the threat-scanned artifact. Request a new draft."); + return parseJson(text); +} + export function writeJson(directory, filename, value) { fs.mkdirSync(directory, { recursive: true }); fs.writeFileSync(path.join(directory, filename), JSON.stringify(value, null, 2) + "\n", { flag: "wx" }); @@ -664,60 +706,28 @@ export function publishedComment(thread, bundle) { return matches[0]; } -async function listArtifacts(github, runId) { - const artifacts = []; - let total; - for (let page = 1; page <= 10; page++) { - const data = (await github.rest.actions.listWorkflowRunArtifacts({ ...API_REPO, run_id: runId, per_page: 100, page })).data; - requireValue(Array.isArray(data.artifacts) && Number.isSafeInteger(data.total_count) && - data.total_count >= 0 && data.total_count <= 1000, "Artifact history is unavailable or exceeds the pilot limit."); - requireValue(total === undefined || total === data.total_count, "Artifact history changed during pagination."); - total = data.total_count; - artifacts.push(...data.artifacts); - if (artifacts.length === total) { - return artifacts; - } - requireValue(artifacts.length < total && data.artifacts.length === 100, "Artifact history is incomplete."); - } - throw new Error("Artifact history exceeds the pilot limit. Human inspection is required."); -} - async function priorIntent(github, publication, excludeId) { - const prefix = `${INTENT_PREFIX}${publication.artifact.id}-`; - const current = await listArtifacts(github, publication.request.runId); - const existing = current.find(artifact => artifact.id !== excludeId && artifact.name.startsWith(prefix)); - if (existing) { - return { runId: publication.request.runId, artifactId: existing.id }; - } + const name = `${INTENT_PREFIX}${publication.artifact.id}`; + // At most one result can be this run's new intent; any other match blocks publication. + const data = (await github.rest.actions.listArtifactsForRepo({ ...API_REPO, name, per_page: 2, page: 1 })).data; + requireValue(data && Array.isArray(data.artifacts) && Number.isSafeInteger(data.total_count) && + data.total_count >= 0 && data.artifacts.length === Math.min(data.total_count, 2), + "Publication intent history is unavailable or incomplete."); const seen = new Set(); - let total; - for (let page = 1; page <= 10; page++) { - const data = (await github.rest.actions.listWorkflowRuns({ - ...API_REPO, workflow_id: path.posix.basename(PUBLISH_WORKFLOW), actor: OWNER.login, - created: `>=${publication.artifact.created_at}`, per_page: 100, page, - })).data; - requireValue(Array.isArray(data.workflow_runs) && Number.isSafeInteger(data.total_count) && - data.total_count >= 0 && data.total_count <= 1000, "Publication history is unavailable or exceeds the pilot limit."); - requireValue(total === undefined || total === data.total_count, "Publication history changed during pagination."); - total = data.total_count; - for (const run of data.workflow_runs) { - integer(run.id, "Invalid publication history run ID."); - requireValue(!seen.has(run.id), "Publication history changed during pagination."); - seen.add(run.id); - if (run.id === publication.request.runId) { - continue; - } - const found = (await listArtifacts(github, run.id)).find(artifact => artifact.name.startsWith(prefix)); - if (found) { - return { runId: run.id, artifactId: found.id }; - } - } - if (seen.size === total) { - return null; - } - requireValue(seen.size < total && data.workflow_runs.length === 100, "Publication history is incomplete."); + for (const artifact of data.artifacts) { + requireValue(artifact && Number.isSafeInteger(artifact.id) && artifact.id > 0 && + artifact.name === name && !seen.has(artifact.id) && + artifact.workflow_run?.repository_id === REPOSITORY.id && + Number.isSafeInteger(artifact.workflow_run.id) && artifact.workflow_run.id > 0, + "Publication intent history contains invalid or duplicate artifacts."); + seen.add(artifact.id); + } + const prior = data.artifacts.find(artifact => artifact.id !== excludeId); + if (prior) { + return { runId: prior.workflow_run.id, artifactId: prior.id }; } - throw new Error("Publication history exceeds the pilot limit. No comment will be posted."); + requireValue(excludeId === undefined || seen.has(excludeId), "The current publication intent is missing from artifact history."); + return null; } async function verifyBundleContext(github, publication, bundle) { @@ -746,7 +756,7 @@ export async function checkPublication(github, context, env, bundle, clock = () requireValue(!prior, "A previous publication intent exists without a confirmed comment. Inspect that run; automatic retry is blocked."); return { shouldPost: true, - intentName: `${INTENT_PREFIX}${publication.artifact.id}-${publication.request.attempt}`, + intentName: `${INTENT_PREFIX}${publication.artifact.id}`, intent: { version: 1, artifactId: publication.artifact.id, bodyHash: bundle.bodyHash, target: bundle.target, approvalComment: publication.request.commentId, @@ -763,7 +773,7 @@ export async function publishDraft(github, context, env, bundle, intentId, clock } const intent = (await github.rest.actions.getArtifact({ ...API_REPO, artifact_id: integer(Number(intentId), "Missing durable publication intent.") })).data; requireValue(intent.id === Number(intentId) && - intent.name === `${INTENT_PREFIX}${publication.artifact.id}-${publication.request.attempt}` && + intent.name === `${INTENT_PREFIX}${publication.artifact.id}` && intent.workflow_run?.id === publication.request.runId && intent.workflow_run.repository_id === REPOSITORY.id && intent.workflow_run.head_sha === context.sha && intent.expired === false && diff --git a/tools/tests/discussion-draft.test.mjs b/tools/tests/discussion-draft.test.mjs index 86875671..69405be8 100644 --- a/tools/tests/discussion-draft.test.mjs +++ b/tools/tests/discussion-draft.test.mjs @@ -13,7 +13,7 @@ import { OWNER, REPOSITORY, ACTIONS_BOT_ID, DRAFT_WORKFLOW, PUBLISH_WORKFLOW, DRAFT_ARTIFACT_PREFIX, INTENT_PREFIX, CONTROL_PATHS, MAX_AGE_MS, MAX_BUNDLE_BYTES, authorize, parseCommand, digest, readThread, readControls, prepareDraft, validateDraft, renderDraft, sealDraft, identifyPublication, checkPublication, publishDraft, - readJson, writeJson, + readJson, writeJson, stageCandidateForDetection, createCandidateScan, readDetectedDraft, } from "../discussion-draft.mjs"; const SHA = "a".repeat(40); @@ -85,7 +85,7 @@ function setup(kind = "issue") { head_branch: "main", head_sha: SHA, actor: { id: OWNER.id, type: "User" }, triggering_actor: { id: OWNER.id, type: "User" }, }, - publisherRuns: [], posts: [], references: new Map(), comment, + historyRequests: [], posts: [], references: new Map(), comment, postError: null, persistBeforeError: false, onHistory: null, }; function event(action = "draft", body = "/typechat draft", commentId = "request", runId = 100, attempt = 1) { @@ -218,17 +218,15 @@ function setup(kind = "issue") { assert.equal(workflow_id, path.posix.basename(DRAFT_WORKFLOW)); return { data: { id: 900 } }; }, - listWorkflowRunArtifacts: async ({ run_id, page: pageNumber }) => { - const list = [...artifacts.values()].filter(item => item.workflow_run.id === run_id); - return { data: { artifacts: structuredClone(list.slice((pageNumber - 1) * 100, pageNumber * 100)), total_count: list.length } }; - }, - listWorkflowRuns: async ({ actor: login, workflow_id, page: pageNumber }) => { - assert.equal(login, OWNER.login); - assert.equal(workflow_id, path.posix.basename(PUBLISH_WORKFLOW)); + listArtifactsForRepo: async ({ name, per_page, page: pageNumber, ...repo }) => { + assert.deepEqual(repo, { owner: "microsoft", repo: "TypeChat" }); + assert.equal(per_page, 2); + assert.equal(pageNumber, 1); + state.historyRequests.push({ method: "listArtifactsForRepo", name, per_page, page: pageNumber }); state.onHistory?.(); + const list = [...artifacts.values()].filter(item => item.name === name); return { data: { - workflow_runs: structuredClone(state.publisherRuns.slice((pageNumber - 1) * 100, pageNumber * 100)), - total_count: state.publisherRuns.length, + artifacts: structuredClone(list.slice(0, per_page)), total_count: list.length, } }; }, }, @@ -518,14 +516,42 @@ test("a prior intent blocks a second attempt when no published comment can be co test("intents from earlier publisher runs also block duplicate writes", async () => { const fixture = await ready(); - fixture.state.publisherRuns.push({ id: 199 }); fixture.state.artifacts.set(499, { - id: 499, name: `${INTENT_PREFIX}500-1`, expired: false, + id: 499, name: `${INTENT_PREFIX}500`, expired: false, workflow_run: { id: 199, repository_id: REPOSITORY.id, head_sha: SHA }, }); await assert.rejects(checkPublication(fixture.github, fixture.publication.context, fixture.publication.env, fixture.bundle, clock), /previous publication intent/); }); +test("intents from earlier attempts of the same publisher run block reruns", async () => { + const fixture = await ready(); + const id = await reserve(fixture); + fixture.publication.env.GITHUB_RUN_ATTEMPT = "2"; + await assert.rejects(() => checkPublication(fixture.github, fixture.publication.context, fixture.publication.env, fixture.bundle, clock), + /previous publication intent/); + assert.equal(fixture.state.artifacts.get(id).name, `${INTENT_PREFIX}500`); + assert.equal(fixture.state.posts.length, 0); +}); + +test("the final intent lookup excludes only the current upload, not another run's intent", async () => { + for (const priorFirst of [false, true]) { + const fixture = await ready(); + const id = await reserve(fixture); + fixture.state.artifacts.set(499, { + id: 499, name: `${INTENT_PREFIX}500`, expired: false, + workflow_run: { id: 199, repository_id: REPOSITORY.id, head_sha: SHA }, + }); + if (priorFirst) { + const current = fixture.state.artifacts.get(id); + fixture.state.artifacts.delete(id); + fixture.state.artifacts.set(id, current); + } + await assert.rejects(() => publishDraft(fixture.github, fixture.publication.context, fixture.publication.env, fixture.bundle, id, clock), + /previous publication attempt/); + assert.equal(fixture.state.posts.length, 0); + } +}); + test("publication needs a current durable intent retained beyond the draft lifetime", async () => { const fixture = await ready(); const id = await reserve(fixture); @@ -599,26 +625,82 @@ test("a copied marker from another bot cannot impersonate an Actions publication test("missing or incomplete publication history fails closed", async () => { for (const data of [ - { workflow_runs: [] }, - { workflow_runs: [], total_count: 1 }, - { workflow_runs: [], total_count: 1001 }, + null, + { artifacts: [] }, + { artifacts: [], total_count: -1 }, + { artifacts: [], total_count: 1 }, + { artifacts: [], total_count: 1001 }, ]) { const fixture = await ready(); - fixture.github.rest.actions.listWorkflowRuns = async () => ({ data }); + fixture.github.rest.actions.listArtifactsForRepo = async () => ({ data }); await assert.rejects(() => checkPublication(fixture.github, fixture.publication.context, fixture.publication.env, fixture.bundle, clock), /history/); assert.equal(fixture.state.posts.length, 0); } }); -test("exactly 1,000 history entries are supported without silently ignoring an extra page", async () => { +test("invalid or duplicate filtered intent artifacts fail closed", async () => { const fixture = await ready(); - fixture.state.publisherRuns = Array.from({ length: 1000 }, (_, index) => ({ id: 1000 + index })); - const checked = await checkPublication(fixture.github, fixture.publication.context, fixture.publication.env, fixture.bundle, clock); - assert.equal(checked.shouldPost, true); - fixture.state.publisherRuns.push({ id: 2000 }); + const artifact = { + id: 499, name: `${INTENT_PREFIX}500`, + workflow_run: { id: 199, repository_id: REPOSITORY.id, head_sha: SHA }, + }; + for (const artifacts of [ + [null], + [{ ...artifact, id: 0 }], + [{ ...artifact, name: `${INTENT_PREFIX}501` }], + [{ ...artifact, workflow_run: { id: 199, repository_id: 1 } }], + [{ ...artifact, workflow_run: { id: 0, repository_id: REPOSITORY.id } }], + [artifact, artifact], + ]) { + fixture.github.rest.actions.listArtifactsForRepo = async () => ({ data: { artifacts, total_count: artifacts.length } }); + await assert.rejects(() => checkPublication(fixture.github, fixture.publication.context, fixture.publication.env, fixture.bundle, clock), + /history/); + } + assert.equal(fixture.state.posts.length, 0); +}); + +test("a missing current intent in the filtered history blocks publication", async () => { + const fixture = await ready(); + const id = await reserve(fixture); + fixture.github.rest.actions.listArtifactsForRepo = async () => ({ data: { artifacts: [], total_count: 0 } }); + await assert.rejects(() => publishDraft(fixture.github, fixture.publication.context, fixture.publication.env, fixture.bundle, id, clock), + /current publication intent is missing/); + assert.equal(fixture.state.posts.length, 0); +}); + +test("two filtered matches suffice to block even a long matching history", async () => { + const fixture = await ready(); + for (let index = 0; index < 1000; index++) { + fixture.state.artifacts.set(1000 + index, { + id: 1000 + index, name: `${INTENT_PREFIX}500`, + workflow_run: { id: 1000 + index, repository_id: REPOSITORY.id, head_sha: SHA }, + }); + } await assert.rejects(() => checkPublication(fixture.github, fixture.publication.context, fixture.publication.env, fixture.bundle, clock), - /history/); + /previous publication intent/); + assert.equal(fixture.state.historyRequests.length, 1); + assert.equal(fixture.state.posts.length, 0); +}); + +test("publication history uses one draft-keyed request per phase regardless of unrelated runs", async () => { + const fixture = await ready(); + for (let index = 0; index < 1001; index++) { + fixture.state.artifacts.set(1000 + index, { + id: 1000 + index, name: `${INTENT_PREFIX}${1000 + index}`, + workflow_run: { id: 1000 + index, repository_id: REPOSITORY.id, head_sha: SHA }, + }); + } + const id = await reserve(fixture); + await publishDraft(fixture.github, fixture.publication.context, fixture.publication.env, fixture.bundle, id, clock); + assert.equal(fixture.state.historyRequests.length, 2); + for (const request of fixture.state.historyRequests) { + assert.equal(request.method, "listArtifactsForRepo"); + assert.equal(request.name, `${INTENT_PREFIX}500`); + assert.equal(request.per_page, 2); + assert.equal(request.page, 1); + } + assert.deepEqual(fixture.state.posts, [fixture.bundle.body]); }); test("exactly 1,000 comments are supported but one more is an explicit context error", async () => { @@ -702,6 +784,19 @@ for (const kind of ["issue", "discussion"]) { fs.copyFileSync(prepared.path, path.join(contextDirectory, "context.json")); const candidateDirectory = downloadDirectory(sealing.find(step => step.name === "Download candidate")); writeJson(candidateDirectory, "discussion-draft.json", candidate()); + const detection = draftWorkflow["safe-outputs"]["threat-detection"]; + const scanCandidateDirectory = downloadDirectory(detection.steps.find(step => step.name === "Download candidate for detection")); + fs.mkdirSync(scanCandidateDirectory); + fs.copyFileSync(path.join(candidateDirectory, "discussion-draft.json"), path.join(scanCandidateDirectory, "discussion-draft.json")); + const staged = await execute(detection.steps.find(step => step.id === "stage_candidate"), draftRun, { + TYPECHAT_DETECTION_DIRECTORY: path.join(temporary, "threat-detection"), + }); + const scanned = await execute(detection["post-steps"].find(step => step.id === "candidate_scan"), draftRun, { + TYPECHAT_CANDIDATE_SHA256: staged.sha256, TYPECHAT_SCAN_OUTCOME: "success", + }); + const scanDirectory = downloadDirectory(sealing.find(step => step.name === "Download candidate scan binding")); + fs.mkdirSync(scanDirectory); + fs.copyFileSync(scanned.path, path.join(scanDirectory, "candidate-scan.json")); const sealed = await execute(sealing.find(step => step.id === "seal"), draftRun); const bundle = readJson(path.dirname(sealed.path), "sealed-draft.json", MAX_BUNDLE_BYTES); assert.equal(fixture.state.posts.length, 0); @@ -758,6 +853,69 @@ for (const kind of ["issue", "discussion"]) { }); } +test("a file-only candidate is included in detector input and bound to the exact scanned bytes", t => { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), "typechat-file-only-scan-")); + t.after(() => fs.rmSync(directory, { recursive: true, force: true })); + const candidateDirectory = path.join(directory, "candidate"); + const detectionDirectory = path.join(directory, "detection"); + writeJson(candidateDirectory, "discussion-draft.json", candidate()); + const file = path.join(candidateDirectory, "discussion-draft.json"); + const text = fs.readFileSync(file, "utf8"); + const hash = stageCandidateForDetection(candidateDirectory, detectionDirectory); + const input = JSON.parse(fs.readFileSync(path.join(detectionDirectory, "agent_output.json"), "utf8")); + assert.deepEqual(input.items, [{ type: "discussion-draft", content: text }]); + assert.equal(hash, digest(text)); + const scan = createCandidateScan(hash, "success", 100, "1"); + assert.deepEqual(readDetectedDraft(candidateDirectory, scan, 100, "1"), candidate()); + fs.appendFileSync(file, "\n"); + assert.deepEqual(JSON.parse(fs.readFileSync(file, "utf8")), candidate()); + assert.throws(() => readDetectedDraft(candidateDirectory, scan, 100, "1"), /differs from the threat-scanned artifact/); +}); + +test("candidate staging preserves native outputs and rejects invalid candidates or native inputs", t => { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), "typechat-native-scan-")); + t.after(() => fs.rmSync(directory, { recursive: true, force: true })); + const candidateDirectory = path.join(directory, "candidate"); + const detectionDirectory = path.join(directory, "detection"); + const original = { items: [{ type: "noop", message: "Draft prepared." }], errors: [] }; + writeJson(candidateDirectory, "discussion-draft.json", candidate()); + writeJson(detectionDirectory, "agent_output.json", original); + stageCandidateForDetection(candidateDirectory, detectionDirectory); + const input = JSON.parse(fs.readFileSync(path.join(detectionDirectory, "agent_output.json"), "utf8")); + assert.deepEqual(input.items.slice(0, -1), original.items); + assert.deepEqual(input.errors, []); + assert.deepEqual(JSON.parse(input.items.at(-1).content), candidate()); + const outputFile = path.join(detectionDirectory, "agent_output.json"); + for (const invalid of ["{malformed", '{"items":null}']) { + fs.writeFileSync(outputFile, invalid); + assert.throws(() => stageCandidateForDetection(candidateDirectory, detectionDirectory), /JSON|malformed/); + assert.equal(fs.readFileSync(outputFile, "utf8"), invalid); + } + const candidateFile = path.join(candidateDirectory, "discussion-draft.json"); + for (const invalid of ["{malformed", "{}", " ".repeat(65537), Buffer.from([0xff])]) { + fs.writeFileSync(candidateFile, invalid); + assert.throws(() => stageCandidateForDetection(candidateDirectory, detectionDirectory)); + } +}); + +test("scan bindings require successful execution and the current run and attempt", t => { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), "typechat-scan-binding-")); + t.after(() => fs.rmSync(directory, { recursive: true, force: true })); + writeJson(directory, "discussion-draft.json", candidate()); + const hash = digest(fs.readFileSync(path.join(directory, "discussion-draft.json"), "utf8")); + const scan = createCandidateScan(hash, "success", 100, 1); + for (const outcome of [undefined, "", "skipped", "failure", "cancelled"]) { + assert.throws(() => createCandidateScan(hash, outcome, 100, 1), /did not execute successfully/); + assert.throws(() => readDetectedDraft(directory, { ...scan, outcome }, 100, 1), /did not execute successfully/); + } + for (const invalid of [ + undefined, {}, { ...scan, extra: true }, { ...scan, version: 2 }, + { ...scan, runId: 99 }, { ...scan, attempt: 2 }, { ...scan, sha256: "" }, + ]) { + assert.throws(() => readDetectedDraft(directory, invalid, 100, 1), /binding|hash/); + } +}); + test("artifact files must be bounded regular JSON files with no extra entries", t => { const directory = fs.mkdtempSync(path.join(os.tmpdir(), "typechat-discussion-test-")); t.after(() => fs.rmSync(directory, { recursive: true, force: true })); diff --git a/tools/tests/discussion-workflows.test.mjs b/tools/tests/discussion-workflows.test.mjs index 6dd7495b..9fc12acd 100644 --- a/tools/tests/discussion-workflows.test.mjs +++ b/tools/tests/discussion-workflows.test.mjs @@ -25,6 +25,7 @@ const agent = compiled.jobs.agent; const agentCommand = agent.steps.find(step => step.name === "Execute GitHub Copilot CLI"); const detectionCommand = compiled.jobs.detection.steps.find(step => step.name === "Execute threat detection with AWF"); const sourceSeal = source.jobs.seal.steps; +const scan = source["safe-outputs"]["threat-detection"]; const publishSteps = publisher.jobs.publish_issue.steps; test("discussion commands are created-comment events guarded by owner and trusted branch", () => { @@ -60,7 +61,13 @@ test("discussion commands are created-comment events guarded by owner and truste test("drafting reuses the proven tools and model without enabling the future build budget", () => { assert.deepEqual(source.imports, ["shared/read-only-analysis.md"]); for (const key of Object.keys(shared)) { - assert.equal(Object.hasOwn(source, key), false, `Do not override shared ${key} controls`); + if (key === "safe-outputs") { + assert.deepEqual(Object.keys(source[key]), ["report-missing-tool", "noop", "threat-detection"]); + assert.equal(source[key]["report-missing-tool"], shared[key]["report-missing-tool"]); + assert.deepEqual(source[key].noop, { max: 3, "report-as-issue": false }); + } else { + assert.equal(Object.hasOwn(source, key), false, `Do not override shared ${key} controls`); + } } assert.equal(shared.engine.model, "gpt-6.1-sol"); assert.equal(source["max-ai-credits"], 50); @@ -141,6 +148,112 @@ test("trusted preparation gates the model and successful detection gates sealing assert.ok(sourceSeal.indexOf(sealedUpload) < sourceSeal.findIndex(step => step.name === "Show the exact preview and approval command")); }); +test("file-only candidates are explicit inputs to the pinned detector", () => { + const detection = compiled.jobs.detection; + const download = detection.steps.findIndex(step => step.name === "Download candidate for detection"); + const stage = detection.steps.findIndex(step => step.id === "stage_candidate"); + const setup = detection.steps.findIndex(step => step.id === "detection_setup"); + const execute = detection.steps.findIndex(step => step.id === "detection_agentic_execution"); + const record = detection.steps.findIndex(step => step.id === "candidate_scan"); + const upload = detection.steps.findIndex(step => step.name === "Upload candidate scan binding"); + const conclude = detection.steps.findIndex(step => step.id === "detection_conclusion"); + assert.ok(download >= 0 && download < stage && stage < setup && setup < execute && + execute < record && record < upload && upload < conclude); + const candidateName = "typechat-discussion-candidate-${{ github.run_attempt }}"; + assert.equal(detection.steps[download].with.name, candidateName); + assert.equal(sourceSeal.find(step => step.name === "Download candidate").with.name, candidateName); + assert.equal(source["post-steps"].find(step => step.name === "Upload candidate draft").with.name, candidateName); + assert.equal(detection.steps[stage].env.TYPECHAT_DETECTION_DIRECTORY, "/tmp/gh-aw/threat-detection"); + assert.equal(detection.steps[record].env.TYPECHAT_CANDIDATE_SHA256, "${{ steps.stage_candidate.outputs.sha256 }}"); + assert.equal(detection.steps[record].env.TYPECHAT_SCAN_OUTCOME, "${{ steps.detection_agentic_execution.outcome }}"); + assert.ok(detection.steps[record].if.includes("steps.detection_agentic_execution.outcome == 'success'")); + assert.ok(detection.steps[record].if.includes("steps.stage_candidate.outcome == 'success'")); + assert.equal(detection.steps[upload].with.name, "typechat-discussion-scan-${{ github.run_attempt }}"); + assert.equal(sourceSeal.find(step => step.name === "Download candidate scan binding").with.name, + detection.steps[upload].with.name); + assert.equal(scan["continue-on-error"], false); + assert.equal(scan["report-as-issue"], false); + assert.equal(detectionCommand.env.GH_AW_DETECTION_CONTINUE_ON_ERROR, "false"); +}); + +test("a file-only model response triggers the compiled detection guard without native model outputs or a patch", () => { + const working = fs.mkdtempSync(path.join(os.tmpdir(), "typechat-detection-guard-")); + try { + const safeOutputs = path.join(working, "gh-aw", "safeoutputs", "outputs.jsonl"); + const githubOutput = path.join(working, "github-output"); + const signal = agent.steps.find(step => step.id === "require_candidate_detection"); + if (signal) { + const result = spawnSync(process.execPath, ["--eval", signal.run], { + cwd: working, encoding: "utf8", env: { ...process.env, GH_AW_SAFE_OUTPUTS: safeOutputs }, + }); + assert.equal(result.status, 0, result.stderr); + } + fs.writeFileSync(path.join(working, "discussion-draft.json"), JSON.stringify({ + summary: "File-only model output.", scope: "in_scope", scopeReason: "Clarify the existing API.", + readiness: "needs_clarification", readinessReason: "A human decision is needed.", + questions: ["Which usage data is needed?"], existingRequirements: [], suggestedCriteria: [], + limitations: ["No implementation was tested."], pullRequests: [], + })); + const records = fs.existsSync(safeOutputs) ? + fs.readFileSync(safeOutputs, "utf8").trim().split("\n").map(line => JSON.parse(line)) : []; + const outputTypes = [...new Set(records.map(record => record.type))].join(","); + const guard = compiled.jobs.detection.steps.find(step => step.id === "detection_guard"); + assert.equal(guard.env.OUTPUT_TYPES, "${{ needs.agent.outputs.output_types }}"); + assert.equal(agent.outputs.output_types, "${{ steps.collect_output.outputs.output_types }}"); + let bash = "bash"; + if (process.platform === "win32") { + const git = spawnSync("git", ["--exec-path"], { encoding: "utf8" }); + assert.equal(git.status, 0, git.error?.message ?? git.stderr); + bash = path.resolve(git.stdout.trim(), "..", "..", "..", "bin", "bash.exe"); + } + const result = spawnSync(bash, ["--noprofile", "--norc", "-c", guard.run], { + cwd: working, encoding: "utf8", + env: { + ...process.env, OUTPUT_TYPES: outputTypes, HAS_PATCH: "false", + GITHUB_OUTPUT: githubOutput.replaceAll("\\", "/"), + }, + }); + assert.equal(result.status, 0, result.error?.message ?? result.stderr); + assert.match(fs.readFileSync(githubOutput, "utf8"), /^run_detection=true$/m); + assert.deepEqual(records.map(record => record.type), ["noop"]); + const signalIndex = agent.steps.indexOf(signal); + assert.ok(signalIndex >= 0 && signalIndex < agent.steps.indexOf(agentCommand)); + const collector = agent.steps.find(step => step.id === "collect_output"); + assert.equal(signal.env.GH_AW_SAFE_OUTPUTS, collector.env.GH_AW_SAFE_OUTPUTS); + assert.ok(agent.steps.indexOf(agentCommand) < agent.steps.indexOf(collector)); + const handler = compiled.jobs.safe_outputs.steps.find(step => step.id === "process_safe_outputs"); + const config = JSON.parse(handler.env.GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG); + assert.deepEqual(config.noop, { max: 3, "report-as-issue": "false" }); + } + finally { + fs.rmSync(working, { recursive: true }); + } +}); + +test("sealing requires an executed successful scan, not just a successful detection job", () => { + const condition = compiled.jobs.seal.if.replace(/^\$\{\{\s*|\s*\}\}$/g, ""); + const canSeal = new Function("needs", `return (${condition});`); + const needs = { + agent: { result: "success" }, + detection: { + result: "success", + outputs: { run_detection: "true", detection_success: "true", detection_conclusion: "success" }, + }, + }; + assert.equal(canSeal(needs), true); + for (const [field, values] of Object.entries({ + run_detection: ["false", ""], + detection_success: ["false", ""], + detection_conclusion: ["skipped", "warning", "failure", ""], + })) { + for (const value of values) { + const changed = structuredClone(needs); + changed.detection.outputs[field] = value; + assert.equal(canSeal(changed), false, `${field}=${value} must not seal a draft`); + } + } +}); + test("publication authenticates provenance before download and reserves intent before its only write", () => { const identify = publishSteps.findIndex(step => step.id === "identify"); const download = publishSteps.findIndex(step => step.name === "Download the approved sealed draft"); @@ -192,7 +305,7 @@ test("trusted scripts, actions, checkouts, and artifact uploads retain their saf assert.equal(step.with.repository, undefined); assert.equal(step.with["persist-credentials"], false); } - for (const step of [...source.jobs.prepare.steps, ...sourceSeal, ...publishSteps]) { + for (const step of [...source.jobs.prepare.steps, ...sourceSeal, ...scan.steps, ...scan["post-steps"], ...publishSteps]) { if (step.uses?.startsWith("actions/checkout@")) { assert.equal(step.with.ref, "${{ github.sha }}"); assert.equal(step.with["persist-credentials"], false); @@ -206,7 +319,7 @@ test("trusted scripts, actions, checkouts, and artifact uploads retain their saf } } } - const uploads = [...source.jobs.prepare.steps, ...sourceSeal, ...source["post-steps"], ...publishSteps] + const uploads = [...source.jobs.prepare.steps, ...sourceSeal, ...source["post-steps"], ...scan["post-steps"], ...publishSteps] .filter(step => step.uses?.startsWith("actions/upload-artifact@")); for (const step of uploads) { assert.equal(step.with["if-no-files-found"], "error");