-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add shell-free cmux restore verb #9265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
45c5634
test: require short CLI restore startup input
a71ddad
feat: restore processes from structured launch data
12d8fd1
fix: prefer live restore binding identity
d5a8b52
fix: route restore through bundled CLI
b2cb818
fix: harden restore startup dispatch
8c8169e
fix: reconcile structured restore overrides
a92b409
fix: harden restore binding and cwd fallback
c24668a
fix: recover restore context without shell env
a5733fe
fix: secure restore transport failures
ade794a
Merge remote-tracking branch 'origin/main' into issue-9258-cmux-resto…
14061df
fix: harden structured restore boundaries
32bfc0a
test: cover restore startup compatibility gaps
426b7d4
fix: type restore cwd fallback explicitly
1a6344c
fix: call restore launch mapper explicitly
ea33240
fix: name restore socket responder distinctly
ad9a4f6
fix: preserve restore startup compatibility
8505f31
fix: ignore empty restore PATH components
453c3d3
fix: bound restore provider preflights
55aa8de
refactor: isolate restore preflight execution
d77e0a9
Merge remote-tracking branch 'origin/main' into issue-9258-cmux-resto…
1741acb
fix: harden restore launch boundaries
a5a582d
Merge branch 'main' of https://github.com/manaflow-ai/cmux into issue…
4a42b04
test: require readable restore startup verb
4d34098
fix: keep restore startup input readable
db84f67
test: cover restore review edge cases
e556138
fix: address restore review edge cases
b699f2f
Merge remote-tracking branch 'origin/main' into issue-9258-cmux-resto…
e52ae8e
refactor: align restore types with package policy
c43c140
test: cover restore compatibility review gaps
11c85cf
fix: close restore review edge cases
4d75d3a
Merge remote-tracking branch 'origin/main' into issue-9258-cmux-resto…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,296 @@ | ||
| import CMUXAgentLaunch | ||
| import Foundation | ||
|
|
||
| extension CMUXCLI { | ||
| func controlAgentLaunchCommandPayload( | ||
| _ command: AgentLaunchCommand | ||
| ) -> [String: Any] { | ||
| var payload: [String: Any] = ["arguments": command.arguments] | ||
| if let launcher = command.launcher { | ||
| payload["launcher"] = launcher | ||
| } | ||
| if let executablePath = command.executablePath { | ||
| payload["executable_path"] = executablePath | ||
| } | ||
| if let workingDirectory = command.workingDirectory { | ||
| payload["working_directory"] = workingDirectory | ||
| } | ||
| if let environment = command.environment { | ||
| payload["environment"] = environment | ||
| } | ||
| if let capturedAt = command.capturedAt { | ||
| payload["captured_at"] = capturedAt | ||
| } | ||
| if let source = command.source { | ||
| payload["source"] = source | ||
| } | ||
| return payload | ||
| } | ||
|
|
||
| func runRestoreCommand( | ||
| commandArgs: [String], | ||
| client: SocketClient, | ||
| processEnvironment: [String: String] | ||
| ) throws { | ||
| let selector = try restoreSelector(commandArgs) | ||
| var params: [String: Any] = [:] | ||
| if let surface = selector.surface { | ||
| let surfaceID = try normalizeSurfaceHandle( | ||
| surface, | ||
| client: client, | ||
| workspaceHandle: nil, | ||
| windowHandle: nil | ||
| ) | ||
| guard let surfaceID else { | ||
| throw loggedRestoreError( | ||
| stage: "surface.lookup", | ||
| detail: surface, | ||
| message: String( | ||
| localized: "cli.restore.error.surfaceNotFound", | ||
| defaultValue: "restore: the requested surface was not found. Check the surface reference, then retry." | ||
| ) | ||
| ) | ||
| } | ||
| params["surface_id"] = surfaceID | ||
| } else if selector.usesCurrentSurface, | ||
| let surfaceID = processEnvironment["CMUX_SURFACE_ID"], | ||
| !surfaceID.isEmpty { | ||
| params["surface_id"] = surfaceID | ||
| } else if selector.usesCurrentSurface, | ||
| let ttyName = resolveCallerTTYName(), | ||
| let caller = resolveTerminalBinding( | ||
| ttyName: ttyName, | ||
| client: client | ||
| ) { | ||
| params["surface_id"] = caller.surfaceId | ||
| } else { | ||
| throw CLIError( | ||
| message: String( | ||
| localized: "cli.restore.error.currentSurfaceUnknown", | ||
| defaultValue: "restore: the current cmux surface could not be identified. Retry from this terminal or pass --surface <id|ref>." | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| let payload = try client.sendV2(method: "surface.resume.get", params: params) | ||
| guard let rawRecord = payload["restore_record"] as? [String: Any] else { | ||
| throw loggedRestoreError( | ||
| stage: "record.missing", | ||
| message: String( | ||
| localized: "cli.restore.error.noRecord", | ||
| defaultValue: "restore: this session has nothing to restore. Start the agent again in this terminal." | ||
| ) | ||
| ) | ||
| } | ||
| let record = try restoreRecord(from: rawRecord) | ||
| if let expectedKind = selector.kind, expectedKind != record.kind { | ||
| throw loggedRestoreError( | ||
| stage: "record.kind-mismatch", | ||
| detail: "expected=\(expectedKind) actual=\(record.kind)", | ||
| message: String( | ||
| localized: "cli.restore.error.kindMismatch", | ||
| defaultValue: "restore: this command no longer matches the session. Run 'cmux restore --surface' to use the current record." | ||
| ) | ||
| ) | ||
| } | ||
| if let expectedCheckpointID = selector.checkpointID, | ||
| expectedCheckpointID != record.checkpointID { | ||
| throw loggedRestoreError( | ||
| stage: "record.checkpoint-mismatch", | ||
| detail: "expected=\(expectedCheckpointID) actual=\(record.checkpointID ?? "none")", | ||
| message: String( | ||
| localized: "cli.restore.error.checkpointMismatch", | ||
| defaultValue: "restore: this command no longer matches the session. Run 'cmux restore --surface' to use the current record." | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| let environment = processEnvironment.merging(record.environment) { _, restored in | ||
| restored | ||
| } | ||
| if record.launchCommand == nil, | ||
| record.preparedArguments == nil, | ||
| let legacyCommand = record.legacyCommand { | ||
| try execLegacyRestoreRecord( | ||
| legacyCommand, | ||
| record: record, | ||
| environment: environment, | ||
| client: client | ||
| ) | ||
| } | ||
|
|
||
| guard let mode = AgentRestoreRequestMode(rawValue: record.mode) else { | ||
| throw loggedRestoreError( | ||
| stage: "record.mode", | ||
| detail: record.mode, | ||
| message: String( | ||
| localized: "cli.restore.error.unsupportedMode", | ||
| defaultValue: "restore: this session's saved restore data is not compatible. Start the agent again in this terminal." | ||
| ) | ||
| ) | ||
| } | ||
| let requestedWorkingDirectory = requestedRestoreWorkingDirectory(for: record) | ||
| let appliedWorkingDirectory = try applyRestoreWorkingDirectory( | ||
| requestedWorkingDirectory | ||
| ) | ||
| let effectiveWorkingDirectory: String? = | ||
| if requestedWorkingDirectory?.isEmpty == false { | ||
| appliedWorkingDirectory ?? FileManager.default.currentDirectoryPath | ||
| } else { | ||
| nil | ||
| } | ||
| let request = AgentRestoreRequest( | ||
| mode: mode, | ||
| kind: record.kind, | ||
| checkpointID: record.checkpointID, | ||
| source: record.source, | ||
| workingDirectory: effectiveWorkingDirectory, | ||
| environment: record.environment, | ||
| launchCommand: record.launchCommand, | ||
| preparedArguments: record.preparedArguments, | ||
| preparedArgumentsWorkingDirectory: normalizedRestoreWorkingDirectory( | ||
| record.preparedArgumentsWorkingDirectory | ||
| ), | ||
| observedPermissionMode: record.permissionMode | ||
| ) | ||
| guard let invocation = AgentRestorePlanner( | ||
| executableFileResolver: AgentRestoreExecutableFileResolver() | ||
| ).invocation( | ||
| for: request, | ||
| ambientEnvironment: processEnvironment | ||
| ) else { | ||
| if let legacyCommand = record.legacyCommand { | ||
| try execLegacyRestoreRecord( | ||
| legacyCommand, | ||
| record: record, | ||
| environment: environment, | ||
| client: client | ||
| ) | ||
| } | ||
| throw loggedRestoreError( | ||
| stage: "record.incomplete", | ||
| detail: "mode=\(record.mode) kind=\(record.kind)", | ||
| message: String( | ||
| localized: "cli.restore.error.incompleteData", | ||
| defaultValue: "restore: this session's saved restore data is not compatible. Start the agent again in this terminal." | ||
| ) | ||
| ) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| for preflight in invocation.preflightInvocations { | ||
| try runRestorePreflight( | ||
| preflight, | ||
| appliedWorkingDirectory: effectiveWorkingDirectory | ||
| ) | ||
| } | ||
| client.close() | ||
| try execRestoreInvocation( | ||
| invocation, | ||
| appliedWorkingDirectory: effectiveWorkingDirectory | ||
| ) | ||
| } | ||
|
|
||
| private func restoreSelector(_ arguments: [String]) throws -> RestoreSelector { | ||
| if arguments.first == "--surface" { | ||
| if arguments.count == 1 { | ||
| return RestoreSelector( | ||
| surface: nil, | ||
| usesCurrentSurface: true, | ||
| kind: nil, | ||
| checkpointID: nil | ||
| ) | ||
| } | ||
| guard arguments.count == 2, !arguments[1].isEmpty else { | ||
| throw CLIError(message: String( | ||
| localized: "cli.restore.usage.surface", | ||
| defaultValue: "Usage: cmux restore --surface [id|ref]" | ||
| )) | ||
| } | ||
| return RestoreSelector( | ||
| surface: arguments[1], | ||
| usesCurrentSurface: false, | ||
| kind: nil, | ||
| checkpointID: nil | ||
| ) | ||
| } | ||
| guard arguments.count == 2, | ||
| !arguments[0].trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, | ||
| !arguments[1].trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { | ||
| throw CLIError(message: String( | ||
| localized: "cli.restore.usage.positional", | ||
| defaultValue: "Usage: cmux restore <kind> <checkpoint-id>" | ||
| )) | ||
| } | ||
| return RestoreSelector( | ||
| surface: nil, | ||
| usesCurrentSurface: true, | ||
| kind: arguments[0], | ||
| checkpointID: arguments[1] | ||
| ) | ||
| } | ||
|
|
||
| private func restoreRecord(from object: [String: Any]) throws -> RestoreRecord { | ||
| guard let mode = object["mode"] as? String, | ||
| let kind = object["kind"] as? String else { | ||
| throw loggedRestoreError( | ||
| stage: "record.decode", | ||
| detail: "keys=\(object.keys.sorted().joined(separator: ","))", | ||
| message: String( | ||
| localized: "cli.restore.error.malformedRecord", | ||
| defaultValue: "restore: this session's saved restore data is not compatible. Start the agent again in this terminal." | ||
| ) | ||
| ) | ||
| } | ||
| let legacyCommand = object["legacy_command"] as? String | ||
| let launchCommand: AgentLaunchCommand? | ||
| do { | ||
| launchCommand = try restoreLaunchCommand(from: object["launch_command"]) | ||
| } catch { | ||
| guard legacyCommand != nil else { | ||
| throw loggedRestoreError( | ||
| stage: "record.launch-command", | ||
| detail: String(reflecting: type(of: error)), | ||
| message: String( | ||
| localized: "cli.restore.error.malformedArguments", | ||
| defaultValue: "restore: this session's saved restore data is not compatible. Start the agent again in this terminal." | ||
| ) | ||
| ) | ||
| } | ||
| launchCommand = nil | ||
| } | ||
| return RestoreRecord( | ||
| mode: mode, | ||
| kind: kind, | ||
| checkpointID: object["checkpoint_id"] as? String, | ||
| source: object["source"] as? String, | ||
| workingDirectory: object["working_directory"] as? String, | ||
| environment: object["environment"] as? [String: String] ?? [:], | ||
| launchCommand: launchCommand, | ||
| preparedArguments: object["prepared_arguments"] as? [String], | ||
| preparedArgumentsWorkingDirectory: | ||
| object["prepared_arguments_working_directory"] as? String, | ||
| permissionMode: object["permission_mode"] as? String, | ||
| legacyCommand: legacyCommand | ||
| ) | ||
| } | ||
|
|
||
| private func restoreLaunchCommand(from value: Any?) throws -> AgentLaunchCommand? { | ||
| guard let object = value as? [String: Any] else { return nil } | ||
| guard let arguments = object["arguments"] as? [String], !arguments.isEmpty else { | ||
| throw CLIError(message: String( | ||
| localized: "cli.restore.error.malformedArguments", | ||
| defaultValue: "restore: this session's saved restore data is not compatible. Start the agent again in this terminal." | ||
| )) | ||
| } | ||
| return AgentLaunchCommand( | ||
| launcher: object["launcher"] as? String, | ||
| executablePath: object["executable_path"] as? String, | ||
| arguments: arguments, | ||
| workingDirectory: object["working_directory"] as? String, | ||
| environment: object["environment"] as? [String: String], | ||
| capturedAt: (object["captured_at"] as? NSNumber)?.doubleValue, | ||
| source: object["source"] as? String | ||
| ) | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1:
cmux restore --surface surface:<n>fails withinvalid_paramsinstead of restoring the selected surface because the ref is forwarded assurface_idrather than resolved to its UUID. Resolving explicit refs/indices to a UUID before callingsurface.resume.getwould make the advertisedid|refform work.Prompt for AI agents