@@ -1,6 +1,7 @@
1-//! Global config: `$XDG_CONFIG_HOME/mini-coder/config.json` (or the equivalent
1+//! Config: `$XDG_CONFIG_HOME/mini-coding-agent/config.json` (or the equivalent
22 //! under `$HOME`). Every user-facing behavior that can vary comes from here,
3-//! with a sane default. There is no project-local config.
3+//! with a sane default. There is no project-local config. A `-c`/`--config`
4+//! file is read after the global one and overrides its keys.
45
56 const std = @import("std");
67 const platform = @import("platform.zig");
@@ -27,36 +28,50 @@ pub const CustomProvider = struct {
2728 };
2829
2930 pub const Config = struct {
31+ /// The file `save` writes back to: the global config, or the override when
32+ /// one was given on the command line.
33+ path: []const u8,
3034 sessions_dir: []const u8,
31- auth_file: []const u8,
3235 system_prompt: []const u8,
3336 discover_agent_files: bool,
3437 skills_dirs: []const []const u8,
3538 tools: []const ToolName,
3639 provider: ?[]const u8,
3740 model: ?[]const u8,
38- thinking_effort: []const u8,
41+ /// Null when unset: no reasoning parameter is sent, so the provider picks.
42+ thinking_effort: ?[]const u8,
3943 custom_providers: []const CustomProvider,
4044 };
4145
4246 const valid_efforts = [_][]const u8{ "off", "minimal", "low", "medium", "high", "xhigh", "max" };
4347 const valid_apis = [_][]const u8{ "openai-completions", "openai-responses", "anthropic-messages", "google-generative-ai" };
4448 const known_keys = [_][]const u8{
45- "sessionsDir", "authFile", "systemPrompt", "discoverAgentFiles",
46- "skillsDirs", "tools", "provider", "model",
47- "thinkingEffort", "customProviders",
49+ "sessionsDir", "systemPrompt", "discoverAgentFiles",
50+ "skillsDirs", "tools", "provider",
51+ "model", "thinkingEffort", "customProviders",
4852 };
4953
5054 fn configDir(a: std.mem.Allocator) []const u8 {
5155 const base = platform.getEnv("XDG_CONFIG_HOME") orelse
5256 util.join(a, &.{ platform.home() orelse "", ".config" }) catch "";
53- return util.join(a, &.{ base, "mini-coder" }) catch "mini-coder";
57+ return util.join(a, &.{ base, "mini-coding-agent" }) catch "mini-coding-agent";
5458 }
5559
5660 pub fn configPath(a: std.mem.Allocator) []const u8 {
5761 return util.join(a, &.{ configDir(a), "config.json" }) catch "config.json";
5862 }
5963
64+/// `$XDG_STATE_HOME/mini-coding-agent/sessions`, else
65+/// `$HOME/.local/state/mini-coding-agent/sessions`, else the relative
66+/// `sessions`. Paths are not mini-coder's cwd-relative default.
67+fn sessionsDir(a: std.mem.Allocator) []const u8 {
68+ const state = platform.getEnv("XDG_STATE_HOME") orelse blk: {
69+ const home = platform.home() orelse return "sessions";
70+ break :blk util.join(a, &.{ home, ".local/state" }) catch return "sessions";
71+ };
72+ return util.join(a, &.{ state, "mini-coding-agent/sessions" }) catch "sessions";
73+}
74+
6075 fn fail(a: std.mem.Allocator, path: []const u8, comptime fmt: []const u8, args: anytype) error{InvalidConfig} {
6176 const detail = std.fmt.allocPrint(a, fmt, args) catch "invalid";
6277 platform.printErr("config {s}: {s}\n", .{ path, detail });
@@ -127,8 +142,10 @@ fn parseCustomProviders(a: std.mem.Allocator, obj: std.json.ObjectMap, path: []c
127142 const p = item.object;
128143 const id = (try strField(a, p, "id", path)) orelse
129144 return fail(a, path, "\"customProviders[{d}].id\" is required", .{i});
145+ if (id.len == 0) return fail(a, path, "\"customProviders[{d}].id\" must not be empty", .{i});
130146 const base_url = (try strField(a, p, "baseUrl", path)) orelse
131147 return fail(a, path, "\"customProviders[{d}].baseUrl\" is required", .{i});
148+ if (base_url.len == 0) return fail(a, path, "\"customProviders[{d}].baseUrl\" must not be empty", .{i});
132149 const api = (try strField(a, p, "api", path)) orelse
133150 return fail(a, path, "\"customProviders[{d}].api\" is required", .{i});
134151 var api_ok = false;
@@ -154,13 +171,29 @@ fn parseCustomProviders(a: std.mem.Allocator, obj: std.json.ObjectMap, path: []c
154171 return out.toOwnedSlice(a) catch error.OutOfMemory;
155172 }
156173
157-/// Loads the global config. A missing file yields every default. Any other
158-/// read, parse, or validation failure is reported on stderr and returned.
159-/// The result is allocated from `a` and lives for the process.
160-pub fn load(a: std.mem.Allocator) !Config {
161- const path = configPath(a);
174+/// Loads the config. The global file is always read; when `override` is given
175+/// it is read after and its keys win. A missing global file means every
176+/// default, but a missing override is an error. Any read, parse, or validation
177+/// failure is reported on stderr and returned. The result is allocated from
178+/// `a` and lives for the process.
179+pub fn load(a: std.mem.Allocator, override: ?[]const u8) !Config {
180+ var cfg = try defaults(a);
181+ try readInto(a, configPath(a), false, &cfg);
182+
183+ if (override) |path| {
184+ cfg.path = path;
185+ try readInto(a, path, true, &cfg);
186+ }
187+ return cfg;
188+}
189+
190+fn readInto(a: std.mem.Allocator, path: []const u8, required: bool, cfg: *Config) !void {
162191 const text = util.readFileAlloc(a, path, 1 << 20) catch |e| switch (e) {
163- error.FileNotFound => return defaults(a),
192+ error.FileNotFound => {
193+ if (!required) return;
194+ platform.printErr("config {s}: no such file\n", .{path});
195+ return error.InvalidConfig;
196+ },
164197 else => {
165198 platform.printErr("config {s}: {s}\n", .{ path, @errorName(e) });
166199 return error.InvalidConfig;
@@ -182,9 +215,7 @@ pub fn load(a: std.mem.Allocator) !Config {
182215 if (!known) return fail(a, path, "unknown key \"{s}\"", .{entry.key_ptr.*});
183216 }
184217
185- var cfg = try defaults(a);
186218 if (try strField(a, obj, "sessionsDir", path)) |v| cfg.sessions_dir = v;
187- if (try strField(a, obj, "authFile", path)) |v| cfg.auth_file = v;
188219 if (try strField(a, obj, "systemPrompt", path)) |v| cfg.system_prompt = v;
189220 if (try strField(a, obj, "provider", path)) |v| cfg.provider = v;
190221 if (try strField(a, obj, "model", path)) |v| cfg.model = v;
@@ -200,7 +231,6 @@ pub fn load(a: std.mem.Allocator) !Config {
200231 if (!ok) return fail(a, path, "unknown thinkingEffort \"{s}\"", .{v});
201232 cfg.thinking_effort = v;
202233 }
203- return cfg;
204234 }
205235
206236 /// The subset of config a TUI command persists. `null` leaves a key alone.
@@ -211,14 +241,14 @@ pub const Update = struct {
211241 };
212242
213243 /// Merges `update` into the on-disk config, preserving every other key. The
214-/// merge is textual: the existing file is parsed, the changed keys replaced,
215-/// and the whole object written back to `config.json`.
216-pub fn save(a: std.mem.Allocator, update: Update) !void {
244+/// merge is textual: the file at `cfg.path` is parsed, the changed keys
245+/// replaced, and the whole object written back.
246+pub fn save(a: std.mem.Allocator, cfg: *const Config, update: Update) !void {
217247 var arena = std.heap.ArenaAllocator.init(a);
218248 defer arena.deinit();
219249 const tmp = arena.allocator();
220250
221- const path = configPath(a);
251+ const path = cfg.path;
222252 var root: std.json.Value = .{ .object = std.json.ObjectMap.empty };
223253 if (util.readFileAlloc(tmp, path, 1 << 20)) |text| {
224254 const parsed = std.json.parseFromSliceLeaky(std.json.Value, tmp, text, .{}) catch
@@ -242,17 +272,16 @@ pub fn save(a: std.mem.Allocator, update: Update) !void {
242272 }
243273
244274 fn defaults(a: std.mem.Allocator) !Config {
245- const cwd = try std.process.currentPathAlloc(platform.io, a);
246275 return .{
247- .sessions_dir = try util.join(a, &.{ cwd, "sessions" }),
248- .auth_file = try util.join(a, &.{ configDir(a), "auth.json" }),
276+ .path = configPath(a),
277+ .sessions_dir = sessionsDir(a),
249278 .system_prompt = "",
250279 .discover_agent_files = true,
251280 .skills_dirs = &.{},
252281 .tools = &.{ .edit, .read, .bash },
253282 .provider = null,
254283 .model = null,
255- .thinking_effort = "medium",
284+ .thinking_effort = null,
256285 .custom_providers = &.{},
257286 };
258287 }