Skip to content

fix(drive): download large files in validated ranged parts - #2839

Open
xu4wang wants to merge 1 commit into
larksuite:mainfrom
xu4wang:fix/drive-download-ranged
Open

xu4wang wants to merge 1 commit into
larksuite:mainfrom
xu4wang:fix/drive-download-ranged

Conversation

@xu4wang

@xu4wang xu4wang commented Oct 21, 2026 •

Copy link
Copy Markdown

Summary

drive +download fetches a file in one streaming response. open.feishu.cn resets that stream about five minutes after it starts, so any Drive file that takes longer than that to transfer cannot be downloaded at all, and each retry restarts from byte 0 (#2324). This PR moves +download onto the existing extension/download engine (already used by im +messages-resources-download) with 128 MiB Range requests, and pins the file revision so parts are never combined from two versions of the same token.

Changes

  • drive +download streams through extension/download.Open with PartSize = 128 MiB, so every response stays far below the reset window; failed parts are retried by the engine.
  • Revision pinning. Drive download responses carry no ETag, and a file token can be replaced in place (+upload --file-token) with same-size, different bytes. So:
    • latest_modify_time (from metas/batch_query) is read before the first part. Only when it is present are parts combined (ImmutableSource); otherwise MutableSource keeps today's single full response.
    • For files larger than one part, latest_modify_time is re-read when the body reaches EOF, before the atomic save commits. A change fails with network/representation_changed (retryable) and no output file is written.
    • Ceiling, documented at the decision point: latest_modify_time has one-second resolution, so a replacement within the same second as the first read is not detected. A server validator (ETag or a readable version) would close it.
  • With --output, the metadata read is best-effort (no new required scope). If it fails, the download falls back to one request and, for files larger than one part, prints a stderr warning.
  • common.DriveMeta gains LatestModifyTime (read with GetStringLoose).
  • Dry-run lists the revision read, the ranged download and the conditional re-check.
  • Behaviour changes worth noting:
    • An explicit --output now also issues one metadata call (the dry-run test that pinned "explicit output skips metadata" is updated).
    • HTTP 429 from the download endpoint now surfaces as the replay-safe transport's typed api/rate_limit (retryable, with backoff hint), the same contract as im resource downloads, instead of network with code 429.

Test Plan

  • Unit tests: go test ./shortcuts/drive/ ./shortcuts/common/ ./extension/download/ ./internal/downloadtransport/
    • New shortcuts/drive/drive_download_ranged_test.go: multi-part assembly with exact Range headers; replacement between parts fails with representation_changed and commits nothing; metadata error falls back to one full response; missing latest_modify_time falls back; numeric latest_modify_time still pins; single-part files skip the re-check.
    • Revert checks: the original +download, always-ImmutableSource, no re-check, re-check on single-part files, ignoring a revision mismatch, swallowing the re-check error, not passing the revision, and GetString instead of GetStringLoose each make at least one of these tests fail.
  • go test ./tests/cli_e2e/drive/ -run TestDriveDownloadDryRun (after make build)
  • go vet ./..., gofmt -l, golangci-lint run --new-from-rev (v2.1.6, Go 1.23): 0 issues; go run -C lint . passes
  • Manual live verification, bot identity, a 4.5 GiB (4,831,838,208 B) file in Drive, macOS arm64:
Binary Runs Result
v1.0.98 (unmodified) 6 6/6 failed at 217–331 s with cannot create file: stream error: stream ID 5; INTERNAL_ERROR; received from peer (with and without an HTTP proxy)
this PR 3 3/3 succeeded in 367 s / 420 s / 680 s (the last one shared bandwidth with a concurrent unmodified run that failed at 331 s); SHA-256 matched the source each time

An unmodified run that happened to finish in 305 s succeeded, which is consistent with a reset at roughly 5 minutes of stream time rather than a size limit.

Part size: each Range request waits ~0.5–1.2 s for its first byte. Measured sequential throughput was 10.2 MB/s with 32 MiB parts versus 15–16 MB/s with 128 MiB parts, which matches a single full stream (~15.8 MB/s).

Related Issues

Summary by CodeRabbit

  • New Features

    • Downloads now retrieve large files in 128 MiB ranged parts with automatic retries.
    • Downloads verify that the file did not change during transfer before committing output.
    • When revision metadata is unavailable, downloads fall back to a single full-response request.
  • Bug Fixes

    • Prevented partially downloaded output from being committed when a file changes during transfer.
    • Improved handling of rate-limit responses and metadata lookup failures.
  • Documentation

    • Added documentation covering ranged downloads, retries, consistency checks, and fallback behavior.

drive +download fetched each file in one response. open.feishu.cn resets
that stream about five minutes in ("stream error: stream ID 5;
INTERNAL_ERROR; received from peer"), so any file that takes longer than
that to transfer can never be downloaded, and every retry restarts from
byte 0.

Route +download through extension/download in 128 MiB Range requests with
per-part retries. Drive download responses carry no ETag and a file token
can be overwritten in place, so parts are combined only when the file's
latest_modify_time was read before the first part; it is re-read before
the output is committed and a change fails with representation_changed.
Without a revision the download keeps using one full response.

Refs larksuite#2324

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@xu4wang
xu4wang requested a review from liangshuo-1 as a code owner October 21, 2026 07:30
@github-actions github-actions Bot added domain/ccm PR touches the ccm domain size/L Large or sensitive change across domains or core paths labels Oct 21, 2026
@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5e331c5d-ed8a-4bb1-850c-115d5f793178
📥 Commits

Reviewing files that changed from the base of the PR and between 017b19a and dc9a4f3.

📒 Files selected for processing: 7
  • shortcuts/common/drive_meta.go
  • shortcuts/drive/drive_download.go
  • shortcuts/drive/drive_download_ranged_test.go
  • shortcuts/drive/drive_io_test.go
  • skills/lark-drive/references/lark-drive-download.md
  • tests/cli_e2e/drive/coverage.md
  • tests/cli_e2e/drive/drive_download_dryrun_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📝 Walkthrough

Drive downloads now use 128 MiB ranged parts when revision metadata is available. Multipart downloads check the file revision after reading. If metadata is unavailable or lacks a revision, the download uses a single full response.

Changes: Drive download flow

Layer / File(s) Summary
Expose Drive revision metadata
shortcuts/common/drive_meta.go
DriveMeta now includes LatestModifyTime, populated from latest_modify_time.
Download and verify ranged content
shortcuts/drive/drive_download.go, shortcuts/drive/drive_download_ranged_test.go, shortcuts/drive/drive_io_test.go, tests/cli_e2e/drive/drive_download_dryrun_test.go, skills/lark-drive/references/lark-drive-download.md, tests/cli_e2e/drive/coverage.md
Downloads use 128 MiB ranged parts when a revision is available and fall back to one full response otherwise. Multipart downloads recheck the revision; a changed revision returns a retryable error before output is committed. Dry-run descriptions, tests, and download guidance cover metadata lookups, ranged requests, and fallback behavior.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant DriveDownload
  participant FetchDriveMeta
  participant RangedDownloadTransport
  participant OutputSaver
  DriveDownload->>FetchDriveMeta: Retrieve title and latest_modify_time
  DriveDownload->>RangedDownloadTransport: Open ranged stream using available revision
  RangedDownloadTransport->>OutputSaver: Provide downloaded stream
  OutputSaver->>RangedDownloadTransport: Read stream through EOF
  RangedDownloadTransport->>FetchDriveMeta: Recheck latest_modify_time after multipart read
  FetchDriveMeta-->>RangedDownloadTransport: Return current revision
  RangedDownloadTransport-->>OutputSaver: Complete stream or return revision-change error
Loading

Suggested reviewers: wittam-01

Merge Risk: ⚪ Minimal · up to dc9a4

No confirmed issue blocks merging the revision-aware download change.

Pre-merge checks: Passed 5
Check name Status Explanation
Description check Passed The description includes all required sections. It clearly explains the motivation, implementation changes, test coverage, manual verification, and related issues.
Title check Passed The title is concise, specific, and accurately summarizes the main change: validated ranged downloads for large Drive files.
Docstring Coverage Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 18 functions across 5 files. (2 skipped: 2 …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 18 functions across 5 files. (2 skipped: 2 unsupported.)

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@CLAassistant

CLAassistant commented Oct 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

wufei-png added a commit to wufei-png/cli that referenced this pull request Oct 22, 2026
Adapt the single-invocation revision checks from larksuite#2839 and share them with +pull. Keep persistent resume separate for the next change.

Co-authored-by: xu4wang <311397+xu4wang@users.noreply.github.com>
@wufei-png

Copy link
Copy Markdown

I've updated #2663 to use the same 128 MiB ranges and latest_modify_time checks, with persistent resume in a separate commit. The adaptation credits your code and tests.

This PR can stay focused on transfers within one invocation. The resume follow-up binds the checkpoint to the source and revision, keeps it when metadata lookup fails, and rechecks before publishing. It retains the documented same-second overwrite limitation.

If this lands first, I'll rebase #2663 and drop the overlapping transfer changes. The unit/race, dry-run, and lint checks pass. My live rerun and three existing live fixture workflows in make test are blocked by a local keychain decryption failure.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

PR Quality Summary

CI did not complete successfully. Use the failed check links below to decide whether this PR needs a code change or a rerun.

CI status

  • Workflow conclusion: failure.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain/ccm PR touches the ccm domain size/L Large or sensitive change across domains or core paths

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants