a3s 0.10.0

a3s — A3S coding agent CLI; `a3s code` launches the interactive TUI
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
513
514
515
516
517
518
519
520
521
522
use std::path::{Path, PathBuf};
use std::process::Command;

use a3s_code_core::CodeConfig;
use anyhow::{bail, Context};
use serde_json::json;

use crate::cli::args::{ConfigArgs, ConfigCommand, ConfigScope, OutputMode};
use crate::cli::context::InvocationContext;
use crate::cli::output::{render_value, usage_error};

#[derive(Clone, Debug)]
pub(crate) struct CodeAssetDirectories {
    pub agent: PathBuf,
    pub mcp: PathBuf,
    pub skill: PathBuf,
    pub flow: PathBuf,
    pub okf: PathBuf,
}

/// Effective configuration and paths for one A3S Code runtime invocation.
///
/// Interactive and non-interactive Code entry points resolve this value once
/// and pass its fields down explicitly. Runtime code must not rediscover these
/// paths from the process current directory.
#[derive(Debug)]
pub(crate) struct CodeRuntimeConfiguration {
    pub config: CodeConfig,
    pub config_path: PathBuf,
    pub asset_directories: CodeAssetDirectories,
    pub memory_dir: PathBuf,
}

pub(crate) async fn run(args: ConfigArgs, context: &InvocationContext) -> anyhow::Result<()> {
    match args.command {
        ConfigCommand::Path => show_path(context),
        ConfigCommand::Paths => show_paths(context),
        ConfigCommand::Show => show(context),
        ConfigCommand::Init(args) => init(args.scope, args.force, context),
        ConfigCommand::Edit(args) => edit(args.scope, context),
        ConfigCommand::Validate(args) => validate(args.path.as_deref(), context),
    }
}

pub(crate) fn active_config_path(context: &InvocationContext) -> anyhow::Result<PathBuf> {
    if let Some(path) = context.explicit_config.clone() {
        return Ok(path);
    }
    if let Some(path) = super::config_resolver::workspace_config_path(&context.directory) {
        return Ok(path);
    }
    user_config_path(context)
}

pub(crate) fn target_config_path(
    scope: ConfigScope,
    context: &InvocationContext,
) -> anyhow::Result<PathBuf> {
    if let Some(path) = context.explicit_config.clone() {
        return Ok(path);
    }
    match scope {
        ConfigScope::Workspace => Ok(context.directory.join(".a3s/config.acl")),
        ConfigScope::User => user_config_path(context),
    }
}

pub(crate) fn load_active_config(
    context: &InvocationContext,
) -> anyhow::Result<(PathBuf, CodeConfig)> {
    let effective = resolve_effective_config(context)?;
    Ok((effective.primary_path, effective.config))
}

pub(crate) fn resolve_effective_config(
    context: &InvocationContext,
) -> anyhow::Result<super::config_resolver::EffectiveConfig> {
    super::config_resolver::resolve(context)
}

pub(crate) fn resolve_code_runtime_configuration(
    context: &InvocationContext,
) -> anyhow::Result<CodeRuntimeConfiguration> {
    let effective = resolve_effective_config(context)?;
    let asset_directories = code_asset_directories_from_effective(context, Some(&effective))?;
    let memory_dir = context
        .environment
        .nonempty_var_os("A3S_MEMORY_DIR")
        .map(PathBuf::from)
        .or_else(|| effective.config.memory_dir.clone())
        .map(|path| context.resolve_path(path))
        .unwrap_or_else(|| context.directory.join(".a3s/memory"));

    Ok(CodeRuntimeConfiguration {
        config: effective.config,
        config_path: effective.primary_path,
        asset_directories,
        memory_dir,
    })
}

pub(crate) fn memory_directory(context: &InvocationContext) -> anyhow::Result<PathBuf> {
    if let Some(path) = context.environment.nonempty_var_os("A3S_MEMORY_DIR") {
        return Ok(context.resolve_path(PathBuf::from(path)));
    }
    let configured =
        optional_effective_config(context)?.and_then(|effective| effective.config.memory_dir);
    Ok(configured_asset_directory(
        context,
        "A3S_MEMORY_DIR",
        configured,
        ".a3s/memory",
    ))
}

