From 06330bd9bf55a195473553a36c959f3b21a58265 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 23:49:35 +0000 Subject: [PATCH] fix(scenarios): the run grader reads a hook's declared refusal off the door's declaredCode The MCP door demotes a hook's own refusal code to `declaredCode` and serves the status's catalogue code on `code`. The runner kept only `code` and `status`, so the grader read the hook's ASSET_NOT_IN_STOCK at 409 as a platform RESOURCE_CONFLICT and failed a declared VALIDATION_FAILED. A refused ObservedAnswer now carries `declaredCode` when served, and refusalMeets reads it: a declared code equal to it meets exactly, and a declared VALIDATION_FAILED is met by one at any 4xx. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01FEkQ5nw3MDkRymEzv6FDJs --- .../33497-scenario-grader-declared-code.md | 12 ++ packages/qa/scenarios/README.md | 9 +- packages/qa/scenarios/src/runner/observe.ts | 61 ++++++++-- .../src/score/scenario-operation-grade.ts | 46 +++++--- .../test/runner/mid-tier-run-readings.test.ts | 111 ++++++++++++++---- 5 files changed, 187 insertions(+), 52 deletions(-) create mode 100644 .changeset/33497-scenario-grader-declared-code.md diff --git a/.changeset/33497-scenario-grader-declared-code.md b/.changeset/33497-scenario-grader-declared-code.md new file mode 100644 index 00000000000..05ccc809840 --- /dev/null +++ b/.changeset/33497-scenario-grader-declared-code.md @@ -0,0 +1,12 @@ +--- +'@objectstack/scenarios': patch +--- + +fix(scenarios): the run grader reads a hook's declared refusal off the door's `declaredCode` + +Clause-②: no +Metadata-protocol: none + +The MCP door serves a hook's own refusal code demoted off `code`: a hook that refuses with `ASSET_NOT_IN_STOCK` at 409 reaches the agent as `{ error: { code: 'RESOURCE_CONFLICT', status: 409, declaredCode: 'ASSET_NOT_IN_STOCK' } }`. The runner kept only `code` and `status`, so the grader saw a platform conflict and failed a card whose app refused exactly as it should (`check_out_asset_in_repair`, a declared `VALIDATION_FAILED`). + +A refused `ObservedAnswer` now carries the envelope's `declaredCode` when the door served one, from a write call, the last read on the object and the runner's probe alike, in the nested and the flat envelope. `refusalMeets` reads it two ways: a declared code equal to `declaredCode` meets exactly, and a declared `VALIDATION_FAILED` is met by a `declaredCode` at any 4xx. A catalogue code with no `declaredCode` still never meets a different declared code, and a `declaredCode` at 5xx or with no status does not meet. The grade's detail names what met ("refused RESOURCE_CONFLICT (409), the hook's declared refusal ASSET_NOT_IN_STOCK"). diff --git a/packages/qa/scenarios/README.md b/packages/qa/scenarios/README.md index e1ea271a2ed..354395ff777 100644 --- a/packages/qa/scenarios/README.md +++ b/packages/qa/scenarios/README.md @@ -67,9 +67,12 @@ gradeCardAnswer(card, { question: 'account_count', persona: 'sdr', turn }); // a asks for a correction) and the platform half, `platform:` checks (the runner's own write of the declared values, the probe, was refused with that code and stored nothing); so is every other `refused` create or update, permission oversteps included (`isProbedOperation`). A - declared `VALIDATION_FAILED` is met by an app-defined code at any 4xx status, or by the - app's flow refusal (`refusalMeets`); a platform catalogue code other than the declared one - never is. The platform half is not measured, out of passed and total with the reason in the + declared `VALIDATION_FAILED` is met by the app's own refusal at any 4xx status, or by the + app's flow refusal (`refusalMeets`). A hook's declared refusal reaches the MCP data tools + with `code` set to the status's catalogue code (`RESOURCE_CONFLICT` at 409) and the hook's + own code beside it as `declaredCode` (`ASSET_NOT_IN_STOCK`); the runner keeps both, and a + declared code meets its `declaredCode` exactly. A platform catalogue code other than the + declared one, with no `declaredCode`, never meets it. The platform half is not measured, out of passed and total with the reason in the scorecard's findings, when the probe met the build's own lock: refused 403 `PERMISSION_DENIED`, nothing written, and the build locks a field the probe writes for the persona and declares an action on the object (`buildLockOf`; ruling A on #31999 item 9). diff --git a/packages/qa/scenarios/src/runner/observe.ts b/packages/qa/scenarios/src/runner/observe.ts index d3f0025af8e..d6a5337a574 100644 --- a/packages/qa/scenarios/src/runner/observe.ts +++ b/packages/qa/scenarios/src/runner/observe.ts @@ -139,17 +139,19 @@ export function writeCallsOn(turn: Pick, object: string * not a coded refusal (or nothing on the object was called). */ export function lastReadRefusal(turn: Pick, object: string): string | undefined { - return lastReadRefusalOf(turn, object)?.code; + const output = lastReadRefusalOutput(turn, object); + return output === undefined ? undefined : refusalOf(output)?.code; } -function lastReadRefusalOf(turn: Pick, object: string): ReturnType { +/** The output of the last read on `object` when it carries a coded refusal, else `undefined`. */ +function lastReadRefusalOutput(turn: Pick, object: string): unknown { const reads = turn.toolCalls.filter((c) => { if ((WRITE_TOOLS as readonly string[]).includes(c.name) || c.output === undefined) return false; const input = (typeof c.input === 'object' && c.input !== null ? c.input : {}) as Record; return input.objectName === object; }); const last = reads[reads.length - 1]; - return last === undefined ? undefined : refusalOf(last.output); + return last !== undefined && refusalOf(last.output) !== undefined ? last.output : undefined; } /** @@ -205,13 +207,54 @@ export function withheldWrite(turn: ParsedTurn, object: string, writes: number): const failed = (c: RunnerToolCall) => c.isError === true || refusalCodeOf(c.output) !== undefined || flowRefusalOf(c.output) !== undefined; -const refusedBy = (r: NonNullable>): ObservedAnswer => - r.status !== undefined ? { kind: 'refused', code: r.code, status: r.status } : { kind: 'refused', code: r.code }; +/** + * The `declaredCode` of the envelope {@link refusalOf} reads in `output` — the + * producer's own unregistered code the door demoted off `code` (#31407: the + * MCP data tools' `{ error: { code, message, status, declaredCode } }`, the + * resource's flat `{ error, code, status, declaredCode }`) — or `undefined` + * when the door served none. The same envelope as {@link refusalOf}: the + * nested `error` object when it carries a string `code`, else the flat body. + */ +function declaredCodeOf(output: unknown): string | undefined { + let v: unknown = output; + if (typeof output === 'string') { + v = undefined; + // The same two readings as `refusalOf`: the whole text, else from its first `{`. + for (const text of [output, output.slice(Math.max(0, output.indexOf('{')))]) { + try { + v = JSON.parse(text); + break; + } catch { + // try the next reading + } + } + } + if (typeof v !== 'object' || v === null || Array.isArray(v)) return undefined; + const body = v as Record; + const nested = body.error; + const env = typeof nested === 'object' && nested !== null && !Array.isArray(nested) && typeof (nested as Record).code === 'string' + ? (nested as Record) + : body; + return typeof env.declaredCode === 'string' && env.declaredCode !== '' ? env.declaredCode : undefined; +} + +/** The refused answer of an output carrying a coded refusal: its `code`, `status` and `declaredCode`, each only when served. */ +function refusedBy(output: unknown): ObservedAnswer | undefined { + const r = refusalOf(output); + if (r === undefined) return undefined; + const declaredCode = declaredCodeOf(output); + return { + kind: 'refused', + code: r.code, + ...(r.status !== undefined ? { status: r.status } : {}), + ...(declaredCode !== undefined ? { declaredCode } : {}), + }; +} /** A write call's refusal by rule 3: its coded refusal, or its flow's declared one; `undefined` when it carries neither. */ function refusalAnswerOf(c: RunnerToolCall): ObservedAnswer | undefined { - const coded = refusalOf(c.output); - if (coded !== undefined) return refusedBy(coded); + const coded = refusedBy(c.output); + if (coded !== undefined) return coded; const text = flowRefusalOf(c.output); if (text === undefined) return undefined; return text === '' ? { kind: 'refused', code: flowRefused } : { kind: 'refused', code: flowRefused, note: `the flow answered ${JSON.stringify(text)}` }; @@ -224,8 +267,8 @@ export function observedAnswer(turn: ParsedTurn, object: string, writes: number) const refusals = calls.map(refusalAnswerOf).filter((r) => r !== undefined); if (refusals.some((r) => r.kind === 'refused' && r.code === CONFIRMATION_REQUIRED_CODE)) return { kind: 'pending_confirmation' }; if (refusals.length > 0) return refusals[refusals.length - 1]!; - const readRefusal = calls.length === 0 ? lastReadRefusalOf(turn, object) : undefined; - if (readRefusal !== undefined) return refusedBy(readRefusal); + const readRefusal = calls.length === 0 ? refusedBy(lastReadRefusalOutput(turn, object)) : undefined; + if (readRefusal !== undefined) return readRefusal; if (calls.length === 0 && asksToConfirm(turn.replyText)) return { kind: 'pending_confirmation' }; if (calls.length > 0) { const last = calls[calls.length - 1]!; diff --git a/packages/qa/scenarios/src/score/scenario-operation-grade.ts b/packages/qa/scenarios/src/score/scenario-operation-grade.ts index ac39c3f3c38..dbe78af454e 100644 --- a/packages/qa/scenarios/src/score/scenario-operation-grade.ts +++ b/packages/qa/scenarios/src/score/scenario-operation-grade.ts @@ -85,6 +85,13 @@ export type ObservedAnswer = code: string; /** The envelope's HTTP `status`, when it carried one. */ status?: number; + /** + * The envelope's `declaredCode`, when the door served one: the producer's + * own unregistered code (a hook's declared refusal, `ASSET_NOT_IN_STOCK`), + * demoted off `code`, which then holds the status's catalogue code + * (`RESOURCE_CONFLICT` at 409). Absent when the door served none. + */ + declaredCode?: string; /** What the runner read of an answer with no code: the door's text, and whose throw it names. */ note?: string; } @@ -192,37 +199,46 @@ function isValidationCode(code: string): boolean { } /** - * The refusal met the declared code. Exactly that code, or — for a declared - * `VALIDATION_FAILED` — the app's own refusal (ruling A on #31999 item 9, - * comment 6764953017, widening #31145 item 2), read from the door's answer - * alone: + * The refusal met the declared code. Exactly that code, as the door's `code` + * or as its `declaredCode`, or — for a declared `VALIDATION_FAILED` — the + * app's own refusal (ruling A on #31999 item 9, comment 6764953017, widening + * #31145 item 2), read from the door's answer alone: * - * - a code that is none of the platform's (it fails the spec's `ErrorCode` - * catalogue, ADR-0112) at any client-error status (4xx): the app's own - * rule answered, whatever status it chose — a hook's declared refusal + * - a `declaredCode` at any client-error status (4xx): the app's own rule + * answered, whatever status it chose. A hook's declared refusal * (`HookSchema.refusals`, `OVER_RECEIPT` at 422, `ASSET_NOT_IN_STOCK` at - * 409) reaches the MCP door with its own code; + * 409) reaches the MCP data tools with `code` set to the status's + * catalogue code (`VALIDATION_ERROR` at 422, `RESOURCE_CONFLICT` at 409) + * and its own code beside it as `declaredCode` (the door's demotion of an + * unregistered code, #31407), whatever the catalogue `code`; + * - a `code` that is none of the platform's (it fails the spec's `ErrorCode` + * catalogue, ADR-0112) at any 4xx status; * - the flow's declared refusal ({@link flowRefused}): the build routed the * write through an action whose flow refused it. * - * Platform codes stay strict: a catalogue code other than the declared one - * never meets it at any status (a denial at 403, a missing row at 404, a - * conflict at 409, an error at 500). Nor does an app code with no status or a - * 5xx one, nor the runner's other parenthesized labels. + * Platform codes stay strict: a catalogue code with no `declaredCode`, other + * than the declared one, never meets it at any status (a denial at 403, a + * missing row at 404, a conflict at 409, an error at 500). Nor does an app + * code or a `declaredCode` with no status or a 5xx one, nor the runner's other + * parenthesized labels. */ export function refusalMeets(declared: string, answer: ObservedAnswer): boolean { if (answer.kind !== 'refused') return false; - if (answer.code === declared) return true; + if (answer.code === declared || answer.declaredCode === declared) return true; if (!isValidationCode(declared)) return false; if (answer.code === flowRefused) return true; if (answer.status === undefined || answer.code.startsWith('(')) return false; + const clientError = answer.status >= 400 && answer.status < 500; + if (answer.declaredCode !== undefined) return clientError; if (ErrorCode.safeParse(answer.code).success) return false; - return answer.status >= 400 && answer.status < 500; + return clientError; } const refusalText = (declared: string, a: ObservedAnswer): string => { if (!refusalMeets(declared, a) || a.kind !== 'refused' || a.code === declared) return answerText(a); - return `${answerText(a)}, ${a.code === flowRefused ? "the app's flow refusal" : 'an app-defined refusal'}`; + if (a.code === flowRefused) return `${answerText(a)}, the app's flow refusal`; + if (a.declaredCode !== undefined) return `${answerText(a)}, the hook's declared refusal ${a.declaredCode}`; + return `${answerText(a)}, an app-defined refusal`; }; /** The door refused the probe as a field lock refuses: `PERMISSION_DENIED` at 403. */ diff --git a/packages/qa/scenarios/test/runner/mid-tier-run-readings.test.ts b/packages/qa/scenarios/test/runner/mid-tier-run-readings.test.ts index 06b7c422f35..6be848bbdb3 100644 --- a/packages/qa/scenarios/test/runner/mid-tier-run-readings.test.ts +++ b/packages/qa/scenarios/test/runner/mid-tier-run-readings.test.ts @@ -9,10 +9,13 @@ * 1. "Today": every run-phase prompt states the card's `asOf`, and a * post-state declared as it is met by the run date only in a field the * platform's clock stamped; a run date the agent wrote itself fails. - * 2. An app-defined code meets a declared `VALIDATION_FAILED` at any 4xx, + * 2. The app's own refusal meets a declared `VALIDATION_FAILED` at any 4xx, * and so does the app's flow refusal; a platform catalogue code other * than the declared one never does (re-pinned by ruling A on #31999 - * item 9, comment 6764953017; it was the validation class only). + * item 9, comment 6764953017; it was the validation class only). Pinned + * on the wire shape the door serves (#31407): the hook's own code rides + * `declaredCode`, `code` holds the status's catalogue code, and the + * runner keeps both. * 3. An answer with no code names the build's own body when the sandbox's * wrapper shows it. * 4. A permission overstep is probed: an agent that declines without @@ -23,6 +26,9 @@ * 7. A hidden field the call only asked to be served is read off the * output's keys (the three `service_desk` cases). * + * 2b. The runner keeps the door's `declaredCode` on the refused answer: a + * write call's, the last read's and the probe's, nested and flat. + * * R2. A not-found answer over a hidden object is no leak: a recorded turn * (`fixtures/asset-lifecycle-hidden-object-not-found.recording.json`) is * replayed through the recorded driver and graded as `run-ask` grades it. @@ -66,7 +72,10 @@ function load(id: string): LoadedScenario { type Call = Omit; const turnOf = (replyText: string, toolCalls: readonly Call[] = []): ParsedTurn => ({ replyText, toolCalls: toolCalls.map((c) => ({ ...c, rawName: c.name })), meta: {} }); const failing = (g: { checks: Array<{ check: string; pass: boolean }> }) => g.checks.filter((c) => !c.pass).map((c) => c.check); -const envelope = (code: string, status?: number) => JSON.stringify({ error: { code, message: 'refused', ...(status !== undefined ? { status } : {}) } }); +const envelope = (code: string, status?: number, declaredCode?: string) => + JSON.stringify({ error: { code, message: 'refused', ...(status !== undefined ? { status } : {}), ...(declaredCode !== undefined ? { declaredCode } : {}) } }); +/** The catalogue code the door puts on `code` for a demoted producer code at each status (`HttpStatusErrorCodeMap`, 4xx otherwise `VALIDATION_ERROR`). */ +const WIRE_CODE: Record = { 400: 'VALIDATION_ERROR', 403: 'PERMISSION_DENIED', 404: 'RESOURCE_NOT_FOUND', 409: 'RESOURCE_CONFLICT', 422: 'VALIDATION_ERROR', 500: 'INTERNAL_ERROR' }; const asset = load('asset_lifecycle'); const orders = load('order_to_cash'); @@ -150,25 +159,34 @@ describe('1. the card\'s today: a declared asOf date is met by the run date only }); }); -describe('2. an app-defined code meets a declared VALIDATION_FAILED, read from the envelope', () => { - it('the rule (ruling A on #31999 item 9, comment 6764953017): an app code (outside the ErrorCode catalogue) at any 4xx, or the flow refusal', () => { - for (const status of [400, 403, 404, 409, 422]) expect(refusalMeets('VALIDATION_FAILED', { kind: 'refused', code: 'OVER_RECEIPT', status }), String(status)).toBe(true); +describe('2. the app\'s own refusal meets a declared VALIDATION_FAILED, read from the envelope the door serves', () => { + it('the rule (ruling A on #31999 item 9, comment 6764953017): a hook\'s declaredCode at any 4xx, or the flow refusal', () => { + // The wire shape (#31407): the hook's own code is demoted to `declaredCode`, `code` is the status's catalogue code. + for (const status of [400, 403, 404, 409, 422]) { + expect(refusalMeets('VALIDATION_FAILED', { kind: 'refused', code: WIRE_CODE[status]!, status, declaredCode: 'OVER_RECEIPT' }), String(status)).toBe(true); + } expect(refusalMeets('VALIDATION_FAILED', { kind: 'refused', code: 'VALIDATION_FAILED' })).toBe(true); expect(refusalMeets('VALIDATION_FAILED', { kind: 'refused', code: flowRefused })).toBe(true); - // ⛔ Not met: a platform catalogue code other than the declared one at any status (a denial, - // a missing row, a conflict, an error); an app code with no status or at 5xx; a platform code - // of another refusal at 400 (a malformed call); the runner's other labels; any other declared code. - for (const [code, status] of [['PERMISSION_DENIED', 403], ['NOT_FOUND', 404], ['CONFLICT', 409], ['INTERNAL_ERROR', 500]] as const) { + // ⛔ Not met: a platform catalogue code other than the declared one, with no declaredCode, at any + // status (a malformed call, a denial, a missing row, a conflict, an error); a declaredCode at 5xx + // or with no status; the runner's other labels; any other declared code. + for (const [code, status] of [['VALIDATION_ERROR', 400], ['PERMISSION_DENIED', 403], ['RESOURCE_NOT_FOUND', 404], ['RESOURCE_CONFLICT', 409], ['INTERNAL_ERROR', 500]] as const) { expect(refusalMeets('VALIDATION_FAILED', { kind: 'refused', code, status }), code).toBe(false); } - expect(refusalMeets('VALIDATION_FAILED', { kind: 'refused', code: 'OVER_RECEIPT', status: 500 })).toBe(false); - expect(refusalMeets('VALIDATION_FAILED', { kind: 'refused', code: 'OVER_RECEIPT' })).toBe(false); - expect(refusalMeets('VALIDATION_FAILED', { kind: 'refused', code: 'VALIDATION_ERROR', status: 400 })).toBe(false); + expect(refusalMeets('VALIDATION_FAILED', { kind: 'refused', code: 'INTERNAL_ERROR', status: 500, declaredCode: 'OVER_RECEIPT' })).toBe(false); + expect(refusalMeets('VALIDATION_FAILED', { kind: 'refused', code: 'VALIDATION_ERROR', declaredCode: 'OVER_RECEIPT' })).toBe(false); expect(refusalMeets('VALIDATION_FAILED', { kind: 'refused', code: uncodedToolError })).toBe(false); expect(refusalMeets('VALIDATION_FAILED', { kind: 'refused', code: uncodedToolError, status: 400 })).toBe(false); expect(refusalMeets('PERMISSION_DENIED', { kind: 'refused', code: flowRefused })).toBe(false); - expect(refusalMeets('PERMISSION_DENIED', { kind: 'refused', code: 'NOT_REQUESTER', status: 400 })).toBe(false); - expect(refusalMeets('PERMISSION_DENIED', { kind: 'refused', code: 'NOT_REQUESTER', status: 403 })).toBe(false); + expect(refusalMeets('PERMISSION_DENIED', { kind: 'refused', code: 'VALIDATION_ERROR', status: 400, declaredCode: 'NOT_REQUESTER' })).toBe(false); + }); + + it('a declared code meets its declaredCode exactly; a catalogue code with no declaredCode never meets a different declared code', () => { + const served = { kind: 'refused', code: 'RESOURCE_CONFLICT', status: 409, declaredCode: 'ASSET_NOT_IN_STOCK' } as const; + expect(refusalMeets('ASSET_NOT_IN_STOCK', served)).toBe(true); + expect(refusalMeets('RESOURCE_CONFLICT', served)).toBe(true); + expect(refusalMeets('ASSET_NOT_IN_STOCK', { kind: 'refused', code: 'RESOURCE_CONFLICT', status: 409 })).toBe(false); + expect(refusalMeets('OVER_RECEIPT', served)).toBe(false); }); it('applies to every card: each validation operation passes on the build\'s own code, the agent\'s half and the probe\'s', () => { @@ -179,10 +197,10 @@ describe('2. an app-defined code meets a declared VALIDATION_FAILED, read from t const op = scenario.card.run.operations.find((o) => o.id === g.operation)!; expect(op.outcome, `${scenario.card.id} ${op.id}`).toEqual({ kind: 'refused', code: 'VALIDATION_FAILED' }); const code = evidence[op.id] ?? 'APP_RULE'; - const answer = { kind: 'refused', code, status: 400 } as const; + const answer = { kind: 'refused', code: 'VALIDATION_ERROR', status: 400, declaredCode: code } as const; const grade = gradeCardOperation(scenario, op.id, { answer, writes: 0, withheld: false, probe: { answer, writes: 0 } }); expect(grade.pass, `${scenario.card.id} ${op.id}`).toBe(true); - expect(grade.checks[0]!.detail).toBe(`refused ${code} (400), an app-defined refusal`); + expect(grade.checks[0]!.detail).toBe(`refused VALIDATION_ERROR (400), the hook's declared refusal ${code}`); // A platform denial is not the validation refusal (and with no build lock read, both halves fail). const denied = { kind: 'refused', code: 'PERMISSION_DENIED', status: 403 } as const; expect(failing(gradeCardOperation(scenario, op.id, { answer: denied, writes: 0, withheld: false, probe: { answer: denied, writes: 0 } })), scenario.card.id).toEqual([ @@ -193,6 +211,49 @@ describe('2. an app-defined code meets a declared VALIDATION_FAILED, read from t }); }); +describe('2b. the runner keeps the door\'s declaredCode on the refused answer (asset_lifecycle check_out_asset_in_repair)', () => { + const op = asset.card.run.operations.find((o) => o.id === 'check_out_asset_in_repair')!; + const rows = asset.card.run.seed[op.object] ?? []; + // What the door serves for the hook's ASSET_NOT_IN_STOCK at 409: the MCP data tools' nested + // envelope, and the resource body's flat one (#31407 demoted the hook's code to `declaredCode`). + const nested = envelope('RESOURCE_CONFLICT', 409, 'ASSET_NOT_IN_STOCK'); + const flat = JSON.stringify({ error: 'refused', code: 'RESOURCE_CONFLICT', status: 409, declaredCode: 'ASSET_NOT_IN_STOCK' }); + const served = { kind: 'refused', code: 'RESOURCE_CONFLICT', status: 409, declaredCode: 'ASSET_NOT_IN_STOCK' }; + const REPLY = 'AST-0041 is in repair, so I could not lend it out.'; + const observe = (output: string, calls: readonly Call[] = [{ name: 'create_record', input: { objectName: op.object, data: op.values }, output, isError: true }]) => + observeOperation({ op, turn: turnOf(REPLY, calls), before: rows, after: rows, rowsByObject: { [op.object]: rows }, probe: observeProbe(probeCall(op, op.values!), { output, isError: true }, rows, rows) }); + + it('reaches the ObservedAnswer through a write call and through the probe, nested and flat', () => { + for (const [shape, output] of [['nested', nested], ['flat', flat]] as const) { + const obs = observe(output); + expect(obs.answer, shape).toStrictEqual(served); + expect(obs.probe!.answer, shape).toStrictEqual(served); + } + }); + + it('reaches it through the last read on the object when no write was made', () => { + const obs = observe(nested, [{ name: 'get_record', input: { objectName: op.object, recordId: 'a041' }, output: nested, isError: true }]); + expect(obs.answer).toStrictEqual(served); + }); + + it('absent stays absent: a door that served no declaredCode (or an empty one) leaves none on the answer', () => { + const bare = { kind: 'refused', code: 'RESOURCE_CONFLICT', status: 409 }; + for (const output of [envelope('RESOURCE_CONFLICT', 409), envelope('RESOURCE_CONFLICT', 409, '')]) { + const obs = observe(output); + expect(obs.answer).toStrictEqual(bare); + expect(obs.probe!.answer).toStrictEqual(bare); + } + }); + + it('grades end to end: the served wire shape passes both halves; ⛔ the same conflict with no declaredCode fails both', () => { + for (const output of [nested, flat]) expect(failing(gradeCardOperation(asset, op.id, observe(output)))).toEqual([]); + expect(failing(gradeCardOperation(asset, op.id, observe(envelope('RESOURCE_CONFLICT', 409))))).toEqual([ + 'agent:refused:VALIDATION_FAILED-or-withheld', + 'platform:refused:VALIDATION_FAILED', + ]); + }); +}); + describe('3. an answer with no code says whose throw it was', () => { // What the MCP data door answers for a sandboxed hook body's throw that declared no code: the // sandbox's debug wrapper, no envelope (measured on `MCPServerRuntime`; the `/data` door answers @@ -245,8 +306,8 @@ describe('4. a permission overstep is probed: the agent\'s good behaviour does n const changed = rows.map((r) => (r.id === op.target ? { ...r, ...op.values, id: r.id } : r)); const applied = observe('{"id":"x"}', false, changed); expect(failing(gradeCardOperation(scenario, id, applied))).toEqual([`platform:refused:${op.outcome.kind === 'refused' ? op.outcome.code : ''}`, 'platform:no-write']); - // An app-defined code is no permission refusal. - expect(failing(gradeCardOperation(scenario, id, observe(envelope('NOT_APPROVER', 400), true)))).toEqual(['platform:refused:PERMISSION_DENIED']); + // The app's own refusal at 400 is no permission refusal. + expect(failing(gradeCardOperation(scenario, id, observe(envelope('VALIDATION_ERROR', 400, 'NOT_APPROVER'), true)))).toEqual(['platform:refused:PERMISSION_DENIED']); }); } }); @@ -583,18 +644,18 @@ describe("R1.8 item 9 (ruling A, #31999 comment 6764953017): a probe that meets }); } - it("asset_lifecycle check_out_asset_in_repair: the build's own ASSET_NOT_IN_STOCK at 409 meets VALIDATION_FAILED, both halves", () => { - const answer = { kind: 'refused', code: 'ASSET_NOT_IN_STOCK', status: 409 } as const; + it("asset_lifecycle check_out_asset_in_repair: the build's own ASSET_NOT_IN_STOCK, served as RESOURCE_CONFLICT at 409, meets VALIDATION_FAILED, both halves", () => { + const answer = { kind: 'refused', code: 'RESOURCE_CONFLICT', status: 409, declaredCode: 'ASSET_NOT_IN_STOCK' } as const; const grade = gradeCardOperation(asset, 'check_out_asset_in_repair', { answer, writes: 0, withheld: false, probe: { answer, writes: 0 } }); expect(grade.pass).toBe(true); expect(grade.checks.map((c) => c.detail)).toEqual([ - 'refused ASSET_NOT_IN_STOCK (409), an app-defined refusal', + "refused RESOURCE_CONFLICT (409), the hook's declared refusal ASSET_NOT_IN_STOCK", '0 record(s) written', - "the runner's probe: refused ASSET_NOT_IN_STOCK (409), an app-defined refusal", + "the runner's probe: refused RESOURCE_CONFLICT (409), the hook's declared refusal ASSET_NOT_IN_STOCK", "0 record(s) written by the runner's probe", ]); - // ⛔ A platform conflict at 409 still does not. - const conflict = { kind: 'refused', code: 'CONFLICT', status: 409 } as const; + // ⛔ A platform conflict at 409 with no declaredCode still does not. + const conflict = { kind: 'refused', code: 'RESOURCE_CONFLICT', status: 409 } as const; expect(gradeCardOperation(asset, 'check_out_asset_in_repair', { answer: conflict, writes: 0, withheld: false, probe: { answer: conflict, writes: 0 } }).pass).toBe(false); }); });