dsh-plugin-wsl-env 0.17.1 → 0.19.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.
Files changed (110) hide show
  1. package/CHANGELOG.md +225 -0
  2. package/CONTRIBUTING.md +37 -5
  3. package/README.md +61 -6
  4. package/SUPPORT.md +3 -3
  5. package/agent/native/linux-aarch64/dsh-wsl-agent +0 -0
  6. package/agent/native/linux-x86_64/dsh-wsl-agent +0 -0
  7. package/agent/wsl-agent.sh +331 -11
  8. package/cordis.patch.yml +10 -5
  9. package/docs/AGENT-PROTOCOL-V7.md +175 -0
  10. package/docs/API.md +24 -0
  11. package/docs/ARCHITECTURE.md +20 -6
  12. package/docs/COMPATIBILITY.md +1 -1
  13. package/docs/CONFIGURATION.md +33 -5
  14. package/docs/CONFIGURATION.zh.md +25 -4
  15. package/docs/LIMITATIONS.md +39 -7
  16. package/docs/LIMITATIONS.zh.md +31 -6
  17. package/docs/MIGRATION.md +4 -4
  18. package/docs/PEER-PARITY.md +94 -35
  19. package/docs/QUICKSTART.md +19 -2
  20. package/docs/QUICKSTART.zh.md +16 -1
  21. package/docs/README.zh.md +47 -7
  22. package/docs/RELEASING.md +3 -2
  23. package/docs/REQUIREMENTS.zh.md +11 -6
  24. package/docs/ROADMAP-1.0.md +40 -29
  25. package/docs/ROADMAP.md +10 -6
  26. package/docs/UPSTREAM-FILE-REFERENCE-SEAM.md +6 -1
  27. package/docs/UPSTREAM-FSIO-EXPORT.md +2 -2
  28. package/docs/UPSTREAM-SERVICE-SWAP.md +124 -0
  29. package/docs/UPSTREAM-SESSION-NOTICE.md +3 -1
  30. package/docs/UPSTREAM-TERMINAL-TITLE.md +3 -0
  31. package/docs/root-fs-routing.md +3 -3
  32. package/lib/agent-confined.js +4 -4
  33. package/lib/agent-errors.d.ts +1 -1
  34. package/lib/agent-errors.js +1 -1
  35. package/lib/agent-exec.d.ts +1 -1
  36. package/lib/agent-exec.js +1 -1
  37. package/lib/agent-protocol.d.ts +58 -8
  38. package/lib/agent-protocol.js +40 -7
  39. package/lib/agent-shared.d.ts +1 -1
  40. package/lib/agent-shared.js +1 -1
  41. package/lib/agent.d.ts +171 -1
  42. package/lib/agent.js +448 -288
  43. package/lib/auto-preset.d.ts +1 -1
  44. package/lib/auto-preset.js +1 -1
  45. package/lib/bwrap.d.ts +1 -1
  46. package/lib/bwrap.js +1 -1
  47. package/lib/distro-route.d.ts +1 -1
  48. package/lib/distro-route.js +1 -1
  49. package/lib/doctor-live.d.ts +53 -0
  50. package/lib/doctor-live.js +37 -0
  51. package/lib/file-reference-wsl.d.ts +1 -1
  52. package/lib/file-reference-wsl.js +1 -1
  53. package/lib/fs-decisions.d.ts +1 -1
  54. package/lib/fs-decisions.js +1 -1
  55. package/lib/fs-routing.d.ts +52 -2
  56. package/lib/fs-routing.js +56 -1
  57. package/lib/fs-substrate.d.ts +90 -11
  58. package/lib/fs-substrate.js +202 -1
  59. package/lib/fs.d.ts +1 -1
  60. package/lib/fs.js +1 -1
  61. package/lib/fsio-agent.d.ts +128 -4
  62. package/lib/fsio-agent.js +224 -15
  63. package/lib/fsio-text.d.ts +1 -1
  64. package/lib/fsio-text.js +1 -1
  65. package/lib/git-exec.d.ts +1 -1
  66. package/lib/git-exec.js +1 -1
  67. package/lib/git-route.d.ts +1 -1
  68. package/lib/git-route.js +1 -1
  69. package/lib/index.d.ts +127 -3
  70. package/lib/index.js +301 -99
  71. package/lib/listing.d.ts +1 -1
  72. package/lib/listing.js +1 -1
  73. package/lib/native-agent.d.ts +1 -1
  74. package/lib/native-agent.js +2 -2
  75. package/lib/paths.d.ts +6 -3
  76. package/lib/paths.js +4 -4
  77. package/lib/picker.d.ts +1 -1
  78. package/lib/picker.js +7 -7
  79. package/lib/ports.d.ts +1 -1
  80. package/lib/ports.js +1 -1
  81. package/lib/preflight.d.ts +1 -1
  82. package/lib/preflight.js +1 -1
  83. package/lib/sandbox-core.d.ts +1 -1
  84. package/lib/sandbox-core.js +2 -2
  85. package/lib/sandbox.d.ts +1 -1
  86. package/lib/sandbox.js +1 -1
  87. package/lib/search-exec.d.ts +1 -1
  88. package/lib/search-exec.js +1 -1
  89. package/lib/search-route.d.ts +1 -1
  90. package/lib/search-route.js +1 -1
  91. package/lib/session-runtime.d.ts +1 -1
  92. package/lib/session-runtime.js +2 -2
  93. package/lib/session-scope.d.ts +1 -1
  94. package/lib/session-scope.js +1 -1
  95. package/lib/shell-env.d.ts +1 -1
  96. package/lib/shell-env.js +1 -1
  97. package/lib/shell.d.ts +1 -1
  98. package/lib/shell.js +1 -1
  99. package/lib/subprocess.d.ts +1 -1
  100. package/lib/subprocess.js +2 -2
  101. package/lib/terminal-activity.d.ts +1 -1
  102. package/lib/terminal-activity.js +1 -1
  103. package/lib/terminal-route.d.ts +1 -1
  104. package/lib/terminal-route.js +1 -1
  105. package/lib/watcher.d.ts +1 -1
  106. package/lib/watcher.js +1 -1
  107. package/lib/wsl.d.ts +1 -31
  108. package/lib/wsl.js +1 -21
  109. package/package.json +41 -28
  110. package/scripts/doctor.mjs +34 -11
@@ -8,15 +8,22 @@
8
8
  # and speaks a line protocol over stdio. Fields are separated by `|`; binary
9
9
  # payloads (cwd, argv words, env values, captured output) travel as single-line
10
10
  # base64. The protocol deliberately avoids JSON: the agent must run under a
11
- # bare POSIX sh with no dependencies beyond coreutils' `base64`, because the
11
+ # bare POSIX sh. The tool floor is GNU coreutils' `base64`, GNU `find`
12
+ # (-printf), `realpath -e/-L`, `sync -f/-d` and `od` — the container suite
13
+ # exercises the BusyBox/apk family against exactly that floor, because the
12
14
  # one thing a distro is guaranteed to have is a shell.
13
15
  #
14
16
  # Handshake — the first line the agent writes:
15
- # HELLO|wsl-agent|<protocol version>|<sha256 of this script file>
17
+ # HELLO|wsl-agent|<protocol version>|<sha256 of this script file>|<pid>|<caps>
16
18
  # The digest lets the host verify that the script it is about to rely on is
17
19
  # byte-identical to the copy it shipped. The script is read in place — a
18
20
  # /mnt/c mirror in the deployed layout — so a stale or half-synced runtime
19
21
  # copy would otherwise pass the version handshake while drifting in behavior.
22
+ # Protocol 7 adds the sixth field, `caps`: a comma-separated capability token
23
+ # list (`conc` = FS ops may answer out of order, `batch` = the FSB frame is
24
+ # accepted). This agent advertises `batch` — its read loop is serial, so a
25
+ # batch (one worker, in-order answers) is safe while pipelined single FS
26
+ # frames are not. A v6 peer parser ignores the extra field.
20
27
  # Requests the agent accepts, one per line on stdin:
21
28
  # PING -> PONG
22
29
  # SHUTDOWN -> exits 0
@@ -48,14 +55,36 @@
48
55
  # ops: stat <path> · lstat <path> · list <dir> · realpath <path>
49
56
  # read <path> <offset> <maxbytes> · write <path> <mode|->
50
57
  # <replace|no-replace> <expected version|-> <content-b64>
58
+ # delete <path> <expected version|-> [protocol 7]
59
+ # move <src> <dst> <dst mode|-> <dst replace|no-replace|->
60
+ # <src expected version|-> <dst expected version|-> [7]
51
61
  # -> ACK|<id> at dequeue, then the same RES|<id>|<exit code>|<b64
52
62
  # stdout>|<b64 stderr> line as EXEC.
53
- # A failed op writes `dsh-fs|<reason>|<b64 message>` as the FIRST line
54
- # of its stderr (reason: notfound notdir perm loop exists stale io),
55
- # which the host's adapter maps onto the FsError codes the tool layer
56
- # knows. A `replace` write carrying an expected version re-verifies it
57
- # against a fresh stat before the rename and refuses with `stale` on
58
- # a mismatch; a later concurrent write can still be overwritten.
63
+ # A failed op answers with the structured line
64
+ # FSERR|<id>|<reason>|<b64 message> instead of the RES (protocol 7;
65
+ # the host maps the reason directly), and ALSO writes
66
+ # `dsh-fs|<reason>|<b64 message>` as the FIRST line of its stderr
67
+ # (reason: notfound notdir perm loop exists stale io) — the
68
+ # convention older hosts parse. A `replace` write carrying an
69
+ # expected version re-verifies it against a fresh stat before the
70
+ # rename and refuses with `stale` on a mismatch; a later concurrent
71
+ # write can still be overwritten. A `delete` re-verifies the same
72
+ # way and refuses with `stale` on a mismatch or a vanished file. A
73
+ # `move` guards the source the same way, publishes the destination
74
+ # through the write machinery's staging (atomic rename, or a
75
+ # no-replace hard link for a guarded create), then removes the
76
+ # source; its stdout carries the destination's fresh stat record
77
+ # and — when the source survived — one SRCFAIL|<reason>|<b64 message>
78
+ # line (the peer's two-effect `FsMoveOutcome`, honestly preserved).
79
+ # FSB|<id>|<timeout seconds|0>|<n ops> [protocol 7]
80
+ # followed by <n ops> records, each an OP|<op>|<n args> line plus its
81
+ # base64 arguments. The ops run IN ORDER in one worker and the answer
82
+ # is ONE RES whose payload is the concatenated per-op records:
83
+ # OK <i>|<b64 payload>
84
+ # BAD <i>|<reason>|<b64 message>
85
+ # A failed op records BAD and the batch continues — a batch is a set of
86
+ # independent reads, not a transaction. One round trip for what costs
87
+ # four to six FS frames (the GUI preview's stat + list + read shape).
59
88
  #
