pico-engine 1.6.4 → 1.9.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 (108) hide show
  1. package/README.md +16 -9
  2. package/dist/RulesetRegistryLoaderFs.js +60 -6
  3. package/dist/auth/cookies.js +32 -0
  4. package/dist/auth/index.js +6 -1
  5. package/dist/channelIdentityPolicy.js +22 -0
  6. package/dist/cli.js +106 -69
  7. package/dist/engineConfig.js +291 -0
  8. package/dist/identity/IdentityService.js +90 -0
  9. package/dist/identity/crossPico.js +24 -8
  10. package/dist/identity/didoModule.js +80 -18
  11. package/dist/identity/establishSubscription.js +2 -2
  12. package/dist/identity/identityPortable.js +92 -0
  13. package/dist/identity/index.js +10 -1
  14. package/dist/identity/localPeerReconcile.js +65 -0
  15. package/dist/identity/peerDid4.js +175 -0
  16. package/dist/identity/peerDid4Create.js +90 -0
  17. package/dist/identity/portableExportPrepare.js +196 -0
  18. package/dist/identity/portableMove.js +87 -0
  19. package/dist/identity/skyIntro.js +6 -3
  20. package/dist/identity/skyProtocol.js +26 -1
  21. package/dist/identity/skyRotation.js +121 -0
  22. package/dist/identity/store.js +5 -1
  23. package/dist/identity/subscriptionEntSync.js +72 -0
  24. package/dist/identity/veramoDidComm.js +41 -2
  25. package/dist/identity/webvh.js +4 -2
  26. package/dist/identity/webvhRelocation.js +115 -0
  27. package/dist/index.js +84 -27
  28. package/dist/logging.js +156 -16
  29. package/dist/oauth/OAuthService.js +6 -2
  30. package/dist/policyConfigFromJson.js +112 -0
  31. package/dist/runMcp.js +27 -0
  32. package/dist/server.js +261 -24
  33. package/dist/stageKrl.js +26 -0
  34. package/dist/subtreeWrangler.js +180 -0
  35. package/dist/uiChannelPolicies.js +15 -0
  36. package/dist/xmluiRuntime.js +68 -0
  37. package/krl/io.picolabs.oauth.krl +41 -2
  38. package/krl/io.picolabs.pds.krl +131 -10
  39. package/krl/io.picolabs.pico-engine-ui.krl +217 -94
  40. package/krl/io.picolabs.portable_subtree.krl +97 -0
  41. package/krl/io.picolabs.subscription.krl +180 -72
  42. package/krl/io.picolabs.wrangler.krl +313 -57
  43. package/krl/io.picolabs.xmlui.krl +295 -0
  44. package/mcp/devResources.js +118 -0
  45. package/mcp/devTools.js +351 -0
  46. package/mcp/engineClient.js +133 -0
  47. package/mcp/meshHttp.js +137 -0
  48. package/mcp/meshTools.js +189 -0
  49. package/mcp/runStdio.js +34 -0
  50. package/mcp/schemaToZod.js +56 -0
  51. package/package.json +24 -9
  52. package/public/.DS_Store +0 -0
  53. package/public/docs/_sidebar.md +3 -1
  54. package/public/docs/llms-full.txt +630 -0
  55. package/public/docs/llms.txt +38 -0
  56. package/public/docs/openapi.yaml +78 -0
  57. package/public/docs/pico-engine.md +239 -4
  58. package/public/docs/pico-overview.md +117 -0
  59. package/public/docs/pico-views-xmlui.md +5 -0
  60. package/public/docs/pico-views.md +140 -0
  61. package/public/docs/pico.md +3 -1
  62. package/public/docs/rulesets/README.md +26 -0
  63. package/public/docs/rulesets/io.picolabs.oauth.md +71 -0
  64. package/public/docs/rulesets/io.picolabs.pds.md +327 -0
  65. package/public/docs/rulesets/io.picolabs.pico-engine-ui.md +65 -0
  66. package/public/docs/rulesets/io.picolabs.portable_subtree.md +94 -0
  67. package/public/docs/rulesets/io.picolabs.subscription.md +398 -0
  68. package/public/docs/rulesets/io.picolabs.wrangler.md +471 -0
  69. package/public/pico-engine-ui.css +1 -1
  70. package/public/pico-engine-ui.js +27 -23
  71. package/public/xmlui/0.12.7.js +419 -0
  72. package/public/xmlui/pico-view-boot.js +153 -0
  73. package/src/RulesetRegistryLoaderFs.ts +66 -7
  74. package/src/auth/cookies.ts +34 -0
  75. package/src/auth/index.ts +7 -0
  76. package/src/channelIdentityPolicy.ts +29 -0
  77. package/src/cli.ts +70 -37
  78. package/src/engineConfig.ts +363 -0
  79. package/src/identity/IdentityService.ts +189 -1
  80. package/src/identity/crossPico.ts +57 -8
  81. package/src/identity/didoModule.ts +140 -23
  82. package/src/identity/establishSubscription.ts +2 -2
  83. package/src/identity/identityPortable.ts +153 -0
  84. package/src/identity/index.ts +14 -0
  85. package/src/identity/localPeerReconcile.ts +97 -0
  86. package/src/identity/peerDid4.ts +197 -0
  87. package/src/identity/peerDid4Create.ts +121 -0
  88. package/src/identity/portableExportPrepare.ts +302 -0
  89. package/src/identity/portableMove.ts +193 -0
  90. package/src/identity/skyIntro.ts +11 -11
  91. package/src/identity/skyProtocol.ts +46 -0
  92. package/src/identity/skyRotation.ts +211 -0
  93. package/src/identity/store.ts +5 -1
  94. package/src/identity/subscriptionEntSync.ts +97 -0
  95. package/src/identity/veramoDidComm.ts +52 -2
  96. package/src/identity/webvh.ts +4 -4
  97. package/src/identity/webvhRelocation.ts +167 -0
  98. package/src/index.ts +123 -31
  99. package/src/logging.ts +173 -18
  100. package/src/oauth/OAuthService.ts +11 -3
  101. package/src/policyConfigFromJson.ts +131 -0
  102. package/src/runMcp.ts +30 -0
  103. package/src/server.ts +328 -27
  104. package/src/stageKrl.ts +26 -0
  105. package/src/subtreeWrangler.ts +278 -0
  106. package/src/uiChannelPolicies.ts +15 -0
  107. package/src/xmluiRuntime.ts +66 -0
  108. package/krl/io.picolabs.did-o.krl +0 -349
