opencode-codes 1.18.5

Typed Rust SDK for the opencode agent server: serde models of its OpenAPI 3.1 HTTP/SSE protocol, plus an async (Tokio) client and a managed local-server launcher for driving opencode sessions, prompts, and permission flows.
Documentation
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Managed launcher for the local `opencode serve` process.
//!
//! [`ManagedServer`] spawns `opencode serve --hostname <host> --port <n>` on a
//! free port (chosen with [`portpicker`] unless overridden), waits until the
//! HTTP surface answers `GET /global/health`, exposes the bound base URL via
//! [`ManagedServer::url`], and tears the process down on drop.
//!
//! # Orphan cleanup
//!
//! `opencode serve` may itself spawn child processes (MCP servers, tool
//! subprocesses). On Unix the child is placed in its own process group
//! (`setpgid(0, 0)`) so that termination signals the entire tree rather than
//! just the top-level `opencode` process. On drop the group receives `SIGTERM`,
//! followed by `SIGKILL` after a short grace period, ensuring nothing is left
//! orphaned even if the parent aborts. [`tokio::process::Command::kill_on_drop`]
//! guarantees the direct child is reaped as a backstop on every platform.
//!
//! # Feature gating
//!
//! This module is compiled only when the `server` feature is enabled.

use std::collections::HashMap;
use std::path::PathBuf;
use std::process::Stdio;
use std::time::Duration;
use std::time::Instant;

use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader;
use tokio::process::Child;
use tokio::process::Command;

use crate::error::Error;
use crate::error::Result;
use crate::http::DEFAULT_USERNAME;

/// Substring emitted on stdout once the server has bound its listener.
///
/// The full line is `opencode server listening on http://<host>:<port>`.
const LISTENING_MARKER: &str = "listening on ";

/// Grace period between `SIGTERM` and `SIGKILL` when terminating the group.
const KILL_GRACE: Duration = Duration::from_millis(250);

#[cfg(unix)]
const SIGTERM: i32 = 15;

#[cfg(unix)]
const SIGKILL: i32 = 9;

/// Send `sig` to the entire process group identified by `pgid`.
///
/// Signalling the negated PGID targets every process in the group created via
/// `process_group(0)`. Best-effort: a missing group (already-exited process,
/// `ESRCH`) is the desired state, and there is nothing actionable to do on any
/// other failure during teardown.
#[cfg(unix)]
#[allow(unsafe_code)] // The workspace denies unsafe_code; this FFI signal call is the one vetted exception.
fn signal_process_group(pgid: i32, sig: i32) {
    extern "C" {
        fn kill(pid: i32, sig: i32) -> i32;
    }

    // SAFETY: `kill` takes two plain integers and borrows no memory across the
    // FFI boundary. A negative pid addresses the process group.
    unsafe {
        kill(-pgid, sig);
    }
}

/// Fluent builder for a [`ManagedServer`].
///
/// Obtain one via [`ManagedServer::builder`]. All fields are optional; the
/// defaults launch `opencode` from `PATH`, bind `127.0.0.1` on a
/// portpicker-selected port, and wait up to ten seconds for health.
#[derive(Debug, Clone)]
pub struct ManagedServerBuilder {
    binary: PathBuf,
    hostname: String,
    port: Option<u16>,
    working_dir: Option<PathBuf>,
    password: Option<String>,
    envs: HashMap<String, String>,
    startup_timeout: Duration,
}

impl Default for ManagedServerBuilder {
    fn default() -> Self {
        Self {
            binary: PathBuf::from("opencode"),
            hostname: "127.0.0.1".to_string(),
            port: None,
            working_dir: None,
            password: None,
            envs: HashMap::new(),
            startup_timeout: Duration::from_secs(10),
        }
    }
}

impl ManagedServerBuilder {
    /// Create a builder with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Override the `opencode` executable (path or `PATH`-resolved name).
    ///
    /// Defaults to `opencode`.
    #[must_use]
    pub fn binary(mut self, binary: impl Into<PathBuf>) -> Self {
        self.binary = binary.into();
        self
    }

    /// Set the hostname passed to `--hostname`. Defaults to `127.0.0.1`.
    #[must_use]
    pub fn hostname(mut self, hostname: impl Into<String>) -> Self {
        self.hostname = hostname.into();
        self
    }

    /// Pin the listen port passed to `--port`.
    ///
    /// When unset, a free port is chosen with [`portpicker`]. The port actually
    /// bound is read back from the server's stdout, so pinning is only needed
    /// when a caller must know the port in advance.
    #[must_use]
    pub fn port(mut self, port: u16) -> Self {
        self.port = Some(port);
        self
    }

    /// Set the child's working directory.
    #[must_use]
    pub fn working_dir(mut self, dir: impl Into<PathBuf>) -> Self {
        self.working_dir = Some(dir.into());
        self
    }

    /// Set `OPENCODE_SERVER_PASSWORD`, enabling HTTP basic auth on the server.
    ///
    /// The managed health check authenticates as
    /// [`DEFAULT_USERNAME`](crate::http::DEFAULT_USERNAME) (`opencode`) with this
    /// password. Consumers of [`ManagedServer::url`] must send the same
    /// credentials.
    #[must_use]
    pub fn password(mut self, password: impl Into<String>) -> Self {
        self.password = Some(password.into());
        self
    }

    /// Inject an environment variable into the child process.
    ///
    /// Applied on top of the inherited parent environment. Call repeatedly to
    /// set several. `OPENCODE_SERVER_PASSWORD` is managed by
    /// [`password`](Self::password); setting it here directly is also honored.
    #[must_use]
    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.envs.insert(key.into(), value.into());
        self
    }

    /// Maximum time to wait for the server to become healthy. Defaults to 10s.
    #[must_use]
    pub fn startup_timeout(mut self, timeout: Duration) -> Self {
        self.startup_timeout = timeout;
        self
    }

    /// Spawn the server and wait until `GET /global/health` succeeds.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Server`] if the binary cannot be spawned, if it exits
    /// before reporting a listening address, or if it does not answer a healthy
    /// response within [`startup_timeout`](Self::startup_timeout).
    pub async fn spawn(self) -> Result<ManagedServer> {
        ManagedServer::spawn(self).await
    }
}

/// A running, self-terminating `opencode serve` process.
///
/// Construct one with [`ManagedServer::builder`]. The process is killed and
/// reaped when the value is dropped (see the [module docs](self) for the
/// process-group teardown strategy). Prefer [`ManagedServer::stop`] for an
/// awaited, deterministic shutdown.
#[derive(Debug)]
pub struct ManagedServer {
    child: Option<Child>,
    base_url: String,
    port: u16,
    password: Option<String>,
    /// Process-group id for whole-tree termination on Unix.
    pgid: Option<i32>,
}

impl ManagedServer {
    /// Start a builder with default settings.
    #[must_use]
    pub fn builder() -> ManagedServerBuilder {
        ManagedServerBuilder::new()
    }

    /// Spawn a server from the given builder configuration.
    ///
    /// # Errors
    ///
    /// See [`ManagedServerBuilder::spawn`].
    pub async fn spawn(builder: ManagedServerBuilder) -> Result<Self> {
        let port = match builder.port {
            Some(p) => p,
            None => portpicker::pick_unused_port()
                .ok_or_else(|| Error::Server("no free TCP port available".to_string()))?,
        };

        let mut cmd = Command::new(&builder.binary);
        cmd.arg("serve")
            .arg("--hostname")
            .arg(&builder.hostname)
            .arg("--port")
            .arg(port.to_string())
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::inherit())
            .kill_on_drop(true);

        #[cfg(unix)]
        cmd.process_group(0);

        if let Some(dir) = &builder.working_dir {
            cmd.current_dir(dir);
        }

        for (key, value) in &builder.envs {
            cmd.env(key, value);
        }

        if let Some(password) = &builder.password {
            cmd.env("OPENCODE_SERVER_PASSWORD", password);
        }

        let mut child = cmd.spawn().map_err(|e| {
            Error::Server(format!(
                "failed to spawn `{}`: {e}",
                builder.binary.display()
            ))
        })?;

        let pgid = child.id().map(|id| id as i32);

        let stdout = child.stdout.take().ok_or_else(|| {
            Error::Server("managed server child produced no stdout handle".to_string())
        })?;

        let deadline = Instant::now() + builder.startup_timeout;
        let result = Self::await_listening(
            stdout,
            &builder.hostname,
            port,
            deadline,
            builder.startup_timeout,
        )
        .await;

        let (base_url, reader) = match result {
            Ok(pair) => pair,
            Err(e) => {
                signal_group_and_kill(pgid, &mut child);
                let _ = child.wait().await;
                return Err(e);
            }
        };

        // Drain any further stdout so the child never blocks on a full pipe.
        tokio::spawn(async move {
            let mut reader = reader;
            while let Ok(Some(_)) = reader.next_line().await {}
        });

        let server = Self {
            child: Some(child),
            base_url,
            port,
            password: builder.password,
            pgid,
        };

        // On failure `server` is dropped here, tearing the process down.
        server.await_healthy(deadline).await?;

        Ok(server)
    }

    /// Read stdout until the listening line appears, returning the parsed base
    /// URL and the still-open reader (so callers can keep draining stdout).
    async fn await_listening(
        stdout: tokio::process::ChildStdout,
        hostname: &str,
        port: u16,
        deadline: Instant,
        startup_timeout: Duration,
    ) -> Result<(
        String,
        tokio::io::Lines<BufReader<tokio::process::ChildStdout>>,
    )> {
        let mut reader = BufReader::new(stdout).lines();

        loop {
            let now = Instant::now();
            if now >= deadline {
                return Err(Error::Server(format!(
                    "opencode serve did not report a listening address within {startup_timeout:?}"
                )));
            }
            let slice = (deadline - now).min(Duration::from_millis(200));

            match tokio::time::timeout(slice, reader.next_line()).await {
                Ok(Ok(Some(line))) => {
                    if let Some(url) = parse_listening_url(&line, hostname, port) {
                        return Ok((url, reader));
                    }
                }
                Ok(Ok(None)) => {
                    return Err(Error::Server(
                        "opencode serve exited before reporting a listening address".to_string(),
                    ));
                }
                Ok(Err(e)) => {
                    return Err(Error::Server(format!(
                        "error reading opencode serve stdout: {e}"
                    )));
                }
                // Per-slice timeout: loop and re-check the overall deadline.
                Err(_) => {}
            }
        }
    }

    /// Poll `GET /global/health` until it answers healthy or the deadline lapses.
    async fn await_healthy(&self, deadline: Instant) -> Result<()> {
        let client = reqwest::Client::new();
        let health_url = format!("{}/global/health", self.base_url);

        loop {
            let request = {
                let req = client.get(&health_url).timeout(Duration::from_millis(500));
                match &self.password {
                    Some(pw) => req.basic_auth(DEFAULT_USERNAME, Some(pw)),
                    None => req,
                }
            };

            if let Ok(resp) = request.send().await {
                if resp.status().is_success() {
                    return Ok(());
                }
            }

            if Instant::now() >= deadline {
                return Err(Error::Server(format!(
                    "opencode serve at {} never became healthy within the startup timeout",
                    self.base_url
                )));
            }

            tokio::time::sleep(Duration::from_millis(50)).await;
        }
    }

    /// The base URL the server bound to, e.g. `http://127.0.0.1:41999` (no
    /// trailing slash). Read back from the server's own stdout, so it reflects
    /// the actually-bound port even when the port was left to the OS.
    #[must_use]
    pub fn url(&self) -> &str {
        &self.base_url
    }

    /// The TCP port the server listens on.
    #[must_use]
    pub fn port(&self) -> u16 {
        self.port
    }

    /// Whether the child process is still running (non-blocking check).
    ///
    /// Returns `false` once the server has been consumed by [`ManagedServer::stop`].
    pub fn is_running(&mut self) -> bool {
        self.child
            .as_mut()
            .is_some_and(|child| matches!(child.try_wait(), Ok(None)))
    }

    /// Terminate the server and await reaping of the child.
    ///
    /// On Unix the whole process group is signalled (`SIGTERM`, then `SIGKILL`
    /// after a grace period) before the direct child is awaited.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Server`] only if awaiting the child fails; an
    /// already-exited process is not an error.
    pub async fn stop(mut self) -> Result<()> {
        #[cfg(unix)]
        if let Some(pgid) = self.pgid.take() {
            signal_process_group(pgid, SIGTERM);
            tokio::time::sleep(KILL_GRACE).await;
            signal_process_group(pgid, SIGKILL);
        }

        if let Some(mut child) = self.child.take() {
            let _ = child.start_kill();
            child
                .wait()
                .await
                .map_err(|e| Error::Server(format!("awaiting managed server exit failed: {e}")))?;
        }
        Ok(())
    }
}

impl Drop for ManagedServer {
    fn drop(&mut self) {
        let child = self.child.take();

        #[cfg(unix)]
        if let Some(pgid) = self.pgid.take() {
            // The direct child is the group leader, so a group SIGTERM starts
            // its grace period too. Drop cannot await, so the delayed SIGKILL
            // runs on a detached thread that also *owns* the child handle: while
            // it is held across the grace, the leader is not reaped, which both
            // keeps the pgid valid throughout (a zombie stays a group member
            // until reaped, so `-pgid` cannot target a recycled group) and
            // defers the handle's `kill_on_drop` SIGKILL until after the grace —
            // matching the documented SIGTERM-then-SIGKILL teardown.
            signal_process_group(pgid, SIGTERM);
            std::thread::spawn(move || {
                let _child = child;
                std::thread::sleep(KILL_GRACE);
                signal_process_group(pgid, SIGKILL);
            });
            return;
        }

        // Non-unix, or a server already consumed by `stop`: the child handle's
        // `kill_on_drop(true)` is the cross-platform backstop.
        if let Some(mut child) = child {
            let _ = child.start_kill();
        }
    }
}

