From 9e13c3eb642c231a14d5385d4190657686d04559 Mon Sep 17 00:00:00 2001 From: lotherk Date: Wed, 18 Nov 2026 21:42:13 +0000 Subject: [PATCH] agent: show the provider's 4xx body and honour Retry-After A 4xx is the request's answer, not a transient fault, but the turn loop printed only the number. The body is what says why (a rejected schema, a bad tool call, a quota message), so include it in the error for 4xx. When junction answers with Retry-After, wait at least that long instead of our own backoff; cap it at two minutes so a hostile header cannot stall a turn. The wire value is already parsed into reply->retry_after_s. make check: all tests passed (clean HEAD + this change). --- src/backend/agent.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/backend/agent.c b/src/backend/agent.c index afaddb6..b228c60 100644 --- a/src/backend/agent.c +++ b/src/backend/agent.c @@ -553,8 +553,13 @@ agent_request(struct sp_agent *a, struct sp_conv *conv, struct sp_reply *reply, (reply->retry_after_s > 0 ? reply->retry_after_s : 300), "refused (HTTP limit)"); - sp_buf_printf(err, "agent: transport status %ld", - reply->status); + if (reply->status >= 400 && reply->status < 500 && + reply->content != NULL && reply->content[0] != '\0') + sp_buf_printf(err, "agent: transport status %ld: " + "%.400s", reply->status, reply->content); + else + sp_buf_printf(err, "agent: transport status %ld", + reply->status); } if (limit_retry(a, err, reply->status, reply->retry_after_s, 0, limit_tries)) { @@ -570,21 +575,31 @@ agent_request(struct sp_agent *a, struct sp_conv *conv, struct sp_reply *reply, if (attempt < SP_NET_RETRIES && !guard.produced && ((sent_rc != 0 && reply->status < 400) || reply->status >= 500)) { + int wait = retry_wait_ms(); + + /* junction's Retry-After is the better guess than ours */ + if (reply->retry_after_s > 0) { + long ra = reply->retry_after_s * 1000; + + if (ra > 120000) + ra = 120000; + if (ra > wait) + wait = (int)ra; + } sp_logf("agent", "retry %d/%d in %dms: %s", - attempt + 1, SP_NET_RETRIES, retry_wait_ms(), + attempt + 1, SP_NET_RETRIES, wait, err->p != NULL ? err->p : "?"); if (a->cfg->note != NULL) { char line[300]; snprintf(line, sizeof(line), sp_td("ui.retry_note", - "retrying in %d s: %s"), - retry_wait_ms() / 1000, + "retrying in %d s: %s"), wait / 1000, err->p != NULL ? err->p : "no answer"); a->cfg->note(a->cfg->note_ctx, line); } sp_buf_clear(err); sp_reply_free(reply); - if (!retry_sleep(a, retry_wait_ms())) { + if (!retry_sleep(a, wait)) { sp_buf_puts(err, "agent: cancelled by operator"); return -1;