dsh-remote-go 0.3.0 → 0.3.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.
- package/README.md +1 -0
- package/dist/client.js +1348 -2095
- package/dist/src/admission.js +3 -28
- package/dist/src/api.js +37 -99
- package/dist/src/atomic-json.js +32 -66
- package/dist/src/bootstrap.js +17 -27
- package/dist/src/connect/attach.js +3 -25
- package/dist/src/connect/bootstrap.js +2 -8
- package/dist/src/connect/failures.js +3 -18
- package/dist/src/connect/guards.js +13 -8
- package/dist/src/connect/launch.js +11 -21
- package/dist/src/connect/links.js +11 -27
- package/dist/src/connect/ownership.js +33 -114
- package/dist/src/connect/pipeline.js +62 -159
- package/dist/src/connect/reattach.js +26 -44
- package/dist/src/connect/registry.js +0 -1
- package/dist/src/connect/repair.js +8 -37
- package/dist/src/connect/restart.js +36 -97
- package/dist/src/connect/stop.js +16 -50
- package/dist/src/connect/transport.js +10 -18
- package/dist/src/connect/verify.js +3 -12
- package/dist/src/connect/watchdog.js +6 -38
- package/dist/src/connect-log.js +16 -41
- package/dist/src/connect-progress.js +13 -45
- package/dist/src/constants.js +18 -43
- package/dist/src/context.js +13 -32
- package/dist/src/diagnostics.js +56 -25
- package/dist/src/download-jobs.js +11 -20
- package/dist/src/errors.js +175 -190
- package/dist/src/events.js +20 -84
- package/dist/src/host-lifecycle.js +69 -177
- package/dist/src/host-registry.js +27 -78
- package/dist/src/host-runtime.js +0 -9
- package/dist/src/host-view.js +6 -29
- package/dist/src/index.js +15 -35
- package/dist/src/kernel-adapter.js +34 -80
- package/dist/src/key-files.js +10 -23
- package/dist/src/known-hosts.js +13 -34
- package/dist/src/layout.js +3 -20
- package/dist/src/local-download.js +94 -163
- package/dist/src/local-ssh.js +12 -19
- package/dist/src/local-version.js +35 -49
- package/dist/src/node/download.js +1 -4
- package/dist/src/node-runtime.js +19 -38
- package/dist/src/probe.js +6 -7
- package/dist/src/prompts.js +56 -128
- package/dist/src/proxy/cookies.js +26 -60
- package/dist/src/proxy/headers.js +30 -125
- package/dist/src/proxy/http.js +33 -41
- package/dist/src/proxy/index.js +20 -34
- package/dist/src/proxy/limits.js +4 -6
- package/dist/src/proxy/routes.js +10 -17
- package/dist/src/proxy/upgrade.js +27 -42
- package/dist/src/redact.js +14 -33
- package/dist/src/remote-files.js +9 -21
- package/dist/src/remote-path.js +21 -62
- package/dist/src/remote-proc.js +35 -103
- package/dist/src/remote-web/registry.js +10 -50
- package/dist/src/remote-web/resolve.js +23 -104
- package/dist/src/remote-web/start.js +27 -64
- package/dist/src/remote-web/stop.js +6 -25
- package/dist/src/request-trust.js +15 -32
- package/dist/src/runtime/dispatch.js +33 -143
- package/dist/src/runtime/effects.js +7 -17
- package/dist/src/runtime/events.js +1 -1
- package/dist/src/runtime/fsm.js +11 -61
- package/dist/src/runtime/rules.js +121 -199
- package/dist/src/secrets.js +15 -38
- package/dist/src/service.js +22 -128
- package/dist/src/settings.js +41 -42
- package/dist/src/ssh-config.js +22 -41
- package/dist/src/ssh-failures.js +23 -74
- package/dist/src/ssh-sftp.js +47 -86
- package/dist/src/ssh.js +99 -179
- package/dist/src/store.js +31 -48
- package/dist/src/tool-shared.js +17 -52
- package/dist/src/tools-exec-policy.js +33 -79
- package/dist/src/tools-exec.js +87 -151
- package/dist/src/tools-report.js +100 -167
- package/dist/src/tools.js +59 -133
- package/dist/src/tunnel.js +9 -27
- package/dist/src/util.js +19 -56
- package/dist/src/version.js +6 -12
- package/dist/src/watchdog-bus.js +0 -8
- package/dist/src/watchdog.js +7 -47
- package/package.json +3 -3
|
@@ -1,62 +1,16 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* `fsm.ts` decides *what* the session block becomes; this file turns that decision into the
|
|
5
|
-
* block's storage form and performs the effects the rule asked for (design-arch §2.4.2/§7.6).
|
|
6
|
-
* It is deliberately free of service imports: every action arrives through the `DispatchHost`
|
|
7
|
-
* the calling module builds from its own context, so `dispatch.ts` creates no import cycle with
|
|
8
|
-
* `host-lifecycle.ts`/`prompts.ts` (both of which call back into `dispatch`).
|
|
9
|
-
*
|
|
10
|
-
* Wiring rules this executor implements (from the design and the P2b hand-off):
|
|
11
|
-
*
|
|
12
|
-
* - **One transition, then one write.** The state is read with `readSession`, computed by the
|
|
13
|
-
* pure `transition`, and written back with `writeBack` before any effect runs. Effects never
|
|
14
|
-
* mutate the session block; they only act on the rest of the runtime and publish.
|
|
15
|
-
* - **`emit` is the only publication.** `options.emit === false` suppresses it for the callers
|
|
16
|
-
* whose old code published later in the same routine (the connect/restart/re-attach catches,
|
|
17
|
-
* which emit after deciding whether a retry is armed), so the frame sequence stays what it was.
|
|
18
|
-
* - **`arm(reconnect)` has a single source.** `connect.failed`/`transport.closed` ask for a retry
|
|
19
|
-
* timer with the *base* delay, but the randomized delay, the budget and the settings switch live
|
|
20
|
-
* in `host-lifecycle.ts#scheduleReconnect`. Creating a timer here would run two retry loops, so
|
|
21
|
-
* only `reconnect.scheduled` — the event `scheduleReconnect` dispatches once it has decided —
|
|
22
|
-
* may arm it. Every other `arm(reconnect)` means "a retry makes sense" and is left to the
|
|
23
|
-
* caller's policy (`scheduleConnectRetry`), never turned into a second timer.
|
|
24
|
-
* - **Refusals are graded by caller.** A one-shot script treats a refusal as a bug
|
|
25
|
-
* (`IllegalTransition`); a transport/proxy callback only logs; a timer logs at debug (the idle
|
|
26
|
-
* stop in particular must not warn: a host that is connected again is the normal race, not a
|
|
27
|
-
* fault).
|
|
28
|
-
* - **`connect.cancelled`'s teardown options are the failure's, not the table's.** The §2.4.2 row
|
|
29
|
-
* says `{stopRemote:false, keepRemote:true}`, but today's cancel branch releases with
|
|
30
|
-
* `stopRemote: remoteStarted && keepAlive === 'stop'` and `keepRemote: remoteStarted &&
|
|
31
|
-
* keepAlive !== 'stop'`; behavior freeze wins (reported as a deviation).
|
|
32
|
-
*/
|
|
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). */
|
|
33
3
|
import { IllegalTransition, isRefusal, transition, writeBack } from './fsm.js';
|
|
34
4
|
import { DEFAULT_IDLE_MINUTES, REMOTE_HEARTBEAT_DEFAULT_SEC, REMOTE_HEARTBEAT_MAX_SEC, REMOTE_HEARTBEAT_MIN_SEC, } from '../constants.js';
|
|
35
|
-
/* ── storage form ↔ pure state ───────────────────────────────────────────────────────────── */
|
|
36
5
|
/** The five activity kinds `host-lifecycle.ts#beginLinkWork` may store (`Activity`). */
|
|
37
6
|
const ACTIVITIES = ['connect', 'restart', 'reattach', 'stop', 'repair'];
|
|
38
|
-
/**
|
|
39
|
-
*
|
|
40
|
-
* (§7.4), so a value written by an older caller must not be handed to the FSM as an activity it
|
|
41
|
-
* does not know: an unknown kind means "no one-shot action", which is what the token checks
|
|
42
|
-
* already assume.
|
|
43
|
-
* @param value - the stored `linkBusy` value.
|
|
44
|
-
* @returns whether it is one of the five activities the machine models.
|
|
45
|
-
*/
|
|
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. */
|
|
46
9
|
export function isActivity(value) {
|
|
47
10
|
return typeof value === 'string' && ACTIVITIES.includes(value);
|
|
48
11
|
}
|
|
49
|
-
/**
|
|
50
|
-
*
|
|
51
|
-
* the only adapter between storage and the pure machine).
|
|
52
|
-
*
|
|
53
|
-
* The four liveness fields (`remoteEpisode`/`episodeSource`/`episodeCooldownUntil`/`remoteFailures`)
|
|
54
|
-
* are **not** read here: `watchdog.ts` owns them and never dispatches an event, so a read would only
|
|
55
|
-
* round-trip the same value back. They used to be read for the dead `remote.*` transitions, which
|
|
56
|
-
* nothing dispatched.
|
|
57
|
-
* @param runtime - the session block of one host.
|
|
58
|
-
* @returns the pure session state.
|
|
59
|
-
*/
|
|
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. */
|
|
60
14
|
export function readSession(runtime) {
|
|
61
15
|
const progress = runtime.progress ?? null;
|
|
62
16
|
return {
|
|
@@ -75,30 +29,9 @@ export function readSession(runtime) {
|
|
|
75
29
|
epoch: Number(runtime.linkToken) || 0,
|
|
76
30
|
};
|
|
77
31
|
}
|
|
78
|
-
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* arrives in the scope). Duplicated here on purpose: importing the lifecycle function would make
|
|
82
|
-
* `dispatch.ts` depend on a module that depends on `dispatch.ts`.
|
|
83
|
-
* @param raw - the configured `remoteHeartbeat` setting.
|
|
84
|
-
* @returns the interval in milliseconds.
|
|
85
|
-
*/
|
|
86
|
-
function heartbeatMs(raw) {
|
|
87
|
-
const seconds = Number(raw?.intervalSec);
|
|
88
|
-
if (!Number.isFinite(seconds))
|
|
89
|
-
return REMOTE_HEARTBEAT_DEFAULT_SEC * 1000;
|
|
90
|
-
return Math.min(REMOTE_HEARTBEAT_MAX_SEC, Math.max(REMOTE_HEARTBEAT_MIN_SEC, Math.round(seconds))) * 1000;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* The scope facts of the two prompt events (`hostkey.*`, `conflict.*`).
|
|
94
|
-
*
|
|
95
|
-
* Their rules read exactly one scope field — `promptsPending` — because a prompt answer is only
|
|
96
|
-
* about the slot it belongs to. The prompt module has no deps/settings slice (design-arch §1.2
|
|
97
|
-
* keeps it narrow), so the unrelated facts are the neutral values: none of these rules consumes
|
|
98
|
-
* them, and a future rule that tried to would be a wiring bug the tests would catch.
|
|
99
|
-
* @param runtime - the host runtime the prompt belongs to.
|
|
100
|
-
* @returns the facts `dispatch()` freezes for a prompt event.
|
|
101
|
-
*/
|
|
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. */
|
|
102
35
|
export function promptScopeFacts(runtime) {
|
|
103
36
|
return {
|
|
104
37
|
disposed: false,
|
|
@@ -113,14 +46,15 @@ export function promptScopeFacts(runtime) {
|
|
|
113
46
|
delays: { reconnectMs: 0, idleMs: 0, heartbeatMs: 0 },
|
|
114
47
|
};
|
|
115
48
|
}
|
|
116
|
-
/**
|
|
117
|
-
*
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
*
|
|
122
|
-
|
|
123
|
-
|
|
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. */
|
|
124
58
|
export function scopeFactsFor(ctx, runtime, event) {
|
|
125
59
|
const settings = ctx.settings();
|
|
126
60
|
const linkAlive = runtime.ssh?.connected === true;
|
|
@@ -143,17 +77,9 @@ export function scopeFactsFor(ctx, runtime, event) {
|
|
|
143
77
|
},
|
|
144
78
|
};
|
|
145
79
|
}
|
|
146
|
-
/**
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
* `promptsPending` is event-specific on purpose: a `hostkey.*` answer is stale once the host-key
|
|
150
|
-
* slot is gone, a `conflict.*` answer once the conflict slot is gone. One shared "any prompt"
|
|
151
|
-
* flag would let a conflict answer satisfy a host-key rule.
|
|
152
|
-
* @param event - the event about to be applied (its payload names the transport, if any).
|
|
153
|
-
* @param runtime - the host runtime (only for the identity fields the patches need).
|
|
154
|
-
* @param facts - the caller's read of what the context can see.
|
|
155
|
-
* @returns the frozen scope the pure machine reads.
|
|
156
|
-
*/
|
|
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. */
|
|
157
83
|
export function scopeFor(event, runtime, facts) {
|
|
158
84
|
const promptsPending = event.type === 'hostkey.pending' || event.type === 'hostkey.answered' || event.type === 'hostkey.timeout'
|
|
159
85
|
? runtime.pendingHostKey !== null
|
|
@@ -174,36 +100,23 @@ export function scopeFor(event, runtime, facts) {
|
|
|
174
100
|
delays: facts.delays,
|
|
175
101
|
};
|
|
176
102
|
}
|
|
177
|
-
|
|
178
|
-
/** What a refusal means for this caller, as the design's three-tier rule states it. */
|
|
103
|
+
/** What a refusal means for this caller, as the three-tier rule states it. */
|
|
179
104
|
function reportRefusal(host, runtime, event, reason, caller) {
|
|
180
105
|
if (caller === 'script')
|
|
181
106
|
throw new IllegalTransition(reason, event.type);
|
|
182
|
-
// The idle timer firing for a host that is wanted again is the normal race
|
|
183
|
-
// keeps today's debug line (and its exact wording), which is what the operator's log shows.
|
|
107
|
+
// The idle timer firing for a host that is wanted again is the normal race: it keeps today's exact debug wording.
|
|
184
108
|
if (event.type === 'idle.elapsed') {
|
|
185
109
|
host.log(runtime, 'debug', 'idle stop skipped: the host is connected again');
|
|
186
110
|
return;
|
|
187
111
|
}
|
|
188
112
|
if (caller === 'silent')
|
|
189
113
|
return;
|
|
190
|
-
// A callback that lost the race is a warning; a timer
|
|
191
|
-
// timer, a retry a user disconnect made stale) and must not warn.
|
|
114
|
+
// A callback that lost the race is a warning; a timer (reconnected host, stale retry) must not warn.
|
|
192
115
|
host.log(runtime, caller === 'timer' ? 'debug' : 'warn', `state transition refused (${event.type}: ${reason})`);
|
|
193
116
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* §2.4.2's `connect.cancelled` row says `{stopRemote:false, keepRemote:true}`; today's cancel
|
|
199
|
-
* branch releases exactly like a failure (`stopRemote` only under the `stop` keep-alive policy,
|
|
200
|
-
* `keepRemote` otherwise). Behavior freeze keeps the failure's options — a cancellation after the
|
|
201
|
-
* remote was started under `keepAlive: 'stop'` must still stop it.
|
|
202
|
-
* @param effect - the rule's teardown effect.
|
|
203
|
-
* @param event - the event being executed.
|
|
204
|
-
* @param scope - the read-only facts (remote started? which keep-alive policy?).
|
|
205
|
-
* @returns the options to hand to the teardown path.
|
|
206
|
-
*/
|
|
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. */
|
|
207
120
|
function teardownOptions(effect, event, scope) {
|
|
208
121
|
if (event.type !== 'connect.cancelled')
|
|
209
122
|
return effect.options;
|
|
@@ -212,15 +125,7 @@ function teardownOptions(effect, event, scope) {
|
|
|
212
125
|
keepRemote: scope.remoteStarted && scope.keepAlive !== 'stop',
|
|
213
126
|
};
|
|
214
127
|
}
|
|
215
|
-
/**
|
|
216
|
-
* Execute the effects of one accepted transition, in the order the rule lists them.
|
|
217
|
-
* @param host - the effect owner bound to this caller's context.
|
|
218
|
-
* @param runtime - the host runtime the transition was computed for.
|
|
219
|
-
* @param event - the event that produced the effects.
|
|
220
|
-
* @param scope - the read-only facts of that event.
|
|
221
|
-
* @param effects - the rule's effect list.
|
|
222
|
-
* @param options - per-call hooks.
|
|
223
|
-
*/
|
|
128
|
+
/** Execute the effects of one accepted transition, in the order the rule lists them. */
|
|
224
129
|
async function runEffects(host, runtime, event, scope, effects, options) {
|
|
225
130
|
for (const effect of effects) {
|
|
226
131
|
switch (effect.type) {
|
|
@@ -239,7 +144,7 @@ async function runEffects(host, runtime, event, scope, effects, options) {
|
|
|
239
144
|
break;
|
|
240
145
|
case 'arm':
|
|
241
146
|
if (effect.timer === 'reconnect') {
|
|
242
|
-
// Single source:
|
|
147
|
+
// Single source: only `reconnect.scheduled` owns the retry timer; one created here would run a second loop.
|
|
243
148
|
if (event.type === 'reconnect.scheduled')
|
|
244
149
|
options.armReconnect?.(effect.ms);
|
|
245
150
|
}
|
|
@@ -264,16 +169,14 @@ async function runEffects(host, runtime, event, scope, effects, options) {
|
|
|
264
169
|
}
|
|
265
170
|
break;
|
|
266
171
|
case 'apply':
|
|
267
|
-
// `mark-detached` describes a released link
|
|
268
|
-
// publish `uiReady:false`/`localPort:null` for a listener that is still serving.
|
|
172
|
+
// `mark-detached` describes a released link: a caller keeping the link must not publish `uiReady:false`.
|
|
269
173
|
if (options.keepLink === true && effect.patch.kind === 'mark-detached')
|
|
270
174
|
break;
|
|
271
175
|
host.apply(runtime, effect.patch);
|
|
272
176
|
break;
|
|
273
177
|
case 'run':
|
|
274
178
|
if (options.run === undefined) {
|
|
275
|
-
// Only
|
|
276
|
-
// branch is a wiring bug, not a runtime condition.
|
|
179
|
+
// Only request events carry `run` and every entry point supplies it, so reaching this is a wiring bug.
|
|
277
180
|
throw new Error(`dispatch: the ${event.type} event needs a run hook`);
|
|
278
181
|
}
|
|
279
182
|
options.run(effect.activity, effect.token);
|
|
@@ -281,21 +184,8 @@ async function runEffects(host, runtime, event, scope, effects, options) {
|
|
|
281
184
|
}
|
|
282
185
|
}
|
|
283
186
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
* Apply one event to a runtime's session block and perform its effects.
|
|
287
|
-
*
|
|
288
|
-
* The state write happens before the first effect, so a script that resumes after an `await`
|
|
289
|
-
* inside an effect list sees the state the table produced (the connect catch depends on this to
|
|
290
|
-
* classify a failure).
|
|
291
|
-
* @param host - the effects, bound to the caller's service context.
|
|
292
|
-
* @param runtime - the host runtime.
|
|
293
|
-
* @param event - one of the 31 events.
|
|
294
|
-
* @param facts - the read-only scope facts.
|
|
295
|
-
* @param options - caller semantics and the per-call effect hooks.
|
|
296
|
-
* @returns the transition result: `ok:true` after the effects ran, or the refusal (state unchanged).
|
|
297
|
-
* @throws IllegalTransition only for a `script` caller, i.e. inside a one-shot action.
|
|
298
|
-
*/
|
|
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). */
|
|
299
189
|
export async function dispatch(host, runtime, event, facts, options = {}) {
|
|
300
190
|
const caller = options.caller ?? 'script';
|
|
301
191
|
const scope = scopeFor(event, runtime, facts);
|
|
@@ -1,29 +1,21 @@
|
|
|
1
1
|
import { cancelConflict, cancelHostKeyPrompt } from '../prompts.js';
|
|
2
2
|
import { cancelIdleStop, cancelReconnect, closeTunnel, releaseRuntimeForShutdown, scheduleIdleStop, stopRemoteHeartbeat, teardownRuntime, } from '../host-lifecycle.js';
|
|
3
3
|
import { startRemoteHeartbeat } from '../watchdog.js';
|
|
4
|
-
/**
|
|
5
|
-
* `
|
|
6
|
-
* halves of the same fact, and until `connect/link.ts#markAttached` exists (P2 step 7) this patch
|
|
7
|
-
* is where the link half lands.
|
|
8
|
-
* @param runtime - the host runtime being attached.
|
|
9
|
-
*/
|
|
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. */
|
|
10
6
|
function markAttached(runtime) {
|
|
11
7
|
runtime.remote.uiReady = true;
|
|
12
8
|
runtime.remote.viewRev = (Number(runtime.remote.viewRev) || 0) + 1;
|
|
13
9
|
runtime.tunnelRepairFailures = 0;
|
|
14
10
|
runtime.nextTunnelRepairAt = 0;
|
|
15
11
|
}
|
|
16
|
-
/**
|
|
17
|
-
* Build the effect owner for one caller.
|
|
18
|
-
* @param ctx - the reporter, and the state/deps a teardown needs.
|
|
19
|
-
* @returns the actions `dispatch()` performs.
|
|
20
|
-
*/
|
|
12
|
+
/** Build the effect owner for one caller; the returned object is what `dispatch()` performs. */
|
|
21
13
|
export function createRuntimeEffects(ctx) {
|
|
22
14
|
return {
|
|
23
15
|
emit: () => ctx.emitState(),
|
|
24
16
|
log: (runtime, level, message) => ctx.progress.log(runtime, level, message),
|
|
25
17
|
logFailure: (runtime, phase, label, view) => ctx.progress.logFailure(runtime, view ?? runtime.error, { phase, label }),
|
|
26
|
-
arm: (runtime, timer
|
|
18
|
+
arm: (runtime, timer) => {
|
|
27
19
|
if (timer === 'idle') {
|
|
28
20
|
scheduleIdleStop(ctx, runtime);
|
|
29
21
|
return;
|
|
@@ -39,7 +31,7 @@ export function createRuntimeEffects(ctx) {
|
|
|
39
31
|
stopRemoteHeartbeat(runtime);
|
|
40
32
|
},
|
|
41
33
|
settlePrompts: (runtime) => {
|
|
42
|
-
// The teardown path does the same pair first; both are idempotent by contract
|
|
34
|
+
// The teardown path does the same pair first; both are idempotent by contract.
|
|
43
35
|
cancelHostKeyPrompt(runtime);
|
|
44
36
|
cancelConflict(runtime);
|
|
45
37
|
},
|
|
@@ -83,9 +75,7 @@ export function createRuntimeEffects(ctx) {
|
|
|
83
75
|
runtime.remote.uiReady = patch.uiReady === true;
|
|
84
76
|
runtime.remote.localPort = patch.localPort;
|
|
85
77
|
},
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
// emits `flushLogs` today, and this stays a no-op instead of reaching into that module.
|
|
89
|
-
},
|
|
78
|
+
// `connect-progress` owns the coalescing flush timer and `log()` already scheduled it: nothing to do here.
|
|
79
|
+
flushLogs: () => { },
|
|
90
80
|
};
|
|
91
81
|
}
|
package/dist/src/runtime/fsm.js
CHANGED
|
@@ -1,39 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The `HostRuntime.session` state machine: a pure `transition(state, event, scope)`, plus the
|
|
3
|
-
* types and the write-back the executor uses.
|
|
4
|
-
*
|
|
5
|
-
* The transition table itself (one `Record<EventType, Rule>` row per event, its guards and its
|
|
6
|
-
* effects) lives in `rules.ts` next door; this file is the engine that looks a row up and applies
|
|
7
|
-
* it. Both halves together are the only place allowed to decide what a host runtime's session block
|
|
8
|
-
* becomes next (design-arch §2.4). Neither imports I/O: no runtime object, no clock, no settings,
|
|
9
|
-
* no timers. Everything outside the state arrives as a frozen `EventScope`; everything the
|
|
10
|
-
* executor has to do arrives as an `Effect` it performs itself. `test/unit/fsm.test.ts` drives all
|
|
11
|
-
* 8 statuses × 6 activities × 2 desires × 2 closing flags = 192 representative states against all
|
|
12
|
-
* 31 events (5,952 pairs) and asserts every pair has an explicit outcome: `ok: true` with effects,
|
|
13
|
-
* or `ok: false` with a `RefusalReason` — never an "unhandled" third shape.
|
|
14
|
-
*
|
|
15
|
-
* The machine does **not** own liveness. `remote.suspected/confirmed/recovered` used to live here
|
|
16
|
-
* and were never dispatched (nothing in `src/` ever emitted one); the watchdog (`watchdog.ts`) owns
|
|
17
|
-
* `remoteEpisode`/`episodeSource`/`remoteFailures`/`episodeCooldownUntil` and publishes its own
|
|
18
|
-
* bus events, so a second liveness writer in the FSM would have been a second source of truth.
|
|
19
|
-
*
|
|
20
|
-
* The table is a `Record<EventType, Rule>` over the 31 names `events.ts` declares, so a missing
|
|
21
|
-
* event is a compile error and an unlisted event cannot reach a rule. A refused transition returns
|
|
22
|
-
* the *same* state object: "invalid transitions never modify the state" is structural, not a
|
|
23
|
-
* promise the implementation has to remember.
|
|
24
|
-
*
|
|
25
|
-
* Semantics worth calling out (all from §2.4.5):
|
|
26
|
-
* - `activity` is the single flight lock. `null` means no one-shot action owns the link, so every
|
|
27
|
-
* entry point except `connect.requested` requires it; `connect.requested` refuses with
|
|
28
|
-
* `already-connecting`/`already-restarting`/`already-connected`, the strings `api.ts` passes on.
|
|
29
|
-
* - `epoch` (== the storage field `linkToken`) is the late-write filter. Request events adopt the
|
|
30
|
-
* caller's token through `epochAfter()`, which never lets the number go backwards; cancel events
|
|
31
|
-
* bump it. A late script compares `state.epoch` with its own token and stops writing.
|
|
32
|
-
* - `desired` is what the user wants, `status` is what is observed; they are deliberately not
|
|
33
|
-
* derived from each other.
|
|
34
|
-
*/
|
|
35
1
|
import { TABLE } from './rules.js';
|
|
36
|
-
/** The exhaustive list of refusals, for tests that assert "no third outcome"
|
|
2
|
+
/** The exhaustive list of refusals, for tests that assert "no third outcome": every pair is `ok` or a refusal. */
|
|
37
3
|
export const REFUSALS = [
|
|
38
4
|
'illegal-transition',
|
|
39
5
|
'unknown-event',
|
|
@@ -49,7 +15,7 @@ export const REFUSALS = [
|
|
|
49
15
|
export function isRefusal(result) {
|
|
50
16
|
return result.ok === false;
|
|
51
17
|
}
|
|
52
|
-
/** Never reaches `ErrorView`: only a script that ran into a refusal treats it as a bug
|
|
18
|
+
/** Never reaches `ErrorView`: only a script that ran into a refusal treats it as a bug. */
|
|
53
19
|
export class IllegalTransition extends Error {
|
|
54
20
|
code = 'illegal-transition';
|
|
55
21
|
eventType;
|
|
@@ -59,17 +25,15 @@ export class IllegalTransition extends Error {
|
|
|
59
25
|
this.eventType = eventType;
|
|
60
26
|
}
|
|
61
27
|
}
|
|
62
|
-
/* ── the two shared predicates (single implementation, §2.4.4) ────────────────────────────── */
|
|
63
28
|
/** "The user still wants this link, and this process still owns the runtime." */
|
|
64
29
|
export function stillWanted(state, scope) {
|
|
65
30
|
return state.desired === 'connected' && !state.closing && scope.runtimeExists && !scope.disposed;
|
|
66
31
|
}
|
|
67
|
-
/** `state.epoch === token`: a one-shot action may write
|
|
32
|
+
/** `state.epoch === token`: a one-shot action may write. */
|
|
68
33
|
export function isActivityCurrent(state, token) {
|
|
69
34
|
return state.epoch === token;
|
|
70
35
|
}
|
|
71
|
-
|
|
72
|
-
/** Put the session block of a runtime back to the state the machine produced (§7.6). */
|
|
36
|
+
/** Put the session block of a runtime back to the state the machine produced. */
|
|
73
37
|
export function writeBack(runtime, state) {
|
|
74
38
|
runtime.status = state.status;
|
|
75
39
|
runtime.desired = state.desired;
|
|
@@ -84,32 +48,18 @@ export function writeBack(runtime, state) {
|
|
|
84
48
|
runtime.connectCancelled = state.connectCancelled;
|
|
85
49
|
runtime.transportLost = state.transportLost;
|
|
86
50
|
runtime.reconnectAttempts = state.reconnectAttempts;
|
|
87
|
-
// The liveness fields (`remoteFailures`/`remoteEpisode`/`episodeSource`/`episodeCooldownUntil`)
|
|
88
|
-
//
|
|
51
|
+
// The liveness fields (`remoteFailures`/`remoteEpisode`/`episodeSource`/`episodeCooldownUntil`) are deliberately
|
|
52
|
+
// absent: `watchdog.ts` is their only writer.
|
|
89
53
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
*
|
|
93
|
-
* event payload of that row: `TABLE[event.type]` in `transition` would otherwise be a union of 31
|
|
94
|
-
* rule types with 31 different parameter types, which TypeScript cannot correlate with the event
|
|
95
|
-
* it was indexed by.
|
|
96
|
-
*/
|
|
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. */
|
|
97
57
|
function ruleFor(type) {
|
|
98
58
|
return TABLE[type];
|
|
99
59
|
}
|
|
100
|
-
/**
|
|
101
|
-
* Compute the next state and the effects of one event. Never mutates, never throws, never reads
|
|
102
|
-
* anything but its three arguments.
|
|
103
|
-
* @param state - the session state before the event.
|
|
104
|
-
* @param event - the event to apply (one of the 31 declared types).
|
|
105
|
-
* @param scope - read-only facts the guards and effects need.
|
|
106
|
-
* @returns `{ok:true,state,effects}` for an accepted move; `{ok:false,reason,state}` (same state
|
|
107
|
-
* object) for a refused one.
|
|
108
|
-
*/
|
|
60
|
+
/** Compute one event's next state and effects: never mutates, never throws, reads only its three arguments. */
|
|
109
61
|
export function transition(state, event, scope) {
|
|
110
|
-
// The table
|
|
111
|
-
// caller; the `unknown-event` branch only protects a JavaScript caller from a name the types
|
|
112
|
-
// forbid (and the completeness test asserts it never fires for a declared event).
|
|
62
|
+
// The table covers the 31 declared names, so a typed caller cannot miss; `unknown-event` only guards a JS caller.
|
|
113
63
|
const rule = ruleFor(event.type);
|
|
114
64
|
if (rule === undefined)
|
|
115
65
|
return { ok: false, reason: 'unknown-event', state };
|