← Session 6 — Recover crashed run; plan all-forks execution
thinking (212 chars)
Bug: after filtering `pk = pk[pk <= n_max]`, the next multiplication uses full `primes` — shapes mismatch. Fix: keep a parallel lp/primes truncated array: pk, lp both shrink; multiply pk * primes[:pk.size]. Edit.
Broadcast bug in the prime-power loop — the filtered `pk` no longer matches `primes`. Fixing.
tool: Edit ok
input
{
"file_path": "D:\\Python\\Quantum\\research\\numerics\\scripts\\E-014-cor16-diagnostics.py",
"old_string": " primes = np.flatnonzero(~is_comp[2:]) + 2\n lp = np.log(primes)\n pk = primes.copy()\n while pk.size and pk[0] <= n_max:\n lam_log[pk] = lp\n pk = pk * primes\n pk = pk[pk <= n_max]\n lp = lp[: pk.size]\n return lam_log",
"new_string": " primes = np.flatnonzero(~is_comp[2:]) + 2\n lp = np.log(primes)\n pk = primes.copy()\n while pk.size and pk[0] <= n_max:\n lam_log[pk] = lp\n pk = pk * primes[: pk.size]\n keep = pk <= n_max\n pk = pk[keep]\n lp = lp[keep]\n return lam_log"
}
result 2026-08-18 08:22:56Z → 2026-08-28 22:28:55Z
{
"filePath": "D:\\Python\\Quantum\\web\\dashboard\\research_data.py",
"oldString": "_TASK_MARK_RE = re.compile(r\"(<li>\\s*(?:<p>)?)(?:\\[(x|~| )\\]|(\u2611))\")\n_TASK_MARK_CLASS = {\"x\": \"x\", \"~\": \"tilde\", \" \": \"open\"}\n\n\ndef _task_mark_span(m):\n \"\"\"Wrap a leading checklist marker in a styled span (display-only;\n the marker text itself stays verbatim).\"\"\"\n if m.group(3) is not None: # \u2611 done \u2014 blue box, same size as [~]/[ ]\n return m.group(1) + '<span class=\"tkm tkm-check\">\u2611</span>'\n cls = _TASK_MARK_CLASS[m.group(2)]\n return (m.group(1) + '<span class=\"tkm tkm-' + cls + '\">['\n + m.group(2) + \"]</span>\")\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 _TASK_MARK_RE.sub(_task_mark_span, html)",
"newString": "def 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",
"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|~| )\\]|(\u2705|\ud83d\udd36|\u2b1c|\u2611|\ud83d\udd38|\u2610))\", line)\n if tm:\n if tm.group(1) is not None:\n key = {\"x\": \"done\", \"~\": \"partial\", \" \": \"open\"}[tm.group(1)]\n else:\n key = {\"\u2705\": \"done\", \"\u2611\": \"done\",\n \"\ud83d\udd36\": \"partial\", \"\ud83d\udd38\": \"partial\",\n \"\u2b1c\": \"open\", \"\u2610\": \"open\"}[tm.group(2)]\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\n_TASK_MARK_RE = re.compile(r\"(<li>\\s*(?:<p>)?)(?:\\[(x|~| )\\]|(\u2611))\")\n_TASK_MARK_CLASS = {\"x\": \"x\", \"~\": \"tilde\", \" \": \"open\"}\n\n\ndef _task_mark_span(m):\n \"\"\"Wrap a leading checklist marker in a styled span (display-only;\n the marker text itself stays verbatim).\"\"\"\n if m.group(3) is not None: # \u2611 done \u2014 blue box, same size as [~]/[ ]\n return m.group(1) + '<span class=\"tkm tkm-check\">\u2611</span>'\n cls = _TASK_MARK_CLASS[m.group(2)]\n return (m.group(1) + '<span class=\"tkm tkm-' + cls + '\">['\n + m.group(2) + \"]</span>\")\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 _TASK_MARK_RE.sub(_task_mark_span, 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\n_JOURNAL_TITLE_RE = re.compile(r\"^(Session\\s+\\S+)\\s*\\(([^)]+)\\)\")\n\n\ndef _journal_title(title):\n \"\"\"Display-only normalization: 'Session 011 (2026-08-19) \u2014 \u2026' \u2192\n 'Session 011 \u2014 2026-08-19 \u2014 \u2026' so all journal rows read alike.\n The archive files are never modified (RH-WEBSITE.md \u00a70); the detail\n page body still renders the original heading verbatim.\"\"\"\n return _JOURNAL_TITLE_RE.sub(\n lambda m: m.group(1) + \" \u2014 \" + m.group(2), title, count=1)\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 title = _first_heading(p) or p.stem\n if kind == \"journal\":\n title = _journal_title(title)\n out.append({\n \"name\": p.stem,\n \"file\": p.name,\n \"title\": title,\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 title = _first_heading(path) or name\n if kind == \"journal\":\n title = _journal_title(title)\n return {\n \"name\": name,\n \"title\": title,\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 _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, [])\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 = _experiments_by_script().get(stem, [])\n return {\"name\": stem, \"file\": path.name,\n \"experiments\": rows,\n \"files\": _script_data_files(stem, rows),\n \"source\": read_text(path),\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 grid = [[{\"v\": cell, \"num\": ci < len(numeric) and numeric[ci]}\n for ci, cell in enumerate(row)] for row in chunk]\n return {\"header\": header, \"rows\": grid,\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\n# ---------------------------------------------------------------- Lean formalization\n\nLEAN_ROOT = RESEARCH / \"lean\" / \"NguyenRH1\"\nLEAN_SRC = LEAN_ROOT / \"NguyenRH1\"\n\n_DECL_LINE_RE = re.compile(\n r\"^(theorem|lemma|def|structure|class|instance|abbrev)\\s+(\\S+)\")\n_SORRY_RE = re.compile(r\"\\bby\\s+sorry\\b|:=\\s*sorry\\b\")\n\n\ndef _lean_decls(text):\n out = []\n for i, line in enumerate(text.splitlines(), 1):\n m = _DECL_LINE_RE.match(line)\n if m:\n out.append({\"kind\": m.group(1), \"name\": m.group(2), \"line\": i})\n return out\n\n\ndef lean_toolchain():\n p = LEAN_ROOT / \"lean-toolchain\"\n return read_text(p).strip() if p.exists() else \"\"\n\n\ndef _lean_file_row(p):\n text = read_text(p)\n return {\"name\": p.stem, \"file\": p.name,\n \"lines\": text.count(\"\\n\") + 1,\n \"decls\": len(_lean_decls(text)),\n \"sorries\": len(_SORRY_RE.findall(text)),\n \"mtime\": _iso(p.stat().st_mtime)}\n\n\ndef lean_files():\n return [_lean_file_row(p) for p in sorted(LEAN_SRC.glob(\"*.lean\"))]\n\n\ndef lean_audits():\n out = []\n for p in sorted(LEAN_ROOT.glob(\"audit-session*-axioms.txt\")):\n text = read_text(p)\n out.append({\"name\": p.stem, \"file\": p.name,\n \"decls\": text.count(\"depends on axioms\"),\n \"sorries\": text.count(\"sorryAx\"),\n \"clean\": \"sorryAx\" not in text,\n \"mtime\": _iso(p.stat().st_mtime)})\n return out\n\n\ndef lean_item(name):\n \"\"\"One Lean source file + parsed declarations. Containment-checked.\"\"\"\n path = (LEAN_SRC / (name + \".lean\")).resolve()\n if LEAN_SRC.resolve() not in path.parents or not path.exists():\n return None\n text = read_text(path)\n return {\"name\": name, \"file\": path.name, \"source\": text,\n \"lines\": text.count(\"\\n\") + 1,\n \"decls\": _lean_decls(text),\n \"sorries\": len(_SORRY_RE.findall(text)),\n \"mtime\": _iso(path.stat().st_mtime)}\n\n\ndef lean_audit_item(name):\n path = (LEAN_ROOT / (name + \".txt\")).resolve()\n if LEAN_ROOT.resolve() not in path.parents or not path.exists():\n return None\n text = read_text(path)\n return {\"name\": name, \"file\": path.name, \"text\": text,\n \"decls\": text.count(\"depends on axioms\"),\n \"sorries\": text.count(\"sorryAx\"),\n \"clean\": \"sorryAx\" not in text,\n \"mtime\": _iso(path.stat().st_mtime)}\n\n\ndef lean_closed_source():\n p = LEAN_SRC / \"LiClosed.lean\"\n return read_text(p) if p.exists() else \"\"\n\n\n_LEAN_PERIMETER = [\n (\"RHUpTo\", \"LiSkeleton\",\n \"Platt\u2013Trudgian computational certificate: RH verified to height T\u2080 = 3\u00d710\u00b9\u00b2.\"),\n (\"LiVerifiedUpTo\", \"LiSkeleton\",\n \"Finite Li-coefficient verification (our tables to n = 3000 + Johansson 2015 \u00a7 4.2 to 10\u2075).\"),\n (\"ZeroCountProp\", \"LiContour\",\n \"Trudgian N(T)/S(T): unit-window zero count, multiplicity-weighted.\"),\n (\"ZeroCountProp2\", \"LiContour\",\n \"Trudgian two-unit window specialization.\"),\n (\"ZeroCountGlobalProp\", \"LiContour\",\n \"Trudgian global N(T) bound.\"),\n (\"NoLowZeros\", \"LiContour\",\n \"Platt-class zero-location certificate: no zeros with |\u03b3| < 14.\"),\n (\"MZeroProp\", \"LiContour\",\n \"M\u2080 interval certificate (the low-|t| gap bound).\"),\n]\n\n\ndef _extract_lean_def(file_stem, def_name):\n \"\"\"The verbatim `def NAME` block (indented continuation lines included)\n + its 1-based start line.\"\"\"\n p = LEAN_SRC / (file_stem + \".lean\")\n if not p.exists():\n return None\n lines = read_text(p).splitlines()\n pat = re.compile(rf\"^(?:def|abbrev|structure|class)\\s+{re.escape(def_name)}\\b\")\n for i, line in enumerate(lines):\n if pat.match(line):\n block = [line]\n j = i + 1\n while j < len(lines) and (not lines[j].strip()\n or lines[j][0].isspace()):\n block.append(lines[j])\n j += 1\n while block and not block[-1].strip():\n block.pop()\n return {\"text\": \"\\n\".join(block), \"line\": i + 1}\n return None\n\n\ndef lean_perimeter():\n out = []\n for name, src, note in _LEAN_PERIMETER:\n d = _extract_lean_def(src, name) or {\"text\": \"(not found)\", \"line\": None}\n out.append({\"name\": name, \"file\": src + \".lean\", \"note\": note, **d})\n return out\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": 167,
"oldLines": 20,
"newStart": 167,
"newLines": 6,
"lines": [
" return \"\\n\".join(out)",
" ",
" ",
"-_TASK_MARK_RE = re.compile(r\"(<li>\\s*(?:<p>)?)(?:\\[(x|~| )\\]|(\u2611))\")",
"-_TASK_MARK_CLASS = {\"x\": \"x\", \"~\": \"tilde\", \" \": \"open\"}",
"-",
"-",
"-def _task_mark_span(m):",
"- \"\"\"Wrap a leading checklist marker in a styled span (display-only;",
"- the marker text itself stays verbatim).\"\"\"",
"- if m.group(3) is not None: # \u2611 done \u2014 blue box, same size as [~]/[ ]",
"- return m.group(1) + '<span class=\"tkm tkm-check\">\u2611</span>'",
"- cls = _TASK_MARK_CLASS[m.group(2)]",
"- return (m.group(1) + '<span class=\"tkm tkm-' + cls + '\">['",
"- + m.group(2) + \"]</span>\")",
"-",
"-",
" def render_markdown(text):",
" \"\"\"Render markdown, protecting $...$ math spans for MathJax.\"\"\"",
" store = []"
]
},
{
"oldStart": 194,
"oldLines": 7,
"newStart": 180,
"newLines": 7,
"lines": [
" protected, extensions=[\"tables\", \"fenced_code\", \"sane_lists\"])",
" for i, s in enumerate(store):",
" html = html.replace(f\"@@MATH{i}@@\", s)",
"- return _TASK_MARK_RE.sub(_task_mark_span, html)",
"+ return html",
" ",
" ",
" # ---------------------------------------------------------------- markdown collections"
]
}
],
"userModified": false,
"replaceAll": false
}
raw content_json (database row)
[{"type":"thinking","thinking":"Bug: after filtering `pk = pk[pk <= n_max]`, the next multiplication uses full `primes` — shapes mismatch. Fix: keep a parallel lp/primes truncated array: pk, lp both shrink; multiply pk * primes[:pk.size]. Edit.","signature":""},{"type":"text","text":"Broadcast bug in the prime-power loop — the filtered `pk` no longer matches `primes`. Fixing."},{"type":"tool_use","id":"Edit_137","name":"Edit","input":{"file_path":"D:\\Python\\Quantum\\research\\numerics\\scripts\\E-014-cor16-diagnostics.py","old_string":" primes = np.flatnonzero(~is_comp[2:]) + 2\n lp = np.log(primes)\n pk = primes.copy()\n while pk.size and pk[0] <= n_max:\n lam_log[pk] = lp\n pk = pk * primes\n pk = pk[pk <= n_max]\n lp = lp[: pk.size]\n return lam_log","new_string":" primes = np.flatnonzero(~is_comp[2:]) + 2\n lp = np.log(primes)\n pk = primes.copy()\n while pk.size and pk[0] <= n_max:\n lam_log[pk] = lp\n pk = pk * primes[: pk.size]\n keep = pk <= n_max\n pk = pk[keep]\n lp = lp[keep]\n return lam_log"},"extra_content":null}]