Files
CatoandClaude Opus 5.5 78943594ab fix: restore the data-entry contract (#211) and the search index (#212), removed by #213 (#216)
* feat(entry): the data-entry contract — candidate rows a cheaper model may add, validated at the gate (#211)

PR 5 of docs/architecture/2026-10-23-knowledge-system.md §3.

- lib/entry/candidate.ts: candidate types derived from each config row
  (CandidateOf<Row>), and the folds that merge a sidecar row into its config
  list flagged `evidence: 'candidate'`. No runtime config import, so config
  modules include it without a cycle.
- lib/entry/schemas.ts (+ vocab.ts, shape.ts): one Zod schema per facet, each
  `satisfies z.ZodType<…Candidate>`, refs resolved through resolveIn / the
  config's closed lists; events reuse lib/event-rules eventProblems.
- config/candidates/<facet>.json: eight empty sidecars, merged into COVERAGE,
  EVENTS, ENDOWMENTS, FACILITIES, INSTRUMENTS, SCIENCE, OWNERSHIP and the
  corpus series the way substrata-events-accepted.json is merged into EVENTS.
- A candidate producer reads "Candidate source", never "Sourced"
  (verificationFor takes the row's evidence), and coverage progress does not
  count it.
- scripts/research/add.ts <facet> <json|-> [--dry-run] [--branch] [--schema]:
  exit 2 schema, 3 unreachable, 4 quote absent (verbatimIn), 5 duplicate;
  appends sorted, idempotent; --branch commits on data/<facet>-<key>.
- test/candidates.test.ts: every sidecar row passes its schema, is
  `candidate`, has url + asOf <= today; the gate exits 4 / appends sorted /
  exits 5 against a local HTTP server.
- prompts/data-entry.md: the §3.4 prompt, how to run it, the printed shapes.
- NODE_TYPE_LABEL moves to config/substrata.ts and the ownership labels to
  config/substrata-ownership-labels.ts (both re-exported) to keep the two
  config files under their size ceilings.

Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

Co-authored-by: Claude <noreply@anthropic.com>
(cherry picked from commit 329dd587d8)

* feat(index): the corpus as retrievable chunks, and the ask-evals that name the failures (#212)

* feat(index): the corpus as retrievable chunks, and the ask-evals that name the failures

PR 2 and PR 3 of docs/architecture/2026-10-23-knowledge-system.md §7.

- test/ask-evals.json: 20 questions (bottleneck, company, country, material,
  policy, science, capital, events, jobs, three off-topic), including
  niger-uranium with mustNotLead oil/natural gas and its quality gap.
  test/ask-evals.test.ts checks them against planLookups and the in-process
  retriever, no model, no DB; rows waiting on PR 1 / PR 6 are skipped with
  a printed count.
- lib/index/: Chunk type, ids, hashes, splitter; one producer per facet
  (entity, event, series, policy, science, restriction, quantity, ranking,
  quality); buildChunks(); retrieve() with the ADR's hybrid score minus
  vectors, reusing scoreDocuments. Evidence labels map to the closed set in
  one function, typed against config/substrata-evidence.
- search_corpus runs on retrieve(), so chat sees events, series, policy,
  restrictions, quantities, rankings and findings as rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

* perf(index): warm the chunk index at server start, off the request path

buildChunks() costs ~5 s cold; without this the first reader to Ask after a
deploy paid for it inside search_corpus. instrumentation.ts register() (nodejs
runtime only) loads lib/index/warm and schedules the build on a later
event-loop turn without awaiting it; a failure only logs, and the index then
builds lazily as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

---------

Co-authored-by: Claude <noreply@anthropic.com>
(cherry picked from commit edcf6eeee7)

* chore: restore #211 and #212, removed by #213's squash-merge

The Ask panel branch (#213) was cut before the data-entry contract (#211)
and the search index (#212) merged; its squash-merge wrote main back to
its own copy of those files and deleted both features (60 files: lib/entry,
lib/index, config/candidates, prompts/data-entry.md, scripts/research/add.ts,
instrumentation.ts, test/ask-evals*). The two commits above re-apply them
onto current main by cherry-pick; both applied without conflicts, and
format, type-check, lint and 660 tests pass (4 pending evals skipped).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-23 13:47:27 +02:00

276 lines
9.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Dated numbers per bottleneck, as time series.
*
* A series is one metric, in one unit, for one geography, on one bottleneck —
* "large power transformer lead time, weeks, US" — and each point on it is a
* value FOR a period, with the page it came from and the sentence on that page
* that carries the number. A point with no quote is a number nobody can check,
* and the site has no place for those.
*
* Two origins, never blended into one claim:
*
* - `corpus` — points read from a public page and committed to
* `research/series.json`. The quote was checked against the fetched page
* (`scripts/research/check-series.ts`); `check` says by whom.
* - `official` — points from an official statistical API (the US Bureau of
* Labor Statistics), fetched on a schedule into the database and shown as
* the agency published them. Nobody re-types them, so nobody reviews them;
* the UI says which agency and when it was fetched.
*
* Nothing here is interpolated. A gap between two points is drawn as a gap in
* time, never as a value.
*/
import corpusFile from '../research/series.json';
import pointCandidates from '../config/candidates/series-points.json';
import { sidecarRows, withCandidatePoints, type SeriesPointCandidate } from './entry/candidate';
export type SeriesKind =
| 'lead-time'
| 'price'
| 'price-index'
| 'capacity'
| 'output'
| 'backlog'
| 'orders'
| 'inventory'
| 'trade'
| 'queue'
| 'workforce'
| 'other';
export const KIND_LABEL: Record<SeriesKind, string> = {
'lead-time': 'Lead time',
price: 'Price',
'price-index': 'Price index',
capacity: 'Capacity',
output: 'Output',
backlog: 'Backlog',
orders: 'Orders',
inventory: 'Inventory',
trade: 'Trade volume',
queue: 'Queue',
workforce: 'Workforce',
other: 'Other',
};
/** Which way is bad news for the bottleneck, so a move can be read as tightening. */
export type Direction = 'up-tightens' | 'up-loosens' | 'neutral';
export interface SeriesPoint {
/** The period the value is FOR: YYYY, YYYY-Qn, YYYY-MM or YYYY-MM-DD. */
date: string;
value: number;
/**
* When the source states a range ("80–210 weeks"), its lower end; `value`
* is then the upper end, and both are shown. Never averaged into one number.
*/
low?: number;
/** The page it came from. For official series, the agency's page for the series. */
source: string;
publisher: string;
/** When the source was published, where it says. */
published?: string;
/** The sentence (or table line) on the source that carries the number. */
quote?: string;
/** The publisher's own statement or statistic, rather than a report of it. */
primary: boolean;
note?: string;
/** Official series: the agency marks the value preliminary. */
preliminary?: boolean;
/** Official series: when the value first reached the database. */
firstSeen?: string;
/** Set only on a row folded in from config/candidates/ — a cheaper model's row nobody has promoted. */
evidence?: 'candidate';
}
export interface Series {
id: string;
/** Bottleneck slug. */
bottleneck: string;
metric: string;
unit: string;
geography: string;
kind: SeriesKind;
direction: Direction;
/** What the series literally counts, where that is narrower or broader than the bottleneck. */
describes?: string;
origin: 'corpus' | 'official';
/** Shown first on its bottleneck: the series that best answers "how bad is it now". */
headline?: boolean;
/** How the points were checked: who read them, and on what date. */
check: string;
checkedOn: string;
points: SeriesPoint[];
}
type CorpusSeries = Omit<Series, 'origin' | 'points'> & { points: SeriesPoint[] };
// Candidate points (config/candidates/series-points.json) join their series
// before sorting, each flagged `evidence: 'candidate'`.
const CORPUS: Series[] = withCandidatePoints(
corpusFile.series as CorpusSeries[],
sidecarRows<SeriesPointCandidate>(pointCandidates),
).map((s) => ({
...s,
origin: 'corpus' as const,
points: sortPoints(s.points),
}));
/** Committed series, every bottleneck. */
export function corpusSeries(): Series[] {
return CORPUS;
}
/**
* Where a period sits on a time axis, in ms. A year is placed mid-year and a
* quarter mid-quarter: a position for drawing, not a claim about the value.
*/
export function periodTime(date: string): number {
const q = date.match(/^(\d{4})-Q([1-4])$/);
if (q) return Date.UTC(Number(q[1]), (Number(q[2]) - 1) * 3 + 1, 15);
const m = date.match(/^(\d{4})(?:-(\d{2}))?(?:-(\d{2}))?$/);
if (!m) return NaN;
if (!m[2]) return Date.UTC(Number(m[1]), 6, 1);
return Date.UTC(Number(m[1]), Number(m[2]) - 1, m[3] ? Number(m[3]) : 15);
}
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
/** "2025", "Q2 2025", "Aug 2026", "3 Mar 2026". */
export function periodLabel(date: string): string {
const q = date.match(/^(\d{4})-Q([1-4])$/);
if (q) return `Q${q[2]} ${q[1]}`;
const m = date.match(/^(\d{4})(?:-(\d{2}))?(?:-(\d{2}))?$/);
if (!m) return date;
if (!m[2]) return m[1];
const month = MONTHS[Number(m[2]) - 1];
return m[3] ? `${Number(m[3])} ${month} ${m[1]}` : `${month} ${m[1]}`;
}
export function sortPoints(points: readonly SeriesPoint[]): SeriesPoint[] {
return [...points].sort((a, b) => periodTime(a.date) - periodTime(b.date));
}
export interface Change {
from: SeriesPoint;
to: SeriesPoint;
/** Fraction, signed: 0.12 is +12%. Undefined when the prior value is zero. */
pct?: number;
delta: number;
}
/** The latest point against the one before it. Undefined with fewer than two points. */
export function latestChange(series: Pick<Series, 'points'>): Change | undefined {
const n = series.points.length;
if (n < 2) return undefined;
return changeBetween(series.points[n - 2], series.points[n - 1]);
}
export function changeBetween(from: SeriesPoint, to: SeriesPoint): Change {
return {
from,
to,
delta: to.value - from.value,
pct: from.value === 0 ? undefined : (to.value - from.value) / Math.abs(from.value),
};
}
/** "+12%", "−3.4%", "0%": one decimal below ten per cent, none above. */
export function formatPct(pct: number): string {
const abs = Math.abs(pct * 100);
const text = abs >= 10 ? abs.toFixed(0) : abs.toFixed(1).replace(/\.0$/, '');
if (text === '0') return '0%';
return `${pct > 0 ? '+' : '−'}${text}%`;
}
/** Values as the source gave them, grouped for reading; never rounded past what was stated. */
export function formatValue(value: number): string {
return value.toLocaleString('en', { maximumFractionDigits: 3 });
}
/** A point's value as stated: "128", or "80–210" where the source gave a range. */
export function formatPoint(point: Pick<SeriesPoint, 'value' | 'low'>): string {
return point.low !== undefined
? `${formatValue(point.low)}–${formatValue(point.value)}`
: formatValue(point.value);
}
/**
* The smallest move, either way, that is called tightening or loosening.
*
* Below it a change is shown with its sign and read as flat: a lead time from
* 128 to 126 weeks, or a monthly price index up 0.4%, is inside the noise of
* a survey or a revision, and calling it "loosens" would be a claim the
* number cannot carry. The method page states it (`series-change`).
*/
export const MIN_MOVE_PCT = 0.03;
/** A move too small to call either way (see MIN_MOVE_PCT). A move off zero has no % and is never small. */
export function isSmallMove(change: Change | undefined): boolean {
return !!change && change.pct !== undefined && Math.abs(change.pct) < MIN_MOVE_PCT;
}
/** Whether a move is bad news, good news or neither for the bottleneck. */
export function effectOf(
series: Pick<Series, 'direction'>,
change: Change | undefined,
): 'tightens' | 'loosens' | 'neutral' {
if (!change || change.delta === 0 || series.direction === 'neutral') return 'neutral';
if (isSmallMove(change)) return 'neutral';
// A plan or forecast has not happened; it moves nothing yet.
if (isPlanned(change.to)) return 'neutral';
const up = change.delta > 0;
return up === (series.direction === 'up-tightens') ? 'tightens' : 'loosens';
}
/**
* A value for a period that has not started: a target, plan or forecast the
* source stated. Shown, and labelled, never read as a measurement.
*/
export function isPlanned(point: Pick<SeriesPoint, 'date'>, now = Date.now()): boolean {
const q = point.date.match(/^(\d{4})-Q([1-4])$/);
const start = q
? Date.UTC(Number(q[1]), (Number(q[2]) - 1) * 3, 1)
: Date.UTC(
Number(point.date.slice(0, 4)),
point.date.length > 4 ? Number(point.date.slice(5, 7)) - 1 : 0,
point.date.length > 7 ? Number(point.date.slice(8, 10)) : 1,
);
return start > now;
}
/** The newest point that is not a plan: what "latest" means when ordering. */
export function lastActual(series: Pick<Series, 'points'>): SeriesPoint | undefined {
return [...series.points].reverse().find((p) => !isPlanned(p));
}
/** A move at least this large, either way, is flagged on the desk. */
export const ALERT_PCT = 0.1;
export function seriesFor(all: readonly Series[], bottleneck: string): Series[] {
return all.filter((s) => s.bottleneck === bottleneck && s.points.length > 0).sort(byRelevance);
}
/**
* Headline series first, then those with a recent actual value and a history,
* then everything else; plans and one-off figures sink.
*/
export function byRelevance(a: Series, b: Series): number {
const recency = (s: Series) => {
const p = lastActual(s);
return p ? periodTime(p.date) : 0;
};
const year = 365 * 86_400_000;
const score = (s: Series) =>
(s.headline ? 4 : 0) +
(s.points.length > 1 ? 2 : 0) +
(recency(s) > Date.now() - 2 * year ? 1 : 0);
return score(b) - score(a) || recency(b) - recency(a) || b.points.length - a.points.length;
}
export function seriesById(all: readonly Series[], id: string): Series | undefined {
return all.find((s) => s.id === id);
}
export { seriesCsv } from './series-csv';