Skip to content

Add a disk cache for skills releases - #8076

Open
simonfaltum wants to merge 18 commits into
mainfrom
skills/s3-disk-cache-and-fallback-chain
Open

simonfaltum wants to merge 18 commits into
mainfrom
skills/s3-disk-cache-and-fallback-chain

Conversation

@simonfaltum

@simonfaltum simonfaltum commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Changes

Adds Cache to libs/skills, an on-disk cache of skills releases under the CLI cache folder. Nothing uses it yet, so behavior is unchanged. The fallback chain, which reads from the live source, then the cache, then the bundled copy, follows in a separate pull request stacked on this one.

  • Cache keeps one folder per release version, keyed by Catalog.Version, with the catalog and the files read from that release. Writes are atomic, so an interrupted write never leaves a corrupt entry.
  • Latest returns the newest cached catalog with no network call. Entries that do not parse, or whose folder name differs from their version, are skipped.
  • Only the release just stored and the newest other release are kept. Other folders are removed, including folders with no readable catalog.
  • File names must stay inside their release folder and be in clean, slash-separated form, and not .. So ./SKILL.md, references/../SKILL.md, . and references\..\SKILL.md are never cached.
  • Catalogs are ordered by FetchedAt, newest first. Only a tie is broken by version, with semver, so 10.0.0 is newer than 9.0.0. Two versions that are not semver fall to text order.
  • The cache is off when DATABRICKS_CACHE_ENABLED is false, when the cache folder cannot be found, or when DATABRICKS_SKILLS_REF, DATABRICKS_SKILLS_BASE_URL or DATABRICKS_SKILLS_DIR is set.
  • isName moves next to escapesDir in manifest.go, and CheckManifestPaths uses it.

Why

Agents call the skills commands several times per session. Without a cache, each call refetches the same release. A file of a release version never changes, so the cache stores it without an expiry.

Decisions to review

  • The cache is keyed by Catalog.Version, and a source that caches must set it to the release tag. The manifest's version is a schema version ("2" for every release), so it cannot be the key. Nothing sets the tag yet; the release source that the chain uses has to set it. fs.go still reports the manifest version, so it must not be cached as is.
  • A pinned source or a local folder (DATABRICKS_SKILLS_REF, DATABRICKS_SKILLS_BASE_URL, DATABRICKS_SKILLS_DIR) stores nothing instead of getting its own folder. A source-keyed folder is left for the change that wires the cache in.
  • Stored file names must be in clean form, so two spellings of one path never make two cache entries. The cost is that a non-clean name is read live every time.
  • The prune is best effort when two CLI processes run at once. A folder removed under a concurrent write costs that write its entry, and the next read misses, which means a live fetch.

Tests

  • TestCacheLatestIsNewestAndSkipsCorruptEntries covers newest-first ordering, and one case each for an entry that does not unmarshal, a truncated entry, a version that is not a name, and a version that differs from its folder. Each corrupt entry is newer than the valid one, so it is served unless its own skip condition holds.
  • TestCacheLatestBreaksTiesByVersion covers two catalogs fetched at the same time, including 9.0.0 against 10.0.0.
  • TestCacheKeepsStoredReleaseAndNewestOther covers a stored release older than the others: two folders are kept, not three.
  • TestCacheSameSchemaVersionKeyedByRelease covers two releases whose manifests share a schema version: each reads its own files.
  • TestCacheInterruptedWriteLeavesNoEntry covers a failed write leaving no entry and no temp file.
  • TestCacheRemovesOlderVersions covers removal of older releases and of a folder with no catalog.
  • TestCacheFilesAreKeyedByVersionAndStayInside covers version keying and rejected paths, including ./SKILL.md, . and backslash paths.
  • TestNewCacheHonoursCacheEnabled, TestNewCacheOffForPinnedSource and TestNewCacheWithoutCacheFolderStoresNothing cover the off switches. The test helper clears the pinned-source and cache-enabled variables, so they pass with them exported.
  • go test ./libs/skills/... and go test ./acceptance -run 'TestAccept/cmd/skills' pass. ./task fmt-q lint-q and tools/check_deadcode.py are clean.

This pull request and its description were written by Isaac.

simonfaltum and others added 7 commits November 26, 2026 11:18
Skill sources need the manifest type and its path checks, and the
libs-no-aitools lint rule keeps shared libs from importing the installer.
Move Manifest, SkillMeta, NormalizeManifest, CheckManifestPaths, EscapesDir
and ErrUnsafeManifestPath down into libs/skills; the installer uses them
from there through aliases. Add the Skill and Catalog types, the Source
interface, sentinel errors, and NewFSSource over any fs.FS laid out like a
skills release. The manifest accepts optional per-skill commands and a
top-level catalog_rules file.

Co-authored-by: Isaac <no-reply@databricks.com>
The installer and libs/skills both reserve .state.json. Define the name once in
libs/skills and have the installer use it.

Co-authored-by: Isaac <no-reply@databricks.com>
The Windows CI checkout turns the LF fixtures into CRLF, so the exact
content assertions failed. Normalize line endings before comparing.

