Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract getCacheWorkflowKeyPrefix()
  • Loading branch information
cklin committed Sep 26, 2025
commit c35d1b3eb4c8bdd944c52cb79576f8f30cb47c71
15 changes: 15 additions & 0 deletions src/overlay-database-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
downloadOverlayBaseDatabaseFromCache,
getCacheRestoreKeyPrefix,
getCacheSaveKey,
getCacheWorkflowKeyPrefix,
OverlayDatabaseMode,
writeBaseDatabaseOidsFile,
writeOverlayChangesFile,
Expand Down Expand Up @@ -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}"`,
);
});
31 changes: 21 additions & 10 deletions src/overlay-database-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}-`;
}

/**
Expand Down