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
125 changes: 119 additions & 6 deletions Prism/Models/RuntimeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,44 @@ struct RuntimeState: Decodable, Sendable {
/// Additive cascade metadata; nil when the backend omits the key (legacy / offline fixture).
let cascade: CascadeMetadata?
let research: ResearchStatus?
/// Continuous runtime projection; absent in older journals and fixtures.
let operations: OperationalStatus?
}

struct OperationalStatus: Decodable, Sendable {
let mode: String
let enabled: Bool
let nextWakeAt: String?
let rationale: String?
let riskStatus: String?
let grossExposure: Double?
let realizedPnl: Double?
let unrealizedPnl: Double?
let sections: [OperationalSection]
let componentCosts: [OperationalCost]
}

struct OperationalSection: Decodable, Identifiable, Sendable {
let id: String
let title: String
let items: [OperationalItem]
}

struct OperationalItem: Decodable, Identifiable, Sendable {
let id: String
let title: String
let detail: String
let status: String?
let time: String?
let sourceUrl: String?
}

struct OperationalCost: Decodable, Identifiable, Sendable {
let id: String
let calls: Int
let spentUsd: Double
let reservedUsd: Double
let limitUsd: Double
}

struct ResearchStatus: Decodable, Sendable {
Expand All @@ -24,6 +62,81 @@ struct ResearchStatus: Decodable, Sendable {
let operatingCosts: ResearchCosts?
let coverage: ResearchCoverage?
let forwardReports: [ForwardResearchReport]?
/// Optional bounded projection; older runtimes do not send it.
let leaderboard: ResearchLeaderboard?
}

struct ResearchLeaderboard: Decodable, Sendable {
let rows: [ResearchLeaderboardRow]?
let total: Int?
let ranked: Int?
let rankingBasis: String?
let limit: Int?

var testWindows: [String] {
Array(Set((rows ?? []).compactMap(\.testWindow))).sorted(by: >)
}

/// Sort finite development returns first, preserving backend order for ties
/// and for rows whose return has not been measured.
var rankedRows: [ResearchLeaderboardRow] {
(rows ?? []).enumerated().sorted { lhs, rhs in
let left = lhs.element.finiteNetReturn
let right = rhs.element.finiteNetReturn
switch (left, right) {
case let (left?, right?):
return left == right ? lhs.offset < rhs.offset : left > right
case (_?, nil):
return true
case (nil, _?):
return false
case (nil, nil):
return lhs.offset < rhs.offset
}
}.map(\.element)
}
}

struct ResearchLeaderboardRow: Decodable, Identifiable, Sendable {
let id: String
let experimentId: String?
let name: String?
let symbol: String?
let family: String?
let phase: String?
let netReturn: Double?
let maxDrawdown: Double?
let trades: Int?
let screenStatus: String?
let prospectiveStatus: String?
let qualification: ResearchQualification?
let forwardNetReturn: Double?
let forwardAsOf: String?
let testStart: String?
let testEnd: String?

var testWindow: String? {
guard let testStart, let testEnd, !testStart.isEmpty, !testEnd.isEmpty else { return nil }
return "\(testStart) → \(testEnd)"
}

var finiteNetReturn: Double? {
guard let netReturn, netReturn.isFinite else { return nil }
return netReturn
}

var finiteMaxDrawdown: Double? {
guard let maxDrawdown, maxDrawdown.isFinite else { return nil }
return maxDrawdown
}
}

struct ResearchQualification: Decodable, Sendable {
let qualified: Bool?
let status: String?
let basis: String?
let rankingEffect: String?
let reasons: [String]?
}

struct ResearchCosts: Decodable, Sendable {
Expand Down Expand Up @@ -370,12 +483,12 @@ enum PrismPage: String, CaseIterable, Identifiable {
var title: String { rawValue.capitalized }
var symbol: String {
switch self {
case .overview: "square.grid.2x2"
case .research: "sparkle.magnifyingglass"
case .test: "waveform.path.ecg"
case .execution: "arrow.left.arrow.right"
case .agents: "person.3"
case .settings: "gearshape"
case .overview: "chart.xyaxis.line"
case .research: "scope"
case .test: "checkmark.seal"
case .execution: "arrow.triangle.swap"
case .agents: "person.2"
case .settings: "slider.horizontal.3"
}
}
}
Expand Down
122 changes: 98 additions & 24 deletions Prism/Views/ConsoleComponents.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
import SwiftUI

struct ConsoleCard<Content: View>: View {
/// Frosted content surfaces share one radius, spacing and accessible fallback.
/// Native liquid glass stays on controls; content uses material to remain legible.
private struct ConsoleGlass: ViewModifier {
@Environment(\.colorScheme) private var colorScheme
@Environment(\.accessibilityReduceTransparency) private var reduceTransparency
@Environment(\.colorSchemeContrast) private var contrast

func body(content: Content) -> some View {
let shape = RoundedRectangle(cornerRadius: 22, style: .continuous)
content
.background {
if reduceTransparency {
shape.fill(colorScheme == .dark ? Color(white: 0.16) : Color(white: 0.96))
} else {
shape.fill(.ultraThinMaterial).opacity(0.6)
shape.fill(.white.opacity(colorScheme == .dark ? 0.025 : 0.12))
}
}
.overlay {
shape.strokeBorder(
contrast == .increased ? AnyShapeStyle(.primary.opacity(0.6)) :
AnyShapeStyle(LinearGradient(colors: [.white.opacity(0.24), .white.opacity(0.025), .white.opacity(0.1)],
startPoint: .topLeading, endPoint: .bottomTrailing)),
lineWidth: contrast == .increased ? 1.5 : 0.65
).allowsHitTesting(false)
}
}
}

struct ConsoleCard<Content: View>: View {
let title: String?
let caption: String?
@ViewBuilder let content: Content
Expand All @@ -12,21 +39,49 @@ struct ConsoleCard<Content: View>: View {
self.caption = caption
self.content = content()
}

var body: some View {
VStack(alignment: .leading, spacing: 18) {
VStack(alignment: .leading, spacing: 16) {
if let title {
HStack {
Text(title).font(.system(size: 14, weight: .semibold))
Spacer()
if let caption {
Text(caption).font(.system(size: 10, weight: .medium, design: .monospaced)).foregroundStyle(.secondary)
ViewThatFits(in: .horizontal) {
HStack(alignment: .firstTextBaseline, spacing: 12) {
heading(title)
Spacer(minLength: 0)
captionText
}
VStack(alignment: .leading, spacing: 5) {
heading(title)
captionText
}
}
}
content
}
.frame(maxWidth: .infinity, alignment: .leading).padding(.vertical, 14).padding(.horizontal, 4)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(20)
.modifier(ConsoleGlass())
}

private func heading(_ title: String) -> some View {
Text(title).font(.system(size: 13, weight: .medium)).accessibilityAddTraits(.isHeader)
}

@ViewBuilder private var captionText: some View {
if let caption {
Text(caption).font(.system(size: 10)).foregroundStyle(.secondary)
}
}
}

struct ConsoleMetricGrid<Content: View>: View {
@ViewBuilder let content: Content
var body: some View {
ViewThatFits(in: .horizontal) {
HStack(alignment: .top, spacing: 12) { content }
LazyVGrid(columns: [GridItem(.flexible(), spacing: 12), GridItem(.flexible())], spacing: 12) {
content
}
}
}
}

Expand All @@ -36,13 +91,30 @@ struct ConsoleMetric: View {
let detail: String
var body: some View {
ConsoleCard {
VStack(alignment: .leading, spacing: 12) {
VStack(alignment: .leading, spacing: 9) {
Text(title).font(.system(size: 11, weight: .medium)).foregroundStyle(.secondary)
Text(value).font(.system(size: 25, weight: .medium, design: .rounded)).monospacedDigit()
.lineLimit(1).minimumScaleFactor(0.65)
Text(detail).font(.system(size: 10)).foregroundStyle(.secondary).lineLimit(2)
Text(detail).font(.system(size: 10)).foregroundStyle(.secondary)
.lineLimit(2, reservesSpace: true)
}
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(minWidth: 130)
.accessibilityElement(children: .combine)
}
}

struct ConsoleIcon: View {
let symbol: String
var body: some View {
Image(systemName: symbol)
.font(.system(size: 16, weight: .regular))
.symbolRenderingMode(.hierarchical)
.foregroundStyle(.secondary)
.frame(width: 32, height: 32)
.background(.primary.opacity(0.045), in: .circle)
.accessibilityHidden(true)
}
}

Expand All @@ -51,8 +123,7 @@ struct StatusPill: View {
var color: Color {
switch status.lowercased() {
case "running", "working", "configured", "active", "completed", "passed", "accepted", "eligible", "paper", "paper_eligible": .mint
case "error", "failed", "rejected": .orange
case "paused", "budget_limited", "waiting", "queued": .secondary
case "error", "failed", "rejected", "blocked", "halted": .orange
default: .secondary
}
}
Expand All @@ -61,7 +132,9 @@ struct StatusPill: View {
Circle().fill(color).frame(width: 5, height: 5)
Text(PrismFormat.label(status)).lineLimit(1)
}
.font(.system(size: 10, weight: .medium)).padding(.horizontal, 9).padding(.vertical, 6)
.font(.system(size: 10, weight: .medium))
.padding(.horizontal, 8).padding(.vertical, 5)
.background(.primary.opacity(0.045), in: .capsule)
.accessibilityLabel("Status: \(PrismFormat.label(status))")
}
}
Expand All @@ -71,29 +144,30 @@ struct ConsoleEmptyState: View {
let title: String
let message: String
var body: some View {
VStack(spacing: 12) {
Image(systemName: symbol).font(.system(size: 27, weight: .ultraLight)).foregroundStyle(.secondary)
Text(title).font(.system(size: 15, weight: .medium))
Text(message).font(.system(size: 12)).foregroundStyle(.secondary)
.multilineTextAlignment(.center).frame(maxWidth: 380)
VStack(spacing: 10) {
ConsoleIcon(symbol: symbol)
Text(title).font(.system(size: 13, weight: .medium))
Text(message).font(.system(size: 11)).foregroundStyle(.secondary)
.multilineTextAlignment(.center).frame(maxWidth: 340)
}
.frame(maxWidth: .infinity).padding(.vertical, 35)
.frame(maxWidth: .infinity).padding(.vertical, 20)
}
}

struct EventRow: View {
let event: RuntimeEvent
var body: some View {
HStack(alignment: .top, spacing: 12) {
Image(systemName: event.level == "error" ? "exclamationmark.circle" : "circle.fill")
.font(.system(size: event.level == "error" ? 13 : 5))
.foregroundStyle(event.level == "error" ? Color.orange : .secondary).frame(width: 14, height: 16)
VStack(alignment: .leading, spacing: 4) {
Image(systemName: event.level == "error" ? "exclamationmark.circle" : "circle")
.font(.system(size: 12, weight: .regular))
.foregroundStyle(event.level == "error" ? Color.orange : .secondary)
.frame(width: 16, height: 16).accessibilityHidden(true)
VStack(alignment: .leading, spacing: 5) {
Text(event.message).font(.system(size: 12)).textSelection(.enabled)
Text(PrismFormat.label(event.source)).font(.system(size: 10)).foregroundStyle(.secondary)
}
Spacer(minLength: 8)
Text(PrismFormat.time(event.timestamp)).font(.system(size: 10, design: .monospaced)).foregroundStyle(.secondary)
Text(PrismFormat.time(event.timestamp)).font(.system(size: 10)).monospacedDigit().foregroundStyle(.secondary)
}
.padding(.vertical, 7)
}
Expand Down
29 changes: 16 additions & 13 deletions Prism/Views/PrismActivityView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@ import SwiftUI
struct PrismActivityView: View {
@ObservedObject var runtime: RuntimeClient
@State private var query = ""

private var events: [RuntimeEvent] {
(runtime.state?.events ?? []).filter {
query.isEmpty || $0.message.localizedCaseInsensitiveContains(query) || $0.source.localizedCaseInsensitiveContains(query)
}
}

var body: some View {
VStack(spacing: 18) {
HStack(spacing: 10) {
Image(systemName: "magnifyingglass").foregroundStyle(.secondary)
TextField("Search", text: $query).textFieldStyle(.plain)
.accessibilityLabel("Search activity")
ConsoleCard("Event log", caption: "\(events.count) EVENTS") {
HStack(spacing: 8) {
ConsoleIcon(symbol: "magnifyingglass")
TextField("Search events", text: $query).textFieldStyle(.plain)
.font(.system(size: 12)).accessibilityLabel("Search activity")
if !query.isEmpty {
Button { query = "" } label: { Image(systemName: "xmark.circle.fill").foregroundStyle(.secondary) }
Button { query = "" } label: { ConsoleIcon(symbol: "xmark") }
.buttonStyle(.plain).accessibilityLabel("Clear search")
}
}
.font(.system(size: 12)).padding(.vertical, 8)
ConsoleCard {
if events.isEmpty {
Text(query.isEmpty ? "No events yet." : "No matching events.").font(.system(size: 12)).foregroundStyle(.secondary)
} else {
LazyVStack(spacing: 8) { ForEach(events) { EventRow(event: $0) } }
}.padding(.vertical, 4)

if events.isEmpty {
Text(query.isEmpty ? "No events yet." : "No matching events.")
.font(.system(size: 11)).foregroundStyle(.secondary)
} else {
LazyVStack(spacing: 4) {
ForEach(events) { EventRow(event: $0) }
}
}
}
Expand Down
Loading
Loading