构建目标模式智能体循环 | AI生成和翻译

首页 2026.07

问题:在像 Hermes(Nous Research)这样的智能体框架中,实际上如何使用“目标模式”/长期任务执行——以及如何将相同的模式构建到你自己的 CLI 智能体中(ww, iclaw, zz)?

答案:

1. Hermes 的实际机制(原理,而非炒作)

Nous Research 的 Hermes Agent 是一个开源 AI 智能体框架,采用 MIT 许可证发布,它运行一个五阶段的学习循环:执行、评估、将可重复的推理模式提取为命名技能、完善这些技能、为新任务检索相关技能,并配备五个沙箱后端——本地、Docker、SSH、Singularity 和 Modal。与 OpenClaw 等框架的区别在于,Hermes 将技能形成视为智能体自身运行循环的一部分,而不是依赖外部技能市场——在完成复杂任务后,它会编写一份结构化的技能文档,记录步骤、陷阱和验证步骤,并在后续运行中改进该技能。

具体来说,“目标模式”的长期任务机制是这样工作的:

⚠️ 注意事项:描述这一机制的几个来源(aiprofitboardroom.com、juliangoldieaiautomation.com、aisuccesslabjuliangoldie.com)是同一个 SEO 内容农场作者重复近乎相同的模板——请将具体数字(ROI 声称、£/月节省)视为营销,而非经过验证的事实。机制本身(基于判断模型的回合预算循环、/goal 命令、配置路径)在不同独立文章中(i-scoop.eu、openhosst.com)得到证实,因此我更相信架构描述,而非案例研究数字

在复制此模式前需要记住的两个失败模式:目标模糊导致判断模型无法确定完成——请具体说明“完成”的含义;以及为大目标使用默认回合预算,因为 20 个回合无法完成 50 步的任务。

2. 这是一种通用模式——用约 80 行代码自行构建

你不需要 Hermes 来实现这一点。核心原语是:执行循环 + 判断/评论 + 回合预算 + 持久化状态。学术工作将其形式化为规划器-执行器分离——规划器接收任务指令,在执行前将其分解为参考子目标图,然后通过将执行器的轨迹与该图进行比较来跟踪进度,当执行器偏离或停滞时进行纠正性反馈。这可以直接用于你的 CLI 智能体。

# goal_loop.py — 适用于 ww/iclaw/zz 的最小化 Hermes 风格目标框架
import json, os, time
from anthropic import Anthropic

client = Anthropic()
STATE_PATH = ".goal_state.json"

def load_state():
    if os.path.exists(STATE_PATH):
        return json.load(open(STATE_PATH))
    return None

def save_state(s):
    json.dump(s, open(STATE_PATH, "w"), indent=2)

def judge(goal: str, transcript: str) -> dict:
    """保守的判断模型——出错时失败开放(遵循 Hermes 语义)。"""
    try:
        resp = client.messages.create(
            model="claude-opus-4-8",  # 使用比执行器更强的模型作为判断模型
            max_tokens=300,
            system=(
                "You are a strict completion judge. Given a GOAL and a TRANSCRIPT of "
                "an agent's work, respond ONLY with JSON: "
                '{"done": bool, "blocked": bool, "reason": str}. '
                "Mark done=true only if the deliverable is explicitly produced, or "
                "blocked=true if further progress is genuinely impossible."
            ),
            messages=[{"role": "user", "content": f"GOAL:\n{goal}\n\nTRANSCRIPT:\n{transcript}"}],
        )
        return json.loads(resp.content[0].text)
    except Exception:
        return {"done": False, "blocked": False, "reason": "judge_error_fail_open"}

def actor_step(goal: str, history: list, tools: list) -> str:
    resp = client.messages.create(
        model="claude-sonnet-5",
        max_tokens=2000,
        system=f"Standing goal: {goal}\nWork one concrete step closer to completion. "
                f"State clearly if you believe the goal is now complete.",
        messages=history,
        tools=tools,
    )
    # ... 处理 tool_use 块,附加 tool_result 等(为简洁省略)
    text = "".join(b.text for b in resp.content if b.type == "text")
    history.append({"role": "assistant", "content": resp.content})
    return text

def run_goal(goal: str, tools: list, max_turns: int = 20, resume: bool = False):
    state = load_state() if resume else None
    history = state["history"] if state else []
    turn = state["turn"] if state else 0

    while turn < max_turns:
        turn += 1
        out = actor_step(goal, history, tools)
        transcript = "\n".join(m.get("content", "") if isinstance(m.get("content"), str) else str(m) for m in history)

        verdict = judge(goal, transcript)
        print(f"[turn {turn}/{max_turns}] judge={verdict}")

        if verdict["done"] or verdict["blocked"]:
            save_state({"history": history, "turn": turn, "status": "finished", "reason": verdict["reason"]})
            return verdict

        save_state({"history": history, "turn": turn, "status": "running"})
        time.sleep(1)  # 退避/速率限制卫生

    save_state({"history": history, "turn": turn, "status": "paused"})
    print(f"⏸ Goal paused — {turn}/{max_turns} turns used. Call run_goal(..., resume=True) to continue.")
    return {"done": False, "blocked": False, "reason": "turn_budget_exhausted"}

关于每个部分对你的上下文重要性的说明:

3. 深入方向(符合你的 MoE/智能体架构方向)

对于你的 CLI 工具具体而言:我会先将 judge() 失败开放模式和回合预算暂停/恢复机制接入 zzww,因为这只需约 80 行且与框架无关,然后再全面采用 Hermes——你就能获得相同的长期任务能力,而无需承担其生态系统(根据比较文章)比 OpenClaw 更小、更年轻,且安全性尚未在大规模下得到验证的依赖。

参考文献:


AI生成和翻译,但整理分享仍需精力。如果觉得有帮助,欢迎 捐助支持。 微信:@lzwjavaWeChat QR · X: @lzwjava · 打个招呼 👋

返回 捐助