hardening: mergeOverlay has no defense against malformed remote blocks
Found while writing v0.9.7 release adversarial tests (packages/opencode/test/skill/release-v0.9.7-adversarial.test.ts).
mergeOverlay (packages/opencode/src/memory/prompt.ts:69-80) has two narrow fragilities, both currently unreachable via the production hydrate/refresh path (which always supplies well-formed RemoteMemoryBlocks via toBlock()), but present in the exported function itself:
- Duplicate ids within the remote list are not deduplicated against each other. The dedup check only compares new remote entries against local keys (
localKeys.has(...)); it never checks a remote entry against remote entries already pushed from the same list. Two remote records sharing one non-sibling scope+id (a corrupted or duplicated overlay fetch) both survive into the merged output. - A remote block missing
updatedthrows a TypeError out of the final sort (b.updated.localeCompare(a.updated)) instead of degrading gracefully.toBlock()always guaranteesupdatedvia a fallback chain ending innew Date().toISOString(), so this isn't reachable today — but a future caller buildingRemoteMemoryBlockby hand, or a corrupted overlay cache, would hit it. The memory-read tool's outer try/catch happens to downgrade this to an error response rather than crashing the process, but that's accidental defense-in-depth one layer up, not a property ofmergeOverlayitself.
Both are pinned by FINDING:-prefixed tests in the adversarial test file (documenting current behavior, not asserting it's correct). Deferred from v0.9.7 because neither is reachable via the real production path today — worth a hardening pass regardless, since mergeOverlay is exported and its contract implicitly promises a 'deterministic, non-crashing merge' that doesn't fully hold for malformed input.
Good catch on both. One correction on the reachability framing for item 1 — I think it's stronger than "currently unreachable via the production hydrate/refresh path".
loadWorkspaceMemory builds the overlay with a bare blocks.push(block) after its filter chain (record.id present → isMirrorRecord → !isArchived → belongsHere → toBlock → not expired). There's no dedup by block_id anywhere in that loop. So the overlay faithfully reproduces whatever the record set contains — if two records share a block_id + scope, two remote blocks reach mergeOverlay, and item 1's gap is live.
The question is then whether two such records can exist server-side, and the write path's own guards suggest yes. mirrorBlock has:
- a bail-out on a truncated record set, with the comment that creating against a list known to be cut short "risks a duplicate: the record may exist just beyond the window"
- a bail-out when the record lookup fails, commented "a failed lookup must not read as 'no record exists' — that is how a duplicate gets created"
Those guards exist because the duplicate condition is real. They narrow the window rather than closing it — the second comment says as much about the newer-remote guard elsewhere ("this narrows the window rather than closing it; closing it needs a conditional update the service does not offer"). So the honest statement is probably: unreachable while the write path's duplicate guards hold, and reachable whenever one of them doesn't — which is a different risk class from "only a hand-built RemoteMemoryBlock could trigger this".
Doesn't change the priority much — it's still a hardening pass on an exported function, and a duplicate record set is an edge case. But it does mean the fix has a production-path justification rather than only a contract-hygiene one, which might matter when it's triaged.
Item 2 (missing updated throwing out of the sort) I have nothing to add on — toBlock's fallback chain does guarantee it today, so that one really is contract hygiene.