ertangle-adapter 1.7.0

Agent adapters (Claude Code, Codex, ACP) for ertangle
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
//! `ertangle-adapter` — Normalization layer for diverse agents.
//!
//! Protocols for each agent (ACP, stream-json, app-server, PTY) are translated here into
//! a uniform set of [`AgentEvent`]s, so layers above the daemon do not need to know which agent is underneath.
//!
//! Structured adapters (native, zero extra dependencies):
//! * [`acp`]: Agent Client Protocol — OpenCode (`opencode acp`), Antigravity (`agy acp`).
//! * [`claude`]: Claude Code's `--input-format/--output-format stream-json`.
//! * [`codex`]: Codex's `codex app-server` (stdio JSON-RPC).
//!
//! Universal fallback:
//! * [`pty`]: PTY passthrough, **works for any CLI agent**.

pub mod acp;
pub mod ansi;
pub mod claude;
pub mod codex;
pub mod models;
pub mod pty;
pub mod question;
pub mod stderr;
pub mod stream;

use anyhow::{bail, Context, Result};
use ertangle_core::{
    AgentKind, Cost, Decision, PermissionPolicy, PermissionRequest, SessionId, SessionStatus,
};
use ertangle_protocol::Transport;
use std::path::PathBuf;
use std::sync::mpsc::Sender;
use tr::tr;

/// Events sent upward after normalization by the adapter.
#[derive(Debug, Clone)]
pub enum AgentEvent {
    Status(SessionStatus),
    /// Text output from the agent (may contain newlines).
    Output(String),
    /// Continues the last unterminated output line (the first segment of the text is
    /// appended to it), so a mid-line stream pause does not split Markdown constructs.
    OutputAppend(String),
    ToolCall {
        name: String,
        input: String,
        /// `None` = in progress.
        ok: Option<bool>,
        output: Option<String>,
    },
    Permission(PermissionRequest),
    Question(ertangle_core::UserQuestion),
    Cost(Cost),
    Model(String),
    /// Native slash command names of the agent (without `/`).
    Commands(Vec<String>),
    /// Native session ID of the agent (for resumption). Sent only when resumption is possible with this ID.
    NativeId(String),
    /// Quota exhausted / rate limited.
    Limited(String),
    /// Sets a status note for the session (e.g. failure reason).
    Note(String),
    /// Session context usage.
    Context {
        used: u64,
        window: Option<u64>,
    },
    /// Account quota status update.
    Quota(ertangle_core::Quota),
    /// Process exited.
    Exited(Option<i32>),
}

/// All information required to launch a session.
#[derive(Debug, Clone)]
pub struct SessionSpec {
    pub id: SessionId,
    pub kind: AgentKind,
    pub cwd: PathBuf,
    pub transport: Transport,
    /// Custom executable path or name (overrides default PATH lookup).
    pub bin: Option<String>,
    /// Extra command-line arguments.
    pub args: Vec<String>,
    /// Model to use (currently only supported by Claude structured mode).
    pub model: Option<String>,
    /// Reasoning effort (Claude `--effort`, Codex `model_reasoning_effort`, ACP configOption).
    pub effort: Option<String>,
    /// Claude-specific: extra allowed / disallowed tools.
    pub allowed_tools: Vec<String>,
    pub disallowed_tools: Vec<String>,
    /// Read-only mode (achieved via plan mode in Claude).
    pub read_only: bool,
    /// Codex-specific sandbox mode.
    pub sandbox: Option<String>,
    /// Extra environment variables.
    pub env: Vec<(String, String)>,
    /// Unified permission policy.
    pub permission: PermissionPolicy,
    /// If the `ertangle` executable path is provided, the adapter injects `ertangle mcp` into the agent at startup.
    pub mcp_exe: Option<PathBuf>,
    /// Collaboration instructions appended to the system prompt (Claude `--append-system-prompt`, Codex `developerInstructions`)
    /// or prepended to the first prompt in ACP; not injected for PTY.
    pub system_append: Option<String>,
    /// First prompt sent immediately after startup.
    pub initial_prompt: Option<String>,
    /// Attachments for the initial prompt.
    pub initial_attachments: Vec<ertangle_core::Attachment>,
    /// Resume an existing conversation: agent's native conversation ID. `None` starts a new conversation.
    pub resume: Option<String>,
    pub cols: u16,
    pub rows: u16,
}

