artifactize 0.5.2

Declare Artifacts and the evals that review them (tests, LLM reviews, human sign-offs), and reuse every verdict while its fingerprints are unchanged.
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
406
407
408
409
410
411
412
413
use std::{fs, path::Path, process::Command};

use rusqlite::params;
use serde_json::{Value, json};
use support::os::bin;

mod support;

/// A link to a directory: a symlink on Unix, and on Windows a junction, which needs no
/// privilege and which artifactize refuses just the same.
fn link_dir(target: &Path, link: &Path) {
    #[cfg(unix)]
    support::os::symlink_dir(target, link).unwrap();
    #[cfg(windows)]
    support::os::junction(target, link);
}

fn remove_link(link: &Path) {
    #[cfg(unix)]
    fs::remove_file(link).unwrap();
    // Windows removes a directory link as a directory.
    #[cfg(windows)]
    fs::remove_dir(link).unwrap();
}

fn command(state: &Path) -> Command {
    let mut command = Command::new(env!("CARGO_BIN_EXE_artifactize"));
    command.arg("--state-dir").arg(state).arg("--json");
    command
}

fn result(command: &mut Command, code: i32) -> Value {
    let output = command.output().unwrap();
    assert_eq!(
        output.status.code(),
        Some(code),
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    serde_json::from_slice(&output.stdout).unwrap()
}

fn check<'a>(report: &'a Value, name: &str) -> &'a Value {
    report["checks"]
        .as_array()
        .unwrap()
        .iter()
        .find(|check| check["name"] == name)
        .unwrap()
}

fn verify(state: &Path, repo: &Path, profile: Value, code: i32) -> Value {
    fs::create_dir_all(repo).unwrap();
    fs::write(repo.join("artifactize.json"), json!({
        "name":"a", "evals":[{"id":"review", "title":"Review", "payload":{"instruction":"Review"}, "profile":profile}]
    }).to_string()).unwrap();
    result(
        command(state)
            .arg("--repo")
            .arg(repo)
            .args(["verify", "--all"]),
        code,
    )
}

fn runtime_run(state: &Path, repo: &Path) -> Value {
    verify(
        state,
        repo,
        json!({"kind":"runtime", "command":bin("/bin/true"), "args":[]}),
        0,
    )
}

fn doctor(state: &Path, path: &Path) -> Command {
    let mut command = command(state);
    command
        .arg("doctor")
        .env("PATH", path)
        .env_remove("OPENAI_API_KEY")
        .env_remove("ANTHROPIC_API_KEY")
        .env_remove("ARTIFACTIZE_CODEX_AUTH_FILE")
        .env("HTTPS_PROXY", "http://127.0.0.1:1")
        .env("HTTP_PROXY", "http://127.0.0.1:1");
    command
}

#[test]
fn doctor_is_local_and_preserves_credentials_and_all_database_rows() {
    let root = tempfile::tempdir().unwrap();
    let state = root.path().join("state");
    let repo = root.path().join("repo");
    let bin = root.path().join("bin");
    fs::create_dir(&bin).unwrap();
    let absent = result(&mut doctor(&state, &bin), 0);
    for name in ["openai", "anthropic"] {
        assert_eq!(check(&absent, name)["status"], "WARN");
        assert_eq!(check(&absent, name)["details"]["present"], false);
    }
    assert_eq!(check(&absent, "codex")["status"], "WARN");
    assert_eq!(check(&absent, "codex")["details"]["source"], "none");
    // 0.5.0 removed the chatgpt and claude backends and their checks.
    let names: Vec<_> = absent["checks"]
        .as_array()
        .unwrap()
        .iter()
        .map(|check| check["name"].as_str().unwrap())
        .collect();
    assert_eq!(
        names,
        [
            "state",
            "schema",
            "limits",
            "openai",
            "anthropic",
            "codex",
            "remote"
        ]
    );
    assert_eq!(check(&absent, "limits")["details"], json!({"backends":{}}));
    assert_eq!(check(&absent, "state")["details"]["writable"], true);
    assert_eq!(check(&absent, "schema")["details"], json!({"schema":null}));
    assert!(!state.exists());
    assert_eq!(fs::read_dir(root.path()).unwrap().count(), 1);

    runtime_run(&state, &repo);
    let before = fs::read(state.join("state.sqlite")).unwrap();
    // Leftover 0.4 ChatGPT credentials are neither read nor removed.
    let auth = state.join("auth");
    support::os::create_private_dir_all(&auth);
    let leftover = json!({"access_token":"private-access", "refresh_token":"private-refresh"});
    support::os::write_private_file(&auth.join("chatgpt.json"), leftover.to_string());
    let present = result(
        doctor(&state, &bin)
            .arg("--repo")
            .arg(&repo)
            .env("OPENAI_API_KEY", "secret-openai")
            .env("ANTHROPIC_API_KEY", "secret-anthropic"),
        0,
    );
    assert_eq!(present["ok"], true);
    assert_eq!(
        check(&present, "schema")["details"],
        json!({"schema":artifactize::store::STATE_SCHEMA_VERSION})
    );
    for name in ["openai", "anthropic", "config", "schema"] {
        assert_eq!(check(&present, name)["status"], "PASS");
    }
    let output = present.to_string();
    for secret in [
        "secret-openai",
        "secret-anthropic",
        "private-access",
        "private-refresh",
    ] {
        assert!(!output.contains(secret));
    }
    assert_eq!(
        fs::read_to_string(auth.join("chatgpt.json")).unwrap(),
        leftover.to_string()
    );
    assert_eq!(fs::read_dir(&auth).unwrap().count(), 1);
    assert_eq!(fs::read(state.join("state.sqlite")).unwrap(), before);

    // A database written by a newer artifactize is a hard error and stays as it is.
    rusqlite::Connection::open(state.join("state.sqlite"))
        .unwrap()
        .pragma_update(None, "user_version", 99)
        .unwrap();
    let before = fs::read(state.join("state.sqlite")).unwrap();
    let newer = result(&mut doctor(&state, &bin), 1);
    assert_eq!(check(&newer, "schema")["status"], "FAIL");
    assert_eq!(check(&newer, "schema")["details"], json!({"schema":99}));
    assert_eq!(fs::read(state.join("state.sqlite")).unwrap(), before);

    // An older database is only reported; the next command that opens it upgrades it.
    let older = root.path().join("older");
    fs::create_dir(&older).unwrap();
    rusqlite::Connection::open(older.join("state.sqlite"))
        .unwrap()
        .execute_batch(&support::recorded_state(
            include_str!("fixtures/state-v2.sql"),
            root.path(),
        ))
        .unwrap();
    let before = fs::read(older.join("state.sqlite")).unwrap();
    let report = result(&mut doctor(&older, &bin), 0);
    assert_eq!(check(&report, "schema")["status"], "PASS");
    assert_eq!(
        check(&report, "schema")["details"],
        json!({"schema":2,"upgradeTo":artifactize::store::STATE_SCHEMA_VERSION})
    );
    assert!(
        check(&report, "schema")["message"]
            .as_str()
            .unwrap()
            .contains("the next artifactize command upgrades it")
    );
    assert_eq!(fs::read(older.join("state.sqlite")).unwrap(), before);
    assert!(!older.join("state.sqlite-wal").exists());
}

