different-ai/openwork · error · EnvStoreReadError

Environment variable store has an invalid format

Error message

Environment variable store has an invalid format

What it means

Error thrown when the environment variable store parses as JSON but does not match the expected schema/format (e.g., the top-level value is not an object or entries are not key/value strings). Usually results from hand-editing the store file or an older store layout. Fix by rewriting the store as a flat JSON object of string-to-string environment variable entries.

Source

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

  try {
    raw = await readFile(path, "utf8");
  } catch (error) {
    if ((error as { code?: string }).code === "ENOENT") return emptyStore();
    if (options.tolerateInvalid) return emptyStore();
    throw new EnvStoreReadError("Environment variable store could not be read");
  }

  let parsed: Partial<EnvStoreFile>;
  try {
    parsed = JSON.parse(raw) as Partial<EnvStoreFile>;
  } catch {
    if (options.tolerateInvalid) return emptyStore();
    throw new EnvStoreReadError("Environment variable store is invalid JSON");
  }

  if (!parsed || typeof parsed !== "object" || !Array.isArray(parsed.variables)) {
    if (options.tolerateInvalid) return emptyStore();
    throw new EnvStoreReadError("Environment variable store has an invalid format");
  }

  const variables = parsed.variables
    .map(parseRecord)
    .filter((entry): entry is EnvRecord => Boolean(entry));
  return {
    schemaVersion: typeof parsed.schemaVersion === "number" ? parsed.schemaVersion : 1,
    updatedAt: typeof parsed.updatedAt === "number" ? parsed.updatedAt : Date.now(),
    variables,
  };
}

async function writeStore(path: string, variables: EnvRecord[]): Promise<void> {
  const dir = dirname(path);
  await ensureDir(dir);
  const payload: EnvStoreFile = {
    schemaVersion: 1,
    updatedAt: Date.now(),

View on GitHub (pinned to 2b7df46e8a)

Solutions

  1. Restore the expected schema: {"variables":[{"key":"NAME","value":"...","updatedAt":<epoch-ms>}]}
  2. Delete the file and recreate entries through the API so the server writes the correct schema
  3. Check whether another tool or sync process is overwriting the file with a foreign format
  4. Enable tolerateInvalid for callers that should treat malformed stores as empty

Example fix

// before
{ "env": { "FOO": "bar" } }
// after
{ "variables": [ { "key": "FOO", "value": "bar", "updatedAt": 1725000000000 } ] }
Defensive patterns

Strategy: type-guard

Validate before calling

if (!isEnvStoreShape(JSON.parse(raw))) resetStoreFile(path);

Type guard

function isEnvStoreShape(v: unknown): v is { variables: unknown[] } { return typeof v === "object" && v !== null && Array.isArray((v as { variables?: unknown }).variables); }

Try / catch

try { return await readStore(path, opts); } catch { return { variables: [] }; }

Prevention

When it happens

Trigger: Store file parses as JSON but is not shaped like EnvStoreFile — e.g. `{}`, `[]`, `{"variables": {}}`, or a JSON string/number at top level.

Common situations: User overwrote the store with a different tool's config format; a migration or backup restore wrote the wrong schema; hand-editing renamed the variables key.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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