rustio-admin-cli 0.27.1

Command-line tools for rustio-admin: project scaffolding, migrations, user management.
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
//! The Builder's append-only event log (Doctrine B2, B3).
//!
//! `DESIGN_BUILDER.md` §4.2 declares the contract this module
//! implements:
//!
//! - **Doctrine B3.** [`append`] is the sole function that appends
//!   to `.rustio/history.jsonl`. §10.1 carries the grep proof: no
//!   other file in `crates/rustio-admin-cli/src/` may construct a
//!   write handle to `history.jsonl`.
//! - **Doctrine B2.** The log is append-only. Reversal is always a
//!   new compensating event; never an in-place edit or truncate.
//! - **§4.2.1.** Line shape: one JSON object per line, with fields
//!   in fixed order -- `id`, `ts`, `op`, `actor`, `args`, optional
//!   `prev_hash`, `schema_v`.
//! - **§4.2.4.** The closed `op` enum is the source of truth for
//!   the serialized values. The drift test below asserts the
//!   `HistoryOp` ↔ on-disk string lockstep.
//!
//! The append boundary lives here. Redaction of secret-bearing
//! payloads happens in [`crate::builder::redact`] before reaching
//! this module -- `append` itself accepts an already-clean payload.
//!
//! ## Atomicity model
//!
//! POSIX guarantees that a write opened with `O_APPEND` and sized
//! at or below `PIPE_BUF` is atomic -- concurrent writers do not
//! interleave bytes within a single write call. `OpenOptions::new()
//! .append(true)` maps to `O_APPEND` on Unix. `PIPE_BUF` is at
//! least 512 bytes (POSIX minimum) and is 4096 on every Linux /
//! macOS we target. The serialized event lines this module emits
//! are well below 1 KB in MVP usage (no large `args` payloads;
//! actor strings bounded; redaction shortens any secret-bearing
//! field to a 35-character fingerprint).
//!
//! On Windows the equivalent guarantee is not load-bearing in the
//! current MVP; the doctrine review tracks this as a deferred
//! concern. Single-process usage is unaffected.
//!
//! A defensive size assertion in [`append`] rejects any line over
//! 4 KB so a future field that quietly grew the payload could not
//! cross the atomicity bound without surfacing.

use std::fs::OpenOptions;
use std::io::Write;
use std::path::Path;

use chrono::{SecondsFormat, Utc};
use serde::Serialize;
use serde_json::Value as JsonValue;

use crate::builder::canonical::canonicalize;
use crate::builder::ulid_gen::new_ulid;

/// The closed enumeration of operations that may appear in
/// `history.jsonl`. New variants here are doctrine amendments;
/// the `op_enum_matches_serialized` drift test below pins each
/// variant to its serialized string.
///
/// `DESIGN_BUILDER.md` §4.2.4: *"The closed enumeration of `op`
/// values lives in a single Rust `enum HistoryOp` whose `as_str()`
/// is the source of truth for the serialized values."*
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum HistoryOp {
    /// Emitted once by `rustio-admin new` to mark project birth.
    ProjectInit,
    /// `rustio-admin add model <Name>`.
    AddModel,
    /// `rustio-admin add field <Model> <name> <type>`.
    AddField,
    /// Emitted by every successful `rustio-admin commit` invocation.
    Commit,
    /// Emitted as a child of a `Commit` event for each generated
    /// file written.
    FileCreated,
    /// Reserved for future use; currently never emitted by the MVP.
    /// Declared up front so the drift test exercises every variant
    /// the doctrine references.
    Undo,
}

impl HistoryOp {
    /// Stable on-disk string representation. Changes here are
    /// breaking and require a `schema_version` bump (§4.6).
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            HistoryOp::ProjectInit => "project_init",
            HistoryOp::AddModel => "add_model",
            HistoryOp::AddField => "add_field",
            HistoryOp::Commit => "commit",
            HistoryOp::FileCreated => "file_created",
            HistoryOp::Undo => "undo",
        }
    }
}

/// The wire shape of a single line in `history.jsonl`.
///
/// Field order is fixed by §4.2.1. The `Serialize` impl on this
/// struct preserves that order; serde renders struct fields in
/// declaration order.
#[derive(Debug, Serialize)]
struct EventLine<'a> {
    id: &'a str,
    ts: &'a str,
    op: &'a str,
    actor: &'a str,
    args: &'a JsonValue,
    #[serde(skip_serializing_if = "Option::is_none")]
    prev_hash: Option<&'a str>,
    schema_v: u32,
}

/// The current `history.jsonl` line schema version. Bumps require
/// a `schema_upgraded` event in the log itself and a transformer
/// under `cli::schema_migration`.
const HISTORY_SCHEMA_V: u32 = 1;

/// Append a single event to `.rustio/history.jsonl`.
///
/// **Doctrine B3 -- sole writer.** This is the only function in the
/// CLI crate authorised to open `history.jsonl` for writing. The
/// grep proof at §10.1 enforces that no other file under
/// `crates/rustio-admin-cli/src/` calls `OpenOptions::append` /
/// `File::create` against a path ending in `history.jsonl`.
///
/// Caller responsibilities:
///
/// - `args` must already have been passed through
///   [`crate::builder::redact`] if it can carry secret content.
///   This function does not redact; it serializes what it is given.
/// - `actor` should be `git config user.email`, falling back to
///   `$RUSTIO_AGENT_ID`, falling back to `unknown`. Callers resolve
///   this; the append boundary does not.
///
/// The function is **append-only**. It opens the file with
/// `O_APPEND` semantics and writes one canonical line. No
/// truncation, no in-place edit.
pub(crate) fn append(
    history_path: &Path,
    op: HistoryOp,
    actor: &str,
    args: JsonValue,
) -> std::io::Result<String> {
    let id = new_ulid();
    let ts = Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true);
    let line = EventLine {
        id: &id,
        ts: &ts,
        op: op.as_str(),
        actor,
        args: &args,
        prev_hash: None, // chain mode is opt-in; off by default per §4.2.5
        schema_v: HISTORY_SCHEMA_V,
    };
    let mut json = serde_json::to_string(&line).map_err(std::io::Error::other)?;
    json.push('\n');
    let canonical = canonicalize(&json);

    // Atomicity bound: POSIX `O_APPEND` writes are atomic up to
    // `PIPE_BUF`, which is ≥ 512 bytes. We allow 4 KB to give
    // headroom on Linux/macOS (`PIPE_BUF` = 4096) while still
    // surfacing accidental payload growth. Lines beyond this risk
    // interleaving under concurrent writers.
    const MAX_ATOMIC_LINE_BYTES: usize = 4096;
    if canonical.len() > MAX_ATOMIC_LINE_BYTES {
        return Err(std::io::Error::other(format!(
            "history.jsonl event line is {} bytes -- exceeds the {}-byte atomic-write \
             bound from `DESIGN_BUILDER.md` §4.2.1. Refusing to write a payload that \
             could interleave under concurrent CLI invocations.",
            canonical.len(),
            MAX_ATOMIC_LINE_BYTES
        )));
    }

    let mut file = OpenOptions::new()
        .create(true)
        .append(true)
        .open(history_path)?;
    file.write_all(canonical.as_bytes())?;
    file.flush()?;
    Ok(id)
}

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

    /// Drift test (§10.8). The closed enum and its serialized
    /// strings must stay in lockstep. A PR that adds a variant
    /// without updating the test list -- or vice versa -- fails here.
    #[test]
    fn op_enum_matches_serialized() {
        let pairs = [
            (HistoryOp::ProjectInit, "project_init"),
            (HistoryOp::AddModel, "add_model"),
            (HistoryOp::AddField, "add_field"),
            (HistoryOp::Commit, "commit"),
            (HistoryOp::FileCreated, "file_created"),
            (HistoryOp::Undo, "undo"),
        ];
        for (variant, expected) in pairs {
            assert_eq!(
                variant.as_str(),
                expected,
                "HistoryOp::{variant:?} must serialize as {expected:?}"
            );
        }
        // Reverse direction: every serialized string must be
        // snake_case ASCII, symmetric to AuditEvent's discipline.
        for (variant, expected) in pairs {
            for c in expected.chars() {
                assert!(
                    c.is_ascii_lowercase() || c == '_' || c.is_ascii_digit(),
                    "HistoryOp::{variant:?} serialized as {expected:?} \
                     contains non-snake_case char {c:?}"
                );
            }
        }
    }

    #[test]
    fn append_writes_one_line_per_call() {
        let dir = tempdir();
        let path = dir.join("history.jsonl");
        append(
            &path,
            HistoryOp::ProjectInit,
            "alice@example.com",
            json!({}),
        )
        .unwrap();
        append(
            &path,
            HistoryOp::AddModel,
            "alice@example.com",
            json!({"name": "Patient"}),
        )
        .unwrap();
        let content = std::fs::read_to_string(&path).unwrap();
        let lines: Vec<&str> = content.split('\n').filter(|l| !l.is_empty()).collect();
        assert_eq!(lines.len(), 2, "expected 2 lines, got: {content:?}");
    }

    #[test]
    fn append_emits_required_fields_in_order() {
        let dir = tempdir();
        let path = dir.join("history.jsonl");
        let id = append(
            &path,
            HistoryOp::AddModel,
            "alice@example.com",
            json!({"name": "Patient"}),
        )
        .unwrap();
        let content = std::fs::read_to_string(&path).unwrap();
        let line = content.lines().next().unwrap();

        // Parse as JSON and check the field set.
        let parsed: serde_json::Value = serde_json::from_str(line).unwrap();
        assert_eq!(parsed["id"], id);
        assert_eq!(parsed["op"], "add_model");
        assert_eq!(parsed["actor"], "alice@example.com");
        assert_eq!(parsed["args"]["name"], "Patient");
        assert_eq!(parsed["schema_v"], 1);
        assert!(parsed["ts"].is_string());

        // Per §4.2.1, field order is fixed in the on-disk byte
        // stream. `serde_json::Value` re-sorts keys alphabetically
        // when iterated, so the order check has to read the raw
        // line. Find each key's first occurrence and assert the
        // sequence.
        let raw = line; // the single line we read from disk
        let expected_order = [
            "\"id\":",
            "\"ts\":",
            "\"op\":",
            "\"actor\":",
            "\"args\":",
            "\"schema_v\":",
        ];
        let mut prev_pos = 0usize;
        for marker in expected_order {
            let pos = raw[prev_pos..]
                .find(marker)
                .unwrap_or_else(|| panic!("missing or out-of-order {marker} in {raw}"));
            prev_pos += pos + marker.len();
        }
    }

    #[test]
    fn append_is_append_only() {
        let dir = tempdir();
        let path = dir.join("history.jsonl");
        append(&path, HistoryOp::ProjectInit, "a", json!({})).unwrap();
        let after_first = std::fs::read_to_string(&path).unwrap();
        append(&path, HistoryOp::AddModel, "a", json!({"name": "X"})).unwrap();
        let after_second = std::fs::read_to_string(&path).unwrap();
        // Doctrine B2: the second append must extend, never replace.
        assert!(
            after_second.starts_with(&after_first),
            "second append truncated prior content:\n\
             after_first  = {after_first:?}\n\
             after_second = {after_second:?}"
        );
        assert!(after_second.len() > after_first.len());
    }

    #[test]
    fn append_uses_second_precision_timestamps() {
        let dir = tempdir();
        let path = dir.join("history.jsonl");
        append(&path, HistoryOp::ProjectInit, "a", json!({})).unwrap();
        let line = std::fs::read_to_string(&path).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(line.trim()).unwrap();
        let ts = parsed["ts"].as_str().unwrap();
        // §4.4 #5: second-precision ISO-8601 with UTC Z suffix. No
        // sub-second component.
        assert!(ts.ends_with('Z'), "timestamp must end with Z: {ts}");
        assert!(
            !ts.contains('.'),
            "timestamp must not have sub-seconds: {ts}"
        );
    }

    /// Sole-writer self-check (§10.1). Reads this crate's own
    /// source tree and asserts no file outside `history.rs` opens
    /// a write handle to `history.jsonl`. Belt-and-suspenders
    /// alongside the CI grep proof.
    #[test]
    fn no_other_writer_to_history_jsonl_in_cli_crate() {
        let cli_src = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
        let mut offenders = Vec::new();
        scan_for_history_writes(&cli_src, &mut offenders);
        assert!(
            offenders.is_empty(),
            "Doctrine B3 violation: files other than \
             builder/history.rs construct write handles to \
             history.jsonl: {offenders:?}",
        );
    }

    fn scan_for_history_writes(dir: &std::path::Path, offenders: &mut Vec<std::path::PathBuf>) {
        let Ok(entries) = std::fs::read_dir(dir) else {
            return;
        };
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                scan_for_history_writes(&path, offenders);
                continue;
            }
            // The file that legitimately defines `append` is exempt.
            if path.ends_with("builder/history.rs") {
                continue;
            }
            // Only Rust sources count.
            if path.extension().and_then(|e| e.to_str()) != Some("rs") {
                continue;
            }
            let Ok(src) = std::fs::read_to_string(&path) else {
                continue;
            };
            // Strip line comments before scanning so doc comments
            // referencing `history.jsonl` do not register as
            // violations. This is not a complete comment parser, but
            // it is enough for the doctrinal smell test.
            for line in src.lines() {
                let code = match line.find("//") {
                    Some(idx) => &line[..idx],
                    None => line,
                };
                let opens_write = (code.contains("OpenOptions")
                    && (code.contains("append(true)") || code.contains(".write(true)")))
                    || code.contains("File::create");
                if opens_write && code.contains("history.jsonl") {
                    offenders.push(path.clone());
                    break;
                }
            }
        }
    }

    #[test]
    fn append_refuses_oversized_lines() {
        let dir = tempdir();
        let path = dir.join("history.jsonl");
        // Inflate the args payload past the 4 KB atomic bound.
        let big = "x".repeat(5000);
        let err = append(
            &path,
            HistoryOp::AddModel,
            "a",
            json!({ "name": "X", "table": big }),
        )
        .expect_err("must refuse oversized line");
        assert!(err.to_string().contains("atomic-write"), "{err}");
        // And no partial line written.
        assert!(
            !path.exists() || std::fs::read_to_string(&path).unwrap().is_empty(),
            "oversized refusal must not leave a partial line on disk"
        );
    }

    fn tempdir() -> std::path::PathBuf {
        let base = std::env::temp_dir().join(format!(
            "rustio-history-test-{}-{}",
            std::process::id(),
            new_ulid()
        ));
        std::fs::create_dir_all(&base).unwrap();
        base
    }
}