Co-authored-by: Isaac <no-reply@databricks.com>
Clean catalog_rules before reading it, expose min_cli_version on Skill,
document the Catalog fields and the release-relative paths the manifest
may name.

Co-authored-by: Isaac <no-reply@databricks.com>
Keep the path helper unexported since only libs/skills calls it, reject a
catalog_rules path that cleans to a directory, and test a missing rules
file. Drop the min_cli_version field, which is not in the skill interface
the package needs yet.

Co-authored-by: Isaac <no-reply@databricks.com>
Skill now has MinCLIVer, read from the manifest's min_cli_version, so a
caller can apply the same rule install uses. The fixture covers it.

Co-authored-by: Isaac <no-reply@databricks.com>
NewChain serves a skills catalog from a live source, then from an
on-disk cache, then from a bundled copy. A cached catalog younger than
ten minutes is served without a live call. A stale catalog is marked as
such, so callers can tell the user its age.

ReadFile serves files of a release version from the cache. A file is
written once per release and never replaced. The cache keeps the current
and the previous release only. Writes are atomic, and an entry that does
not parse is skipped.

Nothing calls the chain yet, so behaviour is unchanged.

Co-authored-by: Isaac <no-reply@databricks.com>
@eng-dev-ecosystem-bot

eng-dev-ecosystem-bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Integration test report

Commit: 8415a91

Run: 46454410598

Env 💚​RECOVERED ✅​pass 🙈​skip Time
💚​ aws linux-2core-8gb 1 88 6 1:47
💚​ aws-windows-latest-4core-16gb 1 88 6 3:20
💚​ azure linux-2core-8gb 1 88 6 1:50
💚​ azure-windows-latest-4core-16gb 1 88 6 2:49
💚​ gcp linux-2core-8gb 1 88 6 2:07
💚​ gcp-windows-latest-4core-16gb 1 88 6 2:45
Test Name aws linux-2core-8gb aws-windows-latest-4core-16gb azure linux-2core-8gb azure-windows-latest-4core-16gb gcp linux-2core-8gb gcp-windows-latest-4core-16gb
💚​ TestAccept 💚​R 💚​R 💚​R 💚​R 💚​R 💚​R
Top 4 slowest tests (at least 2 minutes):
duration env testname
3:18 aws-windows-latest-4core-16gb TestAccept
2:47 azure-windows-latest-4core-16gb TestAccept
2:43 gcp-windows-latest-4core-16gb TestAccept
2:06 gcp linux-2core-8gb TestAccept

ReadFile asked the live source for the catalog once the freshness window
had passed, even when the file was cached. A file of the newest cached
release is now served before any live call, whatever the catalog's age.
NewCache also honours DATABRICKS_CACHE_ENABLED, as libs/cache does.

Co-authored-by: Isaac <no-reply@databricks.com>
@simonfaltum
simonfaltum marked this pull request as ready for review November 26, 2026 20:54
@simonfaltum
simonfaltum requested a review from a team as a code owner November 26, 2026 20:55

@review-oss-bot review-oss-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review at 54ca8f6. Each finding was verified against the code.

Not ready yet: 2 blocking issue(s), tagged [blocking].

This PR adds NewChain and a per-release disk Cache in libs/skills. The chain tries the live source first, then the cache, then the bundled snapshot. Catalog freshness and fallback work as described. ReadFile has three ways to return bytes from a release other than the one in the catalog. Two of them should be fixed before this gets a caller: live bytes get cached under an older version for good, and every live read error falls back to the bundled copy. I also left two smaller notes, on the duplicated cache lookup and on how NewCache fails.

Comment thread libs/skills/chain.go Outdated
}
data, liveErr := c.live.ReadFile(ctx, skill, file)
if liveErr == nil {
if err := c.cache.writeFile(cat.Version, skill, file, data); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] Inside the 10-minute window, cat is the cached catalog. live.ReadFile takes no version, though, and reads whatever release the live source serves now. Say 1.0.0 was cached 5 minutes ago and live has since moved to 2.0.0: ReadFile("alpha", "SKILL.md") stores 2.0.0's bytes at 1.0.0/files/alpha/SKILL.md. Release files never expire, so 1.0.0 serves that content until it is pruned. Can the write be skipped when cat came from the cache, or the live read be tied to cat.Version?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 867cfc9. ReadFile now caches a live file only when the catalog was read live in the same call, so 2.0.0 bytes are never stored under a cached 1.0.0 catalog. A file read under a cached catalog is returned but not cached, so it is read live until the catalog is refreshed. The Source interface has no version argument, so a release published between the two live calls can still mismatch; that window is one call pair long.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delivered in ce9200d, on the stacked pull request #8115. Live reads are pinned to the cached catalog's release: a file read is cached only when a live catalog read after it still reports that release, and a cached file of the newest cached release is served offline. Test: TestChainCatalogThenReadFileIsCachedForOfflineReads.

Comment thread libs/skills/chain.go Outdated
}
return data, nil
}
data, err = c.bundled.ReadFile(ctx, skill, file)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] Every live error lands here, including ErrUnknownSkill and ErrUnknownFile from a live source that was reached fine (fsSource.ReadFile returns these from its own manifest). Say live 1.0.0 drops alpha/old.md and the bundled 0.9.0 still has it: ReadFile returns the 0.9.0 bytes with a nil error, for a file the catalog just served doesn't list. TestChainReadFileFallsBackToBundledUncached asserts this. I expected the fallback only when errors.Is(liveErr, ErrSourceUnreachable), with any other error returned as it is.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 867cfc9. ReadFile now falls back to the bundled copy only when the live error is ErrSourceUnreachable. Any other live error, such as ErrUnknownFile, is returned as it is. The new test TestChainReadFileUnknownFileIsNotBundled covers the unknown-file case. Catalog keeps its broader fallback to the cache, because a cached catalog is still the right answer for any live catalog failure.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delivered in ce9200d, on the stacked pull request #8115. Catalog and ReadFile both fall back only for an error that wraps ErrSourceUnreachable; other live errors are returned, and the contract is documented on Source. Tests: TestChainFreshCacheUnreachableFileReadFallsBackToBundled, TestChainNonUnreachableLiveErrorIsNotFallenBack, TestChainReadFileUnknownFileIsNotBundled.

Comment thread libs/skills/chain.go Outdated
}
// A stale catalog means the live source already failed, so it is not asked again.
if cat.Stale {
return c.bundled.ReadFile(ctx, skill, file)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nice to have] The description's reasoning holds for a stale bundled catalog. A stale catalog can also come from the cache, though: live is down and the cache holds 1.0.0. Here a file that isn't cached is read from the bundled 0.9.0. For a skill that both releases have, ReadFile returns 0.9.0 content under a 1.0.0 catalog. For a skill added in 1.0.0, it returns a bare unknown skill error that never mentions the live failure, unlike the combined error at line 96. Should this check cat.Version against the bundled release, or at least wrap the error with the live failure?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 867cfc9. The bundled copy now stands in only when its release equals the catalog's release. A file under a stale cached 1.0.0 catalog returns an error when the bundled release is 0.9.0, so 0.9.0 bytes are never served under 1.0.0. Limit: on that stale path the error does not repeat the live failure, because the catalog step does not return it. Test: TestChainReadFileStaleCacheOtherBundledReleaseIsNotServed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delivered in ce9200d, on the stacked pull request #8115. The bundled copy is served for a file only when its release equals the catalog's release, so 0.9.0 bytes are not served under a 1.0.0 catalog. Test: TestChainReadFileStaleCacheOtherBundledReleaseIsNotServed.

Comment thread libs/skills/chain.go Outdated
if err != nil {
return nil, err
}
if data, ok := c.cache.readFile(cat.Version, skill, file); ok {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] This lookup almost always repeats the one at lines 71-75. A fresh or stale cached catalog is Latest() again. Bundled is only returned when the cache is empty. A new live release has no files/ folder yet. It only hits when live serves a version that is already cached but isn't the newest. AGENTS.md says to avoid just-in-case fallback branches, so can we drop it and keep a single cache read path?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 867cfc9. The second cache lookup is removed. ReadFile reads the cache once, before the catalog, and then reads the live file as before.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delivered in ce9200d, on the stacked pull request #8115. ReadFile reads the cache once, before the catalog, and passes that catalog to catalog(), so there is no second Latest read.

Comment thread libs/skills/cache.go
}

// Latest returns the cached catalog fetched most recently. It reads the disk only.
func (c *Cache) Latest() (*Catalog, bool) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nice to have] When os.UserCacheDir fails (for example, HOME and XDG_CACHE_HOME are both unset), NewCache returns an error. For the same failure, libs/cache.NewCache returns a no-op cache. A skills command that calls this would probably fail outright, even though a nil Cache already means "store nothing". Should this log at debug level and return nil, nil?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 867cfc9. NewCache now returns nil and logs at debug when the cache folder cannot be found, as libs/cache does, so a skills command gets a working no-op cache instead of an error. The signature is now NewCache(ctx) *Cache. TestNewCacheWithoutCacheFolderStoresNothing covers it.

- ReadFile caches a live file only under a catalog read live in the same
  call, so a file from a newer live release is never stored under an older
  cached release.
- ReadFile falls back to the bundled copy only when the live source is
  unreachable, and only when the bundled release is the catalog's release.
  Other live errors, such as an unknown file, are returned as they are.
- ReadFile reads the cache once, before the catalog, instead of again after it.
- NewCache returns nil and logs at debug when no cache folder can be found,
  as libs/cache does, instead of returning an error.

Co-authored-by: Isaac <no-reply@databricks.com>

@rugpanov rugpanov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fixes in 867cfc9. The fixes for the review-oss-bot items look correct: version-safe writes, bundled fallback only on ErrSourceUnreachable with a release check, one cache read, and a no-op NewCache.

One new blocking issue: the fromLive fix stops the file cache from filling in the normal flow (see chain.go:96). The other comments are smaller.

