zai-org / zai-org/feedback

[Bug] ZCode 3.10.2 (Windows x64) - session model selection persistence fails at bootstrap: FOREIGN KEY constraint (writes session_entry before session row exists)

Open
#448 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

priority: P2
Dominant language
No language data
Stars
22
Forks
1
PR merge metrics
No merged PRs in 30d

Description

Environment
  • ZCode 3.10.2.6414, Desktop (Electron) + CLI, Windows 10 x64 (10.0.19045)
  • Provider: builtin:zai-coding-plan (api.z.ai Anthropic-compatible endpoint), model GLM-5.3
  • Database: ~/.zcode/cli/db/db.sqlite (schema: session_entry.session_id has FK -> session.id)
Summary

At session bootstrap, model selection is persisted via persistSessionModelSelection (module bootstrap, zcode.cjs):

await e.sessionStore.saveSessionEntry({
  id: `${e.sessionId}:runtime-model-selection`,
  sessionID: e.sessionId,
  type: u4,
  touchSession: false,   // <- does not ensure the parent session row exists
  ...
})

session_entry has session_id -> session.id as a foreign key. When the write lands before the session row for that session exists, SQLite rejects it with FOREIGN KEY constraint failed, the selection is dropped, and only a warning is logged:

{"event":"session.model_selection.persist_failed","module":"bootstrap",
 "message":"Session model selection persistence failed",
 "error":"FOREIGN KEY constraint failed","modelId":"GLM-5.3",
 "providerId":"builtin:zai-coding-plan","thoughtLevel":"max"}
Evidence (single machine, one day)

16 occurrences on 2026-08-31 (zcode-2026-08-31.jsonl). Checking every referenced sessionId against the DB:

  • 11 / 16 sessions have no row at all in session (aborted bootstraps) -> model selection permanently lost.
  • 5 / 16 sessions got their session row later; a subsequent persist retry succeeded.

Worked example (own live session):

Time (UTC) Event
18:08:51.305 session.model_selection.persist_failed (FK constraint)
18:09:30.614 session row created (39 s after the persist attempt)
18:27:38.038 session_entry <id>:runtime-model-selection written on a later retry
Impact
  • Per-session model selection is not restored for any session that ends before a successful retry; for aborted sessions it is lost permanently.
  • Warning noise on every affected bootstrap.
  • No DB corruption (failed writes never land; zero orphaned rows), so severity is low-to-medium, but the write ordering is clearly unintended.
Suggested fix

Any of:

  1. Persist model selection only after the session row is guaranteed to exist (await session creation),
  2. Set touchSession: true / upsert the parent session row in the same transaction,
  3. Retry the entry write on FK failure once the session row lands.

Not provider- or model-specific (payload is whatever model is selected), and it reproduces on a stock configuration.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at persistSessionModelSelection in the bootstrap module of zcode.cjs and trace sessionStore.saveSessionEntry alongside creation of the session row in the SQLite database. Use the reported FOREIGN KEY failure and the listed session model-selection event as the reproduction signals; done means selection persistence no longer races session creation and aborted bootstraps do not produce avoidable failures.

Written by the indexing model from the issue text.

Assessment

Tech stack
electron, javascript, sqlite
Domain
cli, databases, desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.