Skip to content

Commit 2c4742b

Browse files
committed
fix(tui,opencode): complete deletion cleanup, snapshot isolation, linear bursts
Oracle verification round: session.deleted now clears todo/permission/ question buckets, orphaned parts, and eviction bookkeeping, with a bounded tombstone so late events and in-flight syncs cannot resurrect payloads (upstream anomalyco#12351). message.removed drops its part bucket and pending deltas. Shell tool state updates publish immutable replacements so shallow-copied PartUpdated events cannot be retro-edited; plugins receive isolated event copies. Delta buffer joins chunks once per flush.
1 parent c8d4b2c commit 2c4742b

8 files changed

Lines changed: 280 additions & 32 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ is fork-owned files):
143143
**Backlog** (unclaimed, no upstream equivalent): LSP files LRU
144144
(`packages/opencode/src/lsp/client.ts` store is set-never-deleted);
145145
BackgroundJob settled-entry eviction (`packages/core/src/background-job.ts`);
146-
per-session Effect EventTarget listener leak (upstream #29204, no fix PR);
146+
run-UI delta coalescing (`packages/opencode/src/cli/cmd/run/session-data.ts`
147+
still concatenates and flushes per delta — changing it means changing footer
148+
render cadence, so it needs its own behavior-preserving round); serve-mode SSE
149+
event queue is unbounded (`handlers/event.ts`) — backpressure policy needed;
147150
log rotation cap; `tool-output/` retention policy; `PRAGMA auto_vacuum`.
148151

149152
## Credit

‎packages/opencode/src/plugin/index.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,12 @@ const layer = Layer.effect(
255255
const unsubscribe = yield* events.listen((event) => {
256256
if (event.location?.directory !== ctx.directory) return Effect.void
257257
return Effect.sync(() => {
258+
// Fork(lowmem): plugins get an isolated copy — plugin code must not
259+
// be able to mutate shared event payloads (heavy string fields are
260+
// shared by structuredClone, so this stays cheap).
261+
const properties = structuredClone(event.data) as any
258262
for (const hook of hooks) {
259-
void hook["event"]?.({ event: { id: event.id, type: event.type, properties: event.data } as any })
263+
void hook["event"]?.({ event: { id: event.id, type: event.type, properties } as any })
260264
}
261265
})
262266
})

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,10 @@ const layer = Layer.effect(
568568
Effect.gen(function* () {
569569
output += chunk
570570
if (part.state.status === "running") {
571-
part.state.metadata = { output }
571+
// Fork(lowmem): reassign, never mutate — updatePart publishes
572+
// shallow copies, so in-place state edits would retro-edit
573+
// already-queued message.part.updated snapshots.
574+
part.state = { ...part.state, metadata: { output } }
572575
yield* sessions.updatePart(part)
573576
}
574577
}),

‎packages/tui/src/context/part-delta-buffer.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,40 @@
44
// writing once per interval keeps reactive updates and allocations linear.
55
export type PartDeltaEntry = { messageID: string; partID: string; field: string; accumulated: string }
66

7+
type PendingEntry = { messageID: string; partID: string; field: string; chunks: string[] }
8+
79
export function createPartDeltaBuffer(options: { intervalMs?: number; apply: (entry: PartDeltaEntry) => void }) {
810
const intervalMs = options.intervalMs ?? 120
9-
const pending = new Map<string, PartDeltaEntry>()
11+
const pending = new Map<string, PendingEntry>()
1012
let timer: ReturnType<typeof setTimeout> | undefined
1113

1214
function flush() {
1315
timer = undefined
1416
const entries = [...pending.values()]
1517
pending.clear()
16-
for (const entry of entries) options.apply(entry)
18+
for (const entry of entries) {
19+
// Join once per flush: chunk arrays keep bursts linear; += per push would be quadratic.
20+
options.apply({
21+
messageID: entry.messageID,
22+
partID: entry.partID,
23+
field: entry.field,
24+
accumulated: entry.chunks.join(""),
25+
})
26+
}
1727
}
1828

1929
return {
2030
push(input: { messageID: string; partID: string; field: string; delta: string }) {
2131
const key = `${input.messageID}:${input.partID}:${input.field}`
2232
const existing = pending.get(key)
23-
if (existing) existing.accumulated += input.delta
24-
else pending.set(key, { ...input, accumulated: input.delta })
33+
if (existing) existing.chunks.push(input.delta)
34+
else
35+
pending.set(key, {
36+
messageID: input.messageID,
37+
partID: input.partID,
38+
field: input.field,
39+
chunks: [input.delta],
40+
})
2541
if (timer === undefined) timer = setTimeout(flush, intervalMs)
2642
},
2743
dropMessage(messageID: string) {

‎packages/tui/src/context/payload-eviction.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,43 @@ export function createPayloadEviction<T extends EvictableStore>(
113113
evictSessionPayload(sessionID)
114114
}
115115

116-
return { activate, viewed, isEvicted, compact, remarkEvicted }
116+
const deleted = new Set<string>()
117+
const deletionOrder: string[] = []
118+
const DELETED_TOMBSTONE_LIMIT = 1024
119+
120+
function isDeleted(sessionID: string) {
121+
return deleted.has(sessionID)
122+
}
123+
124+
// Fork(lowmem): full cleanup on upstream session.deleted. Unlike eviction,
125+
// permission/question buckets die too, bookkeeping is dropped, and the ID is
126+
// tombstoned so in-flight syncs and late events cannot resurrect payloads.
127+
function forgetSession(sessionID: string) {
128+
if (!deleted.has(sessionID)) {
129+
deleted.add(sessionID)
130+
deletionOrder.push(sessionID)
131+
if (deletionOrder.length > DELETED_TOMBSTONE_LIMIT) deleted.delete(deletionOrder.shift()!)
132+
}
133+
evicted.delete(sessionID)
134+
const previous = viewClock.lastIndexOf(sessionID)
135+
if (previous !== -1) viewClock.splice(previous, 1)
136+
deps.fullSyncedSessions.delete(sessionID)
137+
const orphaned = Object.entries(store.part)
138+
.filter(([, parts]) => parts.some((part) => part.sessionID === sessionID))
139+
.map(([messageID]) => messageID)
140+
setStore(
141+
produce((draft: T) => {
142+
for (const message of store.message[sessionID] ?? []) delete draft.part[message.id]
143+
for (const messageID of orphaned) delete draft.part[messageID]
144+
delete draft.message[sessionID]
145+
delete draft.todo[sessionID]
146+
delete draft.session_diff[sessionID]
147+
delete draft.session_status[sessionID]
148+
delete draft.permission[sessionID]
149+
delete draft.question[sessionID]
150+
}),
151+
)
152+
}
153+
154+
return { activate, viewed, isEvicted, compact, remarkEvicted, forgetSession, isDeleted }
117155
}

‎packages/tui/src/context/sync.tsx‎

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,21 @@ export const {
205205
.then((x) => (x.data ?? []).toSorted((a, b) => a.id.localeCompare(b.id)))
206206
}
207207

208+
// Fork(lowmem): narrow the union payload without casting the event itself;
209+
// late events for a deleted session must not resurrect its buckets.
210+
const tombstoneSessionID = (properties: unknown): string | undefined => {
211+
if (typeof properties !== "object" || properties === null) return undefined
212+
const record = properties as Record<string, unknown>
213+
if (typeof record.sessionID === "string") return record.sessionID
214+
const info = record.info
215+
if (typeof info === "object" && info !== null && typeof (info as Record<string, unknown>).id === "string") {
216+
return (info as Record<string, unknown>).id as string
217+
}
218+
return undefined
219+
}
208220
event.subscribe((event, { directory, workspace }) => {
221+
const tombstoneID = tombstoneSessionID(event.properties)
222+
if (tombstoneID !== undefined && payloadEviction.isDeleted(tombstoneID)) return
209223
switch (event.type) {
210224
case "server.instance.disposed":
211225
void bootstrap()
@@ -308,19 +322,15 @@ export const {
308322

309323
case "session.deleted": {
310324
forgetInboundChild(event.properties.info.id)
311-
// Fork(lowmem): drop payload buckets too, or deleted sessions leak
312-
// in the store (upstream #12351).
313-
for (const msg of store.message[event.properties.info.id] ?? []) {
314-
partDeltaBuffer.dropMessage(msg.id)
325+
// Fork(lowmem): full cleanup — payload and bookkeeping buckets die
326+
// with the session, and the ID is tombstoned so late events and
327+
// in-flight syncs cannot resurrect them (upstream #12351).
328+
for (const msg of store.message[event.properties.info.id] ?? []) partDeltaBuffer.dropMessage(msg.id)
329+
for (const [messageID, parts] of Object.entries(store.part)) {
330+
if (parts.some((part) => part.sessionID === event.properties.info.id))
331+
partDeltaBuffer.dropMessage(messageID)
315332
}
316-
setStore(
317-
produce((draft) => {
318-
for (const msg of draft.message[event.properties.info.id] ?? []) delete draft.part[msg.id]
319-
delete draft.message[event.properties.info.id]
320-
delete draft.session_diff[event.properties.info.id]
321-
delete draft.session_status[event.properties.info.id]
322-
}),
323-
)
333+
payloadEviction.forgetSession(event.properties.info.id)
324334
const result = search(store.session, event.properties.info.id, (s) => s.id)
325335
if (result.found) {
326336
setStore(
@@ -433,6 +443,14 @@ export const {
433443
case "message.removed": {
434444
if (payloadEviction.isEvicted(event.properties.sessionID)) break
435445
touchMessage(event.properties.sessionID, event.properties.messageID)
446+
// Fork(lowmem): the part bucket and pending deltas die with the
447+
// message, or removal orphans them forever.
448+
partDeltaBuffer.dropMessage(event.properties.messageID)
449+
setStore(
450+
produce((draft) => {
451+
delete draft.part[event.properties.messageID]
452+
}),
453+
)
436454
const messages = store.message[event.properties.sessionID]
437455
if (!messages) break
438456
const index = messages.findIndex((message) => message.id === event.properties.messageID)
@@ -672,6 +690,9 @@ export const {
672690
return last.time.completed ? "idle" : "working"
673691
},
674692
async sync(sessionID: string) {
693+
// Fork(lowmem): deleted sessions stay deleted; viewed() would re-arm
694+
// the recency clock for a session whose payloads must not return.
695+
if (payloadEviction.isDeleted(sessionID)) return
675696
payloadEviction.viewed(sessionID)
676697
if (fullSyncedSessions.has(sessionID)) return
677698
const syncing = syncingSessions.get(sessionID)
@@ -685,6 +706,9 @@ export const {
685706
sdk.client.session.todo({ sessionID }),
686707
sdk.client.session.diff({ sessionID }),
687708
])
709+
// Fork(lowmem): the session may have been deleted while fetching;
710+
// writing would resurrect its payloads.
711+
if (payloadEviction.isDeleted(sessionID)) return
688712
setStore(
689713
produce((draft) => {
690714
const match = search(draft.session, sessionID, (s) => s.id)
@@ -744,7 +768,9 @@ export const {
744768
hydratingSessions.delete(sessionID)
745769
// Fork(lowmem): re-arm after tracker cleanup (protection checks the
746770
// tracker maps), so a failed hydration gates late events again.
747-
if (store.message[sessionID] === undefined) payloadEviction.remarkEvicted(sessionID)
771+
if (!payloadEviction.isDeleted(sessionID) && store.message[sessionID] === undefined) {
772+
payloadEviction.remarkEvicted(sessionID)
773+
}
748774
payloadEviction.compact()
749775
})
750776
syncingSessions.set(sessionID, task)

0 commit comments

Comments
 (0)