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
2 changes: 1 addition & 1 deletion sdk/tests/package-size-budgets.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"unpacked": 11647000
},
"fx-core.wasm": {
"bytes": 2465000
"bytes": 2475000
},
"fx-term.wasm": {
"bytes": 4254000
Expand Down
1 change: 1 addition & 0 deletions src/builtins/gateway.zig
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ fn streamAgentCompletion(
.transport => .transport,
.agent => .agent,
},
.conversation = .{ .messages = request.messages, .current_turn_messages = request.current_turn_messages },
.shared_pool = shared_pool,
};
const result = (if (request.deadline) |deadline|
Expand Down
2 changes: 2 additions & 0 deletions src/core/agent/runtime/orchestrator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8709,6 +8709,7 @@ fn processQueuedPromptLoop(
.model = gateway_model,
.instructions = gateway_instructions.items,
.messages = request_messages,
.current_turn_messages = within_turn_suffix.items,
.tools = .{
.registry = deps.tool_registry,
.advertised_names = config.advertised_tool_names,
Expand Down Expand Up @@ -9053,6 +9054,7 @@ fn processQueuedPromptLoop(
.retry_count = config.gateway_retry_count,
.instructions = request_data.instructions,
.messages = request_data.messages,
.current_turn_messages = request_data.current_turn_messages,
.tools = request_data.tools,
.tool_choice = request_data.tool_choice,
.vision_mode = request_data.vision_mode,
Expand Down
43 changes: 43 additions & 0 deletions src/core/agent/runtime/tests/tool_flow.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5066,6 +5066,49 @@ test "tool results are stamped as they enter the turn, and later requests and th
try std.testing.expectEqual(write_stamp, steps[1].tool_results[0].created_at_ms);
}

test "every model request carries this turn's calls so far, which a reply may not reuse" {
const Seen = struct {
const ModelRequest = @import("../../stream_provider.zig").ModelRequest;
var turn_call_ids: [3]std.ArrayList(u8) = @splat(.empty);
var requests: usize = 0;

fn observe(request: ModelRequest) !void {
if (requests >= turn_call_ids.len) return error.TestUnexpectedRequest;
for (request.current_turn_messages) |message| {
if (message.role != .assistant) continue;
for (message.tool_calls) |call| try turn_call_ids[requests].print(std.testing.allocator, "{s};", .{call.id});
}
requests += 1;
}
};
Seen.requests = 0;
defer for (&Seen.turn_call_ids) |*ids| ids.deinit(std.testing.allocator);
const alloc = std.testing.allocator;
const first_calls = [_]ToolCall{toolCall("call_one", "read_file", "{\"path\":\"a.txt\"}")};
const second_calls = [_]ToolCall{toolCall("call_two", "read_file", "{\"path\":\"b.txt\"}")};
const completions = [_]FakeCompletion{
.{ .tool_calls = &first_calls },
.{ .tool_calls = &second_calls },
.{ .content = "Done" },
};
var gateway = FakeGateway.init(alloc, &completions);
defer gateway.deinit();
gateway.observe_request = Seen.observe;
var hooks = FakeAgentRuntimeDeps.init(alloc);
defer hooks.deinit();
hooks.exec_plans = &.{
.{ .result = .{ .model_output = "a" } },
.{ .result = .{ .model_output = "b" } },
};
var fixture = PromptFixture{};
try runFakePrompt(&gateway, &hooks, fixture.config(), fixture.job());

try std.testing.expectEqual(@as(usize, 3), Seen.requests);
try std.testing.expectEqualStrings("", Seen.turn_call_ids[0].items);
try std.testing.expectEqualStrings("call_one;", Seen.turn_call_ids[1].items);
try std.testing.expectEqualStrings("call_one;call_two;", Seen.turn_call_ids[2].items);
}

test "terminal publication failure deletes retained command replay" {
const alloc = std.testing.allocator;
var tmp = std.testing.tmpDir(.{});
Expand Down
9 changes: 9 additions & 0 deletions src/core/agent/stream_provider.zig
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ pub const RequestData = struct {
model: []const u8,
instructions: []const types.ChatMessage = &.{},
messages: []const types.ChatMessage,
/// This turn's messages so far, borrowed. Compaction can take some of
/// them out of `messages`; a reply's calls still may not take their ids
/// (`src/gateway/tool_call_ids.zig` ReplyIds).
current_turn_messages: []const types.ChatMessage = &.{},
tools: ToolSelection = .{},
tool_choice: types.ToolChoice,
vision_mode: VisionMode = .unavailable,
Expand Down Expand Up @@ -436,6 +440,10 @@ pub const ModelRequest = struct {
retry_count: usize,
instructions: []const types.ChatMessage = &.{},
messages: []const types.ChatMessage,
/// This turn's messages so far, borrowed. Compaction can take some of
/// them out of `messages`; a reply's calls still may not take their ids
/// (`src/gateway/tool_call_ids.zig` ReplyIds).
current_turn_messages: []const types.ChatMessage = &.{},
tools: ToolSelection = .{},
tool_choice: types.ToolChoice,
vision_mode: VisionMode = .unavailable,
Expand Down Expand Up @@ -465,6 +473,7 @@ pub const ModelRequest = struct {
.model = self.model,
.instructions = self.instructions,
.messages = self.messages,
.current_turn_messages = self.current_turn_messages,
.tools = self.tools,
.tool_choice = self.tool_choice,
.vision_mode = self.vision_mode,
Expand Down
7 changes: 6 additions & 1 deletion src/core/shared/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2510,6 +2510,11 @@ pub const ConversationIdentity = struct {
if (!std.unicode.utf8ValidateSlice(value)) return .invalid_utf8;
return null;
}

/// A call id of only spaces, tabs, and line ends names no call.
pub fn isBlank(value: []const u8) bool {
return std.mem.trim(u8, value, " \t\r\n").len == 0;
}
};

pub const AuthoritativeToolAdmission = union(enum) {
Expand All @@ -2530,7 +2535,7 @@ pub fn authoritativeToolAdmission(completion: ModelCompletion) AuthoritativeTool
}

for (completion.tool_calls) |call| {
const final_identity = if (call.final_identity == .valid and std.mem.trim(u8, call.id, " \t\r\n").len == 0)
const final_identity = if (call.final_identity == .valid and ConversationIdentity.isBlank(call.id))
FinalToolIdentity.empty
else
call.final_identity;
Expand Down
1 change: 1 addition & 0 deletions src/fuzz_targets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ test {
_ = @import("gateway/openai_codex.zig");
_ = @import("gateway/responses_protocol.zig");
_ = @import("gateway/sse.zig");
_ = @import("gateway/tool_call_ids.zig");
_ = @import("main.zig");
_ = @import("tools/agent/ask_user_question.zig");
_ = @import("tools/capabilities/toolbox.zig");
Expand Down
127 changes: 115 additions & 12 deletions src/gateway/anthropic_messages_protocol.zig
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,10 @@ const Block = struct {
};

const Tool = struct {
/// The id the call keeps in fx (`tool_call_ids.ReplyIds`).
id: ?[]u8 = null,
/// The id the server sent, when it sent one fx could read.
source_id: ?[]u8 = null,
name: ?[]u8 = null,
arguments: std.ArrayList(u8) = .empty,
/// The block's starting `input` as JSON, when it was not empty.
Expand All @@ -498,6 +501,7 @@ const Tool = struct {

fn deinit(self: *Tool, alloc: Allocator) void {
if (self.id) |id| alloc.free(id);
if (self.source_id) |id| alloc.free(id);
if (self.name) |name| alloc.free(name);
self.arguments.deinit(alloc);
if (self.start_input) |input| alloc.free(input);
Expand Down Expand Up @@ -551,6 +555,7 @@ const Reducer = struct {
limits: Limits,
choice: types.ToolChoice,
structured_name: ?[]const u8,
call_ids: tool_call_ids.ReplyIds,
blocks: std.ArrayList(Block) = .empty,
tools: std.ArrayList(Tool) = .empty,
thinking: std.ArrayList(Thinking) = .empty,
Expand All @@ -573,10 +578,12 @@ const Reducer = struct {
.limits = limits,
.choice = request.tool_choice,
.structured_name = if (request.response_format) |format| format.name else null,
.call_ids = try tool_call_ids.ReplyIds.init(alloc, .{ .messages = request.messages, .current_turn_messages = request.current_turn_messages }),
};
}

fn deinit(self: *Reducer) void {
self.call_ids.deinit();
self.blocks.deinit(self.alloc);
for (self.tools.items) |*tool| tool.deinit(self.alloc);
self.tools.deinit(self.alloc);
Expand Down Expand Up @@ -687,20 +694,19 @@ const Reducer = struct {
}

fn start_tool(self: *Reducer, block: std.json.ObjectMap) Error!Deltas {
const id = try string(block.get("id") orelse return error.InvalidEvent);
const source_id = try self.tool_source_id(non_null(block, "id"));
const name = try string(block.get("name") orelse return error.InvalidEvent);
if (id.len == 0) return error.InvalidEvent;
if (id.len > self.limits.identity_bytes or name.len > @min(max_name_bytes, self.limits.identity_bytes)) return error.IdentityTooLarge;
if (name.len > @min(max_name_bytes, self.limits.identity_bytes)) return error.IdentityTooLarge;
const structured = if (self.structured_name) |expected| std.mem.eql(u8, expected, name) else false;
// A structured request sends only its own tool, once; a request that
// allows no tools sends none.
if (self.structured_name != null and (!structured or self.tools.items.len != 0)) return error.UnexpectedToolCall;
if (self.choice == .none and !structured) return error.UnexpectedToolCall;
if (self.tools.items.len == self.limits.tool_calls) return error.TooManyTools;
for (self.tools.items) |prior| if (std.mem.eql(u8, prior.id.?, id)) return error.ConflictingIdentity;
var tool: Tool = .{ .structured = structured };
errdefer tool.deinit(self.alloc);
tool.id = try self.alloc.dupe(u8, id);
tool.id = try self.alloc.dupe(u8, try self.call_ids.assign(source_id));
if (source_id) |id| tool.source_id = try self.alloc.dupe(u8, id);
tool.name = try self.alloc.dupe(u8, name);
if (block.get("input")) |input| {
if (input != .object) return error.InvalidEvent;
Expand All @@ -721,6 +727,28 @@ const Reducer = struct {
return .{ .tool_started = .{ .id = stored.id.?, .name = stored.name.? } };
}

/// The id the server gave a call, or null when it gave none fx can keep:
/// then the call gets an fx id (`tool_call_ids.ReplyIds`).
fn tool_source_id(self: *const Reducer, value: ?std.json.Value) Error!?[]const u8 {
const text = try string(value orelse return null);
if (text.len == 0 or text.len > self.limits.identity_bytes) return null;
return text;
}

/// Gives a call that repeats a call from an earlier reply, the same id,
/// tool, and arguments, that id back (`tool_call_ids.ReplyIds.sameCallAgain`).
/// Returns the id fx gave it at its start, which its live start named,
/// as its provisional id; the caller owns it.
fn restore_sent_again(self: *Reducer, tool: *Tool, arguments: []const u8) Error!?[]u8 {
const source = tool.source_id orelse return null;
const started = tool.id.?;
if (std.mem.eql(u8, source, started)) return null;
if (!try self.call_ids.sameCallAgain(source, tool.name.?, arguments)) return null;
tool.id = source;
tool.source_id = null;
return started;
}

fn accept_block_delta(self: *Reducer, root: std.json.ObjectMap) Error!Deltas {
const block = (try self.open_block(root)).*;
const delta = try object(root.get("delta") orelse return error.InvalidEvent);
Expand Down Expand Up @@ -895,11 +923,7 @@ const Reducer = struct {
errdefer if (content) |text| self.alloc.free(text);
var calls: std.ArrayList(types.ToolCall) = .empty;
errdefer {
for (calls.items) |call| {
self.alloc.free(call.id);
self.alloc.free(call.name);
self.alloc.free(call.arguments_json);
}
for (calls.items) |call| types.freeToolCall(self.alloc, call);
calls.deinit(self.alloc);
}
try calls.ensureTotalCapacityPrecise(self.alloc, self.tools.items.len);
Expand All @@ -911,7 +935,9 @@ const Reducer = struct {
content = arguments;
continue;
}
calls.appendAssumeCapacity(.{ .id = tool.id.?, .name = tool.name.?, .arguments_json = arguments });
errdefer self.alloc.free(arguments);
const provisional_id = try self.restore_sent_again(tool, arguments);
calls.appendAssumeCapacity(.{ .id = tool.id.?, .name = tool.name.?, .arguments_json = arguments, .provisional_id = provisional_id });
tool.id = null;
tool.name = null;
}
Expand Down Expand Up @@ -1821,6 +1847,84 @@ test "anthropic messages returns malformed tool arguments as sent" {
try std.testing.expectEqualStrings("{\"path\":\"y\"}", whole.completed.completion.tool_calls[0].arguments_json);
}

fn test_tool_block(comptime index: []const u8, comptime id_field: []const u8) []const u8 {
return "{\"type\":\"content_block_start\",\"index\":" ++ index ++ ",\"content_block\":{\"type\":\"tool_use\"" ++ id_field ++ ",\"name\":\"read_file\",\"input\":{\"path\":\"x\"}}}";
}

fn test_block_stop(comptime index: []const u8) []const u8 {
return "{\"type\":\"content_block_stop\",\"index\":" ++ index ++ "}";
}

test "anthropic messages gives a call the id an earlier step used an fx id" {
const alloc = std.testing.allocator;
var request = test_tool_request();
const earlier = [_]types.ToolCall{.{ .id = "toolu_1", .name = "read_file", .arguments_json = "{}" }};
request.messages = &.{
.{ .role = .user, .content = "hi" },
.{ .role = .assistant, .tool_calls = &earlier },
.{ .role = .tool, .tool_call_id = "toolu_1", .tool_name = "read_file", .content = "read" },
};
var recorder: Recorder = .{ .alloc = alloc };
defer recorder.deinit();
var result = try test_consume(alloc, test_tool_wire, request, recorder.sink());
defer result.deinit(alloc);
const completion = result.completed.completion;
try std.testing.expectEqualStrings("fx_call_1", completion.tool_calls[0].id);
try std.testing.expectEqualStrings("fx_call_1:read_file;", recorder.started.items);
try std.testing.expectEqual(types.AuthoritativeToolAdmission.admitted, types.authoritativeToolAdmission(completion));
}

test "anthropic messages keeps the id of a call sent again with an earlier call's id, tool, and arguments" {
const alloc = std.testing.allocator;
var request = test_tool_request();
const earlier = [_]types.ToolCall{.{ .id = "toolu_1", .name = "read_file", .arguments_json = "{\"path\":\"x\"}" }};
request.messages = &.{
.{ .role = .user, .content = "hi" },
.{ .role = .assistant, .tool_calls = &earlier },
.{ .role = .tool, .tool_call_id = "toolu_1", .tool_name = "read_file", .content = "read" },
};
var recorder: Recorder = .{ .alloc = alloc };
defer recorder.deinit();
var result = try test_consume(alloc, test_tool_wire, request, recorder.sink());
defer result.deinit(alloc);
const call = result.completed.completion.tool_calls[0];
try std.testing.expectEqualStrings("toolu_1", call.id);
// The live start named the id fx gave it when the block began.
try std.testing.expectEqualStrings("fx_call_1:read_file;", recorder.started.items);
try std.testing.expectEqualStrings("fx_call_1", call.provisional_id.?);
try std.testing.expectEqual(types.AuthoritativeToolAdmission.admitted, types.authoritativeToolAdmission(result.completed.completion));
}

test "anthropic messages gives repeated, missing, empty, and oversized call ids fx ids" {
const alloc = std.testing.allocator;
const wire = comptime sse_wire(&.{
test_start,
test_tool_block("0", ",\"id\":\"toolu_1\""),
test_block_stop("0"),
test_tool_block("1", ",\"id\":\"toolu_1\""),
test_block_stop("1"),
test_tool_block("2", ""),
test_block_stop("2"),
test_tool_block("3", ",\"id\":\"\""),
test_block_stop("3"),
test_tool_block("4", ",\"id\":\"" ++ text_utils.repeat("i", 257) ++ "\""),
test_block_stop("4"),
test_tool_use_stop,
test_message_stop,
});
var recorder: Recorder = .{ .alloc = alloc };
defer recorder.deinit();
var result = try test_consume(alloc, wire, test_tool_request(), recorder.sink());
defer result.deinit(alloc);
const completion = result.completed.completion;
var ids: std.ArrayList(u8) = .empty;
defer ids.deinit(alloc);
for (completion.tool_calls) |call| try ids.print(alloc, "{s}:{s};", .{ call.id, call.name });
try std.testing.expectEqualStrings("toolu_1:read_file;fx_call_1:read_file;fx_call_2:read_file;fx_call_3:read_file;fx_call_4:read_file;", ids.items);
try std.testing.expectEqualStrings(ids.items, recorder.started.items);
try std.testing.expectEqual(types.AuthoritativeToolAdmission.admitted, types.authoritativeToolAdmission(completion));
}

test "anthropic messages ignores unknown events blocks and deltas but not misplaced ones" {
const alloc = std.testing.allocator;
const wire = sse_wire(&.{
Expand Down Expand Up @@ -1862,7 +1966,6 @@ test "anthropic messages ignores unknown events blocks and deltas but not mispla
.{ .events = &.{ test_start, test_tool_start, test_text_delta }, .failure = error.InvalidEvent },
.{ .events = &.{ test_start, "{\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}" }, .failure = error.InvalidEvent },
.{ .events = &.{ test_start, test_end_turn, "{\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"max_tokens\"}}" }, .failure = error.InconsistentFinishReason },
.{ .events = &.{ test_start, test_tool_start, "{\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_1\",\"name\":\"shell\",\"input\":{}}}" }, .failure = error.ConflictingIdentity },
}) |case| {
const bad = try test_wire(alloc, case.events);
defer alloc.free(bad);
Expand Down
Loading
Loading