T-87: M1.1l — Security-type classifier: eodSymbols.Type → securityTypeByIsin for the security-master loader (no new dependencies)

This commit is contained in:
Studio 2026-11-28 05:03:34 -05:00
parent 2f5ac2e7e7
commit a368e8a47a
6 changed files with 285 additions and 4 deletions

View file

@ -113,7 +113,8 @@ bun test test/unit/security-master-source-symbol-readers.test.ts
| `Currency`, `Forex`, `FX` | `fx` |
- **Published-vocabulary & no-guess rule:** Because QA MongoDB is unreachable from the agent sandbox, the table is based on the published EODHD `Type` vocabulary. Non-equity-like values (`FUND`, `Mutual Fund`, `Bond`, `Notes`, `INDEX`, etc.), blanks, non-strings, and unmapped strings return `null` and are never guessed from ticker suffixes or company names. A non-string `Type` on an `eodSymbols` document does not skip the document from Stage A/B — it is counted under `securityTypeStats.missingType`.
- **Reviewing unmapped values & conflicts:** After a real security-master run, inspect `unknownSecurityTypes` (sorted by count descending, then ASCII) for any newly observed provider strings that should be added to `EOD_SECURITY_TYPE_MAP`, and inspect `securityTypeConflicts` (ISINs omitted from `securityTypeByIsin` because multiple EOD documents disagreed on their classified type; capped by `maxSecurityTypeConflicts`, default `200`, with total counts in `securityTypeStats.isinsConflicting` and `conflictsTruncated`).
- **Stage A membership & raw vocabulary diagnostics:** `readEodSymbols` / `readSecurityMasterSources` build the loader-facing `securityTypeByIsin` map strictly from EOD rows accepted as Stage A instrument members (`buildSecurityTypeByIsinForStageA`), so rows quarantined by Stage A (`isin_cusip_mismatch`, `duplicate_source_id`) can neither supply nor veto a valid instrument's security type. All emitted EOD rows remain available in `securityTypeRecords` for raw vocabulary diagnostics (`buildSecurityTypeByIsin(sources.securityTypeRecords)`).
- **Reviewing unmapped values & conflicts:** After a real security-master run, inspect `unknownSecurityTypes` (sorted by count descending, then ASCII) for any newly observed provider strings that should be added to `EOD_SECURITY_TYPE_MAP`, and inspect `securityTypeConflicts` (ISINs omitted from `securityTypeByIsin` because multiple Stage A-accepted EOD documents disagreed on their classified type; capped by `maxSecurityTypeConflicts`, default `200`, with total counts in `securityTypeStats.isinsConflicting` and `conflictsTruncated`).
- **Local synthetic benchmark:** Run the unit suite and fixed-seed 300,000-record synthetic benchmark (zero DB connections) with:
```bash

View file

@ -193,6 +193,7 @@ export {
export {
buildSecurityTypeByIsin,
buildSecurityTypeByIsinForStageA,
classifyEodSecurityType,
DEFAULT_MAX_SECURITY_TYPE_CONFLICTS,
EOD_SECURITY_TYPE_MAP,

View file

@ -8,6 +8,7 @@
* suffixes or company names.
*/
import { compareAscii } from './exchange-pass1';
import type { StageAInstrumentCandidate } from './instrument-stage-a';
import type { SecurityType } from './persist';
export const DEFAULT_MAX_SECURITY_TYPE_CONFLICTS = 200;
@ -276,3 +277,49 @@ export function buildSecurityTypeByIsin(
conflicts,
};
}
/**
* Builds the loader-facing `securityTypeByIsin` map strictly from EOD rows accepted as Stage A
* instrument members (`member.source === 'eod'`), keyed by each instrument's `primaryIsin`.
* Stage A-quarantined rows (e.g. `isin_cusip_mismatch`, `duplicate_source_id`) and unresolved
* rows are excluded so they can neither supply nor veto a valid instrument's security type.
*/
export function buildSecurityTypeByIsinForStageA(
records: readonly SecurityTypeInputRecord[],
instruments: readonly StageAInstrumentCandidate[],
options: BuildSecurityTypeByIsinOptions = {}
): BuildSecurityTypeByIsinResult {
const acceptedIsinByEodSourceId = new Map<string, string>();
for (let i = 0; i < instruments.length; i++) {
const instrument = instruments[i];
if (instrument === undefined) {
continue;
}
const members = instrument.members;
for (let j = 0; j < members.length; j++) {
const member = members[j];
if (member !== undefined && member.source === 'eod') {
acceptedIsinByEodSourceId.set(member.sourceId, instrument.primaryIsin);
}
}
}
const acceptedRecords: SecurityTypeInputRecord[] = [];
for (let i = 0; i < records.length; i++) {
const record = records[i];
if (record === undefined) {
continue;
}
const primaryIsin = acceptedIsinByEodSourceId.get(record.sourceId);
if (primaryIsin === undefined) {
continue;
}
acceptedRecords.push({
isin: primaryIsin,
rawType: record.rawType,
sourceId: record.sourceId,
});
}
return buildSecurityTypeByIsin(acceptedRecords, options);
}

View file

@ -17,7 +17,7 @@ import {
type ExchangeSource,
type ExchangeSourceMapRow,
} from './exchange-pass1';
import type { StageASourceRecord } from './instrument-stage-a';
import { matchInstrumentsStageA, type StageASourceRecord } from './instrument-stage-a';
import {
MAX_TICKER_ROOT_LENGTH,
type StageBEodListingInput,
@ -28,7 +28,7 @@ import {
} from './listing-stage-b';
import type { SecurityType } from './persist';
import {
buildSecurityTypeByIsin,
buildSecurityTypeByIsinForStageA,
type SecurityTypeClassificationStats,
type SecurityTypeConflict,
type SecurityTypeInputRecord,
@ -232,6 +232,7 @@ export type AdaptEodSymbolChangeResult =
export interface ReadEodSymbolsResult {
stageARecords: StageASourceRecord[];
eodListings: StageBEodListingInput[];
securityTypeRecords: SecurityTypeInputRecord[];
securityTypeByIsin: Map<string, SecurityType>;
securityTypeStats: SecurityTypeClassificationStats;
unknownSecurityTypes: UnknownSecurityTypeCount[];
@ -274,6 +275,7 @@ export interface SecurityMasterSourcesStats {
export interface SecurityMasterSourcesResult {
stageARecords: StageASourceRecord[];
stageBInput: SecurityMasterStageBSourceInput;
securityTypeRecords: SecurityTypeInputRecord[];
securityTypeByIsin: Map<string, SecurityType>;
securityTypeStats: SecurityTypeClassificationStats;
unknownSecurityTypes: UnknownSecurityTypeCount[];
@ -1047,6 +1049,33 @@ function compareSymbolChanges(a: StageBSymbolChangeInput, b: StageBSymbolChangeI
);
}
function rawTypeSortKey(rawType: unknown): string {
if (typeof rawType === 'string') {
return `s:${rawType}`;
}
if (rawType === null) {
return 'n:null';
}
if (rawType === undefined) {
return 'u:undefined';
}
if (typeof rawType === 'number' || typeof rawType === 'boolean' || typeof rawType === 'bigint') {
return `${typeof rawType}:${String(rawType)}`;
}
return `o:${typeof rawType}`;
}
function compareSecurityTypeRecords(
a: SecurityTypeInputRecord,
b: SecurityTypeInputRecord
): number {
return (
compareAscii(a.sourceId, b.sourceId) ||
compareAscii(a.isin ?? '', b.isin ?? '') ||
compareAscii(rawTypeSortKey(a.rawType), rawTypeSortKey(b.rawType))
);
}
/**
* Pure adapter converting a raw `eodSymbols` document into a Stage A source record
* and Stage B EOD listing input.
@ -1702,9 +1731,16 @@ export async function readEodSymbols(
stageARecords.sort(compareStageARecords);
eodListings.sort(compareEodListings);
securityTypeRecords.sort(compareSecurityTypeRecords);
const securityTypeResult = buildSecurityTypeByIsin(
// Build the loader-facing map strictly from EOD rows accepted as Stage A instrument members so
// Stage A-quarantined rows (e.g. isin_cusip_mismatch, duplicate_source_id) cannot supply or veto
// a valid instrument's type, while keeping all emitted rows in `securityTypeRecords` for raw
// vocabulary diagnostics.
const eodStageA = matchInstrumentsStageA(stageARecords);
const securityTypeResult = buildSecurityTypeByIsinForStageA(
securityTypeRecords,
eodStageA.instruments,
options.maxSecurityTypeConflicts !== undefined
? { maxConflicts: options.maxSecurityTypeConflicts }
: undefined
@ -1713,6 +1749,7 @@ export async function readEodSymbols(
return {
stageARecords,
eodListings,
securityTypeRecords,
securityTypeByIsin: securityTypeResult.securityTypeByIsin,
securityTypeStats: securityTypeResult.stats,
unknownSecurityTypes: securityTypeResult.unknownRawTypes,
@ -1931,6 +1968,7 @@ export async function readSecurityMasterSources(
tickerSources,
symbolChanges: symbolChangeResult.symbolChanges,
},
securityTypeRecords: eodResult.securityTypeRecords,
securityTypeByIsin: eodResult.securityTypeByIsin,
securityTypeStats: eodResult.securityTypeStats,
unknownSecurityTypes: eodResult.unknownSecurityTypes,