Observing a run — geopatcher.observe¶
Callbacks during split / merge, a resumable record of the processed
patches, what a failed patch leaves behind, and strict mode. The
observability guide and the
on-error /
journal recipes show them in use.
Hooks¶
geopatcher.observe.PatcherHook
¶
Bases: Protocol
Optional callbacks emitted by patcher split and merge operations.
Hook objects may implement any subset of these methods. The patchers dispatch callbacks dynamically, in order, and convert hook exceptions into warnings so observability code cannot interrupt patch generation.
on_patch_start and on_patch_done accept an optional trailing
coord_value (or None for integer geometries). _dispatch trims
to the callback's actual arity, so single-arg hooks written for the
pre-coord protocol still receive only anchor without warnings.
See ADR-004 in docs/patcher/decisions.md ("Hook payload extension").
on_patch_skipped fires (instead of on_patch_start /
on_patch_done) for each patch a journal already holds, so
on_split_start's total is accounted for on a resumed run.
on_error receives the original exception — whether the patcher
re-raises it or its on_error policy swallows it.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/hooks.py
geopatcher.observe.UNKNOWN_TOTAL = -1
module-attribute
¶
Journal¶
geopatcher.observe.PatchJournal
dataclass
¶
Append-only local journal keyed by patch anchor.
Anchors may be tuples, lists, dictionaries (string keys), strings,
numbers, booleans, numpy scalars or numpy arrays — everything the
samplers emit, including spatial.sampler.Explicit(anchors_=np.argwhere(...))
rows. Every anchor goes through normalize_anchor before it is keyed
or written, so (np.int64(5), np.int64(10)), np.array([5, 10])
and (5, 10) are one anchor. The journal stores one JSON record per
committed patch. Re-opening the same path reconstructs the latest status
for each anchor, allowing patcher.split(..., journal=journal) to skip
completed work after a crash.
Durability: each commit flushes the Python buffer and calls
os.fsync on the file descriptor before returning. The OS may still
reorder the directory entry on a power-loss event, so treat the
guarantee as "best-effort durable per row" rather than transactional.
Re-running a job after a crash skips anchors that have a row with
status == "ok"; partially-written trailing rows are dropped by the
JSON-decode guard in _load with a warning.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/journal.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
commit(anchor, *, status, runtime_s, output_uri=None, error=None)
¶
Append a durable status row for anchor.
The row is flushed and fsync-ed before the call returns so a
process crash after commit() returns does not lose the record.
Source code in packages/geotoolz-patcher/src/geopatcher/_src/journal.py
completed()
¶
Return every anchor whose latest row has status == "ok".
Anchors come back in their journal (JSON-normalised) form — see
normalize_anchor: tuples and numpy arrays as lists, numpy
scalars as Python numbers.
Examples:
>>> journal = PatchJournal("out/run.jsonl")
>>> journal.commit((0, 0), status="ok", runtime_s=0.1)
>>> journal.commit((0, 8), status="error", runtime_s=0.1)
>>> journal.completed()
[[0, 0]]
>>> len(journal.completed()) # progress on restart
1
Source code in packages/geotoolz-patcher/src/geopatcher/_src/journal.py
has(anchor)
¶
Return True when anchor has a successful journal row.
pending(all_anchors)
¶
Return anchors without a successful journal row.
geopatcher.observe.normalize_anchor(anchor)
¶
Return anchor as plain JSON-compatible Python values.
The one anchor normaliser shared by PatchJournal (rows and keys)
and PatchCache (entry keys), so an anchor is the same key however
a sampler spelled it:
- numpy scalars →
.item();datetime64/timedelta64→ a tagged{"__datetime64__": str}/{"__timedelta64__": str}dict, so they stay JSON-serialisable without colliding with a string anchor ("2024-01-01") or with each other (NaT). Those two keys are reserved: a dict using them is accepted only in exactly that tagged form (it is the datetime's canonical spelling, which journal rows are re-keyed from); - numpy arrays → nested lists of normalised elements;
- tuples and lists → lists;
- dicts → dicts of normalised values (keys must be strings).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchor
|
Any
|
A sampler anchor. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The normalised anchor. |
Raises:
| Type | Description |
|---|---|
TypeError
|
|
Examples:
>>> normalize_anchor((np.int64(5), np.int64(10)))
[5, 10]
>>> normalize_anchor(np.array([41, 31]))
[41, 31]
>>> normalize_anchor({"time": np.int32(2), "lat": 0})
{'time': 2, 'lat': 0}
Source code in packages/geotoolz-patcher/src/geopatcher/_src/journal.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
Errors¶
geopatcher.observe.PatchErrorRecord
dataclass
¶
A failed patch read recorded by a patcher's split.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchor
|
Any
|
Key of the patch that failed to build — the anchor, or the
patch key ( |
required |
kind
|
str
|
Exception class name. |
required |
message
|
str
|
Exception message. |
required |
traceback
|
str
|
Formatted traceback for debugging. |
required |
retry_count
|
int
|
Number of retries already attempted for this failure. |
required |
Source code in packages/geotoolz-patcher/src/geopatcher/_src/walk.py
Strict mode¶
geopatcher.observe.get_strict()
¶
geopatcher.observe.set_strict(value)
¶
Set the process-wide strict flag.
When True, streaming_safe = False aggregations raise instead
of emitting a warning. The flag is read by
_warn_if_unsafe_streaming and any future check that wants the same
fail-fast contract.