From 9edd881cd7d7338c8722fda95b09e656b5c96b36 Mon Sep 17 00:00:00 2001 From: Drake Stapleton <213298340+drakestapleton@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:07:11 -0600 Subject: [PATCH] ollm serve: POST /v1/completions raw-prompt route for the Omega Node coding workflow (DRA-290) --- docs/omegallm/README.md | 15 ++++-- native/omegallm/serve.om | 94 +++++++++++++++++++++++++++++++++-- tests/omegallm/serve-check.py | 83 ++++++++++++++++++++++++++++++- 3 files changed, 184 insertions(+), 8 deletions(-) diff --git a/docs/omegallm/README.md b/docs/omegallm/README.md index 4d18692..9f08acb 100644 --- a/docs/omegallm/README.md +++ b/docs/omegallm/README.md @@ -200,6 +200,15 @@ connections before each token); without it they are 400. A stream holds back a possible marker and sends the calls in one delta chunk. Role `tool` messages are rendered like any other role. Whether the model emits such blocks is a model matter; the checks script the output with `ollm_test_output`. +- `POST /v1/completions` (raw prompt, for the Omega Node edit→verify→review workflow): `prompt` + (a non-empty string) is the whole template: the context is token 0 plus the prompt's encoding, + with no role lines and no `assistant: ` prefix, so the same SEED and TEMPMILLI give + `ollm sample CK SEED N TEMPMILLI PROMPT MERGES`'s bytes. The scalar members are chat's + (`max_tokens`, `temperature`, `seed`, `top_k`, `top_p`, `stop`, `n` = 1). JSON only: + `{"id":"cmpl-ollm-N","object":"text_completion","created":..,"model":"ollm","choices":[{"index":0,"text":..,"finish_reason":"stop"|"length"}],"usage":{..}}`. + 400 `invalid_request` for `stream` true, for `messages`, `tools` or `tool_choice` present, and + for a missing, non-string or empty `prompt`; `tplmax` and `context_length_exceeded` apply as on + chat. Busy 503, client-left 499 and the log line are shared with chat. - Refusals are JSON `{"error":{...}}`: 400 (`invalid_json`, `invalid_request`, `context_length_exceeded`), 404, 405, 503 `server_busy`, and 413 `request_too_large` for a body over 983040 bytes (refused at the head) or over 65532 JSON tokens (the JSON parser's tape). @@ -207,9 +216,9 @@ connections before each token); without it they are 400. (finish 0 none, 1 stop, 2 length, 3 tool_calls, 4 client left). `tests/omegallm/serve-check.sh` starts two servers under taskset/nice/bwrap (defaults on PORT, -`tplmax=4000 test=1` on TPORT), referees them with `serve-check.py` (96 checks; the content oracle -is the same binary's `sample`, decoded with Python's `errors="replace"`; the 499 log line is read -from `server-test.log` via `SERVE_TLOG`) and stops the PIDs it started. The checks added since +`tplmax=4000 test=1` on TPORT), referees them with `serve-check.py` (the content oracle +is the same binary's `sample`, decoded with Python's `errors="replace"`; the 499 log lines are read +from `server-test.log` via `SERVE_TLOG`; `cmpl_*`/`t_cmpl_*` cover `/v1/completions`) and stops the PIDs it started. The checks added since DRA-119's first 27 come with control checks (`*_control`, `*_is_sample`, `sample_differs_from_greedy`, `t_ctl_*`, `t_forced_plain`): the same request with one input changed, expecting the other outcome. A refusal (`refuse_*`) is controlled by the accepted diff --git a/native/omegallm/serve.om b/native/omegallm/serve.om index d3fa4f0..40be7d4 100644 --- a/native/omegallm/serve.om +++ b/native/omegallm/serve.om @@ -69,6 +69,10 @@ text svtctyp "Content-Type" text svphlth "/health" text svpmods "/v1/models" text svpchat "/v1/chat/completions" +text svpcomp "/v1/completions" +text skprompt "prompt" +text svtcomp "text_completion" +text svcidp "cmpl-ollm-" text svmget "GET" text svmpost "POST" text svthlth "{\"status\":\"ok\",\"in_flight\":0,\"busy\":false}" @@ -164,7 +168,7 @@ text sem9 "seed must be an unsigned integer" text sem10 "stream must be true or false" text sem12 "only n = 1 is supported" text sem13 "the request body is over 983040 bytes" -text sem14 "no such path; routes: GET /health, GET /v1/models, POST /v1/chat/completions" +text sem14 "no such path; routes: GET /health, GET /v1/models, POST /v1/chat/completions, POST /v1/completions" text sem15 "method not allowed on this path" text sem16 "response construction failed" text sem17 "message content parts must be {\"type\":\"text\",\"text\":string}; image_url, input_audio, file and other part types are not supported" @@ -176,6 +180,9 @@ text sem22 "tool_choice must be \"none\", \"auto\", \"required\" or a {\"type\": text sem23 "tool_calls must be an array of {\"function\":{\"name\":string,\"arguments\":string or object}} objects" text sem24 "ollm_test_output (a string of at most 16384 bytes) and ollm_test_delay_ms (0 to 10000) need a server started with test=1" text sem25 "the request body has over 65532 JSON tokens (values, keys and closers), the JSON parser's limit" +text sem26 "prompt must be a non-empty string" +text sem27 "stream is not supported on /v1/completions: send stream false or leave it out" +text sem28 "messages, tools and tool_choice belong to /v1/chat/completions; /v1/completions takes prompt" text sem11a "prompt is " text sem11b " tokens including the start token; the context window is " text sem11c " tokens, so no room is left for output" @@ -540,6 +547,15 @@ end if eq e 25 ret svmcat wb p z sem25 end +if eq e 26 +ret svmcat wb p z sem26 +end +if eq e 27 +ret svmcat wb p z sem27 +end +if eq e 28 +ret svmcat wb p z sem28 +end ret p end @@ -675,8 +691,30 @@ end js_warre ob end -note svfull: the chat.completion object (stream false) +note svcfull: the text_completion object of /v1/completions +fn svcfull ob edit uchar 409600 wb edit uchar 1073664 sv edit ulong 64 to long +svhead ob wb sv svtcomp +svwt ob skchoi +js_warr ob +js_wobj ob +svwt ob skidx +js_wulong ob 0 +svwt ob sktext +js_wsbuf ob wb 8192 get sv 13 +svwt ob skfin +svfr ob sv +js_wobje ob +js_warre ob +svusage ob sv +js_wobje ob +ret js_wdone ob +end + +note svfull: the chat.completion object (stream false), or svcfull on /v1/completions fn svfull ob edit uchar 409600 wb edit uchar 1073664 sv edit ulong 64 bd edit uchar 983040 to long +if eq get sv 37 1 +ret svcfull ob wb sv +end svhead ob wb sv svcc svwt ob skchoi js_warr ob @@ -1515,6 +1553,9 @@ ret e end k = js_mnext tp k end +if eq get sv 37 1 +ret svcprm bd tp wb sv +end ma = svmem bd tp 0 skmsgs if eq ma jsnone ret 3 @@ -1529,6 +1570,37 @@ end ret svmsgs bd tp ma wb sv end +note svcprm: a /v1/completions body after its scalars: prompt (a non-empty string) is the whole +note template, with no role lines and no assistant prefix; stream true, messages, tools and +note tool_choice are refused; 0 or an error code +fn svcprm bd edit uchar 983040 tp edit uint 262144 wb edit uchar 1073664 sv edit ulong 64 to ulong +if eq get sv 0 1 +ret 27 +end +if ne svmem bd tp 0 skmsgs jsnone +ret 28 +end +if ne svmem bd tp 0 sktools jsnone +ret 28 +end +if ne svmem bd tp 0 sktchoi jsnone +ret 28 +end +pr = svmem bd tp 0 skprompt +if eq pr jsnone +ret 26 +end +if or ne js_type tp pr 6 eq js_slen tp pr 0 +ret 26 +end +put sv 30 0 +put sv 31 0 +if not svpstr bd tp pr wb sv +ret 6 +end +ret 0 +end + note ======== encoding note svemit: token t, the m-th of an encoding: md 0 onto the context, md 1 into the forced tokens @@ -2198,10 +2270,16 @@ end note ======== serving -note svid: response id chatcmpl-ollm- into wb[82176 ..), its length into sv15 +note svid: response id chatcmpl-ollm- (cmpl-ollm- on /v1/completions) into +note wb[82176 ..), its length into sv15 fn svid wb edit uchar 1073664 sv edit ulong 64 to void put sv 9 add get sv 9 1 -p = svmcat wb 82176 82240 svidp +p = ulong 82176 +if eq get sv 37 1 +p = svmcat wb p 82240 svcidp +else +p = svmcat wb p 82240 svidp +end p = svmdec wb p 82240 get sv 9 put sv 15 sub p 82176 end @@ -2323,6 +2401,14 @@ fn svroute cs edit ulong 320 io edit uchar 131072 bd edit uchar 983040 wb edit u svbeg sv if hispath cs io svpchat if hismeth cs io svmpost +put sv 37 0 +ret svchat cs io bd wb sv +end +ret svend cs io wb sv 15 405 +end +if hispath cs io svpcomp +if hismeth cs io svmpost +put sv 37 1 ret svchat cs io bd wb sv end ret svend cs io wb sv 15 405 diff --git a/tests/omegallm/serve-check.py b/tests/omegallm/serve-check.py index 00583f0..04d8d2e 100755 --- a/tests/omegallm/serve-check.py +++ b/tests/omegallm/serve-check.py @@ -348,6 +348,53 @@ check("silent_conn_closed_10s", eof == b"" and 8 < dts < 20, f"recv={eof!r} afte st, ct, out, _ = call("GET", "/health") check("alive_after_negatives", st == 200, f"{st}") + +# --- POST /v1/completions: prompt is the whole template (start token + prompt, no role lines) --- +def cpost(body, port=None, raw=None): + st, ct, out, _ = call("POST", "/v1/completions", body, raw=raw, port=port) + j = json.loads(out) if st == 200 else {} + return st, j, out + + +def crefused(body, word, port=None, raw=None, code_want="invalid_request"): + st, ct, out, _ = call("POST", "/v1/completions", body, raw=raw, port=port) + code, msg, typ = err_code(out) + return st == 400 and code == code_want and word in msg, f"{st} {code} {msg!r}" + + +cprompt = "fn main to long\n" +cwant = oracle_tpl(cprompt, 7, 8, 0) +st, j, out = cpost({"model": "ollm", "prompt": cprompt, "max_tokens": 8, "temperature": 0, "seed": 7}) +ch = (j.get("choices") or [{}])[0] +u = j.get("usage", {}) +check("cmpl_greedy_parity", st == 200 and j.get("object") == "text_completion" and j.get("id", "").startswith("cmpl-ollm-") + and ch.get("index") == 0 and ch.get("finish_reason") in ("stop", "length") and ch.get("text") == cwant + and "message" not in ch and u.get("total_tokens") == u.get("prompt_tokens", 0) + u.get("completion_tokens", 0) + and 1 <= u.get("completion_tokens", 0) <= 8, f"{st} {out[:240]!r} oracle={cwant!r}") +chatw = oracle_tpl("user: " + cprompt + "\nassistant: ", 7, 8, 0) +check("cmpl_no_chat_template_control", chatw != cwant, f"chat_template={chatw!r} raw={cwant!r}") +st, j, out = cpost({"prompt": cprompt, "max_tokens": 8, "top_p": 0, "temperature": 0.8, "seed": 7}) +check("cmpl_top_p0_is_greedy", st == 200 and (j.get("choices") or [{}])[0].get("text") == cwant, f"{out[:200]!r}") +cb = {"prompt": cprompt, "max_tokens": 4} +for name, body, word in (("cmpl_refuse_stream", dict(cb, stream=True), "stream is not supported on /v1/completions"), + ("cmpl_refuse_messages", dict(cb, messages=msgs), "takes prompt"), + ("cmpl_refuse_tools", dict(cb, tools=tools), "takes prompt"), + ("cmpl_refuse_tool_choice", dict(cb, tool_choice="none"), "takes prompt"), + ("cmpl_refuse_no_prompt", {"max_tokens": 4}, "prompt must be a non-empty string"), + ("cmpl_refuse_prompt_number", dict(cb, prompt=5), "prompt must be a non-empty string"), + ("cmpl_refuse_prompt_array", dict(cb, prompt=["a"]), "prompt must be a non-empty string"), + ("cmpl_refuse_prompt_empty", dict(cb, prompt=""), "prompt must be a non-empty string"), + ("cmpl_refuse_n2", dict(cb, n=2), "n = 1"), + ("cmpl_refuse_prompt_bytes", dict(cb, prompt="x" * 66000), "over 65536 bytes")): + check(name, *crefused(body, word)) +check("cmpl_refuse_context", *crefused({"prompt": long_text, "max_tokens": 4}, "context window is 256", code_want="context_length_exceeded")) +st, ct, out, _ = call("GET", "/v1/completions") +check("cmpl_refuse_405", st == 405 and err_code(out)[0] == "method_not_allowed", f"{st} {out!r}") +st, ct, out, _ = call("GET", "/v1/nope") +check("refuse_404_lists_completions", st == 404 and "POST /v1/completions" in err_code(out)[1], f"{st} {out!r}") +st, ct, out, _ = call("GET", "/health") +check("alive_after_cmpl", st == 200, f"{st}") + # --- second server: tplmax=4000 test=1 (scripted output: protocol checks, not model checks) ---- if TPORT: st, ct, out, _ = call("GET", "/health", port=TPORT) @@ -467,7 +514,7 @@ if TPORT: if TLOG: with open(TLOG) as f: lines = [l.split() for l in f if l.startswith("C 499 ")] - check("t_disconnect_logged", len(lines) == 1 and lines[0][4] == "4", f"{lines}") + check("t_disconnect_logged", len(lines) >= 1 and lines[0][4] == "4", f"{lines}") else: print("NOT_RUN t_disconnect_logged SERVE_TLOG unset", flush=True) box = {} @@ -483,6 +530,40 @@ if TPORT: st, ch, out = post(base, port=TPORT) check("t_idle_not_busy_control", st == 200 and ch["message"]["content"] == greedy, f"{out[:200]!r}") + # /v1/completions on the test server: scripted output, busy 503, and a client that leaves + cslow = {"prompt": "x", "max_tokens": 200, "temperature": 0, "ollm_test_output": slow, "ollm_test_delay_ms": 300} + st, j, out = cpost(dict(cslow, ollm_test_delay_ms=0), port=TPORT) + check("t_cmpl_forced", st == 200 and j["choices"][0]["text"] == slow and j["choices"][0]["finish_reason"] == "stop", f"{out[:200]!r}") + box = {} + th = threading.Thread(target=lambda: box.update(r=cpost(cslow, port=TPORT))) + th.start() + time.sleep(1.5) + st, ct, out, _ = call("GET", "/health", port=TPORT) + hb = json.loads(out) if st == 200 else {} + st5, ct, out5, _ = call("POST", "/v1/completions", cb, port=TPORT) + ra = LAST[threading.get_ident()].get("retry-after") + code, msg, typ = err_code(out5) + th.join() + stb, jb, outb = box.get("r", (0, {}, b"")) + check("t_cmpl_busy_503", hb.get("busy") is True and st5 == 503 and code == "server_busy" and ra == "1", f"health={hb} {st5} {code} {msg!r}") + check("t_cmpl_busy_whole", stb == 200 and (jb.get("choices") or [{}])[0].get("text") == slow, f"{outb[:200]!r}") + body = json.dumps(cslow).encode() + gone = socket.create_connection((HOST, TPORT), timeout=30) + gone.sendall(b"POST /v1/completions HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\nContent-Length: " + + str(len(body)).encode() + b"\r\n\r\n" + body) + time.sleep(1.0) + gone.close() + time.sleep(1.0) + st, ct, out, _ = call("GET", "/health", port=TPORT) + hg = json.loads(out) if st == 200 else {} + check("t_cmpl_disconnect_frees_slot", hg == {"status": "ok", "in_flight": 0, "busy": False}, f"{st} {out!r}") + if TLOG: + with open(TLOG) as f: + lines = [l.split() for l in f if l.startswith("C 499 ")] + check("t_cmpl_disconnect_logged", len(lines) == 2 and lines[1][4] == "4" and int(lines[1][3]) < 200, f"{lines}") + else: + print("NOT_RUN t_cmpl_disconnect_logged SERVE_TLOG unset", flush=True) + fails = results.count(False) print(f"SUMMARY pass={results.count(True)} fail={fails}", flush=True) sys.exit(1 if fails else 0)