[Bug Report]: tarko CLI resolves a defined-but-empty environment variable to the variable's own name
Version
main @ c2ad42e3; multimodal/tarko/agent-cli/package.json is 0.3.0.
Issue Description
resolveValue() in the tarko CLI cannot tell "this environment variable is not defined" apart from "this environment
variable is defined and empty", because it tests the lookup result for truthiness. In the second case it falls through
and returns the variable's own name as if it were the resolved secret.
multimodal/tarko/agent-cli/src/utils/misc.ts:19-27
// If value is in all uppercase, treat it as an environment variable
if (/^[A-Z][A-Z0-9_]*$/.test(value)) {
const envValue = process.env[value];
if (envValue) {
logger.debug(`Using ${label} from environment variable: ${value}`);
return envValue;
} else {
logger.warn(`Environment variable "${value}" not found, using as literal value`);
}
}
return value;Two things go wrong once process.env[value] is '':
- the function returns
OPENAI_API_KEY— the identifier, 15 characters of plaintext placeholder — as the resolved API key / base URL, so the run does not fail fast but goes out with a fabricated credential; - the only diagnostic it prints,
Environment variable "OPENAI_API_KEY" not found, is false. The variable is found; it is empty. That message is the thing a user will grep for, and it points them at the wrong file.
An empty value is not a hypothetical shape — it is exactly what the CLI's own environment loading produces. agent-cli
depends on dotenv, and cli.ts:372 calls loadEnvironmentVars() (src/config/loader.ts:62-71, which does
dotenv.config({ path }) on the workspace .env.local / .env) before cli.ts:395 calls buildAppConfig(). A
placeholder line such as OPENAI_API_KEY= — the shape people commit into .env.example copies — measures as follows
with this package's installed dotenv:
path= <workspace>/.env # contains: OPENAI_API_KEY=
typeof= string JSON= "" len= 0 in= true <-- defined, and empty
UNSET in process.env = false <-- genuinely undefined is distinguishableSo process.env[value] !== undefined is a faithful "was it found" test, and truthiness is not.
Full call chain from a CLI flag
--model.apiKey / the deprecated --apiKey are declared in src/core/options.ts:54-55:
src/core/cli.ts:372→loadEnvironmentVars()populatesprocess.envfrom the workspace.env;src/core/cli.ts:395→buildAppConfig()(src/config/builder.ts:38);src/config/builder.ts:115→resolveModelSecrets();src/config/builder.ts:271→resolveValue(modelApiKey, 'API key'), whose result is written back at:272; the base URL takes the same path at:276;- and independently
src/core/commands/request.ts:41→resolveValue(options.apiKey, 'API key'), passed straight intonew LLMRequester().request({ … apiKey … })at:83(LLMRequestercomes from@tarko/agent, imported at:7).
Note the guard at src/config/builder.ts:269 is if (cliConfigProps.model.apiKey) on the pre-resolution value, so an
empty resolution never short-circuits it — the name is what ends up stored.
Steps to Reproduce
Unit-level, against the package's own committed vitest.config.mts (pnpm test in multimodal/tarko/agent-cli):
process.env.MY_EMPTY_API_KEY = '';
resolveValue('MY_EMPTY_API_KEY', 'API key');Actual on main: 'MY_EMPTY_API_KEY'
Expected: ''
For comparison, the neighbouring cases behave correctly today and must keep behaving identically:
resolveValue(undefined) → undefined; resolveValue('sk-proj-AbC123') → 'sk-proj-AbC123' (not all-uppercase, so not
a variable reference); resolveValue('SET_NAME') with process.env.SET_NAME = 'real' → 'real';
resolveValue('MY_UNSET_API_KEY') with the key absent → 'MY_UNSET_API_KEY' plus the "not found" warning, which is the
intentional documented fallback and the reason a plain return undefined would be the wrong fix.
Additional context
multimodal/tarko/agent-cli/tests/currently has no coverage ofsrc/utils/misc.tsat all, which is why the truthiness slip has survived;tests/config-builder.test.ts:21mocksresolveValueaway rather than exercising it.- I checked before writing this: no open issue or open pull request mentions
resolveValue, and a per-file scan of all 132 open pull requests' changed paths found none touchingagent-cli/src/utils/misc.tsoragent-cli/tests/misc.test.ts. - The one-line change is
if (envValue)→if (envValue !== undefined); I have it locally with a regression test and will file the pull request. I am deliberately not touchingsrc/config/builder.tshere — that file has open pull requests (#1939, #1891) against it.
Source: bytedance/UI-TARS-desktop