pub(crate) fn code_asset_directories(
    context: &InvocationContext,
) -> anyhow::Result<CodeAssetDirectories> {
    let effective = optional_effective_config(context)?;
    code_asset_directories_from_effective(context, effective.as_ref())
}

fn code_asset_directories_from_effective(
    context: &InvocationContext,
    effective: Option<&super::config_resolver::EffectiveConfig>,
) -> anyhow::Result<CodeAssetDirectories> {
    let agent = effective
        .map(|value| {
            layered_acl_path(
                &value.layers,
                &["agent_dir", "agentDir", "agent_dirs", "agentDirs"],
                context,
            )
        })
        .transpose()?
        .flatten()
        .or_else(|| effective.and_then(|value| value.config.agent_dirs.first().cloned()));
    let skill = effective
        .map(|value| {
            layered_acl_path(
                &value.layers,
                &["skill_dir", "skillDir", "skill_dirs", "skillDirs"],
                context,
            )
        })
        .transpose()?
        .flatten()
        .or_else(|| effective.and_then(|value| value.config.skill_dirs.first().cloned()));
    let mcp = effective
        .map(|value| layered_acl_path(&value.layers, &["mcp_dir", "mcpDir"], context))
        .transpose()?
        .flatten();
    let flow = effective
        .map(|value| layered_acl_path(&value.layers, &["flow_dir", "flowDir"], context))
        .transpose()?
        .flatten();
    Ok(CodeAssetDirectories {
        agent: configured_asset_directory(context, "A3S_AGENT_DIR", agent, ".a3s/agents"),
        mcp: configured_asset_directory(context, "A3S_MCP_DIR", mcp, ".a3s/mcps"),
        skill: configured_asset_directory(context, "A3S_SKILL_DIR", skill, ".a3s/skills"),
        flow: configured_asset_directory(context, "A3S_FLOW_DIR", flow, ".a3s/flows"),
        okf: context.directory.join("okf"),
    })
}

fn optional_effective_config(
    context: &InvocationContext,
) -> anyhow::Result<Option<super::config_resolver::EffectiveConfig>> {
    let configured = context.explicit_config.is_some()
        || super::config_resolver::workspace_config_path(&context.directory).is_some()
        || context
            .user_config_path()
            .is_some_and(|path| path.is_file());
    configured
        .then(|| resolve_effective_config(context))
        .transpose()
}

fn layered_acl_path(
    layers: &[super::config_resolver::ConfigLayer],
    keys: &[&str],
    context: &InvocationContext,
) -> anyhow::Result<Option<PathBuf>> {
    let mut selected = None;
    for layer in layers {
        let source = std::fs::read_to_string(&layer.path)
            .with_context(|| format!("could not read A3S ACL {}", layer.path.display()))?;
        let document = a3s_acl::parse_acl(&source)
            .with_context(|| format!("invalid A3S ACL {}", layer.path.display()))?;
        for block in document
            .blocks
            .iter()
            .filter(|block| keys.contains(&block.name.as_str()))
        {
            let value = keys
                .iter()
                .find_map(|key| block.attributes.get(*key))
                .with_context(|| {
                    format!(
                        "A3S ACL {} must assign a value to {}",
                        layer.path.display(),
                        block.name
                    )
                })?;
            selected = acl_path_value(value, &layer.path, context)?;
        }
    }
    Ok(selected)
}

fn acl_path_value(
    value: &a3s_acl::Value,
    source: &Path,
    context: &InvocationContext,
) -> anyhow::Result<Option<PathBuf>> {
    match value {
        a3s_acl::Value::String(path) => Ok(Some(PathBuf::from(path))),
        a3s_acl::Value::List(paths) => paths
            .first()
            .map(|path| acl_path_value(path, source, context))
            .transpose()
            .map(Option::flatten),
        a3s_acl::Value::Call(name, arguments) if name == "env" => {
            let variable = arguments
                .first()
                .and_then(a3s_acl::Value::as_str)
                .with_context(|| {
                    format!("env() in {} requires one variable name", source.display())
                })?;
            context
                .environment
                .nonempty_var_os(variable)
                .map(PathBuf::from)
                .with_context(|| {
                    format!(
                        "{} referenced by A3S ACL {} is not set",
                        variable,
                        source.display()
                    )
                })
                .map(Some)
        }
        _ => bail!(
            "asset directory in {} must be a string path or non-empty path list",
            source.display()
        ),
    }
}

