3 Commits

Author SHA1 Message Date
CoreRasurae a8f68b8262 fix(graph): enforce USD budget at each agent invocation with pre-flight gate
CI / lint (pull_request) Successful in 50s
CI / typecheck (pull_request) Successful in 55s
CI / security (pull_request) Successful in 53s
CI / quality (pull_request) Successful in 38s
CI / unit_tests (pull_request) Successful in 3m15s
CI / integration_tests (pull_request) Successful in 1m14s
CI / build (pull_request) Successful in 39s
CI / coverage (pull_request) Successful in 3m16s
CI / status-check (pull_request) Successful in 3s
CI / lint (push) Successful in 37s
CI / typecheck (push) Successful in 56s
CI / security (push) Successful in 54s
CI / quality (push) Successful in 37s
CI / unit_tests (push) Successful in 3m15s
CI / integration_tests (push) Successful in 1m14s
CI / build (push) Successful in 39s
CI / coverage (push) Successful in 3m16s
CI / status-check (push) Successful in 3s
Enforces the USD budget (max_cost_usd) as a hard pre-flight gate before
every node execution and at each main-agent ainvoke round inside the
multi-turn tool loop, with pruning passes silently skipped when the
budget is exhausted.

Pre-flight gate (pure_graph.py):
  _check_budget_pre_flight() raises ExecutionError(kind='cost',
  reason='budget_exhausted') immediately before node execution in both
  _execute_from_node and all three branches of _stream_from_node.

In-node budget gating (llm.py):
  _check_budget_before_invoke() gates every _retry_ainvoke() call
  inside _execute_tool_loop, process_message, and stream_message.
  _should_skip_pruning() skips pruning passes when budget exhausted.
  _accumulate_cost_after_invoke() computes USD cost from token counts
  using the pricing table and advances current_accumulated_cost so
  subsequent budget checks see the updated cost.

ContextVar plumbing (retry.py, pure_graph.py):
  current_accumulated_cost, current_max_cost_usd, and current_pricing
  ContextVars carry per-node budget state from PureLangGraph into
  LLMAgent. Set before node.execute() via _set_budget_context_for_node,
  reset in finally blocks via _reset_budget_context.

Reviewer fixes (rui.hu #80):
  - Move cost accumulation inside try block in terminal streaming branch
    to prevent ContextVar leak (was set after finally reset).
  - Standardize log precision to $%.6f across all budget messages.
  - Remove orphaned 'already exhausted' step (criterion unreachable).
  - Add dedicated streaming budget enforcement scenario.
  - Remove redundant = None default on _reset_budget_context pricing_token.

ISSUES CLOSED: #76
2026-07-12 19:51:39 +01:00
CoreRasurae 227e2feece feat(agents): implement multi-turn tool-call loop and sandbox improvements
CI / build (pull_request) Successful in 45s
CI / quality (pull_request) Successful in 58s
CI / lint (pull_request) Successful in 1m15s
CI / typecheck (pull_request) Successful in 1m15s
CI / security (pull_request) Successful in 1m15s
CI / integration_tests (pull_request) Successful in 1m24s
CI / unit_tests (pull_request) Successful in 3m25s
CI / coverage (pull_request) Successful in 3m10s
CI / status-check (pull_request) Successful in 6s
CI / lint (push) Successful in 42s
CI / quality (push) Successful in 48s
CI / typecheck (push) Successful in 51s
CI / security (push) Successful in 49s
CI / build (push) Successful in 47s
CI / integration_tests (push) Successful in 1m10s
CI / unit_tests (push) Successful in 3m9s
CI / coverage (push) Successful in 3m7s
CI / status-check (push) Successful in 3s
Implements the complete LLM agent tool-calling pipeline — the tool-calling
path was broken in multiple ways: tools from agent config were not passed
to the LLM, tool execution was single-pass (the model could not see tool
results or make follow-up calls), tool errors were discarded, and shell/
python_exec tools were never registered.

Key changes:

- Multi-turn tool-call loop: passes tools to the LLM via ainvoke(tools=...)
  and re-invokes after each batch of ToolResults, with a configurable
  round limit (tool_max_rounds config / TOOL_MAX_ROUNDS env var, default 20).

- Tool schema module (llm_tools.py): normalize_tool_entry() converts
  string tool names and config dicts to OpenAI function-calling format
  with full parameter schemas and LLM-facing guidance (max_chars for
  file_read, sandbox builtins for python_exec, etc.).

- file_read enhancements: directory listing with file sizes and type
  indicators, max_chars truncation to prevent context overflow, shell
  command detection with redirect to shell tool, file-not-found
  recovery hints with parent directory listing.

- python_exec sandbox: stdout capture so print() produces visible
  output, NameError guidance directing LLM to file_read/file_write
  instead of using open().

- shell/python_exec tool registration in builtin_tools when
  allow_shell / exec_python is enabled.

- create_subprocess_shell for shell commands (was create_subprocess_exec,
  which blocks pipes/redirections/chains).

- Tool error propagation: ExecutionError/ConfigurationError details
  included in ToolMessages for LLM self-correction.

- Stuck-model recovery: injects synthesizing prompt when model
  exhausts tool rounds without producing content.

- LLM provider error details preserved in ExecutionError messages
  instead of generic "LLM processing failed".

- Empty message filtering in _prepare_conversation_history() prevents
  whitespace-only messages from polluting downstream conversation
  history in multi-agent pipelines.

Closes #59
2026-06-23 12:39:20 +01:00
hurui200320 d93c32ccee feat(streaming): add Executor.execute_stream() returning AsyncIterator[str] for token-by-token delivery
CI / lint (pull_request) Successful in 43s
CI / quality (pull_request) Successful in 49s
CI / build (pull_request) Successful in 47s
CI / typecheck (pull_request) Successful in 1m9s
CI / security (pull_request) Successful in 1m9s
CI / integration_tests (pull_request) Successful in 1m8s
CI / unit_tests (pull_request) Successful in 3m9s
CI / coverage (pull_request) Successful in 3m7s
CI / status-check (pull_request) Successful in 3s
CI / lint (push) Successful in 55s
CI / typecheck (push) Successful in 56s
CI / security (push) Successful in 57s
CI / quality (push) Successful in 48s
CI / build (push) Successful in 48s
CI / integration_tests (push) Successful in 1m10s
CI / unit_tests (push) Successful in 3m26s
CI / coverage (push) Failing after 13m5s
CI / status-check (push) Has been cancelled
Implements the full streaming execution path for the CleverThis router:

- **LLMAgent.stream_message(message, context)** (agents/llm.py): Async generator
  calling self.chat_model.astream(messages) and yielding str(chunk.content) per
  chunk. Captures token counts from the final chunk's usage_metadata using the
  same _safe_int() fallback chain as process_message(). Sets _last_token_usage
  and last_token_usage_var after exhaustion.

- **Node.stream_agent(state)** (langgraph/nodes.py): Async generator that mirrors
  _execute_agent() but uses agent.stream_message() for LLMAgent instances and
  falls back to process_message() for non-LLM agents. Captures per-node token
  usage into self._last_stream_usage after stream exhaustion.

- **PureLangGraph.execute_stream()** (langgraph/pure_graph.py): Async generator
  mirroring execute(). Calls _stream_from_node() which uses astream() for all
  AGENT nodes (buffering tokens for intermediate nodes, yielding only from the
  terminal node) and ainvoke() for non-AGENT nodes. Stores final state and node
  usages in _last_stream_state / _last_stream_node_usages for post-stream access.
  Same limit enforcement (timeout_ms, max_depth, max_model_calls, max_tool_calls)
  as _execute_from_node().

- **_execute_llm_stream() / _execute_graph_stream()** (runtime_dispatch.py):
  Async generator dispatch functions mirroring _execute_llm() / _execute_graph().
  Set executor.last_result after stream exhaustion using collected token usage.

- **Executor.execute_stream(message)** (runtime.py): Public async generator that
  dispatches to _execute_llm_stream() for llm actors and _execute_graph_stream()
  for graph actors. Raises ConfigurationError for tool/multi_actor types.
  Resets last_result to None at entry; callers must exhaust the iterator.

- **Executor.last_result** (runtime.py): ActorResult | None attribute set by
  execute_stream() after the iterator is exhausted.

- Intermediate AGENT nodes use astream() internally (tokens buffered, not yielded
  to caller); terminal AGENT nodes yield buffered tokens. This avoids double-
  running the terminal LLM while satisfying AC2.
- Non-AGENT (FUNCTION/TOOL) nodes always use ainvoke() via node.execute().
- timeout_ms in execute_stream() buffers all tokens via asyncio.wait_for() around
  _collect_stream_tokens(), then yields them — this is simpler than wrapping an
  async generator directly.
- Tool and multi_actor actor types raise ConfigurationError (no streaming path).

- 60 new BDD scenarios in features/execute_stream.feature covering all acceptance
  criteria: token delivery, last_result population, token count extraction,
  limit enforcement (timeout, model_calls, tool_calls), error paths, graph
  streaming, non-AGENT node paths, parallel execution paths, and edge cases.
- All 2395 existing unit tests continue to pass.
- Coverage: 96.5% (meets threshold).

ISSUES CLOSED: #16
2026-06-12 10:08:12 +00:00