dsh-remote-go 0.2.0 → 0.3.1

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 (135) hide show
  1. package/README.md +11 -3
  2. package/{client.js → dist/client.js} +7144 -8367
  3. package/dist/index.js +4 -0
  4. package/dist/src/admission.js +21 -0
  5. package/dist/src/api.js +380 -0
  6. package/dist/src/atomic-json.js +201 -0
  7. package/dist/src/bootstrap.js +96 -0
  8. package/dist/src/connect/attach.js +21 -0
  9. package/dist/src/connect/bootstrap.js +8 -0
  10. package/dist/src/connect/failures.js +29 -0
  11. package/dist/src/connect/guards.js +36 -0
  12. package/dist/src/connect/launch.js +70 -0
  13. package/dist/src/connect/links.js +54 -0
  14. package/dist/src/connect/ownership.js +139 -0
  15. package/dist/src/connect/pipeline.js +376 -0
  16. package/dist/src/connect/reattach.js +191 -0
  17. package/dist/src/connect/registry.js +10 -0
  18. package/dist/src/connect/repair.js +97 -0
  19. package/dist/src/connect/restart.js +255 -0
  20. package/dist/src/connect/stop.js +164 -0
  21. package/dist/src/connect/transport.js +101 -0
  22. package/dist/src/connect/verify.js +30 -0
  23. package/dist/src/connect/watchdog.js +44 -0
  24. package/dist/src/connect-log.js +137 -0
  25. package/dist/src/connect-progress.js +110 -0
  26. package/dist/src/constants.js +29 -0
  27. package/dist/src/context.js +96 -0
  28. package/dist/src/diagnostics.js +133 -0
  29. package/dist/src/download-jobs.js +221 -0
  30. package/dist/src/errors.js +228 -0
  31. package/dist/src/events.js +326 -0
  32. package/dist/src/host-lifecycle.js +368 -0
  33. package/dist/src/host-registry.js +181 -0
  34. package/dist/src/host-runtime.js +77 -0
  35. package/dist/src/host-view.js +52 -0
  36. package/dist/src/index.js +196 -0
  37. package/dist/src/kernel-adapter.js +388 -0
  38. package/dist/src/key-files.js +39 -0
  39. package/dist/src/known-hosts.js +104 -0
  40. package/dist/src/layout.js +26 -0
  41. package/dist/src/local-download.js +311 -0
  42. package/dist/src/local-ssh.js +23 -0
  43. package/dist/src/local-version.js +87 -0
  44. package/dist/src/node/download.js +157 -0
  45. package/dist/src/node-runtime.js +327 -0
  46. package/dist/src/probe.js +95 -0
  47. package/dist/src/prompts.js +198 -0
  48. package/dist/src/proxy/cookies.js +102 -0
  49. package/dist/src/proxy/headers.js +167 -0
  50. package/dist/src/proxy/http.js +134 -0
  51. package/dist/src/proxy/index.js +121 -0
  52. package/dist/src/proxy/limits.js +6 -0
  53. package/dist/src/proxy/routes.js +32 -0
  54. package/dist/src/proxy/upgrade.js +160 -0
  55. package/dist/src/redact.js +85 -0
  56. package/dist/src/remote-files.js +60 -0
  57. package/dist/src/remote-path.js +82 -0
  58. package/dist/src/remote-proc.js +271 -0
  59. package/dist/src/remote-web/registry.js +107 -0
  60. package/dist/src/remote-web/resolve.js +229 -0
  61. package/dist/src/remote-web/start.js +190 -0
  62. package/dist/src/remote-web/stop.js +74 -0
  63. package/dist/src/request-trust.js +70 -0
  64. package/dist/src/runtime/dispatch.js +200 -0
  65. package/dist/src/runtime/effects.js +81 -0
  66. package/dist/src/runtime/events.js +35 -0
  67. package/dist/src/runtime/fsm.js +70 -0
  68. package/dist/src/runtime/rules.js +480 -0
  69. package/dist/src/secrets.js +119 -0
  70. package/dist/src/service.js +300 -0
  71. package/dist/src/settings.js +143 -0
  72. package/dist/src/ssh-config.js +257 -0
  73. package/dist/src/ssh-failures.js +71 -0
  74. package/dist/src/ssh-port.js +4 -0
  75. package/dist/src/ssh-sftp.js +258 -0
  76. package/dist/src/ssh.js +395 -0
  77. package/dist/src/store.js +174 -0
  78. package/dist/src/tool-shared.js +146 -0
  79. package/dist/src/tools-exec-policy.js +121 -0
  80. package/dist/src/tools-exec.js +307 -0
  81. package/dist/src/tools-report.js +357 -0
  82. package/dist/src/tools.js +337 -0
  83. package/dist/src/tunnel.js +111 -0
  84. package/dist/src/util.js +112 -0
  85. package/dist/src/version.js +108 -0
  86. package/dist/src/watchdog-bus.js +21 -0
  87. package/dist/src/watchdog.js +116 -0
  88. package/index.js +5 -4
  89. package/locale/en.json +1 -1
  90. package/locale/zh.json +1 -1
  91. package/package.json +37 -19
  92. package/src/admission.js +0 -43
  93. package/src/api.js +0 -536
  94. package/src/atomic-json.js +0 -225
  95. package/src/bootstrap.js +0 -113
  96. package/src/connect-log.js +0 -176
  97. package/src/connect-pipeline.js +0 -1900
  98. package/src/connect-progress.js +0 -155
  99. package/src/constants.js +0 -93
  100. package/src/diagnostics.js +0 -160
  101. package/src/download-jobs.js +0 -234
  102. package/src/errors.js +0 -412
  103. package/src/host-lifecycle.js +0 -604
  104. package/src/host-registry.js +0 -217
  105. package/src/host-runtime.js +0 -127
  106. package/src/host-view.js +0 -75
  107. package/src/index.js +0 -206
  108. package/src/key-files.js +0 -54
  109. package/src/known-hosts.js +0 -133
  110. package/src/layout.js +0 -53
  111. package/src/local-download.js +0 -381
  112. package/src/local-ssh.js +0 -27
  113. package/src/local-version.js +0 -88
  114. package/src/node-runtime.js +0 -521
  115. package/src/probe.js +0 -101
  116. package/src/prompts.js +0 -229
  117. package/src/redact.js +0 -68
  118. package/src/remote-files.js +0 -71
  119. package/src/remote-path.js +0 -137
  120. package/src/remote-proc.js +0 -343
  121. package/src/remote-proxy.js +0 -815
  122. package/src/remote-web.js +0 -751
  123. package/src/request-trust.js +0 -89
  124. package/src/secrets.js +0 -109
  125. package/src/service.js +0 -459
  126. package/src/settings.js +0 -163
  127. package/src/ssh-config.js +0 -271
  128. package/src/ssh.js +0 -731
  129. package/src/store.js +0 -193
  130. package/src/tools.js +0 -386
  131. package/src/tunnel.js +0 -132
  132. package/src/util.js +0 -153
  133. package/src/version.js +0 -103
  134. package/src/webapp.js +0 -43
  135. /package/{vendor → dist/vendor}/ssh2.bundle.cjs +0 -0