/// Control handle for a running session.
pub trait AgentControl: Send + Sync {
    /// Sends user input / prompt text.
    fn send_input(&self, text: &str) -> Result<()>;
    /// Sends a prompt with files/images attached. By default inlines attachments into text; adapters supporting image blocks override this.
    fn send_input_with(&self, text: &str, attachments: &[ertangle_core::Attachment]) -> Result<()> {
        self.send_input(&ertangle_core::compose_text(text, attachments, true))
    }
    /// Interrupts the current turn (without killing the process).
    fn cancel(&self) -> Result<()>;
    /// Terminates and kills the process.
    fn kill(&self) -> Result<()>;
    /// Pauses child processes (SIGSTOP, used for idle sleep).
    fn sleep(&self) -> Result<()> {
        Ok(())
    }
    /// Wakes child processes (SIGCONT).
    fn wake(&self) -> Result<()> {
        Ok(())
    }
    /// Resolves a permission request.
    fn resolve_permission(&self, req: &str, decision: Decision) -> Result<()>;
    /// Resolves an interactive user question.
    fn resolve_question(&self, id: &str, answers: &[String]) -> Result<()> {
        let _ = (id, answers);
        bail!(
            "{}",
            tr!("{transport} 不支援回答問題", transport = self.transport())
        )
    }
    /// Actual transport used by this session (for display).
    fn transport(&self) -> &'static str;
    /// Gets the agent main process PID.
    fn pid(&self) -> Option<u32> {
        None
    }
    /// Switches model and/or effort at runtime (takes effect from the next turn).
    /// On success returns the model actually in effect (if reported by the agent); unsupported adapters return a clear error.
    fn set_config(&self, model: Option<&str>, effort: Option<&str>) -> Result<Option<String>> {
        let _ = (model, effort);
        bail!(
            "{}",
            tr!(
                "{transport} 不支援執行期切換 model/effort",
                transport = self.transport()
            )
        )
    }
}

/// Checks if an effort value is valid.
pub fn check_effort(e: &str) -> Result<()> {
    if !EFFORT_LEVELS.contains(&e) {
        bail!(
            "{}",
            tr!(
                "未知的 effort `{e}`;可用:{list}",
                e = e,
                list = EFFORT_LEVELS.join(", ")
            )
        );
    }
    Ok(())
}

/// Builds arguments for `kill -s <NAME> -- <target>`.
///
/// Always use the `-s NAME --` form: procps-ng 4.0.4's `kill -KILL -<pgid>` misparses
/// `-<pgid>` as `-1` (all processes for the user), equivalent to `kill -9 -1`, which logs out the entire desktop.
fn kill_args(sig: &str, target: String) -> [String; 4] {
    let name = sig.trim_start_matches('-').trim_start_matches("SIG");
    ["-s".into(), name.into(), "--".into(), target]
}

fn run_kill(args: [String; 4]) -> Result<()> {
    let status = std::process::Command::new("kill")
        .args(&args)
        .stderr(std::process::Stdio::null())
        .status()
        .with_context(|| tr!("執行 kill 失敗"))?;
    if !status.success() {
        bail!("{}", tr!("kill {args} 失敗", args = args.join(" ")));
    }
    Ok(())
}

/// Sends a Unix signal (e.g. `-STOP` / `-CONT`) to a specific PID.
pub fn signal_pid(pid: u32, sig: &str) -> Result<()> {
    // Never send to 0 / 1 as they refer to "own group" or init.
    if pid <= 1 {
        bail!("{}", tr!("拒絕對 PID {pid} 送訊號", pid = pid));
    }
    run_kill(kill_args(sig, pid.to_string()))
}

/// Sends a Unix signal (e.g. `-STOP` / `-CONT` / `-KILL`) to a specific process group (PGID).
pub fn signal_pgid(pgid: u32, sig: &str) -> Result<()> {
    // Never send to `-0` (own group) or `-1` (all processes).
    if pgid <= 1 {
        bail!("{}", tr!("拒絕對 PID {pid} 送訊號", pid = pgid));
    }
    run_kill(kill_args(sig, format!("-{pgid}")))
}

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

    #[test]
    fn kill_args_never_put_negative_target_before_double_dash() {
        assert_eq!(
            kill_args("-KILL", "-4242".into()),
            ["-s", "KILL", "--", "-4242"]
        );
        assert_eq!(kill_args("-STOP", "42".into()), ["-s", "STOP", "--", "42"]);
        assert_eq!(
            kill_args("SIGCONT", "42".into()),
            ["-s", "CONT", "--", "42"]
        );
    }

    #[test]
    fn refuses_pid_zero_and_one() {
        assert!(signal_pgid(0, "-KILL").is_err());
        assert!(signal_pgid(1, "-KILL").is_err());
        assert!(signal_pid(0, "-KILL").is_err());
        assert!(signal_pid(1, "-KILL").is_err());
    }

    #[test]
    fn pgid_signal_hits_only_the_group() {
        use std::os::unix::process::CommandExt;
        let mut child = std::process::Command::new("sleep")
            .arg("30")
            .process_group(0)
            .spawn()
            .unwrap();
        signal_pgid(child.id(), "-KILL").unwrap();
        let st = child.wait().unwrap();
        assert!(!st.success());
    }
}

/// SSH 相關的環境變數;agy 看到任一個就判定為 SSH session。
const SSH_ENV_VARS: &[&str] = &["SSH_CLIENT", "SSH_CONNECTION", "SSH_TTY"];

/// 啟動該 agent 前應從環境移除的變數。
///
/// agy 偵測到 SSH session 時會改用檔案儲存 token,讀不到桌面 keyring 裡的登入,於是一直回報
/// 「未登入」。daemon 從 SSH 終端機啟動時會繼承這些變數,所以在 keyring 可用 (有 D-Bus session)
/// 時移除它們,讓 agy 照常讀 keyring。
pub fn env_removals(kind: &AgentKind) -> &'static [&'static str] {
    let has_dbus = std::env::var_os("DBUS_SESSION_BUS_ADDRESS").is_some_and(|v| !v.is_empty());
    env_removals_for(kind, has_dbus)
}

fn env_removals_for(kind: &AgentKind, has_dbus: bool) -> &'static [&'static str] {
    match kind {
        AgentKind::Antigravity if has_dbus => SSH_ENV_VARS,
        _ => &[],
    }
}

/// Probes whether an executable exists and returns its absolute path.
pub fn which(prog: &str) -> Option<PathBuf> {
    if prog.contains('/') {
        let p = PathBuf::from(prog);
        return p.is_file().then_some(p);
    }
    let path = std::env::var_os("PATH")?;
    for dir in std::env::split_paths(&path) {
        let cand = dir.join(prog);
        if cand.is_file() {
            return Some(cand);
        }
    }
    None
}

/// Finds the agent executable: checks environment variable override first (for testing and custom installs), then searches PATH.
pub fn agent_bin(prog: &str, env_var: &str) -> Option<PathBuf> {
    match ertangle_core::env_var(env_var) {
        Some(p) if !p.is_empty() => which(&p),
        _ => which(prog),
    }
}

/// Valid reasoning effort levels.
pub const EFFORT_LEVELS: [&str; 5] = ["low", "medium", "high", "xhigh", "max"];

/// Starts a session and returns a control handle; events stream through `tx`.
///
/// Determines whether to use native structured protocols or PTY passthrough based on `spec.transport`.
/// `Auto` chooses the best native protocol for the agent; falls back to PTY if native startup fails.
pub fn spawn(spec: SessionSpec, tx: Sender<AgentEvent>) -> Result<Box<dyn AgentControl>> {
    // Explicitly reject unsupported granular permission options instead of silently ignoring them.
    let is_claude = matches!(spec.kind, AgentKind::ClaudeCode);
    if let Some(e) = &spec.effort {
        if !EFFORT_LEVELS.contains(&e.as_str()) {
            bail!(
                "{}",
                tr!(
                    "未知的 effort `{e}`;可用:{list}",
                    e = e,
                    list = EFFORT_LEVELS.join(", ")
                )
            );
        }
    }
    let is_codex = matches!(spec.kind, AgentKind::Codex);
    if !is_claude && (!spec.allowed_tools.is_empty() || !spec.disallowed_tools.is_empty()) {
        bail!(
            "{}",
            tr!(
                "{kind} 尚不支援 allowed_tools/disallowed_tools;請改用 claude",
                kind = spec.kind
            )
        );
    }
    if spec.read_only && !is_claude && !is_codex {
        bail!(
            "{}",
            tr!(
                "{kind} 尚不支援 read_only;請改用 claude 或 codex",
                kind = spec.kind
            )
        );
    }
    if spec.sandbox.is_some() && !is_codex {
        bail!("{}", tr!("sandbox 只適用於 codex"));
    }
    // Resumption is only supported via native protocols; do not silently start a new conversation.
    if spec.resume.is_some() {
        if spec.transport == Transport::Pty {
            bail!("{}", tr!("PTY 模式無法接續對話"));
        }
        if matches!(spec.kind, AgentKind::Custom(_)) {
            bail!(
                "{}",
                tr!("{kind} 沒有原生結構化協定,無法接續對話", kind = spec.kind)
            );
        }
    }
    let want_structured = match spec.transport {
        _ if spec.resume.is_some() => true,
        Transport::Pty => false,
        Transport::Structured => true,
        Transport::Auto => supports_structured(&spec.kind),
    };

    if want_structured {
        match structured_start(spec.clone(), tx.clone()) {
            Ok(ctl) => return Ok(ctl),
            Err(e)
                if spec.transport == Transport::Auto
                    && spec.model.is_none()
                    && spec.resume.is_none() =>
            {
                tracing::warn!("{} 原生協定啟動失敗,退回 PTY:{e}", spec.kind);
            }
            Err(e) => return Err(e),
        }
    }

    if let Some(m) = &spec.model {
        if spec.transport != Transport::Pty {
            bail!(
                "{}",
                tr!(
                    "{kind} 尚不支援以結構化模式指定 model ({m});請改用 --transport pty 並以參數傳入",
                    kind = spec.kind,
                    m = m
                )
            );
        }
    }
    let (prog, _) = spec.kind.pty_command();
    if which(prog).is_none() {
        bail!("{}", tr!("找不到 `{prog}`,請先安裝", prog = prog));
    }
    pty::start(spec, tx)
}

