@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,372 @@
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
+ return next
144
+ }
145
+
146
+ /** Copy one run record with the optional free text removed. */
147
+ function stripRunText(record: SshRunRecord): SshRunRecord {
148
+ const stripped: SshRunRecord = {
149
+ runId: record.runId,
150
+ phase: record.phase,
151
+ startedAt: record.startedAt,
152
+ }
153
+ if (record.finishedAt !== undefined) stripped.finishedAt = record.finishedAt
154
+ if (record.exitCode !== undefined) stripped.exitCode = record.exitCode
155
+ if (record.sessionId !== undefined) stripped.sessionId = record.sessionId
156
+ if (record.bytes !== undefined) stripped.bytes = record.bytes
157
+ if (record.followUps !== undefined) stripped.followUps = record.followUps
158
+ return stripped
159
+ }
160
+
161
+ /** Copy one instruction with both free-text fields cut to their stored budget. */
162
+ function boundPending(pending: SshPendingInstruction): SshPendingInstruction {
163
+ const bounded: SshPendingInstruction = {
164
+ kind: pending.kind,
165
+ text: boundText(pending.text, SSH_PENDING_TEXT_MAX_LENGTH),
166
+ queuedAt: pending.queuedAt,
167
+ }
168
+ if (pending.resumeSessionId !== undefined) bounded.resumeSessionId = boundText(pending.resumeSessionId, SSH_SESSION_ID_MAX_LENGTH)
169
+ return bounded
170
+ }
171
+
172
+ /** UTF-8 byte length of one JSON value. */
173
+ function jsonBytes(value: unknown): number {
174
+ return new TextEncoder().encode(JSON.stringify(value)).length
175
+ }
176
+
177
+ /**
178
+ * Byte length of a payload as the board measures it.
179
+ * @param payload - payload to measure.
180
+ * @returns UTF-8 byte length of its JSON serialization.
181
+ */
182
+ export function sshLedgerPayloadBytes(payload: SshLedgerPayload): number {
183
+ return jsonBytes(payload)
184
+ }
185
+
186
+ /**
187
+ * Cut a payload down to the stored budget.
188
+ *
189
+ * The order is fixed and lossy on purpose. Free text is bounded first, and the
190
+ * queued instruction's own bytes are reserved before anything else is measured,
191
+ * so a pass that trims history can never be the reason a person's message is
192
+ * lost. What then goes, in this order: the oldest run records, the active run's
193
+ * optional text, the active run itself, the pinned target — and only with
194
+ * nothing else left, the queued instruction. A payload therefore always comes
195
+ * back strictly under `maxBytes`, and the newest history is the last account of
196
+ * the run to be dropped.
197
+ * @param payload - payload to trim; not mutated.
198
+ * @param options - byte budget and history cap.
199
+ * @returns a payload whose serialization is under the budget.
200
+ */
201
+ export function trimSshLedgerPayload(payload: SshLedgerPayload, options: SshLedgerTrimOptions = {}): SshLedgerPayload {
202
+ const maxBytes = options.maxBytes ?? SSH_LEDGER_LIMIT_BYTES
203
+ const maxHistory = Math.max(0, options.maxHistory ?? SSH_HISTORY_LIMIT)
204
+ const initialHistory = payload.history.map(boundRun)
205
+ const cappedHistory = initialHistory.length > maxHistory ? initialHistory.slice(initialHistory.length - maxHistory) : initialHistory
206
+ const initialActiveRun = payload.activeRun === undefined ? undefined : boundRun(payload.activeRun)
207
+ const initialTarget = payload.target
208
+ let history = [...cappedHistory]
209
+ let activeRun = initialActiveRun
210
+ let target = initialTarget
211
+ let pending = payload.pending === undefined ? undefined : boundPending(payload.pending)
212
+
213
+ const build = (): SshLedgerPayload => {
214
+ const next: SshLedgerPayload = { version: SSH_LEDGER_VERSION, updatedAt: payload.updatedAt, history }
215
+ if (target !== undefined) next.target = target
216
+ if (activeRun !== undefined) next.activeRun = activeRun
217
+ if (pending !== undefined) next.pending = pending
218
+ return next
219
+ }
220
+
221
+ /** Reduce the run account until the full payload (including `pending`) fits `budget`. */
222
+ const shrink = (budget: number): SshLedgerPayload => {
223
+ let candidate = build()
224
+ while (jsonBytes(candidate) >= budget) {
225
+ if (history.length > 0) {
226
+ history = history.slice(1)
227
+ candidate = build()
228
+ continue
229
+ }
230
+ if (activeRun !== undefined && (activeRun.branch !== undefined || activeRun.summary !== undefined || activeRun.error !== undefined)) {
231
+ activeRun = stripRunText(activeRun)
232
+ candidate = build()
233
+ continue
234
+ }
235
+ if (activeRun !== undefined) {
236
+ activeRun = undefined
237
+ candidate = build()
238
+ continue
239
+ }
240
+ if (target !== undefined) {
241
+ target = undefined
242
+ candidate = build()
243
+ continue
244
+ }
245
+ break
246
+ }
247
+ return candidate
248
+ }
249
+
250
+ // `build()` already includes `pending`, so shrinking directly against
251
+ // `maxBytes` preserves `pending` without double-counting its byte size.
252
+ const candidate = shrink(maxBytes)
253
+ if (jsonBytes(candidate) < maxBytes || pending === undefined) return candidate
254
+ // Even with the entire run account stripped, `pending` alone exceeds the
255
+ // budget: drop `pending`, restore the full run account, and trim it cleanly
256
+ // against the full `maxBytes` budget.
257
+ pending = undefined
258
+ history = [...cappedHistory]
259
+ activeRun = initialActiveRun
260
+ target = initialTarget
261
+ return shrink(maxBytes)
262
+ }
263
+
264
+ /**
265
+ * Read one queued instruction.
266
+ *
267
+ * Host-owned bookkeeping that a card may also have carried across builds, so
268
+ * every field is re-validated and an unknown member makes the instruction
269
+ * unusable: a message this build cannot interpret is not one it may hand an
270
+ * agent. Nothing is trimmed here — the parsers report, the trimmer bounds.
271
+ * @param value - the stored member.
272
+ * @returns the instruction, or undefined when it is absent or unusable.
273
+ */
274
+ function parsePendingInstruction(value: unknown): SshPendingInstruction | undefined {
275
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) return undefined
276
+ const record = value as Record<string, unknown>
277
+ const known = new Set(['kind', 'text', 'queuedAt', 'resumeSessionId'])
278
+ if (Object.keys(record).some(key => !known.has(key))) return undefined
279
+ const kind = record.kind === 'follow-up' || record.kind === 'rework' ? record.kind : undefined
280
+ const text = typeof record.text === 'string' && record.text.trim() !== '' && record.text.length <= SSH_PENDING_TEXT_MAX_LENGTH
281
+ ? record.text
282
+ : undefined
283
+ const queuedAt = typeof record.queuedAt === 'number' && Number.isFinite(record.queuedAt) ? record.queuedAt : undefined
284
+ if (kind === undefined || text === undefined || queuedAt === undefined) return undefined
285
+ const pending: SshPendingInstruction = { kind, text, queuedAt }
286
+ if (record.resumeSessionId !== undefined) {
287
+ const sessionId = typeof record.resumeSessionId === 'string' && record.resumeSessionId !== ''
288
+ && record.resumeSessionId.length <= SSH_SESSION_ID_MAX_LENGTH
289
+ ? record.resumeSessionId
290
+ : undefined
291
+ if (sessionId === undefined) return undefined
292
+ pending.resumeSessionId = sessionId
293
+ }
294
+ return pending
295
+ }
296
+
297
+ /**
298
+ * Parse one stored payload.
299
+ *
300
+ * The container is read strictly (version, timestamp, history array) while a
301
+ * single malformed history entry — or an unusable queued instruction — is
302
+ * dropped alone, the same rule the board applies to integrations: one bad member
303
+ * never takes the card's account with it. Nothing here throws, so a corrupted
304
+ * payload degrades to a readable one.
305
+ * @param value - candidate payload from the ledger or an action.
306
+ * @returns the payload, or every reason it was refused.
307
+ */
308
+ export function parseSshLedgerPayload(value: unknown): SshValidationResult<SshLedgerPayload> {
309
+ const errors: SshValidationIssue[] = []
310
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
311
+ return { ok: false, errors: [{ path: '', message: 'must be an object' }] }
312
+ }
313
+ const record = value as Record<string, unknown>
314
+ if (record.version !== SSH_LEDGER_VERSION) {
315
+ return { ok: false, errors: [{ path: 'version', message: `must be ${SSH_LEDGER_VERSION}` }] }
316
+ }
317
+ if (typeof record.updatedAt !== 'number' || !Number.isFinite(record.updatedAt)) {
318
+ errors.push({ path: 'updatedAt', message: 'must be a finite millisecond timestamp' })
319
+ }
320
+ let target: SshTarget | undefined
321
+ if (record.target !== undefined) {
322
+ const parsed = parseSshTarget(record.target)
323
+ if (parsed.ok) target = parsed.value
324
+ else errors.push(...parsed.errors.map(issue => ({ path: issue.path === '' ? 'target' : `target.${issue.path}`, message: issue.message })))
325
+ }
326
+ let activeRun: SshRunRecord | undefined
327
+ if (record.activeRun !== undefined) {
328
+ const parsed = parseSshRunRecord(record.activeRun)
329
+ if (parsed.ok) activeRun = parsed.value
330
+ else errors.push(...parsed.errors.map(issue => ({ path: issue.path === '' ? 'activeRun' : `activeRun.${issue.path}`, message: issue.message })))
331
+ }
332
+ const history: SshRunRecord[] = []
333
+ if (record.history !== undefined) {
334
+ if (!Array.isArray(record.history)) {
335
+ errors.push({ path: 'history', message: 'must be an array' })
336
+ } else {
337
+ for (const entry of record.history) {
338
+ const parsed = parseSshRunRecord(entry)
339
+ // A dropped entry is not an error of the payload: it is one record the
340
+ // card can no longer account for, and the rest still describes the card.
341
+ if (parsed.ok) history.push(parsed.value)
342
+ }
343
+ }
344
+ }
345
+ if (errors.length > 0) return { ok: false, errors }
346
+ const payload: SshLedgerPayload = { version: SSH_LEDGER_VERSION, updatedAt: record.updatedAt as number, history }
347
+ if (target !== undefined) payload.target = target
348
+ if (activeRun !== undefined) payload.activeRun = activeRun
349
+ const pending = parsePendingInstruction(record.pending)
350
+ if (pending !== undefined) payload.pending = pending
351
+ return { ok: true, value: payload }
352
+ }
353
+
354
+ /**
355
+ * The payload a create dialog reports for a card that does not exist yet: the
356
+ * target the new card pins, and no run account beside it.
357
+ *
358
+ * A card being created has never run, so the document carries an empty history
359
+ * and no active run or queued instruction — exactly what the host half stores
360
+ * when it pins a target on an existing card, minus the runs. The result is read
361
+ * back through {@link parseSshLedgerPayload} before it is returned: a draft this
362
+ * build could store but not read back is never reported at all, so a card
363
+ * created through a create dialog always opens on a payload its own detail seat
364
+ * can account for.
365
+ * @param target - the target the reader picked, already parsed.
366
+ * @param updatedAt - instant the draft is reported (ms epoch).
367
+ * @returns the payload, or undefined when the document does not parse.
368
+ */
369
+ export function composeDraftPayload(target: SshTarget, updatedAt: number): SshLedgerPayload | undefined {
370
+ const draft: SshLedgerPayload = { version: SSH_LEDGER_VERSION, updatedAt, history: [], target }
371
+ return parseSshLedgerPayload(draft).ok ? draft : undefined
372
+ }
@@ -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
+ }