agent: validate tool-call arguments before they enter the conversation
A model step with arguments like "{command:cd ..." is not JSON. Splinter
accepted it, ran "sh -c {command:cd ..." (exit 127), then sent the bad
assistant tool_call back on the next request: every endpoint answered 500
"Failed to parse tool call arguments as JSON" and junction cooled all four.
Arguments that start with '{' must parse now. If they do not, the reply is
freed without adding the assistant message or running a tool; the request is
retried up to three times and then fails with "model sent malformed tool
arguments". A bare string (mock/script shape) is still allowed.
test_agent drives a transport with exactly that malformed call: three requests,
zero tool calls, no "command:bad" in the conversation.
make check: all tests passed.
This commit is contained in:
1 parent
7f8493c949
commit
6b560e63a0
2 files changed
+110
-1
No files matched your search
+50
-1
@@ -31,6 +31,7 @@
|
||||
#include "splinter_plugin.h"
|
||||
#include "ack.h"
|
||||
#include "log.h"
|
||||
#include "mcp.h"
|
||||
#include "usage.h"
|
||||
|
||||
/*
|
||||
@@ -596,13 +597,41 @@ agent_request(struct sp_agent *a, struct sp_conv *conv, struct sp_reply *reply,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Tool arguments that start with an object must be valid JSON. A bare string
|
||||
* is the mock/script shape and stays allowed. One model step with "{command:"
|
||||
* reached the endpoint as an assistant tool_call and made every endpoint
|
||||
* answer 500; validation happens before that message enters the conversation.
|
||||
*/
|
||||
static int
|
||||
tool_args_valid(const struct sp_reply *r, size_t *bad)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < r->ncalls; i++) {
|
||||
const char *args = r->calls[i].args;
|
||||
mcp_json *v;
|
||||
|
||||
if (args == NULL || args[0] == '\0' || args[0] != '{')
|
||||
continue;
|
||||
v = mcp_parse(args);
|
||||
if (v == NULL) {
|
||||
if (bad != NULL)
|
||||
*bad = i;
|
||||
return 0;
|
||||
}
|
||||
mcp_json_free(v);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
agent_loop(struct sp_agent *a, struct sp_conv *conv, int resume, int decision,
|
||||
struct sp_buf *answer, struct sp_buf *err)
|
||||
{
|
||||
struct sp_buf obs;
|
||||
size_t step, i;
|
||||
int rc = -1, limit_tries = 0;
|
||||
int rc = -1, limit_tries = 0, bad_tool_tries = 0;
|
||||
|
||||
if (sp_buf_init(&obs) != 0) {
|
||||
sp_buf_puts(err, "agent: out of memory");
|
||||
@@ -787,6 +816,26 @@ agent_loop(struct sp_agent *a, struct sp_conv *conv, int resume, int decision,
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (reply.ncalls > 0) {
|
||||
size_t bad = 0;
|
||||
|
||||
if (!tool_args_valid(&reply, &bad)) {
|
||||
sp_logf("agent", "malformed tool arguments for "
|
||||
"%s: %.240s", reply.calls[bad].name,
|
||||
reply.calls[bad].args != NULL ?
|
||||
reply.calls[bad].args : "");
|
||||
sp_buf_clear(err);
|
||||
sp_reply_free(&reply);
|
||||
if (++bad_tool_tries >= 3) {
|
||||
sp_buf_puts(err, "agent: model sent "
|
||||
"malformed tool arguments");
|
||||
goto out;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
bad_tool_tries = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The reply asks for a tool round. 'step' requests have been sent
|
||||
* so far, the first max_steps of which were allowed to run one
|
||||
|
||||
@@ -87,6 +87,33 @@ flaky_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* a reply whose tool call carries arguments that are not JSON */
|
||||
static int bad_args_calls;
|
||||
static int
|
||||
bad_args_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
||||
struct sp_buf *err)
|
||||
{
|
||||
(void)ctx;
|
||||
(void)conv;
|
||||
bad_args_calls++;
|
||||
memset(out, 0, sizeof(*out));
|
||||
out->status = 200;
|
||||
out->content = strdup("");
|
||||
out->ncalls = 1;
|
||||
out->calls = calloc(1, sizeof(*out->calls));
|
||||
if (out->content == NULL || out->calls == NULL)
|
||||
return -1;
|
||||
out->calls[0].id = strdup("c1");
|
||||
out->calls[0].name = strdup("echo");
|
||||
out->calls[0].args = strdup("{command:bad}");
|
||||
out->calls_json = strdup("[{\"id\":\"c1\",\"type\":\"function\","
|
||||
"\"function\":{\"name\":\"echo\","
|
||||
"\"arguments\":\"{command:bad}\"}}]");
|
||||
(void)err;
|
||||
return out->calls[0].id != NULL && out->calls[0].name != NULL &&
|
||||
out->calls[0].args != NULL && out->calls_json != NULL ? 0 : -1;
|
||||
}
|
||||
|
||||
static char note_text[256];
|
||||
static int note_seen;
|
||||
|
||||
@@ -325,6 +352,39 @@ main(void)
|
||||
(void)unsetenv("SPLINTER_RETRY_WAIT_MS");
|
||||
}
|
||||
|
||||
/*
|
||||
* A tool call whose arguments look like an object but are not JSON
|
||||
* must not enter the conversation. It made all endpoints answer 500
|
||||
* on the next request; the loop retries the model instead and then
|
||||
* fails cleanly.
|
||||
*/
|
||||
{
|
||||
struct sp_agent a;
|
||||
struct sp_agent_config cfg;
|
||||
struct sp_transport tr;
|
||||
|
||||
memset(&a, 0, sizeof(a));
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
memset(&tr, 0, sizeof(tr));
|
||||
cfg.max_steps = 8;
|
||||
cfg.autonomous = 1;
|
||||
tr.send = bad_args_send;
|
||||
a.reg = &r;
|
||||
a.cfg = &cfg;
|
||||
a.tr = tr;
|
||||
bad_args_calls = 0;
|
||||
sp_buf_clear(&answer);
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_agent_run(&a, "bad args", &answer, &err) != 0);
|
||||
CHECK(strstr(err.p, "malformed tool arguments") != NULL);
|
||||
CHECK(bad_args_calls == 3);
|
||||
CHECK(a.stats.tool_calls == 0);
|
||||
CHECK(a.conv == NULL || a.conv->n == 0 ||
|
||||
strstr(a.conv->v[a.conv->n - 1].content != NULL ?
|
||||
a.conv->v[a.conv->n - 1].content : "", "command:bad") == NULL);
|
||||
sp_conv_free(a.conv);
|
||||
}
|
||||
|
||||
sp_buf_free(&answer);
|
||||
sp_buf_free(&err);
|
||||
sp_registry_free(&r);
|
||||
|
||||
Reference in new issue
Block a user