dsh-ris-ide 1.8.6 → 1.8.7

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.
package/lib/index.js CHANGED
@@ -66,6 +66,7 @@ import {
66
66
  waitForJdwpListening, toJsonSafe,
67
67
  } from './debug/engine.js'
68
68
  import { allowList, autoRules, engineSide, gitProxyPlan, injectionFor, isSocksCapableProgram, mergeConfigs, parseHostSpec, parsePortSpec, programKey, proxyConfigPath, proxyHomeDir, proxyPid, proxyStatus, readProxyConfig, ruleEffectiveness, runWindowsGitConfig, startProxy, stopProxy, stopProxySync, writeProxyConfig } from './proxy.js'
69
+ import { proxyServiceSnapshot, releaseProxyLifecycle, startProxyService, stopProxyService } from './proxy-lifecycle.js'
69
70
  import {
70
71
  buildProfile,
71
72
  redirectorCommand,
@@ -670,6 +671,9 @@ function armLocalProxyCleanup() {
670
671
  // same three layers; a second hook would just run the same sync cleanups twice.
671
672
  if (lifecycle !== null) return
672
673
  localProxyCleanup = () => {
674
+ // 待启动 must not outlive the process doing the waiting (a wait means "this
675
+ // process will start the engine"; nobody is left to do it after this exit).
676
+ try { releaseProxyLifecycle() } catch { /* best effort while exiting */ }
673
677
  try { stopProxySync() } catch { /* best effort while exiting */ }
674
678
  try { stopRedirectorSync() } catch { /* best effort while exiting */ }
675
679
  try { clearGitProxySync() } catch { /* best effort while exiting */ }
@@ -3294,14 +3298,20 @@ export async function readPidFile(logPath) {
3294
3298
  const pid = Number(parts[0])
3295
3299
  const wrapperPid = Number(parts[1])
3296
3300
  const startedAt = Number(parts[2])
3297
- const graceMs = Number(parts[3])
3301
+ // The 4th field is optional and every wrapper writes it, empty when the program
3302
+ // did not ask for a grace. Number('') is 0, which clampBytes would turn into the
3303
+ // 1 KiB floor — a one-second TERM→KILL window that kills the wrapper before it
3304
+ // can write its exit banner. Treat "empty" as "not recorded" so the caller's
3305
+ // fallback (per-program config or the route default) wins.
3306
+ const recordedGrace = parts[3] !== undefined && /^\d+$/.test(parts[3]) ? Number(parts[3]) : Number.NaN
3307
+ const graceMs = recordedGrace > 0 ? recordedGrace : undefined
3298
3308
  if (!Number.isSafeInteger(pid) || pid <= 1) return undefined
3299
3309
  if (!Number.isSafeInteger(wrapperPid) || wrapperPid <= 1) return undefined
3300
3310
  return {
3301
3311
  pid,
3302
3312
  wrapperPid,
3303
3313
  startedAt: Number.isFinite(startedAt) ? startedAt : undefined,
3304
- graceMs: parts[3] !== undefined && Number.isFinite(graceMs) ? graceMs : undefined,
3314
+ graceMs,
3305
3315
  }
3306
3316
  }
3307
3317
 
@@ -5036,23 +5046,73 @@ async function clipboardStageMapping(root) {
5036
5046
  const entries = await readdir(base, { withFileTypes: true }).catch(() => [])
5037
5047
  for (const entry of entries) {
5038
5048
  if (!entry.isDirectory()) continue
5039
- const manifest = await readFile(join(base, entry.name, CLIPBOARD_STAGE_MANIFEST), 'utf8').catch(() => undefined)
5040
- if (manifest === undefined) continue
5041
- let parsed
5042
- try {
5043
- parsed = JSON.parse(manifest)
5044
- } catch {
5045
- continue
5046
- }
5047
- if (parsed === null || typeof parsed !== 'object') continue
5048
- const staged = Array.isArray(parsed.staged) ? parsed.staged : []
5049
- const originals = Array.isArray(parsed.originals) ? parsed.originals : []
5050
- for (let index = 0; index < staged.length; index += 1) {
5051
- if (typeof staged[index] === 'string' && typeof originals[index] === 'string') {
5052
- mapping.set(resolve(staged[index]), originals[index])
5053
- }
5049
+ const table = await clipboardStageTable(join(base, entry.name))
5050
+ for (const [staged, original] of table) mapping.set(staged, original)
5051
+ }
5052
+ return mapping
5053
+ }
5054
+
5055
+ /**
5056
+ * One batch's `staged → original` table, read from its manifest.
5057
+ * @param {string} batchDir - absolute `<…>/stage/<batch>`.
5058
+ * @returns {Promise<Map<string,string>>} resolved staged path → original path.
5059
+ */
5060
+ async function clipboardStageTable(batchDir) {
5061
+ const table = new Map()
5062
+ const manifest = await readFile(join(batchDir, CLIPBOARD_STAGE_MANIFEST), 'utf8').catch(() => undefined)
5063
+ if (manifest === undefined) return table
5064
+ let parsed
5065
+ try {
5066
+ parsed = JSON.parse(manifest)
5067
+ } catch {
5068
+ return table
5069
+ }
5070
+ if (parsed === null || typeof parsed !== 'object') return table
5071
+ const staged = Array.isArray(parsed.staged) ? parsed.staged : []
5072
+ const originals = Array.isArray(parsed.originals) ? parsed.originals : []
5073
+ for (let index = 0; index < staged.length; index += 1) {
5074
+ if (typeof staged[index] === 'string' && typeof originals[index] === 'string') {
5075
+ table.set(resolve(staged[index]), originals[index])
5054
5076
  }
5055
5077
  }
5078
+ return table
5079
+ }
5080
+
5081
+ /**
5082
+ * staged path → original path, resolved from the PATH ITSELF.
5083
+ *
5084
+ * A staged copy lives at `<workspace>/.dsh-ris-ide/stage/<batch>/<i>/<name>`, so
5085
+ * the manifest that owns it is findable without knowing which workspace it came
5086
+ * from. Reading only the CURRENT workspace's manifests broke the
5087
+ * cross-workspace case: after switching to B, A's staged paths came back
5088
+ * verbatim, the page compared them with its own clipboard, decided they were
5089
+ * NEWER external files and turned a Copy into an import (and dropped the copy).
5090
+ * @param {string[]} paths - paths read back from the OS clipboard.
5091
+ * @returns {Promise<Map<string,string>>} staged → original for the ones we published.
5092
+ */
5093
+ async function clipboardStageMappingFor(paths) {
5094
+ const mapping = new Map()
5095
+ const manifests = new Map()
5096
+ const marker = `${sep}${CLIPBOARD_STAGE_DIR.split('/').join(sep)}${sep}`
5097
+ for (const raw of paths) {
5098
+ if (typeof raw !== 'string' || raw === '') continue
5099
+ const resolved = resolve(raw)
5100
+ const at = resolved.indexOf(marker)
5101
+ if (at < 0) continue
5102
+ const rest = resolved.slice(at + marker.length)
5103
+ const cut = rest.indexOf(sep)
5104
+ if (cut <= 0) continue
5105
+ // `at + marker.length` sits right after the separator: prepending it keeps
5106
+ // the drive/UNC prefix intact on Windows.
5107
+ const batchDir = resolved.slice(0, at + marker.length) + rest.slice(0, cut)
5108
+ let table = manifests.get(batchDir)
5109
+ if (table === undefined) {
5110
+ table = await clipboardStageTable(batchDir)
5111
+ manifests.set(batchDir, table)
5112
+ }
5113
+ const original = table.get(resolved)
5114
+ if (original !== undefined) mapping.set(resolved, original)
5115
+ }
5056
5116
  return mapping
5057
5117
  }
5058
5118
 
@@ -5311,15 +5371,28 @@ async function serveClipboardRead(ctx, req, res) {
5311
5371
  // second copy of files the tree already has).
5312
5372
  let files = state.files
5313
5373
  let warnings = []
5374
+ // Translate by the staged path ITSELF (see clipboardStageMappingFor): the
5375
+ // clipboard outlives a workspace switch, the current root does not.
5376
+ const mapping = await clipboardStageMappingFor(files)
5377
+ if (mapping.size > 0) files = files.map((path) => mapping.get(resolve(path)) ?? path)
5378
+ // A CUT publish leaves the REAL paths on the clipboard. Once they have been
5379
+ // pasted (or deleted) those paths are gone, and handing them back would offer
5380
+ // the page a ghost selection that fails item by item. Drop them and say so.
5381
+ const missing = []
5382
+ const present = []
5383
+ for (const path of files) {
5384
+ const info = await lstat(path).catch(() => undefined)
5385
+ if (info === undefined) missing.push(path)
5386
+ else present.push(path)
5387
+ }
5388
+ files = present
5314
5389
  if (root !== undefined) {
5315
- const mapping = await clipboardStageMapping(root)
5316
- if (mapping.size > 0) files = files.map((path) => mapping.get(resolve(path)) ?? path)
5317
5390
  // A staged original that vanished between Ctrl+C and Ctrl+V is the exact
5318
5391
  // symptom this route exists to explain: say so once, with the staged path
5319
5392
  // that still holds the bytes, and leave a line in stage/integrity.log.
5320
5393
  warnings = (await verifyClipboardStageOriginals(root)).map((finding) => finding.message)
5321
5394
  }
5322
- writeJson(res, 200, { ok: true, files, text: state.text, method: state.method, warnings })
5395
+ writeJson(res, 200, { ok: true, files, text: state.text, method: state.method, warnings, missing })
5323
5396
  } catch (error) {
5324
5397
  writeTailError(res, error)
5325
5398
  }
@@ -7194,9 +7267,14 @@ async function proxyView(cwd, gitHost) {
7194
7267
  // dialog shows a takeover banner with a one-click stop instead of pretending this
7195
7268
  // session started it. The flag is dropped as soon as that engine is gone.
7196
7269
  const inherited = inheritedEngine !== null && inheritedEngine.pid === status.pid ? inheritedEngine : null
7270
+ // running / pending(待启动) / stopped, plus how long a wait has been going and the
7271
+ // upstream error that caused it. Any process may read it (the daemon may own the
7272
+ // wait), so it comes from the shared state file when this host is not watching.
7273
+ const service = proxyServiceSnapshot()
7197
7274
  return {
7198
7275
  ok: true,
7199
7276
  side: engineSide(),
7277
+ service,
7200
7278
  // The dialog sends this back with its save, so a table edited in another window
7201
7279
  // is refused instead of silently overwritten.
7202
7280
  revision: configRevision(status.config),
@@ -7938,19 +8016,28 @@ async function serveProxyAction(ctx, req, res, options) {
7938
8016
  // answers): this engine is ours, so install the exit hook that auto mode would
7939
8017
  // otherwise rely on the daemon to provide (review C2-P1-3).
7940
8018
  armLocalProxyCleanup()
7941
- const engine = await startProxy(cwd)
7942
- const redirector = await startRedirectors({ interactive })
7943
- const git = await syncGitProxy({ cwd, host: gitHost, enabled: true })
7944
- if (engine.ok === true) inheritedEngine = null
7945
- result = Object.assign({}, engine, { redirector, git })
8019
+ // The service probes SOCKS before spawning anything: an upstream that is down
8020
+ // parks the request in 待启动 and a watcher brings the engine up later (the
8021
+ // engine would otherwise bind its loopback ports and fail every tunnel).
8022
+ const outcome = await startProxyService({ cwd, gitHost, interactive, syncGit: (plan) => syncGitProxy(plan) })
8023
+ if (outcome.phase === 'running') inheritedEngine = null
8024
+ result = Object.assign({}, outcome.engine, {
8025
+ redirector: outcome.redirector,
8026
+ git: outcome.git,
8027
+ phase: outcome.phase,
8028
+ waiting: outcome.waiting === true,
8029
+ reason: outcome.reason ?? '',
8030
+ })
7946
8031
  } else if (action === 'stop') {
7947
- const git = await syncGitProxy({ cwd, host: gitHost, enabled: false })
7948
- const redirector = await stopRedirector()
7949
- const stopped = await stopProxy(cwd)
8032
+ const outcome = await stopProxyService({ cwd, gitHost, syncGit: (plan) => syncGitProxy(plan) })
7950
8033
  disarmLocalProxyCleanup()
7951
8034
  // The banner must not come back once the adopted engine is really gone.
7952
8035
  if (proxyPid() === null) inheritedEngine = null
7953
- result = Object.assign({}, stopped, { redirector, git })
8036
+ result = Object.assign({}, outcome.engine, {
8037
+ redirector: outcome.redirector,
8038
+ git: outcome.git,
8039
+ phase: outcome.phase,
8040
+ })
7954
8041
  } else if (action === 'git-on' || action === 'git-off') {
7955
8042
  const plan = gitProxyPlan(readProxyConfig(cwd), { host: gitHost, enabled: action === 'git-on' })
7956
8043
  result = action === 'git-on'
@@ -11809,6 +11896,7 @@ export const __test = {
11809
11896
  stageClipboardCopy,
11810
11897
  pruneClipboardStage,
11811
11898
  clipboardStageMapping,
11899
+ clipboardStageMappingFor,
11812
11900
  verifyClipboardStageOriginals,
11813
11901
  CLIPBOARD_STAGE_INTEGRITY_LOG,
11814
11902
  serveClipboardFiles,