Repository navigation
feat: local agent ReAct loop with history management and context budgeting - #1030
Conversation
Code Review: PR #1030 -- Local Agent ReAct LoopReviewer: Principal Engineer (Pragmatic Quality Framework) Overall AssessmentThis is a well-structured, net-positive change. The ReAct loop implementation is sound, the trait-generic design is clean, and the 22-test suite covers the critical paths. The separation into Acceptance criteria coverage against issue #1006:
All acceptance criteria are met. FindingsSuggested Improvements1. [Improvement] The chunk collection callback uses if let Ok(mut guard) = collected_for_cb.try_lock() {
guard.push(chunk);
};If the mutex is contended at the moment a chunk arrives, that chunk is silently discarded. While unlikely in practice (the callback runs sequentially within Suggestion: Since the 2. [Improvement] Session removal in The Suggestion: Either use a let result = {
let _guard = scopeguard::guard((), |_| {
// Ensure session goes back even on panic
});
self.agent_loop.run_turn(...)
};Or document this as a known limitation (acceptable for a pre-release codebase since panics in async code are typically caught by the Tokio runtime). 3. [Improvement] Tool calls are accumulated in a Suggestion: Use 4. [Improvement] System prompt is rebuilt every iteration (agent_loop.rs, lines 96-101, 120-128)
Suggestion: For now this is fine given the 8k token budget keeps things small. If performance becomes a concern, the system message could be prepended in-place rather than rebuilt each iteration. 5. [Improvement] Summarization always sends a The summarization prompt is sent as a 6. [Improvement] Missing test: session persistence after error (agent_loop.rs) The Nitpicks1. Nit: Unreachable code after the loop (agent_loop.rs, lines 268-277) // Should not reach here, but just in case
on_status(LocalAgentStatus::Idle);This code after the 2. Nit: let tools = self.tool_executor.available_tools().await.unwrap_or_default();If tool listing fails, the agent proceeds with no tools. This may be intentional (degrade gracefully) but should be documented with a comment or logged. 3. Nit: The summarization function checks both 4. Nit:
SummaryThis is solid work. The architecture is clean, the trait-generic design enables excellent testability, and the test coverage is comprehensive for the critical paths. The suggested improvements above are non-blocking -- the most impactful one is the Status: Approve (non-blocking suggestions) Reviewed with Pragmatic Quality Framework -- Principal Engineer Reviewer |
malibio
left a comment
There was a problem hiding this comment.
Reviewed: Approve with non-blocking suggestions. See detailed review comment above. The most impactful recommendation is switching parse_chunks from HashMap to an ordered collection to preserve tool call ordering in multi-tool turns.
Switching from HashMap to Vec ensures tool calls with causal dependencies execute in the order the model emitted them, not random HashMap iteration order. Addresses reviewer recommendation from PR #1030 review. Co-Authored-By: Claude <[email protected]>
Review Feedback Addressed✅ Fixed tool call ordering: replaced HashMap with Vec to preserve emission order (causal dependencies) |
…eting (closes #1006) Implements the core ReAct (Reason + Act) loop driving the local agent: - LocalAgentLoop: orchestrates inference + tool-call cycles (max 5 iterations), parses streaming chunks into text and tool calls, executes tools via AgentToolExecutor, and feeds results back for next inference round - History management: 8k token budget (2k system + 6k history), automatic summarization of older turns when budget exceeded, keeping recent 3 turns - LocalAgentService: session management facade with create/send/cancel/end/list, per-session CancellationToken support - Status transitions: Idle -> Thinking -> ToolExecution -> Streaming -> Idle - Prompt templates: system prompt, tool definition formatter, summarization prompt - 22 unit tests with mock ChatInferenceEngine and mock AgentToolExecutor covering single-turn, multi-step tool chains, max iteration limit, cancellation, history summarization, and status transitions Also fixes pre-existing issues: - Resolve Cargo.toml merge conflict between #1001 and #1000 - Remove duplicate `pub mod local_agent` in lib.rs - Add PartialEq derive on LocalAgentStatus for test assertions - Add Debug impl for StdioTransport, tempfile dev-dependency - Fix temporary lifetime in context_assembly test - Apply cargo fmt to pre-existing formatting issues Co-Authored-By: Claude <[email protected]>
Switching from HashMap to Vec ensures tool calls with causal dependencies execute in the order the model emitted them, not random HashMap iteration order. Addresses reviewer recommendation from PR #1030 review. Co-Authored-By: Claude <[email protected]>
f0a2506 to
57662fe
Compare
…eting (#1030) * feat: local agent ReAct loop with history management and context budgeting (closes #1006) Implements the core ReAct (Reason + Act) loop driving the local agent: - LocalAgentLoop: orchestrates inference + tool-call cycles (max 5 iterations), parses streaming chunks into text and tool calls, executes tools via AgentToolExecutor, and feeds results back for next inference round - History management: 8k token budget (2k system + 6k history), automatic summarization of older turns when budget exceeded, keeping recent 3 turns - LocalAgentService: session management facade with create/send/cancel/end/list, per-session CancellationToken support - Status transitions: Idle -> Thinking -> ToolExecution -> Streaming -> Idle - Prompt templates: system prompt, tool definition formatter, summarization prompt - 22 unit tests with mock ChatInferenceEngine and mock AgentToolExecutor covering single-turn, multi-step tool chains, max iteration limit, cancellation, history summarization, and status transitions Also fixes pre-existing issues: - Resolve Cargo.toml merge conflict between #1001 and #1000 - Remove duplicate `pub mod local_agent` in lib.rs - Add PartialEq derive on LocalAgentStatus for test assertions - Add Debug impl for StdioTransport, tempfile dev-dependency - Fix temporary lifetime in context_assembly test - Apply cargo fmt to pre-existing formatting issues Co-Authored-By: Claude <[email protected]> * address review: preserve tool call ordering with Vec instead of HashMap Switching from HashMap to Vec ensures tool calls with causal dependencies execute in the order the model emitted them, not random HashMap iteration order. Addresses reviewer recommendation from PR #1030 review. Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Michael Libio <[email protected]> Co-authored-by: Claude <[email protected]>
Summary
Implements the core ReAct (Reason + Act) loop driving the local agent (#1006):
agent_loop.rs): Orchestrates inference + tool-call cycles (max 5 iterations). Parses streaming chunks into text and tool calls, executes tools viaAgentToolExecutor, feeds results back for next inference round.create_session,send_message,cancel,end_session,get_sessions. Per-sessionCancellationTokensupport.prompt_templates.rs): System prompt, tool definition formatter, summarization prompt, tool result formatter.ChatInferenceEngineand mockAgentToolExecutorcovering single-turn, multi-step tool chains, max iteration limit, cancellation, history summarization trigger, and status transitions.Also fixes pre-existing issues: Cargo.toml merge conflict, duplicate
pub mod local_agent, missingPartialEqonLocalAgentStatus,DebugforStdioTransport,tempfiledev-dependency, temporary lifetime bug in context_assembly test, andcargo fmtacross multiple files.Test plan
cargo check -p nodespace-app-- clean, no warningscargo test -p nodespace-app -- local_agent::agent_loop local_agent::prompt_templates-- 22/22 passcargo test -p nodespace-app-- 155 passed, 3 failed (all pre-existing: context_assembly budget, transport buffering, tools direction)cargo fmt -p nodespace-app -- --check-- cleanCloses #1006