[GH-ISSUE #7107] [Bug]: Re-importing an updated .agent.zip creates a duplicate agent instead of updating it #3971

Closed
opened 2026-10-21 06:09:11 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Chronophylos on GitHub (Oct 5, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/7107

Problem

Re-importing an updated .agent.zip does not update the agent you already have — it creates a second agent with the same name. There is no update path anywhere in the import flow, so every version a user imports accumulates as another row.

Why

POST /api/agents/import calls storage.create() unconditionally and never looks for a pre-existing agent to update:

  • packages/server/src/routes/agents.routes.ts:250 — return storage.create(buildImportedAgentInput(input));
  • packages/server/src/services/storage/agents.storage.ts:247-277 — create() only updates in place for built-in types (isBuiltInAgentType). Custom types always get newId() and a fresh row.

The archive carries no stable identity to match on: createAgentConfigSchema (packages/shared/src/schemas/agent.schema.ts:71-83) has no id/externalId/sourceId, and zod strips unknown keys, so a manifest id would be discarded anyway. Export (packages/client/src/lib/agent-transfer.ts:161-204) drops the row id when writing the manifest.

The panel then compounds it: the type is re-randomized twice per import, once on the client and again on the server.

  • packages/client/src/lib/agent-transfer.ts:115 — createImportedAgentType(sourceType)
  • packages/server/src/routes/agents.routes.ts:169 — type: createImportedAgentType(input.agent.type)

createImportedAgentType (packages/shared/src/types/agent.ts:682-693) appends a fresh crypto.randomUUID() each call, so a re-import yields:

archive            custom-body-state
after client       custom-import-custom-body-state-<uuid1>
after server       custom-import-custom-import-custom-body-state-<uuid1>-<uuid2>

Two re-imports produce two unrelated types. keepLatestConfigPerType (agents.storage.ts:38-47), which is what hides duplicate rows from the list, dedupes by type — so it keeps both. Two "Body State" agents appear side by side.

Impact

Beyond the duplicate row:

  • Per-chat settings are orphaned. Chat metadata stores agentOverrides keyed by <type> (packages/shared/src/types/chat.ts:338, chat-presets.storage.ts:70). A new type means the new agent starts with no enable/disable state in every chat, and the old agent's state is stranded under a type that no longer resolves.
  • Old agents keep running. Duplicates are not disabled, so an old version of the same tracker can continue to execute.
  • No UI offers a replace/update choice — the review modal only confirms capabilities.

Reproduction

  1. Import Body-State.agent.zip via Agents → Import. Note the resulting agent.
  2. Edit the agent's prompt in its manifest and export again.
  3. Import the updated archive.
  4. Observe two agents named "Body State" in the list.

Suggested direction

Give the archive a stable identity that survives export/import, then have the import handler upsert on it:

  1. Add an optional stable id to the manifest config (e.g. config.externalId) and to createAgentConfigSchema, and write it on export.
  2. In buildImportedAgentInput, look up an existing agent by externalId before storage.create(). If found, storage.update(existing.id, …) and keep the existing type — that also preserves the chat's agentOverrides keys and the run history.
  3. Only fall back to a fresh custom-import-… type when no externalId is present (hand-written agents, older archives).
  4. Surface the choice in the import review modal when a match exists, so replacing is explicit rather than silent.

Also worth fixing on its own: the type is randomized twice per import. The server-side re-randomization compounds the slug into custom-import-custom-import-…, which is user-visible and makes types unreadable in logs.

Note

Reported from a Body State agent + panel integration. The panel works around this by resolving the agent by name (resolveAgentType), which happens to find the newest row, but that is a workaround for the missing identity — it cannot disambiguate two same-named agents and will silently bind to whichever row listLatest() returns first.

Originally created by @Chronophylos on GitHub (Oct 5, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/7107 ## Problem Re-importing an updated `.agent.zip` does not update the agent you already have — it creates a second agent with the same name. There is no update path anywhere in the import flow, so every version a user imports accumulates as another row. ## Why `POST /api/agents/import` calls `storage.create()` unconditionally and never looks for a pre-existing agent to update: - `packages/server/src/routes/agents.routes.ts:250` — `return storage.create(buildImportedAgentInput(input));` - `packages/server/src/services/storage/agents.storage.ts:247-277` — `create()` only updates in place for **built-in** types (`isBuiltInAgentType`). Custom types always get `newId()` and a fresh row. The archive carries no stable identity to match on: `createAgentConfigSchema` (`packages/shared/src/schemas/agent.schema.ts:71-83`) has no `id`/`externalId`/`sourceId`, and zod strips unknown keys, so a manifest id would be discarded anyway. Export (`packages/client/src/lib/agent-transfer.ts:161-204`) drops the row id when writing the manifest. The panel then compounds it: the type is re-randomized **twice** per import, once on the client and again on the server. - `packages/client/src/lib/agent-transfer.ts:115` — `createImportedAgentType(sourceType)` - `packages/server/src/routes/agents.routes.ts:169` — `type: createImportedAgentType(input.agent.type)` `createImportedAgentType` (`packages/shared/src/types/agent.ts:682-693`) appends a fresh `crypto.randomUUID()` each call, so a re-import yields: ``` archive custom-body-state after client custom-import-custom-body-state-<uuid1> after server custom-import-custom-import-custom-body-state-<uuid1>-<uuid2> ``` Two re-imports produce two unrelated types. `keepLatestConfigPerType` (`agents.storage.ts:38-47`), which is what hides duplicate rows from the list, dedupes **by type** — so it keeps both. Two "Body State" agents appear side by side. ## Impact Beyond the duplicate row: - **Per-chat settings are orphaned.** Chat metadata stores `agentOverrides` keyed by `<type>` (`packages/shared/src/types/chat.ts:338`, `chat-presets.storage.ts:70`). A new type means the new agent starts with no enable/disable state in every chat, and the old agent's state is stranded under a type that no longer resolves. - **Old agents keep running.** Duplicates are not disabled, so an old version of the same tracker can continue to execute. - No UI offers a replace/update choice — the review modal only confirms capabilities. ## Reproduction 1. Import `Body-State.agent.zip` via Agents → Import. Note the resulting agent. 2. Edit the agent's prompt in its manifest and export again. 3. Import the updated archive. 4. Observe two agents named "Body State" in the list. ## Suggested direction Give the archive a stable identity that survives export/import, then have the import handler upsert on it: 1. Add an optional stable id to the manifest config (e.g. `config.externalId`) and to `createAgentConfigSchema`, and write it on export. 2. In `buildImportedAgentInput`, look up an existing agent by `externalId` before `storage.create()`. If found, `storage.update(existing.id, …)` and keep the existing `type` — that also preserves the chat's `agentOverrides` keys and the run history. 3. Only fall back to a fresh `custom-import-…` type when no `externalId` is present (hand-written agents, older archives). 4. Surface the choice in the import review modal when a match exists, so replacing is explicit rather than silent. Also worth fixing on its own: the type is randomized twice per import. The server-side re-randomization compounds the slug into `custom-import-custom-import-…`, which is user-visible and makes types unreadable in logs. ## Note Reported from a Body State agent + panel integration. The panel works around this by resolving the agent by **name** (`resolveAgentType`), which happens to find the newest row, but that is a workaround for the missing identity — it cannot disambiguate two same-named agents and will silently bind to whichever row `listLatest()` returns first.
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#3971
No description provided.