Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(cache): isolate coordinators across app builds (#8252)

gbirman5a71f31b0a4d
diff --git a/apps/web/docs/graphql-normalized-cache-plan.md b/apps/web/docs/graphql-normalized-cache-plan.md
index 88d6774c85..e5a848cb32 100644
--- a/apps/web/docs/graphql-normalized-cache-plan.md
+++ b/apps/web/docs/graphql-normalized-cache-plan.md
@@ -230,11 +230,13 @@ scope only when the coordinator explicitly proves storage was untouched, not fro
a page's last-seen phase. Missing/broken transports remain conservatively
uncertain.
-A build's coordinator is usually a separate SharedWorker (its script URL is
-content-hashed), so after a deploy two builds can target one database. The newer
-build takes it over. Pages report their build time (`__APP_BUILD_TIME__`,
-stamped by Vite) when they register. When the lock stays busy while tabs of
-another build are alive (liveness locks of tabs this coordinator never
+A build's coordinator has its build time in the SharedWorker name, so builds
+use separate coordinators even when their coordinator script URL is identical.
+This keeps each build's tabs attached to its matching engine and GraphQL schema.
+The database identity stays shared across compatible builds; the newer build
+takes it over. Pages use the same build time (`__APP_BUILD_TIME__`, stamped by
+Vite) in the worker name and when they register. When the lock stays busy while
+tabs of another build are alive (liveness locks of tabs this coordinator never
registered; a departed tab's lock can outlast it), the coordinator posts a
takeover request on the cross-build channel
`graphql-cache-takeover:{database name}` (`coordinator-takeover.ts`, a contract
diff --git a/apps/web/src/lib/graphql-cache/worker/browser-test/coordinator.browser.e2e.ts b/apps/web/src/lib/graphql-cache/worker/browser-test/coordinator.browser.e2e.ts
index 138577934e..56498a0aaf 100644
--- a/apps/web/src/lib/graphql-cache/worker/browser-test/coordinator.browser.e2e.ts
+++ b/apps/web/src/lib/graphql-cache/worker/browser-test/coordinator.browser.e2e.ts
@@ -377,7 +377,7 @@ test('production engine waits out a departing owner that still holds the files,
expect(browserErrors).toEqual([]);
});
-test('a newer build takes the production database over and older builds are turned away', async ({
+test('a newer build with the same coordinator URL takes the production database over', async ({
context,
page,
}, testInfo) => {
diff --git a/apps/web/src/lib/graphql-cache/worker/browser-test/production-tab.ts b/apps/web/src/lib/graphql-cache/worker/browser-test/production-tab.ts
index 5e81d4520d..6ed635b6b5 100644
--- a/apps/web/src/lib/graphql-cache/worker/browser-test/production-tab.ts
+++ b/apps/web/src/lib/graphql-cache/worker/browser-test/production-tab.ts
@@ -13,9 +13,7 @@ const parameters = new URLSearchParams(location.search);
const tabId = parameters.get('tabId') ?? '';
const scope = parameters.get('scope') ?? '';
if (!tabId || !scope) throw new Error('missing production harness parameters');
-// A named build gets its own coordinator, as each deployed build does, and
-// reports its build time so the newer build can take the database over.
-const build = parameters.get('build');
+// Exercise the real coordinator identity even when builds share its script URL.
const buildTime = Number(parameters.get('buildTime') ?? 0);
const QUERY = `query Soup($input: SoupInput!) {
@@ -54,18 +52,6 @@ const adapter = createCacheCoordinatorPageAdapter({
scope,
tabId,
buildTime,
- ...(build
- ? {
- createSharedWorker: (workerScope: string) =>
- new SharedWorker(
- new URL('../cache.coordinator.shared-worker.ts', import.meta.url),
- {
- type: 'module',
- name: `graphql-cache-coordinator:${workerScope}:build-${build}`,
- }
- ),
- }
- : {}),
createDedicatedWorker: (_workerScope, ownerEpoch) =>
new Worker(
new URL('./production-cache.engine-worker.ts', import.meta.url),
diff --git a/apps/web/src/lib/graphql-cache/worker/browser-test/production-takeover-harness.ts b/apps/web/src/lib/graphql-cache/worker/browser-test/production-takeover-harness.ts
index ec173c4ac9..a2bbbd6cb4 100644
--- a/apps/web/src/lib/graphql-cache/worker/browser-test/production-takeover-harness.ts
+++ b/apps/web/src/lib/graphql-cache/worker/browser-test/production-takeover-harness.ts
@@ -3,8 +3,8 @@ import type {
ProductionHarnessEnvelope,
} from './production-browser-wire';
-// Tabs of three simulated builds share one scope. Each build runs its own
-// coordinator over the one database, as deployed builds do. The newer build
+// Tabs of three simulated builds share one scope and coordinator script URL.
+// Each build must run its own coordinator over the one database. The newer build
// must take the database over without wiping it, and send the older build's
// tabs on; an even older build must be turned away.
@@ -132,7 +132,6 @@ const run = async (): Promise<Record<string, unknown>> => {
const url = new URL('./production-tab.html', location.href);
url.searchParams.set('scope', scope);
url.searchParams.set('tabId', tabId);
- url.searchParams.set('build', build);
url.searchParams.set('buildTime', String(BUILDS[build]));
const popup = window.open(url, `${scope}:${tabId}`);
if (!popup) throw new Error(`popup blocked for ${tabId}`);
diff --git a/apps/web/src/lib/graphql-cache/worker/coordinator-page-adapter.test.ts b/apps/web/src/lib/graphql-cache/worker/coordinator-page-adapter.test.ts
index fc96cbc709..423c8dc07e 100644
--- a/apps/web/src/lib/graphql-cache/worker/coordinator-page-adapter.test.ts
+++ b/apps/web/src/lib/graphql-cache/worker/coordinator-page-adapter.test.ts
@@ -325,6 +325,65 @@ describe('CacheCoordinatorPageAdapter', () => {
});
});
+ it('isolates different builds when their coordinator script URL is unchanged', async () => {
+ const workers: Array<{
+ url: string;
+ name: string | undefined;
+ port: FakeCoordinatorPort;
+ }> = [];
+ for (const [tabId, buildTime] of [
+ ['old-a', 1_000],
+ ['old-b', 1_000],
+ ['new-a', 2_000],
+ ] as const) {
+ const registration = Promise.withResolvers<FakeCoordinatorPort>();
+ vi.stubGlobal(
+ 'SharedWorker',
+ class {
+ readonly port = new FakeCoordinatorPort();
+
+ constructor(url: URL, options: WorkerOptions) {
+ workers.push({
+ url: url.href,
+ name: options.name,
+ port: this.port,
+ });
+ const postMessage = this.port.postMessage.bind(this.port);
+ vi.spyOn(this.port, 'postMessage').mockImplementation(
+ (message, transfer) => {
+ postMessage(message, transfer);
+ registration.resolve(this.port);
+ }
+ );
+ }
+ }
+ );
+ const adapter = createCacheCoordinatorPageAdapter({
+ scope: 'scope',
+ tabId,
+ buildTime,
+ lockManager: heldLockManager(),
+ createDedicatedWorker: () => new FakeWorker(),
+ });
+ const started = adapter.start();
+ const port = await registration.promise;
+ expect(port.messages[0]?.message).toMatchObject({
+ kind: 'register-tab',
+ scope: 'scope',
+ tabId,
+ buildTime,
+ });
+ port.receive({ ...version, kind: 'registered', tabId });
+ await started;
+ await adapter.dispose({ graceful: false });
+ }
+
+ expect(workers).toHaveLength(3);
+ expect(workers[0]?.url).toBe(workers[2]?.url);
+ expect(workers[0]?.name).toBe(workers[1]?.name);
+ expect(workers[0]?.name).not.toBe(workers[2]?.name);
+ });
+
it('deletes stale databases in a disposable worker, once, for its own engine', async () => {
const cleanupWorker = new FakeWorker();
const createCleanupWorker = vi.fn(() => cleanupWorker);
diff --git a/apps/web/src/lib/graphql-cache/worker/coordinator-page-adapter.ts b/apps/web/src/lib/graphql-cache/worker/coordinator-page-adapter.ts
index e20fe36935..652af7ee48 100644
--- a/apps/web/src/lib/graphql-cache/worker/coordinator-page-adapter.ts
+++ b/apps/web/src/lib/graphql-cache/worker/coordinator-page-adapter.ts
@@ -106,10 +106,13 @@ const withVersion = <
...value,
}) as unknown as T;
-const defaultSharedWorkerFactory = (scope: string): SharedWorkerLike =>
+const defaultSharedWorkerFactory = (
+ scope: string,
+ buildTime: number
+): SharedWorkerLike =>
new SharedWorker(
new URL('./cache.coordinator.shared-worker.ts', import.meta.url),
- { type: 'module', name: `graphql-cache-coordinator:${scope}` }
+ { type: 'module', name: `graphql-cache-coordinator:${scope}:${buildTime}` }
);
const defaultDedicatedWorkerFactory = (
@@ -181,7 +184,9 @@ export class CacheCoordinatorPageAdapter {
}
this.tabId = options.tabId ?? crypto.randomUUID();
this.createSharedWorker =
- options.createSharedWorker ?? defaultSharedWorkerFactory;
+ options.createSharedWorker ??
+ ((scope) =>
+ defaultSharedWorkerFactory(scope, options.buildTime ?? APP_BUILD_TIME));
this.createDedicatedWorker =
options.createDedicatedWorker ?? defaultDedicatedWorkerFactory;
this.lockManager = options.lockManager;
diff --git a/apps/web/src/lib/graphql-cache/worker/coordinator-takeover.ts b/apps/web/src/lib/graphql-cache/worker/coordinator-takeover.ts
index 3c4cc7eccd..c0845f8f0e 100644
--- a/apps/web/src/lib/graphql-cache/worker/coordinator-takeover.ts
+++ b/apps/web/src/lib/graphql-cache/worker/coordinator-takeover.ts
@@ -3,9 +3,9 @@ import { cacheDatabaseIdentity } from './coordinator-protocol';
/**
* Handover of the cache database between app builds.
*
- * A build usually runs its own coordinator, because the coordinator's script
- * URL is content-hashed, yet builds with the same storage version open one
- * database. When a newer build finds that database held by an older one, it
+ * A build runs its own coordinator, with its build time in the SharedWorker
+ * name, yet builds with the same storage version open one database.
+ * When a newer build finds that database held by an older one, it
* asks on this channel, which only builds opening the same database join.
* The holder either yields, closing the database and telling its tabs to
* reload into the newer build, or keeps it.