File size: 4,088 Bytes
2c2dc59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { existsSync } from 'fs'
import { readFile } from 'fs/promises'
import { isAbsolute, join, relative, resolve } from 'path'
import type { RuntimeResolution } from './types.js'
import { resolveSharedPython, SHARED_VENV_DIR } from './biomnibenchAdapter.js'

type EnvManifest = {
  default_env?: string
  envs?: Record<
    string,
    {
      python?: {
        windows?: string
        posix?: string
      }
    }
  >
}

function stripUtf8Bom(value: string): string {
  return value.charCodeAt(0) === 0xfeff ? value.slice(1) : value
}

function platformPythonKey(): 'windows' | 'posix' {
  return process.platform === 'win32' ? 'windows' : 'posix'
}

async function readJsonIfExists<T>(path: string): Promise<T | null> {
  if (!existsSync(path)) return null
  return JSON.parse(stripUtf8Bom(await readFile(path, 'utf8'))) as T
}

function resolveMaybeRelative(base: string, path: string): string {
  return isAbsolute(path) ? resolve(path) : resolve(base, path)
}

function isInside(path: string, parent: string): boolean {
  const child = resolve(path)
  const base = resolve(parent)
  const normalizedChild = process.platform === 'win32' ? child.toLowerCase() : child
  const normalizedBase = process.platform === 'win32' ? base.toLowerCase() : base
  return (
    normalizedChild === normalizedBase ||
    normalizedChild.startsWith(`${normalizedBase}\\`) ||
    normalizedChild.startsWith(`${normalizedBase}/`)
  )
}

function displayPath(publicDir: string, absolutePath: string): string {
  const rel = relative(publicDir, absolutePath).replace(/\\/g, '/')
  return rel.startsWith('..') ? absolutePath : `public/${rel}`
}

export async function resolveTaskRuntime(publicDir: string): Promise<RuntimeResolution> {
  const manifestPath = join(publicDir, 'envs', 'env_manifest.json')
  const manifest = await readJsonIfExists<EnvManifest>(manifestPath)
  const envName = manifest?.default_env ?? 'runtime'
  const platformKey = platformPythonKey()
  const checked: string[] = []
  const configuredPython =
    manifest?.envs?.[envName]?.python?.[platformKey] ??
    manifest?.envs?.runtime?.python?.[platformKey]
  const configuredPath = configuredPython
    ? resolveMaybeRelative(publicDir, configuredPython)
    : undefined

  if (configuredPath && !isInside(configuredPath, publicDir)) {
    return {
      ok: false,
      error: `Configured task Python is outside public/: ${configuredPython}`,
      checked: [configuredPath],
    }
  }

  const candidates = Array.from(new Set([
    configuredPath,
    join(
      publicDir,
      'envs',
      'runtime',
      process.platform === 'win32'
        ? '.venv/Scripts/python.exe'
        : '.venv/bin/python',
    ),
    join(
      publicDir,
      'envs',
      'runtime',
      process.platform === 'win32'
        ? '.venv/Scripts/python.exe'
        : '.venv-posix/bin/python',
    ),
  ].filter((value): value is string => Boolean(value))))

  for (const candidate of candidates) {
    const absolutePath = resolve(candidate)
    checked.push(absolutePath)
    if (existsSync(absolutePath)) {
      return {
        ok: true,
        python: absolutePath,
        displayPath: displayPath(publicDir, absolutePath),
        envName,
        checked,
      }
    }
  }

  // BioMniBench fallback: when the task has no env_manifest.json AND no
  // per-task venv, fall back to the shared venv at <repoRoot>/shared_venv.
  // This lets BioMniBench `da-*` tasks reuse a single Python environment
  // populated with scanpy/anndata/pandas/etc.
  const sharedPython = resolveSharedPython()
  if (sharedPython) {
    checked.push(sharedPython)
    return {
      ok: true,
      python: sharedPython,
      displayPath: relative(publicDir, SHARED_VENV_DIR).startsWith('..')
        ? sharedPython
        : `public/${relative(publicDir, sharedPython).replace(/\\/g, '/')}`,
      envName: 'shared',
      checked,
    }
  }

  return {
    ok: false,
    error: `Unable to resolve task Python from ${manifestPath}; checked configured and fallback runtime paths, and no shared venv at ${SHARED_VENV_DIR}.`,
    checked,
  }
}