{"record":{"id":"4e02cd1b83d1ee78","repo":"different-ai/openwork","slug":"environment-variable-store-has-an-invalid-format","errorCode":null,"errorMessage":"Environment variable store has an invalid format","messagePattern":"Environment variable store has an invalid format","errorType":"error_code","errorClass":"EnvStoreReadError","httpStatus":null,"severity":"error","filePath":"apps/server/src/env-file.ts","lineNumber":101,"sourceCode":"  try {\n    raw = await readFile(path, \"utf8\");\n  } catch (error) {\n    if ((error as { code?: string }).code === \"ENOENT\") return emptyStore();\n    if (options.tolerateInvalid) return emptyStore();\n    throw new EnvStoreReadError(\"Environment variable store could not be read\");\n  }\n\n  let parsed: Partial<EnvStoreFile>;\n  try {\n    parsed = JSON.parse(raw) as Partial<EnvStoreFile>;\n  } catch {\n    if (options.tolerateInvalid) return emptyStore();\n    throw new EnvStoreReadError(\"Environment variable store is invalid JSON\");\n  }\n\n  if (!parsed || typeof parsed !== \"object\" || !Array.isArray(parsed.variables)) {\n    if (options.tolerateInvalid) return emptyStore();\n    throw new EnvStoreReadError(\"Environment variable store has an invalid format\");\n  }\n\n  const variables = parsed.variables\n    .map(parseRecord)\n    .filter((entry): entry is EnvRecord => Boolean(entry));\n  return {\n    schemaVersion: typeof parsed.schemaVersion === \"number\" ? parsed.schemaVersion : 1,\n    updatedAt: typeof parsed.updatedAt === \"number\" ? parsed.updatedAt : Date.now(),\n    variables,\n  };\n}\n\nasync function writeStore(path: string, variables: EnvRecord[]): Promise<void> {\n  const dir = dirname(path);\n  await ensureDir(dir);\n  const payload: EnvStoreFile = {\n    schemaVersion: 1,\n    updatedAt: Date.now(),","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/apps/server/src/env-file.ts#L83-L119","documentation":"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.","triggerScenarios":"Store file parses as JSON but is not shaped like EnvStoreFile — e.g. `{}`, `[]`, `{\"variables\": {}}`, or a JSON string/number at top level.","commonSituations":"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.","solutions":["Restore the expected schema: {\"variables\":[{\"key\":\"NAME\",\"value\":\"...\",\"updatedAt\":<epoch-ms>}]}\n","Delete the file and recreate entries through the API so the server writes the correct schema","Check whether another tool or sync process is overwriting the file with a foreign format","Enable tolerateInvalid for callers that should treat malformed stores as empty"],"exampleFix":"// before\n{ \"env\": { \"FOO\": \"bar\" } }\n// after\n{ \"variables\": [ { \"key\": \"FOO\", \"value\": \"bar\", \"updatedAt\": 1725000000000 } ] }","handlingStrategy":"type-guard","validationCode":"if (!isEnvStoreShape(JSON.parse(raw))) resetStoreFile(path);","typeGuard":"function isEnvStoreShape(v: unknown): v is { variables: unknown[] } { return typeof v === \"object\" && v !== null && Array.isArray((v as { variables?: unknown }).variables); }","tryCatchPattern":"try { return await readStore(path, opts); } catch { return { variables: [] }; }","preventionTips":["Schema-check after external writes","Use the API, not raw file edits, to modify variables","Validate backups before restoring"],"tags":["schema","validation","env-store"],"backgroundTag":"schema-validation-failed","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}