Skip to content

Commit df29270

Browse files
committed
feat(automation): record a run's wait time beside its duration
Adds an optional waitMs to the published run record (ExecutionLog) and fills it on every run-record writer: 0 for a run that never paused, the sum of every pause for a resumed run. The engine keeps one wait clock per suspension (activeMs, persisted as sys_automation_run.active_ms) and the terminal row carries wait_ms. Claude-Session: https://claude.ai/code/session_016WV4cxHwXNcGcAHNerZsom Co-authored-by: Claude <noreply@anthropic.com>
1 parent f068cde commit df29270

9 files changed

Lines changed: 228 additions & 9 deletions

File tree

‎packages/services/service-automation/src/consumed-suspension-restore.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { describeThrownForLog } from './thrown-cause-diagnostics.js';
1919
// The values below (`MAX_CONSUMED_SUSPENSIONS`, `MAX_RESTORE_CHAIN_HOPS`,
20-
// `buildRunTrigger`, `chainEntryOf` and `nestedChildRunId`) are read only
20+
// `buildRunTrigger`, `chainEntryOf`, `nestedChildRunId` and `waitOfPausedRun`) are read only
2121
// inside the function bodies, at call time, never while a module is
2222
// evaluating, so the import cycle with ./engine.js is safe under ESM live
2323
// bindings; the tsup bundle is one file and has no cycle.
@@ -27,6 +27,7 @@ import {
2727
buildRunTrigger,
2828
chainEntryOf,
2929
nestedChildRunId,
30+
waitOfPausedRun,
3031
type AutomationEngine,
3132
type ChainRestoreEntry,
3233
type ConsumedSuspension,
@@ -457,13 +458,22 @@ export async function restoreOneConsumedSuspension(
457458
// row: `ExecutionStatus` carries no such member, so a durable
458459
// status that survives the restart is a spec-vocabulary decision
459460
// nobody has taken — the row's discriminator stays the snapshot.
461+
//
462+
// [#32284] `waitMs` from the SNAPSHOT's clock: the restore re-arms the
463+
// pause the failed resume consumed, so the run reads as never having
464+
// left it — the attempt that failed, the time it sat stranded and the
465+
// wait from here to its next resume are all one pause, and its active
466+
// time is still what it had executed when it first parked. That is
467+
// also what its step log says: the snapshot's steps end at the pause.
468+
const durationMs = Date.now() - consumed.run.startTime;
460469
this.recordLog({
461470
id: runId,
462471
flowName: consumed.run.flowName,
463472
flowVersion: consumed.run.flowVersion,
464473
status: 'paused',
465474
startedAt: consumed.run.startedAt,
466-
durationMs: Date.now() - consumed.run.startTime,
475+
durationMs,
476+
waitMs: waitOfPausedRun(consumed.run, durationMs),
467477
trigger: buildRunTrigger(consumed.run.context),
468478
steps: consumed.run.steps,
469479
variables: consumed.run.variables,

‎packages/services/service-automation/src/dag-traversal.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ import { summarizeRun } from './run-summary.js';
2424
import { withDroppedFields } from './run-dropped-fields.js';
2525
import { describeThrownForLog } from './thrown-cause-diagnostics.js';
2626
// The values below (`AutomationEngine` for its public static
27-
// `MAX_NODE_REENTRIES`) are read only inside the function bodies, at call time,
27+
// `MAX_NODE_REENTRIES`, and `activeMsAtPause`) are read only inside the function bodies, at call time,
2828
// never while a module is evaluating, so the import cycle with ./engine.js is
2929
// safe under ESM live bindings; the tsup bundle is one file and has no cycle.
3030
import {
31+
activeMsAtPause,
3132
AutomationEngine,
3233
buildRunTrigger,
3334
DEFAULT_BRANCH_LABEL,
@@ -1361,6 +1362,8 @@ export async function executeWithoutRetry(
13611362
startedAt,
13621363
completedAt: new Date().toISOString(),
13631364
durationMs,
1365+
// [#32284] A retry attempt is an initial execution: it never paused.
1366+
waitMs: 0,
13641367
trigger: buildRunTrigger(context),
13651368
steps,
13661369
output,
@@ -1421,7 +1424,7 @@ export async function executeWithoutRetry(
14211424
if (isRefusalSignal(err)) {
14221425
return this.finishRefusedRun({
14231426
runId, flowName, flowVersion: flow.version,
1424-
startedAt, durationMs: Date.now() - startTime,
1427+
startedAt, durationMs: Date.now() - startTime, waitMs: 0,
14251428
steps, flow, variables,
14261429
refusalMessage: err.message, context,
14271430
});
@@ -1468,6 +1471,8 @@ export async function executeWithoutRetry(
14681471
// method returns — the only id `resume()` can be called with.
14691472
if (isSuspendSignal(err)) {
14701473
const durationMs = Date.now() - startTime;
1474+
// [#32284] This attempt's first pause — see `execute()`'s.
1475+
const waitMs = 0;
14711476
// #7639 — ONE snapshot expression feeding BOTH consumers: the
14721477
// continuation the run will resume from, and the `paused` log
14731478
// entry run-detail serves. Same object, so what an operator
@@ -1485,6 +1490,7 @@ export async function executeWithoutRetry(
14851490
context: runContext,
14861491
startedAt,
14871492
startTime,
1493+
activeMs: activeMsAtPause(durationMs, waitMs),
14881494
correlation: err.correlation,
14891495
screen: err.screen,
14901496
});
@@ -1495,6 +1501,7 @@ export async function executeWithoutRetry(
14951501
status: 'paused',
14961502
startedAt,
14971503
durationMs,
1504+
waitMs,
14981505
trigger: buildRunTrigger(context),
14991506
steps,
15001507
variables: variablesSnapshot,
@@ -1539,6 +1546,7 @@ export async function executeWithoutRetry(
15391546
startedAt,
15401547
completedAt: new Date().toISOString(),
15411548
durationMs,
1549+
waitMs: 0,
15421550
trigger: buildRunTrigger(context),
15431551
steps,
15441552
error: errorMessage,

‎packages/services/service-automation/src/engine.ts‎

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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;

‎packages/services/service-automation/src/run-history.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ export function runRecordToLogEntry(r: RunRecord): ExecutionLogEntry {
270270
startedAt: r.startedAt,
271271
completedAt: r.finishedAt,
272272
durationMs: r.durationMs,
273+
// [#32284] The persisted wait — absent on a row written before the
274+
// column existed, which reads "not recorded", never "did not wait".
275+
waitMs: r.waitMs,
273276
// #7533 — the persisted trigger attribution, so a run rehydrated
274277
// after a restart still says what fired it and (for a record
275278
// change) which record. `type` falls back to `''` ONLY for rows
@@ -615,6 +618,9 @@ export function recordLog(
615618
startedAt: entry.startedAt,
616619
finishedAt: entry.completedAt,
617620
durationMs: entry.durationMs,
621+
// [#32284] Beside `durationMs`, so the durable row answers "how
622+
// much of that was waiting?" after a restart as the ring does.
623+
waitMs: entry.waitMs,
618624
error: entry.error,
619625
// [#15788] The rendered refusal, carried onto the durable row
620626
// beside `status: 'refused'`. Set by exactly one producer
@@ -717,6 +723,8 @@ export function finishRefusedRun(this: Pick<RunHistoryHost, 'recordLog' | 'logge
717723
flowVersion?: number;
718724
startedAt: string;
719725
durationMs: number;
726+
/** [#32284] The run's `waitMs` — `undefined` only where it is not recorded. */
727+
waitMs: number | undefined;
720728
steps: StepLogEntry[];
721729
/** Read for its `isOutput` variable declarations — see `output` below. */
722730
flow: FlowParsed;
@@ -746,6 +754,7 @@ export function finishRefusedRun(this: Pick<RunHistoryHost, 'recordLog' | 'logge
746754
startedAt: args.startedAt,
747755
completedAt: new Date().toISOString(),
748756
durationMs: args.durationMs,
757+
waitMs: args.waitMs,
749758
trigger: buildRunTrigger(args.context),
750759
steps: args.steps,
751760
output,

0 commit comments

Comments
 (0)