Current task: —

← Session 5 — Phase 0: paper deep-read + Bombieri–Lagarias literature

#174 assistant 2026-08-18 03:04:00.316 UTC · 845→388 tok open ⤢
Now the Part III web log entry, derivations index, and db registration.
tool: Edit ok
input
{
  "file_path": "D:\\Python\\Quantum\\research\\derivations\\D-001-quantum-formalism-index.md",
  "new_string": "Analysis + plan: D-014.\n\n### 2026-08-17 \u2014 Session 003: Suzuki full-text re-fetch (arxiv.org/html/2606.09096v1)\nTaken: eq. 1.11 (W(a,\u03b8;z) via deficiency vectors v_\u00b1, (T_a v_\u00b1)(x) = e^{\u00b1x},\ndeficiency indices (1,1), Lemma 6.2); Thm 1.4 (\u03bb_a = log(1/a)+O(1), small a);\nCor 1.6 exact statement (\u03b8(a), \u03c6(a,z) choice; no distinguished \u03b8 given);\n\ud835\udc9f\u0304_{a,\u03b8} = extensions of first-order \ud835\udc9f_a in \u210b(T_a) (NOT of B_a); Weil\nfunctional rendering (\u039b(n)/n \u2014 superseded by our validated NT-1 conventions).\nUse: D-019. Note: Kim et al. 2607.24830 (3.1 sweep) reported \"lowest\neigenvalue positive, superexponentially decaying\" \u2014 our F1 \u03bb_a-cliff\n(D-019) independently reproduces that behavior.",
  "old_string": "Analysis + plan: D-014.",
  "replace_all": false
}
result 2026-08-18 03:04:00Z → 2026-08-27 17:00:51Z
{
  "filePath": "D:\\Python\\Quantum\\web\\dashboard\\research_data.py",
  "oldString": "def _script_experiments(stem):\n    return [r for r in get_experiments()\n            if r.get(\"script\") and Path(r[\"script\"]).stem == stem]\n\n\ndef script_list():\n    out = []\n    for p in sorted(SCRIPTS_DIR.glob(\"*.py\")):\n        rows = _script_experiments(p.stem)",
  "newString": "def _experiments_by_script():\n    by = {}\n    for r in get_experiments():\n        if r.get(\"script\"):\n            by.setdefault(Path(r[\"script\"]).stem, []).append(r)\n    return by\n\n\ndef script_list():\n    by = _experiments_by_script()\n    out = []\n    for p in sorted(SCRIPTS_DIR.glob(\"*.py\")):\n        rows = by.get(p.stem, [])",
  "originalFile": "\"\"\"\nRead-only access layer for the NGUYEN-RH-1 research archive.\n\nHARD RULE (RH-WEBSITE.md \u00a70): every handle opened here is READ-ONLY.\nSQLite is opened with mode=ro; files are opened 'r'. Nothing in this\nmodule may write to REPO_ROOT or RESEARCH_DIR.\n\"\"\"\nimport csv\nimport json\nimport re\nimport sqlite3\nimport subprocess\nfrom datetime import datetime, timezone\nfrom pathlib import Path\n\nimport markdown\nfrom django.conf import settings\n\nREPO = Path(settings.REPO_ROOT)\nRESEARCH = Path(settings.RESEARCH_DIR)\nDATA = RESEARCH / \"numerics\" / \"data\"\nDB = RESEARCH / \"research.db\"\n\nMAX_CHART_POINTS = 3000\nPREVIEW_ROWS = 25\n\n\n# ---------------------------------------------------------------- files\n\ndef read_text(path):\n    return Path(path).read_text(encoding=\"utf-8\", errors=\"replace\")\n\n\ndef mtime(path):\n    p = Path(path)\n    return p.stat().st_mtime if p.exists() else None\n\n\ndef _iso(ts):\n    return datetime.fromtimestamp(ts, tz=timezone.utc).isoformat(timespec=\"seconds\")\n\n\n# ---------------------------------------------------------------- sqlite (read-only)\n\ndef _query(sql, params=()):\n    con = sqlite3.connect(f\"file:{DB}?mode=ro\", uri=True)\n    try:\n        con.row_factory = sqlite3.Row\n        return [dict(r) for r in con.execute(sql, params)]\n    finally:\n        con.close()\n\n\ndef get_results():\n    return _query(\"SELECT * FROM results ORDER BY id\")\n\n\ndef get_experiments():\n    return _query(\"SELECT * FROM experiments ORDER BY id\")\n\n\ndef get_derivations():\n    return _query(\"SELECT * FROM derivations ORDER BY id\")\n\n\ndef get_graveyard():\n    return _query(\"SELECT * FROM graveyard ORDER BY id\")\n\n\ndef get_sessions():\n    return _query(\"SELECT * FROM sessions ORDER BY id\")\n\n\ndef get_result(rid):\n    rows = _query(\"SELECT * FROM results WHERE id = ?\", (rid,))\n    return rows[0] if rows else None\n\n\ndef get_experiment(eid):\n    rows = _query(\"SELECT * FROM experiments WHERE id = ?\", (eid,))\n    return rows[0] if rows else None\n\n\n# ---------------------------------------------------------------- STATE.md / NGUYEN.md\n\ndef parse_state():\n    \"\"\"Split STATE.md into its '## SECTION' blocks.\"\"\"\n    path = REPO / \"STATE.md\"\n    if not path.exists():\n        return {}\n    sections, current = {}, \"_header\"\n    for line in read_text(path).splitlines():\n        m = re.match(r\"^##\\s+(.*)\", line)\n        if m:\n            current = m.group(1).strip()\n            sections[current] = []\n        else:\n            sections.setdefault(current, []).append(line)\n    return {k: \"\\n\".join(v).strip() for k, v in sections.items()}\n\n\ndef parse_checklist():\n    \"\"\"Parse NGUYEN.md \u00a75 phases and their checkbox tasks.\"\"\"\n    path = REPO / \"NGUYEN.md\"\n    if not path.exists():\n        return {\"phases\": [], \"done\": 0, \"partial\": 0, \"open\": 0}\n    text = read_text(path)\n    m = re.search(r\"## 5\\..*?(?=\\n## 6\\.)\", text, re.S)\n    section = m.group(0) if m else text\n\n    phases, phase = [], None\n    totals = {\"done\": 0, \"partial\": 0, \"open\": 0}\n    for line in section.splitlines():\n        pm = re.match(r\"###\\s+(\u2705|\u2b1c|\ud83d\udd36)?\\s*(PHASE\\s+\\d+.*)\", line)\n        if pm:\n            phase = {\"name\": pm.group(2).strip(), \"marker\": pm.group(1) or \"\",\n                     \"done\": 0, \"partial\": 0, \"open\": 0}\n            phases.append(phase)\n            continue\n        tm = re.match(r\"- \\[(x|~| )\\]\", line)\n        if tm:\n            key = {\"x\": \"done\", \"~\": \"partial\", \" \": \"open\"}[tm.group(1)]\n            totals[key] += 1\n            if phase is not None:\n                phase[key] += 1\n    return {\"phases\": phases, **totals}\n\n\n# ---------------------------------------------------------------- markdown\n\n_MATH_RE = re.compile(\n    r\"(\\$\\$.*?\\$\\$|\\\\\\[.*?\\\\\\]|\\\\\\(.*?\\\\\\)|\\$[^$\\n]+?\\$)\", re.S)\n\n_LIST_ITEM_RE = re.compile(r\"^\\s*([-*+] |\\d+\\. )\")\n_BLOCK_START_RE = re.compile(r\"^\\s*(#|\\||>|[-*+] |\\d+\\. )\")\n\n\ndef _restore_interrupted_lists(text):\n    \"\"\"Insert a blank line before a list item that directly follows a\n    paragraph line.\n\n    python-markdown (with or without sane_lists) never lets a list\n    interrupt a paragraph: a ``- item`` line written right after a text\n    line renders as literal paragraph text. The archive writes such\n    lists (e.g. NGUYEN.md Phase 2 / 5B checklists after their note\n    paragraphs). Adding the missing blank line lets the list parse as a\n    list; visible text is unchanged. Fenced code blocks are skipped.\n    \"\"\"\n    out, prev, in_fence = [], \"\", False\n    for line in text.splitlines():\n        if line.lstrip().startswith((\"```\", \"~~~\")):\n            in_fence = not in_fence\n        elif (not in_fence and _LIST_ITEM_RE.match(line)\n                and prev.strip()               # prev not blank\n                and not prev[0].isspace()      # prev not an indented\n                                               # item-continuation (keeps\n                                               # existing lists tight)\n                and not _BLOCK_START_RE.match(prev)):\n            out.append(\"\")\n        out.append(line)\n        prev = line\n    return \"\\n\".join(out)\n\n\ndef render_markdown(text):\n    \"\"\"Render markdown, protecting $...$ math spans for MathJax.\"\"\"\n    store = []\n\n    def stash(m):\n        store.append(m.group(0))\n        return f\"@@MATH{len(store) - 1}@@\"\n\n    protected = _MATH_RE.sub(stash, _restore_interrupted_lists(text))\n    html = markdown.markdown(\n        protected, extensions=[\"tables\", \"fenced_code\", \"sane_lists\"])\n    for i, s in enumerate(store):\n        html = html.replace(f\"@@MATH{i}@@\", s)\n    return html\n\n\n# ---------------------------------------------------------------- markdown collections\n\nMD_COLLECTIONS = {\n    \"results\": (RESEARCH / \"results\", \".md\"),\n    \"derivations\": (RESEARCH / \"derivations\", \".md\"),\n    \"graveyard\": (RESEARCH / \"graveyard\", \".md\"),\n    \"journal\": (RESEARCH / \"journal\", \".md\"),\n    \"resources\": (RESEARCH / \"resources\", \".txt\"),\n}\n\ndef collection_list(kind):\n    folder, ext = MD_COLLECTIONS[kind]\n    out = []\n    for p in sorted(folder.glob(f\"*{ext}\")):\n        if p.name in (\"index.md\", \"index.txt\"):\n            continue\n        out.append({\n            \"name\": p.stem,\n            \"file\": p.name,\n            \"title\": _first_heading(p) or p.stem,\n            \"mtime\": _iso(p.stat().st_mtime),\n        })\n    return out\n\n\ndef _first_heading(path):\n    try:\n        for line in read_text(path).splitlines():\n            if line.startswith(\"# \"):\n                return line[2:].strip()\n            if line.strip():\n                break\n    except OSError:\n        pass\n    return None\n\n\ndef collection_item(kind, name):\n    \"\"\"Render one markdown file from a collection. Path-traversal safe:\n    security comes from the resolved-path containment check, not a regex.\"\"\"\n    folder, ext = MD_COLLECTIONS[kind]\n    path = (folder / (name + ext)).resolve()\n    if folder.resolve() not in path.parents or not path.exists():\n        return None\n    return {\n        \"name\": name,\n        \"title\": _first_heading(path) or name,\n        \"html\": render_markdown(read_text(path)),\n        \"mtime\": _iso(path.stat().st_mtime),\n    }\n\n\n# ---------------------------------------------------------------- experiments\n\ndef experiment_sidecar(exp_row):\n    \"\"\"Load the JSON metadata sidecar for an experiment row.\"\"\"\n    jf = exp_row.get(\"json_file\")\n    if not jf:\n        return None\n    path = (REPO / jf).resolve()\n    if REPO.resolve() not in path.parents or not path.exists():\n        return None\n    try:\n        return json.loads(read_text(path))\n    except (json.JSONDecodeError, OSError):\n        return None\n\n\ndef _csv_path(exp_row):\n    cf = exp_row.get(\"csv_file\")\n    if not cf:\n        return None\n    path = (REPO / cf).resolve()\n    if REPO.resolve() not in path.parents or not path.exists():\n        return None\n    return path\n\n\ndef csv_preview(exp_row, n=PREVIEW_ROWS):\n    path = _csv_path(exp_row)\n    if not path:\n        return None\n    with open(path, newline=\"\", encoding=\"utf-8\", errors=\"replace\") as fh:\n        rows = list(csv.reader(fh))\n    if not rows:\n        return None\n    return {\"header\": rows[0], \"rows\": rows[1:n + 1],\n            \"total\": max(0, len(rows) - 1)}\n\n\n# Chart configs: which CSV columns to plot per experiment.\n# y may be a list of column names; log_y switches the y axis to log scale.\nCHARTS = {\n    \"E-002\": {\"title\": \"First 10\u2074 zeta zero ordinates \u03b3\u2099\",\n              \"x\": \"n\", \"y\": [\"gamma_n\"]},\n    \"E-008\": {\"title\": \"Li coefficients \u03bb\u2099 (zero side, n \u2264 2000)\",\n              \"x\": \"n\", \"y\": [\"lambda_n\"]},\n    \"E-009b\": {\"title\": \"Galerkin spectrum \u03bb\u2c7c of Suzuki A_a (log scale)\",\n               \"x\": \"a\", \"y\": [\"lambda_j\"], \"log_y\": True},\n    \"E-011\": {\"title\": \"Li coefficients \u03bb\u2099 (arithmetic side, n \u2264 2000)\",\n              \"x\": \"n\", \"y\": [\"lambda_n\"]},\n    \"E-015\": {\"title\": \"Li coefficients \u03bb\u2099 (arithmetic side, n \u2264 3000)\",\n              \"x\": \"n\", \"y\": [\"lambda_n\"]},\n    \"E-017\": {\"title\": \"Thinning remainder |R_\u03c4| / |R\u2080| vs y (log scale)\",\n              \"x\": \"y\", \"y\": [\"ratio_Rtau_R0\"], \"log_y\": True},\n    \"E-018\": {\"title\": \"Descent profile |M| / |R\u2080| (log scale)\",\n              \"x\": \"x\", \"y\": [\"ratio_M_R0\"], \"log_y\": True},\n    \"E-021\": {\"title\": \"S\u221e(n) and S_f(n) curves\",\n              \"x\": \"n\", \"y\": [\"S_inf\", \"S_f\"]},\n    \"E-023\": {\"title\": \"Margin curve: traced vs measured constants\",\n              \"x\": \"n\", \"y\": [\"margin_traced\", \"margin_measured\"]},\n}\n\n\ndef chart_data(exp_row):\n    \"\"\"Return Plotly-ready series for an experiment, or None.\"\"\"\n    cfg = CHARTS.get(exp_row[\"id\"])\n    path = _csv_path(exp_row)\n    if not path:\n        return None\n    with open(path, newline=\"\", encoding=\"utf-8\", errors=\"replace\") as fh:\n        reader = csv.DictReader(fh)\n        fields = reader.fieldnames or []\n        if cfg is None:\n            nums = _numeric_columns(path, fields)\n            if len(nums) < 2:\n                return None\n            cfg = {\"title\": \"Data plot\", \"x\": nums[0], \"y\": nums[1:3]}\n        if cfg[\"x\"] not in fields:\n            return None\n        cols = {c: [] for c in [cfg[\"x\"], *cfg[\"y\"]]}\n        for row in reader:\n            for c in cols:\n                cols[c].append(row.get(c, \"\"))\n\n    n = len(cols[cfg[\"x\"]])\n    step = max(1, n // MAX_CHART_POINTS)\n    traces = []\n    for col in cfg[\"y\"]:\n        traces.append({\n            \"name\": col,\n            \"x\": _to_floats(cols[cfg[\"x\"]][::step]),\n            \"y\": _to_floats(cols[col][::step]),\n        })\n    return {\"title\": cfg.get(\"title\", \"Data plot\"),\n            \"traces\": traces,\n            \"log_y\": bool(cfg.get(\"log_y\")),\n            \"points\": n, \"shown\": len(traces[0][\"x\"]) if traces else 0}\n\n\ndef _numeric_columns(path, fields):\n    with open(path, newline=\"\", encoding=\"utf-8\", errors=\"replace\") as fh:\n        reader = csv.DictReader(fh)\n        sample = [row for _, row in zip(range(50), reader)]\n    nums = []\n    for f in fields:\n        if sample and all(_is_float(r.get(f, \"\")) for r in sample if r.get(f) != \"\"):\n            nums.append(f)\n    return nums\n\n\ndef _is_float(s):\n    try:\n        float(s)\n        return True\n    except (TypeError, ValueError):\n        return False\n\n\ndef _to_floats(values):\n    out = []\n    for v in values:\n        try:\n            out.append(float(v))\n        except (TypeError, ValueError):\n            out.append(None)\n    return out\n\n\n# ---------------------------------------------------------------- live runs / git\n\ndef run_logs():\n    out = []\n    for p in sorted(DATA.glob(\"*-run.log\")):\n        st = p.stat()\n        out.append({\"name\": p.name, \"size\": st.st_size,\n                    \"mtime\": st.st_mtime, \"mtime_iso\": _iso(st.st_mtime)})\n    return out\n\n\n_LOG_NAME = re.compile(r\"^[\\w-]+-run\\.log$\")\n\n\ndef tail_log(name, n=200):\n    \"\"\"Last n lines of a run log. Whitelisted to *-run.log inside DATA.\"\"\"\n    if not _LOG_NAME.match(name):\n        return None\n    path = (DATA / name).resolve()\n    if DATA.resolve() not in path.parents or not path.exists():\n        return None\n    lines = read_text(path).splitlines()\n    return {\"name\": name, \"lines\": lines[-n:], \"total\": len(lines),\n            \"mtime\": _iso(path.stat().st_mtime)}\n\n\ndef git_log(n=12):\n    try:\n        out = subprocess.run(\n            [\"git\", \"-C\", str(REPO), \"log\", \"--oneline\", \"-n\", str(n)],\n            capture_output=True, text=True, encoding=\"utf-8\",\n            errors=\"replace\", timeout=10)\n        # `or \"\"`: a decode failure inside subprocess's reader thread is\n        # swallowed and leaves out.stdout None \u2014 never 500 the page on that.\n        return [l for l in (out.stdout or \"\").splitlines() if l.strip()]\n    except (OSError, subprocess.SubprocessError):\n        return []\n\n\n# ---------------------------------------------------------------- agent conversation DB\n# Nguyen CLI conversation index. Outside the repo; READ-ONLY like everything else.\n\nAGENT_DB = Path.home() / \".nguyen\" / \"projects\" / \"D--Python-Quantum\" / \"agent.db\"\n\n\ndef _agent_query(sql, params=()):\n    con = sqlite3.connect(f\"file:{AGENT_DB}?mode=ro\", uri=True)\n    try:\n        con.row_factory = sqlite3.Row\n        return [dict(r) for r in con.execute(sql, params)]\n    finally:\n        con.close()\n\n\ndef agent_available():\n    return AGENT_DB.exists()\n\n\ndef agent_sessions():\n    return _agent_query(\n        \"SELECT id, session_id, title, model, provider, message_count,\"\n        \" prompt_tokens, completion_tokens, total_cost_usd, created_at,\"\n        \" updated_at FROM sessions ORDER BY id\")\n\n\ndef agent_session(spk):\n    rows = _agent_query(\"SELECT * FROM sessions WHERE id = ?\", (spk,))\n    return rows[0] if rows else None\n\n\ndef _fmt_ts(ts):\n    if not ts:\n        return \"\"\n    ts = ts.replace(\"T\", \" \").replace(\"+00:00\", \" UTC\")\n    return ts[:23] + (\" UTC\" if ts.endswith(\"UTC\") and \"UTC\" not in ts[:23] else \"\")\n\n\ndef _decorate_message(row, tool_map=None):\n    \"\"\"Split a message row into display blocks (thinking / text / tool).\"\"\"\n    blocks = []\n    cj = row.get(\"content_json\") or \"\"\n    if cj:\n        try:\n            for b in json.loads(cj):\n                t = b.get(\"type\")\n                if t == \"thinking\":\n                    blocks.append({\"type\": \"thinking\",\n                                   \"text\": b.get(\"thinking\", \"\")})\n                elif t == \"text\":\n                    blocks.append({\"type\": \"text\",\n                                   \"text\": b.get(\"text\", \"\")})\n                elif t == \"tool_use\":\n                    call = (tool_map or {}).get(b.get(\"id\"))\n                    blocks.append({\n                        \"type\": \"tool\",\n                        \"name\": b.get(\"name\", \"?\"),\n                        \"tool_use_id\": b.get(\"id\", \"\"),\n                        \"input\": json.dumps(b.get(\"input\"), indent=2,\n                                            default=str),\n                        \"call\": call,\n                    })\n        except json.JSONDecodeError:\n            pass\n    if not blocks and row.get(\"content\"):\n        blocks.append({\"type\": \"text\", \"text\": row[\"content\"]})\n    return {\n        \"id\": row[\"id\"],\n        \"role\": row.get(\"role\") or \"?\",\n        \"created\": _fmt_ts(row.get(\"created_at\", \"\")),\n        \"model\": row.get(\"model\") or \"\",\n        \"prompt_tokens\": row.get(\"prompt_tokens\"),\n        \"completion_tokens\": row.get(\"completion_tokens\"),\n        \"blocks\": blocks,\n        \"kinds\": {b[\"type\"] for b in blocks},\n        \"raw_json\": cj,\n    }\n\n\ndef _tool_map(spk):\n    out = {}\n    for tc in _agent_query(\n            \"SELECT tool_use_id, tool_name, arguments_json, result_json,\"\n            \" success, error, started_at, completed_at FROM tool_calls\"\n            \" WHERE session_pk = ?\", (spk,)):\n        result = tc.get(\"result_json\") or \"\"\n        try:\n            parsed = json.loads(result) if result else {}\n            if isinstance(parsed, dict):\n                result = parsed.get(\"stdout\", \"\") or json.dumps(parsed, indent=2, default=str)\n            else:\n                result = str(parsed)\n        except json.JSONDecodeError:\n            pass\n        tc[\"result_text\"] = result\n        tc[\"args_text\"] = json.dumps(json.loads(tc[\"arguments_json\"]), indent=2) \\\n            if tc.get(\"arguments_json\") else \"\"\n        tc[\"started\"] = _fmt_ts(tc.get(\"started_at\", \"\"))\n        tc[\"completed\"] = _fmt_ts(tc.get(\"completed_at\", \"\"))\n        if tc.get(\"tool_use_id\"):\n            out[tc[\"tool_use_id\"]] = tc\n    return out\n\n\ndef agent_messages(spk, q=None, kind=None):\n    \"\"\"All messages of a session, decorated; optional FTS search + kind filter.\"\"\"\n    tmap = _tool_map(spk)\n    if q:\n        phrase = '\"' + q.replace('\"', '\"\"') + '\"'\n        try:\n            rows = _agent_query(\n                \"SELECT m.* FROM messages m\"\n                \" JOIN messages_fts f ON f.rowid = m.id\"\n                \" WHERE messages_fts MATCH ? AND m.session_pk = ?\"\n                \" ORDER BY m.id\", (phrase, spk))\n        except sqlite3.OperationalError:\n            rows = []\n    else:\n        rows = _agent_query(\n            \"SELECT * FROM messages WHERE session_pk = ? ORDER BY id\", (spk,))\n    out = [_decorate_message(r, tmap) for r in rows]\n    if kind in (\"user\", \"thinking\", \"text\", \"tool\"):\n        if kind == \"user\":\n            out = [m for m in out if m[\"role\"] == \"user\"]\n        else:\n            out = [m for m in out if kind in m[\"kinds\"]]\n    return out\n\n\ndef agent_message(spk, mid):\n    rows = _agent_query(\n        \"SELECT * FROM messages WHERE session_pk = ? AND id = ?\", (spk, mid))\n    if not rows:\n        return None\n    tmap = _tool_map(spk)\n    msg = _decorate_message(rows[0], tmap)\n    prev = _agent_query(\n        \"SELECT id FROM messages WHERE session_pk = ? AND id < ?\"\n        \" ORDER BY id DESC LIMIT 1\", (spk, mid))\n    nxt = _agent_query(\n        \"SELECT id FROM messages WHERE session_pk = ? AND id > ?\"\n        \" ORDER BY id ASC LIMIT 1\", (spk, mid))\n    pos = _agent_query(\n        \"SELECT COUNT(*) AS c FROM messages WHERE session_pk = ? AND id <= ?\",\n        (spk, mid))[0][\"c\"]\n    total = _agent_query(\n        \"SELECT COUNT(*) AS c FROM messages WHERE session_pk = ?\", (spk,))[0][\"c\"]\n    msg[\"prev_id\"] = prev[0][\"id\"] if prev else None\n    msg[\"next_id\"] = nxt[0][\"id\"] if nxt else None\n    msg[\"position\"] = pos\n    msg[\"total\"] = total\n    return msg\n\n\n# ---------------------------------------------------------------- global search\n\n_SEARCH_MAX_Q = 200\n_SEARCH_MAX_FILES = 100\n_SEARCH_MAX_SNIPPETS = 3\n_SEARCH_WINDOW = 90  # context chars each side of a match\n\n\ndef _snippets(text, terms):\n    \"\"\"Up to _SEARCH_MAX_SNIPPETS (line_no, before, match, after) raw-text\n    tuples around the first well-separated match positions. HTML-escaping\n    is the template's job (autoescape).\"\"\"\n    low = text.lower()\n    positions = []\n    for t in terms:\n        start = 0\n        while True:\n            i = low.find(t, start)\n            if i < 0:\n                break\n            positions.append((i, len(t)))\n            start = i + len(t)\n    positions.sort()\n    out, last_end = [], -10 ** 9\n    for i, ln in positions:\n        if i < last_end + _SEARCH_WINDOW:\n            continue\n        out.append((text.count(\"\\n\", 0, i) + 1,\n                    text[max(0, i - _SEARCH_WINDOW):i],\n                    text[i:i + ln],\n                    text[i + ln:i + ln + _SEARCH_WINDOW]))\n        last_end = i + ln\n        if len(out) >= _SEARCH_MAX_SNIPPETS:\n            break\n    return out\n\n\ndef search_archive(q):\n    \"\"\"Global case-insensitive search across the markdown collections\n    (RH-WEBSITE.md \u00a77 item 1 \u2014 index-free scan-on-query).\n\n    The query is split into whitespace terms; a file matches when EVERY\n    term appears in its text, title, or filename (AND semantics).\n    Results are grouped by collection; hits in ``results`` link to the\n    ledger detail page. Read-only; every file is opened 'r' per query.\n    \"\"\"\n    q = (q or \"\").strip()[:_SEARCH_MAX_Q]\n    if not q:\n        return None\n    terms = q.lower().split()\n    ledger_urls = {Path(r[\"file\"]).stem: r[\"id\"] for r in get_results()}\n    results, total, truncated = {}, 0, False\n    for kind, (folder, ext) in MD_COLLECTIONS.items():\n        hits = []\n        for p in sorted(folder.glob(f\"*{ext}\")):\n            if p.name in (\"index.md\", \"index.txt\"):\n                continue\n            text = read_text(p)\n            low = text.lower()\n            title = _first_heading(p) or p.stem\n            if not all(t in low or t in title.lower() or t in p.stem.lower()\n                       for t in terms):\n                continue\n            if kind == \"results\" and p.stem in ledger_urls:\n                url = f\"/ledger/{ledger_urls[p.stem]}/\"\n            else:\n                url = f\"/{kind}/{p.stem}/\"\n            hits.append({\"name\": p.stem, \"title\": title, \"url\": url,\n                         \"snippets\": _snippets(text, terms)})\n            total += 1\n            if total >= _SEARCH_MAX_FILES:\n                truncated = True\n                break\n        if hits:\n            results[kind] = hits\n        if truncated:\n            break\n    return {\"q\": q, \"terms\": terms, \"results\": results,\n            \"total\": total, \"truncated\": truncated}\n\n\n# ---------------------------------------------------------------- scripts & their data\n\nSCRIPTS_DIR = RESEARCH / \"numerics\" / \"scripts\"\nCSV_PER_PAGE = 500\n\n\ndef _script_data_files(stem, exp_rows):\n    \"\"\"Data files (research/numerics/data) belonging to a script: the\n    experiment-table links (authoritative) UNION exact-stem matches.\"\"\"\n    files, seen = [], set()\n\n    def add(path):\n        path = Path(path)\n        key = str(path).lower()\n        if key in seen or not path.exists():\n            return\n        seen.add(key)\n        files.append({\"name\": path.name, \"path\": path,\n                      \"kind\": path.suffix.lstrip(\".\"),\n                      \"size\": path.stat().st_size,\n                      \"mtime\": _iso(path.stat().st_mtime)})\n\n    for row in exp_rows:\n        for k in (\"csv_file\", \"json_file\"):\n            if row.get(k):\n                p = (REPO / row[k]).resolve()\n                if REPO.resolve() in p.parents:\n                    add(p)\n    for ext in (\".csv\", \".json\"):\n        for q in sorted(DATA.glob(stem + ext)):\n            add(q.resolve())\n    files.sort(key=lambda f: (f[\"kind\"] != \"csv\", f[\"name\"]))  # csv first\n    return files\n\n\ndef _script_experiments(stem):\n    return [r for r in get_experiments()\n            if r.get(\"script\") and Path(r[\"script\"]).stem == stem]\n\n\ndef script_list():\n    out = []\n    for p in sorted(SCRIPTS_DIR.glob(\"*.py\")):\n        rows = _script_experiments(p.stem)\n        out.append({\n            \"name\": p.stem, \"file\": p.name,\n            \"experiments\": [r[\"id\"] for r in rows],\n            \"data_files\": [f[\"name\"] for f in _script_data_files(p.stem, rows)],\n            \"size\": p.stat().st_size,\n            \"mtime\": _iso(p.stat().st_mtime),\n        })\n    return out\n\n\ndef script_item(stem):\n    \"\"\"One script + its data files. Path-traversal safe via the resolved-\n    path containment check (same pattern as collection_item).\"\"\"\n    path = (SCRIPTS_DIR / (stem + \".py\")).resolve()\n    if SCRIPTS_DIR.resolve() not in path.parents or not path.exists():\n        return None\n    rows = _script_experiments(stem)\n    return {\"name\": stem, \"file\": path.name,\n            \"experiments\": rows,\n            \"files\": _script_data_files(stem, rows),\n            \"mtime\": _iso(path.stat().st_mtime)}\n\n\ndef csv_page(path, page, per=CSV_PER_PAGE):\n    \"\"\"One page of a CSV as a spreadsheet grid: header, numbered rows,\n    right-align flags for numeric columns.\"\"\"\n    with open(path, newline=\"\", encoding=\"utf-8\", errors=\"replace\") as fh:\n        rows = list(csv.reader(fh))\n    if not rows:\n        return None\n    header, body = rows[0], rows[1:]\n    total = len(body)\n    pages = max(1, -(-total // per))\n    page = min(max(1, page), pages)\n    chunk = body[(page - 1) * per: page * per]\n    numeric = []\n    for ci in range(len(header)):\n        vals = [r[ci] for r in chunk[:50] if ci < len(r) and r[ci] != \"\"]\n        numeric.append(bool(vals) and all(_is_float(v) for v in vals))\n    return {\"header\": header, \"rows\": chunk, \"numeric\": numeric,\n            \"total\": total, \"page\": page, \"pages\": pages, \"per\": per,\n            \"first_line\": (page - 1) * per + 2}  # CSV line no. (header = 1)\n\n\ndef json_pretty(path, cap=200_000):\n    try:\n        text = json.dumps(json.loads(read_text(path)),\n                          indent=2, ensure_ascii=False)\n    except (json.JSONDecodeError, OSError):\n        return None\n    if len(text) > cap:\n        return text[:cap] + f\"\\n\u2026 (truncated at {cap:,} chars)\"\n    return text\n\n\ndef summary_stats():\n    return {\n        \"results\": len(get_results()),\n        \"experiments\": len(get_experiments()),\n        \"derivations\": len(get_derivations()),\n        \"graveyard\": len(get_graveyard()),\n        \"sessions\": len(get_sessions()),\n        \"checklist\": parse_checklist(),\n    }\n",
  "structuredPatch": [
    {
      "oldStart": 666,
      "oldLines": 15,
      "newStart": 666,
      "newLines": 19,
      "lines": [
        "     return files",
        " ",
        " ",
        "-def _script_experiments(stem):",
        "-    return [r for r in get_experiments()",
        "-            if r.get(\"script\") and Path(r[\"script\"]).stem == stem]",
        "+def _experiments_by_script():",
        "+    by = {}",
        "+    for r in get_experiments():",
        "+        if r.get(\"script\"):",
        "+            by.setdefault(Path(r[\"script\"]).stem, []).append(r)",
        "+    return by",
        " ",
        " ",
        " def script_list():",
        "+    by = _experiments_by_script()",
        "     out = []",
        "     for p in sorted(SCRIPTS_DIR.glob(\"*.py\")):",
        "-        rows = _script_experiments(p.stem)",
        "+        rows = by.get(p.stem, [])",
        "         out.append({",
        "             \"name\": p.stem, \"file\": p.name,",
        "             \"experiments\": [r[\"id\"] for r in rows],"
      ]
    }
  ],
  "userModified": false,
  "replaceAll": false
}
raw content_json (database row)
[{"type":"text","text":"Now the Part III web log entry, derivations index, and db registration."},{"type":"tool_use","id":"Edit_75","name":"Edit","input":{"file_path":"D:\\Python\\Quantum\\research\\derivations\\D-001-quantum-formalism-index.md","new_string":"Analysis + plan: D-014.\n\n### 2026-08-17 — Session 003: Suzuki full-text re-fetch (arxiv.org/html/2606.09096v1)\nTaken: eq. 1.11 (W(a,θ;z) via deficiency vectors v_±, (T_a v_±)(x) = e^{±x},\ndeficiency indices (1,1), Lemma 6.2); Thm 1.4 (λ_a = log(1/a)+O(1), small a);\nCor 1.6 exact statement (θ(a), φ(a,z) choice; no distinguished θ given);\n𝒟̄_{a,θ} = extensions of first-order 𝒟_a in ℋ(T_a) (NOT of B_a); Weil\nfunctional rendering (Λ(n)/n — superseded by our validated NT-1 conventions).\nUse: D-019. Note: Kim et al. 2607.24830 (3.1 sweep) reported \"lowest\neigenvalue positive, superexponentially decaying\" — our F1 λ_a-cliff\n(D-019) independently reproduces that behavior.","old_string":"Analysis + plan: D-014.","replace_all":false},"extra_content":null}]