xonecas.io
79113
Ma

mini-coding-agent / commit efa156e

fix: adaptive budget reasoning for ant models

xonecas2026-10-09 efa156e857abfc4e9f4deca5ec654bfb558ac1ac parent 2baef73

12 files changed

feedback.md+0−14
@@ -1,14 +0,0 @@
1-# Code/memory quality notes
2-
3-- No leak detection: `gpa` is never `deinit`'d, so `DebugAllocator` leak reports never fire. Leaks would be silent.
4-- **libc `regcomp` into a `[512]u8` opaque buffer** for tree-sitter `match?` predicates — clever but fragile and ABI-shaped.
5-- `tools.json` / `filesystem.listDir` have deeply nested `catch return out.written()` / `catch &.{}` chains — hard to read.
6-- `platform.printErr` heap-allocates per message instead of a stack `[512]u8` buffer.
7-
8-# Bugs
9-
10-`opencode-go/claude-haiku-5-5 · low` fails to stream:
11-
12-```json
13-{"type":"error","error":{"type":"invalid_request_error","message":"Upstream request failed: [invalid_request_error] \"thinking.type.enabled\" is not supported for this model. Use \"thinking.type.adaptive\" and \"output_config.effort\" to control thinking behavior."}}
14-```
src/api/anthropic_messages.zig+11−2
@@ -89,7 +89,8 @@ const Message = union(enum) {
8989 }
9090 };
9191
92-const Thinking = struct { type: []const u8 = "enabled", budget_tokens: u64 };
92+const Thinking = struct { type: []const u8, budget_tokens: ?u64 = null };
93+const OutputConfig = struct { effort: []const u8 };
9394
9495 const Body = struct {
9596 model: []const u8,
@@ -97,6 +98,7 @@ const Body = struct {
9798 stream: bool = true,
9899 system: ?[]const u8 = null,
99100 thinking: ?Thinking = null,
101+ output_config: ?OutputConfig = null,
100102 messages: []const Message,
101103 tools: ?api.Tools = null,
102104 };
@@ -186,11 +188,18 @@ fn buildBody(req: api.Request) ![]u8 {
186188 const budget = budgetFor(req.effort);
187189 var max_tokens: u64 = if (req.model.max_tokens > 0) req.model.max_tokens else 8192;
188190 if (budget > 0 and max_tokens <= budget) max_tokens = budget + 4096;
191+ const adaptive = req.model.thinking == .effort and req.effort.len > 0 and budget > 0;
189192 const body = Body{
190193 .model = req.model.id,
191194 .max_tokens = max_tokens,
192195 .system = if (req.system_prompt.len > 0) req.system_prompt else null,
193- .thinking = if (budget > 0) .{ .budget_tokens = budget } else null,
196+ .thinking = if (adaptive)
197+ Thinking{ .type = "adaptive" }
198+ else if (budget > 0)
199+ Thinking{ .type = "enabled", .budget_tokens = budget }
200+ else
201+ null,
202+ .output_config = if (adaptive) OutputConfig{ .effort = req.effort } else null,
194203 .messages = try buildMessages(a, req),
195204 .tools = if (req.tools_json.len > 0) .{ .a = a, .json = req.tools_json, .form = .{
196205 .open = "[",
src/filesystem.zig+7−7
@@ -23,14 +23,14 @@ pub fn fileExists(path: []const u8) bool {
2323 return true;
2424 }
2525
26-pub fn listDir(a: std.mem.Allocator, root: []const u8) [][]u8 {
27- var out: std.ArrayList([]u8) = .empty;
28- const dir = std.Io.Dir.cwd().openDir(platform.io, root, .{ .iterate = true }) catch return &.{};
26+pub fn listDir(a: std.mem.Allocator, root: []const u8) ![]const []const u8 {
27+ var dir = try std.Io.Dir.cwd().openDir(platform.io, root, .{ .iterate = true });
2928 defer dir.close(platform.io);
29+ var out: std.ArrayList([]const u8) = .empty;
30+ errdefer out.deinit(a);
3031 var it = dir.iterate();
31- while (it.next(platform.io) catch return out.toOwnedSlice(a) catch &.{}) |entry| {
32- out.append(a, a.dupe(u8, entry.name) catch return out.toOwnedSlice(a) catch &.{}) catch
33- return out.toOwnedSlice(a) catch &.{};
32+ while (try it.next(platform.io)) |entry| {
33+ try out.append(a, try a.dupe(u8, entry.name));
3434 }
35- return out.toOwnedSlice(a) catch &.{};
35+ return out.toOwnedSlice(a);
3636 }
src/models.zig+8−0
@@ -62,6 +62,13 @@ fn clampEffort(reasoning: bool, effort: []const []const u8, desired: ?[]const u8
6262 return normalizeEffort(pick);
6363 }
6464
65+fn thinkingStyle(info: modelsdev.Info) types.Thinking {
66+ if (!info.reasoning) return .off;
67+ if (info.budget_tokens) return .budget;
68+ if (info.effort.len > 0) return .effort;
69+ return .budget;
70+}
71+
6572 fn builtin(id: []const u8) ?*const Builtin {
6673 for (&builtins) |*b| {
6774 if (std.mem.eql(u8, b.id, id)) return b;
@@ -112,6 +119,7 @@ pub fn resolveNamed(a: std.mem.Allocator, cfg: *const config.Config, provider_id
112119 .supports_images = info.images,
113120 .context_window = info.context,
114121 .max_tokens = info.max_output,
122+ .thinking = thinkingStyle(info),
115123 .cost_input = info.cost_input,
116124 .cost_output = info.cost_output,
117125 .cost_cache_read = info.cost_cache_read,
src/models_dev.zig+9−0
@@ -15,6 +15,7 @@ pub const Info = struct {
1515 context: u64,
1616 max_output: u64,
1717 effort: []const []const u8,
18+ budget_tokens: bool,
1819 cost_input: f64,
1920 cost_output: f64,
2021 cost_cache_read: f64,
@@ -85,6 +86,7 @@ pub const Db = struct {
8586 .context = m.limit.context,
8687 .max_output = m.limit.output,
8788 .effort = effortOf(m),
89+ .budget_tokens = hasOption(m, "budget_tokens"),
8890 .cost_input = m.cost.input,
8991 .cost_output = m.cost.output,
9092 .cost_cache_read = m.cost.cache_read,
@@ -98,3 +100,10 @@ fn effortOf(m: Model) []const []const u8 {
98100 }
99101 return &.{};
100102 }
103+
104+fn hasOption(m: Model, kind: []const u8) bool {
105+ for (m.reasoning_options) |opt| {
106+ if (std.mem.eql(u8, opt.type, kind)) return true;
107+ }
108+ return false;
109+}
src/platform.zig+7−2
@@ -41,7 +41,12 @@ pub fn writeErr(bytes: []const u8) void {
4141 }
4242
4343 pub fn printErr(comptime fmt: []const u8, fmt_args: anytype) void {
44- const s = std.fmt.allocPrint(gpa, fmt, fmt_args) catch return;
45- defer gpa.free(s);
44+ var buf: [512]u8 = undefined;
45+ const s = std.fmt.bufPrint(&buf, fmt, fmt_args) catch {
46+ const heap = std.fmt.allocPrint(gpa, fmt, fmt_args) catch return;
47+ defer gpa.free(heap);
48+ writeErr(heap);
49+ return;
50+ };
4651 writeErr(s);
4752 }
src/prompt.zig+1−1
@@ -27,7 +27,7 @@ fn frontmatter(text: []const u8) Frontmatter {
2727 fn discoverSkills(a: std.mem.Allocator, dirs: []const []const u8) ![]Skill {
2828 var skills: std.ArrayList(Skill) = .empty;
2929 for (dirs) |root| {
30- for (filesystem.listDir(a, root)) |entry| {
30+ for (filesystem.listDir(a, root) catch continue) |entry| {
3131 const path = filesystem.join(a, &.{ root, entry, "SKILL.md" }) catch continue;
3232 if (!filesystem.fileExists(path)) continue;
3333 const text = filesystem.readFileAlloc(a, path, 1 << 20) catch continue;
src/text.zig+6−0
@@ -1,5 +1,11 @@
11 const std = @import("std");
22
3+const binary_sniff = 8192;
4+
5+pub fn isBinary(bytes: []const u8) bool {
6+ return std.mem.indexOfScalar(u8, bytes[0..@min(bytes.len, binary_sniff)], 0) != null;
7+}
8+
39 fn utf8SeqLen(p: []const u8) ?usize {
410 if (p.len == 0) return null;
511 const c = p[0];
src/tools.zig+9−6
@@ -125,15 +125,18 @@ pub fn execute(a: std.mem.Allocator, scratch: std.mem.Allocator, name: []const u
125125 pub fn json(a: std.mem.Allocator, names: []const config.ToolName, with_images: bool, cwd: []const u8) []const u8 {
126126 const ctx = Describe{ .with_images = with_images, .cwd = cwd };
127127 var out: std.Io.Writer.Allocating = .init(a);
128- const w = &out.writer;
129- w.writeByte('[') catch return "[]";
128+ writeJson(&out.writer, a, names, ctx) catch return "[]";
129+ return out.written();
130+}
131+
132+fn writeJson(w: *std.Io.Writer, a: std.mem.Allocator, names: []const config.ToolName, ctx: Describe) !void {
133+ try w.writeByte('[');
130134 var first = true;
131135 for (names) |name| {
132136 const tool = byName(name) orelse continue;
133- if (!first) w.writeByte(',') catch return out.written();
137+ if (!first) try w.writeByte(',');
134138 first = false;
135- write(w, a, tool, ctx) catch return out.written();
139+ try write(w, a, tool, ctx);
136140 }
137- w.writeByte(']') catch return out.written();
138- return out.written();
141+ try w.writeByte(']');
139142 }
src/tools/read.zig+1−2
@@ -134,8 +134,7 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
134134 };
135135 }
136136
137- const head = data[0..@min(data.len, 8000)];
138- if (std.mem.indexOfScalar(u8, head, 0) != null) {
137+ if (text.isBinary(data)) {
139138 return fail(a, "read failed: {s} is binary; use bash (file, xxd)", .{path});
140139 }
141140
src/tools/snapshot.zig+49−15
@@ -1,12 +1,12 @@
11 const std = @import("std");
22 const platform = @import("../platform.zig");
33 const filesystem = @import("../filesystem.zig");
4+const text = @import("../text.zig");
45 const diff = @import("../diff.zig");
56 const tools = @import("../tools.zig");
67
78 const max_file_bytes = 1 << 20;
89 const max_total_bytes = 32 << 20;
9-const binary_sniff = 8192;
1010
1111 const Kind = enum { text, binary, large, link, untracked };
1212
@@ -25,29 +25,64 @@ pub const Ignore = struct {
2525 uses_gitignore: bool = true,
2626 };
2727
28-fn normalizePattern(raw: []const u8) ?[]const u8 {
28+const Pattern = struct { text: []const u8, anchored: bool };
29+
30+fn normalizePattern(raw: []const u8) ?Pattern {
2931 var line = std.mem.trim(u8, raw, " \t\r");
3032 if (line.len == 0 or line[0] == '#') return null;
3133 if (std.mem.startsWith(u8, line, "./")) line = line[2..];
34+ const anchored = line.len > 0 and line[0] == '/';
35+ if (anchored) line = line[1..];
3236 line = std.mem.trimEnd(u8, line, "/");
33- return if (line.len == 0) null else line;
37+ return if (line.len == 0) null else .{ .text = line, .anchored = anchored };
3438 }
3539
36-fn ignoreSet(tmp: std.mem.Allocator, ignore: Ignore) !std.StringHashMap(void) {
37- var set = std.StringHashMap(void).init(tmp);
40+fn ignorePatterns(tmp: std.mem.Allocator, ignore: Ignore) ![]const Pattern {
3841 var list: std.ArrayList([]const u8) = .empty;
3942 try list.appendSlice(tmp, ignore.dirs);
4043 if (ignore.uses_gitignore) {
41- if (filesystem.readFileAlloc(tmp, ".gitignore", 1 << 20)) |text| {
42- var it = std.mem.tokenizeScalar(u8, text, '\n');
44+ if (filesystem.readFileAlloc(tmp, ".gitignore", 1 << 20)) |gitignore| {
45+ var it = std.mem.tokenizeScalar(u8, gitignore, '\n');
4346 while (it.next()) |line| try list.append(tmp, line);
4447 } else |_| {}
4548 }
46- for (list.items) |dir| {
47- const name = normalizePattern(dir) orelse continue;
48- try set.put(name, {});
49+ var out: std.ArrayList(Pattern) = .empty;
50+ for (list.items) |line| {
51+ if (normalizePattern(line)) |p| try out.append(tmp, p);
52+ }
53+ return out.items;
54+}
55+
56+fn glob(pat: []const u8, str: []const u8) bool {
57+ var p: usize = 0;
58+ var s: usize = 0;
59+ var star: ?usize = null;
60+ var star_s: usize = 0;
61+ while (s < str.len) {
62+ if (p < pat.len and (pat[p] == '?' or pat[p] == str[s])) {
63+ p += 1;
64+ s += 1;
65+ } else if (p < pat.len and pat[p] == '*') {
66+ star = p;
67+ star_s = s;
68+ p += 1;
69+ } else if (star) |sp| {
70+ star_s += 1;
71+ s = star_s;
72+ p = sp + 1;
73+ } else return false;
74+ }
75+ while (p < pat.len and pat[p] == '*') p += 1;
76+ return p == pat.len;
77+}
78+
79+fn ignored(patterns: []const Pattern, rel: []const u8) bool {
80+ const name = std.fs.path.basename(rel);
81+ for (patterns) |p| {
82+ const subject = if (p.anchored or std.mem.indexOfScalar(u8, p.text, '/') != null) rel else name;
83+ if (glob(p.text, subject)) return true;
4984 }
50- return set;
85+ return false;
5186 }
5287
5388 pub fn capture(a: std.mem.Allocator, ignore: Ignore) !Tree {
@@ -57,7 +92,7 @@ pub fn capture(a: std.mem.Allocator, ignore: Ignore) !Tree {
5792 defer arena.deinit();
5893 const tmp = arena.allocator();
5994
60- const ignores = try ignoreSet(tmp, ignore);
95+ const ignores = try ignorePatterns(tmp, ignore);
6196
6297 var stack: std.ArrayList([]const u8) = .empty;
6398 try stack.append(tmp, "");
@@ -76,7 +111,7 @@ pub fn capture(a: std.mem.Allocator, ignore: Ignore) !Tree {
76111 try std.fmt.allocPrint(a, "{s}/{s}", .{ dir_rel, entry.name });
77112
78113 if (entry.kind == .directory) {
79- if (!ignores.contains(entry.name)) try stack.append(tmp, rel);
114+ if (!ignored(ignores, rel)) try stack.append(tmp, rel);
80115 continue;
81116 }
82117
@@ -104,8 +139,7 @@ pub fn capture(a: std.mem.Allocator, ignore: Ignore) !Tree {
104139 try tree.put(rel, state);
105140 continue;
106141 };
107- const sniff = @min(buf.len, binary_sniff);
108- if (std.mem.indexOfScalar(u8, buf[0..sniff], 0) != null) {
142+ if (text.isBinary(buf)) {
109143 state.kind = .binary;
110144 } else {
111145 state.kind = .text;
src/types.zig+3−0
@@ -106,6 +106,8 @@ pub fn assistantText(a: std.mem.Allocator, msg: *const AssistantMessage) ![]cons
106106 return out.items;
107107 }
108108
109+pub const Thinking = enum { off, budget, effort };
110+
109111 pub const Model = struct {
110112 id: []const u8,
111113 name: []const u8,
@@ -122,4 +124,5 @@ pub const Model = struct {
122124 cost_cache_read: f64,
123125 session_header: ?[]const u8,
124126 headers: []const [2][]const u8 = &.{},
127+ thinking: Thinking = .budget,
125128 };