dsh-remote-vs 0.20.0 → 0.21.0
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.en.md +6 -6
- package/README.md +5 -5
- package/cordis.patch.yml +10 -0
- package/lib/client.js +123 -89
- package/lib/index.js +234 -36
- package/lib/p2-router-fs.js +1 -1
- package/lib/types/client/vsc/editor-keymap.d.ts +45 -0
- package/lib/types/client/vsc/file-icons.d.ts +8 -9
- package/lib/types/client/vsc/kit.d.ts +7 -6
- package/lib/types/preset-switch.d.ts +89 -0
- package/lib/{worlds-Be1YE2Ot.js → worlds-DSzyajOB.js} +1 -1
- package/package.json +34 -34
- package/src/client/CodeEditor.tsx +24 -62
- package/src/client/index.tsx +3 -3
- package/src/client/vsc/editor-keymap.ts +97 -0
- package/src/client/vsc/file-icons.tsx +8 -9
- package/src/client/vsc/kit.tsx +7 -6
- package/src/index.ts +115 -52
- package/src/preset-switch.ts +143 -0
package/src/index.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import type { Context } from '@deepseek-ai/cordis'
|
|
10
10
|
import z from '@deepseek-ai/schemastery'
|
|
11
|
-
import type {} from '@deepseek-ai/dsh-agent'
|
|
11
|
+
import type { Agent } from '@deepseek-ai/dsh-agent'
|
|
12
12
|
import type {} from '@deepseek-ai/dsh-agent-preset-registry'
|
|
13
13
|
import type {} from '@deepseek-ai/dsh-host-webserver'
|
|
14
14
|
import type {} from '@deepseek-ai/dsh-system-prompt'
|
|
@@ -27,7 +27,14 @@ import {
|
|
|
27
27
|
MIN_TIMEOUT_MS,
|
|
28
28
|
resolveTimeouts,
|
|
29
29
|
} from './timeouts.ts'
|
|
30
|
-
import { RemoteWorlds, type RoutingMode, type WorldRoute } from './p2/worlds.ts'
|
|
30
|
+
import { RemoteWorlds, TombstonedError, type RoutingMode, type WorldRoute } from './p2/worlds.ts'
|
|
31
|
+
import {
|
|
32
|
+
REMOTE_PRESET_ID,
|
|
33
|
+
decidePresetSwitch,
|
|
34
|
+
runPresetSwitchAttempts,
|
|
35
|
+
type PresetSwitchAttempt,
|
|
36
|
+
type PresetSwitchFacts,
|
|
37
|
+
} from './preset-switch.ts'
|
|
31
38
|
import { SessionChanges } from './changes/recorder.ts'
|
|
32
39
|
import {
|
|
33
40
|
remoteExecTool,
|
|
@@ -449,60 +456,116 @@ export function apply(ctx: Context, config?: ResolvedConfig): void {
|
|
|
449
456
|
}
|
|
450
457
|
|
|
451
458
|
ctx.on('loader/volatile-update', () => { sync() })
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
459
|
+
/**
|
|
460
|
+
* Preset auto-switch for a new session born in a remote workspace (Phase 4b).
|
|
461
|
+
*
|
|
462
|
+
* 2026-11-10, bug "sometimes no Remote preset ~3 s after creating a session":
|
|
463
|
+
*
|
|
464
|
+
* - The Web client does not always MINT a session. `createInWorkspace` reuses
|
|
465
|
+
* the first listed BLANK session of the workspace by passing its id, and the
|
|
466
|
+
* Host answers that through `agents.resume` → `agent/created` with source
|
|
467
|
+
* `'resume'`. The old `source !== 'startup'` guard silently skipped exactly
|
|
468
|
+
* those sessions. Blankness (no turn started) is the precondition the preset
|
|
469
|
+
* registry itself enforces, so it is what we check now — a resumed session
|
|
470
|
+
* with history is excluded by it.
|
|
471
|
+
* - One 2.5 s shot also lost every transient state: the preset may not be
|
|
472
|
+
* composed yet, the working directory may be unreadable, or the workspace
|
|
473
|
+
* binding may not be listed. `runPresetSwitchAttempts` retries those for a
|
|
474
|
+
* bounded time and stops on terminal reasons (routing off, history, user's
|
|
475
|
+
* own preset, local directory).
|
|
476
|
+
*
|
|
477
|
+
* The listener still DETACHES: it returns immediately, so session creation
|
|
478
|
+
* never waits for a switch (the original 2026-10-13 race: selecting during
|
|
479
|
+
* creation while both preset trees register tools could kill the session).
|
|
480
|
+
* Failure modes are logged, never thrown; manual selection stays the fallback.
|
|
481
|
+
*/
|
|
482
|
+
const attemptPresetSwitch = async (agent: Agent): Promise<PresetSwitchAttempt> => {
|
|
483
|
+
const presets = ctx.get('agentPresets')
|
|
484
|
+
const agents = ctx.get('agents') as
|
|
485
|
+
| { turnBoundaryOf?: (session: unknown) => { openTurnStartSeq: number | null; lastTurn: number } | undefined }
|
|
486
|
+
| undefined
|
|
487
|
+
const workingDirectory = ctx.get('workingDirectory') as
|
|
488
|
+
| { get?: (session: unknown) => unknown }
|
|
489
|
+
| undefined
|
|
490
|
+
let boundary: { openTurnStartSeq: number | null; lastTurn: number } | undefined
|
|
491
|
+
try {
|
|
492
|
+
boundary = agents?.turnBoundaryOf?.(agent.session)
|
|
493
|
+
} catch {
|
|
494
|
+
boundary = undefined
|
|
495
|
+
}
|
|
496
|
+
let cwd = ''
|
|
497
|
+
try {
|
|
498
|
+
const current = workingDirectory?.get?.(agent.session)
|
|
499
|
+
if (typeof current === 'string') cwd = current
|
|
500
|
+
} catch {
|
|
501
|
+
// Soft read: an unbound session stays unreadable and retries.
|
|
502
|
+
}
|
|
503
|
+
let remote: boolean | undefined
|
|
504
|
+
let tombstoned = false
|
|
505
|
+
if (cwd !== '') {
|
|
506
|
+
try {
|
|
507
|
+
remote = worlds.routeDir(cwd).kind === 'remote'
|
|
508
|
+
} catch (error) {
|
|
509
|
+
if (error instanceof TombstonedError) tombstoned = true
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
// Service reads can throw during (re)mount; a throw is transient, so the
|
|
513
|
+
// facts fall back to "not composed yet" and the loop retries instead of
|
|
514
|
+
// losing the one chance (2026-11-10: that silent loss was the bug).
|
|
515
|
+
let currentPreset: string | undefined
|
|
516
|
+
let defaultPreset: string | undefined
|
|
517
|
+
try {
|
|
518
|
+
currentPreset = presets?.composedPreset(agent.ctx)
|
|
519
|
+
defaultPreset = presets?.defaultId
|
|
520
|
+
} catch {
|
|
521
|
+
currentPreset = undefined
|
|
522
|
+
defaultPreset = undefined
|
|
523
|
+
}
|
|
524
|
+
const facts: PresetSwitchFacts = {
|
|
525
|
+
mode: worlds.mode,
|
|
526
|
+
hasPresets: presets !== undefined,
|
|
527
|
+
blank: boundary !== undefined && boundary.openTurnStartSeq === null && boundary.lastTurn === 0,
|
|
528
|
+
cwd,
|
|
529
|
+
remote,
|
|
530
|
+
tombstoned,
|
|
531
|
+
currentPreset,
|
|
532
|
+
defaultPreset,
|
|
533
|
+
}
|
|
534
|
+
const step = decidePresetSwitch(facts)
|
|
535
|
+
if (step.action !== 'switch') return { step, facts }
|
|
536
|
+
if (presets === undefined) return { step: { action: 'stop', reason: 'presets-absent' }, facts }
|
|
537
|
+
try {
|
|
538
|
+
await presets.select(agent, REMOTE_PRESET_ID)
|
|
539
|
+
return { step: { action: 'stop', reason: 'switched' }, facts }
|
|
540
|
+
} catch (error) {
|
|
541
|
+
// agent-preset/locked = the user already started a turn: respect it.
|
|
542
|
+
const locked = error instanceof Error && /locked|already started/i.test(error.message)
|
|
543
|
+
return { step: { action: locked ? 'stop' : 'retry', reason: locked ? 'locked' : 'select-failed' }, facts }
|
|
544
|
+
}
|
|
545
|
+
}
|
|
462
546
|
ctx.on('agent/created', (payload) => {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
547
|
+
void (async () => {
|
|
548
|
+
try {
|
|
549
|
+
const { step, facts, attempts } = await runPresetSwitchAttempts(() => attemptPresetSwitch(payload.agent))
|
|
466
550
|
try {
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
if (
|
|
470
|
-
|
|
471
|
-
if (
|
|
472
|
-
|
|
473
|
-
if (workingDirectory === undefined) return
|
|
474
|
-
let cwd: string
|
|
475
|
-
try {
|
|
476
|
-
cwd = workingDirectory.get(payload.agent.session)
|
|
477
|
-
} catch {
|
|
478
|
-
return
|
|
479
|
-
}
|
|
480
|
-
let remote = false
|
|
481
|
-
try {
|
|
482
|
-
remote = worlds.routeDir(cwd).kind === 'remote'
|
|
483
|
-
} catch {
|
|
484
|
-
return
|
|
485
|
-
}
|
|
486
|
-
if (!remote) return
|
|
487
|
-
await presets.select(payload.agent, 'remote')
|
|
488
|
-
try {
|
|
489
|
-
ctx.logger.info(`[remote-vs] new remote session entered ${cwd}; preset switched to remote`)
|
|
490
|
-
} catch {
|
|
491
|
-
// Logging must never break routing.
|
|
492
|
-
}
|
|
493
|
-
} catch (error) {
|
|
494
|
-
// agent-preset/locked = user already started a turn: respect it.
|
|
495
|
-
const locked = error instanceof Error && /locked|already started/i.test(error.message)
|
|
496
|
-
if (locked) return
|
|
497
|
-
try {
|
|
498
|
-
ctx.logger.warn(`[remote-vs] preset auto-switch skipped: ${error instanceof Error ? error.message : String(error)}`)
|
|
499
|
-
} catch {
|
|
500
|
-
// Ignore.
|
|
551
|
+
if (step.reason === 'switched' && facts?.remote === true) {
|
|
552
|
+
ctx.logger.info(`[remote-vs] remote session ${facts.cwd}: preset switched to ${REMOTE_PRESET_ID} after ${attempts} attempt(s)`)
|
|
553
|
+
} else if (step.action === 'retry') {
|
|
554
|
+
ctx.logger.warn(`[remote-vs] preset auto-switch gave up after ${attempts} attempt(s): ${step.reason}`)
|
|
555
|
+
} else if (facts?.remote === true) {
|
|
556
|
+
ctx.logger.info(`[remote-vs] preset auto-switch skipped for ${facts.cwd}: ${step.reason}`)
|
|
501
557
|
}
|
|
558
|
+
} catch {
|
|
559
|
+
// Logging must never break routing.
|
|
502
560
|
}
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
|
|
561
|
+
} catch (error) {
|
|
562
|
+
try {
|
|
563
|
+
ctx.logger.warn(`[remote-vs] preset auto-switch skipped: ${error instanceof Error ? error.message : String(error)}`)
|
|
564
|
+
} catch {
|
|
565
|
+
// Ignore.
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
})()
|
|
506
569
|
return undefined
|
|
507
570
|
})
|
|
508
571
|
sync()
|
|
@@ -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
|
+
}
|