Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ jobs:
with:
python-version: '3.12'
- run: scripts/check-ui-contract.sh
- run: scripts/check-workspace-contract.sh
- run: scripts/check-runtime-client-actions.sh
66 changes: 66 additions & 0 deletions Prism/Models/RuntimeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ struct TradingModeStatus: Decodable, Sendable {
let lifetimeModelBudgetUsd: Double?
let tradingPnl: Double?
let tradingReturn: Double?
/// Zero is a valid observed balance; nil means no usable equity value was projected.
let accountEquity: Double?
let accountAsOf: String?
let accountDay: String?
let accountStatus: String?
let grossExposure: Double?
let leverage: Double?
let leveragePolicy: String?
Expand All @@ -51,10 +55,49 @@ struct TradingModeStatus: Decodable, Sendable {
let apiReservedUsd: Double?
let pending: Bool?
let unsupportedReason: String?
/// Actual Bybit SOL linear position/fill state from a matching local controller snapshot.
/// Missing in older runtimes and fixtures.
let actualSolExecution: SOLExecutionProjection?

var isSolPerpetual: Bool { selected == "sol_perpetual" }
}

struct SOLExecutionProjection: Decodable, Sendable {
let environment: String?
let positionsStatus: String?
let positionsAsOf: String?
let confirmedFlat: Bool?
let positions: [SOLExecutionPosition]?
let positionsTruncated: Bool?
let fillsAvailable: Bool?
let fillsTruncated: Bool?
let fills: [SOLExecutionFill]?
}

struct SOLExecutionPosition: Decodable, Identifiable, Sendable {
let id: String
let category: String?
let symbol: String?
let side: String
let quantity: Double
let entryPrice: Double
let markPrice: Double
let leverage: Double?
let stopLoss: Double?
let ownership: String?
}

struct SOLExecutionFill: Decodable, Identifiable, Sendable {
let id: String
let side: String
let quantity: Double
let price: Double
let timestamp: String
let fee: Double?
let feeCurrency: String?
let realizedPnl: Double?
}

struct TradeReviewStatus: Decodable, Sendable {
let status: String
let decision: String?
Expand All @@ -74,8 +117,15 @@ struct OperationalStatus: Decodable, Sendable {
let grossExposure: Double?
let realizedPnl: Double?
let unrealizedPnl: Double?
let orderCount: Int?
let activeOrderCount: Int?
let ordersTruncated: Bool?
let sections: [OperationalSection]
let componentCosts: [OperationalCost]

func section(id: String) -> OperationalSection? {
sections.first { $0.id == id }
}
}

struct OperationalSection: Decodable, Identifiable, Sendable {
Expand Down Expand Up @@ -479,13 +529,29 @@ struct MarketQuote: Decodable, Identifiable, Sendable {
let fundingRate: Double?
let updatedAt: String

func freshness(at now: Date, staleAfter: TimeInterval = 15,
futureTolerance: TimeInterval = 5) -> MarketQuoteFreshness {
guard let date = PrismFormat.date(updatedAt) else { return .unavailable }
let age = now.timeIntervalSince(date)
guard age.isFinite else { return .unavailable }
if age < -futureTolerance { return .clockSkew }
return age > staleAfter ? .stale(age: age) : .current(age: max(0, age))
}

// Foundation capitalizes the letter after the numeric component in change_24h.
private enum CodingKeys: String, CodingKey {
case symbol, price, fundingRate, updatedAt
case change24h = "change24H"
}
}

enum MarketQuoteFreshness: Equatable, Sendable {
case current(age: TimeInterval)
case stale(age: TimeInterval)
case clockSkew
case unavailable
}

struct RuntimeEvent: Decodable, Identifiable, Sendable {
let id: Int
let timestamp: String
Expand Down
97 changes: 75 additions & 22 deletions Prism/Services/RuntimeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import Foundation
import LocalAuthentication
import Security

struct RuntimeHTTPResponse: Sendable {
let data: Data
let statusCode: Int
}

typealias RuntimeRequestSender = @Sendable (URLRequest) async throws -> RuntimeHTTPResponse

@MainActor
final class RuntimeClient: ObservableObject {
@Published private(set) var state: RuntimeState?
Expand All @@ -16,16 +23,26 @@ final class RuntimeClient: ObservableObject {
private let endpoint = "http://127.0.0.1:8787"
private var token: String
private var hasToken = false
private let session: URLSession
private let requestSender: RuntimeRequestSender
private var isFetching = false
private var stateFetchGeneration: UInt64 = 0
private var mutationFeedback: String?

init() {
token = ""
init(requestSender: RuntimeRequestSender? = nil, controlToken: String? = nil) {
token = controlToken ?? ""
hasToken = !(controlToken ?? "").isEmpty
let configuration = URLSessionConfiguration.ephemeral
configuration.timeoutIntervalForRequest = 8
configuration.timeoutIntervalForResource = 12
configuration.waitsForConnectivity = false
session = URLSession(configuration: configuration, delegate: RuntimeRedirectPolicy(), delegateQueue: nil)
let urlSession = URLSession(configuration: configuration, delegate: RuntimeRedirectPolicy(), delegateQueue: nil)
self.requestSender = requestSender ?? { request in
let (data, response) = try await urlSession.data(for: request)
guard let http = response as? HTTPURLResponse else {
throw RuntimeError.message("The runtime returned an invalid response.")
}
return RuntimeHTTPResponse(data: data, statusCode: http.statusCode)
}
}

func monitor() async {
Expand Down Expand Up @@ -55,8 +72,11 @@ final class RuntimeClient: ObservableObject {
}

private func fetchState() async {
stateFetchGeneration &+= 1
let generation = stateFetchGeneration
do {
let data = try await send(path: "/v1/state")
guard !Task.isCancelled, generation == stateFetchGeneration else { return }
let previousPortfolio = state?.portfolio
let previousSamples = pnlSamples
let (value, samples) = try await Task.detached(priority: .utility) {
Expand All @@ -71,16 +91,16 @@ final class RuntimeClient: ObservableObject {
// timestamps away from the UI thread when a valuation arrives.
return (value, unchanged ? previousSamples : portfolio.pnlSamples)
}.value
guard !Task.isCancelled else { return }
guard !Task.isCancelled, generation == stateFetchGeneration else { return }
pnlSamples = samples
state = value
isConnected = true
lastUpdated = .now
errorMessage = nil
errorMessage = mutationFeedback
} catch is CancellationError {
return
} catch {
guard !Task.isCancelled else { return }
guard !Task.isCancelled, generation == stateFetchGeneration else { return }
isConnected = false
errorMessage = readable(error)
}
Expand All @@ -93,8 +113,11 @@ final class RuntimeClient: ObservableObject {
do {
let body = try JSONSerialization.data(withJSONObject: ["action": action])
_ = try await send(path: "/v1/control", method: "POST", body: body)
await refresh()
} catch { errorMessage = readable(error) }
// Force a post-mutation read even when the monitor already has a
// request in flight. The generation check prevents that older
// response from replacing this confirmed state afterward.
await refreshAfterMutation()
} catch { reportMutationFailure(error) }
}

func updateConfiguration(_ configuration: RuntimeConfiguration) async -> Bool {
Expand All @@ -105,10 +128,10 @@ final class RuntimeClient: ObservableObject {
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
_ = try await send(path: "/v1/config", method: "POST", body: encoder.encode(configuration))
await refresh()
await refreshAfterMutation()
return true
} catch {
errorMessage = readable(error)
reportMutationFailure(error)
return false
}
}
Expand All @@ -122,18 +145,18 @@ final class RuntimeClient: ObservableObject {
do {
let body = try JSONSerialization.data(withJSONObject: ["trading_mode": ["selected": mode]])
_ = try await send(path: "/v1/config", method: "POST", body: body)
await refresh()
await refreshAfterMutation()
return true
} catch {
errorMessage = readable(error)
reportMutationFailure(error)
return false
}
}

func updateDailySpendCeiling(_ amount: Double?) async -> Bool {
guard !isBusy, isConnected else { return false }
if let amount, !amount.isFinite || amount <= 0 {
errorMessage = "Enter a USD amount greater than zero."
reportMutationFailure(RuntimeError.message("Enter a USD amount greater than zero."))
return false
}
isBusy = true
Expand All @@ -142,10 +165,10 @@ final class RuntimeClient: ObservableObject {
let value: Any = amount.map { $0 as Any } ?? NSNull()
let body = try JSONSerialization.data(withJSONObject: ["daily_limit_usd": value])
_ = try await send(path: "/v1/config", method: "POST", body: body)
await refresh()
await refreshAfterMutation()
return true
} catch {
errorMessage = readable(error)
reportMutationFailure(error)
return false
}
}
Expand All @@ -172,19 +195,18 @@ final class RuntimeClient: ObservableObject {
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Accept")
if body != nil { request.setValue("application/json", forHTTPHeaderField: "Content-Type") }
let (data, response) = try await session.data(for: request)
guard let response = response as? HTTPURLResponse else { throw RuntimeError.message("The runtime returned an invalid response.") }
let response = try await requestSender(request)
guard (200..<300).contains(response.statusCode) else {
if response.statusCode == 401 || response.statusCode == 403 {
throw RuntimeError.message("The local control token was rejected. Reinstall the Prism service to relink it.")
throw RuntimeError.authentication("The local control token was rejected. Reinstall the Prism service to relink it.")
}
if let value = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
if let value = try? JSONSerialization.jsonObject(with: response.data) as? [String: Any],
let message = value["error"] as? String {
throw RuntimeError.message(message)
}
throw RuntimeError.message("Runtime request failed (HTTP \(response.statusCode)).")
}
return data
return response.data
}

private func readable(_ error: Error) -> String {
Expand All @@ -197,11 +219,42 @@ final class RuntimeClient: ObservableObject {
}
return error.localizedDescription
}

private func reportMutationFailure(_ error: Error) {
// A failed local transport means the last projected status is only an
// offline snapshot. Keep it visible, but disable further operator actions.
let authenticationRejected = (error as? RuntimeError)?.invalidatesConnection == true
if error is URLError || authenticationRejected {
mutationFeedback = nil
stateFetchGeneration &+= 1
isConnected = false
} else {
// Monitoring does not acknowledge an operator refusal. Keep its
// explanation visible until a subsequent mutation succeeds.
mutationFeedback = readable(error)
}
errorMessage = readable(error)
}

private func refreshAfterMutation() async {
mutationFeedback = nil
await fetchState()
}
}

private enum RuntimeError: LocalizedError {
case message(String)
var errorDescription: String? { if case let .message(message) = self { message } else { nil } }
case authentication(String)

var invalidatesConnection: Bool {
if case .authentication = self { true } else { false }
}

var errorDescription: String? {
switch self {
case let .message(message), let .authentication(message): message
}
}
}

private final class RuntimeRedirectPolicy: NSObject, URLSessionTaskDelegate, @unchecked Sendable {
Expand Down
3 changes: 2 additions & 1 deletion Prism/Views/PrismHomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ struct PrismHomeView: View {

private var connectionNotice: some View {
HStack(alignment: .top, spacing: 8) {
Image(systemName: "wifi.slash").font(.system(size: 12)).foregroundStyle(.secondary)
Image(systemName: runtime.isConnected ? "info.circle" : "wifi.slash")
.font(.system(size: 12)).foregroundStyle(.secondary)
VStack(alignment: .leading, spacing: 4) {
Text(runtime.isConnected ? "Runtime notice" : (runtime.state == nil ? "Runtime offline" : "Offline · showing last snapshot"))
.font(.system(size: 11, weight: .medium))
Expand Down
Loading
Loading