import { randomUUID } from 'node:crypto'; import { mkdirSync, readFileSync, readdirSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import type { AuthProfile } from './auth.js'; import { getUserDataAccountDir } from './user-data.js'; const REF = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/; const SCHEMA = 'ccl.startup-context.v1'; const RETAINED_CONTEXTS = 32; export interface DeferredLoadContext { sync: Record; changes?: Record; capabilities: Record; configuration_warnings: readonly Record[]; persona_available: boolean; } interface StoredContext extends DeferredLoadContext { schema: typeof SCHEMA; context_ref: string; prepared_at: string; } function directory(workspace: string, profile: Pick): string { return join(getUserDataAccountDir(workspace, profile), 'startup-contexts'); } /** Save notices and capability metadata without persona/memory text or a reusable participation snapshot. */ export function saveStartupContext( workspace: string, profile: Pick, context: DeferredLoadContext, ): { context_ref: string; prepared_at: string } { const root = directory(workspace, profile); mkdirSync(root, { recursive: true }); const context_ref = randomUUID(); const prepared_at = new Date().toISOString(); const value: StoredContext = { schema: SCHEMA, context_ref, prepared_at, ...context }; writeFileSync(join(root, `${context_ref}.json`), JSON.stringify(value), { flag: 'wx', mode: 0o600 }); const pointer = join(root, `${context_ref}.pointer`); writeFileSync(pointer, context_ref, { flag: 'wx', mode: 0o600 }); renameSync(pointer, join(root, 'latest')); const previous = readdirSync(root).filter(name => name.endsWith('.json') && REF.test(name.slice(0, -5)) && name !== `${context_ref}.json`) .map(name => ({ name, modified: statSync(join(root, name)).mtimeMs })) .sort((a, b) => b.modified - a.modified); for (const file of previous.slice(RETAINED_CONTEXTS - 1)) unlinkSync(join(root, file.name)); return { context_ref, prepared_at }; } function record(value: unknown): value is Record { return value !== null && typeof value === 'object' && !Array.isArray(value); } /** Retrieve the latest preparation under the active account without running synchronization again. */ export function readStartupContext( workspace: string, profile: Pick, ): StoredContext { let ref: string; try { ref = readFileSync(join(directory(workspace, profile), 'latest'), 'utf8').trim(); } catch { throw new Error('No prepared context for this account. Run ccl prepare before ccl load.'); } if (!REF.test(ref)) throw new Error('Invalid stored startup context pointer.'); let value: unknown; try { value = JSON.parse(readFileSync(join(directory(workspace, profile), `${ref}.json`), 'utf8')); } catch { throw new Error('Startup context is unavailable for this account. Do not repeat start/join; retain already received context and report the missing preparation.'); } if (!record(value) || value.schema !== SCHEMA || value.context_ref !== ref || typeof value.prepared_at !== 'string' || typeof value.persona_available !== 'boolean' || !record(value.sync) || !record(value.capabilities) || (value.changes !== undefined && !record(value.changes)) || !Array.isArray(value.configuration_warnings) || !value.configuration_warnings.every(record)) { throw new Error('Invalid stored startup context. Do not repeat start/join.'); } return { schema: SCHEMA, context_ref: ref, prepared_at: value.prepared_at, sync: value.sync, capabilities: value.capabilities, ...(value.changes ? { changes: value.changes } : {}), configuration_warnings: value.configuration_warnings, persona_available: value.persona_available, }; }