A document source rejects a week-old sync token from an ingestion connector as expired: how do you recover without emptying or doubling the retrieval index?
answer
- keep serving what you have
- a fresh position before the crawl
- stable IDs make re-writes overwrite
- save the cursor after the write
basics
~10 sKeep serving the existing index, take a fresh change position before re-crawling, write every document under its stable source ID, replay changes since that position, then hand documents neither pass saw to delete reconciliation.
solid answer
~50 sA sync token is a bookmark into the source's change history, and sources keep that history only for a retention window, so a connector offline past it gets a 'resync required' error, sometimes an HTTP 410. The two bad reflexes are wiping the source's chunks and re-crawling, which leaves the assistant blind for hours, and re-crawling under new IDs, which doubles every chunk. Instead I keep serving the stale index and flag the source as behind, take a **fresh change position first**, then crawl in full and write each document under its **stable source-derived ID** so re-writes overwrite. Then I replay changes from the fresh position to catch edits made during the crawl, saving each new token only after the changes it covers are written. Only then do documents that neither pass saw go to delete reconciliation, and never when the listing came back suspiciously short.
code
pseudocode · 18 lineson sync_token_rejected(source):
set_state(source, RECOVERING)
fresh = source.current_change_position()
run = new_crawl_run_id()
for doc in source.list_all_documents():
write_document(stable_id(doc), fetch(doc), seen_in = run)
if listed_count(run) < 0.8 * indexed_count(source):
halt_and_alert(source)
stop
token = fresh
loop:
batch = source.changes_since(token)
apply(batch.changes, seen_in = run)
token = batch.next_token
save_token(source, token)
until batch.is_last
send_unseen_to_delete_reconciliation(source, run)
set_state(source, HEALTHY)go deeper
Recall that a sync token points into a change history the source keeps only for a while, and that the fallback is a full crawl.
Walk through the recovery order: keep serving, fresh position first, crawl under stable IDs, then replay. Explain why each step is in that order.
Show the guards: save the token only after its changes are written, stop before a mass delete when the listing is suspiciously short, and alert on downtime before the retention window runs out.
Weigh in-place recovery against building a second index and swapping it in, by storage, embedding cost and how long users see stale content, and set the alerting so recovery stays a replay.
## What a sync token is and why it expires A **sync token** (also called a change token or cursor) is an opaque value a document source's change API returns with each batch of changes. The connector stores it and sends it back next time to ask "what changed since here?". It points into the source's **change history**, and sources rarely keep that history forever: there is a retention window, set by the source, often days to weeks. A connector that was down longer than that, or that held a token the source invalidated for its own reasons, gets the token rejected. Sources signal this differently; an HTTP 410 Gone or an error with a "resync required" code are two shapes seen in practice. The usual way back is a full listing of the source. ## Two reflexes that hurt, and the one that does not | Approach | What users see during recovery | What the index looks like after | |---|---|---| | Delete the source's chunks, then re-crawl | No answers from that source for hours | Correct, eventually | | Re-crawl and insert with fresh random IDs | Every document answered twice | Doubled until someone cleans it | | Recover in place (below) | Week-old answers, flagged as behind | Correct, with no gap and no duplicates | Stale-but-present beats empty for almost every assistant: a week-old answer with a "last synced" notice is more useful than "I have no information". How the notice is shown belongs to freshness work. ## Recovery in place, step by step 1. **Mark the source as recovering.** Keep serving its existing chunks; stop trusting the old token. 2. **Take a fresh change position first.** Many change APIs can issue a token for "now" without listing changes. Where none exists, record the crawl's start time and later poll modified-since from slightly before it. 3. **Crawl in full, checkpointed.** List and fetch every document, recording a crawl run ID on each one written. Checkpoint per folder or space so a crash resumes instead of restarting. 4. **Write under stable IDs.** Each document is written under an ID derived from the source's own document ID, so re-writing an unchanged document overwrites it. How those IDs are built and how a document's chunk set is replaced is the re-indexing step's subject. 5. **Check the listing.** If it is far shorter than the index, stop here (see the guards below). 6. **Replay from the fresh position.** Process every change since step 2, marking each document written with the same run ID. This catches edits and deletions made while the crawl ran. 7. **Hand off what neither pass saw.** Documents indexed before but untouched by the crawl and the replay are delete candidates for reconciliation. 8. **Store the newest token and mark the source healthy.** ## Why the fresh position comes before the crawl A full crawl of a big source takes hours, and people keep editing. If the position were taken after the crawl, everything edited between "this page was crawled" and "position taken" would be lost: - an edit to a page the crawl already passed is replayed from the earlier position; - a document created after the listing passed its folder is picked up by the replay; - a document deleted after it was crawled shows up as a delete event in the replay; - a document moved from an unlisted folder into a listed one mid-crawl is covered by the replay's move event. Replaying changes the crawl already captured is harmless, because writes under stable IDs are idempotent. ## Guards and prevention - **Mass-delete guard.** If the crawl lists far fewer documents than the index holds, suspect the connector first (an expired credential, a narrowed service-account scope, a failing listing call) and stop before deleting. - **Save the token after the write.** Persist each new token only once the changes it covers are written to the index. Saving it first means a crash between save and write loses those changes for good; saving after means a crash replays them, which is safe. - **Alert before expiry.** Track connector downtime against the source's retention window and page someone while recovery is still a replay, not a re-crawl. - **Budget the crawl.** A recovery crawl competes with the source's own users for its request quota; it is the same planning as a first crawl.
- The source cannot issue a change position for 'now'. What do you use instead?Record the crawl's start time, and after the crawl run a modified-since poll from a little before it, with an overlap window to absorb clock skew. That catches edits during the crawl; because a timestamp poll misses deletions and changes that do not bump the timestamp, the next reconciliation crawl remains the backstop for those.
- Why not simply build a second index from the crawl and swap it in when done?It also avoids an empty index, and some teams do it, but it doubles storage and embedding work for every document that did not change, and it still needs the replay from a position taken before the crawl. Recovering in place under stable IDs rewrites only what is needed.
- How do you stop this happening again?Alert on connector downtime well inside the source's retention window, so recovery is still a replay. Save tokens durably and only after their changes are written, and treat a rejected token as a page, not a log line, because every hour of delay is more of the source's changes to rediscover.
- Delete the source's chunks first so the re-crawl starts from a clean slate.
- A fresh sync token returns the changes made while the connector was down.
- Save the new sync token as soon as the response arrives, before applying it.
- Take the fresh change position after the crawl, once the index is current.
- Any document missing from the listing can be deleted immediately.