Production-grade Python code generation system with AST-aware context, layered defense, and multi-agent orchestration
pip install pygenius-ai==0.3.17 Copy PIP instructions
pygenius/ ├── pyproject.toml # 项目配置 + ruff/mypy 规则 ├── README.md ├── sandbox/Dockerfile # Docker 沙箱镜像 ├── examples/ │ └── demo_mock.py # 端到端 demo(无需 API key) ├── src/pygenius/ │ ├── main.py # CLI 入口 (click) │ ├── llm/ # 🆕 LLM Provider 抽象层 │ │ ├── base.py # LLMProvider 抽象类 + 数据模型 │ │ ├── mock.py # MockLLMProvider(测试用) │ │ ├── openai_provider.py # OpenAI 兼容后端 │ │ └── router.py # 🆕 LLMRouter 多 provider 路由 │ ├── agents/ # 多智能体 │ │ ├── base.py # 基类 │ │ ├── planner.py # Plan-and-Solve 架构师 │ │ ├── coder.py # ReAct 码农循环(LLM 驱动) │ │ └── reviewer.py # Reflection 审查专家 │ ├── orchestrator/ │ │ └── orchestrator.py # 协调器(项目经理) │ ├── context/ # AST 上下文 │ │ ├── extractor.py # L1/L2/L3 提取 + 缓存 │ │ ├── dependency.py # 跨模块依赖图 │ │ └── assembler.py # 上下文组装(预算+降级) │ ├── memory/ # 三层记忆 │ │ ├── short_term.py # 会话记忆 + 反思触发 │ │ ├── long_term.py # 持久记忆(TTL+冲突) │ │ └── vector.py # 向量记忆(ChromaDB) │ ├── budget/ # Token 预算 │ │ └── token_budget.py # 全局+任务+Agent 三层限额 │ ├── events/ │ │ └── event_bus.py # 事件总线(观察者模式) │ ├── sandbox/ │ │ └── docker_sandbox.py # Docker 沙箱 │ ├── tools/ # 工具链 │ │ ├── file_ops.py # 文件操作(路径穿越防护) │ │ ├── command.py # 命令执行(白名单) │ │ ├── linter.py # ruff→pyright→mypy 分层 │ │ ├── tester.py # pytest + coverage │ │ ├── quality_checker.py # AST 代码质量检查 🆕 │ │ ├── package_manager.py # pip/uv 安装 + pip-audit │ │ ├── ast_engine.py # AST 分析工具 │ │ ├── git_ops.py # Git 操作 │ │ ├── security.py # 命令分类 + 密钥扫描 │ │ └── registry.py # 工具注册中心 │ ├── config/settings.py # pydantic-settings 配置 │ └── models/ # Pydantic 数据模型 │ ├── messages.py # Agent 消息 │ ├── tasks.py # 任务模型 │ └── context.py # 上下文模型 └── tests/ # 1116 个测试 ├── conftest.py ├── test_llm.py # 🆕 LLM provider 测试 (18) ├── test_coder.py # 🆕 Coder ReAct 测试 (25) ├── test_coder_robustness.py # 🆕 Coder 鲁棒性测试 (41) ├── test_coder_actions.py # 🆕 Coder 动作执行测试 (15) ├── test_reviewer.py # 🆕 Reviewer 测试 (10) ├── test_reviewer_quality.py # 🆕 Reviewer 质量检查测试 (10) ├── test_orchestrator.py # 🆕 Orchestrator 测试 (9) ├── test_orchestrator_edge_cases.py # 🆕 Orchestrator 边界测试 (14) ├── test_dependency.py # 🆕 依赖图测试 (6) ├── test_dependency_comprehensive.py # 🆕 依赖图全面测试 (24) ├── test_command.py # 🆕 命令执行测试 (13) ├── test_command_edge_cases.py # 🆕 命令执行边界测试 (7) ├── test_linter.py # 🆕 Linter 测试 (9) ├── test_linter_comprehensive.py # 🆕 Linter 解析边界测试 (12) ├── test_linter_edge_cases.py # 🆕 Linter 边界测试 (5) ├── test_linter_fallback.py # 🆕 Linter fallback 测试 (3) ├── test_linter_resolution.py # 🆕 Linter 命令解析测试 (12) ├── test_tester.py # 🆕 TestRunner 测试 (7) ├── test_tester_comprehensive.py # 🆕 TestRunner 全面测试 (8) ├── test_config.py # 🆕 配置测试 (8) ├── test_ast_engine.py # AST 引擎 (10) ├── test_ast_engine_comprehensive.py # 🆕 AST 引擎异常测试 (13) ├── test_ast_engine_edge_cases.py # 🆕 AST 引擎边界测试 (2) ├── test_context.py # 上下文组装 (5) ├── test_assembler_comprehensive.py # 🆕 上下文组装全面测试 (19) ├── test_context_comprehensive.py # 🆕 上下文模块全面测试 (36) ├── test_tools.py # 文件/安全 (15) ├── test_file_ops_comprehensive.py # 🆕 文件操作全面测试 (10) ├── test_memory.py # 记忆系统 (12) ├── test_short_term_comprehensive.py # 🆕 STM 全面测试 (11) ├── test_long_term_comprehensive.py # 🆕 LTM 全面测试 (63,含去重/衰减/压实) ├── test_budget.py # Token 预算 (7) ├── test_agents.py # Planner/Plan (8) ├── test_base_agent.py # 🆕 BaseAgent 基础设施测试 (11) ├── test_planner_comprehensive.py # 🆕 Planner 全面测试 (52) ├── test_events.py # 事件总线 (5) ├── test_quality_checker.py # 🆕 代码质量检查测试 (24) ├── test_codegen_mock_expansion.py # 🆕 多项目类型测试 (26) ├── test_main_helpers.py # 🆕 CLI helper 全面测试 (27) ├── test_llm_base_helpers.py # 🆕 LLM base + FakeClock 测试 (6) ├── test_llm_mock_extras.py # 🆕 MockLLMProvider 扩展测试 (3) ├── test_vector_memory.py # 🆕 向量记忆测试 (19) ├── test_openai_provider_extras.py # 🆕 OpenAI provider retry+embed 测试 (12) ├── test_git_ops_safety.py # 🆕 Git hard_reset 安全测试 (3) ├── test_quality_checker_edge_cases.py # 🆕 质量检查边界测试 (6) ├── test_docker_sandbox_mocked.py # 🆕 Docker sandbox mock 测试 (25) ├── test_assembler_edge_cases.py # 🆕 上下文组装边界测试 (7) ├── test_command_docker_mocked.py # 🆕 Command Docker mock 测试 (10) ├── test_coder_parse_edge_cases.py # 🆕 Coder 解析边界测试 (9) ├── test_multi_turn_interaction.py # 🆕 多轮交互端到端测试 (5) ├── test_cli_callback_interaction.py # 🆕 CLI 回调交互测试 (7) ├── test_orchestrator_multi_turn.py # 🆕 Orchestrator 多轮路径测试 (4) ├── test_main_verbose.py # 🆕 CLI verbose traceback 测试 (7) ├── test_parallelism_report.py # 🆕 冲突可视化报告测试 (27) ├── test_ltm_maintenance.py # 🆕 LTM 自动维护钩子测试 (17) ├── test_ltm_maintenance_surface.py # 🆕 LTM 维护表面接线测试 (19) ├── test_packaging.py # 🆕 打包发布测试 (12) ├── test_assembler_remaining.py # 🆕 上下文组装剩余分支测试 (2) ├── test_tool_calling.py # 🆕 原生 tool calling 测试 (30) ├── test_openai_streaming.py # 🆕 OpenAI streaming SSE 测试 (7) ├── test_openai_provider.py # 🆕 OpenAI chat/embed/retry 测试 (30) ├── test_search_replace.py # 🆕 search_replace 工具测试 (19) └── test_web_ops.py # 🆕 web_search/fetch_url 工具测试 (37) 快速开始 运行测试 # 安装 python -m venv .venv .venv/bin/pip install -e ".[dev]" # 跑测试 .venv/bin/python -m pytest tests/ -v # 跑覆盖率 .venv/bin/python -m pytest tests/ --cov=pygenius --cov-report=term # 代码检查 .venv/bin/ruff check src/ tests/ .venv/bin/ruff format src/ tests/ 端到端 Demo # 无需 API key — 用 MockLLMProvider 跑通完整流程 .venv/bin/python examples/demo_mock.py 输出示例:
📋 === Step 1: Planning === Project: user Tech stack: {'framework': 'FastAPI', 'database': 'PostgreSQL'} Subtasks: 7 🔧 === Step 2: Execute T1 (Initialize project) === Status: completed Iterations: 2 ✅ Created: pyproject.toml 🔍 === Step 3: Review === Approved: True ✅ Score: 100.0 📊 === Step 4: Final Status === Tokens used: 14,424 / 500,000 LLM calls: 3 ReAct steps: 2 接入真实 LLM from pygenius.llm.openai_provider import OpenAICompatibleProvider from pygenius.orchestrator.orchestrator import Orchestrator import os # DeepSeek provider = OpenAICompatibleProvider( api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com/v1", default_model="deepseek-chat", ) orch = Orchestrator(project_dir="./my_project", llm=provider) result = orch.execute("Create a FastAPI REST API for a todo app") LLM Provider 抽象 所有 Agent 通过 LLMProvider 接口调用 LLM,可插拔:
from pygenius.llm import LLMRouter, OpenAICompatibleProvider strong = OpenAICompatibleProvider(default_model="gpt-4o") # 规划/重构 balanced = OpenAICompatibleProvider(default_model="gpt-4o-mini") # 日常编码 fast = OpenAICompatibleProvider(default_model="gpt-4o-nano") # 简单编辑 router = LLMRouter( strong=strong, balanced=balanced, fast=fast, enable_fallback=True, # 主 provider 失败自动切换 ) orch = Orchestrator(project_dir="./my_project", llm=router) 路由策略(基于关键词分类复杂度):
Coder 是核心代码生成 agent,使用 ReAct 范式(Thought → Action → Observation 循环):
所有子任务都包含明确的 acceptance criteria(ruff/mypy/pytest 通过标准)。
Orchestrator 管理整个 Plan → Execute → Review 流水线:
PyGenius 支持在关键决策点引入人工审批,通过可插拔的 InteractionCallback 协议:
# 非交互(默认,自动执行) pygenius generate "Create a FastAPI app" # 交互模式(人工审批) pygenius generate "Create a FastAPI app" --interactive # 或简写 pygenius generate "Create a FastAPI app" -i 代码集成:
from pygenius.interaction import CLICallback from pygenius.orchestrator import Orchestrator orch = Orchestrator( project_dir="./my_project", llm=llm, interaction=CLICallback(), # 启用交互 ) result = orch.execute("Create a FastAPI app") 安全 命令三档分类:auto / approval / deny 路径穿越防护(file_ops 校验所有路径) 密钥扫描(PASSWORD / API_KEY / PRIVATE_KEY 等模式) Docker 沙箱:非 root 用户、网络隔离、内存/CPU/PID 限制、只读文件系统 ChromaDB 遥测默认关闭 CLI 用法 # 初始化项目结构 pygenius init # 生成项目计划(dry-run,不执行) pygenius plan "Create a FastAPI user registration system with PostgreSQL" # 并行性预览(波次 + 加速比 + 文件冲突,不执行) pygenius analyze "Create a FastAPI user registration system with PostgreSQL" # 同上,但输出机器可读 JSON(CI / jq 友好) pygenius analyze --json "Create a FastAPI user registration system" | jq .estimated_speedup # 冲突可视化报告:任务清单/波次布局/C-M 冲突矩阵/延迟代价(含传递性)/建议,落盘 Markdown pygenius analyze "Create a FastAPI todo app" --report parallelism-report.md # 并行波次执行(冲突安全,任务并发) pygenius --mock generate --parallel "Create a simple Python CLI tool" pygenius --mock generate -p --max-workers 8 "Create a simple Python CLI tool" # MCP 服务器(Claude Desktop 等客户端通过 stdio 调用) pygenius mcp --project-dir /path/to/projects # Claude Desktop 配置(claude_desktop_config.json): # {"mcpServers": {"pygenius": {"command": "pygenius", "args": ["mcp"], # "env": {"PYGENIUS_PROJECT_DIR": "/path/to/projects"}}}} # 离线测试模式(无需 API key) pygenius --mock generate "Create a simple Python CLI tool" # 使用真实 LLM 生成(需设置 OPENAI_API_KEY 或 LLM_API_KEY) pygenius --model deepseek-chat generate "Build a REST API with SQLite" # 查看项目状态 pygenius status # 对当前项目做完整审查 pygenius review # 管理长期记忆 pygenius memory list pygenius memory search "sqlalchemy" pygenius memory add "Always use AsyncSession for async SQLAlchemy" \ --category pattern --tags "sqlalchemy,async" pygenius memory stats # 统计 + 维护报告(分类分布/平均置信度/陈旧条目数/文件大小) pygenius memory decay # 手动衰减:置信度按闲置天数半衰,低于阈值剪枝 pygenius memory merge # 手动压实:合并近似重复条目(老条目胜出,置信度提升,tags 并集) pygenius memory stats --json | jq .average_confidence # CI/脚本用机器可读输出(三个子命令都支持 --json) # Docker 沙箱管理 pygenius sandbox check # 检查是否可用 pygenius sandbox build # 构建沙箱镜像 pygenius sandbox run "pytest" # 在沙箱中执行命令 测试覆盖 1681+ passed (1-2 skipped) in ~4 min # 全量(含打包 12 + 冲突报告 27 + LTM 维护 17 + 表面接线 19 + memory 子命令 11 + JSON 输出 10) 1686 passed in ~35 min # 内环:pytest -m "not e2e"(跳过 16 个全管线 E2E;含嵌套 coverage 子进程测试,非历史快环) All ruff checks passed! mypy: no issues found in 59 source files ✅ E2E Score: 100/100 ✅ 测试工作流:
发布就绪:hatchling 构建 sdist + wheel(twine check PASSED);PEP 561 py.typed;干净 venv 安装 wheel 后 CLI / MCP 服务器 / analyze --json / generate E2E / --report / memory --json 全部冒烟通过。版本里程碑 git tag v0.1.0 / v0.1.1 / v0.2.0 / v0.2.1 / v0.3.0(0.1.1:冲突可视化报告 + LTM 去重/衰减/压实;0.2.0:LTM 全生命周期——自动维护钩子 + 三表面接线 + memory stats/decay/merge CLI + --json 遥测输出;0.2.1:学习闭环三连——Coder 全类目召回 + Reviewer 教训标注/recurring + stats 健康度指标;0.3.0:闭环可观测与自愈——MCP 记忆可观测(stats resource + memory_search tool)+ recurring 教训自修正(失配降置信)+ 自愈环重写(淘汰教训的确定性后继 + lineage 演化链))。pygenius.__version__ 与 pyproject 版本锁步(0.1.1 曾 desync,已有打包测试守卫)。
剩余 1% 为各模块零散防御性分支——linter.py 163-177 行(mypy 旧版单 JSON 对象解析路径,实际不可达的死代码)及少量需特定异常组合才能触发的分支。
agent ast code-generation llm mcp multi-agent python
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
None of the files in this release were uploaded with provenance metadata.
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.