60
89
  # Confinement is NOT this script's business: the host wraps argv in the bwrap
61
90
  # profile (lib/sandbox.js) before the frame is sent, so the agent executes
@@ -73,7 +102,7 @@ set -u
73
102
  # the child without it (NUL bytes — `check-ignore -z` separators — survive).
74
103
  # Every known consumer parses line-oriented stdin that tolerates a missing
75
104
  # final newline.
76
- PROTO_VERSION=6
105
+ PROTO_VERSION=7
77
106
  TMPDIR_AGENT=$(mktemp -d "${TMPDIR:-/tmp}/wsl-agent.XXXXXX")
78
107
  OUT_FILE="$TMPDIR_AGENT/out"
79
108
  ERR_FILE="$TMPDIR_AGENT/err"
@@ -225,6 +254,7 @@ timeout_watcher() {
225
254
  run_capture() {
226
255
  local req_id timeout_s maxout mode rc watcher_pid out_cut err_cut
227
256
  local out_fifo err_fifo out_reader err_reader spawn_out spawn_err killer_pid
257
+ local first_err rest_fs reason_fs msg_fs
228
258
  req_id=$1
229
259
  timeout_s=$2
230
260
  maxout=$3
@@ -331,6 +361,22 @@ run_capture() {
331
361
  # the protocol.
332
362
  out_cut=0
333
363
  err_cut=0
364
+ # Protocol 7 FS ops answer a failure with the structured FSERR line instead
365
+ # of a RES the host must sniff: the decision is the captured stderr's first
366
+ # line, which the handlers pin to the dsh-fs| convention.
367
+ if [ "$mode" = subshell ] && [ "$rc" -ne 0 ]; then
368
+ first_err=$(head -n 1 -- "$ERR_FILE" 2>/dev/null)
369
+ case $first_err in
370
+ dsh-fs\|*)
371
+ rest_fs=${first_err#dsh-fs|}
372
+ reason_fs=${rest_fs%%|*}
373
+ msg_fs=${rest_fs#*|}
374
+ printf 'FSERR|%s|%s|%s\n' "$req_id" "$reason_fs" "${msg_fs:-$(printf '' | base64 | tr -d '\n')}"
375
+ rm -f "$TMPDIR_AGENT/inflight"
376
+ return 0
377
+ ;;
378
+ esac
379
+ fi
334
380
  if [ "$maxout" -gt 0 ]; then
335
381
  [ "$(wc -c <"$OUT_FILE" 2>/dev/null || printf 0)" -gt "$maxout" ] && out_cut=1
336
382
  [ "$(wc -c <"$ERR_FILE" 2>/dev/null || printf 0)" -gt "$maxout" ] && err_cut=1
@@ -462,7 +508,11 @@ fs_read() {
462
508
  p=$(b64dec "$1")
463
509
  offset=$(b64dec "$2")
464
510
  max=$(b64dec "$3")
465
- [ "$max" -gt 0 ] || return 0
511
+ # A malformed window is a protocol-class failure, not an empty answer: the
512
+ # host always sends numerics, so anything else is a frame worth refusing.
513
+ case $offset in ''|*[!0-9]*) fs_fail protocol "fs read: offset is not a number";; esac
514
+ case $max in ''|*[!0-9]*) fs_fail protocol "fs read: max is not a number";; esac
515
+ [ "$max" -gt 0 ] || fs_fail protocol "fs read: max must be positive, got $max"
466
516
  : >"$ERR_FILE"
467
517
  tail -c +"$(( offset + 1 ))" -- "$p" 2>"$ERR_FILE" | head -c "$max" -- 2>/dev/null
468
518
  if [ -s "$ERR_FILE" ]; then
@@ -626,9 +676,221 @@ fs_write() {
626
676
  fs_fail "$(fs_classify "$err")" "$err"
627
677
  }
628
678
  fi
679
+ # The published name is only as durable as its directory entry: fsync the
680
+ # containing directory too, so a power loss cannot lose the rename that the
681
+ # file sync already made safe. Best effort — sync builds without -d and
682
+ # filesystems that refuse a directory fsync keep the peer's own promise.
683
+ sync -d -- "$(dirname -- "$path")" 2>/dev/null || :
629
684
  drop_staging
630
685
  }
631
686
 
687
+ fs_delete() {
688
+ # $1 = base64 path, $2 = base64 expected version (`-` for none). The peer's
689
+ # requireFile dialect: a vanished file is a stale guard, a non-regular
690
+ # target its own refusal, a version mismatch stale. One regular file comes
691
+ # off the disk; the host holds the diff basis it read beforehand.
692
+ local path expected version err
693
+ path=$(b64dec "$1")
694
+ expected=$(b64dec "$2")
695
+ LC_ALL=C
696
+ export LC_ALL=C
697
+ [ -n "$path" ] && [ "$path" != "/" ] || fs_fail notfound "empty file name"
698
+ if [ ! -e "$path" ] && [ ! -L "$path" ]; then
699
+ fs_fail stale "cannot delete \"$path\": file changed since it was read"
700
+ fi
701
+ if [ -d "$path" ] && [ ! -L "$path" ]; then
702
+ fs_fail notdir "cannot delete \"$path\": not a regular file"
703
+ fi
704
+ if [ "$expected" != "-" ]; then
705
+ version=$(fs_version "$path")
706
+ if [ "$version" != "$expected" ]; then
707
+ fs_fail stale "cannot delete \"$path\": file changed since it was read"
708
+ fi
709
+ fi
710
+ rm -f -- "$path" 2>"$ERR_FILE" || {
711
+ err=$(cat "$ERR_FILE")
712
+ fs_fail "$(fs_classify "$err")" "$err"
713
+ }
714
+ }
715
+
716
+ fs_move() {
717
+ # $1 = base64 source, $2 = base64 destination, $3 = base64 destination mode
718
+ # (`-` keeps the default), $4 = base64 destination publish (`no-replace`
719
+ # for a guarded create, anything else = `replace`), $5 = base64 source
720
+ # expected version (`-` for none), $6 = base64 destination expected version
721
+ # (`-` for none). The peer's moveFile shape: the source passes requireFile,
722
+ # the destination passes writeTarget, the bytes publish through the write
723
+ # machinery's staging (atomic rename, or the no-replace link for a guarded
724
+ # create), and the source is removed last. The stdout carries the
725
+ # destination's fresh stat record (the host turns it into the outcome's
726
+ # version) and — when the source survived — one
727
+ # SRCFAIL|<reason>|<b64 message> line: the two effects are separate, and
728
+ # the peer's `sourceDeleted=false` shape must stay observable.
729
+ local src dst mode guard sexp dexp ddir dbase staging tmp err version u src_err
730
+ src=$(b64dec "$1")
731
+ dst=$(b64dec "$2")
732
+ mode=$(b64dec "$3")
733
+ guard=$(b64dec "$4")
734
+ sexp=$(b64dec "$5")
735
+ dexp=$(b64dec "$6")
736
+ LC_ALL=C
737
+ export LC_ALL=C
738
+ ddir=$(dirname -- "$dst")
739
+ dbase=$(basename -- "$dst")
740
+ [ -n "$dbase" ] && [ "$dbase" != "/" ] || fs_fail notfound "empty file name"
741
+ # Source: requireFile's dialect.
742
+ if [ ! -e "$src" ] && [ ! -L "$src" ]; then
743
+ fs_fail stale "cannot move \"$src\": file changed since it was read"
744
+ fi
745
+ if [ -d "$src" ] && [ ! -L "$src" ]; then
746
+ fs_fail notdir "cannot move \"$src\": not a regular file"
747
+ fi
748
+ if [ "$sexp" != "-" ]; then
749
+ version=$(fs_version "$src")
750
+ if [ "$version" != "$sexp" ]; then
751
+ fs_fail stale "cannot move \"$src\": file changed since it was read"
752
+ fi
753
+ fi
754
+ # Destination: writeTarget's dialect, on whatever the disk holds now.
755
+ if [ -e "$dst" ] || [ -L "$dst" ]; then
756
+ if [ -d "$dst" ] && [ ! -L "$dst" ]; then
757
+ fs_fail notdir "cannot move \"$src\" to \"$dst\": not a regular file"
758
+ fi
759
+ if [ "$dexp" != "-" ]; then
760
+ version=$(fs_version "$dst")
761
+ if [ "$version" != "$dexp" ]; then
762
+ fs_fail stale "cannot move \"$src\" to \"$dst\": file changed since it was read"
763
+ fi
764
+ fi
765
+ else
766
+ if [ "$dexp" != "-" ]; then
767
+ fs_fail stale "cannot move \"$src\" to \"$dst\": file no longer exists"
768
+ fi
769
+ fi
770
+ sweep_staging "$ddir" "$dbase"
771
+ u=$(head -c 8 /dev/urandom 2>/dev/null | od -An -tx1 | tr -d ' \n')
772
+ staging="$ddir/.$dbase.$$.${u:-0}.tmpdir"
773
+ mkdir -- "$staging" 2>"$ERR_FILE" || {
774
+ err=$(cat "$ERR_FILE")
775
+ fs_fail "$(fs_classify "$err")" "$err"
776
+ }
777
+ CURRENT_STAGING=$staging
778
+ chmod 700 -- "$staging" 2>/dev/null
779
+ tmp="$staging/$dbase.tmp"
780
+ # The bytes travel kernel-side (cat to the staged temp) — no base64 round
781
+ # trip for a move's payload.
782
+ cat -- "$src" >"$tmp" 2>"$ERR_FILE" || {
783
+ err=$(cat "$ERR_FILE")
784
+ drop_staging
785
+ fs_fail io "$err"
786
+ }
787
+ chmod 600 -- "$tmp"
788
+ sync -f -- "$tmp" 2>/dev/null || :
789
+ if [ "$mode" != "-" ]; then
790
+ chmod "$mode" -- "$tmp" 2>"$ERR_FILE" || {
791
+ err=$(cat "$ERR_FILE")
792
+ drop_staging
793
+ fs_fail io "$err"
794
+ }
795
+ fi
796
+ if [ "$guard" = "no-replace" ]; then
797
+ if [ -e "$dst" ] || [ -L "$dst" ]; then
798
+ drop_staging
799
+ fs_fail exists "cannot move \"$src\" to \"$dst\": it already exists"
800
+ fi
801
+ ln -- "$tmp" "$dst" 2>"$ERR_FILE" || {
802
+ err=$(cat "$ERR_FILE")
803
+ drop_staging
804
+ fs_fail "$(fs_classify "$err")" "$err"
805
+ }
806
+ if ! [ "$tmp" -ef "$dst" ]; then
807
+ rm -f -- "$dst/$dbase.tmp"
808
+ drop_staging
809
+ fs_fail exists "cannot move \"$src\" to \"$dst\": it was created concurrently"
810
+ fi
811
+ else
812
+ mv -f -- "$tmp" "$dst" 2>"$ERR_FILE" || {
813
+ err=$(cat "$ERR_FILE")
814
+ drop_staging
815
+ fs_fail "$(fs_classify "$err")" "$err"
816
+ }
817
+ fi
818
+ # Same directory-entry durability as the write's publish above.
819
+ sync -d -- "$(dirname -- "$dst")" 2>/dev/null || :
820
+ drop_staging
821
+ # The destination's fresh identity, then the source's fate.
822
+ fs_stat -L "$(printf '%s' "$dst" | base64 | tr -d '\n')"
823
+ if rm -f -- "$src" 2>"$ERR_FILE"; then
824
+ :
825
+ else
826
+ src_err=$(cat "$ERR_FILE")
827
+ printf 'SRCFAIL|%s|%s\n' "$(fs_classify "$src_err")" "$(printf '%s' "$src_err" | base64 | tr -d '\n')"
828
+ fi
829
+ }
830
+
831
+ fs_batch_runner() {
832
+ # $1 = the flat batch record the FSB case assembled: op|nargs|arg…repeated.
833
+ # Every element is pipe-free (base64 alphabet, plain op names), so the
834
+ # walk below splits exactly. Each op runs in a subshell with its own
835
+ # capture files; the record stream lands on stdout (run_capture's OUT_FILE,
836
+ # which becomes the batch's single RES payload).
837
+ local batch op nargs rest arg n i first_err reason msg
838
+ batch=$1
839
+ LC_ALL=C
840
+ export LC_ALL=C
841
+ i=0
842
+ while [ -n "$batch" ]; do
843
+ i=$((i + 1))
844
+ op=${batch%%|*}
845
+ case $batch in
846
+ *\|*) rest=${batch#*|} ;;
847
+ *) rest= ;;
848
+ esac
849
+ nargs=${rest%%|*}
850
+ case $rest in
851
+ *\|*) rest=${rest#*|} ;;
852
+ *) rest= ;;
853
+ esac
854
+ case $nargs in
855
+ ''|*[!0-9]*) nargs=0 ;;
856
+ esac
857
+ set --
858
+ n=0
859
+ while [ "$n" -lt "$nargs" ]; do
860
+ arg=${rest%%|*}
861
+ case $rest in
862
+ *\|*) rest=${rest#*|} ;;
863
+ *) rest= ;;
864
+ esac
865
+ n=$((n + 1))
866
+ set -- "$@" "$arg"
867
+ done
868
+ # What remains belongs to the NEXT op record; keep it before the error
869
+ # parsing below reuses `rest`.
870
+ batch=$rest
871
+ opout="$TMPDIR_AGENT/b.$i.out"
872
+ operr="$TMPDIR_AGENT/b.$i.err"
873
+ if ( fs_dispatch "$op" "$@" ) >"$opout" 2>"$operr"; then
874
+ printf 'OK %s|%s\n' "$i" "$(b64enc_file "$opout")" >>"$TMPDIR_AGENT/b.res"
875
+ else
876
+ first_err=$(head -n 1 -- "$operr" 2>/dev/null)
877
+ case $first_err in
878
+ dsh-fs\|*)
879
+ rest=${first_err#dsh-fs|}
880
+ reason=${rest%%|*}
881
+ msg=${rest#*|}
882
+ printf 'BAD %s|%s|%s\n' "$i" "$reason" "${msg:-$(printf '' | base64 | tr -d '\n')}" >>"$TMPDIR_AGENT/b.res"
883
+ ;;
884
+ *)
885
+ printf 'BAD %s|io|%s\n' "$i" "$(printf '%s' "$first_err" | base64 | tr -d '\n')" >>"$TMPDIR_AGENT/b.res"
886
+ ;;
887
+ esac
888
+ fi
889
+ rm -f -- "$opout" "$operr"
890
+ done
891
+ cat -- "$TMPDIR_AGENT/b.res"
892
+ }
893
+
632
894
  fs_dispatch() {
633
895
  # $1 = op, $2.. = raw base64 arguments (handlers decode what they need to
634
896
  # treat as a path; write's content stays base64 until it hits the file).
@@ -644,13 +906,15 @@ fs_dispatch() {
644
906
  realpath) fs_realpath "$1" ;;
645
907
  read) fs_read "$1" "$2" "$3" ;;
