profile-boot.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * Shared profile boot for every `dsh` surface: resolve the profile, stack its
  3. * patch layers (bundle layers in `dsh.profile.bundles` order, the profile's
  4. * own `cordis.patch.yml`, `--patch` overlays, the telemetry switch), mount the
  5. * tree over the profile's empty root config, apply its selected patch-reload
  6. * lifecycle, and wire fail-loud plus bounded shutdown.
  7. *
  8. * App flags are not the launcher's business: the invocation's inner arguments
  9. * are provided to the tree through `ctx.cmdlineArgs`, where any injected app
  10. * plugin may read the same immutable snapshot.
  11. * @module @deepseek-ai/dsh/profile-boot
  12. */
  13. import { writeFileSync } from 'node:fs'
  14. import { join, resolve } from 'node:path'
  15. import { fileURLToPath } from 'node:url'
  16. import { FiberState, type Context } from '@deepseek-ai/cordis'
  17. import type { PatchOptions } from '@deepseek-ai/cordis-plugin-include'
  18. import type { EntryOptions } from '@deepseek-ai/cordis-plugin-loader'
  19. import {
  20. boot,
  21. composeEntries,
  22. healProfilesModuleFallback,
  23. installFailLoud,
  24. loadOptionalPatches,
  25. loadOverlayPatches,
  26. loadProfile,
  27. PROFILE_PATCH_FILENAME,
  28. watchUserPatches,
  29. type Profile,
  30. } from '@deepseek-ai/dsh-app-boot'
  31. import { resolveDshHome } from '@deepseek-ai/dsh-home-paths'
  32. import { installGlobalProxy, resolveProxyPolicy } from '@deepseek-ai/dsh-http-proxy'
  33. import { DSH_LAUNCH_ENVIRONMENT_KEY, type LaunchEnvironmentSnapshot } from '@deepseek-ai/dsh-launch-environment'
  34. import { provideCmdline, type AppReady } from '@deepseek-ai/dsh-cmdline'
  35. import { createProcessShutdown, type ProcessShutdown } from './process-shutdown.ts'
  36. const NAME = 'dsh'
  37. /** Launcher-owned readiness signal committed only after boot and host setup succeed. */
  38. function createAppReady(): { service: AppReady; commit(): void } {
  39. let ready = false
  40. const listeners = new Set<() => void>()
  41. return {
  42. service: {
  43. onReady(listener) {
  44. if (ready) {
  45. listener()
  46. return () => {}
  47. }
  48. listeners.add(listener)
  49. return () => { listeners.delete(listener) }
  50. },
  51. },
  52. commit() {
  53. if (ready) return
  54. ready = true
  55. for (const listener of [...listeners]) listener()
  56. listeners.clear()
  57. },
  58. }
  59. }
  60. /**
  61. * The home-level user patch layer (`$DSH_HOME/cordis.patch.yml`), applied
  62. * over every profile's own layer. Resolved per call, not at module load:
  63. * `$DSH_HOME` may be set by the test or launcher after import.
  64. * @returns the absolute patch-file path.
  65. */
  66. export function homePatchPath(): string {
  67. return join(resolveDshHome(), PROFILE_PATCH_FILENAME)
  68. }
  69. /** Absolute path of this dsh installation's package.json (both anchors: src/ and lib/ sit one level under apps/cli). */
  70. export const INSTALL_ANCHOR = fileURLToPath(new URL('../package.json', import.meta.url))
  71. /** The session-telemetry row id the DSH_TELEMETRY_DISABLED switch targets. */
  72. const TELEMETRY_ROW_ID = 'session-telemetry-otel'
  73. /** The empty root entry list every profile tree patches over. */
  74. const PROFILE_ROOT_CONFIG = `# dsh profile root — an empty entry list. The tree is composed as patches:
  75. # each bundle in package.json's dsh.profile.bundles, then cordis.patch.yml, then any
  76. # --patch overlays. Edit cordis.patch.yml, not this file.
  77. []
  78. `
  79. /** Root config filename inside a profile directory. */
  80. export const PROFILE_ROOT_FILENAME = 'cordis.yml'
  81. /**
  82. * Resolve the telemetry opt-out switch into its boot patch. ANY non-empty
  83. * value (including `'0'`/`'false'`) disables: a privacy switch prefers
  84. * off-by-mistake over on-by-mistake. A composition without the telemetry row
  85. * exports nothing, so the switch is then trivially satisfied and no patch is
  86. * generated — custom profiles need not mount telemetry to run with the
  87. * switch set.
  88. * @param disabledEnv - the raw `DSH_TELEMETRY_DISABLED` value (`undefined` when unset).
  89. * @param hasRow - whether the composition carries the telemetry row.
  90. * @returns the disable patch, or `undefined` when no hard-disable patch is required.
  91. */
  92. export function resolveTelemetryPatch(disabledEnv: string | undefined, hasRow: boolean): PatchOptions | undefined {
  93. if ((disabledEnv ?? '') === '' || !hasRow) return undefined
  94. return { id: TELEMETRY_ROW_ID, disabled: true }
  95. }
  96. /**
  97. * Load a resolved profile for `name` and (re)write the empty root config. The
  98. * root is always rewritten: the whole composition is patch layers, and the
  99. * vendored Loader's tree write-back (a plugin self-disposing persists the
  100. * current tree) can bake composed rows into this file — which would duplicate
  101. * every bundle insert on the next boot. The file exists on disk only because
  102. * the Loader needs a real include root to anchor `baseUrl` at the profile
  103. * directory (the config dump anchors on the same file, so both compose over
  104. * the identical base).
  105. * @param name - the profile name.
  106. * @param userLayer - `false` skips parsing `cordis.patch.yml` (the default dump).
  107. * @returns the loaded profile.
  108. */
  109. export function prepareProfile(name: string, userLayer = true): Profile {
  110. const profile = loadProfile(NAME, name, INSTALL_ANCHOR, undefined, { userLayer })
  111. writeFileSync(join(profile.dir, PROFILE_ROOT_FILENAME), PROFILE_ROOT_CONFIG)
  112. return profile
  113. }
  114. /** One profile's patch layers, in application order. */
  115. interface ComposedProfile {
  116. profile: Profile
  117. /** Bundle layers concatenated — the part below the user layers on a live reload. */
  118. bundlePatches: PatchOptions[]
  119. /** The home-level user layer (`$DSH_HOME/cordis.patch.yml`), applied after the profile's own. */
  120. homePatches: PatchOptions[]
  121. /** Layers above the user layers on a live reload: `--patch` overlays and the telemetry switch. */
  122. overlays: PatchOptions[]
  123. }
  124. /** The full patch stack of one composed profile, in application order. */
  125. function allPatches(composed: ComposedProfile): PatchOptions[] {
  126. return [
  127. ...composed.bundlePatches,
  128. ...composed.profile.patches,
  129. ...composed.homePatches,
  130. ...composed.overlays,
  131. ]
  132. }
  133. /**
  134. * Load `name` and compose its effective patch stack: bundle layers in
  135. * `dsh.profile.bundles` order (a base-backed profile gets the base bundle's
  136. * platform-gated shell rows), the profile's user layer, the home-level user
  137. * layer (`$DSH_HOME/cordis.patch.yml` — machine-local preferences that apply
  138. * to every profile, so it outranks the per-profile layer), `--patch` overlays,
  139. * then the telemetry switch.
  140. * @param name - the profile name.
  141. * @param patchFiles - `--patch` overlay paths, in argv order.
  142. * @returns the profile and its patch layers.
  143. */
  144. async function composeProfile(
  145. name: string,
  146. patchFiles: readonly string[],
  147. ): Promise<ComposedProfile> {
  148. const profile = prepareProfile(name)
  149. await healProfilesModuleFallback({ installAnchor: INSTALL_ANCHOR, profile })
  150. const homePatches = loadOptionalPatches(NAME, homePatchPath()) ?? []
  151. const overlays = patchFiles.flatMap(file => loadOverlayPatches(NAME, resolve(file)))
  152. const bundlePatches = profile.layers.flatMap(layer => layer.patches)
  153. const rows = new Map<string, EntryOptions>()
  154. for (const row of composeEntries([bundlePatches, profile.patches, homePatches, overlays])) {
  155. if (typeof row.id === 'string') rows.set(row.id, row)
  156. }
  157. const composedOverlays = [...overlays]
  158. const telemetryPatch = resolveTelemetryPatch(process.env.DSH_TELEMETRY_DISABLED, rows.has(TELEMETRY_ROW_ID))
  159. if (telemetryPatch !== undefined) composedOverlays.push(telemetryPatch)
  160. return { profile, bundlePatches, homePatches, overlays: composedOverlays }
  161. }
  162. /** Options for {@link runProfile}. */
  163. export interface RunProfileOptions {
  164. /** This run's frozen environment snapshot, provided before any entry mounts. */
  165. environment: LaunchEnvironmentSnapshot
  166. /** The profile name to boot. */
  167. profile: string
  168. /** `--patch` overlay paths, in argv order. */
  169. patchFiles: readonly string[]
  170. /** The invocation's inner arguments, handed to the tree through `ctx.cmdlineArgs`. */
  171. args: readonly string[]
  172. }
  173. /**
  174. * Re-throw a watcher-setup failure unless a shutdown already owns the tree:
  175. * a signal aborted this invocation, or an app requested exit (`ctx.appExit`
  176. * from a fast one-shot) and the root's disposal rejected the in-flight setup
  177. * await. Either way the failure describes a tree that is exiting as asked,
  178. * not a broken watch.
  179. * @param ctx - the booted root context.
  180. * @param signal - this invocation's signal-shutdown fact.
  181. * @param error - the setup failure.
  182. */
  183. function suppressShutdownError(ctx: Context, signal: AbortSignal, error: unknown): void {
  184. if (signal.aborted) return
  185. if (ctx.fiber.state !== FiberState.ACTIVE || ctx.get('loader') === undefined) return
  186. throw error
  187. }
  188. /**
  189. * Boot one profile invocation end to end and leave process lifetime to the
  190. * mounted plugins (or to a one-shot runner the composition mounts).
  191. * @param options - environment snapshot, profile name, overlays, and the booted app's own arguments.
  192. * @returns the settled root context and the shutdown controller.
  193. */
  194. export async function runProfile(options: RunProfileOptions): Promise<{ ctx: Context; shutdown: ProcessShutdown }> {
  195. // Before the first plugin mounts and before anything can issue a request: Node's fetch ignores the
  196. // proxy environment on its own, so every profile would otherwise connect directly. Resolving from
  197. // the launcher's snapshot — not `process.env` — is what lets a proxy declared in a `.env` layer
  198. // work, which the NODE_USE_ENV_PROXY flag cannot do because Node samples the environment at start.
  199. const { policy: proxyPolicy, diagnostics } = resolveProxyPolicy(options.environment)
  200. // A proxy variable may have been exported for other tools, so a value this harness cannot use is
  201. // reported and skipped rather than being allowed to stop the agent from starting.
  202. for (const diagnostic of diagnostics) process.stderr.write(`${NAME}: ${diagnostic.message}\n`)
  203. const disposeProxy = await installGlobalProxy(proxyPolicy)
  204. const composed = await composeProfile(options.profile, options.patchFiles)
  205. const app: { current?: Context } = {}
  206. const appReady = createAppReady()
  207. const shutdown = createProcessShutdown(async () => {
  208. await app.current?.fiber.dispose()
  209. await disposeProxy()
  210. })
  211. const signalShutdown = new AbortController()
  212. const interrupt = (code: number): void => {
  213. signalShutdown.abort()
  214. shutdown.interrupt(code)
  215. }
  216. // Signals own teardown throughout the startup window, not only after boot()
  217. // settles: an inserted provider can publish before sibling rows finish mounting.
  218. // SIGTERM is a supervisor's ordinary stop request and exits 0 on every
  219. // surface — the launcher does not know whether the app considered its work
  220. // complete; SIGINT is a user interrupt and reports 130.
  221. process.on('SIGTERM', () => { interrupt(0) })
  222. process.on('SIGINT', () => { interrupt(130) })
  223. installFailLoud(NAME, process, async () => {
  224. await app.current?.fiber.dispose()
  225. })
  226. const rootConfig = join(composed.profile.dir, PROFILE_ROOT_FILENAME)
  227. // Recomposition for the live user layers: bundle layers below, overlays
  228. // above, so a user edit can never displace them. Parsed app arguments are
  229. // not in here at all — they live in app-provided services that survive a
  230. // recomposition. BOTH
  231. // user files are re-read per generation (the HMR watcher hands us only the
  232. // changed file's patches, which one of the reads duplicates — fresh reads
  233. // keep the two watchers from stitching in each other's stale copy).
  234. // Fresh clones per generation: the include pushes `insert` rows into the
  235. // mounted tree BY REFERENCE and later id-targeted patches mutate those
  236. // objects in place. Reusing one parsed patch object across applications
  237. // would bake a user override into the bundle's in-memory insert row, so
  238. // removing the override could never revert the row to the bundle default.
  239. const composeLive = (): PatchOptions[] => structuredClone([
  240. ...composed.bundlePatches,
  241. ...loadOptionalPatches(NAME, composed.profile.patchPath) ?? [],
  242. ...loadOptionalPatches(NAME, homePatchPath()) ?? [],
  243. ...composed.overlays,
  244. ])
  245. // Cloned for the same insert-aliasing reason as composeLive: the boot
  246. // application must not mutate the objects later reloads recompose from.
  247. const ctx = await boot(NAME, rootConfig, structuredClone(allPatches(composed)), (hostCtx) => {
  248. app.current = hostCtx
  249. // Before any config-tree entry mounts, so plugins resolve all launch-time
  250. // environment values from the same immutable provenance snapshot.
  251. hostCtx.provide(DSH_LAUNCH_ENVIRONMENT_KEY, options.environment)
  252. // The command line and bounded exit request are launcher facts available
  253. // to every app plugin that injects the argument snapshot.
  254. provideCmdline(hostCtx, {
  255. args: options.args,
  256. exit: code => void shutdown.shutdown(code),
  257. ready: appReady.service,
  258. })
  259. })
  260. app.current = ctx
  261. // A live-reload profile can dispose the whole tree while post-boot watcher
  262. // setup is in flight — a signal or appExit. Loader presence and fiber state
  263. // own liveness; the initial check skips a tree that already exited, and the
  264. // catch below re-checks for an exit that landed mid-setup. Startup-frozen
  265. // profiles apply every user layer above but install no HMR fallback or watcher.
  266. if (composed.profile.patchReload === 'live'
  267. && !signalShutdown.signal.aborted
  268. && ctx.fiber.state === FiberState.ACTIVE
  269. && ctx.get('loader') !== undefined) {
  270. try {
  271. // Config-only HMR for the live profile patch layer: dsh-base disables
  272. // module reload by default, so when no profile explicitly enabled that
  273. // service, mount a watch-only instance with no module roots —
  274. // cordis.patch.yml edits stay live without replacing source modules. A
  275. // silent skip would break the documented reload contract. HMR injects
  276. // the timer service, which a bare custom profile may not mount either.
  277. if (ctx.get('hmr') === undefined) {
  278. if (ctx.get('timer') === undefined) {
  279. await ctx.loader.create({ name: '@deepseek-ai/cordis-plugin-timer' })
  280. }
  281. await ctx.loader.create({ name: '@deepseek-ai/cordis-plugin-hmr', config: { root: [] } })
  282. }
  283. await watchUserPatches(ctx, {
  284. binName: NAME,
  285. filename: composed.profile.patchPath,
  286. compose: composeLive,
  287. })
  288. await watchUserPatches(ctx, {
  289. binName: NAME,
  290. filename: homePatchPath(),
  291. compose: composeLive,
  292. })
  293. } catch (error) {
  294. suppressShutdownError(ctx, signalShutdown.signal, error)
  295. }
  296. }
  297. if (!signalShutdown.signal.aborted
  298. && ctx.fiber.state === FiberState.ACTIVE
  299. && ctx.get('loader') !== undefined) {
  300. appReady.commit()
  301. }
  302. return { ctx, shutdown }
  303. }