fix(release): 修复tableState文本身份与快照统计归属
build / build (push) Successful in 1m57s

tableState 非 resources 分支恢复直接扫描 text 身份,避免 queryRows 把普通
doc id/UUID/复合键/数字 id 当 JSON canonical 校验而失败;resources 仍使用
F01 精确引用闭包。隔离 PG 覆盖字符串、UUID、复合键与数字 ID。

F11:快照 HTTP 投影补 bindings/wiki_log_entries;作业统计按本作业
target_snapshot_id/snapshot_id 取对应快照,取不到显示未知,不借最新封存;
running/pending/failed/unknown 不再在无 detail 时显示完成。

F03:verifications 支持 snapshot_id 精确查询,返回 snapshot_latest 并只按
该快照回答 verified;抽屉“查看校验记录”按选中快照加载,不借用最新记录。

F08:显示主机探测时间;无真实 next-run 时说明尚未核验下一次执行;最近成功
单列未知,不以最近运行冒充。

验证:Go/前端全量、隔离 PG tableState/F01 对拍与 make verify 17 项通过。
实际 Current→Prepare→Verify ES 集成被共享 ES 磁盘 watermark 阻塞,未冒充。
This commit is contained in:
姚章浩
2026-11-05 13:19:27 +08:00
parent 1a0f99e346
commit b81fcd07e3
15 changed files with 404 additions and 41 deletions
@@ -329,32 +329,39 @@ func (t *ProductionLibraryTarget) tableState(ctx context.Context, table string)
return 0, "", fmt.Errorf("读取生产 %s 失败: %w", table, err)
}
rows = items
} else {
exporter := PostgresExporter{db: t.pool}
items, err := exporter.queryRows(ctx, fmt.Sprintf(`SELECT %s FROM %s WHERE %s ORDER BY 1`,
}
// Non-resource identities are plain text (UUID, doc id, numeric id, or a
// composite key), not JSON. Scanning them through queryRows would run
// canonicalJSON on text and fail on every non-empty table.
if table != "resources" {
rows, err := t.pool.Query(ctx, fmt.Sprintf(`SELECT %s FROM %s WHERE %s ORDER BY 1`,
"("+identityExpr(table)+")::text", table, scopeWhere(table)), identity.TenantID, identity.KnowledgeBaseID)
if err != nil {
return 0, "", fmt.Errorf("读取生产 %s 失败", table)
}
rows = items
defer rows.Close()
ids := make([]string, 0, 64)
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
return 0, "", err
}
ids = append(ids, id)
}
if err := rows.Err(); err != nil {
return 0, "", err
}
return int64(len(ids)), digestIDs(ids), nil
}
ids := make([]string, 0, len(rows))
for _, raw := range rows {
var id string
if table == "resources" {
var value struct {
ID string `json:"id"`
}
if err := json.Unmarshal(raw, &value); err != nil || value.ID == "" {
return 0, "", fmt.Errorf("读取生产 %s 身份失败", table)
}
id = value.ID
} else {
if err := json.Unmarshal(raw, &id); err != nil {
return 0, "", err
}
var value struct {
ID string `json:"id"`
}
ids = append(ids, id)
if err := json.Unmarshal(raw, &value); err != nil || value.ID == "" {
return 0, "", fmt.Errorf("读取生产 %s 身份失败", table)
}
ids = append(ids, value.ID)
}
return int64(len(ids)), digestIDs(ids), nil
}
@@ -14,8 +14,10 @@ import (
// F01 regression: a small target library inside a large tenant must resolve
// resources from exact binding ids + body handles, not by scanning every
// tenant resource with a per-row body predicate. The old predicate is kept
// here only as an oracle for equality and timing; never as the product path.
// tenant resource with a per-row body predicate. The fixture has one active
// target document/chunk (plus one deleted and one foreign-tenant row), not the
// historical 4/38 target; the 20,000 unrelated resources are the scale input.
// The old predicate is kept only as an oracle for equality and timing.
func TestLibraryResourceClosureIsBoundedByTargetReferences(t *testing.T) {
dsn := os.Getenv("KG_LIBRARY_TEST_DATABASE_URL")
if dsn == "" {
@@ -106,7 +108,15 @@ func TestLibraryResourceClosureIsBoundedByTargetReferences(t *testing.T) {
if len(newIDs) != 2 {
t.Fatalf("expected bound + body-only resources, got %v", newIDs)
}
t.Logf("unrelated_resources=%d old_scan=%s new_export=%s resources=%v", unrelated, oldElapsed, newElapsed, newIDs)
var activeDocs, activeChunks int
if err := db.QueryRow(ctx, `SELECT count(*) FROM knowledges WHERE tenant_id=10000 AND knowledge_base_id='kb-safe' AND deleted_at IS NULL`).Scan(&activeDocs); err != nil {
t.Fatal(err)
}
if err := db.QueryRow(ctx, `SELECT count(*) FROM chunks c JOIN knowledges k ON k.id=c.knowledge_id WHERE k.tenant_id=10000 AND k.knowledge_base_id='kb-safe' AND k.deleted_at IS NULL AND c.deleted_at IS NULL`).Scan(&activeChunks); err != nil {
t.Fatal(err)
}
t.Logf("target_docs=%d target_chunks=%d unrelated_resources=%d old_scan=%s new_export=%s resources=%v",
activeDocs, activeChunks, unrelated, oldElapsed, newElapsed, newIDs)
if newElapsed >= oldElapsed {
t.Fatalf("F01 path did not improve: old=%s new=%s", oldElapsed, newElapsed)
}
@@ -0,0 +1,58 @@
package v071
import (
"context"
"os"
"strings"
"testing"
"github.com/jackc/pgx/v5/pgxpool"
)
// F01/F02 regression: tableState reads normal text identities (doc id, UUID,
// composite relation key, numeric local id) without treating them as JSON.
// The fixture is session-local temp tables; it never writes a persistent table.
func TestTableStateReadsTextIdentities(t *testing.T) {
dsn := os.Getenv("KG_LIBRARY_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("requires explicitly isolated kg_library_test_ PostgreSQL database")
}
cfg, err := pgxpool.ParseConfig(dsn)
if err != nil || !strings.HasPrefix(cfg.ConnConfig.Database, "kg_library_test_") {
t.Fatal("isolated database required")
}
cfg.MaxConns = 1
cfg.ConnConfig.RuntimeParams["search_path"] = "pg_temp"
cfg.ConnConfig.RuntimeParams["statement_timeout"] = "10000"
ctx := context.Background()
pool, err := pgxpool.NewWithConfig(ctx, cfg)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
_, err = pool.Exec(ctx, `
CREATE TEMP TABLE knowledges(id text,tenant_id bigint,knowledge_base_id text);
CREATE TEMP TABLE knowledge_tag_relations(knowledge_id text,tag_id text);
CREATE TEMP TABLE wiki_log_entries(id bigint,tenant_id bigint,knowledge_base_id text);
INSERT INTO knowledges VALUES ('doc-parent',10000,'kb-parent'),
('f643a7e7-bbb9-4209-9033-fca7f76f011b',10000,'kb-parent');
INSERT INTO knowledge_tag_relations VALUES ('doc-parent','tag-parent'),
('f643a7e7-bbb9-4209-9033-fca7f76f011b','tag-two');
INSERT INTO wiki_log_entries VALUES (1,10000,'kb-parent'),(2,10000,'kb-parent');`)
if err != nil {
t.Fatal(err)
}
target := &ProductionLibraryTarget{pool: pool, scope: ProductionScope{TenantID: 10000, KnowledgeBaseID: "kb-parent"}}
for _, fixture := range []struct {
table string
count int64
}{{"knowledges", 2}, {"knowledge_tag_relations", 2}, {"wiki_log_entries", 2}} {
count, _, err := target.tableState(ctx, fixture.table)
if err != nil {
t.Fatalf("%s plain identity rejected: %v", fixture.table, err)
}
if count != fixture.count {
t.Fatalf("%s count=%d want=%d", fixture.table, count, fixture.count)
}
}
}