refactor(core): consolidate tool architecture

This commit is contained in:
Dax Raad 2026-07-26 20:08:55 -04:00
commit 8db7487c89
466 changed files with 9405 additions and 11071 deletions

View file

@ -2,20 +2,20 @@ import { describe, expect } from "bun:test"
import path from "path"
import { DateTime, Effect, Layer, Stream } from "effect"
import { Money } from "@opencode-ai/schema/money"
import { AgentV2 } from "@opencode-ai/core/agent"
import { Agent } from "@opencode-ai/core/agent"
import { asc, eq } from "drizzle-orm"
import { Database } from "@opencode-ai/core/database/database"
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
import { LayerNode } from "@opencode-ai/util/effect/layer-node"
import { EventV2 } from "@opencode-ai/core/event"
import { Bus } from "@opencode-ai/core/bus"
import { EventTable } from "@opencode-ai/core/event/sql"
import { Location } from "@opencode-ai/core/location"
import { ModelV2 } from "@opencode-ai/core/model"
import { ProjectV2 } from "@opencode-ai/core/project"
import { Model } 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 { Provider } from "@opencode-ai/core/provider"
import { AbsolutePath } from "@opencode-ai/core/schema"
import { SessionV2 } from "@opencode-ai/core/session"
import { Session } from "@opencode-ai/core/session"
import { SessionV1 } from "@opencode-ai/core/v1/session"
import { SessionMessage } from "@opencode-ai/core/session/message"
import { SessionProjector } from "@opencode-ai/core/session/projector"
@ -24,42 +24,42 @@ import { SessionPending } from "@opencode-ai/core/session/pending"
import { SessionEvent } from "@opencode-ai/core/session/event"
import { SessionTable } from "@opencode-ai/core/session/sql"
import { SessionStore } from "@opencode-ai/core/session/store"
import { WorkspaceV2 } from "@opencode-ai/core/workspace"
import { Workspace } from "@opencode-ai/core/workspace"
import { testEffect } from "./lib/effect"
import { tmpdir } from "./fixture/tmpdir"
const projects = Layer.succeed(
ProjectV2.Service,
ProjectV2.Service.of({
Project.Service,
Project.Service.of({
list: () => Effect.succeed([]),
resolve: (directory) => Effect.succeed({ id: ProjectV2.ID.global, directory }),
resolve: (directory) => Effect.succeed({ id: Project.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]),
LayerNode.group([Database.node, Bus.node, SessionProjector.node, SessionStore.node, Session.node]),
[
[ProjectV2.node, projects],
[Project.node, projects],
[SessionExecution.node, SessionExecution.noopLayer],
],
),
)
const location = Location.Ref.make({ directory: AbsolutePath.make("/project") })
const id = SessionV2.ID.create()
const id = Session.ID.create()
/** Public session events from a `log` read, without synced markers. */
const logEvents = (session: SessionV2.Interface, sessionID: SessionV2.ID, follow?: boolean) =>
const logEvents = (session: Session.Interface, sessionID: Session.ID, follow?: boolean) =>
session
.log({ sessionID, follow })
.pipe(Stream.filter((item): item is SessionEvent.DurableEvent => !EventV2.isSynced(item)))
.pipe(Stream.filter((item): item is SessionEvent.DurableEvent => !Bus.isSynced(item)))
const assertCreateInputTypes = (session: SessionV2.Interface) => {
const assertCreateInputTypes = (session: Session.Interface) => {
// @ts-expect-error location or parentID is required.
session.create({})
// @ts-expect-error child sessions inherit their parent's location.
session.create({ parentID: SessionV2.ID.create(), location })
session.create({ parentID: Session.ID.create(), location })
}
void assertCreateInputTypes
@ -70,10 +70,10 @@ function withTmp<A, E, R>(f: (directory: string) => Effect.Effect<A, E, R>) {
).pipe(Effect.flatMap((tmp) => f(tmp.path)))
}
describe("SessionV2.create", () => {
describe("Session.create", () => {
it.effect("creates a fresh projected session when the ID is omitted", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const first = yield* session.create({ location })
const second = yield* session.create({ location })
@ -85,7 +85,7 @@ describe("SessionV2.create", () => {
it.effect("returns the original session when the ID is retried", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const input = { id, location }
const first = yield* session.create(input)
@ -98,18 +98,18 @@ describe("SessionV2.create", () => {
it.effect("stores supplied immutable create attributes", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const workspaceID = WorkspaceV2.ID.make("wrk_test")
const model = ModelV2.Ref.make({
id: ModelV2.ID.make("sonnet"),
providerID: ProviderV2.ID.anthropic,
variant: ModelV2.VariantID.make("fast"),
const session = yield* Session.Service
const workspaceID = Workspace.ID.make("wrk_test")
const model = Model.Ref.make({
id: Model.ID.make("sonnet"),
providerID: Provider.ID.anthropic,
variant: Model.VariantID.make("fast"),
})
expect(
yield* session.create({
location: Location.Ref.make({ directory: location.directory, workspaceID }),
agent: AgentV2.ID.make("build"),
agent: Agent.ID.make("build"),
model,
}),
).toMatchObject({ location: { directory: location.directory, workspaceID }, agent: "build", model })
@ -118,7 +118,7 @@ describe("SessionV2.create", () => {
it.effect("inherits location from an existing parent when omitted", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const parent = yield* session.create({ location })
const child = yield* session.create({ parentID: parent.id, title: "child" })
@ -128,18 +128,18 @@ describe("SessionV2.create", () => {
it.effect("rejects child creation when the parent does not exist", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const missing = SessionV2.ID.create()
const session = yield* Session.Service
const missing = Session.ID.create()
expect(yield* Effect.flip(session.create({ parentID: missing, title: "child" }))).toEqual(
new SessionV2.NotFoundError({ sessionID: missing }),
new Session.NotFoundError({ sessionID: missing }),
)
}),
)
it.effect("filters root sessions before applying the page limit", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const { db } = yield* Database.Service
const staleRoot = yield* session.create({ location, title: "stale root" })
const root = yield* session.create({ location, title: "root" })
@ -173,7 +173,7 @@ describe("SessionV2.create", () => {
it.effect("filters direct child sessions by parent ID", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const parent = yield* session.create({ location, title: "parent" })
const child = yield* session.create({ parentID: parent.id, title: "child" })
yield* session.create({ location, title: "other root" })
@ -186,8 +186,8 @@ describe("SessionV2.create", () => {
it.effect("forks a session by replaying a durable fork event into copied projected rows", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const events = yield* EventV2.Service
const session = yield* Session.Service
const bus = yield* Bus.Service
const { db } = yield* Database.Service
const parent = yield* session.create({ location, title: "Parent" })
const admitted = yield* session.prompt({
@ -195,9 +195,9 @@ describe("SessionV2.create", () => {
text: "First",
resume: false,
})
yield* SessionPending.promote(db, events, parent.id, "steer")
yield* SessionPending.promote(db, bus, parent.id, "steer")
yield* session.synthetic({ sessionID: parent.id, text: "parent note", resume: false })
yield* SessionPending.promote(db, events, parent.id, "steer")
yield* SessionPending.promote(db, bus, parent.id, "steer")
const forked = yield* session.fork({ sessionID: parent.id })
const parentContext = yield* session.context(parent.id)
@ -232,13 +232,13 @@ describe("SessionV2.create", () => {
text: "Parent changed",
resume: false,
})
yield* SessionPending.promote(db, events, parent.id, "steer")
yield* SessionPending.promote(db, bus, parent.id, "steer")
yield* session.prompt({
sessionID: forked.id,
text: "Child continues",
resume: false,
})
yield* SessionPending.promote(db, events, forked.id, "steer")
yield* SessionPending.promote(db, bus, forked.id, "steer")
expect((yield* session.context(parent.id)).map((message) => message.type)).toEqual(["user", "synthetic", "user"])
expect((yield* session.context(forked.id)).map((message) => message.type)).toEqual(["user", "synthetic", "user"])
@ -254,8 +254,8 @@ describe("SessionV2.create", () => {
it.effect("forks before the selected boundary message", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const events = yield* EventV2.Service
const session = yield* Session.Service
const bus = yield* Bus.Service
const { db } = yield* Database.Service
const parent = yield* session.create({ location })
const first = yield* session.prompt({
@ -263,22 +263,22 @@ describe("SessionV2.create", () => {
text: "First",
resume: false,
})
yield* SessionPending.promote(db, events, parent.id, "steer")
yield* SessionPending.promote(db, bus, parent.id, "steer")
const second = yield* session.prompt({
sessionID: parent.id,
text: "Second",
resume: false,
})
yield* SessionPending.promote(db, events, parent.id, "steer")
yield* SessionPending.promote(db, bus, parent.id, "steer")
const assistantMessageID = SessionMessage.ID.create()
const model = ModelV2.Ref.make({ id: ModelV2.ID.make("model"), providerID: ProviderV2.ID.make("provider") })
yield* events.publish(SessionEvent.Step.Started, {
const model = Model.Ref.make({ id: Model.ID.make("model"), providerID: Provider.ID.make("provider") })
yield* bus.publish(SessionEvent.Step.Started, {
sessionID: parent.id,
assistantMessageID,
agent: AgentV2.ID.make("build"),
agent: Agent.ID.make("build"),
model,
})
yield* events.publish(SessionEvent.Step.Ended, {
yield* bus.publish(SessionEvent.Step.Ended, {
sessionID: parent.id,
assistantMessageID,
finish: "stop",
@ -308,15 +308,15 @@ describe("SessionV2.create", () => {
it.effect("returns the existing Session when one ID is reused with different create arguments", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const created = yield* session.create({ id, location })
const changed = [
{ id, location: Location.Ref.make({ directory: AbsolutePath.make("/other") }) },
{ id, location, agent: AgentV2.ID.make("build") },
{ id, location, agent: Agent.ID.make("build") },
{
id,
location,
model: ModelV2.Ref.make({ id: ModelV2.ID.make("sonnet"), providerID: ProviderV2.ID.anthropic }),
model: Model.Ref.make({ id: Model.ID.make("sonnet"), providerID: Provider.ID.anthropic }),
},
]
@ -329,7 +329,7 @@ describe("SessionV2.create", () => {
it.effect("returns one recorded session to concurrent exact retries", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const input = { id, location }
const created = yield* Effect.all([session.create(input), session.create(input)], { concurrency: "unbounded" })
@ -341,7 +341,7 @@ describe("SessionV2.create", () => {
it.effect("returns the current Session projection after updates", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const { db } = yield* Database.Service
const input = { id, location }
const created = yield* session.create(input)
@ -354,12 +354,12 @@ describe("SessionV2.create", () => {
it.effect("returns the current Session projection after projected updates", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const events = yield* EventV2.Service
const session = yield* Session.Service
const bus = yield* Bus.Service
const input = { id, location }
const created = yield* session.create(input)
yield* events.publish(SessionV1.Event.Updated, {
yield* bus.publish(SessionV1.Event.Updated, {
sessionID: id,
info: SessionV1.SessionInfo.make({
id,
@ -379,19 +379,19 @@ describe("SessionV2.create", () => {
it.effect("persists creation through the existing legacy created event", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const { db } = yield* Database.Service
const created = yield* session.create({ location })
expect(
yield* db.select().from(EventTable).where(eq(EventTable.aggregate_id, created.id)).all().pipe(Effect.orDie),
).toMatchObject([{ type: EventV2.versionedType(SessionV1.Event.Created.type, 1) }])
).toMatchObject([{ type: Bus.versionedType(SessionV1.Event.Created.type, 1) }])
}),
)
it.effect("persists caller-ID creation through the existing created event", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const { db } = yield* Database.Service
const created = yield* session.create({ id, location })
@ -403,10 +403,10 @@ describe("SessionV2.create", () => {
}),
)
it.effect("omits legacy creation rows from the V2 Session event stream", () =>
it.effect("omits legacy creation rows from the Session event stream", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const events = yield* EventV2.Service
const session = yield* Session.Service
const bus = yield* Bus.Service
const { db } = yield* Database.Service
const created = yield* session.create({ location })
yield* session.prompt({
@ -414,7 +414,7 @@ describe("SessionV2.create", () => {
text: "Hello",
resume: false,
})
yield* SessionPending.promote(db, events, created.id, "steer")
yield* SessionPending.promote(db, bus, created.id, "steer")
expect(
Array.from(yield* logEvents(session, created.id, true).pipe(Stream.take(2), Stream.runCollect)),
@ -431,10 +431,10 @@ describe("SessionV2.create", () => {
it.effect("replays one prompt lifecycle into a fresh target database", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const sourceEvents = yield* EventV2.Service
const session = yield* Session.Service
const sourceEvents = yield* Bus.Service
const sourceDb = (yield* Database.Service).db
const created = yield* session.create({ id: SessionV2.ID.make("ses_fresh_target_replay"), location })
const created = yield* session.create({ id: Session.ID.make("ses_fresh_target_replay"), location })
const admitted = yield* session.prompt({
sessionID: created.id,
text: "Replay lifecycle",
@ -462,22 +462,22 @@ describe("SessionV2.create", () => {
)
const targetDatabase = Database.layer({ path: path.join(tmp.path, "target.sqlite") })
const targetLayer = AppNodeBuilder.build(
LayerNode.group([Database.node, EventV2.node, SessionProjector.node, SessionStore.node]),
LayerNode.group([Database.node, Bus.node, SessionProjector.node, SessionStore.node]),
[[Database.node, targetDatabase]],
)
yield* Effect.gen(function* () {
const db = (yield* Database.Service).db
const events = yield* EventV2.Service
const bus = yield* Bus.Service
const store = yield* SessionStore.Service
yield* db
.insert(ProjectTable)
.values({ id: ProjectV2.ID.global, worktree: location.directory, sandboxes: [] })
.values({ id: Project.ID.global, worktree: location.directory, sandboxes: [] })
.run()
.pipe(Effect.orDie)
expect(yield* store.get(created.id)).toBeUndefined()
expect(yield* events.replayAll(serialized.slice(0, 2))).toBe(created.id)
expect(yield* bus.replayAll(serialized.slice(0, 2))).toBe(created.id)
expect(yield* SessionPending.find(db, admitted.id)).toMatchObject({
id: admitted.id,
sessionID: created.id,
@ -488,7 +488,7 @@ describe("SessionV2.create", () => {
})
expect(yield* store.context(created.id)).toEqual([])
expect(yield* events.replayAll(serialized.slice(2))).toBe(created.id)
expect(yield* bus.replayAll(serialized.slice(2))).toBe(created.id)
expect(yield* SessionPending.find(db, admitted.id)).toBeUndefined()
expect(yield* store.context(created.id)).toMatchObject([
{ id: admitted.id, type: "user", text: "Replay lifecycle" },
@ -502,9 +502,9 @@ describe("SessionV2.create", () => {
.all()
.pipe(Effect.orDie)).map((event) => [event.seq, event.type]),
).toEqual([
[0, EventV2.versionedType(SessionV1.Event.Created.type, 1)],
[1, EventV2.versionedType(SessionEvent.InputAdmitted.type, 1)],
[2, EventV2.versionedType(SessionEvent.InputPromoted.type, 1)],
[0, Bus.versionedType(SessionV1.Event.Created.type, 1)],
[1, Bus.versionedType(SessionEvent.InputAdmitted.type, 1)],
[2, Bus.versionedType(SessionEvent.InputPromoted.type, 1)],
])
}).pipe(Effect.provide(Layer.fresh(targetLayer)))
}),
@ -512,8 +512,8 @@ describe("SessionV2.create", () => {
it.effect("does not mask unrelated created projector defects", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const event = yield* EventV2.Service
const session = yield* Session.Service
const event = yield* Bus.Service
const defect = new Error("unrelated projector defect")
yield* event.project(SessionV1.Event.Created, () => Effect.die(defect))
@ -524,7 +524,7 @@ describe("SessionV2.create", () => {
it.live("runs a shell command and projects the started/ended shell message", () =>
withTmp((directory) =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const created = yield* session.create({
location: Location.Ref.make({ directory: AbsolutePath.make(directory) }),
})
@ -544,7 +544,7 @@ describe("SessionV2.create", () => {
it.live("still emits shell ended for a failing command", () =>
withTmp((directory) =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const created = yield* session.create({
location: Location.Ref.make({ directory: AbsolutePath.make(directory) }),
})
@ -562,10 +562,10 @@ describe("SessionV2.create", () => {
it.effect("switches the selected agent through the durable Session event", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const created = yield* session.create({ location })
yield* session.switchAgent({ sessionID: created.id, agent: AgentV2.ID.make("plan") })
yield* session.switchAgent({ sessionID: created.id, agent: Agent.ID.make("plan") })
expect(yield* session.get(created.id)).toMatchObject({ agent: "plan" })
expect(
@ -576,11 +576,11 @@ describe("SessionV2.create", () => {
it.effect("rejects an agent switch for a missing Session", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const missing = SessionV2.ID.make("ses_missing_agent_switch")
const session = yield* Session.Service
const missing = Session.ID.make("ses_missing_agent_switch")
expect(
yield* session.switchAgent({ sessionID: missing, agent: AgentV2.ID.make("plan") }).pipe(
yield* session.switchAgent({ sessionID: missing, agent: Agent.ID.make("plan") }).pipe(
Effect.flip,
Effect.map((error) => error._tag),
),
@ -590,28 +590,28 @@ describe("SessionV2.create", () => {
it.effect("switches the selected model through the durable Session event", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const created = yield* session.create({ location })
const model = ModelV2.Ref.make({
id: ModelV2.ID.make("sonnet"),
providerID: ProviderV2.ID.anthropic,
variant: ModelV2.VariantID.make("high"),
const model = Model.Ref.make({
id: Model.ID.make("sonnet"),
providerID: Provider.ID.anthropic,
variant: Model.VariantID.make("high"),
})
yield* session.switchModel({ sessionID: created.id, model })
expect(yield* session.get(created.id)).toMatchObject({ model })
const events = Array.from(yield* logEvents(session, created.id, true).pipe(Stream.take(1), Stream.runCollect))
expect(events).toMatchObject([{ type: "session.model.selected" }])
expect(events[0]?.data).toEqual({ sessionID: created.id, model })
const bus = Array.from(yield* logEvents(session, created.id, true).pipe(Stream.take(1), Stream.runCollect))
expect(bus).toMatchObject([{ type: "session.model.selected" }])
expect(bus[0]?.data).toEqual({ sessionID: created.id, model })
}),
)
it.effect("ignores a model switch when the selected model is unchanged", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const session = yield* Session.Service
const created = yield* session.create({ location })
const model = ModelV2.Ref.make({ id: ModelV2.ID.make("sonnet"), providerID: ProviderV2.ID.anthropic })
const model = Model.Ref.make({ id: Model.ID.make("sonnet"), providerID: Provider.ID.anthropic })
yield* session.switchModel({ sessionID: created.id, model })
yield* session.switchModel({ sessionID: created.id, model })
@ -626,13 +626,13 @@ describe("SessionV2.create", () => {
it.effect("treats an omitted variant as the default variant", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const model = ModelV2.Ref.make({ id: ModelV2.ID.make("sonnet"), providerID: ProviderV2.ID.anthropic })
const session = yield* Session.Service
const model = Model.Ref.make({ id: Model.ID.make("sonnet"), providerID: Provider.ID.anthropic })
const created = yield* session.create({ location, model })
yield* session.switchModel({
sessionID: created.id,
model: ModelV2.Ref.make({ ...model, variant: ModelV2.VariantID.make("default") }),
model: Model.Ref.make({ ...model, variant: Model.VariantID.make("default") }),
})
const { db } = yield* Database.Service
@ -644,14 +644,14 @@ describe("SessionV2.create", () => {
it.effect("rejects a model switch for a missing Session", () =>
Effect.gen(function* () {
const session = yield* SessionV2.Service
const missing = SessionV2.ID.make("ses_missing_model_switch")
const session = yield* Session.Service
const missing = Session.ID.make("ses_missing_model_switch")
expect(
yield* session
.switchModel({
sessionID: missing,
model: ModelV2.Ref.make({ id: ModelV2.ID.make("sonnet"), providerID: ProviderV2.ID.anthropic }),
model: Model.Ref.make({ id: Model.ID.make("sonnet"), providerID: Provider.ID.anthropic }),
})
.pipe(
Effect.flip,