Polarize
GitHub Get in touch

Changelog

What changed in the code.

Merged pull requests across the public polarizetech repositories, newest first, synced from GitHub once a day. Every repository, with the posts about it, is under Work.

2026-11-21 scientific-research-scaffold #14

Use the profile's design system for visual work, and wire new apps to it

Scaffolded projects now use the profile’s design system for anything visual, and new apps actually load it.

Design-system setting (79de1b9)

  • Optional [design] table in profiles: name, package, repo, a pinned release tag (ref), and the URL of its own rules. Empty means nothing changes.
  • When set: an agent rule in AGENTS.md, a README Design section, a [design] table in the manifest (theme, allow_hex), and scaffold check warnings.
  • polarizetech profile pins polarize-ui.

New apps wired to it (59f4b42) New apps weren’t using the design system. Fixes:

  • serve.py reads design files at the pinned tag, not whatever branch the local checkout has out.
  • The stylesheet’s fonts are served; before, every app fell back to system fonts.
  • A missing design system shows a yellow banner with the fix instead of a quietly unstyled page.
  • Each new app’s README has a Design section; app.css is layout only.
  • <body> class comes from the profile’s body_class.
  • scaffold check warns about each HTML page that doesn’t load it or lacks the class (was repo-wide).
  • Pin moved to polarize-ui v0.6.7.

Design checks are warnings, not failures. UPGRADING.md has the by-hand steps for existing repos.

Tests: python3 -m unittest discover -v tests passes on Python 3.9 and 3.12 (105 tests, 2 skipped).

🤖 Generated with Claude Code

2026-11-20 retrieval-augmented-generation #65

Say how to restart the gateway where it runs

Why

Error replies (library and indexing failures in ask, the gateway’s tool errors, and the bridge’s “can’t reach the gateway”) ended with launchctl kickstart -k gui/$(id -u)/com.polarizetech.research-rag, the Mac’s LaunchAgent. The gateway has run on the droplet under systemd since 2026-11-17, so that advice was wrong, and today a session asked for the droplet to be restarted because of it.

What changes

  • research_mcp/hub/contract.py: restart_hint() reads GATEWAY_RESTART_HINT when a reply is made. Without the setting, it falls back to the Mac’s command (DEFAULT_RESTART_HINT). This replaces the hard-coded RESTART_HINT, which was duplicated in bridge.py.
  • research_mcp/ask.py, research_mcp/gateway.py: use restart_hint().
  • research_mcp/bridge.py: restart_advice(url) gives the local command when the gateway is on this machine, and otherwise says “restart it on , where it runs".
  • config/rag.env.example: documents the setting, with the droplet’s value.
  • Tests:
    • the hint follows the setting;
    • the bridge names a remote host;
    • the existing hint asserts now use restart_hint().

After merge

On the droplet, add GATEWAY_RESTART_HINT=sudo systemctl restart research-rag research-rag-public to config/rag.env, update the checkout, and restart both gateways.

Checks

535 tests pass, plus ruff check, ruff format and pyright (0 errors).

🤖 Generated with Claude Code

2026-11-20 retrieval-augmented-generation #66

Skip passage retrieval for code asks in the explore tier

Why

Prototype builds were slow. Across 238 scope-related sessions since Nov 10, rag__retrieve_evidence ran 6,144 times at a median of 26.5 s each (about 60 h), and ask ran 3,070 times at 13.5 s. Most of that time went to gathering evidence, not to writing code.

What

  • research_mcp/ask.py: a code-route ask with strictness='explore' skips passage retrieval. The progress line says “Retrieval: skipped (explore tier)”. grounded and publication are unchanged.
  • research_mcp/gateway.py: adds a description to the strictness schema property.
  • tests/test_ask.py: adds test_an_explore_code_task_skips_retrieval.

The client-side rules and guard for the explore tier live in ~/.claude, outside this repo, and are already in place.

Checks

  • uv run pytest: 534 passed
  • ruff check and ruff format --check: clean
  • pyright research_mcp/ask.py: 0 errors

