Skip to content
Closed
33 changes: 33 additions & 0 deletions Sources/AgentHibernation/AgentHibernationLifecycleState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@ enum AgentHibernationLifecycleState: String, Codable, Sendable, Equatable, CaseI
parse(rawValue)
}

static func aggregate(
statusKeyedStates: [String: AgentHibernationLifecycleState],
fallback: AgentHibernationLifecycleState?
) -> AgentHibernationLifecycleState {
let states = statusKeyedStates
.filter { !AgentHibernationLifecycleStatusKeys.isManualKey($0.key) }
.map(\.value)
guard !states.isEmpty else {
return fallback ?? .unknown
}
if states.contains(.running) { return .running }
if states.contains(.needsInput) { return .needsInput }
if states.contains(.unknown) { return .unknown }
if states.contains(.idle) { return .idle }
return fallback ?? .unknown
}

/// Aggregates only built-in agent keys for TextBox Escape authorization.
/// Hibernation uses `aggregate` so Feed overlays and registered Vault agents
/// remain part of its safety state; Escape passthrough intentionally fails
/// closed for keys that have not been validated as built-in agents.
static func aggregateForTextBoxEscape(
statusKeyedStates: [String: AgentHibernationLifecycleState]
) -> AgentHibernationLifecycleState {
aggregate(
statusKeyedStates: statusKeyedStates.filter {
AgentHibernationLifecycleStatusKeys.isAllowed($0.key)
},
fallback: nil
)
}

