Keep waiting heartbeats out of Running (#10216)

* fix(agents): keep waiting heartbeats out of Running

* fix(protocol): keep the work count default as a wire contract

* test(app): verify heartbeat status before, during, and after its turn
This commit is contained in:
Mohamed Boudra
2026-11-19 18:45:40 +07:00
committed by GitHub
parent 4fb95106e7
commit c951de2332
17 changed files with 136 additions and 32 deletions
+11 -4
View File
@@ -728,7 +728,15 @@ test("a scheduled heartbeat opens a separate turn without reopening the previous
});
expect(created.error).toBeNull();
scheduleId = created.schedule!.id;
const fired = await schedules.scheduleRunOnce({ id: scheduleId });
const running = page.getByTestId("workspace-status-indicator-running");
const tasks = page.getByRole("button", { name: "1 background task", exact: true });
await expect(tasks).toBeVisible();
await expect(running).toHaveCount(0);
const [fired] = await Promise.all([
schedules.scheduleRunOnce({ id: scheduleId }),
expect(running.first()).toBeVisible(),
]);
expect(fired.error).toBeNull();
await expect(
page.getByTestId("tool-call-badge").filter({ hasText: /^Heartbeat/ }),
@@ -743,15 +751,14 @@ test("a scheduled heartbeat opens a separate turn without reopening the previous
await expect(summaries).toHaveCount(2);
await expect.poll(() => rowsBetweenLatestSummaryAndReply(page)).toBe(0);
await expect(page.getByRole("button", { name: /^Worked for/ })).toHaveCount(2);
const tasks = page.getByRole("button", { name: "1 background task", exact: true });
await expect(page.getByTestId("workspace-status-indicator-running").first()).toBeVisible();
await expect(running).toHaveCount(0);
await expect(page.getByRole("button", { name: "Stop agent", exact: true })).toHaveCount(0);
await tasks.click();
const panel = page.getByTestId("background-work-panel");
await expect(panel).toContainText("Heartbeat");
await panel.getByRole("button", { name: "Stop Check the completed work" }).click();
await expect(tasks).toHaveCount(0);
await expect(page.getByTestId("workspace-status-indicator-running")).toHaveCount(0);
await expect(running).toHaveCount(0);
const remaining = await schedules.scheduleList();
expect(remaining.error).toBeNull();
expect(remaining.schedules.map((schedule) => schedule.id)).not.toContain(scheduleId);
@@ -1,6 +1,7 @@
import { findMountedWindowStart } from "@/agent-stream/history-window";
import { z } from "zod";
import {
AgentBackgroundWorkSummarySchema,
AgentStatusSchema,
AgentTimelineItemPayloadSchema,
ForgeWebSchema,
@@ -236,7 +237,7 @@ const StoredAgentSnapshotSchema = z.strictObject({
updatedAt: IsoDateSchema,
lastUserMessageAt: IsoDateSchema.nullable(),
status: AgentStatusSchema,
backgroundWork: z.object({ count: z.number().int().nonnegative() }).optional(),
backgroundWork: AgentBackgroundWorkSummarySchema.optional(),
activeTurn: z
.strictObject({
turnId: z.string(),
+2 -1
View File
@@ -36,6 +36,7 @@ import type {
AgentPersistenceHandle,
} from "@getpaseo/protocol/agent-types";
import type {
AgentBackgroundWorkSummary,
ServerInfoStatusPayload,
ProjectPlacementPayload,
ServerCapabilities,
@@ -78,7 +79,7 @@ export interface Agent {
id: string;
provider: AgentProvider;
status: AgentLifecycleStatus;
backgroundWork?: { count: number };
backgroundWork?: AgentBackgroundWorkSummary;
turn: TurnLiveness;
createdAt: Date;
updatedAt: Date;
@@ -219,3 +219,23 @@ it("warns before archiving an idle subagent with live background work", async ()
message: expect.stringContaining("will stop"),
});
});
it("warns that archiving an idle subagent stops its passive background work", async () => {
const env = createFakeEnv({
initialSubagents: [
{
id: "child",
snapshot: {
title: "Check relay",
status: "idle",
backgroundWork: { count: 1, runningCount: 0 },
},
},
],
});
await requestArchiveSubagent({ serverId: "server-1", subagentId: "child" }, env.deps);
expect(env.recordedConfirmInputs[0]).toMatchObject({
title: "Archive subagent?",
message: "Archiving Check relay will stop its background work and remove it from the track.",
});
});
+10 -4
View File
@@ -1,3 +1,4 @@
import { isAgentRunning } from "@getpaseo/protocol/agent-state-bucket";
import type { Agent } from "@/stores/session-store";
import type { ConfirmDialogInput } from "@/utils/confirm-dialog";
@@ -25,13 +26,18 @@ export function resolveArchiveSubagentDialog(
input: ResolveArchiveSubagentDialogInput,
): ConfirmDialogInput {
const subagentLabel = resolveSubagentLabel(input.title) ?? "this subagent";
const isRunning = input.status === "running" || (input.backgroundWork?.count ?? 0) > 0;
const isRunning = isAgentRunning(input);
let message = `Remove ${subagentLabel} from the track. The subagent will be archived.`;
if (isRunning) {
message = `${subagentLabel} is still running. Archiving it will stop the subagent and remove it from the track.`;
} else if (input.backgroundWork?.count) {
message = `Archiving ${subagentLabel} will stop its background work and remove it from the track.`;
}
return {
title: isRunning ? "Archive running subagent?" : "Archive subagent?",
message: isRunning
? `${subagentLabel} is still running. Archiving it will stop the subagent and remove it from the track.`
: `Remove ${subagentLabel} from the track. The subagent will be archived.`,
message,
confirmLabel: "Archive",
cancelLabel: "Cancel",
destructive: true,
@@ -202,6 +202,14 @@ describe("buildSubagentRowPresentationData", () => {
).toBe("running");
});
it("shows an idle child with only passive background work as done", () => {
expect(
buildSubagentRowPresentationData(
row({ id: "a", status: "idle", backgroundWork: { count: 1, runningCount: 0 } }),
).statusBucket,
).toBe("done");
});
it("keeps a failed child marked failed while it has background work", () => {
expect(
buildSubagentRowPresentationData(
@@ -1,6 +1,8 @@
import type { TFunction } from "i18next";
import type { ComposerTrackPillSegment } from "@/composer/tracks";
import type { SidebarStateBucket } from "@/utils/sidebar-agent-state";
import { isAgentRunning } from "@getpaseo/protocol/agent-state-bucket";
import type { AgentBackgroundWorkSummary } from "@getpaseo/protocol/messages";
import type { AgentLifecycleStatus } from "@getpaseo/protocol/agent-lifecycle";
import { STATUS_BUCKET_ORDER } from "@/utils/sidebar-agent-state";
import type { SubagentRow } from "./select";
@@ -21,10 +23,10 @@ function presentationStatus(row: SubagentRow) {
*/
export function deriveSubagentStatusBucket(
status: AgentLifecycleStatus,
backgroundWork?: { count: number } | null,
backgroundWork?: AgentBackgroundWorkSummary | null,
): SidebarStateBucket {
if (status === "error") return "failed";
if (status === "running" || (backgroundWork?.count ?? 0) > 0) return "running";
if (isAgentRunning({ status, backgroundWork })) return "running";
return "done";
}
@@ -30,6 +30,17 @@ describe("deriveAgentStateBucket", () => {
).toBe("failed");
expect(deriveAgentStateBucket({ status: "idle", backgroundWork: { count: 0 } })).toBe("done");
});
it("keeps passive background work visible without changing agent status or priority", () => {
const passive = { status: "idle", backgroundWork: { count: 1, runningCount: 0 } } as const;
expect(deriveAgentStateBucket(passive)).toBe("done");
expect(deriveAgentStateBucket({ ...passive, requiresAttention: true })).toBe("attention");
expect(deriveAgentStateBucket({ ...passive, status: "running" })).toBe("running");
expect(getAgentStatusPriority(passive)).toBe(getAgentStatusPriority({ status: "idle" }));
expect(
deriveAgentStateBucket({ ...passive, backgroundWork: { count: 2, runningCount: 1 } }),
).toBe("running");
});
it("prioritizes pending permissions as needs_input", () => {
expect(
deriveAgentStateBucket({
+12 -4
View File
@@ -1,5 +1,5 @@
import type { AgentLifecycleStatus } from "./agent-lifecycle.js";
import type { WorkspaceStateBucket } from "./messages.js";
import type { AgentBackgroundWorkSummary, WorkspaceStateBucket } from "./messages.js";
export type { WorkspaceStateBucket };
export type AgentAttentionReason = "finished" | "error" | "permission" | null | undefined;
@@ -9,7 +9,15 @@ export interface AgentStateBucketInput {
pendingPermissionCount?: number;
requiresAttention?: boolean;
attentionReason?: AgentAttentionReason;
backgroundWork?: { count: number } | null;
backgroundWork?: AgentBackgroundWorkSummary | null;
}
export function isAgentRunning(input: {
status: AgentLifecycleStatus | null | undefined;
backgroundWork?: AgentBackgroundWorkSummary | null;
}): boolean {
const { status, backgroundWork } = input;
return status === "running" || (backgroundWork?.runningCount ?? backgroundWork?.count ?? 0) > 0;
}
const WORKSPACE_STATE_BUCKET_PRIORITY = {
@@ -33,7 +41,7 @@ export function deriveAgentStateBucket(input: AgentStateBucketInput): WorkspaceS
if (hasUnreadError(input)) {
return "failed";
}
if (input.status === "running" || (input.backgroundWork?.count ?? 0) > 0) {
if (isAgentRunning(input)) {
return "running";
}
if (input.requiresAttention) {
@@ -53,7 +61,7 @@ export function getAgentStatusPriority(input: AgentStateBucketInput): number {
if (hasUnreadError(input)) {
return 1;
}
if (input.status === "running" || (input.backgroundWork?.count ?? 0) > 0) {
if (isAgentRunning(input)) {
return 2;
}
if (input.status === "initializing") {
+10 -2
View File
@@ -903,6 +903,14 @@ const AgentActiveTurnPayloadSchema = z.object({
startedAt: z.string().nullable(),
});
export const AgentBackgroundWorkSummarySchema = z.object({
count: z.number().int().nonnegative(),
/** Defaults to the total count when omitted. */
runningCount: z.number().int().nonnegative().optional(),
});
export type AgentBackgroundWorkSummary = z.infer<typeof AgentBackgroundWorkSummarySchema>;
export const AgentSnapshotPayloadSchema = z.object({
id: z.string(),
provider: AgentProviderSchema,
@@ -920,7 +928,7 @@ export const AgentSnapshotPayloadSchema = z.object({
updatedAt: z.string(),
lastUserMessageAt: z.string().nullable(),
status: AgentStatusSchema,
backgroundWork: z.object({ count: z.number().int().nonnegative() }).optional(),
backgroundWork: AgentBackgroundWorkSummarySchema.optional(),
activeTurn: AgentActiveTurnPayloadSchema.nullable().optional(),
capabilities: AgentCapabilityFlagsSchema,
currentModeId: z.string().nullable(),
@@ -958,7 +966,7 @@ export const AgentListItemPayloadSchema = z.object({
thinkingOptionId: z.string().nullable().optional(),
effectiveThinkingOptionId: z.string().nullable().optional(),
status: AgentStatusSchema,
backgroundWork: z.object({ count: z.number().int().nonnegative() }).optional(),
backgroundWork: AgentBackgroundWorkSummarySchema.optional(),
cwd: z.string(),
createdAt: z.string(),
updatedAt: z.string(),
@@ -15315,6 +15315,7 @@ test("lists and stops heartbeat background work across provider session closure"
const heartbeat = {
...heartbeatKey,
kind: "heartbeat",
countsAsRunning: false,
description: "Check build",
startedAt: "2026-10-06T10:00:00.000Z",
stop: async () => {
@@ -15341,7 +15342,10 @@ test("lists and stops heartbeat background work across provider session closure"
items: [{ id: item.id, kind: "shell", description: null, canStop: true }],
});
await vi.waitFor(() =>
expect(manager.getAgentBackgroundWorkSummary(agent.id)).toEqual({ count: 2 }),
expect(manager.getAgentBackgroundWorkSummary(agent.id)).toEqual({
count: 2,
runningCount: 1,
}),
);
const providerItem = manager
.listAgentBackgroundWork(agent.id)
@@ -15363,7 +15367,7 @@ test("lists and stops heartbeat background work across provider session closure"
await manager.closeAgent(agent.id);
expect(manager.getAgent(agent.id)).toBeNull();
expect(manager.listAgentBackgroundWork(agent.id)).toEqual([renamed]);
expect(manager.getAgentBackgroundWorkSummary(agent.id)).toEqual({ count: 1 });
expect(manager.getAgentBackgroundWorkSummary(agent.id)).toEqual({ count: 1, runningCount: 0 });
expect(events.slice(eventsBeforeClose)).toContainEqual({
type: "background_work",
agentId: agent.id,
@@ -1480,7 +1480,7 @@ export class AgentManager {
await session.stopBackgroundWork(providerItemId);
}
getAgentBackgroundWorkSummary(agentId: string): { count: number } | undefined {
getAgentBackgroundWorkSummary(agentId: string) {
return this.backgroundWork.summary(agentId);
}
@@ -1,4 +1,5 @@
import { randomUUID } from "node:crypto";
import type { AgentBackgroundWorkSummary } from "@getpaseo/protocol/messages";
export interface AgentBackgroundWorkInput {
id: string;
@@ -24,11 +25,13 @@ export interface DaemonBackgroundWorkInput extends DaemonBackgroundWorkKey {
kind: string;
description: string | null;
startedAt: string;
countsAsRunning?: boolean;
stop: () => Promise<void>;
}
interface DaemonBackgroundWork {
item: AgentBackgroundWorkItem;
countsAsRunning: boolean;
stop: DaemonBackgroundWorkInput["stop"];
}
@@ -91,21 +94,23 @@ export class AgentBackgroundWorkStore {
}
setDaemonBackgroundWork(input: DaemonBackgroundWorkInput): AgentBackgroundWorkItem[] | null {
const { agentId, kind, description, startedAt, stop } = input;
const { agentId, kind, description, startedAt, countsAsRunning = true, stop } = input;
const id = daemonItemId(input);
const items = this.daemonItems.get(agentId) ?? new Map<string, DaemonBackgroundWork>();
const previous = items.get(id)?.item;
const previous = items.get(id);
const item: AgentBackgroundWorkItem = {
id,
kind,
description,
startedAt,
canStop: true,
stopToken: previous?.stopToken ?? randomUUID(),
stopToken: previous?.item.stopToken ?? randomUUID(),
};
items.set(id, { item, stop });
items.set(id, { item, countsAsRunning, stop });
this.daemonItems.set(agentId, items);
return previous && sameItems([previous], [item]) ? null : this.list(agentId);
return previous?.countsAsRunning === countsAsRunning && sameItems([previous.item], [item])
? null
: this.list(agentId);
}
removeDaemonBackgroundWork(key: DaemonBackgroundWorkKey): AgentBackgroundWorkItem[] | null {
@@ -140,12 +145,17 @@ export class AgentBackgroundWorkStore {
return items;
}
summary(agentId: string): { count: number } | undefined {
summary(agentId: string): AgentBackgroundWorkSummary | undefined {
const items = this.providerItems.get(agentId);
const daemonItems = this.daemonItems.get(agentId);
return items || daemonItems
? { count: (items?.length ?? 0) + (daemonItems?.size ?? 0) }
: undefined;
if (!items && !daemonItems) return undefined;
const count = (items?.length ?? 0) + (daemonItems?.size ?? 0);
let runningCount = items?.length ?? 0;
for (const work of daemonItems?.values() ?? []) {
if (work.countsAsRunning) runningCount++;
}
return runningCount === count ? { count } : { count, runningCount };
}
clear(agentId: string): AgentBackgroundWorkItem[] | null {
@@ -2,6 +2,7 @@ import { chmod, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { deriveAgentStateBucket } from "@getpaseo/protocol/agent-state-bucket";
import { AgentManager } from "../agent/agent-manager.js";
import { AgentStorage } from "../agent/agent-storage.js";
import { createAgentCommand } from "../agent/create-agent/create.js";
@@ -649,7 +650,7 @@ describe("ScheduleService", () => {
expect(resumed.nextRunAt).toBe("2026-01-01T00:04:00.000Z");
});
test("shows an active heartbeat and stops it through agent background work", async () => {
test("lists and stops an active heartbeat while the agent is idle", async () => {
const logger = createTestLogger();
const manager = new AgentManager({
logger,
@@ -674,6 +675,12 @@ describe("ScheduleService", () => {
} as const;
const created = await service.create(input);
expect(
deriveAgentStateBucket({
status: "idle",
backgroundWork: manager.getAgentBackgroundWorkSummary(agent.id),
}),
).toBe("done");
const items = manager.listAgentBackgroundWork(agent.id);
expect(items).toEqual([
expect.objectContaining({
@@ -557,6 +557,7 @@ export class ScheduleService {
namespace: "heartbeat",
id,
kind: "heartbeat",
countsAsRunning: false,
description: name,
startedAt: createdAt,
stop,
@@ -337,6 +337,15 @@ describe("WorkspaceDirectory", () => {
});
await expect(workspace.workspaceStatus()).resolves.toBe("running");
});
test("keeps the workspace idle when root and delegated agents only have passive background work", async () => {
const workspace = new WorkspaceStatus();
const backgroundWork = { count: 1, runningCount: 0 };
workspace.hasRootAgent({ id: "parent-agent", status: "idle", backgroundWork });
workspace.hasDelegatedAgent({ id: "child-agent", status: "idle", backgroundWork });
await expect(workspace.workspaceStatus()).resolves.toBe("done");
});
test("uses root agent activity, not delegated child activity, for workspace status", async () => {
const workspace = new WorkspaceStatus();
@@ -8,6 +8,7 @@ import type {
} from "./messages.js";
import {
deriveAgentStateBucket,
isAgentRunning,
getWorkspaceStateBucketPriority,
type WorkspaceStateBucket,
} from "@getpaseo/protocol/agent-state-bucket";
@@ -392,7 +393,7 @@ export class WorkspaceDirectory {
continue;
}
const isWorkspaceRoot = workspaceAgent.id === agent.id;
if (!isWorkspaceRoot && agent.status !== "running" && !agent.backgroundWork?.count) {
if (!isWorkspaceRoot && !isAgentRunning(agent)) {
continue;
}
const bucket = isWorkspaceRoot