-
Notifications
You must be signed in to change notification settings - Fork 589
[feat] Reuse Daytona sandboxes across turns instead of deleting them every turn #5225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a3bbcb9
feat(runner): warm daytona sessions slice 1 - provider pause/reconnec…
mmabrouk 51b18fb
feat(runner): warm daytona sessions, slices 2-5 - park-to-stopped, pr…
mmabrouk 20dbabb
feat(runner): drop sandbox fingerprint, converge network policy at re…
59e49b9
fix(runner): service CodeRabbit review on warm daytona sessions
a588b08
fix(hosting): restore the MCP gate default to off
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
|
|
||
| import uuid_utils.compat as uuid_utils | ||
|
|
||
| from sqlalchemy import select | ||
| from sqlalchemy import Integer, case, cast, func, select | ||
| from sqlalchemy.dialects.postgresql import insert | ||
|
|
||
| from oss.src.utils.logging import get_module_logger | ||
|
|
@@ -94,7 +94,23 @@ async def set_session_state( | |
| } | ||
| if "data" in upsert.model_fields_set: | ||
| update_values["data"] = stmt.excluded.data | ||
| if "sandbox_id" in upsert.model_fields_set: | ||
| guarded_pointer_write = ( | ||
| "sandbox_id" in upsert.model_fields_set | ||
| and upsert.sandbox_turn_index is not None | ||
| ) | ||
| if guarded_pointer_write: | ||
| pointer_write_allowed = ( | ||
| func.coalesce( | ||
| cast(SessionStateDBE.data["latest_turn_index"].astext, Integer), | ||
| -1, | ||
| ) | ||
| <= upsert.sandbox_turn_index | ||
| ) | ||
| update_values["sandbox_id"] = case( | ||
| (pointer_write_allowed, stmt.excluded.sandbox_id), | ||
| else_=SessionStateDBE.sandbox_id, | ||
| ) | ||
| elif "sandbox_id" in upsert.model_fields_set: | ||
| update_values["sandbox_id"] = stmt.excluded.sandbox_id | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kept: the turn-index compare-and-set on sandbox_id (guarded_pointer_write above). This is the multi-runner safety mechanism and it uses only pre-existing columns. Only the fingerprint branch was removed here, so a stale write still cannot overwrite a newer pointer. |
||
|
|
||
| stmt = stmt.on_conflict_do_update( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
docs/design/agent-workflows/projects/qa/scripts/warm_daytona_probe.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # /// script | ||
| # requires-python = ">=3.10" | ||
| # dependencies = ["httpx>=0.27"] | ||
| # /// | ||
| """Warm-daytona verification probe: timed turns against one session. | ||
|
|
||
| Drives the live agent service /invoke with sandbox=daytona and measures wall time per turn. | ||
| Turn 1 (no session header) is the cold create; turn 2 reuses the returned session id | ||
| immediately (park-to-running hit); a later rerun with --session <id> exercises the | ||
| stopped-restart path after the live window expires. | ||
|
|
||
| Usage: | ||
| uv run warm_daytona_probe.py # cold turn + immediate warm turn | ||
| uv run warm_daytona_probe.py --session <id> # one more turn on an existing session | ||
| uv run warm_daytona_probe.py --turns 1 # single cold turn only | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| import os | ||
| import pathlib | ||
| import time | ||
|
|
||
| import httpx | ||
|
|
||
| BASE = os.environ.get("AGENTA_BASE", "http://localhost:8280") | ||
| PROJ = os.environ.get("AGENTA_PROJECT_ID", "019e8df5-2a58-7501-8fe2-56f7b332bd00") | ||
| MODEL = os.environ.get("AGENTA_QA_MODEL", "openai/gpt-4o-mini") | ||
|
|
||
|
|
||
| def api_key() -> str: | ||
| key = os.environ.get("AGENTA_API_KEY") | ||
| if key: | ||
| return key | ||
| repo = pathlib.Path(__file__).resolve().parents[6] | ||
| envf = repo / "examples/python/hotel_agent/draft/.env" | ||
| for line in envf.read_text().splitlines(): | ||
| if line.startswith("AGENTA_API_KEY="): | ||
| return line.split("=", 1)[1].strip() | ||
| raise SystemExit("no AGENTA_API_KEY in env or hotel-agent .env") | ||
|
|
||
|
|
||
| def turn( | ||
| client: httpx.Client, | ||
| key: str, | ||
| history: list[dict], | ||
| message: str, | ||
| session_id: str | None, | ||
| ) -> dict: | ||
| messages = [*history, {"role": "user", "content": message}] | ||
| body = { | ||
| "data": { | ||
| "inputs": {"messages": messages}, | ||
| "parameters": { | ||
| "agent": { | ||
| "harness": {"kind": "pi_core"}, | ||
| "sandbox": {"kind": "daytona"}, | ||
| "llm": {"model": MODEL}, | ||
| "instructions": { | ||
| "agents_md": "Reply with exactly the requested word and nothing else." | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
| } | ||
| headers = {"Authorization": f"ApiKey {key}", "content-type": "application/json"} | ||
| if session_id: | ||
| headers["x-ag-session-id"] = session_id | ||
| started = time.monotonic() | ||
| resp = client.post( | ||
| f"{BASE}/services/agent/v0/invoke", | ||
| params={"project_id": PROJ}, | ||
| headers=headers, | ||
| json=body, | ||
| timeout=240.0, | ||
| ) | ||
| elapsed = time.monotonic() - started | ||
| out: dict = {"wall_s": round(elapsed, 2), "http": resp.status_code} | ||
| try: | ||
| payload = resp.json() | ||
| out["session_id"] = payload.get("session_id") | ||
| data = payload.get("data") or {} | ||
| outputs = data.get("outputs") | ||
| if isinstance(outputs, dict) and isinstance(outputs.get("messages"), list): | ||
| for msg in outputs["messages"]: | ||
| if msg.get("role") == "assistant": | ||
| out["reply"] = (msg.get("content") or "")[:120] | ||
| elif isinstance(outputs, dict): | ||
| out["reply"] = (outputs.get("content") or "")[:120] | ||
| out["status_message"] = ((payload.get("status") or {}).get("message") or "")[ | ||
| :200 | ||
| ] | ||
| except Exception as exc: # noqa: BLE001 | ||
| out["parse_error"] = str(exc) | ||
| out["raw"] = resp.text[:300] | ||
| return out | ||
|
|
||
|
|
||
| def main() -> None: | ||
| ap = argparse.ArgumentParser() | ||
| ap.add_argument("--session", default=None) | ||
| ap.add_argument("--turns", type=int, default=2) | ||
| ap.add_argument( | ||
| "--wait-before-last", | ||
| type=float, | ||
| default=0.0, | ||
| help="seconds to sleep before the final turn (to outlive the live window)", | ||
| ) | ||
| args = ap.parse_args() | ||
|
|
||
| key = api_key() | ||
| session_id = args.session | ||
| history: list[dict] = [] | ||
| with httpx.Client() as client: | ||
| for index in range(args.turns): | ||
| if args.wait_before_last and index == args.turns - 1: | ||
| print(json.dumps({"sleeping_s": args.wait_before_last})) | ||
| time.sleep(args.wait_before_last) | ||
| word = f"PING{index + 1}" | ||
| prompt = f"Reply with exactly the word {word}" | ||
| result = turn(client, key, history, prompt, session_id) | ||
| session_id = result.get("session_id") or session_id | ||
| history.append({"role": "user", "content": prompt}) | ||
| history.append( | ||
| {"role": "assistant", "content": result.get("reply") or word} | ||
| ) | ||
| label = "cold" if index == 0 and not args.session else "warm-candidate" | ||
| print(json.dumps({"turn": index + 1, "label": label, **result})) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 The pointer guard: when the writer supplies
sandbox_turn_index,sandbox_idandsandbox_fingerprintupdate only under this CASE compare-and-set against the row'slatest_turn_index; a tokenless write behaves exactly as before, so existing callers are untouched. SQL note: insideON CONFLICT DO UPDATE, the table-qualified column refers to the EXISTING row andexcludedto the proposed one. The insert path (no existing row) applies unconditionally.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we removed sandbox_fingerprint is this still relevant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fingerprint is gone but this guard stays relevant: it is the turn-index compare-and-set on sandbox_id itself, the multi-runner safety mechanism. It uses only pre-existing columns. Commit 20dbabb removed the fingerprint branch below it.