-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedded.go
More file actions
236 lines (209 loc) · 7.11 KB
/
embedded.go
File metadata and controls
236 lines (209 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// SPDX-License-Identifier: AGPL-3.0-or-later
// Embedded daemon entry points.
//
// The other half of this package (bindings.go) is a thin RPC client
// that talks to an out-of-process daemon over a Unix socket. That
// model fits desktop SDKs (Python, Node) where the daemon is a
// separate long-running process. It does NOT fit iOS: one app =
// one process, no sibling daemon binary, no system-wide socket.
//
// PilotEmbeddedStart boots a daemon directly inside the host process
// (the goroutine model is unchanged from cmd/daemon). Internally it
// still opens a Unix socket so the existing 45 Pilot* RPC functions
// in bindings.go work unchanged — same wire protocol, just a
// different addressing space.
//
// Lifecycle: PilotEmbeddedStart → PilotConnect(socketPath) → use
// driver functions → PilotClose(handle) → PilotEmbeddedStop.
package main
/*
#include <stdlib.h>
#include <stdint.h>
*/
import "C"
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
"time"
"github.com/TeoSlayer/pilotprotocol/pkg/daemon"
"github.com/TeoSlayer/pilotprotocol/pkg/driver"
"github.com/pilot-protocol/handshake"
"github.com/pilot-protocol/policy"
"github.com/pilot-protocol/runtime"
"github.com/pilot-protocol/skillinject"
)
type embeddedNode struct {
d *daemon.Daemon
rt *runtime.Runtime
}
var embedded struct {
sync.Mutex
node *embeddedNode
}
// EmbeddedConfig is what callers supply via JSON in PilotEmbeddedStart.
// Keep it minimal and JSON-friendly so the Swift/Obj-C side can build
// it with a dictionary literal.
type embeddedConfig struct {
DataDir string `json:"data_dir"` // absolute, host-writable
SocketPath string `json:"socket_path"` // ≤ 100 bytes; use relative if abs is too long
RegistryAddr string `json:"registry_addr"` // default 34.71.57.205:9000
BeaconAddr string `json:"beacon_addr"` // default 34.71.57.205:9001
TrustAutoApprove bool `json:"trust_auto_approve"` // accept all incoming handshakes
KeepaliveSec int `json:"keepalive_sec"` // default 30; lower → faster handshake polling
Version string `json:"version"` // surfaced in Info(); cosmetic
}
func (c *embeddedConfig) defaults() {
if c.RegistryAddr == "" {
c.RegistryAddr = "34.71.57.205:9000"
}
if c.BeaconAddr == "" {
c.BeaconAddr = "34.71.57.205:9001"
}
if c.KeepaliveSec <= 0 {
c.KeepaliveSec = 30
}
if c.Version == "" {
c.Version = "embedded"
}
}
// Boot the embedded daemon. configJSON is a JSON-encoded embeddedConfig.
// Returns JSON: on success {"address","node_id","public_key","socket"},
// on failure {"error": "..."}.
//
// Idempotent only in the trivial sense — calling twice without Stop
// returns an error.
//
//export PilotEmbeddedStart
func PilotEmbeddedStart(configJSON *C.char) *C.char {
embedded.Lock()
defer embedded.Unlock()
if embedded.node != nil {
return errJSON(fmt.Errorf("embedded daemon already started"))
}
var cfg embeddedConfig
if err := unmarshalCString(configJSON, &cfg); err != nil {
return errJSON(fmt.Errorf("parse config: %w", err))
}
cfg.defaults()
if cfg.DataDir == "" {
return errJSON(fmt.Errorf("data_dir required"))
}
if cfg.SocketPath == "" {
return errJSON(fmt.Errorf("socket_path required"))
}
identityPath := filepath.Join(cfg.DataDir, "identity.json")
d := daemon.New(daemon.Config{
RegistryAddr: cfg.RegistryAddr,
BeaconAddr: cfg.BeaconAddr,
ListenAddr: ":0",
SocketPath: cfg.SocketPath,
Encrypt: true,
IdentityPath: identityPath,
DisableEcho: true,
DisableDataExchange: true,
DisableEventStream: true,
TrustAutoApprove: cfg.TrustAutoApprove,
KeepaliveInterval: time.Duration(cfg.KeepaliveSec) * time.Second,
Version: cfg.Version,
})
rt := runtime.New(d)
// Minimum plugin set for handshake + datagram I/O. No
// trustedagents (it gates trust to a curated GitHub list and
// breaks ad-hoc peers), no webhook (spams retries to a stale
// URL on macOS dev hosts), no dataexchange/eventstream
// (file-system inbox; clients use SendTo/RecvFrom directly).
if err := rt.Register(skillinject.NewService(skillinject.Config{})); err != nil {
return errJSON(fmt.Errorf("register skillinject: %w", err))
}
policySvc := policy.NewService(runtime.NewPolicyRuntime(d))
if err := rt.Register(policySvc); err != nil {
return errJSON(fmt.Errorf("register policy: %w", err))
}
d.RegisterPolicyManager(runtime.AsDaemonPolicyManager(policySvc.Manager()))
hsSvc := handshake.NewService(runtime.NewHandshakeRuntime(d))
if err := rt.Register(hsSvc); err != nil {
return errJSON(fmt.Errorf("register handshake: %w", err))
}
d.RegisterHandshakeService(runtime.NewHandshakeServiceAdapter(hsSvc))
if err := rt.StartPlugins(context.Background()); err != nil {
return errJSON(fmt.Errorf("plugin startup: %w", err))
}
if err := d.Start(); err != nil {
_ = rt.StopPlugins(context.Background())
return errJSON(fmt.Errorf("daemon start: %w", err))
}
embedded.node = &embeddedNode{d: d, rt: rt}
// Wait for the IPC socket to exist before we probe Info() — Start
// returns once IPC is listening, but on slow simulators the file
// stat can race.
deadline := time.Now().Add(5 * time.Second)
for time.Now().Before(deadline) {
if _, err := os.Stat(cfg.SocketPath); err == nil {
break
}
time.Sleep(50 * time.Millisecond)
}
// Probe Info() so callers get node_id/address/public_key in the
// startup response without an extra round-trip.
probe, err := driver.Connect(cfg.SocketPath)
if err != nil {
return okJSON(map[string]interface{}{
"node_id": d.NodeID(),
"socket": cfg.SocketPath,
"warning": fmt.Sprintf("probe connect: %v", err),
})
}
defer probe.Close()
info, err := probe.Info()
if err != nil {
return okJSON(map[string]interface{}{
"node_id": d.NodeID(),
"socket": cfg.SocketPath,
"warning": fmt.Sprintf("probe info: %v", err),
})
}
return okJSON(map[string]interface{}{
"address": info["address"],
"node_id": info["node_id"],
"public_key": info["public_key"],
"socket": cfg.SocketPath,
})
}
// Tear down the embedded daemon and all plugins. Safe to call when
// not started — returns {"status":"not_started"}.
//
//export PilotEmbeddedStop
func PilotEmbeddedStop() *C.char {
embedded.Lock()
n := embedded.node
embedded.node = nil
embedded.Unlock()
if n == nil {
return okJSON(map[string]string{"status": "not_started"})
}
if err := n.d.Stop(); err != nil {
return errJSON(fmt.Errorf("daemon stop: %w", err))
}
stopCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := n.rt.StopPlugins(stopCtx); err != nil {
// Not fatal — daemon is already down, plugin teardown is
// best-effort. Surface the warning to the caller.
return okJSON(map[string]string{
"status": "stopped",
"warning": err.Error(),
})
}
return okJSON(map[string]string{"status": "stopped"})
}
// unmarshalCString is a tiny helper to JSON-decode a C string into v.
func unmarshalCString(s *C.char, v interface{}) error {
if s == nil {
return fmt.Errorf("nil")
}
return json.Unmarshal([]byte(C.GoString(s)), v)
}