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
24 changes: 24 additions & 0 deletions .changeset/35357-approval-escalation-working-time-clock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@objectstack/spec': minor
'@objectstack/plugin-approvals': minor
'@objectstack/lint': minor
'@object-ui/console': minor
'@object-ui/i18n': minor
---

feat(approvals): an escalation deadline can count working time on an organization's working calendar (`escalation.clock: 'working_time'`)

Clause-②: yes (widening)
Metadata-protocol: added
Skills-docs: follow-up owed — skills/objectstack-automation/references/state-machines-and-approvals.md

An approval node's `escalation` gains two optional keys (ADR-0157 D5):

- **`clock`**: `'wall_clock'` (the default) or `'working_time'`. Under `wall_clock` nothing changes: `timeoutHours` counts elapsed hours from `created_at`, nights, weekends and holidays included, and every request already open keeps its deadline (a stored snapshot carries no `clock`). Under `working_time` the hours count only the working periods of a working calendar (`sys_work_calendar`), skipping its holidays and counting its make-up workdays: a request opened at 17:00 on a Friday with `timeoutHours: 4`, on 09:00-12:00 and 13:00-18:00 periods, escalates at 12:00 on the next working day.
- **`calendar`**: the code of the working calendar to count on. Left out, the request organization's default calendar counts. There is never a fallback to wall-clock hours.

The approvals sweep and the request read share one deadline function. A `working_time` deadline is counted again on every sweep and every read and is never stored, so a holiday entered after a request opened moves its deadline; an escalation that already fired stays fired (single-shot, ADR-0042). When the calendar cannot count the deadline (no calendar has the code, the organization has no default, or the calendar is not maintained as far as the deadline needs), the request does not escalate, the next sweep tries again, and the request read carries a new additive field, `sla_due_refusal` (`code`, `reason` or `date`, `message`), in place of `sla_due_at`. The approvals inbox shows "Calendar can't compute" with the reason where the SLA countdown sits, in every console language.

`@objectstack/lint` refuses a `calendar` written beside the wall clock (`approval-escalation-calendar-without-working-time`, error): nothing would read it. Whether the named calendar exists is the organization's runtime data and is never checked at publish. The Studio approval inspector offers the clock and, under working time only, the calendar.

Not released alone: this ships in the same release as the working-calendar objects and service and the working-day formula functions (ADR-0157 D4).
42 changes: 38 additions & 4 deletions content/docs/automation/approvals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1147,10 +1147,44 @@ resumes exactly as if a human had clicked. Every escalation writes an audit row
of kind `escalate` naming the configured action; it, the auto decision that
follows it, and the escalation's notifications record no actor, because no
person acted (ADR-0118 D1).
`timeoutHours` counts **calendar (wall-clock) hours** — nights, weekends and
holidays included, because the platform ships no business-hours calendar — so a
request opened at 17:00 on a Friday with `timeoutHours: 4` escalates at 21:00
that same Friday.
`timeoutHours` counts from the moment the request was submitted, and
`clock` decides how its hours count (ADR-0157 D5):

- **`wall_clock`** (the default) counts every hour — nights, weekends and
holidays included — so a request opened at 17:00 on a Friday with
`timeoutHours: 4` escalates at 21:00 that same Friday.
- **`working_time`** counts only the working periods of a working calendar
(`sys_work_calendar`, kept by the organization's administrators in Setup),
skipping its holidays and counting its make-up workdays. With 09:00–12:00
and 13:00–18:00 working periods, the same Friday request escalates at 12:00
on the next working day: Monday, or Tuesday when Monday is a holiday, or the
Saturday itself when it is a make-up workday.

```ts
escalation: {
timeoutHours: 4,
clock: 'working_time',
calendar: 'cn_standard', // optional: the organization's default calendar when left out
action: 'notify',
escalateTo: 'approvals_supervisor',
}
```

The calendar is the one `calendar` names, else the organization's default
calendar; there is never a fallback to wall-clock hours. The deadline is
counted again on every sweep and every read, so a holiday entered after a
request opened moves its deadline (an escalation that already fired stays
fired). When the calendar cannot count the deadline — no calendar has that
code, the organization has no default, or the calendar is not maintained as
far as the deadline needs — the request does **not** escalate. Its read
carries no `sla_due_at` and a `sla_due_refusal` instead (the refusal `code`,
its `reason` or the `date` it needs, and a `message`), the approvals inbox
shows "Calendar can't compute" with the reason, and the next sweep tries
again, so fixing the calendar is the only step. Publishing checks only what is
written: a `calendar` beside the wall clock is refused
(`approval-escalation-calendar-without-working-time`), since nothing would read
it; whether the named calendar exists is the organization's data, checked when
the deadline is counted.

<Callout type="warn">
**Escalation needs the job service.** The plugin sweeps pending requests on an
Expand Down
10 changes: 7 additions & 3 deletions content/docs/references/automation/approval.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ const result = ApprovalDecision.parse(data);
| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **enabled** | `boolean` | optional (default: `true`) | Turns this step's deadline on or off. It is on by default once a deadline is set: switch it off to keep the settings without acting on them, or remove the escalation to drop the deadline altogether |
| **timeoutHours** | `number` | ✅ | How many hours a request may wait before the escalation runs. These are calendar (wall-clock) hours — nights, weekends and holidays count. The platform ships no business-hours calendar: a request opened at 17:00 on a Friday with 4 hours escalates at 21:00 that same Friday |
| **timeoutHours** | `number` | ✅ | How many hours a request may wait before the escalation runs, counted from when it was submitted. The clock decides how the hours count: wall-clock hours (the default) count nights, weekends and holidays, so a request opened at 17:00 on a Friday with 4 hours escalates at 21:00 that Friday; working hours count only the working periods of a working calendar |
| **clock** | `Enum<'wall_clock' \| 'working_time'>` | optional (default: `"wall_clock"`) | How the timeout's hours count: every hour, or only working hours. Wall-clock hours ('wall_clock', the default) count nights, weekends and holidays. Working hours ('working_time') count only the working periods of the organization's working calendar, skipping its holidays and counting its make-up workdays; when the calendar cannot count the deadline (no such calendar, or not maintained that far) the request does not escalate and shows why, until the calendar is fixed |
| **calendar** | `string` | optional | Which working calendar counts the hours under working time, by its code. Leave it empty to use the organization's default calendar |
| **action** | `Enum<'reassign' \| 'auto_approve' \| 'auto_reject' \| 'notify'>` | optional (default: `"notify"`) | What happens when the deadline passes: hand the request to someone else, approve it, reject it, or notify someone (the default) |
| **escalateTo** | `string` | optional | Who takes over or is notified when the deadline passes: usually a position, or a specific person. Write a position machine name or 'position:`<name>`', a user id or an email. A 'team:', 'department:', 'org_membership_level:' or 'role:' address is refused: no approver acts under one |
| **notifySubmitter** | `boolean` | optional (default: `true`) | Tell the person who submitted the request when it escalates |
Expand Down Expand Up @@ -77,7 +79,7 @@ const result = ApprovalDecision.parse(data);
| **onEmptyApprovers** | `Enum<'admin_rescue' \| 'fail' \| 'auto_approve' \| 'fallback'>` | optional (default: `"admin_rescue"`) | What happens when this step finds no one to approve: by default the organization's administrators decide; you can instead name backup approvers, fail the step, or approve it automatically. 'admin_rescue' (default) opens the request on the administrators; 'fallback' opens it on fallbackApprovers instead, naming who decides; 'fail' fails the step; 'auto_approve' approves it with no one deciding |
| **fallbackApprovers** | `{ type: Enum<'manager' \| 'position' \| 'department' \| 'team' \| 'queue' \| 'field' \| …>; value?: string; resolveAs?: Enum<'user' \| 'department' \| 'position' \| 'team'>; group?: string; … }[]` | optional | Backup approvers who decide when this step finds no one else. Read only when onEmptyApprovers is 'fallback' |
| **decisionOutputs** | `(string \| { key: string; label?: string; type?: Enum<'text' \| 'user' \| 'department' \| 'position' \| 'team'>; multiple?: boolean; … })[]` | optional | Extra answers the approver gives with the decision, such as who approves a later step; later steps read them. Each entry is a plain name (a text box) or `{ key, type, multiple }` to show a picker |
| **escalation** | `{ enabled?: boolean; timeoutHours: number; action?: Enum<'reassign' \| 'auto_approve' \| 'auto_reject' \| 'notify'>; escalateTo?: string; … }` | optional | A deadline for this step, and what happens when it passes |
| **escalation** | `{ enabled?: boolean; timeoutHours: number; clock?: Enum<'wall_clock' \| 'working_time'>; calendar?: string; … }` | optional | A deadline for this step, and what happens when it passes |
| **maxRevisions** | `integer` | optional (default: `3`) | How many times a request can be sent back for changes before it is rejected automatically (0 turns send-back off) |

### Nested Shape: `ApprovalNodeConfig.approvers[number]`
Expand Down Expand Up @@ -115,7 +117,9 @@ const result = ApprovalDecision.parse(data);
| Property | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| **enabled** | `boolean` | optional (default: `true`) | Turns this step's deadline on or off. It is on by default once a deadline is set: switch it off to keep the settings without acting on them, or remove the escalation to drop the deadline altogether |
| **timeoutHours** | `number` | ✅ | How many hours a request may wait before the escalation runs. These are calendar (wall-clock) hours — nights, weekends and holidays count. The platform ships no business-hours calendar: a request opened at 17:00 on a Friday with 4 hours escalates at 21:00 that same Friday |
| **timeoutHours** | `number` | ✅ | How many hours a request may wait before the escalation runs, counted from when it was submitted. The clock decides how the hours count: wall-clock hours (the default) count nights, weekends and holidays, so a request opened at 17:00 on a Friday with 4 hours escalates at 21:00 that Friday; working hours count only the working periods of a working calendar |
| **clock** | `Enum<'wall_clock' \| 'working_time'>` | optional (default: `"wall_clock"`) | How the timeout's hours count: every hour, or only working hours. Wall-clock hours ('wall_clock', the default) count nights, weekends and holidays. Working hours ('working_time') count only the working periods of the organization's working calendar, skipping its holidays and counting its make-up workdays; when the calendar cannot count the deadline (no such calendar, or not maintained that far) the request does not escalate and shows why, until the calendar is fixed |
| **calendar** | `string` | optional | Which working calendar counts the hours under working time, by its code. Leave it empty to use the organization's default calendar |
| **action** | `Enum<'reassign' \| 'auto_approve' \| 'auto_reject' \| 'notify'>` | optional (default: `"notify"`) | What happens when the deadline passes: hand the request to someone else, approve it, reject it, or notify someone (the default) |
| **escalateTo** | `string` | optional | Who takes over or is notified when the deadline passes: usually a position, or a specific person. Write a position machine name or 'position:`<name>`', a user id or an email. A 'team:', 'department:', 'org_membership_level:' or 'role:' address is refused: no approver acts under one |
| **notifySubmitter** | `boolean` | optional (default: `true`) | Tell the person who submitted the request when it escalates |
Expand Down
1 change: 1 addition & 0 deletions packages/lint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export {
APPROVAL_APPROVER_TYPE_UNKNOWN,
APPROVAL_APPROVER_TYPE_UNSUPPORTED,
APPROVAL_ESCALATION_REASSIGN_NO_TARGET,
APPROVAL_ESCALATION_CALENDAR_WITHOUT_WORKING_TIME,
APPROVAL_APPROVERS_MAY_RESOLVE_EMPTY,
APPROVAL_EXPRESSION_INVALID,
APPROVAL_EXPRESSION_NO_EMPTY_POLICY,
Expand Down
25 changes: 25 additions & 0 deletions packages/lint/src/validate-approval-approvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
APPROVAL_APPROVER_TYPE_UNKNOWN,
APPROVAL_APPROVER_TYPE_UNSUPPORTED,
APPROVAL_ESCALATION_REASSIGN_NO_TARGET,
APPROVAL_ESCALATION_CALENDAR_WITHOUT_WORKING_TIME,
APPROVAL_APPROVERS_MAY_RESOLVE_EMPTY,
APPROVAL_EXPRESSION_INVALID,
APPROVAL_EXPRESSION_NO_EMPTY_POLICY,
Expand Down Expand Up @@ -176,6 +177,30 @@ describe('validateApprovalApprovers', () => {
expect(findings[0].hint).toContain('position');
});

it("refuses an escalation.calendar beside the wall clock: nothing reads it (ADR-0157 D9)", () => {
const stack = stackWithApprovers([{ type: 'user', value: 'u1' }]);
const node = (stack.flows as any)[0].nodes[1];
for (const clock of [undefined, 'wall_clock']) {
node.config.escalation = { enabled: true, timeoutHours: 4, action: 'notify', calendar: 'cn_standard', ...(clock ? { clock } : {}) };
const findings = validateApprovalApprovers(stack);
expect(findings).toHaveLength(1);
expect(findings[0]).toMatchObject({
severity: 'error',
rule: APPROVAL_ESCALATION_CALENDAR_WITHOUT_WORKING_TIME,
path: 'flows[0].nodes[1].config.escalation.calendar',
});
}
});

it("accepts a calendar on 'working_time', and 'working_time' with no calendar: whether a calendar exists is never judged here", () => {
const stack = stackWithApprovers([{ type: 'user', value: 'u1' }]);
const node = (stack.flows as any)[0].nodes[1];
node.config.escalation = { enabled: true, timeoutHours: 4, action: 'notify', clock: 'working_time', calendar: 'no_such_calendar' };
expect(validateApprovalApprovers(stack)).toEqual([]);
node.config.escalation = { enabled: true, timeoutHours: 4, action: 'notify', clock: 'working_time' };
expect(validateApprovalApprovers(stack)).toEqual([]);
});

it('accepts reassign escalation with a target, and non-reassign actions without one', () => {
const stack = stackWithApprovers([{ type: 'user', value: 'u1' }]);
const node = (stack.flows as any)[0].nodes[1];
Expand Down
23 changes: 23 additions & 0 deletions packages/lint/src/validate-approval-approvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* | approval-approver-type-deprecated | warning | ADR-0090 D3 (#3133) |
* | approval-approver-type-unknown | warning | contract-first (PD #12) |
* | approval-escalation-reassign-no-target | warning | silent notify degradation |
* | approval-escalation-calendar-without-working-time | error | ADR-0157 D9 (static only) |
* | approval-approvers-may-resolve-empty | info | empty-position dead-end (#3424) |
* | approval-approvers-may-resolve-empty | info | unset-manager dead-end (#16748) |
* | approval-expression-invalid | error/info | #3447 P2 closed-root expressions |
Expand Down Expand Up @@ -61,6 +62,7 @@ export const APPROVAL_APPROVER_TYPE_DEPRECATED = 'approval-approver-type-depreca
export const APPROVAL_APPROVER_TYPE_UNKNOWN = 'approval-approver-type-unknown';
export const APPROVAL_APPROVER_TYPE_UNSUPPORTED = 'approval-approver-type-unsupported';
export const APPROVAL_ESCALATION_REASSIGN_NO_TARGET = 'approval-escalation-reassign-no-target';
export const APPROVAL_ESCALATION_CALENDAR_WITHOUT_WORKING_TIME = 'approval-escalation-calendar-without-working-time';
export const APPROVAL_APPROVERS_MAY_RESOLVE_EMPTY = 'approval-approvers-may-resolve-empty';
export const APPROVAL_EXPRESSION_INVALID = 'approval-expression-invalid';
export const APPROVAL_EXPRESSION_NO_EMPTY_POLICY = 'approval-expression-no-empty-policy';
Expand Down Expand Up @@ -795,6 +797,27 @@ export function validateApprovalApprovers(
});
}
}

// ADR-0157 D9, the static half of the working-time clock: a calendar
// code beside the wall clock (written, or the default) names a calendar
// nothing reads — the hours count on the wall clock, while the author
// who named a calendar meant working time. Whether the named calendar
// EXISTS is per-organization runtime data and is never judged here.
const calendar = escalation && typeof escalation === 'object' && typeof escalation.calendar === 'string'
? escalation.calendar.trim()
: '';
if (calendar && escalation!.clock !== 'working_time') {
findings.push({
severity: 'error',
rule: APPROVAL_ESCALATION_CALENDAR_WITHOUT_WORKING_TIME,
where,
path: `${nodePath}.config.escalation.calendar`,
message:
`escalation.calendar '${calendar}' is set but escalation.clock is not 'working_time' — the ` +
`wall clock counts the timeout and nothing reads the calendar.`,
hint: `Set escalation.clock to 'working_time' to count working hours on that calendar, or remove escalation.calendar.`,
});
}
}
}

Expand Down
Loading
Loading