T-163: M2.1m — Filesystem library adapter for the MCP copilot read_knowledge tool: KnowledgePort.listLibraryCards over library.yml + cards/<section>/*.md in libs/research/src/mcp/adapters/library-cards.ts (injected read-only fs port, strict front-matter parser, no new deps)

This commit is contained in:
Studio 2026-12-02 10:40:05 -05:00 • committed by Boki
parent a255191b66
commit e80b224e9b
3 changed files with 113 additions and 140 deletions

View file

@ -110,30 +110,84 @@ function createFakeLibraryFs(init?: {
}
}
const readFileByPath = async (filePath: string): Promise<string> => {
readPaths.push(filePath);
const forcedErr = readErrors.get(filePath);
if (forcedErr !== undefined) {
throw forcedErr;
}
const text = files.get(filePath);
if (text === undefined) {
throw makeEnoent(filePath);
}
return text;
};
const listDirByPath = async (dirPath: string): Promise<readonly LibraryCardDirEntry[]> => {
listPaths.push(dirPath);
const forcedErr = listErrors.get(dirPath);
if (forcedErr !== undefined) {
throw forcedErr;
}
const entries = dirs.get(dirPath);
if (entries === undefined) {
throw makeEnoent(dirPath);
}
return [...entries];
};
const createAnchoredFakeDirHandle = (dirPath: string): LibraryCardDirHandle => {
let closed = false;
const assertOpen = (): void => {
if (closed) {
throw new Error(`Directory handle already closed: ${dirPath}`);
}
};
const assertChildName = (name: string): void => {
if (
name.length === 0 ||
name === '.' ||
name === '..' ||
name.includes('/') ||
name.includes('\\') ||
name.includes('\0')
) {
throw new Error(`Invalid child name: ${name}`);
}
};
return {
async listDir(): Promise<readonly LibraryCardDirEntry[]> {
assertOpen();
return listDirByPath(dirPath);
},
async openSubdir(name: string): Promise<LibraryCardDirHandle> {
assertOpen();
assertChildName(name);
const childPath = path.join(dirPath, name);
if (!dirs.has(childPath)) {
throw makeEnoent(childPath);
}
return createAnchoredFakeDirHandle(childPath);
},
async readTextFile(name: string): Promise<string> {
assertOpen();
assertChildName(name);
return readFileByPath(path.join(dirPath, name));
},
async close(): Promise<void> {
closed = true;
},
};
};
const fs: LibraryCardFs = {
async readTextFile(filePath: string): Promise<string> {
readPaths.push(filePath);
const forcedErr = readErrors.get(filePath);
if (forcedErr !== undefined) {
throw forcedErr;
}
const text = files.get(filePath);
if (text === undefined) {
throw makeEnoent(filePath);
}
return text;
},
async listDir(dirPath: string): Promise<readonly LibraryCardDirEntry[]> {
listPaths.push(dirPath);
const forcedErr = listErrors.get(dirPath);
if (forcedErr !== undefined) {
throw forcedErr;
}
const entries = dirs.get(dirPath);
if (entries === undefined) {
readTextFile: readFileByPath,
listDir: listDirByPath,
async openDir(dirPath: string): Promise<LibraryCardDirHandle> {
if (dirPath !== VIRTUAL_ROOT && !dirs.has(dirPath)) {
throw makeEnoent(dirPath);
}
return [...entries];
return createAnchoredFakeDirHandle(dirPath);
},
};
@ -282,6 +336,8 @@ describe('MCP library-cards filesystem adapter', () => {
'library.yml': `id: quant
sections:
alphas: Alphas
-draft: Draft notes
.archived: Archived notes
Bad_Section: Invalid section name
empty-section: Declared section with no directory
`,
@ -291,6 +347,7 @@ sections:
'cards/alphas/UpperCase.md': MOMENTUM_CARD,
'cards/alphas/symlink-card.md': MOMENTUM_CARD,
'cards/undeclared/secret.md': MOMENTUM_CARD,
'cards/-draft/valid-card.md': MOMENTUM_CARD,
'cards/Bad_Section/valid-card.md': MOMENTUM_CARD,
},
dirs: {
@ -301,6 +358,7 @@ sections:
cards: [
{ name: 'alphas', isFile: false, isDirectory: true },
{ name: 'undeclared', isFile: false, isDirectory: true },
{ name: '-draft', isFile: false, isDirectory: true },
{ name: 'Bad_Section', isFile: false, isDirectory: true },
{ name: 'symlink-section', isFile: false, isDirectory: false },
],
@ -352,6 +410,19 @@ sections:
}),
'INVALID_FS'
);
// A two-method wrapper omitting anchored openDir must fail closed at construction.
const nodeFs = createNodeLibraryCardFs();
await expectLibraryCardError(
() =>
createLibraryCardReader({
root: VIRTUAL_ROOT,
fs: {
readTextFile: p => nodeFs.readTextFile(p),
listDir: p => nodeFs.listDir(p),
} as unknown as LibraryCardFs,
}),
'INVALID_FS'
);
// Zero I/O happened during invalid option checks.
expect(fake.readPaths).toHaveLength(0);
@ -750,9 +821,7 @@ LEAKED_SIBLING_CONTENT
readTextFile: p => baseNodeFs.readTextFile(p),
listDir: p => baseNodeFs.listDir(p),
async openDir(dirPath: string): Promise<LibraryCardDirHandle> {
const rootHandle = await (baseNodeFs.openDir as NonNullable<LibraryCardFs['openDir']>)(
dirPath
);
const rootHandle = await baseNodeFs.openDir(dirPath);
return {
listDir: () => rootHandle.listDir(),
readTextFile: name => rootHandle.readTextFile(name),
@ -799,9 +868,7 @@ LEAKED_SIBLING_CONTENT
readTextFile: p => baseNodeFs.readTextFile(p),
listDir: p => baseNodeFs.listDir(p),
async openDir(dirPath: string): Promise<LibraryCardDirHandle> {
const rootHandle = await (baseNodeFs.openDir as NonNullable<LibraryCardFs['openDir']>)(
dirPath
);
const rootHandle = await baseNodeFs.openDir(dirPath);
return {
listDir: () => rootHandle.listDir(),
readTextFile: name => rootHandle.readTextFile(name),