1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
mod client;
mod codex;
mod commands;
mod hook;
mod opencode;
mod runner;
use autofork_core::config::Paths;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(
name = "autofork",
version,
about = "Forks for Claude Code: throwaway forked-context runs at lifecycle moments"
)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Claude Code hook entrypoint (reads the hook JSON on stdin).
#[command(hide = true)]
Hook {
#[arg(value_enum)]
event: hook::HookKind,
},
/// Daemon, session, and fork-run status.
Status,
/// List the forks visible from the current (or given) directory.
Forks {
/// Project directory (defaults to the current directory).
#[arg(long)]
project: Option<std::path::PathBuf>,
},
/// List the lifecycle hooks visible from the current (or given) directory.
///
/// Lifecycle hooks are daemon-run shell commands (no model, no fork) fired
/// at session moments — session_start, resume, activity, idle, session_end
/// — for resource integrations like workspace leases.
Hooks {
/// Project directory (defaults to the current directory).
#[arg(long)]
project: Option<std::path::PathBuf>,
},
/// Print the spawn instruction for a fork (by name, or every fork carrying
/// `--tag`) to paste into an interactive Claude Code session. v0.5 forks
/// run as fork subagents, so autofork can no longer spawn them itself.
Run {
/// Fork name (omit when using --tag).
name: Option<String>,
/// Select every fork carrying this tag instead of one by name.
#[arg(long, conflicts_with = "name")]
tag: Option<String>,
},
/// Show the daemon log.
Logs {
/// Keep following the log.
#[arg(short, long)]
follow: bool,
},
/// Close the sessions `status` marks [stale?] now.
///
/// Stale = open with no parked poll and long idle — a Claude process that
/// died mid-turn. Harmless (a stale session can never fire a fork) but they
/// clutter `status` until the session timeout (default 12h) reaps them.
Prune,
/// Check the installation and report problems.
Doctor,
/// opencode integration: install the plugin, or serve as its hook.
Opencode {
#[command(subcommand)]
command: OpencodeCommand,
},
/// OpenAI Codex CLI integration: install the hooks, or serve as one.
Codex {
#[command(subcommand)]
command: CodexCommand,
},
/// The flush-on-close end-runner (spawned detached by SessionEnd hooks).
#[command(hide = true)]
FinalRun {
#[arg(long)]
client: String,
#[arg(long)]
session: String,
#[arg(long)]
resume_target: String,
#[arg(long)]
cwd: std::path::PathBuf,
#[arg(long)]
specs: std::path::PathBuf,
#[arg(long)]
model: Option<String>,
#[arg(long)]
permission_mode: Option<String>,
#[arg(long)]
bin: Option<std::path::PathBuf>,
},
/// Ask the daemon to exit (it restarts on the next hook event).
StopDaemon {
/// Wait for in-flight fork runs to finish first.
#[arg(long, default_value_t = true)]
drain: bool,
},
}
#[derive(Subcommand)]
enum OpencodeCommand {
/// Install (or refresh) the autofork plugin into opencode's global
/// plugin directory. Restart opencode afterwards.
Install {
/// Print the plugin source to stdout instead of installing it.
#[arg(long)]
print: bool,
},
/// Remove the installed plugin.
Uninstall,
/// The opencode plugin's hook entrypoint (reads JSON on stdin).
#[command(hide = true)]
Hook {
#[arg(value_enum)]
event: opencode::OcHookKind,
},
}
#[derive(Subcommand)]
enum CodexCommand {
/// Merge the autofork hooks into `$CODEX_HOME/hooks.json` and trust them
/// with codex (codex silently skips untrusted hooks). Restart codex
/// sessions afterwards.
Install {
/// Print the merged hooks.json to stdout instead of installing it.
#[arg(long)]
print: bool,
},
/// Remove the autofork hooks from `$CODEX_HOME/hooks.json`.
Uninstall,
/// The codex hooks' entrypoint (reads the hook JSON on stdin).
#[command(hide = true)]
Hook {
#[arg(value_enum)]
event: codex::CxHookKind,
},
/// The per-session waiter: parks the idle long-poll, executes due forks
/// via `codex exec fork`, and queues their reports into the parent.
#[command(hide = true)]
Waiter {
#[arg(long)]
session: String,
#[arg(long)]
rollout: std::path::PathBuf,
#[arg(long)]
codex_pid: u32,
#[arg(long)]
cwd: std::path::PathBuf,
#[arg(long)]
model: Option<String>,
#[arg(long)]
permission_mode: Option<String>,
#[arg(long)]
codex_bin: Option<std::path::PathBuf>,
},
}
fn main() {
let cli = Cli::parse();
let Some(paths) = Paths::from_env() else {
eprintln!("autofork: cannot determine home directory");
std::process::exit(1);
};
match cli.command {
Command::Hook { event } => hook::run_hook(event),
Command::Status => exit_on_err(commands::status(&paths)),
Command::Forks { project } => exit_on_err(commands::list_forks(&paths, project)),
Command::Hooks { project } => exit_on_err(commands::list_hooks(&paths, project)),
Command::Run { name, tag } => exit_on_err(commands::run_fork(&paths, name, tag)),
Command::Logs { follow } => exit_on_err(commands::logs(&paths, follow)),
Command::Prune => exit_on_err(commands::prune(&paths)),
Command::Doctor => exit_on_err(commands::doctor(&paths)),
Command::Opencode { command } => match command {
OpencodeCommand::Install { print } => exit_on_err(opencode::install(print)),
OpencodeCommand::Uninstall => exit_on_err(opencode::uninstall()),
OpencodeCommand::Hook { event } => opencode::run_hook(event),
},
Command::Codex { command } => match command {
CodexCommand::Install { print } => exit_on_err(codex::install(print)),
CodexCommand::Uninstall => exit_on_err(codex::uninstall()),
CodexCommand::Hook { event } => codex::run_hook(event),
CodexCommand::Waiter {
session,
rollout,
codex_pid,
cwd,
model,
permission_mode,
codex_bin,
} => codex::run_waiter(codex::WaiterArgs {
session,
rollout,
codex_pid,
cwd,
model,
permission_mode,
codex_bin,
}),
},
Command::FinalRun {
client,
session,
resume_target,
cwd,
specs,
model,
permission_mode,
bin,
} => {
// Fork children run the harness binary the closing session ran.
runner::set_harness_bin(bin.clone());
codex::set_codex_bin(bin);
let parsed: Vec<autofork_core::protocol::WakeFork> = std::fs::read_to_string(&specs)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default();
let _ = std::fs::remove_file(&specs);
runner::run_final(
&paths,
&client,
&session,
&resume_target,
&cwd,
model.as_deref(),
permission_mode.as_deref(),
parsed,
);
}
Command::StopDaemon { drain } => exit_on_err(stop_daemon(&paths, drain)),
}
}
fn stop_daemon(paths: &Paths, drain: bool) -> Result<(), String> {
use autofork_core::protocol::RequestBody;
match client::Client::connect(paths, std::time::Duration::from_secs(5)) {
Ok(mut c) => {
let _ = c
.request(RequestBody::Shutdown { drain })
.map_err(|e| e.to_string())?;
println!("daemon asked to exit");
Ok(())
}
Err(_) => {
println!("daemon not running");
Ok(())
}
}
}
fn exit_on_err(result: Result<(), String>) {
if let Err(e) = result {
eprintln!("autofork: {e}");
std::process::exit(1);
}
}