fix(chat): keep in-flight answers alive across in-app view switches
Root cause (owner repro, verified in a real browser 2026-09-06): the five navbar views (Chat, RAG, Sources, Tuning, History) were separate HTML documents, so a navbar click was a REAL cross-document navigation — the chat page unloaded, the in-flight SSE fetch was aborted, and the phase-48 teardown (app/api/chat.py `finally`, "chat: turn cancelled") stopped the model. Observed: send question -> click RAG mid-stream -> click Chat -> the answer never finished: no `query_log` row, and on return a dangling question with no brain record (the pre-token pagehide partial persist skips because `acc` is empty). Phase-48 LOCKED-DECISION REFINEMENT (owner-confirmed 2026-09-06, flagged per AGENTS.md rule 3, not silently deviated): "real navigation cancels the fetch" now means LEAVING THE APP — tab close, external/other-document navigation, the Stop button. In-app navbar switches are client-side view switches and no longer cancel. Fix — Option A (SPA shell), chosen over B (Service Worker owns the stream) and C (server-side turn registry + resume): - frontend/index.html is the shell: ONE `<main id="main">` holds the five `<section class="view">` blocks; hidden views carry BOTH `hidden` and `inert` (WCAG — no focus/keyboard traversal). The shared header, the single `doc-modal-*` skeleton, and the `#app-version` footer each exist exactly once; the per-view copies from the four folded pages are dropped. - New frontend/assets/router.js (vanilla module — no framework, no bundler, No-CDN rule intact): lazy-imports a view module on FIRST show only (mount-once, hide-forever — the chat view's in-flight SSE reader persists across switches; that persistence IS the fix); intercepts same-shell navbar links with preventDefault + history.pushState (never a document load); handles popstate; single writer of `.nav-link` active state (is-active + aria-current), document.title, and the per-view meta description (values carried over from the old pages' heads, brand-resolved at write time). - Each folded page's JS becomes `export async function mount(root)` — root-scoped queries; `initSharedHeader()` dropped (the header boots once in the shell via the chat module; the admin flag comes from the same cached `fetchIsAdmin()` promise — zero extra requests). - app/main.py: a small list-driven route factory serves the shell for /tuning.html, /sources.html, /git-sources.html, /history.html — registered AFTER the API routers and BEFORE the static catch-all (routes-first). The phase-33 caching middleware applies no-cache + `?v=` rewriting unchanged; app/core/caching.py needed NO change (the view paths did not change — pinned by the integration tests). - The four old view .html files are DELETED (one source of truth); deep links to the old URLs keep working (the router picks the view from the pathname); `/?chat=<id>` is unaffected; the Containerfile bundles router.js (inlining the lazy view modules) and drops the folded page files. - app/schemas.py: HistoryTurn.text cap 4000 -> 32000 — the shell keeps long saved answers in the chat, and the old cap (stricter than the 24_000-char total history budget) 422-rejected any second turn in such a chat (found by the phase-42 E2E suite on the shell). Boundaries: login.html, shared.html, doc-edit.html, document.html REMAIN separate documents (flow pages, not navbar tabs); a mid-stream navigation to doc-edit/document.html still cancels per phase 48 (follow-up candidate, out of scope). The SSE API is unchanged. Real departures still cancel the turn — phase 48 intact (pinned by tests/e2e/test_stop_generation.py, unchanged, and by the new suite's real-departure control). Tests: - Phase-20 suite REWRITTEN to the new semantics (tests/e2e/test_sources_midstream_bug.py): a navbar switch no longer cancels — the stream survives the switch and the FULL answer settles; the pagehide partial persist REMAINS for real departures (the partial's exact shape — first streamed chunk prefix, no done metadata — is still pinned there). - NEW story suite tests/e2e/test_nav_switch_keeps_stream.py (mock LLM): the owner repro (send -> RAG mid-stream -> Chat: window sentinel survives = same document, FULL answer, exactly one brain turn in bor.chat.v1, exactly one settled query_log row, auto-saved row matches) + the same mid-stream switch against the other three views + the real-departure-still-cancels control + the no-switch baseline. - tests/unit/test_frontend_router.py: source-level pins of the router invariants (click interceptor targets ONLY same-shell view paths, pushState-only switches, mount-once guard, hidden+inert pair, single-writer active state/title); shell-route integration tests (each folded path serves the shell with no-cache + `?v=` body; a non-view path still 404s); the file-reading unit pins re-pointed at the shell (the four view files are gone — the shell is the source of truth). Verification (this commit): full suite green — 1565 unit+integration tests, app/ coverage 99% (>90% floor); ruff + pyright clean; the phase's E2E suites green in isolation (house protocol, AGENTS.md rule 9). Owner repro verified in a real browser against the real LLM (dev server :8010, headful Chromium): "tell me about everquest" -> RAG mid-stream -> Chat — the answer completed with one brain bubble and no error banner, `query_log` gained exactly one settled row (deflected=True: the dev KB holds no EverQuest docs — the settle, not the topic, is the proof), zero "chat: turn cancelled" lines for that turn; the control (real navigation to /shared.html mid-stream) still cancelled (no settled row, the cancel line logged, the partial persisted on return). Screenshots: .agents/screenshots/76_manual_*. Phase 76 (76_spa_nav_shell) complete — moved to .agents/phases/complete/.
This commit is contained in:
@@ -44,7 +44,9 @@ ASSETS = FRONTEND / "assets"
|
||||
HEADER_JS = ASSETS / "header.js"
|
||||
SOURCES_JS = ASSETS / "sources.js"
|
||||
STYLES_CSS = ASSETS / "styles.css"
|
||||
SOURCES_HTML = FRONTEND / "sources.html"
|
||||
# Phase 76 (task 02): sources.html is folded into the ONE-document shell
|
||||
# — the sync markup now lives in the RAG view section of index.html.
|
||||
SHELL_HTML = FRONTEND / "index.html"
|
||||
|
||||
|
||||
def _text(path: Path) -> str:
|
||||
@@ -52,23 +54,46 @@ def _text(path: Path) -> str:
|
||||
return path.read_text(encoding="utf-8")
|
||||
|
||||
|
||||
def _rag_view(html: str) -> str:
|
||||
"""The RAG view section of the shell (view-scoped page-sub scope —
|
||||
the shell carries one .page-sub per view, so a whole-file match
|
||||
would hit the earlier views first)."""
|
||||
i = html.find('<section class="view" id="view-rag"')
|
||||
assert i != -1, "the RAG view section must be in the shell"
|
||||
j = html.find('<section class="view" id="view-git-sources"', i)
|
||||
assert j != -1, "the Sources view section must follow the RAG view"
|
||||
return html[i:j]
|
||||
|
||||
|
||||
def _body(js: str, fn_name: str) -> str:
|
||||
"""The source of the first top-level `function <fn_name>` in js."""
|
||||
"""The source of the first `function <fn_name>` in js (brace
|
||||
balanced — since phase 76 task 02 the functions live inside
|
||||
mount(root), so the closing brace is indented, not column 0)."""
|
||||
fn = js.find(f"function {fn_name}")
|
||||
assert fn != -1, f"{fn_name} must be defined"
|
||||
return js[fn : js.find("\n}", fn)]
|
||||
open_idx = js.find("{", fn)
|
||||
depth = 0
|
||||
for i in range(open_idx, len(js)):
|
||||
c = js[i]
|
||||
if c == "{":
|
||||
depth += 1
|
||||
elif c == "}":
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
return js[fn : i + 1]
|
||||
raise AssertionError(f"unbalanced braces in {fn_name}")
|
||||
|
||||
|
||||
# ---------- sources.html: anonymous-safe ship-hidden markup ----------
|
||||
# ---------- the shell's RAG view: anonymous-safe ship-hidden markup ----------
|
||||
|
||||
|
||||
def test_sync_button_ships_hidden_and_labeled() -> None:
|
||||
"""#sync-btn SHIPS with the hidden attribute (anonymous-safe —
|
||||
sources.js reveals it for the admin at page boot), is a real
|
||||
sources.js reveals it for the admin at view boot), is a real
|
||||
<button type="button">, and carries aria-label="Sync sources" so
|
||||
the accessible name stays stable across the label states."""
|
||||
tag = re.search(r"<button[^>]*id=\"sync-btn\"[^>]*>", _text(SOURCES_HTML))
|
||||
assert tag, "sources.html must carry the #sync-btn button"
|
||||
tag = re.search(r"<button[^>]*id=\"sync-btn\"[^>]*>", _text(SHELL_HTML))
|
||||
assert tag, "the shell must carry the #sync-btn button (RAG view)"
|
||||
attrs = tag.group(0)
|
||||
assert 'class="sync-btn"' in attrs
|
||||
assert 'type="button"' in attrs
|
||||
@@ -80,7 +105,7 @@ def test_sync_button_has_icon_and_label_span() -> None:
|
||||
"""The button body is a refresh-cycle svg (aria-hidden — decorative,
|
||||
the spin is the visible running state) + the .sync-label span with
|
||||
the idle text, so the label can be swapped by sources.js."""
|
||||
text = _text(SOURCES_HTML)
|
||||
text = _text(SHELL_HTML)
|
||||
btn = text[text.find('id="sync-btn"') : text.find("</button>", text.find('id="sync-btn"'))]
|
||||
assert re.search(r'<svg[^>]*class="sync-icon"[^>]*aria-hidden="true"', btn)
|
||||
# class for the module query + id for the E2E label assertions
|
||||
@@ -95,9 +120,9 @@ def test_sync_result_is_the_aria_live_announcer() -> None:
|
||||
"""#sync-result sits right after the button and is a polite live
|
||||
region (role="status" + aria-live="polite") — the last-result /
|
||||
counts announcement for screen readers."""
|
||||
text = _text(SOURCES_HTML)
|
||||
text = _text(SHELL_HTML)
|
||||
tag = re.search(r'<span[^>]*id="sync-result"[^>]*>', text)
|
||||
assert tag, "sources.html must carry the #sync-result announcer"
|
||||
assert tag, "the shell must carry the #sync-result announcer (RAG view)"
|
||||
attrs = tag.group(0)
|
||||
assert 'role="status"' in attrs
|
||||
assert 'aria-live="polite"' in attrs
|
||||
@@ -108,9 +133,9 @@ def test_sync_error_banner_is_a_hidden_alert() -> None:
|
||||
"""The failure banner uses the chat error-banner markup style
|
||||
(kb-banner + is-error) and role="alert", shipping hidden —
|
||||
sources.js un-hides it with the error text on a failed run."""
|
||||
text = _text(SOURCES_HTML)
|
||||
text = _text(SHELL_HTML)
|
||||
tag = re.search(r'<div[^>]*id="sync-error-banner"[^>]*>', text)
|
||||
assert tag, "sources.html must carry the #sync-error-banner"
|
||||
assert tag, "the shell must carry the #sync-error-banner (RAG view)"
|
||||
attrs = tag.group(0)
|
||||
assert "kb-banner" in attrs and "is-error" in attrs
|
||||
assert 'role="alert"' in attrs
|
||||
@@ -125,9 +150,10 @@ def test_page_sub_copy_mentions_the_button() -> None:
|
||||
the latest and re-import (the import CLI docs live elsewhere).
|
||||
Phase 61: the copy describes the current source model (git repos +
|
||||
local directories + uploaded archives), not the old ~/Homelab +
|
||||
~/Deployments clone."""
|
||||
sub = re.search(r'<p class="page-sub">(.*?)</p>', _text(SOURCES_HTML), re.DOTALL)
|
||||
assert sub, "sources.html must keep the .page-sub copy"
|
||||
~/Deployments clone. Phase 76 (task 02): scoped to the RAG view —
|
||||
the shell carries one .page-sub per view."""
|
||||
sub = re.search(r'<p class="page-sub">(.*?)</p>', _rag_view(_text(SHELL_HTML)), re.DOTALL)
|
||||
assert sub, "the RAG view must keep the .page-sub copy"
|
||||
copy = re.sub(r"\s+", " ", sub.group(1)) # the markup wraps lines
|
||||
assert "Press <strong>Sync sources</strong>" in copy
|
||||
assert "pull the latest and re-import" in copy
|
||||
@@ -139,22 +165,23 @@ def test_sources_page_stays_cdn_free() -> None:
|
||||
"""No-CDN rule (PLAN §7.3, A11): the new button markup adds no
|
||||
external references — same-origin assets only (the integration
|
||||
test_index_html_served_locally re-checks this on the served page)."""
|
||||
text = _text(SOURCES_HTML)
|
||||
text = _text(SHELL_HTML)
|
||||
assert 'src="https://' not in text
|
||||
assert 'href="https://' not in text
|
||||
|
||||
|
||||
# ---------- sources.js: the admin reveal (page boot) ----------
|
||||
# ---------- sources.js: the admin reveal (view boot) ----------
|
||||
|
||||
|
||||
def test_sources_js_reveals_sync_btn_on_the_admin_branch() -> None:
|
||||
"""The page boot reveals #sync-btn for the admin on the SAME cached
|
||||
whoami initSharedHeader() used (no extra fetch — header.js keeps
|
||||
the single /api/whoami call site); anonymous users never leave the
|
||||
ship-hidden default."""
|
||||
"""The view boot (mount's tail, phase 76 task 02) reveals #sync-btn
|
||||
for the admin on the SAME cached whoami fetchIsAdmin() reads (no
|
||||
extra fetch — header.js keeps the single /api/whoami call site; the
|
||||
header itself is booted exactly once, by the chat module at shell
|
||||
boot); anonymous users never leave the ship-hidden default."""
|
||||
js = _text(SOURCES_JS)
|
||||
boot = js[js.rfind("(async () => {"):]
|
||||
assert "const admin = await initSharedHeader()" in boot
|
||||
boot = js[js.find("view boot (phase 76 task 02)"):]
|
||||
assert "const admin = await fetchIsAdmin()" in boot
|
||||
assert "syncBtn.hidden = !admin" in boot, "#sync-btn must join the admin reveal"
|
||||
# The reveal must not introduce a second whoami call site.
|
||||
header = _text(HEADER_JS)
|
||||
@@ -337,16 +364,14 @@ def test_sources_js_reattaches_on_load_admin_only() -> None:
|
||||
result, idle settles retry-ready; the click binding wires
|
||||
startSync to the button."""
|
||||
js = _text(SOURCES_JS)
|
||||
fn = js.find("function initSyncButton")
|
||||
assert fn != -1
|
||||
body = js[fn : js.find("\n}\n", fn)]
|
||||
body = _body(js, "initSyncButton")
|
||||
assert "await fetchIsAdmin()" in body, "admin-only boot (no extra fetch)"
|
||||
assert 'fetch("/api/sync/status")' in body
|
||||
assert 'status.state === "running"' in body
|
||||
assert 'status.state === "success"' in body
|
||||
assert 'status.state === "failed"' in body
|
||||
assert (
|
||||
'syncBtn.addEventListener("click", startSync);\n initSyncButton();'
|
||||
'syncBtn.addEventListener("click", startSync);\n initSyncButton();'
|
||||
in js
|
||||
), "the click binding and the boot re-attach ship together, guarded on syncBtn"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user