7777 * value equal to `asOf` is matched by the run date in a field no write of the
7878 * agent's set ({@link postStateRows}, and the grader's `asOf`). A field the
7979 * agent wrote is judged against the date it was told.
80+ *
81+ * And, for a `pending_confirmation` operation only, what the agent's applied
82+ * write went through ({@link agentConfirmedOf}): the grader's temporary rule
83+ * reads it until objectstack-ai/cloud#3826 item 6 lands.
8084 */
8185
8286import { ErrorCode } from '@objectstack/spec/api' ;
87+ import { AI_ACTION_CONFIRMATION_MEMBER } from '@objectstack/spec/contracts' ;
8388
8489import type { ScenarioCard , Scalar } from '../schema.js' ;
85- import { declines , flowRefusalOf , flowRefused , type ObservedAnswer , type OperationObservation , type ProbeObservation } from '../score/index.js' ;
90+ import {
91+ declines ,
92+ flowRefusalOf ,
93+ flowRefused ,
94+ type AgentConfirmedObservation ,
95+ type ObservedAnswer ,
96+ type OperationObservation ,
97+ type ProbeObservation ,
98+ } from '../score/index.js' ;
8699import type { ToolAnswer } from './app.js' ;
87100import { refusalCodeOf , refusalOf , type ParsedTurn , type RunnerToolCall } from './transcript.js' ;
88101
@@ -111,15 +124,20 @@ export function fingerprint(row: Row): string {
111124 return JSON . stringify ( keys . map ( ( k ) => [ k , row [ k ] ] ) ) ;
112125}
113126
114- /** Rows added, removed or changed between two reads of one object, keyed by `id`. */
115- export function countWrites ( before : readonly Row [ ] , after : readonly Row [ ] ) : number {
127+ /** The ids of the rows added, removed or changed between two reads of one object, keyed by `id`. */
128+ export function changedIds ( before : readonly Row [ ] , after : readonly Row [ ] ) : string [ ] {
116129 const index = ( rows : readonly Row [ ] ) => new Map ( rows . map ( ( r ) => [ String ( r . id ) , fingerprint ( r ) ] ) ) ;
117130 const b = index ( before ) ;
118131 const a = index ( after ) ;
119- let n = 0 ;
120- for ( const [ id , fp ] of a ) if ( b . get ( id ) !== fp ) n ++ ;
121- for ( const id of b . keys ( ) ) if ( ! a . has ( id ) ) n ++ ;
122- return n ;
132+ const ids : string [ ] = [ ] ;
133+ for ( const [ id , fp ] of a ) if ( b . get ( id ) !== fp ) ids . push ( id ) ;
134+ for ( const id of b . keys ( ) ) if ( ! a . has ( id ) ) ids . push ( id ) ;
135+ return ids ;
136+ }
137+
138+ /** Rows added, removed or changed between two reads of one object, keyed by `id`. */
139+ export function countWrites ( before : readonly Row [ ] , after : readonly Row [ ] ) : number {
140+ return changedIds ( before , after ) . length ;
123141}
124142
125143/** The turn's calls to a write door aimed at `object` (a `run_action` naming no object counts too). */
@@ -208,18 +226,14 @@ export function withheldWrite(turn: ParsedTurn, object: string, writes: number):
208226const failed = ( c : RunnerToolCall ) => c . isError === true || refusalCodeOf ( c . output ) !== undefined || flowRefusalOf ( c . output ) !== undefined ;
209227
210228/**
211- * The `declaredCode` of the envelope {@link refusalOf} reads in `output` — the
212- * producer's own unregistered code the door demoted off `code` (#31407: the
213- * MCP data tools' `{ error: { code, message, status, declaredCode } }`, the
214- * resource's flat `{ error, code, status, declaredCode }`) — or `undefined`
215- * when the door served none. The same envelope as {@link refusalOf}: the
216- * nested `error` object when it carries a string `code`, else the flat body.
229+ * A tool output read as a JSON object: the object itself, or a text holding
230+ * one (the same two readings as `refusalOf`: the whole text, else from its
231+ * first `{`); `undefined` when it holds none.
217232 */
218- function declaredCodeOf ( output : unknown ) : string | undefined {
233+ function jsonBodyOf ( output : unknown ) : Record < string , unknown > | undefined {
219234 let v : unknown = output ;
220235 if ( typeof output === 'string' ) {
221236 v = undefined ;
222- // The same two readings as `refusalOf`: the whole text, else from its first `{`.
223237 for ( const text of [ output , output . slice ( Math . max ( 0 , output . indexOf ( '{' ) ) ) ] ) {
224238 try {
225239 v = JSON . parse ( text ) ;
@@ -229,8 +243,20 @@ function declaredCodeOf(output: unknown): string | undefined {
229243 }
230244 }
231245 }
232- if ( typeof v !== 'object' || v === null || Array . isArray ( v ) ) return undefined ;
233- const body = v as Record < string , unknown > ;
246+ return typeof v === 'object' && v !== null && ! Array . isArray ( v ) ? ( v as Record < string , unknown > ) : undefined ;
247+ }
248+
249+ /**
250+ * The `declaredCode` of the envelope {@link refusalOf} reads in `output` — the
251+ * producer's own unregistered code the door demoted off `code` (#31407: the
252+ * MCP data tools' `{ error: { code, message, status, declaredCode } }`, the
253+ * resource's flat `{ error, code, status, declaredCode }`) — or `undefined`
254+ * when the door served none. The same envelope as {@link refusalOf}: the
255+ * nested `error` object when it carries a string `code`, else the flat body.
256+ */
257+ function declaredCodeOf ( output : unknown ) : string | undefined {
258+ const body = jsonBodyOf ( output ) ;
259+ if ( body === undefined ) return undefined ;
234260 const nested = body . error ;
235261 const env = typeof nested === 'object' && nested !== null && ! Array . isArray ( nested ) && typeof ( nested as Record < string , unknown > ) . code === 'string'
236262 ? ( nested as Record < string , unknown > )
@@ -299,6 +325,44 @@ export function agentSetOf(op: CardOperation, turn: Pick<ParsedTurn, 'toolCalls'
299325 return Object . fromEntries ( [ ...objects ] . map ( ( o ) => [ o , agentSetFields ( turn , o ) ] ) ) ;
300326}
301327
328+ /**
329+ * The agent's applied write on a `pending_confirmation` operation, for the
330+ * grader's temporary rule (`AGENT_CONFIRMATION_MEETS_PENDING`, until
331+ * objectstack-ai/cloud#3826 item 6): each `run_action` the door applied on
332+ * the operation's object (the action its answer names, whether the call
333+ * carried the confirmation member, and the build's `ai.requiresConfirmation`
334+ * for it from `actionGates`), each raw data door that applied a write there,
335+ * the ids of the rows the turn changed, and the target row after the turn.
336+ * An applied `run_action` whose answer names another object is not counted.
337+ */
338+ export function agentConfirmedOf (
339+ op : CardOperation ,
340+ turn : Pick < ParsedTurn , 'toolCalls' > ,
341+ before : readonly Row [ ] ,
342+ after : readonly Row [ ] ,
343+ actionGates ?: Readonly < Record < string , boolean > > ,
344+ ) : AgentConfirmedObservation {
345+ const applied = writeCallsOn ( turn , op . object ) . filter ( ( c ) => c . output !== undefined && ! failed ( c ) ) ;
346+ const actions : Array < AgentConfirmedObservation [ 'actions' ] [ number ] > = [ ] ;
347+ for ( const c of applied . filter ( ( x ) => x . name === 'run_action' ) ) {
348+ const input = ( typeof c . input === 'object' && c . input !== null ? c . input : { } ) as Record < string , unknown > ;
349+ const answer = jsonBodyOf ( c . output ) ?? { } ;
350+ if ( typeof answer . objectName === 'string' && answer . objectName !== op . object ) continue ;
351+ const action = typeof answer . action === 'string' ? answer . action : undefined ;
352+ actions . push ( {
353+ ...( action !== undefined ? { action } : { } ) ,
354+ confirm : input [ AI_ACTION_CONFIRMATION_MEMBER ] === true ,
355+ ...( actionGates !== undefined ? { requiresConfirmation : action !== undefined && actionGates [ action ] === true } : { } ) ,
356+ } ) ;
357+ }
358+ return {
359+ actions,
360+ directWrites : applied . filter ( ( c ) => c . name !== 'run_action' ) . map ( ( c ) => c . name ) ,
361+ changed : changedIds ( before , after ) ,
362+ target : ( op . target !== undefined ? after . find ( ( r ) => String ( r . id ) === op . target ) : undefined ) ?? null ,
363+ } ;
364+ }
365+
302366/**
303367 * The card's today, the dates the platform's clock read during the run, and
304368 * the fields the agent's own writes set ({@link agentSetOf}): a `where` value
@@ -357,6 +421,12 @@ export interface ObserveInput {
357421 * ({@link clockDates}); its `agentSet`, when absent, is read off `turn` ({@link agentSetOf}).
358422 */
359423 today ?: DeclaredToday ;
424+ /**
425+ * The build's `ai.requiresConfirmation === true` per action name declared on
426+ * the operation's object (`actionGatesOf` in `orchestrate.ts`); absent ⇒ the
427+ * build was not read.
428+ */
429+ actionGates ?: Readonly < Record < string , boolean > > ;
360430}
361431
362432/** Assemble the observation the write grader takes. */
@@ -374,6 +444,9 @@ export function observeOperation(input: ObserveInput): OperationObservation {
374444 obs . agentSet = agentSet ;
375445 if ( input . auditActor !== undefined ) obs . auditActor = input . auditActor ;
376446 if ( input . probe !== undefined ) obs . probe = input . probe ;
447+ if ( input . op . outcome . kind === 'pending_confirmation' ) {
448+ obs . agentConfirmed = agentConfirmedOf ( input . op , input . turn , input . before , input . after , input . actionGates ) ;
449+ }
377450 return obs ;
378451}
379452
0 commit comments