@linxin666/dsh-client-ui-task-board-ssh 0.4.20 → 0.4.21
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.i18n.yaml +2 -2
- package/README.md +13 -2
- package/README.zh.md +13 -2
- package/lib/client.js +9 -3
- package/lib/index.js +1071 -89
- package/lib/types/core/contract.d.ts +8 -0
- package/lib/types/core/target.d.ts +6 -0
- package/lib/types/core/task-record.d.ts +11 -0
- package/lib/types/host/board.d.ts +4 -0
- package/lib/types/host/engine.d.ts +145 -0
- package/lib/types/host/extension.d.ts +2 -0
- package/lib/types/host/inspect.d.ts +99 -0
- package/lib/types/host/remote.d.ts +17 -0
- package/lib/types/host/tools.d.ts +5 -3
- package/package.json +1 -1
- package/src/core/contract.ts +5 -0
- package/src/core/ledger.ts +5 -0
- package/src/core/target.ts +10 -3
- package/src/core/task-record.ts +8 -0
- package/src/host/board.ts +4 -0
- package/src/host/engine.ts +430 -6
- package/src/host/extension.ts +6 -0
- package/src/host/inspect.ts +264 -0
- package/src/host/remote.ts +28 -0
- package/src/host/tools.ts +250 -60
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an agent asks about a remote run before and while it runs: whether the
|
|
3
|
+
* target can work at all (preflight), what the log says without its noise
|
|
4
|
+
* (filtered log query), and what the agent did last (activity).
|
|
5
|
+
*
|
|
6
|
+
* Every command here is read-only and composed through `shellQuote`; none of it
|
|
7
|
+
* touches the launcher script, so the remote side of a run is unchanged.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-task-board-ssh/host/inspect
|
|
10
|
+
*/
|
|
11
|
+
import { shellQuote } from '../core/shell.ts'
|
|
12
|
+
import type { SshAgentTarget, SshTarget } from '../core/target.ts'
|
|
13
|
+
|
|
14
|
+
/** Stable refusal codes a target check or a run can answer with. */
|
|
15
|
+
export const SSH_PREFLIGHT_CODES = ['host-unreachable', 'agent-missing', 'path-missing', 'permission-required'] as const
|
|
16
|
+
|
|
17
|
+
/** One stable preflight code. */
|
|
18
|
+
export type SshPreflightCode = (typeof SSH_PREFLIGHT_CODES)[number]
|
|
19
|
+
|
|
20
|
+
/** A target check that failed, in words the caller can act on. */
|
|
21
|
+
export interface SshPreflightFailure {
|
|
22
|
+
code: SshPreflightCode
|
|
23
|
+
message: string
|
|
24
|
+
/** The next step, one sentence. */
|
|
25
|
+
hint: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Program a built-in agent kind starts on the host, or undefined when it is configured. */
|
|
29
|
+
export function agentProgram(agent: SshAgentTarget, override?: string): string | undefined {
|
|
30
|
+
if (override !== undefined) return override
|
|
31
|
+
switch (agent.kind) {
|
|
32
|
+
case 'dsh': return 'dsh'
|
|
33
|
+
case 'claude': return 'claude'
|
|
34
|
+
case 'codex': return 'codex'
|
|
35
|
+
default: {
|
|
36
|
+
// A custom template starts with the program; a placeholder-led one has none to probe.
|
|
37
|
+
const first = agent.command?.trim().split(/\s+/)[0]
|
|
38
|
+
return first === undefined || first === '' || first.includes('{') || first.includes('$') ? undefined : first
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The single remote command one preflight runs: it always exits zero and prints
|
|
45
|
+
* one `key=value` per fact, so a failed channel is told apart from a failed check.
|
|
46
|
+
*/
|
|
47
|
+
export function preflightCommand(target: SshTarget, program: string | undefined): string {
|
|
48
|
+
const parts = ['echo reach=ok']
|
|
49
|
+
if (program !== undefined) {
|
|
50
|
+
parts.push(`if command -v ${shellQuote(program)} >/dev/null 2>&1; then echo agent=ok; else echo agent=missing; fi`)
|
|
51
|
+
}
|
|
52
|
+
if (target.workspace.kind === 'in-place') {
|
|
53
|
+
const path = shellQuote(target.workspace.path)
|
|
54
|
+
parts.push(`if [ -d ${path} ]; then echo path=ok; if [ -w ${path} ] && [ -x ${path} ]; then echo write=ok; else echo write=denied; fi; else echo path=missing; fi`)
|
|
55
|
+
}
|
|
56
|
+
return parts.join('; ')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Read one preflight answer.
|
|
61
|
+
* @param target - the checked target.
|
|
62
|
+
* @param program - the probed program, if any.
|
|
63
|
+
* @param result - what `exec` returned.
|
|
64
|
+
* @returns the first failed check, or undefined when the target can work.
|
|
65
|
+
*/
|
|
66
|
+
export function readPreflight(
|
|
67
|
+
target: SshTarget,
|
|
68
|
+
program: string | undefined,
|
|
69
|
+
result: { success: boolean; stdout: string; stderr: string; error?: string; timedOut?: boolean },
|
|
70
|
+
): SshPreflightFailure | undefined {
|
|
71
|
+
const facts = new Map<string, string>()
|
|
72
|
+
for (const line of result.stdout.split('\n')) {
|
|
73
|
+
const at = line.indexOf('=')
|
|
74
|
+
if (at > 0) facts.set(line.slice(0, at).trim(), line.slice(at + 1).trim())
|
|
75
|
+
}
|
|
76
|
+
if (facts.get('reach') !== 'ok') {
|
|
77
|
+
const why = result.error ?? (result.stderr.trim() || (result.timedOut === true ? 'the connection timed out' : 'no answer'))
|
|
78
|
+
return {
|
|
79
|
+
code: 'host-unreachable',
|
|
80
|
+
message: `host "${target.host}" did not answer an ssh command: ${why.slice(0, 200)}`,
|
|
81
|
+
hint: 'Check the alias and credentials in the dsh-ssh settings (or use task_board_ssh action "hosts" to list the aliases), then retry.',
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (program !== undefined && facts.get('agent') !== 'ok') {
|
|
85
|
+
return {
|
|
86
|
+
code: 'agent-missing',
|
|
87
|
+
message: `"${program}" is not on the PATH of host "${target.host}"`,
|
|
88
|
+
hint: `Install ${program} there, or pick another agent (dsh, claude, codex, custom).`,
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (target.workspace.kind === 'in-place') {
|
|
92
|
+
if (facts.get('path') !== 'ok') {
|
|
93
|
+
return {
|
|
94
|
+
code: 'path-missing',
|
|
95
|
+
message: `directory ${target.workspace.path} does not exist on host "${target.host}"; an in-place target never creates it`,
|
|
96
|
+
hint: 'Create the directory on the host first, or use workspaceKind "git" to get a per-run worktree.',
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (facts.get('write') !== 'ok') {
|
|
100
|
+
return {
|
|
101
|
+
code: 'permission-required',
|
|
102
|
+
message: `directory ${target.workspace.path} on host "${target.host}" is not writable by the ssh user`,
|
|
103
|
+
hint: 'Grant that user write access (or pick a directory it owns), then retry.',
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return undefined
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// --- filtered log ---------------------------------------------------------------
|
|
111
|
+
|
|
112
|
+
/** Longest line a filtered log returns, in bytes; longer ones are cut and marked. */
|
|
113
|
+
export const LOG_LINE_MAX_BYTES = 2_000
|
|
114
|
+
|
|
115
|
+
/** Default and ceiling of one log reply, in bytes. */
|
|
116
|
+
export const LOG_REPLY_DEFAULT_BYTES = 64 * 1024
|
|
117
|
+
|
|
118
|
+
/** Lines `tail` returns when the caller names neither `tail` nor `since`. */
|
|
119
|
+
export const LOG_TAIL_DEFAULT_LINES = 40
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* awk program that drops what an agent never needs from a stream-json log:
|
|
123
|
+
* every `system` event except `init` (thinking_tokens, hooks, status), rate
|
|
124
|
+
* limit notices and partial stream deltas. It also cuts overlong lines.
|
|
125
|
+
*/
|
|
126
|
+
const NOISE_FILTER = [
|
|
127
|
+
`/^\\{"type":"(stream_event|rate_limit_event)"/ {next}`,
|
|
128
|
+
`/^\\{"type":"system","subtype":"init"/ {print; next}`,
|
|
129
|
+
`/^\\{"type":"system"/ {next}`,
|
|
130
|
+
`{ if (length($0) > ${String(LOG_LINE_MAX_BYTES)}) $0 = substr($0, 1, ${String(LOG_LINE_MAX_BYTES)}) " ...[line cut]"; print }`,
|
|
131
|
+
].join(' ')
|
|
132
|
+
|
|
133
|
+
/** What one log query asks for. */
|
|
134
|
+
export interface SshLogQuery {
|
|
135
|
+
/** Absolute path of the run's stdout log. */
|
|
136
|
+
file: string
|
|
137
|
+
/** Last N filtered lines (default 40). Ignored when `since` is given. */
|
|
138
|
+
tail?: number
|
|
139
|
+
/** First filtered line to return, 1-based: the `nextSince` of an earlier reply. */
|
|
140
|
+
since?: number
|
|
141
|
+
/** Skip the filter and read raw lines. */
|
|
142
|
+
raw?: boolean
|
|
143
|
+
/** Cap on the reply text, in bytes. */
|
|
144
|
+
maxBytes?: number
|
|
145
|
+
/** Count only: no text. */
|
|
146
|
+
statsOnly?: boolean
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Counts of one log, before any text is cost. */
|
|
150
|
+
export interface SshLogStats {
|
|
151
|
+
/** Size of the raw log on the host. */
|
|
152
|
+
rawBytes: number
|
|
153
|
+
/** Lines the filter keeps. */
|
|
154
|
+
lines: number
|
|
155
|
+
/** Bytes the kept lines take. */
|
|
156
|
+
bytes: number
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The command that answers one log query, text after a `stats` line. */
|
|
160
|
+
export function logCommand(query: SshLogQuery): string {
|
|
161
|
+
const file = shellQuote(query.file)
|
|
162
|
+
const filter = query.raw === true ? 'cat' : `LC_ALL=C awk ${shellQuote(NOISE_FILTER)}`
|
|
163
|
+
const count = `LC_ALL=C awk '{ l += 1; b += length($0) + 1 } END { printf "%d %d", l, b }'`
|
|
164
|
+
const cap = Math.max(1, Math.min(Math.trunc(query.maxBytes ?? LOG_REPLY_DEFAULT_BYTES), 128 * 1024))
|
|
165
|
+
const select = query.since !== undefined
|
|
166
|
+
? `tail -n +${String(Math.max(1, Math.trunc(query.since)))}`
|
|
167
|
+
: `tail -n ${String(Math.max(1, Math.min(Math.trunc(query.tail ?? LOG_TAIL_DEFAULT_LINES), 2_000)))}`
|
|
168
|
+
const body = query.statsOnly === true ? '' : `; ${filter} ${file} | ${select} | head -c ${String(cap + 1)}`
|
|
169
|
+
return `if [ ! -f ${file} ]; then echo "stats none"; exit 0; fi; printf 'stats %s ' "$(wc -c < ${file} | tr -d ' ')"; ${filter} ${file} | ${count}; echo${body}`
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** One answered log query. */
|
|
173
|
+
export interface SshLogAnswer {
|
|
174
|
+
stats: SshLogStats | undefined
|
|
175
|
+
/** Filtered text, complete lines only, within the cap. */
|
|
176
|
+
text: string
|
|
177
|
+
/** Filtered lines in `text`. */
|
|
178
|
+
lines: number
|
|
179
|
+
/** Whether lines remain after `text`. */
|
|
180
|
+
more: boolean
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Parse the answer of {@link logCommand}.
|
|
185
|
+
* @param stdout - what the command printed.
|
|
186
|
+
* @param query - the query it answered.
|
|
187
|
+
* @returns stats and the complete lines that fit the cap.
|
|
188
|
+
*/
|
|
189
|
+
export function readLogAnswer(stdout: string, query: SshLogQuery): SshLogAnswer {
|
|
190
|
+
const newline = stdout.indexOf('\n')
|
|
191
|
+
const head = newline === -1 ? stdout : stdout.slice(0, newline)
|
|
192
|
+
const rest = newline === -1 ? '' : stdout.slice(newline + 1)
|
|
193
|
+
const match = /^stats (\d+) (\d+) (\d+)$/.exec(head.trim())
|
|
194
|
+
const stats = match === null
|
|
195
|
+
? undefined
|
|
196
|
+
: { rawBytes: Number(match[1]), lines: Number(match[2]), bytes: Number(match[3]) }
|
|
197
|
+
const cap = Math.max(1, Math.min(Math.trunc(query.maxBytes ?? LOG_REPLY_DEFAULT_BYTES), 128 * 1024))
|
|
198
|
+
let text = rest
|
|
199
|
+
let cut = false
|
|
200
|
+
if (new TextEncoder().encode(text).length > cap) {
|
|
201
|
+
cut = true
|
|
202
|
+
text = text.slice(0, text.lastIndexOf('\n') + 1)
|
|
203
|
+
while (new TextEncoder().encode(text).length > cap) text = text.slice(0, text.slice(0, -1).lastIndexOf('\n') + 1)
|
|
204
|
+
}
|
|
205
|
+
const lines = text === '' ? 0 : text.split('\n').filter((line, index, all) => line !== '' || index < all.length - 1).length
|
|
206
|
+
const firstLine = query.since ?? Math.max(1, (stats?.lines ?? 0) - Math.trunc(query.tail ?? LOG_TAIL_DEFAULT_LINES) + 1)
|
|
207
|
+
const more = cut || (stats !== undefined && firstLine + lines - 1 < stats.lines)
|
|
208
|
+
return { stats, text, lines, more }
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** First filtered line number a tail of `tail` lines starts at, given the totals. */
|
|
212
|
+
export function tailStart(stats: SshLogStats, tail: number): number {
|
|
213
|
+
return Math.max(1, stats.lines - Math.max(1, tail) + 1)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// --- last activity --------------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
/** The agent's last visible action: a tool call or a message. */
|
|
219
|
+
export interface SshActivity {
|
|
220
|
+
/** `tool_use`, `assistant` or `result`. */
|
|
221
|
+
kind: string
|
|
222
|
+
/** Tool name for a tool call. */
|
|
223
|
+
name?: string
|
|
224
|
+
/** One line of what it was given or said. */
|
|
225
|
+
detail: string
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** One short line out of a tool input. */
|
|
229
|
+
function inputLine(input: unknown): string {
|
|
230
|
+
if (typeof input !== 'object' || input === null) return ''
|
|
231
|
+
const row = input as Record<string, unknown>
|
|
232
|
+
for (const key of ['command', 'file_path', 'path', 'pattern', 'url', 'description', 'prompt']) {
|
|
233
|
+
const value = row[key]
|
|
234
|
+
if (typeof value === 'string' && value !== '') return value.replace(/\s+/g, ' ').slice(0, 160)
|
|
235
|
+
}
|
|
236
|
+
return JSON.stringify(row).slice(0, 160)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The last tool call (or message) in a stretch of stream-json lines.
|
|
241
|
+
* @param text - filtered log lines, oldest first.
|
|
242
|
+
* @returns the last action, or undefined when none parses.
|
|
243
|
+
*/
|
|
244
|
+
export function lastActivity(text: string): SshActivity | undefined {
|
|
245
|
+
const lines = text.split('\n').filter(line => line.trim() !== '')
|
|
246
|
+
for (let index = lines.length - 1; index >= 0; index -= 1) {
|
|
247
|
+
let event: unknown
|
|
248
|
+
try { event = JSON.parse(lines[index] as string) } catch { continue }
|
|
249
|
+
if (typeof event !== 'object' || event === null) continue
|
|
250
|
+
const row = event as Record<string, unknown>
|
|
251
|
+
if (row.type === 'result') return { kind: 'result', detail: String(row.subtype ?? 'result') }
|
|
252
|
+
if (row.type !== 'assistant') continue
|
|
253
|
+
const message = row.message as { content?: unknown } | undefined
|
|
254
|
+
if (!Array.isArray(message?.content)) continue
|
|
255
|
+
for (let at = message.content.length - 1; at >= 0; at -= 1) {
|
|
256
|
+
const block = message.content[at] as Record<string, unknown>
|
|
257
|
+
if (block.type === 'tool_use') return { kind: 'tool_use', name: String(block.name ?? ''), detail: inputLine(block.input) }
|
|
258
|
+
if (block.type === 'text' && typeof block.text === 'string' && block.text.trim() !== '') {
|
|
259
|
+
return { kind: 'assistant', detail: block.text.trim().replace(/\s+/g, ' ').slice(0, 160) }
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return undefined
|
|
264
|
+
}
|
package/src/host/remote.ts
CHANGED
|
@@ -119,6 +119,11 @@ export class SshRemote {
|
|
|
119
119
|
this.timeoutMs = options.timeoutMs ?? DEFAULT_COMMAND_TIMEOUT_MS
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
+
/** The state directory of a host when it was already resolved, without a round trip. */
|
|
123
|
+
cachedStateDir(alias: string): string | undefined {
|
|
124
|
+
return this.stateDirs.get(alias)
|
|
125
|
+
}
|
|
126
|
+
|
|
122
127
|
/** Forget every cached path of one host, or of all hosts when none is named. */
|
|
123
128
|
forget(alias?: string): void {
|
|
124
129
|
if (alias === undefined) {
|
|
@@ -313,6 +318,29 @@ export class SshRemote {
|
|
|
313
318
|
await this.run(ref.alias, shellRemoveDir(runDirectory(ref.stateDir, ref.runId)), 'run directory removal')
|
|
314
319
|
}
|
|
315
320
|
|
|
321
|
+
/**
|
|
322
|
+
* Run one read-only diagnostic command and hand back the raw result, whether
|
|
323
|
+
* or not it succeeded: the caller decides what a failure means.
|
|
324
|
+
* @param alias - host alias.
|
|
325
|
+
* @param command - command line, already quoted.
|
|
326
|
+
* @param timeoutMs - ceiling for this command.
|
|
327
|
+
* @returns what the forwarder reported; a thrown forwarder becomes a failed result.
|
|
328
|
+
*/
|
|
329
|
+
async probe(alias: string, command: string, timeoutMs = this.timeoutMs): Promise<{ success: boolean; stdout: string; stderr: string; error?: string; timedOut: boolean }> {
|
|
330
|
+
try {
|
|
331
|
+
const result = await this.ssh.exec(alias, command, { timeoutMs })
|
|
332
|
+
return {
|
|
333
|
+
success: result.success,
|
|
334
|
+
stdout: result.stdout,
|
|
335
|
+
stderr: result.stderr,
|
|
336
|
+
timedOut: result.timedOut,
|
|
337
|
+
...(result.error === undefined ? {} : { error: result.error }),
|
|
338
|
+
}
|
|
339
|
+
} catch (error) {
|
|
340
|
+
return { success: false, stdout: '', stderr: '', timedOut: false, error: error instanceof Error ? error.message : String(error) }
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
316
344
|
/** Run one command and refuse a failed one in the host's own words. */
|
|
317
345
|
private async run(alias: string, command: string, what: string, timeoutMs = this.timeoutMs): Promise<{ stdout: string }> {
|
|
318
346
|
const result = await this.ssh.exec(alias, command, { timeoutMs })
|