@@ -1294,6 +1294,13 @@ export interface ExecutionLogEntry {
12941294 startedAt: string;
12951295 completedAt?: string;
12961296 durationMs?: number;
1297+ /**
1298+ * [#32284] `ExecutionLogSchema.waitMs`: the part of `durationMs` the run
1299+ * spent suspended, summed over every pause it has left — `0` on a run
1300+ * that never paused, absent when not recorded. Written beside `durationMs`
1301+ * at every site that writes one; never derived from it here.
1302+ */
1303+ waitMs?: number;
12971304 trigger: { type: string; userId?: string; object?: string; recordId?: string };
12981305 // ↑ built by `buildRunTrigger` at EVERY site — see its doc comment for why
12991306 // that is a chokepoint rather than eight object literals.
@@ -1776,11 +1783,52 @@ export interface SuspendedRun {
17761783 context: AutomationContext;
17771784 startedAt: string;
17781785 startTime: number;
1786+ /**
1787+ * [#32284] The run's wait clock: the milliseconds it had spent EXECUTING
1788+ * (its age at this pause minus every earlier pause) when it paused here.
1789+ * Nothing executes while the run is parked, so at any later instant the
1790+ * run's total wait is its age minus this — which is how one number sums
1791+ * every pause of a run that suspends, resumes and suspends again
1792+ * ({@link waitOfPausedRun}), and why a restore that re-arms a consumed
1793+ * pause needs no clock of its own: the snapshot carries this value, and
1794+ * the time from the pause to the restore is wait like the rest of it.
1795+ *
1796+ * Set at every site that builds a suspension ({@link activeMsAtPause});
1797+ * carried unchanged by every re-save of the SAME pause (`{ ...run }`).
1798+ * Absent on a suspension recorded before this field existed, or by a
1799+ * host store that does not round-trip it: such a run's `waitMs` is not
1800+ * recorded rather than guessed.
1801+ */
1802+ activeMs?: number;
17791803 correlation?: string;
17801804 /** Screen the run paused on (screen-flow runtime), for re-fetch + UI render. */
17811805 screen?: ScreenSpec;
17821806}
17831807
1808+ /**
1809+ * [#32284] The `activeMs` a suspension records: the run's age at the pause
1810+ * minus the wait it has already accumulated. `undefined` when that wait is not
1811+ * known (the run's previous pause carried no clock), so an unknown wait is
1812+ * never turned into a precise-looking number further on.
1813+ */
1814+ export function activeMsAtPause(elapsedMs: number, waitMs: number | undefined): number | undefined {
1815+ return waitMs === undefined ? undefined : Math.max(0, elapsedMs - waitMs);
1816+ }
1817+
1818+ /**
1819+ * [#32284] A paused run's total wait at the moment its pause ENDS — a resume
1820+ * consumes it, or the run is cancelled, failed or settled where it waits, or a
1821+ * restore records it paused again: its age then (`elapsedMs`, measured from
1822+ * `startTime` like `durationMs`) minus the time it had spent executing when it
1823+ * paused. `undefined` for a suspension with no clock ({@link SuspendedRun.activeMs}).
1824+ *
1825+ * Clamped at zero: `startTime` may have been stamped by another replica's
1826+ * clock, and a published `waitMs` is never negative.
1827+ */
1828+ export function waitOfPausedRun(run: Pick<SuspendedRun, 'activeMs'>, elapsedMs: number): number | undefined {
1829+ return typeof run.activeMs === 'number' ? Math.max(0, elapsedMs - run.activeMs) : undefined;
1830+ }
1831+
17841832/**
17851833 * Pluggable durable store for suspended runs (ADR-0019). The engine persists a
17861834 * {@link SuspendedRun} on suspend and deletes it on terminal completion; on
@@ -1868,6 +1916,13 @@ export interface RunRecord {
18681916 /** When the run reached its terminal state. */
18691917 finishedAt?: string;
18701918 durationMs?: number;
1919+ /**
1920+ * [#32284] The run's {@link ExecutionLogEntry.waitMs}, persisted beside
1921+ * `durationMs` (`sys_automation_run.wait_ms`) so a run read back after a
1922+ * restart or a ring eviction still says how much of its duration was
1923+ * waiting. Absent on a row written before the column existed.
1924+ */
1925+ waitMs?: number;
18711926 /** Failure reason for a `failed` run — what a designer needs to fix it. */
18721927 error?: string;
18731928 /**
@@ -7726,6 +7781,8 @@ export class AutomationEngine implements IAutomationService {
77267781 startedAt,
77277782 completedAt: new Date().toISOString(),
77287783 durationMs,
7784+ // [#32284] The initial execution never paused: a measured 0.
7785+ waitMs: 0,
77297786 trigger: buildRunTrigger(context),
77307787 steps,
77317788 output,
@@ -7824,7 +7881,7 @@ export class AutomationEngine implements IAutomationService {
78247881 if (isRefusalSignal(err)) {
78257882 return this.finishRefusedRun({
78267883 runId, flowName, flowVersion: flow.version,
7827- startedAt, durationMs: Date.now() - startTime,
7884+ startedAt, durationMs: Date.now() - startTime, waitMs: 0,
78287885 steps, flow, variables,
78297886 refusalMessage: err.message, context,
78307887 });
@@ -7834,6 +7891,9 @@ export class AutomationEngine implements IAutomationService {
78347891 // caller can later `resume()` it. This is NOT a failure.
78357892 if (isSuspendSignal(err)) {
78367893 const durationMs = Date.now() - startTime;
7894+ // [#32284] The run's first pause: nothing waited yet, so all
7895+ // of `durationMs` was execution — the clock the resume reads.
7896+ const waitMs = 0;
78377897 // #7639 — ONE snapshot expression feeding BOTH consumers: the
78387898 // continuation the run will resume from, and the `paused` log
78397899 // entry run-detail serves. Same object, so what an operator
@@ -7852,6 +7912,7 @@ export class AutomationEngine implements IAutomationService {
78527912 context: runContext,
78537913 startedAt,
78547914 startTime,
7915+ activeMs: activeMsAtPause(durationMs, waitMs),
78557916 correlation: err.correlation,
78567917 screen: err.screen,
78577918 });
@@ -7862,6 +7923,7 @@ export class AutomationEngine implements IAutomationService {
78627923 status: 'paused',
78637924 startedAt,
78647925 durationMs,
7926+ waitMs,
78657927 trigger: buildRunTrigger(context),
78667928 steps,
78677929 variables: variablesSnapshot,
@@ -7926,6 +7988,7 @@ export class AutomationEngine implements IAutomationService {
79267988 startedAt,
79277989 completedAt: new Date().toISOString(),
79287990 durationMs,
7991+ waitMs: 0,
79297992 trigger: buildRunTrigger(context),
79307993 steps,
79317994 error: errorMessage,
@@ -9943,6 +10006,8 @@ export class AutomationEngine implements IAutomationService {
994310006 flowVersion?: number;
994410007 startedAt: string;
994510008 durationMs: number;
10009+ /** [#32284] The run's `waitMs` — `undefined` only where it is not recorded. */
10010+ waitMs: number | undefined;
994610011 steps: StepLogEntry[];
994710012 /** Read for its `isOutput` variable declarations — see `output` below. */
994810013 flow: FlowParsed;
0 commit comments