diff --git a/src/overlay-database-utils.test.ts b/src/overlay-database-utils.test.ts index d8a7c05bd..8a5ab96e8 100644 --- a/src/overlay-database-utils.test.ts +++ b/src/overlay-database-utils.test.ts @@ -13,6 +13,7 @@ import { downloadOverlayBaseDatabaseFromCache, getCacheRestoreKeyPrefix, getCacheSaveKey, + getCacheWorkflowKeyPrefix, OverlayDatabaseMode, writeBaseDatabaseOidsFile, writeOverlayChangesFile, @@ -295,8 +296,22 @@ test("overlay-base database cache keys remain stable", async (t) => { "This may indicate breaking changes in the cache key generation logic.", ); + const workflowKeyPrefix = await getCacheWorkflowKeyPrefix(); + const expectedWorkflowKeyPrefix = + "codeql-overlay-base-database-1-c5666c509a2d9895-"; + t.is( + workflowKeyPrefix, + expectedWorkflowKeyPrefix, + "Cache workflow key prefix changed unexpectedly. " + + "This may indicate breaking changes in the cache key generation logic.", + ); + t.true( saveKey.startsWith(restoreKeyPrefix), `Expected save key "${saveKey}" to start with restore key prefix "${restoreKeyPrefix}"`, ); + t.true( + restoreKeyPrefix.startsWith(workflowKeyPrefix), + `Expected restore key prefix "${restoreKeyPrefix}" to start with workflow key prefix "${workflowKeyPrefix}"`, + ); }); diff --git a/src/overlay-database-utils.ts b/src/overlay-database-utils.ts index 81d39a990..b4904e976 100644 --- a/src/overlay-database-utils.ts +++ b/src/overlay-database-utils.ts @@ -480,26 +480,37 @@ export async function getCacheRestoreKeyPrefix( codeQlVersion: string, ): Promise { const languages = [...config.languages].sort().join("_"); - - const cacheKeyComponents = { - automationID: await getAutomationID(), - // Add more components here as needed in the future - }; - const componentsHash = createCacheKeyHash(cacheKeyComponents); + const workflowPrefix = await getCacheWorkflowKeyPrefix(); // For a cached overlay-base database to be considered compatible for overlay // analysis, all components in the cache restore key must match: // - // CACHE_PREFIX: distinguishes overlay-base databases from other cache objects - // CACHE_VERSION: cache format version - // componentsHash: hash of additional components (see above for details) + // workflowPrefix contains components that depend only on the workflow: + // CACHE_PREFIX: distinguishes overlay-base databases from other cache objects + // CACHE_VERSION: cache format version + // componentsHash: hash of additional components (see above for details) // languages: the languages included in the overlay-base database // codeQlVersion: CodeQL bundle version // // Technically we can also include languages and codeQlVersion in the // componentsHash, but including them explicitly in the cache key makes it // easier to debug and understand the cache key structure. - return `${CACHE_PREFIX}-${CACHE_VERSION}-${componentsHash}-${languages}-${codeQlVersion}-`; + return `${workflowPrefix}${languages}-${codeQlVersion}-`; +} + +/** + * Computes the cache key prefix that depends only on the workflow. + * + * @returns A promise that resolves to the common cache key prefix in the format + * `${CACHE_PREFIX}-${CACHE_VERSION}-${componentsHash}-` + */ +export async function getCacheWorkflowKeyPrefix(): Promise { + const cacheKeyComponents = { + automationID: await getAutomationID(), + // Add more components here as needed in the future + }; + const componentsHash = createCacheKeyHash(cacheKeyComponents); + return `${CACHE_PREFIX}-${CACHE_VERSION}-${componentsHash}-`; } /**