JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(sandbox): 按路径深度分区 rw bind, 修复 directories 遮蔽 policy bind_mounts
openJiuwen-bot32a5caead0f4
diff --git a/jiuwenbox/src/jiuwenbox/supervisor/bwrap.py b/jiuwenbox/src/jiuwenbox/supervisor/bwrap.pyindex 62f1e199b2..c405af3db1 100644--- a/jiuwenbox/src/jiuwenbox/supervisor/bwrap.py+++ b/jiuwenbox/src/jiuwenbox/supervisor/bwrap.py@@ -114,6 +114,29 @@ def _partition_nested_ro_binds(return non_nested, nested+def _partition_nested_rw_binds(+ rw_binds: list[tuple[str, str]],+) -> tuple[list[tuple[str, str]], list[tuple[str, str]]]:+ """Split ``rw_binds`` into ``(non-nested, nested-under-another-rw)``.++ Mirror image of :func:`_partition_nested_ro_binds`: a nested rw-bind must+ be emitted *after* its rw ancestor so bwrap's later-wins keeps the more+ specific mountpoint live. ``directories`` backing binds are contributed by+ the runtime rather than the policy, so producer order cannot be relied on.+ Both partitions preserve the original relative order of ``rw_binds``.+ """+ rw_dsts = [dst for _, dst in rw_binds]+ non_nested: list[tuple[str, str]] = []+ nested: list[tuple[str, str]] = []+ for bind in rw_binds:+ dst = bind[1]+ if any(_is_strict_subpath(dst, other) for other in rw_dsts):+ nested.append(bind)+ else:+ non_nested.append(bind)+ return non_nested, nested++@dataclassclass BwrapConfig:"""Configuration that maps to bwrap CLI arguments."""@@ -514,13 +537,17 @@ class BwrapConfig:# bind_root_entries-vs-bind_mounts override contract holds for# non-overlapping paths.non_nested_ro, nested_ro = _partition_nested_ro_binds(ro_binds, rw_binds)+ non_nested_rw, nested_rw = _partition_nested_rw_binds(rw_binds)# read-only binds not nested under a rw parentfor src, dst in non_nested_ro:args.extend(["--ro-bind", src, dst])- # read-write binds- for src, dst in rw_binds:+ # read-write binds, shallowest first, so the nested per-path binds+ # below stay live under bwrap's later-wins semantics+ for src, dst in non_nested_rw:+ args.extend(["--bind", src, dst])+ for src, dst in nested_rw:args.extend(["--bind", src, dst])# read-only binds nested under a rw parent — emitted last so the rodiff --git a/jiuwenbox/tests/unit/test_bwrap_to_args.py b/jiuwenbox/tests/unit/test_bwrap_to_args.pyindex 22330d28a7..668e48db2f 100644--- a/jiuwenbox/tests/unit/test_bwrap_to_args.py+++ b/jiuwenbox/tests/unit/test_bwrap_to_args.py@@ -17,6 +17,7 @@ from jiuwenbox.supervisor.bwrap import (BwrapConfig,_is_strict_subpath,_partition_nested_ro_binds,+ _partition_nested_rw_binds,)CONFIG_YAML = "/home/user/.jiuwenswarm/config/config.yaml"@@ -83,6 +84,30 @@ def test_partition_nested_ro_binds_home_ancestor_from_lifecycle_dirs():assert nested == [("/h/cfg", CONFIG_YAML)]+def test_partition_nested_rw_binds_splits_and_preserves_order():+ rw = [("/h/backing", HOME), ("/h/ws", JIUWENSWARM), ("/h/agent", AGENT)]+ non_nested, nested = _partition_nested_rw_binds(rw)+ assert non_nested == [("/h/backing", HOME)]+ assert nested == [("/h/ws", JIUWENSWARM), ("/h/agent", AGENT)]+++def test_partition_nested_rw_binds_deepest_emitted_last():+ # Only the shallowest entry escapes the "nested" group; combined with the+ # non_nested-before-nested emission order the deepest mount wins.+ rw = [("/h/a", "/home/a"), ("/h/a/b", "/home/a/b"), ("/h/a/b/c", "/home/a/b/c")]+ non_nested, nested = _partition_nested_rw_binds(rw)+ assert non_nested == [("/h/a", "/home/a")]+ assert nested == [("/h/a/b", "/home/a/b"), ("/h/a/b/c", "/home/a/b/c")]++ cfg = BwrapConfig(command=["/bin/sh"])+ cfg.rw_binds = rw+ args = cfg.to_args()+ assert _bind_index(args, "--bind", "/home/a") < _bind_index(args, "--bind", "/home/a/b")+ assert _bind_index(args, "--bind", "/home/a/b") < _bind_index(+ args, "--bind", "/home/a/b/c"+ )++# ---------------------------------------------------------------------------# to_args emission ordering# ---------------------------------------------------------------------------@@ -180,3 +205,38 @@ def test_same_path_ro_not_nested_remount_still_fires():remount = _remount_index(args, same)assert ro >= 0 and rw >= 0 and remount >= 0assert ro < rw < remount+++def test_nested_rw_under_home_lifecycle_emitted_after_home_backing():+ # The directories:/home backing bind is appended after the policy's+ # bind_mounts; nested-rw partitioning keeps the per-path bind live.+ cfg = BwrapConfig(command=["/bin/sh"])+ cfg.rw_binds = [(JIUWENSWARM, JIUWENSWARM), (HOME, HOME)]+ args = cfg.to_args()++ home_bind = _bind_index(args, "--bind", HOME)+ ws_bind = _bind_index(args, "--bind", JIUWENSWARM)+ assert home_bind >= 0 and ws_bind >= 0+ assert home_bind < ws_bind+++def test_backing_then_nested_rw_then_nested_ro_order():+ # directories:/home backing + bind_mounts underneath it + a nested ro bind.+ cfg = BwrapConfig(command=["/bin/sh"])+ cfg.ro_binds = [("/host/cfg", CONFIG_YAML), ("/host/bin", "/bin")]+ cfg.rw_binds = [+ (JIUWENSWARM, JIUWENSWARM),+ ("/host/backing-home", HOME),+ ("/host/backing-tmp", "/tmp"),+ ]+ cfg.remount_ro = ["/", "/bin", CONFIG_YAML]+ args = cfg.to_args()++ ro_bin = _bind_index(args, "--ro-bind", "/bin")+ home_bind = _bind_index(args, "--bind", HOME)+ tmp_bind = _bind_index(args, "--bind", "/tmp")+ ws_bind = _bind_index(args, "--bind", JIUWENSWARM)+ ro_cfg = _bind_index(args, "--ro-bind", CONFIG_YAML)+ assert -1 not in (ro_bin, home_bind, tmp_bind, ws_bind, ro_cfg)+ assert ro_bin < home_bind < tmp_bind < ws_bind < ro_cfg+ assert _remount_index(args, CONFIG_YAML) > ro_cfg