Skip to content

feat: reseed a resumed chat after its server session expired (E1.6) - #696

Merged
neuromechanist merged 16 commits into
developfrom
feature/issue-659-reseed-expired-session
Nov 11, 2026
Merged

neuromechanist merged 16 commits into
developfrom
feature/issue-659-reseed-expired-session

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Summary

A conversation that resumes after its server session is gone (24 hour idle lifetime, a restart, or LRU eviction) used to be answered with no context: the widget kept the old session id and the server made a fresh, empty session under it. Now the widget sends a compact transcript on the requests where the server might not have the session, and the server uses it only when the request just created the session.

Server (src/api/routers/community.py, src/agents/model_routing.py):

  • POST /{community}/chat accepts optional prior_turns (at most 24 {role: user|assistant, text} turns, 4,000 characters each, extra fields forbidden, blank text refused, all 422) and mode (quick|analysis).
  • A session with no messages is seeded with ONE HumanMessage built by the new reseeded_message(transcript, reader_message): the transcript (reusing history_transcript, kept to whole turns within SUMMARY_INPUT_MAX_CHARS, the oldest dropped behind an [earlier turns omitted] marker) as fenced background, then the reader's message, so there are never two consecutive user messages. Its own fence is stripped from the text (repeatedly, so a fence rebuilt by removing another is removed too). A session that already has messages ignores both fields.
  • On a request where routing_applies, mode sets session.mode before the turn, so handing_over is false and the turn runs on the stored tier with no handover events and no summarizer call. Elsewhere mode is ignored. The keyword check (wants_analysis) reads the reader's own words, not the transcript, so a keyword in an old turn does not flip a quick chat to analysis.
  • /chat/resume is unchanged.

Widget (frontend/osa-chat-widget.js):

  • The pure part is in a // BEGIN reseed block: newest 12 turns, user and assistant text only, each cut from the middle at 2,000 characters with an ellipsis (never splitting a surrogate pair), the greeting is not a turn, mode from the newest reply that stored one.
  • serverConfirmedAt is set on a session or done event (and a non-streaming reply) and is not persisted. It is reset on page load, on opening a conversation from History or the resume card, on a new, deleted or cleared chat, and, by the rule in reseedFields, when more than SESSION_TTL_MS has passed since the last server answer. Nothing is sent for a new empty chat, a chat with no assistant reply, or a session the server confirmed in this page load.

conversation_id: #694 has since merged. A reseeded session is created under the session id the widget stored, so its usage rows keep that id as their conversation id; after a handover the stored id is the analysis session's, so a reseeded analysis chat starts a new conversation in the cost report, as the metrics docs from #694 say.

Refs #659 (it closes after a real-model check of reseeding on develop-demo, since test_reseed_llm.py has not been run with a key)

Tests

  • tests/test_api/test_reseed.py (new, 47 tests): real app, real create_community_assistant, real graph, real session store. The stand-in model answers from what its input contains (the dataset id, or "no context"), not from a script. Covers: a first request with prior_turns about on002718 and "which dataset were we discussing" answers with the id; the same request without them does not; exactly one user message reaches the model; stored history alternates; a live session ignores prior_turns and mode; 422 for 25 turns, a long turn, bad roles, blank text, extra fields, wrong types and an unknown mode; the limits themselves are accepted; the seed is trimmed to the newest text; a fence in a turn cannot close its section; a stored analysis mode keeps the analysis tier with no handover events and no quick-model call; mode is ignored when not routed (no execute_code, a chosen model, a community that does not route); the non-streaming path seeds too.
  • tests/test_frontend/test_reseed_limits.py (new): keeps the widget's reseed bounds equal to the server's limits.
  • tests/test_integration/test_reseed_llm.py (new, llm and integration): real NEMAR community and real models (quick, analysis, and the no-turns control, using xx099904). SKIPPED here: this environment has no ANTHROPIC_API_KEY, so it has not been run.
  • frontend/test-widget-reseed.js (new, 63 checks, wired into frontend-tests in .github/workflows/tests.yml): asserts the captured request bodies for a restored conversation, a reload, a new empty chat, a conversation with no assistant reply, a conversation opened from History, the idle rule at one hour either side of SESSION_TTL_HOURS, a request the server never answered, the 12 turn and 2,000 character bounds, and text-only; plus the extracted pure block.

Commands run

  • gh pr view 694 --json state,mergedAt: OPEN, not merged
  • uv run pytest tests/test_api/test_reseed.py tests/test_api/test_model_routing.py tests/test_api/test_session_lifecycle.py tests/test_agents/test_model_routing.py tests/test_frontend -q: tests/test_api 1664 passed
  • uv run pytest tests/test_integration/test_reseed_llm.py -m llm -q: 3 skipped (no ANTHROPIC_API_KEY in this environment)
  • bun frontend/test-widget-reseed.js: 63 passed, 0 failed
  • bun frontend/test-widget-fresh-return.js (59 passed), test-widget-history-store.js (212 passed), test-widget-handover-events.js (14 passed)
  • every other frontend/test-widget-*.js suite: exit 0
  • uv run ruff check . and uv run ruff format --check .: clean

…E1.6)

POST /{community}/chat takes optional prior_turns (24 turns, 4,000 chars each) and mode.
A session the request just created is started from one fenced message built by
reseeded_message; a live session ignores both. Tested against the real app and graph.
Skipped here, no ANTHROPIC_API_KEY; not run.
… session (E1.6)

Newest 12 turns, text only, each cut from the middle at 2,000 chars; sent on the first
request of a page load, after opening a chat from History or the resume card, and after
a day without a server answer. New test-widget-reseed.js (54 checks) wired into CI; the
other widget suites still pass.
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Preview Deployment

Name Link
Preview URL https://feature-issue-659-reseed-exp-demo.osc.earth
Branch feature/issue-659-reseed-expired-session
Commit 7d96db0

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

@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 1: outcomes

Fix commits are on this branch (dadf332, 1dedc9f, 8c58489, cee3461, a1b8548).

Tests run

  • uv run ruff check . and uv run ruff format --check .: clean
  • uv run pytest tests --ignore=tests/test_core/test_llm_service.py -m "not llm": 5713 passed, 1 failed (tests/test_tools/test_hed_suggest.py::test_real_program_suggests_a_schema_tag, an external program in this environment, untouched by this PR); test_llm_service.py does not collect on Python 3.14 here (pydantic v1), untouched by this PR
  • bun frontend/test-widget-reseed.js: 60 passed; every other frontend/test-widget-*.js exits 0
  • uv run pytest tests/test_integration/test_reseed_llm.py -m llm: not run, no ANTHROPIC_API_KEY

@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 2: outcomes

  • Important, _FENCE backtracks quadratically (src/agents/model_routing.py): fixed. The two whitespace runs are now one class, <[\s\\/]*(?:summary|earlier_conversation|researcher_message)\b[^>]*>. Added test_a_long_run_of_spaces_after_an_angle_bracket_is_removed_in_linear_time (24 turns of < plus 3,999 spaces, plus the whole 24-turn transcript, under 0.5 s); the existing variant tests still pass.
  • Important, the -m llm run (tests/test_integration/test_reseed_llm.py): not fixable from here. There is no ANTHROPIC_API_KEY in this environment, so the test is unchanged and the PR checklist box stays unticked. Before merge, someone with a key runs uv run pytest tests/test_integration/test_reseed_llm.py -m llm and pastes the result here; if the quick or analysis case fails on a tool_request, strengthen _ASK or the transcript wording.
  • Suggestion, "about 24 KB" (docs/community-widget.md): fixed. It now says about 12,000 characters of text, before JSON escaping.
  • Suggestion, redundant reseeding (src/api/routers/community.py): fixed. The variable is gone; the branch is if turns and not session.messages:.
  • Suggestion, error bubble sent as context (frontend/osa-chat-widget.js): fixed. A bubble that is only an error (the error event with no text, or a stream failure with no text) gets failed: true; selectReseedTurns and lastReplyMode skip it, so a chat whose only assistant message is one sends nothing. Partial text with an error suffix still counts as a reply. Three checks added to test-widget-reseed.js (63 pass).
  • Suggestion, PR body: updated to the whole-turn trimming, 47 server tests, 63 widget checks, test_reseed_limits.py and the new workflow. Reseed a resumed chat after the server session expired #659 is to be closed by hand after merge, since the workflow lands with this PR.

Tested: uv run pytest tests/test_api (1664 passed), tests/test_agents/test_model_routing.py, bun frontend/test-widget-reseed.js (63), test-widget-fresh-return.js (59), test-widget-history-store.js (212), test-streaming.js; uv run ruff check . and ruff format --check src tests clean.

@neuromechanist

Copy link
Copy Markdown
Member Author

Merge verdict: not merged yet

Everything is in place except one criterion.

  • prior_turns and mode on POST /{community}/chat, seeding only a new session, bounded (422 or whole-turn trimming)
  • tests/test_api/test_reseed.py: real app, graph and session store; the id comes back; a live session ignores the fields; oversize and malformed lists refused; analysis mode keeps its tier with no handover
  • frontend/test-widget-reseed.js wired into frontend-tests; send and no-send cases covered; mode round trip covered
  • Review rounds 1 and 2: every finding has an outcome in a PR comment
  • tests/test_integration/test_reseed_llm.py exists and is marked llm and integration, but it has never been run (no ANTHROPIC_API_KEY in the author's environment or in mine), so "a real model answers with the prior context" is not yet shown. The round 1 and round 2 outcomes both name this as owed before merge.
  • CI at the time of this check: Lint pass, Frontend Tests, Test (3.11) and Test (3.12) still pending.

To unblock: someone with a key runs uv run pytest tests/test_integration/test_reseed_llm.py -m llm and pastes the result here. If it passes and CI is green, this is ready to merge (squash). After merge, close #659 by hand, since the new close workflow lands with this PR and does not act on it.

@neuromechanist
neuromechanist merged commit 25858c9 into develop Nov 11, 2026
10 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-659-reseed-expired-session branch November 11, 2026 08:17
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