@deepseek-harness-tui/dsh-tui 0.20.1 → 0.20.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/README.md +14 -3
  2. package/guide/dsh-tui-guide/configuration.en.md +26 -12
  3. package/guide/dsh-tui-guide/configuration.md +18 -9
  4. package/guide/dsh-tui-guide/getting-started.en.md +17 -9
  5. package/guide/dsh-tui-guide/getting-started.md +11 -7
  6. package/guide/dsh-tui-guide/interaction.en.md +1 -0
  7. package/guide/dsh-tui-guide/interaction.md +1 -0
  8. package/guide/dsh-tui-guide/themes.en.md +1 -1
  9. package/guide/dsh-tui-guide/themes.md +1 -1
  10. package/guide/dsh-tui-guide/user-guide.en.md +8 -2
  11. package/guide/dsh-tui-guide/user-guide.md +8 -2
  12. package/lib/settings.json +19 -3
  13. package/lib/types/adapter/ports/channel-settings.d.ts +6 -0
  14. package/lib/types/adapter/ports/channel-settings.d.ts.map +1 -1
  15. package/lib/types/adapter/ports/channel-view.d.ts +28 -2
  16. package/lib/types/adapter/ports/channel-view.d.ts.map +1 -1
  17. package/lib/types/backends/claude/session/lifecycle.d.ts.map +1 -1
  18. package/lib/types/backends/claude/session/lifecycle.js +11 -9
  19. package/lib/types/backends/claude/side-query.d.ts +2 -0
  20. package/lib/types/backends/claude/side-query.d.ts.map +1 -1
  21. package/lib/types/backends/claude/side-query.js +30 -13
  22. package/lib/types/components/messages/AssistantToolUseMessage.d.ts +1 -1
  23. package/lib/types/components/messages/AssistantToolUseMessage.d.ts.map +1 -1
  24. package/lib/types/components/messages/AssistantToolUseMessage.js +228 -27
  25. package/lib/types/components/messages/presentCardBackground.d.ts +28 -0
  26. package/lib/types/components/messages/presentCardBackground.d.ts.map +1 -0
  27. package/lib/types/components/messages/presentCardBackground.js +23 -0
  28. package/lib/types/dsh-adapter/backend/translate.d.ts.map +1 -1
  29. package/lib/types/dsh-adapter/backend/translate.js +28 -1
  30. package/lib/types/dsh-adapter/channel/core/compose.d.ts.map +1 -1
  31. package/lib/types/dsh-adapter/channel/core/files.d.ts.map +1 -1
  32. package/lib/types/dsh-adapter/channel/file-actions.d.ts +1 -1
  33. package/lib/types/dsh-adapter/channel/file-actions.d.ts.map +1 -1
  34. package/lib/types/dsh-adapter/channel/file-actions.js +11 -4
  35. package/lib/types/dsh-adapter/channel/settings-host.d.ts.map +1 -1
  36. package/lib/types/dsh-adapter/channel/settings-host.js +78 -12
  37. package/lib/types/dsh-adapter/channel/state.d.ts +4 -3
  38. package/lib/types/dsh-adapter/channel/state.d.ts.map +1 -1
  39. package/lib/types/dsh-adapter/channel/state.js +4 -3
  40. package/lib/types/dsh-adapter/channel/types.d.ts +1 -1
  41. package/lib/types/dsh-adapter/channel/types.d.ts.map +1 -1
  42. package/lib/types/dsh-adapter/channel.d.ts +1 -1
  43. package/lib/types/dsh-adapter/channel.d.ts.map +1 -1
  44. package/lib/types/dsh-adapter/contract.d.ts +18 -7
  45. package/lib/types/dsh-adapter/contract.d.ts.map +1 -1
  46. package/lib/types/dsh-adapter/contract.js +20 -7
  47. package/lib/types/dsh-adapter/host-contract.d.ts +1 -1
  48. package/lib/types/dsh-adapter/host-contract.js +1 -1
  49. package/lib/types/dsh-adapter/host-entry.js +4 -2
  50. package/lib/types/dsh-adapter/host-service-diagnostics.d.ts +61 -0
  51. package/lib/types/dsh-adapter/host-service-diagnostics.d.ts.map +1 -0
  52. package/lib/types/dsh-adapter/host-service-diagnostics.js +103 -0
  53. package/lib/types/dsh-adapter/index.d.ts +5 -0
  54. package/lib/types/dsh-adapter/index.d.ts.map +1 -1
  55. package/lib/types/dsh-adapter/index.js +1 -0
  56. package/lib/types/dsh-adapter/plugin.d.ts.map +1 -1
  57. package/lib/types/dsh-adapter/plugin.js +39 -2
  58. package/lib/types/dsh-adapter/present-tool.d.ts +20 -0
  59. package/lib/types/dsh-adapter/present-tool.d.ts.map +1 -0
  60. package/lib/types/dsh-adapter/present-tool.js +96 -0
  61. package/lib/types/i18n.d.ts +15 -0
  62. package/lib/types/i18n.d.ts.map +1 -1
  63. package/lib/types/i18n.js +6 -0
  64. package/lib/types/ink/components/ScrollBox.d.ts.map +1 -1
  65. package/lib/types/ink/components/ScrollBox.js +13 -0
  66. package/lib/types/ink/dom.d.ts +3 -0
  67. package/lib/types/ink/dom.d.ts.map +1 -1
  68. package/lib/types/ink/ink.d.ts.map +1 -1
  69. package/lib/types/ink/ink.js +30 -34
  70. package/lib/types/ink/render-node-to-output.d.ts +3 -0
  71. package/lib/types/ink/render-node-to-output.d.ts.map +1 -1
  72. package/lib/types/ink/render-node-to-output.js +54 -40
  73. package/lib/types/ink/selection.d.ts +38 -28
  74. package/lib/types/ink/selection.d.ts.map +1 -1
  75. package/lib/types/ink/selection.js +201 -130
  76. package/lib/types/kernelPrefs.d.ts +5 -2
  77. package/lib/types/kernelPrefs.d.ts.map +1 -1
  78. package/lib/types/kernelPrefs.js +5 -2
  79. package/lib/types/settings/definitions.d.ts +12 -0
  80. package/lib/types/settings/definitions.d.ts.map +1 -1
  81. package/lib/types/settings/definitions.js +10 -2
  82. package/lib/types/theme.d.ts +4 -0
  83. package/lib/types/theme.d.ts.map +1 -1
  84. package/lib/types/theme.js +20 -2
  85. package/lib/types/tuiDisplayPrefs.d.ts +3 -0
  86. package/lib/types/tuiDisplayPrefs.d.ts.map +1 -1
  87. package/lib/types/tuiDisplayPrefs.js +10 -0
  88. package/lib/types/update.d.ts +74 -5
  89. package/lib/types/update.d.ts.map +1 -1
  90. package/lib/types/update.js +297 -46
  91. package/lib/types/updateHost.d.ts.map +1 -1
  92. package/lib/types/updateHost.js +16 -5
  93. package/lib/types/usageStats.d.ts +6 -0
  94. package/lib/types/usageStats.d.ts.map +1 -1
  95. package/lib/types/usageStats.js +8 -0
  96. package/lib/types/utils/colorMix.d.ts +19 -0
  97. package/lib/types/utils/colorMix.d.ts.map +1 -0
  98. package/lib/types/utils/colorMix.js +51 -0
  99. package/lib/types/utils/commandLine.d.ts +8 -0
  100. package/lib/types/utils/commandLine.d.ts.map +1 -0
  101. package/lib/types/utils/commandLine.js +37 -0
  102. package/lib/types/utils/desktopEntry.d.ts +32 -0
  103. package/lib/types/utils/desktopEntry.d.ts.map +1 -0
  104. package/lib/types/utils/desktopEntry.js +302 -0
  105. package/lib/types/utils/externalEditor.d.ts +2 -5
  106. package/lib/types/utils/externalEditor.d.ts.map +1 -1
  107. package/lib/types/utils/externalEditor.js +4 -36
  108. package/lib/types/utils/openExternal.d.ts +9 -0
  109. package/lib/types/utils/openExternal.d.ts.map +1 -1
  110. package/lib/types/utils/openExternal.js +87 -3
  111. package/lib/types/utils/terminalLaunch.d.ts +16 -0
  112. package/lib/types/utils/terminalLaunch.d.ts.map +1 -0
  113. package/lib/types/utils/terminalLaunch.js +121 -0
  114. package/package.json +112 -111
  115. package/tui-profile/feed/README.md +1 -1
  116. package/tui-profile/feed/first-party.json +1 -1
@@ -1,6 +1,6 @@
1
1
  import { execFileSync, spawn } from 'node:child_process';
2
2
  import { createHash, randomUUID } from 'node:crypto';
3
- import { appendFileSync, chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, realpathSync, renameSync, rmSync, statSync, symlinkSync, writeFileSync } from 'node:fs';
3
+ import { accessSync, appendFileSync, chmodSync, constants, copyFileSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, realpathSync, renameSync, rmSync, statSync, symlinkSync, unlinkSync, writeFileSync } from 'node:fs';
4
4
  import { homedir } from 'node:os';
5
5
  import { basename, delimiter, dirname, join, resolve, sep } from 'node:path';
6
6
  import { fileURLToPath } from 'node:url';
@@ -13,7 +13,8 @@ import validRange from 'semver/ranges/valid.js';
13
13
  import { shellQuote } from './utils/shellQuote.js';
14
14
  import { DATA_DIR } from './utils/paths.js';
15
15
  import { stripResumeArgs } from './sessionHistory.js';
16
- import { HOST_ENTRY_PATH_ENV, KERNEL_SWITCH_HANDOFF_ENV, RESUME_BACKEND_ENV, RESUME_RETRY_ENV, hostEntryDisabled, parseBackendId } from './kernelPrefs.js';
16
+ import { flushUsageStats } from './usageStats.js';
17
+ import { HOST_ENTRY_DELEGATED_ENV, HOST_ENTRY_PATH_ENV, KERNEL_SWITCH_HANDOFF_ENV, RESUME_BACKEND_ENV, RESUME_RETRY_ENV, hostEntryDisabled, parseBackendId } from './kernelPrefs.js';
17
18
  import { classifyReplacementOutcome, formatHandoffNotice, handoffEventTag, writeHandoffStage } from './handoffEvents.js';
18
19
  import { HANDOFF_ACK_FD_ENV, HANDOFF_ATTEMPT_ENV, HANDOFF_SCREEN_ENV, parseHandoffAckLine } from './handoffAck.js';
19
20
  import { DISABLE_KITTY_KEYBOARD, DISABLE_MODIFY_OTHER_KEYS, DISABLE_WIN32_INPUT_MODE } from './ink/termio/csi.js';
@@ -2323,6 +2324,35 @@ function sameHostInstall(original, candidate) {
2323
2324
  return false;
2324
2325
  }
2325
2326
  }
2327
+ /** Snapshot a direct pnpm boot before its logical group can be pruned. */
2328
+ function directPnpmInstall(packageDir, env, tuiDir) {
2329
+ if (profileHasLocalKernel(tuiDir))
2330
+ return undefined;
2331
+ const hostEntry = env[HOST_ENTRY_PATH_ENV];
2332
+ if (hostEntry !== undefined && hostEntry !== '' && !hostEntryDisabled(env)
2333
+ && env[HOST_ENTRY_DELEGATED_ENV] !== '1')
2334
+ return undefined;
2335
+ try {
2336
+ if (process.argv[1] === undefined || realpathSync(process.argv[1]) !== hostPackageEntry(packageDir))
2337
+ return undefined;
2338
+ const install = detectHostInstall(packageDir, env);
2339
+ return install.installer === 'pnpm' ? install : undefined;
2340
+ }
2341
+ catch {
2342
+ return undefined;
2343
+ }
2344
+ }
2345
+ /** Follow only the original pnpm home's shims, then re-prove singleton ownership. */
2346
+ async function ownedPnpmPackageDir(install, env) {
2347
+ if (install.installer !== 'pnpm' || install.pnpmHome === undefined)
2348
+ return undefined;
2349
+ const ownerEnv = { ...env, PATH: [join(install.pnpmHome, 'bin'), install.pnpmHome].join(delimiter) };
2350
+ const dir = await findHostDshPackageDir(ownerEnv);
2351
+ if (dir === undefined)
2352
+ return undefined;
2353
+ const candidate = detectHostInstall(dir, ownerEnv);
2354
+ return sameHostInstall(install, candidate) ? candidate.packageDir : undefined;
2355
+ }
2326
2356
  /** This package's installed directory (compiled package or source checkout). */
2327
2357
  function installedTuiPackageDir() {
2328
2358
  const here = dirname(fileURLToPath(import.meta.url));
@@ -2339,6 +2369,11 @@ function installedTuiPackageDir() {
2339
2369
  }
2340
2370
  return undefined;
2341
2371
  }
2372
+ /** Explicit profile dependencies are not the shared compatibility fallback. */
2373
+ function profileHasLocalKernel(tuiDir = installedTuiPackageDir()) {
2374
+ return tuiDir !== undefined
2375
+ && readPackageVersion(resolve(tuiDir, '..', '..', '@deepseek-ai', 'dsh')) !== undefined;
2376
+ }
2342
2377
  /**
2343
2378
  * The profile's `@deepseek-ai/dsh` directory, read straight from disk.
2344
2379
  *
@@ -2439,9 +2474,42 @@ async function readTargetHostRange(registryBase, targetVersion, installedTui, si
2439
2474
  return fetchPackagePeerRange(registryBase, PACKAGE_NAME, targetVersion, UPSTREAM_HOST_PEER_PACKAGE, signal);
2440
2475
  }
2441
2476
  /**
2442
- * The host package dir when module resolution cannot see it (an exotic
2443
- * profile layout). Loaded lazily: a normal profile resolves through
2444
- * `installedKernelPackageDir()` and never pays for the PATH walk.
2477
+ * The engine package this process booted from, read from `argv[1]`.
2478
+ *
2479
+ * A launcher starts `dsh` from PATH, but a direct `dsh --profile` boot may use
2480
+ * an absolute entry while an older or unrelated `dsh` sits earlier on PATH.
2481
+ * Updating that other install would leave the running engine untouched, so the
2482
+ * running package wins over both PATH and module resolution.
2483
+ */
2484
+ function runningHostPackageDir() {
2485
+ const entry = process.argv[1];
2486
+ if (entry === undefined || entry === '')
2487
+ return undefined;
2488
+ try {
2489
+ let dir = dirname(realpathSync(entry));
2490
+ for (;;) {
2491
+ try {
2492
+ const parsed = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
2493
+ if (isRecord(parsed) && parsed.name === UPSTREAM_HOST_PACKAGE)
2494
+ return dir;
2495
+ }
2496
+ catch {
2497
+ // Keep walking: the entry may sit in a nested bin directory.
2498
+ }
2499
+ const parent = dirname(dir);
2500
+ if (parent === dir)
2501
+ return undefined;
2502
+ dir = parent;
2503
+ }
2504
+ }
2505
+ catch {
2506
+ return undefined;
2507
+ }
2508
+ }
2509
+ /**
2510
+ * The host package dir of the first `dsh` on PATH: the engine a default launch
2511
+ * runs. Loaded lazily so a caller that already holds the directory (the
2512
+ * launcher) never pays for the walk.
2445
2513
  */
2446
2514
  async function findHostDshPackageDir(env) {
2447
2515
  try {
@@ -2453,6 +2521,31 @@ async function findHostDshPackageDir(env) {
2453
2521
  return undefined;
2454
2522
  }
2455
2523
  }
2524
+ /**
2525
+ * Pick the engine package the update must advance, in the documented order:
2526
+ * an explicit pre-resolved directory, the package this process booted from,
2527
+ * the first `dsh` on PATH, a caller-supplied module-resolution seam (tests),
2528
+ * then module resolution.
2529
+ *
2530
+ * Each source is a thunk so a caller that already holds a directory never pays
2531
+ * for the PATH walk or the module resolution it would not consult, and so the
2532
+ * order itself is exercisable without a real profile.
2533
+ *
2534
+ * @param input - Candidate sources; omitted sources are skipped.
2535
+ * @returns The first source that names a package directory.
2536
+ */
2537
+ export async function resolveHostPackageDir(input = {}) {
2538
+ if (input.explicit !== undefined)
2539
+ return input.explicit;
2540
+ if (input.running !== undefined)
2541
+ return input.running;
2542
+ const path = input.path === undefined ? undefined : await input.path();
2543
+ if (path !== undefined)
2544
+ return path;
2545
+ if (input.module !== undefined)
2546
+ return input.module;
2547
+ return input.installed?.();
2548
+ }
2456
2549
  /** Predict the launcher step with the same resolver the alignment uses. */
2457
2550
  function planLauncherStep(input) {
2458
2551
  const env = input.env;
@@ -2512,7 +2605,8 @@ function planLauncherStep(input) {
2512
2605
  * than the network allows and never fabricates a version.
2513
2606
  *
2514
2607
  * @param input - Cancellation, a pre-resolved host package dir (the entry has
2515
- * one), an override for the installed engine version (tests), and the env.
2608
+ * one), an override for the installed engine version (tests), a test seam
2609
+ * for the module-resolution host, and the env.
2516
2610
  */
2517
2611
  export async function resolveUpdatePlan(input = {}) {
2518
2612
  const env = input.env ?? process.env;
@@ -2532,6 +2626,20 @@ export async function resolveUpdatePlan(input = {}) {
2532
2626
  blocked: false,
2533
2627
  };
2534
2628
  }
2629
+ // The running package is the engine this update must advance. PATH is next:
2630
+ // `dsh --profile` boots its first `dsh`, while `installedKernelPackageDir()`
2631
+ // resolves through module fallback instead — on Windows that can be the
2632
+ // shared `profiles/node_modules` junction, left pointing at an old npx cache
2633
+ // after a global upgrade (issue #2028). Module resolution stays as the last
2634
+ // fallback for a process whose argv does not name a dsh package (a source
2635
+ // driver or an exotic profile layout).
2636
+ const hostPackageDir = await resolveHostPackageDir({
2637
+ explicit: input.hostPackageDir,
2638
+ running: runningHostPackageDir(),
2639
+ path: () => findHostDshPackageDir(env),
2640
+ module: input.moduleHostPackageDir,
2641
+ installed: () => installedKernelPackageDir(),
2642
+ });
2535
2643
  // Without a target TUI version there is no range to plan an engine against;
2536
2644
  // keep the existing `--latest` fallback instead of guessing a line.
2537
2645
  if (tui.kind === 'unknown') {
@@ -2542,12 +2650,12 @@ export async function resolveUpdatePlan(input = {}) {
2542
2650
  compatible: undefined,
2543
2651
  hostRange: undefined,
2544
2652
  currentHost: input.installedHost,
2653
+ hostPackageDir,
2545
2654
  blocked: false,
2546
2655
  };
2547
2656
  }
2548
2657
  const targetVersion = tui.kind === 'update' ? tui.latest : tui.current;
2549
2658
  const registryBase = resolveRegistryBase();
2550
- const hostPackageDir = input.hostPackageDir ?? installedKernelPackageDir() ?? await findHostDshPackageDir(env);
2551
2659
  const install = hostPackageDir === undefined ? undefined : detectHostInstall(hostPackageDir, env);
2552
2660
  const currentHost = input.installedHost ?? install?.version;
2553
2661
  const [hostRange, available] = await Promise.all([
@@ -2579,6 +2687,7 @@ export async function resolveUpdatePlan(input = {}) {
2579
2687
  compatible,
2580
2688
  hostRange,
2581
2689
  currentHost,
2690
+ hostPackageDir,
2582
2691
  blocked: tui.kind === 'update' && currentSupported === false,
2583
2692
  };
2584
2693
  }
@@ -2987,7 +3096,7 @@ function hostFallbackLink(step, env, tuiDir) {
2987
3096
  || install.pnpmHome !== step.command.env?.PNPM_HOME)
2988
3097
  return undefined;
2989
3098
  const nodeModules = resolve(tuiDir, '..', '..');
2990
- if (readPackageVersion(join(nodeModules, '@deepseek-ai', 'dsh')) !== undefined)
3099
+ if (profileHasLocalKernel(tuiDir))
2991
3100
  return undefined;
2992
3101
  const path = resolve(nodeModules, '..', '..', 'node_modules', '@deepseek-ai', 'dsh');
2993
3102
  try {
@@ -2999,19 +3108,77 @@ function hostFallbackLink(step, env, tuiDir) {
2999
3108
  return undefined;
3000
3109
  }
3001
3110
  }
3111
+ /**
3112
+ * Re-point a Windows directory junction without renaming a directory over an
3113
+ * existing one (Windows cannot replace a non-empty directory atomically).
3114
+ * Stage the new junction first, move the old one aside, then rename the staged
3115
+ * link into place. The old link is restored on any failure, and a failed
3116
+ * restore leaves it aside for inspection instead of deleting the only
3117
+ * fallback.
3118
+ *
3119
+ * Exported for the regression: POSIX ignores the `junction` type, so the same
3120
+ * control flow is exercised on every platform.
3121
+ */
3122
+ export function replaceWindowsJunction(link, target) {
3123
+ const staged = `${link.path}.dsh-tui-${randomUUID()}`;
3124
+ const previous = `${link.path}.dsh-tui-old-${randomUUID()}`;
3125
+ let stagedReady = false;
3126
+ let moved = false;
3127
+ let restored = true;
3128
+ try {
3129
+ symlinkSync(target, staged, 'junction');
3130
+ stagedReady = true;
3131
+ // The installer or another process may have already replaced the link.
3132
+ // Never remove it first: a failed refresh must leave the old fallback intact.
3133
+ if (!lstatSync(link.path).isSymbolicLink() || readlinkSync(link.path) !== link.target)
3134
+ return;
3135
+ renameSync(link.path, previous);
3136
+ moved = true;
3137
+ renameSync(staged, link.path);
3138
+ stagedReady = false;
3139
+ moved = false;
3140
+ }
3141
+ catch {
3142
+ if (moved) {
3143
+ try {
3144
+ renameSync(previous, link.path);
3145
+ moved = false;
3146
+ }
3147
+ catch {
3148
+ restored = false;
3149
+ }
3150
+ }
3151
+ }
3152
+ finally {
3153
+ if (stagedReady) {
3154
+ // Unlink, never a recursive remove: the staged path is a reparse point,
3155
+ // and Windows may resolve it as a directory and descend into the target.
3156
+ try {
3157
+ unlinkSync(staged);
3158
+ }
3159
+ catch { /* Verification still decides the restart. */ }
3160
+ }
3161
+ if (restored) {
3162
+ try {
3163
+ unlinkSync(previous);
3164
+ }
3165
+ catch { /* A stale backup junction is harmless. */ }
3166
+ }
3167
+ }
3168
+ }
3002
3169
  /** Refresh only the unchanged runtime link, to a runnable target in the SAME pnpm home. */
3003
- async function refreshHostFallback(link, version, env, signal) {
3004
- // Windows cannot atomically rename a junction over an existing directory.
3005
- // Keep it unverified rather than unlinking the only surviving fallback first.
3006
- if (process.platform === 'win32')
3007
- return;
3008
- const dir = await findHostDshPackageDir(env);
3170
+ async function refreshHostFallback(link, version, env, signal, packageDir) {
3171
+ const dir = packageDir ?? await findHostDshPackageDir(env);
3009
3172
  if (dir === undefined || signal?.aborted)
3010
3173
  return;
3011
3174
  const install = detectHostInstall(dir, env);
3012
3175
  if (install.installer !== 'pnpm' || install.pnpmHome !== link.pnpmHome
3013
3176
  || install.version !== version || !hostPackageRuns(install.packageDir, env))
3014
3177
  return;
3178
+ if (process.platform === 'win32') {
3179
+ replaceWindowsJunction(link, install.packageDir);
3180
+ return;
3181
+ }
3015
3182
  const staged = `${link.path}.dsh-tui-${randomUUID()}`;
3016
3183
  let created = false;
3017
3184
  try {
@@ -3053,6 +3220,7 @@ async function refreshHostFallback(link, version, env, signal) {
3053
3220
  export async function installHostStep(step, options) {
3054
3221
  const manual = step.manual[0] ?? '';
3055
3222
  const command = step.command;
3223
+ const direct = directPnpmInstall(step.packageDir, options.env, options.tuiDir);
3056
3224
  const fallback = hostFallbackLink(step, options.env, options.tuiDir ?? installedTuiPackageDir());
3057
3225
  let cancelled = false;
3058
3226
  let cleanupUnconfirmed = false;
@@ -3072,26 +3240,28 @@ export async function installHostStep(step, options) {
3072
3240
  const incomplete = { from: step.from, to: step.to, code, verified: false, cancelled, manual };
3073
3241
  if (cancelled || options.signal?.aborted || cleanupUnconfirmed)
3074
3242
  return incomplete;
3243
+ const directDir = direct === undefined ? undefined : await ownedPnpmPackageDir(direct, options.env);
3075
3244
  if (code !== 0) {
3076
3245
  // Non-zero does not roll back a changed pnpm group. Keep fresh observations
3077
3246
  // for the launcher-failure gate, never a `verified` success or restart stamp.
3078
3247
  const cli = probePathDsh(options.env);
3079
3248
  const nextDir = options.nextLaunchDir === undefined ? installedKernelDirFresh(options.tuiDir) : options.nextLaunchDir();
3080
- const installed = readPackageVersion(nextDir ?? step.packageDir);
3249
+ const installed = readPackageVersion(directDir ?? nextDir ?? step.packageDir);
3081
3250
  return {
3082
3251
  ...incomplete,
3083
3252
  ...(installed === undefined ? {} : { installed }),
3084
3253
  ...(cli.kind === 'version' ? { cliVersion: cli.version } : {}),
3085
3254
  };
3086
3255
  }
3087
- const manifestVersion = readPackageVersion(step.packageDir);
3256
+ const manifestVersion = readPackageVersion(directDir ?? step.packageDir);
3088
3257
  const cli = probePathDsh(options.env);
3089
- // pnpm may retain the old store tree behind the shared runtime link. Repair
3090
- // that link only after the actual PATH install proves the intended version;
3091
- // the normal two-entry verification still runs afterwards (#1849 review R4).
3258
+ // A direct pnpm boot follows its owning home, not an unrelated PATH engine.
3259
+ // Default launches retain the profile/PATH cross-check below.
3260
+ const directVerified = directDir !== undefined
3261
+ && readPackageVersion(directDir) === step.to && hostPackageRuns(directDir, options.env, step.to);
3092
3262
  if (fallback !== undefined && !cancelled && !options.signal?.aborted
3093
- && cli.kind === 'version' && cli.version === step.to) {
3094
- await refreshHostFallback(fallback, step.to, options.env, options.signal);
3263
+ && (direct === undefined ? cli.kind === 'version' && cli.version === step.to : directVerified)) {
3264
+ await refreshHostFallback(fallback, step.to, options.env, options.signal, directDir);
3095
3265
  }
3096
3266
  const nextDir = options.nextLaunchDir === undefined ? installedKernelDirFresh(options.tuiDir) : options.nextLaunchDir();
3097
3267
  const resolved = options.resolveInstalled?.()
@@ -3101,16 +3271,32 @@ export async function installHostStep(step, options) {
3101
3271
  // an installer that exits 0 can still leave a half-written tree behind.
3102
3272
  const nextRuns = nextDir === undefined ? undefined : hostPackageRuns(nextDir, options.env);
3103
3273
  const cliVersion = cli.kind === 'version' ? cli.version : undefined;
3274
+ // A shared runtime fallback may point at ANOTHER install (an old npx cache)
3275
+ // rather than at the engine this step replaces (issue #2028): the plan
3276
+ // advances the running/PATH install, but the profile link still resolves the
3277
+ // fossil, so requiring it to match would report a successful install as
3278
+ // unverified and make `dsh-tui update` exit non-zero. npm updates its global
3279
+ // package in place, so its own manifest plus the PATH probe already prove the
3280
+ // next launch runs `step.to`; a broken next entry no longer vetoes a step it
3281
+ // is not the entry for. Default pnpm launches keep two-entry verification;
3282
+ // a direct pnpm boot instead proves its owned replacement above.
3283
+ const installedInPlace = step.command.file === 'npm' && !profileHasLocalKernel(options.tuiDir)
3284
+ && manifestVersion === step.to
3285
+ && cliVersion === step.to;
3104
3286
  const gate = resolved ?? cliVersion;
3105
- const verified = nextRuns === false || cli.kind === 'failed'
3106
- ? false
3107
- : nextRuns === true
3108
- ? resolved === step.to && (cliVersion === undefined || cliVersion === step.to)
3109
- : gate !== undefined
3110
- ? gate === step.to
3111
- && (resolved === undefined || resolved === step.to)
3112
- && (cliVersion === undefined || cliVersion === step.to)
3113
- : manifestVersion === step.to;
3287
+ const verified = direct !== undefined
3288
+ ? directVerified
3289
+ : installedInPlace
3290
+ ? true
3291
+ : nextRuns === false || cli.kind === 'failed'
3292
+ ? false
3293
+ : nextRuns === true
3294
+ ? resolved === step.to && (cliVersion === undefined || cliVersion === step.to)
3295
+ : gate !== undefined
3296
+ ? gate === step.to
3297
+ && (resolved === undefined || resolved === step.to)
3298
+ && (cliVersion === undefined || cliVersion === step.to)
3299
+ : manifestVersion === step.to;
3114
3300
  return {
3115
3301
  from: step.from,
3116
3302
  to: step.to,
@@ -3183,8 +3369,17 @@ export function engineFailureAllowsRestart(input) {
3183
3369
  * @param updatedFrom - Version captured before the installer ran (release-age
3184
3370
  * exclusion and the #307 stamp).
3185
3371
  */
3186
- async function installProfileTui(profile, targetVersion, signal, updatedFrom) {
3187
- const dsh = process.platform === 'win32' ? 'dsh.cmd' : 'dsh';
3372
+ async function installProfileTui(profile, targetVersion, signal, updatedFrom, hostPackageDir) {
3373
+ // Run the installer with the SAME host the plan advances. The `dsh` on PATH
3374
+ // can be a different install (a direct absolute boot, issue #2029 review):
3375
+ // its post-install validation uses its own runtime version and would reject
3376
+ // a TUI the planned host supports. The package's own `bin.dsh` entry is what
3377
+ // the shim launches, without the PATH lookup; keep the PATH route only when
3378
+ // the plan could not name a host.
3379
+ const hostEntry = hostPackageDir === undefined ? undefined : hostPackageEntry(hostPackageDir);
3380
+ const dshFile = hostEntry === undefined ? (process.platform === 'win32' ? 'dsh.cmd' : 'dsh') : process.execPath;
3381
+ const dshShell = hostEntry === undefined;
3382
+ const dshArgs = (args) => hostEntry === undefined ? [...args] : [hostEntry, ...args];
3188
3383
  const updateArgs = tuiUpdatePluginArgs(profile, targetVersion);
3189
3384
  // pnpm ≥11 hard-fails installs whose dependency tree carries un-allowlisted
3190
3385
  // build scripts (ERR_PNPM_IGNORED_BUILDS). The host's pi-ai adapter pulls in
@@ -3226,8 +3421,8 @@ async function installProfileTui(profile, targetVersion, signal, updatedFrom) {
3226
3421
  const capture = (chunk) => { updateStderr += chunk; };
3227
3422
  let cleanupUnconfirmed = false;
3228
3423
  const onCleanupError = () => { cleanupUnconfirmed = true; };
3229
- const installerOptions = { shell: true, onStderr: capture, signal, onCleanupError };
3230
- let updateCode = await runProcess(dsh, updateArgs, installerOptions);
3424
+ const installerOptions = { shell: dshShell, onStderr: capture, signal, onCleanupError };
3425
+ let updateCode = await runProcess(dshFile, dshArgs(updateArgs), installerOptions);
3231
3426
  // Exit codes alone cannot prove ownership cleanup: a supervisor failure can
3232
3427
  // use the same code/stderr as a retryable installer failure. Never overlap
3233
3428
  // a surviving installer with a retry, stale-file removal, repair or restart.
@@ -3241,7 +3436,7 @@ async function installProfileTui(profile, targetVersion, signal, updatedFrom) {
3241
3436
  if (![0, 124, 129, 130, 143].includes(updateCode) && isTransientUpdateFailure(updateStderr)) {
3242
3437
  process.stderr.write('dsh-tui: transient pnpm failure (Windows tmp-rename race) — retrying once…\n');
3243
3438
  updateStderr = '';
3244
- updateCode = await runProcess(dsh, updateArgs, installerOptions);
3439
+ updateCode = await runProcess(dshFile, dshArgs(updateArgs), installerOptions);
3245
3440
  if (cleanupUnconfirmed)
3246
3441
  return { code: updateCode || 1 };
3247
3442
  if (signal?.aborted)
@@ -3260,7 +3455,7 @@ async function installProfileTui(profile, targetVersion, signal, updatedFrom) {
3260
3455
  process.stderr.write(`dsh-tui: pnpm tmp-rename race (${flavor}) — cleared the stale install ` +
3261
3456
  `(package dir: ${removal.packageDir}${stagingNote}) and rerunning the update…\n`);
3262
3457
  updateStderr = '';
3263
- updateCode = await runProcess(dsh, updateArgs, installerOptions);
3458
+ updateCode = await runProcess(dshFile, dshArgs(updateArgs), installerOptions);
3264
3459
  if (cleanupUnconfirmed)
3265
3460
  return { code: updateCode || 1 };
3266
3461
  if (signal?.aborted)
@@ -3294,7 +3489,7 @@ async function installProfileTui(profile, targetVersion, signal, updatedFrom) {
3294
3489
  if (signal?.aborted)
3295
3490
  return { code: 130 };
3296
3491
  if (installed !== targetVersion) {
3297
- const repairCode = await runProcess(dsh, ['plugin', '--profile', profile, 'install'], { shell: true, signal, onCleanupError });
3492
+ const repairCode = await runProcess(dshFile, dshArgs(['plugin', '--profile', profile, 'install']), { shell: dshShell, signal, onCleanupError });
3298
3493
  if (cleanupUnconfirmed)
3299
3494
  return { code: repairCode || 1 };
3300
3495
  if (signal?.aborted)
@@ -3408,7 +3603,7 @@ export async function updateTui(profile, plan, signal, options = {}) {
3408
3603
  process.stderr.write(`${t('update-tui-already-current', { version: updatedFrom || 'unknown' })}\n`);
3409
3604
  }
3410
3605
  else {
3411
- const result = await installProfileTui(profile, targetVersion, signal, updatedFrom);
3606
+ const result = await installProfileTui(profile, targetVersion, signal, updatedFrom, plan.hostPackageDir);
3412
3607
  if (result.code !== 0) {
3413
3608
  return {
3414
3609
  code: result.code,
@@ -3439,6 +3634,7 @@ export async function updateTui(profile, plan, signal, options = {}) {
3439
3634
  return { code: 130, updatedFrom, installed, tuiChanged, compatible: plan.compatible };
3440
3635
  process.stderr.write(`${t('update-engine-starting', { from: plan.host.from ?? 'unknown', to: plan.host.to })}\n`);
3441
3636
  const hadPathDsh = commandOnPath('dsh', process.env);
3637
+ const direct = directPnpmInstall(plan.host.packageDir, process.env);
3442
3638
  host = await installHostStep(plan.host, {
3443
3639
  env: process.env,
3444
3640
  signal,
@@ -3472,7 +3668,11 @@ export async function updateTui(profile, plan, signal, options = {}) {
3472
3668
  // A failed installer may have removed/replaced the global package before
3473
3669
  // exiting (EPERM on a loaded native module, ENOSPC, a killed child). Never
3474
3670
  // restart into an engine that cannot be proven loadable.
3475
- if (!engineSurvivesFailure(plan.host, process.env, options.nextLaunchDir?.(), hadPathDsh)) {
3671
+ const survivingDirect = direct === undefined ? undefined : await ownedPnpmPackageDir(direct, process.env);
3672
+ const survives = direct === undefined
3673
+ ? engineSurvivesFailure(plan.host, process.env, options.nextLaunchDir?.(), hadPathDsh)
3674
+ : survivingDirect !== undefined && hostPackageRuns(survivingDirect, process.env);
3675
+ if (!survives) {
3476
3676
  // The plan §10 rule for refusal copy: current version, target range,
3477
3677
  // why it cannot be automatic, and the command — all four.
3478
3678
  process.stderr.write(`\n${t('update-engine-unsafe-restart', {
@@ -3622,11 +3822,11 @@ export async function updateTuiAndRestart(sessionId, profile, plan, kernel,
3622
3822
  /** The standalone entry's supervision hooks (TuiRestartOptions.onSpawn / onTerminationSignal). */
3623
3823
  supervision = {}, signal) {
3624
3824
  const engineSkip = announceEngineSkip(plan);
3625
- // Prove the direct DSH entry before pnpm can delete its logical group path.
3626
- // Source/custom launchers and the package-owned host entry keep their route.
3825
+ // Prove the direct DSH entry before pnpm can delete its logical group path,
3826
+ // including delegated launches that restartTui replays without the host entry.
3627
3827
  let directInstall;
3628
3828
  const hostEntry = process.env[HOST_ENTRY_PATH_ENV];
3629
- if (plan.host.kind === 'install' && (hostEntry === undefined || hostEntry === '' || hostEntryDisabled())) {
3829
+ if (plan.host.kind === 'install' && (hostEntry === undefined || hostEntry === '' || hostEntryDisabled() || process.env[HOST_ENTRY_DELEGATED_ENV] === '1')) {
3630
3830
  const entry = hostPackageEntry(plan.host.packageDir);
3631
3831
  try {
3632
3832
  if (entry !== undefined && process.argv[1] !== undefined && realpathSync(process.argv[1]) === entry) {
@@ -3645,7 +3845,10 @@ supervision = {}, signal) {
3645
3845
  // npm updates its original package in place; pnpm may rotate the group.
3646
3846
  // PATH can name an unrelated (even older) install, so it is a last resort
3647
3847
  // and must prove the same ownership as the profile/original candidates.
3648
- const candidates = new Set([plan.host.packageDir, installedKernelDirFresh(), await findHostDshPackageDir(process.env)]);
3848
+ const candidates = new Set([
3849
+ await ownedPnpmPackageDir(directInstall, process.env),
3850
+ plan.host.packageDir, installedKernelDirFresh(), await findHostDshPackageDir(process.env),
3851
+ ]);
3649
3852
  const allowed = parseHostLineRange(plan.hostRange);
3650
3853
  for (const dir of candidates) {
3651
3854
  if (dir === undefined)
@@ -3899,19 +4102,40 @@ export function restartArgv(input) {
3899
4102
  args[0] = input.dshEntry;
3900
4103
  return [...input.execArgv, ...args];
3901
4104
  }
4105
+ /**
4106
+ * Whether the current executable can be handed to a POSIX execve right now.
4107
+ * Node aborts the process when the native execve fails, so an unavailable
4108
+ * target must take the spawn fallback before the call: `accessSync` covers
4109
+ * ENOENT/EACCES and `statSync` rejects a directory (execve would EACCES too).
4110
+ */
4111
+ export function restartExecTargetRunnable(path = process.execPath) {
4112
+ try {
4113
+ if (!statSync(path).isFile())
4114
+ return false;
4115
+ accessSync(path, constants.X_OK);
4116
+ return true;
4117
+ }
4118
+ catch {
4119
+ return false;
4120
+ }
4121
+ }
3902
4122
  export async function restartTui(sessionId, options = {}) {
3903
4123
  const kind = options.kind ?? 'restart';
3904
4124
  const tag = options.backend !== undefined ? 'backend-switch' : kind === 'update' ? 'update-restart' : 'restart';
3905
4125
  const hostEntry = process.env[HOST_ENTRY_PATH_ENV];
4126
+ // Re-entering an entry that already delegated to DSH would add another
4127
+ // waiting entry on every restart. Keep that launch path until a kernel
4128
+ // switch explicitly needs the entry again.
4129
+ const delegated = options.backend === undefined && process.env[HOST_ENTRY_DELEGATED_ENV] === '1';
3906
4130
  const argv = restartArgv({
3907
4131
  execArgv: process.execArgv,
3908
4132
  argv: process.argv,
3909
4133
  switching: options.backend !== undefined,
3910
4134
  fresh: sessionId === '',
3911
- hostEntry: hostEntry === undefined || hostEntry === '' || hostEntryDisabled() ? undefined : hostEntry,
4135
+ hostEntry: delegated || hostEntry === undefined || hostEntry === '' || hostEntryDisabled() ? undefined : hostEntry,
3912
4136
  dshEntry: options.dshEntry,
3913
4137
  });
3914
- logRestartEvent(`${tag}: spawning replacement`, {
4138
+ logRestartEvent(`${tag}: preparing replacement`, {
3915
4139
  node: process.execPath,
3916
4140
  argv,
3917
4141
  cwd: process.cwd(),
@@ -3939,8 +4163,35 @@ export async function restartTui(sessionId, options = {}) {
3939
4163
  await writeHandoffStage(process.stdout, formatHandoffNotice('stage-start', { name: options.backendName ?? options.backend, color: process.stdout.isTTY === true }) + '\n');
3940
4164
  logRestartEvent(handoffEventTag('stage-start'), { backend: options.backend, ...(attemptId === undefined ? {} : { attemptId }) });
3941
4165
  }
4166
+ const childEnv = restartChildEnv(process.env, sessionId, kind, options);
4167
+ // Keep the same PID/foreground job instead of retaining another full Node
4168
+ // runtime on each /restart or /update. Kernel switches still need a parent
4169
+ // for their outcome notices and fullscreen ACK pipe. Other runtimes (and
4170
+ // Windows) may not expose execve; an unusable target, an unsupported
4171
+ // implementation or a validation error keeps the console-safe spawn fallback.
4172
+ if (options.backend === undefined && process.platform !== 'win32' && typeof process.execve === 'function') {
4173
+ // Node aborts on a native execve failure, so only a preflighted target may
4174
+ // be attempted; a path that disappears after the check can still abort.
4175
+ if (!restartExecTargetRunnable()) {
4176
+ logRestartEvent(`${tag}: execve target unavailable, falling back to spawn`, { path: process.execPath });
4177
+ }
4178
+ else {
4179
+ const env = Object.fromEntries(Object.entries(childEnv).filter((entry) => entry[1] !== undefined));
4180
+ detachHandoffStdin();
4181
+ // execve skips the JS exit hook; persist the usage tail before replacing.
4182
+ flushUsageStats();
4183
+ logRestartEvent(`${tag}: replacing current process`, { pid: process.pid });
4184
+ try {
4185
+ process.execve(process.execPath, [process.execPath, ...argv], env);
4186
+ }
4187
+ catch (error) {
4188
+ // Only argument validation and implementations that throw reach this
4189
+ // catch; the native call aborts in the Node versions we support.
4190
+ logRestartEvent(`${tag}: execve threw, falling back to spawn`, { message: error instanceof Error ? error.message : String(error) });
4191
+ }
4192
+ }
4193
+ }
3942
4194
  return new Promise(resolve => {
3943
- const childEnv = restartChildEnv(process.env, sessionId, kind, options);
3944
4195
  if (handoff) {
3945
4196
  // stdio[3] is the ACK pipe; the env tells the replacement its fd and the attempt id.
3946
4197
  childEnv[HANDOFF_SCREEN_ENV] = 'alt';
@@ -1 +1 @@
1
- {"version":3,"file":"updateHost.d.ts","sourceRoot":"","sources":["../../src/updateHost.ts"],"names":[],"mappings":"AAqBA,2EAA2E;AAC3E,eAAO,MAAM,UAAU,mBAAmB,CAAA;AAE1C,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAA;AAEhE,2EAA2E;AAC3E,MAAM,WAAW,WAAW;IAC1B,+DAA+D;IAC/D,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,mDAAmD;IACnD,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;IACpC,QAAQ,CAAC,SAAS,EAAE,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AE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS,CAqBtH;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,WAAW,GAAG,SAAS,EAChC,QAAQ,GAAE,MAAM,CAAC,QAA2B,GAC3C,MAAM,EAAE,CAgCV;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE;IACvC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;IACpC,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAA;IACnC,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAA;CACzB,GAAG,OAAO,GAAG,SAAS,CAStB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE;IACvC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;IACpC,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAA;CACpC,GAAG,OAAO,GAAG,SAAS,CAOtB;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE;IAClC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;IACpC,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;IAClC,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,CAAA;IACjD,QAAQ,CAAC,OAAO,EAAE,WAAW,GAAG,SAAS,CAAA;IACzC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAC5B,GAAG,QAAQ,CA4DX"}
@@ -13,7 +13,7 @@
13
13
  * fails the release when the enumeration leaves the validated line, so the
14
14
  * runtime never has to trust a range it cannot enumerate.
15
15
  */
16
- import { accessSync, constants, existsSync, readFileSync, readdirSync, realpathSync } from 'node:fs';
16
+ import { accessSync, constants, existsSync, readFileSync, readdirSync, realpathSync, statSync } from 'node:fs';
17
17
  import { basename, delimiter, dirname, join, resolve, sep } from 'node:path';
18
18
  import gt from 'semver/functions/gt.js';
19
19
  import prerelease from 'semver/functions/prerelease.js';
@@ -125,15 +125,26 @@ function npmPrefix(dir, platform) {
125
125
  return dir.slice(0, posixIndex);
126
126
  if (platform !== 'win32')
127
127
  return undefined;
128
- // npm's Windows shim tree is `<prefix>\node_modules`; accept it only when the
129
- // prefix is npm's own directory, so a project or a yarn/asdf tree stays
130
- // unclassified instead of being installed into by guesswork.
128
+ // npm's Windows shim tree is `<prefix>\node_modules`. A project can carry
129
+ // the same `dsh.cmd` wrapper, so a root manifest or npm lock makes the layout
130
+ // ambiguous: leave it manual-only rather than globally replacing local deps.
131
+ // The default prefix is named `npm`; custom prefixes also need a regular
132
+ // `dsh.cmd` beside node_modules (issue #2028).
131
133
  const windowsMarker = `${sep}node_modules${sep}`;
132
134
  const windowsIndex = dir.lastIndexOf(windowsMarker);
133
135
  if (windowsIndex <= 0)
134
136
  return undefined;
135
137
  const prefix = dir.slice(0, windowsIndex);
136
- return basename(prefix).toLowerCase() === 'npm' ? prefix : undefined;
138
+ if (['package.json', 'package-lock.json', 'npm-shrinkwrap.json'].some(file => existsSync(join(prefix, file))))
139
+ return undefined;
140
+ let npmShim = false;
141
+ try {
142
+ npmShim = statSync(join(prefix, 'dsh.cmd')).isFile();
143
+ }
144
+ catch {
145
+ // No shim (or a directory of that name): not a proven npm global tree.
146
+ }
147
+ return basename(prefix).toLowerCase() === 'npm' || npmShim ? prefix : undefined;
137
148
  }
138
149
  function isWritable(path) {
139
150
  try {
@@ -76,6 +76,12 @@ export declare function pendingStarMilestone(stats: UsageStats): number | null;
76
76
  * @returns 配弹窗且已达成的档位下标;没有则 `null`。
77
77
  */
78
78
  export declare function dueStarModal(dir?: string): number | null;
79
+ /**
80
+ * 把当前进程缓存的时长补记到账本。供进程被 `execve` 原地替换前调用——
81
+ * `execve` 不触发 JS 的 `exit` 事件,退出钩子不会执行。
82
+ * @param dir - 数据目录(测试可注入)。
83
+ */
84
+ export declare function flushUsageStats(dir?: string): void;
79
85
  /**
80
86
  * 记一次启动:累计次数 +1、开始计时,并挂上兜底落盘与退出补记。
81
87
  * 同一进程里对同一目录重复调用只生效一次(LogoV2 反复挂载也只 +1)。
@@ -1 +1 @@
1
- {"version":3,"file":"usageStats.d.ts","sourceRoot":"","sources":["../../src/usageStats.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AASH,2BAA2B;AAC3B,MAAM,WAAW,aAAa;IAC5B,gBAAgB;IAChB,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;IAC1B,gBAAgB;IAChB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IACvB;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAA;CACzB;AAED;;;;GAIG;AACH,eAAO,MAAM,eAAe,EAAE,SAAS,aAAa,EAWnD,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,GAAI,OAAO,MAAM,KAAG,OAAiD,CAAA;AAErG,cAAc;AACd,MAAM,WAAW,UAAU;IACzB,cAAc;IACd,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,cAAc;IACd,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,oDAAoD;IACpD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;CAC5B;AAED,wBAAwB;AACxB,eAAO,MAAM,WAAW,EAAE,UAAuD,CAAA;AAKjF;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,GAAG,GAAE,MAAiB,GAAG,UAAU,CAS5D;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,EAAE,GAAG,GAAE,MAAiB,GAAG,OAAO,CAW7E;AAOD;;;;GAI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