回应 Fasthei 复审(PR #51 CHANGES_REQUESTED): 1. 持久化确认记录(强一致):新增 model.PreflightConfirmation 表 + InsertPreflightConfirmation + PreflightConfirmationExists。confirm 时落库(默认 TTL=HEICODE_PREFLIGHT_CONFIRMATION_TTL_SECONDS =3600s,可设 0 不过期),写失败直接报错(非 best-effort)。部署侧要求该版本存在未过期确认记录 → 杜绝直接拿 GET version 绕过 confirm/审计。 2. 部署重新校验 Ready:verifyDeployPreflight 增加 summary.Ready 检查 —— 预算/agent_slot 等 易变项不进版本哈希,故部署时重查,防 confirm 后余额耗尽/槽位占满仍启动。 3. 版本哈希纳入模板安全面:computePreflightVersion 加 tplDigest(definition+model+name 摘要), 管理员改同一 template_key 的 definition/model 后旧确认失效。补 TestComputePreflightVersion_ChangesOnTemplateEdit。 4. 审计降为附加流:强一致确认记录作为部署门禁;审计 preflight.confirmed 互补。 测试:PreflightConfirmationExists(命中/版本不符/跨用户/过期/不过期/空参)、PreflightBindingKey、 模板变更翻转版本。TestMain + 生产迁移注册 PreflightConfirmation。controller+model 全回归通过。 文档 §4.1.1 更新。 Affects: Manager only(新增 preflight_confirmations 表 + 部署门禁强化)。无计费改动。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
58 lines
1.9 KiB
Go
58 lines
1.9 KiB
Go
package model
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// #41 复审 #1/#4:确认记录强校验 —— 存在/版本不符/过期/跨用户。
|
|
func TestPreflightConfirmationExists(t *testing.T) {
|
|
require.NoError(t, DB.Where("1 = 1").Delete(&PreflightConfirmation{}).Error)
|
|
const now = int64(1_700_000_000_000)
|
|
|
|
require.NoError(t, InsertPreflightConfirmation(&PreflightConfirmation{
|
|
UserID: 7, TemplateID: "architect", BindingKey: "1,2", Version: "pfv1_abc",
|
|
CreatedAtMs: now, ExpiresAtMs: now + 3600_000,
|
|
}))
|
|
|
|
// 命中
|
|
ok, err := PreflightConfirmationExists(7, "architect", "pfv1_abc", now+1000)
|
|
require.NoError(t, err)
|
|
require.True(t, ok)
|
|
|
|
// 版本不符 → 不命中(防止拿别的 version 部署)
|
|
ok, _ = PreflightConfirmationExists(7, "architect", "pfv1_OTHER", now+1000)
|
|
require.False(t, ok)
|
|
|
|
// 跨用户 → 不命中
|
|
ok, _ = PreflightConfirmationExists(9, "architect", "pfv1_abc", now+1000)
|
|
require.False(t, ok)
|
|
|
|
// 过期 → 不命中
|
|
ok, _ = PreflightConfirmationExists(7, "architect", "pfv1_abc", now+7200_000)
|
|
require.False(t, ok)
|
|
|
|
// 空参数 → 不命中
|
|
ok, _ = PreflightConfirmationExists(0, "architect", "pfv1_abc", now)
|
|
require.False(t, ok)
|
|
ok, _ = PreflightConfirmationExists(7, "architect", "", now)
|
|
require.False(t, ok)
|
|
}
|
|
|
|
// 不过期记录(ExpiresAtMs=0)恒命中。
|
|
func TestPreflightConfirmationExists_NoExpiry(t *testing.T) {
|
|
require.NoError(t, DB.Where("1 = 1").Delete(&PreflightConfirmation{}).Error)
|
|
require.NoError(t, InsertPreflightConfirmation(&PreflightConfirmation{
|
|
UserID: 5, TemplateID: "t", Version: "v", CreatedAtMs: 1, ExpiresAtMs: 0,
|
|
}))
|
|
ok, err := PreflightConfirmationExists(5, "t", "v", 9_999_999_999_999)
|
|
require.NoError(t, err)
|
|
require.True(t, ok)
|
|
}
|
|
|
|
func TestPreflightBindingKey_Normalizes(t *testing.T) {
|
|
require.Equal(t, "1,2,3", PreflightBindingKey([]int{3, 1, 2, 3, 0, -1, 1}))
|
|
require.Equal(t, "", PreflightBindingKey(nil))
|
|
}
|