@@ -88,8 +88,11 @@ import {
8888 reconcileActionRegistrations as reconcileActionRegistrationsPure ,
8989 resolveActionHandlerKeys ,
9090 standaloneActionOwnerKey ,
91+ SummaryRecomputeError ,
9192} from '@objectstack/objectql' ;
9293import { earnedAnswerOf , staleSummaryRefusalOrThrow , withEarnedAnswer , type SummaryRecomputeRefusal } from '@objectstack/types' ;
94+ // [#28861] A landed write answers its success to the action handler's code.
95+ import { answerLandedWritesDespiteStaleSummary , landedWriteAnswer } from './landed-write-answer.js' ;
9396
9497// [ADR-0110] The addressing vocabulary and the D5 reconciliation moved to
9598// @objectstack /objectql (the engine owns the map they describe, and its
@@ -3130,23 +3133,35 @@ export function buildActionExecutionContext(ec: any): Record<string, unknown> {
31303133 * hash keyed) and never the stored row. [#21594] An action BODY's API is built
31313134 * over this one by the sandbox (`buildSandboxApi`), whose body layers refuse a
31323135 * family read or write before it reaches this seam.
3136+ *
3137+ * [#28861] A write through it that the engine stored, whose parent roll-up
3138+ * could not be recomputed after it, answers the write's success to the
3139+ * handler (`landed-write-answer.ts`), on this context and every one it
3140+ * derives. That layer sits beneath the reader seam, so the answer is served
3141+ * like any write's return.
31333142 */
31343143export function buildActionApi ( _deps : ActionExecutionDeps , ql : any , ec : any ) : any | undefined {
31353144 if ( ! ql || typeof ql . createContext !== 'function' ) return undefined ;
3145+ const served = ( scoped : unknown ) =>
3146+ serveStoredMetadataReadsThrough ( answerLandedWritesDespiteStaleSummary ( scoped , ACTION_API_SURFACE ) , ql ) ;
31363147 try {
3137- return serveStoredMetadataReadsThrough ( ql . createContext ( buildActionExecutionContext ( ec ) ) , ql ) ;
3148+ return served ( ql . createContext ( buildActionExecutionContext ( ec ) ) ) ;
31383149 } catch {
31393150 // A malformed caller envelope must not sink the action — fall back to
31403151 // the bare elevated context (the same shape hooks default to), marked
31413152 // as the body's elevation like every other (#25653).
31423153 try {
3143- return serveStoredMetadataReadsThrough ( ql . createContext ( buildActionExecutionContext ( undefined ) ) , ql ) ;
3154+ return served ( ql . createContext ( buildActionExecutionContext ( undefined ) ) ) ;
31443155 } catch {
31453156 return undefined ;
31463157 }
31473158 }
31483159}
31493160
3161+ /** [#28861] The surfaces the stale-column line names, for an action handler's two data handles. */
3162+ const ACTION_API_SURFACE = 'action ctx.api' ;
3163+ const ACTION_ENGINE_SURFACE = 'action ctx.engine' ;
3164+
31503165/**
31513166 * The sentence a caller still on the WITHDRAWN bare-filter shape must read
31523167 * (#15124). Exported because the wording IS the contract here: this refusal is
@@ -3234,17 +3249,31 @@ function assertActionEngineFindEnvelope(object: string, query: Record<string, un
32343249 * use. Every call carries {@link buildActionExecutionContext} so `ctx.engine`
32353250 * and `ctx.api` write under the SAME identity (#3914); passing `ec` is what
32363251 * separates a trusted write from a context-less one.
3252+ *
3253+ * [#28861] A write the engine stored, whose parent roll-up could not be
3254+ * recomputed after it, answers the write's success here, as on `ctx.api`
3255+ * ({@link landedWriteAnswer}): the handler's next line runs. Every other
3256+ * throw, a refusal before the write among them, reaches the handler as is.
32373257 */
32383258export function buildActionEngineFacade ( _deps : ActionExecutionDeps , ql : any , ec ?: any ) : any {
32393259 const context = buildActionExecutionContext ( ec ) ;
32403260 return {
32413261 async insert ( object : string , data : Record < string , unknown > ) : Promise < { id : string } > {
3242- const res = await ql . insert ( object , data , { context } ) ;
3262+ let res : unknown ;
3263+ try {
3264+ res = await ql . insert ( object , data , { context } ) ;
3265+ } catch ( e ) {
3266+ res = landedWriteAnswer ( e , object , ACTION_ENGINE_SURFACE ) ;
3267+ }
32433268 const id = ( res && ( res as any ) . id ) ?? ( data as any ) . id ;
32443269 return { id } ;
32453270 } ,
32463271 async update ( object : string , id : string , data : Record < string , unknown > ) : Promise < void > {
3247- await ql . update ( object , data , { where : { id } , context } ) ;
3272+ try {
3273+ await ql . update ( object , data , { where : { id } , context } ) ;
3274+ } catch ( e ) {
3275+ landedWriteAnswer ( e , object , ACTION_ENGINE_SURFACE ) ;
3276+ }
32483277 } ,
32493278 // Both spellings are DECLARED contract (#15117), not a tolerance: the
32503279 // spec's `ActionEngineFacade.delete` takes `string | string[]`.
@@ -3263,10 +3292,17 @@ export function buildActionEngineFacade(_deps: ActionExecutionDeps, ql: any, ec?
32633292 // it pulls the runtime back onto the contract that is already on the
32643293 // record, here, in the spec member doc, and in the `never a null id`
32653294 // pin at `packages/spec/src/ui/action-params.test.ts`.
3295+ //
3296+ // [#28861] Each id's landed delete is answered on its own, so a stale
3297+ // roll-up after one id's delete does not leave the later ids undeleted.
32663298 async delete ( object : string , idOrIds : string | string [ ] ) : Promise < void > {
32673299 const ids = Array . isArray ( idOrIds ) ? idOrIds : [ idOrIds ] ;
32683300 for ( const id of ids ) {
3269- await ql . delete ( object , { where : { id } , context } ) ;
3301+ try {
3302+ await ql . delete ( object , { where : { id } , context } ) ;
3303+ } catch ( e ) {
3304+ landedWriteAnswer ( e , object , ACTION_ENGINE_SURFACE ) ;
3305+ }
32703306 }
32713307 } ,
32723308 // [#15124] The ENVELOPE goes through — this arm no longer builds one.
@@ -4037,15 +4073,39 @@ export async function invokeBusinessAction(deps: ActionExecutionDeps,
40374073 } catch ( err ) {
40384074 // [#28856] A refusal carrying the action's answer is answered as the
40394075 // success it is by the `run_action` tool, so the row records that.
4040- await recordActionInvocation ( deps , requestContext , envId , earnedAnswerOf ( err )
4076+ //
4077+ // [#28861] Read on the declarative-update arm only: that arm composes
4078+ // the ACTION's answer and attaches it (`dispatchAdmittedBusinessAction`).
4079+ // Any other arm's refusal can carry only a WRITE's data answer — a
4080+ // handler that let out a refusal a data door attached its answer to —
4081+ // and the action that threw has no result. It is answered as the
4082+ // failure it is, and leaves this door with nothing attached, so the
4083+ // `run_action` tool, which reads every throw, cannot relay it either.
4084+ const declarative = isDeclarativeUpdateAction ( action ) ;
4085+ await recordActionInvocation ( deps , requestContext , envId , declarative && earnedAnswerOf ( err )
40414086 ? { ...invocation , outcome : 'succeeded' }
40424087 : { ...invocation , outcome : 'failed' , ...actionInvocationFailure ( err ) } ) ;
4043- throw err ;
4088+ throw declarative ? err : withoutWriteAnswer ( err ) ;
40444089 }
40454090 await recordActionInvocation ( deps , requestContext , envId , { ...invocation , outcome : 'succeeded' } ) ;
40464091 return answer ;
40474092}
40484093
4094+ /**
4095+ * [#28861] `err` with no write answer attached: a stale-roll-up refusal a data
4096+ * door attached its answer to comes back as the same refusal — objectql's
4097+ * class, `failures`, `written`, message and stack — with nothing attached;
4098+ * every other value is returned as is. For a non-declarative arm's throw,
4099+ * whose attached answer is a write's, never the action's.
4100+ */
4101+ function withoutWriteAnswer ( err : unknown ) : unknown {
4102+ if ( earnedAnswerOf ( err ) === undefined ) return err ;
4103+ const stale = err as SummaryRecomputeRefusal ;
4104+ const bare = new SummaryRecomputeError ( [ ...( stale . failures ?? [ ] ) ] as SummaryRecomputeError [ 'failures' ] , stale . written ) ;
4105+ if ( typeof stale . stack === 'string' ) bare . stack = stale . stack ;
4106+ return bare ;
4107+ }
4108+
40494109/**
40504110 * The MCP `run_action` dispatch, once every admission gate in
40514111 * {@link invokeBusinessAction} has passed: the declarative update, the
0 commit comments