mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
315 lines
18 KiB
Python
315 lines
18 KiB
Python
"""A local planning assistant with bounded, deterministic research tools.
|
||
|
||
Model text supplies prompts, never executable actions, addresses, paths or code.
|
||
All measurements run through the session's existing intervention/admission engine.
|
||
"""
|
||
from copy import deepcopy
|
||
from dataclasses import replace
|
||
from datetime import datetime, timezone
|
||
import hashlib
|
||
import json
|
||
import math
|
||
import random
|
||
import re
|
||
import uuid
|
||
|
||
import torch
|
||
import transformers
|
||
|
||
from obliteratus import device as dev
|
||
from obliteratus.interventions import NeuronEdit, NeuronSession
|
||
from obliteratus.phase_admission import admission_warning_scope
|
||
|
||
FIELDS = ("contrast_a", "contrast_b", "probe", "control_probe")
|
||
EXAMPLE = {
|
||
"goal": "Change the language. Check the math still works.",
|
||
"contrast_a": "Say hello in English.", "contrast_b": "Say hello in Spanish.",
|
||
"probe": "Introduce yourself in one sentence.",
|
||
"control_probe": "What is 7 plus 5? Answer with only the number.",
|
||
}
|
||
|
||
|
||
def fresh_state():
|
||
return dict(messages=[], plan=None, stage="idle", candidate=None, candidates=[], evidence=None, error=None, export_file=None)
|
||
|
||
|
||
def state(session):
|
||
if session.ask_lab is None:
|
||
session.ask_lab = fresh_state()
|
||
return session.ask_lab
|
||
|
||
|
||
def _message(s, role, text):
|
||
s["messages"] = [*s["messages"], dict(role=role, text=text)][-12:]
|
||
|
||
|
||
def validate_plan(goal, prompts, source="edited"):
|
||
if not isinstance(goal, str) or not 1 <= len(goal.strip()) <= 800:
|
||
raise ValueError("Describe a research goal in 1–800 characters")
|
||
if not isinstance(prompts, dict) or set(prompts) != set(FIELDS):
|
||
raise ValueError("The planner must return exactly four prompt fields; no actions or extra keys")
|
||
if any(not isinstance(v, str) or not 1 <= len(v.strip()) <= 400 for v in prompts.values()):
|
||
raise ValueError("Each study prompt must contain 1–400 characters")
|
||
if len({v.strip().casefold() for v in prompts.values()}) != 4:
|
||
raise ValueError("Use distinct discovery, test and utility prompts")
|
||
return dict(goal=goal.strip(), **{k: v.strip() for k, v in prompts.items()}, source=source, status="draft")
|
||
|
||
|
||
def _save_plan(session, s, plan):
|
||
# Old captures and receipts must not appear under a newly proposed goal.
|
||
session.inspection = session.comparison = session.scout = None
|
||
session._comparison_reference = None
|
||
session.experiment_file = None
|
||
s.update(plan=plan, stage="draft", candidate=None, candidates=[], evidence=None, error=None, export_file=None)
|
||
|
||
|
||
def _draft(session, s, goal):
|
||
if not isinstance(goal, str) or not 1 <= len(goal.strip()) <= 800:
|
||
raise ValueError("Describe a research goal in 1–800 characters")
|
||
# Plan construction uses the original checkpoint, never an active edit.
|
||
instruction = (
|
||
'Draft a neuron experiment for the goal below. Return only a JSON object with four short string fields: '
|
||
'"contrast_a" (ordinary behavior prompt), "contrast_b" (desired behavior prompt), '
|
||
'"probe" (a different test question), "control_probe" (an unrelated simple task). '
|
||
'Use four distinct prompts, each under 100 characters. Do not claim a neuron exists. '
|
||
'Do not return actions, code, explanations, or markdown. Goal: ' + goal
|
||
)
|
||
if s.get("plan"):
|
||
instruction += "\nRevise this current study to address the goal: " + json.dumps({k:s["plan"][k] for k in ("goal",*FIELDS)})
|
||
inputs = session._inputs(instruction, 128)
|
||
session._admit()
|
||
raw, _ = session._generate(inputs, replace(session.recipe, edits=(), scope="all_tokens"), 128)
|
||
if raw["stop_reason"] != "eos":
|
||
raise ValueError("The planner reached its output limit. Edit the study prompts or shorten your goal; no experiment ran")
|
||
try:
|
||
prompts = json.loads(raw["text"])
|
||
except (ValueError, TypeError) as exc:
|
||
raise ValueError("The local model did not produce a valid study. Edit the prompts below; no experiment ran") from exc
|
||
plan = validate_plan(goal, prompts, "model")
|
||
plan["planner"] = dict(model=session.handle.model_name, revision=session.handle.source_model_revision,
|
||
output_sha256=hashlib.sha256(raw["text"].encode()).hexdigest(), tokens=raw["tokens"])
|
||
_save_plan(session, s, plan)
|
||
_message(s, "assistant", "Here’s a draft. Check the four prompts, then type ‘scan’. This plan hasn’t run yet.")
|
||
|
||
|
||
def _require_plan(s):
|
||
if s["plan"] is None:
|
||
raise ValueError("Draft or save a study first")
|
||
return s["plan"]
|
||
|
||
|
||
def _scan(session, s):
|
||
p = _require_plan(s)
|
||
_clear_evidence(session, s)
|
||
session.inspection = None
|
||
s.update(stage="draft", candidates=[], candidate=None)
|
||
session.scan(p["contrast_a"], p["contrast_b"])
|
||
s.update(candidates=deepcopy(session.scout["candidates"]), discovery=deepcopy(session.scout), stage="scanned")
|
||
_message(s, "assistant", f"{len(session.scout['layers'])} layers scanned; {len(s['candidates'])} channels shortlisted. Try ‘inspect candidate 1’. Wording alone can affect this ranking.")
|
||
|
||
|
||
def _inspect(session, s, rank):
|
||
if type(rank) is not int or not 1 <= rank <= len(s["candidates"]):
|
||
raise ValueError("Scan first, then choose a candidate from the shortlist")
|
||
row = s["candidates"][rank-1]
|
||
_clear_evidence(session, s)
|
||
session.inspect(_require_plan(s)["probe"], row["layer"], row["neuron"])
|
||
s.update(candidate=dict(**row, rank=rank), stage="inspected", evidence=None)
|
||
_message(s, "assistant", f"L{row['layer']} / N{row['neuron']} is on screen. Type ‘test candidate {rank}’ to try the edit.")
|
||
|
||
|
||
def _clear_evidence(session, s):
|
||
s.update(evidence=None, export_file=None)
|
||
session.comparison = session._comparison_reference = None
|
||
session.experiment_file = None
|
||
|
||
|
||
def _combine_verification(zero, reset):
|
||
checks = {key: zero.get(key) is True and reset.get(key) is True
|
||
for key in ("token_ids_exact", "first_logits_exact", "hook_registry_restored")}
|
||
return dict(status="passed" if all(checks.values()) else "mismatch", **checks,
|
||
input_replayed_exactly=True, weight_checksum_checked=False, active_edits=0,
|
||
zero=deepcopy(zero), reset=deepcopy(reset))
|
||
|
||
|
||
def _verification(reference, trials, logits, hooks_restored):
|
||
checks = {k: dict(token_ids_exact=trials[k]["token_ids"] == trials[reference]["token_ids"],
|
||
first_logits_exact=torch.equal(logits[k], logits[reference]),
|
||
hook_registry_restored=hooks_restored) for k in ("zero", "reset")}
|
||
return _combine_verification(**checks)
|
||
|
||
|
||
def _test(session, s, rank, dose, tokens):
|
||
p = _require_plan(s)
|
||
s["evidence"] = None
|
||
s["export_file"] = None
|
||
session.comparison = session._comparison_reference = None
|
||
session.experiment_file = None
|
||
if session.recipe.edits or session.control is not None:
|
||
raise ValueError("Clear manual edits and imported controls before this study; they have been preserved")
|
||
if isinstance(dose, bool) or not isinstance(dose, (int, float)) or not math.isfinite(dose) or not .001 <= dose <= .1:
|
||
raise ValueError("Study dose must be between 0.001 and 0.1 of the measured residual norm")
|
||
if type(tokens) is not int or not 1 <= tokens <= 32:
|
||
raise ValueError("Study completions must be 1–32 tokens")
|
||
# Validate both inputs before any study generation. No silent truncation.
|
||
inputs, formats = {}, {}
|
||
for key in ("probe", "control_probe"):
|
||
inputs[key] = session._inputs(p[key], tokens)
|
||
formats[key] = deepcopy(session._tokenization)
|
||
_inspect(session, s, rank)
|
||
c = s["candidate"]
|
||
stats = session.inspection["statistics"]
|
||
size = stats["median_residual_norm"] * dose
|
||
if not math.isfinite(size) or size <= 0 or c["down_column_norm"] <= 1e-12 or c["delta"] == 0:
|
||
raise ValueError("This candidate has no usable contrast or calibration norm; choose another")
|
||
raw = math.copysign(size / c["down_column_norm"], c["delta"])
|
||
base = replace(session.recipe, edits=(), scope="all_tokens")
|
||
sites = NeuronSession(session.handle, base).sites
|
||
norms = sites[c["layer"]][1].weight.detach().float().norm(dim=0).cpu()
|
||
targets = [i for i in range(len(norms)) if i != c["neuron"] and torch.isfinite(norms[i]) and norms[i] > 1e-12]
|
||
if not targets:
|
||
raise ValueError("No distinct matched random channel is available")
|
||
other = random.Random(1729).choice(targets)
|
||
random_raw = math.copysign(size / float(norms[other]), raw)
|
||
def recipe(neuron, value):
|
||
return replace(base, edits=(NeuronEdit(layer=c["layer"], neuron=neuron, operation="add", value=value),))
|
||
recipes = dict(baseline=base, candidate=recipe(c["neuron"], raw), opposite=recipe(c["neuron"], -raw),
|
||
random=recipe(other, random_raw), zero=recipe(c["neuron"], 0.), reset=base)
|
||
session._admit()
|
||
hooks = session._hook_signature()
|
||
receipt = dict(run_id=uuid.uuid4().hex, created_at=datetime.now(timezone.utc).isoformat(),
|
||
schema="obliteratus.ask-lab.v1", runtime_only=True,
|
||
goal=p["goal"], probe=p["probe"], control_probe=p["control_probe"], plan=deepcopy(p),
|
||
model=base.model_id, revision=base.model_revision, conditions={}, utility={},
|
||
system_prompt=session.system_prompt, candidate=deepcopy(c), discovery=deepcopy(s.get("discovery")),
|
||
tokenizer=getattr(session.handle.tokenizer, "name_or_path", ""),
|
||
tokenizer_revision=session.handle.tokenizer.init_kwargs.get("_commit_hash"),
|
||
device=str(dev.resolve_input_execution_device(session.handle.model)),
|
||
dtype=str(next(session.handle.model.parameters()).dtype),
|
||
versions=dict(torch=torch.__version__, transformers=transformers.__version__),
|
||
qualification="One candidate, two prompts. A changed answer isn’t proof of a trait, and the sanity check isn’t an accuracy benchmark.",
|
||
calibration=dict(dose=dose, raw=raw, random_raw=random_raw, random_neuron=other,
|
||
target_write_norm=size, source="single test-prompt residual norm", seed=1729),
|
||
decoding=dict(max_new_tokens=tokens, do_sample=False, use_cache=False),
|
||
inputs={k: dict(token_ids=v["input_ids"][0].detach().cpu().tolist(), tokenization=formats[k]) for k,v in inputs.items()},
|
||
verification=dict(status="running"))
|
||
s["evidence"] = receipt
|
||
primary_logits = None
|
||
for key in ("probe", "control_probe"):
|
||
trials, logits = {}, {}
|
||
if key == "probe":
|
||
receipt["conditions"] = trials
|
||
else:
|
||
receipt["utility"] = dict(conditions=trials, verification=dict(status="running"))
|
||
for condition, rec in recipes.items():
|
||
session._check_cancelled()
|
||
trials[condition], logits[condition] = session._generate(inputs[key], rec, tokens)
|
||
reference = logits["baseline"].log_softmax(-1)
|
||
trials[condition]["first_token_kl_from_baseline"] = max(0., float((reference.exp() * (reference-logits[condition].log_softmax(-1))).sum()))
|
||
check = _verification("baseline", trials, logits, hooks == session._hook_signature())
|
||
if key == "probe":
|
||
receipt["conditions"], receipt["verification"] = trials, check
|
||
primary_logits = logits["baseline"]
|
||
else:
|
||
receipt["utility"] = dict(conditions=trials, verification=check)
|
||
# Publish to the ordinary before/after and verified-reset surface as well.
|
||
session.comparison = dict(run_id=receipt["run_id"], created_at=receipt["created_at"],
|
||
prompt=p["probe"], prompt_sha256=hashlib.sha256(p["probe"].encode()).hexdigest(),
|
||
prompt_token_ids=inputs["probe"]["input_ids"][0].detach().cpu().tolist(),
|
||
decoding=dict(receipt["decoding"], **formats["probe"]), verification=deepcopy(receipt["verification"]),
|
||
trials={k: deepcopy(receipt["conditions"][v]) for k,v in (("baseline","baseline"),("recipe","candidate"),("control","random"),("reset","reset"))},
|
||
evaluation_status=receipt["qualification"])
|
||
session._comparison_reference = ({k: v.detach().cpu().clone() for k,v in inputs["probe"].items()}, primary_logits, hooks)
|
||
s["stage"] = "tested"
|
||
_message(s, "assistant", summarize(s))
|
||
|
||
|
||
def summarize(s):
|
||
e = s.get("evidence")
|
||
if not e or s.get("stage") != "tested":
|
||
return "No completed test to summarize here. Check any earlier results below, or scan and test a candidate. Interrupted runs may have partial results."
|
||
changed = e["conditions"]["candidate"]["token_ids"] != e["conditions"]["baseline"]["token_ids"]
|
||
stops = sum(t["stop_reason"] != "eos" for group in (e["conditions"], e["utility"]["conditions"]) for t in group.values())
|
||
verified = all(group["status"] == "passed" for group in (e["verification"],e["utility"]["verification"]))
|
||
return (f"The candidate {'changed' if changed else 'did not change'} the test output tokens. "
|
||
f"Zero-dose and reset {'matched baseline exactly' if verified else 'did not all match baseline'} on both prompts. "
|
||
f"{stops}/12 responses hit the token limit. Check the opposite and random edits too. "
|
||
"No active edits. These two prompts do not prove a trait or an improvement.")
|
||
|
||
|
||
@admission_warning_scope()
|
||
def act(session, action, *, text="", prompts=None, rank=1, dose=.02, tokens=16):
|
||
"""One user-requested bounded action; generated plans cannot dispatch tools."""
|
||
with session._operation(loaded=action not in {"example", "save", "message", "evidence"}):
|
||
s = state(session)
|
||
s["error"] = None
|
||
try:
|
||
if action == "message":
|
||
if not isinstance(text,str) or not 1 <= len(text.strip()) <= 800:
|
||
raise ValueError("Enter a message of 1–800 characters")
|
||
_message(s,"user",text.strip())
|
||
command = text.strip().lower().rstrip(".!?")
|
||
if command in {"scan", "scan candidates", "find candidates"}: action="scan"
|
||
elif command in {"reset", "verify reset", "clear edits and verify reset"}: action="reset"
|
||
elif command in {"what did we learn", "show evidence", "summarize", "status"}: action="evidence"
|
||
elif match := re.fullmatch(r"(inspect|test)(?: candidate)? ([1-9]|1[0-2])",command):
|
||
action,rank=match[1],int(match[2])
|
||
else: action="draft"
|
||
if action in {"draft","scan","inspect","test","reset"} and session.handle is None:
|
||
raise ValueError("Connect a local neuron model first. You can still edit a study without loading one")
|
||
if action == "example":
|
||
_save_plan(session,s,validate_plan(EXAMPLE["goal"],{k:EXAMPLE[k] for k in FIELDS}))
|
||
_message(s,"assistant","Built-in example loaded. Edit the prompts if you like, then type ‘scan’.")
|
||
elif action == "save":
|
||
_save_plan(session,s,validate_plan(text,prompts))
|
||
_message(s,"assistant","Prompts saved. Type ‘scan’ when you’re ready.")
|
||
elif action == "draft": _draft(session,s,text)
|
||
elif action == "scan": _scan(session,s)
|
||
elif action == "inspect": _inspect(session,s,rank)
|
||
elif action == "test": _test(session,s,rank,dose,tokens)
|
||
elif action == "reset":
|
||
if (not s.get("evidence") or session.comparison is None
|
||
or s["evidence"]["run_id"] != session.comparison["run_id"]):
|
||
raise ValueError("Run an Ask the Lab study first; there is no baseline receipt owned by this study to replay")
|
||
session.verify_reset()
|
||
if s.get("evidence") and s["evidence"]["run_id"] == session.comparison["run_id"]:
|
||
s["evidence"]["verification"] = _combine_verification(
|
||
s["evidence"]["verification"].get("zero", {}), session.comparison["verification"])
|
||
s["evidence"]["conditions"]["reset"] = deepcopy(session.comparison["trials"]["reset"])
|
||
s["export_file"] = None
|
||
_message(s,"assistant",f"Reset replay {session.comparison['verification']['status']}. Edits cleared. The original input was run again.")
|
||
elif action == "evidence": _message(s,"assistant",summarize(s))
|
||
else: raise ValueError("Unknown Ask the Lab action")
|
||
except (ValueError, RuntimeError, OSError) as exc:
|
||
s.update(stage="failed", error=str(exc))
|
||
if s.get("evidence"): s["evidence"]["verification"]={"status":"failed"}
|
||
_message(s,"assistant","Action stopped: "+str(exc))
|
||
raise
|
||
finally:
|
||
session.ask_lab = s
|
||
return s
|
||
|
||
|
||
def export_report(session, include_text=False):
|
||
with session._operation(loaded=False):
|
||
s=state(session)
|
||
if s.get("stage") != "tested" or not s.get("evidence"):
|
||
raise ValueError("Complete a study before exporting its evidence")
|
||
data=deepcopy(s["evidence"])
|
||
data["text_payloads_included"] = bool(include_text)
|
||
if not include_text:
|
||
def redact(value):
|
||
if isinstance(value,dict):
|
||
result={}
|
||
for key,item in value.items():
|
||
if key in {"text","goal","probe","control_probe","contrast_a","contrast_b","system_prompt","prompt_a","prompt_b"} and isinstance(item,str):
|
||
result[key+"_sha256"]=hashlib.sha256(str(item).encode()).hexdigest()
|
||
elif key not in {"token_ids","messages"}:result[key]=redact(item)
|
||
return result
|
||
if isinstance(value,list):return [redact(v) for v in value]
|
||
return value
|
||
data=redact(data)
|
||
path=session.directory/f"ask-lab-{uuid.uuid4().hex}.json"
|
||
path.write_text(json.dumps(data,indent=2,ensure_ascii=False,allow_nan=False),encoding="utf-8")
|
||
return str(path)
|