aion-server 0.21.0

Aion workflow server library: HTTP, gRPC, WebSocket, and worker endpoints. Run it with the `aion` binary from the aion-cli crate.
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! Focused regressions for Aion-home config discovery and state roots.

use std::{fs, path::Path, process::Command};

use crate::config::ConfigSource;
use crate::test_support::CapturedLogs;

use super::{CliOverrides, HomeSource, ServerConfig, aion_home};

const HOME_PROBE: &str = "AION_HOME_TEST_PROBE";
const HOME_EXPECTED: &str = "AION_HOME_TEST_EXPECTED";

fn write(path: &Path, contents: &str) -> Result<(), Box<dyn std::error::Error>> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    fs::write(path, contents)?;
    Ok(())
}

#[test]
fn discovery_precedence_covers_explicit_local_home_and_defaults()
-> Result<(), Box<dyn std::error::Error>> {
    let scratch = crate::test_support::private_tempdir()?;
    let home = scratch.path().join("home");
    let working_dir = scratch.path().join("project");
    fs::create_dir_all(&working_dir)?;
    let home_config = home.join("config.toml");
    let local_config = working_dir.join("aion.toml");
    let explicit_config = scratch.path().join("explicit.toml");

    write(&home_config, "[namespaces]\ndefault = \"home\"\n")?;
    let loaded = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Derived,
        &working_dir,
    )?;
    assert_eq!(loaded.config.namespaces.default, "home");
    assert_eq!(
        loaded.resolution.source,
        ConfigSource::AionHome(home_config)
    );

    write(&local_config, "[namespaces]\ndefault = \"local\"\n")?;
    let loaded = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Derived,
        &working_dir,
    )?;
    assert_eq!(loaded.config.namespaces.default, "local");
    assert_eq!(
        loaded.resolution.source,
        ConfigSource::ProjectLocal(local_config.clone())
    );

    write(&explicit_config, "[namespaces]\ndefault = \"explicit\"\n")?;
    let cli = CliOverrides {
        config_path: Some(explicit_config.clone()),
        ..CliOverrides::default()
    };
    let loaded = ServerConfig::load_for_test(&cli, &home, HomeSource::Derived, &working_dir)?;
    assert_eq!(loaded.config.namespaces.default, "explicit");
    assert_eq!(
        loaded.resolution.source,
        ConfigSource::Explicit(explicit_config)
    );

    fs::remove_file(local_config)?;
    fs::remove_file(home.join("config.toml"))?;
    let loaded = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Derived,
        &working_dir,
    )?;
    assert_eq!(loaded.config.namespaces.default, "default");
    assert_eq!(loaded.resolution.source, ConfigSource::BuiltInDefaults);
    Ok(())
}

#[test]
fn malformed_home_config_is_a_loud_typed_failure() -> Result<(), Box<dyn std::error::Error>> {
    let scratch = crate::test_support::private_tempdir()?;
    let home = scratch.path().join("home");
    let working_dir = scratch.path().join("project");
    fs::create_dir_all(&working_dir)?;
    let config_path = home.join("config.toml");
    write(&config_path, "[store\nbackend = nope")?;

    let error = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Derived,
        &working_dir,
    )
    .err()
    .ok_or("malformed home config unexpectedly loaded")?;
    let message = error.to_string();
    assert!(message.contains("failed to parse Aion home file"));
    assert!(message.contains(&config_path.display().to_string()));
    Ok(())
}

#[test]
fn unconfigured_paths_resolve_under_home_without_eager_creation()
-> Result<(), Box<dyn std::error::Error>> {
    let scratch = crate::test_support::private_tempdir()?;
    let home = scratch.path().join("not-created-yet");
    let working_dir = scratch.path().join("project");
    fs::create_dir_all(&working_dir)?;

    let loaded = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Derived,
        &working_dir,
    )?;
    assert_eq!(
        loaded.config.store.data_dir.as_deref(),
        home.join("data").to_str()
    );
    assert_eq!(
        loaded.config.authoring.workspace_dir.as_deref(),
        Some(home.join("authoring").as_path())
    );
    assert!(
        !home.exists(),
        "config reads must not eagerly create Aion home"
    );
    Ok(())
}

/// The DERIVED world: no `AION_HOME`, so the operator has moved nothing and
/// their durable state genuinely lives in the working directory. The guard
/// adopts it and says so. A fix to #113 that broke this would strand real
/// state, which is worse than the defect it was fixing.
#[test]
fn a_derived_home_adopts_legacy_directories_and_guards_only_unconfigured_defaults()
-> Result<(), Box<dyn std::error::Error>> {
    let scratch = crate::test_support::private_tempdir()?;
    let home = scratch.path().join("home");
    let working_dir = scratch.path().join("project");
    let legacy_data = working_dir.join("aion-data");
    let legacy_authoring = working_dir.join("aion-authoring");
    fs::create_dir_all(&legacy_data)?;
    fs::create_dir_all(&legacy_authoring)?;

    let loaded = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Derived,
        &working_dir,
    )?;
    assert_eq!(
        loaded.config.store.data_dir.as_deref(),
        legacy_data.to_str()
    );
    assert_eq!(
        loaded.config.authoring.workspace_dir.as_deref(),
        Some(legacy_authoring.as_path())
    );
    assert_eq!(loaded.resolution.legacy_notices.len(), 2);
    for notice in &loaded.resolution.legacy_notices {
        let line = notice.to_string();
        assert!(line.contains("AION HOME MIGRATION REQUIRED"));
        assert!(line.contains("stop the server, move"));
        assert!(line.contains(&home.display().to_string()));
        assert!(line.contains(&working_dir.display().to_string()));
    }

    let (captured, ()) = CapturedLogs::capture(|| loaded.resolution.log_startup());
    let logs = captured.text()?;
    assert!(logs.contains("Aion home legacy-directory migration guard active"));
    assert!(logs.contains("AION HOME MIGRATION REQUIRED"));
    assert!(logs.contains(&legacy_data.display().to_string()));
    assert!(logs.contains(&home.join("data").display().to_string()));
    assert!(logs.contains("aion-server configuration resolved"));
    assert!(logs.contains("built-in defaults"));
    assert!(logs.contains("aion-server data root resolved"));
    assert!(logs.contains("aion-server authoring root resolved"));

    write(
        &working_dir.join("aion.toml"),
        "[store]\ndata_dir = \"configured-data\"\n\n[authoring]\nworkspace_dir = \"configured-authoring\"\n",
    )?;
    let loaded = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Derived,
        &working_dir,
    )?;
    assert_eq!(
        loaded.config.store.data_dir.as_deref(),
        Some("configured-data")
    );
    assert_eq!(
        loaded.config.authoring.workspace_dir.as_deref(),
        Some(Path::new("configured-authoring"))
    );
    assert!(loaded.resolution.legacy_notices.is_empty());
    Ok(())
}

#[cfg(unix)]
#[test]
fn legacy_symlink_directories_are_never_selected() -> Result<(), Box<dyn std::error::Error>> {
    use std::os::unix::fs::symlink;

    let scratch = crate::test_support::private_tempdir()?;
    let home = scratch.path().join("home");
    let working_dir = scratch.path().join("project");
    let outside = scratch.path().join("outside");
    fs::create_dir(&working_dir)?;
    fs::create_dir(&outside)?;
    symlink(&outside, working_dir.join("aion-data"))?;
    symlink(&outside, working_dir.join("aion-authoring"))?;

    let loaded = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Derived,
        &working_dir,
    )?;
    assert_eq!(
        loaded.config.store.data_dir.as_deref(),
        home.join("data").to_str()
    );
    assert_eq!(
        loaded.config.authoring.workspace_dir.as_deref(),
        Some(home.join("authoring").as_path())
    );
    assert!(loaded.resolution.legacy_notices.is_empty());
    Ok(())
}

/// The EXPLICIT world, and the #113 regression pin: an operator who names a
/// home is isolating this server's state, so a legacy directory sitting in the
/// working directory must not be able to drag it back. **This assertion fails
/// against the code as it stood before the fix** — both roots resolved to the
/// working directory instead.
#[test]
fn an_explicit_home_is_never_overridden_by_a_legacy_working_directory()
-> Result<(), Box<dyn std::error::Error>> {
    let scratch = crate::test_support::private_tempdir()?;
    let home = scratch.path().join("isolated-home");
    let working_dir = scratch.path().join("project");
    let legacy_data = working_dir.join("aion-data");
    let legacy_authoring = working_dir.join("aion-authoring");
    fs::create_dir_all(&legacy_data)?;
    fs::create_dir_all(&legacy_authoring)?;

    let loaded = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Explicit,
        &working_dir,
    )?;
    assert_eq!(
        loaded.config.store.data_dir.as_deref(),
        home.join("data").to_str(),
        "an explicitly named home lost the data root to a legacy working directory"
    );
    assert_eq!(
        loaded.config.authoring.workspace_dir.as_deref(),
        Some(home.join("authoring").as_path()),
        "an explicitly named home lost the authoring root to a legacy working directory"
    );

    // Ignoring it silently would be its own defect: the operator's control was
    // honoured, and they still need told the other directory is sitting there
    // and that nothing is reading or writing it.
    assert_eq!(loaded.resolution.legacy_notices.len(), 2);
    for notice in &loaded.resolution.legacy_notices {
        let line = notice.to_string();
        assert!(
            line.contains("AION HOME IS SET, SO THE LEGACY DIRECTORY WAS IGNORED"),
            "an ignored legacy directory was reported as a migration chore: {line}"
        );
        assert!(!line.contains("MIGRATION REQUIRED"));
        assert!(line.contains(&working_dir.display().to_string()));
        assert!(line.contains(&home.display().to_string()));
    }

    let (captured, ()) = CapturedLogs::capture(|| loaded.resolution.log_startup());
    let logs = captured.text()?;
    assert!(logs.contains("Aion home legacy directory ignored because AION_HOME is set"));
    assert!(!logs.contains("migration guard active"));
    assert!(logs.contains(&home.join("data").display().to_string()));
    Ok(())
}