The change takes effect on the droplet only after this is merged and deployed.

🤖 Generated with Claude Code

2026-11-20 retrieval-augmented-generation #67

Rerank on the Mac's GPU with PyTorch MPS

The Mac fallback reranked on the CPU (int8 onnxruntime), about 36 s for 50 passages under load, so projects hit the 40 s retrieval limit while Modal is out.

  • TorchReranker in deploy/models_service.py: bge-reranker-v2-m3 in PyTorch on MPS, chosen with RERANK_BACKEND=torch (RERANK_DEVICE, RERANK_TORCH_REPO). The ONNX path is unchanged and stays the default (Modal).
  • Docs bullet in docs/models-service.md.

Checked on the Mac: scores -4.239, -10.884, -11.041, identical to onnx/model.onnx; 50 passages in 3.7 s. ruff and tests/test_models_service.py pass.

Exploratory, in the user’s words: “Mark the GPU reranker exploratory and apply it”.

🤖 Generated with Claude Code

2026-11-20 retrieval-augmented-generation #64

Wake the Modal GPU only for asks that use the models

Why

November’s Modal spend reached about $40. The model app cost $15–16 a day from 18 November, against an estimate of about $27 a month. The droplet’s logs for 19 November show 1,946 tool calls spread over 21 of 24 hours.

The cause: every ask woke the hosted models and waited for them, even code: asks that never touch a model. Every session start woke them too. With a 10-minute idle window, an L4 stayed up about 20 hours that day.

What changes

  • research_mcp/gateway.py:
    • ask classifies the question first and wakes and waits for the models only when its mode uses them (MODEL_MODES: quick, research, fetch).
    • list_tools no longer wakes them at session start.
  • deploy/modal_models.py: scaledown_window goes from 600 to 300 s. After more than 5 idle minutes, the next ask waits for a cold start, about 30–50 s.
  • tests/test_remote_models.py: quick and fetch asks need the models; a code: ask doesn’t.

The automatic fallback to the Mac when Modal is out is in polarizetech/retrieval-augmented-generation#63. Both PRs edit deploy/modal_models.py, in different places.

After merge

  • Update the droplet (git ff, uv sync, restart both gateways).
  • Run modal deploy deploy/modal_models.py once the budget is back.

Checks

531 tests pass, plus ruff check, ruff format and pyright (0 errors).

🤖 Generated with Claude Code

2026-11-20 retrieval-augmented-generation #63

Fall back to the Mac's models when Modal is out

What this does

Modal’s model service ran out of budget and started answering HTTP 429 (“Input rate limit (0/s)”), which took down passage retrieval and the verifier. This adds an automatic fallback to the same models on the Mac.

  • deploy/models_service.py: the model service as one portable file. It serves Ollama’s API (bge-m3, bespoke-minicheck), plus /rerank for bge-reranker-v2-m3, behind the bearer key. Modal serves it on a GPU, and the Mac runs it with uv run next to its native Ollama. The reranker loads on first use and is dropped after 10 idle minutes. It’s loaded from real files in one folder, because onnxruntime refuses the hub cache’s symlinked model.onnx_data.
  • deploy/modal_models.py: serves that same app (add_local_file and create_app) instead of its own copy. The models and their scoring are unchanged.
  • deploy/models_router.py: runs on the droplet at 127.0.0.1:8895.
    • It tries Modal first. On a 402 or 429, or when Modal can’t be reached, it uses the Mac for MODELS_COOLDOWN_SECONDS (900), then tries Modal again.
    • GET /router/status says which side is serving.
    • Only the Mac-bound client uses MODELS_FALLBACK_PROXY (tailscaled’s loopback proxy; the droplet stays in userspace mode). Neither client reads proxy settings from the environment, so paper and web fetches can’t reach the tailnet.
  • tests/test_models_service.py: the router’s switching, and the reranker loading on use and dropping when idle.
  • docs/models-service.md: the set-up on each host.

