@rig-c/review 3.7.0 → 4.0.0
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.
- package/README.md +1 -1
- package/docs/DESIGN.md +145 -74
- package/docs/PANELS.md +20 -12
- package/docs/PARTS_VIEW.md +133 -76
- package/docs/REVIEW_PACKAGE.md +445 -99
- package/docs/STABILITY.md +132 -198
- package/docs/WORKFLOWS.md +41 -120
- package/package.json +4 -3
- package/skills/rig-review/SKILL.md +41 -27
- package/src/build.ts +10 -2
- package/src/cli.ts +17 -16
- package/src/comment-crops.ts +6 -1
- package/src/compare-html.ts +22 -24
- package/src/compare-server.ts +1 -1
- package/src/compare-session.ts +41 -3
- package/src/correction-handoff.ts +27 -4
- package/src/create-pin-gate.ts +3 -0
- package/src/direction-inspection.ts +6 -2
- package/src/feedback-export.ts +68 -1
- package/src/front.ts +5 -200
- package/src/handoff.ts +1 -1
- package/src/interview-routes.ts +1 -1
- package/src/interview-scene.ts +3 -3
- package/src/job-act.ts +77 -73
- package/src/job-api.ts +326 -13
- package/src/job-checkpoint-evidence.ts +113 -0
- package/src/job-cli.ts +27 -14
- package/src/job-contract.ts +284 -62
- package/src/job-front.ts +157 -58
- package/src/job-host.ts +23 -4
- package/src/job-session.ts +109 -24
- package/src/job-source.ts +209 -57
- package/src/job-store.ts +80 -6
- package/src/job-worker.ts +1 -47
- package/src/joined-project.ts +61 -4
- package/src/manifest.ts +32 -4
- package/src/open-workspace.ts +12 -12
- package/src/open.ts +19 -12
- package/src/parts-comment-crops.ts +126 -31
- package/src/parts-comment.ts +351 -28
- package/src/parts-server.ts +19 -7
- package/src/parts-view.ts +588 -109
- package/src/scene-inspection.ts +50 -0
- package/src/server.ts +31 -22
- package/src/session.ts +58 -0
- package/src/stage-comment.ts +48 -1
- package/src/stage-parts.ts +23 -11
- package/web/animation-inspection.ts +1 -1
- package/web/bones-panel.ts +1 -3
- package/web/catalog-en.ts +175 -39
- package/web/catalog-ko.ts +175 -39
- package/web/cloth-panel.ts +1 -2
- package/web/compare.css +3 -13
- package/web/compare.html +3 -3
- package/web/compare.ts +69 -64
- package/web/cuts-panel.ts +1 -3
- package/web/density-strip-panel.ts +1 -2
- package/web/direction-panel.ts +22 -6
- package/web/i18n.ts +13 -1
- package/web/idle-panel.ts +1 -2
- package/web/index.html +1 -5
- package/web/inspection-persistence.ts +2 -1
- package/web/inspector-blocks.ts +149 -40
- package/web/instance-access.ts +24 -5
- package/web/interview-panel.ts +224 -175
- package/web/job-rig-capture.ts +164 -21
- package/web/job-rig-screen.ts +337 -0
- package/web/job-session.ts +611 -1437
- package/web/main.ts +614 -151
- package/web/mesh-panel.ts +1 -2
- package/web/owner-workspace.ts +88 -109
- package/web/part-selection.ts +47 -6
- package/web/part-thumbnails.ts +20 -6
- package/web/parts-render.ts +26 -19
- package/web/parts-viewport-provider.ts +181 -7
- package/web/physics-panel.ts +1 -2
- package/web/readings-panel.ts +1 -2
- package/web/requirements-panel.ts +1 -3
- package/web/retained-frame-stage.ts +105 -0
- package/web/retarget-panel.ts +2 -3
- package/web/rig-fields-panel.ts +1 -2
- package/web/rig-host.ts +64 -0
- package/web/rig-viewport-provider.ts +65 -16
- package/web/scene-plates.ts +57 -4
- package/web/stage-comments.ts +103 -46
- package/web/styles.css +117 -63
- package/web/view-controls.ts +201 -12
- package/web/viewport-capture.ts +12 -2
- package/web/viewport-controller.ts +107 -27
- package/web/viewport-pair.ts +2 -1
- package/web/workspace-preferences.ts +5 -4
- package/web/workspace-ui.ts +151 -19
- package/src/front-worker.ts +0 -2
- package/src/interview-server.ts +0 -54
- package/src/job-hub.ts +0 -304
- package/src/job-image-set.ts +0 -50
- package/web/interview.css +0 -28
- package/web/interview.html +0 -17
- package/web/interview.ts +0 -40
- package/web/job-hub.css +0 -14
- package/web/job-hub.html +0 -5
- package/web/job-hub.ts +0 -179
- package/web/owner-workspace.css +0 -14
- package/web/owner-workspace.html +0 -5
- package/web/parts-flags.ts +0 -48
- package/web/parts.css +0 -104
- package/web/parts.html +0 -49
- package/web/parts.ts +0 -604
- package/web/stage-nav.ts +0 -136
package/src/parts-view.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The parts and atlas view (#348): every part as an image, before any rig is built, and the atlas a built rig ships.
|
|
3
3
|
*
|
|
4
|
-
* Two inputs
|
|
4
|
+
* Two inputs can be read (only parts mode opens the parts workspace):
|
|
5
5
|
* - **parts mode** — a parts package as rig-parts assemble writes it: `parts.json` (draw order, back first) beside
|
|
6
6
|
* `parts/<name>.png`, each PNG cropped to its own box at `x`/`y` in rig pixels;
|
|
7
7
|
* - **atlas mode** — a review package (or rig-parts build root) whose compiled model declares `pages[].regions[]`.
|
|
@@ -16,33 +16,38 @@
|
|
|
16
16
|
* is relative to the verdict file's directory. rig-c/create recomputes it, so the definition is fixed here.
|
|
17
17
|
*/
|
|
18
18
|
import {differencePixels,assertOriginalPaintingSize} from './parts-comparison.ts';
|
|
19
|
-
import {
|
|
19
|
+
import {parsePartsIssue,assertPartsIssueBinding,type PartsComment,type PartsIssue} from './parts-comment.ts';
|
|
20
20
|
import {preparePartsComment,validateStoredPartsComment} from './parts-comment-crops.ts';
|
|
21
21
|
import {resolvedCropPath} from './comment-crops.ts';
|
|
22
22
|
import { createHash } from 'node:crypto';
|
|
23
23
|
import { existsSync } from 'node:fs';
|
|
24
|
-
import { mkdir, open, readFile, realpath, rename, rm, stat } from 'node:fs/promises';
|
|
24
|
+
import { mkdir, open, readFile, realpath, rename, rm, stat, lstat } from 'node:fs/promises';
|
|
25
25
|
import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep } from 'node:path';
|
|
26
26
|
import { PNG } from 'pngjs';
|
|
27
27
|
import { loadReview, safeFile, packagePath, type ReviewPackage } from './manifest.ts';
|
|
28
28
|
import { destinationDirectory } from './destination-directory.ts';
|
|
29
|
+
import {commentsObject, jobSha, jobInteger, parseComments} from './job-api.ts';
|
|
30
|
+
import {PARTS_COMMENTS_VERDICT_SCHEMA, type PartsCommentsVerdict} from './job-contract.ts';
|
|
29
31
|
|
|
30
|
-
export const PARTS_VERDICT_SCHEMA = 'rig-review-parts-verdict/
|
|
31
|
-
const
|
|
32
|
+
export const PARTS_VERDICT_SCHEMA = 'rig-review-parts-verdict/4';
|
|
33
|
+
const LEGACY_VERDICT_SCHEMAS = ['rig-review-parts-verdict/1', 'rig-review-parts-verdict/2', 'rig-review-parts-verdict/3'] as const;
|
|
34
|
+
export type LegacyVerdictSchema = typeof LEGACY_VERDICT_SCHEMAS[number];
|
|
32
35
|
export const PARTS_VERDICT_FILE = 'parts-review.json';
|
|
33
36
|
export const PARTS_NOTE_LIMIT = 10000;
|
|
34
37
|
export type PartsViewMode = 'parts' | 'atlas';
|
|
35
38
|
export type PartOutcome = 'pending' | 'clean' | 'flagged';
|
|
36
|
-
export type VerdictStatus = '
|
|
39
|
+
export type VerdictStatus = 'approved' | 'changes-requested';
|
|
40
|
+
export type LegacyVerdictStatus = 'pending' | 'approved' | 'changes-requested';
|
|
37
41
|
|
|
38
42
|
/** One file the verdict rests on: `path` relative to the verdict file's directory, POSIX separators. */
|
|
39
43
|
export interface HashedFile { path: string; bytes: number; sha256: string }
|
|
40
|
-
interface LocalFile { abs: string; bytes: number; sha256: string }
|
|
44
|
+
interface LocalFile { abs: string; bytes: number; sha256: string; declaredPath?: string }
|
|
41
45
|
|
|
42
46
|
/** A part (parts mode) or a named region (atlas mode). Boxes are rig px (parts) or page px (atlas). */
|
|
43
47
|
export interface PartMesh { vertices: number[]; triangles: number[]; figures: Record<string, unknown>; width: number; height: number; image: string }
|
|
44
48
|
export interface ViewItem {
|
|
45
49
|
mesh?: PartMesh;
|
|
50
|
+
meshUnavailable?: 'PARTS_MESH_GENERATED';
|
|
46
51
|
name: string;
|
|
47
52
|
/** Draw order, back first (parts mode); the region's place in the atlas file (atlas mode). */
|
|
48
53
|
index: number;
|
|
@@ -88,7 +93,7 @@ export interface PartsViewPackage {
|
|
|
88
93
|
pagePaths: string[];
|
|
89
94
|
}
|
|
90
95
|
|
|
91
|
-
export interface PartsViewOptions { rig?: string; meshReport?: string; check?: string; verdict?: string;painting?:string }
|
|
96
|
+
export interface PartsViewOptions { rig?: string; meshReport?: string; check?: string; verdict?: string; painting?: string; paintingRoot?: string }
|
|
92
97
|
|
|
93
98
|
const record = (v: unknown): Record<string, unknown> | null => v && typeof v === 'object' && !Array.isArray(v) ? v as Record<string, unknown> : null;
|
|
94
99
|
const finite = (v: unknown, min: number): v is number => typeof v === 'number' && Number.isFinite(v) && v >= min;
|
|
@@ -277,8 +282,16 @@ export function readPartMeshes(raw: unknown, items: ViewItem[], reports: unknown
|
|
|
277
282
|
if (a?.type !== 'mesh') continue;
|
|
278
283
|
const item = byName.get(name);
|
|
279
284
|
if (!item) refuse('PARTS_MESH_UNKNOWN_PART', `${name}: found mesh; required a part in parts.json`);
|
|
280
|
-
if (item!.mesh) refuse('PARTS_MESH_AMBIGUOUS', `${name}: found multiple meshes; required one attachment`);
|
|
285
|
+
if (item!.mesh || item!.meshUnavailable) refuse('PARTS_MESH_AMBIGUOUS', `${name}: found multiple meshes; required one attachment`);
|
|
281
286
|
if (!finite(a.width, 1) || !Number.isInteger(a.width) || !finite(a.height, 1) || !Number.isInteger(a.height) || typeof a.image !== 'string') refuse('PARTS_MESH_SIZE', `${name}: found invalid raster declaration; required image and positive integer dimensions`);
|
|
287
|
+
if (Object.hasOwn(a, 'generator')) {
|
|
288
|
+
if (!record(a.generator) || Object.hasOwn(a, 'uvs')) refuse('PARTS_MESH_UV', `${name}: found invalid uvs; required nonempty coordinate pairs in [0,1]`);
|
|
289
|
+
if (Object.hasOwn(a, 'triangles')) refuse('PARTS_MESH_TRIANGLE', `${name}: found invalid triangles; required nonempty index triples`);
|
|
290
|
+
const rows = Array.isArray(reports) ? reports.filter(r => record(r)?.part === name) : [];
|
|
291
|
+
if (rows.length > 1) refuse('PARTS_MESH_REPORT_AMBIGUOUS', `${name}: found ${rows.length} report rows; required at most one`);
|
|
292
|
+
item!.meshUnavailable = 'PARTS_MESH_GENERATED';
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
282
295
|
const uv = a.uvs, triangles = a.triangles;
|
|
283
296
|
if (!Array.isArray(uv) || !uv.length || uv.length % 2) refuse('PARTS_MESH_UV', `${name}: found invalid uvs; required nonempty coordinate pairs in [0,1]`);
|
|
284
297
|
for (const v of uv as unknown[]) if (!finite(v, 0) || v > 1) refuse('PARTS_MESH_UV', `${name}: found ${String(v)}; required UV in [0,1]`);
|
|
@@ -330,6 +343,66 @@ async function locateMeshRasters(rig: unknown, root: string, items: ViewItem[],
|
|
|
330
343
|
}
|
|
331
344
|
}
|
|
332
345
|
|
|
346
|
+
/** Resolve the existing inline declaration, or the frozen adjacent sidecar only when absent. */
|
|
347
|
+
export async function resolvePartsPaintingInRig(root: string, manifestBytes: Buffer,
|
|
348
|
+
read: (absolute: string, kind: 'sidecar' | 'painting') => Promise<Buffer> = absolute => readFile(absolute),
|
|
349
|
+
): Promise<{paintingInRig?: PartsViewPackage['paintingInRig']; files: LocalFile[]; sidecar?: true}> {
|
|
350
|
+
const doc = record(JSON.parse(manifestBytes.toString('utf8')));
|
|
351
|
+
if (!doc) return refuse('PARTS_JSON_MALFORMED', 'parts.json must be an object');
|
|
352
|
+
const size = doc.rig_size;
|
|
353
|
+
const rigSize = Array.isArray(size) && size.length === 2 && size.every(n => finite(n, 1)) ? size : null;
|
|
354
|
+
const files: LocalFile[] = [];
|
|
355
|
+
let d = record(doc.painting_in_rig), sidecar: true | undefined;
|
|
356
|
+
if (Object.hasOwn(doc, 'painting_in_rig')) {
|
|
357
|
+
if (!d || Object.keys(d).length !== 3) refuse('PARTS_DIFFERENCE_PROVENANCE', 'declared path, lowercase SHA-256 and lanczos3-rgb-opaque required');
|
|
358
|
+
} else {
|
|
359
|
+
const declaredPath = join(root, 'painting_in_rig.json');
|
|
360
|
+
const info = await lstat(declaredPath).catch(error => {
|
|
361
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return null;
|
|
362
|
+
return refuse('PARTS_DIFFERENCE_PATH', 'sidecar must be readable inside the package');
|
|
363
|
+
});
|
|
364
|
+
if (!info) return {files};
|
|
365
|
+
const abs = await safeFile(root, 'painting_in_rig.json').catch(() => refuse('PARTS_DIFFERENCE_PATH', 'sidecar must be readable inside the package'));
|
|
366
|
+
const bytes = await read(abs, 'sidecar').catch(error => {
|
|
367
|
+
if (['ENOENT', 'EACCES', 'EPERM', 'EISDIR'].includes((error as NodeJS.ErrnoException).code ?? '')) return refuse('PARTS_DIFFERENCE_PATH', 'sidecar must be readable inside the package');
|
|
368
|
+
throw error;
|
|
369
|
+
});
|
|
370
|
+
try { d = record(JSON.parse(bytes.toString('utf8'))); }
|
|
371
|
+
catch { refuse('PARTS_DIFFERENCE_PROVENANCE', 'sidecar must be a JSON declaration'); }
|
|
372
|
+
const keys = ['schema', 'manifest_sha256', 'path', 'sha256', 'resample', 'rig_size'];
|
|
373
|
+
if (!d || Object.keys(d).length !== keys.length || !keys.every(key => Object.hasOwn(d!, key)) ||
|
|
374
|
+
d.schema !== 'rig-parts-painting-reference/1' || typeof d.manifest_sha256 !== 'string' || !/^[a-f0-9]{64}$/.test(d.manifest_sha256)) {
|
|
375
|
+
refuse('PARTS_DIFFERENCE_PROVENANCE', 'sidecar requires the six frozen painting reference fields');
|
|
376
|
+
}
|
|
377
|
+
if (d!.manifest_sha256 !== hashBytes(join(root, 'parts.json'), manifestBytes).sha256) refuse('PARTS_DIFFERENCE_HASH', 'sidecar manifest hash differs');
|
|
378
|
+
const declaredSize = d!.rig_size;
|
|
379
|
+
if (!Array.isArray(declaredSize) || declaredSize.length !== 2 || !declaredSize.every(n => Number.isSafeInteger(n) && n > 0) ||
|
|
380
|
+
!rigSize || declaredSize.some((n, i) => n !== rigSize[i])) refuse('PARTS_DIFFERENCE_SIZE', 'sidecar rig_size must equal parts.json rig_size');
|
|
381
|
+
files.push({...hashBytes(abs, bytes), declaredPath});
|
|
382
|
+
sidecar = true;
|
|
383
|
+
}
|
|
384
|
+
if (!d || typeof d.path !== 'string' || typeof d.sha256 !== 'string' || !/^[a-f0-9]{64}$/.test(d.sha256) || d.resample !== 'lanczos3-rgb-opaque') {
|
|
385
|
+
refuse('PARTS_DIFFERENCE_PROVENANCE', 'declared path, lowercase SHA-256 and lanczos3-rgb-opaque required');
|
|
386
|
+
}
|
|
387
|
+
try { packagePath(d!.path, 'painting_in_rig'); } catch { refuse('PARTS_DIFFERENCE_PATH', 'literal portable inside-package path required'); }
|
|
388
|
+
const declaredPath = join(root, d!.path as string);
|
|
389
|
+
const unreadable = () => refuse('PARTS_DIFFERENCE_PATH', 'declared painting must be readable inside the package');
|
|
390
|
+
const abs = await safeFile(root, d!.path as string).catch(unreadable);
|
|
391
|
+
const bytes = await read(abs, 'painting').catch(error => {
|
|
392
|
+
if (['ENOENT', 'EACCES', 'EPERM', 'EISDIR'].includes((error as NodeJS.ErrnoException).code ?? '')) return unreadable();
|
|
393
|
+
throw error;
|
|
394
|
+
}), sha256 = hashBytes(abs, bytes).sha256;
|
|
395
|
+
if (sha256 !== d!.sha256) refuse('PARTS_DIFFERENCE_HASH', 'declared painting hash differs');
|
|
396
|
+
let png: PNG;
|
|
397
|
+
try { png = PNG.sync.read(bytes); } catch { return refuse('PARTS_DIFFERENCE_PNG', 'declared painting must be RGBA8 PNG'); }
|
|
398
|
+
if (bytes[24] !== 8 || bytes[25] !== 6) refuse('PARTS_DIFFERENCE_PNG', 'declared painting must be RGBA8 PNG');
|
|
399
|
+
if (!rigSize || !rigSize.every(Number.isSafeInteger) || png.width !== rigSize[0] || png.height !== rigSize[1]) refuse('PARTS_DIFFERENCE_SIZE', 'declared painting dimensions must equal integer rig_size');
|
|
400
|
+
for (let i = 3; i < png.data.length; i += 4) if (png.data[i] !== 255) refuse('PARTS_DIFFERENCE_ALPHA', 'declared painting must have alpha 255');
|
|
401
|
+
const paintingInRig = {abs, declaredPath, bytes: bytes.length, sha256, width: png.width, height: png.height};
|
|
402
|
+
files.push(paintingInRig);
|
|
403
|
+
return {paintingInRig, files, ...(sidecar ? {sidecar} : {})};
|
|
404
|
+
}
|
|
405
|
+
|
|
333
406
|
async function loadPartsMode(root: string, options: PartsViewOptions): Promise<PartsViewPackage> {
|
|
334
407
|
const partsJson = await insideRoot(root, join(root, 'parts.json'), 'PARTS_FILE_OUTSIDE', 'parts.json');
|
|
335
408
|
const { bytes: partsBytes, value: partsRaw } = await readJsonBytes(partsJson, 'parts.json');
|
|
@@ -354,27 +427,17 @@ async function loadPartsMode(root: string, options: PartsViewOptions): Promise<P
|
|
|
354
427
|
imagePaths.push(real);
|
|
355
428
|
items.push({ name: part.name, index, x: part.x, y: part.y, w: part.w, h: part.h, image: true });
|
|
356
429
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
const d=record(doc.painting_in_rig);
|
|
360
|
-
if(!d||Object.keys(d).length!==3||typeof d.path!=='string'||typeof d.sha256!=='string'||!/^[a-f0-9]{64}$/.test(d.sha256)||d.resample!=='lanczos3-rgb-opaque')refuse('PARTS_DIFFERENCE_PROVENANCE','declared path, lowercase SHA-256 and lanczos3-rgb-opaque required');
|
|
361
|
-
try{packagePath(d!.path,'painting_in_rig');}catch{refuse('PARTS_DIFFERENCE_PATH','literal portable inside-package path required');}
|
|
362
|
-
const declaredPath=join(root,d!.path as string);
|
|
363
|
-
const unreadable=()=>refuse('PARTS_DIFFERENCE_PATH','declared painting must be readable inside the package');
|
|
364
|
-
const abs=await safeFile(root,d!.path as string).catch(unreadable);
|
|
365
|
-
const bytes=await readFile(abs).catch(unreadable);
|
|
366
|
-
const sha256=createHash('sha256').update(bytes).digest('hex');
|
|
367
|
-
if(sha256!==d!.sha256)refuse('PARTS_DIFFERENCE_HASH','declared painting hash differs');
|
|
368
|
-
let png:PNG;try{png=PNG.sync.read(bytes);}catch{refuse('PARTS_DIFFERENCE_PNG','declared painting must be RGBA8 PNG');}
|
|
369
|
-
if(bytes[24]!==8||bytes[25]!==6)refuse('PARTS_DIFFERENCE_PNG','declared painting must be RGBA8 PNG');
|
|
370
|
-
if(!rigSize||!rigSize.every(Number.isSafeInteger)||png!.width!==rigSize[0]||png!.height!==rigSize[1])refuse('PARTS_DIFFERENCE_SIZE','declared painting dimensions must equal integer rig_size');
|
|
371
|
-
for(let i=3;i<png!.data.length;i+=4)if(png!.data[i]!==255)refuse('PARTS_DIFFERENCE_ALPHA','declared painting must have alpha 255');
|
|
372
|
-
paintingInRig={abs,declaredPath,bytes:bytes.length,sha256,width:png!.width,height:png!.height};files.push(paintingInRig);
|
|
373
|
-
}
|
|
430
|
+
const {paintingInRig, files: paintingFiles} = await resolvePartsPaintingInRig(root, partsBytes);
|
|
431
|
+
files.push(...paintingFiles);
|
|
374
432
|
let painting:PartsViewPackage['painting'];if(options.painting){
|
|
375
433
|
// Both paths are physical: a user-designated alias to an inside file is valid,
|
|
376
434
|
// while a link reaching outside is refused before any painting bytes are read.
|
|
377
|
-
let abs:string,bytes:Buffer;try{
|
|
435
|
+
let abs:string,bytes:Buffer;try{
|
|
436
|
+
const paintingRoot = options.paintingRoot !== undefined ? await realpath(resolve(options.paintingRoot)) : root;
|
|
437
|
+
abs = await insideRoot(paintingRoot, resolve(options.painting), 'PARTS_PAINTING_OUTSIDE', 'painting');
|
|
438
|
+
packagePath(relative(paintingRoot, abs).split(sep).join('/'), 'painting');
|
|
439
|
+
bytes = await readFile(abs);
|
|
440
|
+
}catch(error){if(error instanceof Error&&error.message.startsWith('PARTS_PAINTING_OUTSIDE:'))throw error;refuse('PARTS_PAINTING_UNAVAILABLE','painting must be a readable portable package PNG');}
|
|
378
441
|
let png:PNG;try{png=PNG.sync.read(bytes!);}catch{refuse('PARTS_PAINTING_PNG','painting must be a valid PNG');}
|
|
379
442
|
assertOriginalPaintingSize(png!.width,png!.height,rigSize,doc.scale_rig_per_source);
|
|
380
443
|
const file={abs:abs!,bytes:bytes!.length,sha256:createHash('sha256').update(bytes!).digest('hex')};painting={...file,width:png!.width,height:png!.height};files.push(file);
|
|
@@ -457,6 +520,11 @@ export async function loadPartsView(input: string, options: PartsViewOptions = {
|
|
|
457
520
|
return checkedVerdict(await loadAtlasMode(input, options));
|
|
458
521
|
}
|
|
459
522
|
|
|
523
|
+
/** Opening is narrower than loading: keep embedded atlas inspection and saved verdict reads available. */
|
|
524
|
+
export function assertPartsCanOpen(pkg: Pick<PartsViewPackage, 'mode'>): void {
|
|
525
|
+
if (pkg.mode === 'atlas') refuse('PARTS_ATLAS_NOT_CARRIED', 'atlas inspection is not available in the parts workspace; open the rig review instead.');
|
|
526
|
+
}
|
|
527
|
+
|
|
460
528
|
/** Compare names, real paths (including parent links), and filesystem identities before creating output. */
|
|
461
529
|
async function assertVerdictDestination(pkg: Pick<PartsViewPackage, 'files' | 'flagFiles' | 'protectedPaths' | 'verdictPath'>): Promise<void> {
|
|
462
530
|
const target = resolve(pkg.verdictPath);
|
|
@@ -490,61 +558,292 @@ export interface PartsVerdict {
|
|
|
490
558
|
tool: { name: '@rig-c/review'; version: string };
|
|
491
559
|
source: ReturnType<typeof verdictSource>;
|
|
492
560
|
status: VerdictStatus;
|
|
493
|
-
|
|
494
|
-
comments: PartsComment[];
|
|
561
|
+
issues: PartsIssue[];
|
|
495
562
|
updatedAt: string;
|
|
563
|
+
by?: string;
|
|
496
564
|
}
|
|
497
565
|
|
|
498
|
-
/**
|
|
499
|
-
export
|
|
566
|
+
/** The standalone comments envelope is a decoder until the consumer activation receipt. */
|
|
567
|
+
export function parseCommentsPartsVerdict(value: unknown): PartsCommentsVerdict {
|
|
568
|
+
const raw = commentsObject(value, ['schema', 'mode', 'tool', 'source', 'status', 'comments', 'updatedAt'], ['by']);
|
|
569
|
+
if (raw.schema !== PARTS_COMMENTS_VERDICT_SCHEMA || typeof raw.mode !== 'string' || !['parts', 'atlas'].includes(raw.mode) ||
|
|
570
|
+
typeof raw.status !== 'string' || !['approved', 'changes-requested'].includes(raw.status)) throw new Error('JOB_ACT_INVALID');
|
|
571
|
+
const tool = commentsObject(raw.tool, ['name', 'version']);
|
|
572
|
+
if (tool.name !== '@rig-c/review' || typeof tool.version !== 'string' || !tool.version) throw new Error('JOB_ACT_INVALID');
|
|
573
|
+
const source = commentsObject(raw.source, ['sha256', 'files', 'flags']);
|
|
574
|
+
jobSha(source.sha256);
|
|
575
|
+
if (!Array.isArray(source.files) || !source.files.length || !Array.isArray(source.flags)) throw new Error('JOB_ACT_INVALID');
|
|
576
|
+
for (const file of source.files) {
|
|
577
|
+
const row = commentsObject(file, ['path', 'sha256', 'bytes']);
|
|
578
|
+
if (typeof row.path !== 'string' || !row.path) throw new Error('JOB_ACT_INVALID');
|
|
579
|
+
jobSha(row.sha256); jobInteger(row.bytes);
|
|
580
|
+
}
|
|
581
|
+
for (const file of source.flags) {
|
|
582
|
+
const row = commentsObject(file, ['path', 'sha256']);
|
|
583
|
+
if (typeof row.path !== 'string' || !row.path) throw new Error('JOB_ACT_INVALID');
|
|
584
|
+
jobSha(row.sha256);
|
|
585
|
+
}
|
|
586
|
+
if (sourceSha256(source.files as HashedFile[]) !== source.sha256) throw new Error('JOB_ACT_INVALID');
|
|
587
|
+
if (typeof raw.updatedAt !== 'string' || !Number.isFinite(Date.parse(raw.updatedAt)) ||
|
|
588
|
+
'by' in raw && (typeof raw.by !== 'string' || !raw.by)) throw new Error('JOB_ACT_INVALID');
|
|
589
|
+
const comments = parseComments(raw.comments, source.sha256 as string);
|
|
590
|
+
const issues = comments.filter(comment => comment.anchor && 'schema' in comment.anchor).length;
|
|
591
|
+
if (comments.some(comment => comment.anchor && !('schema' in comment.anchor)) ||
|
|
592
|
+
(raw.status === 'approved') !== (issues === 0)) throw new Error('JOB_ACT_INVALID');
|
|
593
|
+
return {...structuredClone(raw), comments} as unknown as PartsCommentsVerdict;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
export interface LegacyPartsVerdictView {
|
|
597
|
+
schema: LegacyVerdictSchema;
|
|
598
|
+
status: LegacyVerdictStatus;
|
|
599
|
+
readOnly: true;
|
|
600
|
+
code: 'PARTS_VERDICT_LEGACY_FORMAT';
|
|
601
|
+
flaggedCount: number;
|
|
602
|
+
flaggedParts: { name: string; note: string }[];
|
|
603
|
+
issues: PartsIssue[];
|
|
604
|
+
by?: string;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
export interface LegacyPartsVerdictRecord {
|
|
608
|
+
schema: LegacyVerdictSchema;
|
|
609
|
+
mode: PartsViewMode;
|
|
610
|
+
tool: { name: '@rig-c/review'; version: string };
|
|
611
|
+
source: ReturnType<typeof verdictSource>;
|
|
612
|
+
status: LegacyVerdictStatus;
|
|
613
|
+
parts: Record<string, { outcome: PartOutcome; note: string; flags: string[]; mesh?: { outcome: PartOutcome; note: string; flags: string[] } }>;
|
|
614
|
+
comments?: import('./parts-comment.ts').PartsComment[];
|
|
615
|
+
issues: PartsIssue[];
|
|
616
|
+
updatedAt: string;
|
|
617
|
+
by?: string;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/** What the page submits: the status and pointed issues. */
|
|
621
|
+
export interface VerdictSubmission { status: VerdictStatus; issues: PartsIssue[] }
|
|
500
622
|
|
|
501
623
|
/** The distinct names, in item order (atlas sequence frames share a name). */
|
|
502
624
|
export const itemNames = (pkg: Pick<PartsViewPackage, 'items'>): string[] => [...new Set(pkg.items.map(i => i.name))];
|
|
503
625
|
|
|
504
626
|
/** Builds the verdict document, or refuses by name. Pure but for the clock. */
|
|
505
|
-
export function buildVerdict(
|
|
627
|
+
export function buildVerdict(
|
|
628
|
+
pkg: Pick<PartsViewPackage, 'mode' | 'items' | 'flags' | 'files' | 'flagFiles' | 'verdictPath' | 'imagePaths' | 'painting' | 'paintingInRig'>,
|
|
629
|
+
input: unknown,
|
|
630
|
+
version: string,
|
|
631
|
+
now = new Date(),
|
|
632
|
+
by?: string,
|
|
633
|
+
): PartsVerdict {
|
|
506
634
|
const body = record(input);
|
|
507
|
-
if (!body
|
|
635
|
+
if (!body || body.parts !== undefined || body.comments !== undefined) {
|
|
636
|
+
return refuse('PARTS_VERDICT_INVALID', 'expected { status, issues }');
|
|
637
|
+
}
|
|
508
638
|
const status = body.status;
|
|
509
|
-
if (status !== '
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
const
|
|
513
|
-
|
|
514
|
-
|
|
639
|
+
if (status !== 'approved' && status !== 'changes-requested') {
|
|
640
|
+
return refuse('PARTS_VERDICT_INVALID', 'status is approved or changes-requested');
|
|
641
|
+
}
|
|
642
|
+
const issues = body.issues;
|
|
643
|
+
if (!Array.isArray(issues) || issues.length > 1000) {
|
|
644
|
+
return refuse('PARTS_COMMENT_LIMIT', 'issues must be an array of at most 1000 entries');
|
|
645
|
+
}
|
|
646
|
+
if (status === 'approved' && issues.length > 0) {
|
|
647
|
+
return refuse('PARTS_VERDICT_NOT_CLEAN', `approved needs zero issues; ${issues.length} present`);
|
|
648
|
+
}
|
|
649
|
+
if (status === 'changes-requested' && issues.length === 0) {
|
|
650
|
+
return refuse('PARTS_VERDICT_NOTHING_FLAGGED', 'changes-requested needs at least one issue');
|
|
651
|
+
}
|
|
652
|
+
if (by !== undefined && (typeof by !== 'string' || !by.trim())) {
|
|
653
|
+
return refuse('PARTS_VERDICT_INVALID', 'reviewer name must be nonblank');
|
|
654
|
+
}
|
|
655
|
+
const itemMap = new Map(
|
|
656
|
+
pkg.items.map(i => [i.name, { ...i, imageHash: pkg.files.find(f => f.abs === pkg.imagePaths[i.index])?.sha256 ?? '' }]),
|
|
657
|
+
);
|
|
658
|
+
const sourceHash = verdictSource(pkg).sha256;
|
|
659
|
+
const ids = new Set<string>();
|
|
660
|
+
const parsedIssues: PartsIssue[] = [];
|
|
661
|
+
for (const raw of issues as unknown[]) {
|
|
662
|
+
const c = parsePartsIssue(raw, true, false);
|
|
663
|
+
if (c.view.comparison?.mode === 'overlay' && !pkg.painting) {
|
|
664
|
+
refuse('PARTS_COMPARISON_UNAVAILABLE', 'original painting required');
|
|
665
|
+
}
|
|
666
|
+
if (c.view.comparison?.mode === 'difference' && !pkg.paintingInRig) {
|
|
667
|
+
refuse('PARTS_DIFFERENCE_UNAVAILABLE', 'declared painting_in_rig required');
|
|
668
|
+
}
|
|
669
|
+
if (pkg.mode !== 'parts') {
|
|
670
|
+
refuse('PARTS_COMMENT_UNAVAILABLE', 'original part required');
|
|
671
|
+
}
|
|
672
|
+
if (ids.has(c.id)) {
|
|
673
|
+
refuse('PARTS_COMMENT_DUPLICATE', 'duplicate issue id');
|
|
674
|
+
}
|
|
675
|
+
ids.add(c.id);
|
|
676
|
+
assertPartsIssueBinding(c, itemMap, sourceHash);
|
|
677
|
+
parsedIssues.push(c);
|
|
678
|
+
}
|
|
679
|
+
return {
|
|
680
|
+
schema: PARTS_VERDICT_SCHEMA,
|
|
681
|
+
mode: pkg.mode,
|
|
682
|
+
tool: { name: '@rig-c/review', version },
|
|
683
|
+
source: verdictSource(pkg),
|
|
684
|
+
status,
|
|
685
|
+
issues: parsedIssues,
|
|
686
|
+
updatedAt: now.toISOString(),
|
|
687
|
+
...(by !== undefined ? { by } : {}),
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/** Decode-only reader for legacy /1, /2 and /3 files: /1 comments become /2 item issues; uncommented flagged rows and pending open read-only. */
|
|
692
|
+
async function decodeLegacyVerdict(
|
|
693
|
+
pkg: PartsViewPackage,
|
|
694
|
+
v: Record<string, unknown>,
|
|
695
|
+
version: string,
|
|
696
|
+
date: Date,
|
|
697
|
+
by: string | null,
|
|
698
|
+
) {
|
|
699
|
+
const status = v.status;
|
|
700
|
+
if (status !== 'pending' && status !== 'approved' && status !== 'changes-requested') {
|
|
701
|
+
return refuse('PARTS_VERDICT_INVALID', 'status is pending, approved or changes-requested');
|
|
702
|
+
}
|
|
703
|
+
const given = record(v.parts);
|
|
704
|
+
if (!given) {
|
|
705
|
+
return refuse('PARTS_VERDICT_INVALID', 'parts is an object keyed by part name');
|
|
706
|
+
}
|
|
707
|
+
const names = itemNames(pkg);
|
|
708
|
+
const known = new Set(names);
|
|
709
|
+
for (const name of Object.keys(given)) {
|
|
710
|
+
if (!known.has(name)) {
|
|
711
|
+
return refuse('PARTS_VERDICT_UNKNOWN_PART', `${name} is not a part of this input`);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
const parts: LegacyPartsVerdictRecord['parts'] = {};
|
|
515
715
|
for (const name of names) {
|
|
516
716
|
const entry = record(given[name]);
|
|
517
|
-
if (!entry)
|
|
717
|
+
if (!entry) {
|
|
718
|
+
return refuse('PARTS_VERDICT_MISSING_PART', `${name} has no entry`);
|
|
719
|
+
}
|
|
518
720
|
const outcome = entry.outcome;
|
|
519
|
-
if (outcome !== 'pending' && outcome !== 'clean' && outcome !== 'flagged')
|
|
721
|
+
if (outcome !== 'pending' && outcome !== 'clean' && outcome !== 'flagged') {
|
|
722
|
+
return refuse('PARTS_VERDICT_INVALID', `${name}.outcome is pending, clean or flagged`);
|
|
723
|
+
}
|
|
520
724
|
const note = entry.note === undefined ? '' : entry.note;
|
|
521
|
-
if (typeof note !== 'string' || note.length > PARTS_NOTE_LIMIT)
|
|
522
|
-
|
|
725
|
+
if (typeof note !== 'string' || note.length > PARTS_NOTE_LIMIT) {
|
|
726
|
+
return refuse('PARTS_VERDICT_INVALID', `${name}.note is a string of at most ${PARTS_NOTE_LIMIT} characters`);
|
|
727
|
+
}
|
|
728
|
+
if (outcome === 'flagged' && !note.trim()) {
|
|
729
|
+
return refuse('PARTS_VERDICT_NOTE_REQUIRED', `${name} is flagged and has no note saying what is wrong`);
|
|
730
|
+
}
|
|
731
|
+
const partFlags = Array.isArray(entry.flags) ? entry.flags.map(String) : [];
|
|
523
732
|
const meshInput = record(entry.mesh);
|
|
524
733
|
const hasMesh = pkg.items.some(i => i.name === name && i.mesh);
|
|
525
|
-
if (!hasMesh && entry.mesh !== undefined)
|
|
526
|
-
|
|
734
|
+
if (!hasMesh && entry.mesh !== undefined) {
|
|
735
|
+
return refuse('PARTS_MESH_OUTCOME_UNAVAILABLE', `${name}: no mesh; required no mesh outcome`);
|
|
736
|
+
}
|
|
737
|
+
let mesh: { outcome: PartOutcome; note: string; flags: string[] } | undefined;
|
|
527
738
|
if (hasMesh) {
|
|
528
|
-
if (!meshInput || !['pending', 'clean', 'flagged'].includes(String(meshInput.outcome)))
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
739
|
+
if (!meshInput || !['pending', 'clean', 'flagged'].includes(String(meshInput.outcome))) {
|
|
740
|
+
return refuse('PARTS_MESH_OUTCOME', `${name}: required mesh outcome pending, clean or flagged`);
|
|
741
|
+
}
|
|
742
|
+
const meshNote = meshInput.note ?? '';
|
|
743
|
+
if (typeof meshNote !== 'string' || meshNote.length > PARTS_NOTE_LIMIT) {
|
|
744
|
+
return refuse('PARTS_MESH_OUTCOME', `${name}: required mesh note up to ${PARTS_NOTE_LIMIT} characters`);
|
|
745
|
+
}
|
|
746
|
+
if (meshInput.outcome === 'flagged' && !meshNote.trim()) {
|
|
747
|
+
return refuse('PARTS_VERDICT_NOTE_REQUIRED', `${name}.mesh is flagged and has no note`);
|
|
748
|
+
}
|
|
749
|
+
const meshFlags = Array.isArray(meshInput.flags) ? meshInput.flags.map(String) : [];
|
|
750
|
+
mesh = { outcome: meshInput.outcome as PartOutcome, note: meshNote, flags: meshFlags };
|
|
533
751
|
}
|
|
534
|
-
parts[name] = { ...(mesh ? { mesh } : {}), outcome, note, flags:
|
|
752
|
+
parts[name] = { ...(mesh ? { mesh } : {}), outcome, note, flags: partFlags };
|
|
753
|
+
}
|
|
754
|
+
const outcomes = Object.values(parts).flatMap(p => (p.mesh ? [p.outcome, p.mesh.outcome] : [p.outcome]));
|
|
755
|
+
if (status === 'approved' && outcomes.some(o => o !== 'clean')) {
|
|
756
|
+
return refuse('PARTS_VERDICT_NOT_CLEAN', 'approved needs every part clean');
|
|
757
|
+
}
|
|
758
|
+
if (status === 'changes-requested' && !outcomes.includes('flagged')) {
|
|
759
|
+
return refuse('PARTS_VERDICT_NOTHING_FLAGGED', 'changes-requested needs at least one flagged part');
|
|
535
760
|
}
|
|
536
|
-
const
|
|
537
|
-
if (
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
const ids=new Set<string>();
|
|
541
|
-
|
|
761
|
+
const rawComments = v.schema === 'rig-review-parts-verdict/1' ? [] : (v.comments ?? []);
|
|
762
|
+
if (!Array.isArray(rawComments) || rawComments.length > 1000) {
|
|
763
|
+
return refuse('PARTS_COMMENT_LIMIT', 'comments must be an array of at most 1000 entries');
|
|
764
|
+
}
|
|
765
|
+
const ids = new Set<string>();
|
|
766
|
+
const issues: PartsIssue[] = [];
|
|
767
|
+
const legacyComments: import('./parts-comment.ts').PartsComment[] = [];
|
|
768
|
+
for (const raw of rawComments as unknown[]) {
|
|
769
|
+
const c = await validateStoredPartsComment(pkg, raw, true);
|
|
770
|
+
if (ids.has(c.id)) {
|
|
771
|
+
return refuse('PARTS_COMMENT_DUPLICATE', 'duplicate comment id');
|
|
772
|
+
}
|
|
773
|
+
ids.add(c.id);
|
|
774
|
+
issues.push(c);
|
|
775
|
+
const primary = c.parts[0]!;
|
|
776
|
+
const { mode: _mode, ...restView } = c.view;
|
|
777
|
+
legacyComments.push({
|
|
778
|
+
schema: 'rig-review-parts-comment/1',
|
|
779
|
+
id: c.id,
|
|
780
|
+
part: primary.name,
|
|
781
|
+
imageHash: primary.imageHash,
|
|
782
|
+
sourceHash: c.sourceHash,
|
|
783
|
+
summary: c.summary,
|
|
784
|
+
imageRegion: c.imageRegion!,
|
|
785
|
+
rigRegion: c.rigRegion,
|
|
786
|
+
crop: { ...c.crop, png: c.crop.png ?? '' },
|
|
787
|
+
nativeCrop: c.nativeCrop!,
|
|
788
|
+
view: restView,
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
const commentedParts = new Set(issues.flatMap(i => i.parts.map(p => p.name)));
|
|
792
|
+
const flaggedParts: { name: string; note: string }[] = [];
|
|
793
|
+
for (const name of names) {
|
|
794
|
+
const p = parts[name]!;
|
|
795
|
+
const pf = p.outcome === 'flagged';
|
|
796
|
+
const mf = p.mesh?.outcome === 'flagged';
|
|
797
|
+
if ((pf || mf) && !commentedParts.has(name)) {
|
|
798
|
+
flaggedParts.push({
|
|
799
|
+
name,
|
|
800
|
+
note: [pf ? p.note.trim() : '', mf ? p.mesh!.note.trim() : ''].filter(Boolean).join(' / '),
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
const legacyRecord: LegacyPartsVerdictRecord = {
|
|
805
|
+
schema: v.schema as LegacyVerdictSchema,
|
|
806
|
+
mode: pkg.mode,
|
|
807
|
+
tool: { name: '@rig-c/review', version },
|
|
808
|
+
source: verdictSource(pkg),
|
|
809
|
+
status,
|
|
810
|
+
parts,
|
|
811
|
+
...(v.schema === 'rig-review-parts-verdict/1' ? {} : { comments: legacyComments }),
|
|
812
|
+
issues,
|
|
813
|
+
updatedAt: date.toISOString(),
|
|
814
|
+
...(by ? { by } : {}),
|
|
815
|
+
};
|
|
816
|
+
if (flaggedParts.length > 0 || status === 'pending' || (status === 'approved') !== (issues.length === 0)) {
|
|
817
|
+
const legacy: LegacyPartsVerdictView = {
|
|
818
|
+
schema: v.schema as LegacyVerdictSchema,
|
|
819
|
+
status,
|
|
820
|
+
readOnly: true,
|
|
821
|
+
code: 'PARTS_VERDICT_LEGACY_FORMAT',
|
|
822
|
+
flaggedCount: flaggedParts.length,
|
|
823
|
+
flaggedParts,
|
|
824
|
+
issues,
|
|
825
|
+
...(by ? { by } : {}),
|
|
826
|
+
};
|
|
827
|
+
return { verdict: null, legacy, legacyRecord };
|
|
828
|
+
}
|
|
829
|
+
const verdict: PartsVerdict = {
|
|
830
|
+
schema: PARTS_VERDICT_SCHEMA,
|
|
831
|
+
mode: pkg.mode,
|
|
832
|
+
tool: { name: '@rig-c/review', version },
|
|
833
|
+
source: verdictSource(pkg),
|
|
834
|
+
status,
|
|
835
|
+
issues,
|
|
836
|
+
updatedAt: date.toISOString(),
|
|
837
|
+
...(by ? { by } : {}),
|
|
838
|
+
};
|
|
839
|
+
return { verdict, legacy: null, legacyRecord };
|
|
542
840
|
}
|
|
543
841
|
|
|
544
842
|
/** Every file the package was loaded from still has the bytes it was loaded with. */
|
|
545
843
|
export async function assertPartsUnchanged(pkg: Pick<PartsViewPackage, 'files' | 'flagFiles' | 'paintingInRig'>): Promise<void> {
|
|
546
844
|
if(pkg.paintingInRig&&await realpath(pkg.paintingInRig.declaredPath).catch(()=>null)!==pkg.paintingInRig.abs)refuse('PARTS_VIEW_SOURCE_CHANGED','declared painting path changed');
|
|
547
845
|
for (const f of [...pkg.files, ...pkg.flagFiles]) {
|
|
846
|
+
if (f.declaredPath && await realpath(f.declaredPath).catch(() => null) !== f.abs) refuse('PARTS_VIEW_SOURCE_CHANGED', 'declared input path changed');
|
|
548
847
|
const now = await hashFile(f.abs).catch(() => null);
|
|
549
848
|
if (!now || now.sha256 !== f.sha256) refuse('PARTS_VIEW_SOURCE_CHANGED', `${basename(f.abs)} changed after the view opened; reopen it to review what is on disk`);
|
|
550
849
|
}
|
|
@@ -572,11 +871,10 @@ export async function saveVerdictWithReceipt(pkg: PartsViewPackage, input: unkno
|
|
|
572
871
|
await assertVerdictDestination(pkg);
|
|
573
872
|
await assertPartsUnchanged(pkg);
|
|
574
873
|
await readVerdict(pkg); // Existing read-side evidence errors must never become an implicit reset.
|
|
575
|
-
const verdict = buildVerdict(pkg, input, await packageVersion());
|
|
576
|
-
for(const
|
|
874
|
+
const verdict = buildVerdict(pkg, input, await packageVersion(), new Date(), by);
|
|
875
|
+
for(const issue of verdict.issues)await preparePartsComment(pkg,issue,true);
|
|
577
876
|
await assertPartsUnchanged(pkg);
|
|
578
|
-
const
|
|
579
|
-
const bytes = Buffer.from(JSON.stringify(stored, null, 2) + '\n');
|
|
877
|
+
const bytes = Buffer.from(JSON.stringify(verdict, null, 2) + '\n');
|
|
580
878
|
const parent = dirname(pkg.verdictPath);
|
|
581
879
|
const firstCreated = await verdictWrite('mkdir', () => mkdir(parent, { recursive: true }));
|
|
582
880
|
const tmp = `${pkg.verdictPath}.${crypto.randomUUID()}.tmp`;
|
|
@@ -618,53 +916,129 @@ export type VerdictStale = 'files' | 'flags' | null;
|
|
|
618
916
|
* were not shown.
|
|
619
917
|
*/
|
|
620
918
|
async function readVerdictSnapshot(pkg: PartsViewPackage): Promise<{
|
|
621
|
-
verdict: PartsVerdict | null;
|
|
919
|
+
verdict: PartsVerdict | null;
|
|
920
|
+
stale: VerdictStale;
|
|
921
|
+
legacy: LegacyPartsVerdictView | null;
|
|
922
|
+
legacyRecord: LegacyPartsVerdictRecord | null;
|
|
923
|
+
by: string | null;
|
|
924
|
+
savedFileSha256: string | null;
|
|
622
925
|
}> {
|
|
623
926
|
let raw: unknown;
|
|
624
|
-
const unavailable = {verdict: null, by: null, savedFileSha256: null};
|
|
927
|
+
const unavailable = { verdict: null, legacy: null, legacyRecord: null, by: null, savedFileSha256: null };
|
|
625
928
|
let bytes: Buffer;
|
|
626
|
-
try {
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
929
|
+
try {
|
|
930
|
+
bytes = await readFile(pkg.verdictPath);
|
|
931
|
+
} catch (error) {
|
|
932
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
933
|
+
return { ...unavailable, stale: null };
|
|
934
|
+
}
|
|
935
|
+
throw new Error('PARTS_VERDICT_READ: saved verdict cannot be read');
|
|
936
|
+
}
|
|
937
|
+
try {
|
|
938
|
+
raw = JSON.parse(bytes.toString('utf8'));
|
|
939
|
+
} catch {
|
|
940
|
+
throw new Error('PARTS_VERDICT_INVALID: saved verdict is not valid JSON');
|
|
941
|
+
}
|
|
942
|
+
const v = record(raw);
|
|
943
|
+
const source = record(v?.source);
|
|
944
|
+
const now = verdictSource(pkg);
|
|
945
|
+
const isLegacy = Boolean(v && (LEGACY_VERDICT_SCHEMAS as readonly string[]).includes(String(v.schema)));
|
|
946
|
+
if (!v || (v.schema !== PARTS_VERDICT_SCHEMA && !isLegacy) || typeof source?.sha256 !== 'string') {
|
|
947
|
+
throw new Error('PARTS_VERDICT_INVALID: saved verdict schema or binding is invalid');
|
|
948
|
+
}
|
|
631
949
|
let by: string | null = null;
|
|
632
|
-
if (v.schema ===
|
|
633
|
-
if (typeof v.by !== 'string' || !v.by.trim())
|
|
950
|
+
if (v.schema === 'rig-review-parts-verdict/3' || (v.schema === PARTS_VERDICT_SCHEMA && v.by !== undefined)) {
|
|
951
|
+
if (typeof v.by !== 'string' || !v.by.trim()) {
|
|
952
|
+
refuse('PARTS_VERDICT_INVALID', 'saved reviewer name must be nonblank');
|
|
953
|
+
}
|
|
634
954
|
by = v.by as string;
|
|
635
955
|
}
|
|
636
|
-
if(v.mode!==pkg.mode||source.sha256!==now.sha256)
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
956
|
+
if (v.mode !== pkg.mode || source.sha256 !== now.sha256) {
|
|
957
|
+
return { ...unavailable, stale: 'files' };
|
|
958
|
+
}
|
|
959
|
+
if (JSON.stringify(source.flags) !== JSON.stringify(now.flags)) {
|
|
960
|
+
return { ...unavailable, stale: 'flags' };
|
|
961
|
+
}
|
|
962
|
+
const date = new Date(String(v.updatedAt));
|
|
963
|
+
if (!Number.isFinite(date.getTime())) {
|
|
964
|
+
throw new Error('PARTS_VERDICT_INVALID: saved verdict date is invalid');
|
|
965
|
+
}
|
|
966
|
+
const savedFileSha256 = createHash('sha256').update(bytes).digest('hex');
|
|
967
|
+
const version = String(record(v.tool)?.version ?? '');
|
|
968
|
+
if (isLegacy) {
|
|
969
|
+
const decoded = await decodeLegacyVerdict(pkg, v, version, date, by);
|
|
970
|
+
return { ...decoded, stale: null, by, savedFileSha256 };
|
|
971
|
+
}
|
|
972
|
+
const normalized = buildVerdict(pkg, v, version, date, by ?? undefined);
|
|
973
|
+
for (const c of normalized.issues) {
|
|
974
|
+
await validateStoredPartsComment(pkg, c, false);
|
|
975
|
+
}
|
|
976
|
+
return { verdict: normalized, stale: null, legacy: null, legacyRecord: null, by, savedFileSha256 };
|
|
642
977
|
}
|
|
643
978
|
|
|
644
979
|
/** Explicitly project the shared response so retrieval metadata cannot escape through joined wrappers. */
|
|
645
|
-
export async function readVerdict(
|
|
646
|
-
|
|
647
|
-
|
|
980
|
+
export async function readVerdict(
|
|
981
|
+
pkg: PartsViewPackage,
|
|
982
|
+
): Promise<{ verdict: PartsVerdict | null; stale: VerdictStale; legacy?: LegacyPartsVerdictView }> {
|
|
983
|
+
const { verdict, stale, legacy } = await readVerdictSnapshot(pkg);
|
|
984
|
+
return legacy ? { verdict, stale, legacy } : { verdict, stale };
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
/** Validated current or legacy verdict record for CLI read and correction projection paths. */
|
|
988
|
+
export async function readPartsVerdictForCli(
|
|
989
|
+
pkg: PartsViewPackage,
|
|
990
|
+
): Promise<PartsVerdict | LegacyPartsVerdictRecord | null> {
|
|
991
|
+
const saved = await readVerdictSnapshot(pkg);
|
|
992
|
+
return saved.verdict ?? saved.legacyRecord;
|
|
648
993
|
}
|
|
649
994
|
|
|
650
995
|
/** Standalone CLI retrieval metadata and content come from one validated saved-file buffer. */
|
|
651
996
|
export async function readPartsVerdictJson(pkg: PartsViewPackage): Promise<string> {
|
|
652
997
|
const saved = await readVerdictSnapshot(pkg);
|
|
653
|
-
|
|
998
|
+
const target = saved.verdict ?? saved.legacyRecord;
|
|
999
|
+
if (!target) {
|
|
1000
|
+
return refuse('PARTS_VERDICT_UNAVAILABLE', 'save a verdict first');
|
|
1001
|
+
}
|
|
654
1002
|
await assertPartsUnchanged(pkg);
|
|
655
|
-
return
|
|
1003
|
+
return (
|
|
1004
|
+
JSON.stringify(
|
|
1005
|
+
{
|
|
1006
|
+
...JSON.parse(exportPartsVerdict(target, 'json')),
|
|
1007
|
+
by: saved.by,
|
|
1008
|
+
savedFileSha256: saved.savedFileSha256,
|
|
1009
|
+
},
|
|
1010
|
+
null,
|
|
1011
|
+
2,
|
|
1012
|
+
) + '\n'
|
|
1013
|
+
);
|
|
656
1014
|
}
|
|
657
1015
|
|
|
658
1016
|
/** What the page reads: no absolute path leaves the server but the verdict file's name. */
|
|
659
|
-
export function partsViewJson(
|
|
1017
|
+
export function partsViewJson(
|
|
1018
|
+
pkg: PartsViewPackage,
|
|
1019
|
+
saved: { verdict: PartsVerdict | null; stale: VerdictStale; legacy?: LegacyPartsVerdictView | null },
|
|
1020
|
+
) {
|
|
660
1021
|
return {
|
|
661
|
-
schema: 'rig-review-parts-view/1',
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
1022
|
+
schema: 'rig-review-parts-view/1',
|
|
1023
|
+
mode: pkg.mode,
|
|
1024
|
+
title: pkg.title,
|
|
1025
|
+
rigSize: pkg.rigSize,
|
|
1026
|
+
differenceAvailable: !!pkg.paintingInRig,
|
|
1027
|
+
painting: pkg.painting ? { sha256: pkg.painting.sha256, width: pkg.painting.width, height: pkg.painting.height } : null,
|
|
1028
|
+
sourceHash: verdictSource(pkg).sha256,
|
|
1029
|
+
items: pkg.items.map(item => ({
|
|
1030
|
+
...item,
|
|
1031
|
+
...(pkg.mode === 'parts' ? { imageHash: pkg.files.find(f => f.abs === pkg.imagePaths[item.index])?.sha256 } : {}),
|
|
1032
|
+
})),
|
|
1033
|
+
pages: pkg.pages,
|
|
1034
|
+
flags: pkg.flags,
|
|
1035
|
+
readings: pkg.readings ?? [],
|
|
1036
|
+
flagSources: pkg.flagFiles.map(f => basename(f.abs)),
|
|
1037
|
+
flagMissing: pkg.flagNotes,
|
|
1038
|
+
verdictFile: basename(pkg.verdictPath),
|
|
1039
|
+
verdict: saved.verdict,
|
|
1040
|
+
verdictStale: saved.stale,
|
|
1041
|
+
...(saved.legacy ? { legacyVerdict: saved.legacy } : {}),
|
|
668
1042
|
};
|
|
669
1043
|
}
|
|
670
1044
|
|
|
@@ -674,31 +1048,136 @@ export function servedImage(pkg: PartsViewPackage, kind: 'image' | 'page', index
|
|
|
674
1048
|
return Number.isInteger(index) && index >= 0 && index < list.length ? list[index]! : null;
|
|
675
1049
|
}
|
|
676
1050
|
|
|
677
|
-
|
|
678
1051
|
/** Both export forms preserve original text, native and rig coordinates and resolved evidence. */
|
|
679
|
-
export function exportPartsVerdict(
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
1052
|
+
export function exportPartsVerdict(
|
|
1053
|
+
verdict: PartsVerdict | LegacyPartsVerdictRecord,
|
|
1054
|
+
format: 'json' | 'issue',
|
|
1055
|
+
): string {
|
|
1056
|
+
const issues = verdict.issues.map(c => {
|
|
1057
|
+
const { png, ...crop } = c.crop;
|
|
1058
|
+
return {
|
|
1059
|
+
...c,
|
|
1060
|
+
crop: { ...crop, path: resolvedCropPath(crop.path) },
|
|
1061
|
+
nativeCrop: c.nativeCrop ? { ...c.nativeCrop, path: resolvedCropPath(c.nativeCrop.path) } : null,
|
|
1062
|
+
};
|
|
1063
|
+
});
|
|
1064
|
+
const comments =
|
|
1065
|
+
'comments' in verdict && Array.isArray(verdict.comments)
|
|
1066
|
+
? verdict.comments.map(c => {
|
|
1067
|
+
const { png, ...crop } = c.crop;
|
|
1068
|
+
return {
|
|
1069
|
+
...c,
|
|
1070
|
+
crop: { ...crop, path: resolvedCropPath(crop.path) },
|
|
1071
|
+
nativeCrop: { ...c.nativeCrop!, path: resolvedCropPath(c.nativeCrop!.path) },
|
|
1072
|
+
};
|
|
1073
|
+
})
|
|
1074
|
+
: undefined;
|
|
1075
|
+
if (format === 'json') {
|
|
1076
|
+
return (
|
|
1077
|
+
JSON.stringify(
|
|
1078
|
+
{
|
|
1079
|
+
...verdict,
|
|
1080
|
+
issues,
|
|
1081
|
+
...(comments !== undefined ? { comments } : {}),
|
|
1082
|
+
},
|
|
1083
|
+
null,
|
|
1084
|
+
2,
|
|
1085
|
+
) + '\n'
|
|
1086
|
+
);
|
|
1087
|
+
}
|
|
1088
|
+
const commentedParts = new Set(issues.flatMap(i => i.parts.map(p => p.name)));
|
|
1089
|
+
const bareFlaggedLines =
|
|
1090
|
+
'parts' in verdict && verdict.parts
|
|
1091
|
+
? Object.entries(verdict.parts).flatMap(([name, p]) => {
|
|
1092
|
+
const pf = p.outcome === 'flagged';
|
|
1093
|
+
const mf = p.mesh?.outcome === 'flagged';
|
|
1094
|
+
if ((!pf && !mf) || commentedParts.has(name)) {
|
|
1095
|
+
return [];
|
|
1096
|
+
}
|
|
1097
|
+
const note = [pf ? p.note.trim() : '', mf ? p.mesh!.note.trim() : ''].filter(Boolean).join(' / ');
|
|
1098
|
+
return [`\n## ${name}`, note];
|
|
1099
|
+
})
|
|
1100
|
+
: [];
|
|
1101
|
+
return (
|
|
1102
|
+
[
|
|
1103
|
+
`# Parts review`,
|
|
1104
|
+
`Source: ${verdict.source.sha256}`,
|
|
1105
|
+
`Status: ${verdict.status}`,
|
|
1106
|
+
...bareFlaggedLines,
|
|
1107
|
+
...issues.flatMap(c => [
|
|
1108
|
+
`\n## ${c.parts.map(p => p.name).join(', ')}`,
|
|
1109
|
+
`\n### ${c.summary}`,
|
|
1110
|
+
`Part: ${c.parts.map(p => `${p.name} · image SHA-256: ${p.imageHash}`).join(', ')}`,
|
|
1111
|
+
`Image region: ${JSON.stringify(c.imageRegion)}`,
|
|
1112
|
+
`Rig region: ${JSON.stringify(c.rigRegion)}`,
|
|
1113
|
+
`Viewport crop: ${c.crop.path} · SHA-256 ${c.crop.sha256}`,
|
|
1114
|
+
...(c.nativeCrop ? [`Native crop: ${c.nativeCrop.path} · SHA-256 ${c.nativeCrop.sha256}`] : []),
|
|
1115
|
+
`View: ${JSON.stringify(c.view)}`,
|
|
1116
|
+
]),
|
|
1117
|
+
].join('\n') + '\n'
|
|
1118
|
+
);
|
|
683
1119
|
}
|
|
684
1120
|
|
|
685
1121
|
/** Numeric comparison is decoded from bound original PNG bytes on the server, never browser pixels. */
|
|
686
|
-
export async function partsDifference(pkg:PartsViewPackage,index:number,threshold:number,footprints:boolean){
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
1122
|
+
export async function partsDifference(pkg: PartsViewPackage, index: number, threshold: number, footprints: boolean) {
|
|
1123
|
+
await assertPartsUnchanged(pkg);
|
|
1124
|
+
if (!pkg.paintingInRig) {
|
|
1125
|
+
refuse('PARTS_DIFFERENCE_UNAVAILABLE', 'declared painting_in_rig required; legacy records remain readable');
|
|
1126
|
+
}
|
|
1127
|
+
const item = pkg.items[index];
|
|
1128
|
+
if (pkg.mode !== 'parts' || !Number.isInteger(index) || !item) {
|
|
1129
|
+
refuse('PARTS_DIFFERENCE_UNAVAILABLE', 'known original part required');
|
|
1130
|
+
}
|
|
1131
|
+
const decode = async (path: string) => {
|
|
1132
|
+
const bytes = await readFile(path);
|
|
1133
|
+
if (bytes[24] !== 8 || bytes[25] !== 6) {
|
|
1134
|
+
refuse('PARTS_DIFFERENCE_PNG', 'exact RGBA8 operands required');
|
|
1135
|
+
}
|
|
1136
|
+
return PNG.sync.read(bytes);
|
|
1137
|
+
};
|
|
1138
|
+
const painting = await decode(pkg.paintingInRig!.abs);
|
|
1139
|
+
const part = await decode(pkg.imagePaths[index]!);
|
|
1140
|
+
const fronts = footprints
|
|
1141
|
+
? await Promise.all(
|
|
1142
|
+
pkg.items
|
|
1143
|
+
.filter(
|
|
1144
|
+
f =>
|
|
1145
|
+
f.index > index &&
|
|
1146
|
+
f.x < item!.x + item!.w &&
|
|
1147
|
+
f.x + f.w > item!.x &&
|
|
1148
|
+
f.y < item!.y + item!.h &&
|
|
1149
|
+
f.y + f.h > item!.y,
|
|
1150
|
+
)
|
|
1151
|
+
.map(async box => ({ box, raster: await decode(pkg.imagePaths[box.index]!) })),
|
|
1152
|
+
)
|
|
1153
|
+
: [];
|
|
1154
|
+
const result = differencePixels(part, item!, painting, fronts, threshold, footprints);
|
|
1155
|
+
await assertPartsUnchanged(pkg);
|
|
1156
|
+
return result;
|
|
695
1157
|
}
|
|
696
1158
|
|
|
697
1159
|
/** Selected JSON keeps the canonical verdict and names the user's handoff selection. */
|
|
698
|
-
export function exportPartsCorrection(
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
1160
|
+
export function exportPartsCorrection(
|
|
1161
|
+
verdict: PartsVerdict | LegacyPartsVerdictRecord,
|
|
1162
|
+
correction: ReturnType<typeof import('./correction-handoff.ts').projectPartsDescription>,
|
|
1163
|
+
): string {
|
|
1164
|
+
return (
|
|
1165
|
+
JSON.stringify(
|
|
1166
|
+
{
|
|
1167
|
+
...verdict,
|
|
1168
|
+
correction: {
|
|
1169
|
+
path: 'described',
|
|
1170
|
+
stage: correction.stage,
|
|
1171
|
+
itemId: correction.itemId,
|
|
1172
|
+
part: correction.part,
|
|
1173
|
+
verbatim: correction.verbatim,
|
|
1174
|
+
selectorArgs: correction.on,
|
|
1175
|
+
outcome: null,
|
|
1176
|
+
...('imageHash' in correction ? { imageHash: correction.imageHash } : {}),
|
|
1177
|
+
},
|
|
1178
|
+
},
|
|
1179
|
+
null,
|
|
1180
|
+
2,
|
|
1181
|
+
) + '\n'
|
|
1182
|
+
);
|
|
704
1183
|
}
|