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:
@@ -41,8 +41,7 @@ SOURCES_JS = FRONTEND / "assets" / "sources.js"
|
||||
DOCUMENT_JS = FRONTEND / "assets" / "document.js"
|
||||
MARKDOWN_JS = FRONTEND / "assets" / "markdown.js"
|
||||
MODAL_JS = FRONTEND / "assets" / "document-modal.js" # phase 26: the modal owner
|
||||
INDEX_HTML = FRONTEND / "index.html"
|
||||
SOURCES_HTML = FRONTEND / "sources.html"
|
||||
INDEX_HTML = FRONTEND / "index.html" # the ONE-document shell (phase 76: sources.html folded in)
|
||||
|
||||
HAVE_NODE = shutil.which("node") is not None
|
||||
|
||||
@@ -294,10 +293,12 @@ def test_markdown_renderer_stays_xss_safe_and_unchanged() -> None:
|
||||
def test_render_document_exported_and_modal_imports_it() -> None:
|
||||
"""Phase 26: document.js EXPORTS renderDocument(doc, { … }) — the
|
||||
exact renderer the standalone page and the modal share (no drift).
|
||||
The modal module imports it relatively, and BOTH page scripts import
|
||||
the modal module relatively — no direct <script> tag (the header.js
|
||||
single-evaluation design: esbuild inlines it into the page bundle,
|
||||
one module instance per page)."""
|
||||
The modal module imports it relatively, and BOTH view scripts
|
||||
(app.js — the chat view at shell boot — and sources.js — the RAG
|
||||
view module) import the modal module relatively — no direct
|
||||
<script> tag (the header.js single-evaluation design: esbuild
|
||||
inlines it into the bundle, one module instance per document).
|
||||
"""
|
||||
doc_js = _read(DOCUMENT_JS)
|
||||
# Task 03 signature: the page passes its #doc-title / #doc-meta /
|
||||
# #doc-content elements under exactly these names.
|
||||
@@ -315,12 +316,11 @@ def test_render_document_exported_and_modal_imports_it() -> None:
|
||||
assert 'from "./document-modal.js"' in js, (
|
||||
f"{name}: must import the modal module relatively"
|
||||
)
|
||||
for page in (INDEX_HTML, SOURCES_HTML):
|
||||
text = _read(page)
|
||||
assert not re.search(r"<script[^>]*document-modal\.js", text), (
|
||||
f"{page.name}: no direct document-modal.js <script> tag "
|
||||
"(single-evaluation design — the page script imports it)"
|
||||
)
|
||||
text = _read(INDEX_HTML)
|
||||
assert not re.search(r"<script[^>]*document-modal\.js", text), (
|
||||
"index.html: no direct document-modal.js <script> tag "
|
||||
"(single-evaluation design — the view scripts import it)"
|
||||
)
|
||||
|
||||
|
||||
def test_document_js_page_init_is_import_safe() -> None:
|
||||
@@ -338,37 +338,43 @@ def test_document_js_page_init_is_import_safe() -> None:
|
||||
)
|
||||
|
||||
|
||||
def test_both_pages_carry_the_modal_skeleton() -> None:
|
||||
"""Phase 26: chat AND Sources ship the same modal skeleton (the
|
||||
task-01 markup) — the a11y frame included: role=dialog +
|
||||
aria-modal, a labelled close control, a focusable content target
|
||||
(tabindex=-1), and a role=status announcer. Hidden by default —
|
||||
inert until JS opens it."""
|
||||
for page in (INDEX_HTML, SOURCES_HTML):
|
||||
text = _read(page)
|
||||
assert '<div class="doc-modal" id="doc-modal" hidden>' in text, page.name
|
||||
assert 'id="doc-modal-backdrop"' in text, page.name
|
||||
assert 'id="doc-modal-panel"' in text, page.name
|
||||
assert 'role="dialog"' in text and 'aria-modal="true"' in text, page.name
|
||||
assert 'id="doc-modal-title"' in text, page.name
|
||||
assert 'id="doc-modal-meta"' in text, page.name
|
||||
assert 'id="doc-modal-desc"' in text, page.name
|
||||
assert 'id="doc-modal-open"' in text, page.name
|
||||
assert re.search(r'id="doc-modal-content"[^>]*tabindex="-1"', text), page.name
|
||||
assert re.search(
|
||||
r'id="doc-modal-close"[^>]*aria-label="Close document"', text
|
||||
), page.name
|
||||
def test_shell_carries_the_single_modal_skeleton() -> None:
|
||||
"""Phase 26 + phase 76 (task 02) dedup pin: the shell ships the
|
||||
modal skeleton EXACTLY ONCE (the chat's, body level) — the RAG
|
||||
view's second copy was dropped in the fold; BOTH view scripts
|
||||
(app.js chat chips, sources.js RAG row links) open documents
|
||||
through openDocumentModal(...) against that single instance
|
||||
(document-modal.js resolves it by document-level querySelector at
|
||||
import). The a11y frame is included: role=dialog + aria-modal, a
|
||||
labelled close control, a focusable content target (tabindex=-1),
|
||||
and a role=status announcer. Hidden by default — inert until JS
|
||||
opens it."""
|
||||
text = _read(INDEX_HTML)
|
||||
assert text.count('id="doc-modal"') == 1, (
|
||||
"the shell must carry exactly ONE modal skeleton (the fold dedup)"
|
||||
)
|
||||
assert '<div class="doc-modal" id="doc-modal" hidden>' in text
|
||||
assert 'id="doc-modal-backdrop"' in text
|
||||
assert 'id="doc-modal-panel"' in text
|
||||
assert 'role="dialog"' in text and 'aria-modal="true"' in text
|
||||
assert 'id="doc-modal-title"' in text
|
||||
assert 'id="doc-modal-meta"' in text
|
||||
assert 'id="doc-modal-desc"' in text
|
||||
assert 'id="doc-modal-open"' in text
|
||||
assert re.search(r'id="doc-modal-content"[^>]*tabindex="-1"', text)
|
||||
assert re.search(r'id="doc-modal-close"[^>]*aria-label="Close document"', text)
|
||||
|
||||
|
||||
def test_sources_page_loads_markdown_before_its_module() -> None:
|
||||
"""Phase 26: the modal renders md documents on the Sources page too —
|
||||
so sources.html loads the classic markdown.js (global renderMarkdown)
|
||||
via a relative <script src> BEFORE its module script, exactly like
|
||||
index.html does."""
|
||||
html = _read(SOURCES_HTML)
|
||||
def test_shell_loads_markdown_before_its_modules() -> None:
|
||||
"""Phase 26 + phase 76 (task 02): the modal renders md documents
|
||||
in the RAG view too — so the shell loads the classic markdown.js
|
||||
(global renderMarkdown) via a relative <script src> BEFORE its
|
||||
module scripts (app.js — the chat view — and the lazy view
|
||||
modules' modal imports), exactly as the old sources.html did."""
|
||||
html = _read(INDEX_HTML)
|
||||
assert re.search(r'<script src="assets/markdown\.js"></script>', html)
|
||||
assert html.index('src="assets/markdown.js"') < html.index('type="module"'), (
|
||||
"sources.html: markdown.js must load before the module script"
|
||||
"index.html: markdown.js must load before the module scripts"
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user