[GH-ISSUE #2467] [Issue]: Parallel tool-using agents on different connections clobber chat.metadata (lost-update race) #1209

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

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

Severity: Medium — concurrency regression. When two tool-using agents run on different connections in the same turn, their stateful built-in tools read-modify-write the shared chat.metadata blob concurrently, so the second writer's stale snapshot overwrites the first writer's additions (lost update).

Expected behavior

Concurrent metadata mutations from parallel agents must not lose writes — either serialize metadata mutations per chat, or merge against a fresh read (main does both).

Actual behavior

Every stateful tool routes through updateChatMetadata, which reads the shared in-memory input.chat.metadata, awaits a full-blob storage.update, then reassigns input.chat.metadata:

// src/engine/generation/tools-runtime.ts:223-233
const metadata = updater({ ...parseRecord(input.chat.metadata) });
await storage.update("chats", chatId, { metadata });   // whole-blob overwrite
input.chat.metadata = metadata;

agent-runner.ts builds every agent's tool closures over the same input, and agent-pipeline.ts executePhase runs groups on different connections concurrently:

// src/engine/agents-runtime/pipeline/agent-pipeline.ts:298-315
await Promise.all(Array.from(groupsByConnection.values()).map((connectionGroups) => mapWithConcurrency(...)));

Two agents on distinct connections each calling a metadata-mutating tool (write_chat_variable, trigger_event, set_expression, update_game_state, append_chat_summary) interleave read-A → read-B → write-A → write-B; B's update overwrites A's mutation in both the in-memory object and the persisted row. (Agents on the same connection are serialized in a for...of await loop, so they do not race.)

Steps to reproduce

  1. Enable chat tools and configure two agents on two different API connections in the same phase.
  2. Have both call a stateful built-in tool (e.g. both write_chat_variable or trigger_event) during one turn.
  3. One agent's variable/event/expression/game-state write is lost.

Impact area

  • Chat mode
  • Runtime systems: generation, world-state, visuals, tracker, or haptics
  • React-free engine behavior, contracts, agents, or capability ports

Environment

  • Branch or commit: refactor @ 1fae2c42
  • Run mode: Tauri desktop / web shell
  • OS + version: found via static source audit (not runtime-reproduced)
  • Node + pnpm versions: n/a (static audit)
  • Rust version, if relevant: n/a (static audit)
  • Remote Runtime URL configured? not relevant
  • Provider, model, or integration, if relevant: multi-agent / multi-connection setup

Logs, screenshots, or video

Static trace: tools-runtime.ts:223-233 (also inline in update_game_state ~365-367, append_chat_summary ~421-425), parallelism at agent-pipeline.ts:298-315, shared-input entry at agent-runner.ts:1005.

Regression notes

Regression vs main, which avoids this two ways at once: a per-chat serialization queue (withMetadataPatchQueue) plus fresh-read + partial-patch merge in chats.storage.ts patchMetadata. The refactor dropped both, regressing to a shared stale-snapshot full-blob overwrite.

Additional context

Fix: route metadata-mutating tools through a per-chat async queue, and/or have storage.update merge partial metadata keys (agentVariables/agentEvents/agentGameStateUpdates) against a fresh read instead of overwriting with a stale clone — i.e. mirror main's patchMetadata.

Template check

Please uncheck 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 @Romuromylus on GitHub (Jun 6, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2467 Originally assigned to: @SpicyMarinara on GitHub. ## Summary **Severity: Medium — concurrency regression.** When two tool-using agents run on different connections in the same turn, their stateful built-in tools read-modify-write the shared `chat.metadata` blob concurrently, so the second writer's stale snapshot overwrites the first writer's additions (lost update). ## Expected behavior Concurrent metadata mutations from parallel agents must not lose writes — either serialize metadata mutations per chat, or merge against a fresh read (main does both). ## Actual behavior Every stateful tool routes through `updateChatMetadata`, which reads the shared in-memory `input.chat.metadata`, awaits a full-blob `storage.update`, then reassigns `input.chat.metadata`: ```ts // src/engine/generation/tools-runtime.ts:223-233 const metadata = updater({ ...parseRecord(input.chat.metadata) }); await storage.update("chats", chatId, { metadata }); // whole-blob overwrite input.chat.metadata = metadata; ``` `agent-runner.ts` builds every agent's tool closures over the *same* `input`, and `agent-pipeline.ts` `executePhase` runs groups on different connections concurrently: ```ts // src/engine/agents-runtime/pipeline/agent-pipeline.ts:298-315 await Promise.all(Array.from(groupsByConnection.values()).map((connectionGroups) => mapWithConcurrency(...))); ``` Two agents on distinct connections each calling a metadata-mutating tool (`write_chat_variable`, `trigger_event`, `set_expression`, `update_game_state`, `append_chat_summary`) interleave read-A → read-B → write-A → write-B; B's update overwrites A's mutation in both the in-memory object and the persisted row. (Agents on the *same* connection are serialized in a `for...of await` loop, so they do not race.) ## Steps to reproduce 1. Enable chat tools and configure two agents on two different API connections in the same phase. 2. Have both call a stateful built-in tool (e.g. both `write_chat_variable` or `trigger_event`) during one turn. 3. One agent's variable/event/expression/game-state write is lost. ## Impact area - [x] Chat mode - [x] Runtime systems: generation, world-state, visuals, tracker, or haptics - [x] React-free engine behavior, contracts, agents, or capability ports ## Environment - Branch or commit: refactor @ 1fae2c42 - Run mode: Tauri desktop / web shell - OS + version: found via static source audit (not runtime-reproduced) - Node + pnpm versions: n/a (static audit) - Rust version, if relevant: n/a (static audit) - Remote Runtime URL configured? not relevant - Provider, model, or integration, if relevant: multi-agent / multi-connection setup ## Logs, screenshots, or video Static trace: `tools-runtime.ts:223-233` (also inline in `update_game_state` ~365-367, `append_chat_summary` ~421-425), parallelism at `agent-pipeline.ts:298-315`, shared-input entry at `agent-runner.ts:1005`. ## Regression notes Regression vs main, which avoids this two ways at once: a per-chat serialization queue (`withMetadataPatchQueue`) plus fresh-read + partial-patch merge in `chats.storage.ts` `patchMetadata`. The refactor dropped both, regressing to a shared stale-snapshot full-blob overwrite. ## Additional context Fix: route metadata-mutating tools through a per-chat async queue, and/or have `storage.update` merge partial metadata keys (`agentVariables`/`agentEvents`/`agentGameStateUpdates`) against a fresh read instead of overwriting with a stale clone — i.e. mirror main's `patchMetadata`. ## Template check Please uncheck 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.
bsakaguchi 2026-10-21 05:29:06 +07:00
Author
Owner

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

Fixed on refactor in 038b7220.

Tool-driven chat metadata writes are queued per chat and fresh-read before merge, preventing parallel agent lost-update races.

Verification: pnpm typecheck; pnpm build; pnpm check:architecture; cargo check --manifest-path src-tauri/Cargo.toml --workspace; focused Rust backup/profile tests; git diff --check; no .test.ts/.test.tsx files present.

<!-- gh-comment-id:4638721288 --> @SpicyMarinara commented on GitHub (Jun 6, 2026): Fixed on refactor in 038b7220. Tool-driven chat metadata writes are queued per chat and fresh-read before merge, preventing parallel agent lost-update races. Verification: pnpm typecheck; pnpm build; pnpm check:architecture; cargo check --manifest-path src-tauri/Cargo.toml --workspace; focused Rust backup/profile tests; git diff --check; no .test.ts/.test.tsx files present.
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#1209
No description provided.