Skip to content

chenyangyc/SeGa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SeGa

This is the core implementation (i.e., the semantic reasoning agent) of SeGa: Semantics-driven Unit Test Generation for Uncovering Business Logic Bugs.

Introduction

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.

Repository Structure

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

Configuration

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=""

Installation

Prerequisites

  • Python 3.10+
  • ripgrep (for file search functionality)

Setup

pip install langchain langchain-anthropic langchain-openai langgraph pydantic python-dotenv

Usage Example

The 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"
)

Available Tools

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

Output Format

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": [...]
  }
}

Key Components

Semantic Reasoning Agent

Located at agent/semantic_mining_agent.py, this is the core component that:

  1. Analyzes the focal method and its surrounding code context
  2. Summarizes the method's business intent in natural language
  3. Retrieves relevant functionalities from the semantic knowledge base
  4. Derives fine-grained business scenarios with preconditions, actions, expected outcomes, and constraints

LLM Integration

The src/llms/llm.py module supports multiple LLM providers:

Tools

Swerex Runtime

The swerex/ directory contains the sandboxed execution environment for safe tool execution.

About

The core implementation (i.e., the semantic reasoning agent) of SeGa: Semantics-driven Unit Test Generation for Uncovering Business Logic Bugs.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages