Skip to content

Commit 602f25e

Browse files
author
Carl
committed
Share definitive Canvas conflict classification
Signed-off-by: Carl <32a2e2c9d428ee08902cab75d956da2c1d235a22d4766b0dd4138bf6e2e5db1d@buzz.block.builderlab.xyz>
1 parent 140b67e commit 602f25e

4 files changed

Lines changed: 50 additions & 16 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect, it } from "vitest";
2+
import type { OutgoingEvent } from "../relay/outbox";
3+
import { keypair, signed } from "../relay/testing";
4+
import { isDefinitiveCanvasConflict } from "./canvas-conflict";
5+
6+
const viewer = keypair();
7+
8+
it.each([
9+
[40100, "failed", "conflict: the relay state changed", true],
10+
[30078, "failed", "conflict: the relay state changed", false],
11+
[40100, "unknown", "conflict: the relay state changed", false],
12+
[40100, "unknown", "Retry blocked: conflict: the relay state changed", false],
13+
[40100, "failed", "Retry blocked: conflict: the relay state changed", false],
14+
[40100, "accepted", "conflict: the relay state changed", false],
15+
[40100, "seen", "conflict: the relay state changed", false],
16+
[40100, "sending", "conflict: the relay state changed", false],
17+
[40100, "failed", "forbidden: membership changed", false],
18+
[40100, "failed", undefined, false],
19+
] as const)(
20+
"classifies kind=%s delivery=%s error=%s as definitive=%s",
21+
(kind, delivery, error, expected) => {
22+
const operation: OutgoingEvent = {
23+
event: signed(viewer, { kind, content: "Draft", tags: [] }),
24+
delivery,
25+
error,
26+
};
27+
expect(isDefinitiveCanvasConflict(operation)).toBe(expected);
28+
},
29+
);
30+
31+
it("does not classify missing delivery evidence as a refusal", () => {
32+
expect(isDefinitiveCanvasConflict(undefined)).toBe(false);
33+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { OutgoingEvent } from "../relay/outbox";
2+
3+
/** A refused retry cannot prove an earlier uncertain publication never landed. */
4+
export function isDefinitiveCanvasConflict(
5+
operation: OutgoingEvent | undefined,
6+
): boolean {
7+
return (
8+
operation?.event.kind === 40100 &&
9+
operation.delivery === "failed" &&
10+
operation.error?.startsWith("conflict:") === true
11+
);
12+
}

‎src/features/channel-templates/capability.ts‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { RelayReader } from "../relay/reader";
22
import type { RelayEvent, ReadFilter } from "../relay/events";
33
import type { Outbox, LocalEvents } from "../relay/outbox";
44
import type { ChannelKitHost } from "./host";
5+
import { isDefinitiveCanvasConflict } from "./canvas-conflict";
56
import {
67
CANVAS_BYTES,
78
coordinate,
@@ -146,12 +147,7 @@ export function createChannelKit({
146147
"This operation could not be confirmed and is too old to replay. Keep your draft and inspect the current result before making a new save.",
147148
);
148149
// A proven Canvas CAS refusal cannot become valid by replaying these bytes.
149-
if (
150-
saved.event.kind === 40100 &&
151-
saved.delivery === "failed" &&
152-
saved.error?.startsWith("conflict:")
153-
)
154-
throw new Error(saved.error);
150+
if (isDefinitiveCanvasConflict(saved)) throw new Error(saved.error);
155151
await delivered(id);
156152
if (
157153
!(await fresh([{ ids: [id], limit: 1, consistency: "strong" }])).some(
@@ -163,12 +159,7 @@ export function createChannelKit({
163159
);
164160
} catch (error) {
165161
const failed = local?.snapshot().find((item) => item.event.id === id);
166-
if (
167-
outbox &&
168-
failed?.event.kind === 40100 &&
169-
failed.delivery === "failed" &&
170-
failed.error?.startsWith("conflict:")
171-
) {
162+
if (outbox && isDefinitiveCanvasConflict(failed)) {
172163
// Refused before mutation. Release the pending gate, not uncertain saves.
173164
await outbox.dismiss(id);
174165
throw new Error(canvasConflict);

‎src/features/channel-templates/setup.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Outbox, LocalEvents } from "../relay/outbox";
22
import type { KitEntry } from "./model";
3+
import { isDefinitiveCanvasConflict } from "./canvas-conflict";
34
export type ChannelSetup = {
45
canvas: string;
56
agents: string[];
@@ -299,10 +300,7 @@ export function createChannelSetup({
299300
const failed = local
300301
.snapshot()
301302
.find((item) => item.event.id === canvas);
302-
if (
303-
failed?.delivery === "failed" &&
304-
failed.error?.startsWith("conflict:")
305-
) {
303+
if (isDefinitiveCanvasConflict(failed)) {
306304
// A refused seed must not fence a later manual Canvas save.
307305
await outbox.dismiss(canvas);
308306
throw new Error(

0 commit comments

Comments
 (0)