/* * splinter -- a turn that pauses for approval and resumes. * * The tool has not run when the turn returns SP_AGENT_INPUT_REQUIRED: the * assistant call is in the conversation, no observation is. Resuming with a * decision runs the pending call, adds its observation and continues the * model loop. A denial is an observation too, and the turn still ends. */ #include #include #include "splinter.h" static int failures; static int ran; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static int write_tool(const char *args, struct sp_buf *out, void *ctx) { (void)args; (void)ctx; ran++; return sp_buf_puts(out, "written"); } static int approve(void *ctx, const char *name, int auth, const char *args) { (void)name; (void)auth; (void)args; return *(int *)ctx; } int main(void) { struct sp_registry r; struct sp_tool t; struct sp_agent a; struct sp_agent_config cfg; struct sp_conv conv; struct sp_mock m; struct sp_buf answer, err; int verdict; memset(&r, 0, sizeof(r)); memset(&t, 0, sizeof(t)); t.name = "write"; t.description = "mutate"; t.auth = SP_AUTH_MUTATE; t.run = write_tool; CHECK(sp_registry_add(&r, &t) == 0); memset(&a, 0, sizeof(a)); memset(&cfg, 0, sizeof(cfg)); cfg.max_steps = 5; cfg.autonomous = 0; cfg.approve = approve; cfg.approve_ctx = &verdict; cfg.approve_is_channel = 1; sp_conv_init(&conv); a.reg = &r; a.cfg = &cfg; a.conv = &conv; CHECK(sp_buf_init(&answer) == 0); CHECK(sp_buf_init(&err) == 0); /* a call that needs a person: the turn stops before it runs */ CHECK(sp_mock_init(&m) == 0); CHECK(sp_mock_push(&m, 200, "tool write x") == 0); CHECK(sp_mock_push(&m, 200, "answer done") == 0); a.tr = sp_mock_transport(&m); verdict = 2; CHECK(sp_agent_run(&a, "go", &answer, &err) == SP_AGENT_INPUT_REQUIRED); CHECK(ran == 0); CHECK(a.has_pending == 1); CHECK(conv.n >= 2); CHECK(strcmp(conv.v[conv.n - 1].role, "assistant") == 0); /* yes: the pending call runs, its observation goes back, the turn ends */ CHECK(sp_agent_resume(&a, 1, &answer, &err) == 0); CHECK(ran == 1); CHECK(a.has_pending == 0); CHECK(strcmp(answer.p, "done") == 0); CHECK(strstr(m.last_request.p, "observe write written") != NULL); sp_mock_free(&m); sp_conv_free(&conv); /* no: the call does not run, but it is an observation and the turn ends */ ran = 0; memset(&a, 0, sizeof(a)); memset(&cfg, 0, sizeof(cfg)); cfg.max_steps = 5; cfg.approve = approve; cfg.approve_ctx = &verdict; cfg.approve_is_channel = 1; sp_conv_init(&conv); a.reg = &r; a.cfg = &cfg; a.conv = &conv; CHECK(sp_mock_init(&m) == 0); CHECK(sp_mock_push(&m, 200, "tool write x") == 0); CHECK(sp_mock_push(&m, 200, "answer done") == 0); a.tr = sp_mock_transport(&m); verdict = 2; CHECK(sp_agent_run(&a, "go", &answer, &err) == SP_AGENT_INPUT_REQUIRED); CHECK(sp_agent_resume(&a, 0, &answer, &err) == 0); CHECK(ran == 0); CHECK(a.has_pending == 0); CHECK(strstr(m.last_request.p, "observe write") != NULL); CHECK(strstr(m.last_request.p, "denied by operator") != NULL); sp_mock_free(&m); sp_conv_free(&conv); sp_buf_free(&answer); sp_buf_free(&err); sp_registry_free(&r); if (failures == 0) printf("test_pause: ok\n"); return failures == 0 ? 0 : 1; }