T-136: M2.1e — MCP copilot read_knowledge tool: read-only methodology cards + empirical findings / provider caveats lookup via injected ports in libs/research/src/mcp/tools/
This commit is contained in:
parent
515b685ffc
commit
a98bfa8934
2 changed files with 198 additions and 35 deletions
|
|
@ -79,6 +79,7 @@ function createFakeKnowledgePort(options?: {
|
|||
readonly libraryError?: unknown;
|
||||
readonly findingsError?: unknown;
|
||||
readonly caveatsError?: unknown;
|
||||
readonly applySelectors?: boolean;
|
||||
}): FakePortState {
|
||||
const libraryCalls: LibraryCardQuery[] = [];
|
||||
const findingsCalls: FindingQuery[] = [];
|
||||
|
|
@ -88,6 +89,7 @@ function createFakeKnowledgePort(options?: {
|
|||
const libraryCards = options?.libraryCards ?? [];
|
||||
const findings = options?.findings ?? [];
|
||||
const providerCaveats = options?.providerCaveats ?? [];
|
||||
const applySelectors = options?.applySelectors === true;
|
||||
|
||||
const rawPort: KnowledgePort = {
|
||||
async listLibraryCards(q: LibraryCardQuery): Promise<readonly LibraryCardRecord[]> {
|
||||
|
|
@ -95,21 +97,83 @@ function createFakeKnowledgePort(options?: {
|
|||
if (options?.libraryError !== undefined) {
|
||||
throw options.libraryError;
|
||||
}
|
||||
return [...libraryCards];
|
||||
if (!applySelectors) {
|
||||
return [...libraryCards];
|
||||
}
|
||||
return libraryCards.filter(card => {
|
||||
if (q.id !== undefined && card.id !== q.id) {
|
||||
return false;
|
||||
}
|
||||
if (q.tags !== undefined) {
|
||||
const cardTags = new Set(card.tags.map(t => t.toLowerCase()));
|
||||
if (!q.tags.every(t => cardTags.has(t.toLowerCase()))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (q.query !== undefined) {
|
||||
const needle = q.query.toLowerCase();
|
||||
const inText =
|
||||
card.title.toLowerCase().includes(needle) ||
|
||||
card.summary.toLowerCase().includes(needle) ||
|
||||
card.tags.some(t => t.toLowerCase().includes(needle));
|
||||
if (!inText) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
},
|
||||
async listFindings(q: FindingQuery): Promise<readonly FindingRecord[]> {
|
||||
findingsCalls.push({ ...q });
|
||||
if (options?.findingsError !== undefined) {
|
||||
throw options.findingsError;
|
||||
}
|
||||
return [...findings];
|
||||
if (!applySelectors) {
|
||||
return [...findings];
|
||||
}
|
||||
return findings.filter(finding => {
|
||||
if (q.id !== undefined && finding.id !== q.id) {
|
||||
return false;
|
||||
}
|
||||
if (q.query !== undefined) {
|
||||
const needle = q.query.toLowerCase();
|
||||
const inText =
|
||||
finding.title.toLowerCase().includes(needle) ||
|
||||
finding.summary.toLowerCase().includes(needle);
|
||||
if (!inText) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
},
|
||||
async listProviderCaveats(q: ProviderCaveatQuery): Promise<readonly ProviderCaveatRecord[]> {
|
||||
caveatsCalls.push({ ...q });
|
||||
if (options?.caveatsError !== undefined) {
|
||||
throw options.caveatsError;
|
||||
}
|
||||
return [...providerCaveats];
|
||||
if (!applySelectors) {
|
||||
return [...providerCaveats];
|
||||
}
|
||||
return providerCaveats.filter(caveat => {
|
||||
if (q.id !== undefined && caveat.id !== q.id) {
|
||||
return false;
|
||||
}
|
||||
if (q.provider !== undefined && caveat.provider.toLowerCase() !== q.provider.toLowerCase()) {
|
||||
return false;
|
||||
}
|
||||
if (q.query !== undefined) {
|
||||
const needle = q.query.toLowerCase();
|
||||
const inText =
|
||||
caveat.provider.toLowerCase().includes(needle) ||
|
||||
caveat.dataset.toLowerCase().includes(needle) ||
|
||||
caveat.caveat.toLowerCase().includes(needle);
|
||||
if (!inText) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -526,6 +590,112 @@ describe('MCP read_knowledge tool', () => {
|
|||
});
|
||||
});
|
||||
|
||||
test('omits query from port selectors when id is supplied so selector-filtering ports return exact matches', async () => {
|
||||
const filteringFake = createFakeKnowledgePort({
|
||||
applySelectors: true,
|
||||
libraryCards: [
|
||||
makeLibraryCard({
|
||||
id: 'card-cpcv',
|
||||
title: 'Combinatorial Purged Cross-Validation',
|
||||
tags: ['validation'],
|
||||
summary: 'Purge and embargo overlapping labels across folds.',
|
||||
}),
|
||||
],
|
||||
findings: [
|
||||
makeFinding({
|
||||
id: 'card-cpcv',
|
||||
title: 'CPCV split stability',
|
||||
summary: 'No mention of unrelated factors.',
|
||||
}),
|
||||
],
|
||||
providerCaveats: [
|
||||
makeCaveat({
|
||||
id: 'card-cpcv',
|
||||
provider: 'eodhd',
|
||||
dataset: 'eodPrices',
|
||||
caveat: 'Backward split adjustments only.',
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
const res = await callReadKnowledge(filteringFake, {
|
||||
id: 'card-cpcv',
|
||||
query: 'momentum',
|
||||
tags: ['validation'],
|
||||
provider: 'eodhd',
|
||||
});
|
||||
|
||||
expect(res.isError).toBe(false);
|
||||
expect(filteringFake.libraryCalls).toEqual([{ id: 'card-cpcv', tags: ['validation'] }]);
|
||||
expect(filteringFake.findingsCalls).toEqual([{ id: 'card-cpcv' }]);
|
||||
expect(filteringFake.caveatsCalls).toEqual([{ id: 'card-cpcv', provider: 'eodhd' }]);
|
||||
expect(res.payload?.rowCount).toBe(3);
|
||||
expect(res.payload?.sources).toEqual({ library: 1, findings: 1, provider_caveats: 1 });
|
||||
expect(res.payload?.rows.map(r => `${r.source}:${r.id}`)).toEqual([
|
||||
'library:card-cpcv',
|
||||
'findings:card-cpcv',
|
||||
'provider_caveats:card-cpcv',
|
||||
]);
|
||||
});
|
||||
|
||||
test('does not grant exact-ID priority to free-text query matches and stays invariant to query capitalization', async () => {
|
||||
const fake = createFakeKnowledgePort({
|
||||
libraryCards: [
|
||||
makeLibraryCard({
|
||||
id: 'momentum',
|
||||
title: 'Factor Crowding Notes',
|
||||
tags: ['risk'],
|
||||
summary: 'Discusses momentum drawdown regimes.',
|
||||
}),
|
||||
makeLibraryCard({
|
||||
id: 'z-card',
|
||||
title: 'Momentum Basics',
|
||||
tags: ['factor'],
|
||||
summary: 'Introductory overview.',
|
||||
}),
|
||||
],
|
||||
findings: [
|
||||
makeFinding({
|
||||
id: 'momentum',
|
||||
title: 'Regime Decay',
|
||||
summary: 'Observed momentum turnover costs.',
|
||||
}),
|
||||
makeFinding({
|
||||
id: 'z-finding',
|
||||
title: 'Momentum Persistence',
|
||||
summary: 'Observed over 2015–2024.',
|
||||
}),
|
||||
],
|
||||
providerCaveats: [
|
||||
makeCaveat({
|
||||
id: 'momentum',
|
||||
provider: 'eodhd',
|
||||
dataset: 'eodPrices',
|
||||
caveat: 'Affects momentum backtests.',
|
||||
}),
|
||||
makeCaveat({
|
||||
id: 'z-caveat',
|
||||
provider: 'eodhd',
|
||||
dataset: 'momentum_signals',
|
||||
caveat: 'Adjusted series.',
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
for (const source of ['library', 'findings', 'provider_caveats'] as const) {
|
||||
const lowerTop = await callReadKnowledge(fake, { source, query: 'momentum', limit: 1 });
|
||||
const upperTop = await callReadKnowledge(fake, { source, query: 'MOMENTUM', limit: 1 });
|
||||
|
||||
expect(lowerTop.isError).toBe(false);
|
||||
expect(upperTop.isError).toBe(false);
|
||||
expect(lowerTop.payload).toEqual(upperTop.payload);
|
||||
|
||||
const expectedId =
|
||||
source === 'library' ? 'z-card' : source === 'findings' ? 'z-finding' : 'z-caveat';
|
||||
expect(lowerTop.payload?.rows.map(r => r.id)).toEqual([expectedId]);
|
||||
}
|
||||
});
|
||||
|
||||
test('filters case-insensitively across titles, tags, summaries, caveats, and source-specific filters', async () => {
|
||||
const fake = createFakeKnowledgePort({
|
||||
libraryCards: [
|
||||
|
|
@ -777,7 +947,7 @@ describe('MCP read_knowledge tool', () => {
|
|||
}),
|
||||
makeLibraryCard({
|
||||
id: 'lib-over-summary-needle',
|
||||
title: 'Late Needle Summary',
|
||||
title: 'Late Match Summary',
|
||||
summary: overCapWithTailNeedle,
|
||||
body: 'short body',
|
||||
}),
|
||||
|
|
@ -790,7 +960,7 @@ describe('MCP read_knowledge tool', () => {
|
|||
}),
|
||||
makeFinding({
|
||||
id: 'fin-over-needle',
|
||||
title: 'Late Needle Finding',
|
||||
title: 'Late Match Finding',
|
||||
summary: overCapWithTailNeedle,
|
||||
}),
|
||||
],
|
||||
|
|
@ -840,9 +1010,9 @@ describe('MCP read_knowledge tool', () => {
|
|||
const needleRes = await callReadKnowledge(fake, { query: 'needle' });
|
||||
expect(needleRes.isError).toBe(false);
|
||||
expect(needleRes.payload?.rows.map(r => `${r.source}:${r.id}`)).toEqual([
|
||||
'provider_caveats:cav-over-needle',
|
||||
'findings:fin-over-needle',
|
||||
'library:lib-over-summary-needle',
|
||||
'provider_caveats:cav-over-needle',
|
||||
]);
|
||||
for (const row of needleRes.payload?.rows ?? []) {
|
||||
expect(row.truncated).toBe(true);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue