io-boundary.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { readdir, readFile } from "node:fs/promises";
  2. import { join, relative } from "node:path";
  3. import { describe, expect, it } from "vitest";
  4. const repoRoot = join(import.meta.dirname, "..", "..", "..");
  5. const allowedNetworkBoundaryFiles = new Set([
  6. "packages/adapters/src/auth/localhost-callback.ts",
  7. "packages/adapters/src/http/index.ts",
  8. // 仅使用 node:http 的状态码/类型,不执行网络请求。
  9. "packages/adapters/src/http/public-egress-policy.ts",
  10. // 通过调用方注入的 fetch 执行正式 Adapter 网络边界。
  11. "packages/adapters/src/model/model-option-map-fetch.ts",
  12. "packages/adapters/src/model/provider-endpoint-routing-fetch.ts",
  13. // 只读 node:http 的 IncomingMessage 类型,把已到手的响应交给 http/index.ts 与 proxy-fetch.ts,不发请求。
  14. "packages/adapters/src/network/incoming-response.ts",
  15. "packages/adapters/src/network/proxy-fetch.ts",
  16. // MCP 的局部 fetch 由 createNetworkProxyFetch 注入,仅包裹网络观测范围,不直接使用全局 fetch。
  17. "packages/adapters/src/mcp/network.ts",
  18. "packages/cli/src/provider-runtime-env.ts",
  19. // WebFetch 工具只读取 node:http 状态码,真实请求由注入的执行端口完成。
  20. "packages/core/src/tool/handlers/webfetch.ts",
  21. ]);
  22. async function collectTypeScriptFiles(directory: string): Promise<string[]> {
  23. const entries = await readdir(directory, { withFileTypes: true });
  24. const files: string[] = [];
  25. for (const entry of entries) {
  26. const path = join(directory, entry.name);
  27. if (entry.isDirectory()) {
  28. files.push(...(await collectTypeScriptFiles(path)));
  29. continue;
  30. }
  31. if (entry.isFile() && path.endsWith(".ts")) {
  32. files.push(path);
  33. }
  34. }
  35. return files;
  36. }
  37. describe("core I/O boundaries", () => {
  38. it("keeps file tools and context assembly free of direct host I/O", async () => {
  39. const files = [
  40. "packages/core/src/runtime.ts",
  41. "packages/core/src/context/sections/request-user-context.ts",
  42. "packages/core/src/tool/handlers/read.ts",
  43. "packages/core/src/tool/handlers/write.ts",
  44. "packages/core/src/tool/handlers/edit.ts",
  45. "packages/core/src/tool/handlers/skill.ts",
  46. ];
  47. for (const file of files) {
  48. const content = await readFile(join(repoRoot, file), "utf8");
  49. expect(content, file).not.toMatch(
  50. /from\s+["'](?:node:fs|node:fs\/promises|fs\/promises)["']/,
  51. );
  52. expect(content, file).not.toMatch(/process\.(?:cwd|env|platform)/);
  53. }
  54. });
  55. it("keeps in-process HTTP egress behind adapter network boundaries", async () => {
  56. const packageSourceRoots = [
  57. "packages/adapters/src",
  58. "packages/bootstrap/src",
  59. "packages/cli/src",
  60. "packages/contracts/src",
  61. "packages/core/src",
  62. "packages/tui/src",
  63. ];
  64. const files = (
  65. await Promise.all(
  66. packageSourceRoots.map((root) => collectTypeScriptFiles(join(repoRoot, root))),
  67. )
  68. ).flat();
  69. const violations: string[] = [];
  70. for (const file of files) {
  71. const relativePath = relative(repoRoot, file);
  72. if (allowedNetworkBoundaryFiles.has(relativePath)) {
  73. continue;
  74. }
  75. const content = await readFile(file, "utf8");
  76. if (
  77. /\bfetch\s*\(/.test(content) ||
  78. /\bhttps?\.request\s*\(/.test(content) ||
  79. /from\s+["']node:https?["']/.test(content)
  80. ) {
  81. violations.push(relativePath);
  82. }
  83. }
  84. expect(violations).toEqual([]);
  85. });
  86. });