Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

fix(core): a takeover under a selector drops the memo of every old owner (#2868)

Jiangzhou237969820a7f
diff --git a/docs/src/content/docs/programming_guide/app.mdx b/docs/src/content/docs/programming_guide/app.mdx
index 59382943..6b8faf85 100644
--- a/docs/src/content/docs/programming_guide/app.mdx
+++ b/docs/src/content/docs/programming_guide/app.mdx
@@ -311,7 +311,7 @@ Details to rely on:
- **Live mode:** the selector holds for the whole update, including `operator.update` and `operator.delete` calls from later live events. A live component already running in another update keeps running if this update is a preview or a one-shot update, or if the component is unselected or only an ancestor of a selected path. A one-shot update that selects the component runs its own instance beside the running one, as a plain one-shot update does (see [A catch-up update beside a running live update](../advanced_topics/live_component#a-catch-up-update-beside-a-running-live-update)). A live update that selects the component replaces it, as any second live update does. A source that allows one watcher at a time can fail the second run. Values that a live component writes with `write_committed_state` under a selector are not persisted unless the component is selected, because a partial run has not earned a marker such as "full scan done".
- **State of the components that run.** `coco.use_state` values of the components that run, ancestors included, are persisted as usual. Don't store a marker that claims the whole subtree ran (such as "full scan done") with `use_state` in a component that a selector can run partly.
- **Preview** returns the actions the selective update would take: those of the components that run (the selected ones, plus any changes to the own target states of the ancestors and `use_mount` children that run), and the deletions of the selected components that are no longer mounted, by the rules above. As in any preview, a failure that happens only when a sink applies its actions shows up only in the update.
-- **Taking over a target state.** A selected component that declares a target state another component owns takes it over, as it would in a plain update. If the update leaves the other component out, it and its ancestors lose their memos, so that the next update that covers them runs them. The rest of the other component is left alone.
+- **Taking over a target state.** A selected component that declares a target state another component owns takes it over, as it would in a plain update. The other component and its ancestors lose their memos, so that the next update that covers them runs them: even a selected component may not run, for example below an ancestor component that is no longer mounted. The rest of the other component is left alone.
- **Empty selector.** `component_selector=[]` selects nothing: the root and its `use_mount` children run, and no other component does. `None`, the default, is a plain update.
- **A source is read only as far as needed.** A `mount_each()` that the selector leaves out does not read its items at all. One that is only an ancestor of selected paths reads just the selected keys from a source that can read by key, such as `localfs.walk_dir(...).items()` outside live mode, which checks the selected files instead of walking the directory. A selected key that the source no longer has is deleted, as in a plain update. Any other source is listed in full, and `mount_each()` skips the unselected items. A `mount_each()` that is not read cannot report a duplicate key, so a key used twice may only show up in a plain update.
- In Rust, the selector filters `ctx.mount`, `ctx.mount_each` and live components. `ctx.scope`, `use_mount!` and `use_mount_each!` return values, so they always run.
diff --git a/python/tests/core/test_component_selector.py b/python/tests/core/test_component_selector.py
index 9db8f879..f21d2a10 100644
--- a/python/tests/core/test_component_selector.py
+++ b/python/tests/core/test_component_selector.py
@@ -959,6 +959,54 @@ def test_selected_component_takes_over_a_target_state_of_an_unselected_one() ->
assert _stored() == {"x": 2, "c1_own": 1}
+def test_takeover_from_a_selected_owner_below_a_gone_ancestor_drops_its_memo() -> None:
+ """The old owner is selected but does not run: its ancestor component is no
+ longer mounted, so it stays whole. Its memo must go with the target state,
+ or a later memo hit declares nothing and the state is deleted with the
+ component that took it. (Fuzz seed 487.)"""
+
+ @coco.fn(memo=True)
+ async def leaf(value: int) -> None:
+ _runs.append("leaf")
+ _declare("k", value)
+
+ @coco.fn(memo=True)
+ async def parent(value: int) -> None:
+ await coco.mount(coco.component_subpath("x"), leaf, value)
+
+ where = ["old"]
+
+ @coco.fn
+ async def main() -> None:
+ await coco.mount(coco.component_subpath(where[0]), parent, 1)
+
+ app = _app("takeover_below_gone", main)
+ _run_all_then_clear_log(app)
+
+ where[0] = "new"
+ app.update_blocking(
+ component_selector=[
+ coco.ROOT_PATH / "new" / "x",
+ coco.ROOT_PATH / "old" / "x",
+ ]
+ )
+ assert _runs == ["leaf"]
+ assert common.list_target_state_owners_sync(app)[
+ '/@test_target_state/global_dict/"k"'
+ ] == (coco.ROOT_PATH / "new" / "x")
+
+ # Back where it was: the plain update deletes "new" and must run the old
+ # owner again, not reuse its memo.
+ where[0] = "old"
+ _runs.clear()
+ app.update_blocking()
+ assert _runs == ["leaf"]
+ assert _stored() == {"k": 1}
+ assert common.list_target_state_owners_sync(app)[
+ '/@test_target_state/global_dict/"k"'
+ ] == (coco.ROOT_PATH / "old" / "x")
+
+
# ---------------------------------------------------------------------------
# Live components
# ---------------------------------------------------------------------------
diff --git a/rust/core/src/engine/execution.rs b/rust/core/src/engine/execution.rs
index 112515e1..d183fb92 100644
--- a/rust/core/src/engine/execution.rs
+++ b/rust/core/src/engine/execution.rs
@@ -1928,16 +1928,17 @@ async fn pre_commit<'tracking, Prof: EngineProfile>(
// An old owner this update does not run keeps a memo that says it declares
// the target state: a later update would reuse it, declare nothing, and
// leave the state to whoever took it. Its memo, and each ancestor's, must
- // go. An owner the update runs settles it itself, as in a plain update.
- let preempted_owners_left_out: Vec<StablePath> = selector
- .map(|selector| {
- preempted_owner_updates
- .keys()
- .filter(|owner| !selector.update_covers(owner))
- .cloned()
- .collect()
- })
- .unwrap_or_default();
+ // go. Under a selector, being covered does not mean being run: a selected
+ // owner below an ancestor component that is no longer mounted stays whole,
+ // unrun, and this commit cannot tell. So every preempted owner loses its
+ // memo; one that does run writes a new one when it commits, and a plain
+ // update runs every owner, which settles it as usual.
+ let preempted_owner_memos_to_drop: Vec<StablePath> = match selector {
+ Some(selector) if !selector.selects_everything() => {
+ preempted_owner_updates.keys().cloned().collect()
+ }
+ _ => Vec::new(),
+ };
// Provider-generation updates: buffered into the output, applied
// by `submit()` after the precommit txn commits β€” so a re-run of this
@@ -1955,7 +1956,7 @@ async fn pre_commit<'tracking, Prof: EngineProfile>(
self_path: stable_path.clone(),
new_tracking_info: new_tracking_info_bytes,
preempted_owner_updates,
- preempted_owners_left_out,
+ preempted_owner_memos_to_drop,
segment_names,
},
})
diff --git a/rust/core/src/state/component_selector.rs b/rust/core/src/state/component_selector.rs
index d834c25d..8ed96284 100644
--- a/rust/core/src/state/component_selector.rs
+++ b/rust/core/src/state/component_selector.rs
@@ -116,17 +116,6 @@ impl ComponentSelector {
self.classify(path) == ComponentSelection::Selected
}
- /// The update covers `path`, wherever this view's run is. Unlike
- /// [`Self::covers`], a view inside a selected subtree answers for paths
- /// outside it too.
- pub(crate) fn update_covers(&self, path: &StablePath) -> bool {
- Self {
- root: self.root.clone(),
- within_selected: false,
- }
- .covers(path)
- }
-
/// The update runs the component at `path`, wherever this view's run is:
/// it is selected or leads to a selected path. A `use_mount` child that
/// runs anyway is not counted.
diff --git a/rust/core/src/state_store/submit_session.rs b/rust/core/src/state_store/submit_session.rs
index 6acc2c9c..dc6c4e7a 100644
--- a/rust/core/src/state_store/submit_session.rs
+++ b/rust/core/src/state_store/submit_session.rs
@@ -170,9 +170,10 @@ pub struct PrecommitWritePlan {
/// For each preempted other-owner: their new tracking-info bytes
/// (with the preempted item removed by the engine).
pub preempted_owner_updates: BTreeMap<StablePath, Vec<u8>>,
- /// The preempted owners the update does not run: their memos, and their
- /// ancestors', are dropped with the takeover.
- pub preempted_owners_left_out: Vec<StablePath>,
+ /// The preempted owners whose memos, and their ancestors', are dropped
+ /// with the takeover: under a selector, every preempted owner, since the
+ /// update may not run it.
+ pub preempted_owner_memos_to_drop: Vec<StablePath>,
/// Readable names for the provider segments of the declared
/// target-state paths, keyed by lone segment fingerprint. Applied
/// write-once (existing entries left untouched) so inspection can
@@ -443,11 +444,12 @@ impl AppStore {
.write_tracking_info_raw(wtxn, &owner_path, &bytes)
.await?;
}
- // An owner left out of the update no longer holds what
- // its memo, and each ancestor's, say it declares: a hit
- // in a later update would leave the target state to
- // whoever took it, and delete it with them.
- for owner_path in &plan.preempted_owners_left_out {
+ // A preempted owner the update may not run no longer
+ // holds what its memo, and each ancestor's, say it
+ // declares: a hit in a later update would leave the
+ // target state to whoever took it, and delete it with
+ // them.
+ for owner_path in &plan.preempted_owner_memos_to_drop {
let mut memo_path = owner_path.as_ref();
loop {
app_store