Move Idunn health lanes to shared RUDP
This commit is contained in:
parent
1ffdee248e
commit
edca92d35f
17 changed files with 2914 additions and 386 deletions
|
|
@ -181,9 +181,11 @@ walk through the same door.
|
|||
|
||||
The local swarm also opens a RUDP health ingress at `127.0.0.1:17870` unless
|
||||
started with `--rudp-health-bind none`. It accepts raw
|
||||
`idunn.daemon_health` document puts over `cultnet.transport.rudp.v0` and writes
|
||||
them into the keepalive store. That is daemon-owned health publication, not
|
||||
restart/deploy authority.
|
||||
`idunn.daemon_health` document puts on the canonical CNR0 RUDP `schema` channel
|
||||
and writes them into the keepalive store. That is daemon-owned health
|
||||
publication, not restart/deploy authority. On Windows, UDP reset reports from
|
||||
closed one-shot publishers are treated as nonfatal ingress noise so one accepted
|
||||
health frame cannot kill the listener.
|
||||
|
||||
When a fresh daemon-published RUDP health record exists for a target, Idunn uses
|
||||
that record for keepalive planning before falling back to the local command
|
||||
|
|
@ -191,17 +193,17 @@ probe. The record must match the daemon id, health contract, RUDP transport,
|
|||
and freshness window; otherwise the compatibility probe remains fallback
|
||||
evidence.
|
||||
|
||||
The next cuts are updating daemon CultLib dependencies so health and command
|
||||
boundaries publish through `cultnet.transport.rudp.v0`, switching Idunn to those
|
||||
daemon-owned records, promoting the built-in target catalog into Odin-ingested
|
||||
service/provider records, keeping named adapters only for legacy service-manager
|
||||
crossings, and replacing the local operator alarm bridge with a fully
|
||||
Bifrost-owned notification request record.
|
||||
The next cut is Stonks: market-provider health must publish through
|
||||
`cultnet.transport.rudp.v0` instead of remaining HTTP compatibility evidence.
|
||||
After that, continue daemon-by-daemon, promote the built-in target catalog into
|
||||
Odin-ingested service/provider records, keep named adapters only for legacy
|
||||
service-manager crossings, and replace the local operator alarm bridge with a
|
||||
fully Bifrost-owned notification request record.
|
||||
|
||||
Rust no longer gets to claim the transport is imaginary:
|
||||
`vendor/cultnet-rs` includes a narrow stop-and-wait
|
||||
`cultnet.transport.rudp.v0` path for acknowledged CultNet messages over UDP.
|
||||
That is substrate only. Idunn still marks daemon profiles as
|
||||
`vendor/cultnet-rs` includes the canonical CNR0 `cultnet.transport.rudp.v0`
|
||||
session path for acknowledged CultNet messages over UDP. That is substrate
|
||||
only. Idunn still marks daemon profiles as
|
||||
`migration-required` until each daemon actually publishes health and command
|
||||
boundary records through that path.
|
||||
|
||||
|
|
|
|||
|
|
@ -7,12 +7,14 @@ const path = require("path");
|
|||
const { buildConfig, loadCultRuntime } = require("./odin/config.cjs");
|
||||
const { defineOdinDocuments } = require("./odin/documents.cjs");
|
||||
const { createInterfaceDiscovery } = require("./odin/interfaces.cjs");
|
||||
const { createIdunnRudpHealthPublisher, publishIdunnRudpHealth } = require("./odin/idunn-rudp.cjs");
|
||||
const { createLayoutStore } = require("./odin/layout.cjs");
|
||||
const { createStateBuilder } = require("./odin/state.cjs");
|
||||
const { broadcastState, createDashboardServer } = require("./odin/websocket.cjs");
|
||||
|
||||
const config = buildConfig(process.argv.slice(2));
|
||||
fs.mkdirSync(config.stateDir, { recursive: true });
|
||||
const idunnRudpHealthPublisher = createIdunnRudpHealthPublisher(config.idunnRudpHealth);
|
||||
|
||||
const cultRuntime = loadCultRuntime();
|
||||
if (cultRuntime.error) {
|
||||
|
|
@ -67,8 +69,14 @@ async function main() {
|
|||
console.log(`Durable surface cache: ${config.cachePath}`);
|
||||
|
||||
await refresh(dashboardServer.clients);
|
||||
setInterval(() => {
|
||||
refresh(dashboardServer.clients).catch((error) => console.error("refresh failed:", error));
|
||||
scheduleRefresh(dashboardServer.clients);
|
||||
}
|
||||
|
||||
function scheduleRefresh(clients) {
|
||||
setTimeout(() => {
|
||||
refresh(clients)
|
||||
.catch((error) => console.error("refresh failed:", error))
|
||||
.finally(() => scheduleRefresh(clients));
|
||||
}, config.intervalMs);
|
||||
}
|
||||
|
||||
|
|
@ -106,6 +114,7 @@ async function refresh(clients) {
|
|||
error: null,
|
||||
startedAt: new Date(started).toISOString(),
|
||||
};
|
||||
await publishOdinHealth("active", `Odin provider catalog refreshed in ${lastRefresh.durationMs}ms`);
|
||||
} catch (error) {
|
||||
lastRefresh = {
|
||||
completedAt: new Date().toISOString(),
|
||||
|
|
@ -113,10 +122,24 @@ async function refresh(clients) {
|
|||
error: error.message,
|
||||
startedAt: new Date(started).toISOString(),
|
||||
};
|
||||
await publishOdinHealth("failed", `Odin provider refresh failed: ${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function publishOdinHealth(state, detail) {
|
||||
if (!idunnRudpHealthPublisher) return;
|
||||
try {
|
||||
await publishIdunnRudpHealth(idunnRudpHealthPublisher, {
|
||||
state,
|
||||
detail,
|
||||
observedAt: new Date().toISOString(),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Idunn RUDP health publish failed:", error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function persistState(state) {
|
||||
fs.writeFileSync(path.join(config.stateDir, "latest-surface.json"), JSON.stringify(state, null, 2), "utf8");
|
||||
if (!meshNodePromise || !documents.surfaceDefinition) {
|
||||
|
|
|
|||
|
|
@ -29,6 +29,11 @@ function buildConfig(argv) {
|
|||
const gamecultTextDocumentStorePath = args.gamecultTextDocumentStorePath || path.join(repoRoot, "..", "VoidBot", ".voidbot", "private", "gamecult-text-documents.cc");
|
||||
const stonksStateUrl = args.stonksStateUrl || "http://127.0.0.1:8802/market/state";
|
||||
const stonksBurstSize = Math.max(1, Number(args.stonksBurstSize || 8));
|
||||
const idunnRudpHealth = args["idunn-rudp-health"] ? {
|
||||
endpoint: String(args["idunn-rudp-health"]),
|
||||
daemonId: String(args["idunn-daemon"] || "odin"),
|
||||
healthContract: String(args["idunn-health-contract"] || "odin.cultnet-rudp-provider-health"),
|
||||
} : null;
|
||||
const defaultInterfaceBindingStores = [
|
||||
path.join(repoRoot, "..", "VoidBot", ".voidbot", "status", "cultmesh", "voidbot-swarm-state.cc"),
|
||||
path.join(repoRoot, "..", "Bifrost", ".bifrost", "provider-advertisement.cc"),
|
||||
|
|
@ -48,6 +53,7 @@ function buildConfig(argv) {
|
|||
gamecultTextDocumentStorePath,
|
||||
host,
|
||||
interfaceBindingStores,
|
||||
idunnRudpHealth,
|
||||
intervalMs,
|
||||
layoutPath,
|
||||
observationFreshSeconds,
|
||||
|
|
|
|||
254
src/odin/idunn-rudp.cjs
Normal file
254
src/odin/idunn-rudp.cjs
Normal file
|
|
@ -0,0 +1,254 @@
|
|||
"use strict";
|
||||
|
||||
const dgram = require("dgram");
|
||||
const path = require("path");
|
||||
|
||||
const {
|
||||
CultNetRudpSession,
|
||||
decodeRudpPacket,
|
||||
encodeCultNetMessageForWire,
|
||||
encodeRudpPacket,
|
||||
} = require("cultnet-ts");
|
||||
|
||||
const { encode } = require(path.resolve(
|
||||
__dirname,
|
||||
"..",
|
||||
"..",
|
||||
"..",
|
||||
"CultLib",
|
||||
"node_modules",
|
||||
"@msgpack",
|
||||
"msgpack",
|
||||
));
|
||||
|
||||
const CULTNET_RUDP_PROTOCOL_ID = "cultnet.transport.rudp.v0";
|
||||
const IDUNN_HEALTH_RUDP_CONNECTION_ID = 0x1d0d0001;
|
||||
|
||||
function createIdunnRudpHealthPublisher(options) {
|
||||
if (!options) return null;
|
||||
const endpoint = parseEndpoint(options.endpoint);
|
||||
return {
|
||||
daemonId: options.daemonId,
|
||||
endpoint,
|
||||
healthContract: options.healthContract,
|
||||
};
|
||||
}
|
||||
|
||||
async function publishIdunnRudpHealth(publisher, health) {
|
||||
if (!publisher) return;
|
||||
|
||||
const socket = dgram.createSocket(endpointFamily(publisher.endpoint.host));
|
||||
await bindSocket(socket, publisher.endpoint);
|
||||
const receiver = createPacketReceiver(socket);
|
||||
const session = new CultNetRudpSession({
|
||||
connectionId: IDUNN_HEALTH_RUDP_CONNECTION_ID,
|
||||
initialSequence: 1,
|
||||
resendDelayMs: 100,
|
||||
});
|
||||
try {
|
||||
const connect = session.createConnect(Date.now(), new Uint8Array());
|
||||
await sendPacket(socket, publisher.endpoint, connect);
|
||||
await receiveUntil(
|
||||
receiver,
|
||||
session,
|
||||
publisher.endpoint,
|
||||
(packet) => packet.packetType === "accept",
|
||||
5000,
|
||||
"accept",
|
||||
);
|
||||
|
||||
const observedAt = health.observedAt || new Date().toISOString();
|
||||
const record = {
|
||||
daemonId: publisher.daemonId,
|
||||
state: health.state,
|
||||
detail: health.detail,
|
||||
observedAt,
|
||||
healthContract: publisher.healthContract,
|
||||
publicationSource: "daemon-published",
|
||||
transport: CULTNET_RUDP_PROTOCOL_ID,
|
||||
};
|
||||
const payload = encode([
|
||||
record.daemonId,
|
||||
record.state,
|
||||
record.detail,
|
||||
record.observedAt,
|
||||
record.healthContract,
|
||||
record.publicationSource,
|
||||
record.transport,
|
||||
]);
|
||||
const message = {
|
||||
schemaVersion: "cultnet.document_put_raw.v0",
|
||||
messageId: `odin-health:${publisher.daemonId}:${observedAt.replace(/[:.]/g, "-")}`,
|
||||
document: {
|
||||
schemaId: "idunn.daemon_health",
|
||||
recordKey: publisher.daemonId,
|
||||
storedAt: observedAt,
|
||||
payloadEncoding: "messagepack",
|
||||
payload,
|
||||
sourceRuntimeId: "odin-coordinator",
|
||||
sourceRole: "daemon-health-publisher",
|
||||
tags: [CULTNET_RUDP_PROTOCOL_ID],
|
||||
},
|
||||
};
|
||||
const wirePayload = encode(encodeCultNetMessageForWire(message, "cultnet.schema.v0"));
|
||||
const dataPackets = session.sendMany("schema", wirePayload, {
|
||||
reliable: true,
|
||||
ordered: true,
|
||||
nowMs: Date.now(),
|
||||
});
|
||||
const ack = receiveUntil(
|
||||
receiver,
|
||||
session,
|
||||
publisher.endpoint,
|
||||
(packet) => packet.packetType === "ack",
|
||||
500,
|
||||
"ack",
|
||||
).catch(() => undefined);
|
||||
for (const packet of dataPackets) {
|
||||
await sendPacket(socket, publisher.endpoint, packet);
|
||||
}
|
||||
await ack;
|
||||
} finally {
|
||||
receiver.close();
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
async function bindSocket(socket, endpoint) {
|
||||
await new Promise((resolve, reject) => {
|
||||
socket.once("error", reject);
|
||||
socket.bind(0, endpoint.host.includes(":") ? "::" : "0.0.0.0", () => {
|
||||
socket.off("error", reject);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function parseEndpoint(value) {
|
||||
const text = String(value || "").trim();
|
||||
const ipv6 = text.match(/^\[([^\]]+)\]:(\d+)$/);
|
||||
if (ipv6) return { host: ipv6[1], port: parsePort(ipv6[2]) };
|
||||
const index = text.lastIndexOf(":");
|
||||
if (index <= 0) {
|
||||
throw new Error(`Idunn RUDP endpoint must be host:port, got "${value}".`);
|
||||
}
|
||||
return { host: text.slice(0, index), port: parsePort(text.slice(index + 1)) };
|
||||
}
|
||||
|
||||
function parsePort(value) {
|
||||
const port = Number(value);
|
||||
if (!Number.isInteger(port) || port <= 0 || port > 65535) {
|
||||
throw new Error(`Idunn RUDP endpoint port is invalid: ${value}`);
|
||||
}
|
||||
return port;
|
||||
}
|
||||
|
||||
function endpointFamily(host) {
|
||||
return host.includes(":") ? "udp6" : "udp4";
|
||||
}
|
||||
|
||||
async function receiveUntil(receiver, session, endpoint, predicate, timeoutMs, label) {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (Date.now() < deadline) {
|
||||
try {
|
||||
const packet = await receiver.next(Math.min(100, deadline - Date.now()), label);
|
||||
const result = session.receive(packet, Date.now());
|
||||
if (result.reply) {
|
||||
throw new Error("Odin RUDP health publisher received an unexpected reply-required packet.");
|
||||
}
|
||||
if (predicate(packet)) {
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.code !== "ETIMEDOUT") {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
for (const packet of session.dueResends(Date.now())) {
|
||||
await sendPacket(receiver.socket, endpoint, packet);
|
||||
}
|
||||
}
|
||||
throw new Error(`timed out waiting for Idunn RUDP ${label} response after ${timeoutMs}ms`);
|
||||
}
|
||||
|
||||
function createPacketReceiver(socket) {
|
||||
const packets = [];
|
||||
const waiters = [];
|
||||
const errors = [];
|
||||
|
||||
const resolveNext = () => {
|
||||
while (waiters.length > 0 && (packets.length > 0 || errors.length > 0)) {
|
||||
const waiter = waiters.shift();
|
||||
clearTimeout(waiter.timer);
|
||||
if (errors.length > 0) {
|
||||
waiter.reject(errors.shift());
|
||||
} else {
|
||||
waiter.resolve(packets.shift());
|
||||
}
|
||||
}
|
||||
};
|
||||
const onMessage = (wire) => {
|
||||
try {
|
||||
packets.push(decodeRudpPacket(wire));
|
||||
} catch (error) {
|
||||
errors.push(error);
|
||||
}
|
||||
resolveNext();
|
||||
};
|
||||
const onError = (error) => {
|
||||
errors.push(error);
|
||||
resolveNext();
|
||||
};
|
||||
|
||||
socket.on("message", onMessage);
|
||||
socket.on("error", onError);
|
||||
|
||||
return {
|
||||
socket,
|
||||
next(timeoutMs, label = "packet") {
|
||||
if (packets.length > 0) return Promise.resolve(packets.shift());
|
||||
if (errors.length > 0) return Promise.reject(errors.shift());
|
||||
return new Promise((resolve, reject) => {
|
||||
const waiter = {
|
||||
resolve,
|
||||
reject,
|
||||
timer: setTimeout(() => {
|
||||
const index = waiters.indexOf(waiter);
|
||||
if (index >= 0) waiters.splice(index, 1);
|
||||
const error = new Error(`timed out waiting for Idunn RUDP ${label}`);
|
||||
error.code = "ETIMEDOUT";
|
||||
reject(error);
|
||||
}, Math.max(1, timeoutMs)),
|
||||
};
|
||||
waiters.push(waiter);
|
||||
});
|
||||
},
|
||||
close() {
|
||||
socket.off("message", onMessage);
|
||||
socket.off("error", onError);
|
||||
while (waiters.length > 0) {
|
||||
const waiter = waiters.shift();
|
||||
clearTimeout(waiter.timer);
|
||||
const error = new Error("Odin RUDP health publisher closed.");
|
||||
error.code = "ECLOSED";
|
||||
waiter.reject(error);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function sendPacket(socket, endpoint, packet) {
|
||||
const wire = encodeRudpPacket(packet);
|
||||
await new Promise((resolve, reject) => {
|
||||
socket.send(wire, endpoint.port, endpoint.host, (error) => {
|
||||
if (error) reject(error);
|
||||
else resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
CULTNET_RUDP_PROTOCOL_ID,
|
||||
createIdunnRudpHealthPublisher,
|
||||
publishIdunnRudpHealth,
|
||||
};
|
||||
Loading…
Reference in a new issue