Compare commits

...
Author SHA1 Message Date
paseo-bot[bot] 71eca18d56 test(server): reproduce an early finish push from a remote child
Hold the child after permission approval and record every finish push on the parent host. The parent pushes before the child completes.
2026-11-21 08:56:03 +00:00
xiaobaifly7 1d9ca6cb12 fix(server): wait for delegated agents before sending a finish
A permission update from a child wakes its parent. When the parent
returns to idle, it is eligible for finish attention once its busy
children finish. Use the same condition for a new unread finish and
a retry of its notification.

Co-authored-by: xiaobaifly7 <196511160+xiaobaifly7@users.noreply.github.com>
2026-11-21 08:48:45 +00:00
3 changed files with 101 additions and 2 deletions
@@ -9542,6 +9542,76 @@ test("onAgentAttention is not called for delegated child agents", async () => {
expect(attentionCalls).toEqual([]);
});
test.each(["read", "unread"])(
"waits for a delegated agent before reporting a finish (earlier attention is %s)",
async (attentionState) => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-delegated-finish-"));
const config = { provider: "codex", cwd: workdir } as const;
const client = new SessionRecordingAgentClient();
const finishes: Array<{ agentId: string; attentionTimestamp: Date | null }> = [];
const manager = new AgentManager({
clients: { codex: client },
registry: new AgentStorage(join(workdir, "agents"), logger),
logger,
onAgentAttention: ({ agentId, reason, attentionTimestamp }) => {
if (reason === "finished") finishes.push({ agentId, attentionTimestamp });
},
});
const parent = await manager.createAgent(config, undefined, { workspaceId: undefined });
const child = await manager.createAgent(config, undefined, {
labels: { [PARENT_AGENT_ID_LABEL]: parent.id },
workspaceId: undefined,
});
onTestFinished(async () => {
await manager.closeAgent(child.id);
await manager.closeAgent(parent.id);
await manager.flush();
rmSync(workdir, { recursive: true, force: true });
});
const [parentSession, childSession] = client.sessions;
async function completeParentTurn(turnId: string) {
const running = waitForAgentLifecycle(manager, parent.id, "running");
parentSession.pushEvent({ type: "turn_started", provider: "codex", turnId });
await running;
const idle = waitForAgentLifecycle(manager, parent.id, "idle");
parentSession.pushEvent({ type: "turn_completed", provider: "codex", turnId });
await idle;
}
if (attentionState === "unread") {
await completeParentTurn("earlier-parent-finish");
}
const earlierFinishes = [...finishes];
const earlierAttention = manager.getAgent(parent.id)?.attention;
const childRunning = waitForAgentLifecycle(manager, child.id, "running");
const childTurn = { provider: "codex", turnId: "child-turn" } as const;
childSession.pushEvent({ ...childTurn, type: "turn_started" });
await childRunning;
await completeParentTurn("parent-handles-child-update");
expect(finishes).toEqual(earlierFinishes);
expect(manager.getAgent(parent.id)?.attention).toEqual(earlierAttention);
const childIdle = waitForAgentLifecycle(manager, child.id, "idle");
childSession.pushEvent({ ...childTurn, type: "turn_completed" });
await childIdle;
await completeParentTurn("parent-handles-child-finish");
expect(finishes).toEqual([
...earlierFinishes,
{
agentId: parent.id,
attentionTimestamp: earlierFinishes[0]?.attentionTimestamp ?? expect.any(Date),
},
]);
expect(manager.getAgent(parent.id)?.attention).toMatchObject({
requiresAttention: true,
attentionReason: "finished",
});
},
);
test("clearAgentAttention on errored agent stays cleared until a new error transition", async () => {
const workdir = mkdtempSync(join(tmpdir(), "agent-manager-attention-error-"));
const storagePath = join(workdir, "agents");
@@ -5920,7 +5920,10 @@ export class AgentManager {
return;
}
const finished = previousStatus === "running" && currentStatus === "idle";
const finished =
previousStatus === "running" &&
currentStatus === "idle" &&
(isDelegatedAgent(agent) || !this.hasBusyChildAgent(agent.id));
if (agent.attention.requiresAttention) {
// The unread finish stays the same; the notifier knows whether the user was told about it.
if (finished && agent.attention.attentionReason === "finished") {
@@ -5939,6 +5942,15 @@ export class AgentManager {
}
}
private hasBusyChildAgent(agentId: string): boolean {
for (const child of this.agents.values()) {
if (isAgentBusy(child.lifecycle) && getParentAgentIdFromLabels(child.labels) === agentId) {
return true;
}
}
return false;
}
// The failed state is only shown while error attention is unread, so every new failure raises
// it, including one that keeps the lifecycle at "error". An error outranks an unread finish,
// which would otherwise make the failure look green.
@@ -2179,10 +2179,12 @@ test(
);
test(
"remote finish observation survives a permission checkpoint until resumed completion",
"a remote child permission update does not cause an early parent finish push",
{ timeout: 30000 },
async () => {
const parentPrompts: string[] = [];
const finishPushes: string[] = [];
let childReleased = false;
const finish = Promise.withResolvers<void>();
cleanup.push(async () => {
finish.resolve();
@@ -2190,6 +2192,15 @@ test(
const a = await daemon({
mcpEnabled: true,
logger: completionLogger(),
pushNotificationSender: {
async send(payload) {
if (payload.title === "Agent finished") {
finishPushes.push(
childReleased ? "after the child completed" : "while the child was running",
);
}
},
},
agentClients: createTestAgentClients({
onStartTurn: (prompt) => {
if (String(prompt).includes('kind="agent-notification"'))
@@ -2247,6 +2258,10 @@ test(
});
expect(parentPrompts).toHaveLength(1);
expect((await destination.client.fetchAgent(childId))?.agent.status).toBe("running");
await expect
.poll(async () => (await origin.client.fetchAgent(parent.id))?.agent.status)
.toBe("idle");
childReleased = true;
finish.resolve();
await expect.poll(() => parentPrompts, { timeout: 10000 }).toHaveLength(2);
expect(parentPrompts[1]).toContain('event="finished"');
@@ -2261,6 +2276,8 @@ test(
},
)
.toEqual([]);
await expect.poll(() => finishPushes.length).toBeGreaterThan(0);
expect(finishPushes).toEqual(["after the child completed"]);
},
);