# -*- coding: utf-8 -*-
"""r369 收尾：baseline（round 369）+ 台账条目"""
import io, json, os, time

ROOT = r"D:\work buddy\2026-11-05-14-15-52"
BASE = os.path.join(ROOT, "_heartbeat_baseline.json")
MAIN = os.path.join(ROOT, "index_main.html")
LED = os.path.join(ROOT, "项目库扩充方案与进度台账.md")

b = json.load(io.open(BASE, encoding="utf-8"))
rep = json.load(io.open(os.path.join(ROOT, "_density_report.json"), encoding="utf-8"))

st = os.stat(MAIN)
mt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(st.st_mtime))

code = None
try:
    import urllib.request
    with urllib.request.urlopen("http://127.0.0.1:8000/index_main.html", timeout=10) as r:
        code = r.getcode()
except Exception as e:
    code = "ERR:%s" % type(e).__name__

b["round"] = 369
b["timestamp"] = mt
b["schools"] = 36
b["programs"] = 1440
b["catd"] = 2817
b["vd"] = 1587
b["phd"] = 499
b["master"] = 941
b["dead_links"] = 0
b["exclude_residue"] = 0
b["details_on_disk"] = 3775
b["main_file_mtime"] = mt
b["main_file_bytes"] = st.st_size
b["http"] = code
gc = rep["grade_counts"]
b["density_grade"] = {"A": gc.get("A", 0), "B": gc.get("B", 0), "C": gc.get("C", 0)}

ACTIONS = [
    "只读基线：final_check 36/1440/2817、死链 0、残留 0；gap_check Top30 30/30、榜外保留 nyu/illinois/ut/ubc/mcg/uw；vd_check url 去重 1440、VD 1547、磁盘缺失 0、未进 0、孤儿 0、details 3775；node_check 内嵌 script 1 块 exit 0；开工主文件 1,972,241 字节 mtime 2026-12-03 15:59:20",
    "候选判断：_r361_pick.py 仍只有 8 条 score 7（col SIPA 六条 + notre-mscse + mit-physics-phd）；本轮先试 Columbia 替代路径，sipa.columbia.edu / www.columbia.edu / gsas.columbia.edu 全部 403，bulletin.columbia.edu 仅有 Barnard/Columbia College/General Studies/Engineering 四册（SIPA 不在其中，school-international-public-affairs 404），SIPA 六条止损；MIT physics 的 physics.mit.edu 仍 403，但发现 catalog.mit.edu/schools/science/physics/ 200（111K 文本），留作下轮入口",
    "Notre Dame 换池：发现官方入口 graduateschool.nd.edu/degree-programs/（200，12.4 万字符，逐项目列截止/学位/最短驻校/申请材料/联系人），据本轮打通 8 条热门；学生账务处 studentaccounts.nd.edu/rates/graduate-programs/（200，2026-27 研究生费率全表）；研究生院资助 graduateschool.nd.edu/funding/（200，2026-27 博士 stipend 分档 + 94% 资助率）",
    "院系页实测 200：cse.nd.edu/graduate/（2,971 字符）、ee.nd.edu/graduate/（3,444）、ame.nd.edu/graduate/（3,517）、acms.nd.edu/（2,576）、biology.nd.edu/（8,297）；catalog.nd.edu 研究生项目公报页全 404（只有本科目录），课号 5 位（CSE 60xxx）不被 density 正则计分，故本轮 codes 恒为 3",
    "建页 8 条（全部 detail_render.py 生成、无手写 HTML，旧页先 Copy-Item 为 .bak_r369）：notre-mscse 10 / notre-cse-phd 10 / notre-msee 9 / notre-ee-phd 9 / notre-msme 9 / notre-ame-phd 9 / notre-acms-ms 9 / notre-biosci-phd 9",
    "PROGRAMS 回填 10 处（_r369_apply.py，块内正则唯一性断言 + 每块独立重算边界；首版因块内二次替换用旧偏移导致 `url` 被截成 `rl`，node exit 1，已回滚 .bak_r369 重写安全版）：8 条 tui 换成官网学费/资助数字（MSCSE $34,555/学期、$3,840/学分；CSE/EE/AME 博士 stipend $42,435 + 学费奖学金 $69,510；ACMS $2,302/学分、30 学分约 $69,060；BioSci 理科博士 $40,590），另补 notre-ame-phd 与 notre-acms-ms 的 lang",
    "回填后全量校验：node exit 0、final 36/1440/2817、死链 0、残留 0、vd 未进 0/孤儿 0；主文件 1,972,810 字节",
    "密度复核：A 270→278、B 105→101（MSCSE/MSEE/MSME/ACMS 原为 B）、C 1065→1061，需重做 1170→1162；8 条新页 chars 2,903–3,692、money 3–17、dates 2–3、ext 5–6、cards 7",
]
for a in ACTIONS:
    if a not in b["actions_this_round"]:
        b["actions_this_round"].append(a)

NOTES = [
    "r369 新打通（可复用）：graduateschool.nd.edu/degree-programs/ 是 Notre 全部研究生项目的官方总表（每项目含截止、学位、最短驻校、申请材料、联系人），一条 URL 可覆盖圣母 66 条在库项目，后续可批量低成本升级",
    "r369 新打通：studentaccounts.nd.edu/rates/graduate-programs/（2026-27 研究生费率：Graduate School $34,555/学期、$69,110/年、$3,840/学分；ACMS 传统项目 $2,302/学分、6 学分 $13,812、12 学分 $27,624；在线 ACMS-DS Class of 2027 $11,800/学期）；graduateschool.nd.edu/funding/（工程类博士 $42,435、理科 $40,590、人文 $39,000、Mendoza $44,000，select fellows 至 $50,000，94% 博士生获资助）",
    "r369 口径细节：研究生院资助页写「2026-27 学费 $69,510 含 Health 与 Technology 费」，学生账务处拆分 $69,110 学费 + $250 技术费 + $150 健康中心 + $100 活动费 = $69,610；两数差异即 $100 活动费，页内已注明，勿当作矛盾",
    "r369 语言口径：研究生院 admissions/application-requirements 页只列「须提交 TOEFL/IELTS/Duolingo」，未公布统一最低分数线；本轮页面按「官网未披露基线 + 记录历史核验口径 TOEFL iBT 80（口语 23）/IELTS 7.0/Duolingo 120」双写，未编造分数线",
    "r369 不可达（勿重复踩）：sipa.columbia.edu、www.columbia.edu、gsas.columbia.edu 全 403（含 bulletin 也查不到 SIPA 册）；mendoza.nd.edu 403（MSBA/MSF/MSA 三条留待下轮）；catalog.nd.edu/graduate/* 全 404（2026-27 公报只有本科项目页可用）；bulletin.nd.edu、classschedule.nd.edu ConnectionError；cse.nd.edu/graduate/programs/* 与 /mscse/ 均 404",
    "r369 可用但未用：catalog.mit.edu/schools/science/physics/（200，111K 文本，含 MIT 物理研究生要求与 8.xxx 课程）；catalog.mit.edu/subjects/8/（200，物理课程清单）——下轮可据此升级 mit-physics-phd（原页面 403）",
    "r369 记录：Notre 课号一律 5 位（CSE 60xxx / EE 60xxx / ACMS 60xxx），_density_check 的 CODE 正则（2–8 位字母 + 3–4 位数字）识别不到，8 条页 codes 固定 3（仅命中少量 4 位缩写），故本轮以 chars/money/dates/ext 补足到 9–10 分；这是正则限制而非缺课号，页内已写明「课号见系 handbook」",
    "r369 记录：PROGRAMS 首版回填写坏主文件（块内第二次替换沿用旧偏移，把 `url:` 削成 `rl:`，PROGRAMS 掉到 1439、node exit 1），已用 index_main.html.bak_r369 完整回滚并改用「每块独立替换 + 重新计算边界」的安全实现，最终 1440/1440、node 0",
    "r369 待办：Notre 剩余 15 条非 A（notre-acms-ms 已清，其余含 notre-msae 4 / notre-cee-meng 4 / notre-msce 4 / notre-msenve 4 / notre-msba 4 / notre-mse-phd 4 / notre-msf 3 / notre-msa 3 / notre-bme-phd 3 / notre-neuroscience-phd 3 等），工程类可继续用 graduateschool 总表 + 院系页，商科三条须先解决 mendoza 403",
    "r369 待办：col SIPA 六条（col-mia/col-mpa/col-mpadp/col-mpaepm/col-mpaesp/col-mpagl）连续多轮 403，建议视为「官网不可达」按第九章合法 B 处理或等官网改版；mit-physics-phd 走 catalog.mit.edu 可解",
]
for n in NOTES:
    if n not in b["blocked_notes"]:
        b["blocked_notes"].append(n)

io.open(BASE, "w", encoding="utf-8").write(json.dumps(b, ensure_ascii=False, indent=1))
print("baseline round", b["round"], "bytes", b["main_file_bytes"], "http", b["http"],
      "density", b["density_grade"], "notes", len(b["blocked_notes"]), "actions", len(b["actions_this_round"]))

# ---------------- 台账 ----------------
led = io.open(LED, encoding="utf-8").read()
ENTRY = (
 "- **r369 热门 8 条批次（2026-12-03，8 条全部 A · Notre Dame 换池 · 研究生院项目总表打通）**："
 "候选池 `_r361_pick.py` 仍只剩 8 条 score 7（col SIPA 六条 + notre-mscse + mit-physics-phd）。先试 Columbia 替代路径全部止损："
 "sipa.columbia.edu / www.columbia.edu / gsas.columbia.edu 均 403，bulletin.columbia.edu 只挂 Barnard / Columbia College / General Studies / Engineering 四册，"
 "`school-international-public-affairs` 404，SIPA 六条无官网可达页；MIT physics 的 physics.mit.edu 仍 403，但发现 catalog.mit.edu/schools/science/physics/ 200（111K 文本），留作下轮。"
 "于是转打 Notre Dame：新打通 graduateschool.nd.edu/degree-programs/（200，12.4 万字符，逐项目列截止 / 学位 / 最短驻校 / 申请材料 / 联系人）、"
 "studentaccounts.nd.edu/rates/graduate-programs/（2026-27 费率总表）、graduateschool.nd.edu/funding/（博士 stipend 分档与 94% 资助率），"
 "配合院系页 cse/ee/ame.nd.edu/graduate/、acms.nd.edu/、biology.nd.edu/ 完成 8 条："
 "notre-mscse（MSCSE：秋 12/15 · 春 10/15、11 个月（两学期 + 夏季）、最短驻校 1 学期、project 或 thesis、GRE 可选、professional 与传统 M.S. 等值、$34,555/学期（$69,110/年）与 $3,840/学分、含杂费 $34,805/学期）"
 "／notre-cse-phd（CSE PhD：秋 12/15 · 春 10/15、最短驻校 2 年、3–5 年、BS 可直接申请、RA/TA 资助、工程类博士 stipend $42,435（fellow 至 $50,000）、学费 $69,510 含健康与技术费、94% 获资助）"
 "／notre-msee（MSEE：12/15、1–1.5 年、课程为主 + 论文可选、GRE 推荐不强制、典型自费、$34,555/学期）"
 "／notre-ee-phd（EE PhD：12/15（校内 select fellowship 优先轮，春季 11/1 仅教师邀请）、驻校 4 学期、典型 5 年、录取即全额资助、电路与系统 / 材料与器件两大块）"
 "／notre-msme（MSME：秋 12/31 · 春 10/15、1.5–2 年、GRE 可选、5:1 师生比、毕业生去 Google/Intel/Northrup-Grumman 与 NASA JPL）"
 "／notre-ame-phd（AME PhD：秋 12/31 · 春 10/15、驻校 2 年、四大研究块 + Manufacturing Robotics and Controls、$42,435 + 全额学费）"
 "／notre-acms-ms（ACMS 专业硕士：1/15（晚申至 5/31）、30 学分、三方向 Applied Statistics / Applied and Computational Mathematics / Predictive Analytics、GRE 可选、ACMS 按学分 $2,302（6 学分 $13,812、12 学分 $27,624），30 学分约 $69,060）"
 "／notre-biosci-phd（BioSci PhD：秋季 12/1、驻校 2 年、159 名研究生 / 63 位教师 / 40+ 国家、生物医学 / 全球健康 / EEE 三板块、理科博士 stipend $40,590）。"
 "密度验收 8 条全 A（10/10/9/9/9/9/9/9）：A 270→278、B 105→101、C 1065→1061、需重做 1170→1162。"
 "PROGRAMS 回填 10 处（8 条 tui 由「文本描述」换成官网数字，另补 ame-phd 与 acms-ms 的 lang），备份 index_main.html.bak_r369；"
 "首版回填因块内二次替换沿用旧偏移写坏主文件（`url` 被削成 `rl`、PROGRAMS 掉到 1439、node exit 1），已整文件回滚并改为「每块独立替换 + 重算边界」的安全实现。"
 "校验 node exit 0、院校 36 / PROGRAMS 1440 / CATD 2817、死链 0、残留 0、VD 未进 0 / 孤儿 0、details 3775、HTTP 200；主文件 1,972,810 字节。"
 "新增卡点 10 条：Columbia 全线 403 且 bulletin 无 SIPA 册、Mendoza 403（商科三条待解）、catalog.nd.edu 研究生公报 404、Notre 课号 5 位不被计分、"
 "研究生院仅要求提交语言成绩不公布分数线、$69,510 与 $69,610 的 $100 活动费差异、catalog.mit.edu 可作 MIT 物理替代入口、Notre 剩余 15 条非 A 清单、SIPA 六条建议按合法 B 处理。\n"
)
anchor = "## 八、决策层指令"
if "r369 热门 8 条批次" not in led:
    i = led.index(anchor)
    led = led[:i] + ENTRY + "\n" + led[i:]

io.open(LED, "w", encoding="utf-8").write(led)
print("ledger bytes", len(led.encode("utf-8")))
print("## 八、 at line", led.count("\n", 0, led.index(anchor)) + 1)
