Skip to content

feat: keep the DS's watcher state on its own Drive and restore it at startup - #9613

Merged
koenvanderveen merged 28 commits into
pjwerneck/ds-spoolfrom
pjwerneck/ds-watcher-storage
Nov 24, 2026
Merged

koenvanderveen merged 28 commits into
pjwerneck/ds-spoolfrom
pjwerneck/ds-watcher-storage

Conversation

@pjwerneck

@pjwerneck pjwerneck commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

PR 2 of "connsplit v1.3: Scientist durability", stacked on #9611.

  • When a DS receives events messages from a DO, we previously saved each message in a local <syftbox>-event-messages folder. Now the DS saves its state for that DO on its own Drive, in /SyftBox/<email>-<version>-watcher-state/<do email>/, before it marks the messages processed. Nobody else has access to that folder. Each save replaces rolling_state.tar.gz, which holds the changes since the checkpoint and the ids of the received messages. At 50 changed paths, the DS folds those changes into checkpoint.tar.gz. With encryption on, both files are encrypted with the DS's own key.
  • When a DS starts, we previously rebuilt its file hashes and outbox cursor from the local -event-messages folder, so a lost local disk, such as a Colab runtime reset, lost them. Now the DS restores the files, hashes and received message ids of each DO from its Drive. When the DS's key cannot decrypt the saved state, startup raises and says to log in with the key_file of the key that saved it.
  • When an enclave starts, we now keep no watcher state by default. persist_watcher_state follows persist_owner_state, which is off, because an enclave gets a new key at each boot.
  • When a DS receives a change or a delete of a private dataset file it already has, it refuses it. We previously did this only in an enclave, with a pre_write_filter callback that SyftEnclaveClient set on the cache, and it checked whether the file was on disk. Now it is the freeze_private_datasets config flag, on by default, because only an enclave ever receives private dataset files. It checks whether the DS has the file's hash, and the refused change stays out of the saved state.
  • When a DS or a DO reads a change that is not a delete but has no content or no hash, we previously accepted it, and the DO's compaction dropped the file. Now FileChangeEvent refuses it, and it also refuses content that is not valid base64. The DS skips such a message as unreadable, and the DO's compaction raises.
  • When the DO compacts its checkpoints, the merge now goes through Checkpoint.with_events, which the DS's fold uses too. CheckpointFile.content is now bytes, stored as base64 in JSON. It used to be text, so CHECKPOINT_VERSION is now 2, and a version 1 checkpoint loads with its text as UTF-8 bytes. Incremental checkpoints keep version 1, because their format did not change.
  • What users see: a DS's Drive now has a /SyftBox/<email>-<version>-watcher-state folder. The SyftboxManagerConfig builders take persist_watcher_state and freeze_private_datasets, and DataSiteWatcherCache.pre_write_filter is gone.

Guidelines

High level

  • 🟠 Split: fairly confident that the freeze_private_datasets flag, which replaces the enclave's pre_write_filter, could be its own PR. The rest belongs together.
  • 🟢 Size: the DS keeps what it received from each DO on its own Drive, and restores it at startup.

Mid level

  • 🟢 User-facing code
  • 🟢 Error handling
  • 🟢 State versioning: CheckpointFile.content is now base64 bytes, so CHECKPOINT_VERSION is now 2, and a version 1 checkpoint loads with its text content as UTF-8 bytes.
  • 🟢 Function structure
  • 🟢 Tests

Low level

  • 🟢 Naming
  • 🟢 Code style

Security

  • The files a DS received from each DO now also live on the DS's own Drive. Nobody else has access to that folder. With encryption on, the files are encrypted with the DS's own key. With encryption off, they are only compressed, as they are on the local disk.
  • An enclave keeps no watcher state by default.
  • The enclave still refuses a DO's change or delete of a private dataset file it already has. Every other client now refuses it too, by default.
  • A change without content or hash, and content that is not valid base64, are now refused.

