feat: harden and extend the CLI (security, UX, robustness, tests)

Sieben Verbesserungen; die Dateien überschneiden sich thematisch, daher ein
Commit (jeder Commit bleibt grün: 115 Tests).

- #1 --check ist scriptbar: Exit 0 wenn Container läuft und erreichbar,
  sonst 5 (check_exit_code / CheckResult).
- #2 --force implementiert: Bypass eines belegten Locks mit Warnung
  (_container_lock) und stop_container(force=…) schluckt Inkonsistenzen.
- #3 stille Trunkierung behoben: chat_completion_text liefert ChatReply
  (content + finish_reason); bei finish_reason=length Hinweis auf stderr,
  --chat gibt Exit 1 bei leerem Content zurück.
- #4 keine vermeidbare Downtime: --change validiert den Modellpfad VOR dem
  Entfernen des laufenden Containers.
- #5 Netzwerk dicht: Port-Publish standardmäßig nur auf 127.0.0.1
  (--expose/expose für alle Interfaces), optionaler --api-key/api_key
  (Server --api-key + Bearer-Token auf allen Requests).
- #6 --stream: Chat-Reply token-weise via SSE auf stdout (stream_chat).
- #7 tests/test_actions.py: Orchestrierungs-Ebene (dry-run-Nebenwirkungen,
  Lock, validate-before-remove, chat/stream/exit-codes).

Doku aktualisiert (Manpage, README, llama.cpp.config.example).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Dieter Schlüter 2026-07-06 16:57:47 +02:00
commit 24202feee6
14 changed files with 485 additions and 32 deletions

View file

@ -64,7 +64,9 @@ def test_chat_completion_text_success():
mock_resp = MagicMock(status_code=200)
mock_resp.json.return_value = {"choices": [{"message": {"content": "ok"}}]}
mock_post.return_value = mock_resp
assert chat_completion_text(cfg, prompt_cfg) == "ok"
reply = chat_completion_text(cfg, prompt_cfg)
assert reply.content == "ok"
assert reply.finish_reason == ""
def test_chat_completion_text_honors_chat_endpoint():
@ -116,6 +118,53 @@ def test_chat_completion_text_malformed_response():
pass
def test_chat_completion_text_reports_finish_reason():
cfg = make_cfg()
prompt_cfg = PromptConfig(user_prompt="hi")
with patch("requests.post") as mock_post:
mock_resp = MagicMock(status_code=200)
mock_resp.json.return_value = {
"choices": [{"finish_reason": "length", "message": {"content": ""}}]
}
mock_post.return_value = mock_resp
reply = chat_completion_text(cfg, prompt_cfg)
assert reply.content == ""
assert reply.finish_reason == "length"
def test_api_key_sent_as_bearer():
cfg = make_cfg(api_key="secret123")
prompt_cfg = PromptConfig(user_prompt="hi")
with patch("requests.post") as mock_post:
mock_resp = MagicMock(status_code=200)
mock_resp.json.return_value = {"choices": [{"message": {"content": "x"}}]}
mock_post.return_value = mock_resp
chat_completion_text(cfg, prompt_cfg)
_, kwargs = mock_post.call_args
assert kwargs["headers"]["Authorization"] == "Bearer secret123"
def test_stream_chat_yields_content_and_finish():
cfg = make_cfg()
prompt_cfg = PromptConfig(user_prompt="hi", stream=True)
lines = [
'data: {"choices":[{"delta":{"content":"Hal"}}]}',
'data: {"choices":[{"delta":{"content":"lo"}}]}',
'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}',
"data: [DONE]",
]
mock_resp = MagicMock(status_code=200)
mock_resp.iter_lines.return_value = iter(lines)
with patch("requests.post", return_value=mock_resp):
from llamacppctl.http_ops import stream_chat
events = list(stream_chat(cfg, prompt_cfg))
contents = [v for k, v in events if k == "content"]
finishes = [v for k, v in events if k == "finish"]
assert "".join(contents) == "Hallo"
assert finishes == ["stop"]
def test_wait_until_ready_succeeds_immediately():
cfg = make_cfg(timeout=5, poll_interval=0.01)
probe = PromptConfig(user_prompt="ping", max_tokens=1)