Incremental engine for long horizon agents π Star if you like it!
Rust agentic-data-frameworkaiai-agentschange-data-capturecodebase-intelligencecontext-engineeringdata-engineeringdata-indexingdata-processingetlhelp-wantedindexingknowledge-graphllmlong-horizon-agentpythonragreal-timerustsemantic-search
docs(advanced): Python and Rust pages for custom target connectors (#2753)
LJ π₯₯π΄afb418dbf73d
diff --git a/docs/src/content/docs/advanced_topics/custom_target_connector.mdx b/docs/src/content/docs/advanced_topics/custom_target_connector.mdxindex ae6142cf..76987709 100644--- a/docs/src/content/docs/advanced_topics/custom_target_connector.mdx+++ b/docs/src/content/docs/advanced_topics/custom_target_connector.mdx@@ -8,7 +8,7 @@ toc_max_heading_level: 4---import LangTabs from '../../../components/LangTabs.astro';-A **custom target connector** is the mechanism that connects CocoIndex's declarative target state system to external systems. When you call methods like `dir_target.declare_file()` or `table_target.declare_row()`, a target connector handles the actual synchronization β determining what changed and applying those changes to the external system.+A **custom target connector** is the mechanism that connects CocoIndex's declarative target state system to external systems. When you call methods like <Lang only="python">`dir_target.declare_file()` or `table_target.declare_row()`</Lang><Lang only="rust">`dir_target.declare_file(ctx, name, content)` or `table_target.declare_row(ctx, &row)`</Lang>, a target connector handles the actual synchronization β determining what changed and applying those changes to the external system.## When to create a custom target connector@@ -26,11 +26,15 @@ For simple use cases where you just need to write data to an external system witThe labels distinguish types you implement from types CocoIndex provides.-The Rust SDK has the same types, in `cocoindex::prelude`: the `TargetHandler<V>` trait, `TargetActionSink<A>`, `TargetAction<A>`, `TargetReconcileOutput<A, R>`, `TargetStateProvider<V>`, `TargetState<V>`, `ChildTargetDef`, and the functions `register_root_target_states_provider`, `declare_target_state`, `declare_target_state_with_child` and `mount_target`. Each Python snippet below has a Rust tab where the surface matches. For a complete Rust connector, read `rust/sdk/cocoindex/src/connectors/valkey.rs` (a container target with child documents) or `rust/sdk/cocoindex/src/resources/live_map.rs` (the smallest container target).+<Lang only="rust">++The types are in `cocoindex::prelude`: the `TargetHandler<V>` trait, `TargetActionSink<A>`, `TargetAction<A>`, `TargetReconcileOutput<A, R>`, `TargetChildInvalidation`, `TargetStateProvider<V>`, `TargetState<V>`, `ChildTargetDef`, `StableKey`, and the functions `register_root_target_states_provider`, `declare_target_state`, `declare_target_state_with_child` and `mount_target`. The Rust snippets on this page come from `rust/sdk/cocoindex/tests/target_state.rs`, which exercises each piece without an external service. For a complete connector against a database, read `rust/sdk/cocoindex/src/connectors/valkey.rs` (a container target with child documents).++</Lang>### TargetHandler *(you implement)*-A `TargetHandler` implements the reconciliation logic. It's a protocol with one required method and one optional method:+A `TargetHandler` implements the reconciliation logic. It's a <Lang only="python">protocol</Lang><Lang only="rust">trait</Lang> with one required method and one optional method:{/* Rust: rust/sdk/cocoindex/src/target_state.rs (TargetHandler) */}<LangTabs>@@ -79,11 +83,13 @@ where}```-The value type `V` is the trait's type parameter; the tracking record and the action are associated types. `desired_target_state` is `None` where Python passes `NON_EXISTENCE`. The child handler type is not part of the trait: a container's sink builds it (see [container targets](#implementing-container-targets)).+The value type `V` is the trait's type parameter; the tracking record and the action are associated types. The child handler type is not part of the trait: a container's sink builds it (see [container targets](#implementing-container-targets)).</Fragment></LangTabs>+<Lang only="python">+**Type Parameters:**- `ValueT`: The specification for the target state (e.g., file content, row data)@@ -102,10 +108,38 @@ The value type `V` is the trait's type parameter; the tracking record and the ac- `TargetReconcileOutput` if an action is needed- `None` if no changes are required+</Lang>++<Lang only="rust">++**Types:**++- `V`: The specification for the target state (e.g., file content, row data). It is `Serialize + DeserializeOwned`.+- `TrackingRecord`: What's stored to detect changes on future runs+- `Action`: What the sink applies to the external system++**Parameters:**++- `key`: `StableKey` β the key the target state was declared with+- `desired_target_state`: What the user declared, or `None` if no longer declared+- `prev_possible_records`: Tracking records from previous runs (may have multiple due to interrupted updates)+- `prev_may_be_missing`: If `true`, the target state might not exist in the external system++**Returns:**++- `Ok(Some(TargetReconcileOutput { .. }))` if an action is needed+- `Ok(None)` if no changes are required++A container handler (one used with `declare_target_state_with_child` or `mount_target`) must return `Some` output whenever `desired_target_state` is `Some`, even when nothing changed, and its action must be a `Create` or an `Update`: the sink's child handler factory builds the child handler from that action. Returning `None` leaves the child handler missing, and declaring a child target state then fails.++</Lang>+:::warning[Non-blocking and side-effect free]The `reconcile()` method must be **non-blocking** and **free of side effects**. It should only compare states and return an action β actual I/O operations happen later in the `TargetActionSink`. CocoIndex may call `reconcile()` more than once for the same target state when it retries a commit internally; only the output of the committed attempt is applied.:::+<Lang only="python">+:::info[Type annotations]Annotate the `prev_possible_records` parameter with `Collection[YourTrackingRecord]` so CocoIndex can properly reconstruct stored tracking records during deserialization. See [Serialization](../programming_guide/serialization) for details on supported types.:::@@ -116,13 +150,27 @@ CocoIndex holds every declared target state until its processing component commiSo don't rely on the identity of a declared value or an action, or pass mutable state through them; and since the copy is taken at declaration, changes made to a value after it was declared are not seen.:::+</Lang>++<Lang only="rust">++:::info[Values and actions are serialized]+A declared value is serialized when it is declared, and `reconcile` receives a `V` deserialized from that form. The actions and tracking records `reconcile` returns are serialized the same way, and the sink receives actions deserialized from them. So changes made to a value after it was declared are not seen, and no state passes through a value or an action other than its serialized fields.+:::++</Lang>+### Tracking record *(you define)*A **tracking record** captures the essential information needed to detect changes. Good tracking records:- Are **minimal**: Only include what's needed for change detection- Are **deterministic**: Same input always produces the same record-- Are **serializable**: Must be persistable (typically a NamedTuple or dataclass). Dataclasses and NamedTuples are serialized with msgspec automatically. For types requiring pickle, use `@coco.serialize_by_pickle`. In Rust, `TrackingRecord` is any `Serialize + DeserializeOwned` type; `cocoindex::Fingerprint::from_bytes(&bytes)` gives a content fingerprint.+- Are **serializable**: Must be persistable<Lang only="python"> (typically a NamedTuple or dataclass). Dataclasses and NamedTuples are serialized with msgspec automatically. For types requiring pickle, use `@coco.serialize_by_pickle`.</Lang><Lang only="rust">: any `Serialize + DeserializeOwned` type.</Lang>++{/* Rust: rust/sdk/cocoindex/tests/target_state.rs (TableSpec, the tracking record of TableHandler) */}+<LangTabs>+<Fragment slot="python">```python# Example: File tracking record@@ -131,6 +179,22 @@ class _FileTrackingRecord:fingerprint: bytes # Content hash for change detection```+</Fragment>+<Fragment slot="rust">++```rust+// Example: a table's tracking record (here, the declared table spec itself)+#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]+struct TableSpec {+ generation: String,+}+```++</Fragment>+</LangTabs>++<Lang only="python">+:::tip[Fingerprinting]For content-based change detection, use the `connectorkits.fingerprint` utilities. This lets you detect changes without storing the full content:@@ -149,10 +213,24 @@ fp = fingerprint_object(obj):::+</Lang>++<Lang only="rust">++:::tip[Fingerprinting]+For content-based change detection, keep a fingerprint instead of the full content: `cocoindex::Fingerprint::from(&value)` fingerprints any `Serialize` value, and `Fingerprint::from_bytes(&bytes)` fingerprints raw bytes. `Fingerprint` is itself `Serialize + DeserializeOwned + PartialEq`, so it can be the tracking record.+:::++</Lang>+### Action and TargetActionSink *(you implement)*An **action** (you define) describes what operation to perform on the external system:+{/* Rust: rust/sdk/cocoindex/tests/target_state.rs (RowAction) */}+<LangTabs>+<Fragment slot="python">+```python# Example: File actionclass _FileAction(NamedTuple):@@ -160,9 +238,24 @@ class _FileAction(NamedTuple):content: bytes | None # None means delete```+</Fragment>+<Fragment slot="rust">++```rust+/// One row mutation the sink applies. `value: None` is a delete.+#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]+struct RowAction {+ key: String,+ value: Option<String>,+}+```++</Fragment>+</LangTabs>+A **TargetActionSink** batches and executes actions:-{/* Rust: rust/sdk/cocoindex/tests/deadline.rs (declare_memory_row), rust/sdk/cocoindex/src/target_state.rs (TargetActionSink::from_async_fn) */}+{/* Rust: rust/sdk/cocoindex/tests/target_state.rs (recording_sink), rust/sdk/cocoindex/src/target_state.rs (TargetActionSink::from_async_fn) */}<LangTabs><Fragment slot="python">@@ -194,32 +287,37 @@ def apply_actions(<Fragment slot="rust">```rust-#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]-struct WriteAction {- key: String,- value: String,-}--let sink = TargetActionSink::from_async_fn(move |actions| {- let applied = applied.clone();- async move {- let mut applied = applied.lock().unwrap();- for action in actions {- if let TargetAction::Update(action) = action {- applied.push(action);+/// A sink that records each applied action into `log` as `"<verb> <key>[=<value>]"`.+fn recording_sink(log: Log) -> TargetActionSink<RowAction> {+ TargetActionSink::from_async_fn(move |actions: Vec<TargetAction<RowAction>>| {+ let log = log.clone();+ async move {+ let mut log = log.lock().unwrap();+ for action in actions {+ let (verb, row) = match action {+ TargetAction::Create(r) => ("create", r),+ TargetAction::Update(r) => ("update", r),+ TargetAction::Delete(r) => ("delete", r),+ };+ log.push(match row.value {+ Some(v) => format!("{verb} {}={}", row.key, v),+ None => format!("{verb} {}", row.key),+ });}+ Ok(())}- Ok(())- }-});+ })+}```-A Rust sink is always async. Its closure receives `Vec<TargetAction<A>>`, where each item is `TargetAction::Create(a)`, `Update(a)` or `Delete(a)`, so the operation kind travels beside your action. There is no `context_provider` argument: move the connection into the closure, or read it from the `Ctx` when you build the handler. The action type must be `Serialize + DeserializeOwned`.+A sink is always async. Its closure receives `Vec<TargetAction<A>>`, where each item is `TargetAction::Create(a)`, `Update(a)` or `Delete(a)`, so the operation kind travels beside your action. The action type must be `Serialize + DeserializeOwned`. The closure gets no connection from the environment: move the connection into it when you build the handler (see [Use `ContextKey` for external resource identity](#use-contextkey-for-external-resource-identity)).++The engine batches actions by sink *identity*: actions returned with the same sink (or a clone of it) are applied together in one call, and every `from_async_fn*` call creates a new identity. Build the sink once and return a clone from each `reconcile`.</Fragment></LangTabs>-A **container** target's sink also takes a `child_handler` factory, `child_handler(context_provider, action) -> handler`, which returns the handler for the child target states of a created or updated container. The callback stays the same: it applies the actions. See [Implementing container targets](#implementing-container-targets).+A **container** target's sink also takes a `child_handler` factory, <Lang only="python">`child_handler(context_provider, action) -> handler`</Lang><Lang only="rust">`Fn(&A) -> Result<ChildTargetDef>`</Lang>, which returns the handler for the child target states of a created or updated container. The callback stays the same: it applies the actions. See [Implementing container targets](#implementing-container-targets).### TargetReconcileOutput *(you return)*@@ -252,13 +350,17 @@ pub struct TargetReconcileOutput<A, R> {</Fragment></LangTabs>+<Lang only="python">+`OptChildHandlerT` is the handler type of the child target states, which the sink's `child_handler` factory returns for the action (`None` for leaf targets). Nothing in the tuple depends on it; it is how a container handler declares its child handler type.+</Lang>+The `child_invalidation` field is only relevant for **container targets** (those with children). See [Child invalidation](#child-invalidation) for details.### TargetStateProvider *(CocoIndex provides)*-A `TargetStateProvider` is a factory that creates `TargetState` objects. You don't implement this class β CocoIndex gives you one when you register a handler or declare a target state with children.+A `TargetStateProvider` is a factory that creates `TargetState` objects. You don't implement this <Lang only="python">class</Lang><Lang only="rust">type</Lang> β CocoIndex gives you one when you register a handler or declare a target state with children.{/* Rust: rust/sdk/cocoindex/src/target_state.rs (register_root_target_states_provider, declare_target_state_with_child, mount_target, TargetStateProvider::attachment) */}<LangTabs>@@ -335,11 +437,11 @@ Root target states are not nested inside another target.Understanding what happens at runtime:-1. **Registration**: You define a `TargetHandler` and call `register_root_target_states_provider()`. CocoIndex returns a `TargetStateProvider` β a factory for creating target states associated with your handler.+1. **Registration**: You define a `TargetHandler` and call `register_root_target_states_provider()`<Lang only="rust"> from inside a component, on every run</Lang>. CocoIndex returns a `TargetStateProvider` β a factory for creating target states associated with your handler.2. **Declaration**: During execution, user code calls `provider.target_state(key, spec)` to create `TargetState` objects, then `declare_target_state()` to declare them. CocoIndex collects all declared target states.-3. **Reconciliation**: When the processing unit finishes, CocoIndex calls your handler's `reconcile()` method for each target state. For declared target states, `desired_target_state` contains the spec; for previously declared but now missing states, `desired_target_state` is `NON_EXISTENCE` (triggering cleanup). Your `reconcile()` compares the desired state with previous records and returns `TargetReconcileOutput` if an action is needed, or `None` if no changes are required.+3. **Reconciliation**: When the processing unit finishes, CocoIndex calls your handler's `reconcile()` method for each target state. For declared target states, `desired_target_state` contains the spec; for previously declared but now missing states, `desired_target_state` is <Lang only="python">`NON_EXISTENCE`</Lang><Lang only="rust">`None`</Lang> (triggering cleanup). Your `reconcile()` compares the desired state with previous records and returns <Lang only="python">`TargetReconcileOutput` if an action is needed, or `None`</Lang><Lang only="rust">`Some(TargetReconcileOutput { .. })` if an action is needed, or `None`</Lang> if no changes are required.4. **Action Execution**: CocoIndex batches actions by their `TargetActionSink` and executes them. The sink applies changes to the external system (database writes, file operations, API calls, etc.). A batch may combine the actions of several processing components that finish around the same time; if the sink fails, CocoIndex retries in smaller batches along component boundaries, so only the components whose actions actually fail are affected. The sink may therefore see an action from a failed batch again β keep actions idempotent.@@ -353,6 +455,10 @@ Due to interrupted updates, `prev_possible_records` may contain multiple recordsStart by defining the types for your provider:+{/* Rust: rust/sdk/cocoindex/tests/target_state.rs (RowAction; RowHandler keeps the row value as its tracking record) */}+<LangTabs>+<Fragment slot="python">+```pythonfrom typing import NamedTuple, Collectionfrom dataclasses import dataclass@@ -377,9 +483,38 @@ class _RowAction(NamedTuple):data: dict[str, Any] | None # None = delete```+</Fragment>+<Fragment slot="rust">++```rust+use cocoindex::{+ Result, StableKey, TargetAction, TargetActionSink, TargetHandler, TargetReconcileOutput,+ declare_target_state, register_root_target_states_provider,+};+use serde::{Deserialize, Serialize};++// Key: `StableKey`, built from anything that implements `IntoStableKey`+// (`&str`, `String`, `i64`, `u64`, ...).++// Value: what the user declares. Here, a `String` row value.++// Tracking record: what to persist for change detection. Here, the value+// itself (`String`).++/// Action: one row mutation the sink applies. `value: None` is a delete.+#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]+struct RowAction {+ key: String,+ value: Option<String>,+}+```++</Fragment>+</LangTabs>+### Step 2: Implement the handler-{/* Rust: rust/sdk/cocoindex/tests/deadline.rs (MemoryHandler) */}+{/* Rust: rust/sdk/cocoindex/tests/target_state.rs (RowHandler, key_str) */}<LangTabs><Fragment slot="python">@@ -445,43 +580,70 @@ class _RowHandler(coco.TargetHandler[_RowSpec, _RowTrackingRecord]):<Fragment slot="rust">```rust+/// A flat row handler with full CRUD + no-change detection (tracking record is+/// the row value itself).#[derive(Clone)]-struct MemoryHandler {- sink: TargetActionSink<WriteAction>,+struct RowHandler {+ sink: TargetActionSink<RowAction>,}-impl TargetHandler<String> for MemoryHandler {+impl TargetHandler<String> for RowHandler {type TrackingRecord = String;- type Action = WriteAction;+ type Action = RowAction;fn reconcile(&self,key: StableKey,- desired_target_state: Option<String>,- prev_possible_records: Vec<Self::TrackingRecord>,- _prev_may_be_missing: bool,- ) -> cocoindex::Result<Option<TargetReconcileOutput<Self::Action, Self::TrackingRecord>>> {- let Some(value) = desired_target_state else {- return Ok(None);- };- // Skip if unchanged- if prev_possible_records.iter().any(|prev| prev == &value) {- return Ok(None);+ desired: Option<String>,+ prev: Vec<String>,+ prev_may_be_missing: bool,+ ) -> Result<Option<TargetReconcileOutput<RowAction, String>>> {+ let k = key_str(&key);+ match desired {+ Some(value) => {+ // Skip if unchanged+ let unchanged =+ !prev_may_be_missing && !prev.is_empty() && prev.iter().all(|p| *p == value);+ if unchanged {+ return Ok(None);+ }+ let row = RowAction {+ key: k,+ value: Some(value.clone()),+ };+ let action = if prev.is_empty() {+ TargetAction::Create(row)+ } else {+ TargetAction::Update(row)+ };+ Ok(Some(TargetReconcileOutput {+ action,+ sink: self.sink.clone(),+ tracking_record: Some(value),+ child_invalidation: None,+ }))+ }+ // Handle deletion+ None => {+ if prev.is_empty() && !prev_may_be_missing {+ return Ok(None); // Nothing to delete+ }+ Ok(Some(TargetReconcileOutput {+ action: TargetAction::Delete(RowAction {+ key: k,+ value: None,+ }),+ sink: self.sink.clone(),+ tracking_record: None,+ child_invalidation: None,+ }))+ }}- Ok(Some(TargetReconcileOutput {- action: TargetAction::Update(WriteAction {- key: key.to_string(),- value: value.clone(),- }),- sink: self.sink.clone(),- tracking_record: Some(value),- child_invalidation: None,- }))}}```-This handler keeps the value itself as the tracking record and never deletes; a real one returns `TargetAction::Delete(..)` with `tracking_record: None` when `desired_target_state` is `None` and a previous record exists.+`key_str` is a helper of the test that renders the `StableKey` as a string; `sink` is the `recording_sink` [above](#action-and-targetactionsink-you-implement).</Fragment></LangTabs>@@ -490,7 +652,7 @@ This handler keeps the value itself as the tracking record and never deletes; aFor root-level target states (not nested within another target), register a provider:-{/* Rust: rust/sdk/cocoindex/tests/deadline.rs (declare_memory_row) */}+{/* Rust: rust/sdk/cocoindex/tests/target_state.rs (flat_target_insert_update_nochange_delete) */}<LangTabs><Fragment slot="python">@@ -506,20 +668,29 @@ _row_provider = coco.register_root_target_states_provider(```rustlet provider = register_root_target_states_provider(- ctx,+ &ctx,"mycompany.io/mydb/row", // Unique provider name- MemoryHandler { sink },+ RowHandler {+ sink: recording_sink(log),+ },)?;+for (k, v) in rows {+ declare_target_state(&ctx, provider.target_state(k, v.to_string()))?;+}```-A Rust provider is registered for the current run, from inside a component. Once a run stops registering it, the engine cannot reach its handler, and the target states declared through it are left in place.+The provider is registered for the current run, from inside a component, so register it on every run. Once a run stops registering it, the engine cannot reach its handler, and the target states declared through it are left in place; `App::drop_state` leaves them in place too.</Fragment></LangTabs>### Step 4: Create user-facing APIs-Wrap the provider in a user-friendly API. In Rust, this is a struct that holds the `TargetStateProvider<V>` and whose methods take `&Ctx`, as `DirTarget::declare_file(ctx, name, content)` does.+Wrap the provider in a user-friendly API<Lang only="rust">: a struct that holds the `TargetStateProvider<V>` and whose methods take `&Ctx`</Lang>.++{/* Rust: rust/sdk/cocoindex/src/connectors/valkey.rs (IndexTarget::declare_document) */}+<LangTabs>+<Fragment slot="python">```pythonclass TableTarget:@@ -534,6 +705,28 @@ class TableTarget:coco.declare_target_state(target_state)```+</Fragment>+<Fragment slot="rust">++```rust+pub struct IndexTarget {+ index_name: Arc<str>,+ documents: TargetStateProvider<Document>,+}++impl IndexTarget {+ /// Declare a document to upsert into the index.+ pub fn declare_document(&self, ctx: &Ctx, document: Document) -> Result<()> {+ // ... validate the document ...+ let key = StableKey::Str(Arc::from(document.id.as_str()));+ declare_target_state(ctx, self.documents.target_state(key, document))+ }+}+```++</Fragment>+</LangTabs>+## Implementing container targetsContainer targets (directories, tables) have children (files, rows). A child is a non-root target state.@@ -544,8 +737,8 @@ For targets **nested inside another target** (e.g., files inside a directory), tFor root targets, you call `register_root_target_states_provider()` and immediately get a provider with your handler. For non-root targets, the handler comes from the **`child_handler` factory of the parent's sink**, which CocoIndex calls with the parent's action:-1. **Declaration**: Call `declare_target_state_with_child(parent_ts)` β returns an **unresolved** child provider immediately-2. **Resolution**: When the parent reconciles, CocoIndex calls the sink's `child_handler` factory with the parent's action and resolves the child provider with the handler it returns. It does this before the sink applies the action, and in a preview (`app.update(preview=True)`) in place of applying it β so the factory must build the handler from the action alone, with no side effects.+1. **Declaration**: Call <Lang only="python">`declare_target_state_with_child(parent_ts)`</Lang><Lang only="rust">`declare_target_state_with_child(ctx, parent_ts)`</Lang> β returns an **unresolved** child provider immediately<Lang only="rust">. Or call `mount_target(ctx, parent_ts).await`, which declares and commits the parent in a foreground child component and returns a child provider that is already resolved.</Lang>+2. **Resolution**: When the parent reconciles, CocoIndex calls the sink's `child_handler` factory with the parent's action and resolves the child provider with the handler it returns. It does this before the sink applies the action, and in a preview (<Lang only="python">`app.update(preview=True)`</Lang><Lang only="rust">`app.preview(..)`</Lang>) in place of applying it β so the factory must build the handler from the action alone, with no side effects.3. **Usage**: The child provider can now create child target states, which follow the same reconciliation β execution β tracking flow as root targets.Some containers only get their identity when created: the external system assigns it (a Notion database or a Google Drive folder gets a server-assigned id). The factory cannot know that id, so let the handler look it up when it applies: the container sink records `key β id` in a connector-level map when it creates (or finds) the container, and the child handler's sink reads the id from that map by the container's key. Containers reconcile and apply on every run, before their children apply, so the map is filled when the children need it; a preview never applies the children, so it never needs the id.@@ -554,14 +747,18 @@ Some containers only get their identity when created: the external system assignWhen a container target undergoes certain changes, the child target states may be affected. The `child_invalidation` field in `TargetReconcileOutput` lets you signal this to CocoIndex:-- **`"destructive"`** β The container change destroys all existing children (e.g., a primary key change that requires dropping and recreating a table). CocoIndex will ignore all previous tracking records for children under this container and treat them as new.+- **<Lang only="python">`"destructive"`</Lang><Lang only="rust">`TargetChildInvalidation::Destructive`</Lang>** β The container change destroys all existing children (e.g., a primary key change that requires dropping and recreating a table). CocoIndex will ignore all previous tracking records for children under this container and treat them as new.-- **`"lossy"`** β The container change may cause data loss for existing children (e.g., a schema change that removes columns). CocoIndex will force an upsert for all children by setting `prev_may_be_missing=True`, even if their data appears unchanged.+- **<Lang only="python">`"lossy"`</Lang><Lang only="rust">`TargetChildInvalidation::Lossy`</Lang>** β The container change may cause data loss for existing children (e.g., a schema change that removes columns). CocoIndex will force an upsert for all children by setting <Lang only="python">`prev_may_be_missing=True`</Lang><Lang only="rust">`prev_may_be_missing` to `true`</Lang>, even if their data appears unchanged.- **`None`** (default) β No impact on children. Normal change detection applies.Set `child_invalidation` in the **parent handler's** `reconcile()` method when you detect that the container itself has changed in a way that affects its children:+{/* Rust: rust/sdk/cocoindex/tests/target_state.rs (TableSpec, TableAction, TableHandler) */}+<LangTabs>+<Fragment slot="python">+```pythonclass _DirHandler(coco.TargetHandler[_DirSpec, _DirTrackingRecord, _EntryHandler]):def reconcile(self, key, desired_target_state, prev_possible_records, prev_may_be_missing, /):@@ -580,19 +777,103 @@ class _DirHandler(coco.TargetHandler[_DirSpec, _DirTrackingRecord, _EntryHandler)```+</Fragment>+<Fragment slot="rust">++```rust+#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]+struct TableSpec {+ generation: String,+}++#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]+struct TableAction {+ name: String,+ drop: bool,+}++/// A container ("table") handler whose sink produces a child row handler per+/// declared table, and whose child invalidation is configurable.+struct TableHandler {+ sink: TargetActionSink<TableAction>,+ invalidation: Option<TargetChildInvalidation>,+}++impl TargetHandler<TableSpec> for TableHandler {+ type TrackingRecord = TableSpec;+ type Action = TableAction;++ fn reconcile(+ &self,+ key: StableKey,+ desired: Option<TableSpec>,+ prev: Vec<TableSpec>,+ _prev_may_be_missing: bool,+ ) -> Result<Option<TargetReconcileOutput<TableAction, TableSpec>>> {+ let name = key_str(&key);+ match desired {+ // Always emit an output when the table is declared, so the sink+ // builds the child handler.+ Some(spec) => {+ let changed = !prev.is_empty() && !prev.contains(&spec);+ let action = if prev.is_empty() {+ TargetAction::Create(TableAction { name, drop: false })+ } else {+ TargetAction::Update(TableAction { name, drop: false })+ };+ Ok(Some(TargetReconcileOutput {+ action,+ sink: self.sink.clone(),+ tracking_record: Some(spec),+ child_invalidation: changed.then_some(self.invalidation).flatten(),+ }))+ }+ None => Ok(Some(TargetReconcileOutput {+ action: TargetAction::Delete(TableAction { name, drop: true }),+ sink: self.sink.clone(),+ tracking_record: None,+ child_invalidation: Some(TargetChildInvalidation::Destructive),+ })),+ }+ }+}+```++</Fragment>+</LangTabs>+### Deleting a containerWhen a container target state is no longer declared, CocoIndex runs the container handler's non-existence action and does **not** reconcile the children individually. The container action is therefore responsible for its children, in one of two shapes:-- **Destroy** β for a container CocoIndex owns, remove it together with its contents (`DROP TABLE`, `shutil.rmtree`, `drop_table`, β¦). The `_apply_dir_actions` sink in Step 2 below does this.+- **Destroy** β for a container CocoIndex owns, remove it together with its contents (<Lang only="python">`DROP TABLE`, `shutil.rmtree`, `drop_table`, β¦). The `_apply_dir_actions` sink in Step 2 below does this.</Lang><Lang only="rust">`DROP TABLE`, removing a directory tree, dropping an index, β¦). The `TableHandler` above returns a `Delete` action with `drop: true` for this.</Lang>- **Abandon** β for a user-managed container (a message topic, a table the user created), leave the contents untouched. Once the container is no longer declared, CocoIndex has released its claim on it and must not write to it again: another writer may now own those keys, writing to a topic the user has since deleted would recreate it on brokers that auto-create topics, and the deletion policy captured from the old declaration (such as a `deletion_value_fn` closure) no longer exists.-The zvec connector (`python/cocoindex/connectors/zvec/_target.py`, `_apply_collection_actions`) implements both: it destroys system-managed collections and leaves user-managed ones alone. `App.drop()` applies the same shapes to every root container (it drops tables and abandons topics without visiting rows or messages) so document on your connector page which shape it uses. The user-facing contract is in [How CocoIndex syncs target states](../programming_guide/target_state#how-cocoindex-syncs-target-states).+<Lang only="python">++The zvec connector (`python/cocoindex/connectors/zvec/_target.py`, `_apply_collection_actions`) implements both: it destroys system-managed collections and leaves user-managed ones alone. `App.drop()` applies the same shapes to every root container (it drops tables and abandons topics without visiting rows or messages) so document on your connector page which shape it uses.++</Lang>++<Lang only="rust">++The Valkey connector (`rust/sdk/cocoindex/src/connectors/valkey.rs`) implements both: it drops a system-managed index and leaves a user-managed one alone (`ManagedBy::User`, see [statediff](#handle-multiple-previous-states)). Document on your connector page which shape it uses.++</Lang>++The user-facing contract is in [How CocoIndex syncs target states](../programming_guide/target_state#how-cocoindex-syncs-target-states).++<RustNote>+A connector outside the `cocoindex` crate registers its root provider on each run, so a container is deleted only by a run that still registers the provider and no longer declares the container. `App::drop_state` cannot reach such a handler and leaves its target states in place. Built-in connectors such as Valkey register their root provider with every environment when it is built, so `App::drop_state` and a run that no longer declares their targets reach the handler; that registration is internal to the crate.+</RustNote>### Step 1: Define parent and child handlersThe parent handler reconciles the container itself. The child handler reconciles entries within it:+<LangTabs>+<Fragment slot="python">+```python# Parent handler for directoryclass _DirHandler(coco.TargetHandler[_DirSpec, _DirTrackingRecord, _EntryHandler]):@@ -611,9 +892,21 @@ class _EntryHandler(coco.TargetHandler[_EntrySpec, _EntryTrackingRecord]):...```+</Fragment>+<Fragment slot="rust">++The parent is the `TableHandler` [above](#child-invalidation), a `TargetHandler<TableSpec>`. The child is the `RowHandler` from [Step 2 of root target states](#step-2-implement-the-handler), a `TargetHandler<String>`: a child handler is a regular `TargetHandler` for the child value type. The child value type is not part of the parent's trait implementation; the parent's sink names it when it wraps the child handler in a `ChildTargetDef`.++</Fragment>+</LangTabs>+### Step 2: Build child handlers with a `child_handler` factory-The parent's sink is built with `from_fn()` (or `from_async_fn()`), plus a `child_handler` factory. The callback creates and drops containers; the factory returns the handler for a container's child target states:+The parent's sink is built with <Lang only="python">`from_fn()` (or `from_async_fn()`), plus a `child_handler` factory</Lang><Lang only="rust">`TargetActionSink::from_async_fn_with_child_handler(apply, child_handler)`</Lang>. The callback creates and drops containers; the factory returns the handler for a container's child target states<Lang only="rust">: it takes `&A` and returns a `ChildTargetDef`, which wraps a typed child handler</Lang>:++{/* Rust: rust/sdk/cocoindex/tests/target_state.rs (table_sink, log_table_action) */}+<LangTabs>+<Fragment slot="python">```pythondef _apply_dir_actions(@@ -638,27 +931,54 @@ _dir_sink = coco.TargetActionSink.from_fn()```-In Rust, a container sink is `TargetActionSink::from_async_fn_with_child_handler(apply, child_handler)`. The factory takes `&A` and returns a `ChildTargetDef`, which wraps a typed child handler:+</Fragment>+<Fragment slot="rust">-{/* Rust: rust/sdk/cocoindex/src/resources/live_map.rs (ContainerHandler::sink) */}```rust-TargetActionSink::from_async_fn_with_child_handler(- |_actions| async { Ok(()) },- move |_: &ContainerAction| {- Ok(ChildTargetDef::new::<V, _>(EntryHandler {- inner: inner.clone(),- }))- },-)+/// Build a container sink whose child handler factory gives each+/// created/updated table a fresh child row handler (recording into `row_log`).+fn table_sink(table_log: Log, row_log: Log) -> TargetActionSink<TableAction> {+ TargetActionSink::from_async_fn_with_child_handler(+ move |actions: Vec<TargetAction<TableAction>>| {+ let table_log = table_log.clone();+ async move {+ for action in actions {+ log_table_action(&table_log, action);+ }+ Ok(())+ }+ },+ move |_: &TableAction| {+ Ok(ChildTargetDef::new::<String, _>(RowHandler {+ sink: recording_sink(row_log.clone()),+ }))+ },+ )+}++fn log_table_action(table_log: &Log, action: TargetAction<TableAction>) {+ let entry = match action {+ TargetAction::Create(t) | TargetAction::Update(t) => format!("ensure {}", t.name),+ TargetAction::Delete(t) => format!("drop {}", t.name),+ };+ table_log.lock().unwrap().push(entry);+}```-CocoIndex calls the factory once per action whose target state was declared with a child (a created or updated container, never a delete) before the callback applies the batch, and in a preview in place of the callback. So the factory is sync, free of side effects, and builds the handler from the action alone (plus connections looked up through `context_provider`). Actions without a child provider (orphan deletes, or leaf actions when one sink is shared between a container and its children) don't reach the factory.+</Fragment>+</LangTabs>++CocoIndex calls the factory once per action whose target state was declared with a child (a created or updated container, never a delete) before the callback applies the batch, and in a preview in place of the callback. So the factory is sync, free of side effects, and builds the handler from the action alone<Lang only="python"> (plus connections looked up through `context_provider`)</Lang><Lang only="rust"> (plus what the closure captured when the sink was built)</Lang>. Actions without a child provider (orphan deletes, or leaf actions when one sink is shared between a container and its children) don't reach the factory.The container handler's `reconcile()` returns an action on every run while the container is declared, even when nothing about it changed: the factory builds the child handler from that action. Preview lists this action too: equality of arbitrary tracking records does not guarantee that applying the action has no external effect.### Step 3: Create user-facing API-The user-facing API uses `declare_target_state_with_child()` and exposes methods for declaring children:+The user-facing API uses <Lang only="python">`declare_target_state_with_child()`</Lang><Lang only="rust">`declare_target_state_with_child()` or `mount_target()`</Lang> and exposes methods for declaring children:++{/* Rust: rust/sdk/cocoindex/tests/target_state.rs (run_table_with), rust/sdk/cocoindex/src/connectors/valkey.rs (declare_index_target_with_options, mount_index_target_with_options) */}+<LangTabs>+<Fragment slot="python">```pythonclass DirTarget:@@ -689,6 +1009,42 @@ def declare_dir_target(path: pathlib.Path) -> DirTarget:return DirTarget(child_provider)```+</Fragment>+<Fragment slot="rust">++```rust+let table_provider = register_root_target_states_provider(+ &ctx,+ "test/table",+ TableHandler {+ sink: make_sink(table_log, row_log),+ invalidation,+ },+)?;+// Commit the table in a foreground child component; the child provider is+// ready at once.+let child: TargetStateProvider<String> = mount_target(+ &ctx,+ table_provider.target_state(+ "docs",+ TableSpec {+ generation: generation.to_string(),+ },+ ),+)+.await?;+for (k, v) in rows {+ declare_target_state(&ctx, child.target_state(k, v.to_string()))?;+}+```++A connector wraps the child provider in a user-facing struct, as Valkey's `IndexTarget` [above](#step-4-create-user-facing-apis) does: `declare_index_target` returns one built from `declare_target_state_with_child` (its documents resolve when the component commits), and `mount_index_target` one built from `mount_target` (ready at once).++</Fragment>+</LangTabs>++<Lang only="python">+### Migrating from child slots and `ChildTargetDef`Two older container contracts are deprecated and will be removed in a future release. Sinks written against them still run and emit a `DeprecationWarning`, but cannot be previewed:@@ -747,6 +1103,18 @@ _dir_sink = coco.TargetActionSink.from_fn(Leaf sinks need no change. If a leaf sink shares its callback with a container level (one sink per database serving table DDL and row writes), migrate it the same way: leaf actions never reach the factory.+</Lang>++<Lang only="rust">++### Migrating from child slots and child definitions++Two older container sink constructors are deprecated: `TargetActionSink::from_async_fn_with_children`, whose closure receives each action with an `Option<ChildSlot>` and calls `slot.fulfill(ChildTargetDef::new(..))` on each, and `TargetActionSink::from_async_fn_with_child_defs`, whose closure returns a `Vec<Option<ChildTargetDef>>` index-aligned with the actions. A sink built with `from_async_fn_with_children` cannot be previewed.++To migrate, move the construction of the `ChildTargetDef` into a closure `Fn(&A) -> Result<ChildTargetDef>` that builds it from the action alone, drop the slots (or the returned definitions) from the apply closure so it returns `Ok(())`, and build the sink with `from_async_fn_with_child_handler(apply, child_handler)`, as `table_sink` [above](#step-2-build-child-handlers-with-a-child_handler-factory) does.++</Lang>+## Implementing attachment providersAttachment providers let a child handler expose **auxiliary target states** alongside its regular children. For example, a PostgreSQL table handler manages rows as regular children, but can also manage vector indexes and SQL command attachments as separate attachment types β each tracked independently.@@ -775,14 +1143,16 @@ The symbol keys (`@vector_index`, `@sql_command_attachment`) are path namespaces### How it works-1. **Handler implements `attachments()`**: The child handler (e.g., row handler) returns a dict of all supported attachment types and their handlers.+1. **Handler implements `attachments()`**: The child handler (e.g., row handler) returns <Lang only="python">a dict of</Lang><Lang only="rust">a `Vec` of `(name, ChildTargetDef)` pairs for</Lang> all supported attachment types and their handlers.2. **Engine eagerly registers attachment providers**: When the child provider is resolved with the handler from the `child_handler` factory, the engine calls `attachments()` and registers all returned types. This ensures orphaned attachments can be cleaned up even when not declared in the current run.3. **User code calls `provider.attachment()`**: On a resolved child provider, this retrieves the cached attachment sub-provider.4. **Target states declared under the attachment provider** are tracked independently from regular children.### Step 1: Implement the attachment handler-An attachment handler is just a regular `TargetHandler` β it implements `reconcile()` and has an action sink:+An attachment handler is just a regular `TargetHandler` β it implements `reconcile()` and has an action sink<Lang only="rust">. The example below reuses the `RowHandler` from [Step 2 of root target states](#step-2-implement-the-handler) as the attachment handler, with its own recording sink.</Lang><Lang only="python">:</Lang>++<Lang only="python">```pythonclass _VectorIndexSpec(NamedTuple):@@ -815,9 +1185,15 @@ class _VectorIndexHandler:...```+</Lang>+### Step 2: Add `attachments()` to the parent handler-The parent handler (which manages regular children) returns a dict of all supported attachment types:+The parent handler (which manages regular children) returns <Lang only="python">a dict of </Lang>all supported attachment types:++{/* Rust: rust/sdk/cocoindex/tests/target_state.rs (MainHandler) */}+<LangTabs>+<Fragment slot="python">```pythonclass _RowHandler(coco.TargetHandler[_RowValue, _RowFingerprint]):@@ -838,9 +1214,55 @@ class _RowHandler(coco.TargetHandler[_RowValue, _RowFingerprint]):...```+</Fragment>+<Fragment slot="rust">++```rust+/// A main handler (CRUD like `RowHandler`) that also exposes an "index"+/// attachment handler.+struct MainHandler {+ sink: TargetActionSink<RowAction>,+ att_log: Log,+}++impl TargetHandler<String> for MainHandler {+ type TrackingRecord = String;+ type Action = RowAction;++ fn reconcile(+ &self,+ key: StableKey,+ desired: Option<String>,+ prev: Vec<String>,+ prev_may_be_missing: bool,+ ) -> Result<Option<TargetReconcileOutput<RowAction, String>>> {+ RowHandler {+ sink: self.sink.clone(),+ }+ .reconcile(key, desired, prev, prev_may_be_missing)+ }++ fn attachments(&self) -> Result<Vec<(String, ChildTargetDef)>> {+ Ok(vec![(+ "index".to_string(),+ ChildTargetDef::new::<String, _>(RowHandler {+ sink: recording_sink(self.att_log.clone()),+ }),+ )])+ }+}+```++</Fragment>+</LangTabs>+### Step 3: Expose attachment APIs on the user-facing target-The user-facing target class calls `provider.attachment()` to get the attachment sub-provider, then declares target states on it:+The user-facing target <Lang only="python">class</Lang><Lang only="rust">struct</Lang> calls `provider.attachment()` to get the attachment sub-provider, then declares target states on it:++{/* Rust: rust/sdk/cocoindex/tests/target_state.rs (attachment_lifecycle_create_and_cleanup) */}+<LangTabs>+<Fragment slot="python">```pythonclass TableTarget:@@ -858,8 +1280,32 @@ class TableTarget:coco.declare_target_state(att_provider.target_state(name, spec))```+</Fragment>+<Fragment slot="rust">++```rust+let provider = register_root_target_states_provider(+ &ctx,+ "test/main",+ MainHandler {+ sink: recording_sink(main_log),+ att_log,+ },+)?;+declare_target_state(&ctx, provider.target_state("row", "v1".to_string()))?;+if declare_attachment {+ let att: TargetStateProvider<String> = provider.attachment(&ctx, "index")?;+ declare_target_state(&ctx, att.target_state("idx", "spec1".to_string()))?;+}+```++A run that stops declaring the attachment deletes it, even though it was not declared in that run: the engine registered the attachment provider eagerly.++</Fragment>+</LangTabs>+:::tip[Tracking records for teardown]-When an attachment has a teardown step (like `DROP INDEX`), store the full spec as the tracking record instead of a fingerprint. This lets you recover the teardown information from `prev_possible_records` when the attachment is deleted or changed. See the `_SqlCommandHandler` in the PostgreSQL connector for an example.+When an attachment has a teardown step (like `DROP INDEX`), store the full spec as the tracking record instead of a fingerprint. This lets you recover the teardown information from `prev_possible_records` when the attachment is deleted or changed.<Lang only="python"> See the `_SqlCommandHandler` in the PostgreSQL connector for an example.</Lang>:::## Connector implementation guidelines@@ -872,6 +1318,8 @@ Target state keys must be stable across runs for correct reconciliation. CocoInd`ContextKey` solves this by providing a user-defined stable logical name (e.g., `"my_pg"`) that is decoupled from transient connection details:+<Lang only="python">+```python# User creates a stable logical name for the resourcedb = coco.ContextKey[asyncpg.Pool]("my_pg")@@ -894,9 +1342,42 @@ This way, changing a password, switching replicas, or rotating credentials won'tSee `_TableKey` in [`cocoindex/connectors/postgres/_target.py`](https://github.com/cocoindex-io/cocoindex/blob/main/python/cocoindex/connectors/postgres/_target.py) and [`cocoindex/connectors/surrealdb/_target.py`](https://github.com/cocoindex-io/cocoindex/blob/main/python/cocoindex/connectors/surrealdb/_target.py) for reference implementations.+</Lang>++<Lang only="rust">++Put the key's name (`ContextKey::name()`, the string `"my_pg"`) in the target state key. The Valkey connector's index key holds the database's `ContextKey` name plus the index name:++{/* Rust: rust/sdk/cocoindex/src/connectors/valkey.rs (IndexKey) */}+```rust+/// An index target state's key: the database's `ContextKey` name plus the+/// index name.+struct IndexKey {+ db_key: String,+ index_name: String,+}++impl IndexKey {+ fn stable_key(self) -> StableKey {+ StableKey::Array(Arc::from([+ StableKey::Str(Arc::from(self.db_key)),+ StableKey::Str(Arc::from(self.index_name)),+ ]))+ }+}+```++This way, changing a password, switching replicas, or rotating credentials won't invalidate tracked states or break reconciliation.++A sink built with `TargetActionSink::from_async_fn` receives only the actions, not the environment's context values. So resolve the connection with `ctx.get_key(&key)` in the run that registers the provider, and move it into the handler and its sink. The run that registers the provider also produces the delete actions for target states it no longer declares, so the handler has its connection for them too. Pass the typed connection down to child handlers directly, not the key name.++</Lang>+### Idempotent actions-Actions should be idempotent β applying the same action multiple times should have the same effect as applying it once:+Actions should be idempotent β applying the same action multiple times should have the same effect as applying it once<Lang only="rust">: for example `CREATE TABLE IF NOT EXISTS`, `DROP INDEX IF EXISTS` and `INSERT ... ON CONFLICT DO UPDATE` rather than a plain `CREATE TABLE` or `INSERT`, which fail when the object already exists. A sink may see an action again after a failed batch is retried.</Lang><Lang only="python">:</Lang>++<Lang only="python">```python# Good: Idempotent@@ -909,10 +1390,16 @@ path.mkdir() # Fails if existsawait conn.execute("INSERT ...") # Fails on duplicate key```+</Lang>+### Handle multiple previous statesDue to interrupted updates, `prev_possible_records` may contain multiple records. Design your reconciliation logic to handle this:+{/* Rust: rust/sdk/cocoindex/tests/target_state.rs (RowHandler) */}+<LangTabs>+<Fragment slot="python">+```python# Check if ALL previous states match (conservative approach)if not prev_may_be_missing and all(@@ -921,7 +1408,34 @@ if not prev_may_be_missing and all(return None # Safe to skip```-The Rust SDK packages this decision in `cocoindex::statediff`. `TrackingRecordTransition { desired, prev, prev_may_be_missing }` holds the inputs of one `reconcile` call, and `diff(Some(&transition))` returns `None` (skip) or a `DiffAction`: `Insert`, `Upsert`, `Replace` or `Delete`. `Replace` means a previous record differs from the desired one; `Upsert` means the records match but the target may be missing. For a target the user may own, `ManagedBy::{System, User}` and `resolve_system_transition(..)` leave a user-managed target alone.+</Fragment>+<Fragment slot="rust">++```rust+// Check if ALL previous states match (conservative approach)+let unchanged =+ !prev_may_be_missing && !prev.is_empty() && prev.iter().all(|p| *p == value);+if unchanged {+ return Ok(None); // Safe to skip+}+```++</Fragment>+</LangTabs>++<Lang only="python">++`cocoindex.connectorkits.statediff` packages this decision. `TrackingRecordTransition(desired, prev, prev_may_be_missing)` holds the inputs of one `reconcile()` call (`desired` is `coco.NON_EXISTENCE` for a removed target state), and `diff(transition)` returns `None` (skip) or a `DiffAction`: `"insert"`, `"upsert"`, `"replace"` or `"delete"`. `"replace"` means a previous record differs from the desired one; `"upsert"` means the records match but the target may be missing. For a target the user may own, wrap records in `MutualTrackingRecord(tracking_record, managed_by)` with `managed_by` `"system"` or `"user"`; `resolve_system_transition(transition)` returns `None` for a user-managed target, so it is left alone.++</Lang>++<Lang only="rust">++`cocoindex::statediff` packages this decision. `TrackingRecordTransition { desired, prev, prev_may_be_missing }` holds the inputs of one `reconcile` call, and `diff(Some(&transition))` returns `None` (skip) or a `DiffAction`: `Insert`, `Upsert`, `Replace` or `Delete`. `Replace` means a previous record differs from the desired one; `Upsert` means the records match but the target may be missing. For a target the user may own, wrap records in `MutualTrackingRecord::new(record, managed_by)` with `ManagedBy::System` or `ManagedBy::User`; `resolve_system_transition(desired, prev, prev_may_be_missing)` returns `None` for a user-managed target, so it is left alone.++</Lang>++For a container with keyed parts (a table's primary key and its columns), `CompositeTrackingRecord` and `diff_composite` return the main action plus one transition per part.### Efficient change detection@@ -932,7 +1446,9 @@ Choose tracking records that enable efficient change detection without storing f| File content | Content hash (fingerprint) || Database row | Row data hash || Schema/structure | Schema definition |-| Directory existence | `None` (presence is enough) |+| Directory existence | <Lang only="python">`None`</Lang><Lang only="rust">`()`</Lang> (presence is enough) |++<Lang only="python">### Skip reconciling unchanged values@@ -968,9 +1484,17 @@ The attribute has no effect on a container handler, whose `reconcile()` always r`reconcile()` must keep its own comparison: it is still called for values that aren't plain data (anything other than `None`, `bool`, `int`, `float`, `str`, `bytes`, and `list` / `tuple` / `dict`-with-`str`-keys of those), unchanged or not.+</Lang>++<RustNote>+The `TargetHandler` trait has no `tracks_value_fingerprint` flag: `reconcile` runs for every declared target state, so it keeps its own comparison and returns `Ok(None)` for an unchanged value.+</RustNote>+### Shared action sinks-If all instances of a handler use the same action logic, create a shared sink. The action function must accept `context_provider` as its first argument:+If all instances of a handler use the same action logic, create a shared sink.<Lang only="python"> The action function must accept `context_provider` as its first argument:</Lang><Lang only="rust"> Build it once, store it in the handler, and return `self.sink.clone()` from each `reconcile`, as `RowHandler` [above](#step-2-implement-the-handler) does: a clone keeps the sink's identity, so the engine applies the actions of every target state that returned it in one call.</Lang>++<Lang only="python">```pythonimport cocoindex as coco@@ -992,9 +1516,13 @@ class _MyHandler(coco.TargetHandler[...]):)```+</Lang>++<Lang only="python">+## Complete example: local file system-Here's a simplified version of the `localfs` connector showing the complete pattern. The Rust counterpart is `DirTarget` in `rust/sdk/cocoindex/src/resources/fs.rs`; `rust/sdk/cocoindex/src/connectors/valkey.rs` shows the same pattern against a database.+Here's a simplified version of the `localfs` connector showing the complete pattern.```pythonfrom __future__ import annotations@@ -1078,3 +1606,15 @@ See the full implementations in:- [`cocoindex/connectors/localfs/target.py`](https://github.com/cocoindex-io/cocoindex/blob/main/python/cocoindex/connectors/localfs/_target.py) β File system targets- [`cocoindex/connectors/postgres/target.py`](https://github.com/cocoindex-io/cocoindex/blob/main/python/cocoindex/connectors/postgres/_target.py) β PostgreSQL tables and rows++</Lang>++<Lang only="rust">++## Complete examples++- [`rust/sdk/cocoindex/tests/target_state.rs`](https://github.com/cocoindex-io/cocoindex/blob/main/rust/sdk/cocoindex/tests/target_state.rs) β the handler, sink and provider snippets on this page, runnable without an external service: flat targets, containers with `mount_target`, child invalidation, attachments, and the deprecated sink shapes+- [`rust/sdk/cocoindex/src/resources/fs.rs`](https://github.com/cocoindex-io/cocoindex/blob/main/rust/sdk/cocoindex/src/resources/fs.rs) β `DirTarget`, file system targets+- [`rust/sdk/cocoindex/src/connectors/valkey.rs`](https://github.com/cocoindex-io/cocoindex/blob/main/rust/sdk/cocoindex/src/connectors/valkey.rs) β a Valkey index (container) and its documents, with `statediff` and `ManagedBy`++</Lang>