Skip to content

AI Mode search: three-word rule, on by default, under-field hint, a settled field, and a follow-up that sends nothing - #524

Merged
neuromechanist merged 24 commits into
stagingfrom
feature/ai-mode-three-words
Nov 11, 2026
Merged

neuromechanist merged 24 commits into
stagingfrom
feature/ai-mode-three-words

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What

The website side of the AI Mode search from #518 and #519: the three-word rule (maintainer's rule, 2026-11-11), and, on top of it, the four changes from the maintainer's test of AI Mode on test.nemar.org (#525).

Three-word rule (this PR's first commits)

  • An AI overview needs a search of at least three words.
    countQueryWords in src/lib/osa-overview.ts counts the way OSA counts (split the trimmed query on whitespace, count tokens with a letter or digit, Unicode-aware); OVERVIEW_MIN_WORDS = 3.
  • Under three words Discover sends no request to the overview endpoint, and a 422 from the endpoint is treated the same way.
    A one-word search such as "EEG" made OSA's overview endpoint fail (Search overview: a one-word query ('EEG') returns 502 on dev, with Cloudflare's body OpenScience-Collective/osa#728).
  • Home's "Try" examples all have three or more words, and a test reads them from the source.

#525

  1. On by default.
    No stored choice and no ai parameter is on; a stored off is off and survives a reload; ai=1 forces on; ai=0 forces off and is written back (store and address), so a copied link is honest.
    Storage is in try/catch; blocked storage is on.
    ai in src/lib/filters.ts now means on unless ai=0, and only the off is serialised, so ai=0 survives the filter and paging links.
    The page is cached for everyone, so the server renders the on state and the client switches off.
    Discover's hidden ai input is now ai=0 when the address is off, so a search submitted without scripts does not silently flip the state.
    How a cached no-script page behaves is in ADR 0032: it shows the on look and none of its controls, and what it submits means on.
  2. One very small line under the field, only while on and the field holds one or two words: "AI Mode is on. Use three or more words to see an AI response."
    aiHintShown(on, query, refused) in src/lib/osa-ai-mode.ts decides it from the live field value (input event), and from the submitted query on Discover, which the field holds at load; a 422 shows it too.
    Smallest helper size the site has (--fs-xs), muted site tokens (--color-fg-muted on Discover, --ai-try-label on Home, both with dark values), no error colour.
    One polite live region announces it once per appearance: its text is set on the transition only (aiHintAnnouncement), not per keystroke.
    The panel shows nothing in that state: OVERVIEW_MIN_WORDS_NOTE and the panel's note are gone, the no-request rule, countQueryWords and the 422 path stay.
  3. The field does not move.
    The Try row, the disclosure and the new line are slots the server renders whenever aiEndpoint is set, always laid out and drawn with visibility (the text is in the page, so the reserved height is exact at any width); no hidden toggling is left on them.
    Measured in a browser, the field was not moving on Home but the content under it jumped 147 px, and Discover's field was 47 px tall with the ring and 46 px without; Discover's field now has a set height (44 px, input 41 px inside the 1.5 px ring).
    Tokens reused, no new colours.
  4. "Ask a follow-up" sends nothing.
    It calls OSAChatWidget.seed(text, {mode: "center"}) only; no call to ask or open on that path (a source guard asserts it).
    The button is drawn only where seed is a function, checked each time the panel is drawn; a throw from seed shows the existing failure note.
    followUpText and FOLLOW_UP_MAX_CHARS are replaced by the pure seedText(query, answer, origin): first line **AI overview** for "<query>" (the query's Markdown characters and newlines turned to spaces), the summary with each dataset id as [id](<origin>/dataset/<id>) built with overviewDatasetHref, the picks list when the panel shows picks, at most 2000 characters (UTF-16 code units, as the widget counts), shortened by dropping the picks' reasons, then the picks, then cutting the summary at a sentence.
    The overview's prose cannot make a link, image or emphasis of its own either, since the widget renders the text as Markdown and the overview is a model's text about dataset descriptions.
    Nothing resembling "I searched NEMAR for" remains in src/ (a test checks).

Docs: CHANGELOG entry, ADR 0032 updated (default on, the cached no-script page, reserved slots, the seeded follow-up, the 2000 limit), earlier unreleased changelog lines corrected to the new ai semantics.

Closes #525

Deviations and notes

  • Limit. The issue text says 4,000 characters (the first brief said 8,000), but OSA#730 bounds seed at RESEED_TURN_MAX_CHARS = 2000 and refuses a longer text, so the message is built to at most 2000; if OSA raises its bound nothing here breaks.
  • Absent ai is on, and the page never writes ai=1. An on state carries no parameter; only the off is written (ai=0), so links and the edge cache stay as they were for readers who never chose.
    ai=1 still forces on.
  • The e2e check is a local script. CI would have to install a browser and reach api.nemar.org from the dev server, so bun run test:e2e is documented in playwright.config.ts and not wired to CI.
    It needs bunx playwright install chromium once, and E2E_BASE_URL=https://test.nemar.org points it at a deployed site.
  • End-to-end check pending an OSA deploy. seed reaches test.nemar.org only after Widget: OSAChatWidget.seed (open with a reply, send nothing); smooth open and dock; bottom-right Move to corner icon OpenScience-Collective/osa#730 is deployed to develop, so until then the button is not drawn there (develop's widget has open but no seed).
    After that deploy, check with an xx0999NN id (for example xx099904), not an nm id: the dialog opens with the overview as the assistant's reply, and nothing is sent until the reader writes.
    The browser check below stands in for the widget with a recording object (open, seed) and verifies the call and the text.
  • The maintainer closes AI Mode search: on by default, a small under-field hint, a field that does not move, and a follow-up that sends nothing #525 by hand after the staging merge; this PR is not merged here.

Tests

  • src/lib/osa-ai-mode.test.ts: readAiModeSearch, initialAiModeSearch (nothing stored, stored on and off, ai=1 over a stored off, ai=0 over a stored on, odd values, blocked and throwing storage), setAiModeSearch, withAiModeParam, aiLinkHref; aiHintShown for 0, 1, 2 and 3 words, off and a 422; aiHintAnnouncement over a typed sequence (one announcement per appearance); seedFollowUp and aiModeSeedAvailable with a recording stand-in for the widget host object, with and without seed.
  • src/lib/filters.test.ts: on by default, only ai=0 serialised and kept by paging and filter links, ai=1 and odd values read as on.
  • src/lib/ai-overview-panel.test.ts: the panel shows nothing for a short search or a 422; seedText (exact text, query neutralisation, links, no links from prose, picks, 2000 limit in UTF-16 units, the shortening order, no cut inside a link); the controller's refused state; panelView without a follow-up.
  • test/ai-mode-search.test.ts: source guards (no .ask in osa-ai-mode.ts or the client, no hidden on the slots, visibility rules, --fs-xs, the live region, 44 px field, the on state rendered by the server, nothing like "I searched NEMAR for" in src/).
  • e2e/ai-mode-field.spec.ts (Playwright, 16 tests): on Home and Discover at 1280 px and 390 px, light and dark, the field's top and height and the content under it before and after each click, and with the line shown and hidden, equal within 0.5 px, and scrollWidth <= innerWidth; default on, off remembered across a reload, ai=1 and ai=0, a search from an off page keeps the off; the line for a two-word search with no request made, for a 422, and its one announcement; the follow-up calls seed(text, {mode: "center"}) only and is not drawn without seed.

Commands run (in a worktree on feature/ai-mode-three-words)

  • bun run lint: Checked 417 files, no fixes needed
  • bun run typecheck: 0 errors, 0 warnings, 18 hints (486 files)
  • bun run test: 175 files passed, 3 skipped; 4885 tests passed, 17 skipped, 0 failed
  • bun run build: Complete (public/og-image.png regenerated by the build and reverted)
  • bun run test:e2e: 16 passed (Chromium, against astro dev with PUBLIC_OSA_OVERVIEW_URL set)

Refs #518, OpenScience-Collective/osa#728, OpenScience-Collective/osa#730.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Deploying nemar-website with  Cloudflare Pages  Cloudflare Pages

Latest commit: 6fc730f
Status: ✅  Deploy successful!
Preview URL: https://d2d8d757.nemar-website.pages.dev
Branch Preview URL: https://feature-ai-mode-three-words.nemar-website.pages.dev

View logs

@neuromechanist neuromechanist changed the title AI Mode: an overview needs a search of at least three words AI Mode search: three-word rule, on by default, under-field hint, a settled field, and a follow-up that sends nothing Nov 11, 2026
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review round 1: every finding has an outcome. Pushed to this branch (e2f2458 to 2963608). Tested on a clean install (rm -rf node_modules && bun install --frozen-lockfile): bun run typecheck 0 errors (it reproduced the 3 process errors before the fix), bun run lint clean, vitest run 4893 passed.

  • critical, playwright.config.ts typecheck (process): fixed. Reproduced on a fresh install, then added @types/node (^22) as a devDependency with bun.lock. e2f2458.
  • important, seed text lets prose, pick reasons and the search make links: fixed. neutralise now breaks http:// and https:// (case-insensitive) after blanking Markdown characters, for the overview's prose, pick reasons and the quoted search alike (https://x reads https x), so the dataset links are the only links. ADR 0032 and the changelog say so. 0f8ed94.
  • important, test could not fail for the bug: fixed. The prose test now covers [x](url), ![i](url), <url>, bare http://, uppercase HTTPS://, a pick reason and a typed URL query, and asserts that the only https?:// matches (the widget's own rule) are the page's /dataset/a1 links. Run against the old code it fails. The head test no longer expects the URL to survive.
  • suggestion, surrogate pair cut in the quoted search: fixed. The cut backs up one unit before a high surrogate; new test with a long astral query (fails on the old code).
  • suggestion, stale comment and ADR on open vs seed: fixed. The aiModeSearchAvailable doc and ADR 0032 now say the toggle needs open plus the endpoint and the follow-up button independently needs seed. 70da084 and 0f8ed94.
  • suggestion, UnderFieldHint untested DOM glue: fixed. The state machine is extracted as AiHintState in osa-ai-mode.ts (onInput, setOn, refuse, each returning {shown, announcement}) with unit tests, including that typing and a switch off and on clear a refusal. The class now only draws the result; the source guard in test/ai-mode-search.test.ts was updated. Playwright is still local-only, which is deliberate (browser install and api.nemar.org from CI).
  • suggestion, error: 'invalid' representable: fixed. The state is typed Exclude<OverviewErrorKind, "invalid">, and the over-long controller comment is re-wrapped.
  • suggestion, lint skips e2e/ and playwright.config.ts: fixed. lint is biome check src e2e playwright.config.ts; it is clean. a635a19.
  • suggestion, hero hint 12px vs the 13px note: fixed in favour of consistency. .hero__ai-note now uses --fs-xs, as on Discover. Not checked in a browser; say if you would rather the hint grow instead.
  • suggestion, follow-up failure note not announced: fixed. An empty role="status" element (data-ai-follow-up-failed) is drawn with the foot when the button is, and its text is set on failure and cleared on success; it takes no room while empty. Not exercised in a browser.

Nothing rejected or filed; all of it is code this PR touched. CI should now run test and build, since typecheck no longer fails.

… from the summary; changelog narrowed to link, image, emphasis, code (tested: unit suite)
… failure note; a late 422 is ignored once the field was edited (tested: unit suite, typecheck)
… open AI Mode (data-ai-unavailable); ADR 0032 notes it (tested: unit suite, typecheck)
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review round 2: outcomes (commits c870b56, fb675c0, 46453b0; unit suite and typecheck pass).

  • seedFollowUp returns true before an async seed settles (important): fixed. seedFollowUp is now async and resolves false on a throw or a rejection; OverviewPanel.followUp awaits it before setting or clearing [data-ai-follow-up-failed]. The "rejects" test now expects false.
  • PROSE_MARKDOWN leaves headings, quotes, lists and _/~ emphasis (important): fixed. _ and ~ are blanked in prose, and a leading #, >, +, - or N./N) run is dropped from the start of the summary. A pick's reason follows N. [id]: on the same line, where a block marker opens nothing, so only the blanking applies there. The CHANGELOG and ADR 0032 wording now names link, image, emphasis and code, and a test pins both.
  • Late 422 after the reader edits the field (suggestion): fixed. AiHintState.refuse(requested, current) ignores the refusal when the sent query (whitespace-normalised) no longer equals the field; the client passes request.query. Test added for edit then late refusal.
  • Blank rows when the widget cannot open AI Mode (suggestion): fixed. In the ready.length === 0 branch the hint, Try and disclosure slots get data-ai-unavailable and CSS removes them from the layout on Home and Discover; the field does not move since no toggle exists. ADR 0032 and a source-guard test updated.

No findings rejected or filed.

@neuromechanist
neuromechanist merged commit 77edd8b into staging Nov 11, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant