@dreamtree-org/harness-device 0.0.0-stage → 0.1.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 (45) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE +21 -0
  3. package/README.md +255 -2
  4. package/bin/harness-device.js +8 -0
  5. package/package.json +62 -4
  6. package/sh/bin/hd-chromium +51 -0
  7. package/sh/bin/hd-heal +31 -0
  8. package/sh/bin/hd-lib.sh +74 -0
  9. package/sh/bin/hd-ngrok +7 -0
  10. package/sh/bin/hd-proc-loop +23 -0
  11. package/sh/bin/hd-pw-live +33 -0
  12. package/sh/bin/hd-rc-loop +42 -0
  13. package/sh/bin/hd-run +6 -0
  14. package/sh/bin/hd-serve-claude +23 -0
  15. package/sh/bin/hd-tmux-layout +69 -0
  16. package/sh/bin/hd-tmux-supervise +11 -0
  17. package/sh/bin/hd-with-secrets +8 -0
  18. package/sh/setup/darwin.sh +75 -0
  19. package/sh/setup/linux.sh +118 -0
  20. package/src/capabilities.js +237 -0
  21. package/src/cli.js +382 -0
  22. package/src/config.js +239 -0
  23. package/src/context.js +54 -0
  24. package/src/device.js +127 -0
  25. package/src/doctor.js +40 -0
  26. package/src/fleet.js +94 -0
  27. package/src/install.js +91 -0
  28. package/src/mcp/server.js +115 -0
  29. package/src/ngrok.js +62 -0
  30. package/src/platform/darwin.js +190 -0
  31. package/src/platform/index.js +12 -0
  32. package/src/platform/linux.js +142 -0
  33. package/src/providers/claude.js +212 -0
  34. package/src/state.js +197 -0
  35. package/src/util.js +103 -0
  36. package/templates/systemd/harness-chromium.service +17 -0
  37. package/templates/systemd/harness-heal.service +7 -0
  38. package/templates/systemd/harness-heal.timer +10 -0
  39. package/templates/systemd/harness-ngrok.service +13 -0
  40. package/templates/systemd/harness-novnc.service +14 -0
  41. package/templates/systemd/harness-tmux.service +15 -0
  42. package/templates/systemd/harness-ttyd.service +14 -0
  43. package/templates/systemd/harness-vnc.service +14 -0
  44. package/templates/systemd/harness-xvfb.service +13 -0
  45. package/templates/systemd/harness.target +6 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+
5
+ First release.
6
+
7
+ - **Persistent agent sessions.** Each project slot is pinned to one Claude conversation ID and resumed with `claude --remote-control <device>-<project> --resume <id>` after any crash, restart or downtime. Slots: `session new|reset|adopt|rm`. Failing slots are flagged, never silently replaced. `SERVE_MODE=server` keeps plain `claude remote-control` (on-demand sessions, restored only within about 4 hours).
8
+ - **Registered processes** (`proc add|rm|list`) restart on crash and come back after a reboot.
9
+ - **Linux and macOS.** systemd user units with linger on Linux; launchd LaunchAgents on macOS (automatic login needed for unattended recovery; `doctor` checks). System packages come from apt (one sudo) or Homebrew (no sudo).
10
+ - **Capabilities:** shell, system, display (Linux Xvfb + noVNC; macOS screen), browser (persistent Chromium, tabs restored), playwright (MCP on the device browser, `hd-pw-live` headed runs and UI mode), sqlite, docker (Linux Engine; macOS colima), dokploy (your existing deployment through its API and the Dokploy MCP; nothing installed), graphify, ngrok, ollama.
11
+ - **Claude provider:** user-scope MCP registration, a generated `harness-device` skill, a CLAUDE.md pointer, a curated allow/deny list merged into `~/.claude/settings.json` (only its own entries), and transcript retention raised to `TRANSCRIPT_DAYS`.
12
+ - **`harness-device mcp`:** device_status, sessions, proc_add/list/remove, logs, viewers, disk_usage, restart_service, browser_restart, screenshot, run_tests_live, playwright_ui, docker_summary, ngrok_status.
13
+ - **Self-heal** every 2 minutes; `doctor` with fixes; `fleet add|status|run` across devices over ssh.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Dreamtree-Org
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,256 @@
1
- # Temporary Holding Version
1
+ # @dreamtree-org/harness-device
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Turn a Linux server or a Mac into a **device**: a dev environment that a remote AI agent works in, and that survives reboots. You talk to the agent from your phone or claude.ai. It works on the device with a real shell, a browser you can watch, Playwright, Docker, SQLite, graphify and your Dokploy, and after a crash or restart it carries on **in the same conversation**.
4
+
5
+ It starts with **Claude Code Remote Control**. Other agents (Gemini, Codex, OpenCode) plug into the same device later, through providers.
6
+
7
+ [![npm version](https://badge.fury.io/js/@dreamtree-org%2Fharness-device.svg)](https://www.npmjs.com/package/@dreamtree-org/harness-device)
8
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
9
+ [![Website](https://img.shields.io/badge/by-Dreamtree--Org-0ea5e9)](https://www.dreamtreeglobal.com)
10
+
11
+ ## About the Company
12
+
13
+ @dreamtree-org/harness-device is built and maintained under **[Dreamtree-Org](https://www.dreamtreeglobal.com)** — a team crafting thoughtful, developer-first software for the modern web. We believe great products start with a great foundation, and that's exactly what this package aims to be: a polished, open, and dependable home for remote agentic development, for teams everywhere.
14
+
15
+ 🌐 **Website:** [www.dreamtreeglobal.com](https://www.dreamtreeglobal.com)
16
+
17
+ ## Author & Maintainer
18
+
19
+ **Partha Preetham Krishna** — [Dreamtree-Org](https://www.dreamtreeglobal.com)
20
+ 📧 [preetham.krishna.dev@gmail.com](mailto:preetham.krishna.dev@gmail.com)
21
+
22
+ Have an idea, a bug, or just want to say hi? Reach out anytime — feedback from developers like you is what keeps this package growing.
23
+
24
+ ## 💚 Sponsor & Support
25
+
26
+ @dreamtree-org/harness-device is **free and open source**, built in the open and powered entirely by passion and late nights. Every feature you rely on — every resumed conversation, self-healed service, and restored browser tab — represents hours of careful craft so that _your_ agents keep working while you are away from the keyboard.
27
+
28
+ If this package has saved you time, sparked an idea, or spared you a lost session after a reboot, please consider supporting its continued development. **Your sponsorship keeps the updates coming, the bugs squashed, and the docs sharp — for the whole community.** Even the smallest contribution is a huge encouragement. 🙏
29
+
30
+ ### ☕ Make a one-tap donation via UPI
31
+
32
+ **📷 Scan the QR with any UPI app** (Google Pay, PhonePe, Paytm, BHIM, …) to pay instantly:
33
+
34
+ <p>
35
+ <img src="https://unpkg.com/@dreamtree-org/twreact-ui@latest/doc/assets/upi-qr.png" alt="Scan to pay Dreamtree-Org via UPI" width="220" height="220" />
36
+ </p>
37
+
38
+ > **UPI ID:** `dhrugantha.llp@kotak` &nbsp;•&nbsp; **Payee:** Dreamtree-Org
39
+ >
40
+ > 📱 On mobile, you can also tap the button below — it opens your UPI app directly. On desktop, scan the QR above or copy the UPI ID into your payment app.
41
+
42
+ [![Pay via UPI](https://img.shields.io/badge/Pay-via%20UPI-22c55e?style=for-the-badge&logo=googlepay&logoColor=white)](upi://pay?pa=dhrugantha.llp@kotak&pn=Dreamtree-Org&cu=INR&tn=Support%20%40dreamtree-org%2Fharness-device)
43
+
44
+ ### 🤝 Become a Sponsor
45
+
46
+ Want to back the project long-term or as a company? You can sponsor the package directly:
47
+
48
+ - 📦 **npm:** [`@dreamtree-org/harness-device`](https://www.npmjs.com/package/@dreamtree-org/harness-device) — star, share, and sponsor the package
49
+ - ✉️ **Corporate sponsorships & partnerships:** [preetham.krishna.dev@gmail.com](mailto:preetham.krishna.dev@gmail.com)
50
+
51
+ Every star ⭐, share, and contribution helps more than you know. Thank you for being part of the journey!
52
+
53
+ ## Features
54
+
55
+ - **Sessions that survive restarts.** Every project gets a session slot pinned to one conversation ID. After a crash, a reboot or days of downtime, the device resumes that exact conversation from its transcript and reattaches it to Remote Control. You open the Claude app and continue where you stopped.
56
+ - **Processes that survive restarts.** Dev servers, watchers and workers registered with `proc add` restart when they crash and come back after a reboot.
57
+ - **A browser that remembers.** One Chromium with a persistent profile: logins, cookies and open tabs come back. The Playwright MCP drives that same browser, so you can watch what the agent does.
58
+ - **Live progress.** On Linux, a virtual display in your browser (noVNC); on a Mac, Screen Sharing. Also a read-only web view of the tmux session, and Playwright UI mode. All of it is reached over an SSH tunnel, or through ngrok when you switch it on.
59
+ - **Self-healing.** Services restart on failure, and a heal job every 2 minutes revives anything dead or hung and rebuilds missing tmux windows.
60
+ - **Native capabilities for the agent:** shell commands, disk/device/service introspection, SQLite, Docker, your Dokploy (through its API), graphify, ngrok, Ollama. Each one is installed, checked by `doctor`, registered as MCP where that helps, documented to the agent in a generated skill, and pre-approved with a curated allow-list.
61
+ - **Its own MCP server** (`harness-device mcp`): device status, sessions, processes, logs, viewers, screenshot, headed test runs, service restarts.
62
+ - **Any number of devices.** Each device has a name, and its sessions show up in the Claude app as `<device>-<project>`. `fleet status` shows every device from one machine.
63
+ - **Linux and macOS.** systemd user services on Linux, launchd agents on macOS, with the same commands on both.
64
+
65
+ ## How it works
66
+
67
+ ```
68
+ claude.ai / Claude app (phone, browser)
69
+ │ Remote Control (outbound HTTPS only)
70
+ ┌─────────────────────────────────┼───────────────────────────── device ──┐
71
+ │ systemd user units (Linux) / launchd agents (macOS), start at boot │
72
+ │ │
73
+ │ tmux session "harness" │
74
+ │ ├─ rc-<project> claude --remote-control <device>-<project> │
75
+ │ │ --resume <pinned conversation id> │
76
+ │ ├─ rc-<project>-2 another pinned conversation │
77
+ │ └─ proc-<name> registered dev server / watcher / worker │
78
+ │ │
79
+ │ chromium (persistent profile, CDP 127.0.0.1:9222) ← Playwright MCP │
80
+ │ display: Xvfb + noVNC (Linux) / real screen (macOS) · ttyd (tmux r/o) │
81
+ │ heal timer: restart dead services, rebuild windows from ~/.harness-device/state
82
+ └─────────────────────────────────────────────────────────────────────────┘
83
+ ```
84
+
85
+ Why sessions used to vanish: tmux and everything started inside it are ordinary processes, so a restart kills them. Claude Code's own `claude remote-control` server can bring its sessions back only if it restarts within about 4 hours. harness-device owns the conversation IDs itself. It starts each one with `--session-id <id>` the first time and `--resume <id>` every time after, so recovery does not depend on how long the device was down. Transcripts are kept for `TRANSCRIPT_DAYS` (default 365), because restores are made from them.
86
+
87
+ ## Installation
88
+
89
+ Needs Node.js 18.17+ and Claude Code (`curl -fsSL https://claude.ai/install.sh | bash`).
90
+
91
+ - **Linux:** Debian or Ubuntu with systemd. System packages come from apt, through one `sudo` prompt.
92
+ - **macOS:** [Homebrew](https://brew.sh). Packages are installed as your user, with no sudo.
93
+
94
+ ## Quick Start
95
+
96
+ ```bash
97
+ npx @dreamtree-org/harness-device init --device box1 --project app:/home/me/projects/app
98
+ npx @dreamtree-org/harness-device install
99
+ ```
100
+
101
+ Then, once per device:
102
+
103
+ ```bash
104
+ claude auth login # choose the claude.ai account: Remote Control needs it
105
+ harness-device attach # answer the one-time "trust folder" / "enable Remote Control" prompts
106
+ harness-device doctor # everything green
107
+ ```
108
+
109
+ Open the Claude app: the session `box1-app` is there. Reboot the device, and it comes back in the same conversation.
110
+
111
+ Watch the device: `harness-device tunnel` prints an `ssh -N -L …` command to run on your laptop, then open the printed URLs.
112
+
113
+ `install` links the CLI to `~/.local/bin/harness-device`. If that folder is not on your `PATH`, open a new shell or use `~/.harness-device/bin/harness-device`.
114
+
115
+ ### Connect your Dokploy
116
+
117
+ Dokploy stays where it is deployed; the device only talks to its API.
118
+
119
+ ```bash
120
+ harness-device init --with dokploy --dokploy-url https://dokploy.example.com
121
+ harness-device secrets set DOKPLOY_API_KEY # Dokploy → Settings → Profile → API/CLI
122
+ harness-device install
123
+ harness-device doctor # "Dokploy API accepts the key"
124
+ ```
125
+
126
+ ### Updating
127
+
128
+ Run `install` again with the new version: `npx @dreamtree-org/harness-device@latest install`. It is idempotent. It updates scripts, services, MCP registrations and the skill, and never restarts the tmux session, so live conversations keep running. Add or drop capabilities the same way: `init --with …` / `--without …`, then `install`.
129
+
130
+ ## Commands
131
+
132
+ | Command | What it does |
133
+ | --- | --- |
134
+ | `init [--device] [--project name:/dir] [--with/--without cap,…] [--serve-mode] [--dokploy-url]` | Write or update `~/.harness-device/config.env` |
135
+ | `install [--dry-run] [--skip-system]` | Install or update everything. Idempotent, and never restarts the tmux session, so live sessions stay |
136
+ | `doctor` | Check config, restart recovery, services, every capability and the agent login, with a fix for each failure |
137
+ | `status [--json]` | Sessions (with conversation IDs and last activity), processes, services, viewers |
138
+ | `project add name:/dir`, `project rm`, `project list` | Projects get a session slot automatically |
139
+ | `session list`, `session new <project>` | Add another persistent conversation for a project |
140
+ | `session reset <project> [slot]` | Start a fresh conversation in a slot (the old ID is kept in history) |
141
+ | `session adopt <project> <slot> <id>` | Pin a slot to an existing conversation ID |
142
+ | `proc add <name> <dir> '<cmd>'`, `proc rm`, `proc list` | Keep a process running across crashes and reboots |
143
+ | `attach [project]`, `logs <target> [-n] [-f]`, `events` | Look inside; `events` shows restores, resets and failures |
144
+ | `restart <service>` | Restart one harness service |
145
+ | `tunnel` | The SSH command for the viewers |
146
+ | `secrets set KEY`, `secrets list`, `secrets rm KEY` | API keys for MCP servers, stored mode 600. Only the MCP servers get them, not the agent's environment |
147
+ | `ngrok token`, `ngrok up viewers\|apps\|all`, `ngrok down`, `ngrok status` | Public URLs, off until you turn them on |
148
+ | `mcp` | The harness-device MCP server (stdio) |
149
+ | `fleet add <name> <user@host>`, `fleet status`, `fleet run <name> <args>` | Many devices from one machine, over ssh |
150
+ | `uninstall [--purge]` | Remove services and agent wiring (`--purge` also deletes state and config) |
151
+
152
+ ## Capabilities
153
+
154
+ Pick them with `init --with`/`--without`, or edit `CAPABILITIES=` in `config.env`, then run `install` again. The default is `browser, playwright, display, shell, system, sqlite, docker, graphify`.
155
+
156
+ | Capability | Linux | macOS | Gives the agent |
157
+ | --- | --- | --- | --- |
158
+ | `shell` (always) | tmux, systemd units | tmux, launchd agents | Native commands, persistent sessions, `proc` |
159
+ | `system` | | | Disk, memory, ports, services (read-only commands pre-approved) |
160
+ | `display` | Xvfb + x11vnc + noVNC + ttyd | real screen + ttyd | A screen you watch; `screenshot` tool |
161
+ | `browser` | Playwright Chromium | Playwright Chromium | One persistent Chromium on CDP 127.0.0.1:9222 |
162
+ | `playwright` | | | Playwright MCP attached to that browser; `hd-pw-live` headed runs and UI mode |
163
+ | `sqlite` | apt `sqlite3` | built in | `sqlite3` |
164
+ | `docker` | Docker Engine (`get.docker.com`) | colima + docker CLI | `docker`, `docker compose` |
165
+ | `dokploy` | API only | API only | Dokploy MCP against your deployment (`DOKPLOY_URL` + `secrets set DOKPLOY_API_KEY`); nothing is installed |
166
+ | `graphify` | | | `@dreamtree-org/graphify` CLI; `graphify .` wires a project's graph MCP and hooks |
167
+ | `ngrok` | apt repo | Homebrew | Tunnels you open; the agent may only read them |
168
+ | `ollama` | ollama.com script | Homebrew | Local models on 127.0.0.1:11434 (`OLLAMA_MODELS` pulled on install) |
169
+
170
+ Extra MCP servers for the agent: add `MCP_<name>=command args` lines to `config.env`. They run through `hd-with-secrets`, so API keys stay in `secrets.env`.
171
+
172
+ ## Approvals
173
+
174
+ Remote sessions run commands without anyone at the keyboard, so `install` merges a curated list into `~/.claude/settings.json`. It only ever adds or removes its own entries.
175
+
176
+ - **Pre-approved:** git, npm/npx/node, tmux, curl, docker, sqlite3, graphify, `hd-pw-live`, read-only system commands, the read-only `harness-device` subcommands (`status`, `logs`, `proc list`, `session list`/`new`…), the Playwright and graphify MCP tools, and the harness-device MCP tools that only read (status, sessions, logs, viewers, screenshot…) or restart the browser.
177
+ - **Denied:** `sudo`, `su`, shutdown/reboot, `mkfs`, `dd`, `rm -rf` of `/` or home, `tmux kill-server`/`kill-session`, and reading `secrets.env`, the ngrok config and `~/.ssh`.
178
+ - **Never from inside a session:** restarting tmux (it holds every live session); the MCP tool refuses it, and the CLI asks for `--force`.
179
+ - **Everything else asks you**, including Dokploy deploys, registering or removing processes (`proc add`/`rm`), and anything that changes `harness-device` itself (`uninstall`, `ngrok up`, `session reset`).
180
+
181
+ This is a guard against accidents, not a sandbox. Pre-approving `node`, `npx` and `npm` already lets the agent run any code as your user, so give a device only the projects and credentials you would give the agent anyway.
182
+
183
+ ## Restart recovery, by platform
184
+
185
+ | | Linux | macOS |
186
+ | --- | --- | --- |
187
+ | Starts after a reboot | At boot, before anyone logs in (systemd linger) | When the user logs in (launchd agent). For unattended recovery, turn on **automatic login**; Claude Code's login lives in the Keychain, which only unlocks in a logged-in session. `doctor` checks this. |
188
+ | Conversations | Resumed by ID, any downtime | Resumed by ID, any downtime |
189
+ | Browser | Profile + tabs restored | Profile + tabs restored |
190
+ | Registered processes | Restarted | Restarted |
191
+ | Docker containers | `--restart unless-stopped` | Same; colima starts at login |
192
+
193
+ ## Fleet
194
+
195
+ ```bash
196
+ harness-device fleet add box1 me@10.0.0.5
197
+ harness-device fleet run box1 init --device box1 --project app:/home/me/app
198
+ harness-device fleet run box1 install
199
+ harness-device fleet status # every device: sessions, failing slots, services
200
+ ```
201
+
202
+ ## Security
203
+
204
+ - Nothing listens publicly. CDP, noVNC, ttyd and Playwright UI bind to 127.0.0.1, and Remote Control is outbound HTTPS only. ngrok tunnels open only when you run `ngrok up`, and viewer and app tunnels require basic auth.
205
+ - `ANTHROPIC_API_KEY` and similar are stripped from sessions (an API key would bypass the claude.ai login that Remote Control needs), and `secrets set` refuses to store them.
206
+ - `docker` access is root-equivalent on Linux. Leave the `docker` capability off on a device you share.
207
+
208
+ ## Troubleshooting
209
+
210
+ | Symptom | Check |
211
+ | --- | --- |
212
+ | A session is missing in the Claude app | `harness-device status`. A slot marked failing exited quickly 5 times in a row, usually because Claude is logged out (`claude auth login`) or a first-run prompt is waiting (`harness-device attach <project>`). |
213
+ | A session came back as a new conversation | `harness-device events` shows each `resume` and `new conversation`. To pin a slot to a conversation you already have: `session adopt <project> <slot> <id>`. |
214
+ | Nothing started after a reboot (Linux) | `doctor` → "restart recovery": linger must be on (`loginctl enable-linger $USER`). |
215
+ | Nothing started after a reboot (macOS) | Services start at login. Turn on automatic login, or log in once. |
216
+ | The browser is hung or has no CDP | `harness-device restart chromium`; the heal job also does this within 2 minutes. |
217
+ | Dokploy tools fail | `doctor` → dokploy: URL reachable and key accepted. |
218
+
219
+ Logs: `harness-device logs <project|proc-name|service> [-f]`.
220
+
221
+ ## What it deliberately does NOT do
222
+
223
+ - **Install Dokploy, or any server software beyond the tools above.** Dokploy is used through the API of your existing deployment.
224
+ - **Open public URLs on its own.** ngrok stays off until you run `ngrok up`, and the agent can only read tunnel status.
225
+ - **Replace a conversation silently.** A failing slot is flagged; only `session reset` starts a new one.
226
+ - **Sandbox the agent.** It runs as your user, with the approvals above.
227
+ - **Support Windows, or Linux without systemd and apt** (yet).
228
+
229
+ ## Releasing
230
+
231
+ Every push to `main` runs the `Package` workflow (`.github/workflows/package.yml`): the gate, then it stages the next patch after the highest published-or-staged version with `npm stage publish`. It goes live once a maintainer approves it on npmjs.com → Staged Packages (2FA) or with `npm stage approve <id>`. CI sets the version in the build only and never commits, tags or pushes; for a minor/major release, raise `version` in `package.json` in a PR. Pull requests run the same gate and never stage. To re-stage by hand: Actions → Package → Run workflow on `main`. Full process: [RELEASING.md](https://github.com/dreamtree-org/dreamtree-org.config/blob/main/ci/RELEASING.md).
232
+
233
+ ## Contributing
234
+
235
+ 1. Fork the repository
236
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
237
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
238
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
239
+ 5. Open a Pull Request
240
+
241
+ `npm test` runs the whole suite. The restart-recovery tests drive the real shell scripts with a fake `claude` and `tmux`, on both bash 3.2 (macOS) and bash 5 (Linux).
242
+
243
+ ## License
244
+
245
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
246
+
247
+ ## Support
248
+
249
+ - 🐛 [Issue Tracker](https://github.com/dreamtree-org/dreamtree-org.harness-device/issues)
250
+ - 📦 [npm package](https://www.npmjs.com/package/@dreamtree-org/harness-device)
251
+ - 🌐 [Dreamtree-Org](https://www.dreamtreeglobal.com)
252
+ - 📧 [preetham.krishna.dev@gmail.com](mailto:preetham.krishna.dev@gmail.com)
253
+
254
+ ---
255
+
256
+ **Made with ❤️ by [Partha Preetham Krishna](https://www.linkedin.com/in/partha-preetham-krishna-68ba00197/)** under [Dreamtree-Org](https://www.dreamtreeglobal.com) · [www.dreamtreeglobal.com](https://www.dreamtreeglobal.com)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ import { main } from '../src/cli.js';
3
+
4
+ main(process.argv.slice(2)).catch((e) => {
5
+ console.error(`\x1b[31m✗\x1b[0m ${e && e.message ? e.message : e}`);
6
+ if (process.env.HARNESS_DEVICE_DEBUG) console.error(e);
7
+ process.exit(1);
8
+ });
package/package.json CHANGED
@@ -1,6 +1,64 @@
1
1
  {
2
2
  "name": "@dreamtree-org/harness-device",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Turn a Linux server into a reboot-proof Claude Code harness: Remote Control per project in tmux, a persistent Chromium + Playwright MCP on a virtual display, live viewers (noVNC, ttyd, Playwright UI), self-healing systemd services, plus Docker, Dokploy, ngrok and AI-provider tooling.",
5
+ "author": {
6
+ "name": "Partha Preetham Krishna",
7
+ "email": "preetham.krishna.dev@gmail.com",
8
+ "url": "https://www.dreamtreeglobal.com"
9
+ },
10
+ "type": "module",
11
+ "license": "MIT",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/dreamtree-org/dreamtree-org.harness-device.git"
15
+ },
16
+ "homepage": "https://github.com/dreamtree-org/dreamtree-org.harness-device#readme",
17
+ "bugs": {
18
+ "url": "https://github.com/dreamtree-org/dreamtree-org.harness-device/issues"
19
+ },
20
+ "bin": {
21
+ "harness-device": "bin/harness-device.js"
22
+ },
23
+ "files": [
24
+ "bin",
25
+ "src",
26
+ "sh",
27
+ "templates",
28
+ "README.md",
29
+ "CHANGELOG.md",
30
+ "LICENSE"
31
+ ],
32
+ "engines": {
33
+ "node": ">=18.17"
34
+ },
35
+ "os": [
36
+ "linux",
37
+ "darwin"
38
+ ],
39
+ "scripts": {
40
+ "test": "node --test test/*.test.js"
41
+ },
42
+ "keywords": [
43
+ "claude",
44
+ "claude-code",
45
+ "remote-control",
46
+ "tmux",
47
+ "systemd",
48
+ "playwright",
49
+ "chromium",
50
+ "novnc",
51
+ "dokploy",
52
+ "ngrok",
53
+ "ollama",
54
+ "devbox",
55
+ "dreamtree"
56
+ ],
57
+ "publishConfig": {
58
+ "access": "public"
59
+ },
60
+ "dependencies": {
61
+ "@modelcontextprotocol/sdk": "^1.32.2",
62
+ "zod": "^4.4.3"
63
+ }
64
+ }
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env bash
2
+ # Chromium on the virtual display with a persistent profile and CDP on localhost.
3
+ # The profile keeps logins and cookies; --restore-last-session reopens the tabs
4
+ # that were open before a crash or reboot.
5
+ set -euo pipefail
6
+ . "$(dirname "$0")/hd-lib.sh"
7
+ profile="${HD_PROFILE:-$HD_HOME/chromium-profile}"
8
+ mkdir -p "$profile"
9
+
10
+ # An unclean shutdown leaves Singleton* locks that make Chromium refuse the
11
+ # profile, and marks the last exit as a crash (restore bubble). Clear both.
12
+ rm -f "$profile"/SingletonLock "$profile"/SingletonSocket "$profile"/SingletonCookie
13
+ if [ -f "$profile/Default/Preferences" ]; then
14
+ sed -i.bak -e 's/"exit_type":"Crashed"/"exit_type":"Normal"/' -e 's/"exited_cleanly":false/"exited_cleanly":true/' \
15
+ "$profile/Default/Preferences" 2>/dev/null && rm -f "$profile/Default/Preferences.bak" || true
16
+ fi
17
+
18
+ find_chrome() {
19
+ if [ -n "${HD_CHROME:-}" ] && [ -x "$HD_CHROME" ]; then echo "$HD_CHROME"; return; fi
20
+ # Playwright's Chromium first: same build the tests use (and on Linux, not a snap).
21
+ local c base
22
+ if [ "$HD_OS" = Darwin ]; then
23
+ base="$HOME/Library/Caches/ms-playwright"
24
+ for c in $(ls -d "$base"/chromium-*/chrome-mac*/*.app/Contents/MacOS/* 2>/dev/null | sort -r); do
25
+ [ -x "$c" ] && { echo "$c"; return; }
26
+ done
27
+ for c in "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" "/Applications/Chromium.app/Contents/MacOS/Chromium"; do
28
+ [ -x "$c" ] && { echo "$c"; return; }
29
+ done
30
+ else
31
+ c=$(ls -d "$HOME"/.cache/ms-playwright/chromium-*/chrome-linux*/chrome 2>/dev/null | sort | tail -n1 || true)
32
+ if [ -n "$c" ] && [ -x "$c" ]; then echo "$c"; return; fi
33
+ for c in google-chrome chromium chromium-browser; do
34
+ command -v "$c" >/dev/null 2>&1 && { command -v "$c"; return; }
35
+ done
36
+ fi
37
+ return 1
38
+ }
39
+ chrome=$(find_chrome) || { hd_log "no Chromium found; run: harness-device install"; exit 1; }
40
+ hd_log "launching $chrome (CDP 127.0.0.1:$HD_CDP_PORT${DISPLAY:+, display $DISPLAY})"
41
+ exec "$chrome" \
42
+ --user-data-dir="$profile" \
43
+ --remote-debugging-address=127.0.0.1 \
44
+ --remote-debugging-port="$HD_CDP_PORT" \
45
+ --no-first-run --no-default-browser-check \
46
+ --disable-session-crashed-bubble --hide-crash-restore-bubble \
47
+ --disable-dev-shm-usage \
48
+ --password-store=basic \
49
+ --restore-last-session \
50
+ --start-maximized \
51
+ --window-position=0,0
package/sh/bin/hd-heal ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env bash
2
+ # Run every HEAL_INTERVAL (systemd timer on Linux, launchd StartInterval on
3
+ # macOS). Restarts dead or hung services, rebuilds tmux windows from state.
4
+ set -uo pipefail
5
+ . "$(dirname "$0")/hd-lib.sh"
6
+ # HD_UNITS (runtime.env) lists the services the enabled capabilities own.
7
+ # tmux is handled below (its session is the thing to check); heal is us.
8
+ for u in ${HD_UNITS:-}; do
9
+ case "$u" in harness-heal|harness-tmux) continue ;; esac
10
+ if ! hd_svc_active "$u"; then
11
+ hd_log "$u is not running; restarting"
12
+ hd_svc_restart "$u" || true
13
+ fi
14
+ done
15
+ # Alive but hung: the CDP endpoint stops answering.
16
+ if [[ " ${HD_UNITS:-} " == *" harness-chromium "* ]] && hd_svc_active harness-chromium; then
17
+ if ! curl -fsS --max-time 5 "http://127.0.0.1:$HD_CDP_PORT/json/version" >/dev/null 2>&1; then
18
+ hd_log "Chromium is not answering on CDP port $HD_CDP_PORT; restarting"
19
+ hd_svc_restart harness-chromium || true
20
+ fi
21
+ fi
22
+ # The tmux server must belong to the tmux service (on Linux, anything this
23
+ # oneshot starts is killed when it exits). So a missing session means restart
24
+ # that service; an existing one only needs its windows topped up (the tmux
25
+ # server spawns them, not us).
26
+ if tmux has-session -t "=$HD_SESSION" 2>/dev/null; then
27
+ "$HD_HOME/bin/hd-tmux-layout" || hd_log "tmux layout failed"
28
+ else
29
+ hd_log "tmux session $HD_SESSION is gone; restarting harness-tmux"
30
+ hd_svc_restart harness-tmux || true
31
+ fi
@@ -0,0 +1,74 @@
1
+ # shellcheck shell=bash
2
+ # Shared by the hd-* scripts. Sourced, never executed.
3
+ # The scripts live in $HD_HOME/bin, so their own location finds the device home
4
+ # even where systemd/launchd don't pass HARNESS_DEVICE_HOME.
5
+ _hd_bin="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6
+ if [ -n "${HARNESS_DEVICE_HOME:-}" ]; then HD_HOME="$HARNESS_DEVICE_HOME"
7
+ elif [ -f "$_hd_bin/../runtime.env" ]; then HD_HOME="$(dirname "$_hd_bin")"
8
+ else HD_HOME="${HD_HOME:-$HOME/.harness-device}"; fi
9
+ if [ -f "$HD_HOME/runtime.env" ]; then
10
+ set -a
11
+ # shellcheck disable=SC1091
12
+ . "$HD_HOME/runtime.env"
13
+ set +a
14
+ fi
15
+ : "${HD_SESSION:=harness}" "${HD_DISPLAY_NUM:=99}" "${HD_CDP_PORT:=9222}"
16
+ HD_OS="$(uname -s)"
17
+ # Linux uses the virtual display; macOS draws on its real screen.
18
+ [ "$HD_OS" = Linux ] && export DISPLAY="${DISPLAY:-:$HD_DISPLAY_NUM}"
19
+ mkdir -p "$HD_HOME/logs"
20
+
21
+ hd_log() { printf '[harness-device %s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"; }
22
+
23
+ # Anthropic credentials make Claude Code skip claude.ai auth, and Remote
24
+ # Control only works with a claude.ai login: never pass them on.
25
+ hd_strip_anthropic() { unset ANTHROPIC_API_KEY ANTHROPIC_AUTH_TOKEN CLAUDE_CODE_USE_BEDROCK CLAUDE_CODE_USE_VERTEX; }
26
+
27
+ # secrets.env, for the one process that needs it (MCP servers via
28
+ # hd-with-secrets). Not loaded into agent sessions or registered processes.
29
+ hd_load_secrets() {
30
+ if [ -f "$HD_HOME/secrets.env" ]; then
31
+ set -a
32
+ # shellcheck disable=SC1091
33
+ . "$HD_HOME/secrets.env"
34
+ set +a
35
+ fi
36
+ hd_strip_anthropic
37
+ }
38
+
39
+ HD_STATE="$HD_HOME/state"
40
+
41
+ hd_uuid() {
42
+ if [ -r /proc/sys/kernel/random/uuid ]; then cat /proc/sys/kernel/random/uuid
43
+ elif command -v uuidgen >/dev/null 2>&1; then uuidgen | tr 'A-Z' 'a-z'
44
+ else node -e 'console.log(require("crypto").randomUUID())'; fi
45
+ }
46
+
47
+ # Record something worth knowing after a reboot (restores, rotations).
48
+ hd_event() {
49
+ mkdir -p "$HD_STATE"
50
+ printf '%s\t%s\n' "$(date '+%Y-%m-%dT%H:%M:%S%z')" "$*" >> "$HD_STATE/events.log"
51
+ }
52
+
53
+ # The transcript of a Claude conversation, wherever Claude keeps it.
54
+ hd_claude_transcript() {
55
+ find "${CLAUDE_CONFIG_DIR:-$HOME/.claude}/projects" -maxdepth 2 -name "$1.jsonl" -print -quit 2>/dev/null
56
+ }
57
+
58
+ # Services, whichever manager runs them: systemd user units on Linux, launchd
59
+ # agents on macOS. Takes the logical name (harness-tmux, harness-chromium, ...).
60
+ hd_svc_label() { echo "org.dreamtree.harness-device.${1#harness-}"; }
61
+ hd_svc_active() {
62
+ if [ "$HD_OS" = Darwin ]; then
63
+ launchctl print "gui/$(id -u)/$(hd_svc_label "$1")" 2>/dev/null | grep -q '^[[:space:]]*state = running'
64
+ else
65
+ systemctl --user is-active --quiet "$1"
66
+ fi
67
+ }
68
+ hd_svc_restart() {
69
+ if [ "$HD_OS" = Darwin ]; then
70
+ launchctl kickstart -k "gui/$(id -u)/$(hd_svc_label "$1")"
71
+ else
72
+ systemctl --user restart "$1"
73
+ fi
74
+ }
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env bash
2
+ # ngrok with the tunnels chosen by `harness-device ngrok up` (ngrok.env).
3
+ set -eu
4
+ . "$(dirname "$0")/hd-lib.sh"
5
+ . "$HD_HOME/ngrok.env"
6
+ # shellcheck disable=SC2086
7
+ exec ngrok start --config "$HD_HOME/ngrok.yml" --log stdout --log-format logfmt $HD_NGROK_TUNNELS
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env bash
2
+ # Keeps one registered long-running process (dev server, watcher, worker)
3
+ # alive across crashes and reboots. Registered with: harness-device proc add
4
+ # Usage: hd-proc-loop <name>
5
+ set -u
6
+ . "$(dirname "$0")/hd-lib.sh"
7
+ name="$1"
8
+ f="$HD_STATE/procs/$name"
9
+ hd_strip_anthropic
10
+ delay=2
11
+ while [ -f "$f.cmd" ]; do
12
+ cd "$(cat "$f.dir" 2>/dev/null || echo "$HOME")" || exit 1
13
+ started=$(date +%s)
14
+ hd_log "proc $name: $(cat "$f.cmd")"
15
+ bash -c "$(cat "$f.cmd")"
16
+ code=$?
17
+ [ -f "$f.cmd" ] || break
18
+ ran=$(( $(date +%s) - started ))
19
+ if [ "$ran" -ge 120 ]; then delay=2; else delay=$(( delay * 2 )); [ "$delay" -gt 120 ] && delay=120; fi
20
+ hd_log "proc $name exited with $code after ${ran}s; restarting in ${delay}s"
21
+ sleep "$delay"
22
+ done
23
+ hd_log "proc $name was removed; stopping"
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env bash
2
+ # Playwright with live progress.
3
+ # hd-pw-live run <dir> [playwright args] headed on the virtual display (watch in noVNC),
4
+ # output streamed here and to logs/tests.log
5
+ # hd-pw-live ui <dir> [args] Playwright UI mode on 127.0.0.1:$HD_PW_UI_PORT,
6
+ # in tmux window "pw-ui"
7
+ # hd-pw-live stop close the UI window
8
+ # hd-pw-live log follow logs/tests.log
9
+ set -uo pipefail
10
+ . "$(dirname "$0")/hd-lib.sh"
11
+ cmd="${1:-}"; shift || true
12
+ log="$HD_HOME/logs/tests.log"
13
+ case "$cmd" in
14
+ run)
15
+ dir="${1:?usage: hd-pw-live run <dir> [args]}"; shift
16
+ cd "$dir" || exit 1
17
+ { echo; hd_log "=== playwright test --headed $* (in $dir)"; } >> "$log"
18
+ npx playwright test --headed --reporter=list "$@" 2>&1 | tee -a "$log"
19
+ code=${PIPESTATUS[0]}
20
+ hd_log "=== exit $code" | tee -a "$log"
21
+ exit "$code"
22
+ ;;
23
+ ui)
24
+ dir="${1:?usage: hd-pw-live ui <dir> [args]}"; shift
25
+ tmux kill-window -t "=$HD_SESSION:pw-ui" 2>/dev/null || true
26
+ c=$(printf '%q ' npx playwright test --ui-host=127.0.0.1 --ui-port="$HD_PW_UI_PORT" "$@")
27
+ tmux new-window -d -t "=$HD_SESSION:" -n pw-ui -c "$dir" "$c"
28
+ echo "Playwright UI: http://127.0.0.1:$HD_PW_UI_PORT (tmux window pw-ui)"
29
+ ;;
30
+ stop) tmux kill-window -t "=$HD_SESSION:pw-ui" 2>/dev/null && echo "stopped" || echo "not running" ;;
31
+ log) touch "$log"; exec tail -n 200 -F "$log" ;;
32
+ *) sed -n '2,9p' "$0" | sed 's/^# \{0,1\}//'; exit 2 ;;
33
+ esac
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env bash
2
+ # Serves one project session slot to the remote agent forever, restarting with
3
+ # backoff. The provider decides how: hd-serve-<provider>.
4
+ # Usage: hd-rc-loop <project> <dir> <slot|-> ("-" = server mode, no slot)
5
+ set -u
6
+ . "$(dirname "$0")/hd-lib.sh"
7
+ name="$1"; dir="$2"; slot="$3"
8
+ hd_strip_anthropic
9
+ cd "$dir" || { hd_log "project dir $dir is missing; retrying in 60s"; sleep 60; exit 1; }
10
+ label="${HD_DEVICE:-device}-$name"; [ "$slot" = - ] || [ "$slot" = 1 ] || label="$label-$slot"
11
+ slotf="$HD_STATE/sessions/$name/$slot"
12
+
13
+ delay=2; fast=0
14
+ while true; do
15
+ [ "$slot" = - ] || [ -f "$slotf" ] || { hd_log "slot $name/$slot was removed; stopping"; exit 0; }
16
+ started=$(date +%s)
17
+ if [ "$slot" = - ]; then
18
+ hd_log "serving $name ($dir) as $label [server mode]"
19
+ "$(dirname "$0")/hd-serve-${HD_PROVIDER:-claude}" "$label" -
20
+ else
21
+ id=$(cat "$slotf")
22
+ hd_log "serving $name/$slot ($dir) as $label, conversation $id"
23
+ "$(dirname "$0")/hd-serve-${HD_PROVIDER:-claude}" "$label" "$id"
24
+ fi
25
+ code=$?
26
+ ran=$(( $(date +%s) - started ))
27
+ if [ "$ran" -ge 300 ]; then delay=2; fast=0; else delay=$(( delay * 2 )); fast=$(( fast + 1 )); [ "$delay" -gt 300 ] && delay=300; fi
28
+ # Never swap the conversation automatically: a fast failure is usually a
29
+ # login or network problem, and the pinned conversation must survive it.
30
+ # Flag the slot instead; doctor and status report it, and
31
+ # `harness-device session reset <project> [slot]` starts a new one on purpose.
32
+ if [ "$slot" != - ]; then
33
+ if [ "$fast" -ge 5 ]; then
34
+ [ -f "$slotf.failing" ] || hd_event "$name/$slot failing: $fast quick exits in a row (last code $code)"
35
+ echo "$code $(date +%s)" > "$slotf.failing"
36
+ elif [ "$fast" -eq 0 ]; then
37
+ rm -f "$slotf.failing"
38
+ fi
39
+ fi
40
+ hd_log "exited with $code after ${ran}s; restarting in ${delay}s (Ctrl-C now to stop this loop)"
41
+ sleep "$delay"
42
+ done