aa-cli 0.0.1-beta.3

aasm — command-line tool for Agent Assembly
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
//! `aasm gateway start` — spawn aa-gateway as a detached background process.

use std::path::PathBuf;
use std::process::ExitCode;
use std::time::{Duration, Instant};

use clap::Args;
#[cfg(unix)]
use std::os::unix::process::CommandExt;

use super::pid;

const DEFAULT_LISTEN: &str = "127.0.0.1:50051";
const READINESS_TIMEOUT: Duration = Duration::from_secs(10);
const READINESS_POLL: Duration = Duration::from_millis(200);

/// Arguments for `aasm gateway start`.
#[derive(Debug, Args)]
pub struct StartArgs {
    /// Path to the policy YAML file (overrides $AA_POLICY and default locations).
    #[arg(long)]
    pub policy: Option<PathBuf>,

    /// TCP listen address (e.g. "127.0.0.1:50051").
    #[arg(long, default_value = DEFAULT_LISTEN)]
    pub listen: String,

    /// Unix domain socket path. When set, takes precedence over --listen.
    #[arg(long)]
    pub socket: Option<PathBuf>,

    /// Block the caller rather than detaching the gateway to the background.
    #[arg(long)]
    pub no_detach: bool,

    /// Log file path for aa-gateway stdout/stderr (default ~/.aasm/logs/gateway.log).
    #[arg(long)]
    pub log_file: Option<PathBuf>,
}

/// Dispatch `aasm gateway start`.
pub fn dispatch(args: StartArgs) -> ExitCode {
    let binary = match resolve_binary() {
        Some(b) => b,
        None => {
            eprintln!(
                "error: aa-gateway binary not found.\n\
                 Tried: alongside aasm, $PATH, ~/.cargo/bin/aa-gateway, ./target/release/aa-gateway, ./target/debug/aa-gateway"
            );
            return ExitCode::FAILURE;
        }
    };

    let policy = match resolve_policy(&args) {
        Some(p) => p,
        None => {
            eprintln!(
                "error: no policy file found.\n\
                 Tried: $AA_POLICY, ~/.aasm/policy.yaml, /etc/aasm/policy.yaml\n\
                 Use --policy FILE to specify a path."
            );
            return ExitCode::FAILURE;
        }
    };

    let log_file = resolve_log_file(&args);
    if let Some(parent) = log_file.parent() {
        if let Err(e) = std::fs::create_dir_all(parent) {
            eprintln!("warning: could not create log directory {}: {e}", parent.display());
        }
    }

    let log_fd = match std::fs::OpenOptions::new().create(true).append(true).open(&log_file) {
        Ok(f) => f,
        Err(e) => {
            eprintln!("error: cannot open log file {}: {e}", log_file.display());
            return ExitCode::FAILURE;
        }
    };

    let stderr_fd = log_fd.try_clone().unwrap_or_else(|_| {
        std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&log_file)
            .expect("cannot re-open log file")
    });

    // Spawn aa-gateway with explicit args array — no shell involved.
    let mut cmd = std::process::Command::new(&binary);
    cmd.arg("--policy").arg(&policy);

    if let Some(ref socket) = args.socket {
        cmd.arg("--socket").arg(socket);
    } else {
        cmd.arg("--listen").arg(&args.listen);
    }

    cmd.stdin(std::process::Stdio::null()).stdout(log_fd).stderr(stderr_fd);

    if !args.no_detach {
        // setsid so the child survives shell exit (POSIX only).
        #[cfg(unix)]
        unsafe {
            cmd.pre_exec(|| {
                libc::setsid();
                Ok(())
            });
        }
    }

    let child = match cmd.spawn() {
        Ok(c) => c,
        Err(e) => {
            eprintln!("error: failed to spawn {}: {e}", binary.display());
            return ExitCode::FAILURE;
        }
    };

    let gateway_pid = child.id();
    let listen_display = args
        .socket
        .as_ref()
        .map_or(args.listen.clone(), |s| format!("unix:{}", s.display()));

    let now = chrono::Utc::now().to_rfc3339();
    if let Err(e) = pid::write_pid(gateway_pid, &listen_display, &now) {
        eprintln!("warning: could not write PID file: {e}");
    }

    // Readiness probe: poll TCP until the gateway accepts connections.
    if args.socket.is_none() {
        let addr = args.listen.clone();
        if !wait_for_tcp(&addr, READINESS_TIMEOUT) {
            eprintln!("error: gateway did not become ready within 10s on {addr}");
            eprintln!("       Check logs at {}", log_file.display());
            let _ = pid::remove_pid();
            return ExitCode::FAILURE;
        }
    }

    println!("Gateway started on grpc://{listen_display}  (pid {gateway_pid})");
    println!("Logs: {}", log_file.display());
    ExitCode::SUCCESS
}

