Skip to content

Commit 817ac55

Browse files
authored
refactor(tasks): extract delivery policy (#57475)
* refactor(tasks): add executor facade * refactor(tasks): extract delivery policy
1 parent 8623c28 commit 817ac55

3 files changed

Lines changed: 242 additions & 119 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { describe, expect, it } from "vitest";
2+
import {
3+
formatTaskBlockedFollowupMessage,
4+
formatTaskStateChangeMessage,
5+
formatTaskTerminalMessage,
6+
isTerminalTaskStatus,
7+
shouldAutoDeliverTaskStateChange,
8+
shouldAutoDeliverTaskTerminalUpdate,
9+
shouldSuppressDuplicateTerminalDelivery,
10+
} from "./task-executor-policy.js";
11+
import type { TaskEventRecord, TaskRecord } from "./task-registry.types.js";
12+
13+
function createTask(partial: Partial<TaskRecord>): TaskRecord {
14+
return {
15+
taskId: partial.taskId ?? "task-1",
16+
runtime: partial.runtime ?? "acp",
17+
requesterSessionKey: partial.requesterSessionKey ?? "agent:main:main",
18+
task: partial.task ?? "Investigate issue",
19+
status: partial.status ?? "running",
20+
deliveryStatus: partial.deliveryStatus ?? "pending",
21+
notifyPolicy: partial.notifyPolicy ?? "done_only",
22+
createdAt: partial.createdAt ?? 1,
23+
...partial,
24+
};
25+
}
26+
27+
describe("task-executor-policy", () => {
28+
it("identifies terminal statuses", () => {
29+
expect(isTerminalTaskStatus("queued")).toBe(false);
30+
expect(isTerminalTaskStatus("running")).toBe(false);
31+
expect(isTerminalTaskStatus("succeeded")).toBe(true);
32+
expect(isTerminalTaskStatus("failed")).toBe(true);
33+
expect(isTerminalTaskStatus("timed_out")).toBe(true);
34+
expect(isTerminalTaskStatus("cancelled")).toBe(true);
35+
expect(isTerminalTaskStatus("lost")).toBe(true);
36+
});
37+
38+
it("formats terminal, followup, and progress messages", () => {
39+
const blockedTask = createTask({
40+
status: "succeeded",
41+
terminalOutcome: "blocked",
42+
terminalSummary: "Needs login.",
43+
runId: "run-1234567890",
44+
label: "ACP import",
45+
});
46+
const progressEvent: TaskEventRecord = {
47+
at: 10,
48+
kind: "progress",
49+
summary: "No output for 60s.",
50+
};
51+
52+
expect(formatTaskTerminalMessage(blockedTask)).toBe(
53+
"Background task blocked: ACP import (run run-1234). Needs login.",
54+
);
55+
expect(formatTaskBlockedFollowupMessage(blockedTask)).toBe(
56+
"Task needs follow-up: ACP import (run run-1234). Needs login.",
57+
);
58+
expect(formatTaskStateChangeMessage(blockedTask, progressEvent)).toBe(
59+
"Background task update: ACP import. No output for 60s.",
60+
);
61+
});
62+
63+
it("keeps delivery policy decisions explicit", () => {
64+
expect(
65+
shouldAutoDeliverTaskTerminalUpdate(
66+
createTask({
67+
status: "succeeded",
68+
deliveryStatus: "pending",
69+
notifyPolicy: "done_only",
70+
}),
71+
),
72+
).toBe(true);
73+
expect(
74+
shouldAutoDeliverTaskTerminalUpdate(
75+
createTask({
76+
runtime: "subagent",
77+
status: "succeeded",
78+
deliveryStatus: "pending",
79+
}),
80+
),
81+
).toBe(false);
82+
expect(
83+
shouldAutoDeliverTaskStateChange(
84+
createTask({
85+
status: "running",
86+
notifyPolicy: "state_changes",
87+
deliveryStatus: "pending",
88+
}),
89+
),
90+
).toBe(true);
91+
expect(
92+
shouldAutoDeliverTaskStateChange(
93+
createTask({
94+
status: "failed",
95+
notifyPolicy: "state_changes",
96+
deliveryStatus: "pending",
97+
}),
98+
),
99+
).toBe(false);
100+
expect(
101+
shouldSuppressDuplicateTerminalDelivery({
102+
task: createTask({
103+
runtime: "acp",
104+
runId: "run-duplicate",
105+
}),
106+
preferredTaskId: "task-2",
107+
}),
108+
).toBe(true);
109+
});
110+
});

‎src/tasks/task-executor-policy.ts‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import type { TaskEventRecord, TaskRecord, TaskStatus } from "./task-registry.types.js";
2+
3+
export function isTerminalTaskStatus(status: TaskStatus): boolean {
4+
return (
5+
status === "succeeded" ||
6+
status === "failed" ||
7+
status === "timed_out" ||
8+
status === "cancelled" ||
9+
status === "lost"
10+
);
11+
}
12+
13+
function resolveTaskDisplayTitle(task: TaskRecord): string {
14+
return (
15+
task.label?.trim() ||
16+
(task.runtime === "acp"
17+
? "ACP background task"
18+
: task.runtime === "subagent"
19+
? "Subagent task"
20+
: task.task.trim() || "Background task")
21+
);
22+
}
23+
24+
function resolveTaskRunLabel(task: TaskRecord): string {
25+
return task.runId ? ` (run ${task.runId.slice(0, 8)})` : "";
26+
}
27+
28+
export function formatTaskTerminalMessage(task: TaskRecord): string {
29+
const title = resolveTaskDisplayTitle(task);
30+
const runLabel = resolveTaskRunLabel(task);
31+
const summary = task.terminalSummary?.trim();
32+
if (task.status === "succeeded") {
33+
if (task.terminalOutcome === "blocked") {
34+
return summary
35+
? `Background task blocked: ${title}${runLabel}. ${summary}`
36+
: `Background task blocked: ${title}${runLabel}.`;
37+
}
38+
return summary
39+
? `Background task done: ${title}${runLabel}. ${summary}`
40+
: `Background task done: ${title}${runLabel}.`;
41+
}
42+
if (task.status === "timed_out") {
43+
return `Background task timed out: ${title}${runLabel}.`;
44+
}
45+
if (task.status === "lost") {
46+
return `Background task lost: ${title}${runLabel}. ${task.error ?? "Backing session disappeared."}`;
47+
}
48+
if (task.status === "cancelled") {
49+
return `Background task cancelled: ${title}${runLabel}.`;
50+
}
51+
const error = task.error?.trim();
52+
return error
53+
? `Background task failed: ${title}${runLabel}. ${error}`
54+
: `Background task failed: ${title}${runLabel}.`;
55+
}
56+
57+
export function formatTaskBlockedFollowupMessage(task: TaskRecord): string | null {
58+
if (task.status !== "succeeded" || task.terminalOutcome !== "blocked") {
59+
return null;
60+
}
61+
const title = resolveTaskDisplayTitle(task);
62+
const runLabel = resolveTaskRunLabel(task);
63+
const summary = task.terminalSummary?.trim() || "Task is blocked and needs follow-up.";
64+
return `Task needs follow-up: ${title}${runLabel}. ${summary}`;
65+
}
66+
67+
export function formatTaskStateChangeMessage(
68+
task: TaskRecord,
69+
event: TaskEventRecord,
70+
): string | null {
71+
const title = resolveTaskDisplayTitle(task);
72+
if (event.kind === "running") {
73+
return `Background task started: ${title}.`;
74+
}
75+
if (event.kind === "progress") {
76+
return event.summary ? `Background task update: ${title}. ${event.summary}` : null;
77+
}
78+
return null;
79+
}
80+
81+
export function shouldAutoDeliverTaskTerminalUpdate(task: TaskRecord): boolean {
82+
if (task.notifyPolicy === "silent") {
83+
return false;
84+
}
85+
if (task.runtime === "subagent" && task.status !== "cancelled") {
86+
return false;
87+
}
88+
if (!isTerminalTaskStatus(task.status)) {
89+
return false;
90+
}
91+
return task.deliveryStatus === "pending";
92+
}
93+
94+
export function shouldAutoDeliverTaskStateChange(task: TaskRecord): boolean {
95+
return (
96+
task.notifyPolicy === "state_changes" &&
97+
task.deliveryStatus === "pending" &&
98+
!isTerminalTaskStatus(task.status)
99+
);
100+
}
101+
102+
export function shouldSuppressDuplicateTerminalDelivery(params: {
103+
task: TaskRecord;
104+
preferredTaskId?: string;
105+
}): boolean {
106+
if (params.task.runtime !== "acp" || !params.task.runId?.trim()) {
107+
return false;
108+
}
109+
return Boolean(params.preferredTaskId && params.preferredTaskId !== params.task.taskId);
110+
}

0 commit comments

Comments
 (0)