Repository Public
1#!/usr/bin/env python32# SP Notes Core3# File: Scripts/recorders/research.py4# Description: Records the fixtures of the research workspace: references (client ids, extracts, links, DOI, BibTeX5# import / export), experiments, research tasks, working papers, overview, and the AI-workstream routes6# (export.tex, references ?ids= / ?projectId=) as this server answers them.7#8# Author: Simon-Pierre Boucher9# Contact: contact@spboucher.ai10from _parity_support import ParityRecorder, new_id1112r = ParityRecorder("research")13head = r.call("GET", "/api/sync?since=0&limit=1").json["head"]14paper = r.call("POST", "/api/research/papers", {"title": "Fixture · Working paper parity", "status": "writing", "venue": "JREFE"}, save="paper-create").json15r.track(paper["id"])16r.call("POST", "/api/research/papers", {"status": "unicorn"}, save="paper-validation")1718# References.19ref_id = new_id()20body = {"id": ref_id, "pageId": paper["id"], "citeKey": "doe2021parity", "entryType": "article", "title": "Fixture · Mortgage Prepayment and the Term Structure",21 "authors": ["Jane Doe", "John Roe"], "year": 2021, "journal": "Journal of Housing Finance", "url": "https://example.org/doe2021", "fields": {"volume": "12", "pages": "1--20"}}22r.call("POST", "/api/research/references", body, save="ref-create")23r.call("POST", "/api/research/references", dict(body, title="Fixture · retry"), save="ref-create-again")24second = r.call("POST", "/api/research/references", {"title": "Fixture · Library only reference"}, save="ref-create-minimal").json25r.call("POST", "/api/research/references", {"title": ""}, save="ref-validation")26r.call("GET", "/api/research/references?pageId=" + paper["id"], save="ref-list-page")27r.call("GET", "/api/research/references", save="ref-list-library")28r.call("GET", "/api/research/references/" + ref_id, save="ref-get")29r.call("PATCH", "/api/research/references/" + ref_id, {"year": 2022, "journal": None, "fields": {"volume": "13", "pages": "", "note": "revised"}, "verified": "user"}, save="ref-patch")30r.call("PATCH", "/api/research/references/" + ref_id, {"citeKey": second["citeKey"]}, save="ref-patch-citekey-taken")31r.call("GET", "/api/research/references/" + new_id(), save="ref-not-found")32r.call("GET", "/api/research/references/not-a-uuid", save="ref-bad-id")33r.call("GET", "/api/research/references/%s/links" % ref_id, save="ref-links")34r.call("POST", "/api/research/references/%s?action=attach" % ref_id, {"attachmentId": None}, save="ref-attach-none")35r.call("POST", "/api/research/references/%s?action=attach" % ref_id, {"attachmentId": new_id()}, save="ref-attach-unknown")36r.call("POST", "/api/research/references/%s?action=verify" % ref_id, save="ref-verify-no-doi")37r.call("POST", "/api/research/references/%s?action=merge" % ref_id, save="ref-unknown-action")3839# Extracts.40extract_id = new_id()41r.call("POST", "/api/research/references/%s/extracts" % ref_id, {"id": extract_id, "quote": "Prepayment speeds rise when the curve steepens.", "page": "p. 7", "note": "à citer"}, save="extract-create")42r.call("POST", "/api/research/references/%s/extracts" % ref_id, {"id": extract_id, "quote": "retry"}, save="extract-create-again")43r.call("PATCH", "/api/research/references/%s/extracts" % ref_id, {"id": extract_id, "note": None, "page": "p. 8"}, save="extract-patch")44r.call("GET", "/api/research/references/%s/extracts" % ref_id, save="extract-list")45removed = r.call("DELETE", "/api/research/references/%s/extracts?extractId=%s" % (ref_id, extract_id), save="extract-delete").json46r.call("DELETE", "/api/research/references/%s/extracts?extractId=%s" % (ref_id, extract_id), save="extract-delete-again")47if removed and removed.get("extract"):48 r.call("POST", "/api/research/references/%s/extracts?restore=1" % ref_id, {"extract": removed["extract"]}, save="extract-restore")49r.call("DELETE", "/api/research/references/%s/extracts" % ref_id, save="extract-delete-no-id")5051# DOI (Crossref — a bibliographic lookup over the network, no model involved).52r.call("GET", "/api/research/references/doi", save="doi-missing")53r.call("GET", "/api/research/references/doi?doi=not-a-doi", save="doi-invalid")54r.call("GET", "/api/research/references/doi?doi=10.9999/spn-fixture-does-not-exist", save="doi-not-found")55r.call("GET", "/api/research/references/doi?doi=10.2307/2325486", save="doi-lookup")56added = r.call("POST", "/api/research/references/doi", {"doi": "10.2307/2325486", "pageId": paper["id"]}, save="doi-add")57r.call("POST", "/api/research/references/doi", {"doi": "10.2307/2325486", "pageId": paper["id"]}, save="doi-add-duplicate")58r.call("GET", "/api/research/references/doi?doi=10.2307/2325486", save="doi-lookup-duplicate")59if added.status in (200, 201):60 r.call("POST", "/api/research/references/%s?action=verify" % added.json["id"], save="ref-verify")6162# BibTeX.63bib = "@article{roe2019parity,\n author = {Roe, John and Poe, Edgar},\n title = {Fixture: Rents and Rates},\n journal = {Housing Studies},\n year = {2019},\n doi = {10.9999/spn.fixture.2019}\n}\n\n@book{doe2021parity,\n title = {Duplicate key},\n year = {2020}\n}\n\n@misc{broken,\n year = {2020}\n}\n"64r.call("POST", "/api/research/references/import-bibtex?dryRun=1", {"text": bib, "pageId": paper["id"]}, save="bibtex-dry-run")65imported = r.call("POST", "/api/research/references/import-bibtex", {"text": bib, "pageId": paper["id"]}, save="bibtex-import").json66r.form("POST", "/api/research/references/import-bibtex", {"pageId": paper["id"], "dryRun": "1"}, "file", "refs.bib", bib.encode("utf-8"), "application/x-bibtex", save="bibtex-import-file-dry-run")67r.call("POST", "/api/research/references/import-bibtex", {"text": " "}, save="bibtex-empty")68r.call("GET", "/api/research/references/export.bib?pageId=" + paper["id"], save="bib-export-page")69r.call("GET", "/api/research/references/export.bib?inline=1&pageId=" + paper["id"], save="bib-export-inline")70r.call("GET", "/api/research/references/export.bib?pageId=nope", save="bib-export-bad-page")71r.call("GET", "/api/research/references/export.bib?projectId=" + paper["id"], save="bib-export-project")7273# AI-workstream additions, as this server answers them.74r.call("GET", "/api/research/references?projectId=" + paper["id"], save="ref-list-project")75r.call("GET", "/api/research/references?ids=%s,%s" % (ref_id, "00000000-0000-4000-8000-000000000000"), save="ref-list-ids")76r.call("GET", "/api/research/export.tex?format=json&projectId=" + paper["id"], save="export-tex-json")77r.call("GET", "/api/research/export.tex?inline=1&projectId=" + paper["id"], save="export-tex")7879# Experiments.80exp = r.call("POST", "/api/research/experiments", {"pageId": paper["id"], "name": "Fixture · GARCH baseline", "status": "running", "params": {"p": 1, "q": 1}, "metrics": {"qlike": 0.412},81 "datasetRef": "findata://spy/1min", "repoUrl": "https://git.example.org/wp", "notes": "premier essai"}, save="experiment-create").json82r.call("POST", "/api/research/experiments", {"name": "x", "status": "unicorn"}, save="experiment-validation")83r.call("PATCH", "/api/research/experiments/" + exp["id"], {"status": "done", "metrics": {"qlike": 0.398, "mse": 1.2}, "notes": None}, save="experiment-patch")84r.call("GET", "/api/research/experiments?pageId=" + paper["id"], save="experiment-list-page")85r.call("GET", "/api/research/experiments", save="experiment-list")86r.call("PATCH", "/api/research/experiments/" + new_id(), {"name": "x"}, save="experiment-not-found")8788# Research tasks (the tasks of a paper; same table as /api/tasks).89task = r.call("POST", "/api/research/tasks", {"pageId": paper["id"], "title": "Fixture · relire la section 3", "status": "doing", "priority": "high", "dueAt": "2026-12-05", "project": "wp-parity"}, save="rtask-create").json90many = r.call("POST", "/api/research/tasks", {"pageId": paper["id"], "items": [{"title": "Fixture · tableau 2"}, {"title": "Fixture · figure 1", "priority": "low"}]}, save="rtask-create-bulk").json91r.call("PATCH", "/api/research/tasks/" + task["id"], {"status": "done", "dueAt": None}, save="rtask-patch")92r.call("GET", "/api/research/tasks?pageId=" + paper["id"], save="rtask-list")93r.call("GET", "/api/research/tasks", save="rtask-list-no-page")94r.call("DELETE", "/api/research/tasks/" + task["id"], save="rtask-delete")95r.call("POST", "/api/research/tasks/%s?action=restore" % task["id"], save="rtask-restore")96r.call("PATCH", "/api/research/tasks/" + new_id(), {"title": "x"}, save="rtask-not-found")97r.call("GET", "/api/tasks?done=1&pageId=" + paper["id"], save="tasks-list-doing")9899r.call("GET", "/api/research/overview", save="overview")100r.call("GET", "/api/sync?kinds=reference&since=%d&limit=500" % head, save="feed-references")101102# Delete, restore, clean-up.103r.call("DELETE", "/api/research/experiments/" + exp["id"], save="experiment-delete")104r.call("DELETE", "/api/research/experiments/" + exp["id"], save="experiment-delete-again")105r.call("DELETE", "/api/research/references/" + ref_id, save="ref-delete")106r.call("GET", "/api/research/references/" + ref_id, save="ref-get-deleted")107r.call("POST", "/api/research/references", dict(body, title="Fixture · after delete"), save="ref-create-deleted-id")108r.call("POST", "/api/research/references/%s?action=restore" % ref_id, save="ref-restore")109r.call("DELETE", "/api/research/references/" + ref_id)110r.call("DELETE", "/api/research/references/" + ref_id, save="ref-delete-again")111r.call("GET", "/api/sync?kinds=reference&since=%d&limit=500" % head, save="feed-references-deleted")112r.call("DELETE", "/api/research/references/" + second["id"])113if added.status in (200, 201):114 r.call("DELETE", "/api/research/references/" + added.json["id"])115for created in (imported or {}).get("created", []):116 r.call("DELETE", "/api/research/references/" + created["id"])117for t in [task] + (many if isinstance(many, list) else []):118 r.call("DELETE", "/api/tasks/" + t["id"])119r.finish()120