dsh-autorestart 0.8.4 → 0.8.6

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 (42) hide show
  1. package/CHANGELOG.md +313 -0
  2. package/README.md +465 -40
  3. package/assets/icon-9c245e5b.ico +0 -0
  4. package/assets/icon-rescue-1902bb15.ico +0 -0
  5. package/assets/icon-rescue-1902bb15.png +0 -0
  6. package/client.js +3002 -170
  7. package/cordis.patch.yml +12 -0
  8. package/docs/README.zh.md +231 -23
  9. package/docs/platform-matrix.md +171 -55
  10. package/docs/real-machine-verification-0.8.6.md +202 -0
  11. package/index.js +13272 -8513
  12. package/lib/autostart-advice.js +31 -2
  13. package/lib/backup.js +2768 -21
  14. package/lib/checkpoint.js +214 -7
  15. package/lib/cookie-clear.js +260 -0
  16. package/lib/lifecycle.js +23 -40
  17. package/lib/plugin-compare.js +499 -0
  18. package/lib/plugin-failure.js +382 -0
  19. package/lib/plugin-recover.js +1557 -0
  20. package/lib/plugin-watch.js +213 -0
  21. package/lib/rescue-paths.js +982 -56
  22. package/lib/restart-phase.js +5 -0
  23. package/lib/restart-verdict.js +379 -0
  24. package/lib/restart.js +244 -7
  25. package/lib/scheduler-capability.js +19 -3
  26. package/lib/sleep.js +190 -29
  27. package/lib/watchdog-disarm.js +46 -2
  28. package/lib/watchdog-schedule.js +834 -17
  29. package/lib/watchdog-tier.js +8 -1
  30. package/lib/watchdog.js +1222 -47
  31. package/locale/en.json +175 -68
  32. package/locale/zh.json +175 -68
  33. package/package.json +3 -2
  34. package/screenshots.json +6 -6
  35. package/scripts/README.md +6 -3
  36. package/scripts/build-icon.mjs +998 -0
  37. package/scripts/clear-dsh-auth-cookies.mjs +257 -0
  38. package/scripts/emergency-disable.sh +69 -2
  39. package/scripts/emergency-toggle.ps1 +85 -69
  40. package/scripts/rescue-strings.zh.json +36 -18
  41. package/scripts/restore-profile.ps1 +182 -1
  42. package/scripts/watchdog.mjs +432 -79
@@ -0,0 +1,213 @@
1
+ /**
2
+ * WHAT CAN THE RUNNING HOST SEE, AND WHAT DID IT STOP SEEING?
3
+ *
4
+ * A plugin can be present on disk and still do nothing: it never registered, or
5
+ * its fibre died quietly. There is no single "is every plugin alive?" API, but
6
+ * three surfaces together cover what a plugin can actually DO:
7
+ *
8
+ * tools - plugins that provide tools (codekg, vault, release-board…)
9
+ * clientModules - plugins that draw something in the interface
10
+ * packages - packages that register a schema
11
+ *
12
+ * A plugin that provides no tool, draws nothing and registers nothing has also
13
+ * done nothing, so these three layers are enough for the practical cases.
14
+ *
15
+ * THIS MODULE ONLY COLLECTS AND COMPARES. It never decides "a plugin is broken",
16
+ * it never repairs anything and it imports nothing from the host: everything it
17
+ * needs comes in through `ctx`. It runs on the boot path, so it must never throw.
18
+ */
19
+
20
+ /** The three surfaces, in a stable order. */
21
+ export const WATCH_LAYERS = Object.freeze(['tools', 'clientModules', 'packages'])
22
+
23
+ /**
24
+ * Turn whatever a service returned into a plain list of names.
25
+ *
26
+ * The three surfaces do not share a shape, and their shape may change between
27
+ * host versions. Rather than hard-coding one, accept the shapes that mean
28
+ * something and return `null` for anything that does not - `null` says "I could
29
+ * not see this layer", which the comparison treats as "no news", never as "gone".
30
+ *
31
+ * @param {unknown} value
32
+ * @returns {string[]|null}
33
+ */
34
+ export function toNameList(value) {
35
+ if (value === null || value === undefined) return null
36
+ const pick = (item) => {
37
+ if (typeof item === 'string') return item.trim() === '' ? null : item
38
+ if (item === null || typeof item !== 'object') return null
39
+ for (const key of ['name', 'id', 'key', 'label']) {
40
+ const candidate = item[key]
41
+ if (typeof candidate === 'string' && candidate.trim() !== '') return candidate
42
+ }
43
+ return null
44
+ }
45
+ const clean = (list) => {
46
+ const out = []
47
+ const seen = new Set()
48
+ for (const item of list) {
49
+ const name = pick(item)
50
+ if (name === null) continue
51
+ if (seen.has(name)) continue
52
+ seen.add(name)
53
+ out.push(name)
54
+ }
55
+ return out.sort()
56
+ }
57
+ try {
58
+ if (Array.isArray(value)) return clean(value)
59
+ if (value instanceof Map) return clean([...value.keys()])
60
+ if (value instanceof Set) return clean([...value])
61
+ if (typeof value === 'object') {
62
+ // A map keyed by name: the keys ARE the names.
63
+ return clean(Object.keys(value))
64
+ }
65
+ } catch {
66
+ return null
67
+ }
68
+ return null
69
+ }
70
+
71
+ /** Call one surface and normalise it; any failure becomes `null`. */
72
+ function readLayer(read, map = toNameList) {
73
+ try {
74
+ const raw = read()
75
+ return map(raw)
76
+ } catch {
77
+ return null
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Collect the three lists from a live Cordis context.
83
+ *
84
+ * Every layer is independent: a missing service, a service that throws, or a
85
+ * shape nothing recognises only makes THAT layer `null`. The other two are still
86
+ * collected, because a partial picture beats no picture on the boot path.
87
+ *
88
+ * @param {object} ctx
89
+ * @param {{ now?: number }} [options]
90
+ * @returns {{ at: number, tools: string[]|null, clientModules: string[]|null, packages: string[]|null }}
91
+ */
92
+ /**
93
+ * Read one optional service from the context.
94
+ *
95
+ * `ctx.get(name)` is Cordis' OFFICIAL door for OPTIONAL access: a service that is
96
+ * not there answers `undefined` instead of making the plugin refuse to activate.
97
+ * A plain `ctx.name` only works for services this plugin declared in `inject`,
98
+ * which is a HARD dependency - measured on the real host, `ctx.clientModules` and
99
+ * `ctx.typert` were both `undefined` while `ctx.tools` worked, because only
100
+ * `tools` is in the inject list.
101
+ *
102
+ * Declaring the other two in `inject` would be the wrong fix: this plugin is the
103
+ * one that brings a dead host back, so it must never refuse to load just because
104
+ * an optional monitoring surface is missing.
105
+ *
106
+ * Older hosts without `ctx.get` fall back to the property; neither path throws.
107
+ *
108
+ * @param {object} ctx
109
+ * @param {string} name
110
+ * @returns {unknown}
111
+ */
112
+ function serviceOf(ctx, name) {
113
+ const safe = ctx !== null && typeof ctx === 'object' ? ctx : {}
114
+ try {
115
+ if (typeof safe.get === 'function') return safe.get(name)
116
+ } catch {
117
+ // A `get` that throws is the same as a service that is not there.
118
+ return undefined
119
+ }
120
+ return safe[name]
121
+ }
122
+
123
+ export function collectCapabilities(ctx, { now = Date.now() } = {}) {
124
+ const at = Number.isFinite(Number(now)) ? Number(now) : Date.now()
125
+ return {
126
+ at,
127
+ tools: readLayer(() => {
128
+ const service = serviceOf(ctx, 'tools')
129
+ if (!service || typeof service.schemas !== 'function') return undefined
130
+ return service.schemas()
131
+ }),
132
+ clientModules: readLayer(() => {
133
+ const service = serviceOf(ctx, 'clientModules')
134
+ if (!service || typeof service.graph !== 'function') return undefined
135
+ return service.graph()
136
+ }),
137
+ packages: readLayer(() => {
138
+ const service = serviceOf(ctx, 'typert')
139
+ if (!service || typeof service.listPackages !== 'function') return undefined
140
+ return service.listPackages()
141
+ }),
142
+ }
143
+ }
144
+
145
+ /** A snapshot is usable only when it is an object with a timestamp. */
146
+ function isSnapshot(value) {
147
+ return value !== null && typeof value === 'object' && Number.isFinite(Number(value.at))
148
+ }
149
+
150
+ /**
151
+ * What did the host STOP seeing since `previous`?
152
+ *
153
+ * Only disappearances count. A plugin the user just installed is normal and is
154
+ * not reported. An empty or unreadable snapshot reports nothing at all, so the
155
+ * very first boot after installing this plugin is silent instead of alarming.
156
+ *
157
+ * @param {object|null} previous
158
+ * @param {object|null} current
159
+ * @returns {{ at: number|null, layers: Record<string, { removed: string[] }>, removedTotal: number, hasDisappearances: boolean }}
160
+ */
161
+ export function compareCapabilities(previous, current) {
162
+ const layers = {}
163
+ for (const layer of WATCH_LAYERS) layers[layer] = { removed: [] }
164
+
165
+ const before = isSnapshot(previous) ? previous : null
166
+ const after = isSnapshot(current) ? current : null
167
+ // No previous snapshot: nothing to compare against. Nothing is "missing".
168
+ if (before === null || after === null) {
169
+ return { at: after === null ? null : Number(after.at), layers, removedTotal: 0, hasDisappearances: false }
170
+ }
171
+
172
+ let removedTotal = 0
173
+ for (const layer of WATCH_LAYERS) {
174
+ const was = before[layer]
175
+ const is = after[layer]
176
+ // Either side unreadable means "no news" for that layer - never an alarm.
177
+ if (Array.isArray(was) !== true || Array.isArray(is) !== true) continue
178
+ const now = new Set(is)
179
+ const removed = was.filter((name) => typeof name === 'string' && now.has(name) !== true).sort()
180
+ layers[layer] = { removed }
181
+ removedTotal += removed.length
182
+ }
183
+
184
+ return {
185
+ at: Number(after.at),
186
+ layers,
187
+ removedTotal,
188
+ hasDisappearances: removedTotal > 0,
189
+ }
190
+ }
191
+
192
+ /**
193
+ * The layers that lost something, as a flat, printable list.
194
+ *
195
+ * Still only a statement of fact ("this name was here and is not any more"); it
196
+ * does not claim the plugin is broken.
197
+ *
198
+ * @param {ReturnType<typeof compareCapabilities>} comparison
199
+ * @returns {Array<{ layer: string, name: string }>}
200
+ */
201
+ export function listDisappearances(comparison) {
202
+ const out = []
203
+ if (comparison === null || typeof comparison !== 'object') return out
204
+ const layers = comparison.layers !== null && typeof comparison.layers === 'object' ? comparison.layers : {}
205
+ for (const layer of WATCH_LAYERS) {
206
+ const entry = layers[layer]
207
+ if (entry === null || typeof entry !== 'object' || Array.isArray(entry.removed) !== true) continue
208
+ for (const name of entry.removed) {
209
+ if (typeof name === 'string') out.push({ layer, name })
210
+ }
211
+ }
212
+ return out
213
+ }