mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-30 22:42:51 +00:00
用户指正:存在**中断被中断**的场景,所以被打断的现场要进**中断栈**。 我此前把 suspendPool 明确写成“不是栈、按优先级取”,是错的。 改动: - suspendPool 改名 suspendStack,恢复纪律改为**严格 LIFO(只比栈顶)**; 栈内不做优先级重排——嵌套抢占天然使栈自底向上基础级递增, 且“后被打断的先恢复”才是栈语义。取出即弹栈。 - 修掉一个由此暴露的真 bug(抢占空转):一次抢占生效后,被挂起的原任务 会因饥饿防护提升有效级,与抢占者同级;此时若按“先到先服务”,原任务 (入队更早)会被立刻选回,抢占者永远排不到 —— 抢占等于没发生。 现在**同级时 pendingInterrupts 优先于其它两类**,保证抢占必然生效。 - 状态 DTO:SuspendPool/suspend_pool → SuspendStack/suspend_stack - 设计稿:§2 用语更正(它**就是**中断栈)、§4.1 选择函数(候选只含栈顶 + pending 同级优先,并说明为何必需)、§6.2/§6.3/§9/§11 用例同步 测试新增 scheduler_stack_test.go 3 项: - 嵌套 L1→L2→L3,恢复严格 LIFO(B 先于 A) - 只比栈顶:人为构造“栈底 L3、栈顶 L2”,必须取栈顶(区分两种实现) - 嵌套下的深度上限 验收:agent 全量 + -race;全仓 build/vet 通过
152 lines
4.8 KiB
Go
152 lines
4.8 KiB
Go
package core
|
||
|
||
// M4 验收测试:临界区语义显式化 + 抢占延迟到安全点 + 批次不再被中断放弃。
|
||
//
|
||
// 设计依据 docs/zh/input-scheduler-design.md §4.3(临界区)、§11.1(P5/P6)。
|
||
//
|
||
// 关键结构事实:让位检查**只在 step 之间**进行,因此任何正在执行的 step
|
||
// (工具 RPC、ONNX、CAS 落盘)天然不可抢占——中断只能等它返回。
|
||
|
||
import (
|
||
"sync"
|
||
"testing"
|
||
"time"
|
||
|
||
agentAPI "gitcode.com/JianFeeeee/HomeAgent/internal/agent/api"
|
||
agentIO "gitcode.com/JianFeeeee/HomeAgent/internal/agent/io"
|
||
sdk "gitcode.com/JianFeeeee/HomeAgent/internal/sdk"
|
||
)
|
||
|
||
// P5/P6:工具执行期间到达的高优先级中断不得立即抢占;必须等工具返回后的安全点。
|
||
func TestPreempt_DeferredDuringToolExec(t *testing.T) {
|
||
sh := NewStageHost()
|
||
entered := make(chan struct{})
|
||
release := make(chan struct{})
|
||
var once sync.Once
|
||
sh.RegisterTool("t_slow", sdk.ToolDef{Name: "t_slow", Plugin: "t"}, func(args map[string]interface{}) (interface{}, error) {
|
||
once.Do(func() { close(entered) })
|
||
<-release
|
||
return "slow-done", nil
|
||
})
|
||
|
||
sp := &scriptProvider{script: []*agentAPI.CompletionResponse{
|
||
{Content: "", ToolCalls: []agentAPI.ToolCall{tc("c1", "t_slow")}}, // 低优先级任务调用慢工具
|
||
{Content: "low-done"}, // 恢复后收尾
|
||
{Content: "intr-done"}, // 中断任务
|
||
}}
|
||
a := New(AgentConfig{
|
||
ID: "crit",
|
||
Provider: sp,
|
||
ProviderManager: agentAPI.NewProviderManager(),
|
||
IO: agentIO.NewIOManager(),
|
||
StageHost: sh,
|
||
})
|
||
|
||
if _, _ = enqueueTask(t, a, LevelBackground, "qq", "低优先级任务"); true {
|
||
}
|
||
lt, _, _ := a.sched.nextRef()
|
||
done := make(chan struct{})
|
||
go func() { a.executeNewTask(lt); close(done) }()
|
||
|
||
select {
|
||
case <-entered:
|
||
case <-time.After(3 * time.Second):
|
||
t.Fatal("慢工具未被调用")
|
||
}
|
||
|
||
// 工具执行中注入 L4 中断。
|
||
intrEvt, _ := textEvent("cli", "紧急打断")
|
||
intrEvt.Payload["interrupt"] = true
|
||
if !a.sched.requestPreempt(intrEvt, LevelCritical) {
|
||
t.Fatal("L4 应 arm 让位信号")
|
||
}
|
||
// 关键断言:信号已 arm,但任务仍在工具里 —— 绝不能挂起。
|
||
if !a.sched.preemptGrantedFor() {
|
||
t.Fatal("让位信号应已 arm")
|
||
}
|
||
if a.DumpScheduler().Running == nil {
|
||
t.Fatal("工具执行中不得挂起(StepToolExec 是临界区)")
|
||
}
|
||
if len(a.DumpScheduler().SuspendStack) != 0 {
|
||
t.Fatal("工具执行中 suspendStack 应为空")
|
||
}
|
||
|
||
// 放行工具 → 工具返回后的安全点才挂起。
|
||
close(release)
|
||
select {
|
||
case <-done:
|
||
case <-time.After(3 * time.Second):
|
||
t.Fatal("工具返回后未挂起")
|
||
}
|
||
|
||
snap := a.DumpScheduler()
|
||
if len(snap.SuspendStack) != 1 {
|
||
t.Fatalf("工具返回后 suspendStack=%d,期望 1", len(snap.SuspendStack))
|
||
}
|
||
if snap.SuspendStack[0].Frame.Step != StepToolAfter {
|
||
t.Fatalf("应在工具执行后的安全点挂起(StepToolAfter),实际 %v", snap.SuspendStack[0].Frame.Step)
|
||
}
|
||
if len(snap.PendingInterrupts) != 1 {
|
||
t.Fatalf("中断请求不得丢失,pendingInterrupts=%d", len(snap.PendingInterrupts))
|
||
}
|
||
}
|
||
|
||
// _consolidation_ 整任务视为不可抢占(它直接改图库)。
|
||
func TestCriticalSection_ConsolidationMarked(t *testing.T) {
|
||
a := newPreemptAgent(t, newPreemptProvider())
|
||
|
||
a.currentOutputChannel = "cli"
|
||
if a.inCriticalSection() {
|
||
t.Fatal("普通通道不应被判为临界区")
|
||
}
|
||
a.currentOutputChannel = channelConsolidation
|
||
if !a.inCriticalSection() {
|
||
t.Fatal("记忆整理必须是不可抢占临界区")
|
||
}
|
||
}
|
||
|
||
// 新语义:没有抢占时,同批的多个工具必须全部执行——不再有「中断放弃剩余批」。
|
||
func TestBatch_NotAbandonedWithoutPreemption(t *testing.T) {
|
||
sh := NewStageHost()
|
||
var mu sync.Mutex
|
||
var ran []string
|
||
reg := func(name string) {
|
||
sh.RegisterTool(name, sdk.ToolDef{Name: name, Plugin: "t"}, func(args map[string]interface{}) (interface{}, error) {
|
||
mu.Lock()
|
||
ran = append(ran, name)
|
||
mu.Unlock()
|
||
return name + "-out", nil
|
||
})
|
||
}
|
||
reg("t_a")
|
||
reg("t_b")
|
||
|
||
sp := &scriptProvider{script: []*agentAPI.CompletionResponse{
|
||
{Content: "", ToolCalls: []agentAPI.ToolCall{tc("c1", "t_a"), tc("c2", "t_b")}},
|
||
{Content: "全部完成"},
|
||
}}
|
||
a := New(AgentConfig{
|
||
ID: "batch",
|
||
Provider: sp,
|
||
ProviderManager: agentAPI.NewProviderManager(),
|
||
IO: agentIO.NewIOManager(),
|
||
StageHost: sh,
|
||
})
|
||
|
||
if _, _ = enqueueTask(t, a, LevelBackground, "cli", "跑两个工具"); true {
|
||
}
|
||
tt, _, _ := a.sched.nextRef()
|
||
a.executeNewTask(tt)
|
||
|
||
if len(ran) != 2 || ran[0] != "t_a" || ran[1] != "t_b" {
|
||
t.Fatalf("同批工具应全部按序执行,实际 %v", ran)
|
||
}
|
||
snap := a.DumpScheduler()
|
||
if len(snap.SuspendStack) != 0 || len(snap.PendingInterrupts) != 0 {
|
||
t.Fatalf("无抢占时不应有挂起或待处理中断:%+v", snap)
|
||
}
|
||
if snap.Stats.Executed != 1 {
|
||
t.Fatalf("Executed=%d,期望 1", snap.Stats.Executed)
|
||
}
|
||
}
|