verify-git-package.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env node
  2. import assert from "node:assert/strict";
  3. import { execFile } from "node:child_process";
  4. import { constants } from "node:fs";
  5. import { access, lstat, mkdtemp, readFile, realpath, rm } from "node:fs/promises";
  6. import { tmpdir } from "node:os";
  7. import { isAbsolute, join, relative } from "node:path";
  8. import { promisify } from "node:util";
  9. import { Client } from "@modelcontextprotocol/sdk/client/index.js";
  10. import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
  11. const execFileAsync = promisify(execFile);
  12. const EXPECTED_TOOLS = ["overview", "list_threads", "inspect_thread", "wait_for_change"];
  13. const PACKAGE_NAME = "@bodicsek/codex-chief-of-staff";
  14. const BIN_NAME = "codex-chief-of-staff";
  15. const DEFAULT_CACHE = "/private/tmp/codex-chief-of-staff-npm-cache";
  16. const USAGE = "usage: npm run verify:git-package -- <git+https|git+ssh|git+file URL>#<vX.Y.Z|40-character-commit>";
  17. function selectedGitPackage(argv) {
  18. if (argv.length !== 1) throw new Error(USAGE);
  19. const packageSpec = argv[0];
  20. const fragmentAt = packageSpec.lastIndexOf("#");
  21. const repository = packageSpec.slice(0, fragmentAt);
  22. const revision = packageSpec.slice(fragmentAt + 1);
  23. const supportedRepository = /^(?:git\+https|git\+ssh|git\+file):\/\/.+\.git$/.test(repository);
  24. const immutableRevision = /^(?:[0-9a-fA-F]{40}|v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)$/.test(revision);
  25. if (fragmentAt <= 0 || !supportedRepository || !immutableRevision) {
  26. throw new Error("Git package must use a supported URL pinned to a full commit or release tag");
  27. }
  28. return { packageSpec, revision };
  29. }
  30. function isWithin(parent, child) {
  31. const pathFromParent = relative(parent, child);
  32. return pathFromParent === "" || (!pathFromParent.startsWith("..") && !isAbsolute(pathFromParent));
  33. }
  34. function commandExit(error) {
  35. return typeof error === "object" && error !== null && "code" in error
  36. ? String(error.code)
  37. : "unknown";
  38. }
  39. async function installPackage(prefix, cache, packageSpec) {
  40. try {
  41. await execFileAsync("npm", [
  42. "install",
  43. "--global",
  44. "--prefix",
  45. prefix,
  46. "--ignore-scripts",
  47. "--cache",
  48. cache,
  49. packageSpec,
  50. ], { maxBuffer: 10 * 1024 * 1024 });
  51. } catch (error) {
  52. throw new Error(`isolated npm installation failed with exit code ${commandExit(error)}`);
  53. }
  54. }
  55. async function installedPackage(prefix, cache) {
  56. const packageRoot = join(prefix, "lib", "node_modules", PACKAGE_NAME);
  57. const executable = join(prefix, "bin", BIN_NAME);
  58. await access(executable, constants.X_OK);
  59. const packageMetadata = await lstat(packageRoot);
  60. if (packageMetadata.isSymbolicLink()) {
  61. throw new Error("installed package must not be a temporary link");
  62. }
  63. const [realPrefix, realCache, realPackage, realExecutable] = await Promise.all([
  64. realpath(prefix),
  65. realpath(cache),
  66. realpath(packageRoot),
  67. realpath(executable),
  68. ]);
  69. if (!isWithin(realPrefix, realPackage) || isWithin(realCache, realPackage)) {
  70. throw new Error("installed package resolved outside the isolated prefix");
  71. }
  72. if (!isWithin(realPackage, realExecutable) || isWithin(realCache, realExecutable)) {
  73. throw new Error("installed executable resolved outside the installed package");
  74. }
  75. const manifest = JSON.parse(await readFile(join(realPackage, "package.json"), "utf8"));
  76. assert.equal(manifest.name, PACKAGE_NAME);
  77. assert.deepEqual(manifest.bin, { [BIN_NAME]: "dist/src/index.js" });
  78. return { executable, version: String(manifest.version) };
  79. }
  80. async function verify() {
  81. const { packageSpec, revision } = selectedGitPackage(process.argv.slice(2));
  82. const prefix = await mkdtemp(join(tmpdir(), "codex-chief-of-staff-git-package-"));
  83. const cache = process.env.CODEX_CHIEF_OF_STAFF_NPM_CACHE ?? DEFAULT_CACHE;
  84. let client;
  85. try {
  86. await installPackage(prefix, cache, packageSpec);
  87. const installed = await installedPackage(prefix, cache);
  88. client = new Client({ name: "git-package-verifier", version: "1.0.0" });
  89. const transport = new StdioClientTransport({
  90. command: installed.executable,
  91. env: {
  92. ...process.env,
  93. CODEX_APP_SERVER_URL: process.env.CODEX_APP_SERVER_URL ?? "ws://127.0.0.1:4500",
  94. },
  95. });
  96. await client.connect(transport);
  97. assert.match(client.getInstructions() ?? "", /call overview first/i);
  98. const listed = await client.listTools();
  99. assert.deepEqual(listed.tools.map(({ name }) => name), EXPECTED_TOOLS);
  100. process.stdout.write(`${JSON.stringify({
  101. ok: true,
  102. package: `${PACKAGE_NAME}@${installed.version}`,
  103. revision,
  104. executable: `bin/${BIN_NAME}`,
  105. storage: "isolated-prefix",
  106. instructions: "verified",
  107. tools: EXPECTED_TOOLS,
  108. })}\n`);
  109. } finally {
  110. if (client !== undefined) await client.close().catch(() => undefined);
  111. await rm(prefix, { recursive: true, force: true });
  112. }
  113. }
  114. verify().catch((error) => {
  115. const message = error instanceof Error ? error.message : String(error);
  116. process.stderr.write(`Git-package verification failed: ${message}\n`);
  117. process.exitCode = 1;
  118. });