feat(cloud-agent): 单步输出上限与秒级墙钟,超时/空输出可自恢复

画布 Agent 一轮要跑很多步模型调用,此前只有"按任务类型的分钟级超时":某一步卡住只能等
整轮判死;更根本的是每一步请求根本不带输出上限,不写 max_tokens 时上游按剩余上下文放行,
思考模型可以把单步拖到几分钟,且偶发把整个输出预算花在推理上、正文与工具调用皆空。

- 新增运行时策略 task.agentStepTimeoutSeconds 与 task.agentStepMaxOutputTokens:秒级墙钟
  与单步输出上限,都支持环境变量给部署级默认值、管理端热更新;旧配置 JSON 缺字段按默认值
  回填,无需数据迁移。
- 单步墙钟到点中止这一步时不再把整轮判死:以可识别标记记为该步失败,运行期关掉上游思考
  重试同一步一次(model_failure_recovered / step_timeout_retried),重试仍超时才以可读原因
  (model_step_timeout)结束本轮。
- 空输出催办用尽后新增一级升级:关思考 + 放大输出预算重试同一步
  (model_failure_recovered / empty_output_escalated),而不是直接判死。放大档在原值上翻倍
  并以 131072 封顶,策略值为 0 时用 32768 兜底,让重试仍然有界。
- 输出上限真正下发到上游:textOptions.maxOutputTokens 成为任务信封字段,Agent 的两条请求
  路径(内置协议与声明式协议)组装请求体时写入 max_tokens / max_output_tokens,与既有的
  input.MaxOutputTokens 取较小值,避免策略上限超过模型声明的物理上限。
- task_worker:执行时限改用带画布 Agent 分支的 taskExecutionTimeout;执行时限到点本身不再
  被当成"租约丢失",否则任务会停在 running 等租约过期后被反复重跑(实测一次调用被重跑成
  三次上游请求)。续租仍用上游已合入的"不继承父 context 取消"的独立 context。
This commit is contained in:
AetherNo2332 committed 2026-09-22 01:20:36 +08:00
1 parent 0b285f66e8
commit 9f37065e47
11 files changed
+609 -11

No files matched your search

+35
View File
@@ -58,6 +58,7 @@ func runAgentToolTask(ctx context.Context, input canvasGenerationInput) (map[str
}
body["model"] = input.Config.Model
applyTextThinking(body, input, protocol)
applyAgentOutputLimit(body, agentStepOutputLimit(input), protocol)
normalizeAgentToolChoice(body, input, protocol)
result, err := postAgentRequest(ctx, input, path, body, protocol)
if protocol == "chat-completion" && isAgentToolChoiceCompatibilityError(err) {
@@ -90,6 +91,39 @@ func postAgentRequest(ctx context.Context, input canvasGenerationInput, path str
return parseAgentToolPayload(payload, protocol)
}
// agentStepOutputLimit 取本次 Agent 调用的输出上限。
//
// 两个来源语义相同但通道不同:textOptions.maxOutputTokens 是任务信封里下发的策略值
// (画布 Agent 每步按运行时策略给,persist 在任务输入里,重启后仍在);input.MaxOutputTokens
// 是进程内直传的旧入口(渠道模型能力声明)。都为 0 时不写上限字段,交给上游按剩余上下文放行。
// 都非零时取较小值:策略上限不该超过模型自己声明的物理上限。
func agentStepOutputLimit(input canvasGenerationInput) int {
limits := []int{input.TextOptions.MaxOutputTokens, input.MaxOutputTokens}
limit := 0
for _, candidate := range limits {
if candidate <= 0 {
continue
}
if limit == 0 || candidate < limit {
limit = candidate
}
}
return limit
}
// applyAgentOutputLimit 按协议写入输出上限字段名:Claude 与 Chat Completions 用 max_tokens,
// Responses 用 max_output_tokens。
func applyAgentOutputLimit(body map[string]interface{}, limit int, protocol string) {
if limit <= 0 {
return
}
field := "max_tokens"
if protocol == "responses" {
field = "max_output_tokens"
}
applyTextOutputLimit(body, limit, field)
}
func runDeclarativeAgentTask(ctx context.Context, input canvasGenerationInput, adapter protocol.AgentAdapter) (map[string]interface{}, error) {
wire := input.Config.InterfaceType
if wire == string(model.ChannelInterfaceOpenAIResponse) {
@@ -118,6 +152,7 @@ func runDeclarativeAgentTask(ctx context.Context, input canvasGenerationInput, a
return nil, errors.New("声明式 Agent 请求体必须是 JSON 对象")
}
applyTextThinking(body, input, wire)
applyAgentOutputLimit(body, agentStepOutputLimit(input), wire)
normalizeAgentToolChoice(body, input, wire)
spec.Body = body
if input.StreamText {