Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
7,501 changes: 4,135 additions & 3,366 deletions Resources/Localizable.xcstrings

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions Sources/AgentHibernation/AgentWakeFailure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Foundation

/// Why a woken agent was judged not to have come back.
enum AgentWakeFailureReason: Equatable, Sendable {
/// The resume command ran and returned to the shell prompt before the
/// agent reported in.
case exitedBeforeStart
/// Nothing reported in before the verification deadline, and no live
/// agent process was found for the pane.
case didNotStart

/// The second line of the failure banner and the feed entry body.
var detail: String {
switch self {
case .exitedBeforeStart:
String(
localized: "agentWake.reason.exitedBeforeStart",
defaultValue: "Its resume command exited."
)
case .didNotStart:
String(
localized: "agentWake.reason.didNotStart",
defaultValue: "It did not start within 90 seconds."
)
}
}
}

/// A failed wake shown on the terminal pane until retried, dismissed, or
/// superseded by a later success signal.
struct AgentWakeFailure {
let reason: AgentWakeFailureReason
/// The agent name shown to the user, for example "Claude Code".
let agentDisplayName: String
/// The command shown by "Show command". This is the agent's resume
/// command when one exists, otherwise the typed startup input.
let commandText: String
let agent: SessionRestorableAgentSnapshot
/// False while something still runs in the pane, where typing the resume
/// command again would feed it to that program instead of the shell.
var canRetry: Bool = true
}
24 changes: 24 additions & 0 deletions Sources/AgentHibernation/AgentWakeVerification.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation

/// One pane's wake check, owned by its `Workspace`.
struct AgentWakeVerification {
/// Identifies this check so a deadline scheduled for an older check does
/// not act on a newer one.
let token: UUID
let agent: SessionRestorableAgentSnapshot
let startedAt: Date
/// The command shown to the user if the wake fails.
let commandText: String
var state: AgentWakeVerificationState
var deadlineTask: Task<Void, Never>?

/// Whether a hook report under `statusKey` comes from the woken agent.
/// Hook status keys name the agent kind (Claude reports as
/// `claude_code`); a Feed attention overlay for that agent counts too.
/// Reports from another agent in the same pane leave the check pending.
func acceptsReport(statusKey: String) -> Bool {
let source = agent.kind.rawValue
return statusKey == FeedCoordinator.lifecycleStatusKey(forSource: source)
|| statusKey == FeedCoordinator.attentionStatusKey(forSource: source)
}
}
51 changes: 51 additions & 0 deletions Sources/AgentHibernation/AgentWakeVerificationState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Foundation

/// Pure state for checking that an agent woken from hibernation came back.
///
/// The owner feeds it the signals it sees for one pane: the wake itself, an
/// agent hook reporting for the pane, the resume command returning to the
/// shell prompt, and the verification deadline.
enum AgentWakeVerificationState: Equatable, Sendable {
case pending
case succeeded
case failed(AgentWakeFailureReason)

enum Event: Equatable, Sendable {
/// A wake (or a retry) typed the resume command into the pane.
case started
/// An agent hook reported a PID or a lifecycle state for the pane.
case agentReported
Comment thread
teamleaderleo marked this conversation as resolved.
Resolved
/// A live process of the woken agent was found in the pane, which
/// confirms agents without hooks.
case liveProcessFound
/// The resume command ended and the shell prompt came back.
case commandEnded
/// The verification deadline passed. `hasLiveProcess` says whether a
/// live agent process was found for the pane at that moment.
case deadline(hasLiveProcess: Bool)
}

func applying(_ event: Event) -> AgentWakeVerificationState {
switch (self, event) {
case (_, .started):
return .pending
case (_, .agentReported):
// A report after a failure means the agent did come back.
return .succeeded
case (.pending, .liveProcessFound):
return .succeeded
case (.pending, .commandEnded):
return .failed(.exitedBeforeStart)
case (.pending, .deadline(let hasLiveProcess)):
// Some agents have no hooks; a live process is enough.
return hasLiveProcess ? .succeeded : .failed(.didNotStart)
case (.succeeded, _), (.failed, _):
return self
}
}

var failureReason: AgentWakeFailureReason? {
if case .failed(let reason) = self { return reason }
return nil
}
}
98 changes: 98 additions & 0 deletions Sources/Panels/AgentWakeFailureBanner.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import AppKit
import SwiftUI

