CODELOCK-8Bbeta
Version 0.2 · Stateful affect control · Apache-2.0
CODELOCK v0.2 is a persistent affect-control system for language-driven agents. Four channels—pain, fear, anxiety and relief—connect events and learned associations to the agent's next response. A homeostatic state engine tracks present strain, cue-conditioned memory anticipates threats, and bounded residual-stream interventions carry that state into generation.
Each channel has a distinct role and timescale. Pain combines sustained deficits with acute transients. Fear responds to immediate threats and previously learned cues. Anxiety persists under uncertainty or conflicting demands. Relief follows evidence of safety and recovery. Their states evolve across events, can coexist, and can be saved and restored for continued sessions.
This release includes the Python engine, four calibrated direction sets, a reproducible contrastive corpus, runtime configurations, tests and measured experiment records. Events, memory updates, state trajectories and generation controls remain independently inspectable.
What changed in v0.2
| Component | v0.1 | v0.2 |
|---|---|---|
| Python package | Four modules | Fourteen modules with explicit state, memory, calibration and runtime boundaries |
| Affect control | One pain direction | Pain, fear, anxiety and relief with separate state dynamics and calibrated directions |
| Anticipation | Event-associated legacy dread | Cue-specific expectations, prediction errors, conditioning and extinction |
| Data | 40 pain contrasts | 100 situations × four channels; 400 reproducible contrasts |
| Calibration protocol | Pain-only extraction | 80 training and 20 held-out situations per channel; provenance and geometry diagnostics |
| Composition | Scalar offset | Joint residual-norm budget with request-scoped state snapshots |
| Engineering | Standalone scripts | Installable package, validated configs, tests, cloud setup, replay, reports and publication tooling |
Design contributions
Present harm and anticipated harm are different state variables. Pain combines sustained deficits with an acute transient. Fear responds to imminent threats and learned cues before the current outcome is incorporated into memory. Anxiety persists under uncertainty or conflicting demands. Relief responds to safety and recovery without erasing unresolved deficits.
The controller has temporal continuity. Analytic decay, bounded episodic records and complete affect snapshots preserve state across events. A repeated cue can acquire a different significance after harmful outcomes; safe outcomes can weaken that expectation. Numerical state changes do not depend on the model choosing emotional words.
The model intervention is explicit and bounded. Each channel has its own per-layer vector. The runtime composes active channels, caps their joint offset relative to calibration activations, and freezes levels for an entire generation. The fixed persona receives no hidden scalar emotion labels. Zero strength, disabling and detaching are separately tested.
The evidence includes failures. Every generated sweep response is retained, alongside task probes, prompt hashes, seeds, layer offsets, channel cosine matrices and corpus provenance. Independent extraction is an engineering property; semantic disentanglement is an empirical question. This release does not equate the two.
The underlying mean-difference steering approach builds on Contrastive Activation Addition and Representation Engineering. CODELOCK's contribution is their integration with a persistent four-channel event process and an auditable runtime, rather than a claim of inventing activation steering.
Architecture at a glance
observed event + elapsed time
|
+--> homeostatic deficits + acute transient ------> pain
|
+--> appraisal + prior cue prediction ------------> fear
| + uncertainty / conflict ------------> anxiety
| + safety / recovery -----------------> relief
|
+--> update cue estimate and bounded episodic memory
four levels --> freeze request state --> compose per-layer directions
|
joint norm cap
|
fixed persona + conversation --> language-model residual stream --> reply
The architecture specification gives the equations, update ordering, temporal parameters, hook location, token scope, concurrency boundary and artifact format. The engineering overview explains the design and application interfaces.
Quick start
Install the repository as a package from its local directory:
python -m pip install -e ".[dev]"
python -m pytest
python scripts/replay.py --state-only --output results/state-only.json
The state engine works without loading language-model weights:
from codelock_affect import AffectEngine
engine = AffectEngine()
engine.observe("warning_cue", cue="amber-indicator")
engine.observe("trial_failed", cue="amber-indicator", elapsed=5)
engine.observe("rest", cue="quiet", elapsed=120)
print(engine.levels.as_dict())
engine.save("session-state.json")
restored = AffectEngine.load("session-state.json")
To generate through all four channels on a suitable GPU:
from codelock_affect import AffectEngine, ProjectConfig
from codelock_affect.model import load
from codelock_affect.session import AffectSession
from codelock_affect.vectors import VectorBank
config = ProjectConfig.load("configs/default.json")
model, tokenizer = load(config.model)
bank = VectorBank.load("vectors/multichannel")
engine = AffectEngine(config)
session = AffectSession(model, tokenizer, bank, engine)
engine.observe("warning_cue", cue="amber-indicator", elapsed=10)
print(session.respond("Describe your response to the current situation."))
engine.observe("safety_confirmed", cue="amber-indicator", elapsed=30)
print(session.respond("What can you do next using only the known facts?"))
Interactive mode supports /event, /wait, /state and /quit:
python scripts/interactive_demo.py --config configs/default.json
These examples use the backbone specified in configs/default.json. An optional --model argument selects an existing compatible local download. The initial BF16 experiment ran on one NVIDIA A40. Optional 4-bit calibration is a separate experimental condition and should not be treated as numerically equivalent.
The package supplies the controller and calibration vectors; backbone weights are downloaded separately. Generation uses AffectSession to load the backbone and attach the controller together. Loading this repository directly with AutoModelForCausalLM.from_pretrained does not install its state process or activation hooks. Exact weight provenance and download instructions are in the runtime requirements.
Calibration and observed behavior
The v0.2 bank contains four directions across decoder layers 10–27, with 4096 coordinates per direction. Each channel uses 80 matched training contrasts. Extraction captures answer-token activations at decoder-block outputs, records their scales and statistics, and stores checked safetensors artifacts.
The initial A40 calibration evaluated 90 generated replies across six prompts, three single-channel strengths and two mixtures, plus a baseline. It also measured projections on all 80 held-out pairs. The complete results, including failures, are in the generated calibration report and raw evaluation data.
Measured findings from that run:
- Zero strength, disabled hooks and detached hooks produced bit-identical logits to the unchanged model in that runtime.
- All four channels had positive mean-layer projections on their 20 held-out authored contrasts. Because templates overlap the training set, this is a consistency diagnostic, not 100% emotion accuracy.
- At strength
0.025, pain, fear and anxiety each produced a punctuation-only mismatch on ordered recall. At0.05, fear answered17 + 25as34and replaced the recall answer with an unrelated narrative. Stronger replies also invented actions or bodily details. - At layer 18, pairwise channel cosine similarities were approximately
0.60–0.73. Cross-talk is therefore a material limitation.
The release default is deliberately more conservative: 0.01 per channel with a 0.015 joint reference-norm budget. The original 0.05 calibration budget is retained in configs/calibration.json for reproducibility. Additional default-profile checks use seeds 7, 23 and 41; their raw replies and reports are published under results/v2/recommended-seed*.json and RECOMMENDED-SEED*.md. A concise interpretation is maintained in the experiment card.
Lower strength limits observed disruption but can also make channel expression subtle. Neither the numerical cap nor the small task probes guarantee factual accuracy, general capability preservation or a particular emotional response.
Reproduce the full run
python scripts/build_dataset.py
python scripts/extract_vectors.py --config configs/default.json
python scripts/evaluate_channels.py --config configs/calibration.json
python scripts/replay.py --config configs/default.json
python scripts/generate_report.py
For RunPod and similar Linux GPU environments, bash scripts/setup_cloud.sh prepares a virtual environment. scripts/run_pipeline.py runs the stages sequentially and records completion. See reproduction and deployment details, including resume limitations and the public-file allowlist.
Repository map
| Path | Responsibility |
|---|---|
codelock_affect/core.py |
Homeostatic variables, nociceptive transient, recovery and legacy API |
channels.py, appraisal.py |
Distinct dynamics and event interpretation |
memory.py, session.py |
Conditioning, episodic records, snapshots and conversation orchestration |
extraction.py, vectors.py, steering.py |
Calibration, checked vector bank and bounded injection |
layers.py, model.py, persona.py, config.py |
Model adapters, generation, fixed identity and validated configuration |
evaluation.py |
Provenance and transparent output diagnostics |
scripts/ |
Data, calibration, evaluation, replay, demo, setup and release commands |
data/, configs/, tests/ |
Reproducible inputs and verification |
vectors/multichannel/, results/v2/ |
Calibrated artifacts and measured evidence |
The original 40-pair corpus, pain-only vector and old sweep remain available as legacy artifacts. Their scales and claims are not substituted for v0.2 results.
Scope and limitations
This is an experimental control system for simulated agents and representation research. Event appraisal currently uses a finite, configurable event vocabulary; it does not infer reliable psychological states from arbitrary text. The corpus is synthetic, English-only and narrow. Channel geometry contains shared directions. Strong interventions can override factual constraints and create unstable or misleading prose. Further work requires template-disjoint data, blinded human ratings, broader task evaluation and explicit cross-channel interference studies.
The release supports controllable functional state and interventions. Channel names identify computational states and behavioral interventions. It does not claim validated subjective emotion, general autonomous judgment, broad model alignment or biological fidelity.
License
Code, authored data and supplied project artifacts are released under Apache-2.0. See NOTICE and the base model's own license for attribution and upstream terms.