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:
eaiadmin
2026-09-19 01:55:26 +08:00
co-authored by Claude Code
parent 19cf6fb5f2
commit 4f31e0c438
18 changed files with 669 additions and 268 deletions
@@ -8,10 +8,17 @@ import (
"eai_agentplatform/backend/internal/middleware"
"eai_agentplatform/backend/internal/model"
"eai_agentplatform/backend/internal/store"
"eai_agentplatform/backend/internal/repository"
"eai_agentplatform/backend/internal/web"
)
// noteRepo 学习笔记仓库(便于测试时覆写),包内共享。
var noteRepo repository.StudyNoteRepo
func init() {
noteRepo = repository.StudyNoteRepo{}
}
var validNoteItemTypes = map[string]bool{"company": true, "product": true, "course": true}
// ListNotes GET /api/notes?item_type=&item_id= —— 我的学习笔记
@@ -21,21 +28,16 @@ func ListNotes(c *gin.Context) {
web.Fail(c, web.NewAuthError("未登录"))
return
}
q := store.DB.Where("user_id = ?", u.ID)
if t := c.Query("item_type"); t != "" {
q = q.Where("item_type = ?", t)
}
// item_id 解析失败(不是数字)就当没传 —— 不给一个错参数回 400,
// 笔记列表本来就是按内容可筛可不筛。
var itemID *uint
if s := c.Query("item_id"); s != "" {
if n, err := strconv.ParseUint(s, 10, 64); err == nil {
q = q.Where("item_id = ?", n)
id := uint(n)
itemID = &id
}
}
var items []model.StudyNote
if err := q.Order("updated_at DESC").Find(&items).Error; err != nil {
web.Fail(c, web.NewBadRequest("查询笔记失败"))
return
}
web.OK(c, items)
web.OK(c, noteRepo.ListByUser(u.ID, c.Query("item_type"), itemID))
}
// CreateNote POST /api/notes —— 新增学习笔记
@@ -63,7 +65,7 @@ func CreateNote(c *gin.Context) {
return
}
n := model.StudyNote{UserID: u.ID, ItemType: req.ItemType, ItemID: req.ItemID, Content: strings.TrimSpace(req.Content)}
if err := store.DB.Create(&n).Error; err != nil {
if !noteRepo.Insert(&n) {
web.Fail(c, web.NewBadRequest("保存笔记失败"))
return
}
@@ -81,8 +83,8 @@ func UpdateNote(c *gin.Context) {
if !ok {
return
}
var n model.StudyNote
if err := store.DB.First(&n, id).Error; err != nil {
n, found := noteRepo.GetByID(id)
if !found {
web.Fail(c, web.NewNotFoundError("笔记不存在"))
return
}
@@ -102,7 +104,7 @@ func UpdateNote(c *gin.Context) {
return
}
n.Content = strings.TrimSpace(req.Content)
if err := store.DB.Save(&n).Error; err != nil {
if !noteRepo.Update(&n) {
web.Fail(c, web.NewBadRequest("更新笔记失败"))
return
}
@@ -120,8 +122,8 @@ func DeleteNote(c *gin.Context) {
if !ok {
return
}
var n model.StudyNote
if err := store.DB.First(&n, id).Error; err != nil {
n, found := noteRepo.GetByID(id)
if !found {
web.Fail(c, web.NewNotFoundError("笔记不存在"))
return
}
@@ -129,7 +131,7 @@ func DeleteNote(c *gin.Context) {
web.Fail(c, web.NewForbiddenError("无权操作他人笔记"))
return
}
if err := store.DB.Delete(&n).Error; err != nil {
if !noteRepo.Delete(&n) {
web.Fail(c, web.NewBadRequest("删除笔记失败"))
return
}