fix: list a live Monitor whose task Claude Code reports as local_bash (#1607)

Fixes #1606

## Summary

Under ACP v2 a running `Monitor` never reached
`state_update.background`. Claude Code reports its task as `local_bash`
(no `ambient` flag), and the tool-result handling dropped every
`local_bash` task:

```diff
 // src/v2/tasks.ts, tool-result handling
 const task = taskId === undefined ? undefined : this.level.get(taskId);
-if (!task || task.taskType === "local_bash" || task.taskType === "local_agent") continue;
+if (!task || task.taskType === "local_agent") continue;
+// a command is its display terminal; the call of a task-starting tool is that task's entity
+if (task.taskType === "local_bash" && !TASK_TOOLS.has(toolUse.name)) continue;
 this.toolCalls.set(taskId!, { toolCallId, ... });
```

Why nothing else caught the Monitor:

```text
Monitor result, taskType local_bash
  toolResults        skipped by the line above      -> not in toolCalls
  adoptTasks         same local_bash skip           -> no task_<id> call
  terminals          needs a Bash result with backgroundTaskId
  lists()            lists only toolCalls/terminals -> background: []
```

Plain commands still go through `terminals`; `local_agent` is still
skipped. `adoptTasks()` is unchanged, since it already skips a task
whose call is registered.

## Evidence

- **Before:** the new test fails, and a real agent lists nothing (3
runs, 30 `state_update` frames, every one `background: []`).

  ```text
keeps the call in_progress and listed when Claude Code reports the
Monitor's task as local_bash, not ambient
AssertionError: expected [ 'running []', 'idle end_turn []' ] to include
'idle end_turn [toolu_mon]'
  ```

**After:** the same test passes, and the full suite is green (`npx
vitest run`: 4104 passed, 33 skipped; `tsc --noEmit`, `eslint` and
`prettier --check` clean). A real agent built from this branch (SDK
0.3.322) lists the Monitor in 10 of 11 `state_update` frames (the one
before it starts is `[]`), and its call goes `pending` → `in_progress`
with `capabilities: { cancel: {} }`.

The test, in pseudocode:

```text
task = { id: mon1, type: "local_bash" }          // no ambient, as the CLI sends it
script: Monitor tool_use, level [task], task_started, tool_result { taskId: mon1 },
        result, idle, task_notification completed, level []
expect trace == running [] | pending | in_progress | cancel | running [toolu_mon]
              | idle end_turn [toolu_mon] | completed | no cancel | idle []
```

The existing Monitor fixture models the task as `local_monitor` +
`ambient: true` (the shape #1538 fixed), which is why it never saw this.
`monitorStarts` now takes an optional task, defaulting to that fixture,
so the existing tests are unchanged.

## Merge Danger

**Door:** two-way

Revert the commit; nothing is persisted or migrated.

**Blast Radius:** Narrow

ACP v2 only (`src/v2`); v1 is untouched. A `Monitor`, or any tool in
`TASK_TOOLS` whose task Claude Code reports as `local_bash`, is now
registered and listed. A client that decides whether a session is quiet
(to restart or release it, say) will now see a live Monitor as
background work. That is the intended behavior, but it differs from what
those clients saw before.

Not covered:

- The late-level race in #1537 (a level that joins after the tool
result) is a separate bug, and a `local_bash` Monitor hit by it would
still go unlisted.
- `npm run test:integration` was not run against the real CLI. The
real-agent result above is a manual probe, not a test.
- `tool_call/cancel` and the task's end were not exercised against the
real agent, only through fixtures.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Dave Meehan
2026-11-16 11:13:21 +00:00
committed by GitHub
parent 8de49818f1
commit 41417f6eee
2 changed files with 51 additions and 5 deletions

View File

@@ -8570,7 +8570,12 @@ describe("ACP v2 terminal of a command in the background", () => {
describe("a task that a tool call started", () => {
// Claude Code marks every live-update watcher ambient, a requested
// Monitor's task included.
const monitor = {
const monitor: {
task_id: string;
task_type: string;
description: string;
ambient?: boolean;
} = {
task_id: "mon1",
task_type: "local_monitor",
description: "Watch the logs",
@@ -8580,10 +8585,10 @@ describe("ACP v2 terminal of a command in the background", () => {
/** A Monitor call whose task `mon1` runs on after the call returns. The
* tool starts the task, which joins the level before its
* `task_started`, and returns. */
async function* monitorStarts(options: Options) {
async function* monitorStarts(options: Options, task: typeof monitor = monitor) {
yield toolUseMessage(options, "toolu_mon", "Monitor", { command: "tail -f app.log" });
yield level(options, [monitor]);
yield taskEvent(options, "task_started", { ...monitor, tool_use_id: "toolu_mon" });
yield level(options, [task]);
yield taskEvent(options, "task_started", { ...task, tool_use_id: "toolu_mon" });
yield {
...toolResultMessage(options, "toolu_mon", { content: "Monitor started (task mon1)." }),
tool_use_result: { taskId: "mon1", timeoutMs: 0, persistent: true },
@@ -8648,6 +8653,43 @@ describe("ACP v2 terminal of a command in the background", () => {
]);
});
it("keeps the call in_progress and listed when Claude Code reports the Monitor's task as local_bash, not ambient", async () => {
// What Claude Code sends for a requested Monitor in practice: the task
// type of a shell command and no `ambient` flag. The call, not a
// terminal, is the task's entity, as `Monitor` is in TASK_TOOLS.
const localBashMonitor = {
task_id: "mon1",
task_type: "local_bash",
description: "Watch the logs",
};
scriptTurns(async function* (options) {
yield* monitorStarts(options, localBashMonitor);
yield result(options);
yield idleState(options);
yield monitorEvent(options, "task_notification", {
tool_use_id: "toolu_mon",
status: "completed",
output_file: "/tmp/tasks/mon1.output",
summary: "Done",
});
yield level(options, []);
});
const client = await promptUntil("idle end_turn [toolu_mon]");
await vi.waitFor(() => expect(toolCallTrace(client)).toContain("idle []"));
expect(toolCallTrace(client)).toEqual([
"running []",
"pending",
"in_progress",
"cancel",
"running [toolu_mon]",
"idle end_turn [toolu_mon]",
"completed",
"no cancel",
"idle []",
]);
});
it("stops the task on tool_call/cancel, and reports the call cancelled", async () => {
const stopped = Promise.withResolvers<string>();
sdk.stopTask = async (taskId) => stopped.resolve(taskId);

View File

@@ -476,7 +476,11 @@ export class SessionTasks {
(TASK_TOOLS.has(toolUse.name) ? structuredTaskId : undefined) ??
[...this.started].find(([, task]) => task.toolUseId === toolCallId)?.[0];
const task = taskId === undefined ? undefined : this.level.get(taskId);
if (!task || task.taskType === "local_bash" || task.taskType === "local_agent") continue;
if (!task || task.taskType === "local_agent") continue;
// Claude Code reports a Monitor's task as `local_bash`, like a shell
// command's. A command is its display terminal; the call of a tool that
// starts a task is the entity of that task, so it is kept.
if (task.taskType === "local_bash" && !TASK_TOOLS.has(toolUse.name)) continue;
this.toolCalls.set(taskId!, {
toolCallId,
output,