{"record":{"id":"f4131caafd3e130e","repo":"JuliusBrussee/caveman","slug":"kilo-db-contains-an-invalid-path-character","errorCode":null,"errorMessage":"KILO_DB contains an invalid path character","messagePattern":"KILO_DB contains an invalid path character","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":11665,"sourceCode":"  try {\n    preferenceFiles.unshift(join(\"/Library/Managed Preferences\", userInfo().username, \"ai.opencode.managed.plist\"));\n  } catch {\n    // System-level preference remains covered; an unreadable username is itself\n    // unusual, but no user path can be resolved safely enough to claim absence.\n    return true;\n  }\n  return preferenceFiles.some((path) => existsSync(path));\n}\n\nfunction kiloDatabasePath(): string | null {\n  const rawDataRoot = process.env.XDG_DATA_HOME || join(homedir(), \".local\", \"share\");\n  const cleanedDataRoot = rawDataRoot.replace(/[\\r\\n]+/g, \"\");\n  if (!cleanedDataRoot) throw new Error(\"Kilo data root resolves to an empty path\");\n  const dataRoot = normalize(isAbsolute(cleanedDataRoot) ? cleanedDataRoot : resolve(cleanedDataRoot));\n  const configured = process.env.KILO_DB;\n  if (configured === \":memory:\") return null;\n  if (configured) {\n    if (/[\\0\\r\\n]/.test(configured)) throw new Error(\"KILO_DB contains an invalid path character\");\n    return normalize(isAbsolute(configured) ? configured : join(dataRoot, \"kilo\", configured));\n  }\n  return join(dataRoot, \"kilo\", \"kilo.db\");\n}\n\nfunction kiloActiveOrganizationState(): \"none\" | \"active\" | \"unknown\" {\n  let path: string | null;\n  try {\n    path = kiloDatabasePath();\n    if (path === null) return \"none\";\n    const stat = statSync(path);\n    if (!stat.isFile()) return \"unknown\";\n  } catch (error) {\n    if ((error as NodeJS.ErrnoException).code === \"ENOENT\") return \"none\";\n    return \"unknown\";\n  }\n  const script = [\n    'const { DatabaseSync } = require(\"node:sqlite\");',","sourceCodeStart":11647,"sourceCodeEnd":11683,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/5184b3d11ac6a1acb7d44b9bfaa31698157cff97/packages/cli/src/index.ts#L11647-L11683","documentation":"Thrown while resolving the database path when the KILO_DB environment variable contains NUL, carriage return, or newline characters. The library rejects these because they make filesystem paths ambiguous or dangerous and cannot appear in a valid path. It ensures the SQLite database location derived from KILO_DB is a clean, usable path.","triggerScenarios":"Setting process.env.KILO_DB to a value matching /[\\0\\r\\n]/, e.g. a value copied from a Windows file with CRLF line endings, or a shell variable that embedded a literal newline in the path.","commonSituations":"Exporting KILO_DB from a script or CI secret that has trailing newline/CRLF; constructing the env var by concatenating strings that included a line break; pasting a Windows path with embedded CR characters into a .env file.","solutions":["Remove newline/CR/NUL characters from the KILO_DB value: export KILO_DB=\"$(printf '%s' \"$KILO_DB\" | tr -d '\\r\\n')\".","Verify the env var with node -e 'console.log(JSON.stringify(process.env.KILO_DB))' to reveal hidden control characters.","Unset KILO_DB entirely to fall back to the default <dataRoot>/kilo/kilo.db path.","Fix the source (CI secret, .env file, shell profile) that injects the control character instead of patching it at use time."],"exampleFix":"// before\nexport KILO_DB=\"db/kilo.db\r\"\n// after\nexport KILO_DB=\"db/kilo.db\"","handlingStrategy":"validation","validationCode":"const kb = process.env.KILO_DB;\nif (kb && /[\\0\\r\\n]/.test(kb)) throw new Error(\"KILO_DB contains an invalid path character\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set KILO_DB with printf '%s' or tr -d '\\r\\n' when sourcing from files or command output.","Avoid .env parsers that don't strip line endings for CI-injected secrets.","Sanity-check env vars with JSON.stringify output before use in path contexts."],"tags":["env-var","path-validation","database-config"],"backgroundTag":"invalid-env-var-value","analyzedSha":"5184b3d11ac6a1acb7d44b9bfaa31698157cff97","analyzedAt":"2026-09-06T12:00:26.372Z","contentChangedAt":"2026-09-06T12:00:26.372Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}