From 54701f4e2f97256d7d5a07ebfdf01baa949d299f Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 23:46:01 +0000 Subject: [PATCH 1/4] test(approvals): pin that a working-time calendar read that errors costs only its row the deadline Co-authored-by: Claude Claude-Session: https://claude.ai/code/session_016WV4czKnDE6hsyXQzB9wB2 --- ...pproval-service-sla-calendar-clock.test.ts | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/packages/plugins/plugin-approvals/src/approval-service-sla-calendar-clock.test.ts b/packages/plugins/plugin-approvals/src/approval-service-sla-calendar-clock.test.ts index 585a0ae0694..f0f1fb1fdb4 100644 --- a/packages/plugins/plugin-approvals/src/approval-service-sla-calendar-clock.test.ts +++ b/packages/plugins/plugin-approvals/src/approval-service-sla-calendar-clock.test.ts @@ -28,7 +28,9 @@ import { describe, it, expect } from 'vitest'; import { WorkCalendarService } from '@objectstack/objectql'; +import { AutomationEngine } from '@objectstack/service-automation'; import { ApprovalService } from './approval-service.js'; +import { registerApprovalNode } from './approval-node.js'; interface Row { [k: string]: any } @@ -390,3 +392,115 @@ describe("ApprovalEscalation.clock 'working_time' counts the hours on a working expect(row?.sla_due_refusal).toBeUndefined(); }); }); + +// ── a calendar read that ERRORS, as opposed to one that refuses ───────────── +// +// A refusal (`isWorkCalendarRefusal`) is served as `sla_due_refusal` (D9). Any +// other error — a driver failure reading `sys_work_calendar` — leaves that one +// row with no deadline served: it never fails a request open that has already +// written the row and told the approvers, never fails the rest of a list, and +// never falls back to wall-clock hours (D2). The sweep skips the row and asks +// again next pass. + +const CN_BACKUP: Row = { ...CN_CALENDAR, id: 'cal_cn_backup', code: 'cn_backup', is_default: false }; + +/** The real calendar service, except that a read for a calendar code in `failing` throws a plain Error. */ +function erroringCalendarServiceAt(iso: string, failing: Set) { + let nowMs = Date.parse(iso); + const engine = makeEngine(); + const reads = calendarReads([CN_CALENDAR, CN_BACKUP], CN_EXCEPTIONS); + const real = new WorkCalendarService({ getEngine: () => reads, posture: () => 'isolated', ttlMs: 0 }); + const warned: Array<{ msg: string; meta: any }> = []; + const logger = { info() {}, debug() {}, error() {}, warn: (msg: string, meta: any) => { warned.push({ msg, meta }); } }; + const svc = new ApprovalService({ + engine: engine as any, + logger: logger as any, + clock: { now: () => new Date(nowMs) }, + workCalendar: () => ({ + async addWorkingHours(scope, start, hours) { + if (scope.calendar !== undefined && failing.has(scope.calendar)) { + throw new Error('sys_work_calendar read failed: connection reset'); + } + return real.addWorkingHours(scope, start, hours); + }, + }), + }); + return { svc, engine, warned, setNow: (at: string) => { nowMs = Date.parse(at); } }; +} + +describe("ApprovalEscalation.clock 'working_time': a calendar read that errors costs only that row its deadline", () => { + const created = sh('2026-04-03', '17:00'); + + it('the open still returns the request it wrote and told the approvers about, with no deadline served', async () => { + const { svc, engine, warned } = erroringCalendarServiceAt(created, new Set(['cn_standard'])); + const req = await openPending(svc, workingTimeInput('wt_error_open', 4, { calendar: 'cn_standard' })); + + expect(engine._tables.sys_approval_request?.map((r) => r.id)).toEqual([req.id]); + expect(req.status).toBe('pending'); + expect(req.sla_due_at).toBeUndefined(); + expect(req.sla_due_refusal).toBeUndefined(); + expect(warned.some((w) => w.meta?.request === req.id)).toBe(true); + }); + + it('the approval node suspends its run instead of failing it', async () => { + const { svc } = erroringCalendarServiceAt(created, new Set(['cn_standard'])); + const automation = new AutomationEngine({ info() {}, warn() {}, error() {}, debug() {} } as any); + svc.attachAutomation(automation); + registerApprovalNode(automation, svc); + automation.registerFlow('wt_flow', { + name: 'wt_flow', + label: 'Working-time approval', + type: 'autolaunched', + nodes: [ + { id: 'start', type: 'start', label: 'Start' }, + { id: 'approve_step', type: 'approval', label: 'Approve', config: workingTimeInput('approve_step', 4, { calendar: 'cn_standard' }).config }, + { id: 'end', type: 'end', label: 'End' }, + ], + edges: [ + { id: 'e1', source: 'start', target: 'approve_step' }, + { id: 'e2', source: 'approve_step', target: 'end', label: 'approve' }, + ], + } as any); + + const result = await automation.execute('wt_flow', { object: 'opportunity', record: { id: 'opp1' }, userId: 'u1' }); + expect(result.status).toBe('paused'); + expect(automation.listSuspendedRuns()).toHaveLength(1); + }); + + it('a list and a single read keep every other row and its deadline', async () => { + const failing = new Set(); + const { svc } = erroringCalendarServiceAt(created, failing); + const good = await openPending(svc, workingTimeInput('wt_good', 4, { calendar: 'cn_standard' })); + const bad = await openPending(svc, workingTimeInput('wt_bad', 4, { calendar: 'cn_backup' })); + failing.add('cn_backup'); + + const list = await svc.listRequests({ status: 'pending' }, SYS); + expect(list.map((r) => r.id).sort()).toEqual([good.id, bad.id].sort()); + expect(list.find((r) => r.id === good.id)?.sla_due_at).toBe(sh('2026-04-07', '12:00')); + const listedBad = list.find((r) => r.id === bad.id); + expect(listedBad?.sla_due_at).toBeUndefined(); + expect(listedBad?.sla_due_refusal).toBeUndefined(); + + const read = await svc.getRequest(bad.id, SYS); + expect(read?.id).toBe(bad.id); + expect(read?.sla_due_at).toBeUndefined(); + }); + + it('the sweep skips the row, asks again next pass, and still escalates it once', async () => { + const failing = new Set(); + const { svc, setNow } = erroringCalendarServiceAt(created, failing); + const good = await openPending(svc, workingTimeInput('wt_sweep_good', 4, { calendar: 'cn_standard' })); + const bad = await openPending(svc, workingTimeInput('wt_sweep_bad', 4, { calendar: 'cn_backup' })); + failing.add('cn_backup'); + + setNow(sh('2026-04-20', '12:00')); + expect(await svc.runEscalations()).toMatchObject({ escalated: 1 }); + expect((await svc.listActions(good.id, SYS)).some((a) => a.action === 'escalate')).toBe(true); + expect((await svc.listActions(bad.id, SYS)).some((a) => a.action === 'escalate')).toBe(false); + + failing.delete('cn_backup'); + expect(await svc.runEscalations()).toMatchObject({ escalated: 1 }); + expect((await svc.listActions(bad.id, SYS)).filter((a) => a.action === 'escalate')).toHaveLength(1); + expect(await svc.runEscalations()).toMatchObject({ escalated: 0 }); + }); +}); From 8d0727a1add4a9475d96b3bc1e39ebb8cc87138d Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 00:04:50 +0000 Subject: [PATCH 2/4] fix(approvals): a working-time deadline whose calendar read errors costs only its row the deadline Co-authored-by: Claude Claude-Session: https://claude.ai/code/session_016WV4czKnDE6hsyXQzB9wB2 --- .changeset/35953-sla-deadline-error-guard.md | 12 +++++++++++ .../plugin-approvals/src/approval-service.ts | 21 +++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .changeset/35953-sla-deadline-error-guard.md diff --git a/.changeset/35953-sla-deadline-error-guard.md b/.changeset/35953-sla-deadline-error-guard.md new file mode 100644 index 00000000000..d6e6805783d --- /dev/null +++ b/.changeset/35953-sla-deadline-error-guard.md @@ -0,0 +1,12 @@ +--- +'@objectstack/plugin-approvals': patch +--- + +fix(approvals): a working-time SLA deadline whose calendar read errors no longer fails the request open or the whole list + +Clause-②: no +Metadata-protocol: none + +When an approval step's escalation counts `working_time` and the working calendar read itself fails (an error that is not a calendar refusal, such as a driver error reading `sys_work_calendar`), opening the request threw after the `sys_approval_request` row was written and its approvers were told, so the approval node failed its flow run. The same error failed every list read and single read that included the row, and so every decision that loads it. + +That row is now served with no deadline and the error is logged as a warning; the open returns the request, the run suspends as usual, and the other rows of a list keep their deadlines. A calendar refusal is still served as `sla_due_refusal`, the deadline never falls back to wall-clock hours, and the escalation sweep still skips the row and asks the calendar again on its next pass. The served request shape is unchanged. diff --git a/packages/plugins/plugin-approvals/src/approval-service.ts b/packages/plugins/plugin-approvals/src/approval-service.ts index f1d462d9861..4b71ae4045e 100644 --- a/packages/plugins/plugin-approvals/src/approval-service.ts +++ b/packages/plugins/plugin-approvals/src/approval-service.ts @@ -8321,7 +8321,9 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega * organization's calendar, named by `escalation.calendar` or else its default * (D2), recomputed on every call and never stored (D12). A calendar refusal * (none resolves, or it is not maintained that far, D8) answers - * `{ refusal }` instead of a deadline (D9); every other failure throws. + * `{ refusal }` instead of a deadline (D9); every other failure throws, and + * each caller costs only that row its deadline: the sweep skips it until the + * next pass, {@link projectWorkingTimeDue} serves it with none. */ private async slaDeadline(raw: any, cfg: any): Promise { if (!escalationCountsWorkingTime(cfg)) { @@ -8354,12 +8356,27 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega * calendar cannot count it, through {@link slaDeadline}. `raws[i]` is the * stored row `rows[i]` was mapped from; a `wall_clock` row already carries * its deadline from the synchronous mapper and is left alone. + * + * A calendar read that errors (not a refusal) costs only its own row the + * deadline: it is logged and that row is served with none, never a + * wall-clock one (D2). Every caller serves a row that is already written — + * `openNodeRequest` after the insert and the approvers' notices, the list + * and single reads — so the error never fails the write or the other rows. + * The sweep reads {@link slaDeadline} itself and retries the row next pass. */ private async projectWorkingTimeDue(raws: readonly any[], rows: ApprovalRequestRow[]): Promise { for (let i = 0; i < rows.length; i++) { const cfg = parseJson(raws[i]?.node_config_json, undefined); if (!escalationCountsWorkingTime(cfg)) continue; - const deadline = await this.slaDeadline(raws[i], cfg); + let deadline: SlaDeadline | undefined; + try { + deadline = await this.slaDeadline(raws[i], cfg); + } catch (err: any) { + this.logger?.warn?.('[approvals] working-time SLA deadline could not be computed; the request is served without one', { + request: raws[i]?.id, error: err?.message ?? String(err), + }); + continue; + } if (!deadline) continue; if ('refusal' in deadline) rows[i]!.sla_due_refusal = deadline.refusal; else rows[i]!.sla_due_at = deadline.due; From 832f7210c987de3045cb4d96eb77e0f53788bff5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 00:24:32 +0000 Subject: [PATCH 3/4] test(approvals): open the second request on its own record Co-authored-by: Claude Claude-Session: https://claude.ai/code/session_016WV4czKnDE6hsyXQzB9wB2 --- .../src/approval-service-sla-calendar-clock.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/plugins/plugin-approvals/src/approval-service-sla-calendar-clock.test.ts b/packages/plugins/plugin-approvals/src/approval-service-sla-calendar-clock.test.ts index f0f1fb1fdb4..eab6a8204f9 100644 --- a/packages/plugins/plugin-approvals/src/approval-service-sla-calendar-clock.test.ts +++ b/packages/plugins/plugin-approvals/src/approval-service-sla-calendar-clock.test.ts @@ -471,7 +471,7 @@ describe("ApprovalEscalation.clock 'working_time': a calendar read that errors c const failing = new Set(); const { svc } = erroringCalendarServiceAt(created, failing); const good = await openPending(svc, workingTimeInput('wt_good', 4, { calendar: 'cn_standard' })); - const bad = await openPending(svc, workingTimeInput('wt_bad', 4, { calendar: 'cn_backup' })); + const bad = await openPending(svc, { ...workingTimeInput('wt_bad', 4, { calendar: 'cn_backup' }), recordId: 'opp2', record: { id: 'opp2', amount: 100 } }); failing.add('cn_backup'); const list = await svc.listRequests({ status: 'pending' }, SYS); @@ -490,7 +490,7 @@ describe("ApprovalEscalation.clock 'working_time': a calendar read that errors c const failing = new Set(); const { svc, setNow } = erroringCalendarServiceAt(created, failing); const good = await openPending(svc, workingTimeInput('wt_sweep_good', 4, { calendar: 'cn_standard' })); - const bad = await openPending(svc, workingTimeInput('wt_sweep_bad', 4, { calendar: 'cn_backup' })); + const bad = await openPending(svc, { ...workingTimeInput('wt_sweep_bad', 4, { calendar: 'cn_backup' }), recordId: 'opp2', record: { id: 'opp2', amount: 100 } }); failing.add('cn_backup'); setNow(sh('2026-04-20', '12:00')); From dd312c3b9568958f53cb7ac364cf38de75a0cfc4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 00:29:51 +0000 Subject: [PATCH 4/4] test(approvals): narrow the calendar code before the failing-set lookup Co-authored-by: Claude Claude-Session: https://claude.ai/code/session_016WV4czKnDE6hsyXQzB9wB2 --- .../src/approval-service-sla-calendar-clock.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugins/plugin-approvals/src/approval-service-sla-calendar-clock.test.ts b/packages/plugins/plugin-approvals/src/approval-service-sla-calendar-clock.test.ts index eab6a8204f9..de853d65fcf 100644 --- a/packages/plugins/plugin-approvals/src/approval-service-sla-calendar-clock.test.ts +++ b/packages/plugins/plugin-approvals/src/approval-service-sla-calendar-clock.test.ts @@ -418,7 +418,7 @@ function erroringCalendarServiceAt(iso: string, failing: Set) { clock: { now: () => new Date(nowMs) }, workCalendar: () => ({ async addWorkingHours(scope, start, hours) { - if (scope.calendar !== undefined && failing.has(scope.calendar)) { + if (typeof scope.calendar === 'string' && failing.has(scope.calendar)) { throw new Error('sys_work_calendar read failed: connection reset'); } return real.addWorkingHours(scope, start, hours);