Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 39,584 Bytes
5c3cfae db03c40 5c3cfae db03c40 5c3cfae db03c40 5c3cfae db03c40 5c3cfae db03c40 5c3cfae db03c40 5c3cfae db03c40 5c3cfae 51af902 5c3cfae db03c40 5c3cfae 51af902 5c3cfae | 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 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 | """Run the bio-experiment environment with Qwen3.5-0.8B as the planning agent."""
from __future__ import annotations
import json
import os
import re
import time
from pathlib import Path
from typing import Any, Dict, List, Optional
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from models import (
ActionType,
ExperimentAction,
ExperimentObservation,
OutputType,
build_agent_observation_context,
build_agent_system_prompt,
)
from server.hackathon_environment import BioExperimentEnvironment
DASHBOARD_STATE_PATH = Path(__file__).parent / "_dashboard_state.json"
DASHBOARD_CMD_PATH = Path(__file__).parent / "_dashboard_cmd.json"
USE_PIPELINE = os.getenv("RUN_AGENT_USE_PIPELINE", "0").strip().lower() not in {"0", "false", "off"}
def _parse_thinking_flag() -> bool:
import sys
if "--no-thinking" in sys.argv:
return False
if "--thinking" in sys.argv:
return True
return os.getenv("RUN_AGENT_ENABLE_THINKING", "1").strip().lower() not in {"0", "false", "off"}
ENABLE_THINKING = _parse_thinking_flag()
MODEL_ID = "Qwen/Qwen3.5-2B"
MAX_EPISODE_STEPS = int(os.getenv("RUN_AGENT_MAX_EPISODE_STEPS", "20"))
PIPELINE_TASK = "text-generation"
ACTION_TYPES = [a.value for a in ActionType]
ACTION_TYPE_ALIASES = {
"collect_samples": ActionType.COLLECT_SAMPLE.value,
"collect_sample_from_bone_marrow": ActionType.COLLECT_SAMPLE.value,
"collect_samples_from_bone_marrow": ActionType.COLLECT_SAMPLE.value,
"prepare_sc_library": ActionType.PREPARE_LIBRARY.value,
"sequence_single_cells": ActionType.SEQUENCE_CELLS.value,
"qc": ActionType.RUN_QC.value,
"run_quality_control": ActionType.RUN_QC.value,
"cluster": ActionType.CLUSTER_CELLS.value,
"de_analysis": ActionType.DIFFERENTIAL_EXPRESSION.value,
"differential_expression_analysis": ActionType.DIFFERENTIAL_EXPRESSION.value,
"trajectory_inference": ActionType.TRAJECTORY_ANALYSIS.value,
"infer_trajectory": ActionType.TRAJECTORY_ANALYSIS.value,
"network_inference": ActionType.REGULATORY_NETWORK_INFERENCE.value,
"select_markers": ActionType.MARKER_SELECTION.value,
"final_conclusion": ActionType.SYNTHESIZE_CONCLUSION.value,
}
SYSTEM_PROMPT = build_agent_system_prompt()
STANDARD_PIPELINE_ORDER = [
ActionType.COLLECT_SAMPLE,
ActionType.SELECT_COHORT,
ActionType.PREPARE_LIBRARY,
ActionType.SEQUENCE_CELLS,
ActionType.RUN_QC,
ActionType.FILTER_DATA,
ActionType.NORMALIZE_DATA,
ActionType.INTEGRATE_BATCHES,
ActionType.CLUSTER_CELLS,
ActionType.DIFFERENTIAL_EXPRESSION,
ActionType.PATHWAY_ENRICHMENT,
ActionType.MARKER_SELECTION,
ActionType.TRAJECTORY_ANALYSIS,
ActionType.REGULATORY_NETWORK_INFERENCE,
ActionType.SYNTHESIZE_CONCLUSION,
]
MODEL_RESPONSE_PREVIEW_CHARS = int(
os.getenv("RUN_AGENT_MODEL_RESPONSE_PREVIEW_CHARS", "240")
)
def compact_preview(value: Any, max_chars: int = 160) -> str:
try:
text = json.dumps(value, ensure_ascii=True, sort_keys=True)
except TypeError:
text = str(value)
text = re.sub(r"\s+", " ", text).strip()
if len(text) <= max_chars:
return text
return text[: max_chars - 3] + "..."
def format_observation(obs: ExperimentObservation) -> str:
parts = [
f"TASK: {obs.task.problem_statement}",
f"Organism: {obs.task.organism} | Tissue: {obs.task.tissue}",
f"Conditions: {', '.join(obs.task.conditions) or 'N/A'}",
f"Step: {obs.step_index} | Budget: ${obs.resource_usage.budget_remaining:,.0f} | Time: {obs.resource_usage.time_remaining_days:.0f}d",
]
context = build_agent_observation_context(obs, max_tools=5, max_assays=2)
if context:
parts.append(context)
if obs.pipeline_history:
last5 = obs.pipeline_history[-5:]
parts.append("Recent history:")
for h in last5:
tag = "OK" if h.success else "FAIL"
line = f" [{tag}] {h.action_type.value}"
if h.method:
line += f" ({h.method})"
line += f": {h.output_summary[:80]}"
parts.append(line)
completed = {h.action_type for h in obs.pipeline_history if h.success}
if completed:
parts.append(f"Completed steps (do NOT repeat): {', '.join(sorted(a.value for a in completed))}")
remaining = [a.value for a in STANDARD_PIPELINE_ORDER if a not in completed]
if remaining:
parts.append(f"Remaining steps (choose one): {', '.join(remaining)}")
if obs.latest_output and obs.latest_output.data:
parts.append(
f"Latest data: {compact_preview(obs.latest_output.data, 200)}"
)
if obs.rule_violations:
parts.append(f"VIOLATIONS: {obs.rule_violations}")
if obs.discovered_markers:
parts.append(f"Markers found so far: {obs.discovered_markers[:5]}")
parts.append(
'Output ONLY a single JSON object with these exact keys, no comments, no extra text:\n'
'{"action_type": "<one of the remaining steps>", "method": null, "parameters": {}, "justification": "<why>", "confidence": 0.8}'
)
return "\n".join(parts)
def _repair_truncated_json(text: str) -> Optional[str]:
"""Try to repair JSON truncated mid-value (common with small LLMs)."""
s = text.strip()
if not s.startswith("{"):
return None
# Drop dangling partial keys or empty key/value stubs at the tail.
s = re.sub(r',\s*"[^"\n]*$', '', s)
s = re.sub(r',\s*"[^"\n]*"\s*:\s*$', '', s)
in_string = False
escape = False
for ch in s:
if escape:
escape = False
continue
if ch == "\\":
escape = True
continue
if ch == '"':
in_string = not in_string
if in_string:
s += '"'
open_braces = s.count("{") - s.count("}")
open_brackets = s.count("[") - s.count("]")
s += "]" * max(0, open_brackets)
s += "}" * max(0, open_braces)
try:
obj = json.loads(s)
if isinstance(obj, dict):
return s
except json.JSONDecodeError:
pass
s = re.sub(r',\s*([}\]])', r'\1', s)
try:
obj = json.loads(s)
if isinstance(obj, dict):
return s
except json.JSONDecodeError:
pass
return None
def _normalize_jsonish_text(text: str) -> str:
"""Normalize common near-JSON artifacts emitted by small local models."""
text = _strip_js_comments(text)
text = re.sub(r'(?<=:\s)\bNone\b', 'null', text)
text = re.sub(r'(?<=:\s)\bTrue\b', 'true', text)
text = re.sub(r'(?<=:\s)\bFalse\b', 'false', text)
text = re.sub(r'"([^"\n]+?):"\s*,', r'"\1": "",', text)
return text
def _strip_js_comments(text: str) -> str:
"""Remove // and /* */ comments that small LLMs inject into JSON."""
text = re.sub(r'//[^\n]*', '', text)
text = re.sub(r'/\*.*?\*/', '', text, flags=re.DOTALL)
return text
def extract_json_object(text: str) -> Optional[Dict[str, Any]]:
stripped = _normalize_jsonish_text(text).strip()
if stripped.startswith('"') and stripped.endswith('"'):
try:
unwrapped = json.loads(stripped)
except json.JSONDecodeError:
unwrapped = None
if isinstance(unwrapped, str):
stripped = _normalize_jsonish_text(unwrapped).strip()
fence_prefix = "```"
if stripped.startswith(fence_prefix) and stripped.endswith(fence_prefix):
lines = stripped.splitlines()
if len(lines) >= 3:
stripped = "\n".join(lines[1:-1]).strip()
candidates: List[str] = [stripped]
start = stripped.find("{")
while start != -1:
depth = 0
for idx in range(start, len(stripped)):
char = stripped[idx]
if char == "{":
depth += 1
elif char == "}":
depth -= 1
if depth == 0:
candidates.append(stripped[start:idx + 1])
break
start = stripped.find("{", start + 1)
repaired = None
first_brace = stripped.find("{")
if first_brace != -1:
repaired = _repair_truncated_json(stripped[first_brace:])
if repaired is not None:
candidates.append(repaired)
candidates.sort(key=len, reverse=True)
for candidate in candidates:
try:
parsed = json.loads(candidate)
except json.JSONDecodeError:
continue
if isinstance(parsed, dict):
return parsed
return None
def _edit_distance(a: str, b: str) -> int:
if len(a) < len(b):
return _edit_distance(b, a)
if not b:
return len(a)
prev = list(range(len(b) + 1))
for i, ca in enumerate(a):
curr = [i + 1]
for j, cb in enumerate(b):
curr.append(min(prev[j + 1] + 1, curr[j] + 1, prev[j] + (ca != cb)))
prev = curr
return prev[-1]
def get_payload_value(payload: Dict[str, Any], *names: str) -> Any:
for name in names:
if name in payload:
return payload[name]
lowered = {
str(key).lower(): value
for key, value in payload.items()
}
for name in names:
if name.lower() in lowered:
return lowered[name.lower()]
for key, value in lowered.items():
for name in names:
threshold = max(2, len(name) // 3)
if _edit_distance(key, name.lower()) <= threshold:
return value
return None
def normalize_optional_string(value: Any) -> Optional[str]:
if value is None or isinstance(value, bool):
return None
if isinstance(value, str):
value = value.strip()
return value or None
if isinstance(value, (int, float)):
return str(value)
return compact_preview(value, 80)
def normalize_action_type(raw_action_type: Any) -> Optional[str]:
if not isinstance(raw_action_type, str):
return None
candidate = raw_action_type.strip().lower()
if candidate in ACTION_TYPES:
return candidate
if candidate in ACTION_TYPE_ALIASES:
return ACTION_TYPE_ALIASES[candidate]
candidate = re.sub(r"[^a-z0-9]+", "_", candidate).strip("_")
if candidate in ACTION_TYPES:
return candidate
if candidate in ACTION_TYPE_ALIASES:
return ACTION_TYPE_ALIASES[candidate]
heuristics = [
(("collect", "sample"), ActionType.COLLECT_SAMPLE.value),
(("library",), ActionType.PREPARE_LIBRARY.value),
(("sequence",), ActionType.SEQUENCE_CELLS.value),
(("qc",), ActionType.RUN_QC.value),
(("quality", "control"), ActionType.RUN_QC.value),
(("filter",), ActionType.FILTER_DATA.value),
(("normal",), ActionType.NORMALIZE_DATA.value),
(("integrat", "batch"), ActionType.INTEGRATE_BATCHES.value),
(("cluster",), ActionType.CLUSTER_CELLS.value),
(("differential", "expression"), ActionType.DIFFERENTIAL_EXPRESSION.value),
(("pathway",), ActionType.PATHWAY_ENRICHMENT.value),
(("trajectory",), ActionType.TRAJECTORY_ANALYSIS.value),
(("network",), ActionType.REGULATORY_NETWORK_INFERENCE.value),
(("marker",), ActionType.MARKER_SELECTION.value),
(("validat", "marker"), ActionType.VALIDATE_MARKER.value),
(("followup",), ActionType.DESIGN_FOLLOWUP.value),
(("review",), ActionType.REQUEST_SUBAGENT_REVIEW.value),
(("conclusion",), ActionType.SYNTHESIZE_CONCLUSION.value),
]
for fragments, normalized in heuristics:
if all(fragment in candidate for fragment in fragments):
return normalized
return None
def should_block_failed_reattempt(
history: List[Any], action_type: ActionType
) -> bool:
last_failed_idx = None
last_success_idx = None
for idx, record in enumerate(history):
if record.action_type != action_type:
continue
if record.success:
last_success_idx = idx
else:
last_failed_idx = idx
if last_failed_idx is None:
return False
# Allow retry after the same action has already succeeded once, or after the
# pipeline made progress with a different successful step since the failure.
if last_success_idx is not None and last_success_idx > last_failed_idx:
return False
for record in history[last_failed_idx + 1:]:
if record.success and record.action_type != action_type:
return False
return True
def parse_action(text: str) -> Optional[ExperimentAction]:
d = extract_json_object(text)
if d is not None:
action_type = normalize_action_type(get_payload_value(d, "action_type"))
if action_type is None:
pass
else:
parameters = get_payload_value(d, "parameters", "params") or {}
if not isinstance(parameters, dict):
parameters = {}
confidence = get_payload_value(d, "confidence")
if confidence is None:
confidence = 0.5
try:
confidence = float(confidence)
except (TypeError, ValueError):
confidence = 0.5
justification = get_payload_value(
d, "justification", "justifyement", "reasoning", "rationale", "reason"
)
if justification is not None and not isinstance(justification, str):
justification = compact_preview(justification, 200)
method = normalize_optional_string(get_payload_value(d, "method"))
return ExperimentAction(
action_type=ActionType(action_type),
method=method,
parameters=parameters,
justification=justification,
confidence=min(1.0, max(0.0, confidence)),
)
action_match = re.search(
r'["\']action_type["\']\s*:\s*["\']([^"\']+)',
text,
re.IGNORECASE,
)
if not action_match:
return None
action_type = normalize_action_type(action_match.group(1))
if action_type is None:
return None
method_match = re.search(
r'["\']method["\']\s*:\s*("((?:[^"\\]|\\.)*)"|null|none|true|false|-?\d+(?:\.\d+)?)',
text,
re.IGNORECASE,
)
confidence_match = re.search(
r'["\']confidence["\']\s*:\s*([0-9]*\.?[0-9]+)',
text,
re.IGNORECASE,
)
justification_match = re.search(
r'["\'](?:justif\w*|reasoning|rationale|reason)["\']\s*:\s*"((?:[^"\\]|\\.)*)',
text,
re.DOTALL | re.IGNORECASE,
)
confidence = 0.5
if confidence_match:
try:
confidence = float(confidence_match.group(1))
except ValueError:
confidence = 0.5
justification = None
if justification_match:
try:
justification = json.loads(f'"{justification_match.group(1)}"')
except json.JSONDecodeError:
justification = justification_match.group(1)
method = None
if method_match:
raw_method = method_match.group(1)
if raw_method.startswith('"') and raw_method.endswith('"'):
try:
method = json.loads(raw_method)
except json.JSONDecodeError:
method = raw_method.strip('"')
elif raw_method.lower() not in {"null", "none", "true", "false"}:
method = raw_method
method = normalize_optional_string(method)
return ExperimentAction(
action_type=ActionType(action_type),
method=method,
parameters={},
justification=justification,
confidence=min(1.0, max(0.0, confidence)),
)
def should_force_terminal_conclusion(
action: ExperimentAction,
completed_types: set[ActionType],
) -> bool:
meta_repeatables = {
ActionType.DESIGN_FOLLOWUP,
ActionType.REQUEST_SUBAGENT_REVIEW,
}
return (
action.action_type in meta_repeatables
and action.action_type in completed_types
and ActionType.SYNTHESIZE_CONCLUSION not in completed_types
)
def _unique_nonempty(items: List[str], limit: int = 5) -> List[str]:
seen: set[str] = set()
result: List[str] = []
for raw in items:
value = normalize_optional_string(raw)
if not value:
continue
key = value.upper()
if key in seen:
continue
seen.add(key)
result.append(value)
if len(result) >= limit:
break
return result
def _infer_conclusion_evidence(
obs: ExperimentObservation,
) -> tuple[List[str], List[str], Dict[str, float]]:
top_markers = _unique_nonempty(list(obs.discovered_markers), limit=5)
causal_mechanisms = _unique_nonempty(list(obs.candidate_mechanisms), limit=5)
predicted_pathways: Dict[str, float] = {}
for output in reversed(obs.all_outputs):
if not output.success:
continue
data = output.data or {}
if not top_markers:
if output.output_type == OutputType.MARKER_RESULT:
top_markers = _unique_nonempty(list(data.get("markers", [])), limit=5)
elif output.output_type == OutputType.DE_RESULT:
top_markers = _unique_nonempty(
[item.get("gene") for item in data.get("top_genes", []) if isinstance(item, dict)],
limit=5,
)
if output.output_type == OutputType.PATHWAY_RESULT and not predicted_pathways:
for item in data.get("top_pathways", []):
if not isinstance(item, dict):
continue
pathway = normalize_optional_string(item.get("pathway"))
score = item.get("score")
if pathway and isinstance(score, (int, float)):
predicted_pathways[pathway] = float(score)
if len(predicted_pathways) >= 5:
break
if not causal_mechanisms:
if output.output_type == OutputType.PATHWAY_RESULT:
causal_mechanisms = _unique_nonempty(
[item.get("pathway") for item in data.get("top_pathways", []) if isinstance(item, dict)],
limit=5,
)
elif output.output_type == OutputType.NETWORK_RESULT:
causal_mechanisms = _unique_nonempty(
list(data.get("top_regulators", [])),
limit=5,
)
if top_markers and causal_mechanisms and predicted_pathways:
break
return top_markers, causal_mechanisms, predicted_pathways
def ensure_conclusion_claims(
obs: ExperimentObservation,
action: ExperimentAction,
) -> ExperimentAction:
if action.action_type != ActionType.SYNTHESIZE_CONCLUSION:
return action
parameters = dict(action.parameters or {})
raw_claims = parameters.get("claims")
if isinstance(raw_claims, list) and raw_claims:
normalized_claims = [claim for claim in raw_claims if isinstance(claim, dict)]
if normalized_claims:
parameters["claims"] = normalized_claims
if parameters != action.parameters:
return action.model_copy(update={"parameters": parameters})
return action
top_markers, causal_mechanisms, predicted_pathways = _infer_conclusion_evidence(obs)
claim_type = "causal" if causal_mechanisms else "correlational"
conditions = " vs ".join(obs.task.conditions[:2]) if obs.task.conditions else "the task conditions"
claim = action.justification or f"Final synthesis for {conditions}."
parameters["claims"] = [{
"top_markers": top_markers,
"causal_mechanisms": causal_mechanisms,
"predicted_pathways": predicted_pathways,
"confidence": action.confidence,
"claim_type": claim_type,
"claim": claim,
}]
if not action.justification:
action = action.model_copy(update={"justification": claim})
return action.model_copy(update={"parameters": parameters})
def write_dashboard_state(
env: BioExperimentEnvironment,
obs: ExperimentObservation,
*,
step: int,
cumulative_reward: float,
model_response: str = "",
model_thinking: str = "",
action: Optional[ExperimentAction] = None,
gen_time: float = 0.0,
episode_done: bool = False,
) -> None:
"""Serialise the full world state (observable + latent) for the dashboard."""
latent = env._latent
snapshot: Dict[str, Any] = {
"timestamp": time.time(),
"step": step,
"episode_done": episode_done,
"cumulative_reward": cumulative_reward,
"gen_time_s": round(gen_time, 2),
"model_response_raw": model_response[:600],
"model_thinking": model_thinking[:800],
"thinking_enabled": ENABLE_THINKING,
}
snapshot["task"] = {
"problem_statement": obs.task.problem_statement,
"organism": obs.task.organism,
"tissue": obs.task.tissue,
"modality": obs.task.modality,
"conditions": obs.task.conditions,
"budget_limit": obs.task.budget_limit,
"time_limit_days": obs.task.time_limit_days,
}
snapshot["resources"] = {
"budget_used": round(obs.resource_usage.budget_used, 2),
"budget_remaining": round(obs.resource_usage.budget_remaining, 2),
"time_used_days": round(obs.resource_usage.time_used_days, 1),
"time_remaining_days": round(obs.resource_usage.time_remaining_days, 1),
"samples_consumed": obs.resource_usage.samples_consumed,
"compute_hours_used": round(obs.resource_usage.compute_hours_used, 2),
}
snapshot["pipeline_history"] = [
{
"step_index": h.step_index,
"action_type": h.action_type.value,
"method": h.method,
"output_summary": h.output_summary[:120],
"success": h.success,
"quality_score": round(h.quality_score, 3),
"resource_cost": round(h.resource_cost, 2),
"time_cost_days": round(h.time_cost_days, 1),
}
for h in obs.pipeline_history
]
if action:
snapshot["current_action"] = {
"action_type": action.action_type.value,
"method": action.method,
"parameters": action.parameters,
"justification": action.justification,
"confidence": action.confidence,
}
if obs.latest_output:
lo = obs.latest_output
snapshot["latest_output"] = {
"summary": lo.summary,
"success": lo.success,
"quality_score": round(lo.quality_score, 3),
"uncertainty": round(lo.uncertainty, 3),
"warnings": lo.warnings,
"data_preview": compact_preview(lo.data, 300) if lo.data else None,
}
snapshot["discovered_markers"] = obs.discovered_markers[:20]
snapshot["candidate_mechanisms"] = obs.candidate_mechanisms[:20]
snapshot["rule_violations"] = obs.rule_violations
snapshot["uncertainty_summary"] = {
k: round(v, 3) for k, v in obs.uncertainty_summary.items()
}
snapshot["reward_breakdown"] = {
k: round(v, 4) for k, v in obs.step_reward_breakdown.items()
}
if obs.conclusions:
snapshot["conclusions"] = [
{
"claim": c.claim,
"claim_type": c.claim_type,
"confidence": c.confidence,
"top_markers": c.top_markers,
"causal_mechanisms": c.causal_mechanisms,
"predicted_pathways": c.predicted_pathways,
}
for c in obs.conclusions
]
if latent:
bio = latent.biology
snapshot["latent"] = {
"cell_populations": [
{
"name": cp.name,
"proportion": round(cp.proportion, 3),
"marker_genes": cp.marker_genes[:8],
"state": cp.state,
}
for cp in bio.cell_populations
],
"true_markers": bio.true_markers,
"causal_mechanisms": bio.causal_mechanisms,
"true_pathways": {
k: round(v, 3) for k, v in list(bio.true_pathways.items())[:15]
},
"true_de_genes_count": sum(
len(genes) for genes in bio.true_de_genes.values()
),
"true_regulatory_network_size": sum(
len(targets) for targets in bio.true_regulatory_network.values()
),
"confounders": bio.confounders,
"n_true_cells": bio.n_true_cells,
"technical": {
"ambient_rna_fraction": latent.technical.ambient_rna_fraction,
"doublet_rate": latent.technical.doublet_rate,
"dropout_rate": latent.technical.dropout_rate,
"sample_quality": latent.technical.sample_quality,
"library_complexity": latent.technical.library_complexity,
"capture_efficiency": latent.technical.capture_efficiency,
},
"progress": latent.progress.model_dump(),
"hidden_failure_conditions": latent.hidden_failure_conditions,
}
try:
DASHBOARD_STATE_PATH.write_text(
json.dumps(snapshot, indent=2, default=str), encoding="utf-8"
)
except Exception:
pass
def log(msg: str) -> None:
print(msg, flush=True)
def build_observation_prompt(obs: ExperimentObservation) -> str:
return format_observation(obs)
def run_with_pipeline(pipe, prompt: str) -> str:
try:
_pipe_max = 2048 if ENABLE_THINKING else 300
result = pipe(prompt, max_new_tokens=_pipe_max, return_full_text=False)
except Exception:
return ""
if isinstance(result, list) and result:
result = result[0]
if isinstance(result, dict):
text = result.get("generated_text") or result.get("text") or result.get("answer")
elif isinstance(result, str):
text = result
else:
text = ""
return text.strip() if isinstance(text, str) else ""
def resolve_torch_runtime() -> Dict[str, Any]:
use_cuda = torch.cuda.is_available()
bf16 = bool(getattr(torch.cuda, "is_bf16_supported", lambda: False)()) if use_cuda else False
dtype = torch.bfloat16 if bf16 else (
torch.float16 if use_cuda else torch.float32
)
return {
"use_cuda": use_cuda,
"device": "cuda:0" if use_cuda else "cpu",
"dtype": dtype,
"device_map": "auto" if use_cuda else None,
"device_name": torch.cuda.get_device_name(0) if use_cuda else "cpu",
}
def main():
tokenizer = None
model = None
eos_ids: List[int] = []
active_pipeline = None
runtime = resolve_torch_runtime()
log(
f"Using local model runtime: device={runtime['device']} "
f"name={runtime['device_name']} dtype={runtime['dtype']}"
)
if USE_PIPELINE:
log(f"Loading pipeline ({PIPELINE_TASK}) for {MODEL_ID} ...")
try:
active_pipeline = pipeline(
PIPELINE_TASK,
model=MODEL_ID,
trust_remote_code=True,
dtype=runtime["dtype"],
device=0 if runtime["use_cuda"] else -1,
)
log("Pipeline loaded.")
except Exception as exc:
log(f"Pipeline load failed ({exc}), falling back to tokenizer+model.")
if active_pipeline is None:
log(f"Loading tokenizer for {MODEL_ID} ...")
tokenizer = AutoTokenizer.from_pretrained(
MODEL_ID, trust_remote_code=True,
)
log("Tokenizer loaded. Loading model (this may download files on first run) ...")
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
dtype=runtime["dtype"],
device_map=runtime["device_map"],
trust_remote_code=True,
)
log(f"Model loaded. Device: {model.device}")
if tokenizer.eos_token_id is not None:
eos_ids.append(tokenizer.eos_token_id)
extra = tokenizer.convert_tokens_to_ids(["<|im_end|>", "<|endoftext|>"])
for tid in extra:
if isinstance(tid, int) and tid not in eos_ids:
eos_ids.append(tid)
log(f"EOS token ids: {eos_ids}")
def check_dashboard_command() -> Optional[Dict[str, Any]]:
"""Read and consume a command file written by the dashboard."""
try:
raw = DASHBOARD_CMD_PATH.read_text(encoding="utf-8")
try:
DASHBOARD_CMD_PATH.unlink(missing_ok=True)
except OSError:
# Windows: file may be locked by dashboard; still consumed
pass
return json.loads(raw)
except (FileNotFoundError, json.JSONDecodeError):
return None
def run_episode(
scenario_name: Optional[str] = None,
custom_ground_truth: Optional[Dict[str, Any]] = None,
):
env = BioExperimentEnvironment(scenario_name=scenario_name)
obs = env.reset()
if custom_ground_truth and env._latent:
gt = custom_ground_truth
bio = env._latent.biology
if gt.get("true_markers"):
bio.true_markers = gt["true_markers"]
if gt.get("causal_mechanisms"):
bio.causal_mechanisms = gt["causal_mechanisms"]
if gt.get("true_pathways"):
bio.true_pathways = {
k: float(v) for k, v in gt["true_pathways"].items()
}
log("\n" + "=" * 70)
log(f"TASK: {obs.task.problem_statement}")
log(f"Conditions: {obs.task.conditions}")
log(f"Budget: ${obs.task.budget_limit:,.0f} | Time: {obs.task.time_limit_days:.0f} days")
if ENABLE_THINKING:
log("Reasoning mode: ENABLED")
log("=" * 70)
cumulative_reward = 0.0
write_dashboard_state(env, obs, step=0, cumulative_reward=0.0)
for step in range(MAX_EPISODE_STEPS):
cmd = check_dashboard_command()
if cmd and cmd.get("action") == "restart":
log("\n[DASHBOARD] Restart requested — ending episode early.")
break
user_msg = build_observation_prompt(obs)
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_msg},
]
if active_pipeline is not None:
prompt = f"{SYSTEM_PROMPT}\n\n{user_msg}"
else:
try:
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=ENABLE_THINKING,
)
except TypeError:
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
t0 = time.time()
if active_pipeline is not None:
response = run_with_pipeline(active_pipeline, prompt)
if not response:
response = format_observation(obs)
else:
assert tokenizer is not None and model is not None
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
n_input = inputs["input_ids"].shape[1]
max_new = 2048 if ENABLE_THINKING else 300
with torch.no_grad():
output_ids = model.generate(
**inputs,
max_new_tokens=max_new,
do_sample=True,
temperature=0.7,
top_p=0.8,
top_k=20,
repetition_penalty=1.3,
eos_token_id=eos_ids if eos_ids else None,
)
new_tokens = output_ids[0][n_input:]
response = tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
gen_time = time.time() - t0
thinking = ""
if ENABLE_THINKING:
think_match = re.search(
r"<think>(.*?)</think>", response, re.DOTALL
)
if think_match:
thinking = think_match.group(1).strip()
response = response[think_match.end():].strip()
elif response.startswith("<think>"):
parts = response.split("</think>", 1)
if len(parts) == 2:
thinking = parts[0].replace("<think>", "").strip()
response = parts[1].strip()
is_last_step = (step == MAX_EPISODE_STEPS - 1)
action = parse_action(response)
if action is None:
if is_last_step:
log(f"\n [!] Parse failed on final step — forcing synthesize_conclusion.")
action = ExperimentAction(
action_type=ActionType.SYNTHESIZE_CONCLUSION,
justification="forced terminal conclusion",
confidence=0.5,
)
else:
log(f"\n [!] Parse failed, skipping step. Raw: {response[:150]}")
continue
completed_types = {
r.action_type for r in obs.pipeline_history if r.success
}
failed_types = {
r.action_type
for r in obs.pipeline_history
if not r.success
}
if should_force_terminal_conclusion(action, completed_types):
log(
f"\n [!] repeated completed meta step {action.action_type.value} "
f"— forcing synthesize_conclusion."
)
action = ExperimentAction(
action_type=ActionType.SYNTHESIZE_CONCLUSION,
justification="repeated completed meta step forced terminal conclusion",
confidence=action.confidence,
)
completed_types = {
r.action_type for r in obs.pipeline_history if r.success
}
skip_reason = None
if action.action_type in completed_types:
skip_reason = (
f"blocked repeat of completed step {action.action_type.value}"
)
elif action.action_type in failed_types:
if should_block_failed_reattempt(
obs.pipeline_history, action.action_type
):
skip_reason = (
f"blocked re-attempt of failed step {action.action_type.value}"
)
if skip_reason:
if is_last_step:
log(f"\n [!] {skip_reason} on final step — forcing synthesize_conclusion.")
action = ExperimentAction(
action_type=ActionType.SYNTHESIZE_CONCLUSION,
justification="forced terminal conclusion",
confidence=0.5,
)
else:
log(f"\n [!] {skip_reason}, skipping step.")
continue
if is_last_step and action.action_type != ActionType.SYNTHESIZE_CONCLUSION:
log(f"\n [!] Final step — overriding {action.action_type.value} with synthesize_conclusion.")
action = ExperimentAction(
action_type=ActionType.SYNTHESIZE_CONCLUSION,
justification="forced terminal conclusion",
confidence=action.confidence,
)
action = ensure_conclusion_claims(obs, action)
log(f"\nStep {step + 1}: {action.action_type.value} ({gen_time:.1f}s)")
if thinking:
log(f" Thinking: {thinking[:200]}")
if action.justification:
log(f" Rationale: {action.justification}")
else:
log(" Rationale: [model did not provide one]")
if action.parameters:
log(f" Parameters: {compact_preview(action.parameters, 200)}")
elif not action.justification and response:
log(
f" Model response: "
f"{compact_preview(response, MODEL_RESPONSE_PREVIEW_CHARS)}"
)
obs = env.step(action)
if obs.latest_output:
lo = obs.latest_output
status = "OK" if lo.success else "FAIL"
log(f" [{status}] {lo.summary}")
if lo.warnings:
log(f" Warnings: {lo.warnings}")
step_reward = obs.reward
cumulative_reward += step_reward
log(f" Reward: {step_reward:+.3f} (cum: {cumulative_reward:+.3f})")
log(f" Budget: ${obs.resource_usage.budget_remaining:,.0f} | Time: {obs.resource_usage.time_remaining_days:.0f}d")
write_dashboard_state(
env, obs,
step=step + 1,
cumulative_reward=cumulative_reward,
model_response=response,
model_thinking=thinking,
action=action,
gen_time=gen_time,
episode_done=obs.done,
)
if obs.rule_violations:
log(f" Violations: {obs.rule_violations}")
if obs.done:
break
log(f"\n{'=' * 70}")
log("EPISODE COMPLETE" if obs.done else f"MAX STEPS ({MAX_EPISODE_STEPS})")
log(f" Steps: {obs.step_index}")
log(f" Total reward: {cumulative_reward:+.3f}")
log(f" Budget used: ${obs.resource_usage.budget_used:,.0f}")
log(f" Time used: {obs.resource_usage.time_used_days:.0f} days")
if obs.conclusions:
log(" Conclusions:")
for c in obs.conclusions:
log(f" [{c.claim_type}, conf={c.confidence:.2f}] {c.claim}")
if c.top_markers:
log(f" Markers: {c.top_markers}")
if c.causal_mechanisms:
log(f" Mechanisms: {c.causal_mechanisms}")
if c.predicted_pathways:
log(f" Pathways: {c.predicted_pathways}")
log("=" * 70)
try:
DASHBOARD_CMD_PATH.unlink(missing_ok=True)
except OSError:
pass
run_episode()
while True:
log("\nWaiting for dashboard command (restart / new task) ...")
while True:
cmd = check_dashboard_command()
if cmd:
break
time.sleep(1.0)
action_type = cmd.get("action", "restart")
if action_type == "quit":
log("Quit requested.")
break
scenario = cmd.get("scenario_name")
ground_truth = cmd.get("ground_truth")
log(f"\n[DASHBOARD] {action_type} — scenario={scenario}")
run_episode(scenario_name=scenario, custom_ground_truth=ground_truth)
if __name__ == "__main__":
main()
|