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:
@@ -22,14 +22,14 @@ SOURCES_JS = ASSETS / "sources.js"
|
||||
DOCUMENT_JS = ASSETS / "document.js"
|
||||
LOGIN_JS = ASSETS / "login.js"
|
||||
TUNING_JS = ASSETS / "tuning.js"
|
||||
HISTORY_JS = ASSETS / "history.js"
|
||||
STYLES_CSS = ASSETS / "styles.css"
|
||||
|
||||
# Phase 76 (task 02): SOURCES_HTML + GIT_SOURCES_HTML dropped — both
|
||||
# views are folded into the shell (index.html); both files are deleted.
|
||||
INDEX_HTML = FRONTEND / "index.html"
|
||||
SOURCES_HTML = FRONTEND / "sources.html"
|
||||
GIT_SOURCES_HTML = FRONTEND / "git-sources.html"
|
||||
DOCUMENT_HTML = FRONTEND / "document.html"
|
||||
LOGIN_HTML = FRONTEND / "login.html"
|
||||
TUNING_HTML = FRONTEND / "tuning.html"
|
||||
|
||||
|
||||
def _text(path: Path) -> str:
|
||||
@@ -126,27 +126,37 @@ def test_nav_sources_ships_hidden_on_every_nav_page() -> None:
|
||||
all five pages by phase 34 task 03 (owner confirmation 2026-08-26):
|
||||
the RAG nav link is hidden for anonymous — so it SHIPS with the
|
||||
hidden attribute (anonymous-safe default) on every page (they all
|
||||
carry the nav now, viewer included)."""
|
||||
for html in (INDEX_HTML, SOURCES_HTML, DOCUMENT_HTML, LOGIN_HTML, TUNING_HTML):
|
||||
carry the nav now, viewer included).
|
||||
|
||||
Phase 76 (task 01): the page list is the post-shell set — the
|
||||
folded Tuning view's header copy is gone with tuning.html (the
|
||||
shell's ONE header is INDEX_HTML's). Phase 76 (task 02): the RAG
|
||||
+ Sources view files drop out too (the shell's ONE header covers
|
||||
all its views); task 03 drops History."""
|
||||
for html in (INDEX_HTML, DOCUMENT_HTML, LOGIN_HTML):
|
||||
text = _text(html)
|
||||
assert re.search(r'id="nav-sources"[^>]*\bhidden\b', text), (
|
||||
f"{html.name}: #nav-sources must ship hidden"
|
||||
)
|
||||
|
||||
|
||||
def test_all_six_pages_share_the_header_control_order() -> None:
|
||||
def test_shell_and_standalone_pages_share_the_header_control_order() -> None:
|
||||
"""Phase 34 task 03 (owner confirmation 2026-08-26) + phase 35/46
|
||||
(the sixth page, git-sources): every page ships the IDENTICAL
|
||||
header control inventory in the IDENTICAL order — brand, the mobile
|
||||
hamburger, nav [Chat, #nav-sources, #nav-git-sources, #nav-tuning],
|
||||
Sign in, Sign out (the #steering-toggle was removed from the navbar
|
||||
at owner request, 2026-08-28) — inside the shared .header-inner row
|
||||
(the document viewer's row 1). The #sync-btn (Sources page only)
|
||||
and the #new-chat-btn (chat page only, moved from the navbar at
|
||||
(the document viewer's row 1). The #sync-btn (RAG view only)
|
||||
and the #new-chat-btn (chat view only, moved from the navbar at
|
||||
owner request 2026-08-28) are NOT part of the shared bar anymore.
|
||||
Only the current-page is-active nav marker and the static ?next=
|
||||
fallback may differ per page (task 05's story E2E pins the
|
||||
rendered result)."""
|
||||
rendered result).
|
||||
|
||||
Phase 76 (task 02): the post-shell set — the folded views' header
|
||||
copies are gone (the shell's ONE header covers all its views); the
|
||||
RAG/Sources files are deleted, task 03 drops History."""
|
||||
markers = (
|
||||
'class="brand"',
|
||||
'<nav class="app-nav"',
|
||||
@@ -159,12 +169,9 @@ def test_all_six_pages_share_the_header_control_order() -> None:
|
||||
)
|
||||
for html in (
|
||||
INDEX_HTML,
|
||||
SOURCES_HTML,
|
||||
GIT_SOURCES_HTML,
|
||||
DOCUMENT_HTML,
|
||||
TUNING_HTML,
|
||||
LOGIN_HTML,
|
||||
):
|
||||
): # phase 76 (tasks 01/02): the folded view files are gone — the shell's ONE header stands in
|
||||
text = _text(html)
|
||||
start = text.find('<div class="container header-inner">')
|
||||
assert start != -1, f"{html.name}: missing the shared .header-inner row"
|
||||
@@ -185,8 +192,14 @@ def test_all_five_pages_carry_the_steering_panel() -> None:
|
||||
"""Phase 34 task 03: the #steering-panel section (+ the
|
||||
#steering-announcer live region) ships on every page — after
|
||||
#kb-banner in the chat shell, first child of <main> on the other
|
||||
four pages — ship hidden, driven by assets/header.js."""
|
||||
for html in (INDEX_HTML, SOURCES_HTML, DOCUMENT_HTML, TUNING_HTML, LOGIN_HTML):
|
||||
four pages — ship hidden, driven by assets/header.js.
|
||||
|
||||
Phase 76 (task 01): the folded Tuning view's panel COPY is dropped
|
||||
(the shell keeps the chat's ONE instance — duplicate ids are not
|
||||
legal). Phase 76 (task 02): the folded RAG + Sources view copies
|
||||
are dropped with their files (the shell's ONE panel stands in);
|
||||
task 03 drops History. The page list is the post-shell set."""
|
||||
for html in (INDEX_HTML, DOCUMENT_HTML, LOGIN_HTML):
|
||||
text = _text(html)
|
||||
tag = re.search(r'<section[^>]*id="steering-panel"[^>]*>', text)
|
||||
assert tag, f"{html.name}: missing the #steering-panel section"
|
||||
@@ -200,23 +213,32 @@ def test_all_five_pages_carry_the_steering_panel() -> None:
|
||||
assert text.find('id="steering-panel"') < text.find('id="steering-announcer"')
|
||||
|
||||
|
||||
def test_nav_tuning_ships_hidden_on_the_tuning_page() -> None:
|
||||
"""Phase 27: the Global Tuning page reuses the shared header — the
|
||||
"Tuning" nav link is admin-only, so it SHIPS hidden (revealed by
|
||||
initSharedHeader once whoami says admin), is the page's active link
|
||||
(is-active + aria-current), and the page loads markdown.js (classic)
|
||||
+ the tuning.js module with NO direct header.js <script> tag
|
||||
(single-evaluation design)."""
|
||||
text = _text(TUNING_HTML)
|
||||
def test_nav_tuning_ships_hidden_and_unstamped_on_the_shell() -> None:
|
||||
"""Phase 27 + phase 76 (task 01) — the shell form of this pin: the
|
||||
"Tuning" nav link is admin-only, so it SHIPS hidden in the shell's
|
||||
ONE header (revealed by initSharedHeader once whoami says admin).
|
||||
The old page-level active stamp (is-active + aria-current on
|
||||
tuning.html's own link) is GONE — the router is the SINGLE WRITER
|
||||
of the active state (client-side, per view); only the Chat link
|
||||
may carry a static stamp. The shell loads markdown.js (classic) +
|
||||
app.js + router.js (modules) with NO direct header.js <script> tag
|
||||
(single-evaluation design), and no direct tuning.js tag (the router
|
||||
lazy-imports it on first show — mount-once)."""
|
||||
text = _text(INDEX_HTML)
|
||||
tag = re.search(r'<a[^>]*id="nav-tuning"[^>]*>', text)
|
||||
assert tag, "tuning.html must carry the #nav-tuning nav link"
|
||||
assert 'class="nav-link is-active"' in tag.group(0), "the Tuning link is the active one"
|
||||
assert 'aria-current="page"' in tag.group(0)
|
||||
assert tag, "the shell must carry the #nav-tuning nav link"
|
||||
assert "hidden" in tag.group(0), "#nav-tuning must ship hidden (admin-only)"
|
||||
srcs = _script_srcs(TUNING_HTML)
|
||||
assert "is-active" not in tag.group(0), (
|
||||
"no static active stamp — the router is the single writer"
|
||||
)
|
||||
assert 'aria-current="page"' not in tag.group(0)
|
||||
srcs = _script_srcs(INDEX_HTML)
|
||||
assert [s for s in srcs if "header.js" in s] == [], "no direct header.js <script> tag"
|
||||
assert [s for s in srcs if "markdown.js" in s]
|
||||
assert [s for s in srcs if "tuning.js" in s]
|
||||
assert [s for s in srcs if "router.js" in s], "the shell loads the router module"
|
||||
assert [s for s in srcs if "tuning.js" in s] == [], (
|
||||
"no direct tuning.js tag — the router lazy-imports the view module"
|
||||
)
|
||||
|
||||
|
||||
def test_viewer_carries_the_standard_nav() -> None:
|
||||
@@ -253,27 +275,52 @@ def test_viewer_carries_the_standard_nav() -> None:
|
||||
assert back, "the back link keeps its /sources.html no-JS fallback"
|
||||
|
||||
|
||||
def test_sources_and_viewer_carry_the_shared_controls() -> None:
|
||||
"""Sources AND the document viewer carry the Sign in / Sign out pair
|
||||
(both starting hidden — initSharedHeader reveals exactly one after
|
||||
whoami), and their page scripts load header.js (phase 23: via the
|
||||
page script's relative import, not a direct script tag). The New
|
||||
chat button is NOT on non-chat pages — it moved from the navbar to
|
||||
the chat page at owner request (2026-08-28; the single binding is
|
||||
pinned in test_new_chat_binding_is_single_and_module_owned)."""
|
||||
for html in (SOURCES_HTML, DOCUMENT_HTML):
|
||||
def test_sources_view_and_viewer_carry_the_shared_controls() -> None:
|
||||
"""Sources (the shell's RAG view, phase 76 task 02) AND the document
|
||||
viewer carry the Sign in / Sign out pair (both starting hidden —
|
||||
initSharedHeader reveals exactly one after whoami), and their
|
||||
scripts load header.js (phase 23: via the relative import, not a
|
||||
direct script tag). The New chat button is chat-view only — in the
|
||||
shell its ONE instance lives in the chat view (hidden + inert on
|
||||
every other view); the viewer carries none (the single binding is
|
||||
pinned in test_new_chat_binding_is_single_and_module_owned).
|
||||
The RAG view module does NOT boot the shell's header (it boots once
|
||||
via app.js) — it keeps fetchIsAdmin() for its admin gate (zero
|
||||
extra requests)."""
|
||||
for html in (INDEX_HTML, DOCUMENT_HTML):
|
||||
text = _text(html)
|
||||
assert 'id="new-chat-btn"' not in text, (
|
||||
f"{html.name}: the New chat button is chat-page only (owner request)"
|
||||
)
|
||||
assert re.search(r'id="sign-in-link"[^>]*\bhidden\b', text)
|
||||
assert re.search(r'id="sign-out-btn"[^>]*\bhidden\b', text)
|
||||
for js_file in (SOURCES_JS, DOCUMENT_JS):
|
||||
assert 'from "./header.js"' in _text(js_file), (
|
||||
f"{js_file.name}: page script must load the shared header module"
|
||||
)
|
||||
# The New chat button: exactly ONE instance in the shell (the chat
|
||||
# view's) — none on the viewer.
|
||||
assert _text(INDEX_HTML).count('id="new-chat-btn"') == 1
|
||||
assert 'id="new-chat-btn"' not in _text(DOCUMENT_HTML), (
|
||||
"the New chat button is chat-view only (owner request)"
|
||||
)
|
||||
assert 'from "./header.js"' in _text(DOCUMENT_JS), (
|
||||
"document.js: page script must load the shared header module"
|
||||
)
|
||||
# The RAG view module (the phase-76 view-module branch):
|
||||
# relative import, NO header boot, the fetchIsAdmin gate.
|
||||
rag_js = _text(SOURCES_JS)
|
||||
assert 'from "./header.js"' in rag_js
|
||||
import_lines = [
|
||||
line for line in rag_js.splitlines() if line.strip().startswith("import")
|
||||
]
|
||||
assert all("initSharedHeader" not in line for line in import_lines), (
|
||||
"the view must not import the header boot — the shell's header boots "
|
||||
"via the chat module (app.js) at shell boot"
|
||||
)
|
||||
assert "await initSharedHeader()" not in rag_js, (
|
||||
"the view must not re-boot the shell's header"
|
||||
)
|
||||
assert "fetchIsAdmin()" in rag_js, "the admin gate keeps the shared cached promise"
|
||||
assert "new-chat-btn" not in rag_js, (
|
||||
"no #new-chat-btn binding (the module owns the single instance)"
|
||||
)
|
||||
assert 'fetch("/api/whoami")' not in rag_js
|
||||
# Each page's Sign in link returns to ITS OWN page after login.
|
||||
assert 'href="/login.html?next=/sources.html"' in _text(SOURCES_HTML)
|
||||
assert 'href="/login.html?next=/sources.html"' in _text(INDEX_HTML)
|
||||
assert 'href="/login.html?next=/document.html"' in _text(DOCUMENT_HTML)
|
||||
|
||||
|
||||
@@ -288,7 +335,6 @@ def test_header_module_loads_before_the_page_script() -> None:
|
||||
would double-bind the sign-out listener)."""
|
||||
cases = [
|
||||
(INDEX_HTML, "app.js"),
|
||||
(SOURCES_HTML, "sources.js"),
|
||||
(DOCUMENT_HTML, "document.js"),
|
||||
(LOGIN_HTML, "login.js"),
|
||||
]
|
||||
@@ -307,6 +353,20 @@ def test_header_module_loads_before_the_page_script() -> None:
|
||||
assert 'from "/assets/header.js"' not in js, (
|
||||
f"{page_script}: absolute header import would break the esbuild bundle"
|
||||
)
|
||||
# Phase 76 (task 02): the folded RAG view module is NOT directly
|
||||
# loaded by the shell — the router lazy-imports it on first show
|
||||
# (mount-once, like the Tuning view module); it still imports
|
||||
# header.js relatively (single-evaluation design).
|
||||
shell_srcs = _script_srcs(INDEX_HTML)
|
||||
assert [s for s in shell_srcs if "sources.js" in s] == [], (
|
||||
"no direct sources.js <script> tag — the router lazy-imports the view"
|
||||
)
|
||||
assert [s for s in shell_srcs if "router.js" in s], (
|
||||
"the router is the loader of the folded view modules"
|
||||
)
|
||||
js = _text(SOURCES_JS)
|
||||
assert 'from "./header.js"' in js
|
||||
assert 'from "/assets/header.js"' not in js
|
||||
|
||||
|
||||
def test_login_page_carries_the_full_header() -> None:
|
||||
@@ -403,16 +463,71 @@ def test_new_chat_binding_is_single_and_module_owned() -> None:
|
||||
assert 'window.location.href = "/"' not in js, (
|
||||
"no navigate-to-chat branch left (the button left the navbar)"
|
||||
)
|
||||
for js_file in (SOURCES_JS, DOCUMENT_JS, TUNING_JS):
|
||||
page_js = _text(js_file)
|
||||
assert 'from "./header.js"' in page_js
|
||||
assert "initSharedHeader()" in page_js
|
||||
assert "new-chat-btn" not in page_js, (
|
||||
f"{js_file.name}: no #new-chat-btn binding (the module owns it)"
|
||||
)
|
||||
assert 'fetch("/api/whoami")' not in page_js, (
|
||||
f"{js_file.name}: whoami goes through the shared cached promise"
|
||||
)
|
||||
doc_js = _text(DOCUMENT_JS)
|
||||
assert 'from "./header.js"' in doc_js
|
||||
assert "initSharedHeader()" in doc_js
|
||||
assert "new-chat-btn" not in doc_js, (
|
||||
"document.js: no #new-chat-btn binding (the module owns it)"
|
||||
)
|
||||
assert 'fetch("/api/whoami")' not in doc_js, (
|
||||
"document.js: whoami goes through the shared cached promise"
|
||||
)
|
||||
# Phase 76 (task 02): the RAG view module is the VIEW-MODULE branch
|
||||
# (like the Tuning view module below) — it does not boot the shell's
|
||||
# header, binds no #new-chat-btn, and gates on the shared cached
|
||||
# promise.
|
||||
rag_js = _text(SOURCES_JS)
|
||||
assert 'from "./header.js"' in rag_js
|
||||
assert "await initSharedHeader()" not in rag_js
|
||||
assert "fetchIsAdmin()" in rag_js, "the admin gate keeps the shared cached promise"
|
||||
assert "new-chat-btn" not in rag_js, (
|
||||
"sources.js: no #new-chat-btn binding (the module owns it)"
|
||||
)
|
||||
assert 'fetch("/api/whoami")' not in rag_js, (
|
||||
"sources.js: whoami goes through the shared cached promise"
|
||||
)
|
||||
# Phase 76 (task 01): the folded Tuning VIEW module no longer boots
|
||||
# the header — in the shell it runs exactly once, via the chat
|
||||
# module (app.js) at shell boot. The view keeps fetchIsAdmin() for
|
||||
# its admin gate (the SAME cached whoami promise — zero extra
|
||||
# requests) and imports the module relatively (single-evaluation).
|
||||
tuning_js = _text(TUNING_JS)
|
||||
assert 'from "./header.js"' in tuning_js
|
||||
# No CALL to the header boot — the import line carries no
|
||||
# initSharedHeader and no `await initSharedHeader()` call site
|
||||
# exists (a docstring may name it; a call may not).
|
||||
import_lines = [
|
||||
line for line in tuning_js.splitlines() if line.strip().startswith("import")
|
||||
]
|
||||
assert all("initSharedHeader" not in line for line in import_lines), (
|
||||
"the view must not import the header boot — the shell's header boots "
|
||||
"via the chat module (app.js) at shell boot"
|
||||
)
|
||||
assert "await initSharedHeader()" not in tuning_js, (
|
||||
"the view must not re-boot the shell's header"
|
||||
)
|
||||
assert "fetchIsAdmin()" in tuning_js, "the admin gate keeps the shared cached promise"
|
||||
assert "new-chat-btn" not in tuning_js
|
||||
assert 'fetch("/api/whoami")' not in tuning_js
|
||||
# Phase 76 (task 03): the folded History VIEW module — the same
|
||||
# view-module contract: no header boot, no #new-chat-btn binding,
|
||||
# the admin gate on the shared cached whoami promise (zero extra
|
||||
# requests), the relative import for single evaluation.
|
||||
history_js = _text(HISTORY_JS)
|
||||
assert 'from "./header.js"' in history_js
|
||||
import_lines = [
|
||||
line for line in history_js.splitlines() if line.strip().startswith("import")
|
||||
]
|
||||
assert all("initSharedHeader" not in line for line in import_lines), (
|
||||
"the view must not import the header boot — the shell's header boots "
|
||||
"via the chat module (app.js) at shell boot"
|
||||
)
|
||||
assert "await initSharedHeader()" not in history_js, (
|
||||
"the view must not re-boot the shell's header"
|
||||
)
|
||||
assert "fetchIsAdmin()" in history_js, "the admin gate keeps the shared cached promise"
|
||||
assert "new-chat-btn" not in history_js
|
||||
assert 'fetch("/api/whoami")' not in history_js
|
||||
app_js = _text(APP_JS)
|
||||
assert 'window.addEventListener("bor:new-chat", startNewChat)' in app_js
|
||||
assert "newChatBtn.addEventListener" not in app_js, (
|
||||
|
||||
Reference in New Issue
Block a user