/// #114's server half: the CLI can now name the gRPC bind, and the override has
/// to actually reach `[server].grpc_address`. Without this the flag would parse,
/// convert, and be dropped one layer lower — the same shape as the `--endpoint`
/// defect it replaces.
#[test]
fn the_grpc_bind_override_reaches_the_server_config() -> Result<(), Box<dyn std::error::Error>> {
    let scratch = crate::test_support::private_tempdir()?;
    let home = scratch.path().join("home");
    let working_dir = scratch.path().join("project");
    fs::create_dir_all(&working_dir)?;

    let baseline = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Derived,
        &working_dir,
    )?;
    let cli = CliOverrides {
        grpc_address: Some("127.0.0.1:18051".parse()?),
        ..CliOverrides::default()
    };
    let overridden = ServerConfig::load_for_test(&cli, &home, HomeSource::Derived, &working_dir)?;

    assert_ne!(
        baseline.config.server.grpc_address, overridden.config.server.grpc_address,
        "the override changed nothing; comparing against the default would have been vacuous"
    );
    assert_eq!(
        overridden.config.server.grpc_address.port(),
        18051,
        "--grpc-address parsed and converted but never reached the bind"
    );
    Ok(())
}

/// The control against fixing #113 by disabling the branch outright: with no
/// legacy directory present there is nothing to report, so a suite that only
/// asserted "notices exist" would pass on a server that reported them always.
#[test]
fn an_explicit_home_with_no_legacy_directory_reports_nothing()
-> Result<(), Box<dyn std::error::Error>> {
    let scratch = crate::test_support::private_tempdir()?;
    let home = scratch.path().join("isolated-home");
    let working_dir = scratch.path().join("project");
    fs::create_dir_all(&working_dir)?;

    let loaded = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Explicit,
        &working_dir,
    )?;
    assert_eq!(
        loaded.config.store.data_dir.as_deref(),
        home.join("data").to_str()
    );
    assert!(loaded.resolution.legacy_notices.is_empty());
    Ok(())
}

#[test]
fn aion_home_environment_override_is_respected_everywhere() -> Result<(), Box<dyn std::error::Error>>
{
    let scratch = crate::test_support::private_tempdir()?;
    let home = scratch.path().join("overridden-home");
    let working_dir = scratch.path().join("project");
    fs::create_dir_all(&working_dir)?;
    // #113 regression, planted at the production path: the child runs with a
    // real `AION_HOME` and a working directory that holds both legacy
    // directories. Before the fix these won, and a server the operator had
    // deliberately isolated wrote its durable state into the shared location it
    // was pointed away from. The child asserts both roots resolve under the
    // home, so this is a live inversion pin and not a log-message check.
    fs::create_dir_all(working_dir.join("aion-data"))?;
    fs::create_dir_all(working_dir.join("aion-authoring"))?;
    write(
        &home.join("config.toml"),
        "[namespaces]\ndefault = \"from-home-config\"\n",
    )?;
    let executable = std::env::current_exe()?;
    let status = Command::new(executable)
        .arg("--exact")
        .arg("config::load::home_tests::aion_home_env_probe_child")
        .arg("--nocapture")
        .current_dir(&working_dir)
        .env("AION_HOME", &home)
        .env(HOME_PROBE, "1")
        .env(HOME_EXPECTED, &home)
        .env_remove("AION_STORE_DATA_DIR")
        .env_remove("AION_AUTHORING_WORKSPACE_DIR")
        .status()?;
    assert!(status.success(), "AION_HOME probe child failed");
    Ok(())
}

