opencode-remote-login 0.1.0 → 0.1.1

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 (3) hide show
  1. package/README.md +57 -26
  2. package/package.json +1 -1
  3. package/NOTES.md +0 -100
package/README.md CHANGED
@@ -1,65 +1,94 @@
1
1
  # opencode-remote-login
2
2
 
3
- Dispatch opencode sessions to remote hosts and continue tasks there.
3
+ Dispatch opencode sessions to remote hosts via SSH and continue tasks there.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ opencode plug opencode-remote-login
9
+ ```
10
+
11
+ Or add to `opencode.jsonc` manually:
12
+
13
+ ```json
14
+ {
15
+ "plugin": ["opencode-remote-login"]
16
+ }
17
+ ```
4
18
 
5
19
  ## Setup
6
20
 
7
- 1. Copy `hosts.example.json` to `hosts.json` and configure your hosts:
21
+ 1. Create `~/.config/opencode/hosts.json`:
8
22
 
9
23
  ```json
10
24
  {
11
25
  "hosts": {
12
- "pi": "user@192.168.1.100",
26
+ "pi": {
27
+ "host": "wenjun@192.168.100.100",
28
+ "agent": "build",
29
+ "model": "opencode/big-pickle"
30
+ },
13
31
  "dev": "user@10.0.0.5"
14
32
  }
15
33
  }
16
34
  ```
17
35
 
18
- 2. Ensure SSH key-based auth is configured for each host:
36
+ 2. Configure SSH key-based auth:
19
37
 
20
38
  ```bash
21
39
  ssh-copy-id user@192.168.1.100
22
40
  ```
23
41
 
24
- 3. Add to your `opencode.json`:
42
+ ### Alternative: inline config
25
43
 
26
- ```json
44
+ ```jsonc
45
+ // opencode.jsonc
27
46
  {
28
- "plugin": ["../opencode-remote-login/src/index.ts"]
47
+ "plugin": [
48
+ ["opencode-remote-login", {
49
+ "hosts": {
50
+ "pi": { "host": "wenjun@192.168.100.100" }
51
+ }
52
+ }]
53
+ ]
29
54
  }
30
55
  ```
31
56
 
57
+ ## Host config
58
+
59
+ Each host entry supports:
60
+
61
+ | Field | Description |
62
+ | ------- | ---------------------------------------- |
63
+ | `host` | SSH address (`user@host`) |
64
+ | `agent` | Agent name to use on the remote (optional) |
65
+ | `model` | Model in `provider/id` format (optional) |
66
+
67
+ Only host **names** are exposed to the LLM. SSH addresses and credentials stay private.
68
+
32
69
  ## Usage
33
70
 
34
71
  ### One-way mode (default)
35
72
 
36
- Dispatch the session and return immediately. The remote host takes over autonomously.
73
+ Dispatch and return immediately. The remote host takes over autonomously.
37
74
 
38
75
  ```
39
- > call remote_login host=pi, we need to fix the login bug in auth.ts
76
+ > call remote_login host=pi, fix the login bug in auth.ts
40
77
  ```
41
78
 
42
79
  ### Round-trip mode
43
80
 
44
- Dispatch, wait for remote to complete, then pull the updated session back locally.
81
+ Dispatch, wait for remote to complete, then pull the updated session back locally. The original agent and model are automatically restored on return.
45
82
 
46
83
  ```
47
84
  > call remote_login mode=round-trip host=pi, investigate the memory leak in worker.ts
48
85
  ```
49
86
 
50
- ## hostnames.json
87
+ ### With working directory
51
88
 
52
- Only host **names** are exposed to the LLM in the tool description. Actual SSH addresses remain private.
53
-
54
- ```json
55
- {
56
- "hosts": {
57
- "pi": "wenjun@192.168.100.100"
58
- }
59
- }
60
89
  ```
