Agent Python executor: fix hyphenated service names and improve sandboxing #26
Labels
No labels
prio_critical
prio_low
type_bug
type_contact
type_issue
type_lead
type_question
type_story
type_task
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
lhumina_code/hero_router#26
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
The AI agent Python executor in
server/agent.rsfails when service names contain hyphens (e.g.recipe-server). Python cannot import modules with hyphens in the name, causingModuleNotFoundError.Root cause
Invalid Python identifiers from hyphens:
derive_group_namereturns names likerecipe-serverfrom socket paths. The staged client file becomesrecipe-server_client.py, which Python cannot import because-is interpreted as the minus operator.to_class_name()only splits on_: It does not handle hyphens, sorecipe-serverbecomesRecipe-server-- also invalid as a Python class name.LLM suggests
pip installon failure: When the import fails, the retry LLM suggestspip install recipe_server_client, which is wrong -- the client is auto-generated, not a PyPI package.Error observed
Script failed after 4 attempts (14836ms).
Fix required
Normalize service names for Python: Replace hyphens with underscores in all Python-facing identifiers -- file names, import names, class names.
Fix
to_class_name(): Split on both_and-sorecipe-serverbecomesRecipeServer.Add explicit instruction to LLM system prompt: Tell the LLM to never use
pip install-- all client libraries are pre-staged and available for import.Broader: The Python execution environment should use
uv runfor sandboxed, isolated script execution rather than maintaining a persistent venv. This prevents side effects between runs and aligns with thehero_procsandboxing model.Fixed in branch
development_26(commit33e9848).Changes
python_codegen.rs:to_class_name()now splits on both_and-, sorecipe-servercorrectly becomesRecipeServerto_python_module_name()— normalizes hyphens to underscores for valid Python identifiersensure_python_files(),get_cached_client(),get_cached_interface()all use normalized names for cache file pathsserver/agent.rs:stage_client_library()now takes apy_moduleparameter for the Python-safe namepip install— instructs to only use stdlib + pre-staged clientWhat was happening
Service names like
recipe-server(from socket directory names) were used directly as Python module/file names. Python cannot import modules with hyphens (from recipe-server_client import ...fails because-is the minus operator). On failure, the LLM retry loop would suggestpip install recipe_server_clientwhich is wrong — the client is auto-generated.Remaining (tracked separately)
The broader sandboxing improvement (using
uv runfor isolated per-execution environments viahero_proc) is noted in the issue description but is a larger architectural change.Sandboxing improvement also done in commit
0907936.What changed
Replaced the entire persistent venv approach with
uv run --script --no-project --isolated.Before: Created and maintained a persistent venv at
~/.hero/var/router/python/venvwith a complex fallback chain (uv venv -> python3 -m venv). State could leak between runs, stale packages could accumulate.After: Each script execution gets a clean, ephemeral environment via
uv run --isolated. No venv to manage. Falls back topython3directly ifuvis not installed (since scripts only use stdlib + staged client files, no venv is needed).Net result: -51 lines of venv management code removed.
All items from the issue are now addressed:
to_class_name()for hyphensuv run --isolated