/// Signal the process group and start killing the direct child (Unix + fallback).
fn signal_group_and_kill(pgid: Option<i32>, child: &mut Child) {
    #[cfg(unix)]
    if let Some(pgid) = pgid {
        signal_process_group(pgid, SIGTERM);
        signal_process_group(pgid, SIGKILL);
    }
    #[cfg(not(unix))]
    let _ = pgid;
    let _ = child.start_kill();
}

/// Extract the base URL from a `listening on http://host:port` stdout line.
///
/// Returns the URL exactly as printed (trailing slash stripped). Falls back to
/// synthesizing `http://<hostname>:<port>` from the requested values if the
/// line matches the marker but no `http` URL can be located.
fn parse_listening_url(line: &str, hostname: &str, port: u16) -> Option<String> {
    let idx = line.find(LISTENING_MARKER)?;
    let rest = line[idx + LISTENING_MARKER.len()..].trim();
    if let Some(start) = rest.find("http") {
        let url = rest[start..].trim().trim_end_matches('/');
        if !url.is_empty() {
            return Some(url.to_string());
        }
    }
    Some(format!("http://{hostname}:{port}"))
}

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

    #[test]
    fn builder_defaults() {
        let b = ManagedServerBuilder::new();
        assert_eq!(b.binary, PathBuf::from("opencode"));
        assert_eq!(b.hostname, "127.0.0.1");
        assert!(b.port.is_none());
        assert!(b.working_dir.is_none());
        assert!(b.password.is_none());
        assert!(b.envs.is_empty());
        assert_eq!(b.startup_timeout, Duration::from_secs(10));
    }

    #[test]
    fn builder_setters() {
        let b = ManagedServer::builder()
            .binary("/opt/opencode")
            .hostname("0.0.0.0")
            .port(1234)
            .working_dir("/tmp/work")
            .password("hunter2")
            .env("FOO", "bar")
            .env("BAZ", "qux")
            .startup_timeout(Duration::from_secs(3));

        assert_eq!(b.binary, PathBuf::from("/opt/opencode"));
        assert_eq!(b.hostname, "0.0.0.0");
        assert_eq!(b.port, Some(1234));
        assert_eq!(b.working_dir, Some(PathBuf::from("/tmp/work")));
        assert_eq!(b.password.as_deref(), Some("hunter2"));
        assert_eq!(b.envs.get("FOO").map(String::as_str), Some("bar"));
        assert_eq!(b.envs.get("BAZ").map(String::as_str), Some("qux"));
        assert_eq!(b.startup_timeout, Duration::from_secs(3));
    }

    #[test]
    fn parse_listening_url_extracts_printed_url() {
        let line = "opencode server listening on http://127.0.0.1:52439";
        assert_eq!(
            parse_listening_url(line, "127.0.0.1", 4096).as_deref(),
            Some("http://127.0.0.1:52439")
        );
    }

    #[test]
    fn parse_listening_url_strips_trailing_slash() {
        let line = "opencode server listening on http://127.0.0.1:52439/";
        assert_eq!(
            parse_listening_url(line, "127.0.0.1", 4096).as_deref(),
            Some("http://127.0.0.1:52439")
        );
    }

    #[test]
    fn parse_listening_url_falls_back_when_no_url() {
        let line = "opencode server listening on <unknown>";
        assert_eq!(
            parse_listening_url(line, "localhost", 9000).as_deref(),
            Some("http://localhost:9000")
        );
    }

    #[test]
    fn parse_listening_url_rejects_unrelated_lines() {
        assert!(parse_listening_url("Warning: password not set", "127.0.0.1", 4096).is_none());
    }

    /// Live smoke test: spawn the real `opencode` binary through
    /// [`ManagedServer`] and confirm it answers `GET /global/health`.
    ///
    /// Gated behind `integration-tests` (in addition to `server`) since it
    /// requires the `opencode` executable. Override its location with
    /// `OPENCODE_BIN`; otherwise the npm-installed default path is used.
    #[cfg(feature = "integration-tests")]
    #[tokio::test]
    async fn managed_server_reaches_health() {
        let binary = std::env::var("OPENCODE_BIN").unwrap_or_else(|_| {
            let home = std::env::var("HOME").expect("HOME must be set");
            format!("{home}/.local/opencode-npm/node_modules/.bin/opencode")
        });

        let mut server = ManagedServer::builder()
            .binary(binary)
            .startup_timeout(Duration::from_secs(30))
            .spawn()
            .await
            .expect("managed server should start");

        assert!(server.is_running());
        assert!(server.url().starts_with("http://127.0.0.1:"));

        let health = format!("{}/global/health", server.url());
        let body: serde_json::Value = reqwest::Client::new()
            .get(&health)
            .send()
            .await
            .expect("health request should succeed")
            .json()
            .await
            .expect("health body should be JSON");
        assert_eq!(body["healthy"], serde_json::Value::Bool(true));

        server.stop().await.expect("server should stop cleanly");
    }
}