[GH-ISSUE #3670] [Issue]: capability migration is marked complete before chat selections persist #1981

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

Originally created by @Xelvanis on GitHub (Jul 16, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3670

Originally assigned to: @SpicyMarinara on GitHub.

Summary

The v2.3 legacy capability migration writes availability-migration-v1.json before migrating per-chat capability selections. That marker is also the gate that decides whether the chat migration runs. If the process exits or the chat migration fails after the marker is written, the next startup treats migration as complete and permanently skips the unfinished chat work.

This can leave legacy packages installed globally while older chats are missing their expected Maps, Calls, or Conversation-game selections.

Expected behavior

The migration should be restart-safe and idempotent:

  1. Install/record legacy packages.
  2. Migrate every affected chat.
  3. Flush the chat changes to durable storage.
  4. Write the completion marker last.

After a failure at any earlier step, the next startup should retry or resume safely.

Actual behavior

migrateLegacyAvailability() installs packages and then writes the marker with writeFile() plus rename() before returning migrated: true.

Only after that function returns does app.ts call migrateLegacyChatCapabilitySelections(db). On a later startup, the marker makes migrateLegacyAvailability() return migrated: false, and app.ts therefore does not call the chat migration again.

The persistence ordering creates an additional failure window: patchMetadata() updates the in-memory file-native store, while the table write is debounced for 750 ms. There is no flushDB() before migration completion. A hard exit can therefore leave the marker on disk while all or part of the chat migration remains only in memory.

The chat migration call is also outside the package-migration try/catch, so a thrown chat patch aborts startup after the marker has already been written.

Steps to reproduce

  1. Prepare pre-v2.3 user data containing at least one existing Roleplay, Game, Visual Novel, or Conversation chat, and remove capability-packages/availability-migration-v1.json if reproducing against a disposable copy.
  2. Start the first legacy capability migration.
  3. After writeAvailabilityMigration("legacy") completes but before the chat migration is durably flushed, terminate the process abruptly. For deterministic fault injection, throw or exit immediately before migrateLegacyChatCapabilitySelections(db), or from the first patchMetadata() call.
  4. Restart Marinara with the same data directory.
  5. Observe that availability-migration-v1.json exists, so migrateLegacyAvailability() returns migrated: false.
  6. Observe that app.ts skips migrateLegacyChatCapabilitySelections(db) and the affected chats retain unmigrated activeAgentIds.

Environment

  • Marinara Engine version: 2.3.1 (staging at 77f9f2a61e3647cad9f41399430648a00a735210)
  • Install type: all; most visible with file-native storage
  • OS + version: platform independent
  • Browser or app shell: not applicable; server startup migration

Logs, screenshots, or video

No specific error is required; an abrupt process exit is sufficient.

Relevant ordering:

Additional context

The package architecture contract in docs/development/optional-agent-packages.md says migration is idempotent and records completion only after all legacy availability entries are durable. Issue #3612 also explicitly requires interrupted-download and upgrade-retention proof.

Suggested correction:

  1. Move completion-marker ownership into a single orchestrator covering both package and chat migration.
  2. Run the idempotent chat migration before writing the marker.
  3. Call flushDB() and fail if persistence did not complete.
  4. Write/rename the marker only after the flush succeeds.
  5. Add restart regressions for failure immediately before chat migration, midway through the chat loop, and after in-memory patches but before flush.

A separate package-stage marker could be retained for avoiding re-downloads, but it must not suppress unfinished chat migration.

Temporary recovery for an already affected installation is to back up the data directory, remove capability-packages/availability-migration-v1.json, and restart while the official catalog is reachable. The installed-package checks should avoid unnecessary reinstallation, and the chat migration is designed to be idempotent.

Related: #3669 covers the separate problem where the chat migration forces enableAgents: true. This issue concerns completion ordering and retry durability even after that behavior is corrected.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Xelvanis on GitHub (Jul 16, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3670 Originally assigned to: @SpicyMarinara on GitHub. ## Summary The v2.3 legacy capability migration writes `availability-migration-v1.json` before migrating per-chat capability selections. That marker is also the gate that decides whether the chat migration runs. If the process exits or the chat migration fails after the marker is written, the next startup treats migration as complete and permanently skips the unfinished chat work. This can leave legacy packages installed globally while older chats are missing their expected Maps, Calls, or Conversation-game selections. ## Expected behavior The migration should be restart-safe and idempotent: 1. Install/record legacy packages. 2. Migrate every affected chat. 3. Flush the chat changes to durable storage. 4. Write the completion marker last. After a failure at any earlier step, the next startup should retry or resume safely. ## Actual behavior `migrateLegacyAvailability()` installs packages and then writes the marker with `writeFile()` plus `rename()` before returning `migrated: true`. Only after that function returns does `app.ts` call `migrateLegacyChatCapabilitySelections(db)`. On a later startup, the marker makes `migrateLegacyAvailability()` return `migrated: false`, and `app.ts` therefore does not call the chat migration again. The persistence ordering creates an additional failure window: `patchMetadata()` updates the in-memory file-native store, while the table write is debounced for 750 ms. There is no `flushDB()` before migration completion. A hard exit can therefore leave the marker on disk while all or part of the chat migration remains only in memory. The chat migration call is also outside the package-migration `try/catch`, so a thrown chat patch aborts startup after the marker has already been written. ## Steps to reproduce 1. Prepare pre-v2.3 user data containing at least one existing Roleplay, Game, Visual Novel, or Conversation chat, and remove `capability-packages/availability-migration-v1.json` if reproducing against a disposable copy. 2. Start the first legacy capability migration. 3. After `writeAvailabilityMigration("legacy")` completes but before the chat migration is durably flushed, terminate the process abruptly. For deterministic fault injection, throw or exit immediately before `migrateLegacyChatCapabilitySelections(db)`, or from the first `patchMetadata()` call. 4. Restart Marinara with the same data directory. 5. Observe that `availability-migration-v1.json` exists, so `migrateLegacyAvailability()` returns `migrated: false`. 6. Observe that `app.ts` skips `migrateLegacyChatCapabilitySelections(db)` and the affected chats retain unmigrated `activeAgentIds`. ## Environment - Marinara Engine version: 2.3.1 (`staging` at `77f9f2a61e3647cad9f41399430648a00a735210`) - Install type: all; most visible with file-native storage - OS + version: platform independent - Browser or app shell: not applicable; server startup migration ## Logs, screenshots, or video No specific error is required; an abrupt process exit is sufficient. Relevant ordering: - [`package-manager.service.ts`: marker gate and marker write](https://github.com/Pasta-Devs/Marinara-Engine/blob/77f9f2a61e3647cad9f41399430648a00a735210/packages/server/src/services/capability-packages/package-manager.service.ts#L392-L406) - [`app.ts`: chat migration runs only when `migration.migrated` is true](https://github.com/Pasta-Devs/Marinara-Engine/blob/77f9f2a61e3647cad9f41399430648a00a735210/packages/server/src/app.ts#L102-L142) - [`legacy-capability-chat-migration.ts`: sequential per-chat patches](https://github.com/Pasta-Devs/Marinara-Engine/blob/77f9f2a61e3647cad9f41399430648a00a735210/packages/server/src/services/capability-packages/legacy-capability-chat-migration.ts#L25-L55) - [`file-backed-store.ts`: 750 ms debounced persistence](https://github.com/Pasta-Devs/Marinara-Engine/blob/77f9f2a61e3647cad9f41399430648a00a735210/packages/server/src/db/file-backed-store.ts#L1104-L1112) ## Additional context The package architecture contract in `docs/development/optional-agent-packages.md` says migration is idempotent and records completion only after all legacy availability entries are durable. Issue #3612 also explicitly requires interrupted-download and upgrade-retention proof. Suggested correction: 1. Move completion-marker ownership into a single orchestrator covering both package and chat migration. 2. Run the idempotent chat migration before writing the marker. 3. Call `flushDB()` and fail if persistence did not complete. 4. Write/rename the marker only after the flush succeeds. 5. Add restart regressions for failure immediately before chat migration, midway through the chat loop, and after in-memory patches but before flush. A separate package-stage marker could be retained for avoiding re-downloads, but it must not suppress unfinished chat migration. Temporary recovery for an already affected installation is to back up the data directory, remove `capability-packages/availability-migration-v1.json`, and restart while the official catalog is reachable. The installed-package checks should avoid unnecessary reinstallation, and the chat migration is designed to be idempotent. Related: #3669 covers the separate problem where the chat migration forces `enableAgents: true`. This issue concerns completion ordering and retry durability even after that behavior is corrected. ## Template check - [ ] I DID NOT read this template and provide the requested details.
Author
Owner

@SpicyMarinara commented on GitHub (Jul 16, 2026):

Resolved in #3678: the legacy capability migration now flushes per-chat selections before writing its completion marker, so interrupted work retries safely. Migration regressions and the full validation suite pass.

<!-- gh-comment-id:4990211936 --> @SpicyMarinara commented on GitHub (Jul 16, 2026): Resolved in #3678: the legacy capability migration now flushes per-chat selections before writing its completion marker, so interrupted work retries safely. Migration regressions and the full validation suite pass.
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#1981
No description provided.