fix(core): list only reachable subagents in a fork notice

This commit is contained in:
Kit Langton
2026-11-04 20:11:48 -08:00
Unverified
parent c3d1b3f728
commit 26a27c65a3
3 changed files with 243 additions and 169 deletions
+20 -3
View File
@@ -340,7 +340,7 @@ const layer = Layer.effect(
fork: Effect.fn("Session.fork")(function* (input) {
const parent = yield* result.get(input.sessionID)
const boundary = yield* db
.select({ id: SessionMessageTable.id, time: SessionMessageTable.time_created })
.select({ id: SessionMessageTable.id })
.from(SessionMessageTable)
.where(
and(
@@ -381,16 +381,33 @@ 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 })
const subagents = yield* ForkNotice.collect({ db, sessionID, children, jobs, cutoff: copied?.time })
yield* result
.synthetic({
sessionID,
description: "Fork notice",
text: ForkNotice.text({ forkOf: parent.id, title: parent.title, time: boundary.time, subagents }),
text: ForkNotice.text({
forkOf: parent.id,
title: parent.title,
copiedThrough: copied?.time,
subagents,
}),
metadata: { source: "fork", forkOf: parent.id },
resume: false,
})
+12 -4
View File
@@ -28,7 +28,7 @@ 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 that still hold a running subagent job.
* report, and children the copied history can reach 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.
@@ -38,6 +38,8 @@ 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.gen(function* () {
// Map order keeps transcript order for launched entries while deduping by child.
@@ -67,6 +69,9 @@ export const collect = (input: {
}
}
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
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
@@ -148,13 +153,16 @@ const forget = (subagents: Map<string, Subagent>, text: string) => {
export const text = (input: {
readonly forkOf: Session.ID
readonly title?: string
readonly time: number
/** Creation time of the last message copied into the fork; undefined when the fork copied nothing. */
readonly copiedThrough?: number
readonly subagents: readonly Subagent[]
}) => {
const title = input.title === undefined ? "untitled" : `"${input.title}"`
const opening = `This session is a fork of ${input.forkOf} (${title})`
const first =
`This session is a fork of ${input.forkOf} (${title}), copied through its message at ` +
`${DateTime.formatIso(DateTime.makeUnsafe(input.time))}; work started before the fork keeps reporting to the original, not here.`
input.copiedThrough === undefined
? `${opening}. It starts with none of the original's messages; work started before the fork keeps reporting to the original, not here.`
: `${opening}, copied through its message at ${DateTime.formatIso(DateTime.makeUnsafe(input.copiedThrough))}; work started before the fork keeps reporting to the original, not here.`
if (input.subagents.length === 0) return first
return [
first,
+211 -162
View File
@@ -120,6 +120,85 @@ const logEvents = (session: Session.Interface, sessionID: Session.ID, follow?: b
.log({ sessionID, follow })
.pipe(Stream.filter((item): item is SessionEvent.DurableEvent => !Bus.isSynced(item)))
type TextPart = { readonly type: "text"; readonly text: string }
type SubagentCall = {
readonly callID: string
/** The input the model sent; `background: true` asks for a background launch. */
readonly input: Record<string, unknown>
/** Defaults to a plain "started" text part. */
readonly content?: readonly [TextPart, ...TextPart[]]
readonly metadata?: Record<string, string>
/** The child whose completion arrives before the step settles, as a synthetic report. */
readonly report?: Session.ID
}
const started: readonly [TextPart] = [{ type: "text", text: "started" }]
/** Publishes one settled assistant step whose calls launch subagents, so each history test states only its case. */
const subagentStep = (input: { readonly sessionID: Session.ID; readonly calls: ReadonlyArray<SubagentCall> }) =>
Effect.gen(function* () {
const bus = yield* Bus.Service
const session = yield* Session.Service
const { db } = yield* Database.Service
const assistantMessageID = SessionMessage.ID.create()
const publishCall = (call: SubagentCall) =>
Effect.gen(function* () {
yield* bus.publish(SessionEvent.Tool.Input.Started, {
sessionID: input.sessionID,
assistantMessageID,
id: call.callID,
name: "subagent",
})
yield* bus.publish(SessionEvent.Tool.Input.Ended, {
sessionID: input.sessionID,
assistantMessageID,
id: call.callID,
text: JSON.stringify(call.input),
})
yield* bus.publish(SessionEvent.Tool.Called, {
sessionID: input.sessionID,
assistantMessageID,
id: call.callID,
input: call.input,
executed: true,
})
yield* bus.publish(SessionEvent.Tool.Success, {
sessionID: input.sessionID,
assistantMessageID,
id: call.callID,
content: call.content ?? started,
metadata: call.metadata,
executed: true,
})
})
yield* bus.publish(SessionEvent.Step.Started, {
sessionID: input.sessionID,
assistantMessageID,
agent: Agent.ID.make("build"),
model: Model.Ref.make({ id: Model.ID.make("model"), providerID: Provider.ID.make("provider") }),
started: 0,
})
// Callers promote their opening prompt first, so a report promoted here only consumes the report.
for (const call of input.calls) {
yield* publishCall(call)
if (call.report === undefined) continue
yield* session.synthetic({
sessionID: input.sessionID,
text: `<subagent sessionID="${call.report}" state="completed">\ndone\n</subagent>`,
resume: false,
})
yield* SessionInbox.promote(db, bus, input.sessionID, "steer")
}
yield* bus.publish(SessionEvent.Step.Ended, {
sessionID: input.sessionID,
assistantMessageID,
finish: "stop",
cost: Money.USD.make(0),
tokens: { input: 1, output: 1, reasoning: 0, cache: { read: 0, write: 0 } },
})
})
const assertCreateInputTypes = (session: Session.Interface) => {
// @ts-expect-error location or parentID is required.
session.create({})
@@ -785,62 +864,24 @@ describe("Session.create", () => {
const parent = yield* session.create({ location })
yield* session.prompt({ sessionID: parent.id, text: "Launch subagents", resume: false })
yield* SessionInbox.promote(db, bus, parent.id, "steer")
const assistantMessageID = SessionMessage.ID.create()
const model = Model.Ref.make({ id: Model.ID.make("model"), providerID: Provider.ID.make("provider") })
const unreported = Session.ID.create()
const reported = Session.ID.create()
const launch = (id: string, sessionID: Session.ID, description: string) =>
Effect.gen(function* () {
yield* bus.publish(SessionEvent.Tool.Input.Started, {
sessionID: parent.id,
assistantMessageID,
id,
name: "subagent",
})
yield* bus.publish(SessionEvent.Tool.Input.Ended, {
sessionID: parent.id,
assistantMessageID,
id,
text: JSON.stringify({ background: true, description }),
})
yield* bus.publish(SessionEvent.Tool.Called, {
sessionID: parent.id,
assistantMessageID,
id,
input: { background: true, description },
executed: true,
})
yield* bus.publish(SessionEvent.Tool.Success, {
sessionID: parent.id,
assistantMessageID,
id,
content: [{ type: "text", text: "started" }],
metadata: { sessionID, status: "running" },
executed: true,
})
})
yield* bus.publish(SessionEvent.Step.Started, {
yield* subagentStep({
sessionID: parent.id,
assistantMessageID,
agent: Agent.ID.make("build"),
model,
started: 0,
})
yield* launch("call_unreported", unreported, "review the diff")
yield* launch("call_reported", reported, "check the tests")
yield* session.synthetic({
sessionID: parent.id,
text: `<subagent sessionID="${reported}" state="completed">\ndone\n</subagent>`,
resume: false,
})
yield* SessionInbox.promote(db, bus, parent.id, "steer")
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 } },
calls: [
{
callID: "call_unreported",
input: { background: true, description: "review the diff" },
metadata: { sessionID: unreported, status: "running" },
},
{
callID: "call_reported",
input: { background: true, description: "check the tests" },
metadata: { sessionID: reported, status: "running" },
report: reported,
},
],
})
const forked = yield* session.fork({ sessionID: parent.id })
@@ -862,55 +903,24 @@ describe("Session.create", () => {
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, {
yield* subagentStep({
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: [
calls: [
{
type: "text",
text: `The subagent is working in the background (sessionID: ${child}). You will be notified automatically when it finishes.`,
callID: "call_backgrounded",
// No `background: true`: the foreground call was moved to the background while it blocked.
input: { description: "review the diff" },
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" },
},
],
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 })
@@ -931,77 +941,24 @@ describe("Session.create", () => {
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, {
yield* subagentStep({
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: `<subagent sessionID="${child}" state="completed">\ndone\n</subagent>` }],
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 } },
calls: [
{
callID: "call_launch",
input: { background: true, description: "review the diff" },
metadata: { sessionID: child, status: "running" },
},
{
callID: "call_followup",
input: { sessionID: child, description: "review the diff" },
content: [{ type: "text", text: `<subagent sessionID="${child}" state="completed">\ndone\n</subagent>` }],
metadata: { sessionID: child, status: "completed" },
},
],
})
const forked = yield* session.fork({ sessionID: parent.id })
@@ -1015,6 +972,98 @@ describe("Session.create", () => {
}),
)
it.effect("names only subagents created at or before the last copied message", () =>
Effect.gen(function* () {
const session = yield* Session.Service
const bus = yield* Bus.Service
const jobs = yield* Job.Service
const { db } = yield* Database.Service
const parent = yield* session.create({ location, title: "Original" })
// A running job stays in the in-memory map, so the cutoff alone decides whether a child is reachable.
const run = (child: Session.ID, description: string) =>
jobs.start({
id: child,
type: "subagent",
title: description,
recovery: {
kind: "subagent",
parentSessionID: parent.id,
childSessionID: child,
agent: "reviewer",
description,
},
run: Effect.never,
})
const earlier = yield* session.create({ parentID: parent.id, title: "earlier review" })
yield* run(earlier.id, "review the diff")
yield* TestClock.setTime(1_000)
yield* session.prompt({ sessionID: parent.id, text: "First", resume: false })
yield* SessionInbox.promote(db, bus, parent.id, "steer")
yield* TestClock.setTime(2_000)
const second = yield* session.prompt({ sessionID: parent.id, text: "Second", resume: false })
yield* SessionInbox.promote(db, bus, parent.id, "steer")
yield* TestClock.setTime(3_000)
const later = yield* session.create({ parentID: parent.id, title: "later review" })
yield* run(later.id, "check the tests")
const forked = yield* session.fork({ sessionID: parent.id, before: second.id })
const notice = (yield* session.inbox(forked.id)).find((item) => item.type === "synthetic")
const text = notice?.type === "synthetic" ? notice.payload.text : ""
expect(text).toContain(`- ${earlier.id} "review the diff" (running)`)
expect(text).not.toContain(later.id)
// The copied history ends at First; Second is the boundary the fork left behind.
expect(text).toContain(`copied through its message at ${DateTime.formatIso(DateTime.makeUnsafe(1_000))}`)
expect(text).not.toContain(DateTime.formatIso(DateTime.makeUnsafe(2_000)))
yield* jobs.cancel(earlier.id)
yield* jobs.cancel(later.id)
}),
)
it.effect("tells a fork that starts with none of the original's messages", () =>
Effect.gen(function* () {
const session = yield* Session.Service
const bus = yield* Bus.Service
const jobs = yield* Job.Service
const { db } = yield* Database.Service
const parent = yield* session.create({ location, title: "Original" })
// Created at the clock's start, so a cutoff defaulted to 0 would wrongly keep it.
const child = yield* session.create({ parentID: parent.id, title: "background review" })
yield* jobs.start({
id: child.id,
type: "subagent",
title: "review the diff",
recovery: {
kind: "subagent",
parentSessionID: parent.id,
childSessionID: child.id,
agent: "reviewer",
description: "review the diff",
},
run: Effect.never,
})
yield* TestClock.setTime(1_000)
const first = yield* session.prompt({ sessionID: parent.id, text: "First", resume: false })
yield* SessionInbox.promote(db, bus, parent.id, "steer")
const forked = yield* session.fork({ sessionID: parent.id, before: first.id })
const notice = (yield* session.inbox(forked.id)).find((item) => item.type === "synthetic")
const text = notice?.type === "synthetic" ? notice.payload.text : ""
expect(text).toContain(
`This session is a fork of ${parent.id} ("Original"). It starts with none of the original's messages`,
)
expect(text).toContain("work started before the fork keeps reporting to the original, not here.")
expect(text).not.toContain("copied through")
expect(text).not.toContain(child.id)
expect(yield* session.context(forked.id)).toEqual([])
yield* jobs.cancel(child.id)
}),
)
it.effect("keeps a fork untitled when its parent is untitled", () =>
Effect.gen(function* () {
const session = yield* Session.Service