Skip to content

feat(chat-history): server-side chat history with tombstones (follow-up to #9902) - #12663

Merged
mudler merged 18 commits into
mudler:masterfrom
walcz-de:feat/chat-history-server
Oct 19, 2026
Merged

mudler merged 18 commits into
mudler:masterfrom
walcz-de:feat/chat-history-server

Conversation

@walcz-de

Copy link
Copy Markdown
Contributor

Follow-up to #9902 by @TLoE419, continuing on top of their branch with the maintainer's agreement. The eight commits from #9902 are kept unchanged in authorship and rebased onto current master; four commits on top address the open review points.

Closes #9432.

What

Server-side persistence of the web UI chat conversations, per user, in the auth database (SQLite by default, PostgreSQL when configured). The Chat page syncs to /api/conversations and keeps localStorage as a cache.

Why the extra commits

The review on #9902 found that the server could not be authoritative across devices: a conversation deleted on device A came back when a stale device B reconnected, a message sent while the first list request was in flight could be lost, and Save after a soft delete resurrected the row. This PR fixes those with a small, explicit design and adds the missing docs, swagger and tests.

Design: tombstones and versions

Server (authoritative over deletions)

  • DELETE turns the row into a tombstone: content dropped, deleted_at set, row kept. It is idempotent, and deleting an id the server has never seen still records a tombstone. That matters because the UI deletes immediately while a debounced PUT of the same chat may still be in flight; whichever request lands second, the delete wins.
  • Any later write of a tombstoned id returns 410 Gone and writes nothing. GET /api/conversations/{id} returns 410 for a deleted conversation, 404 for one that never existed.
  • GET /api/conversations returns {"conversations": [...], "deleted": [{"id", "version", "deletedAt"}]}.
  • Every row has a server-assigned version: 1 on the first save, +1 on every write including the delete. It is returned on every save and in the list, and ignored on input.
  • Save runs in a transaction. The version bump is a single UPDATE ... SET version = version + 1 with deleted_at IS NULL in the WHERE clause, so concurrent writers each count and a delete that commits between the lookup and the write still wins. Two concurrent first writes of the same id use ON CONFLICT DO NOTHING and retry once into the update path. This also removes the read-modify-write race the review flagged in Save.
  • DeleteAll and PUT /bulk leave tombstones too. ReplaceAll no longer hard-deletes first: it upserts the payload, skips ids that were deleted (returned as skipped), and tombstones the user's other live conversations, so a replacement also reaches other devices as a deletion.
  • createdAt and updatedAt are stored as the client sends them; only zero values are stamped. The UI compares updatedAt across copies, so the server's clock must not replace the client's.

Client (useChat.js)

mergeRemoteAndLocal is replaced by a reconcile step that can tell a never-synced draft from a server-side deletion:

Local Server Result
any listed in deleted dropped locally
present present the copy with the newer updatedAt wins (local keeps the server's version and is uploaded)
present, no version absent never-synced draft: kept and uploaded
present, has version absent the server has seen it and no longer has it: dropped, not resurrected (this also keeps one account's cached chats out of another account on a shared browser)
absent present added
  • The version is kept in the localStorage cache, which is how the next load knows a chat was synced.
  • Saves record the returned version. A 410 on save drops the chat locally instead of retrying it forever.
  • Deletes made while the first list request is in flight are queued and sent when it arrives.
  • Chats received from the server count as already sent, so adopting a server copy is not echoed straight back.
  • The client no longer calls PUT /bulk for the localStorage migration. It replaced the whole server set from a possibly stale browser; the per-chat saves already upload never-synced drafts, and each goes through the tombstone check. The endpoint stays for API clients.

Not in scope: concurrent edits of the same live conversation on two devices are last-writer-wins at conversation granularity, as in #9902. The version is exposed, so conditional writes (If-Match-style, 409 on a stale base version) could be added later without a schema change. I left them out because they need a client-side conflict UI.

Review points and how each was addressed

  1. Cross-device resurrection. Tombstones plus versions, as described above. Server: writes of a deleted id return 410; the list reports tombstones. Client: reconcile drops deleted and synced-but-missing chats and only uploads never-synced drafts. Regressions: API spec "a stale client reconnecting after a remote deletion" (4 cases, core/http/routes/chat_history_auth_test.go) and UI specs "a stale client drops a chat deleted on another device instead of uploading it again" and "a synced chat the server no longer has is dropped, not resurrected" (core/http/react-ui/e2e/chat-history-sync.spec.js).
  2. First-load overwrite of in-flight edits. The reconcile compares updatedAt instead of letting the server always win, and the server now stores the client's updatedAt. UI spec "a message sent while the list is loading survives the older server copy" holds the list request open, sends a message, then releases the list. The opposite direction is covered by "a newer copy from the server replaces the older local one".
  3. Save after delete. Save refuses a tombstoned id with ErrDeleted → 410 Gone, inside a transaction. That is neither a 500 nor a silent resurrection. Store specs "rejects a save after delete instead of resurrecting the row", "records a delete that arrives before the conversation's first save", and the ReplaceAll/DeleteAll variants.
  4. Docs and swagger. New page docs/content/features/chat-history.md (permission, tombstone semantics, sync behaviour, API table, example), a pointer from features/chat.md, and a chat-history row in the instructions table of features/api-discovery.md. The /api/instructions/chat-history intro now describes the tombstone contract, and api_instructions_test.go lists chat-history. Swagger was regenerated with swag init -g core/http/app.go --output swagger (swag v1.16.6, the version in go.mod); the diff is limited to the chat-history paths and schema.Conversation* definitions. json.RawMessage fields carry swaggertype:"array,object" because swag cannot resolve the type otherwise.
  5. Tests. HTTP handler specs for all seven endpoints, a useChat Playwright spec, and the store suite runs on both PostgreSQL and SQLite (see below). To make the handlers testable they now take the *chathistory.Store, and the route table moved into routes.RegisterChatHistoryRoutes, which RegisterLocalAIRoutes calls. The specs drive that function with the real auth.Middleware and the chat_history feature gate.
  6. Field drift. serializeChat sends localaiAssistant, and schema.Conversation has the field. Since May, master's createNewChat added only localaiAssistant (mcpResources was already covered). A handler spec round-trips every per-chat setting the UI stores, and a UI spec checks the toggle on upload and in the local cache.
  7. Default no-op when auth is disabled. Not redesigned; see the open question below. The docs now state it explicitly: without LOCALAI_AUTH=true (or with legacy API keys only, or with the web UI disabled) the endpoints are not registered, and the UI keeps using localStorage.

Also from the earlier review: API surface checklist items are unchanged from #9902 (swagger tags, instructionDefs, RouteFeatureRegistry, FeatureChatHistory in APIFeatures + APIFeatureMetas()). No MCP admin tool: these are per-user endpoints, not RequireAdmin, so .agents/localai-assistant-mcp.md does not apply.

Behaviour changes compared to #9902

  • DELETE /api/conversations/{id} is idempotent: 200 for an unknown or already deleted id (it was 404). Reason: the in-flight save race above.
  • GET of a deleted conversation is 410 (it was 404).
  • PUT /bulk tombstones instead of hard-deleting, skips deleted ids, and returns skipped. count is the number stored.
  • Server keeps the client's createdAt/updatedAt (previously updatedAt was overwritten with server time).
  • Deleted conversations keep no content. The soft-delete in feat: persist chat conversations server-side (#9432) #9902 kept the content; I dropped it because the user asked for a delete and nothing prunes or audits it today.

Rebase notes

The branch was 1129 commits behind. Conflicts and their resolutions:

  • core/http/auth/features.go, permissions.go: master added FeatureDecisions in the same spots. I kept both.
  • api_instructions_test.go: master's count was 21, so with chat-history it is now 22.
  • core/application/application.go: master now uses cmp and path/filepath (router corpus), which TLoE419's GORM refactor commit removed as unused. The imports are restored inside that commit, so every commit still builds.

Tests run

Go, with the tags the coverage gate uses (debug auth):

go run github.com/onsi/ginkgo/v2/ginkgo -tags "debug auth" \
  ./core/services/chathistory/ ./core/http/routes/ ./core/http/endpoints/localai/ ./core/http/auth/ ./core/schema/
Suite Result
core/services/chathistory (PostgreSQL via testcontainers + SQLite via auth.InitDB) 44/44 passed
core/http/routes (incl. 22 chat history endpoint specs) 211/211 passed
core/http/endpoints/localai 388/388 passed
core/http/auth 213/213 passed
core/schema 83/83 passed

golangci-lint run --new-from-merge-base=upstream/master (v2.11.4, the CI version) on the touched packages, with and without --build-tags auth: 0 issues.

UI (Playwright against tests/e2e-ui/ui-test-server with the rebuilt bundle):

cd core/http/react-ui && npx playwright test e2e/chat-history-sync.spec.js   # 7 passed
npx playwright test e2e/chat e2e/forking-chat.spec.js e2e/home e2e/talk     # 212 passed (incl. the new spec)

Each fix was also checked to fail without it:

  • Store/API: with saveInTx put back to feat: persist chat conversations server-side (#9432) #9902's write path (no tombstone check, plain gorm.Save upsert), 14 store specs fail (7 per dialect: the tombstone and version specs), and 4 API specs fail (the whole "stale client reconnecting after a remote deletion" context).
  • UI: with feat: persist chat conversations server-side (#9432) #9902's useChat.js, all 6 sync specs that existed at that point fail. For "a newer copy from the server replaces the older local one" the red comes from the echo assertion, because the old code re-uploads the adopted server copy.
  • Field drift: before the field was added, the handler round-trip spec failed with field "localaiAssistant" was dropped and the UI spec with Received: undefined.

The UI spec stubs /api/conversations with a small fake that follows the server contract (tombstones in the list, 410 for deleted ids, versions). The UI test server runs with auth disabled, where the real endpoints are not registered. The server side of the contract is covered by the Go specs above.

Open question for the maintainer

Chat history is a no-op when auth is disabled, which is LocalAI's default. The store needs Application.authDB, and that database only exists with LOCALAI_AUTH=true. #9902 made this trade-off deliberately (consistent with AgentStore/JobStore). This PR does not change it; it only documents it. Options if you want it on by default:

  • (a) keep it auth-only (current);
  • (b) open the same database without enabling auth and store everything under the anonymous user "". The store already partitions on it, but every browser on that server would then share one history;
  • (c) something else you prefer.

Which way should it go?

A smaller follow-up I'd also suggest: tombstones are tiny (id, version, timestamp) but are never pruned. If you want a bound, a retention sweep on deleted_at is a one-line query. The trade-off: a client offline for longer than the retention period could then re-upload a chat it had synced before. Even then the client drops it rather than resurrecting it, because it carries a version the server no longer has.

Checklist

  • Docs updated in the same change (docs/content/features/chat-history.md, chat.md, api-discovery.md)
  • Swagger regenerated
  • Tests: Go (store on PostgreSQL + SQLite, HTTP handlers with auth), Playwright (useChat sync)
  • AI assistance disclosed via Assisted-by: trailers

🤖 Generated with Claude Code

TLoE419 and others added 12 commits October 13, 2026 10:11
Persist WebUI chat conversations server-side so browser refresh, private
windows, or device changes preserve user history (issue mudler#9432). This
commit adds the on-disk representation:

- Conversation holds id, name, model, opaque history, MCP / sampling
  settings, and timestamps.
- ConversationsFile is the per-user list serialised to JSON.

History is stored as json.RawMessage so the server stays agnostic to
React message shapes (user / assistant / thinking / tool_call /
tool_result mixed with text / image_url / audio_url / file attachments).

Assisted-by: Claude:claude-opus-4-7

Signed-off-by: TLoE419 <tloemizuchizu@gmail.com>
…#9432)

File-backed persister for chat history. Each user's conversations live
under {baseDir}/{userID}/conversations.json (anonymous/ when auth is
disabled).

Design choices:
- In-memory cache backed by sync.Mutex so concurrent saves don't
  interleave writes.
- Atomic write via tmp file + os.Rename so a crash mid-save never
  leaves a corrupted history.
- ID validation regex blocks path-traversal payloads at the store
  boundary, in addition to the auth context constraint.

Tests cover round-trip persistence across instances, user isolation,
unsafe-ID rejection, bulk replace migration, and the anonymous fallback
path.

Assisted-by: Claude:claude-opus-4-7

Signed-off-by: TLoE419 <tloemizuchizu@gmail.com>
Wire the chathistory store into the HTTP layer. New endpoints under
/api/conversations:

- GET    /api/conversations        list
- POST   /api/conversations        upsert (id in body)
- DELETE /api/conversations        delete all
- PUT    /api/conversations/bulk   replace entire set (localStorage migration)
- GET    /api/conversations/:id    fetch one
- PUT    /api/conversations/:id    upsert (id in path; path id wins over body)
- DELETE /api/conversations/:id    delete one

All endpoints scope queries to the authenticated user via getUserID(c)
- callers cannot impersonate other users by passing a user_id in the
body. The endpoints are gated behind the new chat_history feature
permission (default ON in APIFeatures), registered through the existing
RouteFeatureRegistry so the unified feature middleware picks them up
automatically.

Application.ChatHistoryStore() returns nil when DisableWebUI is set or
no persistence path is configured, in which case route registration is
skipped entirely rather than registering handlers that always 503.

Also adds the chat-history instruction entry and Swagger tag so the
endpoint surfaces in /api/instructions and /swagger.

Assisted-by: Claude:claude-opus-4-7

Signed-off-by: TLoE419 <tloemizuchizu@gmail.com>
mudler#9432)

useChat now probes /api/conversations on mount. When the endpoint
responds 200, the server becomes the authoritative source: the hook
merges remote conversations into local state and pushes per-chat PUT
updates on each debounced save. When the endpoint 404s - older LocalAI
deploys or the feature disabled - the hook silently keeps the old
localStorage-only behaviour, so the change is backward-compatible.

Migration: when the server is reachable but empty and the browser has
local conversations with history, the hook fires a single
PUT /api/conversations/bulk to seed the server. The atomic bulk endpoint
avoids partial-upload states where a retry would skip already-uploaded
entries.

Delete operations propagate to the server when it's available; failures
are silently swallowed so the local UI stays responsive on transient
network errors.

Assisted-by: Claude:claude-opus-4-7

Signed-off-by: TLoE419 <tloemizuchizu@gmail.com>
The initial test file used the stdlib testing package (t.Fatalf /
t.Errorf), which is forbidden by the forbidigo linter rule in
.golangci.yml — LocalAI mandates Ginkgo/Gomega for all Go tests (see
.agents/coding-style.md).

Restructured the same six scenarios into Describe / Context / It blocks,
with a chathistory_suite_test.go bootstrap that registers the Ginkgo
fail handler. Test coverage is unchanged: basic CRUD round-trip, cross-
instance persistence, user isolation, unsafe ID rejection (now via
DescribeTable), bulk replace overwrite, and the anonymous fallback
path.

Assisted-by: Claude:claude-opus-4-7

Signed-off-by: TLoE419 <tloemizuchizu@gmail.com>
Addresses mudler's review on mudler#9902. Chat history was the only per-user
state in LocalAI sitting in a separate filesystem store instead of the
shared GORM database — AgentStore and JobStore both live there. Move
it onto Application.authDB so per-user state stays in one place.

* ConversationRecord (GORM model) with composite primary key
  (user_id, conv_id) — React mints IDs locally with no global
  coordination so per-user uniqueness is the natural shape.
* Store.New(db *gorm.DB) replaces Store.New(baseDir string).
  Save / Get / List / Delete / DeleteAll / ReplaceAll keep their
  signatures so HTTP handlers and the React UI are untouched.
* ReplaceAll runs in a transaction with an Unscoped() pre-clear so a
  future retention sweep cannot resurrect a stale ID on re-upload.
* gorm.DeletedAt in the schema means a future retention pruner is a
  one-line query — addresses the second half of the review (unbounded
  in-memory map). The map is gone: reads go straight to the DB.

Trade-off worth flagging in the PR reply: chat history persistence
now requires auth.Enabled = true (that's what initialises
Application.authDB). With auth disabled the React UI transparently
falls back to localStorage, the same path the original PR took on
404. Consistent with AgentStore / JobStore; if a "chat history
always on" mode is preferred we can pull DB init out of auth.InitDB
into a shared helper.

Assisted-by: Claude:claude-opus-4-7 [Read Edit Bash]
Signed-off-by: TLoE419 <tloemizuchizu@gmail.com>
ConversationsFile was the JSON wrapper for the old file-based chat
history store — a {Conversations: [...], UpdatedAt: ...} pair
serialised to disk under {baseDir}/{userID}/conversations.json. The
previous commit replaced that store with a GORM-backed one that
writes rows directly as ConversationRecord, leaving the envelope
without a consumer.

Assisted-by: Claude:claude-opus-4-7 [Read Edit Bash]
Signed-off-by: TLoE419 <tloemizuchizu@gmail.com>
Mirror the AgentStore test setup: spin up a PostgreSQL 16 container
via testutil.SetupTestDB(), construct a Store on top of it, exercise
CRUD round-trips. The previous file-based fixtures (TempDir,
filepath.Join checks) are no longer meaningful now that the store
talks GORM.

Tests are skipped on macOS (testcontainers needs Docker) and run on
CI, same as the other GORM-backed suites in the repo.

Also reframed two cases:
* "unsafe IDs" → "malformed IDs" — the DB-backed store doesn't care
  about path traversal, but idRegex still rejects whitespace /
  control characters so IDs stay safe in logs and HTTP responses.
* "anonymous user" no longer pins a directory path (no directory
  exists anymore); instead it checks the round-trip plus the
  per-user isolation guarantee.

Added a DeleteAll round-trip to cover the bulk-clear endpoint.

Assisted-by: Claude:claude-opus-4-7 [Read Edit Bash]
Signed-off-by: TLoE419 <tloemizuchizu@gmail.com>
The seven /api/conversations handlers had no tests. Inject the
*chathistory.Store into the handlers instead of the whole Application and
move the route table into RegisterChatHistoryRoutes, so a spec can drive
the real registration and middleware chain (auth.Middleware plus the
chat_history feature gate) against an in-memory SQLite auth database.

The new specs cover 401 without credentials on every route, 403 when an
admin revoked chat_history, the CRUD round trip, path id over body id,
400 on malformed ids and bodies, per-user isolation, bulk replace and
delete-all, and that a nil store registers nothing (the UI then falls
back to localStorage).

The handlers' nil-store branches are gone: the routes are only
registered with a store, so those branches could never run.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Stefan Walcz <stefan.walcz@walcz.de>
A conversation deleted on one device came back as soon as a client with
a stale copy wrote it again: Delete soft-deleted the row, and the next
Save went through GORM's upsert, which cleared deleted_at and resurrected
it for every device. The lookup that recovered CreatedAt also ran outside
any transaction, so concurrent writers raced on it.

The server is now authoritative over deletions:

* Delete turns the row into a tombstone: content dropped, deleted_at
  set. It is idempotent, and deleting an id the server has not seen yet
  still records a tombstone, so a delete that overtakes the UI's
  in-flight debounced save of the same chat still wins.
* Save runs in a transaction, refuses a tombstoned id with ErrDeleted
  (HTTP 410 Gone), and bumps the version with a single
  UPDATE ... SET version = version + 1 guarded by deleted_at IS NULL,
  so concurrent writers each count and a delete committed between the
  lookup and the write still wins. Two concurrent first writes of the
  same id retry once (ON CONFLICT DO NOTHING, then update).
* Every row carries a server-assigned version (1 on the first save,
  +1 per write including the delete), returned to the client. A non-zero
  version tells the UI the server has seen a conversation, which is how
  it can tell a never-synced draft from a server-side deletion.
* GET /api/conversations returns the tombstones under "deleted".
* DeleteAll and ReplaceAll leave tombstones too. ReplaceAll no longer
  hard-deletes everything first; it upserts the payload, skips ids that
  were deleted (reported in "skipped"), and tombstones the rest.
* createdAt/updatedAt are stored as the client sends them (only zero
  values are stamped), so clients compare updatedAt from their own
  clocks rather than against the server's.

The store specs now run against PostgreSQL (testcontainers) and, with
the auth tag, SQLite through auth.InitDB, since both back the auth
database. The API specs cover a stale client reconnecting after a remote
deletion. Swagger is regenerated with swag v1.16.6 (json.RawMessage
fields get swaggertype), and the feature is documented, including that
it needs user auth.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Stefan Walcz <stefan.walcz@walcz.de>
mergeRemoteAndLocal kept every chat that was only in the browser and
let the server copy win for every shared id. A stale browser therefore
uploaded chats deleted on another device as if they were new drafts,
and a message sent while the list request was still in flight was
replaced by the older server copy and then lost from localStorage too.

The first load now reconciles with the server's tombstones and versions:

* a chat the server lists under "deleted" is dropped locally;
* a chat on both sides keeps the copy with the newer updatedAt, so an
  edit made during the load wins and is uploaded afterwards;
* a chat only in the browser is uploaded only if it was never synced
  (no version). With a version the server has seen it and no longer
  has it, so it is dropped instead of resurrected (this also keeps one
  account's cached chats out of another account on a shared browser);
* deletes made while the list is loading are queued and sent once it
  arrives.

Saves record the version the server returns, and a 410 on save drops
the chat locally instead of retrying it forever. Chats received from
the server count as already sent, so adopting a server copy no longer
echoes it straight back. The bulk localStorage migration is gone: it
replaced the whole server set from a possibly stale browser, and the
per-chat saves already upload never-synced drafts through the tombstone
check.

The Playwright spec drives each case against a stubbed
/api/conversations that follows the server contract.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Stefan Walcz <stefan.walcz@walcz.de>
master's createNewChat sets localaiAssistant, but serializeChat did not
send it and schema.Conversation had no field for it, so a chat with the
Assistant mode on came back with it off after a reload from the server
(and the local cache dropped it too). Add the field on both sides. The
other fields createNewChat sets were already covered; a handler spec now
round-trips all of them, and the UI spec checks the toggle on the way up
and in the local cache.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Stefan Walcz <stefan.walcz@walcz.de>

@localai-org-maint-bot localai-org-maint-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested on 2cfca0c383680e2fe3a48dfcffe31af85ea058cf. The server tombstones address the earlier successful-delete resurrection case, but two client paths still need correction:

  1. [P1] Associate the local cache and unsynced drafts with the authenticated account. useChat.js still reads/writes the shared browser chat key, and reconcileWithServer keeps every unversioned local conversation missing from the server. Logout does not clear or partition that key. If account A has an unsynced conversation (for example a failed first save), signs out, and account B signs in, B's empty list retains A's conversation and the save effect uploads A's private history into B's account. The version check only protects already-synced rows. Partition the cache/pending operations by account and make legacy anonymous-cache migration explicit; add a two-account regression covering an unsuccessful first save.

  2. [P2] Preserve failed deletions until the server acknowledges them. useChat.js:371-402 removes conversations locally and swallows DELETE failures. pendingDeletesRef is populated only while the initial probe is pending, and is not persisted. After a successful probe, disconnect the network and delete a synced conversation: it disappears from the UI, no pending deletion remains, and reopening Chat after reconnect adds the unchanged server copy back. The same problem affects delete-all and failures while flushing pending deletes. Persist deletion intent and retry it, or visibly restore/report a failed delete instead of silently treating it as complete.

I reproduced both paths with a Node harness executing the PR's actual reconciliation and delete callback: the failed delete left zero pending operations and the next reconciliation restored the row; an unversioned previous-account conversation survived reconciliation against an empty new account. This was a focused logic check, not the full browser/Go suite.

These affect privacy and the deletion contract, so they require coordinated state/sync changes rather than a minor review cleanup. The auth-only default remains a separate maintainer decision as described in the PR.

Two gaps in the client half of server-side chat history, both found in
review:

The browser cache was one shared localStorage key. A draft that one
account could not upload (a failed first save) stayed in it, and after
that account signed out and another signed in, the reconcile kept the
unversioned draft and the save pass uploaded it into the second
account. The cache now lives under localai_chats_data:<user id>, Chat
remounts its state when the account changes, and nothing is synced
without a signed-in account. The shared key is left for browser-only
use without auth and is not uploaded when signed in, because nothing
tells which account it belongs to.

Deletes were dropped locally and their DELETE errors swallowed; only
deletes made while the first list was loading were queued, and not
persisted. A delete made while the server was unreachable therefore
came back with the next list. Deletes (single, delete all, and deletes
on the Home page) are now kept as pending deletes in the account's
cache until the server confirms them, override the server list in the
reconcile, and are sent again on the next change, on the online event
and on the next load. While a delete-all is pending no conversation is
uploaded, so a chat started after it is not caught by it.

The Playwright specs replay both review scenarios (account switch after
a failed first save; delete and delete-all while offline) with
counter-checks that the same account keeps and uploads its own draft
and that a confirmed delete is neither re-sent nor resurrected.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Stefan Walcz <stefan.walcz@walcz.de>
@walcz-de

walcz-de commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks, both reproduced and fixed in ef0ae8f.

P1 (cache bound to the account): the browser cache now lives under localai_chats_data:<user id>, together with that account's pending deletes, and the Chat page remounts its state when the account changes. Without a signed-in account nothing is synced. The old shared key stays in use only without auth; when signed in it is neither read nor uploaded, because nothing tells which account it belongs to. Signing out does not clear the cache, so a draft whose first save failed survives and is uploaded when the same account signs in again, never into another one.

P2 (deletes until confirmed): single deletes, delete-all and deletes from the Home page are kept as pending deletes in the account's cache until the server confirms them. They override the server list in the reconcile and are re-sent on the next change, on online, and on the next load. While a delete-all is pending nothing is uploaded, so a chat started after it is not caught by it.

e2e/chat-history-sync.spec.js replays both scenarios (account switch after a failed first save; delete and delete-all while offline). Both fail on 2cfca0c and pass now. The counter-checks: the same account keeps and uploads its own draft, and a confirmed delete is neither re-sent nor resurrected. Docs updated in features/chat-history.md.

Open question: chats stored under the shared key before chat history was enabled are not adopted by a signed-in account. Is that the behaviour you want, or would you prefer an explicit import path?

@localai-org-maint-bot

Copy link
Copy Markdown
Collaborator

@mudler Incremental source-review sign-off at 03cd9c115, subject to conflict resolution, CI, and human DCO certification of the fix commit. The account-scoped cache and persistent deletion intents address my earlier privacy and failed-delete findings.

I found and fixed one remaining clear-all race: navigating away before the 500 ms cache debounce left an old unsynced draft on disk, allowing it to be uploaded after the deletion was acknowledged. Clear-all now persists the cleared list and deletion intent together. Added a Playwright regression and documentation. An isolated Node harness executing the actual PR functions reproduced the failure before the fix and passed afterward; JS syntax and whitespace checks also pass. I did not run the browser suite.

No further confirmed source blocker in this incremental review. The branch still conflicts with master 48f358dd9 in core/application/application.go, core/http/endpoints/localai/api_instructions_test.go, and core/http/react-ui/src/hooks/useChat.js; it is not merge-ready until those conflicts and the normal checks are resolved. The fix commit has Assisted-by attribution and needs human DCO certification.

localai-org-maint-bot and others added 3 commits October 19, 2026 11:40
Navigation cancels the debounced cache write and can restore unsynced
chats after Clear all. Save the cleared list with its pending deletion
before returning, and cover immediate navigation in the browser spec.

Assisted-by: Codex:GPT-6
Signed-off-by: Stefan Walcz <stefan.walcz@walcz.de>
Conflicts with 48f358d:
- core/application/application.go: keep chatHistoryStore next to the new
  WebSocket ticket fields.
- api_instructions_test.go: both sides added an instruction (chat-history,
  motion); the list has 23 entries now.
- useChat.js: keep the account-scoped cache and server sync. The chat setting
  reasoningEffort added upstream (mudler#12700, mudler#12716) is now part of the synced
  chat, and the browser cache keeps converting the old reasoningOff flag.

Docs-Not-Needed: conflict resolution only; the synced chat is opaque JSON
and the documentation lists no chat fields.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Stefan Walcz <stefan.walcz@walcz.de>
- "clear all persists before navigation" opened a single chat, and the menu
  offers no Clear all for one chat, so the spec timed out in every run. It
  now opens two unsynced chats and checks that neither survives. Reverting
  03cd9c1's save in clearAllChats turns it red again.
- "a delete the server confirmed is not sent again" reopened the page as
  soon as the server had the request, before the page had the answer; the
  unconfirmed delete was then rightly sent again (4 of 5 runs). It now waits
  until the cache holds no pending delete.

Docs-Not-Needed: test-only change.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Stefan Walcz <stefan.walcz@walcz.de>
@walcz-de
walcz-de force-pushed the feat/chat-history-server branch from 03cd9c1 to a5b50c1 Compare October 19, 2026 09:42
@walcz-de

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the clear-all fix. Merged master 48f358dd9 (3e5d17803) and resolved the three conflicts:

Running the browser suite locally turned up two problems in chat-history-sync.spec.js, fixed in a separate test-only commit (a5b50c19e):

  • "clear all persists before navigation …" opened a single chat, and the menu offers no Clear all for one chat, so it timed out on every run. It now opens two unsynced drafts. Reverting the saveChats call from 03cd9c115 turns it red (["draft","other"] left in the cache), so it covers the race you found.
  • "a delete the server confirmed is not sent again …" (mine) reopened the page as soon as the server had the DELETE, before the page had processed the response, so the still-pending delete was correctly re-sent (red in 4 of 5 runs). It now waits until the cache has no pending delete.

Full UI e2e suite: 1772 passed, 4 skipped; Go: core/application, core/http/endpoints/localai, core/services/chathistory pass. All commits carry my DCO sign-off; 03cd9c115 is now 8816a7200, same content, signed off.

The question from my last comment is still open: should chats stored under the shared key before chat history was enabled be adopted by the first account that signs in, or stay unadopted as now?

walcz-de added a commit to walcz-de/LocalAI that referenced this pull request Oct 19, 2026

@mudler-agent mudler-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One remaining persistence issue on this head: the server drops the conversation's reasoning-effort setting. Please preserve it and add a round-trip regression before merging.

Validation: a focused SQLite reproduction using unchanged copies of this head's schema and store confirmed the loss under the race detector. Focused Node checks for account-scoped caches and persisted deletion intent passed. The repository-level Go race tests and vet could not start because generated protobuf packages were absent; full browser tests were not run. These results do not establish full CI coverage.

Comment thread core/schema/chat_conversation.go Outdated
LocalAIAssistant bool `json:"localaiAssistant,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"topP,omitempty"`
TopK *float64 `json:"topK,omitempty"`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve reasoningEffort in the server-side conversation schema. serializeChat in core/http/react-ui/src/hooks/useChat.js sends this property, but Conversation does not declare it. JSON binding therefore discards it before persistence, so loading a synced conversation loses the selected reasoning behavior.

I reproduced this with an actual schema/store round-trip: decode a conversation containing "reasoningEffort":"none", call Store.Save, then Store.Get and JSON-encode the result. The response omits reasoningEffort. The focused SQLite reproduction passed under the race detector.

Please add the field, regenerate the Swagger artifacts, and add a regression asserting that a non-empty reasoning-effort setting survives save/get/list.

reasoningEffort (mudler#12700, mudler#12716) is part of the chat the UI syncs since the
master merge, but schema.Conversation had no field for it, so the server
dropped it and a reload from the server reset the setting. Add the field.

The handler spec that round-trips every chat field now includes
reasoningEffort. A schema test reads serializeChat in useChat.js and fails
for any key the UI sends that schema.Conversation cannot hold, so the next
such drift shows up as a failing test rather than in review (it reports
reasoningEffort on the previous head). Swagger and docs updated.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Stefan Walcz <stefan.walcz@walcz.de>
@walcz-de

Copy link
Copy Markdown
Contributor Author

Right — my merge claimed reasoningEffort was part of the synced chat, but schema.Conversation had no field for it, so the server dropped it. Fixed in 5f42128f1:

  • schema.Conversation gets ReasoningEffort (json:"reasoningEffort,omitempty"); swagger and features/chat-history.md updated.
  • The handler spec that round-trips every chat field (chat_history_auth_test.go) now includes reasoningEffort and checks the value.
  • New core/schema/chat_conversation_ui_test.go: it reads serializeChat in useChat.js and fails for any key the UI sends that schema.Conversation cannot hold. On the previous head it reports exactly reasoningEffort; this is the second time a field drifted this way (localaiAssistant before), so the check should catch the next one.

go test ./core/schema/ ./core/http/routes/ ./core/services/chathistory/ pass locally (with generated protobufs).

walcz-de added a commit to walcz-de/LocalAI that referenced this pull request Oct 19, 2026

@mudler-agent mudler-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reasoningEffort persistence fix in 5f42128 resolves my original finding. I verified the UI/schema guard goes red with the previous schema, and the updated authenticated HTTP/SQLite history tests pass under the race detector.

One test-only blocker remains: core/schema/chat_conversation_ui_test.go uses t.Fatalf (twice), t.Fatal, and t.Errorf. These four new calls fail the repository forbidigo rule. Reproduced with Go 1.26.8 and golangci-lint against the PR diff.

Please keep the guard but convert it to the existing schema package Ginkgo/Gomega style: Describe/It, Expect(err).NotTo(HaveOccurred()), Expect(body).NotTo(BeNil()), a >= 10 key-count assertion, and HaveKey for the reflected JSON fields. I tested that conversion: the missing-field case still fails, the restored schema passes with -race, and changed-package lint reports 0 issues. No production change is needed.

CI is queued; this review does not claim a full CI or PostgreSQL pass.

The new drift guard uses testing methods forbidden by repository lint.
Keep its field coverage and fail-fast parser checks in Ginkgo/Gomega.

Assisted-by: nib:gpt-6.1-sol

@mudler-agent mudler-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reasoningEffort persistence fix and the follow-up Ginkgo conversion resolve my findings. The conversion is pushed as 6781979; it preserves the drift guard, fails with the missing field, and passes schema race tests and changed-package lint with Go 1.26.8. Authenticated HTTP and SQLite history tests also passed under the race detector. Approval is source/local-test review, not a substitute for required CI. PostgreSQL coverage was not available locally. The bot follow-up intentionally leaves human DCO certification to the contributor, per repository policy.

@mudler
mudler merged commit 3630700 into mudler:master Oct 19, 2026
4 of 22 checks passed
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.

feat: persist Chat History server-side

5 participants