File size: 4,100 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
from __future__ import annotations

import copy
import os
import pickle
from pathlib import Path

import pytest

from ctx.runtime.query_decision import QueryHostDescriptor
from ctx.runtime.query_delivery import SensitiveQueryInput, _derive_invocation_ref
from ctx.runtime.query_session import _scope as query_scope
from ctx.runtime.release_skill_dispatcher import (
    ReleaseSkillInstallRequest,
    _scope as install_scope,
)
from ctx.runtime.workspace_identity import (
    WorkspaceIdentityError,
    capture_workspace_identity,
)


def test_workspace_identity_is_alias_stable_and_path_private(tmp_path: Path) -> None:
    workspace = tmp_path / "workspace"
    workspace.mkdir()

    first = capture_workspace_identity(workspace)
    second = capture_workspace_identity(workspace / "child" / "..")

    assert first == second
    assert len(first.digest) == 64
    assert str(workspace) not in repr(first)
    with pytest.raises(TypeError):
        copy.copy(first)
    with pytest.raises(TypeError):
        pickle.dumps(first)


def test_workspace_identity_rejects_symlink_ancestry(tmp_path: Path) -> None:
    workspace = tmp_path / "workspace"
    workspace.mkdir()
    alias = tmp_path / "alias"
    alias.symlink_to(workspace, target_is_directory=True)

    with pytest.raises(WorkspaceIdentityError, match="symlinks"):
        capture_workspace_identity(alias)


def test_workspace_identity_detects_delete_and_recreate(tmp_path: Path) -> None:
    workspace = tmp_path / "workspace"
    workspace.mkdir()
    identity = capture_workspace_identity(workspace)

    _before_ino = workspace.stat().st_ino
    workspace.rmdir()
    workspace.mkdir()
    if workspace.stat().st_ino == _before_ino:
        pytest.skip("filesystem reused the freed inode; recreation is not observable via stat")

    with pytest.raises(WorkspaceIdentityError, match="changed"):
        identity.assert_current()
    assert capture_workspace_identity(workspace).digest != identity.digest


def test_workspace_identity_detects_rename(tmp_path: Path) -> None:
    workspace = tmp_path / "workspace"
    workspace.mkdir()
    identity = capture_workspace_identity(workspace)
    workspace.rename(tmp_path / "moved")

    with pytest.raises(WorkspaceIdentityError, match="changed"):
        identity.assert_current()


@pytest.mark.skipif(os.name == "nt", reason="POSIX hard-link semantics")
def test_workspace_identity_requires_a_directory(tmp_path: Path) -> None:
    regular = tmp_path / "regular"
    regular.write_text("not a workspace", encoding="utf-8")

    with pytest.raises(WorkspaceIdentityError, match="directory"):
        capture_workspace_identity(regular)


def test_install_query_and_delivery_share_one_workspace_identity(tmp_path: Path) -> None:
    workspace = tmp_path / "workspace"
    workspace.mkdir()
    identity = capture_workspace_identity(workspace)
    request = ReleaseSkillInstallRequest(
        host_context_id="codex",
        host_identity_digest="1" * 64,
        task="repair state restoration",
        language="Python",
        session_id="native-session",
        workspace=workspace,
        journal_path=tmp_path / "engine.sqlite3",
        benefit_audit_path=tmp_path / "benefit.sqlite3",
        policy_store_root=tmp_path / "policy",
        skill_store_root=tmp_path / "skills",
        occurred_at="2026-08-02T12:00:00Z",
    )
    host = QueryHostDescriptor.codex("activate")
    sensitive = SensitiveQueryInput(
        native_session_id=request.session_id,
        logical_prompt_id="turn-2",
        workspace=workspace,
        prompt=request.task,
        language=request.language,
    )

    assert install_scope(request).workspace_id == f"workspace-{identity.digest}"
    assert (
        query_scope(
            host=host,
            session_id=request.session_id,
            workspace=workspace,
        ).workspace_id
        == f"workspace-{identity.digest}"
    )
    assert (
        _derive_invocation_ref(
            key=b"k" * 32,
            host=host,
            request=sensitive,
        ).workspace_digest
        == identity.digest
    )