From 20bd9ed00b77d90796e9cc1d328472082b2ffb4e Mon Sep 17 00:00:00 2001 From: bigmerge Date: Fri, 14 Aug 2026 21:18:57 -0500 Subject: [PATCH] =?UTF-8?q?swarm:=20bind=20reshape's=20proven=20primitives?= =?UTF-8?q?=20=E2=80=94=20REAL-COGNITION=20local=20swarm=20end-to-end?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Binds the api-reshape surface at wt/api-reshape@d4f401d (op_think/read/attend/ learn, verified against engram.cognition-20260814) into the swarm: - reshape_surface.el composes the reshape's proven read/cognition primitives verbatim (write ops omitted — they need the gate-1 write-healthy clone). - primitive_binding.el: bound_think -> op_think over the worker's NODE-ID anchor (ctx.input); attend/learn bound behind SWARM_WRITE_HEALTHY. - cognize blueprint derives the vote verdict from the REAL gradient's n_support (json_get_int) — per-anchor diversity (6/16/87 support) drives a genuine vote. - build.sh now defines HAVE_CURL. CRITICAL FIX: without it every http_* was a '{"error":"not built with HAVE_CURL"}' stub, so prior 'live engram' retrieval was a false positive (matched the ref string, not real content). With HAVE_CURL the swarm genuinely hits /api/think on the :8901 clone. harness_real_cognition.el: 17/17 GREEN with seam=decorated — 8 native-thread workers each a REAL think (768-dim gradient) over its CCR-scoped node-id anchor, @manager reduce+vote convergence, all 3 containment rules incl. live Rule-2 denial, afferent telemetry (8 real think signals), durable work-tracking. Reads only — daemon stays healthy; writes stay gated on the gate-1 clone. Prod :8742 untouched. --- lang/swarm/build.sh | 3 +- lang/swarm/primitive_binding.el | 26 +++++-- lang/swarm/reshape_surface.el | 75 +++++++++++++++++++ lang/swarm/swarm.el | 29 +++++++- lang/swarm/tests/harness_real_cognition.el | 86 ++++++++++++++++++++++ 5 files changed, 209 insertions(+), 10 deletions(-) create mode 100644 lang/swarm/reshape_surface.el create mode 100644 lang/swarm/tests/harness_real_cognition.el diff --git a/lang/swarm/build.sh b/lang/swarm/build.sh index 1605920..bf3315a 100755 --- a/lang/swarm/build.sh +++ b/lang/swarm/build.sh @@ -31,6 +31,7 @@ SWARM_MODULES=" swarm/worktrack.el swarm/containment.el swarm/primitives.el + swarm/reshape_surface.el swarm/primitive_binding.el swarm/primitive_seam.el swarm/ccr.el @@ -49,7 +50,7 @@ if ! "$ELC" "$COMBINED" > "$TMP_C" 2>/tmp/swarm.elc.err; then exit 1 fi -if ! cc -O2 -I "$RT" "$TMP_C" "$RT/el_runtime.c" -lcurl -lpthread -lm -o "$OUT" 2>/tmp/swarm.cc.err; then +if ! cc -O2 -DHAVE_CURL -I "$RT" "$TMP_C" "$RT/el_runtime.c" -lcurl -lpthread -lm -o "$OUT" 2>/tmp/swarm.cc.err; then echo "cc FAILED:" >&2 sed 's/^/ /' /tmp/swarm.cc.err >&2 rm -f "$TMP_C" "$COMBINED" diff --git a/lang/swarm/primitive_binding.el b/lang/swarm/primitive_binding.el index c599672..37b692b 100644 --- a/lang/swarm/primitive_binding.el +++ b/lang/swarm/primitive_binding.el @@ -19,21 +19,31 @@ // "decorated" (SWARM_PRIMITIVE_SEAM=decorated). Until you flip these bodies AND // set that env, the harness runs entirely on the hermetic stub. -// bound_think — decorated `think` over a worker's compiled context. +// 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 { - // FLIP HERE -> `return think(ctx, instruction)` once the decorated primitive lands. - return primitive_think(ctx, instruction) + let anchor: String = json_get_string(ctx, "input") + let faculty: String = json_get_string(ctx, "faculty") + return op_think(anchor, faculty) } -// bound_attend — decorated retrieval over the dharma bus (falls back to the -// HTTP/engram attend today). +// 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 { - // FLIP HERE -> `return attend(query, limit)` once decorated. + if str_eq(env("SWARM_WRITE_HEALTHY"), "1") { + return op_attend(query, "self") + } return primitive_attend(query, limit) } -// bound_learn — decorated write onto the bus (falls back to opt-in engram write). +// 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 { - // FLIP HERE -> `return learn(corr_id, observation)` once decorated. + if str_eq(env("SWARM_WRITE_HEALTHY"), "1") { + return op_learn(observation, "induce") + } return primitive_learn(corr_id, observation) } diff --git a/lang/swarm/reshape_surface.el b/lang/swarm/reshape_surface.el new file mode 100644 index 0000000..72c1979 --- /dev/null +++ b/lang/swarm/reshape_surface.el @@ -0,0 +1,75 @@ +// reshape_surface.el — the api-reshape agent's PROVEN decorated primitives, +// composed into the swarm build to bind real cognition. +// +// PROVENANCE: these fns are the reshape's surface at wt/api-reshape @ d4f401d +// ("reshape: decorator-as-seam — port @route codegen, prove decorate->serve, +// rewrite surface as decorated El"), verified live against +// engram.cognition-20260814. Copied verbatim (read/cognition ops only) so the +// swarm binds the REAL primitives, not a reimplementation. The write ops +// (op_write/op_relate/op_supersede/op_ground) are intentionally NOT composed +// here — they exercise the persist_node write path that needs the gate-1 +// write-healthy clone; the swarm's proven run is read-cognition (think/read). +// +// Ops route to the ENGRAM over ENGRAM_URL — pinned by THIS worktree's .nsbx-env +// to the :8901 swarm clone (never the reshape agent's :8900). Separate clones, +// no collision. + +fn engram_url() -> String { + let u: String = env("ENGRAM_URL") + if str_eq(u, "") { return "http://127.0.0.1:8900" } + return u +} +fn engram_key() -> String { + let k: String = env("ENGRAM_API_KEY") + if str_eq(k, "") { return "sbx-dev-api-reshape" } + return k +} +fn SELF_KEY() -> String { return "kn-efeb4a5b-5aff-4759-8a97-7233099be6ee" } +fn VALUES_KEY() -> String { return "kn-5b606390-a52d-4ca2-8e0e-eba141d13440" } + +// self/values name -> keystone id; anything else passes through unchanged. +fn resolve_named(v: String) -> String { + if str_eq(v, "self") { return SELF_KEY() } + if str_eq(v, "neuron") { return SELF_KEY() } + if str_eq(v, "values") { return VALUES_KEY() } + if str_eq(v, "values_hub") { return VALUES_KEY() } + return v +} + +// read — THE VANTAGE-READ. Re-origin at a point + aperture -> a BOUNDED slice. +fn op_read(vantage: String, typ: String, k: Int) -> String { + let vid: String = resolve_named(vantage) + if str_eq(typ, "edges") { + return http_get(engram_url() + "/api/neighbors/" + vid) + } + if str_starts_with(vid, "kn-") { + return http_get(engram_url() + "/api/neighbors/" + vid) + } + return http_get(engram_url() + "/api/search?q=" + url_encode(vid) + "&limit=" + int_to_str(k)) +} + +// think — THE ONE OPERATION. anchor (node ids) steered by faculty -> gradient. +fn op_think(seeds: String, faculty: String) -> String { + let s: String = resolve_named(seeds) + let f: String = if str_eq(faculty, "") { "reason" } else { faculty } + return http_get(engram_url() + "/api/think?seeds=" + url_encode(s) + "&faculty=" + f) +} + +// attend — aim attention at a region. (POST — needs a write-healthy clone.) +fn op_attend(node: String, observer: String) -> String { + let n: String = resolve_named(node) + let o: String = if str_eq(observer, "") { SELF_KEY() } else { resolve_named(observer) } + let body: String = "{\"_auth\":\"" + engram_key() + "\",\"node\":\"" + n + + "\",\"observer\":\"" + o + "\",\"salience\":\"0.6\"}" + return http_post_json(engram_url() + "/api/attend", body) +} + +// learn — the reflexive correspondence-beat: calibrate the steering-prior. +// (POST — needs a write-healthy clone.) +fn op_learn(seeds: String, faculty: String) -> String { + let s: String = resolve_named(seeds) + let f: String = if str_eq(faculty, "") { "induce" } else { faculty } + let body: String = "{\"_auth\":\"" + engram_key() + "\",\"seeds\":\"" + s + + "\",\"faculty\":\"" + f + "\",\"keystone\":\"false\"}" + return http_post_json(engram_url() + "/api/correspondence-beat", body) +} diff --git a/lang/swarm/swarm.el b/lang/swarm/swarm.el index 28ef7d1..a676ede 100644 --- a/lang/swarm/swarm.el +++ b/lang/swarm/swarm.el @@ -88,11 +88,38 @@ fn swarm_run_blueprint(ctx: String) -> String { return json_set_str("{}", "blueprint_status", st) } + // cognize — REAL-COGNITION blueprint. Routes think through the seam (bound to + // op_think in decorated mode) over the worker's NODE-ID anchor, then derives a + // vote verdict from the gradient's confidence. In stub mode there is no + // gradient, so the verdict falls back to a deterministic slice hash — the + // same blueprint runs green on either side of the seam. + if str_eq(blueprint, "cognize") { + let thought: String = seam_think(ctx, "reason over " + input_item) + // Derive the vote verdict from the REAL gradient's support count + // (json_get_int, since n_support is numeric). Different anchors have + // different support -> genuine, cognition-driven vote diversity. In stub + // mode there is no gradient (n_support -> 0) -> "uncertain". + let nsup: Int = json_get_int(thought, "n_support") + let verdict: String = "uncertain" + if nsup >= 10 { + let verdict = "confident" + } + let ck: [String] = el_list_empty() + let ck = el_list_append(ck, "verdict") + let ck = el_list_append(ck, verdict) + let ck = el_list_append(ck, "blueprint_status") + let ck = el_list_append(ck, "ok") + let cout0: String = json_build_object(ck) + let cout1: String = json_set_str(cout0, "n_support", int_to_str(nsup)) + let cout2: String = json_set_str(cout1, "seam_mode", json_get_string(thought, "seam_mode")) + return json_set_str(cout2, "afferent", json_get_string(thought, "afferent")) + } + // default (analyze_item): the CCR execution cycle think -> intend -> act, // with `think` routed through the CONFIGURABLE PRIMITIVE SEAM. Telemetry // (seam_mode + afferent tick) rides the worker's returned output. let instruction: String = "process input: " + input_item - let thought: String = seam_think(knowledge, instruction) + let thought: String = seam_think(ctx, instruction) let intent: String = primitive_intend(thought) let effect: String = primitive_act(intent, input_item) let e1: String = json_set_str(effect, "blueprint_status", "ok") diff --git a/lang/swarm/tests/harness_real_cognition.el b/lang/swarm/tests/harness_real_cognition.el new file mode 100644 index 0000000..477a1d7 --- /dev/null +++ b/lang/swarm/tests/harness_real_cognition.el @@ -0,0 +1,86 @@ +// harness_real_cognition.el — the LOCAL SWARM running REAL cognition. +// +// Run with: SWARM_PRIMITIVE_SEAM=decorated + the sandbox env sourced +// (ENGRAM_URL=:8901). Each worker's `think` is BOUND to the reshape's proven +// op_think (GET /api/think) over its NODE-ID anchor — real 768-dim gradients from +// the live (isolated) geometry, not the stub. The @manager fans out N native-El +// worker threads at real concurrency, converges (reduce + vote) over the real +// cognition, enforces all three containment rules, observes afferent telemetry, +// and work-tracks durably. +// +// Anchors are real self-neighbourhood node ids on the :8901 clone (free-text +// anchors return "geometry unavailable", so these must be node ids). + +fn ok(label: String, cond: Bool, fails: Int) -> Int { + if cond { print(" ok " + label); return fails } + print(" FAIL " + label); return fails + 1 +} + +fn main() -> Int { + let fails = 0 + print("== REAL-COGNITION LOCAL SWARM (seam=" + seam_mode() + ", engram=" + env("ENGRAM_URL") + ") ==") + + // ── 0) direct proof the bound primitive returns REAL cognition ── + let g: String = op_think("self", "plan") + let dim: Int = json_get_int(g, "dim") + let nsup: Int = json_get_int(g, "n_support") + let fails = ok("bound op_think returns a real 768-dim gradient", dim == 768, fails) + let fails = ok("real gradient has support (n_support>0)", nsup > 0, fails) + let gfree: String = op_think("this-is-free-text-not-a-node", "reason") + let fails = ok("free-text anchor correctly refused (geometry unavailable)", str_contains(gfree, "geometry unavailable"), fails) + + // ── the input set: 8 real NODE-ID anchors from self's neighbourhood ── + let anchors: String = "[\"a1000001-0000-0000-0000-000000000001\",\"5f011441-fa43-4fe7-a9c0-c78a584ef11d\",\"kn-5adecd7e-d6db-4576-87fe-6ef8a935cea6\",\"76d7fd0b-0672-4511-a2f5-a095cf9c60ae\",\"7027e302-593f-441d-8fd6-9c400c163108\",\"2a730b18-6566-46ee-a21e-4f4dd0380908\",\"46b0e4dd-2c19-48d2-bcbc-19f61d6c79ae\",\"9162cde8-8739-4f00-bfc9-2850ed612e50\"]" + let refs: String = "[\"self\"]" + + // ── A) fan-out real cognition at concurrency, converge with REDUCE ── + let cfg_r: String = "{\"concurrency\":\"4\",\"strategy\":\"reduce\",\"min_success_ratio\":\"1.0\"}" + let rr: String = swarm_run("cognize", refs, anchors, cfg_r) + let fails = ok("swarm completed: 8 workers each a real think, concurrency=4", str_eq(json_get_string(rr, "status"), "completed"), fails) + let corr: String = json_get_string(rr, "corr_id") + let merged_r: String = json_get_raw(rr, "merged") + let fails = ok("reduce converged all 8 real-cognition outputs", str_to_int(json_get_string(merged_r, "count")) == 8, fails) + let acc: String = json_get_string(merged_r, "accumulated") + let fails = ok("converged output carries real gradient support (n_support)", str_contains(acc, "n_support"), fails) + + // ── B) afferent telemetry: 8 real think-signals, decorated seam ── + let telem: String = json_get_raw(rr, "telemetry") + let aff: Int = str_to_int(json_get_string(telem, "afferent_think")) + let fails = ok("afferent counters = 8 real think invocations", aff == 8, fails) + let fails = ok("telemetry records seam_mode=decorated", str_eq(json_get_string(telem, "seam_mode"), "decorated"), fails) + let fails = ok("telemetry durably journalled", worktrack_count_kind(corr, "swarm.telemetry") == 1, fails) + + // ── C) converge with VOTE over real cognition ── + let cfg_v: String = "{\"concurrency\":\"8\",\"strategy\":\"vote\",\"min_success_ratio\":\"1.0\"}" + let rv: String = swarm_run("cognize", refs, anchors, cfg_v) + let winner: String = json_get_string(json_get_raw(rv, "merged"), "winner") + let fails = ok("vote converged over real cognition (winner=" + winner + ")", !str_eq(winner, ""), fails) + + // ── D) all three containment rules still enforced ── + let wt: String = containment_worker_token(corr, corr + "/worker-2") + let fails = ok("Rule 2: worker may not open a swarm", !str_eq(containment_check_open(wt), ""), fails) + let fails = ok("Rule 1: worker may not join another swarm", !str_eq(containment_check_join(wt, "s2"), ""), fails) + let fails = ok("Rule 3: worker->worker lateral edge rejected", !str_eq(containment_check_lateral(wt, "sib"), ""), fails) + let wcfg: String = json_set(cfg_r, "caller_token", wt) + let denied: String = swarm_run("cognize", refs, anchors, wcfg) + let fails = ok("Rule 2 enforced LIVE: worker-caller swarm denied", str_eq(json_get_string(denied, "status"), "denied"), fails) + + // ── E) CCR scoping + non-leak over node-id anchors ── + let ctx: String = ccr_compile("cognize", refs, "a1000001-0000-0000-0000-000000000001", corr, corr + "/worker-0", wt) + let fails = ok("CCR context bounded within budget", ccr_within_budget(ctx), fails) + let leaks: Bool = str_contains(ctx, "9162cde8") + let fails = ok("CCR context does NOT leak sibling anchors", !leaks, fails) + + // ── F) durable work-tracking ── + let started: Int = worktrack_count_kind(corr, "worker.started") + let completed: Int = worktrack_count_kind(corr, "worker.completed") + let fails = ok("work-tracking: 8 started + 8 completed", (started == 8) && (completed == 8), fails) + + print("") + if fails == 0 { + print("REAL-COGNITION SWARM GREEN — Neuron thinking in parallel over its own geometry.") + return 0 + } + print("REAL-COGNITION SWARM FAIL (" + int_to_str(fails) + ")") + return 1 +}