mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #3011] [Issue]: MAX_CHAT_VARIABLES cap is a TOCTOU soft-guard; concurrent agents bypass it #1582
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1582
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3011
Originally assigned to: @SpicyMarinara on GitHub.
What's wrong
writeChatVariableenforces the 256-variable limit by readingcontext.chatMeta(a shared, refreshed-after-commit reference) BEFORE entering the serialized per-chat metadata patch queue, and the actual write inside the queue's patcher never re-checks the limit. The patcher rebuildsagentVariablesfrom the FRESHcurrentMetabut unconditionally adds the new key. So the cap is advisory: it is checked against one state and committed against another. Tool-using agents run concurrently and all share the samechatMetareference. When the count is at 255, multiple concurrentwrite_chat_variablecalls for distinct NEW keys all read 255, all pass the< 256check, and all commit, pushing the persisted map pastMAX_CHAT_VARIABLES.How to reproduce (try it yourself)
write_chat_variable, seedagentVariablesto 255 entries.write_chat_variablefor two different brand-new keys in the same concurrent tool phase.You'll see: both calls read
chatMetashowing 255 (the< 256check passes for both), both enter the patch queue, and the patcher (which does not re-check the count) writes each key. Persistedmetadata.agentVariablesnow contains 257 entries, exceedingMAX_CHAT_VARIABLES.Expected: the cap holds under concurrency; the 257th distinct key is rejected with the established
{ error: "chat variable limit reached (256)" }.Where it is
packages/server/src/services/tools/tool-executor.ts:567-577(writeChatVariable): the out-of-queue pre-check at 567-571 and the read-modify-write patcher at 574-577 that omits the limit check. The patcher runs insidewithChatMetadataPatchQueue(packages/server/src/services/storage/chats.storage.ts:467-489).What's happening
The existence/limit pre-check reads
context.chatMeta?.agentVariablesoutside thewithChatMetadataPatchQueuecritical section, while the authoritative read-modify-write reads freshcurrentMetabut omits the limit check. Check-then-act is split across two snapshots with no re-validation inside the serialized section; the sharedchatMetais only refreshed after each completed write, so concurrent agents at count 255 all read 255 before any commit lands.Suggested fix
Make the cap authoritative inside the serialized patcher and surface the rejection via a closure flag so the tool still returns the established
{ error }object. Replace the body from line 567:This removes the stale out-of-queue pre-check entirely; the count, existence check, and cap are now evaluated against the FRESH
currentMetainsidewithChatMetadataPatchQueue.Regression avoided: the original suggestion ("return the unchanged agentVariables and surface an error") would, taken literally, fall through to the success return and report
success:truefor a rejected write — a false-success that misleads the model. Throwing instead would convert tosuccess:falsewith a bare message string, breaking the structured{ error }contract every other tool failure here uses. The closure-flag approach returns{}(a true no-op patch) and then branches to the same{ error }object the original returned, and derivesreplacedfrom fresh state.AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.
@SpicyMarinara commented on GitHub (Jun 29, 2026):
Fixed in merged staging PR #3018.