SPB Git

Repository Public License

AgentRunHistory.swift 3.2 KB 67 lines swift

1import Foundation2import NavigatorCore3import Persistence45/// Saved runs, and what happens to runs that were still going when the app quit.6///7/// There is no API to continue a run: the conversation with the model, the confirmations and the page state are8/// not stored, so a run that was cut short can only be shown, never resumed.9public struct AgentRunHistory: Sendable {10    /// Summary of the step appended to a run that the app found unfinished at launch.11    public static let interruptedSummary = "Interrupted: the app quit while this run was in progress. It was not resumed."1213    private let store: AgentRunStore1415    public init(database: NavigatorDatabase) { store = database.agentRuns }1617    /// Call once at launch, before any session starts. Every run still marked `running` or `awaitingConfirmation`18    /// is closed as failed with an "interrupted" step; its open steps become cancelled. Returns the runs changed.19    /// `excluding` protects sessions that are genuinely running in this process.20    @discardableResult21    public func markInterruptedRuns(excluding live: Set<AgentRunID> = [], now: Date = .now) async throws -> [AgentRun] {22        var changed: [AgentRun] = []23        for var run in try await store.unfinished() where !live.contains(run.id) {24            for index in run.steps.indices where run.steps[index].status == .pending || run.steps[index].status == .running {25                run.steps[index].status = .cancelled26                run.steps[index].finishedAt = now27            }28            let nextID = (run.steps.map(\.id).max() ?? 0) + 129            run.steps.append(AgentStep(id: nextID, kind: .error, status: .failed, summary: Self.interruptedSummary, startedAt: now, finishedAt: now))30            run.status = .failed31            run.finishedAt = now32            try await store.save(run)33            changed.append(run)34        }35        return changed36    }3738    public func runs(space: SpaceID? = nil, limit: Int = 100) async throws -> [AgentRun] { try await store.runs(space: space, limit: limit) }39    public func run(_ id: AgentRunID) async throws -> AgentRun? { try await store.run(id) }40    public func delete(_ id: AgentRunID) async throws { try await store.delete(id) }4142    /// How a stored run should be presented.43    public enum DisplayState: String, Sendable, Hashable {44        case completed, failed, cancelled, budgetExhausted45        /// Cut short by the app quitting.46        case interrupted47        /// Still marked active in storage: `markInterruptedRuns` has not run yet, or the run is live in this process.48        case active49    }5051    public static func displayState(of run: AgentRun) -> DisplayState {52        switch run.status {53        case .running, .awaitingConfirmation: .active54        case .completed: .completed55        case .cancelled: .cancelled56        case .budgetExhausted: .budgetExhausted57        case .failed: run.steps.last?.summary == interruptedSummary ? .interrupted : .failed58        }59    }6061    /// Why a finished run ended, taken from its last step (the runtime always writes one).62    public static func endReason(of run: AgentRun) -> String? {63        guard run.status.isTerminal, let last = run.steps.last, last.kind == .error || last.kind == .answer else { return nil }64        return last.summary65    }66}67
Files main
Menu

Theme