646
908
  write) fs_write "$1" "$2" "$3" "$4" "$5" ;;
909
+ delete) fs_delete "$1" "$2" ;;
910
+ move) fs_move "$1" "$2" "$3" "$4" "$5" "$6" ;;
647
911
  # A host/agent contract breach is an I/O-class failure, not a file that
648
912
  # was not found: the mapped reason reads FS_IO_ERROR host-side.
649
913
  *) fs_fail protocol "unknown fs op" ;;
650
914
  esac
651
915
  }
652
916
 
653
- printf 'HELLO|wsl-agent|%s|%s|%s\n' "$PROTO_VERSION" "$AGENT_DIGEST" "$$"
917
+ printf 'HELLO|wsl-agent|%s|%s|%s|%s\n' "$PROTO_VERSION" "$AGENT_DIGEST" "$$" batch
654
918
 
655
919
  while IFS= read -r line; do
656
920
  case $line in
@@ -695,6 +959,12 @@ while IFS= read -r line; do
695
959
  nargs=${rest%%|*}
696
960
  rest=${rest#*|}
697
961
  maxout=${rest%%|*}
962
+ case $timeout_s in
963
+ ''|*[!0-9]*) timeout_s=0 ;;
964
+ esac
965
+ case $nargs in
966
+ ''|*[!0-9]*) nargs=0 ;;
967
+ esac
698
968
  # Protocol v6: an optional seventh field flags one stdin payload line
