# -*- coding: utf-8 -*-
"""r384 收尾：baseline（round=384）+ 台账条目。"""
import datetime, io, json, os, re, requests

P_HTML = 'index_main.html'
st = os.stat(P_HTML)
mtime = datetime.datetime.fromtimestamp(st.st_mtime).strftime('%Y-%m-%d %H:%M:%S')
bl = json.load(io.open('_heartbeat_baseline.json', encoding='utf-8'))
drep = json.load(io.open('_density_report.json', encoding='utf-8'))
rows = drep['rows'] if isinstance(drep, dict) and 'rows' in drep else drep
gc = {'A': 0, 'B': 0, 'C': 0}
for r in rows:
    if r.get('grade') in gc:
        gc[r['grade']] += 1

html = io.open(P_HTML, encoding='utf-8').read()
seg = re.search(r'PROGRAMS\s*=\s*\[(.*?)\n\s*\];', html, re.S).group(1)
phd = len(re.findall(r'lvl:"d"', seg))
mas = len(re.findall(r'lvl:"m"', seg))
progs = len(re.findall(r'\{id:"', seg))
det = len([f for f in os.listdir('details') if f.endswith('.html')])
try:
    http_code = requests.get('http://127.0.0.1:8000/index_main.html', timeout=8).status_code
except Exception:
    http_code = 'ERR'

bl['round'] = 384
bl['timestamp'] = mtime
bl['main_file_mtime'] = mtime
bl['main_file_bytes'] = st.st_size
bl['programs'] = progs
bl['programs_unique_ids'] = len(set(re.findall(r'\{id:"([^"]+)"', seg)))
bl['phd'] = phd
bl['master'] = mas
bl['catd'] = 2817
bl['vd'] = 1547
bl['details_on_disk'] = det
bl['dead_links'] = 0
bl['exclude_residue'] = 0
bl['density_grade'] = gc
bl['density_redo'] = gc['B'] + gc['C']
bl['http'] = http_code

NEW_NOTES = [
 'r384 新通道（Georgetown，第十一个可复用口）：① https://cs.georgetown.edu/graduate/ms/ 项目主页（cohort 15–25、雇主与职位清单、研究中心）与 /graduate/ms/curriculum/（30 学分结构、COSC 课号全表、UNXD 7950 / COSC 7950 实习课）、/graduate/ms/how-to-apply/（两季三档截止、GPA 3.0、TOEFL 90 或新制 4.5、IELTS 7.0、2 封推荐信、SOP 约 500 词、$90）三页全 200；② McCourt DSPP 四条路径全 200：/ms-in-data-science-for-public-policy/（主页）、/admissions-tuition/（Fall 2027 三轮日期 + $2,758/学分 + 39 学分）、/admissions-tuition/how-to-apply/（GRE 政策/送分码 5244、JT7-D8-97、TOEFL 100/5、IELTS 7.0、3 封推荐信、$90）、/curriculum/（PPOL 5004–5208 全课号、McCourt Foundations、120 小时实习）；③ https://studentaccounts.georgetown.edu/tuition/graduate/ 给 2026-2027 研究生费率：每学分 $2,758、论文研究 $2,750/学期、活动费 $27/学期、医保 $4,450/年（学校主站 /tuition-and-fees/ 与 grad.georgetown.edu/tuition/ 均不适用）',
 'r384 新通道（Emory，第十二个可复用口）：① 系站真实域名是 computerscience.emory.edu（www.cs.emory.edu 会 302 到它；PowerShell Invoke-WebRequest 因其证书链 UntrustedRoot 直接失败，必须用 python requests verify=False）；② /graduate-ms/index.html（3 核心 + 4 选修 + 9 学分、1.5–2 年、班额 35）、/graduate-ms/cs-ms-programs.html（CS 523/526/534/557/570/551/554/580 及 524/540/541/553/555/556/558/571/572/573/581/584、597R/599R 全课号与三条结题路径）、/graduate-ms/admissions/index.html（Spring 2027 与 Fall 2027 各三档日期、TOEFL 85/建议 90、IELTS 7、PTE 60、Duolingo 120、$75、机构码 5187、GRE 不要求）、/graduate-ms/admissions/ms-faq.html（2024-25 学费 $23,900/学期或 $2,656/学分）四页全 200；③ 博士：/graduate-phd/index.html（3 核心 + 4 选修 + 2 次 rotation、Ethics/Teaching 训练、平均 5 年）、/graduate-phd/admissions/index.html（12 月 15 日截止、仅秋季、PBT 600、3 封推荐信、$75、全额学费 + stipend）、/graduate-phd/admissions/phd-faqs.html（CGS 4 月 15 日、不做 rolling、可转评硕士）；④ https://gs.emory.edu/funding/ 给 FY26-27 博士 stipend $43,260（2025-09-12 与 SEIU 集体协议，有效期至 2027-08-24）',
 'r384 口径（Georgetown MS CS）：官网对背景是「推荐」而非硬性——建议 CS 或紧密相关本科 + 大量课程/职业经历；希望具备数据结构与算法、硬件与体系结构、编程、数学（离散/概率统计/线代/微积分）；通常要求本科 GPA 3.0；跨专业建议先读 Post-Bacc Certificate；30 学分 STEM，只 2 门核心（算法 + 系统），广度课须覆盖 3 个不同领域（理论必选、系统、AI 与信息处理、安全隐私、Broader Impacts）；论文轨 24 学分（8 门）+ 提案 3 + 论文研究 3 并答辩；最多 2 门外部选修（GU 其他系或 DC 联盟校）；实习课 UNXD 7950（3 学分）与 COSC 7950（0.25 学分），F-1 需 CPT；常开课号 COSC 4550/5521/5260/5290/5295/5402/5610/5750/4410/5510/4850/5540/5480/5760/5470；春季 Oct 1 最终、秋季 Jan 15 优先 + Apr 1 最终（官网未标年份）；申请费 $90；申请材料页未列 GRE/GMAT',
 'r384 口径（Georgetown DSPP）：39 学分 = 定量社科 6 + 政策基础 9 + Civic Data Science 15 + 伦理 1.5 + 沟通 1.5 + 选修 ≥6；课号 PPOL 5004/5006/5007/5008/5009/5200/5201/5202/5203/5204/5205/5206/5207/5208/6810，选修样例 PPOL 6206/6358/6212/6821/6805/6801/6814/6617/6707/6250/6251/6252/6002/6009；Fall 2027 三轮：2026-12-01 Early、2027-01-15 Priority（奖学金）、2027-03-01 Final，配套材料 2026-12-08 / 2027-01-23 / 2027-03-01，回复截止 2027-04-15 / 04-20 / 05-01；自 Fall 2027 起 GRE 不再必需但强烈建议（GMAT 同收，均无最低分，GRE 码 5244、GMAT JT7-D8-97）；TOEFL 100（新制 5）、IELTS 7.0 为严格门槛；$2,758/学分 × 39；McCourt Foundations 开学前 5 天 9:00–17:00 必修；实习 120 小时可豁免；2025 届 6 个月内 94% 就业或升学、DSPP 薪资中位数 $103.2k',
 'r384 口径（Emory CS MS / CSI PhD）：MS 为 3 核心（CS 523/526；CS 534/557/570；CS 551/554/580）+ 4 选修 + 9 学分结题，三选一 —— 项目轨 5 选修 + CS 597R/599R 6 学分、课程轨 7 选修、论文轨 4 选修 + CS 599R 9 学分；必修 practicum；成绩 C 以上且 GPA ≥3.0；最多 1 门外部替代（含 ARCHE 佐治亚理工）；2024-25 学费全日制 $23,900/学期、part-time $2,656/学分；Fall 2027 优先 2027-01-15 / 最终 2027-04-15 / 押金 2027-05-15；Spring 2027 优先 2026-11-01 / 最终 2026-12-01 / 押金 2026-12-15。博士：CSI 由 CS + 生物统计与生物信息 + 生物医学信息三系联合，CS 与 BMI 两 track；3 核心 + 4 选修 + 2 次 rotation + Ethics/Teaching 训练；课程 2–3 年、资格考试、论文提案与答辩、平均 5 年；12 月 15 日截止（官网未标年份）、仅秋季、CGS 4 月 15 日；TOEFL 85/90、PBT 600、IELTS 7、接受 Duolingo/PTE；GRE 不要求可选交；$75 申请费；全额学费 + FY26-27 stipend $43,260；可勾选「未录博士转硕士评审」',
 'r384 不可达（勿重复踩）：① bge.georgetown.edu（Biomedical Graduate Education，承 bioinformatics / health informatics 两个项目）DNS 解析失败（ConnectionError），本轮 geo-bioinfo-ms、geo-hids-ms 两条无法核验，留待替代域或 GUMC 子站确认；② mcgill.ca/compsci 403（需登录）、www.mcgill.ca/cs 与 /computer-science 均 404；③ www.mcgill.ca/study/2026-2027/... 与 /2025-2026/... 返回 202 且 body 为空（JS 盾），eCalendar 项目页无法静态抓取；④ www.mcgill.ca/ece/programs 明确写「ECE offers a Master of Engineering and a Doctor of Philosophy」，未见 M.Sc. —— 现有 mcg-msc-ece 条目 deg 标 MSc 与官网口径可能冲突，待 eCalendar 或系页确认后修正（先登记，不擅改）；⑤ Emory www.cs.emory.edu 证书链 UntrustedRoot，PowerShell Invoke-WebRequest 不可用，须 python requests verify=False；⑥ 本机 python http.server 对新写入的详情页偶发首查 404（再查即 200，疑似目录缓存），验收以 _r380_probe.py 复测为准',
]

have = set(bl.get('blocked_notes', []))
for n in NEW_NOTES:
    if n not in have:
        bl.setdefault('blocked_notes', []).append(n)
bl['notes'] = len(bl.get('blocked_notes', []))

bl['actions_this_round'] = [
 '只读基线：final_check 36/1439/2817、死链 0、残留 0；gap_check Top30 30/30；vd_check url 去重 1439 / VD 1547 / 磁盘 3775 / 未进 0 / 孤儿 0 / 缺失 0；node_check exit 0；开工主文件 1,977,302 字节 mtime 2026-12-04 07:02:28',
 '候选：从非 A 池（1,056 条）按热门优先级取 Georgetown 2 条（MS CS、DSPP）+ Emory 2 条（CS MS、CSI PhD）；同批探测的 geo-bioinfo-ms / geo-hids-ms 因 bge.georgetown.edu DNS 失效未做，mcg 系列因 403 / 202 盾未做',
 '新通道：Georgetown cs 系三页 + McCourt DSPP 四页 + studentaccounts 研究生学费页；Emory computerscience 七页 + gs.emory.edu/funding',
 '逐条实抓后写 4 spec（_r384_specs/，_r384_build.py 一次生成），每页 9 卡，全部 detail_render 生成',
 '密度验收：MS CS 11（5,636 字 / 17 课号 / 6 金额 / 5 日期）、DSPP 11（5,315 / 35 / 4 / 10）、Emory CS MS 11（4,750 / 25 / 4 / 7）、Emory CSI PhD 11（4,436 / 18 / 3 / 5）—— 4 条全 A',
 'PROGRAMS 回填 20 处（_r384_apply.py，备份 index_main.html.bak_r384）：ddl/lang/tui/dur/test 各 4 条',
 '回填后校验：node exit 0、36/1439/2817、死链 0、残留 0、VD 1547 未进 0、孤儿 0、缺失 0、details 3775',
 '复跑密度：A 383 → 387、B 99、C 957 → 953、需重做 1,056 → 1,052、rows 1439',
 '4 页终检无「编辑整理」「待补」「TBD」；HTTP 200 且长度与磁盘一致（21,552 / 23,411 / 21,360 / 20,313）；主文件 1,977,302 → 1,977,815 字节',
 '本轮未推送、未贴链接、未输出汇报；结果只写 _heartbeat_baseline.json 与台账',
]

io.open('_heartbeat_baseline.json', 'w', encoding='utf-8').write(json.dumps(bl, ensure_ascii=False, indent=1))
print('round', bl['round'], 'bytes', bl['main_file_bytes'], 'prog', progs, 'phd', phd, 'mas', mas,
      'vd', bl['vd'], 'det', det, 'density', gc, 'redo', bl['density_redo'], 'notes', bl['notes'],
      'actions', len(bl['actions_this_round']), 'http', http_code)

ENTRY = '''- **r384 Georgetown 2 条 + Emory 2 条（MS CS / DSPP / CS MS / CSI PhD）｜2026-12-04**

  - **只读基线**：final_check 36 / 1439 / 2817、死链 0、残留 0；gap_check Top30 30/30；vd_check url 去重 1439 / VD 1547 / 磁盘 3775 / 未进 0 / 孤儿 0 / 缺失 0；node_check exit 0。开工主文件 1,977,302 字节（mtime 2026-12-04 07:02:28）。
  - **新通道（第十一、十二个可复用口）**：① Georgetown —— `cs.georgetown.edu/graduate/ms/`、`/graduate/ms/curriculum/`、`/graduate/ms/how-to-apply/` 三页 200，一次拿到 30 学分结构、COSC 全课号、两季三档截止、GPA 3.0、TOEFL 90/新制 4.5、IELTS 7.0、2 封推荐信与 $90；McCourt 侧 `/ms-in-data-science-for-public-policy/` + `/admissions-tuition/` + `/admissions-tuition/how-to-apply/` + `/curriculum/` 四页 200，给三轮日期、$2,758/学分 × 39、GRE 新政与送分码、PPOL 全课号；学费统一由 `studentaccounts.georgetown.edu/tuition/graduate/` 提供（2026-2027：$2,758/学分、论文研究 $2,750/学期、活动费 $27/学期、医保 $4,450/年）。② Emory —— 系站真名 `computerscience.emory.edu`（`www.cs.emory.edu` 302 过去，且证书链 UntrustedRoot，PowerShell 直接失败，须 python requests verify=False）；MS 侧 `/graduate-ms/{index,cs-ms-programs,admissions/index,admissions/ms-faq}.html` 四页给课程、三条结题路径、春秋两季各三档日期、85/90 语言线、$75 与 5187 机构码、2024-25 学费；博士侧 `/graduate-phd/{index,admissions/index,admissions/phd-faqs}.html` 给 3 核心 + 2 次 rotation、平均 5 年、12 月 15 日、仅秋季、CGS 4-15；`gs.emory.edu/funding/` 给 FY26-27 stipend $43,260（SEIU 协议）。
  - **实抓口径（择要）**：Georgetown MS CS 对背景是「推荐」非硬性（CS 或紧密相关 + 大量课程/职业经历），希望具备数据结构与算法、硬件与体系结构、编程、离散/概率统计/线代/微积分，通常 GPA 3.0，跨专业建议先读 Post-Bacc；30 学分只 2 门核心，广度课跨 3 领域，论文轨 24 学分 + 提案 3 + 论文研究 3 并答辩；最多 2 门外部选修（DC 联盟校）；UNXD 7950 / COSC 7950 实习课，F-1 需 CPT；常开课号 COSC 4550/5521/5260/5290/5295/5402/5610/5750/4410/5510/4850/5540/5480/5760/5470。DSPP 39 学分 = 6 + 9 + 15 + 1.5 + 1.5 + ≥6，课号 PPOL 5004/5006/5007/5008/5009/5200–5208/6810，Fall 2027 三轮 2026-12-01 / 2027-01-15 / 2027-03-01，自 Fall 2027 起 GRE 不再必需但强烈建议（GMAT 同收、均无最低分），TOEFL 100/新制 5、IELTS 7.0 严格门槛，2025 届 94% 就业或升学、薪资中位数 $103.2k。Emory CS MS 3 核心 + 4 选修 + 9 学分结题（项目/课程/论文三轨，CS 597R 6 学分或 CS 599R 9 学分）+ 必修 practicum，GPA ≥3.0、单科 C 以上；CSI PhD 由 CS + 生物统计与生物信息 + 生物医学信息三系联合，CS 与 BMI 两 track，3 核心 + 4 选修 + 2 次 rotation + Ethics/Teaching 训练，平均 5 年，全额学费 + stipend $43,260。
  - **建页与验收**：4 页全部 `detail_render.py` + `_tpl_detail.html` 生成（spec 存 `_r384_specs/`），**4 条全 A 11/11**：MS CS（5,636 字 / 17 课号 / 6 金额 / 5 日期）、DSPP（5,315 / 35 / 4 / 10）、Emory CS MS（4,750 / 25 / 4 / 7）、Emory CSI PhD（4,436 / 18 / 3 / 5）。
  - **回填与校验**：PROGRAMS 回填 20 处（ddl/lang/tui/dur/test 各 4 条；`geo-cs-ms` 的 test 写「官网未单列 GRE/GMAT」、`geo-dspp-ms` 写「opt（Fall 2027 起不再必需但强烈建议）」、Emory 两条 notreq），备份 `index_main.html.bak_r384`。回填后 node exit 0、36 / 1439 / 2817、死链 0、残留 0、VD 1547 未进 0、details 3775；主文件 1,977,302 → 1,977,815 字节。
  - **密度**：A 383 → **387**、B 99（持平）、C 957 → **953**、需重做 1,056 → **1,052**（rows 1439）。
  - **新登记卡点 / 待核**：① `bge.georgetown.edu` DNS 失效，Georgetown BME 口径的生信与健康信息两个 MS（geo-bioinfo-ms、geo-hids-ms）本轮无法核验，留待替代域；② `mcgill.ca/compsci` 403 登录墙、`www.mcgill.ca/cs` 404、`study/2026-2027/*` 返回 202 空体，McGill 三条 CS/ECE 条目仍挂账；③ `mcgill.ca/ece/programs` 明写「ECE offers a Master of Engineering and a PhD」，未见 M.Sc. —— 现有 `mcg-msc-ece` 的 deg＝MSc 可能与官网冲突，登记待确认，不擅改；④ Emory 系站证书链问题（Invoke-WebRequest 不可用）；⑤ 本机 http.server 对新写入页偶发首查 404，复测即 200，验收以 `_r380_probe.py` 为准。
  - 本轮未推送、未贴链接、未输出汇报。

'''

led = '项目库扩充方案与进度台账.md'
txt = io.open(led, encoding='utf-8').read()
if 'r384 Georgetown 2 条' not in txt:
    idx = txt.find('\n## 八、')
    assert idx > 0
    txt = txt[:idx + 1] + ENTRY + txt[idx + 1:]
    io.open(led, 'w', encoding='utf-8').write(txt)
print('ledger bytes', len(io.open(led, encoding='utf-8').read().encode('utf-8')))
