Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
server: model discovery reports the model's reasoning efforts
A /v1/models entry for a model whose template has a thinking switch, names effort levels or has a think close token adds an OpenRouter-style reasoning: default_enabled, default_effort when the server was started with one, and supported_efforts from the template's own levels. Init reads the thinking switch with the levels, so discovery does not read the template per request.
  • Loading branch information
kelchm committed Oct 14, 2026
commit 061b2d2ef435f2b0ef9cdf868ff8cec65681f203
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ are the loaded engine's effective prompt-plus-reply token limit, including any s
they are not the default reply limit. An unspecified engine window omits both fields. Clients should
still handle per-request memory refusals: an advertised context is not a guarantee that every workload fits.

A model that thinks also carries an OpenRouter-style `reasoning`: `default_enabled`, `default_effort` (only
with `--reasoning-effort`), and `supported_efforts`, the levels its template names. These describe chat and
Responses requests.

## Serve flags

The read-only [`/memory` diagnostics](docs/memory-diagnostics.md) report process footprint and lifetime peak,
Expand Down
7 changes: 5 additions & 2 deletions zig/src/server/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub const Server = struct {
markers: reply_text.Markers = reply_text.think_markers,
chunks: chunk_plan.Plan = .{},
effort_levels: []const []const u8 = &.{},
thinking_switch: bool = false,
eos: []const u32 = &.{},
think_close: []const u32 = &.{},
think_close_end: ?u32 = null,
Expand Down Expand Up @@ -114,9 +115,11 @@ pub const Server = struct {
const a = srv.arena.allocator();
srv.eos = try a.dupe(u32, text.eosIds());
if (text.tokenId(reply_text.channel_markers.close) != null) srv.markers = reply_text.channel_markers;
srv.effort_levels = try fields.effortLevels(a, text.templateSource());
const source = text.templateSource();
srv.effort_levels = try fields.effortLevels(a, source);
srv.thinking_switch = std.mem.indexOf(u8, source, "enable_thinking") != null or std.mem.indexOf(u8, source, "thinking_mode") != null;
srv.late_system = try lateSystem(a, text);
srv.needs_user_after_tool = needsUserAfterTool(text.templateSource());
srv.needs_user_after_tool = needsUserAfterTool(source);
if (text.tokenId("</think>")) |end| {
const lead = try text.encode(a, "\n", false);
const trail = try text.encode(a, "\n\n", false);
Expand Down
36 changes: 36 additions & 0 deletions zig/src/server/server_init_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,39 @@ test "an init that fails after the keepalive started stops its thread and frees
try std.testing.expectEqual(counted.allocated_bytes, counted.freed_bytes);
try std.testing.expectEqual(counted.allocations, counted.deallocations);
}

/// A vocabulary without `</think>` whose template source counts its reads.
const SourceText = struct {
source: []const u8,
reads: u32 = 0,

fn text(t: *SourceText) model_text.Text {
return .{ .ctx = t, .vtable = &.{ .encode = BrokenText.encode, .decode = BrokenText.decode, .token_id = tokenId, .token_string = BrokenText.tokenString, .vocab_size = BrokenText.vocabSize, .eos_ids = BrokenText.eosIds, .render = BrokenText.render, .template_source = templateSource } };
}

fn tokenId(_: *anyopaque, _: []const u8) ?u32 {
return null;
}

fn templateSource(ctx: *anyopaque) []const u8 {
const t: *SourceText = @ptrCast(@alignCast(ctx));
t.reads += 1;
return t.source;
}
};

test "init reads the template once and keeps its thinking switch for model discovery" {
var queue: Queue = .{};
const config: server.Config = .{ .served_name = "m", .model_ids = &.{"m"} };
for ([_]struct { source: []const u8, switch_: bool }{
.{ .source = "{% if enable_thinking %}<think>{% endif %}", .switch_ = true },
.{ .source = "{% if thinking_mode == 'chat' %}{% endif %}", .switch_ = true },
.{ .source = "{{ messages }}", .switch_ = false },
}) |case| {
var text: SourceText = .{ .source = case.source };
const srv = try server.Server.init(std.testing.allocator, std.testing.io, queue.engine(), text.text(), config, null);
defer srv.deinit();
try std.testing.expectEqual(case.switch_, srv.thinking_switch);
try std.testing.expectEqual(@as(u32, 1), text.reads);
}
}
64 changes: 59 additions & 5 deletions zig/src/server/status_routes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ pub fn health(srv: *Server, conn: *Conn, a: Allocator) !void {
pub fn models(srv: *Server, conn: *Conn, a: Allocator) !void {
const created = std.Io.Clock.real.now(srv.io).toSeconds();
const data = try a.alloc(Value, srv.config.model_ids.len);
const thinks = srv.thinking_switch or srv.effort_levels.len > 0 or srv.think_close.len > 0;
const reasoning: ?Value = if (thinks) try reasoningValue(a, srv.config.enable_thinking, srv.thinking_switch, srv.effortFor(null), srv.effort_levels) else null;
for (srv.config.model_ids, data) |id, *slot| {
slot.* = try modelValue(a, id, srv.info, created);
slot.* = try modelValue(a, id, srv.info, created, reasoning);
}
const o = try json.newObject(a);
try o.put(a, "object", .{ .string = "list" });
Expand All @@ -47,7 +49,7 @@ pub fn models(srv: *Server, conn: *Conn, a: Allocator) !void {
}

/// Discovery extensions report the loaded engine's prompt-plus-reply limit; zero leaves it unspecified.
fn modelValue(a: Allocator, id: []const u8, info: api.Info, created: i64) Allocator.Error!Value {
fn modelValue(a: Allocator, id: []const u8, info: api.Info, created: i64, reasoning: ?Value) Allocator.Error!Value {
const m = try json.newObject(a);
try m.put(a, "id", .{ .string = id });
try m.put(a, "object", .{ .string = "model" });
Expand All @@ -57,9 +59,24 @@ fn modelValue(a: Allocator, id: []const u8, info: api.Info, created: i64) Alloca
try m.put(a, "context_length", try json.intValue(a, info.context_window));
try m.put(a, "max_model_len", try json.intValue(a, info.context_window));
}
if (reasoning) |r| try m.put(a, "reasoning", r);
return .{ .object = m };
}

/// /v1/models' ``reasoning``: the thinking default and the template's levels.
fn reasoningValue(a: Allocator, enable: bool, switchable: bool, default: ?[]const u8, levels: []const []const u8) Allocator.Error!Value {
const r = try json.newObject(a);
try r.put(a, "default_enabled", .{ .bool = enable });
if (default) |e| try r.put(a, "default_effort", .{ .string = e });
if (levels.len > 0) {
const named = try a.alloc(Value, levels.len + @intFromBool(switchable));
for (levels, named[0..levels.len]) |l, *slot| slot.* = .{ .string = l };
if (switchable) named[levels.len] = .{ .string = "none" };
try r.put(a, "supported_efforts", .{ .array = named });
}
return .{ .object = r };
}

/// Prometheus text, version 0.0.4.
pub fn metrics(srv: *Server, conn: *Conn, a: Allocator) void {
var out: std.Io.Writer.Allocating = .init(a);
Expand Down Expand Up @@ -127,7 +144,7 @@ test "model discovery reports fitted engine context equally for its served name
defer arena.deinit();
const info: api.Info = .{ .context_window = 8192, .context_fitted = true };
for ([_][]const u8{ "served-model", "model-alias" }) |id| {
const value = try modelValue(arena.allocator(), id, info, 123);
const value = try modelValue(arena.allocator(), id, info, 123, null);
try std.testing.expectEqualStrings(id, value.get("id").?.string);
try std.testing.expectEqual(@as(i64, 8192), value.get("context_length").?.int64().?);
try std.testing.expectEqual(value.get("context_length").?.int64(), value.get("max_model_len").?.int64());
Expand All @@ -138,13 +155,50 @@ test "model discovery reports fitted engine context equally for its served name
test "model discovery omits an unspecified context and preserves the full u32 limit" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const unlimited = try modelValue(arena.allocator(), "model", .{}, 0);
const unlimited = try modelValue(arena.allocator(), "model", .{}, 0, null);
try std.testing.expect(unlimited.get("context_length") == null);
try std.testing.expect(unlimited.get("max_model_len") == null);
const bounded = try modelValue(arena.allocator(), "model", .{ .context_window = std.math.maxInt(u32) }, 0);
const bounded = try modelValue(arena.allocator(), "model", .{ .context_window = std.math.maxInt(u32) }, 0, null);
try std.testing.expectEqual(@as(i64, std.math.maxInt(u32)), bounded.get("max_model_len").?.int64().?);
}

test "model discovery adds reasoning to an entry only when the model has it" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
const plain = try modelValue(a, "model", .{}, 0, null);
try std.testing.expect(plain.get("reasoning") == null);
const thinking = try modelValue(a, "model", .{}, 0, try reasoningValue(a, true, true, null, &.{"low"}));
try std.testing.expect(thinking.get("reasoning").?.get("default_enabled").?.bool);
}

test "model discovery lists the template's levels, and none only behind a thinking switch" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
const qwen = try reasoningValue(a, true, true, null, &.{ "xhigh", "medium", "low" });
const listed = qwen.get("supported_efforts").?.array;
try std.testing.expectEqual(@as(usize, 4), listed.len);
for ([_][]const u8{ "xhigh", "medium", "low", "none" }, listed) |name, v| try std.testing.expectEqualStrings(name, v.string);
const glm = try reasoningValue(a, true, false, null, &.{ "max", "high", "low" });
try std.testing.expectEqual(@as(usize, 3), glm.get("supported_efforts").?.array.len);
const bare = try reasoningValue(a, true, true, null, &.{});
try std.testing.expect(bare.get("supported_efforts") == null);
}

test "model discovery names a default effort only when the server was started with one" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
const levels = [_][]const u8{ "xhigh", "medium", "low" };
const plain = try reasoningValue(a, true, true, null, &levels);
try std.testing.expect(plain.get("default_effort") == null);
try std.testing.expect(plain.get("default_enabled").?.bool);
const started = try reasoningValue(a, false, true, "low", &levels);
try std.testing.expectEqualStrings("low", started.get("default_effort").?.string);
try std.testing.expect(!started.get("default_enabled").?.bool);
}

test "stats snapshot exposes counters and omits unknown memory" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
Expand Down
4 changes: 2 additions & 2 deletions zig/tests/server/golden/framing/chunked-bytes.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
],
[
"Content-Length",
"302"
"496"
]
],
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}]}",
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}]}",
"interim": ""
}
]
Expand Down
4 changes: 2 additions & 2 deletions zig/tests/server/golden/framing/connection-close.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
],
[
"Content-Length",
"302"
"496"
],
[
"Connection",
"close"
]
],
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}]}",
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}]}",
"interim": ""
}
]
Expand Down
4 changes: 2 additions & 2 deletions zig/tests/server/golden/framing/extensions-trailers.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
],
[
"Content-Length",
"302"
"496"
]
],
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}]}",
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}]}",
"interim": ""
}
]
Expand Down
4 changes: 2 additions & 2 deletions zig/tests/server/golden/framing/keepalive-three.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
],
[
"Content-Length",
"302"
"496"
]
],
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}]}",
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}]}",
"interim": ""
}
]
Expand Down
4 changes: 2 additions & 2 deletions zig/tests/server/golden/framing/pipelined.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
],
[
"Content-Length",
"302"
"496"
]
],
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}]}",
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}]}",
"interim": ""
}
]
Expand Down
4 changes: 2 additions & 2 deletions zig/tests/server/golden/keys/models-bare.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
],
[
"Content-Length",
"302"
"496"
]
],
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}]}",
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}]}",
"interim": ""
}
]
Expand Down
4 changes: 2 additions & 2 deletions zig/tests/server/golden/keys/models-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
],
[
"Content-Length",
"302"
"496"
]
],
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}]}",
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}]}",
"interim": ""
}
]
Expand Down
4 changes: 2 additions & 2 deletions zig/tests/server/golden/keys/models-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
],
[
"Content-Length",
"302"
"496"
]
],
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}]}",
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}]}",
"interim": ""
}
]
Expand Down
4 changes: 2 additions & 2 deletions zig/tests/server/golden/keys/models-file.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
],
[
"Content-Length",
"302"
"496"
]
],
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024}]}",
"body": "{\"object\": \"list\", \"data\": [{\"id\": \"fake-model\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}, {\"id\": \"fake-alias\", \"object\": \"model\", \"created\": <ts>, \"owned_by\": \"tensorfold\", \"context_length\": 1024, \"max_model_len\": 1024, \"reasoning\": {\"default_enabled\": true, \"supported_efforts\": [\"xhigh\", \"medium\", \"low\", \"none\"]}}]}",
"interim": ""
}
]
Expand Down
Loading