-
Notifications
You must be signed in to change notification settings - Fork 3
feat(agent): agent-selectable GPU/CPU per execute-code call with project allowance #160
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
39d82b0
feat(agent): agent-selectable GPU/CPU per execute-code call with proj…
d6c4bc9
merge: PR #145 — RunPod as an alternative compute provider to Modal (…
lucastononro 7175a56
fix(agent): gpu-choice review follow-ups — tool event order, owner ti…
lucastononro f7b9287
Merge branch 'staging-v0.0.5' into feat/agent-gpu-choice
lucastononro 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
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,92 @@ | ||
| """Compute allowance — which GPUs/CPUs the agent may request, and how long | ||
| a single execution may run. | ||
|
|
||
| One resolver, two consumers: the execute-code handler (enforcement) and | ||
| the system-prompt renderer in services/agent/runner.py (presentation). | ||
| Sharing it guarantees the prompt never advertises hardware the handler | ||
| would reject. | ||
|
|
||
| Semantics: | ||
| allowed = {"cpu"} ∪ {profile GPUs} ∪ (sandbox_config.allowed_gpus or ∅) | ||
| Profile GPUs are implicitly allowed — the agent can already reach | ||
| them via heavy=True, so denying them on the explicit arg would be | ||
| security theater. "cpu" is always allowed (it's the floor). | ||
| max_timeout = sandbox_config.max_timeout | ||
| or max(profile timeouts, settings.sandbox_timeout) | ||
| so the agent can't self-grant a longer run than the owner ever | ||
| configured. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
| from config import settings | ||
| from schemas import CANONICAL_GPUS | ||
|
|
||
| # Legacy / sloppy labels seen in old project configs and the pre-canonical | ||
| # frontend dropdown. Maps lowercase alias -> canonical label. | ||
| _LABEL_ALIASES: dict[str, str] = { | ||
| "a100": "A100-40GB", # the old frontend 'A100' option | ||
| "none": "cpu", | ||
| } | ||
|
|
||
| _CANONICAL_BY_LOWER = {g.lower(): g for g in CANONICAL_GPUS} | ||
|
|
||
|
|
||
| def normalize_gpu(label: str | None) -> str | None: | ||
| """Case-insensitive map of a user/agent-supplied label to its | ||
| canonical form. Returns None for unknown labels.""" | ||
| if not label: | ||
| return None | ||
| key = str(label).strip().lower() | ||
| key = _LABEL_ALIASES.get(key, key).lower() | ||
| return _CANONICAL_BY_LOWER.get(key) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ComputeAllowance: | ||
| allowed_gpus: list[str] # canonical labels incl. "cpu", cost-ascending | ||
| max_timeout: int # seconds | ||
| explicit: bool # owner set allowed_gpus (vs profile-implied only) | ||
|
|
||
| def permits(self, label: str) -> bool: | ||
| return label in self.allowed_gpus | ||
|
|
||
|
|
||
| def resolve_compute_allowance(sandbox_config: dict | None) -> ComputeAllowance: | ||
| cfg = sandbox_config or {} | ||
| profiles = [cfg.get("default") or {}, cfg.get("training") or {}] | ||
|
|
||
| allowed: set[str] = {"cpu"} | ||
| for profile in profiles: | ||
| normalized = normalize_gpu(profile.get("gpu")) | ||
| if normalized: | ||
| allowed.add(normalized) | ||
|
|
||
| configured = cfg.get("allowed_gpus") or None | ||
| if configured: | ||
| for label in configured: | ||
| normalized = normalize_gpu(label) | ||
| if normalized: | ||
| allowed.add(normalized) | ||
|
|
||
| max_timeout = cfg.get("max_timeout") or max( | ||
| [p.get("timeout") or 0 for p in profiles] + [settings.sandbox_timeout] | ||
| ) | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| order = {g: i for i, g in enumerate(CANONICAL_GPUS)} | ||
| return ComputeAllowance( | ||
| allowed_gpus=sorted(allowed, key=lambda g: order.get(g, 99)), | ||
| max_timeout=int(max_timeout), | ||
| explicit=bool(configured), | ||
| ) | ||
|
|
||
|
|
||
| def clamp_timeout(requested, allowance: ComputeAllowance) -> int | None: | ||
| """Clamp an agent-requested timeout into [10, allowance.max_timeout]. | ||
| Returns None for garbage input (caller falls back to the profile).""" | ||
| try: | ||
| return max(10, min(int(requested), allowance.max_timeout)) | ||
| except (TypeError, ValueError): | ||
| return None | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.