Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
8.1 KB
Raw Blame
import { platform } from "node:os";
import { chmod, readFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { omnirushEnvStorePath } from "@omnirush/paths";
import { writeFileAtomic } from "./atomic-write.js";
import { ensureDir, exists } from "./utils.js";

// User-level environment variables, persisted so the desktop shell can inject
// them into every spawned child (OpenCode and OmniRush.ai server).
// Motivation: Linux GUI launches don't inherit shell env, so users set
// ANTHROPIC_API_KEY / GCLOUD_* / GCP_* in .bashrc and hit silent auth failures.
// Scope: user/machine, not workspace. Not synced to the cloud.

const ENV_KEY_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;

// Keys reserved for internal wiring by the desktop shell and server. This UI
// is for service credentials, not OmniRush.ai/OpenCode runtime knobs; users who
// need OPENCODE_* process settings should set them from the launching shell.
// We refuse writes to these and strip them when reading for injection, so a
// tampered file cannot shadow auth credentials, token paths, or process
// identity.
const RESERVED_PREFIXES = ["OMNIRUSH_", "OPENCODE_"] as const;
const PERSISTABLE_INTERNAL_KEYS = new Set([
  "OMNIRUSH_API_KEY",
  "OMNIRUSH_MODELS_API_KEY",
  "OMNIRUSH_INFERENCE_BASE_URL",
  "OMNIRUSH_MODELS_BASE_URL",
]);

export type EnvRecord = {
  key: string;
  value: string;
  updatedAt: number;
};

type EnvStoreFile = {
  schemaVersion: number;
  updatedAt: number;
  variables: EnvRecord[];
};

export function isValidEnvKey(key: string): boolean {
  return ENV_KEY_PATTERN.test(key);
}

export function isReservedEnvKey(key: string): boolean {
  return isInternalEnvKey(key) && !PERSISTABLE_INTERNAL_KEYS.has(key);
}

function isInternalEnvKey(key: string): boolean {
  return RESERVED_PREFIXES.some((prefix) => key.startsWith(prefix));
}

export function resolveDefaultEnvStorePath(): string {
  return omnirushEnvStorePath();
}

function parseRecord(raw: unknown): EnvRecord | null {
  if (!raw || typeof raw !== "object") return null;
  const record = raw as Partial<EnvRecord>;
  const key = typeof record.key === "string" ? record.key : "";
  const value = typeof record.value === "string" ? record.value : "";
  if (!isValidEnvKey(key)) return null;
  return {
    key,
    value,
    updatedAt: typeof record.updatedAt === "number" ? record.updatedAt : Date.now(),
  };
}

function emptyStore(): EnvStoreFile {
  return { schemaVersion: 1, updatedAt: Date.now(), variables: [] };
}

async function readStore(
  path: string,
  options: { tolerateInvalid?: boolean } = {},
): Promise<EnvStoreFile> {
  if (!(await exists(path))) {
    return emptyStore();
  }
  let raw = "";
  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(),
    variables,
  };
  // 0600 on the temp file before it replaces the store (values may be
  // secrets); a failed write leaves no temp file behind.
  await writeFileAtomic(path, JSON.stringify(payload, null, 2) + "\n", { mode: 0o600 });
  try {
    await chmod(path, 0o600);
  } catch (error) {
    // chmod is a no-op on Windows; values may still contain secrets.
    if (platform() !== "win32") throw error;
  }
}

export type EnvEntry = { key: string; value: string };

export class EnvService {
  private readonly path: string;
  private loaded = false;
  private loadPromise: Promise<void> | null = null;
  private mutationQueue: Promise<void> = Promise.resolve();
  private variables: EnvRecord[] = [];
  private readonly changeListeners = new Set<() => void>();

  onChange(listener: () => void): () => void {
    this.changeListeners.add(listener);
    return () => { this.changeListeners.delete(listener); };
  }

  constructor(options?: { path?: string }) {
    this.path = options?.path ? resolve(options.path) : resolveDefaultEnvStorePath();
  }

  private async ensureLoaded(): Promise<void> {
    if (this.loaded) return;
    if (!this.loadPromise) {
      this.loadPromise = readStore(this.path)
        .then((store) => {
          this.variables = store.variables;
          this.loaded = true;
        })
        .finally(() => {
          this.loadPromise = null;
        });
    }
    await this.loadPromise;
  }

  private enqueueMutation<T>(operation: () => Promise<T>): Promise<T> {
    const run = this.mutationQueue.catch(() => {}).then(operation);
    this.mutationQueue = run.then(
      () => {},
      () => {},
    );
    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;
      for (const listener of this.changeListeners) listener();
    });
  }

  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;
      await writeStore(this.path, nextVariables);
      this.variables = nextVariables;
      for (const listener of this.changeListeners) listener();
      return true;
    });
  }

  // Used by the Electron shell at spawn time. Keep desktop runtime injection
  // in sync on path resolution and reserved-keys policy.
  static async readForInjection(overridePath?: string): Promise<Record<string, string>> {
    const path = overridePath?.trim() ? resolve(overridePath.trim()) : resolveDefaultEnvStorePath();
    const store = await readStore(path, { tolerateInvalid: true });
    const out: Record<string, string> = {};
    for (const entry of store.variables) {
      if (isInternalEnvKey(entry.key)) continue;
      out[entry.key] = entry.value;
    }
    return out;
  }
}

export class EnvStoreReadError extends Error {
  readonly code = "invalid_env_store";
}

export class InvalidEnvKeyError extends Error {
  readonly code: "invalid_env_key" | "reserved_env_key";
  constructor(key: string, code: "invalid_env_key" | "reserved_env_key") {
    super(
      code === "reserved_env_key"
        ? `Environment variable name is reserved for OmniRush.ai internals: ${key}`
        : `Invalid environment variable name: ${key}`,
    );
    this.code = code;
  }
}