T-133: M2.1c — MCP copilot event_study tool: vault-fenced event study (bars + events → buildEventStudyInput → runEventStudy + trial ledger) in libs/research/src/mcp/tools/
This commit is contained in:
parent
f9ac7024d3
commit
5a36dc8aa6
4 changed files with 245 additions and 19 deletions
|
|
@ -70,6 +70,12 @@ export const EVENT_STUDY_MAX_PRE_DAYS = 60;
|
|||
/** Hard maximum post-event window length in trading sessions. */
|
||||
export const EVENT_STUDY_MAX_POST_DAYS = 60;
|
||||
|
||||
/** Hard maximum number of event subtype entries accepted in `event.subtypes`. */
|
||||
export const EVENT_STUDY_MAX_SUBTYPES = 100;
|
||||
|
||||
/** Hard maximum character length of each event subtype string in `event.subtypes`. */
|
||||
export const EVENT_STUDY_MAX_SUBTYPE_LENGTH = 128;
|
||||
|
||||
const LEDGER_UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
const AGENT_PRINCIPAL_RE =
|
||||
/^agent:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
||||
|
|
@ -101,10 +107,13 @@ const eventFilterSpecSchema = z
|
|||
'Non-empty list of event kinds to include (filing, earnings, ceo_post, or corporate_action).'
|
||||
),
|
||||
subtypes: z
|
||||
.array(z.string().min(1))
|
||||
.array(z.string().min(1).max(EVENT_STUDY_MAX_SUBTYPE_LENGTH))
|
||||
.min(1)
|
||||
.max(EVENT_STUDY_MAX_SUBTYPES)
|
||||
.optional()
|
||||
.describe('Optional non-empty exact-match allow-list of event subtypes.'),
|
||||
.describe(
|
||||
`Optional non-empty exact-match allow-list of event subtypes (max ${EVENT_STUDY_MAX_SUBTYPES} entries, ${EVENT_STUDY_MAX_SUBTYPE_LENGTH} chars each).`
|
||||
),
|
||||
})
|
||||
.strict()
|
||||
.describe('Event kind and optional subtype filter specification.');
|
||||
|
|
@ -421,29 +430,40 @@ function filterEligibleBars(
|
|||
bars: readonly BarRecord[],
|
||||
listingIdSet: ReadonlySet<number>,
|
||||
fromMs: number,
|
||||
fromSubMs: number,
|
||||
toMs: number,
|
||||
asOfMs: number,
|
||||
vaultCutoffTs: number,
|
||||
vaultCutoffDate: string
|
||||
): BarRecord[] {
|
||||
): { readonly bars: BarRecord[]; readonly prePanelVaultExcludedDates: number } {
|
||||
const eligible: BarRecord[] = [];
|
||||
const vaultExcludedDates = new Set<string>();
|
||||
const minTsMs = fromSubMs > 0 ? fromMs + 1 : fromMs;
|
||||
|
||||
for (let i = 0; i < bars.length; i += 1) {
|
||||
const row = bars[i];
|
||||
if (typeof row !== 'object' || row === null) {
|
||||
eligible.push(row as unknown as BarRecord);
|
||||
continue;
|
||||
}
|
||||
if (isValidEpochMs(row.ts)) {
|
||||
if (
|
||||
row.ts < fromMs ||
|
||||
row.ts > toMs ||
|
||||
row.ts > asOfMs ||
|
||||
row.ts >= vaultCutoffTs ||
|
||||
utcDateOf(row.ts) >= vaultCutoffDate
|
||||
) {
|
||||
if (typeof row.listingId === 'number' && !listingIdSet.has(row.listingId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const tsValid = isValidEpochMs(row.ts);
|
||||
const tsDate = tsValid ? utcDateOf(row.ts) : null;
|
||||
const tsInVault = tsValid && (row.ts >= vaultCutoffTs || (tsDate as string) >= vaultCutoffDate);
|
||||
if (tsInVault) {
|
||||
vaultExcludedDates.add(tsDate as string);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tsValid) {
|
||||
if (row.ts < minTsMs || row.ts > toMs || row.ts > asOfMs) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (isValidEpochMs(row.knownAt)) {
|
||||
if (
|
||||
row.knownAt > asOfMs ||
|
||||
|
|
@ -453,12 +473,14 @@ function filterEligibleBars(
|
|||
continue;
|
||||
}
|
||||
}
|
||||
if (typeof row.listingId === 'number' && !listingIdSet.has(row.listingId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
eligible.push(row);
|
||||
}
|
||||
return eligible;
|
||||
|
||||
return {
|
||||
bars: eligible,
|
||||
prePanelVaultExcludedDates: vaultExcludedDates.size,
|
||||
};
|
||||
}
|
||||
|
||||
function filterEligibleEventsTable(params: {
|
||||
|
|
@ -467,6 +489,7 @@ function filterEligibleEventsTable(params: {
|
|||
readonly kindSet: ReadonlySet<string>;
|
||||
readonly subtypeSet: ReadonlySet<string> | null;
|
||||
readonly fromMs: number;
|
||||
readonly fromSubMs: number;
|
||||
readonly toMs: number;
|
||||
readonly asOfMs: number;
|
||||
readonly vaultCutoffTs: number;
|
||||
|
|
@ -478,6 +501,7 @@ function filterEligibleEventsTable(params: {
|
|||
kindSet,
|
||||
subtypeSet,
|
||||
fromMs,
|
||||
fromSubMs,
|
||||
toMs,
|
||||
asOfMs,
|
||||
vaultCutoffTs,
|
||||
|
|
@ -495,6 +519,11 @@ function filterEligibleEventsTable(params: {
|
|||
|
||||
const eligibleRows: TableRow[] = [];
|
||||
let prePanelVaultExcludedEvents = 0;
|
||||
// TableRow timestamps carry integer epoch-ms precision. When the effective start
|
||||
// has non-zero microseconds (fromSubMs > 0), any row stamped at `fromMs` is either
|
||||
// strictly earlier (e.g. `.123Z < .123456Z`) or ambiguous at the boundary ms, so
|
||||
// require `ts >= fromMs + 1` conservatively.
|
||||
const minTsMs = fromSubMs > 0 ? fromMs + 1 : fromMs;
|
||||
|
||||
for (let i = 0; i < table.rows.length; i += 1) {
|
||||
const row = table.rows[i];
|
||||
|
|
@ -541,7 +570,7 @@ function filterEligibleEventsTable(params: {
|
|||
}
|
||||
|
||||
if (isValidEpochMs(ts)) {
|
||||
if (ts < fromMs || ts > toMs) {
|
||||
if (ts < minTsMs || ts > toMs) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
|
|
@ -837,10 +866,11 @@ export function createEventStudyTool(options: EventStudyToolOptions): McpToolEnt
|
|||
const kindSet = new Set<string>(canonicalKinds);
|
||||
const subtypeSet = canonicalSubtypes !== undefined ? new Set<string>(canonicalSubtypes) : null;
|
||||
|
||||
const eligibleBars = filterEligibleBars(
|
||||
const { bars: eligibleBars, prePanelVaultExcludedDates } = filterEligibleBars(
|
||||
rawBars,
|
||||
listingIdSet,
|
||||
fenced.fromMs,
|
||||
fenced.fromSubMs,
|
||||
fenced.toMs,
|
||||
fenced.asOfMs,
|
||||
vaultCutoffTs,
|
||||
|
|
@ -854,6 +884,7 @@ export function createEventStudyTool(options: EventStudyToolOptions): McpToolEnt
|
|||
kindSet,
|
||||
subtypeSet,
|
||||
fromMs: fenced.fromMs,
|
||||
fromSubMs: fenced.fromSubMs,
|
||||
toMs: fenced.toMs,
|
||||
asOfMs: fenced.asOfMs,
|
||||
vaultCutoffTs,
|
||||
|
|
@ -877,6 +908,7 @@ export function createEventStudyTool(options: EventStudyToolOptions): McpToolEnt
|
|||
if (fromDate > calendarToDate) {
|
||||
const panelDiagnostics: EventPanelDiagnostics = {
|
||||
...ZERO_EVENT_PANEL_DIAGNOSTICS,
|
||||
vaultExcludedDates: prePanelVaultExcludedDates,
|
||||
vaultExcludedEvents: prePanelVaultExcludedEvents,
|
||||
droppedUnknownListing: events.length,
|
||||
};
|
||||
|
|
@ -918,6 +950,7 @@ export function createEventStudyTool(options: EventStudyToolOptions): McpToolEnt
|
|||
|
||||
const combinedPanelDiagnostics: EventPanelDiagnostics = {
|
||||
...built.diagnostics,
|
||||
vaultExcludedDates: prePanelVaultExcludedDates + built.diagnostics.vaultExcludedDates,
|
||||
vaultExcludedEvents: prePanelVaultExcludedEvents + built.diagnostics.vaultExcludedEvents,
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue