omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / fabric / fab_dispatch.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* fab_dispatch.c -- Fabric dispatch of remote composition candidates (fab_dispatch.h). */ |
| 18 | #include "fab_dispatch.h" |
| 19 | #include "omega_native.h" |
| 20 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | /* Link-map marker (test build only): tools/r16_prod_hygiene.sh refuses any |
| 24 | * production binary that carries an aien_test_build_* symbol. */ |
| 25 | __attribute__((used)) const char aien_test_build_fab_dispatch[] = |
| 26 | "AIEN_TEST_BUILD piece: Fabric F5-0 in-process simulated-machine dispatcher"; |
| 27 | |
| 28 | int fab_dispatch_init(FabDispatch *d, FabNode *node) { |
| 29 | if (!d || !node || !node->cfg.catalog) return FAB_E_ARG; |
| 30 | memset(d, 0, sizeof *d); |
| 31 | if (pthread_mutex_init(&d->mu, NULL) != 0) return FAB_E_ARG; |
| 32 | d->node = node; /* set only once the lock exists (destroy keys on it) */ |
| 33 | return FAB_OK; |
| 34 | } |
| 35 | |
| 36 | void fab_dispatch_destroy(FabDispatch *d) { |
| 37 | if (d && d->node) pthread_mutex_destroy(&d->mu); |
| 38 | if (d) d->node = NULL; |
| 39 | } |
| 40 | |
| 41 | int fab_dispatch_add(FabDispatch *d, const AienMachineId *m, const AgSkillTable *skills) { |
| 42 | if (!d || !d->node || !m || !skills) return FAB_E_ARG; |
| 43 | if (aien_mid_equal(m, &d->node->cfg.self)) return FAB_E_ARG; /* local Skills run locally */ |
| 44 | pthread_mutex_lock(&d->mu); |
| 45 | if (d->n_exec >= FAB_DISPATCH_MAX) { |
| 46 | pthread_mutex_unlock(&d->mu); |
| 47 | return FAB_E_FULL; |
| 48 | } |
| 49 | for (uint32_t i = 0; i < d->n_exec; i++) |
| 50 | if (aien_mid_equal(&d->exec[i].machine, m)) { |
| 51 | pthread_mutex_unlock(&d->mu); |
| 52 | return FAB_E_ARG; |
| 53 | } |
| 54 | d->exec[d->n_exec++] = (FabExecutor){ *m, skills }; |
| 55 | pthread_mutex_unlock(&d->mu); |
| 56 | return FAB_OK; |
| 57 | } |
| 58 | |
| 59 | /* Caller holds the lock. */ |
| 60 | static int pump_locked(FabDispatch *d, uint64_t now_us) { |
| 61 | int n = 0; |
| 62 | FabVerdict v; |
| 63 | for (;;) { |
| 64 | int rc = fab_poll(d->node, now_us, &v); |
| 65 | if (rc < 0) return rc; |
| 66 | if (rc == 0) break; |
| 67 | n++; |
| 68 | d->polled++; |
| 69 | if (v.code != FAB_OK) d->refused_msgs++; |
| 70 | } |
| 71 | (void)fab_tick(d->node, now_us); |
| 72 | return n; |
| 73 | } |
| 74 | |
| 75 | int fab_dispatch_pump(FabDispatch *d, uint64_t now_us) { |
| 76 | if (!d || !d->node) return FAB_E_ARG; |
| 77 | pthread_mutex_lock(&d->mu); |
| 78 | int n = pump_locked(d, now_us); |
| 79 | pthread_mutex_unlock(&d->mu); |
| 80 | return n; |
| 81 | } |
| 82 | |
| 83 | static int refuse(FabDispatch *d, int why) { |
| 84 | d->refused[why]++; |
| 85 | d->last_refusal = why; |
| 86 | return why; |
| 87 | } |
| 88 | |
| 89 | int fab_dispatch_run(void *ctx, const SrRouter *router, const SrRoute *route, uint64_t input, |
| 90 | uint64_t now_us, uint64_t *result) { |
| 91 | FabDispatch *d = ctx; |
| 92 | if (!d || !d->node) return FAB_DX_ARG; |
| 93 | pthread_mutex_lock(&d->mu); |
| 94 | int rc; |
| 95 | if (!router || !route || !result || router->graph != d->node->cfg.catalog) { |
| 96 | rc = refuse(d, FAB_DX_ARG); |
| 97 | goto out; |
| 98 | } |
| 99 | if (pump_locked(d, now_us) < 0) { rc = refuse(d, FAB_DX_TRANSPORT); goto out; } |
| 100 | if (sr_route_check(router, route, now_us) != SR_E_REMOTE) { rc = refuse(d, FAB_DX_ROUTE); goto out; } |
| 101 | if (!route->target_known) { rc = refuse(d, FAB_DX_TARGET); goto out; } |
| 102 | JsHome home; |
| 103 | if (fab_home(d->node, &route->target, now_us, &home) != FAB_OK || |
| 104 | home.locality != JS_HOME_REMOTE_OWNED) { |
| 105 | rc = refuse(d, FAB_DX_NOT_MEMBER); |
| 106 | goto out; |
| 107 | } |
| 108 | const FabExecutor *ex = NULL; |
| 109 | for (uint32_t i = 0; i < d->n_exec && !ex; i++) |
| 110 | if (aien_mid_equal(&d->exec[i].machine, &route->target)) ex = &d->exec[i]; |
| 111 | const AgSkill *s = NULL; |
| 112 | for (uint32_t i = 0; ex && i < ex->skills->n && !s; i++) |
| 113 | if (ex->skills->skill[i].id == route->chosen.skill_id) s = &ex->skills->skill[i]; |
| 114 | if (!s || !s->fn) { rc = refuse(d, FAB_DX_NO_EXECUTOR); goto out; } |
| 115 | /* The machine runs what it advertised, or nothing: an unpinned (zero) |
| 116 | * advertisement is refused too, since nothing would bind the result. */ |
| 117 | { |
| 118 | uint64_t ok = 0; |
| 119 | (void)omega2_fab_om_digest_nonzero_eq(&ok, s->identity, 32, route->skill_digest, 32); |
| 120 | if (!ok) { rc = refuse(d, FAB_DX_DIGEST); goto out; } |
| 121 | } |
| 122 | uint64_t in = input; |
| 123 | int failed = 0; |
| 124 | uint64_t r = s->fn(&in, 1, 0, &failed); |
| 125 | if (failed) { rc = refuse(d, FAB_DX_FAILED); goto out; } |
| 126 | *result = r; |
| 127 | d->dispatched++; |
| 128 | rc = FAB_DX_OK; |
| 129 | out: |
| 130 | pthread_mutex_unlock(&d->mu); |
| 131 | return rc; |
| 132 | } |