[Bug] ZCode 3.10.2 (Windows x64) - session model selection persistence fails at bootstrap: FOREIGN KEY constraint (writes session_entry before session row exists)
Nobody has claimed this yet.
- 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), modelGLM-5.3 - Database:
~/.zcode/cli/db/db.sqlite(schema:session_entry.session_idhas 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
sessionrow 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:
- Persist model selection only after the session row is guaranteed to exist (await session creation),
- Set
touchSession: true/ upsert the parentsessionrow in the same transaction, - 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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