Repository navigation
irx: ship the credential-lifecycle journal to Axiom #15446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
azooz2003-bit
wants to merge
11
commits into
main
Choose a base branch
from
feat-transport-journal-axiom
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
675a92a
irx: journal every silent exit in the credential renewal pipeline
azooz2003-bit cca777a
irx: ship the credential-lifecycle journal to Axiom
azooz2003-bit cd4c1ea
irx: drop journal batches a backend without the transport route rejects
azooz2003-bit be6ba12
irx: move renewal health ownership into control service
azooz2003-bit e12e9a9
irx: split transport journal parsing helpers
azooz2003-bit 8fe0b52
irx: move renewal health ownership into control service
azooz2003-bit f796278
Merge remote-tracking branch 'origin/feat-irx-renewal-observability' …
azooz2003-bit 3772887
irx: anchor snapshot apply watchdog
azooz2003-bit 9605458
test: make watchdog regression deterministic
azooz2003-bit 66c8273
fix: address observability review findings
azooz2003-bit ee52eee
fix: stop journal uploads during shutdown
azooz2003-bit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 227 additions & 0 deletions
227
Packages/Shared/CmuxIrxTransport/Sources/CmuxIrxTransport/IrxJournalUploader.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| public import Foundation | ||
|
|
||
| /// Ships the credential-lifecycle slice of the transport journal to the | ||
| /// authenticated `/api/observability/transport` route, so wedge evidence | ||
| /// survives the Mac's short unified-log retention. Data-plane chatter never | ||
| /// leaves the device: only allowlisted components pass, minus an explicit | ||
| /// event denylist for the periodic ones. | ||
| public actor IrxJournalUploader { | ||
| public struct ClientMetadata: Sendable { | ||
| public var platform: String | ||
| public var clientChannel: String | ||
| public var appVersion: String? | ||
| public var buildNumber: String? | ||
| public var bundleIdentifier: String? | ||
| public var osVersion: String? | ||
| /// 12-hex endpoint prefix; matches client journal and server sink rows. | ||
| public var endpoint: String? | ||
| public var deviceId: String? | ||
| public var buildTag: String? | ||
|
|
||
| public init( | ||
| platform: String, clientChannel: String, appVersion: String? = nil, | ||
| buildNumber: String? = nil, bundleIdentifier: String? = nil, osVersion: String? = nil, | ||
| endpoint: String? = nil, deviceId: String? = nil, buildTag: String? = nil | ||
| ) { | ||
| self.platform = platform | ||
| self.clientChannel = clientChannel | ||
| self.appVersion = appVersion | ||
| self.buildNumber = buildNumber | ||
| self.bundleIdentifier = bundleIdentifier | ||
| self.osVersion = osVersion | ||
| self.endpoint = endpoint | ||
| self.deviceId = deviceId | ||
| self.buildTag = buildTag | ||
| } | ||
| } | ||
|
|
||
| /// Components worth durable retention: the credential-renewal pipeline, | ||
| /// connection lifecycle, and admission. Everything else stays local. | ||
| public static let exportedComponents: Set<String> = [ | ||
| "v2-control", "v2-host", "v2-lifecycle", "endpoint", "admission", | ||
| "host-runtime", "engine", "broker", "control-plane", "legacy-dialect", | ||
| "connection", "client-runtime", "registry", "host-events", "device-list", | ||
| ] | ||
| /// Periodic events inside exported components that would dominate volume | ||
| /// without adding wedge evidence. | ||
| public static let deniedEvents: Set<String> = [ | ||
| "pong-sent", "ponged", "pong", "hint-update", "discovered", "acked", | ||
| "directory", "lane-accepted", | ||
| ] | ||
|
|
||
| public static let maximumBatch = 100 | ||
| private static let bufferCapacity = 500 | ||
| private static let flushThreshold = 50 | ||
|
|
||
| private let endpoint: URL | ||
| private let metadata: ClientMetadata | ||
| private let token: @Sendable (_ forceRefresh: Bool) async throws -> String | ||
| private let transport: @Sendable (URLRequest) async throws -> Int | ||
| private let flushInterval: TimeInterval | ||
| private var buffer: [[String: Any]] = [] | ||
| private var flushTask: Task<Void, Never>? | ||
| private var stopped = false | ||
| public private(set) var droppedCount = 0 | ||
| public private(set) var uploadedCount = 0 | ||
|
|
||
| /// - Parameters: | ||
| /// - endpoint: The complete transport-journal ingest URL. | ||
| /// - metadata: Attribution attached to every exported event. | ||
| /// - token: Bearer credential provider; `true` forces a refresh. | ||
| /// - transport: Executes the request and returns the HTTP status. | ||
| /// - flushInterval: Idle time before a partial batch ships. | ||
| public init( | ||
| endpoint: URL, | ||
| metadata: ClientMetadata, | ||
| token: @escaping @Sendable (_ forceRefresh: Bool) async throws -> String, | ||
| transport: @escaping @Sendable (URLRequest) async throws -> Int, | ||
| flushInterval: TimeInterval = 30 | ||
| ) { | ||
| self.endpoint = endpoint | ||
| self.metadata = metadata | ||
| self.token = token | ||
| self.transport = transport | ||
| self.flushInterval = flushInterval | ||
| } | ||
|
|
||
| /// Journal-tap entry point; synchronous and non-blocking by contract. | ||
| public nonisolated func offer(_ event: IrxJournalEvent) { | ||
| guard Self.exportedComponents.contains(event.component), | ||
| !Self.deniedEvents.contains(event.event) else { return } | ||
| Task { await self.enqueue(event) } | ||
| } | ||
|
|
||
| public func stop() { | ||
| stopped = true | ||
| flushTask?.cancel() | ||
| flushTask = nil | ||
| buffer.removeAll() | ||
| } | ||
|
|
||
| /// Ships everything currently buffered; used by tests and shutdown paths. | ||
| public func flushNow() async { | ||
| flushTask?.cancel() | ||
| flushTask = nil | ||
| await flush() | ||
| } | ||
|
|
||
| private func enqueue(_ event: IrxJournalEvent) async { | ||
| guard !stopped else { return } | ||
| buffer.append(wire(event)) | ||
| if buffer.count > Self.bufferCapacity { | ||
| droppedCount += buffer.count - Self.bufferCapacity | ||
| buffer.removeFirst(buffer.count - Self.bufferCapacity) | ||
| } | ||
| if buffer.count >= Self.flushThreshold { | ||
| flushTask?.cancel() | ||
| flushTask = nil | ||
| await flush() | ||
| return | ||
| } | ||
| guard flushTask == nil else { return } | ||
| let interval = flushInterval | ||
| flushTask = Task { [weak self] in | ||
| do { try await Task.sleep(for: .seconds(interval)) } | ||
| catch { return } | ||
| await self?.scheduledFlush() | ||
| } | ||
| } | ||
|
|
||
| private func scheduledFlush() async { | ||
| flushTask = nil | ||
| await flush() | ||
| } | ||
|
|
||
| private func flush() async { | ||
| guard !stopped, !buffer.isEmpty else { return } | ||
| let batch = Array(buffer.prefix(Self.maximumBatch)) | ||
| buffer.removeFirst(batch.count) | ||
| guard let body = try? JSONSerialization.data(withJSONObject: ["batch": batch]) else { | ||
| droppedCount += batch.count | ||
| return | ||
| } | ||
| var status = await post(body: body, forceToken: false) | ||
| guard !stopped else { return } | ||
| if status == 401 { | ||
| status = await post(body: body, forceToken: true) | ||
| guard !stopped else { return } | ||
| } | ||
| switch status { | ||
| case 200..<300: | ||
| uploadedCount += batch.count | ||
| case 400, 404, 413: | ||
| // The server rejected the batch shape, or this backend does not | ||
| // serve the route yet; retrying this batch cannot succeed. Later | ||
| // batches still try, so the lane comes up when the route ships. | ||
| droppedCount += batch.count | ||
| default: | ||
| // Auth outage, rate limit, or transport failure: retain for the | ||
| // next flush, bounded by the buffer capacity. | ||
| buffer.insert(contentsOf: batch, at: 0) | ||
| if buffer.count > Self.bufferCapacity { | ||
| droppedCount += buffer.count - Self.bufferCapacity | ||
| buffer.removeLast(buffer.count - Self.bufferCapacity) | ||
| } | ||
| } | ||
| if !buffer.isEmpty, flushTask == nil, !stopped { | ||
| let interval = flushInterval | ||
| flushTask = Task { [weak self] in | ||
| do { try await Task.sleep(for: .seconds(interval)) } | ||
| catch { return } | ||
| await self?.scheduledFlush() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func post(body: Data, forceToken: Bool) async -> Int { | ||
| guard !stopped else { return -1 } | ||
| guard let credential = try? await token(forceToken) else { return -1 } | ||
| guard !stopped else { return -1 } | ||
| var request = URLRequest(url: endpoint) | ||
| request.httpMethod = "POST" | ||
| request.timeoutInterval = 15 | ||
| request.setValue("application/json", forHTTPHeaderField: "Content-Type") | ||
| request.setValue("Bearer " + credential, forHTTPHeaderField: "Authorization") | ||
| request.httpBody = body | ||
| let status = (try? await transport(request)) ?? -1 | ||
| guard !stopped else { return -1 } | ||
| return status | ||
| } | ||
|
|
||
| private let timestampFormatter: ISO8601DateFormatter = { | ||
| let formatter = ISO8601DateFormatter() | ||
| formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds] | ||
| return formatter | ||
| }() | ||
|
|
||
| private func wire(_ event: IrxJournalEvent) -> [String: Any] { | ||
| var value: [String: Any] = [ | ||
| "timestamp": timestampFormatter.string(from: event.wallTime), | ||
| "monoMs": event.monotonicMs, | ||
| "component": event.component, | ||
| "event": event.event, | ||
| "platform": metadata.platform, | ||
| "clientChannel": metadata.clientChannel, | ||
| ] | ||
| if let appVersion = metadata.appVersion { value["appVersion"] = appVersion } | ||
| if let buildNumber = metadata.buildNumber { value["buildNumber"] = buildNumber } | ||
| if let bundleIdentifier = metadata.bundleIdentifier { value["bundleIdentifier"] = bundleIdentifier } | ||
| if let osVersion = metadata.osVersion { value["osVersion"] = osVersion } | ||
| if let endpoint = metadata.endpoint { value["endpoint"] = endpoint } | ||
| if let deviceId = metadata.deviceId { value["deviceId"] = deviceId } | ||
| if let buildTag = metadata.buildTag { value["buildTag"] = buildTag } | ||
| if !event.attributes.isEmpty { | ||
| // Server-side caps: 16 keys, snake-case keys, 160-char values. | ||
| var attributes: [String: String] = [:] | ||
| for (key, item) in event.attributes.sorted(by: { $0.key < $1.key }).prefix(16) { | ||
| let normalized = key.lowercased().replacingOccurrences( | ||
| of: "[^a-z0-9_]", with: "_", options: .regularExpression) | ||
| guard !normalized.isEmpty, normalized.count <= 32 else { continue } | ||
| guard !item.isEmpty else { continue } | ||
| attributes[normalized] = String(item.prefix(160)) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| if !attributes.isEmpty { value["attributes"] = attributes } | ||
| } | ||
| return value | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: manaflow-ai/cmux
Length of output: 6322
🏁 Script executed:
Repository: manaflow-ai/cmux
Length of output: 41648
🏁 Script executed:
Repository: manaflow-ai/cmux
Length of output: 41746
Cancel an in-flight upload when
IrxJournalUploader.stop()must end all uploads.stop()does not cancel a transport request that already started. The host transition does not wait for that request, so this does not block shutdown. The old uploader can finish one already-serialized telemetry POST in the background, with no later events or retry afterstop().If the lifecycle contract requires no upload after the old scope stops, track the active transport task and cancel it from
stop(). Add coverage for a transport suspended after the request starts.🤖 Prompt for AI Agents