{"version":3,"file":"selectionSwitch.cjs","names":["resolveLayers","updateHarnessState","requireHarnessPaths","buildPersonaPrompt","path","fs","replaceProfileEnvironment","updateHarnessStateAsync","requireHarnessPaths","buildPersonaPromptAsync","path","writeFileAtomic","nodeHost","replaceProfileEnvironment"],"sources":["../src/services/selectionSwitch/core.ts","../src/services/selectionSwitch/sync.ts","../src/services/selectionSwitch/index.ts"],"sourcesContent":["import type { PoHarnessContext, HarnessState } from '../../types/config';\nimport { type PresetsConfig, resolveLayers } from '../presetConfig';\nimport type { AgentProfile } from '../profiles';\n\n/**\n * Applying a selection axis to harness state.\n *\n * Every axis writes through the harness store and relies on Pi's reload\n * re-executing extension modules, which drops their module-level caches and\n * makes them re-read the environment.\n *\n * These live here rather than in the packages that own the /preset and /profile\n * commands because the synced startup path applies the same switches from its\n * --preset and --profile flags before any command exists to run them.\n * Domains is the exception and stays with its resource staging.\n */\n\nexport const PERSONA_FILE = 'persona.md';\nexport const PERSONA_FILE_MODE = 0o600;\n\n/** The state patch for one preset. */\nexport function presetPatch(\n  config: PresetsConfig,\n  preset: string,\n  _state: PoHarnessContext,\n  compositionFingerprint?: string,\n  childExtensions?: readonly string[],\n): Partial<HarnessState> {\n  const selected = resolveLayers(config, preset);\n  return {\n    preset,\n    layers: selected,\n    compositionFingerprint,\n    ...(childExtensions ? { childExtensions: [...childExtensions] } : {}),\n  };\n}\n\n/**\n * Written unconditionally, including as undefined, so switching to a profile\n * that declares nothing clears the previous persona's name, icon, and voice.\n */\nexport function profilePatch(profile: AgentProfile, profileEnvironment: Record<string, string>): Partial<HarnessState> {\n  return {\n    profile: profile.name,\n    profileEnvironment,\n    profileIdentity: profile.identity,\n    profileVoice: profile.voice,\n  };\n}\n\nexport function noPersonaFiles(profile: AgentProfile): Error {\n  return new Error(`Profile ${profile.name} has no readable persona files`);\n}\n\nexport function profileSummary(profile: AgentProfile): string {\n  const environmentKeys = Object.keys(profile.env).sort();\n  const environmentSummary = environmentKeys.length > 0 ? `; env defaults ${environmentKeys.join(', ')}` : '';\n  return `Loaded ${profile.name}: persona ${profile.persona}${environmentSummary}.`;\n}\n","import fs from 'node:fs';\nimport path from 'node:path';\n\nimport type { PoHarnessContext, HarnessState } from '../../types/config';\nimport { requireHarnessPaths, updateHarnessState } from '../harnessStore';\nimport type { PresetsConfig } from '../presetConfig';\nimport { type AgentProfile, buildPersonaPrompt, replaceProfileEnvironment } from '../profiles';\nimport { PERSONA_FILE, PERSONA_FILE_MODE, presetPatch, noPersonaFiles, profilePatch, profileSummary } from './core';\n\n/**\n * Applies one preset and the hook groups its effective layers contribute.\n *\n * @deprecated Node-only sync shim; use applyPresetAsync. Removed in M2.\n */\nexport function applyPreset(\n  config: PresetsConfig,\n  preset: string,\n  state: PoHarnessContext,\n  compositionFingerprint?: string,\n  childExtensions?: readonly string[],\n): HarnessState {\n  return updateHarnessState(presetPatch(config, preset, state, compositionFingerprint, childExtensions));\n}\n\n/**\n * Persona: rewrites the assembled prompt file that the persona extension reads.\n *\n * The extension re-reads the file on reload and appends its block to whatever\n * system prompt it is given, so the repository's own AGENTS.md keeps applying\n * underneath. Returns false when the directories hold no readable persona.\n *\n * @deprecated Node-only shim over node:fs; use applyPersonaAsync. Removed in M2.\n */\nexport async function applyPersona(\n  personaDirectory: string,\n  state: PoHarnessContext,\n  personaRoot?: string,\n  personaPrompt?: string,\n): Promise<boolean> {\n  const { root, temporaryDirectory } = requireHarnessPaths(state);\n\n  const prompt = personaPrompt ?? buildPersonaPrompt(personaRoot ?? root, personaDirectory);\n  if (!prompt) return false;\n\n  const personaPath = path.join(temporaryDirectory, PERSONA_FILE);\n  await fs.promises.writeFile(personaPath, `${prompt}\\n`, { mode: PERSONA_FILE_MODE });\n  updateHarnessState({ personaFile: personaPath });\n  return true;\n}\n\n/**\n * Profile: one persona plus the environment defaults it declares.\n *\n * @deprecated Node-only shim over node:fs; use applyProfileAsync. Removed in M2.\n */\nexport async function applyProfile(profile: AgentProfile, state: PoHarnessContext): Promise<string> {\n  const appliedPersona = await applyPersona(profile.persona, state, profile.personaRoot, profile.personaPrompt);\n  if (!appliedPersona) throw noPersonaFiles(profile);\n\n  const profileEnvironment = replaceProfileEnvironment(process.env, state.profileEnvironment, profile.env);\n  updateHarnessState(profilePatch(profile, profileEnvironment));\n  return profileSummary(profile);\n}\n","import path from 'node:path';\n\nimport { nodeHost, writeFileAtomic, type FileSystem } from '@agimon-ai/po-core/platform';\n\nimport type { PoHarnessContext, HarnessState } from '../../types/config';\nimport { requireHarnessPaths, updateHarnessStateAsync } from '../harnessStore';\nimport type { PresetsConfig } from '../presetConfig';\nimport { type AgentProfile, buildPersonaPromptAsync, replaceProfileEnvironment } from '../profiles';\nimport { PERSONA_FILE, PERSONA_FILE_MODE, presetPatch, noPersonaFiles, profilePatch, profileSummary } from './core';\n\ntype IoContext = Parameters<FileSystem['readTextFile']>[1];\n\nexport interface SelectionSwitchOptions {\n  /** The admitted environment the harness state pointer lives in. Defaults to empty. */\n  readonly environment?: NodeJS.ProcessEnv;\n}\n\n/** Applies one preset over a FileSystem; the same patch `applyPreset` writes. */\nexport async function applyPresetAsync(\n  fs: FileSystem,\n  config: PresetsConfig,\n  preset: string,\n  state: PoHarnessContext,\n  context: IoContext,\n  options: SelectionSwitchOptions & {\n    readonly compositionFingerprint?: string;\n    readonly childExtensions?: readonly string[];\n  } = {},\n): Promise<HarnessState> {\n  const patch = presetPatch(config, preset, state, options.compositionFingerprint, options.childExtensions);\n  return updateHarnessStateAsync(fs, patch, options.environment ?? {}, context);\n}\n\n/**\n * Persona over a FileSystem: rewrites the assembled prompt file that the persona\n * extension reads, readable by its owner only. Returns false when the\n * directories hold no readable persona.\n */\nexport async function applyPersonaAsync(\n  fs: FileSystem,\n  personaDirectory: string,\n  state: PoHarnessContext,\n  context: IoContext,\n  options: SelectionSwitchOptions & { readonly personaRoot?: string; readonly personaPrompt?: string } = {},\n): Promise<boolean> {\n  const { root, temporaryDirectory } = requireHarnessPaths(state);\n\n  const prompt =\n    options.personaPrompt ??\n    (await buildPersonaPromptAsync(fs, options.personaRoot ?? root, personaDirectory, context));\n  if (!prompt) return false;\n\n  const personaPath = path.join(temporaryDirectory, PERSONA_FILE);\n  await writeFileAtomic(fs, personaPath, `${prompt}\\n`, context);\n  await nodeHost.setMode(personaPath, PERSONA_FILE_MODE);\n  await updateHarnessStateAsync(fs, { personaFile: personaPath }, options.environment ?? {}, context);\n  return true;\n}\n\n/** Profile over a FileSystem: one persona plus the environment defaults it declares. */\nexport async function applyProfileAsync(\n  fs: FileSystem,\n  profile: AgentProfile,\n  state: PoHarnessContext,\n  context: IoContext,\n  options: SelectionSwitchOptions = {},\n): Promise<string> {\n  const environment = options.environment ?? {};\n  const appliedPersona = await applyPersonaAsync(fs, profile.persona, state, context, {\n    environment,\n    personaRoot: profile.personaRoot,\n    personaPrompt: profile.personaPrompt,\n  });\n  if (!appliedPersona) throw noPersonaFiles(profile);\n\n  const profileEnvironment = replaceProfileEnvironment(environment, state.profileEnvironment, profile.env);\n  await updateHarnessStateAsync(fs, profilePatch(profile, profileEnvironment), environment, context);\n  return profileSummary(profile);\n}\n\nexport { applyPreset, applyPersona, applyProfile } from './sync';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAiBA,MAAa,eAAe;;AAI5B,SAAgB,YACd,QACA,QACA,QACA,wBACA,iBACuB;CAEvB,OAAO;EACL;EACA,QAHeA,qBAAAA,cAAc,QAAQ,MAGtB;EACf;EACA,GAAI,kBAAkB,EAAE,iBAAiB,CAAC,GAAG,eAAe,EAAE,IAAI,CAAC;CACrE;AACF;;;;;AAMA,SAAgB,aAAa,SAAuB,oBAAmE;CACrH,OAAO;EACL,SAAS,QAAQ;EACjB;EACA,iBAAiB,QAAQ;EACzB,cAAc,QAAQ;CACxB;AACF;AAEA,SAAgB,eAAe,SAA8B;CAC3D,uBAAO,IAAI,MAAM,WAAW,QAAQ,KAAK,+BAA+B;AAC1E;AAEA,SAAgB,eAAe,SAA+B;CAC5D,MAAM,kBAAkB,OAAO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