{"version":3,"file":"selectionSwitch.cjs","names":["filterHookDisabledLayers","resolveLayers","updateHarnessState","layerHookGroups","requireHarnessPaths","buildPersonaPrompt","path","fs","replaceProfileEnvironment"],"sources":["../src/services/selectionSwitch/index.ts"],"sourcesContent":["import fs from 'node:fs';\nimport path from 'node:path';\n\nimport type { DoomHarnessContext, HarnessState } from '../../types/config';\nimport { requireHarnessPaths, updateHarnessState } from '../harnessStore';\nimport { filterHookDisabledLayers, layerHookGroups, type MajorModesConfig, resolveLayers } from '../majorModes';\nimport { type AgentProfile, buildPersonaPrompt, replaceProfileEnvironment } from '../profiles';\n\nconst PERSONA_FILE = 'persona.md';\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 /mode and /profile\n * commands because the synced startup path applies the same switches from its\n * --major-mode and --profile flags before any command exists to run them.\n * Domains is the exception and stays with its resource staging.\n */\n\n/** Applies one major mode and the hook groups its effective layers contribute. */\nexport function applyMajorMode(\n  config: MajorModesConfig,\n  majorMode: string,\n  state: DoomHarnessContext,\n  compositionFingerprint?: string,\n  childExtensions?: readonly string[],\n): HarnessState {\n  const selected = filterHookDisabledLayers(config, resolveLayers(config, majorMode), state.hooks);\n  return updateHarnessState({\n    majorMode,\n    layers: selected,\n    compositionFingerprint,\n    ...(childExtensions ? { childExtensions: [...childExtensions] } : {}),\n    hookGroups: layerHookGroups(config, selected),\n  });\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 */\nexport async function applyPersona(\n  personaDirectory: string,\n  state: DoomHarnessContext,\n  personaRoot?: string,\n): Promise<boolean> {\n  const { root, temporaryDirectory } = requireHarnessPaths(state);\n\n  const prompt = 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: 0o600 });\n  updateHarnessState({ personaFile: personaPath });\n  return true;\n}\n\n/** Profile: one persona plus the environment defaults it declares. */\nexport async function applyProfile(profile: AgentProfile, state: DoomHarnessContext): Promise<string> {\n  const appliedPersona = await applyPersona(profile.persona, state, profile.personaRoot);\n  if (!appliedPersona) throw new Error(`Profile ${profile.name} has no readable persona files`);\n\n  const profileEnvironment = replaceProfileEnvironment(process.env, state.profileEnvironment, profile.env);\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  updateHarnessState({\n    profile: profile.name,\n    profileEnvironment,\n    profileIdentity: profile.identity,\n    profileVoice: profile.voice,\n  });\n\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"],"mappings":";;;;;;;;;;AAQA,MAAM,eAAe;;;;;;;;;;;;;;AAgBrB,SAAgB,eACd,QACA,WACA,OACA,wBACA,iBACc;CACd,MAAM,WAAWA,mBAAAA,yBAAyB,QAAQC,mBAAAA,cAAc,QAAQ,SAAS,GAAG,MAAM,KAAK;CAC/F,OAAOC,qBAAAA,mBAAmB;EACxB;EACA,QAAQ;EACR;EACA,GAAI,kBAAkB,EAAE,iBAAiB,CAAC,GAAG,eAAe,EAAE,IAAI,CAAC;EACnE,YAAYC,mBAAAA,gBAAgB,QAAQ,QAAQ;CAC9C,CAAC;AACH;;;;;;;;AASA,eAAsB,aACpB,kBACA,OACA,aACkB;CAClB,MAAM,EAAE,MAAM,uBAAuBC,qBAAAA,oBAAoB,KAAK;CAE9D,MAAM,SAASC,iBAAAA,mBAAmB,eAAe,MAAM,gBAAgB;CACvE,IAAI,CAAC,QAAQ,OAAO;CAEpB,MAAM,cA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