feat(tasks): add inherited context and resilient LLM execution
Add read-only task context inheritance, ordered task-level LLM failover, live Worker controls, cascading task cleanup, FIFO task concurrency, cancellation diagnostics, UI updates, tests, build script, and changelog.
This commit is contained in:
1 parent
7090b034f8
commit
9793b522d9
82 files changed
+11094
-1712
No files matched your search
@@ -12,8 +12,6 @@ import (
|
||||
"github.com/Autumn-27/norma/llm"
|
||||
)
|
||||
|
||||
// every TerminalReason the harness can emit must have an explanation — otherwise
|
||||
// a run silently falls back to "未知终态", which is the bug this file exists to fix.
|
||||
func TestReasonHintCoversEveryTerminalReason(t *testing.T) {
|
||||
all := []harness.TerminalReason{
|
||||
harness.ReasonCompleted, harness.ReasonBlockingLimit, harness.ReasonImageError,
|
||||
@@ -21,152 +19,151 @@ func TestReasonHintCoversEveryTerminalReason(t *testing.T) {
|
||||
harness.ReasonPromptTooLong, harness.ReasonStopHookPrevented, harness.ReasonHookStopped,
|
||||
harness.ReasonMaxTurns, harness.ReasonTimeout,
|
||||
}
|
||||
for _, r := range all {
|
||||
if strings.TrimSpace(reasonHint[r]) == "" {
|
||||
t.Errorf("terminal reason %q 没有中文说明", r)
|
||||
for _, reason := range all {
|
||||
if strings.TrimSpace(reasonHint[reason]) == "" {
|
||||
t.Errorf("terminal reason %q has no explanation", reason)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the cause attached at cancel time must survive the ctx chain the engine builds:
|
||||
// task exec ctx -> per-work child -> value ctx -> the run's hard-timeout backstop.
|
||||
func TestAbortCausePropagatesThroughRunContextChain(t *testing.T) {
|
||||
type key struct{}
|
||||
exec, cancelExec := context.WithCancelCause(context.Background())
|
||||
work, cancelWork := context.WithCancelCause(exec)
|
||||
execCtx, cancelExec := context.WithCancelCause(context.Background())
|
||||
workCtx, cancelWork := context.WithCancelCause(execCtx)
|
||||
defer cancelWork(nil)
|
||||
valued := context.WithValue(work, key{}, "task-1")
|
||||
run, cancelRun := context.WithTimeoutCause(valued, time.Hour, AbortRunHardTimeout)
|
||||
valued := context.WithValue(workCtx, key{}, "task-1")
|
||||
runCtx, cancelRun := context.WithTimeoutCause(valued, time.Hour, AbortRunHardTimeout)
|
||||
defer cancelRun()
|
||||
|
||||
cancelExec(AbortPausedByUser)
|
||||
|
||||
code, _, text, ok := AbortReason(run)
|
||||
code, _, text, ok := AbortReason(runCtx)
|
||||
if !ok || code != "paused_by_user" {
|
||||
t.Fatalf("code=%q ok=%v,期望 paused_by_user", code, ok)
|
||||
t.Fatalf("code=%q ok=%v, want paused_by_user", code, ok)
|
||||
}
|
||||
if !strings.Contains(text, "退回 frontier") {
|
||||
t.Errorf("原因文案没传下来: %q", text)
|
||||
if !strings.Contains(text, "frontier") {
|
||||
t.Fatalf("cause detail did not propagate: %q", text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbortReasonFallbacks(t *testing.T) {
|
||||
if _, _, _, ok := AbortReason(context.Background()); ok {
|
||||
t.Error("未取消的 ctx 不应报出中断原因")
|
||||
t.Fatal("live context must not report an abort reason")
|
||||
}
|
||||
// a cancel site that forgot to attach a cause must be called out, not guessed at
|
||||
plain, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
code, _, _, ok := AbortReason(plain)
|
||||
if !ok || code != "canceled_no_cause" {
|
||||
t.Errorf("code=%q ok=%v,期望 canceled_no_cause", code, ok)
|
||||
if code, _, _, ok := AbortReason(plain); !ok || code != "canceled_no_cause" {
|
||||
t.Fatalf("code=%q ok=%v, want canceled_no_cause", code, ok)
|
||||
}
|
||||
|
||||
timed, cancelT := context.WithTimeout(context.Background(), time.Nanosecond)
|
||||
defer cancelT()
|
||||
timed, cancelTimed := context.WithTimeout(context.Background(), time.Nanosecond)
|
||||
defer cancelTimed()
|
||||
<-timed.Done()
|
||||
if code, _, _, _ := AbortReason(timed); code != "deadline_exceeded" {
|
||||
t.Errorf("code=%q,期望 deadline_exceeded", code)
|
||||
t.Fatalf("code=%q, want deadline_exceeded", code)
|
||||
}
|
||||
}
|
||||
|
||||
// an aborted run's one-line summary must name the actual cause, and the detail
|
||||
// must pin down the tool that never came back.
|
||||
func TestTerminalTextAbortedNamesCauseAndHangingTool(t *testing.T) {
|
||||
ctx, cancel := context.WithCancelCause(context.Background())
|
||||
cancel(AbortKilledByPlanner)
|
||||
|
||||
tr := &runTrace{startedAt: time.Now().Add(-90 * time.Second)}
|
||||
tr.start("tu_1", "Bash", `{"command":"nmap -p- 10.0.0.1"}`)
|
||||
|
||||
trace := &runTrace{startedAt: time.Now().Add(-90 * time.Second)}
|
||||
trace.start("tu_1", "Bash", `{"command":"nmap -p- 10.0.0.1"}`)
|
||||
term := &harness.Terminal{
|
||||
Reason: harness.ReasonAbortedTools,
|
||||
Err: context.Canceled,
|
||||
Turns: 7,
|
||||
Usage: llm.Usage{InputTokens: 1200, OutputTokens: 340},
|
||||
}
|
||||
sum, detail := terminalText(ctx, term, tr)
|
||||
|
||||
if !strings.Contains(sum, "kill_work") {
|
||||
t.Errorf("摘要没点出取消方: %q", sum)
|
||||
}
|
||||
if strings.Contains(sum, "\n") {
|
||||
t.Errorf("摘要必须是单行(前端按首行截断): %q", sum)
|
||||
summary, detail := terminalText(ctx, term, trace)
|
||||
if !strings.Contains(summary, "规划者") || strings.Contains(summary, "\n") {
|
||||
t.Fatalf("unexpected summary: %q", summary)
|
||||
}
|
||||
for _, want := range []string{"killed_by_planner", "aborted_tools", "7 轮", "Bash", "未返回结果", "1200"} {
|
||||
if !strings.Contains(detail, want) {
|
||||
t.Errorf("详情缺少 %q:\n%s", want, detail)
|
||||
t.Errorf("detail missing %q:\n%s", want, detail)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// a tool that DID return must not be reported as the hang site.
|
||||
func TestTerminalTextDirectContextCancellationKeepsCause(t *testing.T) {
|
||||
ctx, cancel := context.WithCancelCause(context.Background())
|
||||
cancel(AbortChatStoppedByUser)
|
||||
summary, detail := terminalText(ctx, &harness.Terminal{Err: context.Canceled}, &runTrace{startedAt: time.Now()})
|
||||
if !strings.Contains(summary, "用户停止") || !strings.Contains(detail, "chat_stopped_by_user") {
|
||||
t.Fatalf("direct context cancellation lost cause: %q\n%s", summary, detail)
|
||||
}
|
||||
if !strings.Contains(detail, "context_canceled") {
|
||||
t.Fatalf("missing synthesized terminal label: %s", detail)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTerminalTextAbortedPreservesPartialOutput(t *testing.T) {
|
||||
ctx, cancel := context.WithCancelCause(context.Background())
|
||||
cancel(AbortPausedByUser)
|
||||
summary, detail := terminalText(ctx, &harness.Terminal{
|
||||
Reason: harness.ReasonAbortedStreaming,
|
||||
Text: "已经生成的半段回答",
|
||||
}, &runTrace{startedAt: time.Now()})
|
||||
if !strings.Contains(summary, "用户暂停") {
|
||||
t.Fatalf("abort summary lost cause: %q", summary)
|
||||
}
|
||||
if !strings.Contains(detail, "已经生成的半段回答") {
|
||||
t.Fatalf("abort detail lost partial output: %s", detail)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTerminalTextCompletedToolNotBlamed(t *testing.T) {
|
||||
ctx, cancel := context.WithCancelCause(context.Background())
|
||||
cancel(AbortShutdown)
|
||||
tr := &runTrace{startedAt: time.Now()}
|
||||
tr.start("tu_1", "Read", `{"path":"/etc/hosts"}`)
|
||||
tr.done("tu_1")
|
||||
|
||||
_, detail := terminalText(ctx, &harness.Terminal{Reason: harness.ReasonAbortedStreaming}, tr)
|
||||
if strings.Contains(detail, "未返回结果") {
|
||||
t.Errorf("已返回的工具被误报为卡住:\n%s", detail)
|
||||
}
|
||||
if !strings.Contains(detail, "已正常返回") {
|
||||
t.Errorf("详情没记录最后一个工具:\n%s", detail)
|
||||
trace := &runTrace{startedAt: time.Now()}
|
||||
trace.start("tu_1", "Read", `{"path":"/etc/hosts"}`)
|
||||
trace.done("tu_1")
|
||||
_, detail := terminalText(ctx, &harness.Terminal{Reason: harness.ReasonAbortedStreaming}, trace)
|
||||
if strings.Contains(detail, "未返回结果") || !strings.Contains(detail, "已正常返回") {
|
||||
t.Fatalf("completed tool was blamed:\n%s", detail)
|
||||
}
|
||||
}
|
||||
|
||||
// non-abort terminals keep their own explanation instead of the interruption text.
|
||||
func TestTerminalTextNonAbortReasons(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tr := &runTrace{startedAt: time.Now()}
|
||||
|
||||
sum, _ := terminalText(ctx, &harness.Terminal{Reason: harness.ReasonMaxTurns}, tr)
|
||||
if !strings.Contains(sum, "达运行预算上限") || strings.Contains(sum, "中断") {
|
||||
t.Errorf("max_turns 摘要不对: %q", sum)
|
||||
trace := &runTrace{startedAt: time.Now()}
|
||||
summary, _ := terminalText(context.Background(), &harness.Terminal{Reason: harness.ReasonMaxTurns}, trace)
|
||||
if !strings.Contains(summary, "运行预算上限") || strings.Contains(summary, "中断") {
|
||||
t.Fatalf("unexpected max_turns summary: %q", summary)
|
||||
}
|
||||
|
||||
sum, detail := terminalText(ctx, &harness.Terminal{
|
||||
Reason: harness.ReasonModelError, Err: errors.New("429 rate limited")}, tr)
|
||||
if !strings.Contains(sum, "model_error") {
|
||||
t.Errorf("model_error 摘要不对: %q", sum)
|
||||
}
|
||||
if !strings.Contains(detail, "429 rate limited") {
|
||||
t.Errorf("详情没带上底层错误:\n%s", detail)
|
||||
summary, detail := terminalText(context.Background(), &harness.Terminal{
|
||||
Reason: harness.ReasonModelError, Err: errors.New("429 rate limited"),
|
||||
}, trace)
|
||||
if !strings.Contains(summary, "model_error") || !strings.Contains(detail, "429 rate limited") {
|
||||
t.Fatalf("model_error detail incomplete: %q\n%s", summary, detail)
|
||||
}
|
||||
}
|
||||
|
||||
// every enumerated cause must carry both a compact headline (for the collapsed
|
||||
// row) and a full explanation (for the expanded detail).
|
||||
func TestAbortCausesAreWellFormed(t *testing.T) {
|
||||
all := []*AbortCause{
|
||||
AbortPausedByUser, AbortPausedByOrchestrator, AbortTaskDeleted, AbortPausedOnReload,
|
||||
AbortGoalMet, AbortSettleDrainTimeout, AbortKilledByPlanner, AbortWorkFinished,
|
||||
AbortPausedRaceGuard, AbortChatStoppedByUser, AbortChatTurnFinished,
|
||||
AbortGoalMet, AbortSettleDrainTimeout, AbortKilledByPlanner, AbortWorkPausedByUser,
|
||||
AbortWorkCancelledByUser, AbortWorkFinished, AbortPausedRaceGuard,
|
||||
AbortChatStoppedByUser, AbortChatPausedWithTask, AbortChatTurnFinished,
|
||||
AbortShutdown, AbortRunHardTimeout,
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, c := range all {
|
||||
for _, abort := range all {
|
||||
switch {
|
||||
case c.Code == "" || seen[c.Code]:
|
||||
t.Errorf("code 缺失或重复: %q", c.Code)
|
||||
case c.Short == "" || utf8.RuneCountInString(c.Short) > 40:
|
||||
t.Errorf("%s: Short 必须存在且 ≤40 字(进折叠摘要行): %q", c.Code, c.Short)
|
||||
case utf8.RuneCountInString(c.Text) <= utf8.RuneCountInString(c.Short):
|
||||
t.Errorf("%s: Text 应比 Short 更完整", c.Code)
|
||||
case abort.Code == "" || seen[abort.Code]:
|
||||
t.Errorf("missing or duplicate code: %q", abort.Code)
|
||||
case abort.Short == "" || utf8.RuneCountInString(abort.Short) > 40:
|
||||
t.Errorf("%s has invalid short text: %q", abort.Code, abort.Short)
|
||||
case utf8.RuneCountInString(abort.Text) <= utf8.RuneCountInString(abort.Short):
|
||||
t.Errorf("%s detail must be longer than short text", abort.Code)
|
||||
}
|
||||
seen[c.Code] = true
|
||||
seen[abort.Code] = true
|
||||
}
|
||||
}
|
||||
|
||||
// summaries are mostly 中文; capping by bytes used to slice a rune in half.
|
||||
func TestFirstLineCapsByRuneNotByte(t *testing.T) {
|
||||
got := firstLine(strings.Repeat("恢复", 10), 5)
|
||||
if got != "恢复恢复恢…" {
|
||||
t.Errorf("firstLine 按字节截断了: %q", got)
|
||||
if got := firstLine(strings.Repeat("恢复", 10), 5); got != "恢复恢复恢…" {
|
||||
t.Fatalf("firstLine split a Unicode character: %q", got)
|
||||
}
|
||||
if got := firstLine("头\n尾", 100); got != "头" {
|
||||
t.Errorf("多行没截到首行: %q", got)
|
||||
t.Fatalf("firstLine did not stop at newline: %q", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user