fix(reconciliation): 区分知识库已删除与连接失败

台账里会残留已被删除的测试库(test_publications.test_kb_id 聚合出下拉候选),
页面默认又取排序最靠前的一项,于是「立即对账」打在死库上,远端回 404 被统一
映射成 remote_unavailable,页面提示「检查地址与 Key」——把工程师引去改一个根本
没坏的配置。

- 后端:BeginTask 的 404 单独落 remote_kb_missing,其余仍为 remote_unavailable
- 前端:新增文案「所选测试知识库已不存在」;默认选中当前绑定的测试库并在下拉里
  标注(当前绑定),用户自己选过(含「全部测试库」)后不再改写
This commit is contained in:
Zhero & 助理
2026-12-03 11:35:35 +08:00
committed by 姚章浩
parent c1bdbe181e
commit bceb7ba682
5 changed files with 62 additions and 5 deletions
+12
View File
@@ -6,6 +6,7 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"net/http"
"time"
"knowledge-governance/internal/governance"
@@ -42,6 +43,14 @@ func RuntimeErrorCode(err error) string {
}
}
// knowledgeBaseMissing 只认 WeKnora 明确回答的 404 —— 库被删掉、或 ID 属于
// 别的环境/租户。它与网络不通、Key 无权限要分开:这三种情况的处置完全不同,
// 页面把 404 说成「检查地址与 Key」会让工程师去改一个根本没坏的配置。
func knowledgeBaseMissing(err error) bool {
var httpErr *weknora.HTTPError
return errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusNotFound
}
type Store interface {
ReapStaleReconciliationRuns(context.Context, time.Time, time.Duration) (int64, error)
ClaimReconciliationRun(context.Context, governance.ReconciliationRun, time.Time) (governance.ReconciliationRun, error)
@@ -268,6 +277,9 @@ func (s *Service) ExecuteRun(ctx context.Context, runID string) error {
}
pinned, _, err := s.remote.BeginTask(ctx, run.KBID)
if err != nil {
if knowledgeBaseMissing(err) {
return finished("remote_kb_missing")
}
return finished("remote_unavailable")
}
ledger, err := s.store.LoadReconciliationLedger(pinned, run.KBID)
+22
View File
@@ -145,6 +145,7 @@ type fakeReconciliationRemote struct {
missing map[string]bool
gets map[string]weknora.Knowledge
listErr error
beginErr error
pageSizes []int
deleteCalls int
listInFlight int
@@ -152,6 +153,9 @@ type fakeReconciliationRemote struct {
}
func (f *fakeReconciliationRemote) BeginTask(ctx context.Context, _ string) (context.Context, weknora.InstanceIdentity, error) {
if f.beginErr != nil {
return ctx, weknora.InstanceIdentity{}, f.beginErr
}
return ctx, weknora.InstanceIdentity{BaseURL: "https://test.example", TenantID: 7}, nil
}
func (f *fakeReconciliationRemote) ListKnowledge(_ context.Context, _ string, _ int, pageSize int) ([]weknora.Knowledge, int64, error) {
@@ -395,6 +399,24 @@ func (r *concurrencyRemote) GetKnowledge(context.Context, string) (weknora.Knowl
return weknora.Knowledge{}, &weknora.HTTPError{StatusCode: 404}
}
// 库被删掉时,远端回的是 404,不是网络故障。批次必须落成 remote_kb_missing,
// 页面才能提示「去选当前绑定的库」,而不是让人去改没坏的地址与 Key。
func TestExecuteRunReportsMissingKnowledgeBase(t *testing.T) {
now := time.Date(2026, 12, 1, 12, 0, 0, 0, time.UTC)
store := &fakeReconciliationStore{run: governance.ReconciliationRun{ID: "run-1", KBID: "kb-gone", State: "running", StartedAt: now}}
remote := &fakeReconciliationRemote{beginErr: &weknora.HTTPError{StatusCode: 404}}
service, err := New(Config{Store: store, Remote: remote, Now: func() time.Time { return now }})
if err != nil {
t.Fatal(err)
}
if err := service.ExecuteRun(context.Background(), "run-1"); err != nil {
t.Fatal(err)
}
if store.failCode != "remote_kb_missing" {
t.Fatalf("fail code = %q", store.failCode)
}
}
func TestExecuteRunFailsWholeBatchWithoutFindings(t *testing.T) {
now := time.Date(2026, 12, 1, 12, 0, 0, 0, time.UTC)
store := &fakeReconciliationStore{run: governance.ReconciliationRun{ID: "run-1", KBID: "kb-1", State: "running", StartedAt: now}}