Files
orca/src/shared/command-environment.ts
T
Neilandcarlosbaraza 15baf86660 fix(agents): honor environment prefixes in generation commands (#22427)
* fix(agents): preserve environment prefixes in generation commands

Adapted from the proposal by @carlosbaraza.

Co-authored-by: carlosbaraza <carlosbaraza@users.noreply.github.com>

* test(agents): respect Windows environment key normalization

---------

Co-authored-by: carlosbaraza <carlosbaraza@users.noreply.github.com>
2026-09-25 20:50:39 -07:00

39 lines
1.2 KiB
TypeScript

/** Leading assignments are data, never shell expressions. */
export function extractLeadingEnvAssignments(tokens: string[]): {
env?: Record<string, string>
rest: string[]
} {
const assignments = new Map<string, string>()
let index = 0
for (const token of tokens) {
if (!/^[A-Za-z_][A-Za-z0-9_]*=/.test(token)) {
break
}
const equals = token.indexOf('=')
const name = token.slice(0, equals)
// Preserve last-assignment order when Windows folds differently cased names.
assignments.delete(name)
assignments.set(name, token.slice(equals + 1))
index++
}
return {
...(assignments.size ? { env: Object.fromEntries(assignments) } : {}),
rest: tokens.slice(index)
}
}
export function mergeCommandEnvironment(
base: NodeJS.ProcessEnv | undefined,
overrides: Record<string, string> | undefined,
platform: NodeJS.Platform
): NodeJS.ProcessEnv | undefined {
if (!overrides) {
return base
}
const entries = [...Object.entries(base ?? process.env), ...Object.entries(overrides)]
// Windows keys share one spelling so guards cannot retain differently cased aliases.
return Object.fromEntries(
platform === 'win32' ? entries.map(([key, value]) => [key.toUpperCase(), value]) : entries
)
}