/** * Settings sidecar: `$DSH_HOME/cloud-memory.json`. * * Why a file and not the DSH credentials service: the desktop profile has no * stable host-side credentials face this plugin can depend on (the client-side * `remote.credentials` proxy is a browser service), and a missed dependency * would silently disable reporting. A 0600-ish file under the user's DSH home * directory is the smallest thing that always works; Windows ACLs on * `C:\Users\` already restrict it to that account. * * The file holds the two bearer tokens and the UI-editable overrides. It is * NEVER uploaded, never printed, and `describe()` only exposes a mask. */ import { mkdirSync, readFileSync, renameSync, statSync, writeFileSync, chmodSync } from 'node:fs' import { dirname, resolve } from 'node:path' import { homedir } from 'node:os' import { DEFAULT_SETTINGS, normalizeBaseUrl, settingsFromConfig, type Settings } from './config.ts' /** Persisted document schema. */ export interface PersistedState { version: 1 enabled?: boolean baseUrl?: string clientId?: string requestTimeoutMs?: number defaultK?: number activityToken?: string memoryToken?: string } /** Masked token view handed to the browser. */ export interface TokenView { configured: boolean masked: string } /** Browser-facing settings snapshot (no secrets). */ export interface SettingsView extends Settings { activityToken: TokenView memoryToken: TokenView statePath: string } /** `$DSH_HOME` (or `~/.dsh`) — the same resolution DSH core uses. */ export function dshHome(): string { const fromEnv = process.env.DSH_HOME if (fromEnv !== undefined && fromEnv.trim() !== '') return fromEnv return resolve(homedir(), '.dsh') } /** Absolute path of the settings sidecar. */ export function settingsPath(): string { return resolve(dshHome(), 'cloud-memory.json') } /** Read a token from an env var (an alternative to the sidecar for CI use). */ function envToken(name: string): string | undefined { const value = process.env[name] if (value === undefined || value.trim() === '') return undefined return value.trim() } /** Mask a secret for display: `cmt_abc…9f2c`. */ export function maskToken(token: string): string { if (token.length <= 12) return `${token.slice(0, 3)}…` return `${token.slice(0, 7)}…${token.slice(-4)}` } /** Validate one sidecar field; returns the normalized value or undefined. */ function cleanString(value: unknown): string | undefined { if (typeof value !== 'string') return undefined const trimmed = value.trim() return trimmed === '' ? undefined : trimmed } /** Coerce one numeric field into a clamped integer. */ function clampInt(value: unknown, min: number, max: number): number | undefined { const numeric = typeof value === 'string' ? Number(value) : value if (typeof numeric !== 'number' || !Number.isFinite(numeric)) return undefined return Math.min(max, Math.max(min, Math.trunc(numeric))) } /** Parse an on-disk document defensively (a torn file must not break boot). */ function parseState(raw: string): PersistedState { try { const value: unknown = JSON.parse(raw) if (value === null || typeof value !== 'object' || Array.isArray(value)) return { version: 1 } const source = value as Record const state: PersistedState = { version: 1 } if (typeof source.enabled === 'boolean') state.enabled = source.enabled const baseUrl = normalizeBaseUrl(source.baseUrl) if (baseUrl !== undefined) state.baseUrl = baseUrl const clientId = cleanString(source.clientId) if (clientId !== undefined) state.clientId = clientId.slice(0, 128) const requestTimeoutMs = clampInt(source.requestTimeoutMs, 1000, 60_000) if (requestTimeoutMs !== undefined) state.requestTimeoutMs = requestTimeoutMs const defaultK = clampInt(source.defaultK, 1, 100) if (defaultK !== undefined) state.defaultK = defaultK const activityToken = cleanString(source.activityToken) if (activityToken !== undefined) state.activityToken = activityToken const memoryToken = cleanString(source.memoryToken) if (memoryToken !== undefined) state.memoryToken = memoryToken return state } catch { return { version: 1 } } } /** * In-memory + on-disk settings holder. * * Writes are atomic (tmp file + rename) and best-effort chmod 0600; the reader * is tolerant, so one corrupt document degrades to defaults instead of * failing the whole plugin activation. */ export class SettingsStore { private state: PersistedState private readonly file: string private readonly configDefaults: Settings constructor(rawConfig: unknown, file: string = settingsPath()) { this.file = file this.configDefaults = { ...DEFAULT_SETTINGS, ...settingsFromConfig(rawConfig) } this.state = this.read() } /** Absolute sidecar path (exposed for the status card). */ get path(): string { return this.file } private read(): PersistedState { try { return parseState(readFileSync(this.file, 'utf8')) } catch { return { version: 1 } } } private write(next: PersistedState): void { const directory = dirname(this.file) mkdirSync(directory, { recursive: true }) const temporary = `${this.file}.tmp` writeFileSync(temporary, `${JSON.stringify(next, null, 2)}\n`, { encoding: 'utf8', mode: 0o600 }) renameSync(temporary, this.file) try { chmodSync(this.file, 0o600) } catch { // Windows has no POSIX mode bits; the profile ACL is the real boundary. } } /** Effective settings: config defaults, overridden by sidecar values. */ settings(): Settings { const resolved: Settings = { ...this.configDefaults } if (this.state.enabled !== undefined) resolved.enabled = this.state.enabled if (this.state.baseUrl !== undefined) resolved.baseUrl = this.state.baseUrl if (this.state.clientId !== undefined) resolved.clientId = this.state.clientId if (this.state.requestTimeoutMs !== undefined) resolved.requestTimeoutMs = this.state.requestTimeoutMs if (this.state.defaultK !== undefined) resolved.defaultK = this.state.defaultK return resolved } /** The activity:write bearer token (sidecar first, env fallback). */ activityToken(): string { return this.state.activityToken ?? envToken('DSH_CLOUD_MEMORY_ACTIVITY_TOKEN') ?? '' } /** The memory:read / memory:write bearer token. */ memoryToken(): string { return this.state.memoryToken ?? envToken('DSH_CLOUD_MEMORY_MEMORY_TOKEN') ?? '' } /** Masked snapshot for the browser card. */ describe(): SettingsView { const settings = this.settings() const activity = this.activityToken() const memory = this.memoryToken() return { ...settings, activityToken: { configured: activity !== '', masked: activity === '' ? '' : maskToken(activity) }, memoryToken: { configured: memory !== '', masked: memory === '' ? '' : maskToken(memory) }, statePath: this.file, } } /** * Apply a partial patch from the settings card. * * Token fields follow "absent means keep": only a non-empty string replaces a * stored token, and an explicit boolean `clear*` flag removes one. That way * the browser never has to round-trip a secret it cannot read. */ patch(patch: Record): { ok: boolean; error?: string } { const next: PersistedState = { ...this.state } if (patch.enabled !== undefined) { if (typeof patch.enabled !== 'boolean') return { ok: false, error: 'enabled must be a boolean' } next.enabled = patch.enabled } if (patch.baseUrl !== undefined) { const baseUrl = normalizeBaseUrl(patch.baseUrl) if (baseUrl === undefined) return { ok: false, error: 'baseUrl must be an http(s) URL' } next.baseUrl = baseUrl } if (patch.clientId !== undefined) { const clientId = cleanString(patch.clientId) if (clientId === undefined) return { ok: false, error: 'clientId must be a non-empty string' } next.clientId = clientId.slice(0, 128) } const activityToken = cleanString(patch.activityToken) if (activityToken !== undefined) { if (!activityToken.startsWith('cmt_')) return { ok: false, error: 'activity token must start with cmt_' } next.activityToken = activityToken } const memoryToken = cleanString(patch.memoryToken) if (memoryToken !== undefined) { if (!memoryToken.startsWith('cmt_')) return { ok: false, error: 'memory token must start with cmt_' } next.memoryToken = memoryToken } if (patch.requestTimeoutMs !== undefined) { const requestTimeoutMs = clampInt(patch.requestTimeoutMs, 1000, 60_000) if (requestTimeoutMs === undefined) return { ok: false, error: 'requestTimeoutMs must be a number' } next.requestTimeoutMs = requestTimeoutMs } if (patch.defaultK !== undefined) { const defaultK = clampInt(patch.defaultK, 1, 100) if (defaultK === undefined) return { ok: false, error: 'defaultK must be a number' } next.defaultK = defaultK } if (patch.clearActivityToken === true) delete next.activityToken if (patch.clearMemoryToken === true) delete next.memoryToken this.write(next) this.state = next return { ok: true } } /** File size in bytes, for a "sidecar exists" hint in the card. */ size(): number | undefined { try { return statSync(this.file).size } catch { return undefined } } }