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(skills): keep catalog archives when uninstalling from a sandbox
wizardchen4f670bce7d05
diff --git a/internal/application/service/tenant_sandbox_config.go b/internal/application/service/tenant_sandbox_config.goindex 77394cc81..9c8a95825 100644--- a/internal/application/service/tenant_sandbox_config.go+++ b/internal/application/service/tenant_sandbox_config.go@@ -289,9 +289,9 @@ type TenantSandboxConfigService struct {globalCfg *sandbox.Confignow func() time.Time- // skills and files are used only when deleting a config, to destroy the- // snapshot ledger and drop skill archives. Either may be nil in tests- // that never delete a config that owns skills.+ // skills is used when deleting a config, to destroy the snapshot ledger+ // and drop install rows. Catalog archives are owned by the skill+ // definition and are not deleted here.skills sandboxConfigSkillStorefiles sandboxConfigBundleResolver@@ -1286,7 +1286,6 @@ func (s *TenantSandboxConfigService) cleanupSkillMetadata(if skill == nil {continue}- s.deleteSkillBundleBestEffort(ctx, tenantID, skill.BundleRef)if err := s.skills.DeleteSkill(ctx, tenantID, configID, skill.ID); err != nil {logger.Warnf(ctx, "[sandbox] delete skill %s on config %s failed: %v",skill.ID, configID, err)@@ -1304,23 +1303,6 @@ func (s *TenantSandboxConfigService) cleanupSkillMetadata(}}-func (s *TenantSandboxConfigService) deleteSkillBundleBestEffort(- ctx context.Context, tenantID uint64, bundleRef string,-) {- if s.files == nil || strings.TrimSpace(bundleRef) == "" {- return- }- fs, _, err := s.files.ResolveFileService(ctx, &types.Tenant{ID: tenantID}, "", "", "")- if err != nil || fs == nil {- logger.Warnf(ctx, "[sandbox] resolve file service to delete bundle %s failed: %v",- bundleRef, err)- return- }- if err := fs.DeleteFile(ctx, bundleRef); err != nil {- logger.Warnf(ctx, "[sandbox] delete bundle %s failed: %v", bundleRef, err)- }-}-func (s *TenantSandboxConfigService) clientFor(cfg *types.TenantSandboxConfig,) (sandbox.ConfigSandboxClient, error) {diff --git a/internal/application/service/tenant_sandbox_config_test.go b/internal/application/service/tenant_sandbox_config_test.goindex f38ad79f7..e53eaf89c 100644--- a/internal/application/service/tenant_sandbox_config_test.go+++ b/internal/application/service/tenant_sandbox_config_test.go@@ -1538,7 +1538,8 @@ func TestDeleteReleasesSkillSnapshotsBeforeSoftDelete(t *testing.T) {require.NotContains(t, fx.skills.marks, "row-0:"+types.SkillSnapshotStateDeleted)require.Empty(t, fx.skills.skills, "tenant_skills rows must be dropped before SoftDelete")require.True(t, fx.skills.ledgerCleared)- require.Equal(t, []string{"bundle://skill-a.zip"}, fx.files.deleted)+ require.Empty(t, fx.files.deleted,+ "deleting a sandbox must not drop the catalog archive")// DeleteSkill only takes values filed under a skill; the config-wide ones// would outlive the config without this.require.Equal(t, []string{"7:cfg-a"}, fx.skills.envVarsClearedFor)diff --git a/internal/application/service/tenant_skill_catalog.go b/internal/application/service/tenant_skill_catalog.goindex ef9762b7b..57bd7278f 100644--- a/internal/application/service/tenant_skill_catalog.go+++ b/internal/application/service/tenant_skill_catalog.go@@ -242,7 +242,8 @@ func (s *TenantSkillService) InstallCatalogToConfigs(return result, nil}-// DeleteCatalog removes a definition that has no remaining installations.+// DeleteCatalog removes a definition that has no remaining installations+// and drops the stored zip. Sandbox uninstalls never delete that zip.func (s *TenantSkillService) DeleteCatalog(ctx context.Context, tenantID uint64, catalogID string,) error {@@ -264,7 +265,14 @@ func (s *TenantSkillService) DeleteCatalog(return apperrors.NewConflictError("remove this skill from every sandbox before deleting it from the catalog")}- return s.skills.DeleteCatalog(ctx, tenantID, catalogID)+ ref := strings.TrimSpace(catalog.BundleRef)+ if err := s.skills.DeleteCatalog(ctx, tenantID, catalogID); err != nil {+ return err+ }+ if ref != "" {+ s.deleteBundleBestEffort(ctx, tenantID, ref)+ }+ return nil}func (s *TenantSkillService) upsertCatalogFromBundle(diff --git a/internal/application/service/tenant_skill_catalog_test.go b/internal/application/service/tenant_skill_catalog_test.goindex 8afaaa02b..eba1ecef9 100644--- a/internal/application/service/tenant_skill_catalog_test.go+++ b/internal/application/service/tenant_skill_catalog_test.go@@ -118,6 +118,62 @@ func TestDeleteCatalogRefusesWhileARemovalIsInFlight(t *testing.T) {require.Equal(t, 409, appErr.HTTPCode)}+func TestInstallSkillStoresTheArchiveOnTheCatalog(t *testing.T) {+ fx := newInstallFixture(t)+ ctx := context.Background()+ archive := zipBundle(t, map[string]string{+ "SKILL.md": validSkillMD,+ "scripts/extract.py": "print('hi')\n",+ })++ id, err := fx.svc.InstallSkill(ctx, 7, "cfg-1", archive)+ require.NoError(t, err)++ cat, err := fx.skillRepo.GetCatalogByName(ctx, 7, "pdf-tools")+ require.NoError(t, err)+ require.NotNil(t, cat)+ require.Equal(t, "file://bundle.zip", cat.BundleRef)++ skill, err := fx.skillRepo.GetSkill(ctx, 7, "cfg-1", id)+ require.NoError(t, err)+ require.Equal(t, cat.ID, skill.CatalogID)+ require.Equal(t, cat.BundleRef, skill.BundleRef,+ "the install row locates the same catalog object; it must not own a sandbox-scoped copy")+}++func TestRemovingLastSandboxInstallKeepsTheCatalogArchive(t *testing.T) {+ fx := newInstallFixture(t)+ ctx := context.Background()+ archive := zipBundle(t, map[string]string{+ "SKILL.md": validSkillMD,+ "scripts/extract.py": "print('hi')\n",+ })+ id, err := fx.svc.InstallSkill(ctx, 7, "cfg-1", archive)+ require.NoError(t, err)+ skill, err := fx.skillRepo.GetSkill(ctx, 7, "cfg-1", id)+ require.NoError(t, err)+ skill.Status = types.SkillStatusRemoving+ require.NoError(t, fx.skillRepo.UpdateSkill(ctx, skill))++ require.NoError(t, fx.svc.runRemove(ctx, 7, "cfg-1", id))++ cat, err := fx.skillRepo.GetCatalogByName(ctx, 7, "pdf-tools")+ require.NoError(t, err)+ require.NotNil(t, cat)+ files, err := fx.svc.ListCatalogFiles(ctx, 7, cat.ID)+ require.NoError(t, err)+ require.NotEmpty(t, files)+ require.Empty(t, fx.deletedBundles,+ "uninstalling from the last sandbox must not delete the definition zip")+ gone, err := fx.skillRepo.GetSkill(ctx, 7, "cfg-1", id)+ require.NoError(t, err)+ require.Nil(t, gone)++ require.NoError(t, fx.svc.DeleteCatalog(ctx, 7, cat.ID))+ require.Equal(t, []string{"file://bundle.zip"}, fx.deletedBundles,+ "only deleting the skill from the catalog drops the stored zip")+}+func TestUpsertCatalogDoesNotStampSHAWhenStoreFails(t *testing.T) {fx := newInstallFixture(t)fx.saveErr = errors.New("disk full")diff --git a/internal/application/service/tenant_skill_install.go b/internal/application/service/tenant_skill_install.goindex abccf068b..ea702d910 100644--- a/internal/application/service/tenant_skill_install.go+++ b/internal/application/service/tenant_skill_install.go@@ -84,46 +84,28 @@ func (s *TenantSkillService) InstallSkill(return "", err}if s.canSkipInstall(ctx, existing, bundle) {- catalog, catalogErr := s.upsertCatalogFromBundle(ctx, tenantID, bundle, archive, false)+ catalog, catalogErr := s.upsertCatalogFromBundle(ctx, tenantID, bundle, archive, true)if catalogErr != nil {- return "", fmt.Errorf("record skill catalog: %w", catalogErr)+ return "", fmt.Errorf("store bundle for skill %s: %w", existing.ID, catalogErr)}- if err := s.refreshSkippedBundle(ctx, existing, archive); err != nil {+ if err := s.pointInstallAtCatalog(ctx, existing, catalog); err != nil {return "", fmt.Errorf("store bundle for skill %s: %w", existing.ID, err)}- if catalog != nil && existing.CatalogID != catalog.ID {- if err := s.updateSkillFields(ctx, tenantID, configID, existing.ID, func(e *types.TenantSkillEntity) {- e.CatalogID = catalog.ID- }); err != nil {- return "", err- }- }return existing.ID, nil}- catalog, err := s.upsertCatalogFromBundle(ctx, tenantID, bundle, archive, false)- if err != nil {- return "", fmt.Errorf("record skill catalog: %w", err)- }- catalogID := ""- if catalog != nil {- catalogID = catalog.ID- }-skillID := uuid.NewString()now := s.now()if existing != nil {skillID = existing.IDtakeSkillRowForInstall(existing, bundle, now)- existing.CatalogID = catalogIDif err := s.skills.UpdateSkill(ctx, existing); err != nil {return "", err}} else {if err := s.skills.CreateSkill(ctx, &types.TenantSkillEntity{ID: skillID, TenantID: tenantID, SandboxConfigID: configID,- CatalogID: catalogID,- Name: bundle.Name, Version: bundle.Version,+ Name: bundle.Name, Version: bundle.Version,Description: bundle.Description, Instructions: bundle.Instructions,BundleSHA256: bundle.SHA256, Enabled: true,Status: types.SkillStatusInstalling, InstallingSince: &now,@@ -142,18 +124,17 @@ func (s *TenantSkillService) InstallSkill(}skillID = winner.IDtakeSkillRowForInstall(winner, bundle, now)- winner.CatalogID = catalogIDif err := s.skills.UpdateSkill(ctx, winner); err != nil {return "", err}}}- // Store the archive before the long-running part: read_skill serves file- // contents from it, and a re-install after a crash needs it too. A missing- // archive is a failed install, not a warning — otherwise the row says- // "installing" and later reads have nothing to serve.- ref, err := s.saveBundle(ctx, tenantID, skillID, archive)+ // The zip lives on the catalog, not on this sandbox: uninstalling from+ // the last config must not take the definition's files with it. The+ // install row only copies the catalog ref so read_skill / retry can+ // find the same object.+ catalog, err := s.upsertCatalogFromBundle(ctx, tenantID, bundle, archive, true)if err != nil {failCtx, cancelFail := s.cleanupContext(ctx)defer cancelFail()@@ -163,9 +144,17 @@ func (s *TenantSkillService) InstallSkill(s.failSkill(failCtx, tenantID, configID, skillID, bundle, storeErr)return "", fmt.Errorf("store bundle for skill %s: %w", skillID, err)}- _ = s.updateSkillFields(ctx, tenantID, configID, skillID, func(e *types.TenantSkillEntity) {- e.BundleRef = ref- })+ if err := s.pointInstallAtCatalog(ctx, &types.TenantSkillEntity{+ ID: skillID, TenantID: tenantID, SandboxConfigID: configID,+ }, catalog); err != nil {+ failCtx, cancelFail := s.cleanupContext(ctx)+ defer cancelFail()+ storeErr := fmt.Errorf("store bundle: %w", err)+ logger.Errorf(ctx, "[skill] store bundle failed tenant=%d config=%s skill=%s name=%s: %v",+ tenantID, configID, skillID, bundle.Name, err)+ s.failSkill(failCtx, tenantID, configID, skillID, bundle, storeErr)+ return "", fmt.Errorf("store bundle for skill %s: %w", skillID, err)+ }s.publishProgress(ctx, tenantID, configID, skillID, SkillProgress{Percent: 10, Stage: "accepted", Status: types.SkillStatusInstalling,@@ -227,18 +216,18 @@ func (s *TenantSkillService) ReinstallSkill(if skill == nil {return "", apperrors.NewNotFoundError("skill not found")}- // Reported apart from a generic read failure: nothing a retry does can- // recover a skill whose archive is gone, and the operator needs to be told- // to upload it rather than to press the button again.- if strings.TrimSpace(skill.BundleRef) == "" {- return "", apperrors.NewBadRequestError(- "the archive of this skill is no longer stored; install it again from the original bundle",- )- }+ // The zip is owned by the catalog. An empty install BundleRef is not+ // itself a failure — fall through to skillBundleArchive, which is what+ // reports a definition whose files are actually gone.archive, err := s.skillBundleArchive(ctx, tenantID, configID, skillID)if err != nil {return "", err}+ if len(archive) == 0 {+ return "", apperrors.NewBadRequestError(+ "the archive of this skill is no longer stored; install it again from the original bundle",+ )+ }return s.InstallSkill(ctx, tenantID, configID, archive)}@@ -1153,14 +1142,23 @@ func (s *TenantSkillService) fileServiceForTenant(return fs, nil}-func (s *TenantSkillService) saveBundle(- ctx context.Context, tenantID uint64, skillID string, archive []byte,-) (string, error) {- fs, err := s.fileServiceForTenant(ctx, tenantID)- if err != nil {- return "", err+// pointInstallAtCatalog copies the catalog's stored zip onto the install row.+// The bytes themselves stay on the definition: this is only a locator so a+// sandbox-scoped read can find the same object after the catalog id is set.+func (s *TenantSkillService) pointInstallAtCatalog(+ ctx context.Context, skill *types.TenantSkillEntity, catalog *types.TenantSkillCatalogEntity,+) error {+ if skill == nil {+ return nil}- return fs.SaveBytes(ctx, archive, tenantID, fmt.Sprintf("tenant-skills/%s.zip", skillID), false)+ if catalog == nil || strings.TrimSpace(catalog.BundleRef) == "" {+ return fmt.Errorf("catalog archive is missing")+ }+ return s.updateSkillFields(ctx, skill.TenantID, skill.SandboxConfigID, skill.ID,+ func(e *types.TenantSkillEntity) {+ e.CatalogID = catalog.ID+ e.BundleRef = catalog.BundleRef+ })}// updateSkillFields loads, mutates and writes back one skill row. It logs on@@ -1404,27 +1402,6 @@ func (s *TenantSkillService) beatInstallHeartbeat(}}-// refreshSkippedBundle stores the uploaded archive even when the image work-// is skipped. read_skill serves file contents from it, so a re-upload of a-// ready skill is how a missing object-store blob gets repaired without-// growing a new snapshot. A failure here is returned to the caller rather-// than turning the ready row into a failed install.-func (s *TenantSkillService) refreshSkippedBundle(- ctx context.Context, existing *types.TenantSkillEntity, archive []byte,-) error {- if existing == nil {- return nil- }- ref, err := s.saveBundle(ctx, existing.TenantID, existing.ID, archive)- if err != nil {- return err- }- return s.updateSkillFields(ctx, existing.TenantID, existing.SandboxConfigID, existing.ID,- func(e *types.TenantSkillEntity) {- e.BundleRef = ref- })-}-// startMaintenanceSession opens the session one image operation runs in. The// operation name is carried into the session because the transcript is kept// deliberately, for troubleshooting: filing a removal's under "Skill install"diff --git a/internal/application/service/tenant_skill_install_test.go b/internal/application/service/tenant_skill_install_test.goindex 319c0f500..ba4414994 100644--- a/internal/application/service/tenant_skill_install_test.go+++ b/internal/application/service/tenant_skill_install_test.go@@ -849,7 +849,7 @@ func TestReinstallSkillRefusesWhenTheArchiveIsGone(t *testing.T) {_, err := fx.svc.ReinstallSkill(context.Background(), 7, "cfg-1", "sk-1")require.Error(t, err)- require.ErrorContains(t, err, "no longer stored")+ require.ErrorContains(t, err, "not available")require.Empty(t, fx.sessionCalls, "a retry that cannot run must not boot a billed sandbox")}diff --git a/internal/application/service/tenant_skill_reaper.go b/internal/application/service/tenant_skill_reaper.goindex fada45dbc..bf78b6cbd 100644--- a/internal/application/service/tenant_skill_reaper.go+++ b/internal/application/service/tenant_skill_reaper.go@@ -134,9 +134,10 @@ func (s *TenantSkillService) ReapStuckRuns(ctx context.Context) (int, error) {}reaped++case types.SkillStatusRemoving:- // Deleting the row and its bundle is the one irreversible thing- // the reaper does, so a run it cannot judge is left for the next- // sweep rather than guessed at.+ // Deleting the leftover install row is irreversible, so a run the+ // reaper cannot judge is left for the next sweep rather than+ // guessed at. The catalog archive is not touched: this is a+ // sandbox install, not a definition delete.if !known {continue}@@ -160,9 +161,6 @@ func (s *TenantSkillService) ReapStuckRuns(ctx context.Context) (int, error) {logger.Warnf(ctx, "[skill] drop abandoned removal %s failed: %v", row.ID, err)continue}- if row.BundleRef != "" {- s.deleteBundleBestEffort(ctx, row.TenantID, row.BundleRef)- }reaped++}}diff --git a/internal/application/service/tenant_skill_remove.go b/internal/application/service/tenant_skill_remove.goindex 3f1856e9f..9a1eade1b 100644--- a/internal/application/service/tenant_skill_remove.go+++ b/internal/application/service/tenant_skill_remove.go@@ -378,11 +378,11 @@ func (s *TenantSkillService) clearImagePointer(return s.configs.Update(ctx, cfgEntity)}-// finishRemoval drops the DB row and the stored archive, then invalidates-// bound sandboxes. It runs only once the image no longer contains the skill,-// which is also why the delete is retried rather than reported as a failed-// removal: the files are gone and re-running the whole flow to fix a row would-// be absurd.+// finishRemoval drops the DB row, then invalidates bound sandboxes. The+// catalog archive is left alone: uninstalling from a sandbox must not+// destroy the definition. It runs only once the image no longer contains+// the skill, which is also why the row delete is retried rather than+// reported as a failed removal.func (s *TenantSkillService) finishRemoval(cleanupBase context.Context, tenantID uint64, configID, skillID string, imageChanged bool,) error {@@ -397,22 +397,12 @@ func (s *TenantSkillService) finishRemoval(return nil}- skill, err := s.skills.GetSkill(ctx, tenantID, configID, skillID)- if err != nil {- return err- }- // The row goes first. A surviving row whose BundleRef names a deleted- // object is a broken record - read_skill would serve nothing - whereas an- // archive whose row is gone is only unreferenced bytes.if err := s.retrySkillBookkeeping(ctx, func() error {return s.skills.DeleteSkill(ctx, tenantID, configID, skillID)}); err != nil {return fmt.Errorf("skill %s no longer exists in the image but its row remains: %w",skillID, err)}- if skill != nil && skill.BundleRef != "" {- s.deleteBundleBestEffort(ctx, tenantID, skill.BundleRef)- }// Only an image that actually changed can leave a bound sandbox out of// date. Marking after a removal that moved no pointer would destroy and// rebuild every live sandbox of the config - throwing away each session'sdiff --git a/internal/application/service/tenant_skill_remove_test.go b/internal/application/service/tenant_skill_remove_test.goindex 854e70bec..a2c1e27b2 100644--- a/internal/application/service/tenant_skill_remove_test.go+++ b/internal/application/service/tenant_skill_remove_test.go@@ -38,8 +38,8 @@ func TestRunRemoveProducesANewSnapshotWithoutTheSkillDir(t *testing.T) {"the maintenance session is kept for troubleshooting")require.Equal(t, []string{"Skill remove"}, fx.sessionTitles,"the transcript is kept to troubleshoot this operation, so it must name it")- require.Equal(t, []string{"file://sk-1.zip"}, fx.deletedBundles,- "the archive outlives nothing: its only readers are the removed row and read_skill")+ require.Empty(t, fx.deletedBundles,+ "the zip belongs to the catalog, not this sandbox install")skill, err := fx.skillRepo.GetSkill(context.Background(), 7, "cfg-1", "sk-1")require.NoError(t, err)@@ -256,8 +256,8 @@ func TestRunRemoveDeletesTheOrphanSnapshotAfterTheLockIsLost(t *testing.T) {require.Equal(t, []string{"sess-1"}, fx.destroyedSandboxes)}-// The archive is deleted only once the row that names it is gone. The other-// order leaves a row pointing at an object that no longer exists.+// The catalog archive is independent of this sandbox row. A failed row+// delete must not take the definition zip with it either.func TestRunRemoveKeepsTheBundleWhenTheRowCannotBeDeleted(t *testing.T) {fx := newInstallFixture(t)fx.seedInstalledSkill("sk-1", "snap-old", 2)