From 561780923a797a511359ae7a057f8e50a276a533 Mon Sep 17 00:00:00 2001 From: QQ Date: Tue, 17 Nov 2026 08:47:35 +0800 Subject: [PATCH] Import packages/plugin/src/index.ts --- packages/plugin/src/index.ts | 95 +++++++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 13 deletions(-) diff --git a/packages/plugin/src/index.ts b/packages/plugin/src/index.ts index 0629349..fb4e52a 100644 --- a/packages/plugin/src/index.ts +++ b/packages/plugin/src/index.ts @@ -20,7 +20,9 @@ import { resetDefaultIdentityRoles, serverStorageDirectory, } from './identity-store.js' -import { SafeLogger } from './logging.js' +import { basename, dirname } from 'node:path' +import { HostLogStore, resolveHostLogPath } from './host-log-store.js' +import { SafeLogger, type LogLevel } from './logging.js' import { safeStartupError } from './safe-error.js' import { HostPluginRuntime } from './service.js' import type { ScheduleCreateService } from './harness-remote-bridge.js' @@ -36,6 +38,7 @@ import { editableConfig } from './control-runtime.js' import { setLiteModeEnabled } from './lite-mode.js' import { runHmrSafe, type HmrContextLike } from './hmr-safe-settings.js' import { PluginUpdateChecker } from './update-check.js' +import { LocalPluginUpgrade, type PluginManagerUpdate } from './plugin-self-update.js' import { assertRemoteEventSource } from './tui-event-source.js' import { createOwnerPeerFactory, @@ -141,6 +144,7 @@ export function apply(ctx: Context, entry: EntryConfig | ConfigShape | undefined } catch (error) { storageError = error } + const pendingLogStore = new HostLogStore({ filePath: resolveHostLogPath({ profile: resolveLogProfileName(runtimeContext) }) }) disposePendingControl = runtimeContext.effect(() => { const control = new PluginControlRuntime( resolveConfig(readConfig()), @@ -151,11 +155,16 @@ export function apply(ctx: Context, entry: EntryConfig | ConfigShape | undefined undefined, undefined, true, + pendingLogStore, ) if (storageError !== undefined) { control.setStartupFailure(storageError, { storageAvailable: false }) } - return control.register(connection, webServer) + const dispose = control.register(connection, webServer) + return async () => { + await dispose() + await pendingLogStore.flush() + } }, 'ds-harness-remote pending control') } const startActiveRuntime = async (activeContext: Context): Promise => { @@ -201,6 +210,23 @@ async function activate( // Reads resolve through the binding so the ≤0.1.6 scope's user layer (not the // raw composition seed) is honored; the rc.1 binding reads the live Config. const config: ResolvedConfig = resolveConfig(settingsBinding?.get() ?? readConfig()) + const logStore = new HostLogStore({ filePath: resolveHostLogPath({ profile: resolveLogProfileName(ctx) }) }) + // Mirrors SafeLogger output to the process stdout/stderr as well as the DSH + // logger: the web process stdout is captured by the Desktop shell into + // desktop.log, so remote RPC/transport diagnostics stay greppable there. + const readSafeLogLevel = (): LogLevel => { + try { + const raw = (settingsBinding?.get() ?? readConfig()).logLevel + if (raw === 'debug' || raw === 'info' || raw === 'warn' || raw === 'error') return raw + } catch { /* Keep logging with initial level if live config cannot be read. */ } + return config.logLevel + } + const logger = new SafeLogger({ + debug: message => { logStore.record('debug', message); ctx.logger.debug(message); console.debug(message) }, + info: message => { logStore.record('info', message); ctx.logger.info(message); console.info(message) }, + warn: message => { logStore.record('warn', message); ctx.logger.warn(message); console.warn(message) }, + error: message => { logStore.record('error', message); ctx.logger.error(message); console.error(message) }, + }, readSafeLogLevel) let defaultIdentityDirectory = new IdentityStore().directory let initialStorageError: unknown try { @@ -219,26 +245,39 @@ async function activate( undefined, undefined, true, + logStore, ) if (initialStorageError !== undefined) { + logger.error('Remote plugin startup failed', safeStartupError(initialStorageError)) controlRuntime.setStartupFailure(initialStorageError, { storageAvailable: false }) } - ctx.effect(() => controlRuntime.register(connection, webServer), 'ds-harness-remote disabled control') + ctx.effect(() => { + const dispose = controlRuntime.register(connection, webServer) + return async () => { + await dispose() + await logStore.flush() + } + }, 'ds-harness-remote disabled control') } return } - // Mirrors SafeLogger output to the process stdout/stderr as well as the DSH - // logger: the web process stdout is captured by the Desktop shell into - // desktop.log, so remote RPC/transport diagnostics stay greppable there. - const logger = new SafeLogger({ - debug: message => { ctx.logger.debug(message); console.debug(message) }, - info: message => { ctx.logger.info(message); console.info(message) }, - warn: message => { ctx.logger.warn(message); console.warn(message) }, - error: message => { ctx.logger.error(message); console.error(message) }, - }, () => resolveConfig(settingsBinding?.get() ?? readConfig()).logLevel) // Startup and daily npm dist-tags lookup backing the client update notice. // Lives for the whole activation so Server switches keep its snapshot. const updateChecker = new PluginUpdateChecker(logger) + // Harness 0.2.2 updates a registry-installed third-party bundle in place. + // The manager is a Host boot service, so it keeps running after this plugin + // is removed and replaced. GitHub and path installs are left on the releases link. + const pluginUpgrade = new LocalPluginUpgrade( + updateChecker, + () => readPluginManager(ctx), + listener => { + // The manager emits on its own context. Desktop puts that service on the + // profile root; a nested context with its own bus still needs to hear it. + const buses = ctx.events === ctx.root.events ? [ctx.root.events] : [ctx.root.events, ctx.events] + const dispose = buses.map(events => events.on('plugin-manager/update-state', listener, { global: true })) + return () => { for (const stop of dispose) stop() } + }, + ) // The official Typert gateway (`typertGateway` from dsh-api-gateway) is the // dispatch path behind `/api/commands/*` on the host. It is an explicit // activation dependency so a peer bridge never silently omits commands. @@ -411,8 +450,9 @@ async function activate( clientRuntime, runtime, replaceServer, - updateChecker, + pluginUpgrade, true, + logStore, ) if (initialStorageError !== undefined) { controlRuntime?.setStartupFailure(initialStorageError, { storageAvailable: false }) @@ -463,6 +503,7 @@ async function activate( await disposeControl?.() await clientRuntime?.close() await runtime?.close() + await logStore.flush() } }, 'ds-harness-remote lifecycle') if (config.remoteTools?.enabled) { @@ -483,6 +524,19 @@ async function activate( * {@link EntryConfig} (a live `Volatile` reference); a direct `ctx.plugin()` * caller passes the plain composition object instead. */ +function resolveLogProfileName(ctx: Context): string | undefined { + const envProfile = process.env.DSH_PROFILE?.trim() + if (envProfile) return envProfile + const baseDir = (ctx.root as { baseDir?: unknown } | undefined)?.baseDir + if (typeof baseDir === 'string' && baseDir.trim().length > 0) { + const trimmed = baseDir.trim() + if (basename(dirname(trimmed)) === 'profiles') { + const segment = basename(trimmed) + if (segment.length > 0) return segment + } + } + return undefined +} function readEntryConfig(entry: EntryConfig | ConfigShape | undefined): ConfigInput { if (entry === undefined) return {} return isEntryConfig(entry) ? entry.get() : entry @@ -493,6 +547,21 @@ function isEntryConfig(value: EntryConfig | ConfigShape): value is EntryConfig { return typeof (value as { get?: unknown }).get === 'function' } +/** + * The official plugin manager lives on the profile context. A plugin-scoped + * lookup misses it when the service is isolated to the root. + */ +function readPluginManager(ctx: Context): PluginManagerUpdate | undefined { + const candidate = ctx.get('pluginManager', false) ?? ctx.root.get('pluginManager', false) + if ( + typeof candidate !== 'object' + || candidate === null + || typeof (candidate as { listBundles?: unknown }).listBundles !== 'function' + || typeof (candidate as { updateBundle?: unknown }).updateBundle !== 'function' + ) return undefined + return candidate as PluginManagerUpdate +} + /** Entry id whose live Config backs this plugin instance's settings writes. */ function locateEntryId(ctx: Context): string { const loader = ctx.get('loader') as LoaderLike | undefined