Files
Robert KarlandClaude Fable 5.1 4c25aa6f6a Front end: llama.cpp's server files with the Strata engine behind them (design C), with fake-engine tests
frontend/strata_context.cpp replaces tools/server/server-context.cpp. The other seven server files
(server-http, -queue, -task, -common, -chat, -schema, -stream) compile unmodified from the submodule into
the static library strata_llama_server; frontend/ui_stub/ supplies the empty web-UI asset table that
server-http.cpp references. The context runs one slot: the engine is a child process spoken to over its
line protocol (docs/BACKENDS.md), a vocab-only llama_model provides tokenizing, the chat template and the
reasoning and tool-call parsers, and the server's own task and result types carry the OpenAI JSON, SSE
framing and client-disconnect cancel. Routes kept: /health, /v1/models, /v1/chat/completions, /tokenize,
/detokenize. The strata engine's 8-token reserve is kept out of max_new. A supervisor thread ends the
process with exit code 1 when the engine dies, for a unit with Restart=on-failure.

frontend/engine.hpp is the child adapter: fork+exec, READY/INFO, GEN/GENI, T/PP/RESUME/DONE/ERR/FATAL,
STOP with drain to DONE, non-blocking reap. quit() and the supervisor's reap_exit() share the pid through
an atomic that quit() takes over by exchange, and only a positive local copy ever reaches kill(). The
first draft kept a plain pid_t and the supervisor set it to -1 on reaping, so a SIGTERM during shutdown
raced into kill(-1, SIGKILL), which killed every process of the user; it did so twice on 2026-10-17.

Build: STRATA_FRONTEND (default on) gates the llama.cpp libraries and the front end, not CUDA, so they
build on a machine without a GPU. Makefile: unbiased-strata joins `make build`; `make frontend` builds it
alone without CUDA into build-frontend/; `make test` builds engine_test there and runs it and
frontend/tests/e2e.sh against a fake engine (frontend/tests/fake-engine.py). The nvcc probes run only
when there is an nvcc and only the build and configure goals demand one. UPSTREAM.md records all of it.

Verified on macOS arm64 without CUDA: `make frontend` and `make test` pass. engine_test, 7 cases: READY,
a request, STOP mid-request with drain and a clean next request, a 100k-token burst the caller abandons,
ERR leaves the adapter usable, the engine dying mid-request is diagnosed, a DONE with the wrong prompt
size is visible. e2e.sh, 12 checks: /health, /v1/models, a non-streaming chat with content, finish and
usage, a stream whose deltas join to the content and end with finish_reason and [DONE], a client that
disconnects mid-stream (the server logs the cancel and the next request completes), an oversize prompt
refused with 400, a tokenize/detokenize round trip. Zero new patch lines to llama.cpp, so the step-4
gate of PLAN.md section 5 passes and design C stands. Not yet run against a real engine.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-17 21:31:05 -07:00

110 lines
3.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""A stand-in for strata / strata-poly that speaks the line protocol (docs/BACKENDS.md), for tests without a GPU.
GEN <max_new> [key=value ...] <id,id,...> -> T <id> per token, then DONE
STOP -> the current request ends with finish=cancel
QUIT -> exit 0
Environment variables choose the answers, so one script covers the cases:
FAKE_TOKENS comma-separated ids to emit (default: the prompt ids echoed back, capped by max_new)
FAKE_N_TOKENS emit this many tokens of id FAKE_TOKEN_ID instead (burst tests)
FAKE_TOKEN_ID the id used by FAKE_N_TOKENS (default 100)
FAKE_DELAY_MS sleep this long between tokens (default 0)
FAKE_CONTEXT the context in the READY line (default 4096)
FAKE_ERR if set, answer every GEN with "ERR <text>" instead
FAKE_DIE_AFTER if set, exit 3 after emitting this many tokens of the first request
FAKE_NO_STOP if set, READY without "stop": STOP is ignored and the request runs to its end
FAKE_PROMPT_N if set, lie about the prompt size in DONE (mismatch test)
"""
import os
import select
import sys
import time
out = sys.stdout
def say(s):
out.write(s + "\n")
out.flush()
env = os.environ.get
say("INFO engine=fake batch_slots=0")
say("READY %s%s" % (env("FAKE_CONTEXT", "4096"), "" if env("FAKE_NO_STOP") else " stop"))
first = True
pending = [] # lines read while checking for STOP that are not STOP/QUIT
def readline():
if pending:
return pending.pop(0)
line = sys.stdin.readline()
if line == "":
sys.exit(0)
return line.rstrip("\n")
def stop_requested():
"""Consume what is readable now; True if a STOP was among it."""
stop = False
while select.select([sys.stdin], [], [], 0)[0]:
line = sys.stdin.readline()
if line == "":
sys.exit(0)
line = line.rstrip("\n")
if line == "STOP":
stop = True
elif line == "QUIT":
sys.exit(0)
else:
pending.append(line)
return stop
while True:
line = readline()
if line == "QUIT":
sys.exit(0)
if line == "STOP" or line == "":
continue
parts = line.split()
if parts[0] not in ("GEN", "GENI") or len(parts) < 3:
say("ERR expected: GEN <max_new> <id,id,...>")
continue
max_new = int(parts[1])
prompt = [int(x) for x in parts[-1].split(",") if x]
prompt_n = len(prompt)
if env("FAKE_ERR"):
say("ERR " + env("FAKE_ERR"))
continue
say("RESUME 0")
say("PP %d %d 1 1000" % (prompt_n, prompt_n))
if env("FAKE_N_TOKENS"):
tokens = [int(env("FAKE_TOKEN_ID", "100"))] * int(env("FAKE_N_TOKENS"))
elif env("FAKE_TOKENS"):
tokens = [int(x) for x in env("FAKE_TOKENS").split(",") if x]
else:
tokens = prompt
delay = float(env("FAKE_DELAY_MS", "0")) / 1000.0
die_after = int(env("FAKE_DIE_AFTER", "0") or 0)
produced = 0
finish = "stop"
for t in tokens:
if produced >= max_new:
finish = "length"
break
if stop_requested() and not env("FAKE_NO_STOP"):
finish = "cancel"
break
say("T %d" % t)
produced += 1
if die_after and first and produced >= die_after:
sys.exit(3)
if delay:
time.sleep(delay)
first = False
# DONE <generated> <prompt> <prompt_ms> <decode_ms> <finish> <drafts_accepted> <drafts_offered> <reused>
say("DONE %d %s 12.5 %.1f %s 0 0 0" % (produced, env("FAKE_PROMPT_N", str(prompt_n)), produced * 10.0, finish))