Skip to content

Commit 5382377

Browse files
refactor: replace session_id with session_dir in Node and save session data in worker
1 parent 03c30ee commit 5382377

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

MaxKernel/auto_search/graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class EvaluationResult(BaseModel):
2020
class Node(BaseModel):
2121
node_id: str
2222
parent_id: Optional[str]
23-
session_id: str
23+
session_dir: Optional[str] = None
2424
code: str
2525
plan: str
2626
depth: int

MaxKernel/auto_search/worker.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import logging
23
import os
34
from typing import Any, Dict, Optional
@@ -13,7 +14,7 @@
1314
class ADKSessionWorker:
1415
async def expand_node(
1516
self,
16-
session_id: str,
17+
node_id: str,
1718
parent_node: Node,
1819
session_dir: str,
1920
strategy: Optional[str] = None,
@@ -28,23 +29,24 @@ async def expand_node(
2829
# Run the agent and process results
2930
try:
3031
state = await self._run_agent(
31-
session_id=session_id,
32+
node_id=node_id,
3233
session_dir=session_dir,
3334
strategy=strategy,
3435
agent_config=agent_config,
3536
)
3637
return self._process_results(
37-
session_id=session_id,
38+
node_id=node_id,
3839
parent_node=parent_node,
3940
strategy=strategy,
4041
state=state,
42+
session_dir=session_dir,
4143
)
4244
except Exception as e:
43-
logger.exception(f"Session {session_id} failed: {e}")
45+
logger.exception(f"Node {node_id} expansion failed: {e}")
4446
return Node(
45-
node_id=session_id,
47+
node_id=node_id,
4648
parent_id=parent_node.node_id,
47-
session_id=session_id,
49+
session_dir=session_dir,
4850
code="",
4951
plan="",
5052
depth=parent_node.depth + 1,
@@ -72,7 +74,7 @@ def _prepare_inputs(self, session_dir: str, parent_node: Node) -> None:
7274

7375
async def _run_agent(
7476
self,
75-
session_id: str,
77+
node_id: str,
7678
session_dir: str,
7779
strategy: Optional[str],
7880
agent_config: Optional[Dict[str, Any]] = None,
@@ -101,20 +103,28 @@ async def _run_agent(
101103
)
102104
client = AutoAgentClient(
103105
user_id="orchestrator",
104-
session_id=session_id,
106+
session_id=node_id,
105107
query=query,
106108
agent=custom_agent,
107109
)
108110
await client.create_session()
109111
await client.run_async()
112+
try:
113+
session_json_path = os.path.join(session_dir, "session.json")
114+
with open(session_json_path, "w") as f:
115+
json.dump(client.get_session_data(), f, indent=2)
116+
logger.info(f"Saved session data to {session_json_path}")
117+
except Exception as se:
118+
logger.warning(f"Failed to save session data for {node_id}: {se}")
110119
return client.get_state()
111120

112121
def _process_results(
113122
self,
114-
session_id: str,
123+
node_id: str,
115124
parent_node: Node,
116125
strategy: Optional[str],
117126
state: Dict[str, Any],
127+
session_dir: str,
118128
) -> Node:
119129
"""Reads optimized code/plan and extracts metrics to construct a new Node."""
120130
history = state.get("history", [])
@@ -154,9 +164,9 @@ def _process_results(
154164
optimized_plan = f.read()
155165

156166
return Node(
157-
node_id=session_id,
167+
node_id=node_id,
158168
parent_id=parent_node.node_id,
159-
session_id=session_id,
169+
session_dir=session_dir,
160170
code=optimized_code,
161171
plan=optimized_plan,
162172
depth=parent_node.depth + 1,

0 commit comments

Comments
 (0)