@cello-protocol/cli 0.0.305 → 0.0.307

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 (234) hide show
  1. package/.claude-plugin/marketplace.json +15 -0
  2. package/README.md +107 -0
  3. package/dist/cli.d.ts +2 -0
  4. package/dist/cli.js +564 -0
  5. package/dist/daemon/account-page-assets.d.ts +4 -0
  6. package/dist/daemon/account-page-assets.js +160 -0
  7. package/dist/daemon/account-page.d.ts +14 -0
  8. package/dist/daemon/account-page.js +104 -0
  9. package/dist/daemon/address.d.ts +10 -0
  10. package/dist/daemon/address.js +34 -0
  11. package/dist/daemon/away.d.ts +31 -0
  12. package/dist/daemon/away.js +65 -0
  13. package/dist/daemon/channel-store.d.ts +112 -0
  14. package/dist/daemon/channel-store.js +284 -0
  15. package/dist/daemon/channels.d.ts +46 -0
  16. package/dist/daemon/channels.js +1049 -0
  17. package/dist/daemon/contacts.d.ts +26 -0
  18. package/dist/daemon/contacts.js +74 -0
  19. package/dist/daemon/engine.d.ts +80 -0
  20. package/dist/daemon/engine.js +2882 -0
  21. package/dist/daemon/errors.d.ts +5 -0
  22. package/dist/daemon/errors.js +16 -0
  23. package/dist/daemon/governance.d.ts +66 -0
  24. package/dist/daemon/governance.js +514 -0
  25. package/dist/daemon/guards.d.ts +36 -0
  26. package/dist/daemon/guards.js +140 -0
  27. package/dist/daemon/issuer-client.d.ts +56 -0
  28. package/dist/daemon/issuer-client.js +143 -0
  29. package/dist/daemon/link.d.ts +30 -0
  30. package/dist/daemon/link.js +191 -0
  31. package/dist/daemon/log.d.ts +1 -0
  32. package/dist/daemon/log.js +4 -0
  33. package/dist/daemon/main.d.ts +1 -0
  34. package/dist/daemon/main.js +511 -0
  35. package/dist/daemon/paths.d.ts +6 -0
  36. package/dist/daemon/paths.js +21 -0
  37. package/dist/daemon/pii.d.ts +24 -0
  38. package/dist/daemon/pii.js +146 -0
  39. package/dist/daemon/policy-cadence.d.ts +33 -0
  40. package/dist/daemon/policy-cadence.js +59 -0
  41. package/dist/daemon/policy-proposals.d.ts +41 -0
  42. package/dist/daemon/policy-proposals.js +83 -0
  43. package/dist/daemon/policy-store.d.ts +47 -0
  44. package/dist/daemon/policy-store.js +144 -0
  45. package/dist/daemon/presented.d.ts +36 -0
  46. package/dist/daemon/presented.js +94 -0
  47. package/dist/daemon/rate-limit.d.ts +17 -0
  48. package/dist/daemon/rate-limit.js +44 -0
  49. package/dist/daemon/reach.d.ts +63 -0
  50. package/dist/daemon/reach.js +207 -0
  51. package/dist/daemon/screening.d.ts +97 -0
  52. package/dist/daemon/screening.js +362 -0
  53. package/dist/daemon/settings.d.ts +125 -0
  54. package/dist/daemon/settings.js +73 -0
  55. package/dist/daemon/signal-rules.d.ts +55 -0
  56. package/dist/daemon/signal-rules.js +127 -0
  57. package/dist/daemon/signal-store.d.ts +53 -0
  58. package/dist/daemon/signal-store.js +114 -0
  59. package/dist/daemon/store.d.ts +188 -0
  60. package/dist/daemon/store.js +474 -0
  61. package/dist/daemon/tiers.d.ts +14 -0
  62. package/dist/daemon/tiers.js +9 -0
  63. package/dist/daemon/wallet.d.ts +73 -0
  64. package/dist/daemon/wallet.js +428 -0
  65. package/dist/daemon/who-label.d.ts +3 -0
  66. package/dist/daemon/who-label.js +11 -0
  67. package/dist/guidance.d.ts +39 -0
  68. package/dist/guidance.js +98 -0
  69. package/dist/hermes-bridge.d.ts +26 -0
  70. package/dist/hermes-bridge.js +180 -0
  71. package/dist/install-mcp.d.ts +6 -0
  72. package/dist/install-mcp.js +77 -0
  73. package/dist/ipc/client.d.ts +18 -0
  74. package/dist/ipc/client.js +107 -0
  75. package/dist/ipc/operations.d.ts +30 -0
  76. package/dist/ipc/operations.js +835 -0
  77. package/dist/ipc/protocol.d.ts +21 -0
  78. package/dist/ipc/protocol.js +1 -0
  79. package/dist/issuer-list-sign.d.ts +12 -0
  80. package/dist/issuer-list-sign.js +43 -0
  81. package/dist/local-operations.d.ts +2 -0
  82. package/dist/local-operations.js +73 -0
  83. package/dist/mcp-tools.d.ts +14 -0
  84. package/dist/mcp-tools.js +45 -0
  85. package/dist/mcp.d.ts +2 -0
  86. package/dist/mcp.js +155 -0
  87. package/dist/protocol/aead.d.ts +4 -0
  88. package/dist/protocol/aead.js +35 -0
  89. package/dist/protocol/cbor.d.ts +40 -0
  90. package/dist/protocol/cbor.js +106 -0
  91. package/dist/protocol/chain.d.ts +44 -0
  92. package/dist/protocol/chain.js +126 -0
  93. package/dist/protocol/channel.d.ts +153 -0
  94. package/dist/protocol/channel.js +298 -0
  95. package/dist/protocol/encode.d.ts +6 -0
  96. package/dist/protocol/encode.js +21 -0
  97. package/dist/protocol/identity.d.ts +27 -0
  98. package/dist/protocol/identity.js +94 -0
  99. package/dist/protocol/issuer-list.d.ts +56 -0
  100. package/dist/protocol/issuer-list.js +193 -0
  101. package/dist/protocol/issuer-roots.d.ts +6 -0
  102. package/dist/protocol/issuer-roots.js +10 -0
  103. package/dist/protocol/key-agreement.d.ts +20 -0
  104. package/dist/protocol/key-agreement.js +88 -0
  105. package/dist/protocol/merkle.d.ts +16 -0
  106. package/dist/protocol/merkle.js +166 -0
  107. package/dist/protocol/ml-dsa.d.ts +28 -0
  108. package/dist/protocol/ml-dsa.js +58 -0
  109. package/dist/protocol/ml-kem.d.ts +14 -0
  110. package/dist/protocol/ml-kem.js +57 -0
  111. package/dist/protocol/pq-errors.d.ts +9 -0
  112. package/dist/protocol/pq-errors.js +23 -0
  113. package/dist/protocol/pq-warnings.d.ts +1 -0
  114. package/dist/protocol/pq-warnings.js +18 -0
  115. package/dist/protocol/presented-signals.d.ts +17 -0
  116. package/dist/protocol/presented-signals.js +92 -0
  117. package/dist/protocol/salt.d.ts +5 -0
  118. package/dist/protocol/salt.js +47 -0
  119. package/dist/protocol/seal.d.ts +58 -0
  120. package/dist/protocol/seal.js +220 -0
  121. package/dist/protocol/signal.d.ts +54 -0
  122. package/dist/protocol/signal.js +266 -0
  123. package/dist/protocol/status.d.ts +41 -0
  124. package/dist/protocol/status.js +64 -0
  125. package/dist/protocol/structures.d.ts +359 -0
  126. package/dist/protocol/structures.js +296 -0
  127. package/dist/protocol/transcript.d.ts +61 -0
  128. package/dist/protocol/transcript.js +319 -0
  129. package/dist/protocol/wire.d.ts +212 -0
  130. package/dist/protocol/wire.js +161 -0
  131. package/dist/require-node.d.ts +1 -0
  132. package/dist/require-node.js +10 -0
  133. package/dist/screener/affordance.d.ts +1 -0
  134. package/dist/screener/affordance.js +2 -0
  135. package/dist/screener/classifier-selfcheck.d.ts +29 -0
  136. package/dist/screener/classifier-selfcheck.js +56 -0
  137. package/dist/screener/exfil.d.ts +11 -0
  138. package/dist/screener/exfil.js +45 -0
  139. package/dist/screener/gitleaks-rules.d.ts +9 -0
  140. package/dist/screener/gitleaks-rules.js +226 -0
  141. package/dist/screener/inbound.d.ts +25 -0
  142. package/dist/screener/inbound.js +102 -0
  143. package/dist/screener/injection-classifier-onnx.d.ts +20 -0
  144. package/dist/screener/injection-classifier-onnx.js +89 -0
  145. package/dist/screener/injection-classifier-wasm.d.ts +2 -0
  146. package/dist/screener/injection-classifier-wasm.js +97 -0
  147. package/dist/screener/injection-patterns.d.ts +8 -0
  148. package/dist/screener/injection-patterns.js +48 -0
  149. package/dist/screener/injection-windows.d.ts +9 -0
  150. package/dist/screener/injection-windows.js +37 -0
  151. package/dist/screener/language.d.ts +15 -0
  152. package/dist/screener/language.js +71 -0
  153. package/dist/screener/linear-regex.d.ts +8 -0
  154. package/dist/screener/linear-regex.js +27 -0
  155. package/dist/screener/model-installer.d.ts +14 -0
  156. package/dist/screener/model-installer.js +61 -0
  157. package/dist/screener/outbound.d.ts +19 -0
  158. package/dist/screener/outbound.js +62 -0
  159. package/dist/screener/sanitize.d.ts +44 -0
  160. package/dist/screener/sanitize.js +361 -0
  161. package/dist/screener/scan-variants.d.ts +5 -0
  162. package/dist/screener/scan-variants.js +146 -0
  163. package/dist/screener/screener-model-manifest.d.ts +16 -0
  164. package/dist/screener/screener-model-manifest.js +20 -0
  165. package/dist/screener/screener-state.d.ts +29 -0
  166. package/dist/screener/screener-state.js +119 -0
  167. package/dist/screener/secrets.d.ts +18 -0
  168. package/dist/screener/secrets.js +84 -0
  169. package/dist/screener/worker.d.ts +6 -0
  170. package/dist/screener/worker.js +83 -0
  171. package/dist/setup.d.ts +9 -0
  172. package/dist/setup.js +252 -0
  173. package/dist/shipped-issuer-list.d.ts +1 -0
  174. package/dist/shipped-issuer-list.js +3 -0
  175. package/dist/signals-text.d.ts +46 -0
  176. package/dist/signals-text.js +81 -0
  177. package/dist/verify-seal.d.ts +9 -0
  178. package/dist/verify-seal.js +30 -0
  179. package/hermes/plugin/__init__.py +1040 -0
  180. package/hermes/plugin/plugin.yaml +30 -0
  181. package/hermes/skills/cello-bridge-setup/SKILL.md +38 -0
  182. package/hermes/skills/cello-policy/SKILL.md +44 -0
  183. package/package.json +48 -16
  184. package/plugins/cello/.claude-plugin/plugin.json +19 -0
  185. package/plugins/cello/.mcp.json +7 -0
  186. package/plugins/cello/skills/cello/SKILL.md +117 -0
  187. package/dist/arg-parse.d.ts +0 -19
  188. package/dist/arg-parse.d.ts.map +0 -1
  189. package/dist/arg-parse.js +0 -29
  190. package/dist/arg-parse.js.map +0 -1
  191. package/dist/bin/cello.d.ts +0 -15
  192. package/dist/bin/cello.d.ts.map +0 -1
  193. package/dist/bin/cello.js +0 -110
  194. package/dist/bin/cello.js.map +0 -1
  195. package/dist/cli-args.d.ts +0 -62
  196. package/dist/cli-args.d.ts.map +0 -1
  197. package/dist/cli-args.js +0 -129
  198. package/dist/cli-args.js.map +0 -1
  199. package/dist/commands.d.ts +0 -196
  200. package/dist/commands.d.ts.map +0 -1
  201. package/dist/commands.js +0 -1544
  202. package/dist/commands.js.map +0 -1
  203. package/dist/hermes/assets.d.ts +0 -41
  204. package/dist/hermes/assets.d.ts.map +0 -1
  205. package/dist/hermes/assets.js +0 -2281
  206. package/dist/hermes/assets.js.map +0 -1
  207. package/dist/hermes/install-hermes.d.ts +0 -50
  208. package/dist/hermes/install-hermes.d.ts.map +0 -1
  209. package/dist/hermes/install-hermes.js +0 -232
  210. package/dist/hermes/install-hermes.js.map +0 -1
  211. package/dist/json-out.d.ts +0 -41
  212. package/dist/json-out.d.ts.map +0 -1
  213. package/dist/json-out.js +0 -59
  214. package/dist/json-out.js.map +0 -1
  215. package/dist/node-runtime-guard-init.d.ts +0 -6
  216. package/dist/node-runtime-guard-init.d.ts.map +0 -1
  217. package/dist/node-runtime-guard-init.js +0 -7
  218. package/dist/node-runtime-guard-init.js.map +0 -1
  219. package/dist/node-runtime-guard.d.ts +0 -48
  220. package/dist/node-runtime-guard.d.ts.map +0 -1
  221. package/dist/node-runtime-guard.js +0 -79
  222. package/dist/node-runtime-guard.js.map +0 -1
  223. package/dist/parity-commands.d.ts +0 -427
  224. package/dist/parity-commands.d.ts.map +0 -1
  225. package/dist/parity-commands.js +0 -953
  226. package/dist/parity-commands.js.map +0 -1
  227. package/dist/registry.d.ts +0 -120
  228. package/dist/registry.d.ts.map +0 -1
  229. package/dist/registry.js +0 -2169
  230. package/dist/registry.js.map +0 -1
  231. package/dist/screener-commands.d.ts +0 -85
  232. package/dist/screener-commands.d.ts.map +0 -1
  233. package/dist/screener-commands.js +0 -313
  234. package/dist/screener-commands.js.map +0 -1
@@ -0,0 +1,362 @@
1
+ // Decides what the screening thread does next. Everything durable is in the entries table: this holds only what's in flight,
2
+ // and it's rebuilt from that table after any restart. The worker does one step when asked, which is what makes a short
3
+ // message wait at most one step, about one model window.
4
+ import { extname } from "node:path";
5
+ import { Worker } from "node:worker_threads";
6
+ import { logEvent } from "./log.js";
7
+ const TELL_CHECK_MS = 1_000;
8
+ const RESTART_BASE_MS = 500;
9
+ const RESTART_MAX_MS = 30_000;
10
+ const OUTBOUND_LIMIT_MS = 30_000;
11
+ // Steps in a screen besides model windows: the sanitizer, one for each scan variant, and the verdict.
12
+ const FIXED_STEPS = 24;
13
+ const WINDOW_BYTES_GUESS = 8_000;
14
+ const workerUrl = () => new URL(`../screener/worker${extname(new URL(import.meta.url).pathname)}`, import.meta.url);
15
+ const NOT_SCREENED = { deterministic: false, model: false, model_failed_copies: 0 };
16
+ export class ScreeningQueue {
17
+ #events;
18
+ // Oldest first, per conversation. Only the head may run, so order within a conversation can't change.
19
+ #sessions = new Map();
20
+ // Whose turn is next, per lane. Keyed by the sender's id, because a limit per session is one a stranger opens more sessions to escape.
21
+ #lastSender = { short: "", long: "" };
22
+ #slots = [];
23
+ settings;
24
+ #nextId = 1;
25
+ #tellTimer;
26
+ #msPerStep = 5;
27
+ #stopped = false;
28
+ restarts = 0;
29
+ lastCrash = null;
30
+ model = null;
31
+ constructor(events, settings) {
32
+ this.#events = events;
33
+ this.settings = settings;
34
+ // One thread by default. More are a setting, and only the owner of a machine can say what it can afford.
35
+ for (let i = 0; i < settings.screener_workers; i++)
36
+ this.#slots.push(new Slot(this));
37
+ }
38
+ onModel(model) {
39
+ this.model = model;
40
+ this.#events.modelChecked(model);
41
+ }
42
+ start() {
43
+ for (const slot of this.#slots)
44
+ slot.spawn();
45
+ this.#tellTimer = setInterval(() => this.#tellDelays(), TELL_CHECK_MS);
46
+ this.#tellTimer.unref();
47
+ }
48
+ stop() {
49
+ this.#stopped = true;
50
+ clearInterval(this.#tellTimer);
51
+ for (const slot of this.#slots)
52
+ slot.stop();
53
+ }
54
+ get stopped() {
55
+ return this.#stopped;
56
+ }
57
+ add(job) {
58
+ const key = job.agentHex + job.sessionHex;
59
+ const queue = this.#sessions.get(key) ?? [];
60
+ if (queue.some((queued) => queued.entryId === job.entryId))
61
+ return;
62
+ // Measuring costs a whole tokenizer run, so only what might be short gets it. A wrong guess only delays that one message.
63
+ queue.push({ ...job, id: this.#nextId++, lane: job.bytes <= this.settings.screener_measure_max_bytes ? "short" : "long", started: false, slot: undefined, steps: 0, windows: null, attempts: 0, told: false });
64
+ this.#sessions.set(key, queue);
65
+ this.pump();
66
+ }
67
+ // Takes the worker down and up again, to pick up a newly installed model. Work in flight starts over.
68
+ reload() {
69
+ for (const queue of this.#sessions.values())
70
+ for (const job of queue)
71
+ job.started = false;
72
+ for (const slot of this.#slots) {
73
+ slot.stop();
74
+ slot.spawn();
75
+ }
76
+ }
77
+ pending() {
78
+ const counts = { short: 0, long: 0 };
79
+ for (const queue of this.#sessions.values())
80
+ for (const job of queue)
81
+ counts[job.lane]++;
82
+ return counts;
83
+ }
84
+ // Which position is being waited on in this conversation, and roughly how long until it's done. Null when nothing is.
85
+ waiting(agentHex, sessionHex) {
86
+ const head = this.#sessions.get(agentHex + sessionHex)?.[0];
87
+ return head ? { position: head.position, about_seconds: this.#estimateSeconds(head) } : null;
88
+ }
89
+ #remainingSteps(job) {
90
+ const windows = job.windows ?? (this.model?.running ? Math.ceil(job.bytes / WINDOW_BYTES_GUESS) : 0);
91
+ return Math.max(1, FIXED_STEPS + windows - job.steps);
92
+ }
93
+ // Its own steps, what's ahead of it in its conversation, and the other long messages it takes turns with. Rough by design.
94
+ #estimateSeconds(job) {
95
+ let steps = 0;
96
+ for (const queue of this.#sessions.values()) {
97
+ for (const other of queue) {
98
+ const ahead = queue[0]?.agentHex === job.agentHex && queue[0].sessionHex === job.sessionHex;
99
+ if ((ahead && other.position <= job.position) || (!ahead && other.lane === "long"))
100
+ steps += this.#remainingSteps(other);
101
+ }
102
+ }
103
+ return Math.max(1, Math.ceil(steps * this.#msPerStep / 1000));
104
+ }
105
+ #tellDelays() {
106
+ const now = Date.now();
107
+ for (const queue of this.#sessions.values()) {
108
+ const head = queue[0];
109
+ if (!head || head.told || now - head.arrived < this.settings.screener_tell_after_ms)
110
+ continue;
111
+ head.told = true;
112
+ this.#events.delayed(head, this.#estimateSeconds(head));
113
+ }
114
+ }
115
+ // The next conversation head in this lane, taking turns across senders.
116
+ #pick(lane, slot) {
117
+ const bySender = new Map();
118
+ for (const queue of this.#sessions.values()) {
119
+ const head = queue[0];
120
+ if (!head || head.lane !== lane || bySender.has(head.senderHex) || !this.#free(head, slot))
121
+ continue;
122
+ bySender.set(head.senderHex, head);
123
+ }
124
+ if (bySender.size === 0)
125
+ return undefined;
126
+ const senders = [...bySender.keys()].sort();
127
+ const next = senders.find((sender) => sender > this.#lastSender[lane]) ?? senders[0];
128
+ this.#lastSender[lane] = next;
129
+ return bySender.get(next);
130
+ }
131
+ #free(job, slot) {
132
+ return !this.#slots.some((other) => other.job === job) && (!job.started || job.slot === slot);
133
+ }
134
+ #longInProgress(slot) {
135
+ for (const queue of this.#sessions.values()) {
136
+ const head = queue[0];
137
+ if (head?.lane === "long" && head.started && this.#free(head, slot))
138
+ return head;
139
+ }
140
+ return undefined;
141
+ }
142
+ // Short work goes first, one step at a time. The long lane runs one message at a time, whichever turn it took.
143
+ pump() {
144
+ if (this.#stopped)
145
+ return;
146
+ for (const slot of this.#slots) {
147
+ if (!slot.ready || slot.job)
148
+ continue;
149
+ const job = this.#pick("short", slot) ?? this.#longInProgress(slot) ?? this.#pick("long", slot);
150
+ if (job)
151
+ this.#run(slot, job);
152
+ }
153
+ }
154
+ #run(slot, job) {
155
+ if (job.started) {
156
+ slot.step(job);
157
+ return;
158
+ }
159
+ const loaded = this.#events.load(job);
160
+ if (!loaded) {
161
+ this.#finish(job);
162
+ this.pump();
163
+ return;
164
+ }
165
+ if (loaded.level === "off") {
166
+ this.#finish(job);
167
+ this.#events.verdict(job, { verdict: "allow", delivered: null, findings: [], reason: "its owner turned screening off for this sender", score: null, ran: NOT_SCREENED });
168
+ this.pump();
169
+ return;
170
+ }
171
+ job.started = true;
172
+ job.slot = slot;
173
+ slot.start(job, loaded.body, loaded.level, loaded.language);
174
+ }
175
+ #finish(job) {
176
+ const key = job.agentHex + job.sessionHex;
177
+ const queue = this.#sessions.get(key);
178
+ if (!queue)
179
+ return;
180
+ const at = queue.indexOf(job);
181
+ if (at >= 0)
182
+ queue.splice(at, 1);
183
+ if (queue.length === 0)
184
+ this.#sessions.delete(key);
185
+ }
186
+ onStepped(job, measured, ms) {
187
+ job.steps++;
188
+ this.#msPerStep = this.#msPerStep * 0.9 + ms * 0.1;
189
+ if (measured !== undefined) {
190
+ job.windows = measured;
191
+ job.lane = measured <= this.settings.screener_short_max_windows ? "short" : "long";
192
+ }
193
+ this.pump();
194
+ }
195
+ onDone(job, verdict) {
196
+ this.#finish(job);
197
+ this.#events.verdict(job, verdict);
198
+ this.pump();
199
+ }
200
+ #giveUp(job, message) {
201
+ // A message that stops the screen every time stays undelivered, and mustn't hold up its conversation for good.
202
+ this.#finish(job);
203
+ logEvent("screening.gave_up", { agent: job.agentHex, session: job.sessionHex, position: job.position, message });
204
+ this.#events.verdict(job, { verdict: "blocked", delivered: null, findings: [], reason: `the screen failed on it ${this.settings.screener_max_attempts} times, so it wasn't delivered`, score: null, ran: NOT_SCREENED });
205
+ }
206
+ onFailed(job, message) {
207
+ job.started = false;
208
+ job.attempts++;
209
+ if (job.attempts >= this.settings.screener_max_attempts)
210
+ this.#giveUp(job, message);
211
+ this.pump();
212
+ }
213
+ onCrash(slot, message) {
214
+ const job = slot.job;
215
+ this.restarts++;
216
+ this.lastCrash = message;
217
+ logEvent("screening.worker_crashed", { message, restarts: this.restarts });
218
+ // Only what lived in this thread starts over. Another thread's screens carry on.
219
+ for (const queue of this.#sessions.values())
220
+ for (const queued of queue)
221
+ if (queued.slot === slot)
222
+ queued.started = false;
223
+ if (job) {
224
+ job.attempts++;
225
+ if (job.attempts >= this.settings.screener_max_attempts)
226
+ this.#giveUp(job, message);
227
+ }
228
+ const counts = this.pending();
229
+ this.#events.workerCrashed(message, counts.short + counts.long);
230
+ setTimeout(() => {
231
+ if (!this.#stopped)
232
+ slot.spawn();
233
+ }, Math.min(RESTART_MAX_MS, RESTART_BASE_MS * 2 ** Math.min(this.restarts, 10))).unref();
234
+ }
235
+ }
236
+ class Slot {
237
+ #queue;
238
+ #worker;
239
+ #sentAt = 0;
240
+ ready = false;
241
+ job;
242
+ constructor(queue) {
243
+ this.#queue = queue;
244
+ }
245
+ spawn() {
246
+ const { screener_model_threads: modelThreads, screener_worker_memory_mb: memory } = this.#queue.settings;
247
+ const worker = new Worker(workerUrl(), { workerData: { role: "inbound", modelThreads }, resourceLimits: { maxOldGenerationSizeMb: memory } });
248
+ this.#worker = worker;
249
+ this.ready = false;
250
+ this.job = undefined;
251
+ worker.on("message", (reply) => this.#onReply(reply));
252
+ const lost = (message) => {
253
+ if (this.#worker !== worker)
254
+ return;
255
+ this.#worker = undefined;
256
+ this.ready = false;
257
+ const failed = this.job;
258
+ this.#queue.onCrash(this, message);
259
+ if (this.job === failed)
260
+ this.job = undefined;
261
+ };
262
+ worker.on("error", (err) => lost(err instanceof Error ? err.message : String(err)));
263
+ worker.on("exit", (code) => lost(`the screening thread exited with code ${code}`));
264
+ }
265
+ stop() {
266
+ const worker = this.#worker;
267
+ this.#worker = undefined;
268
+ this.ready = false;
269
+ this.job = undefined;
270
+ void worker?.terminate();
271
+ }
272
+ start(job, body, level, language) {
273
+ this.job = job;
274
+ this.#sentAt = performance.now();
275
+ this.#worker?.postMessage({ type: "start", id: job.id, body, level, language });
276
+ }
277
+ step(job) {
278
+ this.job = job;
279
+ this.#sentAt = performance.now();
280
+ this.#worker?.postMessage({ type: "step", id: job.id });
281
+ }
282
+ #onReply(reply) {
283
+ if (reply.type === "ready") {
284
+ this.#queue.onModel(reply.model);
285
+ this.ready = true;
286
+ logEvent("screening.worker_ready", { model: reply.model.state, running: reply.model.running, backend: reply.model.backend });
287
+ this.#queue.pump();
288
+ return;
289
+ }
290
+ const job = this.job;
291
+ // A reply from before a restart names a job this thread no longer holds.
292
+ if (!job || job.id !== reply.id)
293
+ return;
294
+ this.job = undefined;
295
+ if (reply.type === "stepped")
296
+ this.#queue.onStepped(job, reply.measured, performance.now() - this.#sentAt);
297
+ else if (reply.type === "done")
298
+ this.#queue.onDone(job, reply.verdict);
299
+ else
300
+ this.#queue.onFailed(job, reply.message);
301
+ }
302
+ }
303
+ // Outbound never runs the model, so it has a thread of its own, and a send never waits behind a long inbound screen.
304
+ export class OutboundScreen {
305
+ #calls = new Map();
306
+ #worker;
307
+ #nextId = 1;
308
+ #stopped = false;
309
+ start() {
310
+ const worker = new Worker(workerUrl(), { workerData: { role: "outbound" } });
311
+ this.#worker = worker;
312
+ worker.on("message", (reply) => {
313
+ const call = reply.id === undefined ? undefined : this.#calls.get(reply.id);
314
+ if (!call)
315
+ return;
316
+ this.#calls.delete(reply.id);
317
+ clearTimeout(call.timer);
318
+ if (reply.verdict)
319
+ call.resolve(reply.verdict);
320
+ else
321
+ call.reject(new Error(reply.message ?? "the outbound screen failed"));
322
+ });
323
+ const lost = (message) => {
324
+ if (this.#worker !== worker)
325
+ return;
326
+ this.#worker = undefined;
327
+ logEvent("screening.outbound_worker_crashed", { message });
328
+ for (const call of this.#calls.values()) {
329
+ clearTimeout(call.timer);
330
+ call.reject(new Error(message));
331
+ }
332
+ this.#calls.clear();
333
+ if (!this.#stopped)
334
+ setTimeout(() => { if (!this.#stopped)
335
+ this.start(); }, RESTART_BASE_MS).unref();
336
+ };
337
+ worker.on("error", (err) => lost(err instanceof Error ? err.message : String(err)));
338
+ worker.on("exit", (code) => lost(`the outbound screening thread exited with code ${code}`));
339
+ }
340
+ stop() {
341
+ this.#stopped = true;
342
+ const worker = this.#worker;
343
+ this.#worker = undefined;
344
+ void worker?.terminate();
345
+ }
346
+ screen(text, keys, pii) {
347
+ return new Promise((resolve, reject) => {
348
+ const worker = this.#worker;
349
+ if (!worker) {
350
+ reject(new Error("the outbound screening thread isn't running"));
351
+ return;
352
+ }
353
+ const id = this.#nextId++;
354
+ const timer = setTimeout(() => {
355
+ this.#calls.delete(id);
356
+ reject(new Error(`the outbound screen took longer than ${OUTBOUND_LIMIT_MS / 1000} seconds`));
357
+ }, OUTBOUND_LIMIT_MS);
358
+ this.#calls.set(id, { resolve, reject, timer });
359
+ worker.postMessage({ type: "outbound", id, text, keys, pii });
360
+ });
361
+ }
362
+ }
@@ -0,0 +1,125 @@
1
+ import type { Store } from "./store.ts";
2
+ export declare const SETTINGS: {
3
+ readonly screener_workers: {
4
+ readonly default: 1;
5
+ readonly min: 1;
6
+ readonly max: 8;
7
+ readonly description: "Screening threads. Each costs about 1 GB with the model, and more than one hasn't been measured to help.";
8
+ };
9
+ readonly screener_model_threads: {
10
+ readonly default: 4;
11
+ readonly min: 1;
12
+ readonly max: 64;
13
+ readonly description: "CPU threads the model uses in each screening thread. Fewer costs less CPU, and long messages take longer.";
14
+ };
15
+ readonly screener_worker_memory_mb: {
16
+ readonly default: 2048;
17
+ readonly min: 16;
18
+ readonly max: 65536;
19
+ readonly description: "The JavaScript memory one screening thread may use. Past it the thread crashes and restarts, and the message stays undelivered.";
20
+ };
21
+ readonly screener_measure_max_bytes: {
22
+ readonly default: 16000;
23
+ readonly min: 1000;
24
+ readonly max: 1000000;
25
+ readonly description: "A message up to this size is measured straight away to see if it's short. A longer one goes to the long lane.";
26
+ };
27
+ readonly screener_short_max_windows: {
28
+ readonly default: 1;
29
+ readonly min: 1;
30
+ readonly max: 100;
31
+ readonly description: "A message of up to this many model windows goes in the short lane.";
32
+ };
33
+ readonly screener_tell_after_ms: {
34
+ readonly default: 3000;
35
+ readonly min: 0;
36
+ readonly max: 3600000;
37
+ readonly description: "How long a message can wait for its screen before both sides are told.";
38
+ };
39
+ readonly screener_max_attempts: {
40
+ readonly default: 3;
41
+ readonly min: 1;
42
+ readonly max: 100;
43
+ readonly description: "How many times a screen can fail on one message before that message is refused, so it can't hold its conversation up for good.";
44
+ };
45
+ readonly sender_backlog_gap: {
46
+ readonly default: 8;
47
+ readonly min: 1;
48
+ readonly max: 100000;
49
+ readonly description: "How many of your messages a reader can be behind on screening before your daemon waits before sending the next.";
50
+ };
51
+ readonly sender_backlog_wait_ms: {
52
+ readonly default: 10000;
53
+ readonly min: 0;
54
+ readonly max: 600000;
55
+ readonly description: "The longest one send waits for a reader to catch up. Then it goes anyway, and says so.";
56
+ };
57
+ readonly offer_lifetime_minutes: {
58
+ readonly default: 10080;
59
+ readonly min: 1;
60
+ readonly max: 525600;
61
+ readonly description: "How long a call waits for an answer before it expires.";
62
+ };
63
+ readonly channel_check_seconds: {
64
+ readonly default: 30;
65
+ readonly min: 1;
66
+ readonly max: 86400;
67
+ readonly description: "How often the daemon checks every channel, in case a push was missed. A change takes effect at once.";
68
+ };
69
+ readonly channel_lost_wait_seconds: {
70
+ readonly default: 3600;
71
+ readonly min: 1;
72
+ readonly max: 2592000;
73
+ readonly description: "How long a missing channel post is asked for before a reader calls it lost and moves on, and its publisher is told. A change takes effect at once.";
74
+ };
75
+ readonly channel_default_retention_days: {
76
+ readonly default: 7;
77
+ readonly min: 1;
78
+ readonly max: 3650;
79
+ readonly description: "How long a new channel keeps posts unless channel-create says otherwise. The server sets the most it allows.";
80
+ };
81
+ readonly channel_local_retention_days: {
82
+ readonly default: 30;
83
+ readonly min: 1;
84
+ readonly max: 36500;
85
+ readonly description: "How long this machine keeps channel posts it has read, and your own posts in its log. Positions and unread posts are kept.";
86
+ };
87
+ readonly account_page_port: {
88
+ readonly default: 7421;
89
+ readonly min: 1024;
90
+ readonly max: 65535;
91
+ readonly description: "The port on this machine where the account page is served, to this machine only. If something else holds it, the daemon says so and goes on without the page. A change takes effect at the next login.";
92
+ };
93
+ readonly channel_read_wait_seconds: {
94
+ readonly default: 20;
95
+ readonly min: 0;
96
+ readonly max: 60;
97
+ readonly description: "How long channel-read waits for posts to be screened. What isn't ready by then is listed as still screening, and a doorbell rings when it is.";
98
+ };
99
+ };
100
+ export declare const TEXT_SETTINGS: {
101
+ readonly issuer_url: {
102
+ readonly default: "https://trust.celloprotocol.com";
103
+ readonly description: "The issuer this daemon redeems codes at and collects signals from. Which issuers count is decided by the signed list, not by this.";
104
+ };
105
+ readonly concierge_address: {
106
+ readonly default: "7cd434a45ac1213f504ac06b55ca70724e4d23dc51a5bf3651ecdce81e87538b@server.celloprotocol.com";
107
+ readonly description: "The agent setup calls to prove this one works.";
108
+ };
109
+ };
110
+ export type TextSettingName = keyof typeof TEXT_SETTINGS;
111
+ export declare const DEFAULT_SERVER_URL = "wss://server.celloprotocol.com/v1";
112
+ export type SettingName = keyof typeof SETTINGS;
113
+ export type Settings = Record<SettingName, number>;
114
+ export declare class SettingError extends Error {
115
+ }
116
+ export declare function loadSettings(store: Store): Settings;
117
+ export declare function currentSetting(store: Store, name: SettingName): number;
118
+ export declare function currentText(store: Store, name: TextSettingName): string;
119
+ export declare function changeSetting(store: Store, name: string, raw: string): void;
120
+ export declare function listSettings(store: Store): {
121
+ name: string;
122
+ value: number | string;
123
+ default: number | string;
124
+ description: string;
125
+ }[];
@@ -0,0 +1,73 @@
1
+ export const SETTINGS = {
2
+ screener_workers: { default: 1, min: 1, max: 8, description: "Screening threads. Each costs about 1 GB with the model, and more than one hasn't been measured to help." },
3
+ screener_model_threads: { default: 4, min: 1, max: 64, description: "CPU threads the model uses in each screening thread. Fewer costs less CPU, and long messages take longer." },
4
+ screener_worker_memory_mb: { default: 2048, min: 16, max: 65536, description: "The JavaScript memory one screening thread may use. Past it the thread crashes and restarts, and the message stays undelivered." },
5
+ screener_measure_max_bytes: { default: 16_000, min: 1_000, max: 1_000_000, description: "A message up to this size is measured straight away to see if it's short. A longer one goes to the long lane." },
6
+ screener_short_max_windows: { default: 1, min: 1, max: 100, description: "A message of up to this many model windows goes in the short lane." },
7
+ screener_tell_after_ms: { default: 3_000, min: 0, max: 3_600_000, description: "How long a message can wait for its screen before both sides are told." },
8
+ screener_max_attempts: { default: 3, min: 1, max: 100, description: "How many times a screen can fail on one message before that message is refused, so it can't hold its conversation up for good." },
9
+ sender_backlog_gap: { default: 8, min: 1, max: 100_000, description: "How many of your messages a reader can be behind on screening before your daemon waits before sending the next." },
10
+ sender_backlog_wait_ms: { default: 10_000, min: 0, max: 600_000, description: "The longest one send waits for a reader to catch up. Then it goes anyway, and says so." },
11
+ offer_lifetime_minutes: { default: 10_080, min: 1, max: 525_600, description: "How long a call waits for an answer before it expires." },
12
+ channel_check_seconds: { default: 30, min: 1, max: 86_400, description: "How often the daemon checks every channel, in case a push was missed. A change takes effect at once." },
13
+ channel_lost_wait_seconds: { default: 3_600, min: 1, max: 2_592_000, description: "How long a missing channel post is asked for before a reader calls it lost and moves on, and its publisher is told. A change takes effect at once." },
14
+ channel_default_retention_days: { default: 7, min: 1, max: 3_650, description: "How long a new channel keeps posts unless channel-create says otherwise. The server sets the most it allows." },
15
+ channel_local_retention_days: { default: 30, min: 1, max: 36_500, description: "How long this machine keeps channel posts it has read, and your own posts in its log. Positions and unread posts are kept." },
16
+ account_page_port: { default: 7421, min: 1024, max: 65_535, description: "The port on this machine where the account page is served, to this machine only. If something else holds it, the daemon says so and goes on without the page. A change takes effect at the next login." },
17
+ channel_read_wait_seconds: { default: 20, min: 0, max: 60, description: "How long channel-read waits for posts to be screened. What isn't ready by then is listed as still screening, and a doorbell rings when it is." },
18
+ };
19
+ // Settings that are words rather than numbers. The same rule: the default is written to the database on first start.
20
+ export const TEXT_SETTINGS = {
21
+ issuer_url: { default: "https://trust.celloprotocol.com", description: "The issuer this daemon redeems codes at and collects signals from. Which issuers count is decided by the signed list, not by this." },
22
+ concierge_address: { default: "7cd434a45ac1213f504ac06b55ca70724e4d23dc51a5bf3651ecdce81e87538b@server.celloprotocol.com", description: "The agent setup calls to prove this one works." },
23
+ };
24
+ const TEXT_NAMES = Object.keys(TEXT_SETTINGS);
25
+ export const DEFAULT_SERVER_URL = "wss://server.celloprotocol.com/v1";
26
+ const NAMES = Object.keys(SETTINGS);
27
+ const configKey = (name) => `setting.${name}`;
28
+ export class SettingError extends Error {
29
+ }
30
+ function checked(name, raw) {
31
+ const value = Number(raw);
32
+ const { min, max } = SETTINGS[name];
33
+ if (!Number.isInteger(value) || value < min || value > max)
34
+ throw new SettingError(`${name} must be a whole number from ${min} to ${max}, not ${raw}`);
35
+ return value;
36
+ }
37
+ // Writes any default that's missing, and fails by name on a stored value that doesn't make sense.
38
+ export function loadSettings(store) {
39
+ const loaded = {};
40
+ for (const name of NAMES) {
41
+ const stored = store.getConfig(configKey(name));
42
+ if (stored === undefined)
43
+ store.setConfig(configKey(name), String(SETTINGS[name].default));
44
+ loaded[name] = stored === undefined ? SETTINGS[name].default : checked(name, stored);
45
+ }
46
+ for (const name of TEXT_NAMES) {
47
+ if (store.getConfig(`setting.${name}`) === undefined)
48
+ store.setConfig(`setting.${name}`, TEXT_SETTINGS[name].default);
49
+ }
50
+ return loaded;
51
+ }
52
+ // For what reads a setting each time it's needed, so a change takes effect with no restart.
53
+ export function currentSetting(store, name) {
54
+ return checked(name, store.getConfig(configKey(name)) ?? String(SETTINGS[name].default));
55
+ }
56
+ export function currentText(store, name) {
57
+ return store.getConfig(`setting.${name}`) ?? TEXT_SETTINGS[name].default;
58
+ }
59
+ export function changeSetting(store, name, raw) {
60
+ if (TEXT_NAMES.includes(name)) {
61
+ store.setConfig(`setting.${name}`, raw);
62
+ return;
63
+ }
64
+ if (!NAMES.includes(name))
65
+ throw new SettingError(`there's no setting named ${name}. They are: ${[...NAMES, ...TEXT_NAMES].join(", ")}`);
66
+ store.setConfig(configKey(name), String(checked(name, raw)));
67
+ }
68
+ export function listSettings(store) {
69
+ return [
70
+ ...NAMES.map((name) => ({ name, value: Number(store.getConfig(configKey(name)) ?? SETTINGS[name].default), default: SETTINGS[name].default, description: SETTINGS[name].description })),
71
+ ...TEXT_NAMES.map((name) => ({ name, value: currentText(store, name), default: TEXT_SETTINGS[name].default, description: TEXT_SETTINGS[name].description })),
72
+ ];
73
+ }
@@ -0,0 +1,55 @@
1
+ import type { Store } from "./store.ts";
2
+ import { type TierName } from "./tiers.ts";
3
+ export type RuleDirection = "inbound" | "outbound";
4
+ export type RuleScope = "default" | "tier" | "contact";
5
+ export declare const RULE_DIRECTIONS: readonly RuleDirection[];
6
+ export declare const RULE_SCOPES: readonly RuleScope[];
7
+ export declare const ISSUER_KINDS: readonly ["issuer", "agent"];
8
+ export interface SignalRule {
9
+ require_types: string[];
10
+ require_issuer_kind: (typeof ISSUER_KINDS)[number] | null;
11
+ min_count: number;
12
+ }
13
+ export declare const NO_RULE: SignalRule;
14
+ export declare const isNoRule: (rule: SignalRule) => boolean;
15
+ export interface RuleRow extends SignalRule {
16
+ direction: RuleDirection;
17
+ scope: RuleScope;
18
+ target: string;
19
+ updated_at: number;
20
+ }
21
+ export declare function ensureRulesSchema(db: Store): void;
22
+ export declare function atLeastAsStrict(a: SignalRule, b: SignalRule): boolean;
23
+ export interface Evaluation {
24
+ pass: boolean;
25
+ missing_types?: string[];
26
+ missing_issuer_kind?: boolean;
27
+ counted?: number;
28
+ not_counted_unrecognised?: number;
29
+ not_counted_same_operator?: number;
30
+ }
31
+ interface Shown {
32
+ type: string;
33
+ issuer_kind: string;
34
+ counts_as_verified: boolean;
35
+ same_operator: boolean;
36
+ }
37
+ export declare function evaluate(rule: SignalRule, shown: readonly Shown[]): Evaluation;
38
+ export declare class SignalRules {
39
+ #private;
40
+ constructor(store: Store);
41
+ get(agentId: Uint8Array, direction: RuleDirection, scope: RuleScope, target: string): RuleRow | undefined;
42
+ list(agentId: Uint8Array): RuleRow[];
43
+ put(agentId: Uint8Array, direction: RuleDirection, scope: RuleScope, target: string, rule: SignalRule): void;
44
+ clear(agentId: Uint8Array, direction: RuleDirection, scope: RuleScope, target: string): boolean;
45
+ resolve(agentId: Uint8Array, direction: RuleDirection, tier: TierName, contactHex: string | null, without?: {
46
+ scope: RuleScope;
47
+ target: string;
48
+ }): {
49
+ rule: SignalRule;
50
+ level: RuleScope | "none";
51
+ };
52
+ weakens(agentId: Uint8Array, direction: RuleDirection, scope: RuleScope, target: string, next: SignalRule | null, contactTier: TierName): boolean;
53
+ static checkTarget(scope: RuleScope, target: string | undefined): string;
54
+ }
55
+ export {};