Skip to content

Commit e5394c6

Browse files
max-sixtyclaude
andauthored
Empty a box in the turn its words go on the page (#787)
Adding an option to an Ask left the reader's words on screen twice: once as the option the press had just drawn, and once still sitting in the add field, until the log answered and cleared it. (There is only one add form — the new option is inserted above it, so the form looks like a second box that appeared below.) The two senders in the draft store were split on whether the log alone could supply the gesture's result. That distinction had stopped being true: every dispatch paints its projected outcome synchronously before returning the delivery promise, so both senders draw their result before they are answered. `lf-draft` covered for it by closing its editor first, because the open editor also defers projection; the add field had nothing covering for it. It also broke the rule in `skills/leaf/assets/CLAUDE.md` that where the page can produce the result itself, it produces it in the gesture — the page produced it and left the before-state beside it. So the mask `sendMessage` kept for itself moves into `standGesture`, which both senders now stage through: the generation is masked in this document at the gesture, the record stands in the store until the log settles it, and a refusal withdraws the result and gives the words back. That also stops a pick made mid-send from stranding the option it was sending. Each pick rewrites the add field's draft, because the choice that draft would submit has changed, so one made while the send was open minted a new attempt over the generation that send owned; its answer then settled nothing and the words stayed in the box for good. Two tests in `tests/test_render_options_addition.py` cover the hand-over and the mid-send pick, and both fail against the reverted fix. Alongside the suite I drove the three delivery outcomes in a real browser on a throttled POST: acceptance, a final 400 (option withdrawn, words returned), and a transient failure the outbox retries (mask holds, then settles). > _This was written by Claude Code on behalf of max-sixty_ Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
1 parent c9a5f20 commit e5394c6

3 files changed

Lines changed: 142 additions & 36 deletions

File tree

‎skills/leaf/assets/CLAUDE.md‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,13 @@ a state the reader must return to or act on, such as failure.
727727

728728
Where the page can produce that result itself, it produces it in the gesture. A
729729
suggestion decision paints its projected outcome; a comment or reply paints the message
730-
and opens its thread. The application overlays pending work on the log and restores
731-
authoritative state on refusal. The content and its Undo control are the confirmation,
730+
and opens its thread. Drawing the result moves the words, so the box they were written
731+
in reads empty in that same turn. The reader's answer stands on screen once, rather than
732+
beside a copy of itself still waiting to be sent. The generation is unsettled until the
733+
log answers, and a refusal returns it to the box. The runtime's send owns both halves
734+
(`standGesture`), so no box empties or refills itself.
735+
The application overlays pending work on the log and restores authoritative state on
736+
refusal. The content and its Undo control are the confirmation,
732737
so neither path needs a success notice; announce the same outcome for a reader listening
733738
to the page. For a message that announcement is `post`'s, made where the gesture is first
734739
known to be a message, so a box that sends one adds no second announcement. A box whose

‎skills/leaf/assets/runtime/drafts.js‎

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
distinguish them, because deletion can fail independently and could otherwise
3737
resurrect old words.
3838
39+
Every send draws its result in this document before the log answers. So the send
40+
masks its generation here at the gesture (`standGesture`) and the box the words were
41+
typed in reads empty, while the record stands unsettled until the answer accepts it
42+
or a refusal gives the words back. The mask is this document's; the record is every
43+
tab's.
44+
3945
`base` records the durable shared generation a local edit descends from. A chain of
4046
nondurable local writes keeps that base. Storage news from the base cannot erase the
4147
branch; an unrelated later shared generation owns the context and retires it. Before
@@ -189,18 +195,18 @@ const refreshDraftRecord = (ctx) => {
189195
if (changed) projectDraftRecord(ctx, shared);
190196
return shared;
191197
};
192-
// A generation this document has sent as a message and not heard back on. Its words are
193-
// standing in the conversation, so the box they were written in reads empty — while the
194-
// record itself stands, unsettled, until the log accepts it. The two are different
195-
// facts: what a composer shows is about this document, and settlement is a claim over
196-
// every tab. That is what lets a second tab still show and send the same generation, and
197-
// what brings the words back here on a refusal with nothing to restore them from.
198-
const standingMessages = new Map(); // ctx -> attempt
198+
// A generation this document has sent and not heard back on. Its words are standing
199+
// wherever the gesture drew them, so the box they were written in reads empty, while
200+
// the record itself stands, unsettled, until the log accepts it. The two are different
201+
// facts: what a box shows is about this document, and settlement is a claim over every
202+
// tab. That is what lets a second tab still show and send the same generation, and what
203+
// brings the words back here on a refusal with nothing to restore them from.
204+
const standingGestures = new Map(); // ctx -> attempt
199205

200206
const activeDraftRecord = (ctx) => {
201207
const record = rawDraftRecord(ctx);
202208
if (!record || record.settled || attemptAccepted(record.attempt)) return null;
203-
return standingMessages.get(ctx) === record.attempt ? null : record;
209+
return standingGestures.get(ctx) === record.attempt ? null : record;
204210
};
205211
// Every tombstone is an ownership claim, whether it follows Send, Cancel, a widget
206212
// action, or a poll that observed the attempt in the log. Re-read shared storage before
@@ -310,47 +316,59 @@ function claimDraft(ctx, owns) {
310316
return current;
311317
}
312318

313-
// A gesture whose result only the log can supply: a widget edit, an added option. The
314-
// draft stands until the answer accepts it, so a refusal leaves the reader's text exactly
315-
// where it was, and the caller waits on the answer with `aria-busy` on its own control.
319+
// Both senders below stage the same way, because both draw their result in the document
320+
// before the log has answered: `post` paints the message in its thread, and a dispatched
321+
// action paints its projected outcome. So the box stops showing words the reader can now
322+
// see standing, while the record keeps them — one copy of the words on screen, and a
323+
// generation still owed an answer. Acceptance settles exactly that generation. A refusal
324+
// withdraws the result and lifts the mask, so the box the reader typed in has the words
325+
// again; the record stood in the store throughout, which is what a tab that went down
326+
// with the send recovers from.
327+
const standGesture = (ctx, current) => {
328+
standingGestures.set(ctx, current.attempt);
329+
tellDraft(ctx, null);
330+
return (sent) => {
331+
// Lift this generation's mask, not whatever is standing for the context: a second
332+
// send into the box this one emptied has masked its own words by the time this
333+
// answer arrives, and they are on screen as a gesture of their own.
334+
if (standingGestures.get(ctx) === current.attempt) standingGestures.delete(ctx);
335+
if (sent) {
336+
if (settleDraft(ctx, current.attempt)) tellDraft(ctx, null);
337+
return;
338+
}
339+
// A later edit of their own has a fresh attempt and is not this generation, and
340+
// keeps the box.
341+
if (activeDraftRecord(ctx)?.attempt !== current.attempt) return;
342+
tellDraft(ctx, current.text);
343+
};
344+
};
345+
346+
// An action the sender waits on: a widget edit, an added option. Its dispatch paints the
347+
// projected outcome before the delivery it returns resolves, so the words are already
348+
// standing in the document by the time this awaits them. The caller's own control shows
349+
// the wait.
316350
export async function sendDraft(ctx, owns, send) {
317351
const current = claimDraft(ctx, owns);
318352
if (!current) return null;
319-
const sent = await send(current.attempt, current.payload);
320-
if (sent && settleDraft(ctx, current.attempt)) tellDraft(ctx, null);
353+
const flight = send(current.attempt, current.payload);
354+
const answer = standGesture(ctx, current);
355+
const sent = await flight;
356+
answer(sent);
321357
return sent;
322358
}
323359

324360
// A message, whose result is the words themselves. `post` stages and paints it before it
325361
// returns, so the caller continues against a thread the reader can already see and the
326-
// round trip happens behind them. The generation settles on acceptance, exactly as
327-
// above; what happens in the gesture is that this document stops showing words it is now
328-
// showing in the thread. A refusal lifts that and the box has them again.
362+
// round trip happens behind them.
329363
//
330364
// The returned handle names the message the send drew. It is what a caller opens or
331365
// focuses, and the log's answer renames that same node rather than replacing it.
332366
export function sendMessage(ctx, owns, send) {
333367
const current = claimDraft(ctx, owns);
334368
if (!current) return null;
335369
const flight = send(current.attempt, current.payload);
336-
standingMessages.set(ctx, current.attempt);
337-
tellDraft(ctx, null);
338-
void Promise.resolve(flight).then((sent) => {
339-
// Lift this generation's mask, not whatever is standing for the context: a second
340-
// send into the box this one emptied has masked its own words by the time this
341-
// answer arrives, and they are on screen as a pending message of their own.
342-
if (standingMessages.get(ctx) === current.attempt) standingMessages.delete(ctx);
343-
if (sent) {
344-
if (settleDraft(ctx, current.attempt)) tellDraft(ctx, null);
345-
return;
346-
}
347-
// Refused, so the words are the reader's again — written back to the store rather
348-
// than only told to the boxes, since the one they were typed in may have gone down
349-
// with the send and a reload must still find them. A later edit of their own has a
350-
// fresh attempt and is not this generation, and keeps the box.
351-
if (activeDraftRecord(ctx)?.attempt !== current.attempt) return;
352-
tellDraft(ctx, current.text);
353-
});
370+
const answer = standGesture(ctx, current);
371+
void Promise.resolve(flight).then(answer);
354372
return { attempt: current.attempt, id: `${PENDING}${current.attempt}` };
355373
}
356374

‎tests/test_render_options_addition.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
)
1717
from render_harness import (
1818
EXAMPLE_MEDIA,
19+
STORED_DRAFT_TEXT,
20+
consume_browser_errors,
21+
holding,
1922
open_page,
2023
round_trip,
2124
sending,
@@ -254,6 +257,86 @@ def test_another_option_becomes_a_real_option_without_starting_a_thread(browser,
254257
expect(page.locator("#jobs > lf-option[chosen]")).to_have_count(0)
255258

256259

260+
def test_the_add_field_hands_its_words_to_the_option_it_drew(held_events, serve):
261+
"""The reader's answer stands on screen once, as the option the press drew.
262+
263+
The press paints that option before the log has answered, so the words have moved and
264+
the box they came from is empty in that same turn; a reader who saw both would read
265+
their own answer as still unsent. The generation is standing rather than settled, and
266+
the refusal shows the difference. It takes the option away and gives the words back,
267+
out of a record that never left the store.
268+
"""
269+
browser, held = held_events
270+
page = open_page(browser, serve(ASK_PAGE))
271+
form = page.locator("#jobs > .lf-another")
272+
field = form.get_by_role("textbox", name="Another option", exact=True)
273+
words = "Insulate the camera battery"
274+
field.fill(words)
275+
form.get_by_role("button", name="Add option", exact=True).click(no_wait_after=True)
276+
holding(page, held, 1, "the added option")
277+
278+
added = page.locator("#jobs > lf-option[data-lf-added]")
279+
expect(added).to_have_count(1)
280+
expect(added).to_contain_text(words)
281+
expect(field).to_have_value("")
282+
assert page.evaluate(STORED_DRAFT_TEXT, "option:jobs") == words
283+
284+
attempt = held[0].request.post_data_json["attempt"]
285+
with page.expect_response(lambda response: "/api/event" in response.url):
286+
held.pop(0).fulfill(
287+
status=400,
288+
json={
289+
"ok": False,
290+
"attempt": attempt,
291+
"error": "refused before append",
292+
"final": True,
293+
},
294+
)
295+
expect(added).to_have_count(0)
296+
expect(field).to_have_value(words)
297+
assert page.evaluate(STORED_DRAFT_TEXT, "option:jobs") == words
298+
assert not [
299+
event
300+
for event in sent_events(serve.page_dir)
301+
if event.get("kind") == "action" and event.get("widget") == "jobs"
302+
]
303+
consume_browser_errors(page, "400")
304+
305+
306+
def test_a_pick_made_while_an_option_is_in_flight_cannot_strand_it(held_events, serve):
307+
"""A pick made while the send is open leaves the sent generation alone.
308+
309+
Every pick rewrites the add field's draft, because the choice that draft would submit
310+
has changed. A pick made while the added option was in the wire used to write over
311+
the generation that send owned, so its answer settled nothing and the words it had
312+
already carried into an option stayed in the box for good. A sent generation is no
313+
longer the box's to write — the same mask that empties the box keeps the pick out of
314+
it.
315+
"""
316+
browser, held = held_events
317+
page = open_page(browser, serve(ASK_PAGE))
318+
form = page.locator("#jobs > .lf-another")
319+
field = form.get_by_role("textbox", name="Another option", exact=True)
320+
words = "Insulate the camera battery"
321+
field.fill(words)
322+
form.get_by_role("button", name="Add option", exact=True).click(no_wait_after=True)
323+
holding(page, held, 1, "the added option")
324+
325+
# The queue holds this second action behind the first, so the pick's own paint, not
326+
# another held route, is what says the gesture was taken while the send was open.
327+
page.locator("#job-heater").click()
328+
expect(page.locator("#job-heater")).to_have_attribute("chosen", "")
329+
expect(field).to_have_value("")
330+
331+
while held:
332+
held.pop(0).continue_()
333+
page.unroute("**/api/event")
334+
round_trip(page)
335+
expect(field).to_have_value("")
336+
assert page.evaluate(STORED_DRAFT_TEXT, "option:jobs") is None
337+
expect(page.locator("#jobs > lf-option[data-lf-added]")).to_have_count(1)
338+
339+
257340
PASTE_IMAGE = """(textarea, encoded) => {
258341
const bytes = Uint8Array.from(atob(encoded), char => char.charCodeAt(0));
259342
const transfer = new DataTransfer();

0 commit comments

Comments
 (0)