fix(gemini): implement official hook contract
This commit is contained in:
1 parent
202aa1dff7
commit
653e644e4f
16 files changed
+2360
-204
No files matched your search
+81
-186
@@ -1,206 +1,101 @@
|
||||
"""Gemini CLI plugin — merges AfterTool hook into ~/.gemini/settings.json."""
|
||||
"""Gemini CLI native global user-hook integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
from ._file_io import write_json_atomic, write_text_atomic
|
||||
from .base import AgentPlugin, InstallConfig, PluginStatus
|
||||
from .hook_template import generate_hook_script
|
||||
from .gemini_config import GeminiDocument, render_gemini_document
|
||||
from .gemini_contract import (
|
||||
AGENT_KEY,
|
||||
AUDIT_HOOK_NAME,
|
||||
GUARD_HOOK_NAME,
|
||||
build_gemini_group,
|
||||
disabled_managed_gemini_entries,
|
||||
gemini_runtime_contracts,
|
||||
managed_gemini_hooks_enabled,
|
||||
)
|
||||
from .gemini_installation import (
|
||||
commit_gemini_installation,
|
||||
preflight_gemini_installation,
|
||||
prepare_gemini_installation,
|
||||
)
|
||||
from .gemini_io import (
|
||||
gemini_runtime_files_exist,
|
||||
read_gemini_document,
|
||||
write_gemini_document,
|
||||
)
|
||||
|
||||
|
||||
SETTINGS_PATH = os.path.join(os.path.expanduser("~"), ".gemini", "settings.json")
|
||||
ELYDORA_DIR = os.path.join(os.path.expanduser("~"), ".elydora")
|
||||
def _require_enabled_hooks(document: GeminiDocument) -> None:
|
||||
if not document.hook_controls.enabled:
|
||||
raise ValueError(
|
||||
"Gemini CLI hooks are disabled by hooksConfig.enabled: "
|
||||
f"{document.file_path}"
|
||||
)
|
||||
disabled = disabled_managed_gemini_entries(document.hook_controls)
|
||||
if disabled:
|
||||
raise ValueError(
|
||||
"Gemini CLI hooks are disabled by hooksConfig.disabled: "
|
||||
+ ", ".join(disabled)
|
||||
)
|
||||
|
||||
|
||||
class GeminiPlugin(AgentPlugin):
|
||||
"""Install/uninstall Elydora audit hook for Gemini CLI."""
|
||||
"""Install Elydora into Gemini CLI's native user settings."""
|
||||
|
||||
@staticmethod
|
||||
def _hook_path_for(agent_id: str) -> str:
|
||||
return os.path.join(ELYDORA_DIR, agent_id, "hook.py")
|
||||
manages_guard_runtime = True
|
||||
|
||||
def preflight_install(self, config: InstallConfig) -> None:
|
||||
document = read_gemini_document()
|
||||
_require_enabled_hooks(document)
|
||||
preflight_gemini_installation(config, document)
|
||||
|
||||
def install(self, config: InstallConfig) -> None:
|
||||
agent_id = config.get("agent_id", "")
|
||||
agent_name = config.get("agent_name", "")
|
||||
|
||||
# Create per-agent directory
|
||||
agent_dir = os.path.join(ELYDORA_DIR, agent_id)
|
||||
os.makedirs(agent_dir, exist_ok=True)
|
||||
|
||||
# Write config.json
|
||||
config_data = {
|
||||
"org_id": config.get("org_id", ""),
|
||||
"agent_id": agent_id,
|
||||
"kid": config.get("kid", ""),
|
||||
"base_url": config.get("base_url", "https://api.elydora.com"),
|
||||
"token": config.get("token", ""),
|
||||
"agent_name": agent_name,
|
||||
}
|
||||
config_path = os.path.join(agent_dir, "config.json")
|
||||
write_json_atomic(
|
||||
config_path,
|
||||
config_data,
|
||||
0o600,
|
||||
"Elydora runtime config",
|
||||
document = read_gemini_document()
|
||||
_require_enabled_hooks(document)
|
||||
paths = preflight_gemini_installation(config, document)
|
||||
rendered = render_gemini_document(
|
||||
document,
|
||||
None,
|
||||
{
|
||||
"BeforeTool": build_gemini_group(
|
||||
paths.guard_path, GUARD_HOOK_NAME
|
||||
),
|
||||
"AfterTool": build_gemini_group(
|
||||
paths.audit_path, AUDIT_HOOK_NAME
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
# Write private key
|
||||
private_key_path = os.path.join(agent_dir, "private.key")
|
||||
write_text_atomic(
|
||||
private_key_path,
|
||||
config.get("private_key", ""),
|
||||
0o600,
|
||||
"Elydora private key",
|
||||
)
|
||||
|
||||
script = generate_hook_script(
|
||||
org_id=config.get("org_id", ""),
|
||||
agent_id=agent_id,
|
||||
kid=config.get("kid", ""),
|
||||
base_url=config.get("base_url", "https://api.elydora.com"),
|
||||
)
|
||||
hook_path = self._hook_path_for(agent_id)
|
||||
write_text_atomic(
|
||||
hook_path,
|
||||
script,
|
||||
0o700,
|
||||
"Elydora audit runtime",
|
||||
)
|
||||
|
||||
guard_script_path = config.get("guard_script_path", "")
|
||||
python_exe = sys.executable
|
||||
|
||||
settings = _load_json(SETTINGS_PATH)
|
||||
hooks = settings.setdefault("hooks", {})
|
||||
|
||||
# --- BeforeTool (guard — freeze enforcement) ---
|
||||
before_tool = hooks.setdefault("BeforeTool", [])
|
||||
before_tool[:] = [h for h in before_tool if not _is_elydora_hook(h)]
|
||||
if guard_script_path:
|
||||
before_tool.append({
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": f'"{python_exe}" {guard_script_path}',
|
||||
}
|
||||
],
|
||||
})
|
||||
|
||||
# --- AfterTool (audit logging) ---
|
||||
after_tool = hooks.setdefault("AfterTool", [])
|
||||
|
||||
after_tool[:] = [h for h in after_tool if not _is_elydora_hook(h)]
|
||||
|
||||
after_tool.append({
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": hook_path,
|
||||
}
|
||||
],
|
||||
})
|
||||
|
||||
_save_json(SETTINGS_PATH, settings)
|
||||
print("Elydora hook installed for Gemini CLI.")
|
||||
print(f" Hook script: {hook_path}")
|
||||
print(f" Settings: {SETTINGS_PATH}")
|
||||
changes = prepare_gemini_installation(config, paths, rendered)
|
||||
commit_gemini_installation(changes)
|
||||
print(f"Gemini CLI hooks installed at {document.file_path}")
|
||||
print("Gemini CLI verification: run /hooks list.")
|
||||
|
||||
def uninstall(self, agent_id: str = "") -> None:
|
||||
if os.path.exists(SETTINGS_PATH):
|
||||
settings = _load_json(SETTINGS_PATH)
|
||||
hooks = settings.get("hooks", {})
|
||||
changed = False
|
||||
|
||||
# Remove BeforeTool entries
|
||||
before_tool = hooks.get("BeforeTool", [])
|
||||
before_filtered = [h for h in before_tool if not _is_elydora_hook(h, agent_id)]
|
||||
if len(before_filtered) != len(before_tool):
|
||||
hooks["BeforeTool"] = before_filtered
|
||||
if not before_filtered:
|
||||
del hooks["BeforeTool"]
|
||||
changed = True
|
||||
|
||||
# Remove AfterTool entries
|
||||
after_tool = hooks.get("AfterTool", [])
|
||||
after_filtered = [h for h in after_tool if not _is_elydora_hook(h, agent_id)]
|
||||
if len(after_filtered) != len(after_tool):
|
||||
hooks["AfterTool"] = after_filtered
|
||||
if not after_filtered:
|
||||
del hooks["AfterTool"]
|
||||
changed = True
|
||||
|
||||
if changed:
|
||||
if not hooks:
|
||||
del settings["hooks"]
|
||||
_save_json(SETTINGS_PATH, settings)
|
||||
|
||||
# Hook script removal is handled by cli.py cmd_uninstall (rmtree of agent dir)
|
||||
print("Elydora hook uninstalled from Gemini CLI.")
|
||||
document = read_gemini_document()
|
||||
write_gemini_document(
|
||||
render_gemini_document(document, agent_id or None, {})
|
||||
)
|
||||
|
||||
def status(self) -> PluginStatus:
|
||||
# Scan ~/.elydora/*/hook.py for any installed hook
|
||||
import glob as _glob
|
||||
hook_pattern = os.path.join(ELYDORA_DIR, "*", "hook.py")
|
||||
hook_files = _glob.glob(hook_pattern)
|
||||
hook_exists = len(hook_files) > 0
|
||||
|
||||
settings_configured = False
|
||||
if os.path.exists(SETTINGS_PATH):
|
||||
settings = _load_json(SETTINGS_PATH)
|
||||
hooks = settings.get("hooks", {})
|
||||
before_tool = hooks.get("BeforeTool", [])
|
||||
after_tool = hooks.get("AfterTool", [])
|
||||
before_configured = any(_is_elydora_hook(h) for h in before_tool)
|
||||
after_configured = any(_is_elydora_hook(h) for h in after_tool)
|
||||
settings_configured = before_configured and after_configured
|
||||
|
||||
installed = hook_exists and settings_configured
|
||||
document = read_gemini_document()
|
||||
contracts = gemini_runtime_contracts(document.hooks)
|
||||
controls_enabled = managed_gemini_hooks_enabled(
|
||||
document.hook_controls
|
||||
)
|
||||
configured = controls_enabled and bool(contracts)
|
||||
installed = configured and gemini_runtime_files_exist(contracts)
|
||||
if installed:
|
||||
details = f"Found {len(hook_files)} agent(s): {', '.join(hook_files)}"
|
||||
elif hook_exists:
|
||||
details = "Hook script exists but not configured in settings"
|
||||
elif settings_configured:
|
||||
details = "Configured in settings but hook script missing"
|
||||
details = f"Config: {document.file_path}"
|
||||
elif contracts and not controls_enabled:
|
||||
details = f"Configured hooks are disabled: {document.file_path}"
|
||||
elif contracts:
|
||||
details = (
|
||||
f"Configured at {document.file_path}; managed contract incomplete"
|
||||
)
|
||||
else:
|
||||
details = "Not installed"
|
||||
|
||||
return PluginStatus(installed=installed, agent="gemini", details=details)
|
||||
|
||||
|
||||
def _is_elydora_hook(entry: dict, agent_id: str = "") -> bool:
|
||||
# Collect all command strings from the entry
|
||||
commands: list[str] = []
|
||||
inner_hooks = entry.get("hooks")
|
||||
if isinstance(inner_hooks, list):
|
||||
for hook in inner_hooks:
|
||||
if isinstance(hook, dict) and isinstance(hook.get("command"), str):
|
||||
commands.append(hook["command"])
|
||||
else:
|
||||
command = entry.get("command")
|
||||
if isinstance(command, str):
|
||||
commands.append(command)
|
||||
|
||||
for cmd in commands:
|
||||
cmd_lower = cmd.lower()
|
||||
if "elydora" not in cmd_lower:
|
||||
continue
|
||||
# If agent_id is specified, only match hooks for that specific agent
|
||||
if agent_id and agent_id in cmd:
|
||||
return True
|
||||
if not agent_id:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _load_json(path: str) -> dict:
|
||||
if not os.path.exists(path):
|
||||
return {}
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
def _save_json(path: str, data: dict) -> None:
|
||||
write_json_atomic(path, data, 0o600, "Gemini settings")
|
||||
return PluginStatus(
|
||||
installed=installed,
|
||||
agent=AGENT_KEY,
|
||||
details=details,
|
||||
)
|
||||
@@ -0,0 +1,206 @@
|
||||
"""Gemini CLI hook command construction and exact runtime ownership."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
from dataclasses import dataclass
|
||||
import ntpath
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from typing import Optional, Tuple
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GeminiRuntimeReference:
|
||||
agent_id: str
|
||||
script_path: str
|
||||
|
||||
|
||||
def same_gemini_path(left: str, right: str) -> bool:
|
||||
return os.path.normcase(os.path.abspath(left)) == os.path.normcase(
|
||||
os.path.abspath(right)
|
||||
)
|
||||
|
||||
|
||||
def same_gemini_agent_id(left: str, right: str) -> bool:
|
||||
return os.path.normcase(left) == os.path.normcase(right)
|
||||
|
||||
|
||||
def _quote_posix(value: str) -> str:
|
||||
return "'" + value.replace("'", "'\"'\"'") + "'"
|
||||
|
||||
|
||||
def _quote_powershell(value: str) -> str:
|
||||
return "'" + value.replace("'", "''") + "'"
|
||||
|
||||
|
||||
def _windows_powershell_path() -> str:
|
||||
configured = os.environ.get("SystemRoot")
|
||||
system_root = (
|
||||
configured
|
||||
if configured
|
||||
and ntpath.isabs(configured)
|
||||
and re.search(r'["%\r\n]', configured) is None
|
||||
else r"C:\Windows"
|
||||
)
|
||||
return ntpath.join(
|
||||
system_root,
|
||||
"System32",
|
||||
"WindowsPowerShell",
|
||||
"v1.0",
|
||||
"powershell.exe",
|
||||
)
|
||||
|
||||
|
||||
def _windows_command(script_path: str) -> str:
|
||||
source = (
|
||||
f"& {_quote_powershell(sys.executable)} "
|
||||
f"{_quote_powershell(script_path)}; exit $LASTEXITCODE"
|
||||
)
|
||||
encoded = base64.b64encode(source.encode("utf-16le")).decode("ascii")
|
||||
return (
|
||||
f"& {_quote_powershell(_windows_powershell_path())} "
|
||||
"-NoLogo -NoProfile -NonInteractive -EncodedCommand "
|
||||
f"{encoded}"
|
||||
)
|
||||
|
||||
|
||||
def build_gemini_command(script_path: str) -> str:
|
||||
if not os.path.isabs(sys.executable) or not os.path.isabs(script_path):
|
||||
raise ValueError(
|
||||
"Gemini CLI hook commands require absolute executable and script paths"
|
||||
)
|
||||
if os.name == "nt":
|
||||
return _windows_command(script_path)
|
||||
return f"{_quote_posix(sys.executable)} {_quote_posix(script_path)}"
|
||||
|
||||
|
||||
def _read_posix_argument(command: str, start: int) -> Optional[Tuple[str, int]]:
|
||||
if start >= len(command) or command[start] != "'":
|
||||
return None
|
||||
apostrophe = "'\"'\"'"
|
||||
value = ""
|
||||
index = start + 1
|
||||
while index < len(command):
|
||||
if command.startswith(apostrophe, index):
|
||||
value += "'"
|
||||
index += len(apostrophe)
|
||||
continue
|
||||
if command[index] == "'":
|
||||
return value, index + 1
|
||||
value += command[index]
|
||||
index += 1
|
||||
return None
|
||||
|
||||
|
||||
def _parse_posix_command(command: str) -> Optional[Tuple[str, str]]:
|
||||
executable = _read_posix_argument(command, 0)
|
||||
if executable is None or executable[1] >= len(command):
|
||||
return None
|
||||
if command[executable[1]] != " ":
|
||||
return None
|
||||
script = _read_posix_argument(command, executable[1] + 1)
|
||||
if script is None or script[1] != len(command):
|
||||
return None
|
||||
return executable[0], script[0]
|
||||
|
||||
|
||||
def _read_powershell_argument(
|
||||
command: str, start: int
|
||||
) -> Optional[Tuple[str, int]]:
|
||||
if start >= len(command) or command[start] != "'":
|
||||
return None
|
||||
value = ""
|
||||
index = start + 1
|
||||
while index < len(command):
|
||||
if command[index] != "'":
|
||||
value += command[index]
|
||||
index += 1
|
||||
continue
|
||||
if index + 1 < len(command) and command[index + 1] == "'":
|
||||
value += "'"
|
||||
index += 2
|
||||
continue
|
||||
return value, index + 1
|
||||
return None
|
||||
|
||||
|
||||
def _parse_powershell_source(source: str) -> Optional[Tuple[str, str]]:
|
||||
if not source.startswith("& "):
|
||||
return None
|
||||
executable = _read_powershell_argument(source, 2)
|
||||
if executable is None or executable[1] >= len(source):
|
||||
return None
|
||||
if source[executable[1]] != " ":
|
||||
return None
|
||||
script = _read_powershell_argument(source, executable[1] + 1)
|
||||
if script is None or source[script[1] :] != "; exit $LASTEXITCODE":
|
||||
return None
|
||||
return executable[0], script[0]
|
||||
|
||||
|
||||
def _parse_windows_command(command: str) -> Optional[Tuple[str, str]]:
|
||||
if not command.startswith("& "):
|
||||
return None
|
||||
powershell = _read_powershell_argument(command, 2)
|
||||
if (
|
||||
powershell is None
|
||||
or not ntpath.isabs(powershell[0])
|
||||
or ntpath.basename(powershell[0]).lower() != "powershell.exe"
|
||||
):
|
||||
return None
|
||||
prefix = " -NoLogo -NoProfile -NonInteractive -EncodedCommand "
|
||||
if not command.startswith(prefix, powershell[1]):
|
||||
return None
|
||||
encoded = command[powershell[1] + len(prefix) :]
|
||||
if re.fullmatch(r"[A-Za-z0-9+/]+={0,2}", encoded) is None:
|
||||
return None
|
||||
try:
|
||||
payload = base64.b64decode(encoded, validate=True)
|
||||
if base64.b64encode(payload).decode("ascii") != encoded:
|
||||
return None
|
||||
if len(payload) % 2:
|
||||
return None
|
||||
source = payload.decode("utf-16le")
|
||||
except (ValueError, UnicodeDecodeError):
|
||||
return None
|
||||
return _parse_powershell_source(source)
|
||||
|
||||
|
||||
def _parse_legacy_command(
|
||||
command: str, script_name: str
|
||||
) -> Optional[Tuple[str, str]]:
|
||||
if script_name == "guard.py":
|
||||
match = re.fullmatch(r'"([^"\r\n]+)" ([^\r\n]+)', command)
|
||||
if match is None or not same_gemini_path(match.group(1), sys.executable):
|
||||
return None
|
||||
return match.group(1), match.group(2)
|
||||
if "\r" in command or "\n" in command:
|
||||
return None
|
||||
return sys.executable, command
|
||||
|
||||
|
||||
def gemini_runtime_reference(
|
||||
command: str,
|
||||
script_name: str,
|
||||
include_legacy: bool = False,
|
||||
) -> Optional[GeminiRuntimeReference]:
|
||||
parsed = _parse_posix_command(command) or _parse_windows_command(command)
|
||||
if parsed is None and include_legacy:
|
||||
parsed = _parse_legacy_command(command, script_name)
|
||||
if (
|
||||
parsed is None
|
||||
or not same_gemini_path(parsed[0], sys.executable)
|
||||
or not os.path.isabs(parsed[1])
|
||||
or os.path.basename(parsed[1]) != script_name
|
||||
):
|
||||
return None
|
||||
agent_directory = os.path.dirname(parsed[1])
|
||||
runtime_root = os.path.join(os.path.expanduser("~"), ".elydora")
|
||||
if not same_gemini_path(os.path.dirname(agent_directory), runtime_root):
|
||||
return None
|
||||
agent_id = os.path.basename(agent_directory)
|
||||
if agent_id in {"", ".", ".."}:
|
||||
return None
|
||||
return GeminiRuntimeReference(agent_id, parsed[1])
|
||||
@@ -0,0 +1,181 @@
|
||||
"""Gemini CLI JSONC parsing and source-preserving hook edits."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Dict, Optional
|
||||
|
||||
from ._jsonc import JsoncEditor
|
||||
from .gemini_contract import (
|
||||
MANAGED_EVENTS,
|
||||
GeminiHookControls,
|
||||
GeminiHooks,
|
||||
JsonObject,
|
||||
managed_gemini_removals,
|
||||
read_gemini_hook_controls,
|
||||
read_gemini_hooks,
|
||||
)
|
||||
|
||||
|
||||
OWNED_FILE_MARKER = "// Managed by Elydora"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GeminiDocument:
|
||||
file_path: str
|
||||
exists: bool
|
||||
raw: str
|
||||
root: JsonObject
|
||||
hooks: GeminiHooks
|
||||
hook_controls: GeminiHookControls
|
||||
has_hooks_container: bool
|
||||
owned_file: bool
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RenderedGeminiDocument:
|
||||
document: GeminiDocument
|
||||
changed: bool
|
||||
next_source: Optional[str]
|
||||
|
||||
|
||||
def _label(file_path: str) -> str:
|
||||
return f"Gemini CLI user settings at {file_path}"
|
||||
|
||||
|
||||
def parse_gemini_document(
|
||||
*, exists: bool, file_path: str, raw: str
|
||||
) -> GeminiDocument:
|
||||
editor = JsoncEditor(
|
||||
raw,
|
||||
_label(file_path),
|
||||
allow_trailing_commas=False,
|
||||
)
|
||||
if not isinstance(editor.value, dict):
|
||||
raise ValueError(f"{_label(file_path)} must contain a JSON object")
|
||||
root = editor.value
|
||||
return GeminiDocument(
|
||||
file_path=file_path,
|
||||
exists=exists,
|
||||
raw=raw,
|
||||
root=root,
|
||||
hooks=(read_gemini_hooks(root["hooks"]) if "hooks" in root else {}),
|
||||
hook_controls=(
|
||||
read_gemini_hook_controls(root["hooksConfig"])
|
||||
if "hooksConfig" in root
|
||||
else GeminiHookControls(True, ())
|
||||
),
|
||||
has_hooks_container="hooks" in root,
|
||||
owned_file=raw.startswith(OWNED_FILE_MARKER),
|
||||
)
|
||||
|
||||
|
||||
def create_gemini_document(file_path: str) -> GeminiDocument:
|
||||
return parse_gemini_document(
|
||||
exists=False,
|
||||
file_path=file_path,
|
||||
raw=f"{OWNED_FILE_MARKER}\n{{}}\n",
|
||||
)
|
||||
|
||||
|
||||
def _current_document(
|
||||
document: GeminiDocument, editor: JsoncEditor
|
||||
) -> GeminiDocument:
|
||||
return parse_gemini_document(
|
||||
exists=document.exists,
|
||||
file_path=document.file_path,
|
||||
raw=editor.raw,
|
||||
)
|
||||
|
||||
|
||||
def _remove_managed(
|
||||
editor: JsoncEditor,
|
||||
document: GeminiDocument,
|
||||
agent_id: Optional[str],
|
||||
) -> None:
|
||||
removals = managed_gemini_removals(document.hooks, agent_id)
|
||||
for event in MANAGED_EVENTS:
|
||||
event_removals = sorted(
|
||||
(item for item in removals if item.event == event),
|
||||
key=lambda item: item.group_index,
|
||||
reverse=True,
|
||||
)
|
||||
for removal in event_removals:
|
||||
group_path = ("hooks", event, removal.group_index)
|
||||
if removal.remove_group:
|
||||
editor.delete(group_path)
|
||||
continue
|
||||
for handler_index in sorted(
|
||||
removal.handler_indexes, reverse=True
|
||||
):
|
||||
editor.delete((*group_path, "hooks", handler_index))
|
||||
if event_removals:
|
||||
current = _current_document(document, editor)
|
||||
if event in current.hooks and not current.hooks[event]:
|
||||
editor.delete(("hooks", event))
|
||||
current = _current_document(document, editor)
|
||||
if current.has_hooks_container and not current.hooks:
|
||||
editor.delete(("hooks",))
|
||||
|
||||
|
||||
def _append_group(
|
||||
editor: JsoncEditor,
|
||||
document: GeminiDocument,
|
||||
event: str,
|
||||
group: JsonObject,
|
||||
) -> None:
|
||||
current = _current_document(document, editor)
|
||||
if not current.has_hooks_container:
|
||||
editor.add_property((), "hooks", {event: [group]})
|
||||
elif event in current.hooks:
|
||||
editor.append(("hooks", event), group)
|
||||
else:
|
||||
editor.add_property(("hooks",), event, [group])
|
||||
|
||||
|
||||
def _already_installed(
|
||||
document: GeminiDocument, additions: Dict[str, JsonObject]
|
||||
) -> bool:
|
||||
if set(additions) != set(MANAGED_EVENTS):
|
||||
return False
|
||||
removals = managed_gemini_removals(document.hooks)
|
||||
for event in MANAGED_EVENTS:
|
||||
expected = additions[event]
|
||||
event_removals = [item for item in removals if item.event == event]
|
||||
exact_groups = [
|
||||
group for group in document.hooks.get(event, []) if group == expected
|
||||
]
|
||||
if (
|
||||
len(event_removals) != 1
|
||||
or not event_removals[0].remove_group
|
||||
or len(exact_groups) != 1
|
||||
):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def render_gemini_document(
|
||||
document: GeminiDocument,
|
||||
agent_id: Optional[str],
|
||||
additions: Dict[str, JsonObject],
|
||||
) -> RenderedGeminiDocument:
|
||||
if agent_id is None and _already_installed(document, additions):
|
||||
return RenderedGeminiDocument(document, False, document.raw)
|
||||
editor = JsoncEditor(
|
||||
document.raw,
|
||||
_label(document.file_path),
|
||||
allow_trailing_commas=False,
|
||||
)
|
||||
_remove_managed(editor, document, agent_id)
|
||||
for event in MANAGED_EVENTS:
|
||||
group = additions.get(event)
|
||||
if group is not None:
|
||||
_append_group(editor, document, event, group)
|
||||
current = _current_document(document, editor)
|
||||
if not additions and document.owned_file and not current.root:
|
||||
return RenderedGeminiDocument(document, True, None)
|
||||
return RenderedGeminiDocument(
|
||||
document,
|
||||
editor.raw != document.raw,
|
||||
editor.raw,
|
||||
)
|
||||
@@ -0,0 +1,344 @@
|
||||
"""Gemini CLI hook schema, controls, and exact ownership rules."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import math
|
||||
import os
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
from .gemini_command import (
|
||||
GeminiRuntimeReference,
|
||||
build_gemini_command,
|
||||
gemini_runtime_reference,
|
||||
same_gemini_agent_id,
|
||||
)
|
||||
|
||||
|
||||
AGENT_KEY = "gemini"
|
||||
CONFIG_FILE = "settings.json"
|
||||
GUARD_SCRIPT = "guard.py"
|
||||
AUDIT_SCRIPT = "hook.py"
|
||||
GUARD_HOOK_NAME = "elydora-guard"
|
||||
AUDIT_HOOK_NAME = "elydora-audit"
|
||||
HOOK_TIMEOUT_MILLISECONDS = 10_000
|
||||
MANAGED_EVENTS = ("BeforeTool", "AfterTool")
|
||||
|
||||
KNOWN_EVENTS = {
|
||||
"BeforeTool",
|
||||
"AfterTool",
|
||||
"BeforeAgent",
|
||||
"Notification",
|
||||
"AfterAgent",
|
||||
"SessionStart",
|
||||
"SessionEnd",
|
||||
"PreCompress",
|
||||
"BeforeModel",
|
||||
"AfterModel",
|
||||
"BeforeToolSelection",
|
||||
}
|
||||
|
||||
JsonObject = Dict[str, Any]
|
||||
GeminiHooks = Dict[str, List[JsonObject]]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GeminiHookControls:
|
||||
enabled: bool
|
||||
disabled: Tuple[str, ...]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GeminiRuntimeContract:
|
||||
agent_id: str
|
||||
guard_path: str
|
||||
audit_path: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ManagedRemoval:
|
||||
event: str
|
||||
group_index: int
|
||||
handler_indexes: Tuple[int, ...]
|
||||
remove_group: bool
|
||||
|
||||
|
||||
def elydora_dir() -> str:
|
||||
return os.path.join(os.path.expanduser("~"), ".elydora")
|
||||
|
||||
|
||||
def _optional_string(value: JsonObject, field: str, label: str) -> None:
|
||||
if field in value and not isinstance(value[field], str):
|
||||
raise ValueError(f'{label} field "{field}" must be a string')
|
||||
|
||||
|
||||
def _validate_timeout(value: JsonObject, label: str) -> None:
|
||||
if "timeout" not in value:
|
||||
return
|
||||
timeout = value["timeout"]
|
||||
if (
|
||||
isinstance(timeout, bool)
|
||||
or not isinstance(timeout, (int, float))
|
||||
or not math.isfinite(float(timeout))
|
||||
or timeout < 0
|
||||
):
|
||||
raise ValueError(f"{label} timeout must be a non-negative finite number")
|
||||
|
||||
|
||||
def _validate_environment(value: JsonObject, label: str) -> None:
|
||||
if "env" not in value:
|
||||
return
|
||||
environment = value["env"]
|
||||
if not isinstance(environment, dict) or any(
|
||||
not isinstance(item, str) for item in environment.values()
|
||||
):
|
||||
raise ValueError(f"{label} env must map names to strings")
|
||||
|
||||
|
||||
def _validate_handler(
|
||||
value: Any, event: str, group_index: int, handler_index: int
|
||||
) -> JsonObject:
|
||||
label = (
|
||||
f"Gemini CLI settings handler hooks.{event}[{group_index}]"
|
||||
f".hooks[{handler_index}]"
|
||||
)
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError(f"{label} must be an object")
|
||||
handler = dict(value)
|
||||
if handler.get("type") != "command":
|
||||
raise ValueError(
|
||||
f'{label} has unsupported type "{handler.get("type")}"'
|
||||
)
|
||||
for field in ("name", "description", "source"):
|
||||
_optional_string(handler, field, label)
|
||||
_validate_timeout(handler, label)
|
||||
_validate_environment(handler, label)
|
||||
if not isinstance(handler.get("command"), str) or not handler["command"]:
|
||||
raise ValueError(f"{label} requires a non-empty command")
|
||||
return handler
|
||||
|
||||
|
||||
def _validate_group(value: Any, event: str, index: int) -> JsonObject:
|
||||
label = f"Gemini CLI settings group hooks.{event}[{index}]"
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError(f"{label} must be an object")
|
||||
group = dict(value)
|
||||
if "matcher" in group and not isinstance(group["matcher"], str):
|
||||
raise ValueError(f"{label} matcher must be a string")
|
||||
if "sequential" in group and not isinstance(group["sequential"], bool):
|
||||
raise ValueError(f"{label} sequential must be a boolean")
|
||||
handlers = group.get("hooks")
|
||||
if not isinstance(handlers, list):
|
||||
raise ValueError(f"{label} must contain a hooks array")
|
||||
group["hooks"] = [
|
||||
_validate_handler(handler, event, index, handler_index)
|
||||
for handler_index, handler in enumerate(handlers)
|
||||
]
|
||||
return group
|
||||
|
||||
|
||||
def read_gemini_hooks(value: Any) -> GeminiHooks:
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError('Gemini CLI settings field "hooks" must be an object')
|
||||
hooks: GeminiHooks = {}
|
||||
for event, groups in value.items():
|
||||
if not isinstance(groups, list):
|
||||
raise ValueError(
|
||||
f'Gemini CLI settings field "hooks.{event}" must be an array'
|
||||
)
|
||||
hooks[event] = (
|
||||
[
|
||||
_validate_group(group, event, index)
|
||||
for index, group in enumerate(groups)
|
||||
]
|
||||
if event in KNOWN_EVENTS
|
||||
else list(groups)
|
||||
)
|
||||
return hooks
|
||||
|
||||
|
||||
def read_gemini_hook_controls(value: Any) -> GeminiHookControls:
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError(
|
||||
'Gemini CLI settings field "hooksConfig" must be an object'
|
||||
)
|
||||
supported = {"enabled", "disabled", "notifications"}
|
||||
extra = next((field for field in value if field not in supported), None)
|
||||
if extra is not None:
|
||||
raise ValueError(
|
||||
"Gemini CLI settings field \"hooksConfig\" contains unsupported "
|
||||
f'field "{extra}"'
|
||||
)
|
||||
if "enabled" in value and not isinstance(value["enabled"], bool):
|
||||
raise ValueError(
|
||||
'Gemini CLI settings field "hooksConfig.enabled" must be a boolean'
|
||||
)
|
||||
if "notifications" in value and not isinstance(
|
||||
value["notifications"], bool
|
||||
):
|
||||
raise ValueError(
|
||||
"Gemini CLI settings field \"hooksConfig.notifications\" must be "
|
||||
"a boolean"
|
||||
)
|
||||
disabled = value.get("disabled", [])
|
||||
if not isinstance(disabled, list) or any(
|
||||
not isinstance(item, str) for item in disabled
|
||||
):
|
||||
raise ValueError(
|
||||
"Gemini CLI settings field \"hooksConfig.disabled\" must be an "
|
||||
"array of strings"
|
||||
)
|
||||
return GeminiHookControls(value.get("enabled") is not False, tuple(disabled))
|
||||
|
||||
|
||||
def managed_gemini_hooks_enabled(controls: GeminiHookControls) -> bool:
|
||||
return (
|
||||
controls.enabled
|
||||
and GUARD_HOOK_NAME not in controls.disabled
|
||||
and AUDIT_HOOK_NAME not in controls.disabled
|
||||
)
|
||||
|
||||
|
||||
def disabled_managed_gemini_entries(
|
||||
controls: GeminiHookControls,
|
||||
) -> Tuple[str, ...]:
|
||||
return tuple(
|
||||
entry
|
||||
for entry in controls.disabled
|
||||
if entry in {GUARD_HOOK_NAME, AUDIT_HOOK_NAME}
|
||||
or gemini_runtime_reference(entry, GUARD_SCRIPT, True) is not None
|
||||
or gemini_runtime_reference(entry, AUDIT_SCRIPT, True) is not None
|
||||
)
|
||||
|
||||
|
||||
def build_gemini_group(script_path: str, name: str) -> JsonObject:
|
||||
return {
|
||||
"hooks": [{
|
||||
"type": "command",
|
||||
"name": name,
|
||||
"command": build_gemini_command(script_path),
|
||||
"timeout": HOOK_TIMEOUT_MILLISECONDS,
|
||||
}]
|
||||
}
|
||||
|
||||
|
||||
def _exact_managed_group(group: JsonObject) -> bool:
|
||||
return set(group) == {"hooks"}
|
||||
|
||||
|
||||
def _current_managed_reference(
|
||||
handler: JsonObject, script_name: str, hook_name: str
|
||||
) -> Optional[GeminiRuntimeReference]:
|
||||
if (
|
||||
set(handler) == {"command", "name", "timeout", "type"}
|
||||
and handler.get("type") == "command"
|
||||
and handler.get("name") == hook_name
|
||||
and handler.get("timeout") == HOOK_TIMEOUT_MILLISECONDS
|
||||
and isinstance(handler.get("command"), str)
|
||||
):
|
||||
return gemini_runtime_reference(handler["command"], script_name)
|
||||
return None
|
||||
|
||||
|
||||
def _legacy_managed_reference(
|
||||
handler: JsonObject, script_name: str
|
||||
) -> Optional[GeminiRuntimeReference]:
|
||||
if (
|
||||
set(handler) == {"command", "type"}
|
||||
and handler.get("type") == "command"
|
||||
and isinstance(handler.get("command"), str)
|
||||
):
|
||||
return gemini_runtime_reference(handler["command"], script_name, True)
|
||||
return None
|
||||
|
||||
|
||||
def _managed_reference(
|
||||
handler: JsonObject,
|
||||
script_name: str,
|
||||
hook_name: str,
|
||||
include_legacy: bool,
|
||||
) -> Optional[GeminiRuntimeReference]:
|
||||
current = _current_managed_reference(handler, script_name, hook_name)
|
||||
if current is not None or not include_legacy:
|
||||
return current
|
||||
return _legacy_managed_reference(handler, script_name)
|
||||
|
||||
|
||||
def managed_gemini_removals(
|
||||
hooks: GeminiHooks, agent_id: Optional[str] = None
|
||||
) -> List[ManagedRemoval]:
|
||||
removals = []
|
||||
for event, script_name, hook_name in (
|
||||
("BeforeTool", GUARD_SCRIPT, GUARD_HOOK_NAME),
|
||||
("AfterTool", AUDIT_SCRIPT, AUDIT_HOOK_NAME),
|
||||
):
|
||||
for group_index, group in enumerate(hooks.get(event, [])):
|
||||
handlers = group.get("hooks", [])
|
||||
indexes = tuple(
|
||||
index
|
||||
for index, handler in enumerate(handlers)
|
||||
if isinstance(handler, dict)
|
||||
and (
|
||||
reference := _managed_reference(
|
||||
handler, script_name, hook_name, True
|
||||
)
|
||||
)
|
||||
is not None
|
||||
and (
|
||||
agent_id is None
|
||||
or same_gemini_agent_id(reference.agent_id, agent_id)
|
||||
)
|
||||
)
|
||||
if indexes:
|
||||
removals.append(
|
||||
ManagedRemoval(
|
||||
event,
|
||||
group_index,
|
||||
indexes,
|
||||
_exact_managed_group(group)
|
||||
and len(indexes) == len(handlers),
|
||||
)
|
||||
)
|
||||
return removals
|
||||
|
||||
|
||||
def _references_for_event(
|
||||
groups: List[JsonObject], script_name: str, hook_name: str
|
||||
) -> Dict[str, List[GeminiRuntimeReference]]:
|
||||
result: Dict[str, List[GeminiRuntimeReference]] = {}
|
||||
for group in groups:
|
||||
if not _exact_managed_group(group):
|
||||
continue
|
||||
for handler in group["hooks"]:
|
||||
reference = _current_managed_reference(
|
||||
handler, script_name, hook_name
|
||||
)
|
||||
if reference is None:
|
||||
continue
|
||||
key = os.path.normcase(reference.agent_id)
|
||||
result.setdefault(key, []).append(reference)
|
||||
return result
|
||||
|
||||
|
||||
def gemini_runtime_contracts(
|
||||
hooks: GeminiHooks,
|
||||
) -> List[GeminiRuntimeContract]:
|
||||
guards = _references_for_event(
|
||||
hooks.get("BeforeTool", []), GUARD_SCRIPT, GUARD_HOOK_NAME
|
||||
)
|
||||
audits = _references_for_event(
|
||||
hooks.get("AfterTool", []), AUDIT_SCRIPT, AUDIT_HOOK_NAME
|
||||
)
|
||||
contracts = []
|
||||
for key, guard in guards.items():
|
||||
audit = audits.get(key, [])
|
||||
if len(guard) != 1 or len(audit) != 1:
|
||||
continue
|
||||
contracts.append(
|
||||
GeminiRuntimeContract(
|
||||
guard[0].agent_id,
|
||||
guard[0].script_path,
|
||||
audit[0].script_path,
|
||||
)
|
||||
)
|
||||
return contracts
|
||||
@@ -0,0 +1,161 @@
|
||||
"""Transactional Gemini CLI hook and runtime installation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import json
|
||||
import os
|
||||
from typing import List, Optional
|
||||
|
||||
from elydora._runtime_paths import resolve_agent_directory
|
||||
|
||||
from ._managed_files import MAX_CONFIG_BYTES, MAX_SECRET_BYTES, MAX_SOURCE_BYTES
|
||||
from ._transaction import FileChange, file_change, write_changes
|
||||
from .base import InstallConfig
|
||||
from .gemini_command import same_gemini_path
|
||||
from .gemini_config import GeminiDocument, RenderedGeminiDocument
|
||||
from .gemini_contract import (
|
||||
AGENT_KEY,
|
||||
AUDIT_SCRIPT,
|
||||
GUARD_SCRIPT,
|
||||
elydora_dir,
|
||||
)
|
||||
from .gemini_io import (
|
||||
rendered_change,
|
||||
validate_api_origin,
|
||||
validate_private_key,
|
||||
validate_runtime_tree,
|
||||
)
|
||||
from .hook_template import generate_guard_script, generate_hook_script
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GeminiRuntimePaths:
|
||||
agent_id: str
|
||||
agent_directory: str
|
||||
guard_path: str
|
||||
audit_path: str
|
||||
|
||||
|
||||
def _runtime_config(config: InstallConfig, agent_id: str) -> dict:
|
||||
value = {
|
||||
"org_id": config.get("org_id", ""),
|
||||
"agent_id": agent_id,
|
||||
"kid": config.get("kid", ""),
|
||||
"base_url": config.get("base_url", "https://api.elydora.com"),
|
||||
"agent_name": AGENT_KEY,
|
||||
}
|
||||
token = config.get("token")
|
||||
if token:
|
||||
value["token"] = token
|
||||
return value
|
||||
|
||||
|
||||
def _json_source(value: dict) -> str:
|
||||
return json.dumps(value, indent=2) + "\n"
|
||||
|
||||
|
||||
def _agent_paths(config: InstallConfig) -> GeminiRuntimePaths:
|
||||
agent_id = config.get("agent_id", "")
|
||||
if not agent_id:
|
||||
raise ValueError("agent_id is required")
|
||||
agent_directory = resolve_agent_directory(elydora_dir(), agent_id)
|
||||
guard_path = os.path.join(agent_directory, GUARD_SCRIPT)
|
||||
audit_path = os.path.join(agent_directory, AUDIT_SCRIPT)
|
||||
if not same_gemini_path(config.get("guard_script_path", ""), guard_path):
|
||||
raise ValueError(
|
||||
"Elydora guard runtime must use the managed agent directory: "
|
||||
f"{guard_path}"
|
||||
)
|
||||
return GeminiRuntimePaths(agent_id, agent_directory, guard_path, audit_path)
|
||||
|
||||
|
||||
def _validate_install_config(config: InstallConfig) -> None:
|
||||
for field in ("org_id", "agent_id", "kid", "private_key", "base_url"):
|
||||
value = config.get(field)
|
||||
if not isinstance(value, str) or not value:
|
||||
raise ValueError(f"{field} is required")
|
||||
if config.get("agent_name") != AGENT_KEY:
|
||||
raise ValueError(
|
||||
f"Gemini CLI installation requires agent_name {AGENT_KEY}"
|
||||
)
|
||||
validate_private_key(config["private_key"])
|
||||
validate_api_origin(config["base_url"])
|
||||
if "token" in config:
|
||||
token = config["token"]
|
||||
if not isinstance(token, str) or not token:
|
||||
raise ValueError("token must be a non-empty string when provided")
|
||||
_agent_paths(config)
|
||||
|
||||
|
||||
def preflight_gemini_installation(
|
||||
config: InstallConfig, document: GeminiDocument
|
||||
) -> GeminiRuntimePaths:
|
||||
if not document.file_path:
|
||||
raise ValueError("Gemini CLI installation requires a settings path")
|
||||
_validate_install_config(config)
|
||||
paths = _agent_paths(config)
|
||||
validate_runtime_tree(paths.agent_directory, paths.agent_id)
|
||||
return paths
|
||||
|
||||
|
||||
def _runtime_change(
|
||||
file_path: str,
|
||||
label: str,
|
||||
next_source: str,
|
||||
mode: int,
|
||||
maximum_bytes: int = MAX_SOURCE_BYTES,
|
||||
) -> Optional[FileChange]:
|
||||
return file_change(file_path, label, next_source, mode, maximum_bytes)
|
||||
|
||||
|
||||
def _present(changes: List[Optional[FileChange]]) -> List[FileChange]:
|
||||
return [change for change in changes if change is not None]
|
||||
|
||||
|
||||
def prepare_gemini_installation(
|
||||
config: InstallConfig,
|
||||
paths: GeminiRuntimePaths,
|
||||
rendered: RenderedGeminiDocument,
|
||||
) -> List[FileChange]:
|
||||
guard_script = generate_guard_script(
|
||||
AGENT_KEY,
|
||||
paths.agent_id,
|
||||
success_output="{}\n",
|
||||
)
|
||||
audit_script = generate_hook_script(
|
||||
org_id=config.get("org_id", ""),
|
||||
agent_id=paths.agent_id,
|
||||
kid=config.get("kid", ""),
|
||||
base_url=config.get("base_url", "https://api.elydora.com"),
|
||||
success_output="{}\n",
|
||||
native_payload=True,
|
||||
agent_name=AGENT_KEY,
|
||||
)
|
||||
return _present([
|
||||
_runtime_change(
|
||||
paths.guard_path, "Elydora guard runtime", guard_script, 0o700
|
||||
),
|
||||
_runtime_change(
|
||||
os.path.join(paths.agent_directory, "config.json"),
|
||||
"Elydora runtime config",
|
||||
_json_source(_runtime_config(config, paths.agent_id)),
|
||||
0o600,
|
||||
MAX_CONFIG_BYTES,
|
||||
),
|
||||
_runtime_change(
|
||||
os.path.join(paths.agent_directory, "private.key"),
|
||||
"Elydora private key",
|
||||
config.get("private_key", ""),
|
||||
0o600,
|
||||
MAX_SECRET_BYTES,
|
||||
),
|
||||
_runtime_change(
|
||||
paths.audit_path, "Elydora audit runtime", audit_script, 0o700
|
||||
),
|
||||
rendered_change(rendered),
|
||||
])
|
||||
|
||||
|
||||
def commit_gemini_installation(changes: List[FileChange]) -> None:
|
||||
write_changes(changes, "Install Gemini CLI hooks")
|
||||
@@ -0,0 +1,250 @@
|
||||
"""Gemini CLI settings discovery and strict managed runtime inspection."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import os
|
||||
from typing import Any, List, Optional
|
||||
import urllib.parse
|
||||
|
||||
from ._managed_files import (
|
||||
MAX_CONFIG_BYTES,
|
||||
MAX_SECRET_BYTES,
|
||||
MAX_SOURCE_BYTES,
|
||||
physical_directory_exists,
|
||||
physical_file_exists,
|
||||
read_physical_file,
|
||||
)
|
||||
from ._strict_json import JsonObject, parse_json_object
|
||||
from ._transaction import FileChange, source_change, write_changes
|
||||
from .gemini_command import same_gemini_agent_id, same_gemini_path
|
||||
from .gemini_config import (
|
||||
GeminiDocument,
|
||||
RenderedGeminiDocument,
|
||||
create_gemini_document,
|
||||
parse_gemini_document,
|
||||
)
|
||||
from .gemini_contract import (
|
||||
AGENT_KEY,
|
||||
CONFIG_FILE,
|
||||
GeminiRuntimeContract,
|
||||
elydora_dir,
|
||||
)
|
||||
|
||||
|
||||
def gemini_configuration_directory() -> str:
|
||||
configured_home = os.environ.get("GEMINI_CLI_HOME")
|
||||
home = configured_home if configured_home else os.path.expanduser("~")
|
||||
return os.path.join(home, ".gemini")
|
||||
|
||||
|
||||
def gemini_settings_path() -> str:
|
||||
return os.path.join(gemini_configuration_directory(), CONFIG_FILE)
|
||||
|
||||
|
||||
def read_gemini_document() -> GeminiDocument:
|
||||
file_path = gemini_settings_path()
|
||||
physical_directory_exists(
|
||||
os.path.dirname(file_path), "Gemini CLI configuration directory"
|
||||
)
|
||||
snapshot = read_physical_file(
|
||||
file_path, "Gemini CLI user settings", MAX_SOURCE_BYTES
|
||||
)
|
||||
if snapshot is None:
|
||||
return create_gemini_document(file_path)
|
||||
return parse_gemini_document(
|
||||
exists=True,
|
||||
file_path=file_path,
|
||||
raw=snapshot.contents,
|
||||
)
|
||||
|
||||
|
||||
def rendered_change(
|
||||
rendered: RenderedGeminiDocument,
|
||||
) -> Optional[FileChange]:
|
||||
if not rendered.changed:
|
||||
return None
|
||||
return source_change(
|
||||
rendered.document.file_path,
|
||||
"Gemini CLI user settings",
|
||||
rendered.document.raw if rendered.document.exists else None,
|
||||
rendered.next_source,
|
||||
0o600,
|
||||
MAX_SOURCE_BYTES,
|
||||
)
|
||||
|
||||
|
||||
def write_gemini_document(rendered: RenderedGeminiDocument) -> None:
|
||||
change = rendered_change(rendered)
|
||||
if change is not None:
|
||||
write_changes([change], "Uninstall Gemini CLI hooks")
|
||||
|
||||
|
||||
def validate_api_origin(value: str, label: str = "base_url") -> None:
|
||||
try:
|
||||
parsed = urllib.parse.urlsplit(value)
|
||||
hostname = parsed.hostname
|
||||
parsed.port
|
||||
except ValueError as error:
|
||||
raise ValueError(f"{label} must be an absolute HTTP or HTTPS URL") from error
|
||||
invalid_character = "\\" in value or any(
|
||||
character.isspace() or ord(character) < 32 for character in value
|
||||
)
|
||||
if (
|
||||
parsed.scheme not in ("http", "https")
|
||||
or not parsed.netloc
|
||||
or hostname is None
|
||||
or invalid_character
|
||||
):
|
||||
raise ValueError(f"{label} must be an absolute HTTP or HTTPS URL")
|
||||
if (
|
||||
parsed.username is not None
|
||||
or parsed.password is not None
|
||||
or parsed.query
|
||||
or parsed.fragment
|
||||
):
|
||||
raise ValueError(
|
||||
f"{label} must exclude credentials, query parameters, and fragments"
|
||||
)
|
||||
|
||||
|
||||
def validate_private_key(value: str, label: str = "private_key") -> None:
|
||||
try:
|
||||
padded = value + "=" * ((4 - len(value) % 4) % 4)
|
||||
seed = base64.b64decode(
|
||||
padded.replace("-", "+").replace("_", "/"), validate=True
|
||||
)
|
||||
canonical = base64.urlsafe_b64encode(seed).rstrip(b"=").decode("ascii")
|
||||
except (ValueError, UnicodeEncodeError) as error:
|
||||
raise ValueError(
|
||||
f"{label} must be a canonical 32-byte base64url value"
|
||||
) from error
|
||||
if len(seed) != 32 or canonical != value:
|
||||
raise ValueError(f"{label} must be a canonical 32-byte base64url value")
|
||||
|
||||
|
||||
def _require_non_empty_string(
|
||||
value: Any, field: str, config_path: str
|
||||
) -> str:
|
||||
if not isinstance(value, str) or not value.strip():
|
||||
raise ValueError(
|
||||
f"Elydora runtime config {field} is invalid: {config_path}"
|
||||
)
|
||||
return value
|
||||
|
||||
|
||||
def _validate_runtime_config(
|
||||
config: JsonObject, expected_agent_id: str, config_path: str
|
||||
) -> None:
|
||||
supported = {"org_id", "agent_id", "kid", "base_url", "token", "agent_name"}
|
||||
extra = next((key for key in config if key not in supported), None)
|
||||
if extra is not None:
|
||||
raise ValueError(
|
||||
f'Elydora runtime config has unsupported field "{extra}": {config_path}'
|
||||
)
|
||||
_require_non_empty_string(config.get("org_id"), "org_id", config_path)
|
||||
_require_non_empty_string(config.get("kid"), "kid", config_path)
|
||||
agent_id = _require_non_empty_string(
|
||||
config.get("agent_id"), "agent_id", config_path
|
||||
)
|
||||
if (
|
||||
not same_gemini_agent_id(agent_id, expected_agent_id)
|
||||
or config.get("agent_name") != AGENT_KEY
|
||||
):
|
||||
raise ValueError(
|
||||
"Elydora runtime identity does not match Gemini CLI hooks: "
|
||||
f"{config_path}"
|
||||
)
|
||||
if "token" in config:
|
||||
_require_non_empty_string(config.get("token"), "token", config_path)
|
||||
base_url = _require_non_empty_string(
|
||||
config.get("base_url"), "base_url", config_path
|
||||
)
|
||||
validate_api_origin(base_url, "Elydora runtime config base_url")
|
||||
|
||||
|
||||
def _read_runtime_config(file_path: str) -> Optional[JsonObject]:
|
||||
snapshot = read_physical_file(
|
||||
file_path, "Elydora runtime config", MAX_CONFIG_BYTES
|
||||
)
|
||||
if snapshot is None:
|
||||
return None
|
||||
return parse_json_object(
|
||||
snapshot.contents, f"Elydora runtime config at {file_path}"
|
||||
)
|
||||
|
||||
|
||||
def validate_runtime_tree(agent_directory: str, agent_id: str) -> None:
|
||||
root = elydora_dir()
|
||||
if not physical_directory_exists(root, "Elydora runtime directory"):
|
||||
return
|
||||
if not physical_directory_exists(
|
||||
agent_directory, "Elydora agent runtime directory"
|
||||
):
|
||||
return
|
||||
config_path = os.path.join(agent_directory, "config.json")
|
||||
config = _read_runtime_config(config_path)
|
||||
artifacts = (
|
||||
("private.key", "Elydora private key"),
|
||||
("guard.py", "Elydora guard runtime"),
|
||||
("hook.py", "Elydora audit runtime"),
|
||||
("chain-state.json", "Elydora chain state"),
|
||||
("status-cache.json", "Elydora status cache"),
|
||||
("error.log", "Elydora error log"),
|
||||
)
|
||||
artifact_exists = any(
|
||||
physical_file_exists(os.path.join(agent_directory, name), label)
|
||||
for name, label in artifacts
|
||||
)
|
||||
if config is None:
|
||||
if artifact_exists:
|
||||
raise ValueError(
|
||||
"Elydora runtime identity cannot be verified without config.json: "
|
||||
f"{agent_directory}"
|
||||
)
|
||||
return
|
||||
configured_id = config.get("agent_id")
|
||||
if (
|
||||
not isinstance(configured_id, str)
|
||||
or not same_gemini_agent_id(configured_id, agent_id)
|
||||
or config.get("agent_name") != AGENT_KEY
|
||||
):
|
||||
raise ValueError(
|
||||
"Elydora runtime config identity does not match Gemini CLI agent "
|
||||
f"{agent_id}: {config_path}"
|
||||
)
|
||||
|
||||
|
||||
def _runtime_contract_exists(contract: GeminiRuntimeContract) -> bool:
|
||||
root = elydora_dir()
|
||||
agent_directory = os.path.dirname(contract.guard_path)
|
||||
if (
|
||||
not same_gemini_path(os.path.dirname(agent_directory), root)
|
||||
or not same_gemini_path(
|
||||
contract.audit_path, os.path.join(agent_directory, "hook.py")
|
||||
)
|
||||
):
|
||||
return False
|
||||
if not physical_directory_exists(root, "Elydora runtime directory"):
|
||||
return False
|
||||
if not physical_directory_exists(
|
||||
agent_directory, "Elydora agent runtime directory"
|
||||
):
|
||||
return False
|
||||
config_path = os.path.join(agent_directory, "config.json")
|
||||
key_path = os.path.join(agent_directory, "private.key")
|
||||
config = _read_runtime_config(config_path)
|
||||
key = read_physical_file(key_path, "Elydora private key", MAX_SECRET_BYTES)
|
||||
guard = read_physical_file(contract.guard_path, "Elydora guard runtime")
|
||||
audit = read_physical_file(contract.audit_path, "Elydora audit runtime")
|
||||
if config is None or key is None or guard is None or audit is None:
|
||||
return False
|
||||
_validate_runtime_config(config, contract.agent_id, config_path)
|
||||
validate_private_key(key.contents, "Elydora private key")
|
||||
return bool(guard.contents) and bool(audit.contents)
|
||||
|
||||
|
||||
def gemini_runtime_files_exist(
|
||||
contracts: List[GeminiRuntimeContract],
|
||||
) -> bool:
|
||||
return any(_runtime_contract_exists(contract) for contract in contracts)
|
||||
@@ -43,8 +43,8 @@ SUPPORTED_AGENTS: Dict[str, Dict[str, str]] = {
|
||||
},
|
||||
"gemini": {
|
||||
"name": "Gemini CLI",
|
||||
"hook_event": "AfterTool",
|
||||
"config_path": "~/.gemini/settings.json",
|
||||
"hook_event": "BeforeTool/AfterTool",
|
||||
"config_path": "$GEMINI_CLI_HOME/.gemini/settings.json",
|
||||
},
|
||||
"grok": {
|
||||
"name": "Grok Build",
|
||||
|
||||
Reference in new issue
Block a user