-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflect.py
More file actions
executable file
·194 lines (150 loc) · 5.61 KB
/
reflect.py
File metadata and controls
executable file
·194 lines (150 loc) · 5.61 KB
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
#!/usr/bin/env python3
"""
reflect.py — lightweight post-pass: inject related References/ wikilinks into drafts.
Reads distill-draft.py JSON output from stdin ({"written": [{"path": ..., "title": ...}]}).
For each written draft, scans existing References/ notes, scores relevance by
keyword overlap, and appends up to MAX_LINKS [[wikilinks]] to ## 関連資料.
Deterministic and fast — no claude -p call, no network. Always exits 0 (fail-open).
Usage (from on-end-distill.sh):
echo "$WRITER_JSON" | SECOND_BRAIN_VAULT_PATH=... python3 reflect.py
"""
from __future__ import annotations
import json
import os
import pathlib
import re
import sys
MAX_LINKS = 5
REFERENCES_DIR = "References"
REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent
def warn(msg: str) -> None:
print(f"reflect: {msg}", file=sys.stderr)
def get_vault_path() -> pathlib.Path | None:
env = os.environ.get("SECOND_BRAIN_VAULT_PATH", "").strip()
if env:
p = pathlib.Path(env).expanduser()
return p if p.is_dir() else None
claude_md = REPO_ROOT / "CLAUDE.md"
if claude_md.exists():
for line in claude_md.read_text().splitlines():
m = re.match(r"^- Location: `(.+)`", line)
if m:
p = pathlib.Path(m.group(1).strip()).expanduser()
return p if p.is_dir() else None
return None
def tokenize(text: str) -> set[str]:
"""Extract lowercase words (>= 3 chars) from mixed Japanese/ASCII text."""
return {w.lower() for w in re.findall(r"[A-Za-z0-9]{3,}|[ぁ-ん一-龯]{2,}", text)}
def load_references(vault: pathlib.Path) -> list[dict]:
"""Load all References/ notes with stem, title, and topic for matching."""
refs_dir = vault / REFERENCES_DIR
if not refs_dir.exists():
return []
refs = []
for path in sorted(refs_dir.glob("*.md")):
stem = path.stem
title = stem
topic = ""
try:
text = path.read_text(encoding="utf-8")
m_title = re.search(r"^title:\s*(.+)$", text, re.MULTILINE)
m_topic = re.search(r"^topic:\s*(.+)$", text, re.MULTILINE)
if m_title:
title = m_title.group(1).strip()
if m_topic:
topic = m_topic.group(1).strip()
except OSError:
pass
refs.append({"stem": stem, "title": title, "topic": topic})
return refs
def score_relevance(draft_tokens: set[str], ref: dict) -> int:
ref_tokens = tokenize(f"{ref['stem']} {ref['title']} {ref['topic']}")
return len(draft_tokens & ref_tokens)
def find_related(draft_path: pathlib.Path, vault: pathlib.Path, refs: list[dict]) -> list[str]:
"""Return up to MAX_LINKS wikilink strings for the most relevant references."""
if not refs:
return []
try:
text = draft_path.read_text(encoding="utf-8")
except OSError:
return []
m_title = re.search(r"^title:\s*(.+)$", text, re.MULTILINE)
m_topic = re.search(r"^topic:\s*(.+)$", text, re.MULTILINE)
title = m_title.group(1).strip() if m_title else ""
topic = m_topic.group(1).strip() if m_topic else ""
# Include body sections (## 目的, ## 手順) for better recall
body_sections = re.findall(r"^##\s+(?:目的|手順|Purpose|Steps)\s*\n(.*?)(?=\n##|\Z)", text, re.MULTILINE | re.DOTALL)
body_text = " ".join(s.strip() for s in body_sections)
draft_tokens = tokenize(f"{title} {topic} {body_text}")
if not draft_tokens:
return []
scored = sorted(
((score_relevance(draft_tokens, ref), ref) for ref in refs),
key=lambda x: x[0],
reverse=True,
)
return [
f"[[References/{r['stem']}]]"
for score, r in scored[:MAX_LINKS]
if score > 0
]
def inject_links(draft_path: pathlib.Path, links: list[str]) -> bool:
"""Append wikilinks to ## 関連資料 section. Returns True on success."""
if not links:
return False
try:
text = draft_path.read_text(encoding="utf-8")
except OSError:
return False
# Filter out links already present; add only new ones
links = [link for link in links if link not in text]
if not links:
return False
link_block = "\n".join(f"- {link}" for link in links)
section = "## 関連資料"
if section in text:
idx = text.rindex(section) + len(section)
next_h2 = text.find("\n## ", idx)
insert_at = next_h2 if next_h2 != -1 else len(text)
text = text[:insert_at].rstrip("\n") + f"\n{link_block}\n" + text[insert_at:]
else:
text = text.rstrip("\n") + f"\n\n{section}\n{link_block}\n"
try:
draft_path.write_text(text, encoding="utf-8")
return True
except OSError:
return False
def main() -> int:
vault = get_vault_path()
if vault is None:
warn("vault not found — skipping")
return 0
raw = sys.stdin.read().strip()
if not raw:
return 0
try:
data = json.loads(raw)
except json.JSONDecodeError:
return 0
written = data.get("written", [])
if not written:
return 0
refs = load_references(vault)
if not refs:
return 0
injected = 0
for entry in written:
rel_path = entry.get("path", "")
if not rel_path:
continue
draft_path = vault / rel_path
if not draft_path.exists():
continue
links = find_related(draft_path, vault, refs)
if inject_links(draft_path, links):
injected += 1
if injected:
print(f"reflect: injected links into {injected} draft(s)", file=sys.stderr)
return 0
if __name__ == "__main__":
sys.exit(main())