mirror of
https://github.com/bitbaum/hirnli.git
synced 2026-12-04 05:47:28 +01:00
`THEME_HIERARCHY` names one organisation's core fields of work — labour
integration, circular economy, digital education, digital sovereignty. A theme
in `core` scores 1.5x, one in `secondary` 0.5x, and every tenant's scoring used
that list.
It reaches production twice, and I had to be corrected on both by the code:
- the seven ingest and enrichment scripts call `computeFitScore`, which
writes the number into an organisation's assessment. That number decides
which foundations it is told to approach.
- the "Fit" tab explains the same number back, through `explainFitScore`. It
renders client-side, which is why curling the page never showed it.
It stayed invisible because only one organisation has ever run research, so the
priorities compiled into the module were that organisation's and the scores were
right by coincidence. A second customer turns "right for the only user" into
"wrong for a user".
`org_scoring` has held a complete per-organisation engine since migration 0007
and nothing read it — the fifth producer with no consumer found in this work.
It is read now, and the stored row's thematic categories are byte-identical to
the module's, so the first customer's scores do not move. An organisation with
no row gets one flat category covering every theme id: a worse fit signal than a
stated ranking, and a much better one than somebody else's.
The parameter is required rather than defaulted. A default would have been
silent — every existing caller would keep compiling and quietly change what it
weighted. Making it explicit is what surfaced that two ingest scripts wrote
per-organisation fit scores without resolving an organisation at all, which is
how they came to score with whatever was compiled in.
`ThemeCategory` is exported from the config and used by the reader, the engine
and the scripts. The first draft declared its own and the two drifted on `cap`
within the hour.
Claude-Session: https://claude.ai/code/session_01HVwg8DKHQktxJuHeLM3xpG
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
55 lines
2.1 KiB
TypeScript
55 lines
2.1 KiB
TypeScript
/**
|
|
* A tenant's theme priorities, for standalone scripts.
|
|
*
|
|
* The scripts' counterpart to `src/lib/content/scoring-source.ts`, and it
|
|
* exists for the same reason as `scripts/lib/tenant.ts`: these are `tsx`
|
|
* one-shots outside any Next request, so there is no `headers()` to resolve an
|
|
* org from and no Drizzle client — just the raw `pg` connection.
|
|
*
|
|
* Why it matters here more than anywhere else: the ingest and enrichment
|
|
* scripts are what WRITE fit scores into an organisation's assessments. Those
|
|
* scores decide which foundations it is told to approach. Computing them with
|
|
* the code module's `THEME_HIERARCHY` meant computing every organisation's
|
|
* priorities as one organisation's — invisible while only that organisation had
|
|
* ever run research.
|
|
*
|
|
* A tenant with no row gets null, and the scorer weights every theme equally.
|
|
* That is a worse fit signal than a stated ranking and a much better one than
|
|
* somebody else's ranking.
|
|
*/
|
|
|
|
import { query } from './db';
|
|
import type { ThemeCategory } from '../../src/lib/config/fit-scoring';
|
|
|
|
interface ScoringRow {
|
|
engine: unknown;
|
|
}
|
|
|
|
function isCategoryArray(v: unknown): v is ThemeCategory[] {
|
|
return (
|
|
Array.isArray(v) &&
|
|
v.length > 0 &&
|
|
v.every(
|
|
(c) =>
|
|
typeof c === 'object' &&
|
|
c !== null &&
|
|
typeof (c as ThemeCategory).name === 'string' &&
|
|
Array.isArray((c as ThemeCategory).members) &&
|
|
typeof (c as ThemeCategory).weight === 'number',
|
|
)
|
|
);
|
|
}
|
|
|
|
/** This organisation's theme priorities, or null if it has stated none. */
|
|
export async function loadThemePriorities(orgId: string): Promise<ThemeCategory[] | null> {
|
|
const rows = await query<ScoringRow>('select engine from org_scoring where org_id = $1', [orgId]);
|
|
const engine = rows[0]?.engine as { dimensions?: unknown } | undefined;
|
|
if (!engine || !Array.isArray(engine.dimensions)) return null;
|
|
|
|
const thematic = (engine.dimensions as { id?: string; config?: { categories?: unknown } }[]).find(
|
|
(d) => d.id === 'thematic',
|
|
);
|
|
const categories = thematic?.config?.categories;
|
|
return isCategoryArray(categories) ? categories : null;
|
|
}
|