phase: 172_thinking_level
All verification complete. Final report: **Phase 172 final verification pass — all tasks (01–05) were already complete; verified the whole phase, no code fixes needed.** - **Verified implementation:** `Settings.llm_thinking_level: str = "low"` + `CATALOG` row (`LLM (aipi)` / "Thinking level", plain: not secret/restart/overridable-exempt) + `.env.example` line under `BOR_LLM_CHAT_MODEL` + README table row; `_thinking_extra_body()` → `reasoning_effort` via `extra_body` on `chat()` + `chat_stream()` when trimmed-non-empty, key absent (byte-identical) when empty, embeddings untouched; per-turn line gains `thinking=<value>` (`-` when empty) before `thinking_chars`. - **Tests/lint/coverage (all green):** `uv run pytest` → 4084 passed · `uv run pytest --cov=app --cov-report=term-missing` → 4084 passed, `app/` **99%** (>90%) · `uv run ruff check .` → All checks passed · `uv run pyright` → 0 errors, 0 warnings. - **Completion criteria:** (1) `pytest -k "config and thinking"` 4 passed + `.env.example` grep ✓ (2) live `GET /api/admin/settings` curl shows the row (str, `low`, `default`) + integration PUT/DELETE round-trip ✓ (3) 9 mocked-client unit tests assert `extra_body` present/absent ✓ (4) log-line tests assert `low`/`high`/`-`/whitespace ✓ (5) suite + coverage + lint ✓ (6) `test_thinking_level.py` 3/3 in isolation; `test_admin_settings_panel.py` 12/12; `test_admin_settings_layout.py` 5/5 ✓ (7) atomic commit + phase move = the harness's job (task files already in `complete/`; I ran no git index/history commands). - **Documentation:** README.md reviewed in full — verified consistent (env table complete vs `.env.example`, catalog restart/non-overridable claims re-verified at runtime); AGENTS.md reviewed in full — verified consistent (no rule affected, A5 = no UI change). No changes needed. **Flag (not fixable here):** PLAN.md §9's per-turn-line spec predates the phase-172 `thinking=` field; PLAN.md is off-limits per protocol. - **Next pending phase:** none — `todo/` contains only phase 172 (the harness moves it on this pass's success).
This commit is contained in:
@@ -0,0 +1,405 @@
|
||||
"""Phase 172 E2E (Playwright): the Thinking level row in the admin Settings
|
||||
panel — the owner's acceptance for a pi-style, operator-tunable LLM
|
||||
reasoning knob, proven in a real browser.
|
||||
|
||||
Source: ``TODO.md`` L3 — "Brain of Reese needs to support thinking levels
|
||||
like pi does. should be a free-form text entry like chat model. Default
|
||||
should be 'low'. Update .env and admin settings."
|
||||
|
||||
Run in isolation (DB must be up: ``podman compose up -d db``):
|
||||
|
||||
uv run pytest tests/e2e/test_thinking_level.py -v --no-cov
|
||||
|
||||
Test → contract mapping (one story, one phase, one isolated file):
|
||||
|
||||
1. ``test_row_renders_free_form_with_default_low`` — the row: the
|
||||
``LLM (aipi)`` group band is visible (the phase-171 band
|
||||
contract), a row labelled **Thinking level** with the
|
||||
``BOR_LLM_THINKING_LEVEL`` env-var display (the catalogue
|
||||
pattern) is present, its input is a **free-form text** input
|
||||
(``type="text"`` — NOT ``number``/``select``, the A4
|
||||
contract), it shows ``low`` with the ``env`` badge, and the
|
||||
``llm_chat_model`` row's input is the same ``type="text"``
|
||||
control (the "like chat model" reference, in one assertion
|
||||
pair).
|
||||
2. ``test_saved_level_persists_without_restart`` — the save
|
||||
(the phase-168 §7.4 Save lifecycle: dirty → save → ``DB``
|
||||
badge): ``high`` is typed (the dirty hint appears), then the
|
||||
free-form custom value ``think-soft`` is typed once (no
|
||||
allowlist — A4) and saved; the effective value reads
|
||||
``think-soft`` (the API catalogue AND the raw
|
||||
``app_settings`` row), and a reload of ``/settings.html``
|
||||
still shows ``think-soft`` with the ``DB`` badge (no
|
||||
restart, the phase-168 contract).
|
||||
3. ``test_reset_returns_to_env_default`` — the per-row Reset
|
||||
(the per-row ``DELETE``): after a save, the Reset is live;
|
||||
clicking it lands the status line and the value reads
|
||||
``low`` again with the ``env`` badge (the overlay reset,
|
||||
task 02's contract, in the browser).
|
||||
|
||||
DB isolation: the shared e2e Postgres keeps ``app_settings``
|
||||
(the overlay rows — a leftover row would retune every later
|
||||
suite's app through the conftest pin's failure mode),
|
||||
``ui_settings`` (the single row the caching middleware reads
|
||||
for EVERY served page) and ``api_tokens`` rows across suites.
|
||||
An autouse fixture truncates ``app_settings`` and
|
||||
``ui_settings`` and deletes the ``e2e-``-labeled tokens before
|
||||
AND after every test (never a TRUNCATE on ``api_tokens`` — the
|
||||
shared DB may hold the owner's real tokens); the module teardown
|
||||
pins ``app_settings`` empty. The module app opts BACK IN over
|
||||
the conftest's global ``BOR_SETTINGS_DB=0`` pin (``env["BOR_
|
||||
SETTINGS_DB"] = "1"`` — the ``test_pictorial_pdf.py`` opt-in
|
||||
pattern) and pins ``BOR_LLM_THINKING_LEVEL`` (and the
|
||||
"like chat model" reference, ``BOR_LLM_CHAT_MODEL``) to their
|
||||
CODE DEFAULTS — the env var is set (so the badge reads ``env``,
|
||||
not ``default``) and an operator's local ``.env`` cannot leak a
|
||||
different level into the suite's app (the house leak-guard
|
||||
pattern). A module-scope ``.env`` mtime guard pins LOCKED A1:
|
||||
no code path the suite drives opens the env file for writing.
|
||||
|
||||
ASSUMPTION A6 (phase-locked): the per-turn-line assertion
|
||||
(``thinking=<value>``) lives in the unit/integration layer
|
||||
(task 04) — the mock LLM turn does not exercise the wire, and
|
||||
this suite proves the settings surface only.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from collections.abc import Iterator
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from playwright.sync_api import Page, expect
|
||||
from sqlalchemy import text
|
||||
|
||||
from app.config import Settings
|
||||
from app.db import SessionLocal
|
||||
from e2e.auth_helpers import login
|
||||
from e2e.conftest import (
|
||||
ADMIN_PASSWORD,
|
||||
APP_PORT,
|
||||
REPO,
|
||||
SESSION_SECRET,
|
||||
USE_REAL_LLM,
|
||||
_wait_http,
|
||||
)
|
||||
|
||||
APP_URL = f"http://127.0.0.1:{APP_PORT}"
|
||||
|
||||
#: The save's custom free-form value (test 2) — NOT a known pi
|
||||
#: level (proves the A4 contract: no allowlist, the endpoint is
|
||||
#: the source of truth).
|
||||
SAVED_LEVEL = "think-soft"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Per-module app env (the phase-168 tuning pattern)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _boot_app() -> subprocess.Popen[bytes]:
|
||||
"""The one module app: the shared env block + the phase-172 pins,
|
||||
with the overlay opted BACK IN (``BOR_SETTINGS_DB=1``)."""
|
||||
global _MOCK_PORT
|
||||
env = dict(os.environ)
|
||||
env.pop("DEBUGPY", None)
|
||||
env["BOR_ENVIRONMENT"] = "e2e"
|
||||
env["BOR_STATIC_DIR"] = str(REPO / "frontend")
|
||||
env["BOR_LLM_BASE_URL"] = (
|
||||
"https://aipi.reeseapps.com/v1"
|
||||
if USE_REAL_LLM
|
||||
else f"http://127.0.0.1:{_MOCK_PORT}/v1"
|
||||
)
|
||||
# The E2E-calibrated gate pins (the conftest pattern — the mock's
|
||||
# token-overlap distribution, explicit so an operator's local
|
||||
# .env cannot leak a different gate into the suite's app).
|
||||
env["BOR_RELEVANCE_THRESHOLD"] = "0.30"
|
||||
env["BOR_LEXICAL_SUPPORT_FLOOR"] = "0.15"
|
||||
env["BOR_SOURCE_USEFULNESS_FLOOR"] = "0.15"
|
||||
env["BOR_LLM_RETRY_DELAY"] = "0"
|
||||
env["BOR_LLM_RETRIES"] = str(Settings.model_fields["llm_retries"].default)
|
||||
env.setdefault(
|
||||
"BOR_DATABASE_URL",
|
||||
"postgresql+psycopg://reese:reese@localhost:5432/brain_of_reese",
|
||||
)
|
||||
# Phase 16: admin auth must be set or create_app() refuses to boot.
|
||||
env["BOR_ADMIN_PASSWORD"] = ADMIN_PASSWORD
|
||||
env["BOR_SESSION_SECRET"] = SESSION_SECRET
|
||||
env["BOR_DOCS_REPO"] = ""
|
||||
env["BOR_SUGGESTIONS"] = json.dumps(
|
||||
Settings.model_fields["suggestions"].default
|
||||
)
|
||||
# Leak guards: the branding base and the "like chat model"
|
||||
# reference row stay the CODE defaults (derived from the class
|
||||
# fields, never drift from app/config.py).
|
||||
env["BOR_APP_NAME"] = Settings.model_fields["app_name"].default
|
||||
env["BOR_INPUT_PLACEHOLDER"] = Settings.model_fields["input_placeholder"].default
|
||||
env["BOR_FOOTER_TEXT"] = Settings.model_fields["footer_text"].default
|
||||
env["BOR_GIT_SOURCES"] = ""
|
||||
env["BOR_LLM_CHAT_MODEL"] = Settings.model_fields["llm_chat_model"].default
|
||||
# Phase 168 (the panel suite's pattern): the module opts BACK IN
|
||||
# over the conftest's global BOR_SETTINGS_DB=0 pin (the
|
||||
# test_pictorial_pdf.py pattern) — the row's save/reset
|
||||
# contract needs the overlay on.
|
||||
env["BOR_SETTINGS_DB"] = "1"
|
||||
# Phase 172 (task 05): pin the thinking level to the CODE
|
||||
# DEFAULT — the env var is SET (so the badge reads ``env``, not
|
||||
# ``default``) and an operator's local .env cannot leak a
|
||||
# different level into the suite's app.
|
||||
env["BOR_LLM_THINKING_LEVEL"] = Settings.model_fields[
|
||||
"llm_thinking_level"
|
||||
].default
|
||||
return subprocess.Popen(
|
||||
[sys.executable, "-m", "uvicorn", "app.main:app",
|
||||
"--host", "127.0.0.1", "--port", str(APP_PORT), "--log-level", "warning"],
|
||||
cwd=REPO,
|
||||
env=env,
|
||||
)
|
||||
|
||||
|
||||
def _stop_app(proc: subprocess.Popen[bytes]) -> None:
|
||||
proc.terminate()
|
||||
try:
|
||||
proc.wait(timeout=10)
|
||||
except subprocess.TimeoutExpired:
|
||||
proc.kill()
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def app_server(mock_llm: int) -> Iterator[str]:
|
||||
"""The real app under test — per-module env: the thinking level
|
||||
pinned to the code default ``low`` and ``BOR_SETTINGS_DB=1``
|
||||
(the module opts back in over the conftest pin)."""
|
||||
global _MOCK_PORT
|
||||
_MOCK_PORT = mock_llm
|
||||
proc = _boot_app()
|
||||
try:
|
||||
_wait_http(f"{APP_URL}/api/health")
|
||||
yield APP_URL
|
||||
finally:
|
||||
_stop_app(proc)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def app_url(app_server: str) -> str:
|
||||
return app_server
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# DB isolation + helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _clean_settings_state() -> None:
|
||||
"""Fresh settings + theme + token state per test: truncate
|
||||
``app_settings`` (a leftover row is the cross-suite poison this
|
||||
phase's overlay contract exists to prevent — the new key's rows
|
||||
are wiped between tests) and the single-row ``ui_settings``
|
||||
(the middleware reads it for EVERY page), and delete this
|
||||
suite's issued tokens (label-scoped on ``e2e-`` — never a
|
||||
TRUNCATE: the shared DB may hold the owner's real tokens)."""
|
||||
with SessionLocal() as db:
|
||||
db.execute(text("TRUNCATE app_settings"))
|
||||
db.execute(text("TRUNCATE ui_settings"))
|
||||
db.execute(text("DELETE FROM api_tokens WHERE label LIKE 'e2e-%'"))
|
||||
db.commit()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _clean(db_ready: None) -> Iterator[None]:
|
||||
_clean_settings_state()
|
||||
yield
|
||||
_clean_settings_state()
|
||||
# The module's final state: app_settings EMPTY (the cross-suite
|
||||
# poison guard — the next suite's app must boot with no
|
||||
# overrides).
|
||||
with SessionLocal() as db:
|
||||
assert db.execute(text("SELECT count(*) FROM app_settings")).scalar() == 0
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def _env_never_written() -> Iterator[None]:
|
||||
"""LOCKED A1, mtime-pinned: no code path the suite drives opens
|
||||
``.env`` for writing (the file is gitignored — a ``git status``
|
||||
check cannot see it, so ``stat``'s mtime is the assertion)."""
|
||||
env_file = REPO / ".env"
|
||||
before = env_file.stat().st_mtime_ns if env_file.exists() else None
|
||||
yield
|
||||
after = env_file.stat().st_mtime_ns if env_file.exists() else None
|
||||
assert before == after, "the app under test must never write .env"
|
||||
|
||||
|
||||
def _cookies(page: Page) -> dict[str, str]:
|
||||
"""The session cookies the browser context holds (the test's API
|
||||
side sees exactly what that browser sees)."""
|
||||
return {
|
||||
c["name"]: c["value"]
|
||||
for c in page.context.cookies()
|
||||
if "name" in c and "value" in c
|
||||
}
|
||||
|
||||
|
||||
def _admin_client(app_url: str) -> httpx.Client:
|
||||
"""The API side of the form login (the 204 + the signed session
|
||||
cookie in the jar)."""
|
||||
client = httpx.Client(timeout=60.0)
|
||||
r = client.post(f"{app_url}/api/login", json={"password": ADMIN_PASSWORD})
|
||||
assert r.status_code == 204, f"admin login → {r.status_code}: {r.text[:120]}"
|
||||
return client
|
||||
|
||||
|
||||
def _catalog_item(client: httpx.Client, app_url: str, key: str) -> dict[str, Any]:
|
||||
r = client.get(f"{app_url}/api/admin/settings", timeout=30.0)
|
||||
assert r.status_code == 200, r.text[:200]
|
||||
for item in r.json()["items"]:
|
||||
if item["key"] == key:
|
||||
return item
|
||||
raise AssertionError(f"{key} missing from the catalogue")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 1. The row renders free-form with default low (the "like chat
|
||||
# model" reference, in one assertion pair)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_row_renders_free_form_with_default_low(page: Page, app_url: str, db_ready: None) -> None:
|
||||
page.set_default_timeout(30_000)
|
||||
login(page, app_url, next="/settings.html")
|
||||
expect(page.locator("#settings-content")).to_be_visible(timeout=15_000)
|
||||
expect(page.locator("#settings-disabled")).to_be_hidden() # the overlay is on
|
||||
|
||||
# The LLM (aipi) group band is visible (the phase-171 band
|
||||
# contract — the row sits in it)…
|
||||
expect(page.locator("tr.settings-group-row", has_text="LLM (aipi)")).to_be_visible()
|
||||
# …and the row is present with its label…
|
||||
row = page.locator('tr[data-key="llm_thinking_level"]')
|
||||
expect(row).to_be_visible()
|
||||
expect(row.locator(".cell-clip")).to_have_text("Thinking level")
|
||||
# …and the env-var display (the catalogue pattern).
|
||||
expect(row.locator(".settings-env")).to_have_text("BOR_LLM_THINKING_LEVEL")
|
||||
|
||||
# The input is a FREE-FORM TEXT control (A4: type="text" — NOT
|
||||
# number/select; no allowlist, the endpoint is the source of
|
||||
# truth).
|
||||
assert row.locator(".settings-control").get_attribute("type") == "text"
|
||||
# The "like chat model" reference: the llm_chat_model row's
|
||||
# input is the SAME type="text" control (one assertion pair).
|
||||
thinking_type = row.locator(".settings-control").get_attribute("type")
|
||||
chat_type = page.locator(
|
||||
'tr[data-key="llm_chat_model"] .settings-control'
|
||||
).get_attribute("type")
|
||||
assert thinking_type == chat_type == "text"
|
||||
|
||||
# The default: the env-pinned ``low`` with the env badge (the
|
||||
# env var is set — no app_settings row, no code-default badge).
|
||||
expect(row.locator(".settings-control")).to_have_value("low")
|
||||
expect(row.locator(".settings-src")).to_have_text("env")
|
||||
# Nothing is saved yet: the row's Reset is disabled (no saved
|
||||
# override — the phase-168 render contract).
|
||||
expect(row.locator(".settings-reset")).to_be_disabled()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 2. The save: dirty → save → DB badge, the effective value is the
|
||||
# free-form custom, and it survives a reload WITHOUT a restart
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_saved_level_persists_without_restart(page: Page, app_url: str, db_ready: None) -> None:
|
||||
page.set_default_timeout(30_000)
|
||||
login(page, app_url, next="/settings.html")
|
||||
expect(page.locator("#settings-content")).to_be_visible(timeout=15_000)
|
||||
client = _admin_client(app_url)
|
||||
|
||||
# The §7.4 Save lifecycle: type ``high`` — the dirty hint
|
||||
# appears (one unsaved change on the row)…
|
||||
row = page.locator('tr[data-key="llm_thinking_level"]')
|
||||
row.locator(".settings-control").fill("high")
|
||||
expect(page.locator("#settings-dirty")).to_have_text("1 unsaved changes")
|
||||
# …then type the free-form custom value once (A4 — NOT a known
|
||||
# pi level: no allowlist, the endpoint is the source of truth).
|
||||
row.locator(".settings-control").fill(SAVED_LEVEL)
|
||||
expect(page.locator("#settings-save")).to_be_enabled()
|
||||
|
||||
# Save: the status line, then the badge flips to DB (the
|
||||
# re-GET settled the row).
|
||||
page.click("#settings-save")
|
||||
expect(page.locator("#settings-status")).to_contain_text(
|
||||
"Saved 1 settings.", timeout=30_000
|
||||
)
|
||||
expect(row.locator(".settings-src")).to_have_text("DB")
|
||||
|
||||
# The effective value reads ``think-soft`` — the API catalogue
|
||||
# (what the LLM layer reads per request via get_settings)…
|
||||
item = _catalog_item(client, app_url, "llm_thinking_level")
|
||||
assert item["source"] == "db" and item["value"] == SAVED_LEVEL
|
||||
# …and the raw app_settings row is stored RAW (the overlay's
|
||||
# D1 env-native string form).
|
||||
with SessionLocal() as db:
|
||||
raw = db.execute(
|
||||
text("SELECT value FROM app_settings WHERE key = 'llm_thinking_level'")
|
||||
).scalar_one()
|
||||
assert raw == SAVED_LEVEL, f"the row must be stored raw (got {raw!r})"
|
||||
|
||||
# No restart: the DB value survives a reload of /settings.html
|
||||
# (the panel re-reads the settled server state) with the DB
|
||||
# badge still on.
|
||||
page.goto(app_url + "/settings.html")
|
||||
expect(page.locator("#settings-content")).to_be_visible(timeout=15_000)
|
||||
expect(
|
||||
page.locator('tr[data-key="llm_thinking_level"] .settings-control')
|
||||
).to_have_value(SAVED_LEVEL, timeout=15_000)
|
||||
expect(
|
||||
page.locator('tr[data-key="llm_thinking_level"] .settings-src')
|
||||
).to_have_text("DB")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 3. The Reset: control goes back to the environment (the per-row
|
||||
# DELETE — task 02's contract, in the browser)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_reset_returns_to_env_default(page: Page, app_url: str, db_ready: None) -> None:
|
||||
page.set_default_timeout(30_000)
|
||||
login(page, app_url, next="/settings.html")
|
||||
expect(page.locator("#settings-content")).to_be_visible(timeout=15_000)
|
||||
client = _admin_client(app_url)
|
||||
|
||||
# Establish a saved override first (the Reset is disabled with
|
||||
# nothing to reset — the phase-168 render contract): save
|
||||
# ``high`` so the row's Reset goes live…
|
||||
row = page.locator('tr[data-key="llm_thinking_level"]')
|
||||
row.locator(".settings-control").fill("high")
|
||||
page.click("#settings-save")
|
||||
expect(page.locator("#settings-status")).to_contain_text(
|
||||
"Saved 1 settings.", timeout=30_000
|
||||
)
|
||||
expect(row.locator(".settings-src")).to_have_text("DB")
|
||||
expect(row.locator(".settings-reset")).to_be_enabled()
|
||||
|
||||
# The per-row Reset (the per-row DELETE): the status line…
|
||||
row.locator(".settings-reset").click()
|
||||
expect(page.locator("#settings-status")).to_contain_text(
|
||||
"BOR_LLM_THINKING_LEVEL is back to the environment value.", timeout=30_000
|
||||
)
|
||||
# …the value reads ``low`` again with the env badge (the env
|
||||
# var is set — not the code-default badge)…
|
||||
expect(row.locator(".settings-control")).to_have_value("low")
|
||||
expect(row.locator(".settings-src")).to_have_text("env")
|
||||
# …and the server agrees: the override is gone and the
|
||||
# effective value is the env value again.
|
||||
item = _catalog_item(client, app_url, "llm_thinking_level")
|
||||
assert item["source"] == "env" and item["value"] == "low"
|
||||
with SessionLocal() as db:
|
||||
assert (
|
||||
db.execute(
|
||||
text("SELECT count(*) FROM app_settings WHERE key = 'llm_thinking_level'")
|
||||
).scalar()
|
||||
== 0
|
||||
), "the DELETE must remove the row"
|
||||
@@ -516,3 +516,89 @@ def test_catalog_covers_every_settings_field(admin_client) -> None:
|
||||
assert r.status_code == 200
|
||||
assert {item["key"] for item in r.json()["items"]} == set(Settings.model_fields)
|
||||
assert set(CATALOG) == set(Settings.model_fields)
|
||||
|
||||
|
||||
def test_llm_thinking_level_row_contract(
|
||||
admin_client: TestClient, db: Session, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""Phase 172 (task 02): the new ``llm_thinking_level`` row behaves
|
||||
exactly like ``llm_chat_model`` — the machinery is generic over
|
||||
the catalogue (D4 / A5), so the per-key contract is: (a) present in
|
||||
the GET body (group ``LLM (aipi)``, ``str``, effective ``low``
|
||||
with no row in ``app_settings``, ``env_var`` named); (b) PUT stores
|
||||
the RAW string in ``app_settings`` (the overlay's env-native
|
||||
string form) and the response reports the new effective value;
|
||||
(c) the EMPTY value is storable (the off switch, A2/A4 — the API
|
||||
stores as-is, no allowlist); (d) DELETE resets to the env value;
|
||||
(e) the non-admin 403 gate applies (asserted once here; the full
|
||||
four-route matrix is ``test_auth_matrix_all_four_routes``).
|
||||
``test_catalogue_shape_and_sources_on_empty_table`` +
|
||||
``test_catalog_covers_every_settings_field`` already pin the row's
|
||||
PRESENCE generically — these cases pin the per-key behavior.
|
||||
"""
|
||||
# A deterministic base (machine-independent, the kill-switch test's
|
||||
# idiom): the code default, no row in the table.
|
||||
monkeypatch.delenv("BOR_LLM_THINKING_LEVEL", raising=False)
|
||||
invalidate_settings()
|
||||
assert get_settings().llm_thinking_level == "low"
|
||||
|
||||
# (a) the row: group, type, env var, effective ``low`` with NO row
|
||||
# in app_settings, not secret, not restart-required (the LLM layer
|
||||
# reads the setting per request — live).
|
||||
r = admin_client.get(API)
|
||||
assert r.status_code == 200, r.text
|
||||
item = _item(r.json(), "llm_thinking_level")
|
||||
assert item["group"] == "LLM (aipi)"
|
||||
assert item["label"] == "Thinking level"
|
||||
assert item["type"] == "str"
|
||||
assert item["env_var"] == "BOR_LLM_THINKING_LEVEL"
|
||||
assert item["secret"] is False
|
||||
assert item["restart_required"] is False
|
||||
assert item["value"] == "low"
|
||||
assert item["source"] in ("default", "env") # no row in app_settings
|
||||
assert item["has_override"] is False
|
||||
assert item["db_value"] is None
|
||||
assert item["default"] == "low"
|
||||
|
||||
# (b) PUT stores the RAW string (the overlay's D1 — env-native
|
||||
# string form) and the response reports the new effective value.
|
||||
r = admin_client.put(f"{API}/llm_thinking_level", json={"value": "high"})
|
||||
assert r.status_code == 200, r.text
|
||||
assert r.json()["value"] == "high"
|
||||
assert r.json()["source"] == "db"
|
||||
assert r.json()["has_override"] is True
|
||||
assert r.json()["db_value"] == "high"
|
||||
assert db.execute(
|
||||
text("SELECT value FROM app_settings WHERE key = 'llm_thinking_level'")
|
||||
).scalar() == "high" # stored raw, exactly as sent
|
||||
assert get_settings().llm_thinking_level == "high" # live, no restart
|
||||
|
||||
# (c) the EMPTY value is storable (the off switch, A2/A4 — the
|
||||
# API stores as-is; no allowlist, no value validation): the row
|
||||
# holds the empty string and the effective value is ``""``.
|
||||
r = admin_client.put(f"{API}/llm_thinking_level", json={"value": ""})
|
||||
assert r.status_code == 200, r.text
|
||||
assert r.json()["value"] == ""
|
||||
assert r.json()["has_value"] is False # the effective value is empty
|
||||
assert db.execute(
|
||||
text("SELECT value FROM app_settings WHERE key = 'llm_thinking_level'")
|
||||
).scalar() == "" # stored as-is (the off switch)
|
||||
assert get_settings().llm_thinking_level == ""
|
||||
|
||||
# (d) DELETE: the row is gone and the effective value is back to
|
||||
# the env value (``low`` — the base pinned above).
|
||||
r = admin_client.delete(f"{API}/llm_thinking_level")
|
||||
assert r.status_code == 200, r.text
|
||||
assert r.json()["value"] == "low"
|
||||
assert r.json()["has_override"] is False
|
||||
assert db.execute(
|
||||
text("SELECT count(*) FROM app_settings WHERE key = 'llm_thinking_level'")
|
||||
).scalar() == 0 # the row is gone
|
||||
assert get_settings().llm_thinking_level == "low"
|
||||
|
||||
# (e) the non-admin 403 gate still applies (asserted once for this
|
||||
# key — the router-wide dependency is pinned on ALL four routes in
|
||||
# ``test_auth_matrix_all_four_routes``).
|
||||
anon = TestClient(fastapi_app)
|
||||
r = anon.put(f"{API}/llm_thinking_level", json={"value": "high"})
|
||||
assert r.status_code == 403 and r.json() == {"detail": "admin only"}
|
||||
|
||||
@@ -9,7 +9,10 @@ per-use read, ``app/api/chat.py``'s ``plan_turn`` gate), the shared
|
||||
snapshot, ``app/api/chat.py::get_llm``), and the chat concurrency
|
||||
semaphore (``chat_max_concurrent``). Plus the kill-switch counterpart
|
||||
(``BOR_SETTINGS_DB=0`` with rows present → env values are back) and
|
||||
the ``scripts/``-consumer surface (``import_extension_set``).
|
||||
the ``scripts/``-consumer surface (``import_extension_set``), and the
|
||||
phase-172 thinking level (``llm_thinking_level`` — the value the LLM
|
||||
layer reads per request via ``get_settings()``; the off switch is
|
||||
live-settable, A2).
|
||||
|
||||
The rows are inserted directly through the ``db`` session (the admin
|
||||
API is task 04); the save itself is ``invalidate_settings()`` —
|
||||
@@ -160,6 +163,47 @@ def test_llm_client_repoints_on_the_next_save(db: Session) -> None:
|
||||
assert llm.settings.llm_chat_model == "gpt-second"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# the thinking level: save/reset resolve the live value (phase 172, task 02)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_llm_thinking_level_live_apply_no_restart(
|
||||
db: Session, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""Phase 172 (task 02): the thinking level follows the overlay
|
||||
live — a save (the PUT's write, minus the API: ``_save`` +
|
||||
``invalidate_settings()`` is exactly what task 04's PUT/DELETE
|
||||
call after the write) resolves
|
||||
``get_settings().llm_thinking_level`` WITHOUT a restart (the proof
|
||||
the LLM layer — task 03 reads the setting per request via
|
||||
``get_settings()`` — picks the override up live), the EMPTY value
|
||||
resolves live too (the off switch is live-settable, A2/A4), and a
|
||||
reset (the DELETE) returns the env value.
|
||||
"""
|
||||
# A deterministic base (machine-independent, the kill-switch test's
|
||||
# idiom): the code default, no row in the table.
|
||||
monkeypatch.delenv("BOR_LLM_THINKING_LEVEL", raising=False)
|
||||
invalidate_settings()
|
||||
assert get_settings().llm_thinking_level == "low"
|
||||
_save(db, "llm_thinking_level", "high")
|
||||
invalidate_settings()
|
||||
assert get_settings().llm_thinking_level == "high" # NO restart
|
||||
# The off switch is live-settable too (A2/A4): the empty value
|
||||
# resolves through the overlay, not stripped or coerced.
|
||||
_save(db, "llm_thinking_level", "")
|
||||
invalidate_settings()
|
||||
assert get_settings().llm_thinking_level == ""
|
||||
# The reset returns the env value (no process restart in between —
|
||||
# the LLM layer re-reads per request, task 03).
|
||||
db.execute(
|
||||
text("DELETE FROM app_settings WHERE key = 'llm_thinking_level'")
|
||||
)
|
||||
db.commit()
|
||||
invalidate_settings()
|
||||
assert get_settings().llm_thinking_level == "low"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# the chat concurrency semaphore follows the saved cap
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -1547,22 +1547,33 @@ def test_endpoint_log_line_records_suggested_after_summary_hits(
|
||||
``suggested=N`` immediately AFTER ``summary_hits=N`` — the field
|
||||
order of every existing field is untouched (the phase-114
|
||||
``retries=N scaffold_stripped=N`` tail stays last). The value is the
|
||||
seeded suggestion tier's size (``len(plan.suggested_docs)``)."""
|
||||
seeded suggestion tier's size (``len(plan.suggested_docs)``).
|
||||
Phase 172 (A6): the line gains ``thinking=<level>`` immediately
|
||||
BEFORE ``thinking_chars=N`` — the effective (overlay-resolved)
|
||||
``BOR_LLM_THINKING_LEVEL``, trimmed, ``-`` when empty (the off
|
||||
switch)."""
|
||||
_session, _llm = gate_env
|
||||
doc = _doc("Kubernetes Homelab Cluster", "TALOS_DOC_SENT")
|
||||
chunks = [_chunk(doc, 0.30), _chunk(doc, 0.20, is_summary=True)]
|
||||
monkeypatch.setattr(chat_api, "retrieve", _fake_retriever(chunks))
|
||||
|
||||
with caplog.at_level("INFO", logger="app.chat"):
|
||||
_ask(client, "How is my Kubernetes cluster set up?")
|
||||
def _last_log_line() -> str:
|
||||
caplog.clear()
|
||||
with caplog.at_level("INFO", logger="app.chat"):
|
||||
_ask(client, "How is my Kubernetes cluster set up?")
|
||||
log_lines = [r.getMessage() for r in caplog.records if "question=" in r.getMessage()]
|
||||
assert log_lines
|
||||
return log_lines[-1]
|
||||
|
||||
log_lines = [r.getMessage() for r in caplog.records if "question=" in r.getMessage()]
|
||||
assert log_lines
|
||||
line = log_lines[-1]
|
||||
# The default (the fixture's settings carry the field default):
|
||||
# thinking=low, immediately before thinking_chars.
|
||||
line = _last_log_line()
|
||||
assert "thinking=low thinking_chars=" in line
|
||||
# The new slot: suggested=N right after summary_hits=N (one suggested
|
||||
# doc — the single fixture doc — and one summary hit on it).
|
||||
assert "summary_hits=1 suggested=1" in line
|
||||
# The full field order (the phase-114 tail stays last).
|
||||
# The full field order (the phase-114 tail stays last; phase 172
|
||||
# inserts thinking= between sources= and thinking_chars=).
|
||||
order = (
|
||||
"question=",
|
||||
"embed_ms=",
|
||||
@@ -1576,6 +1587,7 @@ def test_endpoint_log_line_records_suggested_after_summary_hits(
|
||||
"threshold=",
|
||||
"deflected=",
|
||||
"sources=",
|
||||
"thinking=",
|
||||
"thinking_chars=",
|
||||
"tool_calls=",
|
||||
"rounds=",
|
||||
@@ -1589,3 +1601,31 @@ def test_endpoint_log_line_records_suggested_after_summary_hits(
|
||||
pos = line.find(field)
|
||||
assert pos > idx, f"{field} out of order in the per-turn log line"
|
||||
idx = pos
|
||||
|
||||
# The effective value: a non-default setting rides the line as-is.
|
||||
monkeypatch.setattr(
|
||||
chat_api,
|
||||
"get_settings",
|
||||
lambda: Settings(_env_file=None, relevance_threshold=0.30, llm_thinking_level="high"), # pyright: ignore[reportCallIssue]
|
||||
)
|
||||
line = _last_log_line()
|
||||
assert "thinking=high thinking_chars=" in line
|
||||
|
||||
# The off switch: an empty setting logs ``-`` ...
|
||||
monkeypatch.setattr(
|
||||
chat_api,
|
||||
"get_settings",
|
||||
lambda: Settings(_env_file=None, relevance_threshold=0.30, llm_thinking_level=""), # pyright: ignore[reportCallIssue]
|
||||
)
|
||||
line = _last_log_line()
|
||||
assert "thinking=- thinking_chars=" in line
|
||||
|
||||
# ... and so does a whitespace-only one (A6: the TRIMMED value
|
||||
# decides, the log field mirrors the wire's trim).
|
||||
monkeypatch.setattr(
|
||||
chat_api,
|
||||
"get_settings",
|
||||
lambda: Settings(_env_file=None, relevance_threshold=0.30, llm_thinking_level=" "), # pyright: ignore[reportCallIssue]
|
||||
)
|
||||
line = _last_log_line()
|
||||
assert "thinking=- thinking_chars=" in line
|
||||
|
||||
@@ -105,6 +105,34 @@ def test_llm_summary_model_env_override(monkeypatch) -> None:
|
||||
assert s.llm_summary_model == "mini"
|
||||
|
||||
|
||||
def test_llm_thinking_level_default_is_low(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Phase 172: the model's thinking level defaults to ``low``
|
||||
(``BOR_LLM_THINKING_LEVEL`` unset)."""
|
||||
monkeypatch.delenv("BOR_LLM_THINKING_LEVEL", raising=False)
|
||||
s = _settings()
|
||||
assert s.llm_thinking_level == "low"
|
||||
|
||||
|
||||
def test_llm_thinking_level_env_override(monkeypatch) -> None:
|
||||
"""Phase 172 (A4): free-form like ``llm_chat_model`` —
|
||||
``BOR_LLM_THINKING_LEVEL=high`` resolves to ``"high"`` (no
|
||||
allowlist, no validator)."""
|
||||
monkeypatch.setenv("BOR_LLM_THINKING_LEVEL", "high")
|
||||
s = _settings()
|
||||
assert s.llm_thinking_level == "high"
|
||||
|
||||
|
||||
def test_llm_thinking_level_empty_env_value_stays_empty(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
"""Phase 172 (A2): the EMPTY value is the off switch — it must not
|
||||
be coerced back to the default (trimming happens only at the wire,
|
||||
task 03; no normalization at load)."""
|
||||
monkeypatch.setenv("BOR_LLM_THINKING_LEVEL", "")
|
||||
s = _settings()
|
||||
assert s.llm_thinking_level == ""
|
||||
|
||||
|
||||
def test_summary_max_chars_default_and_env_override(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Phase 30: document content sent to the ``lite`` model is capped at
|
||||
``BOR_SUMMARY_MAX_CHARS`` (default 12 000 chars per call)."""
|
||||
|
||||
@@ -1324,6 +1324,124 @@ def test_chat_retry_delay_is_flat_never_backoff(monkeypatch: pytest.MonkeyPatch)
|
||||
assert sleeps == [1.25, 1.25, 1.25]
|
||||
|
||||
|
||||
# ---------- thinking level passthrough (phase 172, task 03) ----------
|
||||
|
||||
|
||||
def test_chat_sends_reasoning_effort_extra_body_when_level_set() -> None:
|
||||
"""Phase 172 (A1): a non-empty level rides the one-shot request as
|
||||
the top-level ``reasoning_effort`` parameter via the SDK's
|
||||
``extra_body``."""
|
||||
llm, completions = _make_chat_client(
|
||||
_FakeCompletion("ok"), llm_thinking_level="low"
|
||||
)
|
||||
asyncio.run(llm.chat([{"role": "user", "content": "q"}]))
|
||||
assert completions.chat_kwargs is not None
|
||||
assert completions.chat_kwargs["extra_body"] == {"reasoning_effort": "low"}
|
||||
|
||||
|
||||
def test_chat_default_level_low_is_always_sent() -> None:
|
||||
"""The default (``low``) is always sent (A2): a client built without
|
||||
naming the setting still carries the parameter."""
|
||||
llm, completions = _make_chat_client(_FakeCompletion("ok"))
|
||||
asyncio.run(llm.chat([{"role": "user", "content": "q"}]))
|
||||
assert completions.chat_kwargs is not None
|
||||
assert completions.chat_kwargs["extra_body"] == {"reasoning_effort": "low"}
|
||||
|
||||
|
||||
def test_chat_trims_the_level_before_sending() -> None:
|
||||
"""TRIM: a hand-edited ``.env`` value with trailing whitespace
|
||||
(``"low "``) is sent trimmed — the raw stored string never reaches
|
||||
the wire with surrounding whitespace."""
|
||||
llm, completions = _make_chat_client(
|
||||
_FakeCompletion("ok"), llm_thinking_level="low "
|
||||
)
|
||||
asyncio.run(llm.chat([{"role": "user", "content": "q"}]))
|
||||
assert completions.chat_kwargs is not None
|
||||
assert completions.chat_kwargs["extra_body"] == {"reasoning_effort": "low"}
|
||||
|
||||
|
||||
def test_chat_empty_level_absent_from_kwargs_byte_identical() -> None:
|
||||
"""Phase 172 (A2): a whitespace value is the off switch —
|
||||
``extra_body`` is ABSENT (never None, never ``"off"``) and the
|
||||
request kwargs keep the pre-phase-172 shape."""
|
||||
llm, completions = _make_chat_client(
|
||||
_FakeCompletion("ok"), llm_thinking_level=" "
|
||||
)
|
||||
asyncio.run(llm.chat([{"role": "user", "content": "q"}]))
|
||||
assert completions.chat_kwargs is not None
|
||||
assert "extra_body" not in completions.chat_kwargs
|
||||
assert set(completions.chat_kwargs) == {
|
||||
"model",
|
||||
"messages",
|
||||
"temperature",
|
||||
"max_tokens",
|
||||
"stream",
|
||||
}
|
||||
|
||||
|
||||
def test_chat_stream_sends_reasoning_effort_extra_body_when_level_set() -> None:
|
||||
"""Phase 172 (A1): the chat turn rides the parameter too."""
|
||||
llm, completions = _make_stream_client(
|
||||
[_chunk("x")], llm_thinking_level="high"
|
||||
)
|
||||
asyncio.run(_collect(llm, [{"role": "user", "content": "q"}]))
|
||||
assert completions.kwargs is not None
|
||||
assert completions.kwargs["extra_body"] == {"reasoning_effort": "high"}
|
||||
|
||||
|
||||
def test_chat_stream_trims_the_level_before_sending() -> None:
|
||||
llm, completions = _make_stream_client(
|
||||
[_chunk("x")], llm_thinking_level=" high "
|
||||
)
|
||||
asyncio.run(_collect(llm, [{"role": "user", "content": "q"}]))
|
||||
assert completions.kwargs is not None
|
||||
assert completions.kwargs["extra_body"] == {"reasoning_effort": "high"}
|
||||
|
||||
|
||||
def test_chat_stream_empty_level_absent_from_kwargs_byte_identical() -> None:
|
||||
"""Phase 172 (A2): the stream's off switch — ``extra_body`` absent
|
||||
(never None) and the kwargs dict keeps the pre-phase-172 shape."""
|
||||
llm, completions = _make_stream_client(
|
||||
[_chunk("x")], llm_thinking_level=""
|
||||
)
|
||||
asyncio.run(_collect(llm, [{"role": "user", "content": "q"}]))
|
||||
assert completions.kwargs is not None
|
||||
assert "extra_body" not in completions.kwargs
|
||||
assert set(completions.kwargs) == {
|
||||
"model",
|
||||
"messages",
|
||||
"temperature",
|
||||
"max_tokens",
|
||||
"stream",
|
||||
}
|
||||
|
||||
|
||||
def test_chat_stream_whitespace_level_is_the_off_switch() -> None:
|
||||
"""Whitespace-only (``" "``) is empty for wire purposes (A2)."""
|
||||
llm, completions = _make_stream_client(
|
||||
[_chunk("x")], llm_thinking_level=" "
|
||||
)
|
||||
asyncio.run(_collect(llm, [{"role": "user", "content": "q"}]))
|
||||
assert completions.kwargs is not None
|
||||
assert "extra_body" not in completions.kwargs
|
||||
|
||||
|
||||
def test_embeddings_body_never_carries_reasoning_effort() -> None:
|
||||
"""Phase 172 (A3): the embeddings path is untouched — with the level
|
||||
set, every POST body stays the minimal ``{model, input}`` payload;
|
||||
``reasoning_effort`` never appears in it."""
|
||||
service = _FakeEmbeddingsService()
|
||||
llm, http = _make_client(service, llm_thinking_level="high")
|
||||
asyncio.run(llm.embed(["a", "b"]))
|
||||
vec = asyncio.run(llm.embed_one("c"))
|
||||
assert len(vec) == 768
|
||||
assert http.bodies # the requests actually happened
|
||||
for body in http.bodies:
|
||||
assert set(body) == {"model", "input"}
|
||||
assert "reasoning_effort" not in body
|
||||
assert "extra_body" not in body
|
||||
|
||||
|
||||
# ---------- chat_stream_retried (phase 67, task 01) ----------
|
||||
|
||||
_RETRY_MSGS: list[dict[str, str]] = [{"role": "user", "content": "q"}]
|
||||
|
||||
@@ -93,6 +93,21 @@ def test_env_var_is_bare_key_upper_prefixed() -> None:
|
||||
assert env_var(key).startswith("BOR_")
|
||||
|
||||
|
||||
def test_llm_thinking_level_is_a_plain_live_overridable_row() -> None:
|
||||
"""Phase 172 (task 01): the new ``llm_thinking_level`` pair sits in
|
||||
``LLM (aipi)`` with the ``Thinking level`` label and is plain —
|
||||
not ``secret`` (a free-form text value, like ``llm_chat_model``),
|
||||
not ``restart_required`` (the LLM client reads the setting per
|
||||
request via ``get_settings()`` — live), overridable (the overlay is
|
||||
generic over ``str`` fields)."""
|
||||
meta = CATALOG["llm_thinking_level"]
|
||||
assert meta.group == "LLM (aipi)"
|
||||
assert meta.label == "Thinking level"
|
||||
assert "llm_thinking_level" not in SECRETS
|
||||
assert "llm_thinking_level" not in RESTART_REQUIRED
|
||||
assert "llm_thinking_level" not in NON_OVERRIDABLE
|
||||
|
||||
|
||||
def test_labels_nonempty_and_unique() -> None:
|
||||
labels = [meta.label for meta in CATALOG.values()]
|
||||
assert all(label.strip() for label in labels), "empty label in the catalogue"
|
||||
|
||||
Reference in New Issue
Block a user