refactor(skills): enhance skill bundle management and catalog handling
- Introduced loadInstalledSkillBundle to streamline the retrieval of installed skill archives. - Updated pinInstallsToReplacedBundle to ensure proper handling of installations when catalog definitions are updated. - Enhanced error handling and logging for skill file retrieval and catalog archive management. - Added tests to verify the behavior of skill installations and catalog updates, ensuring that previous archives are retained when necessary.main
parent
b6243bd985
commit
a2315add0b
|
|
@ -571,35 +571,50 @@ func (s *agentService) tenantSkillSource(
|
|||
// downloads start failing for installed skills only, and loadBundle needs
|
||||
// a ctx parameter.
|
||||
return skills.NewTenantSkillSource(rows, func(row *types.TenantSkillEntity) ([]byte, error) {
|
||||
ref, err := s.skillBundleRef(ctx, ownerTenantID, row)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.readSkillBundle(ctx, ownerTenantID, ref)
|
||||
return s.loadInstalledSkillBundle(ctx, ownerTenantID, row)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *agentService) skillBundleRef(
|
||||
// loadInstalledSkillBundle reads the archive one installed skill was built
|
||||
// from. An object named by the row itself is that archive. A row that only
|
||||
// names a catalog is answered from the definition's zip, but the definition is
|
||||
// mutable — registering the same skill again replaces those bytes in place,
|
||||
// while this sandbox goes on running the image built from the previous ones.
|
||||
// read_skill is documented as serving what was installed, so a definition that
|
||||
// has moved on is reported rather than substituted.
|
||||
func (s *agentService) loadInstalledSkillBundle(
|
||||
ctx context.Context, tenantID uint64, row *types.TenantSkillEntity,
|
||||
) (string, error) {
|
||||
) ([]byte, error) {
|
||||
if row == nil {
|
||||
return "", errors.New("skill is required")
|
||||
return nil, errors.New("skill is required")
|
||||
}
|
||||
if ref := strings.TrimSpace(row.BundleRef); ref != "" {
|
||||
return ref, nil
|
||||
return s.readSkillBundle(ctx, tenantID, ref)
|
||||
}
|
||||
cid := strings.TrimSpace(row.CatalogID)
|
||||
if cid == "" || s.db == nil {
|
||||
return "", fmt.Errorf("skill %s has no stored bundle; its files cannot be read", row.Name)
|
||||
return nil, fmt.Errorf("skill %s has no stored bundle; its files cannot be read", row.Name)
|
||||
}
|
||||
cat, err := repository.NewTenantSkillRepository(s.db).GetCatalog(ctx, tenantID, cid)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
if cat == nil || strings.TrimSpace(cat.BundleRef) == "" {
|
||||
return "", fmt.Errorf("skill %s has no stored bundle; its files cannot be read", row.Name)
|
||||
return nil, fmt.Errorf("skill %s has no stored bundle; its files cannot be read", row.Name)
|
||||
}
|
||||
return strings.TrimSpace(cat.BundleRef), nil
|
||||
archive, err := s.readSkillBundle(ctx, tenantID, strings.TrimSpace(cat.BundleRef))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// The bytes are checked, not the catalog's recorded digest: a reference and
|
||||
// a digest that disagree is exactly the case a stamped-but-unstored archive
|
||||
// leaves behind.
|
||||
if want := strings.TrimSpace(row.BundleSHA256); want != "" && !archiveMatchesSHA(archive, want) {
|
||||
return nil, fmt.Errorf(
|
||||
"skill %s was registered again from a different archive; "+
|
||||
"reinstall it on this sandbox before reading its files", row.Name)
|
||||
}
|
||||
return archive, nil
|
||||
}
|
||||
|
||||
// userEnvResolver builds the per-caller environment resolver for this run, or
|
||||
|
|
|
|||
|
|
@ -354,6 +354,7 @@ func (s *TenantSkillService) storeCatalogBundle(
|
|||
return false, nil
|
||||
}
|
||||
oldRef := strings.TrimSpace(catalog.BundleRef)
|
||||
oldSHA := strings.TrimSpace(catalog.BundleSHA256)
|
||||
ref, err := fs.SaveBytes(ctx, archive, tenantID,
|
||||
fmt.Sprintf("tenant-skills/catalog/%s.zip", catalog.ID), false)
|
||||
if err != nil {
|
||||
|
|
@ -364,12 +365,59 @@ func (s *TenantSkillService) storeCatalogBundle(
|
|||
return false, nil
|
||||
}
|
||||
catalog.BundleRef = ref
|
||||
if oldRef != "" && oldRef != ref {
|
||||
if oldRef != "" && oldRef != ref && !s.pinInstallsToReplacedBundle(ctx, tenantID, catalog, oldRef, oldSHA) {
|
||||
s.deleteBundleBestEffort(ctx, tenantID, oldRef)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// pinInstallsToReplacedBundle hands the archive a definition just replaced to
|
||||
// the installs that are still serving it, and reports whether the object has to
|
||||
// be kept.
|
||||
//
|
||||
// A definition is mutable; an installation is not. Re-registering a skill from
|
||||
// a newer zip leaves every sandbox running the image built from the old one, so
|
||||
// deleting those bytes would take read_skill and the admin file browser down
|
||||
// for installs that are working perfectly. The row is what pins the object:
|
||||
// once it names it, nothing else has to remember which version it was.
|
||||
func (s *TenantSkillService) pinInstallsToReplacedBundle(
|
||||
ctx context.Context, tenantID uint64, catalog *types.TenantSkillCatalogEntity, oldRef, oldSHA string,
|
||||
) bool {
|
||||
installs, err := s.skills.ListSkillsByCatalog(ctx, tenantID, catalog.ID)
|
||||
if err != nil {
|
||||
// Keeping an archive nothing reads costs storage; dropping one an
|
||||
// install still needs costs that install its files.
|
||||
logger.Warnf(ctx, "[skill] list installs of catalog %s before replacing its bundle failed: %v",
|
||||
catalog.ID, err)
|
||||
return true
|
||||
}
|
||||
keep := false
|
||||
for _, row := range installs {
|
||||
if row == nil || strings.TrimSpace(row.BundleRef) != "" {
|
||||
continue
|
||||
}
|
||||
if oldSHA == "" {
|
||||
// The definition never recorded what those bytes were, so no row
|
||||
// can be matched against them. Keep rather than orphan.
|
||||
keep = true
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(row.BundleSHA256) != oldSHA {
|
||||
continue
|
||||
}
|
||||
// Best-effort: a row that could not be stamped still reads the object
|
||||
// through the catalog until the next write, and keep is set either way
|
||||
// so the bytes it needs stay where they are.
|
||||
if err := s.updateSkillFields(ctx, row.TenantID, row.SandboxConfigID, row.ID,
|
||||
func(e *types.TenantSkillEntity) { e.BundleRef = oldRef }); err != nil {
|
||||
logger.Warnf(ctx, "[skill] pin install %s to the replaced archive of catalog %s failed: %v",
|
||||
row.ID, catalog.ID, err)
|
||||
}
|
||||
keep = true
|
||||
}
|
||||
return keep
|
||||
}
|
||||
|
||||
func skillUserErrorMessage(err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
|
|
@ -485,8 +533,11 @@ func (s *TenantSkillService) catalogBundleArchive(
|
|||
if row == nil || strings.TrimSpace(row.BundleRef) == "" {
|
||||
return nil, false
|
||||
}
|
||||
archive, err := s.downloadSkillBundle(ctx, tenantID, row)
|
||||
if err != nil || len(archive) == 0 {
|
||||
// Through the same cache the install rows use: the file drawer lists a
|
||||
// tree and then opens files out of it, and a definition-owned archive
|
||||
// is no cheaper to pull twice than a row-owned one.
|
||||
archive, ok := s.trySkillBundle(ctx, tenantID, row)
|
||||
if !ok || len(archive) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
if wantSHA != "" && !archiveMatchesSHA(archive, wantSHA) {
|
||||
|
|
|
|||
|
|
@ -241,3 +241,93 @@ func TestRegisterCatalogReplacesThePreviousZip(t *testing.T) {
|
|||
require.Equal(t, []string{firstRef}, fx.deletedBundles,
|
||||
"replacing the definition zip must drop the previous object")
|
||||
}
|
||||
|
||||
// The sandbox that installed v1 goes on running v1 no matter what the
|
||||
// definition says next, so the archive it was built from has to outlive the
|
||||
// re-registration that supersedes it. Deleting it here is what would take
|
||||
// read_skill and the admin file browser down for an install that works.
|
||||
func TestRegisterCatalogKeepsTheReplacedZipForInstallsStillOnIt(t *testing.T) {
|
||||
fx := newInstallFixture(t)
|
||||
ctx := context.Background()
|
||||
first := zipBundle(t, map[string]string{
|
||||
"SKILL.md": validSkillMD,
|
||||
"scripts/extract.py": "print('v1')\n",
|
||||
})
|
||||
firstBundle, err := ParseSkillBundle(first)
|
||||
require.NoError(t, err)
|
||||
skillID, err := fx.svc.InstallSkill(ctx, 7, "cfg-1", first)
|
||||
require.NoError(t, err)
|
||||
installed, err := fx.skillRepo.GetSkill(ctx, 7, "cfg-1", skillID)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, installed.BundleRef, "a fresh install reads the definition's copy")
|
||||
firstRef := fx.catalogRefFor(t, installed.CatalogID)
|
||||
|
||||
second := zipBundle(t, map[string]string{
|
||||
"SKILL.md": validSkillMD,
|
||||
"scripts/extract.py": "print('v2')\n",
|
||||
})
|
||||
_, err = fx.svc.RegisterCatalogFromArchive(ctx, 7, second)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Empty(t, fx.deletedBundles,
|
||||
"an archive a live install was built from must survive the definition moving on")
|
||||
installed, err = fx.skillRepo.GetSkill(ctx, 7, "cfg-1", skillID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, firstRef, installed.BundleRef,
|
||||
"the install now names the archive itself, so nothing has to remember its version")
|
||||
require.Equal(t, firstBundle.SHA256, installed.BundleSHA256)
|
||||
|
||||
// The point of keeping the object: the file browser still answers with the
|
||||
// tree this sandbox actually has.
|
||||
files, err := fx.svc.ListSkillFiles(ctx, 7, "cfg-1", skillID)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, files)
|
||||
content, err := fx.svc.ReadSkillFile(ctx, 7, "cfg-1", skillID, "scripts/extract.py")
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, content.Content, "v1")
|
||||
}
|
||||
|
||||
// The mirror of the test above: once the last install of a pinned archive is
|
||||
// gone, nothing can reach those bytes again, so removal is what reclaims them.
|
||||
func TestRemovingThePinnedInstallReclaimsTheReplacedZip(t *testing.T) {
|
||||
fx := newInstallFixture(t)
|
||||
ctx := context.Background()
|
||||
first := zipBundle(t, map[string]string{
|
||||
"SKILL.md": validSkillMD,
|
||||
"scripts/extract.py": "print('v1')\n",
|
||||
})
|
||||
skillID, err := fx.svc.InstallSkill(ctx, 7, "cfg-1", first)
|
||||
require.NoError(t, err)
|
||||
installed, err := fx.skillRepo.GetSkill(ctx, 7, "cfg-1", skillID)
|
||||
require.NoError(t, err)
|
||||
catalogID := installed.CatalogID
|
||||
firstRef := fx.catalogRefFor(t, catalogID)
|
||||
|
||||
second := zipBundle(t, map[string]string{
|
||||
"SKILL.md": validSkillMD,
|
||||
"scripts/extract.py": "print('v2')\n",
|
||||
})
|
||||
_, err = fx.svc.RegisterCatalogFromArchive(ctx, 7, second)
|
||||
require.NoError(t, err)
|
||||
secondRef := fx.catalogRefFor(t, catalogID)
|
||||
require.NotEqual(t, firstRef, secondRef)
|
||||
|
||||
pinned, err := fx.skillRepo.GetSkill(ctx, 7, "cfg-1", skillID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, firstRef, pinned.BundleRef)
|
||||
pinned.Status = types.SkillStatusRemoving
|
||||
require.NoError(t, fx.skillRepo.UpdateSkill(ctx, pinned))
|
||||
|
||||
require.NoError(t, fx.svc.runRemove(ctx, 7, "cfg-1", skillID))
|
||||
|
||||
require.Equal(t, []string{firstRef}, fx.deletedBundles,
|
||||
"the pinned archive is reclaimed with its last reader, the definition's is not")
|
||||
}
|
||||
|
||||
func (f *installFixture) catalogRefFor(t *testing.T, catalogID string) string {
|
||||
t.Helper()
|
||||
cat, err := f.skillRepo.GetCatalog(context.Background(), 7, catalogID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, cat)
|
||||
return cat.BundleRef
|
||||
}
|
||||
|
|
|
|||
|
|
@ -103,28 +103,41 @@ func (s *TenantSkillService) skillBundleArchive(
|
|||
if skill == nil {
|
||||
return nil, apperrors.NewNotFoundError("skill not found")
|
||||
}
|
||||
hasRef := strings.TrimSpace(skill.BundleRef) != ""
|
||||
// An object named by the row itself is the archive this sandbox was built
|
||||
// from, so it answers first and needs no digest check.
|
||||
if archive, ok := s.trySkillBundle(ctx, tenantID, skill); ok {
|
||||
return archive, nil
|
||||
}
|
||||
if hasRef {
|
||||
// The install named a blob that we could not read. Only substitute the
|
||||
// catalog copy when it is the same digest — otherwise the admin would
|
||||
// be looking at a different version than the image.
|
||||
if archive, err := s.sameDigestCatalogArchive(ctx, tenantID, skill); err == nil && len(archive) > 0 {
|
||||
// Otherwise the definition holds the zip. It answers for this install only
|
||||
// while the digests agree: registering the skill again replaces the catalog
|
||||
// object in place, while every sandbox keeps running the image built from
|
||||
// the archive its row names. Serving the newer bytes would show the admin
|
||||
// (and read_skill) a tree that image does not have.
|
||||
if archive, err := s.sameDigestCatalogArchive(ctx, tenantID, skill); err == nil && len(archive) > 0 {
|
||||
return archive, nil
|
||||
}
|
||||
if strings.TrimSpace(skill.BundleSHA256) == "" {
|
||||
// A row that recorded no digest predates the catalog and has nothing to
|
||||
// check against, so the definition's copy is the only answer available.
|
||||
if archive, err := s.anyCatalogArchiveFor(ctx, tenantID, skill); err == nil && len(archive) > 0 {
|
||||
return archive, nil
|
||||
}
|
||||
return nil, apperrors.NewNotFoundError("skill files are not available")
|
||||
}
|
||||
return nil, apperrors.NewNotFoundError("skill files are not available")
|
||||
}
|
||||
|
||||
// anyCatalogArchiveFor resolves the definition an install belongs to without
|
||||
// checking what it holds. It exists for rows written before the catalog, which
|
||||
// carry neither a bundle reference nor a digest.
|
||||
func (s *TenantSkillService) anyCatalogArchiveFor(
|
||||
ctx context.Context, tenantID uint64, skill *types.TenantSkillEntity,
|
||||
) ([]byte, error) {
|
||||
if cid := strings.TrimSpace(skill.CatalogID); cid != "" {
|
||||
if archive, err := s.loadCatalogArchive(ctx, tenantID, cid); err == nil && len(archive) > 0 {
|
||||
return archive, nil
|
||||
}
|
||||
}
|
||||
if archive, err := s.loadCatalogArchive(ctx, tenantID, skill.ID); err == nil && len(archive) > 0 {
|
||||
return archive, nil
|
||||
}
|
||||
return nil, apperrors.NewNotFoundError("skill files are not available")
|
||||
return s.loadCatalogArchive(ctx, tenantID, skill.ID)
|
||||
}
|
||||
|
||||
func (s *TenantSkillService) sameDigestCatalogArchive(
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@ func TestListSkillFilesUsesCatalogWhenInstallHasNoRef(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
skill.CatalogID = "cat-1"
|
||||
skill.BundleRef = ""
|
||||
skill.BundleSHA256 = skillArchiveSHA256(archive)
|
||||
skill.Status = types.SkillStatusReady
|
||||
require.NoError(t, fx.skillRepo.UpdateSkill(ctx, skill))
|
||||
|
||||
|
|
@ -71,6 +72,73 @@ func TestListSkillFilesUsesCatalogWhenInstallHasNoRef(t *testing.T) {
|
|||
require.NotEmpty(t, files)
|
||||
}
|
||||
|
||||
// A definition is mutable and an installation is not: registering the skill
|
||||
// again replaces the catalog object while this sandbox keeps running the image
|
||||
// built from the previous one. Answering with the newer tree would describe
|
||||
// files the image does not have.
|
||||
func TestListSkillFilesRefusesACatalogArchiveTheInstallWasNotBuiltFrom(t *testing.T) {
|
||||
fx := newInstallFixture(t)
|
||||
ctx := context.Background()
|
||||
updated, err := zipSkillFiles(map[string][]byte{
|
||||
"SKILL.md": []byte(validSkillMD),
|
||||
"scripts/extract.py": []byte("print('v2')\n"),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
fx.storedBundles = map[string][]byte{"file://catalog.zip": updated}
|
||||
require.NoError(t, fx.skillRepo.CreateCatalog(ctx, &types.TenantSkillCatalogEntity{
|
||||
ID: "cat-1", TenantID: 7, Name: "pdf-tools",
|
||||
BundleRef: "file://catalog.zip", BundleSHA256: skillArchiveSHA256(updated),
|
||||
}))
|
||||
skill, err := fx.skillRepo.GetSkill(ctx, 7, "cfg-1", "sk-1")
|
||||
require.NoError(t, err)
|
||||
skill.CatalogID = "cat-1"
|
||||
skill.BundleRef = ""
|
||||
skill.BundleSHA256 = strings.Repeat("d", 64)
|
||||
skill.Status = types.SkillStatusReady
|
||||
require.NoError(t, fx.skillRepo.UpdateSkill(ctx, skill))
|
||||
|
||||
_, err = fx.svc.ListSkillFiles(ctx, 7, "cfg-1", "sk-1")
|
||||
require.Error(t, err)
|
||||
appErr, ok := apperrors.IsAppError(err)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, 404, appErr.HTTPCode)
|
||||
}
|
||||
|
||||
// The definition's copy is downloaded once per browse the same way a row's own
|
||||
// object is: the settings drawer lists the tree and then opens files out of it,
|
||||
// and ppt-master-sized archives cannot be pulled again on every click.
|
||||
func TestListAndReadSkillFilesDownloadTheCatalogArchiveOnce(t *testing.T) {
|
||||
fx := newInstallFixture(t)
|
||||
ctx := context.Background()
|
||||
archive, err := zipSkillFiles(map[string][]byte{
|
||||
"SKILL.md": []byte(validSkillMD),
|
||||
"scripts/extract.py": []byte("print('hi')\n"),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
fx.storedBundles = map[string][]byte{"file://catalog.zip": archive}
|
||||
require.NoError(t, fx.skillRepo.CreateCatalog(ctx, &types.TenantSkillCatalogEntity{
|
||||
ID: "cat-1", TenantID: 7, Name: "pdf-tools",
|
||||
BundleRef: "file://catalog.zip", BundleSHA256: skillArchiveSHA256(archive),
|
||||
}))
|
||||
skill, err := fx.skillRepo.GetSkill(ctx, 7, "cfg-1", "sk-1")
|
||||
require.NoError(t, err)
|
||||
skill.CatalogID = "cat-1"
|
||||
skill.BundleRef = ""
|
||||
skill.BundleSHA256 = skillArchiveSHA256(archive)
|
||||
skill.Status = types.SkillStatusReady
|
||||
require.NoError(t, fx.skillRepo.UpdateSkill(ctx, skill))
|
||||
|
||||
files, err := fx.svc.ListSkillFiles(ctx, 7, "cfg-1", "sk-1")
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, files)
|
||||
_, err = fx.svc.ReadSkillFile(ctx, 7, "cfg-1", "sk-1", "SKILL.md")
|
||||
require.NoError(t, err)
|
||||
_, err = fx.svc.ReadSkillFile(ctx, 7, "cfg-1", "sk-1", "scripts/extract.py")
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, int32(1), fx.getFileCalls.Load())
|
||||
}
|
||||
|
||||
func TestReadSkillFileReturnsTextContent(t *testing.T) {
|
||||
fx := newInstallFixture(t)
|
||||
fx.seedStoredSkillBundle(t)
|
||||
|
|
|
|||
|
|
@ -1162,11 +1162,22 @@ func (s *TenantSkillService) pointInstallAtCatalog(
|
|||
if catalog == nil || strings.TrimSpace(catalog.BundleRef) == "" {
|
||||
return fmt.Errorf("catalog archive is missing")
|
||||
}
|
||||
return s.updateSkillFields(ctx, skill.TenantID, skill.SandboxConfigID, skill.ID,
|
||||
superseded := ""
|
||||
if err := s.updateSkillFields(ctx, skill.TenantID, skill.SandboxConfigID, skill.ID,
|
||||
func(e *types.TenantSkillEntity) {
|
||||
superseded = strings.TrimSpace(e.BundleRef)
|
||||
e.CatalogID = catalog.ID
|
||||
e.BundleRef = ""
|
||||
})
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
// This install now reads the definition's copy, so whatever it named before
|
||||
// — a pre-catalog object of its own, or an archive pinned by an earlier
|
||||
// replacement — has one reader fewer.
|
||||
if superseded != "" && superseded != strings.TrimSpace(catalog.BundleRef) {
|
||||
s.releaseInstallBundle(ctx, skill.TenantID, superseded)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateSkillFields loads, mutates and writes back one skill row. It logs on
|
||||
|
|
|
|||
|
|
@ -157,10 +157,15 @@ func (s *TenantSkillService) ReapStuckRuns(ctx context.Context) (int, error) {
|
|||
reaped++
|
||||
continue
|
||||
}
|
||||
pinned := strings.TrimSpace(row.BundleRef)
|
||||
if err := s.skills.DeleteSkill(ctx, row.TenantID, row.SandboxConfigID, row.ID); err != nil {
|
||||
logger.Warnf(ctx, "[skill] drop abandoned removal %s failed: %v", row.ID, err)
|
||||
continue
|
||||
}
|
||||
// The row was the last thing naming an archive it owned outright,
|
||||
// so the sweep that drops it is what makes those bytes reachable
|
||||
// by nothing. A definition's own object has other names and stays.
|
||||
s.releaseInstallBundle(ctx, row.TenantID, pinned)
|
||||
reaped++
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -762,6 +762,7 @@ var (
|
|||
type reaperSkillStore struct {
|
||||
rows map[string]*types.TenantSkillEntity
|
||||
snapshots []*types.TenantSkillSnapshotEntity
|
||||
catalogs []*types.TenantSkillCatalogEntity
|
||||
}
|
||||
|
||||
func (r *reaperSkillStore) put(e *types.TenantSkillEntity) {
|
||||
|
|
@ -903,8 +904,21 @@ func (r *reaperSkillStore) MarkSnapshotState(
|
|||
func (r *reaperSkillStore) DeleteSnapshotRowsByConfig(context.Context, uint64, string) error {
|
||||
panic("DeleteSnapshotRowsByConfig is outside the reaper surface")
|
||||
}
|
||||
func (r *reaperSkillStore) ListSkillsByTenant(context.Context, uint64) ([]*types.TenantSkillEntity, error) {
|
||||
panic("ListSkillsByTenant is outside the reaper surface")
|
||||
|
||||
// ListSkillsByTenant and ListCatalogsByTenant are on the surface because
|
||||
// dropping an abandoned removal is what makes an archive the row owned outright
|
||||
// unreachable, and reclaiming it means asking whether anything else names it.
|
||||
func (r *reaperSkillStore) ListSkillsByTenant(
|
||||
_ context.Context, tenantID uint64,
|
||||
) ([]*types.TenantSkillEntity, error) {
|
||||
var out []*types.TenantSkillEntity
|
||||
for _, e := range r.rows {
|
||||
if e != nil && e.TenantID == tenantID {
|
||||
cp := *e
|
||||
out = append(out, &cp)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
func (r *reaperSkillStore) ListUserEnvVars(
|
||||
context.Context, uint64, types.Principal, string, string,
|
||||
|
|
@ -937,8 +951,17 @@ func (r *reaperSkillStore) GetCatalog(context.Context, uint64, string) (*types.T
|
|||
func (r *reaperSkillStore) GetCatalogByName(context.Context, uint64, string) (*types.TenantSkillCatalogEntity, error) {
|
||||
panic("GetCatalogByName is outside the reaper surface")
|
||||
}
|
||||
func (r *reaperSkillStore) ListCatalogsByTenant(context.Context, uint64) ([]*types.TenantSkillCatalogEntity, error) {
|
||||
panic("ListCatalogsByTenant is outside the reaper surface")
|
||||
func (r *reaperSkillStore) ListCatalogsByTenant(
|
||||
_ context.Context, tenantID uint64,
|
||||
) ([]*types.TenantSkillCatalogEntity, error) {
|
||||
var out []*types.TenantSkillCatalogEntity
|
||||
for _, e := range r.catalogs {
|
||||
if e != nil && e.TenantID == tenantID {
|
||||
cp := *e
|
||||
out = append(out, &cp)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
func (r *reaperSkillStore) UpdateCatalog(context.Context, *types.TenantSkillCatalogEntity) error {
|
||||
panic("UpdateCatalog is outside the reaper surface")
|
||||
|
|
|
|||
|
|
@ -397,12 +397,21 @@ func (s *TenantSkillService) finishRemoval(
|
|||
return nil
|
||||
}
|
||||
|
||||
// Read before the delete: past it the only record of what this row named is
|
||||
// gone, and a pinned archive would be left behind with nothing to reclaim it.
|
||||
pinned := ""
|
||||
if row, err := s.skills.GetSkill(ctx, tenantID, configID, skillID); err == nil && row != nil {
|
||||
pinned = strings.TrimSpace(row.BundleRef)
|
||||
}
|
||||
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)
|
||||
}
|
||||
// The definition's archive survives this; only an object no catalog and no
|
||||
// other install names is reclaimed.
|
||||
s.releaseInstallBundle(ctx, tenantID, pinned)
|
||||
// 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's
|
||||
|
|
@ -460,6 +469,45 @@ func (s *TenantSkillService) removeStillOwnsTheRow(
|
|||
return current.Status == types.SkillStatusRemoving, nil
|
||||
}
|
||||
|
||||
// releaseInstallBundle drops an archive an install row has stopped naming,
|
||||
// unless something else still names it.
|
||||
//
|
||||
// The reference is not owned by one row. A definition's own object is shared by
|
||||
// every install of it, and one replaced archive is pinned by each install that
|
||||
// was built from it, so "this row let go" is not the same question as "nobody
|
||||
// needs these bytes". Both tables are checked before the object is deleted, and
|
||||
// an unreadable table means it is kept: a leaked archive costs storage, a
|
||||
// deleted one costs an install its files.
|
||||
func (s *TenantSkillService) releaseInstallBundle(
|
||||
ctx context.Context, tenantID uint64, bundleRef string,
|
||||
) {
|
||||
ref := strings.TrimSpace(bundleRef)
|
||||
if ref == "" {
|
||||
return
|
||||
}
|
||||
catalogs, err := s.skills.ListCatalogsByTenant(ctx, tenantID)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "[skill] list catalogs before releasing bundle %s failed: %v", ref, err)
|
||||
return
|
||||
}
|
||||
for _, cat := range catalogs {
|
||||
if cat != nil && strings.TrimSpace(cat.BundleRef) == ref {
|
||||
return
|
||||
}
|
||||
}
|
||||
installs, err := s.skills.ListSkillsByTenant(ctx, tenantID)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "[skill] list installs before releasing bundle %s failed: %v", ref, err)
|
||||
return
|
||||
}
|
||||
for _, row := range installs {
|
||||
if row != nil && strings.TrimSpace(row.BundleRef) == ref {
|
||||
return
|
||||
}
|
||||
}
|
||||
s.deleteBundleBestEffort(ctx, tenantID, ref)
|
||||
}
|
||||
|
||||
func (s *TenantSkillService) deleteBundleBestEffort(
|
||||
ctx context.Context, tenantID uint64, bundleRef string,
|
||||
) {
|
||||
|
|
|
|||
|
|
@ -38,8 +38,12 @@ 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.Empty(t, fx.deletedBundles,
|
||||
"the zip belongs to the catalog, not this sandbox install")
|
||||
// The seeded row owns a pre-catalog object: no definition and no sibling
|
||||
// install names it, so letting the row go is what makes it unreachable.
|
||||
// A row that reads the definition's copy has nothing of its own to reclaim,
|
||||
// which is the case the catalog test below covers.
|
||||
require.Equal(t, []string{"file://sk-1.zip"}, fx.deletedBundles,
|
||||
"an archive this row alone named is reclaimed with it")
|
||||
|
||||
skill, err := fx.skillRepo.GetSkill(context.Background(), 7, "cfg-1", "sk-1")
|
||||
require.NoError(t, err)
|
||||
|
|
|
|||
Loading…
Reference in New Issue