Skip to content

feat: store up to 30 conversations (history format v3) - #680

Merged
neuromechanist merged 8 commits into
developfrom
feature/issue-654-conversation-store-v3
Nov 11, 2026
Merged

neuromechanist merged 8 commits into
developfrom
feature/issue-654-conversation-store-v3

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

Replaces the widget's single stored conversation with a v3 store of up to 30 conversations (E1.1). No visible change: the widget still shows the active conversation, and messages and sessionId stay its working copy, so no other reader changes. The History panel (E1.4), fresh-on-return (E1.2) and context detection (E1.3) build on this storage.

  • Record: {version: 3, activeId, conversations: [{id, context, title, sessionId, createdAt, updatedAt, messages}]}. context is null, {type: 'dataset', id} or {type: 'search'}. The last 30 by updatedAt are kept; the active one is never evicted.
  • One contiguous // --- Conversation store --- block beside CHAT_HISTORY_VERSION: pure functions (parsed storage in, envelope out). loadHistory and saveHistory are thin callers; the per-message mapping in saveHistory is untouched, only wrapped.
  • Migration: a v2 record, a v1 record and the bare array each become one conversation (messages and session kept, context from the latest user message that names a dataset, timestamps from now). The below-version-2 citation repair is kept.
  • Safety: saves re-read the stored record and replace only the active conversation, so a second tab or the pop-out keeps the others; a stored version above 3 is never read as current nor overwritten (notice, and a save reports it did not happen); on QuotaExceededError the oldest non-active conversation is evicted and the write retried before the existing storage-full error. A read or parse failure still queues the "corrupted" notice and never writes.
  • API (test hooks only, no UI): startConversation({context}), listConversations(), activateConversation(id), deleteConversation(id). resetChat keeps its meaning: it discards the active conversation and starts an empty one, leaving the others.
  • New frontend/test-widget-history-store.js (163 checks, real widget in happy-dom), wired into the frontend-tests job after the pop-out step. Existing tests that read the stored record now read through the active conversation; two fixtures that used version: 99 as "current" use version: 2.
  • docs/community-widget.md gets a "Saved conversations" section.

Closes #654

Commands run

  • bun install: ok
  • bun frontend/test-widget-history-store.js: 163 passed, 0 failed
  • for f in dataset-questions popout tools settings; do bun frontend/test-widget-$f.js || break; done: all pass (also paced-streaming, which seeded version: 99)
  • node --check frontend/osa-chat-widget.js: ok
  • bun scripts/build-runtime-bundle.js --check: runtime bundle is current
  • uv run pytest tests/test_frontend -q: 113 passed
  • uv run ruff check . and uv run ruff format --check .: clean

Notes for review

  • Decisions beyond the brief: titles collapse whitespace before cutting to 60 characters (code points); the context of a migrated record is the latest user message that names a valid dataset, not strictly the last user message; startConversation and activateConversation drop the conversation they leave when it is only the greeting; resetChat and a deleted active conversation start with context: null; updatedAt is kept when a save changes nothing, so a repair or vote does not make a chat look newer.
  • The automated pr-review-toolkit panel could not be spawned from this session; the review rounds are still to run before merge.

Replace the single stored chat with a v3 record of up to 30 conversations
(#654). Pure store functions beside CHAT_HISTORY_VERSION; loadHistory and
saveHistory are thin callers. Saves re-read the record and replace only the
active conversation; v1, v2 and bare-array records migrate to one
conversation; a version above 3 is never overwritten; a full storage
evicts the oldest inactive conversation and retries. Adds startConversation,
listConversations, activateConversation and deleteConversation hooks, no UI.
Existing widget tests read the active conversation; they pass.
Migration from v2, v1 and bare arrays, the 30-conversation limit, round trip,
unreadable records, a full storage, and two widgets on one storage. 163 checks
pass.
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Preview Deployment

Name Link
Preview URL https://feature-issue-654-conversati-demo.osc.earth
Branch feature/issue-654-conversation-store-v3
Commit a40baa7

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

…t, unreadable records kept aside, votes do not bump updatedAt, feedback flushed on swap, quota notice); tested with test-widget-history-store.js
@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 1: outcome for each finding. Fixes are in the two commits pushed to this branch; bun frontend/test-widget-history-store.js passes (207 checks) and every other frontend/test-widget-*.js suite exits 0.

  1. Empty chat evicts the oldest saved one (important): fixed. trimConversations now counts only conversations with a user message toward the 30; the active one is kept either way and an empty non-active one is dropped. New test: 30 stored, startConversation(), activateConversation back, all 30 remain (also after resetChat). The 31st-conversation test now asserts conv-1 survives startConversation() and is evicted only once the new chat has a first user message.
  2. Unreadable record overwritten by the first save (important): fixed. saveHistory now copies the raw string of an unparsable record, or a version 3 record without a conversations list, to <storageKey>-unreadable before replacing it, and refuses to write if the copy cannot be kept. The load notice says this, and the docs bullet is rewritten to match. A version 3 object with a non-array conversations is now reported as corrupted instead of read as empty. Tests: corrupt JSON then save (copy is byte-identical), copy fails (record untouched), damaged version 3.
  3. A vote moves updatedAt (suggestion): fixed. The unchanged check ignores feedback and feedbackCommitted; the comment and docs now say updatedAt is the last time messages or session changed (a run edited in place counts). Test: a committed thumbs-up keeps updatedAt and is still stored.
  4. Swap without flushing an open down-vote (important): fixed. retireActiveConversation and the delete-active path call flushPendingResponseFeedback first. Test covers startConversation, activateConversation and deleting the active one (the feedback request with its comment is sent). saveHistoryQuietly now returns whether the save succeeded; the four hooks keep their return values, since a failed save already shows its reason on screen and E1.3/E1.4 can read the store.
  5. Silent eviction on quota retry (suggestion): fixed. A successful retry after evicting shows "Browser storage was full, so the oldest saved conversation was removed to keep this one." (plural form for more than one); the quota test asserts it.
  6. v2 widget overwrites a v3 record (important): fixed as a conscious decision, no key change. Keeping storageKey is deliberate: embedders configure it, and the pop-out and a second tab must share the record. The hazard is now documented in docs/community-widget.md and the CHANGELOG release note (embeds pinned by integrity hash should be updated together). Not guardable from a v3 widget: an older widget cannot be taught to refuse.
  7. Test gaps (important): fixed. Round trip now compares against an explicit expected literal (dataset, citations, run code and stdout, images emptied); added tests for dropped ids surviving a failed save and applied by a later one, the isLoading guards on all three hooks, an unreadable record followed by a save, and startConversation on a full store.
  8. Every save re-parses all conversations (suggestion): rejected. Measured with 30 conversations of 40 messages each, 8.5 MB of stored text (already above the usual 5 MB browser quota): 2.3 ms per save under Bun. Bounded by the 30-chat cap and the storage quota; passing other entries through un-normalised would give up the repair-on-write that the merge relies on.
  9. Type clarity (suggestion): fixed. JSDoc typedefs for Conversation, Envelope and ReadResult (with the reason for unreadable) and a note on the working copy; loadHistory now resets messages, session and active conversation up front, so an empty or unrecognized record no longer leaves stale state. Test: second loadHistory call on an unrecognized record and on an empty version 3 record.

Also: trimConversations now sorts the active conversation first so it wins a timestamp tie, and a CHANGELOG entry was added under Unreleased.

…ll, one unreadable rule, storage notices queue when the panel is closed, JSDoc typedefs; tested with test-widget-history-store.js
@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 2 outcomes (commits bbf87de, ed2e27f; frontend/test-widget-history-store.js 212 passed, all frontend/test-widget-*.js exit 0):

  1. Copy-aside traps saving on a full storage (suggestion): fixed. The copy is best effort when it fails with a storage-full error: the record is replaced anyway and a notice says no copy was kept. Any other failure still refuses to overwrite. The docs and changelog now say the copy is never cleaned up and a second corruption replaces the first copy. Not done: deferring the replacement of the old -unreadable copy until after a successful write, because it needs the same free quota the copy does. New tests cover the full-storage copy and the non-quota refusal.
  2. Duplicate "damaged v3" rule (suggestion): fixed. readStoredHistoryRecord now returns read, the result of readConversationStore (a string that is not JSON reads as unreadable, reason damaged), and both loadHistory and saveHistory use it, so parsing and classification live in one place.
  3. Typedefs as line comments, misplaced notes, "thin callers", spacing (suggestion): fixed. Typedefs are /** @typedef */ blocks, the working-copy note sits beside activeConversation, the header says the format logic is pure and the callers do the IO, a blank line precedes let responseSequence, and the extra blank line before the changelog bullet is gone.
  4. Eviction notice auto-hides or lands unseen (suggestion): fixed. A new showStorageNotice shows a persistent notice when the panel is open and queues it for the next open when it is closed. The copy-failure notice uses it too. Tests cover both cases.

@neuromechanist

Copy link
Copy Markdown
Member Author

PM verdict: criteria met, CI green (Lint, Test 3.11/3.12, Frontend Tests, deploy) on ed2e27f. Checked: test-widget-history-store.js is present and wired after the pop-out step; migration (v2, v1, bare array), eviction, round trip, failure/quota, two-instance and resetChat cases are covered; all frontend/test-widget-*.js suites exit 0 (also after a local rebase onto origin/develop, history-store 212 passed). Both review rounds have an outcome for every finding. Not merged. One open item: the branch is behind develop (docs and version bumps only, merges cleanly) and I could not push the rebase from this session, so it needs a branch update or a regular merge by a maintainer.

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