refactor(elastic_cognition): Phase B language-advantage pass — dichotomy wave

ctxBudget resolves through an init-built map (O(1), 0 allocs — 18.8ns/op
benchmarked) instead of the per-call profile scan; benchmarks for the
ctxBudget table and the classifySalience regex hot path added. The
escalation walk is documented DELIBERATELY sequential: fallback-after-
failure is oracle semantics — racing routes would issue requests the
oracle never issues (parity break, not advantage). Constitution
(sync.Once) and cloud-config (mutex-guarded once) caches carry over
from the port. Full ladder re-verified: battery green, live parity
100%, gofmt/vet/build clean (verification/phaseb-*.exit, summary.md).
This commit is contained in:
Nick committed 2026-10-13 18:20:02 +01:00
1 parent d4203f8559
commit d68c72fdbc
5 files changed
+59 -9

No files matched your search

+30 -9
View File
@@ -68,17 +68,31 @@ const (
// ctxBudget — Python _ctx_budget: resolve_context_budget(model_id)
// ["num_ctx"]: window - output reserve - tool reserve (0 for local),
// floored at 1024; unknown model and window 0 both fall back to 8192.
//
// Phase B: the profile table is precomputed into a map at init — O(1)
// lookup on a path every ollama request takes (the Python oracle walks a
// dict per call; behavior is identical, frozen truth pins all branches).
type ecProfileResolved struct {
window int
local bool
}
var ecCtxBudgetTable = func() map[string]ecProfileResolved {
m := make(map[string]ecProfileResolved, len(ecModelProfiles))
for _, p := range ecModelProfiles {
window := p.contextWindow
if window == 0 {
window = 8192
}
m[p.id] = ecProfileResolved{window: window, local: p.local}
}
return m
}()
func ctxBudget(modelID string) int {
window, local := 8192, true
for _, p := range ecModelProfiles {
if p.id == modelID {
window = p.contextWindow
if window == 0 {
window = 8192
}
local = p.local
break
}
if p, ok := ecCtxBudgetTable[modelID]; ok {
window, local = p.window, p.local
}
toolReserve := 0
if !local {
@@ -594,6 +608,13 @@ var escalationOrder = []string{"quick", "coding", "complex", "brainstorm", "fall
// executeWithEscalation — Python execute_with_escalation 1:1: classify →
// budget → cheapest-competent route walk, parse on success.
//
// Phase B note: the walk stays DELIBERATELY SEQUENTIAL — racing routes
// would issue requests the oracle never issues (fallback-after-failure
// is the semantics; a failed quick must be observed before coding is
// attempted), so concurrency here would be a parity break, not an
// advantage. The language advantage in this module is the map-indexed
// ctxBudget table and the once-only constitution/config caches.
func executeWithEscalation(text, channel, context string) map[string]any {
salience := classifySalience(text, channel)
budget := cognitiveBudget(salience, channel)
@@ -631,3 +631,21 @@ func TestSmokeSelfTest(t *testing.T) {
testElasticCognition() // panics on any failed assertion
}
// BenchmarkCtxBudget — Phase B: map-indexed profile lookup (init-built
// table) on the per-ollama-request path.
func BenchmarkCtxBudget(b *testing.B) {
models := []string{"qwen2.5:3b", "qwen3:14b", "qwen3.6:27b", "mimo-v2.5-pro", "deterministic", "unknown-model"}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = ctxBudget(models[i%len(models)])
}
}
// BenchmarkClassifySalience — the salience hot path (precompiled regexes).
func BenchmarkClassifySalience(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = classifySalience("fix the parser bug now", "work")
}
}
+1
View File
@@ -0,0 +1 @@
0
+1
View File
@@ -0,0 +1 @@
0
@@ -42,3 +42,12 @@ not port divergences.
- expected T1 for qwen2.5:7b (oracle: T3 — "3b" not in model id).
- expected raw_length=28 (oracle: 26 — post-strip measurement).
- freeze --verify subprocess lacked PYTHONPATH=src.
- expected raw_length=28 (oracle: 26 — post-strip measurement).
- freeze --verify subprocess lacked PYTHONPATH=src.
## Phase B (commit 2) re-verification
- Go ladder re-run: battery green, gofmt/vet/build clean, benchmarks
(BenchmarkCtxBudget 18.8ns/op 0 allocs, BenchmarkClassifySalience) —
evidence phaseb-goladder.log/.exit.
- Live parity re-run: 100% again after the ctxBudget map rewrite —
evidence phaseb-live-parity.log/.exit.