T-149: M2.1h — MCP copilot analyze_signal: add a Vault-fenced fundamentals signal source (point-in-time fundamentalsAsOf month-end snapshots → SignalObservations) + extend the headless stdio end-to-end test to run a fundamental AND a shorts study (pure, injected ports, no new deps)

This commit is contained in:
Studio 2026-12-01 23:39:37 -05:00
parent 1d9a8fc634
commit ee826bdef4
2 changed files with 392 additions and 17 deletions

View file

@ -1002,6 +1002,7 @@ export function createAnalyzeSignalTool(options: AnalyzeSignalToolOptions): McpT
);
signals = shortsTableToSignalRows(eligibleShortsTable, input.signal.field);
} else {
const { field: fundamentalsField, allowEstimatedLag } = input.signal;
const snapshotDates = deriveMonthlySnapshotDates(eligibleBars);
if (snapshotDates.length > ANALYZE_SIGNAL_MAX_FUNDAMENTAL_SNAPSHOTS) {
return {
@ -1028,13 +1029,13 @@ export function createAnalyzeSignalTool(options: AnalyzeSignalToolOptions): McpT
);
const rawTable = await store.fundamentalsAsOf({
listingIds,
fields: [input.signal.field],
fields: [fundamentalsField],
asOf: snapshot.asOf,
allowEstimatedLag: input.signal.allowEstimatedLag,
allowEstimatedLag,
});
const latestKnownByListing = appendEligibleFundamentalsObservations({
table: rawTable,
field: input.signal.field,
field: fundamentalsField,
listingIdSet,
snapshotAsOfMs: snapshot.asOfMs,
fencedAsOfMs: fenced.asOfMs,
@ -1046,17 +1047,27 @@ export function createAnalyzeSignalTool(options: AnalyzeSignalToolOptions): McpT
// Backfill vintages superseded earlier within this month (or prior
// to the first eligible bar for the initial month) by stepping to
// the latest uncovered eligible bar strictly before each returned
// vintage. Listings sharing a target bar timestamp are batched.
// vintage. When `allowEstimatedLag` is false, DuckDB omits listings
// whose latest vintage uses estimated publication lag, so treat
// omitted listings as unresolved intervals and probe earlier
// eligible closes in the month. Listings sharing a target bar
// timestamp are batched.
const pendingByBarTs = new Map<number, number[]>();
const enqueueBackfill = (
queriedListingIds: readonly number[],
knownByListing: ReadonlyMap<number, number>,
upperBoundExclusiveMs?: number
upperBoundExclusiveMs: number
): void => {
for (const [lid, knownAt] of knownByListing) {
const upperExclusive =
upperBoundExclusiveMs !== undefined
? Math.min(knownAt, upperBoundExclusiveMs)
: knownAt;
for (const lid of queriedListingIds) {
const knownAt = knownByListing.get(lid);
let upperExclusive: number;
if (knownAt !== undefined) {
upperExclusive = Math.min(knownAt, upperBoundExclusiveMs);
} else if (!allowEstimatedLag) {
upperExclusive = upperBoundExclusiveMs;
} else {
continue;
}
const prevBarTs = findLatestBarBefore(
barTimestampsByListing.get(lid),
upperExclusive,
@ -1073,7 +1084,7 @@ export function createAnalyzeSignalTool(options: AnalyzeSignalToolOptions): McpT
}
};
enqueueBackfill(latestKnownByListing);
enqueueBackfill(listingIds, latestKnownByListing, snapshot.asOfMs);
while (pendingByBarTs.size > 0) {
// Pop the highest bar timestamp first so earlier buckets merge
@ -1099,13 +1110,13 @@ export function createAnalyzeSignalTool(options: AnalyzeSignalToolOptions): McpT
const backfillTable = await store.fundamentalsAsOf({
listingIds: batchListingIds,
fields: [input.signal.field],
fields: [fundamentalsField],
asOf: batchAsOf,
allowEstimatedLag: input.signal.allowEstimatedLag,
allowEstimatedLag,
});
const backfillKnownByListing = appendEligibleFundamentalsObservations({
table: backfillTable,
field: input.signal.field,
field: fundamentalsField,
listingIdSet: batchListingIdSet,
snapshotAsOfMs: batchAsOfMs,
fencedAsOfMs: fenced.asOfMs,
@ -1114,7 +1125,7 @@ export function createAnalyzeSignalTool(options: AnalyzeSignalToolOptions): McpT
out: signals,
});
enqueueBackfill(backfillKnownByListing, nextBarTs);
enqueueBackfill(batchListingIds, backfillKnownByListing, nextBarTs);
}
windowFloorMs = snapshot.asOfMs;