@@ -65,7 +65,7 @@ export const DEFAULT_BRANCH_LABEL = 'default';
6565
6666import { runTriggerUserId, type SeededRunContext } from './runtime-identity.js';
6767import { flowContentChecksum } from './flow-identity.js';
68- import { hasReservedVariableKeySegment } from './scope-key-segments.js';
68+ import { HeldValueCycleError, hasReservedVariableKeySegment } from './scope-key-segments.js';
6969import { carriedRefusalOfResult, readCarriedRefusal, type CarriedRefusal } from './inner-refusal.js';
7070import { summarizeRun } from './run-summary.js';
7171import { withDroppedFields } from './run-dropped-fields.js';
@@ -8197,8 +8197,9 @@ export class AutomationEngine implements IAutomationService {
81978197 * ONLY the declared relation keys, and only when the re-read actually returned
81988198 * an object/array, so bare lookup ids and #1872 multi-lookup arrays on other
81998199 * relations — and the formula fields the trigger already hydrated — stay
8200- * untouched. Mutates `record` in place (the same object the run's variable map
8201- * already references). Best-effort: any failure leaves `record` unexpanded
8200+ * untouched. Mutates `record` in place: the run context's own copy of the
8201+ * record ({@link resolveRunContext} takes it first, and the run's variables
8202+ * are seeded from it after), never the object the caller handed. Best-effort: any failure leaves `record` unexpanded
82028203 * (the template then renders the scalar id) — expansion must never break the
82038204 * flow it feeds.
82048205 */
@@ -8232,6 +8233,61 @@ export class AutomationEngine implements IAutomationService {
82328233 return this.executeKey(resolved.key, context, startTime);
82338234 }
82348235
8236+ /**
8237+ * The answer {@link executeKey} gives a run that fails while its values
8238+ * are seeded, before its first node: a value it was handed contains itself
8239+ * ({@link HeldValueCycleError}), so the run cannot take its own copy. The
8240+ * failed-run shape of `execute()`'s failure exit — `status: 'failed'`
8241+ * (the run was dispatched and failed), `error`, `durationMs`, `summary`,
8242+ * `flowLabel` — with one `failed` history row, and no `errorMessage`
8243+ * (there are no run variables to render the author's text over). Never
8244+ * retried: the same value fails the same way on every attempt.
8245+ */
8246+ private failRunBeforeFirstNode(run: {
8247+ runId: string; flowName: string; flow: FlowParsed; startedAt: string; startTime: number;
8248+ context: AutomationContext | undefined; error: string;
8249+ }): AutomationResult {
8250+ const { runId, flowName, flow, startedAt, context, error } = run;
8251+ const durationMs = Date.now() - run.startTime;
8252+ const steps: StepLogEntry[] = [];
8253+ let logged: ExecutionLogEntry | undefined;
8254+ try {
8255+ logged = this.recordLog({
8256+ id: runId,
8257+ flowName,
8258+ flowVersion: flow.version,
8259+ status: 'failed',
8260+ startedAt,
8261+ completedAt: new Date().toISOString(),
8262+ durationMs,
8263+ waitMs: 0,
8264+ trigger: buildRunTrigger(context),
8265+ steps,
8266+ error,
8267+ }, context);
8268+ } catch (bookkeeping) {
8269+ // DURABILITY, so `error`, as at `execute()`'s failure exit: the
8270+ // caller is told the run failed, and the history row is lost.
8271+ this.logger.error(
8272+ `[Automation] run '${runId}' of flow '${flowName}' FAILED before its first node and its run-history ` +
8273+ `bookkeeping threw, so its terminal 'failed' row never landed — nothing retries the write, and ` +
8274+ `this run is invisible to the Runs surfaces. The caller was answered status 'failed' carrying ` +
8275+ `the run's error, so nothing needs re-driving on account of this line. Fix the history failure ` +
8276+ `in this record's meta.`,
8277+ undefined,
8278+ describeThrownForLog(bookkeeping),
8279+ );
8280+ }
8281+ return {
8282+ success: false,
8283+ error,
8284+ durationMs,
8285+ status: 'failed',
8286+ flowLabel: flow.label,
8287+ summary: logged?.summary ?? summarizeRun(steps),
8288+ };
8289+ }
8290+
82358291 /**
82368292 * [#31696] {@link execute} for the flow filed under the slot key
82378293 * `flowName`, already resolved. Every record of the run is keyed by it.
@@ -8319,11 +8375,23 @@ export class AutomationEngine implements IAutomationService {
83198375 // and resolves the triggering user's real grants for `runAs:'user'` (#3356).
83208376 // Also stamps `flowRunId` so this run's data writes are attributable to it
83218377 // (#3456).
8322- const runContext = await this.resolveRunContext(flow, context, runId);
8323- // Seeded AFTER the context resolves: the run takes its own copy of the
8324- // trigger record, so the lookups the start node declares (`expand`,
8325- // grafted while the context resolves) must already be on it.
8326- const variables = this.seedRunVariables(flow, flowName, context, runId);
8378+ let runContext: AutomationContext;
8379+ let variables: Map<string, unknown>;
8380+ try {
8381+ runContext = await this.resolveRunContext(flow, context, runId);
8382+ // Seeded AFTER the context resolves, from the resolved context: its
8383+ // record is the run's own copy, carrying the lookups the start node
8384+ // declares (`expand`, grafted while the context resolves).
8385+ variables = this.seedRunVariables(flow, flowName, runContext, runId);
8386+ } catch (err) {
8387+ // A value the run is handed that contains itself (the record,
8388+ // `previous`) has no copy for the run to take: the run fails
8389+ // before its first node, answered in the failed-run shape below
8390+ // and recorded as such, never a throw out of `execute()`. Any
8391+ // other throw is unchanged.
8392+ if (!(err instanceof HeldValueCycleError)) throw err;
8393+ return this.failRunBeforeFirstNode({ runId, flowName, flow, startedAt, startTime, context, error: err.message });
8394+ }
83278395 stampFlowRunSlot(runContext, flowName, runPackage);
83288396 // [ADR-0162 item 7] The run's root run: itself, or its parent's.
83298397 stampRootRun(runContext, { runId, flowName, startedAt });
@@ -12140,7 +12208,7 @@ export class AutomationEngine implements IAutomationService {
1214012208 * create leg (#3427). Binding it only when truthy left `previous` an
1214112209 * unknown CEL variable on insert, so ANY reference to it (even
1214212210 * `previous == null`) threw "Unknown variable: previous" and failed the
12143- * whole condition;
12211+ * whole condition. Like the record, it is the run's OWN copy ;
1214412212 * 4. `$runId`, so a pausing node (e.g. Approval) can map its external
1214512213 * state back to this run for resume (ADR-0019), and `$flowName` /
1214612214 * `$flowLabel`, so externalized state carries a human-readable origin.
0 commit comments