Risk: LOW. The PR adds only new files in libs/skills. NewCache and NewChain are //deadcode:allow and have no caller, so no command runs this code yet. The defects matter when a later PR connects the chain to the skills commands.

Comment thread libs/skills/chain.go Outdated
data, liveErr := c.live.ReadFile(ctx, skill, file)
if liveErr == nil {
// A cached catalog may be older than the live release, so its files are not cached.
if fromLive {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] fromLive is true only when the catalog() call inside this ReadFile asked the live source. After Catalog() runs, each ReadFile in the next 10 minutes gets the fresh cached catalog with fromLive == false, so no file is stored. At most one file per refresh goes into the cache.

Result: a caller that runs Catalog() and then reads each file caches no file. Later, with live down, catalog() returns the cached catalog as Stale, and readBundled rejects the bundled copy when its release differs. So one online Catalog() call turns offline reads that work with an empty cache into errors. TestChainReadFileLiveReleaseChangeIsNotCachedUnderCachedCatalog covers only the case where live moved to a new release. No test covers Catalog(), then ReadFile, then an offline ReadFile.

Pin the live read to cat.Version (a version argument, or a check of the live release before the write). Alternatively, keep the version of the last live catalog in the chain and also cache when cat.Version matches it. Add a test for the Catalog-then-ReadFile-then-offline flow.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this pull request. The chain moved out of it. The fix is written and tested, and it goes in a follow-up pull request stacked on this one: the cache file is written only when a live catalog read after the file read reports the same release.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delivered in ce9200d, on the stacked pull request #8115. A file is cached only when a live catalog read after the file read still reports the catalog's release. Tests: TestChainCatalogThenReadFileIsCachedForOfflineReads and TestChainReadFileLiveReleaseChangeIsNotCachedUnderCachedCatalog.

Comment thread libs/skills/chain.go Outdated
}
return data, nil
}
if !errors.Is(liveErr, ErrSourceUnreachable) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nice to have] ReadFile falls back to the bundled copy only for an error that wraps ErrSourceUnreachable. catalog() falls back for any live error. The Source doc and the NewChain doc do not say that a live source must wrap transport errors in ErrSourceUnreachable.

Result: with a fresh cached catalog, a plain "connection refused" from the live ReadFile is returned to the caller, also when the bundled release matches. Ten minutes later the same call serves the bundled copy, because catalog() marks the catalog Stale.

Document the wrapping contract on Source, or use the same rule in both paths. Add a test for a fresh cached catalog with an unreachable live ReadFile.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this pull request. The chain moved out of it. The fix is written and tested, and it goes in a follow-up pull request stacked on this one: fall back only on an error that wraps ErrSourceUnreachable, in both Catalog and ReadFile, with the contract documented on Source.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delivered in ce9200d, on the stacked pull request #8115. Both Catalog and ReadFile fall back only on ErrSourceUnreachable, and the contract is documented on Source. Test: TestChainFreshCacheUnreachableFileReadFallsBackToBundled.

