LITOperator / lit_operator /lit_operator.h
OzTianlu's picture
Duplicate from ReasoningKingdom/LITOperator
1ddfac5
Raw History Blame Contribute Delete
12 kB
/*
* lit_operator.h — the LIT DO search operator, the mainline for
* program synthesis.
*
* One intervention per DO node; the tree stores the interventions, not
* machine states. A search path, flattened, is a program: the actions
* from the root to a goal are exactly the program text. The Coder is
* the macro miner that supplies material; it is not the mainline.
*
* The operator owns the DO node arena, the frontier scan, the optional
* global attention (score), and the flat C array; the caller supplies
* the machine/state, the actions, and the oracle. There is no game,
* no DFS engine, and no metadata hash.
*/
#ifndef LIT_OPERATOR_H
#define LIT_OPERATOR_H
#include "lit_core.h"
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define LIT_DO_MAX 256
typedef enum {
LIT_DO_WIRE = 0,
LIT_DO_LOAD = 1
} lit_do_kind;
typedef struct {
lit_do_kind kind;
uint32_t slot;
uint32_t src;
uint64_t gain;
uint64_t weight;
uint64_t value;
} lit_do;
typedef struct lit_donode {
struct lit_donode *parent;
struct lit_donode *first_child;
struct lit_donode *next_sibling;
lit_do op;
uint32_t depth;
uint8_t state;
} lit_donode;
#define LIT_DO_OPEN 0
#define LIT_DO_CLOSED 1
#define LIT_DO_CUT 2
/* Apply one DO to a settled machine, in every cluster: a LOAD writes
* value at slot (masked to L), a WIRE reconnects slot. No tick; the
* caller calls lit_machine_step() next. Returns 0 or -1. */
int lit_do_apply(lit_machine *m, const lit_do *d);
/* Rebuild a node's machine state by replaying its DO chain from the
* root: `m` must already hold the root machine state (before the first
* intervention), and the root node replays nothing. Wiring made by
* WIRE DOs is replayed too, so counterfactual worlds never contaminate
* each other. Returns 0 or -1. */
int lit_do_restore(const lit_donode *node, lit_machine *m);
/* Stable chunked DO node arena: the tree. */
#define LIT_DO_CHUNK 65536
typedef struct {
lit_donode **chunk;
size_t nchunk;
size_t used;
size_t cap;
} lit_do_arena;
lit_donode *lit_do_alloc(lit_do_arena *a, lit_donode *parent,
const lit_do *op, uint32_t depth);
void lit_do_arena_free(lit_do_arena *a);
/* Flat, high-dimensional C array of the DO tree. This is the weight,
* not a walkthrough: parent, the full DO (kind/slot/src/gain/weight/
* value), depth, plus the root machine state stored once. Every other
* node's state is replayed from its DO chain; no per-node state is
* stored. Root has parent UINT32_MAX and no usable DO. It can be
* saved as a binary sidecar and emitted as a static const C source. */
typedef struct {
uint32_t n;
uint32_t words; /* slots * clusters */
uint32_t slots;
uint32_t clusters;
uint32_t bits;
uint32_t *parent; /* UINT32_MAX = root */
uint32_t *kind;
uint32_t *slot;
uint32_t *do_src;
uint64_t *gain; /* DO gain */
uint64_t *weight; /* DO weight */
uint64_t *value; /* LOAD value */
uint32_t *depth;
/* the root machine state, one copy */
uint64_t *root_v; /* words */
uint32_t *root_src; /* words */
uint64_t *root_gain; /* words */
uint64_t *root_weight; /* words */
uint64_t *root_dist; /* clusters */
uint64_t *root_diff; /* words */
} lit_tree;
int lit_tree_flatten(lit_tree *t, const lit_do_arena *a,
const lit_machine *root);
/* Replay flat-tree node i from the stored root state into m. */
int lit_tree_restore(const lit_tree *t, uint32_t i,
lit_machine *m);
void lit_tree_free(lit_tree *t);
int lit_tree_save(const lit_tree *t, const char *path);
int lit_tree_load(lit_tree *t, const char *path);
int lit_tree_write_c(const lit_tree *t, const char *path,
const char *name);
/* The tree keeps one node per DO. Each node's score is the DO's score,
* accumulated along its path from the root (g); the host supplies each
* action's score as its cost. There is no heuristic and no estimate of
* the goal: nothing predicts how far a node is from done, and the goal
* is only the stopping condition. There is no open list: the next node
* to expand is the unexpanded node with the largest score, found by a
* linear comparison (< / >); ties take the earlier node. Duplicate
* observations are dropped when they are generated (seen set). */
typedef struct {
uint32_t kind;
uint32_t id;
uint32_t x;
uint32_t y;
uint32_t cost; /* the DO's score, accumulated into g */
} lit_action;
typedef struct {
uint64_t hi;
uint64_t lo;
} lit_key;
/* A read-only view of one node of the search tree, for a host that
* chooses the next intervention itself (jump/backtrack to any node,
* not just the open frontier). Indices are stable: a node index never
* changes and nodes are only appended. */
typedef struct {
uint32_t parent; /* UINT32_MAX = root */
uint32_t depth;
uint64_t g;
lit_key key;
lit_action act; /* the node's own opaque action */
uint32_t first_child; /* UINT32_MAX = none */
uint32_t next_sibling; /* UINT32_MAX = none */
uint8_t goal;
uint8_t dead;
uint8_t expanded; /* already expanded once (read-only) */
} lit_node_view;
/* Optional host choice. When set on the host, the operator does not
* pick from the frontier: it asks the host for any node index in [0, n)
* and expands that node (a node already expanded may be chosen again:
* backtracking). The seen set is bypassed, so every intervention
* creates a new branch; dead nodes are kept in the tree for the host.
* Return 0 and write the index, or non-zero to stop the search (the
* driver returns LIT_SEARCH_EMPTY). The host owns the policy; the
* operator only exposes the tree. */
typedef int (*lit_choose_fn)(void *ctx, const lit_node_view *nodes,
size_t n, uint32_t *node);
/* Optional global attention. When set (and choose is not), the operator
* asks the host for one score per node of the whole tree (the same
* append-only view array choose sees) and expands the largest by a
* direct comparison; ties take the earlier node. Dead nodes are
* skipped; a chosen goal returns its path. Expanded nodes may be
* chosen again (backtracking) and the seen set is bypassed: every
* intervention creates a new branch. The host supplies the numbers
* (material); the operator owns the comparison and the jump. Return
* non-zero to stop the search (LIT_SEARCH_EMPTY). Score mode requires
* the batch expand callback. */
typedef int (*lit_score_fn)(void *ctx, const lit_node_view *nodes,
size_t n, uint64_t *scores);
typedef struct {
void *ctx;
int (*root)(void *ctx, lit_key *key, int *goal, int *dead);
/* The host rebuilds the state by replaying `path` from the root; no
per-node machine state is stored anywhere. With choose set the
key is informational only. */
int (*actions)(void *ctx, const lit_action *path, size_t path_len,
const lit_action **acts, size_t *n);
/* Batch expansion: one game-engine round trip per parent. The host
evaluates acts[0..nacts) from the state at path and fills keys,
goals, and deads. Returns 0. */
int (*expand)(void *ctx, const lit_action *path, size_t path_len,
const lit_action *acts, const uint64_t *child_g,
size_t nacts, lit_key *keys, int *goals,
int *deads);
int (*eval)(void *ctx, const lit_action *path, size_t path_len,
const lit_action *act, uint64_t child_g,
lit_key *child, int *goal, int *dead);
/* Optional O(1) restore. The operator calls it with the popped
node's key before asking the host for actions; returning 0 means
the world is already at that node, so the host may skip the reset
plus path replay. Non-zero is not an error: the host falls back
to its own restore. */
int (*seek)(void *ctx, const lit_key *key);
void (*event)(void *ctx, const lit_action *path, size_t path_len,
uint64_t pops, uint64_t nodes, size_t open,
double g, double score, int goal);
lit_choose_fn choose; /* optional: jump/backtrack policy */
lit_score_fn score; /* optional: global attention (choose wins) */
} lit_host;
typedef struct {
uint64_t max_steps; /* 0 = unlimited */
uint64_t max_nodes; /* 0 = unlimited; counts pops */
} lit_search_opts;
typedef struct {
lit_action *acts;
size_t n;
uint64_t pops;
uint64_t nodes;
} lit_path;
enum {
LIT_SEARCH_FOUND = 0,
LIT_SEARCH_EMPTY = 1,
LIT_SEARCH_BUDGET = 2,
LIT_SEARCH_ERROR = -1
};
int lit_search_run(const lit_host *host, const lit_search_opts *opts,
lit_path *out);
/* Memory search: the path is the working memory. Every step asks the
* host for its actions (the hard-wired neighbourhood of the current
* state), scores every child with the host's DO score (the action's
* cost), and takes the largest by a direct comparison; the goal is only
* the stopping condition. No open list, no heuristic, no softmax, no
* learned weight. Lives in the operator so the policy is part of the
* machine, not of the host. */
int lit_memory_search(const lit_host *host, const lit_search_opts *opts,
lit_path *out);
/* A persistent search session: the tree and seen set keep growing. A
* found path becomes the anchor for the next level; the next continue
* closes the rest of the tree and grows from that node. The host's
* root callback is used once, at session creation. */
typedef struct lit_search lit_search;
lit_search *lit_search_new(const lit_host *host);
int lit_search_continue(lit_search *s, const lit_search_opts *opts,
lit_path *out);
/* Bulk ingest one causal edge: append a node under `parent` with an
* opaque action, a host key, an accumulated g, and the judge flags.
* The node starts unexpanded, so a later continue can grow from it.
* The seen set records the key for the search. Returns the new index,
* or UINT32_MAX on error. The operator never interprets the action. */
uint32_t lit_search_ingest(lit_search *s, uint32_t parent,
const lit_action *act, lit_key key,
uint64_t g, int goal, int dead);
/* Follow a path's opaque actions through the tree's child links; return
* the node index (UINT32_MAX if a step has no matching child) and, when
* key is non-NULL, the node's key. */
uint32_t lit_search_follow(const lit_search *s, const lit_action *path,
size_t path_len, lit_key *key);
/* Copy one node view: action, key, child links, flags. */
int lit_search_node(const lit_search *s, uint32_t i, lit_node_view *out);
/* Tree statistics: flat nodes, unique keys (seen), open heap size. */
void lit_search_stats(const lit_search *s, uint64_t *nodes,
uint64_t *unique, uint64_t *open);
void lit_search_free(lit_search *s);
void lit_path_free(lit_path *p);
/* Persist a search session: the tree nodes (parent, action, key, g,
* depth, goal, dead), the frontier of unexpanded nodes, and the anchor.
* JSON is the inspectable text form; safetensors is the tensor form, one
* U32/U64/U8 tensor per node array, little-endian. Loading rebinds the
* host without calling its root callback, so the next continue resumes
* the tree; the game itself stays in the host. */
int lit_search_save_json(const lit_search *s, const char *path);
int lit_search_save_safetensors(const lit_search *s, const char *path);
lit_search *lit_search_load_json(const lit_host *host, const char *path);
lit_search *lit_search_load_safetensors(const lit_host *host,
const char *path);
#ifdef __cplusplus
}
#endif
#endif /* LIT_OPERATOR_H */