refactor: 后端仓库层收口(A4:任务/项目/笔记/专员/动作定义)
把 B 档(此前尚无仓库的对象)的 api 裸查询收进仓库,api 层裸 store.DB 从 162 降到 96(口径:internal/ 下非测试 .go,不含 internal/repository/ 自身)。 新增 7 个仓库:TaskRecordRepo / TaskArtifactRepo / TaskRunRepo / ProjectRepo / StudyNoteRepo / SpecialistRepo / ActionDefinitionRepo。 按对象补齐的方法: - TaskRecordRepo:GetByID / GetByIDForOwners / ListBySpecialistKey / ListByOwners / ListByProject / CountBySpecialistKey / ClearProject / DeleteCascade - ProjectRepo:GetByIDForOwners / ListByOwners - StudyNoteRepo:ListByUser / GetByID - SpecialistRepo:GetByKey / GetByID / Query / List / CountByKey - TaskArtifactRepo / TaskRunRepo:GetByID / ListByTask - ActionDefinitionRepo:List / GetByID / GetByKey 几条口径改由仓库单点持有,避免各处手写漂移: - 归属过滤抽成 ownerScope:owners 为空时套恒假条件(空集),绝不退化成全表。 specialists/runtime 的 MyTaskQuery / ProjectQuery 随之删除,调用方改用 MyTaskOwners + 仓库方法 —— 任务与项目共用同一套归属口径。 - 「任务 + 交付物 + 运行记录三张表同事务级联删除」从 handler 收进 DeleteCascade, 不留没有任务的孤儿交付物;删项目只解除其下任务的 project_id 归属(置 NULL, 不是 0),不删任务本身 —— 任务是「做过的事」,删一个分组不该把它一起抹掉。 - 专员目录的 state 档位(默认 active / all 仅管理员 / system / 其它精确匹配, 且除显式要 system 外一律排除 system 记录)收进 SpecialistRepo.List。 - GetByKey 有意不过滤 state:调用方口径不同(建任务时要能查到,对话取 prompt 时要拒绝 inactive),口径留在调用方,仓库只负责取数。顺带把「按 key 找专员」 从 4 个文件里各写一遍收敛成一处。 - ActionDefinitionRepo.List 不替调用方定 state 默认值 ——「不传 state 就只看 active」是列表接口的契约,由 handler 解析 query 后传入。 - StudyNoteRepo.GetByID 不判归属,越权检查留在 handler(那里能把「不存在」与 「不是你的」分别回成 404 / 403)。 保留未动:skills/api/office_handlers.go 里两处 store.DB.Transaction —— 运行记录 与交付物要在同一个事务里落库,QueryBuilder 不带事务,维持原样并就地注明。 验证:go build ./... 与 go vet ./... 干净,go test ./... 6 个包全绿。 另用独立验证程序走真实路由 + 真实 HTTP,对数据库副本跑 148 项断言全绿 (覆盖跨用户越权 404、空 owners 退化成空集、级联删除、解除归属置 NULL、 笔记按用户隔离、专员 key 唯一性排除自身、管理员 state=all 仍排除 system 等)。 另对其中 10 条关键语义做了变异测试:逐条注入反向实现,确认断言确实会失败, 捕获 10 / 漏掉 0。变异测试同时暴露并修掉了验证体系自身的两个漏洞: - 「state=all 排除 system」这条规则此前没有任何断言能观察到 —— 默认档被 state=active 挡着、system 档被 state=system 挡着,删掉实现也不会红; - 变异驱动只跑 HTTP 断言、不跑 go test,导致针对单元测试注入的变异 (拒绝已下线专员的 prompt)永远逮不到。 验证程序为一次性脚手架,验证完成后已删除(tmp_vfy_b/)。 原始 data/eai_agentplatform.db 全程未触碰(mtime 仍为 2026-09-17 15:14)。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -10,9 +10,7 @@ import (
|
||||
|
||||
"eai_agentplatform/backend/internal/middleware"
|
||||
"eai_agentplatform/backend/internal/model"
|
||||
specialistmodel "eai_agentplatform/backend/internal/specialists/model"
|
||||
specialistruntime "eai_agentplatform/backend/internal/specialists/runtime"
|
||||
"eai_agentplatform/backend/internal/store"
|
||||
"eai_agentplatform/backend/internal/web"
|
||||
)
|
||||
|
||||
@@ -33,8 +31,6 @@ type projectReq struct {
|
||||
Pinned *bool `json:"pinned"`
|
||||
}
|
||||
|
||||
|
||||
|
||||
// encodeKeys 把 key 列表存成 JSON 字符串。空列表存空串而不是 "[]",
|
||||
// 读的时候一眼能看出「没配」和「配了但为空」的区别不大,但空串更省。
|
||||
func encodeKeys(keys []string) string {
|
||||
@@ -62,15 +58,7 @@ func ListProjects(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var items []model.Project
|
||||
if err := specialistruntime.ProjectQuery(user).
|
||||
Order("pinned DESC, updated_at DESC, id DESC").
|
||||
Limit(50).
|
||||
Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询项目失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, items)
|
||||
web.OK(c, projectRepo.ListByOwners(specialistruntime.MyTaskOwners(user), 50))
|
||||
}
|
||||
|
||||
// CreateProject 建一个项目。空 body 也收(跟「新建任务」一样,全走默认值),
|
||||
@@ -117,7 +105,7 @@ func CreateProject(c *gin.Context) {
|
||||
SkillKeys: encodeKeys(req.SkillKeys),
|
||||
ConnectorKeys: encodeKeys(req.ConnectorKeys),
|
||||
}
|
||||
if err := store.DB.Create(&project).Error; err != nil {
|
||||
if !projectRepo.Insert(&project) {
|
||||
web.Fail(c, web.NewBadRequest("创建项目失败"))
|
||||
return
|
||||
}
|
||||
@@ -137,8 +125,8 @@ func UpdateProject(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var project model.Project
|
||||
if err := specialistruntime.ProjectQuery(user).Where("id = ?", id).First(&project).Error; err != nil {
|
||||
project, found := projectRepo.GetByIDForOwners(id, specialistruntime.MyTaskOwners(user))
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("项目不存在"))
|
||||
return
|
||||
}
|
||||
@@ -181,7 +169,7 @@ func UpdateProject(c *gin.Context) {
|
||||
project.ConnectorKeys = encodeKeys(req.ConnectorKeys)
|
||||
}
|
||||
|
||||
if err := store.DB.Save(&project).Error; err != nil {
|
||||
if !projectRepo.Update(&project) {
|
||||
web.Fail(c, web.NewBadRequest("更新项目失败"))
|
||||
return
|
||||
}
|
||||
@@ -204,18 +192,16 @@ func DeleteProject(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var project model.Project
|
||||
if err := specialistruntime.ProjectQuery(user).Where("id = ?", id).First(&project).Error; err != nil {
|
||||
project, found := projectRepo.GetByIDForOwners(id, specialistruntime.MyTaskOwners(user))
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("项目不存在"))
|
||||
return
|
||||
}
|
||||
if err := store.DB.Model(&model.TaskRecord{}).
|
||||
Where("project_id = ?", project.ID).
|
||||
Update("project_id", nil).Error; err != nil {
|
||||
if !taskRecordRepo.ClearProject(project.ID) {
|
||||
web.Fail(c, web.NewBadRequest("解除任务归属失败"))
|
||||
return
|
||||
}
|
||||
if err := store.DB.Delete(&project).Error; err != nil {
|
||||
if !projectRepo.Delete(&project) {
|
||||
web.Fail(c, web.NewBadRequest("删除项目失败"))
|
||||
return
|
||||
}
|
||||
@@ -235,21 +221,13 @@ func ListProjectTasks(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var project model.Project
|
||||
if err := specialistruntime.ProjectQuery(user).Where("id = ?", id).First(&project).Error; err != nil {
|
||||
project, found := projectRepo.GetByIDForOwners(id, specialistruntime.MyTaskOwners(user))
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("项目不存在"))
|
||||
return
|
||||
}
|
||||
|
||||
var items []model.TaskRecord
|
||||
if err := store.DB.Where("project_id = ?", project.ID).
|
||||
Order("updated_at DESC, id DESC").
|
||||
Limit(100).
|
||||
Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询项目任务失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, items)
|
||||
web.OK(c, taskRecordRepo.ListByProject(project.ID, 100))
|
||||
}
|
||||
|
||||
// validateSpecialistKeys 专员 key 得真实存在才让存 —— 项目卡片上要显示专员名,
|
||||
@@ -262,8 +240,7 @@ func validateSpecialistKeys(keys []string) error {
|
||||
if trimmed == "" {
|
||||
continue
|
||||
}
|
||||
var specialist specialistmodel.Specialist
|
||||
if err := store.DB.Where("key = ?", trimmed).First(&specialist).Error; err != nil {
|
||||
if _, found := specialistRepo.GetByKey(trimmed); !found {
|
||||
return errors.New("专员不存在:" + trimmed)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user