/// Resolve the `aa-gateway` binary path.
///
/// Search order: directory of the running `aasm` executable →
/// directories in `$PATH` → `~/.cargo/bin/aa-gateway` →
/// `./target/release/aa-gateway` → `./target/debug/aa-gateway`.
///
/// The exe-dir lookup is first so a release / Homebrew install — where
/// `aa-gateway` ships alongside `aasm` in the same directory (AAASM-2975) —
/// works even when that directory is not on `$PATH` (e.g. a tarball unpacked
/// to an arbitrary location).
pub fn resolve_binary() -> Option<PathBuf> {
    if let Ok(exe) = std::env::current_exe() {
        if let Some(candidate) = sibling_binary(&exe) {
            return Some(candidate);
        }
    }
    if let Ok(path_var) = std::env::var("PATH") {
        for dir in path_var.split(':') {
            let candidate = PathBuf::from(dir).join("aa-gateway");
            if is_executable(&candidate) {
                return Some(candidate);
            }
        }
    }
    if let Some(home) = dirs::home_dir() {
        let candidate = home.join(".cargo").join("bin").join("aa-gateway");
        if is_executable(&candidate) {
            return Some(candidate);
        }
    }
    for rel in &["./target/release/aa-gateway", "./target/debug/aa-gateway"] {
        let candidate = PathBuf::from(rel);
        if is_executable(&candidate) {
            return Some(candidate);
        }
    }
    None
}

/// Return the `aa-gateway` binary sitting next to the given `aasm` executable
/// path, if it exists and is executable.
fn sibling_binary(exe: &std::path::Path) -> Option<PathBuf> {
    let candidate = exe.parent()?.join("aa-gateway");
    is_executable(&candidate).then_some(candidate)
}

#[cfg(unix)]
fn is_executable(path: &std::path::Path) -> bool {
    use std::os::unix::fs::PermissionsExt;
    path.metadata().is_ok_and(|m| m.permissions().mode() & 0o111 != 0)
}

#[cfg(not(unix))]
fn is_executable(path: &std::path::Path) -> bool {
    path.exists()
}

/// Resolve the policy file path.
///
/// Resolution order: `--policy` flag → `$AA_POLICY` → `~/.aasm/policy.yaml` →
/// `/etc/aasm/policy.yaml`.
pub fn resolve_policy(args: &StartArgs) -> Option<PathBuf> {
    if let Some(ref p) = args.policy {
        return Some(p.clone());
    }
    if let Ok(env_path) = std::env::var("AA_POLICY") {
        if !env_path.is_empty() {
            let p = PathBuf::from(&env_path);
            if p.exists() {
                return Some(p);
            }
        }
    }
    if let Some(home) = dirs::home_dir() {
        let p = home.join(".aasm").join("policy.yaml");
        if p.exists() {
            return Some(p);
        }
    }
    let system = PathBuf::from("/etc/aasm/policy.yaml");
    if system.exists() {
        return Some(system);
    }
    None
}

/// Resolve the log file path (--log-file flag or ~/.aasm/logs/gateway.log).
fn resolve_log_file(args: &StartArgs) -> PathBuf {
    if let Some(ref p) = args.log_file {
        return p.clone();
    }
    dirs::home_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join(".aasm")
        .join("logs")
        .join("gateway.log")
}

