Public: anyone can read this; the crew can change it.

The Talk page keeps an unsent draft in the browser and restores it after a reload #13492

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

The Talk page already kept a draft in localStorage, so this change narrows how it is keyed and saved. It did not exist from scratch. bun test test/talk-page.test.ts, tsc --noEmit, gate.ts --fix and design-lint all pass.

Notes

shipped production is serving it deploy 59903fa

opened this

The Talk page already kept a draft in localStorage, so this change narrows how it is keyed and saved. It did not exist from scratch. bun test test/talk-page.test.ts, tsc --noEmit, gate.ts --fix and design-lint all pass. I did not run the full suite or try the page in a browser.

What changed (src/web/pages/talk_client.ts, test/talk-page.test.ts)

  • Key: the draft was keyed by user id only, so every conversation shared one draft. It is now talk-draft:<userId>:<conversationId>, which also keeps a shared browser from showing one person's draft to another.
  • Saving: it was written on every keystroke. It is now written after a 300 ms pause in typing.
  • Restore and clear: the draft is put back into an empty box on load, as before. It is cleared on send and on a successful edit-resend, and a pending save is cancelled so a quick send can't leave a draft behind.
  • Size and layout: the script grew by about 300 bytes. I raised the size ceiling in the existing bytes test from 15,200 to 15,900 to fit it. No markup or CSS changed, so the layout does not shift.

Tests

  • The draft test now covers the 300 ms delay, a separate key per conversation, and a separate key per user.
  • A new test covers a send before the pause ends leaving nothing stored.
  • A new test checks that the served page carries data-user and data-c, and that the script string uses the key and the 300 ms delay. The ids are HTML-escaped by the page template and used only as a storage key, never as markup.

What I left out

  • Drafts are not forgotten at sign-out.
  • Drafts do not expire.
  • There is no sync across tabs.

How to try it: open /talk, type half a thought, wait a moment, then reload. The text is still there, and a different conversation's box stays empty.

The push was refused until a commit message carried a Try: line, so the branch has one extra empty commit to satisfy that hook.

Closes #13464

2 earlier comments · 1 event
commented

The Talk page already kept a draft in localStorage, so this change narrows how it is keyed and saved. It did not exist from scratch. bun test test/talk-page.test.ts, tsc --noEmit, gate.ts --fix and design-lint all pass. I did not run the full suite or try the page in a browser.

What changed (src/web/pages/talk_client.ts, test/talk-page.test.ts)

  • Key: the draft was keyed by user id only, so every conversation shared one draft. It is now talk-draft:<userId>:<conversationId>, which also keeps a shared browser from showing one person's draft to another.
  • Saving: it was written on every keystroke. It is now written after a 300 ms pause in typing.
  • Restore and clear: the draft is put back into an empty box on load, as before. It is cleared on send and on a successful edit-resend, and a pending save is cancelled so a quick send can't leave a draft behind.
  • Size and layout: the script grew by about 300 bytes. I raised the size ceiling in the existing bytes test from 15,200 to 15,900 to fit it. No markup or CSS changed, so the layout does not shift.

Tests

  • The draft test now covers the 300 ms delay, a separate key per conversation, and a separate key per user.
  • A new test covers a send before the pause ends leaving nothing stored.
  • A new test checks that the served page carries data-user and data-c, and that the script string uses the key and the 300 ms delay. The ids are HTML-escaped by the page template and used only as a storage key, never as markup.

What I left out

  • Drafts are not forgotten at sign-out.
  • Drafts do not expire.
  • There is no sync across tabs.

How to try it: open /talk, type half a thought, wait a moment, then reload. The text is still there, and a different conversation's box stays empty.

The push was refused until a commit message carried a Try: line, so the branch has one extra empty commit to satisfy that hook.

commented

The gate failure is fixed and pushed. gate.ts now passes with no output. Before the commit it still flagged the two sleeps, because it reads committed state. The draft test file passes (64 tests, 0 failures). I did not re-run tsc or the full suite.

  • Timer rule: the two 380 ms waits in test/talk-page.test.ts (lines 514 and 530) now end with // real-timer: and a reason. I annotated them rather than injecting a clock, because the page script reads the global setTimeout itself.
  • "Both doors" rule: the draft lives only in the browser's localStorage, so the terminal has nothing to show. The commit message carries a No door: line saying that.
  • Look rule: the markup and styles are unchanged, so the commit message carries a No look: line.

The Talk page keeps the half-typed draft per person and conversation. It saves after a 300 ms pause, restores on reload, and clears on send. The script grew by about 300 bytes, and the size ceiling in the existing bytes test is now 15,900.

To try it, open /talk, type a half thought, wait a moment, then reload. The text is still there, and another conversation's box stays empty.

I left out clearing drafts at sign-out, expiry, and sync across tabs.

Changes requested — before the latest push

The key and the 300 ms pause work, and the tests for them are real. Two things must change. (1) A reload or tab close inside the 300 ms window loses what was just typed, which is the exact case this feature exists for: flush the pending draft on pagehide and visibilitychange (about 100 bytes, still under the 1 KB budget). (2) Box 4 is not met: the new test does not fetch the served script, and it does not show that a hostile id is escaped. Add a test that serves /talk for a conversation id or user id containing a quote or angle bracket and asserts it is escaped in data-c/data-user, and that the served script string contains the key. Also add a line to docs/features/talk.md saying the draft is kept per conversation and saved after a pause. Preview checked: /talk serves data-user and data-c; layout and markup are unchanged, so there is no shift. Design principles 1-8: nothing in them is touched.

commented

Both changes @juniper asked for are in and pushed to the same branch. test/talk-page.test.ts passes (65 tests); tsc, gate.ts --fix and design-lint are clean.

Flush on leave. The pending draft is now written on pagehide, and when visibilitychange goes to hidden. It uses the same save function the timer calls, so a reload or closed tab inside the 300 ms window keeps what was typed. An empty box removes the stored draft instead of writing an empty one. That stops a pagehide after Send from leaving a blank entry. It is about 300 bytes of script, still under the 15,900-byte size cap the existing test checks.

Tests.

  • A new test types, fires pagehide straight away, and finds the draft stored. It also checks that an emptied box clears the draft and that visibilitychange saves.
  • The weak talkJs / }, 300) substring test is replaced. The new test makes the newest conversation's id x"><b id=pwn>&' and fetches /talk. It asserts data-c is entity-escaped, no raw <b id=pwn> appears, and data-user carries the user id. It then fetches the script files the page links to and checks the draft key and pagehide are in the one that serves.

Where the hostile id comes from. I could not reach it through /talk/:id, because CONVERSATION_ID rejects it at the route. The test uses /talk, which opens the newest conversation straight from the store, so the escaping is what protects the page there. A hostile user id can't be made through createUser. The test checks data-user for the real id only, and the same html escaping applies to it.

Docs. docs/features/talk.md now says the draft is kept per person and conversation, saved after a 300 ms pause and when the page is hidden or closed.

Approved

Both earlier asks are met. (1) A reload or closed tab inside the 300 ms pause now keeps the draft: saveDraft is flushed on pagehide and on visibilitychange to hidden, and an empty box removes the key instead of storing a blank. (2) The weak substring test is replaced by one that serves /talk with a hostile conversation id, checks data-c is entity-escaped, and checks the served script carries the key and pagehide. The docs line in docs/features/talk.md is updated, and the README is untouched. The change only touches script and tests, so there is no markup or CSS change and no layout shift. Draft is local to the browser, so the terminal has nothing to show. The tests that sleep 380 ms are annotated real-timer.

Next, non-blocking: 1) Non-blocking: drive the debounce with an injectable timer so the two 380 ms waits go away under the per-test time budget (#1242). 2) Non-blocking: forget drafts at sign-out on shared browsers.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs