Metadata-Version: 2.4
Name: acps-math-retrace
Version: 1.0.0
Summary: Multi-agent math error attribution and adaptive variation practice on acps-sdk 2.1.0. A Diagnoser Agent diffs learner steps against the standard solution to locate calculation, misreading or logic gaps; a Concept Tracer Agent maps the mistake to exam knowledge points; a Generator Agent produces three same-difficulty variants for immediate consolidation - closing the loop from blind drilling to precise gap remediation for secondary-school exam review and university calculus self-checks.
Author: ACPs Math Retrace Engineering
License-Expression: MIT
Project-URL: Homepage, https://pypi.org/project/acps-math-retrace/
Keywords: acps-sdk,multi-agent,mathematics,error-analysis,diagnosis,adaptive-learning,exam-prep,calculus
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Education
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: acps-sdk==2.1.0
Requires-Dist: fastapi>=0.100.0
Requires-Dist: httpx>=0.25.0
Requires-Dist: pydantic>=2.0.0
Requires-Dist: uvicorn>=0.27.0
Provides-Extra: dev
Requires-Dist: build>=1.0.0; extra == "dev"
Requires-Dist: pytest>=8.0.0; extra == "dev"
Requires-Dist: ruff>=0.6.0; extra == "dev"
Requires-Dist: twine>=5.0.0; extra == "dev"
Dynamic: license-file

# acps-math-retrace

**数学错题归因与举一反三多智能体协作小工具** —— 基于 `acps-sdk 2.1.0` 构建的三智能体协作系统，把"盲目刷题"升级为"诊断错因 → 溯源考点 → 靶向巩固"的精准查漏补缺闭环。

## 命名含义

**Retrace** = **Re**（回溯归因）+ **trace**（溯源推演）：

- 向后 **re-trace** 学生的解题链路，定位错因根脉；
- 向前 **trace** 出同类型变式，完成当场巩固。

`acps-` 前缀表明本项目构建于 ACPs 智能体平台协议（`acps-sdk`）之上，与同生态的多智能体应用保持一致的工程范式。

## 项目定位与价值

题海战术的痛点不在于"做得少"，而在于"错了却不知道为什么错、错在哪个考点、接下来该练什么"。`acps-math-retrace` 用一组各司其职的 ACPs 智能体协作，替代低效率的人工复盘：

- **中高考数学刷题复盘**：学生提交一道错题的解题步骤与标准答案，系统秒级定位是算错、看错还是逻辑漏，并立刻给出 3 道同考点变式，避免"一错再错"。
- **大学高数作业自查**：对一元二次、二次函数等基础题型进行自检，把每一次错误都收敛到具体知识点，形成可追溯的知识图谱。
- **教培 / 私教备课**：教师批量导入错题，自动生成错因报告与分层变式题，减少重复性组卷劳动。

整个流程由 **Leader（DiagnosisSession）** 通过 AIP v2 协议编排三个独立部署的专家 Agent，每一步都留下可审计的 task 记录，而非单体脚本的"黑盒调用"。

## 三个协作 Agent

| Agent | 角色 | 技能标签 | 产物 |
| --- | --- | --- | --- |
| **Diagnoser Agent** | 错因诊断：逐行对齐学生步骤与标准答案，分类为计算错误 / 审题错误 / 逻辑漏洞 | `error-diagnosis` | `diagnosis-report` |
| **Concept Tracer Agent** | 考点溯源：指明考查的核心知识点（如"韦达定理和判别式的综合运用"），并把错因映射回考点 | `concept-trace` | `concept-trace-report` |
| **Generator Agent** | 举一反三：针对错因生成 3 道同类型、同难度变式题，附答案与解析 | `variant-generation` | `variant-report` |

**调度拓扑**：`Diagnoser → Concept Tracer → Generator → Leader(fan-in)`，Leader 通过 ADP 发现各 Agent 端点，用 `AipRpcClient.start_task` 派发任务并聚合 typed product。

## 安装

```bash
pip install acps-math-retrace
```

或从本地 wheel 安装：

```bash
pip install dist/acps_math_retrace-1.0.0-py3-none-any.whl
```

依赖：`acps-sdk==2.1.0`、`fastapi`、`uvicorn`、`httpx`、`pydantic`（纯 Python，可离线运行，无需任何 LLM API Key）。

## 一键运行（演示）

安装后一行命令启动完整多智能体协作（内置一道**包含计算错误**的韦达定理错题）：

```bash
math-retrace demo
```

无需安装也可从源码一键运行：

```bash
python run_demo.py          # 等价于 math-retrace demo
# 或
python -m math_retrace demo
```

切换其它内置样例：

```bash
math-retrace demo --case 1   # 逻辑漏洞：证明实根时缺失判别式
math-retrace demo --case 2   # 二次函数顶点计算错误
```

### 分析自定义错题

```bash
math-retrace analyze \
  --problem "已知方程 x² − 5x + 6 = 0 的两根为 x₁、x₂，求 x₁² + x₂²。" \
  --student "x₁ + x₂ = 5，所以 x₁² + x₂² = 5。" \
  --standard "x₁ + x₂ = 5，x₁x₂ = 6；x₁² + x₂² = (x₁+x₂)² − 2x₁x₂ = 25 − 12 = 13。" \
  --answer "13"
```

## 程序化调用

```python
import asyncio
from math_retrace.engine.samples import DEMO_PROBLEM
from math_retrace.mesh import LocalAgentMesh
from math_retrace.orchestrator import DiagnosisSession
from math_retrace.report import render_report

async def main():
    with LocalAgentMesh() as mesh:
        async with DiagnosisSession(mesh.registry) as session:
            report = await session.run(DEMO_PROBLEM)
    print(render_report(report))

asyncio.run(main())
```

## 运行测试

```bash
pip install -e ".[dev]"     # 安装 pytest 等开发依赖
pytest                       # 端到端：引擎断言 + 完整 AIP 往返 + 打包元数据
```

## 构建与发布到 PyPI

```bash
# 1. 构建 wheel 与 sdist（产物在 dist/）
python -m build

# 2. 校验产物
python -m twine check dist/*

# 3. 上传到 TestPyPI（先验证）
python -m twine upload --repository testpypi dist/*

# 4. 正式上传到 PyPI
python -m twine upload dist/*
```

> 上传需要 PyPI 账号与 API Token（在 https://pypi.org/manage/account/token/ 生成），并通过 `keyring` 或环境变量 `TWINE_USERNAME`/`TWINE_PASSWORD` 配置凭据，切勿把 Token 写入代码或命令历史。

## 预期输出（`math-retrace demo`）

下面是一次真实运行的输出（`session` / `report` / `task` id 与耗时每次运行不同，其余内容稳定）：

```text
[acps-math-retrace] 正在启动本地多智能体协作网格 ...
[acps-math-retrace] 在线 ACPs Agent: Diagnoser Agent, Concept Tracer Agent, Generator Agent
========================================================================================
ACPs Math Retrace v1.0.0 | session=math-...
端到端耗时 ... ms | report=report-... | SDK=acps-sdk 2.1.0
========================================================================================
[User] 错题题干：
已知关于 x 的一元二次方程 x² − (m+2)x + 2m = 0。
(1) 求证：无论 m 取何值，方程总有两个实数根；
(2) 若方程的两根为 x₁、x₂，且 x₁² + x₂² = 10，求 m 的值。

[1/3] Diagnoser Agent · 错因诊断
检测到 1 处错误，首要错因为【计算错误】——完全平方恒等变形符号错误。
问题 1：完全平方恒等变形符号错误  [计算错误]
  定位位置：第 3 步
  学生步骤：x₁² + x₂² = (x₁+x₂)² + 2x₁x₂ = (m+2)² + 4m = m² + 8m + 4。
  参考正解：x₁² + x₂² = (x₁+x₂)² − 2x₁x₂ = (m+2)² − 4m = m² + 4。
  修正方向：将 '+ 2x₁x₂' 改为 '− 2x₁x₂'，即 x₁²+x₂² = (x₁+x₂)² − 2x₁x₂。
  置信度：92%
参考答案（标准解题步骤）：
  1. Δ = (m+2)² − 4·1·2m = ... = (m−2)² ≥ 0，故方程总有两个实数根。
  2. 由韦达定理：x₁ + x₂ = m + 2，x₁x₂ = 2m。
  3. x₁² + x₂² = (x₁+x₂)² − 2x₁x₂ = ... = m² + 4。
  4. 由 m² + 4 = 10，得 m² = 6，故 m = √6 或 m = −√6。

[2/3] Concept Tracer Agent · 考点溯源
考点总结：本题考查了一元二次方程根的判别式与韦达定理的综合运用，并要求熟练运用完全平方公式对根的对称式进行恒等变形。
核心知识点：
  ★ 根的判别式 Δ = b² − 4ac
  ★ 韦达定理（根与系数的关系）
  ★ 完全平方恒等变形
错因→考点映射：学生的错误集中在“完全平方恒等变形”环节……

[3/3] Generator Agent · 举一反三变式巩固
针对【计算错误】——完全平方恒等变形符号错误 的靶向变式巩固（同类型、同难度，共 3 道）
变式 1：已知 ... x² − 7x + m = 0 ... 且 x₁² + x₂² = 29，求 m 的值。  → 答案：m = 10
变式 2：已知 ... x² − 7x + 10 = 0 ... 求 x₁² + x₂² 的值。              → 答案：x₁² + x₂² = 29
变式 3：已知 ... x² − 10x + 21 = 0 ... 求 |x₁ − x₂| 的值。           → 答案：|x₁ − x₂| = 4

AIP v2 协作审计
Diagnoser Agent      | error-diagnosis     | completed | ... ms | task=task-...
Concept Tracer Agent | concept-trace       | completed | ... ms | task=task-...
Generator Agent      | variant-generation  | completed | ... ms | task=task-...
调度拓扑：Diagnoser Agent → Concept Tracer Agent → Generator Agent → Leader
在线 Agent：Diagnoser Agent, Concept Tracer Agent, Generator Agent
```

## 项目结构

```text
acps-math-retrace/
├── pyproject.toml              # 打包配置：name=acps-math-retrace, version=1.0.0, acps-sdk==2.1.0
├── README.md
├── LICENSE
├── run_demo.py                 # 一键运行入口（无需安装）
├── src/
│   └── math_retrace/
│       ├── __init__.py
│       ├── __main__.py         # python -m math_retrace
│       ├── cli.py              # math-retrace 命令行入口
│       ├── demo.py             # 一键演示编排
│       ├── identity.py         # AIC 身份与 ACS 能力描述
│       ├── registry.py         # 本地 ADP 能力注册与发现
│       ├── runtime.py          # AIP v2 传输：AgentServer / ResultBuilder / 产品编解码
│       ├── models.py           # 全部 typed product（DiagnosisReport 等）
│       ├── mesh.py             # LocalAgentMesh：启动并托管 3 个 Agent 服务
│       ├── orchestrator.py     # DiagnosisSession：Leader 编排与 fan-in
│       ├── report.py           # 终端可读报告渲染
│       ├── py.typed
│       ├── agents/
│       │   ├── base.py         # AipAgent 基类（AIP handler 契约）
│       │   ├── diagnoser.py    # Diagnoser Agent
│       │   ├── concept_tracer.py  # Concept Tracer Agent
│       │   └── generator.py    # Generator Agent
│       └── engine/             # 领域引擎（纯 Python，离线可运行）
│           ├── patterns.py     # 考点家族 / 错因模式 / 概念图谱注册表
│           ├── diagnosis.py    # 错因诊断引擎
│           ├── concepts.py     # 考点溯源引擎
│           ├── variants.py     # 变式生成引擎（算术保证答案正确）
│           └── samples.py      # 内置错题样例
└── tests/
    └── test_workflow.py        # 端到端测试
```

## License

MIT
