diff --git a/.github/workflow-scripts/__tests__/metaImportRisk-test.js b/.github/workflow-scripts/__tests__/metaImportRisk-test.js index 1332648ba1e9..b4eeb89987b9 100644 --- a/.github/workflow-scripts/__tests__/metaImportRisk-test.js +++ b/.github/workflow-scripts/__tests__/metaImportRisk-test.js @@ -134,6 +134,15 @@ describe('assessPullRequest', () => { expect(await assess([file('src/example.js')], fetchImpl)).toMatchObject({ status: 'low', aiComplete: true, + trace: { + model: 'muse-spark-1.2', + forcedBy: [], + decision: { + requiresMetaImport: false, + reason: 'Checked the changed behavior.', + evidenceFiles: [], + }, + }, }); expect(fetchImpl).toHaveBeenCalledWith( 'https://api.meta.ai/v1/responses', @@ -184,9 +193,23 @@ describe('assessPullRequest', () => { test('can escalate a workflow-only change when the model finds a concrete risk', async () => { const path = '.github/workflows/test-all.yml'; - expect( - await assess([file(path)], async () => modelResponse(true, [path])), - ).toMatchObject({status: 'high', aiComplete: true}); + const result = await assess([file(path)], async () => + modelResponse(true, [path]), + ); + expect(result).toMatchObject({ + status: 'high', + aiComplete: true, + trace: { + forcedBy: [], + decision: { + requiresMetaImport: true, + reason: 'Checked the changed behavior.', + evidenceFiles: [path], + }, + }, + }); + expect(JSON.stringify(result.trace)).not.toContain('test-key'); + expect(JSON.stringify(result.trace)).not.toContain('@@ -1 +1 @@'); }); test('never downgrades a sensitive path based on model output', async () => { @@ -194,7 +217,11 @@ describe('assessPullRequest', () => { await assess([file('packages/react-native/package.json')], async () => modelResponse(false), ), - ).toMatchObject({status: 'high', aiComplete: true}); + ).toMatchObject({ + status: 'high', + aiComplete: true, + trace: {forcedBy: ['packages/react-native/package.json']}, + }); }); test('honors a high-risk label on otherwise low-risk files', async () => { @@ -216,31 +243,83 @@ describe('assessPullRequest', () => { await assess([file('src/example.js')], async () => { throw new Error('Network failure'); }), - ).toEqual({status: 'unknown', aiComplete: false}); + ).toMatchObject({ + status: 'unknown', + aiComplete: false, + trace: {failure: 'model_request_failed'}, + }); expect( await assess([file('package.json')], async () => { throw new Error('Network failure'); }), - ).toEqual({status: 'high', aiComplete: false}); + ).toMatchObject({ + status: 'high', + aiComplete: false, + trace: { + failure: 'model_request_failed', + forcedBy: ['package.json'], + }, + }); + }); + + test('records a safe failure category without logging the HTTP response', async () => { + const result = await assess([file('src/example.js')], async () => ({ + ok: false, + status: 429, + })); + expect(result).toMatchObject({ + status: 'unknown', + aiComplete: false, + trace: {failure: 'model_http_429'}, + }); + }); + + test('does not expose invalid model output in the trace', async () => { + const result = await assess([file('src/example.js')], async () => ({ + ok: true, + json: async () => ({output_text: 'untrusted raw model output'}), + })); + expect(result).toMatchObject({ + status: 'unknown', + aiComplete: false, + trace: {failure: 'invalid_model_decision'}, + }); + expect(JSON.stringify(result.trace)).not.toContain('untrusted raw'); + }); + + test('bounds the number of evidence paths in the trace', async () => { + const paths = Array.from({length: 11}, (_, i) => `src/file${i}.js`); + const result = await assess( + paths.map(path => file(path)), + async () => modelResponse(true, paths), + ); + expect(result.trace.decision.evidenceFiles).toHaveLength(10); + expect(result.trace.decision.evidenceFileCount).toBe(11); }); test('treats missing patches and capped file listings as incomplete', async () => { const fetchImpl = jest.fn(); expect( await assess([file('image.png', {patch: undefined})], fetchImpl), - ).toEqual({ + ).toMatchObject({ status: 'unknown', aiComplete: false, + trace: {failure: 'missing_or_incomplete_patch'}, }); expect( await assess(Array(3000).fill(file('README.md')), fetchImpl), - ).toEqual({ + ).toMatchObject({ status: 'unknown', aiComplete: false, + trace: {failure: 'file_list_capped'}, }); expect( await assess([file('src/large.js', {changes: 500})], fetchImpl), - ).toEqual({status: 'unknown', aiComplete: false}); + ).toMatchObject({ + status: 'unknown', + aiComplete: false, + trace: {failure: 'missing_or_incomplete_patch'}, + }); expect(fetchImpl).not.toHaveBeenCalled(); }); }); diff --git a/.github/workflow-scripts/metaImportRisk.js b/.github/workflow-scripts/metaImportRisk.js index 4329abe4dd0f..fe20a5c5450c 100644 --- a/.github/workflow-scripts/metaImportRisk.js +++ b/.github/workflow-scripts/metaImportRisk.js @@ -12,6 +12,20 @@ const MODEL_URL = 'https://api.meta.ai/v1/responses'; const MODEL = 'muse-spark-1.2'; const MAX_DIFF_CHARS = 120_000; +const MAX_TRACE_PATHS = 10; + +function incomplete(status, failure, forcedBy = []) { + return { + status, + aiComplete: false, + trace: { + model: MODEL, + forcedBy: forcedBy.slice(0, MAX_TRACE_PATHS), + forcedByCount: forcedBy.length, + failure, + }, + }; +} function sensitivePaths(files) { return files.flatMap(file => { @@ -80,18 +94,26 @@ async function assessPullRequest({github, context, apiKey, fetchImpl = fetch}) { per_page: 100, }); } catch { - return {status: 'unknown', aiComplete: false}; + return incomplete('unknown', 'file_list_unavailable'); } // GitHub's list-files endpoint caps results at 3,000. Never classify a // potentially incomplete change list as low risk. if (files.length === 0 || files.length >= 3_000) { - return {status: 'unknown', aiComplete: false}; + return incomplete( + 'unknown', + files.length === 0 ? 'file_list_empty' : 'file_list_capped', + ); } - const forced = + const forcedBy = sensitivePaths(files); + if ( context.payload.pull_request.labels.some( label => label.name === 'high-risk', - ) || sensitivePaths(files).length > 0; + ) + ) { + forcedBy.push('high-risk label'); + } + const forced = forcedBy.length > 0; // A missing patch (including binary changes) or oversized diff cannot be // reviewed completely. Still keep deterministic high-risk matches. @@ -103,14 +125,25 @@ async function assessPullRequest({github, context, apiKey, fetchImpl = fetch}) { patch: file.patch, })), ); - if ( - files.some(file => !hasCompletePatch(file)) || - diff.length > MAX_DIFF_CHARS || - !apiKey - ) { - return {status: forced ? 'high' : 'unknown', aiComplete: false}; + if (files.some(file => !hasCompletePatch(file))) { + return incomplete( + forced ? 'high' : 'unknown', + 'missing_or_incomplete_patch', + forcedBy, + ); + } + if (diff.length > MAX_DIFF_CHARS) { + return incomplete(forced ? 'high' : 'unknown', 'diff_too_large', forcedBy); + } + if (!apiKey) { + return incomplete( + forced ? 'high' : 'unknown', + 'model_key_missing', + forcedBy, + ); } + let failure = 'model_request_failed'; try { const response = await fetchImpl(MODEL_URL, { method: 'POST', @@ -134,8 +167,13 @@ async function assessPullRequest({github, context, apiKey, fetchImpl = fetch}) { signal: AbortSignal.timeout(120_000), }); if (!response.ok) { - throw new Error('Model API request failed'); + return incomplete( + forced ? 'high' : 'unknown', + `model_http_${response.status}`, + forcedBy, + ); } + failure = 'invalid_model_decision'; const decision = parseModelDecision( await response.json(), new Set(files.map(file => file.filename)), @@ -144,10 +182,21 @@ async function assessPullRequest({github, context, apiKey, fetchImpl = fetch}) { status: forced || decision.requiresMetaImport ? 'high' : 'low', aiComplete: true, reason: decision.reason, + trace: { + model: MODEL, + forcedBy: forcedBy.slice(0, MAX_TRACE_PATHS), + forcedByCount: forcedBy.length, + decision: { + requiresMetaImport: decision.requiresMetaImport, + reason: decision.reason, + evidenceFiles: decision.evidenceFiles.slice(0, MAX_TRACE_PATHS), + evidenceFileCount: decision.evidenceFiles.length, + }, + }, }; } catch { // Failure is inconclusive, not a negative review. - return {status: forced ? 'high' : 'unknown', aiComplete: false}; + return incomplete(forced ? 'high' : 'unknown', failure, forcedBy); } } diff --git a/.github/workflows/meta-import-risk.yml b/.github/workflows/meta-import-risk.yml index fa50bb04204c..acae04baed6b 100644 --- a/.github/workflows/meta-import-risk.yml +++ b/.github/workflows/meta-import-risk.yml @@ -40,6 +40,9 @@ jobs: }); core.setOutput('risk', result.status); core.info(`Meta import risk: ${result.status}`); + // Log only the validated, bounded decision and failure category. + // Never log the raw patch, model response, or API credential. + core.info(`Meta import Muse trace: ${JSON.stringify(result.trace)}`); if (!result.aiComplete) { core.setFailed('Muse did not assess the complete PR diff; the labeling job will conservatively request Meta review.'); }