xonecas.io
79113
Ma

mini-coding-agent / commit 35198b8

feat: adopt some unix conventions

xonecas2026-10-10 35198b86d65505f9b3d4f9e98de1beeec9ccf433 parent 2a49c2e

6 files changed

src/config.zig+8−2
@@ -67,8 +67,14 @@ const File = struct {
6767 snapshotUsesGitignore: ?bool = null,
6868 };
6969
70+/// An empty environment variable is treated as unset, per the XDG spec.
71+fn xdg(key: []const u8) ?[]const u8 {
72+ const value = platform.getEnv(key) orelse return null;
73+ return if (value.len == 0) null else value;
74+}
75+
7076 fn configDir(a: std.mem.Allocator) []const u8 {
71- const base = platform.getEnv("XDG_CONFIG_HOME") orelse
77+ const base = xdg("XDG_CONFIG_HOME") orelse
7278 filesystem.join(a, &.{ platform.home() orelse "", ".config" }) catch "";
7379 return filesystem.join(a, &.{ base, "mini-coding-agent" }) catch "mini-coding-agent";
7480 }
@@ -78,7 +84,7 @@ pub fn configPath(a: std.mem.Allocator) []const u8 {
7884 }
7985
8086 fn stateDir(a: std.mem.Allocator) []const u8 {
81- return platform.getEnv("XDG_STATE_HOME") orelse blk: {
87+ return xdg("XDG_STATE_HOME") orelse blk: {
8288 const home = platform.home() orelse return "";
8389 break :blk filesystem.join(a, &.{ home, ".local/state" }) catch "";
8490 };
src/filesystem.zig+1−1
@@ -13,7 +13,7 @@ pub fn writeFile(path: []const u8, data: []const u8) !void {
1313 if (std.fs.path.dirname(path)) |dir| {
1414 std.Io.Dir.cwd().createDirPath(platform.io, dir) catch {};
1515 }
16- const f = try std.Io.Dir.cwd().createFile(platform.io, path, .{ .truncate = true });
16+ const f = try std.Io.Dir.cwd().createFile(platform.io, path, .{ .truncate = true, .permissions = .fromMode(0o600) });
1717 defer f.close(platform.io);
1818 try f.writeStreamingAll(platform.io, data);
1919 }
src/headless.zig+5−1
@@ -6,8 +6,10 @@ const message_mod = @import("message.zig");
66 const agent = @import("agent.zig");
77
88 var cancel = std.atomic.Value(bool).init(false);
9+var caught_signal = std.atomic.Value(u32).init(0);
910
10-fn onSignal(_: std.posix.SIG) callconv(.c) void {
11+fn onSignal(sig: std.posix.SIG) callconv(.c) void {
12+ caught_signal.store(@intFromEnum(sig), .seq_cst);
1113 cancel.store(true, .release);
1214 }
1315
@@ -59,6 +61,8 @@ pub fn run(a: std.mem.Allocator, prompt_text: []const u8, opts: agent.Options) u
5961 o.cancel = &cancel;
6062 agent.runTurn(o, &messages, null, .{ .ctx = &pc, .on_event = printEvent });
6163 opts.session.close();
64+ const sig = caught_signal.load(.seq_cst);
65+ if (sig != 0) platform.reRaise(@enumFromInt(sig));
6266 if (pc.failed) return 1;
6367
6468 var i = messages.items.len;
src/platform.zig+19−1
@@ -29,7 +29,25 @@ pub fn getEnv(key: []const u8) ?[]const u8 {
2929 }
3030
3131 pub fn home() ?[]const u8 {
32- return getEnv("HOME");
32+ if (getEnv("HOME")) |h| {
33+ if (h.len > 0) return h;
34+ }
35+ const pw = std.c.getpwuid(std.c.getuid()) orelse return null;
36+ const dir = pw.dir orelse return null;
37+ return std.mem.sliceTo(dir, 0);
38+}
39+
40+/// Reset `sig` to its default disposition and re-raise it, so the process dies
41+/// with the signal instead of masking it behind a normal exit code.
42+pub fn reRaise(sig: std.posix.SIG) noreturn {
43+ const act = std.posix.Sigaction{
44+ .handler = .{ .handler = std.posix.SIG.DFL },
45+ .mask = std.posix.sigemptyset(),
46+ .flags = 0,
47+ };
48+ std.posix.sigaction(sig, &act, null);
49+ std.posix.raise(sig) catch {};
50+ std.process.exit(1);
3351 }
3452
3553 pub fn writeOut(bytes: []const u8) void {
src/session.zig+3−3
@@ -221,7 +221,7 @@ pub const Session = struct {
221221
222222 fn ensure(self: *Session, title: []const u8) !void {
223223 if (self.file != null) return;
224- std.Io.Dir.cwd().createDirPath(platform.io, self.sessions_dir) catch {};
224+ _ = std.Io.Dir.cwd().createDirPathStatus(platform.io, self.sessions_dir, .fromMode(0o700)) catch {};
225225 var stamp_buf: [15]u8 = undefined;
226226 const stamp = time.stamp(&stamp_buf, time.nowMs());
227227
@@ -231,12 +231,12 @@ pub const Session = struct {
231231 shortId(&id_buf);
232232 const name = try std.fmt.allocPrint(self.a, "{s}-{s}-{s}", .{ stamp, title, &id_buf });
233233 const dir = try filesystem.join(self.a, &.{ self.sessions_dir, name });
234- std.Io.Dir.cwd().createDir(platform.io, dir, .default_dir) catch |e| switch (e) {
234+ std.Io.Dir.cwd().createDir(platform.io, dir, .fromMode(0o700)) catch |e| switch (e) {
235235 error.PathAlreadyExists => continue,
236236 else => return e,
237237 };
238238 const log_path = try filesystem.join(self.a, &.{ dir, "session.jsonl" });
239- const file = try std.Io.Dir.cwd().createFile(platform.io, log_path, .{ .truncate = false });
239+ const file = try std.Io.Dir.cwd().createFile(platform.io, log_path, .{ .truncate = false, .permissions = .fromMode(0o600) });
240240 self.id = name;
241241 self.file = file;
242242 var header_buf: std.Io.Writer.Allocating = .init(self.a);
src/tui/tui.zig+5−1
@@ -43,13 +43,15 @@ const Search = struct {
4343
4444 var resize_flag = std.atomic.Value(bool).init(false);
4545 var exit_flag = std.atomic.Value(bool).init(false);
46+var exit_signal = std.atomic.Value(u32).init(0);
4647
4748 fn onWinch(_: std.posix.SIG) callconv(.c) void {
4849 resize_flag.store(true, .seq_cst);
4950 }
5051
51-fn onExitSignal(_: std.posix.SIG) callconv(.c) void {
52+fn onExitSignal(sig: std.posix.SIG) callconv(.c) void {
5253 term.restore();
54+ exit_signal.store(@intFromEnum(sig), .seq_cst);
5355 exit_flag.store(true, .seq_cst);
5456 }
5557
@@ -1079,4 +1081,6 @@ pub fn run(opts: agent.Options, cfg: *const config.Config, tool_names: []const c
10791081 }
10801082 std.Io.sleep(platform.io, .{ .nanoseconds = 8 * std.time.ns_per_ms }, .boot) catch {};
10811083 }
1084+ const sig = exit_signal.load(.seq_cst);
1085+ if (sig != 0) platform.reRaise(@enumFromInt(sig));
10821086 }