#[test]
fn doctor_reports_hard_local_errors_and_models_cli_needs_no_run() {
    let root = tempfile::tempdir().unwrap();
    let state = root.path().join("state");
    let repo = root.path().join("repo");
    fs::create_dir(&repo).unwrap();
    fs::write(repo.join("artifactize.json"), "invalid").unwrap();
    let report = result(doctor(&state, root.path()).arg("--repo").arg(&repo), 1);
    assert_eq!(check(&report, "config")["status"], "FAIL");
    fs::write(&state, "not a directory").unwrap();
    let report = result(&mut doctor(&state, root.path()), 1);
    assert_eq!(check(&report, "state")["status"], "FAIL");
    fs::remove_file(&state).unwrap();
    let report = result(
        doctor(&repo.join("state"), root.path())
            .arg("--repo")
            .arg(&repo),
        1,
    );
    assert_eq!(check(&report, "state")["status"], "FAIL");
    assert!(!repo.join("state").exists());
    for (backend, variable) in [
        ("openai", "OPENAI_API_KEY"),
        ("anthropic", "ANTHROPIC_API_KEY"),
    ] {
        let failure = result(
            command(&state)
                .args(["models", backend])
                .env_remove(variable),
            2,
        );
        assert!(failure["error"].as_str().unwrap().contains(variable));
    }
    for removed in ["chatgpt", "claude"] {
        result(command(&state).args(["models", removed]), 2);
    }
    assert!(!state.exists());
}

