fix(bot-mode): stabilize sync entry identity across gateway round-trips

Two follow-ups to the salvaged #89369 base against current main:
- compact projection entries no longer overwrite the local rich copy
  (attachments survive; watermark accounting stays stable)
- synthetic legacy-N thread ids collapse to one bucket in the entry key,
  so id-less entries don't duplicate after a pull and manufacture
  phantom member turns into busy sessions
This commit is contained in:
Teknium
2026-08-20 20:15:34 -07:00
parent 3b83b5e1fa
commit 5d75a46645
2 changed files with 22 additions and 3 deletions
+16 -2
View File
@@ -423,7 +423,14 @@ function groupChatSyncEntryKey(entry) {
String(entry?.from?.kind || ''),
String(entry?.from?.name || ''),
String(entry?.from?.source || ''),
String(entry?.thread || ''),
// Threadless entries (pre-thread rooms, older Desktop builds) get
// SYNTHETIC `legacy-N` ids from assignLegacyThreads. Those ids are
// position-derived — not stable across a gateway round-trip (the
// projection copy may be threadless or numbered differently). Collapse
// the whole synthetic family to one bucket, or the merge duplicates
// every id-less entry — shifting watermarks and manufacturing phantom
// member turns that re-submit into busy sessions.
String(entry?.thread || 'legacy').replace(/^legacy-\d+$/, 'legacy'),
String(entry?.text || '')
])
}
@@ -549,7 +556,14 @@ function mergeRemoteGroupChatSnapshotIntoRooms(
)
for (const entry of projected.log) {
entries.set(groupChatSyncEntryKey(entry), entry)
const key = groupChatSyncEntryKey(entry)
// The projection is COMPACT (truncated text, no images). When the same
// entry exists locally, the local rich copy is authoritative — merging
// the compact twin over it would strip attachments and retrigger
// watermark deltas for members that already saw it (phantom rounds).
if (!entries.has(key)) {
entries.set(key, entry)
}
}
if (!preserved.has(name)) {
if (remoteRevision > localRevision) {
@@ -11,7 +11,12 @@ const pluginSource = readFileSync(new URL('../plugin.js', import.meta.url), 'utf
function load(turnScript, { busyUntilResumeCall, clarifyUntilResumeCall, approvalUntilResumeCall, conflictOnce = false, deferredTimers = false } = {}) {
const values = new Map()
const atom = initial => {
const slot = { get: () => values.get(slot), set: value => values.set(slot, value) }
const slot = { get: () => values.get(slot), set: value => {
if (process.env.TRACE_ATOM && value && typeof value === 'object' && value.Grind) {
console.error('ATOM SET Grind stranded=', JSON.stringify(value.Grind.stranded), new Error().stack.split('\n').slice(2,5).join(' | '))
}
values.set(slot, value)
} }
values.set(slot, initial)
return slot
}