Already live

  • The droplet runs the router from /home/rag/models-router/ until this merges. Its three model URLs point at the router, with .before-router backups kept.
  • The Mac runs the service, published tailnet-only on port 8896.
  • With Modal still answering 429, rag__retrieve_evidence and ask return passages through the Mac.

Not yet verified

  • modal deploy with the shared file: waits until the Modal budget is back.
  • minicheck through the fallback in a research run.

Checks

532 tests pass, plus ruff check, ruff format and pyright (0 errors).

🤖 Generated with Claude Code

2026-11-19 paper-fetch #16

Add reextract: re-run TEI extraction on stored originals

Why

#15 made xml_text keep GROBID display formulas, but only for papers fetched after it. index --refresh re-reads the stored fulltext.txt and never re-extracts, so older papers keep text with every display equation missing. Example: W2014360559 (Machura et al. 2007, fetched 2026-11-10) has no eq. 1 in the index, while W2057708301 (fetched 2026-11-19) does.

What

  • Library.reextract(identifiers=None, *, collection=None): for each held work with a stored fulltext.tei.xml, re-runs xml_text and the prose gate. If the text changed it rewrites fulltext.txt, updates provenance (sha256, text_method, text_gate, and a reextracted entry with date, old and new sha256), and re-indexes the work’s passages. Identical text: counted unchanged, nothing written. No stored TEI, not held, or gate failure: reported in skipped. Local only; the stored XML is never touched.
  • CLI: paper-fetch reextract [id ...] [--collection C]; one line in the CLI docstring and in CLAUDE.md.
  • Tests (written first): formula restored with provenance updated and the XML untouched; second run unchanged; no TEI skipped; the formula retrievable after re-indexing.

