1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
* Net C SDK
*
* Network Event Transport — a latency-first encrypted mesh protocol.
*
* One header, one shared library. This is the entire C SDK.
* Links against libnet.so (Linux), libnet.dylib (macOS), or net.dll (Windows).
*
* Thread Safety: All functions are thread-safe. Handles can be shared across threads.
*
* Memory: Handles from net_init() must be freed with net_shutdown().
* Poll results from net_poll_ex() must be freed with net_free_poll_result().
* Strings from net_generate_keypair() must be freed with net_free_string().
*/
#ifndef NET_SDK_H
#define NET_SDK_H
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ========================================================================= */
/* Types */
/* ========================================================================= */
/* Opaque handle to the event bus. */
typedef void* net_handle_t;
/*
* Error codes.
*
* Kept in sync with the Rust-side `NetError` enum and with the Go
* binding's copy at `bindings/go/net/net.h`. The library has a
* regression test that scans both headers to detect drift.
*/
typedef enum {
NET_SUCCESS = 0,
NET_ERR_NULL_POINTER = -1,
NET_ERR_INVALID_UTF8 = -2,
NET_ERR_INVALID_JSON = -3,
NET_ERR_INIT_FAILED = -4,
NET_ERR_INGESTION_FAILED = -5,
NET_ERR_POLL_FAILED = -6,
NET_ERR_BUFFER_TOO_SMALL = -7,
NET_ERR_SHUTTING_DOWN = -8,
/*
* Response byte count exceeds `c_int::MAX`. The data was already
* copied into the caller's buffer (so resizing won't help — that
* is BUFFER_TOO_SMALL); the caller's int counter just can't
* represent the count. Surfaced by net_poll / net_stats when
* their JSON output is multi-gigabyte.
*/
NET_ERR_INT_OVERFLOW = -9,
/*
* A stream handle was passed to a send-family FFI for a
* net_handle_t that did not create it. The FFI layer rejects
* such cross-handle traffic to prevent silent leaks between
* sessions when a caller bug crosses handles.
*/
NET_ERR_MISMATCHED_HANDLES = -10,
/*
* `CString::new` failure: the input bytes are valid UTF-8 (by
* Rust's String invariant) but contain an interior NUL byte
* that the C ABI's NUL-terminated string can't carry. Pre-fix
* this was reported as NET_ERR_INVALID_UTF8, which was wrong:
* the input is UTF-8-valid; it just has a NUL where C expects
* none. Bindings that branch on the typed error get the right
* cause now.
*/
NET_ERR_INTERIOR_NUL = -11,
NET_ERR_UNKNOWN = -99
} net_error_t;
/* Ingestion receipt. */
typedef struct {
uint16_t shard_id;
uint64_t timestamp;
} net_receipt_t;
/* A single stored event. */
typedef struct {
const char* id;
size_t id_len;
const char* raw;
size_t raw_len;
uint64_t insertion_ts;
uint16_t shard_id;
} net_event_t;
/* Poll result containing events and cursor. */
typedef struct {
net_event_t* events;
size_t count;
char* next_id;
int has_more;
} net_poll_result_t;
/* Ingestion statistics. */
typedef struct {
uint64_t events_ingested;
uint64_t events_dropped;
uint64_t batches_dispatched;
} net_stats_t;
/* ========================================================================= */
/* Lifecycle */
/* ========================================================================= */
/*
* Initialize a new node.
*
* @param config_json JSON config string (null-terminated), or NULL for defaults.
* @return Handle to the node, or NULL on failure.
*
* Example: net_init("{\"num_shards\": 4}")
* Example: net_init(NULL) // defaults
*/
net_handle_t net_init(const char* config_json);
/*
* Shut down the node and free all resources.
* The handle is invalid after this call.
*
* @return 0 on success, negative error code on failure.
*/
int net_shutdown(net_handle_t handle);
/*
* Get the library version string (static, do not free).
*/
const char* net_version(void);
/*
* Get the number of shards.
*
* @return Number of shards, or 0 if handle is NULL.
*/
uint16_t net_num_shards(net_handle_t handle);
/* ========================================================================= */
/* Ingestion */
/* ========================================================================= */
/*
* Ingest a raw JSON string (fastest path, no parsing).
*
* @param json JSON string (not null-terminated — length is explicit).
* @param len Length of the JSON string in bytes.
* @return 0 on success, negative error code on failure.
*/
int net_ingest_raw(net_handle_t handle, const char* json, size_t len);
/*
* Ingest a raw JSON string and get a receipt.
*
* @param json JSON string.
* @param len Length of the JSON string in bytes.
* @param out Receipt output (shard_id, timestamp). May be NULL.
* @return 0 on success, negative error code on failure.
*/
int net_ingest_raw_ex(net_handle_t handle, const char* json, size_t len, net_receipt_t* out);
/*
* Ingest a single event (parses JSON for validation).
*
* @param event_json JSON event string.
* @param len Length of the event string in bytes.
* @return 0 on success, negative error code on failure.
*/
int net_ingest(net_handle_t handle, const char* event_json, size_t len);
/*
* Ingest multiple raw JSON strings in a batch.
*
* @param jsons Array of pointers to JSON strings.
* @param lens Array of lengths for each string.
* @param count Number of events.
* @return Number of successfully ingested events, or negative error code.
*/
int net_ingest_raw_batch(
net_handle_t handle,
const char** jsons,
const size_t* lens,
size_t count
);
/*
* Ingest events from a JSON array string.
*
* @param events_json JSON array (null-terminated).
* @return Number of ingested events, or negative error code.
*/
int net_ingest_batch(net_handle_t handle, const char* events_json);
/* ========================================================================= */
/* Consumption */
/* ========================================================================= */
/*
* Poll events (JSON interface).
*
* @param request_json JSON request, e.g. {"limit": 100}. NULL for defaults.
* @param out_buffer Output buffer for JSON response.
* @param buffer_len Size of output buffer.
* @return Bytes written on success, negative error code on failure.
*/
int net_poll(
net_handle_t handle,
const char* request_json,
char* out_buffer,
size_t buffer_len
);
/*
* Poll events (structured interface, no JSON overhead).
*
* @param limit Maximum number of events.
* @param cursor Resume cursor (null-terminated), or NULL for start.
* @param out Poll result output. Must be freed with net_free_poll_result().
* @return 0 on success, negative error code on failure.
*/
int net_poll_ex(
net_handle_t handle,
size_t limit,
const char* cursor,
net_poll_result_t* out
);
/*
* Free a poll result returned by net_poll_ex().
*/
void net_free_poll_result(net_poll_result_t* result);
/* ========================================================================= */
/* Statistics */
/* ========================================================================= */
/*
* Get statistics (JSON interface).
*
* @param out_buffer Output buffer for JSON.
* @param buffer_len Size of output buffer.
* @return Bytes written on success, negative error code on failure.
*/
int net_stats(net_handle_t handle, char* out_buffer, size_t buffer_len);
/*
* Get statistics (structured, no JSON overhead).
*
* @param out Stats output.
* @return 0 on success, negative error code on failure.
*/
int net_stats_ex(net_handle_t handle, net_stats_t* out);
/* ========================================================================= */
/* Utilities */
/* ========================================================================= */
/*
* Flush pending batches to the adapter.
*
* @return 0 on success, negative error code on failure.
*/
int net_flush(net_handle_t handle);
/*
* Generate a new keypair for encrypted mesh transport.
*
* @return JSON string with hex-encoded public_key and secret_key.
* Caller must free with net_free_string(). NULL if not available.
*/
char* net_generate_keypair(void);
/*
* Free a string returned by net_generate_keypair() or similar.
*/
void net_free_string(char* s);
/* =========================================================================
* Redis Streams consumer-side dedup helper
* (compiled when the Rust cdylib has the `redis` feature on).
*
* The Net Redis adapter writes a stable `dedup_id` field on every
* XADD entry of the form
*
* {producer_nonce:hex}:{shard_id}:{sequence_start}:{i}
*
* When the producer's MULTI/EXEC times out client-side but runs
* server-side anyway, the retry produces a duplicate stream entry
* with a distinct server-generated `*` id but the same `dedup_id`.
* This helper maintains an LRU-bounded set of seen ids and answers
* a test-and-insert query so consumers can filter at consume time.
*
* Each handle wraps an LRU-bounded set protected by an internal
* mutex; concurrent calls from multiple threads on the same handle
* are safe but serialize. Production callers typically instantiate
* one helper per consumer thread and key on the `dedup_id` field
* extracted from each XRANGE / XREAD entry. See `include/README.md`
* and the language-specific binding READMEs for runnable examples.
* ========================================================================= */
typedef struct net_redis_dedup_s net_redis_dedup_t;
/*
* Create a new dedup helper.
*
* @param capacity LRU capacity. `0` selects the default (4096).
* Production callers should size to their dedup
* window — a consumer at ~10k events/sec with a
* 1 min window wants ~600,000.
* @return Heap-allocated handle. Never returns NULL. Free with
* `net_redis_dedup_free`.
*/
net_redis_dedup_t* net_redis_dedup_new(size_t capacity);
/*
* Free a helper handle. NULL is a no-op.
*/
void net_redis_dedup_free(net_redis_dedup_t* handle);
/*
* Test-and-insert.
*
* @return 1 — duplicate (caller should skip the entry)
* 0 — new (caller should process AND we've now
* marked it seen)
* -1 — NULL handle or NULL dedup_id
* -2 — invalid UTF-8 in dedup_id
*/
int net_redis_dedup_is_duplicate(net_redis_dedup_t* handle, const char* dedup_id);
/*
* Number of distinct ids currently tracked. Returns 0 on NULL
* handle (mirrors the "no ids" semantic).
*/
size_t net_redis_dedup_len(net_redis_dedup_t* handle);
/*
* Configured maximum capacity. Returns 0 on NULL handle.
*/
size_t net_redis_dedup_capacity(net_redis_dedup_t* handle);
/*
* Returns 1 if no ids are tracked, 0 if the helper has at least
* one id, -1 on NULL handle.
*/
int net_redis_dedup_is_empty(net_redis_dedup_t* handle);
/*
* Clear all tracked ids. Use after a consumer-group rebalance.
* NULL is a no-op.
*/
void net_redis_dedup_clear(net_redis_dedup_t* handle);
/* ========================================================================= */
/* Aggregator: registry RPC client + channel visibility setter. */
/* ========================================================================= */
/*
* Stage 5 of SDK_AGGREGATOR_SUBNET_PLAN.md. Client surface for
* the daemon's `aggregator.registry` RPC service. All ops are
* blocking against the shared mesh-FFI tokio runtime — call
* from a non-tokio thread (CGo / cgo-fronted Go is fine).
*/
/* Opaque handle for a RegistryClient. */
typedef struct net_registry_client_handle_t net_registry_client_handle_t;
/* Error-kind discriminants. Stable across SDK releases. */
#define NET_REGISTRY_OK 0
#define NET_REGISTRY_ERR_TRANSPORT 1
#define NET_REGISTRY_ERR_CODEC 2
#define NET_REGISTRY_ERR_UNKNOWN_TEMPLATE 3
#define NET_REGISTRY_ERR_DUPLICATE_GROUP_NAME 4
#define NET_REGISTRY_ERR_SPAWN_REJECTED 5
#define NET_REGISTRY_ERR_SPAWN_NOT_SUPPORTED 6
#define NET_REGISTRY_ERR_UNKNOWN_KIND 7
#define NET_REGISTRY_ERR_INVALID_ARGS 99
/* Visibility discriminants, mirroring the substrate's `Visibility` enum.
* Operator code referring to these by literal value (not just name)
* stays correct across SDK releases. */
typedef enum {
NET_VISIBILITY_GLOBAL = 0,
NET_VISIBILITY_PARENT_VISIBLE = 1,
NET_VISIBILITY_EXPORTED = 2,
NET_VISIBILITY_SUBNET_LOCAL = 3,
} net_visibility_t;
/*
* Build a RegistryClient from an existing net_mesh handle.
* Returns NULL on null input.
* Free with `net_registry_client_free`.
*/
net_registry_client_handle_t* net_registry_client_new(void* mesh_handle);
/* Free a RegistryClient. Idempotent on NULL. */
void net_registry_client_free(net_registry_client_handle_t* handle);
/*
* Override the per-call deadline in milliseconds. `millis == 0`
* resets to the substrate default.
*/
void net_registry_client_set_deadline(
net_registry_client_handle_t* handle,
uint64_t millis);
/*
* Enumerate groups registered on `target_node_id`. Returns a
* JSON-encoded `[RegistryGroupSummaryJson]` string — caller
* frees via `net_free_string`. On error, writes the error
* discriminant to `*out_error_kind` and returns NULL. The
* `out_error_kind` pointer is required (non-NULL).
*
* JSON shape:
* [{"name": "...", "group_seed_hex": "...", "replicas": [
* {"generation": N, "healthy": true|false,
* "diagnostic": "..."|null, "placement_node_id": N|null}
* ]}, ...]
*/
char* net_registry_client_list(
net_registry_client_handle_t* handle,
uint64_t target_node_id,
int* out_error_kind);
/*
* Spawn a new group by referencing a daemon-side template by
* name. Returns a JSON-encoded `RegistryGroupSummaryJson` for
* the spawned group; caller frees via `net_free_string`.
*
* `template_name` and `group_name` are NUL-terminated UTF-8
* strings owned by the caller for the duration of the call.
*/
char* net_registry_client_spawn(
net_registry_client_handle_t* handle,
uint64_t target_node_id,
const char* template_name,
const char* group_name,
uint8_t replica_count,
int* out_error_kind);
/*
* Tear down a registered group. Returns:
* 1 — group existed and was stopped
* 0 — no such group was registered
* -1 — transport / codec / invalid-args failure (consult
* `out_error_kind`)
*/
int net_registry_client_unregister(
net_registry_client_handle_t* handle,
uint64_t target_node_id,
const char* group_name,
int* out_error_kind);
/*
* Operator-facing detail string for the most recent non-OK op.
* Returns a NUL-terminated C string owned by the handle —
* valid until the next op on the handle (which may overwrite)
* or until the handle is freed. Returns NULL when no error has
* been recorded.
*/
const char* net_registry_last_error_detail(net_registry_client_handle_t* handle);
/*
* Register a channel with a specific visibility tier. Mirrors
* `Mesh::register_channel` at the C boundary. Returns
* `NET_REGISTRY_OK` on success or a typed error code. The
* mesh must have a ChannelConfigRegistry installed — true for
* any net_mesh built via `net_mesh_new`.
*
* `name` is a NUL-terminated UTF-8 channel name.
*/
int net_register_channel(
void* mesh_handle,
const char* name,
int visibility);
/* ─── FoldQueryClient ────────────────────────────────────────────────────── */
/* Opaque handle for a FoldQueryClient. */
typedef struct net_fold_query_client_handle_t net_fold_query_client_handle_t;
/*
* Build a FoldQueryClient from an existing net_mesh handle.
* Returns NULL on null input. Free with
* `net_fold_query_client_free`.
*/
net_fold_query_client_handle_t* net_fold_query_client_new(void* mesh_handle);
/* Free a FoldQueryClient. Idempotent on NULL. */
void net_fold_query_client_free(net_fold_query_client_handle_t* handle);
/*
* Override the cache TTL in milliseconds. `millis == 0` disables
* the cache entirely.
*/
void net_fold_query_client_set_ttl(
net_fold_query_client_handle_t* handle,
uint64_t millis);
/*
* Override the per-call deadline in milliseconds.
* `millis == 0` resets to the substrate default.
*/
void net_fold_query_client_set_deadline(
net_fold_query_client_handle_t* handle,
uint64_t millis);
/*
* Query the aggregator's latest cached summaries. Cache hit
* returns immediately; miss issues a wire RPC, caches the
* response, and returns. Returns a JSON-encoded
* `[SummaryAnnouncementJson]` string — caller frees via
* `net_free_string`. On error, writes the error discriminant
* to `*out_error_kind` and returns NULL.
*
* JSON shape:
* [{"fold_kind": N, "source_subnet": "...", "generation": N,
* "buckets": [{"name": "...", "count": N}, ...]}, ...]
*/
char* net_fold_query_client_query_latest(
net_fold_query_client_handle_t* handle,
uint64_t target_node_id,
uint16_t kind,
int* out_error_kind);
/*
* Force a fresh `SummarizeNow` query — never cached. Same JSON
* shape as `_query_latest`.
*/
char* net_fold_query_client_query_summarize_now(
net_fold_query_client_handle_t* handle,
uint64_t target_node_id,
uint16_t kind,
int* out_error_kind);
/* Drop every cached entry. */
void net_fold_query_client_invalidate_cache(
net_fold_query_client_handle_t* handle);
/* Drop only cache entries matching `target_node_id`. */
void net_fold_query_client_invalidate_target(
net_fold_query_client_handle_t* handle,
uint64_t target_node_id);
/*
* Operator-facing detail string for the most recent non-OK
* fold-query op on this handle. Same valid-until contract as
* `net_registry_last_error_detail`.
*/
const char* net_fold_query_last_error_detail(
net_fold_query_client_handle_t* handle);
#ifdef __cplusplus
}
#endif
#endif /* NET_SDK_H */