fix: preserve branch scope during Embedded tag time travel (#4852)

Embedded tag checkout replaced the handle's only branch identity. After
reading a tag on another branch, `checkout_latest()` followed that
branch and subsequent writes or restores could commit there.

Keep the handle's original branch and Lance `BranchIdentifier`
separately from its visible snapshot. A branch name alone cannot
distinguish the original branch from a replacement after deletion and
reuse. Cross-branch tags remain readable, and Python pickle and snapshot
copies preserve both identities. `checkout_latest()` reattaches to the
original branch, so later appends stay there. `current_branch()`
continues to report the visible snapshot's branch.

Validate the retained identity before `checkout_latest()` changes the
handle or `restore(tag)` commits; a reused branch name is rejected
without changing either branch tip. Independent and query snapshot
copies retain this identity too. Reject an ambiguous cross-branch
restore before committing. To restore on the tag's branch, first open a
handle there with `branches.checkout(...)` or Rust's
`checkout_branch(...)`. Update Rust, Python sync/async, TypeScript,
generated API documentation, and the tag-based versioning guide for this
behavior.

Python pickle state also carries the original `BranchIdentifier` through
reopening. A foreign snapshot stays readable if the originating branch
is replaced while serialized, but latest checkout and restore reject the
replacement. Latest and same-branch pinned handles validate the saved
identity before reopening returns. Older pickle states without the
identifier keep their existing reopen behavior; they cannot validate an
identity that was never saved.

Validation:

- All 12 new branch-name reuse regression cases failed before the
identity fix and pass afterward. They cover latest checkout and restore,
direct handles and snapshot copies, all three consistency modes, and
unchanged versions and contents on both branch tips. Query snapshot
identity retention is also covered.
- All 8 new Python pickle regression cases failed before the
serialization fix and pass afterward. They replace the branch while the
handle is serialized, cover latest checkout and restore across three
consistency modes, verify unchanged tips, and reject replacement data
when reopening latest or same-branch pinned handles.
- 494 Rust table tests and 23 dataset consistency tests passed with
`--features remote --profile ci`; Clippy for the core and Python
bindings, Rust formatting, and `git diff --check` passed.
- 62 targeted Python tag, branch, pickle, and restore tests passed in
the uv-managed environment after rebuilding the extension. An additional
compatibility check verified that older pickle states without the
identifier still reopen and return to the original branch.
- Earlier SDK validation: 9 targeted Node tests passed, including the
cross-branch regression. Node native build, TypeScript compilation,
lint/format checks, and API documentation generation passed. TypeScript
sources are unchanged in this Python pickle follow-up.
- Python package Ruff lint/format checks passed; repository-wide Ruff
reported 59 pre-existing findings in untouched `docs/web-tests/` files.
Vale 3.24.0 found no prose issues on the changed guide lines.

Fixes #4842

<!-- lance-gatekeeper-fix:v1 agent=c1c6448a3f02ec6db445df5e3d8dd77f
generation=1 -->


<!-- gatefixer-walkthrough:v1
head=078eceadbedc15cf179d9d4dcce249c83e7010f9 -->
## Change walkthrough

```mermaid
flowchart LR
    A["Save original identity"] --> B["Reopen visible snapshot"]
    B --> C{"Foreign snapshot?"}
    C -->|No| D["Validate before return"]
    C -->|Yes| E["Retain saved identity"]
    E --> F["Validate latest or restore"]
```

**Read:** [Serialize and
reopen](https://github.com/lancedb/lancedb/blob/078eceadbedc15cf179d9d4dcce249c83e7010f9/python/python/lancedb/table.py#L4541-L4594)
→ [Reinstate identity
safely](https://github.com/lancedb/lancedb/blob/078eceadbedc15cf179d9d4dcce249c83e7010f9/rust/lancedb/src/table/dataset.rs#L278-L307)
→ [Serialized-handle
regressions](https://github.com/lancedb/lancedb/blob/078eceadbedc15cf179d9d4dcce249c83e7010f9/python/python/tests/test_table.py#L1300-L1360).

<sub>Snapshot:
[078ecea](https://github.com/lancedb/lancedb/commit/078eceadbedc15cf179d9d4dcce249c83e7010f9)</sub>
<!-- gatefixer-walkthrough:end -->

---------

Co-authored-by: Gatefixer <313497061+lancedb-gatefixer[bot]@users.noreply.github.com>
This commit is contained in:
lancedb-gatefixer[bot]
2026-11-30 20:05:58 +08:00
committed by GitHub
parent c0d46c31ba
commit 2d4e0fb3f2
10 changed files with 820 additions and 54 deletions
+11 -3
View File
@@ -191,6 +191,11 @@ the `restore` function.
Calling this method will set the table into time-travel mode. If you
wish to return to standard mode, call `checkoutLatest`.
In LanceDB Embedded, a tag can select a read-only snapshot on another branch.
`checkoutLatest()` returns to this handle's original branch. Restoring a
snapshot from another branch is rejected before writing. Use
[Branches.checkout](Branches.md#checkout) to open that branch before restoring its tag.
#### Parameters
* **version**: `string` \| `number`
@@ -520,10 +525,10 @@ account for the replacement caveat described in [Table.createIndex](Table.md#cre
abstract currentBranch(): null | string
```
The branch this table handle is scoped to, or `null` for the main branch.
The branch of the visible snapshot, or `null` for the main branch.
A handle returned by [Branches.create](Branches.md#create) or [Branches.checkout](Branches.md#checkout)
reports the branch it targets; a handle opened normally reports `null`.
During Embedded tag time travel, this can differ from the handle's original
branch, which `checkoutLatest()` returns to.
#### Returns
@@ -1668,6 +1673,9 @@ abstract restore(): Promise<void>
Restore the table to the currently checked out version
LanceDB Embedded rejects a snapshot on another branch before writing.
Open that branch with [Branches.checkout](Branches.md#checkout) before restoring its tag.
This operation will fail if checkout has not been called previously
This operation will overwrite the latest version of the table with a
+14 -4
View File
@@ -1741,10 +1741,20 @@ page).
The tags API supports the standard CRUD operations (create, list, update, delete),
plus checking out by tag name.
A tag identifies both a branch and a version. On Enterprise tables, a tag must belong
to the handle's current branch; checking out a tag on another branch raises an
error without changing the current selection. Obtain a handle for the tag's
branch first: in Python, use `branch_table = table.branches.checkout("exp")`, then
A tag identifies both a branch and a version.
On Embedded tables, checking out a tag on another branch selects a read-only
snapshot on that branch. `checkout_latest()` (Python and Rust) or
`checkoutLatest()` (TypeScript) returns to the handle's original branch, so later
writes stay on that branch. Restoring a snapshot from another branch raises an
error before writing. To restore the tag's snapshot, first open a handle on its
branch with `branches.checkout()` (Python and TypeScript) or `checkout_branch()`
(Rust).
On Enterprise tables, a tag must belong to the handle's current branch;
checking out a tag on another branch raises an error without changing the
current selection. Obtain a handle for the tag's branch first: in Python, use
`branch_table = table.branches.checkout("exp")`, then
`branch_table.checkout("baseline")` to read a tag named `baseline` on `exp`.
See [Branches](/tables/branching) for more on branch handles.