|
10 | 10 | * → boot, seed, one user + key per persona → the Run half |
11 | 11 | * (every question as every persona it names, `askRepeats` times |
12 | 12 | * each, every operation once as its persona, each over the app's |
13 | | - * own MCP, and a probed |
| 13 | + * own MCP; a `pending_confirmation` operation the agent met by |
| 14 | + * asking first gets a second turn carrying the person's |
| 15 | + * confirmation, whose write is graded on its own; and a probed |
14 | 16 | * operation's declared write — the validation operation's and |
15 | 17 | * each permission overstep's — re-sent by the runner itself as |
16 | 18 | * that persona, `observeProbe`; an `applied` operation's too |
@@ -47,29 +49,36 @@ import { SECURITY_ROLLUP_SOURCE_UNREADABLE, validateRollupSourceVisibility, type |
47 | 49 | import { applyExpectDelta, readableNumbers, truthsOver, type LoadedScenario } from '../loader.js'; |
48 | 50 | import { isProbedOperation, PERSON_USER_FIELD, probesBuildDenial, scenarioNamespace, type CardTitle } from '../schema.js'; |
49 | 51 | import { |
| 52 | + askedFirst, |
| 53 | + flowRefusalOf, |
50 | 54 | gradeCardAnswer, |
51 | 55 | gradeCardInjection, |
| 56 | + gradeConfirmedWrite, |
52 | 57 | buildDeniesWrite, |
53 | 58 | gradeCardOperation, |
54 | 59 | INJECTION_AS_DATA_CHECK, |
55 | 60 | isWriteDenial, |
56 | 61 | readArtifactFacts, |
| 62 | + refusalCodeOf, |
57 | 63 | scoreCardArtifact, |
58 | 64 | type CardTurn, |
59 | 65 | type CheckResult, |
| 66 | + type ConfirmedWriteGrade, |
| 67 | + type ConfirmedWriteObservation, |
60 | 68 | type OperationGrade, |
61 | 69 | type OperationObservation, |
62 | 70 | type PersonaRunGrade, |
63 | 71 | type ProbeObservation, |
64 | 72 | type ScenarioArtifactScore, |
65 | 73 | } from '../score/index.js'; |
66 | 74 | import type { AppHarness, PersonaAccount } from './app.js'; |
67 | | -import { ReplayNotFaithfulError, servedAsRequested, type AgentDriver, type AskTask, type StepRef } from './driver.js'; |
| 75 | +import { MissingRecordingError, ReplayNotFaithfulError, servedAsRequested, type AgentDriver, type AskTask, type StepRef } from './driver.js'; |
68 | 76 | import { citationLabels, namespaceOfArtifact, namespacedScenario, permissionSetName, templateHostName } from './namespace.js'; |
69 | | -import { agentSetOf, clockDates, observeOperation, observeProbe, probeCall, touchedRowId, writeCallsOn, type Row } from './observe.js'; |
| 77 | +import { agentSetOf, clockDates, countWrites, fingerprint, observedAnswer, observeOperation, observeProbe, probeCall, touchedRowId, writeCallsOn, type CardOperation, type Row } from './observe.js'; |
70 | 78 | import type { CommandResult } from './project.js'; |
71 | 79 | import { |
72 | 80 | buildPrompt, |
| 81 | + confirmPrompt, |
73 | 82 | iteratePrompt, |
74 | 83 | operationPrompt, |
75 | 84 | personaHandles, |
@@ -273,9 +282,42 @@ export interface OperationResult { |
273 | 282 | observation?: OperationObservation; |
274 | 283 | grade?: OperationGrade; |
275 | 284 | turn?: TurnSummary; |
| 285 | + /** |
| 286 | + * The second turn of a `pending_confirmation` operation, sent only when the |
| 287 | + * first one passed by asking first (`OperationGrade.confirmation` is |
| 288 | + * `asked first`): the person confirms, and the turn's write is graded on |
| 289 | + * its own ({@link ConfirmTurnResult}). Absent on every other operation, and |
| 290 | + * on one whose first turn wrote, failed or measured nothing. |
| 291 | + */ |
| 292 | + confirmTurn?: ConfirmTurnResult; |
276 | 293 | error?: string; |
277 | 294 | } |
278 | 295 |
|
| 296 | +/** |
| 297 | + * The confirmation turn of a `pending_confirmation` operation |
| 298 | + * ({@link OperationResult.confirmTurn}): a fresh turn as the same persona whose |
| 299 | + * prompt carries the first turn's ask and reply and then the person's |
| 300 | + * confirmation (`confirmPrompt`), its write observed and graded against the |
| 301 | + * operation's declared write (`gradeCardConfirmedWrite`). Its own grade, never |
| 302 | + * merged into the first turn's, so neither half hides the other. |
| 303 | + */ |
| 304 | +export interface ConfirmTurnResult { |
| 305 | + /** |
| 306 | + * Why the confirmed write measured nothing: the replayed recording holds no |
| 307 | + * confirmation turn ({@link confirmationNotRecorded}), or the replay of it |
| 308 | + * was not faithful (`ReplayNotFaithfulError`). Never a fail; the operation |
| 309 | + * is then scored on its first turn alone, as before. |
| 310 | + */ |
| 311 | + notMeasured?: string; |
| 312 | + observation?: ConfirmedWriteObservation; |
| 313 | + grade?: ConfirmedWriteGrade; |
| 314 | + turn?: TurnSummary; |
| 315 | + error?: string; |
| 316 | +} |
| 317 | + |
| 318 | +/** Why a replayed operation's confirmed write is not measured: its recording was made before the runner sent a confirmation turn. */ |
| 319 | +export const confirmationNotRecorded = 'the recording holds no confirmation turn for this operation, so the confirmed write was not replayed'; |
| 320 | + |
279 | 321 | export interface RunHalf { |
280 | 322 | personas: Array<Omit<PersonaAccount, 'apiKey'>>; |
281 | 323 | seed: { |
@@ -772,6 +814,45 @@ export function actionGatesOf(built: unknown, op: Operation): Record<string, boo |
772 | 814 | return gates; |
773 | 815 | } |
774 | 816 |
|
| 817 | +/** |
| 818 | + * The confirmation turn of a `pending_confirmation` operation, as the grader's |
| 819 | + * `ConfirmedWriteObservation` (`gradeConfirmedWrite`), read the way |
| 820 | + * `observe.ts` reads the first turn: the door's answer by its ordered rules |
| 821 | + * (`observedAnswer`), the ids of the operation object's rows the turn added, |
| 822 | + * removed or changed (keyed by `id`, every column but the clock), the target |
| 823 | + * after it, the write doors that applied a write there (`run_action <name>` |
| 824 | + * for a business action; reported, never graded), the dates the platform's |
| 825 | + * clock read and the fields the turn's own writes set. |
| 826 | + */ |
| 827 | +export function observeConfirmedWrite(op: CardOperation, turn: ParsedTurn, before: readonly Row[], after: readonly Row[], runDates?: readonly string[]): ConfirmedWriteObservation { |
| 828 | + const writes = countWrites(before, after); |
| 829 | + const was = new Map(before.map((r) => [String(r.id), fingerprint(r)])); |
| 830 | + const now = new Map(after.map((r) => [String(r.id), fingerprint(r)])); |
| 831 | + const changed = [...new Set([...was.keys(), ...now.keys()])].filter((id) => was.get(id) !== now.get(id)).sort(); |
| 832 | + const applied = writeCallsOn(turn, op.object).filter( |
| 833 | + (c) => c.output !== undefined && c.isError !== true && refusalCodeOf(c.output) === undefined && flowRefusalOf(c.output) === undefined, |
| 834 | + ); |
| 835 | + const via = applied.map((c) => { |
| 836 | + if (c.name !== 'run_action') return c.name; |
| 837 | + let action: unknown; |
| 838 | + try { |
| 839 | + action = (JSON.parse(typeof c.output === 'string' ? c.output : JSON.stringify(c.output)) as { action?: unknown }).action; |
| 840 | + } catch { |
| 841 | + action = undefined; |
| 842 | + } |
| 843 | + return typeof action === 'string' ? `run_action ${action}` : c.name; |
| 844 | + }); |
| 845 | + return { |
| 846 | + answer: observedAnswer(turn, op.object, writes), |
| 847 | + writes, |
| 848 | + changed, |
| 849 | + target: (op.target !== undefined ? after.find((r) => String(r.id) === op.target) : undefined) ?? null, |
| 850 | + via, |
| 851 | + ...(runDates !== undefined ? { runDates } : {}), |
| 852 | + agentSet: agentSetOf(op, turn), |
| 853 | + }; |
| 854 | +} |
| 855 | + |
775 | 856 | function personaOfUser(userId: string | null, accounts: readonly PersonaAccount[], adminId: string): string | null { |
776 | 857 | if (userId === null) return null; |
777 | 858 | const hit = accounts.find((a) => a.userId === userId); |
@@ -960,7 +1041,10 @@ export async function seedApp( |
960 | 1041 | * key is the one a single ask has. Every question is asked before any |
961 | 1042 | * operation writes, so each ask reads the same rows. A turn the seed takes out |
962 | 1043 | * ({@link seedDependence}) is not measured at every ask alike. Operations run |
963 | | - * once. The guardrails are graded over every turn that ran, each ask included. |
| 1044 | + * once; a `pending_confirmation` one the agent met by asking first is followed |
| 1045 | + * by its confirmation turn ({@link OperationResult.confirmTurn}). The |
| 1046 | + * guardrails are graded over every turn that ran, each ask and each |
| 1047 | + * confirmation turn included. |
964 | 1048 | * Every question and operation turn is sent in the prompt choice `choice` |
965 | 1049 | * ({@link RunOptions.promptChoice}); absent ⇒ the default prompts. |
966 | 1050 | */ |
@@ -1098,6 +1182,42 @@ export async function runHalf( |
1098 | 1182 | if (op.kind !== 'delete' && res.grade.checks.some((c) => c.notMeasured?.startsWith(buildDeniesWrite))) { |
1099 | 1183 | res.buildDenial = { persona: op.actingAs, object: op.object, verb: op.kind }; |
1100 | 1184 | } |
| 1185 | + // The agent asked first and wrote nothing: the person confirms, in a |
| 1186 | + // turn of its own, and that turn's write is graded on its own. |
| 1187 | + if (op.outcome.kind === 'pending_confirmation' && res.grade.confirmation === askedFirst) { |
| 1188 | + const confirmed: ConfirmTurnResult = {}; |
| 1189 | + try { |
| 1190 | + const beforeConfirm = (await app.listRows(op.object)).rows as Row[]; |
| 1191 | + const startedConfirm = new Date(); |
| 1192 | + let second: ParsedTurn | undefined; |
| 1193 | + try { |
| 1194 | + second = await driver.ask({ |
| 1195 | + ...ref, |
| 1196 | + step: op.id, |
| 1197 | + persona: op.actingAs, |
| 1198 | + confirmation: true, |
| 1199 | + prompt: confirmPrompt(card, op.id, turn.replyText, choice), |
| 1200 | + ...doorOf(op.actingAs), |
| 1201 | + }); |
| 1202 | + } catch (e) { |
| 1203 | + if (e instanceof MissingRecordingError) confirmed.notMeasured = confirmationNotRecorded; |
| 1204 | + else if (e instanceof ReplayNotFaithfulError) { |
| 1205 | + confirmed.turn = summarize(e.turn); |
| 1206 | + confirmed.notMeasured = replayNotFaithful(e); |
| 1207 | + } else throw e; |
| 1208 | + } |
| 1209 | + if (second !== undefined) { |
| 1210 | + turns.push({ step: op.id, persona: op.actingAs, turn: second }); |
| 1211 | + confirmed.turn = summarize(second); |
| 1212 | + const afterConfirm = (await app.listRows(op.object)).rows as Row[]; |
| 1213 | + confirmed.observation = observeConfirmedWrite(op, second, beforeConfirm, afterConfirm, clockDates(startedConfirm, new Date())); |
| 1214 | + confirmed.grade = gradeConfirmedWrite(op, confirmed.observation, card.asOf !== undefined ? { asOf: card.asOf } : {}); |
| 1215 | + } |
| 1216 | + } catch (e) { |
| 1217 | + confirmed.error = errText(e); |
| 1218 | + } |
| 1219 | + res.confirmTurn = confirmed; |
| 1220 | + } |
1101 | 1221 | } catch (e) { |
1102 | 1222 | res.error = errText(e); |
1103 | 1223 | } |
|
0 commit comments