fux 0.2.0

Agent-native terminal workspace
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
use super::{
    DaemonPaths, Descriptor, DescriptorError, IDLE_WORKSPACE_TTL_MS, INITIAL_REQUEST_GRACE_MS,
    MAX_CONNECTIONS_PER_WORKSPACE, MAX_WORKSPACES, ManagerIdentity, PathError,
    recover_stale_descriptors, write_descriptor,
};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::fs;
use std::future::Future;
use std::io::{self, Read};
use std::net::SocketAddrV4;
use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::{Path, PathBuf};

pub trait EndpointHandle: Send {
    fn endpoint_id(&self) -> &str;
    fn direct_addr(&self) -> SocketAddrV4;
    fn close(&mut self);
    fn reap_terminal_sessions(&mut self, now_ms: u64, ttl_ms: u64);
    fn active_tasks(&self) -> usize {
        0
    }
}

pub trait EndpointFactory {
    fn create(
        &mut self,
        name: &str,
        key_path: &Path,
        allow: &BTreeSet<String>,
    ) -> Result<Box<dyn EndpointHandle>, ManagerError>;
}

pub struct WorkspaceEndpoint {
    pub descriptor: Descriptor,
    endpoint: Box<dyn EndpointHandle>,
    allow: BTreeSet<String>,
    local_client_id: String,
    viewers: usize,
}

impl WorkspaceEndpoint {
    pub fn authorize_and_attach(&mut self, peer: &str) -> Result<(), ManagerError> {
        if peer != self.local_client_id && !self.allow.contains(peer) {
            return Err(ManagerError::Unauthorized);
        }
        if self.viewers >= MAX_CONNECTIONS_PER_WORKSPACE {
            return Err(ManagerError::ConnectionLimit);
        }
        self.viewers += 1;
        Ok(())
    }
    pub fn detach(&mut self) {
        self.viewers = self.viewers.saturating_sub(1);
    }
    pub const fn viewers(&self) -> usize {
        self.viewers
    }
}

