diff --git a/src/backend/delegate.c b/src/backend/delegate.c index 81139b3..5433df3 100644 --- a/src/backend/delegate.c +++ b/src/backend/delegate.c @@ -1102,6 +1102,13 @@ jobrec_read(const struct del_ctx *c, const char *id, struct jobrec *j) */ #define COOLDOWN_DEFAULT 3600 +/* + * The longest bench that is still a bench. A reset further out than this is + * not a reset: the value came from a provider's text or from a file, and an + * accepted 9223372036854775807 would skip that delegate for ever. + */ +#define LIMIT_MAX_AHEAD (370L * 86400L) + static void cooldown_path(const char *id, char *buf, size_t len) { @@ -1115,13 +1122,20 @@ cooldown_path(const char *id, char *buf, size_t len) home != NULL ? home : "/tmp", id); } -/* the epoch the delegate is benched until, 0 when it is free */ +/* + * the epoch the delegate is benched until, 0 when it is free. + * + * The file is not ours alone: it is plain text in a directory other splinter + * runs (and a person) can write, so a value that is not a time is not a bench + * either. Handing LONG_MAX back would wedge the delegate for good - choose() + * skips it and says nothing. + */ long sp_cooldown_until(const char *id) { char path[700]; FILE *f; - long until = 0; + long until = 0, now = (long)time(NULL); cooldown_path(id, path, sizeof(path)); f = fopen(path, "r"); @@ -1130,7 +1144,7 @@ sp_cooldown_until(const char *id) if (fscanf(f, "%ld", &until) != 1) until = 0; fclose(f); - if (until <= (long)time(NULL)) { + if (until <= now || until > now + LIMIT_MAX_AHEAD) { (void)unlink(path); return 0; } @@ -1280,7 +1294,15 @@ has_status_429(const char *e) return 0; } -/* Is this error a limit, and until when? Returns 1 and sets *until. */ +/* + * Is this error a limit, and until when? Returns 1 and sets *until. + * + * The value comes out of text a provider sent, so it is not trusted to be a + * time: "ready_at=9223372036854775807" was accepted as-is, written to the + * cooldown file, and benched the delegate until the year 292 billion - not + * one call, every call, and choose() skips a benched delegate without a word. + * A reset beyond LIMIT_MAX_AHEAD is not a reset; the default bench applies. + */ int sp_limit_parse(const char *e, long *until) { @@ -1296,6 +1318,8 @@ sp_limit_parse(const char *e, long *until) has_ci(e, "quota") || strstr(e, "WAIT_QUOTA") != NULL)) return 0; t = parse_reset(e); + if (t > (long)time(NULL) + LIMIT_MAX_AHEAD) + t = 0; *until = t > 0 ? t : (long)time(NULL) + COOLDOWN_DEFAULT; return 1; } @@ -1340,8 +1364,12 @@ run_child_fb(const struct del_ctx *c, const struct sp_delegate **dp, time_t tt = (time_t)until; struct tm tm; - strftime(when, sizeof(when), "%Y-%m-%d %H:%M", - localtime_r(&tt, &tm)); + /* an unrepresentable time must not become a wrong one + * (see agent.c); the benched note is read by the operator */ + strcpy(when, "?"); + if (localtime_r(&tt, &tm) != NULL) + strftime(when, sizeof(when), + "%Y-%m-%d %H:%M", &tm); sp_buf_printf(note, "[%s at its limit, benched until %s]\n", d->id, when); } @@ -1679,7 +1707,10 @@ par_fallback(struct del_ctx *c, struct job *jobs, size_t njobs, size_t i, return 0; sp_cooldown_set(j->d->id, until, j->out.p); tt = (time_t)until; - strftime(when, sizeof(when), "%Y-%m-%d %H:%M", localtime_r(&tt, &tm)); + /* an unrepresentable time must not become a wrong one (see agent.c) */ + strcpy(when, "?"); + if (localtime_r(&tt, &tm) != NULL) + strftime(when, sizeof(when), "%Y-%m-%d %H:%M", &tm); nl = strlen(j->note); snprintf(j->note + nl, sizeof(j->note) - nl, "[%s at its limit, benched until %s]", j->d->id, when); diff --git a/tests/test_delegate.c b/tests/test_delegate.c index 6b0aec5..b778214 100644 --- a/tests/test_delegate.c +++ b/tests/test_delegate.c @@ -153,6 +153,60 @@ main(void) CHECK(sp_limit_parse("built 14290 objects", &until) == 0); CHECK(sp_limit_parse("exit 1, 429 tests passed", &until) == 0); } + + /* + * ---- a limit's reset is text a provider sent, not a time ---- + * "ready_at=9223372036854775807" used to be taken as-is: the delegate + * was benched until the year 292 billion, and choose() skips a benched + * one without a word. A reset has to be a plausible reset, or the + * default bench applies. + */ + { + long until = 0, now = (long)time(NULL); + char cool[64] = "/tmp/splinter-limit-XXXXXX"; + char path[128]; + FILE *f; + + CHECK(mkdtemp(cool) != NULL); + CHECK(sp_limit_parse("429 rate limit reached " + "ready_at=9223372036854775807", &until) == 1); + CHECK(until > now); + CHECK(until <= now + 2 * 3600); /* the default bench, not 292e9 */ +#ifdef LONG_MAX + CHECK(until != (long)LONG_MAX); +#endif + /* the same text with a plausible reset keeps that reset */ + CHECK(sp_limit_parse("429 rate limit ready_at=", &until) == 1); + { + char msg[128]; + long want = now + 7200; + + snprintf(msg, sizeof(msg), + "429 rate limit ready_at=%ld", want); + CHECK(sp_limit_parse(msg, &until) == 1); + CHECK(until == want); + } + + /* + * The cooldown file is plain text any run (or a person) can + * write. A value that is not a time must not bench for ever. + */ + (void)setenv("SPLINTER_COOLDOWN_DIR", cool, 1); + snprintf(path, sizeof(path), "%s/claude0", cool); + f = fopen(path, "w"); + CHECK(f != NULL); + if (f != NULL) { + fprintf(f, "9223372036854775807\nno time at all\n"); + fclose(f); + } + CHECK(sp_cooldown_until("claude0") == 0); + /* a plausible bench is still honoured */ + sp_cooldown_set("claude0", now + 1800, "a real reset"); + CHECK(sp_cooldown_until("claude0") > now); + (void)unsetenv("SPLINTER_COOLDOWN_DIR"); + snprintf(path, sizeof(path), "rm -rf '%s'", cool); + (void)system(path); + } memset(&c, 0, sizeof(c)); add(&c, "coder", "C99, POSIX, kennt den Quellbaum", "code,c99,review"); add(&c, "quick", "kurze Antworten, Chat", "chat,cheap,summary");