Files
streamline/satellite/test/claude_pty_bg_detect.test.ts
T
2026-11-16 17:26:44 -08:00

79 lines
3.4 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { structuredBackgroundId, terminalTaskIds } from "../../bin/claude-pty";
/**
* 后台子 agent 也是后台任务。
*
* 2026-10-14 feature 62640d3e 的评估:模型派出后台 Agent 调研代码、回一句「等它回来再写」
* 就 end_turn。claude-pty 只认 backgroundTaskId,不认 async_launched,判定「没有后台任务」
* 当场收尾,三次评估都没有 ASSESSMENT,被盖成「信息不足」。2.1.285 / 2.1.287 实测一致。
*/
describe("structuredBackgroundId", () => {
test("后台 Bash / Monitor 的 backgroundTaskId", () => {
expect(structuredBackgroundId({ backgroundTaskId: "br5e715ft" })).toBe("br5e715ft");
});
test("后台 Agent 的 async_launched 取 agentId", () => {
expect(structuredBackgroundId({
isAsync: true,
status: "async_launched",
agentId: "af0fbe1446b3f034a",
description: "Research iOS chat notice code",
outputFile: "/tmp/x/tasks/af0fbe1446b3f034a.output",
})).toBe("af0fbe1446b3f034a");
});
test("前台跑完的 Agent 不算后台任务", () => {
expect(structuredBackgroundId({ status: "completed", agentId: "a1", content: [] })).toBeNull();
});
test("非对象 / 空值", () => {
expect(structuredBackgroundId(undefined)).toBeNull();
expect(structuredBackgroundId(null)).toBeNull();
expect(structuredBackgroundId("Error: something")).toBeNull();
});
});
/** 2.1.287 实际下发的通知原文(路径已缩短)。 */
const agentDone = (id: string, note = "") => `<task-notification>
<task-id>${id}</task-id>
<tool-use-id>toolu_0177Azxdjav5B8Ti6K3QgZeg</tool-use-id>
<output-file>/tmp/x/tasks/${id}.output</output-file>
<status>completed</status>
<summary>Agent "sleeper" finished</summary>${note}
<result>SUBAGENT_OK</result>
</task-notification>`;
const INTERIM_NOTE = "\n<note>This agent stopped with background work of its own still running. It may resume on its own "
+ "when that work completes or reports, and the same task-id notifies again if it does; the result below may be interim.</note>";
describe("terminalTaskIds", () => {
test("子 agent 真正完成", () => {
expect(terminalTaskIds(agentDone("a5a89b5de738d1aec"))).toEqual(["a5a89b5de738d1aec"]);
});
test("子 agent 自己还挂着后台任务时的中途停下不算完成", () => {
expect(terminalTaskIds(agentDone("a5a89b5de738d1aec", INTERIM_NOTE))).toEqual([]);
});
test("原始 transcript 行(JSON 转义过的 \\n)里的中途停下同样不算", () => {
const raw = JSON.stringify({ type: "user", message: { content: agentDone("a5a89b5de738d1aec", INTERIM_NOTE) } });
expect(terminalTaskIds(raw)).toEqual([]);
});
test("同一段里一个中途、一个完成,只放掉完成的那个", () => {
const text = agentDone("agentA", INTERIM_NOTE) + "\n" + agentDone("agentB");
expect(terminalTaskIds(text)).toEqual(["agentB"]);
});
test("后台 Bash 的 killed / TaskOutput 的 <task_id> 照旧", () => {
expect(terminalTaskIds("<task-notification>\n<task-id>bl1qid203</task-id>\n<status>killed</status>\n</task-notification>"))
.toEqual(["bl1qid203"]);
expect(terminalTaskIds("<task_id>bt51xghtb</task_id>\n<status>completed</status>")).toEqual(["bt51xghtb"]);
});
test("running 不算完成", () => {
expect(terminalTaskIds("<task_id>bt51xghtb</task_id>\n<status>running</status>")).toEqual([]);
});
});