summaryrefslogtreecommitdiff
path: root/libpanto/src/agent.zig
blob: a42c16ca67237b718bc340bbdf671c1c76fe5e40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
//! The Agent owns the conversation-driving loop: provider streaming +
//! tool dispatch.
//!
//! On each turn, after the provider streams an assistant message, the
//! agent inspects it for ToolUse blocks. If any are present, the agent:
//!
//!   1. Groups them by their *owning registration* in the registry — a
//!      single `Tool` is its own group; every `ToolSource`-backed tool
//!      whose name maps to the same source forms one group.
//!   2. Spawns one concurrent task per group via `std.Io.Group`.
//!      A single-`Tool` group runs the tool's `invoke` once; a
//!      `ToolSource` group calls the source's `invoke_batch` with all
//!      of its calls at once. We use `Group.concurrent` (not `async`)
//!      because tool invocations may block on I/O and we need real
//!      concurrency, not just expressed asynchrony.
//!   3. Awaits the group. ToolResult blocks are assembled in the
//!      *original* call order (i.e. the order the LLM emitted them).
//!   4. Appends a user message containing the ToolResult blocks back
//!      into the conversation and loops.
//!
//! The "thread-safe" promise for single `Tool` registrations is
//! unchanged. For `ToolSource`-backed tools, the source's runtime
//! receives all of its calls on one thread per turn, so it can keep a
//! single-threaded interpreter (Lua, Python, ...) without further
//! synchronization.

const std = @import("std");
const Allocator = std.mem.Allocator;
const Io = std.Io;

const provider_mod = @import("provider.zig");
const config_mod = @import("config.zig");
const conversation = @import("conversation.zig");
const tool_mod = @import("tool.zig");
const tool_source_mod = @import("tool_source.zig");
const tool_registry_mod = @import("tool_registry.zig");

pub const Tool = tool_mod.Tool;
pub const ToolSource = tool_source_mod.ToolSource;
pub const ToolRegistry = tool_registry_mod.ToolRegistry;

const Entry = tool_registry_mod.Entry;

pub const Config = config_mod.Config;

fn isValidToolInput(input: []const u8) bool {
    if (input.len == 0) return true;
    if (input[0] != '{') return true; // legacy tests/tools may use opaque bytes
    var parsed = std.json.parseFromSlice(std.json.Value, std.heap.page_allocator, input, .{}) catch return false;
    defer parsed.deinit();
    return parsed.value == .object;
}

fn invalidInputResult(allocator: Allocator, input: []const u8) ![]u8 {
    return std.fmt.allocPrint(
        allocator,
        "Tool call was not executed: tool input was incomplete or invalid JSON. Partial input: {s}",
        .{input},
    );
}


