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
ee6137c770
commit
bec64c5ec6
2 changed files with 418 additions and 3 deletions
|
|
@ -2537,6 +2537,265 @@ describe('MCP analyze_signal tool — fundamentals source', () => {
|
|||
expect(futureLedger.rows).toEqual(mcpLedger.rows);
|
||||
});
|
||||
|
||||
test('same-month restatement replay retains earlier intra-month vintage through the day before the amendment', async () => {
|
||||
// Regression fixture for intra-month superseded vintages:
|
||||
// - Dataset ID: `synthetic:mcp-analyze-signal-same-month-restatement:seed-20261204:v1`
|
||||
// - 6 listings across 2023-03-01..2023-05-12 (53 sessions), Vault cutoff 2023-05-15T00:00:00Z
|
||||
// - March 10: initial Q1 release (listings 0..4 only; listing 5 has NO prior release before April 5)
|
||||
// - April 5: Q2 release for all 6 listings (strictly ascending ttm_revenue across listings)
|
||||
// - April 18: Q1 restatement (SAME month as Q2!) keeping Q2 as latest period while reversing
|
||||
// ttm_revenue cross-sectional ordering.
|
||||
// Under latest-only month-end reads, the April 30 snapshot returns only the April 18 vintage
|
||||
// and erases April 5 from April 5..17 (falling back to March for listings 0..4 and dropping
|
||||
// listing 5 completely). With intra-month backfill, April 5..17 uses the original April 5 Q2
|
||||
// values and April 18+ uses the restated values.
|
||||
const listingIds = [81_001, 81_002, 81_003, 81_004, 81_005, 81_006];
|
||||
const allDates = makeBusinessDates('2023-03-01', 53);
|
||||
const vaultCutoffDate = '2023-05-15';
|
||||
const vaultCutoffTs = makeUtcMs(vaultCutoffDate, 0);
|
||||
|
||||
const marKnownAt = makeUtcMs('2023-03-10', 13);
|
||||
const aprQ2KnownAt = makeUtcMs('2023-04-05', 13);
|
||||
const aprRestatedKnownAt = makeUtcMs('2023-04-18', 13);
|
||||
|
||||
const rand = createSeededRng(20261204);
|
||||
const bars: BarRecord[] = [];
|
||||
const prices = new Float64Array(listingIds.length).fill(40);
|
||||
for (let t = 0; t < allDates.length; t += 1) {
|
||||
const date = allDates[t]!;
|
||||
const closeTs = makeUtcMs(date, 21);
|
||||
for (let l = 0; l < listingIds.length; l += 1) {
|
||||
const ret = (l - 2.5) * 0.002 + (rand() * 2 - 1) * 0.009;
|
||||
prices[l] = Math.max(1, prices[l]! * (1 + ret));
|
||||
const close = Number(prices[l]!.toFixed(6));
|
||||
bars.push({
|
||||
listingId: listingIds[l]!,
|
||||
ts: closeTs,
|
||||
open: close,
|
||||
high: close,
|
||||
low: close,
|
||||
close,
|
||||
volume: 30_000,
|
||||
vwap: close,
|
||||
knownAt: closeTs,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const pitRows: FundamentalsPitFixtureRow[] = [];
|
||||
const expectedAllVintagesObs: SignalObservation[] = [];
|
||||
const brokenMonthEndOnlyObs: SignalObservation[] = [];
|
||||
|
||||
for (let l = 0; l < listingIds.length; l += 1) {
|
||||
const lid = listingIds[l]!;
|
||||
// March ordering: alternating/scrambled; April 5 Q2: strictly ascending; April 18 restated: strictly descending
|
||||
const marVal = 200 + ((l * 3) % listingIds.length) * 25;
|
||||
const aprQ2Val = 100 + l * 40;
|
||||
const aprRestatedVal = 400 - l * 45;
|
||||
|
||||
if (l < 5) {
|
||||
pitRows.push(
|
||||
makeFundamentalsRow({
|
||||
listingId: lid,
|
||||
effectiveKnownAt: marKnownAt,
|
||||
periodEnd: '2022-12-31',
|
||||
fiscalPeriod: 'Q1',
|
||||
revenue: marVal / 4,
|
||||
ttmRevenue: marVal,
|
||||
})
|
||||
);
|
||||
expectedAllVintagesObs.push({ listingId: lid, knownAt: marKnownAt, value: marVal });
|
||||
brokenMonthEndOnlyObs.push({ listingId: lid, knownAt: marKnownAt, value: marVal });
|
||||
}
|
||||
|
||||
pitRows.push(
|
||||
makeFundamentalsRow({
|
||||
listingId: lid,
|
||||
effectiveKnownAt: aprQ2KnownAt,
|
||||
periodEnd: '2023-03-31',
|
||||
fiscalPeriod: 'Q2',
|
||||
revenue: aprQ2Val / 4,
|
||||
ttmRevenue: aprQ2Val,
|
||||
}),
|
||||
makeFundamentalsRow({
|
||||
listingId: lid,
|
||||
effectiveKnownAt: aprRestatedKnownAt,
|
||||
periodEnd: '2023-03-31',
|
||||
fiscalPeriod: 'Q2',
|
||||
revenue: aprQ2Val / 4,
|
||||
ttmRevenue: aprRestatedVal,
|
||||
})
|
||||
);
|
||||
|
||||
expectedAllVintagesObs.push(
|
||||
{ listingId: lid, knownAt: aprQ2KnownAt, value: aprQ2Val },
|
||||
{ listingId: lid, knownAt: aprRestatedKnownAt, value: aprRestatedVal }
|
||||
);
|
||||
// Month-end-only baseline misses the April 5 vintage completely
|
||||
brokenMonthEndOnlyObs.push({ listingId: lid, knownAt: aprRestatedKnownAt, value: aprRestatedVal });
|
||||
}
|
||||
|
||||
// 1. Direct baseline with all eligible vintages (including April 5 Q2)
|
||||
const directBuilt = buildForwardReturnPanel({
|
||||
bars,
|
||||
signals: expectedAllVintagesObs,
|
||||
horizonSessions: 3,
|
||||
vaultCutoffDate,
|
||||
});
|
||||
const directLedger = createFakeTrialLedger();
|
||||
const directStudy = await analyzeSignal(directLedger.executor, {
|
||||
principalId: AGENT_PRINCIPAL,
|
||||
universeMarket: 'US_EQ',
|
||||
horizonBucket: 'swing_1_10d',
|
||||
expression: {
|
||||
expression: 'direct_same_month_baseline',
|
||||
params: { field: 'ttm_revenue', horizonSessions: 3 },
|
||||
universe: listingIds,
|
||||
horizon: '3d',
|
||||
},
|
||||
panel: directBuilt.panel,
|
||||
vaultCutoffDate,
|
||||
});
|
||||
expect(directStudy.status).toBe('ok');
|
||||
if (directStudy.status !== 'ok') {
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Broken month-end-only counterfactual (omits April 5 Q2)
|
||||
const brokenBuilt = buildForwardReturnPanel({
|
||||
bars,
|
||||
signals: brokenMonthEndOnlyObs,
|
||||
horizonSessions: 3,
|
||||
vaultCutoffDate,
|
||||
});
|
||||
const brokenLedger = createFakeTrialLedger();
|
||||
const brokenStudy = await analyzeSignal(brokenLedger.executor, {
|
||||
principalId: AGENT_PRINCIPAL,
|
||||
universeMarket: 'US_EQ',
|
||||
horizonBucket: 'swing_1_10d',
|
||||
expression: {
|
||||
expression: 'broken_month_end_only_counterfactual',
|
||||
params: { field: 'ttm_revenue', horizonSessions: 3 },
|
||||
universe: listingIds,
|
||||
horizon: '3d',
|
||||
},
|
||||
panel: brokenBuilt.panel,
|
||||
vaultCutoffDate,
|
||||
});
|
||||
expect(brokenStudy.status).toBe('ok');
|
||||
|
||||
// 3. MCP handler over multi-month window (March..May)
|
||||
const mcpStore = createFakeStore({
|
||||
bars,
|
||||
fundamentalsResolver: q => queryFundamentalsFixtureRows(pitRows, q),
|
||||
});
|
||||
const mcpLedger = createFakeTrialLedger();
|
||||
const mcpRes = await callAnalyzeSignal({
|
||||
store: mcpStore.store,
|
||||
ledger: mcpLedger.executor,
|
||||
vaultCutoffTs,
|
||||
args: {
|
||||
listingIds,
|
||||
from: allDates[0]!,
|
||||
to: allDates[allDates.length - 1]!,
|
||||
signal: { source: 'fundamentals', field: 'ttm_revenue' },
|
||||
horizonSessions: 3,
|
||||
universeMarket: 'US_EQ',
|
||||
horizonBucket: 'swing_1_10d',
|
||||
},
|
||||
});
|
||||
expect(mcpRes.isError).toBe(false);
|
||||
const mcpPayload = JSON.parse(mcpRes.text) as AnalyzeSignalResultPayload;
|
||||
expect(mcpPayload.status).toBe('ok');
|
||||
if (mcpPayload.status !== 'ok') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Exact parity with the full-vintage direct baseline across all dates (including April 5..17)
|
||||
expect(mcpPayload.analysis.rankIc).toEqual(directStudy.analysis.rankIc);
|
||||
expect(mcpPayload.analysis.pearsonIc).toEqual(directStudy.analysis.pearsonIc);
|
||||
expect(mcpLedger.rows[0]?.daily_series).toEqual(directLedger.rows[0]?.daily_series);
|
||||
|
||||
// Specifically verify April 5..17 dated points match direct baseline and differ from month-end-only
|
||||
const aprilPreAmendmentDates = mcpPayload.analysis.ledgerSeries.points
|
||||
.filter(p => p.date >= '2023-04-05' && p.date <= '2023-04-17')
|
||||
.map(p => p.date);
|
||||
expect(aprilPreAmendmentDates.length).toBeGreaterThan(0);
|
||||
expect(aprilPreAmendmentDates[0]).toBe('2023-04-05');
|
||||
expect(aprilPreAmendmentDates[aprilPreAmendmentDates.length - 1]).toBe('2023-04-17');
|
||||
|
||||
const mcpByDate = new Map(mcpPayload.analysis.ledgerSeries.points.map(p => [p.date, p.value]));
|
||||
const brokenByDate = new Map(
|
||||
brokenStudy.analysis.dates.map((d, idx) => [d, brokenStudy.analysis.rankIcSeries[idx]!])
|
||||
);
|
||||
let aprilDiffCount = 0;
|
||||
for (const d of aprilPreAmendmentDates) {
|
||||
if (mcpByDate.get(d) !== brokenByDate.get(d)) {
|
||||
aprilDiffCount += 1;
|
||||
}
|
||||
}
|
||||
expect(aprilDiffCount).toBeGreaterThan(0);
|
||||
|
||||
// Every fundamentalsAsOf call (month-end + backfill) stays strictly below Vault cutoff and forwards allowEstimatedLag
|
||||
for (const call of mcpStore.fundamentalsCalls) {
|
||||
expect(Date.parse(call.asOf)).toBeLessThan(vaultCutoffTs);
|
||||
expect(call.allowEstimatedLag).toBe(false);
|
||||
}
|
||||
|
||||
// 4. Single-month window starting in April (2023-04-03..2023-04-28):
|
||||
// must retain March prior (for Apr 3..4), April 5 Q2 (for Apr 5..17), and April 18 restatement (Apr 18..28).
|
||||
const aprilOnlyBars = bars.filter(b => b.ts >= makeUtcMs('2023-04-03', 0) && b.ts <= makeUtcMs('2023-04-28', 23));
|
||||
const aprilDirectBuilt = buildForwardReturnPanel({
|
||||
bars: aprilOnlyBars,
|
||||
signals: expectedAllVintagesObs,
|
||||
horizonSessions: 3,
|
||||
vaultCutoffDate,
|
||||
});
|
||||
const aprilDirectLedger = createFakeTrialLedger();
|
||||
const aprilDirectStudy = await analyzeSignal(aprilDirectLedger.executor, {
|
||||
principalId: AGENT_PRINCIPAL,
|
||||
universeMarket: 'US_EQ',
|
||||
horizonBucket: 'swing_1_10d',
|
||||
expression: {
|
||||
expression: 'direct_april_only_baseline',
|
||||
params: { field: 'ttm_revenue', horizonSessions: 3 },
|
||||
universe: listingIds,
|
||||
horizon: '3d',
|
||||
},
|
||||
panel: aprilDirectBuilt.panel,
|
||||
vaultCutoffDate,
|
||||
});
|
||||
expect(aprilDirectStudy.status).toBe('ok');
|
||||
|
||||
const aprilMcpStore = createFakeStore({
|
||||
bars: aprilOnlyBars,
|
||||
fundamentalsResolver: q => queryFundamentalsFixtureRows(pitRows, q),
|
||||
});
|
||||
const aprilMcpLedger = createFakeTrialLedger();
|
||||
const aprilMcpRes = await callAnalyzeSignal({
|
||||
store: aprilMcpStore.store,
|
||||
ledger: aprilMcpLedger.executor,
|
||||
vaultCutoffTs,
|
||||
args: {
|
||||
listingIds,
|
||||
from: '2023-04-03',
|
||||
to: '2023-04-28',
|
||||
signal: { source: 'fundamentals', field: 'ttm_revenue' },
|
||||
horizonSessions: 3,
|
||||
universeMarket: 'US_EQ',
|
||||
horizonBucket: 'swing_1_10d',
|
||||
},
|
||||
});
|
||||
expect(aprilMcpRes.isError).toBe(false);
|
||||
const aprilMcpPayload = JSON.parse(aprilMcpRes.text) as AnalyzeSignalResultPayload;
|
||||
expect(aprilMcpPayload.status).toBe('ok');
|
||||
if (aprilMcpPayload.status === 'ok' && aprilDirectStudy.status === 'ok') {
|
||||
expect(aprilMcpPayload.analysis.rankIc).toEqual(aprilDirectStudy.analysis.rankIc);
|
||||
expect(aprilMcpLedger.rows[0]?.daily_series).toEqual(aprilDirectLedger.rows[0]?.daily_series);
|
||||
expect(aprilMcpPayload.trial.sampleStart).toBe('2023-04-03');
|
||||
}
|
||||
});
|
||||
|
||||
test('Vault fence and defensive filtering: clean vs deliberately leaky fundamentals store produce identical payloads and ledger rows', async () => {
|
||||
const fixture = buildFundamentalsStudyFixture();
|
||||
const from = fixture.allDates[0]!;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue