-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_handler.go
More file actions
369 lines (347 loc) · 12 KB
/
plugin_handler.go
File metadata and controls
369 lines (347 loc) · 12 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// plugin_handler.go — Plug-and-Play Task Packs (roadmap Phase 1-2-4-3).
//
// POST /api/plugins/install (multipart "file" = .fwpack zip)
// → validate → CAPS CONSENT gate → extract agent(s) (staging+atomic-rename →
// hot-load) → daftarin kategori+crew → SMOKE-TEST synth (gagal = disable).
// Kategori kebaca mr-flow classifier (Phase 0 dynamic). Loopback-only.
//
// Core = installPluginPack(raw, approveCaps): dipakai handler HTTP DAN watcher
// drop-folder (plugin_watcher.go) — satu jalur, no duplikasi.
//
// .fwpack layout (zip): plugin.json + agents/<id>/{agent.wasm, manifest.json}
//
// CATATAN: SENGAJA ga manggil UploadHandler (stabil) — extract self-contained +
// path-safe (anti zip-slip), staging+rename biar watcher hot-load 1 dir lengkap.
package main
import (
"archive/zip"
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"flowork-gui/internal/agentdb"
"flowork-gui/internal/floworkdb"
"flowork-gui/internal/kernel/loader"
"flowork-gui/internal/kernelhost"
)
var pluginIDRe = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{1,63}$`)
// pluginDangerCapPrefixes — caps yang WAJIB consent owner pas install plugin
// (untrusted). Pakai PRIMITIVE ASLI Flowork (manifest.go: fs/net/kv/exec/bus/
// secret/time/rpc/state). Bahaya buat plugin pihak-ketiga:
//
// exec:* → jalanin command / kendali PC (exec:power)
// secret:* → baca secret owner (TOKEN exfil!)
// fs:shared → akses file warga lain
// rpc:agent-invoke → setir agent lain
//
// net:fetch / fs (workspace sendiri) / state / time = normal, ga di-flag.
var pluginDangerCapPrefixes = []string{"exec:", "secret:", "fs:shared", "rpc:agent-invoke"}
func pluginCapDangerous(c string) bool {
for _, p := range pluginDangerCapPrefixes {
if strings.HasPrefix(c, p) {
return true
}
}
return false
}
type pluginCrewMember struct {
AgentID string `json:"agent_id"`
RoleLabel string `json:"role_label"`
Kind string `json:"kind"` // "worker" | "synth"
Persona string `json:"persona,omitempty"` // kv.prompt — "jiwa" app, ikut pack (NO token/secret)
}
type pluginManifest struct {
ID string `json:"id"`
Name string `json:"name"`
Version string `json:"version"`
Author string `json:"author"`
Category struct {
ID string `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
TriggerHint string `json:"trigger_hint"`
SynthDirective string `json:"synth_directive"`
WorkerDirective string `json:"worker_directive"`
} `json:"category"`
Crew []pluginCrewMember `json:"crew"`
}
// validate — tolak pack ngaco SEBELUM nyentuh disk/DB. Balik "" kalau valid.
func (m *pluginManifest) validate() string {
if !pluginIDRe.MatchString(m.ID) {
return "plugin.id invalid (^[a-z0-9][a-z0-9_-]{1,63}$)"
}
if !pluginIDRe.MatchString(m.Category.ID) {
return "category.id invalid"
}
if len(m.Crew) == 0 {
return "crew kosong"
}
synthCount := 0
for _, c := range m.Crew {
if !pluginIDRe.MatchString(c.AgentID) {
return "crew agent_id invalid: " + c.AgentID
}
if c.Kind == "synth" {
synthCount++
}
}
if synthCount != 1 {
return "crew WAJIB tepat 1 synth, ada " + strconv.Itoa(synthCount)
}
return ""
}
// scanPackCaps — baca manifest tiap agent di pack → kumpulin caps berbahaya.
func scanPackCaps(zr *zip.Reader) (danger []string) {
seen := map[string]bool{}
for _, f := range zr.File {
name := strings.TrimPrefix(f.Name, "./")
if !strings.HasPrefix(name, "agents/") || !strings.HasSuffix(name, "/manifest.json") {
continue
}
rc, e := f.Open()
if e != nil {
continue
}
raw, _ := io.ReadAll(io.LimitReader(rc, 1<<20))
rc.Close()
var mf struct {
CapabilitiesRequired []string `json:"capabilities_required"`
}
if json.Unmarshal(raw, &mf) != nil {
continue
}
for _, c := range mf.CapabilitiesRequired {
if pluginCapDangerous(c) && !seen[c] {
seen[c] = true
danger = append(danger, c)
}
}
}
return danger
}
// pluginInstallResult — hasil core install (status 0 = sukses/200).
type pluginInstallResult struct {
status int
body map[string]any
}
// installPluginPack — CORE install (dipakai HTTP handler + watcher drop-folder).
// approveCaps=true → lewatin consent gate (owner-trusted, mis. drop-folder).
func installPluginPack(host *kernelhost.Host, store *floworkdb.Store, raw []byte, approveCaps bool) pluginInstallResult {
bad := func(code int, b map[string]any) pluginInstallResult { return pluginInstallResult{code, b} }
sum := sha256.Sum256(raw) // checksum integritas pack (Phase 6.3)
checksum := hex.EncodeToString(sum[:])
zr, err := zip.NewReader(bytes.NewReader(raw), int64(len(raw)))
if err != nil {
return bad(http.StatusBadRequest, map[string]any{"error": "not a valid zip: " + err.Error()})
}
// 1) parse + validate plugin.json
var manRaw []byte
for _, f := range zr.File {
base := strings.TrimPrefix(f.Name, "./")
if base == "plugin.json" || strings.HasSuffix(base, "/plugin.json") {
rc, e := f.Open()
if e == nil {
manRaw, _ = io.ReadAll(io.LimitReader(rc, 1<<20))
rc.Close()
}
break
}
}
if manRaw == nil {
return bad(http.StatusBadRequest, map[string]any{"error": "plugin.json ga ketemu di pack"})
}
var man pluginManifest
if err := json.Unmarshal(manRaw, &man); err != nil {
return bad(http.StatusBadRequest, map[string]any{"error": "plugin.json parse: " + err.Error()})
}
if msg := man.validate(); msg != "" {
return bad(http.StatusBadRequest, map[string]any{"error": "manifest invalid: " + msg})
}
// 2) CAPS CONSENT (Phase 4.1)
danger := scanPackCaps(zr)
if len(danger) > 0 && !approveCaps {
return bad(http.StatusForbidden, map[string]any{
"error": "pack minta caps BERBAHAYA — butuh persetujuan owner",
"dangerous_caps": danger,
"approve_hint": "install ulang dengan query ?approve_caps=1 kalau lo percaya pack ini",
"plugin": man.ID,
"consent_required": true,
})
}
// 3) extract ke STAGING → ATOMIC RENAME ke AgentsDir/<id>.fwagent → hot-load bersih
agentsRoot := loader.AgentsDir()
staging := filepath.Join(agentsRoot, ".plugin-staging-"+man.ID)
_ = os.RemoveAll(staging)
defer os.RemoveAll(staging)
installed := map[string]int{}
for _, f := range zr.File {
name := strings.TrimPrefix(f.Name, "./")
if !strings.HasPrefix(name, "agents/") || strings.HasSuffix(name, "/") {
continue
}
rest := strings.TrimPrefix(name, "agents/")
slash := strings.IndexByte(rest, '/')
if slash <= 0 {
continue
}
aid, rel := rest[:slash], rest[slash+1:]
if !pluginIDRe.MatchString(aid) || rel == "" {
continue
}
stageDir := filepath.Join(staging, aid)
dest := filepath.Join(stageDir, filepath.FromSlash(rel))
if c, e := filepath.Rel(stageDir, dest); e != nil || strings.HasPrefix(c, "..") {
continue // anti zip-slip
}
if e := os.MkdirAll(filepath.Dir(dest), 0o755); e != nil {
return bad(http.StatusInternalServerError, map[string]any{"error": "mkdir: " + e.Error()})
}
rc, e := f.Open()
if e != nil {
continue
}
out, e := os.Create(dest)
if e != nil {
rc.Close()
return bad(http.StatusInternalServerError, map[string]any{"error": "create: " + e.Error()})
}
_, _ = io.Copy(out, io.LimitReader(rc, 64<<20))
out.Close()
rc.Close()
installed[aid]++
}
// 3b) tulis PERSONA ke state.db staging SEBELUM rename → pas agent ke-load
// (fsnotify) personanya udah ada. Di staging = agent belum jalan → NOL
// lock-contention (nutup dogfood bug #4). Persona = "jiwa" app, ikut pack.
personaSet := []string{}
for _, c := range man.Crew {
if c.Persona == "" || installed[c.AgentID] == 0 {
continue
}
dbPath := filepath.Join(staging, c.AgentID, "workspace", "state.db")
st, e := agentdb.Open(dbPath)
if e != nil {
os.Stderr.WriteString("[plugin] persona open " + c.AgentID + ": " + e.Error() + "\n")
continue
}
if e := st.SetPrompt(c.Persona); e != nil {
os.Stderr.WriteString("[plugin] persona write " + c.AgentID + ": " + e.Error() + "\n")
} else {
personaSet = append(personaSet, c.AgentID)
}
st.Close()
}
for aid := range installed {
finalDir := filepath.Join(agentsRoot, aid+".fwagent")
_ = os.RemoveAll(finalDir)
if e := os.Rename(filepath.Join(staging, aid), finalDir); e != nil {
return bad(http.StatusInternalServerError, map[string]any{"error": "install agent " + aid + ": " + e.Error()})
}
}
// 4) daftarin kategori + crew
synth := ""
var workers []floworkdb.TaskAgent
for i, c := range man.Crew {
if c.Kind == "synth" {
synth = c.AgentID
continue
}
workers = append(workers, floworkdb.TaskAgent{
AgentID: c.AgentID, RoleLabel: c.RoleLabel, OrderIdx: i, Mode: "seq",
})
}
cat := floworkdb.TaskCategory{
ID: man.Category.ID, Name: man.Category.Name, Icon: man.Category.Icon,
TriggerHint: man.Category.TriggerHint, Synthesizer: synth,
SynthDirective: man.Category.SynthDirective, WorkerDirective: man.Category.WorkerDirective, Enabled: true,
}
if err := store.UpsertCategory(cat); err != nil {
return bad(http.StatusInternalServerError, map[string]any{"error": "upsert category: " + err.Error()})
}
if err := store.SetCrew(cat.ID, workers); err != nil {
return bad(http.StatusInternalServerError, map[string]any{"error": "set crew: " + err.Error()})
}
// 5) SMOKE-TEST: synth ga load (pack broken) → DISABLE kategori.
smoke := smokeTestSynth(host, synth)
if smoke == "not_loaded" {
cat.Enabled = false
_ = store.UpsertCategory(cat)
}
return pluginInstallResult{0, map[string]any{
"ok": smoke != "not_loaded",
"plugin": man.ID,
"category": cat.ID,
"enabled": smoke != "not_loaded",
"synth": synth,
"workers": len(workers),
"agents_extract": installed,
"persona_set": personaSet,
"verify": verifyPackStatic(raw), // VERIFIER verdict (advisory) — gerbang deploy
"caps_approved": len(danger) > 0 && approveCaps,
"dangerous_caps": danger,
"sha256": checksum,
"smoke": smoke,
"next": "kategori LIVE — mr-flow classifier auto-discover (cache <=60s).",
}}
}
func pluginInstallHandler(host *kernelhost.Host, store *floworkdb.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
tfWriteJSON(w, http.StatusMethodNotAllowed, map[string]any{"error": "POST only"})
return
}
if err := r.ParseMultipartForm(64 << 20); err != nil {
tfWriteJSON(w, http.StatusBadRequest, map[string]any{"error": "parse form: " + err.Error()})
return
}
file, _, err := r.FormFile("file")
if err != nil {
tfWriteJSON(w, http.StatusBadRequest, map[string]any{"error": "missing file field"})
return
}
defer file.Close()
raw, err := io.ReadAll(io.LimitReader(file, 128<<20))
if err != nil {
tfWriteJSON(w, http.StatusBadRequest, map[string]any{"error": "read: " + err.Error()})
return
}
res := installPluginPack(host, store, raw, r.URL.Query().Get("approve_caps") == "1")
tfWriteJSON(w, res.status, res.body)
}
}
// smokeTestSynth — tunggu synth hot-load (fsnotify) lalu ping. Balik:
//
// "ok" → synth load + balas (sehat)
// "llm_error" → synth load tapi LLM hiccup (tetep dianggap install OK)
// "not_loaded" → synth ga ke-load dalam timeout (pack broken → disable kategori)
func smokeTestSynth(host *kernelhost.Host, synthID string) string {
if host == nil || synthID == "" {
return "not_loaded"
}
for attempt := 0; attempt < 8; attempt++ {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
reply, err := host.InvokeAgentMessage(ctx, synthID, "PING instalasi — balas singkat 'ok' aja.", "plugin-smoke")
cancel()
if err == nil {
if strings.TrimSpace(reply) != "" {
return "ok"
}
return "llm_error"
}
if strings.Contains(err.Error(), "not loaded") {
time.Sleep(1500 * time.Millisecond) // kasih waktu fsnotify hot-load
continue
}
return "llm_error" // error lain = agent ada tapi gagal jalan (transient)
}
return "not_loaded"
}