Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
240 changes: 138 additions & 102 deletions Sources/ContentView+ForkAgentConversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,136 +23,172 @@ 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 {
clearCommandPaletteForkableAgentCache(panelKey: 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
commandPaletteForkableAgentResultHadFallbackByPanelKey[panelKey] = fallbackFingerprint != nil

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 {
clearCommandPaletteForkableAgentCache(panelKey: panelKey)
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 {
clearCommandPaletteForkableAgentCache(panelKey: panelKey)
NSSound.beep()
return
}
Comment thread
lawrencecchen marked this conversation as resolved.
Resolved
}

private func clearCommandPaletteForkableAgentCache(panelKey: String) {
commandPaletteForkableAgentSupportedPanelKeys.remove(panelKey)
commandPaletteForkableAgentSnapshotsByPanelKey.removeValue(forKey: panelKey)
commandPaletteForkableAgentSnapshotFingerprintsByPanelKey.removeValue(forKey: panelKey)
commandPaletteForkableAgentRemoteContextsByPanelKey.removeValue(forKey: panelKey)
commandPaletteForkableAgentResultHadFallbackByPanelKey.removeValue(forKey: panelKey)
}
}

extension ContentView {
static func commandPaletteForkExecutionSnapshot(
indexSnapshot: SessionRestorableAgentSnapshot?,
static func commandPaletteImmediateForkExecutionSnapshot(
workspaceId: UUID,
panelId: UUID,
isRemoteTerminal: Bool,
supportedPanelKeys: Set<String>,
supportedRemoteContextsByPanelKey: [String: Bool],
snapshotFingerprintsByPanelKey: [String: String],
fallbackSnapshot: SessionRestorableAgentSnapshot?,
cachedSnapshot: SessionRestorableAgentSnapshot?
) -> SessionRestorableAgentSnapshot? {
indexSnapshot ?? fallbackSnapshot ?? cachedSnapshot
}
let panelKey = commandPaletteForkableAgentPanelKey(
workspaceId: workspaceId,
panelId: panelId
)
func verifiedCachedSnapshot(expectedFingerprint: String?) -> SessionRestorableAgentSnapshot? {
guard let cachedSnapshot,
supportedPanelKeys.contains(panelKey),
supportedRemoteContextsByPanelKey[panelKey] == isRemoteTerminal else {
return nil
}
if let expectedFingerprint,
snapshotFingerprintsByPanelKey[panelKey] != expectedFingerprint {
return nil
}
guard commandPaletteSnapshotForkAvailability(
cachedSnapshot,
isRemoteTerminal: isRemoteTerminal
) != .unsupported else {
return nil
}
return 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
if let fallbackSnapshot {
let fallbackFingerprint = commandPaletteForkSnapshotFingerprint(fallbackSnapshot)
switch commandPaletteSnapshotForkAvailability(
fallbackSnapshot,
isRemoteTerminal: isRemoteTerminal
) {
case .supportedWithoutProbe:
return verifiedCachedSnapshot(expectedFingerprint: fallbackFingerprint) ?? fallbackSnapshot
case .unsupported:
return nil
case .requiresProbe:
guard commandPaletteForkableAgentProbeResultMatches(
panelKey: panelKey,
supportedPanelKeys: supportedPanelKeys,
supportedRemoteContextsByPanelKey: supportedRemoteContextsByPanelKey,
snapshotFingerprintsByPanelKey: snapshotFingerprintsByPanelKey,
expectedSnapshotFingerprint: fallbackFingerprint,
isRemoteTerminal: isRemoteTerminal
) else {
return nil
}
return verifiedCachedSnapshot(expectedFingerprint: fallbackFingerprint) ?? fallbackSnapshot
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
}

guard let cachedSnapshot = verifiedCachedSnapshot(expectedFingerprint: nil) else {
return nil
}
switch commandPaletteSnapshotForkAvailability(
cachedSnapshot,
isRemoteTerminal: isRemoteTerminal
) {
case .supportedWithoutProbe, .requiresProbe:
return cachedSnapshot
case .unsupported:
return nil
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
}
}

Expand Down
Loading
Loading