/// Poll `addr` (TCP connect) until it accepts a connection or `timeout` elapses.
///
/// Uses `connect_timeout` with `READINESS_POLL` as the per-attempt bound so
/// filtered ports (no immediate ECONNREFUSED) cannot block longer than one
/// poll interval — critical for test determinism on Linux CI.
pub fn wait_for_tcp(addr: &str, timeout: Duration) -> bool {
    let Ok(socket_addr) = addr.parse() else {
        return false;
    };
    let deadline = Instant::now() + timeout;
    loop {
        let remaining = deadline.saturating_duration_since(Instant::now());
        if remaining.is_zero() {
            return false;
        }
        if std::net::TcpStream::connect_timeout(&socket_addr, remaining.min(READINESS_POLL)).is_ok() {
            return true;
        }
        let remaining = deadline.saturating_duration_since(Instant::now());
        if remaining.is_zero() {
            return false;
        }
        std::thread::sleep(remaining.min(READINESS_POLL));
    }
}

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

    struct PolicyEnvGuard {
        _lock: MutexGuard<'static, ()>,
        prior: Option<String>,
    }
    impl PolicyEnvGuard {
        fn set(value: &str) -> Self {
            let lock = crate::test_support::env_guard();
            let prior = std::env::var("AA_POLICY").ok();
            std::env::set_var("AA_POLICY", value);
            Self { _lock: lock, prior }
        }
    }
    impl Drop for PolicyEnvGuard {
        fn drop(&mut self) {
            match self.prior.take() {
                Some(v) => std::env::set_var("AA_POLICY", v),
                None => std::env::remove_var("AA_POLICY"),
            }
        }
    }

    #[test]
    fn resolve_policy_uses_flag_when_provided() {
        let args = StartArgs {
            policy: Some(PathBuf::from("/tmp/policy.yaml")),
            listen: DEFAULT_LISTEN.to_string(),
            socket: None,
            no_detach: false,
            log_file: None,
        };
        assert_eq!(resolve_policy(&args), Some(PathBuf::from("/tmp/policy.yaml")));
    }

    #[test]
    fn resolve_policy_uses_env_when_no_flag_and_file_exists() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        let path = tmp.path().to_path_buf();
        let _guard = PolicyEnvGuard::set(path.to_str().unwrap());

        let args = StartArgs {
            policy: None,
            listen: DEFAULT_LISTEN.to_string(),
            socket: None,
            no_detach: false,
            log_file: None,
        };
        let result = resolve_policy(&args);
        assert_eq!(result, Some(path));
    }

    #[test]
    fn resolve_policy_skips_env_when_path_does_not_exist() {
        let _guard = PolicyEnvGuard::set("/nonexistent/path/policy.yaml");

        let args = StartArgs {
            policy: None,
            listen: DEFAULT_LISTEN.to_string(),
            socket: None,
            no_detach: false,
            log_file: None,
        };
        let result = resolve_policy(&args);

        // Falls through to home/system paths; only None if those also don't exist.
        let has_default = dirs::home_dir().is_some_and(|h| h.join(".aasm").join("policy.yaml").exists())
            || PathBuf::from("/etc/aasm/policy.yaml").exists();
        if !has_default {
            assert!(result.is_none());
        }
    }

    #[test]
    fn wait_for_tcp_returns_false_on_closed_port() {
        assert!(!wait_for_tcp("127.0.0.1:1", Duration::from_millis(300)));
    }

    #[test]
    fn wait_for_tcp_returns_true_when_port_is_open() {
        use std::net::TcpListener;
        let _net = crate::test_support::net_guard();
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        let addr = format!("127.0.0.1:{port}");
        assert!(wait_for_tcp(&addr, Duration::from_secs(1)));
    }

    /// Create an executable file at `path` (sets the user-exec bit on Unix).
    fn touch_executable(path: &std::path::Path) {
        std::fs::write(path, b"#!/bin/sh\n").unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = std::fs::metadata(path).unwrap().permissions();
            perms.set_mode(0o755);
            std::fs::set_permissions(path, perms).unwrap();
        }
    }

    #[test]
    fn sibling_binary_resolves_aa_gateway_next_to_exe() {
        let dir = tempfile::tempdir().unwrap();
        let exe = dir.path().join("aasm");
        touch_executable(&exe);
        let gateway = dir.path().join("aa-gateway");
        touch_executable(&gateway);

        assert_eq!(sibling_binary(&exe), Some(gateway));
    }

    #[test]
    fn sibling_binary_returns_none_when_gateway_absent() {
        let dir = tempfile::tempdir().unwrap();
        let exe = dir.path().join("aasm");
        touch_executable(&exe);
        // No aa-gateway alongside it.
        assert_eq!(sibling_binary(&exe), None);
    }

    #[cfg(unix)]
    #[test]
    fn sibling_binary_returns_none_when_gateway_not_executable() {
        let dir = tempfile::tempdir().unwrap();
        let exe = dir.path().join("aasm");
        touch_executable(&exe);
        // A non-executable file named aa-gateway must not be selected.
        std::fs::write(dir.path().join("aa-gateway"), b"not a binary").unwrap();
        assert_eq!(sibling_binary(&exe), None);
    }
}