interface LeaseSnapshot { activeLeaseId: string | null; activeTaskId: string | null; activeTaskIds: string[]; activeCount: number; attemptId: string | null; startedAt: number | null; leaseExpiresAt: number | null; lastExtendedAt: number | null; lastReleasedAt: number | null; lastOutcome: "leased" | "extended" | "completed" | "failed" | "cleared" | null; } interface ActiveLeaseState { taskId: string; attemptId: string | null; leaseExpiresAt: number | null; startedAt: number; lastExtendedAt: number | null; } const activeLeases = new Map(); const leaseState: LeaseSnapshot = { activeLeaseId: null, activeTaskId: null, activeTaskIds: [], activeCount: 0, attemptId: null, startedAt: null, leaseExpiresAt: null, lastExtendedAt: null, lastReleasedAt: null, lastOutcome: null, }; function normalizeLeaseExpiresAt(value: number | string | null | undefined): number | null { if (typeof value === "number") { return Number.isFinite(value) ? value : null; } if (typeof value !== "string" || !value.trim()) { return null; } const timestamp = Date.parse(value); return Number.isNaN(timestamp) ? null : timestamp; } export function setActiveLease(input: { taskId: string; attemptId?: string | null; leaseExpiresAt?: number | string | null; } | null): void { if (!input) { activeLeases.clear(); leaseState.activeLeaseId = null; leaseState.activeTaskId = null; leaseState.activeTaskIds = []; leaseState.activeCount = 0; leaseState.attemptId = null; leaseState.startedAt = null; leaseState.leaseExpiresAt = null; leaseState.lastOutcome = "cleared"; leaseState.lastReleasedAt = Date.now(); return; } const normalized: ActiveLeaseState = { taskId: input.taskId, attemptId: input.attemptId ?? null, leaseExpiresAt: normalizeLeaseExpiresAt(input.leaseExpiresAt), startedAt: Date.now(), lastExtendedAt: null, }; activeLeases.set(normalized.taskId, normalized); syncLeaseSnapshot(); leaseState.lastOutcome = "leased"; } export function noteLeaseExtended( leaseExpiresAt?: number | string | null, taskId?: string, ): void { if (taskId) { const active = activeLeases.get(taskId); if (active) { active.leaseExpiresAt = normalizeLeaseExpiresAt(leaseExpiresAt) ?? active.leaseExpiresAt; active.lastExtendedAt = Date.now(); activeLeases.set(taskId, active); } } else { const first = activeLeases.values().next().value as ActiveLeaseState | undefined; if (first) { first.leaseExpiresAt = normalizeLeaseExpiresAt(leaseExpiresAt) ?? first.leaseExpiresAt; first.lastExtendedAt = Date.now(); activeLeases.set(first.taskId, first); } } syncLeaseSnapshot(); leaseState.lastExtendedAt = Date.now(); leaseState.lastOutcome = "extended"; } export function noteLeaseReleased( reason: Exclude, taskId?: string, ): void { if (taskId) { activeLeases.delete(taskId); } else { activeLeases.clear(); } syncLeaseSnapshot(); leaseState.lastReleasedAt = Date.now(); leaseState.lastOutcome = reason; } export function getLeaseManagerSnapshot(): LeaseSnapshot { return { ...leaseState }; } function syncLeaseSnapshot(): void { const first = activeLeases.values().next().value as ActiveLeaseState | undefined; leaseState.activeTaskIds = [...activeLeases.keys()]; leaseState.activeCount = activeLeases.size; leaseState.activeLeaseId = first?.taskId ?? null; leaseState.activeTaskId = first?.taskId ?? null; leaseState.attemptId = first?.attemptId ?? null; leaseState.startedAt = first?.startedAt ?? null; leaseState.leaseExpiresAt = first?.leaseExpiresAt ?? null; }