@@ -109,6 +109,7 @@ import {
109109 reconcileActionRegistrations as reconcileActionRegistrationsPure ,
110110 resolveActionByPackage ,
111111 resolveActionHandlerKeys ,
112+ resolveRecordTitle ,
112113 standaloneActionOwnerKey ,
113114 SummaryRecomputeError ,
114115 type PackageScopedRefusal ,
@@ -3258,6 +3259,127 @@ export async function handOffActionConfirmation(
32583259 } ;
32593260}
32603261
3262+ /**
3263+ * [#33712] What an MCP door asks the person to confirm (step 1 of the amended
3264+ * contract, `AI_ACTION_CONFIRMATION_CAPABILITY`): the action, the object and
3265+ * the record it runs on, its id and, when the subject load read one, its
3266+ * title. Structurally the `McpActionConfirmationSubject` of `@objectstack/mcp`,
3267+ * which this package does not depend on.
3268+ */
3269+ export interface ActionConfirmationElicitSubject {
3270+ actionName : string ;
3271+ actionLabel ?: string ;
3272+ objectName ?: string ;
3273+ recordId ?: string ;
3274+ recordTitle ?: string ;
3275+ }
3276+
3277+ /** [#33712] The person's answer, as the door read it (`McpActionConfirmationAnswer`). */
3278+ export type ActionConfirmationElicitAnswer = 'accept' | 'decline' | 'cancel' | 'timed_out' ;
3279+
3280+ /**
3281+ * [#33712] The door's channel to the person's client for ONE call
3282+ * (`McpActionConfirmationElicitor`). Present only when the client can be
3283+ * elicited; its absence means "hand off" (step 2), never a refusal.
3284+ */
3285+ export type ActionConfirmationElicitor = ( subject : ActionConfirmationElicitSubject ) => Promise < ActionConfirmationElicitAnswer > ;
3286+
3287+ /**
3288+ * [#33712] The subject an elicitation names, built from what the admitted
3289+ * sequence already holds: the declaration, the object and the record the
3290+ * caller-scope subject load read. The title is the object's own
3291+ * (`resolveRecordTitle`: its `nameField`, formula or column), and is left out
3292+ * rather than replaced by the id when it does not resolve.
3293+ */
3294+ export function actionConfirmationElicitSubject (
3295+ action : any ,
3296+ actionName : string ,
3297+ objectName : string | undefined ,
3298+ recordId : string | undefined ,
3299+ obj : unknown ,
3300+ record : Record < string , unknown > | undefined ,
3301+ ) : ActionConfirmationElicitSubject {
3302+ const label = typeof action ?. label === 'string' && action . label !== '' ? action . label : undefined ;
3303+ let recordTitle : string | undefined ;
3304+ if ( recordId !== undefined && record && Object . keys ( record ) . length > 0 ) {
3305+ try {
3306+ recordTitle = resolveRecordTitle ( obj , record ) ;
3307+ } catch {
3308+ // A title that cannot be composed is left out: the id still names the record.
3309+ recordTitle = undefined ;
3310+ }
3311+ }
3312+ return {
3313+ actionName,
3314+ ...( label !== undefined ? { actionLabel : label } : { } ) ,
3315+ ...( objectName && ! isObjectLessActionKey ( objectName ) ? { objectName } : { } ) ,
3316+ ...( recordId !== undefined ? { recordId } : { } ) ,
3317+ ...( recordTitle !== undefined && recordTitle !== '' ? { recordTitle } : { } ) ,
3318+ } ;
3319+ }
3320+
3321+ /**
3322+ * [#33712] Step 1 of the amended contract: ask the person in their client, and
3323+ * read the answer as step 4 does. Asked by a door that holds an elicitor, once
3324+ * every admission gate has passed (the subject load and its caller-scope
3325+ * verdict included) and never for an AI service account, which the
3326+ * confirmation step refuses before it gets here.
3327+ *
3328+ * - `accept`: `{ accepted: true }`; the caller dispatches the call exactly
3329+ * once, as any admitted call. Nothing is stored: the next call is asked again.
3330+ * - `decline`: refused `428`, `reason: 'declined'`.
3331+ * - `cancel` (a dismissal, an error answer, a dropped connection): refused,
3332+ * `reason: 'cancelled'`.
3333+ * - `timed_out` (no answer within the door's bound): refused,
3334+ * `reason: 'timed_out'`.
3335+ *
3336+ * An elicitor that throws, or answers anything else, reads as `cancel`:
3337+ * nothing runs for want of a readable answer. Nothing is handed off on any of
3338+ * these answers: the person was asked, and their answer (or their silence)
3339+ * stands.
3340+ */
3341+ export async function elicitActionConfirmation (
3342+ elicit : ActionConfirmationElicitor ,
3343+ subject : ActionConfirmationElicitSubject ,
3344+ ) : Promise < { accepted : true } | { refused : ActionConfirmationRefusal } > {
3345+ let answer : unknown ;
3346+ try {
3347+ answer = await elicit ( subject ) ;
3348+ } catch {
3349+ answer = 'cancel' ;
3350+ }
3351+ if ( answer === 'accept' ) return { accepted : true } ;
3352+ const on = confirmationSubjectOn ( subject . objectName ) ;
3353+ const declared = `Action '${ subject . actionName } '${ on } declares ai.requiresConfirmation: true, so the person was asked to confirm it in their client` ;
3354+ if ( answer === 'decline' ) {
3355+ return {
3356+ refused : confirmationRefusalOf (
3357+ subject . actionName , subject . objectName ,
3358+ `${ declared } , and they declined — nothing was run. Do not retry unless they ask again.` ,
3359+ 'declined' ,
3360+ ) ,
3361+ } ;
3362+ }
3363+ if ( answer === 'timed_out' ) {
3364+ return {
3365+ refused : confirmationRefusalOf (
3366+ subject . actionName , subject . objectName ,
3367+ `${ declared } , and no answer arrived in time — nothing was run, and a later answer runs nothing. `
3368+ + 'Retry only when they are there to answer.' ,
3369+ 'timed_out' ,
3370+ ) ,
3371+ } ;
3372+ }
3373+ return {
3374+ refused : confirmationRefusalOf (
3375+ subject . actionName , subject . objectName ,
3376+ `${ declared } , and the question was dismissed without an answer — nothing was run. `
3377+ + 'Retry only if they ask again.' ,
3378+ 'cancelled' ,
3379+ ) ,
3380+ } ;
3381+ }
3382+
32613383/**
32623384 * [#33690] The caller-scope subject verdict, as the dispatch arm that would
32633385 * run `action` reads it, asked BEFORE a hand-off (the amended contract, step
@@ -4814,9 +4936,16 @@ export async function invokeBusinessAction(deps: ActionExecutionDeps,
48144936 // metadata service's `loadMany('action')` is read, as before.
48154937 readActions ?: ( ) => Promise < readonly any [ ] | undefined > ;
48164938 callData : ( action : string , params : any , dataDriver ?: any , scopeId ?: string , ec ?: ExecutionContext ) => Promise < any > ;
4939+ /**
4940+ * [#33712] The MCP door's channel to the person's client for this call
4941+ * (step 1 of the amended contract), present only when the client can
4942+ * be elicited. A declared-gated call is then asked in place instead of
4943+ * handed off; see {@link elicitActionConfirmation}.
4944+ */
4945+ elicitConfirmation ?: ActionConfirmationElicitor ;
48174946 } ,
48184947) : Promise < any > {
4819- const { driver, envId, ec, getMeta, readObjects, readActions, callData } = wiring ;
4948+ const { driver, envId, ec, getMeta, readObjects, readActions, callData, elicitConfirmation } = wiring ;
48204949 const meta : any = await getMeta ( ) ;
48214950 const params = input ?. params && typeof input . params === 'object' ? input . params : { } ;
48224951 const recordId = typeof input ?. recordId === 'string' && input . recordId . length > 0 ? input . recordId : undefined ;
@@ -4901,7 +5030,13 @@ export async function invokeBusinessAction(deps: ActionExecutionDeps,
49015030 // [#33690] This door hands a declared-gated call to the person it runs
49025031 // as: the caller's `confirm` is not passed on, because it never counts
49035032 // there (the amended contract, `AI_ACTION_CONFIRMATION_CAPABILITY`).
4904- confirmationHandOff : { door : 'mcp' , ...( namespace !== undefined ? { namespace } : { } ) } ,
5033+ // [#33712] With the door's elicitor, the person is asked in their
5034+ // client first (step 1); the hand-off is then never taken.
5035+ confirmationHandOff : {
5036+ door : 'mcp' ,
5037+ ...( namespace !== undefined ? { namespace } : { } ) ,
5038+ ...( elicitConfirmation ? { elicitConfirmation } : { } ) ,
5039+ } ,
49055040 ql : engine ,
49065041 wiring : {
49075042 ec, envId, driver, callData,
0 commit comments