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(documents): stop reloading an owner's document on every access poll (#9118)

gbirman388db7ecf08f
diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.md
index 38f09df93c..8ce61c1ada 100644
--- a/apps/web/AGENTS.md
+++ b/apps/web/AGENTS.md
@@ -418,6 +418,8 @@ resolved session ID; keep the existing `AgentSessionProvider` subscription to
- Preserve loaded block content on initial access confirmation when its loader
supplies no permissions; reload only on a later confirmed permission change.
Apply shared access in place only after a successful permissionless load.
+ Request state can report an owner as edit, so compare a loaded level with
+ the confirmed one through `matchesConfirmedAccess`.
- Grouped Soup's `graphql/create-grouped-soup-live-query.ts` owns initial and
continuation pages plus their local predicate projection. Group readers only
delegate paging; pass the parent's live `groupedPaging` accessor from every
diff --git a/apps/web/src/features/access-requests/core/types.ts b/apps/web/src/features/access-requests/core/types.ts
index cf7f081fe7..285312cfda 100644
--- a/apps/web/src/features/access-requests/core/types.ts
+++ b/apps/web/src/features/access-requests/core/types.ts
@@ -61,6 +61,18 @@ export function shareEntity(
}
}
+/**
+ * Whether confirmed request-state access still matches the level a document
+ * loaded with. Request state can report an owner as edit, so a loaded owner
+ * matches a confirmed edit.
+ */
+export function matchesConfirmedAccess(
+ loaded: AccessLevel | undefined,
+ confirmed: AccessLevel
+): boolean {
+ return loaded === confirmed || (loaded === 'owner' && confirmed === 'edit');
+}
+
export function requestLevels(state: RequestState | undefined): RequestLevel[] {
if (!state || state.canReview) return [];
const rank: Record<AccessLevel, number> = {
diff --git a/apps/web/src/features/drive-view/components/FileDetail.test.tsx b/apps/web/src/features/drive-view/components/FileDetail.test.tsx
index 750c49eb0a..b6e430351a 100644
--- a/apps/web/src/features/drive-view/components/FileDetail.test.tsx
+++ b/apps/web/src/features/drive-view/components/FileDetail.test.tsx
@@ -139,6 +139,50 @@ describe('file access errors', () => {
expect(load).toHaveBeenCalledTimes(3);
});
+ it('keeps an owner’s open document when request state confirms edit', async () => {
+ const cleanupEditor = vi.fn();
+ const load = vi
+ .fn<() => Promise<Loaded>>()
+ .mockResolvedValueOnce({ userAccessLevel: 'owner' })
+ .mockResolvedValue({ userAccessLevel: 'view' });
+ const container = mount(load, cleanupEditor);
+ await vi.waitFor(() =>
+ expect(container.textContent).toContain('Document content: owner')
+ );
+ const content = container.firstElementChild;
+
+ notifyAccess?.('edit');
+ notifyAccess?.('edit');
+ notifyAccess?.('owner');
+ expect(load).toHaveBeenCalledTimes(1);
+ expect(cleanupEditor).not.toHaveBeenCalled();
+ expect(container.firstElementChild).toBe(content);
+
+ notifyAccess?.('view');
+ await vi.waitFor(() =>
+ expect(container.textContent).toContain('Document content: view')
+ );
+ expect(load).toHaveBeenCalledTimes(2);
+ });
+
+ it('restarts a pending load when access is confirmed during it', async () => {
+ const pending = Promise.withResolvers<Loaded>();
+ const load = vi
+ .fn<() => Promise<Loaded>>()
+ .mockReturnValueOnce(pending.promise)
+ .mockResolvedValue({ userAccessLevel: 'view' });
+ const container = mount(load);
+ // The access poll answers in a later task than the one that started the load.
+ await new Promise((resolve) => setTimeout(resolve));
+
+ notifyAccess?.('view');
+ pending.resolve({ userAccessLevel: 'edit' });
+ await vi.waitFor(() =>
+ expect(container.textContent).toContain('Document content: view')
+ );
+ expect(load).toHaveBeenCalledTimes(2);
+ });
+
it('counts an open once the document loads, not on access reloads', async () => {
const load = vi
.fn<() => Promise<Loaded>>()
diff --git a/apps/web/src/features/drive-view/components/FileDetail.tsx b/apps/web/src/features/drive-view/components/FileDetail.tsx
index 45ef9d698a..a83df2dd74 100644
--- a/apps/web/src/features/drive-view/components/FileDetail.tsx
+++ b/apps/web/src/features/drive-view/components/FileDetail.tsx
@@ -2,7 +2,10 @@ import {
DocumentAccessDenied,
useSharedAccessUpdates,
} from '@app/features/access-requests/access-requests';
-import { shareEntity } from '@app/features/access-requests/core/types';
+import {
+ matchesConfirmedAccess,
+ shareEntity,
+} from '@app/features/access-requests/core/types';
import { useAnalytics } from '@app/lib/analytics/analytics-context';
import {
DocumentFileSidePanelSections,
@@ -138,7 +141,7 @@ export function FileDetailLoadGate<
return;
}
const loaded = document.state === 'ready' ? document.latest : undefined;
- if (loaded?.userAccessLevel !== level) reload();
+ if (!matchesConfirmedAccess(loaded?.userAccessLevel, level)) reload();
}
);
const accessDenied = () => {
diff --git a/apps/web/src/lib/core/internal/BlockLoader.test.tsx b/apps/web/src/lib/core/internal/BlockLoader.test.tsx
index be5430465e..7c494cbdc1 100644
--- a/apps/web/src/lib/core/internal/BlockLoader.test.tsx
+++ b/apps/web/src/lib/core/internal/BlockLoader.test.tsx
@@ -273,6 +273,43 @@ it.each(['view', undefined] as const)(
}
);
+it('keeps an owner’s open document when request state confirms edit', async () => {
+ const loaded = Promise.withResolvers<void>();
+ const cleanupSource = vi.fn();
+ const load = vi.fn(async () =>
+ ok({ userAccessLevel: 'owner', syncSource: { cleanup: cleanupSource } })
+ );
+ render(() => {
+ createEffect(
+ on(blockDataSignal.get, (data) => {
+ if (data) loaded.resolve();
+ })
+ );
+ return (
+ <BlockLoader
+ id="01a140ad-06d5-7791-8b68-bc128654bbba"
+ source={{ type: 'dss', id: '01a140ad-06d5-7791-8b68-bc128654bbba' }}
+ definition={{
+ name: 'md',
+ description: 'Document',
+ accepted: {},
+ component: () => null,
+ load,
+ }}
+ />
+ );
+ });
+ await loaded.promise;
+ const data = blockDataSignal.get();
+
+ notifyAccess('edit');
+ notifyAccess('edit');
+ expect(cleanupSource).not.toHaveBeenCalled();
+ expect(blockDataSignal.get()).toBe(data);
+ expect(blockUserAccessSignal.get()).toBe('owner');
+ expect(load).toHaveBeenCalledTimes(1);
+});
+
it('reconciles a stale permission-bearing load on a later unchanged confirmation', async () => {
const pending = Promise.withResolvers<void>();
const loaded = Promise.withResolvers<void>();
diff --git a/apps/web/src/lib/core/internal/BlockLoader.tsx b/apps/web/src/lib/core/internal/BlockLoader.tsx
index 23a7b21529..29a57cc76c 100644
--- a/apps/web/src/lib/core/internal/BlockLoader.tsx
+++ b/apps/web/src/lib/core/internal/BlockLoader.tsx
@@ -1,5 +1,8 @@
import { useSharedAccessUpdates } from '@app/features/access-requests/access-requests';
-import { shareEntity } from '@app/features/access-requests/core/types';
+import {
+ matchesConfirmedAccess,
+ shareEntity,
+} from '@app/features/access-requests/core/types';
import { useAnalytics } from '@app/lib/analytics/analytics-context';
import { useIsAuthenticated } from '@core/context/user';
import type { OwnedBlockHandle } from '@core/orchestrator';
@@ -74,18 +77,20 @@ export function BlockLoader<
const currentSharedAccess = useSharedAccessUpdates(
() => shareEntity(blockNameToItemType(props.definition.name), props.id),
(level, previous) => {
+ if (level === null) {
+ clearLoadedDocument();
+ setError('UNAUTHORIZED');
+ return;
+ }
const data = blockDataSignal.get();
const hasLoadedPermissions =
data != null && typeof data === 'object' && 'userAccessLevel' in data;
// Lightweight loaders (e.g. email) supply no permissions. Their initial
// fallback is not a confirmed access change that requires a remount.
const accessChanged = hasLoadedPermissions
- ? blockUserAccessSignal.get() !== level
+ ? !matchesConfirmedAccess(blockUserAccessSignal.get(), level)
: previous !== undefined && previous !== level;
- if (level === null) {
- clearLoadedDocument();
- setError('UNAUTHORIZED');
- } else if (
+ if (
(data != null && accessChanged) ||
blockErrorSignal.get() === 'UNAUTHORIZED'
) {