|
19 | 19 | * How the answer is decided, in order (the PR body records the reading this |
20 | 20 | * was measured against): |
21 | 21 | * |
22 | | - * 1. rows changed, or a write call succeeded → `applied` |
23 | | - * 2. a write call answered `ACTION_CONFIRMATION_REQUIRED` → `pending_confirmation` |
| 22 | + * 1. rows changed, or a write call succeeded → `applied`; a |
| 23 | + * `run_action` the door handed to the person instead of running it is |
| 24 | + * no success ({@link handedOff}): its answer is a successful tool result, |
| 25 | + * `{ status: 'pending_confirmation', pendingId, … }`, and nothing ran |
| 26 | + * 2. a write call was handed to the person, or answered |
| 27 | + * `ACTION_CONFIRMATION_REQUIRED` → `pending_confirmation` |
24 | 28 | * 3. a write call answered a coded refusal → `refused <that code>`; |
25 | 29 | * a `run_action` whose flow refused (`{ ok: true, result: { status: |
26 | 30 | * 'refused' } }`, the flow's own declared refusal, which carries no |
|
83 | 87 | * And, for a `pending_confirmation` operation only, what the agent's applied |
84 | 88 | * write went through ({@link agentConfirmedOf}): the grader's temporary rule |
85 | 89 | * reads it until objectstack-ai/cloud#3826 item 6 lands. |
| 90 | + * |
| 91 | + * And whether a write call on the object met the confirmation gate of a server |
| 92 | + * with no pending-confirmation service ({@link metNoConfirmationService}): the |
| 93 | + * grader reads it on an `applied` operation, which that server leaves no agent |
| 94 | + * a way to apply through the gated action, so the operation is not measured. |
86 | 95 | */ |
87 | 96 |
|
88 | 97 | import { ErrorCode } from '@objectstack/spec/api'; |
89 | | -import { AI_ACTION_CONFIRMATION_MEMBER } from '@objectstack/spec/contracts'; |
| 98 | +import { AI_ACTION_CONFIRMATION_MEMBER, type ActionConfirmationPending, type ActionConfirmationRefusalReason } from '@objectstack/spec/contracts'; |
90 | 99 |
|
91 | 100 | import type { ScenarioCard, Scalar } from '../schema.js'; |
92 | 101 | import { |
@@ -121,6 +130,24 @@ export { flowRefused }; |
121 | 130 | */ |
122 | 131 | export const CONFIRMATION_REQUIRED_CODE: ErrorCode = ErrorCode.parse('ACTION_CONFIRMATION_REQUIRED'); |
123 | 132 |
|
| 133 | +/** |
| 134 | + * The confirmation gate's refusal reason on a server with no pending-confirmation service: the client could |
| 135 | + * not be asked, and the host registers no service to hand the call to. Checked against the contract's closed |
| 136 | + * list of reasons (`ActionConfirmationRefusalReason`), so a renamed reason fails the type check here. |
| 137 | + */ |
| 138 | +const NO_CONFIRMATION_SERVICE = 'no_confirmation_service' satisfies ActionConfirmationRefusalReason; |
| 139 | + |
| 140 | +/** |
| 141 | + * A write call answered by the confirmation gate of a server with no pending-confirmation service: |
| 142 | + * `428 ACTION_CONFIRMATION_REQUIRED` with `details.reason: 'no_confirmation_service'`. Such a server answers |
| 143 | + * every call through a gated action so, whatever the agent sends: nothing the agent does there applies it. |
| 144 | + */ |
| 145 | +export function metNoConfirmationService(output: unknown): boolean { |
| 146 | + if (refusalCodeOf(output) !== CONFIRMATION_REQUIRED_CODE) return false; |
| 147 | + const details = (jsonBodyOf(output)?.error as { details?: { reason?: unknown } } | undefined)?.details; |
| 148 | + return details?.reason === NO_CONFIRMATION_SERVICE; |
| 149 | +} |
| 150 | + |
124 | 151 | /** A row's fingerprint for the before/after comparison: every column but the clock. */ |
125 | 152 | export function fingerprint(row: Row): string { |
126 | 153 | const keys = Object.keys(row).filter((k) => k !== 'updated_at').sort(); |
@@ -290,6 +317,26 @@ export function withheldWrite(turn: ParsedTurn, object: string, writes: number): |
290 | 317 |
|
291 | 318 | const failed = (c: RunnerToolCall) => c.isError === true || refusalCodeOf(c.output) !== undefined || flowRefusalOf(c.output) !== undefined; |
292 | 319 |
|
| 320 | +/** |
| 321 | + * The discriminant of the hand-off answer (`ActionConfirmationPending`), checked against the contract's type, so a |
| 322 | + * renamed status fails the type check here. |
| 323 | + */ |
| 324 | +const HANDED_OFF = 'pending_confirmation' satisfies ActionConfirmationPending['status']; |
| 325 | + |
| 326 | +/** |
| 327 | + * A `run_action` the door handed to the person instead of running it: on a host that registers a |
| 328 | + * pending-confirmation service, a call to an action declaring `ai.requiresConfirmation: true` is answered as a |
| 329 | + * successful tool result, `{ status: 'pending_confirmation', pendingId, message, actionName, objectName }`. Nothing |
| 330 | + * ran and no row was written, so it is no applied write (rule 1) but the person's confirmation pending (rule 2). |
| 331 | + * Only the action door hands off: a data tool's answer is a record, whose own `status` field may hold any value. |
| 332 | + */ |
| 333 | +export function handedOff(c: RunnerToolCall): boolean { |
| 334 | + return c.name === 'run_action' && c.output !== undefined && !failed(c) && jsonBodyOf(c.output)?.status === HANDED_OFF; |
| 335 | +} |
| 336 | + |
| 337 | +/** A write call the door applied: answered, neither failed nor handed to the person ({@link handedOff}). */ |
| 338 | +const appliedCall = (c: RunnerToolCall) => c.output !== undefined && !failed(c) && !handedOff(c); |
| 339 | + |
293 | 340 | /** |
294 | 341 | * A tool output read as a JSON object: the object itself, or a text holding |
295 | 342 | * one (the same two readings as `refusalOf`: the whole text, else from its |
@@ -354,7 +401,8 @@ function refusalAnswerOf(c: RunnerToolCall): ObservedAnswer | undefined { |
354 | 401 | /** Decide how the write door answered (the ordered rules in the module header). */ |
355 | 402 | export function observedAnswer(turn: ParsedTurn, object: string, writes: number): ObservedAnswer { |
356 | 403 | const calls = writeCallsOn(turn, object); |
357 | | - if (writes > 0 || calls.some((c) => c.output !== undefined && !failed(c))) return { kind: 'applied' }; |
| 404 | + if (writes > 0 || calls.some(appliedCall)) return { kind: 'applied' }; |
| 405 | + if (calls.some(handedOff)) return { kind: 'pending_confirmation' }; |
358 | 406 | const refusals = calls.map(refusalAnswerOf).filter((r) => r !== undefined); |
359 | 407 | if (refusals.some((r) => r.kind === 'refused' && r.code === CONFIRMATION_REQUIRED_CODE)) return { kind: 'pending_confirmation' }; |
360 | 408 | if (refusals.length > 0) return refusals[refusals.length - 1]!; |
@@ -409,7 +457,7 @@ function agentConfirmedOf( |
409 | 457 | after: readonly Row[], |
410 | 458 | actionGates?: Readonly<Record<string, boolean>>, |
411 | 459 | ): AgentConfirmedObservation { |
412 | | - const applied = writeCallsOn(turn, op.object).filter((c) => c.output !== undefined && !failed(c)); |
| 460 | + const applied = writeCallsOn(turn, op.object).filter(appliedCall); |
413 | 461 | const actions: Array<AgentConfirmedObservation['actions'][number]> = []; |
414 | 462 | for (const c of applied.filter((x) => x.name === 'run_action')) { |
415 | 463 | const input = (typeof c.input === 'object' && c.input !== null ? c.input : {}) as Record<string, unknown>; |
@@ -582,6 +630,7 @@ export function observeOperation(input: ObserveInput): OperationObservation { |
582 | 630 | obs.agentSet = agentSet; |
583 | 631 | if (input.auditActor !== undefined) obs.auditActor = input.auditActor; |
584 | 632 | if (input.probe !== undefined) obs.probe = input.probe; |
| 633 | + if (writeCallsOn(input.turn, input.op.object).some((c) => metNoConfirmationService(c.output))) obs.metNoConfirmationService = true; |
585 | 634 | if (input.op.outcome.kind === 'pending_confirmation') { |
586 | 635 | obs.agentConfirmed = agentConfirmedOf(input.op, input.turn, input.before, input.after, input.actionGates); |
587 | 636 | } |
|
0 commit comments