Files
smartmate/backend/conv/schedule_state_test.go
Losita 2038185730 Version: 0.9.2.dev.260406
后端:
   1.Chat 四路由升级(二分类 chat/task → 四路由 direct_reply/execute/deep_answer/plan)
     - 新建model/chat_contract.go:路由决策模型,含 NeedsRoughBuild 粗排标记
     - 更新node/chat.go:四路由分流;新增 deep_answer 深度回答路径(二次 LLM 开 thinking)
     - 更新prompt/chat.go:意图分类 prompt 升级为四路由 prompt;新增 deep_answer prompt
   2.粗排节点(RoughBuild)全链路
     - 新建node/rough_build.go:粗排节点,调用注入的算法函数,结果写入 ScheduleState 后进 Execute 微调
     - 更新graph/common_graph.go:注册 RoughBuild 节点;Chat/Confirm 后可路由至粗排
     - 更新model/graph_run_state.go:新增 RoughBuildPlacement/RoughBuildFunc 类型;Deps 注入入口
     - 更新model/plan_contract.go:PlanDecision 新增 NeedsRoughBuild/TaskClassIDs 字段
     - 更新node/plan.go:plan_done 时写入粗排标记和 TaskClassIDs
   3.任务类约束元数据(TaskClassMeta)贯穿 prompt → tools → 持久化
     - 更新tools/state.go:新增 TaskClassMeta;ScheduleState.TaskClasses;ScheduleTask.TaskClassID;Clone 深拷贝
     - 更新conv/schedule_state.go:加载时构建 TaskClassMeta;Diff 支持 HostEventID 嵌入关系
     - 更新conv/schedule_provider.go:新增 LoadTaskClassMetas 按需加载
     - 更新model/state_store.go:ScheduleStateProvider 接口新增 LoadTaskClassMetas
     - 更新prompt/base.go:renderStateSummary 渲染任务类约束
     - 更新prompt/plan.go:注入任务类 ID 上下文和粗排识别规则
     - 更新tools/read_tools.go:GetOverview 展示任务类约束
     - 更新model/common_state.go:CommonState 新增 TaskClassIDs/TaskClasses/NeedsRoughBuild
   4.Execute 健壮性增强(correction 重试 + 纯 ReAct 模式)
     - 更新node/execute.go:未知工具名/空文本走 correction 重试而非 fatal;maxConsecutiveCorrections 提升为包级常量;新增无 plan 纯ReAct 模式;工具结果截断;speak 排除 ask_user/confirm
     - 更新prompt/execute.go:新增 ReAct 模式 system prompt 和 contract
   5.写入持久化完善(task_item source + 嵌入水课)
     - 更新conv/schedule_persist.go:place/move/unplace 支持 task_item source,含嵌入水课和普通 task event 两条路径
     - 新建conv/schedule_preview.go:ScheduleState → 排程预览缓存,复用旧格式,前端无需改动
   6.状态持久化体系(Redis → MySQL outbox 异步)
     - 更新dao/cache.go:Redis 快照 TTL 从 24h 改为 2h,配合 MySQL outbox
     - 新建model/agent_state_snapshot_record.go:快照 MySQL 记录模型
     - 新建service/events/agent_state_persist.go:outbox 异步持久化处理器
     - 更新cmd/start.go + inits/mysql.go:注册快照事件处理器 + AutoMigrate
     - 更新service/agentsvc/agent_newagent.go:注入 RoughBuildFunc;outbox 异步写快照;排程结果写 Redis 预览缓存
   7.基础设施与稳定性
     - 更新stream/sse_adapter.go:outChan 满时静默丢弃,保证持久化不被 SSE 阻断
     - 更新service/agentsvc/agent.go:新增 readAgentExtraIntSlice;outChan 容量 8→256
     - 更新node/agent_nodes.go:Chat 注入工具 schema;Deliver 改 saveAgentState 替代 deleteAgentState
前端:无
仓库:无
2026-04-06 23:15:54 +08:00

280 lines
8.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package conv
import (
"testing"
newagenttools "github.com/LoveLosita/smartflow/backend/newAgent/tools"
)
// buildTestState 构造最小可用的 ScheduleStateDayMapping 让 expandToCoords 能正常工作。
func buildTestState(days []newagenttools.DayMapping, tasks []newagenttools.ScheduleTask) *newagenttools.ScheduleState {
return &newagenttools.ScheduleState{
Window: newagenttools.ScheduleWindow{
TotalDays: len(days),
DayMapping: days,
},
Tasks: tasks,
}
}
// defaultDays 返回 3 天的 DayMappingday1=week3/dow1, day2=week3/dow2, day3=week3/dow3
func defaultDays() []newagenttools.DayMapping {
return []newagenttools.DayMapping{
{DayIndex: 1, Week: 3, DayOfWeek: 1},
{DayIndex: 2, Week: 3, DayOfWeek: 2},
{DayIndex: 3, Week: 3, DayOfWeek: 3},
}
}
// ==================== DiffScheduleState: task_item place ====================
// TestDiff_PlaceTaskItem_NonEmbed 验证:普通放置 task_item 时 HostEventID=0。
func TestDiff_PlaceTaskItem_NonEmbed(t *testing.T) {
days := defaultDays()
original := buildTestState(days, []newagenttools.ScheduleTask{
{StateID: 1, Source: "task_item", SourceID: 10, Name: "复习线代", Status: "pending", Duration: 2},
})
modified := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 1,
Source: "task_item",
SourceID: 10,
Name: "复习线代",
Status: "existing",
Slots: []newagenttools.TaskSlot{{Day: 1, SlotStart: 1, SlotEnd: 2}},
},
})
changes := DiffScheduleState(original, modified)
if len(changes) != 1 {
t.Fatalf("期望 1 个变更,实际 %d 个", len(changes))
}
c := changes[0]
if c.Type != ChangePlace {
t.Errorf("期望 ChangePlace实际 %s", c.Type)
}
if c.Source != "task_item" || c.SourceID != 10 {
t.Errorf("source 或 sourceID 错误: %s/%d", c.Source, c.SourceID)
}
if c.HostEventID != 0 {
t.Errorf("非嵌入路径 HostEventID 应为 0实际 %d", c.HostEventID)
}
if len(c.NewCoords) != 2 {
t.Errorf("期望 2 个节次坐标,实际 %d", len(c.NewCoords))
}
}
// TestDiff_PlaceTaskItem_Embed 验证:嵌入放置时 HostEventID = 宿主的 SourceID。
func TestDiff_PlaceTaskItem_Embed(t *testing.T) {
days := defaultDays()
// 原始宿主水课已安排guest 待安排
original := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 100,
Source: "event",
SourceID: 999, // ScheduleEvent.ID of the host course
Name: "高数",
Status: "existing",
CanEmbed: true,
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}},
},
{StateID: 1, Source: "task_item", SourceID: 10, Name: "复习线代", Status: "pending", Duration: 2},
})
hostID := 100
// 修改后guest 嵌入到宿主
modified := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 100,
Source: "event",
SourceID: 999,
Name: "高数",
Status: "existing",
CanEmbed: true,
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}},
EmbeddedBy: &[]int{1}[0],
},
{
StateID: 1,
Source: "task_item",
SourceID: 10,
Name: "复习线代",
Status: "existing",
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}},
EmbedHost: &hostID,
},
})
changes := DiffScheduleState(original, modified)
// 宿主 slots 未变,只有 guest 产生 place 变更
var placeChange *ScheduleChange
for i := range changes {
if changes[i].SourceID == 10 {
placeChange = &changes[i]
}
}
if placeChange == nil {
t.Fatal("未找到 task_item 的 place 变更")
}
if placeChange.HostEventID != 999 {
t.Errorf("嵌入路径 HostEventID 应为 999宿主 SourceID实际 %d", placeChange.HostEventID)
}
}
// ==================== DiffScheduleState: task_item unplace ====================
// TestDiff_UnplaceTaskItem_NonEmbed 验证:从普通位置移除时 HostEventID=0。
func TestDiff_UnplaceTaskItem_NonEmbed(t *testing.T) {
days := defaultDays()
original := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 1,
Source: "task_item",
SourceID: 10,
Name: "复习线代",
Status: "existing",
Slots: []newagenttools.TaskSlot{{Day: 1, SlotStart: 5, SlotEnd: 6}},
},
})
modified := buildTestState(days, []newagenttools.ScheduleTask{
{StateID: 1, Source: "task_item", SourceID: 10, Name: "复习线代", Status: "pending"},
})
changes := DiffScheduleState(original, modified)
if len(changes) != 1 {
t.Fatalf("期望 1 个变更,实际 %d", len(changes))
}
c := changes[0]
if c.Type != ChangeUnplace {
t.Errorf("期望 ChangeUnplace实际 %s", c.Type)
}
if c.HostEventID != 0 {
t.Errorf("普通移除 HostEventID 应为 0实际 %d", c.HostEventID)
}
if len(c.OldCoords) != 2 {
t.Errorf("期望 2 个旧坐标,实际 %d", len(c.OldCoords))
}
}
// TestDiff_UnplaceTaskItem_Embed 验证:从嵌入位置移除时 HostEventID = 宿主 SourceID。
func TestDiff_UnplaceTaskItem_Embed(t *testing.T) {
days := defaultDays()
hostStateID := 100
original := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 100,
Source: "event",
SourceID: 999,
Name: "高数",
Status: "existing",
CanEmbed: true,
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}},
EmbeddedBy: &[]int{1}[0],
},
{
StateID: 1,
Source: "task_item",
SourceID: 10,
Name: "复习线代",
Status: "existing",
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}},
EmbedHost: &hostStateID,
},
})
modified := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 100,
Source: "event",
SourceID: 999,
Name: "高数",
Status: "existing",
CanEmbed: true,
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}},
},
{StateID: 1, Source: "task_item", SourceID: 10, Name: "复习线代", Status: "pending"},
})
changes := DiffScheduleState(original, modified)
var unplaceChange *ScheduleChange
for i := range changes {
if changes[i].SourceID == 10 {
unplaceChange = &changes[i]
}
}
if unplaceChange == nil {
t.Fatal("未找到 task_item 的 unplace 变更")
}
if unplaceChange.HostEventID != 999 {
t.Errorf("嵌入移除 HostEventID 应为 999实际 %d", unplaceChange.HostEventID)
}
}
// ==================== DiffScheduleState: task_item move ====================
// TestDiff_MoveTaskItem 验证task_item 移动时 OldHostEventID 和 HostEventID 分别对应旧/新位置宿主。
func TestDiff_MoveTaskItem_NonEmbedToNonEmbed(t *testing.T) {
days := defaultDays()
original := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 1,
Source: "task_item",
SourceID: 10,
Name: "复习线代",
Status: "existing",
Slots: []newagenttools.TaskSlot{{Day: 1, SlotStart: 1, SlotEnd: 2}},
},
})
modified := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 1,
Source: "task_item",
SourceID: 10,
Name: "复习线代",
Status: "existing",
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 5, SlotEnd: 6}},
},
})
changes := DiffScheduleState(original, modified)
if len(changes) != 1 {
t.Fatalf("期望 1 个变更,实际 %d", len(changes))
}
c := changes[0]
if c.Type != ChangeMove {
t.Errorf("期望 ChangeMove实际 %s", c.Type)
}
if c.HostEventID != 0 || c.OldHostEventID != 0 {
t.Errorf("非嵌入移动两个 HostEventID 均应为 0实际 %d/%d", c.OldHostEventID, c.HostEventID)
}
if len(c.OldCoords) != 2 || len(c.NewCoords) != 2 {
t.Errorf("旧坐标 %d 个,新坐标 %d 个,均期望 2 个", len(c.OldCoords), len(c.NewCoords))
}
}
// ==================== resolveHostEventID ====================
func TestResolveHostEventID_NoEmbed(t *testing.T) {
task := &newagenttools.ScheduleTask{StateID: 1, EmbedHost: nil}
state := buildTestState(defaultDays(), nil)
if got := resolveHostEventID(task, state); got != 0 {
t.Errorf("无嵌入时应返回 0实际 %d", got)
}
}
func TestResolveHostEventID_WithEmbed(t *testing.T) {
hostID := 100
task := &newagenttools.ScheduleTask{StateID: 1, EmbedHost: &hostID}
state := buildTestState(defaultDays(), []newagenttools.ScheduleTask{
{StateID: 100, Source: "event", SourceID: 999},
})
if got := resolveHostEventID(task, state); got != 999 {
t.Errorf("期望宿主 SourceID=999实际 %d", got)
}
}