#3205·midscene

(feat) Load model config from a user-level config file or OS keyring, not just env vars

Author: tan-yong-shengCreated Sep 30, 2026Updated Sep 30, 2026

What problem does this feature solve?

Problem

Model configuration is only read from process.env:

  • packages/shared/src/env/global-config-manager.ts:40-43 — getAllEnvConfig() reduces ALL_ENV_KEYS over process.env.
  • packages/shared/src/cli/cli-runner.ts:220-223 — the only file support is dotenv.config({ path: join(process.cwd(), '.env') }), which is cwd-dependent and does not override real env vars.

That leaves two ways to set a key today, both bad:

Approach Problem
export in ~/.bashrc / direnv / systemd EnvironmentFile (the documented pattern) The key lands in the environment of every process, including every child an AI agent spawns — env, printenv, ps eww, or any crash dumping process.env all show it. rc files also get cat-ed, screenshotted, and synced into dotfile repos.
Plaintext .env inside the project directory cwd-dependent, one leak away from being committed, and it drags report output into that directory.

A 0600 user-level file is already a meaningful improvement with no keyring involved: not in printenv, not inherited by unrelated children, never in shell history, read only at startup. Keyring support is the optional next step.

(No design hides a value from another process running as the same user. The goal is narrower: stop spraying the key into every process.)

What does the proposed API look like?

Proposal

Resolution order, first match wins:

  1. Programmatic override (already exists — overrideAIConfig)
  2. MIDSCENE_CONFIG=<path>
  3. $XDG_CONFIG_HOME/midscene/config.json, falling back to ~/.config/midscene/config.json on all platforms
  4. $PWD/.env (unchanged)
  5. process.env (unchanged)
jsonc
// ~/.config/midscene/config.json — mode 0600
{
  "model": {
    "name": "qwen3.7-plus",
    "baseUrl": "https://dashscope.aliyuncs.com/compatible-mode/v1",
    "family": "qwen3",

    "apiKey": "sk-..."                          // plaintext, fine in a 0600 file
    // "apiKey": { "fromKeyring": "midscene/model/default" }
  },
  "planning": {
    "name": "gpt-5.6-sol",
    "baseUrl": "https://api.openai.com/v1",
    "family": "gpt-5",
    "apiKey": { "fromEnv": "PLANNING_KEY" }
  }
}

A bare string is shorthand for { "fromPlaintext": ... }. Providers:

Provider Behaviour
{ "fromPlaintext": "sk-..." } Key in the config file. First-class, supported path — warn once at startup if the file is group/world-readable.
{ "fromKeyring": "<service/account>" } libsecret (Linux), Keychain (macOS), Credential Manager (Windows)
{ "fromCommand": ["<cmd>", "…"] } Optional. Exec a user-supplied command and read the secret from stdout, so any external secrets manager can plug in
{ "fromFile": "~/.config/midscene/key" } Separate 0600 key file, so the config itself stays shareable
{ "fromEnv": "VAR" } Current behaviour

Three small additions that help on their own:

  • --config <path> on the Skills CLIs (@midscene/web, @midscene/computer, …), as the legacy YAML runner already has. Removes the cwd dependence without changing the config format.
  • MIDSCENE_REDACT_ENV=true to blank MIDSCENE_*_API_KEY values in Midscene's own logs and crash dumps.

Hope for consideration.