qlogicagent 2.11.2 → 2.11.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/cli.js +278 -270
  2. package/dist/index.js +277 -269
  3. package/dist/pet-contracts.js +1 -1
  4. package/dist/protocol.js +1 -1
  5. package/dist/types/cli/handlers/session-handler.d.ts +3 -3
  6. package/dist/types/cli/media-runtime-facade.d.ts +8 -0
  7. package/dist/types/cli/permission-bootstrap.d.ts +7 -0
  8. package/dist/types/cli/pet-runtime.d.ts +1 -3
  9. package/dist/types/cli/session-coordinator.d.ts +9 -0
  10. package/dist/types/cli/session-query-service.d.ts +1 -0
  11. package/dist/types/cli/tool-bootstrap.d.ts +31 -2
  12. package/dist/types/cli/turn-permission-sync.d.ts +1 -2
  13. package/dist/types/protocol/wire/acp-protocol.d.ts +7 -0
  14. package/dist/types/protocol/wire/chat-types.d.ts +6 -0
  15. package/dist/types/protocol/wire/gateway-rpc.d.ts +1 -0
  16. package/dist/types/protocol/wire/notification-payloads.d.ts +12 -0
  17. package/dist/types/protocol/wire/pet-contracts.d.ts +3 -2
  18. package/dist/types/runtime/infra/acp-detector.d.ts +21 -0
  19. package/dist/types/runtime/infra/default-path-service.d.ts +3 -0
  20. package/dist/types/runtime/infra/project-store.d.ts +1 -0
  21. package/dist/types/runtime/permission-model.d.ts +19 -0
  22. package/dist/types/runtime/pet/hatch-pet-runner.d.ts +18 -0
  23. package/dist/types/runtime/pet/index.d.ts +12 -5
  24. package/dist/types/runtime/pet/pet-file-loader.d.ts +1 -1
  25. package/dist/types/runtime/pet/pet-growth-engine.d.ts +2 -58
  26. package/dist/types/runtime/pet/pet-profile-service.d.ts +5 -7
  27. package/dist/types/runtime/pet/pet-reaction-engine.d.ts +11 -0
  28. package/dist/types/runtime/pet/pet-soul-service.d.ts +2 -2
  29. package/dist/types/runtime/pet/pet-types.d.ts +1 -32
  30. package/dist/types/runtime/pet/petdex-asset.d.ts +3 -109
  31. package/dist/types/runtime/pet/petdex-forge-service.d.ts +3 -94
  32. package/dist/types/runtime/ports/path-service.d.ts +10 -0
  33. package/dist/types/runtime/ports/permission-contracts.d.ts +20 -3
  34. package/dist/types/runtime/ports/tool-contracts.d.ts +3 -0
  35. package/dist/types/runtime/ports/tool-risk.d.ts +7 -0
  36. package/dist/types/runtime/prompt/fresh-workspace-evidence.d.ts +1 -0
  37. package/dist/types/runtime/session/session-locator.d.ts +2 -0
  38. package/dist/types/runtime/session/session-persistence.d.ts +17 -3
  39. package/dist/types/skills/permissions/hook-runner.d.ts +3 -0
  40. package/dist/types/skills/permissions/operation-classifier.d.ts +36 -0
  41. package/dist/types/skills/permissions/rule-engine.d.ts +19 -14
  42. package/dist/types/skills/portable-tool.d.ts +18 -0
  43. package/dist/types/skills/tools/exec-tool.d.ts +7 -0
  44. package/dist/types/skills/tools/patch-tool.d.ts +7 -0
  45. package/dist/types/skills/tools/petdex-create-tool.d.ts +2 -16
  46. package/dist/types/skills/tools/shell/sandbox/helper-resolver.d.ts +7 -0
  47. package/dist/types/skills/tools/shell/sandbox/landlock-argv.d.ts +6 -0
  48. package/dist/types/skills/tools/shell/sandbox/platform.d.ts +3 -0
  49. package/dist/types/skills/tools/shell/sandbox/sandbox-launcher.d.ts +15 -0
  50. package/dist/types/skills/tools/shell/sandbox/sandbox-scope.d.ts +6 -0
  51. package/dist/types/skills/tools/shell/sandbox/sandbox-types.d.ts +47 -0
  52. package/dist/types/skills/tools/shell/sandbox/seatbelt-profile.d.ts +6 -0
  53. package/dist/types/skills/tools/shell/shell-exec.d.ts +3 -0
  54. package/dist/vendor/hatch-pet/LICENSE.txt +201 -0
  55. package/dist/vendor/hatch-pet/NOTICE.md +25 -0
  56. package/dist/vendor/hatch-pet/references/animation-rows.md +29 -0
  57. package/dist/vendor/hatch-pet/references/codex-pet-contract.md +35 -0
  58. package/dist/vendor/hatch-pet/references/qa-rubric.md +66 -0
  59. package/dist/vendor/hatch-pet/scripts/compose_atlas.py +169 -0
  60. package/dist/vendor/hatch-pet/scripts/derive_running_left_from_running_right.py +150 -0
  61. package/dist/vendor/hatch-pet/scripts/extract_strip_frames.py +408 -0
  62. package/dist/vendor/hatch-pet/scripts/inspect_frames.py +256 -0
  63. package/dist/vendor/hatch-pet/scripts/make_contact_sheet.py +96 -0
  64. package/dist/vendor/hatch-pet/scripts/prepare_pet_run.py +830 -0
  65. package/dist/vendor/hatch-pet/scripts/render_animation_previews.py +78 -0
  66. package/dist/vendor/hatch-pet/scripts/validate_atlas.py +157 -0
  67. package/package.json +5 -3
  68. package/dist/types/runtime/pet/pet-reaction-service.d.ts +0 -33
