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(
|
function rankLibraryCard(
|
||||||
card: LibraryCardRecord,
|
card: LibraryCardRecord,
|
||||||
input: ReadKnowledgeInput,
|
exactId: string | undefined,
|
||||||
normalizedQuery: string | null,
|
normalizedQuery: string | null,
|
||||||
requiredLowerTags: readonly string[] | null
|
requiredLowerTags: readonly string[] | null
|
||||||
): number | null {
|
): number | null {
|
||||||
if (requiredLowerTags !== null && !matchesAllTags(card.tags, requiredLowerTags)) {
|
if (requiredLowerTags !== null && !matchesAllTags(card.tags, requiredLowerTags)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (input.id !== undefined) {
|
if (exactId !== undefined) {
|
||||||
return card.id === input.id ? 0 : null;
|
return card.id === exactId ? 0 : null;
|
||||||
}
|
}
|
||||||
if (normalizedQuery === null) {
|
if (normalizedQuery === null) {
|
||||||
return 2;
|
return 2;
|
||||||
|
|
@ -332,19 +332,16 @@ function rankLibraryCard(
|
||||||
if (!titleMatches && !tagMatches && !summaryMatches) {
|
if (!titleMatches && !tagMatches && !summaryMatches) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (card.id === input.query) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return titleMatches ? 1 : 2;
|
return titleMatches ? 1 : 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
function rankFinding(
|
function rankFinding(
|
||||||
finding: FindingRecord,
|
finding: FindingRecord,
|
||||||
input: ReadKnowledgeInput,
|
exactId: string | undefined,
|
||||||
normalizedQuery: string | null
|
normalizedQuery: string | null
|
||||||
): number | null {
|
): number | null {
|
||||||
if (input.id !== undefined) {
|
if (exactId !== undefined) {
|
||||||
return finding.id === input.id ? 0 : null;
|
return finding.id === exactId ? 0 : null;
|
||||||
}
|
}
|
||||||
if (normalizedQuery === null) {
|
if (normalizedQuery === null) {
|
||||||
return 2;
|
return 2;
|
||||||
|
|
@ -354,23 +351,20 @@ function rankFinding(
|
||||||
if (!titleMatches && !summaryMatches) {
|
if (!titleMatches && !summaryMatches) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (finding.id === input.query) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return titleMatches ? 1 : 2;
|
return titleMatches ? 1 : 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
function rankProviderCaveat(
|
function rankProviderCaveat(
|
||||||
caveat: ProviderCaveatRecord,
|
caveat: ProviderCaveatRecord,
|
||||||
input: ReadKnowledgeInput,
|
exactId: string | undefined,
|
||||||
normalizedQuery: string | null,
|
normalizedQuery: string | null,
|
||||||
normalizedProvider: string | null
|
normalizedProvider: string | null
|
||||||
): number | null {
|
): number | null {
|
||||||
if (normalizedProvider !== null && caveat.provider.toLowerCase() !== normalizedProvider) {
|
if (normalizedProvider !== null && caveat.provider.toLowerCase() !== normalizedProvider) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (input.id !== undefined) {
|
if (exactId !== undefined) {
|
||||||
return caveat.id === input.id ? 0 : null;
|
return caveat.id === exactId ? 0 : null;
|
||||||
}
|
}
|
||||||
if (normalizedQuery === null) {
|
if (normalizedQuery === null) {
|
||||||
return 2;
|
return 2;
|
||||||
|
|
@ -381,9 +375,6 @@ function rankProviderCaveat(
|
||||||
if (!datasetMatches && !providerMatches && !caveatMatches) {
|
if (!datasetMatches && !providerMatches && !caveatMatches) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (caveat.id === input.query) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return datasetMatches ? 1 : 2;
|
return datasetMatches ? 1 : 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -479,18 +470,20 @@ export function createReadKnowledgeTool(options: ReadKnowledgeToolOptions): McpT
|
||||||
const wantFindings = input.source === 'all' || input.source === 'findings';
|
const wantFindings = input.source === 'all' || input.source === 'findings';
|
||||||
const wantCaveats = input.source === 'all' || input.source === 'provider_caveats';
|
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 = {
|
const libraryQuery: LibraryCardQuery = {
|
||||||
...(input.id !== undefined ? { id: input.id } : {}),
|
...baseQuery,
|
||||||
...(input.query !== undefined ? { query: input.query } : {}),
|
|
||||||
...(input.tags !== undefined ? { tags: input.tags } : {}),
|
...(input.tags !== undefined ? { tags: input.tags } : {}),
|
||||||
};
|
};
|
||||||
const findingQuery: FindingQuery = {
|
const findingQuery: FindingQuery = baseQuery;
|
||||||
...(input.id !== undefined ? { id: input.id } : {}),
|
|
||||||
...(input.query !== undefined ? { query: input.query } : {}),
|
|
||||||
};
|
|
||||||
const caveatQuery: ProviderCaveatQuery = {
|
const caveatQuery: ProviderCaveatQuery = {
|
||||||
...(input.id !== undefined ? { id: input.id } : {}),
|
...baseQuery,
|
||||||
...(input.query !== undefined ? { query: input.query } : {}),
|
|
||||||
...(input.provider !== undefined ? { provider: input.provider } : {}),
|
...(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) {
|
for (let i = 0; i < rawLibrary.length; i += 1) {
|
||||||
const card = validateLibraryCard(rawLibrary[i] as LibraryCardRecord);
|
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) {
|
if (rank !== null) {
|
||||||
eligible.push({ rank, row: projectLibraryCard(card) });
|
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) {
|
for (let i = 0; i < rawFindings.length; i += 1) {
|
||||||
const finding = validateFinding(rawFindings[i] as FindingRecord);
|
const finding = validateFinding(rawFindings[i] as FindingRecord);
|
||||||
const rank = rankFinding(finding, input, normalizedQuery);
|
const rank = rankFinding(finding, input.id, normalizedQuery);
|
||||||
if (rank !== null) {
|
if (rank !== null) {
|
||||||
eligible.push({ rank, row: projectFinding(finding) });
|
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) {
|
for (let i = 0; i < rawCaveats.length; i += 1) {
|
||||||
const caveat = validateProviderCaveat(rawCaveats[i] as ProviderCaveatRecord);
|
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) {
|
if (rank !== null) {
|
||||||
eligible.push({ rank, row: projectProviderCaveat(caveat) });
|
eligible.push({ rank, row: projectProviderCaveat(caveat) });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,7 @@ function createFakeKnowledgePort(options?: {
|
||||||
readonly libraryError?: unknown;
|
readonly libraryError?: unknown;
|
||||||
readonly findingsError?: unknown;
|
readonly findingsError?: unknown;
|
||||||
readonly caveatsError?: unknown;
|
readonly caveatsError?: unknown;
|
||||||
|
readonly applySelectors?: boolean;
|
||||||
}): FakePortState {
|
}): FakePortState {
|
||||||
const libraryCalls: LibraryCardQuery[] = [];
|
const libraryCalls: LibraryCardQuery[] = [];
|
||||||
const findingsCalls: FindingQuery[] = [];
|
const findingsCalls: FindingQuery[] = [];
|
||||||
|
|
@ -88,6 +89,7 @@ function createFakeKnowledgePort(options?: {
|
||||||
const libraryCards = options?.libraryCards ?? [];
|
const libraryCards = options?.libraryCards ?? [];
|
||||||
const findings = options?.findings ?? [];
|
const findings = options?.findings ?? [];
|
||||||
const providerCaveats = options?.providerCaveats ?? [];
|
const providerCaveats = options?.providerCaveats ?? [];
|
||||||
|
const applySelectors = options?.applySelectors === true;
|
||||||
|
|
||||||
const rawPort: KnowledgePort = {
|
const rawPort: KnowledgePort = {
|
||||||
async listLibraryCards(q: LibraryCardQuery): Promise<readonly LibraryCardRecord[]> {
|
async listLibraryCards(q: LibraryCardQuery): Promise<readonly LibraryCardRecord[]> {
|
||||||
|
|
@ -95,21 +97,83 @@ function createFakeKnowledgePort(options?: {
|
||||||
if (options?.libraryError !== undefined) {
|
if (options?.libraryError !== undefined) {
|
||||||
throw options.libraryError;
|
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[]> {
|
async listFindings(q: FindingQuery): Promise<readonly FindingRecord[]> {
|
||||||
findingsCalls.push({ ...q });
|
findingsCalls.push({ ...q });
|
||||||
if (options?.findingsError !== undefined) {
|
if (options?.findingsError !== undefined) {
|
||||||
throw options.findingsError;
|
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[]> {
|
async listProviderCaveats(q: ProviderCaveatQuery): Promise<readonly ProviderCaveatRecord[]> {
|
||||||
caveatsCalls.push({ ...q });
|
caveatsCalls.push({ ...q });
|
||||||
if (options?.caveatsError !== undefined) {
|
if (options?.caveatsError !== undefined) {
|
||||||
throw options.caveatsError;
|
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 () => {
|
test('filters case-insensitively across titles, tags, summaries, caveats, and source-specific filters', async () => {
|
||||||
const fake = createFakeKnowledgePort({
|
const fake = createFakeKnowledgePort({
|
||||||
libraryCards: [
|
libraryCards: [
|
||||||
|
|
@ -777,7 +947,7 @@ describe('MCP read_knowledge tool', () => {
|
||||||
}),
|
}),
|
||||||
makeLibraryCard({
|
makeLibraryCard({
|
||||||
id: 'lib-over-summary-needle',
|
id: 'lib-over-summary-needle',
|
||||||
title: 'Late Needle Summary',
|
title: 'Late Match Summary',
|
||||||
summary: overCapWithTailNeedle,
|
summary: overCapWithTailNeedle,
|
||||||
body: 'short body',
|
body: 'short body',
|
||||||
}),
|
}),
|
||||||
|
|
@ -790,7 +960,7 @@ describe('MCP read_knowledge tool', () => {
|
||||||
}),
|
}),
|
||||||
makeFinding({
|
makeFinding({
|
||||||
id: 'fin-over-needle',
|
id: 'fin-over-needle',
|
||||||
title: 'Late Needle Finding',
|
title: 'Late Match Finding',
|
||||||
summary: overCapWithTailNeedle,
|
summary: overCapWithTailNeedle,
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
|
|
@ -840,9 +1010,9 @@ describe('MCP read_knowledge tool', () => {
|
||||||
const needleRes = await callReadKnowledge(fake, { query: 'needle' });
|
const needleRes = await callReadKnowledge(fake, { query: 'needle' });
|
||||||
expect(needleRes.isError).toBe(false);
|
expect(needleRes.isError).toBe(false);
|
||||||
expect(needleRes.payload?.rows.map(r => `${r.source}:${r.id}`)).toEqual([
|
expect(needleRes.payload?.rows.map(r => `${r.source}:${r.id}`)).toEqual([
|
||||||
|
'provider_caveats:cav-over-needle',
|
||||||
'findings:fin-over-needle',
|
'findings:fin-over-needle',
|
||||||
'library:lib-over-summary-needle',
|
'library:lib-over-summary-needle',
|
||||||
'provider_caveats:cav-over-needle',
|
|
||||||
]);
|
]);
|
||||||
for (const row of needleRes.payload?.rows ?? []) {
|
for (const row of needleRes.payload?.rows ?? []) {
|
||||||
expect(row.truncated).toBe(true);
|
expect(row.truncated).toBe(true);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue