zexus 1.6.2
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.
- package/LICENSE +0 -0
- package/README.md +2513 -0
- package/bin/zexus +2 -0
- package/bin/zpics +2 -0
- package/bin/zpm +2 -0
- package/bin/zx +2 -0
- package/bin/zx-deploy +2 -0
- package/bin/zx-dev +2 -0
- package/bin/zx-run +2 -0
- package/package.json +66 -0
- package/scripts/README.md +24 -0
- package/scripts/postinstall.js +44 -0
- package/shared_config.json +24 -0
- package/src/README.md +1525 -0
- package/src/tests/run_zexus_tests.py +117 -0
- package/src/tests/test_all_phases.zx +346 -0
- package/src/tests/test_blockchain_features.zx +306 -0
- package/src/tests/test_complexity_features.zx +321 -0
- package/src/tests/test_core_integration.py +185 -0
- package/src/tests/test_phase10_ecosystem.zx +177 -0
- package/src/tests/test_phase1_modifiers.zx +87 -0
- package/src/tests/test_phase2_plugins.zx +80 -0
- package/src/tests/test_phase3_security.zx +97 -0
- package/src/tests/test_phase4_vfs.zx +116 -0
- package/src/tests/test_phase5_types.zx +117 -0
- package/src/tests/test_phase6_metaprogramming.zx +125 -0
- package/src/tests/test_phase7_optimization.zx +132 -0
- package/src/tests/test_phase9_advanced_types.zx +157 -0
- package/src/tests/test_security_features.py +419 -0
- package/src/tests/test_security_features.zx +276 -0
- package/src/tests/test_simple_zx.zx +1 -0
- package/src/tests/test_verification_simple.zx +69 -0
- package/src/zexus/__init__.py +28 -0
- package/src/zexus/__main__.py +5 -0
- package/src/zexus/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/advanced_types.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/builtin_modules.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/capability_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/complexity_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/concurrency_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/config.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/dependency_injection.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/ecosystem.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/environment.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/error_reporter.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/hybrid_orchestrator.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/lexer.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/metaprogramming.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/module_cache.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/object.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/optimization.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/plugin_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/policy_engine.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/security.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/stdlib_integration.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/strategy_recovery.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/syntax_validator.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/type_system.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/virtual_filesystem.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/zexus_ast.cpython-312.pyc +0 -0
- package/src/zexus/__pycache__/zexus_token.cpython-312.pyc +0 -0
- package/src/zexus/advanced_types.py +401 -0
- package/src/zexus/blockchain/__init__.py +40 -0
- package/src/zexus/blockchain/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/blockchain/__pycache__/crypto.cpython-312.pyc +0 -0
- package/src/zexus/blockchain/__pycache__/ledger.cpython-312.pyc +0 -0
- package/src/zexus/blockchain/__pycache__/transaction.cpython-312.pyc +0 -0
- package/src/zexus/blockchain/crypto.py +463 -0
- package/src/zexus/blockchain/ledger.py +255 -0
- package/src/zexus/blockchain/transaction.py +267 -0
- package/src/zexus/builtin_modules.py +284 -0
- package/src/zexus/builtin_plugins.py +317 -0
- package/src/zexus/capability_system.py +372 -0
- package/src/zexus/cli/__init__.py +2 -0
- package/src/zexus/cli/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/cli/__pycache__/main.cpython-312.pyc +0 -0
- package/src/zexus/cli/main.py +707 -0
- package/src/zexus/cli/zpm.py +203 -0
- package/src/zexus/compare_interpreter_compiler.py +146 -0
- package/src/zexus/compiler/__init__.py +169 -0
- package/src/zexus/compiler/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/compiler/__pycache__/lexer.cpython-312.pyc +0 -0
- package/src/zexus/compiler/__pycache__/parser.cpython-312.pyc +0 -0
- package/src/zexus/compiler/__pycache__/zexus_ast.cpython-312.pyc +0 -0
- package/src/zexus/compiler/bytecode.py +266 -0
- package/src/zexus/compiler/compat_runtime.py +277 -0
- package/src/zexus/compiler/lexer.py +257 -0
- package/src/zexus/compiler/parser.py +779 -0
- package/src/zexus/compiler/semantic.py +118 -0
- package/src/zexus/compiler/zexus_ast.py +454 -0
- package/src/zexus/complexity_system.py +575 -0
- package/src/zexus/concurrency_system.py +493 -0
- package/src/zexus/config.py +201 -0
- package/src/zexus/crypto_bridge.py +19 -0
- package/src/zexus/dependency_injection.py +423 -0
- package/src/zexus/ecosystem.py +434 -0
- package/src/zexus/environment.py +101 -0
- package/src/zexus/environment_manager.py +119 -0
- package/src/zexus/error_reporter.py +314 -0
- package/src/zexus/evaluator/__init__.py +12 -0
- package/src/zexus/evaluator/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/bytecode_compiler.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/core.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/expressions.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/functions.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/integration.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/statements.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/__pycache__/utils.cpython-312.pyc +0 -0
- package/src/zexus/evaluator/bytecode_compiler.py +700 -0
- package/src/zexus/evaluator/core.py +891 -0
- package/src/zexus/evaluator/expressions.py +827 -0
- package/src/zexus/evaluator/functions.py +3989 -0
- package/src/zexus/evaluator/integration.py +396 -0
- package/src/zexus/evaluator/statements.py +4303 -0
- package/src/zexus/evaluator/utils.py +126 -0
- package/src/zexus/evaluator_original.py +2041 -0
- package/src/zexus/external_bridge.py +16 -0
- package/src/zexus/find_affected_imports.sh +155 -0
- package/src/zexus/hybrid_orchestrator.py +152 -0
- package/src/zexus/input_validation.py +259 -0
- package/src/zexus/lexer.py +571 -0
- package/src/zexus/logging.py +89 -0
- package/src/zexus/lsp/__init__.py +9 -0
- package/src/zexus/lsp/completion_provider.py +207 -0
- package/src/zexus/lsp/definition_provider.py +22 -0
- package/src/zexus/lsp/hover_provider.py +71 -0
- package/src/zexus/lsp/server.py +269 -0
- package/src/zexus/lsp/symbol_provider.py +31 -0
- package/src/zexus/metaprogramming.py +321 -0
- package/src/zexus/module_cache.py +89 -0
- package/src/zexus/module_manager.py +107 -0
- package/src/zexus/object.py +973 -0
- package/src/zexus/optimization.py +424 -0
- package/src/zexus/parser/__init__.py +31 -0
- package/src/zexus/parser/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/parser/__pycache__/parser.cpython-312.pyc +0 -0
- package/src/zexus/parser/__pycache__/strategy_context.cpython-312.pyc +0 -0
- package/src/zexus/parser/__pycache__/strategy_structural.cpython-312.pyc +0 -0
- package/src/zexus/parser/integration.py +86 -0
- package/src/zexus/parser/parser.py +3977 -0
- package/src/zexus/parser/strategy_context.py +7254 -0
- package/src/zexus/parser/strategy_structural.py +1033 -0
- package/src/zexus/persistence.py +391 -0
- package/src/zexus/plugin_system.py +290 -0
- package/src/zexus/policy_engine.py +365 -0
- package/src/zexus/profiler/__init__.py +5 -0
- package/src/zexus/profiler/profiler.py +233 -0
- package/src/zexus/purity_system.py +398 -0
- package/src/zexus/runtime/__init__.py +20 -0
- package/src/zexus/runtime/async_runtime.py +324 -0
- package/src/zexus/search_old_imports.sh +65 -0
- package/src/zexus/security.py +1407 -0
- package/src/zexus/stack_trace.py +233 -0
- package/src/zexus/stdlib/__init__.py +27 -0
- package/src/zexus/stdlib/blockchain.py +341 -0
- package/src/zexus/stdlib/compression.py +167 -0
- package/src/zexus/stdlib/crypto.py +124 -0
- package/src/zexus/stdlib/datetime.py +163 -0
- package/src/zexus/stdlib/db_mongo.py +199 -0
- package/src/zexus/stdlib/db_mysql.py +162 -0
- package/src/zexus/stdlib/db_postgres.py +163 -0
- package/src/zexus/stdlib/db_sqlite.py +133 -0
- package/src/zexus/stdlib/encoding.py +230 -0
- package/src/zexus/stdlib/fs.py +195 -0
- package/src/zexus/stdlib/http.py +219 -0
- package/src/zexus/stdlib/http_server.py +248 -0
- package/src/zexus/stdlib/json_module.py +61 -0
- package/src/zexus/stdlib/math.py +360 -0
- package/src/zexus/stdlib/os_module.py +265 -0
- package/src/zexus/stdlib/regex.py +148 -0
- package/src/zexus/stdlib/sockets.py +253 -0
- package/src/zexus/stdlib/test_framework.zx +208 -0
- package/src/zexus/stdlib/test_runner.zx +119 -0
- package/src/zexus/stdlib_integration.py +341 -0
- package/src/zexus/strategy_recovery.py +256 -0
- package/src/zexus/syntax_validator.py +356 -0
- package/src/zexus/testing/zpics.py +407 -0
- package/src/zexus/testing/zpics_runtime.py +369 -0
- package/src/zexus/type_system.py +374 -0
- package/src/zexus/validation_system.py +569 -0
- package/src/zexus/virtual_filesystem.py +355 -0
- package/src/zexus/vm/__init__.py +8 -0
- package/src/zexus/vm/__pycache__/__init__.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/async_optimizer.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/bytecode.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/cache.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/jit.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/memory_manager.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/memory_pool.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/optimizer.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/parallel_vm.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/peephole_optimizer.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/profiler.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/register_allocator.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/register_vm.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/ssa_converter.cpython-312.pyc +0 -0
- package/src/zexus/vm/__pycache__/vm.cpython-312.pyc +0 -0
- package/src/zexus/vm/async_optimizer.py +420 -0
- package/src/zexus/vm/bytecode.py +428 -0
- package/src/zexus/vm/bytecode_converter.py +297 -0
- package/src/zexus/vm/cache.py +532 -0
- package/src/zexus/vm/jit.py +720 -0
- package/src/zexus/vm/memory_manager.py +520 -0
- package/src/zexus/vm/memory_pool.py +511 -0
- package/src/zexus/vm/optimizer.py +478 -0
- package/src/zexus/vm/parallel_vm.py +899 -0
- package/src/zexus/vm/peephole_optimizer.py +452 -0
- package/src/zexus/vm/profiler.py +527 -0
- package/src/zexus/vm/register_allocator.py +462 -0
- package/src/zexus/vm/register_vm.py +520 -0
- package/src/zexus/vm/ssa_converter.py +757 -0
- package/src/zexus/vm/vm.py +1392 -0
- package/src/zexus/zexus_ast.py +1782 -0
- package/src/zexus/zexus_token.py +253 -0
- package/src/zexus/zpm/__init__.py +15 -0
- package/src/zexus/zpm/installer.py +116 -0
- package/src/zexus/zpm/package_manager.py +208 -0
- package/src/zexus/zpm/publisher.py +98 -0
- package/src/zexus/zpm/registry.py +110 -0
- package/src/zexus.egg-info/PKG-INFO +2235 -0
- package/src/zexus.egg-info/SOURCES.txt +876 -0
- package/src/zexus.egg-info/dependency_links.txt +1 -0
- package/src/zexus.egg-info/entry_points.txt +3 -0
- package/src/zexus.egg-info/not-zip-safe +1 -0
- package/src/zexus.egg-info/requires.txt +14 -0
- package/src/zexus.egg-info/top_level.txt +2 -0
- package/zexus.json +14 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Virtual filesystem and memory layer for sandboxed execution.
|
|
3
|
+
|
|
4
|
+
Provides isolated file access and memory quotas for plugins.
|
|
5
|
+
Each plugin operates in a restricted filesystem namespace.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from typing import Dict, List, Optional, Tuple, Any, Set
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from enum import Enum
|
|
12
|
+
import os
|
|
13
|
+
import sys
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class FileAccessMode(Enum):
|
|
17
|
+
"""File access permission levels."""
|
|
18
|
+
NONE = 0
|
|
19
|
+
READ = 1
|
|
20
|
+
WRITE = 2
|
|
21
|
+
READ_WRITE = 3
|
|
22
|
+
EXECUTE = 4
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class MemoryQuota:
|
|
27
|
+
"""Memory usage quota for a plugin/sandbox."""
|
|
28
|
+
max_bytes: int
|
|
29
|
+
warning_threshold: float = 0.8 # Warn at 80%
|
|
30
|
+
current_usage: int = 0
|
|
31
|
+
|
|
32
|
+
def is_over_quota(self) -> bool:
|
|
33
|
+
"""Check if quota exceeded."""
|
|
34
|
+
return self.current_usage > self.max_bytes
|
|
35
|
+
|
|
36
|
+
def is_over_warning(self) -> bool:
|
|
37
|
+
"""Check if warning threshold exceeded."""
|
|
38
|
+
return self.current_usage > (self.max_bytes * self.warning_threshold)
|
|
39
|
+
|
|
40
|
+
def get_available(self) -> int:
|
|
41
|
+
"""Get available memory."""
|
|
42
|
+
return max(0, self.max_bytes - self.current_usage)
|
|
43
|
+
|
|
44
|
+
def allocate(self, size: int) -> bool:
|
|
45
|
+
"""Try to allocate memory."""
|
|
46
|
+
if self.current_usage + size > self.max_bytes:
|
|
47
|
+
return False
|
|
48
|
+
self.current_usage += size
|
|
49
|
+
return True
|
|
50
|
+
|
|
51
|
+
def deallocate(self, size: int):
|
|
52
|
+
"""Deallocate memory."""
|
|
53
|
+
self.current_usage = max(0, self.current_usage - size)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass
|
|
57
|
+
class FileSystemPath:
|
|
58
|
+
"""Represents a path in the virtual filesystem."""
|
|
59
|
+
real_path: str # Actual filesystem path
|
|
60
|
+
virtual_path: str # How it appears to sandbox
|
|
61
|
+
access_mode: FileAccessMode = FileAccessMode.READ
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class SandboxFileSystem:
|
|
65
|
+
"""
|
|
66
|
+
Virtual filesystem for a sandbox/plugin.
|
|
67
|
+
|
|
68
|
+
Provides isolated file access with path restrictions.
|
|
69
|
+
Each plugin sees only paths it's granted access to.
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
def __init__(self, sandbox_id: str):
|
|
73
|
+
"""Initialize sandbox filesystem."""
|
|
74
|
+
self.sandbox_id = sandbox_id
|
|
75
|
+
self.mounts: Dict[str, FileSystemPath] = {} # virtual_path -> FileSystemPath
|
|
76
|
+
self.access_log: List[Dict] = []
|
|
77
|
+
|
|
78
|
+
def mount(self, real_path: str, virtual_path: str,
|
|
79
|
+
access_mode: FileAccessMode = FileAccessMode.READ) -> bool:
|
|
80
|
+
"""
|
|
81
|
+
Mount a real filesystem path into the sandbox.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
real_path: Actual path on filesystem
|
|
85
|
+
virtual_path: Path as seen by sandbox
|
|
86
|
+
access_mode: READ, WRITE, READ_WRITE, or EXECUTE
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
True if mounted successfully
|
|
90
|
+
"""
|
|
91
|
+
if virtual_path in self.mounts:
|
|
92
|
+
return False # Already mounted
|
|
93
|
+
|
|
94
|
+
# Normalize paths
|
|
95
|
+
real_path = os.path.normpath(real_path)
|
|
96
|
+
virtual_path = os.path.normpath(virtual_path)
|
|
97
|
+
|
|
98
|
+
if not os.path.exists(real_path):
|
|
99
|
+
return False
|
|
100
|
+
|
|
101
|
+
mount = FileSystemPath(real_path, virtual_path, access_mode)
|
|
102
|
+
self.mounts[virtual_path] = mount
|
|
103
|
+
return True
|
|
104
|
+
|
|
105
|
+
def unmount(self, virtual_path: str) -> bool:
|
|
106
|
+
"""Unmount a path from the sandbox."""
|
|
107
|
+
if virtual_path in self.mounts:
|
|
108
|
+
del self.mounts[virtual_path]
|
|
109
|
+
return True
|
|
110
|
+
return False
|
|
111
|
+
|
|
112
|
+
def resolve_path(self, virtual_path: str) -> Optional[Tuple[str, FileAccessMode]]:
|
|
113
|
+
"""
|
|
114
|
+
Resolve a virtual path to a real path.
|
|
115
|
+
|
|
116
|
+
Returns:
|
|
117
|
+
(real_path, access_mode) if accessible, None otherwise
|
|
118
|
+
"""
|
|
119
|
+
virtual_path = os.path.normpath(virtual_path)
|
|
120
|
+
|
|
121
|
+
# Check exact mount
|
|
122
|
+
if virtual_path in self.mounts:
|
|
123
|
+
mount = self.mounts[virtual_path]
|
|
124
|
+
return mount.real_path, mount.access_mode
|
|
125
|
+
|
|
126
|
+
# Check if under a mounted directory
|
|
127
|
+
for mount_point, mount in self.mounts.items():
|
|
128
|
+
if virtual_path.startswith(mount_point + os.sep):
|
|
129
|
+
# Get relative path
|
|
130
|
+
rel_path = virtual_path[len(mount_point)+1:]
|
|
131
|
+
real_path = os.path.normpath(os.path.join(mount.real_path, rel_path))
|
|
132
|
+
|
|
133
|
+
# Ensure real path is still under mounted root
|
|
134
|
+
if real_path.startswith(mount.real_path):
|
|
135
|
+
return real_path, mount.access_mode
|
|
136
|
+
|
|
137
|
+
return None
|
|
138
|
+
|
|
139
|
+
def can_read(self, virtual_path: str) -> bool:
|
|
140
|
+
"""Check if path can be read."""
|
|
141
|
+
result = self.resolve_path(virtual_path)
|
|
142
|
+
if result is None:
|
|
143
|
+
return False
|
|
144
|
+
_, mode = result
|
|
145
|
+
return mode in (FileAccessMode.READ, FileAccessMode.READ_WRITE)
|
|
146
|
+
|
|
147
|
+
def can_write(self, virtual_path: str) -> bool:
|
|
148
|
+
"""Check if path can be written."""
|
|
149
|
+
result = self.resolve_path(virtual_path)
|
|
150
|
+
if result is None:
|
|
151
|
+
return False
|
|
152
|
+
_, mode = result
|
|
153
|
+
return mode in (FileAccessMode.WRITE, FileAccessMode.READ_WRITE)
|
|
154
|
+
|
|
155
|
+
def log_access(self, operation: str, virtual_path: str, allowed: bool, reason: str = ""):
|
|
156
|
+
"""Log a filesystem access attempt."""
|
|
157
|
+
entry = {
|
|
158
|
+
"operation": operation,
|
|
159
|
+
"virtual_path": virtual_path,
|
|
160
|
+
"allowed": allowed,
|
|
161
|
+
"reason": reason
|
|
162
|
+
}
|
|
163
|
+
self.access_log.append(entry)
|
|
164
|
+
|
|
165
|
+
def get_access_log(self) -> List[Dict]:
|
|
166
|
+
"""Get filesystem access log."""
|
|
167
|
+
return self.access_log.copy()
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
class VirtualFileSystemManager:
|
|
171
|
+
"""
|
|
172
|
+
Manages virtual filesystems for multiple sandboxes.
|
|
173
|
+
|
|
174
|
+
Coordinates sandbox creation, isolation, and resource cleanup.
|
|
175
|
+
"""
|
|
176
|
+
|
|
177
|
+
def __init__(self):
|
|
178
|
+
"""Initialize filesystem manager."""
|
|
179
|
+
self.sandboxes: Dict[str, SandboxFileSystem] = {}
|
|
180
|
+
self.memory_quotas: Dict[str, MemoryQuota] = {}
|
|
181
|
+
self.default_memory_quota = 1024 * 1024 * 100 # 100MB default
|
|
182
|
+
|
|
183
|
+
def create_sandbox(self, sandbox_id: str, memory_quota_mb: int = 100) -> SandboxFileSystem:
|
|
184
|
+
"""
|
|
185
|
+
Create a new sandbox.
|
|
186
|
+
|
|
187
|
+
Args:
|
|
188
|
+
sandbox_id: Unique identifier for sandbox
|
|
189
|
+
memory_quota_mb: Memory limit in MB
|
|
190
|
+
|
|
191
|
+
Returns:
|
|
192
|
+
SandboxFileSystem instance
|
|
193
|
+
"""
|
|
194
|
+
if sandbox_id in self.sandboxes:
|
|
195
|
+
return self.sandboxes[sandbox_id]
|
|
196
|
+
|
|
197
|
+
sandbox = SandboxFileSystem(sandbox_id)
|
|
198
|
+
self.sandboxes[sandbox_id] = sandbox
|
|
199
|
+
|
|
200
|
+
# Set memory quota
|
|
201
|
+
quota = MemoryQuota(max_bytes=memory_quota_mb * 1024 * 1024)
|
|
202
|
+
self.memory_quotas[sandbox_id] = quota
|
|
203
|
+
|
|
204
|
+
return sandbox
|
|
205
|
+
|
|
206
|
+
def get_sandbox(self, sandbox_id: str) -> Optional[SandboxFileSystem]:
|
|
207
|
+
"""Get existing sandbox."""
|
|
208
|
+
return self.sandboxes.get(sandbox_id)
|
|
209
|
+
|
|
210
|
+
def delete_sandbox(self, sandbox_id: str) -> bool:
|
|
211
|
+
"""Delete a sandbox and cleanup."""
|
|
212
|
+
if sandbox_id not in self.sandboxes:
|
|
213
|
+
return False
|
|
214
|
+
|
|
215
|
+
del self.sandboxes[sandbox_id]
|
|
216
|
+
if sandbox_id in self.memory_quotas:
|
|
217
|
+
del self.memory_quotas[sandbox_id]
|
|
218
|
+
|
|
219
|
+
return True
|
|
220
|
+
|
|
221
|
+
def allocate_memory(self, sandbox_id: str, size: int) -> bool:
|
|
222
|
+
"""
|
|
223
|
+
Allocate memory for a sandbox.
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
True if allocated, False if quota exceeded
|
|
227
|
+
"""
|
|
228
|
+
if sandbox_id not in self.memory_quotas:
|
|
229
|
+
return False
|
|
230
|
+
|
|
231
|
+
quota = self.memory_quotas[sandbox_id]
|
|
232
|
+
return quota.allocate(size)
|
|
233
|
+
|
|
234
|
+
def deallocate_memory(self, sandbox_id: str, size: int):
|
|
235
|
+
"""Deallocate memory from a sandbox."""
|
|
236
|
+
if sandbox_id in self.memory_quotas:
|
|
237
|
+
quota = self.memory_quotas[sandbox_id]
|
|
238
|
+
quota.deallocate(size)
|
|
239
|
+
|
|
240
|
+
def get_memory_quota(self, sandbox_id: str) -> Optional[MemoryQuota]:
|
|
241
|
+
"""Get memory quota for a sandbox."""
|
|
242
|
+
return self.memory_quotas.get(sandbox_id)
|
|
243
|
+
|
|
244
|
+
def list_sandboxes(self) -> List[str]:
|
|
245
|
+
"""List all active sandboxes."""
|
|
246
|
+
return list(self.sandboxes.keys())
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
class StandardMounts:
|
|
250
|
+
"""Standard filesystem mount configurations."""
|
|
251
|
+
|
|
252
|
+
@staticmethod
|
|
253
|
+
def read_only_home() -> Dict[str, Tuple[str, FileAccessMode]]:
|
|
254
|
+
"""Read-only home directory access."""
|
|
255
|
+
return {
|
|
256
|
+
"/home": (os.path.expanduser("~"), FileAccessMode.READ),
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
@staticmethod
|
|
260
|
+
def temp_directory() -> Dict[str, Tuple[str, FileAccessMode]]:
|
|
261
|
+
"""Temporary directory with read-write access."""
|
|
262
|
+
return {
|
|
263
|
+
"/tmp": ("/tmp", FileAccessMode.READ_WRITE),
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
@staticmethod
|
|
267
|
+
def app_data() -> Dict[str, Tuple[str, FileAccessMode]]:
|
|
268
|
+
"""Application data directory."""
|
|
269
|
+
return {
|
|
270
|
+
"/app": (os.path.expanduser("~/.app"), FileAccessMode.READ_WRITE),
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
@staticmethod
|
|
274
|
+
def system_readonly() -> Dict[str, Tuple[str, FileAccessMode]]:
|
|
275
|
+
"""System paths (read-only)."""
|
|
276
|
+
return {
|
|
277
|
+
"/etc": ("/etc", FileAccessMode.READ),
|
|
278
|
+
"/usr/share": ("/usr/share", FileAccessMode.READ),
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
class SandboxBuilder:
|
|
283
|
+
"""Builder for configuring sandboxes with standard mounts."""
|
|
284
|
+
|
|
285
|
+
def __init__(self, manager: VirtualFileSystemManager, sandbox_id: str):
|
|
286
|
+
"""Initialize builder."""
|
|
287
|
+
self.manager = manager
|
|
288
|
+
self.sandbox_id = sandbox_id
|
|
289
|
+
self.sandbox = manager.create_sandbox(sandbox_id)
|
|
290
|
+
self.mounts: Dict[str, Tuple[str, FileAccessMode]] = {}
|
|
291
|
+
|
|
292
|
+
def add_mount(self, virtual_path: str, real_path: str,
|
|
293
|
+
access_mode: FileAccessMode = FileAccessMode.READ) -> 'SandboxBuilder':
|
|
294
|
+
"""Add a mount point."""
|
|
295
|
+
self.mounts[virtual_path] = (real_path, access_mode)
|
|
296
|
+
return self
|
|
297
|
+
|
|
298
|
+
def with_temp_access(self) -> 'SandboxBuilder':
|
|
299
|
+
"""Add temporary directory access."""
|
|
300
|
+
self.mounts.update(StandardMounts.temp_directory())
|
|
301
|
+
return self
|
|
302
|
+
|
|
303
|
+
def with_home_readonly(self) -> 'SandboxBuilder':
|
|
304
|
+
"""Add read-only home directory."""
|
|
305
|
+
self.mounts.update(StandardMounts.read_only_home())
|
|
306
|
+
return self
|
|
307
|
+
|
|
308
|
+
def with_app_data(self) -> 'SandboxBuilder':
|
|
309
|
+
"""Add application data directory."""
|
|
310
|
+
self.mounts.update(StandardMounts.app_data())
|
|
311
|
+
return self
|
|
312
|
+
|
|
313
|
+
def build(self) -> SandboxFileSystem:
|
|
314
|
+
"""Build the sandbox with configured mounts."""
|
|
315
|
+
for virtual_path, (real_path, access_mode) in self.mounts.items():
|
|
316
|
+
self.sandbox.mount(real_path, virtual_path, access_mode)
|
|
317
|
+
return self.sandbox
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
# Predefined sandbox configurations
|
|
321
|
+
|
|
322
|
+
SANDBOX_PRESETS = {
|
|
323
|
+
"read_only": {
|
|
324
|
+
"description": "Read-only file access",
|
|
325
|
+
"mounts": {
|
|
326
|
+
"/data": ("./data", FileAccessMode.READ),
|
|
327
|
+
},
|
|
328
|
+
"memory_quota_mb": 50
|
|
329
|
+
},
|
|
330
|
+
|
|
331
|
+
"trusted": {
|
|
332
|
+
"description": "Trusted code with full access",
|
|
333
|
+
"mounts": {
|
|
334
|
+
"/": ("/", FileAccessMode.READ_WRITE),
|
|
335
|
+
},
|
|
336
|
+
"memory_quota_mb": 500
|
|
337
|
+
},
|
|
338
|
+
|
|
339
|
+
"isolated": {
|
|
340
|
+
"description": "Isolated with only temp directory",
|
|
341
|
+
"mounts": {
|
|
342
|
+
"/tmp": ("/tmp", FileAccessMode.READ_WRITE),
|
|
343
|
+
},
|
|
344
|
+
"memory_quota_mb": 100
|
|
345
|
+
},
|
|
346
|
+
|
|
347
|
+
"plugin": {
|
|
348
|
+
"description": "Plugin with app data and temp",
|
|
349
|
+
"mounts": {
|
|
350
|
+
"/app": (os.path.expanduser("~/.zexus/plugins"), FileAccessMode.READ_WRITE),
|
|
351
|
+
"/tmp": ("/tmp", FileAccessMode.READ_WRITE),
|
|
352
|
+
},
|
|
353
|
+
"memory_quota_mb": 200
|
|
354
|
+
},
|
|
355
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|