From 227ab3ac9ec5ca0724fdf543e42d0cafb174796a Mon Sep 17 00:00:00 2001 From: logohere <69400476+logohere@users.noreply.github.com> Date: Wed, 11 Nov 2026 18:15:08 -0500 Subject: [PATCH 1/2] feat: preserve local recall and Codex broker work --- README.md | 1 + agent/agent_init.py | 103 +++++---- agent/local_recall_provider.py | 270 ++++++++++++++++++++++ dag_cli/codex_broker.py | 346 ++++++++++++++++++++++++++++ pyproject.toml | 3 + scripts/codex_broker.py | 331 ++++++++++++++++++++++++++ scripts/llm-call-optimizer.sh | 10 + tests/test_codex_broker.py | 23 ++ tests/test_local_recall_provider.py | 59 +++++ 9 files changed, 1098 insertions(+), 48 deletions(-) create mode 100644 agent/local_recall_provider.py create mode 100644 dag_cli/codex_broker.py create mode 100644 scripts/codex_broker.py create mode 100644 scripts/llm-call-optimizer.sh create mode 100644 tests/test_codex_broker.py create mode 100644 tests/test_local_recall_provider.py diff --git a/README.md b/README.md index 1aaa419..e300564 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ collar setup # first-time setup wizard ## Related - [dotdog](https://github.com/specdog/dotdog) — `.dog` spec format + compiler +- [dogbench](https://github.com/specdog/dogbench) — token-savings benchmark harness - [specdog](https://github.com/specdog) — the org - PRs use an agentic template: `.github/pull_request_template.md` diff --git a/agent/agent_init.py b/agent/agent_init.py index 72d485b..3ec3aea 100644 --- a/agent/agent_init.py +++ b/agent/agent_init.py @@ -1131,69 +1131,76 @@ def init_agent( - # Memory provider plugin (external — one at a time, alongside built-in) - # Reads memory.provider from config to select which plugin to activate. + # Memory providers: always-on builtin local recall plus at most one + # configured external plugin provider. agent._memory_manager = None if not skip_memory: try: + from agent.local_recall_provider import LocalRecallProvider + from agent.memory_manager import MemoryManager as _MemoryManager + _mem_provider_name = mem_config.get("provider", "") if mem_config else "" + agent._memory_manager = _MemoryManager() + agent._memory_manager.add_provider(LocalRecallProvider()) if _mem_provider_name and _mem_provider_name.strip(): - from agent.memory_manager import MemoryManager as _MemoryManager from plugins.memory import load_memory_provider as _load_mem - agent._memory_manager = _MemoryManager() + _mp = _load_mem(_mem_provider_name) if _mp and _mp.is_available(): agent._memory_manager.add_provider(_mp) - if agent._memory_manager.providers: - _init_kwargs = { - "session_id": agent.session_id, - "platform": platform or "cli", - "dag_home": str(get_dag_home()), - "agent_context": "primary", - } - # Thread session title for memory provider scoping - # (e.g. honcho uses this to derive chat-scoped session keys) - if agent._session_db: - try: - _st = agent._session_db.get_session_title(agent.session_id) - if _st: - _init_kwargs["session_title"] = _st - except Exception: - pass - # Thread gateway user identity for per-user memory scoping - if agent._user_id: - _init_kwargs["user_id"] = agent._user_id - if agent._user_id_alt: - _init_kwargs["user_id_alt"] = agent._user_id_alt - if agent._user_name: - _init_kwargs["user_name"] = agent._user_name - if agent._chat_id: - _init_kwargs["chat_id"] = agent._chat_id - if agent._chat_name: - _init_kwargs["chat_name"] = agent._chat_name - if agent._chat_type: - _init_kwargs["chat_type"] = agent._chat_type - if agent._thread_id: - _init_kwargs["thread_id"] = agent._thread_id - # Thread gateway session key for stable per-chat Honcho session isolation - if agent._gateway_session_key: - _init_kwargs["gateway_session_key"] = agent._gateway_session_key - # Profile identity for per-profile provider scoping + + if agent._memory_manager.providers: + _init_kwargs = { + "session_id": agent.session_id, + "platform": platform or "cli", + "dag_home": str(get_dag_home()), + "agent_context": "primary", + "cwd": getattr(agent, "session_cwd", None) or os.getcwd(), + "session_db": agent._session_db, + } + # Thread session title for memory provider scoping + # (e.g. honcho uses this to derive chat-scoped session keys) + if agent._session_db: try: - from dag_cli.profiles import get_active_profile_name - _profile = get_active_profile_name() - _init_kwargs["agent_identity"] = _profile - _init_kwargs["agent_workspace"] = "dag" + _st = agent._session_db.get_session_title(agent.session_id) + if _st: + _init_kwargs["session_title"] = _st except Exception: pass - agent._memory_manager.initialize_all(**_init_kwargs) + # Thread gateway user identity for per-user memory scoping + if agent._user_id: + _init_kwargs["user_id"] = agent._user_id + if agent._user_id_alt: + _init_kwargs["user_id_alt"] = agent._user_id_alt + if agent._user_name: + _init_kwargs["user_name"] = agent._user_name + if agent._chat_id: + _init_kwargs["chat_id"] = agent._chat_id + if agent._chat_name: + _init_kwargs["chat_name"] = agent._chat_name + if agent._chat_type: + _init_kwargs["chat_type"] = agent._chat_type + if agent._thread_id: + _init_kwargs["thread_id"] = agent._thread_id + # Thread gateway session key for stable per-chat Honcho session isolation + if agent._gateway_session_key: + _init_kwargs["gateway_session_key"] = agent._gateway_session_key + # Profile identity for per-profile provider scoping + try: + from dag_cli.profiles import get_active_profile_name + _profile = get_active_profile_name() + _init_kwargs["agent_identity"] = _profile + _init_kwargs["agent_workspace"] = "dag" + except Exception: + pass + agent._memory_manager.initialize_all(**_init_kwargs) + if _mem_provider_name and _mem_provider_name.strip(): _ra().logger.info("Memory provider '%s' activated", _mem_provider_name) - else: - _ra().logger.debug("Memory provider '%s' not found or not available", _mem_provider_name) - agent._memory_manager = None + else: + agent._memory_manager = None except Exception as _mpe: - _ra().logger.warning("Memory provider plugin init failed: %s", _mpe) + _ra().logger.warning("Memory provider init failed: %s", _mpe) agent._memory_manager = None from agent.memory_manager import inject_memory_provider_tools as _inject_memory_provider_tools diff --git a/agent/local_recall_provider.py b/agent/local_recall_provider.py new file mode 100644 index 0000000..a3a0850 --- /dev/null +++ b/agent/local_recall_provider.py @@ -0,0 +1,270 @@ +"""Built-in local recall provider. + +Cheap, file-backed exact-turn memoization plus optional zero-LLM session +search for explicit recall-style queries. This provider is always safe to run: +it never makes network calls, never mutates the system prompt mid-session, and +coexists with one external memory provider under MemoryManager. +""" + +from __future__ import annotations + +import hashlib +import json +import logging +import os +import re +import subprocess +import time +from pathlib import Path +from typing import Any, Dict, List, Optional + +from agent.memory_provider import MemoryProvider +from utils import atomic_json_write + +logger = logging.getLogger(__name__) + +_MAX_CACHE_ENTRIES = 256 +_MAX_ANSWER_CHARS = 2400 +_MAX_PREFETCH_CHARS = 1600 +_CACHE_TTL_SECONDS = 30 * 24 * 60 * 60 +_RECALL_QUERY_RE = re.compile( + r"\b(" + r"remember|recall|previous|earlier|before|last time|what did|" + r"how did|which file|where did|search session|past session" + r")\b", + re.IGNORECASE, +) + + +def _normalize_query(text: str) -> str: + collapsed = re.sub(r"\s+", " ", (text or "").strip().lower()) + return collapsed[:400] + + +def _sha(text: str) -> str: + return hashlib.sha256(text.encode("utf-8")).hexdigest() + + +def _run(cmd: list[str], cwd: Optional[str] = None) -> tuple[int, str]: + try: + proc = subprocess.run( + cmd, + cwd=cwd, + capture_output=True, + text=True, + check=False, + ) + except FileNotFoundError: + return 127, "" + out = proc.stdout.strip() + err = proc.stderr.strip() + if out and err: + return proc.returncode, out + "\n" + err + return proc.returncode, out or err + + +def _git_root(path: str) -> str: + code, out = _run(["git", "-C", path, "rev-parse", "--show-toplevel"]) + if code == 0 and out: + return out.splitlines()[0] + return path + + +def _repo_fingerprint(path: str) -> str: + root = _git_root(path) + head_code, head = _run(["git", "-C", root, "rev-parse", "HEAD"]) + status_code, status = _run(["git", "-C", root, "status", "--short"]) + payload = "\n".join( + [ + root, + head if head_code == 0 else "", + status if status_code == 0 else "", + ] + ) + return _sha(payload) + + +class LocalRecallProvider(MemoryProvider): + """Always-on local recall provider with exact-turn memoization.""" + + @property + def name(self) -> str: + return "builtin" + + def __init__(self) -> None: + self._session_id = "" + self._dag_home = Path.home() / ".dag" + self._cwd = os.getcwd() + self._cache_path = self._dag_home / "cache" / "local_recall.json" + self._cache: Dict[str, Dict[str, Any]] = {} + self._session_db = None + + def is_available(self) -> bool: + return True + + def initialize(self, session_id: str, **kwargs) -> None: + self._session_id = session_id + self._dag_home = Path(str(kwargs.get("dag_home") or self._dag_home)) + self._cwd = str(kwargs.get("cwd") or self._cwd) + self._cache_path = self._dag_home / "cache" / "local_recall.json" + self._session_db = kwargs.get("session_db") + self._load_cache() + + def get_tool_schemas(self) -> List[Dict[str, Any]]: + return [] + + def prefetch(self, query: str, *, session_id: str = "") -> str: + norm = _normalize_query(query) + if len(norm) < 8: + return "" + + exact = self._lookup_exact(norm) + if exact: + answer = str(exact.get("assistant") or "")[:_MAX_PREFETCH_CHARS] + if answer: + return ( + "LOCAL RECALL:\n" + "Exact prior answer for the same workspace state and request:\n" + f"{answer}" + ) + + if not _RECALL_QUERY_RE.search(query or ""): + return "" + + rows = self._search_sessions(norm) + if not rows: + return "" + + parts = ["SESSION RECALL:"] + for row in rows[:2]: + snippet = str(row.get("snippet") or row.get("content") or "").strip() + if not snippet: + continue + snippet = re.sub(r"\s+", " ", snippet)[:320] + parts.append( + f"- {row.get('role') or 'message'} in session {row.get('session_id')}: {snippet}" + ) + return "\n".join(parts)[:_MAX_PREFETCH_CHARS] + + def sync_turn( + self, + user_content: str, + assistant_content: str, + *, + session_id: str = "", + messages: Optional[List[Dict[str, Any]]] = None, + ) -> None: + norm = _normalize_query(user_content) + answer = (assistant_content or "").strip() + if len(norm) < 8 or not answer: + return + if messages and _turn_used_tools(messages): + return + + try: + repo_fp = _repo_fingerprint(self._cwd) + except Exception: + repo_fp = _sha(self._cwd) + + key = _sha(f"{self._cwd}\n{repo_fp}\n{norm}") + self._cache[key] = { + "query": norm, + "assistant": answer[:_MAX_ANSWER_CHARS], + "cwd": self._cwd, + "repo_fp": repo_fp, + "session_id": session_id or self._session_id, + "ts": time.time(), + } + self._prune_cache() + self._save_cache() + + def on_session_switch( + self, + new_session_id: str, + *, + parent_session_id: str = "", + reset: bool = False, + rewound: bool = False, + **kwargs, + ) -> None: + self._session_id = new_session_id + + def _lookup_exact(self, norm_query: str) -> Optional[Dict[str, Any]]: + try: + repo_fp = _repo_fingerprint(self._cwd) + except Exception: + repo_fp = _sha(self._cwd) + key = _sha(f"{self._cwd}\n{repo_fp}\n{norm_query}") + entry = self._cache.get(key) + if not entry: + return None + if (time.time() - float(entry.get("ts") or 0)) > _CACHE_TTL_SECONDS: + self._cache.pop(key, None) + return None + return entry + + def _search_sessions(self, norm_query: str) -> List[Dict[str, Any]]: + db = self._session_db + if db is None: + try: + from dag_state import SessionDB + + db = SessionDB( + db_path=self._dag_home / "state.db", + read_only=True, + ) + except Exception: + logger.debug("local recall: failed to open state.db", exc_info=True) + return [] + try: + return db.search_messages(norm_query, limit=2, sort="newest") or [] + except Exception: + logger.debug("local recall: session search failed", exc_info=True) + return [] + + def _load_cache(self) -> None: + try: + raw = json.loads(self._cache_path.read_text(encoding="utf-8")) + except (FileNotFoundError, OSError, json.JSONDecodeError): + self._cache = {} + return + self._cache = raw if isinstance(raw, dict) else {} + self._prune_cache() + + def _save_cache(self) -> None: + try: + atomic_json_write( + self._cache_path, + self._cache, + indent=2, + sort_keys=True, + ) + except Exception: + logger.debug("local recall: cache write failed", exc_info=True) + + def _prune_cache(self) -> None: + now = time.time() + items = [ + (key, value) + for key, value in self._cache.items() + if isinstance(value, dict) + and (now - float(value.get("ts") or 0)) <= _CACHE_TTL_SECONDS + ] + items.sort(key=lambda item: float(item[1].get("ts") or 0), reverse=True) + self._cache = dict(items[:_MAX_CACHE_ENTRIES]) + + +def _turn_used_tools(messages: List[Dict[str, Any]]) -> bool: + last_user_idx = -1 + for idx, msg in enumerate(messages): + if msg.get("role") == "user": + last_user_idx = idx + if last_user_idx < 0: + return False + for msg in messages[last_user_idx + 1:]: + if msg.get("role") == "tool": + return True + if msg.get("tool_calls"): + return True + return False + diff --git a/dag_cli/codex_broker.py b/dag_cli/codex_broker.py new file mode 100644 index 0000000..ca297e2 --- /dev/null +++ b/dag_cli/codex_broker.py @@ -0,0 +1,346 @@ +#!/usr/bin/env python3 +"""Stateless local-first broker for Codex CLI. + +Optimizations: +- exact response cache keyed by repo fingerprint + normalized prompt +- local short-circuit for common read/search/status requests +- compact prompt packing from git state + relevant file snippets + +This wrapper is intentionally stateless per turn. It is safe to cache final +answers because it does not rely on Codex app-server's hidden thread memory. +""" + +from __future__ import annotations + +import argparse +import hashlib +import json +import os +import re +import subprocess +import sys +import time +from pathlib import Path + + +LOCAL_PREFIXES = ("git ", "rg ", "find ", "ls", "sed ", "cat ", "pwd", "date") +MAX_RELEVANT_FILES = 4 +MAX_SNIPPET_LINES = 40 +MAX_CACHE_ENTRIES = 400 +CACHE_TTL_SECONDS = 30 * 24 * 60 * 60 +_PACK_HINT_RE = re.compile( + r"\b(" + r"fix|failing|test|bug|regression|error|traceback|stack|refactor|" + r"implement|edit|patch|change|update|add|remove|rename|file|repo|" + r"codebase|function|class|module|import|build|compile|lint" + r")\b", + re.IGNORECASE, +) + + +def _run(cmd: list[str], cwd: str | None = None) -> tuple[int, str]: + try: + proc = subprocess.run( + cmd, + cwd=cwd, + capture_output=True, + text=True, + check=False, + ) + except FileNotFoundError: + return 127, "" + out = proc.stdout.strip() + err = proc.stderr.strip() + if out and err: + return proc.returncode, out + "\n" + err + return proc.returncode, out or err + + +def _run_shell(cmd: str, cwd: str) -> int: + return subprocess.run(cmd, cwd=cwd, shell=True, check=False).returncode + + +def _git_root(root: str) -> str: + code, out = _run(["git", "-C", root, "rev-parse", "--show-toplevel"]) + if code == 0 and out: + return out.splitlines()[0] + return root + + +def _sha(text: str) -> str: + return hashlib.sha256(text.encode("utf-8")).hexdigest() + + +def _normalize_prompt(prompt: str) -> str: + return re.sub(r"\s+", " ", prompt.strip().lower())[:1200] + + +def _repo_fingerprint(root: str) -> str: + root = _git_root(root) + head_code, head = _run(["git", "-C", root, "rev-parse", "HEAD"]) + status_code, status = _run(["git", "-C", root, "status", "--short"]) + payload = "\n".join( + [root, head if head_code == 0 else "", status if status_code == 0 else ""] + ) + return _sha(payload) + + +def _cache_dir(root: str) -> Path: + d = Path(root) / ".codex-broker" + d.mkdir(parents=True, exist_ok=True) + return d + + +def _cache_path(root: str) -> Path: + return _cache_dir(root) / "responses.json" + + +def _load_cache(root: str) -> dict[str, dict]: + path = _cache_path(root) + try: + data = json.loads(path.read_text()) + except Exception: + return {} + return data if isinstance(data, dict) else {} + + +def _save_cache(root: str, payload: dict[str, dict]) -> None: + items = sorted( + payload.items(), + key=lambda item: float(item[1].get("ts") or 0), + reverse=True, + )[:MAX_CACHE_ENTRIES] + _cache_path(root).write_text(json.dumps(dict(items), indent=2)) + + +def _cache_key(root: str, fingerprint: str, prompt: str) -> str: + return _sha("\n".join([root, fingerprint, _normalize_prompt(prompt), "v1"])) + + +def _keywords(prompt: str) -> list[str]: + seen: list[str] = [] + for token in re.findall(r"[A-Za-z0-9_./-]{4,}", prompt.lower()): + if token not in seen: + seen.append(token) + if len(seen) >= 8: + break + return seen + + +def _read_file_prompt(prompt: str, root: str) -> str | None: + m = re.match(r"^(?:read|show|open|cat)\s+(.+)$", prompt.strip(), re.IGNORECASE) + if not m: + return None + raw = m.group(1).strip().strip("\"'") + path = raw if os.path.isabs(raw) else str(Path(root) / raw) + if not os.path.isfile(path): + return f"Missing file: {path}" + _, out = _run(["sed", "-n", "1,200p", path]) + return f"--- {path}\n{out}" + + +def _search_prompt(prompt: str, root: str) -> str | None: + m = re.match(r"^(?:search|find text|grep)\s+(.+)$", prompt.strip(), re.IGNORECASE) + if not m: + return None + pattern = m.group(1).strip().strip("\"'") + if not pattern: + return "" + _, out = _run(["rg", "-n", "-g", "!node_modules", "-g", "!.git", pattern, root]) + return out + + +def _diagnostic_prompt(prompt: str) -> list[str] | None: + low = prompt.strip().lower() + if low in {"status", "git status"}: + return ["git", "status", "--short", "--branch"] + if low in {"branch", "git branch"}: + return ["git", "branch", "--show-current"] + if low in {"changed files", "git diff", "diff"}: + return ["git", "diff", "--stat"] + if low in {"list files", "files"}: + return ["rg", "--files", "."] + if low == "pwd": + return ["pwd"] + if low == "date": + return ["date"] + return None + + +def _should_run_local(prompt: str, root: str) -> bool: + low = prompt.strip().lower() + if prompt.startswith("!"): + return True + if low.startswith(LOCAL_PREFIXES): + return True + if _read_file_prompt(prompt, root) is not None: + return True + if _search_prompt(prompt, root) is not None: + return True + return _diagnostic_prompt(prompt) is not None + + +def _handle_local(prompt: str, root: str) -> int: + if prompt.startswith("!"): + return _run_shell(prompt[1:].strip(), root) + file_view = _read_file_prompt(prompt, root) + if file_view is not None: + print(file_view) + return 0 + search_view = _search_prompt(prompt, root) + if search_view is not None: + print(search_view) + return 0 + diagnostic = _diagnostic_prompt(prompt) + if diagnostic is None: + return 0 + if diagnostic == ["pwd"]: + print(root) + return 0 + code, out = _run(diagnostic, cwd=root) + if out: + print(out) + return code + + +def _resolve_candidate_files(root: str, prompt: str) -> list[str]: + root_path = Path(root) + files: list[str] = [] + seen: set[str] = set() + explicit_tokens = re.findall(r"(?:[A-Za-z0-9_.-]+/)*[A-Za-z0-9_.-]+\.[A-Za-z0-9_.-]+", prompt) + for token in explicit_tokens: + path = token if os.path.isabs(token) else str(root_path / token) + if os.path.isfile(path) and path not in seen: + seen.add(path) + files.append(path) + if len(files) >= MAX_RELEVANT_FILES: + return files + for key in _keywords(prompt): + _, out = _run(["rg", "-l", "-g", "!node_modules", "-g", "!.git", key, root]) + for line in out.splitlines(): + if line and line not in seen and os.path.isfile(line): + seen.add(line) + files.append(line) + if len(files) >= MAX_RELEVANT_FILES: + return files + return files + + +def _pack_context(root: str, prompt: str) -> str: + root = _git_root(root) + _, git_status = _run(["git", "-C", root, "status", "--short", "--branch"]) + _, git_diff = _run(["git", "-C", root, "diff", "--stat", "--", ".", ":(exclude)node_modules", ":(exclude).git"]) + parts = [ + f"WORKSPACE: {root}", + f"GOAL: {prompt}", + "RULE: do the cheapest useful work locally first.", + "RULE: keep context tight.", + "RULE: prefer direct commands and minimal diffs.", + "", + "GIT_STATUS:", + git_status, + "", + "GIT_DIFFSTAT:", + git_diff, + ] + for path in _resolve_candidate_files(root, prompt): + parts.extend(["", f"--- {path}"]) + _, snippet = _run(["sed", "-n", f"1,{MAX_SNIPPET_LINES}p", path]) + parts.append(snippet) + return "\n".join(parts).strip() + "\n" + + +def _should_pack_context(prompt: str) -> bool: + if _PACK_HINT_RE.search(prompt): + return True + if re.search(r"(?:[A-Za-z0-9_.-]+/)*[A-Za-z0-9_.-]+\.[A-Za-z0-9_.-]+", prompt): + return True + return len(_keywords(prompt)) >= 3 + + +def _lookup_cached_response(root: str, prompt: str) -> str | None: + fingerprint = _repo_fingerprint(root) + key = _cache_key(root, fingerprint, prompt) + cache = _load_cache(root) + entry = cache.get(key) + if not entry: + return None + if (time.time() - float(entry.get("ts") or 0)) > CACHE_TTL_SECONDS: + cache.pop(key, None) + _save_cache(root, cache) + return None + return str(entry.get("response") or "") + + +def _store_cached_response(root: str, prompt: str, response: str) -> None: + cache = _load_cache(root) + key = _cache_key(root, _repo_fingerprint(root), prompt) + cache[key] = { + "prompt": _normalize_prompt(prompt), + "response": response, + "ts": time.time(), + } + _save_cache(root, cache) + + +def _invoke_codex(codex_bin: str, prompt: str) -> tuple[int, str]: + proc = subprocess.run( + [codex_bin, prompt], + capture_output=True, + text=True, + check=False, + ) + out = proc.stdout.strip() + err = proc.stderr.strip() + if out and err: + return proc.returncode, out + "\n" + err + return proc.returncode, out or err + + +def main() -> int: + ap = argparse.ArgumentParser(add_help=True) + ap.add_argument("--root", default=os.getcwd()) + ap.add_argument("--codex-bin", default=os.environ.get("CODEX_BIN", "codex")) + ap.add_argument("--pack-only", action="store_true") + ap.add_argument("--cache-status", action="store_true") + ap.add_argument("prompt", nargs=argparse.REMAINDER) + ns = ap.parse_args() + + prompt_parts = ns.prompt + if prompt_parts and prompt_parts[0] == "--": + prompt_parts = prompt_parts[1:] + prompt = " ".join(prompt_parts).strip() + root = _git_root(os.path.abspath(ns.root)) + + if ns.cache_status: + cache = _load_cache(root) + print(json.dumps({"root": root, "entries": len(cache)})) + return 0 + if ns.pack_only: + sys.stdout.write(_pack_context(root, prompt)) + return 0 + if not prompt: + print("prompt required", file=sys.stderr) + return 2 + if _should_run_local(prompt, root): + return _handle_local(prompt, root) + cached = _lookup_cached_response(root, prompt) + if cached: + print(cached) + return 0 + outbound = _pack_context(root, prompt) if _should_pack_context(prompt) else prompt + code, out = _invoke_codex(ns.codex_bin, outbound) + if out: + print(out) + if code == 0 and out.strip(): + _store_cached_response(root, prompt, out.strip()) + return code + + +__all__ = [ + "_lookup_cached_response", + "_read_file_prompt", + "_resolve_candidate_files", + "_store_cached_response", + "main", +] diff --git a/pyproject.toml b/pyproject.toml index ba6f6c6..3ff178e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -281,6 +281,9 @@ collar = "dag_cli.main:main" dag = "dag_cli.main:main" dag-agent = "run_agent:main" dag-acp = "acp_adapter.entry:main" +collar-opt = "dag_cli.codex_broker:main" +dag-opt = "dag_cli.codex_broker:main" +codex-broker = "dag_cli.codex_broker:main" [tool.setuptools] py-modules = ["run_agent", "model_tools", "toolsets", "batch_runner", "trajectory_compressor", "toolset_distributions", "cli", "dag_bootstrap", "dag_constants", "dag_state", "dag_time", "dag_logging", "utils", "mcp_serve"] diff --git a/scripts/codex_broker.py b/scripts/codex_broker.py new file mode 100644 index 0000000..1f711d3 --- /dev/null +++ b/scripts/codex_broker.py @@ -0,0 +1,331 @@ +#!/usr/bin/env python3 +"""Stateless local-first broker for Codex CLI. + +Optimizations: +- exact response cache keyed by repo fingerprint + normalized prompt +- local short-circuit for common read/search/status requests +- compact prompt packing from relevant file snippets, with git state opt-in + +This wrapper is intentionally stateless per turn. It is safe to cache final +answers because it does not rely on Codex app-server's hidden thread memory. +""" + +from __future__ import annotations + +import argparse +import hashlib +import json +import os +import re +import subprocess +import sys +import time +from pathlib import Path + + +LOCAL_PREFIXES = ("git ", "rg ", "find ", "ls", "sed ", "cat ", "pwd", "date") +MAX_RELEVANT_FILES = 4 +MAX_SNIPPET_LINES = 40 +MAX_CACHE_ENTRIES = 400 +CACHE_TTL_SECONDS = 30 * 24 * 60 * 60 +INCLUDE_GIT_STATE = os.environ.get("CODEX_BROKER_INCLUDE_GIT_STATE") == "1" + + +def _run(cmd: list[str], cwd: str | None = None) -> tuple[int, str]: + try: + proc = subprocess.run( + cmd, + cwd=cwd, + capture_output=True, + text=True, + check=False, + ) + except FileNotFoundError: + return 127, "" + out = proc.stdout.strip() + err = proc.stderr.strip() + if out and err: + return proc.returncode, out + "\n" + err + return proc.returncode, out or err + + +def _run_shell(cmd: str, cwd: str) -> int: + return subprocess.run(cmd, cwd=cwd, shell=True, check=False).returncode + + +def _git_root(root: str) -> str: + code, out = _run(["git", "-C", root, "rev-parse", "--show-toplevel"]) + if code == 0 and out: + return out.splitlines()[0] + return root + + +def _sha(text: str) -> str: + return hashlib.sha256(text.encode("utf-8")).hexdigest() + + +def _normalize_prompt(prompt: str) -> str: + return re.sub(r"\s+", " ", prompt.strip().lower())[:1200] + + +def _repo_fingerprint(root: str) -> str: + root = _git_root(root) + head_code, head = _run(["git", "-C", root, "rev-parse", "HEAD"]) + status_code, status = _run(["git", "-C", root, "status", "--short"]) + payload = "\n".join( + [root, head if head_code == 0 else "", status if status_code == 0 else ""] + ) + return _sha(payload) + + +def _cache_dir(root: str) -> Path: + d = Path(root) / ".codex-broker" + d.mkdir(parents=True, exist_ok=True) + return d + + +def _cache_path(root: str) -> Path: + return _cache_dir(root) / "responses.json" + + +def _load_cache(root: str) -> dict[str, dict]: + path = _cache_path(root) + try: + data = json.loads(path.read_text()) + except Exception: + return {} + return data if isinstance(data, dict) else {} + + +def _save_cache(root: str, payload: dict[str, dict]) -> None: + items = sorted( + payload.items(), + key=lambda item: float(item[1].get("ts") or 0), + reverse=True, + )[:MAX_CACHE_ENTRIES] + _cache_path(root).write_text(json.dumps(dict(items), indent=2)) + + +def _cache_key(root: str, fingerprint: str, prompt: str) -> str: + return _sha("\n".join([root, fingerprint, _normalize_prompt(prompt), "v1"])) + + +def _keywords(prompt: str) -> list[str]: + seen: list[str] = [] + for token in re.findall(r"[A-Za-z0-9_./-]{4,}", prompt.lower()): + if token not in seen: + seen.append(token) + if len(seen) >= 8: + break + return seen + + +def _read_file_prompt(prompt: str, root: str) -> str | None: + m = re.match(r"^(?:read|show|open|cat)\s+(.+)$", prompt.strip(), re.IGNORECASE) + if not m: + return None + raw = m.group(1).strip().strip("\"'") + path = raw if os.path.isabs(raw) else str(Path(root) / raw) + if not os.path.isfile(path): + return f"Missing file: {path}" + _, out = _run(["sed", "-n", "1,200p", path]) + return f"--- {path}\n{out}" + + +def _search_prompt(prompt: str, root: str) -> str | None: + m = re.match(r"^(?:search|find text|grep)\s+(.+)$", prompt.strip(), re.IGNORECASE) + if not m: + return None + pattern = m.group(1).strip().strip("\"'") + if not pattern: + return "" + _, out = _run(["rg", "-n", "-g", "!node_modules", "-g", "!.git", pattern, root]) + return out + + +def _diagnostic_prompt(prompt: str) -> list[str] | None: + low = prompt.strip().lower() + if low in {"status", "git status"}: + return ["git", "status", "--short", "--branch"] + if low in {"branch", "git branch"}: + return ["git", "branch", "--show-current"] + if low in {"changed files", "git diff", "diff"}: + return ["git", "diff", "--stat"] + if low in {"list files", "files"}: + return ["rg", "--files", "."] + if low == "pwd": + return ["pwd"] + if low == "date": + return ["date"] + return None + + +def _should_run_local(prompt: str, root: str) -> bool: + low = prompt.strip().lower() + if prompt.startswith("!"): + return True + if low.startswith(LOCAL_PREFIXES): + return True + if _read_file_prompt(prompt, root) is not None: + return True + if _search_prompt(prompt, root) is not None: + return True + return _diagnostic_prompt(prompt) is not None + + +def _handle_local(prompt: str, root: str) -> int: + if prompt.startswith("!"): + return _run_shell(prompt[1:].strip(), root) + file_view = _read_file_prompt(prompt, root) + if file_view is not None: + print(file_view) + return 0 + search_view = _search_prompt(prompt, root) + if search_view is not None: + print(search_view) + return 0 + diagnostic = _diagnostic_prompt(prompt) + if diagnostic is None: + return 0 + if diagnostic == ["pwd"]: + print(root) + return 0 + code, out = _run(diagnostic, cwd=root) + if out: + print(out) + return code + + +def _resolve_candidate_files(root: str, prompt: str) -> list[str]: + root_path = Path(root) + files: list[str] = [] + seen: set[str] = set() + explicit_tokens = re.findall(r"(?:[A-Za-z0-9_.-]+/)*[A-Za-z0-9_.-]+\.[A-Za-z0-9_.-]+", prompt) + for token in explicit_tokens: + path = token if os.path.isabs(token) else str(root_path / token) + if os.path.isfile(path) and path not in seen: + seen.add(path) + files.append(path) + if len(files) >= MAX_RELEVANT_FILES: + return files + for key in _keywords(prompt): + _, out = _run(["rg", "-l", "-g", "!node_modules", "-g", "!.git", key, root]) + for line in out.splitlines(): + if line and line not in seen and os.path.isfile(line): + seen.add(line) + files.append(line) + if len(files) >= MAX_RELEVANT_FILES: + return files + return files + + +def _pack_context(root: str, prompt: str) -> str: + root = _git_root(root) + parts = [ + f"WORKSPACE: {root}", + f"GOAL: {prompt}", + "RULE: do the cheapest useful work locally first.", + "RULE: keep context tight.", + "RULE: prefer direct commands and minimal diffs.", + ] + if INCLUDE_GIT_STATE: + _, git_status = _run(["git", "-C", root, "status", "--short", "--branch"]) + _, git_diff = _run(["git", "-C", root, "diff", "--stat", "--", ".", ":(exclude)node_modules", ":(exclude).git"]) + parts.extend( + [ + "", + "GIT_STATUS:", + git_status, + "", + "GIT_DIFFSTAT:", + git_diff, + ] + ) + for path in _resolve_candidate_files(root, prompt): + parts.extend(["", f"--- {path}"]) + _, snippet = _run(["sed", "-n", f"1,{MAX_SNIPPET_LINES}p", path]) + parts.append(snippet) + return "\n".join(parts).strip() + "\n" + + +def _lookup_cached_response(root: str, prompt: str) -> str | None: + fingerprint = _repo_fingerprint(root) + key = _cache_key(root, fingerprint, prompt) + cache = _load_cache(root) + entry = cache.get(key) + if not entry: + return None + if (time.time() - float(entry.get("ts") or 0)) > CACHE_TTL_SECONDS: + cache.pop(key, None) + _save_cache(root, cache) + return None + return str(entry.get("response") or "") + + +def _store_cached_response(root: str, prompt: str, response: str) -> None: + cache = _load_cache(root) + key = _cache_key(root, _repo_fingerprint(root), prompt) + cache[key] = { + "prompt": _normalize_prompt(prompt), + "response": response, + "ts": time.time(), + } + _save_cache(root, cache) + + +def _invoke_codex(codex_bin: str, prompt: str) -> tuple[int, str]: + proc = subprocess.run( + [codex_bin, prompt], + capture_output=True, + text=True, + check=False, + ) + out = proc.stdout.strip() + err = proc.stderr.strip() + if out and err: + return proc.returncode, out + "\n" + err + return proc.returncode, out or err + + +def main() -> int: + ap = argparse.ArgumentParser(add_help=True) + ap.add_argument("--root", default=os.getcwd()) + ap.add_argument("--codex-bin", default=os.environ.get("CODEX_BIN", "codex")) + ap.add_argument("--pack-only", action="store_true") + ap.add_argument("--cache-status", action="store_true") + ap.add_argument("prompt", nargs=argparse.REMAINDER) + ns = ap.parse_args() + + prompt_parts = ns.prompt + if prompt_parts and prompt_parts[0] == "--": + prompt_parts = prompt_parts[1:] + prompt = " ".join(prompt_parts).strip() + root = _git_root(os.path.abspath(ns.root)) + + if ns.cache_status: + cache = _load_cache(root) + print(json.dumps({"root": root, "entries": len(cache)})) + return 0 + if ns.pack_only: + sys.stdout.write(_pack_context(root, prompt)) + return 0 + if not prompt: + print("prompt required", file=sys.stderr) + return 2 + if _should_run_local(prompt, root): + return _handle_local(prompt, root) + cached = _lookup_cached_response(root, prompt) + if cached: + print(cached) + return 0 + packed = _pack_context(root, prompt) + code, out = _invoke_codex(ns.codex_bin, packed) + if out: + print(out) + if code == 0 and out.strip(): + _store_cached_response(root, prompt, out.strip()) + return code + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/llm-call-optimizer.sh b/scripts/llm-call-optimizer.sh new file mode 100644 index 0000000..404f6ec --- /dev/null +++ b/scripts/llm-call-optimizer.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -euo pipefail + +ROOT="${CODEX_OPT_ROOT:-$PWD}" +CODEX_BIN="${CODEX_OPT_CODEX_BIN:-codex}" + +exec "${CODEX_OPT_PYTHON_BIN:-python3}" -m dag_cli.codex_broker \ + --root "$ROOT" \ + --codex-bin "$CODEX_BIN" \ + -- "$@" diff --git a/tests/test_codex_broker.py b/tests/test_codex_broker.py new file mode 100644 index 0000000..838d37f --- /dev/null +++ b/tests/test_codex_broker.py @@ -0,0 +1,23 @@ +from dag_cli import codex_broker + + +def test_local_read_short_circuit(tmp_path): + sample = tmp_path / "README.md" + sample.write_text("hello\nworld\n") + out = codex_broker._read_file_prompt("read README.md", str(tmp_path)) + assert "README.md" in out + assert "hello" in out + + +def test_cache_round_trip(tmp_path, monkeypatch): + monkeypatch.setattr(codex_broker, "_repo_fingerprint", lambda root: "fp") + root = str(tmp_path) + codex_broker._store_cached_response(root, "fix parser", "done") + assert codex_broker._lookup_cached_response(root, "fix parser") == "done" + + +def test_relevant_file_resolution_prefers_explicit_paths(tmp_path): + sample = tmp_path / "src.txt" + sample.write_text("alpha\n") + files = codex_broker._resolve_candidate_files(str(tmp_path), "check src.txt") + assert str(sample) in files diff --git a/tests/test_local_recall_provider.py b/tests/test_local_recall_provider.py new file mode 100644 index 0000000..54d07fb --- /dev/null +++ b/tests/test_local_recall_provider.py @@ -0,0 +1,59 @@ +from agent.local_recall_provider import LocalRecallProvider + + +def _fake_git_root(path: str) -> str: + return path + + +def _fake_repo_fingerprint(path: str) -> str: + return "repo-fp" + + +def test_exact_recall_prefetch_roundtrip(monkeypatch, tmp_path): + monkeypatch.setattr("agent.local_recall_provider._git_root", _fake_git_root) + monkeypatch.setattr( + "agent.local_recall_provider._repo_fingerprint", _fake_repo_fingerprint + ) + + provider = LocalRecallProvider() + provider.initialize("sess-1", dag_home=str(tmp_path), cwd=str(tmp_path)) + provider.sync_turn( + "How do we start the worker?", + "Run `dag worker start` from the repo root.", + session_id="sess-1", + messages=[ + {"role": "user", "content": "How do we start the worker?"}, + {"role": "assistant", "content": "Run `dag worker start` from the repo root."}, + ], + ) + + block = provider.prefetch("How do we start the worker?") + assert "LOCAL RECALL:" in block + assert "dag worker start" in block + + +def test_tool_turns_do_not_enter_exact_cache(monkeypatch, tmp_path): + monkeypatch.setattr("agent.local_recall_provider._git_root", _fake_git_root) + monkeypatch.setattr( + "agent.local_recall_provider._repo_fingerprint", _fake_repo_fingerprint + ) + + provider = LocalRecallProvider() + provider.initialize("sess-1", dag_home=str(tmp_path), cwd=str(tmp_path)) + provider.sync_turn( + "Fix the test", + "Patched the file and ran the test.", + session_id="sess-1", + messages=[ + {"role": "user", "content": "Fix the test"}, + { + "role": "assistant", + "content": None, + "tool_calls": [{"id": "1", "type": "function", "function": {"name": "exec"}}], + }, + {"role": "tool", "tool_call_id": "1", "content": "ok"}, + {"role": "assistant", "content": "Patched the file and ran the test."}, + ], + ) + + assert provider.prefetch("Fix the test") == "" From 9b444ee30b851ed5ac4aec8a25680d44a74bd921 Mon Sep 17 00:00:00 2001 From: logohere <69400476+logohere@users.noreply.github.com> Date: Wed, 11 Nov 2026 18:15:36 -0500 Subject: [PATCH 2/2] style: remove trailing blank line --- agent/local_recall_provider.py | 1 - 1 file changed, 1 deletion(-) diff --git a/agent/local_recall_provider.py b/agent/local_recall_provider.py index a3a0850..674a14c 100644 --- a/agent/local_recall_provider.py +++ b/agent/local_recall_provider.py @@ -267,4 +267,3 @@ def _turn_used_tools(messages: List[Dict[str, Any]]) -> bool: if msg.get("tool_calls"): return True return False -