checkpointing-assembly-state-for-resume
A 550-piece jigsaw takes a robot 5-9 hours; a LEGO set or a domino field is similar. Over that time something stops the run - a motor fault, a crash, the end of the day. Graph-as-Policy's graph state lives in the process; this skill keeps the task's state on disk so a new process can pick it up, and then asks whether the world still looks like that state before building on it.
How it composes
- Save after every verified step (or every few): the forward loop's
placements(choosing-next-piece-and-slot / backtracking-a-wrong-placement:{piece, col, row, score?, margin?, confidence?, verified?, locked?}), the bricks placed so far (planning-brick-assembly-order'ssequenceentries:{id, seq, step, cell {x, y, layer, rotation}, size, colour, arm, ...}), genericitems({id, pose?, cell?, mask? | polygon_px? | box?}- dominoes, parts), thependingqueue (choose_next'squeueor the planner's remaining sequence),in_progress(the job started but not finished:{piece | id, col, row, arm, phase}), the ledger (ledger_pathof backtracking-a-wrong-placement, read and embedded with its hash), the arms ({id, joints?, ee_pose?, gripper?, holding?}),grid, and a reference snapshot:snapshot_now(onerobot.get_observation, cut tocamera_name),reference_snapshot(an Observation or CameraFrame) orreference_snapshot_path(.npz with rgb / depth_m / K / pose, or an image; its arrays are copied in, so the file may go away). Take the snapshot only after a step passed its check; between snapshots the last one is carried over, so pieces placed since are known not to be in it. - Resume once at start-up, before the arms move:
resumed- go on withpending;resumed_with_rechecks- park the arms (returning-to-a-recorded- pose), look forlost_items, run the jigsaw / brick checks on therecheckids (amovedpiece: nudge it back or re-seat it;missing/misplaced/unseatedbricks: verifying-build-against-model's repairs), then go on;no_checkpoint- start fresh;state_mismatch- re-perceive the board (localizer, build check) and save a new checkpoint. The returnedplacements,bricks,pending,ledger_path,piecesare in the formats the other skills take.
save_checkpoint
- One file per save,
<task>.<seq:08d>.ckpt.jsonincheckpoint_dir:{"schema", "schema_version": 3, "sha256", "payload"}, the hash over the payload's canonical JSON (sorted keys, no spaces). Written to a temp file in the same directory, flushed, fsynced, renamed over the name, then the directory is fsynced: a reader sees the old set of files or the old set plus the whole new one. - Arrays (snapshot RGB / depth, item masks) go to
blobs/<sha256>.npy.z(zlib-compressed .npy, named by the hash of the uncompressed bytes, written atomically before the checkpoint that names them); the payload holds{"$blob", "dtype", "shape"}. An unchanged snapshot is stored once. Arrays up to 64 numbers (a pose, K, joints) stay inline. - Every save is the whole state except the reference, which is carried
from the previous checkpoint unless a new one is given (
carry_reference). Jigsaw placements become snapshot-diff items by cell whengridis given (the diff skill's grid forms); items with a mask, polygon or box are used as they are; or passreference_items. - Activity stream (py_trees' blackboard ActivityStream, adapted): the
state is viewed as keys (
/placements/<piece>,/bricks/<id>,/arms/<id>,/ledgers/<name>,/pending,/step_index, ...) and every key INITIALISED, WRITTEN or UNSET since the previous checkpoint is appended with its previous and current value and the step; the newest 500 are kept. - Rotate: the newest
keep(5) checkpoints of the task stay; blobs no checkpoint in the directory references are deleted once 10 minutes old.
resume_from_checkpoint
- Pick: the task's files newest first (or every task's with no
task_id); the first that parses, matches its hash, is not from a newer schema and has a task and step is used. Each one skipped is listed inskippedwith the reason; leftover temp files of a killed save are listed instale_temp_filesand ignored. - Migrate older versions step by step: v1, a bare run state as the
jigsaw / LEGO skills take it (
placements, or a plannersequencewithplaced- the count already built -,ledger/ledger_path,queue,snapshot_path; no envelope, no hash); v2, the first envelope (step,queue,armskeyed by id, onesnapshot_path,saved_atin unix seconds). A v1 file has no hash to check. - Ledgers: the file at the saved path is compared with the embedded copy.
Same - fine. Missing or unreadable - the copy is written back
(
restore_ledgers). More events than the copy - backtracking decided after the checkpoint, its actions may be half done: the event's piece and every piece its actions name are rechecked (ledger_after_checkpoint), and the newer file is returned. - What the stop left behind: a piece an arm was
holdingis lost (held_when_stopped,lost_items); thein_progressjob's piece is rechecked; a placement saved withverified: falsetoo. - Snapshot check: one fresh
robot.get_observation(orcurrent) goes to detecting-disturbed-pieces-by-snapshot-diff with the stored reference and items (andgrid,diff_args):moved/lostitems are rechecked with the shift, rotation and depth change it measured,occludedones too; an item it calls unchanged although 40 % of its pixels changed is rechecked asappearance_changed(a removed thin piece can still match the table texture under it); unexplained changes becomeunexpected_objectentries; items removed from the state since the snapshot (backtracking lifted them) are expected to be gone and ignored. Unregistrable views, or a quarter (and 3) of the items moved, givestate_mismatch. - Build check (bricks with a stored
build-plate_pose,plate_studs,system,camera_name,args): the placed bricks are the target of verifying-build-against-model, seen through the fresh frame's depth orbuild_observedbricks;missing,misplaced,wrong_colour,unseated,extraand unverified bricks are rechecked; a quarter (and 3) wrong givesstate_mismatch. Colour is off unlessbuild.check_colour. - Everything else that no check cleared is rechecked:
placed_after_referencewhen the snapshot was compared but the piece is newer,not_checkedwhen no check could run (no reference, no observation, a damaged snapshot blob, the sibling skill not installed).verify: noneskips the checks and returns the state as saved.
Both sibling skills are loaded from the skills installed beside this one (or
a GAP_SKILLS_PATH root); when one is missing its check is skipped and said
so in detail.
Measured
All on the CPU (Intel Core Ultra 7 265KF, 20 cores), no robot, no GPU and no
simulator: nothing moves, so the simulation check does not apply.
tests/bundles/_checkpoint_eval.py reproduces every number below.
Killed writers (--part kill, 200 trials): a separate process saves a
550-piece jigsaw state (placements, the localizer records of all 550 pieces,
a 40-event ledger file, a new 640 x 480 RGB-D snapshot every save, keep 5) in
a loop and is SIGKILLed at a random moment after 1-3 saves. Resume returned a
valid checkpoint 200 / 200 times: the last acknowledged one 148 times, the one
being written (renamed but not yet acknowledged) 52 times, never an older
one, no file skipped; 20 kills left a temp file, listed and ignored; the
ledger compared same every time. SIGKILL does not lose the page cache, so
this tests the rename protocol, not power loss (see Limits).
Damaged files (--part corrupt, 400 trials): one random byte of the
newest checkpoint changed (200) or the file cut at a random length (200):
400 / 400 skipped with the reason and the previous checkpoint returned; no
damaged state was ever accepted. (A flip that produced invalid UTF-8 crashed
the reader before this run; fixed.)
Disturbed boards (--part disturb, seeds 100-119): the synthetic RGB-D
boards of detecting-disturbed-pieces-by-snapshot-diff's tests (15 jigsaw
pieces, 8 bricks or 8 standing dominoes; the overhead camera at 1 m or the
wrist camera at 25 cm; sensor noise per frame), saved with a reference
snapshot and the items' masks, resumed against a new render. 20 boards per
row; "found" = disturbed items in recheck; "extra" = other items rechecked
per board.
| family / camera | untouched: resumed | one moved 3-6 mm: found | one removed: found | two moved: found (mismatch) | sweep 40 % moved: state_mismatch | extra per board (mean / max) |
|---|---|---|---|---|---|---|
| jigsaw / overhead | 20/20 | 20/20 | 20/20 | 40/40 (0) | 20/20 | 0-1.1 / 3 |
| jigsaw / wrist | 20/20 | 20/20 | 20/20 | 40/40 (3) | 20/20 | 0-1.15 / 3 |
| bricks / overhead | 20/20 | 20/20 | 20/20 | 40/40 (0) | 20/20 | 0 / 0 |
| bricks / wrist | 20/20 | 20/20 | 20/20 | 40/40 (0) | 20/20 | 0 / 0 |
| dominoes / overhead | 20/20 | 20/20 | 20/20 | 40/40 (0) | 20/20 | 0 / 0 |
| dominoes / wrist | 20/20 | 20/20 | 20/20 | 40/40 (2) | 20/20 | 0-0.2 / 1 |
- The extra rechecks are the moved item's direct neighbours (the diff skill flags a slid jigsaw piece's neighbour whose footprint it overlaps); the sweep rows found 599 of 600 moved items.
- "Two moved" ended
state_mismatchon 5 of 120 small boards: two moved plus one flagged neighbour is 3 of 8 dominoes (or 4 of 15 pieces), over the quarter. On a 550-piece board the fraction decides, not the count. - Before the
appearance_changedguard, 2 of 20 removed jigsaw pieces (overhead) resumed unflagged: the diff skill matched them to the table under them at 0.56-0.59 with 48-86 % of their pixels changed. The 0.4 threshold was then set on separate seeds (0-19: unmoved items read at most 0.21 of 3475); the table is the rerun on seeds 100-119.
Brick builds (--part build, 20 random 8-brick LEGO builds on a 12 x 12
plate, wrist D405 depth with the station noise model): untouched 20 / 20
resumed; a top brick removed 20 / 20 rechecked first as missing; a top
brick moved one stud 19 / 20 first as misplaced (1 as an extra brick
plus the brick missing), every one resumed_with_rechecks.
Recorded station frames (overhead Orbbec, RGB only, 5 sessions, the scene
static; not committed): 24 pixel-square items on the table, saved from one
frame's PNG through reference_snapshot_path, resumed against the next frame
(10 consecutive pairs and first-to-last per session): 55 / 55 resumed, no
recheck.
Time (--part timing, median of 15): save of the 550-piece state 36 ms
(a 498 kB file), 79 ms with a new 640 x 480 RGB-D snapshot, 51 ms with the
snapshot carried over; resume with verify: none 11 ms. A resume with the
snapshot check is the diff skill's time: 0.7-0.8 s for 15 items at
640 x 480 alone, median 1.0-1.8 s with 12 resumes running at once.
Unit tests (tests/bundles/test_checkpointing_assembly_state_for_resume.py,
31): the envelope and round trip, rotation, the activity stream, a crash
before the rename, torn / flipped / empty files, all files corrupt, writers
SIGKILLed at random moments (4 runs), a corrupt snapshot blob, blob
collection, a snapshot file deleted after saving, unstorable values; v1 bare
jigsaw state, v1 planner sequence, v2 envelope, a newer version skipped, an
edited v2 file, a migrated state saved again; held, in-progress and
unverified items; no observation; a grid mismatch; a newer ledger file and a
missing one; an untouched, knocked, emptied, swept and foreign board; pieces
placed and lifted after the snapshot; grid cells as items; bricks from depth
and from upstream perception; the examples' routes.
Limits
- Power loss is not tested. The writes follow the POSIX recipe (fsync the
file, rename, fsync the directory), and a SIGKILL never left a broken
state, but a kill keeps the page cache; a power cut on a disk that lies
about fsync can still lose the newest file (then the one before is used).
fsync: falseis faster and only safe against process crashes. - One writer per task. Two processes saving the same
task_idinto one directory can take the same sequence number; the rename keeps one of them whole. Different tasks may share a directory. - Only as good as the checks. Pieces placed after the last snapshot can
only be listed (
placed_after_reference), not verified; take a snapshot after each verified placement or batch. The snapshot check inherits the diff skill's limits (a 1 mm bump is under the overhead camera's noise, textureless items cannot have their move measured, the camera must not move by centimetres) and the build check the verify skill's (overhead LEGO seating is below the noise). - Arms are reported, not moved. The skill treats a held piece as lost and says so; parking (returning-to-a-recorded-pose) and finding the piece are the graph's job. The arms' saved joints are not compared with now.
- Synthetic boards and static station frames only; no stopped robot run was resumed on hardware yet. The station frames are RGB only, without pieces.
- Schema versions 1 and 2 are this format's own earlier drafts and the bare run state the jigsaw / LEGO skills pass; the migration chain is there for the versions that come next.
MAX_DISTURBED_FRACTION(a quarter) andMIN_MISMATCH_ITEMS(3) are design choices checked on the synthetic boards, not on robot runs.
The constants, and why
Each constant in scripts/ carries its source. In short: KEEP 5 (a torn
newest file leaves four; 0.5 MB each for 550 pieces); MAX_ACTIVITY 500
(py_trees' ActivityStream default); INLINE_ARRAY_MAX 64 (a pose, K or a
joint vector stays readable in the JSON); BLOB_ZLIB_LEVEL 1 (a full-frame
mask 307 kB -> 1-2 kB); BLOB_GRACE_S 600 s (another task's blobs are never
collected between its blob and checkpoint writes); MAX_DISTURBED_FRACTION
0.25 and MIN_MISMATCH_ITEMS 3 (a nudge moves 1-3 items, a sweep most;
see the table); CHANGED_FRACTION_RECHECK 0.4 (unmoved items read at most
0.21).
Method
The candidate (jigsaw survey, "2 state / 8 long run") pointed at py_trees'
blackboard - a keyed store with an activity log - as the pattern; no
jigsaw code persists its state. This skill keeps that pattern: the state is
viewed as blackboard keys (absolute_name with the / separator) and the
difference between checkpoints is recorded as py_trees' activity items
(INITIALISED / WRITE / UNSET with previous and current values) in a bounded
stream - adapted from py_trees/blackboard.py (BSD-3-Clause, commit 49bcffd,
header in scripts/checkpoint_store.py, licence in LICENSE.py_trees), with
the stream dropping its oldest item where upstream's ActivityStream.push
pops its newest. The file format (hashed envelope, content-addressed blobs,
atomic rename, rotation, migration chain) and the resume logic (ledger
reconciliation, held and in-progress items, the two sibling checks and the
recheck list) are this skill's design. The candidate's append-only JSON-lines
step log is not implemented: every checkpoint is a whole state, and the
activity stream inside it says what changed.


