fix(muse): keep background monitors visible and stoppable #40435
OpenYidaWeng(GitHub profile, opens in a new tab)wants to merge 9 commits into
main fromrefs/pull/40435/headopened View on GitHubPushedf890f3b
- @@ -768,0 +796,21 @@796+ // A monitor has no running turn item, so `task/list` is the only place it shows up.797+ for (const [taskId, task] of liveTasks) {798+ if (listed.has(taskId)) continue;799+ listed.add(taskId);800+ tasks.push({ taskId, ...task });801+ }802+ yield* updateThread({ pendingBackgroundTasks: tasks });803+ });804+ // The item stream says a tool was backgrounded; only `task/list` says whether it still runs.805+ const refreshBackgroundTasks = Effect.fnUntraced(function* () {806+ const listed = yield* request("task/list", {}, false).pipe(807+ Effect.flatMap((value) => decode(MuseTaskList, value)),808+ // Never hold the event stream long on a host that will not answer.809+ Effect.timeoutOption(5_000),810+ Effect.option,811+ );812+ // A host that cannot list tasks keeps what the items told us.813+ if (Option.isNone(listed) || Option.isNone(listed.value)) return;814+ liveTasks.clear();815+ for (const task of listed.value.value.tasks)816+ if (task.status === "inProgress") liveTasks.set(task.taskId, liveBackgroundTask(task));@@ -2363,11 +2426,11 @@23632426 interruptTurn: Effect.fn("MuseAdapter.interruptTurn")(23642427 function* (args) {23652428 const turn = active;2366- // Recovering an ended turn is done unless Muse still runs work Stop cannot end.2367- if (2368- !turn &&2369- args.requestRuntimeRestart === true &&2370- observedChildren.size === 0 &&2371- wake === undefined2372- )2373- return;2429+ // Recovering an ended turn is done unless Muse still runs work Stop can end.2430+ if (!turn && args.requestRuntimeRestart === true) {2431+ if (liveTasks.size === 0) {2432+ // A child or a held wake is not this call's to end; an idle turn is done.2433+ if (observedChildren.size === 0 && wake === undefined) return;2434+ } else {2435+ // A monitor outlives its turn, so Stop must reach Muse's own task registry.2436+ yield* request("task/stopAll", {}).pipe(Effect.ignore);@@ -2401,3 +2469,5 @@24012469 for (const joined of turn.joined)24022470 yield* request("turn/interrupt", { turnId: joined }).pipe(Effect.ignore);24032471 yield* request("turn/interrupt", { turnId: turn.nativeId });2472+ // Monitors and backgrounded tools are Muse-side work; the turn ending does not stop them.2473+ yield* request("task/stopAll", {}).pipe(Effect.ignore);
Pushedb5a04ed
Pushed722436c
Pushedea5bddf
Pushed79fc28f
Pushedada720d
- @@ -2365,9 +2435,13 @@23652435 const turn = active;2366- // Recovering an ended turn is done unless Muse still runs work Stop cannot end.2367- if (2368- !turn &&2369- args.requestRuntimeRestart === true &&2370- observedChildren.size === 0 &&2371- wake === undefined2372- )2373- return;2436+ // Recovering an ended turn is done unless Muse still runs work Stop can end.2437+ if (!turn && args.requestRuntimeRestart === true) {2438+ if (liveTasks.size === 0) {2439+ // A child or a held wake is not this call's to end; an idle turn is done.2440+ if (observedChildren.size === 0 && wake === undefined) return;2441+ } else {2442+ // A monitor outlives its turn, so Stop must reach Muse's own task registry.2443+ // A failed stop keeps the listing, so the thread still shows work to stop.2444+ yield* request("task/stopAll", {});2445+ // A monitor that already woke the session left a turn this call must end too.2446+ if (wake)2447+ yield* request("turn/interrupt", { turnId: wake.nativeId }).pipe(Effect.ignore);
juliusmarminge added labelstakeover:blocked
juliusmarminge removed labelstakeover:in-progress
Pushed5a7d4cc
- @@ -2401,3 +2491,14 @@24012491 for (const joined of turn.joined)24022492 yield* request("turn/interrupt", { turnId: joined }).pipe(Effect.ignore);24032493 yield* request("turn/interrupt", { turnId: turn.nativeId });2494+ // Monitors and backgrounded tools are Muse-side work; the turn ending does not stop them.2495+ // Keep the rows if the stop fails: those tasks can still be running.2496+ const stopped = yield* request("task/stopAll", {}).pipe(2497+ Effect.as(true),2498+ Effect.orElseSucceed(() => false),2499+ );2500+ if (stopped) {2501+ // A `task/list` read started before this stop must not put the rows back.2502+ taskListGeneration++;2503+ liveTasks.clear();2504+ yield* syncBackground();
Pushed61cac23
Sign in to join this conversation.
Note
Reviews paused#
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review.
Walkthrough
The Muse adapter tracks background tasks reported by Muse and backgrounded tool calls. It includes live tasks in pending-work state, refreshes task listings, and calls
task/stopAllduring Stop handling.Changes: Muse background task lifecycle
packages/provider-muse/src/server/protocol.ts,packages/provider-muse/src/server/adapter.tsMuseItemadds an optionalbackgroundfield, andMuseTaskListdefines the task-list response shape. The adapter classifies live tasks as monitors or commands and stores them in a live-task map.packages/provider-muse/src/server/adapter.ts,packages/provider-muse/src/server/adapter.test.tspackages/provider-muse/src/server/adapter.ts,packages/provider-muse/src/server/adapter.test.tstask/stopAll. Failed requests retain tracked tasks; successful requests clear them. Cleanup invalidates in-flight task-list reads. Tests cover retries and Stop races.Estimated code review effort: 3 (Moderate) | ~25 minutes
Change: Bug fix · Severity of issue fixed: Medium
Sequence Diagram(s)#
Suggested reviewers:
juliusmarmingeMerge Risk:
Low · up to
61cacMuse background tasks now show as pending work, count toward session retention, and are stopped through
task/stopAll. In a narrow race, a stopped monitor can reappear as pending work after Stop and keep the thread looking busy until the next turn ends or the thread is reattached. It is a bounded, recoverable glitch, but it is not yet fixed. The behavior has also only been tested against a fake host, so a live Muse check is still worth doing.Pre-merge checks:
4
#19061asks for these coding behaviors: (1) list live Muse monitors and backgrounded tool calls as pending background work; (2) count them in hasPendingBackgroundWork so idle rel…#19061and explains …Full details: Title check
Explanation
The title "fix(muse): keep background monitors visible and stoppable" matches the main change. The PR tracks backgrounded Muse monitor tool calls as pending background work, reconciles them with task/list, and makes them stoppable through task/stopAll. The title is concise, uses a conventional commit format, and names the specific area (muse provider) and behavior.
Full details: Linked Issues check
Explanation
Directly linked issue
#19061asks for these coding behaviors: (1) list live Muse monitors and backgrounded tool calls as pending background work; (2) count them in hasPendingBackgroundWork so idle release waits; (3) stop them through task/stopAll or task/stop; (4) clear them when the work finishes or times out. The previous pass found (1)-(3) met. Head adds one change: liveBackgroundTask in adapter.ts now sets awaited: true for every live row, including commands. The issue does not state an awaited value for commands, so this change does not alter any requirement. Head still lists monitors and backgrounded tools (kind monitor or command) and feeds live tasks into hasPendingBackgroundWork. The Stop path (task/stopAll) and the task/list reconciliation are unchanged by the head commit as far as the readable evidence shows. Requirement (4) keeps its disclosed limit: a monitor that times out without waking the session stays listed until the next turn end or reattach, because Muse sends no task-ended notification and task/list has no timeout. The issue names a supported Muse signal as the better option, so this is a host API limit. Evidence: the PR description, the previous assessment, and the head definition of liveBackgroundTask. The adapter.ts diff against the previous head was unavailable (changed blobs missing), and the file read was truncated before the call sites, so call-site behavior of the awaited change was not re-read. Live Muse behavior is not verified; tests use a fake host.#16499,#17625, and#18880are context only.Full details: Out of Scope Changes check
Explanation
All changes stay in packages/provider-muse. The head commit 61cac23 sets awaited: true on every live background row, including yielded commands. This is a change in behavior for backgrounded tool calls, which the issue covers. The head comment states the reason: Muse hands a backgrounded task's result back as runtime context and wakes the session. The PR summary and a PR comment from alecrt describe this same Muse behavior, so the change connects to the issue's pending-work objective and does not add unrelated work. The awaited change does not add a new surface. The protocol additions (background flag, MuseTaskList) and the Stop, reconciliation, and generation-guard code remain tied to
#19061. The overlap with#25810is a rebase concern only. Limitation: the current PR description still says backgrounded shell calls list as kind command without awaited. That text is stale for head. It does not change scope, but the description should match the code. Source call sites were not re-read because the diff and the full file read were unavailable or truncated.Full details: Description check
Explanation
The description follows the template. It has Problem, Change, Scope, and Verification sections, and each section is filled in with specific detail. The Problem section cites issue
#19061and explains the failure. The Change section lists the behavior changes in the adapter, and the Scope section links the issue and limits the work to provider-muse. The Verification section lists focused tests with observed results, and it states what was not verified, which is a live Muse account. The author also names the model and harness, as the template requests. The description does not include a before/after screenshot. This check does not apply because the change is not a UI change. The title line in the template is a conventional-commit title. The PR description does not state a title, so the title cannot be checked against that guidance here.Finishing Touches
Generate unit tests (beta)
a919a55b-7aaf-4576-b439-089de3fe4596Reviewing files that changed from the base of the PR and between 5a7d4cc5832a924e4c67caf1dca094cbfe890892 and 61cac2321229ab7086b073a9f2c24d2de21fb306.
packages/provider-muse/src/server/adapter.test.tspackages/provider-muse/src/server/adapter.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.