autofork 0.27.3

autofork CLI: Claude Code hook entrypoint and daemon control
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! `autofork opencode …`: the opencode integration.
//!
//! opencode has no hook-command/exit-2 mechanism; instead an opencode server
//! plugin (installed by `autofork opencode install`) shells out to
//! `autofork opencode hook <kind>` with a small JSON object on stdin — the
//! same transport role Claude Code's hooks play, reusing the daemon
//! spawn/flock and version-handshake logic. Unlike the Claude Code hooks,
//! `stop-wait` answers on **stdout** with structured JSON: the plugin runs
//! fork sessions itself (opencode's native session fork + a prompt), so no
//! model-facing payload or exit-code signalling is involved.

use crate::client::{spawn_daemon_detached, Client};
use autofork_core::config::Paths;
use autofork_core::protocol::{Event, EventKind, RequestBody, ResponseBody};
use serde::Deserialize;
use std::path::PathBuf;
use std::time::Duration;

/// The client name stamped on every event this integration sends.
const CLIENT: &str = "opencode";

/// The plugin file name inside opencode's global config plugin dir.
const PLUGIN_FILE: &str = "autofork.js";

/// How long the `chat.message` drain gives a `session_start` feed that is
/// still running. Long enough for a command that reads files or shells out,
/// short enough that a wedged feed costs a noticeable pause and not a hung
/// prompt — past it the blocks fall back to the old lane (the next turn).
const FEED_DRAIN_WAIT_MS: u64 = 4000;

/// The embedded opencode plugin. `{{VERSION}}` is replaced at install time so
/// `doctor` can tell an out-of-date installed copy from the current one.
const PLUGIN_SOURCE: &str = include_str!("../assets/opencode-plugin.js");

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum OcHookKind {
    /// First sight of a session (or its resume): register it.
    SessionStart,
    /// A genuine user turn started: cancels any parked stop-wait, bumps the
    /// pause epoch.
    PromptSubmit,
    /// A user message is being assembled, before it is sent: drain the
    /// session's quiet feed blocks so they ride INSIDE this turn. Answers on
    /// stdout: `{"context":{"blocks":[…]}}` or `{}`.
    Message,
    /// The session went idle: long-poll for due forks. Answers on stdout:
    /// `{"wake":{"payload":…,"forks":[…],"feed":…}}` or `{"waited":true}`.
    /// `feed` (additive) carries lifecycle-feed blocks to inject instead of
    /// forks to run.
    StopWait,
    /// The session was deleted.
    SessionEnd,
    /// The plugin forked the session and prompted the copy.
    ForkSpawned,
    /// A fork run reached a terminal status.
    ForkCompleted,
}

/// What the plugin writes on stdin. One shape for every kind; unused fields
/// are simply absent.
#[derive(Debug, Deserialize)]
struct OcInput {
    session_id: String,
    /// The opencode instance directory (the project the plugin serves).
    directory: PathBuf,
    /// The worktree root, when opencode resolves one above `directory`.
    #[serde(default)]
    worktree: Option<PathBuf>,
    /// Model id of the session's last assistant message (e.g.
    /// `claude-haiku-4-5`), for context-window resolution.
    #[serde(default)]
    model: Option<String>,
    /// The session's context gauge in tokens (input + cache read + cache
    /// write of the last assistant step).
    #[serde(default)]
    context_tokens: Option<u64>,
    /// The model's real context window (`limit.context` from opencode's
    /// provider catalog), for context-threshold resolution.
    #[serde(default)]
    context_window: Option<u64>,
    /// fork-spawned / fork-completed: the fork's name.
    #[serde(default)]
    fork: Option<String>,
    /// fork-spawned / fork-completed: the fork session's id.
    #[serde(default)]
    run_ref: Option<String>,
    /// fork-completed: `completed` / `failed` / `stopped`.
    #[serde(default)]
    status: Option<String>,
    /// stop-wait: the session is mid-run (busy poll — `every:`/context
    /// triggers only; no idle deadlines, no pause baseline).
    #[serde(default)]
    busy: Option<bool>,
    /// prompt-submit: whether this turn is genuine user activity. The plugin
    /// sends `false` for the turn its own chain-report injection starts —
    /// that turn must not bump the pause epoch. Absent = genuine (`true`).
    #[serde(default)]
    waking: Option<bool>,
    /// fork-completed: the run's report ended with the chain sentinel.
    #[serde(default, rename = "continue")]
    cont: Option<bool>,
    /// session-end: why the session ended (`disposed` = the opencode
    /// instance shut down cleanly; `deleted` = the session was deleted).
    #[serde(default)]
    reason: Option<String>,
    /// session-end: the opencode process's own executable
    /// (`process.execPath` from the plugin), so flush-on-close runs the SAME
    /// opencode the session ran — PATH may resolve a different install.
    #[serde(default)]
    bin: Option<PathBuf>,
}

pub fn run_hook(kind: OcHookKind) {
    // Like the Claude Code hooks: never break the host, whatever happens.
    // stop-wait must still answer JSON so the plugin's read completes.
    if run_hook_inner(kind).is_none() {
        match kind {
            OcHookKind::StopWait => println!("{{\"waited\":true}}"),
            OcHookKind::Message => println!("{{}}"),
            _ => {}
        }
    }
}

fn run_hook_inner(kind: OcHookKind) -> Option<()> {
    // Recursion guard, same as the Claude Code and codex hooks. A
    // flush-on-close child is a fresh `opencode run -s <parent> --fork`
    // process with AUTOFORK_FORK=1 in its env, and Bun.spawn hands that env
    // to us. Without this, the child's plugin registers the fork copy as a
    // real session (it has no parentID, opencode's own "(fork #N)" title,
    // and the parent's last user message), the daemon rosters every fork on
    // it, and its close spawns N more children: an unbounded fork-of-fork
    // cascade (288 sessions, 10 levels deep, load 155 on 2026-09-02).
    if std::env::var_os("AUTOFORK_FORK").is_some()
        || std::env::var_os("AUTOFORK_SESSION_ID").is_some()
    {
        return None;
    }
    let mut raw = String::new();
    use std::io::Read;
    std::io::stdin().read_to_string(&mut raw).ok()?;
    let input: OcInput = serde_json::from_str(&raw).ok()?;
    let paths = Paths::from_env()?;

    let root = project_root_for(input.worktree.as_deref(), &input.directory);

    let event = |ev: EventKind| Event {
        event: ev,
        session_id: input.session_id.clone(),
        transcript_path: None,
        cwd: input.directory.clone(),
        project_root: root.clone(),
        source: None,
        reason: input.reason.clone(),
        model: input.model.clone(),
        enable_tags: crate::hook::tags_from_env("AUTOFORK_ENABLE_TAGS"),
        disable_tags: crate::hook::tags_from_env("AUTOFORK_DISABLE_TAGS"),
        waking: None,
        notif_tool_use_id: None,
        notif_task_id: None,
        notif_status: None,
        notif_continue: None,
        context_tokens: input.context_tokens,
        context_window: input.context_window,
        client: Some(CLIENT.to_string()),
        busy: input.busy,
        harness: None,
    };

    match kind {
        OcHookKind::SessionStart => {
            let client = Client::connect_or_spawn(&paths, Duration::from_secs(5)).ok()?;
            let mut client = client.ensure_current_version(&paths).ok()?;
            let _ = client.request(RequestBody::Event(event(EventKind::SessionStart)));
        }
        OcHookKind::PromptSubmit => {
            // Same hard budget as the Claude Code prompt hook: never wait on a
            // daemon spawn in the turn-start path.
            let Ok(mut client) = Client::connect(&paths, Duration::from_millis(1500)) else {
                spawn_daemon_detached(&paths);
                return Some(());
            };
            let mut ev = event(EventKind::PromptSubmit);
            // The plugin skips its own zero-turn report injections entirely,
            // and flags the one turn it *does* start itself — a chain fork's
            // turn-triggering report — as `waking: false`. Everything else is
            // a genuine user turn and starts a new pause.
            ev.waking = Some(input.waking.unwrap_or(true));
            let _ = client.request(RequestBody::Event(ev));
        }
        OcHookKind::Message => {
            // opencode's stand-in for Claude Code's `additionalContext`: the
            // plugin's `chat.message` hook can add parts to the user message
            // before it is sent, so a quiet feed reaches the model in the
            // turn the user is starting — not as a message injected behind
            // its back, which the model would only read one turn later.
            //
            // Spawn-and-wait like SessionStart rather than the prompt hook's
            // hard budget: the session's `session_start` feeds are fired by
            // this very prompt (opencode has no event before it), so a
            // daemon that isn't up yet is exactly the case where the blocks
            // would otherwise be lost to the first turn of every session.
            let client = Client::connect_or_spawn(&paths, Duration::from_secs(5)).ok()?;
            let mut client = client.ensure_current_version(&paths).ok()?;
            let blocks = match client.request(RequestBody::TakeReports {
                session_id: input.session_id.clone(),
                wait_ms: Some(FEED_DRAIN_WAIT_MS),
            }) {
                Ok(ResponseBody::Reports { blocks }) => blocks,
                _ => Vec::new(),
            };
            let out = serde_json::json!({ "context": { "blocks": blocks } });
            println!("{out}");
        }
        OcHookKind::StopWait => {
            // Orphan watchdog: this subprocess is the session's liveness
            // heartbeat. The plugin that spawned it dies with the opencode
            // process, but nothing kills *us* — and an orphaned poll keeps
            // the daemon convinced the session is alive forever (no
            // [stale?], no grace-close). When our parent dies we get
            // reparented; exit then, dropping the poll so the daemon's
            // poll-loss grace-close fires. Covers crashes and exits that
            // never reach the plugin's dispose hook.
            let ppid0 = autofork_core::sys::parent_pid();
            std::thread::spawn(move || loop {
                std::thread::sleep(Duration::from_secs(5));
                if autofork_core::sys::parent_pid() != ppid0 {
                    std::process::exit(0);
                }
            });
            let client = Client::connect_or_spawn(&paths, Duration::from_secs(10)).ok()?;
            let mut client = client.ensure_current_version(&paths).ok()?;
            match client.stop_wait(event(EventKind::Stop)) {
                Ok(ResponseBody::Wake {
                    payload,
                    forks,
                    feed,
                }) => {
                    // A wake carries either fork specs (spawn these) or feed
                    // blocks (put this text in the session) — never both, so
                    // the plugin can branch on which key is present. `wake`
                    // says whether the blocks should start a turn the model
                    // reacts to (`deliver: wake`) or ride in silently as a
                    // no-reply message (`deliver: context`, the lane for a
                    // quiet feed that fires while the session is idle — a turn
                    // that is already under way drains the spool itself, see
                    // `OcHookKind::Message`).
                    let out = serde_json::json!({
                        "wake": {
                            "payload": payload,
                            "forks": forks.unwrap_or_default(),
                            "feed": feed.as_ref().map(|f| serde_json::json!({
                                "blocks": f.blocks,
                                "wake": f.wake,
                            })),
                        }
                    });
                    println!("{out}");
                }
                _ => println!("{{\"waited\":true}}"),
            }
        }
        OcHookKind::SessionEnd => {
            let mut client = Client::connect_or_spawn(&paths, Duration::from_secs(5)).ok()?;
            // `flush_on_close`: the end-runner continues forks of the closed
            // session via `opencode run -s <id> --fork` (self-hosted, no live
            // instance needed). Reports have nowhere to be delivered — the
            // runs' work is the point; the plugin's startup sweep cleans the
            // leftover fork sessions by their spawn-prompt fingerprint.
            let flush = {
                let (cfg, _w) =
                    autofork_core::config::load_config_at(Some(&root), &paths.user_config());
                cfg.flush_on_close
            };
            if flush {
                if let Ok(ResponseBody::Due { forks }) =
                    client.request(RequestBody::TakeFinalRuns {
                        session_id: input.session_id.clone(),
                    })
                {
                    crate::runner::spawn_final_runner(
                        &paths,
                        "opencode",
                        &input.session_id,
                        &input.session_id,
                        &input.directory,
                        input.model.as_deref(),
                        None,
                        input.bin.as_deref(),
                        &forks,
                    );
                }
            }
            let _ = client.request(RequestBody::Event(event(EventKind::SessionEnd)));
        }
        OcHookKind::ForkSpawned => {
            let mut client = Client::connect_or_spawn(&paths, Duration::from_secs(5)).ok()?;
            let _ = client.request(RequestBody::ForkSpawned {
                session_id: input.session_id.clone(),
                fork: input.fork.clone()?,
                run_ref: input.run_ref.clone()?,
            });
        }
        OcHookKind::ForkCompleted => {
            let mut client = Client::connect_or_spawn(&paths, Duration::from_secs(5)).ok()?;
            let _ = client.request(RequestBody::ForkCompleted {
                session_id: input.session_id.clone(),
                fork: input.fork.clone()?,
                run_ref: input.run_ref.clone()?,
                status: input.status.clone().unwrap_or_else(|| "completed".into()),
                cont: input.cont,
            });
        }
    }
    Some(())
}

/// The project root for an opencode instance. The worktree (when known) is
/// the project identity; discovery walks up from it either way. opencode
/// reports `/` as the worktree for directories outside any VCS — that is no
/// project root, so fall back to the directory itself (likewise for a
/// worktree that doesn't actually contain the directory).
fn project_root_for(worktree: Option<&std::path::Path>, directory: &std::path::Path) -> PathBuf {
    worktree
        .filter(|w| *w != std::path::Path::new("/") && directory.starts_with(w))
        .map(|w| w.to_path_buf())
        .unwrap_or_else(|| directory.to_path_buf())
}

/// The rendered plugin source (version stamped).
pub fn plugin_source() -> String {
    PLUGIN_SOURCE.replace("{{VERSION}}", env!("CARGO_PKG_VERSION"))
}

/// opencode's global config dir (`$XDG_CONFIG_HOME/opencode`, defaulting to
/// `~/.config/opencode`).
pub fn opencode_config_dir() -> Option<PathBuf> {
    if let Some(xdg) = std::env::var_os("XDG_CONFIG_HOME").filter(|v| !v.is_empty()) {
        return Some(PathBuf::from(xdg).join("opencode"));
    }
    dirs_home().map(|h| h.join(".config").join("opencode"))
}

fn dirs_home() -> Option<PathBuf> {
    autofork_core::sys::home_dir()
}