@@ -0,0 +1,408 @@
1
+ #!/usr/bin/env python3
2
+ """Extract generated horizontal row strips into 192x208 sprite frames."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import argparse
7
+ import json
8
+ import math
9
+ import re
10
+ from pathlib import Path
11
+
12
+ from PIL import Image
13
+
14
+ CELL_WIDTH = 192
15
+ CELL_HEIGHT = 208
16
+ ROW_FRAME_COUNTS = {
17
+ "idle": 6,
18
+ "running-right": 8,
19
+ "running-left": 8,
20
+ "waving": 4,
21
+ "jumping": 5,
22
+ "failed": 8,
23
+ "waiting": 6,
24
+ "running": 6,
25
+ "review": 6,
26
+ }
27
+
28
+
29
+ def parse_states(raw: str) -> list[str]:
30
+ if raw.strip().lower() == "all":
31
+ return list(ROW_FRAME_COUNTS)
32
+ states = [item.strip() for item in raw.split(",") if item.strip()]
33
+ unknown = sorted(set(states) - set(ROW_FRAME_COUNTS))
34
+ if unknown:
35
+ raise SystemExit(f"unknown state(s): {', '.join(unknown)}")
36
+ return states
37
+
38
+
39
+ def parse_hex_color(value: str) -> tuple[int, int, int]:
40
+ if not re.fullmatch(r"#[0-9a-fA-F]{6}", value):
41
+ raise SystemExit(f"invalid chroma key color: {value}; expected #RRGGBB")
42
+ return tuple(int(value[index : index + 2], 16) for index in (1, 3, 5))
43
+
44
+
45
+ def load_chroma_key(decoded_dir: Path, override: str | None) -> tuple[int, int, int]:
46
+ if override:
47
+ return parse_hex_color(override)
48
+ request_path = decoded_dir.parent / "pet_request.json"
49
+ if request_path.is_file():
50
+ request = json.loads(request_path.read_text(encoding="utf-8"))
51
+ chroma_key = request.get("chroma_key")
52
+ if isinstance(chroma_key, dict) and isinstance(chroma_key.get("hex"), str):
53
+ return parse_hex_color(chroma_key["hex"])
54
+ return parse_hex_color("#00FF00")
55
+
56
+
57
+ def color_distance(
58
+ red: int,
59
+ green: int,
60
+ blue: int,
61
+ key: tuple[int, int, int],
62
+ ) -> float:
63
+ return math.sqrt((red - key[0]) ** 2 + (green - key[1]) ** 2 + (blue - key[2]) ** 2)
64
+
65
+
66
+ def remove_chroma_background(
67
+ image: Image.Image,
68
+ chroma_key: tuple[int, int, int],
69
+ threshold: float,
70
+ ) -> Image.Image:
71
+ rgba = image.convert("RGBA")
72
+ pixels = rgba.load()
73
+ for y in range(rgba.height):
74
+ for x in range(rgba.width):
75
+ red, green, blue, alpha = pixels[x, y]
76
+ if color_distance(red, green, blue, chroma_key) <= threshold:
77
+ pixels[x, y] = (0, 0, 0, 0)
78
+ return rgba
79
+
80
+
81
+ def fit_to_cell(image: Image.Image) -> Image.Image:
82
+ bbox = image.getbbox()
83
+ target = Image.new("RGBA", (CELL_WIDTH, CELL_HEIGHT), (0, 0, 0, 0))
84
+ if bbox is None:
85
+ return target
86
+
87
+ sprite = image.crop(bbox)
88
+ max_width = CELL_WIDTH - 10
89
+ max_height = CELL_HEIGHT - 10
90
+ scale = min(max_width / sprite.width, max_height / sprite.height, 1.0)
91
+ if scale != 1.0:
92
+ sprite = sprite.resize(
93
+ (max(1, round(sprite.width * scale)), max(1, round(sprite.height * scale))),
94
+ Image.Resampling.LANCZOS,
95
+ )
96
+ left = (CELL_WIDTH - sprite.width) // 2
97
+ top = (CELL_HEIGHT - sprite.height) // 2
98
+ target.alpha_composite(sprite, (left, top))
99
+ return target
100
+
101
+
102
+ def fit_viewport_to_cell(image: Image.Image) -> Image.Image:
103
+ target = Image.new("RGBA", (CELL_WIDTH, CELL_HEIGHT), (0, 0, 0, 0))
104
+ if image.getbbox() is None:
105
+ return target
106
+
107
+ viewport = image.copy()
108
+ max_width = CELL_WIDTH - 10
109
+ max_height = CELL_HEIGHT - 10
110
+ scale = min(max_width / viewport.width, max_height / viewport.height, 1.0)
111
+ if scale != 1.0:
112
+ viewport = viewport.resize(
113
+ (max(1, round(viewport.width * scale)), max(1, round(viewport.height * scale))),
114
+ Image.Resampling.LANCZOS,
115
+ )
116
+ left = (CELL_WIDTH - viewport.width) // 2
117
+ top = (CELL_HEIGHT - viewport.height) // 2
118
+ target.alpha_composite(viewport, (left, top))
119
+ return target
120
+
121
+
122
+ def connected_components(image: Image.Image) -> list[dict[str, object]]:
123
+ alpha = image.getchannel("A")
124
+ width, height = image.size
125
+ data = alpha.tobytes()
126
+ visited = bytearray(width * height)
127
+ components: list[dict[str, object]] = []
128
+
129
+ for start, alpha_value in enumerate(data):
130
+ if alpha_value <= 16 or visited[start]:
131
+ continue
132
+
133
+ stack = [start]
134
+ visited[start] = 1
135
+ pixels: list[int] = []
136
+ min_x = width
137
+ min_y = height
138
+ max_x = 0
139
+ max_y = 0
140
+
141
+ while stack:
142
+ current = stack.pop()
143
+ pixels.append(current)
144
+ x = current % width
145
+ y = current // width
146
+ min_x = min(min_x, x)
147
+ min_y = min(min_y, y)
148
+ max_x = max(max_x, x)
149
+ max_y = max(max_y, y)
150
+
151
+ if x > 0:
152
+ neighbor = current - 1
153
+ if not visited[neighbor] and data[neighbor] > 16:
154
+ visited[neighbor] = 1
155
+ stack.append(neighbor)
156
+ if x + 1 < width:
157
+ neighbor = current + 1
158
+ if not visited[neighbor] and data[neighbor] > 16:
159
+ visited[neighbor] = 1
160
+ stack.append(neighbor)
161
+ if y > 0:
162
+ neighbor = current - width
163
+ if not visited[neighbor] and data[neighbor] > 16:
164
+ visited[neighbor] = 1
165
+ stack.append(neighbor)
166
+ if y + 1 < height:
167
+ neighbor = current + width
168
+ if not visited[neighbor] and data[neighbor] > 16:
169
+ visited[neighbor] = 1
170
+ stack.append(neighbor)
171
+
172
+ components.append(
173
+ {
174
+ "pixels": pixels,
175
+ "area": len(pixels),
176
+ "bbox": (min_x, min_y, max_x + 1, max_y + 1),
177
+ "center_x": (min_x + max_x + 1) / 2,
178
+ }
179
+ )
180
+
181
+ return components
182
+
183
+
184
+ def component_group_image(
185
+ source: Image.Image,
186
+ components: list[dict[str, object]],
187
+ padding: int = 4,
188
+ ) -> Image.Image:
189
+ width, height = source.size
190
+ min_x = max(0, min(component["bbox"][0] for component in components) - padding)
191
+ min_y = max(0, min(component["bbox"][1] for component in components) - padding)
192
+ max_x = min(width, max(component["bbox"][2] for component in components) + padding)
193
+ max_y = min(height, max(component["bbox"][3] for component in components) + padding)
194
+
195
+ output = Image.new("RGBA", (max_x - min_x, max_y - min_y), (0, 0, 0, 0))
196
+ source_pixels = source.load()
197
+ output_pixels = output.load()
198
+ for component in components:
199
+ for pixel_index in component["pixels"]:
200
+ x = pixel_index % width
201
+ y = pixel_index // width
202
+ output_pixels[x - min_x, y - min_y] = source_pixels[x, y]
203
+ return output
204
+
205
+
206
+ def component_frame_groups(
207
+ strip: Image.Image,
208
+ frame_count: int,
209
+ ) -> list[list[dict[str, object]]] | None:
210
+ components = connected_components(strip)
211
+ if not components:
212
+ return None
213
+
214
+ largest_area = max(component["area"] for component in components)
215
+ seed_threshold = max(120, largest_area * 0.20)
216
+ seeds = [component for component in components if component["area"] >= seed_threshold]
217
+ if len(seeds) < frame_count:
218
+ seeds = sorted(components, key=lambda component: component["area"], reverse=True)[
219
+ :frame_count
220
+ ]
221
+ if len(seeds) < frame_count:
222
+ return None
223
+
224
+ seeds = sorted(
225
+ sorted(seeds, key=lambda component: component["area"], reverse=True)[:frame_count],
226
+ key=lambda component: component["center_x"],
227
+ )
228
+ seed_ids = {id(seed) for seed in seeds}
229
+ groups: list[list[dict[str, object]]] = [[seed] for seed in seeds]
230
+ noise_threshold = max(12, largest_area * 0.002)
231
+
232
+ for component in components:
233
+ if id(component) in seed_ids or component["area"] < noise_threshold:
234
+ continue
235
+ nearest_index = min(
236
+ range(len(seeds)),
237
+ key=lambda index: abs(seeds[index]["center_x"] - component["center_x"]),
238
+ )
239
+ groups[nearest_index].append(component)
240
+
241
+ return groups
242
+
243
+
244
+ def extract_component_frames(strip: Image.Image, frame_count: int) -> list[Image.Image] | None:
245
+ groups = component_frame_groups(strip, frame_count)
246
+ if groups is None:
247
+ return None
248
+ return [fit_to_cell(component_group_image(strip, group)) for group in groups]
249
+
250
+
251
+ def component_bounds(components: list[dict[str, object]]) -> tuple[int, int, int, int]:
252
+ return (
253
+ min(component["bbox"][0] for component in components),
254
+ min(component["bbox"][1] for component in components),
255
+ max(component["bbox"][2] for component in components),
256
+ max(component["bbox"][3] for component in components),
257
+ )
258
+
259
+
260
+ def extract_slot_frames(strip: Image.Image, frame_count: int) -> list[Image.Image]:
261
+ slot_width = strip.width / frame_count
262
+ frames = []
263
+ for index in range(frame_count):
264
+ left = round(index * slot_width)
265
+ right = round((index + 1) * slot_width)
266
+ crop = strip.crop((left, 0, right, strip.height))
267
+ frames.append(fit_to_cell(crop))
268
+ return frames
269
+
270
+
271
+ def extract_stable_slot_frames(strip: Image.Image, frame_count: int) -> list[Image.Image]:
272
+ groups = component_frame_groups(strip, frame_count)
273
+ padding = 4
274
+ if groups is not None:
275
+ bboxes = [component_bounds(group) for group in groups]
276
+ shared_top = max(0, min(bbox[1] for bbox in bboxes) - padding)
277
+ shared_bottom = min(strip.height, max(bbox[3] for bbox in bboxes) + padding)
278
+ viewport_width = max(bbox[2] - bbox[0] for bbox in bboxes) + padding * 2
279
+ viewport_height = max(1, shared_bottom - shared_top)
280
+ frames = []
281
+ for group, bbox in zip(groups, bboxes):
282
+ grouped = component_group_image(strip, group, padding=padding)
283
+ grouped_top = max(0, bbox[1] - padding)
284
+ viewport = Image.new(
285
+ "RGBA",
286
+ (viewport_width, viewport_height),
287
+ (0, 0, 0, 0),
288
+ )
289
+ left = (viewport_width - grouped.width) // 2
290
+ viewport.alpha_composite(grouped, (left, grouped_top - shared_top))
291
+ frames.append(fit_viewport_to_cell(viewport))
292
+ return frames
293
+
294
+ bbox = strip.getbbox()
295
+ if bbox is None:
296
+ return [
297
+ Image.new("RGBA", (CELL_WIDTH, CELL_HEIGHT), (0, 0, 0, 0))
298
+ for _ in range(frame_count)
299
+ ]
300
+
301
+ shared_top = max(0, bbox[1] - padding)
302
+ shared_bottom = min(strip.height, bbox[3] + padding)
303
+ slot_width = strip.width / frame_count
304
+ frames = []
305
+ for index in range(frame_count):
306
+ left = round(index * slot_width)
307
+ right = round((index + 1) * slot_width)
308
+ crop = strip.crop((left, shared_top, right, shared_bottom))
309
+ frames.append(fit_viewport_to_cell(crop))
310
+ return frames
311
+
312
+
313
+ def extract_state(
314
+ strip_path: Path,
315
+ state: str,
316
+ output_root: Path,
317
+ chroma_key: tuple[int, int, int],
318
+ threshold: float,
319
+ method: str,
320
+ ) -> dict[str, object]:
321
+ frame_count = ROW_FRAME_COUNTS[state]
322
+ with Image.open(strip_path) as opened:
323
+ strip = remove_chroma_background(opened, chroma_key, threshold)
324
+
325
+ state_dir = output_root / state
326
+ state_dir.mkdir(parents=True, exist_ok=True)
327
+
328
+ frames = None
329
+ used_method = method
330
+ if method in {"auto", "components"}:
331
+ frames = extract_component_frames(strip, frame_count)
332
+ if frames is None and method == "components":
333
+ raise SystemExit(f"could not find {frame_count} sprite components in {strip_path}")
334
+ if frames is not None:
335
+ used_method = "components"
336
+
337
+ if frames is None:
338
+ if method == "stable-slots":
339
+ frames = extract_stable_slot_frames(strip, frame_count)
340
+ used_method = "stable-slots"
341
+ else:
342
+ frames = extract_slot_frames(strip, frame_count)
343
+ used_method = "slots"
344
+
345
+ outputs = []
346
+ for index, frame in enumerate(frames):
347
+ output = state_dir / f"{index:02d}.png"
348
+ frame.save(output)
349
+ outputs.append(str(output))
350
+ return {"state": state, "frames": outputs, "method": used_method}
351
+
352
+
353
+ def main() -> None:
354
+ parser = argparse.ArgumentParser(description=__doc__)
355
+ parser.add_argument("--decoded-dir", required=True)
356
+ parser.add_argument("--output-dir", required=True)
357
+ parser.add_argument("--states", default="all")
358
+ parser.add_argument("--chroma-key", help="Override chroma key as #RRGGBB.")
359
+ parser.add_argument("--key-threshold", type=float, default=96.0)
360
+ parser.add_argument(
361
+ "--method",
362
+ choices=("auto", "components", "slots", "stable-slots"),
363
+ default="auto",
364
+ help="Use connected sprite components when possible, raw equal slots, or row-stable slot viewports.",
365
+ )
366
+ args = parser.parse_args()
367
+
368
+ decoded_dir = Path(args.decoded_dir).expanduser().resolve()
369
+ output_dir = Path(args.output_dir).expanduser().resolve()
370
+ chroma_key = load_chroma_key(decoded_dir, args.chroma_key)
371
+ states = parse_states(args.states)
372
+ manifest = []
373
+ for state in states:
374
+ strip_path = decoded_dir / f"{state}.png"
375
+ if not strip_path.is_file():
376
+ raise SystemExit(f"missing generated strip for {state}: {strip_path}")
377
+ manifest.append(
378
+ extract_state(
379
+ strip_path,
380
+ state,
381
+ output_dir,
382
+ chroma_key,
383
+ args.key_threshold,
384
+ args.method,
385
+ )
386
+ )
387
+
388
+ (output_dir / "frames-manifest.json").write_text(
389
+ json.dumps(
390
+ {
391
+ "ok": True,
392
+ "chroma_key": {
393
+ "hex": f"#{chroma_key[0]:02X}{chroma_key[1]:02X}{chroma_key[2]:02X}",
394
+ "rgb": list(chroma_key),
395
+ "threshold": args.key_threshold,
396
+ },
397
+ "rows": manifest,
398
+ },
399
+ indent=2,
400
+ )
401
+ + "\n",
402
+ encoding="utf-8",
403
+ )
404
+ print(json.dumps({"ok": True, "frames_root": str(output_dir), "states": states}, indent=2))
405
+
406
+
407
+ if __name__ == "__main__":
408
+ main()
@@ -0,0 +1,256 @@
1
+ #!/usr/bin/env python3
2
+ """Inspect extracted Codex pet frames before atlas composition."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import argparse
7
+ import json
8
+ import math
9
+ from pathlib import Path
10
+ from statistics import median
11
+
12
+ from PIL import Image
13
+
14
+ CELL_WIDTH = 192
15
+ CELL_HEIGHT = 208
16
+ ROW_FRAME_COUNTS = {
17
+ "idle": 6,
18
+ "running-right": 8,
19
+ "running-left": 8,
20
+ "waving": 4,
21
+ "jumping": 5,
22
+ "failed": 8,
23
+ "waiting": 6,
24
+ "running": 6,
25
+ "review": 6,
26
+ }
27
+ IMAGE_SUFFIXES = {".png", ".webp", ".jpg", ".jpeg"}
28
+
29
+
30
+ def alpha_nonzero_count(image: Image.Image) -> int:
31
+ alpha = image if image.mode == "L" else image.getchannel("A")
32
+ return sum(alpha.histogram()[1:])
33
+
34
+
35
+ def edge_alpha_count(image: Image.Image, margin: int) -> int:
36
+ alpha = image.getchannel("A")
37
+ width, height = alpha.size
38
+ total = 0
39
+ for box in (
40
+ (0, 0, width, margin),
41
+ (0, height - margin, width, height),
42
+ (0, 0, margin, height),
43
+ (width - margin, 0, width, height),
44
+ ):
45
+ total += alpha_nonzero_count(alpha.crop(box))
46
+ return total
47
+
48
+
49
+ def color_distance(left: tuple[int, int, int], right: tuple[int, int, int]) -> float:
50
+ return math.sqrt(sum((left[index] - right[index]) ** 2 for index in range(3)))
51
+
52
+
53
+ def chroma_adjacent_count(
54
+ image: Image.Image,
55
+ chroma_key: tuple[int, int, int] | None,
56
+ threshold: float,
57
+ ) -> int:
58
+ if chroma_key is None:
59
+ return 0
60
+ rgba = image.convert("RGBA")
61
+ data = rgba.tobytes()
62
+ count = 0
63
+ for index in range(0, len(data), 4):
64
+ red, green, blue, alpha = data[index : index + 4]
65
+ if alpha > 16 and color_distance((red, green, blue), chroma_key) <= threshold:
66
+ count += 1
67
+ return count
68
+
69
+
70
+ def frame_files(state_dir: Path) -> list[Path]:
71
+ if not state_dir.is_dir():
72
+ return []
73
+ return sorted(path for path in state_dir.iterdir() if path.suffix.lower() in IMAGE_SUFFIXES)
74
+
75
+
76
+ def load_manifest(frames_root: Path) -> dict[str, dict[str, object]]:
77
+ manifest_path = frames_root / "frames-manifest.json"
78
+ if not manifest_path.is_file():
79
+ return {}
80
+ manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
81
+ rows = manifest.get("rows", [])
82
+ if not isinstance(rows, list):
83
+ return {}
84
+ return {
85
+ row["state"]: row
86
+ for row in rows
87
+ if isinstance(row, dict) and isinstance(row.get("state"), str)
88
+ }
89
+
90
+
91
+ def load_chroma_key(frames_root: Path) -> tuple[int, int, int] | None:
92
+ manifest_path = frames_root / "frames-manifest.json"
93
+ if not manifest_path.is_file():
94
+ return None
95
+ manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
96
+ chroma_key = manifest.get("chroma_key")
97
+ if not isinstance(chroma_key, dict):
98
+ return None
99
+ rgb = chroma_key.get("rgb")
100
+ if (
101
+ not isinstance(rgb, list)
102
+ or len(rgb) != 3
103
+ or not all(isinstance(value, int) for value in rgb)
104
+ ):
105
+ return None
106
+ return (rgb[0], rgb[1], rgb[2])
107
+
108
+
109
+ def inspect_state(
110
+ frames_root: Path,
111
+ state: str,
112
+ expected_count: int,
113
+ manifest_rows: dict[str, dict[str, object]],
114
+ chroma_key: tuple[int, int, int] | None,
115
+ args: argparse.Namespace,
116
+ ) -> dict[str, object]:
117
+ state_dir = frames_root / state
118
+ files = frame_files(state_dir)
119
+ row_errors: list[str] = []
120
+ row_warnings: list[str] = []
121
+ frames: list[dict[str, object]] = []
122
+ areas: list[int] = []
123
+ manifest_row = manifest_rows.get(state, {})
124
+ method = manifest_row.get("method")
125
+
126
+ if len(files) != expected_count:
127
+ row_errors.append(f"expected {expected_count} frame files for {state}, found {len(files)}")
128
+
129
+ if args.require_components and method and method != "components":
130
+ if method == "stable-slots" and args.allow_stable_slots:
131
+ row_warnings.append(
132
+ f"{state} used extraction method stable-slots; confirm motion playback remains stable and unclipped"
133
+ )
134
+ else:
135
+ row_errors.append(
136
+ f"{state} used extraction method {method}; regenerate the row or inspect slot slicing"
137
+ )
138
+ elif method and method != "components":
139
+ row_warnings.append(
140
+ f"{state} used extraction method {method}; component extraction is preferred"
141
+ )
142
+
143
+ for index, frame_path in enumerate(files[:expected_count]):
144
+ with Image.open(frame_path) as opened:
145
+ frame = opened.convert("RGBA")
146
+ nontransparent = alpha_nonzero_count(frame)
147
+ bbox = frame.getbbox()
148
+ edge_pixels = edge_alpha_count(frame, args.edge_margin)
149
+ chroma_adjacent_pixels = chroma_adjacent_count(
150
+ frame,
151
+ chroma_key,
152
+ args.chroma_adjacent_threshold,
153
+ )
154
+ info = {
155
+ "index": index,
156
+ "file": str(frame_path),
157
+ "width": frame.width,
158
+ "height": frame.height,
159
+ "nontransparent_pixels": nontransparent,
160
+ "bbox": list(bbox) if bbox else None,
161
+ "edge_pixels": edge_pixels,
162
+ "chroma_adjacent_pixels": chroma_adjacent_pixels,
163
+ }
164
+ frames.append(info)
165
+ areas.append(nontransparent)
166
+
167
+ if frame.size != (CELL_WIDTH, CELL_HEIGHT):
168
+ row_errors.append(
169
+ f"{state} frame {index:02d} is {frame.width}x{frame.height}; expected {CELL_WIDTH}x{CELL_HEIGHT}"
170
+ )
171
+ if nontransparent < args.min_used_pixels:
172
+ row_errors.append(
173
+ f"{state} frame {index:02d} is empty or too sparse ({nontransparent} pixels)"
174
+ )
175
+ if edge_pixels > args.edge_pixel_threshold:
176
+ row_warnings.append(
177
+ f"{state} frame {index:02d} has {edge_pixels} non-transparent pixels near the cell edge"
178
+ )
179
+ if chroma_adjacent_pixels > args.chroma_adjacent_pixel_threshold:
180
+ row_errors.append(
181
+ f"{state} frame {index:02d} has {chroma_adjacent_pixels} non-transparent pixels close to the chroma key"
182
+ )
183
+
184
+ if areas:
185
+ row_median = median(areas)
186
+ for index, area in enumerate(areas[:expected_count]):
187
+ if row_median > 0 and area < row_median * args.small_outlier_ratio:
188
+ row_warnings.append(
189
+ f"{state} frame {index:02d} is much smaller than the row median ({area} vs {row_median:.0f})"
190
+ )
191
+ if row_median > 0 and area > row_median * args.large_outlier_ratio:
192
+ row_warnings.append(
193
+ f"{state} frame {index:02d} is much larger than the row median ({area} vs {row_median:.0f})"
194
+ )
195
+
196
+ return {
197
+ "state": state,
198
+ "expected_frames": expected_count,
199
+ "actual_frames": len(files),
200
+ "extraction_method": method,
201
+ "ok": not row_errors,
202
+ "errors": row_errors,
203
+ "warnings": row_warnings,
204
+ "frames": frames,
205
+ }
206
+
207
+
208
+ def main() -> None:
209
+ parser = argparse.ArgumentParser(description=__doc__)
210
+ parser.add_argument("--frames-root", required=True)
211
+ parser.add_argument("--json-out", required=True)
212
+ parser.add_argument("--min-used-pixels", type=int, default=400)
213
+ parser.add_argument("--edge-margin", type=int, default=2)
214
+ parser.add_argument("--edge-pixel-threshold", type=int, default=24)
215
+ parser.add_argument("--chroma-adjacent-threshold", type=float, default=150.0)
216
+ parser.add_argument("--chroma-adjacent-pixel-threshold", type=int, default=800)
217
+ parser.add_argument("--small-outlier-ratio", type=float, default=0.35)
218
+ parser.add_argument("--large-outlier-ratio", type=float, default=2.75)
219
+ parser.add_argument(
220
+ "--require-components",
221
+ action="store_true",
222
+ help="Fail rows that fell back to equal-slot extraction.",
223
+ )
224
+ parser.add_argument(
225
+ "--allow-stable-slots",
226
+ action="store_true",
227
+ help="Permit explicitly chosen stable-slots extraction while still warning for visual review.",
228
+ )
229
+ args = parser.parse_args()
230
+
231
+ frames_root = Path(args.frames_root).expanduser().resolve()
232
+ manifest_rows = load_manifest(frames_root)
233
+ chroma_key = load_chroma_key(frames_root)
234
+ rows = [
235
+ inspect_state(frames_root, state, count, manifest_rows, chroma_key, args)
236
+ for state, count in ROW_FRAME_COUNTS.items()
237
+ ]
238
+ errors = [error for row in rows for error in row["errors"]]
239
+ warnings = [warning for row in rows for warning in row["warnings"]]
240
+ result = {
241
+ "ok": not errors,
242
+ "frames_root": str(frames_root),
243
+ "errors": errors,
244
+ "warnings": warnings,
245
+ "rows": rows,
246
+ }
247
+
248
+ json_out = Path(args.json_out).expanduser().resolve()
249
+ json_out.parent.mkdir(parents=True, exist_ok=True)
250
+ json_out.write_text(json.dumps(result, indent=2) + "\n", encoding="utf-8")
251
+ print(json.dumps({k: v for k, v in result.items() if k != "rows"}, indent=2))
252
+ raise SystemExit(0 if result["ok"] else 1)
253
+
254
+
255
+ if __name__ == "__main__":
256
+ main()