@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,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every git command line a workspace strategy sends to a host.
|
|
3
|
+
*
|
|
4
|
+
* Built from `core/shell.ts` words and never by interpolating a path into a
|
|
5
|
+
* string: the remote side is an arbitrary `sh` on a machine this package does
|
|
6
|
+
* not own, so a directory whose name contains a space or a quote has to survive
|
|
7
|
+
* as one argument. `GIT_TERMINAL_PROMPT=0` belongs to every command because a
|
|
8
|
+
* remote run has nobody at a terminal to answer a credential prompt, and
|
|
9
|
+
* `env` carries it instead of relying on a shell assignment.
|
|
10
|
+
*
|
|
11
|
+
* Quoting is not enough on its own. A quoted word still reaches git as that
|
|
12
|
+
* word, and git reads a leading dash as an option — `--upload-pack` and
|
|
13
|
+
* `--receive-pack` are options that run a program, so a card's repository
|
|
14
|
+
* address is a command-execution vector in the repository slot. The parser
|
|
15
|
+
* refuses a leading dash first; the commands below put {@link END_OF_OPTIONS}
|
|
16
|
+
* in front of the repository as the command layer's own guard, which also
|
|
17
|
+
* makes every refspec after it a value. Every other positional word this
|
|
18
|
+
* module passes is prefixed by a constant (`refs/heads/`, `refs/dsh-remote/`,
|
|
19
|
+
* `refs/dsh-base/`), an absolute remote path, or a validated object name, so no
|
|
20
|
+
* other slot can start with a dash.
|
|
21
|
+
*
|
|
22
|
+
* @module dsh-task-board-ssh/host/workspace/remote-git
|
|
23
|
+
*/
|
|
24
|
+
import { shellEnsureDir, shellJoin, shellQuote } from '../../core/shell.ts'
|
|
25
|
+
import type { SshGitSource } from '../../core/target.ts'
|
|
26
|
+
import type { GitWorkspaceLayout } from './layout.ts'
|
|
27
|
+
|
|
28
|
+
/** Prefix every remote git invocation carries. */
|
|
29
|
+
const GIT_PREFIX = ['env', 'GIT_TERMINAL_PROMPT=0', 'git'] as const
|
|
30
|
+
|
|
31
|
+
/** Namespace a url source's remote branches land in. */
|
|
32
|
+
const REMOTE_REF_NAMESPACE = 'refs/dsh-remote/'
|
|
33
|
+
|
|
34
|
+
/** Ref a url source's remote HEAD lands in. */
|
|
35
|
+
const REMOTE_HEAD_REF = 'refs/dsh-remote-head'
|
|
36
|
+
|
|
37
|
+
/** The word that ends git's option parsing, so every word after it is a value. */
|
|
38
|
+
const END_OF_OPTIONS = '--'
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Compose one git command line.
|
|
42
|
+
* @param words - git arguments, unquoted.
|
|
43
|
+
* @returns the quoted command line.
|
|
44
|
+
*/
|
|
45
|
+
function gitCommand(...words: readonly string[]): string {
|
|
46
|
+
return shellJoin([...GIT_PREFIX, ...words])
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Ref a fetched url source leaves its start commit on.
|
|
51
|
+
* @param source - the pinned url source.
|
|
52
|
+
* @returns the remote branch's ref, or the remote HEAD's when no base was pinned.
|
|
53
|
+
*/
|
|
54
|
+
export function remoteStartRef(source: Extract<SshGitSource, { kind: 'url' }>): string {
|
|
55
|
+
return source.base === undefined ? REMOTE_HEAD_REF : `${REMOTE_REF_NAMESPACE}${source.base}`
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Build the probe that answers whether one absolute directory exists.
|
|
60
|
+
* @param path - absolute remote directory.
|
|
61
|
+
* @returns one shell command.
|
|
62
|
+
*/
|
|
63
|
+
export function directoryProbeCommand(path: string): string {
|
|
64
|
+
return shellJoin(['test', '-d', path])
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Build the probe that answers whether a run's worktree is already in place.
|
|
69
|
+
* @param layout - the run's paths.
|
|
70
|
+
* @returns one shell command that exits zero only when both halves hold.
|
|
71
|
+
*/
|
|
72
|
+
export function preparedProbeCommand(layout: GitWorkspaceLayout): string {
|
|
73
|
+
const worktree = shellJoin(['test', '-d', layout.worktreeDir])
|
|
74
|
+
return `${worktree} && ${branchProbeCommand(layout)}`
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Build the probe that answers whether a run's branch exists in the cache.
|
|
79
|
+
* @param layout - the run's paths.
|
|
80
|
+
* @returns one shell command.
|
|
81
|
+
*/
|
|
82
|
+
export function branchProbeCommand(layout: GitWorkspaceLayout): string {
|
|
83
|
+
return gitCommand('-C', layout.cacheDir, 'show-ref', '--verify', '--quiet', layout.branchRef)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Build the command that creates the bare cache and drops dead worktrees.
|
|
88
|
+
*
|
|
89
|
+
* `git init --bare` on an existing cache is a no-op, which is what makes this
|
|
90
|
+
* safe to replay; `worktree prune` forgets registrations whose directory is
|
|
91
|
+
* gone, so a worktree a person deleted by hand can be re-attached instead of
|
|
92
|
+
* refused as "already registered".
|
|
93
|
+
* @param layout - the run's paths.
|
|
94
|
+
* @returns one shell command.
|
|
95
|
+
*/
|
|
96
|
+
export function prepareCacheCommand(layout: GitWorkspaceLayout): string {
|
|
97
|
+
return [
|
|
98
|
+
shellEnsureDir(layout.reposDir),
|
|
99
|
+
gitCommand('init', '--bare', '--quiet', layout.cacheDir),
|
|
100
|
+
gitCommand('-C', layout.cacheDir, 'worktree', 'prune'),
|
|
101
|
+
].join(' && ')
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Build the command that fetches a url source's branches into the cache.
|
|
106
|
+
* @param layout - the run's paths.
|
|
107
|
+
* @param url - clone URL the host can reach.
|
|
108
|
+
* @returns one shell command.
|
|
109
|
+
*/
|
|
110
|
+
export function fetchUrlCommand(layout: GitWorkspaceLayout, url: string): string {
|
|
111
|
+
return gitCommand(
|
|
112
|
+
'-C',
|
|
113
|
+
layout.cacheDir,
|
|
114
|
+
'fetch',
|
|
115
|
+
'--no-tags',
|
|
116
|
+
END_OF_OPTIONS,
|
|
117
|
+
url,
|
|
118
|
+
`+refs/heads/*:${REMOTE_REF_NAMESPACE}*`,
|
|
119
|
+
`+HEAD:${REMOTE_HEAD_REF}`,
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Build the command that unpacks an uploaded bundle into the cache.
|
|
125
|
+
*
|
|
126
|
+
* Uses `git bundle unbundle` instead of `git fetch +HEAD:...`: when the local
|
|
127
|
+
* working tree is dirty (`git stash create`), `git bundle` only advertises
|
|
128
|
+
* `HEAD` as a ref header while carrying the snapshot commit in the packfile.
|
|
129
|
+
* On a second run where `HEAD` has not moved, `git fetch` sees `HEAD` already
|
|
130
|
+
* in the bare cache and skips unpacking the packfile, leaving the snapshot
|
|
131
|
+
* commit missing. `bundle unbundle` unconditionally unpacks every object so
|
|
132
|
+
* {@link pinBaseCommand} can pin the snapshot SHA directly.
|
|
133
|
+
* @param layout - the run's paths.
|
|
134
|
+
* @param bundlePath - absolute remote path of the uploaded bundle.
|
|
135
|
+
* @returns one shell command.
|
|
136
|
+
*/
|
|
137
|
+
export function fetchBundleCommand(layout: GitWorkspaceLayout, bundlePath: string): string {
|
|
138
|
+
return gitCommand(
|
|
139
|
+
'-C',
|
|
140
|
+
layout.cacheDir,
|
|
141
|
+
'bundle',
|
|
142
|
+
'unbundle',
|
|
143
|
+
bundlePath,
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Build the command that pins the commit a run branch starts from.
|
|
149
|
+
* @param layout - the run's paths.
|
|
150
|
+
* @param commit - object name to pin.
|
|
151
|
+
* @returns one shell command.
|
|
152
|
+
*/
|
|
153
|
+
export function pinBaseCommand(layout: GitWorkspaceLayout, commit: string): string {
|
|
154
|
+
return gitCommand('-C', layout.cacheDir, 'update-ref', layout.baseRef, commit)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Build the command that creates a run's worktree on a new branch.
|
|
159
|
+
* @param layout - the run's paths.
|
|
160
|
+
* @param startRef - ref or commit the branch starts from.
|
|
161
|
+
* @returns one shell command.
|
|
162
|
+
*/
|
|
163
|
+
export function addWorktreeCommand(layout: GitWorkspaceLayout, startRef: string): string {
|
|
164
|
+
const add = gitCommand(
|
|
165
|
+
'-C',
|
|
166
|
+
layout.cacheDir,
|
|
167
|
+
'worktree',
|
|
168
|
+
'add',
|
|
169
|
+
'-q',
|
|
170
|
+
'-b',
|
|
171
|
+
layout.branch,
|
|
172
|
+
layout.worktreeDir,
|
|
173
|
+
startRef,
|
|
174
|
+
)
|
|
175
|
+
return `${shellEnsureDir(layout.workspacesDir)} && ${add}`
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Build the command that re-attaches a run's worktree to its existing branch.
|
|
180
|
+
* @param layout - the run's paths.
|
|
181
|
+
* @returns one shell command.
|
|
182
|
+
*/
|
|
183
|
+
export function attachWorktreeCommand(layout: GitWorkspaceLayout): string {
|
|
184
|
+
const add = gitCommand('-C', layout.cacheDir, 'worktree', 'add', '-q', layout.worktreeDir, layout.branch)
|
|
185
|
+
return `${shellEnsureDir(layout.workspacesDir)} && ${add}`
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Build the command that counts the commits a run branch carries past its base.
|
|
190
|
+
* @param layout - the run's paths.
|
|
191
|
+
* @returns one shell command that prints one integer.
|
|
192
|
+
*/
|
|
193
|
+
export function countRunCommitsCommand(layout: GitWorkspaceLayout): string {
|
|
194
|
+
return gitCommand('-C', layout.cacheDir, 'rev-list', '--count', `${layout.baseRef}..${layout.branchRef}`)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Build the command that pushes a run branch to a url source's repository.
|
|
199
|
+
* @param layout - the run's paths.
|
|
200
|
+
* @param url - clone URL the host can reach.
|
|
201
|
+
* @returns one shell command.
|
|
202
|
+
*/
|
|
203
|
+
export function pushBranchCommand(layout: GitWorkspaceLayout, url: string): string {
|
|
204
|
+
return gitCommand('-C', layout.cacheDir, 'push', END_OF_OPTIONS, url, `${layout.branchRef}:${layout.branchRef}`)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Build the command that bundles a run branch without its base.
|
|
209
|
+
*
|
|
210
|
+
* The base commit is the local repository's own HEAD or its working-tree
|
|
211
|
+
* snapshot, so leaving it out keeps the download to the commits the run made.
|
|
212
|
+
* @param layout - the run's paths.
|
|
213
|
+
* @param bundlePath - absolute remote path to write.
|
|
214
|
+
* @returns one shell command.
|
|
215
|
+
*/
|
|
216
|
+
export function bundleResultCommand(layout: GitWorkspaceLayout, bundlePath: string): string {
|
|
217
|
+
return gitCommand(
|
|
218
|
+
'-C',
|
|
219
|
+
layout.cacheDir,
|
|
220
|
+
'bundle',
|
|
221
|
+
'create',
|
|
222
|
+
'--quiet',
|
|
223
|
+
bundlePath,
|
|
224
|
+
layout.branchRef,
|
|
225
|
+
'--not',
|
|
226
|
+
layout.baseRef,
|
|
227
|
+
)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Build the best-effort command that removes one run's worktree, branch and
|
|
232
|
+
* base ref from the shared bare cache when its card is deleted.
|
|
233
|
+
* @param layout - the run's paths.
|
|
234
|
+
* @returns one shell command.
|
|
235
|
+
*/
|
|
236
|
+
export function removeWorktreeCommand(layout: GitWorkspaceLayout): string {
|
|
237
|
+
return [
|
|
238
|
+
`${gitCommand('-C', layout.cacheDir, 'worktree', 'remove', '--force', layout.worktreeDir)} 2>/dev/null || true`,
|
|
239
|
+
`rm -rf ${shellQuote(layout.worktreeDir)}`,
|
|
240
|
+
`${gitCommand('-C', layout.cacheDir, 'worktree', 'prune')} 2>/dev/null || true`,
|
|
241
|
+
`${gitCommand('-C', layout.cacheDir, 'branch', '-D', layout.branch)} 2>/dev/null || true`,
|
|
242
|
+
`${gitCommand('-C', layout.cacheDir, 'update-ref', '-d', layout.baseRef)} 2>/dev/null || true`,
|
|
243
|
+
].join('; ')
|
|
244
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The only way a workspace strategy talks to a host.
|
|
3
|
+
*
|
|
4
|
+
* Every command is built by `remote-git.ts` (or a helper in `core/shell.ts`) and
|
|
5
|
+
* handed to `dshSsh` verbatim: no strategy opens a connection of its own, so a
|
|
6
|
+
* run shares the host inventory, the credentials and the connection pool the
|
|
7
|
+
* user already operates. A transport failure is raised as the same error type
|
|
8
|
+
* as a command failure, because a workspace that could not be looked at must
|
|
9
|
+
* never be reported as missing.
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-task-board-ssh/host/workspace/remote
|
|
12
|
+
*/
|
|
13
|
+
import type { DshSshExecResult } from '../../core/contract.ts'
|
|
14
|
+
import { shellQuote } from '../../core/shell.ts'
|
|
15
|
+
import type { SshWorkspaceRun } from '../../core/workspace.ts'
|
|
16
|
+
import { SshWorkspaceError, boundedLine } from './errors.ts'
|
|
17
|
+
|
|
18
|
+
/** Whether an error is a cancellation, which is not a workspace failure. */
|
|
19
|
+
function isAbort(error: unknown): boolean {
|
|
20
|
+
return error instanceof Error && error.name === 'AbortError'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Wrap a failure that never reached the host.
|
|
25
|
+
* @param what - what was being attempted, in one clause.
|
|
26
|
+
* @param error - what the service threw.
|
|
27
|
+
* @returns the workspace failure to raise.
|
|
28
|
+
*/
|
|
29
|
+
function transportFailure(what: string, error: unknown): SshWorkspaceError {
|
|
30
|
+
const detail = boundedLine(error instanceof Error ? error.message : String(error)) ?? 'unknown failure'
|
|
31
|
+
return new SshWorkspaceError(`${what}: ${detail}`, { cause: error })
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** One sentence describing why a remote command did not succeed. */
|
|
35
|
+
function describeExecResult(result: DshSshExecResult): string {
|
|
36
|
+
if (result.error !== undefined && result.error !== '') return boundedLine(result.error) ?? 'the connection failed'
|
|
37
|
+
if (result.timedOut) return 'the command timed out'
|
|
38
|
+
const stderr = boundedLine(result.stderr)
|
|
39
|
+
if (stderr !== undefined) return stderr
|
|
40
|
+
return result.exitCode === null ? 'the command exited without a code' : `the command exited with ${result.exitCode}`
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Default command timeout for remote git/workspace commands (5 min for large clones/pushes). */
|
|
44
|
+
export const WORKSPACE_COMMAND_TIMEOUT_MS = 300_000
|
|
45
|
+
|
|
46
|
+
/** Redact embedded credentials (`user:pass@`) from a clone URL before surfacing it in errors. */
|
|
47
|
+
export function redactGitUrl(url: string): string {
|
|
48
|
+
try {
|
|
49
|
+
const parsed = new URL(url)
|
|
50
|
+
if (parsed.username === '' && parsed.password === '') return url
|
|
51
|
+
parsed.username = '***'
|
|
52
|
+
parsed.password = ''
|
|
53
|
+
return parsed.toString()
|
|
54
|
+
} catch {
|
|
55
|
+
return url.replace(/\/\/[^/\s@]+@/g, '//***@')
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Run one command on the run's host.
|
|
61
|
+
* @param run - the run being served.
|
|
62
|
+
* @param command - command line built from quoted words.
|
|
63
|
+
* @param timeoutMs - optional timeout ceiling; defaults to {@link WORKSPACE_COMMAND_TIMEOUT_MS}.
|
|
64
|
+
* @returns what the command did.
|
|
65
|
+
* @throws SshWorkspaceError when the host could not be reached at all.
|
|
66
|
+
*/
|
|
67
|
+
export async function execRemote(
|
|
68
|
+
run: SshWorkspaceRun,
|
|
69
|
+
command: string,
|
|
70
|
+
timeoutMs = WORKSPACE_COMMAND_TIMEOUT_MS,
|
|
71
|
+
): Promise<DshSshExecResult> {
|
|
72
|
+
try {
|
|
73
|
+
return await run.ssh.exec(run.host, command, {
|
|
74
|
+
timeoutMs,
|
|
75
|
+
...(run.signal === undefined ? {} : { signal: run.signal }),
|
|
76
|
+
})
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (isAbort(error)) throw error
|
|
79
|
+
throw transportFailure(`running a command on ${run.host} failed`, error)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Run one command and require it to succeed.
|
|
85
|
+
* @param run - the run being served.
|
|
86
|
+
* @param command - command line built from quoted words.
|
|
87
|
+
* @param what - what the command does, for the diagnostic.
|
|
88
|
+
* @returns the finished command.
|
|
89
|
+
* @throws SshWorkspaceError when it failed or the host was unreachable.
|
|
90
|
+
*/
|
|
91
|
+
export async function execRemoteChecked(
|
|
92
|
+
run: SshWorkspaceRun,
|
|
93
|
+
command: string,
|
|
94
|
+
what: string,
|
|
95
|
+
): Promise<DshSshExecResult> {
|
|
96
|
+
const result = await execRemote(run, command)
|
|
97
|
+
if (result.success) return result
|
|
98
|
+
throw new SshWorkspaceError(`${what} failed on ${run.host}: ${describeExecResult(result)}`)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Run one probe and report whether it held.
|
|
103
|
+
*
|
|
104
|
+
* A probe's non-zero exit is an answer, not a failure: it is how "this
|
|
105
|
+
* directory does not exist" and "this branch is gone" are read without an
|
|
106
|
+
* exception per negative case.
|
|
107
|
+
* @param run - the run being served.
|
|
108
|
+
* @param command - command line that exits zero when the questioned fact holds.
|
|
109
|
+
* @returns whether it held.
|
|
110
|
+
* @throws SshWorkspaceError when the host could not be reached.
|
|
111
|
+
*/
|
|
112
|
+
export async function probeRemote(run: SshWorkspaceRun, command: string): Promise<boolean> {
|
|
113
|
+
const result = await execRemote(run, command)
|
|
114
|
+
if (result.error !== undefined && result.error !== '') {
|
|
115
|
+
throw new SshWorkspaceError(`cannot reach ${run.host}: ${boundedLine(result.error) ?? 'the connection failed'}`)
|
|
116
|
+
}
|
|
117
|
+
return result.success
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Upload one local file to an absolute remote path.
|
|
122
|
+
* @param run - the run being served.
|
|
123
|
+
* @param localPath - file on this machine.
|
|
124
|
+
* @param remotePath - absolute destination path on the host.
|
|
125
|
+
* @throws SshWorkspaceError when the transfer failed.
|
|
126
|
+
*/
|
|
127
|
+
export async function uploadToHost(run: SshWorkspaceRun, localPath: string, remotePath: string): Promise<void> {
|
|
128
|
+
try {
|
|
129
|
+
await run.ssh.upload(run.host, localPath, remotePath)
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if (isAbort(error)) throw error
|
|
132
|
+
throw transportFailure(`sending ${localPath} to ${remotePath} on ${run.host} failed`, error)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Download one remote file to a local path.
|
|
138
|
+
* @param run - the run being served.
|
|
139
|
+
* @param remotePath - file on the host.
|
|
140
|
+
* @param localPath - destination path on this machine.
|
|
141
|
+
* @throws SshWorkspaceError when the transfer failed.
|
|
142
|
+
*/
|
|
143
|
+
export async function downloadFromHost(run: SshWorkspaceRun, remotePath: string, localPath: string): Promise<void> {
|
|
144
|
+
try {
|
|
145
|
+
await run.ssh.download(run.host, remotePath, localPath)
|
|
146
|
+
} catch (error) {
|
|
147
|
+
if (isAbort(error)) throw error
|
|
148
|
+
throw transportFailure(`bringing ${remotePath} back from ${run.host} failed`, error)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Remove one remote file, tolerating every failure.
|
|
154
|
+
*
|
|
155
|
+
* Bundles are the only temporary files this package puts on a host, and a
|
|
156
|
+
* leftover one changes no outcome: it is removed on the way past a successful
|
|
157
|
+
* transfer, and a broken connection during that removal must not turn a
|
|
158
|
+
* finished run into a failed one.
|
|
159
|
+
* @param run - the run being served.
|
|
160
|
+
* @param path - absolute remote file path.
|
|
161
|
+
*/
|
|
162
|
+
export async function removeRemoteFile(run: SshWorkspaceRun, path: string): Promise<void> {
|
|
163
|
+
try {
|
|
164
|
+
await execRemote(run, removeFileCommand(path))
|
|
165
|
+
} catch {
|
|
166
|
+
// Deliberate: see the note above.
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Build the command that removes one remote file, absence included.
|
|
172
|
+
*
|
|
173
|
+
* The only command this package composes outside `core/shell.ts`: that module
|
|
174
|
+
* owns directory removal (`rm -rf`) and nothing else, so a constant verb plus
|
|
175
|
+
* one quoted path is written here rather than there.
|
|
176
|
+
* @param path - absolute remote file path.
|
|
177
|
+
* @returns one shell command.
|
|
178
|
+
*/
|
|
179
|
+
export function removeFileCommand(path: string): string {
|
|
180
|
+
return `rm -f ${shellQuote(path)}`
|
|
181
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What this package adds to the frozen workspace-strategy interface.
|
|
3
|
+
*
|
|
4
|
+
* `src/core/workspace.ts` is the seam both halves were written against and it
|
|
5
|
+
* stays frozen: a strategy returns the directory the agent runs in, and the
|
|
6
|
+
* collection either carries a branch or does not. The one fact the card has to
|
|
7
|
+
* explain is how many local files the run did not receive, so the strategies
|
|
8
|
+
* here return a preparation that may carry that note, and the factory hands
|
|
9
|
+
* back a strategy narrowed to it. The narrowing is assignable to the frozen
|
|
10
|
+
* interface, so an engine written against `SshWorkspaceStrategy` accepts it
|
|
11
|
+
* unchanged.
|
|
12
|
+
*
|
|
13
|
+
* @module dsh-task-board-ssh/host/workspace/types
|
|
14
|
+
*/
|
|
15
|
+
import type {
|
|
16
|
+
SshWorkspacePreparation,
|
|
17
|
+
SshWorkspaceRun,
|
|
18
|
+
SshWorkspaceStrategy,
|
|
19
|
+
} from '../../core/workspace.ts'
|
|
20
|
+
|
|
21
|
+
/** What preparing a run produced, with the note this package adds to it. */
|
|
22
|
+
export interface SshWorkspacePreparationNote extends SshWorkspacePreparation {
|
|
23
|
+
/**
|
|
24
|
+
* Untracked files the run did not receive, because a bundle carries commits
|
|
25
|
+
* and not the untracked working tree. Absent for a strategy that never looks
|
|
26
|
+
* at a local repository, so `0` always means "counted, none".
|
|
27
|
+
*/
|
|
28
|
+
untrackedFiles?: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The strategy this package's factory builds: the frozen interface, sourced. */
|
|
32
|
+
export interface SshWorkspaceStrategyWithNote extends SshWorkspaceStrategy {
|
|
33
|
+
prepare(run: SshWorkspaceRun): Promise<SshWorkspacePreparationNote>
|
|
34
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The seam the placement strategies are assembled behind.
|
|
3
|
+
*
|
|
4
|
+
* The strategies themselves live in `src/host/workspace/` and are owned there;
|
|
5
|
+
* the engine only ever sees the frozen interface from `src/core/workspace.ts`
|
|
6
|
+
* through this resolver. That keeps two things true at once: the engine never
|
|
7
|
+
* imports a strategy implementation, and a deployment that serves none still
|
|
8
|
+
* mounts — a git run then fails on the card with a diagnosable reason instead of
|
|
9
|
+
* silently working in the wrong place.
|
|
10
|
+
*
|
|
11
|
+
* Two facts a run needs from the deployment live here as well, because both are
|
|
12
|
+
* about the workspace rather than about the run protocol: the local directory a
|
|
13
|
+
* `workspace` repository source points at, and the note a strategy attaches when
|
|
14
|
+
* it could not send every local file.
|
|
15
|
+
*
|
|
16
|
+
* @module dsh-task-board-ssh/host/workspace-seam
|
|
17
|
+
*/
|
|
18
|
+
import type { DshSshService, ServiceLookupSeat } from '../core/contract.ts'
|
|
19
|
+
import type { SshWorkspaceTarget } from '../core/target.ts'
|
|
20
|
+
import type { SshWorkspacePreparation, SshWorkspaceStrategy } from '../core/workspace.ts'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Resolve the placement strategy for one pinned workspace target.
|
|
24
|
+
*
|
|
25
|
+
* The whole target is passed rather than its kind, because which strategy serves
|
|
26
|
+
* a git run depends on where its repository comes from: a URL the host reaches
|
|
27
|
+
* and the local workspace sent as a bundle are two mechanisms behind one kind.
|
|
28
|
+
* Called once per run, at the moment the run is prepared, so it may answer
|
|
29
|
+
* differently as a deployment's strategies change.
|
|
30
|
+
*/
|
|
31
|
+
export type SshWorkspaceStrategyResolver = (target: SshWorkspaceTarget) => SshWorkspaceStrategy | undefined
|
|
32
|
+
|
|
33
|
+
/** What a strategy assembly receives. */
|
|
34
|
+
export interface SshWorkspaceAssemblyInput {
|
|
35
|
+
/** The shared `dshSsh` face: every remote operation a strategy makes goes through it. */
|
|
36
|
+
ssh: DshSshService
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The shape of `createWorkspaceStrategies`-style assembly in
|
|
41
|
+
* `src/host/workspace/index.ts`.
|
|
42
|
+
*
|
|
43
|
+
* The host entry assembles its resolver through that module's single entry and
|
|
44
|
+
* this declaration is the interface the two halves meet on.
|
|
45
|
+
*/
|
|
46
|
+
export type CreateSshWorkspaceStrategies = (input: SshWorkspaceAssemblyInput) => SshWorkspaceStrategyResolver
|
|
47
|
+
|
|
48
|
+
/** The resolver of a deployment that serves no placement strategy. */
|
|
49
|
+
export const NO_WORKSPACE_STRATEGIES: SshWorkspaceStrategyResolver = () => undefined
|
|
50
|
+
|
|
51
|
+
/** Cordis service name of the deployment's workspace registry. */
|
|
52
|
+
export const WORKSPACE_REGISTRY_SERVICE = 'workspaceRegistry'
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The slice of the deployment's workspace registry this package reads.
|
|
56
|
+
*
|
|
57
|
+
* The board resolves the same service for the same reason — a card that pins a
|
|
58
|
+
* workspace id has to become a directory before anything can run in it — and
|
|
59
|
+
* every member used here is one the runtime already publishes.
|
|
60
|
+
*/
|
|
61
|
+
export interface WorkspaceRegistrySeat {
|
|
62
|
+
/** Every workspace this deployment knows, in store order. */
|
|
63
|
+
list(): ReadonlyArray<{ id: string; path: string }>
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Resolve the workspace registry, when this deployment serves one.
|
|
68
|
+
* @param ctx - a seat that can read services by name.
|
|
69
|
+
* @returns the registry, or undefined when the deployment serves none.
|
|
70
|
+
*/
|
|
71
|
+
export function resolveWorkspaceRegistry(ctx: ServiceLookupSeat): WorkspaceRegistrySeat | undefined {
|
|
72
|
+
try {
|
|
73
|
+
const face = ctx.get(WORKSPACE_REGISTRY_SERVICE) as { list?: unknown } | undefined
|
|
74
|
+
if (face === undefined || typeof face.list !== 'function') return undefined
|
|
75
|
+
return face as WorkspaceRegistrySeat
|
|
76
|
+
} catch {
|
|
77
|
+
return undefined
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Resolve one workspace id to the local repository a git run bundles.
|
|
83
|
+
*
|
|
84
|
+
* A missing registry, a missing workspace and a path this deployment cannot read
|
|
85
|
+
* all answer undefined: the run then fails with the strategy's own "was not
|
|
86
|
+
* resolved" refusal, which says more than a rejected id would.
|
|
87
|
+
* @param registry - the deployment's registry, when it serves one.
|
|
88
|
+
* @param workspaceId - workspace the card's target names.
|
|
89
|
+
* @returns the absolute local directory, or undefined when it does not resolve.
|
|
90
|
+
*/
|
|
91
|
+
export function readWorkspaceDirectory(registry: WorkspaceRegistrySeat | undefined, workspaceId: string): string | undefined {
|
|
92
|
+
if (registry === undefined || workspaceId === '') return undefined
|
|
93
|
+
try {
|
|
94
|
+
const workspace = registry.list().find(entry => entry.id === workspaceId)
|
|
95
|
+
if (workspace === undefined) return undefined
|
|
96
|
+
return typeof workspace.path === 'string' && workspace.path !== '' ? workspace.path : undefined
|
|
97
|
+
} catch {
|
|
98
|
+
return undefined
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Read the untracked-file note a preparation may carry.
|
|
104
|
+
*
|
|
105
|
+
* The frozen `SshWorkspacePreparation` promises one member, the directory; the
|
|
106
|
+
* strategies in `src/host/workspace/` add this one, because a bundle carries
|
|
107
|
+
* commits and not the untracked working tree, and a card has to be able to say
|
|
108
|
+
* how much of it stayed behind. Read structurally: a strategy of this
|
|
109
|
+
* deployment's own that reports nothing is a strategy that sent everything.
|
|
110
|
+
* @param prepared - what a strategy returned.
|
|
111
|
+
* @returns the count, or undefined when the strategy did not measure one.
|
|
112
|
+
*/
|
|
113
|
+
export function readUntrackedFiles(prepared: SshWorkspacePreparation): number | undefined {
|
|
114
|
+
const value = (prepared as { untrackedFiles?: unknown }).untrackedFiles
|
|
115
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.trunc(value) : undefined
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* One sentence saying how many local files the run did not receive.
|
|
120
|
+
*
|
|
121
|
+
* It travels in the outcome the card records, which is the only free-text channel
|
|
122
|
+
* an external run has: the ledger keeps the account of a run, not a second
|
|
123
|
+
* schema field for a workspace detail.
|
|
124
|
+
* @param files - untracked files that stayed on this machine.
|
|
125
|
+
* @returns the sentence, or an empty string when there is nothing to say.
|
|
126
|
+
*/
|
|
127
|
+
export function untrackedNote(files: number | undefined): string {
|
|
128
|
+
if (files === undefined || files <= 0) return ''
|
|
129
|
+
return files === 1
|
|
130
|
+
? '1 untracked local file was not sent: a bundle carries commits, not the untracked working tree.'
|
|
131
|
+
: `${String(files)} untracked local files were not sent: a bundle carries commits, not the untracked working tree.`
|
|
132
|
+
}
|