#[test]
fn prune_removes_only_finished_output_and_dry_run_preserves_everything() {
    let root = tempfile::tempdir().unwrap();
    let state = root.path().join("state");
    let repo = root.path().join("repo");
    let finished = runtime_run(&state, &repo);
    let waiting = verify(&state, &repo, json!({"kind":"human"}), 4);
    let active = runtime_run(&state, &repo);
    let owned = runtime_run(&state, &repo);
    let db = rusqlite::Connection::open(state.join("state.sqlite")).unwrap();
    let id = |run: &Value| run["id"].as_str().unwrap().to_owned();
    db.execute("UPDATE runs SET status='RUNNING' WHERE id=?", [id(&active)])
        .unwrap();
    let start = support::os::start_time(std::process::id()) as i64;
    db.execute("UPDATE executions SET owner_pid=?,owner_start_time=? WHERE json_extract(data,'$.provenance.runId')=?", params![std::process::id(), start, id(&owned)]).unwrap();
    drop(db);
    let run = state.join("runs").join(id(&finished));
    for path in [
        // Claude CLI invocation output from 0.4 Runs is still pruned.
        "claude-execution/mcp-config.json",
        "tool-leftover/runtime-leftover/output/content",
        "tool-output-test/content",
        "unknown/keep",
        "runtime-extra/audit.json",
    ] {
        let path = run.join(path);
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(path, "keep audit, prune scratch").unwrap();
    }
    let waiting_output = state.join("runs").join(id(&waiting)).join("output");
    fs::create_dir_all(&waiting_output).unwrap();
    let before = fs::read(state.join("state.sqlite")).unwrap();
    let repo_before = fs::read(repo.join("artifactize.json")).unwrap();
    let dry = result(command(&state).args(["prune", "--dry-run"]), 0);
    assert_eq!(dry["removed"], json!([]));
    assert_eq!(dry["wouldRemove"].as_array().unwrap().len(), 7);
    assert!(
        dry["wouldRemove"]
            .as_array()
            .unwrap()
            .iter()
            .all(|path| Path::new(path.as_str().unwrap()).is_dir())
    );
    for run in [&waiting, &active, &owned] {
        assert!(
            dry["skippedRuns"]
                .as_array()
                .unwrap()
                .contains(&json!(id(run)))
        );
    }
    let young = result(command(&state).args(["prune", "--older-than", "7d"]), 0);
    assert_eq!(young["removed"], json!([]));
    assert!(
        young["skippedRuns"]
            .as_array()
            .unwrap()
            .contains(&json!(id(&finished)))
    );
    let removed = result(command(&state).args(["prune", "--older-than", "0s"]), 0);
    assert_eq!(removed["removed"], dry["wouldRemove"]);
    for path in removed["removed"].as_array().unwrap() {
        assert!(!Path::new(path.as_str().unwrap()).exists());
    }
    assert!(waiting_output.is_dir());
    assert!(
        Path::new(active["requests"][0]["runDir"].as_str().unwrap())
            .join("output")
            .is_dir()
    );
    assert!(
        Path::new(owned["requests"][0]["runDir"].as_str().unwrap())
            .join("output")
            .is_dir()
    );
    assert!(run.join("unknown/keep").exists());
    assert!(run.join("runtime-extra/audit.json").exists());
    assert_eq!(fs::read(state.join("state.sqlite")).unwrap(), before);
    assert_eq!(
        fs::read(repo.join("artifactize.json")).unwrap(),
        repo_before
    );
    let shown = result(command(&state).args(["run", "show", &id(&finished)]), 0);
    assert_eq!(shown, finished);
}

#[test]
fn prune_refuses_symlinks_and_repository_targets_before_deleting() {
    let root = tempfile::tempdir().unwrap();
    let state = root.path().join("state");
    let repo = root.path().join("repo");
    let finished = runtime_run(&state, &repo);
    let run = state.join("runs").join(finished["id"].as_str().unwrap());
    let output = Path::new(finished["requests"][0]["runDir"].as_str().unwrap()).join("output");
    for link in [run.join("tool-output-link"), output.join("nested-link")] {
        link_dir(&repo, &link);
        let error = result(command(&state).args(["prune"]), 2);
        assert!(error["error"].as_str().unwrap().contains("symlink"));
        assert!(output.is_dir());
        assert!(repo.join("artifactize.json").exists());
        remove_link(&link);
    }
    let alias = root.path().join("alias");
    link_dir(&state, &alias);
    assert!(
        result(command(&alias).arg("prune"), 2)["error"]
            .as_str()
            .unwrap()
            .contains("symlink")
    );
    let moved = state.join("saved-runs");
    fs::rename(state.join("runs"), &moved).unwrap();
    link_dir(&moved, &state.join("runs"));
    assert!(
        result(command(&state).arg("prune"), 2)["error"]
            .as_str()
            .unwrap()
            .contains("symlink")
    );
    remove_link(&state.join("runs"));
    fs::rename(moved, state.join("runs")).unwrap();
    fs::write(run.join(".git"), "gitdir: elsewhere").unwrap();
    assert!(
        result(command(&state).arg("prune"), 2)["error"]
            .as_str()
            .unwrap()
            .contains("repository")
    );
    assert!(output.is_dir());
    fs::remove_file(run.join(".git")).unwrap();
    fs::write(output.join("artifactize.json"), "{}").unwrap();
    assert!(
        result(command(&state).arg("prune"), 2)["error"]
            .as_str()
            .unwrap()
            .contains("repository")
    );
    assert!(output.is_dir());
    for duration in ["-1d", "10", "1.5h", "999999999999999999999d"] {
        result(command(&state).args(["prune", "--older-than", duration]), 2);
    }
}

/// Windows opens a name in any case, so a state folder named in another case is still inside
/// the repository. Prune compares canonical paths and refuses it as it refuses the same case.
#[cfg(windows)]
#[test]
fn prune_refuses_a_state_inside_the_repository_named_in_another_case() {
    let root = tempfile::tempdir().unwrap();
    let repo = support::os::canonical(root.path()).join("Repo");
    fs::create_dir(&repo).unwrap();
    fs::write(
        repo.join("artifactize.json"),
        r#"{"name":"a","basis":true}"#,
    )
    .unwrap();
    for state in [
        repo.join("state"),
        support::os::other_case(&repo).join("state"),
    ] {
        let error = result(command(&state).arg("prune").arg("--repo").arg(&repo), 2);
        assert!(
            error["error"]
                .as_str()
                .unwrap()
                .contains("outside the reviewed repository"),
            "{state:?}: {error}"
        );
    }
}