-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Recover fresh hook-bound agents after crash restore #10187
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,131 @@ import Testing | |||||
|
|
||||||
| @Suite(.serialized) | ||||||
| struct AgentSessionAutoResumeSwiftTests { | ||||||
| @MainActor | ||||||
| @Test(arguments: ["codex", "claude", "kimi"]) | ||||||
| func freshTrustedHookBindingRecoversAfterCrash(kind: String) throws { | ||||||
| let defaultsName = "cmux-crash-auto-resume-\(kind)-\(UUID().uuidString)" | ||||||
| let defaults = try #require(UserDefaults(suiteName: defaultsName)) | ||||||
| defer { defaults.removePersistentDomain(forName: defaultsName) } | ||||||
| defaults.set(true, forKey: AgentSessionAutoResumeSettings.autoResumeAgentSessionsKey) | ||||||
|
|
||||||
| let fixture = try crashRecoverySnapshot( | ||||||
| kind: kind, | ||||||
| updatedAt: Date().timeIntervalSince1970 - 60 | ||||||
| ) | ||||||
|
Comment on lines
+24
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟠 Major | 🏗️ Heavy lift Use a controlled clock for binding freshness. These tests derive fresh and stale timestamps from As per coding guidelines, “Test code must avoid real wall-clock dependencies: use injected virtual clocks and advance them manually.” Also applies to: 49-52, 85-89, 126-129 🤖 Prompt for AI AgentsSource: Coding guidelines |
||||||
| let restored = Workspace(agentSessionAutoResumeDefaults: defaults) | ||||||
| defer { restored.teardownAllPanels() } | ||||||
| let restoredPanelIDs = restored.restoreSessionSnapshot(fixture.snapshot) | ||||||
| let restoredPanelID = try #require(restoredPanelIDs[fixture.panelID]) | ||||||
| let restoredPanel = try #require(restored.terminalPanel(for: restoredPanelID)) | ||||||
|
|
||||||
| #expect(restoredPanel.surface.debugInitialInputForTesting() != nil) | ||||||
| #expect( | ||||||
| restored.restoredAgentResumeStatesByPanelId[restoredPanelID] | ||||||
| == .awaitingAutoResumeCommand | ||||||
| ) | ||||||
| #expect(restoredPanel.surface.debugHasHeadlessStartupWindowForTesting()) | ||||||
| } | ||||||
|
|
||||||
| @MainActor | ||||||
| @Test func dockFreshTrustedHookBindingRecoversAfterCrash() throws { | ||||||
| let defaultsName = "cmux-dock-crash-auto-resume-\(UUID().uuidString)" | ||||||
| let defaults = try #require(UserDefaults(suiteName: defaultsName)) | ||||||
| defer { defaults.removePersistentDomain(forName: defaultsName) } | ||||||
| defaults.set(true, forKey: AgentSessionAutoResumeSettings.autoResumeAgentSessionsKey) | ||||||
|
|
||||||
| let fixture = try crashRecoverySnapshot( | ||||||
| kind: "codex", | ||||||
| updatedAt: Date().timeIntervalSince1970 - 60 | ||||||
| ) | ||||||
| let panelSnapshot = try #require( | ||||||
| fixture.snapshot.panels.first { $0.id == fixture.panelID } | ||||||
| ) | ||||||
| let dock = DockSplitStore( | ||||||
| workspaceId: UUID(), | ||||||
| baseDirectoryProvider: { nil }, | ||||||
| agentSessionAutoResumeDefaults: defaults | ||||||
| ) | ||||||
| defer { dock.closeAllPanels() } | ||||||
| let restoredPanelIDs = dock.restoreSessionSnapshot( | ||||||
| SessionSplitContainerSnapshot( | ||||||
| focusedPanelId: fixture.panelID, | ||||||
| layout: .pane(SessionPaneLayoutSnapshot( | ||||||
| panelIds: [fixture.panelID], | ||||||
| selectedPanelId: fixture.panelID | ||||||
| )), | ||||||
| panels: [panelSnapshot] | ||||||
| ) | ||||||
| ) | ||||||
| let restoredPanelID = try #require(restoredPanelIDs[fixture.panelID]) | ||||||
| let restoredPanel = try #require(dock.panels[restoredPanelID] as? TerminalPanel) | ||||||
|
|
||||||
| #expect(restoredPanel.surface.debugInitialInputForTesting() != nil) | ||||||
| #expect( | ||||||
| dock.restoredAgentLifecycle.resumeStatesByPanelId[restoredPanelID] | ||||||
| == .awaitingAutoResumeCommand | ||||||
| ) | ||||||
| #expect(restoredPanel.surface.debugHasHeadlessStartupWindowForTesting()) | ||||||
| } | ||||||
|
|
||||||
| @MainActor | ||||||
| @Test func unsafeCrashRecoveryBindingsStayManual() throws { | ||||||
| let cases: [(kind: String, updatedAt: TimeInterval, enabled: Bool)] = [ | ||||||
| ("codex", Date().timeIntervalSince1970 - 13 * 60 * 60, true), | ||||||
| ("opencode", Date().timeIntervalSince1970 - 60, true), | ||||||
| ("codex", Date().timeIntervalSince1970 - 60, false), | ||||||
| ] | ||||||
|
|
||||||
| for testCase in cases { | ||||||
| let defaultsName = "cmux-unsafe-crash-resume-\(UUID().uuidString)" | ||||||
| let defaults = try #require(UserDefaults(suiteName: defaultsName)) | ||||||
| defer { defaults.removePersistentDomain(forName: defaultsName) } | ||||||
| defaults.set( | ||||||
| testCase.enabled, | ||||||
| forKey: AgentSessionAutoResumeSettings.autoResumeAgentSessionsKey | ||||||
| ) | ||||||
| let fixture = try crashRecoverySnapshot( | ||||||
| kind: testCase.kind, | ||||||
| updatedAt: testCase.updatedAt | ||||||
| ) | ||||||
| let restored = Workspace(agentSessionAutoResumeDefaults: defaults) | ||||||
| defer { restored.teardownAllPanels() } | ||||||
| let restoredPanelIDs = restored.restoreSessionSnapshot(fixture.snapshot) | ||||||
| let restoredPanelID = try #require(restoredPanelIDs[fixture.panelID]) | ||||||
| let restoredPanel = try #require(restored.terminalPanel(for: restoredPanelID)) | ||||||
|
|
||||||
| #expect(restoredPanel.surface.debugInitialInputForTesting() == nil) | ||||||
| #expect(!restoredPanel.surface.debugHasHeadlessStartupWindowForTesting()) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| @MainActor | ||||||
| @Test func freshCrossKindHookBindingDoesNotReplacePersistedAgent() throws { | ||||||
| let defaultsName = "cmux-cross-kind-crash-resume-\(UUID().uuidString)" | ||||||
| let defaults = try #require(UserDefaults(suiteName: defaultsName)) | ||||||
| defer { defaults.removePersistentDomain(forName: defaultsName) } | ||||||
| defaults.set(true, forKey: AgentSessionAutoResumeSettings.autoResumeAgentSessionsKey) | ||||||
|
|
||||||
| let persistedClaude = SessionRestorableAgentSnapshot( | ||||||
| kind: .claude, | ||||||
| sessionId: "claude-current-session", | ||||||
| workingDirectory: "/tmp/cmux-crash-recovery" | ||||||
| ) | ||||||
| let fixture = try crashRecoverySnapshot( | ||||||
| kind: "kimi", | ||||||
| updatedAt: Date().timeIntervalSince1970 - 60, | ||||||
| persistedAgent: persistedClaude | ||||||
| ) | ||||||
| let restored = Workspace(agentSessionAutoResumeDefaults: defaults) | ||||||
| defer { restored.teardownAllPanels() } | ||||||
| let restoredPanelIDs = restored.restoreSessionSnapshot(fixture.snapshot) | ||||||
| let restoredPanelID = try #require(restoredPanelIDs[fixture.panelID]) | ||||||
| let restoredPanel = try #require(restored.terminalPanel(for: restoredPanelID)) | ||||||
|
|
||||||
| #expect(restoredPanel.surface.debugInitialInputForTesting() == nil) | ||||||
| #expect(!restoredPanel.surface.debugHasHeadlessStartupWindowForTesting()) | ||||||
| } | ||||||
|
|
||||||
| /// Regression for #9619: cmux-owned restore input is an implementation | ||||||
| /// detail, not a user or agent title. Preserve the automatic title captured | ||||||
| /// before relaunch through that bootstrap event, then accept the first real | ||||||
|
|
@@ -1625,6 +1750,35 @@ struct AgentSessionAutoResumeSwiftTests { | |||||
| .replacingOccurrences(of: ".", with: "-") | ||||||
| } | ||||||
|
|
||||||
| @MainActor | ||||||
| private func crashRecoverySnapshot( | ||||||
| kind: String, | ||||||
| updatedAt: TimeInterval, | ||||||
| persistedAgent: SessionRestorableAgentSnapshot? = nil | ||||||
| ) throws -> (snapshot: SessionWorkspaceSnapshot, panelID: UUID) { | ||||||
| let source = Workspace() | ||||||
| defer { source.teardownAllPanels() } | ||||||
| let panelID = try #require(source.focusedPanelId) | ||||||
| var snapshot = source.sessionSnapshot(includeScrollback: false) | ||||||
| let panelIndex = try #require(snapshot.panels.firstIndex { $0.id == panelID }) | ||||||
| var terminal = try #require(snapshot.panels[panelIndex].terminal) | ||||||
| let checkpointID = "\(kind)-crash-recovery-\(UUID().uuidString)" | ||||||
| terminal.agent = persistedAgent | ||||||
| terminal.resumeBinding = SurfaceResumeBindingSnapshot( | ||||||
| name: kind.capitalized, | ||||||
| kind: kind, | ||||||
| command: "\(kind) --resume \(checkpointID)", | ||||||
| cwd: "/tmp/cmux-crash-recovery", | ||||||
| checkpointId: checkpointID, | ||||||
| source: "agent-hook", | ||||||
| autoResume: false, | ||||||
| updatedAt: updatedAt | ||||||
| ) | ||||||
| terminal.wasAgentRunning = false | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Set
Proposed fix- terminal.wasAgentRunning = false
+ terminal.wasAgentRunning = true📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| snapshot.panels[panelIndex].terminal = terminal | ||||||
| return (snapshot, panelID) | ||||||
| } | ||||||
|
|
||||||
| private func writeClaudeTranscript(sessionId: String, transcriptURL: URL) throws { | ||||||
| try FileManager.default.createDirectory( | ||||||
| at: transcriptURL.deletingLastPathComponent(), | ||||||
|
|
||||||
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.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Keep crash-recovery promotion out of persisted binding state.
bindingForCrashRecoverycreates a binding withautoResume = true. Later, Lines 1686-1693 storeeffectiveResumeBindingForStartupinsurfaceResumeBindingsByPanelId. This persists the promoted copy instead of the original manual binding.A later snapshot can then serialize the binding as auto-resumable. This violates the restore-only contract and can change a stale or otherwise manual binding into persisted auto-resume state.
Keep the original binding for storage. Use a separate launch-only binding for approval and startup. Add a restore-then-snapshot regression test that confirms
autoResumeremains unchanged in persisted bindings.As per coding guidelines, Swift architecture changes must preserve clear ownership and invariants.
🤖 Prompt for AI Agents
Source: Coding guidelines