opencode-agent-harness 0.0.0-stage → 0.3.14

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 (171) hide show
  1. package/CHANGELOG.md +217 -0
  2. package/LICENSE +21 -0
  3. package/README.md +636 -2
  4. package/bin/agent-harness.js +3182 -0
  5. package/contrib/global-plugins/README.md +49 -0
  6. package/contrib/global-plugins/harness-freshness.ts +355 -0
  7. package/package.json +86 -4
  8. package/templates/.agents/AGENTS.md +148 -0
  9. package/templates/.agents/features/INDEX.md +10 -0
  10. package/templates/.agents/handoffs/README.md +87 -0
  11. package/templates/.agents/memory/dev-loop.md +59 -0
  12. package/templates/.agents/memory/domain-map.md +29 -0
  13. package/templates/.agents/memory/environments.md +35 -0
  14. package/templates/.agents/memory/flow-map.md +23 -0
  15. package/templates/.agents/memory/history.md +7 -0
  16. package/templates/.agents/memory/locations.md +14 -0
  17. package/templates/.agents/memory/model-routing.md +261 -0
  18. package/templates/.agents/memory/orchestration.md +183 -0
  19. package/templates/.agents/memory/owned-files.md +19 -0
  20. package/templates/.agents/memory/product-principles.md +37 -0
  21. package/templates/.agents/memory/quality-parameters.md +37 -0
  22. package/templates/.agents/memory/stack-versions.md +35 -0
  23. package/templates/.agents/memory/test-design-parameters.md +36 -0
  24. package/templates/.agents/nightly.json +17 -0
  25. package/templates/.agents/qa-plans/README.md +70 -0
  26. package/templates/.agents/qa-runs/README.md +21 -0
  27. package/templates/.agents/reviews/README.md +47 -0
  28. package/templates/.agents/rules/00-operating.md +199 -0
  29. package/templates/.agents/skills/data-engineer/SKILL.md +31 -0
  30. package/templates/.agents/skills/devops-engineer/SKILL.md +50 -0
  31. package/templates/.agents/skills/diagnostics-expert/SKILL.md +44 -0
  32. package/templates/.agents/skills/docs/SKILL.md +140 -0
  33. package/templates/.agents/skills/features/SKILL.md +187 -0
  34. package/templates/.agents/skills/frontend-engineer/SKILL.md +81 -0
  35. package/templates/.agents/skills/handoff/SKILL.md +85 -0
  36. package/templates/.agents/skills/mobile-engineer/SKILL.md +101 -0
  37. package/templates/.agents/skills/planner/SKILL.md +112 -0
  38. package/templates/.agents/skills/product-manager/SKILL.md +115 -0
  39. package/templates/.agents/skills/qa-architect/SKILL.md +224 -0
  40. package/templates/.agents/skills/qa-runner/SKILL.md +64 -0
  41. package/templates/.agents/skills/security-engineer/SKILL.md +53 -0
  42. package/templates/.agents/skills/system-architect/SKILL.md +42 -0
  43. package/templates/.agents/skills/ui-designer/SKILL.md +71 -0
  44. package/templates/.github/workflows/ci.yml +148 -0
  45. package/templates/.github/workflows/deploy-uat.yml +95 -0
  46. package/templates/.github/workflows/nightly.yml +83 -0
  47. package/templates/.github/workflows/uat-health.yml +41 -0
  48. package/templates/.husky/pre-push +23 -0
  49. package/templates/.ignore +28 -0
  50. package/templates/.opencode/agents/data-engineer.md +37 -0
  51. package/templates/.opencode/agents/devops-engineer.md +37 -0
  52. package/templates/.opencode/agents/diagnostics-expert.md +37 -0
  53. package/templates/.opencode/agents/docs.md +37 -0
  54. package/templates/.opencode/agents/features.md +21 -0
  55. package/templates/.opencode/agents/frontend-engineer.md +37 -0
  56. package/templates/.opencode/agents/handoff.md +21 -0
  57. package/templates/.opencode/agents/mobile-engineer.md +37 -0
  58. package/templates/.opencode/agents/planner.md +34 -0
  59. package/templates/.opencode/agents/product-manager.md +29 -0
  60. package/templates/.opencode/agents/qa-architect.md +27 -0
  61. package/templates/.opencode/agents/qa-runner.md +17 -0
  62. package/templates/.opencode/agents/security-engineer.md +37 -0
  63. package/templates/.opencode/agents/system-architect.md +37 -0
  64. package/templates/.opencode/agents/ui-designer.md +37 -0
  65. package/templates/.opencode/commands/board.md +15 -0
  66. package/templates/.opencode/commands/checkpoint.md +25 -0
  67. package/templates/.opencode/commands/feature.md +56 -0
  68. package/templates/.opencode/commands/finish.md +36 -0
  69. package/templates/.opencode/commands/next.md +49 -0
  70. package/templates/.opencode/commands/report.md +55 -0
  71. package/templates/.opencode/commands/resume.md +39 -0
  72. package/templates/.opencode/commands/switch-model.md +34 -0
  73. package/templates/.opencode/commands/tidy.md +49 -0
  74. package/templates/.opencode/plugins/backlog-notify.ts +57 -0
  75. package/templates/.opencode/plugins/backlog.ts +272 -0
  76. package/templates/.opencode/plugins/checkpoint.ts +160 -0
  77. package/templates/.opencode/plugins/context-guard.ts +1164 -0
  78. package/templates/.opencode/plugins/diff.ts +85 -0
  79. package/templates/.opencode/plugins/gates.ts +71 -0
  80. package/templates/.opencode/plugins/job-notify.ts +241 -0
  81. package/templates/.opencode/plugins/orient.ts +67 -0
  82. package/templates/.opencode/plugins/session.ts +89 -0
  83. package/templates/.opencode/plugins/wait.ts +101 -0
  84. package/templates/.opencode/plugins/where.ts +70 -0
  85. package/templates/.opencode/skills/bounded-explore/SKILL.md +38 -0
  86. package/templates/.opencode/skills/dev-loop/SKILL.md +87 -0
  87. package/templates/.vercelignore +16 -0
  88. package/templates/AGENTS.md +27 -0
  89. package/templates/BACKLOG.md +95 -0
  90. package/templates/docs/ci-cd-pipeline.md +81 -0
  91. package/templates/docs/gates.md +128 -0
  92. package/templates/docs/mcp-integrations.md +97 -0
  93. package/templates/docs/mobile-testing.md +555 -0
  94. package/templates/docs/nightly-testing.md +106 -0
  95. package/templates/docs/wsl-optimizations.md +290 -0
  96. package/templates/opencode.json +188 -0
  97. package/templates/scripts/agent-report.js +541 -0
  98. package/templates/scripts/agent-search.sh +117 -0
  99. package/templates/scripts/backlog.js +1924 -0
  100. package/templates/scripts/board-service.sh +214 -0
  101. package/templates/scripts/checkpoint.js +290 -0
  102. package/templates/scripts/finalize.js +416 -0
  103. package/templates/scripts/nightly.js +913 -0
  104. package/templates/scripts/orient.js +514 -0
  105. package/templates/scripts/qa/board-client.js +323 -0
  106. package/templates/scripts/qa/board-lib.js +96 -0
  107. package/templates/scripts/qa/board-sync.js +499 -0
  108. package/templates/scripts/qa/capacity-lib.js +916 -0
  109. package/templates/scripts/qa/check-backlog.js +434 -0
  110. package/templates/scripts/qa/check-capacity.js +85 -0
  111. package/templates/scripts/qa/check-ci-policy.js +164 -0
  112. package/templates/scripts/qa/check-concurrency.js +344 -0
  113. package/templates/scripts/qa/check-dispatch-config.js +296 -0
  114. package/templates/scripts/qa/check-docs.js +288 -0
  115. package/templates/scripts/qa/check-integrate.js +98 -0
  116. package/templates/scripts/qa/check-linger.js +160 -0
  117. package/templates/scripts/qa/check-prefix-stability.js +187 -0
  118. package/templates/scripts/qa/check-product.js +177 -0
  119. package/templates/scripts/qa/check-qa-plan.js +378 -0
  120. package/templates/scripts/qa/check-qa-scripts.js +85 -0
  121. package/templates/scripts/qa/check-quality.js +249 -0
  122. package/templates/scripts/qa/check-test-wiring.js +253 -0
  123. package/templates/scripts/qa/check-worktrees.js +93 -0
  124. package/templates/scripts/qa/ci-triage.js +346 -0
  125. package/templates/scripts/qa/ci-usage-lib.js +185 -0
  126. package/templates/scripts/qa/ci-wait-lib.js +294 -0
  127. package/templates/scripts/qa/command-classifier.js +552 -0
  128. package/templates/scripts/qa/context-guard-core.js +1618 -0
  129. package/templates/scripts/qa/diff.js +174 -0
  130. package/templates/scripts/qa/feature-scaffold.js +224 -0
  131. package/templates/scripts/qa/fixtures/backlog.md +104 -0
  132. package/templates/scripts/qa/governance.js +174 -0
  133. package/templates/scripts/qa/hash-file.js +32 -0
  134. package/templates/scripts/qa/lease-lib.js +263 -0
  135. package/templates/scripts/qa/merge-backlog.js +324 -0
  136. package/templates/scripts/qa/mobile-e2e/analyze-timings.js +300 -0
  137. package/templates/scripts/qa/mobile-e2e/apk-cache.js +420 -0
  138. package/templates/scripts/qa/mobile-e2e/backend-build-cache.js +203 -0
  139. package/templates/scripts/qa/mobile-e2e/boot-emulator.sh +432 -0
  140. package/templates/scripts/qa/mobile-e2e/build-apk.sh +210 -0
  141. package/templates/scripts/qa/mobile-e2e/check-flows.js +568 -0
  142. package/templates/scripts/qa/mobile-e2e/config.yaml +18 -0
  143. package/templates/scripts/qa/mobile-e2e/device-down.sh +88 -0
  144. package/templates/scripts/qa/mobile-e2e/device-up.sh +258 -0
  145. package/templates/scripts/qa/mobile-e2e/ios-simulator-down.sh +72 -0
  146. package/templates/scripts/qa/mobile-e2e/ios-simulator-up.sh +153 -0
  147. package/templates/scripts/qa/mobile-e2e/job-events.js +181 -0
  148. package/templates/scripts/qa/mobile-e2e/prewarm.sh +56 -0
  149. package/templates/scripts/qa/mobile-e2e/run-detached.sh +222 -0
  150. package/templates/scripts/qa/mobile-e2e/run-maestro.js +1543 -0
  151. package/templates/scripts/qa/mobile-e2e/ui-dump.js +327 -0
  152. package/templates/scripts/qa/models.allowlist.txt +32 -0
  153. package/templates/scripts/qa/provider-stats.js +878 -0
  154. package/templates/scripts/qa/run-clean.js +83 -0
  155. package/templates/scripts/qa/run-gates.js +213 -0
  156. package/templates/scripts/qa/run-loop.js +220 -0
  157. package/templates/scripts/qa/session-recon.js +512 -0
  158. package/templates/scripts/qa/store-lib.js +324 -0
  159. package/templates/scripts/qa/tool-lib.js +134 -0
  160. package/templates/scripts/qa/where.js +214 -0
  161. package/templates/scripts/qa/worktree-hygiene.js +217 -0
  162. package/templates/scripts/qa/wslconfig-merge.js +157 -0
  163. package/templates/scripts/serve.js +1328 -0
  164. package/templates/scripts/setup-nightly.sh +185 -0
  165. package/templates/scripts/setup-tmpfs.sh +374 -0
  166. package/templates/scripts/ship.js +586 -0
  167. package/templates/scripts/switch-reasoning-model.ps1 +101 -0
  168. package/templates/scripts/switch-reasoning-model.sh +104 -0
  169. package/templates/scripts/wait-for.js +342 -0
  170. package/templates/scripts/wsl-gpu.sh +180 -0
  171. package/templates/scripts/wsl-tune.sh +319 -0
@@ -0,0 +1,319 @@
1
+ #!/usr/bin/env bash
2
+ # wsl-tune.sh — one idempotent bootstrap for WSL2 memory/CPU pressure.
3
+ #
4
+ # The 2026-09-29 freeze was a capacity exhaustion: WSL2 is one Linux VM, and an
5
+ # emulator + agents + `next dev` filled RAM, thrashed swap, and starved the
6
+ # host<->VM vsock relay so every terminal froze at once. The capacity envelope
7
+ # (scripts/qa/capacity-lib.js) serializes heavy tiers; this script tunes the VM
8
+ # itself so a spike degrades instead of freezing:
9
+ #
10
+ # - sysctls: low swappiness, small min_free_kbytes floor, bounded dirty ratios
11
+ # - a systemd user.slice resource ceiling (MemoryHigh/MemoryMax/CPUQuota), so a
12
+ # runaway is throttled/OOM-killed inside its cgroup, not the whole VM
13
+ # - a SOFT init.scope MemoryHigh backstop (agent-wsl-backstop.service) for the
14
+ # cgroup agents/emulators actually run in — WSL puts the wsl.exe session in
15
+ # init.scope, NOT user.slice, so the ceiling above never binds the workload
16
+ # - `.wslconfig` keys (processors, swap, vmIdleTimeout, reclaim) merged safely
17
+ # via scripts/qa/wslconfig-merge.js
18
+ #
19
+ # Usage:
20
+ # bash scripts/wsl-tune.sh status # what is applied (default)
21
+ # bash scripts/wsl-tune.sh apply # apply everything (auto-sudo)
22
+ # bash scripts/wsl-tune.sh wslconfig # merge .wslconfig keys only
23
+ # bash scripts/wsl-tune.sh down # remove the in-WSL tunables
24
+ # bash scripts/wsl-tune.sh apply --dry-run # print, change nothing
25
+ #
26
+ # No-op outside WSL (override with AGENT_WSL_TUNE_FORCE=1). Env overrides:
27
+ # AGENT_WSL_TUNE_SYSCTL, AGENT_WSL_TUNE_SLICE_DIR, AGENT_WSL_CONFIG,
28
+ # AGENT_WSL_TUNE_DRY_RUN, AGENT_WSL_TUNE_PROCESSORS, AGENT_WSL_TUNE_SWAP,
29
+ # AGENT_WSL_TUNE_IDLE_MS, AGENT_WSL_TUNE_NODE (node binary for the merge)
30
+ set -euo pipefail
31
+
32
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
33
+ MERGERS="$SCRIPT_DIR/qa/wslconfig-merge.js"
34
+
35
+ SYSCTL_FILE="${AGENT_WSL_TUNE_SYSCTL:-/etc/sysctl.d/99-agent-wsl.conf}"
36
+ SLICE_DIR="${AGENT_WSL_TUNE_SLICE_DIR:-/etc/systemd/system/user.slice.d}"
37
+ SLICE_FILE="$SLICE_DIR/90-agent-cap.conf"
38
+ UNIT_DIR="${AGENT_WSL_TUNE_UNIT_DIR:-/etc/systemd/system}"
39
+ BACKSTOP_UNIT="$UNIT_DIR/agent-wsl-backstop.service"
40
+ BACKSTOP="${AGENT_WSL_TUNE_BACKSTOP:-8704M}"
41
+ DRY_RUN="${AGENT_WSL_TUNE_DRY_RUN:-0}"
42
+
43
+ log() { printf '%s\n' "$*"; }
44
+ warn() { printf '%s\n' "$*" >&2; }
45
+
46
+ is_wsl() {
47
+ [ "${AGENT_WSL_TUNE_FORCE:-0}" = "1" ] && return 0
48
+ [ -r /proc/version ] && grep -qiE 'microsoft|wsl' /proc/version
49
+ }
50
+
51
+ find_config() {
52
+ if [ -n "${AGENT_WSL_CONFIG:-}" ]; then printf '%s' "$AGENT_WSL_CONFIG"; return; fi
53
+ local f
54
+ for f in /mnt/c/Users/*/.wslconfig; do
55
+ [ -f "$f" ] && { printf '%s' "$f"; return 0; }
56
+ done
57
+ return 0
58
+ }
59
+
60
+ powershell_exe() {
61
+ if command -v powershell.exe >/dev/null 2>&1; then command -v powershell.exe; return 0; fi
62
+ local p="/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"
63
+ [ -x "$p" ] && { printf '%s' "$p"; return 0; }
64
+ return 1
65
+ }
66
+
67
+ host_cpus() {
68
+ local exe v
69
+ exe="$(powershell_exe)" || return 1
70
+ v="$("$exe" -NoProfile -Command "(Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors" 2>/dev/null | tr -d '\r ')"
71
+ case "$v" in ''|*[!0-9]*) return 1;; *) printf '%s' "$v";; esac
72
+ }
73
+
74
+ target_processors() {
75
+ if [ -n "${AGENT_WSL_TUNE_PROCESSORS:-}" ]; then printf '%s' "$AGENT_WSL_TUNE_PROCESSORS"; return 0; fi
76
+ local hc
77
+ hc="$(host_cpus)" || return 0
78
+ case "$hc" in ''|*[!0-9]*) return 0;; esac
79
+ if [ "$hc" -gt 6 ]; then printf '%s' "$((hc - 4))"; else printf '%s' "$((hc > 2 ? hc - 1 : 2))"; fi
80
+ }
81
+
82
+ SYSCTL_BODY="# Managed by opencode-agent-harness (scripts/wsl-tune.sh). Idempotent; 'wsl-tune.sh down' removes it.
83
+ vm.swappiness=10
84
+ vm.vfs_cache_pressure=50
85
+ vm.min_free_kbytes=262144
86
+ vm.dirty_background_ratio=5
87
+ vm.dirty_ratio=10"
88
+
89
+ slice_body() {
90
+ cat <<EOF
91
+ # Managed by opencode-agent-harness (scripts/wsl-tune.sh). Idempotent; 'wsl-tune.sh down' removes it.
92
+ [Slice]
93
+ MemoryHigh=8G
94
+ MemoryMax=9G
95
+ CPUQuota=800%
96
+ EOF
97
+ }
98
+
99
+ # The user.slice ceiling above never binds the real workload: WSL's wsl.exe
100
+ # session — and so every agent, emulator, and dev server — is in init.scope, a
101
+ # sibling of user.slice, not a child. This oneshot runs at boot and puts a SOFT
102
+ # MemoryHigh on init.scope. Soft is deliberate: MemoryHigh reclaims/throttles as
103
+ # the line is approached and never OOM-kills, so a spike degrades instead of
104
+ # thrash-freezing every terminal. Heavy tiers also get a per-unit cap from
105
+ # serve.js; this backstop covers the agents themselves.
106
+ backstop_body() {
107
+ cat <<EOF
108
+ # Managed by opencode-agent-harness (scripts/wsl-tune.sh). Idempotent; 'wsl-tune.sh down' removes it.
109
+ [Unit]
110
+ Description=agent-harness WSL memory backstop (soft MemoryHigh on init.scope)
111
+ After=systemd-user-sessions.service
112
+
113
+ [Service]
114
+ Type=oneshot
115
+ RemainAfterExit=yes
116
+ ExecStart=/bin/sh -c 'systemctl set-property init.scope MemoryHigh=${BACKSTOP}'
117
+
118
+ [Install]
119
+ WantedBy=multi-user.target
120
+ EOF
121
+ }
122
+
123
+ # `sudo` resets PATH to the secure_path, so a user-local node (nvm, volta,
124
+ # fnm, ~/.local/bin) is invisible to `sudo bash scripts/wsl-tune.sh apply`.
125
+ # Resolve it from the invoking user's installs instead of assuming PATH.
126
+ node_bin() {
127
+ if [ -n "${AGENT_WSL_TUNE_NODE:-}" ]; then
128
+ if [ -x "$AGENT_WSL_TUNE_NODE" ]; then printf '%s' "$AGENT_WSL_TUNE_NODE"; return 0; fi
129
+ warn "wsl-tune: AGENT_WSL_TUNE_NODE=$AGENT_WSL_TUNE_NODE is not executable"
130
+ return 1
131
+ fi
132
+ if command -v node >/dev/null 2>&1; then command -v node; return 0; fi
133
+ local home="" cand
134
+ if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then
135
+ home="$(getent passwd "$SUDO_USER" 2>/dev/null | cut -d: -f6 || true)"
136
+ fi
137
+ [ -n "$home" ] || home="${HOME:-}"
138
+ for cand in \
139
+ "$home/.local/bin/node" \
140
+ "$home/.volta/bin/node" \
141
+ "$home/.asdf/shims/node" \
142
+ /usr/local/bin/node /usr/bin/node /snap/bin/node; do
143
+ [ -x "$cand" ] && { printf '%s' "$cand"; return 0; }
144
+ done
145
+ # nvm/fnm keep versioned installs; take the newest.
146
+ cand="$(ls -1d "$home"/.nvm/versions/node/*/bin/node \
147
+ "$home"/.fnm/node-versions/*/installation/bin/node 2>/dev/null | sort -V | tail -n1 || true)"
148
+ [ -n "$cand" ] && [ -x "$cand" ] && { printf '%s' "$cand"; return 0; }
149
+ return 1
150
+ }
151
+
152
+ # The merge only touches the Windows-side .wslconfig, so under sudo run it as
153
+ # the invoking user: that is whose node we resolved, and the backup keeps the
154
+ # user's ownership on the drvfs mount.
155
+ run_merger() {
156
+ local nb="$1"; shift
157
+ if [ "$(id -u)" -eq 0 ] && [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ] \
158
+ && command -v sudo >/dev/null 2>&1; then
159
+ sudo -u "$SUDO_USER" -H "$nb" "$MERGERS" "$@"
160
+ else
161
+ "$nb" "$MERGERS" "$@"
162
+ fi
163
+ }
164
+
165
+ apply_wslconfig() {
166
+ local cfg sets=() nb
167
+ cfg="$(find_config)"
168
+ if [ -z "$cfg" ]; then
169
+ warn "wsl-tune: no .wslconfig found (set AGENT_WSL_CONFIG to target one) — skipping host tuning"
170
+ return 0
171
+ fi
172
+ local proc swap idle
173
+ proc="$(target_processors || true)"
174
+ swap="${AGENT_WSL_TUNE_SWAP:-4GB}"
175
+ idle="${AGENT_WSL_TUNE_IDLE_MS:-7200000}"
176
+ [ -n "$proc" ] && sets+=("--set" "wsl2.processors=$proc")
177
+ sets+=("--set" "wsl2.swap=$swap")
178
+ sets+=("--set" "wsl2.vmIdleTimeout=$idle")
179
+ sets+=("--set" "experimental.autoMemoryReclaim=gradual")
180
+ sets+=("--set" "experimental.sparseVhd=true")
181
+
182
+ if ! nb="$(node_bin)"; then
183
+ warn "wsl-tune: node not found (sudo resets PATH, so a user-local node is invisible)."
184
+ warn " merge .wslconfig as your user: npm run wsl-tune -- wslconfig"
185
+ warn " (or pass AGENT_WSL_TUNE_NODE=/path/to/node)"
186
+ return 1
187
+ fi
188
+
189
+ if [ "$DRY_RUN" = "1" ]; then
190
+ log "wsl-tune: would merge into $cfg:"
191
+ run_merger "$nb" --file "$cfg" "${sets[@]}" | sed 's/^/ /'
192
+ return 0
193
+ fi
194
+ run_merger "$nb" --file "$cfg" --write --backup "${sets[@]}"
195
+ }
196
+
197
+ do_status() {
198
+ log "wsl-tune status"
199
+ if ! is_wsl; then log " not WSL — tuning is a no-op here"; return 0; fi
200
+ if [ -f "$SYSCTL_FILE" ]; then log " sysctl: $SYSCTL_FILE present"; else log " sysctl: not applied ($SYSCTL_FILE missing)"; fi
201
+ if [ -r /proc/sys/vm/swappiness ]; then log " vm.swappiness=$(cat /proc/sys/vm/swappiness)"; fi
202
+ if [ -f "$SLICE_FILE" ]; then log " slice: $SLICE_FILE present"; else log " slice: not applied ($SLICE_FILE missing)"; fi
203
+ if command -v systemctl >/dev/null 2>&1; then
204
+ local mm cq
205
+ mm="$(systemctl show user.slice -p MemoryMax --value 2>/dev/null || true)"
206
+ cq="$(systemctl show user.slice -p CPUQuotaPerSecUSec --value 2>/dev/null || true)"
207
+ [ -n "$mm" ] && log " user.slice MemoryMax=$mm CPUQuota=$cq"
208
+ fi
209
+ if [ -f "$BACKSTOP_UNIT" ]; then log " backstop: $BACKSTOP_UNIT present"; else log " backstop: not applied ($BACKSTOP_UNIT missing)"; fi
210
+ if command -v systemctl >/dev/null 2>&1; then
211
+ local ih
212
+ ih="$(systemctl show init.scope -p MemoryHigh --value 2>/dev/null || true)"
213
+ [ -n "$ih" ] && log " init.scope MemoryHigh=$ih (the cgroup agents/emulators actually run in)"
214
+ fi
215
+ local cfg; cfg="$(find_config)"
216
+ if [ -n "$cfg" ] && [ -f "$cfg" ]; then
217
+ log " wslconfig: $cfg"
218
+ grep -E '^[[:space:]]*(processors|memory|swap|vmIdleTimeout|autoMemoryReclaim|sparseVhd)[[:space:]]*=' "$cfg" | sed 's/^/ /' || true
219
+ else
220
+ log " wslconfig: not found"
221
+ fi
222
+ if [ -r /proc/pressure/memory ]; then log " PSI mem: $(grep -m1 '^some' /proc/pressure/memory)"; fi
223
+ log ""
224
+ log " Apply/refresh with: sudo bash scripts/wsl-tune.sh apply"
225
+ log " (.wslconfig changes need 'wsl --shutdown' from Windows PowerShell to take effect.)"
226
+ }
227
+
228
+ do_apply() {
229
+ if ! is_wsl; then log "wsl-tune: not WSL — no-op."; return 0; fi
230
+ if [ "$DRY_RUN" = "1" ]; then
231
+ log "wsl-tune: would write $SYSCTL_FILE:"; printf '%s\n' "$SYSCTL_BODY" | sed 's/^/ /'
232
+ log "wsl-tune: would write $SLICE_FILE:"; slice_body | sed 's/^/ /'
233
+ log "wsl-tune: would write $BACKSTOP_UNIT:"; backstop_body | sed 's/^/ /'
234
+ log "wsl-tune: would reload sysctl + systemd, enable agent-wsl-backstop.service"
235
+ apply_wslconfig
236
+ return 0
237
+ fi
238
+ if [ "$(id -u)" -ne 0 ]; then
239
+ if command -v sudo >/dev/null 2>&1 && sudo -n true 2>/dev/null; then
240
+ exec sudo -E bash "$0" apply "$@"
241
+ fi
242
+ warn "wsl-tune: in-WSL tunables need root — re-run: sudo bash scripts/wsl-tune.sh apply"
243
+ local rc=0
244
+ apply_wslconfig || rc=$?
245
+ return "$rc"
246
+ fi
247
+ printf '%s\n' "$SYSCTL_BODY" > "$SYSCTL_FILE"
248
+ log "wsl-tune: wrote $SYSCTL_FILE"
249
+ sysctl --system >/dev/null 2>&1 || sysctl -p "$SYSCTL_FILE" >/dev/null 2>&1 || true
250
+ log "wsl-tune: applied sysctls"
251
+ mkdir -p "$SLICE_DIR"
252
+ slice_body > "$SLICE_FILE"
253
+ log "wsl-tune: wrote $SLICE_FILE"
254
+ if command -v systemctl >/dev/null 2>&1; then
255
+ systemctl daemon-reload 2>/dev/null || true
256
+ systemctl set-property --runtime user.slice MemoryHigh=8G MemoryMax=9G CPUQuota=800% 2>/dev/null || true
257
+ log "wsl-tune: applied user.slice ceiling"
258
+ fi
259
+ printf '%s\n' "$(backstop_body)" > "$BACKSTOP_UNIT"
260
+ log "wsl-tune: wrote $BACKSTOP_UNIT"
261
+ if command -v systemctl >/dev/null 2>&1; then
262
+ systemctl daemon-reload 2>/dev/null || true
263
+ if systemctl enable --now agent-wsl-backstop.service >/dev/null 2>&1; then
264
+ log "wsl-tune: applied soft init.scope backstop (MemoryHigh=$BACKSTOP)"
265
+ else
266
+ warn "wsl-tune: could not start agent-wsl-backstop.service — run it after reboot, or apply manually:"
267
+ warn " systemctl set-property init.scope MemoryHigh=$BACKSTOP"
268
+ fi
269
+ fi
270
+ local merge_rc=0
271
+ apply_wslconfig || merge_rc=$?
272
+ log ""
273
+ if [ "$merge_rc" -eq 0 ]; then
274
+ log "wsl-tune: in-WSL tunables applied; .wslconfig merged."
275
+ log "wsl-tune: done. MANUAL STEP — from Windows PowerShell run: wsl --shutdown"
276
+ log " (only needed for the .wslconfig changes: processors/swap/vmIdleTimeout.)"
277
+ else
278
+ warn "wsl-tune: in-WSL tunables applied, but the .wslconfig merge did not complete (see above)."
279
+ warn " Finish it as your user: npm run wsl-tune -- wslconfig"
280
+ fi
281
+ return "$merge_rc"
282
+ }
283
+
284
+ do_down() {
285
+ if [ "$(id -u)" -ne 0 ]; then
286
+ warn "wsl-tune down needs root — re-run: sudo bash scripts/wsl-tune.sh down"
287
+ return 1
288
+ fi
289
+ rm -f "$SYSCTL_FILE" && log "wsl-tune: removed $SYSCTL_FILE" || true
290
+ rm -f "$SLICE_FILE" && log "wsl-tune: removed $SLICE_FILE" || true
291
+ if command -v systemctl >/dev/null 2>&1; then
292
+ systemctl disable --now agent-wsl-backstop.service >/dev/null 2>&1 || true
293
+ systemctl set-property init.scope MemoryHigh=infinity >/dev/null 2>&1 || true
294
+ fi
295
+ rm -f "$BACKSTOP_UNIT" && log "wsl-tune: removed $BACKSTOP_UNIT" || true
296
+ sysctl --system >/dev/null 2>&1 || true
297
+ command -v systemctl >/dev/null 2>&1 && systemctl daemon-reload 2>/dev/null || true
298
+ log "wsl-tune: .wslconfig left untouched (restore a .bak-* file if you want to revert it)."
299
+ }
300
+
301
+ main() {
302
+ local sub="status"
303
+ local args=()
304
+ for a in "$@"; do
305
+ case "$a" in
306
+ status|apply|wslconfig|down) sub="$a" ;;
307
+ --dry-run) DRY_RUN=1 ;;
308
+ *) args+=("$a") ;;
309
+ esac
310
+ done
311
+ case "$sub" in
312
+ status) do_status ;;
313
+ apply) do_apply "${args[@]}" ;;
314
+ wslconfig) apply_wslconfig ;;
315
+ down) do_down ;;
316
+ esac
317
+ }
318
+
319
+ main "$@"