different-ai/openwork · error · InvalidEnvKeyError

invalid_env_key

invalid_env_key

Error message

invalid_env_key

What it means

upsertMany validates each key with isValidEnvKey before persisting and throws InvalidEnvKeyError (code invalid_env_key) when a key is not a legal environment-variable name (typically /^[A-Za-z_][A-Za-z0-9_]*$/ style).

Source

Thrown at apps/server/src/env-file.ts:203

      () => {},
      () => {},
    );
    return run;
  }

  async list(): Promise<EnvRecord[]> {
    await this.ensureLoaded();
    return this.variables.slice();
  }

  async upsertMany(entries: EnvEntry[]): Promise<void> {
    return this.enqueueMutation(async () => {
      await this.ensureLoaded();
      const now = Date.now();
      const next = new Map(this.variables.map((entry) => [entry.key, entry] as const));
      for (const entry of entries) {
        if (!isValidEnvKey(entry.key)) {
          throw new InvalidEnvKeyError(entry.key, "invalid_env_key");
        }
        if (isReservedEnvKey(entry.key)) {
          throw new InvalidEnvKeyError(entry.key, "reserved_env_key");
        }
        next.set(entry.key, { key: entry.key, value: entry.value, updatedAt: now });
      }
      const nextVariables = Array.from(next.values()).sort((a, b) => a.key.localeCompare(b.key));
      await writeStore(this.path, nextVariables);
      this.variables = nextVariables;
    });
  }

  async delete(key: string): Promise<boolean> {
    return this.enqueueMutation(async () => {
      await this.ensureLoaded();
      const before = this.variables.length;
      const nextVariables = this.variables.filter((entry) => entry.key !== key);
      if (nextVariables.length === before) return false;

View on GitHub (pinned to 2b7df46e8a)

Solutions

  1. Rename the key to a valid identifier: uppercase letters, digits, underscores only, not starting with a digit
  2. Sanitize/normalize keys on the client (e.g. replace non-alphanumerics with underscores) before submitting
  3. Reject invalid keys in the UI at input time so users never submit them

Example fix

// before
await envStore.upsertMany([{ key: "MY-APP-TOKEN", value: "x" }]);
// after
await envStore.upsertMany([{ key: "MY_APP_TOKEN", value: "x" }]);
Defensive patterns

Strategy: validation

Validate before calling

const bad = entries.filter(e => !/^[A-Za-z_][A-Za-z0-9_]*$/.test(e.key)); if (bad.length) throw new Error(`invalid keys: ${bad.map(b => b.key).join(",")}`);

Type guard

const isEnvKey = (k: string): k is string => /^[A-Za-z_][A-Za-z0-9_]*$/.test(k);

Try / catch

try { await envStore.upsertMany(entries); } catch (e) { if (e instanceof InvalidEnvKeyError) { return { skipped: e.key, reason: e.code }; } throw e; }

Prevention

When it happens

Trigger: Calling the env-store upsert API with keys containing hyphens, dots, spaces, leading digits, lowercase is fine but empty strings, or non-ASCII characters are not.

Common situations: Binding UI input straight through without validation; deriving keys from filenames or package names (my-app, foo.bar); copying shell var names with a leading digit like 1PASSWORD.

Related errors


AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01). Data as JSON: /api/errors/4f06fa76d3f752b6. Report an issue: GitHub.