/// Shown above the terminal when an agent woken from hibernation did not come back.
struct AgentWakeFailureBanner: View {
let failure: AgentWakeFailure
let onRetry: () -> Void
let onDismiss: () -> Void

@State private var showsCommand = false

var body: some View {
VStack(alignment: .leading, spacing: 6) {
HStack(alignment: .top, spacing: 8) {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundStyle(.orange)
.accessibilityHidden(true)
VStack(alignment: .leading, spacing: 2) {
Text(message)
.font(.callout)
.accessibilityIdentifier("agent-wake-failure-message")
Text(failure.reason.detail)
.font(.callout)
.foregroundStyle(.secondary)
.accessibilityIdentifier("agent-wake-failure-detail")
}
.textSelection(.enabled)
Spacer(minLength: 0)
if failure.canRetry {
Button(retryTitle, action: onRetry)
.controlSize(.small)
.accessibilityIdentifier("agent-wake-failure-retry")
}
Button(showsCommand ? hideCommandTitle : showCommandTitle) {
showsCommand.toggle()
}
.controlSize(.small)
.accessibilityIdentifier("agent-wake-failure-show-command")
Button(action: onDismiss) {
Image(systemName: "xmark")
}
.buttonStyle(.borderless)
.help(closeTitle)
.accessibilityLabel(closeTitle)
.accessibilityIdentifier("agent-wake-failure-close")
}
if showsCommand {
HStack(alignment: .top, spacing: 8) {
Text(verbatim: failure.commandText)
.font(.system(.callout, design: .monospaced))
.textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading)
.accessibilityIdentifier("agent-wake-failure-command")
Button(copyTitle) {
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString(failure.commandText, forType: .string)
}
.controlSize(.small)
.accessibilityIdentifier("agent-wake-failure-copy")
}
}
}
.padding(10)
.accessibilityElement(children: .contain)
.accessibilityIdentifier("agent-wake-failure-banner")
}

private var message: String {
String(
format: String(
localized: "agentWake.banner.message",
defaultValue: "%@ didn't resume after waking."
),
failure.agentDisplayName
)
}

private var retryTitle: String {
String(localized: "agentWake.banner.retry", defaultValue: "Retry")
}

private var showCommandTitle: String {
String(localized: "agentWake.banner.showCommand", defaultValue: "Show command")
}

private var hideCommandTitle: String {
String(localized: "agentWake.banner.hideCommand", defaultValue: "Hide command")
}

private var copyTitle: String {
String(localized: "agentWake.banner.copy", defaultValue: "Copy")
}

private var closeTitle: String {
String(localized: "agentWake.banner.close", defaultValue: "Close")
}
}
14 changes: 14 additions & 0 deletions Sources/Panels/TerminalPanel+AgentHibernation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ extension TerminalPanel {
onRequestAgentHibernationTerminationRetry?()
}

/// Banner action: type the resume command again and restart the wake check.
func retryAgentWake() {
onRequestAgentWakeRetry?()
}

/// Banner action: close the wake failure banner.
func dismissAgentWakeFailure() {
if let onDismissAgentWakeFailure {
onDismissAgentWakeFailure()
} else {
agentWakeFailure = nil
}
}

private func suspendRuntimeForAgentHibernation(reason: String) -> Bool {
guard surface.suspendRuntimeSurfaceForAgentHibernation(reason: reason) else {
return false
Expand Down
6 changes: 6 additions & 0 deletions Sources/Panels/TerminalPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ final class TerminalPanel: Panel, ObservableObject {
@Published var viewReattachToken: UInt64 = 0

@Published var agentHibernationPhase: AgentHibernationPanelPhase = .live
/// Set when an agent woken from hibernation did not come back; drives
/// `AgentWakeFailureBanner`.
@Published var agentWakeFailure: AgentWakeFailure?
/// Set by the owning workspace while `agentWakeFailure` is shown.
var onRequestAgentWakeRetry: (() -> Void)?
var onDismissAgentWakeFailure: (() -> Void)?
/// A native cloud pane's live attachment state (nil for local terminals).
/// Written only by the owning cloud session; the view shows it.
var cloudAttachment: CloudTerminalAttachmentStatus?
Expand Down
7 changes: 7 additions & 0 deletions Sources/Panels/TerminalPanelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ struct TerminalPanelView: View {
if let recovery = panel.restoreRecovery.state {
AgentRestoreRecoveryView(state: recovery)
}
if let wakeFailure = panel.agentWakeFailure {
AgentWakeFailureBanner(
failure: wakeFailure,
onRetry: { panel.retryAgentWake() },
onDismiss: { panel.dismissAgentWakeFailure() }
)
}
// Layering contract: terminal find UI is mounted in GhosttySurfaceScrollView (AppKit portal layer)
// via `searchState`. Rendering `SurfaceSearchOverlay` in this SwiftUI container can hide it.
GhosttyTerminalView(
Expand Down
7 changes: 7 additions & 0 deletions Sources/Workspace+AgentLifecycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ extension Workspace {
markRestoredAgentCompleted(panelId: panelId, snapshot: restoredAgent)
restoredResumeSessionWorkingDirectoriesByPanelId.removeValue(forKey: panelId)
retireAgentHookResumeBinding(panelId: panelId, matching: restoredAgent)
// Fails a pending wake check that no agent hook confirmed.
noteAgentWakeCommandEnded(panelId: panelId)
case .some(.awaitingAutoResumeCommand), .some(.manualResumeAvailable),
.some(.completedAgentExit), nil:
// The terminal owns prompt-ready startup input delivery.
Expand Down Expand Up @@ -499,6 +501,11 @@ extension Workspace {
agentLifecycleStatesByPanelId[targetPanelId, default: [:]][key] = lifecycle
if !AgentHibernationLifecycleStatusKeys.isManualKey(key) {
recordAgentLifecycleChange(panelId: targetPanelId)
// Wake confirmation needs the report's own pane, never the
// focused-pane fallback.
if let panelId {
noteAgentWakeAgentReported(panelId: panelId, statusKey: key)
}
}
}

Expand Down
Loading
Loading