pub const Agent = struct {
    allocator: Allocator,
    io: Io,
    /// The active configuration snapshot, consulted fresh at the top of
    /// every turn. Immutable while a turn is in flight; swap this pointer
    /// (`setConfig`) between turns to change provider/model/base_url and/or
    /// the visible tool set atomically. The pointee and its registry are
    /// owned by the embedder, not the agent.
    config: *const Config,
    /// Injectable streaming seam. Defaults to the real provider dispatch
    /// (`provider_mod.streamStep`); tests override it with a stub.
    stream_fn: provider_mod.StreamFn = provider_mod.streamStep,

    pub fn init(allocator: Allocator, io: Io, config: *const Config) Agent {
        return .{
            .allocator = allocator,
            .io = io,
            .config = config,
        };
    }

    pub fn deinit(self: *Agent) void {
        // The agent owns neither the config snapshot nor the registry it
        // borrows; the embedder tears those down.
        _ = self;
    }

    /// Swap the active configuration snapshot. Takes effect at the start of
    /// the next turn. Safe to call between `runStep` invocations or from a
    /// tool handler that runs between provider steps; never mutates a
    /// snapshot a turn is currently reading.
    pub fn setConfig(self: *Agent, config: *const Config) void {
        self.config = config;
    }

    /// The registry exposed by the active snapshot.
    pub fn registry(self: *const Agent) *const ToolRegistry {
        return self.config.registry;
    }

    /// Drive the conversation forward until the model stops calling tools.
    pub fn runStep(
        self: *Agent,
        conv: *conversation.Conversation,
        receiver: *provider_mod.Receiver,
    ) !void {
        while (true) {
            // Re-read the config snapshot at the top of each turn so a
            // mid-conversation swap takes effect here, never mid-stream.
            const cfg = self.config;
            try self.stream_fn(self.allocator, self.io, cfg, conv, receiver);

            const last = conv.messages.items[conv.messages.items.len - 1];
            std.debug.assert(last.role == .assistant);

            // Defense-in-depth: a provider that silently committed an
            // empty assistant message means the turn made no observable
            // progress. Surface it instead of looping back to the prompt.
            if (last.content.items.len == 0) return error.EmptyAssistantResponse;

            if (!hasToolUseBlock(last)) return;

            try self.dispatchToolCalls(conv, last);
        }
    }

    fn hasToolUseBlock(msg: conversation.Message) bool {
        for (msg.content.items) |block| {
            if (block == .ToolUse) return true;
        }
        return false;
    }

    /// Dispatch every ToolUse block in `assistant_msg`. Groups by owning
    /// registration; one OS thread per group; results assembled in the
    /// original call order.
    fn dispatchToolCalls(
        self: *Agent,
        conv: *conversation.Conversation,
        assistant_msg: conversation.Message,
    ) !void {
        // Build the flat call list (in original order) and group calls
        // by owning registration.
        var calls: std.array_list.Managed(FlatCall) = .init(self.allocator);
        defer calls.deinit();

        for (assistant_msg.content.items) |block| {
            if (block != .ToolUse) continue;
            const tu = block.ToolUse;
            if (!isValidToolInput(tu.input.items)) {
                try calls.append(.{
                    .tool_use_id = tu.id,
                    .tool_name = tu.name,
                    .input = tu.input.items,
                    .entry = null,
                    .result = try invalidInputResult(self.allocator, tu.input.items),
                    .err = null,
                });
                continue;
            }
            const entry = self.config.registry.lookup(tu.name) orelse {
                // Unknown tool: abort the turn with a clear error.
                return error.UnknownTool;
            };
            try calls.append(.{
                .tool_use_id = tu.id,
                .tool_name = tu.name,
                .input = tu.input.items,
                .entry = entry.entry,
                .result = null,
                .err = null,
            });
        }
        std.debug.assert(calls.items.len > 0);

        // Partition into groups. A group's `kind` determines how it
        // runs; the `member_indices` are positions into `calls` (the
        // original call order) so we can write back results without
        // re-ordering.
        var groups: std.array_list.Managed(Group) = .init(self.allocator);
        defer {
            for (groups.items) |*g| g.deinit(self.allocator);
            groups.deinit();
        }
        try buildGroups(self.allocator, calls.items, &groups);

        // Spawn one concurrent task per group via `std.Io.Group`.
        // Single-tool groups run the tool's vtable; source groups run
        // the source's `invoke_batch`. We use `concurrent` rather than
        // `async` because tool work may block on I/O — under a
        // single-threaded `Io` `async` would deadlock; `concurrent`
        // forces real concurrency (or `error.ConcurrencyUnavailable`).
        var task_group: Io.Group = .init;
        // `cancel` is idempotent with `await`; if anything below this
        // point errors before we successfully `await`, this releases
        // the group's resources.
        defer task_group.cancel(self.io);
        errdefer {
            for (calls.items) |*c| {
                if (c.result) |r| self.allocator.free(r);
            }
        }

        for (groups.items) |*g| {
            try task_group.concurrent(self.io, runGroup, .{ self, g, calls.items });
        }
        // `error.Canceled` here means cancellation propagated into this
        // dispatch from above; surface it like any other error.
        try task_group.await(self.io);

        // Assemble ToolResult blocks in original call order. If any
        // call errored, prefer to abort the turn — but only after the
        // standard errdefer above has freed remaining results.
        var content: std.ArrayList(conversation.ContentBlock) = .empty;
        errdefer {
            for (content.items) |*b| b.deinit(self.allocator);
            content.deinit(self.allocator);
        }
        try content.ensureTotalCapacity(self.allocator, calls.items.len);

        var first_err: ?anyerror = null;
        for (calls.items) |*c| {
            if (c.err) |e| {
                first_err = e;
                continue;
            }
            const result_bytes = c.result orelse {
                // Internal error: every successful call should have left
                // bytes behind. Treat as MissingToolResult.
                first_err = error.MissingToolResult;
                continue;
            };
            c.result = null; // ownership transferred below

            const id_copy = try self.allocator.dupe(u8, c.tool_use_id);
            errdefer self.allocator.free(id_copy);

            var content_buf: conversation.TextualBlock = .empty;
            errdefer content_buf.deinit(self.allocator);
            try content_buf.appendSlice(self.allocator, result_bytes);
            self.allocator.free(result_bytes);

            content.appendAssumeCapacity(.{ .ToolResult = .{
                .tool_use_id = id_copy,
                .content = content_buf,
            } });
        }

        if (first_err) |e| return e;

        try conv.messages.append(self.allocator, .{
            .role = .user,
            .content = content,
        });
    }
};

/// One ToolUse, as flattened into the agent's dispatch list. `result`
/// and `err` are filled in by the worker; exactly one is non-null on
/// successful task completion.
const FlatCall = struct {
    tool_use_id: []const u8, // borrowed from assistant_msg
    tool_name: []const u8, // borrowed from assistant_msg
    input: []const u8, // borrowed from assistant_msg
    entry: ?Entry,

    /// Owned result bytes from `Tool.invoke` or `ToolSource.invoke_batch`.
    /// Allocated with the agent's allocator. Transferred into a
    /// ToolResultBlock on success.
    result: ?[]u8,

    /// If non-null, the call failed and the turn must abort.
    err: ?anyerror,
};

/// One dispatch group. Either a single Tool invocation, or a batch of
/// calls headed to one ToolSource.
const Group = union(enum) {
    single: SingleGroup,
    source: SourceGroup,

    pub const SingleGroup = struct {
        tool: Tool,
        /// Index into the flat calls array.
        call_index: usize,
    };

    pub const SourceGroup = struct {
        source: *ToolSource,
        /// Indices into the flat calls array. Owned by the group.
        member_indices: []usize,
    };

    fn deinit(self: *Group, allocator: Allocator) void {
        switch (self.*) {
            .single => {},
            .source => |sg| allocator.free(sg.member_indices),
        }
    }
};

/// Partition the flat call list into groups. Order of groups is
/// arbitrary; order within a `source` group preserves the original
/// call order so that batch results can be written back positionally.
fn buildGroups(
    allocator: Allocator,
    calls: []const FlatCall,
    out: *std.array_list.Managed(Group),
) !void {
    // Map from source pointer to the index of its group in `out`.
    // Buffers per source, accumulated then frozen into slices.
    var pending: std.AutoHashMap(*ToolSource, std.array_list.Managed(usize)) =
        .init(allocator);
    defer {
        var it = pending.valueIterator();
        while (it.next()) |l| l.deinit();
        pending.deinit();
    }

    for (calls, 0..) |c, i| {
        const ent = c.entry orelse continue;
        switch (ent) {
            .single => |t| try out.append(.{ .single = .{ .tool = t, .call_index = i } }),
            .source => |sr| {
                const gop = try pending.getOrPut(sr.source);
                if (!gop.found_existing) {
                    gop.value_ptr.* = std.array_list.Managed(usize).init(allocator);
                }
                try gop.value_ptr.append(i);
            },
        }
    }

    // Freeze each pending list into a source-group entry. We move
    // ownership of the indices into `Group.source.member_indices`.
    var pit = pending.iterator();
    while (pit.next()) |entry| {
        const src = entry.key_ptr.*;
        const indices = try entry.value_ptr.toOwnedSlice();
        try out.append(.{ .source = .{ .source = src, .member_indices = indices } });
    }
}

