Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
201 changes: 103 additions & 98 deletions Sources/ContentView+ForkAgentConversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,110 +23,89 @@ extension ContentView {
}

private func forkFocusedAgentConversation(_ destination: AgentConversationForkDestination) {
guard let initialContext = focusedPanelContext,
initialContext.panel.panelType == .terminal else {
guard let currentContext = focusedPanelContext,
currentContext.panel.panelType == .terminal else {
NSSound.beep()
return
}

let workspaceId = initialContext.workspace.id
let panelId = initialContext.panelId
let workspaceId = currentContext.workspace.id
let panelId = currentContext.panelId
let panelKey = Self.commandPaletteForkableAgentPanelKey(
workspaceId: workspaceId,
panelId: panelId
)

Task { @MainActor in
let index = await RestorableAgentSessionIndex.loadIncludingProcessDetectedSnapshots()
guard let currentContext = focusedPanelContext,
currentContext.workspace.id == workspaceId,
currentContext.panelId == panelId,
currentContext.panel.panelType == .terminal else {
NSSound.beep()
return
}
let fallbackSnapshot = currentContext.workspace.restoredAgentSnapshotsByPanelId[panelId]
let isRemoteContext = currentContext.workspace.isRemoteTerminalSurface(panelId)
let snapshot = Self.commandPaletteImmediateForkExecutionSnapshot(
workspaceId: workspaceId,
panelId: panelId,
isRemoteTerminal: isRemoteContext,
supportedPanelKeys: commandPaletteForkableAgentSupportedPanelKeys,
supportedRemoteContextsByPanelKey: commandPaletteForkableAgentRemoteContextsByPanelKey,
snapshotFingerprintsByPanelKey: commandPaletteForkableAgentSnapshotFingerprintsByPanelKey,
fallbackSnapshot: fallbackSnapshot,
cachedSnapshot: commandPaletteForkableAgentSnapshotsByPanelKey[panelKey]
)
guard let snapshot else {
commandPaletteForkableAgentSupportedPanelKeys.remove(panelKey)
commandPaletteForkableAgentSnapshotsByPanelKey.removeValue(forKey: panelKey)
commandPaletteForkableAgentSnapshotFingerprintsByPanelKey.removeValue(forKey: panelKey)
commandPaletteForkableAgentRemoteContextsByPanelKey.removeValue(forKey: panelKey)
NSSound.beep()
return
}

let snapshot = Self.commandPaletteForkExecutionSnapshot(
indexSnapshot: index.snapshot(workspaceId: workspaceId, panelId: panelId),
fallbackSnapshot: currentContext.workspace.restoredAgentSnapshotsByPanelId[panelId],
cachedSnapshot: commandPaletteForkableAgentSnapshotsByPanelKey[panelKey]
)
guard let snapshot else {
commandPaletteForkableAgentSupportedPanelKeys.remove(panelKey)
commandPaletteForkableAgentSnapshotsByPanelKey.removeValue(forKey: panelKey)
commandPaletteForkableAgentSnapshotFingerprintsByPanelKey.removeValue(forKey: panelKey)
NSSound.beep()
return
}
let isRemoteContext = currentContext.workspace.isRemoteTerminalSurface(panelId)
guard await AgentForkSupport.supportsFork(
let fallbackFingerprint = fallbackSnapshot.map(Self.commandPaletteForkSnapshotFingerprint)
commandPaletteForkableAgentSupportedPanelKeys.insert(panelKey)
commandPaletteForkableAgentSnapshotsByPanelKey[panelKey] = snapshot
commandPaletteForkableAgentSnapshotFingerprintsByPanelKey[panelKey] = Self.commandPaletteForkCacheFingerprint(
snapshot: snapshot,
fallbackFingerprint: fallbackFingerprint
)
commandPaletteForkableAgentRemoteContextsByPanelKey[panelKey] = isRemoteContext

let didFork: Bool
switch destination {
case .split(let direction):
didFork = currentContext.workspace.forkAgentConversation(
fromPanelId: panelId,
snapshot: snapshot,
isRemoteContext: isRemoteContext
direction: direction
) != nil
case .newWorkspace:
guard let launch = currentContext.workspace.forkAgentWorkspaceLaunch(
fromPanelId: panelId,
snapshot: snapshot
) else {
NSSound.beep()
return
}
Comment thread
lawrencecchen marked this conversation as resolved.
Resolved
guard let postProbeContext = focusedPanelContext,
Self.commandPaletteForkPostProbeContextStillMatches(
expectedWorkspaceId: workspaceId,
expectedPanelId: panelId,
expectedIsRemoteContext: isRemoteContext,
currentWorkspaceId: postProbeContext.workspace.id,
currentPanelId: postProbeContext.panelId,
currentPanelIsTerminal: postProbeContext.panel.panelType == .terminal,
currentIsRemoteContext: postProbeContext.workspace.isRemoteTerminalSurface(panelId)
) else {
NSSound.beep()
return
}
commandPaletteForkableAgentSupportedPanelKeys.insert(
panelKey
let forkWorkspace = tabManager.addWorkspace(
workingDirectory: launch.terminalWorkingDirectory,
initialTerminalCommand: launch.initialTerminalCommand,
initialTerminalInput: launch.initialTerminalInput,
inheritWorkingDirectory: launch.terminalWorkingDirectory != nil,
autoWelcomeIfNeeded: false
)
commandPaletteForkableAgentSnapshotsByPanelKey[panelKey] = snapshot
commandPaletteForkableAgentSnapshotFingerprintsByPanelKey[panelKey] = Self.commandPaletteForkSnapshotFingerprint(snapshot)
commandPaletteForkableAgentRemoteContextsByPanelKey[panelKey] = isRemoteContext

let didFork: Bool
switch destination {
case .split(let direction):
didFork = postProbeContext.workspace.forkAgentConversation(
fromPanelId: panelId,
snapshot: snapshot,
direction: direction
) != nil
case .newWorkspace:
guard let launch = postProbeContext.workspace.forkAgentWorkspaceLaunch(
fromPanelId: panelId,
snapshot: snapshot
) else {
NSSound.beep()
return
}
let forkWorkspace = tabManager.addWorkspace(
workingDirectory: launch.terminalWorkingDirectory,
initialTerminalCommand: launch.initialTerminalCommand,
initialTerminalInput: launch.initialTerminalInput,
inheritWorkingDirectory: launch.terminalWorkingDirectory != nil,
autoWelcomeIfNeeded: false
if let remoteConfiguration = launch.remoteConfiguration {
forkWorkspace.configureRemoteConnection(
remoteConfiguration,
autoConnect: launch.autoConnectRemoteConfiguration
)
if let remoteConfiguration = launch.remoteConfiguration {
forkWorkspace.configureRemoteConnection(
remoteConfiguration,
autoConnect: launch.autoConnectRemoteConfiguration
)
}
if let workingDirectory = launch.workingDirectory,
launch.terminalWorkingDirectory == nil,
let forkPanelId = forkWorkspace.focusedPanelId {
forkWorkspace.updatePanelDirectory(panelId: forkPanelId, directory: workingDirectory)
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
didFork = true
}

guard didFork else {
NSSound.beep()
return
if let workingDirectory = launch.workingDirectory,
launch.terminalWorkingDirectory == nil,
let forkPanelId = forkWorkspace.focusedPanelId {
forkWorkspace.updatePanelDirectory(panelId: forkPanelId, directory: workingDirectory)
}
didFork = true
}

guard didFork else {
NSSound.beep()
return
}
Comment thread
lawrencecchen marked this conversation as resolved.
Resolved
}
}
Expand All @@ -140,19 +119,45 @@ extension ContentView {
indexSnapshot ?? fallbackSnapshot ?? cachedSnapshot
}

static func commandPaletteForkPostProbeContextStillMatches(
expectedWorkspaceId: UUID,
expectedPanelId: UUID,
expectedIsRemoteContext: Bool,
currentWorkspaceId: UUID,
currentPanelId: UUID,
currentPanelIsTerminal: Bool,
currentIsRemoteContext: Bool
) -> Bool {
currentWorkspaceId == expectedWorkspaceId
&& currentPanelId == expectedPanelId
&& currentPanelIsTerminal
&& currentIsRemoteContext == expectedIsRemoteContext
static func commandPaletteImmediateForkExecutionSnapshot(
workspaceId: UUID,
panelId: UUID,
isRemoteTerminal: Bool,
supportedPanelKeys: Set<String>,
supportedRemoteContextsByPanelKey: [String: Bool],
snapshotFingerprintsByPanelKey: [String: String],
fallbackSnapshot: SessionRestorableAgentSnapshot?,
cachedSnapshot: SessionRestorableAgentSnapshot?
) -> SessionRestorableAgentSnapshot? {
let panelKey = commandPaletteForkableAgentPanelKey(
workspaceId: workspaceId,
panelId: panelId
)
guard let snapshot = commandPaletteForkExecutionSnapshot(
indexSnapshot: nil,
fallbackSnapshot: fallbackSnapshot,
cachedSnapshot: cachedSnapshot
) else {
return nil
}

switch commandPaletteSnapshotForkAvailability(snapshot, isRemoteTerminal: isRemoteTerminal) {
case .supportedWithoutProbe:
return snapshot
case .unsupported:
return nil
case .requiresProbe:
guard supportedPanelKeys.contains(panelKey),
supportedRemoteContextsByPanelKey[panelKey] == isRemoteTerminal else {
return nil
}
let expectedFingerprint = (fallbackSnapshot ?? cachedSnapshot).map(commandPaletteForkSnapshotFingerprint)
guard let expectedFingerprint,
snapshotFingerprintsByPanelKey[panelKey] == expectedFingerprint else {
return nil
}
return cachedSnapshot ?? snapshot
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5858,13 +5858,13 @@ struct ContentView: View {
"\(workspaceId.uuidString):\(panelId.uuidString)"
}

private enum CommandPaletteForkSnapshotAvailability {
enum CommandPaletteForkSnapshotAvailability {
case unsupported
case supportedWithoutProbe
case requiresProbe
}

private static func commandPaletteSnapshotForkAvailability(
static func commandPaletteSnapshotForkAvailability(
_ snapshot: SessionRestorableAgentSnapshot,
isRemoteTerminal: Bool = false
) -> CommandPaletteForkSnapshotAvailability {
Expand Down
Loading
Loading