Something went wrong. Try again.
minimal agent cli memoria.tools
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262package main
import "./session"import "./tools"import "core:os"import "core:strings"import "core:sync"import "core:testing"import "core:time"
tool_runner_test_executor :: proc( _: rawptr, _: string, name: string, arguments: string, _: tools.Cancel_Check, _: rawptr,) -> string { return strings.concatenate({name, ":", arguments})}
tool_runner_test_bash_output_executor :: proc( _: rawptr, _: string, _: string, _: string, _: tools.Cancel_Check, _: rawptr,) -> string { output := [8]u8{'c', 'o', 'm', 'p', 'l', 'e', 't', 'e'} return tools.bash_result( output[:], os.Process_State{exited = true, success = true}, false, false, time.Second, )}
tool_runner_test_cancellable_executor :: proc( _: rawptr, _: string, _: string, _: string, cancel: tools.Cancel_Check, cancel_data: rawptr,) -> string { for cancel == nil || !cancel(cancel_data) { time.sleep(time.Millisecond) } return strings.clone("cancelled by test")}
tool_runner_test_wait :: proc(runner: ^Tool_Runner) -> (Tool_Completion, bool) { for _ in 0 ..< 1_000 { if completion, ok := tool_runner_take_completion(runner); ok { return completion, true } time.sleep(time.Millisecond) } return {}, false}
@(test)tool_runner_transfers_latest_progress_once :: proc(t: ^testing.T) { runner: Tool_Runner defer tool_runner_destroy(&runner) tool_runner_publish_progress(&runner, "previous line", "current line")
prompt, older, latest, rows, changed := tool_runner_take_progress(&runner) defer delete(prompt) defer delete(older) defer delete(latest) testing.expect(t, changed) testing.expect_value(t, older, "previous line") testing.expect_value(t, latest, "current line") testing.expect_value(t, rows, 0) _, _, _, _, changed_again := tool_runner_take_progress(&runner) testing.expect(t, !changed_again)}
@(test)tool_runner_publishes_task_prompt_and_three_row_offset :: proc(t: ^testing.T) { runner: Tool_Runner if !testing.expect(t, tool_runner_init(&runner, tool_runner_test_executor)) { return } defer tool_runner_destroy(&runner) calls := [1]session.Tool_Call { { id = "call-task", function_call = { name = "task", arguments = `{"description":"inspect","prompt":"Inspect\nsrc"}`, }, }, } testing.expect(t, tool_runner_submit(&runner, calls[:], ".")) completion, completed := tool_runner_test_wait(&runner) if !testing.expect(t, completed) { return } defer tool_completion_destroy(&completion)
prompt, older, latest, rows, changed := tool_runner_take_progress(&runner) defer delete(prompt) defer delete(older) defer delete(latest) testing.expect(t, changed) testing.expect_value(t, prompt, "Inspect\nsrc") testing.expect_value(t, rows, 3)}
@(test)tool_runner_executes_owned_calls_off_the_submission_path :: proc(t: ^testing.T) { runner: Tool_Runner if !testing.expect(t, tool_runner_init(&runner, tool_runner_test_executor)) { return } defer tool_runner_destroy(&runner)
event := Stream_Event { kind = .Tool_Calls, } append( &event.tool_calls, session.Tool_Call { id = strings.clone("call-1"), function_call = { name = strings.clone("fake"), arguments = strings.clone(`{"value":1}`), }, }, ) testing.expect(t, tool_runner_submit(&runner, event.tool_calls[:], ".")) stream_event_destroy(&event)
completion, completed := tool_runner_test_wait(&runner) if !testing.expect(t, completed) { return } defer tool_completion_destroy(&completion) testing.expect(t, !completion.cancelled) testing.expect_value(t, len(completion.results), 1) testing.expect_value(t, completion.results[0].call_id, "call-1") testing.expect_value(t, completion.results[0].output, `fake:{"value":1}`)}
@(test)tool_runner_cancels_cooperative_tools :: proc(t: ^testing.T) { runner: Tool_Runner if !testing.expect(t, tool_runner_init(&runner, tool_runner_test_cancellable_executor)) { return } defer tool_runner_destroy(&runner) calls := [1]session.Tool_Call { {id = "call-1", function_call = {name = "fake", arguments = `{}`}}, } testing.expect(t, tool_runner_submit(&runner, calls[:], ".")) tool_runner_cancel(&runner)
completion, completed := tool_runner_test_wait(&runner) if !testing.expect(t, completed) { return } defer tool_completion_destroy(&completion) testing.expect(t, completion.cancelled) testing.expect_value(t, len(completion.results), 1)}
@(test)tool_runner_transfers_successful_bash_output_across_allocators :: proc(t: ^testing.T) { // Keep this a runner/ownership unit test: launching sandbox-exec here would // require nested sandboxing when the suite is run through Memoria's bash tool. runner: Tool_Runner if !testing.expect(t, tool_runner_init(&runner, tool_runner_test_bash_output_executor)) { return } defer tool_runner_destroy(&runner) calls := [1]session.Tool_Call { { id = "call-1", function_call = { name = "bash", arguments = `{"command":"printf complete"}`, }, }, } testing.expect(t, tool_runner_submit(&runner, calls[:], "."))
completion, completed := tool_runner_test_wait(&runner) if !testing.expect(t, completed) { return } defer tool_completion_destroy(&completion) testing.expect(t, !completion.cancelled) testing.expect_value(t, completion.results[0].output, "complete")}
Tool_Spinner_Gate :: struct { mutex: sync.Mutex, step: int,}
tool_spinner_gate_executor :: proc( data: rawptr, _: string, name: string, _: string, cancel: tools.Cancel_Check, cancel_data: rawptr,) -> string { gate := cast(^Tool_Spinner_Gate)data wanted := name == "first" ? 1 : 2 for _ in 0 ..< 1_000 { sync.mutex_lock(&gate.mutex) step := gate.step sync.mutex_unlock(&gate.mutex) if step >= wanted || cancel(cancel_data) do break time.sleep(time.Millisecond) } return strings.clone(name)}
@(test)tool_runner_spinner_tracks_serial_calls_and_cancellation :: proc(t: ^testing.T) { gate: Tool_Spinner_Gate runner: Tool_Runner if !testing.expect(t, tool_runner_init(&runner, tool_spinner_gate_executor, &gate)) do return defer tool_runner_destroy(&runner) calls := [2]session.Tool_Call { {function_call = {name = "first"}}, {function_call = {name = "second"}}, } if !testing.expect(t, tool_runner_submit(&runner, calls[:], ".")) do return index, spinning := tool_runner_spinning_call(&runner) testing.expect(t, spinning) testing.expect_value(t, index, 0) sync.mutex_lock(&gate.mutex) gate.step = 1 sync.mutex_unlock(&gate.mutex) advanced := false for _ in 0 ..< 1_000 { index, spinning = tool_runner_spinning_call(&runner) if index == 1 && spinning { advanced = true break } time.sleep(time.Millisecond) } testing.expect(t, advanced) tool_runner_cancel(&runner) _, spinning = tool_runner_spinning_call(&runner) testing.expect(t, !spinning) completion, completed := tool_runner_test_wait(&runner) if !testing.expect(t, completed) do return defer tool_completion_destroy(&completion) testing.expect(t, completion.cancelled) _, spinning = tool_runner_spinning_call(&runner) testing.expect(t, !spinning)}