File size: 22,965 Bytes
5e5d6c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 | from __future__ import annotations
import copy
import os
import pickle
import stat
from pathlib import Path
import pytest
import ctx.runtime.release_skill_layout as layout_module
from ctx.runtime.release_skill_layout import (
ReleaseSkillRuntimeLayoutError,
open_release_skill_runtime_layout,
open_workspace_release_skill_runtime_layout,
)
def _open(tmp_path: Path, workspace: Path):
return open_release_skill_runtime_layout(
state_root=tmp_path / "state",
host_context_id="codex",
native_session_id="native/session secret",
workspace=workspace,
)
def _open_workspace_layout(
tmp_path: Path,
workspace: Path,
*,
state_name: str = "workspace-state",
policy_name: str = "onboarding-policy",
):
policy_root = tmp_path / policy_name
policy_root.mkdir(mode=0o700, exist_ok=True)
policy_root.chmod(0o700)
return open_workspace_release_skill_runtime_layout(
state_root=tmp_path / state_name,
policy_store_root=policy_root,
workspace=workspace,
)
def test_workspace_layout_is_stable_across_native_host_sessions(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
policy_root = tmp_path / "onboarding-policy"
policy_root.mkdir(mode=0o700)
policy_root.chmod(0o700)
before = open_workspace_release_skill_runtime_layout(
state_root=tmp_path / "workspace-state",
policy_store_root=policy_root,
workspace=workspace,
)
open_release_skill_runtime_layout(
state_root=tmp_path / "legacy-state",
host_context_id="codex",
native_session_id="codex-turn-1",
workspace=workspace,
)
open_release_skill_runtime_layout(
state_root=tmp_path / "legacy-state",
host_context_id="claude-code",
native_session_id="claude-turn-9",
workspace=workspace,
)
after = open_workspace_release_skill_runtime_layout(
state_root=tmp_path / "workspace-state",
policy_store_root=policy_root,
workspace=workspace,
)
assert after == before
assert before.host_context_id == "ctx-workspace-management"
assert before.session_id.startswith("workspace-session-")
assert "codex" not in before.session_id
assert "claude" not in before.session_id
assert before.journal_path == after.journal_path
assert before.benefit_audit_path == after.benefit_audit_path
assert before.consent_broker_path == after.consent_broker_path
assert before.skill_store_root == after.skill_store_root
assert before.policy_store_root == policy_root
assert not (before.managed_root / "install-policy-v1").exists()
def test_workspace_layout_does_not_reinterpret_or_migrate_v1_state(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
state_root = tmp_path / "shared-state"
legacy = open_release_skill_runtime_layout(
state_root=state_root,
host_context_id="codex",
native_session_id="legacy-session",
workspace=workspace,
)
policy_root = tmp_path / "onboarding-policy"
policy_root.mkdir(mode=0o700)
policy_root.chmod(0o700)
workspace_layout = open_workspace_release_skill_runtime_layout(
state_root=state_root,
policy_store_root=policy_root,
workspace=workspace,
)
assert legacy.managed_root == state_root / "managed-capabilities-v1"
assert workspace_layout.managed_root == state_root / "managed-capabilities-v2"
assert legacy.policy_store_root.exists()
assert legacy.skill_store_root.exists()
assert workspace_layout.policy_store_root == policy_root
assert workspace_layout.skill_store_root != legacy.skill_store_root
assert legacy.consent_broker_path is None
assert workspace_layout.consent_broker_path is not None
def test_v1_layout_keeps_legacy_namespace_and_host_session_partition(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
codex = open_release_skill_runtime_layout(
state_root=tmp_path / "legacy-state",
host_context_id="codex",
native_session_id="codex-session",
workspace=workspace,
)
claude = open_release_skill_runtime_layout(
state_root=tmp_path / "legacy-state",
host_context_id="claude-code",
native_session_id="claude-session",
workspace=workspace,
)
assert codex.managed_root == codex.state_root / "managed-capabilities-v1"
assert codex.policy_store_root == codex.managed_root / "install-policy-v1"
assert codex.skill_store_root == codex.managed_root / "skill-cas-v1"
assert codex.session_root != claude.session_root
assert codex.journal_path == codex.session_root / "engine.sqlite3"
assert codex.benefit_audit_path == codex.session_root / "benefit.sqlite3"
assert codex.consent_broker_path is None
assert claude.consent_broker_path is None
def test_workspace_layout_keeps_management_and_host_exposure_separate(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
layout = _open_workspace_layout(tmp_path, workspace)
assert layout.managed_root == layout.state_root / "managed-capabilities-v2"
assert layout.session_root.parent.name == "workspaces"
assert layout.session_root.name == layout.workspace_identity_digest
assert layout.skill_store_root == layout.session_root / "skill-cas-v1"
assert layout.consent_broker_path == layout.session_root / "install-consent-v1.sqlite3"
assert not layout.consent_broker_path.exists()
assert not any(part.startswith("host-") for part in layout.session_root.parts)
assert "codex" not in os.fspath(layout.session_root)
assert "claude" not in os.fspath(layout.session_root)
request = layout.install_request(
task="manage one useful capability",
language="Python",
occurred_at="2026-08-02T12:00:00Z",
)
assert request.host_context_id == "ctx-workspace-management"
assert request.host_identity_digest == layout.host_identity_digest
assert request.session_id == layout.session_id
assert request.policy_store_root == layout.policy_store_root
def test_workspace_consent_broker_path_is_shared_but_outside_user_policy_and_cas_roots(
tmp_path: Path,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
first = _open_workspace_layout(tmp_path, workspace)
second = open_workspace_release_skill_runtime_layout(
state_root=first.state_root,
policy_store_root=first.policy_store_root,
workspace=workspace,
)
assert first.consent_broker_path is not None
assert second.consent_broker_path == first.consent_broker_path
assert first.consent_broker_path.parent == first.session_root
assert first.consent_broker_path not in workspace.parents
assert workspace not in first.consent_broker_path.parents
assert first.policy_store_root not in first.consent_broker_path.parents
assert first.skill_store_root not in first.consent_broker_path.parents
assert not first.consent_broker_path.exists()
@pytest.mark.skipif(os.name == "nt", reason="POSIX owner-mode contract")
def test_workspace_layout_accepts_only_private_single_link_broker_database(
tmp_path: Path,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
layout = _open_workspace_layout(tmp_path, workspace)
assert layout.consent_broker_path is not None
layout.consent_broker_path.touch(mode=0o600)
layout.consent_broker_path.chmod(0o600)
reopened = _open_workspace_layout(tmp_path, workspace)
assert reopened.consent_broker_path == layout.consent_broker_path
reopened.assert_current()
layout.consent_broker_path.chmod(0o644)
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="unsafe|owner-private"):
reopened.assert_current()
@pytest.mark.skipif(os.name == "nt", reason="POSIX inode contract")
@pytest.mark.parametrize("hazard", ["symlink", "directory", "hardlink"])
def test_workspace_layout_rejects_broker_path_alias_hazards(
tmp_path: Path,
hazard: str,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
layout = _open_workspace_layout(tmp_path, workspace)
assert layout.consent_broker_path is not None
if hazard == "symlink":
layout.consent_broker_path.symlink_to(layout.policy_store_root, target_is_directory=True)
elif hazard == "directory":
layout.consent_broker_path.mkdir(mode=0o700)
else:
layout.journal_path.touch(mode=0o600)
layout.journal_path.chmod(0o600)
os.link(layout.journal_path, layout.consent_broker_path)
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="unsafe|distinct|regular|overlap"):
_open_workspace_layout(tmp_path, workspace)
def test_workspace_layout_rejects_broker_path_escape_or_cas_nesting(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
layout = _open_workspace_layout(tmp_path, workspace)
assert layout.consent_broker_path is not None
object.__setattr__(
layout,
"consent_broker_path",
layout.skill_store_root / "install-consent-v1.sqlite3",
)
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="changed|unsafe"):
layout.assert_current()
def test_workspace_layout_isolated_per_workspace_but_shares_explicit_policy(
tmp_path: Path,
) -> None:
first_workspace = tmp_path / "first-workspace"
second_workspace = tmp_path / "second-workspace"
first_workspace.mkdir()
second_workspace.mkdir()
policy_root = tmp_path / "onboarding-policy"
policy_root.mkdir(mode=0o700)
policy_root.chmod(0o700)
first = open_workspace_release_skill_runtime_layout(
state_root=tmp_path / "workspace-state",
policy_store_root=policy_root,
workspace=first_workspace,
)
second = open_workspace_release_skill_runtime_layout(
state_root=tmp_path / "workspace-state",
policy_store_root=policy_root,
workspace=second_workspace,
)
assert first.session_id != second.session_id
assert first.journal_path != second.journal_path
assert first.benefit_audit_path != second.benefit_audit_path
assert first.skill_store_root != second.skill_store_root
assert first.policy_store_root == second.policy_store_root == policy_root
def test_workspace_layout_requires_absolute_input_roots(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
policy_root = tmp_path / "policy"
policy_root.mkdir(mode=0o700)
policy_root.chmod(0o700)
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="absolute"):
open_workspace_release_skill_runtime_layout(
state_root=Path("relative-state"),
policy_store_root=policy_root,
workspace=workspace,
)
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="absolute"):
open_workspace_release_skill_runtime_layout(
state_root=tmp_path / "state",
policy_store_root=Path("relative-policy"),
workspace=workspace,
)
monkeypatch.chdir(tmp_path)
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="absolute"):
open_workspace_release_skill_runtime_layout(
state_root=tmp_path / "state",
policy_store_root=policy_root,
workspace=Path("workspace"),
)
def test_workspace_layout_canonicalizes_absolute_lexical_aliases(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
policy_root = tmp_path / "policy"
policy_root.mkdir(mode=0o700)
policy_root.chmod(0o700)
lexical_parent = tmp_path / "lexical-parent"
lexical_parent.mkdir()
lexical = open_workspace_release_skill_runtime_layout(
state_root=lexical_parent / ".." / "state",
policy_store_root=lexical_parent / ".." / "policy",
workspace=lexical_parent / ".." / "workspace",
)
canonical = open_workspace_release_skill_runtime_layout(
state_root=tmp_path / "state",
policy_store_root=policy_root,
workspace=workspace,
)
assert lexical == canonical
assert lexical.state_root == tmp_path / "state"
assert lexical.policy_store_root == policy_root
@pytest.mark.parametrize(
("alias_kind", "expected_error"),
(
("state", "distinct"),
("managed", "overlap"),
("workspace", "overlap"),
("skill", "overlap"),
),
)
def test_workspace_layout_rejects_policy_root_aliases(
tmp_path: Path,
alias_kind: str,
expected_error: str,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
state_root = tmp_path / "state"
policy_root = tmp_path / "policy"
policy_root.mkdir(mode=0o700)
policy_root.chmod(0o700)
baseline = open_workspace_release_skill_runtime_layout(
state_root=state_root,
policy_store_root=policy_root,
workspace=workspace,
)
aliases = {
"state": baseline.state_root,
"managed": baseline.managed_root,
"workspace": baseline.session_root,
"skill": baseline.skill_store_root,
}
with pytest.raises(ReleaseSkillRuntimeLayoutError, match=expected_error):
open_workspace_release_skill_runtime_layout(
state_root=state_root,
policy_store_root=aliases[alias_kind],
workspace=workspace,
)
def test_workspace_layout_rejects_state_or_policy_alias_of_workspace(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir(mode=0o700)
workspace.chmod(0o700)
policy_root = tmp_path / "policy"
policy_root.mkdir(mode=0o700)
policy_root.chmod(0o700)
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="distinct"):
open_workspace_release_skill_runtime_layout(
state_root=workspace,
policy_store_root=policy_root,
workspace=workspace,
)
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="distinct"):
open_workspace_release_skill_runtime_layout(
state_root=tmp_path / "state",
policy_store_root=workspace,
workspace=workspace,
)
@pytest.mark.parametrize(
("descendant", "ancestor"),
(
("state", "workspace"),
("policy", "workspace"),
("workspace", "state"),
("workspace", "policy"),
("policy", "state"),
("state", "policy"),
),
)
def test_workspace_layout_rejects_nested_caller_roots(
tmp_path: Path,
descendant: str,
ancestor: str,
) -> None:
roots = {
"state": tmp_path / "state",
"policy": tmp_path / "policy",
"workspace": tmp_path / "workspace",
}
roots[descendant] = roots[ancestor] / f"nested-{descendant}"
for path in sorted(set(roots.values()), key=lambda item: len(item.parts)):
path.mkdir(parents=True, exist_ok=True, mode=0o700)
path.chmod(0o700)
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="overlap"):
open_workspace_release_skill_runtime_layout(
state_root=roots["state"],
policy_store_root=roots["policy"],
workspace=roots["workspace"],
)
assert not (roots["state"] / "managed-capabilities-v2").exists()
def test_workspace_layout_rejects_physical_aliases_before_creating_state(
tmp_path: Path,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
state_root = tmp_path / "state"
state_root.mkdir(mode=0o700)
state_root.chmod(0o700)
policy_alias = tmp_path / "policy-alias"
policy_alias.symlink_to(state_root, target_is_directory=True)
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="distinct"):
open_workspace_release_skill_runtime_layout(
state_root=state_root,
policy_store_root=policy_alias,
workspace=workspace,
)
assert not (state_root / "managed-capabilities-v2").exists()
def test_workspace_layout_rejects_symlinked_policy_alias(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
real_policy = tmp_path / "real-policy"
real_policy.mkdir(mode=0o700)
real_policy.chmod(0o700)
policy_alias = tmp_path / "policy-alias"
policy_alias.symlink_to(real_policy, target_is_directory=True)
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="unsafe"):
open_workspace_release_skill_runtime_layout(
state_root=tmp_path / "state",
policy_store_root=policy_alias,
workspace=workspace,
)
def test_workspace_layout_detects_policy_alias_after_open(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
layout = _open_workspace_layout(tmp_path, workspace)
layout.policy_store_root.rmdir()
layout.policy_store_root.symlink_to(layout.skill_store_root, target_is_directory=True)
with pytest.raises(ReleaseSkillRuntimeLayoutError):
layout.assert_current()
@pytest.mark.parametrize("public_root", ["state", "policy"])
def test_workspace_layout_rejects_non_private_roots(
tmp_path: Path,
public_root: str,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
state_root = tmp_path / "state"
policy_root = tmp_path / "policy"
state_root.mkdir(mode=0o700)
policy_root.mkdir(mode=0o700)
state_root.chmod(0o755 if public_root == "state" else 0o700)
policy_root.chmod(0o755 if public_root == "policy" else 0o700)
with pytest.raises(
ReleaseSkillRuntimeLayoutError,
match="unsafe|owner-private",
):
open_workspace_release_skill_runtime_layout(
state_root=state_root,
policy_store_root=policy_root,
workspace=workspace,
)
@pytest.mark.skipif(os.name == "nt", reason="POSIX owner-mode contract")
def test_workspace_layout_roots_are_absolute_distinct_and_owner_private(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
layout = _open_workspace_layout(tmp_path, workspace)
roots = (
layout.state_root,
layout.managed_root,
layout.session_root,
layout.skill_store_root,
layout.policy_store_root,
)
assert all(root.is_absolute() for root in roots)
assert len(set(roots)) == len(roots)
assert len({(root.stat().st_dev, root.stat().st_ino) for root in roots}) == len(roots)
assert all(stat.S_IMODE(root.stat(follow_symlinks=False).st_mode) == 0o700 for root in roots)
def test_workspace_layout_fails_closed_on_windows(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
policy_root = tmp_path / "policy"
policy_root.mkdir()
monkeypatch.setattr(layout_module.os, "name", "nt")
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="not available on Windows"):
open_workspace_release_skill_runtime_layout(
state_root=tmp_path / "state",
policy_store_root=policy_root,
workspace=workspace,
)
def test_release_skill_layout_is_restart_stable_and_path_private(tmp_path: Path) -> None:
workspace = tmp_path / "customer-secret-workspace"
workspace.mkdir()
first = _open(tmp_path, workspace)
second = _open(tmp_path, workspace)
assert first == second
assert first.workspace_identity_digest == second.workspace_identity_digest
assert first.session_id == second.session_id
assert "native/session secret" not in os.fspath(first.session_root)
assert workspace.name not in os.fspath(first.session_root)
assert str(workspace) not in repr(first)
with pytest.raises(TypeError):
copy.copy(first)
with pytest.raises(TypeError):
pickle.dumps(first)
@pytest.mark.skipif(os.name == "nt", reason="POSIX owner-mode contract")
def test_release_skill_layout_creates_only_owner_private_directories(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
layout = _open(tmp_path, workspace)
for directory in (
layout.state_root,
layout.managed_root,
layout.session_root,
layout.skill_store_root,
layout.policy_store_root,
):
assert stat.S_IMODE(directory.stat(follow_symlinks=False).st_mode) == 0o700
def test_release_skill_layout_request_uses_only_derived_stable_identity(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
layout = _open(tmp_path, workspace)
request = layout.install_request(
task="repair state restoration",
language="Python",
occurred_at="2026-08-02T12:00:00Z",
)
assert request.workspace == workspace
assert request.session_id == layout.session_id
assert request.journal_path == layout.journal_path
assert request.benefit_audit_path == layout.benefit_audit_path
assert request.skill_store_root == layout.skill_store_root
assert request.policy_store_root == layout.policy_store_root
assert request.host_identity_digest == layout.host_identity_digest
def test_release_skill_layout_does_not_inherit_recreated_workspace(tmp_path: Path) -> None:
"""Recreating the workspace must not inherit the previous session.
Only observable where the filesystem does not immediately reuse the freed
inode. ext4 does, so this skips there rather than asserting something the
platform cannot deliver -- see ``_identity_digest`` for why stat alone
cannot close that gap, and GOOD_FIRST_ISSUES.md for the durable fix.
"""
workspace = tmp_path / "workspace"
workspace.mkdir()
first = _open(tmp_path, workspace)
before = workspace.stat().st_ino
workspace.rmdir()
workspace.mkdir()
if workspace.stat().st_ino == before:
pytest.skip("filesystem reused the freed inode; recreation is not observable via stat")
second = _open(tmp_path, workspace)
assert second.workspace_identity_digest != first.workspace_identity_digest
assert second.session_root != first.session_root
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="changed"):
first.assert_current()
def test_release_skill_layout_rejects_symlink_state_root(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
real_state = tmp_path / "real-state"
real_state.mkdir()
alias = tmp_path / "state-alias"
alias.symlink_to(real_state, target_is_directory=True)
with pytest.raises(ReleaseSkillRuntimeLayoutError, match="unsafe"):
open_release_skill_runtime_layout(
state_root=alias,
host_context_id="codex",
native_session_id="session",
workspace=workspace,
)
|