mirror of
https://github.com/bitbaum/substrata.git
synced 2026-12-04 07:58:05 +01:00
* fix(chat): the assistant stops answering a question it was never asked
A reader on /bottlenecks/euv-lithography-scanners asked who owns the
company that makes them, whether it is public or private, and what its
history is. The answer was "Not in your data", followed by a report that
"the web passage provided" discussed the difference between public and
private limited companies. Four separate faults produced that.
1. The web passage was never shown. `answerQuestion` returns `web`, and
the component has a block that renders it under its own heading — but
the stream handler destructured only `answer` and `sources`, so the
block was dead code and had been since it was written. The reader saw
an answer citing evidence the screen was hiding. Its classes had never
been exercised either: `border-strong` resolves to nothing, since the
token is `--color-border-strong`. It is a `companion-web` class now,
where this repo keeps visual decisions.
2. The lookup searched the question verbatim. "who owns this company is
it a public company..." has no subject in it. `lib/chat-query.ts` puts
the subject back: the page the reader is standing on, or the first
company among the retrieved rows when the question is about a company
and the page is a material. A question that already names something on
screen is left alone.
3. The refusal detector was `/not in your data/i` against one exact
phrase, so a model that wrote "the records do not cover" — which the
prompt invites — never triggered the lookup that exists for it.
4. There was no third register. "Is it listed?" is not a research
question, and a corpus is not supposed to hold it. The ladder now ends
in BACKGROUND: fenced to stable public facts, never a figure, a
capacity, a price or a supplier relationship, never cited, opened with
"Outside the corpus —", and marked on screen. Grounding verification
is skipped on that path, because its repair would reinstate the wall;
the fact-check route opts out of the whole ladder, since defending an
article with material the records do not hold is the one thing it can
never do.
Follow-ups were "Open the evidence for X" — not questions, and never
sent to the browser either. They are now questions this assistant can
answer, and they are buttons.
And the composer was five controls in a wrapping row: a native select at
full width, two square bordered boxes, a rounded filled button and a
borderless one — four visual weights, two ragged lines on a phone, with
the model picker the loudest thing in it and the contribution form (a
different action entirely) sitting inline with Ask. The textarea and the
tool bar now share one border and read as one surface; every quiet
control is the same height and weight; Ask is the only filled thing;
sending evidence sits below on its own. Checked at 320px, in both themes,
and with a long model id — the picker truncates rather than clipping the
labels beside it.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NkjZ1jJPhHzxAPvbSSG3wH
* fix(portal): the header is a menu again, at every width
The README has said "three menu groups and one action, because seven flat
items was more than a reader could hold" since components/portal/Megamenu.tsx
existed. That file was deleted in 7f59e09 and nothing noticed. What shipped
in its place was a flat list of nine uppercase links, and it was broken in a
different way at every width:
- 1440px: `.public-nav` had `flex-wrap: wrap`, so the nine links wrapped onto
two rows inside the header and made it twice as tall as the wordmark.
- 768–1099px: `.public-nav` was `display: none` until 1100px, so an iPad got
a lone letter-spaced "MENU" adrift beside the wordmark and an otherwise
empty bar.
- 390px: the panel was `position: absolute; right: 0` on a <details> sitting
mid-header, so a 22rem panel began 22rem to the LEFT of a button nowhere
near the right edge. The search field and every link label rendered off the
left side of the screen. That is the whole mobile menu, unusable.
The grouping is back, and this time it lives in `config/site-nav.ts` rather
than in a component that can be deleted quietly: every link is declared once
in `LINK`, and the header groups, the research list, the desk sidebar and the
footer are all built from those objects. Each destination carries one line
taken from that page's own description, so the menu says what is behind a
noun without adding a second sentence to keep true.
Wide (≥820px): three group dropdowns plus Join, one row, never wrapping, the
group you are inside marked under its own label. Narrow: one panel the full
width of the shell, anchored to the header's container — groups stacked,
search at the top, current page ruled down its left edge. Both are <details>,
so neither needs JavaScript to open.
Two things moved to make 820px fit: the inline search form now appears at lg
rather than md (below that it was competing with the nav for the same row,
which is why the nav was hidden to 1100px in the first place), and the menu
button moved from beside the wordmark to the end of the right-hand cluster,
where a thumb reaches. The header also moves to z-[60] so the open panel is
not painted over by the floating Ask dock, which sits at 50.
`test/nav.test.ts` reads the README's own table and fails if it stops
matching NAV_GROUPS. A README cannot notice its own component being deleted;
this can.
Also: chat starters are one column below 30rem instead of six ragged widths.
Verified in the running app at 320/390/834/1024/1440 — no horizontal overflow
at any width and the header is 65px tall at all five, with every nav route
returning 200.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NkjZ1jJPhHzxAPvbSSG3wH
---------
Co-authored-by: Claude <noreply@anthropic.com>
159 lines
6.5 KiB
TypeScript
159 lines
6.5 KiB
TypeScript
/**
|
|
* Turning what a reader typed into something worth searching for.
|
|
*
|
|
* The assistant's web lookup used to send the question verbatim. That is fine
|
|
* for "who makes EUV optics" and useless for the question people actually ask
|
|
* while looking at a page:
|
|
*
|
|
* "who owns this company is it a public company is a private company
|
|
* what's the history of the company could you do"
|
|
*
|
|
* There is no subject in that string. Sent to a search engine it returns an
|
|
* explainer on the difference between public and private limited companies,
|
|
* which is exactly what came back on 2026-09-20, and the answer then
|
|
* (correctly) reported that the passage did not address the question. The
|
|
* reader asked about ASML, was standing on the EUV scanner page, and got a
|
|
* paragraph about company law.
|
|
*
|
|
* The subject was never missing — it was on screen. This module puts it back:
|
|
* the page the reader is on, or the first company among the retrieved rows when
|
|
* the question is about a company and the page is not one, becomes the head of
|
|
* the query. Everything here is pure, so it is tested without a network.
|
|
*/
|
|
import type { EntityKind } from './entities/types';
|
|
|
|
/** The page the reader is on, as far as query building cares. */
|
|
export interface QueryAnchor {
|
|
name: string;
|
|
kind: EntityKind;
|
|
}
|
|
|
|
/** A retrieved row, as far as query building cares. */
|
|
export interface QueryRow {
|
|
title: string;
|
|
kind: EntityKind;
|
|
}
|
|
|
|
/**
|
|
* Words that mean "I am asking about an organisation".
|
|
*
|
|
* On a bottleneck page, "this company" does not mean the bottleneck — it means
|
|
* whoever makes it. Resolving that to the material is how the answer ended up
|
|
* calling ASML "the sole EUV lithography-scanner maker" and never naming it.
|
|
*/
|
|
const ABOUT_A_COMPANY =
|
|
/\b(compan(?:y|ies)|firm|maker|manufacturer|supplier|vendor|owner|owns?|owned|ownership|shareholders?|listed|publicly|public|private|ipo|stock|share(?:s|holder)?|headquarter(?:s|ed)?|founded|founder|history|revenue|parent)\b/i;
|
|
|
|
/** Openers that carry no search value and crowd out the words that do. */
|
|
const POLITENESS =
|
|
/^(?:(?:hi|hey|hello|ok|okay|so|and|also|please|could|can|would|will|do)\s+(?:you\s+)?(?:please\s+)?(?:tell\s+me\s+|explain\s+|give\s+me\s+)?)+/i;
|
|
|
|
const MAX_QUERY = 180;
|
|
|
|
/** One line, no politeness, short enough to be a query rather than a paragraph. */
|
|
export function condense(question: string): string {
|
|
const flat = question.replace(/\s+/g, ' ').trim().replace(POLITENESS, '');
|
|
if (flat.length <= MAX_QUERY) return flat;
|
|
const cut = flat.slice(0, MAX_QUERY);
|
|
const space = cut.lastIndexOf(' ');
|
|
return (space > 40 ? cut.slice(0, space) : cut).trim();
|
|
}
|
|
|
|
/** Case-insensitive, so "asml" counts as naming ASML. */
|
|
function names(text: string, subject: string): boolean {
|
|
return text.toLowerCase().includes(subject.toLowerCase());
|
|
}
|
|
|
|
/**
|
|
* What the question is actually about, when the question does not say.
|
|
*
|
|
* Order matters: a question about an organisation asked on a page that is not
|
|
* one resolves to the first company among the retrieved rows, because that is
|
|
* what "this company" means to the person typing it.
|
|
*/
|
|
export function lookupSubject(
|
|
question: string,
|
|
anchor?: QueryAnchor,
|
|
context: QueryRow[] = [],
|
|
): string | undefined {
|
|
if (ABOUT_A_COMPANY.test(question) && anchor?.kind !== 'company') {
|
|
const company = context.find((row) => row.kind === 'company');
|
|
if (company) return company.title;
|
|
}
|
|
return anchor?.name ?? context[0]?.title;
|
|
}
|
|
|
|
/**
|
|
* The string sent to the search engine.
|
|
*
|
|
* A question that already names something on screen is left alone. Prefixing
|
|
* the page title onto "when did ASML buy Cymer" would bury the one term that
|
|
* makes the query specific, so the test is against every row retrieved, not
|
|
* only against the subject this module would otherwise pick.
|
|
*/
|
|
export function lookupQuery(
|
|
question: string,
|
|
anchor?: QueryAnchor,
|
|
context: QueryRow[] = [],
|
|
): string {
|
|
const core = condense(question);
|
|
const onScreen = [anchor?.name, ...context.map((row) => row.title)].filter(
|
|
(name): name is string => Boolean(name),
|
|
);
|
|
if (onScreen.some((name) => names(core, name))) return core;
|
|
const subject = lookupSubject(question, anchor, context);
|
|
if (!subject) return core;
|
|
return `${subject} ${core}`.trim();
|
|
}
|
|
|
|
/**
|
|
* Whether an answer is telling the reader the corpus does not hold this.
|
|
*
|
|
* This used to be `/not in your data/i` against one exact phrase, which meant a
|
|
* model that wrote "the records do not cover" — a perfectly good refusal, and
|
|
* one the prompt invites — never triggered the lookup that exists precisely for
|
|
* that case. Over-matching costs one bounded search; under-matching costs the
|
|
* reader an answer, so this leans towards looking.
|
|
*/
|
|
const NO_ANSWER: RegExp[] = [
|
|
/not in (?:your|the) data/i,
|
|
/\b(?:records?|corpus|data|rows?)\b[^.]{0,80}?\b(?:do(?:es)? not|don't|doesn't|cannot|can't|fail to)\b[^.]{0,40}?\b(?:contain|include|cover|identify|record|say|show|support|answer|address)/i,
|
|
/\b(?:no|nothing)\b[^.]{0,40}?\bin (?:the )?(?:corpus|records|data)\b/i,
|
|
/\bnot (?:covered|recorded|held|captured) (?:in|by) (?:the )?(?:corpus|records|data)\b/i,
|
|
];
|
|
|
|
export function saysNotInCorpus(answer: string): boolean {
|
|
return NO_ANSWER.some((pattern) => pattern.test(answer));
|
|
}
|
|
|
|
/**
|
|
* Questions worth asking next, built from what retrieval actually found.
|
|
*
|
|
* The old list was three copies of "Open the evidence for X", which is not a
|
|
* question, was never sent to the browser, and was therefore rendered nowhere.
|
|
* These are answerable by this assistant from the corpus, which is the point:
|
|
* an answer the corpus could not carry should still end somewhere useful.
|
|
*/
|
|
export function followUpsFor(context: QueryRow[]): string[] {
|
|
const asked: string[] = [];
|
|
for (const row of context) {
|
|
const question =
|
|
row.kind === 'bottleneck'
|
|
? `Who is recorded as producing ${row.title}?`
|
|
: row.kind === 'company'
|
|
? `What does the corpus record ${row.title} as making?`
|
|
: row.kind === 'policy'
|
|
? `Which bottlenecks does ${row.title} touch?`
|
|
: row.kind === 'country'
|
|
? `What is ${row.title} recorded as holding?`
|
|
: row.kind === 'science'
|
|
? `How ready is ${row.title}, and what would settle it?`
|
|
: row.kind === 'facility'
|
|
? `What is ${row.title} recorded as supplying?`
|
|
: undefined;
|
|
if (question && !asked.includes(question)) asked.push(question);
|
|
if (asked.length === 3) break;
|
|
}
|
|
return asked;
|
|
}
|