feat(qa-ops): 持久本地问答副本与安全答案预览
build / build (push) Successful in 1m11s

新增迁移50与每5分钟/手动持久同步,API只读本地;同步失败保留、权限阻断、租约与质量失效原子激活。
保留全量稳定分页/固定列,已存质量批量读取,元数据不反复解压长答案;详情安全Markdown与原文。

已运行整仓 make verify、源与副本隔离PG -race、真实只读源及隔离浏览器。
实际同步2525条;固定窗口640条本地中位1.46秒,对照直接源33.10秒。
停worker、缺源凭据、重启API后仍可查询;未更新8088,未修改生产原问答。
This commit is contained in:
姚章浩
2026-10-18 19:10:47 +08:00
parent 74ba737b1e
commit 3e7fcce0b1
49 changed files with 4038 additions and 59 deletions
+62
View File
@@ -0,0 +1,62 @@
package qaopsquality
import (
"context"
"encoding/json"
"knowledge-governance/internal/dobotaiops"
)
// ReadViews projects quality already observed by mirror activation. It performs
// one SELECT, never inserts inputs/checks or advances an observation epoch.
func (s *Store) ReadViews(ctx context.Context, keys []dobotaiops.QualityKey) (map[string]View, error) {
if !s.available() {
return nil, ErrUnavailable
}
if len(keys) > 5000 {
return nil, ErrInvalid
}
expected := map[string]string{}
ids := make([]string, 0, len(keys))
for _, k := range keys {
if !validID(k.ID) || !hashPattern.MatchString(k.InputHash) {
return nil, ErrInvalid
}
if old, exists := expected[k.ID]; exists {
if old != k.InputHash {
return nil, ErrInvalid
}
continue
}
expected[k.ID] = k.InputHash
ids = append(ids, k.ID)
}
out := map[string]View{}
if len(ids) == 0 {
return out, nil
}
rows, e := s.pool.Query(ctx, `SELECT h.record_id,h.input_hash,h.input_epoch,h.review_revision,`+checkObject+`::text,COALESCE((SELECT `+reviewObject+` FROM qa_ops_quality_reviews r WHERE r.tenant_key=h.tenant_key AND r.record_id=h.record_id AND r.revision=h.review_revision),'null'::jsonb)::text FROM qa_ops_quality_records h JOIN qa_ops_quality_inputs i ON i.tenant_key=h.tenant_key AND i.record_id=h.record_id AND i.input_epoch=h.input_epoch JOIN qa_ops_quality_checks c ON c.tenant_key=h.tenant_key AND c.record_id=h.record_id AND c.input_epoch=h.input_epoch AND c.rule_version=$3 WHERE h.tenant_key=$1 AND h.record_id=ANY($2::text[])`, s.tenant, ids, dobotaiops.CoverageRuleVersion)
if e != nil {
return nil, ErrUnavailable
}
defer rows.Close()
for rows.Next() {
var id, hash, checkRaw, reviewRaw string
var epoch, revision int64
if rows.Scan(&id, &hash, &epoch, &revision, &checkRaw, &reviewRaw) != nil {
return nil, ErrUnavailable
}
if expected[id] != hash {
return nil, ErrConflict
}
var automatic Check
var review *Review
if json.Unmarshal([]byte(checkRaw), &automatic) != nil || json.Unmarshal([]byte(reviewRaw), &review) != nil {
return nil, ErrUnavailable
}
out[id] = Project(hash, epoch, revision, automatic, review)
}
if rows.Err() != nil || len(out) != len(ids) {
return nil, ErrUnavailable
}
return out, nil
}
+28
View File
@@ -131,6 +131,34 @@ func (s *Store) view(ctx context.Context, tx pgx.Tx, id string) (View, error) {
}
return Project(hash, epoch, revision, automatic, review), nil
}
// ObserveRecordsInTx makes mirror activation and review invalidation one commit.
// Only the caller owns commit/rollback; no remote I/O occurs while these locks are held.
func (s *Store) ObserveRecordsInTx(ctx context.Context, tx pgx.Tx, records []dobotaiops.Record) error {
if !s.available() || tx == nil || len(records) > 100 {
return ErrInvalid
}
items := make([]Evidence, 0, len(records))
size := 0
for _, r := range records {
e, err := EvidenceFor(r)
if err != nil {
return err
}
size += len(e.Payload)
if size > 32<<20 {
return ErrInvalid
}
items = append(items, e)
}
sort.Slice(items, func(i, j int) bool { return items[i].Record.ID < items[j].Record.ID })
for _, e := range items {
if err := s.observeLocked(ctx, tx, e); err != nil {
return err
}
}
return nil
}
func (s *Store) Observe(ctx context.Context, records []dobotaiops.Record) (map[string]View, error) {
if len(records) > 5000 {
return nil, ErrInvalid