oxdock-ssh-plugin 0.47.0-alpha

Ephemeral user-space SSH server and client for OxDock scripts, bridged to stdio pipelines through the Engine facade.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# oxdock-ssh-plugin

Ephemeral user-space SSH server and client for OxDock scripts, bridged to stdio pipelines through the Engine facade.

> Part of the [OxDock]https://github.com/jzombie/rust-oxdock workspace.

## Overview

The SSH module spins up ephemeral loopback SSH servers with in-memory
credentials, dequeues each session with its metadata, and pumps bytes
through explicit DSL pipes. Import it with `IMPORT [STD, SSH]` in a
runner that registers the host module on the
[Engine facade](https://github.com/jzombie/rust-oxdock/blob/main/crates/oxdock-core/src/exec/engine.rs). The function
reference below lists every entry with signatures; the type reference
lists the session and server handles.

A minimal proxy accepts one session, relays its identity into the
child environment, and bridges the wire through a PTY child until
both directions drain:

```oxdock
IMPORT [STD, SSH]
INHERIT_ENV [PATH]

# Serve the proxy on a runner-mapped service.
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-demo", {username: "u", password: "p"})

# Pick the echo child per platform. Each child reads one stdin line
# and exits by itself: console children never observe stdin EOF.
LET $child: LIST<STRING> = ["cat"]
[family:windows] {
    $child = ["cmd", "/v:on", "/c", "(set /p line= && echo got:!line!)"]
}

# Accept one session in the background and bridge the wire through the PTY child.
LET $srv: HANDLE<VOID> = ASYNC {
    LET $sess: SSH_DEQUEUE_INFO = SSH_DEQUEUE($m.server)

    ENV SSH_USER="{{ $sess.username }}"
    ENV SSH_CLIENT="{{ $sess.addr }}"
    ENV SSH_SERVER="{{ $m.addr }}"

    LET $c_in: PIPE
    LET $c_out: PIPE

    LET $pty: HANDLE<INT> = ASYNC { RETURN SSH_PTY_RUN($sess.session, $child, 0, 0, $c_out, $c_in) }
    LET $pump: HANDLE<VOID> = ASYNC { SSH_PUMP_CHANNEL($sess.session, $c_in, $c_out) }

    LET $code: INT = AWAIT $pty
    ASSERT_EQ $code 0

    AWAIT $pump
}

# Dial the proxy and prove the child echoed the bytes back. The
# pump expands the newline for console line input on Windows, so one
# portable line works everywhere; `cat` echoes it back harmlessly on
# Unix.
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE<MAP<closed: BOOL, exit_status: INT>> = ASYNC { RETURN SSH_CONNECT("doc-ssh-demo", "u", "p", $cin, $cout) }

WITH_IO [stdout=$cin] ECHO "proxy-ping"

LET $cDone: MAP<closed: BOOL, exit_status: INT> = AWAIT $c
ASSERT_EQ $cDone.closed true
ASSERT_EQ $cDone.exit_status 0
ASSERT_CONTAINS $cout "proxy-ping"

AWAIT $srv
SSH_CLOSE($m.server)
```

<!-- GENERATED by oxdock-render from SSH function metadata. Do not edit by hand. -->
## Functions

Callable as `MODULE::NAME(...)` in expressions (or bare `NAME(...)` with the module imported via `IMPORT`). Introspectable from scripts with `FUNCTIONS()` and `DESCRIBE(name)`.

### SSH_ACCEPT

Accept one SSH session into pipes.

`SSH_DEQUEUE` + `SSH_PUMP_CHANNEL` in one call, for worker loops
that need no pre-pump inspection. Must run inside `ASYNC`.

The server sends first: the client side never EOFs its input, so
the reply cannot race teardown.


**Signature:** `SSH_ACCEPT($server: SSH_SERVER, $in_pipe: PIPE, $out_pipe: PIPE): MAP<closed: BOOL, command: STRING>`

**Contexts:** [`AST`](#context-ast) only

**Parameters:**

- `$server` (`SSH_SERVER`): Server handle from `SSH_SERVE`.
- `$in_pipe` (`PIPE`): Pipe carrying bytes consumed by the wire side.
- `$out_pipe` (`PIPE`): Pipe carrying bytes produced by the wire side.

**Returns:** `MAP<closed: BOOL, command: STRING>`

  - `closed` (`BOOL`): True when the session closed cleanly.
  - `command` (`STRING`): Executed command, empty for shells.

**Examples:**

**Example: accept and close**

```oxdock
IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-accept", {username: "u", password: "p"})
LET $in: PIPE
LET $out: PIPE
LET $acc: HANDLE<MAP<ANY>> = ASYNC { RETURN SSH_ACCEPT($m.server, $in, $out) }
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE<MAP<closed: BOOL, exit_status: INT>> = ASYNC { RETURN SSH_CONNECT("doc-ssh-accept", "u", "p", $cin, $cout) }

# Close both inputs: each pump observes EOF, half-closes, and both
# tasks return their result maps with no cancellation.
WITH_IO [stdout=$cin] ECHO "client-hello"
WITH_IO [stdout=$in] ECHO "server-greeting"
LET $done: MAP<ANY> = AWAIT $acc
ASSERT_EQ $done.closed true
ASSERT_CONTAINS $done "command"
LET $cDone: MAP<ANY> = AWAIT $c
ASSERT_EQ $cDone.closed true
SSH_CLOSE($m.server)
```

### SSH_CLOSE

Shut down an SSH server.

Shut a server down and join its runtime thread (bounded).
Idempotent.


**Signature:** `SSH_CLOSE($server: SSH_SERVER): BOOL`

**Contexts:** [`AST`](#context-ast) only

**Parameters:**

- `$server` (`SSH_SERVER`): Server handle from `SSH_SERVE`.

**Returns:** `BOOL`

**Examples:**

**Example: shut down**

```oxdock
IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-close", {username: "u", password: "p"})
LET $ok: BOOL = SSH_CLOSE($m.server)
ASSERT_EQ $ok true
```

### SSH_CONNECT

Open an SSH client session into pipes.

Open an SSH client session and pump its shell channel through
explicit pipes until it closes. Must run inside `ASYNC`, beside
the pump tasks. Under `--offline` the dial bails before any
socket work. The result MAP carries `exit_status`, or -1 when the
server sent none.

**Signature:** `SSH_CONNECT($target: STRING, $username: STRING, $password: STRING, $in_pipe: PIPE, $out_pipe: PIPE): MAP<closed: BOOL, exit_status: INT>`

**Contexts:** [`AST`](#context-ast) only

**Parameters:**

- `$target` (`STRING`): Dial target: a service name resolved through runner mappings.
- `$username` (`STRING`): Inner credentials for the SSH session.
- `$password` (`STRING`): Inner credentials for the SSH session.
- `$in_pipe` (`PIPE`): Pipe carrying bytes consumed by the wire side.
- `$out_pipe` (`PIPE`): Pipe carrying bytes produced by the wire side.

**Returns:** `MAP<closed: BOOL, exit_status: INT>`

  - `closed` (`BOOL`): True when the session closed cleanly.
  - `exit_status` (`INT`): Remote exit status, or -1 when none arrived.

**Examples:**

**Example: connect and close**

```oxdock
IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-connect", {username: "u", password: "p"})
LET $in: PIPE
LET $out: PIPE
LET $acc: HANDLE<MAP<ANY>> = ASYNC { RETURN SSH_ACCEPT($m.server, $in, $out) }
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE<MAP<closed: BOOL, exit_status: INT>> = ASYNC { RETURN SSH_CONNECT("doc-ssh-connect", "u", "p", $cin, $cout) }

# Close both inputs: each pump observes EOF, half-closes, and both
# tasks return their result maps with no cancellation.
WITH_IO [stdout=$cin] ECHO "client-hello"
WITH_IO [stdout=$in] ECHO "server-greeting"
LET $cDone: MAP<closed: BOOL, exit_status: INT> = AWAIT $c
ASSERT_EQ $cDone.closed true
LET $adone: MAP<ANY> = AWAIT $acc
ASSERT_EQ $adone.closed true
SSH_CLOSE($m.server)
```

### SSH_DEQUEUE

Dequeue one SSH session with its metadata.

Dequeue one authenticated session for routing before any byte
pumping starts. Must run inside `ASYNC`.

The server sends first: the client side never EOFs its input, so a
synthetic reply cannot race teardown.


**Signature:** `SSH_DEQUEUE($server: SSH_SERVER): MAP<session: SSH_SESSION, command: STRING, username: STRING, addr: STRING>`

**Contexts:** [`AST`](#context-ast) only

**Parameters:**

- `$server` (`SSH_SERVER`): Server handle from `SSH_SERVE`.

**Returns:** `MAP<session: SSH_SESSION, command: STRING, username: STRING, addr: STRING>`

  - `session` (`SSH_SESSION`): Session handle for `SSH_PUMP_CHANNEL` (pump ends take once).
  - `command` (`STRING`): Executed command, empty for shells.
  - `username` (`STRING`): Authenticated username, empty when unset.
  - `addr` (`STRING`): Peer address, empty when unset.

**Examples:**

**Example: route one session**

```oxdock
IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-route", {username: "u", password: "p"})

# Serve: dequeue one session and pump a synthetic reply.
LET $in: PIPE
LET $out: PIPE
LET $w: HANDLE<VOID> = ASYNC {
    LET $sess: SSH_DEQUEUE_INFO = SSH_DEQUEUE($m.server)
    ASSERT_EQ $sess.username "u"
    SSH_PUMP_CHANNEL($sess.session, $in, $out)
}

# Connect a client to the server.
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE<MAP<closed: BOOL, exit_status: INT>> = ASYNC { RETURN SSH_CONNECT("doc-ssh-route", "u", "p", $cin, $cout) }

# Close both inputs: each pump observes EOF, half-closes, and every
# task returns with no cancellation.
WITH_IO [stdout=$cin] ECHO "client-hello"
WITH_IO [stdout=$in] ECHO "server-greeting"
LET $cDone: MAP<closed: BOOL, exit_status: INT> = AWAIT $c
ASSERT_EQ $cDone.closed true
AWAIT $w
SSH_CLOSE($m.server)
```

### SSH_EXEC

Run argv on a remote host over SSH and collect the result.

Run one argv vector on a remote host over SSH and collect the
result. Stateless by design: every call carries its target,
credentials, and argv, holds no session, and returns one MAP with
`exit`, `stdout`, `stderr`, `truncated`, and `timed_out`. A retry
after a dropped channel repeats the same bytes, so idempotent
commands make retries boring.

The argv never becomes a shell string in script hands: it joins
once here through `shell_words::join`, the same vetted
implementation behind `SHELL_SPLIT_ARGS`/`SHELL_JOIN_ARGS` and the
remote plugins. Quoting `$HOME`, backticks, and redirects arrive as
literal arguments because no layer between the caller and this join
re-parses them.

Output caps apply while reading: past `max_bytes` per stream the
buffers stop growing but the drain continues, so a flood still
learns its exit status and reports `truncated`. A timeout kills
the wait and returns whatever arrived with `timed_out`. `exit` is
-1 when the server never sent a status.

**Signature:** `SSH_EXEC($target: STRING, $username: STRING, $password: STRING, $argv: LIST<STRING>, $options: MAP<timeout?: DURATION|STRING, max_bytes?: INT>): MAP`

**Contexts:** [`AST`](#context-ast) only

**Parameters:**

- `$target` (`STRING`): Dial target: a service name resolved through runner mappings.
- `$username` (`STRING`): Inner credentials for the SSH session.
- `$password` (`STRING`): Inner credentials for the SSH session.
- `$argv` (`LIST<STRING>`): Program and arguments to run remotely; joined once, by the
plugin, with POSIX shell quoting.
- `$options` (`MAP<timeout?: DURATION|STRING, max_bytes?: INT>`): `timeout` DURATION|STRING (default 60s) and `max_bytes` INT
(default 8 MiB per stream).
  - `timeout` (`DURATION|STRING`, optional)
  - `max_bytes` (`INT`, optional)

**Returns:** `MAP`

**Examples:**

**Example: exec collects remote output**

```oxdock
# The server side answers exec with a local run: dequeue the
# session, split the joined command back into argv, run it, and
# pump the child output into the channel. The exit status rides
# the wire through the session report.
IMPORT [STD, SSH]
INHERIT_ENV [PATH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-exec", {username: "u", password: "p"})
LET $d: HANDLE<SSH_DEQUEUE_INFO> = ASYNC { RETURN SSH_DEQUEUE($m.server) }
LET $runner: HANDLE<VOID> = ASYNC {
    LET $sess: SSH_DEQUEUE_INFO = AWAIT $d
    LET $argv: LIST<STRING> = SHELL_SPLIT_ARGS($sess.command)
    LET $wire: PIPE
    LET $drain: PIPE
    LET $pump: HANDLE<MAP<ANY>> = ASYNC { RETURN SSH_PUMP_CHANNEL($sess.session, $wire, $drain) }
    LET $pin: PIPE
    LET $pout: PIPE
    LET $perr: PIPE
    LET $run: HANDLE<INT> = ASYNC { RETURN SSH_RUN($sess.session, $argv, $pin, $pout, $perr) }
    LET $n: INT = SSH_PUMP($pout, $wire)
    LET $code: INT = AWAIT $run
    LET $pDone: MAP<ANY> = AWAIT $pump
    ASSERT_EQ $code 0
}

# One call, one MAP: argv in, collected streams out.
[family:unix] {
    LET $res: MAP<ANY> = SSH_EXEC("doc-ssh-exec", "u", "p", ["echo", "exec-hi"], {})
    ASSERT_EQ $res.timed_out false
    ASSERT_EQ $res.exit 0
    ASSERT_CONTAINS $res.stdout "exec-hi"
}
[family:windows] {
    LET $res: MAP<ANY> = SSH_EXEC("doc-ssh-exec", "u", "p", ["cmd", "/c", "echo exec-hi"], {})
    ASSERT_EQ $res.timed_out false
    ASSERT_EQ $res.exit 0
    ASSERT_CONTAINS $res.stdout "exec-hi"
}
AWAIT $runner
SSH_CLOSE($m.server)
```

### SSH_GENERATE_PASSWORD

Mint a random server password (256 bits, hex).

Each call draws fresh OS-RNG key material and renders 64
lowercase hex chars, so committed scripts carry no usable
credential: mint at runtime, hand the value to both sides, drop
it after. Pure (no step context), so it runs anywhere a STRING
binds, including option maps and placeholders.

**Signature:** `SSH_GENERATE_PASSWORD(): STRING`

**Contexts:** [`AST`](#context-ast), [`RPN`](#context-rpn)

**Returns:** `STRING`

**Examples:**

**Example: mint and serve**

```oxdock
IMPORT [STD, SSH]

# Fresh draws mint 64 hex chars with no committed secret.
LET $pass: STRING = SSH_GENERATE_PASSWORD()
LET $len: INT = LEN($pass)
ASSERT_EQ $len 64

# The minted value serves directly as a server password.
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-gen-pass", {username: "u", password: $pass})
SSH_CLOSE($m.server)
```

### SSH_PTY_RUN

Run a command under a sized local terminal into pipes.

Run `argv` under a local pseudo-terminal and pump it through
explicit pipes until the child exits. Must run inside `ASYNC`.
Returns the INT exit code.

`rows`/`cols` seed the initial size when positive, else the
session's requested size (24x80 default). Outer window-changes
resize live; each session owns its cell.

The code also reports as the wire `exit-status` at child exit, so
stock clients see the real code instead of 255.

Every session opens cleared and homed, like Win32-OpenSSH, so the
child renders onto a known blank screen.

Windows joining follows MSVCRT rules, so CRT programs parse back
exactly `argv`. `cmd /c` is the exception (its tail is a command,
not `argv`): route command text via `ENV SSH_COMMAND="..."` with
`["cmd", "/c", "%SSH_COMMAND%"]` instead.

Stdin arrives with console line endings handled: the pump expands
a lone `\n` to `\r\n` on Windows, so portable lines submit as-is
and an explicit `\r` passes through untouched. Real terminals
already send CR for Enter.

Environment is exactly the script environment like `RUN`:
block-scoped `ENV` (such as the session's `SSH_USER` / `SSH_CLIENT` /
`SSH_SERVER` / `SSH_COMMAND` relay) reaches the child; host names
arrive only through `INHERIT_ENV`, including `TERM` for terminal
definitions. The working directory comes from the script.

`WITH_IO [tty]` shares the same pump core and covers terminal
runs with host or static sizing and step-local reporting. This
function covers the two session-bound cases a script assembly
cannot reproduce: a rendered run with static geometry observes
the opener but never an outer resize (no script primitive
subscribes a running pty to the session cell), and a failing DSL
`RUN` aborts its task before any script-level status report could
follow (there is no try/catch; the close race after that would be
the second problem). If a second consumer ever needs
session-driven sizing or reaper-ordered reporting, that is the
point to revisit unifying the two.


**Signature:** `SSH_PTY_RUN($session: SSH_SESSION, $argv: LIST<STRING>, $rows: INT, $cols: INT, $in_pipe: PIPE, $out_pipe: PIPE): INT`

**Contexts:** [`AST`](#context-ast) only

**Parameters:**

- `$session` (`SSH_SESSION`): Session handle from `SSH_DEQUEUE`.
- `$argv` (`LIST<STRING>`): Program and arguments to run.
- `$rows` (`INT`): Terminal rows when positive, session size otherwise.
- `$cols` (`INT`): Terminal columns when positive, session size otherwise.
- `$in_pipe` (`PIPE`): Pipe carrying bytes consumed by the child.
- `$out_pipe` (`PIPE`): Pipe carrying bytes produced by the child.

**Returns:** `INT`

**Examples:**

**Example: run under a terminal**

```oxdock
IMPORT [STD, SSH]
INHERIT_ENV [PATH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-pty", {username: "u", password: "p"})
LET $d: HANDLE<SSH_DEQUEUE_INFO> = ASYNC { RETURN SSH_DEQUEUE($m.server) }
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE<VOID> = ASYNC { SSH_CONNECT("doc-ssh-pty", "u", "p", $cin, $cout) }
WITH_IO [stdout=$cin] ECHO "client-hello"
LET $sess: SSH_DEQUEUE_INFO = AWAIT $d

# The client task only proved the session live: cancel it before the
# terminal run, so no two pumps share the session.
CANCEL $c
LET $pin: PIPE
LET $pout: PIPE
# Assert on the whole pipe: terminal streams carry setup noise ahead
# of child output, so line reads are version-dependent.
[family:unix] {
    LET $t: HANDLE<INT> = ASYNC { RETURN SSH_PTY_RUN($sess.session, ["sh", "-c", "echo pty-hi"], 0, 0, $pin, $pout) }
    LET $code: INT = AWAIT $t
    ASSERT_EQ $code 0
    ASSERT_CONTAINS $pout "pty-hi"
}
[family:windows] {
    LET $t: HANDLE<INT> = ASYNC { RETURN SSH_PTY_RUN($sess.session, ["cmd", "/c", "echo pty-hi"], 0, 0, $pin, $pout) }
    LET $code: INT = AWAIT $t
    ASSERT_EQ $code 0
    ASSERT_CONTAINS $pout "pty-hi"
}
SSH_CLOSE($m.server)
```

### SSH_PUMP

Copy one pipe into another until EOF.

Copy one pipe into another until EOF, then close the target.
Either task placement works, as long as the other end is live.


**Signature:** `SSH_PUMP($from_pipe: PIPE, $to_pipe: PIPE): INT`

**Contexts:** [`AST`](#context-ast) only

**Parameters:**

- `$from_pipe` (`PIPE`): Source pipe to drain.
- `$to_pipe` (`PIPE`): Target pipe receiving the bytes (closed at EOF).

**Returns:** `INT`

**Examples:**

**Example: copy**

```oxdock
# Closing the source ends the copy: the count proves the bytes moved.
IMPORT [STD, SSH]
LET $in: PIPE
WITH_IO [stdout=$in] ECHO "pump-me"
LET $out: PIPE
LET $n: INT = SSH_PUMP($in, $out)
ASSERT_EQ $n 8
WITH_IO [stdin=$out] READ_LINE $got
ASSERT_EQ $got "pump-me"
```

### SSH_PUMP_CHANNEL

Pump a dequeued SSH session through pipes.

Pump a dequeued session through explicit pipes until the channel
closes. Must run inside `ASYNC`. Pump ends take once: a second
pump bails instead of splitting bytes.


**Signature:** `SSH_PUMP_CHANNEL($session: SSH_SESSION, $in_pipe: PIPE, $out_pipe: PIPE): MAP<closed: BOOL>`

**Contexts:** [`AST`](#context-ast) only

**Parameters:**

- `$session` (`SSH_SESSION`): Session handle from `SSH_DEQUEUE` (pump ends take once).
- `$in_pipe` (`PIPE`): Pipe carrying bytes consumed by the wire side.
- `$out_pipe` (`PIPE`): Pipe carrying bytes produced by the wire side.

**Returns:** `MAP<closed: BOOL>`

  - `closed` (`BOOL`): True when the session closed cleanly.

**Examples:**

**Example: pump a session**

```oxdock
IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-pump", {username: "u", password: "p"})
LET $d: HANDLE<SSH_DEQUEUE_INFO> = ASYNC { RETURN SSH_DEQUEUE($m.server) }
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE<MAP<closed: BOOL, exit_status: INT>> = ASYNC { RETURN SSH_CONNECT("doc-ssh-pump", "u", "p", $cin, $cout) }
WITH_IO [stdout=$cin] ECHO "client-hello"
LET $sess: SSH_DEQUEUE_INFO = AWAIT $d
LET $sin: PIPE
LET $sout: PIPE
LET $p: HANDLE<MAP<ANY>> = ASYNC { RETURN SSH_PUMP_CHANNEL($sess.session, $sin, $sout) }
WITH_IO [stdout=$sin] ECHO "server-greeting"
LET $cDone: MAP<closed: BOOL, exit_status: INT> = AWAIT $c
ASSERT_EQ $cDone.closed true
LET $pDone: MAP<ANY> = AWAIT $p
ASSERT_EQ $pDone.closed true
SSH_CLOSE($m.server)
```

### SSH_RUN

Run a command with piped stdio into explicit pipes.

Run `argv` directly (no terminal) and pump it through explicit pipes
until the child exits. Must run inside `ASYNC`. Returns the INT exit
code (-1 when the child died by signal).

Bytes travel untouched: no init sequence, no newline translation, no
setup handshake. This is the binary-safe leg `scp`-style protocols
need; `SSH_PTY_RUN` owns the interactive side instead.

The code reports as the wire `exit-status` at child exit, so stock
clients see the real code instead of 255. Signaled children report
nothing: clients observe the close without a status.

Stdin EOF closes the child's stdin, so stdin-driven children like
`cat` exit on their own. Standard output and error ride separate
pipes: diagnostics must never pollute the byte stream.

Environment is exactly the script environment like `RUN`:
block-scoped `ENV` reaches the child; host names arrive only through
`INHERIT_ENV`. The working directory comes from the script.

Windows joining follows MSVCRT rules, so CRT programs parse back
exactly `argv`. `cmd /c` is the exception (its tail is a command,
not `argv`): route command text via `ENV SSH_COMMAND="..."` with
`["cmd", "/c", "%SSH_COMMAND%"]` instead.

**Signature:** `SSH_RUN($session: SSH_SESSION, $argv: LIST<STRING>, $in_pipe: PIPE, $out_pipe: PIPE, $err_pipe: PIPE): INT`

**Contexts:** [`AST`](#context-ast) only

**Parameters:**

- `$session` (`SSH_SESSION`): Session handle from `SSH_DEQUEUE`.
- `$argv` (`LIST<STRING>`): Program and arguments to run.
- `$in_pipe` (`PIPE`): Pipe carrying bytes consumed by the child on stdin.
- `$out_pipe` (`PIPE`): Pipe carrying bytes produced by the child on stdout.
- `$err_pipe` (`PIPE`): Pipe carrying bytes produced by the child on stderr.

**Returns:** `INT`

**Examples:**

**Example: run with piped stdio**

```oxdock
IMPORT [STD, SSH]
INHERIT_ENV [PATH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-run", {username: "u", password: "p"})
LET $d: HANDLE<SSH_DEQUEUE_INFO> = ASYNC { RETURN SSH_DEQUEUE($m.server) }
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE<VOID> = ASYNC { SSH_CONNECT("doc-ssh-run", "u", "p", $cin, $cout) }
WITH_IO [stdout=$cin] ECHO "client-hello"
LET $sess: SSH_DEQUEUE_INFO = AWAIT $d

# The client task only proved the session live: cancel it before the
# run, so no two pumps share the session.
CANCEL $c
LET $pin: PIPE
LET $pout: PIPE
LET $perr: PIPE
LET $argv: LIST<STRING> = ["sh", "-c", "echo run-hi"]
[family:windows] {
    $argv = ["cmd", "/c", "echo run-hi"]
}
LET $t: HANDLE<INT> = ASYNC { RETURN SSH_RUN($sess.session, $argv, $pin, $pout, $perr) }
LET $code: INT = AWAIT $t
ASSERT_EQ $code 0
ASSERT_CONTAINS $pout "run-hi"
SSH_CLOSE($m.server)
```

### SSH_SEND_EXIT_STATUS

Send the session exit status to the client.

Report the session's exit status to the client (RFC 4254 6.10),
ahead of the channel close. Survives the take-once pump ends, so
call it before the pump drains. `SSH_PTY_RUN` reports by itself;
this covers other legs (`RUN`, synthetic replies).

**Signature:** `SSH_SEND_EXIT_STATUS($session: SSH_SESSION, $code: INT): BOOL`

**Contexts:** [`AST`](#context-ast) only

**Parameters:**

- `$session` (`SSH_SESSION`): Session handle from `SSH_DEQUEUE`.
- `$code` (`INT`): Exit status to report (0 to 4294967295).

**Returns:** `BOOL`

**Examples:**

**Example: report exit status**

```oxdock
IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-exit-status", {username: "u", password: "p"})
LET $srv: HANDLE<VOID> = ASYNC {
    LET $sess: SSH_DEQUEUE_INFO = SSH_DEQUEUE($m.server)
    LET $sin: PIPE
    LET $sout: PIPE
    LET $p: HANDLE<VOID> = ASYNC { SSH_PUMP_CHANNEL($sess.session, $sin, $sout) }
    SSH_SEND_EXIT_STATUS($sess.session, 3)
    WITH_IO [stdout=$sin] ECHO "server-greeting"
    AWAIT $p
}
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE<MAP<closed: BOOL, exit_status: INT>> = ASYNC { RETURN SSH_CONNECT("doc-ssh-exit-status", "u", "p", $cin, $cout) }
# Close the client input: its pump observes EOF and half-closes, so
# both pumps drain instead of idling on an empty pipe.
WITH_IO [stdout=$cin] ECHO "client-hello"
LET $cDone: MAP<closed: BOOL, exit_status: INT> = AWAIT $c
ASSERT_EQ $cDone.closed true
ASSERT_EQ $cDone.exit_status 3
AWAIT $srv
SSH_CLOSE($m.server)
```

### SSH_SERVE

Serve SSH on a virtual service endpoint.

Spin up an SSH server on a runner-mapped service. Unmapped and
memory services bail: SSH needs a TCP socket.
`options` takes required `username`/`password` STRINGs plus the
optional `key_path` STRING (workspace-relative Ed25519 file,
load-or-create; blank keeps the ephemeral key) and the optional
`idle_timeout` DURATION (quiet-connection kill, default 5m; zero
disables it for persistent sessions).

**Signature:** `SSH_SERVE($bind: STRING, $options: MAP<username: STRING, password: STRING, key_path?: STRING, idle_timeout?: DURATION|STRING>): MAP<server: SSH_SERVER, addr: STRING, username: STRING, password: STRING, virtual: STRING>`

**Contexts:** [`AST`](#context-ast) only

**Parameters:**

- `$bind` (`STRING`): Service name to serve; the runner maps it with -p/--listen.
- `$options` (`MAP<username: STRING, password: STRING, key_path?: STRING, idle_timeout?: DURATION|STRING>`): Server credentials and host key.
  - `username` (`STRING`, required)
  - `password` (`STRING`, required)
  - `key_path` (`STRING`, optional)
  - `idle_timeout` (`DURATION|STRING`, optional)

**Returns:** `MAP<server: SSH_SERVER, addr: STRING, username: STRING, password: STRING, virtual: STRING>`

  - `server` (`SSH_SERVER`): Server handle for `SSH_ACCEPT`, `SSH_DEQUEUE`, and `SSH_CLOSE`.
  - `addr` (`STRING`): Bound socket address (`ip:port`) of the server.
  - `username` (`STRING`): Configured username.
  - `password` (`STRING`): Configured password.
  - `virtual` (`STRING`): Virtual endpoint echo of the claimed slot.

**Examples:**

**Example: serve and close**

```oxdock
IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-serve", {username: "u", password: "p"})
ASSERT_EQ $m.virtual "doc-ssh-serve"
SSH_CLOSE($m.server)
```

## Value types

### Value type: SSH_SERVER

Handle to one ephemeral SSH server instance.

Cloning the value shares the server; dropping the last clone
signals shutdown.

Minted by `SSH_SERVE`; used by `SSH_ACCEPT`, `SSH_CLOSE`, `SSH_DEQUEUE`.

### Value type: SSH_SESSION

Handle to one dequeued SSH session instance.

Cloning the value shares the session; metadata reads never consume.
Display shows id and peer only: the command string may carry secrets.

Minted by `SSH_DEQUEUE`; used by `SSH_PTY_RUN`, `SSH_PUMP_CHANNEL`, `SSH_RUN`, `SSH_SEND_EXIT_STATUS`.

<!-- GENERATED by oxdock-render from function context metadata. Do not edit by hand. -->
## Contexts

Every function lists the contexts it runs in. `AST` is always available. `RPN` additionally requires a pure function or an explicit `rpn` opt in.

### Context: AST

The parsed tree of a script, walked one step at a time. Statements, declarations, scopes, pipes, and IO live here. Every step knows its line number, so failures name it. Every function runs in this context.

### Context: RPN

Arithmetic and comparison expressions compiled to a flat stack program of values with no statements, scopes, pipes, or step numbers. Pure functions run here with no flag. Stateful functions run here only when they opt in with `#[oxdock_func(rpn)]`, which is reserved for read only queries that stay meaningful inside math (`GLOB`, `LOAD_TOML`, `LOAD_JSON` do this). A call that fails inside math reports the bare error with no step number.

## License

`oxdock-ssh-plugin` is distributed under the terms of the [Apache License (Version 2.0)](https://github.com/jzombie/rust-oxdock/blob/main/LICENSE).