61
-
62
- The LLM sees: `Available hosts: pi`
90
+ > call remote_login host=pi directory=/home/wenjun/projects/myapp
91
+ ```
63
92
 
64
93
  ## How it works
65
94
 
@@ -67,9 +96,11 @@ The LLM sees: `Available hosts: pi`
67
96
  local opencode remote opencode
68
97
  ───────────── ───────────────
69
98
  1. export session (JSON)
70
- 2. SCP remote import
71
- 3. Trigger remote task (nohup)
72
- 4. [round-trip] poll for back-file
73
- 5. [round-trip] SCP remote export
74
- 6. [round-trip] local import
99
+ 2. patch agent/model from host config
100
+ 3. SCP remote import
101
+ 4. Trigger remote task (nohup)
102
+ 5. [round-trip] poll for back-file
103
+ 6. [round-trip] SCP ← remote export
104
+ 7. [round-trip] restore original agent/model
105
+ 8. [round-trip] local import
75
106
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-remote-login",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./server": "./src/index.ts"
package/NOTES.md DELETED
@@ -1,100 +0,0 @@
1
- # Implementation Notes
2
-
3
- ## Architecture
4
-
5
- ```
6
- opencode-remote-login/
7
- ├── package.json # exports: ./server (plugin entry)
8
- ├── hosts.json # host aliases (gitignored)
9
- ├── hosts.example.json # template
10
- └── src/
11
- └── index.ts # server plugin (~250 lines)
12
- ```
13
-
14
- The plugin registers a single tool `remote_login` with two modes (`one-way` / `round-trip`) and a `tool.execute.after` hook that triggers `promptAsync` after round-trip completes.
15
-
16
- ## Round-trip Flow
17
-
18
- ```
19
- local remote
20
- ───── ──────
21
- 1. opencode export <sid> → stdout JSON
22
- 2. fixPendingRemoteLogin() ← patch pending tool-call in JSON
23
- 3. writeFile → SCP → /tmp/opencode-import-<sid>.json
24
- 4. SSH opencode import → writes to remote SQLite
25
- 5. SCP script → SSH bash → nohup opencode run --session=<sid>
26
- 6. poll: SSH "test -f back-file" → wait for remote export
27
- 7. SCP back-file ← ← opencode export <sid>
28
- 8. opencode import back-file → writes to local SQLite
29
- 9. [hook] promptAsync fork → LLM processes imported context
30
- ```
31
-
32
- ## Pitfalls and Lessons
33
-
34
- ### 1. `opencode export` captures the tool mid-execution
35
-
36
- The export subprocess runs while the `remote_login` tool is still executing. The exported JSON shows the tool call in `"pending"` state with empty input. The remote LLM sees an incomplete tool call and gets confused.
37
-
38
- **Fix**: `fixPendingRemoteLogin()` patches the last assistant message's `remote_login` tool part from `pending` → `completed` with proper output and a `step-finish` part.
39
-
40
- ### 2. Shell quoting across SSH boundaries
41
-
42
- Building shell commands with nested quoting (ssh → bash → opencode run) is fragile. Double quotes in the continuation message were passed literally to the remote LLM.
43
-
44
- **Fix**: Write a shell script file locally, SCP it to remote, then execute it. Avoids quoting hell entirely.
45
-
46
- ### 3. `execSync` blocks the event loop
47
-
48
- `child_process.execSync` is synchronous and blocks Node's event loop. This is fine for shell commands but incompatible with async SDK calls.
49
-
50
- **Fix**: All SDK calls (`promptAsync`) are done OUTSIDE `execSync` blocks, either after all sync work is done or in hooks.
51
-
52
- ### 4. `session.prompt({ noReply: true })` does not persist
53
-
54
- Looking at the source (`prompt.ts:1631`): when `noReply === true`, the message is created in memory and returned via HTTP but never written to SQLite. No SSE events are emitted. The TUI never sees it.
55
-
56
- **Lesson**: Always verify persistence behavior in the source code.
57
-
58
- ### 5. `session.prompt({ noReply: false })` blocks the tool pipeline
59
-
60
- Calling `session.prompt` (without `noReply`) from within `tool.execute.after` hook blocks the entire tool execution pipeline waiting for LLM processing. The session state transitions cause conflicts.
61
-
62
- **Lesson**: `tool.execute.after` runs synchronously within the tool execution Effect fiber. Don't block it.
63
-
64
- ### 6. `promptAsync` has a different API shape than `prompt`
65
-
66
- ```ts
67
- // prompt (v1 SDK)
68
- session.prompt({ id: string, parts: [...] })
69
-
70
- // promptAsync (v1 SDK)
71
- session.promptAsync({ path: { id: string }, body: { parts: [...] } })
72
- ```
73
-
74
- Using the `prompt` shape for `promptAsync` resulted in `{id}` not being substituted in the URL path, causing `%7Bid%7D` errors.
75
-
76
- **Lesson**: Always check the generated SDK types. Don't assume consistent API shapes.
77
-
78
- ### 7. `promptAsync` forks via `Effect.forkIn(scope)` — scope matters
79
-
80
- The `promptAsync` handler forks the LLM loop into the HTTP request's scope. The forked fiber runs independently of the caller. The hook returns immediately while the LLM processes in the background. SSE events are emitted for the response.
81
-
82
- **Key insight**: This is the only way to trigger LLM processing from a plugin hook without blocking. The `Effect.forkIn(scope, { startImmediately: true })` pattern makes the prompt fire-and-forget.
83
-
84
- ### 8. TUI session messages are loaded once, then updated via SSE events only
85
-
86
- The TUI loads session messages on first entry (`createResource` → `sync.session.sync()`). After that, messages are only added/updated via SSE events (`message.updated`). Direct DB writes by subprocesses produce no SSE events, so the TUI never shows them.
87
-
88
- **Fix**: Use `promptAsync` (which goes through the server API and emits SSE) to trigger a visible response. The full imported history is in the DB and visible after restart.
89
-
90
- ### 9. Cross-platform `sleep` needs a JavaScript spin-loop
91
-
92
- `child_process.execSync('sleep 2')` fails on Windows. `execSync('timeout /t 2')` fails on Linux. Using `process.platform` branching is fragile.
93
-
94
- **Fix**: Simple spin-loop `while (Date.now() < end) {}` works everywhere.
95
-
96
- ### 10. Host config should be separate from code
97
-
98
- Hardcoding SSH addresses in the plugin is inflexible and leaks info to the LLM.
99
-
100
- **Fix**: `hosts.json` maps host names to addresses. Only names are injected into the tool description. The file is gitignored; `hosts.example.json` serves as a template.