Skip to content

fix(platform): reconnect a lapsed cloud import, warn on a partial one - #3984

Merged
yannickmonney merged 5 commits into
mainfrom
fix/cloud-import-expired-grant-partial
Sep 30, 2026
Merged

yannickmonney merged 5 commits into
mainfrom
fix/cloud-import-expired-grant-partial

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #3905, on the owner's decision about its two "Left open" items: an import whose Microsoft or Google grant ends part-way, and an import where only some files come in. Refs #3905. No GitHub issue asks for this, so nothing is closed.

What changed

1. Access that ends mid-import lands in the connect dialog (OneDrive, SharePoint, Google Drive)

Before this change, the import door read the member's grant once, when the request started. A grant revoked or expired while the files came in (Disconnect in another tab, access removed at Microsoft or Google, a refresh token that died) failed every later file on a refused token. The import then read Import failed with a generic line, and the files already imported went unsaid.

The backend (backend/core/cloud_import/import_grant.ts, used by both importers):

  • The import reads the grant again before each file. That is the same check tokenFor makes for every listing, so it sees a Tale-side revoke, a dead grant and a token near expiry (refreshed).
  • It asks for a new token once more (resolveCloudAccessToken(…, { forceRefresh: true })) when the provider refuses the token a file's call carried (HTTP 401). Access removed at Google ends the token at once, while the stored expiry still counts it live.
  • A refresh may happen again only after a file ends in something other than a refusal, so there is never one refresh per remaining file.
  • A grant that answers "reconnect" stops the import at that file. The files before it stay. The answer carries the counts so far and the grant check's own sentence: the one every listing hands off on.
  • A read that throws or cannot be answered (a database blip, a throttled token endpoint) leaves the import on the token it has. The grant is then not read again for 30 seconds.
  • The sync engine passes no resolver and behaves as before.
  • The listing doors refresh once on a 401 too (listUnderGrant, all five listing routes). Access removed at the provider therefore reaches the connect dialog from the picker and from an import's folder walk, instead of Couldn't load items / Import failed.
  • A Disconnect is no longer undone by a refresh. A refreshed token is stored only while its grant is still active (UPDATE … AND status = 'active'). A refresh racing a Disconnect used to write the grant back to active.

The app:

  • Both pickers apply their existing isCloudImportAuthError rule to the import's answer, and to the folder walk's listing error.
  • On a match, the picker closes and hands off to onRequireConnect({ imported, total }), takes the Import started notice down, and raises no toast.
  • The connect dialog then reads Reconnect Microsoft 365 / Reconnect Google Drive. Its description, read out with the title when it opens, says that access ended during the import and how many files were imported before it (documents.cloudImport.importInterrupted). It reads No files were imported. when access ended during the folder walk.
  • Its body says to reconnect and import the same files again (documents.cloudImport.reconnectToImportRest). The button is the sync dialog's reconnect button.

Re-running the same selection: I checked how the import dedupes before changing anything, and nothing needed to change. findDocumentByExternalId finds a document by (org_id, external_item_id), the provider file id, with no lifecycle filter (unique since 0073). An unchanged file is skipped without a download, and a changed one is updated in place. New tests prove that a re-run after an interrupted import skips the files already in and creates only the rest.

2. A partial import is one warning

  • A new toast variant. @tale/ui gets a warning variant. It uses Alert's warning glyph and tones: amber-600, which keeps 3:1 on the light surface where the --warning token does not. The toast guide and its demo are updated.
  • Some files came in: one warning toast titled Imported N of M files (documents.cloudImport.importedPartial, and syncedPartial for a sync import). Its line names the first failed file with that failure's words (firstFailureDetail, through documents.cloudImport.failedFileDetail, '{name}: {reason}'). A fault gets the counts alone. The dialog stays open, so importing again retries the failed files and skips the rest.
  • Nothing came in: the destructive Import failed stays, with the same rule: the words, else 0 of M files imported.
  • An empty OneDrive selection (only empty folders) is refused in words (documents.onedrive.noFilesSelected), as Google's already was. Before, it sent an empty list, which the door refused in zod's English.
  • Which failures have words. A failed file carries a reason only for a refusal Tale wrote for people. Today that is the size cap's 413, the upload door's own sentence (The file is … bytes; the limit is 512 MiB). Everything else is a fault whose text goes to the log alone: a provider's 403/404/429/5xx, an empty file's Invalid blob size, a database error. The import answer's own error still never reaches a toast (fix(platform): close the #3806 cloud import and bulk send findings #3905's rule).
  • Imported counts skipped files. "Imported" now means successCount + skippedCount, the files of the selection that are in Documents. The success toast uses the same count. Before, re-running a half-imported selection read Import completed · 7 of 10 files imported with nothing missing.

3. The single-failure-toast guard sees handoffs

As written, the guard could not see these paths. The import writes are quiet (errorToast: false), and a handoff to the connect dialog was not a report to it.

It now treats onRequireConnect, or a function that always calls it, as a report, and checks two things:

  • A toasting write whose catch or .catch hands off is reported twice.
  • A new check, handoffsBesideToasts, fails when a toast shares the branch that hands off. That covers a toast in that block; a toast unconditionally before it, unless the branch takes that notice down (started.dismiss()); a toast in a finally around it; and a toast after it while the block runs on.

Samples cover each shape. Four mutations of the real dialogs are each caught by the new check alone:

  • a toast added in the interrupted branch;
  • the return after the walk handoff removed;
  • the return after the answer handoff removed;
  • started.dismiss() removed, which leaves Import started on screen beside the dialog.

Docs and the manual layer

  • docs/{en,de,fr}/platform/knowledge/documents.md say what the import's notices mean, and what happens when access ends mid-import.
  • The knowledge suite has two new boxes. KNOW-B19 covers access that ends mid-import, provoked by Disconnect in a second tab, plus the re-run. KNOW-B20 covers the partial import: a file over 512 MiB, and an empty file for the fault path. KNOW-B16 also covers access removed at Google while the picker is open. The suite's Cost is now 60.
  • reference/automation.md: the import-path row from fix(platform): close the #3806 cloud import and bulk send findings #3905 is updated, a row owns the unit half of both new boxes, the KNOW-B16 row names the listings' refresh on a 401, and the "one failure, one toast" row names the handoff rule.
  • The tests/manual/readme.md suite table is recounted from the suites: knowledge 60, plus main's new responsive and tasks boxes. The total is now 1023.

How I verified it

Local runs were on a sandbox shared with other agents. Workspace-wide gates (the whole platform suite, tsc, Playwright) are left to CI.

Tests at the pushed commit (all green)

Scope Files Tests
Backend: backend/core/{cloud_import,onedrive,google_drive}, backend/domains/{onedrive,google_drive,cloud_import}, plus the single-failure-toast and error-message-description guards and lib/i18n/messages.test.ts 31 333
App: app/features/documents and app/lib/backend (client project) 43 445
packages/ui toaster.test.tsx 1 7
Docs site (services/docs): locale tree, outline, components, links 36 200
services/ui-docs 11 82

New and changed tests include:

  • cloud-import-dialogs.import-outcome.test.tsx: the Documents menu → picker → walk → import through useBackendAction and its adapter row → the connect dialog or the real toast, for OneDrive, SharePoint and Google Drive. It has 38 cases:
    • access ending mid-import in en/de/fr;
    • access ending during the walk;
    • the grant already gone when the import starts;
    • a sync import cut short;
    • a partial import in en/de/fr, with words and as a fault;
    • the re-run counting skipped files;
    • a full failure, with words and as a fault.
  • Backend: import_grant.test.ts (11), import_files.test.ts for OneDrive/SharePoint and for Google Drive, routes.grant.test.ts for both domains (the import door plus every listing door on a 401), service.import-deps.test.ts (download flags), cloud_import/service.test.ts (the forced refresh; a refreshed token dropped after a revoke), and the lister and metadata tests.
  • App units: lib/cloud-import-outcome.test.ts (5).

