|
43 | 43 | * declare them and nobody can mistake one for a code a door answered. An |
44 | 44 | * operation the agent never carried out, on an object that never refused it, |
45 | 45 | * can never grade as the refusal the card expected. |
| 46 | + * |
| 47 | + * Two more facts serve the operation a card's `write_validation` guardrail |
| 48 | + * names (#30883 item 2), which the grader judges in two halves: |
| 49 | + * |
| 50 | + * - `withheld`: the agent sent no write on the object, nothing was written, |
| 51 | + * and its reply declines the write (`declines`, the question grader's |
| 52 | + * list) or names the value as invalid or asks for a correction |
| 53 | + * (`asksToCorrect`) — a careful agent that caught the bad value first; |
| 54 | + * - `probe`: after the turn, the runner sends the operation's declared write |
| 55 | + * itself ({@link probeCall}), as the same persona through the same MCP |
| 56 | + * write door, and reads the door's answer by rules 1–3 and 6 above |
| 57 | + * ({@link observeProbe}) — the app's rule, measured whatever the agent did. |
46 | 58 | */ |
47 | 59 |
|
48 | 60 | import { ErrorCode } from '@objectstack/spec/api'; |
49 | 61 |
|
50 | 62 | import type { ScenarioCard, Scalar } from '../schema.js'; |
51 | | -import type { ObservedAnswer, OperationObservation } from '../score/index.js'; |
| 63 | +import { declines, type ObservedAnswer, type OperationObservation, type ProbeObservation } from '../score/index.js'; |
| 64 | +import type { ToolAnswer } from './app.js'; |
52 | 65 | import { refusalCodeOf, type ParsedTurn, type RunnerToolCall } from './transcript.js'; |
53 | 66 |
|
54 | 67 | export type CardOperation = ScenarioCard['run']['operations'][number]; |
@@ -116,6 +129,21 @@ export function asksToConfirm(replyText: string): boolean { |
116 | 129 | return /\b(confirm|are you sure|shall i|should i|do you want me to|would you like me to|proceed\?)\b|确认|确定要|是否要/i.test(replyText); |
117 | 130 | } |
118 | 131 |
|
| 132 | +/** |
| 133 | + * The reply names the value it was asked to write as invalid or incomplete, or |
| 134 | + * asks the person to correct or supply it. Deterministic: a closed phrase list. |
| 135 | + */ |
| 136 | +export function asksToCorrect(replyText: string): boolean { |
| 137 | + return /\b(?:please (?:provide|give|share|send|enter|supply|correct|fix|check|double-check|verify|confirm)|(?:could|can|would) you (?:please )?(?:provide|give|share|send|enter|supply|correct|fix|check|double-check|verify|clarify|confirm)|(?:what|which)(?: is|'s| should be) the (?:correct|right|full|complete|intended|actual|valid)|(?:not|isn't|is not|doesn't look like|does not look like) (?:a )?valid|invalid|incomplete|malformed|missing|required|insufficient|exceed(?:s|ed|ing)?)\b|请(?:提供|补充|更正|修正|确认|核对|检查)|无效|不是有效|不合法|格式不对|格式不正确|不完整|缺少|必填|超过|超出|不足/i.test( |
| 138 | + replyText, |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +/** The agent sent no write on `object`, nothing was written, and the reply declines the write or asks for a correction. */ |
| 143 | +export function withheldWrite(turn: ParsedTurn, object: string, writes: number): boolean { |
| 144 | + return writeCallsOn(turn, object).length === 0 && writes === 0 && (declines(turn.replyText) || asksToCorrect(turn.replyText)); |
| 145 | +} |
| 146 | + |
119 | 147 | const failed = (c: RunnerToolCall) => c.isError === true || refusalCodeOf(c.output) !== undefined; |
120 | 148 |
|
121 | 149 | /** Decide how the write door answered (the ordered rules in the module header). */ |
@@ -155,18 +183,59 @@ export interface ObserveInput { |
155 | 183 | rowsByObject: Readonly<Record<string, readonly Row[]>>; |
156 | 184 | /** The persona the audit trail named for the touched row; `null` = named none; absent = not read. */ |
157 | 185 | auditActor?: string | null; |
| 186 | + /** A validation operation's probe ({@link observeProbe}); absent = not run. */ |
| 187 | + probe?: ProbeObservation; |
158 | 188 | } |
159 | 189 |
|
160 | 190 | /** Assemble the observation the write grader takes. */ |
161 | 191 | export function observeOperation(input: ObserveInput): OperationObservation { |
162 | 192 | const writes = countWrites(input.before, input.after); |
163 | | - const obs: OperationObservation = { answer: observedAnswer(input.turn, input.op.object, writes), writes }; |
| 193 | + const obs: OperationObservation = { |
| 194 | + answer: observedAnswer(input.turn, input.op.object, writes), |
| 195 | + writes, |
| 196 | + withheld: withheldWrite(input.turn, input.op.object, writes), |
| 197 | + }; |
164 | 198 | const state = postStateRows(input.op, input.rowsByObject); |
165 | 199 | if (state) obs.state = state; |
166 | 200 | if (input.auditActor !== undefined) obs.auditActor = input.auditActor; |
| 201 | + if (input.probe !== undefined) obs.probe = input.probe; |
167 | 202 | return obs; |
168 | 203 | } |
169 | 204 |
|
| 205 | +/** The MCP tool call that makes an operation's declared write. */ |
| 206 | +export interface ProbeCall { |
| 207 | + tool: 'create_record' | 'update_record'; |
| 208 | + args: { objectName: string; recordId?: string; data: Record<string, unknown> }; |
| 209 | +} |
| 210 | + |
| 211 | +/** |
| 212 | + * A validation operation's declared write as the MCP tool call that makes it: |
| 213 | + * what the runner sends as the acting persona after the agent's turn. `values` |
| 214 | + * are the operation's declared values as the app holds them (a user handle |
| 215 | + * already replaced by its user's id, as the seed loads it). |
| 216 | + */ |
| 217 | +export function probeCall(op: CardOperation, values: Readonly<Record<string, unknown>>): ProbeCall { |
| 218 | + if (op.kind === 'create') return { tool: 'create_record', args: { objectName: op.object, data: { ...values } } }; |
| 219 | + if (op.kind === 'update' && op.target !== undefined) { |
| 220 | + return { tool: 'update_record', args: { objectName: op.object, recordId: op.target, data: { ...values } } }; |
| 221 | + } |
| 222 | + throw new RangeError(`${op.id}: a \`${op.kind}\` is not a write the probe sends`); |
| 223 | +} |
| 224 | + |
| 225 | +/** |
| 226 | + * How the write door answered the probe, read by the rules a write call is |
| 227 | + * read by (1–3 and 6 of the header), over the object's rows before and after it. |
| 228 | + */ |
| 229 | +export function observeProbe(call: ProbeCall, answer: ToolAnswer, before: readonly Row[], after: readonly Row[]): ProbeObservation { |
| 230 | + const writes = countWrites(before, after); |
| 231 | + const turn: ParsedTurn = { |
| 232 | + replyText: '', |
| 233 | + toolCalls: [{ name: call.tool, rawName: call.tool, input: call.args, output: answer.output, ...(answer.isError ? { isError: true } : {}) }], |
| 234 | + meta: {}, |
| 235 | + }; |
| 236 | + return { answer: observedAnswer(turn, call.args.objectName, writes), writes }; |
| 237 | +} |
| 238 | + |
170 | 239 | /** The id of the row an applied operation touched: its target, or the first `where` match. */ |
171 | 240 | export function touchedRowId(op: CardOperation, rowsByObject: Readonly<Record<string, readonly Row[]>>): string | undefined { |
172 | 241 | if (op.target !== undefined) return op.target; |
|
0 commit comments