From c94a6bbbc4be512eb821d2a18c5b93abcfd1fcf4 Mon Sep 17 00:00:00 2001 From: Jayce-Ping <315229706@qq.com> Date: Sun, 17 May 2026 10:36:34 +0800 Subject: [PATCH 1/2] [adapter] refactor: hoist os.path.expanduser into _resolve_checkpoint_path Move the `path = os.path.expanduser(path)` call out of `load_checkpoint` and into `_resolve_checkpoint_path` itself. The resolver is now the single entry point that takes a raw user-provided spec and returns a usable local directory; callers no longer need to remember to normalize `~` separately. Safe in all three input branches: - Local path with `~`: expanded correctly (unchanged behavior). - Local path without `~`: no-op. - HF spec ('owner/repo' or 'hf://...'): no-op since `expanduser` only acts on a leading `~`. Co-authored-by: Cursor --- src/flow_factory/models/abc.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/flow_factory/models/abc.py b/src/flow_factory/models/abc.py index d1a36818..276670e3 100644 --- a/src/flow_factory/models/abc.py +++ b/src/flow_factory/models/abc.py @@ -1491,6 +1491,10 @@ def _resolve_checkpoint_path(self, path: str) -> str: Raises: FileNotFoundError: When the spec is neither a local path nor a reachable HF repo. """ + # Normalize leading ``~`` for local-path inputs; no-op for HF specs since + # ``expanduser`` only acts on a leading ``~``. + path = os.path.expanduser(path) + force_hf = path.startswith(HF_PATH_PREFIX) spec = path[len(HF_PATH_PREFIX):] if force_hf else path @@ -1741,7 +1745,6 @@ def load_checkpoint( - 'state': Load full training state (model + optimizer + scheduler + RNG) - None: Auto-detect based on checkpoint directory contents """ - path = os.path.expanduser(path) path = self._resolve_checkpoint_path(path) if not os.path.exists(path): raise FileNotFoundError( From 464357a88201692c1db39cecb23a1714413c44b8 Mon Sep 17 00:00:00 2001 From: Jayce-Ping <315229706@qq.com> Date: Sun, 17 May 2026 10:39:21 +0800 Subject: [PATCH 2/2] [adapter] refactor: drop dead exists-check; collapse `spec` local Two small cleanups in the HF-checkpoint resolve path, both follow-ups to the un-gating change: A. Remove the dead `if not os.path.exists(path)` guard in `load_checkpoint`. `_resolve_checkpoint_path` now either: - returns a path it has just confirmed via `os.path.exists`, or - returns a downloaded path that `download_hf_checkpoint` has verified with `os.path.isdir`, or - raises `FileNotFoundError` with the actual root cause. The guard was useful pre-ungating (the old post-barrier `snapshot_download` call could return a non-existent path on cache-miss-after-failure); now it's unreachable AND its error message would lie about the failure mode. B. Collapse the redundant `spec` local in `_resolve_checkpoint_path`. `parse_hf_checkpoint_path` already strips the `hf://` prefix internally, and the local-path `os.path.exists` check is gated on `not force_hf` so passing `path` (with the prefix still attached on the HF branch) is safe. Removes one variable from the reader's mental model. Pure refactor: no behavior change. The 8 happy-path + 5 original-error + 6 path-traversal parser cases all still pass. Co-authored-by: Cursor --- src/flow_factory/models/abc.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/flow_factory/models/abc.py b/src/flow_factory/models/abc.py index 276670e3..3df1871f 100644 --- a/src/flow_factory/models/abc.py +++ b/src/flow_factory/models/abc.py @@ -1494,14 +1494,14 @@ def _resolve_checkpoint_path(self, path: str) -> str: # Normalize leading ``~`` for local-path inputs; no-op for HF specs since # ``expanduser`` only acts on a leading ``~``. path = os.path.expanduser(path) - force_hf = path.startswith(HF_PATH_PREFIX) - spec = path[len(HF_PATH_PREFIX):] if force_hf else path - if not force_hf and os.path.exists(spec): - return spec + # Local path wins unless an explicit ``hf://`` prefix forces remote. + if not force_hf and os.path.exists(path): + return path - repo_id, subfolder, revision = parse_hf_checkpoint_path(spec) + # ``parse_hf_checkpoint_path`` handles the ``hf://`` prefix internally. + repo_id, subfolder, revision = parse_hf_checkpoint_path(path) try: local_path = download_hf_checkpoint(repo_id, subfolder, revision) @@ -1746,10 +1746,6 @@ def load_checkpoint( - None: Auto-detect based on checkpoint directory contents """ path = self._resolve_checkpoint_path(path) - if not os.path.exists(path): - raise FileNotFoundError( - f"Checkpoint path not found locally or on Hugging Face Hub: {path!r}" - ) # Auto-detect if not specified if resume_type is None: