40bb6ff579
- swarm.el: coordinator running fan-out/converge on El NATIVE threads (thread.el spawn/join) in bounded concurrency waves, order-preserving; convergence strategies collect/merge/vote/reduce; integer per-mille failure threshold (El float division is unreliable — avoided deliberately). - ccr.el: per-worker Compiled Context Routing — retrieval/scoping/compaction into a bounded, minimal package; the compiled-context boundary is the security boundary (a worker cannot receive or leak sibling inputs). - containment.el: the three Swarm containment rules enforced via scope tokens (Rule 1 no join, Rule 2 no open, Rule 3 no lateral edge) + execution-tree lateral-edge check. - primitives.el: attend/think/intend/act/learn seam the swarm composes over, with engram-backed fallbacks and an explicit binding point for the reshape. - prototype json_array_push in el_runtime.h (defined but unprototyped). test_swarm: 12/12 — native fan-out/converge, bounded concurrency, durable tracking, CCR bounding + non-leak, and all three containment rules.
154 lines
6.9 KiB
EmacsLisp
154 lines
6.9 KiB
EmacsLisp
// ccr.el — Compiled Context Routing for work distribution.
|
|
//
|
|
// The same spine as the API's vantage-read, applied per worker. Instead of
|
|
// handing every worker the coordinator's full memory, CCR compiles a MINIMAL,
|
|
// BOUNDED context package scoped to exactly one worker's input (CCR §5, "Compiled
|
|
// Context Injection"; Swarm §9.3, "The Compiled Context Boundary as Security
|
|
// Boundary").
|
|
//
|
|
// The pipeline is CCR §5.1: Retrieval -> Scoping -> Compilation -> (Injection,
|
|
// which here is placing the package into the worker's task envelope).
|
|
//
|
|
// 1. Retrieval — resolve the blueprint's knowledge refs + the input's salient
|
|
// terms against the mind (primitive_attend).
|
|
// 2. Scoping — keep only what THIS input needs; drop everything else. A
|
|
// worker never receives sibling inputs or unrelated memory.
|
|
// 3. Compilation— compact to a CTX string within a token budget (lossless of
|
|
// meaning, smaller in tokens): collapse blank runs, dedupe
|
|
// lines, then bound to the budget.
|
|
//
|
|
// The package a worker receives is therefore (a) sufficient for its task and
|
|
// (b) incapable of leaking what it was never given — the containment boundary
|
|
// and the security boundary are the same object.
|
|
|
|
// ── token budget helpers ─────────────────────────────────────────────────────
|
|
|
|
// ccr_est_tokens — cheap token estimate (~4 chars/token).
|
|
fn ccr_est_tokens(s: String) -> Int {
|
|
return str_len(s) / 4
|
|
}
|
|
|
|
// ccr_default_budget — default per-worker context budget in tokens.
|
|
// Override with CCR_TOKEN_BUDGET.
|
|
fn ccr_default_budget() -> Int {
|
|
let b: String = env("CCR_TOKEN_BUDGET")
|
|
if str_eq(b, "") {
|
|
return 1200
|
|
}
|
|
return str_to_int(b)
|
|
}
|
|
|
|
// ── stage 3: compaction ──────────────────────────────────────────────────────
|
|
|
|
// ccr_compact — collapse blank-line runs and drop exact duplicate lines, then
|
|
// bound the result to `budget` tokens (truncate on a line boundary). Meaning is
|
|
// preserved; token count falls (CCR §5.2).
|
|
fn ccr_compact(text: String, budget: Int) -> String {
|
|
let lines: [String] = str_split_lines(text)
|
|
let n: Int = el_list_len(lines)
|
|
let seen: String = "\n"
|
|
let out: String = ""
|
|
let out_tokens = 0
|
|
let i = 0
|
|
while i < n {
|
|
let ln: String = str_trim(el_list_get(lines, i))
|
|
if str_eq(ln, "") {
|
|
let i = i + 1
|
|
} else {
|
|
let marker: String = "\n" + ln + "\n"
|
|
if str_contains(seen, marker) {
|
|
// duplicate line — skip
|
|
let i = i + 1
|
|
} else {
|
|
let seen = seen + ln + "\n"
|
|
let line_tokens: Int = ccr_est_tokens(ln) + 1
|
|
if out_tokens + line_tokens > budget {
|
|
// budget exhausted — stop (bounded)
|
|
let i = n
|
|
} else {
|
|
let out = out + ln + "\n"
|
|
let out_tokens = out_tokens + line_tokens
|
|
let i = i + 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ── stages 1+2: retrieve + scope ─────────────────────────────────────────────
|
|
|
|
// ccr_retrieve_scoped — pull context relevant to this input and its blueprint
|
|
// knowledge refs, scoped to a fraction of the budget so no single source floods
|
|
// the package. Returns compacted retrieved text (may be empty if the mind is
|
|
// unreachable — the input alone is still a valid minimal context).
|
|
fn ccr_retrieve_scoped(blueprint: String, knowledge_refs: String, input_item: String, budget: Int) -> String {
|
|
let acc: String = ""
|
|
// knowledge_refs is a JSON array of query strings.
|
|
let m: Int = json_array_len(knowledge_refs)
|
|
let i = 0
|
|
while i < m {
|
|
let ref: String = json_array_get(knowledge_refs, i)
|
|
let hit: String = primitive_attend(ref, 3)
|
|
let acc = acc + "# ref:" + ref + "\n" + hit + "\n"
|
|
let i = i + 1
|
|
}
|
|
// the input's own salient text also seeds retrieval
|
|
let hit2: String = primitive_attend(input_item, 3)
|
|
let acc = acc + "# input-context\n" + hit2 + "\n"
|
|
// scope retrieval to ~60% of budget; the input itself gets the rest
|
|
let retr_budget: Int = (budget * 6) / 10
|
|
return ccr_compact(acc, retr_budget)
|
|
}
|
|
|
|
// ── ccr_compile — assemble the bounded per-worker context package ─────────────
|
|
//
|
|
// blueprint : task blueprint name
|
|
// knowledge_refs : JSON array of retrieval queries from the blueprint
|
|
// input_item : THIS worker's single input (and nothing else)
|
|
// corr_id : swarm correlation ID
|
|
// worker_id : this worker's ID
|
|
// scope_token : the worker's containment token (closed boundary)
|
|
//
|
|
// Returns a JSON package: { blueprint, corr_id, worker_id, scope_token,
|
|
// input, knowledge, budget_tokens, compiled_tokens }. `knowledge` is compiled
|
|
// and bounded; the package as a whole is bounded by budget.
|
|
fn ccr_compile(blueprint: String, knowledge_refs: String, input_item: String,
|
|
corr_id: String, worker_id: String, scope_token: String) -> String {
|
|
let budget: Int = ccr_default_budget()
|
|
let knowledge: String = ccr_retrieve_scoped(blueprint, knowledge_refs, input_item, budget)
|
|
|
|
let kv: [String] = el_list_empty()
|
|
let kv = el_list_append(kv, "blueprint")
|
|
let kv = el_list_append(kv, blueprint)
|
|
let kv = el_list_append(kv, "corr_id")
|
|
let kv = el_list_append(kv, corr_id)
|
|
let kv = el_list_append(kv, "worker_id")
|
|
let kv = el_list_append(kv, worker_id)
|
|
let kv = el_list_append(kv, "input")
|
|
let kv = el_list_append(kv, input_item)
|
|
let kv = el_list_append(kv, "knowledge")
|
|
let kv = el_list_append(kv, knowledge)
|
|
let kv = el_list_append(kv, "budget_tokens")
|
|
let kv = el_list_append(kv, int_to_str(budget))
|
|
let pkg: String = json_build_object(kv)
|
|
// stamp the scope token as a nested object, and the measured size
|
|
let pkg2: String = json_set(pkg, "scope_token", scope_token)
|
|
let compiled_tokens: Int = ccr_est_tokens(pkg2)
|
|
let pkg3: String = json_set(pkg2, "compiled_tokens", int_to_str(compiled_tokens))
|
|
return pkg3
|
|
}
|
|
|
|
// ccr_within_budget — did the compiled package stay within its budget?
|
|
// (Retrieval is bounded to 60% and the input is small; this asserts the whole
|
|
// package is bounded — the property distribution relies on.)
|
|
fn ccr_within_budget(pkg: String) -> Bool {
|
|
let budget: Int = str_to_int(json_get_string(pkg, "budget_tokens"))
|
|
let compiled: Int = str_to_int(json_get_string(pkg, "compiled_tokens"))
|
|
// allow a small envelope for JSON framing overhead
|
|
if compiled <= budget + 200 {
|
|
return true
|
|
}
|
|
return false
|
|
}
|