Skip to content

Commit b100a7a

Browse files
author
ART3
committed
Report artifact conflicts as rejected writes
Two writers updating an artifact from the same prev get a definite refusal for the second: the relay answers 409 on /events, or OK false "conflict: ..." on the socket, before any mutation. Both reached the outbox as `unknown`, so a plugin could not tell a competing edit from a lost acknowledgement. A 409 publish response and a socket `conflict:` for kind 45010 are now PublishRejected, and the dev broker answers a proven conflict with 409 like the relay. The outbox entry is `failed` with a fixed `conflict:` error, never raw relay text. Internal errors and unknown prefixes stay `unknown`. Signed-off-by: ART3 <822f566773c6b5ec5de21614f3900e5dd78a36d772fb826a3ee0f4b140d68255@buzz.block.builderlab.xyz>
1 parent 52604d2 commit b100a7a

6 files changed

Lines changed: 91 additions & 4 deletions

File tree

‎dev/relay-broker-live.test.mjs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,41 @@ test.each([
13761376
},
13771377
);
13781378

1379+
test.each([
1380+
["conflict: artifact head changed", "failed", "conflict: the relay state"],
1381+
["error: internal server error", "unknown", "could not be confirmed"],
1382+
])(
1383+
"artifact refusal %s reaches broker/outbox as %s / %s",
1384+
async (reason, delivery, error) => {
1385+
const h = await harness();
1386+
let traffic, owner;
1387+
try {
1388+
browserFetch(h.base);
1389+
const transport = await connectBrokerTransport(h.base);
1390+
traffic = transport.subscribe(callbacks);
1391+
await until(() => h.requests.length >= 1);
1392+
owner = createOutbox(transport.viewer, transport.writer, {
1393+
load: () => [],
1394+
save() {},
1395+
});
1396+
const id = owner.outbox.send({
1397+
kind: 45010,
1398+
content: "",
1399+
tags: [["h", "00000000-0000-4000-8000-000000000001"]],
1400+
});
1401+
await until(() => h.publications.length === 1);
1402+
await h.sockets[0].receive(["OK", id, false, reason]);
1403+
await until(() => owner.outbox.snapshot()[0]?.delivery === delivery);
1404+
expect(owner.outbox.snapshot()[0].error).toContain(error);
1405+
} finally {
1406+
owner?.dispose();
1407+
traffic?.dispose();
1408+
vi.unstubAllGlobals();
1409+
await h.close();
1410+
}
1411+
},
1412+
);
1413+
13791414
test("joined batches survive the real HTTP boundary and retirement only rebuilds the affected scope", async () => {
13801415
const h = await harness();
13811416
let traffic;

‎dev/relay-broker.mjs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2478,7 +2478,11 @@ export function relayBrokerPlugin({
24782478
log.warn(
24792479
`${publication} stage=socket sent=${failure ? failure.sent : "unknown"} reason=${failure?.message ?? "unclassified failure"}${failure?.refusal ? ` refusal=${failure.refusal}` : ""}`,
24802480
);
2481-
return json(res, 503, {
2481+
// Match the relay's HTTP status for a proven CAS refusal.
2482+
const conflict =
2483+
failure?.sent === false &&
2484+
failure.refusal?.startsWith("conflict:");
2485+
return json(res, conflict ? 409 : 503, {
24822486
error:
24832487
failure?.sent === false &&
24842488
failure.refusal?.startsWith("rate-limited:")

‎docs/relay-queries.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,11 @@ text to 16 KiB. Its ten-second deadline includes queued/authentication time.
564564
Disconnect, cancellation or timeout after dispatch, throwing sends, malformed
565565
receipts and internal/unknown negative receipts remain uncertain. Only a proven
566566
unsent operation or documented validation/admission rejection proves non-delivery.
567+
A compare-and-set refusal is proven too: `conflict:` for workflow and NIP-AR
568+
artifact (45010) events, which the dev broker answers as 409 like the relay's
569+
`/events`. Either reaches the outbox as `failed`; an artifact write carries a fixed
570+
`conflict:` error, so its writer reloads and reconciles instead of retrying a
571+
stale revision.
567572
NIP-01 rejection does not guarantee rollback of Buzz command side effects. Accepted
568573
command receipt text stays ephemeral; it is never journaled or replaced by an echo.
569574

‎src/features/relay/signed-admission.test.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { beforeEach, afterEach, assert, expect, it, vi } from "vitest";
22
import { connectSignedTransport } from "./transport";
3-
import { PublishRejected } from "./outbox";
3+
import { createOutbox, PublishRejected } from "./outbox";
44
import { keypair, signed } from "./testing";
55

66
const filters = [{ kinds: [0], limit: 1 }];
@@ -149,3 +149,37 @@ it("explicit quota rejection is retryable; missing response stays unknown with n
149149
await vi.advanceTimersByTimeAsync(500);
150150
expect(fetcher).toHaveBeenCalledTimes(2);
151151
});
152+
it.each([
153+
[
154+
409,
155+
"failed",
156+
"conflict: the relay state changed; reload before writing again",
157+
],
158+
[500, "unknown", "Relay delivery could not be confirmed (500)"],
159+
])(
160+
"artifact write answered %s reaches the outbox as %s",
161+
async (status, delivery, error) => {
162+
const identity = signer();
163+
const transport = await connectSignedTransport(
164+
identity,
165+
"https://artifact.test",
166+
"relay",
167+
);
168+
assert.exists(transport.writer);
169+
vi.stubGlobal(
170+
"fetch",
171+
vi.fn(async () =>
172+
Response.json({ error: "conflict: artifact head changed" }, { status }),
173+
),
174+
);
175+
const owner = createOutbox(transport.viewer, transport.writer, {
176+
load: () => [],
177+
save() {},
178+
});
179+
owner.outbox.send({ kind: 45010, content: "", tags: [["h", "c"]] });
180+
await vi.waitFor(() =>
181+
expect(owner.outbox.snapshot()[0]).toMatchObject({ delivery, error }),
182+
);
183+
owner.dispose();
184+
},
185+
);

‎src/features/relay/socket-requests.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ export function createSocketPublications(wake: () => void) {
107107
/^(invalid|blocked|restricted|auth-required|rate-limited):/.test(
108108
data[3],
109109
) ||
110-
// Buzz workflow CAS/authority refusals precede domain mutation.
110+
// Buzz workflow CAS/authority and artifact CAS refusals precede
111+
// domain mutation.
111112
([30620, 46020].includes(job.event.kind) &&
112-
/^(conflict|forbidden):/.test(data[3]));
113+
/^(conflict|forbidden):/.test(data[3])) ||
114+
(job.event.kind === 45010 && data[3].startsWith("conflict:"));
113115
job.finish(
114116
undefined,
115117
new SocketRequestError(

‎src/features/relay/transport.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,13 @@ async function signedPost(
12281228
/** A transport failure is an unknown outcome; only a definitive rejection is a failed write. */
12291229
export async function acceptPublish(response: Response, id: string) {
12301230
if (!response.ok) {
1231+
// The relay answers 409 only for a compare-and-set refusal before mutation,
1232+
// and the broker relays the socket's equivalent the same way. The fixed
1233+
// prefix tells the writer to reload and reconcile rather than retry.
1234+
if (response.status === 409)
1235+
throw new PublishRejected(
1236+
"conflict: the relay state changed; reload before writing again",
1237+
);
12311238
if ([400, 401, 403, 404, 413, 422].includes(response.status))
12321239
throw new PublishRejected(
12331240
`Relay rejected the message (${response.status})`,

0 commit comments

Comments
 (0)