Skip to content

Fix pre dryrun fail - #6

Merged
dongjiang1989 merged 4 commits into
masterfrom
fix-pre-dryrun-fail
May 29, 2026
Merged

Fix pre dryrun fail#6
dongjiang1989 merged 4 commits into
masterfrom
fix-pre-dryrun-fail

Conversation

@dongjiang1989

@dongjiang1989 dongjiang1989 commented May 29, 2026

Copy link
Copy Markdown
Member

Closes: #5

Summary by CodeRabbit

  • Bug Fixes

    • More robust snapshot error handling with warnings instead of crashes and safer repo-existence checks.
    • CLI exit remains non-zero on item failures.
  • Performance Improvements

    • Smarter transfer scheduling: small files in parallel, large files sequentially with progress logging.
    • Skip state updates when nothing was transferred to avoid needless writes.
  • Tests

    • Added tests covering snapshot-failure scenarios across sync directions.

Review Change Stack

sync.yml:
- Fix word-splitting bug: replace string ARGS with bash array,
  same issue as entrypoint.sh (PR #1 review)

sync_engine.py:
- Wrap HF snapshot fetch in try/except (was unprotected, unlike MS)
- Add explicit guard: HF_TO_MS fails clearly when HF snapshot unavailable
- Add explicit guard: BIDIRECTIONAL fails clearly when both snapshots unavailable
- Fix _update_state: handle None hf_snapshot (was causing AttributeError)
- Change exit code: only exit(1) when ALL items fail, not on partial failure

tests:
- test_both_snapshots_fail_bidirectional: both adapters fail → FAILED
- test_hf_snapshot_fails_ms_to_hf_ok: HF fails but MS_TO_HF only
  needs MS snapshot → SUCCESS (exposes _update_state bug that was fixed)

Signed-off-by: dongjiang <dongjiang1989@126.com>
Address PR #4 review from gemini-code-assist:

1. BIDIRECTIONAL: change `and` to `or` in snapshot None check.
   Both snapshots are required for bidirectional sync — if either
   is None, detect_bidirectional() crashes with AttributeError on
   the None snapshot. Now raises RuntimeError with specific status
   per adapter (e.g. "HF=OK, MS=FAILED").

2. Exit code: revert to exit(1) on any failure (not just all-fail).
   CI/CD downstream steps already use `if: always()` so they run
   regardless of exit code. Returning 0 on partial failure would
   silently mask errors in automated pipelines.

+1 test: test_one_snapshot_fails_bidirectional

Signed-off-by: dongjiang <dongjiang1989@126.com>
Problem analysis from failed sync run (151s, exit code 1, empty .sync_state/):

1. Disk space: 4 parallel downloads of Qwen2.5-7B (~14GB total) fills
   up the GitHub Actions runner (~14GB). Fix: large files (>100MB)
   transfer sequentially (download→upload→delete→next), small files
   still use parallel.

2. create_repo_if_needed: was calling get_repo_snapshot (full file
   listing) just to check if repo exists. Fix: use repo_exists() which
   is a lightweight API call. Also wrap in try/except to be non-fatal.

3. _update_state: only called on full success, so partial transfers
   were lost. Fix: call _update_state whenever files_synced is non-empty,
   so successfully transferred files are recorded and won't be re-synced.

Signed-off-by: dongjiang <dongjiang1989@126.com>
@coderabbitai

coderabbitai Bot commented May 29, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ba497946-ead9-4c7f-a0cd-1f08647d9cb0

📥 Commits

Reviewing files that changed from the base of the PR and between 1ed02f8 and 8047205.

📒 Files selected for processing (2)
  • src/adapters/modelscope_adapter.py
  • src/sync_engine.py

📝 Walkthrough

Walkthrough

This PR hardens the HF↔MS sync engine by catching snapshot fetch errors, validating required snapshots by direction, optimizing transfers with a 100MB small/large file strategy, persisting state only after successful transfers, and adding adapter/workflow safety and tests.

Changes

Snapshot error handling and sync optimization

Layer / File(s) Summary
Snapshot error handling and validation
src/sync_engine.py
HuggingFace snapshot fetching is wrapped in try/except to log warnings instead of crashing. BIDIRECTIONAL and HF_TO_MS modes validate that required snapshots exist, raising RuntimeError with repo details when missing.
Conditional state persistence
src/sync_engine.py
Sync state and HF commit tracking are updated only when transfers succeed and produce synced files; per-file SHA lookup falls back to the other platform's snapshot map when entries are missing.
Disk-space-aware transfer execution
src/sync_engine.py
Introduces a 100MB threshold to separate small files (parallel transfer via ThreadPoolExecutor) from large files (sequential transfer with progress logs); actions are sorted by size and existing success/failure aggregation is preserved.
Repository safety and workflow CLI improvements
src/adapters/modelscope_adapter.py, .github/workflows/sync.yml, src/sync_engine.py
ModelScope adapter explicitly checks repo existence before creating and logs non-fatal warnings on errors. Workflow constructs CLI args using a Bash array to preserve boundaries. CLI main computes an any_failed flag for exit logic.
Error handling test coverage
tests/test_sync_engine_extended.py
Three new tests verify bidirectional failure modes when snapshots are unavailable and confirm ms_to_hf can succeed when only the HF snapshot fetch fails.

Sequence Diagram(s)

sequenceDiagram
  participant Workflow as GH_Workflow
  participant SyncEngine
  participant HF_API as HuggingFaceAPI
  participant MS_API as ModelScopeAPI
  participant Executor as ThreadPoolExecutor
  participant Storage

  Workflow->>SyncEngine: invoke sync (python -m src.sync_engine with ARGS)
  SyncEngine->>HF_API: fetch HF snapshot (try/except)
  SyncEngine->>MS_API: fetch MS snapshot
  HF_API-->>SyncEngine: snapshot or error (logged)
  MS_API-->>SyncEngine: snapshot or error (logged)
  SyncEngine->>Executor: schedule small-file transfers (concurrent)
  SyncEngine->>Storage: sequentially transfer large files (one-by-one)
  Executor-->>SyncEngine: transfer results (success/failed)
  Storage-->>SyncEngine: transfer results
  SyncEngine->>Storage: update persisted state (only if files_synced non-empty)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

"A rabbit hops through the sync with care,
Catching failures mid-air,
Large files walk, small ones race,
State updates wait for success's embrace,
Tests hop in to make the path fair." 🐇✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 77.78% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'Fix pre dryrun fail' is vague and does not clearly communicate the scope of changes, which include workflow refactoring, error handling improvements, and transfer strategy optimization. Consider a more descriptive title like 'Fix sync workflow and snapshot error handling' that better summarizes the primary changes across workflow, adapter, and engine components.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed The PR closes issue #5 (sync workflow failure on 2026-05-29) with multiple substantive fixes: word-splitting bug in sync.yml, HF snapshot fetch error handling, runtime guards for missing snapshots, state persistence logic, and large-file transfer optimization.
Out of Scope Changes check ✅ Passed All changes directly address the reported sync failure: workflow improvements, snapshot error handling, transfer strategy optimization, and state persistence fixes are aligned with resolving the issue.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-pre-dryrun-fail

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

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces disk-space awareness by transferring large files (>100MB) sequentially and small files in parallel, improves error handling during snapshot fetching, and adds corresponding test cases. However, several critical issues were identified in the review: the ModelScope HubApi lacks a repo_exists method, which will cause an AttributeError; repository creation does not specify the repo_type for datasets; and updating the sync state can fail to record file hashes if the target snapshot is missing or doesn't yet contain the newly synced files.

Comment thread src/adapters/modelscope_adapter.py
Comment thread src/adapters/modelscope_adapter.py Outdated
Comment thread src/sync_engine.py Outdated
…ate update

PR #6 review fixes (2 of 3 comments valid):

1. modelscope_adapter.py: Pass repo_type parameter to create_repo()
   - Without this, dataset repos were incorrectly created as model repos
   - ModelScope SDK defaults repo_type='model' when not specified

2. sync_engine.py: Cross-reference file maps when updating sync state
   - When syncing MS→HF, the synced file's SHA is in ms_file_map, not hf_file_map
   - Without fallback, synced_files wouldn't be recorded in state
   - Now tries both maps: hf_file_map.get(fp) or ms_file_map.get(fp)
   - Same logic for MS state: ms_file_map.get(fp) or hf_file_map.get(fp)

Comment 1 (repo_exists method) was invalid: HubApi.repo_exists() does exist
in the ModelScope SDK (verified: returns bool, accepts repo_type parameter).

Signed-off-by: dongjiang <dongjiang1989@126.com>
@dongjiang1989
dongjiang1989 merged commit b324042 into master May 29, 2026
11 of 12 checks passed
dongjiang1989 added a commit that referenced this pull request May 29, 2026
…ate update

PR #6 review fixes (2 of 3 comments valid):

1. modelscope_adapter.py: Pass repo_type parameter to create_repo()
   - Without this, dataset repos were incorrectly created as model repos
   - ModelScope SDK defaults repo_type='model' when not specified

2. sync_engine.py: Cross-reference file maps when updating sync state
   - When syncing MS→HF, the synced file's SHA is in ms_file_map, not hf_file_map
   - Without fallback, synced_files wouldn't be recorded in state
   - Now tries both maps: hf_file_map.get(fp) or ms_file_map.get(fp)
   - Same logic for MS state: ms_file_map.get(fp) or hf_file_map.get(fp)

Comment 1 (repo_exists method) was invalid: HubApi.repo_exists() does exist
in the ModelScope SDK (verified: returns bool, accepts repo_type parameter).

Signed-off-by: dongjiang <dongjiang1989@126.com>
dongjiang1989 added a commit that referenced this pull request May 29, 2026
* fix: harden sync workflow and snapshot error handling (#3)

sync.yml:
- Fix word-splitting bug: replace string ARGS with bash array,
  same issue as entrypoint.sh (PR #1 review)

sync_engine.py:
- Wrap HF snapshot fetch in try/except (was unprotected, unlike MS)
- Add explicit guard: HF_TO_MS fails clearly when HF snapshot unavailable
- Add explicit guard: BIDIRECTIONAL fails clearly when both snapshots unavailable
- Fix _update_state: handle None hf_snapshot (was causing AttributeError)
- Change exit code: only exit(1) when ALL items fail, not on partial failure

tests:
- test_both_snapshots_fail_bidirectional: both adapters fail → FAILED
- test_hf_snapshot_fails_ms_to_hf_ok: HF fails but MS_TO_HF only
  needs MS snapshot → SUCCESS (exposes _update_state bug that was fixed)

Signed-off-by: dongjiang <dongjiang1989@126.com>

* fix: bidirectional snapshot guard uses 'or', revert partial exit code

Address PR #4 review from gemini-code-assist:

1. BIDIRECTIONAL: change `and` to `or` in snapshot None check.
   Both snapshots are required for bidirectional sync — if either
   is None, detect_bidirectional() crashes with AttributeError on
   the None snapshot. Now raises RuntimeError with specific status
   per adapter (e.g. "HF=OK, MS=FAILED").

2. Exit code: revert to exit(1) on any failure (not just all-fail).
   CI/CD downstream steps already use `if: always()` so they run
   regardless of exit code. Returning 0 on partial failure would
   silently mask errors in automated pipelines.

+1 test: test_one_snapshot_fails_bidirectional

Signed-off-by: dongjiang <dongjiang1989@126.com>

* fix: disk space, create_repo, and partial state persistence

Problem analysis from failed sync run (151s, exit code 1, empty .sync_state/):

1. Disk space: 4 parallel downloads of Qwen2.5-7B (~14GB total) fills
   up the GitHub Actions runner (~14GB). Fix: large files (>100MB)
   transfer sequentially (download→upload→delete→next), small files
   still use parallel.

2. create_repo_if_needed: was calling get_repo_snapshot (full file
   listing) just to check if repo exists. Fix: use repo_exists() which
   is a lightweight API call. Also wrap in try/except to be non-fatal.

3. _update_state: only called on full success, so partial transfers
   were lost. Fix: call _update_state whenever files_synced is non-empty,
   so successfully transferred files are recorded and won't be re-synced.

Signed-off-by: dongjiang <dongjiang1989@126.com>

* fix: add repo_type to create_repo and cross-reference file maps in state update

PR #6 review fixes (2 of 3 comments valid):

1. modelscope_adapter.py: Pass repo_type parameter to create_repo()
   - Without this, dataset repos were incorrectly created as model repos
   - ModelScope SDK defaults repo_type='model' when not specified

2. sync_engine.py: Cross-reference file maps when updating sync state
   - When syncing MS→HF, the synced file's SHA is in ms_file_map, not hf_file_map
   - Without fallback, synced_files wouldn't be recorded in state
   - Now tries both maps: hf_file_map.get(fp) or ms_file_map.get(fp)
   - Same logic for MS state: ms_file_map.get(fp) or hf_file_map.get(fp)

Comment 1 (repo_exists method) was invalid: HubApi.repo_exists() does exist
in the ModelScope SDK (verified: returns bool, accepts repo_type parameter).

Signed-off-by: dongjiang <dongjiang1989@126.com>

* fix(modelscope): improve error message for permission failures

When uploading to ModelScope fails due to lack of write access, the API
returns a misleading "resource does not exist" error. This commit adds
explicit error detection for permission failures (401/403/"does not exist")
and raises a clear PermissionError with actionable guidance:

- Tells the user they don't have write access
- Suggests using their own namespace (e.g., 'username/model-name')
- Applies to both SDK upload and HF fallback upload paths

This prevents confusing errors when users try to sync to repos they
don't own (e.g., 'Qwen/Qwen2.5-7B-Instruct' instead of their own repo).

Signed-off-by: dongjiang <dongjiang1989@126.com>

* chore(config): use own ModelScope namespace for sync target

The previous ms_repo_id 'Qwen/Qwen2.5-7B-Instruct' belongs to the
Qwen organization and requires write access that external users do
not have. Changed to 'dongjiang1989/Qwen2.5-7B-Instruct' so the
sync can create and upload to a repo under the user's own namespace.

Signed-off-by: dongjiang <dongjiang@kubeservice.com>
Signed-off-by: dongjiang <dongjiang1989@126.com>

* fix(modelscope): case-insensitive permission error detection

Use str(e).lower() when matching error messages so that variants
like "Does Not Exist", "Forbidden", or "FORBIDDEN" are all caught.
Also added "forbidden" as an additional keyword to match.

Addresses gemini-code-assist review on PR #8.

Signed-off-by: dongjiang <dongjiang@kubeservice.com>
Signed-off-by: dongjiang <dongjiang1989@126.com>

* style(modelscope): fix E501 line-too-long in permission checks

Refactor long `or` chain into `any()` with a tuple of keywords to
keep lines under 100 chars.

Signed-off-by: dongjiang <dongjiang@kubeservice.com>
Signed-off-by: dongjiang <dongjiang1989@126.com>

---------

Signed-off-by: dongjiang <dongjiang1989@126.com>
Signed-off-by: dongjiang <dongjiang@kubeservice.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sync failure on 2026-05-29

1 participant