Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .changeset/34579-resume-failed-carries-refusal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@objectstack/types': patch
'@objectstack/plugin-approvals': patch
'@objectstack/rest': patch
---

fix(approvals): a decision stranded by a refused next step says which refusal stranded it

Clause-②: yes (widening)
Metadata-protocol: none

Say an approval flow's next step after the decision is an `update_record`, and a record hook refuses that write (a declared refusal, a validation rule, a permission). `POST /api/v1/approvals/requests/{id}/approve` answers `500 RESUME_FAILED`. The status code and the four #13807 facts are unchanged: the decision stands (`finalized: true`), and `runId` and `repairable` name the stranded run. The body did not say why the run stopped. The engine reported the refusal (`AutomationResult.innerRefusal` and `.userMessage`), and the approvals service dropped it before it built the error.

The `RESUME_FAILED` body now also carries that refusal when there was one:

- `innerRefusal`: `{ code, message }`, the same member the flow doors serve in their `FLOW_FAILED` details;
- `userMessage`: the refusal's marked end-user text at the top level, for example a hook's declared sentence in the caller's locale.

Both are absent when the run met a fault rather than a refusal. `StrandedDecisionDetails` (`@objectstack/types`) declares them as optional members, and `strandedDecisionDetails` drops a malformed one without dropping the four facts.

A flow author who wants the refusal handled instead of stranded can draw a `fault` edge from that step. The engine routes the refusal down it, and the decision then answers normally. This is now pinned and documented in the approvals guide.
14 changes: 14 additions & 0 deletions content/docs/automation/approvals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,20 @@ for you.
would open a second approval request.
</Callout>

<Callout type="warn">
**A refusal on the step after the decision.** Say the `approve` branch
updates the record and that record's `beforeUpdate` hook refuses the write
(a declared refusal, a validation rule, a permission). With no `fault` edge
on that step, `POST …/approve` answers **500 `RESUME_FAILED`**. The decision
still stands (`finalized: true`), the run is stranded (`runId`, and
`repairable` says whether an operator can re-arm it), and the body also
carries the refusal: `innerRefusal` (`{ code, message }`) and the hook's
`userMessage`, the sentence to show the approver. Draw a `fault` edge from
that step to a handler, for example one that sends the record back to its
owner, and the engine routes the refusal there instead: the decision answers
normally and the run finishes without an administrator.
</Callout>

</Steps>

### Beyond approve/reject — the full decision surface
Expand Down
43 changes: 39 additions & 4 deletions packages/plugins/plugin-approvals/src/approval-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
type ObjectSchemaReadDelegate,
type RecordOrganizationResolver,
} from '@objectstack/metadata-core';
import { isObjectAbsentError, keysetWalk, strandedDecisionFailure } from '@objectstack/types';
import { isObjectAbsentError, keysetWalk, strandedDecisionFailure, type StrandedDecisionDetails } from '@objectstack/types';
// [#15981 / #16166] Every built-in identity NAME is deliberately absent here:
// both arms of `isOverrideActor` read an ADR-0095 capability rung, never a name.
// `BUILTIN_IDENTITY_PLATFORM_ADMIN` went with the platform arm's name read and
Expand Down Expand Up @@ -2064,6 +2064,12 @@ export function slotLabelSourceOf(service: ApprovalService): SlotLabelSource {
return source;
}

/**
* [#34579] The refusal half of a failed resume, as {@link ApprovalService}
* carries it from the engine's answer to the stranded-decision envelope.
*/
type ResumeRefusal = Pick<StrandedDecisionDetails, 'innerRefusal' | 'userMessage'>;

/**
* [#22559] The visibility source the read gate consumes for `service`: the
* approvals door's own `visibleRequestIds`, asked for a read that arrives
Expand Down Expand Up @@ -5925,16 +5931,29 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
): Promise<{ runId: string; repairable: boolean; error: string } | undefined> {
const result = await this.automation!.resume!(runId, { ...signal, [RESUME_AUTHORITY_SERVICE]: true });
const reported = result as
{ success?: boolean; code?: string; error?: string; status?: string } | undefined;
| ({ success?: boolean; code?: string; error?: string; status?: string } & ResumeRefusal)
| undefined;
// Only an explicit `success: false` is a failure. An engine (or a test
// double) that returns nothing is reporting nothing, and has always meant
// "it ran".
if (reported && typeof reported === 'object' && reported.success === false) {
const err = new Error(
`resume of run '${runId}' failed${reported.code ? ` [${reported.code}]` : ''}: ${reported.error ?? 'unknown error'}`,
) as Error & { resumeCode?: string; resumeStatus?: string };
) as Error & { resumeCode?: string; resumeStatus?: string; resumeRefusal?: ResumeRefusal };
err.resumeCode = reported.code;
err.resumeStatus = reported.status;
// [#34579] The refusal the continuation met, as the engine reported it
// (`AutomationResult.innerRefusal` / `.userMessage`, ruling B′ on
// #24914) — kept beside the code and status instead of being dropped
// here, one line before the envelope is built, which is where #13807
// found `status` too. Copied as reported; the recogniser in
// `@objectstack/types` validates it before anything reaches the wire.
if (reported.innerRefusal !== undefined || reported.userMessage !== undefined) {
err.resumeRefusal = {
...(reported.innerRefusal !== undefined ? { innerRefusal: reported.innerRefusal } : {}),
...(reported.userMessage !== undefined ? { userMessage: reported.userMessage } : {}),
};
}
throw err;
}
return this.automation?.takeSubflowParentStrand?.(runId);
Expand Down Expand Up @@ -5962,6 +5981,18 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
return (err as { resumeStatus?: string } | undefined)?.resumeStatus;
}

/**
* The refusal behind a {@link serviceResume} rejection — the engine's
* `innerRefusal` / `userMessage` — or `{}` when the engine reported none
* (#34579). A third sibling of {@link resumeCodeOf} and
* {@link resumeStatusOf}: `code` says why a resume was refused, `status`
* how the run ended, and this what the continuation's own data call was
* refused with. Only the stranded-decision envelope reads it.
*/
private static resumeRefusalOf(err: unknown): ResumeRefusal {
return (err as { resumeRefusal?: ResumeRefusal } | undefined)?.resumeRefusal ?? {};
}

/**
* A decision or a comment attaches only files its caller uploaded.
*
Expand Down Expand Up @@ -6257,10 +6288,14 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
...(signal.resumedBy && signal.impersonatedBy ? { impersonatedBy: signal.impersonatedBy } : {}),
});
}
// [#34579] The refusal the continuation met rides the same envelope —
// the status code still does not move (#13807 B), but an approver whose
// decision stranded on a record hook's refusal is told WHICH refusal.
// The recogniser drops anything not in the declared shape.
throw strandedDecisionFailure(
`RESUME_FAILED: ${what} was recorded on request ${requestId}, but its flow run '${runId}' ` +
`could not be resumed and is now stranded: ${reason}`,
{ finalized: true, decision, runId, repairable },
{ finalized: true, decision, runId, repairable, ...ApprovalService.resumeRefusalOf(err) },
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* #34579 — a record hook refuses the step AFTER an approval decision.
*
* ## The reported case
*
* A special-approval flow parks at an `approval` node; on `approve` an
* `update_record` moves the plan to `approved`. The plan's `beforeUpdate` hook
* re-checks capacity and refuses with a declared refusal. The approver's
* `POST …/approve` then answered `500 RESUME_FAILED`, the decision stood, the
* run stranded, and the approver could not read WHY: the engine reported the
* refusal (`AutomationResult.innerRefusal` / `.userMessage`, ruling B′ on
* #24914), and the approvals door dropped it before building the envelope.
*
* ## What is pinned, within the rulings on file
*
* ⛔ The status code does not move and the decision is not rolled back or made
* atomic: #13807 B keeps the 500 and the #13937 shape-4 ruling excludes undoing
* a human decision. So the card is pinned on its fallback route:
*
* 1. NO fault edge: the `RESUME_FAILED` envelope carries the refusal the step
* met — `innerRefusal` in the engine's own shape and the hook's marked
* `userMessage` — beside the four #13807 facts, which are unchanged.
* 2. A declared FAULT edge on that step: the hook refusal is routed down it,
* like any run-time failure (`dag-traversal.ts`; only guard refusals are
* not routed). The decision answers normally, `resumed: true`, and the run
* completes through the author's handler. A flow author can bounce the
* record back without an administrator.
* 3. Reverse control: a step that FAULTS (a throw that declares no HTTP
* answer) met no refusal, so the envelope carries neither member — nothing
* is synthesised from the prose.
*/

import { describe, it, expect, beforeEach } from 'vitest';
import { AutomationEngine, InMemorySuspendedRunStore, registerCrudNodes } from '@objectstack/service-automation';
import { assertEngineDeleteDispatch, assertEngineUpdateDispatch } from '@objectstack/objectql';
import { strandedDecisionDetails } from '@objectstack/types';
import { ApprovalService } from './approval-service.js';
import { registerApprovalNode } from './approval-node.js';

const SYSTEM_CTX = { isSystem: true, positions: [], permissions: [] } as any;
const noopLogger: any = { info() {}, warn() {}, error() {}, debug() {} };
noopLogger.child = () => noopLogger;

/** The hook's declared sentence, in the caller's locale — what the approver should read. */
const HOOK_USER_TEXT = '产能已被其他订单占用,该方案无法按当前产能通过。';

/**
* A declared hook refusal as the runtime answers one
* (`resolveHookRefusal`, `runtime/src/sandbox/hook-refusal-catalogue.ts`):
* the diagnostic message names the hook, the declared code and status ride
* the error, and the declared sentence is both the caller-facing message and
* the marked `userMessage`.
*/
const hookRefusal = () => Object.assign(
new Error("hook 'alloc_plan_capacity_recheck' threw: Error: over_capacity"),
{ name: 'SandboxError', innerMessage: HOOK_USER_TEXT, code: 'VALIDATION_FAILED', status: 400, userMessage: HOOK_USER_TEXT },
);

/** In-memory ObjectQL stand-in: approvals tables plus the plan the flow writes. */
function makeFakeEngine(refuseUpdateOf: string, refusal: () => unknown) {
const tables = new Map<string, any[]>();
const rows = (o: string) => (tables.get(o) ?? (tables.set(o, []), tables.get(o)!));
const matches = (row: any, where: any) => Object.entries(where ?? {}).every(([k, v]) => {
if (k.startsWith('$')) throw new Error(`fake engine: unsupported filter operator ${k}`);
if (v && typeof v === 'object' && '$in' in (v as any)) return (v as any).$in.includes(row[k]);
if (v && typeof v === 'object' && '$ne' in (v as any)) return row[k] !== (v as any).$ne;
return row[k] === v;
});
return {
tables,
async find(object: string, opts: any = {}) {
const where = opts.where ?? opts.filter ?? {};
const out = rows(object).filter(r => matches(r, where));
const start = opts.offset ?? 0;
const page = typeof opts.limit === 'number' ? out.slice(start, start + opts.limit) : out.slice(start);
return page.map(r => ({ ...r }));
},
async insert(object: string, data: any) { rows(object).push({ ...data }); return { ...data }; },
async update(object: string, data: any, options?: any) {
const dispatch = assertEngineUpdateDispatch(data, options);
// The record hook: accepts the call the way ObjectQL does, then refuses.
if (object === refuseUpdateOf) throw refusal();
const table = rows(object);
if (dispatch.kind === 'multi') {
let n = 0;
for (let i = 0; i < table.length; i++) {
if (matches(table[i], options?.where)) { table[i] = { ...table[i], ...data }; n++; }
}
return { updated: n };
}
const i = table.findIndex(r => r.id === dispatch.id);
if (i >= 0) table[i] = { ...table[i], ...data };
return i >= 0 ? { ...table[i] } : null;
},
async delete(object: string, options?: any) {
const dispatch = assertEngineDeleteDispatch(options);
const table = rows(object);
const i = table.findIndex(r => r.id === (dispatch as any).id);
if (i >= 0) table.splice(i, 1);
return { id: (dispatch as any).id };
},
};
}

/** start → approval → (approve) adopt the plan → end; (reject) → end. */
function specialApprovalFlow(withFaultEdge: boolean) {
return {
name: 'plan_special_approval',
label: 'Plan Special Approval',
type: 'autolaunched',
nodes: [
{ id: 'start', type: 'start', label: 'Start' },
{
id: 'director', type: 'approval', label: 'Director Approval',
config: { approvers: [{ type: 'user', value: 'director' }] },
},
{
id: 'adopt_plan', type: 'update_record', label: 'Adopt Plan',
config: { objectName: 'alloc_plan', filter: { id: 'p1' }, fields: { status: 'approved' } },
},
{ id: 'bounce_back', type: 'mark', label: 'Bounce back to the rep' },
{ id: 'end', type: 'end', label: 'End' },
],
edges: [
{ id: 'e1', source: 'start', target: 'director' },
{ id: 'e2', source: 'director', target: 'adopt_plan', label: 'approve' },
{ id: 'e3', source: 'director', target: 'end', label: 'reject' },
{ id: 'e4', source: 'adopt_plan', target: 'end' },
...(withFaultEdge
? [
{ id: 'ef', source: 'adopt_plan', target: 'bounce_back', type: 'fault' },
{ id: 'e5', source: 'bounce_back', target: 'end' },
]
: []),
],
};
}

describe('#34579 — a hook refusal on the step after an approval decision', () => {
let marks: string[];

beforeEach(() => { marks = []; });

/** One live process: real engine, real CRUD + approval nodes, real approvals service. */
function boot(opts: { withFaultEdge: boolean; refusal?: () => unknown }) {
const data = makeFakeEngine('alloc_plan', opts.refusal ?? hookRefusal);
data.tables.set('alloc_plan', [{ id: 'p1', status: 'submitted' }]);
const service = new ApprovalService({ engine: data as any, logger: noopLogger });
const automation = new AutomationEngine(noopLogger, new InMemorySuspendedRunStore());
registerCrudNodes(automation, { logger: noopLogger, getService: (n: string) => (n === 'data' ? data : undefined) } as any);
registerApprovalNode(automation, service, noopLogger);
automation.registerNodeExecutor({
type: 'mark',
async execute(node: any) { marks.push(node.id); return { success: true }; },
} as never);
automation.registerFlow('plan_special_approval', specialApprovalFlow(opts.withFaultEdge) as never);
service.attachAutomation(automation);
return { data, service, automation };
}

async function park(env: ReturnType<typeof boot>) {
const started = await env.automation.execute('plan_special_approval', {
object: 'alloc_plan', record: { id: 'p1', status: 'submitted' }, userId: 'rep',
} as never);
expect(started.status, 'the run must park at the approval node').toBe('paused');
const req = (await env.data.find('sys_approval_request', { where: { status: 'pending' } }))[0];
expect(req?.flow_run_id, 'the request must name the run it gates').toBeTruthy();
return req;
}

it('NO fault edge: still 500 RESUME_FAILED, and the envelope now carries the refusal the step met', async () => {
const env = boot({ withFaultEdge: false });
const req = await park(env);
const runId = req.flow_run_id;

const err = await env.service
.decide(req.id, { decision: 'approve', actorId: 'director' }, SYSTEM_CTX)
.then(() => null, (e: Error) => e);

// #13807 B, unchanged: the door still throws, and the prose still names the run.
expect(err, '⛔ not "return 200" — the status code does not move').toBeTruthy();
expect(err?.message).toMatch(/^RESUME_FAILED: the approve decision was recorded on request /);
expect(err?.message).toMatch(/could not be resumed and is now stranded/);

// The decision is durable and the plan did not move: the reported state.
const row = (await env.data.find('sys_approval_request', { where: { id: req.id } }))[0];
expect(row.status).toBe('approved');
expect((await env.data.find('alloc_plan', { where: { id: 'p1' } }))[0].status).toBe('submitted');

// ⭐ The card's deliverable: the refusal, structured, beside the four facts.
expect(strandedDecisionDetails(err)).toEqual({
finalized: true,
decision: 'approve',
runId,
repairable: true,
innerRefusal: { code: 'VALIDATION_FAILED', message: hookRefusal().message },
userMessage: HOOK_USER_TEXT,
});
});

it('a declared FAULT edge on that step routes the hook refusal to the handler — the decision answers normally and the run does not strand', async () => {
const env = boot({ withFaultEdge: true });
const req = await park(env);
const runId = req.flow_run_id;

const out = await env.service.decide(req.id, { decision: 'approve', actorId: 'director' }, SYSTEM_CTX);

expect(out.finalized).toBe(true);
expect(out.decision).toBe('approve');
expect(out.resumed, 'the run advanced through the fault edge').toBe(true);
expect(out.resumeError).toBeUndefined();
expect(marks, "the author's handler ran").toEqual(['bounce_back']);
// Nothing left parked and nothing to repair: the run ended.
expect(await env.automation.hasSuspendedRun(runId)).toBe(false);
expect(strandedDecisionDetails(out as unknown)).toBeUndefined();
});

it('reverse control: a step that FAULTS met no refusal — the envelope carries neither member', async () => {
const env = boot({ withFaultEdge: false, refusal: () => new Error('ECONNREFUSED: could not reach the store') });
const req = await park(env);

const err = await env.service
.decide(req.id, { decision: 'approve', actorId: 'director' }, SYSTEM_CTX)
.then(() => null, (e: Error) => e);

const details = strandedDecisionDetails(err);
expect(details).toEqual({ finalized: true, decision: 'approve', runId: req.flow_run_id, repairable: true });
expect(details && 'innerRefusal' in details).toBe(false);
expect(details && 'userMessage' in details).toBe(false);
});
});
8 changes: 8 additions & 0 deletions packages/rest/src/approvals-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ export function registerApprovalsEndpoints(this: ApprovalsRouteHost, basePath: s
// same reason the strip above is: these four facts are
// about a recorded-decision-with-stranded-run and
// about nothing else.
//
// [#34579] …and, when the run stranded on a refusal,
// that refusal: `innerRefusal` (ruling B′ on #24914,
// the engine's own shape) and the marked `userMessage`
// at the top level, the channel a record save's refusal
// and every flow door's `FLOW_FAILED` already use. Both
// are present only when the producer attached them and
// the recogniser accepted them.
...(code === 'RESUME_FAILED' && stranded ? stranded : {}),
});
return true;
Expand Down
Loading
Loading