backend: print the 参考文献 list as a bibliography, one entry per work

The list was printing each citation's quote under a `引用 #53` tag: no authors,
no year, no pages, no DOI — the quoted passage where the entry should be — and
it numbered *citations*, so a paper with 86 citations to 65 works listed 21 of
them two or three times under different numbers. The same article was `[5]` in
one paragraph and `[17]` in another.

Two rules were wrong, so two are stated here:

**A work is numbered where the reader first meets it.** A citation is a place
in the text; an entry is a work. `citations` still has one row per place, and
each row now carries the number of its *work*, so the marker in the text, the
tooltip on it and the entry in the list are the same number. The document grew
a `references` list — the entries, in order — and `reference_style`, which
names the style that wrote them.

**How a reference prints is a property of a style, not of a citation.** The new
`app/citation_styles` package holds one module per style; `springer.py`
implements Springer's basic style, whose shape is
`Names: Title. <where it sits> (year). <identifier>` with a branch per document
type — a journal with its volume(issue) and pages, a book with publisher and
place, a chapter inside somebody else's book, a patent with its number, a page
whose URL is its source.

Three of its rules are decisions rather than transcription, and each is
documented where it applies: up to six contributors are named and more become
three plus `et al.`; a journal prints under its 刊名缩写 when the writer stored
one, because no algorithm abbreviates an arbitrary title correctly; and a CJK
name keeps its printed form even with halves filled in, because dotting 「三」
into an initial is wrong in Chinese and insufficient in English.

Nothing is invented to fill a gap. An entry says what it knows and reports the
rest as `missing` (「缺 责任者、出版年」), and the document warns about the
incomplete ones by number: an entry that is short because fields are empty
looks like a fact about the document rather than a library that needs attention.

`document_text` had the same bug in the MCP surface and now prints the entries,
with in-text markers taken from the document's numbering rather than from each
sentence's local position — which is what made the text an agent reads disagree
with the list under it.

`scripts/check_citation_styles.py` checks the formatter with no server and no
database: one reference of every type, and the exact entry the style calls for.
This commit is contained in:
2026-09-22 21:50:27 +08:00
parent 15955f552d
commit 76708ce767
8 changed files with 1305 additions and 46 deletions
+36 -13
View File
@@ -165,13 +165,36 @@ def main() -> int:
check("citations numbered", [c["sort"] for c in paragraph["sentences"][1]["citations"]] == [1, 2])
check("three citations in the paper", len(document["citations"]) == 3)
check(
"citation index is reading order",
[c["index"] for c in document["citations"]] == [1, 2, 3],
"a citation carries the number of its work, not of itself",
[c["index"] for c in document["citations"]] == [1, 1, 2],
str([c["index"] for c in document["citations"]]),
)
print("5. the 参考文献 list: one entry per work, in Springer's basic style")
check(
"two works — the article cited twice, and the unlinked quote",
len(document["references"]) == 2,
str(len(document["references"])),
)
first, second = document["references"]
check("both places that cite the article share its entry", len(first["citations"]) == 2)
check(
"the entry is a bibliography line, not the quote",
first["text"]
== "测试作者: 用于验证引用链路的文献. 测试期刊 (2024)",
first["text"],
)
check(
"what a complete entry still lacks is named",
first["missing"] == ["卷期页码"],
str(first["missing"]),
)
check("the unlinked quote is its own entry", second["reference_id"] is None)
check("and prints no fabricated entry", second["text"] == "", second["text"])
check("the style is reported", document["reference_style"] == "springer", document["reference_style"])
check("sentence template stamped", paragraph["sentences"][0]["template_id"] == wide["id"])
print("5. explicit sorts decide the order, not the order they were sent")
print("6. explicit sorts decide the order, not the order they were sent")
second_sort = document["paragraphs"][1]["paper_template_filed_sort"]
reordered = call(
"PUT",
@@ -202,7 +225,7 @@ def main() -> int:
{"sentences": []},
)
print("6. the seam between sentences depends on the language")
print("7. the seam between sentences depends on the language")
# Chinese needs nothing after 。; English needs a space, or two
# sentences print as `First.Second.`
mixed = call(
@@ -244,7 +267,7 @@ def main() -> int:
# Clear it again so the counts asserted below stay meaningful.
call("PUT", f"/papers/{paper['id']}/paragraphs/{second_sort}", {"sentences": []})
print("7. reject a citation with no quote")
print("8. reject a citation with no quote")
bad = call(
"PUT",
f"/papers/{paper['id']}/paragraphs/{first_sort}",
@@ -262,7 +285,7 @@ def main() -> int:
)
check("citation without content refused", "detail" in bad)
print("8. a sentence with no paragraph in the template survives a switch")
print("9. a sentence with no paragraph in the template survives a switch")
# A position neither template defines, so the check means the same
# thing before and after the switch below.
orphan_sort = (
@@ -290,7 +313,7 @@ def main() -> int:
p for p in document["paragraphs"] if p["paper_template_filed_sort"] == orphan_sort
)["name"] is None)
print("9. switch the template")
print("10. switch the template")
switched = call("PATCH", f"/papers/{paper['id']}", {"template_id": narrow["id"]})
check("template changed", switched["template_id"] == narrow["id"])
document = call("GET", f"/papers/{paper['id']}/document")
@@ -320,7 +343,7 @@ def main() -> int:
),
)
print("10. move one paragraph onto another position")
print("11. move one paragraph onto another position")
document = call(
"PUT",
f"/papers/{paper['id']}/paragraphs/{orphan_sort}",
@@ -335,18 +358,18 @@ def main() -> int:
all(p["paper_template_filed_sort"] != orphan_sort for p in document["paragraphs"]),
)
print("11. paper list reports progress")
print("12. paper list reports progress")
listing = call("GET", f"/papers?keyword=冒烟&status=writing")
check("found by keyword and status", listing["total"] == 1, str(listing["total"]))
row = listing["items"][0]
check("paragraph count", row["paragraph_count"] == 1, str(row["paragraph_count"]))
check("sentence count", row["sentence_count"] == 3, str(row["sentence_count"]))
print("12. a template in use cannot be deleted")
print("13. a template in use cannot be deleted")
conflict = call("DELETE", f"/templates/{narrow['id']}", expect=409)
check("template delete refused", "正被论文使用" in conflict["detail"], conflict["detail"])
print("13. single sentence edit and delete")
print("14. single sentence edit and delete")
sentence_id = target["sentences"][0]["id"]
updated = call(
"PATCH",
@@ -362,7 +385,7 @@ def main() -> int:
sum(len(p["sentences"]) for p in document["paragraphs"]) == 2,
)
print("14. a paper with no template still works")
print("15. a paper with no template still works")
bare = call(
"POST",
"/papers",
@@ -385,7 +408,7 @@ def main() -> int:
call("DELETE", f"/papers/{bare['id']}", expect=204)
created_papers.remove(bare["id"])
print("15. delete the paper")
print("16. delete the paper")
call("DELETE", f"/papers/{paper['id']}", expect=204)
created_papers.clear()
for reference_id in created_references: