Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(sandbox): rebuild templates without dropping the live ID
wizardchenacac16d540a3
diff --git a/docs/sandbox-cluster.md b/docs/sandbox-cluster.mdindex e35b362f8..b27613f39 100644--- a/docs/sandbox-cluster.md+++ b/docs/sandbox-cluster.md@@ -63,7 +63,7 @@ Cube 变体只发布 linux/amd64——envd 的来源镜像 `cubesandbox-base`1. 按 [CubeSandbox Quick Start](https://github.com/TencentCloud/CubeSandbox/blob/master/docs/zh/guide/quickstart.md) 完成控制面、计算节点、CubeProxy 与域名解析。生产环境还需按官方文档完成鉴权、TLS、网络策略与多节点部署。2. 在 WeKnora 的空间设置中填写 CubeAPI、CubeProxy、sandbox domain 和可选 API Key。需要自定义 guest DNS 时填写「DNS 服务器」(须为 IP);留空则使用集群默认。若这些端点位于 RFC1918/loopback 网络,显式打开“允许访问私网集群地址”。-3. 点击“连接并继续”。WeKnora 先验证控制面地址与凭据,通过后才进入模板步骤并列出集群模板。**不会自动创建**。没有 WeKnora 标准模板时在占位卡片上点「创建」,会从 `wechatopenai/weknora-sandbox:latest-cube` 发起构建。改 DNS 或需要换镜像时在 weknora 卡片上点「重建」:先删掉现有标准模板再用当前配置新建。已有模板按名称 `weknora` 或镜像识别;失败模板同样用「重建」(CubeMaster 拒绝 redo、错误码 130400 时尤其需要)。+3. 点击“连接并继续”。WeKnora 先验证控制面地址与凭据,通过后才进入模板步骤并列出集群模板。**不会自动创建**。没有 WeKnora 标准模板时在占位卡片上点「创建」,会从 `wechatopenai/weknora-sandbox:latest-cube` 发起构建。改 DNS 或需要换镜像时在 weknora 卡片上点「重建」:优先对现有标准模板做 in-place rebuild(模板 ID 不变);只有 redo 被拒绝时才先建新模板、成功后再删旧的。已安装 Skill 的配置(以及同一集群上其它已装 Skill 的配置)不能重建。失败模板同样用「重建」(CubeMaster 拒绝 redo、错误码 130400 时尤其需要)。4. 模板构建状态会自动刷新。状态变为 `READY` 后才可选择并进入运行配置;界面显示模板名称、状态和版本,配置内部才保存该集群自己的 `template_id`。模板镜像必须提供 uid 1000 的 `user` 账号:WeKnora 以该账号执行脚本与文件操作。写权限只保证在 `/workspace/output` 与 `/workspace/input` 下。diff --git a/frontend/src/api/system/index.ts b/frontend/src/api/system/index.tsindex 7c3588cbb..5e6ba451b 100644--- a/frontend/src/api/system/index.ts+++ b/frontend/src/api/system/index.ts@@ -925,9 +925,9 @@ export function getSandboxConfigInventory(id: string): Promise<{ data: SandboxIn/*** Fetch templates using the connection currently entered in the drawer.* `ensure_standard` starts a provider-side build when no WeKnora template is- * present. `replace_standard` deletes the existing WeKnora template first so- * a new spec (DNS, image) can take effect. The returned building item can be- * polled through the same endpoint.+ * present. `replace_standard` rebuilds the WeKnora template so a new spec+ * (DNS, image) can take effect; it requires `config_id`. The returned+ * building item can be polled through the same endpoint.*/export function querySandboxTemplates(payload: {config: SandboxConfigdiff --git a/frontend/src/components/SandboxConfigEditorDrawer.vue b/frontend/src/components/SandboxConfigEditorDrawer.vueindex 34fbb4541..bd0f927eb 100644--- a/frontend/src/components/SandboxConfigEditorDrawer.vue+++ b/frontend/src/components/SandboxConfigEditorDrawer.vue@@ -982,12 +982,23 @@ async function loadTemplates(ensureStandard = false, silent = false, replaceStantemplatesLoaded.value = trueconst standardID = res.data?.standard_template_idconst current = templates.value.find((item) => item.id === currentTemplateId.value)- if (+ if (replaceStandard && standardID) {+ selectTemplate(standardID)+ } else if (currentTemplateId.value- && (!current || !isTemplateSelectable(current))+ && (!current || (!isTemplateSelectable(current) && !isTemplatePending(current)))&& !hasSkillSnapshot.value) {- clearTemplateSelection()+ if (standardID) {+ const next = templates.value.find((item) => item.id === standardID)+ if (next && (isTemplateSelectable(next) || isTemplatePending(next))) {+ selectTemplate(standardID)+ } else {+ clearTemplateSelection()+ }+ } else {+ clearTemplateSelection()+ }}const readyStandard = templates.value.find((item) => item.id === standardID && isTemplateSelectable(item))|| templates.value.find((item) => item.standard && isTemplateSelectable(item))@@ -1013,7 +1024,6 @@ function createStandardTemplate() {function replaceStandardTemplate() {if (hasSkillSnapshot.value) return- clearTemplateSelection()return loadTemplates(false, false, true)}diff --git a/internal/application/service/tenant_sandbox_config.go b/internal/application/service/tenant_sandbox_config.goindex 56c44b67a..e4865f4ea 100644--- a/internal/application/service/tenant_sandbox_config.go+++ b/internal/application/service/tenant_sandbox_config.go@@ -75,6 +75,18 @@ func configHasSkillSnapshot(cfg *types.TenantSandboxConfig) bool {return cfg != nil && cfg.SkillImage != nil && strings.TrimSpace(cfg.SkillImage.SnapshotID) != ""}+// skillRetargetWouldChange reports edits that would retarget the environment+// a skill snapshot is built from: identity, spawn template, or Cube DNS.+func skillRetargetWouldChange(stored, merged *types.TenantSandboxConfig) bool {+ if SandboxIdentityChanged(stored, merged) {+ return true+ }+ if spawnTemplateID(stored) != spawnTemplateID(merged) {+ return true+ }+ return !sameStrings(cubeDNSServers(stored), cubeDNSServers(merged))+}+// spawnTemplateID is the template/image this config would boot without a// skill snapshot. Cube and E2B store it as template_id; Docker as the image.func spawnTemplateID(cfg *types.TenantSandboxConfig) string {@@ -121,16 +133,7 @@ func sameStrings(a, b []string) bool {// environment a skill snapshot was built from: identity, spawn template, or// Cube DNS (which only applies if the template is rebuilt).func skillSnapshotBlocksConnectionChange(stored, merged *types.TenantSandboxConfig) bool {- if !configHasSkillSnapshot(stored) {- return false- }- if SandboxIdentityChanged(stored, merged) {- return true- }- if spawnTemplateID(stored) != spawnTemplateID(merged) {- return true- }- return !sameStrings(cubeDNSServers(stored), cubeDNSServers(merged))+ return configHasSkillSnapshot(stored) && skillRetargetWouldChange(stored, merged)}// ErrSandboxesStillLive is returned when an identity change or deletion is@@ -523,6 +526,10 @@ func (s *TenantSandboxConfigService) Get(func (s *TenantSandboxConfigService) QueryTemplates(ctx context.Context, tenantID uint64, in SandboxTemplateQueryInput,) (*SandboxTemplateCatalog, error) {+ if in.ReplaceStandard && strings.TrimSpace(in.ConfigID) == "" {+ return nil, apperrors.NewBadRequestError(+ "config_id is required to rebuild the standard template")+ }var existing *types.TenantSandboxConfigif strings.TrimSpace(in.ConfigID) != "" {entity, err := s.repo.GetByID(ctx, tenantID, in.ConfigID)@@ -534,9 +541,6 @@ func (s *TenantSandboxConfigService) QueryTemplates(}existing = entity.Config}- if in.ReplaceStandard && configHasSkillSnapshot(existing) {- return nil, ErrSkillSnapshotBlocksTemplateChange- }merged := types.MergeSandboxConfigForUpdate(in.Config, existing)if merged == nil {merged = types.MergeSandboxConfigForUpdate(existing, nil)@@ -544,6 +548,11 @@ func (s *TenantSandboxConfigService) QueryTemplates(if merged == nil {return nil, apperrors.NewBadRequestError("sandbox config is required")}+ if in.ReplaceStandard {+ if err := s.refuseClusterSkillTemplateReplace(ctx, tenantID, merged); err != nil {+ return nil, err+ }+ }// Catalog access needs the control-plane connection but does not need a// spawn template yet. A private placeholder lets the same effective-config@@ -607,13 +616,18 @@ func (s *TenantSandboxConfigService) QueryTemplates(if usable != nil {result.StandardTemplateID = usable.ID}+ oldStandardIDs := standardTemplateIDs(result.Templates)// Listing is read-only. Creating or replacing the WeKnora template is an// explicit settings-page action: auto-ensure on every refresh made DNS// and image changes impossible to apply, and left admins unsure whether// they had asked for a build.wantStandard := in.ReplaceStandard || (in.EnsureStandard && usable == nil)if wantStandard {- key := ensureTemplateKey(tenantID, sandbox.IdentityOf(merged))+ op := "ensure"+ if in.ReplaceStandard {+ op = "replace"+ }+ key := op + ":" + ensureTemplateKey(tenantID, sandbox.IdentityOf(merged))ensured, ensureErr, _ := s.ensureTemplate.Do(key, func() (any, error) {if in.ReplaceStandard {return catalog.ReplaceStandardTemplate(ctx)@@ -628,10 +642,10 @@ func (s *TenantSandboxConfigService) QueryTemplates(return nil, fmt.Errorf("sandbox: provider %q returned no standard template", effective.Type)}if in.ReplaceStandard {- // The listing above still contains the template we just deleted.+ // The listing above still contains the template we just replaced.kept := result.Templates[:0]for _, item := range result.Templates {- if !item.Standard {+ if !item.Standard || item.ID == standard.ID {kept = append(kept, item)}}@@ -641,6 +655,9 @@ func (s *TenantSandboxConfigService) QueryTemplates(if !sandbox.IsTemplateBuildFailed(standard.Status) {result.Provisioned = trueresult.StandardTemplateID = standard.ID+ if in.ReplaceStandard {+ s.persistSpawnTemplateID(ctx, tenantID, merged, standard.ID, oldStandardIDs)+ }} else if in.ReplaceStandard {result.StandardTemplateID = ""}@@ -654,6 +671,129 @@ func (s *TenantSandboxConfigService) QueryTemplates(return result, nil}+func standardTemplateIDs(items []sandbox.RemoteTemplate) []string {+ out := make([]string, 0, len(items))+ for _, item := range items {+ if item.Standard {+ if id := strings.TrimSpace(item.ID); id != "" {+ out = append(out, id)+ }+ }+ }+ return out+}++func (s *TenantSandboxConfigService) refuseClusterSkillTemplateReplace(+ ctx context.Context, tenantID uint64, merged *types.TenantSandboxConfig,+) error {+ identity := sandbox.IdentityOf(merged)+ list, err := s.repo.ListByTenant(ctx, tenantID)+ if err != nil {+ return err+ }+ for _, entity := range list {+ if entity == nil || types.IsSandboxWorkspacePolicyRow(entity) {+ continue+ }+ if sandbox.IdentityOf(entity.Config) != identity {+ continue+ }+ if configHasSkillSnapshot(entity.Config) ||+ s.configHasInFlightSkill(ctx, entity.TenantID, entity.ID) {+ return ErrSkillSnapshotBlocksTemplateChange+ }+ }+ return nil+}++func (s *TenantSandboxConfigService) configHasInFlightSkill(+ ctx context.Context, tenantID uint64, configID string,+) bool {+ if s == nil || s.skills == nil || strings.TrimSpace(configID) == "" {+ return false+ }+ rows, err := s.skills.ListSkillsByConfig(ctx, tenantID, configID)+ if err != nil {+ logger.Warnf(ctx, "[sandbox] cannot read skills of config %s while judging a retarget: %v",+ configID, err)+ return true+ }+ for _, row := range rows {+ if row == nil {+ continue+ }+ switch row.Status {+ case types.SkillStatusInstalling, types.SkillStatusRemoving:+ return true+ }+ }+ return false+}++func (s *TenantSandboxConfigService) persistSpawnTemplateID(+ ctx context.Context,+ tenantID uint64,+ merged *types.TenantSandboxConfig,+ newID string,+ oldIDs []string,+) {+ newID = strings.TrimSpace(newID)+ if newID == "" || s == nil || s.repo == nil {+ return+ }+ old := make(map[string]struct{}, len(oldIDs))+ for _, id := range oldIDs {+ if id = strings.TrimSpace(id); id != "" {+ old[id] = struct{}{}+ }+ }+ identity := sandbox.IdentityOf(merged)+ list, err := s.repo.ListByTenant(ctx, tenantID)+ if err != nil {+ logger.Warnf(ctx, "[sandbox] persist rebuilt template id: list configs: %v", err)+ return+ }+ for _, entity := range list {+ if entity == nil || entity.Config == nil || types.IsSandboxWorkspacePolicyRow(entity) {+ continue+ }+ if sandbox.IdentityOf(entity.Config) != identity {+ continue+ }+ current := spawnTemplateID(entity.Config)+ if current == newID {+ continue+ }+ if current != "" {+ if _, pointedAtOld := old[current]; !pointedAtOld {+ continue+ }+ }+ setSpawnTemplateID(entity.Config, newID)+ if err := s.repo.Update(ctx, entity); err != nil {+ logger.Warnf(ctx, "[sandbox] persist rebuilt template id on config %s: %v", entity.ID, err)+ continue+ }+ logger.Infof(ctx, "[sandbox] config %s spawn template is now %s after rebuild", entity.ID, newID)+ }+}++func setSpawnTemplateID(cfg *types.TenantSandboxConfig, id string) {+ if cfg == nil {+ return+ }+ switch sandbox.SandboxType(cfg.SandboxType) {+ case sandbox.SandboxTypeCube:+ if cfg.Cube != nil {+ cfg.Cube.TemplateID = id+ }+ case sandbox.SandboxTypeE2B:+ if cfg.E2B != nil {+ cfg.E2B.TemplateID = id+ }+ }+}+// pickStandardTemplate returns the WeKnora template the UI should preselect, or// nil when the cluster has none that could ever spawn a sandbox. A failed build// is skipped so the caller can reprovision instead of offering it.@@ -791,7 +931,8 @@ func (s *TenantSandboxConfigService) Update(if err := validateNamedSandboxBackend(merged); err != nil {return nil, err}- if skillSnapshotBlocksConnectionChange(entity.Config, merged) {+ if skillSnapshotBlocksConnectionChange(entity.Config, merged) ||+ (skillRetargetWouldChange(entity.Config, merged) && s.configHasInFlightSkill(ctx, tenantID, id)) {return nil, ErrSkillSnapshotBlocksTemplateChange}if !SandboxIdentityChanged(entity.Config, merged) {@@ -939,6 +1080,8 @@ func resolveAbandonedBuildIDs(logger.Warnf(ctx, "[sandbox] list snapshots to release abandoned builds failed: %v", err)return}+ prefix := skillSnapshotNamePrefix(unresolved[0].TenantID, unresolved[0].SandboxConfigID)+ listed = snapshotsNotFromOtherConfig(listed, prefix)for _, row := range unresolved {id := matchSnapshotByName(listed, row.PlannedName)if id == "" {diff --git a/internal/application/service/tenant_sandbox_config_test.go b/internal/application/service/tenant_sandbox_config_test.goindex e9245dfd8..e9700194f 100644--- a/internal/application/service/tenant_sandbox_config_test.go+++ b/internal/application/service/tenant_sandbox_config_test.go@@ -207,6 +207,7 @@ func TestSandboxIdentityChangedJudgesUnreachableOldEndpoint(t *testing.T) {type fakeConfigRepo struct {entity *types.TenantSandboxConfigEntitypolicy *types.TenantSandboxConfigEntity+ others []*types.TenantSandboxConfigEntityevents []stringupdated *types.TenantSandboxConfigEntity@@ -244,6 +245,7 @@ func (f *fakeConfigRepo) ListByTenant(if f.policy != nil {out = append(out, f.policy)}+ out = append(out, f.others...)return out, nil}@@ -332,6 +334,9 @@ func (s *stubProviderClient) EnsureStandardTemplate(context.Context) (*sandbox.Rfunc (s *stubProviderClient) ReplaceStandardTemplate(ctx context.Context) (*sandbox.RemoteTemplate, error) {s.replaceCalls.Add(1)+ if s.ensureDelay > 0 {+ time.Sleep(s.ensureDelay)+ }for _, item := range s.templates {if item.Standard && item.ID != "" {s.deletedTemplates = append(s.deletedTemplates, item.ID)@@ -511,11 +516,10 @@ func TestQueryTemplatesSurfacesFailedEnsureWithoutProvisioning(t *testing.T) {require.Equal(t, "TOOMANYREQUESTS", result.Templates[0].Error)}-func TestQueryTemplatesReplaceStandardDeletesExisting(t *testing.T) {+func TestQueryTemplatesReplaceStandardRequiresConfigID(t *testing.T) {client := &stubProviderClient{templates: []sandbox.RemoteTemplate{{ID: "tpl-old", Name: "weknora", Status: "ready", Standard: true},- {ID: "tpl-custom", Name: "custom", Status: "ready"},},replaced: &sandbox.RemoteTemplate{ID: "tpl-new", Name: "weknora", Status: "building", Standard: true,@@ -523,10 +527,35 @@ func TestQueryTemplatesReplaceStandardDeletesExisting(t *testing.T) {}svc := newTestConfigService(t, &fakeConfigRepo{}, client, stubAgentRepo{})- result, err := svc.QueryTemplates(context.Background(), 7, SandboxTemplateQueryInput{+ _, err := svc.QueryTemplates(context.Background(), 7, SandboxTemplateQueryInput{Config: e2bCfg("key-a", "https://api.e2b.app", "e2b.app", "", 300),ReplaceStandard: true,})+ require.Error(t, err)+ require.Equal(t, int32(0), client.replaceCalls.Load())+}++func TestQueryTemplatesReplaceStandardPersistsNewTemplateID(t *testing.T) {+ stored := e2bCfg("key-a", "https://api.e2b.app", "e2b.app", "tpl-old", 300)+ repo := &fakeConfigRepo{entity: &types.TenantSandboxConfigEntity{+ ID: "cfg-a", TenantID: 7, SandboxType: "e2b", Config: stored,+ }}+ client := &stubProviderClient{+ templates: []sandbox.RemoteTemplate{+ {ID: "tpl-old", Name: "weknora", Status: "ready", Standard: true},+ {ID: "tpl-custom", Name: "custom", Status: "ready"},+ },+ replaced: &sandbox.RemoteTemplate{+ ID: "tpl-new", Name: "weknora", Status: "building", Standard: true,+ },+ }+ svc := newTestConfigService(t, repo, client, stubAgentRepo{})++ result, err := svc.QueryTemplates(context.Background(), 7, SandboxTemplateQueryInput{+ ConfigID: "cfg-a",+ Config: stored,+ ReplaceStandard: true,+ })require.NoError(t, err)require.Equal(t, int32(1), client.replaceCalls.Load())@@ -534,11 +563,44 @@ func TestQueryTemplatesReplaceStandardDeletesExisting(t *testing.T) {require.Equal(t, []string{"tpl-old"}, client.deletedTemplates)require.True(t, result.Provisioned)require.Equal(t, "tpl-new", result.StandardTemplateID)+ require.Equal(t, "tpl-new", repo.entity.Config.E2B.TemplateID,+ "rebuild must write the new template id so sessions do not keep targeting the deleted one")require.Len(t, result.Templates, 2)require.Equal(t, "tpl-new", result.Templates[0].ID)require.Equal(t, "tpl-custom", result.Templates[1].ID)}+func TestQueryTemplatesReplaceStandardRefusesWhenSiblingHasSkillSnapshot(t *testing.T) {+ stored := e2bCfg("key-a", "https://api.e2b.app", "e2b.app", "t1", 300)+ sibling := e2bCfg("key-a", "https://api.e2b.app", "e2b.app", "t1", 300)+ sibling.SkillImage = &types.SkillImageConfig{SnapshotID: "snap-1"}+ repo := &fakeConfigRepo{+ entity: &types.TenantSandboxConfigEntity{+ ID: "cfg-a", TenantID: 7, SandboxType: "e2b", Config: stored,+ },+ others: []*types.TenantSandboxConfigEntity{{+ ID: "cfg-b", TenantID: 7, SandboxType: "e2b", Config: sibling,+ }},+ }+ client := &stubProviderClient{+ templates: []sandbox.RemoteTemplate{+ {ID: "t1", Name: "weknora", Status: "ready", Standard: true},+ },+ replaced: &sandbox.RemoteTemplate{+ ID: "tpl-new", Name: "weknora", Status: "building", Standard: true,+ },+ }+ svc := newTestConfigService(t, repo, client, stubAgentRepo{})++ _, err := svc.QueryTemplates(context.Background(), 7, SandboxTemplateQueryInput{+ ConfigID: "cfg-a",+ Config: stored,+ ReplaceStandard: true,+ })+ require.ErrorIs(t, err, ErrSkillSnapshotBlocksTemplateChange)+ require.Equal(t, int32(0), client.replaceCalls.Load())+}+func TestQueryTemplatesReplaceStandardRefusesWhenSkillSnapshotExists(t *testing.T) {stored := e2bCfg("key-a", "https://api.e2b.app", "e2b.app", "t1", 300)stored.SkillImage = &types.SkillImageConfig{SnapshotID: "snap-1"}@@ -609,6 +671,73 @@ func TestUpdateRefusesIdentityChangeWhenSkillSnapshotExists(t *testing.T) {require.Nil(t, repo.updated)}+func TestUpdateRefusesIdentityChangeWhileSkillIsInstalling(t *testing.T) {+ t.Setenv("SYSTEM_AES_KEY", strings.Repeat("k", 32))+ stored := e2bCfg("key-a", "https://api.e2b.app", "e2b.app", "t1", 300)+ repo := &fakeConfigRepo{entity: &types.TenantSandboxConfigEntity{+ ID: "cfg-a",+ TenantID: 7,+ Name: "prod",+ SandboxType: "e2b",+ Config: stored,+ }}+ svc := newTestConfigService(t, repo, &stubProviderClient{}, stubAgentRepo{})+ svc.skills = &deleteSkillStore{skills: []*types.TenantSkillEntity{{+ ID: "sk-1", TenantID: 7, SandboxConfigID: "cfg-a",+ Status: types.SkillStatusInstalling,+ }}}++ _, err := svc.Update(context.Background(), 7, "cfg-a", UpdateSandboxConfigInput{+ Name: "prod",+ Config: e2bCfg("key-b", "https://api.e2b.app", "e2b.app", "t1", 300),+ })+ require.ErrorIs(t, err, ErrSkillSnapshotBlocksTemplateChange)+ require.Nil(t, repo.updated)+}++func TestQueryTemplatesEnsureAndReplaceDoNotShareSingleflight(t *testing.T) {+ stored := e2bCfg("key-a", "https://api.e2b.app", "e2b.app", "t1", 300)+ repo := &fakeConfigRepo{entity: &types.TenantSandboxConfigEntity{+ ID: "cfg-a", TenantID: 7, SandboxType: "e2b", Config: stored,+ }}+ client := &stubProviderClient{+ templates: []sandbox.RemoteTemplate{+ {ID: "tpl-broken", Name: "weknora", Status: "failed", Standard: true},+ },+ ensureDelay: 80 * time.Millisecond,+ ensured: &sandbox.RemoteTemplate{+ ID: "tpl-ensured", Name: "weknora", Status: "building", Standard: true,+ },+ replaced: &sandbox.RemoteTemplate{+ ID: "tpl-replaced", Name: "weknora", Status: "building", Standard: true,+ },+ }+ svc := newTestConfigService(t, repo, client, stubAgentRepo{})++ errCh := make(chan error, 2)+ go func() {+ _, err := svc.QueryTemplates(context.Background(), 7, SandboxTemplateQueryInput{+ ConfigID: "cfg-a",+ Config: stored,+ EnsureStandard: true,+ })+ errCh <- err+ }()+ go func() {+ time.Sleep(20 * time.Millisecond)+ _, err := svc.QueryTemplates(context.Background(), 7, SandboxTemplateQueryInput{+ ConfigID: "cfg-a",+ Config: stored,+ ReplaceStandard: true,+ })+ errCh <- err+ }()+ require.NoError(t, <-errCh)+ require.NoError(t, <-errCh)+ require.Equal(t, int32(1), client.ensureCalls.Load())+ require.Equal(t, int32(1), client.replaceCalls.Load())+}+func TestUpdateRefusesDNSChangeWhenSkillSnapshotExists(t *testing.T) {t.Setenv("SYSTEM_AES_KEY", strings.Repeat("k", 32))stored := cubeCfg("key-a", "https://203.0.113.10", "https://203.0.113.11", "cube.app")diff --git a/internal/application/service/tenant_skill_install.go b/internal/application/service/tenant_skill_install.goindex 6c1c3b427..5362ed81e 100644--- a/internal/application/service/tenant_skill_install.go+++ b/internal/application/service/tenant_skill_install.go@@ -365,7 +365,7 @@ func (s *TenantSkillService) runInstall(// so a run that dies during the commit still leaves the ledger able to// name what it was building. Without it the snapshot would be a provider// resource nothing could ever address, let alone reclaim.- snapshotName := skillSnapshotBuildName(configID, generation)+ snapshotName := skillSnapshotBuildName(tenantID, configID, generation)if err := s.skills.CreateSnapshotRow(ctx, &types.TenantSkillSnapshotEntity{ID: installRowID, TenantID: tenantID, SandboxConfigID: configID, SkillID: skillID,ParentSnapshotID: currentSnapshotID(cfgEntity), Generation: generation,@@ -1329,23 +1329,86 @@ func currentSnapshotID(cfgEntity *types.TenantSandboxConfigEntity) string {return cfgEntity.Config.SkillImage.SnapshotID}+func skillSnapshotNamePrefix(tenantID uint64, configID string) string {+ return fmt.Sprintf("weknora-sk-t%d-%s", tenantID, compactConfigID(configID))+}+// skillSnapshotBuildName is the name every generation of a config's image// chain is committed under. It is deterministic so the abandoned-build sweep// can match a provider listing against a ledger row that never learned its-// snapshot ID.-func skillSnapshotBuildName(configID string, generation int) string {- return fmt.Sprintf("weknora-sk-%s-g%d", shortID(configID), generation)+// snapshot ID. Tenant and the full config id are in the name because Cube,+// E2B and Docker all list snapshots across a shared account or daemon; a+// short config prefix would let two configs overwrite or reclaim each+// other's images.+func skillSnapshotBuildName(tenantID uint64, configID string, generation int) string {+ return fmt.Sprintf("%s-g%d", skillSnapshotNamePrefix(tenantID, configID), generation)+}++func compactConfigID(id string) string {+ return strings.ToLower(strings.ReplaceAll(strings.TrimSpace(id), "-", ""))+}++// weknoraSkillSnapshotName pulls the weknora-sk-… token out of a provider+// listing. Cube and E2B echo it in Names; Docker embeds it in the image tag.+func weknoraSkillSnapshotName(raw string) string {+ s := strings.TrimSpace(raw)+ s = strings.TrimPrefix(s, "docker.io/")+ if i := strings.LastIndex(s, "/"); i >= 0 {+ s = s[i+1:]+ }+ if cut := strings.IndexByte(s, ':'); cut >= 0 {+ s = s[:cut]+ }+ if strings.HasPrefix(s, "weknora-sk-") {+ return s+ }+ return ""}-func shortID(id string) string {- trimmed := strings.ReplaceAll(strings.TrimSpace(id), "-", "")- if len(trimmed) > 8 {- return trimmed[:8]+// snapshotsNotFromOtherConfig drops provider listings that already name a+// different WeKnora config. Cube, E2B and Docker all ListSnapshots across the+// whole account/daemon, so without this a reconcile of one config would treat+// every other config's image as an extra, and an abandoned-build match could+// bind to the wrong snapshot.+func snapshotsNotFromOtherConfig(+ listed []sandbox.RemoteSnapshotRef, prefix string,+) []sandbox.RemoteSnapshotRef {+ if strings.TrimSpace(prefix) == "" {+ return listed+ }+ out := make([]sandbox.RemoteSnapshotRef, 0, len(listed))+ for _, snap := range listed {+ if snapshotBelongsToOtherConfig(snap, prefix) {+ continue+ }+ out = append(out, snap)+ }+ return out+}++func snapshotBelongsToOtherConfig(snap sandbox.RemoteSnapshotRef, prefix string) bool {+ if strings.TrimSpace(prefix) == "" {+ return false}- if trimmed == "" {- return "config"+ needle := prefix + "-g"+ sawForeign := false+ for _, candidate := range append([]string{snap.ID}, snap.Names...) {+ name := weknoraSkillSnapshotName(candidate)+ if name == "" {+ continue+ }+ if strings.HasPrefix(name, needle) {+ return false+ }+ // New-format names are weknora-sk-t<tenant>-<config>-gN. Legacy+ // weknora-sk-<short>-gN names are left alone so a row written before+ // the prefix existed can still be matched.+ rest := strings.TrimPrefix(name, "weknora-sk-")+ if len(rest) > 1 && rest[0] == 't' && rest[1] >= '0' && rest[1] <= '9' {+ sawForeign = true+ }}- return trimmed+ return sawForeign}func buildInstallPrompt(skillDir string, bundle *SkillBundle, uvAvailable bool) string {diff --git a/internal/application/service/tenant_skill_install_test.go b/internal/application/service/tenant_skill_install_test.goindex 7294f2cfe..9985bc567 100644--- a/internal/application/service/tenant_skill_install_test.go+++ b/internal/application/service/tenant_skill_install_test.go@@ -89,6 +89,15 @@ func TestRunInstallSucceedsOnDockerConfig(t *testing.T) {require.Equal(t, fx.fingerprint, fx.configRepo.saved.Config.SkillImage.OwnerFingerprint)}+func TestSkillSnapshotBuildNameIncludesTenantAndFullConfig(t *testing.T) {+ a := skillSnapshotBuildName(7, "aaaaaaaa-bbbb-cccc-dddd-eeeeffff0001", 1)+ b := skillSnapshotBuildName(8, "aaaaaaaa-bbbb-cccc-dddd-eeeeffff0001", 1)+ c := skillSnapshotBuildName(7, "aaaaaaaa-bbbb-cccc-dddd-eeeeffff0002", 1)+ require.Equal(t, "weknora-sk-t7-aaaaaaaabbbbccccddddeeeeffff0001-g1", a)+ require.NotEqual(t, a, b, "the same config in another tenant must not share a tag")+ require.NotEqual(t, a, c, "two configs must not share a tag")+}+// TestRunInstallIssuesExactlyTheseCommands pins the order, not just the set.// Ownership and permissions are normalised BEFORE verification on purpose: the// agent creates the tree as root, so a restrictive root umask would leave thediff --git a/internal/application/service/tenant_skill_reaper.go b/internal/application/service/tenant_skill_reaper.goindex 67b8b20a7..9aa7e3f85 100644--- a/internal/application/service/tenant_skill_reaper.go+++ b/internal/application/service/tenant_skill_reaper.go@@ -310,6 +310,7 @@ func (s *TenantSkillService) ReconcileSnapshots(if err != nil {return 0, err}+ listed = snapshotsNotFromOtherConfig(listed, skillSnapshotNamePrefix(tenantID, configID))extras := 0for _, snap := range listed {id := strings.TrimSpace(snap.ID)@@ -551,6 +552,7 @@ func (s *TenantSkillService) reapAbandonedBuilds(cfg.ID, err)return 0}+ listed = snapshotsNotFromOtherConfig(listed, skillSnapshotNamePrefix(cfg.TenantID, cfg.ID))live := strings.TrimSpace(currentSnapshotID(cfg))reaped := 0diff --git a/internal/application/service/tenant_skill_reaper_test.go b/internal/application/service/tenant_skill_reaper_test.goindex 7522e4af6..b20c8d419 100644--- a/internal/application/service/tenant_skill_reaper_test.go+++ b/internal/application/service/tenant_skill_reaper_test.go@@ -553,6 +553,41 @@ func TestConfiguredSandboxTTLCoversTheDockerIdleWindow(t *testing.T) {}), "an unset idle TTL leaves the retention floor in charge")}+func TestSnapshotBelongsToOtherConfig(t *testing.T) {+ prefix := skillSnapshotNamePrefix(7, "cfg-1")+ dockerOurs := sandbox.RemoteSnapshotRef{+ ID: "weknora-skill/weknora-sk-t7-cfg1-g1", Names: []string{"weknora-skill/weknora-sk-t7-cfg1-g1"},+ }+ dockerTheirs := sandbox.RemoteSnapshotRef{+ ID: "weknora-skill/weknora-sk-t7-cfg2-g1", Names: []string{"weknora-skill/weknora-sk-t7-cfg2-g1"},+ }+ cubeOurs := sandbox.RemoteSnapshotRef{ID: "snap-ours", Names: []string{"weknora-sk-t7-cfg1-g1"}}+ cubeTheirs := sandbox.RemoteSnapshotRef{ID: "snap-theirs", Names: []string{"weknora-sk-t8-cfg1-g1"}}+ e2bTheirs := sandbox.RemoteSnapshotRef{ID: "tpl-other", Names: []string{"weknora-sk-t7-aaaa-g3"}}+ legacy := sandbox.RemoteSnapshotRef{ID: "snap-legacy", Names: []string{"weknora-sk-cfg1-g2"}}+ unnamed := sandbox.RemoteSnapshotRef{ID: "snap-plain"}++ require.False(t, snapshotBelongsToOtherConfig(dockerOurs, prefix))+ require.True(t, snapshotBelongsToOtherConfig(dockerTheirs, prefix))+ require.False(t, snapshotBelongsToOtherConfig(cubeOurs, prefix))+ require.True(t, snapshotBelongsToOtherConfig(cubeTheirs, prefix),+ "a Cube snapshot named for another tenant must not be this config's extra")+ require.True(t, snapshotBelongsToOtherConfig(e2bTheirs, prefix))+ require.False(t, snapshotBelongsToOtherConfig(legacy, prefix),+ "legacy names without a tenant prefix must still be matchable")+ require.False(t, snapshotBelongsToOtherConfig(unnamed, prefix),+ "a listing that does not echo our name is not classified as another config")++ kept := snapshotsNotFromOtherConfig([]sandbox.RemoteSnapshotRef{+ dockerOurs, dockerTheirs, cubeOurs, cubeTheirs, legacy, unnamed,+ }, prefix)+ ids := make([]string, 0, len(kept))+ for _, snap := range kept {+ ids = append(ids, snap.ID)+ }+ require.Equal(t, []string{"weknora-skill/weknora-sk-t7-cfg1-g1", "snap-ours", "snap-legacy", "snap-plain"}, ids)+}+func TestTenantSkillServiceStartIsIdempotent(t *testing.T) {svc := NewTenantSkillService(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)diff --git a/internal/application/service/tenant_skill_remove.go b/internal/application/service/tenant_skill_remove.goindex a2db25275..a1568e26c 100644--- a/internal/application/service/tenant_skill_remove.go+++ b/internal/application/service/tenant_skill_remove.go@@ -227,7 +227,7 @@ func (s *TenantSkillService) runRemove(// The ledger row is written before the snapshot: a snapshot with no ledger// entry is a provider resource nobody knows exists.removeRowID := uuid.NewString()- snapshotName := skillSnapshotBuildName(configID, generation)+ snapshotName := skillSnapshotBuildName(tenantID, configID, generation)if err := s.skills.CreateSnapshotRow(ctx, &types.TenantSkillSnapshotEntity{ID: removeRowID, TenantID: tenantID, SandboxConfigID: configID, SkillID: skillID,ParentSnapshotID: currentSnapshotID(cfgEntity), Generation: generation,diff --git a/internal/handler/sandbox_config.go b/internal/handler/sandbox_config.goindex aac83956b..98e4ddcd0 100644--- a/internal/handler/sandbox_config.go+++ b/internal/handler/sandbox_config.go@@ -36,8 +36,8 @@ type sandboxTemplateQueryRequest struct {// QueryTemplates returns the templates visible through an unsaved workspace// connection. ensure_standard starts a build only when the cluster has no-// usable WeKnora template; replace_standard deletes that template first so-// a new spec (DNS, image) can take effect.+// usable WeKnora template; replace_standard rebuilds that template so a+// new spec (DNS, image) can take effect. replace_standard requires config_id.func (h *SandboxConfigHandler) QueryTemplates(c *gin.Context) {var req sandboxTemplateQueryRequestif err := c.ShouldBindJSON(&req); err != nil {diff --git a/internal/sandbox/cube_remote_client.go b/internal/sandbox/cube_remote_client.goindex a17d253b0..489956d61 100644--- a/internal/sandbox/cube_remote_client.go+++ b/internal/sandbox/cube_remote_client.go@@ -246,27 +246,60 @@ func (c *CubeRemoteClient) EnsureStandardTemplate(ctx context.Context) (*RemoteTreturn c.buildStandardTemplate(ctx)}-// ReplaceStandardTemplate deletes every WeKnora template on the cluster and-// builds a fresh one from the current spec. Cube bakes DNS and-// allowInternetAccess into the template at create time, so a READY template-// cannot pick up a settings change any other way.+// ReplaceStandardTemplate applies the current spec to the cluster's WeKnora+// template. Cube bakes DNS into the template, so a READY template only picks+// up a settings change via rebuild (preferred: same ID) or a replacement.+//+// The old template is never deleted until a replacement with an ID exists:+// deleting first left every config's stored template_id pointing at a+// missing template for the duration of the build.func (c *CubeRemoteClient) ReplaceStandardTemplate(ctx context.Context) (*RemoteTemplate, error) {items, err := c.ListTemplates(ctx)if err != nil {return nil, err}+ var current *RemoteTemplate+ for i := range items {+ if !items[i].Standard || strings.TrimSpace(items[i].ID) == "" {+ continue+ }+ item := items[i]+ if current == nil {+ current = &item+ continue+ }+ if IsTemplateBuildFailed(current.Status) && !IsTemplateBuildFailed(item.Status) {+ current = &item+ }+ }+ if current != nil {+ rebuilt, err := c.tryRebuildStandardTemplate(ctx, *current)+ if err == nil {+ return rebuilt, nil+ }+ logger.Warnf(ctx,+ "cube in-place rebuild of standard template %s failed: %v; building a replacement",+ current.ID, err)+ }+ created, err := c.buildStandardTemplate(ctx)+ if err != nil {+ return nil, err+ }+ if created == nil || strings.TrimSpace(created.ID) == "" || IsTemplateBuildFailed(created.Status) {+ return created, nil+ }for _, item := range items {- if !item.Standard || strings.TrimSpace(item.ID) == "" {+ if !item.Standard || strings.TrimSpace(item.ID) == "" || item.ID == created.ID {continue}- logger.Infof(ctx, "cube replacing standard template %s", item.ID)+ logger.Infof(ctx, "cube deleting superseded standard template %s", item.ID)if err := c.client.DeleteTemplate(ctx, item.ID); err != nil {if normalized := normalizeCubeError("DeleteTemplate", err); !IsRemoteNotFound(normalized) {- return nil, normalized+ logger.Warnf(ctx, "cube delete of replaced template %s failed: %v", item.ID, err)}}}- return c.buildStandardTemplate(ctx)+ return created, nil}// rebuildStandardTemplate restarts the build of a template that already exists,@@ -275,15 +308,26 @@ func (c *CubeRemoteClient) rebuildStandardTemplate(ctx context.Context,current RemoteTemplate,) (*RemoteTemplate, error) {- logger.Infof(ctx, "cube standard template %s failed (%s), rebuilding in place",- current.ID, current.Status)- job, err := c.client.RebuildTemplate(ctx, current.ID, c.standardTemplateSpec())+ rebuilt, err := c.tryRebuildStandardTemplate(ctx, current)if err != nil {// The failed template is still in the catalog; returning it lets the// settings page show lastError instead of 500ing the whole query.logger.Warnf(ctx, "cube rebuild of standard template %s failed: %v", current.ID, err)return ¤t, nil}+ return rebuilt, nil+}++func (c *CubeRemoteClient) tryRebuildStandardTemplate(+ ctx context.Context,+ current RemoteTemplate,+) (*RemoteTemplate, error) {+ logger.Infof(ctx, "cube rebuilding standard template %s in place (%s)",+ current.ID, current.Status)+ job, err := c.client.RebuildTemplate(ctx, current.ID, c.standardTemplateSpec())+ if err != nil {+ return nil, normalizeCubeError("RebuildTemplate", err)+ }rebuilt := currentrebuilt.Status = normalizeCubeTemplateStatus(job.Status)if rebuilt.Status == "" {diff --git a/internal/sandbox/cube_template_catalog_test.go b/internal/sandbox/cube_template_catalog_test.goindex b8c2626de..d0fc3c06e 100644--- a/internal/sandbox/cube_template_catalog_test.go+++ b/internal/sandbox/cube_template_catalog_test.go@@ -390,8 +390,9 @@ func TestCubeRemoteClientEnsureStandardTemplateIncludesConfiguredDNS(t *testing.require.Equal(t, []any{"8.8.8.8", "1.1.1.1"}, payload["dns"])}-func TestCubeRemoteClientReplaceStandardTemplateDeletesThenBuilds(t *testing.T) {+func TestCubeRemoteClientReplaceStandardTemplateRebuildsInPlace(t *testing.T) {var deleted atomic.Int32+ var created atomic.Int32var payload map[string]anyclient := newCubeTemplateClient(t, func(w http.ResponseWriter, r *http.Request) {switch {@@ -402,11 +403,17 @@ func TestCubeRemoteClientReplaceStandardTemplateDeletesThenBuilds(t *testing.T)"imageInfo": DefaultDockerImage,"aliases": []string{StandardTemplateName},}})+ case r.URL.Path == "/templates/tpl-old" && r.Method == http.MethodPost:+ require.NoError(t, json.NewDecoder(r.Body).Decode(&payload))+ writeJSON(w, http.StatusAccepted, map[string]any{+ "templateID": "tpl-old",+ "status": "PENDING",+ })case r.URL.Path == "/templates/tpl-old" && r.Method == http.MethodDelete:deleted.Add(1)w.WriteHeader(http.StatusNoContent)case r.URL.Path == "/templates" && r.Method == http.MethodPost:- require.NoError(t, json.NewDecoder(r.Body).Decode(&payload))+ created.Add(1)writeJSON(w, http.StatusAccepted, map[string]any{"templateID": "tpl-new","status": "PENDING",@@ -421,13 +428,54 @@ func TestCubeRemoteClientReplaceStandardTemplateDeletesThenBuilds(t *testing.T)template, err := client.ReplaceStandardTemplate(context.Background())require.NoError(t, err)- require.Equal(t, "tpl-new", template.ID)+ require.Equal(t, "tpl-old", template.ID)require.Equal(t, "building", template.Status)- require.Equal(t, int32(1), deleted.Load())+ require.Equal(t, int32(0), deleted.Load(), "in-place rebuild must keep the stored template ID")+ require.Equal(t, int32(0), created.Load())require.Equal(t, []any{"192.0.2.53"}, payload["dns"])require.Equal(t, true, payload["allowInternetAccess"])}+func TestCubeRemoteClientReplaceStandardTemplateBuildsThenDeletesWhenRedoBlocked(t *testing.T) {+ var deleted atomic.Int32+ var payload map[string]any+ client := newCubeTemplateClient(t, func(w http.ResponseWriter, r *http.Request) {+ switch {+ case r.URL.Path == "/templates" && r.Method == http.MethodGet:+ writeJSON(w, http.StatusOK, []map[string]any{{+ "templateID": "tpl-old",+ "status": "READY",+ "imageInfo": DefaultDockerImage,+ "aliases": []string{StandardTemplateName},+ }})+ case r.URL.Path == "/templates/tpl-old" && r.Method == http.MethodPost:+ writeJSON(w, http.StatusBadRequest, map[string]any{+ "code": 400,+ "message": "template redo is not allowed",+ })+ case r.URL.Path == "/templates" && r.Method == http.MethodPost:+ require.NoError(t, json.NewDecoder(r.Body).Decode(&payload))+ writeJSON(w, http.StatusAccepted, map[string]any{+ "templateID": "tpl-new",+ "status": "PENDING",+ })+ case r.URL.Path == "/templates/tpl-old" && r.Method == http.MethodDelete:+ deleted.Add(1)+ w.WriteHeader(http.StatusNoContent)+ case r.URL.Path == "/snapshots" && r.Method == http.MethodGet:+ writeJSON(w, http.StatusOK, []map[string]any{})+ default:+ http.NotFound(w, r)+ }+ })++ template, err := client.ReplaceStandardTemplate(context.Background())+ require.NoError(t, err)+ require.Equal(t, "tpl-new", template.ID)+ require.Equal(t, int32(1), deleted.Load(), "the old template is deleted only after the replacement has an ID")+ require.Equal(t, DefaultCubeTemplateImage, payload["image"])+}+// The plain and Cube images share a repository, so a template built from either// one is still recognised as ours — which is what lets an existing template// built from the envd-less image be rebuilt in place rather than duplicated.diff --git a/internal/sandbox/docker_engine.go b/internal/sandbox/docker_engine.goindex 64f144fe0..c5e07d13b 100644--- a/internal/sandbox/docker_engine.go+++ b/internal/sandbox/docker_engine.go@@ -89,9 +89,10 @@ var _ dockerEngineAPI = (*client.Client)(nil)const DefaultDockerHost = "unix:///var/run/docker.sock"// DefaultDockerHTTPTimeout bounds a single short Engine API call (ping, create,-// inspect, list, delete, exec create/inspect). Streaming calls — image pull,-// exec hijack, archive copy — use the caller's context instead, because-// http.Client.Timeout would kill them mid-body.+// inspect, list, exec create/inspect). Streaming and long storage calls —+// image pull, image remove with prune, commit, exec hijack, archive copy —+// use the caller's context instead, because a 30s deadline would kill them+// mid-body.const DefaultDockerHTTPTimeout = 30 * time.Second// DefaultDockerIdleTTL is how long a session container may go without an execdiff --git a/internal/sandbox/docker_rpc_timeout.go b/internal/sandbox/docker_rpc_timeout.goindex 34e8f1c1f..0fb892d7a 100644--- a/internal/sandbox/docker_rpc_timeout.go+++ b/internal/sandbox/docker_rpc_timeout.go@@ -7,10 +7,11 @@ import ("github.com/moby/moby/client")-// withDockerRPCTimeout bounds short Engine API calls. Streaming methods are-// left on the caller's context: http.Client.Timeout (and a blanket RPC-// deadline) would abort a pull or hijacked exec after the budget, which is-// exactly how the previous 30s client timeout broke cold image pulls.+// withDockerRPCTimeout bounds short Engine API calls. Streaming and long+// storage methods (pull, commit, image remove) are left on the caller's+// context: http.Client.Timeout (and a blanket RPC deadline) would abort them+// after the budget, which is exactly how the previous 30s client timeout+// broke cold image pulls.func withDockerRPCTimeout(inner dockerEngineAPI, timeout time.Duration) dockerEngineAPI {if inner == nil || timeout <= 0 {return inner@@ -138,9 +139,10 @@ func (a *dockerRPCTimeoutAPI) ImageList(func (a *dockerRPCTimeoutAPI) ImageRemove(ctx context.Context, imageID string, options client.ImageRemoveOptions,) (client.ImageRemoveResult, error) {- rpcCtx, cancel := a.rpcCtx(ctx)- defer cancel()- return a.inner.ImageRemove(rpcCtx, imageID, options)+ // PruneChildren on a retired skill chain can run well past the short+ // RPC budget, the way a commit or pull does. Timing out here would+ // leave the ledger unmarked and the layers on disk.+ return a.inner.ImageRemove(ctx, imageID, options)}func (a *dockerRPCTimeoutAPI) ContainerCommit(diff --git a/internal/sandbox/e2b_remote_client.go b/internal/sandbox/e2b_remote_client.goindex 8d97a61a3..526be4bc1 100644--- a/internal/sandbox/e2b_remote_client.go+++ b/internal/sandbox/e2b_remote_client.go@@ -334,27 +334,53 @@ func (c *E2BRemoteClient) EnsureStandardTemplate(ctx context.Context) (*RemoteTereturn c.buildStandardTemplate(ctx)}-// ReplaceStandardTemplate deletes the WeKnora template and starts a new build-// so a later image (or an untagged leftover) can actually replace what is-// already READY.+// ReplaceStandardTemplate starts a new WeKnora-template build from the current+// spec. E2B resolves builds by name, so this is often a rebuild of the same+// ID. Old standard templates are deleted only after a replacement with an ID+// exists — deleting first left stored template_ids pointing at a missing+// template for the duration of the build.func (c *E2BRemoteClient) ReplaceStandardTemplate(ctx context.Context) (*RemoteTemplate, error) {items, err := c.ListTemplates(ctx)if err != nil {return nil, err}+ created, err := c.buildStandardTemplate(ctx)+ if err != nil || created == nil || strings.TrimSpace(created.ID) == "" {+ // A READY template may refuse a second build under the same name.+ // Only then delete, and only to retry — if this retry also fails the+ // old template is already gone, which is the original E2B limitation.+ if err != nil {+ logger.Warnf(ctx,+ "e2b standard template rebuild without delete failed: %v; deleting then retrying",+ err)+ }+ for _, item := range items {+ if !item.Standard || strings.TrimSpace(item.ID) == "" {+ continue+ }+ logger.Infof(ctx, "e2b replacing standard template %s", item.ID)+ if delErr := c.client.DeleteTemplate(ctx, item.ID); delErr != nil {+ var notFound *e2b.TemplateNotFoundError+ if !errors.As(delErr, ¬Found) {+ return nil, normalizeE2BError("DeleteTemplate", delErr)+ }+ }+ }+ return c.buildStandardTemplate(ctx)+ }for _, item := range items {- if !item.Standard || strings.TrimSpace(item.ID) == "" {+ if !item.Standard || strings.TrimSpace(item.ID) == "" || item.ID == created.ID {continue}- logger.Infof(ctx, "e2b replacing standard template %s", item.ID)+ logger.Infof(ctx, "e2b deleting superseded standard template %s", item.ID)if err := c.client.DeleteTemplate(ctx, item.ID); err != nil {var notFound *e2b.TemplateNotFoundErrorif !errors.As(err, ¬Found) {- return nil, normalizeE2BError("DeleteTemplate", err)+ logger.Warnf(ctx, "e2b delete of replaced template %s failed: %v", item.ID, err)}}}- return c.buildStandardTemplate(ctx)+ return created, nil}func (c *E2BRemoteClient) buildStandardTemplate(ctx context.Context) (*RemoteTemplate, error) {diff --git a/internal/sandbox/template_catalog.go b/internal/sandbox/template_catalog.goindex f059570e4..277369877 100644--- a/internal/sandbox/template_catalog.go+++ b/internal/sandbox/template_catalog.go@@ -46,9 +46,10 @@ type RemoteTemplate struct {type RemoteTemplateCatalog interface {ListTemplates(ctx context.Context) ([]RemoteTemplate, error)EnsureStandardTemplate(ctx context.Context) (*RemoteTemplate, error)- // ReplaceStandardTemplate deletes the cluster's WeKnora template and- // builds a new one from the current spec. Changing DNS or the image- // otherwise has no effect on an already-READY template.+ // ReplaceStandardTemplate applies the current spec to the cluster's+ // WeKnora template (DNS, image). A READY template cannot pick those up+ // any other way. Implementations must not delete a usable template+ // until a replacement with an ID exists.ReplaceStandardTemplate(ctx context.Context) (*RemoteTemplate, error)}