feat(server): durable log reads, changes feed, and watermarked snapshots (#34962)
This commit is contained in:
parent
33705e632a
commit
bc2e270f82
28 changed files with 1402 additions and 1605 deletions
|
|
@ -78,6 +78,12 @@ const durableData = (sessionID: Session.ID, text: string) => ({
|
|||
messageID: SessionV1.MessageID.ascending(`msg_${text}`),
|
||||
})
|
||||
|
||||
/** 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)))
|
||||
|
||||
const it = testEffect(
|
||||
AppNodeBuilder.build(LayerNode.group([Database.node, EventV2.node, Location.node]), [[Location.node, locationLayer]]),
|
||||
)
|
||||
|
|
@ -119,7 +125,7 @@ describe("EventV2", () => {
|
|||
const event = yield* events.publish(VersionedMessage, { id: "one", text: "hello" })
|
||||
|
||||
expect(event.type).toBe("test.versioned")
|
||||
expect(event.durable?.version).toBe(2)
|
||||
expect(event.durable?.version).toBe(EventV2.Version.make(2))
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
@ -145,7 +151,7 @@ describe("EventV2", () => {
|
|||
Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
const typed = yield* events.subscribe(Message).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
|
||||
const wildcard = yield* events.all().pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
|
||||
const wildcard = yield* events.live().pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
|
||||
yield* Effect.yieldNow
|
||||
const event = yield* events.publish(Message, { text: "hello" })
|
||||
|
||||
|
|
@ -226,7 +232,7 @@ describe("EventV2", () => {
|
|||
Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
const received = new Array<string>()
|
||||
const fiber = yield* events.all().pipe(
|
||||
const fiber = yield* events.live().pipe(
|
||||
Stream.take(1),
|
||||
Stream.runForEach(() => Effect.sync(() => received.push("stream"))),
|
||||
Effect.forkScoped,
|
||||
|
|
@ -325,8 +331,8 @@ describe("EventV2", () => {
|
|||
const events = yield* EventV2.Service
|
||||
const consuming = yield* Deferred.make<void>()
|
||||
const release = yield* Deferred.make<void>()
|
||||
const slowStream = yield* EventV2.allBounded(events, 1)
|
||||
const fastStream = yield* EventV2.allBounded(events, 8)
|
||||
const slowStream = yield* EventV2.liveBounded(events, 1)
|
||||
const fastStream = yield* EventV2.liveBounded(events, 8)
|
||||
const slow = yield* slowStream.pipe(
|
||||
Stream.runForEach(() => Deferred.succeed(consuming, undefined).pipe(Effect.andThen(Deferred.await(release)))),
|
||||
Effect.forkScoped,
|
||||
|
|
@ -425,9 +431,11 @@ describe("EventV2", () => {
|
|||
const aggregateID = Session.ID.create()
|
||||
yield* events.publish(DurableMessage, durableData(aggregateID, "zero"))
|
||||
yield* events.publish(DurableMessage, durableData(aggregateID, "one"))
|
||||
const fiber = yield* events
|
||||
.durable({ aggregateID, after: 0 })
|
||||
.pipe(Stream.take(2), Stream.runCollect, Effect.forkScoped)
|
||||
const fiber = yield* tail(events, { aggregateID, after: 0 }).pipe(
|
||||
Stream.take(2),
|
||||
Stream.runCollect,
|
||||
Effect.forkScoped,
|
||||
)
|
||||
yield* Effect.yieldNow
|
||||
|
||||
yield* events.publish(DurableMessage, durableData(aggregateID, "two"))
|
||||
|
|
@ -444,7 +452,7 @@ describe("EventV2", () => {
|
|||
const events = yield* EventV2.Service
|
||||
const aggregateID = Session.ID.create()
|
||||
yield* events.publish(DurableMessage, durableData(aggregateID, "zero"))
|
||||
const fiber = yield* events.durable({ aggregateID }).pipe(Stream.take(2), Stream.runCollect, Effect.forkScoped)
|
||||
const fiber = yield* tail(events, { aggregateID }).pipe(Stream.take(2), Stream.runCollect, Effect.forkScoped)
|
||||
|
||||
yield* events.publish(DurableMessage, durableData(aggregateID, "one"))
|
||||
|
||||
|
|
@ -470,7 +478,7 @@ describe("EventV2", () => {
|
|||
yield* Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
const aggregateID = Session.ID.create()
|
||||
const fiber = yield* events.durable({ aggregateID }).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
|
||||
const fiber = yield* tail(events, { aggregateID }).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
|
||||
yield* Deferred.await(readStarted)
|
||||
|
||||
pause = false
|
||||
|
|
@ -489,9 +497,7 @@ describe("EventV2", () => {
|
|||
const events = yield* EventV2.Service
|
||||
const aggregateID = Session.ID.create()
|
||||
const count = 64
|
||||
const fiber = yield* events
|
||||
.durable({ aggregateID })
|
||||
.pipe(Stream.take(count), Stream.runCollect, Effect.forkScoped)
|
||||
const fiber = yield* tail(events, { aggregateID }).pipe(Stream.take(count), Stream.runCollect, Effect.forkScoped)
|
||||
yield* Effect.yieldNow
|
||||
|
||||
for (let index = 0; index < count; index++) {
|
||||
|
|
@ -508,7 +514,7 @@ describe("EventV2", () => {
|
|||
Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
const aggregateID = Session.ID.create()
|
||||
const fiber = yield* events.durable({ aggregateID }).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
|
||||
const fiber = yield* tail(events, { aggregateID }).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
|
||||
yield* Effect.yieldNow
|
||||
|
||||
yield* events.publish(Message, { text: "live only" })
|
||||
|
|
@ -1121,4 +1127,125 @@ describe("EventV2", () => {
|
|||
expect(received[0]?.data).toEqual(durableData(aggregateID, "replayed"))
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("log without follow replays events and completes with a caught-up marker", () =>
|
||||
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"))
|
||||
|
||||
const items = Array.from(yield* Stream.runCollect(events.log({ aggregateID })))
|
||||
|
||||
expect(items.map((item) => (EventV2.isCaughtUp(item) ? item.type : item.durable?.seq))).toEqual([
|
||||
EventV2.Seq.make(0),
|
||||
EventV2.Seq.make(1),
|
||||
"log.caught_up",
|
||||
])
|
||||
expect(items.at(-1)).toEqual({ type: "log.caught_up", aggregateID, seq: EventV2.Seq.make(1) })
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("log caught-up 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()
|
||||
|
||||
const empty = Array.from(yield* Stream.runCollect(events.log({ aggregateID })))
|
||||
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[0]).not.toHaveProperty("seq")
|
||||
expect(drained).toEqual([{ type: "log.caught_up", aggregateID, seq: EventV2.Seq.make(0) }])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("log with follow emits the caught-up marker at the replay-to-live boundary", () =>
|
||||
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* Effect.yieldNow
|
||||
|
||||
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([
|
||||
EventV2.Seq.make(0),
|
||||
{ type: "log.caught_up", aggregateID, seq: EventV2.Seq.make(0) },
|
||||
EventV2.Seq.make(1),
|
||||
])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("changes emits sweep-required on subscribe then coalesced hints per aggregate", () =>
|
||||
Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
const first = Session.ID.create()
|
||||
const second = Session.ID.create()
|
||||
const pull = yield* Stream.toPull(events.changes())
|
||||
|
||||
expect(Array.from(yield* pull)).toEqual([{ type: "log.sweep_required" }])
|
||||
|
||||
yield* events.publish(DurableMessage, durableData(first, "zero"))
|
||||
yield* events.publish(DurableMessage, durableData(first, "one"))
|
||||
yield* events.publish(DurableMessage, durableData(first, "two"))
|
||||
yield* events.publish(DurableMessage, durableData(second, "zero"))
|
||||
|
||||
expect(Array.from(yield* pull)).toEqual([
|
||||
{ type: "log.hint", aggregateID: first, seq: EventV2.Seq.make(2) },
|
||||
{ type: "log.hint", aggregateID: second, seq: EventV2.Seq.make(0) },
|
||||
])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("changes abandons the hint buffer for a sweep when key retention is exceeded", () =>
|
||||
Effect.gen(function* () {
|
||||
const eventLayer = EventV2.layerWith({ changesKeyCapacity: 2 }).pipe(
|
||||
Layer.provide(LayerNode.compile(Database.node)),
|
||||
)
|
||||
|
||||
yield* Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
const pull = yield* Stream.toPull(events.changes())
|
||||
expect(Array.from(yield* pull)).toEqual([{ type: "log.sweep_required" }])
|
||||
|
||||
yield* events.publish(DurableMessage, durableData(Session.ID.create(), "a"))
|
||||
yield* events.publish(DurableMessage, durableData(Session.ID.create(), "b"))
|
||||
yield* events.publish(DurableMessage, durableData(Session.ID.create(), "c"))
|
||||
|
||||
expect(Array.from(yield* pull)).toEqual([{ type: "log.sweep_required" }])
|
||||
|
||||
const late = Session.ID.create()
|
||||
yield* events.publish(DurableMessage, durableData(late, "d"))
|
||||
|
||||
expect(Array.from(yield* pull)).toEqual([{ type: "log.hint", aggregateID: late, seq: EventV2.Seq.make(0) }])
|
||||
}).pipe(Effect.provide(Layer.merge(LayerNode.compile(Database.node), eventLayer)))
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("sequences returns the latest committed seq per aggregate and omits unknown aggregates", () =>
|
||||
Effect.gen(function* () {
|
||||
const events = yield* EventV2.Service
|
||||
const first = Session.ID.create()
|
||||
const second = Session.ID.create()
|
||||
yield* events.publish(DurableMessage, durableData(first, "zero"))
|
||||
yield* events.publish(DurableMessage, durableData(first, "one"))
|
||||
yield* events.publish(DurableMessage, durableData(second, "zero"))
|
||||
|
||||
const sequences = yield* events.sequences([first, second, Session.ID.create()])
|
||||
|
||||
expect(sequences).toEqual(
|
||||
new Map([
|
||||
[first, EventV2.Seq.make(1)],
|
||||
[second, EventV2.Seq.make(0)],
|
||||
]),
|
||||
)
|
||||
expect(yield* events.sequences([])).toEqual(new Map())
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -49,6 +49,12 @@ 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. */
|
||||
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)))
|
||||
|
||||
const assertCreateInputTypes = (session: SessionV2.Interface) => {
|
||||
// @ts-expect-error location or parentID is required.
|
||||
session.create({})
|
||||
|
|
@ -66,7 +72,7 @@ describe("SessionV2.create", () => {
|
|||
const second = yield* session.create({ location })
|
||||
|
||||
expect(second.id).not.toBe(first.id)
|
||||
expect(yield* session.list()).toHaveLength(2)
|
||||
expect((yield* session.list()).data).toHaveLength(2)
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
@ -79,7 +85,7 @@ describe("SessionV2.create", () => {
|
|||
const retried = yield* session.create(input)
|
||||
|
||||
expect(retried).toEqual(first)
|
||||
expect(yield* session.list()).toEqual([first])
|
||||
expect((yield* session.list()).data).toEqual([first])
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
@ -146,7 +152,7 @@ describe("SessionV2.create", () => {
|
|||
const forked = yield* session.fork({ sessionID: parent.id })
|
||||
const parentContext = yield* session.context(parent.id)
|
||||
const forkContext = yield* session.context(forked.id)
|
||||
const history = yield* session.history({ sessionID: forked.id, limit: 10 })
|
||||
const history = Array.from(yield* Stream.runCollect(logEvents(session, forked.id)))
|
||||
|
||||
expect(forked).toMatchObject({ parentID: parent.id, title: "Parent (fork #1)" })
|
||||
expect(forkContext).toMatchObject([
|
||||
|
|
@ -154,8 +160,8 @@ describe("SessionV2.create", () => {
|
|||
{ type: "synthetic", text: "parent note", sessionID: forked.id },
|
||||
])
|
||||
expect(forkContext.map((message) => message.id)).not.toEqual(parentContext.map((message) => message.id))
|
||||
expect(history.events).toHaveLength(1)
|
||||
expect(history.events[0]).toMatchObject({
|
||||
expect(history).toHaveLength(1)
|
||||
expect(history[0]).toMatchObject({
|
||||
type: "session.next.forked",
|
||||
durable: { seq: 0 },
|
||||
data: { sessionID: forked.id, parentID: parent.id },
|
||||
|
|
@ -175,7 +181,9 @@ describe("SessionV2.create", () => {
|
|||
expect((yield* session.context(forked.id)).map((message) => message.type)).toEqual(["user", "synthetic", "user"])
|
||||
expect((yield* session.context(forked.id)).at(-1)).toMatchObject({ text: "Child continues" })
|
||||
expect(
|
||||
(yield* session.history({ sessionID: forked.id, limit: 10 })).events.map((event) => event.durable?.seq),
|
||||
Array.from(yield* Stream.runCollect(logEvents(session, forked.id))).map(
|
||||
(event): number | undefined => event.durable?.seq,
|
||||
),
|
||||
).toEqual([0, 4, 5])
|
||||
expect(yield* SessionInput.find(db, admitted.id)).toMatchObject({ sessionID: parent.id })
|
||||
}),
|
||||
|
|
@ -203,10 +211,10 @@ describe("SessionV2.create", () => {
|
|||
const forked = yield* session.fork({ sessionID: parent.id, messageID: second.id })
|
||||
|
||||
const context = yield* session.context(forked.id)
|
||||
const history = yield* session.history({ sessionID: forked.id, limit: 10 })
|
||||
const history = Array.from(yield* Stream.runCollect(logEvents(session, forked.id)))
|
||||
expect(context).toMatchObject([{ text: "First" }])
|
||||
expect(context[0]?.id).not.toBe(first.id)
|
||||
expect(history.events[0]).toMatchObject({ data: { messageID: second.id } })
|
||||
expect(history[0]).toMatchObject({ data: { messageID: second.id } })
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
@ -227,7 +235,7 @@ describe("SessionV2.create", () => {
|
|||
for (const input of changed) {
|
||||
expect(yield* session.create(input)).toEqual(created)
|
||||
}
|
||||
expect(yield* session.list()).toHaveLength(1)
|
||||
expect((yield* session.list()).data).toHaveLength(1)
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
@ -239,7 +247,7 @@ describe("SessionV2.create", () => {
|
|||
const created = yield* Effect.all([session.create(input), session.create(input)], { concurrency: "unbounded" })
|
||||
|
||||
expect(created[1]).toEqual(created[0])
|
||||
expect(yield* session.list()).toEqual([created[0]])
|
||||
expect((yield* session.list()).data).toEqual([created[0]])
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
@ -317,7 +325,7 @@ describe("SessionV2.create", () => {
|
|||
yield* SessionInput.promoteSteers(db, events, created.id, Number.MAX_SAFE_INTEGER)
|
||||
|
||||
expect(
|
||||
Array.from(yield* session.events({ sessionID: created.id }).pipe(Stream.take(2), Stream.runCollect)),
|
||||
Array.from(yield* logEvents(session, created.id, true).pipe(Stream.take(2), Stream.runCollect)),
|
||||
).toMatchObject([
|
||||
{ durable: { seq: 1 }, type: "session.next.prompt.admitted", data: { prompt: { text: "Hello" } } },
|
||||
{ durable: { seq: 2 }, type: "session.next.prompted" },
|
||||
|
|
@ -447,7 +455,7 @@ describe("SessionV2.create", () => {
|
|||
|
||||
expect(yield* session.get(created.id)).toMatchObject({ agent: "plan" })
|
||||
expect(
|
||||
Array.from(yield* session.events({ sessionID: created.id }).pipe(Stream.take(1), Stream.runCollect)),
|
||||
Array.from(yield* logEvents(session, created.id, true).pipe(Stream.take(1), Stream.runCollect)),
|
||||
).toMatchObject([{ type: "session.next.agent.switched", data: { agent: "plan" } }])
|
||||
}),
|
||||
)
|
||||
|
|
@ -480,7 +488,7 @@ describe("SessionV2.create", () => {
|
|||
|
||||
expect(yield* session.get(created.id)).toMatchObject({ model })
|
||||
expect(
|
||||
Array.from(yield* session.events({ sessionID: created.id }).pipe(Stream.take(1), Stream.runCollect)),
|
||||
Array.from(yield* logEvents(session, created.id, true).pipe(Stream.take(1), Stream.runCollect)),
|
||||
).toMatchObject([{ type: "session.next.model.switched", data: { model } }])
|
||||
}),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,166 +0,0 @@
|
|||
import { describe, expect } from "bun:test"
|
||||
import { Effect, Layer, Schema } from "effect"
|
||||
import { Database } from "@opencode-ai/core/database/database"
|
||||
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 { Job } from "@opencode-ai/core/job"
|
||||
import { Location } from "@opencode-ai/core/location"
|
||||
import { ProjectV2 } from "@opencode-ai/core/project"
|
||||
import { ProjectTable } from "@opencode-ai/core/project/sql"
|
||||
import { AbsolutePath } from "@opencode-ai/core/schema"
|
||||
import { SessionV2 } from "@opencode-ai/core/session"
|
||||
import { SessionExecution } from "@opencode-ai/core/session/execution"
|
||||
import { SessionProjector } from "@opencode-ai/core/session/projector"
|
||||
import { SessionStore } from "@opencode-ai/core/session/store"
|
||||
import { SessionTable } from "@opencode-ai/core/session/sql"
|
||||
import { testEffect } from "./lib/effect"
|
||||
|
||||
const projects = Layer.succeed(
|
||||
ProjectV2.Service,
|
||||
ProjectV2.Service.of({
|
||||
resolve: (directory) => Effect.succeed({ id: ProjectV2.ID.global, directory }),
|
||||
directories: () => Effect.succeed([]),
|
||||
commit: () => Effect.void,
|
||||
}),
|
||||
)
|
||||
const it = testEffect(
|
||||
AppNodeBuilder.build(
|
||||
LayerNode.group([Database.node, EventV2.node, SessionProjector.node, SessionStore.node, SessionV2.node]),
|
||||
[
|
||||
[ProjectV2.node, projects],
|
||||
[SessionExecution.node, SessionExecution.noopLayer],
|
||||
],
|
||||
),
|
||||
)
|
||||
const location = Location.Ref.make({ directory: AbsolutePath.make("/project") })
|
||||
|
||||
const GapEvent = EventV2.define({
|
||||
type: "test.session.history.gap",
|
||||
durable: { aggregate: "sessionID", version: 1 },
|
||||
schema: { sessionID: SessionV2.ID, value: Schema.String },
|
||||
})
|
||||
|
||||
describe("SessionV2.history", () => {
|
||||
it.effect("returns an exhausted page for a migrated Session with no event sequence", () =>
|
||||
Effect.gen(function* () {
|
||||
const db = (yield* Database.Service).db
|
||||
const session = yield* SessionV2.Service
|
||||
const sessionID = SessionV2.ID.make("ses_empty_history")
|
||||
yield* db
|
||||
.insert(ProjectTable)
|
||||
.values({ id: ProjectV2.ID.global, worktree: AbsolutePath.make("/project"), sandboxes: [] })
|
||||
.onConflictDoNothing()
|
||||
.run()
|
||||
yield* db
|
||||
.insert(SessionTable)
|
||||
.values({
|
||||
id: sessionID,
|
||||
project_id: ProjectV2.ID.global,
|
||||
slug: "empty-history",
|
||||
directory: "/project",
|
||||
title: "Empty history",
|
||||
version: "test",
|
||||
})
|
||||
.run()
|
||||
|
||||
const first = yield* session.history({ sessionID, limit: 10 })
|
||||
|
||||
expect(first).toEqual({ events: [], hasMore: false })
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("treats after as an exclusive aggregate sequence", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* SessionV2.Service
|
||||
const created = yield* session.create({ location })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "one" })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "two" })
|
||||
|
||||
const page = yield* session.history({ sessionID: created.id, after: 1, limit: 10 })
|
||||
|
||||
expect(page.events.map((event) => event.durable?.seq)).toEqual([2])
|
||||
expect(page.hasMore).toBe(false)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("paginates public events in aggregate order across filtered gaps without duplicates", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* SessionV2.Service
|
||||
const events = yield* EventV2.Service
|
||||
const created = yield* session.create({ location })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "one" })
|
||||
yield* events.publish(GapEvent, { sessionID: created.id, value: "filtered" })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "two" })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "three" })
|
||||
|
||||
const first = yield* session.history({ sessionID: created.id, limit: 2 })
|
||||
const after = first.events.at(-1)?.durable?.seq
|
||||
const second = yield* session.history({
|
||||
sessionID: created.id,
|
||||
after,
|
||||
limit: 2,
|
||||
})
|
||||
const sequence = [...first.events, ...second.events].map((event) => event.durable?.seq)
|
||||
|
||||
expect(first.hasMore).toBe(true)
|
||||
expect(second.hasMore).toBe(false)
|
||||
expect(sequence).toEqual([1, 3, 4])
|
||||
expect(new Set(sequence).size).toBe(sequence.length)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("includes events committed between pages", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* SessionV2.Service
|
||||
const created = yield* session.create({ location })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "one" })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "two" })
|
||||
|
||||
const first = yield* session.history({ sessionID: created.id, limit: 1 })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "later" })
|
||||
const second = yield* session.history({
|
||||
sessionID: created.id,
|
||||
after: first.events.at(-1)?.durable?.seq,
|
||||
limit: 10,
|
||||
})
|
||||
|
||||
expect(first.hasMore).toBe(true)
|
||||
expect([...first.events, ...second.events].map((event) => event.durable?.seq)).toEqual([1, 2, 3])
|
||||
expect(second.hasMore).toBe(false)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("reports exhaustion for exact-limit and limit-plus-one pages", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* SessionV2.Service
|
||||
const created = yield* session.create({ location })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "one" })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "two" })
|
||||
|
||||
const exact = yield* session.history({ sessionID: created.id, limit: 2 })
|
||||
const oneMore = yield* session.history({ sessionID: created.id, limit: 1 })
|
||||
const exhausted = yield* session.history({
|
||||
sessionID: created.id,
|
||||
after: oneMore.events.at(-1)?.durable?.seq,
|
||||
limit: 1,
|
||||
})
|
||||
|
||||
expect(exact.events).toHaveLength(2)
|
||||
expect(exact.hasMore).toBe(false)
|
||||
expect(oneMore.events).toHaveLength(1)
|
||||
expect(oneMore.hasMore).toBe(true)
|
||||
expect(exhausted.events).toHaveLength(1)
|
||||
expect(exhausted.hasMore).toBe(false)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("fails with NotFoundError for a missing Session", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* SessionV2.Service
|
||||
const error = yield* session.history({ sessionID: SessionV2.ID.make("ses_missing"), limit: 10 }).pipe(Effect.flip)
|
||||
|
||||
expect(error._tag).toBe("Session.NotFoundError")
|
||||
}),
|
||||
)
|
||||
})
|
||||
161
packages/core/test/session-log.test.ts
Normal file
161
packages/core/test/session-log.test.ts
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
import { describe, expect } from "bun:test"
|
||||
import { Effect, Fiber, Layer, Schema, Stream } from "effect"
|
||||
import { Database } from "@opencode-ai/core/database/database"
|
||||
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 { Location } from "@opencode-ai/core/location"
|
||||
import { ProjectV2 } from "@opencode-ai/core/project"
|
||||
import { ProjectTable } from "@opencode-ai/core/project/sql"
|
||||
import { AbsolutePath } from "@opencode-ai/core/schema"
|
||||
import { SessionV2 } from "@opencode-ai/core/session"
|
||||
import { SessionProjector } from "@opencode-ai/core/session/projector"
|
||||
import { SessionExecution } from "@opencode-ai/core/session/execution"
|
||||
import { SessionStore } from "@opencode-ai/core/session/store"
|
||||
import { SessionTable } from "@opencode-ai/core/session/sql"
|
||||
import { testEffect } from "./lib/effect"
|
||||
|
||||
const projects = Layer.succeed(
|
||||
ProjectV2.Service,
|
||||
ProjectV2.Service.of({
|
||||
resolve: (directory) => Effect.succeed({ id: ProjectV2.ID.global, directory }),
|
||||
directories: () => Effect.succeed([]),
|
||||
commit: () => Effect.void,
|
||||
}),
|
||||
)
|
||||
const it = testEffect(
|
||||
AppNodeBuilder.build(
|
||||
LayerNode.group([Database.node, EventV2.node, SessionProjector.node, SessionStore.node, SessionV2.node]),
|
||||
[
|
||||
[ProjectV2.node, projects],
|
||||
[SessionExecution.node, SessionExecution.noopLayer],
|
||||
],
|
||||
),
|
||||
)
|
||||
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", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* SessionV2.Service
|
||||
const events = yield* EventV2.Service
|
||||
const created = yield* session.create({ location })
|
||||
yield* session.rename({ sessionID: created.id, title: "renamed" })
|
||||
|
||||
const items = Array.from(yield* Stream.runCollect(session.log({ sessionID: created.id })))
|
||||
const watermark = (yield* events.sequences([created.id])).get(created.id)
|
||||
|
||||
// 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 })
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("continues with live public events when following", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* SessionV2.Service
|
||||
const created = yield* session.create({ location })
|
||||
const fiber = yield* session
|
||||
.log({ sessionID: created.id, follow: true })
|
||||
.pipe(Stream.take(2), Stream.runCollect, Effect.forkScoped)
|
||||
yield* Effect.yieldNow
|
||||
|
||||
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"])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("fails with NotFound for an unknown session", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* SessionV2.Service
|
||||
const error = yield* Effect.flip(Stream.runCollect(session.log({ sessionID: SessionV2.ID.create() })))
|
||||
expect(error._tag).toBe("Session.NotFoundError")
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("reads across undecodable gaps in aggregate order and marks the true log position", () =>
|
||||
Effect.gen(function* () {
|
||||
const GapEvent = EventV2.define({
|
||||
type: "test.session.log.gap",
|
||||
durable: { aggregate: "sessionID", version: 1 },
|
||||
schema: { sessionID: SessionV2.ID, value: Schema.String },
|
||||
})
|
||||
const session = yield* SessionV2.Service
|
||||
const events = yield* EventV2.Service
|
||||
const created = yield* session.create({ location })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "one" })
|
||||
// Not in the durable manifest, so reads must skip it without failing.
|
||||
yield* events.publish(GapEvent, { sessionID: created.id, value: "filtered" })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "two" })
|
||||
yield* session.switchAgent({ sessionID: created.id, agent: "three" })
|
||||
|
||||
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) })
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("completes with a bare caught-up marker for a migrated Session with no event sequence", () =>
|
||||
Effect.gen(function* () {
|
||||
const db = (yield* Database.Service).db
|
||||
const session = yield* SessionV2.Service
|
||||
const sessionID = SessionV2.ID.make("ses_empty_log")
|
||||
yield* db
|
||||
.insert(ProjectTable)
|
||||
.values({ id: ProjectV2.ID.global, worktree: AbsolutePath.make("/project"), sandboxes: [] })
|
||||
.onConflictDoNothing()
|
||||
.run()
|
||||
yield* db
|
||||
.insert(SessionTable)
|
||||
.values({
|
||||
id: sessionID,
|
||||
project_id: ProjectV2.ID.global,
|
||||
slug: "empty-log",
|
||||
directory: "/project",
|
||||
title: "Empty log",
|
||||
version: "test",
|
||||
})
|
||||
.run()
|
||||
|
||||
const items = Array.from(yield* Stream.runCollect(session.log({ sessionID })))
|
||||
|
||||
expect(items).toEqual([{ type: "log.caught_up", aggregateID: sessionID }])
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
describe("SessionV2 watermarks", () => {
|
||||
it.effect("list pairs each session snapshot with its durable log watermark", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* SessionV2.Service
|
||||
const events = yield* EventV2.Service
|
||||
const first = yield* session.create({ location })
|
||||
const second = yield* session.create({ location })
|
||||
yield* session.rename({ sessionID: first.id, title: "renamed" })
|
||||
|
||||
const page = yield* session.list()
|
||||
const sequences = yield* events.sequences([first.id, second.id])
|
||||
|
||||
expect(page.data.map((info) => info.id).toSorted()).toEqual([first.id, second.id].toSorted())
|
||||
expect(page.watermarks).toEqual(sequences)
|
||||
expect(page.watermarks.get(first.id)).toBeGreaterThan(page.watermarks.get(second.id)!)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("watermarks omits sessions without durable events", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* SessionV2.Service
|
||||
const created = yield* session.create({ location })
|
||||
|
||||
const watermarks = yield* session.watermarks([created.id, SessionV2.ID.create()])
|
||||
|
||||
expect(Array.from(watermarks.keys())).toEqual([created.id])
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
|
@ -245,7 +245,11 @@ describe("SessionV2.prompt", () => {
|
|||
const session = yield* SessionV2.Service
|
||||
const events = yield* EventV2.Service
|
||||
const { db } = yield* Database.Service
|
||||
const fiber = yield* session.events({ sessionID }).pipe(Stream.take(4), Stream.runCollect, Effect.forkScoped)
|
||||
const publicEvents = (input: { sessionID: SessionV2.ID; after?: number }) =>
|
||||
session
|
||||
.log({ ...input, follow: true })
|
||||
.pipe(Stream.filter((item): item is SessionEvent.DurableEvent => !EventV2.isCaughtUp(item)))
|
||||
const fiber = yield* publicEvents({ sessionID }).pipe(Stream.take(4), Stream.runCollect, Effect.forkScoped)
|
||||
yield* Effect.yieldNow
|
||||
|
||||
yield* session.prompt({ sessionID, prompt: Prompt.make({ text: "First" }), resume: false })
|
||||
|
|
@ -253,7 +257,7 @@ describe("SessionV2.prompt", () => {
|
|||
yield* SessionInput.promoteSteers(db, events, sessionID, Number.MAX_SAFE_INTEGER)
|
||||
const streamed = Array.from(yield* Fiber.join(fiber))
|
||||
|
||||
expect(streamed.map((event) => [event.durable?.seq, event.type])).toEqual([
|
||||
expect(streamed.map((event): [number | undefined, string] => [event.durable?.seq, event.type])).toEqual([
|
||||
[0, "session.next.prompt.admitted"],
|
||||
[1, "session.next.prompt.admitted"],
|
||||
[2, "session.next.prompted"],
|
||||
|
|
@ -261,10 +265,8 @@ describe("SessionV2.prompt", () => {
|
|||
])
|
||||
expect(
|
||||
Array.from(
|
||||
yield* session
|
||||
.events({ sessionID, after: streamed[0]!.durable?.seq })
|
||||
.pipe(Stream.take(1), Stream.runCollect),
|
||||
).map((event) => [event.durable?.seq, event.type]),
|
||||
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"]])
|
||||
}),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -27,8 +27,10 @@ const capture = () => {
|
|||
return event
|
||||
}),
|
||||
subscribe: () => Stream.empty,
|
||||
all: () => Stream.empty,
|
||||
durable: () => Stream.empty,
|
||||
live: () => Stream.empty,
|
||||
log: () => Stream.empty,
|
||||
changes: () => Stream.empty,
|
||||
sequences: () => Effect.succeed(new Map()),
|
||||
listen: () => Effect.succeed(Effect.void),
|
||||
project: () => Effect.void,
|
||||
replay: () => Effect.void,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue