Skip to content

Feat: implement AI-driven ReAct agent#27

Merged
ChingEnLin merged 6 commits into
devfrom
feat/query_generator_agent
May 10, 2026
Merged

Feat: implement AI-driven ReAct agent#27
ChingEnLin merged 6 commits into
devfrom
feat/query_generator_agent

Conversation

@ChingEnLin

Copy link
Copy Markdown
Owner

This pull request introduces a powerful, self-correcting ReAct (Reasoning and Acting) agent to the Query Generator page. By leveraging langgraph, this agent autonomously generates, executes, and evaluates MongoDB queries in a sandbox, significantly improving the accuracy and reliability of AI-generated queries.

Key Features & Changes

1. LangGraph ReAct Agent Integration

  • Iterative Self-Correction: Implemented a new react_agent_service utilizing a cyclic state graph (langgraph). The agent iteratively transitions between generate_query, execute_query_in_sandbox, and evaluate_query nodes until a valid, accurate query is produced.
  • Sandbox Execution: The agent safely runs generated read queries against the database in the background to verify correctness before presenting the final result to the user.
  • Max Iterations Guardrail: Configured safeguards to prevent infinite loops by setting a hard limit on the number of reasoning iterations.

2. Gated Write Operations & Safety

  • Strict Read-Only Agent: To ensure absolute safety, the ReAct agent is strictly prohibited from running write operations (Insert, Update, Delete) in its sandbox. Write queries are immediately passed back to the user without execution.
  • Post-Execution Write Evaluation: Introduced an "Evaluate with AI" workflow. Users manually review and run write operations, after which the new evaluate_write_service can verify the result of the manual execution against the user's initial intent.

3. Secure Write Audit Logging

  • Comprehensive Auditing: Intercepts executed write operations directly within the /query/execute endpoint.
  • Data Parity: Parses the target collection using robust RegEx and securely logs the user's email, full query, and operation result into the write_audit_log PostgreSQL table, ensuring audit parity with the Data Explorer.

ChingEnLin added 6 commits May 9, 2026 20:32
…pport

- Add max_iterations field to QueryPrompt schema (default 3, hard cap 10)
- Pass max_iterations through route -> run_query_generator -> AgentState
- Enforce hard limit via min(user_value, 10) in should_continue()
- Extend GENERATE_PROMPT to allow aggregate() pipelines as a tool
- Add maxIterations slider (1-10) above Generate button in UI with info tooltip
- Forward maxIterations from frontend to backend via API call
- Update geminiService.test.ts to satisfy new 2-arg minimum signature
Fix 1 - Add auth guard to /query/evaluate-write (OBO token + Bearer check)
Fix 2 - Cap evaluate_write_service tool-call loop at MAX_TOOL_ROUNDS=5
Fix 3 - Replace write-method substring heuristic with AST parsing in agent service
Fix 4 - Move inline imports to file-level in query.py
Fix 5 - Add Pydantic Field(ge=1, le=10) server-side cap on max_iterations
Fix 6 - Improve connection_string error logs with account ID context
Fix 7 - Replace fragile regex collection extraction with AST-based helper

Update tests: fix evaluate_write auth header + exchange_token_obo mock,
fix log_write_operation patch path to routes.query.* after import hoisting
@ChingEnLin ChingEnLin merged commit d26c652 into dev May 10, 2026
@ChingEnLin ChingEnLin deleted the feat/query_generator_agent branch May 10, 2026 13:01
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 2.9.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant