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:
parent
b49bbd59b4
commit
1d9a8fc634
2 changed files with 418 additions and 3 deletions
|
|
@ -579,6 +579,69 @@ function resolveSnapshotAsOf(
|
|||
};
|
||||
}
|
||||
|
||||
function buildBarTimestampsByListing(bars: readonly BarRecord[]): Map<number, number[]> {
|
||||
const byListing = new Map<number, number[]>();
|
||||
for (let i = 0; i < bars.length; i += 1) {
|
||||
const bar = bars[i];
|
||||
if (
|
||||
typeof bar !== 'object' ||
|
||||
bar === null ||
|
||||
typeof bar.listingId !== 'number' ||
|
||||
!Number.isSafeInteger(bar.listingId) ||
|
||||
typeof bar.ts !== 'number' ||
|
||||
!Number.isFinite(bar.ts)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
let list = byListing.get(bar.listingId);
|
||||
if (list === undefined) {
|
||||
list = [];
|
||||
byListing.set(bar.listingId, list);
|
||||
}
|
||||
list.push(bar.ts);
|
||||
}
|
||||
for (const [lid, list] of byListing) {
|
||||
list.sort((a, b) => a - b);
|
||||
let write = 0;
|
||||
for (let i = 0; i < list.length; i += 1) {
|
||||
const ts = list[i] as number;
|
||||
if (write === 0 || list[write - 1] !== ts) {
|
||||
list[write] = ts;
|
||||
write += 1;
|
||||
}
|
||||
}
|
||||
list.length = write;
|
||||
byListing.set(lid, list);
|
||||
}
|
||||
return byListing;
|
||||
}
|
||||
|
||||
function findLatestBarBefore(
|
||||
sortedTs: readonly number[] | undefined,
|
||||
upperExclusiveMs: number,
|
||||
lowerExclusiveMs: number
|
||||
): number | null {
|
||||
if (sortedTs === undefined || sortedTs.length === 0) {
|
||||
return null;
|
||||
}
|
||||
let lo = 0;
|
||||
let hi = sortedTs.length;
|
||||
while (lo < hi) {
|
||||
const mid = (lo + hi) >>> 1;
|
||||
if ((sortedTs[mid] as number) < upperExclusiveMs) {
|
||||
lo = mid + 1;
|
||||
} else {
|
||||
hi = mid;
|
||||
}
|
||||
}
|
||||
const idx = lo - 1;
|
||||
if (idx < 0) {
|
||||
return null;
|
||||
}
|
||||
const candidate = sortedTs[idx] as number;
|
||||
return candidate > lowerExclusiveMs ? candidate : null;
|
||||
}
|
||||
|
||||
function appendEligibleFundamentalsObservations(params: {
|
||||
readonly table: Table;
|
||||
readonly field: FundamentalsPitValueField;
|
||||
|
|
@ -588,7 +651,7 @@ function appendEligibleFundamentalsObservations(params: {
|
|||
readonly vaultCutoffTs: number;
|
||||
readonly seenKeys: Set<string>;
|
||||
readonly out: SignalObservation[];
|
||||
}): void {
|
||||
}): Map<number, number> {
|
||||
const {
|
||||
table,
|
||||
field,
|
||||
|
|
@ -612,6 +675,8 @@ function appendEligibleFundamentalsObservations(params: {
|
|||
);
|
||||
}
|
||||
|
||||
const latestKnownAtByListing = new Map<number, number>();
|
||||
|
||||
for (let i = 0; i < table.rows.length; i += 1) {
|
||||
const row = table.rows[i];
|
||||
if (typeof row !== 'object' || row === null) {
|
||||
|
|
@ -640,12 +705,22 @@ function appendEligibleFundamentalsObservations(params: {
|
|||
}
|
||||
|
||||
const rawValue = row[field];
|
||||
if (typeof rawValue !== 'number' || !Number.isFinite(rawValue)) {
|
||||
const isFiniteValue = typeof rawValue === 'number' && Number.isFinite(rawValue);
|
||||
if (!isFiniteValue && rawValue !== null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const normalizedListingId = listingId === 0 ? 0 : listingId;
|
||||
const normalizedKnownAt = knownAt === 0 ? 0 : knownAt;
|
||||
const prevLatest = latestKnownAtByListing.get(normalizedListingId);
|
||||
if (prevLatest === undefined || normalizedKnownAt > prevLatest) {
|
||||
latestKnownAtByListing.set(normalizedListingId, normalizedKnownAt);
|
||||
}
|
||||
|
||||
if (!isFiniteValue) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const normalizedValue = rawValue === 0 ? 0 : rawValue;
|
||||
const dedupeKey = `${normalizedListingId}:${normalizedKnownAt}`;
|
||||
if (seenKeys.has(dedupeKey)) {
|
||||
|
|
@ -658,6 +733,8 @@ function appendEligibleFundamentalsObservations(params: {
|
|||
value: normalizedValue,
|
||||
});
|
||||
}
|
||||
|
||||
return latestKnownAtByListing;
|
||||
}
|
||||
|
||||
function buildCanonicalStudyExpression(params: {
|
||||
|
|
@ -940,6 +1017,9 @@ export function createAnalyzeSignalTool(options: AnalyzeSignalToolOptions): McpT
|
|||
|
||||
signals = [];
|
||||
const seenKeys = new Set<string>();
|
||||
const barTimestampsByListing = buildBarTimestampsByListing(eligibleBars);
|
||||
let windowFloorMs = Number.NEGATIVE_INFINITY;
|
||||
|
||||
for (const snapshotDate of snapshotDates) {
|
||||
const snapshot = resolveSnapshotAsOf(
|
||||
snapshotDate,
|
||||
|
|
@ -952,7 +1032,7 @@ export function createAnalyzeSignalTool(options: AnalyzeSignalToolOptions): McpT
|
|||
asOf: snapshot.asOf,
|
||||
allowEstimatedLag: input.signal.allowEstimatedLag,
|
||||
});
|
||||
appendEligibleFundamentalsObservations({
|
||||
const latestKnownByListing = appendEligibleFundamentalsObservations({
|
||||
table: rawTable,
|
||||
field: input.signal.field,
|
||||
listingIdSet,
|
||||
|
|
@ -962,6 +1042,82 @@ export function createAnalyzeSignalTool(options: AnalyzeSignalToolOptions): McpT
|
|||
seenKeys,
|
||||
out: signals,
|
||||
});
|
||||
|
||||
// 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.
|
||||
const pendingByBarTs = new Map<number, number[]>();
|
||||
const enqueueBackfill = (
|
||||
knownByListing: ReadonlyMap<number, number>,
|
||||
upperBoundExclusiveMs?: number
|
||||
): void => {
|
||||
for (const [lid, knownAt] of knownByListing) {
|
||||
const upperExclusive =
|
||||
upperBoundExclusiveMs !== undefined
|
||||
? Math.min(knownAt, upperBoundExclusiveMs)
|
||||
: knownAt;
|
||||
const prevBarTs = findLatestBarBefore(
|
||||
barTimestampsByListing.get(lid),
|
||||
upperExclusive,
|
||||
windowFloorMs
|
||||
);
|
||||
if (prevBarTs !== null) {
|
||||
let bucket = pendingByBarTs.get(prevBarTs);
|
||||
if (bucket === undefined) {
|
||||
bucket = [];
|
||||
pendingByBarTs.set(prevBarTs, bucket);
|
||||
}
|
||||
bucket.push(lid);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
enqueueBackfill(latestKnownByListing);
|
||||
|
||||
while (pendingByBarTs.size > 0) {
|
||||
// Pop the highest bar timestamp first so earlier buckets merge
|
||||
// newly discovered listings landing on the same session.
|
||||
let nextBarTs = Number.NEGATIVE_INFINITY;
|
||||
for (const ts of pendingByBarTs.keys()) {
|
||||
if (ts > nextBarTs) {
|
||||
nextBarTs = ts;
|
||||
}
|
||||
}
|
||||
const batchListingIds = sortAndDedupeListingIds(pendingByBarTs.get(nextBarTs) ?? []);
|
||||
pendingByBarTs.delete(nextBarTs);
|
||||
if (batchListingIds.length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const batchAsOfMs = Math.min(nextBarTs, fenced.asOfMs);
|
||||
const batchAsOf =
|
||||
batchAsOfMs === fenced.asOfMs
|
||||
? fenced.canonicalAsOf
|
||||
: formatUtcIsoWithMicros(batchAsOfMs, 0);
|
||||
const batchListingIdSet = new Set<number>(batchListingIds);
|
||||
|
||||
const backfillTable = await store.fundamentalsAsOf({
|
||||
listingIds: batchListingIds,
|
||||
fields: [input.signal.field],
|
||||
asOf: batchAsOf,
|
||||
allowEstimatedLag: input.signal.allowEstimatedLag,
|
||||
});
|
||||
const backfillKnownByListing = appendEligibleFundamentalsObservations({
|
||||
table: backfillTable,
|
||||
field: input.signal.field,
|
||||
listingIdSet: batchListingIdSet,
|
||||
snapshotAsOfMs: batchAsOfMs,
|
||||
fencedAsOfMs: fenced.asOfMs,
|
||||
vaultCutoffTs,
|
||||
seenKeys,
|
||||
out: signals,
|
||||
});
|
||||
|
||||
enqueueBackfill(backfillKnownByListing, nextBarTs);
|
||||
}
|
||||
|
||||
windowFloorMs = snapshot.asOfMs;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue