mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
fix(core): collect fork notices within the bound-parameter limit
A page of 100 assistant rows bound 102 parameters in the fork notice's own part query, so a large fork was persisted without a notice. Collect now merges parts through SessionHistory.withParts, which also returns the last copied row's time, dropping both the separate copied-through query and the redundant fork guard in the subagent tool.
This commit is contained in:
@@ -381,23 +381,13 @@ const layer = Layer.effect(
|
||||
})
|
||||
.pipe(Effect.provideService(SessionBlob.ForkMedia, media))
|
||||
const forked = yield* result.get(sessionID).pipe(Effect.orDie)
|
||||
// The copy is the only writer before this point and the notice is admitted after it, so the fork's newest
|
||||
// row is the last copied message. A `before` boundary is the first message left behind, and for a running
|
||||
// assistant or shell the boundary row is skipped by the copy's settlement filter, so neither is the copy's
|
||||
// last message. A fork that copied nothing has no row at all, which is what `cutoff` undefined means.
|
||||
const copied = yield* db
|
||||
.select({ time: SessionMessageTable.time_created })
|
||||
.from(SessionMessageTable)
|
||||
.where(eq(SessionMessageTable.session_id, sessionID))
|
||||
.orderBy(desc(SessionMessageTable.seq))
|
||||
.limit(1)
|
||||
.get()
|
||||
.pipe(Effect.orDie)
|
||||
// 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 children = yield* store.list({ parentID: parent.id }).pipe(Effect.orDie)
|
||||
const subagents = yield* ForkNotice.collect({ db, sessionID, children, jobs, cutoff: copied?.time })
|
||||
// The copy is the only writer before this point, so the scan's last row is the last copied message: a
|
||||
// `before` boundary is the first message left behind, and a running assistant or shell at the boundary is
|
||||
// skipped by the copy's settlement filter. 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 { subagents, copiedThrough } = yield* ForkNotice.collect({ db, sessionID, children, jobs })
|
||||
yield* result
|
||||
.synthetic({
|
||||
sessionID,
|
||||
@@ -405,7 +395,7 @@ const layer = Layer.effect(
|
||||
text: ForkNotice.text({
|
||||
forkOf: parent.id,
|
||||
title: parent.title,
|
||||
copiedThrough: copied?.time,
|
||||
copiedThrough,
|
||||
subagents,
|
||||
}),
|
||||
metadata: { source: "fork", forkOf: parent.id },
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
export * as ForkNotice from "./fork-notice.js"
|
||||
|
||||
import { DateTime, Effect } from "effect"
|
||||
import { and, asc, eq, gt, inArray } from "drizzle-orm"
|
||||
import { and, asc, eq, gt } 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 { SessionHistory } from "./history.js"
|
||||
import type { SessionMessage } from "./message.js"
|
||||
import { SessionMessageTable, SessionPartTable } from "./sql.js"
|
||||
import { SessionMessageTable } from "./sql.js"
|
||||
|
||||
export type Subagent = {
|
||||
readonly sessionID: string
|
||||
@@ -23,12 +23,10 @@ const closing =
|
||||
/** 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: launches in the copied history with no later
|
||||
* report, and children the copied history can reach that still hold a running subagent job.
|
||||
* report, and children the copied history can reach that still hold a running subagent job. Returns the creation
|
||||
* time of the last copied message too, which the notice names; a fork that copied nothing has none.
|
||||
*
|
||||
* 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.
|
||||
@@ -38,13 +36,12 @@ export const collect = (input: {
|
||||
readonly sessionID: Session.ID
|
||||
readonly children: readonly Session.Info[]
|
||||
readonly jobs: Pick<Job.Interface, "get">
|
||||
/** Creation time of the last message copied into the fork; undefined when the fork copied nothing. */
|
||||
readonly cutoff?: number
|
||||
}): Effect.Effect<readonly Subagent[]> =>
|
||||
}): Effect.Effect<{ readonly subagents: readonly Subagent[]; readonly copiedThrough?: number }> =>
|
||||
Effect.gen(function* () {
|
||||
// Map order keeps transcript order for launched entries while deduping by child.
|
||||
const subagents = new Map<string, Subagent>()
|
||||
let cursor = -1
|
||||
let copiedThrough: number | undefined
|
||||
while (true) {
|
||||
const rows = yield* input.db
|
||||
.select()
|
||||
@@ -55,23 +52,24 @@ export const collect = (input: {
|
||||
.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) {
|
||||
const last = rows.at(-1)!
|
||||
cursor = last.seq
|
||||
copiedThrough = last.time_created
|
||||
for (const row of yield* SessionHistory.withParts(input.db, 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)
|
||||
if (!("content" in row.data) || !Array.isArray(row.data.content)) continue
|
||||
for (const part of row.data.content) fold(subagents, part)
|
||||
}
|
||||
}
|
||||
for (const child of input.children) {
|
||||
// A child created after the fork point is unreachable from the copied history, even while it runs.
|
||||
if (input.cutoff === undefined) continue
|
||||
if (DateTime.toEpochMillis(child.time.created) > input.cutoff) continue
|
||||
if (copiedThrough === undefined) continue
|
||||
if (DateTime.toEpochMillis(child.time.created) > copiedThrough) continue
|
||||
const job = yield* input.jobs.get(child.id)
|
||||
// Finished jobs stay in the in-memory map, so only a running job is outstanding.
|
||||
if (job?.type !== "subagent" || job.status !== "running") continue
|
||||
@@ -81,35 +79,10 @@ export const collect = (input: {
|
||||
state: job.status,
|
||||
})
|
||||
}
|
||||
return [...subagents.values()]
|
||||
return { subagents: [...subagents.values()], copiedThrough }
|
||||
})
|
||||
|
||||
/**
|
||||
* 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<MessageRow>) =>
|
||||
Effect.gen(function* () {
|
||||
const partsByMessage = new Map<SessionMessage.ID, Part[]>()
|
||||
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<string, Subagent>, part: Part) => {
|
||||
const fold = (subagents: Map<string, Subagent>, part: SessionMessage.AssistantContentEncoded) => {
|
||||
if (part.type === "text") {
|
||||
forget(subagents, part.text)
|
||||
return
|
||||
@@ -118,31 +91,20 @@ const fold = (subagents: Map<string, Subagent>, part: Part) => {
|
||||
// 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)
|
||||
const launched = launch(part.state)
|
||||
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.
|
||||
* A background launch. `subagent.ts` always writes `{ sessionID, status }`, and a result that is still running is a
|
||||
* launch, whether the call asked to run in the background or was moved there before the fork.
|
||||
*/
|
||||
const launch = (
|
||||
state: Exclude<SessionMessage.ToolState, { status: "streaming" }>,
|
||||
content: ReadonlyArray<{ readonly type: string; readonly text?: string }>,
|
||||
) => {
|
||||
const launch = (state: Exclude<SessionMessage.ToolState, { status: "streaming" }>) => {
|
||||
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
|
||||
if (metadata?.status !== "running" || typeof metadata.sessionID !== "string") return
|
||||
const description = state.input.description
|
||||
return { sessionID, description: typeof description === "string" ? description : "subagent" }
|
||||
return { sessionID: metadata.sessionID, description: typeof description === "string" ? description : "subagent" }
|
||||
}
|
||||
|
||||
/** Matches the report `SubagentCompletion` delivers, keeping the closing quote so one child id can't prefix another. */
|
||||
|
||||
@@ -161,13 +161,16 @@ export const Plugin = {
|
||||
new ToolFailure({ message: `Subagent session not found: ${input.sessionID}`, error }),
|
||||
),
|
||||
)
|
||||
if (existing !== undefined && existing.parentID !== context.sessionID)
|
||||
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
|
||||
return yield* new ToolFailure({
|
||||
message:
|
||||
existing.parentID !== undefined && parent.fork?.sessionID === existing.parentID
|
||||
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} is not a child of the current session`,
|
||||
})
|
||||
}
|
||||
const override = input.model === undefined ? undefined : yield* resolveModel(input.model)
|
||||
// Continuing with a different agent switches the child, mirroring create semantics
|
||||
// where an explicit model wins over the agent's configured model, which wins over the inherited one.
|
||||
|
||||
@@ -1064,6 +1064,38 @@ describe("Session.create", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
durableIt.effect("admits a fork notice when the copy fills a statement", () =>
|
||||
Effect.gen(function* () {
|
||||
const session = yield* Session.Service
|
||||
const transfer = yield* SessionTransfer.Service
|
||||
const template = yield* session.create({ location })
|
||||
const sessionID = Session.ID.create()
|
||||
// One collect page, and a merged-parts read that binds exactly the 100 ids its statement holds.
|
||||
const messages = Array.from({ length: 101 }, (_, index) =>
|
||||
SessionMessage.Assistant.make({
|
||||
id: SessionMessage.ID.create(),
|
||||
type: "assistant",
|
||||
agent: Agent.defaultID,
|
||||
model: { id: Model.ID.make("model"), providerID: Provider.ID.make("provider") },
|
||||
content: [{ type: "text" as const, text: `step ${index}` }],
|
||||
time: { created: DateTime.makeUnsafe(index), completed: DateTime.makeUnsafe(index) },
|
||||
}),
|
||||
)
|
||||
|
||||
yield* transfer.import({ data: { info: { ...template, id: sessionID }, messages }, location })
|
||||
|
||||
const forked = yield* session.fork({ sessionID })
|
||||
const notice = (yield* session.inbox(forked.id)).filter((item) => item.type === "synthetic")
|
||||
const text = notice[0]?.type === "synthetic" ? notice[0].payload.text : ""
|
||||
|
||||
expect(notice).toHaveLength(1)
|
||||
expect(text).toContain(
|
||||
`This session is a fork of ${sessionID} (untitled), copied through its message at ${DateTime.formatIso(DateTime.makeUnsafe(100))}`,
|
||||
)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user