Skip to content

Commit 20f994c

Browse files
authored
feat(session): bring agents back after a restart as an unsent resume line (#389)
* feat(session): bring agents back after a restart as an unsent resume line Every Crook update is a restart, and a restart ended every running agent: restore brought back an empty shell. A pane now remembers the agent it was running, by its program name only, and comes back with that agent's resume line waiting in its composer, unsent. - PaneSnapshot gains `agent`: the first word of the pane's running command, by file name, and only when it names a program in agent.rs's table, so a prompt typed on the command line never reaches session.json. A value that is not a string reads as no agent rather than costing the tab. The session is saved when a pane's agent changes. - A restored pane keeps the agent (AgentSession::restored_agent) until anything runs there, so a second restart before Enter still offers it, and only when its directory came back, since the directory is the session key. - On restore the composer holds `claude --continue`, `codex resume --last`, `gemini --resume latest` or `copilot --resume`; two panes of one agent in one directory get the picker (`claude --resume`, `codex resume`) or, with none, nothing. OpenCode gets no default: its `--continue` crosses a repository's root-level worktrees (anomalyco/opencode#41562). - `resume_lines` in settings.json overrides the line per program; only what a person wrote is stored, so a later build's corrected flags still reach them, and an empty line turns an agent off. - "Resume every agent" (crook/window/resume-agents) submits every restored line still standing exactly as typed, and declines when there is none. - README, skill.md and docs/architecture.md say what is remembered and that nothing runs until Enter. * fix(session): forget a restored agent once any command finishes in its pane A restored pane named its agent until a TerminalUpdate::Running(Some) arrived, but Running is read off the pane at rest, so a command that starts and ends between two reads (cd, ls, git status) never sent one. After a cd the pane kept the agent and the next save wrote it against the new directory, where `claude --continue` would resume another conversation. TerminalUpdate::CommandFinished comes from the shell's D mark, sent for every command however quick. On it the workspace now drops the pane's resume offer and AgentSession::command_finished clears restored_agent, and the session is saved when that changed the agent. An agent the pane is running now stays named. Tests: a CommandFinished alone spends the agent and writes it out of the file; a real shell's cd out of the restored directory does the same; the session-level check covers a quick command and a running agent. * fix(session): take a restored resume line out of the field for --run and --type A windowed launch restores the session, which types an agent's resume line into the focused pane's field, and then --run and --type type into that same field at the caret. `--run 'git status'` sent `claude --continuegit status`, and `--type x` left `claude --continuex`. After the overrides, a launch that types into the field (Overrides::types_into_the_field) now calls Workspace::withdraw_resume_offer on the focused pane, which empties the field when it still holds the line exactly as the restore typed it and drops the offer. The pane's agent stays named: a command the launch runs spends it the way any finished command does. Tests: a withdrawn field takes a typed line as its whole text, an edited one is left alone, and the agent is still named; the override predicate is covered in lib's tests. * docs(session): say which agents a restored pane offers a line to The README said two panes of one agent in one directory get its picker, but Gemini CLI has none and they get nothing; it now says so, and that any command run first, a cd included, ends the offer. skill.md said every known agent comes back with its resume line; it now names the four that do and says OpenCode and aider get one only from resume_lines. docs/architecture.md adds "or nothing where it has none" to the picker rule. agent.rs said aider keeps no conversation to go back to, but aider documents --restore-chat-history. It now says aider has no default line and that flag is one a person can give it, and the README names aider beside OpenCode as having no line until one is given. * fix(session): keep a restored agent through a line that ran nothing A pane forgot its restored agent on every TerminalUpdate::CommandFinished, and that update also comes from the bare D a shell sends for a line that ran nothing: ctrl-c at the prompt and Enter on an empty field in bash and zsh, and ctrl-c in fish. Clearing the offered `claude --continue` with ctrl-c saved the session with "agent": null, and the next restart offered nothing, though nothing had run. Clearing it with Backspace kept it. The block model now says whether a command ran behind the boundary it closed: Finished::ran is true when the block has a command line or the shell sent C for it. It is read before the close takes the command, and it rides on TerminalEvent::CommandFinished and TerminalUpdate::CommandFinished. The workspace spends the restored agent only when it is true. Plugins still hear every D, as before. The status is not the test, because bash 3.2 sends a bare D for a `( ... )` line, which did run. The README now says clearing the line keeps the offer for the next restart. The doc on AgentSession::command_finished and on spend_restored_agent says a line that ran nothing does not call them. Tests: through a real shell, ctrl-c and an empty Enter at the prompt leave the agent named, in the pane and in what the file would say. In the block model, ctrl-c, an empty line and a repeated D report no run, and a subshell line, a C with no B and an ordinary command report one. * test(session): cover --run and --type opening on an empty field after a restore The step that takes a restored resume line back out of the field for --run and --type lived inline in Shell::new, which no test reaches. The workspace test called Workspace::withdraw_resume_offer by hand, so deleting the step, moving it above the restore or turning its guard around failed nothing, and `claude --continuegit status` could come back unnoticed. The restore, the overrides and the withdrawal are now one function, restore_and_override, which Shell::new calls. Its test opens a restored claude pane through it three times: with --run and with --type the field is empty, and with neither it still holds `claude --continue`. Removing the withdrawal, putting it before the restore, inverting types_into_the_field or dropping that guard each fails the test. * test(session): say whether a finished command ran in the tests main added
1 parent 9c712f9 commit 20f994c

19 files changed

Lines changed: 1683 additions & 42 deletions

‎README.md‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,29 @@ Nineteen features, and the page that configures them:
178178
and shows the slot it will drop into, and a group whose last tab leaves it goes away by
179179
itself. A group is not a split screen — the tabs in it are still one at a time, and splitting
180180
a tab is still `cmd-d` (`ctrl-shift-d` off macOS), asked for on purpose.
181+
182+
**The window comes back the way it was closed**: the tabs with their names, pins, colours
183+
and groups, the splits, the directory each shell was in — and, for a pane that was running a
184+
coding agent, that agent's name. Every Crook update is a restart and a restart ends every
185+
process in the window, so a pane that had Claude Code in it comes back as a shell in the same
186+
directory with `claude --continue` waiting in its command line, **unsent**: nothing runs
187+
until you press Enter. Codex is offered `codex resume --last` and Gemini CLI `gemini --resume
188+
latest`, each the most recent conversation in that directory, and Copilot its `copilot
189+
--resume` picker, since its most recent is the repository's rather than the worktree's. Two
190+
panes of one agent in one directory are offered its picker instead (`claude --resume`,
191+
`codex resume`), because the directory names one conversation and they had two; Gemini CLI
192+
has no picker on its command line, so two of its panes in one directory are offered nothing.
193+
Any command that runs in a pane first, even a `cd`, ends the offer there; clearing the line,
194+
with ctrl-c or otherwise, does not, and the next restart offers it again. **Resume
195+
every agent** in the palette (`resume-agents`) sends every line a restore typed that you
196+
have not touched, in one press. What is remembered is the program's name and nothing after
197+
it, so a prompt typed on the command line never reaches the file, and it needs the shell
198+
integration, which is what says what a pane is running. A line of your own goes in
199+
`settings.json` under `resume_lines`, keyed by program — `{"claude": "claude --continue
200+
--model opus"}` — and an empty one offers nothing. OpenCode and aider have no line until you
201+
give them one: OpenCode's `--continue` reaches across a repository's worktrees, and aider's
202+
`--restore-chat-history` is left for you to choose. No output comes back, and no process
203+
does.
181204
- **The window's own title bar.** There is no strip of system chrome above Crook. The window is
182205
opened with the application's frame, so the header *is* the title bar: dragging its empty
183206
space moves the window and a double click maximises it. On macOS that surface carries
@@ -503,7 +526,8 @@ Nineteen features, and the page that configures them:
503526
written to `<config>/crook/settings.json`, which is the same eight keys that menu writes, the
504527
status marks, the theme, the light and dark pair it follows the desktop between, the
505528
terminal's type size, whether the tabs come back, which stops post a notification, and —
506-
set in the file rather than on the page — its font family.
529+
set in the file rather than on the page — its font family and the line each agent is resumed
530+
with.
507531
The type size is also on `cmd/ctrl-plus`, `-minus` and `-0`, and every pane resizes with it:
508532
a pane's columns and rows are its box divided by a cell, so the ptys follow.
509533
It is the one pane with no shell under it and no field: every control on it is a click.
@@ -535,7 +559,7 @@ Nineteen features, and the page that configures them:
535559
page tells you where it is.
536560

537561
- **Everything the window does has a name, and most of it has a key.** The window registers
538-
fifty-one commands of its own and ships chords for thirty-seven; the other fourteen are
562+
fifty-two commands of its own and ships chords for thirty-seven; the other fifteen are
539563
reached by name, from the palette or from a chord of your own. That split is deliberate — a
540564
shipped chord is a key taken away from the shell in every pane, forever, so it is spent on
541565
what is pressed often and not on what is done once a week.
@@ -561,7 +585,8 @@ Nineteen features, and the page that configures them:
561585
Without a chord, by name: `split-left` and `split-up`, `grow-pane`, `shrink-pane` and
562586
`even-panes`, a block's menu (`open-block-menu`) and every entry of it (`copy-block`,
563587
`copy-block-command`, `copy-block-output`, `copy-block-directory`, `copy-block-branch`,
564-
`rerun-block`, `scroll-to-block-top`, `scroll-to-block-bottom`), every entry of a tab's
588+
`rerun-block`, `scroll-to-block-top`, `scroll-to-block-bottom`), `resume-agents` (the
589+
agents a restart ended — see Tabs), every entry of a tab's
565590
(`crook/tabs/pin-tab`, `close-tab`, `open-menu`, `view-options`,
566591
`toggle-group`, `close-group`, the seven colours), the worktree list (`crook/worktrees/menu`)
567592
and every settings page (`crook/appearance/open-page` and its four neighbours). The block

‎app/src/agent.rs‎

Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
//! plugin its plugin directory loads instead; `aider` has no hooks, and gets
2626
//! the sentence that says so and what to do instead.
2727
//!
28+
//! The same table is how a window knows an agent when it sees one:
29+
//! [`program_of`] reads a pane's command line for the program an agent is
30+
//! started as, which is the one word of a process the session file keeps, and
31+
//! [`resume_line`] is what a pane restored from that file is offered to bring
32+
//! the agent's conversation back.
33+
//!
2834
//! `--skill` prints [`SKILL`], the file that teaches an agent the rest of
2935
//! this: how it tells it is in a pane, what the four words do to the row,
3036
//! and what else the binary will do for it. A hook makes Claude Code report
@@ -227,12 +233,37 @@ pub struct Hooks {
227233

228234
/// One coding agent `--agent-hooks` knows.
229235
struct Agent {
230-
/// The word on the command line.
236+
/// The word on the command line, which is also the program's own file
237+
/// name — `claude` is both what `--agent-hooks` takes and what a person
238+
/// types to start Claude Code, and [`program_of`] reads a pane's command
239+
/// line by it.
231240
name: &'static str,
232241
/// What the program calls itself.
233242
program: &'static str,
234243
/// How the fragment is spelled, and where it goes.
235244
fragment: Fragment,
245+
/// The line that goes back to the most recent conversation in the
246+
/// directory it is run in, when the agent has one — see [`resume_line`].
247+
last: Option<&'static str>,
248+
/// The line that lists the agent's conversations for a person to choose
249+
/// from, when the agent has one.
250+
pick: Option<&'static str>,
251+
}
252+
253+
/// Which of an agent's two resume lines a restored pane is offered.
254+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
255+
pub enum Resume {
256+
/// The most recent conversation in the pane's directory: `claude
257+
/// --continue`. What a pane is offered when it is the only one of its
258+
/// agent in its directory, which Crook's one-agent-per-worktree makes the
259+
/// ordinary case.
260+
Last,
261+
/// The agent's own list to choose from: `claude --resume`. What two panes
262+
/// of one agent in one directory are offered instead, because "the most
263+
/// recent conversation" is one conversation and they had two, and
264+
/// guessing which pane had which would put one agent's work in the
265+
/// other's pane.
266+
Pick,
236267
}
237268

238269
/// The shapes a fragment comes in.
@@ -391,6 +422,24 @@ export const CrookPlugin = async ({ $ }) => {
391422
"#;
392423

393424
/// The agents, in the order the errors list them.
425+
///
426+
/// The resume lines are each CLI's own documented spelling, and only where
427+
/// the documentation says the line is about the directory it runs in: the
428+
/// directory is the whole of how a restored pane finds its conversation
429+
/// again. Claude Code's `--continue` is "the most recent conversation in the
430+
/// current directory" and its bare `--resume` a picker; Codex's `resume
431+
/// --last` is "the most recent chat from the current working directory" and
432+
/// its bare `resume` a picker of that directory's; Gemini CLI keeps its
433+
/// sessions per project path and `--resume latest` takes the newest, with no
434+
/// picker on the command line. Copilot's `--continue` is documented as the
435+
/// most recent session of the *repository*, which every worktree of a
436+
/// repository shares, so it gets its picker for both. OpenCode's `--continue`
437+
/// is the newest session across a repository's root-level worktrees — an
438+
/// open bug, anomalyco/opencode#41562 — and it has no picker flag, so it
439+
/// gets neither. Aider gets none by default either, though
440+
/// `aider --restore-chat-history`, which reloads its chat history file, is a
441+
/// line to give it. Any of them can be given a line in the settings; see
442+
/// [`Settings::resume_line`](crate::settings::Settings::resume_line).
394443
const AGENTS: &[Agent] = &[
395444
Agent {
396445
name: "claude",
@@ -400,6 +449,8 @@ const AGENTS: &[Agent] = &[
400449
events: CLAUDE_EVENTS,
401450
missing: "",
402451
},
452+
last: Some("claude --continue"),
453+
pick: Some("claude --resume"),
403454
},
404455
Agent {
405456
name: "codex",
@@ -410,6 +461,8 @@ const AGENTS: &[Agent] = &[
410461
missing: " Codex has no notification hook: the row says needs-input for a \
411462
permission, and a question the model asks ends its turn as idle.",
412463
},
464+
last: Some("codex resume --last"),
465+
pick: Some("codex resume"),
413466
},
414467
Agent {
415468
name: "gemini",
@@ -420,6 +473,8 @@ permission, and a question the model asks ends its turn as idle.",
420473
missing: " Gemini CLI notifies for a tool permission only: a question the \
421474
model asks ends its turn as idle.",
422475
},
476+
last: Some("gemini --resume latest"),
477+
pick: None,
423478
},
424479
Agent {
425480
name: "copilot",
@@ -428,6 +483,8 @@ model asks ends its turn as idle.",
428483
file: "~/.copilot/hooks/crook.json, or a project's .github/hooks/crook.json",
429484
events: COPILOT_EVENTS,
430485
},
486+
last: Some("copilot --resume"),
487+
pick: Some("copilot --resume"),
431488
},
432489
Agent {
433490
name: "opencode",
@@ -436,6 +493,8 @@ model asks ends its turn as idle.",
436493
file: "~/.config/opencode/plugins/crook.ts, or a project's .opencode/plugins/crook.ts",
437494
source: OPENCODE_PLUGIN,
438495
},
496+
last: None,
497+
pick: None,
439498
},
440499
Agent {
441500
name: "aider",
@@ -446,9 +505,50 @@ ends and it waits for you, so `aider --notifications --notifications-command \"B
446505
--agent idle\"` reports idle there and nothing else; for running, start it from a \
447506
wrapper script that runs `BINARY --agent running` first.",
448507
),
508+
last: None,
509+
pick: None,
449510
},
450511
];
451512

