Commit 68287d06b7

68287d06b71b8d077e4be842459fc3f485c86d88

parent: ac4279f4d2

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-24 06:18 UTC

Make the external data cache typed

execute was generic over an enum of every payload kind and handed each caller a
closure to pull its own case back out. When the two disagreed it called
fatalError. It is generic over the value now: the payload enum and the three
extract closures are gone, operations return their value directly, and the live
path runs a typed Task and returns its result with no cast — so the mismatch
cannot arise rather than merely being unlikely. A cached or in-flight value of
the wrong type falls through to a fetch instead of ending the process.

LookupRuntime carries the identical trap with fourteen callers. It is the
inspection cache — provenance, cache-source attribution, in-flight coalescing —
and it has no tests, because that is W2's unfinished half. Converting it blind
is how the bug this prevents gets introduced, so C4 stays half open until those
tests exist.

Layout: unified · split

DomainDig/ExternalDataService.swift +20 −37
@@ -23,14 +23,8 @@ actor ExternalDataService {
2323 }
2424 }
2525
26 private enum CachedPayload {
27 case ownershipHistory(ServiceResult<[DomainOwnershipHistoryEvent]>)
28 case dnsHistory(ServiceResult<[DNSHistoryEvent]>)
29 case extendedSubdomains(ServiceResult<[DiscoveredSubdomain]>)
30 }
31
3226 private struct CacheEntry {
33 let payload: CachedPayload
27 let value: any Sendable
3428 let expiresAt: Date
3529 }
3630
@@ -43,7 +37,7 @@ actor ExternalDataService {
4337 private let ttl: TimeInterval = 900
4438 private let session: URLSession = .shared
4539 private var cache: [RequestKey: CacheEntry] = [:]
46 private var inFlight: [RequestKey: Task<CachedPayload, Never>] = [:]
40 private var inFlight: [RequestKey: Task<any Sendable, Never>] = [:]
4741 private var nextAllowedAt: [RateLimitBucket: Date] = [:]
4842
4943 func clearCache() {
@@ -62,10 +56,6 @@ actor ExternalDataService {
6256 return await execute(
6357 key: .ownershipHistory(normalizedDomain),
6458 rateLimitBucket: .history,
65 extract: { payload in
66 guard case let .ownershipHistory(result) = payload else { return nil }
67 return result
68 },
6959 operation: { [configuration = configuration()] in
7060 let localEvents = Self.localOwnershipHistory(
7161 domain: normalizedDomain,
@@ -73,7 +63,7 @@ actor ExternalDataService {
7363 historyEntries: historyEntries
7464 )
7565 let externalEvents = await self.fetchOwnershipHistory(domain: normalizedDomain, configuration: configuration)
76 return .ownershipHistory(Self.mergeOwnershipHistory(localEvents: localEvents, externalEvents: externalEvents))
66 return Self.mergeOwnershipHistory(localEvents: localEvents, externalEvents: externalEvents)
7767 }
7868 )
7969 }
@@ -87,10 +77,6 @@ actor ExternalDataService {
8777 return await execute(
8878 key: .dnsHistory(normalizedDomain),
8979 rateLimitBucket: .history,
90 extract: { payload in
91 guard case let .dnsHistory(result) = payload else { return nil }
92 return result
93 },
9480 operation: { [configuration = configuration()] in
9581 let localEvents = Self.localDNSHistory(
9682 domain: normalizedDomain,
@@ -98,7 +84,7 @@ actor ExternalDataService {
9884 historyEntries: historyEntries
9985 )
10086 let externalEvents = await self.fetchDNSHistory(domain: normalizedDomain, configuration: configuration)
101 return .dnsHistory(Self.mergeDNSHistory(localEvents: localEvents, externalEvents: externalEvents))
87 return Self.mergeDNSHistory(localEvents: localEvents, externalEvents: externalEvents)
10288 }
10389 )
10490 }
@@ -111,10 +97,6 @@ actor ExternalDataService {
11197 return await execute(
11298 key: .extendedSubdomains(normalizedDomain),
11399 rateLimitBucket: .subdomains,
114 extract: { payload in
115 guard case let .extendedSubdomains(result) = payload else { return nil }
116 return result
117 },
118100 operation: { [configuration = configuration()] in
119101 var merged = existing
120102 switch await SubdomainDiscoveryService.discover(for: normalizedDomain, limit: 100) {
@@ -131,43 +113,44 @@ actor ExternalDataService {
131113 }
132114
133115 if merged.count <= existing.count {
134 return .extendedSubdomains(.empty("No extended subdomains available"))
116 return .empty("No extended subdomains available")
135117 }
136118
137119 let onlyExtended = merged.filter(\.isExtended)
138 return .extendedSubdomains(.success(onlyExtended.sorted { $0.hostname < $1.hostname }))
120 return .success(onlyExtended.sorted { $0.hostname < $1.hostname })
139121 }
140122 )
141123 }
142124
143 private func execute<T>(
125 /// Generic over the value rather than over an enum of every payload kind.
126 /// The previous shape stored that enum and handed each caller a closure to
127 /// pull its own case back out, which trapped when the two disagreed; the
128 /// live path here has no cast at all, so the mismatch cannot arise. A
129 /// cached or in-flight value of the wrong type falls through to a fetch
130 /// instead of ending the process.
131 private func execute<T: Sendable>(
144132 key: RequestKey,
145133 rateLimitBucket: RateLimitBucket,
146 extract: @escaping (CachedPayload) -> T?,
147 operation: @escaping @Sendable () async -> CachedPayload
134 operation: @escaping @Sendable () async -> T
148135 ) async -> CachedLookupResult<T> {
149 if let cachedEntry = cache[key], cachedEntry.expiresAt > Date(), let value = extract(cachedEntry.payload) {
136 if let cachedEntry = cache[key], cachedEntry.expiresAt > Date(), let value = cachedEntry.value as? T {
150137 return CachedLookupResult(value: value, source: .cached)
151138 }
152139
153 if let task = inFlight[key], let value = extract(await task.value) {
140 if let task = inFlight[key], let value = await task.value as? T {
154141 return CachedLookupResult(value: value, source: .mixed)
155142 }
156143
157 let task = Task<CachedPayload, Never> {
144 let task = Task<T, Never> {
158145 await self.enforceRateLimit(for: rateLimitBucket)
159146 return await operation()
160147 }
161 inFlight[key] = task
148 inFlight[key] = Task { await task.value as any Sendable }
162149
163 let payload = await task.value
164 cache[key] = CacheEntry(payload: payload, expiresAt: Date().addingTimeInterval(ttl))
150 let value = await task.value
151 cache[key] = CacheEntry(value: value, expiresAt: Date().addingTimeInterval(ttl))
165152 inFlight[key] = nil
166153
167 guard let value = extract(payload) else {
168 fatalError("ExternalDataService payload extraction mismatch")
169 }
170
171154 return CachedLookupResult(value: value, source: .live)
172155 }
173156