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.

feat(md/tasks): use batch fire and forget wakeup (#3054)

teoe69b4ad7b597
diff --git a/js/app/packages/app/component/command/CommandItem.tsx b/js/app/packages/app/component/command/CommandItem.tsx
index 3c7b46d30f..32ac0c1f1f 100644
--- a/js/app/packages/app/component/command/CommandItem.tsx
+++ b/js/app/packages/app/component/command/CommandItem.tsx
@@ -1,7 +1,7 @@
import { Hotkey } from '@core/component/Hotkey';
import { hasValidHotkey } from '@core/hotkey/utils';
import type { HotkeySequenceStep } from '@core/component/Tooltip';
-import { For, Match, Show, Switch } from 'solid-js';
+import { createEffect, For, Match, Show, Switch } from 'solid-js';
import {
isCommandItem,
isEntityItem,
@@ -10,6 +10,12 @@ import {
type SearchItem,
} from './useCommandItems';
import { Entity, type EntityData } from '@entity';
+import { useFeatureFlag } from '@app/lib/analytics/posthog';
+import {
+ BULK_DOCUMENT_WAKEUP_FEATURE_FLAG,
+ enqueueDocumentWakeup,
+ isWakeableDocument,
+} from '@queries/preview';
import { cn } from '@ui/utils/classname';
import Terminal from '@phosphor-icons/core/regular/terminal.svg?component-solid';
import SearchIcon from '@macro-icons/macro-magnifying-glass.svg';
@@ -109,6 +115,15 @@ function CommandDisplay(props: { item: CommandMenuItem }) {
}
function EntityDisplay(props: { entity: EntityData }) {
+ const bulkWakeupEnabled = useFeatureFlag(BULK_DOCUMENT_WAKEUP_FEATURE_FLAG);
+
+ createEffect(() => {
+ if (!bulkWakeupEnabled().enabled) return;
+ if (!isWakeableDocument(props.entity)) return;
+
+ enqueueDocumentWakeup(props.entity);
+ });
+
return (
<div class="flex items-center gap-2 flex-1 min-w-0">
<div class="size-5 p-0.5 flex items-center justify-center text-ink-muted shrink-0">
diff --git a/js/app/packages/core/component/ItemPreview.tsx b/js/app/packages/core/component/ItemPreview.tsx
index 62c312254b..1e457905f7 100644
--- a/js/app/packages/core/component/ItemPreview.tsx
+++ b/js/app/packages/core/component/ItemPreview.tsx
@@ -1,7 +1,10 @@
import type { BlockAlias, BlockName } from '@core/block';
import { fileTypeToBlockName } from '@core/constant/allBlocks';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
+import { useFeatureFlag } from '@app/lib/analytics/posthog';
import {
+ BULK_DOCUMENT_WAKEUP_FEATURE_FLAG,
+ enqueuePreviewWakeup,
isAccessiblePreviewItem,
useItemPreview,
type ItemEntity,
@@ -20,6 +23,7 @@ import {
Match,
Switch,
Suspense,
+ createEffect,
type ComponentProps,
type Accessor,
} from 'solid-js';
@@ -35,6 +39,13 @@ import { cn } from '@ui/utils/classname';
export function useItemPreviewData(entity: Accessor<ItemEntity>) {
const [item] = useItemPreview(entity);
+ const bulkWakeupEnabled = useFeatureFlag(BULK_DOCUMENT_WAKEUP_FEATURE_FLAG);
+
+ createEffect(() => {
+ if (!bulkWakeupEnabled().enabled) return;
+
+ enqueuePreviewWakeup(item());
+ });
const { replaceOrInsertSplit, insertSplit } = useSplitLayout();
diff --git a/js/app/packages/entity/src/composed/ListEntity.tsx b/js/app/packages/entity/src/composed/ListEntity.tsx
index 4a29141cf4..4cebde5cc9 100644
--- a/js/app/packages/entity/src/composed/ListEntity.tsx
+++ b/js/app/packages/entity/src/composed/ListEntity.tsx
@@ -1,8 +1,14 @@
import './ListEntity.css';
import { EntityRow, EntityRowContext } from '@app/component/mobile/EntityRow';
import { useSplitPanel } from '@app/component/split-layout/layoutUtils';
+import { useFeatureFlag } from '@app/lib/analytics/posthog';
import { isMobile } from '@core/mobile/isMobile';
import type { DateValue } from '@core/util/date';
+import {
+ BULK_DOCUMENT_WAKEUP_FEATURE_FLAG,
+ enqueueDocumentWakeup,
+ isWakeableDocument,
+} from '@queries/preview';
import { stackNotifications } from '@notifications';
import {
getStreamState,
@@ -105,6 +111,14 @@ function MaybeEntityRow(props: {
export function ListEntity(props: ListEntityProps) {
const unread = () => unreadFilterFn(props.entity);
const isShared = useIsShared(props.entity);
+ const bulkWakeupEnabled = useFeatureFlag(BULK_DOCUMENT_WAKEUP_FEATURE_FLAG);
+
+ createEffect(() => {
+ if (!bulkWakeupEnabled().enabled) return;
+ if (!isWakeableDocument(props.entity)) return;
+
+ enqueueDocumentWakeup(props.entity);
+ });
subscribeToStreamState(props.entity.id, props.entity.type);
const streamState = getStreamState(props.entity.id);
diff --git a/js/app/packages/queries/preview/fetchers.ts b/js/app/packages/queries/preview/fetchers.ts
index 72bf84ed13..7271f4f45a 100644
--- a/js/app/packages/queries/preview/fetchers.ts
+++ b/js/app/packages/queries/preview/fetchers.ts
@@ -6,7 +6,6 @@ import { commsServiceClient } from '@service-comms/client';
import { emailClient } from '@service-email/client';
import { storageServiceClient } from '@service-storage/client';
import type { FileType } from '@service-storage/generated/schemas/fileType';
-import { syncServiceClient } from '@service-sync/client';
import type { ItemEntity, MessageContext, PreviewItem } from './types';
async function fetchChannelPreviews(
@@ -89,9 +88,6 @@ async function fetchDocumentPreviews(ids: string[]): Promise<PreviewItem[]> {
switch (doc.type) {
case 'access':
- if (doc.file_type === 'md') {
- syncServiceClient.safeWakeup(doc.document_id);
- }
return {
...base,
access: 'access' as const,
diff --git a/js/app/packages/queries/preview/index.ts b/js/app/packages/queries/preview/index.ts
index ecc1dd5644..dd24775d4d 100644
--- a/js/app/packages/queries/preview/index.ts
+++ b/js/app/packages/queries/preview/index.ts
@@ -19,3 +19,10 @@ export {
isChannelPreviewItem,
isPreviewItemNoAccess,
} from './types';
+export {
+ BULK_DOCUMENT_WAKEUP_FEATURE_FLAG,
+ enqueueDocumentWakeup,
+ enqueuePreviewWakeup,
+ isWakeableDocument,
+ type WakeableDocument,
+} from './wakeup';
diff --git a/js/app/packages/queries/preview/wakeup.ts b/js/app/packages/queries/preview/wakeup.ts
new file mode 100644
index 0000000000..b055531f9f
--- /dev/null
+++ b/js/app/packages/queries/preview/wakeup.ts
@@ -0,0 +1,89 @@
+import { AsyncBatcher } from '@tanstack/pacer';
+import { storageServiceClient } from '@service-storage/client';
+import { isErr } from '@core/util/maybeResult';
+import type { ItemLike } from '@core/constant/allBlocks';
+import type { PreviewItem } from './types';
+
+const WAKEUP_TTL_MS = 60 * 1000;
+const WAKEUP_DEBOUNCE_MS = 200;
+const WAKEUP_MAX_BATCH_SIZE = 100;
+
+export const BULK_DOCUMENT_WAKEUP_FEATURE_FLAG = 'enable-bulk-document-wakeup';
+
+const recentWakeups = new Map<string, number>();
+
+function cleanupRecentWakeups(now: number) {
+ for (const [documentId, timestamp] of recentWakeups.entries()) {
+ if (now - timestamp >= WAKEUP_TTL_MS) {
+ recentWakeups.delete(documentId);
+ }
+ }
+}
+
+const documentWakeupBatcher = new AsyncBatcher<string>(
+ async (documentIds) => {
+ const uniqueDocumentIds = [...new Set(documentIds)];
+ if (uniqueDocumentIds.length === 0) return;
+
+ const result = await storageServiceClient.bulkWakeupSyncServiceDocuments({
+ document_ids: uniqueDocumentIds,
+ });
+
+ if (isErr(result)) {
+ throw new Error('Failed to bulk wakeup sync service documents');
+ }
+ },
+ {
+ wait: WAKEUP_DEBOUNCE_MS,
+ maxSize: WAKEUP_MAX_BATCH_SIZE,
+ asyncRetryerOptions: {
+ maxAttempts: 2,
+ backoff: 'exponential',
+ baseWait: 200,
+ jitter: 0.1,
+ throwOnError: 'last',
+ },
+ onError: (error, documentIds) => {
+ for (const documentId of documentIds) {
+ recentWakeups.delete(documentId);
+ }
+ console.error('Failed to bulk wakeup sync service documents', error);
+ },
+ throwOnError: false,
+ }
+);
+
+function enqueueDocumentIdWakeup(documentId: string) {
+ const now = Date.now();
+ const lastWakeup = recentWakeups.get(documentId);
+ if (lastWakeup && now - lastWakeup < WAKEUP_TTL_MS) {
+ return;
+ }
+
+ recentWakeups.set(documentId, now);
+ cleanupRecentWakeups(now);
+ documentWakeupBatcher.addItem(documentId);
+}
+
+export type WakeableDocument = ItemLike & {
+ id: string;
+ type: 'document';
+ fileType: 'md';
+};
+
+export function isWakeableDocument(
+ item: ItemLike & { id: string }
+): item is WakeableDocument {
+ return item.type === 'document' && item.fileType === 'md';
+}
+
+export function enqueueDocumentWakeup(item: WakeableDocument) {
+ enqueueDocumentIdWakeup(item.id);
+}
+
+export function enqueuePreviewWakeup(item: PreviewItem) {
+ if (item.loading || item.access !== 'access') return;
+ if (!isWakeableDocument(item)) return;
+
+ enqueueDocumentWakeup(item);
+}
diff --git a/js/app/packages/service-clients/service-storage/client.ts b/js/app/packages/service-clients/service-storage/client.ts
index 12261a67c5..ab4da0208a 100644
--- a/js/app/packages/service-clients/service-storage/client.ts
+++ b/js/app/packages/service-clients/service-storage/client.ts
@@ -221,6 +221,16 @@ export const storageServiceClient = {
);
},
+ async bulkWakeupSyncServiceDocuments(args: { document_ids: string[] }) {
+ return mapOk(
+ await dssFetch<{ dispatched: number }>(`/sync_service/wakeup`, {
+ method: 'POST',
+ body: JSON.stringify({ document_ids: args.document_ids }),
+ }),
+ (result) => result
+ );
+ },
+
async getSoupItems(args: {
params: { cursor?: string | null };
body: PostSoupRequest;
diff --git a/js/app/packages/service-clients/service-storage/generated/zod.ts b/js/app/packages/service-clients/service-storage/generated/zod.ts
index 3ba78b8e07..645cdc6d7f 100644
--- a/js/app/packages/service-clients/service-storage/generated/zod.ts
+++ b/js/app/packages/service-clients/service-storage/generated/zod.ts
@@ -3893,10 +3893,6 @@ export const createInstructionsHandlerResponse = zod.object({
documentId: zod.string(),
});
-export const bulkWakeupSyncServiceDocumentsBody = zod.object({
- document_ids: zod.array(zod.string()),
-});
-
/**
* @summary Gets the items the user has access to
*/
@@ -10107,6 +10103,10 @@ export const patchViewHandlerBody = zod.object({
name: zod.string().nullish(),
});
+export const bulkWakeupSyncServiceDocumentsBody = zod.object({
+ document_ids: zod.array(zod.string()),
+});
+
/**
* @summary Edits the share permissions of a thread.
*/
diff --git a/js/app/packages/service-clients/service-storage/openapi.json b/js/app/packages/service-clients/service-storage/openapi.json
index a8d4d105e7..3702c2cedb 100644
--- a/js/app/packages/service-clients/service-storage/openapi.json
+++ b/js/app/packages/service-clients/service-storage/openapi.json
@@ -3807,40 +3807,6 @@
}
}
},
- "/internal/sync_service/wakeup": {
- "post": {
- "tags": ["sync_service"],
- "operationId": "bulk_wakeup_sync_service_documents",
- "requestBody": {
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/BulkWakeupRequest"
- }
- }
- },
- "required": true
- },
- "responses": {
- "202": {
- "description": "Wakeups accepted for fire-and-forget dispatch",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/BulkWakeupResponse"
- }
- }
- }
- },
- "400": {
- "description": "Malformed request or missing internal auth header"
- },
- "401": {
- "description": "Invalid internal auth header"
- }
- }
- }
- },
"/items/soup": {
"get": {
"tags": ["soup::inbound::axum_router"],
@@ -5306,6 +5272,40 @@
}
}
},
+ "/sync_service/wakeup": {
+ "post": {
+ "tags": ["sync_service"],
+ "operationId": "bulk_wakeup_sync_service_documents",
+ "requestBody": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/BulkWakeupRequest"
+ }
+ }
+ },
+ "required": true
+ },
+ "responses": {
+ "202": {
+ "description": "Wakeups accepted for fire-and-forget dispatch",
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/BulkWakeupResponse"
+ }
+ }
+ }
+ },
+ "400": {
+ "description": "Malformed request or missing internal auth header"
+ },
+ "401": {
+ "description": "Invalid internal auth header"
+ }
+ }
+ }
+ },
"/threads/{thread_id}": {
"patch": {
"tags": ["threads"],
diff --git a/rust/cloud-storage/document_storage_service/src/api/mod.rs b/rust/cloud-storage/document_storage_service/src/api/mod.rs
index c2aeedba60..988c1fd57a 100644
--- a/rust/cloud-storage/document_storage_service/src/api/mod.rs
+++ b/rust/cloud-storage/document_storage_service/src/api/mod.rs
@@ -193,6 +193,14 @@ fn api_router(state: ApiContext) -> Router {
"/search",
search_service::search_router().with_state(SearchHandlerState::from_ref(&state)),
)
+ .nest(
+ "/sync_service",
+ sync_service_hex::inbound::axum_router::sync_service_router(
+ sync_service_hex::inbound::axum_router::SyncServiceRouterState {
+ service: state.sync_service_client.clone(),
+ },
+ ),
+ )
.nest(
"/comms",
comms_service::comms_router(&CommsHandlerState::from_ref(&state))
diff --git a/rust/cloud-storage/sync_service/src/inbound/axum_router.rs b/rust/cloud-storage/sync_service/src/inbound/axum_router.rs
index 14c527b939..1c0283945d 100644
--- a/rust/cloud-storage/sync_service/src/inbound/axum_router.rs
+++ b/rust/cloud-storage/sync_service/src/inbound/axum_router.rs
@@ -38,7 +38,7 @@ where
#[utoipa::path(
tag = "sync_service",
post,
- path = "/internal/sync_service/wakeup",
+ path = "/sync_service/wakeup",
operation_id = "bulk_wakeup_sync_service_documents",
request_body = BulkWakeupRequest,
responses(