CI on b589a663e: every check passed (60 in all; 8 were skipped by design), about 20 minutes after the push. These tasks executed rather than replaying a cached verdict (cache miss, executing):

Task Result
@tale/platform:test 959 files, 80,278 tests
@tale/platform:test:ui 553 files, 4,892 tests, including the 38 outcome cases
@tale/platform:typecheck executed
@tale/platform:lint executed
@tale/ui:test executed, including the toaster cases

Negative controls, run on the final tree with the sources swapped back to origin/main and the new tests kept

  • The four app components at origin/main: all 38 outcome cases fail.
  • The importers, listers, routes and grant service at origin/main: 47 of the 115 cases in the ten changed backend test files fail. The 68 that still pass are the tests that were already there.

Guard mutations: the four mutations above each fail only never toasts on a branch that hands the failure to the connect dialog.

Static checks:

  • oxlint --type-aware --type-check on every changed file (39 platform files, plus packages/ui and services/ui-docs): clean.
  • oxfmt --check: clean.
  • bun run knip:check: clean.
  • bun run lint:manual: ok.
  • check-guide.ts on knowledge.md: OK. On reference/automation.md it reports the same 50 lines as on the base.
  • The i18n report-mode checks show no finding on any new key.

Review: two passes by a read-only adversarial reviewer. The first found two medium defects, both fixed here:

  • a thrown grant read aborted a running import;
  • a provider-side revoke met during the folder walk still toasted.

It also found low ones, all fixed: a refresh storm on a throttled token endpoint, a refresh that could undo a Disconnect, guard gaps, the OneDrive empty selection, and the docs wording. The second pass confirmed every fix and found no regression.

Left open

  • A file whose earlier import sits in the trash comes back skipped (the import deliberately never resurrects a trashed document), and it counts as imported. Whether a re-import should restore it, or say so, is a product call. The copy now says those files "are kept" rather than "stay in Documents".
  • The 30-second quiet window after a grant read that could not be answered is a trade-off. During it, a Tale-side Disconnect is noticed up to 30 s late, and a file refused (401) is not refreshed. A Google-side revoke plus a token-endpoint blip in the same window reads as a partial or failed toast; the re-run lands in the connect dialog.
  • Only the size cap carries words. The other per-file failures read as counts alone: a file the provider no longer has (404), refuses (403) or throttles (429), and an empty file (Invalid blob size). Naming them needs new backend sentences.
  • firstFailureDetail reads the first failure only. This is unchanged shared behaviour, as fix(platform): close the #3806 cloud import and bulk send findings #3905 noted.
  • Failures before a lapse go unsaid for now. A file that failed for another reason before access ended is not in the connect dialog, which is that import's one report; the re-run names it.
  • The reconnect race is closed only for Disconnect. A refresh started before a reconnect can still overwrite the new grant's tokens (this predates the change). A compare-and-swap on updated_at_ms would close it.
  • The guard's handoff rule trades reach for precision. It follows only functions that always hand off, so a toasting write whose catch calls a helper that hands off conditionally is not flagged. That is the price of not flagging handleImport itself.
  • The sync engine still reads a config owner's grant once per run. An interrupted sync import's dialog says to import again, which is correct though not required: its configs exist and resume after reconnecting.
  • suites/governance.md still says Cost 72 for its 73 boxes on main. I did not touch it.

A toast could only say success or failure, so an operation that finished
in part had to read as one or the other. The warning variant carries
Alert's warning glyph and tones (amber-600, which keeps 3:1 on the light
surface where the --warning token does not). The toast guide, its demo
and the stories show it.
The import doors read the member's grant once, when the request started.
A grant revoked or expired while the files came in failed every later
file on a refused token, and the import answered a plain failure.

