@log0u7/opencode-mesh 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 (64) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/LICENSE +201 -0
  3. package/README.md +88 -0
  4. package/dist/index.d.ts +15 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +15 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/lib/client.d.ts +10 -0
  9. package/dist/lib/client.d.ts.map +1 -0
  10. package/dist/lib/client.js +26 -0
  11. package/dist/lib/client.js.map +1 -0
  12. package/dist/lib/db.d.ts +7 -0
  13. package/dist/lib/db.d.ts.map +1 -0
  14. package/dist/lib/db.js +55 -0
  15. package/dist/lib/db.js.map +1 -0
  16. package/dist/lib/discovery.d.ts +27 -0
  17. package/dist/lib/discovery.d.ts.map +1 -0
  18. package/dist/lib/discovery.js +63 -0
  19. package/dist/lib/discovery.js.map +1 -0
  20. package/dist/lib/identity.d.ts +15 -0
  21. package/dist/lib/identity.d.ts.map +1 -0
  22. package/dist/lib/identity.js +63 -0
  23. package/dist/lib/identity.js.map +1 -0
  24. package/dist/lib/locks.d.ts +17 -0
  25. package/dist/lib/locks.d.ts.map +1 -0
  26. package/dist/lib/locks.js +39 -0
  27. package/dist/lib/locks.js.map +1 -0
  28. package/dist/lib/mail.d.ts +11 -0
  29. package/dist/lib/mail.d.ts.map +1 -0
  30. package/dist/lib/mail.js +42 -0
  31. package/dist/lib/mail.js.map +1 -0
  32. package/dist/lib/peers.d.ts +31 -0
  33. package/dist/lib/peers.d.ts.map +1 -0
  34. package/dist/lib/peers.js +95 -0
  35. package/dist/lib/peers.js.map +1 -0
  36. package/dist/lib/serve.d.ts +12 -0
  37. package/dist/lib/serve.d.ts.map +1 -0
  38. package/dist/lib/serve.js +165 -0
  39. package/dist/lib/serve.js.map +1 -0
  40. package/dist/lib/spawn.d.ts +33 -0
  41. package/dist/lib/spawn.d.ts.map +1 -0
  42. package/dist/lib/spawn.js +130 -0
  43. package/dist/lib/spawn.js.map +1 -0
  44. package/dist/lib/sqlite.d.ts +9 -0
  45. package/dist/lib/sqlite.d.ts.map +1 -0
  46. package/dist/lib/sqlite.js +63 -0
  47. package/dist/lib/sqlite.js.map +1 -0
  48. package/dist/lib/workers.d.ts +29 -0
  49. package/dist/lib/workers.d.ts.map +1 -0
  50. package/dist/lib/workers.js +74 -0
  51. package/dist/lib/workers.js.map +1 -0
  52. package/dist/lib/worktree.d.ts +30 -0
  53. package/dist/lib/worktree.d.ts.map +1 -0
  54. package/dist/lib/worktree.js +43 -0
  55. package/dist/lib/worktree.js.map +1 -0
  56. package/dist/plugin.d.ts +3 -0
  57. package/dist/plugin.d.ts.map +1 -0
  58. package/dist/plugin.js +310 -0
  59. package/dist/plugin.js.map +1 -0
  60. package/dist/types/peer.d.ts +26 -0
  61. package/dist/types/peer.d.ts.map +1 -0
  62. package/dist/types/peer.js +2 -0
  63. package/dist/types/peer.js.map +1 -0
  64. package/package.json +61 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,31 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-09-18
11
+
12
+ ### Changed
13
+
14
+ - Dev deps: vitest 5 with @vitest/coverage-v8 5 (coverage gate unchanged: 90/80).
15
+ - Trunk-based workflow: `main` is the only branch (PRs required, protected by rulesets); `dev` removed.
16
+
17
+ ### Added
18
+
19
+ - Orchestrator: `mesh_spawn` runs tasks as tracked headless workers in isolated opencode worktrees (native worktree API), with `mesh_workers`, `mesh_worker_logs`, `mesh_worker_stop`, and `mesh_worker_remove`; plugin dispose SIGTERMs live workers.
20
+ - Coverage gate in CI: 90% lines/functions, 80% branches (`pnpm test:coverage` locally).
21
+ - Dependabot group coupling vitest with @vitest/* (coverage provider) so major bumps arrive together.
22
+ - CONTRIBUTING.md documents the mandatory test-first flow and the solo-maintainer merge preference.
23
+ - CONTRIBUTING.md documents the Dependabot dependency PR routine (criteria, maintainer approval, workflow-file merge options).
24
+ - Project scaffold: Apache-2.0 license, CI (biome + typecheck + vitest + gitleaks, Node 22/24 matrix), npm publish via trusted publishing, issue and PR templates, Dependabot, lefthook pre-commit.
25
+
26
+ ### Fixed
27
+
28
+ - Install docs register the scoped plugin name in `opencode.json` (an unscoped spec would attempt to install a nonexistent npm package).
29
+ - Removed a stray committed PR body file (`.pr-body.md`).
30
+ - Runtime compatibility with the installed OpenCode binary: ephemeral-port fallback for mesh servers, in-process fetch reuse for the worktree API in `opencode run` mode, `bun:sqlite` module resolution for hot-loaded plugins, and workers defaulting to `--agent build`.
31
+ - CI gitleaks job no longer fails on Dependabot pull requests (push-only).
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 log0u7
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # opencode-mesh
2
+
3
+ [![CI](https://github.com/log0u7/opencode-mesh/actions/workflows/ci.yml/badge.svg)](https://github.com/log0u7/opencode-mesh/actions/workflows/ci.yml)
4
+ [![npm](https://img.shields.io/npm/v/@log0u7/opencode-mesh)](https://www.npmjs.com/package/@log0u7/opencode-mesh)
5
+ [![License: Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
6
+
7
+ Coordination for [OpenCode](https://opencode.ai): multiple sessions working together on one machine, and a peer pool across a LAN or a Tailscale tailnet.
8
+
9
+ Each node advertises itself over mDNS, pairs with explicit operator approval (per-peer bearer tokens, deny by default), then sessions can exchange messages (`mesh_send` / `mesh_inbox`) and coordinate on shared work (`mesh_lock` / `mesh_unlock`). Tailscale IPs are picked up automatically when `tailscale status` works, for cross-subnet pools.
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ npm install -g @log0u7/opencode-mesh
15
+ ```
16
+
17
+ Then register the plugin in `opencode.json`:
18
+
19
+ ```json
20
+ {
21
+ "plugin": ["@log0u7/opencode-mesh"]
22
+ }
23
+ ```
24
+
25
+ Restart OpenCode. Node identity and mailbox live under `~/.local/share/opencode-mesh/` (honors `XDG_DATA_HOME`), created with restrictive permissions.
26
+
27
+ Note (npm 12): the package ships no lifecycle install scripts and needs no `allowScripts` / allowlist on install (its runtime dependency `bonjour-service` has none either).
28
+
29
+ ## Usage
30
+
31
+ Same machine: start two OpenCode instances in the same repository.
32
+
33
+ ```
34
+ mesh_status() # local sessions + paired peers
35
+ mesh_pair() # list discovered peers, approve pairing
36
+ mesh_send(peer="laptop", subject="handoff", body="...")
37
+ mesh_inbox() # pull new messages, ack
38
+ mesh_lock(path="src/api.ts", ttl=300) # claim a file before editing
39
+ mesh_unlock(path="src/api.ts")
40
+ ```
41
+
42
+ ### Tools
43
+
44
+ | Tool | Args | Description |
45
+ |---|---|---|
46
+ | `mesh_status` | : local session registry, own address, paired peers. |
47
+ | `mesh_pair` | `query?` | List discovered peers; operator approves by hostname/fingerprint. |
48
+ | `mesh_send` | `peer`, `subject`, `body` | Deliver a message to a peer's mailbox. |
49
+ | `mesh_inbox` | `ack?` | Fetch unread messages; ack marks them read. |
50
+ | `mesh_lock` | `path`, `ttl?` | Exclusive claim on a file or worktree path (default ttl 600s). |
51
+ | `mesh_unlock` | `path` | Release a claim. |
52
+
53
+ ### Orchestrator
54
+
55
+ Spawn headless workers in isolated opencode worktrees (native worktree API), track them, and clean up:
56
+
57
+ ```
58
+ mesh_spawn(task="implement the parser", name="parser") # -> worker_id, worktree, branch
59
+ mesh_workers() # lifecycle states, session ids
60
+ mesh_worker_logs(worker_id="...", tail=50) # captured output
61
+ mesh_worker_stop(worker_id="...") # SIGTERM a running worker
62
+ mesh_worker_remove(worker_id="...", force=true) # worktree + registry row cleanup
63
+ ```
64
+
65
+ Workers run `opencode run --dir <worktree> --format json` headless with `--auto` permissions by default (`auto=false` to require approvals). model/agent are optional args (defaults: your local config). `dispose` SIGTERMs every live worker so stopping opencode never leaves orphan processes.
66
+
67
+ ### Security model
68
+
69
+ - Every HTTP endpoint requires a per-peer bearer token; nothing is open by default.
70
+ - All peers, same machine or remote, pair through `mesh_pair`: one side emits a one-time token, the other completes the pairing with it.
71
+ - mDNS TXT records carry node id + key fingerprint only, never secrets.
72
+ - LAN-only by default; tailnet addresses are surfaced by `mesh_status` when `tailscale` runs, for cross-subnet pools (`"allow_tailnet": true` plugin option, planned).
73
+
74
+ ## Development
75
+
76
+ Requires Node >= 22 (mise: `mise install`) and pnpm (corepack).
77
+
78
+ ```sh
79
+ pnpm install
80
+ pnpm verify # biome + typecheck + vitest
81
+ pnpm build:check # tsc build + pack dry-run
82
+ ```
83
+
84
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for the workflow.
85
+
86
+ ## License
87
+
88
+ [Apache-2.0](LICENSE)
@@ -0,0 +1,15 @@
1
+ /**
2
+ * OpenCode Mesh Plugin
3
+ *
4
+ * Multi-session coordination and peer pool for OpenCode.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ declare const pluginModule: {
9
+ id: string;
10
+ server: import("@opencode-ai/plugin").Plugin;
11
+ };
12
+ export default pluginModule;
13
+ export type { Lock, MailMessage, Peer } from "./types/peer.js";
14
+ export { MeshPlugin } from "./plugin.js";
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AASH,QAAA,MAAM,YAAY;;;CAGQ,CAAC;eAEZ,YAAY;AAE3B,YAAY,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE/D,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * OpenCode Mesh Plugin
3
+ *
4
+ * Multi-session coordination and peer pool for OpenCode.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ import { MeshPlugin } from "./plugin.js";
9
+ const pluginModule = {
10
+ id: "@log0u7/opencode-mesh",
11
+ server: MeshPlugin,
12
+ };
13
+ export default pluginModule;
14
+ export { MeshPlugin } from "./plugin.js";
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAOzC,MAAM,YAAY,GAAG;IACnB,EAAE,EAAE,uBAAuB;IAC3B,MAAM,EAAE,UAAU;CACM,CAAC;AAE3B,eAAe,YAAY,CAAC;AAI5B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,10 @@
1
+ export type SendTarget = {
2
+ url: string;
3
+ token: string;
4
+ from: string;
5
+ subject: string;
6
+ body: string;
7
+ };
8
+ export type SendResult = "ok" | "unreachable" | "unauthorized" | "error";
9
+ export declare function sendMail(target: SendTarget): Promise<SendResult>;
10
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/lib/client.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,IAAI,GAAG,aAAa,GAAG,cAAc,GAAG,OAAO,CAAC;AAIzE,wBAAsB,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAqBtE"}
@@ -0,0 +1,26 @@
1
+ // Deliver a message to a peer's /mail endpoint. Never throws: the model reads
2
+ // the result string and reacts.
3
+ export async function sendMail(target) {
4
+ try {
5
+ const response = await fetch(`${target.url}/mail`, {
6
+ method: "POST",
7
+ headers: {
8
+ "content-type": "application/json",
9
+ authorization: `Bearer ${target.token}`,
10
+ },
11
+ body: JSON.stringify({ from: target.from, subject: target.subject, body: target.body }),
12
+ signal: AbortSignal.timeout(5000),
13
+ });
14
+ if (response.ok) {
15
+ return "ok";
16
+ }
17
+ if (response.status === 401) {
18
+ return "unauthorized";
19
+ }
20
+ return "error";
21
+ }
22
+ catch {
23
+ return "unreachable";
24
+ }
25
+ }
26
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/lib/client.ts"],"names":[],"mappings":"AAUA,8EAA8E;AAC9E,gCAAgC;AAChC,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,MAAkB;IAC/C,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,MAAM,CAAC,GAAG,OAAO,EAAE;YACjD,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,cAAc,EAAE,kBAAkB;gBAClC,aAAa,EAAE,UAAU,MAAM,CAAC,KAAK,EAAE;aACxC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;YACvF,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC;SAClC,CAAC,CAAC;QACH,IAAI,QAAQ,CAAC,EAAE,EAAE,CAAC;YAChB,OAAO,IAAI,CAAC;QACd,CAAC;QACD,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;YAC5B,OAAO,cAAc,CAAC;QACxB,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,aAAa,CAAC;IACvB,CAAC;AACH,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { type SqliteConn } from "./sqlite.js";
2
+ export type MeshDb = {
3
+ conn: SqliteConn;
4
+ close(): void;
5
+ };
6
+ export declare function openMeshDb(path: string): MeshDb;
7
+ //# sourceMappingURL=db.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/lib/db.ts"],"names":[],"mappings":"AAGA,OAAO,EAAc,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAE1D,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AA4CF,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAW/C"}
package/dist/lib/db.js ADDED
@@ -0,0 +1,55 @@
1
+ import { mkdirSync } from "node:fs";
2
+ import { dirname } from "node:path";
3
+ import { openSqlite } from "./sqlite.js";
4
+ const SCHEMA = `
5
+ CREATE TABLE IF NOT EXISTS peers (
6
+ node_id TEXT PRIMARY KEY,
7
+ hostname TEXT NOT NULL,
8
+ fingerprint TEXT NOT NULL,
9
+ addresses TEXT NOT NULL DEFAULT '[]',
10
+ paired INTEGER NOT NULL DEFAULT 0,
11
+ token TEXT,
12
+ last_seen INTEGER NOT NULL
13
+ );
14
+ CREATE TABLE IF NOT EXISTS messages (
15
+ id TEXT PRIMARY KEY,
16
+ from_node TEXT NOT NULL,
17
+ subject TEXT NOT NULL,
18
+ body TEXT NOT NULL,
19
+ status TEXT NOT NULL DEFAULT 'unread',
20
+ created_at INTEGER NOT NULL
21
+ );
22
+ CREATE TABLE IF NOT EXISTS locks (
23
+ path TEXT PRIMARY KEY,
24
+ owner_node TEXT NOT NULL,
25
+ owner_session TEXT NOT NULL,
26
+ expires_at INTEGER NOT NULL
27
+ );
28
+ CREATE TABLE IF NOT EXISTS pending_tokens (
29
+ token TEXT PRIMARY KEY,
30
+ created_at INTEGER NOT NULL
31
+ );
32
+ CREATE TABLE IF NOT EXISTS workers (
33
+ id TEXT PRIMARY KEY,
34
+ task TEXT NOT NULL,
35
+ worktree_path TEXT NOT NULL,
36
+ branch TEXT NOT NULL,
37
+ pid INTEGER,
38
+ session_id TEXT,
39
+ status TEXT NOT NULL DEFAULT 'running',
40
+ exit_code INTEGER,
41
+ started_at INTEGER NOT NULL,
42
+ finished_at INTEGER
43
+ );
44
+ `;
45
+ export function openMeshDb(path) {
46
+ if (path !== ":memory:") {
47
+ mkdirSync(dirname(path), { recursive: true });
48
+ }
49
+ const conn = openSqlite(path);
50
+ conn.run("PRAGMA journal_mode = WAL");
51
+ conn.run("PRAGMA busy_timeout = 5000");
52
+ conn.exec(SCHEMA);
53
+ return { conn, close: () => conn.close() };
54
+ }
55
+ //# sourceMappingURL=db.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"db.js","sourceRoot":"","sources":["../../src/lib/db.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,UAAU,EAAmB,MAAM,aAAa,CAAC;AAO1D,MAAM,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAwCd,CAAC;AAEF,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,IAAI,IAAI,KAAK,UAAU,EAAE,CAAC;QACxB,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAChD,CAAC;IAED,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;IAC9B,IAAI,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC;IACtC,IAAI,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;IACvC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAElB,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,EAAE,CAAC;AAC7C,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { Bonjour } from "bonjour-service";
2
+ import type { MeshDb } from "./db.js";
3
+ export declare const MESH_SERVICE_TYPE = "opencode-mesh";
4
+ export type AdvertiseHandle = {
5
+ close(): void;
6
+ };
7
+ export declare function advertiseMesh(options: {
8
+ bonjour: Bonjour;
9
+ port: number;
10
+ node_id: string;
11
+ fingerprint: string;
12
+ }): AdvertiseHandle;
13
+ export type DiscoveredPeer = {
14
+ node_id: string;
15
+ hostname: string;
16
+ fingerprint: string;
17
+ addresses: string[];
18
+ };
19
+ export declare function browseMesh(options: {
20
+ bonjour: Bonjour;
21
+ onDiscovered: (peer: DiscoveredPeer) => void;
22
+ }): {
23
+ close(): void;
24
+ };
25
+ export declare function rememberPeer(db: MeshDb, peer: DiscoveredPeer): void;
26
+ export declare function tailnetAddresses(): Promise<string[]>;
27
+ //# sourceMappingURL=discovery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"discovery.d.ts","sourceRoot":"","sources":["../../src/lib/discovery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAG/C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEtC,eAAO,MAAM,iBAAiB,kBAAkB,CAAC;AAEjD,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAIF,wBAAgB,aAAa,CAAC,OAAO,EAAE;IACrC,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;CACrB,GAAG,eAAe,CAalB;AAED,MAAM,MAAM,cAAc,GAAG;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,EAAE,CAAC;CACrB,CAAC;AAEF,wBAAgB,UAAU,CAAC,OAAO,EAAE;IAClC,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,IAAI,CAAC;CAC9C,GAAG;IAAE,KAAK,IAAI,IAAI,CAAA;CAAE,CAoBpB;AAGD,wBAAgB,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,GAAG,IAAI,CAEnE;AAID,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAkB1D"}
@@ -0,0 +1,63 @@
1
+ import { upsertDiscoveredPeer } from "./peers.js";
2
+ export const MESH_SERVICE_TYPE = "opencode-mesh";
3
+ // Advertise this node on the LAN. TXT carries only the node id and key
4
+ // fingerprint: never secrets.
5
+ export function advertiseMesh(options) {
6
+ const service = options.bonjour.publish({
7
+ name: options.node_id,
8
+ type: MESH_SERVICE_TYPE,
9
+ port: options.port,
10
+ txt: { id: options.node_id, fp: options.fingerprint },
11
+ });
12
+ return {
13
+ close() {
14
+ service.stop();
15
+ },
16
+ };
17
+ }
18
+ export function browseMesh(options) {
19
+ const browser = options.bonjour.find({ type: MESH_SERVICE_TYPE }, (service) => {
20
+ const id = service.txt?.id;
21
+ const fp = service.txt?.fp;
22
+ if (typeof id !== "string" || typeof fp !== "string" || id.length === 0) {
23
+ return;
24
+ }
25
+ options.onDiscovered({
26
+ node_id: id,
27
+ hostname: service.host ?? service.name,
28
+ fingerprint: fp,
29
+ addresses: service.addresses ?? [],
30
+ });
31
+ });
32
+ return {
33
+ close() {
34
+ browser.stop();
35
+ },
36
+ };
37
+ }
38
+ // Feed a discovered peer into the local peer table (unpaired until mesh_pair).
39
+ export function rememberPeer(db, peer) {
40
+ upsertDiscoveredPeer(db, peer);
41
+ }
42
+ // Tailnet addresses let peers connect across subnets when tailscale runs.
43
+ // Absent CLI = empty list, never an error.
44
+ export async function tailnetAddresses() {
45
+ const { execFile } = await import("node:child_process");
46
+ return new Promise((resolve) => {
47
+ execFile("tailscale", ["status", "--json"], (error, stdout) => {
48
+ if (error) {
49
+ resolve([]);
50
+ return;
51
+ }
52
+ try {
53
+ const status = JSON.parse(stdout);
54
+ const ips = status.Self?.TailscaleIPs;
55
+ resolve(Array.isArray(ips) ? ips.filter((ip) => typeof ip === "string") : []);
56
+ }
57
+ catch {
58
+ resolve([]);
59
+ }
60
+ });
61
+ });
62
+ }
63
+ //# sourceMappingURL=discovery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"discovery.js","sourceRoot":"","sources":["../../src/lib/discovery.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAGlD,MAAM,CAAC,MAAM,iBAAiB,GAAG,eAAe,CAAC;AAMjD,uEAAuE;AACvE,8BAA8B;AAC9B,MAAM,UAAU,aAAa,CAAC,OAK7B;IACC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC;QACtC,IAAI,EAAE,OAAO,CAAC,OAAO;QACrB,IAAI,EAAE,iBAAiB;QACvB,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,GAAG,EAAE,EAAE,EAAE,EAAE,OAAO,CAAC,OAAO,EAAE,EAAE,EAAE,OAAO,CAAC,WAAW,EAAE;KACtD,CAAC,CAAC;IAEH,OAAO;QACL,KAAK;YACH,OAAO,CAAC,IAAI,EAAE,CAAC;QACjB,CAAC;KACF,CAAC;AACJ,CAAC;AASD,MAAM,UAAU,UAAU,CAAC,OAG1B;IACC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,iBAAiB,EAAE,EAAE,CAAC,OAAO,EAAE,EAAE;QAC5E,MAAM,EAAE,GAAG,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC;QAC3B,MAAM,EAAE,GAAG,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC;QAC3B,IAAI,OAAO,EAAE,KAAK,QAAQ,IAAI,OAAO,EAAE,KAAK,QAAQ,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACxE,OAAO;QACT,CAAC;QACD,OAAO,CAAC,YAAY,CAAC;YACnB,OAAO,EAAE,EAAE;YACX,QAAQ,EAAE,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,IAAI;YACtC,WAAW,EAAE,EAAE;YACf,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,EAAE;SACnC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,KAAK;YACH,OAAO,CAAC,IAAI,EAAE,CAAC;QACjB,CAAC;KACF,CAAC;AACJ,CAAC;AAED,+EAA+E;AAC/E,MAAM,UAAU,YAAY,CAAC,EAAU,EAAE,IAAoB;IAC3D,oBAAoB,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;AACjC,CAAC;AAED,0EAA0E;AAC1E,2CAA2C;AAC3C,MAAM,CAAC,KAAK,UAAU,gBAAgB;IACpC,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,MAAM,CAAC,oBAAoB,CAAC,CAAC;IAExD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,QAAQ,CAAC,WAAW,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,EAAE,CAAC,KAAmB,EAAE,MAAc,EAAE,EAAE;YAClF,IAAI,KAAK,EAAE,CAAC;gBACV,OAAO,CAAC,EAAE,CAAC,CAAC;gBACZ,OAAO;YACT,CAAC;YACD,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAA2C,CAAC;gBAC5E,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC;gBACtC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,EAAE,EAAgB,EAAE,CAAC,OAAO,EAAE,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;YAC9F,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,CAAC,EAAE,CAAC,CAAC;YACd,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,15 @@
1
+ type Jwk = {
2
+ kty?: string;
3
+ x?: string;
4
+ [key: string]: unknown;
5
+ };
6
+ export type NodeIdentity = {
7
+ node_id: string;
8
+ hostname: string;
9
+ fingerprint: string;
10
+ public_key: Jwk;
11
+ private_key: Jwk;
12
+ };
13
+ export declare function loadOrCreateIdentity(dataDir: string): NodeIdentity;
14
+ export {};
15
+ //# sourceMappingURL=identity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../../src/lib/identity.ts"],"names":[],"mappings":"AAIA,KAAK,GAAG,GAAG;IACT,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,GAAG,CAAC;IAChB,WAAW,EAAE,GAAG,CAAC;CAClB,CAAC;AASF,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,CASlE"}