# 用 Gemini 4 Carbon 构建修复失败测试的编程智能体。

Canonical URL: https://www.iagenify.com/zh/news/gemini-4-carbon-coding-agent-fix-failing-tests

让本地智能体读取错误、修改文件并重新运行测试，同时遵守额度预算；运行过程中也可以继续指导它。

## Key points
- 本地智能体只会修改工作区文件，并执行权限回调允许的命令。
- 额度预算会在超过设定金额前停止运行。
- 使用 `agent.queue()` 可在运行期间添加限制，不必停止智能体。

修复失败的测试是一种循环：读取错误、查找原因、修改代码、再次运行测试。IaGenify 本地智能体可以在你的电脑上执行这个循环，你可以实时观察；本教程使用 Gemini 4 Carbon。可运行的命令和额度都由你决定。

## 把项目放入智能体的工作区

在电脑上，本地智能体使用用户目录中 IaGenify SDK 自带的工作文件夹。把项目克隆到该目录，这样智能体读取和编辑的就是你需要检查的文件。


```
git clone https://github.com/your-org/shop-api ~/.iagenify/files/shop-api
```

## 创建智能体

Auto 模式允许智能体无需询问便修改工作区中的文件，但运行命令仍需由权限回调决定。本例只允许测试和 lint 脚本，其他命令一律拒绝。预算会在支出超过 60 个额度前停止运行，记忆功能则保存可回退的检查点。


```
import { IaGenify } from "iagenify";

const api = new IaGenify({ apiKey: process.env.IAGENIFY_API_KEY });
const pc = await api.storage.get();

const agent = api.agents.create({
  runtime: "local",
  machine: pc,
  model: "gemini-4-carbon",
  mode: "auto",
  budget: 60,
  memory: true,
  onPermission: async ({ category, args }) =>
    category === "exec" && /npm (test|run lint)\b/.test(args.command ?? "") ? "once" : "deny",
});
```

## 运行并适时引导


```
const running = agent.run({
  message: "In shop-api, run npm test, find why the failing tests fail, fix the code (not the tests), and run npm test again until it passes.",
  onEvent(event) {
    if (event.type === "tool_start") console.log("→", event.tool, event.args?.path ?? event.args?.command ?? "");
  },
});

// A constraint you thought of later, read before the agent's next step:
setTimeout(() => agent.queue("Keep the public functions of src/cart.js unchanged."), 20_000);

const result = await running;
console.log(result.stopReason, result.usage.credits);
```

运行结束后，`stopReason` 会指出智能体已完成（`end_turn`）还是触及预算上限；`usage.credits` 会显示实际支出。

## 提交前检查

智能体会修改文件，但不会自动提交。请检查差异、亲自运行测试，再决定是否合并。如果方向有误，`rewind()` 会把工作区恢复到发送该消息之前。

## 参考资料
- [本地智能体](https://developers.iagenify.com/sdk/ref/local-agent)
- [智能体权限](https://developers.iagenify.com/sdk/agent/permissions)
- [Gemini 4 模型](https://developers.iagenify.com/sdk/ref/models/gemini-4)

## FAQ
### 智能体能把修改推送到我的仓库吗？
本例中不能：权限回调只允许测试和 lint 脚本，因此会拒绝 `git push`。

### Carbon 的费用是多少？
截至 2026 年 11 月 30 日，每百万输入 token 为 500 额度，每百万输出 token 为 2,500 额度；从 12 月 1 日起分别为 1,000 和 5,000。运行预算限制总支出。
