Skip to content

Commit 3b49389

Browse files
authored
Log structured Muse import risk decisions (#60335)
1 parent 6407b5e commit 3b49389

3 files changed

Lines changed: 152 additions & 21 deletions

File tree

‎.github/workflow-scripts/__tests__/metaImportRisk-test.js‎

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ describe('assessPullRequest', () => {
134134
expect(await assess([file('src/example.js')], fetchImpl)).toMatchObject({
135135
status: 'low',
136136
aiComplete: true,
137+
trace: {
138+
model: 'muse-spark-1.2',
139+
forcedBy: [],
140+
decision: {
141+
requiresMetaImport: false,
142+
reason: 'Checked the changed behavior.',
143+
evidenceFiles: [],
144+
},
145+
},
137146
});
138147
expect(fetchImpl).toHaveBeenCalledWith(
139148
'https://api.meta.ai/v1/responses',
@@ -184,17 +193,35 @@ describe('assessPullRequest', () => {
184193

185194
test('can escalate a workflow-only change when the model finds a concrete risk', async () => {
186195
const path = '.github/workflows/test-all.yml';
187-
expect(
188-
await assess([file(path)], async () => modelResponse(true, [path])),
189-
).toMatchObject({status: 'high', aiComplete: true});
196+
const result = await assess([file(path)], async () =>
197+
modelResponse(true, [path]),
198+
);
199+
expect(result).toMatchObject({
200+
status: 'high',
201+
aiComplete: true,
202+
trace: {
203+
forcedBy: [],
204+
decision: {
205+
requiresMetaImport: true,
206+
reason: 'Checked the changed behavior.',
207+
evidenceFiles: [path],
208+
},
209+
},
210+
});
211+
expect(JSON.stringify(result.trace)).not.toContain('test-key');
212+
expect(JSON.stringify(result.trace)).not.toContain('@@ -1 +1 @@');
190213
});
191214

192215
test('never downgrades a sensitive path based on model output', async () => {
193216
expect(
194217
await assess([file('packages/react-native/package.json')], async () =>
195218
modelResponse(false),
196219
),
197-
).toMatchObject({status: 'high', aiComplete: true});
220+
).toMatchObject({
221+
status: 'high',
222+
aiComplete: true,
223+
trace: {forcedBy: ['packages/react-native/package.json']},
224+
});
198225
});
199226

200227
test('honors a high-risk label on otherwise low-risk files', async () => {
@@ -216,31 +243,83 @@ describe('assessPullRequest', () => {
216243
await assess([file('src/example.js')], async () => {
217244
throw new Error('Network failure');
218245
}),
219-
).toEqual({status: 'unknown', aiComplete: false});
246+
).toMatchObject({
247+
status: 'unknown',
248+
aiComplete: false,
249+
trace: {failure: 'model_request_failed'},
250+
});
220251
expect(
221252
await assess([file('package.json')], async () => {
222253
throw new Error('Network failure');
223254
}),
224-
).toEqual({status: 'high', aiComplete: false});
255+
).toMatchObject({
256+
status: 'high',
257+
aiComplete: false,
258+
trace: {
259+
failure: 'model_request_failed',
260+
forcedBy: ['package.json'],
261+
},
262+
});
263+
});
264+
265+
test('records a safe failure category without logging the HTTP response', async () => {
266+
const result = await assess([file('src/example.js')], async () => ({
267+
ok: false,
268+
status: 429,
269+
}));
270+
expect(result).toMatchObject({
271+
status: 'unknown',
272+
aiComplete: false,
273+
trace: {failure: 'model_http_429'},
274+
});
275+
});
276+
277+
test('does not expose invalid model output in the trace', async () => {
278+
const result = await assess([file('src/example.js')], async () => ({
279+
ok: true,
280+
json: async () => ({output_text: 'untrusted raw model output'}),
281+
}));
282+
expect(result).toMatchObject({
283+
status: 'unknown',
284+
aiComplete: false,
285+
trace: {failure: 'invalid_model_decision'},
286+
});
287+
expect(JSON.stringify(result.trace)).not.toContain('untrusted raw');
288+
});
289+
290+
test('bounds the number of evidence paths in the trace', async () => {
291+
const paths = Array.from({length: 11}, (_, i) => `src/file${i}.js`);
292+
const result = await assess(
293+
paths.map(path => file(path)),
294+
async () => modelResponse(true, paths),
295+
);
296+
expect(result.trace.decision.evidenceFiles).toHaveLength(10);
297+
expect(result.trace.decision.evidenceFileCount).toBe(11);
225298
});
226299

227300
test('treats missing patches and capped file listings as incomplete', async () => {
228301
const fetchImpl = jest.fn();
229302
expect(
230303
await assess([file('image.png', {patch: undefined})], fetchImpl),
231-
).toEqual({
304+
).toMatchObject({
232305
status: 'unknown',
233306
aiComplete: false,
307+
trace: {failure: 'missing_or_incomplete_patch'},
234308
});
235309
expect(
236310
await assess(Array(3000).fill(file('README.md')), fetchImpl),
237-
).toEqual({
311+
).toMatchObject({
238312
status: 'unknown',
239313
aiComplete: false,
314+
trace: {failure: 'file_list_capped'},
240315
});
241316
expect(
242317
await assess([file('src/large.js', {changes: 500})], fetchImpl),
243-
).toEqual({status: 'unknown', aiComplete: false});
318+
).toMatchObject({
319+
status: 'unknown',
320+
aiComplete: false,
321+
trace: {failure: 'missing_or_incomplete_patch'},
322+
});
244323
expect(fetchImpl).not.toHaveBeenCalled();
245324
});
246325
});

‎.github/workflow-scripts/metaImportRisk.js‎

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212
const MODEL_URL = 'https://api.meta.ai/v1/responses';
1313
const MODEL = 'muse-spark-1.2';
1414
const MAX_DIFF_CHARS = 120_000;
15+
const MAX_TRACE_PATHS = 10;
16+
17+
function incomplete(status, failure, forcedBy = []) {
18+
return {
19+
status,
20+
aiComplete: false,
21+
trace: {
22+
model: MODEL,
23+
forcedBy: forcedBy.slice(0, MAX_TRACE_PATHS),
24+
forcedByCount: forcedBy.length,
25+
failure,
26+
},
27+
};
28+
}
1529

1630
function sensitivePaths(files) {
1731
return files.flatMap(file => {
@@ -80,18 +94,26 @@ async function assessPullRequest({github, context, apiKey, fetchImpl = fetch}) {
8094
per_page: 100,
8195
});
8296
} catch {
83-
return {status: 'unknown', aiComplete: false};
97+
return incomplete('unknown', 'file_list_unavailable');
8498
}
8599

86100
// GitHub's list-files endpoint caps results at 3,000. Never classify a
87101
// potentially incomplete change list as low risk.
88102
if (files.length === 0 || files.length >= 3_000) {
89-
return {status: 'unknown', aiComplete: false};
103+
return incomplete(
104+
'unknown',
105+
files.length === 0 ? 'file_list_empty' : 'file_list_capped',
106+
);
90107
}
91-
const forced =
108+
const forcedBy = sensitivePaths(files);
109+
if (
92110
context.payload.pull_request.labels.some(
93111
label => label.name === 'high-risk',
94-
) || sensitivePaths(files).length > 0;
112+
)
113+
) {
114+
forcedBy.push('high-risk label');
115+
}
116+
const forced = forcedBy.length > 0;
95117

96118
// A missing patch (including binary changes) or oversized diff cannot be
97119
// reviewed completely. Still keep deterministic high-risk matches.
@@ -103,14 +125,25 @@ async function assessPullRequest({github, context, apiKey, fetchImpl = fetch}) {
103125
patch: file.patch,
104126
})),
105127
);
106-
if (
107-
files.some(file => !hasCompletePatch(file)) ||
108-
diff.length > MAX_DIFF_CHARS ||
109-
!apiKey
110-
) {
111-
return {status: forced ? 'high' : 'unknown', aiComplete: false};
128+
if (files.some(file => !hasCompletePatch(file))) {
129+
return incomplete(
130+
forced ? 'high' : 'unknown',
131+
'missing_or_incomplete_patch',
132+
forcedBy,
133+
);
134+
}
135+
if (diff.length > MAX_DIFF_CHARS) {
136+
return incomplete(forced ? 'high' : 'unknown', 'diff_too_large', forcedBy);
137+
}
138+
if (!apiKey) {
139+
return incomplete(
140+
forced ? 'high' : 'unknown',
141+
'model_key_missing',
142+
forcedBy,
143+
);
112144
}
113145

146+
let failure = 'model_request_failed';
114147
try {
115148
const response = await fetchImpl(MODEL_URL, {
116149
method: 'POST',
@@ -134,8 +167,13 @@ async function assessPullRequest({github, context, apiKey, fetchImpl = fetch}) {
134167
signal: AbortSignal.timeout(120_000),
135168
});
136169
if (!response.ok) {
137-
throw new Error('Model API request failed');
170+
return incomplete(
171+
forced ? 'high' : 'unknown',
172+
`model_http_${response.status}`,
173+
forcedBy,
174+
);
138175
}
176+
failure = 'invalid_model_decision';
139177
const decision = parseModelDecision(
140178
await response.json(),
141179
new Set(files.map(file => file.filename)),
@@ -144,10 +182,21 @@ async function assessPullRequest({github, context, apiKey, fetchImpl = fetch}) {
144182
status: forced || decision.requiresMetaImport ? 'high' : 'low',
145183
aiComplete: true,
146184
reason: decision.reason,
185+
trace: {
186+
model: MODEL,
187+
forcedBy: forcedBy.slice(0, MAX_TRACE_PATHS),
188+
forcedByCount: forcedBy.length,
189+
decision: {
190+
requiresMetaImport: decision.requiresMetaImport,
191+
reason: decision.reason,
192+
evidenceFiles: decision.evidenceFiles.slice(0, MAX_TRACE_PATHS),
193+
evidenceFileCount: decision.evidenceFiles.length,
194+
},
195+
},
147196
};
148197
} catch {
149198
// Failure is inconclusive, not a negative review.
150-
return {status: forced ? 'high' : 'unknown', aiComplete: false};
199+
return incomplete(forced ? 'high' : 'unknown', failure, forcedBy);
151200
}
152201
}
153202

‎.github/workflows/meta-import-risk.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ jobs:
6868
});
6969
core.setOutput('risk', result.status);
7070
core.info(`Meta import risk: ${result.status}`);
71+
// Log only the validated, bounded decision and failure category.
72+
// Never log the raw patch, model response, or API credential.
73+
core.info(`Meta import Muse trace: ${JSON.stringify(result.trace)}`);
7174
if (!result.aiComplete) {
7275
core.setFailed('Muse did not assess the complete PR diff; the labeling job will conservatively request Meta review.');
7376
}

0 commit comments

Comments
 (0)