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(comments): stop commenter sessions writing the shared document (#9576)

Wolf Mermelsteine2aa64db32d4
diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.md
index 67a87c1d81..21cb5865a7 100644
--- a/apps/web/AGENTS.md
+++ b/apps/web/AGENTS.md
@@ -1143,7 +1143,9 @@ Keep collaborative drafts as cursor-based overlays outside the shared editor tre
Flush pending text, catch up missing server history before publication, then
publish the mark from an isolated peer with `PeerCommentUpdate`,
and post the message only after acknowledgement. Refused operations never enter
-the editor's WAL. Preserve input on failure. Cancelled publications retry mark
+the editor's WAL, so a session without edit access writes nothing to Loro, not
+even state its editor derives; only editors remove a deleted comment's mark.
+Preserve input on failure. Cancelled publications retry mark
cleanup from the user/document-scoped IndexedDB queue; preserve marks when a
message request's outcome is uncertain. Keep failed cleanup entries queued without
blocking later entries. Overlapping comments must retain each
@@ -1152,8 +1154,8 @@ Comment controls preserve editor focus on both pointerdown and mousedown.
The backend-free browser regression is
`bunx playwright test --config src/features/block-md/comments/browser-test/playwright.config.ts`.
It exercises selection, press, cancel, retry, and repeated posting in editor and
-commenter modes. The collaboration fixture includes concurrent remote edits and
-rejected updates; provider tests cover message metadata arriving on either side
+commenter modes. The collaboration fixture includes concurrent remote edits,
+rejected updates, and a server that refuses a commenter's non-comment updates; provider tests cover message metadata arriving on either side
of posting, multi-paragraph anchors, saved ranges, and cancellation during posting.
## Performance
diff --git a/apps/web/src/features/block-md/comments/MessageCommentsProvider.tsx b/apps/web/src/features/block-md/comments/MessageCommentsProvider.tsx
index 4c3d95c198..2e1f739c57 100644
--- a/apps/web/src/features/block-md/comments/MessageCommentsProvider.tsx
+++ b/apps/web/src/features/block-md/comments/MessageCommentsProvider.tsx
@@ -166,7 +166,6 @@ export const MessageCommentsProvider: VoidComponent<{
setCommentState('highlightedCommentId', v);
const setActiveMarkIds = activeMarkIdsSignal.set;
const canEdit = permissions.canEdit;
- const canComment = permissions.canComment;
const userId = useUserId();
const patchThread = usePatchThreadMutation();
// Pending commands, not a comment projection: metadata for an old root may
@@ -236,11 +235,9 @@ export const MessageCommentsProvider: VoidComponent<{
if (node && element) updateMarkPresentation(node, element, markId);
}
});
- // Deleting one's own comment also includes removing its persisted mark.
- if (
- state.deleted_at &&
- (canEdit() || (canComment() && state.user_id === userId()))
- )
+ // Only an editor's session writes the document, so it removes the
+ // persisted mark, including for comments deleted while it was closed.
+ if (state.deleted_at && canEdit())
editor.dispatchCommand(DELETE_COMMENT_COMMAND, [markId, true]);
}
};
diff --git a/apps/web/src/features/block-md/comments/browser-test/collab.tsx b/apps/web/src/features/block-md/comments/browser-test/collab.tsx
index 294eac5b67..1afe621252 100644
--- a/apps/web/src/features/block-md/comments/browser-test/collab.tsx
+++ b/apps/web/src/features/block-md/comments/browser-test/collab.tsx
@@ -69,8 +69,13 @@ function Fixture() {
listeners.delete(listener);
};
},
- pushUpdate: async (updates) => {
- if (new URLSearchParams(location.search).has('reject')) return false;
+ pushUpdate: async (updates, options) => {
+ const params = new URLSearchParams(location.search);
+ if (params.has('reject')) return false;
+ // Sync-service accepts only comment marks from a commenter, so any
+ // other change from their session, even one the editor derives on its
+ // own, is never acknowledged.
+ if (params.has('readonly') && !options?.commentOnly) return false;
for (const update of updates) {
if (server.importUpdate(update).isErr()) return false;
}
@@ -281,7 +286,6 @@ function Editor(props: {
syncSource={props.syncSource}
sourceReady={() => true}
canEdit={() => !readonly}
- canComment={() => true}
editorError={editorError}
/>
</div>
diff --git a/apps/web/src/features/block-md/component/MarkdownCollabProvider.tsx b/apps/web/src/features/block-md/component/MarkdownCollabProvider.tsx
index 4c3a672ef1..d9ebfbe318 100644
--- a/apps/web/src/features/block-md/component/MarkdownCollabProvider.tsx
+++ b/apps/web/src/features/block-md/component/MarkdownCollabProvider.tsx
@@ -55,7 +55,6 @@ export function MarkdownCollabProvider(props: MarkdownCollabProviderProps) {
syncSource={syncSource}
sourceReady={() => documentSource().type === 'sync'}
canEdit={permissions.canEdit}
- canComment={permissions.canComment}
editorError={state.editor.error}
span={flow.span}
/>
diff --git a/apps/web/src/features/projects/project-description.tsx b/apps/web/src/features/projects/project-description.tsx
index 2d0b866266..3fcd1a5d38 100644
--- a/apps/web/src/features/projects/project-description.tsx
+++ b/apps/web/src/features/projects/project-description.tsx
@@ -19,7 +19,6 @@ function DescriptionSession(props: {
sourceId={props.projectId}
session={session}
canEdit={() => props.canEdit}
- canComment={() => false}
label="Project description"
namespace="project-description"
class="min-h-24 mt-1.5 text-base"
diff --git a/apps/web/src/lib/core/collab-surface/CollabMarkdownEditor.tsx b/apps/web/src/lib/core/collab-surface/CollabMarkdownEditor.tsx
index 2cd5722750..25db524831 100644
--- a/apps/web/src/lib/core/collab-surface/CollabMarkdownEditor.tsx
+++ b/apps/web/src/lib/core/collab-surface/CollabMarkdownEditor.tsx
@@ -68,8 +68,6 @@ export type CollabMarkdownEditorProps = {
* merely gets its updates rejected. Defaults to editable.
*/
canEdit?: Accessor<boolean>;
- /** Whether the caller may comment. Defaults to false. */
- canComment?: Accessor<boolean>;
/** Lexical namespace, for devtools disambiguation. */
namespace?: string;
/** Class applied to the outer container. */
@@ -100,7 +98,6 @@ false && fileFolderDrop;
*/
export function CollabMarkdownEditor(props: CollabMarkdownEditorProps) {
const canEdit = props.canEdit ?? (() => true);
- const canComment = props.canComment ?? (() => false);
const session = props.session;
@@ -275,7 +272,6 @@ export function CollabMarkdownEditor(props: CollabMarkdownEditorProps) {
syncSource={session.syncSource}
sourceReady={() => true}
canEdit={canEdit}
- canComment={canComment}
editorError={editorError}
statusChrome={props.statusChrome}
/>
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/CollabProvider.tsx b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/CollabProvider.tsx
index 1701a570b4..db6531bfc6 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/CollabProvider.tsx
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/CollabProvider.tsx
@@ -97,7 +97,6 @@ export type CollabProviderProps = {
*/
sourceReady: Accessor<boolean>;
canEdit: Accessor<boolean | undefined>;
- canComment: Accessor<boolean | undefined>;
/** External error state; the engine stops when it becomes non-null. */
editorError: Accessor<MarkdownEditorErrors | null>;
/** Caller-owned load context; the provider never ends it. */
@@ -138,7 +137,11 @@ export function CollabProvider(props: CollabProviderProps) {
}
);
- const readOnly = () => !(props.canEdit() || props.canComment());
+ // A session that cannot edit writes nothing to the shared document. The
+ // server refuses its updates, even state the editor derives on its own, and
+ // a refused update would hold the WAL, and every comment flushed behind it,
+ // forever. Comment marks are published from a separate peer instead.
+ const readOnly = () => !props.canEdit();
const walSyncer = createWALSyncSource(syncSource()!);
const syncEngine = createSyncEngine({
@@ -156,6 +159,9 @@ export function CollabProvider(props: CollabProviderProps) {
syncSource()!.documentId
),
});
+ const writeStateToLoro: typeof syncEngine.syncStateToLoro = async (state) => {
+ if (!readOnly()) await syncEngine.syncStateToLoro(state);
+ };
onCleanup(
props.editor.registerCommand(
@@ -168,7 +174,7 @@ export function CollabProvider(props: CollabProviderProps) {
complete(false);
return;
}
- await syncEngine.syncStateToLoro({ ...state });
+ await writeStateToLoro({ ...state });
await walSyncer.flush();
complete((await walSyncer.summary()).dirty === 0);
} catch (error) {
@@ -392,7 +398,7 @@ export function CollabProvider(props: CollabProviderProps) {
tag: CODE_HIGHLIGHT_IDS_TAG,
onUpdate: async () => {
const stateToSync = loroSyncState(props.editor.getEditorState());
- await syncEngine.syncStateToLoro(stateToSync as any);
+ await writeStateToLoro(stateToSync as any);
$afterSyncCursorUpdate(loroManager);
},
}
@@ -445,7 +451,7 @@ export function CollabProvider(props: CollabProviderProps) {
// Clean the state to remove any state properties that should not be synced
const stateToSync = loroSyncState(state);
- await syncEngine.syncStateToLoro(stateToSync as any);
+ await writeStateToLoro(stateToSync as any);
}
$afterSyncCursorUpdate(loroManager);
@@ -469,7 +475,7 @@ export function CollabProvider(props: CollabProviderProps) {
FORCE_SYNC_COMMAND,
() => {
const stateToSync = loroSyncState(props.editor.getEditorState());
- syncEngine.syncStateToLoro(stateToSync as any).catch(reportSyncError);
+ writeStateToLoro(stateToSync as any).catch(reportSyncError);
return true;
},
COMMAND_PRIORITY_NORMAL
@@ -578,7 +584,7 @@ export function CollabProvider(props: CollabProviderProps) {
// Start the sync engine
startSync();
// Remove drafts that earlier sessions left in the shared document.
- if (hasAbandonedDrafts && !readOnly()) {
+ if (hasAbandonedDrafts) {
props.editor.dispatchCommand(FORCE_SYNC_COMMAND, undefined);
}
props.setEditorReady(true);
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.tsx b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.tsx
index fa82d2b77b..33e3c32b66 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.tsx
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/browser-test/large-undo.tsx
@@ -241,7 +241,6 @@ function Editor(props: {
syncSource={props.syncSource}
sourceReady={() => true}
canEdit={() => true}
- canComment={() => false}
editorError={editorError}
/>
</div>
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/fuzz/fixture.tsx b/apps/web/src/lib/core/component/LexicalMarkdown/fuzz/fixture.tsx
index d5ee0a08db..48dbd5a1b8 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/fuzz/fixture.tsx
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/fuzz/fixture.tsx
@@ -496,7 +496,6 @@ function FuzzEditor(props: {
syncSource={props.syncSource}
sourceReady={() => true}
canEdit={() => true}
- canComment={() => false}
editorError={editorError}
/>
)}
diff --git a/docs/agent-guide/documents.md b/docs/agent-guide/documents.md
index 1492d8f3d4..19602c3337 100644
--- a/docs/agent-guide/documents.md
+++ b/docs/agent-guide/documents.md
@@ -2755,6 +2755,9 @@ survives, and cancelling lets another comment open without changing document tex
Document typing briefly pauses during anchor publication so pending text is
flushed before the highlight is saved. Repeat posting and starting another comment
with both edit and comment-only access; creating a document tests the owner path.
+For comment-only access, use a document no editor has opened since it was
+created: a commenter's session writes nothing to the document, so its comments
+still post.
Also withhold a collaborator's update before posting: catch up its missing history
before publishing the highlight, preserving both the remote text and the comment.
Confirm the posted comment appears in a separate browser, since reopening the