Enable implicit phase-memory activation on every warden command.

Load coordination memory by default via ensure_memory_context on app bootstrap
and route/access flows; invalidate cache after episode writes. WARDEN_MEMORY=0
remains the opt-out. Document that warden memory activate is optional only.
This commit is contained in:
2026-07-03 00:49:36 +02:00
parent 04929e7981
commit 120de64bcb
6 changed files with 129 additions and 13 deletions

View File

@@ -10,6 +10,17 @@ _PHASE_MEMORY_ERROR = (
"Install with: pip install phase-memory (or set PYTHONPATH to phase-memory/src)."
)
# In-process cache: implicit activation is default; no separate `warden memory activate`
# is required for normal route/access/worker/sign use within one CLI invocation tree.
_CONTEXT_CACHE: dict[str, Any] | None = None
_CONTEXT_CACHE_KEY: tuple[str, str] = ("", "")
def _invalidate_context_cache() -> None:
global _CONTEXT_CACHE, _CONTEXT_CACHE_KEY
_CONTEXT_CACHE = None
_CONTEXT_CACHE_KEY = ("", "")
def _phase_memory():
try:
@@ -46,24 +57,76 @@ def status(environ: Mapping[str, str] | None = None) -> dict[str, Any]:
return pm.OpsWardenMemoryStore.open(environ=environ).status()
def memory_context_summary(activation: dict[str, Any] | None) -> dict[str, Any]:
if not activation:
return {"enabled": False}
return {
"enabled": True,
"implicit": bool(activation.get("implicit")),
"session_kind": activation.get("session_kind", ""),
"episode_count": activation.get("episode_count", 0),
"stabilized_route_id": (activation.get("stabilized_route") or {}).get("route_id", ""),
"llm_calls_avoided": bool(activation.get("llm_calls_avoided")),
"selected_episode_count": len(activation.get("selected_episodes") or ()),
}
def ensure_memory_context(
need: str = "",
*,
agent: Optional[str] = None,
session_id: str = "",
environ: Mapping[str, str] | None = None,
implicit: bool = True,
) -> dict[str, Any] | None:
"""Load coordination memory for the current session (default, no extra command)."""
global _CONTEXT_CACHE, _CONTEXT_CACHE_KEY
if not enabled(environ):
return None
if not memory_available():
return None
pm = _phase_memory()
env = dict(environ or os.environ)
if agent:
env["WARDEN_AGENT_ID"] = agent
kind = pm.resolve_session_kind(env)
fingerprint = pm.need_fingerprint(need) if need else ""
cache_key = (kind, fingerprint)
if _CONTEXT_CACHE is not None and _CONTEXT_CACHE_KEY == cache_key:
return _CONTEXT_CACHE
try:
activation = activate(need=need, agent=agent, session_id=session_id, environ=env)
except RuntimeError:
return None
activation = {**activation, "implicit": implicit}
_CONTEXT_CACHE = activation
_CONTEXT_CACHE_KEY = cache_key
return activation
def activate(
*,
need: str = "",
agent: Optional[str] = None,
session_id: str = "",
environ: Mapping[str, str] | None = None,
implicit: bool = False,
) -> dict[str, Any]:
pm = _phase_memory()
env = dict(environ or os.environ)
if agent:
env["WARDEN_AGENT_ID"] = agent
kind = pm.resolve_session_kind(env)
return pm.activate_ops_warden_memory(
activation = pm.activate_ops_warden_memory(
pm.OpsWardenMemoryStore.open(environ=env),
session_kind=kind,
need=need,
session_id=session_id,
)
if implicit:
activation = {**activation, "implicit": True}
return activation
def record_command_episode(
@@ -91,13 +154,16 @@ def record_command_episode(
diagnostic_codes=diagnostic_codes,
metadata=metadata,
)
return pm.record_session_event(pm.OpsWardenMemoryStore.open(environ=env), event)
result = pm.record_session_event(pm.OpsWardenMemoryStore.open(environ=env), event)
if result.get("valid"):
_invalidate_context_cache()
return result
def worker_activation_context(need: str = "", environ: Mapping[str, str] | None = None) -> dict[str, Any]:
env = dict(environ or os.environ)
env["WARDEN_SESSION_KIND"] = "warden.worker"
return activate(need=need, environ=env)
return ensure_memory_context(need=need, environ=env, implicit=True) or activate(need=need, environ=env, implicit=True)
def stabilized_route_for_need(need: str, environ: Mapping[str, str] | None = None) -> Optional[dict[str, Any]]: