dsh-remote-vs 0.20.0 → 0.20.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.
@@ -0,0 +1,143 @@
1
+ /**
2
+ * Auto-switch decision for a new session born in a remote workspace.
3
+ *
4
+ * Why this module is pure
5
+ * -----------------------
6
+ * The 2026-11-10 bug was "sometimes no preset switch about 3 s after creating a
7
+ * session". Two independent causes had to be found by reading the Host:
8
+ *
9
+ * 1. **The switch only listened to `source === 'startup'`.** The Web client does
10
+ * not always mint a session: `session.create` accepts a named BLANK session
11
+ * and reuses it (`createInWorkspace` passes the first listed blank id), which
12
+ * the Host answers through `agents.resume` → `agent/created` with source
13
+ * `'resume'`. Those sessions never even armed the timer.
14
+ * 2. **A single 2.5 s shot lost every transient state.** At fire time the preset
15
+ * may not be composed yet, the working directory may be unreadable, or the
16
+ * workspace binding may not be listed; each guard returned silently, and the
17
+ * one chance was spent.
18
+ *
19
+ * The fix keeps the "never block session creation" contract and turns the
20
+ * decision into a pure function plus a bounded retry loop, so every branch is
21
+ * covered by a gate instead of being inferred from a silent return.
22
+ *
23
+ * Blankness — no turn started — is the real precondition (`presets.select`
24
+ * enforces exactly that), and it is also what excludes resumed sessions with
25
+ * history. The event source only told us how the session came to be.
26
+ */
27
+
28
+ /** Preset id this plugin switches remote sessions to (see `cordis.patch.yml`). */
29
+ export const REMOTE_PRESET_ID = 'remote'
30
+
31
+ /** Delay before each attempt, in order. The first attempt keeps the old 2.5 s. */
32
+ export const PRESET_SWITCH_DELAYS_MS: readonly number[] = [2500, 1000, 1500, 2000, 3000]
33
+
34
+ /** Why one attempt ended the way it did (logged, never thrown). */
35
+ export type PresetSwitchReason =
36
+ | 'switched'
37
+ | 'already-remote'
38
+ | 'custom-preset'
39
+ | 'routing-off'
40
+ | 'presets-absent'
41
+ | 'services-absent'
42
+ | 'not-blank'
43
+ | 'preset-unbound'
44
+ | 'cwd-unavailable'
45
+ | 'route-unknown'
46
+ | 'tombstoned'
47
+ | 'local-session'
48
+ | 'locked'
49
+ | 'select-failed'
50
+
51
+ /** One attempt's outcome: switch now, try again later, or stop for good. */
52
+ export type PresetSwitchStep =
53
+ | { action: 'switch' }
54
+ | { action: 'retry'; reason: PresetSwitchReason }
55
+ | { action: 'stop'; reason: PresetSwitchReason }
56
+
57
+ /** A settled attempt (`switch` is reported as `stop/switched`). */
58
+ export type PresetSwitchSettled = Exclude<PresetSwitchStep, { action: 'switch' }>
59
+
60
+ /** Everything the decision reads, resolved by the caller (no services here). */
61
+ export interface PresetSwitchFacts {
62
+ /** Routing mode from the plugin config: only `enforce` auto-switches. */
63
+ mode: string
64
+ /** The preset registry service is mounted at all. */
65
+ hasPresets: boolean
66
+ /** No turn has started yet (`turnBoundaryOf`): openTurnStartSeq null, lastTurn 0. */
67
+ blank: boolean
68
+ /** The step also runs for local sessions; the caller decides what to log. */
69
+ cwd: string
70
+ /** `undefined` when the route lookup threw (retried); false means local. */
71
+ remote: boolean | undefined
72
+ /** The directory belongs to a retired binding: dead session, never switch. */
73
+ tombstoned: boolean
74
+ currentPreset: string | undefined
75
+ defaultPreset: string | undefined
76
+ }
77
+
78
+ /**
79
+ * Decide one attempt.
80
+ *
81
+ * Order matters: the terminal reasons come first (config, history, user choice),
82
+ * then the transient ones (composed preset, cwd, route), then the switch.
83
+ */
84
+ export function decidePresetSwitch(facts: PresetSwitchFacts): PresetSwitchStep {
85
+ if (facts.mode !== 'enforce') return { action: 'stop', reason: 'routing-off' }
86
+ if (!facts.hasPresets) return { action: 'stop', reason: 'presets-absent' }
87
+ if (!facts.blank) return { action: 'stop', reason: 'not-blank' }
88
+ if (facts.currentPreset === REMOTE_PRESET_ID) return { action: 'stop', reason: 'already-remote' }
89
+ if (facts.currentPreset === undefined) return { action: 'retry', reason: 'preset-unbound' }
90
+ if (facts.currentPreset !== facts.defaultPreset) return { action: 'stop', reason: 'custom-preset' }
91
+ if (facts.cwd.trim() === '') return { action: 'retry', reason: 'cwd-unavailable' }
92
+ if (facts.tombstoned) return { action: 'stop', reason: 'tombstoned' }
93
+ if (facts.remote === undefined) return { action: 'retry', reason: 'route-unknown' }
94
+ if (!facts.remote) return { action: 'stop', reason: 'local-session' }
95
+ return { action: 'switch' }
96
+ }
97
+
98
+ /** One attempt as the caller sees it: the step plus the facts it was made from. */
99
+ export interface PresetSwitchAttempt {
100
+ step: PresetSwitchSettled
101
+ facts: PresetSwitchFacts
102
+ }
103
+
104
+ /** Wait for `ms`, unref'd so a pending switch never keeps the Host alive. */
105
+ function defaultSleep(ms: number): Promise<void> {
106
+ return new Promise<void>(resolve => {
107
+ const timer = setTimeout(resolve, ms)
108
+ timer.unref?.()
109
+ })
110
+ }
111
+
112
+ /**
113
+ * Run attempts with the given delays until one stops or switches.
114
+ *
115
+ * A retry is only worth spending for the transient reasons the planner names;
116
+ * the loop itself does not re-classify, so a future terminal reason added to the
117
+ * planner stays terminal automatically.
118
+ *
119
+ * @returns the last attempt and how many attempts were used (1-based).
120
+ */
121
+ export async function runPresetSwitchAttempts(
122
+ attempt: () => Promise<PresetSwitchAttempt> | PresetSwitchAttempt,
123
+ sleep: (ms: number) => Promise<void> = defaultSleep,
124
+ delays: readonly number[] = PRESET_SWITCH_DELAYS_MS,
125
+ ): Promise<{ step: PresetSwitchSettled; facts: PresetSwitchFacts | undefined; attempts: number }> {
126
+ let last: PresetSwitchAttempt | undefined
127
+ let attempts = 0
128
+ for (const delay of delays) {
129
+ attempts += 1
130
+ await sleep(delay)
131
+ last = await attempt()
132
+ if (last.step.action !== 'retry') {
133
+ return { step: last.step, facts: last.facts, attempts }
134
+ }
135
+ }
136
+ return {
137
+ // An exhausted retry keeps `action: 'retry'`: the caller reads that as
138
+ // "gave up after the last attempt" and logs the reason exactly once.
139
+ step: last === undefined ? { action: 'stop', reason: 'services-absent' } : last.step,
140
+ facts: last?.facts,
141
+ attempts,
142
+ }
143
+ }