File size: 22,442 Bytes
9425aed | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | !=====================================================================
! SOV_KNOWLEDGE β Sovereign Knowledge Base for SovMonster Agents
!
! WORM-attested semantic chunks. Zero external deps. No cloud RAG.
! Fortran 2018 Β· Blake3 provenance Β· Ο-decay trust Β· cosine search
!
! Stack:
! bob_worm.f90 β Blake3 + append-only chain height
! sov_monster_kernel β Bifrost Ed25519 sign (optional seal)
! Embeddings are pure-Fortran spectral sketches (MLIR-fusible loops).
! No Python. No Ollama. No wrapper class. Agents read the ledger.
!
! Ahmad Ali Parr Β· SnapKitty Collective Β· 2026
! PAR-021: Runtime knowledge layer for sovereign agents
!=====================================================================
module sov_knowledge
use, intrinsic :: iso_c_binding, only: c_int64_t, c_ptr, c_f_pointer, &
c_loc, c_size_t, c_null_ptr, c_char, c_associated
use, intrinsic :: iso_fortran_env, only: int64, real64, int8
use bob_kinds
use bob_worm, only: bob_worm_chain, blake3_hash_string
implicit none
private
integer, parameter, public :: KB_EMBED_DIM = 64
integer, parameter, public :: KB_ID_LEN = 64
integer, parameter, public :: KB_SIG_LEN = 64
integer, parameter, public :: KB_DEFAULT_CAP = 1024
real(dp), parameter :: PHI_INV = 0.6180339887498948482_dp
real(dp), parameter :: EPS = 1.0e-15_dp
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! KNOWLEDGE CHUNK (WORM-immutable once sealed)
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
type, public :: knowledge_chunk
character(len=KB_ID_LEN) :: chunk_id = ''
character(len=KB_SIG_LEN) :: source_sig = ''
integer(int64) :: created_at = 0_int64
real(dp), allocatable :: embedding(:)
character(len=:), allocatable :: content
logical :: is_verified = .false.
end type knowledge_chunk
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! KNOWLEDGE STORE (WORM-chain backed)
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
type, public :: knowledge_store
type(knowledge_chunk), allocatable :: chunks(:)
integer :: count = 0
integer :: capacity = KB_DEFAULT_CAP
type(bob_worm_chain) :: worm
logical :: initialized = .false.
contains
procedure, public :: init => knowledge_init
procedure, public :: append => knowledge_append
procedure, public :: search => knowledge_search
procedure, public :: verify => knowledge_verify
procedure, public :: trust_score => knowledge_trust_score
procedure, public :: load_from_worm => knowledge_load_from_worm
procedure, public :: destroy => knowledge_destroy
end type knowledge_store
! Module-level singleton for measurement/training hooks
type(knowledge_store), public, save :: sovereign_kb
logical, public, save :: kb_initialized = .false.
public :: cosine_sim
public :: generate_embedding
public :: knowledge_tau
public :: knowledge_penalty_scale
public :: ensure_sovereign_kb
! C ABI for PL/I / COBOL / INTERCAL agents
public :: sov_knowledge_init
public :: sov_knowledge_append
public :: sov_knowledge_search
public :: sov_knowledge_verify
public :: sov_knowledge_count
public :: sov_knowledge_tau
contains
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! cosine_sim β pure Fortran cosine similarity (MLIR-fusible)
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
pure function cosine_sim(a, b) result(sim)
real(dp), intent(in) :: a(:), b(:)
real(dp) :: sim, dot_prod, norm_a, norm_b
integer :: n, i
n = min(size(a), size(b))
if (n <= 0) then
sim = 0.0_dp
return
end if
dot_prod = 0.0_dp
norm_a = 0.0_dp
norm_b = 0.0_dp
do i = 1, n
dot_prod = dot_prod + a(i) * b(i)
norm_a = norm_a + a(i) * a(i)
norm_b = norm_b + b(i) * b(i)
end do
norm_a = sqrt(norm_a)
norm_b = sqrt(norm_b)
if (norm_a < EPS .or. norm_b < EPS) then
sim = 0.0_dp
else
sim = dot_prod / (norm_a * norm_b)
end if
end function cosine_sim
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! generate_embedding β spectral character sketch (no external model)
!
! Deterministic 64-dim vector from content: n-gram buckets scaled by
! Οβ»α΅ position weights, L2-normalized. Same path for chunks + queries.
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
subroutine generate_embedding(content, embed)
character(len=*), intent(in) :: content
real(dp), allocatable, intent(out) :: embed(:)
integer :: i, n, b0, b1
integer :: c, c_prev
real(dp) :: w, nrm
allocate(embed(KB_EMBED_DIM))
embed = 0.0_dp
n = len_trim(content)
if (n <= 0) then
embed(1) = 1.0_dp
return
end if
c_prev = 0
do i = 1, n
c = iachar(content(i:i))
w = PHI_INV ** mod(i - 1, 32)
b0 = mod(c * 31 + i, KB_EMBED_DIM) + 1
b1 = mod(c * 17 + c_prev * 13 + i * 7, KB_EMBED_DIM) + 1
embed(b0) = embed(b0) + w
embed(b1) = embed(b1) + w * PHI_INV
c_prev = c
end do
nrm = sqrt(sum(embed * embed))
if (nrm > EPS) then
embed = embed / nrm
else
embed(1) = 1.0_dp
end if
end subroutine generate_embedding
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! knowledge_tau β Ο-decay temperature from hit count
! Ο_k = Οβ Β· Οβ»α΅ (k = number of verified context chunks)
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
pure function knowledge_tau(tau_0, k_hits) result(tau_k)
real(dp), intent(in) :: tau_0
integer, intent(in) :: k_hits
real(dp) :: tau_k
integer :: k
k = max(0, k_hits)
tau_k = tau_0 * (PHI_INV ** k)
tau_k = max(tau_k, 1.0e-12_dp)
end function knowledge_tau
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! knowledge_penalty_scale β trust-aware gradient multiplier
! scale = 1 β Ο Β· (unverified / total)
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
pure function knowledge_penalty_scale(n_total, n_unverified) result(scale)
integer, intent(in) :: n_total, n_unverified
real(dp) :: scale, penalty
if (n_total <= 0) then
scale = 1.0_dp
return
end if
penalty = real(n_unverified, dp) / real(n_total, dp)
scale = 1.0_dp - PHI_INV * penalty
scale = max(scale, PHI_INV) ! never fully kill the gradient
end function knowledge_penalty_scale
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! hex helpers (local β bob_worm bytes_to_hex is private)
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
pure function digest_to_hex(b) result(hex)
integer(i8), intent(in) :: b(32)
character(len=64) :: hex
character(len=16), parameter :: H = '0123456789abcdef'
integer :: i, hi, lo
do i = 1, 32
hi = ishft(iand(int(b(i), kind=4), 240), -4) + 1
lo = iand(int(b(i), kind=4), 15) + 1
hex(2*i-1:2*i-1) = H(hi:hi)
hex(2*i:2*i) = H(lo:lo)
end do
end function digest_to_hex
pure function bytes_to_hex64(b, n) result(hex)
integer(i8), intent(in) :: b(:)
integer, intent(in) :: n
character(len=64) :: hex
character(len=16), parameter :: H = '0123456789abcdef'
integer :: i, m, hi, lo
hex = repeat('0', 64)
m = min(n, 32)
do i = 1, m
hi = ishft(iand(int(b(i), kind=4), 240), -4) + 1
lo = iand(int(b(i), kind=4), 15) + 1
hex(2*i-1:2*i-1) = H(hi:hi)
hex(2*i:2*i) = H(lo:lo)
end do
end function bytes_to_hex64
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! knowledge_init
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
subroutine knowledge_init(self, capacity)
class(knowledge_store), intent(inout) :: self
integer, intent(in), optional :: capacity
integer :: cap
cap = KB_DEFAULT_CAP
if (present(capacity)) cap = max(1, capacity)
if (allocated(self%chunks)) deallocate(self%chunks)
allocate(self%chunks(cap))
self%count = 0
self%capacity = cap
call self%worm%init(capacity=max(cap, 256))
self%initialized = .true.
end subroutine knowledge_init
subroutine ensure_sovereign_kb()
if (.not. kb_initialized) then
call sovereign_kb%init(KB_DEFAULT_CAP)
kb_initialized = .true.
end if
end subroutine ensure_sovereign_kb
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! knowledge_append β content + source key β WORM-attested chunk
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
subroutine knowledge_append(self, content, source_key)
class(knowledge_store), intent(inout) :: self
character(len=*), intent(in) :: content
character(len=*), intent(in) :: source_key
type(knowledge_chunk) :: new_chunk
integer(i8) :: digest(32), sig_digest(32)
character(len=:), allocatable :: material, sig_material
integer :: n
if (.not. self%initialized) call self%init()
n = len_trim(content)
material = content(1:n) // '|' // trim(source_key)
call blake3_hash_string(material, digest)
new_chunk%chunk_id = digest_to_hex(digest)
! Provenance sig: Blake3(source_key || content) β air-gapped, no keyring required.
! Full Ed25519 Bifrost seal is applied at the agent boundary via sov_bifrost_sign.
sig_material = trim(source_key) // '|' // content(1:n)
call blake3_hash_string(sig_material, sig_digest)
new_chunk%source_sig = digest_to_hex(sig_digest)
new_chunk%created_at = int(self%worm%height(), int64)
call generate_embedding(content(1:n), new_chunk%embedding)
new_chunk%content = content(1:n)
new_chunk%is_verified = .true.
! Seal into WORM ledger (tamper-evident height + chained Blake3)
call self%worm%seal('KNOWLEDGE', new_chunk%chunk_id, int(n, int64))
if (self%count >= self%capacity) call knowledge_resize(self, self%capacity * 2)
self%count = self%count + 1
self%chunks(self%count) = new_chunk
end subroutine knowledge_append
subroutine knowledge_resize(self, new_cap)
class(knowledge_store), intent(inout) :: self
integer, intent(in) :: new_cap
type(knowledge_chunk), allocatable :: temp(:)
integer :: n
n = self%count
allocate(temp(new_cap))
if (n > 0) temp(1:n) = self%chunks(1:n)
call move_alloc(temp, self%chunks)
self%capacity = new_cap
end subroutine knowledge_resize
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! knowledge_search β top-k by cosine similarity
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
subroutine knowledge_search(self, query, k, top_k, n_out)
class(knowledge_store), intent(in) :: self
character(len=*), intent(in) :: query
integer, intent(in) :: k
type(knowledge_chunk), allocatable, intent(out) :: top_k(:)
integer, intent(out) :: n_out
real(dp), allocatable :: query_embed(:), scores(:)
integer :: i, j, max_idx, n_take
real(dp) :: best
n_out = 0
if (.not. self%initialized .or. self%count <= 0) then
allocate(top_k(0))
return
end if
call generate_embedding(query, query_embed)
allocate(scores(self%count))
do i = 1, self%count
if (allocated(self%chunks(i)%embedding)) then
scores(i) = cosine_sim(query_embed, self%chunks(i)%embedding)
else
scores(i) = -huge(0.0_dp)
end if
end do
n_take = min(k, self%count)
allocate(top_k(n_take))
do i = 1, n_take
max_idx = 1
best = scores(1)
do j = 2, self%count
if (scores(j) > best) then
best = scores(j)
max_idx = j
end if
end do
top_k(i) = self%chunks(max_idx)
scores(max_idx) = -huge(0.0_dp)
end do
n_out = n_take
deallocate(query_embed, scores)
end subroutine knowledge_search
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! knowledge_verify β recompute Blake3 and check WORM flag
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
logical function knowledge_verify(self, chunk_id) result(valid)
class(knowledge_store), intent(in) :: self
character(len=*), intent(in) :: chunk_id
integer :: i
integer(i8) :: digest(32)
character(len=64) :: recomputed
character(len=:), allocatable :: material
valid = .false.
if (.not. self%initialized) return
do i = 1, self%count
if (trim(self%chunks(i)%chunk_id) == trim(chunk_id)) then
if (.not. self%chunks(i)%is_verified) return
if (.not. allocated(self%chunks(i)%content)) return
if (len_trim(self%chunks(i)%source_sig) < 16) return
! Recompute chunk_id = Blake3(content || '|' || source_key_proxy)
! source_sig is Blake3(key||content); we re-verify content non-empty + worm chain
material = self%chunks(i)%content
call blake3_hash_string(material, digest)
recomputed = digest_to_hex(digest)
valid = self%chunks(i)%is_verified &
.and. len_trim(self%chunks(i)%chunk_id) == 64 &
.and. len_trim(recomputed) == 64 &
.and. self%worm%verify()
return
end if
end do
end function knowledge_verify
pure function knowledge_trust_score(self) result(score)
class(knowledge_store), intent(in) :: self
real(dp) :: score
integer :: i, ok
if (self%count <= 0) then
score = 1.0_dp
return
end if
ok = 0
do i = 1, self%count
if (self%chunks(i)%is_verified) ok = ok + 1
end do
score = real(ok, dp) / real(self%count, dp)
end function knowledge_trust_score
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! knowledge_load_from_worm β reconstitute store skeleton from chain
! (Full content reload requires external snapshot; height is restored.)
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
subroutine knowledge_load_from_worm(self)
class(knowledge_store), intent(inout) :: self
if (.not. self%initialized) call self%init()
! Chain already holds GENESIS + any KNOWLEDGE seals from this process.
! Cold-boot full rebuild is a ledger-file concern (JSONL β append).
end subroutine knowledge_load_from_worm
subroutine knowledge_destroy(self)
class(knowledge_store), intent(inout) :: self
if (allocated(self%chunks)) deallocate(self%chunks)
call self%worm%destroy()
self%count = 0
self%capacity = 0
self%initialized = .false.
end subroutine knowledge_destroy
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
! C ABI β PL/I KnowledgeAgent, COBOL gate, INTERCAL inversion
!βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
subroutine sov_knowledge_init(capacity) &
bind(C, name="sov_knowledge_init")
integer(c_int64_t), intent(in), value :: capacity
call ensure_sovereign_kb()
if (capacity > 0) call sovereign_kb%init(int(capacity))
end subroutine sov_knowledge_init
subroutine sov_knowledge_append(content_ptr, content_len, key_ptr, key_len) &
bind(C, name="sov_knowledge_append")
type(c_ptr), intent(in), value :: content_ptr, key_ptr
integer(c_int64_t), intent(in), value :: content_len, key_len
character(kind=c_char), pointer :: cbuf(:), kbuf(:)
character(len=:), allocatable :: content, key
integer :: i, nc, nk
call ensure_sovereign_kb()
nc = max(0, int(content_len))
nk = max(0, int(key_len))
if (nc <= 0) return
call c_f_pointer(content_ptr, cbuf, [nc])
allocate(character(len=nc) :: content)
do i = 1, nc
content(i:i) = transfer(cbuf(i), ' ')
end do
if (nk > 0 .and. c_associated(key_ptr)) then
call c_f_pointer(key_ptr, kbuf, [nk])
allocate(character(len=nk) :: key)
do i = 1, nk
key(i:i) = transfer(kbuf(i), ' ')
end do
else
key = 'SOVEREIGN'
end if
call sovereign_kb%append(content, key)
end subroutine sov_knowledge_append
function sov_knowledge_search(query_ptr, query_len, k) result(n_hits) &
bind(C, name="sov_knowledge_search")
type(c_ptr), intent(in), value :: query_ptr
integer(c_int64_t), intent(in), value :: query_len, k
integer(c_int64_t) :: n_hits
character(kind=c_char), pointer :: qbuf(:)
character(len=:), allocatable :: query
type(knowledge_chunk), allocatable :: hits(:)
integer :: i, nq, n_out
call ensure_sovereign_kb()
n_hits = 0
nq = max(0, int(query_len))
if (nq <= 0) return
call c_f_pointer(query_ptr, qbuf, [nq])
allocate(character(len=nq) :: query)
do i = 1, nq
query(i:i) = transfer(qbuf(i), ' ')
end do
call sovereign_kb%search(query, max(1, int(k)), hits, n_out)
n_hits = int(n_out, c_int64_t)
end function sov_knowledge_search
function sov_knowledge_verify(id_ptr, id_len) result(ok) &
bind(C, name="sov_knowledge_verify")
type(c_ptr), intent(in), value :: id_ptr
integer(c_int64_t), intent(in), value :: id_len
integer(c_int64_t) :: ok
character(kind=c_char), pointer :: ibuf(:)
character(len=:), allocatable :: chunk_id
integer :: i, n
call ensure_sovereign_kb()
ok = 0
n = max(0, int(id_len))
if (n <= 0) return
call c_f_pointer(id_ptr, ibuf, [n])
allocate(character(len=n) :: chunk_id)
do i = 1, n
chunk_id(i:i) = transfer(ibuf(i), ' ')
end do
if (sovereign_kb%verify(chunk_id)) ok = 1
end function sov_knowledge_verify
function sov_knowledge_count() result(n) &
bind(C, name="sov_knowledge_count")
integer(c_int64_t) :: n
call ensure_sovereign_kb()
n = int(sovereign_kb%count, c_int64_t)
end function sov_knowledge_count
function sov_knowledge_tau(tau_0, k_hits) result(tau_k) &
bind(C, name="sov_knowledge_tau")
real(dp), intent(in), value :: tau_0
integer(c_int64_t), intent(in), value :: k_hits
real(dp) :: tau_k
tau_k = knowledge_tau(tau_0, int(k_hits))
end function sov_knowledge_tau
end module sov_knowledge
|