TF-5239 Add ADR-0121 Drive LOOKUP and saved state

Addresses: 4779101134, 4779101130
https://github.com/linagora/tmail-flutter/pull/5212#discussion_r4779101134
https://github.com/linagora/tmail-flutter/pull/5212#discussion_r4779101130
This commit is contained in:
Dang Dat committed 2026-12-03 10:32:32 +07:00
1 parent 42c2659594
commit edcab8ac3d
2 files changed
+106

No files matched your search

+99
View File
@@ -0,0 +1,99 @@
# 121. Drive LOOKUP and the saved state of attachments
Date: 2026-12-02
## Status
Proposed
## Reference
- [#4791](https://github.com/linagora/tmail-flutter/issues/4791) Save to my Drive
- [#5239](https://github.com/linagora/tmail-flutter/issues/5239) ADR - Drive LOOKUP saved state
- [ADR-0118](0118-save-attachment-to-drive.md) holds the per-file save
- [ADR-0120](0120-save-all-to-drive.md) holds the save queue
## Context
- Mail shows whether an attachment is already in the user's Drive.
- Drive answers through the LOOKUP intent ([Drive #4664](https://github.com/linagora/twake-drive/pull/4664)).
- The chip, the save queue and See in Drive all read and write that state.
- This ADR defines the Drive contract, when LOOKUP runs, the store that owns the state,
and the rules for failure, cancel and logging.
## Decision
### Drive contract
- Each reference has type `io.cozy.mails.attachments` and ID `<accountId>:<emailId>:<blobId>`.
- LOOKUP takes `references` and returns `null` or `{ fileId }` per reference, with no size.
- A found file enables "See in Drive" at `<Drive root>/#/file/<fileId>`.
A `null` entry enables "Save to my Drive".
- A moved file is still found. A trashed file returns `null`.
- A call takes 1 to 100 references. 101 fails the call. Mail splits longer lists into chunks of 100.
- An invalid reference fails its entire chunk.
- A full chunk took about 12 s on staging.
### When it runs
- On email open, when the gates of ADR-0118 hold and the email has attachments and the `savedtodrive` keyword.
A save sets that keyword on the email with `Email/set` after its UPLOAD ends saved.
An app kill between the two, and an email saved before the keyword, show idle chips:
a Save press finds the file through the pre-save LOOKUP and saves no copy.
- Once per save queue run, before the first save, for all eligible attachments of the email.
- After an unknown save outcome: a fresh LOOKUP for that file, skipping the stored result.
It starts 15 s after the UPLOAD dispatch (the stack's 10 s download deadline plus a margin).
The chip shows checking until then.
A save cut by leaving the view gets it when its email shows again, no earlier than 15 s after dispatch.
- Each chunk is its own intent with its own headless page load. All chunks of one run share one linked-token exchange.
An opened email with the keyword and up to 100 attachments costs one intent and one page load per LOOKUP run.
### Store
- One app-level store, keyed `<accountId>:<emailId>`, owns the known save results for the app session:
saved, saved without a reference, found by LOOKUP, size mismatch, not confirmed.
- Running LOOKUPs, saves and recovery timers belong to the conversation view ([ADR-0120](0120-save-all-to-drive.md)).
Leaving it cancels them.
- The LOOKUP runner, the save queue and the chip are its clients. The chip never calls LOOKUP.
- It is cleared on account switch and logout.
- A save or LOOKUP result that ends after the store was cleared is dropped.
### State model
- Three writers: a LOOKUP answer, a save outcome, and a recovery LOOKUP.
- A LOOKUP answer never overwrites a file that is saving, or that was saved after that LOOKUP started.
- A save writes its outcome as soon as it ends.
- Reopening the email within 60 s reads the store and does not repeat LOOKUP. A later reopen runs LOOKUP again,
so changes made by other clients show.
- One LOOKUP runs per email at a time, shared by email open and Save presses.
It ends when the conversation view leaves.
- A size mismatch is a known outcome. The store keeps its file ID, and a LOOKUP hit on it leaves the attachment idle.
A LOOKUP `null` for it clears the mismatch and its file ID, so the attachment can be saved again.
LOOKUP returns no size, so after an app restart that file shows as saved.
- A file saved without a reference stays saved in the store for the app session.
A LOOKUP `null` does not clear it. After an app restart it shows idle.
### Failure, cancel and logging
- A LOOKUP with no answer keeps the stored state of its attachments. One with no stored state stays idle.
There is no automatic retry. The causes are:
a `ready` or result timeout, `error`, renderer loss, a failed chunk, leaving the view, and backgrounding.
- After an unknown save outcome, a fresh LOOKUP with no answer returns that file to idle (ADR-0118).
- `services: []` means LOOKUP is unsupported. Hide See in Drive from the start. The Save chip stays, idle.
- Log the intent ID, the chunk count, the outcome type and the duration.
Never log tokens, URLs, reference IDs or file names.
## Consequences
- Each chunk waits 20 s for `ready` (ADR-0118), then 30 s for its LOOKUP result.
- A saved-state rollout needs a Drive release with LOOKUP ([Drive #4664](https://github.com/linagora/twake-drive/pull/4664), merged) and its deployment.
- Without LOOKUP, saving again after a not-confirmed outcome may create a numbered copy.
## Sources
- [ADR-0096: Drive intent handshake](0096-postmessage-handshake-for-drive-intent-webview.md)
- [ADR-0108: Workplace call transport](0108-workplace-call-transport.md)
- [ADR-0118: Save an attachment to Twake Drive](0118-save-attachment-to-drive.md)
- [ADR-0120: Save all attachments to Twake Drive](0120-save-all-to-drive.md)
- [Mail React LOOKUP](https://github.com/Crash--/twake-mail-frontend/pull/1205)
- [Mail React savedtodrive keyword](https://github.com/Crash--/twake-mail-frontend/pull/1237)
+7
View File
@@ -59,3 +59,10 @@ Categories: UX, UI, Product, Architecture, Infra, Tech.
- Decided by: Dang Dat, in linagora/tmail-flutter#5237.
- Why: two flows on one email would run two UPLOADs at once and end in several toasts. A save cut on leave is resolved by LOOKUP when the email shows again.
- Scope: Save to Drive (#4791), `docs/adr/0120-save-all-to-drive.md`.
## 2026-12-02, [Architecture] Drive LOOKUP is the only source of saved state, results held by one app-level store
- Decision: Mail reads saved state with the Drive LOOKUP intent on opening an email with the `savedtodrive` keyword, which a save sets, before a save queue run and after an unknown save outcome. One app-level store per email owns the save results; running LOOKUPs and saves belong to the conversation view. A file saved without a reference stays saved for the app session. Details in ADR-0121.
- Decided by: Dang Dat, in linagora/tmail-flutter#5239.
- Why: LOOKUP rules were spread over ADR-0118, and a reference-free save went back to idle on reopen.
- Scope: Save to Drive (#4791), `docs/adr/0121-drive-lookup-saved-state.md`.