| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- import { describe, expect } from "bun:test"
- import { DateTime, Effect, Schema } from "effect"
- import { asc, eq } from "drizzle-orm"
- import { Database } from "@opencode-ai/core/database/database"
- import { LayerNode } from "@opencode-ai/core/effect/layer-node"
- import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
- import { EventV2 } from "@opencode-ai/core/event"
- import { EventTable } from "@opencode-ai/core/event/sql"
- import { ModelV2 } from "@opencode-ai/core/model"
- import { Project } from "@opencode-ai/core/project"
- import { ProjectTable } from "@opencode-ai/core/project/sql"
- import { ProviderV2 } from "@opencode-ai/core/provider"
- import { AbsolutePath } from "@opencode-ai/core/schema"
- import { SessionV2 } from "@opencode-ai/core/session"
- import { SessionEvent } from "@opencode-ai/core/session/event"
- import { SessionMessage } from "@opencode-ai/core/session/message"
- import { Prompt } from "@opencode-ai/core/session/prompt"
- import { SessionMessageUpdater } from "@opencode-ai/core/session/message-updater"
- import { SessionProjector } from "@opencode-ai/core/session/projector"
- import { SessionExecution } from "@opencode-ai/core/session/execution"
- import { SessionInput } from "@opencode-ai/core/session/input"
- import { SessionInputTable, SessionMessageTable, SessionTable } from "@opencode-ai/core/session/sql"
- import { testEffect } from "./lib/effect"
- import { Snapshot } from "@opencode-ai/core/snapshot"
- const it = testEffect(AppNodeBuilder.build(LayerNode.group([Database.node, EventV2.node, SessionProjector.node])))
- const sessionsLayer = AppNodeBuilder.build(SessionV2.node, [[SessionExecution.node, SessionExecution.noopLayer]])
- const sessionID = SessionV2.ID.make("ses_projector_test")
- const created = DateTime.makeUnsafe(0)
- const model = { id: ModelV2.ID.make("model"), providerID: ProviderV2.ID.make("provider") }
- const encodeMessage = Schema.encodeSync(SessionMessage.Message)
- const assistantRow = (
- id: SessionMessage.ID,
- seq: number,
- time: { created: DateTime.Utc; completed?: DateTime.Utc } = { created },
- ) => {
- const {
- id: _,
- type,
- ...data
- } = encodeMessage(SessionMessage.Assistant.make({ id, type: "assistant", agent: "build", model, content: [], time }))
- return { id, session_id: sessionID, type, seq, time_created: DateTime.toEpochMillis(time.created), data }
- }
- describe("SessionProjector", () => {
- it.effect("projects staged, cleared, and committed reverts", () =>
- Effect.gen(function* () {
- const db = (yield* Database.Service).db
- yield* db
- .insert(ProjectTable)
- .values({ id: Project.ID.global, worktree: AbsolutePath.make("/project"), sandboxes: [] })
- .run()
- yield* db
- .insert(SessionTable)
- .values({
- id: sessionID,
- project_id: Project.ID.global,
- slug: "test",
- directory: "/project",
- title: "test",
- version: "test",
- })
- .run()
- const boundary = SessionMessage.ID.make("msg_boundary")
- yield* db
- .insert(SessionMessageTable)
- .values([assistantRow(boundary, 1), assistantRow(SessionMessage.ID.make("msg_later"), 2)])
- .run()
- const events = yield* EventV2.Service
- yield* events.publish(SessionEvent.RevertEvent.Staged, {
- sessionID,
- timestamp: DateTime.makeUnsafe(1),
- revert: { messageID: boundary, snapshot: Snapshot.ID.make("tree"), diff: "patch", files: [] },
- })
- expect((yield* db.select({ revert: SessionTable.revert }).from(SessionTable).get())?.revert).toMatchObject({
- messageID: boundary,
- snapshot: "tree",
- files: [],
- })
- yield* events.publish(SessionEvent.RevertEvent.Cleared, { sessionID, timestamp: DateTime.makeUnsafe(2) })
- expect((yield* db.select({ revert: SessionTable.revert }).from(SessionTable).get())?.revert).toBeNull()
- yield* events.publish(SessionEvent.RevertEvent.Staged, {
- sessionID,
- timestamp: DateTime.makeUnsafe(3),
- revert: { messageID: boundary, files: [] },
- })
- yield* events.publish(SessionEvent.RevertEvent.Committed, {
- sessionID,
- messageID: boundary,
- timestamp: DateTime.makeUnsafe(4),
- })
- expect(
- (yield* db.select({ id: SessionMessageTable.id }).from(SessionMessageTable).all()).map((row) => row.id),
- ).toEqual([boundary])
- }),
- )
- it.effect("orders projected messages and context by durable aggregate sequence", () =>
- Effect.gen(function* () {
- const { db } = yield* Database.Service
- yield* db
- .insert(ProjectTable)
- .values({ id: Project.ID.global, worktree: AbsolutePath.make("/project"), sandboxes: [] })
- .run()
- .pipe(Effect.orDie)
- yield* db
- .insert(SessionTable)
- .values({
- id: sessionID,
- project_id: Project.ID.global,
- slug: "test",
- directory: "/project",
- title: "test",
- version: "test",
- })
- .run()
- .pipe(Effect.orDie)
- const events = yield* EventV2.Service
- yield* events.publish(
- SessionEvent.Prompted,
- {
- sessionID,
- messageID: SessionMessage.ID.make("msg_first"),
- timestamp: created,
- prompt: Prompt.make({ text: "first" }),
- delivery: "steer",
- },
- { id: EventV2.ID.make("evt_z") },
- )
- yield* events.publish(
- SessionEvent.Prompted,
- {
- sessionID,
- messageID: SessionMessage.ID.make("msg_second"),
- timestamp: created,
- prompt: Prompt.make({ text: "second" }),
- delivery: "steer",
- },
- { id: EventV2.ID.make("evt_a") },
- )
- const sessions = yield* SessionV2.Service
- const firstPage = yield* sessions.messages({ sessionID, limit: 1, order: "asc" })
- expect(firstPage.map((message) => (message.type === "user" ? message.text : message.type))).toEqual(["first"])
- const secondPage = yield* sessions.messages({
- sessionID,
- limit: 1,
- order: "asc",
- cursor: { id: firstPage[0]!.id, direction: "next" },
- })
- expect(secondPage.map((message) => (message.type === "user" ? message.text : message.type))).toEqual(["second"])
- expect(
- (yield* sessions.messages({
- sessionID,
- limit: 1,
- order: "asc",
- cursor: { id: secondPage[0]!.id, direction: "previous" },
- })).map((message) => (message.type === "user" ? message.text : message.type)),
- ).toEqual(["first"])
- expect(
- (yield* sessions.context(sessionID)).map((message) => (message.type === "user" ? message.text : message.type)),
- ).toEqual(["first", "second"])
- }).pipe(Effect.provide(sessionsLayer)),
- )
- it.effect("marks an inbox row promoted with the Prompted event sequence", () =>
- Effect.gen(function* () {
- const { db } = yield* Database.Service
- yield* db
- .insert(ProjectTable)
- .values({ id: Project.ID.global, worktree: AbsolutePath.make("/project"), sandboxes: [] })
- .run()
- .pipe(Effect.orDie)
- yield* db
- .insert(SessionTable)
- .values({
- id: sessionID,
- project_id: Project.ID.global,
- slug: "test",
- directory: "/project",
- title: "test",
- version: "test",
- })
- .run()
- .pipe(Effect.orDie)
- const events = yield* EventV2.Service
- const id = SessionMessage.ID.make("msg_admitted")
- const admitted = yield* SessionInput.admit(db, events, {
- id,
- sessionID,
- prompt: Prompt.make({ text: "promote me" }),
- delivery: "steer",
- })
- if (!admitted) return yield* Effect.die("Prompt admission failed")
- const event = yield* events.publish(SessionEvent.Prompted, {
- sessionID,
- timestamp: admitted.timeCreated,
- messageID: id,
- prompt: Prompt.make({ text: "promote me" }),
- delivery: "steer",
- })
- expect(
- yield* db.select().from(SessionInputTable).where(eq(SessionInputTable.id, id)).get().pipe(Effect.orDie),
- ).toMatchObject({ promoted_seq: event.durable?.seq })
- }),
- )
- it.effect("projects durable context messages supported by the updater", () =>
- Effect.gen(function* () {
- const { db } = yield* Database.Service
- yield* db
- .insert(ProjectTable)
- .values({ id: Project.ID.global, worktree: AbsolutePath.make("/project"), sandboxes: [] })
- .run()
- .pipe(Effect.orDie)
- yield* db
- .insert(SessionTable)
- .values({
- id: sessionID,
- project_id: Project.ID.global,
- slug: "test",
- directory: "/project",
- title: "test",
- version: "test",
- })
- .run()
- .pipe(Effect.orDie)
- const events = yield* EventV2.Service
- yield* events.publish(SessionEvent.AgentSwitched, {
- sessionID,
- messageID: SessionMessage.ID.create(),
- timestamp: created,
- agent: "build",
- })
- yield* events.publish(SessionEvent.ModelSwitched, {
- sessionID,
- messageID: SessionMessage.ID.create(),
- timestamp: created,
- model,
- })
- yield* events.publish(SessionEvent.Synthetic, {
- sessionID,
- messageID: SessionMessage.ID.create(),
- timestamp: created,
- text: "synthetic context",
- })
- yield* events.publish(SessionEvent.Shell.Started, {
- sessionID,
- messageID: SessionMessage.ID.create(),
- timestamp: created,
- callID: "shell-1",
- command: "pwd",
- })
- yield* events.publish(SessionEvent.Shell.Ended, {
- sessionID,
- timestamp: DateTime.makeUnsafe(1),
- callID: "shell-1",
- output: "/project",
- })
- const compactionID = SessionMessage.ID.create()
- yield* events.publish(SessionEvent.Compaction.Started, {
- sessionID,
- messageID: compactionID,
- timestamp: created,
- reason: "manual",
- })
- yield* events.publish(SessionEvent.Compaction.Delta, {
- sessionID,
- messageID: compactionID,
- timestamp: created,
- text: "partial",
- })
- expect(
- yield* db
- .select({ id: EventTable.id })
- .from(EventTable)
- .where(eq(EventTable.type, SessionEvent.Compaction.Delta.type))
- .all()
- .pipe(Effect.orDie),
- ).toEqual([])
- expect(
- yield* db
- .select({ id: SessionMessageTable.id })
- .from(SessionMessageTable)
- .where(eq(SessionMessageTable.type, "compaction"))
- .all()
- .pipe(Effect.orDie),
- ).toEqual([])
- yield* events.publish(SessionEvent.Compaction.Ended, {
- sessionID,
- messageID: compactionID,
- timestamp: DateTime.makeUnsafe(1),
- reason: "manual",
- text: "summary",
- recent: "recent context",
- })
- const rows = yield* db
- .select()
- .from(SessionMessageTable)
- .where(eq(SessionMessageTable.session_id, sessionID))
- .orderBy(asc(SessionMessageTable.seq))
- .all()
- .pipe(Effect.orDie)
- const messages = rows.map((row) =>
- Schema.decodeUnknownSync(SessionMessage.Message)({ ...row.data, id: row.id, type: row.type }),
- )
- expect(messages.map((message) => message.type)).toEqual([
- "agent-switched",
- "model-switched",
- "synthetic",
- "shell",
- "compaction",
- ])
- expect(messages.find((message) => message.type === "shell")).toMatchObject({
- output: "/project",
- time: { completed: DateTime.makeUnsafe(1) },
- })
- expect(messages.find((message) => message.type === "compaction")).toMatchObject({
- summary: "summary",
- recent: "recent context",
- })
- expect(
- yield* db.select().from(SessionTable).where(eq(SessionTable.id, sessionID)).get().pipe(Effect.orDie),
- ).toMatchObject({
- agent: "build",
- model,
- time_updated: DateTime.toEpochMillis(created),
- })
- }),
- )
- it.effect("rejects distinct creator events that reuse one projected message ID", () =>
- Effect.gen(function* () {
- const { db } = yield* Database.Service
- yield* db
- .insert(ProjectTable)
- .values({ id: Project.ID.global, worktree: AbsolutePath.make("/project"), sandboxes: [] })
- .run()
- .pipe(Effect.orDie)
- yield* db
- .insert(SessionTable)
- .values({
- id: sessionID,
- project_id: Project.ID.global,
- slug: "test",
- directory: "/project",
- title: "test",
- version: "test",
- })
- .run()
- .pipe(Effect.orDie)
- const events = yield* EventV2.Service
- const id = SessionMessage.ID.make("msg_creator_collision")
- yield* events.publish(SessionEvent.Synthetic, { sessionID, messageID: id, timestamp: created, text: "keep me" })
- const exit = yield* events
- .publish(SessionEvent.Step.Started, {
- sessionID,
- assistantMessageID: id,
- timestamp: created,
- agent: "build",
- model,
- })
- .pipe(Effect.exit)
- expect(exit._tag).toBe("Failure")
- expect(
- yield* db.select().from(SessionMessageTable).where(eq(SessionMessageTable.id, id)).get().pipe(Effect.orDie),
- ).toMatchObject({ type: "synthetic" })
- }),
- )
- it.effect("does not revive a stale incomplete in-memory assistant projection", () =>
- Effect.gen(function* () {
- const stale = SessionMessage.Assistant.make({
- id: SessionMessage.ID.make("msg_assistant_stale"),
- type: "assistant",
- agent: "build",
- model,
- content: [],
- time: { created },
- })
- const completed = SessionMessage.Assistant.make({
- id: SessionMessage.ID.make("msg_assistant_completed"),
- type: "assistant",
- agent: "build",
- model,
- content: [],
- time: { created: DateTime.makeUnsafe(1), completed: DateTime.makeUnsafe(2) },
- })
- expect(
- yield* SessionMessageUpdater.memory({ messages: [stale, completed] }).getCurrentAssistant(),
- ).toBeUndefined()
- }),
- )
- it.effect("updates only the newest incomplete assistant projection", () =>
- Effect.gen(function* () {
- const { db } = yield* Database.Service
- yield* db
- .insert(ProjectTable)
- .values({ id: Project.ID.global, worktree: AbsolutePath.make("/project"), sandboxes: [] })
- .run()
- .pipe(Effect.orDie)
- yield* db
- .insert(SessionTable)
- .values({
- id: sessionID,
- project_id: Project.ID.global,
- slug: "test",
- directory: "/project",
- title: "test",
- version: "test",
- })
- .run()
- .pipe(Effect.orDie)
- yield* db
- .insert(SessionMessageTable)
- .values([
- assistantRow(SessionMessage.ID.make("msg_assistant_1"), 0),
- assistantRow(SessionMessage.ID.make("msg_assistant_2"), 1),
- ])
- .run()
- .pipe(Effect.orDie)
- const service = yield* EventV2.Service
- yield* service.publish(SessionEvent.Step.Ended, {
- sessionID,
- timestamp: DateTime.makeUnsafe(1),
- assistantMessageID: SessionMessage.ID.make("msg_assistant_2"),
- finish: "stop",
- cost: 0,
- tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
- })
- const rows = yield* db
- .select()
- .from(SessionMessageTable)
- .where(eq(SessionMessageTable.session_id, sessionID))
- .orderBy(asc(SessionMessageTable.id))
- .all()
- .pipe(Effect.orDie)
- const messages = rows.map((row) =>
- Schema.decodeUnknownSync(SessionMessage.Message)({ ...row.data, id: row.id, type: row.type }),
- )
- expect(messages[0]).not.toHaveProperty("time.completed")
- expect(messages[1]).toMatchObject({
- type: "assistant",
- finish: "stop",
- time: { completed: DateTime.makeUnsafe(1) },
- })
- }),
- )
- it.effect("does not revive a stale incomplete assistant projection", () =>
- Effect.gen(function* () {
- const { db } = yield* Database.Service
- yield* db
- .insert(ProjectTable)
- .values({ id: Project.ID.global, worktree: AbsolutePath.make("/project"), sandboxes: [] })
- .run()
- .pipe(Effect.orDie)
- yield* db
- .insert(SessionTable)
- .values({
- id: sessionID,
- project_id: Project.ID.global,
- slug: "test",
- directory: "/project",
- title: "test",
- version: "test",
- })
- .run()
- .pipe(Effect.orDie)
- yield* db
- .insert(SessionMessageTable)
- .values([
- assistantRow(SessionMessage.ID.make("msg_assistant_stale"), 0),
- assistantRow(SessionMessage.ID.make("msg_assistant_completed"), 1, {
- created: DateTime.makeUnsafe(1),
- completed: DateTime.makeUnsafe(2),
- }),
- ])
- .run()
- .pipe(Effect.orDie)
- const service = yield* EventV2.Service
- yield* service.publish(SessionEvent.Text.Started, {
- sessionID,
- assistantMessageID: SessionMessage.ID.make("msg_assistant_completed"),
- timestamp: DateTime.makeUnsafe(3),
- textID: "text-stale",
- })
- const rows = yield* db
- .select()
- .from(SessionMessageTable)
- .where(eq(SessionMessageTable.session_id, sessionID))
- .orderBy(asc(SessionMessageTable.id))
- .all()
- .pipe(Effect.orDie)
- const messages = rows.map((row) =>
- Schema.decodeUnknownSync(SessionMessage.Message)({ ...row.data, id: row.id, type: row.type }),
- )
- expect(messages).toEqual([
- SessionMessage.Assistant.make({
- id: SessionMessage.ID.make("msg_assistant_completed"),
- type: "assistant",
- agent: "build",
- model,
- content: [SessionMessage.AssistantText.make({ type: "text", id: "text-stale", text: "" })],
- time: { created: DateTime.makeUnsafe(1), completed: DateTime.makeUnsafe(2) },
- }),
- SessionMessage.Assistant.make({
- id: SessionMessage.ID.make("msg_assistant_stale"),
- type: "assistant",
- agent: "build",
- model,
- content: [],
- time: { created },
- }),
- ])
- }),
- )
- })
|