Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(sandbox): delete replaced templates only after persist succeeds

wizardchene7241e4d9104
diff --git a/frontend/src/components/SandboxConfigEditorDrawer.vue b/frontend/src/components/SandboxConfigEditorDrawer.vue
index bd0f927eb..b7846ea8b 100644
--- a/frontend/src/components/SandboxConfigEditorDrawer.vue
+++ b/frontend/src/components/SandboxConfigEditorDrawer.vue
@@ -86,7 +86,7 @@
<t-form-item :label="$t('settings.sandbox.backendType')">
<t-select :value="backend" :placeholder="$t('settings.sandbox.backendTypePlaceholder')"
class="backend-select" :popup-props="{ overlayClassName: 'sandbox-backend-popup' }"
- :disabled="hasSkillSnapshot"
+ :disabled="retargetFrozen"
@change="(v: any) => selectBackend(String(v))">
<t-option v-for="opt in backendOptions" :key="opt" :value="opt" :label="backendLabel(opt)">
<span class="backend-choice">
@@ -117,29 +117,31 @@
<h4 class="setting-drawer__section-title">{{ $t('settings.sandbox.sectionConnection') }}</h4>
<t-alert v-if="hasSkillSnapshot" theme="info" class="identity-hint compact-alert"
:message="$t('settings.sandbox.connectionLockedBySkills')" />
+ <t-alert v-else-if="hasInFlightSkill" theme="info" class="identity-hint compact-alert"
+ :message="$t('settings.sandbox.connectionLockedByInFlight')" />
<t-alert v-else-if="record" theme="info" class="identity-hint compact-alert"
:message="$t('settings.sandbox.identityFieldHint')" />
<template v-if="backend === 'cube'">
<t-form-item :label="requiredLabel('apiUrl')" :status="fieldStatus('api_url')" :tips="fieldTip('api_url')">
<t-input v-model="cube.api_url" placeholder="http://cube.example.com:33000"
- :disabled="hasSkillSnapshot" @input="onConnectionInput('api_url')" />
+ :disabled="retargetFrozen" @input="onConnectionInput('api_url')" />
</t-form-item>
<div class="form-grid form-grid--two">
<t-form-item :label="requiredLabel('proxyUrl')" :status="fieldStatus('proxy_url')"
:tips="fieldTip('proxy_url')">
<t-input v-model="cube.proxy_url" placeholder="http://cube.example.com:80"
- :disabled="hasSkillSnapshot" @input="onConnectionInput('proxy_url')" />
+ :disabled="retargetFrozen" @input="onConnectionInput('proxy_url')" />
</t-form-item>
<t-form-item :label="requiredLabel('sandboxDomain')" :status="fieldStatus('sandbox_domain')"
:tips="fieldTip('sandbox_domain')">
<t-input v-model="cube.sandbox_domain" placeholder="cube.app"
- :disabled="hasSkillSnapshot" @input="onConnectionInput('sandbox_domain')" />
+ :disabled="retargetFrozen" @input="onConnectionInput('sandbox_domain')" />
</t-form-item>
</div>
<t-form-item :label="$t('settings.sandbox.apiKey')">
<t-input v-model="cube.api_key" type="password" :placeholder="secretInputPlaceholder('cube')"
- :disabled="hasSkillSnapshot" @input="invalidateConnection" />
+ :disabled="retargetFrozen" @input="invalidateConnection" />
<div class="field-hints">
<p class="section-help">
{{ storedSecrets.cube
@@ -155,7 +157,7 @@
<t-form-item :label="$t('settings.sandbox.cubeDnsServers')"
:help="$t('settings.sandbox.cubeDnsServersHelp')">
<t-tag-input v-model="cube.dns_servers" :placeholder="$t('settings.sandbox.cubeDnsServersPlaceholder')"
- :disabled="hasSkillSnapshot" clearable @change="invalidateConnection" />
+ :disabled="retargetFrozen" clearable @change="invalidateConnection" />
</t-form-item>
</template>
@@ -163,7 +165,7 @@
<t-form-item :label="requiredLabel('apiKey')" :status="fieldStatus('api_key')"
:tips="fieldTip('api_key')">
<t-input v-model="e2b.api_key" type="password" :placeholder="secretInputPlaceholder('e2b')"
- :disabled="hasSkillSnapshot" @input="onConnectionInput('api_key')" />
+ :disabled="retargetFrozen" @input="onConnectionInput('api_key')" />
<div class="field-hints">
<p class="section-help">
{{ storedSecrets.e2b
@@ -179,16 +181,16 @@
<div class="form-grid form-grid--two">
<t-form-item :label="$t('settings.sandbox.apiUrl')" :help="$t('settings.sandbox.e2bApiUrlOptional')">
<t-input v-model="e2b.api_url" placeholder="https://api.e2b.app"
- :disabled="hasSkillSnapshot" @input="invalidateConnection" />
+ :disabled="retargetFrozen" @input="invalidateConnection" />
</t-form-item>
<t-form-item :label="$t('settings.sandbox.sandboxDomain')" :help="$t('settings.sandbox.e2bDomainOptional')">
<t-input v-model="e2b.sandbox_domain" placeholder="e2b.app"
- :disabled="hasSkillSnapshot" @input="invalidateConnection" />
+ :disabled="retargetFrozen" @input="invalidateConnection" />
</t-form-item>
</div>
<t-form-item :label="$t('settings.sandbox.proxyUrl')" :help="$t('settings.sandbox.e2bProxyUrlOptional')">
<t-input v-model="e2b.proxy_url" placeholder="http://sandbox-gateway.example.com"
- :disabled="hasSkillSnapshot" @input="invalidateConnection" />
+ :disabled="retargetFrozen" @input="invalidateConnection" />
</t-form-item>
</template>
<div class="private-endpoint-row">
@@ -198,7 +200,7 @@
</div>
<t-switch
v-model="allowPrivateEndpoints"
- :disabled="hasSkillSnapshot"
+ :disabled="retargetFrozen"
@change="invalidateConnection"
/>
</div>
@@ -220,19 +222,21 @@
<t-form-item :label="requiredLabel('dockerImage')" :status="fieldStatus('image')"
:tips="fieldTip('image')">
<t-input v-model="docker.image" placeholder="wechatopenai/weknora-sandbox:latest"
- :disabled="hasSkillSnapshot" @input="onFieldInput('image')" />
- <p v-if="hasSkillSnapshot" class="section-help section-help--field">
- {{ $t('settings.sandbox.templateLockedBySkills') }}
+ :disabled="retargetFrozen" @input="onFieldInput('image')" />
+ <p v-if="retargetFrozen" class="section-help section-help--field">
+ {{ hasSkillSnapshot
+ ? $t('settings.sandbox.templateLockedBySkills')
+ : $t('settings.sandbox.templateLockedByInFlight') }}
</p>
</t-form-item>
<t-form-item :label="$t('settings.sandbox.dockerHost')" :help="$t('settings.sandbox.dockerHostHelp')">
<t-input v-model="docker.host" placeholder="unix:///var/run/docker.sock"
- :disabled="hasSkillSnapshot" @input="onFieldInput('host')" />
+ :disabled="retargetFrozen" @input="onFieldInput('host')" />
</t-form-item>
<t-form-item :label="$t('settings.sandbox.dockerTlsCertPath')"
:help="$t('settings.sandbox.dockerTlsCertPathHelp')">
<t-input v-model="docker.tls_cert_path" placeholder="/etc/weknora/docker-certs"
- :disabled="hasSkillSnapshot" @input="onFieldInput('tls_cert_path')" />
+ :disabled="retargetFrozen" @input="onFieldInput('tls_cert_path')" />
</t-form-item>
</template>
<t-alert v-else theme="warning" class="compact-alert" :message="$t('settings.sandbox.localRuntimeWarning')" />
@@ -248,6 +252,8 @@
</div>
<t-alert v-if="hasSkillSnapshot" theme="info" class="compact-alert"
:message="$t('settings.sandbox.templateLockedBySkills')" />
+ <t-alert v-else-if="hasInFlightSkill" theme="info" class="compact-alert"
+ :message="$t('settings.sandbox.templateLockedByInFlight')" />
<div v-if="templatesLoading && !templatesLoaded" class="template-loading">
<t-loading size="small" />
<span>{{ $t('settings.sandbox.loadingTemplates') }}</span>
@@ -276,12 +282,12 @@
:class="{
'is-active': currentTemplateId === item.id,
'is-pending': isTemplatePending(item),
- 'is-disabled': !isTemplateSelectable(item) || (hasSkillSnapshot && currentTemplateId !== item.id),
+ 'is-disabled': !isTemplateSelectable(item) || (retargetFrozen && currentTemplateId !== item.id),
}"
role="radio"
:aria-checked="currentTemplateId === item.id"
- :aria-disabled="!isTemplateSelectable(item) || (hasSkillSnapshot && currentTemplateId !== item.id)"
- :tabindex="isTemplateSelectable(item) && !(hasSkillSnapshot && currentTemplateId !== item.id) ? 0 : -1"
+ :aria-disabled="!isTemplateSelectable(item) || (retargetFrozen && currentTemplateId !== item.id)"
+ :tabindex="isTemplateSelectable(item) && !(retargetFrozen && currentTemplateId !== item.id) ? 0 : -1"
@click="onTemplateCardClick(item)"
@keydown.enter.prevent="onTemplateCardClick(item)"
@keydown.space.prevent="onTemplateCardClick(item)"
@@ -441,6 +447,7 @@
:record="effectiveRecord"
@updated="onSkillsConfigUpdated"
@skills-changed="emit('skillsChanged')"
+ @in-flight-change="inFlightFromPanel = $event"
/>
<p v-else class="skills-locked">{{ $t('settings.sandbox.stepSkillsLocked') }}</p>
</template>
@@ -511,6 +518,7 @@ const props = defineProps<{
// Which page to land on when opening an existing config, e.g. 'skills' from
// the card's 管理技能 entry. Ignored while creating, where order is enforced.
initialStep?: SandboxStepKey
+ hasInFlightSkill?: boolean
}>()
const emit = defineEmits<{
@@ -573,7 +581,7 @@ const currentTemplateId = computed(() => (
const selectedTemplate = computed(() => templates.value.find((item) => item.id === currentTemplateId.value))
const clusterStandardTemplate = computed(() => templates.value.find((item) => item.standard && item.id))
const canCreateStandard = computed(() => (
- isRemoteBackend.value && templatesLoaded.value && !clusterStandardTemplate.value && !hasSkillSnapshot.value
+ isRemoteBackend.value && templatesLoaded.value && !clusterStandardTemplate.value && !retargetFrozen.value
))
const wizardSteps = computed<Array<{ key: SandboxStepKey; title: string }>>(() => {
const steps: Array<{ key: SandboxStepKey; title: string }> = [
@@ -627,6 +635,9 @@ const effectiveRecord = computed(() => savedRecord.value || props.record)
const hasSkillSnapshot = computed(() => (
Boolean(effectiveRecord.value?.config?.skill_image?.snapshot_id?.trim())
))
+const inFlightFromPanel = ref(false)
+const hasInFlightSkill = computed(() => Boolean(props.hasInFlightSkill) || inFlightFromPanel.value)
+const retargetFrozen = computed(() => hasSkillSnapshot.value || hasInFlightSkill.value)
function onSkillsConfigUpdated(record: SandboxConfigRecord) {
savedRecord.value = record
@@ -785,6 +796,7 @@ function reset() {
templatesLoaded.value = false
templatesError.value = ''
savedRecord.value = null
+ inFlightFromPanel.value = false
wizardStep.value = 0
// "管理技能" opens this drawer straight on the skills step. It is a jump like
// any other, so it only holds for a config that already exists.
@@ -807,6 +819,7 @@ watch(() => props.visible, (open) => {
if (open) {
reset()
} else {
+ inFlightFromPanel.value = false
stopTemplatePolling()
}
})
@@ -840,13 +853,13 @@ function clearTemplateSelection() {
}
function onTemplateCardClick(item: SandboxTemplate) {
- if (hasSkillSnapshot.value && item.id !== currentTemplateId.value) return
+ if (retargetFrozen.value && item.id !== currentTemplateId.value) return
if (!isTemplateSelectable(item)) return
selectTemplate(item.id)
}
function canRebuildTemplate(item: SandboxTemplate): boolean {
- return Boolean(item.standard && item.id) && !isTemplatePending(item) && !hasSkillSnapshot.value
+ return Boolean(item.standard && item.id) && !isTemplatePending(item) && !retargetFrozen.value
}
function templateDisplayName(item: SandboxTemplate): string {
@@ -987,7 +1000,7 @@ async function loadTemplates(ensureStandard = false, silent = false, replaceStan
} else if (
currentTemplateId.value
&& (!current || (!isTemplateSelectable(current) && !isTemplatePending(current)))
- && !hasSkillSnapshot.value
+ && !retargetFrozen.value
) {
if (standardID) {
const next = templates.value.find((item) => item.id === standardID)
@@ -1023,7 +1036,7 @@ function createStandardTemplate() {
}
function replaceStandardTemplate() {
- if (hasSkillSnapshot.value) return
+ if (retargetFrozen.value) return
return loadTemplates(false, false, true)
}
diff --git a/frontend/src/components/SandboxSkillsPanel.vue b/frontend/src/components/SandboxSkillsPanel.vue
index bde9c006e..8fa76ad43 100644
--- a/frontend/src/components/SandboxSkillsPanel.vue
+++ b/frontend/src/components/SandboxSkillsPanel.vue
@@ -364,6 +364,7 @@ const props = defineProps<{
const emit = defineEmits<{
updated: [record: SandboxConfigRecord]
skillsChanged: []
+ inFlightChange: [busy: boolean]
}>()
const { t, locale } = useI18n()
@@ -460,6 +461,12 @@ function isBusy(skill: ConfigSkill): boolean {
return skill.status === 'installing' || skill.status === 'removing'
}
+watch(
+ () => skills.value.some(isBusy),
+ (busy) => emit('inFlightChange', busy),
+ { immediate: true },
+)
+
// The locators are written only after the installer sandbox is up and the
// agent has a message to stream into. The row itself is already "installing"
// the moment the upload is accepted, and that is when the button has to
diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts
index bf8cab583..4146872dc 100755
--- a/frontend/src/i18n/locales/en-US.ts
+++ b/frontend/src/i18n/locales/en-US.ts
@@ -1106,6 +1106,7 @@ export default {
noConfigs: 'No sandbox yet. Agents without a workspace configuration will not run skill scripts.',
identityFieldHint: 'While this config owns sandboxes, these cannot be changed: backend type, API endpoint, API key, sandbox domain, proxy endpoint.',
connectionLockedBySkills: 'This sandbox already has skills. Connection, credentials, and DNS would retarget the skill snapshot, and DNS only applies after a template rebuild. Create a new sandbox instead.',
+ connectionLockedByInFlight: 'A skill is still installing or being removed. Connection, credentials, and DNS cannot change until that finishes.',
viewSandboxes: 'View sandbox usage',
inventoryTitle: 'Sandbox usage',
inventoryDrawerDesc: 'Sandboxes this config still holds, plus the sessions and agents affected.',
@@ -1173,8 +1174,9 @@ export default {
createStandardTemplate: 'Create',
createStandardTemplateHint: 'Built with the current connection settings, including DNS. After changing those settings, rebuild from the card.',
replaceStandardTemplate: 'Rebuild',
- replaceStandardTemplateConfirm: 'This deletes the WeKnora standard template on the cluster and rebuilds it with the current settings, including DNS. Sandboxes already using that template may no longer start.',
+ replaceStandardTemplateConfirm: 'Rebuild the WeKnora standard template with the current settings, including DNS. The previous spawnable template is not deleted until the replacement is ready.',
templateLockedBySkills: 'This sandbox already has skills. The skill environment is bound to the current snapshot, so the runtime template cannot be changed or rebuilt. Create a new sandbox and install skills from the new template.',
+ templateLockedByInFlight: 'A skill is still installing or being removed. The runtime template cannot be changed or rebuilt until that finishes.',
templateUnnamed: 'Unnamed template',
templateFieldImage: 'Image',
templateFieldVersion: 'Version',
diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts
index c0c6f1c16..b8cf617a7 100755
--- a/frontend/src/i18n/locales/ko-KR.ts
+++ b/frontend/src/i18n/locales/ko-KR.ts
@@ -4959,6 +4959,7 @@ export default {
noConfigs: '아직 샌드박스가 없습니다. 워크스페이스 설정을 선택하지 않은 에이전트는 스킬 스크립트를 실행하지 않습니다.',
identityFieldHint: 'While this config owns sandboxes, these cannot be changed: backend type, API endpoint, API key, sandbox domain, proxy endpoint.',
connectionLockedBySkills: '이 샌드박스에는 이미 Skill이 있습니다. 연결, 자격 증명, DNS를 바꾸면 스킬 스냅샷의 환경이 달라지고, DNS는 템플릿을 재구축해야 적용됩니다. 새 샌드박스를 만드세요.',
+ connectionLockedByInFlight: 'Skill을 설치하거나 제거하는 중입니다. 끝날 때까지 연결, 자격 증명, DNS를 바꿀 수 없습니다.',
viewSandboxes: 'View sandbox usage',
inventoryTitle: 'Sandbox usage',
inventoryDrawerDesc: '이 설정이 아직 점유한 샌드박스와 영향을 받는 세션·에이전트입니다.',
@@ -5026,8 +5027,9 @@ export default {
createStandardTemplate: '만들기',
createStandardTemplateHint: '현재 연결 설정(DNS 포함)으로 구축합니다. 설정을 바꾼 뒤에는 카드에서 재구축하세요.',
replaceStandardTemplate: '재구축',
- replaceStandardTemplateConfirm: '클러스터의 WeKnora 표준 템플릿을 삭제한 뒤 현재 설정(DNS 포함)으로 다시 만듭니다. 해당 템플릿을 쓰는 기존 샌드박스는 더 이상 시작되지 않을 수 있습니다.',
+ replaceStandardTemplateConfirm: '현재 설정(DNS 포함)으로 WeKnora 표준 템플릿을 다시 만듭니다. 새 템플릿이 준비되기 전에는 기존에 쓸 수 있는 템플릿을 삭제하지 않습니다.',
templateLockedBySkills: '이 샌드박스에는 이미 Skill이 설치되어 있습니다. 스킬 환경이 현재 스냅샷에 묶여 있어 런타임 템플릿을 바꾸거나 재구축할 수 없습니다. 새 샌드박스를 만들고 새 템플릿에서 Skill을 다시 설치하세요.',
+ templateLockedByInFlight: 'Skill을 설치하거나 제거하는 중입니다. 끝날 때까지 런타임 템플릿을 바꾸거나 재구축할 수 없습니다.',
templateUnnamed: '이름 없는 템플릿',
templateFieldImage: '이미지',
templateFieldVersion: '버전',
diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts
index bb72bb845..ca0b762a6 100755
--- a/frontend/src/i18n/locales/ru-RU.ts
+++ b/frontend/src/i18n/locales/ru-RU.ts
@@ -4959,6 +4959,7 @@ export default {
noConfigs: 'Песочниц пока нет. Агенты без конфигурации пространства не выполняют скрипты навыков.',
identityFieldHint: 'While this config owns sandboxes, these cannot be changed: backend type, API endpoint, API key, sandbox domain, proxy endpoint.',
connectionLockedBySkills: 'В этой песочнице уже установлены навыки. Смена подключения, учётных данных или DNS меняет среду снимка, а DNS применяется только после пересборки шаблона. Создайте новую песочницу.',
+ connectionLockedByInFlight: 'Навык ещё устанавливается или удаляется. Подключение, учётные данные и DNS нельзя менять, пока это не закончится.',
viewSandboxes: 'View sandbox usage',
inventoryTitle: 'Sandbox usage',
inventoryDrawerDesc: 'Песочницы, которые всё ещё занимает эта конфигурация, а также затронутые сессии и агенты.',
@@ -5026,8 +5027,9 @@ export default {
createStandardTemplate: 'Создать',
createStandardTemplateHint: 'Собирается с текущими настройками подключения, включая DNS. После их изменения пересоберите шаблон на карточке.',
replaceStandardTemplate: 'Пересобрать',
- replaceStandardTemplateConfirm: 'Стандартный шаблон WeKnora будет удалён из кластера и собран заново с текущими настройками, включая DNS. Песочницы, которые уже используют этот шаблон, могут больше не запускаться.',
+ replaceStandardTemplateConfirm: 'Стандартный шаблон WeKnora будет собран заново с текущими настройками, включая DNS. Предыдущий готовый шаблон не удаляется, пока замена не станет готовой к запуску.',
templateLockedBySkills: 'В этой песочнице уже установлены навыки. Среда навыков привязана к текущему снимку, поэтому шаблон нельзя сменить или пересобрать. Создайте новую песочницу и установите навыки с нового шаблона.',
+ templateLockedByInFlight: 'Навык ещё устанавливается или удаляется. Шаблон нельзя сменить или пересобрать, пока это не закончится.',
templateUnnamed: 'Шаблон без имени',
templateFieldImage: 'Образ',
templateFieldVersion: 'Версия',
diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts
index 407179e67..1a16c3697 100755
--- a/frontend/src/i18n/locales/zh-CN.ts
+++ b/frontend/src/i18n/locales/zh-CN.ts
@@ -4961,6 +4961,7 @@ export default {
noConfigs: '还没有沙箱配置。智能体未选择空间配置时不会执行技能脚本。',
identityFieldHint: '该配置下有沙箱运行时,以下项无法修改:后端类型、API 端点、API Key、沙箱域名、Proxy 端点。',
connectionLockedBySkills: '该沙箱已安装 Skill。连接地址、凭据和 DNS 会改变技能快照所属环境,且 DNS 需重建模板才能生效;请新建一份沙箱。',
+ connectionLockedByInFlight: '该沙箱正在安装或移除 Skill,完成前不能更换连接、凭据或 DNS。',
viewSandboxes: '查看沙箱占用',
inventoryTitle: '沙箱占用',
inventoryDrawerDesc: '该配置当前占用的沙箱,以及受影响的会话与智能体。',
@@ -5028,8 +5029,9 @@ export default {
createStandardTemplate: '创建',
createStandardTemplateHint: '按当前连接配置构建,包含 DNS。之后改配置可在卡片上重建。',
replaceStandardTemplate: '重建',
- replaceStandardTemplateConfirm: '将删除集群上的 WeKnora 标准模板,并用当前配置(含 DNS)重新构建。使用该模板的存量沙箱可能无法再启动。',
+ replaceStandardTemplateConfirm: '将用当前配置(含 DNS)重建 WeKnora 标准模板。新模板就绪前不会删除仍可启动的旧模板。',
templateLockedBySkills: '该沙箱已安装 Skill,技能环境绑在当前快照上,不能更换或重建运行模板。请新建一份沙箱,从新模板再装 Skill。',
+ templateLockedByInFlight: '该沙箱正在安装或移除 Skill,完成前不能更换或重建运行模板。',
templateUnnamed: '未命名模板',
templateFieldImage: '镜像',
templateFieldVersion: '版本',
diff --git a/frontend/src/views/settings/SandboxSettings.vue b/frontend/src/views/settings/SandboxSettings.vue
index 31ffb1613..df08020b8 100644
--- a/frontend/src/views/settings/SandboxSettings.vue
+++ b/frontend/src/views/settings/SandboxSettings.vue
@@ -140,6 +140,7 @@
<SandboxConfigEditorDrawer v-model:visible="showEditor" :record="editingRecord"
:preset-type="activeType === 'all' ? '' : activeType" :initial-step="editorStep"
+ :has-in-flight-skill="editorHasInFlightSkill"
@saved="load" @skills-changed="onSkillsChanged" />
<!--
@@ -293,6 +294,16 @@ function skillTagTheme(skill: ConfigSkill): 'default' | 'warning' | 'primary' {
return 'default'
}
+function skillInFlight(skill: ConfigSkill): boolean {
+ return skill.status === 'installing' || skill.status === 'removing'
+}
+
+const editorHasInFlightSkill = computed(() => {
+ const id = editingRecord.value?.id
+ if (!id) return false
+ return (skillsByConfig.value[id] || []).some(skillInFlight)
+})
+
const filteredRecords = computed(() => {
const base = activeType.value === 'all'
? records.value
diff --git a/internal/application/service/tenant_sandbox_config.go b/internal/application/service/tenant_sandbox_config.go
index e4865f4ea..54896eaa4 100644
--- a/internal/application/service/tenant_sandbox_config.go
+++ b/internal/application/service/tenant_sandbox_config.go
@@ -641,25 +641,27 @@ func (s *TenantSandboxConfigService) QueryTemplates(
if !ok || standard == nil {
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 replaced.
- kept := result.Templates[:0]
- for _, item := range result.Templates {
- if !item.Standard || item.ID == standard.ID {
- kept = append(kept, item)
- }
- }
- result.Templates = kept
- }
result.Templates = deduplicateSandboxTemplates(append(result.Templates, *standard))
- if !sandbox.IsTemplateBuildFailed(standard.Status) {
+ if sandbox.IsTemplateReady(standard.Status) {
result.Provisioned = true
- result.StandardTemplateID = standard.ID
if in.ReplaceStandard {
- s.persistSpawnTemplateID(ctx, tenantID, merged, standard.ID, oldStandardIDs)
+ persistErr := s.persistSpawnTemplateID(ctx, tenantID, merged, standard.ID, oldStandardIDs)
+ if persistErr != nil {
+ logger.Warnf(ctx, "[sandbox] persist rebuilt template id: %v; keeping previous templates",
+ persistErr)
+ } else {
+ result.StandardTemplateID = standard.ID
+ result.Templates = hideSupersededStandardTemplates(result.Templates, standard.ID)
+ s.deleteSupersededStandardTemplates(ctx, catalog, standard.ID)
+ }
+ } else {
+ result.StandardTemplateID = standard.ID
+ }
+ } else if !sandbox.IsTemplateBuildFailed(standard.Status) {
+ result.Provisioned = true
+ if result.StandardTemplateID == "" {
+ result.StandardTemplateID = standard.ID
}
- } else if in.ReplaceStandard {
- result.StandardTemplateID = ""
}
}
sort.SliceStable(result.Templates, func(i, j int) bool {
@@ -671,6 +673,18 @@ func (s *TenantSandboxConfigService) QueryTemplates(
return result, nil
}
+func hideSupersededStandardTemplates(items []sandbox.RemoteTemplate, keepID string) []sandbox.RemoteTemplate {
+ keepID = strings.TrimSpace(keepID)
+ kept := make([]sandbox.RemoteTemplate, 0, len(items))
+ for _, item := range items {
+ if item.Standard && strings.TrimSpace(item.ID) != keepID {
+ continue
+ }
+ kept = append(kept, item)
+ }
+ return kept
+}
+
func standardTemplateIDs(items []sandbox.RemoteTemplate) []string {
out := make([]string, 0, len(items))
for _, item := range items {
@@ -736,10 +750,10 @@ func (s *TenantSandboxConfigService) persistSpawnTemplateID(
merged *types.TenantSandboxConfig,
newID string,
oldIDs []string,
-) {
+) error {
newID = strings.TrimSpace(newID)
if newID == "" || s == nil || s.repo == nil {
- return
+ return nil
}
old := make(map[string]struct{}, len(oldIDs))
for _, id := range oldIDs {
@@ -750,9 +764,9 @@ func (s *TenantSandboxConfigService) persistSpawnTemplateID(
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
+ return fmt.Errorf("list configs: %w", err)
}
+ var persistErr error
for _, entity := range list {
if entity == nil || entity.Config == nil || types.IsSandboxWorkspacePolicyRow(entity) {
continue
@@ -769,13 +783,28 @@ func (s *TenantSandboxConfigService) persistSpawnTemplateID(
continue
}
}
+ previous := current
setSpawnTemplateID(entity.Config, newID)
if err := s.repo.Update(ctx, entity); err != nil {
+ setSpawnTemplateID(entity.Config, previous)
logger.Warnf(ctx, "[sandbox] persist rebuilt template id on config %s: %v", entity.ID, err)
+ persistErr = err
continue
}
logger.Infof(ctx, "[sandbox] config %s spawn template is now %s after rebuild", entity.ID, newID)
}
+ return persistErr
+}
+
+func (s *TenantSandboxConfigService) deleteSupersededStandardTemplates(
+ ctx context.Context, catalog sandbox.RemoteTemplateCatalog, keepID string,
+) {
+ if catalog == nil || strings.TrimSpace(keepID) == "" {
+ return
+ }
+ if err := catalog.DeleteSupersededStandardTemplates(ctx, keepID); err != nil {
+ logger.Warnf(ctx, "[sandbox] delete superseded standard templates failed: %v", err)
+ }
}
func setSpawnTemplateID(cfg *types.TenantSandboxConfig, id string) {
diff --git a/internal/application/service/tenant_sandbox_config_test.go b/internal/application/service/tenant_sandbox_config_test.go
index e9700194f..89ef731b1 100644
--- a/internal/application/service/tenant_sandbox_config_test.go
+++ b/internal/application/service/tenant_sandbox_config_test.go
@@ -215,6 +215,7 @@ type fakeConfigRepo struct {
onSetCordon func()
onUpdate func()
+ updateErr error
clearCordonCtxErr error
}
@@ -261,6 +262,9 @@ func (f *fakeConfigRepo) Update(
if f.onUpdate != nil {
f.onUpdate()
}
+ if f.updateErr != nil {
+ return f.updateErr
+ }
return nil
}
@@ -337,11 +341,6 @@ func (s *stubProviderClient) ReplaceStandardTemplate(ctx context.Context) (*sand
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)
- }
- }
if s.replaced != nil {
copyTpl := *s.replaced
return &copyTpl, nil
@@ -349,6 +348,15 @@ func (s *stubProviderClient) ReplaceStandardTemplate(ctx context.Context) (*sand
return s.EnsureStandardTemplate(ctx)
}
+func (s *stubProviderClient) DeleteSupersededStandardTemplates(_ context.Context, keepID string) error {
+ for _, item := range s.templates {
+ if item.Standard && item.ID != "" && item.ID != keepID {
+ s.deletedTemplates = append(s.deletedTemplates, item.ID)
+ }
+ }
+ return nil
+}
+
func (s *stubProviderClient) List(
ctx context.Context, _ sandbox.RemoteListFilter,
) ([]sandbox.RemoteSandboxSummary, error) {
@@ -546,7 +554,7 @@ func TestQueryTemplatesReplaceStandardPersistsNewTemplateID(t *testing.T) {
{ID: "tpl-custom", Name: "custom", Status: "ready"},
},
replaced: &sandbox.RemoteTemplate{
- ID: "tpl-new", Name: "weknora", Status: "building", Standard: true,
+ ID: "tpl-new", Name: "weknora", Status: "ready", Standard: true,
},
}
svc := newTestConfigService(t, repo, client, stubAgentRepo{})
@@ -570,6 +578,109 @@ func TestQueryTemplatesReplaceStandardPersistsNewTemplateID(t *testing.T) {
require.Equal(t, "tpl-custom", result.Templates[1].ID)
}
+func TestQueryTemplatesReplaceStandardKeepsOldTemplateWhileReplacementBuilds(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.Empty(t, client.deletedTemplates,
+ "a building replacement must not retire the spawnable template")
+ require.Equal(t, "tpl-old", repo.entity.Config.E2B.TemplateID,
+ "sessions must keep the previous READY id until the replacement can spawn")
+ require.Equal(t, "tpl-old", result.StandardTemplateID)
+ require.True(t, result.Provisioned)
+ ids := make([]string, 0, len(result.Templates))
+ for _, item := range result.Templates {
+ ids = append(ids, item.ID)
+ }
+ require.Contains(t, ids, "tpl-old")
+ require.Contains(t, ids, "tpl-new")
+}
+
+func TestQueryTemplatesReplaceStandardKeepsOldTemplateWhenPersistFails(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,
+ },
+ updateErr: stderrors.New("db write failed"),
+ }
+ 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: "ready", 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.Empty(t, client.deletedTemplates,
+ "a persist failure must not retire the previous spawnable template")
+ require.Equal(t, "tpl-old", repo.entity.Config.E2B.TemplateID)
+ require.Equal(t, "tpl-old", result.StandardTemplateID)
+ ids := make([]string, 0, len(result.Templates))
+ for _, item := range result.Templates {
+ ids = append(ids, item.ID)
+ }
+ require.Contains(t, ids, "tpl-old")
+ require.Contains(t, ids, "tpl-new")
+}
+
+func TestQueryTemplatesReplaceStandardRefusesWhenSkillIsInstalling(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: "t1", Name: "weknora", Status: "ready", Standard: true},
+ },
+ replaced: &sandbox.RemoteTemplate{
+ ID: "tpl-new", Name: "weknora", Status: "ready", Standard: true,
+ },
+ }
+ svc := newTestConfigService(t, repo, client, stubAgentRepo{})
+ svc.skills = &deleteSkillStore{skills: []*types.TenantSkillEntity{{
+ ID: "sk-1", TenantID: 7, SandboxConfigID: "cfg-a",
+ Status: types.SkillStatusInstalling,
+ }}}
+
+ _, 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 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)
diff --git a/internal/application/service/tenant_skill_install.go b/internal/application/service/tenant_skill_install.go
index 5362ed81e..1f2218025 100644
--- a/internal/application/service/tenant_skill_install.go
+++ b/internal/application/service/tenant_skill_install.go
@@ -351,21 +351,22 @@ func (s *TenantSkillService) runInstall(
if !owned {
return nil
}
- // The generation comes from the config read at the top of this function,
- // while switchImagePointer deliberately re-reads for everything else it
- // writes. That asymmetry is sound because the two fields have different
- // writers: SkillImage is written only by an install or a removal, and
- // withConfigLock serialises every one of those per config, so no other
- // writer can have advanced the generation while this run held the lock.
- // The rest of the entity is written by the config service under its own
- // cordon, which this lock says nothing about — hence the re-read there.
- generation := currentGeneration(cfgEntity) + 1
+ // Generation is max(live pointer, ledger)+1 so a build that died after
+ // the commit but before the pointer moved cannot share a name with the
+ // next install. withConfigLock still serialises writers of SkillImage;
+ // the ledger read is what closes the crash window the lock cannot see.
+ ledger, err := s.skills.ListSnapshotsByConfig(ctx, tenantID, configID)
+ if err != nil {
+ return fmt.Errorf("list snapshots of config %s: %w", configID, err)
+ }
+ generation := nextSnapshotGeneration(currentGeneration(cfgEntity), ledger)
installRowID := uuid.NewString()
// The name is recorded with the row rather than derived at the call below,
// 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(tenantID, configID, generation)
+ // resource nothing could ever address, let alone reclaim. The row id is
+ // in the name so two rows of the same generation cannot share a tag.
+ snapshotName := skillSnapshotBuildName(tenantID, configID, generation, installRowID)
if err := s.skills.CreateSnapshotRow(ctx, &types.TenantSkillSnapshotEntity{
ID: installRowID, TenantID: tenantID, SandboxConfigID: configID, SkillID: skillID,
ParentSnapshotID: currentSnapshotID(cfgEntity), Generation: generation,
@@ -1333,15 +1334,44 @@ func skillSnapshotNamePrefix(tenantID uint64, configID string) string {
return fmt.Sprintf("weknora-sk-t%d-%s", tenantID, compactConfigID(configID))
}
+// nextSnapshotGeneration is one past both the live pointer and every ledger
+// row. An abandoned building row still occupies its generation; reusing it
+// would mint the same planned name and let the reaper delete a later install's
+// snapshot.
+func nextSnapshotGeneration(live int, rows []*types.TenantSkillSnapshotEntity) int {
+ highest := live
+ for _, row := range rows {
+ if row != nil && row.Generation > highest {
+ highest = row.Generation
+ }
+ }
+ if highest < 0 {
+ highest = 0
+ }
+ return highest + 1
+}
+
+func compactSnapshotToken(id string) string {
+ s := compactConfigID(id)
+ const n = 8
+ if len(s) > n {
+ return s[:n]
+ }
+ if s == "" {
+ return "row"
+ }
+ return s
+}
+
// 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. 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)
+// chain is committed under. It is recorded on the ledger row before the
+// provider call so an abandoned build stays identifiable. Tenant and the
+// full config id are in the name because Cube, E2B and Docker all list
+// snapshots across a shared account or daemon; the row token stops two
+// builds of the same generation from sharing a tag.
+func skillSnapshotBuildName(tenantID uint64, configID string, generation int, rowID string) string {
+ prefix := skillSnapshotNamePrefix(tenantID, configID)
+ return fmt.Sprintf("%s-g%d-%s", prefix, generation, compactSnapshotToken(rowID))
}
func compactConfigID(id string) string {
diff --git a/internal/application/service/tenant_skill_install_test.go b/internal/application/service/tenant_skill_install_test.go
index 9985bc567..44e949a43 100644
--- a/internal/application/service/tenant_skill_install_test.go
+++ b/internal/application/service/tenant_skill_install_test.go
@@ -90,12 +90,22 @@ func TestRunInstallSucceedsOnDockerConfig(t *testing.T) {
}
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)
+ a := skillSnapshotBuildName(7, "aaaaaaaa-bbbb-cccc-dddd-eeeeffff0001", 1, "11111111-2222-3333-4444-555555555555")
+ b := skillSnapshotBuildName(8, "aaaaaaaa-bbbb-cccc-dddd-eeeeffff0001", 1, "11111111-2222-3333-4444-555555555555")
+ c := skillSnapshotBuildName(7, "aaaaaaaa-bbbb-cccc-dddd-eeeeffff0002", 1, "11111111-2222-3333-4444-555555555555")
+ d := skillSnapshotBuildName(7, "aaaaaaaa-bbbb-cccc-dddd-eeeeffff0001", 1, "aaaaaaaa-bbbb-cccc-dddd-eeeeffff0001")
+ require.Equal(t, "weknora-sk-t7-aaaaaaaabbbbccccddddeeeeffff0001-g1-11111111", 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")
+ require.NotEqual(t, a, d, "two builds of the same generation must not share a tag")
+}
+
+func TestNextSnapshotGenerationSkipsAbandonedLedgerRows(t *testing.T) {
+ require.Equal(t, 1, nextSnapshotGeneration(0, nil))
+ require.Equal(t, 4, nextSnapshotGeneration(2, []*types.TenantSkillSnapshotEntity{
+ {Generation: 3, State: types.SkillSnapshotStateBuilding},
+ {Generation: 1, State: types.SkillSnapshotStateActive},
+ }))
}
// TestRunInstallIssuesExactlyTheseCommands pins the order, not just the set.
diff --git a/internal/application/service/tenant_skill_reaper.go b/internal/application/service/tenant_skill_reaper.go
index 9aa7e3f85..e6f70f69a 100644
--- a/internal/application/service/tenant_skill_reaper.go
+++ b/internal/application/service/tenant_skill_reaper.go
@@ -540,6 +540,9 @@ func (s *TenantSkillService) reapAbandonedBuilds(
if len(pending) == 0 {
return 0
}
+ if s.configHasInFlightSkill(ctx, cfg.TenantID, cfg.ID) {
+ return 0
+ }
lister := snapshotListerFrom(ctx, s.sandboxes, cfg.TenantID, cfg.ID)
deleter := snapshotDeleterFrom(ctx, s.sandboxes, cfg.TenantID, cfg.ID)
@@ -597,6 +600,30 @@ func abandonedBuild(row *types.TenantSkillSnapshotEntity, cutoff time.Time) bool
return row.CreatedAt.Before(cutoff)
}
+func (s *TenantSkillService) 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, "[skill] cannot read skills of config %s while reaping abandoned builds: %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
+}
+
// buildStillRunning reports whether the run that opened this row is alive.
//
// The age check alone would be enough for any commit that finishes in the
diff --git a/internal/application/service/tenant_skill_reaper_test.go b/internal/application/service/tenant_skill_reaper_test.go
index b20c8d419..3c105b7d6 100644
--- a/internal/application/service/tenant_skill_reaper_test.go
+++ b/internal/application/service/tenant_skill_reaper_test.go
@@ -509,6 +509,29 @@ func TestPruneLeavesABuildWhoseInstallIsStillBeating(t *testing.T) {
"a live install must not even cost a provider client")
}
+func TestPruneLeavesAbandonedBuildWhileAnotherSkillIsInstalling(t *testing.T) {
+ fx := newReaperFixture(t)
+ fx.live("snap-live")
+ fx.installed("sk-1", "snap-live", "")
+ fx.building("sk-2", "weknora-sk-t7-cfg1-g2-aaaaaaaa", fx.now.Add(-skillInstallStuckTTL-time.Minute))
+ alive := fx.now.Add(-time.Minute)
+ fx.skills.put(&types.TenantSkillEntity{
+ ID: "sk-3", TenantID: 7, SandboxConfigID: "cfg-1",
+ Name: "xlsx", Status: types.SkillStatusInstalling, InstallingSince: &alive,
+ })
+ fx.provider.listed = []sandbox.RemoteSnapshotRef{
+ {ID: "snap-live"},
+ {ID: "snap-new", Names: []string{"weknora-sk-t7-cfg1-g2-aaaaaaaa"}},
+ }
+
+ n, err := fx.svc.PruneSupersededSnapshots(context.Background())
+
+ require.NoError(t, err)
+ require.Zero(t, n)
+ require.Empty(t, fx.provider.deleted,
+ "an in-flight install on the same config may have reused the abandoned name")
+}
+
func TestPruneLeavesARecentBuildAlone(t *testing.T) {
fx := newReaperFixture(t)
fx.building("sk-2", "weknora-sk-cfg1-g2", fx.now.Add(-time.Minute))
@@ -783,8 +806,17 @@ func (r *reaperSkillStore) GetSkillByName(context.Context, uint64, string, strin
panic("GetSkillByName is outside the reaper surface")
}
-func (r *reaperSkillStore) ListSkillsByConfig(context.Context, uint64, string) ([]*types.TenantSkillEntity, error) {
- panic("ListSkillsByConfig is outside the reaper surface")
+func (r *reaperSkillStore) ListSkillsByConfig(
+ _ context.Context, tenantID uint64, configID string,
+) ([]*types.TenantSkillEntity, error) {
+ var out []*types.TenantSkillEntity
+ for _, e := range r.rows {
+ if e != nil && e.TenantID == tenantID && e.SandboxConfigID == configID {
+ cp := *e
+ out = append(out, &cp)
+ }
+ }
+ return out, nil
}
func (r *reaperSkillStore) DeleteSkill(_ context.Context, tenantID uint64, configID, skillID string) error {
diff --git a/internal/application/service/tenant_skill_remove.go b/internal/application/service/tenant_skill_remove.go
index a1568e26c..3f1856e9f 100644
--- a/internal/application/service/tenant_skill_remove.go
+++ b/internal/application/service/tenant_skill_remove.go
@@ -218,16 +218,15 @@ func (s *TenantSkillService) runRemove(
if !owned {
return nil
}
- // The generation comes from the config read at the top of this function
- // while switchImagePointer re-reads for everything else, for the same
- // reason it does in an install: SkillImage is written only by an install
- // or a removal and this lock serialises both, whereas the rest of the
- // entity is written by the config service under its own cordon.
- generation := currentGeneration(cfgEntity) + 1
+ ledger, err := s.skills.ListSnapshotsByConfig(ctx, tenantID, configID)
+ if err != nil {
+ return fmt.Errorf("list snapshots of config %s: %w", configID, err)
+ }
+ generation := nextSnapshotGeneration(currentGeneration(cfgEntity), ledger)
// 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(tenantID, configID, generation)
+ snapshotName := skillSnapshotBuildName(tenantID, configID, generation, removeRowID)
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/sandbox/cube_remote_client.go b/internal/sandbox/cube_remote_client.go
index 489956d61..c10de2fb4 100644
--- a/internal/sandbox/cube_remote_client.go
+++ b/internal/sandbox/cube_remote_client.go
@@ -250,9 +250,11 @@ func (c *CubeRemoteClient) EnsureStandardTemplate(ctx context.Context) (*RemoteT
// 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.
+// The previous template is left in the catalog. Deleting it here would race
+// persistSpawnTemplateID: sessions would target a missing ID for the rest of
+// the build, and a persist failure would leave every config pointing at a
+// template that no longer exists. Callers persist a READY replacement first,
+// then DeleteSupersededStandardTemplates.
func (c *CubeRemoteClient) ReplaceStandardTemplate(ctx context.Context) (*RemoteTemplate, error) {
items, err := c.ListTemplates(ctx)
if err != nil {
@@ -281,15 +283,21 @@ func (c *CubeRemoteClient) ReplaceStandardTemplate(ctx context.Context) (*Remote
"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
+ return c.buildStandardTemplate(ctx)
+}
+
+// DeleteSupersededStandardTemplates drops WeKnora templates other than keepID.
+func (c *CubeRemoteClient) DeleteSupersededStandardTemplates(ctx context.Context, keepID string) error {
+ keepID = strings.TrimSpace(keepID)
+ if keepID == "" {
+ return cubeInvalidRequest("DeleteSupersededStandardTemplates", "template ID is required", nil)
}
- if created == nil || strings.TrimSpace(created.ID) == "" || IsTemplateBuildFailed(created.Status) {
- return created, nil
+ items, err := c.ListTemplates(ctx)
+ if err != nil {
+ return err
}
for _, item := range items {
- if !item.Standard || strings.TrimSpace(item.ID) == "" || item.ID == created.ID {
+ if !item.Standard || strings.TrimSpace(item.ID) == "" || item.ID == keepID {
continue
}
logger.Infof(ctx, "cube deleting superseded standard template %s", item.ID)
@@ -299,7 +307,7 @@ func (c *CubeRemoteClient) ReplaceStandardTemplate(ctx context.Context) (*Remote
}
}
}
- return created, nil
+ return nil
}
// rebuildStandardTemplate restarts the build of a template that already exists,
@@ -1194,5 +1202,6 @@ func cubeCredentialPresence(value string) string {
var (
_ RemoteSandboxClient = (*CubeRemoteClient)(nil)
_ RemoteSnapshotManager = (*CubeRemoteClient)(nil)
+ _ RemoteTemplateCatalog = (*CubeRemoteClient)(nil)
_ RemoteSandboxHandle = (*cubeRemoteHandle)(nil)
)
diff --git a/internal/sandbox/cube_template_catalog_test.go b/internal/sandbox/cube_template_catalog_test.go
index d0fc3c06e..b523314a4 100644
--- a/internal/sandbox/cube_template_catalog_test.go
+++ b/internal/sandbox/cube_template_catalog_test.go
@@ -436,7 +436,7 @@ func TestCubeRemoteClientReplaceStandardTemplateRebuildsInPlace(t *testing.T) {
require.Equal(t, true, payload["allowInternetAccess"])
}
-func TestCubeRemoteClientReplaceStandardTemplateBuildsThenDeletesWhenRedoBlocked(t *testing.T) {
+func TestCubeRemoteClientReplaceStandardTemplateBuildsWithoutDeletingWhenRedoBlocked(t *testing.T) {
var deleted atomic.Int32
var payload map[string]any
client := newCubeTemplateClient(t, func(w http.ResponseWriter, r *http.Request) {
@@ -472,10 +472,47 @@ func TestCubeRemoteClientReplaceStandardTemplateBuildsThenDeletesWhenRedoBlocked
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, "building", template.Status)
+ require.Equal(t, int32(0), deleted.Load(),
+ "replace must not retire the READY template while the replacement is still building")
require.Equal(t, DefaultCubeTemplateImage, payload["image"])
}
+func TestCubeRemoteClientDeleteSupersededStandardTemplatesSkipsKeepID(t *testing.T) {
+ var deleted atomic.Int32
+ 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},
+ },
+ {
+ "templateID": "tpl-new",
+ "status": "READY",
+ "imageInfo": DefaultDockerImage,
+ "aliases": []string{StandardTemplateName},
+ },
+ })
+ case r.URL.Path == "/templates/tpl-old" && r.Method == http.MethodDelete:
+ deleted.Add(1)
+ w.WriteHeader(http.StatusNoContent)
+ case r.URL.Path == "/templates/tpl-new" && r.Method == http.MethodDelete:
+ t.Fatal("keepID must not be deleted")
+ case r.URL.Path == "/snapshots" && r.Method == http.MethodGet:
+ writeJSON(w, http.StatusOK, []map[string]any{})
+ default:
+ http.NotFound(w, r)
+ }
+ })
+
+ require.NoError(t, client.DeleteSupersededStandardTemplates(context.Background(), "tpl-new"))
+ require.Equal(t, int32(1), deleted.Load())
+}
+
// 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_snapshot.go b/internal/sandbox/docker_snapshot.go
index 548c752c7..bd2f70fbd 100644
--- a/internal/sandbox/docker_snapshot.go
+++ b/internal/sandbox/docker_snapshot.go
@@ -77,7 +77,6 @@ func (c *DockerRemoteClient) DeleteSnapshot(ctx context.Context, snapshotID stri
return dockerInvalidRequest("DeleteSnapshot", "snapshot ID is required")
}
_, err := c.api.ImageRemove(ctx, id, client.ImageRemoveOptions{
- Force: true,
PruneChildren: true,
})
if err != nil {
diff --git a/internal/sandbox/docker_snapshot_test.go b/internal/sandbox/docker_snapshot_test.go
index 989430ce9..8f731d0fa 100644
--- a/internal/sandbox/docker_snapshot_test.go
+++ b/internal/sandbox/docker_snapshot_test.go
@@ -88,7 +88,8 @@ func TestDockerDeleteSnapshotPrunesUntaggedAncestors(t *testing.T) {
require.True(t, engine.removeImageOptions[0].PruneChildren,
"a delete that keeps untagged parents can never reclaim a retired chain")
- require.True(t, engine.removeImageOptions[0].Force)
+ require.False(t, engine.removeImageOptions[0].Force,
+ "force-remove would untag an image a live session container still holds")
}
// A skill snapshot the ledger cannot name is unreachable: snapshots are always
diff --git a/internal/sandbox/docker_template_catalog.go b/internal/sandbox/docker_template_catalog.go
index 6eeb6d0d5..b17a89bdf 100644
--- a/internal/sandbox/docker_template_catalog.go
+++ b/internal/sandbox/docker_template_catalog.go
@@ -166,6 +166,12 @@ func (c *DockerRemoteClient) ReplaceStandardTemplate(ctx context.Context) (*Remo
return c.EnsureStandardTemplate(ctx)
}
+// DeleteSupersededStandardTemplates is a no-op: Docker has no cluster-side
+// template object, and skill snapshots live in a separate image namespace.
+func (c *DockerRemoteClient) DeleteSupersededStandardTemplates(context.Context, string) error {
+ return nil
+}
+
func (c *DockerRemoteClient) pullInBackground(ctx context.Context, key, image string) {
pullCtx, cancel := context.WithTimeout(ctx, dockerImagePullBudget)
defer cancel()
diff --git a/internal/sandbox/e2b_remote_client.go b/internal/sandbox/e2b_remote_client.go
index 526be4bc1..f835044f9 100644
--- a/internal/sandbox/e2b_remote_client.go
+++ b/internal/sandbox/e2b_remote_client.go
@@ -336,40 +336,26 @@ func (c *E2BRemoteClient) EnsureStandardTemplate(ctx context.Context) (*RemoteTe
// 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.
+// ID. The previous template stays listed until the caller has persisted a
+// spawnable replacement and called DeleteSupersededStandardTemplates: deleting
+// first left stored template_ids pointing at a missing template, and retrying
+// a refused rebuild by deleting the live ID did the same.
func (c *E2BRemoteClient) ReplaceStandardTemplate(ctx context.Context) (*RemoteTemplate, error) {
+ return c.buildStandardTemplate(ctx)
+}
+
+// DeleteSupersededStandardTemplates drops WeKnora templates other than keepID.
+func (c *E2BRemoteClient) DeleteSupersededStandardTemplates(ctx context.Context, keepID string) error {
+ keepID = strings.TrimSpace(keepID)
+ if keepID == "" {
+ return e2bInvalidRequest("DeleteSupersededStandardTemplates", "template ID is required", nil)
+ }
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, &notFound) {
- return nil, normalizeE2BError("DeleteTemplate", delErr)
- }
- }
- }
- return c.buildStandardTemplate(ctx)
+ return err
}
for _, item := range items {
- if !item.Standard || strings.TrimSpace(item.ID) == "" || item.ID == created.ID {
+ if !item.Standard || strings.TrimSpace(item.ID) == "" || item.ID == keepID {
continue
}
logger.Infof(ctx, "e2b deleting superseded standard template %s", item.ID)
@@ -380,7 +366,7 @@ func (c *E2BRemoteClient) ReplaceStandardTemplate(ctx context.Context) (*RemoteT
}
}
}
- return created, nil
+ return nil
}
func (c *E2BRemoteClient) buildStandardTemplate(ctx context.Context) (*RemoteTemplate, error) {
@@ -1288,5 +1274,6 @@ func e2bRemoteEntryType(fileType string) RemoteDirEntryType {
var (
_ RemoteSandboxClient = (*E2BRemoteClient)(nil)
_ RemoteSnapshotManager = (*E2BRemoteClient)(nil)
+ _ RemoteTemplateCatalog = (*E2BRemoteClient)(nil)
_ RemoteSandboxHandle = (*e2bRemoteHandle)(nil)
)
diff --git a/internal/sandbox/template_catalog.go b/internal/sandbox/template_catalog.go
index 277369877..669566220 100644
--- a/internal/sandbox/template_catalog.go
+++ b/internal/sandbox/template_catalog.go
@@ -48,9 +48,13 @@ type RemoteTemplateCatalog interface {
EnsureStandardTemplate(ctx context.Context) (*RemoteTemplate, error)
// 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.
+ // any other way. It must not delete a usable template: callers persist
+ // the replacement ID first, then DeleteSupersededStandardTemplates.
ReplaceStandardTemplate(ctx context.Context) (*RemoteTemplate, error)
+ // DeleteSupersededStandardTemplates removes WeKnora templates other than
+ // keepID. Call only after keepID is spawnable and has been written onto
+ // every config that still pointed at the previous standard template.
+ DeleteSupersededStandardTemplates(ctx context.Context, keepID string) error
}
func isStandardTemplate(name string) bool {
@@ -111,3 +115,15 @@ func IsTemplateBuildFailed(status string) bool {
return false
}
}
+
+// IsTemplateReady reports whether a template can spawn a sandbox. Rebuild
+// replacements stay listed alongside the previous READY template until this
+// is true, so sessions never lose a spawnable ID mid-build.
+func IsTemplateReady(status string) bool {
+ switch strings.ToLower(strings.TrimSpace(status)) {
+ case "ready", "available", "complete", "completed", "success", "succeeded":
+ return true
+ default:
+ return false
+ }
+}