Repository navigation
Route Vault resume through cmux restore #9924
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 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bfcfac8
test: cover Vault restore-verb resume path
austinywang 63f31dc
fix: route Vault resume through restore verb
austinywang 28638ef
test: cover Vault restore persistence lifecycle
austinywang 8935a7b
fix: persist Vault restores in agent lifecycle
austinywang 7c866e4
refactor: quarantine Vault copy resume commands
austinywang 59dab37
Merge remote-tracking branch 'origin/main' into issue-9923-vault-resu…
austinywang 78b2492
test: cover Vault restore fallback and placement
austinywang 85f460a
test: cover Vault restore cwd lifecycle
austinywang 6c194c4
fix: unify restored-agent cwd lifecycle
austinywang 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
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,222 @@ | ||
| import CMUXAgentLaunch | ||
| import Foundation | ||
|
|
||
| /// The terminal startup plan shared by every Vault resume entry point. | ||
| struct SessionEntryResumeLaunch: Sendable { | ||
| enum Strategy: Sendable, Equatable { | ||
| case restoreVerb | ||
| case legacyCommand | ||
| } | ||
|
|
||
| let strategy: Strategy | ||
| let initialInput: String | ||
| let workingDirectory: String? | ||
| let startupRestoreAgent: SessionRestorableAgentSnapshot? | ||
| } | ||
|
|
||
| private struct SessionEntryResumeSnapshotComponents { | ||
| let arguments: [String] | ||
| let environment: [String: String] | ||
| let registration: CmuxVaultAgentRegistration? | ||
| let permissionMode: String? | ||
| } | ||
|
|
||
| extension SessionEntry { | ||
| /// Builds the same surface-scoped restore selector used by relaunch restore. | ||
| /// | ||
| /// Registered agents deliberately fall back to the quarantined copyable | ||
| /// shell command only when their registration cannot produce structured argv. | ||
| var resumeLaunch: SessionEntryResumeLaunch? { | ||
| guard let snapshot = vaultResumeSnapshot else { | ||
| return legacyResumeLaunch | ||
| } | ||
| if let preparedArguments = snapshot.preparedResumeArguments( | ||
| launchCommand: snapshot.launchCommand, | ||
| workingDirectory: snapshot.workingDirectory, | ||
| observedPermissionMode: snapshot.permissionMode | ||
| ), !preparedArguments.isEmpty, | ||
| let initialInput = snapshot.resumeStartupInput(useLocalRestoreVerb: true) { | ||
| return SessionEntryResumeLaunch( | ||
| strategy: .restoreVerb, | ||
| initialInput: initialInput, | ||
| workingDirectory: resumeWorkingDirectory, | ||
| startupRestoreAgent: snapshot | ||
| ) | ||
| } | ||
|
|
||
| return legacyResumeLaunch | ||
| } | ||
|
|
||
| private var legacyResumeLaunch: SessionEntryResumeLaunch? { | ||
| guard let legacyCommand = copyResumeCommand else { return nil } | ||
| return SessionEntryResumeLaunch( | ||
| strategy: .legacyCommand, | ||
| initialInput: legacyCommand + "\n", | ||
| workingDirectory: resumeWorkingDirectory, | ||
| startupRestoreAgent: nil | ||
| ) | ||
| } | ||
|
|
||
| private var vaultResumeSnapshot: SessionRestorableAgentSnapshot? { | ||
| let components: SessionEntryResumeSnapshotComponents | ||
| switch specifics { | ||
| case let .claude(model, permissionMode, configDirectoryForResume): | ||
| var arguments = ["claude"] | ||
| if let model = Self.nonEmptyResumeValue(model) { | ||
| arguments.append(contentsOf: ["--model", model]) | ||
| } | ||
| let environment = Self.nonEmptyResumeValue(configDirectoryForResume) | ||
| .map { ["CLAUDE_CONFIG_DIR": $0] } ?? [:] | ||
| components = SessionEntryResumeSnapshotComponents( | ||
| arguments: arguments, | ||
| environment: environment, | ||
| registration: nil, | ||
| permissionMode: Self.nonEmptyResumeValue(permissionMode) | ||
| ) | ||
| case let .codex(model, approvalPolicy, sandboxMode, effort): | ||
| var arguments = ["codex"] | ||
| if let model = Self.nonEmptyResumeValue(model) { | ||
| arguments.append(contentsOf: ["-m", model]) | ||
| } | ||
| arguments.append(contentsOf: Self.codexApprovalSandboxArgumentTokens( | ||
| approvalPolicy: approvalPolicy, | ||
| sandboxMode: sandboxMode | ||
| )) | ||
| if let effort = Self.nonEmptyResumeValue(effort) { | ||
| arguments.append(contentsOf: ["-c", "model_reasoning_effort=\(effort)"]) | ||
| } | ||
| components = SessionEntryResumeSnapshotComponents( | ||
| arguments: arguments, | ||
| environment: [:], | ||
| registration: nil, | ||
| permissionMode: nil | ||
| ) | ||
| case let .grok(model, permissionMode, sandboxMode, grokHome): | ||
| var arguments = ["grok"] | ||
| if let model = Self.nonEmptyResumeValue(model) { | ||
| arguments.append(contentsOf: ["-m", model]) | ||
| } | ||
| if let permissionMode = Self.nonEmptyResumeValue(permissionMode) { | ||
| arguments.append(contentsOf: ["--permission-mode", permissionMode]) | ||
| } | ||
| if let sandboxMode = Self.nonEmptyResumeValue(sandboxMode) { | ||
| arguments.append(contentsOf: ["--sandbox", sandboxMode]) | ||
| } | ||
| let environment = Self.nonEmptyResumeValue(grokHome) | ||
| .map { ["GROK_HOME": $0] } ?? [:] | ||
| components = SessionEntryResumeSnapshotComponents( | ||
| arguments: arguments, | ||
| environment: environment, | ||
| registration: nil, | ||
| permissionMode: nil | ||
| ) | ||
| case let .opencode(providerModel, agentName): | ||
| var arguments = ["opencode"] | ||
| if let providerModel = Self.nonEmptyResumeValue(providerModel) { | ||
| arguments.append(contentsOf: ["-m", providerModel]) | ||
| } | ||
| if let agentName = Self.nonEmptyResumeValue(agentName) { | ||
| arguments.append(contentsOf: ["--agent", agentName]) | ||
| } | ||
| components = SessionEntryResumeSnapshotComponents( | ||
| arguments: arguments, | ||
| environment: [:], | ||
| registration: nil, | ||
| permissionMode: nil | ||
| ) | ||
| case .rovodev: | ||
| components = SessionEntryResumeSnapshotComponents( | ||
| arguments: ["acli", "rovodev", "run"], | ||
| environment: [:], | ||
| registration: nil, | ||
| permissionMode: nil | ||
| ) | ||
| case let .hermesAgent(source, model, hermesHome): | ||
| var arguments = ["hermes"] | ||
| if source == "tui" { | ||
| arguments.append("--tui") | ||
| } | ||
| if let model = Self.nonEmptyResumeValue(model) { | ||
| arguments.append(contentsOf: ["--model", model]) | ||
| } | ||
| let environment = Self.nonEmptyResumeValue(hermesHome) | ||
| .map { ["HERMES_HOME": $0] } ?? [:] | ||
| components = SessionEntryResumeSnapshotComponents( | ||
| arguments: arguments, | ||
| environment: environment, | ||
| registration: nil, | ||
| permissionMode: nil | ||
| ) | ||
| case .registered(let registration): | ||
| components = SessionEntryResumeSnapshotComponents( | ||
| arguments: [registration.defaultExecutable], | ||
| environment: [:], | ||
| registration: registration, | ||
| permissionMode: nil | ||
| ) | ||
| } | ||
|
|
||
| guard let kind = RestorableAgentKind( | ||
| persistedRawValue: agent.rawValue, | ||
| registration: components.registration | ||
| ) else { | ||
| return nil | ||
| } | ||
| let workingDirectory = resumeWorkingDirectory | ||
| return SessionRestorableAgentSnapshot( | ||
| kind: kind, | ||
| sessionId: sessionId, | ||
| workingDirectory: workingDirectory, | ||
| launchCommand: AgentLaunchCommandSnapshot( | ||
| arguments: components.arguments, | ||
| workingDirectory: workingDirectory, | ||
| environment: components.environment.isEmpty ? nil : components.environment, | ||
| source: "vault" | ||
| ), | ||
| registration: components.registration, | ||
| permissionMode: components.permissionMode | ||
| ) | ||
| } | ||
|
|
||
| /// Sandbox-policy values the Codex CLI `--sandbox` flag accepts. | ||
| /// | ||
| /// cmux captures Codex's internal sandbox-policy `type`, which also includes | ||
| /// values such as `disabled` and `managed` that the CLI rejects. | ||
| private static let codexCLISandboxModes: Set<String> = [ | ||
| "read-only", | ||
| "workspace-write", | ||
| "danger-full-access", | ||
| ] | ||
|
|
||
| /// Returns a structured argument vector for the captured Codex policy. | ||
| /// | ||
| /// The captured `(approval: "never", sandbox: "disabled")` pair is the exact | ||
| /// inverse of `--dangerously-bypass-approvals-and-sandbox`, so it becomes that | ||
| /// one flag instead of the invalid `-a never -s disabled` combination. | ||
| static func codexApprovalSandboxArgumentTokens( | ||
| approvalPolicy: String?, | ||
| sandboxMode: String? | ||
| ) -> [String] { | ||
| if approvalPolicy == "never", sandboxMode == "disabled" { | ||
| return ["--dangerously-bypass-approvals-and-sandbox"] | ||
| } | ||
|
|
||
| var arguments: [String] = [] | ||
| if let approvalPolicy, !approvalPolicy.isEmpty { | ||
| arguments.append(contentsOf: ["-a", approvalPolicy]) | ||
| } | ||
| if let sandboxMode, !sandboxMode.isEmpty, | ||
| codexCLISandboxModes.contains(sandboxMode) { | ||
| arguments.append(contentsOf: ["-s", sandboxMode]) | ||
| } | ||
| return arguments | ||
| } | ||
|
|
||
| private static func nonEmptyResumeValue(_ value: String?) -> String? { | ||
| guard let value = value?.trimmingCharacters(in: .whitespacesAndNewlines), | ||
| !value.isEmpty else { | ||
| return nil | ||
| } | ||
| return value | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.