Skip to content

fix(agent): retire unreadable Bot worker watches and store only what a wake needs - #6627

Merged
M4n5ter merged 2 commits into
rust-previewfrom
fix/bot-watch-heal
Nov 5, 2026
Merged

M4n5ter merged 2 commits into
rust-previewfrom
fix/bot-watch-heal

Conversation

@liugddx

@liugddx liugddx commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #6624, following M4n5ter's review there. #6624 skipped an unreadable worker watch inside watches(), kept the bad record and added an in-memory set to report it once. That left three problems, all addressed here:

  • Garbage collection was bypassed. An unreadable watch of an archived or missing worker was dropped before wake_bot_pass could retire it, so it stayed forever. watches() now returns each record's decode result, and wake_bot_pass checks that the worker is alive first: a gone worker's watch is retired without being decoded. For a live worker the decode error is collected, the healthy workers keep waking, and the combined error is returned, so start_bot_wake's existing previous_error reports it once. The unreadable set on Bot is removed.
  • The write path could not repair a bad record. Bot::watch read the old value through read::<Watch>, which failed on an unreadable record, so every later SessionSend to that worker failed to refresh its watch. It now reads the record itself and, when the old value does not decode, writes the new one with notified unset.
  • The root cause: a watch embedded a type owned by another module. A Watch held a ReturnRoute, which held an AcceptedModel; a change to that type made stored watches unreadable. A watch now keeps only the model selection, thinking level, workspace, binding and notified, and the model is resolved with Source::select when the Bot is woken (the way scheduled tasks do). A model that cannot be resolved at that moment is reported like an unreadable watch and retried on the next pass. ReturnRoute itself is unchanged for WorkHub; it only gained a constructor and a thinking-level accessor.

There is no compatibility branch for the old record format, as requested in the review.

Migration

