Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
172 lines (159 loc) · 7.14 KB
/
Copy pathmain.py
File metadata and controls
172 lines (159 loc) · 7.14 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""Kill and restart a worker while its Temporal server and E2B sandbox stay alive."""
import asyncio
import json
import os
import sys
import uuid
from datetime import timedelta
from pathlib import Path
from tempfile import TemporaryDirectory
from project import (
ENV,
FIXTURE,
WORKDIR,
configure,
fingerprints,
sandbox_ids,
seed,
verify,
)
from pydantic_ai.durable_exec.temporal import PydanticAIPlugin
from pydantic_ai.workspaces import Workspace
from pydantic_ai_harness.e2b_sandbox import E2BSandbox, E2BSandboxBackend
from temporalio.client import WorkflowExecutionStatus
from temporalio.exceptions import TemporalError
from temporalio.service import RPCError, RPCStatusCode
from temporalio.testing import WorkflowEnvironment
async def start_worker(address: str, queue: str):
return await asyncio.create_subprocess_exec(
sys.executable,
str(Path(__file__).parent / "worker.py"),
address,
queue,
)
async def stop_worker(worker) -> None:
if worker is not None and worker.returncode is None:
worker.kill() # SIGKILL on macOS/Linux: no Python finally or graceful worker shutdown.
await asyncio.wait_for(worker.wait(), timeout=10)
async def wait_checkpoint(handle, worker) -> dict:
async with asyncio.timeout(120):
while worker.returncode is None:
try:
checkpoint = await handle.query(
"diagnosis_checkpoint", rpc_timeout=timedelta(seconds=15)
)
if checkpoint is not None:
return checkpoint
except RPCError as error:
if error.status != RPCStatusCode.DEADLINE_EXCEEDED:
raise
except TemporalError:
if (await handle.describe()).status != WorkflowExecutionStatus.RUNNING:
# Propagate a failed workflow instead of polling until timeout.
await handle.result()
raise RuntimeError("Workflow ended without a diagnosis checkpoint")
await asyncio.sleep(0.25)
raise RuntimeError(f"Worker exited before the checkpoint: {worker.returncode}")
async def main() -> None:
configure()
# Optional: serve the Temporal Web UI on this port to watch the workflow live.
ui_port = int(os.environ.get("TEMPORAL_UI_PORT") or 0)
baseline = await sandbox_ids()
backend = E2BSandboxBackend(
working_dir=WORKDIR, env=ENV, sandbox_timeout=600, allow_internet_access=False
)
worker = None
try:
async with asyncio.timeout(300) as deadline:
workspace = Workspace(backend)
await seed(workspace)
ref = backend.ref
assert ref is not None
print(f"Created sandbox: {ref.id}", flush=True)
# The application owns the sandbox; its credential-free ref is durable workflow input.
with TemporaryDirectory(prefix="pydantic-temporal-") as directory:
async with await WorkflowEnvironment.start_local(
ui=bool(ui_port),
ui_port=ui_port or None,
plugins=[PydanticAIPlugin()],
dev_server_database_filename=str(Path(directory) / "temporal.db"),
# Warn-level server logs (slow queries, history paging) interleave with the demo output.
dev_server_log_level="error",
) as server:
queue = f"log-cli-{uuid.uuid4()}"
worker = await start_worker(
server.client.service_client.config.target_host, queue
)
handle = await server.client.start_workflow(
"FixLogCLI",
ref,
id=queue,
task_queue=queue,
)
checkpoint = await wait_checkpoint(handle, worker)
assert checkpoint["sandbox_id"] == ref.id
assert checkpoint["checksums"] == await fingerprints(workspace)
assert (
await workspace.read_bytes("log_stats.py")
== (FIXTURE / "log_stats.py").read_bytes()
)
assert (await workspace.read_text("diagnosis.md")).strip()
print(
f"Durable checkpoint: {ref.id}; diagnosis and files saved",
flush=True,
)
old_pid = worker.pid
await stop_worker(worker)
print(
f"Worker crashed: pid={old_pid}, exit={worker.returncode}",
flush=True,
)
worker = await start_worker(
server.client.service_client.config.target_host, queue
)
assert worker.pid != old_pid
recovered = await wait_checkpoint(handle, worker)
assert recovered == checkpoint
print(
f"Checkpoint recovered by pid={worker.pid}; sandbox={ref.id}",
flush=True,
)
await handle.signal("continue_fix")
result = await handle.result()
assert result["sandbox_id"] == ref.id
assert await sandbox_ids() - baseline == {ref.id}, (
"Unexpected extra sandbox (run without concurrent sandbox creation)"
)
print(json.dumps(result, indent=2), flush=True)
print(f"CLI: {await verify(workspace)}", flush=True)
print(
"Exactly one sandbox; original workflow completed after worker restart",
flush=True,
)
if ui_port:
# The server stops with this block; keep the finished workflow viewable.
# The viewing pause must not use up the demo's 300-second deadline.
deadline.reschedule(deadline.when() + 30)
print(
f"Temporal UI stays up for 30s: http://localhost:{ui_port}"
f"/namespaces/default/workflows/{queue}",
flush=True,
)
await asyncio.sleep(30)
await stop_worker(worker)
worker = None
finally:
try:
await stop_worker(worker)
finally:
# A failed local shutdown must not skip remote cleanup.
if backend.ref is not None:
print(f"Cleaning up sandbox: {backend.ref.id}", flush=True)
async with asyncio.timeout(30):
await E2BSandbox().destroy(backend.ref)
assert backend.ref.id not in await sandbox_ids(), (
"Cleanup did not remove the sandbox"
)
print(f"Cleanup verified: {backend.ref.id}", flush=True)
if __name__ == "__main__":
asyncio.run(main())