Reuse sandbox resources instead of charging quota on every tool call

This commit is contained in:
WebUp Cloud committed 2026-11-07 23:26:34 +08:00
1 parent cb896bb0fa
commit 8f232c442b
3 files changed
+83 -14

No files matched your search

+36 -1
View File
@@ -73,7 +73,10 @@ class GatewayTests(unittest.TestCase):
def test_created_workspace_has_isolation_and_persistent_data(self):
core, net = MagicMock(), MagicMock()
core.read_namespaced_pod.return_value = types.SimpleNamespace(status=types.SimpleNamespace(phase="Running", container_statuses=[types.SimpleNamespace(ready=True)]))
net.read_namespaced_network_policy.side_effect = gateway.ApiException(status=404)
core.read_namespaced_persistent_volume_claim.side_effect = gateway.ApiException(status=404)
core.read_namespaced_pod.side_effect = gateway.ApiException(status=404)
core.create_namespaced_pod.return_value = types.SimpleNamespace(status=types.SimpleNamespace(phase="Running", container_statuses=[types.SimpleNamespace(ready=True)]))
with patch.object(gateway.app.state, "core", core, create=True), patch.object(gateway.app.state, "net", net, create=True), patch.object(gateway, "IMAGE", "test/isolated-image:local"):
gateway.ensure_workspace("00000000-0000-0000-0000-000000000001")
pod = core.create_namespaced_pod.call_args.kwargs["body"]["spec"]
@@ -86,6 +89,38 @@ class GatewayTests(unittest.TestCase):
self.assertEqual([], policy["ingress"])
self.assertIn("169.254.0.0/16", policy["egress"][1]["to"][0]["ipBlock"]["except"])
def test_existing_workspace_does_not_submit_creates_to_admission(self):
core, net = MagicMock(), MagicMock()
core.read_namespaced_pod.return_value = types.SimpleNamespace(
status=types.SimpleNamespace(phase="Running", container_statuses=[types.SimpleNamespace(ready=True)])
)
project = "00000000-0000-0000-0000-000000000004"
with (patch.object(gateway.app.state, "core", core, create=True),
patch.object(gateway.app.state, "net", net, create=True)):
for _ in range(3):
self.assertEqual("dev-00000000000000000000000000000004", gateway.ensure_workspace(project))
net.create_namespaced_network_policy.assert_not_called()
core.create_namespaced_persistent_volume_claim.assert_not_called()
core.create_namespaced_pod.assert_not_called()
def test_create_with_lost_response_checks_for_resource_before_retry(self):
resource = object()
read = MagicMock(side_effect=[gateway.ApiException(status=404), resource])
create = MagicMock(side_effect=gateway.ApiException(status=503))
with patch.object(gateway.time, "sleep"):
self.assertIs(resource, gateway.ensure_resource(read, create, "qa", {"metadata": {"name": "workspace"}}))
create.assert_called_once()
def test_quota_denial_of_new_resource_reports_capacity(self):
read = MagicMock(side_effect=gateway.ApiException(status=404))
denial = gateway.ApiException(status=403)
denial.body = json.dumps({"message": 'pods "workspace" is forbidden: exceeded quota: quota, requested: pods=1'})
create = MagicMock(side_effect=denial)
with self.assertRaises(gateway.HTTPException) as error:
gateway.ensure_resource(read, create, "qa", {"metadata": {"name": "workspace"}})
self.assertEqual((503, "WORKSPACE_CAPACITY"), (error.exception.status_code, error.exception.detail))
create.assert_called_once()
def test_transient_exec_handshake_recovers_before_tool_input(self):
project = "00000000-0000-0000-0000-000000000002"
request = gateway.Tool(name="write_file", arguments={"path": "index.html", "content": "test"})