@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.
- package/LICENSE +202 -0
- package/README.i18n.yaml +6 -0
- package/README.md +96 -2
- package/README.zh.md +97 -0
- package/cordis.patch.yml +15 -0
- package/icon.svg +5 -0
- package/lib/client.js +4435 -0
- package/lib/index.js +7082 -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 +453 -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 +149 -0
- package/lib/types/core/task-record.d.ts +74 -0
- package/lib/types/core/workspace.d.ts +74 -0
- package/lib/types/host/board.d.ts +75 -0
- package/lib/types/host/engine.d.ts +563 -0
- package/lib/types/host/extension.d.ts +61 -0
- package/lib/types/host/inspect.d.ts +99 -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 +137 -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 +17 -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 +515 -0
- package/src/core/launcher-script.ts +313 -0
- package/src/core/launcher.ts +283 -0
- package/src/core/ledger.ts +377 -0
- package/src/core/shell.ts +77 -0
- package/src/core/target.ts +420 -0
- package/src/core/task-record.ts +73 -0
- package/src/core/workspace.ts +78 -0
- package/src/host/board.ts +107 -0
- package/src/host/engine.ts +2026 -0
- package/src/host/extension.ts +310 -0
- package/src/host/inspect.ts +264 -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 +356 -0
- package/src/host/settings.ts +294 -0
- package/src/host/timers.ts +33 -0
- package/src/host/tools.ts +449 -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,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ledger payload this provider owns on a card.
|
|
3
|
+
*
|
|
4
|
+
* The board stores one opaque payload per extension under
|
|
5
|
+
* `TaskRecord.integrations[SSH_INTEGRATION_KEY]` and refuses anything above
|
|
6
|
+
* 64 KiB. Log bytes never enter it: the payload names the run and the account
|
|
7
|
+
* of what happened, and the log itself stays a remote file read by byte offset.
|
|
8
|
+
* Everything else here exists to keep that promise — the bounding constants,
|
|
9
|
+
* the bounded history, the one instruction a card owes its next run, and
|
|
10
|
+
* {@link trimSshLedgerPayload}, which is the one function a writer calls before
|
|
11
|
+
* storing.
|
|
12
|
+
*
|
|
13
|
+
* @module dsh-task-board-ssh/core/ledger
|
|
14
|
+
*/
|
|
15
|
+
import { TASK_BOARD_MESSAGE_MAX_LENGTH, TASK_BOARD_PAYLOAD_LIMIT_BYTES } from './contract.ts'
|
|
16
|
+
import {
|
|
17
|
+
parseSshRunRecord,
|
|
18
|
+
parseSshTarget,
|
|
19
|
+
type SshRunRecord,
|
|
20
|
+
type SshTarget,
|
|
21
|
+
type SshValidationIssue,
|
|
22
|
+
type SshValidationResult,
|
|
23
|
+
} from './target.ts'
|
|
24
|
+
|
|
25
|
+
/** Key this provider's payload occupies inside `TaskRecord.integrations`. */
|
|
26
|
+
export const SSH_INTEGRATION_KEY = 'ssh'
|
|
27
|
+
|
|
28
|
+
/** Schema version of {@link SshLedgerPayload}; a payload at another version is refused. */
|
|
29
|
+
export const SSH_LEDGER_VERSION = 1
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Budget for one stored payload, in bytes of its JSON serialization. It equals
|
|
33
|
+
* the board's own ceiling and is kept below it: the board refuses a payload
|
|
34
|
+
* that is not strictly under its limit, so trimming targets the same number.
|
|
35
|
+
*/
|
|
36
|
+
export const SSH_LEDGER_LIMIT_BYTES = TASK_BOARD_PAYLOAD_LIMIT_BYTES
|
|
37
|
+
|
|
38
|
+
/** How many run records one payload keeps by default. */
|
|
39
|
+
export const SSH_HISTORY_LIMIT = 50
|
|
40
|
+
|
|
41
|
+
/** Longest stored agent summary, in characters. */
|
|
42
|
+
export const SSH_SUMMARY_MAX_LENGTH = 512
|
|
43
|
+
|
|
44
|
+
/** Longest stored failure text, in characters. */
|
|
45
|
+
export const SSH_ERROR_MAX_LENGTH = 512
|
|
46
|
+
|
|
47
|
+
/** Longest stored branch name, in characters. */
|
|
48
|
+
export const SSH_BRANCH_MAX_LENGTH = 200
|
|
49
|
+
|
|
50
|
+
/** Longest stored session id, in characters. */
|
|
51
|
+
export const SSH_SESSION_ID_MAX_LENGTH = 200
|
|
52
|
+
|
|
53
|
+
/** Longest stored instruction text, in characters; the board's own message ceiling. */
|
|
54
|
+
export const SSH_PENDING_TEXT_MAX_LENGTH = TASK_BOARD_MESSAGE_MAX_LENGTH
|
|
55
|
+
|
|
56
|
+
/** Which entry queued an instruction for the card's next run. */
|
|
57
|
+
export type SshPendingKind = 'follow-up' | 'rework'
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* One instruction the card owes a run: the follow-up or rework message that
|
|
61
|
+
* arrived while a run was already in flight.
|
|
62
|
+
*
|
|
63
|
+
* It is stored beside the frozen run account rather than in it, because it is
|
|
64
|
+
* not a record of what happened — it is work still to do. A card carries at most
|
|
65
|
+
* one, and {@link trimSshLedgerPayload} treats it as the last thing it may drop:
|
|
66
|
+
* the account of finished runs can shrink, but a message a person sent is
|
|
67
|
+
* carried until nothing else is left.
|
|
68
|
+
*/
|
|
69
|
+
export interface SshPendingInstruction {
|
|
70
|
+
/** Which entry asked for it. */
|
|
71
|
+
kind: SshPendingKind
|
|
72
|
+
/** Text the next run's prompt carries, bounded like every board message. */
|
|
73
|
+
text: string
|
|
74
|
+
/** Instant it was queued (ms epoch). */
|
|
75
|
+
queuedAt: number
|
|
76
|
+
/** Session known when it was queued, when the card had one. */
|
|
77
|
+
resumeSessionId?: string
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* What one card remembers about its remote runs.
|
|
82
|
+
*
|
|
83
|
+
* `activeRun` is the run the card is waiting on; `history` keeps the bounded
|
|
84
|
+
* past, oldest first; `pending` is the one instruction still owed to a future
|
|
85
|
+
* run. None of them carries log bytes.
|
|
86
|
+
*/
|
|
87
|
+
export interface SshLedgerPayload {
|
|
88
|
+
/** Schema version; a reader that does not know it refuses the payload. */
|
|
89
|
+
version: typeof SSH_LEDGER_VERSION
|
|
90
|
+
/** Instant this payload was last written (ms epoch). */
|
|
91
|
+
updatedAt: number
|
|
92
|
+
/** The target the card pins, absent while the card pins none. */
|
|
93
|
+
target?: SshTarget
|
|
94
|
+
/** The run in flight, absent between runs. */
|
|
95
|
+
activeRun?: SshRunRecord
|
|
96
|
+
/** Bounded run history, oldest first. */
|
|
97
|
+
history: SshRunRecord[]
|
|
98
|
+
/** The one instruction a future run still owes, absent when none is queued. */
|
|
99
|
+
pending?: SshPendingInstruction
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** What one trimming pass may change. */
|
|
103
|
+
export interface SshLedgerTrimOptions {
|
|
104
|
+
/** Byte budget for the serialized result; defaults to {@link SSH_LEDGER_LIMIT_BYTES}. */
|
|
105
|
+
maxBytes?: number
|
|
106
|
+
/** How many history records to keep at most; defaults to {@link SSH_HISTORY_LIMIT}. */
|
|
107
|
+
maxHistory?: number
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Cut text to a character budget without splitting a surrogate pair. */
|
|
111
|
+
function boundText(text: string, max: number): string {
|
|
112
|
+
if (text.length <= max) return text
|
|
113
|
+
const characters = Array.from(text)
|
|
114
|
+
if (characters.length <= max) return text
|
|
115
|
+
return characters.slice(0, max).join('')
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Bound an optional string field, turning empty strings into undefined so `parseSshRunRecord` keeps the record. */
|
|
119
|
+
function boundOptionalText(text: string | undefined, max: number): string | undefined {
|
|
120
|
+
if (text === undefined || text.length === 0) return undefined
|
|
121
|
+
return boundText(text, max)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Copy one run record with every free-text field cut to its stored budget. */
|
|
125
|
+
function boundRun(record: SshRunRecord): SshRunRecord {
|
|
126
|
+
const sessionId = boundOptionalText(record.sessionId, SSH_SESSION_ID_MAX_LENGTH)
|
|
127
|
+
const branch = boundOptionalText(record.branch, SSH_BRANCH_MAX_LENGTH)
|
|
128
|
+
const summary = boundOptionalText(record.summary, SSH_SUMMARY_MAX_LENGTH)
|
|
129
|
+
const error = boundOptionalText(record.error, SSH_ERROR_MAX_LENGTH)
|
|
130
|
+
const next: SshRunRecord = {
|
|
131
|
+
runId: boundText(record.runId, 128),
|
|
132
|
+
phase: record.phase,
|
|
133
|
+
startedAt: record.startedAt,
|
|
134
|
+
}
|
|
135
|
+
if (record.finishedAt !== undefined) next.finishedAt = record.finishedAt
|
|
136
|
+
if (record.exitCode !== undefined) next.exitCode = record.exitCode
|
|
137
|
+
if (sessionId !== undefined) next.sessionId = sessionId
|
|
138
|
+
if (record.bytes !== undefined) next.bytes = record.bytes
|
|
139
|
+
if (branch !== undefined) next.branch = branch
|
|
140
|
+
if (summary !== undefined) next.summary = summary
|
|
141
|
+
if (error !== undefined) next.error = error
|
|
142
|
+
if (record.followUps !== undefined) next.followUps = record.followUps
|
|
143
|
+
if (record.executionId !== undefined) next.executionId = boundText(record.executionId, 128)
|
|
144
|
+
if (record.lastActivityAt !== undefined) next.lastActivityAt = record.lastActivityAt
|
|
145
|
+
if (record.errorCode !== undefined) next.errorCode = boundText(record.errorCode, 64)
|
|
146
|
+
return next
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Copy one run record with the optional free text removed. */
|
|
150
|
+
function stripRunText(record: SshRunRecord): SshRunRecord {
|
|
151
|
+
const stripped: SshRunRecord = {
|
|
152
|
+
runId: record.runId,
|
|
153
|
+
phase: record.phase,
|
|
154
|
+
startedAt: record.startedAt,
|
|
155
|
+
}
|
|
156
|
+
if (record.finishedAt !== undefined) stripped.finishedAt = record.finishedAt
|
|
157
|
+
if (record.exitCode !== undefined) stripped.exitCode = record.exitCode
|
|
158
|
+
if (record.sessionId !== undefined) stripped.sessionId = record.sessionId
|
|
159
|
+
if (record.bytes !== undefined) stripped.bytes = record.bytes
|
|
160
|
+
if (record.followUps !== undefined) stripped.followUps = record.followUps
|
|
161
|
+
if (record.executionId !== undefined) stripped.executionId = record.executionId
|
|
162
|
+
if (record.lastActivityAt !== undefined) stripped.lastActivityAt = record.lastActivityAt
|
|
163
|
+
return stripped
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Copy one instruction with both free-text fields cut to their stored budget. */
|
|
167
|
+
function boundPending(pending: SshPendingInstruction): SshPendingInstruction {
|
|
168
|
+
const bounded: SshPendingInstruction = {
|
|
169
|
+
kind: pending.kind,
|
|
170
|
+
text: boundText(pending.text, SSH_PENDING_TEXT_MAX_LENGTH),
|
|
171
|
+
queuedAt: pending.queuedAt,
|
|
172
|
+
}
|
|
173
|
+
if (pending.resumeSessionId !== undefined) bounded.resumeSessionId = boundText(pending.resumeSessionId, SSH_SESSION_ID_MAX_LENGTH)
|
|
174
|
+
return bounded
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** UTF-8 byte length of one JSON value. */
|
|
178
|
+
function jsonBytes(value: unknown): number {
|
|
179
|
+
return new TextEncoder().encode(JSON.stringify(value)).length
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Byte length of a payload as the board measures it.
|
|
184
|
+
* @param payload - payload to measure.
|
|
185
|
+
* @returns UTF-8 byte length of its JSON serialization.
|
|
186
|
+
*/
|
|
187
|
+
export function sshLedgerPayloadBytes(payload: SshLedgerPayload): number {
|
|
188
|
+
return jsonBytes(payload)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Cut a payload down to the stored budget.
|
|
193
|
+
*
|
|
194
|
+
* The order is fixed and lossy on purpose. Free text is bounded first, and the
|
|
195
|
+
* queued instruction's own bytes are reserved before anything else is measured,
|
|
196
|
+
* so a pass that trims history can never be the reason a person's message is
|
|
197
|
+
* lost. What then goes, in this order: the oldest run records, the active run's
|
|
198
|
+
* optional text, the active run itself, the pinned target — and only with
|
|
199
|
+
* nothing else left, the queued instruction. A payload therefore always comes
|
|
200
|
+
* back strictly under `maxBytes`, and the newest history is the last account of
|
|
201
|
+
* the run to be dropped.
|
|
202
|
+
* @param payload - payload to trim; not mutated.
|
|
203
|
+
* @param options - byte budget and history cap.
|
|
204
|
+
* @returns a payload whose serialization is under the budget.
|
|
205
|
+
*/
|
|
206
|
+
export function trimSshLedgerPayload(payload: SshLedgerPayload, options: SshLedgerTrimOptions = {}): SshLedgerPayload {
|
|
207
|
+
const maxBytes = options.maxBytes ?? SSH_LEDGER_LIMIT_BYTES
|
|
208
|
+
const maxHistory = Math.max(0, options.maxHistory ?? SSH_HISTORY_LIMIT)
|
|
209
|
+
const initialHistory = payload.history.map(boundRun)
|
|
210
|
+
const cappedHistory = initialHistory.length > maxHistory ? initialHistory.slice(initialHistory.length - maxHistory) : initialHistory
|
|
211
|
+
const initialActiveRun = payload.activeRun === undefined ? undefined : boundRun(payload.activeRun)
|
|
212
|
+
const initialTarget = payload.target
|
|
213
|
+
let history = [...cappedHistory]
|
|
214
|
+
let activeRun = initialActiveRun
|
|
215
|
+
let target = initialTarget
|
|
216
|
+
let pending = payload.pending === undefined ? undefined : boundPending(payload.pending)
|
|
217
|
+
|
|
218
|
+
const build = (): SshLedgerPayload => {
|
|
219
|
+
const next: SshLedgerPayload = { version: SSH_LEDGER_VERSION, updatedAt: payload.updatedAt, history }
|
|
220
|
+
if (target !== undefined) next.target = target
|
|
221
|
+
if (activeRun !== undefined) next.activeRun = activeRun
|
|
222
|
+
if (pending !== undefined) next.pending = pending
|
|
223
|
+
return next
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Reduce the run account until the full payload (including `pending`) fits `budget`. */
|
|
227
|
+
const shrink = (budget: number): SshLedgerPayload => {
|
|
228
|
+
let candidate = build()
|
|
229
|
+
while (jsonBytes(candidate) >= budget) {
|
|
230
|
+
if (history.length > 0) {
|
|
231
|
+
history = history.slice(1)
|
|
232
|
+
candidate = build()
|
|
233
|
+
continue
|
|
234
|
+
}
|
|
235
|
+
if (activeRun !== undefined && (activeRun.branch !== undefined || activeRun.summary !== undefined || activeRun.error !== undefined)) {
|
|
236
|
+
activeRun = stripRunText(activeRun)
|
|
237
|
+
candidate = build()
|
|
238
|
+
continue
|
|
239
|
+
}
|
|
240
|
+
if (activeRun !== undefined) {
|
|
241
|
+
activeRun = undefined
|
|
242
|
+
candidate = build()
|
|
243
|
+
continue
|
|
244
|
+
}
|
|
245
|
+
if (target !== undefined) {
|
|
246
|
+
target = undefined
|
|
247
|
+
candidate = build()
|
|
248
|
+
continue
|
|
249
|
+
}
|
|
250
|
+
break
|
|
251
|
+
}
|
|
252
|
+
return candidate
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// `build()` already includes `pending`, so shrinking directly against
|
|
256
|
+
// `maxBytes` preserves `pending` without double-counting its byte size.
|
|
257
|
+
const candidate = shrink(maxBytes)
|
|
258
|
+
if (jsonBytes(candidate) < maxBytes || pending === undefined) return candidate
|
|
259
|
+
// Even with the entire run account stripped, `pending` alone exceeds the
|
|
260
|
+
// budget: drop `pending`, restore the full run account, and trim it cleanly
|
|
261
|
+
// against the full `maxBytes` budget.
|
|
262
|
+
pending = undefined
|
|
263
|
+
history = [...cappedHistory]
|
|
264
|
+
activeRun = initialActiveRun
|
|
265
|
+
target = initialTarget
|
|
266
|
+
return shrink(maxBytes)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Read one queued instruction.
|
|
271
|
+
*
|
|
272
|
+
* Host-owned bookkeeping that a card may also have carried across builds, so
|
|
273
|
+
* every field is re-validated and an unknown member makes the instruction
|
|
274
|
+
* unusable: a message this build cannot interpret is not one it may hand an
|
|
275
|
+
* agent. Nothing is trimmed here — the parsers report, the trimmer bounds.
|
|
276
|
+
* @param value - the stored member.
|
|
277
|
+
* @returns the instruction, or undefined when it is absent or unusable.
|
|
278
|
+
*/
|
|
279
|
+
function parsePendingInstruction(value: unknown): SshPendingInstruction | undefined {
|
|
280
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) return undefined
|
|
281
|
+
const record = value as Record<string, unknown>
|
|
282
|
+
const known = new Set(['kind', 'text', 'queuedAt', 'resumeSessionId'])
|
|
283
|
+
if (Object.keys(record).some(key => !known.has(key))) return undefined
|
|
284
|
+
const kind = record.kind === 'follow-up' || record.kind === 'rework' ? record.kind : undefined
|
|
285
|
+
const text = typeof record.text === 'string' && record.text.trim() !== '' && record.text.length <= SSH_PENDING_TEXT_MAX_LENGTH
|
|
286
|
+
? record.text
|
|
287
|
+
: undefined
|
|
288
|
+
const queuedAt = typeof record.queuedAt === 'number' && Number.isFinite(record.queuedAt) ? record.queuedAt : undefined
|
|
289
|
+
if (kind === undefined || text === undefined || queuedAt === undefined) return undefined
|
|
290
|
+
const pending: SshPendingInstruction = { kind, text, queuedAt }
|
|
291
|
+
if (record.resumeSessionId !== undefined) {
|
|
292
|
+
const sessionId = typeof record.resumeSessionId === 'string' && record.resumeSessionId !== ''
|
|
293
|
+
&& record.resumeSessionId.length <= SSH_SESSION_ID_MAX_LENGTH
|
|
294
|
+
? record.resumeSessionId
|
|
295
|
+
: undefined
|
|
296
|
+
if (sessionId === undefined) return undefined
|
|
297
|
+
pending.resumeSessionId = sessionId
|
|
298
|
+
}
|
|
299
|
+
return pending
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Parse one stored payload.
|
|
304
|
+
*
|
|
305
|
+
* The container is read strictly (version, timestamp, history array) while a
|
|
306
|
+
* single malformed history entry — or an unusable queued instruction — is
|
|
307
|
+
* dropped alone, the same rule the board applies to integrations: one bad member
|
|
308
|
+
* never takes the card's account with it. Nothing here throws, so a corrupted
|
|
309
|
+
* payload degrades to a readable one.
|
|
310
|
+
* @param value - candidate payload from the ledger or an action.
|
|
311
|
+
* @returns the payload, or every reason it was refused.
|
|
312
|
+
*/
|
|
313
|
+
export function parseSshLedgerPayload(value: unknown): SshValidationResult<SshLedgerPayload> {
|
|
314
|
+
const errors: SshValidationIssue[] = []
|
|
315
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
316
|
+
return { ok: false, errors: [{ path: '', message: 'must be an object' }] }
|
|
317
|
+
}
|
|
318
|
+
const record = value as Record<string, unknown>
|
|
319
|
+
if (record.version !== SSH_LEDGER_VERSION) {
|
|
320
|
+
return { ok: false, errors: [{ path: 'version', message: `must be ${SSH_LEDGER_VERSION}` }] }
|
|
321
|
+
}
|
|
322
|
+
if (typeof record.updatedAt !== 'number' || !Number.isFinite(record.updatedAt)) {
|
|
323
|
+
errors.push({ path: 'updatedAt', message: 'must be a finite millisecond timestamp' })
|
|
324
|
+
}
|
|
325
|
+
let target: SshTarget | undefined
|
|
326
|
+
if (record.target !== undefined) {
|
|
327
|
+
const parsed = parseSshTarget(record.target)
|
|
328
|
+
if (parsed.ok) target = parsed.value
|
|
329
|
+
else errors.push(...parsed.errors.map(issue => ({ path: issue.path === '' ? 'target' : `target.${issue.path}`, message: issue.message })))
|
|
330
|
+
}
|
|
331
|
+
let activeRun: SshRunRecord | undefined
|
|
332
|
+
if (record.activeRun !== undefined) {
|
|
333
|
+
const parsed = parseSshRunRecord(record.activeRun)
|
|
334
|
+
if (parsed.ok) activeRun = parsed.value
|
|
335
|
+
else errors.push(...parsed.errors.map(issue => ({ path: issue.path === '' ? 'activeRun' : `activeRun.${issue.path}`, message: issue.message })))
|
|
336
|
+
}
|
|
337
|
+
const history: SshRunRecord[] = []
|
|
338
|
+
if (record.history !== undefined) {
|
|
339
|
+
if (!Array.isArray(record.history)) {
|
|
340
|
+
errors.push({ path: 'history', message: 'must be an array' })
|
|
341
|
+
} else {
|
|
342
|
+
for (const entry of record.history) {
|
|
343
|
+
const parsed = parseSshRunRecord(entry)
|
|
344
|
+
// A dropped entry is not an error of the payload: it is one record the
|
|
345
|
+
// card can no longer account for, and the rest still describes the card.
|
|
346
|
+
if (parsed.ok) history.push(parsed.value)
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (errors.length > 0) return { ok: false, errors }
|
|
351
|
+
const payload: SshLedgerPayload = { version: SSH_LEDGER_VERSION, updatedAt: record.updatedAt as number, history }
|
|
352
|
+
if (target !== undefined) payload.target = target
|
|
353
|
+
if (activeRun !== undefined) payload.activeRun = activeRun
|
|
354
|
+
const pending = parsePendingInstruction(record.pending)
|
|
355
|
+
if (pending !== undefined) payload.pending = pending
|
|
356
|
+
return { ok: true, value: payload }
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* The payload a create dialog reports for a card that does not exist yet: the
|
|
361
|
+
* target the new card pins, and no run account beside it.
|
|
362
|
+
*
|
|
363
|
+
* A card being created has never run, so the document carries an empty history
|
|
364
|
+
* and no active run or queued instruction — exactly what the host half stores
|
|
365
|
+
* when it pins a target on an existing card, minus the runs. The result is read
|
|
366
|
+
* back through {@link parseSshLedgerPayload} before it is returned: a draft this
|
|
367
|
+
* build could store but not read back is never reported at all, so a card
|
|
368
|
+
* created through a create dialog always opens on a payload its own detail seat
|
|
369
|
+
* can account for.
|
|
370
|
+
* @param target - the target the reader picked, already parsed.
|
|
371
|
+
* @param updatedAt - instant the draft is reported (ms epoch).
|
|
372
|
+
* @returns the payload, or undefined when the document does not parse.
|
|
373
|
+
*/
|
|
374
|
+
export function composeDraftPayload(target: SshTarget, updatedAt: number): SshLedgerPayload | undefined {
|
|
375
|
+
const draft: SshLedgerPayload = { version: SSH_LEDGER_VERSION, updatedAt, history: [], target }
|
|
376
|
+
return parseSshLedgerPayload(draft).ok ? draft : undefined
|
|
377
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* POSIX shell quoting for every value this provider sends to a remote host.
|
|
3
|
+
*
|
|
4
|
+
* The remote side is an arbitrary `sh` on a machine the provider does not own,
|
|
5
|
+
* so nothing is interpolated: each value becomes one single-quoted word, and a
|
|
6
|
+
* value that cannot survive an `execve` argument list is refused instead of
|
|
7
|
+
* silently truncated. Callers compose command lines out of these functions and
|
|
8
|
+
* never concatenate raw text into one.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-task-board-ssh/core/shell
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Quote one value as a single POSIX shell word.
|
|
15
|
+
*
|
|
16
|
+
* The result is a single-quoted string, so the remote shell expands nothing
|
|
17
|
+
* inside it: `$(...)`, backticks, newlines, semicolons and globs stay literal.
|
|
18
|
+
* An embedded quote is closed, emitted as an escaped quote, and reopened, which
|
|
19
|
+
* is the only form that survives quoting inside a quoted string.
|
|
20
|
+
* @param value - text to quote.
|
|
21
|
+
* @returns one shell word that evaluates back to `value`.
|
|
22
|
+
* @throws RangeError when the value contains a NUL byte, which no shell word can carry.
|
|
23
|
+
*/
|
|
24
|
+
export function shellQuote(value: string): string {
|
|
25
|
+
if (value.includes('\u0000')) throw new RangeError('a shell word cannot carry a NUL byte')
|
|
26
|
+
return `'${value.split("'").join("'\\''")}'`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Quote and join several words into one command line.
|
|
31
|
+
* @param words - command and arguments, unquoted.
|
|
32
|
+
* @returns the command line with every word quoted separately.
|
|
33
|
+
*/
|
|
34
|
+
export function shellJoin(words: readonly string[]): string {
|
|
35
|
+
return words.map(shellQuote).join(' ')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Build the command that ensures one remote directory exists.
|
|
40
|
+
* @param path - absolute remote directory.
|
|
41
|
+
* @returns one shell command.
|
|
42
|
+
*/
|
|
43
|
+
export function shellEnsureDir(path: string): string {
|
|
44
|
+
return `mkdir -p ${shellQuote(path)}`
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Build the command that writes one file's exact bytes on the remote host.
|
|
49
|
+
*
|
|
50
|
+
* `printf '%s'` copies its argument verbatim, so the content travels as one
|
|
51
|
+
* quoted word: no heredoc delimiter has to be invented and no line is
|
|
52
|
+
* reinterpreted. The command line's own length is the only limit.
|
|
53
|
+
* @param path - absolute remote path.
|
|
54
|
+
* @param content - UTF-8 text without NUL bytes.
|
|
55
|
+
* @returns one shell command.
|
|
56
|
+
*/
|
|
57
|
+
export function shellWriteFile(path: string, content: string): string {
|
|
58
|
+
return `printf '%s' ${shellQuote(content)} > ${shellQuote(path)}`
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Build one command that writes several files, in order.
|
|
63
|
+
* @param entries - remote path and content pairs.
|
|
64
|
+
* @returns one shell command, or an empty string for an empty list.
|
|
65
|
+
*/
|
|
66
|
+
export function shellWriteFiles(entries: readonly { path: string; content: string }[]): string {
|
|
67
|
+
return entries.map(entry => shellWriteFile(entry.path, entry.content)).join(' && ')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Build the command that removes one remote directory, tolerating its absence.
|
|
72
|
+
* @param path - absolute remote directory.
|
|
73
|
+
* @returns one shell command.
|
|
74
|
+
*/
|
|
75
|
+
export function shellRemoveDir(path: string): string {
|
|
76
|
+
return `rm -rf ${shellQuote(path)}`
|
|
77
|
+
}
|