@ours.network/tg-connector 0.1.1 → 0.1.3

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 (33) hide show
  1. package/README.md +62 -0
  2. package/dist/cli.js +35 -2
  3. package/dist/connector.js +105 -7
  4. package/dist/mufl_code/2DA1D5F860E42C605D92F1E24E2466C70EFDDBF057BBCBA52F0ECC639EFCA387.muflo +0 -0
  5. package/dist/mufl_code/core/CLUSTER_API.md +640 -0
  6. package/dist/mufl_code/core/CLUSTER_CONTRACT.md +121 -0
  7. package/dist/mufl_code/core/CLUSTER_INTRODUCTION_PLAN.md +200 -0
  8. package/dist/mufl_code/core/CONFIGURATION_DESIGN.md +35 -0
  9. package/dist/mufl_code/core/CONFIGURATION_PLAN.md +394 -0
  10. package/dist/mufl_code/core/DEVELOPMENT.md +168 -0
  11. package/dist/mufl_code/core/EPHEMERAL_INVITE_PLAN.md +465 -0
  12. package/dist/mufl_code/core/GROUP_CHAT_DESIGN.md +520 -0
  13. package/dist/mufl_code/core/LICENSE +98 -0
  14. package/dist/mufl_code/core/README.md +76 -0
  15. package/dist/mufl_code/core/a2a_capabilities.mm +350 -0
  16. package/dist/mufl_code/core/a2a_cluster.mm +601 -0
  17. package/dist/mufl_code/core/a2a_control.mm +89 -0
  18. package/dist/mufl_code/core/a2a_messaging.mm +2103 -0
  19. package/dist/mufl_code/core/a2a_monitoring.mm +94 -0
  20. package/dist/mufl_code/core/a2a_protocol.mm +231 -0
  21. package/dist/mufl_code/core/config.mufl +26 -0
  22. package/dist/mufl_code/core/release-notes/2.0.md +268 -0
  23. package/dist/mufl_code/core/release-notes/2.1.md +92 -0
  24. package/dist/mufl_code/core/release-notes/2.2.md +94 -0
  25. package/dist/mufl_code/core/release-notes/3.0.md +162 -0
  26. package/dist/mufl_code/core/release-notes/3.1.md +43 -0
  27. package/dist/mufl_code/core/release-notes/README.md +32 -0
  28. package/dist/mufl_code/core/tests/README.md +66 -0
  29. package/dist/mufl_code/core/tests/run.sh +67 -0
  30. package/dist/mufl_code/core/tests/test.mjs +467 -0
  31. package/dist/mufl_code/core/tests/test_actor.mu +287 -0
  32. package/dist/mufl_code/core/version.mm +24 -0
  33. package/package.json +4 -2
@@ -0,0 +1,76 @@
1
+ # ours-mufl-core
2
+
3
+ **[ours.network](https://ours.network) is free, source-available infrastructure for secure agent-to-agent communication.**
4
+
5
+ `ours-mufl-core` is the shared agent-to-agent protocol, written as a set of
6
+ MUFL (`.mm`) libraries. It is **the protocol** — a change here is a protocol
7
+ revision for the whole network. The core ships no standalone build; it is
8
+ vendored as a **git submodule** and compiled into each consumer's packet:
9
+
10
+ - [`ours-mcp`](https://github.com/adapt-toolkit/ours-mcp) — the MCP agent server + Claude Code plugin
11
+ - [`ours-messenger`](https://github.com/adapt-toolkit/ours-messenger) — the human-facing web client
12
+ - [`ours-tg-connector`](https://github.com/adapt-toolkit/ours-tg-connector) — the Telegram connector
13
+
14
+ Because every client links the same libraries, they all speak an identical wire
15
+ format and verification logic.
16
+
17
+ ## Modules
18
+
19
+ | File | Library | Purpose |
20
+ |------|---------|---------|
21
+ | `a2a_capabilities.mm` | `a2a_capabilities` | App manifest (`app_manifest_t`: app_id + name + capability map) read via `get_manifest`; the capability/verb envelope (`control_envelope_t` = `cap`/`verb`/`args`/`req_id`) and `dispatch`; well-known caps (`core.configuration`, `core.monitoring`, `core.connect`, `core.cluster`); secret-field redaction. |
22
+ | `a2a_protocol.mm` | `a2a_protocol` | Wire-facing shapes — invites, delegation certificates, root profiles, contact roots, local-book introduction credentials, control-plane governance attestation — plus the shared verification helpers (`verify_peer_delegation`, `rebuild_peer_address_document`, `verify_cp_attestation`, `verify_root_cp_binding`). |
23
+ | `a2a_messaging.mm` | `a2a_messaging` | Shared contact + messaging transactions (`generate_invite`, `add_contact`, `send_message`, `send_file`, `remove_contact`, `list_contacts`, inbound `accept_contact`/`receive_message`/`receive_file`) and the state they own; also the control-plane monitoring/config gate (`hidden` so apps cannot override it) and the `core.connect` introduction flow. Self-heal contact restore (3-leg handshake, network-visible): `request_contact_restore`, `submit_restore_response`, `complete_restore`. Host-driven sweep/flush surface: `restore_degraded_contacts` (re-issue restore for every degraded contact), `flush_deferred` (drain a healed contact's deferred queue), `list_degraded_contacts` / `list_deferred_queues` (readonly status for the boot/GC sweep). |
24
+ | `a2a_cluster.mm` | `a2a_cluster` | The `core.cluster` capability surface: child/subagent lifecycle, per-child monitoring authorization, the host-local contact book, and child↔contact plus cross-cluster introductions. |
25
+ | `a2a_monitoring.mm` | `a2a_monitoring` | Control-plane **receiver** side of forced monitoring: inbound `receive_monitoring_copy` (known-contact only), delegating to the app's `$on_monitoring_copy_received` init hook. |
26
+ | `a2a_control.mm` | `a2a_control` | Control-plane transport: `send_control` sends an opaque payload to a contact over the encrypted channel; inbound `control_message` validates origin + known-sender and delegates to the `$on_control_received` hook. |
27
+ | `version.mm` | `version` | Hardcoded core version (`get_core_version`). **Bump it with every change to this repo.** |
28
+ | `config.mufl` | — | Exports the libraries above for `config_load #"core"`. |
29
+
30
+ The protocol surface is documented in [`CLUSTER_API.md`](./CLUSTER_API.md) (the
31
+ control-protocol-over-MUFL contract) and [`CLUSTER_CONTRACT.md`](./CLUSTER_CONTRACT.md)
32
+ (the `core.cluster` verb contract). See [`DEVELOPMENT.md`](./DEVELOPMENT.md) for
33
+ architecture, build, and extension guidance.
34
+
35
+ ## Using it (vendor as a submodule)
36
+
37
+ This repo has no standalone build. A consumer checks it out at `<app>/mufl_code/core/`
38
+ and compiles its own packet with the directory present:
39
+
40
+ ```sh
41
+ git submodule add git@github.com:adapt-toolkit/ours-mufl-core.git mufl_code/core
42
+ ```
43
+
44
+ The consumer's `config.mufl` merges the core's exports with the MUFL stdlib via
45
+ `config_load #"core"`, and its application loads the libraries by name:
46
+
47
+ ```mufl
48
+ application actor loads libraries ..., a2a_protocol, version uses transactions
49
+ ```
50
+
51
+ Both packets expose the compiled-in version through their read-only
52
+ `get_version` transaction, so the deployed core version is observable at runtime.
53
+
54
+ ## Versioning
55
+
56
+ `version.mm` is the single source of truth (`core_version`, `MAJ.MIN.PATCH`),
57
+ starting at **0.0.1**. Rules:
58
+
59
+ - **Every** change to this repo bumps `core_version` in the same commit.
60
+ - **PATCH** — fixes that change no wire shape and no verification semantics.
61
+ - **MIN** — backward-compatible additions (new shapes, new helpers).
62
+ - **MAJ** — anything that changes the bytes of an existing wire shape or the
63
+ semantics of existing verification logic.
64
+
65
+ ## Donate
66
+
67
+ We build free, FSL source-available software and run the broker/relay services
68
+ that connect agents at our own cost. Every dollar helps keep it free and open.
69
+ Thank you for chipping in.
70
+
71
+ Donate: https://ours.network/donate
72
+
73
+ ## License
74
+
75
+ Licensed under the **Functional Source License, Version 1.1, Apache 2.0 Future
76
+ License** (`FSL-1.1-Apache-2.0`). See [LICENSE](./LICENSE).
@@ -0,0 +1,350 @@
1
+ // Shared ours capabilities library (core 1.6).
2
+ //
3
+ // Makes CAPABILITIES a first-class protocol property. Every node advertises an
4
+ // app manifest (app_id + name + description + monitoring_status + a TYPED MAP
5
+ // of capabilities), and capability VERBS multiplex over the existing
6
+ // a2a_control transport via a {cap, verb, args, req_id} envelope. This library
7
+ // owns envelope parsing + dispatch: it routes an inbound envelope to the
8
+ // app-injected per-capability handler, so apps stop hand-parsing control JSON.
9
+ // Adding a capability = a new $cap id string — no wire-shape change.
10
+ //
11
+ // Layering: app_id (the cheap "WHO", core 1.5) stays on the a2a_control
12
+ // transport; the manifest is the richer pull (name/description live ONLY here,
13
+ // no double-sourcing). The per-capability $params and verb $args remain opaque
14
+ // JSON strings — that is what a dumb frontend renders (e.g. the config schema +
15
+ // value for core.configuration) — while the ROUTING (cap/verb) is typed.
16
+ //
17
+ // Transport-agnostic by design: dispatch operates on an already-parsed envelope
18
+ // RECORD. The consumer (or its daemon) adapts whatever a2a_control delivers
19
+ // (opaque JSON string today) into that record before calling dispatch.
20
+ library a2a_capabilities loads libraries
21
+ current_transaction_info,
22
+ version
23
+ uses transactions
24
+ {
25
+ // ---- well-known capability ids ---------------------------------------
26
+ // Open id space: "core.*" is reserved for protocol capabilities, apps use
27
+ // "app.*". A new capability is just a new id — no wire change. core.monitoring
28
+ // is governance-required and auto-present on every node (see a2a_monitoring).
29
+ cap_configuration = "core.configuration".
30
+ cap_monitoring = "core.monitoring".
31
+ cap_connect = "core.connect".
32
+ // core.cluster (core 2.3): promotes cluster/subagent management from private
33
+ // a2a_messaging plumbing to a FIRST-CLASS, app-reusable capability. It owns
34
+ // child/subagent lifecycle, per-child monitoring authorization, the host-local
35
+ // contact book, and child<->contact + cross-cluster introductions, so ANY
36
+ // root-managing app (messenger today, telegram proxy later) exposes the same
37
+ // generic surface. Verbs stay convention (opaque $verb/$args, see
38
+ // control_envelope_t) — this id only RESERVES the namespace and the contract
39
+ // below blesses it; no wire/type/dispatch change. See CLUSTER_CONTRACT.md.
40
+ //
41
+ // VERB CONTRACT (verb -> reference handler / composition -> current wire alias).
42
+ // Wire aliases are KEPT as-is (no hard rename); core.cluster is the documented
43
+ // umbrella, "app.agents" remains a recognized alias for back-compat. NOTE:
44
+ // CamelCase handlers (provisionIdentity/delegateRole/deleteIdentityCompletely/
45
+ // setAgentMonitoring/connect_sibling/sign_monitoring_auth) are reference-host
46
+ // (ours-mcp) DAEMON composites, NOT single core trns; snake_case names are
47
+ // core a2a_messaging transactions. core.cluster blesses the VERB surface; the
48
+ // consuming app/daemon owns the composition. (Verified vs real handlers, R7.)
49
+ // child.create -> provisionIdentity + set_my_bio + delegateRole + best-effort enroll_delegated_node (alias: create_agent)
50
+ // child.list -> list_contact_roots / list_contacts (cluster filter) (alias: list_agents)
51
+ // child.set_bio -> set_my_bio (child-scoped role label) (alias: update_role)
52
+ // child.remove -> deleteIdentityCompletely — FULL teardown (packet/disk/contact-book/binding) (alias: remove_agent)
53
+ // child.set_monitoring -> setAgentMonitoring = connect_sibling + sign_monitoring_auth(root) + set_monitoring(role) (alias: set_monitoring)
54
+ // contact.list -> list_contacts (host-local book)
55
+ // contact.add -> generate_invite (returns an invite blob the caller redeems) (alias: contact_agent)
56
+ // contact.remove -> remove_contact (host-local book)
57
+ // introduce.child_to_contact -> introduce (1:1 introduction)
58
+ // introduce.cross_cluster -> introduce_to_group (cluster fan-out)
59
+ //
60
+ // GUARDRAILS (critic R6-3 / R6-5 / R7): core.cluster promotes MECHANISMS, NOT
61
+ // POLICY. Policy knobs are APP PARAMS — stated as honest TARGETs, not as
62
+ // already-current behavior:
63
+ // - local_auto_accept: RECOMMENDED safe default = false (introductions queue
64
+ // for operator approval). The reference host ours-mcp currently defaults
65
+ // TRUE (index.ts:1496,1541); flipping to false is a TRACKED OBLIGATION,
66
+ // not yet implemented.
67
+ // - child.set_monitoring child-visible notice: TARGET — authorizing per-child
68
+ // monitoring SHOULD surface a child-visible notice. Current code stores the
69
+ // root-signed monitoring_auth with NO notice; surfacing it is an obligation
70
+ // on the consuming app.
71
+ // - child.remove: DESTRUCTIVE — MUST be operator-confirmed. The reference
72
+ // frontend (messenger AgentNode) implements a confirm() dialog.
73
+ // Tiered authz (observe < manage < create < destroy) is ROADMAP, not yet
74
+ // enforced in core; today the app gates verbs. See CLUSTER_CONTRACT.md.
75
+ // DISCOVERY: advertising core.cluster != having a manageable cluster.
76
+ // describe() is static/shared, so EVERY identity (incl. leaf children with no
77
+ // children) advertises it. A generic consumer (e.g. telegram-proxy) MUST
78
+ // confirm an actual cluster via child.list (empty = none) before offering
79
+ // child.create/child.remove. Benign for messenger (only bound roots show the
80
+ // Cluster tab); a false-positive for capability-presence-keyed UIs.
81
+ cap_cluster = "core.cluster".
82
+
83
+ // ---- secret-field sentinels (config dialect, core.configuration) ------
84
+ // A secret field's VALUE is never echoed in plaintext: reads carry one of
85
+ // these sentinels, and writes interpret them. "$needs_reentry" is the
86
+ // load-bearing "no new party" guarantee — secrets auto-clear to it on any
87
+ // control-plane change (evict/rebind) so they must be re-entered for the
88
+ // new authority. The schema/value tree itself is opaque JSON in $params;
89
+ // these are the shared constants the daemon (write/store) and frontend
90
+ // (render redacted) agree on.
91
+ secret_set = "$set".
92
+ secret_unset = "$unset".
93
+ secret_needs_reentry = "$needs_reentry".
94
+ // Sentinel for a contact_ref field whose target is not yet a contact:
95
+ // selecting it triggers core.connect for that target (the config <-> connect
96
+ // hinge the telegram connector needs).
97
+ contact_ref_connect = "$connect".
98
+
99
+ // ---- wire shapes ------------------------------------------------------
100
+ // A secret field's redacted state, carried IN PLACE of its value — the value
101
+ // is NEVER present in the manifest. $status is one of the $set / $unset /
102
+ // $needs_reentry sentinels above; $epoch is the control-plane epoch the
103
+ // stored secret was last written under. At manifest build the app derives
104
+ // $status = $needs_reentry whenever $epoch is behind the node's current
105
+ // control-plane epoch (any evict/rebind bumps it), so a secret entered for a
106
+ // prior authority must be re-entered for the new one — the load-bearing
107
+ // "no new party inherits live secrets" guarantee, now in the type system.
108
+ metadef secret_field_t: ($status -> str, $epoch -> int).
109
+ // First-class capability descriptor. $cap is the stable enumerable id (the
110
+ // protocol property a dumb frontend keys off); $version is the per-capability
111
+ // schema version; $params is the opaque per-capability JSON; $secrets is the
112
+ // per-field redacted secret state, keyed by the field's path within $params'
113
+ // schema. $secrets is value-less by construction: $params declares WHICH
114
+ // fields are secret, $secrets carries only their status + epoch, so a secret
115
+ // VALUE can never ride the manifest wire.
116
+ metadef capability_t: ($cap -> str, $version -> int, $params -> str, $secrets -> (str ->> secret_field_t)).
117
+ // The node's self-description. monitoring_status is a first-class manifest
118
+ // field (governance-visible). $version is the manifest envelope version, bumped
119
+ // whenever the manifest content changes so a controller knows to refetch.
120
+ metadef app_manifest_t: (
121
+ $version -> int,
122
+ $app_id -> str,
123
+ $name -> str,
124
+ $description -> str,
125
+ $monitoring_status -> str,
126
+ $capabilities -> (str ->> capability_t)
127
+ ).
128
+ // Capability verb envelope, carried as the a2a_control payload. All fields
129
+ // are plain strings so it round-trips as JSON or as a native record; the
130
+ // per-verb $args stays opaque JSON for the handler to interpret.
131
+ // $args is a NATIVE value (R1): the daemon adapts the inbound JSON payload into
132
+ // this record AND parses $args to native before dispatch — MUFL has no JSON
133
+ // DECODER either, so a string $args would be unreadable by a handler. $cap/$verb/
134
+ // $req_id stay plain strings (routing/correlation only).
135
+ metadef control_envelope_t: ($cap -> str, $verb -> str, $args -> any, $req_id -> str).
136
+ // Response envelope, shipped back as the a2a_control payload and correlated by
137
+ // the controller on (sender,$req_id). $result/$err are NATIVE values (records/
138
+ // arrays/strings), not pre-stringified JSON: MUFL has no JSON encoder, so a
139
+ // handler returns this as transaction return_data and the daemon performs the
140
+ // generic JSON marshalling at the transport boundary (no verb logic in TS).
141
+ // $result present iff $ok; $err = ($code,$message) iff not. See CLUSTER_API.md §2.
142
+ metadef response_envelope_t: ($req_id -> str, $ok -> bool, $result -> any, $err -> any).
143
+
144
+ hidden
145
+ {
146
+ // App-injected: returns the node's CURRENT manifest (secrets already
147
+ // redacted by the config capability). Fires on get_manifest.
148
+ describe is (any -> app_manifest_t) = fn (_: any) -> app_manifest_t {
149
+ abort "describe hook is unset in a2a_capabilities (call a2a_capabilities::init)." when TRUE.
150
+ return ($version -> 0, $app_id -> "", $name -> "", $description -> "", $monitoring_status -> "off", $capabilities -> (,)).
151
+ }
152
+ // App-injected per-capability verb handlers, keyed by $cap id. Each
153
+ // receives ($sender_id, $sender_name, $app_id, $verb, $args, $req_id,
154
+ // $date) and returns the transaction actions (persist / notify / send a
155
+ // response). A handler decides whether to process in-packet or enqueue.
156
+ handlers is (str ->> (any -> transaction::action::type[])) = (,).
157
+ // Fallback for an unknown / unhandled capability id.
158
+ on_unknown is (any -> transaction::action::type[]) = fn (_: any) -> transaction::action::type[] { return []. }
159
+ // App-injected STATEFUL authz gate (RR-9): wired to a2a_messaging::authorize_control
160
+ // (which reads the bound monitoring_proxy — hidden there, a2a_capabilities cannot).
161
+ // Receives ($sender_id,$cap,$verb), returns TRUE iff the sender may run it.
162
+ // NIL until init wires it; dispatch FAIL-FASTS (abort) if a controller-class
163
+ // verb is routed with this unset, so an app can never forget the gate.
164
+ authorizer is (any -> bool)+ = NIL.
165
+ }
166
+
167
+ // FAIL-FAST: $supported is the static list of capability ids this app
168
+ // implements; init aborts if any lacks a wired handler, so "declared" and
169
+ // "implemented" can never drift. (Distinct from the dynamic describe(),
170
+ // which reads live state at get_manifest time.)
171
+ init = fn (_:(
172
+ $describe -> describe_cb: (any -> app_manifest_t),
173
+ $supported -> supported_caps: str[],
174
+ $handlers -> handler_map: (str ->> (any -> transaction::action::type[])),
175
+ $on_unknown -> fallback: (any -> transaction::action::type[]),
176
+ // RR-9: the stateful authz gate. OPTIONAL for back-compat — an app with no
177
+ // controller-class verbs (e.g. the connector: only core.configuration via
178
+ // its own path) may omit it. If omitted, dispatch fail-fasts on the first
179
+ // controller-class verb, so a cluster app MUST wire it.
180
+ $authorizer -> authorizer_cb: (any -> bool)+
181
+ ))
182
+ {
183
+ describe -> describe_cb.
184
+ handlers -> handler_map.
185
+ on_unknown -> fallback.
186
+ if authorizer_cb != NIL { authorizer -> authorizer_cb. }
187
+ sc supported_caps -- ( -> cap)
188
+ {
189
+ abort "Capability declared without a handler: " + cap when (handler_map cap) == NIL.
190
+ }
191
+ }
192
+
193
+ // ---- manifest helpers -------------------------------------------------
194
+ fn has_capability (_:($manifest -> m: app_manifest_t, $cap -> cap: str)) -> bool
195
+ {
196
+ return (m $capabilities cap) != NIL.
197
+ }
198
+
199
+ fn get_capability (_:($manifest -> m: app_manifest_t, $cap -> cap: str)) -> capability_t+
200
+ {
201
+ return m $capabilities cap.
202
+ }
203
+
204
+ // The node's current live manifest (reads the app-injected describe hook).
205
+ // Public fn accessor so a handler library (e.g. a2a_cluster's bind handler,
206
+ // which returns {manifest,members,config}) can read it without the readonly
207
+ // get_manifest trn (handlers run inside a trn and call fns, not trns).
208
+ fn current_manifest (_) -> app_manifest_t
209
+ {
210
+ return describe NIL.
211
+ }
212
+
213
+ // Does THIS node's own live manifest advertise `cap`? Reads the app-injected
214
+ // describe() hook (live state at call time) so another library — e.g.
215
+ // a2a_messaging's ingest_connect_descriptor node-side gate — can enforce
216
+ // "I only accept introductions if I actually support core.connect" without
217
+ // reaching into the hidden describe field. describe is wired by init, which
218
+ // every packet runs before serving, so it is set whenever this is reached.
219
+ fn self_supports (cap: str) -> bool
220
+ {
221
+ return has_capability ($manifest -> describe NIL, $cap -> cap).
222
+ }
223
+
224
+ // ---- authorization policy (pure) + response builder -------------------
225
+ // PURE policy table for the single pre-dispatch authz chokepoint
226
+ // (CLUSTER_API.md §3). Returns the auth CLASS a (cap,verb) requires; it reads
227
+ // NO state, so it lives here in the lowest layer. The STATEFUL gate
228
+ // a2a_messaging::authorize_control combines this with the bound-proxy identity
229
+ // (monitoring_proxy is hidden in a2a_messaging, which loads THIS library —
230
+ // the dependency runs that way, not the reverse). FAIL-CLOSED: anything not
231
+ // explicitly public/bootstrap returns "controller", so an unknown or typo'd
232
+ // cap/verb is denied to strangers at the chokepoint before dispatch.
233
+ // "public" — no auth; the get_manifest readonly trn only (no member data).
234
+ // "bootstrap" — core.monitoring.bind only; auth is 6-digit code possession.
235
+ // "controller" — the bound control proxy; the explicit verbs below.
236
+ // "deny" — anything not listed (RR-6 deny-all): a new verb must be
237
+ // consciously classified here to become reachable.
238
+ fn control_auth_class (_:($cap -> cap: str, $verb -> verb: str)) -> str
239
+ {
240
+ // NOTE (C6): get_manifest is NOT classified here — it is a standalone
241
+ // `trn readonly` (get_manifest below), never routed through dispatch, so it
242
+ // never reaches this table. The CLUSTER_API.md §3 "public" row documents the
243
+ // get_manifest trn itself, not a (cap,verb) envelope.
244
+ if cap == cap_monitoring && verb == "bind" { return "bootstrap". }
245
+ if cap == cap_cluster && (verb == "list" || verb == "create" || verb == "set_bio"
246
+ || verb == "set_persona"
247
+ || verb == "remove" || verb == "set_monitoring" || verb == "contact"
248
+ || verb == "introduce") { return "controller". }
249
+ if cap == cap_monitoring && verb == "disable" { return "controller". }
250
+ if cap == cap_connect && verb == "introduce" { return "controller". }
251
+ if cap == cap_configuration && (verb == "get_config" || verb == "set_config") { return "controller". }
252
+ return "deny".
253
+ }
254
+
255
+ // Build a response envelope (CLUSTER_API.md §2). $result/$err are NATIVE values
256
+ // the handler assembles per verb; it is carried back as transaction return_data
257
+ // and the daemon marshals it to JSON + ships — no in-MUFL JSON, no core send.
258
+ fn build_response (_:(
259
+ $req_id -> req_id: str, $ok -> ok: bool, $result -> result_val: any, $err -> err_val: any
260
+ )) -> response_envelope_t
261
+ {
262
+ return ($req_id -> req_id, $ok -> ok, $result -> result_val, $err -> err_val).
263
+ }
264
+
265
+ // Build the return_data action carrying an error response_envelope (the daemon
266
+ // marshals + ships it). Used by the dispatch authz chokepoint and handlers.
267
+ fn deny_action (_:($req_id -> req_id: str, $code -> code: str, $message -> message: str)) -> transaction::action::type
268
+ {
269
+ return transaction::action::return_data (
270
+ $kind -> $data,
271
+ $payload -> build_response (
272
+ $req_id -> req_id, $ok -> FALSE,
273
+ $result -> (,), $err -> ($code -> code, $message -> message)
274
+ )
275
+ ).
276
+ }
277
+
278
+ // ---- dispatch ---------------------------------------------------------
279
+ // Route a parsed capability envelope to its handler. The consumer wires this
280
+ // into a2a_control's on_control_received (after adapting the opaque payload
281
+ // into an envelope record). Unknown caps fall through to on_unknown.
282
+ fn dispatch (_:(
283
+ $sender_id -> sender_id: global_id,
284
+ $sender_name -> sender_name: str,
285
+ $app_id -> app_id: str,
286
+ $envelope -> env: any,
287
+ $date -> date: time
288
+ )) -> transaction::action::type[]
289
+ {
290
+ cap = (env $cap) safe str.
291
+ verb = (env $verb) safe str.
292
+ args = env $args. // NATIVE (R1), handler interprets per verb
293
+ req_id = ((env $req_id) == NIL ?? "" ; (env $req_id) safe str).
294
+
295
+ // ---- RR-9 pre-route authz chokepoint (non-bypassable BY CONSTRUCTION) ----
296
+ // The gate lives HERE, inside dispatch, so an app cannot wire routing while
297
+ // forgetting the gate. deny/unauthorized are returned as a response_envelope
298
+ // via return_data (the daemon marshals+ships); no in-MUFL JSON, no core send.
299
+ klass = control_auth_class ($cap -> cap, $verb -> verb).
300
+ if klass == "deny"
301
+ {
302
+ return [ deny_action ($req_id -> req_id, $code -> "unknown_verb", $message -> "verb not permitted: " + cap + "/" + verb) ].
303
+ }
304
+ if klass == "controller"
305
+ {
306
+ // FAIL-FAST (mirrors init's missing-handler abort): a controller verb must
307
+ // never route without the stateful gate wired.
308
+ abort "a2a_capabilities::dispatch routed a controller verb without an authorizer wired (pass $authorizer to init)." when authorizer == NIL.
309
+ if (authorizer? ($sender_id -> sender_id, $cap -> cap, $verb -> verb)) != TRUE
310
+ {
311
+ return [ deny_action ($req_id -> req_id, $code -> "unauthorized", $message -> "sender is not the bound controller") ].
312
+ }
313
+ }
314
+ // "public" + "bootstrap" fall through (bootstrap's own check is in the bind handler).
315
+
316
+ ctx = (
317
+ $sender_id -> sender_id,
318
+ $sender_name -> sender_name,
319
+ $app_id -> app_id,
320
+ $verb -> verb,
321
+ $args -> args,
322
+ $req_id -> req_id,
323
+ $date -> date
324
+ ).
325
+
326
+ handler = handlers cap.
327
+ if handler == NIL
328
+ {
329
+ return on_unknown (
330
+ $sender_id -> sender_id,
331
+ $sender_name -> sender_name,
332
+ $app_id -> app_id,
333
+ $cap -> cap,
334
+ $verb -> verb,
335
+ $args -> args,
336
+ $req_id -> req_id,
337
+ $date -> date
338
+ ).
339
+ }
340
+ return handler? ctx.
341
+ }
342
+
343
+ // ---- manifest read ----------------------------------------------------
344
+ // Pull the current manifest (for a controller / connecting peer). Readonly:
345
+ // no state change. Secrets are already redacted by the app's describe().
346
+ trn readonly get_manifest _
347
+ {
348
+ return describe NIL.
349
+ }
350
+ }