fix(release): 以实例身份阻断绑定与发布跨实例

网页可能在实例 A 上列库并保存生产绑定,而文档发布执行器实际连实例 B。
克隆实例可以有相同知识库 ID、名称甚至租户,单靠 ID 无法证明写的是同一个
目标。现在绑定保存会冻结候选实例地址/租户,发布计划把身份冻结到每个项,
执行器在 Contract/Prepare/Activate/Restore/Compensate 前核对;不一致以
release_contract_mismatch 非重试失败关闭,不写目标。整库发布目标不变。

测试入库对象在上传前记录实际实例地址/租户;轮询、验收、移除先核对当前
连接,改绑实例或租户后旧对象不会被克隆实例接管。旧行没有身份时失败关闭。

「知识库绑定」保存要求对内/对外各选一个库,不再把显式未绑定回落到部署
默认 ID,避免换绑实例后带过去旧对象的默认库。

验证:隔离 PostgreSQL 覆盖绑定身份落库与发布计划冻结;v071 用同 ID/同租户
的 A/B 实例做正反例;四个测试 handler 的实例错配负例;前端 607 项通过。
This commit is contained in:
姚章浩
2026-11-19 15:17:17 +08:00
parent ce2a155ebc
commit 1f5bf72aec
43 changed files with 823 additions and 113 deletions
+10 -1
View File
@@ -37,6 +37,9 @@ var (
ErrInvalidReleaseJob = errors.New("invalid_release_job")
ErrReleaseRetryable = errors.New("release_retryable")
ErrReleaseState = errors.New("release_state_failed")
// ErrTargetInstanceMismatch: the binding was verified on instance A while
// the executor writes instance B (even with the same knowledge base id).
ErrTargetInstanceMismatch = errors.New("release_target_instance_mismatch")
)
type PlanItem struct {
@@ -51,7 +54,13 @@ type PlanItem struct {
// TargetKnowledgeBaseID is the 源库 → 目标库 mapping resolved when the
// execution started (see production_kb_bindings). It equals the tested base
// when the scope is unbound, which is the historical same-id semantics.
TargetKnowledgeBaseID string `json:"target_knowledge_base_id"`
TargetKnowledgeBaseID string `json:"target_knowledge_base_id"`
// TargetInstanceURL/TargetTenantID freeze the instance identity the console
// verified the selected base on. The executor compares them with its own
// route target before any write; a cloned instance with the same base id is
// not the same target.
TargetInstanceURL string `json:"target_instance_url,omitempty"`
TargetTenantID uint64 `json:"target_tenant_id,omitempty"`
KnowledgeID string `json:"knowledge_id"`
DecidedVisibility governance.Visibility `json:"decided_visibility"`
PreviousProductionRevision string `json:"previous_production_revision"`
@@ -132,7 +132,7 @@ func (publisher *SidecarPublisher) Publish(ctx context.Context, item PlanItem) (
}
targetContract, err := publisher.target.Contract(ctx, item)
if err != nil {
return ItemResult{}, newSidecarError(ErrorCodeTargetUnavailable, true, err)
return ItemResult{}, targetError(err)
}
if err := releasebundle.Validate(manifest, targetContract, releaseItemMapping(item)); err != nil {
return ItemResult{}, newSidecarError(ErrorCodeContractMismatch, false, err)
@@ -166,7 +166,7 @@ func (publisher *SidecarPublisher) Rollback(ctx context.Context, item PlanItem)
}
restoredRevision, err := publisher.target.Restore(ctx, item, item.PreviousProductionRevision)
if err != nil {
return ItemResult{}, newSidecarError(ErrorCodeTargetWriteFailed, true, err)
return ItemResult{}, targetError(err)
}
if restoredRevision != item.PreviousProductionRevision {
return ItemResult{}, newSidecarError(ErrorCodeContractMismatch, false, errors.New("restored revision mismatch"))
@@ -197,7 +197,7 @@ func (publisher *SidecarPublisher) Delete(ctx context.Context, item PlanItem) (I
}
targetContract, err := publisher.target.Contract(ctx, item)
if err != nil {
return ItemResult{}, newSidecarError(ErrorCodeTargetUnavailable, true, err)
return ItemResult{}, targetError(err)
}
if err := releasebundle.Validate(manifest, targetContract, releaseItemMapping(item)); err != nil {
return ItemResult{}, newSidecarError(ErrorCodeContractMismatch, false, err)
@@ -224,6 +224,9 @@ func (publisher *SidecarPublisher) Delete(ctx context.Context, item PlanItem) (I
func (publisher *SidecarPublisher) compensateError(ctx context.Context, item PlanItem, change PreparedChange, code string, cause error) error {
if err := publisher.target.Compensate(ctx, item, change); err != nil {
if errors.Is(err, ErrTargetInstanceMismatch) {
return newSidecarError(ErrorCodeContractMismatch, false, err)
}
return newSidecarError(ErrorCodeRollbackFailed, false, err)
}
return newSidecarError(code, false, cause)
@@ -233,6 +236,15 @@ func newSidecarError(code string, retryable bool, cause error) error {
return &ItemError{Code: code, Retryable: retryable, Cause: cause}
}
// targetError keeps an instance mismatch non-retryable: retrying the same item
// on the same wrong instance will never become the verified target.
func targetError(err error) error {
if errors.Is(err, ErrTargetInstanceMismatch) {
return newSidecarError(ErrorCodeContractMismatch, false, err)
}
return newSidecarError(ErrorCodeTargetUnavailable, true, err)
}
func validRevision(value string) bool {
return value == strings.TrimSpace(value) && value != "" && len(value) <= 128 && !strings.ContainsAny(value, "\x00\r\n\t/?#\\")
}
@@ -163,6 +163,7 @@ func (bundle fakeExportedBundle) ReleaseManifest() releasebundle.Manifest { retu
type fakeSidecarTarget struct {
contract releasebundle.TargetContract
contractErr error
calls *[]string
prepared PreparedChange
prepareErr, activateErr, verifyErr, compensateErr, restoreErr, verifyRestoredErr error
@@ -172,7 +173,7 @@ type fakeSidecarTarget struct {
func (f *fakeSidecarTarget) Contract(context.Context, PlanItem) (releasebundle.TargetContract, error) {
*f.calls = append(*f.calls, "contract")
return f.contract, nil
return f.contract, f.contractErr
}
func (f *fakeSidecarTarget) Prepare(context.Context, PlanItem, ExportedBundle) (PreparedChange, error) {
*f.calls = append(*f.calls, "prepare")
@@ -360,3 +361,26 @@ func TestSidecarPublisherGatesDeleteAndRollbackToo(t *testing.T) {
}
}
}
// Instance identity mismatch is a contract failure, not a transient target
// outage: retrying the same item on the same wrong instance can never succeed.
func TestSidecarPublisherMapsInstanceMismatchToNonRetryableContractError(t *testing.T) {
calls := []string{}
manifest, err := releasebundle.Seal(testManifest())
if err != nil {
t.Fatal(err)
}
publisher, err := NewSidecarPublisher(&fakeExporter{manifest: manifest, calls: &calls},
&fakeSidecarTarget{contract: testTargetContract(), contractErr: ErrTargetInstanceMismatch, calls: &calls})
if err != nil {
t.Fatal(err)
}
_, err = publisher.Publish(context.Background(), testPlanItem())
var itemErr *ItemError
if !errors.As(err, &itemErr) || itemErr.Code != ErrorCodeContractMismatch || itemErr.Retryable {
t.Fatalf("error=%#v", err)
}
if !reflect.DeepEqual(calls, []string{"export", "contract"}) {
t.Fatalf("calls=%v", calls)
}
}