@@ -0,0 +1,200 @@
1
+ /** The executor half of the `HostRuntime.session` state machine (`fsm.ts` decides, this file stores and performs the
2
+ * effects through `DispatchHost`, hence no service import and no import cycle with the modules that call it back). */
3
+ import { IllegalTransition, isRefusal, transition, writeBack } from './fsm.js';
4
+ import { DEFAULT_IDLE_MINUTES, REMOTE_HEARTBEAT_DEFAULT_SEC, REMOTE_HEARTBEAT_MAX_SEC, REMOTE_HEARTBEAT_MIN_SEC, } from '../constants.js';
5
+ /** The five activity kinds `host-lifecycle.ts#beginLinkWork` may store (`Activity`). */
6
+ const ACTIVITIES = ['connect', 'restart', 'reattach', 'stop', 'repair'];
7
+ /** `linkBusy`'s storage form: the declared type stays the wide `string | null`, and an unknown kind from an older
8
+ * caller means "no one-shot action" (what the token checks assume), so it must not reach the FSM as an activity. */
9
+ export function isActivity(value) {
10
+ return typeof value === 'string' && ACTIVITIES.includes(value);
11
+ }
12
+ /** Read the session block out of the flat runtime: the only adapter between storage and the pure machine. The four
13
+ * liveness fields are not read — `watchdog.ts` owns them and never dispatches, so a read would round-trip the value. */
14
+ export function readSession(runtime) {
15
+ const progress = runtime.progress ?? null;
16
+ return {
17
+ status: runtime.status,
18
+ desired: runtime.desired,
19
+ activity: isActivity(runtime.linkBusy) ? runtime.linkBusy : null,
20
+ phase: runtime.phase,
21
+ progress,
22
+ error: runtime.error,
23
+ warning: runtime.warning,
24
+ warningSource: runtime.warningSource,
25
+ closing: runtime.closing === true,
26
+ connectCancelled: runtime.connectCancelled === true,
27
+ transportLost: runtime.transportLost === true,
28
+ reconnectAttempts: Number(runtime.reconnectAttempts) || 0,
29
+ epoch: Number(runtime.linkToken) || 0,
30
+ };
31
+ }
32
+ /** The scope facts of the two prompt events (`hostkey.*`, `conflict.*`). Their rules read one scope field —
33
+ * `promptsPending` — because a prompt answer is only about the slot it belongs to, and the prompt module has no
34
+ * deps/settings slice: the neutral values below are unconsumed, so a future rule reading one would be a wiring bug. */
35
+ export function promptScopeFacts(runtime) {
36
+ return {
37
+ disposed: false,
38
+ runtimeExists: true,
39
+ linkAlive: runtime.ssh?.connected === true,
40
+ tunnelAlive: runtime.tunnel !== null,
41
+ transportIsCurrent: true,
42
+ promptsPending: runtime.pendingHostKey !== null || runtime.pendingConflict !== null,
43
+ keepAlive: runtime.entry.keepAlive,
44
+ remoteStarted: runtime.remote.pid !== null || runtime.remote.port !== null,
45
+ recoverable: false,
46
+ delays: { reconnectMs: 0, idleMs: 0, heartbeatMs: 0 },
47
+ };
48
+ }
49
+ /** The heartbeat interval the watchdog would use, clamped exactly like `host-lifecycle.ts#remoteHeartbeatIntervalSec`
50
+ * (a pure rule cannot read settings). Duplicated on purpose: importing the lifecycle function would close a cycle. */
51
+ function heartbeatMs(raw) {
52
+ const seconds = Number(raw?.intervalSec);
53
+ if (!Number.isFinite(seconds))
54
+ return REMOTE_HEARTBEAT_DEFAULT_SEC * 1000;
55
+ return Math.min(REMOTE_HEARTBEAT_MAX_SEC, Math.max(REMOTE_HEARTBEAT_MIN_SEC, Math.round(seconds))) * 1000;
56
+ }
57
+ /** One event's scope facts, read from the caller's context; every value is a read, the executor's half of the split. */
58
+ export function scopeFactsFor(ctx, runtime, event) {
59
+ const settings = ctx.settings();
60
+ const linkAlive = runtime.ssh?.connected === true;
61
+ const keepAlive = runtime.entry.keepAlive;
62
+ const idleMinutes = Math.max(1, runtime.entry.idleMinutes ?? DEFAULT_IDLE_MINUTES);
63
+ return {
64
+ disposed: ctx.state.disposed === true,
65
+ runtimeExists: ctx.state.runtimes.has(runtime.id),
66
+ linkAlive,
67
+ tunnelAlive: runtime.tunnel !== null,
68
+ transportIsCurrent: event.type === 'transport.closed' ? runtime.ssh === event.transport || runtime.jumpSsh === event.transport : true,
69
+ promptsPending: runtime.pendingHostKey !== null || runtime.pendingConflict !== null,
70
+ keepAlive,
71
+ remoteStarted: runtime.remote.pid !== null || runtime.remote.port !== null,
72
+ recoverable: runtime.status === 'error' && runtime.error?.code === 'remote-unreachable' && linkAlive && runtime.facts !== null,
73
+ delays: {
74
+ reconnectMs: Number(settings.autoReconnect.baseDelayMs) || 0,
75
+ idleMs: idleMinutes * 60_000,
76
+ heartbeatMs: heartbeatMs(settings.remoteHeartbeat),
77
+ },
78
+ };
79
+ }
80
+ /** Build the read-only scope of one event. `promptsPending` is event-specific on purpose: a `hostkey.*` answer goes
81
+ * stale when the host-key slot is gone, a `conflict.*` answer when the conflict slot is, and one shared "any prompt"
82
+ * flag would let a conflict answer satisfy a host-key rule. */
83
+ export function scopeFor(event, runtime, facts) {
84
+ const promptsPending = event.type === 'hostkey.pending' || event.type === 'hostkey.answered' || event.type === 'hostkey.timeout'
85
+ ? runtime.pendingHostKey !== null
86
+ : event.type === 'conflict.pending' || event.type === 'conflict.answered'
87
+ ? runtime.pendingConflict !== null
88
+ : runtime.pendingHostKey !== null || runtime.pendingConflict !== null;
89
+ return {
90
+ disposed: facts.disposed,
91
+ runtimeExists: facts.runtimeExists,
92
+ linkAlive: facts.linkAlive,
93
+ tunnelAlive: facts.tunnelAlive,
94
+ promptsPending,
95
+ transportIsCurrent: facts.transportIsCurrent,
96
+ hostId: runtime.id,
97
+ keepAlive: facts.keepAlive,
98
+ remoteStarted: facts.remoteStarted,
99
+ recoverable: facts.recoverable,
100
+ delays: facts.delays,
101
+ };
102
+ }
103
+ /** What a refusal means for this caller, as the three-tier rule states it. */
104
+ function reportRefusal(host, runtime, event, reason, caller) {
105
+ if (caller === 'script')
106
+ throw new IllegalTransition(reason, event.type);
107
+ // The idle timer firing for a host that is wanted again is the normal race: it keeps today's exact debug wording.
108
+ if (event.type === 'idle.elapsed') {
109
+ host.log(runtime, 'debug', 'idle stop skipped: the host is connected again');
110
+ return;
111
+ }
112
+ if (caller === 'silent')
113
+ return;
114
+ // A callback that lost the race is a warning; a timer (reconnected host, stale retry) must not warn.
115
+ host.log(runtime, caller === 'timer' ? 'debug' : 'warn', `state transition refused (${event.type}: ${reason})`);
116
+ }
117
+ /** The teardown options one event really uses: the `connect.cancelled` row says `{stopRemote:false, keepRemote:true}`,
118
+ * but today's cancel branch releases like a failure (`stopRemote` only under the `stop` policy), and a cancellation
119
+ * after the remote started under `keepAlive: 'stop'` must still stop it, so behavior freeze keeps the failure's. */
120
+ function teardownOptions(effect, event, scope) {
121
+ if (event.type !== 'connect.cancelled')
122
+ return effect.options;
123
+ return {
124
+ stopRemote: scope.remoteStarted && scope.keepAlive === 'stop',
125
+ keepRemote: scope.remoteStarted && scope.keepAlive !== 'stop',
126
+ };
127
+ }
128
+ /** Execute the effects of one accepted transition, in the order the rule lists them. */
129
+ async function runEffects(host, runtime, event, scope, effects, options) {
130
+ for (const effect of effects) {
131
+ switch (effect.type) {
132
+ case 'emit':
133
+ if (options.emit !== false)
134
+ host.emit();
135
+ break;
136
+ case 'flushLogs':
137
+ host.flushLogs(runtime);
138
+ break;
139
+ case 'log':
140
+ host.log(runtime, effect.level, effect.message);
141
+ break;
142
+ case 'logFailure':
143
+ host.logFailure(runtime, effect.phase, effect.label, effect.view);
144
+ break;
145
+ case 'arm':
146
+ if (effect.timer === 'reconnect') {
147
+ // Single source: only `reconnect.scheduled` owns the retry timer; one created here would run a second loop.
148
+ if (event.type === 'reconnect.scheduled')
149
+ options.armReconnect?.(effect.ms);
150
+ }
151
+ else {
152
+ host.arm(runtime, effect.timer, effect.ms);
153
+ }
154
+ break;
155
+ case 'disarm':
156
+ host.disarm(runtime, effect.timer);
157
+ break;
158
+ case 'settlePrompts':
159
+ host.settlePrompts(runtime);
160
+ break;
161
+ case 'closeLink':
162
+ if (options.keepLink !== true)
163
+ host.closeLink(runtime);
164
+ break;
165
+ case 'teardown':
166
+ if (options.keepLink !== true) {
167
+ const chosen = options.teardown ?? teardownOptions(effect, event, scope);
168
+ await host.teardown(runtime, chosen, event);
169
+ }
170
+ break;
171
+ case 'apply':
172
+ // `mark-detached` describes a released link: a caller keeping the link must not publish `uiReady:false`.
173
+ if (options.keepLink === true && effect.patch.kind === 'mark-detached')
174
+ break;
175
+ host.apply(runtime, effect.patch);
176
+ break;
177
+ case 'run':
178
+ if (options.run === undefined) {
179
+ // Only request events carry `run` and every entry point supplies it, so reaching this is a wiring bug.
180
+ throw new Error(`dispatch: the ${event.type} event needs a run hook`);
181
+ }
182
+ options.run(effect.activity, effect.token);
183
+ break;
184
+ }
185
+ }
186
+ }
187
+ /** Apply one event to a runtime's session block and perform its effects. The state write happens before the first
188
+ * effect, so a script resuming after an `await` sees the table's state (the connect catch classifies from it). */
189
+ export async function dispatch(host, runtime, event, facts, options = {}) {
190
+ const caller = options.caller ?? 'script';
191
+ const scope = scopeFor(event, runtime, facts);
192
+ const result = transition(readSession(runtime), event, scope);
193
+ if (isRefusal(result)) {
194
+ reportRefusal(host, runtime, event, result.reason, caller);
195
+ return result;
196
+ }
197
+ writeBack(runtime, result.state);
198
+ await runEffects(host, runtime, event, scope, result.effects, options);
199
+ return result;
200
+ }
@@ -0,0 +1,81 @@
1
+ import { cancelConflict, cancelHostKeyPrompt } from '../prompts.js';
2
+ import { cancelIdleStop, cancelReconnect, closeTunnel, releaseRuntimeForShutdown, scheduleIdleStop, stopRemoteHeartbeat, teardownRuntime, } from '../host-lifecycle.js';
3
+ import { startRemoteHeartbeat } from '../watchdog.js';
4
+ /** `mark-attached` also resets the link block's repair budget: attaching heals the link as well as the UI, and
5
+ * `markConnected` owns both halves of that same fact. */
6
+ function markAttached(runtime) {
7
+ runtime.remote.uiReady = true;
8
+ runtime.remote.viewRev = (Number(runtime.remote.viewRev) || 0) + 1;
9
+ runtime.tunnelRepairFailures = 0;
10
+ runtime.nextTunnelRepairAt = 0;
11
+ }
12
+ /** Build the effect owner for one caller; the returned object is what `dispatch()` performs. */
13
+ export function createRuntimeEffects(ctx) {
14
+ return {
15
+ emit: () => ctx.emitState(),
16
+ log: (runtime, level, message) => ctx.progress.log(runtime, level, message),
17
+ logFailure: (runtime, phase, label, view) => ctx.progress.logFailure(runtime, view ?? runtime.error, { phase, label }),
18
+ arm: (runtime, timer) => {
19
+ if (timer === 'idle') {
20
+ scheduleIdleStop(ctx, runtime);
21
+ return;
22
+ }
23
+ startRemoteHeartbeat(ctx, runtime);
24
+ },
25
+ disarm: (runtime, timer) => {
26
+ if (timer === 'reconnect')
27
+ cancelReconnect(runtime);
28
+ else if (timer === 'idle')
29
+ cancelIdleStop(runtime);
30
+ else
31
+ stopRemoteHeartbeat(runtime);
32
+ },
33
+ settlePrompts: (runtime) => {
34
+ // The teardown path does the same pair first; both are idempotent by contract.
35
+ cancelHostKeyPrompt(runtime);
36
+ cancelConflict(runtime);
37
+ },
38
+ closeLink: (runtime) => {
39
+ void closeTunnel(runtime);
40
+ },
41
+ teardown: async (runtime, options) => {
42
+ if (options.shutdown === true) {
43
+ await releaseRuntimeForShutdown(ctx, runtime, options.stopRemote === true);
44
+ return;
45
+ }
46
+ await teardownRuntime(ctx, runtime, {
47
+ stopRemote: options.stopRemote === true,
48
+ ...(options.keepRemote === true ? { keepRemote: true } : {}),
49
+ ...(options.keepSshForIdle === true ? { keepSshForIdle: true } : {}),
50
+ });
51
+ },
52
+ apply: (runtime, patch) => {
53
+ if (patch.kind === 'mark-attached') {
54
+ markAttached(runtime);
55
+ return;
56
+ }
57
+ if (patch.kind === 'mark-detached') {
58
+ runtime.remote.uiReady = false;
59
+ runtime.remote.localPort = null;
60
+ return;
61
+ }
62
+ if (patch.kind === 'mark-ui-up') {
63
+ runtime.remote.uiReady = true;
64
+ return;
65
+ }
66
+ if (patch.kind === 'clear-conflict') {
67
+ runtime.conflict = null;
68
+ return;
69
+ }
70
+ if (patch.kind === 'publish-active-host') {
71
+ ctx.state.activeHostId = patch.hostId;
72
+ return;
73
+ }
74
+ // `restore-stop`: the two `remote` fields a failed stop has to put back.
75
+ runtime.remote.uiReady = patch.uiReady === true;
76
+ runtime.remote.localPort = patch.localPort;
77
+ },
78
+ // `connect-progress` owns the coalescing flush timer and `log()` already scheduled it: nothing to do here.
79
+ flushLogs: () => { },
80
+ };
81
+ }
@@ -0,0 +1,35 @@
1
+ /** The 31 event names, in table order (minus the three dead `remote.*`). */
2
+ export const EVENT_TYPES = [
3
+ 'connect.requested',
4
+ 'step.entered',
5
+ 'step.done',
6
+ 'transport.ready',
7
+ 'transport.closed',
8
+ 'hostkey.pending',
9
+ 'hostkey.answered',
10
+ 'hostkey.timeout',
11
+ 'conflict.pending',
12
+ 'conflict.answered',
13
+ 'connect.succeeded',
14
+ 'connect.cancelled',
15
+ 'connect.failed',
16
+ 'disconnect.requested',
17
+ 'idle.elapsed',
18
+ 'repair.requested',
19
+ 'repair.finished',
20
+ 'restart.requested',
21
+ 'restart.resumed',
22
+ 'restart.failed',
23
+ 'stop.requested',
24
+ 'stop.refused',
25
+ 'stop.succeeded',
26
+ 'reattach.requested',
27
+ 'reattach.failed',
28
+ 'reconnect.scheduled',
29
+ 'reconnect.gaveup',
30
+ 'reconnect.skipped',
31
+ 'link.cancelled',
32
+ 'shutdown',
33
+ 'host.removed',
34
+ ];
35
+ export const EVENT_TYPE_PARITY = true;
@@ -0,0 +1,70 @@
1
+ import { TABLE } from './rules.js';
2
+ /** The exhaustive list of refusals, for tests that assert "no third outcome": every pair is `ok` or a refusal. */
3
+ export const REFUSALS = [
4
+ 'illegal-transition',
5
+ 'unknown-event',
6
+ 'already-connecting',
7
+ 'already-restarting',
8
+ 'already-connected',
9
+ 'not-connected',
10
+ 'not-recoverable',
11
+ 'busy',
12
+ 'prompt-not-pending',
13
+ 'stale-prompt',
14
+ ];
15
+ export function isRefusal(result) {
16
+ return result.ok === false;
17
+ }
18
+ /** Never reaches `ErrorView`: only a script that ran into a refusal treats it as a bug. */
19
+ export class IllegalTransition extends Error {
20
+ code = 'illegal-transition';
21
+ eventType;
22
+ constructor(reason, eventType) {
23
+ super(`illegal transition: ${eventType} refused (${reason})`);
24
+ this.name = 'IllegalTransition';
25
+ this.eventType = eventType;
26
+ }
27
+ }
28
+ /** "The user still wants this link, and this process still owns the runtime." */
29
+ export function stillWanted(state, scope) {
30
+ return state.desired === 'connected' && !state.closing && scope.runtimeExists && !scope.disposed;
31
+ }
32
+ /** `state.epoch === token`: a one-shot action may write. */
33
+ export function isActivityCurrent(state, token) {
34
+ return state.epoch === token;
35
+ }
36
+ /** Put the session block of a runtime back to the state the machine produced. */
37
+ export function writeBack(runtime, state) {
38
+ runtime.status = state.status;
39
+ runtime.desired = state.desired;
40
+ runtime.linkBusy = state.activity;
41
+ runtime.linkToken = state.epoch;
42
+ runtime.phase = state.phase;
43
+ runtime.progress = state.progress;
44
+ runtime.error = state.error;
45
+ runtime.warning = state.warning;
46
+ runtime.warningSource = state.warningSource;
47
+ runtime.closing = state.closing;
48
+ runtime.connectCancelled = state.connectCancelled;
49
+ runtime.transportLost = state.transportLost;
50
+ runtime.reconnectAttempts = state.reconnectAttempts;
51
+ // The liveness fields (`remoteFailures`/`remoteEpisode`/`episodeSource`/`episodeCooldownUntil`) are deliberately
52
+ // absent: `watchdog.ts` is their only writer.
53
+ }
54
+ /** Look one rule up by its event name, generic on the key so the returned rule keeps the *narrow* payload of that row:
55
+ * `TABLE[event.type]` in `transition` would otherwise be a union of 31 rule types with 31 parameter types, which
56
+ * TypeScript cannot correlate with the event it was indexed by. */
57
+ function ruleFor(type) {
58
+ return TABLE[type];
59
+ }
60
+ /** Compute one event's next state and effects: never mutates, never throws, reads only its three arguments. */
61
+ export function transition(state, event, scope) {
62
+ // The table covers the 31 declared names, so a typed caller cannot miss; `unknown-event` only guards a JS caller.
63
+ const rule = ruleFor(event.type);
64
+ if (rule === undefined)
65
+ return { ok: false, reason: 'unknown-event', state };
66
+ const verdict = rule.guard(state, event, scope);
67
+ if (verdict !== true)
68
+ return { ok: false, reason: verdict, state };
69
+ return { ok: true, state: rule.apply(state, event, scope), effects: rule.effects(state, event, scope) };
70
+ }