#[test]
fn missing_home_is_a_typed_error() -> Result<(), Box<dyn std::error::Error>> {
    let executable = std::env::current_exe()?;
    let status = Command::new(executable)
        .arg("--exact")
        .arg("config::load::home_tests::aion_home_env_probe_child")
        .arg("--nocapture")
        .env_remove("AION_HOME")
        .env_remove("HOME")
        .env(HOME_PROBE, "missing")
        .status()?;
    assert!(status.success(), "missing-home probe child failed");
    Ok(())
}

#[test]
fn aion_home_env_probe_child() -> Result<(), Box<dyn std::error::Error>> {
    let Some(mode) = std::env::var_os(HOME_PROBE) else {
        return Ok(());
    };
    if mode == "missing" {
        let error = aion_home()
            .err()
            .ok_or("missing AION_HOME and HOME unexpectedly resolved")?;
        assert!(error.to_string().contains("set AION_HOME or HOME"));
        return Ok(());
    }
    let expected = std::env::var_os(HOME_EXPECTED).ok_or("probe omitted expected home")?;
    let expected = std::path::PathBuf::from(expected);
    let resolved = aion_home()?;
    assert_eq!(resolved.path, expected);
    // The provenance is the fact the legacy fallback needs and used to lose.
    assert_eq!(resolved.source, HomeSource::Explicit);
    let config = ServerConfig::load(&CliOverrides::default())?;
    assert_eq!(config.namespaces.default, "from-home-config");
    assert_eq!(
        config.store.data_dir.as_deref(),
        expected.join("data").to_str()
    );
    assert_eq!(
        config.authoring.workspace_dir.as_deref(),
        Some(expected.join("authoring").as_path())
    );
    Ok(())
}

/// #180 review m10: a leading `~` in the operator-supplied path VALUES
/// (`store.data_dir`, `authoring.workspace_dir`) expands against `$HOME`
/// exactly as it does for `AION_HOME` and `--config`, instead of silently
/// becoming a literal `./~` directory; `~user` is refused by name; and a
/// tilde-free path passes through byte-for-byte.
#[test]
fn operator_path_values_expand_a_leading_tilde() -> Result<(), Box<dyn std::error::Error>> {
    let Some(real_home) = std::env::var_os("HOME").filter(|value| !value.is_empty()) else {
        // Tilde expansion is defined against $HOME; without one there is
        // nothing to measure here (the refusal path has its own pin in
        // `home::expand_tilde`'s tests).
        tracing::info!("HOME is not set; skipping tilde-value expansion test");
        return Ok(());
    };
    let scratch = crate::test_support::private_tempdir()?;
    let home = scratch.path().join("home");
    let working_dir = scratch.path().join("project");
    fs::create_dir_all(&working_dir)?;

    write(
        &home.join("config.toml"),
        "[store]\nbackend = \"haematite\"\ndata_dir = \"~/relocated-store\"\n\
         [authoring]\nworkspace_dir = \"~/relocated-workspace\"\n",
    )?;
    let loaded = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Derived,
        &working_dir,
    )?;
    let expected_data = Path::new(&real_home).join("relocated-store");
    assert_eq!(
        loaded.config.store.data_dir.as_deref(),
        expected_data.to_str(),
        "store.data_dir must expand `~/` against $HOME"
    );
    assert_eq!(
        loaded.config.authoring.workspace_dir.as_deref(),
        Some(Path::new(&real_home).join("relocated-workspace").as_path()),
        "authoring.workspace_dir must expand `~/` against $HOME"
    );

    // `~user` is refused by name, not passed through.
    write(
        &home.join("config.toml"),
        "[store]\nbackend = \"haematite\"\ndata_dir = \"~someone/store\"\n",
    )?;
    let Err(error) = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Derived,
        &working_dir,
    ) else {
        return Err("~user in store.data_dir must refuse".into());
    };
    assert!(
        error.to_string().contains("~user"),
        "the refusal must name the unsupported `~user` form, got: {error}"
    );

    // A tilde-free value is untouched.
    write(
        &home.join("config.toml"),
        "[store]\nbackend = \"haematite\"\ndata_dir = \"plain-data\"\n",
    )?;
    let loaded = ServerConfig::load_for_test(
        &CliOverrides::default(),
        &home,
        HomeSource::Derived,
        &working_dir,
    )?;
    assert_eq!(
        loaded.config.store.data_dir.as_deref(),
        Some("plain-data"),
        "a tilde-free data_dir must pass through byte-for-byte"
    );
    Ok(())
}