[GH-ISSUE #2689] [Issue]: Re-importing a built-in agent wipes its connectionId, settings, and OAuth tokens (create→update reroute) #1333

Closed
opened 2026-10-21 05:31:42 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 20, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2689

Originally assigned to: @SpicyMarinara on GitHub.

Summary

Re-importing (or re-creating) a built-in agent silently wipes its stored connectionId, settings (including Spotify/YouTube OAuth tokens), and promptTemplate. create() detects an existing built-in type and reroutes to update() with a full schema-default payload, which wholesale-overwrites the stored row — so the agent loses its per-agent connection (falling back to the wrong provider/model) and any auth tokens stored in settings.

Expected behavior

Re-importing or re-saving a built-in agent should not clobber fields the caller didn't intend to change — especially the per-agent connectionId and OAuth tokens stored in settings. Import should be idempotent or merge, mirroring the OAuth-field preservation the agent editor already does.

Actual behavior

create() detects a built-in type and, if a config of that type exists, returns this.update(existing.id, input). The POST /agents route parses the body with createAgentConfigSchema, which (zod) applies all defaults — so the parsed input always contains connectionId:null, settings:{} (or imported, token-stripped settings), promptTemplate:"", etc. update() then runs every if (data.X !== undefined) branch: connectionId is present (null) → overwrites the stored connection with null; settings is present → JSON.stringify({ ...data.settings }) wholesale-replaces stored settings (no merge); promptTemplate overwrites. Agent export hardcodes connectionId:null and strips Spotify/YouTube tokens, so the import payload carries exactly those clobbering defaults.

How it breaks the pipeline

Code trace (staging @ b826f8f4):

  • packages/server/src/services/storage/agents.storage.ts:187-191 — create() built-in reroute to update(existing.id, input).
  • packages/server/src/routes/agents.routes.ts:221 — parses with createAgentConfigSchema (applies defaults).
  • agents.storage.ts:219-239 — update() if (data.X !== undefined) branches: connectionId overwritten to null (:226); settings wholesale-replaced via JSON.stringify({ ...data.settings }) (:230-233); promptTemplate overwritten.
  • Export/import: AgentsPanel.serializeAgentConfig / AgentEditor hardcode connectionId:null; agent-transfer.ts:2-6 strips spotifyAccessToken/spotifyRefreshToken/youtubeApiKey; import loop POSTs each entry (AgentsPanel.tsx:490-499). The editor's own handleSave works around the wholesale replace by re-injecting preservedSpotifyFields (AgentEditor.tsx:877-892) — but the import/create-reroute path has no such preservation.

Steps to reproduce

  1. Configure the built-in Spotify/Music-DJ (or any built-in) agent with a specific per-agent connection and (for Spotify) OAuth.
  2. Export that agent, then import it via AgentsPanel.
  3. POST /agents → create() sees the existing built-in → update(existing.id, importedInput) with connectionId:null and token-stripped settings.
  4. The row now has connection_id NULL and settings without the per-agent connection / Spotify tokens → the agent resolves to the default/main connection and (Spotify) loses auth.

Suggested fix

Do not run create()'s update-reroute with a full createAgentConfigSchema payload carrying schema defaults. Either (a) forward only fields the caller actually supplied (omit connectionId/settings/promptTemplate when they equal unintended defaults), (b) make update()'s settings branch merge with stored settings and skip overwriting connectionId/promptTemplate when not meaningfully provided, or (c) treat built-in re-import as an idempotent merge. Mirror the OAuth-field preservation the editor already does (spotifyAccessToken/spotifyRefreshToken/spotifyExpiresAt/spotifyScope/youtubeApiKey).

Environment

  • Marinara Engine version: 2.0.0 (staging @ b826f8f4)
  • Install type: source
  • OS + version: All (code-level defect, platform-independent)
  • Browser or app shell: All

Additional context

Found via a systematic multi-agent audit of the agent subsystem. Causes data loss (per-agent connection + OAuth tokens) and silently moves the agent to the wrong connection on a routine export→import. Part of a set of agent-pipeline issues filed together.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Jun 20, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2689 Originally assigned to: @SpicyMarinara on GitHub. ## Summary Re-importing (or re-creating) a **built-in** agent silently wipes its stored `connectionId`, settings (including Spotify/YouTube OAuth tokens), and `promptTemplate`. `create()` detects an existing built-in type and reroutes to `update()` with a full schema-default payload, which wholesale-overwrites the stored row — so the agent loses its per-agent connection (falling back to the wrong provider/model) and any auth tokens stored in settings. ## Expected behavior Re-importing or re-saving a built-in agent should not clobber fields the caller didn't intend to change — especially the per-agent `connectionId` and OAuth tokens stored in settings. Import should be idempotent or merge, mirroring the OAuth-field preservation the agent editor already does. ## Actual behavior `create()` detects a built-in type and, if a config of that type exists, returns `this.update(existing.id, input)`. The POST `/agents` route parses the body with `createAgentConfigSchema`, which (zod) applies **all** defaults — so the parsed `input` always contains `connectionId:null`, `settings:{}` (or imported, token-stripped settings), `promptTemplate:""`, etc. `update()` then runs every `if (data.X !== undefined)` branch: `connectionId` is present (null) → overwrites the stored connection with null; `settings` is present → `JSON.stringify({ ...data.settings })` wholesale-replaces stored settings (no merge); `promptTemplate` overwrites. Agent export hardcodes `connectionId:null` and strips Spotify/YouTube tokens, so the import payload carries exactly those clobbering defaults. ## How it breaks the pipeline Code trace (staging @ `b826f8f4`): - `packages/server/src/services/storage/agents.storage.ts:187-191` — `create()` built-in reroute to `update(existing.id, input)`. - `packages/server/src/routes/agents.routes.ts:221` — parses with `createAgentConfigSchema` (applies defaults). - `agents.storage.ts:219-239` — `update()` `if (data.X !== undefined)` branches: `connectionId` overwritten to null (`:226`); `settings` wholesale-replaced via `JSON.stringify({ ...data.settings })` (`:230-233`); `promptTemplate` overwritten. - Export/import: `AgentsPanel.serializeAgentConfig` / `AgentEditor` hardcode `connectionId:null`; `agent-transfer.ts:2-6` strips `spotifyAccessToken`/`spotifyRefreshToken`/`youtubeApiKey`; import loop POSTs each entry (`AgentsPanel.tsx:490-499`). The editor's own `handleSave` works around the wholesale replace by re-injecting `preservedSpotifyFields` (`AgentEditor.tsx:877-892`) — but the import/create-reroute path has no such preservation. ## Steps to reproduce 1. Configure the built-in Spotify/Music-DJ (or any built-in) agent with a specific per-agent connection and (for Spotify) OAuth. 2. Export that agent, then import it via AgentsPanel. 3. POST `/agents` → `create()` sees the existing built-in → `update(existing.id, importedInput)` with `connectionId:null` and token-stripped settings. 4. The row now has `connection_id` NULL and settings without the per-agent connection / Spotify tokens → the agent resolves to the default/main connection and (Spotify) loses auth. ## Suggested fix Do not run `create()`'s update-reroute with a full `createAgentConfigSchema` payload carrying schema defaults. Either (a) forward only fields the caller actually supplied (omit `connectionId`/`settings`/`promptTemplate` when they equal unintended defaults), (b) make `update()`'s settings branch **merge** with stored settings and skip overwriting `connectionId`/`promptTemplate` when not meaningfully provided, or (c) treat built-in re-import as an idempotent merge. Mirror the OAuth-field preservation the editor already does (`spotifyAccessToken`/`spotifyRefreshToken`/`spotifyExpiresAt`/`spotifyScope`/`youtubeApiKey`). ## Environment - Marinara Engine version: 2.0.0 (staging @ `b826f8f4`) - Install type: source - OS + version: All (code-level defect, platform-independent) - Browser or app shell: All ## Additional context Found via a systematic multi-agent audit of the agent subsystem. Causes data loss (per-agent connection + OAuth tokens) and silently moves the agent to the wrong connection on a routine export→import. Part of a set of agent-pipeline issues filed together. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
Author
Owner

@SpicyMarinara commented on GitHub (Jun 21, 2026):

Fixed in branch codex/spicy-issue-sweep-2026-06-21.

What changed:

  • Built-in agent re-imports now merge over the existing stored row instead of sending schema defaults directly into the full update path.
  • Existing server-held settings are preserved while safe imported settings can still update the agent.
  • Export/default placeholders no longer wipe an existing built-in agent's connection, image path, or prompt template.

Final repo checks will run after the full issue sweep.

<!-- gh-comment-id:4761504589 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Fixed in branch `codex/spicy-issue-sweep-2026-06-21`. What changed: - Built-in agent re-imports now merge over the existing stored row instead of sending schema defaults directly into the full update path. - Existing server-held settings are preserved while safe imported settings can still update the agent. - Export/default placeholders no longer wipe an existing built-in agent's connection, image path, or prompt template. Final repo checks will run after the full issue sweep.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1333
No description provided.