aix-rs 1.0.0

Manage AI gateway profiles and launch AI coding tools with the right credentials
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
use crate::error::AixError;
use directories::BaseDirs;
use serde::{Deserialize, Serialize};
use std::fs::{self, File, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use uuid::Uuid;

pub const RUN_SCHEMA_VERSION: u8 = 3;

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum RunStatus {
    Running,
    Succeeded,
    Failed,
    Interrupted,
    #[serde(rename = "timed_out")]
    TimedOut,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
// Retained only to read run history written before key-management support was removed.
pub enum LeaseCleanupStatus {
    Revoked,
    ExpiredOrUnverified,
    RevokeFailed,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
// Legacy history data, never used to create or revoke credentials.
pub struct RunLeaseRecord {
    pub key_alias: String,
    pub budget: f64,
    pub duration: String,
    pub expires_at: Option<String>,
    pub allowed_models: Vec<String>,
    pub spend: Option<f64>,
    pub cleanup_status: LeaseCleanupStatus,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunPolicyRecord {
    pub name: String,
    // Read old records without reintroducing monetary enforcement for new runs.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub effective_budget: Option<f64>,
    pub effective_duration: String,
    pub effective_allowed_models: Vec<String>,
    pub effective_tags: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RunUsageRecord {
    pub request_count: u64,
    pub successful_requests: u64,
    pub failed_requests: u64,
    pub input_tokens_total: Option<u64>,
    pub input_tokens_uncached: Option<u64>,
    pub cache_read_input_tokens: Option<u64>,
    pub cache_write_input_tokens: Option<u64>,
    pub output_tokens: Option<u64>,
    pub total_tokens: Option<u64>,
    pub models: Vec<String>,
    pub protocols: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunRecord {
    pub schema_version: u8,
    pub run_id: Uuid,
    pub name: Option<String>,
    pub workflow: Option<String>,
    pub task_id: Option<String>,
    pub tags: Vec<String>,
    pub profile: String,
    pub logical_tool_name: Option<String>,
    pub executable_name: String,
    pub started_at_unix_ms: u64,
    pub finished_at_unix_ms: Option<u64>,
    pub duration_ms: Option<u64>,
    pub process_exit_code: Option<i32>,
    pub status: RunStatus,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub policy: Option<RunPolicyRecord>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub lease: Option<RunLeaseRecord>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub usage: Option<RunUsageRecord>,
}

impl RunRecord {
    pub fn now_unix_ms() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis()
            .try_into()
            .unwrap_or(u64::MAX)
    }
}

pub struct RunStore {
    runs_dir: PathBuf,
}

pub struct RunHandle {
    directory: PathBuf,
    revision: u64,
}

impl RunStore {
    pub fn from_environment() -> Result<Self, AixError> {
        let state_dir = match std::env::var_os("AIX_STATE_DIR") {
            Some(path) if !path.is_empty() => PathBuf::from(path),
            Some(_) => {
                return Err(AixError::RunHistoryIo {
                    path: PathBuf::from("AIX_STATE_DIR"),
                    source: std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "AIX_STATE_DIR must not be empty",
                    ),
                })
            }
            None => default_state_dir().ok_or_else(|| AixError::RunHistoryIo {
                path: PathBuf::from("aix state directory"),
                source: std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    "no platform state directory is available; set AIX_STATE_DIR",
                ),
            })?,
        };

        Ok(Self {
            runs_dir: state_dir.join("runs"),
        })
    }

    pub fn begin(&self, record: &RunRecord) -> Result<RunHandle, AixError> {
        let directory = self.runs_dir.join(record.run_id.to_string());
        create_private_directory(&self.runs_dir)?;
        fs::create_dir(&directory).map_err(|source| AixError::RunHistoryIo {
            path: directory.clone(),
            source,
        })?;
        set_private_directory_permissions(&directory).map_err(|source| AixError::RunHistoryIo {
            path: directory.clone(),
            source,
        })?;

        let mut handle = RunHandle {
            directory,
            revision: 0,
        };
        self.write(&mut handle, record)?;
        Ok(handle)
    }

    pub fn write(&self, handle: &mut RunHandle, record: &RunRecord) -> Result<(), AixError> {
        let revision = handle.revision.saturating_add(1);
        let destination = handle.directory.join(format!("{revision:020}.json"));
        let bytes = serde_json::to_vec_pretty(record)?;
        write_new_snapshot(&destination, &bytes).map_err(|source| AixError::RunHistoryIo {
            path: destination,
            source,
        })?;
        handle.revision = revision;
        Ok(())
    }

    pub fn list(&self, limit: usize) -> Result<Vec<RunRecord>, AixError> {
        if !self.runs_dir.exists() {
            return Ok(Vec::new());
        }

        let entries = fs::read_dir(&self.runs_dir).map_err(|source| AixError::RunHistoryIo {
            path: self.runs_dir.clone(),
            source,
        })?;
        let mut records = Vec::new();
        for entry in entries {
            let entry = entry.map_err(|source| AixError::RunHistoryIo {
                path: self.runs_dir.clone(),
                source,
            })?;
            if entry
                .file_type()
                .map_err(|source| AixError::RunHistoryIo {
                    path: entry.path(),
                    source,
                })?
                .is_dir()
            {
                if let Some(record) = read_latest_record(&entry.path())? {
                    records.push(record);
                }
            }
        }

        records.sort_by(|left, right| {
            right
                .started_at_unix_ms
                .cmp(&left.started_at_unix_ms)
                .then_with(|| right.run_id.cmp(&left.run_id))
        });
        records.truncate(limit);
        Ok(records)
    }

    pub fn get(&self, run_id: &str) -> Result<RunRecord, AixError> {
        let parsed = Uuid::parse_str(run_id).map_err(|_| AixError::RunNotFound {
            run_id: run_id.to_string(),
        })?;
        let directory = self.runs_dir.join(parsed.to_string());
        read_latest_record(&directory)?.ok_or_else(|| AixError::RunNotFound {
            run_id: run_id.to_string(),
        })
    }
}

fn default_state_dir() -> Option<PathBuf> {
    let base_dirs = BaseDirs::new()?;
    Some(
        base_dirs
            .state_dir()
            .unwrap_or_else(|| base_dirs.data_local_dir())
            .join("aix"),
    )
}

fn read_latest_record(directory: &Path) -> Result<Option<RunRecord>, AixError> {
    if !directory.exists() {
        return Ok(None);
    }

    let entries = fs::read_dir(directory).map_err(|source| AixError::RunHistoryIo {
        path: directory.to_path_buf(),
        source,
    })?;
    let mut latest: Option<(u64, PathBuf)> = None;
    for entry in entries {
        let entry = entry.map_err(|source| AixError::RunHistoryIo {
            path: directory.to_path_buf(),
            source,
        })?;
        if !entry
            .file_type()
            .map_err(|source| AixError::RunHistoryIo {
                path: entry.path(),
                source,
            })?
            .is_file()
        {
            continue;
        }
        let path = entry.path();
        if path.extension().and_then(|ext| ext.to_str()) != Some("json") {
            continue;
        }
        let Some(revision) = path
            .file_stem()
            .and_then(|stem| stem.to_str())
            .and_then(|stem| stem.parse::<u64>().ok())
        else {
            continue;
        };
        if latest
            .as_ref()
            .is_none_or(|(current, _)| revision > *current)
        {
            latest = Some((revision, path));
        }
    }

    let Some((_, path)) = latest else {
        return Ok(None);
    };
    let content = fs::read(&path).map_err(|source| AixError::RunHistoryIo {
        path: path.clone(),
        source,
    })?;
    let record = serde_json::from_slice(&content).map_err(|source| AixError::RunHistoryIo {
        path,
        source: std::io::Error::new(std::io::ErrorKind::InvalidData, source),
    })?;
    Ok(Some(record))
}

fn write_new_snapshot(destination: &Path, bytes: &[u8]) -> std::io::Result<()> {
    if destination.exists() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::AlreadyExists,
            "run record revision already exists",
        ));
    }

    let directory = destination
        .parent()
        .expect("snapshot has a parent directory");
    let temporary = directory.join(format!(".{}.tmp", Uuid::new_v4()));
    let mut file = new_private_file(&temporary)?;
    let result = (|| {
        file.write_all(bytes)?;
        file.flush()?;
        file.sync_all()?;
        drop(file);
        fs::rename(&temporary, destination)
    })();
    if result.is_err() {
        let _ = fs::remove_file(&temporary);
    }
    result
}

fn create_private_directory(path: &Path) -> Result<(), AixError> {
    fs::create_dir_all(path).map_err(|source| AixError::RunHistoryIo {
        path: path.to_path_buf(),
        source,
    })?;
    set_private_directory_permissions(path).map_err(|source| AixError::RunHistoryIo {
        path: path.to_path_buf(),
        source,
    })
}

#[cfg(unix)]
fn set_private_directory_permissions(path: &Path) -> std::io::Result<()> {
    use std::os::unix::fs::PermissionsExt;
    fs::set_permissions(path, fs::Permissions::from_mode(0o700))
}

#[cfg(not(unix))]
fn set_private_directory_permissions(_path: &Path) -> std::io::Result<()> {
    Ok(())
}

fn new_private_file(path: &Path) -> std::io::Result<File> {
    let mut options = OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    options.open(path)
}

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

    fn record(run_id: &Uuid, started_at_unix_ms: u64) -> RunRecord {
        RunRecord {
            schema_version: RUN_SCHEMA_VERSION,
            run_id: *run_id,
            name: None,
            workflow: None,
            task_id: None,
            tags: Vec::new(),
            profile: "work".to_string(),
            logical_tool_name: None,
            executable_name: "sh".to_string(),
            started_at_unix_ms,
            finished_at_unix_ms: None,
            duration_ms: None,
            process_exit_code: None,
            status: RunStatus::Running,
            policy: None,
            lease: None,
            usage: None,
        }
    }

    #[test]
    fn list_sorts_newest_first_and_applies_limit() {
        let temp = assert_fs::TempDir::new().unwrap();
        let store = RunStore {
            runs_dir: temp.path().join("runs"),
        };
        let ids = [Uuid::new_v4(), Uuid::new_v4()];
        for (id, started) in ids.iter().zip([10, 20]) {
            store.begin(&record(id, started)).unwrap();
        }

        let listed = store.list(1).unwrap();
        assert_eq!(listed.len(), 1);
        assert_eq!(listed[0].started_at_unix_ms, 20);
    }

    #[test]
    fn failed_snapshot_does_not_hide_the_previous_valid_record() {
        let temp = assert_fs::TempDir::new().unwrap();
        let store = RunStore {
            runs_dir: temp.path().join("runs"),
        };
        let id = Uuid::new_v4();
        let mut handle = store.begin(&record(&id, 10)).unwrap();
        let blocked_revision = handle.directory.join("00000000000000000002.json");
        fs::create_dir(&blocked_revision).unwrap();

        let mut updated = record(&id, 10);
        updated.status = RunStatus::Succeeded;
        assert!(store.write(&mut handle, &updated).is_err());
        assert_eq!(
            store.get(&id.to_string()).unwrap().status,
            RunStatus::Running
        );
    }
}