@linxin666/dsh-client-ui-task-board-ssh 0.0.0-stage → 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.
Files changed (128) hide show
  1. package/LICENSE +202 -0
  2. package/README.i18n.yaml +6 -0
  3. package/README.md +96 -2
  4. package/README.zh.md +97 -0
  5. package/cordis.patch.yml +15 -0
  6. package/icon.svg +5 -0
  7. package/lib/client.js +4435 -0
  8. package/lib/index.js +7082 -0
  9. package/lib/types/client/PluginSettingsCard.d.ts +255 -0
  10. package/lib/types/client/RangeBubble.d.ts +46 -0
  11. package/lib/types/client/SegmentGroup.d.ts +28 -0
  12. package/lib/types/client/index.d.ts +100 -0
  13. package/lib/types/client/locales.d.ts +125 -0
  14. package/lib/types/client/seat-props.d.ts +53 -0
  15. package/lib/types/client/settings-entry-form.d.ts +49 -0
  16. package/lib/types/client/settings-form.d.ts +237 -0
  17. package/lib/types/client/ssh/card-decoration.d.ts +23 -0
  18. package/lib/types/client/ssh/compose-section.d.ts +7 -0
  19. package/lib/types/client/ssh/detail-section.d.ts +7 -0
  20. package/lib/types/client/ssh/extension.d.ts +54 -0
  21. package/lib/types/client/ssh/log-buffer.d.ts +128 -0
  22. package/lib/types/client/ssh/log-view.d.ts +37 -0
  23. package/lib/types/client/ssh/model.d.ts +130 -0
  24. package/lib/types/client/ssh/settings-section.d.ts +134 -0
  25. package/lib/types/client/ssh/summary.d.ts +160 -0
  26. package/lib/types/client/ssh/target-form.d.ts +60 -0
  27. package/lib/types/core/action.d.ts +89 -0
  28. package/lib/types/core/adapters/claude.d.ts +15 -0
  29. package/lib/types/core/adapters/codex.d.ts +15 -0
  30. package/lib/types/core/adapters/custom.d.ts +29 -0
  31. package/lib/types/core/adapters/dsh.d.ts +21 -0
  32. package/lib/types/core/adapters/index.d.ts +25 -0
  33. package/lib/types/core/adapters/types.d.ts +76 -0
  34. package/lib/types/core/contract.d.ts +453 -0
  35. package/lib/types/core/launcher-script.d.ts +13 -0
  36. package/lib/types/core/launcher.d.ts +153 -0
  37. package/lib/types/core/ledger.d.ts +123 -0
  38. package/lib/types/core/shell.d.ts +61 -0
  39. package/lib/types/core/target.d.ts +149 -0
  40. package/lib/types/core/task-record.d.ts +74 -0
  41. package/lib/types/core/workspace.d.ts +74 -0
  42. package/lib/types/host/board.d.ts +75 -0
  43. package/lib/types/host/engine.d.ts +563 -0
  44. package/lib/types/host/extension.d.ts +61 -0
  45. package/lib/types/host/inspect.d.ts +99 -0
  46. package/lib/types/host/logs.d.ts +135 -0
  47. package/lib/types/host/payload.d.ts +75 -0
  48. package/lib/types/host/prompt.d.ts +44 -0
  49. package/lib/types/host/queue.d.ts +53 -0
  50. package/lib/types/host/remote.d.ts +137 -0
  51. package/lib/types/host/settings.d.ts +149 -0
  52. package/lib/types/host/timers.d.ts +20 -0
  53. package/lib/types/host/tools.d.ts +17 -0
  54. package/lib/types/host/workspace/errors.d.ts +29 -0
  55. package/lib/types/host/workspace/git.d.ts +108 -0
  56. package/lib/types/host/workspace/in-place.d.ts +36 -0
  57. package/lib/types/host/workspace/index.d.ts +28 -0
  58. package/lib/types/host/workspace/layout.d.ts +62 -0
  59. package/lib/types/host/workspace/local-git.d.ts +72 -0
  60. package/lib/types/host/workspace/remote-git.d.ts +111 -0
  61. package/lib/types/host/workspace/remote.d.ts +85 -0
  62. package/lib/types/host/workspace/types.d.ts +28 -0
  63. package/lib/types/host/workspace-seam.d.ts +100 -0
  64. package/lib/types/index.d.ts +97 -0
  65. package/lib/types/mount-once.d.ts +55 -0
  66. package/locale/en.json +6 -0
  67. package/locale/zh.json +6 -0
  68. package/package.json +89 -3
  69. package/src/client/PluginSettingsCard.tsx +838 -0
  70. package/src/client/RangeBubble.tsx +164 -0
  71. package/src/client/SegmentGroup.tsx +93 -0
  72. package/src/client/css-modules.d.ts +5 -0
  73. package/src/client/index.ts +195 -0
  74. package/src/client/locales.ts +257 -0
  75. package/src/client/seat-props.ts +58 -0
  76. package/src/client/settings-card.module.css +821 -0
  77. package/src/client/settings-entry-form.ts +141 -0
  78. package/src/client/settings-form.ts +529 -0
  79. package/src/client/ssh/card-decoration.tsx +55 -0
  80. package/src/client/ssh/compose-section.tsx +147 -0
  81. package/src/client/ssh/detail-section.tsx +345 -0
  82. package/src/client/ssh/extension.ts +271 -0
  83. package/src/client/ssh/log-buffer.ts +224 -0
  84. package/src/client/ssh/log-view.tsx +149 -0
  85. package/src/client/ssh/model.ts +299 -0
  86. package/src/client/ssh/settings-section.tsx +401 -0
  87. package/src/client/ssh/summary.ts +388 -0
  88. package/src/client/ssh/target-form.tsx +380 -0
  89. package/src/client/ssh.module.css +617 -0
  90. package/src/core/action.ts +270 -0
  91. package/src/core/adapters/claude.ts +64 -0
  92. package/src/core/adapters/codex.ts +83 -0
  93. package/src/core/adapters/custom.ts +88 -0
  94. package/src/core/adapters/dsh.ts +87 -0
  95. package/src/core/adapters/index.ts +74 -0
  96. package/src/core/adapters/types.ts +95 -0
  97. package/src/core/contract.ts +515 -0
  98. package/src/core/launcher-script.ts +313 -0
  99. package/src/core/launcher.ts +283 -0
  100. package/src/core/ledger.ts +377 -0
  101. package/src/core/shell.ts +77 -0
  102. package/src/core/target.ts +420 -0
  103. package/src/core/task-record.ts +73 -0
  104. package/src/core/workspace.ts +78 -0
  105. package/src/host/board.ts +107 -0
  106. package/src/host/engine.ts +2026 -0
  107. package/src/host/extension.ts +310 -0
  108. package/src/host/inspect.ts +264 -0
  109. package/src/host/logs.ts +297 -0
  110. package/src/host/payload.ts +134 -0
  111. package/src/host/prompt.ts +89 -0
  112. package/src/host/queue.ts +73 -0
  113. package/src/host/remote.ts +356 -0
  114. package/src/host/settings.ts +294 -0
  115. package/src/host/timers.ts +33 -0
  116. package/src/host/tools.ts +449 -0
  117. package/src/host/workspace/errors.ts +43 -0
  118. package/src/host/workspace/git.ts +305 -0
  119. package/src/host/workspace/in-place.ts +58 -0
  120. package/src/host/workspace/index.ts +37 -0
  121. package/src/host/workspace/layout.ts +122 -0
  122. package/src/host/workspace/local-git.ts +164 -0
  123. package/src/host/workspace/remote-git.ts +244 -0
  124. package/src/host/workspace/remote.ts +181 -0
  125. package/src/host/workspace/types.ts +34 -0
  126. package/src/host/workspace-seam.ts +132 -0
  127. package/src/index.ts +243 -0
  128. package/src/mount-once.ts +142 -0
@@ -0,0 +1,449 @@
1
+ /**
2
+ * The model-visible tools this extension contributes: `task_board_ssh` (one
3
+ * surface: create, bind, read, run, status, log, followup) and the three older
4
+ * tools, kept as a compatibility layer over the same engine calls.
5
+ *
6
+ * They live with the provider — the board's own agent-tools module never learns
7
+ * SSH vocabulary — and they register through the extension capability face, so
8
+ * they follow the same board-enabled x extension-enabled gate as every other
9
+ * provider surface: switching either off withdraws all three.
10
+ *
11
+ * What a tool returns is a projection, never a host entry: an alias, an address,
12
+ * a port and a user are what a card needs to name its host, and a credential is
13
+ * not part of this surface at all (the `dshSsh` face carries none, and the
14
+ * projection here would drop one that appeared anyway).
15
+ *
16
+ * @module dsh-task-board-ssh/host/tools
17
+ */
18
+ import type { ContentBlock } from '@deepseek-ai/dsh-llm'
19
+ import { defineTool, type ToolDefinition } from '@deepseek-ai/dsh-tools'
20
+ import { TASK_BOARD_MESSAGE_MAX_LENGTH, type DshSshHost } from '../core/contract.ts'
21
+ import { SSH_AGENT_KINDS, parseSshTarget, type SshExecutionMode, type SshTarget } from '../core/target.ts'
22
+ import type { SshRunEngine } from './engine.ts'
23
+ import { SSH_PREFLIGHT_CODES } from './inspect.ts'
24
+
25
+ /** Tool names, in registration order; the first is the one surface, the rest are the compatibility layer. */
26
+ export const SSH_TOOL_NAMES = ['task_board_ssh', 'task_board_ssh_target', 'task_board_ssh_status', 'task_board_ssh_follow_up'] as const
27
+
28
+ /** One tool name. */
29
+ export type SshToolName = (typeof SSH_TOOL_NAMES)[number]
30
+
31
+ type Json = null | boolean | number | string | Json[] | { [key: string]: Json }
32
+
33
+ /** Model-facing JSON rendering shared by the three tools. */
34
+ function renderJson(_args: unknown, value: unknown): ContentBlock[] {
35
+ return [{ type: 'text' as const, text: JSON.stringify(value, null, 2) }]
36
+ }
37
+
38
+ /** Mark an already JSON-safe projection as the tool's canonical value. */
39
+ function json(value: unknown): Json {
40
+ return value as Json
41
+ }
42
+
43
+ /** A domain refusal the model is expected to read and act on. */
44
+ function refused(code: string, message: string): Json {
45
+ return json({ ok: false, code, message })
46
+ }
47
+
48
+ /** The message of anything thrown by a caller below. */
49
+ function messageOf(error: unknown): string {
50
+ return error instanceof Error ? error.message : String(error)
51
+ }
52
+
53
+ /**
54
+ * Project one host as a tool may report it.
55
+ *
56
+ * Written field by field on purpose: a deployment's host summary may grow a
57
+ * member tomorrow, and the model-facing surface must not grow it too by
58
+ * accident.
59
+ * @param host - the secret-free host the `dshSsh` face reports.
60
+ * @returns the projection.
61
+ */
62
+ function hostView(host: DshSshHost): Record<string, unknown> {
63
+ return {
64
+ alias: host.alias,
65
+ host: host.host,
66
+ port: host.port,
67
+ user: host.user,
68
+ tags: [...host.tags],
69
+ proxyJump: [...host.proxyJump],
70
+ forwardAgent: host.forwardAgent,
71
+ keyReady: host.keyReady,
72
+ ...(host.description === undefined ? {} : { description: host.description }),
73
+ ...(host.environment === undefined ? {} : { environment: host.environment }),
74
+ ...(host.location === undefined ? {} : { location: host.location }),
75
+ }
76
+ }
77
+
78
+ /** Every host this deployment can pin a card to, in the same projection. */
79
+ function hostViews(engine: SshRunEngine): Record<string, unknown>[] {
80
+ return engine.hosts().slice(0, 50).map(hostView)
81
+ }
82
+
83
+ /** Read one string parameter, or undefined when it is absent or blank. */
84
+ function text(value: unknown): string | undefined {
85
+ if (typeof value !== 'string') return undefined
86
+ const trimmed = value.trim()
87
+ return trimmed === '' ? undefined : trimmed
88
+ }
89
+
90
+ /** Read one number parameter, or undefined when it is absent or unusable. */
91
+ function count(value: unknown): number | undefined {
92
+ return typeof value === 'number' && Number.isFinite(value) ? Math.trunc(value) : undefined
93
+ }
94
+
95
+ /**
96
+ * Assemble a target out of the flat parameters a tool call carries.
97
+ * @param args - the tool arguments.
98
+ * @returns the candidate target, still to be validated by the parser.
99
+ */
100
+ function targetFromArgs(args: Record<string, unknown>): unknown {
101
+ const host = text(args.host) ?? ''
102
+ const mode = args.mode === undefined ? 'direct' : args.mode
103
+ const agentKind = args.agent === undefined ? 'dsh' : args.agent
104
+ const agentCommand = text(args.agentCommand)
105
+ const agentResumeCommand = text(args.agentResumeCommand)
106
+ const workspaceKind = args.workspaceKind === undefined ? 'in-place' : args.workspaceKind
107
+ let workspace: unknown
108
+ if (workspaceKind === 'git') {
109
+ workspace = text(args.repositoryUrl) !== undefined
110
+ ? {
111
+ kind: 'git',
112
+ source: {
113
+ kind: 'url',
114
+ url: text(args.repositoryUrl),
115
+ ...(text(args.repositoryBase) === undefined ? {} : { base: text(args.repositoryBase) }),
116
+ },
117
+ }
118
+ : { kind: 'git', source: { kind: 'workspace', workspaceId: text(args.workspaceId) ?? '' } }
119
+ } else if (workspaceKind === 'in-place') {
120
+ workspace = { kind: 'in-place', path: text(args.workspacePath) ?? '' }
121
+ } else {
122
+ workspace = { kind: workspaceKind }
123
+ }
124
+ const agent: Record<string, unknown> = { kind: agentKind }
125
+ if (agentCommand !== undefined) agent.command = agentCommand
126
+ if (agentResumeCommand !== undefined) agent.resumeCommand = agentResumeCommand
127
+ return {
128
+ mode,
129
+ host,
130
+ agent,
131
+ workspace,
132
+ }
133
+ }
134
+
135
+ /** What the error codes of this surface mean, shared by every tool description. */
136
+ const CODE_HELP = 'Stable refusal codes: host-unreachable (alias unknown to dsh-ssh or the host did not answer: fix the alias or the dsh-ssh credentials), agent-missing (the agent name is not one of dsh/claude/codex/custom, or its program is not on the host PATH), path-missing (an in-place directory does not exist; it is never created), permission-required (the directory exists but the ssh user cannot write it, or a human must confirm in the board UI), run-active (the card already has a run in flight: wait for it or cancel it), invalid-target (the other target fields are malformed; issues says which).'
137
+
138
+ /** The target parameters every binding entry shares. */
139
+ const TARGET_PARAMETERS = {
140
+ host: { type: 'string', description: 'dsh-ssh host alias the card addresses (read the available aliases with action "read").' },
141
+ mode: { type: 'string', enum: ['direct', 'proxy'], description: 'direct (default): a headless agent runs on the host, no local session, best for self-contained jobs; proxy: a local session drives the host through ssh_* tools, best when the work needs local context or tools.' },
142
+ // Deliberately not an enum: a bad name must reach the handler and come back as the stable agent-missing code, not as a schema error.
143
+ agent: { type: 'string', description: `Agent CLI the remote run starts: ${SSH_AGENT_KINDS.join(', ')} (default dsh; custom needs agentCommand). Its program must exist on the host; any other name is refused with agent-missing.` },
144
+ workspaceKind: { type: 'string', enum: ['in-place', 'git'], description: 'in-place (default): work directly inside the existing remote directory workspacePath, nothing is copied or cleaned up; git: a per-run worktree on its own branch dsh-task/<runId>, from repositoryUrl or the local repository workspaceId.' },
145
+ workspacePath: { type: 'string', description: 'Absolute remote directory of an in-place target. It must exist and be writable.' },
146
+ repositoryUrl: { type: 'string', description: 'Clone URL a git target prepares its worktree from, when the host can reach it.' },
147
+ repositoryBase: { type: 'string', description: 'Base branch that worktree starts from; the remote HEAD when absent.' },
148
+ workspaceId: { type: 'string', description: 'Local workspace id a git target runs from, when its repository is not reachable from the host.' },
149
+ agentCommand: { type: 'string', description: 'Start command template of a custom agent; must reference {prompt_file}.' },
150
+ agentResumeCommand: { type: 'string', description: 'Resume command template of a custom agent; without one a follow-up runs fresh.' },
151
+ preflight: { type: 'boolean', description: 'Check the host, the agent program and the directory before binding (default true).' },
152
+ } as const
153
+
154
+ /** Read a boolean argument; undefined when absent. */
155
+ function flag(value: unknown): boolean | undefined {
156
+ return typeof value === 'boolean' ? value : undefined
157
+ }
158
+
159
+ /**
160
+ * Parse the flat target arguments, mapping parser issues onto the stable codes
161
+ * the caller can act on: a bad host alias or agent name is a host/agent refusal,
162
+ * anything else stays `invalid-target`.
163
+ */
164
+ function parseTarget(args: Record<string, unknown>): { ok: true; target: SshTarget } | { ok: false; reply: Json } {
165
+ const parsed = parseSshTarget(targetFromArgs(args))
166
+ if (parsed.ok) return { ok: true, target: parsed.value }
167
+ const first = parsed.errors[0]
168
+ const issues = parsed.errors.map(issue => ({ path: issue.path, message: issue.message }))
169
+ if (first !== undefined && first.path === 'host') {
170
+ return { ok: false, reply: json({ ok: false, code: 'host-unreachable', message: `host ${JSON.stringify(text(args.host) ?? '')} is not a valid dsh-ssh alias. Read the configured aliases with task_board_ssh {action:"read"} and pass one of them.`, issues }) }
171
+ }
172
+ if (first !== undefined && first.path.startsWith('agent') && !(SSH_AGENT_KINDS as readonly string[]).includes(String(args.agent ?? 'dsh'))) {
173
+ return { ok: false, reply: json({ ok: false, code: 'agent-missing', message: `agent ${JSON.stringify(args.agent)} is not supported. Use one of: ${SSH_AGENT_KINDS.join(', ')}.`, issues }) }
174
+ }
175
+ return { ok: false, reply: json({ ok: false, code: 'invalid-target', message: 'the target is malformed; issues lists each field.', issues }) }
176
+ }
177
+
178
+ /** Run the preflight and answer its refusal as a tool reply, or undefined when the target is usable. */
179
+ async function preflightReply(engine: SshRunEngine, target: SshTarget, args: Record<string, unknown>): Promise<Json | undefined> {
180
+ if (flag(args.preflight) === false) return undefined
181
+ const failure = await engine.preflight(target)
182
+ return failure === undefined ? undefined : json({ ok: false, code: failure.code, message: failure.message, hint: failure.hint })
183
+ }
184
+
185
+ /** Bind a target to an existing card, after parsing and preflight. */
186
+ async function bindResult(engine: SshRunEngine, taskId: string, args: Record<string, unknown>): Promise<Json> {
187
+ const parsed = parseTarget(args)
188
+ if (!parsed.ok) return parsed.reply
189
+ const refusal = await preflightReply(engine, parsed.target, args)
190
+ if (refusal !== undefined) return refusal
191
+ const reply = engine.setTarget(taskId, parsed.target)
192
+ if (!reply.ok) return refused(reply.code, reply.error)
193
+ return json({ ok: true, target: parsed.target, preflight: flag(args.preflight) === false ? 'skipped' : 'passed', hosts: hostViews(engine) })
194
+ }
195
+
196
+ /** One run in the uniform shape, optionally blocking until it settles. */
197
+ async function statusResult(engine: SshRunEngine, taskId: string, args: Record<string, unknown>): Promise<Json> {
198
+ const wait = args.wait === true
199
+ const waited = wait ? await engine.waitStatus(taskId, count(args.timeoutMs)) : undefined
200
+ const view = waited?.view ?? engine.statusView(taskId)
201
+ const running = !view.settled && view.phase !== 'none'
202
+ const activity = running && flag(args.activity) !== false ? await engine.activityOf(taskId) : undefined
203
+ return json({
204
+ ok: true,
205
+ ...view,
206
+ ...(waited === undefined ? {} : { timedOut: waited.timedOut }),
207
+ ...(activity === undefined ? {} : { lastActivity: activity }),
208
+ ...(view.settled ? { next: 'The run settled; the verdict is on the card (task_board_get) and the log is still readable with action "log".' } : running ? { next: 'Still running: call status again with wait:true, or read progress with action "log".' } : {}),
209
+ })
210
+ }
211
+
212
+ /** One filtered page of the log, with its cost stated first. */
213
+ async function logResult(engine: SshRunEngine, taskId: string, args: Record<string, unknown>): Promise<Json> {
214
+ const runId = text(args.runId)
215
+ const tail = count(args.tail)
216
+ const since = count(args.since)
217
+ const maxBytes = count(args.maxBytes)
218
+ const page = await engine.queryLog(taskId, {
219
+ ...(runId === undefined ? {} : { runId }),
220
+ ...(tail === undefined ? {} : { tail }),
221
+ ...(since === undefined ? {} : { since }),
222
+ ...(maxBytes === undefined ? {} : { maxBytes }),
223
+ ...(args.raw === true ? { raw: true } : {}),
224
+ ...(args.statsOnly === true ? { statsOnly: true } : {}),
225
+ })
226
+ if (!page.ok) return json({ ok: false, code: page.code, message: page.error })
227
+ return json({
228
+ ok: true,
229
+ runId: page.runId,
230
+ filtered: page.filtered,
231
+ stats: { rawBytes: page.stats.rawBytes, filteredLines: page.stats.lines, filteredBytes: page.stats.bytes },
232
+ from: page.from,
233
+ lines: page.lines,
234
+ nextSince: page.nextSince,
235
+ more: page.more,
236
+ text: page.text,
237
+ })
238
+ }
239
+
240
+ /** Queue one instruction for the card's remote session. */
241
+ async function followUpResult(engine: SshRunEngine, args: Record<string, unknown>): Promise<Json> {
242
+ const taskId = text(args.taskId)
243
+ const rawText = typeof args.text === 'string' ? args.text : ''
244
+ const instruction = rawText.trim()
245
+ if (taskId === undefined) return refused('task-required', 'taskId is required')
246
+ if (instruction === '') return refused('text-required', 'text must be a non-blank instruction')
247
+ if (rawText.length > TASK_BOARD_MESSAGE_MAX_LENGTH) {
248
+ return refused('text-too-long', `text must be at most ${TASK_BOARD_MESSAGE_MAX_LENGTH} characters`)
249
+ }
250
+ const rawKind = args.kind
251
+ if (rawKind !== undefined && rawKind !== 'follow-up' && rawKind !== 'rework') {
252
+ return refused('invalid-kind', 'kind must be "follow-up" or "rework"')
253
+ }
254
+ const kind = rawKind ?? 'follow-up'
255
+ try {
256
+ const reply = await engine.instruct(taskId, instruction, kind)
257
+ if (!reply.ok) return refused(reply.code, reply.error)
258
+ return json({
259
+ ok: true,
260
+ taskId: reply.taskId,
261
+ ...(reply.runId === undefined ? {} : { runId: reply.runId }),
262
+ ...(reply.phase === undefined ? {} : { phase: reply.phase }),
263
+ message: reply.message ?? '',
264
+ })
265
+ } catch (error) {
266
+ return refused('follow-up-failed', messageOf(error))
267
+ }
268
+ }
269
+
270
+ /** The run handle plus, when asked, the settled status: what one `run` answers. */
271
+ async function runResult(engine: SshRunEngine, taskId: string, args: Record<string, unknown>): Promise<Json> {
272
+ const reply = await engine.runCard(taskId)
273
+ if (!reply.ok) return refused(reply.code, reply.error)
274
+ const handle = engine.describeRun(taskId)
275
+ const status = args.wait === true ? await statusResult(engine, taskId, args) : undefined
276
+ return json({ ok: true, taskId, ...handle, ...(status === undefined ? {} : { status }) })
277
+ }
278
+
279
+ /** The one surface: every action an agent needs against a remote card. */
280
+ function buildUnifiedTool(engine: SshRunEngine): ToolDefinition {
281
+ return defineTool({
282
+ name: 'task_board_ssh',
283
+ description: [
284
+ 'Run a task board card on a remote host over SSH, end to end, with the fewest calls: task_board_ssh {action:"create", title, prompt, host, workspacePath, run:true, wait:true} creates the card WITH its target, checks the host/agent/directory first, starts the run and blocks until it settles - one call.',
285
+ 'Actions: create (new card bound to a target; run:true starts it, wait:true blocks for the verdict), bind (set or replace the target of an existing card), read (current target plus the host aliases), run (start a bound direct card and get its handle: executionId, runId, logPath, target, estimatedDurationMs), status (one uniform shape whether running or settled: phase, exitCode, result, summary, error{code,message,retryHint}, durationMs, remoteSessionId, lastActivityAt, plus lastActivity = the agent\'s last tool call while running; wait:true with timeoutMs blocks until it settles), log (the run log with thinking_tokens/system noise removed; tail N lines or since a cursor, at most 64 KB per reply, stats.filteredLines/filteredBytes tell the cost first; statsOnly:true returns only the counts; raw:true skips the filter), followup (one more instruction for the remote session; kind rework asks again for wrong work).',
286
+ 'Choosing a target: direct mode is for self-contained jobs and keeps no local session; proxy mode runs a local session that drives the host and fits work needing local context. in-place edits an existing remote directory; git makes a per-run worktree on branch dsh-task/<runId>. Agents: dsh, claude, codex, custom.',
287
+ 'A succeeded run records its conclusion as the execution summary; error carries failures only. ' + CODE_HELP,
288
+ ].join(' '),
289
+ parameters: {
290
+ action: { type: 'string', required: true, enum: ['create', 'bind', 'read', 'run', 'status', 'log', 'followup'], description: 'What to do; see the tool description.' },
291
+ taskId: { type: 'string', description: 'Card to act on. Required by every action except create.' },
292
+ title: { type: 'string', description: 'create: card title (one line).' },
293
+ prompt: { type: 'string', description: 'create: the instruction the remote agent runs; the title when blank.' },
294
+ description: { type: 'string', description: 'create: longer human description.' },
295
+ parentId: { type: 'string', description: 'create: parent card id, to make a subtask.' },
296
+ run: { type: 'boolean', description: 'create: start the run right after creating the card.' },
297
+ wait: { type: 'boolean', description: 'create (with run), run and status: block until the run settles or timeoutMs passes.' },
298
+ timeoutMs: { type: 'number', description: 'Longest a wait blocks, in milliseconds (default 120000, at most 600000). A timeout is not a failure: the run keeps going, call status with wait again.' },
299
+ activity: { type: 'boolean', description: 'status: include lastActivity while running (default true).' },
300
+ tail: { type: 'number', description: 'log: last N filtered lines (default 40).' },
301
+ since: { type: 'number', description: 'log: first filtered line to return, the nextSince of an earlier reply.' },
302
+ raw: { type: 'boolean', description: 'log: skip the noise filter.' },
303
+ statsOnly: { type: 'boolean', description: 'log: return only the filtered line count and size.' },
304
+ maxBytes: { type: 'number', description: 'log: cap on the returned text in bytes (default 65536, at most 131072).' },
305
+ runId: { type: 'string', description: 'log: a specific earlier run; the latest when absent.' },
306
+ text: { type: 'string', description: 'followup: the instruction itself.' },
307
+ kind: { type: 'string', enum: ['follow-up', 'rework'], description: 'followup: follow-up adds work; rework asks again for work the previous run got wrong.' },
308
+ ...TARGET_PARAMETERS,
309
+ },
310
+ output: { schema: { type: 'json' }, render: renderJson },
311
+ async execute(rawArgs) {
312
+ const args = rawArgs as Record<string, unknown>
313
+ const action = text(args.action)
314
+ const taskId = text(args.taskId)
315
+ if (action === 'create') {
316
+ const prompt = text(args.prompt) ?? text(args.title)
317
+ const title = text(args.title) ?? (prompt === undefined ? undefined : prompt.split('\n')[0]?.slice(0, 80))
318
+ if (title === undefined || prompt === undefined) return refused('title-required', 'create needs a title or a prompt')
319
+ const parsed = parseTarget(args)
320
+ if (!parsed.ok) return parsed.reply
321
+ const target = parsed.target
322
+ if (flag(args.preflight) !== false) {
323
+ const refusal = await preflightReply(engine, target, args)
324
+ if (refusal !== undefined) return refusal
325
+ }
326
+ const created = await engine.createBound({
327
+ title,
328
+ prompt,
329
+ ...(text(args.description) === undefined ? {} : { description: text(args.description) as string }),
330
+ ...(text(args.parentId) === undefined ? {} : { parentId: text(args.parentId) as string }),
331
+ }, target, { preflight: false })
332
+ if (!created.ok) return refused(created.code, created.error)
333
+ if (args.run !== true) return json({ ok: true, taskId: created.taskId, target, next: 'Start it with task_board_ssh {action:"run"} or task_board_run.' })
334
+ return json({ created: true, ...(await runResult(engine, created.taskId, args) as Record<string, Json>) })
335
+ }
336
+ if (action === 'read' && taskId === undefined) return json({ ok: true, target: null, hosts: hostViews(engine) })
337
+ if (taskId === undefined) return refused('task-required', 'taskId is required')
338
+ switch (action) {
339
+ case 'read': return json({ ok: true, target: engine.targetOf(taskId) ?? null, hosts: hostViews(engine) })
340
+ case 'bind': return await bindResult(engine, taskId, args)
341
+ case 'run': return await runResult(engine, taskId, args)
342
+ case 'status': return await statusResult(engine, taskId, args)
343
+ case 'log': return await logResult(engine, taskId, args)
344
+ case 'followup': return await followUpResult(engine, args)
345
+ default: return refused('unknown-action', 'action must be create, bind, read, run, status, log or followup')
346
+ }
347
+ },
348
+ })
349
+ }
350
+
351
+ /** The target tool (compatibility): read or pin where one card runs. */
352
+ function buildTargetTool(engine: SshRunEngine): ToolDefinition {
353
+ return defineTool({
354
+ name: 'task_board_ssh_target',
355
+ description: 'Compatibility entry of task_board_ssh (actions read and bind). Read or pin the SSH execution target of one task board card; pinning now checks the host, the agent program and the directory first and refuses with a stable code before anything is saved. direct mode starts a headless agent on the host and keeps no local session; proxy mode runs a local session that drives the host. ' + CODE_HELP,
356
+ parameters: {
357
+ action: { type: 'string', required: true, enum: ['read', 'set'], description: 'read reports the current pin and the hosts available; set stores a new pin.' },
358
+ taskId: { type: 'string', required: true, description: 'Card to read or retarget.' },
359
+ ...TARGET_PARAMETERS,
360
+ },
361
+ output: { schema: { type: 'json' }, render: renderJson },
362
+ async execute(args) {
363
+ const taskId = text(args.taskId)
364
+ if (taskId === undefined) return refused('task-required', 'taskId is required')
365
+ const action = text(args.action) ?? 'read'
366
+ if (action === 'read') {
367
+ const target = engine.targetOf(taskId)
368
+ return json({ ok: true, target: target ?? null, hosts: hostViews(engine) })
369
+ }
370
+ if (action !== 'set') return refused('unknown-action', 'action must be read or set')
371
+ return await bindResult(engine, taskId, args as Record<string, unknown>)
372
+ },
373
+ })
374
+ }
375
+
376
+ /** The status tool (compatibility): one card's remote run, and optionally a page of its log. */
377
+ function buildStatusTool(engine: SshRunEngine): ToolDefinition {
378
+ return defineTool({
379
+ name: 'task_board_ssh_status',
380
+ description: 'Compatibility entry of task_board_ssh (actions status and log). Read the remote run state of one card. `state` is the uniform shape (phase, exitCode, result, summary, error, durationMs, remoteSessionId, lastActivityAt) for a running and a settled run alike; wait:true with timeoutMs blocks until it settles. `status` keeps the older shape with the run history. Pass logOffset/logLimit for one raw byte page of the log, or tail/since for the filtered log (at most 64 KB by default).',
381
+ parameters: {
382
+ taskId: { type: 'string', required: true, description: 'Card whose remote run to inspect.' },
383
+ wait: { type: 'boolean', description: 'Block until the run settles or timeoutMs passes.' },
384
+ timeoutMs: { type: 'number', description: 'Longest a wait blocks, in milliseconds (default 120000, at most 600000).' },
385
+ runId: { type: 'string', description: 'One specific run to read the log of; the active run when absent.' },
386
+ tail: { type: 'number', description: 'Filtered log: last N lines.' },
387
+ since: { type: 'number', description: 'Filtered log: first line to return (nextSince of an earlier reply).' },
388
+ raw: { type: 'boolean', description: 'Filtered log: skip the noise filter.' },
389
+ logOffset: { type: 'number', description: 'Raw byte offset the log page starts at (default 0).' },
390
+ logLimit: { type: 'number', description: 'Largest raw page to return, in bytes (default 65536, at most 131072).' },
391
+ },
392
+ output: { schema: { type: 'json' }, render: renderJson },
393
+ async execute(rawArgs) {
394
+ const args = rawArgs as Record<string, unknown>
395
+ const taskId = text(args.taskId)
396
+ if (taskId === undefined) return refused('task-required', 'taskId is required')
397
+ const state = await statusResult(engine, taskId, args)
398
+ const status = engine.status(taskId)
399
+ const runId = text(args.runId)
400
+ const offset = count(args.logOffset)
401
+ const limit = count(args.logLimit)
402
+ const filtered = count(args.tail) !== undefined || count(args.since) !== undefined
403
+ if (filtered) return json({ ok: true, status, state, log: await logResult(engine, taskId, args) })
404
+ if (runId === undefined && offset === undefined && limit === undefined) return json({ ok: true, status, state })
405
+ const page = await engine.readLog(taskId, {
406
+ ...(runId === undefined ? {} : { runId }),
407
+ ...(offset === undefined ? {} : { offset }),
408
+ ...(limit === undefined ? {} : { limit }),
409
+ })
410
+ if (!page.ok) return json({ ok: false, status, state, code: page.code, message: page.error })
411
+ return json({
412
+ ok: true,
413
+ status,
414
+ state,
415
+ log: { runId: page.runId, offset: page.offset, nextOffset: page.nextOffset, bytes: page.bytes, eof: page.eof, text: page.text },
416
+ })
417
+ },
418
+ })
419
+ }
420
+
421
+ /** The follow-up tool (compatibility): one more instruction for the card's remote session. */
422
+ function buildFollowUpTool(engine: SshRunEngine): ToolDefinition {
423
+ return defineTool({
424
+ name: 'task_board_ssh_follow_up',
425
+ description: 'Compatibility entry of task_board_ssh (action followup). Send one more instruction to the remote agent session of a task board card. On a card whose run is still going the instruction waits for that run to finish and then continues the same remote session; on a settled card it re-enters the card run with the earlier session resumed. An agent that cannot resume degrades to a fresh run that carries the previous summary, which the execution record says.',
426
+ parameters: {
427
+ taskId: { type: 'string', required: true, description: 'Card whose remote session receives the instruction.' },
428
+ text: { type: 'string', required: true, description: 'The instruction itself.' },
429
+ kind: { type: 'string', enum: ['follow-up', 'rework'], description: 'follow-up adds work; rework asks again for work the previous run got wrong. Defaults to follow-up.' },
430
+ },
431
+ output: { schema: { type: 'json' }, render: renderJson },
432
+ async execute(args) {
433
+ return await followUpResult(engine, args as Record<string, unknown>)
434
+ },
435
+ })
436
+ }
437
+
438
+ /**
439
+ * Build every tool this extension registers.
440
+ * @param engine - the live run engine behind them.
441
+ * @returns the tool definitions, in registration order.
442
+ */
443
+ export function buildSshTools(engine: SshRunEngine): ToolDefinition[] {
444
+ return [buildUnifiedTool(engine), buildTargetTool(engine), buildStatusTool(engine), buildFollowUpTool(engine)]
445
+ }
446
+
447
+ /** Re-exported for the specs: the modes a target may pin and the stable preflight codes. */
448
+ export type { SshExecutionMode }
449
+ export { SSH_PREFLIGHT_CODES }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The one failure type the workspace strategies raise.
3
+ *
4
+ * Every failure a strategy throws is this class, so the run engine can record a
5
+ * workspace that could not be prepared as a run failure without matching on
6
+ * message text. Each message names what was attempted and the host, path or
7
+ * command it failed on, because it lands in a card where the user has no shell
8
+ * to look behind it.
9
+ *
10
+ * @module dsh-task-board-ssh/host/workspace/errors
11
+ */
12
+
13
+ /** Longest failure text one message carries, in characters. */
14
+ const DETAIL_LIMIT = 400
15
+
16
+ /** A workspace that cannot be prepared or collected. */
17
+ export class SshWorkspaceError extends Error {
18
+ /**
19
+ * @param message - what failed, in one sentence a person can act on.
20
+ * @param options - standard error options, used to keep the underlying failure.
21
+ */
22
+ constructor(message: string, options?: ErrorOptions) {
23
+ super(message, options)
24
+ this.name = 'SshWorkspaceError'
25
+ }
26
+ }
27
+
28
+ /**
29
+ * The one line worth quoting out of a command's failure text.
30
+ *
31
+ * A tool prints progress first and the diagnosis last, so the last non-empty
32
+ * line is the part a person needs; the whole output is bounded away because it
33
+ * would bury the sentence it is quoted in.
34
+ * @param text - captured stderr, stdout or an error message.
35
+ * @returns that line, or undefined when there is nothing to quote.
36
+ */
37
+ export function boundedLine(text: string | undefined): string | undefined {
38
+ if (text === undefined) return undefined
39
+ const lines = text.split('\n').map(line => line.trim()).filter(line => line !== '')
40
+ const last = lines.at(-1)
41
+ if (last === undefined) return undefined
42
+ return last.length <= DETAIL_LIMIT ? last : `${last.slice(0, DETAIL_LIMIT)}...`
43
+ }