Testing

  • New tests/unit/test_watcher_state.py covers these cases:
    • a restart on an empty folder;
    • a restart after a checkpoint, with a deleted file;
    • a restart with a new key;
    • watcher state off;
    • the in-memory cache;
    • a binary file;
    • an email in another case;
    • a message with a change without content.
  • New tests/unit/test_watcher_immutable_collections.py covers freeze_private_datasets on and off, and a refused change that is not restored after a restart.
  • Other new tests:
    • test_drive_connections.py tests the Drive watcher store.
    • test_encryption.py tests encryption at rest.
    • test_checkpoints.py tests later versions and a change without content.
    • test_checkpoint_version.py loads a version 1 checkpoint whose text, true, is also valid base64.
    • test_file_change_events_serialization.py tests corrupt base64.
  • The restart tests that read the local cache now restore from Drive. This includes the integration test test_ds_cache_aware_sync_gdrive.
  • Root unit, migrations, syft-rds, syft-job, syft-enclave and syft-datasets tests pass locally.

Asana task

- WatcherCheckpoint and WatcherRollingState in syft/sync/checkpoints/watcher_state.py hold the files received from one DO, the changes since then and the cursor in its outbox. They refuse a state that a later client wrote.
- parse_versioned_json() checks the version of JSON text or bytes before the fields. read_versioned_json() now calls it for a file.
- DriveWatcherStoreConnection keeps one folder per DO in /SyftBox/<me>-<version>-watcher-state, which is shared with nobody. A save replaces the content of the checkpoint file or the rolling-state file in place.
- put_checkpoint() and put_rolling_state() take no file name, because each DO has one file of each kind.
…crypted with the DS key

- watcher_save_checkpoint() and watcher_save_rolling_state() encrypt the state for the DS when encryption is required, then store it.
- watcher_load_checkpoint() and watcher_load_rolling_state() return None only when nothing is saved. They raise when the state cannot be decrypted or a later client saved it.
- When the DS key cannot decrypt the state, the error tells the DS to log in with the key_file of the key that saved it.
…at startup

- At startup, a DS in on-disk mode restores every DO that has saved state. It writes the files of the checkpoint and the rolling state, and records their hashes and the received message ids.
- After a pull that receives messages, the DS saves the rolling state of that DO, then marks the messages processed. At 50 changed paths, it folds them into the checkpoint.
- The DS keeps the received message ids per DO. An event that pre_write_filter refuses is not saved, so a restore does not write it.
- WatcherRollingState has no last_msg_id, because the cursor is the newest received id.
- peers_with_saved_state() lists the DOs that have saved state.
… holds its state

- In on-disk mode, the DS keeps its record of received messages in memory, as in-memory mode does. A restart restores only from the saved state on Drive.
- The code that rebuilt the cursor and the file hashes from the local folder is gone, and so is the unused events_base_path.
- The local cleanup no longer deletes a -event-messages folder.
- The three tests that restarted from the local folder now check the state restored from Drive.
…checkpoint models

- Watcher state is not encrypted until key recovery exists, as the ticket says. With the DS key, an enclave could not boot twice: its new key could not read the state of the last boot.
- The DS saves a Checkpoint and a RollingState from syft/sync/checkpoints/, as the ticket says. watcher_state.py and parse_versioned_json are gone.
- CheckpointFile.content holds str or bytes, so a binary file survives a checkpoint. RollingState.received_msg_ids holds the ids that a DS received.
- The DS saves and loads the state of a DO under its email without case, so two spellings share one Drive folder. A restore writes the checkpoint files under the email as the DO's events spell it.
- Such a change blocked every later sync of the DS. A change with no hash made each fold raise, and a change with no content raised when it was applied.
- The DS now drops it with a warning, as it does for an event aimed at another datasite.
…e DO and the DS

- syft/sync/utils/file_content.py holds the text-or-bytes content code that ProposedFileChange, FileChangeEvent and CheckpointFile used to repeat. The JSON shape does not change.
- Checkpoint.with_events() applies events to a checkpoint. The DO compaction and the DS fold both use it, and compact_incremental_checkpoints is gone.
- The DO compaction now raises on a change without content or hash. Before, it dropped the file and could write "None" as a hash.
- decode_content() dropped characters outside the base64 alphabet and returned other bytes, with no error. It now raises, so the model refuses the content.
- DEFAULT_CHECKPOINT_EVENT_THRESHOLD moved to syft/sync/checkpoints/checkpoint.py. The DO syncer, the DS cache and SyftboxManager all read it.
- WATCHER_CHECKPOINT_THRESHOLD and three literal defaults of 50 in SyftboxManager are gone.
- DriveWatcherStoreConnection.clone_for_thread() returned the same object, so another thread would share one Drive client. It now returns a store over a new client, as the other Drive connections do.
…ndle

