Skip to content

Commit 55823dd

Browse files
ksepehrPorcupineclaude
authored
feat(cli): guard canvas set with --expected (#9945)
## Summary `buzz canvas set` replaced the canvas with no version check, so two writers doing read → edit → write could silently drop each other's edits. It now takes an optional `--expected <rev|none>`. The write lands only if `<rev>` is still the head, or with `none` only if the channel has no canvas. A moved head, or a write that loses the race at the relay, exits 5 (conflict) without changing the canvas. The submit, conflict reconciliation and post-write survival check are now shared with `restore` as `submit_checked_canvas_write`. One behavior change for `restore`: a lost race now exits 5 instead of passing the relay's 409 through as exit 2. Without `--expected`, `set` is still an unconditional replace, so existing scripts keep working. The desktop agent tool got the same guard in block/buzz-app#4365. ### Related issue None found. ### Testing - `cargo test -p buzz-cli canvas` (23 tests) - Ran on a live relay, 10 parallel writers with the same `--expected <head>` for 5 rounds. Each round had exactly one exit 0 and nine exit 5, and history grew by exactly one revision per round. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Sepehr Karimi <sepehr@squareup.com> Co-authored-by: Porcupine <2306086b18f2ceee6760d2725859c7e03ce63a19299623a99dfab5ba2ac49938@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ddc3585 commit 55823dd

5 files changed

Lines changed: 233 additions & 49 deletions

File tree

‎crates/buzz-cli/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ buzz messages vote --event <event-id> --direction up
147147
# Canvas
148148
buzz canvas get --channel <uuid>
149149
buzz canvas set --channel <uuid> --content "# Welcome"
150+
# Replace only if <rev> is still the head (from `canvas history --limit 1`);
151+
# a moved head exits 5. `--expected none` creates only when no canvas exists.
152+
buzz canvas set --channel <uuid> --content - --expected <rev>
150153

151154
# Sidebar sections (your own sidebar, synced with Buzz desktop)
152155
buzz sidebar sections create --name "Engineering" --icon "🛠"

‎crates/buzz-cli/TESTING.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ echo "# Canvas from stdin" | buzz canvas set --channel "$CHANNEL_ID" --content -
194194
# canvas get
195195
buzz canvas get --channel "$CHANNEL_ID"
196196
# Expected: raw markdown string, or: null
197+
198+
# canvas set guarded by the head it read
199+
HEAD=$(buzz canvas history --channel "$CHANNEL_ID" --limit 1 | jq -r '.[0].event_id')
200+
buzz canvas set --channel "$CHANNEL_ID" --content "# Guarded" --expected "$HEAD" | jq .
201+
# Expected: {"event_id":"...","accepted":true,...}
202+
buzz canvas set --channel "$CHANNEL_ID" --content "# Stale" --expected "$HEAD"; echo "exit $?"
203+
# Expected: {"error":"conflict",...} on stderr, exit 5, canvas unchanged
197204
```
198205

199206
### 6.3 Messages
@@ -599,7 +606,7 @@ buzz channels delete --channel "$FORUM_ID" | jq .
599606
| 21 | `channels add-member` | ☐ | Needs admin:channels |
600607
| 22 | `channels remove-member` | ☐ | Needs admin:channels |
601608
| 23 | `canvas get` | ☐ | |
602-
| 24 | `canvas set` | ☐ | Direct and stdin |
609+
| 24 | `canvas set` | ☐ | Direct, stdin and `--expected` |
603610
| 25 | `reactions add` | ☐ | |
604611
| 26 | `reactions remove` | ☐ | |
605612
| 27 | `reactions get` | ☐ | |

‎crates/buzz-cli/src/client.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ fn resp_was_success(status: u16) -> bool {
216216
(200..300).contains(&status)
217217
}
218218

219+
/// A relay canvas CAS rejection (409 `conflict: canvas …`): the head moved,
220+
/// the expected head was deleted, or the write would not supersede the head.
221+
/// The canvas commands settle these by reading the canvas back.
222+
pub(crate) fn is_canvas_conflict(e: &CliError) -> bool {
223+
matches!(e, CliError::Relay { status: 409, body } if body.contains("conflict: canvas"))
224+
}
225+
219226
/// Returns `true` if a stored-event exhaustion error is ambiguous (the relay
220227
/// may have executed the command) and should be converted to `DeliveryUnknown`.
221228
///
@@ -1361,7 +1368,7 @@ impl BuzzClient {
13611368
// Once an attempt may have been stored, no later answer settles
13621369
// the outcome, except a canvas 409, which the canvas command
13631370
// resolves by looking the canvas up.
1364-
let settled = matches!(e, CliError::Relay { status: 409, body } if body.contains("canvas changed"));
1371+
let settled = is_canvas_conflict(e);
13651372
if is_stored_event_exhaustion_ambiguous(e)
13661373
|| (maybe_delivered.load(std::sync::atomic::Ordering::Relaxed) && !settled)
13671374
{

0 commit comments

Comments
 (0)