mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
fix(core): write the fork notice to history and catch forks of forks
This commit is contained in:
@@ -385,20 +385,16 @@ const layer = Layer.effect(
|
||||
...inherited,
|
||||
})
|
||||
.pipe(Effect.provideService(SessionBlob.ForkMedia, media))
|
||||
const forked = yield* result.get(sessionID).pipe(Effect.orDie)
|
||||
// Appended after the copied rows, so the prompt-cache prefix stays byte-identical. It waits in the inbox as a
|
||||
// steer and doesn't wake the fork.
|
||||
yield* result
|
||||
.synthetic({
|
||||
sessionID,
|
||||
description: "Fork notice",
|
||||
text: forkNotice(parent),
|
||||
metadata: { source: "fork", forkOf: parent.id },
|
||||
resume: false,
|
||||
})
|
||||
// A fresh message ID cannot conflict; keep the public error channel unchanged.
|
||||
.pipe(Effect.catchTag("Session.SyntheticConflictError", Effect.die))
|
||||
return forked
|
||||
// Appended to history after the copied rows, so the prompt-cache prefix stays byte-identical. Written directly
|
||||
// rather than through the inbox: an inbox steer would be promoted, and start a turn, on the fork's next move,
|
||||
// compaction or revert.
|
||||
yield* bus.publish(SessionEvent.Synthetic, {
|
||||
sessionID,
|
||||
description: "Fork notice",
|
||||
text: forkNotice(parent),
|
||||
metadata: { source: "fork", forkOf: parent.id },
|
||||
})
|
||||
return yield* result.get(sessionID).pipe(Effect.orDie)
|
||||
}),
|
||||
get: (sessionID) => sessions.forSession(sessionID).get(),
|
||||
environment: Effect.fn("Session.environment")(function* (input) {
|
||||
|
||||
@@ -162,12 +162,20 @@ export const Plugin = {
|
||||
),
|
||||
)
|
||||
if (existing !== undefined && existing.parentID !== context.sessionID) {
|
||||
// A fork's parent is the original, so a child of the original reports there, not here.
|
||||
const original = parent.fork?.sessionID
|
||||
// A child of any session this one was forked from (a fork of a fork too) reports there, not here.
|
||||
const seen = new Set<string>([parent.id])
|
||||
let original = parent.fork?.sessionID
|
||||
while (original !== undefined && original !== existing.parentID && !seen.has(original)) {
|
||||
seen.add(original)
|
||||
original = yield* sessions.get(original).pipe(
|
||||
Effect.map((session) => session.fork?.sessionID),
|
||||
Effect.orElseSucceed(() => undefined),
|
||||
)
|
||||
}
|
||||
return yield* new ToolFailure({
|
||||
message:
|
||||
original !== undefined && original === existing.parentID
|
||||
? `Session ${existing.id} belongs to the original session ${existing.parentID}; this session is a fork of it, so that subagent reports there. Start a new subagent here instead.`
|
||||
? `Session ${existing.id} belongs to ${existing.parentID}, which this session was forked from, so that subagent reports there. Start a new subagent here instead.`
|
||||
: `Session ${existing.id} is not a child of the current session`,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -110,6 +110,7 @@ const projectIt = testEffect(
|
||||
)
|
||||
const location = Location.Ref.make({ directory: AbsolutePath.make("/project") })
|
||||
const id = Session.ID.create()
|
||||
const forkNoticeMessage = { type: "synthetic", description: "Fork notice" }
|
||||
|
||||
/** Public session events from a `log` read, without synced markers. */
|
||||
const logEvents = (session: Session.Interface, sessionID: Session.ID, follow?: boolean) =>
|
||||
@@ -631,22 +632,20 @@ describe("Session.create", () => {
|
||||
const parentContext = yield* session.context(parent.id)
|
||||
const forkContext = yield* session.context(forked.id)
|
||||
const history = Array.from(yield* Stream.runCollect(logEvents(session, forked.id)))
|
||||
const notices = (yield* session.inbox(forked.id)).filter((item) => item.type === "synthetic")
|
||||
|
||||
expect(notices).toMatchObject([
|
||||
{
|
||||
sessionID: forked.id,
|
||||
delivery: "steer",
|
||||
payload: {
|
||||
description: "Fork notice",
|
||||
text: `This session is a fork of ${parent.id} ("Parent"). Subagents started before the fork belong to the original and report there, not here. Don't wait for them; start a new subagent if this fork needs that work.`,
|
||||
metadata: { source: "fork", forkOf: parent.id },
|
||||
},
|
||||
},
|
||||
])
|
||||
// The notice is history, not an inbox item, so nothing waits to start a turn on the idle fork.
|
||||
expect(yield* session.inbox(forked.id)).toEqual([])
|
||||
expect(forked).toMatchObject({ title: "Parent (fork #1)", fork: { sessionID: parent.id } })
|
||||
expect(forked.parentID).toBeUndefined()
|
||||
expect(forkContext).toMatchObject([Expected.user("First"), { type: "synthetic", text: "parent note" }])
|
||||
expect(forkContext).toMatchObject([
|
||||
Expected.user("First"),
|
||||
{ type: "synthetic", text: "parent note" },
|
||||
{
|
||||
type: "synthetic",
|
||||
description: "Fork notice",
|
||||
text: `This session is a fork of ${parent.id} ("Parent"). Subagents started before the fork belong to the original and report there, not here. Don't wait for them; start a new subagent if this fork needs that work.`,
|
||||
},
|
||||
])
|
||||
expect(forkContext.map((message) => message.id)).not.toEqual(parentContext.map((message) => message.id))
|
||||
expect(history).toHaveLength(2)
|
||||
expect(history[0]).toMatchObject({
|
||||
@@ -655,8 +654,8 @@ describe("Session.create", () => {
|
||||
data: { sessionID: forked.id, parentID: parent.id },
|
||||
})
|
||||
expect(history[1]).toMatchObject({
|
||||
type: "session.inbox.enqueued",
|
||||
data: { sessionID: forked.id, item: { type: "synthetic", delivery: "steer" } },
|
||||
type: "session.synthetic",
|
||||
data: { sessionID: forked.id, metadata: { source: "fork", forkOf: parent.id } },
|
||||
})
|
||||
expect(yield* SessionInbox.find(db, forkContext[0].id)).toBeUndefined()
|
||||
expect(yield* SessionInbox.find(db, forkContext[1].id)).toBeUndefined()
|
||||
@@ -680,13 +679,8 @@ describe("Session.create", () => {
|
||||
expect((yield* session.context(parent.id)).map((message) => message.type)).toEqual(["user", "synthetic", "user"])
|
||||
const forkedContext = yield* session.context(forked.id)
|
||||
expect(forkedContext.map((message) => message.type)).toEqual(["user", "synthetic", "synthetic", "user"])
|
||||
expect(forkedContext.at(-2)).toMatchObject({ type: "synthetic", id: notices[0]?.id })
|
||||
expect(forkedContext.at(-2)).toMatchObject(forkNoticeMessage)
|
||||
expect(forkedContext.at(-1)).toMatchObject({ text: "Child continues" })
|
||||
expect(
|
||||
Array.from(yield* Stream.runCollect(logEvents(session, forked.id))).map(
|
||||
(event): number | undefined => event.durable?.seq,
|
||||
),
|
||||
).toEqual([0, 5, 6, 7, 8])
|
||||
expect(yield* SessionInbox.find(db, admitted.id)).toBeUndefined()
|
||||
}),
|
||||
)
|
||||
@@ -724,20 +718,23 @@ describe("Session.create", () => {
|
||||
.select()
|
||||
.from(EventTable)
|
||||
.where(eq(EventTable.aggregate_id, forked.id))
|
||||
.get()
|
||||
.orderBy(EventTable.seq)
|
||||
.all()
|
||||
.pipe(Effect.orDie)
|
||||
if (!recorded) return yield* Effect.die(new Error("Fork event not found"))
|
||||
expect(recorded).toHaveLength(2)
|
||||
|
||||
yield* bus.remove(forked.id)
|
||||
yield* db.delete(SessionTable).where(eq(SessionTable.id, forked.id)).run().pipe(Effect.orDie)
|
||||
yield* bus.replay({
|
||||
id: recorded.id,
|
||||
created: recorded.created,
|
||||
aggregateID: recorded.aggregate_id,
|
||||
seq: recorded.seq,
|
||||
type: recorded.type,
|
||||
data: recorded.data,
|
||||
})
|
||||
yield* Effect.forEach(recorded, (event) =>
|
||||
bus.replay({
|
||||
id: event.id,
|
||||
created: event.created,
|
||||
aggregateID: event.aggregate_id,
|
||||
seq: event.seq,
|
||||
type: event.type,
|
||||
data: event.data,
|
||||
}),
|
||||
)
|
||||
|
||||
expect((yield* session.context(forked.id)).map((message) => message.id)).toEqual(original)
|
||||
}),
|
||||
@@ -841,7 +838,7 @@ describe("Session.create", () => {
|
||||
Expected.user("Run both tools"),
|
||||
Expected.assistant({}, [{ type: "tool", id: "call_running", state: { status: "running" } }]),
|
||||
])
|
||||
expect(yield* session.context(forked.id)).toMatchObject([Expected.user("Run both tools")])
|
||||
expect(yield* session.context(forked.id)).toMatchObject([Expected.user("Run both tools"), forkNoticeMessage])
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -871,7 +868,7 @@ describe("Session.create", () => {
|
||||
Expected.user("Run a shell"),
|
||||
{ type: "shell", command: "sleep 10", status: "running" },
|
||||
])
|
||||
expect(yield* session.context(running.id)).toMatchObject([Expected.user("Run a shell")])
|
||||
expect(yield* session.context(running.id)).toMatchObject([Expected.user("Run a shell"), forkNoticeMessage])
|
||||
|
||||
yield* bus.publish(SessionEvent.Shell.Ended, {
|
||||
sessionID: parent.id,
|
||||
@@ -880,10 +877,11 @@ describe("Session.create", () => {
|
||||
})
|
||||
const completed = yield* session.fork({ sessionID: parent.id })
|
||||
|
||||
expect(yield* session.context(running.id)).toMatchObject([Expected.user("Run a shell")])
|
||||
expect(yield* session.context(running.id)).toMatchObject([Expected.user("Run a shell"), forkNoticeMessage])
|
||||
expect(yield* session.context(completed.id)).toMatchObject([
|
||||
Expected.user("Run a shell"),
|
||||
{ type: "shell", command: "sleep 10", status: "exited", output: { output: "complete" } },
|
||||
forkNoticeMessage,
|
||||
])
|
||||
}),
|
||||
)
|
||||
@@ -951,18 +949,20 @@ describe("Session.create", () => {
|
||||
sessionID: parent.id,
|
||||
boundary: { type: "before", messageID: second.id },
|
||||
})
|
||||
expect(context).toMatchObject([{ text: "First" }])
|
||||
expect(context).toMatchObject([{ text: "First" }, forkNoticeMessage])
|
||||
expect(context[0]?.id).not.toBe(first.id)
|
||||
expect(history[0]).toMatchObject({
|
||||
data: { boundary: { type: "before", messageID: second.id } },
|
||||
})
|
||||
expect(forked).toMatchObject({ cost: 0, tokens: { input: 0, output: 0, reasoning: 0 } })
|
||||
expect(yield* session.context(beforeFirst.id)).toEqual([])
|
||||
// A fork before the first message gets the notice too, so an untitled empty fork still learns what it is.
|
||||
expect(yield* session.context(beforeFirst.id)).toMatchObject([forkNoticeMessage])
|
||||
expect(beforeFirst).toMatchObject({ cost: 0, tokens: { input: 0, output: 0, reasoning: 0 } })
|
||||
expect(yield* session.context(complete.id)).toMatchObject([
|
||||
Expected.user("First"),
|
||||
Expected.user("Second"),
|
||||
{ type: "assistant", finish: "stop" },
|
||||
forkNoticeMessage,
|
||||
])
|
||||
expect(complete).toMatchObject({
|
||||
cost: 0,
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { FileSystem } from "@opencode/core/filesystem"
|
||||
import { DateTime, Effect, Fiber, Layer, LayerMap, Schema, Stream } from "effect"
|
||||
import path from "path"
|
||||
import { pathToFileURL } from "url"
|
||||
import { eq } from "drizzle-orm"
|
||||
import { and, eq } from "drizzle-orm"
|
||||
import { Database } from "@opencode/core/database/database"
|
||||
import { Agent } from "@opencode/core/agent"
|
||||
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
||||
@@ -266,7 +266,7 @@ const forkInlineParent = Effect.fnUntraced(function* (id: string, directory: str
|
||||
const row = yield* db
|
||||
.select()
|
||||
.from(SessionMessageTable)
|
||||
.where(eq(SessionMessageTable.session_id, child.id))
|
||||
.where(and(eq(SessionMessageTable.session_id, child.id), eq(SessionMessageTable.type, "user")))
|
||||
.get()
|
||||
.pipe(Effect.orDie)
|
||||
return row!
|
||||
|
||||
@@ -189,8 +189,9 @@ describe("Session.skill", () => {
|
||||
yield* SessionInbox.promote(database.db, bus, session.id, "steer")
|
||||
const forked = yield* sessions.fork({ sessionID: session.id, before: selected })
|
||||
|
||||
expect(yield* sessions.messages({ sessionID: forked.id })).toEqual([
|
||||
expect(yield* sessions.messages({ sessionID: forked.id, order: "asc" })).toEqual([
|
||||
expect.objectContaining({ type: "user", text: "Before the skill" }),
|
||||
expect.objectContaining({ type: "synthetic", description: "Fork notice" }),
|
||||
])
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -723,7 +723,7 @@ describe("SubagentTool", () => {
|
||||
),
|
||||
)
|
||||
|
||||
it.live("rejects continuing the original's subagent from a fork", () =>
|
||||
it.live("rejects continuing the original's subagent from a fork of a fork", () =>
|
||||
tmpdirScoped().pipe(
|
||||
Effect.flatMap((dir) =>
|
||||
Effect.gen(function* () {
|
||||
@@ -739,7 +739,8 @@ describe("SubagentTool", () => {
|
||||
title: "original review",
|
||||
agent: Agent.ID.make("reviewer"),
|
||||
})
|
||||
const forked = yield* sessions.fork({ sessionID: parent.id })
|
||||
const first = yield* sessions.fork({ sessionID: parent.id })
|
||||
const forked = yield* sessions.fork({ sessionID: first.id })
|
||||
yield* withSubagent(parent.location)
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
const registry = yield* Tool.Service.pipe(Effect.provide(locations.get(forked.location)))
|
||||
@@ -759,7 +760,7 @@ describe("SubagentTool", () => {
|
||||
status: "error",
|
||||
error: {
|
||||
type: "tool.execution",
|
||||
message: `Session ${child.id} belongs to the original session ${parent.id}; this session is a fork of it, so that subagent reports there. Start a new subagent here instead.`,
|
||||
message: `Session ${child.id} belongs to ${parent.id}, which this session was forked from, so that subagent reports there. Start a new subagent here instead.`,
|
||||
},
|
||||
})
|
||||
}),
|
||||
|
||||
@@ -118,15 +118,7 @@ test("Promise instances are lazy, share by key and Location, and stay isolated b
|
||||
expect(fork.fork?.sessionID).toBe(sessionID)
|
||||
const inherited = await first.sessions.prompt({ sessionID: fork.id, text: "Review the fork", resume: false })
|
||||
expect(inherited.payload.text).toBe(`first:alpha@${directory.path}: Review the fork`)
|
||||
const forkInbox = await first.sessions.inbox.list({ sessionID: fork.id })
|
||||
expect(forkInbox.filter((item) => item.type === "user")).toEqual([inherited])
|
||||
expect(forkInbox.filter((item) => item.type === "synthetic")).toMatchObject([
|
||||
{
|
||||
sessionID: fork.id,
|
||||
delivery: "steer",
|
||||
payload: { description: "Fork notice", metadata: { source: "fork", forkOf: sessionID } },
|
||||
},
|
||||
])
|
||||
expect(await first.sessions.inbox.list({ sessionID: fork.id })).toEqual([inherited])
|
||||
|
||||
expect(configured.toSorted()).toEqual(["first:alpha", "first:alpha", "first:beta", "second:alpha"])
|
||||
expect(setups.toSorted()).toEqual(
|
||||
|
||||
Reference in New Issue
Block a user