pub struct Daemon {
    paths: DaemonPaths,
    identity: ManagerIdentity,
    workspaces: BTreeMap<String, WorkspaceEndpoint>,
    explicit_allow: BTreeSet<String>,
    local_client_id: String,
    started_ms: u64,
    received_initial_request: bool,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Resolution {
    Attach(Descriptor),
    Create(String),
    Pick(Vec<String>),
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DaemonAction {
    Continue,
    ReplyThenExit,
}

impl Daemon {
    pub fn new(
        paths: DaemonPaths,
        pid: u32,
        explicit_allow: BTreeSet<String>,
        local_client_id: String,
        started_ms: u64,
    ) -> Result<Self, ManagerError> {
        paths.prepare()?;
        if pid == 0 || local_client_id.is_empty() {
            return Err(ManagerError::Invalid);
        }
        let identity = ManagerIdentity {
            pid,
            instance_nonce: random_nonce()?,
        };
        recover_stale_descriptors(&paths, &identity)?;
        Ok(Self {
            paths,
            identity,
            workspaces: BTreeMap::new(),
            explicit_allow,
            local_client_id,
            started_ms,
            received_initial_request: false,
        })
    }

    pub fn identity(&self) -> &ManagerIdentity {
        &self.identity
    }
    pub fn workspace(&self, name: &str) -> Option<&WorkspaceEndpoint> {
        self.workspaces.get(name)
    }
    pub fn workspace_mut(&mut self, name: &str) -> Option<&mut WorkspaceEndpoint> {
        self.workspaces.get_mut(name)
    }
    pub fn names(&self) -> Vec<String> {
        self.workspaces.keys().cloned().collect()
    }

    pub fn create_or_find(
        &mut self,
        name: &str,
        factory: &mut impl EndpointFactory,
    ) -> Result<Descriptor, ManagerError> {
        super::validate_workspace_name(name)?;
        self.received_initial_request = true;
        if let Some(existing) = self.workspaces.get(name) {
            return Ok(existing.descriptor.clone());
        }
        if self.workspaces.len() >= MAX_WORKSPACES {
            return Err(ManagerError::WorkspaceLimit);
        }
        let key = self.paths.key(name)?;
        let mut allow = self.explicit_allow.clone();
        allow.insert(self.local_client_id.clone());
        let endpoint = factory.create(name, &key, &allow)?;
        self.insert_created(name, endpoint)
    }

    pub async fn create_or_find_async<F, Fut>(
        &mut self,
        name: &str,
        factory: F,
    ) -> Result<Descriptor, ManagerError>
    where
        F: FnOnce(PathBuf, BTreeSet<String>) -> Fut,
        Fut: Future<Output = Result<Box<dyn EndpointHandle>, ManagerError>>,
    {
        super::validate_workspace_name(name)?;
        self.received_initial_request = true;
        if let Some(existing) = self.workspaces.get(name) {
            return Ok(existing.descriptor.clone());
        }
        if self.workspaces.len() >= MAX_WORKSPACES {
            return Err(ManagerError::WorkspaceLimit);
        }
        let key = self.paths.key(name)?;
        let mut allow = self.explicit_allow.clone();
        allow.insert(self.local_client_id.clone());
        let endpoint = factory(key, allow).await?;
        self.insert_created(name, endpoint)
    }

    fn insert_created(
        &mut self,
        name: &str,
        endpoint: Box<dyn EndpointHandle>,
    ) -> Result<Descriptor, ManagerError> {
        if self
            .workspaces
            .values()
            .any(|workspace| workspace.endpoint.endpoint_id() == endpoint.endpoint_id())
        {
            return Err(ManagerError::DuplicateEndpoint);
        }
        let descriptor = Descriptor {
            name: name.to_owned(),
            pid: self.identity.pid,
            instance_nonce: self.identity.instance_nonce.clone(),
            endpoint_id: endpoint.endpoint_id().to_owned(),
            direct_addr: endpoint.direct_addr(),
        };
        write_descriptor(&self.paths, &descriptor)?;
        self.workspaces.insert(
            name.to_owned(),
            WorkspaceEndpoint {
                descriptor: descriptor.clone(),
                endpoint,
                allow: self.explicit_allow.clone(),
                local_client_id: self.local_client_id.clone(),
                viewers: 0,
            },
        );
        Ok(descriptor)
    }

    pub fn resolve(&self, name: Option<&str>) -> Result<Resolution, ManagerError> {
        match name {
            Some(name) => {
                super::validate_workspace_name(name)?;
                Ok(self.workspaces.get(name).map_or_else(
                    || Resolution::Create(name.to_owned()),
                    |workspace| Resolution::Attach(workspace.descriptor.clone()),
                ))
            }
            None => match self.workspaces.len() {
                0 => Ok(Resolution::Create("default".to_owned())),
                1 => self
                    .workspaces
                    .values()
                    .next()
                    .map(|workspace| Resolution::Attach(workspace.descriptor.clone()))
                    .ok_or(ManagerError::Invalid),
                _ => Ok(Resolution::Pick(self.names())),
            },
        }
    }

    pub fn kill(&mut self, name: &str) -> Result<DaemonAction, ManagerError> {
        let mut workspace = self.workspaces.remove(name).ok_or(ManagerError::NotFound)?;
        workspace.endpoint.close();
        let descriptor = self.paths.descriptor(name)?;
        remove_regular_owned_file(&descriptor, &self.paths.descriptors_dir)?;
        Ok(if self.workspaces.is_empty() {
            DaemonAction::ReplyThenExit
        } else {
            DaemonAction::Continue
        })
    }

    pub fn tick(&mut self, now_ms: u64) -> DaemonAction {
        for workspace in self.workspaces.values_mut() {
            workspace
                .endpoint
                .reap_terminal_sessions(now_ms, IDLE_WORKSPACE_TTL_MS);
        }
        if self.workspaces.is_empty()
            && !self.received_initial_request
            && now_ms.saturating_sub(self.started_ms) >= INITIAL_REQUEST_GRACE_MS
        {
            DaemonAction::ReplyThenExit
        } else {
            DaemonAction::Continue
        }
    }
}

impl Drop for Daemon {
    fn drop(&mut self) {
        for workspace in self.workspaces.values_mut() {
            workspace.endpoint.close();
        }
        for name in self.workspaces.keys() {
            if let Ok(path) = self.paths.descriptor(name) {
                let _ = remove_regular_owned_file(&path, &self.paths.descriptors_dir);
            }
        }
    }
}

#[derive(Debug)]
pub struct ManagerLock {
    listener: UnixListener,
    path: PathBuf,
    device: u64,
    inode: u64,
}
impl ManagerLock {
    pub fn bind(paths: &DaemonPaths) -> Result<Self, ManagerError> {
        paths.prepare()?;
        remove_stale_socket(&paths.manager_socket, &paths.runtime_dir)?;
        let listener = UnixListener::bind(&paths.manager_socket).map_err(ManagerError::Io)?;
        fs::set_permissions(&paths.manager_socket, fs::Permissions::from_mode(0o600))
            .map_err(ManagerError::Io)?;
        let metadata = fs::metadata(&paths.manager_socket).map_err(ManagerError::Io)?;
        Ok(Self {
            listener,
            path: paths.manager_socket.clone(),
            device: metadata.dev(),
            inode: metadata.ino(),
        })
    }
    pub fn listener(&self) -> &UnixListener {
        &self.listener
    }
}
impl Drop for ManagerLock {
    fn drop(&mut self) {
        if fs::symlink_metadata(&self.path).is_ok_and(|metadata| {
            metadata.file_type().is_socket()
                && metadata.dev() == self.device
                && metadata.ino() == self.inode
        }) {
            let _ = fs::remove_file(&self.path);
        }
    }
}

#[derive(Debug)]
pub enum ManagerError {
    Path(PathError),
    Descriptor(DescriptorError),
    Io(io::Error),
    Invalid,
    NotFound,
    Unauthorized,
    ConnectionLimit,
    WorkspaceLimit,
    DuplicateEndpoint,
}
impl fmt::Display for ManagerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:?}")
    }
}
impl std::error::Error for ManagerError {}
impl From<PathError> for ManagerError {
    fn from(value: PathError) -> Self {
        Self::Path(value)
    }
}
impl From<DescriptorError> for ManagerError {
    fn from(value: DescriptorError) -> Self {
        Self::Descriptor(value)
    }
}

