This is the core implementation (i.e., the semantic reasoning agent) of SeGa: Semantics-driven Unit Test Generation for Uncovering Business Logic Bugs.
Business logic bugs violate intended business semantics and are particularly prevalent in enterprise software. Yet most existing unit test generation techniques are code-centric, making such bugs difficult to expose. To address these limitations, we propose SeGa, a novel semantics-driven unit test generation technique that systematically integrates business semantics extracted from Product Requirement Documents (PRDs) into LLM-based test generation. SeGa constructs a semantic knowledge base from PRDs, represented as a set of functionality entries that group related requirements under a common business intent. Given a focal method, SeGa retrieves the relevant functionality entries and derives fine-grained business scenarios with explicit preconditions, triggering actions, expected outcomes, and semantic constraints to guide LLM-based test generation.
SeGa
│
├── agent/ # Main agent implementation
│ └── semantic_mining_agent.py # Semantic reasoning agent for scenario derivation
│
├── src/ # Core source code
│ ├── agents/ # Agent state definitions
│ │ └── state.py # LangGraph state schema
│ ├── llms/ # LLM interface
│ │ └── llm.py # LLM configuration (SeedCode, Claude, DeepSeek)
│ ├── memory/ # Memory management
│ │ ├── manager.py # Simple memory manager for historical patterns
│ │ └── simple_memory.py # Memory storage utilities
│ ├── prompts/ # Prompt templates
│ │ ├── context_model.py # Context model for agent runtime
│ │ ├── functionaliy_extraction_prompt.py # Prompts for extract functionality entries
│ │ ├── semantic_mining_prompt.py # Prompts for semantic reasoning agent
│ ├── tools/ # Agent tools
│ │ ├── editor.py # File editor core logic
│ │ ├── editor_tool.py # File editor tool (view, create, str_replace, insert)
│ │ ├── search_tools.py # File search by keywords using ripgrep
│ │ ├── code_inspector.py # Code inspection utilities
│ │ └── swerex_bash_tool.py # Bash command execution tool
│ └── utils/ # Utility scripts
│ └── traj_utils.py # Trajectory utilities
│
└── swerex/ # Swerex runtime for sandboxed execution
├── deployment/ # Deployment configurations
│ ├── local.py # Local deployment
│ ├── docker.py # Docker deployment
│ └── abstract.py # Abstract deployment interface
└── runtime/ # Runtime management
├── local.py # Local runtime
└── abstract.py # Abstract runtime interface
Create a .env file in the root directory to specify your API keys and model details:
# For OpenAI-compatible models
LLM_API_KEY=your_api_key
LLM_API_BASE=https://api.base.url
LANGCHAIN_TRACING_V2=true
LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"
LANGCHAIN_API_KEY=""
LANGCHAIN_PROJECT=""- Python 3.10+
- ripgrep (for file search functionality)
pip install langchain langchain-anthropic langchain-openai langgraph pydantic python-dotenvThe semantic reasoning agent analyzes a focal method and derives fine-grained business scenarios:
from agent.semantic_mining_agent import semantic_mining_agent
# Prepare input data
focal_method_info = {
"line_no": 42,
"change_lines": [...],
"unique_id": "method_id",
"max_agent_actions": 10
}
init_state = {
"target_base_info": {
"func_definition": "func checkEdit(...)",
# ... other method info
},
"target_file_info_dict": {
"repo_root": "/path/to/repo",
"rel_file_path": "path/to/file.go"
},
"target_context": [...] # Dependency info
}
# PRD data
summarize_prd = '{"functionalities": [...]}' # JSON string of extracted functionalities
row_prd_path = "/path/to/prd.md"
# Run the semantic reasoning agent
result, messages = semantic_mining_agent(
focal_method_info=focal_method_info,
init_state=init_state,
summarize_prd=summarize_prd,
row_prd_path=row_prd_path,
model_name="seedcode"
)The semantic reasoning agent has access to the following tools:
| Tool | Description |
|---|---|
str_replace_editor |
View, create, and edit files (view, create, str_replace, insert commands) |
search_files_by_keywords |
Search files by keywords using ripgrep |
run_shell_cmd |
Execute bash commands in the sandboxed environment |
The agent outputs structured JSON containing:
{
"business_scenarios": [
{
"functionality": "...",
"scenario": "...",
"pre_condition": "...",
"action_or_input": "...",
"expected_result": "...",
"semantic_constraints": "..."
}
],
"risk_and_constraints": {
"potential_defect_scenarios": [...],
"constraints": [...]
}
}Located at agent/semantic_mining_agent.py, this is the core component that:
- Analyzes the focal method and its surrounding code context
- Summarizes the method's business intent in natural language
- Retrieves relevant functionalities from the semantic knowledge base
- Derives fine-grained business scenarios with preconditions, actions, expected outcomes, and constraints
The src/llms/llm.py module supports multiple LLM providers:
- Editor Tool (
src/tools/editor_tool.py): File viewing and editing with persistent state - Search Tool (
src/tools/search_tools.py): Fast file search using ripgrep - Bash Tool (
src/tools/swerex_bash_tool.py): Shell command execution
The swerex/ directory contains the sandboxed execution environment for safe tool execution.