Skip to content

Commit 1b85fba

Browse files
tmoostingclaude
andcommitted
0.100.1: records_of reads a world in one pass
The Character's stories are found once, by each Event they name, and each record looks its stories up, where it scanned every element again per record. Answers are unchanged: compared with 0.100.0 and with the npm SDK on 20,000 random worlds, including Narratives that name an Event twice and events lists holding other values. A test holds one story per Narrative and Event, and stories that are each record's own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 576c1d7 commit 1b85fba

4 files changed

Lines changed: 55 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is `
44

55
## Unreleased
66

7+
## 0.100.1 (2026-11-01)
8+
9+
- **`records_of` reads a world in one pass.** It finds the Character's stories by each Event they name once, then looks each record's stories up, where it scanned every element again for each record. Every answer is the same as 0.100.0's and as the npm SDK's (compared on 20,000 random worlds, including Narratives that name an Event twice and `events` lists holding other values), and each record's stories are its own dicts.
10+
711
## 0.100.0 (2026-11-01)
812

913
- **`records_of(character_id, elements)`, `record_mark_of(element)` and `RECORD_FIELD`: the record profile** (schema-dist `profiles/record.md`, serial 102). A game's run is kept in the world its Characters live in, as an Event carrying the mark `x_record`: `tool` and `played_at` required; `version`, `seed`, `outcome` and `seats` optional. `records_of` reads one Character's records from the elements as the reader is served them, newest first, each with that Character's stories of the run (a Narrative naming them as `protagonist`, `narrator` or both). Who took part is the Event's `characters` alone. A run or a story still proposed is not read. `played_at` must be a real calendar instant, and records order by the instant to the millisecond, a finer fraction truncated as JavaScript reads it. The fixture `profiles/record.fixture.json` (32 cases) is the test; the npm SDK's `recordsOf` (4.124.0) gives the same answers.

‎src/onlyworlds/_version.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.100.0"
1+
__version__ = "0.100.1"

‎src/onlyworlds/record.py‎

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ def _proposed(element: Mapping[str, Any]) -> bool:
6868
return element.get("x_review_proposed") is not None
6969

7070

71+
def _hashable(value: Any) -> bool:
72+
try:
73+
hash(value)
74+
except TypeError:
75+
return False
76+
return True
77+
78+
7179
def record_mark_of(element: Any) -> RecordMark | None:
7280
"""The record's mark on an element, or None.
7381
@@ -132,6 +140,25 @@ def records_of(character_id: str, elements: Sequence[Any]) -> list[Record]:
132140
raise TypeError("records_of: character_id must be a string")
133141
if isinstance(elements, (str, bytes)) or not isinstance(elements, Sequence):
134142
raise TypeError("records_of: elements must be a list")
143+
# This Character's stories, by each Event they name (once per Narrative, however often it names it): one pass over
144+
# the world, then a lookup per record, where each record rescanned the whole world through 0.100.0. An id that is
145+
# not hashable names no story, as the npm reader's includes() matches no object.
146+
stories_of: dict[Any, list[RecordStory]] = {}
147+
for n in elements:
148+
if not isinstance(n, Mapping) or n.get("type") != "narrative" or _proposed(n):
149+
continue
150+
events = n.get("events")
151+
if not isinstance(events, list):
152+
continue
153+
p = n.get("protagonist") == character_id
154+
r = n.get("narrator") == character_id
155+
if not p and not r:
156+
continue
157+
name = n.get("name")
158+
role: Literal["both", "protagonist", "narrator"] = "both" if p and r else "protagonist" if p else "narrator"
159+
story: RecordStory = {"id": n["id"], "name": name if isinstance(name, str) else "", "as": role}
160+
for e in {e for e in events if _hashable(e)}:
161+
stories_of.setdefault(e, []).append(story)
135162
out: list[Record] = []
136163
for ev in elements:
137164
mark = record_mark_of(ev)
@@ -140,20 +167,8 @@ def records_of(character_id: str, elements: Sequence[Any]) -> list[Record]:
140167
characters = ev.get("characters")
141168
if not isinstance(characters, list) or character_id not in characters:
142169
continue
143-
stories: list[RecordStory] = []
144-
for n in elements:
145-
if not isinstance(n, Mapping) or n.get("type") != "narrative" or _proposed(n):
146-
continue
147-
events = n.get("events")
148-
if not isinstance(events, list) or ev["id"] not in events:
149-
continue
150-
p = n.get("protagonist") == character_id
151-
r = n.get("narrator") == character_id
152-
if not p and not r:
153-
continue
154-
name = n.get("name")
155-
role: Literal["both", "protagonist", "narrator"] = "both" if p and r else "protagonist" if p else "narrator"
156-
stories.append({"id": n["id"], "name": name if isinstance(name, str) else "", "as": role})
170+
found = stories_of.get(ev["id"], []) if _hashable(ev["id"]) else []
171+
stories: list[RecordStory] = [s.copy() for s in found]
157172
stories.sort(key=lambda s: s["id"])
158173
ev_name = ev.get("name")
159174
seat = mark["seats"].get(character_id)

‎tests/test_record.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@ def test_records_of_is_pure() -> None:
6565
assert records_of(who, elements)[0]["event"]["name"] != "planted"
6666

6767

68+
def test_a_narrative_naming_an_event_twice_is_one_story_and_no_two_records_share_one() -> None:
69+
c = "019a8000-0000-7000-8000-000000000101"
70+
71+
def ev(i: str, at: str) -> dict[str, Any]:
72+
return {"id": i, "type": "event", "name": i, "characters": [c], "x_record": {"tool": "tangle", "played_at": at}}
73+
74+
world: list[Any] = [
75+
ev("e1", "2026-11-02T00:00:00Z"),
76+
ev("e2", "2026-11-01T00:00:00Z"),
77+
{"id": "n1", "type": "narrative", "name": "One", "protagonist": c, "events": ["e1", "e1", "e2"]},
78+
{"id": "n2", "type": "narrative", "name": "Two", "narrator": c, "events": [7, None, {}, ["e1"], "e1"]},
79+
]
80+
got = records_of(c, world)
81+
assert [(r["event"]["id"], [f"{s['id']}:{s['as']}" for s in r["stories"]]) for r in got] == [
82+
("e1", ["n1:protagonist", "n2:narrator"]),
83+
("e2", ["n1:protagonist"]),
84+
]
85+
got[0]["stories"][0]["name"] = "planted"
86+
assert got[1]["stories"][0]["name"] == "One"
87+
88+
6889
def test_records_of_refuses_what_is_not_an_id_or_a_list() -> None:
6990
with pytest.raises(TypeError):
7091
records_of(None, []) # type: ignore[arg-type]

0 commit comments

Comments
 (0)