/// Worker entry point. Runs one group to completion, populating
/// `calls[i].result` or `calls[i].err` for each member call.
///
/// Return type is `void`, which coerces to `Io.Cancelable!void` as
/// required by `Group.concurrent`. Tool errors are reported via
/// `FlatCall.err`, not by returning from this function.
fn runGroup(agent: *Agent, group: *Group, calls: []FlatCall) void {
    switch (group.*) {
        .single => |sg| {
            const i = sg.call_index;
            const c = &calls[i];
            const out = sg.tool.vtable.invoke(sg.tool.ctx, c.input, agent.allocator) catch |e| {
                c.err = e;
                return;
            };
            c.result = out;
        },
        .source => |sg| runSourceGroup(agent, sg, calls),
    }
}

fn runSourceGroup(agent: *Agent, sg: Group.SourceGroup, calls: []FlatCall) void {
    const n = sg.member_indices.len;

    const batch_calls = agent.allocator.alloc(tool_source_mod.Call, n) catch |e| {
        for (sg.member_indices) |i| calls[i].err = e;
        return;
    };
    defer agent.allocator.free(batch_calls);

    const batch_results = agent.allocator.alloc(tool_source_mod.CallResult, n) catch |e| {
        for (sg.member_indices) |i| calls[i].err = e;
        return;
    };
    defer agent.allocator.free(batch_results);

    for (sg.member_indices, 0..) |idx, j| {
        batch_calls[j] = .{
            .tool_name = calls[idx].tool_name,
            .input = calls[idx].input,
        };
        batch_results[j] = .{ .err = error.SourceDroppedCall };
    }

    sg.source.vtable.invoke_batch(
        sg.source.ctx,
        batch_calls,
        batch_results,
        agent.allocator,
    ) catch |e| {
        // Whole-batch failure: free any partial successes the source
        // already wrote, then mark every member as failed.
        for (batch_results) |r| switch (r) {
            .ok => |b| agent.allocator.free(b),
            .err => {},
        };
        for (sg.member_indices) |i| calls[i].err = e;
        return;
    };

    // Per-call success/error.
    for (sg.member_indices, 0..) |i, j| {
        switch (batch_results[j]) {
            .ok => |b| calls[i].result = b,
            .err => |e| calls[i].err = e,
        }
    }
}

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

const testing = std.testing;

/// Test harness for the injectable `stream_fn` seam.
///
/// `provider_mod.StreamFn` carries no user context (it mirrors the real
/// free function exactly), so the stub parks its state in a module-level
/// pointer that `stubStreamStep` reads. The Zig test runner executes tests
/// serially in one process, so a single global slot is safe; each test
/// sets it via `install` before driving the agent.
var stub_active: ?*StubProvider = null;

const StubProvider = struct {
    allocator: Allocator,
    scripted: []const ScriptedTurn,
    next: usize = 0,

    const ScriptedTurn = struct {
        blocks: []const TestBlock,
    };

    const TestBlock = union(enum) {
        Text: []const u8,
        ToolUse: struct {
            id: []const u8,
            name: []const u8,
            input: []const u8,
        },
    };

    /// Point the global seam at this stub and return the function to assign
    /// to `agent.stream_fn`. Call once per test, after constructing the
    /// stub on the stack.
    fn install(self: *StubProvider) provider_mod.StreamFn {
        stub_active = self;
        return stubStreamStep;
    }
};

fn stubStreamStep(
    allocator: Allocator,
    _: Io,
    _: *const config_mod.Config,
    conv: *conversation.Conversation,
    _: *provider_mod.Receiver,
) anyerror!void {
    const self = stub_active orelse return error.NoStubInstalled;
    _ = allocator;
    if (self.next >= self.scripted.len) return error.NoMoreScriptedTurns;
    const turn = self.scripted[self.next];
    self.next += 1;

    var blocks: std.ArrayList(conversation.ContentBlock) = .empty;
    errdefer {
        for (blocks.items) |*b| b.deinit(self.allocator);
        blocks.deinit(self.allocator);
    }
    for (turn.blocks) |tb| {
        switch (tb) {
            .Text => |s| {
                try blocks.append(self.allocator, .{
                    .Text = try conversation.textualBlockFromSlice(self.allocator, s),
                });
            },
            .ToolUse => |tu| {
                const id = try self.allocator.dupe(u8, tu.id);
                errdefer self.allocator.free(id);
                const name = try self.allocator.dupe(u8, tu.name);
                errdefer self.allocator.free(name);
                var input_buf: conversation.TextualBlock = .empty;
                errdefer input_buf.deinit(self.allocator);
                try input_buf.appendSlice(self.allocator, tu.input);
                try blocks.append(self.allocator, .{ .ToolUse = .{
                    .id = id,
                    .name = name,
                    .input = input_buf,
                } });
            },
        }
    }
    const moved = try blocks.toOwnedSlice(self.allocator);
    defer self.allocator.free(moved);
    try conv.addAssistantMessage(moved);
}

/// Build a stack registry + active `Config` snapshot wired together, for
/// tests that drive the agent. The caller owns both and must keep them
/// alive for the agent's lifetime.
const TestHarness = struct {
    registry: ToolRegistry,
    config: config_mod.Config,

    fn init(allocator: Allocator) TestHarness {
        return .{ .registry = ToolRegistry.init(allocator), .config = undefined };
    }

    /// Finalize the config snapshot to point at this harness's registry.
    /// Must be called after `init` and before constructing the agent, once
    /// the harness has a stable address.
    fn activate(self: *TestHarness) void {
        self.config = .{
            .provider = .{ .openai_chat = .{ .api_key = "k", .base_url = "u", .model = "m" } },
            .registry = &self.registry,
        };
    }

    fn deinit(self: *TestHarness) void {
        self.registry.deinit();
    }
};

const EchoTool = struct {
    prefix_owned: []u8,
    name_owned: []u8,

    fn create(allocator: Allocator, name: []const u8, prefix: []const u8) !Tool {
        const self = try allocator.create(EchoTool);
        errdefer allocator.destroy(self);
        self.name_owned = try allocator.dupe(u8, name);
        errdefer allocator.free(self.name_owned);
        self.prefix_owned = try allocator.dupe(u8, prefix);
        return .{
            .decl = .{
                .name = self.name_owned,
                .description = "echo",
                .schema_json = "{}",
            },
            .ctx = self,
            .vtable = &vt,
        };
    }

    const vt: Tool.VTable = .{ .invoke = invoke, .deinit = deinit };

    fn invoke(ctx: *anyopaque, input: []const u8, allocator: Allocator) anyerror![]u8 {
        const self: *EchoTool = @ptrCast(@alignCast(ctx));
        return try std.fmt.allocPrint(allocator, "{s}{s}", .{ self.prefix_owned, input });
    }

    fn deinit(ctx: *anyopaque, allocator: Allocator) void {
        const self: *EchoTool = @ptrCast(@alignCast(ctx));
        allocator.free(self.name_owned);
        allocator.free(self.prefix_owned);
        allocator.destroy(self);
    }
};

const BarrierTool = struct {
    name_owned: []u8,
    barrier: *Barrier,

    const Barrier = struct {
        target: u32,
        arrived: std.atomic.Value(u32) = .init(0),
        thread_ids: [4]std.atomic.Value(u64) = .{
            .init(0), .init(0), .init(0), .init(0),
        },
    };

    fn create(allocator: Allocator, name: []const u8, barrier: *Barrier) !Tool {
        const self = try allocator.create(BarrierTool);
        errdefer allocator.destroy(self);
        self.name_owned = try allocator.dupe(u8, name);
        self.barrier = barrier;
        return .{
            .decl = .{
                .name = self.name_owned,
                .description = "barrier",
                .schema_json = "{}",
            },
            .ctx = self,
            .vtable = &vt,
        };
    }

    const vt: Tool.VTable = .{ .invoke = invoke, .deinit = deinit };

    fn invoke(ctx: *anyopaque, _: []const u8, allocator: Allocator) anyerror![]u8 {
        const self: *BarrierTool = @ptrCast(@alignCast(ctx));
        const arrived = self.barrier.arrived.fetchAdd(1, .acq_rel);
        if (arrived < self.barrier.thread_ids.len) {
            self.barrier.thread_ids[arrived].store(std.Thread.getCurrentId(), .release);
        }

        var i: usize = 0;
        while (self.barrier.arrived.load(.acquire) < self.barrier.target) : (i += 1) {
            if (i > 50_000) return error.BarrierTimeout;
            std.Thread.yield() catch {};
        }
        return try allocator.dupe(u8, "done");
    }

    fn deinit(ctx: *anyopaque, allocator: Allocator) void {
        const self: *BarrierTool = @ptrCast(@alignCast(ctx));
        allocator.free(self.name_owned);
        allocator.destroy(self);
    }
};

const FailingTool = struct {
    name_owned: []u8,

    fn create(allocator: Allocator, name: []const u8) !Tool {
        const self = try allocator.create(FailingTool);
        errdefer allocator.destroy(self);
        self.name_owned = try allocator.dupe(u8, name);
        return .{
            .decl = .{
                .name = self.name_owned,
                .description = "fails",
                .schema_json = "{}",
            },
            .ctx = self,
            .vtable = &vt,
        };
    }

    const vt: Tool.VTable = .{ .invoke = invoke, .deinit = deinit };

    fn invoke(_: *anyopaque, _: []const u8, _: Allocator) anyerror![]u8 {
        return error.ToolExploded;
    }

    fn deinit(ctx: *anyopaque, allocator: Allocator) void {
        const self: *FailingTool = @ptrCast(@alignCast(ctx));
        allocator.free(self.name_owned);
        allocator.destroy(self);
    }
};

const NoopReceiver = struct {
    fn make() provider_mod.Receiver {
        return .{ .ptr = @ptrCast(@constCast(&dummy)), .vtable = &vt };
    }
    var dummy: u8 = 0;
    const vt: provider_mod.ReceiverVTable = .{
        .onMessageStart = noop1,
        .onBlockStart = noop2,
        .onToolDetails = noopToolDetails,
        .onContentDelta = noop3,
        .onBlockComplete = noop4,
        .onMessageComplete = noop5,
        .onError = noop6,
    };
    fn noop1(_: *anyopaque, _: conversation.MessageRole) anyerror!void {}
    fn noop2(_: *anyopaque, _: provider_mod.ContentBlockType, _: usize) anyerror!void {}
    fn noopToolDetails(_: *anyopaque, _: usize, _: []const u8, _: []const u8) anyerror!void {}
    fn noop3(_: *anyopaque, _: usize, _: []const u8) anyerror!void {}
    fn noop4(_: *anyopaque, _: usize, _: conversation.ContentBlock) anyerror!void {}
    fn noop5(_: *anyopaque, _: conversation.Message, _: ?provider_mod.Usage) anyerror!void {}
    fn noop6(_: *anyopaque, _: anyerror) void {}
};

/// A configurable ToolSource for testing the grouped-dispatch path.
/// Stores every batch it receives so tests can assert "calls X and Y
/// arrived in the same batch on the same thread".
const TestSource = struct {
    name_owned: []u8,
    decls: []tool_source_mod.ToolDecl,
    decl_strings: std.array_list.Managed([]u8),
    /// Sequence of (thread_id, [tool_name; n]) per batch received.
    /// Only mutated inside `invoke_batch`. Because libpanto guarantees
    /// at most one outstanding `invoke_batch` per source at any time
    /// (one batch per turn per source), no synchronization is needed.
    batches: std.array_list.Managed(Batch),
    allocator: Allocator,

    const Batch = struct {
        thread_id: u64,
        names: std.array_list.Managed([]u8),
    };

    fn create(
        allocator: Allocator,
        source_name: []const u8,
        tool_names: []const []const u8,
    ) !ToolSource {
        const self = try allocator.create(TestSource);
        errdefer allocator.destroy(self);

        var strings = std.array_list.Managed([]u8).init(allocator);
        errdefer {
            for (strings.items) |s| allocator.free(s);
            strings.deinit();
        }

        const name_owned = try allocator.dupe(u8, source_name);
        try strings.append(name_owned);

        const decls = try allocator.alloc(tool_source_mod.ToolDecl, tool_names.len);
        errdefer allocator.free(decls);
        for (tool_names, 0..) |tn, i| {
            const n = try allocator.dupe(u8, tn);
            try strings.append(n);
            const d = try allocator.dupe(u8, "test src tool");
            try strings.append(d);
            const s = try allocator.dupe(u8, "{}");
            try strings.append(s);
            decls[i] = .{ .name = n, .description = d, .schema_json = s };
        }

        self.* = .{
            .name_owned = name_owned,
            .decls = decls,
            .decl_strings = strings,
            .batches = std.array_list.Managed(Batch).init(allocator),
            .allocator = allocator,
        };

        return ToolSource{
            .name = self.name_owned,
            .tools = self.decls,
            .ctx = self,
            .vtable = &vt,
        };
    }

    const vt: ToolSource.VTable = .{
        .invoke_batch = invokeBatch,
        .deinit = deinitSrc,
    };

    fn invokeBatch(
        ctx: *anyopaque,
        calls: []const tool_source_mod.Call,
        results: []tool_source_mod.CallResult,
        allocator: Allocator,
    ) anyerror!void {
        const self: *TestSource = @ptrCast(@alignCast(ctx));
        var batch: Batch = .{
            .thread_id = std.Thread.getCurrentId(),
            .names = std.array_list.Managed([]u8).init(self.allocator),
        };
        for (calls) |c| {
            const copy = try self.allocator.dupe(u8, c.tool_name);
            try batch.names.append(copy);
        }
        try self.batches.append(batch);

        for (calls, 0..) |c, i| {
            results[i] = .{
                .ok = std.fmt.allocPrint(
                    allocator,
                    "{s}->{s}",
                    .{ c.tool_name, c.input },
                ) catch |e| {
                    results[i] = .{ .err = e };
                    continue;
                },
            };
        }
    }

    fn deinitSrc(ctx: *anyopaque, _: Allocator) void {
        const self: *TestSource = @ptrCast(@alignCast(ctx));
        for (self.decl_strings.items) |s| self.allocator.free(s);
        self.decl_strings.deinit();
        for (self.batches.items) |*b| {
            for (b.names.items) |n| self.allocator.free(n);
            b.names.deinit();
        }
        self.batches.deinit();
        self.allocator.free(self.decls);
        self.allocator.destroy(self);
    }
};

/// A source that always fails the whole batch by returning an error
/// from invoke_batch (rather than recording per-call errors). Used to
/// verify libpanto's whole-batch-failure path.
const FailingSource = struct {
    name_owned: []u8,
    decls: []tool_source_mod.ToolDecl,
    decl_strings: std.array_list.Managed([]u8),
    allocator: Allocator,

    fn create(allocator: Allocator, source_name: []const u8, tool_names: []const []const u8) !ToolSource {
        const self = try allocator.create(FailingSource);
        errdefer allocator.destroy(self);

        var strings = std.array_list.Managed([]u8).init(allocator);
        errdefer {
            for (strings.items) |s| allocator.free(s);
            strings.deinit();
        }

        const name_owned = try allocator.dupe(u8, source_name);
        try strings.append(name_owned);

        const decls = try allocator.alloc(tool_source_mod.ToolDecl, tool_names.len);
        errdefer allocator.free(decls);
        for (tool_names, 0..) |tn, i| {
            const n = try allocator.dupe(u8, tn);
            try strings.append(n);
            const d = try allocator.dupe(u8, "fails");
            try strings.append(d);
            const s = try allocator.dupe(u8, "{}");
            try strings.append(s);
            decls[i] = .{ .name = n, .description = d, .schema_json = s };
        }

        self.* = .{
            .name_owned = name_owned,
            .decls = decls,
            .decl_strings = strings,
            .allocator = allocator,
        };
        return ToolSource{ .name = self.name_owned, .tools = self.decls, .ctx = self, .vtable = &vt };
    }

    const vt: ToolSource.VTable = .{ .invoke_batch = invokeBatch, .deinit = deinitSrc };

    fn invokeBatch(
        _: *anyopaque,
        _: []const tool_source_mod.Call,
        _: []tool_source_mod.CallResult,
        _: Allocator,
    ) anyerror!void {
        return error.SourceExploded;
    }

    fn deinitSrc(ctx: *anyopaque, _: Allocator) void {
        const self: *FailingSource = @ptrCast(@alignCast(ctx));
        for (self.decl_strings.items) |s| self.allocator.free(s);
        self.decl_strings.deinit();
        self.allocator.free(self.decls);
        self.allocator.destroy(self);
    }
};

test "registry register and lookup" {
    var h = TestHarness.init(testing.allocator);
    defer h.deinit();
    try h.registry.register(try EchoTool.create(testing.allocator, "echo", "ECHO:"));
    try testing.expectEqual(@as(usize, 1), h.registry.count());
    try testing.expect(h.registry.lookup("echo") != null);
}

test "duplicate register returns error" {
    var h = TestHarness.init(testing.allocator);
    defer h.deinit();
    try h.registry.register(try EchoTool.create(testing.allocator, "echo", "A:"));

    var dup = try EchoTool.create(testing.allocator, "echo", "B:");
    try testing.expectError(error.DuplicateTool, h.registry.register(dup));
    dup.vtable.deinit(dup.ctx, testing.allocator);
}

