Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/agent_env/agents/agents/gemini/gemini_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

`~/.gemini/settings.json` holds what every run shares, the changelog's hook. Each run
gets settings of its own as the CLI's system settings, which override the user's: the
task's MCP servers with the turn's role header, and the turn's system prompt as a
context file named by its content. So turns running at once never share a role, and a
system prompt keeps one path from turn to turn.
task's MCP servers with the turn's role header, the task directories that exist as
included directories, and the turn's system prompt as a context file named by its content.
So turns running at once never share a role, and a system prompt keeps one path from turn
to turn.
"""
from __future__ import annotations

Expand All @@ -28,6 +29,10 @@
_DEFAULT_SYSTEM_SETTINGS = Path("/etc/gemini-cli/settings.json")
# The CLI's context file in a workspace, which a run with a system prompt still loads beside its own.
_WORKSPACE_CONTEXT_FILE = "GEMINI.md"
# Where tasks put files for the agent outside its workspace: /tmp/file_artifacts is load_artifact's default for an
# agent. The CLI's file tools refuse paths outside the workspace and its included directories, and it fails to start
# when an included directory is missing.
TASK_DIRS = ("/app", "/tmp/data", "/tmp/file_artifacts")
_prompt_files: Counter[Path] = Counter()
_prompt_files_lock = threading.Lock()

Expand Down Expand Up @@ -83,13 +88,14 @@ def run_settings(servers: Mapping[str, Mapping[str, Any]], role: str | None,
**(settings.get("mcpServers") if isinstance(settings.get("mcpServers"), dict) else {}),
**{settings_name(name): _server_entry(info["url"], info.get("headers"), role) for name, info in servers.items()},
}
context = dict(settings["context"]) if isinstance(settings.get("context"), dict) else {}
if task_dirs := [d for d in TASK_DIRS if os.path.isdir(d)]:
context["includeDirectories"] = list(dict.fromkeys([*(context.get("includeDirectories") or []), *task_dirs]))
with ExitStack() as stack:
if system_prompt:
context = stack.enter_context(_prompt_file(home, system_prompt))
settings["context"] = {
**(settings.get("context") if isinstance(settings.get("context"), dict) else {}),
"fileName": [_WORKSPACE_CONTEXT_FILE, context.name],
}
context["fileName"] = [_WORKSPACE_CONTEXT_FILE, stack.enter_context(_prompt_file(home, system_prompt)).name]
if context:
settings["context"] = context
path = runs / f"{token}.json"
# It carries the MCP servers' headers.
with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "w") as handle:
Expand Down
3 changes: 2 additions & 1 deletion tst/unit/agents/gemini/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from agentenv_protocol import transfers as protocol_transfers
from starlette.testclient import TestClient

from tst.unit.agents.gemini.agent import a2a_server, gemini_bridge
from tst.unit.agents.gemini.agent import a2a_server, gemini_bridge, gemini_config
from tst.unit.agents.gemini.support import FakeStore, Staging


Expand All @@ -34,6 +34,7 @@ def home(monkeypatch, tmp_path) -> Path:
monkeypatch.setattr(a2a_server, "GEMINI_CWD", cwd)
monkeypatch.setattr(a2a_server, "SKILLS_DIR", gemini_home / "skills")
monkeypatch.setattr(a2a_server, "INPUTS_DIR", Path(cwd) / "inputs")
monkeypatch.setattr(gemini_config, "TASK_DIRS", ())
return root


Expand Down
13 changes: 13 additions & 0 deletions tst/unit/agents/gemini/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ def test_turns_sharing_a_system_prompt_share_its_file_until_the_last_ends(home):
assert prompt.read_text() == "be brief" and not second.exists()
assert not prompt.exists() and not first.exists()


def test_a_run_includes_the_task_directories_that_exist_so_the_file_tools_read_them(monkeypatch, tmp_path, home):
app_dir, data_dir = tmp_path / "app", tmp_path / "data"
app_dir.mkdir()
monkeypatch.setattr(gemini_config, "TASK_DIRS", (str(app_dir), str(data_dir)))

with gemini_config.run_settings({}, None, None) as path:
assert json.loads(path.read_text())["context"] == {"includeDirectories": [str(app_dir)]}
data_dir.mkdir()
with gemini_config.run_settings({}, None, "be brief") as path:
context = json.loads(path.read_text())["context"]
assert context["includeDirectories"] == [str(app_dir), str(data_dir)] and context["fileName"][0] == "GEMINI.md"

def test_a_duplicate_url_or_name_is_a_409(home, client):
client.post(MCP_CONFIG_PATH, json={"url": "https://gw/a/mcp", "name": "env1"})

Expand Down
Loading