fix: guard os.userInfo() for UID-only containers, exclude HOME from cache key

Address Greptile review feedback:

1. Wrap os.userInfo() in try/catch — it throws SystemError when the
   current UID has no /etc/passwd entry (e.g. `docker run --user 1234`
   with a minimal image). Falls back to process.env.HOME gracefully.

2. Add HOME to VOLATILE_ENV_KEY_EXACT so the discovery cache key is
   not affected by the caller-supplied HOME vs the resolved HOME.
   os.userInfo().homedir is constant for the process lifetime, so
   HOME adds no useful cache differentiation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
John Wessel
2026-03-17 13:05:23 -04:00
parent 2aa607c828
commit 5965266cb8

View File

@@ -21,7 +21,7 @@ function resolveOpenCodeCommand(input: unknown): string {
const discoveryCache = new Map<string, { expiresAt: number; models: AdapterModel[] }>(); const discoveryCache = new Map<string, { expiresAt: number; models: AdapterModel[] }>();
const VOLATILE_ENV_KEY_PREFIXES = ["PAPERCLIP_", "npm_", "NPM_"] as const; const VOLATILE_ENV_KEY_PREFIXES = ["PAPERCLIP_", "npm_", "NPM_"] as const;
const VOLATILE_ENV_KEY_EXACT = new Set(["PWD", "OLDPWD", "SHLVL", "_", "TERM_SESSION_ID"]); const VOLATILE_ENV_KEY_EXACT = new Set(["PWD", "OLDPWD", "SHLVL", "_", "TERM_SESSION_ID", "HOME"]);
function dedupeModels(models: AdapterModel[]): AdapterModel[] { function dedupeModels(models: AdapterModel[]): AdapterModel[] {
const seen = new Set<string>(); const seen = new Set<string>();
@@ -112,7 +112,14 @@ export async function discoverOpenCodeModels(input: {
// When the server is started via `runuser -u <user>`, HOME may still // When the server is started via `runuser -u <user>`, HOME may still
// reflect the parent process (e.g. /root), causing OpenCode to miss // reflect the parent process (e.g. /root), causing OpenCode to miss
// provider auth credentials stored under the target user's home. // provider auth credentials stored under the target user's home.
const resolvedHome = os.userInfo().homedir; let resolvedHome: string | undefined;
try {
resolvedHome = os.userInfo().homedir || undefined;
} catch {
// os.userInfo() throws a SystemError when the current UID has no
// /etc/passwd entry (e.g. `docker run --user 1234` with a minimal
// image). Fall back to process.env.HOME.
}
const runtimeEnv = normalizeEnv(ensurePathInEnv({ ...process.env, ...env, ...(resolvedHome ? { HOME: resolvedHome } : {}) })); const runtimeEnv = normalizeEnv(ensurePathInEnv({ ...process.env, ...env, ...(resolvedHome ? { HOME: resolvedHome } : {}) }));
const result = await runChildProcess( const result = await runChildProcess(