Source: housekeeping (re-runs the extraction from #15, which carries its sources).

Not in this PR

  • The catalogue row is not updated: a work whose row says full_text: false keeps that until a catalogue rebuild.
  • Inline maths GROBID itself garbles; preferring arXiv PDFs; filling ids.arxiv.

Checks

uv run pytest -q 390 passed; ruff check and ruff format --check clean; pyright 0 errors.

After merge

On the research-rag droplet: update paper-fetch, run paper-fetch reextract W2014360559, check eq. 1 retrieves verbatim, then run it for all tei-xml works.

🤖 Generated with Claude Code

2026-11-19 retrieval-augmented-generation #62

Add ask fetch mode and delegate/strict autonomy

Why

ask asked too many obvious questions (“fallback or stop?” whenever a component failed, “should I fetch/index this paper?”), and any request mentioning a paper ran the whole research pipeline.

What changed

Fetch mode

  • routing.py / config/routing.default.toml: new fetch mode. Prefixes fetch: and paper:, a [fetch] trigger list (“get the paper”, “index this paper”, …), or a DOI together with get/fetch/download/index/add (FETCH_VERBS). A DOI on its own (e.g. in a code task) does not route to fetch.
  • ask.py: fetch mode indexes DOIs and pmid: ids with rag__index_paper. A title is searched with papers__search (limit 3), the top hit is indexed, and the other two are listed as “if this isn’t the paper you meant”. No pipeline, no specialist, no fresh search.
  • gateway.py: call_for_ask routes the ask’s rag__index_paper call to the built-in evidence store (it isn’t an upstream tool). Adds fetch to the mode enum and a sentence to the server instructions.

Autonomy

  • hub/contract.py: AUTONOMY = ("delegate", "strict"), MUST_ASK = ("library", "verifier").
  • ask.py: ask(autonomy=...). Under delegate (the default) a failing component takes its labelled fallback and the degraded problem is recorded; library and verifier still ask; a session “stop” still wins. strict keeps today’s behaviour.
  • brief.py: under delegate, the answer format tells the assistant to fetch and index up to five fresh leads and retrieve again; the strict text is unchanged.
  • gateway.py: default from env ASK_AUTONOMY (default delegate); autonomy in ask’s inputSchema; delegate instruction sentence.
  • docs/mcp-clients.md: paragraphs on fetch mode and the two autonomy modes.

Notes for reviewers

  • Behaviour change on deploy: the default becomes delegate, so the reranker, embeddings, provider, github and corpus failures no longer stop to ask. Set ASK_AUTONOMY=strict on the droplet to keep the old behaviour.
  • arXiv ids and PMCIDs are not recognised yet (no pattern was specified).
  • tests/test_hub.py’s Rig.ask now defaults to autonomy="strict", since those tests cover asking; delegate is covered in the new tests/test_autonomy.py.
  • A title fetch uses the hit’s openalex work id when it has no DOI.

Checks

  • uv run pytest -q: 530 passed
  • uv run ruff check .: passed
  • uv run ruff format --check .: 109 files already formatted
  • uv run pyright: 0 errors (1 existing warning, psycopg import)

🤖 Generated with Claude Code

2026-11-19 polarize-ui #12

fix: emit --surface and --surface-2 for every theme

tokens.json defines surface and surface-2 for every theme, and .ui-readout cells, .ui-kpi, navbar links, legend keys and table cells use var(–surface)/var(–surface-2), but build_css.py never emitted them, so those components fell back to an unset colour (white readout cells on data-theme=polarize). Adds both variables to every theme block and a dev_check that fails on var(–x) used without a fallback and never defined. Not breaking: adds variables, renames or removes nothing.

🤖 Generated with Claude Code

2026-11-18 paper-fetch #15

Keep GROBID display formulas in TEI text

Problem

xml_text() built fulltext.txt from <p> elements only. GROBID writes display equations as <formula> elements beside the paragraphs (equation number in <label>), so every equation was dropped from the text and the passage index, and none could get an evidence id. Example: Nobukawa et al. 2019, doi:10.1587/transfun.2019eap1081 (W2998340499), equations (1)–(14).

Change

  • src/paper_fetch/text.py: walk the body in document order and keep each <formula> that is not inside a <p>, as its own block: the formula text, then its label (e.g. ż = -β y -γz + Ku(z i ). (9)). Formulas inside a <p> were already kept through the paragraph text and are not duplicated. The fallback for text with no paragraphs is unchanged.
  • tests/test_text.py: test_tei_keeps_formulas_with_labels_in_order: a labelled formula, an unlabelled formula and document order. The fixture has no digits, because the owner’s citation check requires every number in a change to be in the cited passages.

Sources

  • Lo, Wang, Neumann et al. 2020, S2ORC: The Semantic Scholar Open Research Corpus, ACL, doi:10.18653/v1/2020.acl-main.447. Evidence W3015453090#p6.c5137c44: “From the XML output of GROBID, we extract … (iv) equations, table content, headers, and footers, which we remove from the body text”. This confirms GROBID gives equations apart from the paragraphs. S2ORC itself chose to remove them.
  • Zanibbi, Mansouri, Agarwal 2025, Mathematical Information Retrieval: Search and Question Answering, Foundations and Trends in IR, doi:10.1561/1500000095. Evidence W4406906359#p168.95529d47: “the text-notation interactions … provide important context that is missing when formulas and text are indexed separately”.
  • research-rag’s citation check confirms both quotes are in their passages; it does not check that a passage supports the change. No paper covers where the label goes; putting it after the formula follows GROBID’s own element.

Checks

  • ruff check . and ruff format --check . pass.
  • pytest: 382 passed, 4 failed. All 4 failures are PytestUnraisableExceptionWarnings from temp files in the http, library, scite and store tests. 4 also fail in those files on unchanged main; which library test fails varies between runs.
  • On the stored TEI of W2998340499, the text passes the readable-text gate, and equations (1)–(6), (9) and (11) appear with their labels.

Not in this PR

  • JATS <disp-formula>/MathML.
  • Bumping the pin in retrieval-augmented-generation.
  • Deploying to the droplet and re-indexing W2998340499. Each of these needs the owner’s OK. Already-held papers keep their old fulltext.txt until they’re re-fetched with force=True and re-indexed.

🤖 Generated with Claude Code