mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
`serve service install` copied the caller's PATH into the systemd unit or LaunchAgent. Run from the AppImage, that PATH starts with the launch's temporary /tmp/.mount_* directory (and $APPDIR/usr/sbin), which is gone by the service's next start; another process's mount could later take the same name. The unit PATH now drops every directory the service command lookup already refuses as tied to one Orca version or AppImage launch: profile and orcad artifact dirs, the AppImage FUSE mount, and extract-and-run copies (appimage_extracted_*), plus relative entries. AppImage mount detection now lives in one shared helper used by the service command check, the unit PATH and the daemon host-image probe; the command check previously missed extract-and-run copies. Fixes #36000
94 lines
4.0 KiB
TypeScript
94 lines
4.0 KiB
TypeScript
import { accessSync, constants } from 'node:fs'
|
||
import { delimiter, dirname, isAbsolute, join, relative } from 'node:path'
|
||
import { RuntimeClientError } from './types'
|
||
import { resolveLaunchUserDataPath } from './launch-user-data-path'
|
||
import { appImageEphemeralRootOf } from '../../shared/appimage-ephemeral-root'
|
||
|
||
/** A remote server’s Node-based CLI cannot start a local app server. */
|
||
export function canStartOrcaServer(env: NodeJS.ProcessEnv): boolean {
|
||
const override = env.ORCA_APP_EXECUTABLE
|
||
return (
|
||
(typeof override === 'string' && override.trim().length > 0) || env.ELECTRON_RUN_AS_NODE === '1'
|
||
)
|
||
}
|
||
|
||
/** Linux installs the app's CLI as `orca-ide`: a bare `orca` there is often the screen reader. */
|
||
export function appCliName(platform: NodeJS.Platform): string {
|
||
return platform === 'linux' ? 'orca-ide' : 'orca'
|
||
}
|
||
|
||
const NOT_AN_APP_CLI =
|
||
"This `orca` command isn't running from an installed Orca app. On a host Orca reaches over SSH, it belongs to that connection's server and can't start one of its own."
|
||
|
||
export function serverOnlyServeMessage(platform: NodeJS.Platform): string {
|
||
return `${NOT_AN_APP_CLI} Install the Orca app on this machine and run \`${appCliName(platform)} serve\` from a plain login shell, or set ORCA_APP_EXECUTABLE to the Orca app executable.`
|
||
}
|
||
|
||
function serverOnlyServiceInstallMessage(platform: NodeJS.Platform): string {
|
||
const name = appCliName(platform)
|
||
return `Could not find the installed Orca app's \`${name}\` command on PATH. ${NOT_AN_APP_CLI} Install the Orca app on this machine, then run \`${name} serve service install\` from a plain login shell so the service uses the app's own profile, or pass --command <absolute path to the app's ${name}>.`
|
||
}
|
||
|
||
function isVersionScopedDir(dir: string, userData: string): boolean {
|
||
const inside = relative(userData, dir)
|
||
return (
|
||
inside === '' ||
|
||
(!inside.startsWith('..') && !isAbsolute(inside)) ||
|
||
dir.includes('orcad-artifacts') ||
|
||
appImageEphemeralRootOf(dir) !== null
|
||
)
|
||
}
|
||
|
||
/** PATH for a persisted service: entries tied to one Orca version or AppImage launch are gone by its next start. */
|
||
export function resolveServicePath(env: NodeJS.ProcessEnv): string {
|
||
const userData = resolveLaunchUserDataPath(env)
|
||
const durable = (env.PATH ?? '')
|
||
.split(delimiter)
|
||
.filter((dir) => isAbsolute(dir) && !isVersionScopedDir(dir, userData))
|
||
return durable.length > 0 ? durable.join(delimiter) : '/usr/bin:/bin'
|
||
}
|
||
|
||
/** The stable launcher on PATH: a version-specific executable would vanish with the next update. */
|
||
export function resolveServiceCommand(
|
||
explicit: string | null,
|
||
deps: { platform: NodeJS.Platform; env: NodeJS.ProcessEnv }
|
||
): string {
|
||
const isExecutable = (path: string): boolean => {
|
||
try {
|
||
accessSync(path, constants.X_OK)
|
||
return true
|
||
} catch {
|
||
return false
|
||
}
|
||
}
|
||
const userData = resolveLaunchUserDataPath(deps.env)
|
||
if (explicit) {
|
||
if (!isAbsolute(explicit) || !isExecutable(explicit)) {
|
||
throw new RuntimeClientError(
|
||
'invalid_argument',
|
||
`--command must be an absolute path to an executable: ${explicit}`
|
||
)
|
||
}
|
||
if (isVersionScopedDir(dirname(explicit), userData)) {
|
||
throw new RuntimeClientError(
|
||
'invalid_argument',
|
||
`--command points at a launcher tied to one Orca version, which the next update replaces: ${explicit}. Pass the installed Orca app's \`${appCliName(deps.platform)}\` instead.`
|
||
)
|
||
}
|
||
return explicit
|
||
}
|
||
const name = appCliName(deps.platform)
|
||
for (const dir of (deps.env.PATH ?? '').split(delimiter).filter(isAbsolute)) {
|
||
// Profile and AppImage launchers point at files that disappear with an update.
|
||
if (!isVersionScopedDir(dir, userData) && isExecutable(join(dir, name))) {
|
||
return join(dir, name)
|
||
}
|
||
}
|
||
throw new RuntimeClientError(
|
||
'cli_not_found',
|
||
canStartOrcaServer(deps.env)
|
||
? `Could not find the \`${name}\` command on PATH. Install it from Orca Settings, or pass --command <absolute path>.`
|
||
: serverOnlyServiceInstallMessage(deps.platform)
|
||
)
|
||
}
|