Extract getCacheWorkflowKeyPrefix()

This commit is contained in:
Chuan-kai Lin
2025-09-24 12:59:10 -07:00
parent eb5531f129
commit c35d1b3eb4
2 changed files with 36 additions and 10 deletions

View File

@@ -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}"`,
);
});

View File

@@ -480,26 +480,37 @@ export async function getCacheRestoreKeyPrefix(
codeQlVersion: string,
): Promise<string> {
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<string> {
const cacheKeyComponents = {
automationID: await getAutomationID(),
// Add more components here as needed in the future
};
const componentsHash = createCacheKeyHash(cacheKeyComponents);
return `${CACHE_PREFIX}-${CACHE_VERSION}-${componentsHash}-`;
}
/**