diff --git a/README.md b/README.md index fb3078c..544bdb1 100644 --- a/README.md +++ b/README.md @@ -241,10 +241,22 @@ never on a command line, never into a log. Parallel handover uses `delegate_parallel` with a list of tasks - each picks its own model, up to eight run at once, and the answers come back labelled: - [parallel: 2 tasks, 12 s] - ## d1 (score 5, requested tag 'a') + [parallel: 2 task(s), 12018 ms wall] + + ## task 1 | model slow | score 5 | 12018 ms | exit 0 | 75 B | requested tag 'slow' + > sag A ... - ## d2 ... + + ## task 2 | model fast | score 5 | 16 ms | exit 0 | 74 B | requested tag 'fast' + ... + + ## summary + - slow: 1 task(s), 12018 ms total + - fast: 1 task(s), 16 ms total + +The planner gets who did what, how long it took and whether it worked - not a wall +of answers. A delegate that is slow or failing is visible in the header before +anybody reads its prose. `max_parallel` still applies: a delegate that is full skips the task with a note instead of queueing it. diff --git a/libsplinter.so b/libsplinter.so index cd3b51e..d67e907 100755 Binary files a/libsplinter.so and b/libsplinter.so differ diff --git a/src/delegate.c b/src/delegate.c index a6051fe..312eab4 100644 --- a/src/delegate.c +++ b/src/delegate.c @@ -18,6 +18,7 @@ */ #include #include +#include #include #include #include @@ -380,10 +381,42 @@ struct job { const struct sp_delegate *d; int score; char why[160]; + char task[256]; int done; - long started; + long start_ms; + long end_ms; + int status; }; +static long +now_ms(void) +{ + struct timespec ts; + + (void)clock_gettime(CLOCK_MONOTONIC, &ts); + return (long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; +} + +/* Drop the child's own log lines; they are diagnostics, not an answer. */ +static void +copy_answer(struct sp_buf *dst, const struct sp_buf *src) +{ + const char *p; + + if (src->p == NULL) + return; + for (p = src->p; *p != '\0'; ) { + const char *nl = strchr(p, '\n'); + size_t len = nl != NULL ? (size_t)(nl - p) : strlen(p); + + if (!(len >= 9 && strncmp(p, "splinter:", 9) == 0)) + (void)sp_buf_add(dst, p, len + (nl != NULL ? 1 : 0)); + if (nl == NULL) + break; + p = nl + 1; + } +} + /* * Spawn without waiting: the same argv a single delegation builds, but the pipe * comes back to the caller so eight of them can run at the same time. @@ -463,6 +496,8 @@ spawn_job(const struct del_ctx *c, const struct sp_delegate *d, _exit(127); } close(fds[1]); + /* non-blocking, so a finished child is noticed at once */ + (void)fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL, 0) | O_NONBLOCK); j->pid = child; j->fd = fds[0]; return 0; @@ -530,7 +565,10 @@ tool_delegate_parallel(const char *args, struct sp_buf *out, void *ctx) sp_buf_free(&jobs[njobs].out); continue; } - jobs[njobs].started = (long)time(NULL); + snprintf(jobs[njobs].task, sizeof(jobs[njobs].task), "%s", task); + jobs[njobs].start_ms = now_ms(); + jobs[njobs].end_ms = 0; + jobs[njobs].status = -1; njobs++; running_now++; } @@ -542,7 +580,7 @@ tool_delegate_parallel(const char *args, struct sp_buf *out, void *ctx) } /* collect: poll every pipe, nobody waits for the one in front of them */ - start = (long)time(NULL); + start = now_ms(); while (running_now > 0) { struct pollfd pfd[PAR_MAX_TASKS]; int nfds = 0, ready; @@ -560,7 +598,7 @@ tool_delegate_parallel(const char *args, struct sp_buf *out, void *ctx) ready = poll(pfd, (nfds_t)nfds, 1000); if (ready < 0 && errno != EINTR) break; - if ((long)time(NULL) - start > DELEGATE_TIMEOUT_MS / 1000) { + if (now_ms() - start > DELEGATE_TIMEOUT_MS) { sp_buf_puts(out, "[parallel: timeout, killing the rest]\n"); for (i = 0; i < njobs; i++) if (!jobs[i].done) @@ -569,39 +607,100 @@ tool_delegate_parallel(const char *args, struct sp_buf *out, void *ctx) } if (ready <= 0) continue; + /* + * Drain everything that is there, then ask the kernel whether the + * child is gone. Relying on POLLHUP alone left fast jobs marked + * "done" only when the slow one finished - every task reported the + * same duration, which is exactly the number the planner is supposed + * to use to tell a fast model from a slow one. + */ for (i = 0; i < njobs; i++) { char chunk[4096]; ssize_t got; if (jobs[i].done || jobs[i].fd < 0) continue; - got = read(jobs[i].fd, chunk, sizeof(chunk)); - if (got > 0) { - if (jobs[i].out.len + (size_t)got <= - DELEGATE_MAX_BYTES) - (void)sp_buf_add(&jobs[i].out, chunk, - (size_t)got); - continue; + for (;;) { + got = read(jobs[i].fd, chunk, sizeof(chunk)); + if (got > 0) { + if (jobs[i].out.len + (size_t)got <= + DELEGATE_MAX_BYTES) + (void)sp_buf_add(&jobs[i].out, + chunk, (size_t)got); + continue; + } + break; + } + { + int st = 0; + pid_t reaped = waitpid(jobs[i].pid, &st, WNOHANG); + + if (reaped != jobs[i].pid) + continue; /* still running */ + jobs[i].end_ms = now_ms(); + jobs[i].status = WIFEXITED(st) ? + WEXITSTATUS(st) : -1; + close(jobs[i].fd); + jobs[i].fd = -1; + jobs[i].done = 1; + running_now--; } - /* EOF or error: this one is finished */ - close(jobs[i].fd); - jobs[i].fd = -1; - jobs[i].done = 1; - (void)waitpid(jobs[i].pid, NULL, 0); - running_now--; } } - sp_buf_printf(out, "[parallel: %zu tasks, %ld s]\n", njobs, - (long)time(NULL) - start); + /* + * The planner needs to know who did what and how long it took - not just + * a wall of answers. One header per task, then a summary with the totals + * per model, so a slow or failing delegate is visible without reading + * every word it produced. + */ + sp_buf_printf(out, "[parallel: %zu task(s), %ld ms wall]\n", njobs, + now_ms() - start); for (i = 0; i < njobs; i++) { - sp_buf_printf(out, "\n## %s (score %d%s%s)\n", jobs[i].d->id, - jobs[i].score, jobs[i].why[0] != '\0' ? ", " : "", - jobs[i].why); - sp_buf_printf(out, "%s\n", jobs[i].out.p != NULL ? - jobs[i].out.p : "(no answer)"); + long took = jobs[i].end_ms > 0 ? + jobs[i].end_ms - jobs[i].start_ms : 0; + + sp_buf_printf(out, "\n## task %zu | model %s | score %d | %ld ms | " + "exit %d | %zu B%s%s\n", i + 1, jobs[i].d->id, + jobs[i].score, took, jobs[i].status, + jobs[i].out.len, + jobs[i].why[0] != '\0' ? " | " : "", jobs[i].why); + sp_buf_printf(out, "> %s\n\n", jobs[i].task); + if (jobs[i].out.p != NULL) + copy_answer(out, &jobs[i].out); + else + sp_buf_puts(out, "(no answer)\n"); sp_buf_free(&jobs[i].out); } + { + size_t k; + + sp_buf_puts(out, "\n## summary\n"); + for (k = 0; k < njobs; k++) { + size_t m, count = 0; + long total = 0; + + /* one line per model, accumulated */ + { + size_t earlier = 0; + + for (m = 0; m < k; m++) + if (jobs[m].d == jobs[k].d) + earlier++; + if (earlier > 0) + continue; + } + for (m = 0; m < njobs; m++) { + if (jobs[m].d != jobs[k].d) + continue; + count++; + total += jobs[m].end_ms > 0 ? + jobs[m].end_ms - jobs[m].start_ms : 0; + } + sp_buf_printf(out, "- %s: %zu task(s), %ld ms total\n", + jobs[k].d->id, count, total); + } + } sp_buf_free(&err); return 0; }