install.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. """Install two pinned, public Skills into a host-supplied Skills directory.
  2. No guessed host directories, credential discovery, account creation or business writes.
  3. The official VOC artifact is downloaded from npm and verified before extraction.
  4. """
  5. import argparse
  6. import hashlib
  7. import io
  8. import json
  9. import os
  10. from pathlib import Path, PurePosixPath
  11. import re
  12. import shutil
  13. import subprocess
  14. import sys
  15. import tarfile
  16. import tempfile
  17. import urllib.request
  18. class InstallError(Exception):
  19. pass
  20. def require(condition, code):
  21. if not condition:
  22. raise InstallError(code)
  23. def digest(raw):
  24. return hashlib.sha256(raw).hexdigest()
  25. def encode(value):
  26. return (json.dumps(value, ensure_ascii=False, indent=2) + '\n').encode('utf-8')
  27. def safe_name(name):
  28. parts = PurePosixPath(name).parts
  29. return (isinstance(name, str) and bool(name) and '\\' not in name and ':' not in name
  30. and not name.startswith('/') and all(p not in ('..', '.') for p in parts)
  31. and name == '/'.join(parts))
  32. def plain_path(path):
  33. path = Path(os.path.abspath(path))
  34. for part in [*reversed(path.parents), path]:
  35. require(not part.is_symlink(), 'SYMLINK_PATH')
  36. if part.exists():
  37. require(not hasattr(part, 'is_junction') or not part.is_junction(), 'JUNCTION_PATH')
  38. return path
  39. def inside(parent, child):
  40. return child != parent and child.is_relative_to(parent)
  41. def verify_package(package):
  42. package = plain_path(package)
  43. manifest = package / 'FILES.sha256'
  44. require(manifest.is_file(), 'PACKAGE_MANIFEST_MISSING')
  45. names = set()
  46. for line in manifest.read_text(encoding='utf-8').splitlines():
  47. match = re.fullmatch(r'([a-f0-9]{64}) (.+)', line)
  48. require(match is not None, 'INVALID_PACKAGE_MANIFEST')
  49. h, name = match.groups()
  50. require(safe_name(name) and name not in names, 'INVALID_PACKAGE_PATH')
  51. names.add(name)
  52. p = plain_path(package / name)
  53. require(p.is_file() and digest(p.read_bytes()) == h, 'PACKAGE_HASH_MISMATCH')
  54. meta = json.loads((package / 'release.json').read_bytes())
  55. require(meta['schema'] == 'ksf.workshop-onboarding.v1', 'INVALID_RELEASE')
  56. return meta
  57. def voc_archive_files(raw, expected):
  58. require(len(raw) == expected['bytes'] and digest(raw) == expected['sha256'], 'VOC_ARCHIVE_HASH_MISMATCH')
  59. result = {}
  60. with tarfile.open(fileobj=io.BytesIO(raw), mode='r:gz') as archive:
  61. for member in archive.getmembers():
  62. require(member.name.startswith('package/'), 'INVALID_VOC_ARCHIVE_PATH')
  63. name = member.name[len('package/'):]
  64. require(safe_name(name), 'INVALID_VOC_ARCHIVE_PATH')
  65. require(member.isfile(), 'NON_FILE_VOC_ARCHIVE_MEMBER')
  66. require(name not in result and name in expected['files'], 'UNEXPECTED_VOC_ARCHIVE_FILE')
  67. require(member.size <= 8_000_000, 'VOC_ARCHIVE_MEMBER_TOO_LARGE')
  68. raw_file = archive.extractfile(member).read()
  69. require(digest(raw_file) == expected['files'][name], 'VOC_FILE_HASH_MISMATCH')
  70. result[name] = raw_file
  71. require(set(result) == set(expected['files']), 'VOC_ARCHIVE_INCOMPLETE')
  72. return result
  73. def source_files(package, meta, archive=None):
  74. expected = meta['voc']
  75. if archive:
  76. raw = plain_path(archive).read_bytes()
  77. else:
  78. require(expected['url'] == 'https://registry.npmjs.org/@vocmarket/voc-skill/-/voc-skill-0.3.42.tgz', 'UNEXPECTED_VOC_SOURCE')
  79. request = urllib.request.Request(expected['url'], headers={'User-Agent': 'ksf-workshop-onboarding/1'})
  80. with urllib.request.urlopen(request, timeout=30) as response:
  81. require(response.status == 200, 'VOC_DOWNLOAD_FAILED')
  82. raw = response.read(expected['bytes'] + 1)
  83. voc = voc_archive_files(raw, expected)
  84. workshop = {}
  85. for name, h in meta['workshop']['files'].items():
  86. require(safe_name(name), 'INVALID_WORKSHOP_PATH')
  87. raw_file = plain_path(package / 'skills/ksf-workshop-runtime' / name).read_bytes()
  88. require(digest(raw_file) == h, 'WORKSHOP_FILE_HASH_MISMATCH')
  89. workshop[name] = raw_file
  90. return {'ksf-workshop-runtime': workshop, 'voc-intelligence': voc}
  91. def mcp_fragment(voc_dir):
  92. return {'mcpServers': {'voc': {'command': 'node',
  93. 'args': [str(voc_dir / 'mcp/src/server.js')], 'cwd': str(voc_dir),
  94. 'env': {'VOC_SKILL_NO_UPDATE_CHECK': '1'}}}}
  95. def verify_component(target, files):
  96. require(target.is_dir(), 'EXISTING_SKILL_NOT_DIRECTORY')
  97. for name, raw in files.items():
  98. p = plain_path(target / name)
  99. require(p.is_file() and digest(p.read_bytes()) == digest(raw), 'EXISTING_SKILL_DIFFERS')
  100. def plan(skills_dir, files):
  101. actions = []
  102. for name, content in files.items():
  103. target = plain_path(skills_dir / name)
  104. if target.exists():
  105. verify_component(target, content)
  106. action = 'verify-only'
  107. else:
  108. action = 'create'
  109. actions.append({'skill': name, 'path': str(target), 'action': action, 'files': len(content)})
  110. return actions
  111. def remove_created(path, intended_parent):
  112. """Only remove our own newly created directory, after checking its final location."""
  113. path = plain_path(path)
  114. intended_parent = plain_path(intended_parent)
  115. require(inside(intended_parent, path), 'CLEANUP_PATH_OUTSIDE_TARGET')
  116. require(path.name.startswith('.ksf-init-') or path.name in ('ksf-workshop-runtime', 'voc-intelligence'),
  117. 'CLEANUP_TARGET_NOT_OWNED')
  118. shutil.rmtree(path)
  119. def install_files(skills_dir, files, actions):
  120. skills_dir.mkdir(parents=True, exist_ok=True)
  121. staging = Path(tempfile.mkdtemp(prefix='.ksf-init-', dir=skills_dir))
  122. created = []
  123. try:
  124. for row in actions:
  125. if row['action'] != 'create':
  126. continue
  127. staged = staging / row['skill']
  128. for name, raw in files[row['skill']].items():
  129. p = staged / name
  130. p.parent.mkdir(parents=True, exist_ok=True)
  131. p.write_bytes(raw)
  132. verify_component(staged, files[row['skill']])
  133. # Recheck both targets before committing either component.
  134. for row in actions:
  135. target = skills_dir / row['skill']
  136. if row['action'] == 'create':
  137. require(not target.exists(), 'CONCURRENT_INSTALL_CHANGE')
  138. else:
  139. verify_component(target, files[row['skill']])
  140. for row in actions:
  141. if row['action'] == 'create':
  142. target = skills_dir / row['skill']
  143. require(not target.exists(), 'CONCURRENT_INSTALL_CHANGE')
  144. (staging / row['skill']).rename(target)
  145. created.append(target)
  146. for row in actions:
  147. verify_component(skills_dir / row['skill'], files[row['skill']])
  148. except Exception:
  149. for target in reversed(created):
  150. verify_component(target, files[target.name])
  151. remove_created(target, skills_dir)
  152. raise
  153. finally:
  154. if staging.exists():
  155. remove_created(staging, skills_dir)
  156. def node_and_npm():
  157. node = shutil.which('node')
  158. npm = shutil.which('npm')
  159. if not node or not npm:
  160. return None
  161. version = subprocess.run([node, '-p', 'process.versions.node'], capture_output=True, timeout=15, check=False)
  162. require(version.returncode == 0 and int(version.stdout.decode().strip().split('.')[0]) >= 18,
  163. 'NODE_18_REQUIRED')
  164. if os.name == 'nt':
  165. cli = Path(npm).parent / 'node_modules/npm/bin/npm-cli.js'
  166. require(cli.is_file(), 'NPM_CLI_NOT_FOUND')
  167. return [node, str(cli)]
  168. return [npm]
  169. def prepare_runtime(package, voc_dir, output_dir):
  170. npm = node_and_npm()
  171. if not npm:
  172. return {'status': 'pending', 'reason': 'NODE_OR_NPM_NOT_AVAILABLE'}
  173. # Dependency resolution affects this newly installed VOC directory only.
  174. command = npm + ['install', '--omit=dev', '--ignore-scripts', '--no-audit', '--no-fund',
  175. '--registry', 'https://registry.npmjs.org', '--cache', str(output_dir / 'npm-cache')]
  176. result = subprocess.run(command, cwd=voc_dir, capture_output=True, check=False, timeout=240)
  177. # Raw process logs are intentionally not copied into the user-facing receipt.
  178. if result.returncode:
  179. return {'status': 'pending', 'reason': 'VOC_DEPENDENCY_INSTALL_FAILED', 'exitCode': result.returncode}
  180. check = subprocess.run([shutil.which('node'), str(package / 'check-voc.cjs'), str(voc_dir)],
  181. cwd=package, capture_output=True, check=False, timeout=40)
  182. if check.returncode:
  183. return {'status': 'pending', 'reason': 'VOC_LOCAL_CHECK_FAILED', 'exitCode': check.returncode}
  184. value = json.loads(check.stdout)
  185. require(value.get('ok') is True, 'INVALID_VOC_CHECK_RESULT')
  186. return {'status': 'verified', 'check': value}
  187. def run(args, package=None):
  188. package = Path(package).resolve() if package else Path(__file__).resolve().parent
  189. require(Path(args.skills_dir).is_absolute(), 'ABSOLUTE_HOST_SKILLS_DIRECTORY_REQUIRED')
  190. skills_dir = plain_path(args.skills_dir)
  191. require(skills_dir != Path(skills_dir.anchor), 'FILESYSTEM_ROOT_NOT_ALLOWED')
  192. require(not skills_dir.exists() or skills_dir.is_dir(), 'SKILLS_DIRECTORY_REQUIRED')
  193. require(not package.is_relative_to(skills_dir) and not skills_dir.is_relative_to(package), 'PACKAGE_TARGET_OVERLAP')
  194. require(not args.prepare_runtime or args.apply, 'PREPARE_RUNTIME_REQUIRES_APPLY')
  195. if args.apply:
  196. require(args.out and Path(args.out).is_absolute(), 'ABSOLUTE_RESULT_DIRECTORY_REQUIRED')
  197. output = plain_path(args.out)
  198. require(not output.exists(), 'RESULT_DIRECTORY_EXISTS')
  199. require(not output.is_relative_to(skills_dir) and not skills_dir.is_relative_to(output), 'RESULT_TARGET_OVERLAP')
  200. meta = verify_package(package)
  201. files = source_files(package, meta, args.voc_archive)
  202. # The npm artifact contains the author's machine-specific MCP paths.
  203. # Adapt that one generated configuration after verifying the original artifact.
  204. original_mcp_sha = digest(files['voc-intelligence']['.mcp.json'])
  205. files['voc-intelligence']['.mcp.json'] = encode(mcp_fragment(skills_dir / 'voc-intelligence'))
  206. actions = plan(skills_dir, files)
  207. receipt = {'schema': 'ksf.workshop-onboarding-result.v1', 'ok': True,
  208. 'releaseId': meta['releaseId'], 'readOnly': not args.apply, 'actions': actions,
  209. 'filesInstalled': False, 'hostSkillsLoaded': 'not-verified',
  210. 'vocRuntime': {'status': 'not-checked'}, 'hostToolRegistration': 'not-verified',
  211. 'vocLiveAccess': 'not-verified', 'researchLibraryAccess': 'not-verified',
  212. 'businessSaveAccess': 'not-verified', 'accountOrEnterpriseCreated': False,
  213. 'researchOrBusinessDataWritten': False, 'remoteInstalled': False,
  214. 'adaptedVocConfig': {'path': '.mcp.json', 'originalSha256': original_mcp_sha,
  215. 'installedSha256': digest(files['voc-intelligence']['.mcp.json'])},
  216. 'note': 'Files, local runtime, host discovery and live access have separate acceptance.'}
  217. if not args.apply:
  218. return receipt
  219. output.mkdir(parents=True, exist_ok=False)
  220. try:
  221. install_files(skills_dir, files, actions)
  222. receipt['filesInstalled'] = True
  223. voc_dir = skills_dir / 'voc-intelligence'
  224. # A neutral fragment is written to the result directory only; the host owns registration.
  225. (output / 'voc-mcp.fragment.json').write_bytes(encode(mcp_fragment(voc_dir)))
  226. receipt['mcpFragment'] = str(output / 'voc-mcp.fragment.json')
  227. if args.prepare_runtime:
  228. try:
  229. receipt['vocRuntime'] = prepare_runtime(package, voc_dir, output)
  230. except subprocess.TimeoutExpired:
  231. receipt['vocRuntime'] = {'status': 'pending', 'reason': 'RUNTIME_PREPARATION_TIMEOUT'}
  232. else:
  233. receipt['vocRuntime'] = {'status': 'pending', 'reason': 'DEPENDENCIES_NOT_PREPARED'}
  234. except Exception as exc:
  235. receipt['ok'] = False
  236. receipt['errorCode'] = str(exc) if isinstance(exc, InstallError) else 'INSTALL_FAILED'
  237. raise
  238. finally:
  239. (output / 'install-receipt.json').write_bytes(encode(receipt))
  240. return receipt
  241. def main():
  242. parser = argparse.ArgumentParser(description=__doc__)
  243. parser.add_argument('--skills-dir', required=True, help='Actual Skills directory provided by the host')
  244. parser.add_argument('--out', help='New absolute result directory outside the Skills directory')
  245. parser.add_argument('--voc-archive', help='Optional local copy of the exact official npm tarball')
  246. parser.add_argument('--apply', action='store_true')
  247. parser.add_argument('--prepare-runtime', action='store_true')
  248. args = parser.parse_args()
  249. try:
  250. receipt = run(args)
  251. print(json.dumps(receipt, ensure_ascii=False, indent=2))
  252. return 0
  253. except Exception as exc:
  254. code = str(exc) if isinstance(exc, InstallError) else 'INITIALIZATION_FAILED'
  255. print(json.dumps({'ok': False, 'errorCode': code, 'note': 'No credentials or raw environment were printed.'}))
  256. return 1
  257. if __name__ == '__main__':
  258. sys.exit(main())