Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion server/agent-edit-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,22 @@ export async function applyAgentEditV2(
const usingAuthoritativeFallback = authoritativeBase.base.source === 'live_yjs'
|| authoritativeMarkdown !== stripEphemeralCollabSpans(doc.markdown ?? '');
if (usingAuthoritativeFallback) {
const persistedBase = parseMarkdownWithHtmlFallback(parser, stripEphemeralCollabSpans(doc.markdown ?? ''));
// The b1..bN refs the agent sent were assigned against the base it declared. For a
// baseRevision-only edit that base is the persisted document row at that revision.
// doc.markdown here is the live-fallback canonical read, so using it would compare the
// live authoritative fragment against itself and never detect that a concurrent edit
// shifted block topology under the agent's refs. Use the persisted row as the drift
// baseline. When a baseToken is supplied the agent opted into the live base directly
// (validated above), so that base is already the correct comparison frame.
const persistedDriftBaseline = baseToken ? null : getDocumentBySlug(slug);
const driftBaselineMarkdown = baseToken
? stripEphemeralCollabSpans(doc.markdown ?? '')
: stripEphemeralCollabSpans(
persistedDriftBaseline?.revision === baseRevision
? persistedDriftBaseline.markdown
: doc.markdown ?? '',
);
const persistedBase = parseMarkdownWithHtmlFallback(parser, driftBaselineMarkdown);
if (!persistedBase.doc) {
return {
status: 500,
Expand Down