/* harakiri_hivemind.h — HiveMind: agent coordination
 *
 * The HiveMind is the conductor. It holds the agent registry, one Minnoe
 * ring per agent for telemetry, the Brood scheduler state, and the
 * Spider/Mouse cognition loop. One hk_hivemind per system.
 *
 * Coordination cycle (called once per tick):
 *   1. hk_hive_collect() — drain every agent's ring, fold telemetry into
 *      the cognition activity counters
 *   2. hk_spider_eval()  — Spider proposes a mode from the activity
 *   3. hk_spider_commit()— commit if confident
 *   4. Agents read b.mode via hk_brood_boost() to bias scheduling
 *
 * v1: single-system, no cross-device mesh. The mesh protocol (HiveMind
 * nodes gossiping over Minnoe) is the next layer up.
 */
#ifndef _HARAKIRI_HIVEMIND_H
#define _HARAKIRI_HIVEMIND_H

#include <linux/types.h>
#include <linux/string.h>
#include <harakiri/harakiri_agent.h>
#include <harakiri/harakiri_minnoe.h>
#include <harakiri/harakiri_brood.h>
#include <harakiri/harakiri_spider_mouse.h>

#define HK_HIVE_MAX_AGENTS	32

struct hk_hive_agent_slot {
	struct hk_agent		agent;
	struct hk_minnoe_ring	ring;	/* agent → hive telemetry */
	bool			used;
};

struct hk_hivemind {
	struct hk_hive_agent_slot	slots[HK_HIVE_MAX_AGENTS];
	u32				next_id;
	struct hk_brood		brood;
	struct hk_cognition	cog;
	u32			tick;
};

static inline void hk_hive_init(struct hk_hivemind *h)
{
	memset(h, 0, sizeof(*h));
	h->next_id = 1; /* 0 = invalid */
	hk_cog_init(&h->cog);
	{
		int i;
		for (i = 0; i < HK_HIVE_MAX_AGENTS; i++)
			hk_minnoe_init(&h->slots[i].ring);
	}
}

/*
 * Register an agent. Returns the agent id, or 0 if the table is full.
 * The caller gets a pointer to its ring for sending telemetry.
 */
static inline u32 hk_hive_register(struct hk_hivemind *h, u8 role,
				   const char *name,
				   struct hk_minnoe_ring **ring_out)
{
	int i;
	for (i = 0; i < HK_HIVE_MAX_AGENTS; i++) {
		if (!h->slots[i].used) {
			struct hk_hive_agent_slot *s = &h->slots[i];
			s->used = true;
			s->agent.id = h->next_id++;
			s->agent.role = role;
			s->agent.state = HK_IDLE;
			s->agent.quanta = 0;
			s->agent.quanta_done = 0;
			s->agent.eta_x1000 = 1000;
			s->agent.last_run_ns = 0;
			s->agent.total_ns = 0;
			if (name) {
				int j;
				for (j = 0; j < 15 && name[j]; j++)
					s->agent.name[j] = name[j];
				s->agent.name[j] = '\0';
			} else {
				s->agent.name[0] = '\0';
			}
			if (ring_out)
				*ring_out = &s->ring;
			return s->agent.id;
		}
	}
	return 0;
}

/* Find a slot by agent id. NULL if not registered. */
static inline struct hk_hive_agent_slot *
hk_hive_find(struct hk_hivemind *h, u32 id)
{
	int i;
	for (i = 0; i < HK_HIVE_MAX_AGENTS; i++) {
		if (h->slots[i].used && h->slots[i].agent.id == id)
			return &h->slots[i];
	}
	return NULL;
}

/*
 * One coordination tick. Drains telemetry, runs the Spider, commits
 * the mode. Returns true if the mode changed this tick.
 */
static inline bool hk_hive_tick(struct hk_hivemind *h)
{
	u32 activity[HK_NROLES] = { 0 };
	int i;
	bool changed;

	/* 1. Collect: drain every ring, count quanta per role */
	for (i = 0; i < HK_HIVE_MAX_AGENTS; i++) {
		struct hk_hive_agent_slot *s = &h->slots[i];
		struct hk_telemetry t;
		if (!s->used)
			continue;
		while (hk_minnoe_recv(&s->ring, &t) == 0) {
			if (t.agent_id != s->agent.id)
				continue; /* spoofed — drop */
			s->agent.quanta_done = t.quanta_done;
			s->agent.eta_x1000 = t.eta_x1000;
			s->agent.state = t.state;
			if (s->agent.role < HK_NROLES)
				activity[s->agent.role] += t.quanta_done;
		}
	}

	/* 2+3. Spider evaluates and commits */
	hk_spider_eval(&h->cog, activity);
	changed = hk_spider_commit(&h->cog);

	/* 4. Push the mode into Brood so boosts follow */
	h->brood.mode = h->cog.mode;
	h->tick++;
	return changed;
}

#endif /* _HARAKIRI_HIVEMIND_H */
