test_run_workspace.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. """
  2. Tests for PaaS Run Workspace — persistent run directories.
  3. Tests cover:
  4. 1. create_run_workspace() creates correct directory structure
  5. 2. save_run_artifacts() writes output/trace/cost files
  6. 3. Multiple runs preserve all history (no deletion)
  7. 4. Context carries workspace_path through fork/extend
  8. 5. Workspace not created when agent_dir is empty
  9. """
  10. import json
  11. import os
  12. import shutil
  13. import tempfile
  14. import pytest
  15. from agentpaas.engine.sandbox import (
  16. create_run_workspace,
  17. save_run_artifacts,
  18. ExecutionResult,
  19. )
  20. from lambdagent.core import Context, TraceEntry
  21. @pytest.fixture
  22. def agent_dir():
  23. """Create a temporary agent directory for testing."""
  24. d = tempfile.mkdtemp(prefix="test_agent_")
  25. yield d
  26. shutil.rmtree(d, ignore_errors=True)
  27. # ============================================================
  28. # 1. create_run_workspace()
  29. # ============================================================
  30. class TestCreateRunWorkspace:
  31. def test_creates_directory_structure(self, agent_dir):
  32. """Creates workspace/run_YYYYMMDD_HHMMSS/ with subdirs"""
  33. ws = create_run_workspace(agent_dir, "run_abc123", "hello", {"type": "simple"})
  34. assert os.path.isdir(ws)
  35. assert os.path.isdir(os.path.join(ws, "code"))
  36. assert os.path.isdir(os.path.join(ws, "results"))
  37. assert os.path.isdir(os.path.join(ws, "final"))
  38. def test_saves_input_json(self, agent_dir):
  39. """input.json contains run_id and input text"""
  40. ws = create_run_workspace(agent_dir, "run_abc123", "test input", {})
  41. with open(os.path.join(ws, "input.json")) as f:
  42. data = json.load(f)
  43. assert data["run_id"] == "run_abc123"
  44. assert data["input"] == "test input"
  45. def test_saves_config_yml(self, agent_dir):
  46. """config.yml is a snapshot of the agent config"""
  47. config = {"type": "react", "systemPrompt": "You are helpful"}
  48. ws = create_run_workspace(agent_dir, "run_abc123", "input", config)
  49. assert os.path.isfile(os.path.join(ws, "config.yml"))
  50. def test_workspace_under_agent_dir(self, agent_dir):
  51. """Workspace is under {agent_dir}/workspace/"""
  52. ws = create_run_workspace(agent_dir, "run_abc123", "input", {})
  53. assert ws.startswith(os.path.join(agent_dir, "workspace"))
  54. def test_run_dir_has_timestamp(self, agent_dir):
  55. """Run directory name matches run_YYYYMMDD_HHMMSS pattern"""
  56. ws = create_run_workspace(agent_dir, "run_abc123", "input", {})
  57. dirname = os.path.basename(ws)
  58. assert dirname.startswith("run_20")
  59. # ============================================================
  60. # 2. save_run_artifacts()
  61. # ============================================================
  62. class TestSaveRunArtifacts:
  63. def test_saves_output_json(self, agent_dir):
  64. ws = create_run_workspace(agent_dir, "run_123", "input", {})
  65. save_run_artifacts(ws, "final result", [], 1000, status="completed")
  66. with open(os.path.join(ws, "output.json")) as f:
  67. data = json.load(f)
  68. assert data["output"] == "final result"
  69. assert data["status"] == "completed"
  70. def test_saves_trace_json(self, agent_dir):
  71. ws = create_run_workspace(agent_dir, "run_123", "input", {})
  72. trace = [TraceEntry("agent1", "id1", "in", "out", 100.0, "model", 50)]
  73. save_run_artifacts(ws, "result", trace, 500)
  74. with open(os.path.join(ws, "trace.json")) as f:
  75. data = json.load(f)
  76. assert len(data) == 1
  77. assert data[0]["term_name"] == "agent1"
  78. assert data[0]["tokens_used"] == 50
  79. def test_saves_cost_json(self, agent_dir):
  80. ws = create_run_workspace(agent_dir, "run_123", "input", {})
  81. save_run_artifacts(ws, "result", [], 1500, input_tokens=100, steps=5)
  82. with open(os.path.join(ws, "cost.json")) as f:
  83. data = json.load(f)
  84. assert data["duration_ms"] == 1500
  85. assert data["input_tokens"] == 100
  86. assert data["steps"] == 5
  87. def test_saves_on_failure(self, agent_dir):
  88. ws = create_run_workspace(agent_dir, "run_123", "input", {})
  89. save_run_artifacts(ws, "", [], 500, error="boom", status="failed")
  90. with open(os.path.join(ws, "output.json")) as f:
  91. data = json.load(f)
  92. assert data["status"] == "failed"
  93. assert data["error"] == "boom"
  94. def test_noop_without_workspace(self):
  95. """save_run_artifacts does nothing if workspace_path is empty"""
  96. save_run_artifacts("", "result", [], 100) # Should not raise
  97. save_run_artifacts("/nonexistent/path", "result", [], 100) # Should not raise
  98. # ============================================================
  99. # 3. Multiple Runs Preserve History
  100. # ============================================================
  101. class TestMultipleRuns:
  102. def test_multiple_runs_coexist(self, agent_dir):
  103. """Each run creates a separate directory, old ones are not deleted"""
  104. import time
  105. ws1 = create_run_workspace(agent_dir, "run_aaa", "input1", {"v": 1})
  106. time.sleep(1.1) # Ensure different timestamp
  107. ws2 = create_run_workspace(agent_dir, "run_bbb", "input2", {"v": 2})
  108. assert ws1 != ws2
  109. assert os.path.isdir(ws1) # First run still exists
  110. assert os.path.isdir(ws2) # Second run also exists
  111. # Both have their own input files
  112. with open(os.path.join(ws1, "input.json")) as f:
  113. assert json.load(f)["run_id"] == "run_aaa"
  114. with open(os.path.join(ws2, "input.json")) as f:
  115. assert json.load(f)["run_id"] == "run_bbb"
  116. def test_workspace_dir_lists_all_runs(self, agent_dir):
  117. """workspace/ directory contains all run directories"""
  118. import time
  119. create_run_workspace(agent_dir, "run_1", "i1", {})
  120. time.sleep(1.1)
  121. create_run_workspace(agent_dir, "run_2", "i2", {})
  122. ws_dir = os.path.join(agent_dir, "workspace")
  123. runs = os.listdir(ws_dir)
  124. assert len(runs) == 2
  125. assert all(r.startswith("run_") for r in runs)
  126. # ============================================================
  127. # 4. Context Carries workspace_path
  128. # ============================================================
  129. class TestContextWorkspace:
  130. def test_context_workspace_path(self):
  131. ctx = Context(workspace_path="/tmp/test_ws", run_id="run_123")
  132. assert ctx.workspace_path == "/tmp/test_ws"
  133. assert ctx.run_id == "run_123"
  134. def test_fork_preserves_workspace(self):
  135. ctx = Context(workspace_path="/tmp/ws", run_id="run_abc")
  136. forked = ctx.fork()
  137. assert forked.workspace_path == "/tmp/ws"
  138. assert forked.run_id == "run_abc"
  139. def test_extend_preserves_workspace(self):
  140. ctx = Context(workspace_path="/tmp/ws", run_id="run_abc")
  141. child = ctx.extend(x=42)
  142. assert child.workspace_path == "/tmp/ws"
  143. assert child.run_id == "run_abc"
  144. def test_default_workspace_is_none(self):
  145. ctx = Context()
  146. assert ctx.workspace_path is None
  147. assert ctx.run_id is None
  148. # ============================================================
  149. # 5. No Workspace When agent_dir Empty
  150. # ============================================================
  151. class TestNoWorkspaceWithoutAgentDir:
  152. def test_execution_result_default_empty(self):
  153. r = ExecutionResult()
  154. assert r.workspace_path == ""