@huaqiu/dsh-tool-plugin-update 0.6.25 → 0.6.28

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huaqiu/dsh-tool-plugin-update",
3
- "version": "0.6.25",
3
+ "version": "0.6.28",
4
4
  "type": "module",
5
5
  "main": "./lib/index.mjs",
6
6
  "types": "./lib/index.d.mts",
@@ -31,8 +31,8 @@
31
31
  "@deepseek-ai/dsh-host-webserver": "^0.2.1-rc.2"
32
32
  },
33
33
  "dependencies": {
34
- "@huaqiu/dsh-plugin-log": "0.6.25",
35
- "@huaqiu/dsh-sidebar-entry": "0.6.25"
34
+ "@huaqiu/dsh-plugin-log": "0.6.28",
35
+ "@huaqiu/dsh-sidebar-entry": "0.6.28"
36
36
  },
37
37
  "files": [
38
38
  "lib",
@@ -41,7 +41,7 @@ import {
41
41
  } from './api.js'
42
42
  import { COPY } from './copy.js'
43
43
  import { PLUGIN_ICON } from './icons.js'
44
- import { openUpdateView, type UpdateStoreLike } from './update-view.js'
44
+ import { openUpdateView, takeOverUpdateView, type UpdateStoreLike } from './update-view.js'
45
45
  import { badgeCountFor } from './badge.js'
46
46
 
47
47
  /** Stable data attributes identifying the injected entry row. */
@@ -1890,6 +1890,16 @@ export interface ClientContext {
1890
1890
  effect?(fn: () => (() => void) | void, label?: string): void
1891
1891
  }
1892
1892
 
1893
+ /**
1894
+ * A transient "installing" flag with no live job behind it is stale. An install
1895
+ * of this plugin's own package restarts DSH, so the last `/activity` read can
1896
+ * be lost; `schedulePoll()` then stops (no active job) while the 60 s report
1897
+ * refresh keeps running — the footer would say "更新中" forever. A real install
1898
+ * always has a non-terminal active job, so after this window without one we
1899
+ * stop claiming progress rather than waiting for a read that will never come.
1900
+ */
1901
+ const STALE_INSTALL_MS = 120_000
1902
+
1893
1903
  /**
1894
1904
  * In-memory update-report store shared by the badge and the modal.
1895
1905
  *
@@ -1911,6 +1921,11 @@ export class UpdateStore implements UpdateStoreLike {
1911
1921
  private pollInFlight = false
1912
1922
  /** The job this panel started (or adopted) and is following by id. */
1913
1923
  private watchedJobId: string | null = null
1924
+ /** True between `setInstalling(true)` and the install response landing: a
1925
+ * state read in that window has no job yet, and must not clear the flag. */
1926
+ private installPending = false
1927
+ /** When the transient installing flag was raised (stale-install watchdog). */
1928
+ private installingSinceMs = 0
1914
1929
  /** hq-edge process identity from the last `/activity` read. */
1915
1930
  private activityBootId: string | null = null
1916
1931
  private stalledFlag = false
@@ -1949,6 +1964,9 @@ export class UpdateStore implements UpdateStoreLike {
1949
1964
  * page comes back to a server that has no "active job" left to report).
1950
1965
  */
1951
1966
  watchJob(id: string | null): void {
1967
+ // The install response has landed: from here on a state read is allowed to
1968
+ // judge whether the flag is stale (see settleStaleInstall).
1969
+ this.installPending = false
1952
1970
  this.watchedJobId = id
1953
1971
  if (id !== null) this.schedulePoll()
1954
1972
  }
@@ -1987,7 +2005,16 @@ export class UpdateStore implements UpdateStoreLike {
1987
2005
  this.active = status.activeJob
1988
2006
  this.recent = status.recentJobs
1989
2007
  if (this.active !== null) this.watchedJobId = this.active.id
1990
- if (wasActive && this.active === null) this.observeSettled()
2008
+ // `/status` is the fallback for an older hq-edge with no `/activity`. It
2009
+ // has no `job` field, but a settled watched job is still in recentJobs —
2010
+ // the same answer the re-attach read would have given us.
2011
+ const watchedSettled =
2012
+ this.watchedJobId !== null &&
2013
+ status.recentJobs.some(
2014
+ (entry) => entry.id === this.watchedJobId && TERMINAL_PHASES.has(entry.phase),
2015
+ )
2016
+ if ((wasActive && this.active === null) || watchedSettled) this.observeSettled()
2017
+ else this.settleStaleInstall()
1991
2018
  this.emit()
1992
2019
  this.schedulePoll()
1993
2020
  }
@@ -1995,7 +2022,7 @@ export class UpdateStore implements UpdateStoreLike {
1995
2022
  /**
1996
2023
  * Adopt a `/activity` snapshot.
1997
2024
  *
1998
- * Two things only this read can tell us:
2025
+ * Three things only this read can tell us:
1999
2026
  *
2000
2027
  * - `bootId` changed → hq-edge was recreated underneath the page. That is
2001
2028
  * what an install of this plugin's own package looks like from here: the
@@ -2003,6 +2030,11 @@ export class UpdateStore implements UpdateStoreLike {
2003
2030
  * trusted, so the installing flag is cleared and the report re-read.
2004
2031
  * - `stalled` → the active job has stopped advancing. The progress bar must
2005
2032
  * stop pretending; the panel shows a "confirming" note with a retry.
2033
+ * - `job` → the durable record of the job we followed by id. It is the ONLY
2034
+ * evidence left when that job settled while DSH was restarting: the
2035
+ * active slot is already empty on this page's first read back, so there
2036
+ * is no active → null transition to notice. Without this check the
2037
+ * installing flag survives the restart and the panel stays "更新中".
2006
2038
  */
2007
2039
  private applyActivity(activity: UpdateActivity): void {
2008
2040
  const rebooted = this.activityBootId !== null && this.activityBootId !== activity.bootId
@@ -2014,13 +2046,25 @@ export class UpdateStore implements UpdateStoreLike {
2014
2046
  this.active = activity.activeJob
2015
2047
  if (activity.job !== null) this.upsertRecent(activity.job)
2016
2048
 
2049
+ // Settle on EITHER signal: the active slot emptied, or our own job (the one
2050
+ // `watchJob` made every read ask about) reached a terminal phase. The id
2051
+ // comparison matters — `job` may be any job in the ledger.
2052
+ const watchedSettled =
2053
+ this.watchedJobId !== null &&
2054
+ activity.job !== null &&
2055
+ activity.job.id === this.watchedJobId &&
2056
+ TERMINAL_PHASES.has(activity.job.phase)
2057
+ const settled = (wasActive && this.active === null) || watchedSettled
2058
+
2017
2059
  if (rebooted) {
2018
2060
  this.installingFlag = false
2019
2061
  this.stalledFlag = false
2020
2062
  this.phaseAgeMsValue = 0
2021
- void this.refresh()
2063
+ // observeSettled() refreshes too — avoid two identical report reads.
2064
+ if (!settled) void this.refresh()
2022
2065
  }
2023
- if (wasActive && this.active === null) this.observeSettled()
2066
+ if (settled) this.observeSettled()
2067
+ else this.settleStaleInstall()
2024
2068
  this.emit()
2025
2069
  this.schedulePoll()
2026
2070
  }
@@ -2032,12 +2076,35 @@ export class UpdateStore implements UpdateStoreLike {
2032
2076
  */
2033
2077
  private observeSettled(): void {
2034
2078
  this.installingFlag = false
2079
+ this.installPending = false
2080
+ this.installingSinceMs = 0
2035
2081
  this.stalledFlag = false
2036
2082
  this.phaseAgeMsValue = 0
2037
2083
  this.watchedJobId = null
2038
2084
  void this.refresh()
2039
2085
  }
2040
2086
 
2087
+ /**
2088
+ * Last-resort reset for the restart case: the page is back, the transient
2089
+ * installing flag is still set, but no job is running and no re-attach read
2090
+ * is coming (the poll loop stops without an active job). Called on mount,
2091
+ * on every successful state read and on each idle refresh, so the footer
2092
+ * cannot stay "更新中" forever. A failed read is not evidence of anything, so
2093
+ * it never clears the flag. A live job, or an install request in flight, always
2094
+ * wins — this only clears a flag with nothing behind it.
2095
+ */
2096
+ settleStaleInstall(): void {
2097
+ if (!this.installingFlag || this.installPending) return
2098
+ const liveJob = this.active !== null && !TERMINAL_PHASES.has(this.active.phase)
2099
+ if (liveJob) return
2100
+ // A watched id means /activity may still answer; give it the watchdog
2101
+ // window before declaring the re-attach lost. No watched id means the
2102
+ // panel has nothing left to ask about, so reset on the first state read.
2103
+ const timedOut = Date.now() - this.installingSinceMs >= STALE_INSTALL_MS
2104
+ if (this.watchedJobId !== null && !timedOut) return
2105
+ this.observeSettled()
2106
+ }
2107
+
2041
2108
  /** Keep one job fresh in the local history without duplicating it. */
2042
2109
  private upsertRecent(job: UpdateJob): void {
2043
2110
  const rest = this.recent.filter((entry) => entry.id !== job.id)
@@ -2059,6 +2126,28 @@ export class UpdateStore implements UpdateStoreLike {
2059
2126
  }
2060
2127
  }
2061
2128
 
2129
+ /**
2130
+ * Re-attach after this plugin's own bundle was swapped mid-install.
2131
+ *
2132
+ * The freshly re-mounted panel starts with no state at all. If the restarted
2133
+ * DSH is not answering yet, one failed read leaves it on its skeleton until
2134
+ * the next 60 s idle tick — indistinguishable from "the page never updated".
2135
+ * Retry the durable-job read and the report a few times; both emit, so the
2136
+ * panel converges as soon as the server is back.
2137
+ */
2138
+ async resync(attempts = 6, delayMs = 1_000): Promise<void> {
2139
+ for (let attempt = 0; attempt < attempts; attempt += 1) {
2140
+ await this.fetchStatus()
2141
+ await this.refresh()
2142
+ if (this.current !== null) return
2143
+ if (attempt + 1 < attempts) {
2144
+ await new Promise<void>((resolve) => {
2145
+ window.setTimeout(resolve, delayMs)
2146
+ })
2147
+ }
2148
+ }
2149
+ }
2150
+
2062
2151
  /**
2063
2152
  * One read. While a job is being followed, ask `/activity` about THAT job
2064
2153
  * (survives the restart an update of this plugin causes); otherwise fall back
@@ -2086,7 +2175,16 @@ export class UpdateStore implements UpdateStoreLike {
2086
2175
  this.pollTimer = null
2087
2176
  }
2088
2177
  const job = this.active
2089
- if (job === null || TERMINAL_PHASES.has(job.phase)) {
2178
+ const jobActive = job !== null && !TERMINAL_PHASES.has(job.phase)
2179
+ // Re-attach window: the install replaced this plugin's own process, so the
2180
+ // active slot can be empty for a while even though the job has not been
2181
+ // confirmed settled. Keep asking about the watched id (bounded by the
2182
+ // stale-install watchdog) instead of letting one lost read end the poll.
2183
+ const awaitingReattach =
2184
+ this.installingFlag &&
2185
+ this.watchedJobId !== null &&
2186
+ Date.now() - this.installingSinceMs < STALE_INSTALL_MS
2187
+ if (!jobActive && !awaitingReattach) {
2090
2188
  this.pollBackoffMs = 1_000
2091
2189
  return
2092
2190
  }
@@ -2136,14 +2234,20 @@ export class UpdateStore implements UpdateStoreLike {
2136
2234
 
2137
2235
  /** Mark the set as installing (set by the modal before the restart). */
2138
2236
  setInstalling(flag: boolean): void {
2237
+ if (flag && !this.installingFlag) this.installingSinceMs = Date.now()
2139
2238
  this.installingFlag = flag
2239
+ this.installPending = flag
2240
+ if (!flag) this.installingSinceMs = 0
2140
2241
  this.emit()
2141
2242
  }
2142
2243
 
2143
- /** Idle refresh while mounted (60 s) — no overlapping poll loop. */
2244
+ /** Idle refresh while mounted (60 s) — no overlapping poll loop. This is
2245
+ * also the only timer that still runs after a re-attach read was lost, so
2246
+ * it carries the stale-install fallback as well. */
2144
2247
  startIdleRefresh(): void {
2145
2248
  if (this.refreshTimer !== null) return
2146
2249
  this.refreshTimer = window.setInterval(() => {
2250
+ this.settleStaleInstall()
2147
2251
  void this.refresh()
2148
2252
  }, 60_000) as unknown as number
2149
2253
  }
@@ -2269,9 +2373,28 @@ export function apply(ctx: ClientContext): () => void {
2269
2373
  }
2270
2374
  })
2271
2375
 
2376
+ // Mount path: a page that came back from the DSH restart must not inherit a
2377
+ // transient "installing" state with no job to follow.
2378
+ store.settleStaleInstall()
2272
2379
  store.startIdleRefresh()
2273
- void store.refresh()
2274
- void store.fetchStatus()
2380
+
2381
+ // A self-update (this plugin standalone, or the whole set including it)
2382
+ // replaces THIS client bundle mid-install. The host disposes the old module
2383
+ // — stopping its store and clearing its subscriptions — and applies a fresh
2384
+ // one, but the open review panel's DOM survives. Left alone it stays frozen
2385
+ // at "Updating…" forever, subscribed to the dead store. Re-create it against
2386
+ // this instance's store so it re-reads the settled job and the report from
2387
+ // the restarted DSH and shows the final state. When no panel was open (a
2388
+ // normal boot) just do the usual mount reads.
2389
+ if (takeOverUpdateView()) {
2390
+ openView()
2391
+ // The restarted DSH may not answer the first read; keep re-reading until
2392
+ // it does rather than waiting for the 60 s idle tick (see resync()).
2393
+ void store.resync()
2394
+ } else {
2395
+ void store.refresh()
2396
+ void store.fetchStatus()
2397
+ }
2275
2398
 
2276
2399
  const dispose = (): void => {
2277
2400
  try {
@@ -1037,6 +1037,36 @@ function showToast(message: string, tone: 'success' | 'error'): void {
1037
1037
 
1038
1038
  // ── Panel ─────────────────────────────────────────────────────────────────
1039
1039
 
1040
+ /**
1041
+ * Installing a set that contains THIS plugin replaces its client bundle in the
1042
+ * browser: the host tears down the old module's effects (stopping its store and
1043
+ * clearing its subscriptions) and re-applies a fresh module instance. The open
1044
+ * review panel's DOM is NOT part of that teardown — it stays on screen, but
1045
+ * bound to the disposed store, so it never sees the settled job or the new
1046
+ * report. The replacement module calls this to tear the orphan down and
1047
+ * re-create the panel against its own store.
1048
+ *
1049
+ * The close handle lives ON the overlay element, not in module memory: by the
1050
+ * time the replacement runs, this module's variables are gone, while
1051
+ * document.body is not.
1052
+ */
1053
+ interface PanelOverlay extends HTMLElement {
1054
+ __hqpuClose?: () => void
1055
+ }
1056
+
1057
+ /**
1058
+ * Tear down a panel left open by a previous instance of this plugin.
1059
+ * @returns true when a panel had been open and the caller should re-open it.
1060
+ */
1061
+ export function takeOverUpdateView(): boolean {
1062
+ const overlay = document.querySelector<HTMLElement>('[data-hq-plugin-update-view]')
1063
+ if (overlay === null) return false
1064
+ const close = (overlay as PanelOverlay).__hqpuClose
1065
+ if (typeof close === 'function') close()
1066
+ else overlay.remove()
1067
+ return true
1068
+ }
1069
+
1040
1070
  /**
1041
1071
  * Open (or focus) the update panel. Re-renders from the store; when the store
1042
1072
  * is stale the panel first refreshes the report.
@@ -1893,6 +1923,11 @@ export function openUpdateView(store: UpdateStoreLike, copy: UpdateCopy): void {
1893
1923
  if (previouslyFocused !== null && previouslyFocused.isConnected) previouslyFocused.focus()
1894
1924
  }
1895
1925
 
1926
+ // Publish the teardown on the DOM node so a replacement module instance can
1927
+ // dispose this panel properly (unsubscribe, drop listeners, remove the
1928
+ // overlay) before re-opening it against its own store.
1929
+ ;(overlay as PanelOverlay).__hqpuClose = onClose
1930
+
1896
1931
  render()
1897
1932
  }
1898
1933