699
969
  # after the argv words. Absent (a stale peer) or 0 keeps the v5 shape —
700
970
  # the child reads an empty stdin and immediate EOF.
@@ -756,6 +1026,12 @@ while IFS= read -r line; do
756
1026
  rest=${rest#*|}
757
1027
  timeout_s=${rest%%|*}
758
1028
  nargs=${rest#*|}
1029
+ case $timeout_s in
1030
+ ''|*[!0-9]*) timeout_s=0 ;;
1031
+ esac
1032
+ case $nargs in
1033
+ ''|*[!0-9]*) nargs=0 ;;
1034
+ esac
759
1035
  # Arguments stay base64 (write's content must never become a shell
760
1036
  # variable of raw bytes); handlers decode what they treat as a path.
761
1037
  # A frame that ends before its declared argument count must never run
@@ -772,6 +1048,50 @@ while IFS= read -r line; do
772
1048
  # by the protocol.
773
1049
  run_capture "$req_id" "$timeout_s" 0 subshell fs_dispatch "$op" "$@"
774
1050
  ;;
1051
+ FSB\|*)
1052
+ # Protocol 7's batched FS frame: op records are read into one flat
1053
+ # pipe-free record string and handed to a single worker, whose record
1054
+ # stream becomes the one RES payload. The batch file starts empty so a
1055
+ # previous batch's records can never leak into this answer.
1056
+ header=${line#FSB|}
1057
+ req_id=${header%%|*}
1058
+ rest=${header#*|}
1059
+ timeout_s=${rest%%|*}
1060
+ nops=${rest#*|}
1061
+ case $nops in
1062
+ ''|*[!0-9]*) nops=0 ;;
1063
+ esac
1064
+ batch=
1065
+ n=0
1066
+ while [ "$n" -lt "$nops" ]; do
1067
+ IFS= read -r specline || exit 0
1068
+ specline=${specline#OP|}
1069
+ op=${specline%%|*}
1070
+ case $specline in
1071
+ *\|*) rest=${specline#*|} ;;
1072
+ *) rest= ;;
1073
+ esac
1074
+ nargs=${rest%%|*}
1075
+ case $rest in
1076
+ *\|*) rest=${rest#*|} ;;
1077
+ *) rest= ;;
1078
+ esac
1079
+ case $nargs in
1080
+ ''|*[!0-9]*) nargs=0 ;;
1081
+ esac
1082
+ n=$((n + 1))
1083
+ batch="${batch:+$batch|}$op|$nargs"
1084
+ k=0
1085
+ while [ "$k" -lt "$nargs" ]; do
1086
+ IFS= read -r arg_b64 || exit 0
1087
+ k=$((k + 1))
1088
+ batch="$batch|$arg_b64"
1089
+ done
1090
+ done
1091
+ : >"$TMPDIR_AGENT/b.res"
1092
+ run_capture "$req_id" "$timeout_s" 0 subshell fs_batch_runner "$batch"
1093
+ rm -f -- "$TMPDIR_AGENT/b.res"
1094
+ ;;
775
1095
  *)
776
1096
  printf 'ERR||protocol|%s\n' "$(printf '%s' "unrecognized request" | base64 | tr -d '\n')"
777
1097
  ;;
package/cordis.patch.yml CHANGED
@@ -191,7 +191,7 @@
191
191
  # Generated from the shipped standard preset so the embedded plan-mode
192
192
  # section and every nested group survive byte-for-byte: this copy is
193
193
  # `packages/bundle/web-app/presets/standard.patch.yml` of the
194
- # `dsh-v0.2.1-rc.3` source tree (unchanged since `0.2.1-rc.2`), changed
194
+ # `dsh-v0.2.2-rc.1` source tree, changed
195
195
  # in exactly three ways —
196
196
  # 1. every shipped row mounts inside `wsl-env`, so the preset's tools see
197
197
  # the distro's shell and fs (the realm is declared on the group);
@@ -288,6 +288,12 @@
288
288
  disabled: true
289
289
  - id: tool-fs
290
290
  name: '@deepseek-ai/dsh-tool-fs'
291
+ config:
292
+ # Set false to drop the apply_patch tool beside edit. The
293
+ # base patchText is the host-side implementation; the distro
294
+ # substrate serves read/write/edit itself and must not have
295
+ # a patch path that quietly crosses the 9p share instead.
296
+ applyPatch: false
291
297
  - id: tool-fs-search
292
298
  name: '@deepseek-ai/dsh-tool-fs-search'
293
299
  config:
@@ -350,14 +356,13 @@
350
356
  config:
351
357
  - id: tool-subagent-control
352
358
  name: '@deepseek-ai/dsh-tool-subagent-control'
353
- - id: tool-subagent-list-agents
354
- name: '@deepseek-ai/dsh-tool-subagent-control/list-agents'
355
359
  - id: tool-subagent