@@ -3,9 +3,14 @@ ruleset io.picolabs.pico-engine-ui {
3
3
  version "0.0.0"
4
4
  name "pico-engine-ui"
5
5
  description "This is the only ruleset the pico-engine-ui.js needs to operate"
6
- shares box, uiECI, pico, logs, testingECI, name, needsStartupLayout
6
+ shares box, uiECI, pico, logs, testingECI, testingHistory, testingSaved, name, needsStartupLayout
7
7
  }
8
8
  global {
9
+ /**
10
+ * The ECI of this pico's UI channel (tagged engine,ui), used by the developer
11
+ * UI to send engine_ui events and queries to this pico.
12
+ * @returns String
13
+ */
9
14
  uiECI = function(){
10
15
  return ctx:channels
11
16
  .filter(function(c){c["tags"].sort().join(",") == "engine,ui"})
@@ -23,15 +28,48 @@ ruleset io.picolabs.pico-engine-ui {
23
28
  .map(uiEciForChild)
24
29
  .filter(function(eci){eci})
25
30
  }
31
+ /**
32
+ * The channel the developer UI's Testing tab last selected for sending events
33
+ * and queries to this pico, or null.
34
+ * @returns String
35
+ */
26
36
  testingECI = function(){
27
37
  return ent:testingECI
28
38
  }
39
+ testingHistoryCap = 50
40
+ /**
41
+ * Recent Testing-tab sends for this pico, keyed by ruleset rid (newest first).
42
+ * @param rid String optional "ruleset id; omit for the full map rid → entries"
43
+ * @returns Map or Array
44
+ */
45
+ testingHistory = function(rid = null) {
46
+ rid.isnull() || rid == "" => ent:testing_history.defaultsTo({}) |
47
+ ent:testing_history{rid}.defaultsTo([])
48
+ }
49
+ /**
50
+ * Named Testing-tab requests kept on this pico, keyed by ruleset rid.
51
+ * @param rid String optional ruleset id; omit for the full map rid → entries
52
+ * @returns Map or Array
53
+ */
54
+ testingSaved = function(rid = null) {
55
+ rid.isnull() || rid == "" => ent:testing_saved.defaultsTo({}) |
56
+ ent:testing_saved{rid}.defaultsTo([])
57
+ }
58
+ /**
59
+ * This pico's display name in the developer UI ("Pico" if never set).
60
+ * @returns String
61
+ */
29
62
  name = function(){
30
63
  ent:name || "Pico"
31
64
  }
32
65
  needsStartupLayout = function(){
33
66
  ent:x.isnull() && ent:y.isnull()
34
67
  }
68
+ /**
69
+ * How the developer UI draws this pico: name, color, position, size, and the
70
+ * UI channels of its parent and children.
71
+ * @returns Map "{ eci, parent, children, name, backgroundColor, x, y, width, height }"
72
+ */
35
73
  box = function(){
36
74
  return {
37
75
  "eci": uiECI(),
@@ -41,10 +79,15 @@ ruleset io.picolabs.pico-engine-ui {
41
79
  "backgroundColor": ent:backgroundColor || "#87cefa",
42
80
  "x": ent:x || 100,
43
81
  "y": ent:y || 100,
44
- "width": ent:width || 100,
45
- "height": ent:height || 100
82
+ "width": ent:width || 125,
83
+ "height": ent:height || 90
46
84
  }
47
85
  }
86
+ /**
87
+ * This pico's channels (with their policies) and installed rulesets, plus the
88
+ * UI channels of its parent and children. A quick way to see what a pico is.
89
+ * @returns Map "{ eci, parent, children, channels, rulesets }"
90
+ */
48
91
  pico = function(){
49
92
  return {
50
93
  "eci": uiECI(),
@@ -54,78 +97,14 @@ ruleset io.picolabs.pico-engine-ui {
54
97
  "rulesets": ctx:rulesets
55
98
  }
56
99
  }
100
+ /**
101
+ * Recent activity on this pico, newest first. Each transaction is a list of
102
+ * steps (`in`, `rule`, `raise`, `klog`, `log`, `error`, `done`) assembled
103
+ * from the engine log. The Logging tab hides engine rulesets in the browser.
104
+ * @returns Array "[ { txnId, kind, start, durationMs, outcome, steps } ]"
105
+ */
57
106
  logs = function(){
58
- logOther = function(entry){
59
- entry.delete("time")
60
- .delete("level")
61
- .delete("msg")
62
- .delete("txnId")
63
- .encode()
64
- }
65
- logQuery = function(query){
66
- args = query.get("args").delete("_headers") || {}
67
- <<QUERY #{query.get("eci")} #{query.get("rid")}/#{query.get("name")} #{args.encode()}>>
68
- }
69
- logEvent = function(event){
70
- attrs = event.get(["data","attrs"]).delete("_headers") || {}
71
- <<EVENT #{event.get("eci")} #{event.get("domain")}:#{event.get("name")} #{attrs.encode()}>>
72
- }
73
- logFirst = function(entry){
74
- txn = entry.get("txn")
75
- kind = txn.get("kind")
76
- kind == "query" => logQuery(txn.get("query")) |
77
- kind == "event" => logEvent(txn.get("event")) |
78
- logOther(entry)
79
- }
80
- logDetails = function(entry){
81
- msg = entry.get("msg")
82
- msg == "txnQueued" => logFirst(entry) |
83
- msg == "event added to schedule" => entry.get("event").encode() |
84
- msg == "rule selected" => <<#{entry.get("rid")} : #{entry.get("rule_name")}>> |
85
- entry.get("level") == "klog" => entry.get("val").encode() || "null" |
86
- msg.match(re#fired$#) => "" |
87
- logOther(entry)
88
- }
89
- episode_line = function(x,i){
90
- level = x{"level"}.uc();
91
- x{"time"}.split("T")[1] +
92
- " [" +
93
- level +
94
- "] "+
95
- x{"msg"} +
96
- " " +
97
- logDetails(x)
98
- };
99
- entryMap = function(a,e){
100
- // a.head() is array of entries; a[1] is whether last entry is eats
101
- // a[2] is domain:name of the event
102
- eats = e{"msg"} == "event added to schedule"
103
- event_dt = eats => e{["event","domain"]} + ":" + e{["event","name"]}
104
- | ""
105
- a[1] && eats && a[2]==event_dt
106
- => [a.head(),true,event_dt] // omit a duplicate eats
107
- | [a.head().append(episode_line(e)),eats,event_dt]
108
- }
109
- episodes = ctx:logs()
110
- .collect(function(e){e.get("txnId")})
111
- episodes.keys()
112
- .map(function(k){
113
- episode = episodes.get(k)
114
- entries = episode
115
- .reduce(entryMap,[[],false,""]).head()
116
- return {
117
- "txnId": k,
118
- "time": episode.head().get("time"),
119
- "header": entries.head().split(re# txnQueued #)[1],
120
- "entries": entries,
121
- }
122
- })
123
- .filter(function(g){
124
- g{"header"}.match(re#^QUERY .*io.picolabs.pico-engine-ui/#) => false |
125
- g{"header"}.match(re#^QUERY .*io.picolabs.subscription/established#) => false |
126
- true
127
- })
128
- .reverse()
107
+ ctx:logTransactions()
129
108
  }
130
109
  uiChannelEventPolicy = function() {
131
110
  {
@@ -141,7 +120,12 @@ ruleset io.picolabs.pico-engine-ui {
141
120
  { "domain": "engine_ui", "name": "del_channel" },
142
121
  { "domain": "engine_ui", "name": "update_channel" },
143
122
  { "domain": "engine_ui", "name": "testing_eci" },
123
+ { "domain": "engine_ui", "name": "testing_history_record" },
124
+ { "domain": "engine_ui", "name": "testing_history_clear" },
125
+ { "domain": "engine_ui", "name": "testing_saved_upsert" },
126
+ { "domain": "engine_ui", "name": "testing_saved_delete" },
144
127
  { "domain": "engine", "name": "started" },
128
+ { "domain": "engine", "name": "ruleset_set_changed" },
145
129
  { "domain": "wrangler", "name": "subscription" },
146
130
  { "domain": "wrangler", "name": "relationship" },
147
131
  { "domain": "wrangler", "name": "pending_subscription_approval" },
@@ -154,7 +138,12 @@ ruleset io.picolabs.pico-engine-ui {
154
138
  { "domain": "wrangler", "name": "relationship_cancellation" },
155
139
  { "domain": "wrangler", "name": "set_public_intro" },
156
140
  { "domain": "wrangler", "name": "send_event_on_subs" },
157
- { "domain": "wrangler", "name": "send_event_on_relationships" }
141
+ { "domain": "wrangler", "name": "send_event_on_relationships" },
142
+ { "domain": "wrangler", "name": "child_deleted" },
143
+ { "domain": "wrangler", "name": "new_child_request" },
144
+ { "domain": "wrangler", "name": "new_child_created" },
145
+ { "domain": "wrangler", "name": "install_ruleset_request" },
146
+ { "domain": "wrangler", "name": "new_channel_request" }
158
147
  ],
159
148
  "deny": []
160
149
  }
@@ -163,18 +152,23 @@ ruleset io.picolabs.pico-engine-ui {
163
152
  {
164
153
  "allow": [
165
154
  { "rid": "*", "name": "__testing" },
155
+ { "rid": "*", "name": "__consoleEval" },
166
156
  { "rid": "io.picolabs.pico-engine-ui", "name": "uiECI" },
167
157
  { "rid": "io.picolabs.pico-engine-ui", "name": "box" },
168
158
  { "rid": "io.picolabs.pico-engine-ui", "name": "pico" },
169
159
  { "rid": "io.picolabs.pico-engine-ui", "name": "logs" },
170
160
  { "rid": "io.picolabs.pico-engine-ui", "name": "testingECI" },
161
+ { "rid": "io.picolabs.pico-engine-ui", "name": "testingHistory" },
162
+ { "rid": "io.picolabs.pico-engine-ui", "name": "testingSaved" },
171
163
  { "rid": "io.picolabs.pico-engine-ui", "name": "name" },
172
164
  { "rid": "io.picolabs.subscription", "name": "established" },
173
165
  { "rid": "io.picolabs.subscription", "name": "inbound" },
174
166
  { "rid": "io.picolabs.subscription", "name": "outbound" },
175
167
  { "rid": "io.picolabs.subscription", "name": "wellKnown_Rx" },
176
168
  { "rid": "io.picolabs.wrangler", "name": "myDid" },
177
- { "rid": "io.picolabs.wrangler", "name": "publicIntro" }
169
+ { "rid": "io.picolabs.wrangler", "name": "publicIntro" },
170
+ { "rid": "io.picolabs.wrangler", "name": "rulesetSchema" },
171
+ { "rid": "io.picolabs.wrangler", "name": "picoSchema" }
178
172
  ],
179
173
  "deny": []
180
174
  }
@@ -183,6 +177,12 @@ ruleset io.picolabs.pico-engine-ui {
183
177
  "#FFB347", "#77DD77", "#B19CD9", "#FF6961", "#FDFD96",
184
178
  "#84B6F4", "#FFD1DC", "#C9A0DC", "#98FB98", "#E6E6FA"
185
179
  ]
180
+ // Grid slot from live children only — not ent:child_ui_ecis map size (stale
181
+ // entries survive pico deletion and were inflating layout Y on every recreate).
182
+ childLayoutIndex = function(familyEci) {
183
+ idx = ctx:children.index(familyEci)
184
+ idx.isnull() => ctx:children.length() - 1 | idx
185
+ }
186
186
  refreshUiChannel = defaction() {
187
187
  ctx:upsertChannel(
188
188
  tags = ["engine", "ui"],
@@ -191,6 +191,9 @@ ruleset io.picolabs.pico-engine-ui {
191
191
  )
192
192
  }
193
193
  }
194
+ // engine_ui events are the developer UI's private protocol and are deliberately
195
+ // not discoverable. Their public equivalents are wrangler events
196
+ // (new_child_request, install_ruleset_request, new_channel_request, …).
194
197
  rule setup {
195
198
  select when engine_ui setup
196
199
  refreshUiChannel()
@@ -218,12 +221,15 @@ ruleset io.picolabs.pico-engine-ui {
218
221
  pre {
219
222
  uiEci = event:attr("uiEci")
220
223
  familyEci = event:attr("eci")
221
- childIndex = ent:child_ui_ecis.defaultsTo({}).keys().length()
224
+ requestedColor = event:attrs{"backgroundColor"}
225
+ childIndex = childLayoutIndex(familyEci)
222
226
  gridCol = childIndex % 4
223
227
  gridRow = (childIndex - childIndex % 4) / 4
224
- layoutX = ent:x.defaultsTo(100) + 48 + gridCol * 118
225
- layoutY = ent:y.defaultsTo(100) + 48 + gridRow * 98
226
- layoutColor = childPalette[childIndex % childPalette.length]
228
+ layoutX = ent:x.defaultsTo(100) + 48 + gridCol * 143
229
+ layoutY = ent:y.defaultsTo(100) + 48 + gridRow * 108
230
+ layoutColor = requestedColor.match(re#^\#[0-9A-F]{6}$#i)
231
+ => requestedColor
232
+ | childPalette[childIndex % childPalette.length]
227
233
  }
228
234
  if uiEci && familyEci then
229
235
  ctx:event(
@@ -233,8 +239,8 @@ ruleset io.picolabs.pico-engine-ui {
233
239
  attrs={
234
240
  "x": layoutX,
235
241
  "y": layoutY,
236
- "width": 100,
237
- "height": 100,
242
+ "width": 125,
243
+ "height": 90,
238
244
  "backgroundColor": layoutColor,
239
245
  "parentUiEci": uiECI()
240
246
  }
@@ -245,18 +251,17 @@ ruleset io.picolabs.pico-engine-ui {
245
251
  }
246
252
  rule spread_children_on_startup {
247
253
  select when engine started
248
- foreach ent:child_ui_ecis.defaultsTo({}).keys() setting(familyEci)
254
+ foreach ctx:children setting(familyEci)
249
255
  pre {
250
256
  uiEci = ent:child_ui_ecis{familyEci}
251
- childKeys = ent:child_ui_ecis.defaultsTo({}).keys()
252
- childIndex = childKeys.index(familyEci)
253
- originX = ent:x.defaultsTo(100)
254
- originY = ent:y.defaultsTo(100)
257
+ childIndex = childLayoutIndex(familyEci)
255
258
  gridCol = childIndex % 4
256
259
  gridRow = (childIndex - childIndex % 4) / 4
257
- layoutX = originX + 48 + gridCol * 118
258
- layoutY = originY + 48 + gridRow * 98
259
- layoutColor = childPalette[childIndex % childPalette.length]
260
+ layoutX = ent:x.defaultsTo(100) + 48 + gridCol * 143
261
+ layoutY = ent:y.defaultsTo(100) + 48 + gridRow * 108
262
+ childBox = ctx:query(uiEci, ctx:rid, "box")
263
+ layoutColor = childBox{"backgroundColor"}
264
+ .defaultsTo(childPalette[childIndex % childPalette.length])
260
265
  }
261
266
  if uiEci && ctx:query(uiEci, ctx:rid, "needsStartupLayout") then
262
267
  ctx:event(
@@ -266,8 +271,8 @@ ruleset io.picolabs.pico-engine-ui {
266
271
  attrs={
267
272
  "x": layoutX,
268
273
  "y": layoutY,
269
- "width": 100,
270
- "height": 100,
274
+ "width": 125,
275
+ "height": 90,
271
276
  "backgroundColor": layoutColor,
272
277
  "parentUiEci": uiECI()
273
278
  }
@@ -281,6 +286,21 @@ ruleset io.picolabs.pico-engine-ui {
281
286
  ent:child_ui_ecis{[familyEci]} := ctx:query(familyEci, ctx:rid, "uiECI")
282
287
  }
283
288
  }
289
+ rule prune_stale_child_ui_ecis {
290
+ select when engine started
291
+ foreach ent:child_ui_ecis.defaultsTo({}).keys() setting(familyEci)
292
+ always {
293
+ clear ent:child_ui_ecis{familyEci}
294
+ if ctx:children.index(familyEci).isnull()
295
+ }
296
+ }
297
+ rule forget_child_ui_eci {
298
+ select when wrangler child_deleted
299
+ eci re#(.+)# setting(familyEci)
300
+ always {
301
+ clear ent:child_ui_ecis{familyEci}
302
+ }
303
+ }
284
304
  rule backfill_parent_ui_eci {
285
305
  select when engine started
286
306
  if ctx:parent && ent:parent_ui_eci.isnull() then noop()
@@ -312,6 +332,9 @@ ruleset io.picolabs.pico-engine-ui {
312
332
  .head()
313
333
  }
314
334
  ctx:delPico(delEci)
335
+ always {
336
+ clear ent:child_ui_ecis{delEci} if delEci
337
+ }
315
338
  }
316
339
  rule install {
317
340
  select when engine_ui install
@@ -333,16 +356,51 @@ ruleset io.picolabs.pico-engine-ui {
333
356
  }
334
357
  rule flush {
335
358
  select when engine_ui flush
336
- ctx:flush(url=event:attrs{"url"})
359
+ pre {
360
+ url = event:attrs{"url"}
361
+ validation = ctx:validate(url)
362
+ ok = validation{"ok"}
363
+ validate_errors = validation{"errors"}
364
+ validate_warnings = validation{"warnings"}
365
+ validate_rid = validation{"rid"}
366
+ }
367
+ if ok then every {
368
+ ctx:flush(url=url)
369
+ }
370
+ fired {
371
+ raise engine_ui event "flush_succeeded" attributes {"url": url}
372
+ }
373
+ else {
374
+ raise wrangler event "ruleset_flush_failed" attributes
375
+ {"url": url, "rid": validate_rid, "errors": validate_errors, "warnings": validate_warnings}
376
+ }
337
377
  }
338
378
  rule refresh_ui_channel_after_flush {
339
- select when engine_ui flush
379
+ select when engine_ui flush_succeeded
340
380
  pre {
341
381
  url = event:attrs{"url"} || ""
342
382
  self_flush = url.match(re#io\.picolabs\.pico-engine-ui#)
343
383
  }
344
384
  if self_flush then refreshUiChannel()
345
385
  }
386
+ // Reinstall the flushed ruleset on this pico so the new KRL takes effect.
387
+ // The rid comes from the installed ruleset whose url matches: the UI sends the
388
+ // url from pico(){"rulesets"}, so it is exactly the installed ruleset's url.
389
+ // (Previously the rid was parsed out of the url, which never matched —
390
+ // substr(0, 11) is "io.picolabs" without the dot — and truncated dotted rids.)
391
+ rule reinstall_after_flush {
392
+ select when engine_ui flush_succeeded
393
+ pre {
394
+ url = event:attrs{"url"} || ""
395
+ rid = ctx:rulesets
396
+ .filter(function(rs){ rs{"url"} == url })
397
+ .head(){"rid"}
398
+ }
399
+ if url && rid then noop()
400
+ fired {
401
+ raise wrangler event "install_ruleset_request" attributes {"rid": rid, "url": url}
402
+ }
403
+ }
346
404
  rule new_channel {
347
405
  select when engine_ui new_channel
348
406
  ctx:newChannel(
@@ -371,4 +429,69 @@ ruleset io.picolabs.pico-engine-ui {
371
429
  ent:testingECI := event:attrs{"eci"}
372
430
  }
373
431
  }
432
+
433
+ rule testing_history_record {
434
+ select when engine_ui testing_history_record
435
+ pre {
436
+ rid = event:attrs{"rid"}
437
+ entry = event:attrs{"entry"}
438
+ prior = ent:testing_history{rid}.defaultsTo([])
439
+ merged = [entry].append(prior)
440
+ }
441
+ if rid && entry then noop()
442
+ fired {
443
+ ent:testing_history := ent:testing_history.defaultsTo({})
444
+ ent:testing_history{rid} := merged.length() > testingHistoryCap
445
+ => merged.slice(0, testingHistoryCap) |
446
+ merged
447
+ }
448
+ }
449
+
450
+ rule testing_history_clear {
451
+ select when engine_ui testing_history_clear
452
+ pre {
453
+ rid = event:attrs{"rid"}
454
+ clearAll = rid.isnull() || rid == ""
455
+ }
456
+ if clearAll then noop()
457
+ fired {
458
+ ent:testing_history := {}
459
+ }
460
+ else {
461
+ ent:testing_history := ent:testing_history.defaultsTo({})
462
+ ent:testing_history{rid} := []
463
+ }
464
+ }
465
+
466
+ rule testing_saved_upsert {
467
+ select when engine_ui testing_saved_upsert
468
+ pre {
469
+ rid = event:attrs{"rid"}
470
+ entry = event:attrs{"entry"}
471
+ entryId = entry.get("id")
472
+ prior = ent:testing_saved{rid}.defaultsTo([])
473
+ rest = prior.filter(function(e){e.get("id") != entryId})
474
+ merged = [entry].append(rest)
475
+ }
476
+ if rid && entry && entryId then noop()
477
+ fired {
478
+ ent:testing_saved := ent:testing_saved.defaultsTo({})
479
+ ent:testing_saved{rid} := merged
480
+ }
481
+ }
482
+
483
+ rule testing_saved_delete {
484
+ select when engine_ui testing_saved_delete
485
+ pre {
486
+ rid = event:attrs{"rid"}
487
+ id = event:attrs{"id"}
488
+ prior = ent:testing_saved{rid}.defaultsTo([])
489
+ merged = prior.filter(function(e){e.get("id") != id})
490
+ }
491
+ if rid && id then noop()
492
+ fired {
493
+ ent:testing_saved := ent:testing_saved.defaultsTo({})
494
+ ent:testing_saved{rid} := merged
495
+ }
496
+ }
374
497
  }
@@ -0,0 +1,97 @@
1
+ ruleset io.picolabs.portable_subtree {
2
+ meta {
3
+ name "Portable subtree"
4
+ description <<
5
+ Optional wrangler utility for copying a pico subtree (rulesets, entity state,
6
+ channels, schedules, and Layer 2 identity) to another parent — on the same
7
+ engine or a different one. Not installed by default.
8
+
9
+ Export (install on an ancestor of the subtree to copy):
10
+ portable_subtree:exportSubtree(subtreeRootPicoId, secret)
11
+ → encrypted blob (transfer manually to the destination engine)
12
+
13
+ Inspect a blob before import (same secret):
14
+ portable_subtree:summarizeExportBlob(blob, secret)
15
+
16
+ Import (install on the adopting parent):
17
+ raise portable_subtree event "import"
18
+ attributes { "blob": "...", "secret": "..." }
19
+ → portable_subtree:imported event; read result with portable_subtree:lastImport()
20
+
21
+ The importer must ensure the recipient mesh provides any platform services
22
+ the subtree relied on from ancestors in the donor mesh (e.g. import a
23
+ Manifold app pico under a Manifold parent, not a bare wrangler root).
24
+
25
+ After import, delete the source subtree manually if you no longer need it.
26
+ >>
27
+ author "PICOLABS"
28
+
29
+ provides exportSubtree, lastImport, summarizeExportBlob
30
+ shares exportSubtree, lastImport, summarizeExportBlob
31
+ }
32
+
33
+ global {
34
+ /**
35
+ * Export a descendant subtree (or this pico itself) as an encrypted blob:
36
+ * rulesets, entity state, channels, schedules, and Layer 2 identity. Read-only;
37
+ * the source subtree is untouched. Transfer the blob to the adopting parent and
38
+ * send it portable_subtree:import there with the same secret.
39
+ * @param subtreeRootPicoId String "pico id of the subtree root; this pico's own id exports this pico"
40
+ * @param secret String "encryption secret; the importer needs the same one"
41
+ * @returns String "encrypted export blob"
42
+ */
43
+ exportSubtree = function(subtreeRootPicoId, secret) {
44
+ dido:exportPortableSubtree(subtreeRootPicoId, secret)
45
+ }
46
+
47
+ /**
48
+ * Result of the most recent portable_subtree:import on this pico (the same
49
+ * attrs as the portable_subtree:imported event), or null if none.
50
+ * @returns Map
51
+ */
52
+ lastImport = function() {
53
+ ent:lastImport
54
+ }
55
+
56
+ /**
57
+ * Inspect an export blob before importing it: subtree root name, relationship
58
+ * count, rulesets with entity state, and similar. Read-only.
59
+ * @param blob String "export blob from exportSubtree"
60
+ * @param secret String "the secret used to export it"
61
+ * @returns Map "{ wranglerName, establishedCount, entRulesets, … }"
62
+ */
63
+ summarizeExportBlob = function(blob, secret) {
64
+ dido:summarizePortableSubtreeExport(blob, secret)
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Import an exported subtree as a child of this pico. Rulesets, entity state,
70
+ * channels, schedules, and identity are restored, and Layer 2 relationships are
71
+ * relocated so they survive the move. The result is stored for lastImport() and
72
+ * raised as portable_subtree:imported. This pico must provide any platform
73
+ * services the subtree relied on in its old mesh (e.g. import a Manifold app
74
+ * pico under a Manifold parent). The source subtree is not deleted.
75
+ * @attr blob String "export blob from exportSubtree"
76
+ * @attr secret String "the secret used to export it"
77
+ * @attr renewIngress Boolean optional "mint a fresh DIDComm ingress channel when relocating each pico's did:webvh (default true)"
78
+ * @attr bootstrapDisplayName String optional "display name for the imported root"
79
+ * @example "import" { "blob": "<blob from exportSubtree>", "secret": "<secret>" }
80
+ */
81
+ rule import_subtree {
82
+ select when portable_subtree import
83
+ pre {
84
+ blob = event:attr("blob")
85
+ secret = event:attr("secret")
86
+ result = dido:importPortableSubtree(blob, secret, {
87
+ "renewIngress": event:attr("renewIngress").defaultsTo(true),
88
+ "parentChannelEci": event:eci,
89
+ "bootstrapDisplayName": event:attr("bootstrapDisplayName")
90
+ })
91
+ }
92
+ fired {
93
+ ent:lastImport := result
94
+ raise portable_subtree event "imported" attributes result
95
+ }
96
+ }
97
+ }