fn show_path(context: &InvocationContext) -> anyhow::Result<()> {
    let output = context.output_mode();
    let path = active_config_path(context)?;
    let exists = path.is_file();
    let data = json!({
        "path": path,
        "exists": exists,
        "explicit": context.explicit_config.is_some(),
    });
    render_value(output, "config.path", data, || {
        println!("{}", path.display());
        if !exists {
            eprintln!("not created; run `a3s config init`");
        }
    })
}

fn show_paths(context: &InvocationContext) -> anyhow::Result<()> {
    let output = context.output_mode();
    let config = active_config_path(context)?;
    let user_config = user_config_path(context)?;
    let workspace_config = context.directory.join(".a3s/config.acl");
    let data_root = context.component_paths.data_root.clone();
    let state_root = context.component_paths.state_root.clone();
    let cache_root = context.component_paths.cache_root.clone();
    let CodeAssetDirectories {
        agent,
        mcp,
        skill,
        flow,
        okf,
    } = code_asset_directories(context)?;
    let memory = memory_directory(context)?;
    let kb = crate::tui::kbutil::kb_dir(&context.directory.to_string_lossy());

    let data = json!({
        "config": config,
        "userConfig": user_config,
        "workspaceConfig": workspace_config,
        "data": data_root,
        "state": state_root,
        "cache": cache_root,
        "assets": {
            "agent": agent,
            "mcp": mcp,
            "skill": skill,
            "flow": flow,
        },
        "memory": memory,
        "knowledgeBase": kb,
        "okf": okf,
    });
    render_value(output, "config.paths", data, || {
        for (name, path) in [
            ("config", config.as_path()),
            ("user config", user_config.as_path()),
            ("workspace config", workspace_config.as_path()),
            ("data", data_root.as_path()),
            ("state", state_root.as_path()),
            ("cache", cache_root.as_path()),
            ("agent", agent.as_path()),
            ("mcp", mcp.as_path()),
            ("skill", skill.as_path()),
            ("flow", flow.as_path()),
            ("memory", memory.as_path()),
            ("kb", kb.as_path()),
            ("okf", okf.as_path()),
        ] {
            println!("{name:<18} {}", path.display());
        }
    })
}

fn show(context: &InvocationContext) -> anyhow::Result<()> {
    let output = context.output_mode();
    let effective = resolve_effective_config(context)?;
    let path = effective.primary_path;
    let config = effective.config;
    let layers = effective.layers;
    let provenance = effective.provenance;
    let explicit = effective.explicit;
    let models = config
        .list_models()
        .into_iter()
        .map(|(provider, model)| {
            json!({
                "id": format!("{}/{}", provider.name, model.id),
                "name": model.name,
                "reasoning": model.reasoning,
                "toolCall": model.tool_call,
            })
        })
        .collect::<Vec<_>>();
    let providers = config
        .providers
        .iter()
        .map(|provider| provider.name.clone())
        .collect::<Vec<_>>();
    let default_model = config.default_model.clone();
    let os_address = config.os.as_ref().map(|os| os.address.clone());
    let data = json!({
        "path": path,
        "explicit": explicit,
        "layers": layers,
        "provenance": provenance,
        "defaultModel": default_model,
        "providers": providers,
        "models": models,
        "os": {
            "configured": os_address.is_some(),
            "address": os_address,
        },
    });
    render_value(output, "config.show", data, || {
        println!("config: {}", path.display());
        if explicit {
            println!("resolution: explicit single file");
        } else {
            println!("layers: {}", layers.len());
            for layer in &layers {
                println!("  {:?}: {}", layer.kind, layer.path.display());
            }
        }
        println!(
            "default model: {}",
            default_model.as_deref().unwrap_or("(not set)")
        );
        println!("providers: {}", providers.len());
        println!("models: {}", models.len());
        println!(
            "os: {}",
            os_address.as_deref().unwrap_or("(not configured)")
        );
    })
}

