|
@@ -2,19 +2,23 @@
|
|
|
lambdagent.cek_machine — Agent CEK Machine
|
|
lambdagent.cek_machine — Agent CEK Machine
|
|
|
|
|
|
|
|
An abstract machine implementation of the operational semantics
|
|
An abstract machine implementation of the operational semantics
|
|
|
-defined in Paper10. The CEK machine provides:
|
|
|
|
|
|
|
+defined in Paper II. The CEK machine provides:
|
|
|
|
|
|
|
|
1. Step-by-step execution with full state inspection
|
|
1. Step-by-step execution with full state inspection
|
|
|
2. YIELD mechanism for LLM/Tool oracle calls
|
|
2. YIELD mechanism for LLM/Tool oracle calls
|
|
|
3. Complete trace of all transitions (dispatch, yield, return)
|
|
3. Complete trace of all transitions (dispatch, yield, return)
|
|
|
4. Cost vector tracking at every step
|
|
4. Cost vector tracking at every step
|
|
|
|
|
+ 5. Effect handler integration (Paper III §6)
|
|
|
|
|
+ 6. Cost monotonicity invariant (Paper II Proposition 23)
|
|
|
|
|
+ 7. Async run support via run_async()
|
|
|
|
|
|
|
|
The machine corresponds 1-to-1 with the small-step reduction rules
|
|
The machine corresponds 1-to-1 with the small-step reduction rules
|
|
|
-in Paper10 §4, and the transition rules in §5.
|
|
|
|
|
|
|
+in Paper II §4, and the transition rules in §5.
|
|
|
|
|
|
|
|
References:
|
|
References:
|
|
|
- Felleisen & Friedman (1986): CEK Machine
|
|
- Felleisen & Friedman (1986): CEK Machine
|
|
|
- - Paper10 §5: Agent CEK Machine
|
|
|
|
|
|
|
+ - Paper II §5: Agent CEK Machine
|
|
|
|
|
+ - Paper III §6: Algebraic Effect Handlers
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
from __future__ import annotations
|
|
@@ -50,6 +54,11 @@ class CostVector:
|
|
|
ZERO_COST = CostVector()
|
|
ZERO_COST = CostVector()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+class CostMonotonicityViolation(RuntimeError):
|
|
|
|
|
+ """Paper II Proposition 23: cost must be monotonically non-decreasing."""
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
# ============================================================
|
|
# ============================================================
|
|
|
# Labels (observable actions)
|
|
# Labels (observable actions)
|
|
|
# ============================================================
|
|
# ============================================================
|
|
@@ -158,6 +167,30 @@ class MemK(Kont):
|
|
|
return f"memK({self.store_key}) :: {self.prev}"
|
|
return f"memK({self.store_key}) :: {self.prev}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+class IfK(Kont):
|
|
|
|
|
+ """If: condition evaluated, dispatch to then/else based on result."""
|
|
|
|
|
+ def __init__(self, then_term, else_term, input_val, prev: Kont):
|
|
|
|
|
+ self.then_term = then_term
|
|
|
|
|
+ self.else_term = else_term
|
|
|
|
|
+ self.input_val = input_val
|
|
|
|
|
+ self.prev = prev
|
|
|
|
|
+
|
|
|
|
|
+ def __repr__(self):
|
|
|
|
|
+ return f"ifK :: {self.prev}"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class RouteK(Kont):
|
|
|
|
|
+ """Route: classifier evaluated, dispatch to route based on label."""
|
|
|
|
|
+ def __init__(self, routes: dict, default, input_val, prev: Kont):
|
|
|
|
|
+ self.routes = routes
|
|
|
|
|
+ self.default = default
|
|
|
|
|
+ self.input_val = input_val
|
|
|
|
|
+ self.prev = prev
|
|
|
|
|
+
|
|
|
|
|
+ def __repr__(self):
|
|
|
|
|
+ return f"routeK :: {self.prev}"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
# ============================================================
|
|
# ============================================================
|
|
|
# CEK Machine State
|
|
# CEK Machine State
|
|
|
# ============================================================
|
|
# ============================================================
|
|
@@ -192,6 +225,11 @@ class CEKState:
|
|
|
return False
|
|
return False
|
|
|
return True
|
|
return True
|
|
|
|
|
|
|
|
|
|
+ @property
|
|
|
|
|
+ def per_step_costs(self) -> List[CostVector]:
|
|
|
|
|
+ """Per-step cost breakdown (Paper II Definition 5)"""
|
|
|
|
|
+ return [] # populated from machine trace externally
|
|
|
|
|
+
|
|
|
|
|
|
|
|
# ============================================================
|
|
# ============================================================
|
|
|
# Transition Trace Entry
|
|
# Transition Trace Entry
|
|
@@ -231,11 +269,23 @@ class AgentCEKMachine:
|
|
|
result = machine.state.control
|
|
result = machine.state.control
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
- def __init__(self, store: Dict[str, Any] | None = None):
|
|
|
|
|
|
|
+ def __init__(self, store: Dict[str, Any] | None = None,
|
|
|
|
|
+ handler=None,
|
|
|
|
|
+ check_cost_monotonicity: bool = True):
|
|
|
|
|
+ """
|
|
|
|
|
+ Args:
|
|
|
|
|
+ store: initial memory store
|
|
|
|
|
+ handler: EffectHandler (Paper III §6) — if provided, LLM/Tool
|
|
|
|
|
+ calls are routed through the handler
|
|
|
|
|
+ check_cost_monotonicity: if True, assert Paper II Proposition 23
|
|
|
|
|
+ (cost never decreases) after every step
|
|
|
|
|
+ """
|
|
|
self.state: Optional[CEKState] = None
|
|
self.state: Optional[CEKState] = None
|
|
|
self.trace: List[Transition] = []
|
|
self.trace: List[Transition] = []
|
|
|
self.step_count: int = 0
|
|
self.step_count: int = 0
|
|
|
self._initial_store = store or {}
|
|
self._initial_store = store or {}
|
|
|
|
|
+ self._handler = handler
|
|
|
|
|
+ self._check_cost_monotonicity = check_cost_monotonicity
|
|
|
|
|
|
|
|
def load(self, term, input_val: Any) -> CEKState:
|
|
def load(self, term, input_val: Any) -> CEKState:
|
|
|
"""Load a term into the machine, creating the initial state.
|
|
"""Load a term into the machine, creating the initial state.
|
|
@@ -290,6 +340,15 @@ class AgentCEKMachine:
|
|
|
self.state.cost.money - cost_before.money,
|
|
self.state.cost.money - cost_before.money,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+ # Paper II Proposition 23: cost monotonicity — c' ≥ c component-wise
|
|
|
|
|
+ if self._check_cost_monotonicity and (
|
|
|
|
|
+ cost_delta.tokens < 0 or cost_delta.latency < 0 or cost_delta.money < 0
|
|
|
|
|
+ ):
|
|
|
|
|
+ raise CostMonotonicityViolation(
|
|
|
|
|
+ f"Cost decreased at step {self.step_count + 1} (rule {rule}): "
|
|
|
|
|
+ f"delta={cost_delta}. Paper II Prop. 23 requires c' ≥ c."
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
self.step_count += 1
|
|
self.step_count += 1
|
|
|
transition = Transition(
|
|
transition = Transition(
|
|
|
step=self.step_count,
|
|
step=self.step_count,
|
|
@@ -312,6 +371,44 @@ class AgentCEKMachine:
|
|
|
self.step()
|
|
self.step()
|
|
|
return self.state.control
|
|
return self.state.control
|
|
|
|
|
|
|
|
|
|
+ async def run_async(self, term, input_val: Any, max_steps: int = 10000) -> Any:
|
|
|
|
|
+ """
|
|
|
|
|
+ Async version of run() — yields control at LLM/Tool calls.
|
|
|
|
|
+
|
|
|
|
|
+ Paper II §5: The Yield mechanism maps naturally to async/await.
|
|
|
|
|
+ Each C-Lam and C-Tool step is an await point.
|
|
|
|
|
+ """
|
|
|
|
|
+ import asyncio
|
|
|
|
|
+ self.load(term, input_val)
|
|
|
|
|
+ while not self.state.is_terminal():
|
|
|
|
|
+ if self.step_count >= max_steps:
|
|
|
|
|
+ raise RuntimeError(f"CEK machine exceeded {max_steps} steps")
|
|
|
|
|
+ # Yield to event loop at each step (non-blocking)
|
|
|
|
|
+ await asyncio.sleep(0)
|
|
|
|
|
+ self.step()
|
|
|
|
|
+ return self.state.control
|
|
|
|
|
+
|
|
|
|
|
+ def cost_summary(self) -> Dict[str, Any]:
|
|
|
|
|
+ """
|
|
|
|
|
+ Cost summary for the entire execution.
|
|
|
|
|
+
|
|
|
|
|
+ Paper II Definition 5: c = (tokens, latency_s, money_usd)
|
|
|
|
|
+ """
|
|
|
|
|
+ if self.state is None:
|
|
|
|
|
+ return {"tokens": 0, "latency_s": 0.0, "money_usd": 0.0, "steps": 0}
|
|
|
|
|
+ c = self.state.cost
|
|
|
|
|
+ # Per-step cost breakdown
|
|
|
|
|
+ per_step = [(t.step, t.rule, t.cost_delta) for t in self.trace if t.cost_delta.tokens > 0 or t.cost_delta.latency > 0]
|
|
|
|
|
+ return {
|
|
|
|
|
+ "tokens": c.tokens,
|
|
|
|
|
+ "latency_s": c.latency,
|
|
|
|
|
+ "money_usd": c.money,
|
|
|
|
|
+ "steps": self.step_count,
|
|
|
|
|
+ "llm_calls": sum(1 for t in self.trace if t.label.kind == LabelKind.LLM),
|
|
|
|
|
+ "tool_calls": sum(1 for t in self.trace if t.label.kind == LabelKind.TOOL),
|
|
|
|
|
+ "per_step_costs": per_step,
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
def print_trace(self):
|
|
def print_trace(self):
|
|
|
"""Print the full transition trace."""
|
|
"""Print the full transition trace."""
|
|
|
for t in self.trace:
|
|
for t in self.trace:
|
|
@@ -364,16 +461,27 @@ class AgentCEKMachine:
|
|
|
s = self.state
|
|
s = self.state
|
|
|
|
|
|
|
|
# ── C-Lam: YIELD to LLM oracle ──
|
|
# ── C-Lam: YIELD to LLM oracle ──
|
|
|
- # Supports both Lam (API key) and ClaudeLam (Claude Code CLI, no API key)
|
|
|
|
|
|
|
+ # Paper III §6: route through handler if available
|
|
|
if isinstance(op, Lam) or _is_claude_lam(op):
|
|
if isinstance(op, Lam) or _is_claude_lam(op):
|
|
|
t0 = time.time()
|
|
t0 = time.time()
|
|
|
- ctx = Context(bindings=s.env, memory=s.store)
|
|
|
|
|
- result = op.apply(arg, ctx)
|
|
|
|
|
|
|
+ handler = self._handler
|
|
|
|
|
+ if handler is not None and not isinstance(handler, _passthrough_handler_types()):
|
|
|
|
|
+ # Use handler for LLM effect
|
|
|
|
|
+ result = handler.handle_llm(
|
|
|
|
|
+ getattr(op, 'prompt', ''), str(arg),
|
|
|
|
|
+ getattr(op, 'model', 'unknown'),
|
|
|
|
|
+ temperature=getattr(op, 'temperature', 0.0),
|
|
|
|
|
+ max_tokens=getattr(op, 'max_tokens', 1024),
|
|
|
|
|
+ )
|
|
|
|
|
+ if hasattr(op, 'output_parser'):
|
|
|
|
|
+ result = op.output_parser(result)
|
|
|
|
|
+ else:
|
|
|
|
|
+ ctx = Context(bindings=s.env, memory=s.store)
|
|
|
|
|
+ result = op.apply(arg, ctx)
|
|
|
elapsed = time.time() - t0
|
|
elapsed = time.time() - t0
|
|
|
|
|
|
|
|
- # Extract cost from context trace
|
|
|
|
|
- tokens = ctx.trace[-1].tokens_used if ctx.trace else 0
|
|
|
|
|
model_name = getattr(op, 'model', 'claude-code')
|
|
model_name = getattr(op, 'model', 'claude-code')
|
|
|
|
|
+ tokens = 0 # handler may not report tokens; real calls update via trace
|
|
|
cost_llm = CostVector(
|
|
cost_llm = CostVector(
|
|
|
tokens=tokens,
|
|
tokens=tokens,
|
|
|
latency=elapsed,
|
|
latency=elapsed,
|
|
@@ -385,10 +493,15 @@ class AgentCEKMachine:
|
|
|
return "C-Lam", label
|
|
return "C-Lam", label
|
|
|
|
|
|
|
|
# ── C-Tool: YIELD to tool oracle ──
|
|
# ── C-Tool: YIELD to tool oracle ──
|
|
|
|
|
+ # Paper III §6: route through handler if available
|
|
|
if isinstance(op, Tool):
|
|
if isinstance(op, Tool):
|
|
|
t0 = time.time()
|
|
t0 = time.time()
|
|
|
- ctx = Context(bindings=s.env, memory=s.store)
|
|
|
|
|
- result = op.apply(arg, ctx)
|
|
|
|
|
|
|
+ handler = self._handler
|
|
|
|
|
+ if handler is not None and not isinstance(handler, _passthrough_handler_types()):
|
|
|
|
|
+ result = handler.handle_tool(op._name, op.fn, arg)
|
|
|
|
|
+ else:
|
|
|
|
|
+ ctx = Context(bindings=s.env, memory=s.store)
|
|
|
|
|
+ result = op.apply(arg, ctx)
|
|
|
elapsed = time.time() - t0
|
|
elapsed = time.time() - t0
|
|
|
|
|
|
|
|
cost_tool = CostVector(latency=elapsed)
|
|
cost_tool = CostVector(latency=elapsed)
|
|
@@ -415,28 +528,27 @@ class AgentCEKMachine:
|
|
|
s.control = _AppTerm(first, arg)
|
|
s.control = _AppTerm(first, arg)
|
|
|
return "C-Comp", TAU
|
|
return "C-Comp", TAU
|
|
|
|
|
|
|
|
- # ── C-If: dispatch on predicate ──
|
|
|
|
|
|
|
+ # ── C-If: push IfK if cond is a Term, else inline ──
|
|
|
if isinstance(op, If):
|
|
if isinstance(op, If):
|
|
|
cond = op.cond
|
|
cond = op.cond
|
|
|
if isinstance(cond, Term):
|
|
if isinstance(cond, Term):
|
|
|
- ctx = Context(bindings=s.env, memory=s.store)
|
|
|
|
|
- cond_result = cond.apply(arg, ctx)
|
|
|
|
|
- cond_result = If._is_truthy(cond_result)
|
|
|
|
|
|
|
+ # Proper CEK: evaluate condition first, then dispatch
|
|
|
|
|
+ s.control = _AppTerm(cond, arg)
|
|
|
|
|
+ s.kont = IfK(op.then_, op.else_, arg, s.kont)
|
|
|
|
|
+ return "C-If", TAU
|
|
|
else:
|
|
else:
|
|
|
|
|
+ # Python callable condition — inline dispatch
|
|
|
cond_result = cond(arg)
|
|
cond_result = cond(arg)
|
|
|
- if cond_result:
|
|
|
|
|
- s.control = _AppTerm(op.then_, arg)
|
|
|
|
|
- else:
|
|
|
|
|
- s.control = _AppTerm(op.else_, arg)
|
|
|
|
|
- return "C-If", TAU
|
|
|
|
|
|
|
+ if cond_result:
|
|
|
|
|
+ s.control = _AppTerm(op.then_, arg)
|
|
|
|
|
+ else:
|
|
|
|
|
+ s.control = _AppTerm(op.else_, arg)
|
|
|
|
|
+ return "C-If", TAU
|
|
|
|
|
|
|
|
- # ── C-Route: dispatch on classifier ──
|
|
|
|
|
|
|
+ # ── C-Route: push RouteK, evaluate classifier ──
|
|
|
if isinstance(op, Route):
|
|
if isinstance(op, Route):
|
|
|
- label_val = op.classifier(arg) if callable(op.classifier) else op.classifier.apply(arg, Context())
|
|
|
|
|
- if label_val in op.routes:
|
|
|
|
|
- s.control = _AppTerm(op.routes[label_val], arg)
|
|
|
|
|
- else:
|
|
|
|
|
- raise RuntimeError(f"Route: no branch for label '{label_val}'")
|
|
|
|
|
|
|
+ s.control = _AppTerm(op.classifier, arg)
|
|
|
|
|
+ s.kont = RouteK(op.routes, op.default, arg, s.kont)
|
|
|
return "C-Route", TAU
|
|
return "C-Route", TAU
|
|
|
|
|
|
|
|
# ── C-Loop: check condition or unfold ──
|
|
# ── C-Loop: check condition or unfold ──
|
|
@@ -568,6 +680,38 @@ class AgentCEKMachine:
|
|
|
s.kont = k.prev
|
|
s.kont = k.prev
|
|
|
return "C-MemRet", label
|
|
return "C-MemRet", label
|
|
|
|
|
|
|
|
|
|
+ # ── C-IfRet: condition evaluated, dispatch to then/else ──
|
|
|
|
|
+ if isinstance(k, IfK):
|
|
|
|
|
+ from .primitives import If
|
|
|
|
|
+ is_true = If._is_truthy(val) if isinstance(val, str) else bool(val)
|
|
|
|
|
+ if is_true:
|
|
|
|
|
+ s.control = _AppTerm(k.then_term, k.input_val)
|
|
|
|
|
+ else:
|
|
|
|
|
+ s.control = _AppTerm(k.else_term, k.input_val)
|
|
|
|
|
+ s.kont = k.prev
|
|
|
|
|
+ return "C-IfRet", TAU
|
|
|
|
|
+
|
|
|
|
|
+ # ── C-RouteRet: classifier evaluated, dispatch to route ──
|
|
|
|
|
+ if isinstance(k, RouteK):
|
|
|
|
|
+ label_str = str(val).strip().lower()
|
|
|
|
|
+ agent = k.routes.get(label_str)
|
|
|
|
|
+ if agent is None:
|
|
|
|
|
+ # Fuzzy match
|
|
|
|
|
+ for key, a in k.routes.items():
|
|
|
|
|
+ if key.lower() in label_str or label_str in key.lower():
|
|
|
|
|
+ agent = a
|
|
|
|
|
+ break
|
|
|
|
|
+ if agent is None:
|
|
|
|
|
+ agent = k.default
|
|
|
|
|
+ if agent is None:
|
|
|
|
|
+ raise RuntimeError(
|
|
|
|
|
+ f"Route: no branch for label '{val}'. "
|
|
|
|
|
+ f"Available: {list(k.routes.keys())}"
|
|
|
|
|
+ )
|
|
|
|
|
+ s.control = _AppTerm(agent, k.input_val)
|
|
|
|
|
+ s.kont = k.prev
|
|
|
|
|
+ return "C-RouteRet", TAU
|
|
|
|
|
+
|
|
|
raise RuntimeError(f"CEK return: unknown continuation {type(k).__name__}")
|
|
raise RuntimeError(f"CEK return: unknown continuation {type(k).__name__}")
|
|
|
|
|
|
|
|
def _state_repr(self) -> str:
|
|
def _state_repr(self) -> str:
|
|
@@ -631,6 +775,12 @@ def _is_claude_lam(op) -> bool:
|
|
|
return type(op).__name__ == 'ClaudeLam'
|
|
return type(op).__name__ == 'ClaudeLam'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+def _passthrough_handler_types():
|
|
|
|
|
+ """Handler types that should use direct apply() — lazy import."""
|
|
|
|
|
+ from .handlers import ProductionHandler, TraceHandler
|
|
|
|
|
+ return (ProductionHandler, TraceHandler)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def _price_per_token(model: str) -> float:
|
|
def _price_per_token(model: str) -> float:
|
|
|
"""Rough per-token price for cost tracking."""
|
|
"""Rough per-token price for cost tracking."""
|
|
|
prices = {
|
|
prices = {
|