ithrl 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ithrl/__init__.py +1 -0
- ithrl/__main__.py +6 -0
- ithrl/adapters/__init__.py +49 -0
- ithrl/adapters/_jsonl.py +266 -0
- ithrl/adapters/codex/__init__.py +5 -0
- ithrl/adapters/codex/context.py +204 -0
- ithrl/adapters/codex/decisions.py +224 -0
- ithrl/adapters/codex/discovery.py +60 -0
- ithrl/adapters/codex/parser.py +191 -0
- ithrl/adapters/contracts.py +106 -0
- ithrl/adapters/copilot/__init__.py +5 -0
- ithrl/adapters/copilot/context.py +182 -0
- ithrl/adapters/copilot/decisions.py +294 -0
- ithrl/adapters/copilot/discovery.py +28 -0
- ithrl/adapters/copilot/parser.py +283 -0
- ithrl/adapters/discovery.py +220 -0
- ithrl/adapters/errors.py +52 -0
- ithrl/adapters/registry.py +433 -0
- ithrl/behavior.py +138 -0
- ithrl/calibration.py +434 -0
- ithrl/case/__init__.py +33 -0
- ithrl/case/contracts.py +88 -0
- ithrl/case/evidence.py +169 -0
- ithrl/case/filesystem.py +340 -0
- ithrl/case/source.py +208 -0
- ithrl/case/source_context.py +119 -0
- ithrl/case/source_storage.py +234 -0
- ithrl/case/storage.py +377 -0
- ithrl/cli/__init__.py +968 -0
- ithrl/cli/extract.py +478 -0
- ithrl/cli/native.py +484 -0
- ithrl/cli/output.py +786 -0
- ithrl/cli/paths.py +24 -0
- ithrl/cli/sampled.py +485 -0
- ithrl/cli/sampled_output.py +472 -0
- ithrl/cli/sampled_reduction.py +1036 -0
- ithrl/cli/sampled_session.py +342 -0
- ithrl/cli/shrink.py +523 -0
- ithrl/context.py +465 -0
- ithrl/decisions.py +271 -0
- ithrl/examples/synthetic-trace.json +46 -0
- ithrl/execution/__init__.py +68 -0
- ithrl/execution/budget.py +146 -0
- ithrl/execution/contracts.py +426 -0
- ithrl/execution/preflight.py +346 -0
- ithrl/experiment.py +453 -0
- ithrl/extraction.py +206 -0
- ithrl/measurement.py +315 -0
- ithrl/observations.py +1148 -0
- ithrl/path_safety.py +100 -0
- ithrl/reduce/__init__.py +53 -0
- ithrl/reduce/contracts.py +508 -0
- ithrl/reduce/engine.py +901 -0
- ithrl/reduce/search.py +178 -0
- ithrl/results/__init__.py +49 -0
- ithrl/results/models.py +294 -0
- ithrl/results/storage.py +485 -0
- ithrl/results/summary.py +420 -0
- ithrl/results/validation.py +611 -0
- ithrl/run_state.py +1135 -0
- ithrl/runner/__init__.py +44 -0
- ithrl/runner/codex/__init__.py +17 -0
- ithrl/runner/codex/capability.py +708 -0
- ithrl/runner/codex/configuration.py +529 -0
- ithrl/runner/codex/isolation.py +396 -0
- ithrl/runner/codex/materialize.py +358 -0
- ithrl/runner/codex/policy.py +67 -0
- ithrl/runner/codex/protocol.py +649 -0
- ithrl/runner/codex/protocol_contracts.py +171 -0
- ithrl/runner/codex/runner.py +533 -0
- ithrl/runner/codex/runtime_configuration.py +264 -0
- ithrl/runner/codex/schema.py +527 -0
- ithrl/runner/codex/transcript.py +130 -0
- ithrl/runner/codex/worker.py +175 -0
- ithrl/runner/contracts.py +419 -0
- ithrl/runner/copilot/__init__.py +18 -0
- ithrl/runner/copilot/capability.py +825 -0
- ithrl/runner/copilot/configuration.py +201 -0
- ithrl/runner/copilot/isolation.py +374 -0
- ithrl/runner/copilot/materialize.py +484 -0
- ithrl/runner/copilot/policy.py +110 -0
- ithrl/runner/copilot/protocol/__init__.py +18 -0
- ithrl/runner/copilot/protocol/contracts.py +74 -0
- ithrl/runner/copilot/protocol/decoding.py +93 -0
- ithrl/runner/copilot/protocol/parser.py +418 -0
- ithrl/runner/copilot/protocol/state.py +130 -0
- ithrl/runner/copilot/runner.py +425 -0
- ithrl/runner/copilot/worker.py +351 -0
- ithrl/runner/evidence.py +345 -0
- ithrl/runner/fake.py +107 -0
- ithrl/runner/registry.py +90 -0
- ithrl/runner/runtime/__init__.py +50 -0
- ithrl/runner/runtime/contracts.py +157 -0
- ithrl/runner/runtime/output.py +184 -0
- ithrl/runner/runtime/process.py +954 -0
- ithrl/runner/runtime/protection.py +562 -0
- ithrl/runner/runtime/state.py +282 -0
- ithrl/runner/validation.py +376 -0
- ithrl/sampling.py +412 -0
- ithrl/statistics.py +276 -0
- ithrl/synthetic.py +511 -0
- ithrl/timestamps.py +26 -0
- ithrl/workflow.py +570 -0
- ithrl-0.1.0.dist-info/METADATA +118 -0
- ithrl-0.1.0.dist-info/RECORD +108 -0
- ithrl-0.1.0.dist-info/WHEEL +4 -0
- ithrl-0.1.0.dist-info/entry_points.txt +2 -0
- ithrl-0.1.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"""Resolve private GitHub Copilot authentication for an isolated attempt."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from enum import StrEnum
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from types import MappingProxyType
|
|
10
|
+
from typing import Literal
|
|
11
|
+
|
|
12
|
+
from ithrl.case import CaseFormatError
|
|
13
|
+
from ithrl.case.filesystem import read_safe_bytes
|
|
14
|
+
from ithrl.context import content_digest
|
|
15
|
+
|
|
16
|
+
MAX_CONFIG_BYTES = 1024 * 1024
|
|
17
|
+
AUTH_ENVIRONMENT_NAMES = (
|
|
18
|
+
"COPILOT_GITHUB_TOKEN",
|
|
19
|
+
"GH_TOKEN",
|
|
20
|
+
"GITHUB_TOKEN",
|
|
21
|
+
)
|
|
22
|
+
# Forward only connectivity settings required by the Node-based CLI. Inheriting the
|
|
23
|
+
# complete parent environment could expose unrelated credentials to the harness.
|
|
24
|
+
PASSTHROUGH_ENVIRONMENT_NAMES = (
|
|
25
|
+
"HTTP_PROXY",
|
|
26
|
+
"HTTPS_PROXY",
|
|
27
|
+
"NO_PROXY",
|
|
28
|
+
"http_proxy",
|
|
29
|
+
"https_proxy",
|
|
30
|
+
"no_proxy",
|
|
31
|
+
"SSL_CERT_FILE",
|
|
32
|
+
"SSL_CERT_DIR",
|
|
33
|
+
"NODE_EXTRA_CA_CERTS",
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
type CopilotAuthentication = Literal[
|
|
37
|
+
"copilot-github-token",
|
|
38
|
+
"gh-token",
|
|
39
|
+
"github-token",
|
|
40
|
+
"managed-config",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
_AUTHENTICATION_BY_ENVIRONMENT: dict[str, CopilotAuthentication] = {
|
|
44
|
+
"COPILOT_GITHUB_TOKEN": "copilot-github-token",
|
|
45
|
+
"GH_TOKEN": "gh-token",
|
|
46
|
+
"GITHUB_TOKEN": "github-token",
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
_ISOLATED_SETTINGS = b'{"askUser":false,"autoUpdate":false,"remote":"off","remoteExport":false}'
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class CopilotConfigurationErrorCode(StrEnum):
|
|
53
|
+
"""Stable refusal categories for private Copilot configuration."""
|
|
54
|
+
|
|
55
|
+
AUTHENTICATION_UNAVAILABLE = "authentication-unavailable"
|
|
56
|
+
MALFORMED_AUTHENTICATION = "malformed-authentication"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class CopilotConfigurationError(RuntimeError):
|
|
60
|
+
"""Copilot authentication cannot be isolated safely."""
|
|
61
|
+
|
|
62
|
+
def __init__(self, code: CopilotConfigurationErrorCode, message: str) -> None:
|
|
63
|
+
super().__init__(message)
|
|
64
|
+
self.code = code
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@dataclass(frozen=True, slots=True)
|
|
68
|
+
class CopilotConfiguration:
|
|
69
|
+
"""Private authentication and fixed settings for one Copilot runtime."""
|
|
70
|
+
|
|
71
|
+
authentication: CopilotAuthentication
|
|
72
|
+
environment_names: tuple[str, ...]
|
|
73
|
+
environment: Mapping[str, str] = field(repr=False, compare=False)
|
|
74
|
+
config_data: bytes | None = field(default=None, repr=False, compare=False)
|
|
75
|
+
settings_data: bytes = field(
|
|
76
|
+
default=_ISOLATED_SETTINGS,
|
|
77
|
+
repr=False,
|
|
78
|
+
compare=False,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
def __post_init__(self) -> None:
|
|
82
|
+
auth_names = _validate_environment(self.environment_names, self.environment)
|
|
83
|
+
_validate_authentication(self.authentication, auth_names, self.config_data)
|
|
84
|
+
_validate_settings(self.settings_data)
|
|
85
|
+
object.__setattr__(self, "environment", MappingProxyType(dict(self.environment)))
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def public_digest(self) -> str:
|
|
89
|
+
"""Return a stable identity that contains no authentication material."""
|
|
90
|
+
|
|
91
|
+
return content_digest(
|
|
92
|
+
{
|
|
93
|
+
"authentication": self.authentication,
|
|
94
|
+
"environment_names": list(self.environment_names),
|
|
95
|
+
"isolated_settings": self.settings_data.decode("utf-8"),
|
|
96
|
+
}
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def resolve_copilot_configuration(
|
|
101
|
+
*,
|
|
102
|
+
environment: Mapping[str, str],
|
|
103
|
+
config_file: Path | None,
|
|
104
|
+
) -> CopilotConfiguration:
|
|
105
|
+
"""Select documented token precedence or one bounded managed config file."""
|
|
106
|
+
|
|
107
|
+
for name in AUTH_ENVIRONMENT_NAMES:
|
|
108
|
+
value = environment.get(name)
|
|
109
|
+
if value:
|
|
110
|
+
copied = _connectivity_environment(environment)
|
|
111
|
+
copied[name] = value
|
|
112
|
+
return CopilotConfiguration(
|
|
113
|
+
authentication=_AUTHENTICATION_BY_ENVIRONMENT[name],
|
|
114
|
+
environment_names=tuple(copied),
|
|
115
|
+
environment=MappingProxyType(copied),
|
|
116
|
+
)
|
|
117
|
+
if config_file is None:
|
|
118
|
+
raise CopilotConfigurationError(
|
|
119
|
+
CopilotConfigurationErrorCode.AUTHENTICATION_UNAVAILABLE,
|
|
120
|
+
"Copilot authentication is unavailable",
|
|
121
|
+
)
|
|
122
|
+
connectivity = _connectivity_environment(environment)
|
|
123
|
+
return CopilotConfiguration(
|
|
124
|
+
authentication="managed-config",
|
|
125
|
+
environment_names=tuple(connectivity),
|
|
126
|
+
environment=MappingProxyType(connectivity),
|
|
127
|
+
config_data=_read_managed_config(config_file),
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _connectivity_environment(environment: Mapping[str, str]) -> dict[str, str]:
|
|
132
|
+
return {
|
|
133
|
+
name: value for name in PASSTHROUGH_ENVIRONMENT_NAMES if (value := environment.get(name))
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _validate_environment(
|
|
138
|
+
names: tuple[str, ...],
|
|
139
|
+
environment: Mapping[str, str],
|
|
140
|
+
) -> tuple[str, ...]:
|
|
141
|
+
if tuple(environment) != names:
|
|
142
|
+
raise ValueError("Copilot environment does not match its declaration")
|
|
143
|
+
if any(not value for value in environment.values()):
|
|
144
|
+
raise ValueError("Copilot environment contains an empty value")
|
|
145
|
+
allowed_names = set(PASSTHROUGH_ENVIRONMENT_NAMES) | set(AUTH_ENVIRONMENT_NAMES)
|
|
146
|
+
if any(name not in allowed_names for name in names):
|
|
147
|
+
raise ValueError("Copilot environment contains an unsupported name")
|
|
148
|
+
auth_names = tuple(name for name in names if name in AUTH_ENVIRONMENT_NAMES)
|
|
149
|
+
if len(auth_names) > 1:
|
|
150
|
+
raise ValueError("Copilot authentication must use one credential source")
|
|
151
|
+
return auth_names
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _validate_authentication(
|
|
155
|
+
authentication: CopilotAuthentication,
|
|
156
|
+
auth_names: tuple[str, ...],
|
|
157
|
+
config_data: bytes | None,
|
|
158
|
+
) -> None:
|
|
159
|
+
has_file = config_data is not None
|
|
160
|
+
if has_file == bool(auth_names):
|
|
161
|
+
raise ValueError("Copilot authentication must use exactly one private source")
|
|
162
|
+
if has_file != (authentication == "managed-config"):
|
|
163
|
+
raise ValueError("Copilot authentication method does not match its private source")
|
|
164
|
+
if auth_names and _AUTHENTICATION_BY_ENVIRONMENT[auth_names[0]] != authentication:
|
|
165
|
+
raise ValueError("Copilot authentication method does not match its private source")
|
|
166
|
+
if config_data == b"":
|
|
167
|
+
raise ValueError("Copilot managed configuration cannot be empty")
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _validate_settings(settings_data: bytes) -> None:
|
|
171
|
+
if settings_data != _ISOLATED_SETTINGS:
|
|
172
|
+
raise ValueError("Copilot isolated settings are fixed by the runner policy")
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _read_managed_config(path: Path) -> bytes:
|
|
176
|
+
try:
|
|
177
|
+
content = read_safe_bytes(
|
|
178
|
+
path,
|
|
179
|
+
max_bytes=MAX_CONFIG_BYTES,
|
|
180
|
+
label="Copilot managed configuration",
|
|
181
|
+
)
|
|
182
|
+
if not content:
|
|
183
|
+
raise ValueError
|
|
184
|
+
except (CaseFormatError, ValueError) as error:
|
|
185
|
+
raise CopilotConfigurationError(
|
|
186
|
+
CopilotConfigurationErrorCode.MALFORMED_AUTHENTICATION,
|
|
187
|
+
"Copilot managed authentication is invalid",
|
|
188
|
+
) from error
|
|
189
|
+
return content
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
__all__ = [
|
|
193
|
+
"AUTH_ENVIRONMENT_NAMES",
|
|
194
|
+
"MAX_CONFIG_BYTES",
|
|
195
|
+
"PASSTHROUGH_ENVIRONMENT_NAMES",
|
|
196
|
+
"CopilotAuthentication",
|
|
197
|
+
"CopilotConfiguration",
|
|
198
|
+
"CopilotConfigurationError",
|
|
199
|
+
"CopilotConfigurationErrorCode",
|
|
200
|
+
"resolve_copilot_configuration",
|
|
201
|
+
]
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
"""Prepare a GitHub Copilot worker inside a restricted Bubblewrap process."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import stat
|
|
9
|
+
from collections.abc import Mapping
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
from ithrl.case import CaseError
|
|
14
|
+
from ithrl.case.filesystem import atomic_write_bytes
|
|
15
|
+
from ithrl.context import content_digest
|
|
16
|
+
from ithrl.runner.copilot.configuration import CopilotConfiguration
|
|
17
|
+
from ithrl.runner.copilot.policy import CopilotTarget, target_policy
|
|
18
|
+
from ithrl.runner.copilot.protocol import is_denied_tool_completion
|
|
19
|
+
from ithrl.runner.runtime import OwnedAttemptState, ProcessSpec
|
|
20
|
+
|
|
21
|
+
SANDBOX_ROOT = "/tmp/ithrl-state" # noqa: S108 - private tmpfs path inside Bubblewrap
|
|
22
|
+
MAX_SEMANTIC_SESSION_BYTES = 64 * 1024 * 1024
|
|
23
|
+
# Bundled tooling is extracted into each isolated cache before even --version.
|
|
24
|
+
# Reserve room for those assets as well as the bounded semantic session and output.
|
|
25
|
+
DEFAULT_DISPOSABLE_STATE_BYTES = 256 * 1024 * 1024
|
|
26
|
+
MAX_TRIGGER_BYTES = 1024 * 1024
|
|
27
|
+
_MAX_WORKER_BYTES = 1024 * 1024
|
|
28
|
+
_MAX_MODEL_BYTES = 1024
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class CopilotIsolationError(RuntimeError):
|
|
32
|
+
"""A preventive Copilot process boundary cannot be prepared."""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True, slots=True)
|
|
36
|
+
class CopilotRuntime:
|
|
37
|
+
"""Content-bound Copilot and Bubblewrap executables."""
|
|
38
|
+
|
|
39
|
+
copilot: Path = field(repr=False)
|
|
40
|
+
bubblewrap: Path = field(repr=False)
|
|
41
|
+
_digests: tuple[str, str] = field(init=False, repr=False)
|
|
42
|
+
|
|
43
|
+
def __post_init__(self) -> None:
|
|
44
|
+
programs = tuple(_read_executable(path) for path in (self.copilot, self.bubblewrap))
|
|
45
|
+
object.__setattr__(self, "copilot", programs[0][0])
|
|
46
|
+
object.__setattr__(self, "bubblewrap", programs[1][0])
|
|
47
|
+
object.__setattr__(self, "_digests", (programs[0][1], programs[1][1]))
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def identity(self) -> str:
|
|
51
|
+
"""Return a credential-free identity for the selected executables."""
|
|
52
|
+
|
|
53
|
+
return content_digest(
|
|
54
|
+
{
|
|
55
|
+
"bubblewrap": self._digests[1],
|
|
56
|
+
"copilot": self._digests[0],
|
|
57
|
+
}
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
def assert_unchanged(self) -> None:
|
|
61
|
+
"""Refuse when either selected executable changed after detection."""
|
|
62
|
+
|
|
63
|
+
current = tuple(_read_executable(path)[1] for path in (self.copilot, self.bubblewrap))
|
|
64
|
+
if current != self._digests:
|
|
65
|
+
raise CopilotIsolationError("a required Copilot runtime executable changed")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def prepare_copilot_process(
|
|
69
|
+
state: OwnedAttemptState,
|
|
70
|
+
*,
|
|
71
|
+
runtime: CopilotRuntime,
|
|
72
|
+
configuration: CopilotConfiguration,
|
|
73
|
+
semantic_session: bytes,
|
|
74
|
+
trigger: str,
|
|
75
|
+
model: str,
|
|
76
|
+
target: CopilotTarget,
|
|
77
|
+
worker_source: Path,
|
|
78
|
+
worker_digest: str,
|
|
79
|
+
) -> ProcessSpec:
|
|
80
|
+
"""Write private worker inputs and return one direct Bubblewrap invocation."""
|
|
81
|
+
|
|
82
|
+
state.assert_unchanged()
|
|
83
|
+
runtime.assert_unchanged()
|
|
84
|
+
_validate_private_input(semantic_session, trigger=trigger, model=model)
|
|
85
|
+
policy = target_policy(target)
|
|
86
|
+
|
|
87
|
+
workspace = _private_directory(state.root / "workspace")
|
|
88
|
+
copilot_home = _private_directory(state.root / "copilot-home")
|
|
89
|
+
_private_directory(state.root / "copilot-cache")
|
|
90
|
+
_private_directory(state.root / "home")
|
|
91
|
+
_private_directory(state.root / "runner")
|
|
92
|
+
worker = _copy_private_program(
|
|
93
|
+
worker_source,
|
|
94
|
+
state.root / "worker",
|
|
95
|
+
expected_digest=worker_digest,
|
|
96
|
+
)
|
|
97
|
+
request = _worker_request(
|
|
98
|
+
trigger=trigger,
|
|
99
|
+
model=model,
|
|
100
|
+
target=target,
|
|
101
|
+
command_flags=policy.command_flags,
|
|
102
|
+
secret_environment_names=configuration.environment_names,
|
|
103
|
+
)
|
|
104
|
+
try:
|
|
105
|
+
atomic_write_bytes(state.root / "session.jsonl", semantic_session)
|
|
106
|
+
atomic_write_bytes(state.root / "request.json", request)
|
|
107
|
+
atomic_write_bytes(copilot_home / "settings.json", configuration.settings_data)
|
|
108
|
+
if configuration.config_data is not None:
|
|
109
|
+
atomic_write_bytes(copilot_home / "config.json", configuration.config_data)
|
|
110
|
+
except CaseError as error:
|
|
111
|
+
raise CopilotIsolationError("cannot prepare private Copilot attempt state") from error
|
|
112
|
+
|
|
113
|
+
return ProcessSpec(
|
|
114
|
+
argv=_bubblewrap_argv(runtime, state=state.root, worker=worker.name),
|
|
115
|
+
cwd=workspace,
|
|
116
|
+
environment=_worker_environment(configuration.environment),
|
|
117
|
+
stop_after_stdout_line=(is_denied_tool_completion if target == "shell-action" else None),
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _validate_private_input(semantic_session: bytes, *, trigger: str, model: str) -> None:
|
|
122
|
+
if not semantic_session or len(semantic_session) > MAX_SEMANTIC_SESSION_BYTES:
|
|
123
|
+
raise CopilotIsolationError("Copilot semantic session exceeds its byte limit")
|
|
124
|
+
try:
|
|
125
|
+
trigger_size = len(trigger.encode("utf-8"))
|
|
126
|
+
model_size = len(model.encode("utf-8"))
|
|
127
|
+
except UnicodeError as error:
|
|
128
|
+
raise CopilotIsolationError("Copilot worker input is invalid") from error
|
|
129
|
+
if not trigger or trigger_size > MAX_TRIGGER_BYTES:
|
|
130
|
+
raise CopilotIsolationError("Copilot trigger exceeds its byte limit")
|
|
131
|
+
if not model or model_size > _MAX_MODEL_BYTES or "\0" in model:
|
|
132
|
+
raise CopilotIsolationError("Copilot model identity is invalid")
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _worker_request(
|
|
136
|
+
*,
|
|
137
|
+
trigger: str,
|
|
138
|
+
model: str,
|
|
139
|
+
target: CopilotTarget,
|
|
140
|
+
command_flags: tuple[str, ...],
|
|
141
|
+
secret_environment_names: tuple[str, ...],
|
|
142
|
+
) -> bytes:
|
|
143
|
+
flags = list(command_flags)
|
|
144
|
+
if secret_environment_names:
|
|
145
|
+
flags.append(f"--secret-env-vars={','.join(secret_environment_names)}")
|
|
146
|
+
document = {
|
|
147
|
+
"commandFlags": flags,
|
|
148
|
+
"model": model,
|
|
149
|
+
"semanticSession": f"{SANDBOX_ROOT}/session.jsonl",
|
|
150
|
+
"target": target,
|
|
151
|
+
"trigger": trigger,
|
|
152
|
+
"workspace": f"{SANDBOX_ROOT}/workspace",
|
|
153
|
+
}
|
|
154
|
+
try:
|
|
155
|
+
return json.dumps(
|
|
156
|
+
document,
|
|
157
|
+
allow_nan=False,
|
|
158
|
+
ensure_ascii=False,
|
|
159
|
+
separators=(",", ":"),
|
|
160
|
+
).encode("utf-8")
|
|
161
|
+
except (TypeError, UnicodeError, ValueError) as error:
|
|
162
|
+
raise CopilotIsolationError("Copilot worker input is invalid") from error
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _bubblewrap_argv(
|
|
166
|
+
runtime: CopilotRuntime,
|
|
167
|
+
*,
|
|
168
|
+
state: Path,
|
|
169
|
+
worker: str,
|
|
170
|
+
network_denied: bool = False,
|
|
171
|
+
) -> tuple[str, ...]:
|
|
172
|
+
return (
|
|
173
|
+
*_boundary_argv(runtime, state=state, network_denied=network_denied),
|
|
174
|
+
f"{SANDBOX_ROOT}/{worker}",
|
|
175
|
+
f"{SANDBOX_ROOT}/request.json",
|
|
176
|
+
f"{SANDBOX_ROOT}/runner/copilot",
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _discovery_argv(
|
|
181
|
+
runtime: CopilotRuntime,
|
|
182
|
+
*,
|
|
183
|
+
state: Path,
|
|
184
|
+
arguments: tuple[str, ...],
|
|
185
|
+
) -> tuple[str, ...]:
|
|
186
|
+
"""Run a structural command without writable host state or network access."""
|
|
187
|
+
|
|
188
|
+
runtime.assert_unchanged()
|
|
189
|
+
return (
|
|
190
|
+
*_boundary_argv(runtime, state=state, network_denied=True),
|
|
191
|
+
f"{SANDBOX_ROOT}/runner/copilot",
|
|
192
|
+
*arguments,
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _boundary_argv(
|
|
197
|
+
runtime: CopilotRuntime,
|
|
198
|
+
*,
|
|
199
|
+
state: Path,
|
|
200
|
+
network_denied: bool,
|
|
201
|
+
) -> tuple[str, ...]:
|
|
202
|
+
copilot = runtime.copilot.resolve(strict=True)
|
|
203
|
+
return (
|
|
204
|
+
os.fspath(runtime.bubblewrap.resolve(strict=True)),
|
|
205
|
+
"--die-with-parent",
|
|
206
|
+
"--new-session",
|
|
207
|
+
"--unshare-pid",
|
|
208
|
+
"--unshare-ipc",
|
|
209
|
+
"--unshare-uts",
|
|
210
|
+
*(("--unshare-net",) if network_denied else ()),
|
|
211
|
+
"--ro-bind",
|
|
212
|
+
"/",
|
|
213
|
+
"/",
|
|
214
|
+
"--tmpfs",
|
|
215
|
+
"/home",
|
|
216
|
+
*(("--tmpfs", "/root") if network_denied else ()),
|
|
217
|
+
"--tmpfs",
|
|
218
|
+
"/tmp", # noqa: S108 - private tmpfs inside Bubblewrap
|
|
219
|
+
"--tmpfs",
|
|
220
|
+
"/run",
|
|
221
|
+
"--proc",
|
|
222
|
+
"/proc",
|
|
223
|
+
"--dev",
|
|
224
|
+
"/dev",
|
|
225
|
+
"--dir",
|
|
226
|
+
SANDBOX_ROOT,
|
|
227
|
+
"--bind",
|
|
228
|
+
os.fspath(state),
|
|
229
|
+
SANDBOX_ROOT,
|
|
230
|
+
"--ro-bind",
|
|
231
|
+
os.fspath(copilot),
|
|
232
|
+
f"{SANDBOX_ROOT}/runner/copilot",
|
|
233
|
+
*(
|
|
234
|
+
(
|
|
235
|
+
"--remount-ro",
|
|
236
|
+
"/home",
|
|
237
|
+
"--remount-ro",
|
|
238
|
+
"/root",
|
|
239
|
+
"--remount-ro",
|
|
240
|
+
"/tmp", # noqa: S108 - private tmpfs inside Bubblewrap
|
|
241
|
+
"--remount-ro",
|
|
242
|
+
"/run",
|
|
243
|
+
)
|
|
244
|
+
if network_denied
|
|
245
|
+
else ()
|
|
246
|
+
),
|
|
247
|
+
"--chdir",
|
|
248
|
+
f"{SANDBOX_ROOT}/workspace",
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _worker_environment(supplied: Mapping[str, str]) -> dict[str, str]:
|
|
253
|
+
environment = dict(supplied)
|
|
254
|
+
environment.update(
|
|
255
|
+
{
|
|
256
|
+
"COPILOT_AUTO_UPDATE": "false",
|
|
257
|
+
"COPILOT_CACHE_HOME": f"{SANDBOX_ROOT}/copilot-cache",
|
|
258
|
+
"COPILOT_HOME": f"{SANDBOX_ROOT}/copilot-home",
|
|
259
|
+
"HOME": f"{SANDBOX_ROOT}/home",
|
|
260
|
+
"LANG": "C.UTF-8",
|
|
261
|
+
"LC_ALL": "C.UTF-8",
|
|
262
|
+
"NO_COLOR": "1",
|
|
263
|
+
"PATH": "/usr/bin:/bin",
|
|
264
|
+
}
|
|
265
|
+
)
|
|
266
|
+
return environment
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _private_directory(path: Path) -> Path:
|
|
270
|
+
try:
|
|
271
|
+
path.mkdir(mode=0o700)
|
|
272
|
+
metadata = path.lstat()
|
|
273
|
+
except OSError as error:
|
|
274
|
+
raise CopilotIsolationError("cannot prepare private Copilot attempt state") from error
|
|
275
|
+
if not stat.S_ISDIR(metadata.st_mode) or stat.S_ISLNK(metadata.st_mode):
|
|
276
|
+
raise CopilotIsolationError("Copilot attempt state contains an unsafe directory")
|
|
277
|
+
return path
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def program_digest(source: Path) -> str:
|
|
281
|
+
"""Return the content identity of one bounded Copilot worker."""
|
|
282
|
+
|
|
283
|
+
_, content = _read_program(source)
|
|
284
|
+
return hashlib.sha256(content).hexdigest()
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def _copy_private_program(source: Path, destination: Path, *, expected_digest: str) -> Path:
|
|
288
|
+
try:
|
|
289
|
+
_, content = _read_program(source)
|
|
290
|
+
if hashlib.sha256(content).hexdigest() != expected_digest:
|
|
291
|
+
raise CopilotIsolationError("the Copilot worker source changed")
|
|
292
|
+
atomic_write_bytes(destination, content)
|
|
293
|
+
destination.chmod(0o500)
|
|
294
|
+
except CopilotIsolationError:
|
|
295
|
+
raise
|
|
296
|
+
except (CaseError, OSError) as error:
|
|
297
|
+
raise CopilotIsolationError("cannot prepare the private Copilot worker") from error
|
|
298
|
+
return destination
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def _read_program(source: Path) -> tuple[Path, bytes]:
|
|
302
|
+
try:
|
|
303
|
+
resolved = source.resolve(strict=True)
|
|
304
|
+
metadata = resolved.stat()
|
|
305
|
+
if not stat.S_ISREG(metadata.st_mode) or metadata.st_size > _MAX_WORKER_BYTES:
|
|
306
|
+
raise CopilotIsolationError("the Copilot worker source is invalid")
|
|
307
|
+
with resolved.open("rb") as stream:
|
|
308
|
+
opened = os.fstat(stream.fileno())
|
|
309
|
+
if _file_state(opened) != _file_state(metadata):
|
|
310
|
+
raise CopilotIsolationError("the Copilot worker source changed while opening")
|
|
311
|
+
content = stream.read(_MAX_WORKER_BYTES + 1)
|
|
312
|
+
finished = os.fstat(stream.fileno())
|
|
313
|
+
if (
|
|
314
|
+
len(content) != metadata.st_size
|
|
315
|
+
or _file_state(finished) != _file_state(metadata)
|
|
316
|
+
or _file_state(resolved.stat()) != _file_state(metadata)
|
|
317
|
+
):
|
|
318
|
+
raise CopilotIsolationError("the Copilot worker source changed while it was read")
|
|
319
|
+
except CopilotIsolationError:
|
|
320
|
+
raise
|
|
321
|
+
except OSError as error:
|
|
322
|
+
raise CopilotIsolationError("cannot read the private Copilot worker") from error
|
|
323
|
+
return resolved, content
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def _read_executable(path: Path) -> tuple[Path, str]:
|
|
327
|
+
if not isinstance(path, Path) or not path.is_absolute() or ".." in path.parts:
|
|
328
|
+
raise ValueError("Copilot runtime paths must be absolute")
|
|
329
|
+
try:
|
|
330
|
+
target = path.resolve(strict=True)
|
|
331
|
+
metadata = target.stat()
|
|
332
|
+
if not stat.S_ISREG(metadata.st_mode) or metadata.st_mode & 0o111 == 0:
|
|
333
|
+
raise OSError
|
|
334
|
+
digest = hashlib.sha256()
|
|
335
|
+
with target.open("rb") as stream:
|
|
336
|
+
opened = os.fstat(stream.fileno())
|
|
337
|
+
if _file_state(opened) != _file_state(metadata):
|
|
338
|
+
raise CopilotIsolationError("a required Copilot runtime executable changed")
|
|
339
|
+
while chunk := stream.read(1024 * 1024):
|
|
340
|
+
digest.update(chunk)
|
|
341
|
+
finished = os.fstat(stream.fileno())
|
|
342
|
+
if _file_state(finished) != _file_state(metadata) or _file_state(
|
|
343
|
+
target.stat()
|
|
344
|
+
) != _file_state(metadata):
|
|
345
|
+
raise CopilotIsolationError("a required Copilot runtime executable changed")
|
|
346
|
+
except CopilotIsolationError:
|
|
347
|
+
raise
|
|
348
|
+
except OSError as error:
|
|
349
|
+
raise CopilotIsolationError(
|
|
350
|
+
"a required Copilot runtime executable is unavailable"
|
|
351
|
+
) from error
|
|
352
|
+
return target, digest.hexdigest()
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def _file_state(metadata: os.stat_result) -> tuple[int, int, int, int, int, int]:
|
|
356
|
+
return (
|
|
357
|
+
metadata.st_dev,
|
|
358
|
+
metadata.st_ino,
|
|
359
|
+
metadata.st_mode,
|
|
360
|
+
metadata.st_size,
|
|
361
|
+
metadata.st_mtime_ns,
|
|
362
|
+
metadata.st_ctime_ns,
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
__all__ = [
|
|
367
|
+
"MAX_SEMANTIC_SESSION_BYTES",
|
|
368
|
+
"MAX_TRIGGER_BYTES",
|
|
369
|
+
"SANDBOX_ROOT",
|
|
370
|
+
"CopilotIsolationError",
|
|
371
|
+
"CopilotRuntime",
|
|
372
|
+
"prepare_copilot_process",
|
|
373
|
+
"program_digest",
|
|
374
|
+
]
|