|
| 1 | +import { randomUUID } from "node:crypto"; |
| 2 | + |
| 3 | +export const NODE_PENDING_WORK_TYPES = ["status.request", "location.request"] as const; |
| 4 | +export type NodePendingWorkType = (typeof NODE_PENDING_WORK_TYPES)[number]; |
| 5 | + |
| 6 | +export const NODE_PENDING_WORK_PRIORITIES = ["default", "normal", "high"] as const; |
| 7 | +export type NodePendingWorkPriority = (typeof NODE_PENDING_WORK_PRIORITIES)[number]; |
| 8 | + |
| 9 | +export type NodePendingWorkItem = { |
| 10 | + id: string; |
| 11 | + type: NodePendingWorkType; |
| 12 | + priority: NodePendingWorkPriority; |
| 13 | + createdAtMs: number; |
| 14 | + expiresAtMs: number | null; |
| 15 | + payload?: Record<string, unknown>; |
| 16 | +}; |
| 17 | + |
| 18 | +type NodePendingWorkState = { |
| 19 | + revision: number; |
| 20 | + itemsById: Map<string, NodePendingWorkItem>; |
| 21 | +}; |
| 22 | + |
| 23 | +type DrainOptions = { |
| 24 | + maxItems?: number; |
| 25 | + includeDefaultStatus?: boolean; |
| 26 | + nowMs?: number; |
| 27 | +}; |
| 28 | + |
| 29 | +type DrainResult = { |
| 30 | + revision: number; |
| 31 | + items: NodePendingWorkItem[]; |
| 32 | + hasMore: boolean; |
| 33 | +}; |
| 34 | + |
| 35 | +const DEFAULT_STATUS_ITEM_ID = "baseline-status"; |
| 36 | +const DEFAULT_STATUS_PRIORITY: NodePendingWorkPriority = "default"; |
| 37 | +const DEFAULT_PRIORITY: NodePendingWorkPriority = "normal"; |
| 38 | +const DEFAULT_MAX_ITEMS = 4; |
| 39 | +const MAX_ITEMS = 10; |
| 40 | +const PRIORITY_RANK: Record<NodePendingWorkPriority, number> = { |
| 41 | + high: 3, |
| 42 | + normal: 2, |
| 43 | + default: 1, |
| 44 | +}; |
| 45 | + |
| 46 | +const stateByNodeId = new Map<string, NodePendingWorkState>(); |
| 47 | + |
| 48 | +function getState(nodeId: string): NodePendingWorkState { |
| 49 | + let state = stateByNodeId.get(nodeId); |
| 50 | + if (!state) { |
| 51 | + state = { |
| 52 | + revision: 0, |
| 53 | + itemsById: new Map(), |
| 54 | + }; |
| 55 | + stateByNodeId.set(nodeId, state); |
| 56 | + } |
| 57 | + return state; |
| 58 | +} |
| 59 | + |
| 60 | +function pruneExpired(state: NodePendingWorkState, nowMs: number): boolean { |
| 61 | + let changed = false; |
| 62 | + for (const [id, item] of state.itemsById) { |
| 63 | + if (item.expiresAtMs !== null && item.expiresAtMs <= nowMs) { |
| 64 | + state.itemsById.delete(id); |
| 65 | + changed = true; |
| 66 | + } |
| 67 | + } |
| 68 | + if (changed) { |
| 69 | + state.revision += 1; |
| 70 | + } |
| 71 | + return changed; |
| 72 | +} |
| 73 | + |
| 74 | +function sortedItems(state: NodePendingWorkState): NodePendingWorkItem[] { |
| 75 | + return [...state.itemsById.values()].toSorted((a, b) => { |
| 76 | + const priorityDelta = PRIORITY_RANK[b.priority] - PRIORITY_RANK[a.priority]; |
| 77 | + if (priorityDelta !== 0) { |
| 78 | + return priorityDelta; |
| 79 | + } |
| 80 | + if (a.createdAtMs !== b.createdAtMs) { |
| 81 | + return a.createdAtMs - b.createdAtMs; |
| 82 | + } |
| 83 | + return a.id.localeCompare(b.id); |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +function makeBaselineStatusItem(nowMs: number): NodePendingWorkItem { |
| 88 | + return { |
| 89 | + id: DEFAULT_STATUS_ITEM_ID, |
| 90 | + type: "status.request", |
| 91 | + priority: DEFAULT_STATUS_PRIORITY, |
| 92 | + createdAtMs: nowMs, |
| 93 | + expiresAtMs: null, |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +export function enqueueNodePendingWork(params: { |
| 98 | + nodeId: string; |
| 99 | + type: NodePendingWorkType; |
| 100 | + priority?: NodePendingWorkPriority; |
| 101 | + expiresInMs?: number; |
| 102 | + payload?: Record<string, unknown>; |
| 103 | +}): { revision: number; item: NodePendingWorkItem; deduped: boolean } { |
| 104 | + const nodeId = params.nodeId.trim(); |
| 105 | + if (!nodeId) { |
| 106 | + throw new Error("nodeId required"); |
| 107 | + } |
| 108 | + const nowMs = Date.now(); |
| 109 | + const state = getState(nodeId); |
| 110 | + pruneExpired(state, nowMs); |
| 111 | + const existing = [...state.itemsById.values()].find((item) => item.type === params.type); |
| 112 | + if (existing) { |
| 113 | + return { revision: state.revision, item: existing, deduped: true }; |
| 114 | + } |
| 115 | + const item: NodePendingWorkItem = { |
| 116 | + id: randomUUID(), |
| 117 | + type: params.type, |
| 118 | + priority: params.priority ?? DEFAULT_PRIORITY, |
| 119 | + createdAtMs: nowMs, |
| 120 | + expiresAtMs: |
| 121 | + typeof params.expiresInMs === "number" && Number.isFinite(params.expiresInMs) |
| 122 | + ? nowMs + Math.max(1_000, Math.trunc(params.expiresInMs)) |
| 123 | + : null, |
| 124 | + ...(params.payload ? { payload: params.payload } : {}), |
| 125 | + }; |
| 126 | + state.itemsById.set(item.id, item); |
| 127 | + state.revision += 1; |
| 128 | + return { revision: state.revision, item, deduped: false }; |
| 129 | +} |
| 130 | + |
| 131 | +export function drainNodePendingWork(nodeId: string, opts: DrainOptions = {}): DrainResult { |
| 132 | + const normalizedNodeId = nodeId.trim(); |
| 133 | + if (!normalizedNodeId) { |
| 134 | + return { revision: 0, items: [], hasMore: false }; |
| 135 | + } |
| 136 | + const nowMs = opts.nowMs ?? Date.now(); |
| 137 | + const state = getState(normalizedNodeId); |
| 138 | + pruneExpired(state, nowMs); |
| 139 | + const maxItems = Math.min(MAX_ITEMS, Math.max(1, Math.trunc(opts.maxItems ?? DEFAULT_MAX_ITEMS))); |
| 140 | + const explicitItems = sortedItems(state); |
| 141 | + const items = explicitItems.slice(0, maxItems); |
| 142 | + const hasExplicitStatus = explicitItems.some((item) => item.type === "status.request"); |
| 143 | + const includeBaseline = opts.includeDefaultStatus !== false && !hasExplicitStatus; |
| 144 | + if (includeBaseline && items.length < maxItems) { |
| 145 | + items.push(makeBaselineStatusItem(nowMs)); |
| 146 | + } |
| 147 | + return { |
| 148 | + revision: state.revision, |
| 149 | + items, |
| 150 | + hasMore: |
| 151 | + explicitItems.length > items.filter((item) => item.id !== DEFAULT_STATUS_ITEM_ID).length, |
| 152 | + }; |
| 153 | +} |
| 154 | + |
| 155 | +export function acknowledgeNodePendingWork(params: { nodeId: string; itemIds: string[] }): { |
| 156 | + revision: number; |
| 157 | + removedItemIds: string[]; |
| 158 | +} { |
| 159 | + const nodeId = params.nodeId.trim(); |
| 160 | + if (!nodeId) { |
| 161 | + return { revision: 0, removedItemIds: [] }; |
| 162 | + } |
| 163 | + const state = getState(nodeId); |
| 164 | + const removedItemIds: string[] = []; |
| 165 | + for (const itemId of params.itemIds) { |
| 166 | + const trimmedId = itemId.trim(); |
| 167 | + if (!trimmedId || trimmedId === DEFAULT_STATUS_ITEM_ID) { |
| 168 | + continue; |
| 169 | + } |
| 170 | + if (state.itemsById.delete(trimmedId)) { |
| 171 | + removedItemIds.push(trimmedId); |
| 172 | + } |
| 173 | + } |
| 174 | + if (removedItemIds.length > 0) { |
| 175 | + state.revision += 1; |
| 176 | + } |
| 177 | + return { revision: state.revision, removedItemIds }; |
| 178 | +} |
| 179 | + |
| 180 | +export function resetNodePendingWorkForTests() { |
| 181 | + stateByNodeId.clear(); |
| 182 | +} |
0 commit comments