diff --git a/packages/core/src/session.ts b/packages/core/src/session.ts index 7906c09d951..4f57d6470cc 100644 --- a/packages/core/src/session.ts +++ b/packages/core/src/session.ts @@ -384,9 +384,8 @@ const layer = Layer.effect( // Append-only: the notice is a new admission after the copied rows, so the // copied prompt-cache prefix stays byte-identical. It waits in the inbox as a // steer and never wakes this fork's runner on creation. - const copied = yield* store.messages({ sessionID, order: "asc" }).pipe(Effect.orDie) const children = yield* store.list({ parentID: parent.id }).pipe(Effect.orDie) - const subagents = yield* ForkNotice.collect({ messages: copied, children, jobs }) + const subagents = yield* ForkNotice.collect({ db, sessionID, children, jobs }) yield* result .synthetic({ sessionID, diff --git a/packages/core/src/session/fork-notice.ts b/packages/core/src/session/fork-notice.ts index 1e1f3ee25f9..7ed6c23a484 100644 --- a/packages/core/src/session/fork-notice.ts +++ b/packages/core/src/session/fork-notice.ts @@ -1,9 +1,13 @@ export * as ForkNotice from "./fork-notice.js" import { DateTime, Effect } from "effect" +import { and, asc, eq, gt, inArray } from "drizzle-orm" import type { Session } from "@opencode/schema/session" +import { DatabaseBatch } from "../database/batch.js" +import type { Database } from "../database/database.js" import type { Job } from "../job.js" import type { SessionMessage } from "./message.js" +import { SessionMessageTable, SessionPartTable } from "./sql.js" export type Subagent = { readonly sessionID: string @@ -16,35 +20,52 @@ const outstanding = "These subagents of the original were still outstanding when const closing = "Don't wait for these. Their results won't arrive here. Start a new subagent if this fork needs that work." +/** Reads copies in the same page size as the projection loop, so a fork scan never materializes the whole transcript. */ +const ScanBatchSize = 500 + +type MessageRow = typeof SessionMessageTable.$inferSelect +type Part = SessionMessage.AssistantContentEncoded + /** - * Names the original's work that keeps reporting to the original: background - * launches in the copied history with no later report, and children that still - * hold a running subagent job. + * Names the original's work that keeps reporting to the original: launches in the copied history with no later + * report, and children that still hold a running subagent job. + * + * One pass in transcript order, straight from the copied rows: a launch enters the set and a later report deletes + * it, so no launch rescans the transcript and no media blob is ever hydrated. */ export const collect = (input: { - readonly messages: readonly SessionMessage.Info[] + readonly db: Database.Interface["db"] + readonly sessionID: Session.ID readonly children: readonly Session.Info[] readonly jobs: Pick }): Effect.Effect => Effect.gen(function* () { // Map order keeps transcript order for launched entries while deduping by child. const subagents = new Map() - input.messages.forEach((message, index) => { - if (message.type !== "assistant") return - for (const part of message.content) { - if (part.type !== "tool" || part.name !== "subagent" || part.state.status === "streaming") continue - if (part.state.input.background !== true) continue - const sessionID = part.state.metadata?.sessionID - if (typeof sessionID !== "string" || subagents.has(sessionID)) continue - if (input.messages.slice(index + 1).some((later) => mentions(later, report(sessionID)))) continue - const description = part.state.input.description - subagents.set(sessionID, { - sessionID, - description: typeof description === "string" ? description : "subagent", - state: "unreported", - }) + let cursor = -1 + while (true) { + const rows = yield* input.db + .select() + .from(SessionMessageTable) + .where(and(eq(SessionMessageTable.session_id, input.sessionID), gt(SessionMessageTable.seq, cursor))) + .orderBy(asc(SessionMessageTable.seq)) + .limit(ScanBatchSize) + .all() + .pipe(Effect.orDie) + if (rows.length === 0) break + cursor = rows.at(-1)!.seq + const parts = yield* contentParts(input.db, rows) + for (const row of rows) { + if (row.type !== "assistant") { + if (!("text" in row.data)) continue + const text = row.data.text + if (typeof text === "string") forget(subagents, text) + continue + } + const inline = "content" in row.data && Array.isArray(row.data.content) ? row.data.content : [] + for (const part of [...(parts.get(row.id) ?? []), ...inline]) fold(subagents, part) } - }) + } for (const child of input.children) { const job = yield* input.jobs.get(child.id) // Finished jobs stay in the in-memory map, so only a running job is outstanding. @@ -58,17 +79,70 @@ export const collect = (input: { return [...subagents.values()] }) -/** Includes the closing quote so one child id cannot prefix-match another. */ -const report = (sessionID: string) => ` { - if (message.type !== "assistant") return "text" in message && message.text.includes(needle) - return message.content.some((part) => { - if (part.type === "text") return part.text.includes(needle) - if (part.type !== "tool" || part.state.status === "streaming") return false - if (!("content" in part.state)) return false - return (part.state.content ?? []).some((content) => content.type === "text" && content.text.includes(needle)) +/** + * Text and tool parts a split assistant row keeps in `session_part`, in ordinal order. Media parts stay in the + * table, and legacy rows without parts keep their inline content. + */ +const contentParts = (db: Database.Interface["db"], rows: ReadonlyArray) => + Effect.gen(function* () { + const partsByMessage = new Map() + const assistantIDs = rows.flatMap((row) => (row.type === "assistant" ? [row.id] : [])) + for (const ids of DatabaseBatch.batches(assistantIDs, 1)) { + const found = yield* db + .select({ message_id: SessionPartTable.message_id, data: SessionPartTable.data }) + .from(SessionPartTable) + .where(and(inArray(SessionPartTable.message_id, ids), inArray(SessionPartTable.type, ["text", "tool"]))) + .orderBy(asc(SessionPartTable.message_id), asc(SessionPartTable.ordinal)) + .all() + .pipe(Effect.orDie) + for (const part of found) { + const parts = partsByMessage.get(part.message_id) + if (parts) parts.push(part.data) + else partsByMessage.set(part.message_id, [part.data]) + } + } + return partsByMessage }) + +const fold = (subagents: Map, part: Part) => { + if (part.type === "text") { + forget(subagents, part.text) + return + } + if (part.type !== "tool" || part.name !== "subagent" || part.state.status === "streaming") return + // A foreground follow-up's result reports its child in the tool content. + const content = "content" in part.state ? (part.state.content ?? []) : [] + for (const item of content) if (item.type === "text") forget(subagents, item.text) + const launched = launch(part.state, content) + if (launched === undefined || subagents.has(launched.sessionID)) return + subagents.set(launched.sessionID, { ...launched, state: "unreported" }) +} + +/** + * A background launch, whether the call asked for one or a foreground call was moved to the background before the + * fork. The result then says it is running; a completed result is not an outstanding launch. + */ +const launch = ( + state: Exclude, + content: ReadonlyArray<{ readonly type: string; readonly text?: string }>, +) => { + const metadata = state.metadata + const text = content + .flatMap((item) => (item.type === "text" && item.text !== undefined ? [item.text] : [])) + .join("\n") + const sessionID = + typeof metadata?.sessionID === "string" + ? metadata.sessionID + : /is working in the background \(sessionID: ([^)]+)\)/.exec(text)?.[1] + if (sessionID === undefined) return + if (state.input.background !== true && metadata?.status !== "running") return + const description = state.input.description + return { sessionID, description: typeof description === "string" ? description : "subagent" } +} + +/** Matches the report `SubagentCompletion` delivers, keeping the closing quote so one child id can't prefix another. */ +const forget = (subagents: Map, text: string) => { + for (const match of text.matchAll(/ { }), ) + it.effect("names a foreground subagent that was moved to the background before the fork", () => + Effect.gen(function* () { + const session = yield* Session.Service + const bus = yield* Bus.Service + const { db } = yield* Database.Service + const parent = yield* session.create({ location }) + yield* session.prompt({ sessionID: parent.id, text: "Review the diff", resume: false }) + yield* SessionInbox.promote(db, bus, parent.id, "steer") + const assistantMessageID = SessionMessage.ID.create() + const child = Session.ID.create() + + yield* bus.publish(SessionEvent.Step.Started, { + sessionID: parent.id, + assistantMessageID, + agent: Agent.ID.make("build"), + model: Model.Ref.make({ id: Model.ID.make("model"), providerID: Provider.ID.make("provider") }), + started: 0, + }) + yield* bus.publish(SessionEvent.Tool.Input.Started, { + sessionID: parent.id, + assistantMessageID, + id: "call_backgrounded", + name: "subagent", + }) + yield* bus.publish(SessionEvent.Tool.Input.Ended, { + sessionID: parent.id, + assistantMessageID, + id: "call_backgrounded", + text: JSON.stringify({ description: "review the diff" }), + }) + // No `background: true`: the foreground call was moved to the background while it blocked. + yield* bus.publish(SessionEvent.Tool.Called, { + sessionID: parent.id, + assistantMessageID, + id: "call_backgrounded", + input: { description: "review the diff" }, + executed: true, + }) + yield* bus.publish(SessionEvent.Tool.Success, { + sessionID: parent.id, + assistantMessageID, + id: "call_backgrounded", + content: [ + { + type: "text", + text: `The subagent is working in the background (sessionID: ${child}). You will be notified automatically when it finishes.`, + }, + ], + metadata: { sessionID: child, status: "running" }, + executed: true, + }) + yield* bus.publish(SessionEvent.Step.Ended, { + sessionID: parent.id, + assistantMessageID, + finish: "stop", + cost: Money.USD.make(0), + tokens: { input: 1, output: 1, reasoning: 0, cache: { read: 0, write: 0 } }, + }) + + const forked = yield* session.fork({ sessionID: parent.id }) + yield* SessionInbox.promote(db, bus, forked.id, "steer") + const notice = (yield* session.context(forked.id)).at(-1) + + expect(notice).toMatchObject({ type: "synthetic" }) + const text = notice?.type === "synthetic" ? notice.text : "" + expect(text).toContain(`- ${child} "review the diff" (unreported)`) + }), + ) + + it.effect("does not name a launch whose report is in the copied history", () => + Effect.gen(function* () { + const session = yield* Session.Service + const bus = yield* Bus.Service + const { db } = yield* Database.Service + const parent = yield* session.create({ location }) + yield* session.prompt({ sessionID: parent.id, text: "Launch a subagent", resume: false }) + yield* SessionInbox.promote(db, bus, parent.id, "steer") + const assistantMessageID = SessionMessage.ID.create() + const child = Session.ID.create() + + yield* bus.publish(SessionEvent.Step.Started, { + sessionID: parent.id, + assistantMessageID, + agent: Agent.ID.make("build"), + model: Model.Ref.make({ id: Model.ID.make("model"), providerID: Provider.ID.make("provider") }), + started: 0, + }) + yield* bus.publish(SessionEvent.Tool.Input.Started, { + sessionID: parent.id, + assistantMessageID, + id: "call_launch", + name: "subagent", + }) + yield* bus.publish(SessionEvent.Tool.Input.Ended, { + sessionID: parent.id, + assistantMessageID, + id: "call_launch", + text: JSON.stringify({ background: true, description: "review the diff" }), + }) + yield* bus.publish(SessionEvent.Tool.Called, { + sessionID: parent.id, + assistantMessageID, + id: "call_launch", + input: { background: true, description: "review the diff" }, + executed: true, + }) + yield* bus.publish(SessionEvent.Tool.Success, { + sessionID: parent.id, + assistantMessageID, + id: "call_launch", + content: [{ type: "text", text: "started" }], + metadata: { sessionID: child, status: "running" }, + executed: true, + }) + // The child reported back through a foreground follow-up before the fork. + yield* bus.publish(SessionEvent.Tool.Input.Started, { + sessionID: parent.id, + assistantMessageID, + id: "call_followup", + name: "subagent", + }) + yield* bus.publish(SessionEvent.Tool.Input.Ended, { + sessionID: parent.id, + assistantMessageID, + id: "call_followup", + text: JSON.stringify({ sessionID: child, description: "review the diff" }), + }) + yield* bus.publish(SessionEvent.Tool.Called, { + sessionID: parent.id, + assistantMessageID, + id: "call_followup", + input: { sessionID: child, description: "review the diff" }, + executed: true, + }) + yield* bus.publish(SessionEvent.Tool.Success, { + sessionID: parent.id, + assistantMessageID, + id: "call_followup", + content: [{ type: "text", text: `\ndone\n` }], + metadata: { sessionID: child, status: "completed" }, + executed: true, + }) + yield* bus.publish(SessionEvent.Step.Ended, { + sessionID: parent.id, + assistantMessageID, + finish: "stop", + cost: Money.USD.make(0), + tokens: { input: 1, output: 1, reasoning: 0, cache: { read: 0, write: 0 } }, + }) + + const forked = yield* session.fork({ sessionID: parent.id }) + yield* SessionInbox.promote(db, bus, forked.id, "steer") + const notice = (yield* session.context(forked.id)).at(-1) + + expect(notice).toMatchObject({ type: "synthetic" }) + const text = notice?.type === "synthetic" ? notice.text : "" + expect(text).not.toContain(child) + expect(text).not.toContain("Don't wait for these") + }), + ) + it.effect("keeps a fork untitled when its parent is untitled", () => Effect.gen(function* () { const session = yield* Session.Service