diff --git a/docs/CONFIG.md b/docs/CONFIG.md index a3d99ec..db1c4a6 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -91,7 +91,9 @@ is benched until that reset, and the same task goes to the next delegate that fits (the request's tags plus the benched one's). The bench is a file, `~/.splinter/cooldown/` holding the epoch, so every splinter on the machine skips it, even when it is asked for by name; delete the file to unbench early. -A reset that cannot be read benches for an hour. Only when nothing else fits +A reset that cannot be read benches for an hour. In `delegate_parallel` the +task moves to the next delegate in its own slot while the others keep running, +unless that one is already at its `max_parallel`. Only when nothing else fits does the job wait (`waiting_quota`) for the reset. ## Example: local model diff --git a/src/delegate.c b/src/delegate.c index d67ec91..5b0d83b 100644 --- a/src/delegate.c +++ b/src/delegate.c @@ -1170,6 +1170,8 @@ struct job { int score; char why[160]; char task[256]; + char tags[128]; /* as asked, for a fallback's choice */ + char note[256]; /* benched / fell back, for the header */ int done; long start_ms; long end_ms; @@ -1243,6 +1245,66 @@ spawn_job(const struct del_ctx *c, const struct sp_delegate *d, return 0; } +/* + * A parallel task whose delegate answered with its limit: bench that delegate + * and start the same task in the same slot on the next one that fits, while + * the others keep running. Not taken when the fallback is already at its + * max_parallel among the running jobs - then the limit error stands. Returns 1 + * when the task runs again. + */ +static int +par_fallback(struct del_ctx *c, struct job *jobs, size_t njobs, size_t i, + struct jobrec *jr, struct sp_buf *err) +{ + struct job *j = &jobs[i]; + const struct sp_delegate *next; + char want[300], why[160], when[32]; + long until; + int score; + size_t k, same = 0, nl; + time_t tt; + struct tm tm; + + if (j->out.p == NULL || !looks_limit(j->out.p, &until)) + return 0; + 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)); + nl = strlen(j->note); + snprintf(j->note + nl, sizeof(j->note) - nl, + "[%s at its limit, benched until %s]", j->d->id, when); + snprintf(want, sizeof(want), "%s,%s", j->tags, j->d->tags); + next = choose(c->cfg, jr->task.p != NULL ? jr->task.p : j->task, want, + &score, why, sizeof(why)); + if (next == NULL || next == j->d) + return 0; + for (k = 0; k < njobs; k++) + if (k != i && !jobs[k].done && jobs[k].d == next) + same++; + if ((int)same >= next->max_parallel) { + nl = strlen(j->note); + snprintf(j->note + nl, sizeof(j->note) - nl, + "[%s busy, no fallback]", next->id); + return 0; + } + sp_buf_clear(err); + if (spawn_job(c, next, jr->task.p != NULL ? jr->task.p : j->task, j, + err) != 0) + return 0; + sp_buf_clear(&j->out); + nl = strlen(j->note); + snprintf(j->note + nl, sizeof(j->note) - nl, "[falling back to %s]", + next->id); + j->d = next; + j->score = score; + snprintf(j->why, sizeof(j->why), "%s", why); + snprintf(jr->delegate_id, sizeof(jr->delegate_id), "%s", next->id); + jr->pid = (int)j->pid; + jr->attempts++; + jobrec_write(c, jr); + return 1; +} + static int tool_delegate_parallel(const char *args, struct sp_buf *out, void *ctx) { @@ -1303,6 +1365,8 @@ tool_delegate_parallel(const char *args, struct sp_buf *out, void *ctx) } } jobs[njobs].d = d; + snprintf(jobs[njobs].tags, sizeof(jobs[njobs].tags), "%s", + tags != NULL ? tags : ""); jobrec_init(&jrec[njobs]); gen_job_id(jrec[njobs].id, sizeof(jrec[njobs].id)); snprintf(jids[njobs], sizeof(jids[njobs]), "%s", jrec[njobs].id); @@ -1405,6 +1469,12 @@ tool_delegate_parallel(const char *args, struct sp_buf *out, void *ctx) WEXITSTATUS(st) : -1; close(jobs[i].fd); jobs[i].fd = -1; + /* at its limit: same slot, next delegate */ + if (jobs[i].status != 0 && par_fallback(c, jobs, + njobs, i, &jrec[i], &err)) { + jobs[i].end_ms = 0; + continue; + } jobs[i].done = 1; running_now--; { @@ -1466,6 +1536,8 @@ tool_delegate_parallel(const char *args, struct sp_buf *out, void *ctx) jobs[i].score, took, jobs[i].status, jobs[i].out.len, jobs[i].why[0] != '\0' ? " | " : "", jobs[i].why); + if (jobs[i].note[0] != '\0') + sp_buf_printf(out, "%s\n", jobs[i].note); sp_buf_printf(out, "> %s\n\n", jobs[i].task); if (jobs[i].out.p != NULL) copy_answer(out, &jobs[i].out); diff --git a/tests/test_delegate.c b/tests/test_delegate.c index 3bed551..fd218b7 100644 --- a/tests/test_delegate.c +++ b/tests/test_delegate.c @@ -275,6 +275,35 @@ main(void) fclose(f); } + /* + * parallel: the spent one fails mid-run, the task moves to the + * next one in the same slot, the other task is not disturbed + */ + snprintf(line, sizeof(line), "rm -rf '%s'", cool); + (void)system(line); + sp_buf_free(&out); + sp_registry_free(&r); + add(&c, "other", "unrelated", "small"); + snprintf(c.delegate[2].host, sizeof(c.delegate[2].host), "fresh"); + c.delegate[2].max_parallel = 1; + memset(&r, 0, sizeof(r)); + CHECK(sp_delegate_register(&r, &c, NULL) == 0); + t = sp_registry_find(&r, "delegate_parallel"); + CHECK(t != NULL); + CHECK(sp_buf_init(&out) == 0); + if (t != NULL) + (void)t->run("{\"tasks\":[{\"task\":\"p1\",\"tags\":\"big\"}," + "{\"task\":\"p2\",\"tags\":\"small\"}]}", &out, t->ctx); + if (getenv("TEST_VERBOSE") != NULL) + fprintf(stderr, "parallel said: %s\n", out.p); + CHECK(out.p != NULL && strstr(out.p, "[claude0 at its limit") != NULL); + CHECK(out.p != NULL && strstr(out.p, "[falling back to claude1]") != NULL); + CHECK(out.p != NULL && strstr(out.p, "model claude1") != NULL); + CHECK(out.p != NULL && strstr(out.p, "model other") != NULL); + CHECK(out.p != NULL && strstr(out.p, "exit 1") == NULL); + snprintf(path, sizeof(path), "%s/claude0", cool); + CHECK(access(path, F_OK) == 0); + sp_buf_free(&out); sp_registry_free(&r); (void)setenv("PATH", oldpath != NULL ? oldpath : "/usr/bin:/bin", 1);