533db535f1
build / build (push) Successful in 1m12s
按已确认需求提供独立安全模型配置、真实探测、正式字典字段评估与原问题译中,持久批次支持只补空白、暂停恢复、有限重试、幂等和逐字段依据。覆盖固定规则、本地语言、组织关联与主表有效看板共用治理侧读模型,保留源生产和冻结历史。 验证:完整 make verify 退出0,702项Go与329项前端通过;默认118项外部/隔离集成明确跳过,另跑完整镜像及组织隔离PG竞态。12并发、输入漂移、过期租约、暂停/恢复及有限重试通过。用户授权的百炼 deepseek-v4.1-flash 完成真实后台与浏览器评估/翻译;552行、16列、11块看板及完整PNG、独立配置页测试/保存、重开恢复和零JS异常通过。仅非业务样本外发,自动处理关闭,原payload不变。 上线前治理库已做导出快照原生备份并真实恢复,60表行数与17个QA表内容哈希一致;不修改DobotAI生产或隧道。本提交时尚未部署,线上验收须独立完成。
149 lines
4.8 KiB
Go
149 lines
4.8 KiB
Go
package qaopsai
|
|
|
|
import (
|
|
"context"
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
func automationColumns(kind string) (string, string, error) {
|
|
switch kind {
|
|
case "evaluation":
|
|
return "automatic_evaluation", "evaluation_initialized", nil
|
|
case "translation":
|
|
return "automatic_translation", "translation_initialized", nil
|
|
}
|
|
return "", "", ErrInvalid
|
|
}
|
|
func (s *Store) SetAutomation(ctx context.Context, kind string, enabled bool, actor string) (Settings, error) {
|
|
column, initializedColumn, err := automationColumns(kind)
|
|
if err != nil || !s.valid() || actor == "" {
|
|
return Settings{}, ErrInvalid
|
|
}
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return Settings{}, ErrUnavailable
|
|
}
|
|
defer tx.Rollback(context.Background())
|
|
var initialized bool
|
|
err = tx.QueryRow(ctx, `SELECT `+initializedColumn+` FROM qa_ops_ai_settings WHERE tenant_key=$1 FOR UPDATE`, s.tenant).Scan(&initialized)
|
|
if err != nil {
|
|
return Settings{}, ErrUnavailable
|
|
}
|
|
cfg, err := s.readSettings(ctx, tx)
|
|
if err != nil {
|
|
return Settings{}, err
|
|
}
|
|
if enabled && (cfg.Verification != "verified" || !cfg.KeyConfigured || !cfg.StorageReady) {
|
|
return Settings{}, ErrConflict
|
|
}
|
|
baseline, resumed := 0, 0
|
|
if enabled && !initialized {
|
|
tag, e := tx.Exec(ctx, `INSERT INTO qa_ops_ai_automatic_seen(tenant_key,kind,record_id)SELECT tenant_key,$2,id FROM qa_ops_master_records WHERE tenant_key=$1 ON CONFLICT DO NOTHING`, s.tenant, kind)
|
|
if e != nil {
|
|
return Settings{}, ErrUnavailable
|
|
}
|
|
baseline = int(tag.RowsAffected())
|
|
initialized = true
|
|
}
|
|
if _, err = tx.Exec(ctx, `UPDATE qa_ops_ai_settings SET `+column+`=$2,`+initializedColumn+`=$3 WHERE tenant_key=$1`, s.tenant, enabled, initialized); err != nil {
|
|
return Settings{}, ErrUnavailable
|
|
}
|
|
if enabled {
|
|
tag, e := tx.Exec(ctx, `UPDATE qa_ops_ai_work SET state='queued',stage='queued',updated_at=clock_timestamp() WHERE tenant_key=$1 AND kind=$2 AND trigger_mode='automatic' AND state='paused'`, s.tenant, kind)
|
|
if e != nil {
|
|
return Settings{}, ErrUnavailable
|
|
}
|
|
resumed = int(tag.RowsAffected())
|
|
} else {
|
|
if _, err = tx.Exec(ctx, `UPDATE qa_ops_ai_work SET state='paused',stage='paused',updated_at=clock_timestamp() WHERE tenant_key=$1 AND kind=$2 AND trigger_mode='automatic' AND state IN ('queued','retry')`, s.tenant, kind); err != nil {
|
|
return Settings{}, ErrUnavailable
|
|
}
|
|
}
|
|
event, err := newID("qaia-")
|
|
if err != nil {
|
|
return Settings{}, err
|
|
}
|
|
if _, err = tx.Exec(ctx, `INSERT INTO qa_ops_ai_automation_events(tenant_key,id,kind,enabled,actor,baseline_records,resumed_records)VALUES($1,$2,$3,$4,$5,$6,$7)`, s.tenant, event, kind, enabled, actor, baseline, resumed); err != nil {
|
|
return Settings{}, ErrUnavailable
|
|
}
|
|
cfg, err = s.readSettings(ctx, tx)
|
|
if err != nil {
|
|
return Settings{}, err
|
|
}
|
|
if tx.Commit(ctx) != nil {
|
|
return Settings{}, ErrUnavailable
|
|
}
|
|
return cfg.Settings, nil
|
|
}
|
|
func (s *Store) ScanAutomatic(ctx context.Context) (int, error) {
|
|
if !s.valid() {
|
|
return 0, ErrUnavailable
|
|
}
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return 0, ErrUnavailable
|
|
}
|
|
defer tx.Rollback(context.Background())
|
|
var revision int64
|
|
err = tx.QueryRow(ctx, `SELECT revision FROM qa_ops_ai_settings WHERE tenant_key=$1 FOR UPDATE`, s.tenant).Scan(&revision)
|
|
if err == pgx.ErrNoRows {
|
|
return 0, nil
|
|
}
|
|
if err != nil {
|
|
return 0, ErrUnavailable
|
|
}
|
|
cfg, err := s.readSettings(ctx, tx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if cfg.Verification != "verified" || !cfg.KeyConfigured || !cfg.StorageReady {
|
|
return 0, nil
|
|
}
|
|
total := 0
|
|
for _, kind := range []string{"evaluation", "translation"} {
|
|
if kind == "evaluation" && !cfg.AutomaticEvaluation || kind == "translation" && !cfg.AutomaticTranslation {
|
|
continue
|
|
}
|
|
rows, e := tx.Query(ctx, `SELECT m.id FROM qa_ops_master_records m WHERE m.tenant_key=$1 AND m.origin='dobotai_2_0' AND NOT EXISTS(SELECT 1 FROM qa_ops_ai_automatic_seen a WHERE a.tenant_key=m.tenant_key AND a.record_id=m.id AND a.kind=$2) ORDER BY m.created_at,m.id LIMIT 500`, s.tenant, kind)
|
|
if e != nil {
|
|
return 0, ErrUnavailable
|
|
}
|
|
ids := []string{}
|
|
for rows.Next() {
|
|
var id string
|
|
if rows.Scan(&id) != nil {
|
|
rows.Close()
|
|
return 0, ErrUnavailable
|
|
}
|
|
ids = append(ids, id)
|
|
}
|
|
e = rows.Err()
|
|
rows.Close()
|
|
if e != nil {
|
|
return 0, ErrUnavailable
|
|
}
|
|
if len(ids) == 0 {
|
|
continue
|
|
}
|
|
metas, e := s.readMetas(ctx, tx, ids, nil)
|
|
if e != nil {
|
|
return 0, e
|
|
}
|
|
for _, m := range metas {
|
|
if m.Status == "succeeded" && m.Coverage.State == "checked" && len(needs(m, kind)) > 0 {
|
|
if _, e = s.enqueueMeta(ctx, tx, m, kind, "automatic", cfg.Revision); e != nil {
|
|
return 0, e
|
|
}
|
|
total++
|
|
}
|
|
if _, e = tx.Exec(ctx, `INSERT INTO qa_ops_ai_automatic_seen(tenant_key,kind,record_id)VALUES($1,$2,$3)ON CONFLICT DO NOTHING`, s.tenant, kind, m.ID); e != nil {
|
|
return 0, ErrUnavailable
|
|
}
|
|
}
|
|
}
|
|
if tx.Commit(ctx) != nil {
|
|
return 0, ErrUnavailable
|
|
}
|
|
return total, nil
|
|
}
|