@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.
Files changed (126) hide show
  1. package/LICENSE +202 -0
  2. package/README.i18n.yaml +6 -0
  3. package/README.md +85 -2
  4. package/README.zh.md +86 -0
  5. package/cordis.patch.yml +15 -0
  6. package/icon.svg +5 -0
  7. package/lib/client.js +4429 -0
  8. package/lib/index.js +6100 -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 +445 -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 +143 -0
  40. package/lib/types/core/task-record.d.ts +63 -0
  41. package/lib/types/core/workspace.d.ts +74 -0
  42. package/lib/types/host/board.d.ts +71 -0
  43. package/lib/types/host/engine.d.ts +418 -0
  44. package/lib/types/host/extension.d.ts +59 -0
  45. package/lib/types/host/logs.d.ts +135 -0
  46. package/lib/types/host/payload.d.ts +75 -0
  47. package/lib/types/host/prompt.d.ts +44 -0
  48. package/lib/types/host/queue.d.ts +53 -0
  49. package/lib/types/host/remote.d.ts +120 -0
  50. package/lib/types/host/settings.d.ts +149 -0
  51. package/lib/types/host/timers.d.ts +20 -0
  52. package/lib/types/host/tools.d.ts +15 -0
  53. package/lib/types/host/workspace/errors.d.ts +29 -0
  54. package/lib/types/host/workspace/git.d.ts +108 -0
  55. package/lib/types/host/workspace/in-place.d.ts +36 -0
  56. package/lib/types/host/workspace/index.d.ts +28 -0
  57. package/lib/types/host/workspace/layout.d.ts +62 -0
  58. package/lib/types/host/workspace/local-git.d.ts +72 -0
  59. package/lib/types/host/workspace/remote-git.d.ts +111 -0
  60. package/lib/types/host/workspace/remote.d.ts +85 -0
  61. package/lib/types/host/workspace/types.d.ts +28 -0
  62. package/lib/types/host/workspace-seam.d.ts +100 -0
  63. package/lib/types/index.d.ts +97 -0
  64. package/lib/types/mount-once.d.ts +55 -0
  65. package/locale/en.json +6 -0
  66. package/locale/zh.json +6 -0
  67. package/package.json +89 -3
  68. package/src/client/PluginSettingsCard.tsx +838 -0
  69. package/src/client/RangeBubble.tsx +164 -0
  70. package/src/client/SegmentGroup.tsx +93 -0
  71. package/src/client/css-modules.d.ts +5 -0
  72. package/src/client/index.ts +195 -0
  73. package/src/client/locales.ts +257 -0
  74. package/src/client/seat-props.ts +58 -0
  75. package/src/client/settings-card.module.css +821 -0
  76. package/src/client/settings-entry-form.ts +141 -0
  77. package/src/client/settings-form.ts +529 -0
  78. package/src/client/ssh/card-decoration.tsx +55 -0
  79. package/src/client/ssh/compose-section.tsx +147 -0
  80. package/src/client/ssh/detail-section.tsx +345 -0
  81. package/src/client/ssh/extension.ts +271 -0
  82. package/src/client/ssh/log-buffer.ts +224 -0
  83. package/src/client/ssh/log-view.tsx +149 -0
  84. package/src/client/ssh/model.ts +299 -0
  85. package/src/client/ssh/settings-section.tsx +401 -0
  86. package/src/client/ssh/summary.ts +388 -0
  87. package/src/client/ssh/target-form.tsx +380 -0
  88. package/src/client/ssh.module.css +617 -0
  89. package/src/core/action.ts +270 -0
  90. package/src/core/adapters/claude.ts +64 -0
  91. package/src/core/adapters/codex.ts +83 -0
  92. package/src/core/adapters/custom.ts +88 -0
  93. package/src/core/adapters/dsh.ts +87 -0
  94. package/src/core/adapters/index.ts +74 -0
  95. package/src/core/adapters/types.ts +95 -0
  96. package/src/core/contract.ts +510 -0
  97. package/src/core/launcher-script.ts +313 -0
  98. package/src/core/launcher.ts +283 -0
  99. package/src/core/ledger.ts +372 -0
  100. package/src/core/shell.ts +77 -0
  101. package/src/core/target.ts +413 -0
  102. package/src/core/task-record.ts +65 -0
  103. package/src/core/workspace.ts +78 -0
  104. package/src/host/board.ts +103 -0
  105. package/src/host/engine.ts +1602 -0
  106. package/src/host/extension.ts +304 -0
  107. package/src/host/logs.ts +297 -0
  108. package/src/host/payload.ts +134 -0
  109. package/src/host/prompt.ts +89 -0
  110. package/src/host/queue.ts +73 -0
  111. package/src/host/remote.ts +328 -0
  112. package/src/host/settings.ts +294 -0
  113. package/src/host/timers.ts +33 -0
  114. package/src/host/tools.ts +259 -0
  115. package/src/host/workspace/errors.ts +43 -0
  116. package/src/host/workspace/git.ts +305 -0
  117. package/src/host/workspace/in-place.ts +58 -0
  118. package/src/host/workspace/index.ts +37 -0
  119. package/src/host/workspace/layout.ts +122 -0
  120. package/src/host/workspace/local-git.ts +164 -0
  121. package/src/host/workspace/remote-git.ts +244 -0
  122. package/src/host/workspace/remote.ts +181 -0
  123. package/src/host/workspace/types.ts +34 -0
  124. package/src/host/workspace-seam.ts +132 -0
  125. package/src/index.ts +243 -0
  126. package/src/mount-once.ts +142 -0
@@ -0,0 +1,413 @@
1
+ /**
2
+ * The execution target a card pins, and the run record the ledger carries.
3
+ *
4
+ * Both shapes are the wire form: they are what the browser half sends, what the
5
+ * host half stores under `integrations.ssh`, and what an agent tool returns, so
6
+ * both are parsed from `unknown` and never trusted. Parsing reports the exact
7
+ * path of every problem instead of throwing, because a malformed payload
8
+ * arrives from outside the process and must not become an exception in the
9
+ * board's action channel.
10
+ *
11
+ * @module dsh-task-board-ssh/core/target
12
+ */
13
+
14
+ import { validateCustomTemplate } from './adapters/custom.ts'
15
+
16
+ /** How a pinned card reaches the remote host. */
17
+ export type SshExecutionMode = 'direct' | 'proxy'
18
+
19
+ /** Which agent CLI the remote run starts. */
20
+ export const SSH_AGENT_KINDS = ['dsh', 'claude', 'codex', 'custom'] as const
21
+
22
+ /** Agent CLI the remote run starts. */
23
+ export type SshAgentKind = (typeof SSH_AGENT_KINDS)[number]
24
+
25
+ /** Which filesystem mechanism a run works in. */
26
+ export type SshWorkspaceKind = 'in-place' | 'git'
27
+
28
+ /** Where the git workspace strategy takes its repository from. */
29
+ export type SshGitSource =
30
+ | {
31
+ /** A repository the remote host can reach on its own. */
32
+ kind: 'url'
33
+ /**
34
+ * Clone URL: https, ssh or `user@host:path` form. It reaches `git fetch`
35
+ * and `git push` as a bare word, so a leading dash — which git reads as
36
+ * an option, including the `--upload-pack` that runs a command — is
37
+ * refused by the parser rather than escaped.
38
+ */
39
+ url: string
40
+ /**
41
+ * Branch the per-run worktree starts from; absent uses the remote HEAD.
42
+ * A leading dash is refused for the same reason: the value is composed
43
+ * into ref names and refspecs, and one future caller will pass it bare.
44
+ */
45
+ base?: string
46
+ }
47
+ | {
48
+ /** The workspace's local repository, sent to the host as a git bundle. */
49
+ kind: 'workspace'
50
+ /** Workspace id the local repository comes from. */
51
+ workspaceId: string
52
+ }
53
+
54
+ /** Run in an existing remote directory. */
55
+ export interface SshInPlaceWorkspace {
56
+ /** Discriminator. */
57
+ kind: 'in-place'
58
+ /** Absolute remote directory the agent works in. */
59
+ path: string
60
+ }
61
+
62
+ /** Run in a per-run worktree of a git repository. */
63
+ export interface SshGitWorkspace {
64
+ /** Discriminator. */
65
+ kind: 'git'
66
+ /** Repository the worktree branches from. */
67
+ source: SshGitSource
68
+ }
69
+
70
+ /** Filesystem mechanism one run uses. */
71
+ export type SshWorkspaceTarget = SshInPlaceWorkspace | SshGitWorkspace
72
+
73
+ /** Which agent CLI a target pins, and the command template when it is custom. */
74
+ export interface SshAgentTarget {
75
+ /** Agent CLI kind. */
76
+ kind: SshAgentKind
77
+ /** Start template of a custom agent; required for, and only read with, `custom`. */
78
+ command?: string
79
+ /** Resume template of a custom agent; absent degrades a follow-up to a fresh run. */
80
+ resumeCommand?: string
81
+ }
82
+
83
+ /** Everything one card pins about where and how it runs. */
84
+ export interface SshTarget {
85
+ /** Whether the card runs remotely at all, or through a local session driving the host. */
86
+ mode: SshExecutionMode
87
+ /** `dshSsh` host alias the run addresses. */
88
+ host: string
89
+ /** Agent CLI the remote run starts. */
90
+ agent: SshAgentTarget
91
+ /** Filesystem mechanism the run works in. */
92
+ workspace: SshWorkspaceTarget
93
+ }
94
+
95
+ /** Phase of one remote run, as the ledger records it. */
96
+ export type SshRunPhase = 'queued' | 'running' | 'succeeded' | 'failed' | 'cancelled' | 'lost'
97
+
98
+ /** One terminal phase; nothing schedules more work for a run in one of these. */
99
+ export const SSH_TERMINAL_RUN_PHASES: readonly SshRunPhase[] = ['succeeded', 'failed', 'cancelled', 'lost']
100
+
101
+ /** What a launch record cost and produced: one run of one card on one host. */
102
+ export interface SshRunRecord {
103
+ /** Identifier the remote launcher is idempotent on. */
104
+ runId: string
105
+ /** Phase this record was last seen in. */
106
+ phase: SshRunPhase
107
+ /** Instant the run was requested (ms epoch). */
108
+ startedAt: number
109
+ /** Instant the run reached a terminal phase (ms epoch), when it did. */
110
+ finishedAt?: number
111
+ /** Remote exit code, once the run settled. */
112
+ exitCode?: number
113
+ /** Session id the remote agent reported, used to resume it. */
114
+ sessionId?: string
115
+ /** Bytes of the remote stdout log the provider has read. */
116
+ bytes?: number
117
+ /** Branch the git workspace collected, when it produced one. */
118
+ branch?: string
119
+ /** Final summary the remote agent reported, bounded before it lands here. */
120
+ summary?: string
121
+ /** Failure text, bounded before it lands here. */
122
+ error?: string
123
+ /** Follow-up messages delivered into this run's session. */
124
+ followUps?: number
125
+ }
126
+
127
+ /** One diagnostic about a malformed value, addressed by its path inside it. */
128
+ export interface SshValidationIssue {
129
+ /** Dotted path of the offending field, empty for the value itself. */
130
+ path: string
131
+ /** What is wrong with it, in one sentence. */
132
+ message: string
133
+ }
134
+
135
+ /** A parse result: the value, or every reason it was refused. */
136
+ export type SshValidationResult<T> =
137
+ | { ok: true; value: T }
138
+ | { ok: false; errors: readonly SshValidationIssue[] }
139
+
140
+ /** Host aliases `dshSsh` accepts: letters, digits, dots, hyphens, underscores. */
141
+ export const HOST_ALIAS_RE = /^[A-Za-z0-9._-]{1,128}$/
142
+
143
+ /** Longest accepted free text in one target or run field. */
144
+ const FIELD_MAX = 2_048
145
+
146
+ /** Membership test helper that keeps the union narrow without a cast. */
147
+ function oneOf<T extends string>(value: unknown, allowed: readonly T[]): value is T {
148
+ return typeof value === 'string' && (allowed as readonly string[]).includes(value)
149
+ }
150
+
151
+ /** Whether a value is a plain object (not an array, not null, no exotic prototype). */
152
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
153
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) return false
154
+ const prototype = Object.getPrototypeOf(value)
155
+ return prototype === Object.prototype || prototype === null
156
+ }
157
+
158
+ /** Why a value that git would read as an option is refused. */
159
+ const OPTION_INJECTION_REASON = 'must not start with "-": git reads a leading dash as an option, not as a word'
160
+
161
+ /**
162
+ * Whether git would read a value as an option instead of as a word.
163
+ * @param value - the candidate argument.
164
+ * @returns true for a value whose first character is a dash.
165
+ */
166
+ function startsWithDash(value: string): boolean {
167
+ return value.startsWith('-')
168
+ }
169
+
170
+ /** Whether a string is a usable absolute POSIX path. */
171
+ function isAbsolutePath(value: string): boolean {
172
+ return value.startsWith('/') && !value.includes('\u0000') && !value.includes('\n') && !value.includes('\r')
173
+ }
174
+
175
+ /** Whether a string is safe to compose into a git branch/ref name. */
176
+ function isSafeGitRefSegment(value: string): boolean {
177
+ if (/[\u0000-\u001f\u007f\s~^:?*[\\]/.test(value)) return false
178
+ if (value.includes('..') || value.includes('@{') || value.startsWith('/') || value.endsWith('/') || value.endsWith('.') || value.endsWith('.lock') || value.includes('//')) {
179
+ return false
180
+ }
181
+ return true
182
+ }
183
+
184
+ /** Collects issues while a parser walks one value. */
185
+ class IssueList {
186
+ readonly errors: SshValidationIssue[] = []
187
+ add(path: string, message: string): void {
188
+ this.errors.push({ path, message })
189
+ }
190
+ /** Join a path segment onto a parent path. */
191
+ static join(path: string, key: string | number): string {
192
+ return path === '' ? String(key) : `${path}.${String(key)}`
193
+ }
194
+ }
195
+
196
+ /**
197
+ * Refuse unknown keys so a misspelled field is reported instead of ignored.
198
+ * @param value - plain object being read.
199
+ * @param allowed - keys the shape defines.
200
+ * @param path - path of the object for the diagnostics.
201
+ * @param issues - collector.
202
+ */
203
+ function rejectUnknownKeys(value: Record<string, unknown>, allowed: readonly string[], path: string, issues: IssueList): void {
204
+ for (const key of Object.keys(value)) {
205
+ if (!allowed.includes(key)) issues.add(IssueList.join(path, key), `unknown field (accepted: ${allowed.join(', ')})`)
206
+ }
207
+ }
208
+
209
+ /**
210
+ * Read one required bounded string.
211
+ * @param value - candidate field.
212
+ * @param path - path of the field for the diagnostics.
213
+ * @param issues - collector.
214
+ * @returns the text, or undefined when it was refused.
215
+ */
216
+ function readText(value: unknown, path: string, issues: IssueList): string | undefined {
217
+ if (typeof value !== 'string') {
218
+ issues.add(path, 'must be a string')
219
+ return undefined
220
+ }
221
+ if (value.length === 0) {
222
+ issues.add(path, 'must not be empty')
223
+ return undefined
224
+ }
225
+ if (value.length > FIELD_MAX) {
226
+ issues.add(path, `must be at most ${FIELD_MAX} characters`)
227
+ return undefined
228
+ }
229
+ return value
230
+ }
231
+
232
+ /** Read the git source of a git workspace. */
233
+ function parseGitSource(value: unknown, path: string, issues: IssueList): SshGitSource | undefined {
234
+ if (!isPlainObject(value)) {
235
+ issues.add(path, 'must be an object')
236
+ return undefined
237
+ }
238
+ if (value.kind === 'url') {
239
+ rejectUnknownKeys(value, ['kind', 'url', 'base'], path, issues)
240
+ const urlPath = IssueList.join(path, 'url')
241
+ const basePath = IssueList.join(path, 'base')
242
+ const url = readText(value.url, urlPath, issues)
243
+ const base = value.base === undefined ? undefined : readText(value.base, basePath, issues)
244
+ if (url === undefined) return undefined
245
+ // Option injection is checked before whitespace so the classic
246
+ // `--upload-pack=touch /x` reports what is actually wrong with it. Both
247
+ // words are handed to git as argv entries, and git parses a leading dash as
248
+ // an option — which is how a repository URL becomes a command.
249
+ if (startsWithDash(url)) {
250
+ issues.add(urlPath, OPTION_INJECTION_REASON)
251
+ return undefined
252
+ }
253
+ if (url.includes('\u0000')) {
254
+ issues.add(urlPath, 'must not contain NUL bytes')
255
+ return undefined
256
+ }
257
+ if (/\s/.test(url)) {
258
+ issues.add(urlPath, 'must not contain whitespace')
259
+ return undefined
260
+ }
261
+ if (base !== undefined && startsWithDash(base)) {
262
+ issues.add(basePath, OPTION_INJECTION_REASON)
263
+ return undefined
264
+ }
265
+ if (base !== undefined && !isSafeGitRefSegment(base)) {
266
+ issues.add(basePath, 'must be a valid git branch name without whitespace or control characters')
267
+ return undefined
268
+ }
269
+ return base === undefined ? { kind: 'url', url } : { kind: 'url', url, base }
270
+ }
271
+ if (value.kind === 'workspace') {
272
+ rejectUnknownKeys(value, ['kind', 'workspaceId'], path, issues)
273
+ const workspaceIdPath = IssueList.join(path, 'workspaceId')
274
+ const workspaceId = readText(value.workspaceId, workspaceIdPath, issues)
275
+ if (workspaceId === undefined) return undefined
276
+ if (workspaceId.includes('\u0000')) {
277
+ issues.add(workspaceIdPath, 'must not contain NUL bytes')
278
+ return undefined
279
+ }
280
+ return { kind: 'workspace', workspaceId }
281
+ }
282
+ issues.add(IssueList.join(path, 'kind'), 'must be "url" or "workspace"')
283
+ return undefined
284
+ }
285
+
286
+ /** Read the filesystem mechanism of one target. */
287
+ function parseWorkspace(value: unknown, path: string, issues: IssueList): SshWorkspaceTarget | undefined {
288
+ if (!isPlainObject(value)) {
289
+ issues.add(path, 'must be an object')
290
+ return undefined
291
+ }
292
+ if (value.kind === 'in-place') {
293
+ rejectUnknownKeys(value, ['kind', 'path'], path, issues)
294
+ const p = readText(value.path, IssueList.join(path, 'path'), issues)
295
+ if (p === undefined) return undefined
296
+ if (!isAbsolutePath(p)) {
297
+ issues.add(IssueList.join(path, 'path'), 'must be an absolute remote path')
298
+ return undefined
299
+ }
300
+ return { kind: 'in-place', path: p }
301
+ }
302
+ if (value.kind === 'git') {
303
+ rejectUnknownKeys(value, ['kind', 'source'], path, issues)
304
+ const source = parseGitSource(value.source, IssueList.join(path, 'source'), issues)
305
+ return source === undefined ? undefined : { kind: 'git', source }
306
+ }
307
+ issues.add(IssueList.join(path, 'kind'), 'must be "in-place" or "git"')
308
+ return undefined
309
+ }
310
+
311
+ /** Read which agent CLI a target pins. */
312
+ function parseAgentTarget(value: unknown, path: string, issues: IssueList): SshAgentTarget | undefined {
313
+ if (!isPlainObject(value)) {
314
+ issues.add(path, 'must be an object')
315
+ return undefined
316
+ }
317
+ rejectUnknownKeys(value, ['kind', 'command', 'resumeCommand'], path, issues)
318
+ if (!oneOf(value.kind, SSH_AGENT_KINDS)) {
319
+ issues.add(IssueList.join(path, 'kind'), `must be one of: ${SSH_AGENT_KINDS.join(', ')}`)
320
+ return undefined
321
+ }
322
+ if (value.kind !== 'custom') {
323
+ if (value.command !== undefined || value.resumeCommand !== undefined) {
324
+ issues.add(path, 'command templates apply to kind "custom" only')
325
+ return undefined
326
+ }
327
+ return { kind: value.kind }
328
+ }
329
+ const commandPath = IssueList.join(path, 'command')
330
+ const resumePath = IssueList.join(path, 'resumeCommand')
331
+ const command = readText(value.command, commandPath, issues)
332
+ const resumeCommand = value.resumeCommand === undefined
333
+ ? undefined
334
+ : readText(value.resumeCommand, resumePath, issues)
335
+ if (command === undefined) {
336
+ issues.add(commandPath, 'a custom agent needs a start template')
337
+ return undefined
338
+ }
339
+ const commandProblem = validateCustomTemplate(command)
340
+ if (commandProblem !== undefined) {
341
+ issues.add(commandPath, commandProblem)
342
+ return undefined
343
+ }
344
+ if (resumeCommand !== undefined) {
345
+ const resumeProblem = validateCustomTemplate(resumeCommand)
346
+ if (resumeProblem !== undefined) {
347
+ issues.add(resumePath, resumeProblem)
348
+ return undefined
349
+ }
350
+ }
351
+ return resumeCommand === undefined ? { kind: 'custom', command } : { kind: 'custom', command, resumeCommand }
352
+ }
353
+
354
+ /**
355
+ * Parse one pinned execution target.
356
+ * @param value - candidate target from the wire, the ledger or a settings form.
357
+ * @returns the target, or every reason it was refused.
358
+ */
359
+ export function parseSshTarget(value: unknown): SshValidationResult<SshTarget> {
360
+ const issues = new IssueList()
361
+ if (!isPlainObject(value)) return { ok: false, errors: [{ path: '', message: 'must be an object' }] }
362
+ rejectUnknownKeys(value, ['mode', 'host', 'agent', 'workspace'], '', issues)
363
+ if (!oneOf(value.mode, ['direct', 'proxy'] as const)) {
364
+ issues.add('mode', 'must be "direct" or "proxy"')
365
+ }
366
+ const host = typeof value.host === 'string' && HOST_ALIAS_RE.test(value.host) ? value.host : undefined
367
+ if (host === undefined) issues.add('host', 'must be a dshSsh host alias (letters, digits, dots, hyphens, underscores)')
368
+ const agent = parseAgentTarget(value.agent, 'agent', issues)
369
+ const workspace = parseWorkspace(value.workspace, 'workspace', issues)
370
+ if (issues.errors.length > 0) return { ok: false, errors: issues.errors }
371
+ return { ok: true, value: { mode: value.mode as SshExecutionMode, host: host as string, agent: agent as SshAgentTarget, workspace: workspace as SshWorkspaceTarget } }
372
+ }
373
+
374
+ /**
375
+ * Parse one run record.
376
+ * @param value - candidate record from the ledger or an action payload.
377
+ * @returns the record, or every reason it was refused.
378
+ */
379
+ export function parseSshRunRecord(value: unknown): SshValidationResult<SshRunRecord> {
380
+ const issues = new IssueList()
381
+ if (!isPlainObject(value)) return { ok: false, errors: [{ path: '', message: 'must be an object' }] }
382
+ rejectUnknownKeys(
383
+ value,
384
+ ['runId', 'phase', 'startedAt', 'finishedAt', 'exitCode', 'sessionId', 'bytes', 'branch', 'summary', 'error', 'followUps'],
385
+ '',
386
+ issues,
387
+ )
388
+ const runId = typeof value.runId === 'string' && value.runId !== '.' && value.runId !== '..' && /^[A-Za-z0-9._-]{1,128}$/.test(value.runId) ? value.runId : undefined
389
+ if (runId === undefined) issues.add('runId', 'must be 1-128 characters of letters, digits, dots, underscores or hyphens')
390
+ if (!oneOf(value.phase, ['queued', 'running', 'succeeded', 'failed', 'cancelled', 'lost'] as const)) {
391
+ issues.add('phase', 'must be one of: queued, running, succeeded, failed, cancelled, lost')
392
+ }
393
+ if (typeof value.startedAt !== 'number' || !Number.isFinite(value.startedAt)) issues.add('startedAt', 'must be a finite millisecond timestamp')
394
+ for (const key of ['finishedAt', 'exitCode', 'bytes', 'followUps'] as const) {
395
+ if (value[key] === undefined) continue
396
+ if (typeof value[key] !== 'number' || !Number.isFinite(value[key])) issues.add(key, 'must be a finite number')
397
+ }
398
+ for (const key of ['sessionId', 'branch', 'summary', 'error'] as const) {
399
+ if (value[key] === undefined) continue
400
+ readText(value[key], key, issues)
401
+ }
402
+ if (issues.errors.length > 0) return { ok: false, errors: issues.errors }
403
+ return { ok: true, value: { ...(value as unknown as SshRunRecord) } }
404
+ }
405
+
406
+ /**
407
+ * Whether a run phase is terminal.
408
+ * @param phase - phase to test.
409
+ * @returns true for succeeded, failed, cancelled and lost.
410
+ */
411
+ export function isTerminalRunPhase(phase: SshRunPhase): boolean {
412
+ return SSH_TERMINAL_RUN_PHASES.includes(phase)
413
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Structural view of the task-board vocabulary this extension reads.
3
+ *
4
+ * The extension is a separate bundle and may not import the board's internals,
5
+ * so it restates the record shape the provider contract hands it. These are
6
+ * read-only structural declarations: the board's own values satisfy them and
7
+ * nothing here is constructed by the board. The single source of truth for the
8
+ * contract remains the board's `src/core/extension.ts`.
9
+ *
10
+ * @module dsh-task-board-ssh/core/task-record
11
+ */
12
+
13
+ /** Column a card occupies. */
14
+ export type TaskStatus = 'backlog' | 'todo' | 'running' | 'done' | 'failed'
15
+
16
+ /** Settlement of one local execution. */
17
+ export type ExecutionOutcome = 'succeeded' | 'failed' | 'cancelled'
18
+
19
+ /** One execution of a card, as a provider reads it. */
20
+ export interface ExecutionRecord {
21
+ /** Stable execution id. */
22
+ id: string
23
+ /** Session the execution runs in, once one was opened. */
24
+ sessionId?: string
25
+ /** Instant the execution started (ms epoch). */
26
+ startedAt: number
27
+ /** Instant the execution settled (ms epoch). */
28
+ settledAt?: number
29
+ /** Settlement, absent while the execution is still running. */
30
+ result?: ExecutionOutcome
31
+ }
32
+
33
+ /** One card, as a provider reads it through the capability face. */
34
+ export interface TaskRecord {
35
+ /** Stable card id. */
36
+ id: string
37
+ /** Card title. */
38
+ title: string
39
+ /** Card description. */
40
+ description: string
41
+ /** Prompt one execution sends to the agent. */
42
+ prompt: string
43
+ /** Column the card occupies. */
44
+ status: TaskStatus
45
+ /** Instant the card was created (ms epoch). */
46
+ createdAt: number
47
+ /** Instant the card was last written (ms epoch). */
48
+ updatedAt: number
49
+ /** Parent card id, when this card is a subtask. */
50
+ parentId?: string
51
+ /** Workspace the execution must run in, when the card pins one. */
52
+ workspaceId?: string
53
+ /** Qualified provider/model the card pins for its execution, when it pins one. */
54
+ model?: string
55
+ /** True for a card the board keeps off the board view. */
56
+ hidden?: boolean
57
+ /** Instant the card was archived (ms epoch), absent while it is live. */
58
+ archivedAt?: number
59
+ /** Every execution this card has had, oldest first. */
60
+ executions: ExecutionRecord[]
61
+ /** The board's native tags. */
62
+ tags?: ReadonlyArray<{ name: string }>
63
+ /** Opaque provider payloads, keyed by extension id. */
64
+ integrations?: Record<string, unknown>
65
+ }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * The frozen workspace-strategy interface.
3
+ *
4
+ * One interface covers the three user-visible placement modes the proposal
5
+ * settled: an existing remote directory (`in-place`) and a per-run git
6
+ * worktree whose repository is either a URL the host can reach or the local
7
+ * workspace repository sent as a bundle (`git`). The host engine drives it;
8
+ * this module is the seam only, so the two strategies and the engine can be
9
+ * written against one shape.
10
+ *
11
+ * A strategy never decides policy: it receives the run, prepares a directory,
12
+ * and collects the result. Failures are thrown and recorded by the caller,
13
+ * because a workspace that cannot be prepared must fail the run before any
14
+ * agent starts.
15
+ *
16
+ * @module dsh-task-board-ssh/core/workspace
17
+ */
18
+ import type { DshSshService } from './contract.ts'
19
+ import type { SshGitSource, SshWorkspaceKind } from './target.ts'
20
+
21
+ /** Everything a strategy needs about one run. */
22
+ export interface SshWorkspaceRun {
23
+ /** Run identifier; also the branch suffix of a git worktree. */
24
+ runId: string
25
+ /** `dshSsh` host alias the run addresses. */
26
+ host: string
27
+ /** Absolute remote state directory the provider owns on that host. */
28
+ stateDir: string
29
+ /** The host face every remote operation goes through. */
30
+ ssh: DshSshService
31
+ /** Repository source, present for the git mode. */
32
+ source?: SshGitSource
33
+ /** Local repository root, present when `source` is the workspace repository. */
34
+ localRepository?: string
35
+ /**
36
+ * Earlier run id whose git worktree/branch this follow-up/rework run reuses
37
+ * so prior commits stay in place and cwd-scoped sessions (e.g. Claude Code)
38
+ * can resume cleanly.
39
+ */
40
+ reuseWorktreeRunId?: string
41
+ /** Aborts a transfer or a remote command. */
42
+ signal?: AbortSignal
43
+ }
44
+
45
+ /** What preparing a run produced. */
46
+ export interface SshWorkspacePreparation {
47
+ /** Absolute remote directory the agent CLI runs in. */
48
+ cwd: string
49
+ }
50
+
51
+ /** What collecting a run produced. */
52
+ export interface SshWorkspaceCollection {
53
+ /** Branch carrying the run's commits, when the strategy brings work back as one. */
54
+ branch?: string
55
+ }
56
+
57
+ /** One placement mechanism. */
58
+ export interface SshWorkspaceStrategy {
59
+ /** Which mechanism this is; matches `SshWorkspaceTarget.kind`. */
60
+ readonly kind: SshWorkspaceKind
61
+ /**
62
+ * Make the run's working directory exist and return it.
63
+ * @param run - the run being prepared.
64
+ * @returns the absolute remote directory the agent runs in.
65
+ */
66
+ prepare(run: SshWorkspaceRun): Promise<SshWorkspacePreparation>
67
+ /**
68
+ * Bring back whatever the run produced.
69
+ * @param run - the settled run.
70
+ * @returns the branch that carries the result, when there is one.
71
+ */
72
+ collect(run: SshWorkspaceRun): Promise<SshWorkspaceCollection>
73
+ /**
74
+ * Optional cleanup invoked when a card is deleted (removes worktrees/refs).
75
+ * @param run - run whose workspace resources should be reclaimed.
76
+ */
77
+ cleanup?(run: SshWorkspaceRun): Promise<void>
78
+ }
@@ -0,0 +1,103 @@
1
+ /**
2
+ * The board's in-process action seat, and the execution facts this package
3
+ * reads off a card.
4
+ *
5
+ * A provider's own writes reach the ledger through exactly what the browser
6
+ * uses: the board service's `apply`, which every gate (running lock, content
7
+ * freeze, permission confirmation, spend cap) still judges. The registration
8
+ * contract this package restates in `src/core/contract.ts` carries the read
9
+ * face only, so the seat is read structurally off the same injected service and
10
+ * refused when the board revision does not serve it — a provider without it
11
+ * still mounts, it just cannot open or close a run.
12
+ *
13
+ * @module dsh-task-board-ssh/host/board
14
+ */
15
+ import { TASK_BOARD_SERVICE_NAME, type ServiceLookupSeat } from '../core/contract.ts'
16
+ import type { TaskRecord } from '../core/task-record.ts'
17
+
18
+ /** One action this package submits to the board. Field names are the board's own. */
19
+ export type BoardAction =
20
+ | { kind: 'run'; taskId: string }
21
+ | { kind: 'rerun'; taskId: string }
22
+ | {
23
+ kind: 'record-external-outcome'
24
+ taskId: string
25
+ result: 'succeeded' | 'failed'
26
+ initiatedBy: string
27
+ summary?: string
28
+ }
29
+
30
+ /**
31
+ * The board service's `apply`, as this package drives it.
32
+ *
33
+ * Structurally the board's own: the returned value is the board snapshot, which
34
+ * no caller here reads beyond "the write was accepted", so it stays `unknown`.
35
+ */
36
+ export interface BoardActionSeat {
37
+ /** Submit one action; a refusal throws the board's own message. */
38
+ apply(requestId: string, action: BoardAction, initiator?: string): unknown
39
+ }
40
+
41
+ /** Prefix every external execution record this package writes carries. */
42
+ export const SSH_INITIATOR_PREFIX = 'ssh:'
43
+
44
+ /**
45
+ * Read the action seat off the board service face.
46
+ * @param face - the resolved `taskBoard` host service.
47
+ * @returns the seat, or undefined when this board revision serves no applier.
48
+ */
49
+ export function readBoardActionSeat(face: unknown): BoardActionSeat | undefined {
50
+ if (typeof face !== 'object' || face === null) return undefined
51
+ const apply = (face as { apply?: unknown }).apply
52
+ if (typeof apply !== 'function') return undefined
53
+ return {
54
+ apply: (requestId: string, action: BoardAction, initiator?: string): unknown =>
55
+ (apply as (this: unknown, a: string, b: BoardAction, c?: string) => unknown).call(face, requestId, action, initiator),
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Resolve the board's action seat off the injected service.
61
+ *
62
+ * The registration contract this package restates carries the read face only, so
63
+ * the applier is read off the same service structurally: the board publishes one
64
+ * object with both, and a revision that serves no applier answers undefined here
65
+ * instead of failing the provider.
66
+ * @param ctx - the injected scope the board service was resolved from.
67
+ * @returns the seat, or undefined when this board revision serves no applier.
68
+ */
69
+ export function resolveBoardActionSeat(ctx: ServiceLookupSeat): BoardActionSeat | undefined {
70
+ try {
71
+ return readBoardActionSeat(ctx.get(TASK_BOARD_SERVICE_NAME))
72
+ } catch {
73
+ return undefined
74
+ }
75
+ }
76
+
77
+ /** The `initiatedBy` a board execution record carries, when it recorded one. */
78
+ function executionInitiator(record: unknown): string | undefined {
79
+ if (typeof record !== 'object' || record === null) return undefined
80
+ const initiatedBy = (record as { initiatedBy?: unknown }).initiatedBy
81
+ return typeof initiatedBy === 'string' ? initiatedBy : undefined
82
+ }
83
+
84
+ /**
85
+ * Whether the card's execution history already holds the outcome of one run.
86
+ *
87
+ * Reconciliation must close a run that finished while the local host was down
88
+ * exactly once, and a crash between the board write and the payload write would
89
+ * otherwise record it twice: the board appends a fresh external execution for
90
+ * every call, so this package's own marker is the only idempotency key there is.
91
+ * @param task - the card to read.
92
+ * @param runId - the run whose outcome may already be recorded.
93
+ * @returns true when a record this package wrote for `runId` is present.
94
+ */
95
+ export function alreadyRecorded(task: TaskRecord, runId: string): boolean {
96
+ const executions: unknown = (task as { executions?: unknown }).executions
97
+ if (!Array.isArray(executions)) return false
98
+ const marker = SSH_INITIATOR_PREFIX + runId
99
+ return executions.some(record => {
100
+ const initiator = executionInitiator(record)
101
+ return initiator === marker
102
+ })
103
+ }