# auto-generated by @connexum/ai-governance — do not edit; regenerate via npx ai-governance init --agent-dir
# agent-id: {{AGENT_ID}}
# source:   {{SOURCE_FILE}}
"""
Governance shim for {{MODULE_NAME}}.
Wraps Ollama subprocess.run calls with Connexum governance tool-call hooks.
Ollama runs locally — no BAA required, but governance audit logging still applies.
Change pack assignments in governance.json; regenerate this file to apply.
"""
from __future__ import annotations

import importlib
import importlib.util
import subprocess
import sys
import os
from pathlib import Path
from typing import Any

# ---------------------------------------------------------------------------
# Load original module
# ---------------------------------------------------------------------------
_src_dir = Path(__file__).parent
_orig_spec = importlib.util.spec_from_file_location(
    "{{MODULE_NAME}}._original",
    _src_dir / "{{SOURCE_FILE}}",
)
_orig_mod = importlib.util.module_from_spec(_orig_spec)  # type: ignore[arg-type]
_orig_spec.loader.exec_module(_orig_mod)  # type: ignore[union-attr]

# ---------------------------------------------------------------------------
# Governed subprocess wrapper for Ollama
# ---------------------------------------------------------------------------
from connexum_governance import GovernanceClient
from connexum_governance.integrations.ollama import OllamaGovernance

_gov_client = GovernanceClient(
    api_url=os.environ.get("CONNEXUM_API_URL", "http://localhost:4201"),
    license_key=os.environ.get("CONNEXUM_LICENSE_KEY", ""),
)
_gov = OllamaGovernance(
    client=_gov_client,
    agent_name="{{AGENT_ID}}",
    packs={{PACKS}},
)

# Patch subprocess.run in the original module's namespace to route through
# the governance tool-call hook when the command targets ollama.
_original_subprocess_run = subprocess.run

def _governed_subprocess_run(args_: Any, **kwargs: Any) -> Any:
    _cmd = args_ if isinstance(args_, (list, tuple)) else [args_]
    if _cmd and str(_cmd[0]).lower() == "ollama":
        _gov.pre_subprocess_hook(_cmd)
    result = _original_subprocess_run(args_, **kwargs)
    if _cmd and str(_cmd[0]).lower() == "ollama":
        _gov.post_subprocess_hook(_cmd, result)
    return result

# Patch within the original module's subprocess reference
import types as _types
if hasattr(_orig_mod, "subprocess"):
    _patched_subprocess = _types.ModuleType("subprocess")
    _patched_subprocess.__dict__.update(_orig_mod.subprocess.__dict__)
    _patched_subprocess.run = _governed_subprocess_run  # type: ignore[attr-defined]
    _orig_mod.subprocess = _patched_subprocess

# ---------------------------------------------------------------------------
# Re-export original module's public API
# ---------------------------------------------------------------------------
for _name in dir(_orig_mod):
    if not _name.startswith("_"):
        globals()[_name] = getattr(_orig_mod, _name)

__all__ = [n for n in dir(_orig_mod) if not n.startswith("_")]
