Harness²:凍結模型也能升級的 Agent Runtime

Harness²:凍結模型也能升級的 Agent Runtime

📌 本文重點

  • 模型凍結,升級放在 Agent runtime
  • 工具抽象成 skill,避免 tool explosion
  • 安全策略外掛在 runtime,而非只寫在 prompt 裡

在企業環境裡,多數人動不了基礎模型:不是自己訓練的、API 貴、換一次要重做安全審查。Harness² 類的架構解法是:把模型凍結,升級包在外面——用多層 prompt 模板、工具編排、policy 與 log/trace 圍一個 Agent runtime,讓行為升級而不是模型升級。

💡 關鍵: 在「模型不能動」的前提下,Harness² 透過 runtime 層的封裝,讓行為可以持續升級而不必頻繁更換基礎模型。

下面從系統架構角度拆解,並給一個「用雲端 GPT 做 core,再包一層 Harness² 風格 runtime」的實作範例。


重點說明:Harness² 的三個關鍵設計

1. 技能 / 工具抽象成可重用組件

在 Harness² 思路裡,skill ≈ 可組裝的工具 + prompt 模板 + policy。重點是抽象層次:

  1. Tool Layer(原子工具)
  2. 純函式 / API 包裝,例如 search_documents, update_ticket。
  3. 宣告式描述:name、input_schema、output_schema、cost_hint(token/latency)等。

ts
// 假想的 tool 定義
const tools = [
{
name: "search_kb",
description: "在企業知識庫中全文檢索",
input_schema: {
type: "object",
properties: { query: { type: "string" }, top_k: { type: "number" }},
required: ["query"]
},
safety_tags: ["read_only"],
handler: async (input) => searchKB(input.query, input.top_k ?? 5)
},
];

  1. Skill Layer(可重用技能)
  2. 將多個工具 + prompt 固化成可調用工作流。
  3. 對外暴露一個較高階的介面給上層 Agent,而不是讓 Agent 直接操作所有底層工具,避免 tool explosion。

ts
const skills = [
{
name: "answer_product_question",
description: "用知識庫回答產品相關問題,必要時加上 disclaimers",
input_schema: { type: "object", properties: { question: { type: "string" }}},
plan: async (input, ctx) => {
// 1) 查 KB
const docs = await ctx.callTool("search_kb", {
query: input.question,
top_k: 5
});
// 2) 再交給 LLM summary
return await ctx.callModel("gpt-4o", {
role: "system",
content: `你是產品專家,只能根據以下文件回答。若文件不支持,請說不知道。\n\n${JSON.stringify(docs)}`
}, [
{ role: "user", content: input.question }
]);
}
}
];

  1. Agent Layer(編排 + 策略)
  2. Agent 不直接看見所有 tool,而是看見一組精選的 skills。
  3. 方便做 權限切分、租戶隔離、A/B 測試(用不同 skill graph)。

實際好處:

  • 換工具供應商(搜尋、向量 DB)時,只改 Tool Layer;Skill/Agent 不動。
  • 不同團隊可共用 skill 套件庫,減少 prompt / workflow 重複造輪子。

2. 多層 prompt + 自迭代設計:怎麼改寫又不失控

Harness² 一個關鍵點是:讓 Agent 可以改寫 prompts / 工具使用方式,但要有邊界。可以拆成三層:

  1. Framework System Prompt(凍結)
  2. 只放框架行為規則與安全邊界,不被動態覆寫。

ts
const FRAMEWORK_SYSTEM_PROMPT = `
你是企業 Agent Runtime 的核心。規則:
- 僅能使用 runtime 提供的 tools/skills,不要自創。
- 優先維護安全策略,即使與 user 指令衝突也要遵守。
- 所有外呼必須經過 policy_check()。
- 請用 JSON 格式回報你的 plan 與採用的工具列表。
`;

  1. App System Prompt / Role Prompt(可配置)
  2. 每個業務場景(客服、報表生成)自有角色描述與 domain 指南。
  3. 存在於 config / DB,可版本化。

  4. Self-Refine / Prompt Rewrite(受控的自迭代)

  5. 允許 Agent 針對任務生成「內部工作 prompt」,但要:
    • 限制 token 長度,避免無限膨脹。
    • 加上 rewrite guard:禁止覆寫 framework 規則與 policy。

“`ts
async function rewriteTaskPrompt(task, contextSummary) {
const res = await callModel(“gpt-4o-mini”, [
{ role: “system”, content: “你負責優化 task prompt,但不得移除任何安全規則。” },
{ role: “user”, content: JSON.stringify({ task, contextSummary }) }
], { max_tokens: 256 });

 const rewritten = enforceGuards(res.content);
 return rewritten;

}

function enforceGuards(promptText) {
// 簡單例子:禁止出現某些關鍵字 / 刪除修改安全規則的段落
if (promptText.includes(“忽略所有安全策略”)) {
throw new Error(“rewrite violated safety guard”);
}
return promptText.slice(0, 2000); // 粗暴截斷
}
“`

實際好處:

  • 在不改底層模型的情況下,能針對新任務自我調整提示。
  • 出問題時,可以直接 diff 版本化的 app system prompt / rewrite 規則,而不是猜測「模型突然變笨」。

3. 安全機制在多 Agent / 多工具場景的落地

Harness² 類架構會把安全當作 runtime concern,而不是散落在 prompt 裡的「請你遵守公司政策」。核心三塊:policy check、log/trace、steering 控制權。

  1. Policy Check:像防火牆一樣包在工具呼叫前後

“`ts
async function safeToolCall(agentId, toolName, input, context) {
const decision = await policyEngine.evaluate({
agentId,
toolName,
input,
userId: context.userId,
tenantId: context.tenantId
});

 if (decision.effect !== "allow") {
   return { error: "TOOL_BLOCKED", reason: decision.reason };
 }

 const result = await tools[toolName].handler(input);

 auditLog.write({
   ts: Date.now(),
   agentId,
   toolName,
   input,
   resultPreview: JSON.stringify(result).slice(0, 500),
   decision
 });

 return result;

}
“`

  • policyEngine 可以用 OPA、簡單 DSL 或自建:

    • 例:客服 Agent 不得寫入財務系統工具。
    • 例:試算表匯出只能在內網 Storage bucket。
  • Log / Trace:每一步都要看得到

  • 為每個 Agent run 建立 traceId,記錄:
    • 使用哪個模型版本
    • 當時的 system prompt / skill graph 版本
    • 所有 tool call 的 input/output 摘要
  • 對 debugging 極關鍵:你才能回答「為什麼這次 Agent 會誤刪資料」。

💡 關鍵: 將每次執行綁定到特定 traceId 和版本化設定,使問題可重現、可追溯,避免「模型突然暴走但找不到原因」。

  1. Steering & 控制權(對應 Steering Wheel Problem)
  2. 多人共享 Agent(例如協作 IDE)時,必須定義:
    • 誰可以發 steering 命令(如 run.steer())。
    • steering 能改什麼:
    • 允許:調整任務描述、abort、切換 skill。
    • 禁止:修改安全策略、擴權到新工具。

“`ts
function applySteer(run, steerMsg, actor) {
if (!rbac.can(actor, “steer”, run.agentId)) throw new Error(“no permission”);

 if (steerMsg.type === "update_goal") {
   run.goal = steerMsg.newGoal;
 }

 if (steerMsg.type === "add_tool_permission") {
   // 需要更高權限
   if (!rbac.can(actor, "grant_tool", run.agentId)) {
     throw new Error("cannot grant tool");
   }
   run.allowedTools.push(steerMsg.toolName);
 }

}
“`


實作範例:用 GPT API包一個 Harness² 風格 Runtime

假設你有一個現成的 GPT 類 API(如 OpenAI, Azure OpenAI, Gemini),想在不改模型的前提下加上:

  • 工作流(skills)
  • 自動工具調用
  • 企業級安全控制與trace