Comment thread libs/skills/chain.go Outdated
// live source in this call.
func (c *chain) catalog(ctx context.Context) (*Catalog, bool, error) {
cached, hasCached := c.cache.Latest()
if hasCached && c.now().Sub(cached.FetchedAt) < catalogFreshFor {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nice to have] A FetchedAt in the future gives a negative age, and a negative age is less than catalogFreshFor. This occurs after the clock moves back, or with a cache folder shared by hosts with clock skew.

Result: the cached catalog is served with no live refresh until the clock passes FetchedAt + 10m. For a skew of one day, the catalog does not refresh for one day. catalogs() also sorts that entry first, so ReadFile keeps reading its files.

Treat a negative age as not fresh, for example age := c.now().Sub(cached.FetchedAt); if hasCached && age >= 0 && age < catalogFreshFor {.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this pull request. The chain moved out of it. The fix is written and tested, and it goes in a follow-up pull request stacked on this one: a negative age is treated as not fresh.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delivered in ce9200d, on the stacked pull request #8115. A negative age is not fresh. Test: TestChainFutureFetchedAtIsNotFresh.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up on the stacked pull request #8115: a clock that moved back no longer makes a stored catalog tie with the newest cached one. In ea4a9bf a live catalog that would tie with, or sort behind, the newest cached one is stamped one nanosecond after it, so the newest write stays the Latest entry.

Comment thread libs/skills/cache.go Outdated
}
cats := c.catalogs()
for _, old := range cats[min(keptVersions, len(cats)):] {
_ = os.RemoveAll(filepath.Join(c.dir, old.Version))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Pruning removes only the folders whose catalog.json parses, and it uses the parsed old.Version, not the folder name. A folder with a missing or corrupt catalog.json stays for good. For example, putCatalog fails in catalog() (only logged), then writeFile creates <version>/files/... with no catalog.json.

List the folders by e.Name(), and remove each folder that is not one of the kept versions.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 33a6e1c. putCatalog now removes every folder that is not kept, by folder name, so a folder with no readable catalog.json is removed too. TestCacheRemovesOlderVersions covers it.

Comment thread libs/skills/chain.go Outdated
// A cached file of the newest cached release is served before any live call,
// whatever the catalog's age, so a cached read makes no live call.
if cached, ok := c.cache.Latest(); ok {
if data, ok := c.cache.readFile(cached.Version, skill, file); ok {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] The cache-first read does not check that the cached catalog lists file. ./SKILL.md and references/../SKILL.md pass cacheable, and filepath.Join resolves them to SKILL.md.

Result: with a warm cache, ReadFile("alpha", "./SKILL.md") returns the cached bytes. With a cold cache, the live fsSource returns ErrUnknownFile, because it uses an exact match. The answer depends on the cache state.

Serve a cached file only when cached.Skills[skill].Files contains file exactly, or reject a path that is not clean.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 33a6e1c. cacheable now requires a clean file path, so ./SKILL.md and references/../SKILL.md are never cached and a read does not depend on cache state. The cases are in TestCacheFilesAreKeyedByVersionAndStayInside.

Comment thread libs/skills/chain_test.go Outdated
*now = now.Add(30 * time.Minute)

_, err = c.ReadFile(t.Context(), "alpha", "SKILL.md")
require.Error(t, err, "0.9.0 bytes under a 1.0.0 catalog are not served")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] require.Error accepts any error. The test also passes when the bundled read fails for another reason. Use require.ErrorContains(t, err, "is not release 1.0.0").

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this pull request. The chain moved out of it. The follow-up pull request uses require.ErrorContains for that check.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delivered in ce9200d, on the stacked pull request #8115. The check uses require.ErrorContains.

Comment thread libs/skills/chain_test.go Outdated

_, err := c.Catalog(t.Context())
require.NoError(t, err)
fetchedAt := *now

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] The local fetchedAt hides the package-level test helper fetchedAt in cache_test.go. Rename it, for example to firstFetch. Also, line 56 repeats the t0 date from cache_test.go; use t0.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this pull request. The chain moved out of it. The follow-up pull request renames the local variable and uses t0.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delivered in ce9200d, on the stacked pull request #8115. The local variable is renamed to firstFetch, and the tests use t0.

Comment thread libs/skills/cache.go
if err != nil {
return err
}
path := filepath.Join(c.dir, cat.Version, catalogFile)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Question] The cache key is only cat.Version, with no source identity. libs/aitools/installer/installer.go supports DATABRICKS_SKILLS_REF and DATABRICKS_SKILLS_BASE_URL. Will the live source put the ref or base URL into the cache folder? Without it, after a user switches the ref, the chain serves the other source's catalog for 10 minutes, and its files at any age when the manifest version is the same.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answered in 33a6e1c with the bypass. NewCache returns no cache when DATABRICKS_SKILLS_REF or DATABRICKS_SKILLS_BASE_URL is set, so a pinned source never shares the release-keyed folder. TestNewCacheOffForPinnedSource covers it.

Comment thread libs/skills/chain.go Outdated
if err != nil {
return nil, false, fmt.Errorf("no skills catalog available: live source: %w; bundled source: %w", liveErr, err)
}
bundled.Stale = true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Question] The bundled catalog is marked Stale, but fsSource.Catalog sets its FetchedAt to time.Now(). The Stale doc in skills.go says "the source served a cached copy it could not refresh". Is this the intended meaning? A caller that shows "stale, fetched N ago" will show about 0 s for the bundled copy.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this pull request. The chain moved out of it. The follow-up pull request documents that Stale covers the bundled fallback and that a bundled FetchedAt is the time it was read.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delivered in ce9200d, on the stacked pull request #8115. skills.go documents that Stale covers the bundled fallback, and that a bundled copy's FetchedAt is the time it was read. The bundled source no longer zeroes it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up on the stacked pull request #8115: Stale alone no longer changes how ReadFile reads. In 87fc602 ReadFile falls back to the cache or the bundled copy only when the live catalog read failed in that call, so a live source that reports Stale with no error has its files read live. The answer above on what Stale means stands.

Comment thread libs/skills/chain.go Outdated
if hasCached && c.now().Sub(cached.FetchedAt) < catalogFreshFor {
return cached, false, nil
}
live, liveErr := c.live.Catalog(ctx)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Question] A live failure is not remembered. When the cache is past the window and live is down, each Catalog call and each uncached ReadFile waits for the live call again. Is a short negative cache planned for the live source? This matters because agents call the skills commands several times in one session.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this pull request. The chain moved out of it. A negative cache is not planned for that follow-up. Each live call is bounded by the live source budget, and a negative cache would be a separate decision.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answer, no negative cache in ce9200d on the stacked pull request #8115. A failed live call is retried on the next call. Each live call is bounded by the live source's budget, and a negative cache would be a separate decision.

Move the fallback chain out of this change; it builds on the cache in a
follow-up. Prune by folder name, so a folder with no readable catalog is
removed. Keep file names clean so cached and live reads agree. Move isName
next to escapesDir. Turn the cache off when a pinned skills source is set,
since the cache is keyed by release version only.

Co-authored-by: Isaac <no-reply@databricks.com>
@simonfaltum simonfaltum changed the title Add a disk cache and fallback chain for skills sources Add a disk cache for skills releases Nov 26, 2026
Base automatically changed from skills/c1-skill-source-interface-and-file-system-s to main November 27, 2026 09:23
@simonfaltum
simonfaltum requested review from a team and lennartkats-db as code owners November 27, 2026 09:23
The skills source interface (#8003) is squash-merged, so this branch
takes main's copy of those files. The disk cache change stays on top.

Co-authored-by: Isaac <no-reply@databricks.com>
Comment thread libs/skills/cache.go Outdated
if enabled, ok := env.GetBool(ctx, "DATABRICKS_CACHE_ENABLED"); ok && !enabled {
return nil
}
if env.Get(ctx, "DATABRICKS_SKILLS_REF") != "" || env.Get(ctx, "DATABRICKS_SKILLS_BASE_URL") != "" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[non-blocking] Turning the cache off whenever DATABRICKS_SKILLS_BASE_URL is set also turns it off in every skills acceptance test, since that variable is how the tests point the CLI at the fake server, and the harness already gives each test its own DATABRICKS_CACHE_DIR. Once the chain is wired in, no acceptance test will be able to cover the cache or the fallback, so consider keying the folder by source (or skipping the cache only for DATABRICKS_SKILLS_REF) in the follow-up.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept as it is, on purpose. The cache is bypassed for both DATABRICKS_SKILLS_REF and DATABRICKS_SKILLS_BASE_URL, so a test or mirror source never shares the default release-keyed folder. That is the decided behaviour, so nothing changes here. The cost you name is real: acceptance tests cannot cover the cache until a follow-up gives them a source-keyed folder or a test-only cache root. That follow-up goes with the wiring change, not this pull request.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in 148369b: DATABRICKS_SKILLS_DIR now also turns the cache off, with the other pinned sources, because a local folder reports the schema version as its catalog version. The acceptance tests still have no cache, as described above; the source-keyed folder or test cache root is the follow-up.

Comment thread libs/skills/cache_test.go
func TestNewCacheHonoursCacheEnabled(t *testing.T) {
ctx := env.Set(t.Context(), "DATABRICKS_CACHE_DIR", t.TempDir())

assert.Nil(t, NewCache(env.Set(ctx, "DATABRICKS_CACHE_ENABLED", "false")), "a disabled cache stores nothing")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[non-blocking] These tests check that NewCache returns nil when the cache is off, but nothing calls Latest, putCatalog, readFile or writeFile on a nil *Cache, so the documented 'a nil Cache stores nothing' contract the chain will rely on has no coverage. One short test on a nil receiver would pin it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 6652a09: TestNilCacheStoresNothing. A nil *Cache returns no catalog from Latest, putCatalog and writeFile return no error without storing anything, and readFile reports nothing cached.

simonfaltum and others added 3 commits November 27, 2026 10:22
The fallback chain relies on a nil Cache storing nothing and reading
nothing. Pin that with a test on a nil receiver.

Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Each corrupt cache entry in the test now has a newer FetchedAt than the valid
release, so it is served unless its own skip condition holds. The catalogs
also skip an entry whose version differs from its folder name, and catalogs
fetched at the same time are ordered by version. The NewCache tests clear the
pinned source variables, so an exported DATABRICKS_SKILLS_REF does not fail them.

Co-authored-by: Isaac <no-reply@databricks.com>
@simonfaltum

Copy link
Copy Markdown
Member Author

@rugpanov every point from your changes-requested review is addressed in later commits, and each thread has a reply with the commit or the reason. Could you take another look when you have a moment? Thanks.

@rugpanov rugpanov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates. The chain.go points of my earlier review are no longer in this PR, so I will check them in #8115. The cache.go fixes (prune by folder name, the shared isName) and the pinned-source bypass look good.

One blocking point: the cache key (inline at libs/skills/cache.go:84). Upstream, the manifest version is a schema version ("2"). So all releases share one cache folder, and cached files go stale.

Also libs/skills/skills.go:4 (not in the diff): the package doc says that only ref.go and fetch.go read DATABRICKS_SKILLS_REF and DATABRICKS_SKILLS_BASE_URL. cache.go:48 now reads them too. Update that sentence.

Risk: LOW. Nothing outside tests calls NewCache, Latest, putCatalog, readFile or writeFile, so no CLI command runs this code yet. The only production change, the isName extraction in CheckManifestPaths, keeps the same behavior, and a revert rolls it back.

Comment thread libs/skills/cache.go
if err != nil {
return err
}
path := filepath.Join(c.dir, cat.Version, catalogFile)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] The cache key is cat.Version. The only Catalog producer, fs.go:32, sets it from the manifest version field. In databricks/databricks-agent-skills, that field is "2" at main, v0.2.54 and v0.2.56. It is a schema version, not a release version.

So all releases share one folder, 2/. putCatalog replaces 2/catalog.json, but the files under 2/files/ have no expiry. After a new release, the new catalog points at the old file bytes. The chain in #8115 calls readFile(cached.Version, ...) before a live call, so it serves the old files with no end.

Key the cache by the release tag (the ref from ResolveSkillsRef), not by the manifest version. Or make the live source set Catalog.Version to the tag, and document that contract on Cache. Add a test with two releases that have the same manifest version.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the contract, not the key. Cache keys by Catalog.Version, and Catalog.Version is now documented as the release tag for a published release (skills.go, cache.go). The manifest version is a schema version ("2" in v0.2.56, as you say), so the release source that the chain reads must set Version to the tag. That source is not in this PR, and fs.go still reports the manifest version, so it must not be cached as is. Added TestCacheSameSchemaVersionKeyedByRelease: two releases with the same schema version keep their own files.

Comment thread libs/skills/cache.go
}
entries, _ := os.ReadDir(c.dir)
for _, e := range entries {
if !keep[e.Name()] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nice to have] The prune reads catalogs() first and os.ReadDir after it. Another CLI process can write a newer release between the two calls. Then this process removes that new folder, or the other process gets a rename error. Agents can run skills commands in parallel, so this can occur.

