dsh-remote-vs 0.26.0 → 0.28.0
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/README.en.md +112 -28
- package/README.md +84 -25
- package/THIRD-PARTY-NOTICES.md +7 -2
- package/lib/client.js +1237 -254
- package/lib/index.js +821 -17
- package/lib/types/changes/recorder.d.ts +119 -2
- package/lib/types/client/CodeEditor.d.ts +29 -0
- package/lib/types/client/OutlinePanel.d.ts +22 -0
- package/lib/types/client/ProcessesSection.d.ts +19 -0
- package/lib/types/client/api.d.ts +22 -2
- package/lib/types/client/blame-model.d.ts +77 -0
- package/lib/types/client/changes/apply-failure.d.ts +41 -0
- package/lib/types/client/changes/block-confirm.d.ts +97 -0
- package/lib/types/client/changes/diff-outcome.d.ts +56 -0
- package/lib/types/client/changes/freshness.d.ts +51 -0
- package/lib/types/client/changes/model.d.ts +19 -1
- package/lib/types/client/changes/session-diff.d.ts +100 -0
- package/lib/types/client/changes/staleness.d.ts +98 -0
- package/lib/types/client/changes/turn-reverted.d.ts +82 -0
- package/lib/types/client/changes/turn-view.d.ts +131 -0
- package/lib/types/client/changes/undo-affordance.d.ts +44 -10
- package/lib/types/client/changes/undo-target.d.ts +12 -0
- package/lib/types/client/i18n.d.ts +1 -1
- package/lib/types/client/outline-model.d.ts +59 -0
- package/lib/types/client/scm/actions.d.ts +12 -1
- package/lib/types/client/store.d.ts +109 -1
- package/lib/types/client/vsc/blame.d.ts +19 -0
- package/lib/types/client/vsc/change-marks.d.ts +21 -0
- package/lib/types/client/vsc/kit.d.ts +0 -2
- package/lib/types/client/vsc/preview-document.d.ts +46 -0
- package/lib/types/client/vsc/preview-model.d.ts +31 -0
- package/lib/types/client/vsc/preview-services.d.ts +24 -0
- package/lib/types/git-ops.d.ts +18 -0
- package/lib/types/processes.d.ts +43 -0
- package/lib/types/protocol.d.ts +133 -1
- package/package.json +2 -1
- package/src/changes/recorder.ts +851 -8
- package/src/client/CodeEditor.tsx +86 -4
- package/src/client/EditorPane.tsx +581 -41
- package/src/client/MonitorView.tsx +16 -5
- package/src/client/OutlinePanel.tsx +287 -0
- package/src/client/ProcessesSection.tsx +292 -0
- package/src/client/api.ts +38 -3
- package/src/client/blame-model.ts +242 -0
- package/src/client/changes/ChangesDock.tsx +539 -54
- package/src/client/changes/apply-failure.ts +73 -0
- package/src/client/changes/block-confirm.ts +180 -0
- package/src/client/changes/changes.css +45 -0
- package/src/client/changes/diff-outcome.ts +97 -0
- package/src/client/changes/freshness.ts +68 -0
- package/src/client/changes/model.ts +56 -1
- package/src/client/changes/session-diff.ts +187 -0
- package/src/client/changes/staleness.ts +148 -0
- package/src/client/changes/turn-reverted.ts +254 -0
- package/src/client/changes/turn-view.ts +195 -0
- package/src/client/changes/undo-affordance.ts +295 -63
- package/src/client/changes/undo-target.ts +12 -0
- package/src/client/i18n.ts +294 -2
- package/src/client/index.tsx +4 -0
- package/src/client/outline-model.ts +775 -0
- package/src/client/scm/DiffView.tsx +32 -13
- package/src/client/scm/HistoryPanel.tsx +38 -1
- package/src/client/scm/actions.ts +55 -2
- package/src/client/store.tsx +446 -21
- package/src/client/vsc/blame.ts +147 -0
- package/src/client/vsc/change-marks.ts +21 -0
- package/src/client/vsc/kit.tsx +3 -11
- package/src/client/vsc/preview-document.tsx +326 -0
- package/src/client/vsc/preview-model.ts +216 -0
- package/src/client/vsc/preview-services.ts +118 -0
- package/src/client/vsc/vsc.css +965 -39
- package/src/git-ops.ts +34 -0
- package/src/processes.ts +177 -0
- package/src/protocol.ts +138 -1
- package/src/routes.ts +138 -9
package/src/git-ops.ts
CHANGED
|
@@ -327,6 +327,40 @@ export function fileAtRevCommand(workdir: string, rev: string, path: string): st
|
|
|
327
327
|
return capped(`${git(workdir)} show ${shQuote(spec)}`)
|
|
328
328
|
}
|
|
329
329
|
|
|
330
|
+
/**
|
|
331
|
+
* One inclusive line window of `git blame` for a repo-relative file.
|
|
332
|
+
*
|
|
333
|
+
* The window is part of the command (not a post-filter): blame is one of the
|
|
334
|
+
* reads that can be very expensive on a large file, and the editor only ever
|
|
335
|
+
* needs the lines around the viewport. `--line-porcelain` repeats the commit
|
|
336
|
+
* header for every line, so the parser is stateless (see `blame-model.ts`).
|
|
337
|
+
*/
|
|
338
|
+
export function blameCommand(workdir: string, path: string, from: number, to: number): string {
|
|
339
|
+
const start = Number.isFinite(from) ? Math.max(1, Math.floor(from)) : 1
|
|
340
|
+
const end = Number.isFinite(to) ? Math.max(start, Math.floor(to)) : start
|
|
341
|
+
return capped(`${git(workdir)} blame --line-porcelain -L ${start},${end} -- ${shQuote(assertRepoPath(path))}`)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Commits that touched one file, newest first, with rename following.
|
|
346
|
+
*
|
|
347
|
+
* `--follow` works only for a single path; the format matches `parseLog`, so
|
|
348
|
+
* the existing history row type is reused without a second parser.
|
|
349
|
+
*/
|
|
350
|
+
export function fileHistoryCommand(workdir: string, path: string, max = 100): string {
|
|
351
|
+
const count = Math.min(Math.max(Number.isFinite(max) ? Math.floor(max) : 100, 1), 200)
|
|
352
|
+
return capped(
|
|
353
|
+
`${git(workdir)} log --follow --name-status -M --pretty=format:'%x00%H%x1f%P%x1f%an%x1f%ae%x1f%at%x1f%D%x1f%s' -n ${count} -- ${shQuote(assertRepoPath(path))}`,
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** One commit's header fields, even when it is older than the log window. */
|
|
358
|
+
export function commitMetaCommand(workdir: string, ref: string): string {
|
|
359
|
+
return capped(
|
|
360
|
+
`${git(workdir)} log -1 --pretty=format:'%H%x1f%P%x1f%an%x1f%ae%x1f%at%x1f%D%x1f%s%x1e' ${assertRef(ref)} --`,
|
|
361
|
+
)
|
|
362
|
+
}
|
|
363
|
+
|
|
330
364
|
/** Local + remote-tracking branches with their upstream and tip. */
|
|
331
365
|
export function allBranchesCommand(workdir: string): string {
|
|
332
366
|
return capped(`${git(workdir)} for-each-ref --format='%(refname:short)%x1f%(objectname:short)%x1f%(upstream:short)%x1f%(HEAD)%x1f%(refname)' refs/heads refs/remotes`)
|
package/src/processes.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remote process snapshots for the monitor page.
|
|
3
|
+
*
|
|
4
|
+
* Same model as `listen-ports.ts`: one bounded POSIX probe (GNU `ps`, a BSD
|
|
5
|
+
* `ps` fallback, then an explicit `none`), with all parsing done host-side so
|
|
6
|
+
* the browser only receives a bounded table. A process is never killed by a
|
|
7
|
+
* command assembled in the browser: the route validates the PID against the
|
|
8
|
+
* latest snapshot and builds the one signal command here.
|
|
9
|
+
*/
|
|
10
|
+
import type { RemoteProcess, RemoteProcessesResult } from './protocol.ts'
|
|
11
|
+
import { shQuote } from './git-format.ts'
|
|
12
|
+
|
|
13
|
+
/** Marker proving which probe branch actually ran. */
|
|
14
|
+
export const PROCESSES_MARK = '@@RVS-PROCS'
|
|
15
|
+
|
|
16
|
+
/** Budget for the whole probe; a stuck process table must not pin a channel. */
|
|
17
|
+
export const PROCESSES_TIMEOUT_MS = 15_000
|
|
18
|
+
|
|
19
|
+
/** Hard cap on rows per snapshot (a container host can have thousands). */
|
|
20
|
+
export const PROCESSES_MAX = 500
|
|
21
|
+
|
|
22
|
+
/** Longest command string sent to the browser per row. */
|
|
23
|
+
export const PROCESS_COMMAND_MAX = 300
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* One bounded probe.
|
|
27
|
+
*
|
|
28
|
+
* GNU first (`--sort` + `etimes`), then the portable `ps aux` table for BSD /
|
|
29
|
+
* old systems (`ppid` and elapsed time are unknown there). The marker
|
|
30
|
+
* line is printed BEFORE the table so an interrupted probe is distinguishable
|
|
31
|
+
* from a genuinely empty one; the final marker lets the parser reject a
|
|
32
|
+
* truncated run.
|
|
33
|
+
*/
|
|
34
|
+
export function processesCommand(max: number = PROCESSES_MAX): string {
|
|
35
|
+
const cap = Math.max(1, Math.min(PROCESSES_MAX, Math.floor(max)))
|
|
36
|
+
// Cap + 1: the parser needs one extra row to tell "exactly at the cap" from
|
|
37
|
+
// "there were more rows, we cut them". The GNU branch is sorted by ps; the
|
|
38
|
+
// aux fallback is sorted here (ps aux on BSD has no --sort) on the %CPU column.
|
|
39
|
+
// `user:20` avoids procps' default 8-character user truncation (the panel's
|
|
40
|
+
// user filter would otherwise silently miss the rest of the name).
|
|
41
|
+
const gnu = `ps -eo pid=,ppid=,user:20=,pcpu=,pmem=,etimes=,args= --sort=-pcpu 2>/dev/null`
|
|
42
|
+
return [
|
|
43
|
+
// EXPORTED: a plain LC_ALL assignment does not reach `ps` itself.
|
|
44
|
+
'export LC_ALL=C',
|
|
45
|
+
`if ${gnu} >/dev/null 2>&1; then`,
|
|
46
|
+
` echo "${PROCESSES_MARK} gnu"`,
|
|
47
|
+
` ${gnu} | head -n ${cap + 1}`,
|
|
48
|
+
'elif ps aux >/dev/null 2>&1; then',
|
|
49
|
+
` echo "${PROCESSES_MARK} bsd"`,
|
|
50
|
+
` ps aux | tail -n +2 | sort -k3 -rn | head -n ${cap + 1}`,
|
|
51
|
+
'else',
|
|
52
|
+
` echo "${PROCESSES_MARK} none"`,
|
|
53
|
+
'fi',
|
|
54
|
+
`echo "${PROCESSES_MARK} end"`,
|
|
55
|
+
].join('\n')
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A kill signal the route accepts; never an arbitrary string from the client. */
|
|
59
|
+
export type KillSignal = 'TERM' | 'KILL'
|
|
60
|
+
|
|
61
|
+
export function isKillSignal(value: unknown): value is KillSignal {
|
|
62
|
+
return value === 'TERM' || value === 'KILL'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Assert a PID a process snapshot may contain (1 is the remote init). */
|
|
66
|
+
export function assertProcessPid(value: unknown): number {
|
|
67
|
+
const pid = typeof value === 'number' ? value : Number(value)
|
|
68
|
+
if (!Number.isInteger(pid) || pid <= 1 || pid > 2_147_483_647) throw new Error('invalid process id')
|
|
69
|
+
return pid
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Build the one kill command. The PID has already passed the snapshot check;
|
|
74
|
+
* single-quoting it keeps the command shape uniform with every other builder.
|
|
75
|
+
*/
|
|
76
|
+
export function killCommand(pid: number, signal: KillSignal): string {
|
|
77
|
+
return `kill -s ${signal} ${shQuote(String(assertProcessPid(pid)))}`
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function numberOr(value: string): number | null {
|
|
81
|
+
if (!/^[0-9]+(?:\.[0-9]+)?$/.test(value)) return null
|
|
82
|
+
const number = Number(value)
|
|
83
|
+
return Number.isFinite(number) ? number : null
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Bound one argv line before it is serialized into the JSON response. */
|
|
87
|
+
function trimCommand(command: string): string {
|
|
88
|
+
return command.length <= PROCESS_COMMAND_MAX
|
|
89
|
+
? command
|
|
90
|
+
: `${command.slice(0, PROCESS_COMMAND_MAX - 1)}…`
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** GNU row: pid ppid user pcpu pmem etimes args. */
|
|
94
|
+
function parseGnuRow(line: string): RemoteProcess | undefined {
|
|
95
|
+
const match = /^\s*(\d+)\s+(\d+)\s+(\S+)\s+([0-9.]+)\s+([0-9.]+)\s+(\d+)\s+(.*)$/.exec(line)
|
|
96
|
+
if (match === null) return undefined
|
|
97
|
+
const pid = match[1] as string
|
|
98
|
+
const ppid = match[2] as string
|
|
99
|
+
const user = match[3] as string
|
|
100
|
+
const cpu = match[4] as string
|
|
101
|
+
const mem = match[5] as string
|
|
102
|
+
const elapsed = match[6] as string
|
|
103
|
+
const command = match[7] as string
|
|
104
|
+
return {
|
|
105
|
+
pid: Number(pid),
|
|
106
|
+
ppid: Number(ppid),
|
|
107
|
+
user,
|
|
108
|
+
cpuPercent: numberOr(cpu) ?? 0,
|
|
109
|
+
memPercent: numberOr(mem) ?? 0,
|
|
110
|
+
elapsedSeconds: numberOr(elapsed) ?? null,
|
|
111
|
+
command: trimCommand(command.trim()),
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** BSD `ps aux` row: user pid %cpu %mem vsz rss tt stat started time command. */
|
|
116
|
+
function parseBsdRow(line: string): RemoteProcess | undefined {
|
|
117
|
+
const match = /^\s*(\S+)\s+(\d+)\s+([0-9.]+)\s+([0-9.]+)\s+\S+\s+\S+\s+\S+\s+\S+\s+(\S+)\s+\S+\s+(.*)$/.exec(line)
|
|
118
|
+
if (match === null) return undefined
|
|
119
|
+
const user = match[1] as string
|
|
120
|
+
const pid = match[2] as string
|
|
121
|
+
const cpu = match[3] as string
|
|
122
|
+
const mem = match[4] as string
|
|
123
|
+
const command = match[6] as string
|
|
124
|
+
return {
|
|
125
|
+
pid: Number(pid),
|
|
126
|
+
ppid: 0,
|
|
127
|
+
user,
|
|
128
|
+
cpuPercent: numberOr(cpu) ?? 0,
|
|
129
|
+
memPercent: numberOr(mem) ?? 0,
|
|
130
|
+
elapsedSeconds: null,
|
|
131
|
+
command: trimCommand(command.trim()),
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Parse one probe. `exitCode === null` (channel died) or a missing end marker
|
|
137
|
+
* means `unknown`, never an empty process table.
|
|
138
|
+
*/
|
|
139
|
+
export function parseProcesses(stdout: string, exitCode: number | null): RemoteProcessesResult {
|
|
140
|
+
const lines = stdout.split('\n').map(line => line.endsWith('\r') ? line.slice(0, -1) : line)
|
|
141
|
+
const markerAt = lines.findIndex(line => line.trim() === `${PROCESSES_MARK} gnu` || line.trim() === `${PROCESSES_MARK} bsd` || line.trim() === `${PROCESSES_MARK} none`)
|
|
142
|
+
if (exitCode === null || markerAt < 0) {
|
|
143
|
+
return { tool: 'unknown', processes: [], truncated: false, error: 'the remote process probe ended without a complete answer' }
|
|
144
|
+
}
|
|
145
|
+
const marker = lines[markerAt]?.trim() ?? ''
|
|
146
|
+
// The real terminator is the LAST marker line: a process argv containing a
|
|
147
|
+
// literal `@@RVS-PROCS end` must not truncate the table early.
|
|
148
|
+
let endAt = -1
|
|
149
|
+
for (let index = lines.length - 1; index > markerAt; index -= 1) {
|
|
150
|
+
if ((lines[index] as string).trim() === `${PROCESSES_MARK} end`) {
|
|
151
|
+
endAt = index
|
|
152
|
+
break
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (endAt < 0) {
|
|
156
|
+
return { tool: 'unknown', processes: [], truncated: false, error: 'the remote process probe was interrupted' }
|
|
157
|
+
}
|
|
158
|
+
if (marker.endsWith('none')) return { tool: 'none', processes: [], truncated: false }
|
|
159
|
+
const gnu = marker.endsWith('gnu')
|
|
160
|
+
const parsedProcesses: RemoteProcess[] = []
|
|
161
|
+
for (const line of lines.slice(markerAt + 1, endAt)) {
|
|
162
|
+
if (line.trim() === '') continue
|
|
163
|
+
const row = gnu ? parseGnuRow(line) : parseBsdRow(line)
|
|
164
|
+
// The probe's own shell/head/ps wrappers contain the marker in their argv;
|
|
165
|
+
// they are not user processes and must not be offered for a kill.
|
|
166
|
+
if (row !== undefined && !row.command.includes(PROCESSES_MARK)) parsedProcesses.push(row)
|
|
167
|
+
}
|
|
168
|
+
if (parsedProcesses.length === 0) {
|
|
169
|
+
return { tool: 'unknown', processes: [], truncated: false, error: 'the remote ps output could not be parsed' }
|
|
170
|
+
}
|
|
171
|
+
const truncated = parsedProcesses.length > PROCESSES_MAX
|
|
172
|
+
return {
|
|
173
|
+
tool: gnu ? 'gnu' : 'bsd',
|
|
174
|
+
processes: parsedProcesses.slice(0, PROCESSES_MAX),
|
|
175
|
+
truncated,
|
|
176
|
+
}
|
|
177
|
+
}
|
package/src/protocol.ts
CHANGED
|
@@ -42,6 +42,8 @@ export const REMOTE_API = {
|
|
|
42
42
|
listenPorts: '/api/dsh-remote/listen-ports',
|
|
43
43
|
/** Live system metrics for the monitor panel (one bounded probe per host). */
|
|
44
44
|
metrics: '/api/dsh-remote/metrics',
|
|
45
|
+
/** One bounded process table for the monitor page, plus TERM/KILL actions. */
|
|
46
|
+
processes: '/api/dsh-remote/processes',
|
|
45
47
|
} as const
|
|
46
48
|
|
|
47
49
|
/**
|
|
@@ -530,6 +532,84 @@ export interface RemoteChangedFile {
|
|
|
530
532
|
revert?: 'patch' | 'delete'
|
|
531
533
|
}
|
|
532
534
|
|
|
535
|
+
/**
|
|
536
|
+
* The most recent COMPLETED turn, as the review panel switches to it.
|
|
537
|
+
*
|
|
538
|
+
* "This turn" is answered from the ledger's own turn boundaries: the host keeps
|
|
539
|
+
* every listed path's text at each turn close and compares the next close
|
|
540
|
+
* against it. Deliberately ONE turn deep — a per-turn history would need the
|
|
541
|
+
* full text of every file at every boundary, and the panel's job is "what did
|
|
542
|
+
* the turn I just ran change", not a time machine.
|
|
543
|
+
*/
|
|
544
|
+
export interface RemoteChangesTurnView {
|
|
545
|
+
/** Turn number the delta describes (0 = no turn has closed yet). */
|
|
546
|
+
turn: number
|
|
547
|
+
/** Files this turn changed, with THIS TURN's counts (not the session total). */
|
|
548
|
+
files: RemoteChangedFile[]
|
|
549
|
+
added: number
|
|
550
|
+
deleted: number
|
|
551
|
+
/** Wall-clock ms when the delta was computed. */
|
|
552
|
+
at: number
|
|
553
|
+
/**
|
|
554
|
+
* Whether the per-file hunks behind `turnDiff` are cached.
|
|
555
|
+
*
|
|
556
|
+
* `false` = the turn's contents were too large for the memory budget (or the
|
|
557
|
+
* file cap made attribution unreliable): the counts above are still real, but
|
|
558
|
+
* clicking a row cannot paint hunks or revert. Honest degradation — the
|
|
559
|
+
* alternative would be comparing the next turn against a half-updated
|
|
560
|
+
* snapshot and attributing old changes to it.
|
|
561
|
+
*/
|
|
562
|
+
cached: boolean
|
|
563
|
+
/** Why `cached` is false (shown to the user, never swallowed). */
|
|
564
|
+
reason?: string
|
|
565
|
+
/**
|
|
566
|
+
* This view is INCOMPLETE — and it is not "a prefix of a longer list".
|
|
567
|
+
*
|
|
568
|
+
* Three ways rows go missing:
|
|
569
|
+
* - the file cap cut the list (`files` is the first N of the scanned paths);
|
|
570
|
+
* - the previous turn's snapshot was cut by that cap, or a previous side was
|
|
571
|
+
* withheld (binary / over the read cap), so a path that cannot be compared is
|
|
572
|
+
* DROPPED instead of being guessed as a whole-file creation;
|
|
573
|
+
* - the previous turn's slow scan never landed before this turn closed, so a
|
|
574
|
+
* file missing from that snapshot cannot be proven new either.
|
|
575
|
+
*
|
|
576
|
+
* A partial view must say so: "these rows are what this turn provably changed",
|
|
577
|
+
* not "this is everything the turn changed".
|
|
578
|
+
*/
|
|
579
|
+
truncated?: true
|
|
580
|
+
/**
|
|
581
|
+
* The turn's slow scan (`git ls-files --others`) has not landed yet.
|
|
582
|
+
*
|
|
583
|
+
* The same turn is REBUILT when it does (`correctTurn`), so this list may still
|
|
584
|
+
* grow — it is never relabelled as the next turn. When a newer turn closes
|
|
585
|
+
* first, the view stops waiting and `truncated` records that it never caught
|
|
586
|
+
* up.
|
|
587
|
+
*/
|
|
588
|
+
pending?: true
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* Body of `op: 'list'` on the changes route (the request half of
|
|
593
|
+
* {@link RemoteChangesSummary}).
|
|
594
|
+
*
|
|
595
|
+
* The two refresh intents are separated because they cost very different things
|
|
596
|
+
* on the remote:
|
|
597
|
+
* - {@link RemoteChangesListRequest.force} (`⟳`) — tracked scan NOW and re-arm the
|
|
598
|
+
* slow `git ls-files --others` walk (clears its write-off). Deliberate click.
|
|
599
|
+
* - {@link RemoteChangesListRequest.trackedOnly} — "the user came back to the
|
|
600
|
+
* page": tracked files now, but the slow walk keeps its cadence and its
|
|
601
|
+
* written-off verdict. Focus events repeat every few seconds; re-arming an 11s
|
|
602
|
+
* (cold: minutes) walk on each one would multiply the remote load.
|
|
603
|
+
*/
|
|
604
|
+
export interface RemoteChangesListRequest {
|
|
605
|
+
op: 'list'
|
|
606
|
+
sessionId: string
|
|
607
|
+
force?: true
|
|
608
|
+
trackedOnly?: true
|
|
609
|
+
/** Repository directory from the Source Control panel ('' = the session root). */
|
|
610
|
+
repoSubdir?: string
|
|
611
|
+
}
|
|
612
|
+
|
|
533
613
|
/**
|
|
534
614
|
* What one session changed on its remote host since the session's baseline.
|
|
535
615
|
*
|
|
@@ -571,11 +651,31 @@ export interface RemoteChangesSummary {
|
|
|
571
651
|
pending: boolean
|
|
572
652
|
/** Why this session has no ledger (local session, no git, timeout, …). */
|
|
573
653
|
unavailable?: string
|
|
654
|
+
/**
|
|
655
|
+
* The last completed turn's delta, when the host computed one.
|
|
656
|
+
*
|
|
657
|
+
* Optional on purpose: an older host (or a session with no closed turn yet)
|
|
658
|
+
* omits it and the panel stays on the session-cumulative view it has always
|
|
659
|
+
* shown. A client that does not know the field ignores it.
|
|
660
|
+
*/
|
|
661
|
+
turnView?: RemoteChangesTurnView
|
|
574
662
|
}
|
|
575
663
|
|
|
576
664
|
/** One file's whole-session comparison, ready for editor decorations. */
|
|
577
665
|
export interface RemoteChangesDiff {
|
|
578
|
-
|
|
666
|
+
/**
|
|
667
|
+
* `unavailable` / `stale` belong to the per-turn comparison only.
|
|
668
|
+
*
|
|
669
|
+
* - `unavailable`: the host has no cached per-turn hunks for this session
|
|
670
|
+
* (memory budget, file cap, or no completed turn) — `reason` says which;
|
|
671
|
+
* - `stale`: the file changed after the turn closed, so the cached comparison
|
|
672
|
+
* no longer describes what is on disk; the client falls back to the
|
|
673
|
+
* session-cumulative view instead of painting wrong lines.
|
|
674
|
+
*
|
|
675
|
+
* The whole-session `diff` never returns them, so a client that switches on
|
|
676
|
+
* the old three kinds keeps working unchanged.
|
|
677
|
+
*/
|
|
678
|
+
kind: 'text' | 'binary' | 'missing' | 'unavailable' | 'stale'
|
|
579
679
|
/** Remote absolute POSIX path. */
|
|
580
680
|
path: string
|
|
581
681
|
display: string
|
|
@@ -594,6 +694,8 @@ export interface RemoteChangesDiff {
|
|
|
594
694
|
status?: 'A' | 'M' | 'D' | 'R' | 'C' | 'T' | 'U'
|
|
595
695
|
/** The diff was cut at the byte cap: `hunks` is a prefix. */
|
|
596
696
|
truncated?: true
|
|
697
|
+
/** For `unavailable` / `stale`: what happened, in words the panel can show. */
|
|
698
|
+
reason?: string
|
|
597
699
|
}
|
|
598
700
|
|
|
599
701
|
/** One row of the side-by-side diff projection. */
|
|
@@ -716,6 +818,41 @@ export interface RemoteListenPortsResult {
|
|
|
716
818
|
error?: string
|
|
717
819
|
}
|
|
718
820
|
|
|
821
|
+
/** One row of the remote process table. */
|
|
822
|
+
export interface RemoteProcess {
|
|
823
|
+
pid: number
|
|
824
|
+
/** Parent PID; 0 on a BSD `ps aux` table that does not print it. */
|
|
825
|
+
ppid: number
|
|
826
|
+
user: string
|
|
827
|
+
cpuPercent: number
|
|
828
|
+
memPercent: number
|
|
829
|
+
/** Elapsed seconds when the probe can provide them; null on BSD `ps aux`. */
|
|
830
|
+
elapsedSeconds: number | null
|
|
831
|
+
command: string
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* The remote process probe's answer.
|
|
836
|
+
*
|
|
837
|
+
* `tool: 'none' | 'unknown'` means "cannot enumerate", never "no processes";
|
|
838
|
+
* the monitor page must explain that instead of rendering an empty table.
|
|
839
|
+
*/
|
|
840
|
+
export interface RemoteProcessesResult {
|
|
841
|
+
tool: 'gnu' | 'bsd' | 'none' | 'unknown'
|
|
842
|
+
processes: RemoteProcess[]
|
|
843
|
+
truncated: boolean
|
|
844
|
+
error?: string
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
/** Result of one TERM/KILL request (the route validates the PID first). */
|
|
848
|
+
export interface RemoteProcessKillResult {
|
|
849
|
+
ok: boolean
|
|
850
|
+
pid: number
|
|
851
|
+
signal: 'TERM' | 'KILL'
|
|
852
|
+
/** Remote error text when the signal was refused. */
|
|
853
|
+
error?: string
|
|
854
|
+
}
|
|
855
|
+
|
|
719
856
|
/**
|
|
720
857
|
* How one host metrics sample ended.
|
|
721
858
|
*
|
package/src/routes.ts
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
selectionDownloadName,
|
|
19
19
|
type ExecStreamEvent,
|
|
20
20
|
type ImportReport,
|
|
21
|
+
type RemoteProcessesResult,
|
|
21
22
|
type RemoteHostMetrics,
|
|
22
23
|
type RemoteTimeouts,
|
|
23
24
|
} from './protocol.ts'
|
|
@@ -33,6 +34,7 @@ import { TERMINAL_MAX_FRAME_BYTES, TerminalSessionRegistry } from './terminal.ts
|
|
|
33
34
|
import { ForwardManager, validateForwardRequest } from './forward.ts'
|
|
34
35
|
import { LISTEN_PORTS_TIMEOUT_MS, listenPortsCommand, parseListenPorts } from './listen-ports.ts'
|
|
35
36
|
import { HostMonitor } from './monitor.ts'
|
|
37
|
+
import { PROCESSES_TIMEOUT_MS, assertProcessPid, isKillSignal, killCommand, parseProcesses, processesCommand } from './processes.ts'
|
|
36
38
|
import type { SessionChanges } from './changes/recorder.ts'
|
|
37
39
|
import { DEFAULT_TIMEOUTS, clampExecTimeout } from './timeouts.ts'
|
|
38
40
|
import { RemoteViewSettingsStore } from './view-settings.ts'
|
|
@@ -299,6 +301,49 @@ export function makeRoutes(deps: RemoteRoutesDeps): {
|
|
|
299
301
|
// costs one extra sample, so it is safe to construct here instead of owning a
|
|
300
302
|
// fiber-scoped effect like the forward manager.
|
|
301
303
|
const monitor = new HostMonitor(pool)
|
|
304
|
+
/**
|
|
305
|
+
* Last process snapshot per alias. The kill route validates against this
|
|
306
|
+
* table, so a browser cannot construct a `kill` for an arbitrary PID; a stale
|
|
307
|
+
* entry is refreshed before validation rather than trusted indefinitely.
|
|
308
|
+
*/
|
|
309
|
+
const processSnapshots = new Map<string, { at: number; result: RemoteProcessesResult }>()
|
|
310
|
+
/** One probe per alias at a time; a second reader joins the in-flight one. */
|
|
311
|
+
const processInflight = new Map<string, Promise<RemoteProcessesResult>>()
|
|
312
|
+
const forgetProcesses = (alias: string): void => {
|
|
313
|
+
processSnapshots.delete(alias)
|
|
314
|
+
processInflight.delete(alias)
|
|
315
|
+
}
|
|
316
|
+
const readProcesses = async (alias: string, maxAgeMs: number): Promise<RemoteProcessesResult> => {
|
|
317
|
+
const cached = processSnapshots.get(alias)
|
|
318
|
+
if (maxAgeMs > 0 && cached !== undefined && Date.now() - cached.at < maxAgeMs) return cached.result
|
|
319
|
+
const joined = processInflight.get(alias)
|
|
320
|
+
if (joined !== undefined) return joined
|
|
321
|
+
const task = (async (): Promise<RemoteProcessesResult> => {
|
|
322
|
+
const result = await pool.exec(alias, processesCommand(), { timeoutMs: PROCESSES_TIMEOUT_MS })
|
|
323
|
+
let parsed: RemoteProcessesResult
|
|
324
|
+
if (result.error !== undefined) {
|
|
325
|
+
parsed = { tool: 'unknown', processes: [], truncated: false, error: result.error }
|
|
326
|
+
} else if (result.timedOut) {
|
|
327
|
+
parsed = { tool: 'unknown', processes: [], truncated: false, error: `the process probe did not finish within ${PROCESSES_TIMEOUT_MS / 1000}s` }
|
|
328
|
+
} else if (result.exitCode === null) {
|
|
329
|
+
parsed = { tool: 'unknown', processes: [], truncated: false, error: 'the process probe ended without an exit status' }
|
|
330
|
+
} else {
|
|
331
|
+
parsed = parseProcesses(result.stdout, result.exitCode)
|
|
332
|
+
}
|
|
333
|
+
if (parsed.tool === 'gnu' || parsed.tool === 'bsd') {
|
|
334
|
+
processSnapshots.set(alias, { at: Date.now(), result: parsed })
|
|
335
|
+
} else {
|
|
336
|
+
processSnapshots.delete(alias)
|
|
337
|
+
}
|
|
338
|
+
return parsed
|
|
339
|
+
})()
|
|
340
|
+
processInflight.set(alias, task)
|
|
341
|
+
void task.then(
|
|
342
|
+
() => { if (processInflight.get(alias) === task) processInflight.delete(alias) },
|
|
343
|
+
() => { if (processInflight.get(alias) === task) processInflight.delete(alias) },
|
|
344
|
+
)
|
|
345
|
+
return task
|
|
346
|
+
}
|
|
302
347
|
const forwards = deps.forwards
|
|
303
348
|
const viewSettings = deps.viewSettings ?? new RemoteViewSettingsStore()
|
|
304
349
|
|
|
@@ -509,6 +554,7 @@ export function makeRoutes(deps: RemoteRoutesDeps): {
|
|
|
509
554
|
forwards?.stopAlias(alias)
|
|
510
555
|
pool.forget(alias)
|
|
511
556
|
monitor.forget(alias)
|
|
557
|
+
forgetProcesses(alias)
|
|
512
558
|
void worlds.dropHost(alias).catch(() => undefined)
|
|
513
559
|
if (!store.remove(alias)) {
|
|
514
560
|
writeJson(res, 404, { error: `unknown host alias: ${alias}` })
|
|
@@ -528,6 +574,7 @@ export function makeRoutes(deps: RemoteRoutesDeps): {
|
|
|
528
574
|
try {
|
|
529
575
|
pool.forget(alias)
|
|
530
576
|
monitor.forget(alias)
|
|
577
|
+
forgetProcesses(alias)
|
|
531
578
|
void worlds.dropHost(alias).catch(() => undefined)
|
|
532
579
|
const entry = store.update(alias, body as Partial<HostPayload>)
|
|
533
580
|
writeJson(res, 200, { host: store.summarize(entry) })
|
|
@@ -1241,7 +1288,7 @@ export function makeRoutes(deps: RemoteRoutesDeps): {
|
|
|
1241
1288
|
// ------------------------------------------- session change ledger
|
|
1242
1289
|
// What one live session changed on its remote host. The ledger itself is
|
|
1243
1290
|
// fed by turn events (see index.ts); the browser only ever reads it, which
|
|
1244
|
-
// is why
|
|
1291
|
+
// is why all three ops are side-effect free and answer null instead of 404 when
|
|
1245
1292
|
// the session has no ledger yet (the panel shows its pending/absent state).
|
|
1246
1293
|
{
|
|
1247
1294
|
kind: 'exact',
|
|
@@ -1260,8 +1307,8 @@ export function makeRoutes(deps: RemoteRoutesDeps): {
|
|
|
1260
1307
|
const body = read.body
|
|
1261
1308
|
const sessionId = typeof body?.sessionId === 'string' ? body.sessionId : ''
|
|
1262
1309
|
const op = typeof body?.op === 'string' ? body.op : ''
|
|
1263
|
-
if (sessionId === '' || (op !== 'list' && op !== 'diff')) {
|
|
1264
|
-
writeJson(res, 400, { error: 'op (list|diff) and a non-empty sessionId are required' })
|
|
1310
|
+
if (sessionId === '' || (op !== 'list' && op !== 'diff' && op !== 'turnDiff')) {
|
|
1311
|
+
writeJson(res, 400, { error: 'op (list|diff|turnDiff) and a non-empty sessionId are required' })
|
|
1265
1312
|
return
|
|
1266
1313
|
}
|
|
1267
1314
|
const ledger = deps.changes
|
|
@@ -1284,22 +1331,33 @@ export function makeRoutes(deps: RemoteRoutesDeps): {
|
|
|
1284
1331
|
const cwd = liveSessionCwd(ctx, sessionId)
|
|
1285
1332
|
if (cwd !== undefined) ledger.beginTurn({ sessionId, cwd })
|
|
1286
1333
|
}
|
|
1287
|
-
//
|
|
1288
|
-
//
|
|
1289
|
-
//
|
|
1290
|
-
//
|
|
1334
|
+
// Three refresh intents, deliberately different in COST:
|
|
1335
|
+
// - `force` = the panel's ⟳ button: tracked scan now AND re-arm the slow
|
|
1336
|
+
// new-file walk (clears its write-off). A human click can afford it.
|
|
1337
|
+
// - `trackedOnly` = the panel's "back to the page" refresh: tracked files
|
|
1338
|
+
// now, but the slow walk keeps its cadence and its written-off verdict.
|
|
1339
|
+
// A window focus can happen every few seconds, and re-arming an 11s
|
|
1340
|
+
// (cold: minutes) walk on each one would multiply remote load.
|
|
1341
|
+
// - neither = the ordinary poll, which self-heals once data is stale.
|
|
1291
1342
|
if (body?.force === true) ledger.rescan(sessionId)
|
|
1343
|
+
else if (body?.trackedOnly === true) ledger.touch(sessionId, 0)
|
|
1292
1344
|
else ledger.touch(sessionId)
|
|
1293
1345
|
writeJson(res, 200, { summary: ledger.summary(sessionId) })
|
|
1294
1346
|
return
|
|
1295
1347
|
}
|
|
1296
1348
|
const path = typeof body?.path === 'string' ? body.path : ''
|
|
1297
1349
|
if (path === '') {
|
|
1298
|
-
writeJson(res, 400, { error:
|
|
1350
|
+
writeJson(res, 400, { error: `path is required for op=${op}` })
|
|
1299
1351
|
return
|
|
1300
1352
|
}
|
|
1301
1353
|
try {
|
|
1302
|
-
|
|
1354
|
+
// `turnDiff` answers the same shape from the ledger's last completed
|
|
1355
|
+
// turn (cached hunks, plus a `stale` verdict when the file moved on).
|
|
1356
|
+
writeJson(res, 200, {
|
|
1357
|
+
diff: op === 'turnDiff'
|
|
1358
|
+
? await ledger.turnDiff(sessionId, path)
|
|
1359
|
+
: await ledger.diff(sessionId, path),
|
|
1360
|
+
})
|
|
1303
1361
|
} catch (error) {
|
|
1304
1362
|
// Reading the diff is a remote (git) round trip: it failed upstream.
|
|
1305
1363
|
writeRouteError(res, error)
|
|
@@ -1632,6 +1690,77 @@ export function makeRoutes(deps: RemoteRoutesDeps): {
|
|
|
1632
1690
|
}
|
|
1633
1691
|
},
|
|
1634
1692
|
},
|
|
1693
|
+
// -------------------------------------------------- remote processes
|
|
1694
|
+
{
|
|
1695
|
+
kind: 'exact',
|
|
1696
|
+
path: REMOTE_API.processes,
|
|
1697
|
+
handler: async (req, res) => {
|
|
1698
|
+
if (!isLoopbackRequest(req)) {
|
|
1699
|
+
writeJson(res, 403, { error: 'forbidden: loopback-only' })
|
|
1700
|
+
return
|
|
1701
|
+
}
|
|
1702
|
+
const method = req.method ?? 'GET'
|
|
1703
|
+
if (method !== 'GET' && method !== 'POST') {
|
|
1704
|
+
writeJson(res, 405, { error: `method not allowed: ${method}` })
|
|
1705
|
+
return
|
|
1706
|
+
}
|
|
1707
|
+
try {
|
|
1708
|
+
if (method === 'GET') {
|
|
1709
|
+
const url = new URL(req.url ?? '/', 'http://localhost')
|
|
1710
|
+
const alias = queryParam(url, 'alias')
|
|
1711
|
+
if (alias === undefined || alias === '') {
|
|
1712
|
+
writeJson(res, 400, { error: 'alias query parameter is required' })
|
|
1713
|
+
return
|
|
1714
|
+
}
|
|
1715
|
+
if (store.get(alias) === undefined) {
|
|
1716
|
+
writeJson(res, 404, { error: `unknown host alias: ${alias}` })
|
|
1717
|
+
return
|
|
1718
|
+
}
|
|
1719
|
+
writeJson(res, 200, await readProcesses(alias, queryParam(url, 'refresh') === '1' ? 0 : 3_000))
|
|
1720
|
+
return
|
|
1721
|
+
}
|
|
1722
|
+
const read = await readBodyOrError<{ alias?: unknown; pid?: unknown; signal?: unknown }>(req, res, { objectOnly: true })
|
|
1723
|
+
if (!read.ok) return
|
|
1724
|
+
const body = read.body ?? {}
|
|
1725
|
+
const alias = typeof body.alias === 'string' ? body.alias : ''
|
|
1726
|
+
if (alias === '') {
|
|
1727
|
+
writeJson(res, 400, { error: 'alias is required' })
|
|
1728
|
+
return
|
|
1729
|
+
}
|
|
1730
|
+
let pid: number
|
|
1731
|
+
try {
|
|
1732
|
+
pid = assertProcessPid(body.pid)
|
|
1733
|
+
} catch (error) {
|
|
1734
|
+
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) })
|
|
1735
|
+
return
|
|
1736
|
+
}
|
|
1737
|
+
if (!isKillSignal(body.signal)) {
|
|
1738
|
+
writeJson(res, 400, { error: 'signal must be TERM or KILL' })
|
|
1739
|
+
return
|
|
1740
|
+
}
|
|
1741
|
+
// The snapshot is the authorization list: a PID that is not in the
|
|
1742
|
+
// current remote table cannot be signalled, even if it is numeric.
|
|
1743
|
+
const snapshot = await readProcesses(alias, 0)
|
|
1744
|
+
const target = snapshot.processes.find(process => process.pid === pid)
|
|
1745
|
+
if (target === undefined) {
|
|
1746
|
+
writeJson(res, 400, { error: `process ${pid} is not in the current remote snapshot` })
|
|
1747
|
+
return
|
|
1748
|
+
}
|
|
1749
|
+
const result = await pool.exec(alias, killCommand(pid, body.signal), { timeoutMs: 10_000 })
|
|
1750
|
+
if (result.error !== undefined || result.exitCode !== 0) {
|
|
1751
|
+
const message = result.error ?? result.stderr.trim() ?? ''
|
|
1752
|
+
writeJson(res, 502, { error: message === '' ? `kill failed [exit code: ${result.exitCode ?? '?'}]` : message })
|
|
1753
|
+
return
|
|
1754
|
+
}
|
|
1755
|
+
// The next GET must not serve the pre-kill snapshot.
|
|
1756
|
+
processSnapshots.delete(alias)
|
|
1757
|
+
writeJson(res, 200, { ok: true, pid, signal: body.signal })
|
|
1758
|
+
} catch (error) {
|
|
1759
|
+
if (isStoreError(error)) writeStoreError(res, error)
|
|
1760
|
+
else writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) })
|
|
1761
|
+
}
|
|
1762
|
+
},
|
|
1763
|
+
},
|
|
1635
1764
|
// ------------------------------------------------------- host metrics
|
|
1636
1765
|
// Live CPU/RAM/disk/net/temperature for the monitor panel. One bounded
|
|
1637
1766
|
// POSIX probe per host (no helper, no Python on the remote); `HostMonitor`
|