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
@@ -39,26 +39,48 @@ func (s *Store) SaveProductionKBBindings(ctx context.Context, internalID, extern
if now.IsZero() {
return errors.New("production binding save requires a timestamp")
}
if _, err := s.db.Exec(ctx, productionBindingUpsertSQL, internalID, externalID, updatedBy, now); err != nil {
// 旧入口没有实例身份可写:显式清空身份列,执行器随后会失败关闭,
// 而不是沿用上一条(可能属于另一个实例的)地址/租户。
if _, err := s.db.Exec(ctx, productionBindingIdentityUpsertSQL,
internalID, externalID, updatedBy, now, "", "", 0, 0); err != nil {
return errors.New("save production kb bindings")
}
return nil
}
const productionBindingUpsertSQL = `
INSERT INTO production_kb_bindings(id,internal_kb_id,external_kb_id,updated_by,updated_at)
VALUES('active',$1,$2,$3,$4)
// productionBindingIdentityUpsertSQL freezes the instance identity the console
// verified each side on. An empty URL keeps the legacy same-id behaviour for
// old callers; the executor then cannot prove the instance and refuses a mapped
// write instead of guessing.
const productionBindingIdentityUpsertSQL = `
INSERT INTO production_kb_bindings(
id,internal_kb_id,external_kb_id,updated_by,updated_at,
internal_instance_url,external_instance_url,internal_tenant_id,external_tenant_id)
VALUES('active',$1,$2,$3,$4,$5,$6,$7,$8)
ON CONFLICT(id) DO UPDATE SET
internal_kb_id=EXCLUDED.internal_kb_id,
external_kb_id=EXCLUDED.external_kb_id,
updated_by=EXCLUDED.updated_by,
updated_at=EXCLUDED.updated_at`
updated_at=EXCLUDED.updated_at,
internal_instance_url=EXCLUDED.internal_instance_url,
external_instance_url=EXCLUDED.external_instance_url,
internal_tenant_id=EXCLUDED.internal_tenant_id,
external_tenant_id=EXCLUDED.external_tenant_id`
// SaveProductionKBBindingsTx lets the environment-scoped 「知识库绑定」save
// update the same row inside its own transaction, so connection and bindings
// cannot be half-written.
func SaveProductionKBBindingsTx(ctx context.Context, tx pgx.Tx, internalID, externalID, updatedBy string, now time.Time) error {
if _, err := tx.Exec(ctx, productionBindingUpsertSQL, internalID, externalID, updatedBy, now); err != nil {
return SaveProductionKBBindingsWithIdentityTx(ctx, tx, internalID, externalID, "", "", 0, 0, updatedBy, now)
}
// SaveProductionKBBindingsWithIdentityTx is used by the environment-scoped
// 「知识库绑定」save: it stores which instance/tenant each side was verified on,
// so the release executor can refuse a cloned instance with the same base id.
func SaveProductionKBBindingsWithIdentityTx(ctx context.Context, tx pgx.Tx, internalID, externalID, internalURL, externalURL string, internalTenant, externalTenant uint64, updatedBy string, now time.Time) error {
if _, err := tx.Exec(ctx, productionBindingIdentityUpsertSQL,
internalID, externalID, updatedBy, now,
internalURL, externalURL, internalTenant, externalTenant); err != nil {
return errors.New("save production kb bindings")
}
return nil