fn random_nonce() -> Result<String, ManagerError> {
    let mut bytes = [0_u8; 16];
    fs::File::open("/dev/urandom")
        .and_then(|mut file| file.read_exact(&mut bytes))
        .map_err(ManagerError::Io)?;
    Ok(bytes.iter().map(|byte| format!("{byte:02x}")).collect())
}

fn remove_stale_socket(path: &Path, owner_dir: &Path) -> Result<(), ManagerError> {
    let metadata = match fs::symlink_metadata(path) {
        Ok(value) => value,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
        Err(error) => return Err(ManagerError::Io(error)),
    };
    if !metadata.file_type().is_socket() {
        return Err(ManagerError::Io(io::Error::new(
            io::ErrorKind::AlreadyExists,
            "manager path is not a socket",
        )));
    }
    // A concurrently elected listener can be visible just before connect becomes observable on
    // all supported Unix kernels. Retry briefly before classifying it as stale and unlinking it.
    for _ in 0..3 {
        if UnixStream::connect(path).is_ok() {
            return Err(ManagerError::Io(io::Error::new(
                io::ErrorKind::AddrInUse,
                "daemon already running",
            )));
        }
        std::thread::sleep(std::time::Duration::from_millis(5));
    }
    let owner = fs::metadata(owner_dir).map_err(ManagerError::Io)?;
    if metadata.uid() != owner.uid() {
        return Err(ManagerError::Io(io::Error::new(
            io::ErrorKind::PermissionDenied,
            "stale manager socket owner mismatch",
        )));
    }
    let current = match fs::symlink_metadata(path) {
        Ok(current) => current,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
        Err(error) => return Err(ManagerError::Io(error)),
    };
    if !current.file_type().is_socket()
        || current.dev() != metadata.dev()
        || current.ino() != metadata.ino()
    {
        return Err(ManagerError::Io(io::Error::new(
            io::ErrorKind::AddrInUse,
            "manager socket changed during stale recovery",
        )));
    }
    fs::remove_file(path).map_err(ManagerError::Io)
}

fn remove_regular_owned_file(path: &Path, owner_dir: &Path) -> Result<(), ManagerError> {
    match fs::symlink_metadata(path) {
        Ok(metadata)
            if metadata.file_type().is_file()
                && !metadata.file_type().is_symlink()
                && metadata.uid() == fs::metadata(owner_dir).map_err(ManagerError::Io)?.uid() =>
        {
            fs::remove_file(path).map_err(ManagerError::Io)
        }
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
        Ok(_) => Err(ManagerError::Io(io::Error::new(
            io::ErrorKind::PermissionDenied,
            "refusing unsafe descriptor cleanup",
        ))),
        Err(error) => Err(ManagerError::Io(error)),
    }
}