private static func parse(_ rawValue: String) -> AgentHibernationLifecycleState? {
let normalized = rawValue
.trimmingCharacters(in: .whitespacesAndNewlines)
Expand Down Expand Up @@ -60,6 +92,7 @@ enum AgentHibernationLifecycleStatusKeys {
static let allowedStatusKeys: Set<String> = [
"amp",
"antigravity",
"campfire",
"claude_code",
"codebuddy",
"codex",
Expand Down
16 changes: 16 additions & 0 deletions Sources/DockSplitStore+RestoredAgentLifecycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,22 @@ extension DockSplitStore {
}
}

func agentHibernationLifecycleState(
panelId: UUID,
fallback: AgentHibernationLifecycleState?
) -> AgentHibernationLifecycleState {
AgentHibernationLifecycleState.aggregate(
statusKeyedStates: agentRuntimeByPanelId[panelId]?.agentLifecycleStates ?? [:],
fallback: fallback
)
}

func agentLifecycleStateForTextBoxEscape(panelId: UUID) -> AgentHibernationLifecycleState {
AgentHibernationLifecycleState.aggregateForTextBoxEscape(
statusKeyedStates: agentRuntimeByPanelId[panelId]?.agentLifecycleStates ?? [:]
)
}

@discardableResult
func clearAgentLifecycle(key: String, panelId: UUID) -> Bool {
var didClear = false
Expand Down
11 changes: 11 additions & 0 deletions Sources/Panels/TerminalPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ final class TerminalPanel: Panel, ObservableObject {
}

func handleTextBoxEscape() {
if containerAgentLifecycleStateForTextBoxEscape == .running {
_ = sendNamedKeyResult(TextBoxTerminalKey.escape.rawValue)
}
let hadTextBoxView = textBoxInputView != nil
let didFocusTerminal = focusTerminalSurface(
respectForeignFirstResponder: false,
Expand All @@ -348,6 +351,14 @@ final class TerminalPanel: Panel, ObservableObject {
shouldHideTextBoxOnNextEscape = isTextBoxActive && (hadTextBoxView || didFocusTerminal)
}

private var containerAgentLifecycleStateForTextBoxEscape: AgentHibernationLifecycleState {
if let dock = DockSplitStore.liveStore(containingPanel: id) {
return dock.agentLifecycleStateForTextBoxEscape(panelId: id)
}
return surface.owningWorkspace()?
.agentLifecycleStateForTextBoxEscape(panelId: id) ?? .unknown
}

@discardableResult
func consumeTextBoxHideEscapeIfArmed(in window: NSWindow?) -> Bool {
guard isTextBoxActive,
Expand Down
21 changes: 10 additions & 11 deletions Sources/Workspace+AgentLifecycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -580,17 +580,16 @@ extension Workspace {
panelId: UUID,
fallback: AgentHibernationLifecycleState?
) -> AgentHibernationLifecycleState {
let states = (agentLifecycleStatesByPanelId[panelId] ?? [:])
.filter { !AgentHibernationLifecycleStatusKeys.isManualKey($0.key) }
.map(\.value)
guard !states.isEmpty else {
return fallback ?? .unknown
}
if states.contains(.running) { return .running }
if states.contains(.needsInput) { return .needsInput }
if states.contains(.unknown) { return .unknown }
if states.contains(.idle) { return .idle }
return fallback ?? .unknown
AgentHibernationLifecycleState.aggregate(
statusKeyedStates: agentLifecycleStatesByPanelId[panelId] ?? [:],
fallback: fallback
)
}

func agentLifecycleStateForTextBoxEscape(panelId: UUID) -> AgentHibernationLifecycleState {
AgentHibernationLifecycleState.aggregateForTextBoxEscape(
statusKeyedStates: agentLifecycleStatesByPanelId[panelId] ?? [:]
)
}

private func recordAgentLifecycleChange(panelId: UUID) {
Expand Down
4 changes: 4 additions & 0 deletions cmux.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2509,6 +2509,7 @@ C0DE71B10000000000000001 /* AppDelegate+AgentChatNotifications.swift in Sources
B7758A020000000000000001 /* TerminationWatchdogTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7758A020000000000000002 /* TerminationWatchdogTests.swift */; };
C0DE7B330000000000000001 /* TextBoxAgentDetection.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0DE7B330000000000000002 /* TextBoxAgentDetection.swift */; };
B8835100000000000000000D /* TextBoxAttachment+PreparedPaste.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8835100000000000000000E /* TextBoxAttachment+PreparedPaste.swift */; };
970400000000000000000001 /* TextBoxEscapePassthroughTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 970400000000000000000002 /* TextBoxEscapePassthroughTests.swift */; };
7898A0020000000000000001 /* TextBoxGitIgnoreProbe.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7898A0020000000000000002 /* TextBoxGitIgnoreProbe.swift */; };
C0DE86250000000000000001 /* TextBoxIMECompositionLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0DE86250000000000000002 /* TextBoxIMECompositionLayoutTests.swift */; };
5330B0715330B0715330B071 /* TextBoxInlineAttachmentCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5330B0725330B0725330B072 /* TextBoxInlineAttachmentCell.swift */; };
Expand Down Expand Up @@ -5313,6 +5314,7 @@ B8B056D80000000000000002 /* MobileHostIdentityTests.swift */ = {isa = PBXFileRef
B7758A020000000000000002 /* TerminationWatchdogTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminationWatchdogTests.swift; sourceTree = "<group>"; };
C0DE7B330000000000000002 /* TextBoxAgentDetection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextBoxAgentDetection.swift; sourceTree = "<group>"; };
B8835100000000000000000E /* TextBoxAttachment+PreparedPaste.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TextBoxAttachment+PreparedPaste.swift"; sourceTree = "<group>"; };
970400000000000000000002 /* TextBoxEscapePassthroughTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextBoxEscapePassthroughTests.swift; sourceTree = "<group>"; };
7898A0020000000000000002 /* TextBoxGitIgnoreProbe.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextBoxGitIgnoreProbe.swift; sourceTree = "<group>"; };
C0DE86250000000000000002 /* TextBoxIMECompositionLayoutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextBoxIMECompositionLayoutTests.swift; sourceTree = "<group>"; };
5330B0725330B0725330B072 /* TextBoxInlineAttachmentCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextBoxInlineAttachmentCell.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -8023,6 +8025,7 @@ B8B056D80000000000000002 /* MobileHostIdentityTests.swift */ = {isa = PBXFileRef
5330A0025330A0025330A002 /* TextBoxInlineAttachmentRenderingTests.swift */,
C0DE7B300000000000000002 /* TextBoxMentionCompletionTests.swift */,
C0DE86250000000000000002 /* TextBoxIMECompositionLayoutTests.swift */,
970400000000000000000002 /* TextBoxEscapePassthroughTests.swift */,
9005A0010000000000000002 /* TextBoxSelectionReplacementTests.swift */,
7898A0010000000000000002 /* TextBoxMentionGitIgnoreProbeTests.swift */,
C1713005C1713005C1713005 /* CommandPaletteShortcutCustomizationTests.swift */,
Expand Down Expand Up @@ -11760,6 +11763,7 @@ B8B056D80000000000000002 /* MobileHostIdentityTests.swift */ = {isa = PBXFileRef
AA11BB22CC33DD44EE55F002 /* TerminalWindowPortalLayoutPassRefreshTests.swift in Sources */,
C95BAC3D37A6111B487582DC /* TerminalWindowPortalLifecycleHiddenRefreshTests.swift in Sources */,
B7758A020000000000000001 /* TerminationWatchdogTests.swift in Sources */,
970400000000000000000001 /* TextBoxEscapePassthroughTests.swift in Sources */,
C0DE86250000000000000001 /* TextBoxIMECompositionLayoutTests.swift in Sources */,
5330A0015330A0015330A001 /* TextBoxInlineAttachmentRenderingTests.swift in Sources */,
C0DE7B300000000000000001 /* TextBoxMentionCompletionTests.swift in Sources */,
Expand Down
Loading
Loading