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: cors due to otel broke history (#5350)

Wolf Mermelsteinabe5cfd0a389
diff --git a/apps/web/src/features/block-md/component/sidepanel/MarkdownSidePanelSections.tsx b/apps/web/src/features/block-md/component/sidepanel/MarkdownSidePanelSections.tsx
index 63bf23a2c5..d70a2fc5f2 100644
--- a/apps/web/src/features/block-md/component/sidepanel/MarkdownSidePanelSections.tsx
+++ b/apps/web/src/features/block-md/component/sidepanel/MarkdownSidePanelSections.tsx
@@ -168,7 +168,15 @@ function HistorySectionContent() {
<Show when={!history.loading.sessions()} fallback={<HistorySkeleton />}>
<Show
when={totalEdits() > 1 && history.sessions()}
- fallback={<p class="text-xs text-ink-muted">No history yet</p>}
+ fallback={
+ // Don't claim the document has no edits when we couldn't load them.
+ <p
+ class="text-xs text-ink-muted"
+ title={history.error() ?? undefined}
+ >
+ {history.error() ? "Couldn't load history" : 'No history yet'}
+ </p>
+ }
>
{(sessions) => (
<div class="hidden min-w-0 overflow-hidden md:block">
diff --git a/apps/web/src/features/block-md/history/HistoryContext.tsx b/apps/web/src/features/block-md/history/HistoryContext.tsx
index 75f1b6ff78..a95e742dce 100644
--- a/apps/web/src/features/block-md/history/HistoryContext.tsx
+++ b/apps/web/src/features/block-md/history/HistoryContext.tsx
@@ -33,6 +33,15 @@ export type HistoryUser = {
color: string;
};
+function describeError(error: unknown): string {
+ if (error instanceof ThrownResultError) {
+ const [first] = error.errors;
+ return first?.description ?? error.message;
+ }
+ if (error instanceof Error) return error.message;
+ return String(error);
+}
+
type HistoryContextValue = {
isOpen: Accessor<boolean>;
selectedAt: Accessor<Date | null>;
@@ -43,6 +52,8 @@ type HistoryContextValue = {
requestLoad: () => void;
sessions: Accessor<readonly HistorySession[]>;
loading: { sessions: Accessor<boolean>; doc: Accessor<boolean> };
+ /** Underlying failure message when the snapshot or peer map didn't load. */
+ error: Accessor<string | null>;
checkoutAt: (ms: number) => SerializedEditorState | null;
versionIdAt: (ms: number) => HistoryVersionId | null;
diff: {
@@ -133,6 +144,14 @@ export function HistoryProvider(props: {
const loadedPeers = (): Map<string, string> | undefined =>
peerMap.isSuccess ? peerMap.data : undefined;
+ // Either fetch failing leaves the timeline permanently empty, so surface it
+ // instead of leaving the UI to guess between "still loading" and "no edits".
+ const error = createMemo<string | null>(() => {
+ if (historyDoc.error !== undefined) return describeError(historyDoc.error);
+ if (peerMap.isError) return describeError(peerMap.error);
+ return null;
+ });
+
// One stable useDisplayName per unique userId — batched into a single fetch.
const uniqueUserIds = createMemo(() => {
const peers = loadedPeers();
@@ -238,9 +257,12 @@ export function HistoryProvider(props: {
requestLoad,
sessions,
loading: {
- sessions: () => loadedDoc() == null || peerMap.isPending,
- doc: () => loadedDoc() == null,
+ // A failed load is not a pending one — `error` owns that state.
+ sessions: () =>
+ error() == null && (loadedDoc() == null || peerMap.isPending),
+ doc: () => error() == null && loadedDoc() == null,
},
+ error,
checkoutAt,
versionIdAt,
userByPeer,
diff --git a/apps/web/src/lib/service-clients/service-sync/client.ts b/apps/web/src/lib/service-clients/service-sync/client.ts
index 00da07edbc..18daa91464 100644
--- a/apps/web/src/lib/service-clients/service-sync/client.ts
+++ b/apps/web/src/lib/service-clients/service-sync/client.ts
@@ -237,6 +237,18 @@ export const syncServiceClient = {
}
);
+ // Without this an error body decodes as a snapshot, surfacing as a bogus
+ // Loro parse failure. Transport failures (offline, CORS) reject instead,
+ // which callers already see.
+ if (!response.ok) {
+ return err([
+ {
+ code: 'SNAPSHOT_HTTP_ERROR',
+ message: `The sync service returned HTTP ${response.status}.`,
+ },
+ ]);
+ }
+
const data = await response.arrayBuffer();
const array = new Uint8Array(data);
diff --git a/services/sync-service/src/durable_object.rs b/services/sync-service/src/durable_object.rs
index 1b04830f7a..65598d05a9 100644
--- a/services/sync-service/src/durable_object.rs
+++ b/services/sync-service/src/durable_object.rs
@@ -1242,7 +1242,17 @@ pub fn cors(request_origin: Option<&str>) -> Cors {
Cors::new()
.with_credentials(true)
- .with_allowed_headers(vec!["authorization", "content-type"])
+ // `traceparent`/`tracestate` are injected by the web client's traced
+ // fetch wrapper (see `safeFetch`), so they must be preflight-allowed or
+ // the browser blocks every instrumented call. Kept in sync with
+ // `macro_cors::EXTRA_HEADERS`, which the axum services use; this worker
+ // can't share that list because it's built on tower-http.
+ .with_allowed_headers(vec![
+ "authorization",
+ "content-type",
+ "traceparent",
+ "tracestate",
+ ])
.with_methods(vec![
Method::Get,
Method::Post,
diff --git a/services/sync-service/tests/cors.test.ts b/services/sync-service/tests/cors.test.ts
index db6c3c650d..c70a670e87 100644
--- a/services/sync-service/tests/cors.test.ts
+++ b/services/sync-service/tests/cors.test.ts
@@ -163,6 +163,29 @@ describe('CORS middleware tests', async () => {
expect(allowedHeaders).toContain('content-type');
});
+ // The web client's traced fetch wrapper injects W3C trace context on every
+ // instrumented request. If these aren't preflight-allowed the browser blocks
+ // the response outright, which previously broke /metadata (and with it the
+ // document history timeline and blame tooltips).
+ test('should allow w3c trace context headers', async () => {
+ const response = await mf.dispatchFetch(
+ 'http://localhost:8787/document/test-doc/metadata',
+ {
+ method: 'OPTIONS',
+ headers: {
+ Origin: 'https://dev.macro.com',
+ 'Access-Control-Request-Method': 'GET',
+ 'Access-Control-Request-Headers': 'authorization,traceparent',
+ },
+ }
+ );
+
+ expect(response.status).toBe(200);
+ const allowedHeaders = response.headers.get('Access-Control-Allow-Headers');
+ expect(allowedHeaders).toContain('traceparent');
+ expect(allowedHeaders).toContain('tracestate');
+ });
+
test('should work with actual API requests after CORS validation', async () => {
// Create a document first
const user = await createTestUser(mf, 'cors-test-doc');