/* harakiri_brood.h — Brood: agent-aware execution fabric
 *
 * Brood tags tasks with the agent they serve and computes a priority
 * boost from (agent role × system mode). In gaming mode, threads feeding
 * the game get first pick of the CPU; in phone mode, the UI and radio
 * stay responsive while background agents yield.
 *
 * v1: pure policy — mapping tables and boost math, no scheduler surgery.
 * The actual hook into pick_next_task() comes once the policy is proven.
 */
#ifndef _HARAKIRI_BROOD_H
#define _HARAKIRI_BROOD_H

#include <linux/types.h>
#include <harakiri/harakiri_agent.h>

/* System modes — mirrors the prototype's mode engine */
enum hk_mode {
	HK_MODE_PHONE = 0,	/* calls, messages, data; agents quiet */
	HK_MODE_GAMING,		/* game thread first; notifications at the gate */
	HK_MODE_DESKTOP,	/* workstation; throughput over latency */
	HK_NMODES
};

/* How many pids we track. Power of two for cheap hashing. */
#define HK_BROOD_TABLE_SHIFT	10
#define HK_BROOD_TABLE_SIZE	(1u << HK_BROOD_TABLE_SHIFT)
#define HK_BROOD_TABLE_MASK	(HK_BROOD_TABLE_SIZE - 1u)

/* One tagged task */
struct hk_brood_tag {
	u32	pid;		/* 0 = slot free */
	u32	agent_id;	/* hk_agent.id this task serves */
	u8	role;		/* cached hk_role for fast boost math */
	u8	_pad[3];
};

struct hk_brood {
	struct hk_brood_tag	table[HK_BROOD_TABLE_SIZE];
	u8			mode;	/* enum hk_mode */
	u8			_pad[7];
};

/* Boost table: [mode][role] → priority bump (0-39, like nice) */
static const u8 hk_brood_boost_table[HK_NMODES][HK_NROLES] = {
	/* PHONE:   VERA MIRA MERL GAVE BROOD HIVE PAINTR */
	[HK_MODE_PHONE]   = {  5,   5,   3,   8,    0,   2,     6 },
	/* GAMING:  game-feeding Brood workers jump the queue */
	[HK_MODE_GAMING]  = {  3,   2,   5,   8,   20,   2,     2 },
	/* DESKTOP: throughput — Merlin and HiveMind lead */
	[HK_MODE_DESKTOP] = {  4,   4,  15,   8,   10,  12,    14 },
};

/* Hash a pid into the table */
static inline u32 hk_brood_hash(u32 pid)
{
	pid ^= pid >> 16;
	pid *= 0x7feb352du;
	pid ^= pid >> 15;
	return pid & HK_BROOD_TABLE_MASK;
}

/* Tag the calling task as serving an agent. Overwrites any old tag. */
static inline void hk_brood_tag(struct hk_brood *b, u32 pid,
				u32 agent_id, u8 role)
{
	u32 i = hk_brood_hash(pid);
	/* Linear probe for our pid or a free slot */
	u32 j;
	for (j = 0; j < HK_BROOD_TABLE_SIZE; j++) {
		u32 k = (i + j) & HK_BROOD_TABLE_MASK;
		if (b->table[k].pid == pid || b->table[k].pid == 0) {
			b->table[k].pid = pid;
			b->table[k].agent_id = agent_id;
			b->table[k].role = role;
			return;
		}
	}
	/* Table full: evict the hashed slot (oldest-ish, good enough for v1) */
	b->table[i].pid = pid;
	b->table[i].agent_id = agent_id;
	b->table[i].role = role;
}

/* Remove a task's tag (on exit) */
static inline void hk_brood_untag(struct hk_brood *b, u32 pid)
{
	u32 i = hk_brood_hash(pid);
	u32 j;
	for (j = 0; j < HK_BROOD_TABLE_SIZE; j++) {
		u32 k = (i + j) & HK_BROOD_TABLE_MASK;
		if (b->table[k].pid == pid) {
			b->table[k].pid = 0;
			return;
		}
		if (b->table[k].pid == 0)
			return; /* not tagged */
	}
}

/* Priority boost for a pid under the current mode. 0 if untagged. */
static inline u8 hk_brood_boost(struct hk_brood *b, u32 pid)
{
	u32 i = hk_brood_hash(pid);
	u32 j;
	for (j = 0; j < HK_BROOD_TABLE_SIZE; j++) {
		u32 k = (i + j) & HK_BROOD_TABLE_MASK;
		if (b->table[k].pid == pid) {
			u8 role = b->table[k].role;
			if (role >= HK_NROLES)
				return 0;
			return hk_brood_boost_table[b->mode][role];
		}
		if (b->table[k].pid == 0)
			return 0;
	}
	return 0;
}

#endif /* _HARAKIRI_BROOD_H */