Skip a folder that is not in keep but has a recent modification time, or document the prune as best-effort. Also, a stored catalog with an older FetchedAt keeps three folders, not the two that the doc comment says.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the count and documented the race. putCatalog now keeps the release it just stored plus the newest other release, so a stored release older than the others leaves two folders, not three (TestCacheKeepsStoredReleaseAndNewestOther). The prune is documented as best effort: a folder removed under a concurrent write costs that write its entry, and the next read misses and fetches live. No data is served wrongly.

Comment thread libs/skills/cache.go Outdated
// the cache folder of their release. The file must be clean, so that "./SKILL.md"
// is not cached under the name "SKILL.md" and reads do not depend on the cache.
func cacheable(version, skill, file string) bool {
return isName(version) && isName(skill) && !escapesDir(file) && path.Clean(file) == file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nice to have] path.Clean(file) == file checks only /, but escapesDir also splits on \. So references\..\SKILL.md and .\SKILL.md pass cacheable. On Windows, filePath cleans them with filepath.Join to the same file as SKILL.md. Two spellings then share one cache entry, against the rule in the doc comment.

The file . also passes, and its path is the skill folder itself. A write of . puts a regular file there, and later writes of that skill fail.

Reject \ and . in file, as libs/aitools/installer/source.go already does. Add these cases to the rejected list in cache_test.go.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. cacheable now rejects "." and any backslash in the file name, so references..\SKILL.md and .\SKILL.md no longer share an entry with SKILL.md on Windows, and "." no longer writes a file over the skill folder. The cases are added to the rejected list in TestCacheFilesAreKeyedByVersionAndStayInside.

Comment thread libs/skills/cache.go Outdated
cats = append(cats, &cat)
}
slices.SortFunc(cats, func(a, b *Catalog) int {
return cmp.Or(b.FetchedAt.Compare(a.FetchedAt), strings.Compare(b.Version, a.Version))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] The tie-break uses strings.Compare on the version. With equal FetchedAt, 9.0.0 wins over 10.0.0, and v0.2.9 wins over v0.2.56. #8115 makes ties possible: storedCopy copies the newest FetchedAt when the clock moves back. Compare with golang.org/x/mod/semver, which ref.go already uses, and fall back to text order.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Versions are compared by semver with golang.org/x/mod/semver, as ref.go does, and as text only when neither is semver (compareVersions). 9.0.0 now sorts below 10.0.0 (TestCacheLatestBreaksTiesByVersion). The clock-tie case that made ties possible through the chain is handled on #8115 in ea4a9bf, which stamps the stored copy one nanosecond after the newest cached catalog.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the comment in 148369b to match the semver package: an invalid version ranks below a valid one, and only two invalid versions fall to text order. The code is unchanged.

Comment thread libs/skills/cache.go Outdated
keptVersions = 2
)

// Cache keeps skills releases on disk, one folder per release version, so a

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] The comment says that a file of a release is written once and never replaced. writeFile and putCatalog always replace the target, and #8115 rewrites catalog.json on each refresh. Change the comment to say what the code does, or enforce the rule in writeFile.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the comment to say what the code does: a read never asks the source again, and a write replaces its file atomically, so a reader sees the old bytes or the new ones. Write-once is not enforced, because putCatalog rewrites catalog.json on each refresh and the rule is not needed for correctness here.

Comment thread libs/skills/cache_test.go
for _, c := range []struct{ name, folder, body string }{
{"does not unmarshal", "3.0.0", `{"Version":"3.0.0",` + newer + `,"Skills":"not a list"}`},
{"truncated", "4.0.0", `{"Version":"4.0.0",` + newer},
{"version is not a name", "5.0.0", `{"Version":"../5.0.0",` + newer + `}`},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] The comment on line 33 says that each case has one condition only. This case also fails the folder-name check in catalogs(), because ../5.0.0 is not 5.0.0. If you remove !isName(cat.Version), the test still passes. Remove the "one condition only" claim, or note that this case does not isolate isName.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworded. The comment now says the version case also fails the folder check, so it does not isolate isName. The case stays, because it still tests a bad catalog version. isName on its own is covered by the skill-name cases ("..", "a/b") in TestCacheFilesAreKeyedByVersionAndStayInside.

Comment thread libs/skills/cache_test.go
assert.Nil(t, NewCache(env.Set(ctx, "DATABRICKS_SKILLS_BASE_URL", "http://localhost:8080")), "a custom base URL stores nothing")
}

func TestNewCacheWithoutCacheFolderStoresNothing(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] This test does not use newCacheTestCtx, so it does not clear DATABRICKS_SKILLS_REF, DATABRICKS_SKILLS_BASE_URL or DATABRICKS_CACHE_ENABLED. If a developer exports one of them, NewCache returns nil before cache.BaseDir. Then the test passes without a test of the missing folder. Start from newCacheTestCtx(t), then set DATABRICKS_CACHE_DIR to an empty value.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. TestNewCacheWithoutCacheFolderStoresNothing now starts from newCacheTestCtx and sets DATABRICKS_CACHE_DIR to empty. newCacheTestCtx also sets DATABRICKS_CACHE_ENABLED to true now, so no exported variable turns the cache off in these tests.

simonfaltum and others added 3 commits November 27, 2026 15:23
The cache key is Catalog.Version. The manifest's version is a schema
version shared by every release, so the source must set Version to the
release tag. Document that contract on Cache and Catalog.

Keep the release just stored and the newest other one. A stored release
older than the rest used to leave three folders.

Order versions by semver in the tie-break, so 10.0.0 sorts above 9.0.0.

Reject a file name with a backslash, or ".", in the cache, so two
spellings of one path never share an entry on Windows.

Document the prune as best effort under concurrent CLI runs, and fix
the package doc on which code reads the pinned-source variables.

Co-authored-by: Isaac <no-reply@databricks.com>
A local folder from DATABRICKS_SKILLS_DIR reports the schema version as its
catalog version, so it would be cached as a release. NewCache now returns
nil for it, as it does for a pinned ref and a custom base URL. Also fix the
comments on compareVersions and Latest.

Co-authored-by: Isaac <no-reply@databricks.com>
The cache test helper cleared the ref and base URL variables but not
DATABRICKS_SKILLS_DIR, so an exported folder turned the cache off and
two tests failed on a developer machine. The cache now reads the variable
through its dirEnv constant, and a stored catalog never reads back as
stale because Stale is not serialized.

Co-authored-by: Isaac <no-reply@databricks.com>
@simonfaltum
simonfaltum requested a review from denik November 29, 2026 09:28
stevenayers pushed a commit to stevenayers/cli that referenced this pull request Nov 30, 2026
…ricks#8118)

## Changes

A `skill/<name>` user agent pair is sent only when the name matches one
of two lists. Anything else is dropped, and the dropped name isn't
logged.

1. **Static list (`knownSkills`).** Every skill name in the history of
`databricks/databricks-agent-skills`, including renamed and retired
names (`databricks` → `databricks-core`, `databricks-bundles` →
`databricks-dabs`, ...), plus `databricks-quickstart`. Retired names
must stay here, because no current release lists them.
2. **Bundled skills release.** The skill names in the snapshot compiled
into the binary (`libs/skills` `NewBundledSource`). It's read once per
process with `sync.OnceValues`, and only when a name misses the static
list. It reads only the compiled-in snapshot: no network call, and not
`DATABRICKS_SKILLS_DIR`, which can point at a folder of the user's own
skills.

The bundled snapshot is empty until databricks#8075 fills it at release build
time. Until then this behaves like the static list alone, and new skills
must be added to `knownSkills` by hand.

Debug logs no longer include rejected names, including invalid entries
and names over the byte budget.

## Why

The experimental `skill/<name>` pairs (databricks#7730, databricks#7783) report whatever the
agent passes to `--skills-invoked` / `DATABRICKS_SKILLS_INVOKED`. Agents
pass every skill they loaded, not only Databricks ones, so the CLI can
end up sending the names of third-party or user-written skills. We only
want to report Databricks skills.

A `databricks-` prefix check isn't enough, because users name their own
skills that way too.

The bundled release is read synchronously, not under a deadline. It's
compiled in, so the read takes about 0.2ms and does no I/O, and a
deadline would only make the result vary on a loaded machine.

Possible follow-up: once the disk cache in databricks#8076 lands, its newest
catalog could be added as a third source, to cover skills released
between CLI releases. The cache is already off when
`DATABRICKS_SKILLS_REF` or `DATABRICKS_SKILLS_BASE_URL` is set, so it
only holds official releases.

## Tests

- Unit tests: unknown names are dropped, including ones with the
`databricks-` prefix. Every static name survives. A name only in the
bundled release is reported. An empty snapshot falls back to the static
list. Static matches never read the bundled release. The format and
length cases now fail if those checks are removed.
- Acceptance: added a case to `acceptance/cmd/skills-invoked`, and moved
the existing flag-vs-env case to real skill names.

_This pull request and its description were written by Isaac._

---------

Co-authored-by: Isaac <no-reply@databricks.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants