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

183 lines
6.4 KiB
TypeScript

/**
* The map as data, for machines.
*
* The site renders the research for people. This builds the same objects —
* the same coverage rows, the same evidence, the same thesis — into one JSON
* document for an agent or a script that wants to consume the map rather
* than scrape the prose. Nothing here is a second copy: every field is read
* from the config the pages are drawn from, so the API cannot say something
* the site does not.
*
* The verification vocabulary is the site's: `sourced` is a checked claim and
* `unverified` is not. Pages the sourcing engine found are a review queue in
* the database, not corpus, so this document (and the export digest built
* from it) never carries them.
*
* Created: 2026-09-14
*/
import { COMPANY, MANDATE_CURVES, MATERIALS, areaFor } from '@/config/substrata';
import { CHOKEPOINTS, COVERAGE } from '@/config/substrata-coverage';
import { chokepointProgress, coverageProgress } from '@/lib/coverage-progress';
import { INVESTMENT_THESIS } from '@/config/substrata-acting';
import { CAPITAL_KINDS, CAPITAL_PROVIDERS, FUNDING_ASSESSMENTS } from '@/config/substrata-capital';
import { CHAIN_LAYERS, PARTICIPANTS } from '@/config/substrata-participants';
import { verificationFor } from '@/config/substrata-evidence';
import { RESEARCH_PROGRAMMES, programmeProgress } from '@/config/substrata-programmes';
import { CALLS, record } from '@/config/substrata-calls';
import { eventsNewestFirst } from '@/config/substrata-events';
import { STAGES } from '@/config/substrata-stages';
import { BOTTLENECKS } from '@/lib/bottlenecks';
import { SITE } from '@/lib/site';
export function buildMap() {
const coverage = coverageProgress();
const chokepoints = chokepointProgress();
return {
name: COMPANY.name,
tagline: COMPANY.tagline,
host: SITE.host,
generatedAt: new Date().toISOString(),
notice:
'Research, not advice. A row is "sourced" when a claim is backed by a primary source and ' +
'"unverified" otherwise. Only "sourced" is a finding. A bottleneck whose rows are only ' +
'partly sourced reads "candidate".',
progress: {
producers: {
total: coverage.total,
sourced: coverage.sourced,
},
chokepoints: { total: chokepoints.total, sourced: chokepoints.sourced },
},
stages: STAGES.map((stage) => ({
id: stage.id,
name: stage.name,
reliefTime: stage.reliefTime,
bottlenecks: BOTTLENECKS.filter((b) => b.stage === stage.id).map((b) => b.slug),
})),
bottlenecks: BOTTLENECKS.map((b) => ({
slug: b.slug,
name: b.name,
kind: b.kind,
stage: b.stage,
binding: b.binding,
score: b.score,
horizon: b.horizon,
judgedOn: b.judgedOn,
state: b.state,
events: b.events.map((e) => e.id),
})),
events: eventsNewestFirst(),
calls: {
record: record(),
items: CALLS.map((call) => ({
id: call.id,
madeOn: call.madeOn,
claim: call.claim,
tests: call.tests,
bottlenecks: call.bottlenecks,
resolveBy: call.resolveBy,
settledBy: call.settledBy,
resolution: call.resolution,
})),
},
curves: MANDATE_CURVES.map((curve) => ({ id: curve.id, label: curve.label, test: curve.test })),
materials: COVERAGE.map((entry) => {
const listing = MATERIALS.find((m) => m.title === entry.material);
return {
material: entry.material,
area: listing ? areaFor(listing).id : null,
curve: listing ? areaFor(listing).curve : null,
spec: listing?.spec ?? null,
thesis: entry.thesis,
producers: entry.producers.map((producer) => ({
name: producer.name,
jurisdictions: producer.jurisdictions,
role: producer.role,
verification: verificationFor(producer.source, producer.evidence),
source: producer.source,
})),
};
}),
chokepoints: CHOKEPOINTS.map((point) => ({
name: point.name,
type: point.type,
curve: point.curve,
jurisdictions: point.jurisdictions,
why: point.why,
verification: point.source ? 'sourced' : 'unverified',
source: point.source,
})),
participants: {
layers: CHAIN_LAYERS.map((layer) => ({ id: layer.id, name: layer.name, curve: layer.curve })),
rows: PARTICIPANTS.map((item) => ({
name: item.name,
layer: item.layer,
role: item.role,
jurisdictions: item.jurisdictions,
scarcity: item.scarcity,
why: item.why,
})),
},
// Capital is a map of who funds what on whose mandate. `willNotFund` is the
// field that carries it, and `constraint` says out loud where money is not
// what is missing — which, for most rows here, is the answer. Nothing in
// this block is advice on where to put money.
capital: {
kinds: CAPITAL_KINDS.map((kind) => ({
id: kind.id,
name: kind.name,
plain: kind.plain,
chequeSize: kind.chequeSize,
horizon: kind.horizon,
willFund: kind.willFund,
willNotFund: kind.willNotFund,
})),
providers: CAPITAL_PROVIDERS.map((provider) => ({
id: provider.id,
name: provider.name,
kind: provider.kind,
jurisdiction: provider.jurisdiction,
mandate: provider.mandate,
canMove: provider.canMove,
source: provider.source,
primary: provider.primary,
quote: provider.quote,
readOn: provider.readOn,
})),
funding: FUNDING_ASSESSMENTS.map((item) => ({
bottleneck: item.bottleneck,
constraint: item.constraint,
why: item.why,
})),
},
thesis: INVESTMENT_THESIS.map((claim) => ({
id: claim.id,
claim: claim.claim,
falsifier: claim.falsifier,
})),
programmes: RESEARCH_PROGRAMMES.map((programme) => ({
id: programme.id,
title: programme.title,
question: programme.question,
opened: programme.opened,
status: programme.status,
progress: programmeProgress(programme),
layers: programme.layers.map((layer) => ({
id: layer.id,
name: layer.name,
period: layer.period,
gatedBy: layer.gatedBy,
})),
questions: programme.questions.map((q) => ({
id: q.id,
question: q.question,
settledBy: q.settledBy,
})),
})),
};
}
export type SubstrataMap = ReturnType<typeof buildMap>;