98294b9583
build / build (push) Failing after 24s
why:审计 19 项(12 P1 / 7 P2)来自同一批根因,逐点试错会把协议撕成十几份:
· 意图与执行混为一谈(F01/F15):`library.sync:<scope>:<快照>` 是终身幂等键,
A→B→A 的第三次发布拿到第一次的历史成功作业,什么都不做;
· 版本行既是实体又是事件(F02/F09):主键只由 (kind,scope,快照) 决定、写入是
upsert,第二个作业覆盖 `source_job_id`/`evidence`/`created_at`;
· 接管没有代次(F13):两个 worker 都能认领同一 pending 作业,失租的旧执行还能
写业务状态、释放别人的维护锁;
· 早退与终态没有统一政策(F03/F04/F05/F06):半写窗口的前置检查失败会重新开门、
服务起不来也照样开入口、journal 终态后收尾不可重建、恢复成功不回写 current;
· 数据身份与准入各写一套(F07/F08/F14/F17):接管丢了复用语义、全库搬运不改
location_hash、通用入口可造 library.* 作业、目标模型从没被问过;
· 存储/容量/UI/CI 判据各自为政(F10/F11/F12/F16/F18/F19)。
what:按批准的一套协议集中实施(详见 handoffs/release-consolidated-implementation/
current-contract.md):
· operation(一次明确意图)/ attempt(epoch 代次)/ 不可变版本事件 / current 指针;
治理库迁移 76,只动治理库,不碰 WeKnora Schema/源码/镜像;
· 所有业务状态、阶段、版本事件、维护锁写入带 epoch 守卫(失租代次写不进去);
心跳失租立刻取消 handler;
· 半写窗口早退保持关闭;服务未全部健康绝不开入口;journal 终态可从持久事实重建
收尾;恢复成功登记 recovery 事件并推进 current;
· worker 在动维护前核对业务行/kind/scope/队列关联;通用入口拒绝 library.*;
· 全库搬运重算 location_hash 并与文档级导入共用实现;接管消费写集合的复用决定;
发布前核目标实时模型契约、容量门禁前置;
· 存储保护闭包改为全量依赖、类型只认显式登记、保护未知则不出可删候选;
· 页面核 HTTP 状态与结构、恢复纳入轮询、Wiki 差异覆盖标题/摘要/父级。
how:`make verify` rc=0(16 项必需检查全绿,含 go test 全量与前端 485 例);
`go vet ./...` rc=0;`npx tsc -p tsconfig.json --noEmit` rc=0;逐项负例见各包测试
(F03/F04/F08/F12/F13/F14/F16/F18/F19 都有先红后绿记录)。
未执行:治理库迁移到任何已有库、真实 PG/ES/S3/WeKnora 集成、故障注入、部署。
125 lines
6.2 KiB
Go
125 lines
6.2 KiB
Go
package httpapi
|
||
|
||
import (
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
"knowledge-governance/internal/governance"
|
||
)
|
||
|
||
// F01:同一个目标快照的**新意图**必须真的执行,不能被"这个快照曾经成功过"吞掉。
|
||
//
|
||
// 现场形状:同步 B 成功 → 恢复 A → 再次同步 B。第三次请求是一次合法意图,
|
||
// 旧实现返回第一次的成功作业(202, 不再并入队),用户要的状态没实现。
|
||
//
|
||
// 判据:换 operation_key ⇒ 新 operation(ID 不同、作业幂等键不同);
|
||
// 同一个 operation_key ⇒ 同一 operation(重放)。
|
||
func TestLibrarySyncNewIntentReExecutesInsteadOfReusingHistory(t *testing.T) {
|
||
target := "snap-1"
|
||
digest, err := librarySyncParamsDigest("internal", target)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
now := time.Now().UTC()
|
||
newStore := func() *fakeLibraryStore {
|
||
store := &fakeLibraryStore{
|
||
current: &governance.LibraryVersion{ID: "ver-prod", Scope: "internal",
|
||
Kind: governance.LibraryVersionProductionEffective, SnapshotID: "prod-snap",
|
||
VersionLabel: "prod-2026.10", CreatedAt: now.Add(-72 * time.Hour)},
|
||
}
|
||
store.enqueueEchoRequestedID = true
|
||
store.grant = &governance.LibraryReleaseAuthorization{ID: "auth-1", Scope: "internal", SnapshotID: target,
|
||
ParamsDigest: digest, AuthorizedBy: "admin", ExpiresAt: now.Add(30 * time.Minute), CreatedAt: now}
|
||
return store
|
||
}
|
||
config := func(store *fakeLibraryStore) Config {
|
||
return Config{Store: store, ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: true,
|
||
LibraryProductionDatabaseURL: "postgres://prod.invalid/db", LibraryProductionInternalKB: "kb-int",
|
||
LibraryCapacityBudgetBytes: 1 << 40}
|
||
}
|
||
post := func(store *fakeLibraryStore, body string) *httptest.ResponseRecorder {
|
||
recorder := httptest.NewRecorder()
|
||
librarySyncEnqueue(recorder, httptest.NewRequest(http.MethodPost, "/api/v1/library/syncs",
|
||
strings.NewReader(body)), config(store))
|
||
return recorder
|
||
}
|
||
withKey := `{"scope":"internal","target_snapshot_id":"` + target + `","operation_key":"%s"}`
|
||
|
||
// 第一次意图:建作业。
|
||
first := newStore()
|
||
if recorder := post(first, strings.Replace(withKey, "%s", "intent-1", 1)); recorder.Code != http.StatusAccepted {
|
||
t.Fatalf("首次发布会话 status=%d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
firstOperation := first.lastOperation.ID
|
||
if firstOperation == "" || first.lastJob.OperationID != firstOperation {
|
||
t.Fatalf("作业必须挂在意图上:job.operation=%q intent=%q", first.lastJob.OperationID, firstOperation)
|
||
}
|
||
|
||
// 同一次意图重放(双击/超时重发):同一个 operation,不产生第二条意图。
|
||
replay := newStore()
|
||
if recorder := post(replay, strings.Replace(withKey, "%s", "intent-1", 1)); recorder.Code != http.StatusAccepted {
|
||
t.Fatalf("重放 status=%d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if replay.lastOperation.ID != firstOperation {
|
||
t.Fatalf("同一意图键得到了不同 operation:%q vs %q", replay.lastOperation.ID, firstOperation)
|
||
}
|
||
|
||
// 新意图(同一快照、不同键):必须是新 operation ⇒ 新作业幂等键 ⇒ 真的执行。
|
||
second := newStore()
|
||
if recorder := post(second, strings.Replace(withKey, "%s", "intent-2", 1)); recorder.Code != http.StatusAccepted {
|
||
t.Fatalf("新意图 status=%d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if second.lastOperation.ID == firstOperation {
|
||
t.Fatal("同一快照的新意图必须得到新 operation:否则历史成功会吞掉本次请求(F01)")
|
||
}
|
||
if second.lastJob.IdempotencyKey == first.lastJob.IdempotencyKey {
|
||
t.Fatal("新意图的作业幂等键与历史作业相同:CreateLibraryJob 会返回旧作业,什么也不执行")
|
||
}
|
||
if second.createN != 1 {
|
||
t.Fatalf("新意图必须建作业并执行,created=%d", second.createN)
|
||
}
|
||
}
|
||
|
||
// 同一个意图键配上不同快照 = 冲突,不能悄悄共享谱系。
|
||
func TestLibrarySyncIntentKeyReusedWithAnotherSnapshotConflicts(t *testing.T) {
|
||
target := "snap-1"
|
||
digest, _ := librarySyncParamsDigest("internal", target)
|
||
now := time.Now().UTC()
|
||
store := &fakeLibraryStore{current: &governance.LibraryVersion{ID: "ver-prod", Scope: "internal",
|
||
Kind: governance.LibraryVersionProductionEffective, SnapshotID: "prod-snap", CreatedAt: now.Add(-time.Hour)}}
|
||
store.grant = &governance.LibraryReleaseAuthorization{ID: "auth-1", Scope: "internal", SnapshotID: target,
|
||
ParamsDigest: digest, AuthorizedBy: "admin", ExpiresAt: now.Add(time.Hour), CreatedAt: now}
|
||
// 预先登记一条"同键但不同快照"的意图。
|
||
if _, _, err := store.EnsureLibraryOperation(nil, governance.LibraryOperation{
|
||
ID: "libop-x", Scope: "internal", Kind: governance.LibraryJobSync, TargetSnapshotID: "other-snap",
|
||
RequestDigest: "different", OperationKey: target + "#intent-1", CreatedAt: now}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
config := Config{Store: store, ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: true,
|
||
LibraryProductionDatabaseURL: "postgres://prod.invalid/db", LibraryProductionInternalKB: "kb-int",
|
||
LibraryCapacityBudgetBytes: 1 << 40}
|
||
body := `{"scope":"internal","target_snapshot_id":"` + target + `","operation_key":"intent-1"}`
|
||
recorder := httptest.NewRecorder()
|
||
librarySyncEnqueue(recorder, httptest.NewRequest(http.MethodPost, "/api/v1/library/syncs", strings.NewReader(body)), config)
|
||
if recorder.Code != http.StatusConflict {
|
||
t.Fatalf("同键不同内容必须 409,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
}
|
||
|
||
// F14:通用 queue 入口不许创建整库保留类型——那里的角色检查替代不了发布准入。
|
||
func TestCreateJobRejectsReservedLibraryTypes(t *testing.T) {
|
||
store := &fakeJobStore{}
|
||
config := Config{Store: store}
|
||
body := `{"type":"library.baseline","payload":{"scope":"internal"},"idempotency_key":"k-1"}`
|
||
recorder := httptest.NewRecorder()
|
||
createJob(recorder, httptest.NewRequest(http.MethodPost, "/api/v1/jobs", strings.NewReader(body)), config)
|
||
if recorder.Code != http.StatusBadRequest {
|
||
t.Fatalf("保留类型必须被通用入口拒绝:status=%d body=%s", recorder.Code, recorder.Body.String())
|
||
}
|
||
if !strings.Contains(recorder.Body.String(), "发布管理专用入口") {
|
||
t.Fatalf("拒绝原因必须指向专用入口:%s", recorder.Body.String())
|
||
}
|
||
} |