Build 2026(2):Agent Harness的两个杀手级特性:CodeAct 与 Handoff

Microsoft Build 2026

Posted by Bruce Wong on June 7, 2026

上一篇讲了 微软押注生产级 Agent 执行层——框架级执行层。这篇深入两个改变交互范式的能力:CodeAct 减少往返,Handoff 实现动态路由。


CodeAct + Hyperlight:用代码减少往返

Harness 默认走传统 tool-calling:模型选工具 → 执行 → 结果返回 → 再选下一步,多轮往返。CodeAct 是可选加速模式——给模型一个 execute_code 工具,让它把多步调用写成一段 Python,一次性执行

减少的是 model ↔ execution 的往返次数,不是执行速度

以”计算多用户订单总价”为例(涉及 list users / get orders / get discounts 等数十次 tool call):

模式 时间 Tokens 提升
Traditional tool-calling 27.81s 6,890
CodeAct 13.23s 2,489 52% / 64%

为什么更快、更省 token

Traditional 流程(5 次 tool call = 5 次 LLM 介入):

用户: "计算所有用户订单总价"
  ↓
LLM: 我需要先 list users → 生成 tool call: list_users()
  ↓
[执行] → 返回 8 个用户 ID
  ↓
LLM: 拿到用户了,现在 get orders for user_1 → 生成 tool call: get_orders("user_1")
  ↓
[执行] → 返回订单列表
  ↓
LLM: 继续 get orders for user_2 → 生成 tool call: get_orders("user_2")
  ↓
...(重复 8 次)
  ↓
LLM: 现在算折扣和税费 → 生成 tool call: get_discount("order_123")
  ↓
...(更多往返)
  ↓
LLM: 终于能生成最终回答了

CodeAct 流程(1 次 LLM 介入 + 沙箱执行):

用户: "计算所有用户订单总价"
  ↓
LLM: 我把整个逻辑写成代码 → 生成 execute_code:
       users = call_tool("list_users")
       total = 0
       for user in users:
           orders = call_tool("get_orders", user_id=user.id)
           for order in orders:
               discount = call_tool("get_discount", order_id=order.id)
               tax = call_tool("get_tax_rate", region=order.region)
               total += (order.amount - discount) * (1 + tax)
       print(f"总价: {total}")
  ↓
[Hyperlight 沙箱执行所有 tool calls,返回最终结果]
  ↓
LLM: 生成最终回答
  • Traditional:5 次 tool call = 5 次 model turns,每次都要重新生成思考过程
  • CodeAct:1 次 model turn 生成代码,沙箱执行完返回最终结果

安全不妥协:模型生成的代码跑在 Hyperlight micro-VM 里——每次调用一个新鲜隔离的虚拟机,强隔离 overhead 几乎为零。代码通过 call_tool(...) 调用宿主注册的真实工具,沙箱本身不直接访问外部资源。

from agent_framework_hyperlight import HyperlightCodeActProvider

codeact = HyperlightCodeActProvider(tools=[get_weather])
agent = create_harness_agent(client=client, context_providers=[codeact])

CodeAct 不做编排决策,编排还是 LLM 的事。它的价值是:让 LLM 能用代码表达复杂逻辑(循环、条件、变量),并在安全环境里执行。

一句话澄清安全边界:CodeAct 沙箱 = 防 LLM 乱写代码,不防 LLM 乱用你的工具。工具安全还是你的事。如果 send_email() 有副作用,给工具加 approval_mode="always_require",而不是指望沙箱兜底。


Handoff:Harness 里的多 agent 编排

多 agent 的经典问题:router 转发一次就断,follow-up 和上下文传递全靠 hack。

Harness 的 Handoff pattern:你声明拓扑,框架注入 handoff 工具,agent 自己决定什么时候移交控制权

workflow = (
    HandoffBuilder(name="customer_support")
    .participants([coordinator, refund, shipping, tech])
    .set_coordinator(coordinator)
    .with_interaction_mode("autonomous")
    .with_termination_condition(should_terminate)
    .build()
)

和 GitHub Copilot Custom Agents Handoff 的区别

如果你用过Github Copilot 的 Custom Agents 功能,可能对它的 Handoff 也很熟悉。Copilot Handoff 是预定义的顺序流水线,MAF Handoff 是动态路由的会话转移——像客服中心:前台评估问题,自动转给退款/物流/技术支持,处理完可以结束,也可以转回前台。

  Copilot Handoff MAF Handoff
路由方式 固定顺序(Plan→Implement→Review) 动态决定(根据内容路由到 specialist)
触发方式 用户点击按钮 Agent 自主调用 handoff 工具
终止条件 必须走完所有步骤 可提前结束(条件满足就终止)
回到起点 不支持 支持(specialist 可 handoff 回 coordinator)
适用场景 开发工作流 运行时服务(客服、分诊)

一句话

CodeAct 让 agent 执行更快,Handoff 让 agent 协作更智能——一个是单 agent 的效率优化,一个是多 agent 的动态编排。合在一起,Harness 才不只是”能跑”,而是能扛、能扩展、能自治


参考链接

  1. CodeAct in Agent Framework: Faster Agents with Fewer Model Turns
  2. Executable Code Actions Elicit Better LLM Agents (arXiv:2402.01030)
  3. Handoff Workflows in Microsoft Agent Framework
  4. GitHub Copilot Custom Agent File Guidelines
  5. Microsoft Execution Containers (MXC) on GitHub

理解 AI,用好 AI,让 AI 帮助自我进化,加油。