Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions go/internal/agent/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestInterceptorRejectionMiddlewareSkipsRecoveryBudget(t *testing.T) {
t.Fatalf("拒绝未回灌结构化提示: %#v", payload)
}
// 策略拒绝不消耗修复预算,但必须留在终态门禁中,防止模型直接声称成功。
if !state.unresolved() || state.cumulativeRepairFailures != 0 || data["harness_recovery"] != nil {
if !state.unresolved() || data["harness_recovery"] != nil {
t.Fatalf("策略拒绝应阻止成功终态且不消耗预算: unresolved=%v state=%#v data=%#v", state.unresolved(), state, data)
}
if guardErr := (&Service{}).terminalReplyGuard(ctx, "unused"); guardErr == nil {
Expand Down Expand Up @@ -104,8 +104,7 @@ func TestInterceptorRejectionNotRetriedOnTransientText(t *testing.T) {
if err != nil || calls != 1 {
t.Fatalf("含 transient 词的策略拒绝也不得重试: calls=%d err=%v", calls, err)
}
if decodeRecoveryPayload(t, output.Result)["status"] != "failed" || state.unresolved() ||
state.cumulativeRepairFailures != 0 {
if decodeRecoveryPayload(t, output.Result)["status"] != "failed" || state.unresolved() {
t.Fatal("非确认类拒绝应回灌结构化提示、不触发重试且不污染确认门禁")
}
}
Expand Down
2 changes: 0 additions & 2 deletions go/internal/agent/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ var (
// 工具修复预算穷尽分因(H4):streak = 连续失败链超限;cumulative = 连击预算被成功清零、
// 但 turn 级累计预算仍超限——正是「交替 fail→success 想刷新预算却被累计计数挡住」的信号
// (H-B P2「预算重叠」)。
metricRecoveryStreakExhausted = telemetry.NewCounter("agent_recovery_streak_exhausted_total")
metricRecoveryCumulativeExhausted = telemetry.NewCounter("agent_recovery_cumulative_exhausted_total")

// 工作区用户记忆注入规模(M6/M8):三个累计条数、实际 section rune 分布与发生
// 截断的构建次数。omitted ratio 由累计 omitted/total 派生,供容量继续校准。
Expand Down
2 changes: 1 addition & 1 deletion go/internal/agent/preview_qa_atomic_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func TestDynamicPreviewQAReActReturnsToMutationAndRefreshesTimeline(t *testing.T
if len(clips) != 1 || clips[0].SourceStartFrame != 15 || clips[0].SourceEndFrame != 60 {
t.Fatalf("QA 修正未精确提交: %#v", clips)
}
if recoveryState.unresolved() || recoveryState.recoveryExhausted() {
if recoveryState.unresolved() {
t.Fatalf("preview QA 后仍有未解决 recovery: %s", recoveryState.summary())
}
}
Expand Down
2 changes: 1 addition & 1 deletion go/internal/agent/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ WorldState.user_memory 是跨草稿的用户长期偏好、习惯与纠正;与
1. 只通过已注册能力完成理解、编辑、验证、预览和质检;不得编造文件、素材、时间线、任务或产物。最终导出和下载不属于你的工具能力,只能由用户在编辑器 UI 中明确点击触发。
2. 目标明确就直接执行。镜头取舍、气口与重复处理、B-roll、节奏、字幕、转场、调色和 BGM 等可逆创作细节由你结合证据自主决定,先交付结果,再接受用户增量反馈或 Rewind;不得把首剪方案、EDL 或参数清单交给用户逐项审批。用户只给出宽泛剪辑请求但已有可用素材时,结合素材证据、user_memory 和安全默认值先做可回滚首剪;未指定成片类型、时长或风格本身不构成阻塞。只有缺少会让成片目标产生实质冲突、且无法从素材、上下文或安全默认值推断的关键信息时,才可用 decision_type=critical 的 interaction.ask_user,问题必须只聚焦一个核心分歧。破坏性或外部影响动作改用 interaction.confirm_action。
3. 每次工具调用的 arguments 必须是一个完整、合法且没有尾随字符的 JSON 对象。精确时间坐标统一使用整数帧;编辑操作必须是带 kind 的扁平对象,禁止自行换算或传递秒字段。
4. 失败后先读 observation、recovery 和已给出的最新状态,再调整参数或补取证据;不可原样重试,也不可删除用户要求来绕过错误。若结果明确业务状态未变化且已给出有效替代参数,直接只重试失败原语,不重复读取未变状态或重跑已成功原语;只有目标已陈旧、依赖新 ID/坐标或状态可能变化时才补读。恢复预算耗尽时停止调用,并如实说明已完成、未完成及下一步。
4. 每次工具失败只属于本次调用。先读 error_code、message、invalid_fields、current_state 和 recovery,再修正参数或补取证据并直接重试;之前成功的原语不受影响,无关工具也可继续执行。不要删除用户要求来绕过错误。
5. 浏览器编辑代理负责即时预览,普通移动、裁剪和分割不触发离线渲染。只有用户明确需要可分享预览或离线画质检查时才生成预览并质检。用户要求最终导出或下载时,不得调用预览代替、不得创建或轮询 final job;完成必要编辑与校验后,只引导用户在编辑器 UI 的导出区域选择规格并点击“导出视频”。
6. 用户反馈可以推翻旧的节奏或镜头结论。应从当前状态和本轮证据继续,不复用已过期判断;除非用户明确要求,不从头重做,也不删除已有素材、时间线或已完成理解。
7. 最终回复只呈现结论与已完成的事实。禁止把「但等等」「让我再确认」「不对,重新想」这类自我怀疑、中途推翻或二次确认的过程性语句写进正式回复;验证性思考在内部完成,不外泄给用户。`
Expand Down
4 changes: 2 additions & 2 deletions go/internal/agent/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func TestCoreSystemPromptStaysSmallAndContainsNoIncidentExamples(t *testing.T) {
fragments []string
}{
"core": {coreSystemPrompt, []string{
"唯一客观事实", "目标明确就直接执行", "完整、合法且没有尾随字符的 JSON 对象", "整数帧", "不可原样重试",
"业务状态未变化", "只重试失败原语", "不重复读取未变状态",
"唯一客观事实", "目标明确就直接执行", "完整、合法且没有尾随字符的 JSON 对象", "整数帧",
"每次工具失败只属于本次调用", "invalid_fields", "之前成功的原语不受影响", "无关工具也可继续执行",
"即时预览", "用户反馈可以推翻旧的节奏或镜头结论",
"draft.content_plan", "plan.update", "RFC 7396", "不是日志或转写",
"WorldState.user_memory", "memory.set", "memory.remove", "本回合为准", "一次性要求不要入库",
Expand Down
13 changes: 5 additions & 8 deletions go/internal/agent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,9 @@ func (service *Service) runTurn(ctx context.Context, item QueueItem) error {
if errors.Is(err, context.Canceled) || ctx.Err() != nil {
return finishCancelled(err)
}
if recoveryState.recoveryExhausted() {
err = &terminalReplyGuardError{kind: "recovery_exhausted", details: recoveryState.summary()}
content = ""
} else if guardErr := service.terminalReplyGuard(ctx, item.DraftID); guardErr != nil {
// 即便 provider 在成功写入或工具失败之后又返回普通错误,也必须优先暴露
// harness 已记录的真实终态,不能让错误路径绕过同版本检查或未解决失败门禁。
if guardErr := service.terminalReplyGuard(ctx, item.DraftID); guardErr != nil {
// 即便 provider 在成功写入或工具调用之后又返回普通错误,也必须优先暴露
// harness 已记录的真实终态,不能让错误路径绕过同版本检查或待确认策略门禁。
err = guardErr
content = ""
}
Expand Down Expand Up @@ -990,7 +987,7 @@ func requireConfirmedToolSuccess(
result, ok := terminalTruthToolResult(output)
if !ok {
return rushestools.ToolResult{}, &terminalReplyGuardError{
kind: "tool_failure_unresolved", details: "确认重放的 " + name + " 未返回有效 ToolResult",
kind: "tool_policy_unresolved", details: "确认重放的 " + name + " 未返回有效 ToolResult",
}
}
// 该路径直接同步调用 Service.ExecuteTool,返回值与当前 typed input 同栈绑定;
Expand All @@ -1001,7 +998,7 @@ func requireConfirmedToolSuccess(
details = "status=" + result.Status
}
return rushestools.ToolResult{}, &terminalReplyGuardError{
kind: "tool_failure_unresolved", details: "确认重放的 " + name + " 失败:" + details,
kind: "tool_policy_unresolved", details: "确认重放的 " + name + " 失败:" + details,
}
}
return result, nil
Expand Down
49 changes: 25 additions & 24 deletions go/internal/agent/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type terminatingFailureLoopModel struct {
terminalErr bool
}

const repeatedFailureCallsBeforeTerminal = 6

type failingReadToolServiceModel struct {
mu sync.Mutex
calls int
Expand Down Expand Up @@ -225,7 +227,7 @@ func (modelValue *terminatingFailureLoopModel) Generate(
modelValue.mu.Lock()
defer modelValue.mu.Unlock()
modelValue.calls++
if modelValue.calls <= maxModelRepairAttempts+1 {
if modelValue.calls <= repeatedFailureCallsBeforeTerminal {
return schema.AssistantMessage("", []schema.ToolCall{{
ID: agentexec.RandomID("bounded_loop_call"),
Function: schema.FunctionCall{
Expand All @@ -234,7 +236,7 @@ func (modelValue *terminatingFailureLoopModel) Generate(
}}), nil
}
if modelValue.terminalErr {
return nil, errors.New("provider stream failed after recovery exhaustion")
return nil, errors.New("provider stream failed after tool failures")
}
return schema.AssistantMessage("已经全部完成。", nil), nil
}
Expand Down Expand Up @@ -458,7 +460,7 @@ func (modelValue *failingReadToolServiceModel) Generate(
!strings.Contains(messages[len(messages)-1].Content, `"retryable":false`) {
return nil, errors.New("确定性参数失败没有立即回灌模型")
}
return schema.AssistantMessage("已经全部完成。", nil), nil
return schema.AssistantMessage("拍点分析失败;本轮未修改时间线,你可以更换素材后重试。", nil), nil
}

func (modelValue *failingReadToolServiceModel) Stream(
Expand Down Expand Up @@ -685,7 +687,7 @@ func TestServiceReturnsToolFailureToModelForSelfRepair(t *testing.T) {
}
}

func TestServiceRejectsSuccessClaimAfterUnresolvedToolFailure(t *testing.T) {
func TestServiceAllowsHonestTerminalReplyAfterIndependentToolFailure(t *testing.T) {
t.Parallel()
database := agenttest.AgentTestDatabase(t)
agenttest.CreateAgentDraft(t, database, "draft_retry_trace")
Expand Down Expand Up @@ -722,9 +724,9 @@ func TestServiceRejectsSuccessClaimAfterUnresolvedToolFailure(t *testing.T) {
}
}
final := messages[len(messages)-1]
if toolRows != 1 || len(messages) != 3 || final.Role != "system" || final.Kind != "turn_failure" ||
!strings.Contains(final.Content, "工具调用仍处于失败状态") || strings.Contains(final.Content, "已经全部完成") {
t.Fatalf("未解决工具失败必须拒绝伪成功:tool_rows=%d messages=%#v", toolRows, messages)
if toolRows != 1 || len(messages) != 3 || final.Role != "assistant" || final.Kind != "reply" ||
final.Content != "拍点分析失败;本轮未修改时间线,你可以更换素材后重试。" {
t.Fatalf("历史失败不应阻止诚实终态:tool_rows=%d messages=%#v", toolRows, messages)
}
}

Expand Down Expand Up @@ -1534,7 +1536,7 @@ func TestFallbackMainlineDecisionReplayStatusAndPreviewInspection(t *testing.T)
},
})
var replayGuardErr *terminalReplyGuardError
if replayed != "" || !errors.As(err, &replayGuardErr) || replayGuardErr.kind != "tool_failure_unresolved" {
if replayed != "" || !errors.As(err, &replayGuardErr) || replayGuardErr.kind != "tool_policy_unresolved" {
t.Fatalf("replayed=%q err=%v", replayed, err)
}
if cancelled, err := service.replayPendingTool(ctx, QueueItem{DraftID: "draft_full", Payload: map[string]any{
Expand Down Expand Up @@ -2115,7 +2117,7 @@ func TestEmptyModelReplyStillProducesVisibleFailure(t *testing.T) {
}
}

func TestExhaustedRecoveryReplyIsVisibleAndMarkedFailed(t *testing.T) {
func TestRepeatedIdenticalFailuresCanReachAnHonestTerminalReply(t *testing.T) {
t.Parallel()
database := agenttest.AgentTestDatabase(t)
agenttest.CreateAgentDraft(t, database, "draft_bounded_recovery")
Expand All @@ -2136,12 +2138,11 @@ func TestExhaustedRecoveryReplyIsVisibleAndMarkedFailed(t *testing.T) {
switch event["type"] {
case "message_completed":
completed, _ = event["content"].(string)
if event["kind"] != "turn_failure" {
t.Fatalf("恢复耗尽必须由 harness 输出确定性失败:%#v", event)
if event["kind"] != "reply" {
t.Fatalf("相同失败后的模型终态应如实透传:%#v", event)
}
case "turn_ended":
if event["outcome"] != "failed" || !strings.Contains(completed, "工具自修复次数已经用尽") ||
strings.Contains(completed, "已经全部完成") {
if event["outcome"] != "finished" || completed != "已经全部完成。" {
t.Fatalf("completed=%q event=%#v", completed, event)
}
messages, listErr := storage.ListMessages(t.Context(), database.Read(), "draft_bounded_recovery", 20)
Expand All @@ -2154,22 +2155,22 @@ func TestExhaustedRecoveryReplyIsVisibleAndMarkedFailed(t *testing.T) {
toolRows++
}
}
if toolRows != 1 {
t.Fatalf("重复失败不应污染 UI:tool_rows=%d messages=%#v", toolRows, messages)
if toolRows != repeatedFailureCallsBeforeTerminal {
t.Fatalf("每次相同失败都应有独立 trace:tool_rows=%d messages=%#v", toolRows, messages)
}
reply := messages[len(messages)-1]
if reply.Role != "system" || reply.Kind != "turn_failure" || reply.Content != completed {
t.Fatalf("恢复耗尽的确定性失败应落 system/turn_failure:%#v", reply)
if reply.Role != "assistant" || reply.Kind != "reply" || reply.Content != completed {
t.Fatalf("诚实终态应落 assistant/reply:%#v", reply)
}
return
}
case <-time.After(3 * time.Second):
t.Fatal("恢复预算耗尽后没有可见终态")
t.Fatal("重复失败后没有可见终态")
}
}
}

func TestExhaustedRecoveryOverridesProviderErrorWithoutModelFallback(t *testing.T) {
func TestProviderErrorAfterIndependentFailuresRemainsObservable(t *testing.T) {
t.Parallel()
database := agenttest.AgentTestDatabase(t)
const draftID = "draft_bounded_recovery_provider_error"
Expand All @@ -2189,14 +2190,14 @@ func TestExhaustedRecoveryOverridesProviderErrorWithoutModelFallback(t *testing.
t.Fatal(err)
}
final := messages[len(messages)-1]
if final.Kind != "turn_failure" || !strings.Contains(final.Content, "工具自修复次数已经用尽") ||
strings.Contains(final.Content, "provider stream failed") {
t.Fatalf("恢复耗尽应覆盖 provider 错误并使用固定正文:%#v", final)
if final.Kind != "turn_failure" || !strings.Contains(final.Content, "provider stream failed") ||
strings.Contains(final.Content, "工具自修复次数已经用尽") {
t.Fatalf("provider 错误不应被历史工具失败覆盖:%#v", final)
}
modelValue.mu.Lock()
calls := modelValue.calls
modelValue.mu.Unlock()
if calls != maxModelRepairAttempts+2 {
if calls != repeatedFailureCallsBeforeTerminal+1 {
t.Fatalf("恢复耗尽后不得为失败收尾再次调用模型:calls=%d", calls)
}
}
Expand Down Expand Up @@ -2226,7 +2227,7 @@ func TestRepeatedFailedToolLoopStillRepliesAndEndsTurn(t *testing.T) {
completed, _ = event["content"].(string)
case "turn_ended":
if event["outcome"] != "failed" || !strings.Contains(completed, "本轮没有完成") ||
!strings.Contains(completed, "模型修复失败次数") {
!strings.Contains(completed, "exceeds max steps") {
t.Fatalf("completed=%q event=%#v", completed, event)
}
return
Expand Down
8 changes: 3 additions & 5 deletions go/internal/agent/terminal_truth.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ type terminalReplyGuardError struct {

func (guardErr *terminalReplyGuardError) Error() string {
switch guardErr.kind {
case "recovery_exhausted":
return "工具自修复次数已用尽:" + guardErr.details
case "tool_failure_unresolved":
return "工具失败尚未解决:" + guardErr.details
case "tool_policy_unresolved":
return "工具策略确认尚未解决:" + guardErr.details
case "timeline_check_missing":
return fmt.Sprintf("最新编辑 %s 尚未通过同版本 timeline.check", guardErr.mutationTimelineID)
case "timeline_check_stale":
Expand Down Expand Up @@ -166,7 +164,7 @@ func terminalTruthToolResult(output any) (rushestools.ToolResult, bool) {
func (service *Service) terminalReplyGuard(ctx context.Context, draftID string) error {
if recovery := toolRecoveryFromContext(ctx); recovery != nil && recovery.unresolved() {
return &terminalReplyGuardError{
kind: "tool_failure_unresolved", details: recovery.summary(),
kind: "tool_policy_unresolved", details: recovery.summary(),
}
}
snapshot := terminalTimelineTruthFromContext(ctx).snapshot()
Expand Down
4 changes: 2 additions & 2 deletions go/internal/agent/terminal_truth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,11 @@ func TestConfirmedToolRejectsStructuredAndMalformedFailures(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
_, err := requireConfirmedToolSuccess(test.tool, test.arguments, test.output, "draft")
var guardErr *terminalReplyGuardError
if !errors.As(err, &guardErr) || guardErr.kind != "tool_failure_unresolved" {
if !errors.As(err, &guardErr) || guardErr.kind != "tool_policy_unresolved" {
t.Fatalf("err=%#v", err)
}
failure := terminalFailureReply(t.Context(), err)
if !strings.Contains(failure, "本轮没有完成") || !strings.Contains(failure, "拒绝未验收的成功声明") {
if !strings.Contains(failure, "本轮没有完成") || !strings.Contains(failure, "拒绝越过确认直接收尾") {
t.Fatalf("failure=%q", failure)
}
})
Expand Down
Loading
Loading