review: n>1 und Content-Arrays unverändert durchreichen, stream_options entfernen
Der Review-Pfad erzwang stream:false, ließ aber stream_options stehen; strenge Provider lehnen diese Kombination ab. j_review_nonstream entfernt das Feld jetzt JSON-genau (mit Komma), bevor es stream auf false setzt. n>1: mehrere Choices sind keine eine Antwort, die geprüft werden könnte. Solche Anfragen werden vor der ersten Runde erkannt und unverändert weitergeleitet; Header und Log nennen X-Junction-Review-Skipped: n>1. Der Stub beantwortet n jetzt mit n Choices, damit der Test alle Choices und die eine Provider-Anfrage prüfen kann. Content-Arrays: j_review_extract erkennt content als Liste und meldet "content_array" statt still "no_text"; die Originalantwort bleibt unverändert, Header und Log nennen den Grund. Regressionstests in review.sh decken n>1, Content-Arrays und stream_options ab (inkl. gültigem JSON beider Upstream-Bodies). Damit sind auch die Tests für Pool-Live-Regeln, Original-4xx und das Review-Budget aus t0471/t0474/t0475 zusammen abgesichert. Tests: 453 OK, 0 FAIL.
This commit is contained in:
1 parent
6d3547e794
commit
fd3226c09b
6 files changed
+300
-26
No files matched your search
+25
-6
@@ -3417,7 +3417,8 @@ static int review_serve(j_conn *client, j_endpoint *ep, const j_req *rq,
|
||||
|
||||
char *answer = NULL;
|
||||
int tools = 0;
|
||||
int have_answer = (j_review_extract(cap.data, &answer, &tools) == 0 &&
|
||||
const char *why = NULL;
|
||||
int have_answer = (j_review_extract(cap.data, &answer, &tools, &why) == 0 &&
|
||||
answer != NULL && answer[0] != '\0');
|
||||
char *current = have_answer ? answer : NULL;
|
||||
if (!have_answer) free(answer);
|
||||
@@ -3426,7 +3427,10 @@ static int review_serve(j_conn *client, j_endpoint *ep, const j_req *rq,
|
||||
snprintf(t_review_skipped, sizeof t_review_skipped, "tool_calls");
|
||||
jlog("review", "round=0 endpoint=%s skipped=tool_calls", ep->name);
|
||||
} else if (!have_answer) {
|
||||
snprintf(t_review_skipped, sizeof t_review_skipped, "no_text");
|
||||
snprintf(t_review_skipped, sizeof t_review_skipped, "%s",
|
||||
why ? why : "no_text");
|
||||
jlog("review", "round=0 endpoint=%s skipped=%s", ep->name,
|
||||
t_review_skipped);
|
||||
}
|
||||
|
||||
char *last_json = strdup(cap.data ? cap.data : "");
|
||||
@@ -3464,12 +3468,14 @@ static int review_serve(j_conn *client, j_endpoint *ep, const j_req *rq,
|
||||
}
|
||||
char *next = NULL;
|
||||
int next_tools = 0;
|
||||
if (j_review_extract(cap.data, &next, &next_tools) != 0 ||
|
||||
const char *next_why = NULL;
|
||||
if (j_review_extract(cap.data, &next, &next_tools, &next_why) != 0 ||
|
||||
next == NULL || next[0] == '\0' || next_tools) {
|
||||
snprintf(t_review_skipped, sizeof t_review_skipped, "%s",
|
||||
next_tools ? "tool_calls" : "no_text");
|
||||
const char *reason = next_tools ? "tool_calls"
|
||||
: (next_why ? next_why : "no_text");
|
||||
snprintf(t_review_skipped, sizeof t_review_skipped, "%s", reason);
|
||||
jlog("review", "round=%d endpoint=%s skipped=%s", round, ep->name,
|
||||
next_tools ? "tool_calls" : "no_text");
|
||||
reason);
|
||||
free(next);
|
||||
break;
|
||||
}
|
||||
@@ -4344,6 +4350,19 @@ static void handle_client_inner(j_conn *client)
|
||||
snprintf(t_review_skipped, sizeof t_review_skipped, "protocol");
|
||||
jlog("review_skipped", "endpoint=%s reason=protocol", ep->name);
|
||||
}
|
||||
if (can_review) {
|
||||
/* Multiple choices cannot be reviewed as one answer; forward them
|
||||
* untouched. The header still names the configured review so a client
|
||||
* sees why nothing was reviewed. */
|
||||
const char *skip = j_review_skip_reason(rq.body);
|
||||
if (skip) {
|
||||
can_review = 0;
|
||||
t_review_count = rule->review_count;
|
||||
t_review_done = 0;
|
||||
snprintf(t_review_skipped, sizeof t_review_skipped, "%s", skip);
|
||||
jlog("review_skipped", "endpoint=%s reason=%s", ep->name, skip);
|
||||
}
|
||||
}
|
||||
int rc = can_review
|
||||
? review_serve(client, ep, &rq, send_body, send_model, eff_model,
|
||||
rule->review_count, streaming, started,
|
||||
|
||||
+118
-11
@@ -213,35 +213,116 @@ static int json_unescape(const char *s, size_t len, char *out, size_t cap)
|
||||
return (int)o;
|
||||
}
|
||||
|
||||
/* Remove one top-level member (key, value and one separating comma) from a JSON
|
||||
* object. Returns a malloc'ed copy; a body without the key is copied unchanged.
|
||||
* The review path forces stream:false, but stream_options is only valid together
|
||||
* with a streaming request — strict providers reject the pair. */
|
||||
static int remove_top_member(const char *body, const char *key,
|
||||
char **out, size_t *outlen)
|
||||
{
|
||||
*out = NULL;
|
||||
*outlen = 0;
|
||||
const char *end = body + strlen(body);
|
||||
const char *p = skip_ws(body, end);
|
||||
if (p >= end || *p != '{') return -1;
|
||||
p++;
|
||||
|
||||
const char *key_start = NULL, *val_end = NULL;
|
||||
for (;;) {
|
||||
p = skip_ws(p, end);
|
||||
if (p >= end || *p == '}') break;
|
||||
if (*p != '"') return -1;
|
||||
const char *ks = p;
|
||||
const char *ke = skip_string(p, end);
|
||||
if (!ke) return -1;
|
||||
size_t klen = (size_t)((ke - 1) - (ks + 1));
|
||||
p = skip_ws(ke, end);
|
||||
if (p >= end || *p != ':') return -1;
|
||||
p = skip_ws(p + 1, end);
|
||||
const char *ve = skip_value(p, end);
|
||||
if (!ve) return -1;
|
||||
if (klen == strlen(key) && !memcmp(ks + 1, key, klen)) {
|
||||
key_start = ks;
|
||||
val_end = ve;
|
||||
break;
|
||||
}
|
||||
p = skip_ws(ve, end);
|
||||
if (p < end && *p == ',') { p++; continue; }
|
||||
break;
|
||||
}
|
||||
|
||||
if (!key_start) {
|
||||
size_t n = (size_t)(end - body);
|
||||
char *copy = malloc(n + 1);
|
||||
if (!copy) return -1;
|
||||
memcpy(copy, body, n + 1);
|
||||
*out = copy;
|
||||
*outlen = n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Prefer the comma after the value. Only the last member has none; then the
|
||||
* comma before the key goes instead. The first member needs no comma at all. */
|
||||
const char *after = skip_ws(val_end, end);
|
||||
const char *cut_start = NULL, *cut_end = NULL;
|
||||
if (after < end && *after == ',') {
|
||||
cut_start = key_start;
|
||||
cut_end = after + 1;
|
||||
} else {
|
||||
const char *q = key_start;
|
||||
while (q > body && isspace((unsigned char)q[-1])) q--;
|
||||
if (q > body && q[-1] == ',') {
|
||||
cut_start = q - 1;
|
||||
cut_end = val_end;
|
||||
} else {
|
||||
cut_start = key_start;
|
||||
cut_end = val_end;
|
||||
}
|
||||
}
|
||||
|
||||
j_buf b;
|
||||
j_buf_init(&b);
|
||||
if (j_buf_append(&b, body, (size_t)(cut_start - body)) != 0 ||
|
||||
j_buf_append(&b, cut_end, strlen(cut_end)) != 0) {
|
||||
j_buf_free(&b);
|
||||
return -1;
|
||||
}
|
||||
*out = b.data;
|
||||
*outlen = b.len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int j_review_nonstream(const char *body, char **out, size_t *outlen)
|
||||
{
|
||||
*out = NULL;
|
||||
*outlen = 0;
|
||||
if (!body) return -1;
|
||||
const char *end = body + strlen(body);
|
||||
const char *v = find_top_value(body, end, "stream");
|
||||
char *clean = NULL;
|
||||
size_t clean_len = 0;
|
||||
if (remove_top_member(body, "stream_options", &clean, &clean_len) != 0)
|
||||
return -1;
|
||||
const char *end = clean + clean_len;
|
||||
const char *v = find_top_value(clean, end, "stream");
|
||||
if (v) {
|
||||
v = skip_ws(v, end);
|
||||
if (v + 4 <= end && !memcmp(v, "true", 4)) {
|
||||
j_buf b;
|
||||
j_buf_init(&b);
|
||||
if (j_buf_append(&b, body, (size_t)(v - body)) != 0 ||
|
||||
if (j_buf_append(&b, clean, (size_t)(v - clean)) != 0 ||
|
||||
j_buf_append(&b, "false", 5) != 0 ||
|
||||
j_buf_append(&b, v + 4, strlen(v + 4)) != 0) {
|
||||
j_buf_free(&b);
|
||||
free(clean);
|
||||
return -1;
|
||||
}
|
||||
free(clean);
|
||||
*out = b.data;
|
||||
*outlen = b.len;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
size_t n = strlen(body);
|
||||
char *copy = malloc(n + 1);
|
||||
if (!copy) return -1;
|
||||
memcpy(copy, body, n + 1);
|
||||
*out = copy;
|
||||
*outlen = n;
|
||||
*out = clean;
|
||||
*outlen = clean_len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -373,10 +454,28 @@ static const char *first_choice(const char *json, const char *end,
|
||||
return choice;
|
||||
}
|
||||
|
||||
int j_review_extract(const char *json, char **answer, int *tool_calls)
|
||||
/* Reviewing one answer is the whole point; with n>1 the provider returns several
|
||||
* independent answers in one response. Re-serialising the review would collapse
|
||||
* them to the first choice, so such a request is forwarded unreviewed instead. */
|
||||
const char *j_review_skip_reason(const char *body)
|
||||
{
|
||||
if (!body) return NULL;
|
||||
const char *end = body + strlen(body);
|
||||
const char *v = find_top_value(body, end, "n");
|
||||
if (!v) return NULL;
|
||||
v = skip_ws(v, end);
|
||||
char *num_end = NULL;
|
||||
long n = strtol(v, &num_end, 10);
|
||||
if (num_end == v) return NULL;
|
||||
return n > 1 ? "n>1" : NULL;
|
||||
}
|
||||
|
||||
int j_review_extract(const char *json, char **answer, int *tool_calls,
|
||||
const char **reason)
|
||||
{
|
||||
*answer = NULL;
|
||||
*tool_calls = 0;
|
||||
if (reason) *reason = "no_text";
|
||||
if (!json) return -1;
|
||||
const char *end = json + strlen(json);
|
||||
const char *cend = NULL, *msg = NULL, *mend = NULL;
|
||||
@@ -386,7 +485,15 @@ int j_review_extract(const char *json, char **answer, int *tool_calls)
|
||||
const char *content = find_top_value(msg, mend, "content");
|
||||
if (!content) return -1;
|
||||
content = skip_ws(content, mend);
|
||||
if (content >= mend || *content != '"') return -1;
|
||||
if (content >= mend) return -1;
|
||||
/* OpenAI also allows content as an array of parts (text + images). Reviewing
|
||||
* that would mean reassembling a structure; the original answer is passed
|
||||
* through unchanged instead, with a name for the reason in the log/header. */
|
||||
if (*content == '[') {
|
||||
if (reason) *reason = "content_array";
|
||||
return -1;
|
||||
}
|
||||
if (*content != '"') return -1;
|
||||
const char *s = content + 1;
|
||||
const char *cend_str = skip_string(content, mend);
|
||||
if (!cend_str) return -1;
|
||||
|
||||
+13
-4
@@ -10,18 +10,27 @@
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* Copy a request body with `stream` forced to false. Needed because the review
|
||||
* path buffers the answer and a chunked SSE body would not be parseable. */
|
||||
/* Copy a request body with `stream` forced to false and `stream_options`
|
||||
* removed. Needed because the review path buffers the answer and a chunked SSE
|
||||
* body would not be parseable; strict providers reject stream_options without
|
||||
* a streaming request. */
|
||||
int j_review_nonstream(const char *body, char **out, size_t *outlen);
|
||||
|
||||
/* Append one assistant answer and the review instruction to the messages array of
|
||||
* an already non-streaming request body. */
|
||||
int j_review_body(const char *body, const char *answer, char **out, size_t *outlen);
|
||||
|
||||
/* Is this request unsuitable for review before the first call? Returns a static
|
||||
* reason ("n>1") or NULL. Several choices cannot be reviewed as one answer. */
|
||||
const char *j_review_skip_reason(const char *body);
|
||||
|
||||
/* Extract choices[0].message.content from an OpenAI chat completion. Returns 0 and
|
||||
* a malloc'ed answer; -1 when there is no text. *tool_calls is set when the message
|
||||
* carries tool_calls, because such an answer must pass through unreviewed. */
|
||||
int j_review_extract(const char *json, char **answer, int *tool_calls);
|
||||
* carries tool_calls, because such an answer must pass through unreviewed. When the
|
||||
* return value is -1 and `reason` is not NULL, it names why ("content_array",
|
||||
* "no_text"); callers use that for the log line and the response header. */
|
||||
int j_review_extract(const char *json, char **answer, int *tool_calls,
|
||||
const char **reason);
|
||||
|
||||
/* Turn a completed OpenAI JSON response into the SSE frames the client expects:
|
||||
* one chunk carrying the raw message (text, reasoning, tool_calls), a stop chunk,
|
||||
|
||||
@@ -13,6 +13,10 @@ RT=$((PORT+282))
|
||||
RJT=$((PORT+283))
|
||||
RB=$((PORT+284))
|
||||
RBJ=$((PORT+285))
|
||||
RA=$((PORT+286))
|
||||
RAJ=$((PORT+287))
|
||||
RS=$((PORT+288))
|
||||
RSJ=$((PORT+289))
|
||||
|
||||
STUB_STATE="$TMP/review.state" STUB_ANSWERS="first answer|second answer|third answer" \
|
||||
tests/stub $RW "REVIEWER" > "$TMP/review_stub.log" 2>&1 &
|
||||
@@ -23,9 +27,17 @@ PIDS+=($!)
|
||||
STUB_STATE="$TMP/review_bad.state" STUB_ANSWERS="unused" tests/stub $RB "BADREQUEST" --status 400 \
|
||||
> "$TMP/review_bad_stub.log" 2>&1 &
|
||||
PIDS+=($!)
|
||||
STUB_STATE="$TMP/review_array.state" STUB_ANSWERS="array answer" STUB_CONTENT_ARRAY=1 \
|
||||
tests/stub $RA "ARRAYER" > "$TMP/review_array_stub.log" 2>&1 &
|
||||
PIDS+=($!)
|
||||
STUB_STATE="$TMP/review_so.state" STUB_ANSWERS="so answer|so reviewed" \
|
||||
STUB_REJECT_STREAM_OPTIONS=1 tests/stub $RS "STRICT" > "$TMP/review_so_stub.log" 2>&1 &
|
||||
PIDS+=($!)
|
||||
wait_port $RW 10 || bad "the review stub did not come up"
|
||||
wait_port $RT 10 || bad "the tool-review stub did not come up"
|
||||
wait_port $RB 10 || bad "the 400 stub did not come up"
|
||||
wait_port $RA 10 || bad "the content-array stub did not come up"
|
||||
wait_port $RS 10 || bad "the strict stub did not come up"
|
||||
|
||||
cat > "$TMP/review.conf" <<CFG
|
||||
listen 127.0.0.1:$RJ
|
||||
@@ -157,6 +169,86 @@ BCALLS=$(tr -d '\n' < "$TMP/review_bad.state")
|
||||
[ "$BCALLS" = "1" ] && ok "the streaming 400 also stopped before reviews" \
|
||||
|| bad "the streaming 400 path made '$BCALLS' provider calls"
|
||||
|
||||
# n>1: mehrere Choices sind keine eine Antwort, die geprüft werden könnte.
|
||||
# Sie müssen unverändert ankommen, und keine Review-Runde darf laufen.
|
||||
rm -f "$TMP/review.state"
|
||||
N2=$(curl -s -D "$TMP/review_n2_h.txt" -m 20 -X POST -H 'Content-Type: application/json' \
|
||||
-d '{"model":"vm-reviewed","n":2,"messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
|
||||
"http://127.0.0.1:$RJ/v1/chat/completions")
|
||||
contains "$N2" '"index":1' "n>1: both choices reach the client"
|
||||
N2H=$(tr -d '\r' < "$TMP/review_n2_h.txt")
|
||||
contains "$N2H" "X-Junction-Review-Skipped: n>1" "n>1 names the skip reason"
|
||||
contains "$N2H" "X-Junction-Review-Rounds: 0" "n>1 runs no review round"
|
||||
NCALLS=$(tr -d '\n' < "$TMP/review.state")
|
||||
[ "$NCALLS" = "1" ] && ok "n>1 makes only the original model call" \
|
||||
|| bad "n>1 made '$NCALLS' model calls"
|
||||
contains "$(cat "$TMP/review.log")" "review_skipped endpoint=worker reason=n>1" \
|
||||
"n>1 is logged with its reason"
|
||||
|
||||
# Content-Arrays: OpenAI erlaubt content als Liste von Teilen. Ein Review würde
|
||||
# die Struktur neu zusammensetzen; stattdessen bleibt die Antwort unverändert.
|
||||
cat > "$TMP/review_array.conf" <<CFG
|
||||
listen 127.0.0.1:$RAJ
|
||||
queue_timeout 10
|
||||
models allow "vm-array" "ARRAYER"
|
||||
endpoint arr { base http://127.0.0.1:$RA/v1; context 8000; timeout 10 }
|
||||
req for "vm-array" review 1 pass arr as "array-model"
|
||||
CFG
|
||||
./junction --config "$TMP/review_array.conf" > "$TMP/review_array.log" 2>&1 &
|
||||
PIDS+=($!)
|
||||
wait_port "$RAJ" 10 || bad "the content-array instance did not come up"
|
||||
ARR=$(curl -s -D "$TMP/review_array_h.txt" -m 20 -X POST -H 'Content-Type: application/json' \
|
||||
-d '{"model":"vm-array","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
|
||||
"http://127.0.0.1:$RAJ/v1/chat/completions")
|
||||
contains "$ARR" '"content":[{"type":"text","text":"array answer"}]' \
|
||||
"a content-array answer reaches the client unchanged"
|
||||
ARRH=$(tr -d '\r' < "$TMP/review_array_h.txt")
|
||||
contains "$ARRH" "X-Junction-Review-Skipped: content_array" \
|
||||
"the content-array skip names its reason"
|
||||
contains "$ARRH" "X-Junction-Review-Rounds: 0" "the content-array answer is not reviewed"
|
||||
ACALLS=$(tr -d '\n' < "$TMP/review_array.state")
|
||||
[ "$ACALLS" = "1" ] && ok "the content-array answer makes only the original call" \
|
||||
|| bad "the content-array answer made '$ACALLS' model calls"
|
||||
|
||||
# stream_options gehört zu einer Streaming-Anfrage. Der Review-Pfad erzwingt
|
||||
# stream:false und muss das Feld entfernen, sonst lehnen strenge Provider ab.
|
||||
cat > "$TMP/review_so.conf" <<CFG
|
||||
listen 127.0.0.1:$RSJ
|
||||
queue_timeout 10
|
||||
models allow "vm-so" "STRICT"
|
||||
endpoint strict { base http://127.0.0.1:$RS/v1; context 8000; timeout 10 }
|
||||
req for "vm-so" review 1 pass strict as "strict-model"
|
||||
CFG
|
||||
J_DUMP_REQ="$TMP/review_so_dump.txt" ./junction --config "$TMP/review_so.conf" \
|
||||
> "$TMP/review_so.log" 2>&1 &
|
||||
PIDS+=($!)
|
||||
wait_port "$RSJ" 10 || bad "the stream_options instance did not come up"
|
||||
SO=$(curl -s -D "$TMP/review_so_h.txt" -m 20 -X POST -H 'Content-Type: application/json' \
|
||||
-d '{"model":"vm-so","stream_options":{"include_usage":true},"messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
|
||||
"http://127.0.0.1:$RSJ/v1/chat/completions")
|
||||
contains "$SO" "so reviewed" "the review call reaches a strict provider without stream_options"
|
||||
SOH=$(tr -d '\r' < "$TMP/review_so_h.txt")
|
||||
contains "$SOH" "X-Junction-Review-Rounds: 1" "the strict provider allowed the review round"
|
||||
SOCALLS=$(tr -d '\n' < "$TMP/review_so.state")
|
||||
[ "$SOCALLS" = "2" ] && ok "the strict provider saw the original and one review call" \
|
||||
|| bad "the strict provider saw '$SOCALLS' calls instead of 2"
|
||||
if python3 - "$TMP/review_so_dump.txt" <<'PY'
|
||||
import json, sys
|
||||
raw = open(sys.argv[1]).read()
|
||||
bodies = 0
|
||||
for chunk in raw.split("--- ")[1:]:
|
||||
marker = "=== BODY ===\n"
|
||||
if marker not in chunk:
|
||||
continue
|
||||
json.loads(chunk.split(marker, 1)[1])
|
||||
bodies += 1
|
||||
if bodies != 2:
|
||||
raise SystemExit("expected 2 dumped bodies, got %d" % bodies)
|
||||
PY
|
||||
then ok "both upstream bodies stay valid JSON after removing stream_options"
|
||||
else bad "removing stream_options produced invalid JSON"
|
||||
fi
|
||||
|
||||
# Konfigurationsfehler müssen laut sein.
|
||||
printf 'endpoint e {\n base http://127.0.0.1:9995/v1\n}\nreq for "vm" review 5 pass e\n' \
|
||||
> "$TMP/review_bad_count.conf"
|
||||
|
||||
+1
-1
@@ -71,7 +71,7 @@ log_has() {
|
||||
# answer requests with somebody else's config), so fail loudly and early.
|
||||
# 8157 gehört auf dieser Maschine dem Hermes-Gateway — der Port wird von keinem
|
||||
# Test benutzt, also darf er die Suite nicht aufhalten.
|
||||
for port in 8361 8362 8363 8364 8357 8358 8359 8360 8355 8356 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8375 8376 8377 8378 8385 8386 8387 8388 8389 8395 8396 8397 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8160 8161 8162 8163 8164 8165 8166 8365 8366 8405 8406 8407 8408 8409 8410 8411 8412 8415 8416 8417 8418 8419 8420 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 8208 9443 9990 9991 9992 9993 9994 9995 9996; do
|
||||
for port in 8361 8362 8363 8364 8357 8358 8359 8360 8355 8356 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8375 8376 8377 8378 8385 8386 8387 8388 8389 8395 8396 8397 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8160 8161 8162 8163 8164 8165 8166 8365 8366 8405 8406 8407 8408 8409 8410 8411 8412 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 8208 9443 9990 9991 9992 9993 9994 9995 9996; do
|
||||
if ss -tln 2>/dev/null | grep -q ":$port "; then
|
||||
echo "port $port is busy — a leftover junction/stub is still running:"
|
||||
ss -tlnp 2>/dev/null | grep ":$port " | sed 's/^/ /'
|
||||
|
||||
+51
-4
@@ -84,6 +84,19 @@ static void find_json_string(const char *body, const char *key, char *out, size_
|
||||
out[i] = '\0';
|
||||
}
|
||||
|
||||
/* A JSON number field, enough for tests ("n":2). */
|
||||
static long find_json_int(const char *body, const char *key)
|
||||
{
|
||||
char pat[64];
|
||||
snprintf(pat, sizeof pat, "\"%s\"", key);
|
||||
const char *p = strstr(body, pat);
|
||||
if (!p) return -1;
|
||||
p += strlen(pat);
|
||||
while (*p == ':' || *p == ' ' || *p == '\t') p++;
|
||||
if (*p < '0' || *p > '9') return -1;
|
||||
return strtol(p, NULL, 10);
|
||||
}
|
||||
|
||||
/* Zählt Anfragen über Prozessgrenzen hinweg — der Stub forkt je Verbindung, ein
|
||||
* Zähler im Speicher des Vaters wäre für das Kind unsichtbar. Die Datei kommt aus
|
||||
* $STUB_STATE; ohne sie zählt niemand und --fail-n bleibt wirkungslos. */
|
||||
@@ -264,6 +277,13 @@ int main(int argc, char **argv)
|
||||
long body_len = (long)used - (long)head_len;
|
||||
if (body_len < 0) body_len = 0;
|
||||
|
||||
/* A strict provider rejects stream_options on a non-streaming request.
|
||||
* junction must strip it before forcing stream:false for a review. */
|
||||
const char *reject_so = getenv("STUB_REJECT_STREAM_OPTIONS");
|
||||
if (reject_so && *reject_so && counts &&
|
||||
strstr(req + head_len, "\"stream_options\""))
|
||||
forced = 400;
|
||||
|
||||
if (delay > 0) {
|
||||
struct timespec ts = { (time_t)delay, (time_t)((delay - (time_t)delay) * 1e9) };
|
||||
nanosleep(&ts, NULL);
|
||||
@@ -419,10 +439,37 @@ int main(int argc, char **argv)
|
||||
else {
|
||||
char default_answer[256];
|
||||
snprintf(default_answer, sizeof default_answer, "served by %s", name);
|
||||
snprintf(b, sizeof b,
|
||||
"{\"id\":\"stub\",\"model\":\"%s\",\"choices\":[{\"index\":0,"
|
||||
"\"message\":{\"role\":\"assistant\",\"content\":\"%s\"}}]}",
|
||||
name, selected_answer[0] ? selected_answer : default_answer);
|
||||
const char *ans = selected_answer[0] ? selected_answer : default_answer;
|
||||
const char *content_array = getenv("STUB_CONTENT_ARRAY");
|
||||
char content[1280];
|
||||
if (content_array && *content_array)
|
||||
snprintf(content, sizeof content,
|
||||
"[{\"type\":\"text\",\"text\":\"%s\"}]", ans);
|
||||
else
|
||||
snprintf(content, sizeof content, "\"%s\"", ans);
|
||||
/* Honour the client's n so a test can prove that several choices
|
||||
* pass through unreviewed. */
|
||||
long choices = find_json_int(req + head_len, "n");
|
||||
if (choices < 1) choices = 1;
|
||||
if (choices > 4) choices = 4;
|
||||
size_t bo = 0;
|
||||
int wrote = snprintf(b, sizeof b,
|
||||
"{\"id\":\"stub\",\"model\":\"%s\",\"choices\":[",
|
||||
name);
|
||||
if (wrote > 0) bo = (size_t)wrote;
|
||||
for (long c = 0; c < choices; c++) {
|
||||
if (bo >= sizeof b) break;
|
||||
wrote = snprintf(b + bo, sizeof b - bo,
|
||||
"%s{\"index\":%ld,\"message\":{\"role\":\"assistant\","
|
||||
"\"content\":%s}}",
|
||||
c ? "," : "", c, content);
|
||||
if (wrote < 0 || (size_t)wrote >= sizeof b - bo) {
|
||||
bo = sizeof b - 1;
|
||||
break;
|
||||
}
|
||||
bo += (size_t)wrote;
|
||||
}
|
||||
if (bo < sizeof b - 2) snprintf(b + bo, sizeof b - bo, "]}");
|
||||
}
|
||||
snprintf(body, sizeof body,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
|
||||
|
||||
Reference in new issue
Block a user