import { isRuntimeExecutionCapability, type RuntimeExecutionCapability, } from './execution-capabilities'; export type RunExecutionPlacementKind = 'root' | 'inline_child'; export type RunExecutionAuthorityInput = { readonly orgId: string; readonly workflowId?: string; readonly billingRunId?: string | null; readonly maxCreditsPerRun?: number | null; readonly integrationMode?: 'live' | 'eval_stub' | 'fixture' | null; readonly synthetic?: boolean; readonly capabilities: readonly RuntimeExecutionCapability[]; }; export type RunExecutionScope = Readonly<{ logical: Readonly<{ runId: string; playId: string; parentRunId: string | null; parentPlayId: string | null; rootRunId: string; rootPlayId: string; }>; receipt: Readonly<{ ownerRunId: string; ownerAttempt: number; namespace: string; }>; placement: Readonly<{ kind: RunExecutionPlacementKind; executorRunId: string; executorPlayId: string; }>; /** Persistable runtime authority facts. Bearer tokens and secret names do not belong here. */ authority: Readonly<{ orgId: string; workflowId: string; runId: string; playId: string; billingRunId: string | null; maxCreditsPerRun: number | null; integrationMode: 'live' | 'eval_stub' | 'fixture' | null; synthetic: boolean; capabilities: readonly RuntimeExecutionCapability[]; }>; }>; export type RootRunExecutionScopeInput = { readonly runId: string; readonly playId: string; readonly attempt: number; readonly receiptNamespace: string; readonly authority: RunExecutionAuthorityInput; }; export type ChildRunExecutionScopeInput = { readonly placement: 'inline'; readonly runId: string; readonly playId: string; readonly receiptNamespace: string; }; function requireIdentity(value: string, field: string): string { if ( typeof value !== 'string' || value.length === 0 || value !== value.trim() ) { throw new Error(`${field} must be a non-empty, trimmed string.`); } return value; } function requireAttempt(value: number, field: string): number { if (!Number.isSafeInteger(value) || value < 0) { throw new Error(`${field} must be a non-negative safe integer.`); } return value; } function requireMaxCredits(value: number | null | undefined): number | null { if (value == null) return null; if (!Number.isFinite(value) || value < 0) { throw new Error( 'authority.maxCreditsPerRun must be a non-negative number.', ); } return value; } function requireCapabilities( values: readonly RuntimeExecutionCapability[], ): readonly RuntimeExecutionCapability[] { if (!Array.isArray(values) || values.length === 0) { throw new Error( 'authority.capabilities must contain at least one capability.', ); } const capabilities = [...new Set(values)]; if (!capabilities.every(isRuntimeExecutionCapability)) { throw new Error('authority.capabilities contains an unknown capability.'); } return Object.freeze(capabilities.sort()); } function authorityMetadata(input: { runId: string; playId: string; orgId: string; authority: Omit; }): RunExecutionScope['authority'] { return Object.freeze({ orgId: requireIdentity(input.orgId, 'authority.orgId'), workflowId: requireIdentity( input.authority.workflowId ?? input.runId, 'authority.workflowId', ), runId: input.runId, playId: input.playId, billingRunId: input.authority.billingRunId == null ? null : requireIdentity( input.authority.billingRunId, 'authority.billingRunId', ), maxCreditsPerRun: requireMaxCredits(input.authority.maxCreditsPerRun), integrationMode: input.authority.integrationMode ?? null, synthetic: input.authority.synthetic === true, capabilities: requireCapabilities(input.authority.capabilities), }); } function freezeScope(input: RunExecutionScope): RunExecutionScope { return Object.freeze({ logical: Object.freeze(input.logical), receipt: Object.freeze(input.receipt), placement: Object.freeze(input.placement), authority: input.authority, }); } export function createRootRunExecutionScope( input: RootRunExecutionScopeInput, ): RunExecutionScope { const runId = requireIdentity(input.runId, 'runId'); const playId = requireIdentity(input.playId, 'playId'); const attempt = requireAttempt(input.attempt, 'attempt'); const namespace = requireIdentity(input.receiptNamespace, 'receiptNamespace'); return freezeScope({ logical: { runId, playId, parentRunId: null, parentPlayId: null, rootRunId: runId, rootPlayId: playId, }, receipt: { ownerRunId: runId, ownerAttempt: attempt, namespace }, placement: { kind: 'root', executorRunId: runId, executorPlayId: playId }, authority: authorityMetadata({ runId, playId, orgId: input.authority.orgId, authority: input.authority, }), }); } export function deriveChildRunExecutionScope( parent: RunExecutionScope, input: ChildRunExecutionScopeInput, ): RunExecutionScope { const runId = requireIdentity(input.runId, 'runId'); const playId = requireIdentity(input.playId, 'playId'); const namespace = requireIdentity(input.receiptNamespace, 'receiptNamespace'); if (runId === parent.logical.runId) { throw new Error('A child runId must differ from its parent runId.'); } const logical: RunExecutionScope['logical'] = { runId, playId, parentRunId: parent.logical.runId, parentPlayId: parent.logical.playId, rootRunId: parent.logical.rootRunId, rootPlayId: parent.logical.rootPlayId, }; return freezeScope({ logical, receipt: { ownerRunId: parent.receipt.ownerRunId, ownerAttempt: parent.receipt.ownerAttempt, namespace, }, placement: { kind: 'inline_child', executorRunId: parent.placement.executorRunId, executorPlayId: parent.placement.executorPlayId, }, authority: parent.authority, }); }