{"record":{"id":"72fde1a7705535d1","repo":"different-ai/openwork","slug":"error-code","errorCode":"error.code","errorMessage":"Environment variable store is invalid. Fix or remove the local env file before editing.","messagePattern":"Environment variable store is invalid\\. Fix or remove the local env file before editing\\.","errorType":"error_code","errorClass":"ApiError","httpStatus":409,"severity":"critical","filePath":"apps/server/src/routes/core.ts","lineNumber":361,"sourceCode":"      throw new ApiError(400, \"invalid_scope\", \"Token scope must be owner, collaborator, or viewer\");\n    }\n    const label = typeof body.label === \"string\" ? body.label.trim() : undefined;\n    const issued = await tokens.create(scope, { label });\n    return jsonResponse(issued, 201);\n  });\n\n  addRoute(routes, \"DELETE\", \"/tokens/:id\", \"host\", async (ctx) => {\n    ensureWritable(config);\n    const ok = await tokens.revoke(ctx.params.id);\n    if (!ok) {\n      throw new ApiError(404, \"token_not_found\", \"Token not found\");\n    }\n    return jsonResponse({ ok: true });\n  });\n\n  function rethrowEnvStoreReadError(error: unknown): never {\n    if (error instanceof EnvStoreReadError) {\n      throw new ApiError(\n        409,\n        error.code,\n        \"Environment variable store is invalid. Fix or remove the local env file before editing.\",\n      );\n    }\n    throw error;\n  }\n\n  // User-level env vars (see apps/app/pr/environment-variables.md). All routes\n  // require the desktop host token (not owner bearer tokens). List callers can\n  // request metadata-only results so renderer settings panes do not receive\n  // every raw secret value up front. Reload semantics are driven from the UI\n  // after a write; this surface is user-scoped, not workspace-scoped, so no audit.\n  addRoute(routes, \"GET\", \"/env\", \"host-token\", async (ctx) => {\n    const includeValues = parseOptionalBoolean(ctx.url.searchParams.get(\"includeValues\"), \"includeValues\") ?? true;\n    const items = await env.list().catch(rethrowEnvStoreReadError);\n    return jsonResponse({\n      items: items.map((item) => ({","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/apps/server/src/routes/core.ts#L343-L379","documentation":"rethrowEnvStoreReadError is a helper that converts EnvStoreReadError — thrown when the server fails to parse the local env variable store file — into a 409 ApiError that carries the store-specific code (error.code) as the ApiError code. It tells the client the env store is corrupt/unreadable and must be repaired before any env read or write can succeed. The original error is rethrown unchanged if it is not an EnvStoreReadError.","triggerScenarios":"Any /env route (list, get, set, delete) calling env.list().catch(rethrowEnvStoreReadError) when the local env file is malformed, has invalid syntax, wrong permissions, or an unsupported format.","commonSituations":"A user hand-edited the env file and introduced a syntax error; a partial write/crash left the file truncated; the file was created by a different app version with an incompatible format.","solutions":["Open the local env file, fix or remove the invalid content (as the message says)","Back up the file, delete it, and re-enter the variables through the API/UI","Check disk permissions on the env store file","Verify the file was not written by an older incompatible version"],"exampleFix":"// before (corrupt env file)\nFOO=bar\nBAR (missing value/quotes -> parse error)\n// after\nFOO=bar\nBAR=baz","handlingStrategy":"try-catch","validationCode":"import { readFileSync } from \"node:fs\";\ntry { readFileSync(envFilePath, \"utf8\"); } catch { console.error(\"env store unreadable — fix before calling /env APIs\"); }","typeGuard":"function isEnvStoreReadError(e: unknown): e is { status: 409; code: string; message: string } {\n  return typeof e === \"object\" && e !== null && (e as { status?: number }).status === 409;\n}","tryCatchPattern":"try {\n  await api.get(\"/env\");\n} catch (e) {\n  if (isEnvStoreReadError(e)) {\n    console.error(`Env store invalid (code=${e.code}); fix or remove the local env file before editing.`);\n    return;\n  }\n  throw e;\n}","preventionTips":["Never hand-edit the env store file while the server is running; use the API/UI","Back up the env file before manual changes","Validate env file syntax after every manual edit","Watch for server crashes/interrupted writes and re-validate the file afterwards"],"tags":["http-409","env-file","config-corruption"],"backgroundTag":"env-store-corrupt","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}