Skip to content

feat: fresh chat on return and reset on context change (E1.2, E1.3) - #685

Merged
neuromechanist merged 11 commits into
developfrom
feature/issue-656-fresh-chat-and-context-switch
Nov 11, 2026
Merged

neuromechanist merged 11 commits into
developfrom
feature/issue-656-fresh-chat-and-context-switch

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

Two commit groups on the E1 conversation store (#654 / #680).

E1.2, fresh chat on return (#655).
init() starts a new conversation after loadHistory when the active one has a user message and either the sessionStorage marker <storageKey>-session is absent (it is set after checking) or Date.now() - updatedAt exceeds 24 h (SESSION_TTL_HOURS, pinned to the backend constant by a drift test).
A sessionStorage that throws reads as "marker present"; a pop-out (window.__OSA_CHAT_CONFIG__) never resets; an empty chat is never replaced.
The new chat takes the page context and shows the A4 empty state ("Welcome back. This is a new chat about ." and a "Pick up where you left off: <title>" card that calls activateConversation on the chat just left); the card is hidden after the first send and not persisted.

E1.3, context switch (#656).
A new // --- Context switch --- block in the widget holds the pure helpers (contextForPage, sameContext, pageContext) and the notice, Continue and send-time logic.
The page context is the setDataset dataset, else a path under search_paths (segment boundary), else null (keep the current context).
It is re-evaluated at init, after setDataset, after the community config loads, and just before a send.
When it differs from a chat that has user messages, the A1 notice shows above the input and the old chat stays visible; the next send calls startConversation({context: new}) first; "Continue the chat instead" sets the context, shows the divider and the dashed "The assistant is told" box, and the next request carries moved_from, cleared only after the server answered, so a failed send keeps it.
page_context now also carries dataset_id (sent whether or not the reader shares the page URL, like widget_instructions; documented in docs/community-widget.md).
Backend: PageContext validates dataset_id ([A-Za-z0-9._-]{1,64}) and moved_from (dataset with an id, or search), the system prompt gets a "Reader Context" block, and widget.search_paths is a validated key (leading slash, no query or fragment, at most 5) resolved only when non-empty; NEMAR sets [/discover].

Decision stated as asked: a chat with no user message, or with no context yet, adopts the page's context silently, with no notice.

Closes #655
Closes #656

Tests and commands run

  • bun frontend/test-widget-fresh-return.js (new, wired into frontend-tests): 57 passed
  • bun frontend/test-widget-context-switch.js (new, wired into frontend-tests): 124 passed
  • for f in history-store popout dataset-questions tools settings run-card; do bun frontend/test-widget-$f.js || break; done: all pass (141, 61, 443, 184, 261, 212 per script); every other frontend/test-*.js also exits 0
  • node --check frontend/osa-chat-widget.js: ok
  • uv run pytest tests/test_frontend tests/test_api/test_page_context.py tests/test_agents/test_page_context.py tests/test_core/test_config/test_community.py tests/test_api/test_communities_endpoint.py -q: 706 passed, 1 skipped
  • uv run ruff check . && uv run ruff format --check .: clean

(pytest was run with -o addopts="" to skip the coverage flags; the selection is the one listed.)

Notes for review

  • Existing widget tests that seed a saved chat (test-widget-history-store.js, test-widget-dataset-questions.js) now start each window as a reload in the same browser session (sessionStorage marker set) and use recent fixture timestamps; otherwise the new return rules would, correctly, start a fresh chat.
  • test-widget-dataset-questions.js had one assertion that a new dataset's question lands in the same chat; it now asserts the E1.3 behavior (two conversations).

A new browser session or a chat idle past the server's 24 h session
starts a new conversation and offers the old one back. Never in a pop-out
or over an empty chat. Tested: new fresh-return widget test, existing
widget tests, drift test.
…(E1.3, #656)

Backend side of the context switch: PageContext validates dataset_id and
moved_from, the system prompt gets a reader-context block, and widget
search_paths is a validated community key (NEMAR: /discover). Tested:
API model, prompt, config and endpoint tests.
The widget compares the page's context (setDataset, or a search path)
with the chat's, shows the A1 notice, starts a new chat on the next send
or continues with moved_from, and sends dataset_id. Tested: new
context-switch widget test and the existing widget tests.
Adds the widget guide sections and CHANGELOG entries for E1.2 and E1.3
(the E1.2 entry is already in; this adds E1.3). Docs only.
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Preview Deployment

Name Link
Preview URL https://feature-issue-656-fresh-chat-demo.osc.earth
Branch feature/issue-656-fresh-chat-and-context-switch
Commit 7e58f7a

This preview will be updated automatically when you push new commits.

…tests for search_paths limits and page context off (tested: pytest, ruff)
…told only when carried and still current, saved wording, a11y, focus, popstate (tested: bun widget suites)
@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 1: outcome for each finding. Fixes are in 9a39a8d, e9bb23c and 02cf1d9.

Important

  • "stays in History" copy: fixed. The notice now says "the chat is saved"; the test assertions, docs and CHANGELOG match, and the CHANGELOG says the History panel comes later.
  • / in search_paths matches every page: fixed in the widget. / now matches the home page alone (contextForPage), with pure-function cases (including ['/', '/discover']). The Python side accepts / and a test pins it; the docs say so.
  • moved_from rendered against a destination the widget did not choose: fixed. The widget sends a move only while the page's context still equals where the reader moved to (carriedMove), so setDataset(null) on a page that is neither no longer produces "moved to the dataset search"; the move waits and is sent again when the page is back. Test added. As defence in depth the prompt also omits a moved_from equal to where the reader is now, with prompt tests.
  • Move marked told by any answered request: fixed. sendMessage records carriedMove() where the body is built and marks only that move told; postResume does the same after its response. Tests added for Continue clicked while a reply is pending and for a resumed reply.
  • role="separator" hides the divider text: fixed. The role is removed and the test asserts its absence.

Suggestions

  • Focus after the resume card: fixed; focus goes to the input, with an assertion in test-widget-fresh-return.js.
  • Notice live region: fixed. A persistent empty role="status" slot sits above the input in the widget template, and the notice is added into it; a test checks the slot is the same element before and after.
  • Second move merge untested / A to B to A: fixed. Tests for A to B to C (one move from A, one divider) and A to B to A (no move, no divider). A move back to where the chat started now clears the move.
  • MovedFrom accepts an id on search: fixed; it is refused, with tests.
  • validate_search_paths rules undocumented, 200-character branch untested: fixed; the docs table lists whitespace and length, and a boundary test covers 200 and 201.
  • Toggle label vs dataset_id: rejected. This is the intentional, documented design: dataset_id is about the chat (like widget_instructions), not the reader's browsing, and the CHANGELOG and docs already say it is sent when the page URL is not shared. Changing the label is a product copy decision for the maintainer, not a defect.
  • Garbled contextName comment: fixed.
  • Reader Context test with a stub model / enable_page_context: false: kept the stub model, as the file already does. Added a test that turns enable_page_context off on a real NEMAR assistant's config and asserts that the dataset id and the move are absent from the prompt.
  • Context change found only at send on a single-page site: partly fixed. The widget now also evaluates on popstate (back and forward buttons), so the notice appears before the send, with a test. A pushState navigation that never calls setDataset reports nothing in the browser; the docs now say Continue is offered only when the widget learns of the move, and that a single-page host calls setDataset on each navigation.

Tests: all frontend/test-widget-*.js suites pass (context-switch 160, fresh-return 58); uv run ruff check . and ruff format --check . clean; pytest tests -m "not network and not llm": 5348 passed, 1 failed (tests/test_tools/test_hed_suggest.py::test_real_program_suggests_a_schema_tag, a HED tool test in code this PR does not touch; it needs the real suggestion program in this environment).

@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 2: outcomes

  • Unsent move lost on reload (suggestion): fixed (docs). Keeping the move with the stored conversation would change the store format for a narrow window, so docs/community-widget.md now states that the move is not stored and a reload before the first message after "Continue" drops it (the chat keeps the new context; the assistant is not told about the move). Commit c1a599d.
  • Empty bold span in the resume card (suggestion): fixed. The card falls back to "your last chat" when the previous title is empty; a test with a whitespace-only first message covers it. Commit 48537cf.
  • Pop-out requests carry dataset_id with no moved_from (suggestion): rejected. This is the documented "the pop-out never asks" design and matches the acceptance criteria; it is residual risk, not a defect, and the code is the code this PR touched, so no issue is filed.

Tested: bun widget suites (context-switch 160, fresh-return 59, history-store 212, popout 141, and the rest of frontend/test-widget-*.js), tests/test_frontend/test_embed_snippet_hygiene.py (17 passed), ruff check and ruff format --check clean.

@neuromechanist

Copy link
Copy Markdown
Member Author

Project manager verdict: ready to merge. The branch conflicted with develop in CHANGELOG.md; I merged develop in (kept all three entries, c9d1fad) and pushed normally. All acceptance criteria are covered by the diff and tests, every review finding has an outcome in the round 1 and round 2 comments, and all checks pass on c9d1fad (Lint, Test 3.11 and 3.12, Frontend Tests, Docker build, notebook site; skipping jobs are expected). The squash merge itself was not run by this agent (blocked by the permission classifier), so a maintainer needs to run it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant