fix(qaops): 点踩问题统一判为不正确
build / build (push) Successful in 1m11s

通过持久来源关联读取最新本地反馈,强制总表有效正确性为否,保留原问答和人工证据。统一列表、详情、指标和两阶段读取的反馈快照,拒绝冲突选择并保留其他字段编辑。已通过完整本地门禁、隔离PG竞态和真实恢复库52条点踩逐条读回、桌面及手机验收;未调用模型或连接源生产库。
This commit is contained in:
姚章浩
2026-10-20 08:18:03 +08:00
parent 87f97a2f9b
commit 2c0bb32183
22 changed files with 403 additions and 35 deletions
@@ -0,0 +1,53 @@
package httpapi
import (
"context"
"encoding/json"
"knowledge-governance/internal/dobotaiops"
"knowledge-governance/internal/qaopsquality"
"testing"
)
type downvoteSnapshotReader struct {
QAOpsReader
view qaopsquality.View
}
func (s downvoteSnapshotReader) ReadStoredQuality(context.Context, []dobotaiops.QualityKey) (map[string]qaopsquality.View, error) {
return map[string]qaopsquality.View{"fixture-feedback-race": s.view}, nil
}
func TestMasterQualityEnrichmentUsesOneFeedbackDecisionEvenWhenVoteChangesBetweenReads(t *testing.T) {
for _, test := range []struct {
name string
old, current dobotaiops.RecordFeedback
label, correctness, rule string
}{
{"new-downvote", dobotaiops.RecordFeedback{}, dobotaiops.RecordFeedback{Down: 1}, "否", "incorrect", "downvote-is-incorrect-v1"},
{"withdrawn-current-vote", dobotaiops.RecordFeedback{Down: 1}, dobotaiops.RecordFeedback{}, "是", "correct", ""},
} {
t.Run(test.name, func(t *testing.T) {
view := qaopsquality.View{EffectiveSource: "master-effective-fields-v1", FeedbackRule: test.rule, Effective: qaopsquality.Fields{Correctness: test.correctness}, MasterFeedback: &test.current}
c := Config{QAOps: downvoteSnapshotReader{view: view}, QAQuality: &qualityFake{}}
item := dobotaiops.Record{ID: "fixture-feedback-race", RecordOrigin: "dobotai_2_0", Feedback: test.old, MasterFields: map[string]string{"is_correct": "待确认"}}
if err := enrichQAQualityRecord(context.Background(), c, &item); err != nil {
t.Fatal(err)
}
if item.Feedback != test.current || item.MasterFields["is_correct"] != test.label {
t.Fatal("detail combines an old feedback snapshot with a newer quality decision")
}
page := dobotaiops.RecordPage{Items: []dobotaiops.Record{{ID: item.ID, RecordOrigin: item.RecordOrigin, Feedback: test.old, MasterFields: map[string]string{"is_correct": "待确认"}}}}
// The common projection must be shared by the list and detail enrichment paths.
if err := enrichQAQualityPage(context.Background(), c, &page); err != nil {
t.Fatal(err)
}
if page.Items[0].Feedback != test.current || page.Items[0].MasterFields["is_correct"] != test.label {
t.Fatal("list combines an old feedback snapshot with a newer quality decision")
}
var quality map[string]any
if json.Unmarshal(item.Quality, &quality) != nil {
t.Fatal("quality response missing")
}
})
}
}
+2
View File
@@ -43,6 +43,8 @@ func qaMasterFieldPatch(w http.ResponseWriter, r *http.Request, c Config) {
result, err := writer.PatchMasterFields(r.Context(), r.PathValue("id"), q, identity.Username)
if err != nil {
switch {
case errors.Is(err, qaopsquality.ErrDownvoted):
writeJSON(w, 409, map[string]string{"error": "问题已点踩,是否正确固定为否;未保存冲突选择,请重新读取"})
case errors.Is(err, qaopsquality.ErrConflict):
writeJSON(w, 409, map[string]string{"error": "字段或问答版本已变化,请重新读取后修改;未覆盖其他人的内容"})
case errors.Is(err, dobotaiops.ErrInvalidQuery):
+19 -2
View File
@@ -68,9 +68,26 @@ func enrichQAQualityRecord(ctx context.Context, c Config, item *dobotaiops.Recor
if !ok {
return qaopsquality.ErrUnavailable
}
item.Quality, err = json.Marshal(v)
return attachQAQualityView(item, v)
}
func attachQAQualityView(item *dobotaiops.Record, view qaopsquality.View) error {
// A vote may change between the payload and quality reads; return the vote
// that actually produced this decision, not a contradictory older snapshot.
if view.MasterFeedback != nil && item.RecordOrigin != "" {
item.Feedback = *view.MasterFeedback
fields := make(map[string]string, len(item.MasterFields)+1)
for key, value := range item.MasterFields {
fields[key] = value
}
fields["is_correct"] = map[string]string{"correct": "是", "incorrect": "否", "pending": "待确认"}[view.Effective.Correctness]
item.MasterFields = fields
}
raw, err := json.Marshal(view)
item.Quality = raw
return err
}
func enrichQAQualityPage(ctx context.Context, c Config, page *dobotaiops.RecordPage) error {
if c.QAQuality == nil {
return nil
@@ -84,7 +101,7 @@ func enrichQAQualityPage(ctx context.Context, c Config, page *dobotaiops.RecordP
if !ok {
return qaopsquality.ErrUnavailable
}
page.Items[i].Quality, err = json.Marshal(v)
err = attachQAQualityView(&page.Items[i], v)
if err != nil {
return err
}