File size: 66,525 Bytes
9a70a84 | 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 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 | """Evidence-bound reproduction, triage, disclosure, and patch workflows."""
from __future__ import annotations
import hashlib
import hmac
import json
import os
import platform
import re
import secrets
import sys
import time
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any, Callable, Mapping, Sequence
from .artifacts import ArtifactStore
from .contracts import (
ToolCall,
ToolExecutionContext,
ToolExecutionResult,
ToolParameter,
ToolSpec,
)
from .repository import atomic_patch_text, atomic_write_text
from .sandbox import control_root, ensure_control_root
from .security import SecretRedactor
from .transactions import TransactionStore
RunCommand = Callable[
[str, str, float], tuple[bool, str, str, int | None]
]
_ID_RE = re.compile(r"(?:rep|tri|dis|patch|evb)_[a-f0-9]{32}")
_SHA256_RE = re.compile(r"[a-f0-9]{64}")
_SEVERITIES = ("informational", "low", "moderate", "high", "critical")
_AUDIENCES = ("maintainer", "operator", "coordinated", "public")
ENGINEERING_TOOL_SPECS: tuple[ToolSpec, ...] = (
ToolSpec(
"ReproductionRun",
"engineering",
"Run one contained reproduction attempt and persist exact environment, state, output, and exit evidence.",
"ReproductionRun(command='python -m pytest tests/test_api.py', snapshot_paths=['src', 'tests'])",
(
ToolParameter("command", "string", "Exact contained command to execute."),
ToolParameter(
"working_directory",
"string",
"Optional workspace-relative working directory.",
required=False,
),
ToolParameter(
"snapshot_paths",
"array",
"Optional workspace paths whose content state is captured before and after execution.",
required=False,
),
),
risk="workspace_write",
parallel_safe=False,
idempotent=False,
task_support="optional",
),
ToolSpec(
"ReproductionCompare",
"engineering",
"Compare any number of session reproduction receipts for stable outcomes and state drift.",
"ReproductionCompare(reproduction_ids=['rep_...', 'rep_...'])",
(
ToolParameter(
"reproduction_ids", "array", "Reproduction receipt identifiers."
),
),
),
ToolSpec(
"TriageCreate",
"engineering",
"Create evidence-linked triage that keeps observed facts separate from hypotheses and next actions.",
"TriageCreate(reproduction_ids=['rep_...'], observed_facts=['exit changed'], hypotheses=['configuration drift'], severity='moderate', confidence=0.7, next_actions=['inspect configuration'])",
(
ToolParameter(
"reproduction_ids", "array", "Cited reproduction receipt identifiers."
),
ToolParameter("observed_facts", "array", "Evidence-supported facts."),
ToolParameter("hypotheses", "array", "Unconfirmed explanations."),
ToolParameter(
"severity",
"string",
"Current impact classification.",
enum=_SEVERITIES,
),
ToolParameter("confidence", "number", "Confidence from zero to one."),
ToolParameter("next_actions", "array", "Model-selected investigation actions."),
),
risk="workspace_write",
parallel_safe=False,
idempotent=False,
),
ToolSpec(
"TriageStatus",
"engineering",
"Read one session-scoped triage receipt with its bound reproduction evidence and drift state.",
"TriageStatus(triage_id='tri_...')",
(ToolParameter("triage_id", "string", "Triage receipt identifier."),),
),
ToolSpec(
"TriageUpdate",
"engineering",
"Append new evidence, facts, hypotheses, and next actions to an existing triage receipt.",
"TriageUpdate(triage_id='tri_...', reproduction_ids=['rep_...'], observed_facts=['new fact'])",
(
ToolParameter("triage_id", "string", "Triage receipt identifier."),
ToolParameter(
"reproduction_ids",
"array",
"Additional reproduction receipt identifiers.",
required=False,
),
ToolParameter(
"observed_facts",
"array",
"Additional evidence-supported facts.",
required=False,
),
ToolParameter(
"hypotheses",
"array",
"Additional unconfirmed explanations.",
required=False,
),
ToolParameter(
"severity",
"string",
"Updated impact classification.",
required=False,
enum=_SEVERITIES,
),
ToolParameter(
"confidence",
"number",
"Updated confidence from zero to one.",
required=False,
),
ToolParameter(
"next_actions",
"array",
"Additional model-selected investigation actions.",
required=False,
),
),
risk="workspace_write",
parallel_safe=False,
idempotent=False,
),
ToolSpec(
"DisclosureCreate",
"engineering",
"Create a sanitized disclosure artifact from cited triage and optional verified patch evidence.",
"DisclosureCreate(triage_id='tri_...', title='Issue title', summary='Summary', impact='Impact', remediation='Resolution')",
(
ToolParameter("triage_id", "string", "Cited triage receipt identifier."),
ToolParameter("title", "string", "Disclosure title."),
ToolParameter("summary", "string", "Evidence-grounded summary."),
ToolParameter("impact", "string", "Observed or bounded impact."),
ToolParameter("remediation", "string", "Repair and verification guidance."),
ToolParameter(
"audience",
"string",
"Intended disclosure audience.",
required=False,
enum=_AUDIENCES,
),
ToolParameter(
"patch_id",
"string",
"Optional patch receipt to include by digest and verification state.",
required=False,
),
),
risk="workspace_write",
parallel_safe=False,
idempotent=False,
),
ToolSpec(
"DisclosureStatus",
"engineering",
"Read one session-scoped disclosure receipt and its sanitized artifact reference.",
"DisclosureStatus(disclosure_id='dis_...')",
(ToolParameter("disclosure_id", "string", "Disclosure receipt identifier."),),
),
ToolSpec(
"EvidenceBundleCreate",
"engineering",
"Create a sanitized handoff artifact from cited engineering receipts without raw private output.",
"EvidenceBundleCreate(title='Repair handoff', triage_ids=['tri_...'], patch_ids=['patch_...'])",
(
ToolParameter(
"title",
"string",
"Bundle title shown in the sanitized handoff artifact.",
),
ToolParameter(
"reproduction_ids",
"array",
"Optional reproduction receipt identifiers.",
required=False,
),
ToolParameter(
"triage_ids",
"array",
"Optional triage receipt identifiers.",
required=False,
),
ToolParameter(
"disclosure_ids",
"array",
"Optional disclosure receipt identifiers.",
required=False,
),
ToolParameter(
"patch_ids",
"array",
"Optional patch receipt identifiers.",
required=False,
),
),
risk="workspace_write",
parallel_safe=False,
idempotent=False,
),
ToolSpec(
"EvidenceBundleStatus",
"engineering",
"Read one session-scoped sanitized evidence bundle receipt.",
"EvidenceBundleStatus(bundle_id='evb_...')",
(ToolParameter("bundle_id", "string", "Evidence bundle receipt identifier."),),
),
ToolSpec(
"PatchBegin",
"engineering",
"Begin an evidence-linked multi-file patch transaction with immutable original snapshots.",
"PatchBegin(triage_id='tri_...', paths=['src/app.py', 'tests/test_app.py'])",
(
ToolParameter("triage_id", "string", "Cited triage receipt identifier."),
ToolParameter("paths", "array", "Exact workspace files covered by the patch."),
),
risk="workspace_write",
parallel_safe=False,
idempotent=False,
),
ToolSpec(
"PatchApply",
"engineering",
"Apply an atomic compare-and-swap patch set and restore every covered file if any change fails.",
"PatchApply(patch_id='patch_...', changes=[{'operation':'replace','path':'src/app.py','expected_sha256':'...','old_text':'before','new_text':'after'}])",
(
ToolParameter("patch_id", "string", "Patch receipt identifier."),
ToolParameter(
"changes",
"array",
"Changes using replace or write operations with exact current digests.",
),
),
risk="workspace_write",
parallel_safe=False,
idempotent=False,
),
ToolSpec(
"PatchVerify",
"engineering",
"Run contained model-selected verification and bind its reproduction receipt to the patch.",
"PatchVerify(patch_id='patch_...', command='python -m pytest tests/test_app.py')",
(
ToolParameter("patch_id", "string", "Patch receipt identifier."),
ToolParameter("command", "string", "Exact verification command."),
ToolParameter(
"working_directory",
"string",
"Optional workspace-relative working directory.",
required=False,
),
),
risk="workspace_write",
parallel_safe=False,
idempotent=False,
task_support="optional",
),
ToolSpec(
"PatchCommit",
"engineering",
"Commit only a successfully verified patch whose covered files still match the verified state.",
"PatchCommit(patch_id='patch_...')",
(ToolParameter("patch_id", "string", "Patch receipt identifier."),),
risk="workspace_write",
parallel_safe=False,
),
ToolSpec(
"PatchRollback",
"engineering",
"Restore the original patch snapshots only when every covered file still matches current receipts, then bind a restoration digest proof.",
"PatchRollback(patch_id='patch_...')",
(ToolParameter("patch_id", "string", "Patch receipt identifier."),),
risk="destructive",
parallel_safe=False,
idempotent=True,
),
ToolSpec(
"PatchStatus",
"engineering",
"Read patch mutations, verification receipts, transaction state, and rollback availability.",
"PatchStatus(patch_id='patch_...')",
(ToolParameter("patch_id", "string", "Patch receipt identifier."),),
),
)
ENGINEERING_TOOL_NAMES = frozenset(spec.name for spec in ENGINEERING_TOOL_SPECS)
_ENGINEERING_TOOL_BY_NAME = {spec.name: spec for spec in ENGINEERING_TOOL_SPECS}
@dataclass(frozen=True)
class FileState:
path: str
sha256: str
bytes: int
@dataclass(frozen=True)
class ReproductionRecord:
reproduction_id: str
session_sha256: str
command_sha256: str
working_directory: str
environment_sha256: str
before_state_sha256: str
after_state_sha256: str
before_files: tuple[FileState, ...]
after_files: tuple[FileState, ...]
stdout_artifact_id: str
stderr_artifact_id: str
stdout_sha256: str
stderr_sha256: str
exit_code: int | None
command_ok: bool
status: str
created_unix_ms: int
finished_unix_ms: int
def to_dict(self) -> dict[str, Any]:
return asdict(self)
@dataclass(frozen=True)
class TriageRecord:
triage_id: str
session_sha256: str
reproduction_ids: tuple[str, ...]
observed_facts: tuple[str, ...]
hypotheses: tuple[str, ...]
severity: str
confidence: float
next_actions: tuple[str, ...]
evidence_sha256: str
created_unix_ms: int
def to_dict(self) -> dict[str, Any]:
return asdict(self)
@dataclass(frozen=True)
class DisclosureRecord:
disclosure_id: str
session_sha256: str
triage_id: str
patch_id: str
audience: str
evidence_sha256: str
artifact_id: str
artifact_sha256: str
created_unix_ms: int
def to_dict(self) -> dict[str, Any]:
return asdict(self)
@dataclass(frozen=True)
class EvidenceBundleRecord:
bundle_id: str
session_sha256: str
title: str
reproduction_ids: tuple[str, ...]
triage_ids: tuple[str, ...]
disclosure_ids: tuple[str, ...]
patch_ids: tuple[str, ...]
evidence_sha256: str
artifact_id: str
artifact_sha256: str
created_unix_ms: int
def to_dict(self) -> dict[str, Any]:
return asdict(self)
@dataclass(frozen=True)
class PatchMutation:
operation: str
path: str
before_sha256: str
after_sha256: str
bytes: int
created: bool
@dataclass(frozen=True)
class PatchRecord:
patch_id: str
session_sha256: str
triage_id: str
transaction_id: str
paths: tuple[str, ...]
status: str
mutations: tuple[PatchMutation, ...]
verification_ids: tuple[str, ...]
verified_state_sha256: str
last_error: str
created_unix_ms: int
updated_unix_ms: int
restoration_sha256: str = ""
def to_dict(self) -> dict[str, Any]:
return asdict(self)
def _atomic_json(path: Path, payload: Mapping[str, Any]) -> None:
path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
os.chmod(path.parent, 0o700)
temporary = path.with_name(f".{path.name}.{secrets.token_hex(8)}.tmp")
temporary.write_text(
json.dumps(payload, sort_keys=True, separators=(",", ":")),
encoding="utf-8",
)
temporary.chmod(0o600)
with temporary.open("rb") as handle:
os.fsync(handle.fileno())
os.replace(temporary, path)
directory_fd = os.open(path.parent, os.O_RDONLY)
try:
os.fsync(directory_fd)
finally:
os.close(directory_fd)
def _session_sha256(session_id: str) -> str:
return hashlib.sha256((session_id or "direct").encode("utf-8")).hexdigest()
def _require_session(session_sha256: str, session_id: str) -> None:
if not hmac.compare_digest(session_sha256, _session_sha256(session_id)):
raise PermissionError("engineering receipt does not belong to this session")
def _record_path(root: Path, identifier: str, prefix: str) -> Path:
if not _ID_RE.fullmatch(identifier) or not identifier.startswith(prefix + "_"):
raise ValueError(f"{prefix} receipt id is invalid")
return root / f"{identifier}.json"
def _append_unique(existing: tuple[str, ...], additions: tuple[str, ...]) -> tuple[str, ...]:
rows = list(existing)
seen = set(rows)
for item in additions:
if item not in seen:
rows.append(item)
seen.add(item)
return tuple(rows)
def _load_payload(path: Path) -> dict[str, Any]:
payload = json.loads(path.read_text(encoding="utf-8"))
if not isinstance(payload, dict):
raise RuntimeError("engineering receipt is invalid")
return payload
def _string_tuple(value: object, name: str, *, allow_empty: bool = True) -> tuple[str, ...]:
if not isinstance(value, list) or any(not isinstance(item, str) for item in value):
raise ValueError(f"{name} must be an array of strings")
rendered = tuple(item.strip() for item in value)
if any(not item for item in rendered) or (not allow_empty and not rendered):
raise ValueError(f"{name} must contain non-empty strings")
return rendered
def _contained_path(
workspace: Path, raw_path: str, *, require_directory: bool = False
) -> Path:
raw = raw_path.strip() or "."
candidate = (workspace / raw).resolve()
try:
relative = candidate.relative_to(workspace)
except ValueError as exc:
raise ValueError("engineering path leaves the workspace") from exc
if relative.parts and relative.parts[0] == ".nexum":
raise ValueError("workspace control paths require dedicated runtime tools")
if require_directory and not candidate.is_dir():
raise ValueError("working directory does not exist")
return candidate
def _sha256_file(path: Path) -> tuple[str, int]:
digest = hashlib.sha256()
size = 0
with path.open("rb") as handle:
while chunk := handle.read(1024 * 1024):
digest.update(chunk)
size += len(chunk)
return digest.hexdigest(), size
def _snapshot(workspace: Path, raw_paths: tuple[str, ...]) -> tuple[FileState, ...]:
states: list[FileState] = []
seen: set[str] = set()
for raw_path in raw_paths:
target = _contained_path(workspace, raw_path)
if not target.exists():
relative = target.relative_to(workspace).as_posix()
if relative not in seen:
states.append(FileState(relative, "missing", 0))
seen.add(relative)
continue
candidates = (target,) if target.is_file() else tuple(sorted(target.rglob("*")))
for candidate in candidates:
if candidate.is_symlink():
raise ValueError("snapshot paths cannot contain symbolic links")
if not candidate.is_file():
continue
relative = candidate.resolve().relative_to(workspace).as_posix()
if relative.startswith(".nexum/") or relative.startswith(".git/"):
continue
if "/.git/" in f"/{relative}/" or relative in seen:
continue
digest, size = _sha256_file(candidate)
states.append(FileState(relative, digest, size))
seen.add(relative)
return tuple(sorted(states, key=lambda state: state.path))
def _state_sha256(states: tuple[FileState, ...]) -> str:
encoded = json.dumps(
[asdict(state) for state in states],
sort_keys=True,
separators=(",", ":"),
).encode("utf-8")
return hashlib.sha256(encoded).hexdigest()
def _sanitize(text: str, workspace: Path) -> str:
return SecretRedactor().redact(text).replace(str(workspace), "[WORKSPACE]")
class ReproductionStore:
def __init__(self, workspace: str | Path) -> None:
self.workspace = ensure_control_root(workspace)
self.root = control_root(self.workspace) / "engineering" / "reproductions"
self.root.mkdir(parents=True, exist_ok=True, mode=0o700)
os.chmod(self.root, 0o700)
self.artifacts = ArtifactStore(self.workspace)
def run(
self,
*,
command: str,
working_directory: str,
snapshot_paths: tuple[str, ...],
timeout_s: float,
session_id: str,
run_command: RunCommand,
) -> tuple[ReproductionRecord, str, str]:
if not command.strip():
raise ValueError("reproduction command is required")
workdir = _contained_path(
self.workspace, working_directory, require_directory=True
)
before = _snapshot(self.workspace, snapshot_paths)
relative_workdir = workdir.relative_to(self.workspace).as_posix() or "."
environment = {
"architecture": platform.machine(),
"os_name": platform.system(),
"os_release": platform.release(),
"python": ".".join(str(part) for part in sys.version_info[:3]),
"working_directory": relative_workdir,
}
environment_sha256 = hashlib.sha256(
json.dumps(environment, sort_keys=True, separators=(",", ":")).encode(
"utf-8"
)
).hexdigest()
started = int(time.time() * 1000)
command_ok, stdout, stderr, exit_code = run_command(
command, str(workdir), timeout_s
)
finished = int(time.time() * 1000)
safe_stdout = _sanitize(stdout, self.workspace)
safe_stderr = _sanitize(stderr, self.workspace)
after = _snapshot(self.workspace, snapshot_paths)
stdout_artifact = self.artifacts.put_text(
safe_stdout,
source="reproduction_stdout",
session_id=session_id or "direct",
)
stderr_artifact = self.artifacts.put_text(
safe_stderr,
source="reproduction_stderr",
session_id=session_id or "direct",
)
record = ReproductionRecord(
reproduction_id="rep_" + secrets.token_hex(16),
session_sha256=_session_sha256(session_id),
command_sha256=hashlib.sha256(command.encode("utf-8")).hexdigest(),
working_directory=relative_workdir,
environment_sha256=environment_sha256,
before_state_sha256=_state_sha256(before),
after_state_sha256=_state_sha256(after),
before_files=before,
after_files=after,
stdout_artifact_id=stdout_artifact.artifact_id,
stderr_artifact_id=stderr_artifact.artifact_id,
stdout_sha256=hashlib.sha256(safe_stdout.encode("utf-8")).hexdigest(),
stderr_sha256=hashlib.sha256(safe_stderr.encode("utf-8")).hexdigest(),
exit_code=exit_code,
command_ok=command_ok,
status="exited" if exit_code is not None else "failed_to_start",
created_unix_ms=started,
finished_unix_ms=finished,
)
_atomic_json(
_record_path(self.root, record.reproduction_id, "rep"),
record.to_dict(),
)
return record, safe_stdout, safe_stderr
def get(self, reproduction_id: str, *, session_id: str) -> ReproductionRecord:
payload = _load_payload(_record_path(self.root, reproduction_id, "rep"))
before = tuple(FileState(**row) for row in payload.pop("before_files", []))
after = tuple(FileState(**row) for row in payload.pop("after_files", []))
record = ReproductionRecord(
**payload,
before_files=before,
after_files=after,
)
_require_session(record.session_sha256, session_id)
return record
def compare(
self, reproduction_ids: tuple[str, ...], *, session_id: str
) -> dict[str, Any]:
if not reproduction_ids or len(reproduction_ids) != len(set(reproduction_ids)):
raise ValueError("reproduction ids must be non-empty and unique")
records = tuple(
self.get(identifier, session_id=session_id)
for identifier in reproduction_ids
)
def same(name: str) -> bool:
return len({getattr(record, name) for record in records}) == 1
return {
"reproduction_ids": list(reproduction_ids),
"same_command": same("command_sha256"),
"same_environment": same("environment_sha256"),
"same_exit_code": same("exit_code"),
"same_stdout": same("stdout_sha256"),
"same_stderr": same("stderr_sha256"),
"same_before_state": same("before_state_sha256"),
"same_after_state": same("after_state_sha256"),
"stable_outcome": all(
same(name)
for name in ("exit_code", "stdout_sha256", "stderr_sha256")
),
"state_drift_observed": not same("after_state_sha256"),
}
class TriageStore:
def __init__(self, workspace: str | Path) -> None:
self.workspace = ensure_control_root(workspace)
self.root = control_root(self.workspace) / "engineering" / "triage"
self.root.mkdir(parents=True, exist_ok=True, mode=0o700)
os.chmod(self.root, 0o700)
self.reproductions = ReproductionStore(self.workspace)
def create(
self,
*,
reproduction_ids: tuple[str, ...],
observed_facts: tuple[str, ...],
hypotheses: tuple[str, ...],
severity: str,
confidence: float,
next_actions: tuple[str, ...],
session_id: str,
) -> TriageRecord:
if not reproduction_ids or len(reproduction_ids) != len(set(reproduction_ids)):
raise ValueError("triage requires unique reproduction evidence")
if not observed_facts:
raise ValueError("triage requires at least one observed fact")
if severity not in _SEVERITIES:
raise ValueError("triage severity is invalid")
if confidence < 0.0 or confidence > 1.0:
raise ValueError("triage confidence must be between zero and one")
records = tuple(
self.reproductions.get(identifier, session_id=session_id)
for identifier in reproduction_ids
)
evidence = [
{
"id": record.reproduction_id,
"command": record.command_sha256,
"environment": record.environment_sha256,
"exit_code": record.exit_code,
"stdout": record.stdout_sha256,
"stderr": record.stderr_sha256,
"before": record.before_state_sha256,
"after": record.after_state_sha256,
}
for record in records
]
evidence_sha256 = hashlib.sha256(
json.dumps(evidence, sort_keys=True, separators=(",", ":")).encode(
"utf-8"
)
).hexdigest()
def sanitize(value: str) -> str:
return _sanitize(value, self.workspace)
record = TriageRecord(
triage_id="tri_" + secrets.token_hex(16),
session_sha256=_session_sha256(session_id),
reproduction_ids=reproduction_ids,
observed_facts=tuple(sanitize(item) for item in observed_facts),
hypotheses=tuple(sanitize(item) for item in hypotheses),
severity=severity,
confidence=confidence,
next_actions=tuple(sanitize(item) for item in next_actions),
evidence_sha256=evidence_sha256,
created_unix_ms=int(time.time() * 1000),
)
_atomic_json(_record_path(self.root, record.triage_id, "tri"), record.to_dict())
return record
def get(self, triage_id: str, *, session_id: str) -> TriageRecord:
payload = _load_payload(_record_path(self.root, triage_id, "tri"))
record = TriageRecord(
**{
**payload,
"reproduction_ids": tuple(payload.get("reproduction_ids", [])),
"observed_facts": tuple(payload.get("observed_facts", [])),
"hypotheses": tuple(payload.get("hypotheses", [])),
"next_actions": tuple(payload.get("next_actions", [])),
}
)
_require_session(record.session_sha256, session_id)
return record
def status(self, triage_id: str, *, session_id: str) -> dict[str, Any]:
record = self.get(triage_id, session_id=session_id)
evidence: list[dict[str, Any]] = []
for identifier in record.reproduction_ids:
reproduction = self.reproductions.get(
identifier, session_id=session_id
)
evidence.append(
{
"id": reproduction.reproduction_id,
"exit_code": reproduction.exit_code,
"command_ok": reproduction.command_ok,
"status": reproduction.status,
"state_drift_observed": reproduction.before_state_sha256
!= reproduction.after_state_sha256,
"stdout": reproduction.stdout_sha256,
"stderr": reproduction.stderr_sha256,
}
)
return {
**record.to_dict(),
"evidence": evidence,
"state_drift_observed": any(
item["state_drift_observed"] for item in evidence
),
}
def update(
self,
triage_id: str,
*,
reproduction_ids: tuple[str, ...],
observed_facts: tuple[str, ...],
hypotheses: tuple[str, ...],
severity: str,
confidence: float,
next_actions: tuple[str, ...],
session_id: str,
) -> TriageRecord:
record = self.get(triage_id, session_id=session_id)
if severity not in _SEVERITIES:
raise ValueError("triage severity is invalid")
if confidence < 0.0 or confidence > 1.0:
raise ValueError("triage confidence must be between zero and one")
merged_reproductions = _append_unique(record.reproduction_ids, reproduction_ids)
if not merged_reproductions:
raise ValueError("triage requires reproduction evidence")
records = tuple(
self.reproductions.get(identifier, session_id=session_id)
for identifier in merged_reproductions
)
evidence = [
{
"id": row.reproduction_id,
"command": row.command_sha256,
"environment": row.environment_sha256,
"exit_code": row.exit_code,
"stdout": row.stdout_sha256,
"stderr": row.stderr_sha256,
"before": row.before_state_sha256,
"after": row.after_state_sha256,
}
for row in records
]
updated = TriageRecord(
triage_id=record.triage_id,
session_sha256=record.session_sha256,
reproduction_ids=merged_reproductions,
observed_facts=_append_unique(
record.observed_facts,
tuple(_sanitize(item, self.workspace) for item in observed_facts),
),
hypotheses=_append_unique(
record.hypotheses,
tuple(_sanitize(item, self.workspace) for item in hypotheses),
),
severity=severity,
confidence=confidence,
next_actions=_append_unique(
record.next_actions,
tuple(_sanitize(item, self.workspace) for item in next_actions),
),
evidence_sha256=hashlib.sha256(
json.dumps(evidence, sort_keys=True, separators=(",", ":")).encode(
"utf-8"
)
).hexdigest(),
created_unix_ms=record.created_unix_ms,
)
_atomic_json(_record_path(self.root, updated.triage_id, "tri"), updated.to_dict())
return updated
class PatchStore:
def __init__(self, workspace: str | Path) -> None:
self.workspace = ensure_control_root(workspace)
self.root = control_root(self.workspace) / "engineering" / "patches"
self.root.mkdir(parents=True, exist_ok=True, mode=0o700)
os.chmod(self.root, 0o700)
self.triage = TriageStore(self.workspace)
self.reproductions = ReproductionStore(self.workspace)
self.transactions = TransactionStore(self.workspace)
def _write(self, record: PatchRecord) -> None:
_atomic_json(_record_path(self.root, record.patch_id, "patch"), record.to_dict())
def get(self, patch_id: str, *, session_id: str) -> PatchRecord:
payload = _load_payload(_record_path(self.root, patch_id, "patch"))
mutations = tuple(PatchMutation(**row) for row in payload.pop("mutations", []))
record = PatchRecord(
**{
**payload,
"paths": tuple(payload.get("paths", [])),
"verification_ids": tuple(payload.get("verification_ids", [])),
"mutations": mutations,
}
)
_require_session(record.session_sha256, session_id)
return record
def begin(
self, *, triage_id: str, paths: tuple[str, ...], session_id: str
) -> PatchRecord:
self.triage.get(triage_id, session_id=session_id)
if not paths or len(paths) != len(set(paths)):
raise ValueError("patch paths must be non-empty and unique")
transaction = self.transactions.begin(paths, session_id=session_id)
now = int(time.time() * 1000)
record = PatchRecord(
patch_id="patch_" + secrets.token_hex(16),
session_sha256=_session_sha256(session_id),
triage_id=triage_id,
transaction_id=transaction.transaction_id,
paths=tuple(entry.path for entry in transaction.entries),
status="open",
mutations=(),
verification_ids=(),
verified_state_sha256="",
last_error="",
created_unix_ms=now,
updated_unix_ms=now,
)
self._write(record)
return record
def _current_digests(self, paths: tuple[str, ...]) -> dict[str, str]:
current: dict[str, str] = {}
for raw_path in paths:
target = _contained_path(self.workspace, raw_path)
current[raw_path] = (
_sha256_file(target)[0] if target.is_file() else "missing"
)
return current
def _covered_state(self, paths: tuple[str, ...]) -> str:
states = tuple(
FileState(path, digest, 0)
for path, digest in sorted(self._current_digests(paths).items())
)
return _state_sha256(states)
def apply(
self,
patch_id: str,
changes: Sequence[Mapping[str, object]],
*,
session_id: str,
) -> tuple[PatchRecord, str]:
record = self.get(patch_id, session_id=session_id)
if record.status not in {"open", "applied", "verification_failed"}:
raise RuntimeError(f"patch is already {record.status}")
if not changes:
raise ValueError("patch changes must not be empty")
changed_paths: set[str] = set()
mutations: list[PatchMutation] = list(record.mutations)
error = ""
try:
for change in changes:
if not isinstance(change, Mapping):
raise ValueError("each patch change must be an object")
operation = str(change.get("operation") or "")
path = str(change.get("path") or "")
if path not in record.paths:
raise ValueError("patch change path is not covered by the transaction")
if path in changed_paths:
raise ValueError("each path may appear once per patch application")
changed_paths.add(path)
expected = str(change.get("expected_sha256") or "")
if expected and not _SHA256_RE.fullmatch(expected):
raise ValueError("expected_sha256 is invalid")
if operation == "replace":
if not expected:
raise ValueError("replace changes require expected_sha256")
result = atomic_patch_text(
self.workspace,
path,
expected_sha256=expected,
old_text=str(change.get("old_text") or ""),
new_text=str(change.get("new_text") or ""),
)
elif operation == "write":
result = atomic_write_text(
self.workspace,
path,
str(change.get("content") or ""),
expected_sha256=expected or None,
)
else:
raise ValueError("patch operation must be replace or write")
mutations.append(
PatchMutation(
operation=operation,
path=result.path,
before_sha256=result.before_sha256 or "missing",
after_sha256=result.after_sha256,
bytes=result.bytes,
created=result.created,
)
)
except (OSError, RuntimeError, UnicodeError, ValueError) as exc:
error = _sanitize(f"{type(exc).__name__}: {exc}", self.workspace)
status = "rolled_back"
try:
self.transactions.rollback(
record.transaction_id,
self._current_digests(record.paths),
session_id=session_id,
)
except (OSError, RuntimeError, ValueError) as rollback_exc:
status = "rollback_failed"
error += "; rollback: " + _sanitize(
f"{type(rollback_exc).__name__}: {rollback_exc}",
self.workspace,
)
failed = PatchRecord(
**{
**record.to_dict(),
"status": status,
"mutations": tuple(mutations),
"last_error": error,
"updated_unix_ms": int(time.time() * 1000),
}
)
self._write(failed)
return failed, error
updated = PatchRecord(
**{
**record.to_dict(),
"status": "applied",
"mutations": tuple(mutations),
"verified_state_sha256": "",
"last_error": "",
"updated_unix_ms": int(time.time() * 1000),
}
)
self._write(updated)
return updated, ""
def verify(
self,
patch_id: str,
*,
command: str,
working_directory: str,
timeout_s: float,
session_id: str,
run_command: RunCommand,
) -> tuple[PatchRecord, ReproductionRecord, str, str]:
record = self.get(patch_id, session_id=session_id)
if record.status not in {"applied", "verification_failed"}:
raise RuntimeError("patch must be applied before verification")
reproduction, stdout, stderr = self.reproductions.run(
command=command,
working_directory=working_directory,
snapshot_paths=record.paths,
timeout_s=timeout_s,
session_id=session_id,
run_command=run_command,
)
passed = reproduction.command_ok and reproduction.exit_code == 0
updated = PatchRecord(
**{
**record.to_dict(),
"status": "verified" if passed else "verification_failed",
"verification_ids": (*record.verification_ids, reproduction.reproduction_id),
"verified_state_sha256": self._covered_state(record.paths)
if passed
else "",
"last_error": "" if passed else "verification command did not succeed",
"updated_unix_ms": int(time.time() * 1000),
}
)
self._write(updated)
return updated, reproduction, stdout, stderr
def commit(self, patch_id: str, *, session_id: str) -> PatchRecord:
record = self.get(patch_id, session_id=session_id)
if record.status != "verified":
raise RuntimeError("patch must have successful current verification")
current_state = self._covered_state(record.paths)
if not hmac.compare_digest(current_state, record.verified_state_sha256):
raise RuntimeError("covered files changed after patch verification")
self.transactions.commit(record.transaction_id, session_id=session_id)
updated = PatchRecord(
**{
**record.to_dict(),
"status": "committed",
"updated_unix_ms": int(time.time() * 1000),
}
)
self._write(updated)
return updated
def require_current_verification(
self, patch_id: str, *, session_id: str
) -> PatchRecord:
record = self.get(patch_id, session_id=session_id)
if record.status not in {"verified", "committed"}:
raise ValueError("public disclosure requires a verified or committed patch")
current_state = self._covered_state(record.paths)
if not record.verified_state_sha256 or not hmac.compare_digest(
current_state, record.verified_state_sha256
):
raise RuntimeError("covered files changed after patch verification")
return record
def rollback(self, patch_id: str, *, session_id: str) -> PatchRecord:
record = self.get(patch_id, session_id=session_id)
if record.status == "rolled_back":
return record
if record.status == "rollback_failed":
raise RuntimeError("automatic rollback failed; inspect current file state")
self.transactions.rollback(
record.transaction_id,
self._current_digests(record.paths),
session_id=session_id,
)
transaction = self.transactions.get(
record.transaction_id, session_id=session_id
)
restored = self._current_digests(record.paths)
for entry in transaction.entries:
if not hmac.compare_digest(restored[entry.path], entry.sha256):
raise RuntimeError("rollback restoration proof failed")
restoration_sha256 = _state_sha256(
tuple(
FileState(path, digest, 0)
for path, digest in sorted(restored.items())
)
)
updated = PatchRecord(
**{
**record.to_dict(),
"status": "rolled_back",
"verified_state_sha256": "",
"restoration_sha256": restoration_sha256,
"updated_unix_ms": int(time.time() * 1000),
}
)
self._write(updated)
return updated
class DisclosureStore:
def __init__(self, workspace: str | Path) -> None:
self.workspace = ensure_control_root(workspace)
self.root = control_root(self.workspace) / "engineering" / "disclosures"
self.root.mkdir(parents=True, exist_ok=True, mode=0o700)
os.chmod(self.root, 0o700)
self.triage = TriageStore(self.workspace)
self.patches = PatchStore(self.workspace)
self.artifacts = ArtifactStore(self.workspace)
def create(
self,
*,
triage_id: str,
title: str,
summary: str,
impact: str,
remediation: str,
audience: str,
patch_id: str,
session_id: str,
) -> DisclosureRecord:
triage = self.triage.get(triage_id, session_id=session_id)
if audience not in _AUDIENCES:
raise ValueError("disclosure audience is invalid")
fields = tuple(
_sanitize(value, self.workspace)
for value in (title, summary, impact, remediation)
)
if any(not value.strip() for value in fields):
raise ValueError("disclosure fields must not be empty")
patch = self.patches.get(patch_id, session_id=session_id) if patch_id else None
if audience == "public":
if patch is None:
raise ValueError(
"public disclosure requires a verified or committed patch"
)
patch = self.patches.require_current_verification(
patch.patch_id, session_id=session_id
)
evidence = {
"triage": triage.evidence_sha256,
"reproductions": list(triage.reproduction_ids),
"patch": {
"id": patch.patch_id,
"status": patch.status,
"verification_ids": list(patch.verification_ids),
"mutations": [
{
"before": mutation.before_sha256,
"after": mutation.after_sha256,
}
for mutation in patch.mutations
],
}
if patch is not None
else None,
}
evidence_sha256 = hashlib.sha256(
json.dumps(evidence, sort_keys=True, separators=(",", ":")).encode(
"utf-8"
)
).hexdigest()
fact_lines = "\n".join(f"- {fact}" for fact in triage.observed_facts)
hypothesis_lines = "\n".join(
f"- {hypothesis}" for hypothesis in triage.hypotheses
) or "- None recorded"
patch_section = ""
if patch is not None:
patch_section = (
"\n## Repair Evidence\n"
f"- Status: {patch.status}\n"
f"- Changed files: {len({mutation.path for mutation in patch.mutations})}\n"
f"- Verification receipts: {', '.join(patch.verification_ids) or 'none'}\n"
)
document = (
f"# {fields[0]}\n\n"
f"Audience: {audience}\n\n"
"## Summary\n"
f"{fields[1]}\n\n"
"## Impact\n"
f"{fields[2]}\n\n"
"## Observed Facts\n"
f"{fact_lines}\n\n"
"## Hypotheses\n"
f"{hypothesis_lines}\n\n"
"## Evidence\n"
f"- Evidence digest: {evidence_sha256}\n"
f"- Reproduction receipts: {', '.join(triage.reproduction_ids)}\n"
f"- Severity: {triage.severity}\n"
f"- Confidence: {triage.confidence:.3f}\n"
f"{patch_section}\n"
"## Remediation\n"
f"{fields[3]}\n\n"
"Raw command output, credentials, absolute workspace paths, private session identifiers, and internal control state are omitted.\n"
)
artifact = self.artifacts.put_text(
document,
media_type="text/markdown; charset=utf-8",
source="sanitized_disclosure",
session_id=session_id or "direct",
)
record = DisclosureRecord(
disclosure_id="dis_" + secrets.token_hex(16),
session_sha256=_session_sha256(session_id),
triage_id=triage_id,
patch_id=patch_id,
audience=audience,
evidence_sha256=evidence_sha256,
artifact_id=artifact.artifact_id,
artifact_sha256=artifact.sha256,
created_unix_ms=int(time.time() * 1000),
)
_atomic_json(
_record_path(self.root, record.disclosure_id, "dis"), record.to_dict()
)
return record
def get(self, disclosure_id: str, *, session_id: str) -> DisclosureRecord:
payload = _load_payload(_record_path(self.root, disclosure_id, "dis"))
record = DisclosureRecord(**payload)
_require_session(record.session_sha256, session_id)
return record
class EvidenceBundleStore:
def __init__(self, workspace: str | Path) -> None:
self.workspace = ensure_control_root(workspace)
self.root = control_root(self.workspace) / "engineering" / "bundles"
self.root.mkdir(parents=True, exist_ok=True, mode=0o700)
os.chmod(self.root, 0o700)
self.reproductions = ReproductionStore(self.workspace)
self.triage = TriageStore(self.workspace)
self.disclosures = DisclosureStore(self.workspace)
self.patches = PatchStore(self.workspace)
self.artifacts = ArtifactStore(self.workspace)
def create(
self,
*,
title: str,
reproduction_ids: tuple[str, ...],
triage_ids: tuple[str, ...],
disclosure_ids: tuple[str, ...],
patch_ids: tuple[str, ...],
session_id: str,
) -> EvidenceBundleRecord:
safe_title = _sanitize(title, self.workspace).strip()
if not safe_title:
raise ValueError("evidence bundle title is required")
if not any((reproduction_ids, triage_ids, disclosure_ids, patch_ids)):
raise ValueError("evidence bundle requires at least one receipt id")
if any(
len(rows) != len(set(rows))
for rows in (reproduction_ids, triage_ids, disclosure_ids, patch_ids)
):
raise ValueError("evidence bundle receipt ids must be unique per type")
reproductions = tuple(
self.reproductions.get(identifier, session_id=session_id)
for identifier in reproduction_ids
)
triages = tuple(
self.triage.status(identifier, session_id=session_id)
for identifier in triage_ids
)
disclosures = tuple(
self.disclosures.get(identifier, session_id=session_id)
for identifier in disclosure_ids
)
patches = tuple(
self.patches.get(identifier, session_id=session_id)
for identifier in patch_ids
)
evidence = {
"reproductions": [
{
"id": record.reproduction_id,
"command": record.command_sha256,
"environment": record.environment_sha256,
"exit_code": record.exit_code,
"status": record.status,
"stdout": record.stdout_sha256,
"stderr": record.stderr_sha256,
"before": record.before_state_sha256,
"after": record.after_state_sha256,
}
for record in reproductions
],
"triage": [
{
"id": str(row["triage_id"]),
"evidence": str(row["evidence_sha256"]),
"severity": str(row["severity"]),
"confidence": float(row["confidence"]),
"state_drift_observed": bool(row["state_drift_observed"]),
}
for row in triages
],
"disclosures": [
{
"id": record.disclosure_id,
"triage": record.triage_id,
"patch": record.patch_id,
"audience": record.audience,
"artifact": record.artifact_id,
"artifact_sha256": record.artifact_sha256,
}
for record in disclosures
],
"patches": [
{
"id": record.patch_id,
"triage": record.triage_id,
"status": record.status,
"paths": len(record.paths),
"mutations": len(record.mutations),
"verification_ids": list(record.verification_ids),
"verified_state": record.verified_state_sha256,
"restoration": record.restoration_sha256,
}
for record in patches
],
}
evidence_sha256 = hashlib.sha256(
json.dumps(evidence, sort_keys=True, separators=(",", ":")).encode(
"utf-8"
)
).hexdigest()
document = (
f"# {safe_title}\n\n"
f"Evidence digest: {evidence_sha256}\n\n"
"## Reproductions\n"
+ "\n".join(
(
f"- {record.reproduction_id}: status={record.status}, "
f"exit={record.exit_code}, stdout={record.stdout_sha256}, "
f"stderr={record.stderr_sha256}"
)
for record in reproductions
)
+ ("\n" if reproductions else "- None\n")
+ "\n## Triage\n"
+ "\n".join(
(
f"- {row['triage_id']}: severity={row['severity']}, "
f"confidence={float(row['confidence']):.3f}, "
f"evidence={row['evidence_sha256']}"
)
for row in triages
)
+ ("\n" if triages else "- None\n")
+ "\n## Disclosures\n"
+ "\n".join(
(
f"- {record.disclosure_id}: audience={record.audience}, "
f"artifact={record.artifact_id}, sha256={record.artifact_sha256}"
)
for record in disclosures
)
+ ("\n" if disclosures else "- None\n")
+ "\n## Patches\n"
+ "\n".join(
(
f"- {record.patch_id}: status={record.status}, "
f"mutations={len(record.mutations)}, "
f"verification={','.join(record.verification_ids) or 'none'}"
)
for record in patches
)
+ ("\n" if patches else "- None\n")
+ "\nRaw command output, credentials, absolute workspace paths, private session identifiers, and internal control state are omitted.\n"
)
artifact = self.artifacts.put_text(
document,
media_type="text/markdown; charset=utf-8",
source="sanitized_evidence_bundle",
session_id=session_id or "direct",
)
record = EvidenceBundleRecord(
bundle_id="evb_" + secrets.token_hex(16),
session_sha256=_session_sha256(session_id),
title=safe_title,
reproduction_ids=reproduction_ids,
triage_ids=triage_ids,
disclosure_ids=disclosure_ids,
patch_ids=patch_ids,
evidence_sha256=evidence_sha256,
artifact_id=artifact.artifact_id,
artifact_sha256=artifact.sha256,
created_unix_ms=int(time.time() * 1000),
)
_atomic_json(_record_path(self.root, record.bundle_id, "evb"), record.to_dict())
return record
def get(self, bundle_id: str, *, session_id: str) -> EvidenceBundleRecord:
payload = _load_payload(_record_path(self.root, bundle_id, "evb"))
record = EvidenceBundleRecord(
**{
**payload,
"reproduction_ids": tuple(payload.get("reproduction_ids", [])),
"triage_ids": tuple(payload.get("triage_ids", [])),
"disclosure_ids": tuple(payload.get("disclosure_ids", [])),
"patch_ids": tuple(payload.get("patch_ids", [])),
}
)
_require_session(record.session_sha256, session_id)
return record
def _tool_result(
call: ToolCall,
*,
started: float,
ok: bool,
output: str = "",
error: str = "",
exit_code: int | None = None,
) -> ToolExecutionResult:
rendered = output or error
return ToolExecutionResult(
name=call.name,
args=call.args,
ok=ok,
tool_call_id=call.call_id,
output=output,
error=error,
exit_code=exit_code,
executed=True,
elapsed_s=round(time.perf_counter() - started, 4),
source_trust="trusted_execution",
output_sha256=hashlib.sha256(rendered.encode("utf-8")).hexdigest(),
)
def execute_engineering_tool(
call: ToolCall,
context: ToolExecutionContext,
*,
run_command: RunCommand,
) -> ToolExecutionResult:
"""Execute one evidence-bound engineering tool at the runtime boundary."""
started = time.perf_counter()
spec = _ENGINEERING_TOOL_BY_NAME.get(call.name)
if spec is None:
return _tool_result(
call,
started=started,
ok=False,
error=f"unsupported engineering tool: {call.name}",
)
try:
spec.validate_arguments(call.args)
workspace = context.workspace
session_id = context.session_id
if call.name == "ReproductionRun":
reproduction_record, stdout, stderr = ReproductionStore(workspace).run(
command=str(call.args["command"]),
working_directory=str(call.args.get("working_directory") or "."),
snapshot_paths=_string_tuple(
call.args.get("snapshot_paths", []), "snapshot_paths"
),
timeout_s=context.timeout_s,
session_id=session_id,
run_command=run_command,
)
output = json.dumps(
{
"record": reproduction_record.to_dict(),
"stdout": stdout,
"stderr": stderr,
},
sort_keys=True,
)
return _tool_result(
call,
started=started,
ok=reproduction_record.exit_code is not None,
output=output,
exit_code=reproduction_record.exit_code,
)
if call.name == "ReproductionCompare":
comparison = ReproductionStore(workspace).compare(
_string_tuple(
call.args["reproduction_ids"],
"reproduction_ids",
allow_empty=False,
),
session_id=session_id,
)
return _tool_result(
call,
started=started,
ok=True,
output=json.dumps(comparison, sort_keys=True),
)
if call.name == "TriageCreate":
triage_record = TriageStore(workspace).create(
reproduction_ids=_string_tuple(
call.args["reproduction_ids"],
"reproduction_ids",
allow_empty=False,
),
observed_facts=_string_tuple(
call.args["observed_facts"],
"observed_facts",
allow_empty=False,
),
hypotheses=_string_tuple(call.args["hypotheses"], "hypotheses"),
severity=str(call.args["severity"]),
confidence=float(call.args["confidence"]),
next_actions=_string_tuple(
call.args["next_actions"], "next_actions"
),
session_id=session_id,
)
return _tool_result(
call,
started=started,
ok=True,
output=json.dumps(triage_record.to_dict(), sort_keys=True),
)
if call.name == "TriageStatus":
triage_status = TriageStore(workspace).status(
str(call.args["triage_id"]), session_id=session_id
)
return _tool_result(
call,
started=started,
ok=True,
output=json.dumps(triage_status, sort_keys=True),
)
if call.name == "TriageUpdate":
raw_confidence = call.args.get("confidence")
current = TriageStore(workspace).get(
str(call.args["triage_id"]), session_id=session_id
)
triage_record = TriageStore(workspace).update(
str(call.args["triage_id"]),
reproduction_ids=_string_tuple(
call.args.get("reproduction_ids", []),
"reproduction_ids",
),
observed_facts=_string_tuple(
call.args.get("observed_facts", []), "observed_facts"
),
hypotheses=_string_tuple(call.args.get("hypotheses", []), "hypotheses"),
severity=str(call.args.get("severity") or current.severity),
confidence=float(raw_confidence)
if raw_confidence is not None
else current.confidence,
next_actions=_string_tuple(
call.args.get("next_actions", []), "next_actions"
),
session_id=session_id,
)
return _tool_result(
call,
started=started,
ok=True,
output=json.dumps(triage_record.to_dict(), sort_keys=True),
)
if call.name == "DisclosureCreate":
disclosure_record = DisclosureStore(workspace).create(
triage_id=str(call.args["triage_id"]),
title=str(call.args["title"]),
summary=str(call.args["summary"]),
impact=str(call.args["impact"]),
remediation=str(call.args["remediation"]),
audience=str(call.args.get("audience") or "maintainer"),
patch_id=str(call.args.get("patch_id") or ""),
session_id=session_id,
)
return _tool_result(
call,
started=started,
ok=True,
output=json.dumps(disclosure_record.to_dict(), sort_keys=True),
)
if call.name == "DisclosureStatus":
disclosure_record = DisclosureStore(workspace).get(
str(call.args["disclosure_id"]), session_id=session_id
)
return _tool_result(
call,
started=started,
ok=True,
output=json.dumps(disclosure_record.to_dict(), sort_keys=True),
)
if call.name == "EvidenceBundleCreate":
bundle_record = EvidenceBundleStore(workspace).create(
title=str(call.args["title"]),
reproduction_ids=_string_tuple(
call.args.get("reproduction_ids", []), "reproduction_ids"
),
triage_ids=_string_tuple(call.args.get("triage_ids", []), "triage_ids"),
disclosure_ids=_string_tuple(
call.args.get("disclosure_ids", []), "disclosure_ids"
),
patch_ids=_string_tuple(call.args.get("patch_ids", []), "patch_ids"),
session_id=session_id,
)
return _tool_result(
call,
started=started,
ok=True,
output=json.dumps(bundle_record.to_dict(), sort_keys=True),
)
if call.name == "EvidenceBundleStatus":
bundle_record = EvidenceBundleStore(workspace).get(
str(call.args["bundle_id"]), session_id=session_id
)
return _tool_result(
call,
started=started,
ok=True,
output=json.dumps(bundle_record.to_dict(), sort_keys=True),
)
patches = PatchStore(workspace)
if call.name == "PatchBegin":
patch_record = patches.begin(
triage_id=str(call.args["triage_id"]),
paths=_string_tuple(
call.args["paths"], "paths", allow_empty=False
),
session_id=session_id,
)
elif call.name == "PatchApply":
raw_changes = call.args["changes"]
if not isinstance(raw_changes, list):
raise ValueError("changes must be an array")
patch_record, error = patches.apply(
str(call.args["patch_id"]),
tuple(raw_changes),
session_id=session_id,
)
return _tool_result(
call,
started=started,
ok=not error,
output=json.dumps(patch_record.to_dict(), sort_keys=True),
error=error,
)
elif call.name == "PatchVerify":
patch_record, verification_record, stdout, stderr = patches.verify(
str(call.args["patch_id"]),
command=str(call.args["command"]),
working_directory=str(call.args.get("working_directory") or "."),
timeout_s=context.timeout_s,
session_id=session_id,
run_command=run_command,
)
output = json.dumps(
{
"patch": patch_record.to_dict(),
"reproduction": verification_record.to_dict(),
"stdout": stdout,
"stderr": stderr,
},
sort_keys=True,
)
return _tool_result(
call,
started=started,
ok=patch_record.status == "verified",
output=output,
error=patch_record.last_error,
exit_code=verification_record.exit_code,
)
elif call.name == "PatchCommit":
patch_record = patches.commit(
str(call.args["patch_id"]), session_id=session_id
)
elif call.name == "PatchRollback":
patch_record = patches.rollback(
str(call.args["patch_id"]), session_id=session_id
)
elif call.name == "PatchStatus":
patch_record = patches.get(
str(call.args["patch_id"]), session_id=session_id
)
else:
raise RuntimeError("engineering tool dispatch is incomplete")
return _tool_result(
call,
started=started,
ok=True,
output=json.dumps(patch_record.to_dict(), sort_keys=True),
)
except (OSError, PermissionError, RuntimeError, TypeError, ValueError) as exc:
return _tool_result(
call,
started=started,
ok=False,
error=f"{type(exc).__name__}: {exc}",
)
__all__ = [
"DisclosureRecord",
"DisclosureStore",
"ENGINEERING_TOOL_NAMES",
"ENGINEERING_TOOL_SPECS",
"EvidenceBundleRecord",
"EvidenceBundleStore",
"FileState",
"PatchMutation",
"PatchRecord",
"PatchStore",
"ReproductionRecord",
"ReproductionStore",
"TriageRecord",
"TriageStore",
"execute_engineering_tool",
]
|