162abdc97c
- Add Prettier 3 with prettier-plugin-organize-imports (sorts/removes unused imports) - Add ESLint 10 flat config with typescript-eslint + eslint-plugin-vue + eslint-config-prettier - Add root scripts: format, format:check, lint, lint:fix - Reformat 257 files across admin-web, ops-web, desktop-client, packages - Remove unused locals/exports flagged by --noUnusedLocals/--noUnusedParameters - Fix duplicate localTabLabel key, surrogate-pair regex u-flag, NBSP literal in regex - Skip server/ (Go) and apps/browser-extension/ (deprecated per ADR) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
135 lines
3.6 KiB
TypeScript
135 lines
3.6 KiB
TypeScript
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<string, ActiveLeaseState>()
|
|
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<LeaseSnapshot['lastOutcome'], 'leased' | 'extended' | null>,
|
|
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
|
|
}
|