verify-git-package.mjs 5.0 KB

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