@linxin666/dsh-client-ui-task-board-ssh 0.0.0-stage → 0.4.20
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/LICENSE +202 -0
- package/README.i18n.yaml +6 -0
- package/README.md +85 -2
- package/README.zh.md +86 -0
- package/cordis.patch.yml +15 -0
- package/icon.svg +5 -0
- package/lib/client.js +4429 -0
- package/lib/index.js +6100 -0
- package/lib/types/client/PluginSettingsCard.d.ts +255 -0
- package/lib/types/client/RangeBubble.d.ts +46 -0
- package/lib/types/client/SegmentGroup.d.ts +28 -0
- package/lib/types/client/index.d.ts +100 -0
- package/lib/types/client/locales.d.ts +125 -0
- package/lib/types/client/seat-props.d.ts +53 -0
- package/lib/types/client/settings-entry-form.d.ts +49 -0
- package/lib/types/client/settings-form.d.ts +237 -0
- package/lib/types/client/ssh/card-decoration.d.ts +23 -0
- package/lib/types/client/ssh/compose-section.d.ts +7 -0
- package/lib/types/client/ssh/detail-section.d.ts +7 -0
- package/lib/types/client/ssh/extension.d.ts +54 -0
- package/lib/types/client/ssh/log-buffer.d.ts +128 -0
- package/lib/types/client/ssh/log-view.d.ts +37 -0
- package/lib/types/client/ssh/model.d.ts +130 -0
- package/lib/types/client/ssh/settings-section.d.ts +134 -0
- package/lib/types/client/ssh/summary.d.ts +160 -0
- package/lib/types/client/ssh/target-form.d.ts +60 -0
- package/lib/types/core/action.d.ts +89 -0
- package/lib/types/core/adapters/claude.d.ts +15 -0
- package/lib/types/core/adapters/codex.d.ts +15 -0
- package/lib/types/core/adapters/custom.d.ts +29 -0
- package/lib/types/core/adapters/dsh.d.ts +21 -0
- package/lib/types/core/adapters/index.d.ts +25 -0
- package/lib/types/core/adapters/types.d.ts +76 -0
- package/lib/types/core/contract.d.ts +445 -0
- package/lib/types/core/launcher-script.d.ts +13 -0
- package/lib/types/core/launcher.d.ts +153 -0
- package/lib/types/core/ledger.d.ts +123 -0
- package/lib/types/core/shell.d.ts +61 -0
- package/lib/types/core/target.d.ts +143 -0
- package/lib/types/core/task-record.d.ts +63 -0
- package/lib/types/core/workspace.d.ts +74 -0
- package/lib/types/host/board.d.ts +71 -0
- package/lib/types/host/engine.d.ts +418 -0
- package/lib/types/host/extension.d.ts +59 -0
- package/lib/types/host/logs.d.ts +135 -0
- package/lib/types/host/payload.d.ts +75 -0
- package/lib/types/host/prompt.d.ts +44 -0
- package/lib/types/host/queue.d.ts +53 -0
- package/lib/types/host/remote.d.ts +120 -0
- package/lib/types/host/settings.d.ts +149 -0
- package/lib/types/host/timers.d.ts +20 -0
- package/lib/types/host/tools.d.ts +15 -0
- package/lib/types/host/workspace/errors.d.ts +29 -0
- package/lib/types/host/workspace/git.d.ts +108 -0
- package/lib/types/host/workspace/in-place.d.ts +36 -0
- package/lib/types/host/workspace/index.d.ts +28 -0
- package/lib/types/host/workspace/layout.d.ts +62 -0
- package/lib/types/host/workspace/local-git.d.ts +72 -0
- package/lib/types/host/workspace/remote-git.d.ts +111 -0
- package/lib/types/host/workspace/remote.d.ts +85 -0
- package/lib/types/host/workspace/types.d.ts +28 -0
- package/lib/types/host/workspace-seam.d.ts +100 -0
- package/lib/types/index.d.ts +97 -0
- package/lib/types/mount-once.d.ts +55 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +89 -3
- package/src/client/PluginSettingsCard.tsx +838 -0
- package/src/client/RangeBubble.tsx +164 -0
- package/src/client/SegmentGroup.tsx +93 -0
- package/src/client/css-modules.d.ts +5 -0
- package/src/client/index.ts +195 -0
- package/src/client/locales.ts +257 -0
- package/src/client/seat-props.ts +58 -0
- package/src/client/settings-card.module.css +821 -0
- package/src/client/settings-entry-form.ts +141 -0
- package/src/client/settings-form.ts +529 -0
- package/src/client/ssh/card-decoration.tsx +55 -0
- package/src/client/ssh/compose-section.tsx +147 -0
- package/src/client/ssh/detail-section.tsx +345 -0
- package/src/client/ssh/extension.ts +271 -0
- package/src/client/ssh/log-buffer.ts +224 -0
- package/src/client/ssh/log-view.tsx +149 -0
- package/src/client/ssh/model.ts +299 -0
- package/src/client/ssh/settings-section.tsx +401 -0
- package/src/client/ssh/summary.ts +388 -0
- package/src/client/ssh/target-form.tsx +380 -0
- package/src/client/ssh.module.css +617 -0
- package/src/core/action.ts +270 -0
- package/src/core/adapters/claude.ts +64 -0
- package/src/core/adapters/codex.ts +83 -0
- package/src/core/adapters/custom.ts +88 -0
- package/src/core/adapters/dsh.ts +87 -0
- package/src/core/adapters/index.ts +74 -0
- package/src/core/adapters/types.ts +95 -0
- package/src/core/contract.ts +510 -0
- package/src/core/launcher-script.ts +313 -0
- package/src/core/launcher.ts +283 -0
- package/src/core/ledger.ts +372 -0
- package/src/core/shell.ts +77 -0
- package/src/core/target.ts +413 -0
- package/src/core/task-record.ts +65 -0
- package/src/core/workspace.ts +78 -0
- package/src/host/board.ts +103 -0
- package/src/host/engine.ts +1602 -0
- package/src/host/extension.ts +304 -0
- package/src/host/logs.ts +297 -0
- package/src/host/payload.ts +134 -0
- package/src/host/prompt.ts +89 -0
- package/src/host/queue.ts +73 -0
- package/src/host/remote.ts +328 -0
- package/src/host/settings.ts +294 -0
- package/src/host/timers.ts +33 -0
- package/src/host/tools.ts +259 -0
- package/src/host/workspace/errors.ts +43 -0
- package/src/host/workspace/git.ts +305 -0
- package/src/host/workspace/in-place.ts +58 -0
- package/src/host/workspace/index.ts +37 -0
- package/src/host/workspace/layout.ts +122 -0
- package/src/host/workspace/local-git.ts +164 -0
- package/src/host/workspace/remote-git.ts +244 -0
- package/src/host/workspace/remote.ts +181 -0
- package/src/host/workspace/types.ts +34 -0
- package/src/host/workspace-seam.ts +132 -0
- package/src/index.ts +243 -0
- package/src/mount-once.ts +142 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The host half's reading of the payload it stores on a card.
|
|
3
|
+
*
|
|
4
|
+
* The schema itself belongs to `src/core/ledger.ts` — the target pin, the active
|
|
5
|
+
* run, the bounded history and the one instruction a future run still owes — and
|
|
6
|
+
* the host half adds no member of its own to it. What this module owns is the
|
|
7
|
+
* write: it stamps the document, keeps it under the board's 64 KiB ceiling and
|
|
8
|
+
* makes every member it clears explicit, because the board merges a written
|
|
9
|
+
* payload into the stored one key by key.
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-task-board-ssh/host/payload
|
|
12
|
+
*/
|
|
13
|
+
import {
|
|
14
|
+
SSH_INTEGRATION_KEY,
|
|
15
|
+
SSH_LEDGER_VERSION,
|
|
16
|
+
parseSshLedgerPayload,
|
|
17
|
+
trimSshLedgerPayload,
|
|
18
|
+
type SshLedgerPayload,
|
|
19
|
+
} from '../core/ledger.ts'
|
|
20
|
+
import { isTerminalRunPhase, type SshRunRecord } from '../core/target.ts'
|
|
21
|
+
|
|
22
|
+
export { SSH_INTEGRATION_KEY }
|
|
23
|
+
export type { SshPendingInstruction } from '../core/ledger.ts'
|
|
24
|
+
|
|
25
|
+
/** How many settled runs one card keeps here, oldest dropped first. */
|
|
26
|
+
export const HOST_HISTORY_LIMIT = 20
|
|
27
|
+
|
|
28
|
+
/** One card's payload: exactly the frozen document, with nothing added beside it. */
|
|
29
|
+
export type HostSshPayload = Omit<SshLedgerPayload, 'version'>
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Parse one stored payload into what the host half works with.
|
|
33
|
+
* @param raw - the card's `integrations.ssh` value, or undefined when it carries none.
|
|
34
|
+
* @returns the payload, or undefined when the card carries none this build understands.
|
|
35
|
+
*/
|
|
36
|
+
export function readHostPayload(raw: unknown): HostSshPayload | undefined {
|
|
37
|
+
const frozen = parseSshLedgerPayload(raw)
|
|
38
|
+
if (!frozen.ok) return undefined
|
|
39
|
+
const { version: _version, ...payload } = frozen.value
|
|
40
|
+
return { ...payload, history: [...payload.history] }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Serialize one payload for storage, keeping it strictly under the board's ceiling.
|
|
45
|
+
*
|
|
46
|
+
* The frozen trimmer does the budgeting, including the room it reserves for a
|
|
47
|
+
* queued instruction, so the store may drop the oldest history and never the
|
|
48
|
+
* message a person is waiting on.
|
|
49
|
+
* @param next - the payload to store.
|
|
50
|
+
* @param now - clock instant written into the document (ms epoch).
|
|
51
|
+
* @returns the board-ready extension payload.
|
|
52
|
+
*/
|
|
53
|
+
export function writeHostPayload(next: HostSshPayload, now: number): Record<string, unknown> {
|
|
54
|
+
const trimmed = trimSshLedgerPayload({ ...next, version: SSH_LEDGER_VERSION, updatedAt: now }, {
|
|
55
|
+
maxHistory: HOST_HISTORY_LIMIT,
|
|
56
|
+
})
|
|
57
|
+
// The board merges a written payload into the stored one key by key, so a
|
|
58
|
+
// member this write clears has to be present as an explicit undefined (the
|
|
59
|
+
// board deletes such a key, and JSON drops it on the way to disk).
|
|
60
|
+
return {
|
|
61
|
+
...trimmed,
|
|
62
|
+
target: trimmed.target,
|
|
63
|
+
activeRun: trimmed.activeRun,
|
|
64
|
+
pending: trimmed.pending,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The same payload carrying one run as the run in flight.
|
|
70
|
+
* @param payload - the current payload.
|
|
71
|
+
* @param run - the run to carry.
|
|
72
|
+
* @returns the next payload.
|
|
73
|
+
*/
|
|
74
|
+
export function withActiveRun(payload: HostSshPayload, run: SshRunRecord): HostSshPayload {
|
|
75
|
+
return { ...payload, activeRun: run, history: [...payload.history] }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The same payload with one run settled into the bounded history.
|
|
80
|
+
* @param payload - the current payload.
|
|
81
|
+
* @param run - the settled run.
|
|
82
|
+
* @returns the next payload, the active run cleared.
|
|
83
|
+
*/
|
|
84
|
+
export function withSettledRun(payload: HostSshPayload, run: SshRunRecord): HostSshPayload {
|
|
85
|
+
const history = [...payload.history.filter(record => record.runId !== run.runId), run]
|
|
86
|
+
const next: HostSshPayload = {
|
|
87
|
+
...payload,
|
|
88
|
+
history: history.slice(Math.max(0, history.length - HOST_HISTORY_LIMIT)),
|
|
89
|
+
}
|
|
90
|
+
delete next.activeRun
|
|
91
|
+
return next
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* One run of a card's payload by id.
|
|
96
|
+
*
|
|
97
|
+
* A run stays addressable after it settles — the detail view names a historical
|
|
98
|
+
* run to replay its log — so the search covers the run in flight and the whole
|
|
99
|
+
* bounded history.
|
|
100
|
+
* @param payload - the payload to read.
|
|
101
|
+
* @param runId - run to find.
|
|
102
|
+
* @returns the run, or undefined when this card never had it.
|
|
103
|
+
*/
|
|
104
|
+
export function findRun(payload: HostSshPayload | undefined, runId: string): SshRunRecord | undefined {
|
|
105
|
+
if (payload === undefined) return undefined
|
|
106
|
+
if (payload.activeRun?.runId === runId) return payload.activeRun
|
|
107
|
+
return payload.history.find(record => record.runId === runId)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The most recent session this card's runs recorded, which is the one a
|
|
112
|
+
* follow-up would continue.
|
|
113
|
+
* @param payload - the payload to read.
|
|
114
|
+
* @returns the session id, or undefined when no run of this card reported one.
|
|
115
|
+
*/
|
|
116
|
+
export function latestSessionId(payload: HostSshPayload): string | undefined {
|
|
117
|
+
const candidates = [...payload.history, ...(payload.activeRun === undefined ? [] : [payload.activeRun])]
|
|
118
|
+
for (let index = candidates.length - 1; index >= 0; index -= 1) {
|
|
119
|
+
const sessionId = candidates[index]?.sessionId
|
|
120
|
+
if (sessionId !== undefined && sessionId !== '') return sessionId
|
|
121
|
+
}
|
|
122
|
+
return undefined
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The run a card is waiting on, when it is still in a phase that schedules work.
|
|
127
|
+
* @param payload - the payload to read.
|
|
128
|
+
* @returns the live run, or undefined when the card waits on nothing.
|
|
129
|
+
*/
|
|
130
|
+
export function liveRunOf(payload: HostSshPayload): SshRunRecord | undefined {
|
|
131
|
+
const run = payload.activeRun
|
|
132
|
+
if (run === undefined) return undefined
|
|
133
|
+
return isTerminalRunPhase(run.phase) ? undefined : run
|
|
134
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The text one run's prompt carries: the proxy preamble a local session starts
|
|
3
|
+
* with, and the instruction a resumed (or degraded) run reads.
|
|
4
|
+
*
|
|
5
|
+
* These are model-facing strings, so they live here rather than in a locale
|
|
6
|
+
* dictionary, exactly as the board's own tool descriptions do. Nothing in them
|
|
7
|
+
* is rendered to a person.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-task-board-ssh/host/prompt
|
|
10
|
+
*/
|
|
11
|
+
import type { TaskRecord } from '../core/task-record.ts'
|
|
12
|
+
import type { SshTarget } from '../core/target.ts'
|
|
13
|
+
import type { SshPendingInstruction } from './payload.ts'
|
|
14
|
+
|
|
15
|
+
/** The tool family a proxy run has to drive the host with. */
|
|
16
|
+
export const SSH_TOOL_FAMILY = 'ssh_list, ssh_exec, ssh_upload and ssh_download'
|
|
17
|
+
|
|
18
|
+
/** Prefix a recorded summary carries when the instruction started a fresh run. */
|
|
19
|
+
export const SSH_DEGRADED_SUMMARY_MARKER = '[fresh run: this agent cannot resume, so the previous summary was carried in the prompt]'
|
|
20
|
+
|
|
21
|
+
/** Where a proxy run's work happens, in the words the model reads. */
|
|
22
|
+
function proxyPlacement(target: SshTarget): string {
|
|
23
|
+
const workspace = target.workspace
|
|
24
|
+
if (workspace.kind === 'in-place') {
|
|
25
|
+
return `Remote directory: ${workspace.path}`
|
|
26
|
+
}
|
|
27
|
+
const source = workspace.source
|
|
28
|
+
if (source.kind === 'url') {
|
|
29
|
+
const base = source.base === undefined ? '' : ` (base branch ${source.base})`
|
|
30
|
+
return `Repository: ${source.url}${base}. Create a per-run worktree of it on the host before you edit, and leave the result on the host.`
|
|
31
|
+
}
|
|
32
|
+
return `Repository: the workspace repository ${source.workspaceId}. It has to be sent to the host before the run, so ask the user to pin a repository URL instead when one is reachable there.`
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Build the preamble a proxy card's local session opens with.
|
|
37
|
+
*
|
|
38
|
+
* The board injects it ahead of the card body on the same path it injects tag
|
|
39
|
+
* prefixes, so the session reads what this run IS before it reads the task: the
|
|
40
|
+
* work happens on a pinned host through the `ssh_*` tools, and nothing about the
|
|
41
|
+
* card is local.
|
|
42
|
+
* @param task - the card about to run.
|
|
43
|
+
* @param target - the target the card pins.
|
|
44
|
+
* @returns the preamble text.
|
|
45
|
+
*/
|
|
46
|
+
export function proxyPreamble(task: TaskRecord, target: SshTarget): string {
|
|
47
|
+
return [
|
|
48
|
+
'Remote execution target (task board SSH provider, proxy mode).',
|
|
49
|
+
`Host alias: ${target.host}`,
|
|
50
|
+
proxyPlacement(target),
|
|
51
|
+
`Run every command and every file operation for this card on that host through the ssh_* tools of the dsh-ssh plugin (${SSH_TOOL_FAMILY}); read the host list with ssh_list before you start if you are unsure of the alias. The paths above exist on the host, not on this machine, so do not do the work locally and do not assume a local path mirrors a remote one.`,
|
|
52
|
+
`Card: ${task.title} (id ${task.id})`,
|
|
53
|
+
].join('\n')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Build the prompt one run reads.
|
|
58
|
+
*
|
|
59
|
+
* A start reads the card's own prompt. A follow-up or a rework that continues the
|
|
60
|
+
* agent's session reads the instruction alone, because the session already holds
|
|
61
|
+
* the work. A degraded follow-up — the agent cannot be resumed — reads what the
|
|
62
|
+
* previous run reported before it reads the instruction, which is the whole
|
|
63
|
+
* reason that path exists.
|
|
64
|
+
* @param task - the card being run.
|
|
65
|
+
* @param pending - the instruction this run carries, when it carries one.
|
|
66
|
+
* @param previousSummary - what the previous run reported, when it is known.
|
|
67
|
+
* @param resumed - whether the run continues the agent's session.
|
|
68
|
+
* @returns the text written to the run's prompt file.
|
|
69
|
+
*/
|
|
70
|
+
export function runPrompt(
|
|
71
|
+
task: TaskRecord,
|
|
72
|
+
pending: SshPendingInstruction | undefined,
|
|
73
|
+
previousSummary: string | undefined,
|
|
74
|
+
resumed: boolean,
|
|
75
|
+
): string {
|
|
76
|
+
if (pending === undefined) return task.prompt
|
|
77
|
+
const heading = pending.kind === 'rework' ? 'Rework instructions for the previous run of this card.' : 'Follow-up for the previous run of this card.'
|
|
78
|
+
if (resumed) return `${heading}\n\n${pending.text}`
|
|
79
|
+
const previous = previousSummary === undefined || previousSummary === ''
|
|
80
|
+
? 'The previous run reported no summary.'
|
|
81
|
+
: `The previous run reported:\n${previousSummary}`
|
|
82
|
+
return [
|
|
83
|
+
heading,
|
|
84
|
+
'The agent cannot be resumed in its earlier session, so this is a fresh run that carries what the previous run reported.',
|
|
85
|
+
previous,
|
|
86
|
+
'Instruction:',
|
|
87
|
+
pending.text,
|
|
88
|
+
].join('\n\n')
|
|
89
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-host admission: the concurrency ceiling and the first-in-first-out queue.
|
|
3
|
+
*
|
|
4
|
+
* The queue is not stored anywhere of its own — it is the ordered set of cards
|
|
5
|
+
* whose payload carries a queued run, which is what makes it survive a restart
|
|
6
|
+
* for free: the same cards, read in the same order, produce the same queue.
|
|
7
|
+
* Order is the instant each run was requested, with the run id as the tie-break
|
|
8
|
+
* so two runs requested in the same millisecond still have one order.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-task-board-ssh/host/queue
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** One run waiting for a slot on its host. */
|
|
14
|
+
export interface SshQueueEntry {
|
|
15
|
+
/** Card waiting for the slot. */
|
|
16
|
+
taskId: string
|
|
17
|
+
/** Run the card waits on. */
|
|
18
|
+
runId: string
|
|
19
|
+
/** Instant the run was requested (ms epoch). */
|
|
20
|
+
startedAt: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** What one admission pass decided. */
|
|
24
|
+
export interface SshAdmission {
|
|
25
|
+
/** Runs that may start now, in queue order. */
|
|
26
|
+
start: SshQueueEntry[]
|
|
27
|
+
/** Runs left waiting, in the order they will be reached. */
|
|
28
|
+
waiting: SshQueueEntry[]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Default ceiling of concurrent runs on one host. */
|
|
32
|
+
export const DEFAULT_PER_HOST_CAP = 2
|
|
33
|
+
|
|
34
|
+
/** Lowest accepted ceiling; one run at a time is the smallest queue worth naming. */
|
|
35
|
+
export const PER_HOST_CAP_MIN = 1
|
|
36
|
+
|
|
37
|
+
/** Highest accepted ceiling per host. */
|
|
38
|
+
export const PER_HOST_CAP_MAX = 16
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Clamp a configured ceiling into the accepted range.
|
|
42
|
+
* @param value - configured value; anything unusable falls back to the default.
|
|
43
|
+
* @returns an integer between {@link PER_HOST_CAP_MIN} and {@link PER_HOST_CAP_MAX}.
|
|
44
|
+
*/
|
|
45
|
+
export function normalizePerHostCap(value: unknown): number {
|
|
46
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return DEFAULT_PER_HOST_CAP
|
|
47
|
+
return Math.min(PER_HOST_CAP_MAX, Math.max(PER_HOST_CAP_MIN, Math.trunc(value)))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Order a host's queue the way it will be served.
|
|
52
|
+
* @param entries - every queued run of one host.
|
|
53
|
+
* @returns the same entries, oldest first.
|
|
54
|
+
*/
|
|
55
|
+
export function orderQueue(entries: readonly SshQueueEntry[]): SshQueueEntry[] {
|
|
56
|
+
return [...entries].sort((left, right) => {
|
|
57
|
+
if (left.startedAt !== right.startedAt) return left.startedAt - right.startedAt
|
|
58
|
+
return left.runId < right.runId ? -1 : left.runId > right.runId ? 1 : 0
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Decide which queued runs may start now.
|
|
64
|
+
* @param entries - every queued run of one host, in any order.
|
|
65
|
+
* @param running - how many runs of that host already hold a slot.
|
|
66
|
+
* @param cap - the host's ceiling.
|
|
67
|
+
* @returns the runs to start now and the ones still waiting.
|
|
68
|
+
*/
|
|
69
|
+
export function admitRuns(entries: readonly SshQueueEntry[], running: number, cap: number): SshAdmission {
|
|
70
|
+
const ordered = orderQueue(entries)
|
|
71
|
+
const free = Math.max(0, normalizePerHostCap(cap) - Math.max(0, Math.trunc(running)))
|
|
72
|
+
return { start: ordered.slice(0, free), waiting: ordered.slice(free) }
|
|
73
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every remote operation one run performs, over the `dshSsh` face.
|
|
3
|
+
*
|
|
4
|
+
* The remote side is one POSIX `sh` script the provider installs once per host
|
|
5
|
+
* (`src/core/launcher.ts` builds every command and reads every answer); this
|
|
6
|
+
* module is the only place that composes those commands with the `dshSsh`
|
|
7
|
+
* `exec` forwarder, so no other file in the host half talks to a host directly.
|
|
8
|
+
*
|
|
9
|
+
* Three guarantees live here and nowhere else:
|
|
10
|
+
*
|
|
11
|
+
* - The state directory is resolved once per host from the host's own `$HOME`
|
|
12
|
+
* and cached, so no command depends on the remote shell expanding a tilde.
|
|
13
|
+
* - `start` is de-duplicated in flight: the pool may replay a command when it
|
|
14
|
+
* reconnects, and a replayed start must join the start already running instead
|
|
15
|
+
* of spawning a second agent (the script is idempotent on the run id as well,
|
|
16
|
+
* which covers a replay that arrives after this process forgot the first one).
|
|
17
|
+
* - A failed remote command is one diagnosable error carrying the host's own
|
|
18
|
+
* stderr, bounded, instead of a silent `success: false` a caller can ignore.
|
|
19
|
+
*
|
|
20
|
+
* @module dsh-task-board-ssh/host/remote
|
|
21
|
+
*/
|
|
22
|
+
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
|
|
23
|
+
import { tmpdir } from 'node:os'
|
|
24
|
+
import { join } from 'node:path'
|
|
25
|
+
import type { DshSshService } from '../core/contract.ts'
|
|
26
|
+
import type { SshAgentCommand } from '../core/adapters/types.ts'
|
|
27
|
+
import {
|
|
28
|
+
agentScript,
|
|
29
|
+
buildLauncherCommand,
|
|
30
|
+
buildLauncherInstallCommand,
|
|
31
|
+
buildRunFileCommands,
|
|
32
|
+
parseLauncherStatus,
|
|
33
|
+
runDirectory,
|
|
34
|
+
runFilePath,
|
|
35
|
+
stateDirForHome,
|
|
36
|
+
type LauncherStatus,
|
|
37
|
+
} from '../core/launcher.ts'
|
|
38
|
+
import { shellEnsureDir, shellRemoveDir, shellWriteFile } from '../core/shell.ts'
|
|
39
|
+
|
|
40
|
+
/** Ceiling for one remote command, in milliseconds. */
|
|
41
|
+
export const DEFAULT_COMMAND_TIMEOUT_MS = 30_000
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Prompts whose shell-quoted command exceeds this threshold are uploaded via
|
|
45
|
+
* SFTP instead of inlined into `sh -c`, staying well below Linux's 128 KiB
|
|
46
|
+
* `MAX_ARG_STRLEN` limit (E2BIG).
|
|
47
|
+
*/
|
|
48
|
+
const INLINE_PROMPT_COMMAND_LIMIT_BYTES = 32 * 1024
|
|
49
|
+
|
|
50
|
+
/** Longest remote error text this module carries into a message, in characters. */
|
|
51
|
+
const ERROR_TEXT_MAX = 400
|
|
52
|
+
|
|
53
|
+
/** What one remote call addresses. */
|
|
54
|
+
export interface SshRemoteRef {
|
|
55
|
+
/** `dshSsh` host alias. */
|
|
56
|
+
alias: string
|
|
57
|
+
/** Absolute remote state directory the provider owns on that host. */
|
|
58
|
+
stateDir: string
|
|
59
|
+
/** Run the call addresses. */
|
|
60
|
+
runId: string
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The files one run's start writes before the launcher runs it. */
|
|
64
|
+
export interface SshRemoteRunFiles {
|
|
65
|
+
/** Prompt text the agent reads. */
|
|
66
|
+
prompt: string
|
|
67
|
+
/** Quoted command line the launcher executes. */
|
|
68
|
+
command: SshAgentCommand
|
|
69
|
+
/** Absolute remote directory the run works in. */
|
|
70
|
+
cwd: string
|
|
71
|
+
/** Opaque spec the provider keeps for its own reconciliation. */
|
|
72
|
+
spec: unknown
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A remote command that could not be completed, in the host's own words. */
|
|
76
|
+
export class SshRemoteError extends Error {
|
|
77
|
+
/** Host the command addressed. */
|
|
78
|
+
readonly alias: string
|
|
79
|
+
/** What the command was for. */
|
|
80
|
+
readonly what: string
|
|
81
|
+
|
|
82
|
+
constructor(alias: string, what: string, message: string) {
|
|
83
|
+
super(`remote ${what} on ${alias} failed: ${message}`)
|
|
84
|
+
this.name = 'SshRemoteError'
|
|
85
|
+
this.alias = alias
|
|
86
|
+
this.what = what
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** One page of a run's log, as the read verb returned it. */
|
|
91
|
+
export interface SshTailPage {
|
|
92
|
+
/** Bytes the remote log held from the requested offset to its end, as text. */
|
|
93
|
+
text: string
|
|
94
|
+
/** Byte offset the next read starts at. */
|
|
95
|
+
bytes: number
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Options of {@link SshRemote}. */
|
|
99
|
+
export interface SshRemoteOptions {
|
|
100
|
+
/** Ceiling for one remote command, in milliseconds. */
|
|
101
|
+
timeoutMs?: number
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Bound a remote message before it reaches a card, a log line or a summary. */
|
|
105
|
+
function bounded(text: string): string {
|
|
106
|
+
const trimmed = text.trim()
|
|
107
|
+
return trimmed.length <= ERROR_TEXT_MAX ? trimmed : trimmed.slice(0, ERROR_TEXT_MAX) + '...'
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export class SshRemote {
|
|
111
|
+
private readonly ssh: DshSshService
|
|
112
|
+
private readonly timeoutMs: number
|
|
113
|
+
private readonly stateDirs = new Map<string, string>()
|
|
114
|
+
private readonly starting = new Map<string, Promise<LauncherStatus>>()
|
|
115
|
+
private readonly installing = new Map<string, Promise<void>>()
|
|
116
|
+
|
|
117
|
+
constructor(ssh: DshSshService, options: SshRemoteOptions = {}) {
|
|
118
|
+
this.ssh = ssh
|
|
119
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_COMMAND_TIMEOUT_MS
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Forget every cached path of one host, or of all hosts when none is named. */
|
|
123
|
+
forget(alias?: string): void {
|
|
124
|
+
if (alias === undefined) {
|
|
125
|
+
this.stateDirs.clear()
|
|
126
|
+
this.installing.clear()
|
|
127
|
+
} else {
|
|
128
|
+
this.stateDirs.delete(alias)
|
|
129
|
+
for (const key of this.installing.keys()) {
|
|
130
|
+
if (key.startsWith(alias + '/')) this.installing.delete(key)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Resolve (and cache) the absolute state directory of one host.
|
|
137
|
+
* @param alias - host alias.
|
|
138
|
+
* @returns the directory the provider installs the launcher and its runs under.
|
|
139
|
+
*/
|
|
140
|
+
async stateDir(alias: string): Promise<string> {
|
|
141
|
+
const cached = this.stateDirs.get(alias)
|
|
142
|
+
if (cached !== undefined) return cached
|
|
143
|
+
const result = await this.ssh.exec(alias, 'printf %s "$HOME"', { timeoutMs: this.timeoutMs })
|
|
144
|
+
const home = result.success ? result.stdout.trim() : ''
|
|
145
|
+
if (home === '' || !home.startsWith('/')) {
|
|
146
|
+
throw new SshRemoteError(alias, 'home lookup', result.error ?? (result.stderr.trim() || 'the host reported no home directory'))
|
|
147
|
+
}
|
|
148
|
+
const stateDir = stateDirForHome(home)
|
|
149
|
+
this.stateDirs.set(alias, stateDir)
|
|
150
|
+
return stateDir
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Install (or refresh) the launcher script on one host, deduplicating
|
|
155
|
+
* concurrent installs on the same host and state directory.
|
|
156
|
+
* @param ref - host and state directory.
|
|
157
|
+
*/
|
|
158
|
+
async installLauncher(ref: { alias: string; stateDir: string }): Promise<void> {
|
|
159
|
+
const key = ref.alias + '/' + ref.stateDir
|
|
160
|
+
const inflight = this.installing.get(key)
|
|
161
|
+
if (inflight !== undefined) return await inflight
|
|
162
|
+
const task = (async (): Promise<void> => {
|
|
163
|
+
try {
|
|
164
|
+
await this.run(ref.alias, buildLauncherInstallCommand(ref.stateDir), 'launcher install')
|
|
165
|
+
} finally {
|
|
166
|
+
this.installing.delete(key)
|
|
167
|
+
}
|
|
168
|
+
})()
|
|
169
|
+
this.installing.set(key, task)
|
|
170
|
+
return await task
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Write every file one run needs before `start`.
|
|
175
|
+
*
|
|
176
|
+
* Large prompts are uploaded through SFTP instead of inlined into a single
|
|
177
|
+
* shell command line so multi-megabyte prompts never trip Linux `E2BIG`.
|
|
178
|
+
* @param ref - host, state directory and run.
|
|
179
|
+
* @param files - prompt, command line, working directory and spec.
|
|
180
|
+
*/
|
|
181
|
+
async writeRunFiles(ref: SshRemoteRef, files: SshRemoteRunFiles): Promise<void> {
|
|
182
|
+
const commands = buildRunFileCommands({
|
|
183
|
+
stateDir: ref.stateDir,
|
|
184
|
+
runId: ref.runId,
|
|
185
|
+
prompt: files.prompt,
|
|
186
|
+
command: files.command,
|
|
187
|
+
cwd: files.cwd,
|
|
188
|
+
spec: files.spec,
|
|
189
|
+
})
|
|
190
|
+
const combined = commands.join(' && ')
|
|
191
|
+
if (new TextEncoder().encode(combined).length <= INLINE_PROMPT_COMMAND_LIMIT_BYTES) {
|
|
192
|
+
await this.run(ref.alias, combined, 'run file write')
|
|
193
|
+
return
|
|
194
|
+
}
|
|
195
|
+
const directory = runDirectory(ref.stateDir, ref.runId)
|
|
196
|
+
const metadataCommands = [
|
|
197
|
+
shellEnsureDir(directory),
|
|
198
|
+
shellWriteFile(runFilePath(ref.stateDir, ref.runId, 'cmd.sh'), agentScript(files.command)),
|
|
199
|
+
shellWriteFile(runFilePath(ref.stateDir, ref.runId, 'cwd'), files.cwd),
|
|
200
|
+
shellWriteFile(runFilePath(ref.stateDir, ref.runId, 'spec.json'), JSON.stringify(files.spec)),
|
|
201
|
+
]
|
|
202
|
+
await this.run(ref.alias, metadataCommands.join(' && '), 'run file write')
|
|
203
|
+
const scratch = await mkdtemp(join(tmpdir(), 'dsh-task-board-ssh-prompt-'))
|
|
204
|
+
try {
|
|
205
|
+
const localPrompt = join(scratch, 'prompt.txt')
|
|
206
|
+
await writeFile(localPrompt, files.prompt, 'utf8')
|
|
207
|
+
await this.ssh.upload(ref.alias, localPrompt, runFilePath(ref.stateDir, ref.runId, 'prompt.txt'))
|
|
208
|
+
} catch (error) {
|
|
209
|
+
throw new SshRemoteError(ref.alias, 'prompt upload', error instanceof Error ? error.message : String(error))
|
|
210
|
+
} finally {
|
|
211
|
+
await rm(scratch, { recursive: true, force: true })
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Ask the launcher to start one run, joining a start already in flight.
|
|
217
|
+
* @param ref - host, state directory and run.
|
|
218
|
+
* @returns the state line the launcher printed.
|
|
219
|
+
*/
|
|
220
|
+
async start(ref: SshRemoteRef): Promise<LauncherStatus> {
|
|
221
|
+
const key = ref.alias + '/' + ref.runId
|
|
222
|
+
const inflight = this.starting.get(key)
|
|
223
|
+
if (inflight !== undefined) return await inflight
|
|
224
|
+
const started = (async (): Promise<LauncherStatus> => {
|
|
225
|
+
try {
|
|
226
|
+
const result = await this.run(ref.alias, buildLauncherCommand({
|
|
227
|
+
stateDir: ref.stateDir,
|
|
228
|
+
verb: 'start',
|
|
229
|
+
runId: ref.runId,
|
|
230
|
+
}), 'start')
|
|
231
|
+
return parseLauncherStatus(result.stdout)
|
|
232
|
+
} finally {
|
|
233
|
+
this.starting.delete(key)
|
|
234
|
+
}
|
|
235
|
+
})()
|
|
236
|
+
this.starting.set(key, started)
|
|
237
|
+
return await started
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Read one run's state.
|
|
242
|
+
* @param ref - host, state directory and run.
|
|
243
|
+
* @returns the state line the launcher printed.
|
|
244
|
+
*/
|
|
245
|
+
async status(ref: SshRemoteRef): Promise<LauncherStatus> {
|
|
246
|
+
const result = await this.run(ref.alias, buildLauncherCommand({
|
|
247
|
+
stateDir: ref.stateDir,
|
|
248
|
+
verb: 'status',
|
|
249
|
+
runId: ref.runId,
|
|
250
|
+
}), 'status')
|
|
251
|
+
return parseLauncherStatus(result.stdout)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Read a run's log from a byte offset, optionally capped to `limit` bytes.
|
|
256
|
+
* @param ref - host, state directory and run.
|
|
257
|
+
* @param offset - first byte to return.
|
|
258
|
+
* @param limit - optional maximum byte count to return (prevents hitting dsh-ssh's 2 MiB output cap).
|
|
259
|
+
* @returns the text and the offset the next read starts at.
|
|
260
|
+
*/
|
|
261
|
+
async tail(ref: SshRemoteRef, offset: number, limit?: number): Promise<SshTailPage> {
|
|
262
|
+
const result = await this.run(ref.alias, buildLauncherCommand({
|
|
263
|
+
stateDir: ref.stateDir,
|
|
264
|
+
verb: 'tail',
|
|
265
|
+
runId: ref.runId,
|
|
266
|
+
offset,
|
|
267
|
+
...(limit === undefined ? {} : { limit }),
|
|
268
|
+
}), 'tail')
|
|
269
|
+
const text = result.stdout
|
|
270
|
+
return { text, bytes: offset + new TextEncoder().encode(text).length }
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Read the bounded tail of a run's `stderr.log`, for diagnosing failures when
|
|
275
|
+
* `stdout.log` carries no structured agent event.
|
|
276
|
+
* @param ref - host, state directory and run.
|
|
277
|
+
* @param limit - maximum bytes from the end of `stderr.log`.
|
|
278
|
+
* @returns trimmed stderr text, or empty string when unavailable.
|
|
279
|
+
*/
|
|
280
|
+
async stderr(ref: SshRemoteRef, limit = 4096): Promise<string> {
|
|
281
|
+
try {
|
|
282
|
+
const result = await this.run(ref.alias, buildLauncherCommand({
|
|
283
|
+
stateDir: ref.stateDir,
|
|
284
|
+
verb: 'stderr',
|
|
285
|
+
runId: ref.runId,
|
|
286
|
+
limit,
|
|
287
|
+
}), 'stderr')
|
|
288
|
+
return result.stdout.trim()
|
|
289
|
+
} catch {
|
|
290
|
+
return ''
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Ask the launcher to cancel one run.
|
|
296
|
+
* @param ref - host, state directory and run.
|
|
297
|
+
* @returns the state line the launcher printed.
|
|
298
|
+
*/
|
|
299
|
+
async cancel(ref: SshRemoteRef): Promise<LauncherStatus> {
|
|
300
|
+
const result = await this.run(ref.alias, buildLauncherCommand({
|
|
301
|
+
stateDir: ref.stateDir,
|
|
302
|
+
verb: 'cancel',
|
|
303
|
+
runId: ref.runId,
|
|
304
|
+
}), 'cancel', 15_000)
|
|
305
|
+
return parseLauncherStatus(result.stdout)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Remove one run directory from the host, tolerating its absence.
|
|
310
|
+
* @param ref - host, state directory and run.
|
|
311
|
+
*/
|
|
312
|
+
async removeRun(ref: SshRemoteRef): Promise<void> {
|
|
313
|
+
await this.run(ref.alias, shellRemoveDir(runDirectory(ref.stateDir, ref.runId)), 'run directory removal')
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** Run one command and refuse a failed one in the host's own words. */
|
|
317
|
+
private async run(alias: string, command: string, what: string, timeoutMs = this.timeoutMs): Promise<{ stdout: string }> {
|
|
318
|
+
const result = await this.ssh.exec(alias, command, { timeoutMs })
|
|
319
|
+
if (result.success) return { stdout: result.stdout }
|
|
320
|
+
const detail = result.error ?? result.stderr.trim()
|
|
321
|
+
const reason = detail !== ''
|
|
322
|
+
? bounded(detail)
|
|
323
|
+
: result.timedOut
|
|
324
|
+
? `it did not finish within ${String(timeoutMs)} ms`
|
|
325
|
+
: `the command exited with code ${String(result.exitCode ?? 'none')}`
|
|
326
|
+
throw new SshRemoteError(alias, what, reason)
|
|
327
|
+
}
|
|
328
|
+
}
|