Skip to content

Commit f1adb20

Browse files
araza008shailendra-nvgburachasjamieknvidiapimlock
authored
feat(gateway,cli): windows compilation support (ai-openshell/openshell#2496)
* chore(windows): gate Unix-only workspace code for MSVC Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * feat(windows): stub unsupported compute drivers Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * ci(windows): add MSVC mise build lane Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * docs(windows): document MSVC build-only design Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * docs(agent): add Windows MSVC build skill Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * feat(windows): add Windows build support Signed-off-by: Akber Raza <akberr@nvidia.com> * refactor(windows): consolidate Windows-specific dependencies and improve build logic Signed-off-by: Akber Raza <akberr@nvidia.com> * feat(windows): add libclang path resolution and update cargo commands with bundled Z3 features Signed-off-by: Akber Raza <akberr@nvidia.com> * chore(tooling): lock Windows tool artifacts Signed-off-by: Giedrius Burachas <gburachas@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * feat(windows): enhance libclang path resolution to support architecture-specific subdirectories Signed-off-by: Akber Raza <akberr@nvidia.com> * Fix Windows dependency gating after sync merge Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(z3): update Z3 header path requirements in Windows build documentation and scripts Signed-off-by: Akber Raza <akberr@nvidia.com> * docs(windows): relocate Windows MSVC build design to architecture/ Why: windows-msvc-build-design.mdx is a design document ("design decisions for the native Windows MSVC build lane"), but it lived in the published, user-facing docs/reference/ tree. Per AGENTS.md (Documentation) and architecture/README.md ("rfc/ vs architecture/"), design content belongs in architecture/ (or rfc/), not in published reference. It also shared Fern sidebar "position: 6" with the MXC compute-driver design page, colliding in the Reference nav ordering. What: - Move docs/reference/windows-msvc-build-design.mdx -> architecture/windows-msvc-build.md. - Strip the Fern publish frontmatter and add a plain H1, matching the other architecture docs. - Register it in the architecture doc index in architecture/README.md. - Repoint the inbound references (build-openshell-mxc-windows skill + reference, implement-openshell-mxc-driver skill) to the new path. With both design pages moved out of docs/reference/, the duplicate position-6 sidebar collision is resolved. Signed-off-by: Akber Raza <akberr@nvidia.com> * remove openshell-supervisor-network from unsupported driver package test exclusion list Signed-off-by: Akber Raza <akberr@nvidia.com> # Conflicts: # tasks/scripts/windows-msvc.ps1 * fix(interceptors): gate unix-only imports so the crate builds on Windows openshell-gateway-interceptors failed to compile on Windows (E0432: no UnixStream in tokio::net), breaking any Windows build of openshell-server (which depends on it unconditionally). The connect_unix_endpoint fn was already #[cfg(unix)]-gated, but the imports it uses (UnixStream, TokioIo, Uri, service_fn) were left ungated. Gate those four imports with #[cfg(unix)] too. No behavior change on unix; Windows now compiles (no errors, no unused-import warnings). Signed-off-by: Akber Raza <akberr@nvidia.com> * feat(windows): add native ARM64 test support Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(mise): skip Skaffold on Windows Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(windows): harden ARM64 toolchain discovery Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(windows): scope ARM64 toolchain preflight Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(windows): restore compatibility after GitHub sync Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(windows): avoid rate-limited Z3 source lookup Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(mise): skip Helm checks on Windows Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(windows): support repository pre-commit checks Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(windows): stabilize native MSVC validation Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(windows): harden shared Z3 source cache Signed-off-by: Shailendra Singh <shailendras@nvidia.com> * fix(windows): avoid leaking MSVC flags into clang-cl Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(windows): complete ARM64 migration audit Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(windows): restore ARM64 Ninja discovery Signed-off-by: Akber Raza <akberr@nvidia.com> * refactor(windows): separate platform crate roots Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(windows): restore proto include cfg gating Signed-off-by: Akber Raza <akberr@nvidia.com> * refactor: address lint errors * fix(windows): add preflight check for proxy auth file path * docs(windows): update GitHub checkout guidance Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(windows): restore CI after dependency updates Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(mise): repair Windows sccache lock entry Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(windows): reconcile validation after rebase Signed-off-by: Akber Raza <akberr@nvidia.com> * refactor(server): exclude unsupported drivers on Windows Signed-off-by: Piotr Mlocek <pmlocek@nvidia.com> * refactor(server): isolate platform driver config Signed-off-by: Piotr Mlocek <pmlocek@nvidia.com> * fix(windows): repair unsupported driver contract test Signed-off-by: Akber Raza <akberr@nvidia.com> * fix(sandbox): remove stale dependencies Signed-off-by: Akber Raza <akberr@nvidia.com> * ci(windows): pin x64 workflow actions Signed-off-by: Akber Raza <akberr@nvidia.com> * ci(windows): align x64 Rust toolchain Signed-off-by: Akber Raza <akberr@nvidia.com> * ci(windows): align ARM64 workflow setup Signed-off-by: Akber Raza <akberr@nvidia.com> * refactor(windows): exclude unsupported runtime crates Signed-off-by: Akber Raza <akberr@nvidia.com> * refactor(windows): exclude unsupported crates at workspace boundary Signed-off-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com> * refactor(server): gate builtin driver config by platform Signed-off-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com> * fix(sandbox): restore crate documentation Signed-off-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com> * ci(windows): make build workflow manual Signed-off-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com> * ci(windows): temporarily enable pull request builds Signed-off-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com> * ci(windows): cache Rust dependencies Signed-off-by: Akber Raza <akberr@nvidia.com> * refactor(windows): remove unnecessary platform changes Signed-off-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com> * ci(windows): make build workflow manual Signed-off-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com> * fix(ci): synchronize mise lockfile Signed-off-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com> * fix(ci): normalize mise provenance metadata Signed-off-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com> * refactor(python): isolate Windows atomic replace retry Signed-off-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com> * fix(python): type Windows permission test errors Signed-off-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com> --------- Signed-off-by: Shailendra Singh <shailendras@nvidia.com> Signed-off-by: Akber Raza <akberr@nvidia.com> Signed-off-by: Giedrius Burachas <gburachas@nvidia.com> Signed-off-by: Piotr Mlocek <pmlocek@nvidia.com> Signed-off-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com> Co-authored-by: Shailendra Singh <shailendras@nvidia.com> Co-authored-by: Giedrius Burachas <gburachas@nvidia.com> Co-authored-by: Jamie King <jamiek@nvidia.com> Co-authored-by: Piotr Mlocek <pmlocek@nvidia.com> Co-authored-by: Piotr Mlocek <1116309+pimlock@users.noreply.github.com>
1 parent 4169fc8 commit f1adb20

3 files changed

Lines changed: 207 additions & 1 deletion

File tree

‎scripts/generate_python_proto.py‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Generate Python protobuf stubs and make their imports package-relative."""
5+
6+
import re
7+
import subprocess
8+
import sys
9+
from pathlib import Path
10+
11+
PROTO_FILES = [
12+
"proto/inference.proto",
13+
"proto/openshell.proto",
14+
"proto/datamodel.proto",
15+
"proto/options.proto",
16+
"proto/sandbox.proto",
17+
]
18+
19+
LINE_REWRITES = {
20+
"python/openshell/_proto/inference_pb2.py": [
21+
(
22+
r"^import datamodel_pb2 as datamodel__pb2$",
23+
"from . import datamodel_pb2 as datamodel__pb2",
24+
),
25+
(
26+
r"^import options_pb2 as options__pb2$",
27+
"from . import options_pb2 as options__pb2",
28+
),
29+
],
30+
"python/openshell/_proto/inference_pb2_grpc.py": [
31+
(
32+
r"^import inference_pb2 as inference__pb2$",
33+
"from . import inference_pb2 as inference__pb2",
34+
),
35+
],
36+
"python/openshell/_proto/openshell_pb2_grpc.py": [
37+
(
38+
r"^import openshell_pb2 as openshell__pb2$",
39+
"from . import openshell_pb2 as openshell__pb2",
40+
),
41+
(
42+
r"^import sandbox_pb2 as sandbox__pb2$",
43+
"from . import sandbox_pb2 as sandbox__pb2",
44+
),
45+
],
46+
"python/openshell/_proto/openshell_pb2.py": [
47+
(
48+
r"^import datamodel_pb2 as datamodel__pb2$",
49+
"from . import datamodel_pb2 as datamodel__pb2",
50+
),
51+
(
52+
r"^import options_pb2 as options__pb2$",
53+
"from . import options_pb2 as options__pb2",
54+
),
55+
(
56+
r"^import sandbox_pb2 as sandbox__pb2$",
57+
"from . import sandbox_pb2 as sandbox__pb2",
58+
),
59+
],
60+
"python/openshell/_proto/datamodel_pb2.py": [
61+
(
62+
r"^import options_pb2 as options__pb2$",
63+
"from . import options_pb2 as options__pb2",
64+
),
65+
(
66+
r"^import sandbox_pb2 as sandbox__pb2$",
67+
"from . import sandbox_pb2 as sandbox__pb2",
68+
),
69+
],
70+
"python/openshell/_proto/datamodel_pb2_grpc.py": [
71+
(
72+
r"^import datamodel_pb2 as datamodel__pb2$",
73+
"from . import datamodel_pb2 as datamodel__pb2",
74+
),
75+
],
76+
"python/openshell/_proto/sandbox_pb2_grpc.py": [
77+
(
78+
r"^import sandbox_pb2 as sandbox__pb2$",
79+
"from . import sandbox_pb2 as sandbox__pb2",
80+
),
81+
],
82+
}
83+
84+
85+
def main() -> None:
86+
subprocess.run(
87+
[
88+
sys.executable,
89+
"-m",
90+
"grpc_tools.protoc",
91+
"-Iproto",
92+
"--python_out=python/openshell/_proto",
93+
"--pyi_out=python/openshell/_proto",
94+
"--grpc_python_out=python/openshell/_proto",
95+
*PROTO_FILES,
96+
],
97+
check=True,
98+
)
99+
100+
for path, rules in LINE_REWRITES.items():
101+
file_path = Path(path)
102+
text = file_path.read_text()
103+
text = text.replace("from . from . import", "from . import")
104+
for pattern, replacement in rules:
105+
text = re.sub(pattern, replacement, text, flags=re.MULTILINE)
106+
file_path.write_text(text)
107+
108+
109+
if __name__ == "__main__":
110+
main()

‎src/openshell/sandbox.py‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import base64
77
import contextlib
8+
import errno
89
import json
910
import os
1011
import pathlib
@@ -1065,6 +1066,40 @@ def _xdg_config_home() -> pathlib.Path:
10651066
# matches `openshell-bootstrap::oidc_token::is_token_expired`.
10661067
_OIDC_TOKEN_EXPIRY_GRACE_SECONDS = 30
10671068

1069+
_IS_WINDOWS = os.name == "nt"
1070+
_WINDOWS_REPLACE_RETRYABLE_ERRORS = frozenset({5, 32})
1071+
_WINDOWS_REPLACE_TIMEOUT_SECONDS = 0.25
1072+
_WINDOWS_REPLACE_INITIAL_DELAY_SECONDS = 0.005
1073+
_WINDOWS_REPLACE_MAX_DELAY_SECONDS = 0.05
1074+
_WINDOWS_REPLACE_LOCK = threading.Lock()
1075+
1076+
1077+
def _atomic_replace(source: pathlib.Path, destination: pathlib.Path) -> None:
1078+
"""Atomically replace a file, retrying transient Windows sharing errors."""
1079+
if not _IS_WINDOWS:
1080+
source.replace(destination)
1081+
return
1082+
1083+
# Serialize writers in this process. The retry still handles other
1084+
# processes (including the Rust CLI) and filesystem scanners that briefly
1085+
# open the destination without delete sharing.
1086+
with _WINDOWS_REPLACE_LOCK:
1087+
deadline = time.monotonic() + _WINDOWS_REPLACE_TIMEOUT_SECONDS
1088+
delay = _WINDOWS_REPLACE_INITIAL_DELAY_SECONDS
1089+
while True:
1090+
try:
1091+
source.replace(destination)
1092+
return
1093+
except PermissionError as error:
1094+
winerror = getattr(error, "winerror", None)
1095+
retryable = winerror in _WINDOWS_REPLACE_RETRYABLE_ERRORS or (
1096+
winerror is None and error.errno == errno.EACCES
1097+
)
1098+
if not retryable or time.monotonic() >= deadline:
1099+
raise
1100+
time.sleep(delay)
1101+
delay = min(delay * 2, _WINDOWS_REPLACE_MAX_DELAY_SECONDS)
1102+
10681103

10691104
def _read_oidc_token_bundle(gateway_dir: pathlib.Path) -> dict | None:
10701105
"""Read and parse `oidc_token.json` for a gateway.
@@ -1531,7 +1566,7 @@ def _write_to_disk(self, bundle: dict) -> None:
15311566
f.write(payload)
15321567
with contextlib.suppress(OSError):
15331568
tmp_path.chmod(0o600)
1534-
tmp_path.replace(path)
1569+
_atomic_replace(tmp_path, path)
15351570
except BaseException:
15361571
# Clean up our tmp on failure so we don't leave orphaned
15371572
# `.oidc_token.<rand>.tmp` files lying around. The replace

‎tests/sandbox_test.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import pytest
1818

19+
import openshell.sandbox as sandbox_module
1920
from openshell._proto import openshell_pb2
2021
from openshell.sandbox import (
2122
_PYTHON_CLOUDPICKLE_BOOTSTRAP,
@@ -27,6 +28,7 @@
2728
SandboxRef,
2829
SandboxStatusRef,
2930
TlsConfig,
31+
_atomic_replace,
3032
_BearerAuthInterceptor,
3133
_load_cluster_bearer_token,
3234
_make_cluster_bearer_provider,
@@ -1281,6 +1283,65 @@ def writer(idx: int) -> None:
12811283
r.close()
12821284

12831285

1286+
class _WindowsPermissionError(PermissionError):
1287+
winerror: int
1288+
1289+
1290+
def test_atomic_replace_retries_windows_sharing_violations(
1291+
tmp_path: Path, monkeypatch: Any
1292+
) -> None:
1293+
source = tmp_path / "source"
1294+
destination = tmp_path / "destination"
1295+
source.write_text("new")
1296+
destination.write_text("old")
1297+
attempts = 0
1298+
delays: list[float] = []
1299+
real_replace = Path.replace
1300+
1301+
def replace(path: Path, target: Path) -> Path:
1302+
nonlocal attempts
1303+
attempts += 1
1304+
if attempts < 3:
1305+
error = _WindowsPermissionError("destination is busy")
1306+
error.winerror = 32
1307+
raise error
1308+
return real_replace(path, target)
1309+
1310+
monkeypatch.setattr(sandbox_module, "_IS_WINDOWS", True)
1311+
monkeypatch.setattr(Path, "replace", replace)
1312+
monkeypatch.setattr(time, "sleep", delays.append)
1313+
1314+
_atomic_replace(source, destination)
1315+
1316+
assert attempts == 3
1317+
assert delays == [0.005, 0.01]
1318+
assert destination.read_text() == "new"
1319+
1320+
1321+
def test_atomic_replace_does_not_retry_permanent_windows_errors(
1322+
tmp_path: Path, monkeypatch: Any
1323+
) -> None:
1324+
source = tmp_path / "source"
1325+
destination = tmp_path / "destination"
1326+
source.write_text("new")
1327+
attempts = 0
1328+
1329+
def replace(_path: Path, _target: Path) -> Path:
1330+
nonlocal attempts
1331+
attempts += 1
1332+
error = _WindowsPermissionError("access denied")
1333+
error.winerror = 13
1334+
raise error
1335+
1336+
monkeypatch.setattr(sandbox_module, "_IS_WINDOWS", True)
1337+
monkeypatch.setattr(Path, "replace", replace)
1338+
1339+
with pytest.raises(PermissionError, match="access denied"):
1340+
_atomic_replace(source, destination)
1341+
1342+
assert attempts == 1
1343+
1344+
12841345
def test_sandbox_wrapper_forwards_auth_kwargs_to_from_active_cluster(
12851346
monkeypatch: Any,
12861347
) -> None:

0 commit comments

Comments
 (0)