Skip to content

Commit d96c284

Browse files
committed
fix(core): keep message seq as inbox cutoff for promoted revert boundaries
The commit projection recovered the inbox cutoff from the durable event log, which is only written when event persistence is on. The default local server leaves it off, so the lookup found nothing there and the wider cutoff only applied on some deployments. Keep the message seq as the inbox cutoff and drop the two tests that covered the promoted boundary cutoff, per opencodejr review.
1 parent 38d6df4 commit d96c284

2 files changed

Lines changed: 8 additions & 105 deletions

File tree

‎packages/core/src/session/projector.ts‎

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ import path from "path"
77
import { Config } from "../config.js"
88
import { Database } from "../database/database.js"
99
import { DatabaseBatch } from "../database/batch.js"
10-
import { EventTable } from "../event/sql.js"
1110
import { Bus } from "../bus.js"
1211
import { makeGlobalNode } from "@opencode/util/effect/app-node"
1312
import { Model } from "@opencode/schema/model"
14-
import { Event } from "@opencode/schema/event"
1513
import { SessionEvent } from "./event.js"
1614
import { SessionMessage } from "./message.js"
1715
import { SessionMessageUpdater } from "./message-updater.js"
@@ -1141,31 +1139,13 @@ const layer = Layer.effectDiscard(
11411139
.get()
11421140
.pipe(Effect.orDie)
11431141
if (message) {
1144-
// The message seq is the message-history cutoff. The inbox cutoff
1145-
// is the boundary's original enqueue seq, recovered from the
1146-
// durable event log: an inbox item enqueued after the boundary but
1147-
// before its delivery still belongs to the reverted suffix. Without
1148-
// an enqueue record (e.g. a forked message) fall back to the
1149-
// message seq, the previous behavior.
1150-
const admitted = yield* db
1151-
.select({ seq: EventTable.seq })
1152-
.from(EventTable)
1153-
.where(
1154-
and(
1155-
eq(EventTable.aggregate_id, event.data.sessionID),
1156-
eq(
1157-
EventTable.type,
1158-
Event.versionedType(
1159-
SessionEvent.InboxEnqueued.type,
1160-
SessionEvent.InboxEnqueued.durable.version,
1161-
),
1162-
),
1163-
sql`json_extract(${EventTable.data}, '$.inboxID') = ${event.data.to}`,
1164-
),
1165-
)
1166-
.get()
1167-
.pipe(Effect.orDie)
1168-
const inboxCutoff = admitted?.seq ?? message.seq
1142+
// The message seq is both the message-history cutoff and the inbox
1143+
// cutoff. An inbox item enqueued after the boundary but before its
1144+
// delivery is not dropped: its enqueue seq is not recoverable from
1145+
// projected state (the durable event log is only written when event
1146+
// persistence is on), so a wider cutoff would only apply on some
1147+
// deployments. If that cutoff is wanted, record the enqueue seq on
1148+
// projected state and land it in a separate PR.
11691149
yield* db
11701150
.delete(SessionMessageTable)
11711151
.where(
@@ -1178,7 +1158,7 @@ const layer = Layer.effectDiscard(
11781158
.where(
11791159
and(
11801160
eq(SessionInboxTable.session_id, event.data.sessionID),
1181-
gte(SessionInboxTable.enqueued_seq, inboxCutoff),
1161+
gte(SessionInboxTable.enqueued_seq, message.seq),
11821162
),
11831163
)
11841164
.run()

‎packages/core/test/session-revert.test.ts‎

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -113,83 +113,6 @@ describe("Session.revert admitted boundaries", () => {
113113
}),
114114
)
115115

116-
it.live("commits a revert staged while unpromoted after the boundary is promoted", () =>
117-
Effect.gen(function* () {
118-
const tmp = yield* tmpdirScoped()
119-
const { session, created } = yield* setupSession(tmp)
120-
const database = yield* Database.Service
121-
const bus = yield* Bus.Service
122-
123-
const boundary = yield* session.prompt({ sessionID: created.id, text: "Undo me", resume: false })
124-
const later = yield* session.prompt({
125-
sessionID: created.id,
126-
text: "Drop me",
127-
resume: false,
128-
delivery: "queue",
129-
})
130-
yield* session.revert.stage({ sessionID: created.id, messageID: boundary.id })
131-
yield* SessionInbox.promote(database.db, bus, created.id, "steer")
132-
133-
expect((yield* session.messages({ sessionID: created.id })).map((message) => message.id)).toContain(
134-
boundary.id,
135-
)
136-
expect(yield* admitted(later.id)).toMatchObject({ id: later.id })
137-
138-
yield* session.revert.commit(created.id)
139-
140-
expect((yield* session.get(created.id)).revert).toBeUndefined()
141-
expect((yield* session.messages({ sessionID: created.id })).map((message) => message.id)).not.toContain(
142-
boundary.id,
143-
)
144-
// Enqueued after the boundary but before its delivery, so it belongs to the reverted suffix.
145-
expect(yield* admitted(later.id)).toBeUndefined()
146-
}),
147-
)
148-
149-
it.live("keeps materialized assistant messages when committing an unpromoted boundary", () =>
150-
Effect.gen(function* () {
151-
const tmp = yield* tmpdirScoped()
152-
const { session, created } = yield* setupSession(tmp)
153-
const bus = yield* Bus.Service
154-
155-
const boundary = yield* session.prompt({ sessionID: created.id, text: "Undo me", resume: false })
156-
const assistantMessageID = SessionMessage.ID.create()
157-
yield* Effect.gen(function* () {
158-
const snapshot = yield* Snapshot.Service
159-
const before = yield* snapshot.capture()
160-
if (!before) throw new Error("Initial snapshot missing")
161-
yield* bus.publish(SessionEvent.Step.Started, {
162-
sessionID: created.id,
163-
assistantMessageID,
164-
agent: Agent.defaultID,
165-
model: { id: Model.ID.make("test-model"), providerID: Provider.ID.make("test-provider") },
166-
snapshot: before,
167-
started: 0,
168-
})
169-
yield* bus.publish(SessionEvent.Step.Ended, {
170-
sessionID: created.id,
171-
assistantMessageID,
172-
finish: "stop",
173-
cost: Money.USD.zero,
174-
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
175-
snapshot: before,
176-
files: [],
177-
})
178-
}).pipe(Effect.provide(LocationServiceMap.Service.get(created.location)))
179-
expect((yield* session.messages({ sessionID: created.id })).map((message) => message.id)).toContain(
180-
assistantMessageID,
181-
)
182-
183-
yield* session.revert.stage({ sessionID: created.id, messageID: boundary.id })
184-
yield* session.revert.commit(created.id)
185-
186-
expect(yield* admitted(boundary.id)).toBeUndefined()
187-
expect((yield* session.messages({ sessionID: created.id })).map((message) => message.id)).toContain(
188-
assistantMessageID,
189-
)
190-
}),
191-
)
192-
193116
it.live("rejects a compaction inbox item as a revert boundary", () =>
194117
Effect.gen(function* () {
195118
const tmp = yield* tmpdirScoped()

0 commit comments

Comments
 (0)