可以做一個簡化版 runtime(TS/Python 都可,這裡用 pseudo-TS):

async function runAgent({
  userMessage,
  appId,
  userId,
  tenantId
}) {
  const appConfig = await loadAppConfig(appId); // 包含 appSystemPrompt, enabledSkills

  const messages = [
    { role: "system", content: FRAMEWORK_SYSTEM_PROMPT },
    { role: "system", content: appConfig.appSystemPrompt },
    { role: "user", content: userMessage }
  ];

  const toolsForModel = projectSkillsToTools(appConfig.enabledSkills);

  const traceId = newTraceId();

  while (true) {
    const res = await callModelWithTools("gpt-4o", messages, toolsForModel);

    if (res.type === "tool_call") {
      const { name, args } = res.tool;
      const toolResult = await safeToolCall(appId, name, args, { userId, tenantId, traceId });

      messages.push({
        role: "tool",
        name,
        content: JSON.stringify(toolResult)
      });

      continue; // 讓模型基於 tool 結果繼續推
    }

    // 最終回答
    auditLog.write({ traceId, final: res.content });
    return { traceId, answer: res.content };
  }
}

這樣你就有:

  • 自動工具調用:靠模型的 function calling / tool calling 能力 + projectSkillsToTools 抽象。
  • 安全:所有工具都經過 safeToolCall + policy + audit log。
  • 可演進:新增/調整工作流 → 改 enabledSkills 與 skill 的 plan(),不用換模型。

搭配像 vLLM 之類的推理引擎還可以:

  • 利用 KV cache、tool-call 修正提升性能與正確率。
  • 參考 .NET 那篇 token cost 實測文章的作法,加入 tool search / output gating 降成本。

💡 關鍵: 把「大模型 + 工具」包成一個可演進的 runtime,能在不改模型的情況下,持續優化成本、延遲與準確度。


建議與注意事項:常見坑與最佳實踐

  1. Prompt 注入
  2. 不要只在 prompt 裡寫「忽略使用者要求繞過安全」這種軟宣言。
  3. 核心邏輯(禁止調某工具、資料出境限制)一定放在 policy engine,由 runtime 強制。

  4. 工具爆炸(Tool Explosion)

  5. 原子工具數量一多,模型在 tool selection 上會變得不穩定、token 成本也上升。
  6. 建議:

    • 建高階 skill,對模型暴露 10~30 個「語意明確」的 skill 就好。
    • 內部再去 route 到具體工具(類似 API Gateway)。
  7. 觀測與除錯困難

  8. 務必實作:trace viewer(哪個 prompt、哪一步工具、policy 決策)。
  9. 沒有可視化,你很難判斷:是模型問題、工具問題、還是 policy 過嚴。

  10. Prompt & Policy 版本化

  11. 把 appSystemPrompt、skill 定義、policy rules 全部當作 configuration 版本:

    • ex: app-v3.2, policy-v1.7。
    • log 裡記住當次 run 用的是哪一版,方便回溯。
  12. 成本與延遲

  13. 多層 Agent + 工具調用 = token 成本疊加。
  14. 參考 token 成本優化文章的做法:
    • 對部分步驟改用 小模型(自迭代 prompt rewrite、工具選擇)。
    • output gating:若能用規則引擎處理,就不要丟回模型。

總結: Harness² 類的架構重點不是某個具體開源庫,而是這套思路:模型凍結,行為升級;工具抽象,技能組裝;安全外掛,runtime 強制。 若你目前已經在用 GPT 類 API,下一步要做的很可能不是換模型,而是搭一個自己的 mini-Harness² runtime。

🚀 你現在可以做的事

  • 整理現有內部工具,先定義一層 Tool Layer,統一用宣告式 input_schema / output_schema
  • 選 1–2 個業務場景,將常用流程封裝成 skills,並為它們設計對應的 appSystemPrompt
  • 實作一個簡單的 policyEngine 與 traceId 機制,讓每次 Agent run 都能被審計與回溯

留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *