{"record":{"id":"543cdbbc11863def","repo":"mastra-ai/mastra","slug":"could-not-save-the-modeid-mode-model","errorCode":null,"errorMessage":"Could not save the ${modeId} mode model","messagePattern":"Could not save the (.+?) mode model","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/tui/src/tui/commands/models.ts","lineNumber":88,"sourceCode":"    } else {\n      nextSettings.customModelPacks.push({\n        name: customName,\n        models: modeModels,\n        createdAt: new Date().toISOString(),\n      });\n    }\n    nextSettings.models.activeModelPackId = nextPackId;\n    nextSettings.models.modeDefaults = modeModels;\n  }\n\n  let modeSettingSaved = false;\n  let packSettingSaved = false;\n  let globalSettingsWriteStarted = false;\n  try {\n    await ctx.state.session.thread.setSetting({ key: modeSettingKey, value: modelId });\n    modeSettingSaved = true;\n    const savedModeSetting = await ctx.state.session.thread.getSetting({ key: modeSettingKey });\n    if (savedModeSetting !== modelId) throw new Error(`Could not save the ${modeId} mode model`);\n\n    await ctx.state.session.thread.setSetting({ key: THREAD_ACTIVE_MODEL_PACK_ID_KEY, value: nextPackId });\n    packSettingSaved = true;\n    const savedPackSetting = await ctx.state.session.thread.getSetting({ key: THREAD_ACTIVE_MODEL_PACK_ID_KEY });\n    if (savedPackSetting !== nextPackId) throw new Error('Could not save the active model pack');\n    globalSettingsWriteStarted = true;\n    saveSettings(nextSettings);\n    await ctx.state.session.model.switch({ modelId, scope: 'global' });\n  } catch (error) {\n    if (globalSettingsWriteStarted) {\n      try {\n        saveSettings(settings);\n      } catch {\n        // Keep the original failure. The thread and active model still roll back below.\n      }\n    }\n\n    const rollbacks: Array<Promise<unknown>> = [];","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/tui/src/tui/commands/models.ts#L70-L106","documentation":"switchCurrentModeModel writes the chosen modelId to the thread setting for the mode, then reads it back to verify the write actually persisted. If the read-back value differs from what was written, the persistence layer silently failed, and the command throws 'Could not save the <modeId> mode model' so the UI doesn't report success on a lost write.","triggerScenarios":"thread.setSetting succeeded without throwing but thread.getSetting returns a stale/different value — e.g. the memory/storage backend failed to persist, a concurrent write clobbered it, or the storage adapter ignores unknown setting keys.","commonSituations":"Storage backend misconfigured or read-only; session thread bound to a different memory instance than the one written; race with another writer to the same setting; custom memory adapter whose setSetting is a no-op.","solutions":["Verify the storage/memory backend is writable and correctly wired to the session thread, then retry the model switch","Retry the switch — transient storage failures resolve on a second attempt","If it persists, inspect the storage adapter's set/getSetting implementation for lost writes or key mismatch (modeSettingKey)"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Verify persistence works before switching models\nconst probeKey = '__write_probe__';\nawait ctx.state.session.thread.setSetting({ key: probeKey, value: 'ok' });\nif ((await ctx.state.session.thread.getSetting({ key: probeKey })) !== 'ok') {\n  throw new Error('Thread settings are not persisting — check the storage/memory backend.');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await switchCurrentModeModel(ctx, modeId, modelId, nextPackId);\n} catch (e) {\n  if (String(e.message).startsWith('Could not save the')) {\n    console.error('Setting did not persist; verify the memory/storage backend is writable, then retry.');\n  } else throw e;\n}","preventionTips":["Ensure the session thread's memory/storage backend is correctly wired and writable","Investigate immediately if setSetting succeeds but getSetting disagrees — it signals a broken adapter or a race","Serialize setting writes if multiple writers can touch the same thread","Test the storage adapter's set/getSetting round-trip in CI"],"tags":["persistence","settings","storage","tui"],"backgroundTag":"write-not-persisted","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}