513+
/// The known agent a command line starts, by its program's name, or `None`
514+
/// when the line starts something else.
515+
///
516+
/// The first word only, and only its file name: `claude "fix the login bug"`
517+
/// and `/opt/homebrew/bin/claude --model opus` are both `claude`. Nothing
518+
/// after the first word is ever handed on, because what comes after it is
519+
/// where a person types the prompt, and the answer to this is written into
520+
/// the session file. A word that names no agent this module knows — `cargo`,
521+
/// `vim`, a `claudette` — is `None`, so a file never holds the name of a
522+
/// program Crook would not know how to offer back.
523+
pub fn program_of(command: &str) -> Option<&'static str> {
524+
let first = command.split_whitespace().next()?;
525+
let file = first.rsplit(['/', '\\']).next().unwrap_or(first);
526+
// Windows' own spellings of the same program: the binary, and the shim
527+
// npm installs a Node CLI as.
528+
let program = file
529+
.strip_suffix(".exe")
530+
.or_else(|| file.strip_suffix(".cmd"))
531+
.unwrap_or(file);
532+
AGENTS
533+
.iter()
534+
.find(|agent| agent.name == program)
535+
.map(|agent| agent.name)
536+
}
537+
538+
/// The line that resumes `program`'s conversation in the directory it is
539+
/// run in, in the `resume` form, or `None` when the agent has none.
540+
///
541+
/// Built in; a person's own lines are the settings' — see
542+
/// [`Settings::resume_line`](crate::settings::Settings::resume_line), which
543+
/// is what a restored pane actually asks.
544+
pub fn resume_line(program: &str, resume: Resume) -> Option<&'static str> {
545+
let agent = AGENTS.iter().find(|agent| agent.name == program)?;
546+
match resume {
547+
Resume::Last => agent.last,
548+
Resume::Pick => agent.pick,
549+
}
550+
}
551+
452552
/// The known names, listed the way an error lists them: "claude, codex, ...
453553
/// or aider".
454554
pub fn names_listed() -> String {
@@ -833,6 +933,86 @@ mod tests {
833933
}
834934
}
835935

936+
#[test]
937+
fn a_command_line_names_its_agent_by_the_first_word_and_nothing_after_it() {
938+
assert_eq!(Some("claude"), program_of("claude"));
939+
// The prompt is the rest of the line, and none of it is the answer.
940+
assert_eq!(Some("claude"), program_of("claude \"fix the bug\""));
941+
assert_eq!(
942+
Some("codex"),
943+
program_of(" codex --model o4 'port the tab bar'")
944+
);
945+
// By the program's file name, wherever it was run from.
946+
assert_eq!(
947+
Some("claude"),
948+
program_of("/opt/homebrew/bin/claude --model opus")
949+
);
950+
assert_eq!(
951+
Some("copilot"),
952+
program_of(r"C:\Users\me\AppData\Roaming\npm\copilot.cmd")
953+
);
954+
for agent in AGENTS {
955+
assert_eq!(Some(agent.name), program_of(agent.name));
956+
}
957+
}
958+
959+
#[test]
960+
fn a_command_line_that_starts_no_known_agent_names_nothing() {
961+
for command in [
962+
"",
963+
" ",
964+
"cargo test",
965+
"vim claude.md",
966+
"claudette",
967+
"echo claude",
968+
"./claude-notes.sh",
969+
] {
970+
assert_eq!(None, program_of(command), "{command:?}");
971+
}
972+
}
973+
974+
#[test]
975+
fn each_agent_is_resumed_by_its_own_documented_line() {
976+
assert_eq!(
977+
Some("claude --continue"),
978+
resume_line("claude", Resume::Last)
979+
);
980+
assert_eq!(Some("claude --resume"), resume_line("claude", Resume::Pick));
981+
assert_eq!(
982+
Some("codex resume --last"),
983+
resume_line("codex", Resume::Last)
984+
);
985+
assert_eq!(Some("codex resume"), resume_line("codex", Resume::Pick));
986+
assert_eq!(
987+
Some("gemini --resume latest"),
988+
resume_line("gemini", Resume::Last)
989+
);
990+
// No picker on Gemini's command line, and an offer of the newest
991+
// conversation to two panes would be the same one twice.
992+
assert_eq!(None, resume_line("gemini", Resume::Pick));
993+
// Copilot's newest is the repository's, which a worktree shares.
994+
assert_eq!(
995+
Some("copilot --resume"),
996+
resume_line("copilot", Resume::Last)
997+
);
998+
// OpenCode's newest is too, and it has no picker to fall back on.
999+
assert_eq!(None, resume_line("opencode", Resume::Last));
1000+
assert_eq!(None, resume_line("aider", Resume::Last));
1001+
assert_eq!(None, resume_line("vim", Resume::Last));
1002+
}
1003+
1004+
#[test]
1005+
fn every_resume_line_starts_the_agent_it_resumes() {
1006+
// A line whose first word were another program's would be recorded,
1007+
// once it ran, as that program: the pane would come back next time
1008+
// offering the wrong agent.
1009+
for agent in AGENTS {
1010+
for line in [agent.last, agent.pick].into_iter().flatten() {
1011+
assert_eq!(Some(agent.name), program_of(line), "{line}");
1012+
}
1013+
}
1014+
}
1015+
8361016
#[test]
8371017
fn a_word_that_is_not_a_status_is_refused_with_the_words_that_are() {
8381018
let error = report("sleeping", None, None).unwrap_err();

‎app/src/input_keys.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ pub enum Binding {
283283
OpenBlockMenu,
284284
/// Bring one edge of the selected block to the matching edge of the pane.
285285
ScrollToBlock(BlockEdge),
286+
/// Send every resume line a restore left in a composer and nobody has
287+
/// touched since.
288+
ResumeAgents,
286289
}
287290

288291
/// **The whole keyboard policy of a pane, in one function.**

0 commit comments

Comments
 (0)