Both import pipelines now read the grant again before each file, the
check every listing makes, and ask for a new token once more when the
provider refuses the one a file's call carried (401). A grant that
answers "reconnect" stops the import at that file: the files before it
stay, and the answer carries the counts and the grant check's sentence.
A read that throws or cannot be answered leaves the import on the token
it has and is not retried for 30 seconds. The sync engine passes no
resolver and is unchanged.

The listing doors refresh once on a 401 as well, so access removed at
the provider reaches the connect dialog from the picker and from an
import's folder walk. A refreshed token is stored only while its grant
is still active, so a Disconnect during a refresh is no longer undone.
A file past the size cap carries the upload door's own sentence as its
reason; every other failed file keeps its text for the log.
The single-failure-toast guard could not see the cloud import's
handoffs. The import writes are quiet, and handing a failure to the
connect dialog was no report to it. A function that always calls
onRequireConnect now counts as one. A toasting write whose catch hands
off is reported twice, and a new check fails when a toast shares the
branch that hands off: in that block, unconditionally before it unless
the branch takes that notice down, in a finally around it, or after it
while the branch runs on.
An import that access ended part-way, and one where only some files
came in, both read "Import failed" with a generic line.

The OneDrive, SharePoint and Google Drive pickers read the import's
answer with the rule their listings hand off on. Access that ended
closes the picker and opens the connect dialog, which says so, how many
files came in, and how to bring in the rest; the "Import started"
notice is taken down and no toast is raised. An import where some files
failed is one warning, "Imported N of M files", naming the first failed
file with its words (firstFailureDetail) or giving the counts alone for
a fault. Nothing imported keeps "Import failed" with the same rule.
Files an earlier import brought in unchanged count as imported, the
success toast included. An empty OneDrive selection is refused in
words, as Google's is, instead of the door's English.

The re-run needs no change: the import finds a document by its provider
file id and skips an unchanged one, which the tests prove. The knowledge
suite adds KNOW-B19 and KNOW-B20, the automation register owns their
unit half, and the manual readme's suite table is recounted.
The Documents guide (EN/DE/FR) says what an import's notices count and
name, and what happens when access to Microsoft 365 or Google Drive ends
during an import.
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Independent review of b589a663e6b1927671c6b953a61b035517dc48ba: one P2 regression to fix before merge — a stale import can discard a newer picker selection.

Both new handOffInterruptedImport helpers call parent callbacks after the originating picker has unmounted (google-drive-import-dialog.tsx:196–198, onedrive-import-dialog.tsx:158–160). The parent callbacks close whichever picker is open now and open Reconnect (documents-action-menu.tsx:152–167).

Reproduction for OneDrive and Google Drive:

  1. Start an import and hold its response.
  2. Dismiss the started toast and picker with Escape, reopen the same provider, and select a folder.
  3. Deliver the old import’s lapsed-grant answer.
  4. The new picker closes, its checked selection is lost, and the old result opens Reconnect.

Please scope the handoff to the interaction that started it, preserve a newer picker/selection, and retain a once-only, non-disruptive report for the completed background request. Dismissing the UI does not mean the server import was cancelled.

Local controls used the real Documents menu, pickers, Dialog, action hook and toast, with action answers and authorization/configuration status stubbed. Both cases fail at this head in jsdom and real Chromium; replacing only the four UI components with their base b9f5a028 versions makes both pass in each environment. Source was restored byte-for-byte. Retained dependencies were used, not a fresh frozen install; Chromium’s harness warns that Vitest 4.1.11 and browser 4.1.10 differ, so the matching jsdom/source and before-controls are also retained. No production or live provider calls were made.

Other focused checks passed: 65 UI tests, 154 backend/guard tests, 27 provider-boundary tests, and two independent partial-retry controls. No new tenant/auth exposure or additional blocker was established in this bounded review.

@yannickmonney
yannickmonney merged commit 17cecce into main Sep 30, 2026
60 checks passed
@yannickmonney
yannickmonney deleted the fix/cloud-import-expired-grant-partial branch September 30, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant