Co-authored-by: multica <multica@users.noreply.github.com> Co-authored-by: multica-agent <github@multica.ai>
206 lines
9.2 KiB
Python
206 lines
9.2 KiB
Python
"""Recompute only provenance fields; preserve approved values, notes and qualifications."""
|
||
|
||
import copy
|
||
import csv
|
||
import json
|
||
import re
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
from attribution import Attributor, sha
|
||
|
||
ROOT = Path(__file__).resolve().parents[3]
|
||
HERE = Path(__file__).resolve().parent
|
||
PRIOR = HERE / "prior175"
|
||
ORIGINAL = ROOT / "docs/hel-892/batch175-closure-evidence"
|
||
LOCAL = ROOT / ".local-evidence/history-attribution-correction"
|
||
BASE = "0e3e7b071f4c293642900849db8b1ead997417b8"
|
||
HISTORY_KEYS = {"delivery_comment_id", "review_basis", "delivery_lookup_rule"}
|
||
|
||
|
||
def load(path):
|
||
return json.loads(path.read_bytes())
|
||
|
||
|
||
def write(out, name, obj):
|
||
with (out / name).open("x", encoding="utf-8") as f:
|
||
json.dump(obj, f, ensure_ascii=False, indent=2)
|
||
f.write("\n")
|
||
|
||
|
||
def text(out, name, value):
|
||
with (out / name).open("x", encoding="utf-8") as f:
|
||
f.write(value.rstrip() + "\n")
|
||
|
||
|
||
def main():
|
||
out = LOCAL / sys.argv[1]
|
||
assert out.parent == LOCAL
|
||
out.mkdir()
|
||
old = load(PRIOR / "ledger.json")
|
||
messages = load(PRIOR / "historical-comment-texts.json")
|
||
attribution = Attributor(ROOT, messages, BASE, load(PRIOR / "current-instruction.json"))
|
||
new, matrix, differences = [], [], []
|
||
for i, row in enumerate(old):
|
||
path, pin = row["source_repo_path"], row["pin"]
|
||
result = attribution.select(path, pin, row["delivery_comment_id"])
|
||
matrix.append(result)
|
||
updated = copy.deepcopy(row)
|
||
updated["delivery_comment_id"] = result["delivery_comment_id"]
|
||
updated["review_basis"] = result["review_basis"]
|
||
updated["delivery_lookup_rule"] = result["selection_rule"]
|
||
updated.update(
|
||
pin_record_last_touch_commit=row["latest_pin_record_commit"],
|
||
definition_pin_introduction_commit=result["definition_pin_introductions"][0]
|
||
if len(result["definition_pin_introductions"]) == 1
|
||
else "unknown",
|
||
definition_pin_introductions=result["definition_pin_introductions"],
|
||
delivery_head=result.get("delivery_head", "unknown"),
|
||
tested_implementation=result.get("tested_implementation", "unknown"),
|
||
file_pin_implementation_commit=result.get("file_pin_implementation_commit", "unknown"),
|
||
successor_record=result.get("successor_record", "unknown"),
|
||
historical_attribution_evidence=f"candidate-matrix.json#/{i}",
|
||
prior_delivery_comment_id=row["delivery_comment_id"],
|
||
)
|
||
assert {k: v for k, v in updated.items() if k in row and k not in HISTORY_KEYS} == {
|
||
k: v for k, v in row.items() if k not in HISTORY_KEYS
|
||
}
|
||
changed = {
|
||
k: dict(before=row.get(k), after=updated.get(k))
|
||
for k in updated
|
||
if row.get(k) != updated.get(k) or k not in row
|
||
}
|
||
differences.append(
|
||
dict(
|
||
source_repo_path=path,
|
||
old_delivery=row["delivery_comment_id"],
|
||
new_delivery=updated["delivery_comment_id"],
|
||
changed_fields=changed,
|
||
approved_non_history_fields_unchanged=True,
|
||
)
|
||
)
|
||
new.append(updated)
|
||
assert len(new) == len({r["source_repo_path"] for r in new}) == 155
|
||
summary = load(PRIOR / "summary.json")
|
||
summary["delivery_unresolved"] = sum(r["delivery_comment_id"] == "unknown" for r in new)
|
||
summary["review_basis_unknown"] = sum(r["review_basis"]["kind"] == "unknown" for r in new)
|
||
summary["attribution_correction"] = dict(
|
||
base=BASE,
|
||
delivery_changed=sum(r["old_delivery"] != r["new_delivery"] for r in differences),
|
||
unique_pin_origins=sum(len(r["definition_pin_introductions"]) == 1 for r in matrix),
|
||
original_review_unknown=load(PRIOR / "summary.json")["review_basis_unknown"],
|
||
chronology_used_for_selection=False,
|
||
main_card_history_bulk_fetched=False,
|
||
old_ledger_sha256=sha((PRIOR / "ledger.json").read_bytes()),
|
||
)
|
||
oldremaining = load(PRIOR / "remaining-work.json")
|
||
remaining = copy.deepcopy(oldremaining)
|
||
for i, r in enumerate(remaining):
|
||
assert r["source_repo_path"] == new[i]["source_repo_path"]
|
||
r["historical_attribution"] = dict(
|
||
delivery_comment_id=new[i]["delivery_comment_id"],
|
||
review_basis=new[i]["review_basis"],
|
||
candidate_matrix=f"candidate-matrix.json#/{i}",
|
||
)
|
||
assert {k: v for k, v in r.items() if k != "historical_attribution"} == oldremaining[i]
|
||
write(out, "ledger.json", new)
|
||
write(out, "summary.json", summary)
|
||
write(out, "remaining-work.json", remaining)
|
||
write(out, "candidate-matrix.json", matrix)
|
||
write(out, "row-field-differences.json", differences)
|
||
write(
|
||
out,
|
||
"comment-role-records.json",
|
||
[
|
||
dict(
|
||
comment_id=m["id"],
|
||
original_utf8_sha256=sha(m["content"].encode()),
|
||
references=attribution.refs[m["id"]],
|
||
)
|
||
for m in attribution.own
|
||
],
|
||
)
|
||
write(
|
||
out,
|
||
"git-facts.json",
|
||
dict(
|
||
resolutions=attribution.git.resolutions,
|
||
code=attribution.git.code,
|
||
parents=attribution.git.parent_cache,
|
||
pin_origins=attribution.git.origin_cache,
|
||
original_blobs=attribution.git.blobs,
|
||
),
|
||
)
|
||
with (out / "ledger.csv").open("x", encoding="utf-8-sig", newline="") as f:
|
||
writer = csv.DictWriter(f, fieldnames=list(new[0]), lineterminator="\n")
|
||
writer.writeheader()
|
||
writer.writerows(
|
||
{
|
||
k: json.dumps(v, ensure_ascii=False) if isinstance(v, (dict, list)) else v
|
||
for k, v in r.items()
|
||
}
|
||
for r in new
|
||
)
|
||
# Keep the approved descriptive body; replace only delivery/audit paragraphs and the old claim.
|
||
original = (PRIOR / "逐文件总账与剩余条件.md").read_text()
|
||
parts = re.split(r"(?m)^## (?=\d{3} )", original)
|
||
assert len(parts) == 156
|
||
intro = parts[0]
|
||
intro = re.sub(
|
||
r"每份交付按匹配pin证据文件.*?历史未通过。",
|
||
"旧175历史归因已被独立审核指出误配。"
|
||
"本版按唯一Git pin起点、提交角色与本文件闭包定位;记录文件最后触碰不当定义起点,"
|
||
"候选含基线/旧版/依赖引用均保留拒绝理由,歧义留unknown。",
|
||
intro,
|
||
flags=re.S,
|
||
)
|
||
intro = intro.replace("46份", str(summary["review_basis_unknown"]) + "份")
|
||
sections = [intro.rstrip()]
|
||
for oldsection, row in zip(parts[1:], new):
|
||
assert "源:`" + row["source_repo_path"] + "`" in oldsection
|
||
lines = oldsection.splitlines()
|
||
count = 0
|
||
for j, line in enumerate(lines):
|
||
if line.startswith("交付:"):
|
||
lines[j] = (
|
||
f"交付:记录文件最后触碰`{row['pin_record_last_touch_commit']}`;"
|
||
f"代码定义pin起点`{row['definition_pin_introduction_commit']}`;"
|
||
f"主head`{row['delivery_head']}`;明确受测实现`{row['tested_implementation']}`;"
|
||
f"评论`{row['delivery_comment_id']}`。逐候选原文/角色/闭包/拒绝理由:"
|
||
f"`{row['historical_attribution_evidence']}`。"
|
||
)
|
||
count += 1
|
||
elif line.startswith("审核依据:"):
|
||
lines[j] = "审核依据:" + json.dumps(row["review_basis"], ensure_ascii=False) + "。"
|
||
count += 1
|
||
assert count == 2
|
||
sections.append("## " + "\n".join(lines).rstrip())
|
||
text(out, "逐文件总账与剩余条件.md", "\n\n".join(sections))
|
||
text(
|
||
out,
|
||
"进度说明.md",
|
||
f"第175批历史归因窄修:全155行已重核,交付定位变更"
|
||
f"{summary['attribution_correction']['delivery_changed']}行,交付未能唯一确定"
|
||
f"{summary['delivery_unresolved']}行,审核依据仍unknown为{summary['review_basis_unknown']}行"
|
||
"(旧46不是门槛,unknown不等于未通过)。每行保留记录最后触碰、定义pin起点、"
|
||
"主交付、受测实现及基线/依赖/旧版/后继的不同含义,不按最新评论覆盖。\n\n"
|
||
"155固定参考、10761保存项、287注释、原SHA/pin、身份/来源字节和资格全部冻结。"
|
||
"仍16 pending_check/139 needs_conversion、1217/3660/294分列,全停用、未检查、"
|
||
"空定稿/实跑时间;定稿/新增可靠转换/真实生成仍均0。未重跑已通过的原值或构建/浏览套件。\n\n"
|
||
"只交本轮归因工具、全表候选矩阵、原/新总账SHA与逐字段差集、格式/负例验证及冻结证据。"
|
||
"原错误账和失败历史保存在prior175及原Git提交;174/175旧包仅引用并核SHA。"
|
||
"上一轮的两份试点建议暂停,未实施;不推进176、目标连接、转换修改、实跑或部署。"
|
||
"当前无需新增模型素材或机器窗口,等待HEL-893独立复审。",
|
||
)
|
||
print(json.dumps(summary["attribution_correction"], ensure_ascii=False))
|
||
print(
|
||
"unknown delivery",
|
||
summary["delivery_unresolved"],
|
||
"unknown audit",
|
||
summary["review_basis_unknown"],
|
||
)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|