|
| 1 | +import { createDeferredCore, type Deferred } from "../shared/deferred.js"; |
| 2 | +import { PreparedModelRuntimePublicationSupersededError } from "./prepared-model-runtime.errors.js"; |
| 3 | +import { ownerKey, resolveConfiguredOwner } from "./prepared-model-runtime.owner.js"; |
| 4 | +import type { |
| 5 | + PreparedModelRuntimeOwner, |
| 6 | + PreparedModelRuntimeReplacementGateId, |
| 7 | + PreparedModelRuntimeSnapshot, |
| 8 | +} from "./prepared-model-runtime.types.js"; |
| 9 | + |
| 10 | +export type PreparedModelRuntimeAuthMutation = { |
| 11 | + agentDir?: string; |
| 12 | + affectsInheritedStores: boolean; |
| 13 | +}; |
| 14 | + |
| 15 | +type PreparedModelRuntimeAuthTransaction = { |
| 16 | + adoptedBy?: PreparedModelRuntimeReplacementGateId; |
| 17 | + ownerGates: Map<PreparedModelRuntimeOwner, Deferred<PreparedModelRuntimeSnapshot>>; |
| 18 | + publicationQueued: boolean; |
| 19 | +}; |
| 20 | + |
| 21 | +function partitionAuthMutationOwners( |
| 22 | + mutations: readonly (readonly PreparedModelRuntimeOwner[])[], |
| 23 | +): PreparedModelRuntimeOwner[][] { |
| 24 | + // Overlapping owner identities share one atomic failure boundary. Independent components |
| 25 | + // retain FIFO order so one scoped credential failure cannot poison an unrelated owner. |
| 26 | + const components: Set<PreparedModelRuntimeOwner>[] = []; |
| 27 | + for (const invalidatedOwners of mutations) { |
| 28 | + const target = new Set(invalidatedOwners); |
| 29 | + let insertAt = components.length; |
| 30 | + for (let index = components.length - 1; index >= 0; index -= 1) { |
| 31 | + if (![...target].some((owner) => components[index]!.has(owner))) { |
| 32 | + continue; |
| 33 | + } |
| 34 | + insertAt = index; |
| 35 | + for (const owner of components[index]!) { |
| 36 | + target.add(owner); |
| 37 | + } |
| 38 | + components.splice(index, 1); |
| 39 | + } |
| 40 | + components.splice(insertAt, 0, target); |
| 41 | + } |
| 42 | + return components.map((component) => Array.from(component)); |
| 43 | +} |
| 44 | + |
| 45 | +export class PreparedModelRuntimeAuthPublicationOwner { |
| 46 | + readonly #events: (readonly PreparedModelRuntimeOwner[])[] = []; |
| 47 | + #transaction: PreparedModelRuntimeAuthTransaction | undefined; |
| 48 | + |
| 49 | + enqueue( |
| 50 | + invalidatedOwners: readonly PreparedModelRuntimeOwner[], |
| 51 | + ): PreparedModelRuntimeAuthTransaction { |
| 52 | + this.#events.push([...invalidatedOwners]); |
| 53 | + const transaction = |
| 54 | + this.#transaction ?? |
| 55 | + (this.#transaction = { |
| 56 | + ownerGates: new Map(), |
| 57 | + publicationQueued: false, |
| 58 | + }); |
| 59 | + for (const owner of invalidatedOwners) { |
| 60 | + let gate = transaction.ownerGates.get(owner); |
| 61 | + if (!gate) { |
| 62 | + gate = createDeferredCore<PreparedModelRuntimeSnapshot>(); |
| 63 | + transaction.ownerGates.set(owner, gate); |
| 64 | + void gate.promise.catch(() => undefined); |
| 65 | + } |
| 66 | + owner.pending = gate.promise; |
| 67 | + } |
| 68 | + return transaction; |
| 69 | + } |
| 70 | + |
| 71 | + claimPublication(transaction: PreparedModelRuntimeAuthTransaction): boolean { |
| 72 | + if (transaction.publicationQueued) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + transaction.publicationQueued = true; |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + isCurrent(transaction: PreparedModelRuntimeAuthTransaction): boolean { |
| 80 | + return this.#transaction === transaction; |
| 81 | + } |
| 82 | + |
| 83 | + adopt(gateId: PreparedModelRuntimeReplacementGateId): void { |
| 84 | + if (this.#transaction) { |
| 85 | + this.#transaction.adoptedBy = gateId; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + adoptTransaction( |
| 90 | + transaction: PreparedModelRuntimeAuthTransaction, |
| 91 | + gateId: PreparedModelRuntimeReplacementGateId, |
| 92 | + ): void { |
| 93 | + if (this.#transaction === transaction) { |
| 94 | + transaction.adoptedBy = gateId; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + prepareAdoptedCommit( |
| 99 | + gateId: PreparedModelRuntimeReplacementGateId, |
| 100 | + ): PreparedModelRuntimeAuthTransaction | undefined { |
| 101 | + const transaction = this.#transaction; |
| 102 | + if (transaction?.adoptedBy !== gateId) { |
| 103 | + return undefined; |
| 104 | + } |
| 105 | + this.clearOwnerGates(transaction); |
| 106 | + return transaction; |
| 107 | + } |
| 108 | + |
| 109 | + resolve( |
| 110 | + transaction: PreparedModelRuntimeAuthTransaction, |
| 111 | + owners: Map<string, PreparedModelRuntimeOwner>, |
| 112 | + ): boolean { |
| 113 | + if (this.#transaction !== transaction) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + if (transaction.adoptedBy) { |
| 117 | + this.#transaction = undefined; |
| 118 | + } else if (transaction.ownerGates.size === 0) { |
| 119 | + this.#transaction = undefined; |
| 120 | + return true; |
| 121 | + } else { |
| 122 | + return false; |
| 123 | + } |
| 124 | + this.clearOwnerGates(transaction); |
| 125 | + for (const [owner, gate] of transaction.ownerGates) { |
| 126 | + const published = |
| 127 | + owners.get(ownerKey(owner.input)) ?? resolveConfiguredOwner(owners, owner.input); |
| 128 | + if (published?.snapshot && !published.needsRefresh && !published.pending) { |
| 129 | + gate.resolve(published.snapshot); |
| 130 | + } else { |
| 131 | + gate.reject( |
| 132 | + new PreparedModelRuntimePublicationSupersededError( |
| 133 | + `prepared model runtime publication was superseded for ${owner.input.agentDir}`, |
| 134 | + ), |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + settleComponent( |
| 142 | + transaction: PreparedModelRuntimeAuthTransaction, |
| 143 | + componentOwners: readonly PreparedModelRuntimeOwner[], |
| 144 | + owners: Map<string, PreparedModelRuntimeOwner>, |
| 145 | + publishOwners: (owners: readonly PreparedModelRuntimeOwner[]) => void, |
| 146 | + ): void { |
| 147 | + if (this.#transaction !== transaction || transaction.adoptedBy) { |
| 148 | + return; |
| 149 | + } |
| 150 | + const queuedOwners = new Set(this.#events.flat()); |
| 151 | + const unsettled = componentOwners.flatMap((owner) => { |
| 152 | + const gate = transaction.ownerGates.get(owner); |
| 153 | + return gate && !queuedOwners.has(owner) ? [{ owner, gate }] : []; |
| 154 | + }); |
| 155 | + const completed = unsettled.flatMap(({ owner, gate }) => |
| 156 | + owner.pending === gate.promise && |
| 157 | + owners.get(ownerKey(owner.input)) === owner && |
| 158 | + owner.snapshot && |
| 159 | + !owner.needsRefresh |
| 160 | + ? [{ owner, gate, snapshot: owner.snapshot }] |
| 161 | + : [], |
| 162 | + ); |
| 163 | + // Dispatch projection must become visible before exact-gate waiters resume. |
| 164 | + for (const { owner } of completed) { |
| 165 | + owner.pending = undefined; |
| 166 | + } |
| 167 | + try { |
| 168 | + if (completed.length > 0) { |
| 169 | + publishOwners(completed.map(({ owner }) => owner)); |
| 170 | + } |
| 171 | + } catch (error) { |
| 172 | + for (const { owner, gate } of completed) { |
| 173 | + if (transaction.ownerGates.get(owner) === gate && owner.pending === undefined) { |
| 174 | + owner.pending = gate.promise; |
| 175 | + } |
| 176 | + } |
| 177 | + throw error; |
| 178 | + } |
| 179 | + for (const { owner, gate, snapshot } of completed) { |
| 180 | + transaction.ownerGates.delete(owner); |
| 181 | + gate.resolve(snapshot); |
| 182 | + } |
| 183 | + this.rejectComponentOwners( |
| 184 | + transaction, |
| 185 | + componentOwners, |
| 186 | + new PreparedModelRuntimePublicationSupersededError( |
| 187 | + "prepared model runtime auth publication owner was superseded", |
| 188 | + ), |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + reject(transaction: PreparedModelRuntimeAuthTransaction, error: Error): void { |
| 193 | + if (this.#transaction === transaction) { |
| 194 | + this.#transaction = undefined; |
| 195 | + } |
| 196 | + this.clearOwnerGates(transaction); |
| 197 | + for (const gate of transaction.ownerGates.values()) { |
| 198 | + gate.reject(error); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + rejectAdopted(gateId: PreparedModelRuntimeReplacementGateId, error: Error): void { |
| 203 | + if (this.#transaction?.adoptedBy === gateId) { |
| 204 | + this.reject(this.#transaction, error); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + async drain(params: { |
| 209 | + owners: Map<string, PreparedModelRuntimeOwner>; |
| 210 | + publish: ( |
| 211 | + entries: Array<{ |
| 212 | + owner: PreparedModelRuntimeOwner; |
| 213 | + input: PreparedModelRuntimeOwner["input"]; |
| 214 | + }>, |
| 215 | + ) => Promise<void>; |
| 216 | + publishOwners: (owners: readonly PreparedModelRuntimeOwner[]) => void; |
| 217 | + commit?: () => void; |
| 218 | + onOwnerFailure?: (error: unknown) => void; |
| 219 | + }): Promise<void> { |
| 220 | + while (this.#events.length > 0) { |
| 221 | + const components = partitionAuthMutationOwners(this.#events.splice(0)); |
| 222 | + for (const componentOwners of components) { |
| 223 | + const entries = componentOwners.flatMap((owner) => |
| 224 | + params.owners.get(ownerKey(owner.input)) === owner ? [{ owner, input: owner.input }] : [], |
| 225 | + ); |
| 226 | + try { |
| 227 | + if (entries.length > 0) { |
| 228 | + await params.publish(entries); |
| 229 | + } |
| 230 | + const transaction = this.#transaction; |
| 231 | + if (transaction) { |
| 232 | + this.settleComponent(transaction, componentOwners, params.owners, params.publishOwners); |
| 233 | + } |
| 234 | + } catch (error) { |
| 235 | + if (this.#transaction?.adoptedBy) { |
| 236 | + // The replacement transaction exclusively settles adopted gates from its own result. |
| 237 | + throw error; |
| 238 | + } |
| 239 | + const transaction = this.#transaction; |
| 240 | + if (transaction && this.rejectComponentOwners(transaction, componentOwners, error) > 0) { |
| 241 | + params.onOwnerFailure?.(error); |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + // The queue check and commit share one synchronous section so no mutation can be orphaned. |
| 247 | + params.commit?.(); |
| 248 | + } |
| 249 | + |
| 250 | + reset(error: Error): void { |
| 251 | + if (this.#transaction) { |
| 252 | + this.reject(this.#transaction, error); |
| 253 | + } |
| 254 | + this.#events.length = 0; |
| 255 | + } |
| 256 | + |
| 257 | + private clearOwnerGates(transaction: PreparedModelRuntimeAuthTransaction): void { |
| 258 | + for (const [owner, gate] of transaction.ownerGates) { |
| 259 | + if (owner.pending === gate.promise) { |
| 260 | + owner.pending = undefined; |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + private rejectComponentOwners( |
| 266 | + transaction: PreparedModelRuntimeAuthTransaction, |
| 267 | + componentOwners: readonly PreparedModelRuntimeOwner[], |
| 268 | + error: unknown, |
| 269 | + ): number { |
| 270 | + const queuedOwners = new Set(this.#events.flat()); |
| 271 | + let rejected = 0; |
| 272 | + for (const owner of componentOwners) { |
| 273 | + if (queuedOwners.has(owner)) { |
| 274 | + continue; |
| 275 | + } |
| 276 | + const gate = transaction.ownerGates.get(owner); |
| 277 | + if (!gate) { |
| 278 | + continue; |
| 279 | + } |
| 280 | + if (owner.pending === gate.promise) { |
| 281 | + owner.pending = undefined; |
| 282 | + } |
| 283 | + transaction.ownerGates.delete(owner); |
| 284 | + gate.reject(error); |
| 285 | + rejected += 1; |
| 286 | + } |
| 287 | + return rejected; |
| 288 | + } |
| 289 | +} |
0 commit comments