Repository navigation
Add terminal.claudeResumeMode to auto-answer Claude Code's resume prompt #6487
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
Open
RubiconPerform
wants to merge
12
commits into
manaflow-ai:main
Choose a base branch
from
RubiconPerform:agent/claude-resume-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+743
−3
Open
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6a4c44f
feat(terminal): add terminal.claudeResumeMode to auto-answer Claude's…
RubiconPerform f61b427
Address review feedback for terminal.claudeResumeMode
RubiconPerform fca9d7a
Localize terminal.claudeResumeMode (review follow-up)
RubiconPerform ab0f531
Address CodeRabbit follow-up: strict-parse doc + agent-hook binding g…
RubiconPerform a2df375
Merge remote-tracking branch 'origin/main' into agent/claude-resume-mode
austinywang 2dc2cf1
Fix Claude resume prompt package convention
austinywang 80826cc
Retry Claude resume auto-response until delivered
austinywang 975bf24
Satisfy Claude resume policy checks
austinywang 53bdf7d
Harden Claude resume responder
austinywang 2c45152
Bound Claude resume pending responders
austinywang c72221a
Ignore stale Claude resume menu blocks
austinywang 9a8ed08
Merge remote-tracking branch 'origin/main' into agent/claude-resume-mode
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
53 changes: 53 additions & 0 deletions
53
Packages/macOS/CmuxSettings/Sources/CmuxSettings/Values/ClaudeResumeMode.swift
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,53 @@ | ||
| import Foundation | ||
|
|
||
| /// How cmux answers Claude Code's compacted-session resume prompt when it | ||
| /// auto-resumes a Claude agent on session restore. Claude shows an interactive | ||
| /// menu ("Resume from summary" / "Resume full session as-is" / "Don't ask me | ||
| /// again") that has no CLI flag or config to skip, so cmux drives it for you. | ||
| /// - ``ask`` — leave the prompt for you to answer (current behavior, default). | ||
| /// - ``full`` — auto-select "Resume full session as-is". | ||
| /// - ``summary`` — auto-select "Resume from summary". | ||
| public enum ClaudeResumeMode: String, CaseIterable, Sendable, Identifiable, SettingCodable { | ||
| case ask | ||
| case full | ||
| case summary | ||
|
|
||
| public var id: String { rawValue } | ||
|
|
||
| /// Parse shared by every configuration surface (`cmux.json`, `settings.json`, | ||
| /// the control socket): trims whitespace and accepts only the exact schema | ||
| /// enum values (`ask`/`full`/`summary`) case-insensitively; anything else is | ||
| /// rejected so out-of-schema config is reported invalid. | ||
| public init?(rawString: String?) { | ||
| guard let raw = rawString?.trimmingCharacters(in: .whitespacesAndNewlines), | ||
| !raw.isEmpty else { return nil } | ||
| // Accept only the exact schema enum values (case-insensitive + trimmed); | ||
| // anything else is rejected so out-of-schema config is reported invalid | ||
| // rather than silently accepted. Keep in sync with | ||
| // web/data/cmux.schema.json (`terminal.claudeResumeMode` enum). | ||
| switch raw.lowercased() { | ||
| case "ask": | ||
| self = .ask | ||
| case "full": | ||
| self = .full | ||
| case "summary": | ||
| self = .summary | ||
| default: | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| public static func decodeFromUserDefaults(_ raw: Any?) -> ClaudeResumeMode? { | ||
| guard let string = raw as? String else { return nil } | ||
| return ClaudeResumeMode(rawString: string) | ||
| } | ||
|
|
||
| public func encodeForUserDefaults() -> Any { rawValue } | ||
|
|
||
| public static func decodeFromJSON(_ raw: Any?) -> ClaudeResumeMode? { | ||
| guard let string = raw as? String else { return nil } | ||
| return ClaudeResumeMode(rawString: string) | ||
| } | ||
|
|
||
| public func encodeForJSON() -> Any { rawValue } | ||
| } | ||
115 changes: 115 additions & 0 deletions
115
Packages/macOS/CmuxSettings/Sources/CmuxSettings/Values/ClaudeResumePrompt.swift
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,115 @@ | ||
| import Foundation | ||
|
|
||
| /// A single key cmux synthesizes into a Claude agent pane to drive its | ||
| /// compacted-session resume menu. Maps to cmux's named-key sender so libghostty | ||
| /// encodes the correct escape sequence for the pane's cursor mode. | ||
| public enum ClaudeResumeKey: String, Sendable { | ||
| case up | ||
| case down | ||
| case enter | ||
|
|
||
| /// Name understood by `TerminalSurface.sendNamedKey` / `TerminalPanel.sendNamedKeyResult`. | ||
| public var namedKey: String { rawValue } | ||
| } | ||
|
|
||
| /// Pure, UI-free detection and keystroke planning for Claude Code's | ||
| /// compacted-session resume menu. Kept free of AppKit/ghostty so it is fully | ||
| /// unit-testable; the app-side controller supplies the rendered screen text and | ||
| /// performs the side effects (reading the pane, sending keys). | ||
| public enum ClaudeResumePrompt { | ||
| /// Distinctive option labels Claude Code prints for the resume menu. Matching | ||
| /// the two "Resume …" labels together is specific enough not to fire on | ||
| /// ordinary conversation. The apostrophe in "Don't ask me again" is avoided | ||
| /// on purpose (straight-vs-curly quote ambiguity across renderers). | ||
| public static let summaryLabel = "Resume from summary" | ||
| public static let fullLabel = "Resume full session as-is" | ||
| public static let dontAskLabel = "ask me again" | ||
|
|
||
| /// Selection-pointer glyphs Ink-style menus render next to the active row. | ||
| private static let pointerGlyphs: Set<Character> = ["❯", "›", "▶", "▸", "➤"] | ||
|
|
||
| /// True when the resume menu is currently on screen. Requires all three | ||
| /// option labels together — a deliberately strict signal so the responder | ||
| /// never synthesizes keys just because one phrase appears in ordinary | ||
| /// terminal output. | ||
| public static func isVisible(in screen: String) -> Bool { | ||
| screen.contains(summaryLabel) && screen.contains(fullLabel) && screen.contains(dontAskLabel) | ||
| } | ||
|
|
||
| /// The keys needed to land on `mode`'s option and confirm it, given the | ||
| /// rendered screen. Returns nil when the menu isn't present, the mode is | ||
| /// `.ask` (no automation), or the target option can't be located. | ||
| /// | ||
| /// Selection model: the menu's option rows are detected by their labels, in | ||
| /// display order. The currently highlighted row is found by its pointer | ||
| /// glyph; if no glyph is rendered we assume the first (recommended) row is | ||
| /// highlighted, which matches Claude's default. We then move up/down to the | ||
| /// target row and press Enter. Detecting the row order (rather than | ||
| /// hard-coding "press Down once") keeps this working if Claude reorders the | ||
| /// options. | ||
| public static func keystrokes(for mode: ClaudeResumeMode, in screen: String) -> [ClaudeResumeKey]? { | ||
| guard mode != .ask, isVisible(in: screen) else { return nil } | ||
|
|
||
| let targetLabel: String | ||
| switch mode { | ||
| case .full: targetLabel = fullLabel | ||
| case .summary: targetLabel = summaryLabel | ||
| case .ask: return nil | ||
| } | ||
|
|
||
| // Single pass over the screen: collect the menu's option rows (in display | ||
| // order) and note which row currently carries the selection pointer. | ||
| var optionLines: [String] = [] | ||
| var pointerPosition: Int? | ||
| for rawLine in screen.split(separator: "\n", omittingEmptySubsequences: false) { | ||
| let line = String(rawLine) | ||
| guard line.contains(summaryLabel) || line.contains(fullLabel) || line.contains(dontAskLabel) else { | ||
| continue | ||
| } | ||
| if line.contains(where: { pointerGlyphs.contains($0) }) { | ||
| pointerPosition = optionLines.count | ||
| } | ||
| optionLines.append(line) | ||
| } | ||
|
|
||
| guard let targetPosition = optionLines.firstIndex(where: { $0.contains(targetLabel) }) else { | ||
| return nil | ||
| } | ||
| // Claude highlights the recommended (first) row by default; if no pointer | ||
| // glyph rendered, assume the first option is selected. | ||
| let currentPosition = pointerPosition ?? 0 | ||
|
|
||
| var keys: [ClaudeResumeKey] = [] | ||
| let delta = targetPosition - currentPosition | ||
| if delta > 0 { | ||
| keys.append(contentsOf: Array(repeating: .down, count: delta)) | ||
| } else if delta < 0 { | ||
| keys.append(contentsOf: Array(repeating: .up, count: -delta)) | ||
| } | ||
| keys.append(.enter) | ||
| return keys | ||
| } | ||
| } | ||
|
|
||
| /// One-shot responder armed for a single resumed Claude pane. The controller | ||
| /// feeds it the pane's rendered screen on each poll; it returns the keys to send | ||
| /// exactly once — when the menu first appears — then reports having responded so | ||
| /// the controller can disarm it. | ||
| public final class ClaudeResumeAutoResponder { | ||
| public let mode: ClaudeResumeMode | ||
| public private(set) var hasResponded = false | ||
|
|
||
| public init(mode: ClaudeResumeMode) { | ||
| self.mode = mode | ||
| } | ||
|
|
||
| /// Returns the keys to send if the menu is now visible and we haven't already | ||
| /// responded; nil otherwise. Marks itself responded when it returns keys so a | ||
| /// later call never double-fires. | ||
| public func evaluate(screen: String) -> [ClaudeResumeKey]? { | ||
| guard !hasResponded, mode != .ask else { return nil } | ||
| guard let keys = ClaudeResumePrompt.keystrokes(for: mode, in: screen) else { return nil } | ||
| hasResponded = true | ||
| return keys | ||
| } | ||
| } |
116 changes: 116 additions & 0 deletions
116
Packages/macOS/CmuxSettings/Tests/CmuxSettingsTests/ClaudeResumeModeTests.swift
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,116 @@ | ||
| import Foundation | ||
| import Testing | ||
| @testable import CmuxSettings | ||
|
|
||
| @Suite("ClaudeResumeMode") | ||
| struct ClaudeResumeModeTests { | ||
| @Test func strictRawStringParse() { | ||
| #expect(ClaudeResumeMode(rawString: "ask") == .ask) | ||
| #expect(ClaudeResumeMode(rawString: "full") == .full) | ||
| #expect(ClaudeResumeMode(rawString: "summary") == .summary) | ||
| #expect(ClaudeResumeMode(rawString: "FULL") == .full) // case-insensitive | ||
| #expect(ClaudeResumeMode(rawString: " summary ") == .summary) // trimmed | ||
| // Non-schema aliases must be rejected so invalid config is reported. | ||
| #expect(ClaudeResumeMode(rawString: "manual") == nil) | ||
| #expect(ClaudeResumeMode(rawString: "full-session") == nil) | ||
| #expect(ClaudeResumeMode(rawString: "garbage") == nil) | ||
| #expect(ClaudeResumeMode(rawString: "") == nil) | ||
| #expect(ClaudeResumeMode(rawString: nil) == nil) | ||
| } | ||
|
|
||
| @Test func roundTripsThroughUserDefaultsAndJSON() { | ||
| for mode in ClaudeResumeMode.allCases { | ||
| #expect(ClaudeResumeMode.decodeFromUserDefaults(mode.encodeForUserDefaults()) == mode) | ||
| #expect(ClaudeResumeMode.decodeFromJSON(mode.encodeForJSON()) == mode) | ||
| } | ||
| #expect(ClaudeResumeMode.decodeFromUserDefaults(42) == nil) | ||
| } | ||
| } | ||
|
|
||
| @Suite("ClaudeResumePrompt") | ||
| struct ClaudeResumePromptTests { | ||
| /// Default rendering: pointer (❯) on the recommended first option (summary). | ||
| private let menu = """ | ||
| Resuming the full session will consume a substantial portion of your usage limits. We recommend resuming from a summary. | ||
| ❯ 1. Resume from summary (recommended) | ||
| 2. Resume full session as-is | ||
| 3. Don't ask me again | ||
| """ | ||
|
|
||
| @Test func detectsPromptOnlyWhenAllOptionLabelsPresent() { | ||
| #expect(ClaudeResumePrompt.isVisible(in: menu)) | ||
| #expect(!ClaudeResumePrompt.isVisible(in: "just some normal claude output\nnothing to see")) | ||
| #expect(!ClaudeResumePrompt.isVisible(in: "Resume from summary mentioned alone")) | ||
| // Two of three labels appearing in prose must NOT count as the menu. | ||
| #expect(!ClaudeResumePrompt.isVisible(in: "We support Resume from summary and Resume full session as-is here.")) | ||
| } | ||
|
|
||
| @Test func fullModeMovesDownToSecondOptionThenEnter() { | ||
| #expect(ClaudeResumePrompt.keystrokes(for: .full, in: menu) == [.down, .enter]) | ||
| } | ||
|
|
||
| @Test func summaryModeConfirmsDefaultWithEnter() { | ||
| #expect(ClaudeResumePrompt.keystrokes(for: .summary, in: menu) == [.enter]) | ||
| } | ||
|
|
||
| @Test func askModeNeverActs() { | ||
| #expect(ClaudeResumePrompt.keystrokes(for: .ask, in: menu) == nil) | ||
| } | ||
|
|
||
| @Test func returnsNilWhenMenuAbsent() { | ||
| #expect(ClaudeResumePrompt.keystrokes(for: .full, in: "no menu here") == nil) | ||
| } | ||
|
|
||
| /// Order-independence: if Claude lists "full" first with the pointer on it, | ||
| /// full needs only Enter and summary needs one Down. | ||
| @Test func adaptsToReorderedOptions() { | ||
| let reordered = """ | ||
| ❯ 1. Resume full session as-is | ||
| 2. Resume from summary (recommended) | ||
| 3. Don't ask me again | ||
| """ | ||
| #expect(ClaudeResumePrompt.keystrokes(for: .full, in: reordered) == [.enter]) | ||
| #expect(ClaudeResumePrompt.keystrokes(for: .summary, in: reordered) == [.down, .enter]) | ||
| } | ||
|
|
||
| /// With no pointer glyph rendered we assume the first option is highlighted. | ||
| @Test func assumesFirstOptionHighlightedWithoutPointer() { | ||
| let noPointer = """ | ||
| 1. Resume from summary (recommended) | ||
| 2. Resume full session as-is | ||
| 3. Don't ask me again | ||
| """ | ||
| #expect(ClaudeResumePrompt.keystrokes(for: .full, in: noPointer) == [.down, .enter]) | ||
| } | ||
| } | ||
|
|
||
| @Suite("ClaudeResumeAutoResponder") | ||
| struct ClaudeResumeAutoResponderTests { | ||
| private let menu = """ | ||
| ❯ 1. Resume from summary (recommended) | ||
| 2. Resume full session as-is | ||
| 3. Don't ask me again | ||
| """ | ||
|
|
||
| @Test func firesExactlyOnce() { | ||
| let responder = ClaudeResumeAutoResponder(mode: .full) | ||
| #expect(responder.hasResponded == false) | ||
| #expect(responder.evaluate(screen: menu) == [.down, .enter]) | ||
| #expect(responder.hasResponded == true) | ||
| // Subsequent polls (menu still on screen) must not re-send. | ||
| #expect(responder.evaluate(screen: menu) == nil) | ||
| } | ||
|
|
||
| @Test func waitsForMenuBeforeFiring() { | ||
| let responder = ClaudeResumeAutoResponder(mode: .full) | ||
| #expect(responder.evaluate(screen: "claude is starting up…") == nil) | ||
| #expect(responder.hasResponded == false) | ||
| #expect(responder.evaluate(screen: menu) == [.down, .enter]) | ||
| } | ||
|
|
||
| @Test func askModeIsAlwaysInert() { | ||
| let responder = ClaudeResumeAutoResponder(mode: .ask) | ||
| #expect(responder.evaluate(screen: menu) == nil) | ||
| #expect(responder.hasResponded == false) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.