/// Whether this agent has a native structured protocol available.
pub fn supports_structured(kind: &AgentKind) -> bool {
    match kind {
        AgentKind::OpenCode | AgentKind::Antigravity => kind.acp_command().is_some(),
        AgentKind::ClaudeCode => agent_bin("claude", "ERTANGLE_CLAUDE_BIN").is_some(),
        AgentKind::Codex => which("codex").is_some(),
        AgentKind::Custom(_) => false,
    }
}

/// Checks whether the corresponding structured agent binary is installed in local `$PATH`.
pub fn is_installed(kind: &AgentKind) -> bool {
    match kind {
        AgentKind::ClaudeCode => agent_bin("claude", "ERTANGLE_CLAUDE_BIN").is_some(),
        _ => is_installed_with(kind, |prog| which(prog).is_some()),
    }
}

fn is_installed_with<F: Fn(&str) -> bool>(kind: &AgentKind, has_bin: F) -> bool {
    match kind {
        AgentKind::OpenCode | AgentKind::Antigravity => {
            kind.acp_command().is_some_and(|(prog, _)| has_bin(prog))
        }
        AgentKind::ClaudeCode => has_bin("claude"),
        AgentKind::Codex => has_bin("codex"),
        AgentKind::Custom(_) => false,
    }
}

/// Starts the corresponding native structured adapter based on agent kind.
fn structured_start(spec: SessionSpec, tx: Sender<AgentEvent>) -> Result<Box<dyn AgentControl>> {
    match spec.kind {
        AgentKind::OpenCode | AgentKind::Antigravity => {
            let (prog, _) = spec
                .kind
                .acp_command()
                .with_context(|| tr!("此 agent 不支援 ACP"))?;
            let target = spec.bin.as_deref().unwrap_or(prog);
            if which(target).is_none() {
                bail!(
                    "{}",
                    tr!(
                        "找不到 `{target}`,請先安裝或改用 --transport pty",
                        target = target
                    )
                );
            }
            acp::start(spec, tx)
        }
        AgentKind::ClaudeCode => {
            let target = spec.bin.as_deref().unwrap_or("claude");
            if spec.bin.is_none() && ertangle_core::env_var_os("ERTANGLE_CLAUDE_BIN").is_some() {
                // Handled by claude::start via ERTANGLE_CLAUDE_BIN
            } else if which(target).is_none() {
                bail!(
                    "{}",
                    tr!(
                        "找不到 `{target}`,請先安裝或改用 --transport pty",
                        target = target
                    )
                );
            }
            claude::start(spec, tx)
        }
        AgentKind::Codex => {
            let target = spec.bin.as_deref().unwrap_or("codex");
            if which(target).is_none() {
                bail!(
                    "{}",
                    tr!(
                        "找不到 `{target}`,請先安裝或改用 --transport pty",
                        target = target
                    )
                );
            }
            codex::start(spec, tx)
        }
        AgentKind::Custom(_) => bail!(
            "{}",
            tr!("自訂 agent 沒有原生結構化協定,請用 --transport pty")
        ),
    }
}

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

    #[test]
    fn which_finds_sh_and_misses_nonsense() {
        assert!(which("sh").is_some());
        assert!(which("definitely-not-a-real-binary-xyz").is_none());
    }

    #[test]
    fn is_installed_checks_each_agent_binary() {
        let only_agy_and_claude = |bin: &str| matches!(bin, "agy" | "claude");
        assert!(is_installed_with(
            &AgentKind::Antigravity,
            only_agy_and_claude
        ));
        assert!(is_installed_with(
            &AgentKind::ClaudeCode,
            only_agy_and_claude
        ));
        assert!(!is_installed_with(
            &AgentKind::OpenCode,
            only_agy_and_claude
        ));
        assert!(!is_installed_with(&AgentKind::Codex, only_agy_and_claude));
        assert!(!is_installed_with(&AgentKind::Custom("foo".into()), |_| {
            true
        }));
    }
}

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

    #[test]
    fn agy_drops_ssh_vars_only_when_keyring_reachable() {
        assert_eq!(
            env_removals_for(&AgentKind::Antigravity, true),
            SSH_ENV_VARS
        );
        assert!(env_removals_for(&AgentKind::Antigravity, false).is_empty());
        assert!(env_removals_for(&AgentKind::OpenCode, true).is_empty());
        assert!(env_removals_for(&AgentKind::Codex, true).is_empty());
    }
}