diff --git a/src/review.c b/src/review.c index 39a2eba..cd1a0fa 100644 --- a/src/review.c +++ b/src/review.c @@ -245,20 +245,85 @@ int j_review_nonstream(const char *body, char **out, size_t *outlen) return 0; } +/* A review is a new answer, not a one-token continuation. If the client asked for a + * tiny output budget, the reasoning of a #max model can eat it all and leave the + * review with empty content. Give the review rounds a usable floor; the original + * worker call still uses exactly the client's budget. */ +#define REVIEW_MIN_TOKENS 1024 + +static int raise_max_tokens(const char *body, char **out, size_t *outlen) +{ + const char *end = body + strlen(body); + const char *v = find_top_value(body, end, "max_tokens"); + if (!v) v = find_top_value(body, end, "max_completion_tokens"); + if (v) { + v = skip_ws(v, end); + char *num_end = NULL; + long n = strtol(v, &num_end, 10); + if (num_end != v && n >= REVIEW_MIN_TOKENS) { + size_t len = strlen(body); + char *copy = malloc(len + 1); + if (!copy) return -1; + memcpy(copy, body, len + 1); + *out = copy; + *outlen = len; + return 0; + } + if (num_end != v) { + j_buf b; + j_buf_init(&b); + char num[32]; + int nl = snprintf(num, sizeof num, "%d", REVIEW_MIN_TOKENS); + if (j_buf_append(&b, body, (size_t)(v - body)) != 0 || + j_buf_append(&b, num, (size_t)nl) != 0 || + j_buf_append(&b, num_end, strlen(num_end)) != 0) { + j_buf_free(&b); + return -1; + } + *out = b.data; + *outlen = b.len; + return 0; + } + } + + /* No usable max_tokens: add one before the top-level closing brace. */ + const char *close = end; + while (close > body && isspace((unsigned char)close[-1])) close--; + if (close <= body || close[-1] != '}') return -1; + close--; + j_buf b; + j_buf_init(&b); + char ins[48]; + int n = snprintf(ins, sizeof ins, ",\"max_tokens\":%d", REVIEW_MIN_TOKENS); + if (j_buf_append(&b, body, (size_t)(close - body)) != 0 || + j_buf_append(&b, ins, (size_t)n) != 0 || + j_buf_append(&b, close, strlen(close)) != 0) { + j_buf_free(&b); + return -1; + } + *out = b.data; + *outlen = b.len; + return 0; +} + int j_review_body(const char *body, const char *answer, char **out, size_t *outlen) { *out = NULL; *outlen = 0; if (!body || !answer) return -1; + char *raised = NULL; + size_t raised_len = 0; + if (raise_max_tokens(body, &raised, &raised_len) != 0) return -1; + body = raised; const char *end = body + strlen(body); const char *arr = find_top_value(body, end, "messages"); - if (!arr) return -1; + if (!arr) { free(raised); return -1; } arr = skip_ws(arr, end); - if (arr >= end || *arr != '[') return -1; + if (arr >= end || *arr != '[') { free(raised); return -1; } const char *arr_end = skip_value(arr, end); - if (!arr_end) return -1; + if (!arr_end) { free(raised); return -1; } const char *close = arr_end - 1; /* the closing ']' */ - if (close < arr || *close != ']') return -1; + if (close < arr || *close != ']') { free(raised); return -1; } const char *inner = skip_ws(arr + 1, close); int empty = (inner >= close); @@ -275,10 +340,12 @@ int j_review_body(const char *body, const char *answer, char **out, size_t *outl j_buf_append(&b, "}", 1) != 0 || j_buf_append(&b, close, strlen(close)) != 0) { j_buf_free(&b); + free(raised); return -1; } *out = b.data; *outlen = b.len; + free(raised); return 0; } diff --git a/tests/review.sh b/tests/review.sh index 4f8b1a9..f87326a 100644 --- a/tests/review.sh +++ b/tests/review.sh @@ -57,6 +57,8 @@ contains "$(cat "$TMP/review-dump.txt")" "first answer" \ "the first answer was fed back into the first review" contains "$(cat "$TMP/review-dump.txt")" "second answer" \ "the second answer was fed back into the second review" +contains "$(cat "$TMP/review-dump.txt")" '"max_tokens":1024' \ + "the review rounds get a usable output budget" RH=$(tr -d '\r' < "$TMP/review_h.txt") contains "$RH" "X-Junction-Review: 2" "the response header names the review count" contains "$RH" "X-Junction-Review-Rounds: 2" "the response header names the completed rounds"