Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- `dyro dispatch backends` 在 Grok CLI 只配置了 API key、`grok models` 未打印会话登录句时,也会识别为已认证;CLI 明确表示未登录时仍视为未认证。
- 修复 macOS 上 `/tmp` 与 `/private/tmp`(以及 `/var` 与 `/private/var`)路径写法不一致时,技能清单被误判过期、进程内状态锁重入死锁,以及蓝图加入与全局主目录路径对不齐的问题;旧版清单中的原写法仍可识别。

## 0.7.17 - 2026-11-14

Expand Down
7 changes: 5 additions & 2 deletions src/dyro/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .errors import DyroError, ValidationError
from .onboarding import RepositoryInput, render_config
from .process import git, require_ok, run
from .read_limits import canonical_entry_path
from .state import atomic_write_text, exclusive_lock
from .workspace import STORAGE_MODES, Line, create_line, doctor, get_line, is_missing_origin_finding

Expand Down Expand Up @@ -453,7 +454,9 @@ def load_blueprint_source(
def default_join_root(blueprint: Blueprint) -> Path:
override = os.environ.get("DYRO_PROJECTS_HOME", "").strip()
parent = Path(override).expanduser() if override else Path.home() / "DyroProjects"
return (parent / blueprint.suggested_directory).absolute()
# Rewrite trusted macOS /tmp|/var aliases only; keep user symlinks visible
# to preflight_join_plan / _reject_symlink_components.
return canonical_entry_path(parent / blueprint.suggested_directory)


def build_join_plan(
Expand All @@ -472,7 +475,7 @@ def build_join_plan(
f"蓝图中没有开发线 {selected_line};可选:{', '.join(sorted(blueprint.lines))}"
) from exc
root = (
Path(target).expanduser().absolute()
canonical_entry_path(Path(target).expanduser())
if target is not None
else default_join_root(blueprint)
)
Expand Down
10 changes: 5 additions & 5 deletions src/dyro/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from .config import Config, load, validate_id
from .errors import DyroError, ValidationError
from .read_limits import ReadBudget, ReadLimitCode, ReadLimitError
from .read_limits import ReadBudget, ReadLimitCode, ReadLimitError, canonical_entry_path
from .state import atomic_write_text, exclusive_lock


Expand Down Expand Up @@ -179,15 +179,15 @@ def unregistered_workspace_error(name: str, names: tuple[str, ...]) -> DyroError
def registry_home() -> Path:
override = os.environ.get("DYRO_HOME", "").strip()
if override:
return Path(override).expanduser().absolute()
return canonical_entry_path(Path(override).expanduser())
if os.name == "nt":
base = os.environ.get("APPDATA", "").strip()
if base:
return Path(base).expanduser().absolute() / "Dyro"
return canonical_entry_path(Path(base).expanduser()) / "Dyro"
base = os.environ.get("XDG_CONFIG_HOME", "").strip()
if base:
return Path(base).expanduser().absolute() / "dyro"
return Path.home() / ".config" / "dyro"
return canonical_entry_path(Path(base).expanduser()) / "dyro"
return canonical_entry_path(Path.home() / ".config" / "dyro")


def _registry_path() -> Path:
Expand Down
33 changes: 26 additions & 7 deletions src/dyro/integrations/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ReadLimitCode,
ReadLimitError,
bounded_directory_names,
canonical_entry_path,
trusted_system_alias_target,
)
from ..state import atomic_write_text, exclusive_lock, fsync_directory
Expand Down Expand Up @@ -217,7 +218,7 @@ def _absolute_path(value: Path, label: str) -> Path:
expanded = value.expanduser()
if not expanded.is_absolute():
raise ValidationError(f"{label} 必须是绝对路径:{value}")
return Path(os.path.normpath(expanded))
return canonical_entry_path(Path(os.path.normpath(expanded)))


def _dyro_home(override: Path | None) -> Path:
Expand Down Expand Up @@ -682,7 +683,12 @@ def _inspect_avatar(
str(avatar.readlink()) if avatar.is_symlink() else str(avatar.resolve()),
)
if avatar.is_dir() and not avatar.is_symlink():
if recorded is not None and str(recorded.get("path")) == str(avatar):
recorded_path = recorded.get("path") if recorded is not None else None
if (
isinstance(recorded_path, str)
and canonical_entry_path(Path(recorded_path))
== canonical_entry_path(avatar)
):
return AvatarStatus(host, avatar, "drifted", "分身被替换为普通目录")
# Owned legacy copy may still sit here before migration.
return AvatarStatus(
Expand Down Expand Up @@ -723,9 +729,13 @@ def _legacy_owned_copy(
except ValidationError:
return None
target = Path(str(manifest["target"]))
if expected_target is not None and target != expected_target:
if expected_target is not None and canonical_entry_path(target) != canonical_entry_path(
expected_target
):
return None
if allowed_targets is not None and target not in allowed_targets:
if allowed_targets is not None and canonical_entry_path(target) not in {
canonical_entry_path(path) for path in allowed_targets
}:
return None
if target.is_symlink() or not target.is_dir():
return None
Expand Down Expand Up @@ -899,7 +909,11 @@ def integration_status(
tuple(avatar_rows),
)

if manifest["mirror"] != str(mirror):
stored_mirror = manifest["mirror"]
if (
not isinstance(stored_mirror, str)
or canonical_entry_path(Path(stored_mirror)) != canonical_entry_path(mirror)
):
return IntegrationStatus(
requested,
IntegrationState.STALE_MANIFEST,
Expand Down Expand Up @@ -1186,7 +1200,11 @@ def _restored_owned_installation(
if manifest_path.read_text(encoding="utf-8") != original_manifest_text:
return False
manifest = _parse_manifest(manifest_path, spec)
if manifest["mirror"] != str(mirror):
stored_mirror = manifest["mirror"]
if (
not isinstance(stored_mirror, str)
or canonical_entry_path(Path(stored_mirror)) != canonical_entry_path(mirror)
):
return False
if _inventory(mirror) != manifest["files"]:
return False
Expand Down Expand Up @@ -1279,7 +1297,8 @@ def _install_avatars(
if avatar.exists() or avatar.is_symlink():
if (
legacy_target is not None
and avatar == legacy_target
and canonical_entry_path(avatar)
== canonical_entry_path(legacy_target)
and avatar in allowed
and avatar.is_dir()
and not avatar.is_symlink()
Expand Down
20 changes: 15 additions & 5 deletions src/dyro/read_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,26 @@ def trusted_system_alias_target(path: Path) -> Path | None:
return None


def canonical_entry_path(path: Path) -> Path:
"""Absolute path with trusted macOS ``/tmp`` / ``/var`` aliases rewritten.

Does not follow arbitrary symlinks. Use this when two spellings of the same
entry directory must compare equal (for example ``/tmp/x`` vs ``/private/tmp/x``).
"""
absolute = path.absolute()
if len(absolute.parts) > 1:
target = trusted_system_alias_target(Path(*absolute.parts[:2]))
if target is not None:
return target.joinpath(*absolute.parts[2:])
return absolute


def _checked_absolute(path: Path, label: str) -> Path:
if any(part in {".", ".."} for part in path.parts):
raise ValidationError(f"{label} 不得包含点路径分量")
absolute = path.absolute()
absolute = canonical_entry_path(path)
if not absolute.is_absolute() or not absolute.anchor:
raise ValidationError(f"{label} 必须是绝对路径")
if len(absolute.parts) > 1:
target = trusted_system_alias_target(Path(*absolute.parts[:2]))
if target is not None:
absolute = target.joinpath(*absolute.parts[2:])
return absolute


Expand Down
3 changes: 2 additions & 1 deletion src/dyro/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Iterator, TextIO

from .errors import DyroError
from .read_limits import canonical_entry_path


_LOCK_CONDITION = threading.Condition()
Expand Down Expand Up @@ -200,7 +201,7 @@ def _unlock(handle: TextIO) -> None:
@contextmanager
def exclusive_lock(path: Path, *, timeout_seconds: float = 15.0) -> Iterator[None]:
"""Acquire a process-safe, thread-reentrant lock for a small state transition."""
resolved = path.absolute()
resolved = canonical_entry_path(path)
owner = threading.get_ident()
deadline = time.monotonic() + timeout_seconds
reentrant = False
Expand Down
35 changes: 35 additions & 0 deletions tests/test_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
build_join_plan,
load_blueprint_source,
parse_blueprint,
preflight_join_plan,
)
from dyro.cli import main
from dyro.config import load
from dyro.errors import DyroError, ValidationError
from dyro.read_limits import canonical_entry_path
from dyro.workspace import doctor, get_line, is_missing_origin_finding

from .support import shell
Expand Down Expand Up @@ -411,6 +413,39 @@ def test_local_directory_source_rejects_symlinked_path_components(self) -> None:
with self.assertRaisesRegex(DyroError, "不能经过符号链接"):
load_blueprint_source(str(source), blueprint_file="config/team.toml")

@unittest.skipUnless(
Path("/tmp").is_symlink() and Path("/tmp").resolve() == Path("/private/tmp"),
"requires the macOS /tmp system alias",
)
def test_join_plan_root_resolves_tmp_alias_spelling(self) -> None:
with tempfile.TemporaryDirectory(prefix="dyro-join-alias-", dir="/tmp") as temporary:
canonical = Path(temporary).resolve()
try:
alias = Path("/tmp") / canonical.relative_to("/private/tmp")
except ValueError:
self.skipTest("temporary directory is not below /private/tmp")
target = alias / "workspace"
document = load_blueprint_source(str(self.blueprint_file))
plan = build_join_plan(document, target=target, line_id=None)
self.assertEqual(plan.root, canonical_entry_path(target))
self.assertEqual(plan.root, canonical / "workspace")

def test_join_refuses_symlinked_target_root(self) -> None:
real = self.parent / "real-workspace"
real.mkdir()
link = self.parent / "workspace-link"
link.symlink_to(real, target_is_directory=True)
plan = build_join_plan(
load_blueprint_source(str(self.blueprint_file)),
target=link,
line_id=None,
)
self.assertEqual(plan.root, canonical_entry_path(link))
self.assertTrue(plan.root.is_symlink())
with self.assertRaisesRegex(DyroError, "目标工作区不能是符号链接"):
preflight_join_plan(plan)
self.assertEqual(list(real.iterdir()), [])


if __name__ == "__main__":
unittest.main()
17 changes: 16 additions & 1 deletion tests/test_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
unique_registered_alias,
workspace_alias_retargets_root,
)
from dyro.read_limits import canonical_entry_path
from dyro.tooling import (
ToolPreferences,
ToolState,
Expand Down Expand Up @@ -93,7 +94,7 @@ def test_numeric_repository_id_wins_over_index(self) -> None:
class RegistryTests(unittest.TestCase):
def setUp(self) -> None:
self.tmp = tempfile.TemporaryDirectory(prefix="dyro-hub-")
self.base = Path(self.tmp.name)
self.base = canonical_entry_path(Path(self.tmp.name))
self.state = self.base / "state"
self.workspace = self.base / "workspace"
self.workspace.mkdir()
Expand Down Expand Up @@ -280,6 +281,20 @@ def test_malformed_registry_rejects_non_string_alias(self) -> None:
with self.assertRaisesRegex(Exception, "别名"):
load_registry()

@unittest.skipUnless(
Path("/tmp").is_symlink() and Path("/tmp").resolve() == Path("/private/tmp"),
"requires the macOS /tmp system alias",
)
def test_registry_home_rewrites_tmp_alias_spelling(self) -> None:
with tempfile.TemporaryDirectory(prefix="dyro-hub-alias-", dir="/tmp") as temporary:
canonical = Path(temporary).resolve()
try:
alias = Path("/tmp") / canonical.relative_to("/private/tmp")
except ValueError:
self.skipTest("temporary directory is not below /private/tmp")
with patch.dict(os.environ, {"DYRO_HOME": str(alias)}):
self.assertEqual(registry_home(), canonical)


class HubCliTests(WorkspaceCase):
def setUp(self) -> None:
Expand Down
Loading
Loading