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
|
|
@ -313,15 +313,15 @@ function matchesAllTags(
|
|||
|
||||
function rankLibraryCard(
|
||||
card: LibraryCardRecord,
|
||||
input: ReadKnowledgeInput,
|
||||
exactId: string | undefined,
|
||||
normalizedQuery: string | null,
|
||||
requiredLowerTags: readonly string[] | null
|
||||
): number | null {
|
||||
if (requiredLowerTags !== null && !matchesAllTags(card.tags, requiredLowerTags)) {
|
||||
return null;
|
||||
}
|
||||
if (input.id !== undefined) {
|
||||
return card.id === input.id ? 0 : null;
|
||||
if (exactId !== undefined) {
|
||||
return card.id === exactId ? 0 : null;
|
||||
}
|
||||
if (normalizedQuery === null) {
|
||||
return 2;
|
||||
|
|
@ -332,19 +332,16 @@ function rankLibraryCard(
|
|||
if (!titleMatches && !tagMatches && !summaryMatches) {
|
||||
return null;
|
||||
}
|
||||
if (card.id === input.query) {
|
||||
return 0;
|
||||
}
|
||||
return titleMatches ? 1 : 2;
|
||||
}
|
||||
|
||||
function rankFinding(
|
||||
finding: FindingRecord,
|
||||
input: ReadKnowledgeInput,
|
||||
exactId: string | undefined,
|
||||
normalizedQuery: string | null
|
||||
): number | null {
|
||||
if (input.id !== undefined) {
|
||||
return finding.id === input.id ? 0 : null;
|
||||
if (exactId !== undefined) {
|
||||
return finding.id === exactId ? 0 : null;
|
||||
}
|
||||
if (normalizedQuery === null) {
|
||||
return 2;
|
||||
|
|
@ -354,23 +351,20 @@ function rankFinding(
|
|||
if (!titleMatches && !summaryMatches) {
|
||||
return null;
|
||||
}
|
||||
if (finding.id === input.query) {
|
||||
return 0;
|
||||
}
|
||||
return titleMatches ? 1 : 2;
|
||||
}
|
||||
|
||||
function rankProviderCaveat(
|
||||
caveat: ProviderCaveatRecord,
|
||||
input: ReadKnowledgeInput,
|
||||
exactId: string | undefined,
|
||||
normalizedQuery: string | null,
|
||||
normalizedProvider: string | null
|
||||
): number | null {
|
||||
if (normalizedProvider !== null && caveat.provider.toLowerCase() !== normalizedProvider) {
|
||||
return null;
|
||||
}
|
||||
if (input.id !== undefined) {
|
||||
return caveat.id === input.id ? 0 : null;
|
||||
if (exactId !== undefined) {
|
||||
return caveat.id === exactId ? 0 : null;
|
||||
}
|
||||
if (normalizedQuery === null) {
|
||||
return 2;
|
||||
|
|
@ -381,9 +375,6 @@ function rankProviderCaveat(
|
|||
if (!datasetMatches && !providerMatches && !caveatMatches) {
|
||||
return null;
|
||||
}
|
||||
if (caveat.id === input.query) {
|
||||
return 0;
|
||||
}
|
||||
return datasetMatches ? 1 : 2;
|
||||
}
|
||||
|
||||
|
|
@ -479,18 +470,20 @@ export function createReadKnowledgeTool(options: ReadKnowledgeToolOptions): McpT
|
|||
const wantFindings = input.source === 'all' || input.source === 'findings';
|
||||
const wantCaveats = input.source === 'all' || input.source === 'provider_caveats';
|
||||
|
||||
const baseQuery: FindingQuery =
|
||||
input.id !== undefined
|
||||
? { id: input.id }
|
||||
: input.query !== undefined
|
||||
? { query: input.query }
|
||||
: {};
|
||||
|
||||
const libraryQuery: LibraryCardQuery = {
|
||||
...(input.id !== undefined ? { id: input.id } : {}),
|
||||
...(input.query !== undefined ? { query: input.query } : {}),
|
||||
...baseQuery,
|
||||
...(input.tags !== undefined ? { tags: input.tags } : {}),
|
||||
};
|
||||
const findingQuery: FindingQuery = {
|
||||
...(input.id !== undefined ? { id: input.id } : {}),
|
||||
...(input.query !== undefined ? { query: input.query } : {}),
|
||||
};
|
||||
const findingQuery: FindingQuery = baseQuery;
|
||||
const caveatQuery: ProviderCaveatQuery = {
|
||||
...(input.id !== undefined ? { id: input.id } : {}),
|
||||
...(input.query !== undefined ? { query: input.query } : {}),
|
||||
...baseQuery,
|
||||
...(input.provider !== undefined ? { provider: input.provider } : {}),
|
||||
};
|
||||
|
||||
|
|
@ -515,7 +508,7 @@ export function createReadKnowledgeTool(options: ReadKnowledgeToolOptions): McpT
|
|||
}
|
||||
for (let i = 0; i < rawLibrary.length; i += 1) {
|
||||
const card = validateLibraryCard(rawLibrary[i] as LibraryCardRecord);
|
||||
const rank = rankLibraryCard(card, input, normalizedQuery, requiredLowerTags);
|
||||
const rank = rankLibraryCard(card, input.id, normalizedQuery, requiredLowerTags);
|
||||
if (rank !== null) {
|
||||
eligible.push({ rank, row: projectLibraryCard(card) });
|
||||
}
|
||||
|
|
@ -528,7 +521,7 @@ export function createReadKnowledgeTool(options: ReadKnowledgeToolOptions): McpT
|
|||
}
|
||||
for (let i = 0; i < rawFindings.length; i += 1) {
|
||||
const finding = validateFinding(rawFindings[i] as FindingRecord);
|
||||
const rank = rankFinding(finding, input, normalizedQuery);
|
||||
const rank = rankFinding(finding, input.id, normalizedQuery);
|
||||
if (rank !== null) {
|
||||
eligible.push({ rank, row: projectFinding(finding) });
|
||||
}
|
||||
|
|
@ -541,7 +534,7 @@ export function createReadKnowledgeTool(options: ReadKnowledgeToolOptions): McpT
|
|||
}
|
||||
for (let i = 0; i < rawCaveats.length; i += 1) {
|
||||
const caveat = validateProviderCaveat(rawCaveats[i] as ProviderCaveatRecord);
|
||||
const rank = rankProviderCaveat(caveat, input, normalizedQuery, normalizedProvider);
|
||||
const rank = rankProviderCaveat(caveat, input.id, normalizedQuery, normalizedProvider);
|
||||
if (rank !== null) {
|
||||
eligible.push({ rank, row: projectProviderCaveat(caveat) });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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