bioma-micro 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bioma_micro-1.0.0/CHANGELOG.md +13 -0
- bioma_micro-1.0.0/Cargo.lock +196 -0
- bioma_micro-1.0.0/Cargo.toml +29 -0
- bioma_micro-1.0.0/LICENSE +114 -0
- bioma_micro-1.0.0/PKG-INFO +106 -0
- bioma_micro-1.0.0/PUBLISH.md +29 -0
- bioma_micro-1.0.0/README.md +85 -0
- bioma_micro-1.0.0/pyproject.toml +29 -0
- bioma_micro-1.0.0/src/context_apoptosis.rs +258 -0
- bioma_micro-1.0.0/src/hormonal_bus.rs +205 -0
- bioma_micro-1.0.0/src/lib.rs +40 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0 — 2026-07-16
|
|
4
|
+
|
|
5
|
+
First public release on PyPI.
|
|
6
|
+
|
|
7
|
+
- `dehydrate()` — one-shot, stateless history dehydration (recency-weighted half-life
|
|
8
|
+
decay, protected `SYSTEM`/`FACT` classes, audited token savings, GIL-released hot path).
|
|
9
|
+
- `ContextApoptosis` — stateful incremental engine (`insert` / `dehydrate` / `render`,
|
|
10
|
+
atomic audit counters).
|
|
11
|
+
- `saturation_scan()` — O(n) duplicate-shingle flood detector (cognitive-DDoS guard).
|
|
12
|
+
- `HormonalBus` / `HormonalSignal` — lock-free in-memory signal bus (crossbeam).
|
|
13
|
+
- abi3 wheel (`cp38-abi3`): one binary wheel covers CPython ≥ 3.8.
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
|
2
|
+
# It is not intended for manual editing.
|
|
3
|
+
version = 4
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "autocfg"
|
|
7
|
+
version = "1.5.1"
|
|
8
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
9
|
+
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
|
10
|
+
|
|
11
|
+
[[package]]
|
|
12
|
+
name = "bioma_micro"
|
|
13
|
+
version = "1.0.0"
|
|
14
|
+
dependencies = [
|
|
15
|
+
"crossbeam-channel",
|
|
16
|
+
"pyo3",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[[package]]
|
|
20
|
+
name = "cfg-if"
|
|
21
|
+
version = "1.0.4"
|
|
22
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
23
|
+
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
|
24
|
+
|
|
25
|
+
[[package]]
|
|
26
|
+
name = "crossbeam-channel"
|
|
27
|
+
version = "0.5.16"
|
|
28
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
29
|
+
checksum = "d85363c37faeca707aef026efa9f3b34d077bce547e48f770770625c6013679e"
|
|
30
|
+
dependencies = [
|
|
31
|
+
"crossbeam-utils",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[[package]]
|
|
35
|
+
name = "crossbeam-utils"
|
|
36
|
+
version = "0.8.22"
|
|
37
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
38
|
+
checksum = "61803da095bee82a81bb1a452ecc25d3b2f1416d1897eb86430c6159ef717c17"
|
|
39
|
+
|
|
40
|
+
[[package]]
|
|
41
|
+
name = "heck"
|
|
42
|
+
version = "0.5.0"
|
|
43
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
44
|
+
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
|
45
|
+
|
|
46
|
+
[[package]]
|
|
47
|
+
name = "indoc"
|
|
48
|
+
version = "2.0.7"
|
|
49
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
50
|
+
checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706"
|
|
51
|
+
dependencies = [
|
|
52
|
+
"rustversion",
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
[[package]]
|
|
56
|
+
name = "libc"
|
|
57
|
+
version = "0.2.186"
|
|
58
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
59
|
+
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
|
|
60
|
+
|
|
61
|
+
[[package]]
|
|
62
|
+
name = "memoffset"
|
|
63
|
+
version = "0.9.1"
|
|
64
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
65
|
+
checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
|
|
66
|
+
dependencies = [
|
|
67
|
+
"autocfg",
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
[[package]]
|
|
71
|
+
name = "once_cell"
|
|
72
|
+
version = "1.21.4"
|
|
73
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
74
|
+
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
|
75
|
+
|
|
76
|
+
[[package]]
|
|
77
|
+
name = "portable-atomic"
|
|
78
|
+
version = "1.13.1"
|
|
79
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
80
|
+
checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
|
|
81
|
+
|
|
82
|
+
[[package]]
|
|
83
|
+
name = "proc-macro2"
|
|
84
|
+
version = "1.0.106"
|
|
85
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
86
|
+
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
|
87
|
+
dependencies = [
|
|
88
|
+
"unicode-ident",
|
|
89
|
+
]
|
|
90
|
+
|
|
91
|
+
[[package]]
|
|
92
|
+
name = "pyo3"
|
|
93
|
+
version = "0.22.6"
|
|
94
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
95
|
+
checksum = "f402062616ab18202ae8319da13fa4279883a2b8a9d9f83f20dbade813ce1884"
|
|
96
|
+
dependencies = [
|
|
97
|
+
"cfg-if",
|
|
98
|
+
"indoc",
|
|
99
|
+
"libc",
|
|
100
|
+
"memoffset",
|
|
101
|
+
"once_cell",
|
|
102
|
+
"portable-atomic",
|
|
103
|
+
"pyo3-build-config",
|
|
104
|
+
"pyo3-ffi",
|
|
105
|
+
"pyo3-macros",
|
|
106
|
+
"unindent",
|
|
107
|
+
]
|
|
108
|
+
|
|
109
|
+
[[package]]
|
|
110
|
+
name = "pyo3-build-config"
|
|
111
|
+
version = "0.22.6"
|
|
112
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
113
|
+
checksum = "b14b5775b5ff446dd1056212d778012cbe8a0fbffd368029fd9e25b514479c38"
|
|
114
|
+
dependencies = [
|
|
115
|
+
"once_cell",
|
|
116
|
+
"target-lexicon",
|
|
117
|
+
]
|
|
118
|
+
|
|
119
|
+
[[package]]
|
|
120
|
+
name = "pyo3-ffi"
|
|
121
|
+
version = "0.22.6"
|
|
122
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
123
|
+
checksum = "9ab5bcf04a2cdcbb50c7d6105de943f543f9ed92af55818fd17b660390fc8636"
|
|
124
|
+
dependencies = [
|
|
125
|
+
"libc",
|
|
126
|
+
"pyo3-build-config",
|
|
127
|
+
]
|
|
128
|
+
|
|
129
|
+
[[package]]
|
|
130
|
+
name = "pyo3-macros"
|
|
131
|
+
version = "0.22.6"
|
|
132
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
133
|
+
checksum = "0fd24d897903a9e6d80b968368a34e1525aeb719d568dba8b3d4bfa5dc67d453"
|
|
134
|
+
dependencies = [
|
|
135
|
+
"proc-macro2",
|
|
136
|
+
"pyo3-macros-backend",
|
|
137
|
+
"quote",
|
|
138
|
+
"syn",
|
|
139
|
+
]
|
|
140
|
+
|
|
141
|
+
[[package]]
|
|
142
|
+
name = "pyo3-macros-backend"
|
|
143
|
+
version = "0.22.6"
|
|
144
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
145
|
+
checksum = "36c011a03ba1e50152b4b394b479826cad97e7a21eb52df179cd91ac411cbfbe"
|
|
146
|
+
dependencies = [
|
|
147
|
+
"heck",
|
|
148
|
+
"proc-macro2",
|
|
149
|
+
"pyo3-build-config",
|
|
150
|
+
"quote",
|
|
151
|
+
"syn",
|
|
152
|
+
]
|
|
153
|
+
|
|
154
|
+
[[package]]
|
|
155
|
+
name = "quote"
|
|
156
|
+
version = "1.0.46"
|
|
157
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
158
|
+
checksum = "dfbc457d0c7a0759a614551b11a6409e5951f6c7537be1f1b7682b9ae9230368"
|
|
159
|
+
dependencies = [
|
|
160
|
+
"proc-macro2",
|
|
161
|
+
]
|
|
162
|
+
|
|
163
|
+
[[package]]
|
|
164
|
+
name = "rustversion"
|
|
165
|
+
version = "1.0.23"
|
|
166
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
167
|
+
checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f"
|
|
168
|
+
|
|
169
|
+
[[package]]
|
|
170
|
+
name = "syn"
|
|
171
|
+
version = "2.0.118"
|
|
172
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
173
|
+
checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422"
|
|
174
|
+
dependencies = [
|
|
175
|
+
"proc-macro2",
|
|
176
|
+
"quote",
|
|
177
|
+
"unicode-ident",
|
|
178
|
+
]
|
|
179
|
+
|
|
180
|
+
[[package]]
|
|
181
|
+
name = "target-lexicon"
|
|
182
|
+
version = "0.12.16"
|
|
183
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
184
|
+
checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1"
|
|
185
|
+
|
|
186
|
+
[[package]]
|
|
187
|
+
name = "unicode-ident"
|
|
188
|
+
version = "1.0.24"
|
|
189
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
190
|
+
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
|
191
|
+
|
|
192
|
+
[[package]]
|
|
193
|
+
name = "unindent"
|
|
194
|
+
version = "0.2.4"
|
|
195
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
196
|
+
checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3"
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "bioma_micro"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
description = "B.I.O.M.A. Micro-Kernel — lock-free hormonal bus + context apoptosis (PyO3)."
|
|
6
|
+
license = "LicenseRef-FSL-1.1-MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
|
|
9
|
+
[lib]
|
|
10
|
+
name = "bioma_micro"
|
|
11
|
+
# cdylib → a native Python extension module loadable by CPython.
|
|
12
|
+
crate-type = ["cdylib"]
|
|
13
|
+
|
|
14
|
+
[dependencies]
|
|
15
|
+
pyo3 = { version = "0.22", features = ["extension-module"] }
|
|
16
|
+
crossbeam-channel = "0.5"
|
|
17
|
+
|
|
18
|
+
# Aggressive optimisation for the microsecond hot path — inline everything, fuse
|
|
19
|
+
# codegen units. No panic="abort": PyO3 converts Rust panics into Python
|
|
20
|
+
# exceptions at the FFI boundary, which requires unwinding.
|
|
21
|
+
[profile.release]
|
|
22
|
+
opt-level = 3
|
|
23
|
+
lto = true
|
|
24
|
+
codegen-units = 1
|
|
25
|
+
strip = true
|
|
26
|
+
|
|
27
|
+
# Standalone crate — its own workspace root, so nested-Cargo workspace
|
|
28
|
+
# resolution never clashes with any parent tree.
|
|
29
|
+
[workspace]
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, MIT Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-MIT
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 Jonathas Cordeiro
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, INCLUDING
|
|
74
|
+
BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the MIT License that is effective on the second anniversary of the date we make
|
|
91
|
+
the Software available. On or after that date, you may use the Software under
|
|
92
|
+
the MIT License, in which case the following will apply:
|
|
93
|
+
|
|
94
|
+
MIT License
|
|
95
|
+
|
|
96
|
+
Copyright 2026 Jonathas Cordeiro
|
|
97
|
+
|
|
98
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
99
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
100
|
+
the Software without restriction, including without limitation the rights to
|
|
101
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
102
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
103
|
+
so, subject to the following conditions:
|
|
104
|
+
|
|
105
|
+
The above copyright notice and this permission notice shall be included in all
|
|
106
|
+
copies or substantial portions of the Software.
|
|
107
|
+
|
|
108
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
109
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
110
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
111
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
112
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
113
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
114
|
+
SOFTWARE.
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bioma_micro
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
5
|
+
Classifier: Intended Audience :: Developers
|
|
6
|
+
Classifier: Programming Language :: Rust
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Summary: B.I.O.M.A. Micro-Kernel — lock-free hormonal bus + autonomous context apoptosis.
|
|
13
|
+
Keywords: llm,context-window,prompt-compression,token-optimization,agents,rust,pyo3
|
|
14
|
+
Author-email: Jonathas Cordeiro <jonathas.cordeiro2023@gmail.com>
|
|
15
|
+
License: LicenseRef-FSL-1.1-MIT
|
|
16
|
+
Requires-Python: >=3.8
|
|
17
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
18
|
+
Project-URL: Changelog, https://github.com/jonathascordeiro20/bioma-framework/blob/main/CHANGELOG.md
|
|
19
|
+
Project-URL: Repository, https://github.com/jonathascordeiro20/bioma-framework
|
|
20
|
+
|
|
21
|
+
# bioma-micro
|
|
22
|
+
|
|
23
|
+
**B.I.O.M.A. Micro-Kernel** — a lean efficiency & resilience core for LLM infrastructure,
|
|
24
|
+
written in Rust, exposed to Python via PyO3.
|
|
25
|
+
|
|
26
|
+
Exactly two proven primitives, nothing else:
|
|
27
|
+
|
|
28
|
+
- **Context apoptosis** — autonomous history dehydration. Long agent sessions accumulate
|
|
29
|
+
low-value ballast (verbose tool logs, stale turns). The kernel assigns each message a
|
|
30
|
+
metabolic weight by class, applies half-life decay, and purges dead weight *before* the
|
|
31
|
+
payload hits the API. Microsecond-scale, GIL-released, allocation-light.
|
|
32
|
+
- **Hormonal bus** — lock-free in-memory signal injection (~2M signals/s, ~5 µs).
|
|
33
|
+
|
|
34
|
+
Plus `saturation_scan()`, an O(n) repetition detector for cognitive-DDoS / flood inputs.
|
|
35
|
+
|
|
36
|
+
## Install
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install bioma-micro
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Prebuilt wheel for Windows (Python ≥ 3.8, abi3). Other platforms build from the sdist
|
|
43
|
+
(requires a Rust toolchain: `pip` will invoke `maturin` automatically).
|
|
44
|
+
|
|
45
|
+
## Quick start
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
import bioma_micro as bm
|
|
49
|
+
|
|
50
|
+
messages = [
|
|
51
|
+
("You are a precise operations copilot.", bm.SYSTEM), # never purged
|
|
52
|
+
("FACT: the deploy freeze ends on 2026-07-18.", bm.FACT), # never purged
|
|
53
|
+
("[tool log] 40 KB of verbose audit output ...", bm.TOOL), # prime target
|
|
54
|
+
("Round 1: any anomaly?", bm.USER),
|
|
55
|
+
("Nothing above baseline.", bm.ASSISTANT),
|
|
56
|
+
# ... hundreds of turns later ...
|
|
57
|
+
("What is the freeze end date?", bm.USER),
|
|
58
|
+
]
|
|
59
|
+
|
|
60
|
+
result = bm.dehydrate(messages, half_life=6.0, safe_threshold=0.35)
|
|
61
|
+
print(result["reduction"]) # fraction of input tokens purged (0..1)
|
|
62
|
+
print(result["kernel_latency_us"]) # pure-Rust decision pass, microseconds
|
|
63
|
+
prompt = "\n".join(result["kept"]) # dispatch this instead of the full history
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Flood detection:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
bm.saturation_scan(suspicious_text) # ~1.0 = repetitive flood, ~0.0 = natural text
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## The honest contract
|
|
73
|
+
|
|
74
|
+
Measured, auditable behavior (full methodology and raw data in the
|
|
75
|
+
[framework repo](https://github.com/jonathascordeiro20/bioma-framework)):
|
|
76
|
+
|
|
77
|
+
| Client behavior | Measured effect |
|
|
78
|
+
| :--- | :--- |
|
|
79
|
+
| Naive tool-calling agent (resends growing history) | **−84%** input tokens |
|
|
80
|
+
| Generic long session (16 rounds, resends everything) | **−95.8%** input tokens |
|
|
81
|
+
| Claude Code (already self-manages context) | **~0%** — safe no-op |
|
|
82
|
+
|
|
83
|
+
- Values tagged `FACT` and recent turns survive; answer quality stays at parity with the
|
|
84
|
+
full-context baseline (verified with objective probes against real online models).
|
|
85
|
+
- Durable info buried in old untagged turns **is purged by design** — tag it `FACT`.
|
|
86
|
+
- Against an already-lean agent the kernel is a no-op: it never helps, never hurts.
|
|
87
|
+
|
|
88
|
+
This is a safety net for agents that do **not** manage their own context — not a
|
|
89
|
+
universal "−X%" claim.
|
|
90
|
+
|
|
91
|
+
## API surface
|
|
92
|
+
|
|
93
|
+
| Symbol | Kind | Purpose |
|
|
94
|
+
| :--- | :--- | :--- |
|
|
95
|
+
| `dehydrate(messages, half_life=6.0, safe_threshold=0.35)` | function | one-shot history dehydration; returns kept blocks + audit dict |
|
|
96
|
+
| `saturation_scan(text, window=8)` | function | duplicate-shingle flood score (0..1) |
|
|
97
|
+
| `ContextApoptosis(half_life, safe_threshold, capacity)` | class | stateful incremental engine (`insert` / `dehydrate` / `render`) |
|
|
98
|
+
| `HormonalBus`, `HormonalSignal` | class | lock-free signal bus |
|
|
99
|
+
| `SYSTEM`, `USER`, `ASSISTANT`, `FACT`, `TOOL` | flags | metabolic signal classes |
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
[FSL-1.1-MIT](https://fsl.software/) — Functional Source License 1.1 with MIT future
|
|
104
|
+
license. Free for internal use, non-commercial use, and professional services; converts
|
|
105
|
+
to MIT two years after each release.
|
|
106
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Publicando o bioma-micro no PyPI
|
|
2
|
+
|
|
3
|
+
Artefatos da release 1.0.0 (já compilados e validados com `twine check` + smoke test):
|
|
4
|
+
|
|
5
|
+
- `target/wheels/bioma_micro-1.0.0-cp38-abi3-win_amd64.whl` — wheel abi3, cobre CPython ≥ 3.8 no Windows
|
|
6
|
+
- `target/wheels/bioma_micro-1.0.0.tar.gz` — sdist (Linux/macOS compilam via Rust automaticamente)
|
|
7
|
+
|
|
8
|
+
## Passo único que falta: o token
|
|
9
|
+
|
|
10
|
+
1. Crie um API token em https://pypi.org/manage/account/token/ (escopo "Entire account"
|
|
11
|
+
para a primeira publicação; depois restrinja ao projeto `bioma-micro`).
|
|
12
|
+
2. Rode:
|
|
13
|
+
|
|
14
|
+
```powershell
|
|
15
|
+
cd c:\Users\jonat\A.N.I.M.A\workspace\bioma_micro
|
|
16
|
+
$env:MATURIN_PYPI_TOKEN = "pypi-AgEIcHlwaS5vcmc..." # seu token
|
|
17
|
+
maturin upload target/wheels/bioma_micro-1.0.0-cp38-abi3-win_amd64.whl target/wheels/bioma_micro-1.0.0.tar.gz
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
3. Verifique: `pip install bioma-micro` num venv limpo.
|
|
21
|
+
|
|
22
|
+
> Não suba o wheel antigo `cp312-cp312` — o abi3 o substitui e cobre 3.8→3.13.
|
|
23
|
+
|
|
24
|
+
## Wheels Linux/macOS (adoção total)
|
|
25
|
+
|
|
26
|
+
O sdist já permite `pip install bioma-micro` em qualquer plataforma com Rust. Para
|
|
27
|
+
wheels prontos multi-plataforma, o caminho padrão é GitHub Actions com
|
|
28
|
+
`PyO3/maturin-action` (matriz linux/musllinux/macos/windows) publicando via
|
|
29
|
+
`maturin upload` — adicione quando o repo público existir.
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# bioma-micro
|
|
2
|
+
|
|
3
|
+
**B.I.O.M.A. Micro-Kernel** — a lean efficiency & resilience core for LLM infrastructure,
|
|
4
|
+
written in Rust, exposed to Python via PyO3.
|
|
5
|
+
|
|
6
|
+
Exactly two proven primitives, nothing else:
|
|
7
|
+
|
|
8
|
+
- **Context apoptosis** — autonomous history dehydration. Long agent sessions accumulate
|
|
9
|
+
low-value ballast (verbose tool logs, stale turns). The kernel assigns each message a
|
|
10
|
+
metabolic weight by class, applies half-life decay, and purges dead weight *before* the
|
|
11
|
+
payload hits the API. Microsecond-scale, GIL-released, allocation-light.
|
|
12
|
+
- **Hormonal bus** — lock-free in-memory signal injection (~2M signals/s, ~5 µs).
|
|
13
|
+
|
|
14
|
+
Plus `saturation_scan()`, an O(n) repetition detector for cognitive-DDoS / flood inputs.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pip install bioma-micro
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Prebuilt wheel for Windows (Python ≥ 3.8, abi3). Other platforms build from the sdist
|
|
23
|
+
(requires a Rust toolchain: `pip` will invoke `maturin` automatically).
|
|
24
|
+
|
|
25
|
+
## Quick start
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
import bioma_micro as bm
|
|
29
|
+
|
|
30
|
+
messages = [
|
|
31
|
+
("You are a precise operations copilot.", bm.SYSTEM), # never purged
|
|
32
|
+
("FACT: the deploy freeze ends on 2026-07-18.", bm.FACT), # never purged
|
|
33
|
+
("[tool log] 40 KB of verbose audit output ...", bm.TOOL), # prime target
|
|
34
|
+
("Round 1: any anomaly?", bm.USER),
|
|
35
|
+
("Nothing above baseline.", bm.ASSISTANT),
|
|
36
|
+
# ... hundreds of turns later ...
|
|
37
|
+
("What is the freeze end date?", bm.USER),
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
result = bm.dehydrate(messages, half_life=6.0, safe_threshold=0.35)
|
|
41
|
+
print(result["reduction"]) # fraction of input tokens purged (0..1)
|
|
42
|
+
print(result["kernel_latency_us"]) # pure-Rust decision pass, microseconds
|
|
43
|
+
prompt = "\n".join(result["kept"]) # dispatch this instead of the full history
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Flood detection:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
bm.saturation_scan(suspicious_text) # ~1.0 = repetitive flood, ~0.0 = natural text
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## The honest contract
|
|
53
|
+
|
|
54
|
+
Measured, auditable behavior (full methodology and raw data in the
|
|
55
|
+
[framework repo](https://github.com/jonathascordeiro20/bioma-framework)):
|
|
56
|
+
|
|
57
|
+
| Client behavior | Measured effect |
|
|
58
|
+
| :--- | :--- |
|
|
59
|
+
| Naive tool-calling agent (resends growing history) | **−84%** input tokens |
|
|
60
|
+
| Generic long session (16 rounds, resends everything) | **−95.8%** input tokens |
|
|
61
|
+
| Claude Code (already self-manages context) | **~0%** — safe no-op |
|
|
62
|
+
|
|
63
|
+
- Values tagged `FACT` and recent turns survive; answer quality stays at parity with the
|
|
64
|
+
full-context baseline (verified with objective probes against real online models).
|
|
65
|
+
- Durable info buried in old untagged turns **is purged by design** — tag it `FACT`.
|
|
66
|
+
- Against an already-lean agent the kernel is a no-op: it never helps, never hurts.
|
|
67
|
+
|
|
68
|
+
This is a safety net for agents that do **not** manage their own context — not a
|
|
69
|
+
universal "−X%" claim.
|
|
70
|
+
|
|
71
|
+
## API surface
|
|
72
|
+
|
|
73
|
+
| Symbol | Kind | Purpose |
|
|
74
|
+
| :--- | :--- | :--- |
|
|
75
|
+
| `dehydrate(messages, half_life=6.0, safe_threshold=0.35)` | function | one-shot history dehydration; returns kept blocks + audit dict |
|
|
76
|
+
| `saturation_scan(text, window=8)` | function | duplicate-shingle flood score (0..1) |
|
|
77
|
+
| `ContextApoptosis(half_life, safe_threshold, capacity)` | class | stateful incremental engine (`insert` / `dehydrate` / `render`) |
|
|
78
|
+
| `HormonalBus`, `HormonalSignal` | class | lock-free signal bus |
|
|
79
|
+
| `SYSTEM`, `USER`, `ASSISTANT`, `FACT`, `TOOL` | flags | metabolic signal classes |
|
|
80
|
+
|
|
81
|
+
## License
|
|
82
|
+
|
|
83
|
+
[FSL-1.1-MIT](https://fsl.software/) — Functional Source License 1.1 with MIT future
|
|
84
|
+
license. Free for internal use, non-commercial use, and professional services; converts
|
|
85
|
+
to MIT two years after each release.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["maturin>=1.5,<2.0"]
|
|
3
|
+
build-backend = "maturin"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "bioma_micro"
|
|
7
|
+
description = "B.I.O.M.A. Micro-Kernel — lock-free hormonal bus + autonomous context apoptosis."
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.8"
|
|
10
|
+
license = { text = "LicenseRef-FSL-1.1-MIT" }
|
|
11
|
+
authors = [{ name = "Jonathas Cordeiro", email = "jonathas.cordeiro2023@gmail.com" }]
|
|
12
|
+
keywords = ["llm", "context-window", "prompt-compression", "token-optimization", "agents", "rust", "pyo3"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 5 - Production/Stable",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Programming Language :: Rust",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
21
|
+
]
|
|
22
|
+
dynamic = ["version"]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Repository = "https://github.com/jonathascordeiro20/bioma-framework"
|
|
26
|
+
Changelog = "https://github.com/jonathascordeiro20/bioma-framework/blob/main/CHANGELOG.md"
|
|
27
|
+
|
|
28
|
+
[tool.maturin]
|
|
29
|
+
features = ["pyo3/extension-module", "pyo3/abi3-py38"]
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
//! `context_apoptosis.rs` — autonomous history dehydration.
|
|
2
|
+
//!
|
|
3
|
+
//! Long agent sessions (audit logs, massive chats) accumulate low-value ballast:
|
|
4
|
+
//! verbose tool output, stale turns, resolved chatter. This engine assigns each
|
|
5
|
+
//! message a **metabolic weight** by class, applies an aggressive **half-life
|
|
6
|
+
//! decay** (older + lower-value → drains faster), and **purges** any block whose
|
|
7
|
+
//! oxygen falls below the safe threshold — before the payload is dispatched to the
|
|
8
|
+
//! API. Durable classes (SYSTEM, FACT) are reinforced and never purged.
|
|
9
|
+
//!
|
|
10
|
+
//! Result: the input context window is dehydrated (universal input-token savings)
|
|
11
|
+
//! with a pure-Rust, thread-safe, allocation-light pass.
|
|
12
|
+
|
|
13
|
+
use std::collections::hash_map::DefaultHasher;
|
|
14
|
+
use std::collections::HashSet;
|
|
15
|
+
use std::hash::{Hash, Hasher};
|
|
16
|
+
use std::sync::atomic::{AtomicU64, Ordering};
|
|
17
|
+
use std::sync::Mutex;
|
|
18
|
+
use std::time::Instant;
|
|
19
|
+
|
|
20
|
+
use pyo3::prelude::*;
|
|
21
|
+
use pyo3::types::PyDict;
|
|
22
|
+
|
|
23
|
+
// Metabolic signal classes (bitwise flags shared with the Python layer).
|
|
24
|
+
pub const SYSTEM: u32 = 1 << 0; // durable instructions — never purge
|
|
25
|
+
pub const USER: u32 = 1 << 1; // user turns
|
|
26
|
+
pub const ASSISTANT: u32 = 1 << 2; // model turns
|
|
27
|
+
pub const FACT: u32 = 1 << 3; // retrieved facts / decisions to keep
|
|
28
|
+
pub const TOOL: u32 = 1 << 4; // verbose tool logs / scratchpad — prime target
|
|
29
|
+
const PROTECTED: u32 = SYSTEM | FACT;
|
|
30
|
+
|
|
31
|
+
/// ~4 chars/token — identical to the Python side so 'full' and 'pruned' counts agree.
|
|
32
|
+
#[inline]
|
|
33
|
+
pub fn est_tokens(s: &str) -> u32 {
|
|
34
|
+
((s.len() / 4) + 1) as u32
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// Initial metabolic weight (oxygen) by signal class. Durable data start rich;
|
|
38
|
+
/// verbose tool output starts oxygen-poor and dehydrates first.
|
|
39
|
+
#[inline]
|
|
40
|
+
fn metabolic_weight(signal: u32) -> f32 {
|
|
41
|
+
if signal & PROTECTED != 0 {
|
|
42
|
+
4.0
|
|
43
|
+
} else if signal & (USER | ASSISTANT) != 0 {
|
|
44
|
+
1.0
|
|
45
|
+
} else if signal & TOOL != 0 {
|
|
46
|
+
0.25
|
|
47
|
+
} else {
|
|
48
|
+
0.6
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
struct Cell {
|
|
53
|
+
content: String,
|
|
54
|
+
oxygen: f32,
|
|
55
|
+
signal: u32,
|
|
56
|
+
tokens: u32,
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// --------------------------------------------------------------------------- //
|
|
60
|
+
// ContextApoptosis — stateful, incremental engine
|
|
61
|
+
// --------------------------------------------------------------------------- //
|
|
62
|
+
/// A self-dehydrating context window. Insert blocks, then run `dehydrate` cycles;
|
|
63
|
+
/// each cycle halves oxygen over `half_life` and purges anything below the
|
|
64
|
+
/// `safe_threshold` (durable classes are reinforced and survive).
|
|
65
|
+
#[pyclass]
|
|
66
|
+
pub struct ContextApoptosis {
|
|
67
|
+
half_life: f32,
|
|
68
|
+
safe_threshold: f32,
|
|
69
|
+
items: Mutex<Vec<Cell>>,
|
|
70
|
+
inserted_tokens: AtomicU64,
|
|
71
|
+
apoptosed_tokens: AtomicU64,
|
|
72
|
+
apoptosed_count: AtomicU64,
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
#[pymethods]
|
|
76
|
+
impl ContextApoptosis {
|
|
77
|
+
#[new]
|
|
78
|
+
#[pyo3(signature = (half_life = 2.0, safe_threshold = 0.35, capacity = 256))]
|
|
79
|
+
fn new(half_life: f32, safe_threshold: f32, capacity: usize) -> Self {
|
|
80
|
+
Self {
|
|
81
|
+
half_life: half_life.max(0.1),
|
|
82
|
+
safe_threshold,
|
|
83
|
+
items: Mutex::new(Vec::with_capacity(capacity)),
|
|
84
|
+
inserted_tokens: AtomicU64::new(0),
|
|
85
|
+
apoptosed_tokens: AtomicU64::new(0),
|
|
86
|
+
apoptosed_count: AtomicU64::new(0),
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// Insert a context block. `oxygen < 0` (default) → auto metabolic weight.
|
|
91
|
+
#[pyo3(signature = (content, signal = 0, oxygen = -1.0))]
|
|
92
|
+
fn insert(&self, content: String, signal: u32, oxygen: f32) {
|
|
93
|
+
let tokens = est_tokens(&content);
|
|
94
|
+
self.inserted_tokens.fetch_add(tokens as u64, Ordering::Relaxed);
|
|
95
|
+
let oxy = if oxygen < 0.0 { metabolic_weight(signal) } else { oxygen };
|
|
96
|
+
self.items.lock().unwrap().push(Cell { content, oxygen: oxy, signal, tokens });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/// One half-life decay cycle → returns the number of purged blocks. Durable
|
|
100
|
+
/// classes are reinforced by `reinforce_boost`; everything else decays by
|
|
101
|
+
/// `2^(-1/half_life)` and is purged in place if it drops below the threshold.
|
|
102
|
+
#[pyo3(signature = (reinforce_boost = 0.5))]
|
|
103
|
+
fn dehydrate(&self, py: Python<'_>, reinforce_boost: f32) -> usize {
|
|
104
|
+
let factor = 2f32.powf(-1.0 / self.half_life);
|
|
105
|
+
let eps = self.safe_threshold;
|
|
106
|
+
py.allow_threads(|| {
|
|
107
|
+
let mut items = self.items.lock().unwrap();
|
|
108
|
+
for c in items.iter_mut() {
|
|
109
|
+
if c.signal & PROTECTED != 0 {
|
|
110
|
+
c.oxygen += reinforce_boost;
|
|
111
|
+
} else {
|
|
112
|
+
c.oxygen *= factor;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
let before = items.len();
|
|
116
|
+
let mut freed: u64 = 0;
|
|
117
|
+
items.retain(|c| {
|
|
118
|
+
let alive = (c.signal & PROTECTED != 0) || c.oxygen >= eps;
|
|
119
|
+
if !alive {
|
|
120
|
+
freed += c.tokens as u64;
|
|
121
|
+
}
|
|
122
|
+
alive
|
|
123
|
+
});
|
|
124
|
+
let purged = before - items.len();
|
|
125
|
+
self.apoptosed_count.fetch_add(purged as u64, Ordering::Relaxed);
|
|
126
|
+
self.apoptosed_tokens.fetch_add(freed, Ordering::Relaxed);
|
|
127
|
+
purged
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
fn active_context(&self) -> Vec<String> {
|
|
132
|
+
self.items.lock().unwrap().iter().map(|c| c.content.clone()).collect()
|
|
133
|
+
}
|
|
134
|
+
/// The dehydrated context, newline-joined, ready to dispatch.
|
|
135
|
+
fn render(&self) -> String {
|
|
136
|
+
self.items.lock().unwrap().iter().map(|c| c.content.as_str()).collect::<Vec<_>>().join("\n")
|
|
137
|
+
}
|
|
138
|
+
fn active_tokens(&self) -> u64 {
|
|
139
|
+
self.items.lock().unwrap().iter().map(|c| c.tokens as u64).sum()
|
|
140
|
+
}
|
|
141
|
+
fn inserted_tokens(&self) -> u64 {
|
|
142
|
+
self.inserted_tokens.load(Ordering::Relaxed)
|
|
143
|
+
}
|
|
144
|
+
fn __len__(&self) -> usize {
|
|
145
|
+
self.items.lock().unwrap().len()
|
|
146
|
+
}
|
|
147
|
+
/// Fraction of inserted tokens reclaimed by apoptosis (0..1).
|
|
148
|
+
fn reduction_ratio(&self) -> f32 {
|
|
149
|
+
let ins = self.inserted_tokens();
|
|
150
|
+
if ins == 0 { 0.0 } else { self.apoptosed_tokens.load(Ordering::Relaxed) as f32 / ins as f32 }
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// --------------------------------------------------------------------------- //
|
|
155
|
+
// dehydrate() — the one-shot stateless filter used by the OpenRouter client
|
|
156
|
+
// --------------------------------------------------------------------------- //
|
|
157
|
+
/// Dehydrate a whole message history in a single pass (oldest → newest).
|
|
158
|
+
///
|
|
159
|
+
/// `messages` is a list of `(content, signal_flags)`. Older, lower-value blocks
|
|
160
|
+
/// are decayed by recency-weighted half-life and purged below `safe_threshold`;
|
|
161
|
+
/// durable classes (SYSTEM, FACT) always survive. Returns the surviving blocks
|
|
162
|
+
/// plus audited savings and the pure-kernel latency in microseconds.
|
|
163
|
+
#[pyfunction]
|
|
164
|
+
#[pyo3(signature = (messages, half_life = 6.0, safe_threshold = 0.35))]
|
|
165
|
+
pub fn dehydrate<'py>(
|
|
166
|
+
py: Python<'py>,
|
|
167
|
+
messages: Vec<(String, u32)>,
|
|
168
|
+
half_life: f32,
|
|
169
|
+
safe_threshold: f32,
|
|
170
|
+
) -> PyResult<Bound<'py, PyDict>> {
|
|
171
|
+
let hl = half_life.max(0.1);
|
|
172
|
+
let n = messages.len();
|
|
173
|
+
|
|
174
|
+
// --- the measured kernel hot-path: decide survivors (pure compute, GIL released) ---
|
|
175
|
+
let t0 = Instant::now();
|
|
176
|
+
let decisions: Vec<bool> = py.allow_threads(|| {
|
|
177
|
+
let mut keep = Vec::with_capacity(n);
|
|
178
|
+
for (i, (content, signal)) in messages.iter().enumerate() {
|
|
179
|
+
let protected = signal & PROTECTED != 0;
|
|
180
|
+
if protected {
|
|
181
|
+
keep.push(true);
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
let age = (n - 1 - i) as f32; // 0 = newest
|
|
185
|
+
let oxygen = metabolic_weight(*signal) * 2f32.powf(-age / hl);
|
|
186
|
+
let _ = content; // content length already priced into tokens below
|
|
187
|
+
keep.push(oxygen >= safe_threshold);
|
|
188
|
+
}
|
|
189
|
+
keep
|
|
190
|
+
});
|
|
191
|
+
let latency_us = t0.elapsed().as_nanos() as f64 / 1000.0;
|
|
192
|
+
|
|
193
|
+
// --- marshal survivors + audit token savings (not counted in kernel latency) ---
|
|
194
|
+
let kept = pyo3::types::PyList::empty_bound(py);
|
|
195
|
+
let mut tokens_before: u64 = 0;
|
|
196
|
+
let mut tokens_after: u64 = 0;
|
|
197
|
+
let mut purged: usize = 0;
|
|
198
|
+
for ((content, _sig), &alive) in messages.iter().zip(decisions.iter()) {
|
|
199
|
+
let tok = est_tokens(content) as u64;
|
|
200
|
+
tokens_before += tok;
|
|
201
|
+
if alive {
|
|
202
|
+
tokens_after += tok;
|
|
203
|
+
kept.append(content)?;
|
|
204
|
+
} else {
|
|
205
|
+
purged += 1;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
let reduction = if tokens_before == 0 {
|
|
209
|
+
0.0
|
|
210
|
+
} else {
|
|
211
|
+
1.0 - (tokens_after as f64 / tokens_before as f64)
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
let d = PyDict::new_bound(py);
|
|
215
|
+
d.set_item("kept", kept)?;
|
|
216
|
+
d.set_item("blocks_in", n)?;
|
|
217
|
+
d.set_item("blocks_kept", n - purged)?;
|
|
218
|
+
d.set_item("blocks_purged", purged)?;
|
|
219
|
+
d.set_item("tokens_before", tokens_before)?;
|
|
220
|
+
d.set_item("tokens_after", tokens_after)?;
|
|
221
|
+
d.set_item("reduction", reduction)?;
|
|
222
|
+
d.set_item("kernel_latency_us", latency_us)?;
|
|
223
|
+
Ok(d)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// --------------------------------------------------------------------------- //
|
|
227
|
+
// saturation_scan() — cognitive-DDoS / flood detector
|
|
228
|
+
// --------------------------------------------------------------------------- //
|
|
229
|
+
/// Detect input saturation: the fraction of the payload that is **repetition**.
|
|
230
|
+
///
|
|
231
|
+
/// Cognitive-DDoS, forged-log floods and jailbreak spam are highly repetitive —
|
|
232
|
+
/// they reuse the same phrases to exhaust the context window. This scores the
|
|
233
|
+
/// fraction of duplicate `w`-token shingles (default 8): ~1.0 = a repetitive
|
|
234
|
+
/// flood (RED ALERT), ~0.0 = natural, high-entropy text. Pure O(n), no
|
|
235
|
+
/// allocation (shingles are hashed, not materialised), microsecond-scale.
|
|
236
|
+
#[pyfunction]
|
|
237
|
+
#[pyo3(signature = (text, window = 8))]
|
|
238
|
+
pub fn saturation_scan(text: &str, window: usize) -> f32 {
|
|
239
|
+
let w = window.max(1);
|
|
240
|
+
let tokens: Vec<&str> = text.split_whitespace().collect();
|
|
241
|
+
let n = tokens.len();
|
|
242
|
+
if n < w * 2 {
|
|
243
|
+
return 0.0;
|
|
244
|
+
}
|
|
245
|
+
let windows = n - w + 1;
|
|
246
|
+
let mut seen: HashSet<u64> = HashSet::with_capacity(windows);
|
|
247
|
+
let mut dupes = 0usize;
|
|
248
|
+
for i in 0..windows {
|
|
249
|
+
let mut h = DefaultHasher::new();
|
|
250
|
+
for t in &tokens[i..i + w] {
|
|
251
|
+
t.hash(&mut h);
|
|
252
|
+
}
|
|
253
|
+
if !seen.insert(h.finish()) {
|
|
254
|
+
dupes += 1;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
dupes as f32 / windows as f32
|
|
258
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
//! `hormonal_bus.rs` — the lock-free in-memory signalling substrate.
|
|
2
|
+
//!
|
|
3
|
+
//! A fixed bank of atomic `f32` concentration cells (one per signal channel) is
|
|
4
|
+
//! updated with lock-free CAS loops, so `inject`/`sense` never take a lock and the
|
|
5
|
+
//! GIL is released on the hot path. Signal *events* are additionally fanned out
|
|
6
|
+
//! through a bounded `crossbeam-channel` for consumers that want the stream.
|
|
7
|
+
//!
|
|
8
|
+
//! Design target (measured on the reference host): ~2M signals/s at ~5μs mean
|
|
9
|
+
//! latency, bounded under 10× concurrent load. See `tests/`.
|
|
10
|
+
|
|
11
|
+
use std::collections::HashMap;
|
|
12
|
+
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
|
|
13
|
+
use std::sync::Arc;
|
|
14
|
+
|
|
15
|
+
use crossbeam_channel::{bounded, Receiver, Sender};
|
|
16
|
+
use pyo3::prelude::*;
|
|
17
|
+
|
|
18
|
+
// --------------------------------------------------------------------------- //
|
|
19
|
+
// Lock-free atomic f32 cells (concentration stored as its IEEE-754 bit pattern)
|
|
20
|
+
// --------------------------------------------------------------------------- //
|
|
21
|
+
#[inline(always)]
|
|
22
|
+
fn add_f32(cell: &AtomicU32, delta: f32) {
|
|
23
|
+
let mut cur = cell.load(Ordering::Relaxed);
|
|
24
|
+
loop {
|
|
25
|
+
let new = f32::from_bits(cur) + delta;
|
|
26
|
+
match cell.compare_exchange_weak(cur, new.to_bits(), Ordering::AcqRel, Ordering::Relaxed) {
|
|
27
|
+
Ok(_) => break,
|
|
28
|
+
Err(actual) => cur = actual,
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
#[inline(always)]
|
|
33
|
+
fn mul_f32(cell: &AtomicU32, factor: f32) {
|
|
34
|
+
let mut cur = cell.load(Ordering::Relaxed);
|
|
35
|
+
loop {
|
|
36
|
+
let new = f32::from_bits(cur) * factor;
|
|
37
|
+
match cell.compare_exchange_weak(cur, new.to_bits(), Ordering::AcqRel, Ordering::Relaxed) {
|
|
38
|
+
Ok(_) => break,
|
|
39
|
+
Err(actual) => cur = actual,
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
#[inline(always)]
|
|
44
|
+
fn load_f32(cell: &AtomicU32) -> f32 {
|
|
45
|
+
f32::from_bits(cell.load(Ordering::Relaxed))
|
|
46
|
+
}
|
|
47
|
+
#[inline(always)]
|
|
48
|
+
fn valid_mask(n: usize) -> u32 {
|
|
49
|
+
if n >= 32 { u32::MAX } else { (1u32 << n) - 1 }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// --------------------------------------------------------------------------- //
|
|
53
|
+
// HormonalSignal — the lightweight payload on the wire
|
|
54
|
+
// --------------------------------------------------------------------------- //
|
|
55
|
+
/// A single hormonal signal: bitwise channel `flags` (`u32`) + `intensity` (`f32`).
|
|
56
|
+
#[pyclass]
|
|
57
|
+
#[derive(Clone, Copy)]
|
|
58
|
+
pub struct HormonalSignal {
|
|
59
|
+
#[pyo3(get, set)]
|
|
60
|
+
pub flags: u32,
|
|
61
|
+
#[pyo3(get, set)]
|
|
62
|
+
pub intensity: f32,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#[pymethods]
|
|
66
|
+
impl HormonalSignal {
|
|
67
|
+
#[new]
|
|
68
|
+
#[pyo3(signature = (flags, intensity = 1.0))]
|
|
69
|
+
fn new(flags: u32, intensity: f32) -> Self {
|
|
70
|
+
Self { flags, intensity }
|
|
71
|
+
}
|
|
72
|
+
fn __repr__(&self) -> String {
|
|
73
|
+
format!("HormonalSignal(flags=0x{:X}, intensity={:.3})", self.flags, self.intensity)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// --------------------------------------------------------------------------- //
|
|
78
|
+
// BusCore — the shareable, lock-free core
|
|
79
|
+
// --------------------------------------------------------------------------- //
|
|
80
|
+
struct BusCore {
|
|
81
|
+
conc: Box<[AtomicU32]>,
|
|
82
|
+
n: usize,
|
|
83
|
+
secretions: AtomicU64,
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
impl BusCore {
|
|
87
|
+
fn new(n: usize) -> Self {
|
|
88
|
+
let conc = (0..n).map(|_| AtomicU32::new(0)).collect::<Vec<_>>().into_boxed_slice();
|
|
89
|
+
Self { conc, n, secretions: AtomicU64::new(0) }
|
|
90
|
+
}
|
|
91
|
+
#[inline(always)]
|
|
92
|
+
fn secrete(&self, flags: u32, intensity: f32) {
|
|
93
|
+
let mut f = flags & valid_mask(self.n);
|
|
94
|
+
while f != 0 {
|
|
95
|
+
let bit = f.trailing_zeros() as usize;
|
|
96
|
+
add_f32(&self.conc[bit], intensity);
|
|
97
|
+
f &= f - 1;
|
|
98
|
+
}
|
|
99
|
+
self.secretions.fetch_add(1, Ordering::Relaxed);
|
|
100
|
+
}
|
|
101
|
+
#[inline(always)]
|
|
102
|
+
fn sense(&self, mask: u32) -> f32 {
|
|
103
|
+
let mut acc = 0.0f32;
|
|
104
|
+
let mut m = mask & valid_mask(self.n);
|
|
105
|
+
while m != 0 {
|
|
106
|
+
let bit = m.trailing_zeros() as usize;
|
|
107
|
+
acc += load_f32(&self.conc[bit]);
|
|
108
|
+
m &= m - 1;
|
|
109
|
+
}
|
|
110
|
+
acc
|
|
111
|
+
}
|
|
112
|
+
fn tick(&self, decay: f32) {
|
|
113
|
+
for c in self.conc.iter() {
|
|
114
|
+
mul_f32(c, decay);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
fn snapshot(&self) -> Vec<f32> {
|
|
118
|
+
self.conc.iter().map(load_f32).collect()
|
|
119
|
+
}
|
|
120
|
+
fn total(&self) -> f32 {
|
|
121
|
+
self.conc.iter().map(load_f32).sum()
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// --------------------------------------------------------------------------- //
|
|
126
|
+
// HormonalBus — the Python-facing signalling bus
|
|
127
|
+
// --------------------------------------------------------------------------- //
|
|
128
|
+
/// Lock-free hormonal signalling bus. `inject`/`secrete` are O(popcount) and
|
|
129
|
+
/// release the GIL; `sense` reads the current concentration for a channel mask.
|
|
130
|
+
#[pyclass]
|
|
131
|
+
pub struct HormonalBus {
|
|
132
|
+
core: Arc<BusCore>,
|
|
133
|
+
tx: Sender<(u32, f32)>,
|
|
134
|
+
rx: Receiver<(u32, f32)>,
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
#[pymethods]
|
|
138
|
+
impl HormonalBus {
|
|
139
|
+
#[new]
|
|
140
|
+
#[pyo3(signature = (num_signals = 32, event_capacity = 4096))]
|
|
141
|
+
fn new(num_signals: usize, event_capacity: usize) -> Self {
|
|
142
|
+
let n = num_signals.clamp(1, 32);
|
|
143
|
+
let (tx, rx) = bounded(event_capacity.max(1));
|
|
144
|
+
Self { core: Arc::new(BusCore::new(n)), tx, rx }
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/// Inject a typed `HormonalSignal` (the primary API).
|
|
148
|
+
fn inject(&self, py: Python<'_>, signal: HormonalSignal) {
|
|
149
|
+
py.allow_threads(|| {
|
|
150
|
+
self.core.secrete(signal.flags, signal.intensity);
|
|
151
|
+
let _ = self.tx.try_send((signal.flags, signal.intensity));
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/// Raw injection by `(flags, intensity)` — the microsecond hot path.
|
|
156
|
+
fn secrete(&self, py: Python<'_>, flags: u32, intensity: f32) {
|
|
157
|
+
py.allow_threads(|| {
|
|
158
|
+
self.core.secrete(flags, intensity);
|
|
159
|
+
let _ = self.tx.try_send((flags, intensity));
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/// Current summed concentration across every channel in `mask`.
|
|
164
|
+
fn sense(&self, py: Python<'_>, mask: u32) -> f32 {
|
|
165
|
+
py.allow_threads(|| self.core.sense(mask))
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/// Concentration of a single channel bit.
|
|
169
|
+
fn concentration(&self, signal_bit: usize) -> f32 {
|
|
170
|
+
if signal_bit < self.core.n {
|
|
171
|
+
load_f32(&self.core.conc[signal_bit])
|
|
172
|
+
} else {
|
|
173
|
+
0.0
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/// Multiplicative decay/dissipation of every channel (temporal homeostasis).
|
|
178
|
+
fn tick(&self, py: Python<'_>, decay: f32) {
|
|
179
|
+
py.allow_threads(|| self.core.tick(decay));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
fn snapshot(&self) -> Vec<f32> {
|
|
183
|
+
self.core.snapshot()
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/// Drain up to `max` buffered signal events as `(flags, intensity)` pairs.
|
|
187
|
+
fn drain_events(&self, max: usize) -> Vec<(u32, f32)> {
|
|
188
|
+
let mut out = Vec::new();
|
|
189
|
+
while out.len() < max {
|
|
190
|
+
match self.rx.try_recv() {
|
|
191
|
+
Ok(e) => out.push(e),
|
|
192
|
+
Err(_) => break,
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
out
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
fn stats(&self) -> HashMap<String, f64> {
|
|
199
|
+
let mut m = HashMap::new();
|
|
200
|
+
m.insert("num_signals".into(), self.core.n as f64);
|
|
201
|
+
m.insert("secretions".into(), self.core.secretions.load(Ordering::Relaxed) as f64);
|
|
202
|
+
m.insert("total_concentration".into(), self.core.total() as f64);
|
|
203
|
+
m
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//! B.I.O.M.A. Micro-Kernel — a lean efficiency & resilience core for LLM infra.
|
|
2
|
+
//!
|
|
3
|
+
//! Lean topology: exactly two proven primitives, nothing else.
|
|
4
|
+
//! * [`hormonal_bus`] — lock-free in-memory signal injection (~2M sig/s, ~5μs).
|
|
5
|
+
//! * [`context_apoptosis`] — autonomous history dehydration (universal input-token savings).
|
|
6
|
+
//!
|
|
7
|
+
//! No agents, no mitosis, no orchestration — only the microsecond hot path and the
|
|
8
|
+
//! apoptosis filter, exposed to Python via PyO3.
|
|
9
|
+
|
|
10
|
+
mod context_apoptosis;
|
|
11
|
+
mod hormonal_bus;
|
|
12
|
+
|
|
13
|
+
use pyo3::prelude::*;
|
|
14
|
+
use pyo3::wrap_pyfunction;
|
|
15
|
+
|
|
16
|
+
use context_apoptosis::{dehydrate, saturation_scan, ContextApoptosis};
|
|
17
|
+
use hormonal_bus::{HormonalBus, HormonalSignal};
|
|
18
|
+
|
|
19
|
+
/// The native module — `import bioma_micro`.
|
|
20
|
+
#[pymodule]
|
|
21
|
+
fn bioma_micro(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
22
|
+
// Signal injection
|
|
23
|
+
m.add_class::<HormonalBus>()?;
|
|
24
|
+
m.add_class::<HormonalSignal>()?;
|
|
25
|
+
// Apoptosis filter
|
|
26
|
+
m.add_class::<ContextApoptosis>()?;
|
|
27
|
+
m.add_function(wrap_pyfunction!(dehydrate, m)?)?;
|
|
28
|
+
m.add_function(wrap_pyfunction!(saturation_scan, m)?)?;
|
|
29
|
+
|
|
30
|
+
// Metabolic signal classes (bitwise flags) for the Python layer.
|
|
31
|
+
m.add("SYSTEM", context_apoptosis::SYSTEM)?;
|
|
32
|
+
m.add("USER", context_apoptosis::USER)?;
|
|
33
|
+
m.add("ASSISTANT", context_apoptosis::ASSISTANT)?;
|
|
34
|
+
m.add("FACT", context_apoptosis::FACT)?;
|
|
35
|
+
m.add("TOOL", context_apoptosis::TOOL)?;
|
|
36
|
+
|
|
37
|
+
m.add("__doc__", "B.I.O.M.A. Micro-Kernel — lock-free hormonal bus + context apoptosis.")?;
|
|
38
|
+
m.add("__version__", "1.0.0")?;
|
|
39
|
+
Ok(())
|
|
40
|
+
}
|