plato-sdk-v2 2.3.1__py3-none-any.whl → 2.3.2__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.
- plato/agents/__init__.py +4 -0
- plato/agents/logging.py +114 -0
- plato/worlds/__init__.py +3 -1
- plato/worlds/base.py +276 -2
- plato/worlds/config.py +38 -1
- plato/worlds/runner.py +93 -43
- {plato_sdk_v2-2.3.1.dist-info → plato_sdk_v2-2.3.2.dist-info}/METADATA +2 -1
- {plato_sdk_v2-2.3.1.dist-info → plato_sdk_v2-2.3.2.dist-info}/RECORD +10 -10
- {plato_sdk_v2-2.3.1.dist-info → plato_sdk_v2-2.3.2.dist-info}/WHEEL +0 -0
- {plato_sdk_v2-2.3.1.dist-info → plato_sdk_v2-2.3.2.dist-info}/entry_points.txt +0 -0
plato/agents/__init__.py
CHANGED
|
@@ -85,6 +85,8 @@ __all__ = [
|
|
|
85
85
|
"span",
|
|
86
86
|
"log_event",
|
|
87
87
|
"upload_artifacts",
|
|
88
|
+
"upload_artifact",
|
|
89
|
+
"upload_checkpoint",
|
|
88
90
|
"reset_logging",
|
|
89
91
|
]
|
|
90
92
|
|
|
@@ -102,7 +104,9 @@ from plato.agents.logging import (
|
|
|
102
104
|
log_event,
|
|
103
105
|
reset_logging,
|
|
104
106
|
span,
|
|
107
|
+
upload_artifact,
|
|
105
108
|
upload_artifacts,
|
|
109
|
+
upload_checkpoint,
|
|
106
110
|
)
|
|
107
111
|
from plato.agents.runner import run_agent
|
|
108
112
|
from plato.agents.trajectory import (
|
plato/agents/logging.py
CHANGED
|
@@ -399,3 +399,117 @@ async def upload_artifacts(dir_path: str) -> str | None:
|
|
|
399
399
|
except Exception as e:
|
|
400
400
|
logger.warning(f"Failed to upload artifacts: {e}")
|
|
401
401
|
return None
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
# =============================================================================
|
|
405
|
+
# Artifact Upload (Generic)
|
|
406
|
+
# =============================================================================
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
async def upload_artifact(
|
|
410
|
+
data: bytes,
|
|
411
|
+
artifact_type: str,
|
|
412
|
+
filename: str | None = None,
|
|
413
|
+
extra: dict[str, Any] | None = None,
|
|
414
|
+
) -> dict[str, Any] | None:
|
|
415
|
+
"""Upload an artifact to Chronos.
|
|
416
|
+
|
|
417
|
+
Artifacts are stored in S3 and linked to the session in the database.
|
|
418
|
+
|
|
419
|
+
Args:
|
|
420
|
+
data: Raw bytes of the artifact
|
|
421
|
+
artifact_type: Type of artifact (e.g., "state", "logs", "trajectory")
|
|
422
|
+
filename: Optional filename for the artifact
|
|
423
|
+
extra: Optional extra data to store with the artifact
|
|
424
|
+
|
|
425
|
+
Returns:
|
|
426
|
+
Dict with artifact_id and s3_url if successful, None otherwise.
|
|
427
|
+
"""
|
|
428
|
+
chronos = _ChronosLogger._instance
|
|
429
|
+
if not chronos or not chronos.enabled:
|
|
430
|
+
return None
|
|
431
|
+
|
|
432
|
+
try:
|
|
433
|
+
data_base64 = base64.b64encode(data).decode("utf-8")
|
|
434
|
+
logger.info(f"Uploading artifact: type={artifact_type}, size={len(data)} bytes")
|
|
435
|
+
except Exception as e:
|
|
436
|
+
logger.warning(f"Failed to encode artifact: {e}")
|
|
437
|
+
return None
|
|
438
|
+
|
|
439
|
+
try:
|
|
440
|
+
async with httpx.AsyncClient(timeout=120.0) as client:
|
|
441
|
+
response = await client.post(
|
|
442
|
+
f"{chronos.callback_url}/artifact",
|
|
443
|
+
json={
|
|
444
|
+
"session_id": chronos.session_id,
|
|
445
|
+
"artifact_type": artifact_type,
|
|
446
|
+
"data_base64": data_base64,
|
|
447
|
+
"filename": filename,
|
|
448
|
+
"extra": extra or {},
|
|
449
|
+
},
|
|
450
|
+
)
|
|
451
|
+
if response.status_code == 200:
|
|
452
|
+
result = response.json()
|
|
453
|
+
logger.info(f"Uploaded artifact: {result}")
|
|
454
|
+
return result
|
|
455
|
+
else:
|
|
456
|
+
logger.warning(f"Failed to upload artifact: {response.status_code} {response.text}")
|
|
457
|
+
return None
|
|
458
|
+
except Exception as e:
|
|
459
|
+
logger.warning(f"Failed to upload artifact: {e}")
|
|
460
|
+
return None
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
# =============================================================================
|
|
464
|
+
# Checkpoint Upload
|
|
465
|
+
# =============================================================================
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
async def upload_checkpoint(
|
|
469
|
+
step_number: int,
|
|
470
|
+
env_snapshots: dict[str, str],
|
|
471
|
+
state_artifact_id: str | None = None,
|
|
472
|
+
extra: dict[str, Any] | None = None,
|
|
473
|
+
) -> dict[str, Any] | None:
|
|
474
|
+
"""Upload checkpoint data to Chronos.
|
|
475
|
+
|
|
476
|
+
A checkpoint includes:
|
|
477
|
+
- Environment snapshots (artifact IDs per env alias)
|
|
478
|
+
- State artifact (git bundle of /state directory)
|
|
479
|
+
- Extra data (step number, timestamp, etc.)
|
|
480
|
+
|
|
481
|
+
Args:
|
|
482
|
+
step_number: The step number when this checkpoint was created
|
|
483
|
+
env_snapshots: Dict mapping env alias to artifact_id
|
|
484
|
+
state_artifact_id: Artifact ID of the state bundle (from upload_artifact)
|
|
485
|
+
extra: Optional additional data
|
|
486
|
+
|
|
487
|
+
Returns:
|
|
488
|
+
Dict with checkpoint_id if successful, None otherwise.
|
|
489
|
+
"""
|
|
490
|
+
chronos = _ChronosLogger._instance
|
|
491
|
+
if not chronos or not chronos.enabled:
|
|
492
|
+
return None
|
|
493
|
+
|
|
494
|
+
try:
|
|
495
|
+
async with httpx.AsyncClient(timeout=60.0) as client:
|
|
496
|
+
response = await client.post(
|
|
497
|
+
f"{chronos.callback_url}/checkpoint",
|
|
498
|
+
json={
|
|
499
|
+
"session_id": chronos.session_id,
|
|
500
|
+
"step_number": step_number,
|
|
501
|
+
"env_snapshots": env_snapshots,
|
|
502
|
+
"state_artifact_id": state_artifact_id,
|
|
503
|
+
"extra": extra or {},
|
|
504
|
+
},
|
|
505
|
+
)
|
|
506
|
+
if response.status_code == 200:
|
|
507
|
+
result = response.json()
|
|
508
|
+
logger.info(f"Uploaded checkpoint: step={step_number}, checkpoint_id={result.get('checkpoint_id')}")
|
|
509
|
+
return result
|
|
510
|
+
else:
|
|
511
|
+
logger.warning(f"Failed to upload checkpoint: {response.status_code} {response.text}")
|
|
512
|
+
return None
|
|
513
|
+
except Exception as e:
|
|
514
|
+
logger.warning(f"Failed to upload checkpoint: {e}")
|
|
515
|
+
return None
|
plato/worlds/__init__.py
CHANGED
|
@@ -52,7 +52,7 @@ from plato.worlds.base import (
|
|
|
52
52
|
get_world,
|
|
53
53
|
register_world,
|
|
54
54
|
)
|
|
55
|
-
from plato.worlds.config import Agent, AgentConfig, Env, EnvConfig, RunConfig, Secret
|
|
55
|
+
from plato.worlds.config import Agent, AgentConfig, CheckpointConfig, Env, EnvConfig, RunConfig, Secret, StateConfig
|
|
56
56
|
from plato.worlds.runner import run_world
|
|
57
57
|
|
|
58
58
|
__all__ = [
|
|
@@ -66,6 +66,8 @@ __all__ = [
|
|
|
66
66
|
"get_world",
|
|
67
67
|
# Config
|
|
68
68
|
"RunConfig",
|
|
69
|
+
"CheckpointConfig",
|
|
70
|
+
"StateConfig",
|
|
69
71
|
"AgentConfig",
|
|
70
72
|
"Agent",
|
|
71
73
|
"Secret",
|
plato/worlds/base.py
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import logging
|
|
6
|
+
import subprocess
|
|
6
7
|
from abc import ABC, abstractmethod
|
|
8
|
+
from pathlib import Path
|
|
7
9
|
from typing import TYPE_CHECKING, Any, ClassVar, Generic, TypeVar, get_args, get_origin
|
|
8
10
|
|
|
9
11
|
from pydantic import BaseModel, Field
|
|
@@ -18,6 +20,8 @@ from plato.agents.logging import init_logging as _init_chronos_logging
|
|
|
18
20
|
from plato.agents.logging import log_event as _log_event
|
|
19
21
|
from plato.agents.logging import reset_logging as _reset_chronos_logging
|
|
20
22
|
from plato.agents.logging import span as _span
|
|
23
|
+
from plato.agents.logging import upload_artifact as _upload_artifact
|
|
24
|
+
from plato.agents.logging import upload_checkpoint as _upload_checkpoint
|
|
21
25
|
|
|
22
26
|
logger = logging.getLogger(__name__)
|
|
23
27
|
|
|
@@ -195,6 +199,248 @@ class BaseWorld(ABC, Generic[ConfigT]):
|
|
|
195
199
|
except Exception as e:
|
|
196
200
|
self.logger.warning(f"Error stopping Plato heartbeat: {e}")
|
|
197
201
|
|
|
202
|
+
async def _create_checkpoint(self) -> dict[str, str] | None:
|
|
203
|
+
"""Create a checkpoint snapshot of all environments (excluding configured envs).
|
|
204
|
+
|
|
205
|
+
Uses snapshot_store for efficient chunk-based deduplication.
|
|
206
|
+
|
|
207
|
+
Returns:
|
|
208
|
+
Dict mapping environment alias to artifact_id, or None if no session connected.
|
|
209
|
+
"""
|
|
210
|
+
if not self.plato_session:
|
|
211
|
+
self.logger.warning("Cannot create checkpoint: Plato session not connected")
|
|
212
|
+
return None
|
|
213
|
+
|
|
214
|
+
exclude_envs = set(self.config.checkpoint.exclude_envs)
|
|
215
|
+
envs_to_snapshot = [env for env in self.plato_session.envs if env.alias not in exclude_envs]
|
|
216
|
+
|
|
217
|
+
if not envs_to_snapshot:
|
|
218
|
+
self.logger.info("No environments to checkpoint (all excluded)")
|
|
219
|
+
return {}
|
|
220
|
+
|
|
221
|
+
self.logger.info(
|
|
222
|
+
f"Creating checkpoint for {len(envs_to_snapshot)} environment(s): {[e.alias for e in envs_to_snapshot]}"
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
results: dict[str, str] = {}
|
|
226
|
+
for env in envs_to_snapshot:
|
|
227
|
+
try:
|
|
228
|
+
result = await env.snapshot_store()
|
|
229
|
+
artifact_id = result.artifact_id
|
|
230
|
+
results[env.alias] = artifact_id
|
|
231
|
+
|
|
232
|
+
# Check for success/error fields (available after SDK regeneration)
|
|
233
|
+
success = getattr(result, "success", True)
|
|
234
|
+
error = getattr(result, "error", None)
|
|
235
|
+
|
|
236
|
+
if not success or error:
|
|
237
|
+
self.logger.error(
|
|
238
|
+
f"Checkpoint failed for '{env.alias}': {error or 'unknown error'} (job_id={env.job_id})"
|
|
239
|
+
)
|
|
240
|
+
elif artifact_id:
|
|
241
|
+
self.logger.info(f"Checkpoint created for '{env.alias}': {artifact_id}")
|
|
242
|
+
else:
|
|
243
|
+
self.logger.warning(
|
|
244
|
+
f"Checkpoint for '{env.alias}' returned empty artifact_id (job_id={env.job_id})"
|
|
245
|
+
)
|
|
246
|
+
except Exception as e:
|
|
247
|
+
self.logger.error(f"Failed to checkpoint '{env.alias}': {e}")
|
|
248
|
+
|
|
249
|
+
return results
|
|
250
|
+
|
|
251
|
+
def _init_state_directory(self) -> None:
|
|
252
|
+
"""Initialize the state directory as a git repository.
|
|
253
|
+
|
|
254
|
+
Creates the state directory if it doesn't exist and initializes it
|
|
255
|
+
as a git repository with an initial commit.
|
|
256
|
+
"""
|
|
257
|
+
if not self.config.state.enabled:
|
|
258
|
+
return
|
|
259
|
+
|
|
260
|
+
state_path = Path(self.config.state.path)
|
|
261
|
+
|
|
262
|
+
# Create directory if it doesn't exist
|
|
263
|
+
if not state_path.exists():
|
|
264
|
+
state_path.mkdir(parents=True)
|
|
265
|
+
self.logger.info(f"Created state directory: {state_path}")
|
|
266
|
+
|
|
267
|
+
# Check if already a git repo
|
|
268
|
+
git_dir = state_path / ".git"
|
|
269
|
+
if git_dir.exists():
|
|
270
|
+
self.logger.info(f"State directory already initialized: {state_path}")
|
|
271
|
+
return
|
|
272
|
+
|
|
273
|
+
# Initialize git repo
|
|
274
|
+
try:
|
|
275
|
+
subprocess.run(
|
|
276
|
+
["git", "init"],
|
|
277
|
+
cwd=state_path,
|
|
278
|
+
capture_output=True,
|
|
279
|
+
check=True,
|
|
280
|
+
)
|
|
281
|
+
# Create initial commit (even if empty)
|
|
282
|
+
subprocess.run(
|
|
283
|
+
["git", "config", "user.email", "plato@plato.so"],
|
|
284
|
+
cwd=state_path,
|
|
285
|
+
capture_output=True,
|
|
286
|
+
check=True,
|
|
287
|
+
)
|
|
288
|
+
subprocess.run(
|
|
289
|
+
["git", "config", "user.name", "Plato"],
|
|
290
|
+
cwd=state_path,
|
|
291
|
+
capture_output=True,
|
|
292
|
+
check=True,
|
|
293
|
+
)
|
|
294
|
+
# Add all files and create initial commit
|
|
295
|
+
subprocess.run(
|
|
296
|
+
["git", "add", "-A"],
|
|
297
|
+
cwd=state_path,
|
|
298
|
+
capture_output=True,
|
|
299
|
+
check=True,
|
|
300
|
+
)
|
|
301
|
+
subprocess.run(
|
|
302
|
+
["git", "commit", "--allow-empty", "-m", "Initial state"],
|
|
303
|
+
cwd=state_path,
|
|
304
|
+
capture_output=True,
|
|
305
|
+
check=True,
|
|
306
|
+
)
|
|
307
|
+
self.logger.info(f"Initialized git repo in state directory: {state_path}")
|
|
308
|
+
except subprocess.CalledProcessError as e:
|
|
309
|
+
self.logger.warning(f"Failed to initialize state git repo: {e.stderr}")
|
|
310
|
+
|
|
311
|
+
def _commit_state(self, message: str) -> bool:
|
|
312
|
+
"""Commit current state directory changes.
|
|
313
|
+
|
|
314
|
+
Args:
|
|
315
|
+
message: Commit message
|
|
316
|
+
|
|
317
|
+
Returns:
|
|
318
|
+
True if commit was created (or no changes), False on error.
|
|
319
|
+
"""
|
|
320
|
+
if not self.config.state.enabled:
|
|
321
|
+
return True
|
|
322
|
+
|
|
323
|
+
state_path = Path(self.config.state.path)
|
|
324
|
+
if not state_path.exists():
|
|
325
|
+
return True
|
|
326
|
+
|
|
327
|
+
try:
|
|
328
|
+
# Add all changes
|
|
329
|
+
subprocess.run(
|
|
330
|
+
["git", "add", "-A"],
|
|
331
|
+
cwd=state_path,
|
|
332
|
+
capture_output=True,
|
|
333
|
+
check=True,
|
|
334
|
+
)
|
|
335
|
+
# Check if there are changes to commit
|
|
336
|
+
result = subprocess.run(
|
|
337
|
+
["git", "status", "--porcelain"],
|
|
338
|
+
cwd=state_path,
|
|
339
|
+
capture_output=True,
|
|
340
|
+
text=True,
|
|
341
|
+
check=True,
|
|
342
|
+
)
|
|
343
|
+
if not result.stdout.strip():
|
|
344
|
+
self.logger.debug("No state changes to commit")
|
|
345
|
+
return True
|
|
346
|
+
|
|
347
|
+
# Commit changes
|
|
348
|
+
subprocess.run(
|
|
349
|
+
["git", "commit", "-m", message],
|
|
350
|
+
cwd=state_path,
|
|
351
|
+
capture_output=True,
|
|
352
|
+
check=True,
|
|
353
|
+
)
|
|
354
|
+
self.logger.info(f"Committed state changes: {message}")
|
|
355
|
+
return True
|
|
356
|
+
except subprocess.CalledProcessError as e:
|
|
357
|
+
self.logger.warning(f"Failed to commit state: {e.stderr}")
|
|
358
|
+
return False
|
|
359
|
+
|
|
360
|
+
def _create_state_bundle(self) -> bytes | None:
|
|
361
|
+
"""Create a git bundle of the state directory.
|
|
362
|
+
|
|
363
|
+
Returns:
|
|
364
|
+
Bundle bytes if successful, None otherwise.
|
|
365
|
+
"""
|
|
366
|
+
if not self.config.state.enabled:
|
|
367
|
+
return None
|
|
368
|
+
|
|
369
|
+
state_path = Path(self.config.state.path)
|
|
370
|
+
if not state_path.exists():
|
|
371
|
+
return None
|
|
372
|
+
|
|
373
|
+
git_dir = state_path / ".git"
|
|
374
|
+
if not git_dir.exists():
|
|
375
|
+
self.logger.warning("State directory is not a git repository")
|
|
376
|
+
return None
|
|
377
|
+
|
|
378
|
+
try:
|
|
379
|
+
# Create bundle to stdout
|
|
380
|
+
result = subprocess.run(
|
|
381
|
+
["git", "bundle", "create", "-", "--all"],
|
|
382
|
+
cwd=state_path,
|
|
383
|
+
capture_output=True,
|
|
384
|
+
check=True,
|
|
385
|
+
)
|
|
386
|
+
bundle_data = result.stdout
|
|
387
|
+
self.logger.info(f"Created state bundle: {len(bundle_data)} bytes")
|
|
388
|
+
return bundle_data
|
|
389
|
+
except subprocess.CalledProcessError as e:
|
|
390
|
+
self.logger.warning(f"Failed to create state bundle: {e.stderr}")
|
|
391
|
+
return None
|
|
392
|
+
|
|
393
|
+
async def _create_and_upload_checkpoint(self) -> dict[str, Any] | None:
|
|
394
|
+
"""Create a full checkpoint including env snapshots and state bundle.
|
|
395
|
+
|
|
396
|
+
This method:
|
|
397
|
+
1. Commits any pending state changes
|
|
398
|
+
2. Creates env snapshots using snapshot_store
|
|
399
|
+
3. Creates and uploads state bundle as an artifact
|
|
400
|
+
4. Calls the checkpoint endpoint with all data
|
|
401
|
+
|
|
402
|
+
Returns:
|
|
403
|
+
Checkpoint result dict if successful, None otherwise.
|
|
404
|
+
"""
|
|
405
|
+
# Commit state changes first
|
|
406
|
+
self._commit_state(f"Checkpoint at step {self._step_count}")
|
|
407
|
+
|
|
408
|
+
# Create env snapshots
|
|
409
|
+
env_snapshots = await self._create_checkpoint()
|
|
410
|
+
if env_snapshots is None:
|
|
411
|
+
env_snapshots = {}
|
|
412
|
+
|
|
413
|
+
# Create and upload state bundle
|
|
414
|
+
state_artifact_id: str | None = None
|
|
415
|
+
if self.config.state.enabled:
|
|
416
|
+
bundle_data = self._create_state_bundle()
|
|
417
|
+
if bundle_data:
|
|
418
|
+
result = await _upload_artifact(
|
|
419
|
+
data=bundle_data,
|
|
420
|
+
artifact_type="state",
|
|
421
|
+
filename=f"state_step_{self._step_count}.bundle",
|
|
422
|
+
extra={
|
|
423
|
+
"step_number": self._step_count,
|
|
424
|
+
"state_path": self.config.state.path,
|
|
425
|
+
},
|
|
426
|
+
)
|
|
427
|
+
if result:
|
|
428
|
+
state_artifact_id = result.get("artifact_id")
|
|
429
|
+
self.logger.info(f"Uploaded state artifact: {state_artifact_id}")
|
|
430
|
+
|
|
431
|
+
# Upload checkpoint with all data
|
|
432
|
+
checkpoint_result = await _upload_checkpoint(
|
|
433
|
+
step_number=self._step_count,
|
|
434
|
+
env_snapshots=env_snapshots,
|
|
435
|
+
state_artifact_id=state_artifact_id,
|
|
436
|
+
extra={
|
|
437
|
+
"world_name": self.name,
|
|
438
|
+
"world_version": self.get_version(),
|
|
439
|
+
},
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
return checkpoint_result
|
|
443
|
+
|
|
198
444
|
def get_env(self, alias: str) -> Environment | None:
|
|
199
445
|
"""Get an environment by alias.
|
|
200
446
|
|
|
@@ -236,6 +482,9 @@ class BaseWorld(ABC, Generic[ConfigT]):
|
|
|
236
482
|
Returns:
|
|
237
483
|
Dict of environment variable name -> value
|
|
238
484
|
|
|
485
|
+
Raises:
|
|
486
|
+
ImportError: If a sim environment is configured but package is not installed.
|
|
487
|
+
|
|
239
488
|
Example:
|
|
240
489
|
env_vars = self.get_sim_env_vars()
|
|
241
490
|
# Returns: {"AWS_ENDPOINT_URL": "https://...", "GITEA_URL": "https://...", ...}
|
|
@@ -263,7 +512,13 @@ class BaseWorld(ABC, Generic[ConfigT]):
|
|
|
263
512
|
env_vars.update(sim_vars)
|
|
264
513
|
self.logger.info(f"{package_name} env vars: {list(sim_vars.keys())}")
|
|
265
514
|
except ImportError:
|
|
266
|
-
|
|
515
|
+
raise ImportError(
|
|
516
|
+
f"Environment '{env_alias}' is configured but 'plato.sims.{package_name}' "
|
|
517
|
+
f"package is not installed.\n\n"
|
|
518
|
+
f"Install sims packages:\n"
|
|
519
|
+
f' export INDEX_URL="https://__token__:${{PLATO_API_KEY}}@plato.so/api/v2/pypi/sims/simple/"\n'
|
|
520
|
+
f" uv pip install '.[sims]' --extra-index-url $INDEX_URL"
|
|
521
|
+
) from None
|
|
267
522
|
except Exception as e:
|
|
268
523
|
self.logger.warning(f"Failed to get {package_name} env vars: {e}")
|
|
269
524
|
|
|
@@ -278,6 +533,9 @@ class BaseWorld(ABC, Generic[ConfigT]):
|
|
|
278
533
|
Returns:
|
|
279
534
|
Markdown string with instructions, or empty string if no sims configured.
|
|
280
535
|
|
|
536
|
+
Raises:
|
|
537
|
+
ImportError: If a sim environment is configured but package is not installed.
|
|
538
|
+
|
|
281
539
|
Example:
|
|
282
540
|
instructions = self.get_sim_instructions()
|
|
283
541
|
# Returns markdown with LocalStack/Gitea setup instructions
|
|
@@ -306,7 +564,13 @@ class BaseWorld(ABC, Generic[ConfigT]):
|
|
|
306
564
|
instructions_parts.append(instructions)
|
|
307
565
|
self.logger.info(f"Added {package_name} instructions to prompt")
|
|
308
566
|
except ImportError:
|
|
309
|
-
|
|
567
|
+
raise ImportError(
|
|
568
|
+
f"Environment '{env_alias}' is configured but 'plato.sims.{package_name}' "
|
|
569
|
+
f"package is not installed.\n\n"
|
|
570
|
+
f"Install sims packages:\n"
|
|
571
|
+
f' export INDEX_URL="https://__token__:${{PLATO_API_KEY}}@plato.so/api/v2/pypi/sims/simple/"\n'
|
|
572
|
+
f" uv pip install '.[sims]' --extra-index-url $INDEX_URL"
|
|
573
|
+
) from None
|
|
310
574
|
except Exception as e:
|
|
311
575
|
self.logger.warning(f"Failed to get {package_name} instructions: {e}")
|
|
312
576
|
|
|
@@ -363,6 +627,9 @@ The following services are available for your use:
|
|
|
363
627
|
|
|
364
628
|
self.logger.info(f"Starting world '{self.name}'")
|
|
365
629
|
|
|
630
|
+
# Initialize state directory (creates git repo if needed)
|
|
631
|
+
self._init_state_directory()
|
|
632
|
+
|
|
366
633
|
# Initialize the logging singleton for agents to use
|
|
367
634
|
if config.callback_url and config.session_id:
|
|
368
635
|
_init_chronos_logging(
|
|
@@ -415,6 +682,13 @@ The following services are available for your use:
|
|
|
415
682
|
|
|
416
683
|
self.logger.info(f"Step {self._step_count}: done={result.done}")
|
|
417
684
|
|
|
685
|
+
# Create checkpoint if enabled and interval matches
|
|
686
|
+
# Note: The checkpoint event is created by the callback endpoint,
|
|
687
|
+
# so we don't need a span wrapper here (would create duplicates)
|
|
688
|
+
if self.config.checkpoint.enabled and self._step_count % self.config.checkpoint.interval == 0:
|
|
689
|
+
self.logger.info(f"Creating checkpoint after step {self._step_count}")
|
|
690
|
+
await self._create_and_upload_checkpoint()
|
|
691
|
+
|
|
418
692
|
if result.done:
|
|
419
693
|
break
|
|
420
694
|
|
plato/worlds/config.py
CHANGED
|
@@ -72,6 +72,36 @@ class Env:
|
|
|
72
72
|
self.required = required
|
|
73
73
|
|
|
74
74
|
|
|
75
|
+
class StateConfig(BaseModel):
|
|
76
|
+
"""Configuration for world state persistence.
|
|
77
|
+
|
|
78
|
+
The state directory is a git-tracked directory that persists across checkpoints.
|
|
79
|
+
At each checkpoint, the state directory is git bundled and uploaded as an artifact.
|
|
80
|
+
On restore, bootstrap.sh downloads and unbundles the state before the world starts.
|
|
81
|
+
|
|
82
|
+
Attributes:
|
|
83
|
+
enabled: Whether to enable state persistence (default: True).
|
|
84
|
+
path: Path to the state directory (default: /state).
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
enabled: bool = True
|
|
88
|
+
path: str = "/state"
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class CheckpointConfig(BaseModel):
|
|
92
|
+
"""Configuration for automatic checkpointing during world execution.
|
|
93
|
+
|
|
94
|
+
Attributes:
|
|
95
|
+
enabled: Whether to enable automatic checkpoints after steps.
|
|
96
|
+
interval: Create checkpoint every N steps (default: 1 = every step).
|
|
97
|
+
exclude_envs: Environment aliases to exclude from checkpoints (default: ["runtime"]).
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
enabled: bool = True
|
|
101
|
+
interval: int = 1
|
|
102
|
+
exclude_envs: list[str] = Field(default_factory=lambda: ["runtime"])
|
|
103
|
+
|
|
104
|
+
|
|
75
105
|
class RunConfig(BaseModel):
|
|
76
106
|
"""Base configuration for running a world.
|
|
77
107
|
|
|
@@ -98,6 +128,7 @@ class RunConfig(BaseModel):
|
|
|
98
128
|
session_id: Unique Chronos session identifier
|
|
99
129
|
callback_url: Callback URL for status updates
|
|
100
130
|
plato_session: Serialized Plato session for connecting to existing VM session
|
|
131
|
+
checkpoint: Configuration for automatic checkpoints after steps
|
|
101
132
|
"""
|
|
102
133
|
|
|
103
134
|
session_id: str = ""
|
|
@@ -108,6 +139,12 @@ class RunConfig(BaseModel):
|
|
|
108
139
|
# This is the output of Session.dump() - used to restore session with Session.load()
|
|
109
140
|
plato_session: SerializedSession | None = None
|
|
110
141
|
|
|
142
|
+
# Checkpoint configuration for automatic snapshots after steps
|
|
143
|
+
checkpoint: CheckpointConfig = Field(default_factory=CheckpointConfig)
|
|
144
|
+
|
|
145
|
+
# State persistence configuration
|
|
146
|
+
state: StateConfig = Field(default_factory=StateConfig)
|
|
147
|
+
|
|
111
148
|
model_config = {"extra": "allow"}
|
|
112
149
|
|
|
113
150
|
@classmethod
|
|
@@ -145,7 +182,7 @@ class RunConfig(BaseModel):
|
|
|
145
182
|
envs = []
|
|
146
183
|
|
|
147
184
|
# Skip runtime fields
|
|
148
|
-
runtime_fields = {"session_id", "callback_url", "all_secrets", "plato_session"}
|
|
185
|
+
runtime_fields = {"session_id", "callback_url", "all_secrets", "plato_session", "checkpoint", "state"}
|
|
149
186
|
|
|
150
187
|
for field_name, prop_schema in properties.items():
|
|
151
188
|
if field_name in runtime_fields:
|
plato/worlds/runner.py
CHANGED
|
@@ -246,12 +246,49 @@ def _extract_agent_images_from_config(config_data: dict) -> list[str]:
|
|
|
246
246
|
return images
|
|
247
247
|
|
|
248
248
|
|
|
249
|
+
async def _create_chronos_session(
|
|
250
|
+
chronos_url: str,
|
|
251
|
+
api_key: str,
|
|
252
|
+
world_name: str,
|
|
253
|
+
world_config: dict,
|
|
254
|
+
plato_session_id: str | None = None,
|
|
255
|
+
) -> tuple[str, str]:
|
|
256
|
+
"""Create a session in Chronos.
|
|
257
|
+
|
|
258
|
+
Args:
|
|
259
|
+
chronos_url: Chronos base URL (e.g., https://chronos.plato.so)
|
|
260
|
+
api_key: Plato API key for authentication
|
|
261
|
+
world_name: Name of the world being run
|
|
262
|
+
world_config: World configuration dict
|
|
263
|
+
plato_session_id: Optional Plato session ID if already created
|
|
264
|
+
|
|
265
|
+
Returns:
|
|
266
|
+
Tuple of (session_id, callback_url)
|
|
267
|
+
"""
|
|
268
|
+
import httpx
|
|
269
|
+
|
|
270
|
+
url = f"{chronos_url.rstrip('/')}/api/sessions"
|
|
271
|
+
|
|
272
|
+
async with httpx.AsyncClient(timeout=30.0) as client:
|
|
273
|
+
response = await client.post(
|
|
274
|
+
url,
|
|
275
|
+
json={
|
|
276
|
+
"world_name": world_name,
|
|
277
|
+
"world_config": world_config,
|
|
278
|
+
"plato_session_id": plato_session_id,
|
|
279
|
+
},
|
|
280
|
+
headers={"x-api-key": api_key},
|
|
281
|
+
)
|
|
282
|
+
response.raise_for_status()
|
|
283
|
+
data = response.json()
|
|
284
|
+
|
|
285
|
+
return data["public_id"], data["callback_url"]
|
|
286
|
+
|
|
287
|
+
|
|
249
288
|
async def _run_dev(
|
|
250
289
|
world_name: str,
|
|
251
290
|
config_path: Path,
|
|
252
291
|
env_timeout: int = 600,
|
|
253
|
-
chronos_url: str | None = None,
|
|
254
|
-
api_key: str | None = None,
|
|
255
292
|
agents_dir: Path | None = None,
|
|
256
293
|
) -> None:
|
|
257
294
|
"""Run a world locally with automatic environment creation.
|
|
@@ -260,22 +297,31 @@ async def _run_dev(
|
|
|
260
297
|
1. Load and parse the config
|
|
261
298
|
2. Build local agent images if --agents-dir is provided
|
|
262
299
|
3. Create Plato session with all environments
|
|
263
|
-
4.
|
|
300
|
+
4. Create Chronos session for logging/callbacks
|
|
264
301
|
5. Run the world with the session attached
|
|
265
302
|
|
|
303
|
+
Requires environment variables:
|
|
304
|
+
CHRONOS_URL: Chronos base URL (e.g., https://chronos.plato.so)
|
|
305
|
+
PLATO_API_KEY: API key for Plato and Chronos authentication
|
|
306
|
+
|
|
266
307
|
Args:
|
|
267
308
|
world_name: Name of the world to run
|
|
268
309
|
config_path: Path to the config JSON file
|
|
269
310
|
env_timeout: Timeout for environment creation (seconds)
|
|
270
|
-
chronos_url: Optional Chronos base URL for sending log events
|
|
271
|
-
api_key: Optional Plato API key (used for Chronos session creation)
|
|
272
311
|
agents_dir: Optional directory containing agent source code
|
|
273
312
|
"""
|
|
274
|
-
from uuid import uuid4
|
|
275
|
-
|
|
276
313
|
from plato.v2 import AsyncPlato
|
|
277
314
|
from plato.worlds.base import get_world
|
|
278
315
|
|
|
316
|
+
# Get required env vars
|
|
317
|
+
chronos_url = os.environ.get("CHRONOS_URL")
|
|
318
|
+
api_key = os.environ.get("PLATO_API_KEY")
|
|
319
|
+
|
|
320
|
+
if not chronos_url:
|
|
321
|
+
raise ValueError("CHRONOS_URL environment variable is required")
|
|
322
|
+
if not api_key:
|
|
323
|
+
raise ValueError("PLATO_API_KEY environment variable is required")
|
|
324
|
+
|
|
279
325
|
discover_worlds()
|
|
280
326
|
|
|
281
327
|
world_cls = get_world(world_name)
|
|
@@ -316,29 +362,14 @@ async def _run_dev(
|
|
|
316
362
|
# Create Plato client
|
|
317
363
|
plato = AsyncPlato()
|
|
318
364
|
session = None
|
|
319
|
-
|
|
320
|
-
# Initialize Chronos logging if URL provided
|
|
321
|
-
chronos_session_id: str | None = None
|
|
322
|
-
if chronos_url:
|
|
323
|
-
from plato.agents import init_logging
|
|
324
|
-
|
|
325
|
-
chronos_session_id = f"dev-{uuid4().hex[:8]}"
|
|
326
|
-
callback_url = f"{chronos_url.rstrip('/')}/api/v1/callback"
|
|
327
|
-
init_logging(
|
|
328
|
-
callback_url=callback_url,
|
|
329
|
-
session_id=chronos_session_id,
|
|
330
|
-
)
|
|
331
|
-
logger.info(f"Chronos logging enabled: {callback_url} (session: {chronos_session_id})")
|
|
332
|
-
|
|
333
|
-
# Update run_config with session info for agents
|
|
334
|
-
run_config.session_id = chronos_session_id
|
|
335
|
-
run_config.callback_url = callback_url
|
|
365
|
+
plato_session_id: str | None = None
|
|
336
366
|
|
|
337
367
|
try:
|
|
338
368
|
if env_configs:
|
|
339
369
|
logger.info(f"Creating {len(env_configs)} environments...")
|
|
340
370
|
session = await plato.sessions.create(envs=env_configs, timeout=env_timeout)
|
|
341
|
-
|
|
371
|
+
plato_session_id = session.session_id
|
|
372
|
+
logger.info(f"Created Plato session: {plato_session_id}")
|
|
342
373
|
logger.info(f"Environments: {[e.alias for e in session.envs]}")
|
|
343
374
|
|
|
344
375
|
# Serialize and add to config
|
|
@@ -347,6 +378,30 @@ async def _run_dev(
|
|
|
347
378
|
else:
|
|
348
379
|
logger.info("No environments defined for this world")
|
|
349
380
|
|
|
381
|
+
# Create Chronos session (after Plato session so we can link them)
|
|
382
|
+
logger.info(f"Creating Chronos session at {chronos_url}...")
|
|
383
|
+
chronos_session_id, callback_url = await _create_chronos_session(
|
|
384
|
+
chronos_url=chronos_url,
|
|
385
|
+
api_key=api_key,
|
|
386
|
+
world_name=world_name,
|
|
387
|
+
world_config=config_data,
|
|
388
|
+
plato_session_id=plato_session_id,
|
|
389
|
+
)
|
|
390
|
+
logger.info(f"Created Chronos session: {chronos_session_id}")
|
|
391
|
+
logger.info(f"View at: {chronos_url}/sessions/{chronos_session_id}")
|
|
392
|
+
|
|
393
|
+
# Initialize logging
|
|
394
|
+
from plato.agents import init_logging
|
|
395
|
+
|
|
396
|
+
init_logging(
|
|
397
|
+
callback_url=callback_url,
|
|
398
|
+
session_id=chronos_session_id,
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
# Update run_config with session info for agents
|
|
402
|
+
run_config.session_id = chronos_session_id
|
|
403
|
+
run_config.callback_url = callback_url
|
|
404
|
+
|
|
350
405
|
# Run the world
|
|
351
406
|
logger.info(f"Starting world '{world_name}'...")
|
|
352
407
|
world_instance = world_cls()
|
|
@@ -360,10 +415,9 @@ async def _run_dev(
|
|
|
360
415
|
await plato.close()
|
|
361
416
|
|
|
362
417
|
# Reset logging
|
|
363
|
-
|
|
364
|
-
from plato.agents import reset_logging
|
|
418
|
+
from plato.agents import reset_logging
|
|
365
419
|
|
|
366
|
-
|
|
420
|
+
reset_logging()
|
|
367
421
|
|
|
368
422
|
|
|
369
423
|
def _setup_colored_logging(verbose: bool = False) -> None:
|
|
@@ -411,10 +465,6 @@ def dev(
|
|
|
411
465
|
world: Annotated[str, typer.Option("--world", "-w", help="World name to run")],
|
|
412
466
|
config: Annotated[Path, typer.Option("--config", "-c", help="Path to config JSON file")],
|
|
413
467
|
env_timeout: Annotated[int, typer.Option("--env-timeout", help="Timeout for environment creation (seconds)")] = 600,
|
|
414
|
-
chronos_url: Annotated[
|
|
415
|
-
str | None, typer.Option("--chronos-url", help="Chronos base URL for log events (e.g., http://localhost:8000)")
|
|
416
|
-
] = None,
|
|
417
|
-
api_key: Annotated[str | None, typer.Option("--api-key", help="Plato API key for Chronos authentication")] = None,
|
|
418
468
|
agents_dir: Annotated[
|
|
419
469
|
Path | None,
|
|
420
470
|
typer.Option("--agents-dir", "-a", help="Directory containing agent source code (builds local images)"),
|
|
@@ -423,10 +473,8 @@ def dev(
|
|
|
423
473
|
) -> None:
|
|
424
474
|
"""Run a world locally for development/debugging.
|
|
425
475
|
|
|
426
|
-
This creates Plato environments automatically (like Chronos does)
|
|
427
|
-
and runs the world
|
|
428
|
-
|
|
429
|
-
Optionally sends log events to a Chronos server for real-time monitoring.
|
|
476
|
+
This creates Plato environments automatically (like Chronos does),
|
|
477
|
+
creates a Chronos session for logging, and runs the world.
|
|
430
478
|
|
|
431
479
|
Example config.json:
|
|
432
480
|
{
|
|
@@ -440,18 +488,16 @@ def dev(
|
|
|
440
488
|
}
|
|
441
489
|
}
|
|
442
490
|
|
|
443
|
-
|
|
444
|
-
|
|
491
|
+
Required environment variables:
|
|
492
|
+
CHRONOS_URL: Chronos base URL (e.g., https://chronos.plato.so)
|
|
493
|
+
PLATO_API_KEY: API key for Plato and Chronos authentication
|
|
445
494
|
|
|
446
495
|
Examples:
|
|
447
496
|
# Basic usage
|
|
448
|
-
plato-world-runner dev -w code -c config.json
|
|
497
|
+
CHRONOS_URL=https://chronos.plato.so plato-world-runner dev -w code -c config.json
|
|
449
498
|
|
|
450
499
|
# With local agent builds (from plato-client repo)
|
|
451
500
|
plato-world-runner dev -w code -c config.json --agents-dir ~/plato-client/agents
|
|
452
|
-
|
|
453
|
-
# With Chronos logging
|
|
454
|
-
plato-world-runner dev -w code -c config.json --chronos-url http://localhost:8000
|
|
455
501
|
"""
|
|
456
502
|
# Setup colored logging with filtered noisy loggers
|
|
457
503
|
_setup_colored_logging(verbose)
|
|
@@ -460,12 +506,16 @@ def dev(
|
|
|
460
506
|
typer.echo(f"Error: Config file not found: {config}", err=True)
|
|
461
507
|
raise typer.Exit(1)
|
|
462
508
|
|
|
509
|
+
if not os.environ.get("CHRONOS_URL"):
|
|
510
|
+
typer.echo("Error: CHRONOS_URL environment variable required", err=True)
|
|
511
|
+
raise typer.Exit(1)
|
|
512
|
+
|
|
463
513
|
if not os.environ.get("PLATO_API_KEY"):
|
|
464
514
|
typer.echo("Error: PLATO_API_KEY environment variable required", err=True)
|
|
465
515
|
raise typer.Exit(1)
|
|
466
516
|
|
|
467
517
|
try:
|
|
468
|
-
asyncio.run(_run_dev(world, config, env_timeout,
|
|
518
|
+
asyncio.run(_run_dev(world, config, env_timeout, agents_dir))
|
|
469
519
|
except Exception as e:
|
|
470
520
|
logger.exception(f"World execution failed: {e}")
|
|
471
521
|
raise typer.Exit(1)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: plato-sdk-v2
|
|
3
|
-
Version: 2.3.
|
|
3
|
+
Version: 2.3.2
|
|
4
4
|
Summary: Python SDK for the Plato API
|
|
5
5
|
Author-email: Plato <support@plato.so>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -44,6 +44,7 @@ Description-Content-Type: text/markdown
|
|
|
44
44
|
|
|
45
45
|
# Plato Python SDK
|
|
46
46
|
|
|
47
|
+
|
|
47
48
|
Python SDK for the Plato platform. Uses [Harbor](https://harborframework.com) for agent execution.
|
|
48
49
|
|
|
49
50
|
## Installation
|
|
@@ -297,11 +297,11 @@ plato/_sims_generator/templates/python/errors.py.jinja,sha256=8L_FbHczBNLXJrbSlN
|
|
|
297
297
|
plato/_sims_generator/templates/python/package_init.py.jinja,sha256=sOcJxUT0LuOWu5jOMGGKYxfCEjcYQv1hGF3n0iOA4hQ,986
|
|
298
298
|
plato/_sims_generator/templates/python/tag_init.py.jinja,sha256=WB_9cv0JKIVg5TOXeSolET3tAfVg7sExjboh5jbCXz4,170
|
|
299
299
|
plato/_sims_generator/templates/python/version_init.py.jinja,sha256=sGvFcYVfzXFyQDAe0PSOrg9yys93KE0XInFQNb1TvCY,179
|
|
300
|
-
plato/agents/__init__.py,sha256=
|
|
300
|
+
plato/agents/__init__.py,sha256=1l-Vw5SFydmPsl1toXdc2oH3eLhFwz6jtI_DKUx4vmo,2847
|
|
301
301
|
plato/agents/base.py,sha256=vUbPQuNSo6Ka2lIB_ZOXgi4EoAjtAD7GIj9LnNotam0,4577
|
|
302
302
|
plato/agents/build.py,sha256=CNMbVQFs2_pYit1dA29Davve28Yi4c7TNK9wBB7odrE,1621
|
|
303
303
|
plato/agents/config.py,sha256=VZVMdCmEQnoR0VkrGdScG8p6zSKVFe7BZPd2h8lKNjI,5460
|
|
304
|
-
plato/agents/logging.py,sha256=
|
|
304
|
+
plato/agents/logging.py,sha256=bL3Q14YeFxWCQydGKrQzQPgKQzBoCCXy_AAQPZpBylU,16307
|
|
305
305
|
plato/agents/runner.py,sha256=k3t6TZYfk0K4efpjNUmMbRd76KPyiQs5BRCfU53RZHA,6721
|
|
306
306
|
plato/agents/trajectory.py,sha256=WdiBmua0KvCrNaM3qgPI7-7B4xmSkfbP4oZ_9_8qHzU,10529
|
|
307
307
|
plato/chronos/__init__.py,sha256=RHMvSrQS_-vkKOyTRuAkp2gKDP1HEuBLDnw8jcZs1Jg,739
|
|
@@ -463,12 +463,12 @@ plato/v2/utils/db_cleanup.py,sha256=lnI5lsMHNHpG85Y99MaE4Rzc3618piuzhvH-uXO1zIc,
|
|
|
463
463
|
plato/v2/utils/models.py,sha256=PwehSSnIRG-tM3tWL1PzZEH77ZHhIAZ9R0UPs6YknbM,1441
|
|
464
464
|
plato/v2/utils/proxy_tunnel.py,sha256=8ZTd0jCGSfIHMvSv1fgEyacuISWnGPHLPbDglWroTzY,10463
|
|
465
465
|
plato/worlds/README.md,sha256=TgG4aidude0ouJSCfY81Ev45hsUxPkO85HUIiWNqkcc,5463
|
|
466
|
-
plato/worlds/__init__.py,sha256=
|
|
467
|
-
plato/worlds/base.py,sha256=
|
|
466
|
+
plato/worlds/__init__.py,sha256=ALoou3l5lXvs_YZc5eH6HdMHpvhnpzKWqz__aSC1jFc,2152
|
|
467
|
+
plato/worlds/base.py,sha256=KsDq1TVVd4BKEFo42WodF4DPgodr00Gd1NrfzZEPqyw,25632
|
|
468
468
|
plato/worlds/build_hook.py,sha256=KSoW0kqa5b7NyZ7MYOw2qsZ_2FkWuz0M3Ru7AKOP7Qw,3486
|
|
469
|
-
plato/worlds/config.py,sha256=
|
|
470
|
-
plato/worlds/runner.py,sha256=
|
|
471
|
-
plato_sdk_v2-2.3.
|
|
472
|
-
plato_sdk_v2-2.3.
|
|
473
|
-
plato_sdk_v2-2.3.
|
|
474
|
-
plato_sdk_v2-2.3.
|
|
469
|
+
plato/worlds/config.py,sha256=fkWoetCawIPf5xgj5EyJCvopmPPzZQAqt0Ke9P4hq5c,10845
|
|
470
|
+
plato/worlds/runner.py,sha256=306dZrH1NSUxXcA4oJSlmOfSSfwHtH7KMquPvgxxbCI,17255
|
|
471
|
+
plato_sdk_v2-2.3.2.dist-info/METADATA,sha256=0Qy3LhEnq8ZEKOcQgkV0t9Kcd37I6X2_7fClGV6ZHtg,8509
|
|
472
|
+
plato_sdk_v2-2.3.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
473
|
+
plato_sdk_v2-2.3.2.dist-info/entry_points.txt,sha256=upGMbJCx6YWUTKrPoYvYUYfFCqYr75nHDwhA-45m6p8,136
|
|
474
|
+
plato_sdk_v2-2.3.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|