A watch written by the build before this one ({"route": ..., "notified": ...}) does not decode with the new shape, even if it was readable before. It is retired when its worker is gone and replaced by the next SessionSend to a live worker. The one case this does not cover: a Bot that delegated work before the upgrade and sends nothing more to that worker is not woken when the worker finishes. Existing development data can be cleaned with a one-off script that deletes watch/* records in the maka.bot namespace; the runtime keeps a single current format.

Verification

  • an_unreadable_watch_of_a_gone_worker_is_retired_and_does_not_stop_other_wakes: an unreadable watch for a worker that does not exist is stored; another worker finishes and wakes the Bot, and afterwards the unreadable watch is gone.
  • the_next_send_to_a_worker_replaces_its_unreadable_watch_so_it_wakes_the_bot: a live worker's watch is overwritten with an unreadable value; the Bot's next SessionSend replaces it and the finished worker wakes the Bot. Without the write-path change this test fails (the Bot is never woken), checked by reverting it.
  • The two existing wake tests and the other Bot tests pass (24 in total, with LANG=en_US.UTF-8). Nightly rustfmt is clean. Clippy -D warnings reports three existing findings in files this PR does not touch (executor.rs, executor/oar/worker.rs, owner/tests/session_parent_authority.rs) and none in the changed files.
  • Not run: a desktop session with this change. The same wake path was run against a real model before the change (the Bot was woken for two workers).

AI use

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

Tool(s) and scope: Claude Code (Claude Opus 5.5) wrote the code, the tests and this description; the changes were run and reviewed by the contributor. The commit carries Generated-by: Claude Code.

Checklist

  • Tests cover the change and fail without it
  • Format, typecheck and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review notice: This comment was posted by an automated review agent. It is not an independent human review and does not replace one.

I reviewed all four changed files at 17fa84a564c8a5ea2593c3396f6cce56fbd9a39c. I found one P2 upgrade regression: the new persisted Watch shape rejects watches that were valid immediately before this change, and active workers are skipped until another send replaces their record. The upgrade case is detailed inline.

The improvements to damaged-record handling are real: all records retain their revisions, missing/archived/non-child workers reach retirement before decoding, refreshing a damaged record no longer depends on decoding it, and decode/model-resolution failures are collected without stopping healthy workers. I checked the model reselection and notification path, including workspace/binding checks and notification identity handling. The per-worker in-memory error set is removed; the existing whole-pass error suppression now reports changes in the combined error.

I read the actual native logs for CI 41808555131: gone-worker retirement passed in 12.267 seconds, repair by a later send in 11.811 seconds, ordinary wake in 16.110 seconds and the busy-Bot case in 15.919 seconds. Those tests start with the new format or deliberately send again; they do not establish upgrade recovery for a live worker that finishes without another send. A small Rust decoding check using the exact new Watch declaration and opaque adapters for nested field types also rejects the old top-level route key. That check is limited to the storage shape, not a native Host integration run.

The actual Git merge against rust-preview at a08d327f19a0afd17dbe4c941123c06b4a46d871 succeeds. The exact-head CI's two composer failures are already present on that base after #6617, rather than evidence against these wake tests. I did not execute the full Rust native service, a desktop or a live provider. The AI declaration and required trailer are present.


Automated review notice: This comment was posted by an automated review agent operated by Astro-Han. It is not an independent human review and does not replace one.

#[serde(deny_unknown_fields)]
struct Watch {
route: crate::owner::notifications::ReturnRoute,
model: ModelSelection,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Migrate valid pre-upgrade watches before requiring the new shape.

The preceding build stores {"route": {...}, "notified": ...} in this same namespace/key. This deny_unknown_fields decoder now rejects route and requires the new top-level model/workspace/binding fields, even when the old record was fully readable before upgrading. For a still-live child, wake_bot_pass collects that error and continues before looking at its completed invocation. If the Bot delegated work and is waiting for its result, there may be no later SessionSend to repair the watch, so the completion never wakes the Bot, across subsequent polls and restarts.

This affects valid existing watches, not just the already-damaged records from #6624. The new repair test explicitly sends again after corrupting a watch and therefore misses this upgrade path. Add a legacy decoder or migration that preserves the model selection, workspace/binding and notified marker, plus a regression that writes a valid old-format watch, upgrades/reopens, completes the worker without another send and observes exactly one wake.


Automated review notice: This comment was posted by an automated review agent operated by Astro-Han. It is not an independent human review and does not replace one.

@M4n5ter M4n5ter left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English

Thanks for following up on #6624! Retiring gone workers' watches before decoding, healing unreadable watches on refresh, removing Bot.unreadable, and decoupling Watch from ReturnRoute / AcceptedModel without adding legacy runtime compatibility branches (please disregard the earlier automated comment suggesting a legacy route decoder) all look clean and well-tested.

One small hot-path adjustment:

  • Check Bot readiness before calling Source::select in wake_bot_pass (plugins/agent/native/src/owner/bot/wake.rs:318):
    start_bot_wake runs on store.subscribe_changes() (coalesced at 250ms) as well as the 2s poll. Previously, wake_bot_pass entered prepare_notification directly and hit runs.busy(&source.session_id) || self.has_parked_invocation(&source.session_id) at the top (notifications.rs:83-85), returning Ok(None) without touching provider state. With Source::select placed ahead of prepare_notification, whenever a worker finishes while the Bot is in the middle of a Turn (or after an earlier worker in the same scan wakes the Bot), every 250ms commit pass runs a full Source::select (cloning the catalog snapshot, reading/decrypting credentials from ConfigurationStore, and preparing provider/inventory material) only for prepare_notification to immediately discard it because the Bot is busy.
    Adding a quick if self.runs.lock().await.busy(&bot) || self.has_parked_invocation(&bot).await? { continue; } right before Source::select (after the gone-worker forget and watch.notified checks) avoids that repeated work while keeping GC intact.

I'll push a small follow-up commit for this guard and merge directly.

简体中文

感谢跟进 #6624!先判活走 forget 回收、写入路径覆盖修复坏记录、移除 Bot.unreadable,以及在不引入旧格式兼容分支的前提下将 Watch 与 ReturnRoute / AcceptedModel 解耦(无需理会前面自动化评论关于添加旧 route 兼容解码器的建议),整体实现和测试都很干净。

只需顺手调整一处热路径顺序:

  • 在 wake_bot_pass 调用 Source::select 前先检查 Bot 是否空闲可唤醒(plugins/agent/native/src/owner/bot/wake.rs:318):
    start_bot_wake 除了 2s 轮询外还订阅了 store.subscribe_changes()(250ms 合并)。改动前直接进入 prepare_notification,在顶部(notifications.rs:83-85)命中 runs.busy(&source.session_id) || self.has_parked_invocation(&source.session_id) 就会零开销返回 Ok(None);现在把 Source::select 提到了 prepare_notification 之前,只要有子会话在 Bot 正在执行一轮对话期间结束(或同轮扫描中前一个 worker 刚唤醒了 Bot),后续每次 250ms 事件提交都会对每个待汇报 worker 空跑一遍完整的 Source::select(克隆目录快照、从 ConfigurationStore 读取解密凭据、准备 provider/inventory),紧接着又因 Bot 处于 busy 被 prepare_notification 丢弃。
    在 forget 清理与 watch.notified 判断之后、Source::select 之前先加一行 if self.runs.lock().await.busy(&bot) || self.has_parked_invocation(&bot).await? { continue; } 即可在保留 GC 的同时避免重复解析。

我直接补一个精简提交处理掉这里并合入。

Automated review generated by Antigravity.

liugddx and others added 2 commits November 5, 2026 11:59
…h, store only what a wake needs

Follow-up to #6624, following the review there.

- watches() returns each record's decode result. wake_bot_pass checks that the
  worker is alive first, so a watch of an archived or missing worker is retired
  without being decoded; for a live worker it collects the error, keeps waking the
  healthy ones and returns the combined error, which start_bot_wake already
  de-duplicates. The in-memory set of reported workers is removed.
- Bot::watch reads the record itself, so a value that no longer decodes is
  replaced by the refresh with notified unset instead of failing every send.
- A watch no longer embeds ReturnRoute and AcceptedModel. It keeps the model
  selection, thinking level, workspace, binding and notified, and the model is
  resolved with Source::select when the Bot is woken, so a later change to an
  accepted model cannot make stored watches unreadable.

Generated-by: Claude Code
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Avoid running Source::select on every store-change pass for finished workers
while the Bot Session is busy or parked on an interaction; dead-worker watch
retirement above remains unaffected.

Generated-by: Antigravity
@M4n5ter
M4n5ter force-pushed the fix/bot-watch-heal branch from 17fa84a to 111a8b8 Compare November 5, 2026 04:06
@M4n5ter
M4n5ter merged commit 390360d into rust-preview Nov 5, 2026
1 check passed
@M4n5ter
M4n5ter deleted the fix/bot-watch-heal branch November 5, 2026 04:06

@Astro-Han Astro-Han left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review notice: This comment was posted by an automated review agent. It is not an independent human review and does not replace one.

I checked the update at 111a8b8d24a831395786f4d59f242631fad3e63f. The original patch is equivalent to the previously reviewed 17fa84a5 after rebasing; the additional four lines defer model reselection while the Bot is busy or has a parked interaction. Retirement of missing/archived workers still occurs before that guard, and the existing notification admission checks remain in place. The wake loop retains both store-change and periodic retries. I found no additional confirmed P0–P2 in this increment.

The P2 upgrade issue in my earlier review remains: the persisted Watch declaration is byte-for-byte unchanged from that reviewed version and still rejects the preceding build's valid route-based records. A live worker's completion is skipped until another send replaces its watch. The new busy-Bot guard runs after decoding and does not repair or migrate those records. The PR description now explicitly acknowledges this case; deleting watches is not evidence that pending completion notifications are preserved.

I read the complete four-file patch and compared the authored revisions with git range-diff, separating the incoming branch changes. Actual merge into ed79386f826ab0c2d4267dafa361240487d2f30e succeeded. The current head's CI is queued. I did not rerun the earlier Rust decoding probe, native wake tests, or a desktop/provider session; this update is a source and integration comparison, not a new runtime validation.


Automated review notice: This comment was posted by an automated review agent operated by Astro-Han. It is not an independent human review and does not replace one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants