// primitive_binding.el — THE ONE FLIP POINT.
//
// This file is the single seam between the swarm and the real agentic
// primitives. Binding the reshape's decorated primitives is a one-line change
// HERE and nothing else changes anywhere in the swarm.
//
// The api-reshape agent (wt/api-reshape) is wiring the primitives as DECORATED
// El on the dharma_* event bus over the engram — think/attend/learn/ground/assert
// become decorated fns that emit afferent events onto the bus. The moment they
// land, flip `bound_think` (and its siblings) to call them.
//
//   TODAY  (stub fallback, compiles + runs now against :8901):
//       fn bound_think(...) { return primitive_think(ctx, instruction) }
//
//   THE FLIP (when reshape's decorated primitives land — one line each):
//       fn bound_think(...) { return think(ctx, instruction) }   // decorated, on dharma bus
//
// Keep the stub as fallback: `bound_think` is only reached when the seam mode is
// "decorated" (SWARM_PRIMITIVE_SEAM=decorated). Until you flip these bodies AND
// set that env, the harness runs entirely on the hermetic stub.

// bound_think — BOUND to the reshape's proven decorated `think` (op_think),
// real cognition over the engram geometry. The worker's CCR slice carries a
// NODE-ID anchor in ctx.input (free-text anchors return "geometry unavailable");
// think re-origins at that node's region under the faculty and returns a real
// 768-dim gradient.
fn bound_think(ctx: String, instruction: String) -> String {
    let anchor: String = json_get_string(ctx, "input")
    let faculty: String = json_get_string(ctx, "faculty")
    return op_think(anchor, faculty)
}

// bound_attend — BOUND to the reshape's op_attend (POST /api/attend). Needs the
// gate-1 write-healthy clone; falls back to the read-side attend otherwise.
fn bound_attend(query: String, limit: Int) -> String {
    if str_eq(env("SWARM_WRITE_HEALTHY"), "1") {
        return op_attend(query, "self")
    }
    return primitive_attend(query, limit)
}

// bound_learn — BOUND to the reshape's op_learn (correspondence-beat). Needs the
// gate-1 write-healthy clone; falls back to the opt-in journal-only learn.
fn bound_learn(corr_id: String, observation: String) -> String {
    if str_eq(env("SWARM_WRITE_HEALTHY"), "1") {
        return op_learn(observation, "induce")
    }
    return primitive_learn(corr_id, observation)
}
