#!/usr/bin/env python3 """분해 작업 재료를 `tech-log-tree.json` 으로 옮기고 폴더에서 지운다. `analysis/` 를 `final/document.md` 로 옮긴 것과 같은 일을 `tech-log-studio/` 에서 한다. 끝난 프로젝트의 `tech-log-studio/` 에는 `tech-log-tree.json` 과 기록 폴더만 있다. 옮기는 것 root-tree.md 사람이 읽는 트리 + Node Specifications → topics candidate-ledger.json 후보와 처분 → candidates root-tree-source-manifest.json 원본 해시 → ssotSha256 _meta/state.json 생성·편집·검증 이력 → history _meta/** 편집 과정 기록 → 지운다 (git 에 남는다) python3 scripts/fold-studio-contract-into-index.py <프로젝트> [--dry-run] [--keep] **옮기는 것이지 요약하는 것이 아니다.** 노드의 칸은 하나도 버리지 않는다. """ from __future__ import annotations import argparse import datetime import hashlib import json import os import re import shutil import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import techlog # noqa: E402 from techlog import KINDS # noqa: E402 ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) KIND_LABELS = {"CASE": "case", "CONCEPT": "concept", "REFERENCE": "reference", "OPEN QUESTION": "question", "QUESTION": "question", "DECISION": "decision"} FIELD_NAMES = ("slug", "readiness", "disposition", "source", "code", "evidence", "classification", "missing-verification", "relations", "scope", "exceptions", "known", "unknown", "next-verification", "decision-criterion", "decision-status", "decision-evidence", "grounds", "basis-version") MULTI = {"source", "code", "evidence", "relations", "grounds", "decision-evidence", "known", "unknown", "scope", "exceptions"} _TOPIC_HEAD = re.compile(r"^##\s+TOPIC(?:\s+\d+)?\s+[—-]\s+(.+?)\s*$") _SPEC_HEAD = re.compile(r"^###\s+(CASE|CONCEPT|REFERENCE|OPEN QUESTION|QUESTION|DECISION)\s+[—-]\s+(.+?)\s*$") _BRANCH = re.compile(r"^[├└]──\s+(CASE|CONCEPT|REFERENCE|OPEN QUESTION|QUESTION|DECISION)\s*$") _ITEM = re.compile(r"^(?:│|\s)\s{2,}[├└]──\s+(.+?)\s*$") _FIELD = re.compile(r"^-\s+([a-z][a-z-]*):\s*(.*)$") _CONT = re.compile(r"^\s{2,}-\s+(.+?)\s*$") _INLINE = re.compile(r"\s+·\s+(" + "|".join(FIELD_NAMES) + r"):\s*") _READER_Q = re.compile(r"^독자\s*질문\s*[—:-]\s*(.+?)\s*$") EMPTY = ("(추가 없음)", "(없음)", "(none)", "-") def _set(fields: dict, key: str, value: str) -> None: value = value.strip() if not value: fields.setdefault(key, []) return items = [v.strip() for v in value.split(" · ")] if key in MULTI else [value] fields[key] = [v for v in items if v] def parse_root_tree(path: str) -> dict: """사람이 읽는 트리와 Node Specifications 를 하나로 읽는다.""" text = open(path, encoding="utf-8").read() header, body = {}, text if text.startswith("---"): end = text.find("\n---", 3) for line in text[3:end].splitlines(): m = re.match(r"^([A-Za-z][A-Za-z0-9_]*):\s*(.*)$", line) if m: header[m.group(1)] = m.group(2).strip().strip('"') body = text[end + 4:] topics, specs, prose = [], [], [] in_specs = False topic = branch = spec = None spec_topic = "" pending = None expect = 0 for raw in body.splitlines(): line = raw.rstrip() stripped = line.strip() if line.startswith("# Node Specifications"): in_specs = True topic = branch = None continue if not in_specs: if stripped.startswith(">"): prose.append(stripped.lstrip("> ").rstrip()) continue if stripped == "---": continue if stripped == "PROJECT": expect = -1 continue if expect == -1: if stripped: expect = 0 continue if stripped == "TOPIC": topic = {"topic": "", "title": "", "readerQuestion": "", "nodes": []} topics.append(topic) branch, expect = None, 1 continue if topic is not None and expect in (1, 2, 3): if expect == 1 and stripped: topic["title"] = stripped expect = 2 continue if expect == 2 and stripped: topic["topic"] = stripped expect = 3 continue if expect == 3: m = _READER_Q.match(stripped) if m: topic["readerQuestion"] = m.group(1) expect = 0 continue if stripped: expect = 0 m = _BRANCH.match(stripped) if m: branch = KIND_LABELS[m.group(1)] continue m = _ITEM.match(line) if m and topic is not None and branch: title = m.group(1).strip() if title not in EMPTY: topic["nodes"].append({"kind": branch, "title": title}) continue m = _TOPIC_HEAD.match(line) if m: spec_topic, spec, pending = m.group(1).strip(), None, None continue m = _SPEC_HEAD.match(line) if m: spec = {"topic": spec_topic, "kind": KIND_LABELS[m.group(1)], "title": m.group(2).strip(), "fields": {}} specs.append(spec) pending = None continue if spec is None: continue m = _FIELD.match(line) if m: key, value = m.group(1), m.group(2).strip() parts = _INLINE.split(value) _set(spec["fields"], key, parts[0]) rest = parts[1:] while rest: _set(spec["fields"], rest[0], rest[1]) rest = rest[2:] pending = key if not rest else None continue m = _CONT.match(line) if m and pending: spec["fields"].setdefault(pending, []).append(m.group(1).strip()) continue if not stripped: pending = None return {"header": header, "topics": topics, "specs": specs, "prose": [p for p in prose if p]} def node_from_spec(spec: dict) -> dict: node = {"title": spec["title"], "kind": spec["kind"]} for key in ("slug", "readiness"): values = spec["fields"].get(key) or [] node[key] = values[0].strip("`").strip() if values else "" node["readiness"] = node["readiness"].upper() for key, values in spec["fields"].items(): if key in ("slug", "readiness") or not values: continue node[key] = values if key in MULTI or len(values) > 1 else values[0] return node def main() -> int: ap = argparse.ArgumentParser(description="분해 작업 재료를 tech-log-tree.json 으로 옮긴다.") ap.add_argument("project") ap.add_argument("--dry-run", action="store_true") ap.add_argument("--keep", action="store_true", help="옮기기만 하고 지우지 않는다") args = ap.parse_args() base = os.path.join(ROOT, "docs", args.project) studio = os.path.join(base, "tech-log-studio") tree_path = os.path.join(studio, "root-tree.md") index_path = os.path.join(studio, "tech-log-tree.json") if not os.path.exists(tree_path): print(f"{args.project}: root-tree.md 가 없다 — 이미 옮겼거나 분해 계약을 쓴 적이 없다") return 0 parsed = parse_root_tree(tree_path) header = parsed["header"] by_key = {(s["topic"], s["kind"], s["title"]): s for s in parsed["specs"]} topics = {} used = set() for t in parsed["topics"]: entry = {"topic": t["topic"], "title": t["title"], "readerQuestion": t["readerQuestion"], "kinds": {k: [] for k in KINDS}} for n in t["nodes"]: key = (t["topic"], n["kind"], n["title"]) spec = by_key.get(key) entry["kinds"][n["kind"]].append( node_from_spec(spec) if spec else {"title": n["title"], "kind": n["kind"], "slug": "", "readiness": ""}) used.add(key) topics[t["topic"]] = entry # 사람이 읽는 트리에 줄이 없던 Node Specification 도 잃지 않는다 orphans = 0 for key, spec in by_key.items(): if key in used: continue orphans += 1 entry = topics.setdefault(spec["topic"], { "topic": spec["topic"], "title": "", "readerQuestion": "", "kinds": {k: [] for k in KINDS}}) node = node_from_spec(spec) node["listedInTree"] = False entry["kinds"][spec["kind"]].append(node) ledger_path = os.path.join(studio, "candidate-ledger.json") ledger = json.load(open(ledger_path, encoding="utf-8")) if os.path.exists(ledger_path) else {} meta_state_path = os.path.join(studio, "_meta", "state.json") meta_state = json.load(open(meta_state_path, encoding="utf-8")) \ if os.path.exists(meta_state_path) else {} index = json.load(open(index_path, encoding="utf-8")) if os.path.exists(index_path) else {} ssot = header.get("sourceDocument", "final/document.md") out = { "schemaVersion": 4, "project": args.project, "ssot": ssot, "ssotSha256": techlog.sha256_of(os.path.join(base, ssot)), "sourceRevision": header.get("sourceRevision", ""), "generatedAt": datetime.date.today().isoformat(), "note": ("이 프로젝트의 글감 전부다. 분해 계약이자 색인이고, 이 파일이 정본이다. " "노드의 칸(readiness·source·classification·relations…)은 사람이 적고, " "file·publication·status 는 기록 파일에서 읽어 채운다 — " "python3 scripts/build-tech-log-tree.py <프로젝트>"), "contract": { "decomposition": parsed["prose"], "readinessValues": techlog.READINESS, "dispositionValues": ledger.get("dispositionValues", {}), }, "counts": {}, "topics": topics, "candidates": ledger.get("candidates", []), "history": {k: v for k, v in meta_state.items() if k not in ("schemaVersion", "project", "rootTreePath")}, } for key in ("counts", "unmapped", "explicitAnalysisCandidates", "cycle2", "cycle3", "conceptRecall", "migration", "fold"): if key in ledger: out["history"].setdefault("ledger", {})[key] = ledger[key] total = sum(len(v) for t in topics.values() for v in t["kinds"].values()) out["counts"] = {"topics": len(topics), "nodes": total, "candidates": len(out["candidates"])} print(f"{args.project}: 주제 {len(topics)} · 글감 {total} · 후보 {len(out['candidates'])}") if orphans: print(f" 사람이 읽는 트리에 줄이 없던 노드 {orphans}건은 listedInTree=false 로 옮겼다") if args.dry_run: print(" (--dry-run: 쓰지 않았다)") return 0 with open(index_path, "w", encoding="utf-8") as fh: json.dump(out, fh, ensure_ascii=False, indent=2) fh.write("\n") print(f" tech-log-tree.json {os.path.getsize(index_path):,} bytes") if not args.keep: for name in ("root-tree.md", "candidate-ledger.json", "root-tree-source-manifest.json"): path = os.path.join(studio, name) if os.path.exists(path): os.remove(path) shutil.rmtree(os.path.join(studio, "_meta"), ignore_errors=True) print(" root-tree.md · candidate-ledger.json · manifest · _meta/ 를 지웠다") return 0 if __name__ == "__main__": raise SystemExit(main())