fn init(scope: ConfigScope, force: bool, context: &InvocationContext) -> anyhow::Result<()> {
    let output = context.output_mode();
    let path = target_config_path(scope, context)?;
    let existed = path.exists();
    if existed && !force {
        let data = json!({"path": path, "created": false, "replaced": false});
        return render_value(output, "config.init", data, || {
            println!("config already exists: {}", path.display());
        });
    }

    CodeConfig::from_acl(crate::config::config_template())
        .map_err(|error| anyhow::anyhow!("built-in A3S ACL template is invalid: {error}"))?;
    crate::api::code_web::config::persistence::write_atomic(
        &path,
        crate::config::config_template().as_bytes(),
    )
    .map_err(|error| anyhow::anyhow!("could not write {}: {error}", path.display()))?;

    let data = json!({"path": path, "created": !existed, "replaced": existed});
    render_value(output, "config.init", data, || {
        if existed {
            println!("replaced config: {}", path.display());
        } else {
            println!("created config: {}", path.display());
        }
    })
}

fn edit(scope: ConfigScope, context: &InvocationContext) -> anyhow::Result<()> {
    let output = context.output_mode();
    let path = target_config_path(scope, context)?;
    if !path.exists() {
        crate::config::write_template_config(&path)
            .with_context(|| format!("could not create {}", path.display()))?;
    }
    if output != OutputMode::Human {
        return Err(usage_error(
            "`a3s config edit` is interactive and requires human output mode",
        ));
    }
    open_editor(&path, context)?;
    render_value(output, "config.edit", json!({"path": path}), || {})
}

fn validate(path: Option<&Path>, context: &InvocationContext) -> anyhow::Result<()> {
    let output = context.output_mode();
    let (path, config, layers) = match path {
        Some(path) => {
            let path = context.resolve_path(path.to_path_buf());
            let config = CodeConfig::from_file(&path)
                .map_err(|error| anyhow::anyhow!("invalid A3S ACL {}: {error}", path.display()))?;
            (path, config, None)
        }
        None => {
            let effective = resolve_effective_config(context)?;
            (
                effective.primary_path,
                effective.config,
                Some(effective.layers),
            )
        }
    };
    let issues = crate::api::code_web::config::validation::validate_config(&config);
    if !issues.is_empty() {
        bail!("invalid A3S ACL {}: {}", path.display(), issues.join("; "));
    }
    let data = json!({
        "path": path,
        "valid": true,
        "layers": layers,
        "providers": config.providers.len(),
        "models": config.list_models().len(),
    });
    render_value(output, "config.validate", data, || {
        if let Some(layers) = layers {
            println!("valid effective A3S ACL ({} layers)", layers.len());
            for layer in layers {
                println!("  {:?}: {}", layer.kind, layer.path.display());
            }
        } else {
            println!("valid A3S ACL: {}", path.display());
        }
    })
}

fn user_config_path(context: &InvocationContext) -> anyhow::Result<PathBuf> {
    context
        .user_config_path()
        .ok_or_else(|| anyhow::anyhow!("user home is unavailable; pass `--config <path>`"))
}

fn configured_asset_directory(
    context: &InvocationContext,
    environment_name: &str,
    configured: Option<PathBuf>,
    fallback: &str,
) -> PathBuf {
    context
        .environment
        .nonempty_var_os(environment_name)
        .map(PathBuf::from)
        .or(configured)
        .map(|path| context.resolve_path(path))
        .or_else(|| context.home.as_deref().map(|home| home.join(fallback)))
        .unwrap_or_else(|| context.directory.join(fallback))
}

fn open_editor(path: &Path, context: &InvocationContext) -> anyhow::Result<()> {
    let editor = context
        .environment
        .utf8("VISUAL")?
        .filter(|value| !value.trim().is_empty())
        .or(context
            .environment
            .utf8("EDITOR")?
            .filter(|value| !value.trim().is_empty()));
    let Some(editor) = editor else {
        println!("{}", path.display());
        println!("set VISUAL or EDITOR to edit from the CLI");
        return Ok(());
    };
    let mut parts = editor.split_whitespace();
    let program = parts
        .next()
        .ok_or_else(|| anyhow::anyhow!("editor command is empty"))?;
    let status = Command::new(program)
        .args(parts)
        .arg(path)
        .current_dir(&context.directory)
        .status()
        .with_context(|| format!("failed to launch editor `{editor}`"))?;
    if !status.success() {
        bail!("editor `{editor}` exited with {status}");
    }
    Ok(())
}