Compare commits

...
Author SHA1 Message Date
paseo-bot[bot] 7f3e197c0d test(server): wait for the recursive watcher before creating a file
macOS can start delivering events after the native watcher starts. Verify liveness before creating the fixture and clear the probe from the inventory before checking the file count.
2026-11-21 09:00:53 +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 89 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.
@@ -2,6 +2,7 @@ import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { expect, test } from "vitest";
import { createWatcherLivenessCanary } from "../watcher-liveness-canary.js";
import type { FileChange } from "./index.js";
import { createNativeRecursiveBackend } from "./internal/native-recursive.js";
import { createObserverPaths } from "./internal/paths.js";
@@ -9,6 +10,7 @@ import { createObserverPaths } from "./internal/paths.js";
test("the recursive watcher remembers newly announced files before reconciliation", async () => {
const root = await mkdtemp(join(tmpdir(), "paseo-observer-native-"));
const paths = createObserverPaths(process.platform);
const canary = createWatcherLivenessCanary(root);
const changes: FileChange[] = [];
let active = true;
const backend = createNativeRecursiveBackend(
@@ -34,7 +36,7 @@ test("the recursive watcher remembers newly announced files before reconciliatio
isIgnored: () => false,
isPathInside: paths.isInside,
queueEvent: (type, path) => {
changes.push({ type, path });
changes.push(...canary.filterEvents([{ type, path }]));
},
fail: (error) => {
throw error;
@@ -44,6 +46,9 @@ test("the recursive watcher remembers newly announced files before reconciliatio
);
try {
await backend.start();
await canary.verify();
// Reconcile the removed probe before counting the files announced by the test.
await backend.updateIgnore();
const path = join(root, "new.txt");
await writeFile(path, "created");
await expect