/// Where the plugin gets installed.
pub fn plugin_path() -> Option<PathBuf> {
    Some(opencode_config_dir()?.join("plugin").join(PLUGIN_FILE))
}

/// `autofork opencode install`: write the plugin into opencode's global
/// plugin dir (opencode auto-discovers `plugin/*.js` there).
pub fn install(print: bool) -> Result<(), String> {
    if print {
        print!("{}", plugin_source());
        return Ok(());
    }
    let path = plugin_path().ok_or("cannot determine opencode config dir")?;
    let dir = path.parent().unwrap();
    std::fs::create_dir_all(dir).map_err(|e| format!("creating {}: {e}", dir.display()))?;
    std::fs::write(&path, plugin_source())
        .map_err(|e| format!("writing {}: {e}", path.display()))?;
    println!("installed {}", path.display());
    println!("restart opencode to load it (plugins load at instance start)");
    Ok(())
}

/// `autofork opencode uninstall`: remove the installed plugin.
pub fn uninstall() -> Result<(), String> {
    let path = plugin_path().ok_or("cannot determine opencode config dir")?;
    match std::fs::remove_file(&path) {
        Ok(()) => {
            println!("removed {}", path.display());
            Ok(())
        }
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            println!("not installed ({} absent)", path.display());
            Ok(())
        }
        Err(e) => Err(format!("removing {}: {e}", path.display())),
    }
}

/// Doctor check: is the plugin installed and current? Returns lines to print
/// (empty when everything is fine or opencode isn't in use).
pub fn doctor_lines() -> Vec<String> {
    let mut lines = Vec::new();
    let have_opencode = std::process::Command::new("opencode")
        .arg("--version")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);
    let Some(path) = plugin_path() else {
        return lines;
    };
    match std::fs::read_to_string(&path) {
        Ok(installed) => {
            if installed != plugin_source() {
                lines.push(format!(
                    "opencode plugin at {} is outdated or modified — run `autofork opencode install` to refresh it",
                    path.display()
                ));
            } else {
                lines.push(format!("opencode plugin installed ({})", path.display()));
            }
            if !have_opencode {
                lines.push(
                    "opencode plugin is installed but `opencode` was not found on PATH".into(),
                );
            }
        }
        Err(_) => {
            if have_opencode {
                lines.push(
                    "opencode detected but the autofork plugin is not installed — run `autofork opencode install` to enable forks in opencode sessions"
                        .into(),
                );
            }
        }
    }
    lines
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn plugin_source_is_version_stamped() {
        let src = plugin_source();
        assert!(!src.contains("{{VERSION}}"));
        assert!(src.contains(env!("CARGO_PKG_VERSION")));
    }

    #[test]
    fn project_root_ignores_the_slash_worktree() {
        use std::path::Path;
        // A real worktree above the directory wins.
        assert_eq!(
            project_root_for(Some(Path::new("/repo")), Path::new("/repo/sub")),
            Path::new("/repo")
        );
        // opencode's non-VCS sentinel `/` is not a project root.
        assert_eq!(
            project_root_for(Some(Path::new("/")), Path::new("/tmp/proj")),
            Path::new("/tmp/proj")
        );
        // A worktree that doesn't contain the directory is ignored too.
        assert_eq!(
            project_root_for(Some(Path::new("/elsewhere")), Path::new("/tmp/proj")),
            Path::new("/tmp/proj")
        );
        assert_eq!(
            project_root_for(None, Path::new("/tmp/proj")),
            Path::new("/tmp/proj")
        );
    }

    #[test]
    fn oc_input_parses_minimal_and_full() {
        let min: OcInput = serde_json::from_str(r#"{"session_id":"s","directory":"/p"}"#).unwrap();
        assert_eq!(min.session_id, "s");
        assert!(min.model.is_none());
        let full: OcInput = serde_json::from_str(
            r#"{"session_id":"s","directory":"/p","worktree":"/w","model":"claude-haiku-4-5",
                "context_tokens":1234,"context_window":1000000,
                "fork":"journal","run_ref":"ses_x","status":"completed"}"#,
        )
        .unwrap();
        assert_eq!(full.worktree.as_deref(), Some(std::path::Path::new("/w")));
        assert_eq!(full.context_tokens, Some(1234));
        assert_eq!(full.context_window, Some(1_000_000));
        assert_eq!(full.status.as_deref(), Some("completed"));
    }
}