feat(core): deterministic session log replay with synced watermark (#35040)
This commit is contained in:
parent
1aae92c42a
commit
57e9e9771d
16 changed files with 205 additions and 87 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { describe, expect } from "bun:test"
|
||||
import { Cause, DateTime, Deferred, Effect, Exit, Fiber, Layer, Option, Schema, Stream } from "effect"
|
||||
import { Cause, DateTime, Deferred, Effect, Exit, Fiber, Layer, Option, Ref, Schema, Stream } from "effect"
|
||||
import { EventV2 } from "@opencode-ai/core/event"
|
||||
import { Event } from "@opencode-ai/schema/event"
|
||||
import { Session } from "@opencode-ai/schema/session"
|
||||
|
|
@ -80,9 +80,7 @@ const durableData = (sessionID: Session.ID, text: string) => ({
|
|||
|
||||
/** Followed log read without markers: the old `durable` stream shape. */
|
||||
const tail = (events: EventV2.Interface, input: { aggregateID: string; after?: number }) =>
|
||||
events
|
||||
.log({ ...input, follow: true })
|
||||
.pipe(Stream.filter((item): item is EventV2.Payload => !EventV2.isCaughtUp(item)))
|
||||
events.log({ ...input, follow: true }).pipe(Stream.filter((item): item is EventV2.Payload => !EventV2.isSynced(item)))
|
||||
|
||||
const it = testEffect(
|
||||
AppNodeBuilder.build(LayerNode.group([Database.node, EventV2.node, Location.node]), [[Location.node, locationLayer]]),
|
||||
|
|
@ -1128,7 +1126,7 @@ describe("EventV2", () => {
|
|||
}),
|
||||
)
|
||||
|
||||
it.effect("log without follow replays events and completes with a caught-up marker", () =>
|
||||
it.effect("log without follow replays events and completes with a synced marker", () =>
|
||||
Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
const aggregateID = Session.ID.create()
|
||||
|
|
@ -1137,16 +1135,16 @@ describe("EventV2", () => {
|
|||
|
||||
const items = Array.from(yield* Stream.runCollect(events.log({ aggregateID })))
|
||||
|
||||
expect(items.map((item) => (EventV2.isCaughtUp(item) ? item.type : item.durable?.seq))).toEqual([
|
||||
expect(items.map((item) => (EventV2.isSynced(item) ? item.type : item.durable?.seq))).toEqual([
|
||||
EventV2.Seq.make(0),
|
||||
EventV2.Seq.make(1),
|
||||
"log.caught_up",
|
||||
"log.synced",
|
||||
])
|
||||
expect(items.at(-1)).toEqual({ type: "log.caught_up", aggregateID, seq: EventV2.Seq.make(1) })
|
||||
expect(items.at(-1)).toEqual({ type: "log.synced", aggregateID, seq: EventV2.Seq.make(1) })
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("log caught-up marker omits seq for an empty log and keeps the cursor otherwise", () =>
|
||||
it.effect("log synced marker omits seq for an empty log and keeps the cursor otherwise", () =>
|
||||
Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
const aggregateID = Session.ID.create()
|
||||
|
|
@ -1155,13 +1153,13 @@ describe("EventV2", () => {
|
|||
yield* events.publish(DurableMessage, durableData(aggregateID, "zero"))
|
||||
const drained = Array.from(yield* Stream.runCollect(events.log({ aggregateID, after: 0 })))
|
||||
|
||||
expect(empty).toEqual([{ type: "log.caught_up", aggregateID }])
|
||||
expect(empty).toEqual([{ type: "log.synced", aggregateID }])
|
||||
expect(empty[0]).not.toHaveProperty("seq")
|
||||
expect(drained).toEqual([{ type: "log.caught_up", aggregateID, seq: EventV2.Seq.make(0) }])
|
||||
expect(drained).toEqual([{ type: "log.synced", aggregateID, seq: EventV2.Seq.make(0) }])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("log with follow emits the caught-up marker at the replay-to-live boundary", () =>
|
||||
it.effect("log with follow emits the synced marker at the replay-to-live boundary", () =>
|
||||
Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
const aggregateID = Session.ID.create()
|
||||
|
|
@ -1174,14 +1172,79 @@ describe("EventV2", () => {
|
|||
yield* events.publish(DurableMessage, durableData(aggregateID, "one"))
|
||||
|
||||
const items = Array.from(yield* Fiber.join(fiber))
|
||||
expect(items.map((item) => (EventV2.isCaughtUp(item) ? item : item.durable?.seq))).toEqual([
|
||||
expect(items.map((item) => (EventV2.isSynced(item) ? item : item.durable?.seq))).toEqual([
|
||||
EventV2.Seq.make(0),
|
||||
{ type: "log.caught_up", aggregateID, seq: EventV2.Seq.make(0) },
|
||||
{ type: "log.synced", aggregateID, seq: EventV2.Seq.make(0) },
|
||||
EventV2.Seq.make(1),
|
||||
])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("log replays across configured read pages", () =>
|
||||
Effect.gen(function* () {
|
||||
const eventLayer = EventV2.layerWith({ logReadPageSize: 2 }).pipe(Layer.provide(LayerNode.compile(Database.node)))
|
||||
|
||||
yield* Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
const aggregateID = Session.ID.create()
|
||||
yield* events.publish(DurableMessage, durableData(aggregateID, "zero"))
|
||||
yield* events.publish(DurableMessage, durableData(aggregateID, "one"))
|
||||
yield* events.publish(DurableMessage, durableData(aggregateID, "two"))
|
||||
yield* events.publish(DurableMessage, durableData(aggregateID, "three"))
|
||||
yield* events.publish(DurableMessage, durableData(aggregateID, "four"))
|
||||
|
||||
const items = Array.from(yield* Stream.runCollect(events.log({ aggregateID })))
|
||||
|
||||
expect(items.map((item) => (EventV2.isSynced(item) ? item.type : item.durable?.seq))).toEqual([
|
||||
EventV2.Seq.make(0),
|
||||
EventV2.Seq.make(1),
|
||||
EventV2.Seq.make(2),
|
||||
EventV2.Seq.make(3),
|
||||
EventV2.Seq.make(4),
|
||||
"log.synced",
|
||||
])
|
||||
expect(items.at(-1)).toEqual({ type: "log.synced", aggregateID, seq: EventV2.Seq.make(4) })
|
||||
}).pipe(Effect.provide(Layer.merge(LayerNode.compile(Database.node), eventLayer)))
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("log with follow emits events committed during replay after the synced marker", () =>
|
||||
Effect.gen(function* () {
|
||||
const readStarted = yield* Deferred.make<void>()
|
||||
const releaseRead = yield* Deferred.make<void>()
|
||||
const firstRead = yield* Ref.make(true)
|
||||
const eventLayer = EventV2.layerWith({
|
||||
beforeAggregateRead: () =>
|
||||
Ref.getAndSet(firstRead, false).pipe(
|
||||
Effect.flatMap((shouldBlock) => {
|
||||
if (!shouldBlock) return Effect.void
|
||||
return Deferred.succeed(readStarted, undefined).pipe(Effect.andThen(Deferred.await(releaseRead)))
|
||||
}),
|
||||
),
|
||||
}).pipe(Layer.provide(LayerNode.compile(Database.node)))
|
||||
|
||||
yield* Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
const aggregateID = Session.ID.create()
|
||||
yield* events.publish(DurableMessage, durableData(aggregateID, "zero"))
|
||||
const fiber = yield* events
|
||||
.log({ aggregateID, follow: true })
|
||||
.pipe(Stream.take(3), Stream.runCollect, Effect.forkScoped)
|
||||
|
||||
yield* Deferred.await(readStarted)
|
||||
yield* events.publish(DurableMessage, durableData(aggregateID, "one"))
|
||||
yield* Deferred.succeed(releaseRead, undefined)
|
||||
|
||||
const items = Array.from(yield* Fiber.join(fiber))
|
||||
expect(items.map((item) => (EventV2.isSynced(item) ? item : item.durable?.seq))).toEqual([
|
||||
EventV2.Seq.make(0),
|
||||
{ type: "log.synced", aggregateID, seq: EventV2.Seq.make(0) },
|
||||
EventV2.Seq.make(1),
|
||||
])
|
||||
}).pipe(Effect.provide(Layer.merge(LayerNode.compile(Database.node), eventLayer)))
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("changes emits sweep-required on subscribe then coalesced hints per aggregate", () =>
|
||||
Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
|
|||
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
||||
import { EventV2 } from "@opencode-ai/core/event"
|
||||
import { EventTable } from "@opencode-ai/core/event/sql"
|
||||
import { Job } from "@opencode-ai/core/job"
|
||||
import { Location } from "@opencode-ai/core/location"
|
||||
import { ModelV2 } from "@opencode-ai/core/model"
|
||||
import { ProjectV2 } from "@opencode-ai/core/project"
|
||||
|
|
@ -49,11 +48,11 @@ const it = testEffect(
|
|||
const location = Location.Ref.make({ directory: AbsolutePath.make("/project") })
|
||||
const id = SessionV2.ID.create()
|
||||
|
||||
/** Public session events from a `log` read, without caught-up markers. */
|
||||
/** Public session events from a `log` read, without synced markers. */
|
||||
const logEvents = (session: SessionV2.Interface, sessionID: SessionV2.ID, follow?: boolean) =>
|
||||
session
|
||||
.log({ sessionID, follow })
|
||||
.pipe(Stream.filter((item): item is SessionEvent.DurableEvent => !EventV2.isCaughtUp(item)))
|
||||
.pipe(Stream.filter((item): item is SessionEvent.DurableEvent => !EventV2.isSynced(item)))
|
||||
|
||||
const assertCreateInputTypes = (session: SessionV2.Interface) => {
|
||||
// @ts-expect-error location or parentID is required.
|
||||
|
|
@ -213,7 +212,7 @@ describe("SessionV2.create", () => {
|
|||
durable: { seq: 0 },
|
||||
data: { sessionID: forked.id, parentID: parent.id },
|
||||
})
|
||||
expect(yield* SessionInput.find(db, forkContext[0]!.id)).toMatchObject({
|
||||
expect(yield* SessionInput.find(db, forkContext[0].id)).toMatchObject({
|
||||
sessionID: forked.id,
|
||||
prompt: { text: "First" },
|
||||
promotedSeq: 2,
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ const it = testEffect(
|
|||
const location = Location.Ref.make({ directory: AbsolutePath.make("/project") })
|
||||
|
||||
describe("SessionV2.log", () => {
|
||||
it.effect("replays public session events and marks caught-up at the aggregate watermark", () =>
|
||||
it.effect("replays public session events and marks synced at the aggregate watermark", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* SessionV2.Service
|
||||
const events = yield* EventV2.Service
|
||||
|
|
@ -47,8 +47,8 @@ describe("SessionV2.log", () => {
|
|||
|
||||
// Session creation commits a non-public durable event, so the marker's
|
||||
// seq covers more of the aggregate than the public events emitted.
|
||||
expect(items.map((item) => item.type)).toEqual(["session.next.renamed", "log.caught_up"])
|
||||
expect(items.at(-1)).toEqual({ type: "log.caught_up", aggregateID: created.id, seq: watermark })
|
||||
expect(items.map((item) => item.type)).toEqual(["session.next.renamed", "log.synced"])
|
||||
expect(items.at(-1)).toEqual({ type: "log.synced", aggregateID: created.id, seq: watermark })
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
@ -64,7 +64,7 @@ describe("SessionV2.log", () => {
|
|||
yield* session.rename({ sessionID: created.id, title: "renamed live" })
|
||||
|
||||
const items = Array.from(yield* Fiber.join(fiber))
|
||||
expect(items.map((item) => item.type)).toEqual(["log.caught_up", "session.next.renamed"])
|
||||
expect(items.map((item) => item.type)).toEqual(["log.synced", "session.next.renamed"])
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
@ -95,13 +95,13 @@ describe("SessionV2.log", () => {
|
|||
const items = Array.from(yield* Stream.runCollect(session.log({ sessionID: created.id, after: 1 })))
|
||||
|
||||
expect(
|
||||
items.map((item): number | string | undefined => (EventV2.isCaughtUp(item) ? item.type : item.durable?.seq)),
|
||||
).toEqual([3, 4, "log.caught_up"])
|
||||
expect(items.at(-1)).toEqual({ type: "log.caught_up", aggregateID: created.id, seq: EventV2.Seq.make(4) })
|
||||
items.map((item): number | string | undefined => (EventV2.isSynced(item) ? item.type : item.durable?.seq)),
|
||||
).toEqual([3, 4, "log.synced"])
|
||||
expect(items.at(-1)).toEqual({ type: "log.synced", aggregateID: created.id, seq: EventV2.Seq.make(4) })
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("completes with a bare caught-up marker for a migrated Session with no event sequence", () =>
|
||||
it.effect("completes with a bare synced marker for a migrated Session with no event sequence", () =>
|
||||
Effect.gen(function* () {
|
||||
const db = (yield* Database.Service).db
|
||||
const session = yield* SessionV2.Service
|
||||
|
|
@ -125,7 +125,7 @@ describe("SessionV2.log", () => {
|
|||
|
||||
const items = Array.from(yield* Stream.runCollect(session.log({ sessionID })))
|
||||
|
||||
expect(items).toEqual([{ type: "log.caught_up", aggregateID: sessionID }])
|
||||
expect(items).toEqual([{ type: "log.synced", aggregateID: sessionID }])
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
|
|||
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
||||
import { EventV2 } from "@opencode-ai/core/event"
|
||||
import { EventTable } from "@opencode-ai/core/event/sql"
|
||||
import { Job } from "@opencode-ai/core/job"
|
||||
import { SessionEvent } from "@opencode-ai/core/session/event"
|
||||
import { ModelV2 } from "@opencode-ai/core/model"
|
||||
import { ProviderV2 } from "@opencode-ai/core/provider"
|
||||
|
|
@ -248,7 +247,7 @@ describe("SessionV2.prompt", () => {
|
|||
const publicEvents = (input: { sessionID: SessionV2.ID; after?: number }) =>
|
||||
session
|
||||
.log({ ...input, follow: true })
|
||||
.pipe(Stream.filter((item): item is SessionEvent.DurableEvent => !EventV2.isCaughtUp(item)))
|
||||
.pipe(Stream.filter((item): item is SessionEvent.DurableEvent => !EventV2.isSynced(item)))
|
||||
const fiber = yield* publicEvents({ sessionID }).pipe(Stream.take(4), Stream.runCollect, Effect.forkScoped)
|
||||
yield* Effect.yieldNow
|
||||
|
||||
|
|
@ -265,7 +264,7 @@ describe("SessionV2.prompt", () => {
|
|||
])
|
||||
expect(
|
||||
Array.from(
|
||||
yield* publicEvents({ sessionID, after: streamed[0]!.durable?.seq }).pipe(Stream.take(1), Stream.runCollect),
|
||||
yield* publicEvents({ sessionID, after: streamed[0].durable?.seq }).pipe(Stream.take(1), Stream.runCollect),
|
||||
).map((event): [number | undefined, string] => [event.durable?.seq, event.type]),
|
||||
).toEqual([[1, "session.next.prompt.admitted"]])
|
||||
}),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue