fix(server): preserve plugin child outcomes after close (#8563)

* fix(server): keep a plugin child's canceled or failed turn on close

A plugin child that ended its turn as canceled or failed and then sent a
plain session.closed was upserted as completed. Remember the outcome of
the child's latest finished turn and use it when it closes without an
error.

* fix(server): clear plugin child outcomes when replaying a branch

Keep the close-status fix compatible with reused-child running updates and discard remembered outcomes when rewind replaces a binding. Cover terminal close states through the agent API and replay.

Co-authored-by: hadan8977 <zc-huang23@mails.tsinghua.edu.cn>

---------

Co-authored-by: hadan8977 <zc-huang23@mails.tsinghua.edu.cn>
Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
This commit is contained in:
paseo-bot[bot]andhadan8977 authored and GitHub committed 2026-11-14 19:59:39 +00:00
1 parent 6e2fee462f
commit 2bd9e9d87f
2 files changed
+164 -1

No files matched your search

@@ -496,6 +496,58 @@ async function pluginMessages(session: Awaited<ReturnType<AgentClient["createSes
);
}
test("plugin rewind discards child outcomes from the withdrawn branch", async () => {
await withPluginRewindSession(
{
async handleInput(input, emit) {
if (input.type !== "session.open" || input.history !== "replay") return false;
emit({
type: "session.opened",
requestId: input.requestId,
sessionId: input.sessionId,
capabilities: CAPABILITIES,
restoration: "core",
persistence: input.persistence,
cwd: input.config.cwd,
});
emit({
type: "session.opened",
sessionId: "rewound-child",
parentSessionId: input.sessionId,
capabilities: [],
restoration: "parent",
cwd: input.config.cwd,
});
emit({ type: "session.closed", sessionId: "rewound-child" });
emit({ type: "session.ready", requestId: input.requestId, sessionId: input.sessionId });
return true;
},
},
async (session, harness) => {
const open = harness.inputs.find((input) => input.type === "session.open")!;
harness.emit({
type: "session.opened",
sessionId: "rewound-child",
parentSessionId: open.sessionId,
capabilities: [],
restoration: "parent",
cwd: tmpdir(),
});
harness.emit({
type: "session.turn",
sessionId: "rewound-child",
turnId: "withdrawn-turn",
state: "failed",
});
await session.revertConversation!({ messageId: "user-B" });
const history = await Array.fromAsync(session.streamHistory());
expect(history.findLast((event) => event.type === "provider_subagent")).toMatchObject({
event: { type: "upsert", id: "rewound-child", status: "completed" },
});
},
);
});
test("combined plugin rewind replays the retained native branch", async () => {
await withPluginRewindSession({}, async (session) => {
await session.revertBoth!({ messageId: "user-B" });
@@ -1690,6 +1742,11 @@ describe("PluginAgentClientRegistry", () => {
expect(manager.listProviderSubagents(agent.id)).toMatchObject([
{ id: child.id, title: child.title, status: outcome },
]);
harness.emit({ type: "session.closed", sessionId: "reusable-child" });
await nextTurn();
expect(manager.listProviderSubagents(agent.id)).toMatchObject([
{ id: child.id, title: child.title, status: outcome },
]);
} finally {
await Promise.all(manager.listAgents().map((agent) => manager.closeAgent(agent.id)));
await plugins.shutdown();
@@ -1724,6 +1781,7 @@ describe("PluginAgentClientRegistry", () => {
turnId: "second-turn",
state,
});
emit({ type: "session.closed", sessionId: "reusable-child" });
}
},
});
@@ -1750,6 +1808,100 @@ describe("PluginAgentClientRegistry", () => {
}
},
);
test("keeps a child's canceled or failed turn when the child closes normally", async () => {
const harness = createProviderHarness();
const registry = new PluginAgentClientRegistry(createTestLogger());
registry.replace([harness.registration]);
const session = await registry.clients()[harness.registration.id]!.createSession({
provider: harness.registration.id,
cwd: "/workspace",
});
try {
const events: AgentStreamEvent[] = [];
session.subscribe((event) => events.push(event));
const open = harness.inputs.find((input) => input.type === "session.open")!;
const outcomes = [
["child-canceled", "canceled"],
["child-failed", "failed"],
["child-completed", "completed"],
] as const;
for (const [sessionId, state] of outcomes) {
harness.emit({
type: "session.opened",
sessionId,
parentSessionId: open.sessionId,
capabilities: [],
restoration: "parent",
cwd: "/workspace",
});
harness.emit({ type: "session.turn", sessionId, turnId: "turn", state: "started" });
harness.emit({ type: "session.turn", sessionId, turnId: "turn", state });
harness.emit({ type: "session.closed", sessionId });
}
const lastStatus = (id: string) =>
events
.flatMap((event) =>
event.type === "provider_subagent" &&
event.event.type === "upsert" &&
event.event.id === id
? [event.event.status]
: [],
)
.at(-1);
expect(lastStatus("child-canceled")).toBe("canceled");
expect(lastStatus("child-failed")).toBe("failed");
expect(lastStatus("child-completed")).toBe("completed");
} finally {
await session.close();
registry.replace([]);
}
});
test.each([
{ turns: [], error: false, expected: "completed" },
{ turns: ["canceled", "started"], error: false, expected: "completed" },
{ turns: ["failed", "started", "completed"], error: false, expected: "completed" },
{ turns: ["canceled"], error: true, expected: "failed" },
] as const)(
"closes a plugin child using its latest turn: $turns, error=$error",
async ({ turns, error, expected }) => {
const harness = createProviderHarness();
const registry = new PluginAgentClientRegistry(createTestLogger());
registry.replace([harness.registration]);
try {
const session = await registry.clients()[harness.registration.id]!.createSession({
provider: harness.registration.id,
cwd: tmpdir(),
});
const events: AgentStreamEvent[] = [];
session.subscribe((event) => events.push(event));
const open = harness.inputs.find((input) => input.type === "session.open")!;
harness.emit({
type: "session.opened",
sessionId: "close-control",
parentSessionId: open.sessionId,
capabilities: [],
restoration: "parent",
cwd: tmpdir(),
});
for (const state of turns) {
harness.emit({ type: "session.turn", sessionId: "close-control", turnId: "turn", state });
}
harness.emit({
type: "session.closed",
sessionId: "close-control",
...(error ? { error: { message: "Provider failed to close" } } : {}),
});
expect(events.at(-1)).toMatchObject({
type: "provider_subagent",
event: { type: "upsert", id: "close-control", status: expected },
});
} finally {
await registry.shutdown();
}
},
);
test("gates persistence operations on negotiated provider capabilities", async () => {
const harness = createProviderHarness({ capabilities: ["session.persistence"] });
@@ -1202,6 +1202,8 @@ class PluginAgentSession implements AgentSession {
{ session: ProviderRuntimeSession; unsubscribe: () => void }
>();
private readonly childSnapshots = new Map<string, Map<string, TimelineTextSnapshot>>();
// Outcome of each child's latest finished turn, so a plain close does not report it as completed.
private readonly childTurnOutcomes = new Map<string, "completed" | "canceled" | "failed">();
private unsubscribe: (() => void) | null = null;
/** How the opened session differs from the options it was given. */
readonly openOptionsPatch: Record<string, JsonValue>;
@@ -1766,6 +1768,11 @@ class PluginAgentSession implements AgentSession {
return;
}
if (event.type === "session.turn") {
if (event.state === "started") {
this.childTurnOutcomes.delete(childId);
} else {
this.childTurnOutcomes.set(childId, event.state);
}
this.publish({
type: "provider_subagent",
provider: this.provider,
@@ -1784,7 +1791,10 @@ class PluginAgentSession implements AgentSession {
event: {
type: "upsert",
id: childId,
status: event.type === "session.runtime_failed" || event.error ? "failed" : "completed",
status:
event.type === "session.runtime_failed" || event.error
? "failed"
: (this.childTurnOutcomes.get(childId) ?? "completed"),
},
});
}
@@ -1802,6 +1812,7 @@ class PluginAgentSession implements AgentSession {
this.history.clear();
this.timelineSnapshots.clear();
this.childSnapshots.clear();
this.childTurnOutcomes.clear();
this.revertTokens.clear();
this.pendingPermissions.clear();
this.permissionResponses.clear();