test "runStep dispatches a tool call and loops to a final text turn" {
    const allocator = testing.allocator;

    const scripted = [_]StubProvider.ScriptedTurn{
        .{ .blocks = &.{
            .{ .ToolUse = .{ .id = "tc_1", .name = "echo", .input = "hello" } },
        } },
        .{ .blocks = &.{
            .{ .Text = "ok" },
        } },
    };
    var stub = StubProvider{ .allocator = allocator, .scripted = &scripted };
    var threaded: std.Io.Threaded = .init(allocator, .{});
    defer threaded.deinit();
    const io = threaded.io();
    var h = TestHarness.init(allocator);
    defer h.deinit();
    try h.registry.register(try EchoTool.create(allocator, "echo", "ECHO:"));
    h.activate();
    var agent = Agent.init(allocator, io, &h.config);
    agent.stream_fn = stub.install();

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("call a tool");

    var recv = NoopReceiver.make();
    try agent.runStep(&conv, &recv);

    try testing.expectEqual(@as(usize, 4), conv.messages.items.len);

    try testing.expectEqual(conversation.MessageRole.assistant, conv.messages.items[1].role);
    try testing.expectEqual(@as(usize, 1), conv.messages.items[1].content.items.len);
    try testing.expectEqualStrings("tc_1", conv.messages.items[1].content.items[0].ToolUse.id);

    try testing.expectEqual(conversation.MessageRole.user, conv.messages.items[2].role);
    try testing.expectEqual(@as(usize, 1), conv.messages.items[2].content.items.len);
    const tr = conv.messages.items[2].content.items[0].ToolResult;
    try testing.expectEqualStrings("tc_1", tr.tool_use_id);
    try testing.expectEqualStrings("ECHO:hello", tr.content.items);

    try testing.expectEqual(conversation.MessageRole.assistant, conv.messages.items[3].role);
    try testing.expectEqualStrings("ok", conv.messages.items[3].content.items[0].Text.items);
}

test "runStep dispatches multiple tool calls in parallel" {
    const allocator = testing.allocator;

    var barrier: BarrierTool.Barrier = .{ .target = 3 };

    const scripted = [_]StubProvider.ScriptedTurn{
        .{ .blocks = &.{
            .{ .ToolUse = .{ .id = "a", .name = "barrierA", .input = "" } },
            .{ .ToolUse = .{ .id = "b", .name = "barrierB", .input = "" } },
            .{ .ToolUse = .{ .id = "c", .name = "barrierC", .input = "" } },
        } },
        .{ .blocks = &.{
            .{ .Text = "done" },
        } },
    };
    var stub = StubProvider{ .allocator = allocator, .scripted = &scripted };
    var threaded: std.Io.Threaded = .init(allocator, .{});
    defer threaded.deinit();
    const io = threaded.io();
    var h = TestHarness.init(allocator);
    defer h.deinit();
    try h.registry.register(try BarrierTool.create(allocator, "barrierA", &barrier));
    try h.registry.register(try BarrierTool.create(allocator, "barrierB", &barrier));
    try h.registry.register(try BarrierTool.create(allocator, "barrierC", &barrier));
    h.activate();
    var agent = Agent.init(allocator, io, &h.config);
    agent.stream_fn = stub.install();

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("go");

    var recv = NoopReceiver.make();
    try agent.runStep(&conv, &recv);

    const tr_msg = conv.messages.items[2];
    try testing.expectEqual(@as(usize, 3), tr_msg.content.items.len);
    try testing.expectEqualStrings("a", tr_msg.content.items[0].ToolResult.tool_use_id);
    try testing.expectEqualStrings("b", tr_msg.content.items[1].ToolResult.tool_use_id);
    try testing.expectEqualStrings("c", tr_msg.content.items[2].ToolResult.tool_use_id);

    const t0 = barrier.thread_ids[0].load(.acquire);
    const t1 = barrier.thread_ids[1].load(.acquire);
    const t2 = barrier.thread_ids[2].load(.acquire);
    try testing.expect(t0 != 0 and t1 != 0 and t2 != 0);
    try testing.expect(t0 != t1 and t1 != t2 and t0 != t2);
}

test "runStep propagates tool errors and aborts the turn" {
    const allocator = testing.allocator;

    const scripted = [_]StubProvider.ScriptedTurn{
        .{ .blocks = &.{
            .{ .ToolUse = .{ .id = "x", .name = "boom", .input = "" } },
        } },
        .{ .blocks = &.{.{ .Text = "should-not-see" }} },
    };
    var stub = StubProvider{ .allocator = allocator, .scripted = &scripted };
    var threaded: std.Io.Threaded = .init(allocator, .{});
    defer threaded.deinit();
    const io = threaded.io();
    var h = TestHarness.init(allocator);
    defer h.deinit();
    try h.registry.register(try FailingTool.create(allocator, "boom"));
    h.activate();
    var agent = Agent.init(allocator, io, &h.config);
    agent.stream_fn = stub.install();

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("break it");

    var recv = NoopReceiver.make();
    try testing.expectError(error.ToolExploded, agent.runStep(&conv, &recv));

    try testing.expectEqual(@as(usize, 2), conv.messages.items.len);
}

test "runStep errors UnknownTool when the model calls something unregistered" {
    const allocator = testing.allocator;

    const scripted = [_]StubProvider.ScriptedTurn{
        .{ .blocks = &.{
            .{ .ToolUse = .{ .id = "z", .name = "ghost", .input = "" } },
        } },
    };
    var stub = StubProvider{ .allocator = allocator, .scripted = &scripted };
    var threaded: std.Io.Threaded = .init(allocator, .{});
    defer threaded.deinit();
    const io = threaded.io();
    var h = TestHarness.init(allocator);
    defer h.deinit();
    h.activate();
    var agent = Agent.init(allocator, io, &h.config);
    agent.stream_fn = stub.install();

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("call a ghost");

    var recv = NoopReceiver.make();
    try testing.expectError(error.UnknownTool, agent.runStep(&conv, &recv));
}

test "runStep with no tool calls returns after one provider step" {
    const allocator = testing.allocator;

    const scripted = [_]StubProvider.ScriptedTurn{
        .{ .blocks = &.{.{ .Text = "hi" }} },
    };
    var stub = StubProvider{ .allocator = allocator, .scripted = &scripted };
    var threaded: std.Io.Threaded = .init(allocator, .{});
    defer threaded.deinit();
    const io = threaded.io();
    var h = TestHarness.init(allocator);
    defer h.deinit();
    h.activate();
    var agent = Agent.init(allocator, io, &h.config);
    agent.stream_fn = stub.install();

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("hello");

    var recv = NoopReceiver.make();
    try agent.runStep(&conv, &recv);

    try testing.expectEqual(@as(usize, 2), conv.messages.items.len);
    try testing.expectEqualStrings("hi", conv.messages.items[1].content.items[0].Text.items);
}

test "runStep surfaces EmptyAssistantResponse when provider commits an empty message" {
    const allocator = testing.allocator;

    const scripted = [_]StubProvider.ScriptedTurn{
        .{ .blocks = &.{} },
    };
    var stub = StubProvider{ .allocator = allocator, .scripted = &scripted };
    var threaded: std.Io.Threaded = .init(allocator, .{});
    defer threaded.deinit();
    const io = threaded.io();
    var h = TestHarness.init(allocator);
    defer h.deinit();
    h.activate();
    var agent = Agent.init(allocator, io, &h.config);
    agent.stream_fn = stub.install();

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("hi");

    var recv = NoopReceiver.make();
    try testing.expectError(error.EmptyAssistantResponse, agent.runStep(&conv, &recv));
}

// ------------ ToolSource tests ------------

test "runStep delivers all source-backed calls in one batch on one thread" {
    const allocator = testing.allocator;

    const scripted = [_]StubProvider.ScriptedTurn{
        .{ .blocks = &.{
            .{ .ToolUse = .{ .id = "a", .name = "lua_x", .input = "1" } },
            .{ .ToolUse = .{ .id = "b", .name = "lua_y", .input = "2" } },
            .{ .ToolUse = .{ .id = "c", .name = "lua_x", .input = "3" } },
        } },
        .{ .blocks = &.{.{ .Text = "done" }} },
    };
    var stub = StubProvider{ .allocator = allocator, .scripted = &scripted };
    var threaded: std.Io.Threaded = .init(allocator, .{});
    defer threaded.deinit();
    const io = threaded.io();
    var h = TestHarness.init(allocator);
    defer h.deinit();
    try h.registry.registerSource(try TestSource.create(allocator, "panto-lua", &.{ "lua_x", "lua_y" }));
    h.activate();
    var agent = Agent.init(allocator, io, &h.config);
    agent.stream_fn = stub.install();

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("go");

    var recv = NoopReceiver.make();
    try agent.runStep(&conv, &recv);

    // Locate the source and inspect its observed batches.
    const view = h.registry.lookup("lua_x") orelse return error.NotFound;
    const src_ptr = view.entry.source.source;
    const test_src: *TestSource = @ptrCast(@alignCast(src_ptr.ctx));

    try testing.expectEqual(@as(usize, 1), test_src.batches.items.len);
    const b = test_src.batches.items[0];
    try testing.expectEqual(@as(usize, 3), b.names.items.len);
    try testing.expectEqualStrings("lua_x", b.names.items[0]);
    try testing.expectEqualStrings("lua_y", b.names.items[1]);
    try testing.expectEqualStrings("lua_x", b.names.items[2]);

    // ToolResults arrived in the original call order.
    const tr_msg = conv.messages.items[2];
    try testing.expectEqual(@as(usize, 3), tr_msg.content.items.len);
    try testing.expectEqualStrings("a", tr_msg.content.items[0].ToolResult.tool_use_id);
    try testing.expectEqualStrings("lua_x->1", tr_msg.content.items[0].ToolResult.content.items);
    try testing.expectEqualStrings("b", tr_msg.content.items[1].ToolResult.tool_use_id);
    try testing.expectEqualStrings("lua_y->2", tr_msg.content.items[1].ToolResult.content.items);
    try testing.expectEqualStrings("c", tr_msg.content.items[2].ToolResult.tool_use_id);
    try testing.expectEqualStrings("lua_x->3", tr_msg.content.items[2].ToolResult.content.items);
}

test "runStep: distinct sources run on distinct threads in parallel" {
    const allocator = testing.allocator;

    const scripted = [_]StubProvider.ScriptedTurn{
        .{ .blocks = &.{
            .{ .ToolUse = .{ .id = "a", .name = "src_a_t", .input = "" } },
            .{ .ToolUse = .{ .id = "b", .name = "src_b_t", .input = "" } },
        } },
        .{ .blocks = &.{.{ .Text = "done" }} },
    };
    var stub = StubProvider{ .allocator = allocator, .scripted = &scripted };
    var threaded: std.Io.Threaded = .init(allocator, .{});
    defer threaded.deinit();
    const io = threaded.io();
    var h = TestHarness.init(allocator);
    defer h.deinit();
    try h.registry.registerSource(try TestSource.create(allocator, "src_a", &.{"src_a_t"}));
    try h.registry.registerSource(try TestSource.create(allocator, "src_b", &.{"src_b_t"}));
    h.activate();
    var agent = Agent.init(allocator, io, &h.config);
    agent.stream_fn = stub.install();

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("go");

    var recv = NoopReceiver.make();
    try agent.runStep(&conv, &recv);

    const view_a = h.registry.lookup("src_a_t") orelse return error.NotFound;
    const view_b = h.registry.lookup("src_b_t") orelse return error.NotFound;
    const sa: *TestSource = @ptrCast(@alignCast(view_a.entry.source.source.ctx));
    const sb: *TestSource = @ptrCast(@alignCast(view_b.entry.source.source.ctx));

    try testing.expectEqual(@as(usize, 1), sa.batches.items.len);
    try testing.expectEqual(@as(usize, 1), sb.batches.items.len);
    // The two sources ran on distinct OS threads.
    try testing.expect(sa.batches.items[0].thread_id != sb.batches.items[0].thread_id);
}

test "runStep: source whole-batch error aborts the turn" {
    const allocator = testing.allocator;

    const scripted = [_]StubProvider.ScriptedTurn{
        .{ .blocks = &.{
            .{ .ToolUse = .{ .id = "a", .name = "fa", .input = "" } },
            .{ .ToolUse = .{ .id = "b", .name = "fb", .input = "" } },
        } },
        .{ .blocks = &.{.{ .Text = "never" }} },
    };
    var stub = StubProvider{ .allocator = allocator, .scripted = &scripted };
    var threaded: std.Io.Threaded = .init(allocator, .{});
    defer threaded.deinit();
    const io = threaded.io();
    var h = TestHarness.init(allocator);
    defer h.deinit();
    try h.registry.registerSource(try FailingSource.create(allocator, "fs", &.{ "fa", "fb" }));
    h.activate();
    var agent = Agent.init(allocator, io, &h.config);
    agent.stream_fn = stub.install();

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("kaboom");

    var recv = NoopReceiver.make();
    try testing.expectError(error.SourceExploded, agent.runStep(&conv, &recv));

    // Conversation stops at user + assistant(tool_use). No ToolResult appended.
    try testing.expectEqual(@as(usize, 2), conv.messages.items.len);
}

test "runStep: mixed single Tools and source-backed tools coexist in one turn" {
    const allocator = testing.allocator;

    const scripted = [_]StubProvider.ScriptedTurn{
        .{ .blocks = &.{
            .{ .ToolUse = .{ .id = "a", .name = "single", .input = "X" } },
            .{ .ToolUse = .{ .id = "b", .name = "src_t1", .input = "Y" } },
            .{ .ToolUse = .{ .id = "c", .name = "src_t2", .input = "Z" } },
        } },
        .{ .blocks = &.{.{ .Text = "done" }} },
    };
    var stub = StubProvider{ .allocator = allocator, .scripted = &scripted };
    var threaded: std.Io.Threaded = .init(allocator, .{});
    defer threaded.deinit();
    const io = threaded.io();
    var h = TestHarness.init(allocator);
    defer h.deinit();
    try h.registry.register(try EchoTool.create(allocator, "single", "S:"));
    try h.registry.registerSource(try TestSource.create(allocator, "src", &.{ "src_t1", "src_t2" }));
    h.activate();
    var agent = Agent.init(allocator, io, &h.config);
    agent.stream_fn = stub.install();

    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("go");

    var recv = NoopReceiver.make();
    try agent.runStep(&conv, &recv);

    const tr_msg = conv.messages.items[2];
    try testing.expectEqual(@as(usize, 3), tr_msg.content.items.len);
    try testing.expectEqualStrings("S:X", tr_msg.content.items[0].ToolResult.content.items);
    try testing.expectEqualStrings("src_t1->Y", tr_msg.content.items[1].ToolResult.content.items);
    try testing.expectEqualStrings("src_t2->Z", tr_msg.content.items[2].ToolResult.content.items);
}

test "setConfig swaps the visible tool set between turns" {
    // The core RCU promise: the agent reads `*const Config` fresh each
    // turn, so swapping the pointer mid-conversation changes the tool set
    // the next turn sees. Config A exposes only `echo`; config B only
    // `late`. After `setConfig(&cfg_b)`, a turn that calls `late` resolves
    // — proving both the swap and per-turn re-consultation.
    const allocator = testing.allocator;

    const scripted = [_]StubProvider.ScriptedTurn{
        .{ .blocks = &.{.{ .ToolUse = .{ .id = "2", .name = "late", .input = "B" } }} },
        .{ .blocks = &.{.{ .Text = "done" }} },
    };
    var stub = StubProvider{ .allocator = allocator, .scripted = &scripted };
    var threaded: std.Io.Threaded = .init(allocator, .{});
    defer threaded.deinit();
    const io = threaded.io();

    // Config A: only `echo`.
    var reg_a = ToolRegistry.init(allocator);
    defer reg_a.deinit();
    try reg_a.register(try EchoTool.create(allocator, "echo", "A:"));
    const cfg_a: config_mod.Config = .{
        .provider = .{ .openai_chat = .{ .api_key = "k", .base_url = "u", .model = "m" } },
        .registry = &reg_a,
    };

    // Config B: only `late`.
    var reg_b = ToolRegistry.init(allocator);
    defer reg_b.deinit();
    try reg_b.register(try EchoTool.create(allocator, "late", "B:"));
    const cfg_b: config_mod.Config = .{
        .provider = .{ .openai_chat = .{ .api_key = "k", .base_url = "u", .model = "m" } },
        .registry = &reg_b,
    };

    var agent = Agent.init(allocator, io, &cfg_a);
    agent.stream_fn = stub.install();

    // Under A: `echo` visible, `late` not.
    try testing.expect(agent.config.registry.lookup("echo") != null);
    try testing.expect(agent.config.registry.lookup("late") == null);

    // Swap. Under B: the visibility inverts.
    agent.setConfig(&cfg_b);
    try testing.expect(agent.config.registry.lookup("echo") == null);
    try testing.expect(agent.config.registry.lookup("late") != null);

    // A real turn under B resolves `late` (which would have been
    // UnknownTool under A), then loops to the final text turn.
    var conv = conversation.Conversation.init(allocator);
    defer conv.deinit();
    try conv.addUserMessage("go");
    var recv = NoopReceiver.make();
    try agent.runStep(&conv, &recv);

    const tr = conv.messages.items[2].content.items[0].ToolResult;
    try testing.expectEqualStrings("2", tr.tool_use_id);
    try testing.expectEqualStrings("B:B", tr.content.items);
}