356
360
  name: '@deepseek-ai/dsh-tool-subagent'
357
361
  config:
358
362
  provider: spawn
359
363
  toolName: subagent
360
- modelSelectionSettings: true
364
+ modelSelection: true
365
+ modelDiscovery: true
361
366
  - id: tool-subagent-fork
362
367
  name: '@deepseek-ai/dsh-tool-subagent'
363
368
  config:
@@ -384,7 +389,7 @@
384
389
  name: '@deepseek-ai/dsh-tool-present'
385
390
  - id: tool-plugin-manager
386
391
  name: '@deepseek-ai/dsh-plugin-manager/tools'
387
- disabled: true
392
+ disabled: !!js "!ctx.get('profileContext')"
388
393
  - id: tool-plugin-market
389
394
  name: '@deepseek-ai/dsh-plugin-market/tools'
390
395
  disabled: true
@@ -0,0 +1,175 @@
1
+ # Agent protocol v7 — design note (WSL-312)
2
+
3
+ Status: accepted · Supersedes: the v6 increment's unclaimed FS part ·
4
+ Author: the plugin maintainers · Date: 2026-11-05
5
+
6
+ ## What v7 is
7
+
8
+ Protocol v6 (0.17.0) took the EXEC stdin-payload increment; the FS half of
9
+ WSL-312 was deferred. v7 is the FS half, plus the substrate for the peer's
10
+ new `patchText` / `deleteFile` / `moveFile` operations. One revision, one
11
+ negotiation rule, four capabilities.
12
+
13
+ ## What ships
14
+
15
+ | Capability | Wire | Native agent | Shell agent | Host |
16
+ |---|---|---|---|---|
17
+ | Capability advertisement | HELLO gains a 6th field | `conc,batch` | `batch` | capability-aware FS scheduling |
18
+ | Structured FS errors | new `FSERR\|id\|reason\|b64 message` line | yes | yes | direct FsError mapping (the `dsh-fs\|…` stderr-line sniffing stays as a fallback for older agents) |
19
+ | Batched FS reads | new `FSB\|id\|timeout\|nops` frame, `OP\|op\|nargs` op specs follow | sequential under `fs_lock`, one RES | sequential in one worker, one RES | GUI-preview-shaped page reads in one round trip |
20
+ | `delete` / `move` FS ops | new op names in the FS frame vocabulary | yes | yes | guarded, mapped to `FsDeleteOutcome` / `FsMoveOutcome` |
21
+ | `patchText` | **no wire change** — host-side hunk application + a guarded `replace` write | n/a | n/a | hunks applied in-process via `@deepseek-ai/dsh-fs-patch` (a new peer dependency, the peer's own `applyHunks` — perfect error parity, `FS_PATCH_MISMATCH` free) |
22
+
23
+ `PROTOCOL_VERSION` becomes 7. `@deepseek-ai/dsh-fs-patch@0.2.2-rc.1` is added
24
+ to `peerDependencies` (the host imports `applyHunks` at runtime) and pinned in
25
+ `devDependencies`.
26
+
27
+ ## Negotiation rule (unchanged in spirit, sharpened)
28
+
29
+ The version in HELLO is the AGENT's floor for the frames it accepts: a v7
30
+ agent parses every v5/v6 frame forever. The HOST decides what it may send:
31
+
32
+ - a host sends `FSB` / `FSERR`-expecting frames only against `HELLO ≥ 7`;
33
+ - a v6 host (an older mirror) against a v7 agent sees `HELLO|…|6` never —
34
+ wait, the agent reports its own version. The rule that matters: **the host
35
+ gates new frames on `HELLO.version ≥ 7`, and the doctor's handshake reports
36
+ the negotiated pair**. A half-synced mirror (agent 6, host 7) therefore
37
+ degrades to v6 behaviour instead of desyncing — the digest check still
38
+ catches script drift, and version mismatch is the same recoverable state it
39
+ is today (rebuild once, then one-shot fallback).
40
+
41
+ Concurrency capability is negotiated separately, because the two backends
42
+ honestly differ:
43
+
44
+ - `conc` — the agent runs FS ops concurrently and may answer out of order
45
+ (the native agent already does: worker threads + `fs_lock` on mutations).
46
+ - `batch` — the agent accepts `FSB` (any backend; a batch is sequential
47
+ inside one worker, which is exactly why it is safe everywhere).
48
+ - The SHELL agent does **not** advertise `conc`: its read loop is serial and
49
+ POSIX sh gives no safe way to interleave 1 MB base64 lines on one pipe.
50
+ The host therefore serializes single FS frames against `batch`-only agents
51
+ and may pipeline them against `conc` agents. Batched reads — the actual
52
+ GUI-preview win — work on both backends.
53
+
54
+ ## Frame details
55
+
56
+ ### HELLO v7
57
+
58
+ ```
59
+ HELLO|wsl-agent|7|<sha256>|<pid>|<caps>
60
+ ```
61
+
62
+ `caps` is a comma-separated token list (`conc`, `batch`). Absent or empty
63
+ (a v6 peer, a fake) means no capabilities — the host behaves exactly as
64
+ today. The host-side parser keeps accepting 5-field HELLOs.
65
+
66
+ ### FSERR
67
+
68
+ ```
69
+ FSERR|<id>|<reason>|<b64 message>
70
+ ```
71
+
72
+ Emitted by both agents wherever an FS op fails (replacing the
73
+ `dsh-fs|reason|b64` first-stderr-line convention, which stays parsed as a
74
+ fallback). The host maps `reason` → `FsErrorCode` directly; `reason`
75
+ vocabulary is the existing `notfound notdir perm loop exists stale io
76
+ protocol` set.
77
+
78
+ ### FSB — batched FS
79
+
80
+ ```
81
+ FSB|<id>|<timeout seconds|0>|<n ops>
82
+ OP|<op>|<n args>
83
+ <b64 arg>…
84
+ OP|…
85
+ ```
86
+
87
+ One `ACK`, then ONE `RES|<id>|0|<b64 payload>` where the payload is the
88
+ concatenated per-op records:
89
+
90
+ ```
91
+ OK <i>|<b64 payload>
92
+ BAD <i>|<reason>|<b64 message>
93
+ ```
94
+
95
+ Ops run in order in ONE worker; a failed op records `BAD` and continues with
96
+ the next (a batch is a set of independent reads, not a transaction). Payloads
97
+ are the op's normal stdout (stat record, list records, read bytes). The
98
+ per-op exit is visible per record, so the host can retry one member
99
+ individually.
100
+
101
+ ### `delete` / `move` FS ops
102
+
103
+ ```
104
+ delete <path> <expected version|->
105
+ move <src path> <dst path> <dst mode|-> <dst publish replace|no-replace> <src expected version|-> <dst expected version|->
106
+ ```
107
+
108
+ Both reuse the write machinery's guards: version re-verification against a
109
+ fresh stat before the destructive step, staging + atomic publish for the
110
+ destination (`no-replace` = hard-link no-replace, `exists` refusal on a
111
+ concurrent creator), source removal after destination publication. A failed
112
+ source removal after a published destination is reported in the payload
113
+ (`sourceDeleted=false` plus the failure record) — the two-effect shape of the
114
+ peer's `FsMoveOutcome`, honestly preserved.
115
+
116
+ ## `patchText` — why there is no frame
117
+
118
+ Hunks are JSON-serializable, but hunk APPLICATION belongs to
119
+ `@deepseek-ai/dsh-fs-patch`'s `applyHunks` — the peer's own matcher, whose
120
+ `FS_PATCH_MISMATCH` dialect (`hunk at patch line N (stage) does not match`)
121
+ this plugin must reproduce verbatim to stay in parity. Reimplementing it in
122
+ awk or Rust is a permanent second source of truth for the subtleties
123
+ (anchors, EOF-candidate retries, trailing-sentinel handling). So v7 does it
124
+ the parity way:
125
+
126
+ 1. host `read`s the file (one round trip — batchable with the stat next to
127
+ it),
128
+ 2. applies `applyHunks` in-process (`FS_PATCH_MISMATCH` on failure, verbatim
129
+ dialect),
130
+ 3. issues a guarded `replace` write with the original version (one round
131
+ trip).
132
+
133
+ Two round trips, zero new wire semantics, zero second implementation. The
134
+ `dsh-fs-patch` peer floor is `0.2.2-rc.1`, the same line the plugin already
135
+ targets.
136
+
137
+ ## What breaks
138
+
139
+ Nothing for a correct peer. The compatibility matrix:
140
+
141
+ | host \ agent | 6 | 7 |
142
+ |---|---|---|
143
+ | v6 host (old mirror) | today | sends no v7 frames; HELLO extra field ignored |
144
+ | v7 host | no `FSB`/`FSERR`/`delete`/`move` (version-gated); parity gap stays | everything |
145
+
146
+ The host-side risk is bounded by the same rebuild-once-then-one-shot ladder
147
+ the executor already runs on any handshake mismatch.
148
+
149
+ ## Acceptance mapping
150
+
151
+ - design note — this file.
152
+ - batched-read benchmark — `test/probe/gui-preview-batch.sh`: stat+list+read page
153
+ shape, FSB vs five individual frames, both backends. Measured 2026-11-05 on
154
+ `ubuntu` (five runs each, two samples): native (the shipped default)
155
+ **4.4–7.4 → 1.9–2.1 ms/view (2.3–3.5x)**; shell backend 1.0–1.2x — its
156
+ per-op cost is coreutils forks, not transport round trips, so batching
157
+ there is a wash (the fork cost dominates either shape).
158
+ - lockstep — `agent/native/spike/test.mjs` gains v7 frames against the
159
+ native binary; `test/agent-fs-script.test.mjs` gains them against the
160
+ shell script; the doctor's handshake reports the negotiated version and
161
+ capabilities; `PROTOCOL_VERSION = 7`.
162
+ - substrate — `fsio-agent`/`fs-substrate` serve `patchText`/`deleteFile`/
163
+ `moveFile` for real; the composition tests flip from refusal to behaviour;
164
+ `docs/PEER-PARITY.md`'s declared-gap section becomes an implementation
165
+ record.
166
+
167
+ ## The SETENV verb: implemented, host-refused
168
+
169
+ `SETENV|id|key|value-b64` is a protocol-6 verb both agent implementations
170
+ still answer (`agent/wsl-agent.sh`, the native binary) and the suite still
171
+ tests, but no host code sends it: per-request environment rides the EXEC
172
+ frame's env block, and a global mutation would outlive the request that
173
+ made it. The verb is reserved — a host that wants idempotent
174
+ environment pre-warming can adopt it without a protocol bump. Until then
175
+ it is dormant surface, kept because removing it means a native rebuild.