adbrent 0.3.2

One Android device per AI agent: an adb device lock for parallel Claude Code, Codex and other agent sessions
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
//! Rental state: one small JSON file per rented device under `~/.adbrent`.
//! There is no daemon. A rental whose owner process is gone is simply ignored
//! and overwritten by the next renter.

use crate::proc;
use serde::{Deserialize, Serialize};
use std::os::unix::io::AsRawFd;
use std::path::PathBuf;
use std::{env, fs, io};

pub fn home() -> PathBuf {
    if let Some(h) = env::var_os("ADBRENT_HOME").filter(|h| !h.is_empty()) {
        return h.into();
    }
    PathBuf::from(env::var_os("HOME").unwrap_or_else(|| "/tmp".into())).join(".adbrent")
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Lease {
    pub serial: String,
    pub owner: String,
    pub pid: i32,
    pub start: u64,
    pub label: String,
    pub since: u64,
    /// Other sessions the holder has let in with `adbrent share`.
    #[serde(default)]
    pub guests: Vec<String>,
    /// The code those sessions joined with, once one has been made.
    #[serde(default)]
    pub code: String,
}

impl Lease {
    pub fn alive(&self) -> bool {
        proc::alive(self.pid, self.start)
    }
    /// May `me` use this device: its renter, or a session the renter let in.
    pub fn allows(&self, me: &Me) -> bool {
        self.owner == me.owner || self.guests.contains(&me.owner)
    }
    pub fn age(&self) -> String {
        let s = now().saturating_sub(self.since);
        match s {
            0..=59 => format!("{s}s"),
            60..=3599 => format!("{}m", s / 60),
            _ => format!("{}h{}m", s / 3600, s % 3600 / 60),
        }
    }
}

#[derive(Serialize, Deserialize)]
pub struct Session {
    pub pid: i32,
    pub start: u64,
    pub name: String,
    pub cwd: String,
}

/// The identity of whoever is running this command.
pub struct Me {
    pub owner: String,
    pub pid: i32,
    pub start: u64,
    pub label: String,
}

pub fn now() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_secs())
}

fn safe(name: &str) -> String {
    name.bytes()
        .map(|b| match b {
            b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'.' | b'_' | b'-' => (b as char).to_string(),
            _ => format!("%{b:02X}"),
        })
        .collect()
}

fn lease_path(serial: &str) -> PathBuf {
    home().join("leases").join(format!("{}.json", safe(serial)))
}

fn session_path(id: &str) -> PathBuf {
    home().join("sessions").join(format!("{}.json", safe(id)))
}

fn write_json<T: Serialize>(path: &PathBuf, value: &T) -> io::Result<()> {
    if let Some(dir) = path.parent() {
        fs::create_dir_all(dir)?;
    }
    let tmp = path.with_extension(format!("tmp{}", std::process::id()));
    fs::write(&tmp, serde_json::to_vec(value)?)?;
    fs::rename(&tmp, path)
}

fn read_json<T: for<'de> Deserialize<'de>>(path: &PathBuf) -> Option<T> {
    serde_json::from_slice(&fs::read(path).ok()?).ok()
}

/// Exclusive lock over all rental changes; released when dropped.
pub struct Lock(#[allow(dead_code)] fs::File);

pub fn lock() -> io::Result<Lock> {
    fs::create_dir_all(home())?;
    let f = fs::OpenOptions::new()
        .create(true)
        .write(true)
        .truncate(false)
        .open(home().join("lock"))?;
    if unsafe { libc::flock(f.as_raw_fd(), libc::LOCK_EX) } != 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(Lock(f))
}

pub fn read_lease(serial: &str) -> Option<Lease> {
    read_json(&lease_path(serial))
}

fn all() -> Vec<Lease> {
    let Ok(dir) = fs::read_dir(home().join("leases")) else {
        return Vec::new();
    };
    dir.flatten()
        .filter(|e| e.path().extension().is_some_and(|x| x == "json"))
        .filter_map(|e| read_json::<Lease>(&e.path()))
        .collect()
}

/// All rentals whose owner is still alive.
pub fn leases() -> Vec<Lease> {
    all().into_iter().filter(Lease::alive).collect()
}

/// Move rentals this same process took under an earlier session id over to `me`.
pub fn adopt(me: &Me) -> io::Result<()> {
    let _lock = lock()?;
    for mut l in all() {
        if l.pid == me.pid && l.start == me.start && l.owner != me.owner {
            l.owner = me.owner.clone();
            l.label = me.label.clone();
            write_json(&lease_path(&l.serial), &l)?;
        }
    }
    Ok(())
}

/// The live rental on `serial` held by someone other than `me`, if any.
pub fn held_by_other(serial: &str, me: &Me) -> Option<Lease> {
    read_lease(serial).filter(|l| !l.allows(me) && l.alive())
}

/// True when `me` already holds `serial` and nothing needs writing.
pub fn held_by_me(serial: &str, me: &Me) -> bool {
    read_lease(serial).is_some_and(|l| l.allows(me) && l.alive())
}

/// Rent `serial` for `me`. `Ok(Ok(true))` is a new rental, `Ok(Ok(false))` one
/// already held, `Ok(Err(lease))` means another live owner holds it.
pub fn acquire(serial: &str, me: &Me) -> io::Result<Result<bool, Lease>> {
    let _lock = lock()?;
    let mut lease = Lease {
        serial: serial.to_string(),
        owner: me.owner.clone(),
        pid: me.pid,
        start: me.start,
        label: me.label.clone(),
        since: now(),
        guests: Vec::new(),
        code: String::new(),
    };
    let mut new = true;
    if let Some(l) = read_lease(serial) {
        if l.alive() {
            // Held under a live process. Ours (as renter or guest): leave it alone,
            // whichever of the session's processes is asking. Otherwise: refused.
            return Ok(if l.allows(me) { Ok(false) } else { Err(l) });
        }
        if l.owner == me.owner {
            // Same session under a new process (a resumed session): keep the rental.
            lease.since = l.since;
            lease.guests = l.guests;
            lease.code = l.code;
            new = false;
        } else {
            record_span(&l);
        }
    }
    write_json(&lease_path(serial), &lease)?;
    Ok(Ok(new))
}

/// Make a code that lets other sessions use `serial` alongside `me`, its renter.
pub fn share(serial: &str, me: &Me) -> io::Result<Option<String>> {
    let _lock = lock()?;
    let Some(mut l) = read_lease(serial).filter(|l| l.owner == me.owner && l.alive()) else {
        return Ok(None);
    };
    if l.code.is_empty() {
        let nanos = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |d| d.subsec_nanos());
        l.code = format!(
            "{:06x}",
            (nanos ^ std::process::id().wrapping_mul(2_654_435_761)) & 0xff_ffff
        );
        write_json(&lease_path(serial), &l)?;
    }
    Ok(Some(l.code))
}

/// Let `me` in on the device whose renter made `code`. Returns its serial.
pub fn join(code: &str, me: &Me) -> io::Result<Option<String>> {
    let _lock = lock()?;
    let Some(mut l) = all()
        .into_iter()
        .find(|l| !l.code.is_empty() && l.code == code && l.alive())
    else {
        return Ok(None);
    };
    if !l.allows(me) {
        l.guests.push(me.owner.clone());
        write_json(&lease_path(&l.serial), &l)?;
    }
    Ok(Some(l.serial))
}

/// Take `owner` off every device it was let in on.
pub fn leave(owner: &str) -> io::Result<()> {
    let _lock = lock()?;
    for mut l in all() {
        if l.guests.iter().any(|g| g == owner) {
            l.guests.retain(|g| g != owner);
            write_json(&lease_path(&l.serial), &l)?;
        }
    }
    Ok(())
}

/// Remove rentals matching `pick`, plus any whose owner has died. Returns the
/// serials that `pick` released.
pub fn release(pick: impl Fn(&Lease) -> bool) -> io::Result<Vec<String>> {
    let _lock = lock()?;
    let mut released = Vec::new();
    let Ok(dir) = fs::read_dir(home().join("leases")) else {
        return Ok(released);
    };
    for entry in dir.flatten() {
        let path = entry.path();
        let Some(lease) = read_json::<Lease>(&path) else {
            continue;
        };
        if pick(&lease) {
            fs::remove_file(&path)?;
            record_span(&lease);
            released.push(lease.serial);
        } else if !lease.alive() {
            let _ = fs::remove_file(&path);
            record_span(&lease);
        }
    }
    Ok(released)
}

fn span_path(serial: &str) -> PathBuf {
    home()
        .join("history")
        .join(format!("{}.json", safe(serial)))
}

/// Note that `lease` has ended, so that whatever its renter left running on
/// the device is later recognised as a leftover and not as someone else's use.
fn record_span(lease: &Lease) {
    let path = span_path(&lease.serial);
    let mut spans: Vec<(u64, u64)> = read_json(&path).unwrap_or_default();
    spans.push((lease.since, now()));
    let keep = spans.len().saturating_sub(20);
    let _ = write_json(&path, &spans[keep..].to_vec());
}

/// The periods (start, end) during which `serial` was rented: past rentals,
/// and the present one if there is one.
pub fn spans(serial: &str) -> Vec<(u64, u64)> {
    let mut spans: Vec<(u64, u64)> = read_json(&span_path(serial)).unwrap_or_default();
    if let Some(l) = read_lease(serial) {
        spans.push((l.since, now()));
    }
    spans
}

/// A user-kept list, one entry per line: `ignore` (pieces of text that mark
/// an on-device command as a helper to disregard) or `exclude` (serials of
/// devices agents must leave alone).
pub fn list(name: &str) -> Vec<String> {
    fs::read_to_string(home().join(name))
        .unwrap_or_default()
        .lines()
        .map(str::trim)
        .filter(|l| !l.is_empty())
        .map(String::from)
        .collect()
}

pub fn set_list(name: &str, entries: &[String]) -> io::Result<()> {
    fs::create_dir_all(home())?;
    fs::write(
        home().join(name),
        entries.iter().map(|e| format!("{e}\n")).collect::<String>(),
    )
}

pub fn write_session(id: &str, session: &Session) -> io::Result<()> {
    write_json(&session_path(id), session)
}

/// Forget sessions whose process is gone (a crashed session never says goodbye).
pub fn sweep_sessions() {
    let Ok(dir) = fs::read_dir(home().join("sessions")) else {
        return;
    };
    for entry in dir.flatten() {
        let path = entry.path();
        if read_json::<Session>(&path).is_none_or(|s| !proc::alive(s.pid, s.start)) {
            let _ = fs::remove_file(path);
        }
    }
}

pub fn remove_session(id: &str) {
    let _ = fs::remove_file(session_path(id));
}

pub fn me() -> Me {
    // A session id travels in the environment, so it also reaches background
    // jobs that have been cut loose from the agent's process tree. Ours is set
    // by the plugin; Claude Code sets its own in every command it runs.
    let named = |var: &str| env::var(var).ok().filter(|s| !s.is_empty());
    match named("ADBRENT_SESSION").or_else(|| named("CLAUDE_CODE_SESSION_ID")) {
        Some(id) => me_for_session(&id),
        None => {
            let a = anchor();
            Me {
                owner: format!("p:{}:{}", a.pid, a.start),
                pid: a.pid,
                start: a.start,
                label: format!("{} (pid {})", a.name, a.pid),
            }
        }
    }
}

/// Identity for a harness session that registered itself (or was named through
/// `ADBRENT_SESSION`). The rental follows the session id, and the registered
/// process tells us when the session is gone.
pub fn me_for_session(id: &str) -> Me {
    let short: String = id.chars().take(8).collect();
    let owner = format!("s:{id}");
    if let Some(s) = read_json::<Session>(&session_path(id)).filter(|s| proc::alive(s.pid, s.start))
    {
        let dir = s.cwd.rsplit('/').next().unwrap_or_default();
        let place = if dir.is_empty() {
            String::new()
        } else {
            format!(" in {dir}")
        };
        return Me {
            owner,
            pid: s.pid,
            start: s.start,
            label: format!("{} session {short}{place}", s.name),
        };
    }
    // Claude Code names its own process to the commands it runs.
    let claude = env::var("CLAUDE_PID")
        .ok()
        .and_then(|p| proc::info(p.parse().ok()?));
    if let Some(p) = claude {
        return Me {
            owner,
            pid: p.pid,
            start: p.start,
            label: format!("claude session {short}"),
        };
    }
    let a = anchor();
    Me {
        owner,
        pid: a.pid,
        start: a.start,
        label: format!("{} session {short}", a.name),
    }
}

/// Note something worth the user's attention: a refusal, a forced rental, an
/// agent's feedback. Kept on this machine only; never allowed to fail a command.
pub fn log_event(kind: &str, me: &Me, serial: &str, detail: &str) {
    use std::io::Write;
    let path = home().join("log.jsonl");
    let line = serde_json::json!({
        "t": now(), "kind": kind, "who": me.label, "serial": serial,
        "detail": detail, "version": env!("CARGO_PKG_VERSION"),
    });
    // Keep the log small: past 1 MB, drop the older half.
    if fs::metadata(&path).is_ok_and(|m| m.len() > 1_000_000) {
        if let Ok(text) = fs::read_to_string(&path) {
            let lines: Vec<&str> = text.lines().collect();
            let _ = fs::write(&path, lines[lines.len() / 2..].join("\n") + "\n");
        }
    }
    let _ = fs::create_dir_all(home());
    if let Ok(mut f) = fs::OpenOptions::new().create(true).append(true).open(path) {
        let _ = writeln!(f, "{line}");
    }
}

pub fn events() -> Vec<serde_json::Value> {
    fs::read_to_string(home().join("log.jsonl"))
        .unwrap_or_default()
        .lines()
        .filter_map(|l| serde_json::from_str(l).ok())
        .collect()
}

pub fn anchor() -> proc::Proc {
    proc::anchor()
        .or_else(|| proc::info(std::process::id() as i32))
        .unwrap_or(proc::Proc {
            pid: std::process::id() as i32,
            ppid: 0,
            name: "unknown".into(),
            start: 0,
        })
}