mirror of
https://github.com/Heretek-AI/opencode.git
synced 2026-11-23 17:09:27 -10:00
refactor(core): use parent ID for session forks
This commit is contained in:
1 parent
e91480201a
commit
f736e6f111
4 files changed
+64
-36
No files matched your search
@@ -93,7 +93,7 @@ type CompactInput = Parameters<Session.Handle["compact"]>[0] & { sessionID: Sess
|
||||
type ForkInput = {
|
||||
sessionID: SessionSchema.ID
|
||||
boundary: SessionSchema.ForkRequestBoundary
|
||||
child?: Pick<CreateBaseInput, "title" | "agent" | "model">
|
||||
parentID?: SessionSchema.ID
|
||||
}
|
||||
|
||||
export {
|
||||
@@ -312,8 +312,8 @@ const layer = Layer.effect(
|
||||
messageID: input.boundary.messageID,
|
||||
})
|
||||
if (!boundary) return yield* new ForkEmptyError({ sessionID: input.sessionID })
|
||||
const sessionID = input.child
|
||||
? (yield* result.create({ parentID: parent.id, ...input.child })).id
|
||||
const sessionID = input.parentID
|
||||
? (yield* result.create({ parentID: input.parentID })).id
|
||||
: SessionSchema.ID.create()
|
||||
const inherited = yield* db
|
||||
.transaction(() =>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export * as SessionProjector from "./projector.js"
|
||||
|
||||
import { and, asc, desc, eq, gt, gte, inArray, isNull, lt, lte, or, sql } from "drizzle-orm"
|
||||
import { and, asc, desc, eq, gt, gte, inArray, isNotNull, isNull, lt, lte, or, sql } from "drizzle-orm"
|
||||
import { DateTime, Effect, Layer, Schema, Stream } from "effect"
|
||||
import path from "path"
|
||||
import { Database } from "../database/database.js"
|
||||
@@ -144,22 +144,25 @@ const projectFork = Effect.fn("SessionProjector.projectFork")(function* (
|
||||
.pipe(Effect.orDie)
|
||||
const copiedSeq = copied?.seq
|
||||
|
||||
const inherited = {
|
||||
fork_session_id: event.data.parentID,
|
||||
fork_boundary: event.data.boundary,
|
||||
project_id: parent.project_id,
|
||||
workspace_id: parent.workspace_id,
|
||||
directory: parent.directory,
|
||||
path: parent.path,
|
||||
title: forkTitle(parent.title ?? undefined),
|
||||
agent: parent.agent,
|
||||
model: parent.model,
|
||||
metadata: parent.metadata,
|
||||
}
|
||||
const stored = yield* db
|
||||
.insert(SessionTable)
|
||||
.values({
|
||||
id: event.data.sessionID,
|
||||
parent_id: null,
|
||||
fork_session_id: event.data.parentID,
|
||||
fork_boundary: event.data.boundary,
|
||||
project_id: parent.project_id,
|
||||
workspace_id: parent.workspace_id,
|
||||
...inherited,
|
||||
slug: Slug.create(),
|
||||
directory: parent.directory,
|
||||
path: parent.path,
|
||||
title: forkTitle(parent.title ?? undefined),
|
||||
agent: parent.agent,
|
||||
model: parent.model,
|
||||
metadata: parent.metadata,
|
||||
version: parent.version,
|
||||
cost: 0,
|
||||
tokens_input: 0,
|
||||
@@ -170,11 +173,11 @@ const projectFork = Effect.fn("SessionProjector.projectFork")(function* (
|
||||
time_created: event.created,
|
||||
time_updated: event.created,
|
||||
})
|
||||
// Subagents already have a Created event owning their identity and configuration.
|
||||
// Created records optional ownership; Forked supplies the source's history and defaults.
|
||||
.onConflictDoUpdate({
|
||||
target: SessionTable.id,
|
||||
set: { fork_session_id: event.data.parentID, fork_boundary: event.data.boundary, time_updated: event.created },
|
||||
setWhere: and(eq(SessionTable.parent_id, event.data.parentID), isNull(SessionTable.fork_session_id)),
|
||||
set: { ...inherited, time_updated: event.created },
|
||||
setWhere: and(isNotNull(SessionTable.parent_id), isNull(SessionTable.fork_session_id)),
|
||||
})
|
||||
.returning({ sessionID: SessionTable.id })
|
||||
.get()
|
||||
|
||||
@@ -171,8 +171,8 @@ export const Plugin = {
|
||||
fork && input.fork
|
||||
? sessions.fork({
|
||||
sessionID: context.sessionID,
|
||||
parentID: context.sessionID,
|
||||
boundary: { type: "before", messageID: context.messageID },
|
||||
child: { title: input.description, agent: agent.id, model },
|
||||
})
|
||||
: sessions.create({
|
||||
parentID: context.sessionID,
|
||||
@@ -193,6 +193,15 @@ export const Plugin = {
|
||||
),
|
||||
))
|
||||
|
||||
if (fork && input.fork)
|
||||
yield* sessions.rename({ sessionID: child.id, title: input.description }).pipe(
|
||||
Effect.andThen(sessions.switchAgent({ sessionID: child.id, agent: agent.id })),
|
||||
Effect.andThen(model ? sessions.switchModel({ sessionID: child.id, model }) : Effect.void),
|
||||
Effect.mapError(
|
||||
(error) => new ToolFailure({ message: `Failed to configure subagent: ${child.id}`, error }),
|
||||
),
|
||||
)
|
||||
|
||||
const background = input.background === true
|
||||
yield* context.progress({ sessionID: child.id, status: "running" })
|
||||
|
||||
|
||||
@@ -578,27 +578,43 @@ describe("Session.create", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
for (const child of [
|
||||
undefined,
|
||||
{
|
||||
title: "Child",
|
||||
agent: Agent.ID.make("reviewer"),
|
||||
model: Model.Ref.make({ id: Model.ID.make("child"), providerID: Provider.ID.make("test") }),
|
||||
},
|
||||
]) {
|
||||
it.effect(`replays a ${child ? "child " : ""}fork with stable projected identities`, () =>
|
||||
for (const ownership of ["none", "source", "other"] as const) {
|
||||
it.effect(`replays a fork with ${ownership} ownership and stable projected identities`, () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* Session.Service
|
||||
const bus = yield* Bus.Service
|
||||
const { db } = yield* Database.Service
|
||||
const parent = yield* session.create({ location, title: "Parent" })
|
||||
yield* session.prompt({ sessionID: parent.id, text: "First", resume: false })
|
||||
yield* SessionInbox.promote(db, bus, parent.id, "steer")
|
||||
yield* session.synthetic({ sessionID: parent.id, text: "Second", resume: false })
|
||||
yield* SessionInbox.promote(db, bus, parent.id, "steer")
|
||||
const forked = yield* session.fork({ sessionID: parent.id, boundary: { type: "through" }, child })
|
||||
expect(forked.parentID).toBe(child ? parent.id : undefined)
|
||||
if (child) expect(forked).toMatchObject(child)
|
||||
const source = yield* session.create({
|
||||
location,
|
||||
title: "Source",
|
||||
agent: Agent.ID.make("build"),
|
||||
model: Model.Ref.make({ id: Model.ID.make("source"), providerID: Provider.ID.make("test") }),
|
||||
metadata: { source: true },
|
||||
})
|
||||
const parentID =
|
||||
ownership === "none"
|
||||
? undefined
|
||||
: ownership === "source"
|
||||
? source.id
|
||||
: (yield* session.create({
|
||||
location: Location.Ref.make({ directory: AbsolutePath.make("/owner") }),
|
||||
title: "Owner",
|
||||
metadata: { owner: true },
|
||||
})).id
|
||||
yield* session.prompt({ sessionID: source.id, text: "First", resume: false })
|
||||
yield* SessionInbox.promote(db, bus, source.id, "steer")
|
||||
yield* session.synthetic({ sessionID: source.id, text: "Second", resume: false })
|
||||
yield* SessionInbox.promote(db, bus, source.id, "steer")
|
||||
const forked = yield* session.fork({ sessionID: source.id, boundary: { type: "through" }, parentID })
|
||||
expect(forked.parentID).toBe(parentID)
|
||||
expect(forked).toMatchObject({
|
||||
title: "Source (fork #1)",
|
||||
agent: source.agent,
|
||||
model: source.model,
|
||||
metadata: source.metadata,
|
||||
location: source.location,
|
||||
fork: { sessionID: source.id },
|
||||
})
|
||||
const original = (yield* session.context(forked.id)).map((message) => message.id)
|
||||
const recorded = yield* db
|
||||
.select()
|
||||
@@ -608,7 +624,7 @@ describe("Session.create", () => {
|
||||
.all()
|
||||
.pipe(Effect.orDie)
|
||||
expect(recorded.map((event) => event.type)).toEqual(
|
||||
child ? ["session.created.1", "session.forked.2"] : ["session.forked.2"],
|
||||
parentID ? ["session.created.1", "session.forked.2"] : ["session.forked.2"],
|
||||
)
|
||||
|
||||
yield* bus.remove(forked.id)
|
||||
|
||||
Reference in new issue
Block a user