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
+23 -18
View File
@@ -91,15 +91,16 @@ type QualityKey struct {
InputHash string `json:"input_hash"`
}
type RecordPage struct {
ScopeQuality []QualityKey `json:"-"`
ScopeActors []ActorIdentity `json:"-"`
Organization *OrganizationVersion `json:"organization,omitempty"`
Items []Record `json:"items"`
Columns []string `json:"columns"`
Total int64 `json:"total"`
HasMore bool `json:"has_more"`
NextCursor string `json:"next_cursor"`
AsOf time.Time `json:"as_of"`
LocalRevision int64 `json:"local_revision,omitempty"`
ScopeQuality []QualityKey `json:"-"`
ScopeActors []ActorIdentity `json:"-"`
Organization *OrganizationVersion `json:"organization,omitempty"`
Items []Record `json:"items"`
Columns []string `json:"columns"`
Total int64 `json:"total"`
HasMore bool `json:"has_more"`
NextCursor string `json:"next_cursor"`
AsOf time.Time `json:"as_of"`
}
type recordCursor struct {
@@ -172,7 +173,7 @@ func recordKeywordPattern(keyword string) (string, error) {
return "%" + strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(keyword) + "%", nil
}
func (c *Client) ListRecords(ctx context.Context, query RecordQuery) (RecordPage, error) {
func (c *RecordsRepository) ListRecords(ctx context.Context, query RecordQuery) (RecordPage, error) {
if err := c.validateQueryWindow(query.Window); err != nil {
return RecordPage{}, err
}
@@ -197,12 +198,16 @@ func (c *Client) ListRecords(ctx context.Context, query RecordQuery) (RecordPage
}
args := []any{query.Window.TenantKey, query.Window.Start, query.Window.End, pattern}
page := RecordPage{Items: make([]Record, 0), Columns: make([]string, 0)}
err := c.withReadOnlyTx(ctx, func(ctx context.Context, q querier) error {
if err := q.QueryRow(ctx, "SELECT transaction_timestamp()").Scan(&page.AsOf); err != nil {
err := c.transact(ctx, func(ctx context.Context, q querier) error {
if err := c.readAsOf(ctx, q, &page.AsOf); err != nil {
return ErrUnavailable
}
var rawColumns, rawActors, rawQuality string
if err := q.QueryRow(ctx, recordsBaseSQL+recordColumnsSQL+recordFilterSQL, append(args, noPublishedMarker)...).Scan(&page.Total, &rawColumns, &rawActors, &rawQuality); err != nil {
summarySQL := c.baseSQL + recordColumnsSQL
if c.summarySQL != "" {
summarySQL = c.summarySQL
}
if err := q.QueryRow(ctx, summarySQL+recordFilterSQL, append(args, noPublishedMarker)...).Scan(&page.Total, &rawColumns, &rawActors, &rawQuality); err != nil {
return fmt.Errorf("%w: record count failed", ErrUnavailable)
}
if json.Unmarshal([]byte(rawQuality), &page.ScopeQuality) != nil {
@@ -223,7 +228,7 @@ func (c *Client) ListRecords(ctx context.Context, query RecordQuery) (RecordPage
page.Columns = append(page.Columns, column)
}
}
rows, err := q.Query(ctx, recordsBaseSQL+` SELECT payload::text FROM records WHERE `+recordFilterSQL+`
rows, err := q.Query(ctx, c.baseSQL+` SELECT payload::text FROM records WHERE `+recordFilterSQL+`
AND ($5::timestamptz IS NULL OR (asked_at,id)<($5,$6)) ORDER BY asked_at DESC,id DESC LIMIT $7`, append(args, before, cursor.ID, query.Limit+1)...)
if err != nil {
return fmt.Errorf("%w: record list failed", ErrUnavailable)
@@ -259,14 +264,14 @@ func (c *Client) ListRecords(ctx context.Context, query RecordQuery) (RecordPage
return page, nil
}
func (c *Client) GetRecord(ctx context.Context, tenantKey, id string) (Record, error) {
if strings.TrimSpace(tenantKey) == "" || id == "" || len(id) > 256 || strings.ContainsRune(id, 0) {
func (c *RecordsRepository) GetRecord(ctx context.Context, tenantKey, id string) (Record, error) {
if strings.TrimSpace(tenantKey) == "" || (c.tenant != "" && tenantKey != c.tenant) || id == "" || len(id) > 256 || strings.ContainsRune(id, 0) {
return Record{}, ErrInvalidQuery
}
var item Record
err := c.withReadOnlyTx(ctx, func(ctx context.Context, q querier) error {
err := c.transact(ctx, func(ctx context.Context, q querier) error {
var raw string
err := q.QueryRow(ctx, recordsBaseSQL+` SELECT payload::text FROM records WHERE id=$2`, tenantKey, id).Scan(&raw)
err := q.QueryRow(ctx, c.baseSQL+` SELECT payload::text FROM records WHERE id=$2`, tenantKey, id).Scan(&raw)
if errors.Is(err, pgx.ErrNoRows) {
return ErrRecordNotFound
}