- WatcherStoreConnection.latest_checkpoint() is now get_checkpoint(), because each DO has one checkpoint. put_checkpoint() and put_rolling_state() return nothing, because no caller read the file id.
- DriveWatcherStoreConnection names its helpers after the watcher folder and the files: _watcher_folder_id(), _save_file(), _read_file() and _find_id().
- DataSiteWatcherCache has _save_watcher_state() and _restore_watcher_state(). The DO compaction names its sorted checkpoints incrementals_in_order.
…test

- test_ds_cache_handles_deletions_correctly reloaded over an empty mock Drive, so it passed even with no restore. It now reloads over the DS's own router.
- The test also checks that a kept file comes back, so a restore that does nothing fails it.
Comment thread syft/sync/checkpoints/checkpoint.py Resolved
Comment thread syft/sync/connections/protocols.py Resolved
Comment on lines +365 to +366
def _save_rolling_state(self, peer_email: str) -> None:
key = _peer_key(peer_email)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The watcher state of a DO is saved under _peer_key(), the lowercased email, while restored checkpoint files go under the email as the DO's events spell it.

Let's make a ticket in Asana to lowercase this everywhere, and only when we first read it into an object.

Comment thread syft/sync/checkpoints/checkpoint.py Resolved
… helpers

- _restore_watcher_state() calls _restore_checkpoint() and then _restore_rolling_state() for each DO with saved state. The checkpoint goes first, because the rolling state holds the changes after it.
…al()

- _rolling_state() is now _rolling_state_local(), so its name says that it returns the copy in memory. _save_rolling_state() writes that copy to the DS's storage.
- DriveWatcherStoreConnection keeps the ids of the watcher folder, of each DO folder and of each file once it finds or creates them. After the first save, a save is one Drive call and a load is one download. Before, each save looked up three ids first.
- reset(peer) drops the ids of that DO, reset_caches() drops all of them, and clone_for_thread() copies them.
…the DS

- FileChangeEvent refuses a change that is not a delete and has no content or no hash. No code builds one, and a message that holds one cannot be read: the DS skips it with a warning, as any unreadable outbox message.
- The DS no longer checks each event for content. _accepted_events() is _events_within_peer_datasite() again.
- Checkpoint.with_events() keeps its check as a guard of that rule.
- CheckpointFile.content is always bytes, as base64 in JSON. A checkpoint built from text encodes it as UTF-8, so the hashes do not change.
- The content_type field and its two hooks are gone from CheckpointFile. Events and proposals keep str or bytes, because peers exchange them.
- The router encrypts the watcher state with the DS key when the DS requires encryption, as it does the DO's own state. A key that cannot decrypt it raises an error that says to log in with the key_file of the key that saved it.
- persist_watcher_state, next to persist_owner_state, turns the watcher state on or off, in both cache modes. An enclave gets a new key at each boot, so for_enclave() sets both from the same setting, which is off by default.
- The in-memory cache now saves and restores the watcher state too, so test clients run it as a real client does.
- peers_with_saved_state() no longer fills the folder-id cache from its listing. That listing skipped the check that a DO has exactly one folder, so two folders made at the same time were read as one without an error.
- The test of the id cache counts Drive calls with _count_drive_calls(), which sees every list request.
…_content

- file_content.content_bytes() encodes text as UTF-8. The checkpoint and the send queue use it, instead of a copy each.
- The send queue reads ContentType from file_content, instead of its own copy.
- With freeze_private_datasets on, the watcher does not change or delete a file that it already has in an immutable collection.
- The enclave flow turns the flag on, so syft_enclaves.immutability is gone.
@koenvanderveen

koenvanderveen commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

lets get this fixed:
State versioning: CheckpointFile.content is now base64 bytes, but CHECKPOINT_VERSION is still 1. A DO checkpoint saved before this PR fails to load. When its text happens to be valid base64, such as true, it loads as the wrong bytes with no error. After a failed load, get_latest_checkpoint returns None, so the next compaction uploads a checkpoint without the old files and deletes the old checkpoint. To fix this, bump the version and read the content of version 1 as text.

Lets also add a test for that

@koenvanderveen
koenvanderveen merged commit bc669fb into pjwerneck/ds-spool Nov 24, 2026
20 checks passed
@koenvanderveen
koenvanderveen deleted the pjwerneck/ds-watcher-storage branch November 24, 2026 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants