Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ The package version runs on its own semver; the schema it is generated from is `

## Unreleased

## 0.143.0 (2026-11-23)

- **Fixed: `write_world_file(include_hidden=False)` could carry a hidden element.** 0.142.0 made the package's reader refuse a file nested more than 200 deep, and the writer of a world file took that refusal for "this file is no element": a hidden element whose file was nested that deep travelled in a share, whole and with its author fields, where 0.141.0 had left it out. The same was true in every earlier version for an element file holding an integer of more than 4,300 digits. Now a file the reader refuses stops the write: `write_world_file` and `world_file_bytes` raise `ValueError` naming the file and why, and nothing is written, whenever they would have had to read that file, to leave out the hidden (`include_hidden=False`) or to take out author fields (the default). A copy that leaves nothing out (`include_hidden=True, include_author_fields=True`) reads no element file and still carries every one byte for byte. Text that is no JSON at all is no element to any reader and travels as it is, as before. If you share world files from folders you did not write yourself, upgrade.
- **A number that is not finite makes its file unreadable.** `1e400` in an element file read as infinity, which no writer of this package can write back and a JavaScript reader serves as `null`; `NaN`, `Infinity` and `-Infinity`, which are no JSON, were read as floats by the Kanka and LegendKeeper doors. The one reader for text the package did not write now refuses all of them with a plain `ValueError` (`a number too large to read`, `NaN is not JSON`), so `read_folder` skips such a file with that reason, the doctor lists it under `unread`, and the Kanka door skips it with a note instead of stopping with an error at the write. A finite number of any size reads as before.
- Not changed: an integer of up to 4,300 digits is read exactly, as Python reads it. The npm SDK's Kanka door skips a file holding an integer past 2^53, which a JavaScript number rounds: the one input on which the two doors differ.

## 0.142.0 (2026-11-23)

- **The doctor names what it could not read.** On a folder, `Report.unread` (and `unread` in `--json`, after `written`) lists each file the folder reader did not read as an element, with why: a file that is not JSON, is not an object or has no `id`, a second file of one id, a directory that is no type's. The prose report puts the list first, and any entry makes the exit code 1. Until now the doctor said nothing: with one element file cut off mid-write it reported the other elements as the whole world and listed every link to the unread one under "a link to an element that is not there". A dot-prefixed file is not listed. The API door's list is always empty.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ world = read_world_file("moppetopia.owworld")
world.to_folder("moppetopia-copy")
```

`write_world_file` carries `world.json` byte for byte (its `producer`, `source` and `format_version` included) and the folder's parts, `elements/`, `spatial/`, `media/` and `records/`, and nothing else: never an `api` block, never a tool's own folder unless you hand it in (`private_dir=".atlas"`, which makes the file a private copy rather than one to send around), and never a producer's records its `records/RECORDS.json` entry marks `"private": true` unless you ask (`include_private_records=True`); the manifest itself always travels as it is. A hidden element (`hidden: true` in its file) travels byte for byte, since the file is a copy; for a file you hand to someone else, `include_hidden=False` leaves every hidden element's file out whole, and a list passed as `hidden_left_out=` receives their paths. Nothing else changes, so a link from a visible element to a hidden one keeps an id the file no longer holds. An element's author fields (`created_by`, `changed_by`, `changed_via`, `changed_client` and `doors`) record which member and which tool wrote it, which means nothing to someone the world is handed to, so every element file leaves them out by default (`created_at` and `updated_at` stay): a file holding any of them is rewritten without them in the folder's own form, and a file holding none travels byte for byte. To archive your own world as it is on disk, pass `include_author_fields=True`. The same folder gives the same bytes on one Python. `read_world_file` checks the whole archive before it unpacks anything and refuses a file it cannot open safely with a `WorldFileError`, whose `reason` names one of the spec's twelve causes (`unsafe_path`, `collision`, `too_big`, ...). It gives the same verdict as the TypeScript and Unity SDKs on every case of the format's conformance set.
`write_world_file` carries `world.json` byte for byte (its `producer`, `source` and `format_version` included) and the folder's parts, `elements/`, `spatial/`, `media/` and `records/`, and nothing else: never an `api` block, never a tool's own folder unless you hand it in (`private_dir=".atlas"`, which makes the file a private copy rather than one to send around), and never a producer's records its `records/RECORDS.json` entry marks `"private": true` unless you ask (`include_private_records=True`); the manifest itself always travels as it is. A hidden element (`hidden: true` in its file) travels byte for byte, since the file is a copy; for a file you hand to someone else, `include_hidden=False` leaves every hidden element's file out whole, and a list passed as `hidden_left_out=` receives their paths. Nothing else changes, so a link from a visible element to a hidden one keeps an id the file no longer holds. An element's author fields (`created_by`, `changed_by`, `changed_via`, `changed_client` and `doors`) record which member and which tool wrote it, which means nothing to someone the world is handed to, so every element file leaves them out by default (`created_at` and `updated_at` stay): a file holding any of them is rewritten without them in the folder's own form, and a file holding none travels byte for byte. To archive your own world as it is on disk, pass `include_author_fields=True`. An element file the writer must read and cannot hold (nested more than 200 deep, or holding a number that is not finite or an integer of more than 4,300 digits) stops the write with a `ValueError` naming it: whether it is hidden cannot be told, and nothing is carried by guess. The same folder gives the same bytes on one Python. `read_world_file` checks the whole archive before it unpacks anything and refuses a file it cannot open safely with a `WorldFileError`, whose `reason` names one of the spec's twelve causes (`unsafe_path`, `collision`, `too_big`, ...). It gives the same verdict as the TypeScript and Unity SDKs on every case of the format's conformance set.

To send a folder's edits back, keep a copy exported before you edited, then push the difference:

Expand Down Expand Up @@ -253,7 +253,7 @@ for finding in report.findings:

It looks for links to elements that are not there, Events that end before they start, elements with no links in or out, two elements of one type whose names collide once case, character width, spacing and punctuation are folded (a name shared across types, such as a Location and its Pin, is not one), stubs never filled (a known-of element older than `--days`, or an element with no text in any of its type's text fields, on every type but map, marker, pin and relation), and a charter Law untouched after `--changes` changes in the feed. On a folder it also checks `world.json`: a `format_version` older than this package's (flag 9), and an `api_key` in a `world.json` that git tracks (flag 10). On both doors it reads the world's own house rules (flag 11, the checks profile: `x_checks_rules` on the world's Charter, the newest Law with supertype Charter that carries the mark `x_charter_of`, applied by `onlyworlds.apply_checks`) and names every element a rule finds short, with each rule's scoped and short counts, and every rule it cannot apply, with why; an older marked Charter is a finding, its rules not applied; a Law with supertype Charter and no mark is lore, not a Charter and not a finding, and its rules are never applied. Flag 14 names a marked Guide or Domain whose links name an element that is not in the world or a hidden one (every other reader is served the Guide or Domain without it; not through a guest key, and the hidden half only through a key that is served hidden elements). On a spine world (one where an element carries an `x_spine_` key), flag 13 reads each element with `read_spine_element` and names an off-list supertype on a listed type, every value the reader drops and a probe not in the probe form; then, on a source world, it checks that each row (an Object, Construct, Location or Law of a listed kind, the Charter too) is kept by exactly one Seat (a Title with supertype `Seat`, through its field for the row's type; any other Title neither owns nor counts), that every `x_spine_depends_on` id is in the world (not through a guest key), that a `legacy` or `provenance` row has an Event with supertype `Retirement` that names it in `x_spine_retires` and links it through its typed field where the type has one (`objects`, `constructs`, `locations`; an Event has no `laws` field, so a Law's retirement is attributed by the key alone; only through a key that sees every element, hidden ones too), that no row is still at `gone` (its retirement is unfinished), and that no two rows of one row type share a name once trimmed and folded for case (a Seat or any other element is outside that rule). Whether a probe passes and `x_spine_probed` are not checked, and the report says so. A projection (a public world built from a spine, each row naming its keeping Seat in `x_spine_owner`) is marked on its World with `x_spine_projection`, `{source, at}`: `source` the projected-from world's id as a string, a UUID that is no element's id in this world, and `at` the ISO 8601 time of the last rebuild, which must parse; each is a finding on the world otherwise. On a projection only the projection's own checks run: every row must carry `x_spine_owner` (the keeping Seat's name in the source, which need match nothing in the projection) and no element may be hidden; the source's checks do not run there, and the report names each. On an unmarked world `x_spine_owner` is a finding (the projection's third check), and a malformed mark is a finding on the world, which is then read as unmarked. Each check says how much it could compare, and one it cannot run (a folder has no change feed; nothing records whether a message was read) is listed with the reason, never counted as clean. A finding on a message or a room thing (supertype Message, Board, Work item, Spec, Heart, Vote or Sitting) is marked `"kind": "message"` and listed last, outside the count of things to look at. `--json` prints the same report for a tool, and `--packet` prints the findings as work packets (the `packet` extension profile: a Relation with supertype `Work item`, its elements with a reason each under `x_packet_elements`, a JSON array, empty when clean) for a tool that lands them in a world or a parlor. The exit code is 0 when there is nothing to look at, 1 when there is (a message finding too), 2 when nothing could be read.

On a folder, the report names first what it could not read as an element (`report.unread`, each a `file` and a `why`): a file that is not JSON, is not an object or has no `id`, a second file of one id, a directory that is no type's. Every check ran on the rest, so an element in such a file counts as not there and a link to it reads as a link to a missing element; any unread file makes the exit code 1. A dot-prefixed file is a tool's litter and is not listed.
On a folder, the report names first what it could not read as an element (`report.unread`, each a `file` and a `why`): a file that is not JSON, is nested more than 200 deep or holds a number that is not finite, is not an object or has no `id`, a second file of one id, a directory that is no type's. Every check ran on the rest, so an element in such a file counts as not there and a link to it reads as a link to a missing element; any unread file makes the exit code 1. A dot-prefixed file is a tool's litter and is not listed.

The API door reads the key from `OW_API_KEY` and the PIN from `OW_API_PIN`, or from the toolkit's `ONLYWORLDS_API_KEY` and `ONLYWORLDS_API_PIN` when those are not set.

Expand Down
27 changes: 23 additions & 4 deletions src/onlyworlds/_outside_json.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""``json.loads`` for text this package did not write: a world folder's file, an archive, another tool's export.

Two inputs that are JSON by the grammar make ``json.loads`` do something its callers do not expect.
Three inputs make ``json.loads`` do something its callers do not expect.

A value nested very deep: where the interpreter's own limit is met first, ``json.loads`` raises
``RecursionError``, which is no ``ValueError``, so every reader that catches "not JSON" let it through as
Expand All @@ -10,12 +10,18 @@

An integer of more than 4,300 digits: a plain ``ValueError`` whose message advises a Python setting.

Here both are a ``ValueError`` in plain words, so a caller's ``except ValueError`` reads either as a file
it cannot read, and says why."""
A number that is not finite: ``1e400`` reads as infinity, and ``NaN``, ``Infinity`` and ``-Infinity``, which
are no JSON, are read as floats. No writer of this package can write such a value back, and a JavaScript
reader serves it as ``null``.

Here each is a ``ValueError`` in plain words that is no ``json.JSONDecodeError``, so a caller's
``except ValueError`` reads it as a file it cannot read, and says why; a caller that must tell text that is
no JSON from JSON this reader refuses catches the decode error first."""

from __future__ import annotations

import json
import math
import sys
from typing import Any

Expand All @@ -26,6 +32,17 @@
_TOO_DEEP = f"nested more than {MAX_DEPTH} deep"


def _float(text: str) -> float:
value = float(text)
if not math.isfinite(value):
raise ValueError("a number too large to read")
return value


def _constant(token: str) -> Any:
raise ValueError(f"{token} is not JSON")


def _int(text: str) -> int:
limit = sys.get_int_max_str_digits()
if limit and len(text.lstrip("-")) > limit:
Expand All @@ -51,8 +68,10 @@ def _too_deep(value: Any) -> bool:


def loads(text: str, **kwargs: Any) -> Any:
"""``json.loads(text, **kwargs)``, with the two cases above raised as ``ValueError``."""
"""``json.loads(text, **kwargs)``, with the cases above raised as ``ValueError``."""
kwargs.setdefault("parse_int", _int)
kwargs.setdefault("parse_float", _float)
kwargs.setdefault("parse_constant", _constant)
try:
value = json.loads(text, **kwargs)
except RecursionError:
Expand Down
2 changes: 1 addition & 1 deletion src/onlyworlds/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.142.0"
__version__ = "0.143.0"
45 changes: 29 additions & 16 deletions src/onlyworlds/worldfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from __future__ import annotations

import io
import json
import os
import re
import shutil
Expand Down Expand Up @@ -835,17 +836,34 @@ def _default_top(world: Mapping[str, Any]) -> str:
return f"{slugify(name if isinstance(name, str) else None) or 'world'}-{str(world['id'])[:8]}"


def _hidden_element_file(rel: str, src: Path) -> bool:
"""An element file (a ``.json`` file directly in a type folder of ``elements/`` or legacy ``spatial/``)
whose JSON object carries the hidden mark at the top level (``folder.is_hidden``). A file anywhere
else, or that is not a JSON object, is not an element and is carried as it is, as ever."""
def _element_body(rel: str, src: Path, to: str) -> Any:
"""What an element file holds (a ``.json`` file directly in a type folder of ``elements/`` or legacy
``spatial/``), for a writer that must read it ``to`` leave something out. None for a file anywhere else,
or one that is not UTF-8 JSON: no reader takes that as an element, and it is carried as it is, as ever.

A file that is JSON this package's reader refuses (nested past its depth, a number it cannot hold) raises
``ValueError``: what it holds cannot be read, so the writer leaves nothing in by guess, as with a records
manifest it cannot read (OW-8)."""
segs = rel.split("/")
if len(segs) != 3 or segs[0] not in (ELEMENTS_DIR, LEGACY_SPATIAL_DIR) or not rel.lower().endswith(".json"):
return False
return None
try:
body = _outside_json.loads(src.read_bytes().decode("utf-8-sig"))
except (UnicodeDecodeError, ValueError):
return False
return _outside_json.loads(src.read_bytes().decode("utf-8-sig"))
except (UnicodeDecodeError, json.JSONDecodeError):
return None
except ValueError as exc:
raise ValueError(
f"{src} cannot be read ({exc}), so the writer cannot {to} and carries nothing by guess. Fix or "
"remove the file, or pass include_hidden=True and include_author_fields=True to carry every file "
"as it is."
) from None


def _hidden_element_file(rel: str, src: Path) -> bool:
"""An element file whose JSON object carries the hidden mark at the top level (``folder.is_hidden``).
A file that is not an element file, or not a JSON object, is carried as it is, as ever; one the reader
refuses raises (``_element_body``)."""
body = _element_body(rel, src, "tell whether it is hidden")
return isinstance(body, dict) and is_hidden(body)


Expand All @@ -859,14 +877,9 @@ def _without_author_fields(rel: str, src: Path) -> bytes | None:
as the TypeScript writer reads one) without its author fields, in the folder's own form
(``_jsonfmt.encode``: two-space indent, a trailing newline, the other keys in their order), or
None when it holds none of them or is not a JSON object: then it travels byte for byte. Only
top-level keys go; an ``x_*`` value that holds one of the names is the extension's, and stays."""
segs = rel.split("/")
if len(segs) != 3 or segs[0] not in (ELEMENTS_DIR, LEGACY_SPATIAL_DIR) or not rel.lower().endswith(".json"):
return None
try:
body = _outside_json.loads(src.read_bytes().decode("utf-8").removeprefix(_BOM), parse_constant=_reject_constant)
except ValueError: # not UTF-8, or not JSON
return None
top-level keys go; an ``x_*`` value that holds one of the names is the extension's, and stays.
A file the reader refuses raises (``_element_body``): its author fields cannot be told from its bytes."""
body = _element_body(rel, src, "take out its author fields")
if not isinstance(body, dict) or not any(k in body for k in _AUTHOR_FIELDS):
return None
return encode({k: v for k, v in body.items() if k not in _AUTHOR_FIELDS})
Expand Down
Loading
Loading