rig-review 2.7.1 → 3.7.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.
@@ -3,6 +3,12 @@ import { closeSync, constants, fstatSync, lstatSync, openSync, readSync, realpat
3
3
  import { extname, join, resolve } from 'node:path';
4
4
  import { isDeepStrictEqual as equal } from 'node:util';
5
5
  import {coveringChange, sceneAt} from './interview-scene.ts';
6
+ import type {
7
+ JobInterviewDiagnostic,
8
+ JobInterviewDiagnostics,
9
+ JobInterviewJson,
10
+ JobInterviewRecord,
11
+ } from './job-contract.ts';
6
12
 
7
13
  type RecordData = Record<string, unknown>;
8
14
  export interface SetRef {path: string; sha256: string}
@@ -234,7 +240,37 @@ function inspectContext(root: string, project: RecordData, raw: RecordData, set:
234
240
  return {images, hasParts: ctx.kind !== 'inputs'};
235
241
  }
236
242
 
243
+ function cloneJson(value: unknown): JobInterviewJson {
244
+ if (value === null || typeof value === 'boolean' || typeof value === 'string') return value;
245
+ if (typeof value === 'number') return Number.isFinite(value) ? value : null;
246
+ if (Array.isArray(value)) return value.map(cloneJson);
247
+ if (typeof value === 'object') {
248
+ return Object.fromEntries(
249
+ Object.entries(value as RecordData)
250
+ .filter(([, entry]) => entry !== undefined)
251
+ .map(([key, entry]) => [key, cloneJson(entry)]),
252
+ );
253
+ }
254
+ return null;
255
+ }
256
+
257
+ function cloneJsonObject(value: RecordData): { readonly [key: string]: JobInterviewJson } {
258
+ return cloneJson(value) as { readonly [key: string]: JobInterviewJson };
259
+ }
260
+
261
+ function observeRetainedSet(root: string, ref: SetRef): SetRef | null {
262
+ try {
263
+ if (['derived', 'outputs', 'scratch', 'node_modules', '.git'].includes(ref.path.split('/')[0]!)) return null;
264
+ const bytes = readRegular(checkedPath(root, ref.path), 64 * 1024 * 1024);
265
+ return { path: ref.path, sha256: digest(bytes) };
266
+ } catch {
267
+ return null;
268
+ }
269
+ }
270
+
237
271
  export function readProject(root: string, now = Date.now()) {
272
+ if (!Number.isFinite(now)) return invalid('Evaluation instant must be finite');
273
+ const evaluatedAt = new Date(now).toISOString();
238
274
  try {
239
275
  root = realpathSync(resolve(root));
240
276
  if (!lstatSync(root).isDirectory()) return invalid('Project root must be a directory');
@@ -246,10 +282,13 @@ export function readProject(root: string, now = Date.now()) {
246
282
  project = jsonObject(bytes);
247
283
  } catch {return invalid('project.json must be a readable regular JSON file');}
248
284
  if (project.schema !== 'rig-create/project@0') return invalid('Unsupported create project');
285
+ const hash = digest(bytes);
249
286
  const interview = object(project.interview);
250
287
  const questions: InterviewQuestion[] = [];
251
288
  const historical: HistoricalInterview[] = [];
252
289
  const diagnostics: InterviewDiagnostic[] = [];
290
+ const interviewRecords: JobInterviewRecord[] = [];
291
+ const interviewDiagnostics: JobInterviewDiagnostic[] = [];
253
292
  let sceneAnswers = 0;
254
293
  const seen = new Map<string, string>();
255
294
  for (const [items, answerKey, allowed, collection] of [
@@ -290,11 +329,66 @@ export function readProject(root: string, now = Date.now()) {
290
329
  } catch (error) {
291
330
  if (editable) throw error;
292
331
  const answer = entry.answer!;
332
+ const observedSet = observeRetainedSet(root, ref);
333
+ const answerAt = typeof answer.at === 'string' ? answer.at : null;
293
334
  historical.push({question: String(item.id), verbatim: typeof answer.verbatim === 'string' ? answer.verbatim : '',
294
- by: typeof answer.by === 'string' ? answer.by : undefined, at: typeof answer.at === 'string' ? answer.at : undefined,
335
+ by: typeof answer.by === 'string' ? answer.by : undefined, at: answerAt ?? undefined,
295
336
  ref, record: entry.record, answer});
296
337
  diagnostics.push({code: 'INTERVIEW_OPTIONS_STALE', question: String(item.id), record: entry.record, ref,
297
- ...(typeof answer.at === 'string' ? {at: answer.at} : {})});
338
+ ...(answerAt !== null ? {at: answerAt} : {})});
339
+ interviewRecords.push({
340
+ questionId: String(item.id),
341
+ record: entry.record,
342
+ setId: null,
343
+ ref,
344
+ observedSet,
345
+ timeKind: 'answer',
346
+ at: answerAt,
347
+ original: cloneJsonObject(answer),
348
+ });
349
+ interviewDiagnostics.push({
350
+ record: entry.record,
351
+ at: answerAt,
352
+ code: 'INTERVIEW_OPTIONS_STALE',
353
+ severity: 'error',
354
+ });
355
+ const rawDecision = answer.agent_decision;
356
+ if (rawDecision !== null && typeof rawDecision === 'object' && !Array.isArray(rawDecision)) {
357
+ const decision = rawDecision as RecordData;
358
+ const decisionRecord = entry.record + '.agent_decision';
359
+ if (['at', 'value', 'fragments'].some(key => Object.hasOwn(decision, key))) {
360
+ const decisionAt = typeof decision.at === 'string' ? decision.at : null;
361
+ interviewRecords.push({
362
+ questionId: String(item.id),
363
+ record: decisionRecord,
364
+ setId: null,
365
+ ref,
366
+ observedSet,
367
+ timeKind: 'decision',
368
+ at: decisionAt,
369
+ original: cloneJsonObject(decision),
370
+ });
371
+ diagnostics.push({code: 'INTERVIEW_OPTIONS_STALE', question: String(item.id), record: decisionRecord, ref,
372
+ ...(decisionAt !== null ? {at: decisionAt} : {})});
373
+ interviewDiagnostics.push({
374
+ record: decisionRecord,
375
+ at: decisionAt,
376
+ code: 'INTERVIEW_OPTIONS_STALE',
377
+ severity: 'error',
378
+ });
379
+ } else {
380
+ interviewRecords.push({
381
+ questionId: String(item.id),
382
+ record: decisionRecord,
383
+ setId: null,
384
+ ref,
385
+ observedSet,
386
+ timeKind: 'decision',
387
+ at: null,
388
+ original: cloneJsonObject(decision),
389
+ });
390
+ }
391
+ }
298
392
  continue;
299
393
  }
300
394
  if (editable && (set.stage === 'interview' || set.stage === 'rigging-interview') &&
@@ -304,27 +398,96 @@ export function readProject(root: string, now = Date.now()) {
304
398
  if (editable && approval(project, set.stage === 'interview' ? 'plan-approved' : set.stage, now)) return invalid('Question approval is still live');
305
399
  const sceneKind = object(raw.context).kind === 'scene';
306
400
  if (!editable && sceneKind) sceneAnswers++;
307
- const evaluate = (at: number, record: string) => {
401
+ const observedSet: SetRef = { path: ref.path, sha256: ref.sha256 };
402
+ const evaluate = (at: number, record: string, recordAt: string | null) => {
308
403
  try {
309
404
  const {legacy, ...context} = inspectContext(root, project, raw, set, at, editable);
310
- if (legacy) diagnostics.push({code: 'INTERVIEW_OPTIONS_SCENE_LEGACY', question: set.question, record, ref, at: new Date(at).toISOString()});
405
+ if (legacy) {
406
+ diagnostics.push({code: 'INTERVIEW_OPTIONS_SCENE_LEGACY', question: set.question, record, ref,
407
+ ...(recordAt !== null ? {at: recordAt} : {})});
408
+ interviewDiagnostics.push({
409
+ record,
410
+ at: recordAt,
411
+ code: 'INTERVIEW_OPTIONS_SCENE_LEGACY',
412
+ severity: 'warning',
413
+ });
414
+ }
311
415
  return context;
312
416
  } catch (error) {
313
417
  if (editable || !(error instanceof Error) || !error.message.startsWith('INTERVIEW_OPTIONS_STALE:')) throw error;
314
418
  diagnostics.push({code: 'INTERVIEW_OPTIONS_STALE', question: set.question, record, ref,
315
- ...(Number.isFinite(at) ? {at: new Date(at).toISOString()} : {})});
419
+ ...(recordAt !== null ? {at: recordAt} : {})});
420
+ interviewDiagnostics.push({
421
+ record,
422
+ at: recordAt,
423
+ code: 'INTERVIEW_OPTIONS_STALE',
424
+ severity: 'error',
425
+ });
316
426
  return {images: [], hasParts: false};
317
427
  }
318
428
  };
319
- const context = evaluate(editable ? now : date(entry.answer?.at), entry.record);
320
- const decision = object(entry.answer?.agent_decision);
321
- if (!editable && ['at', 'value', 'fragments'].some(key => Object.hasOwn(decision, key))) {
322
- evaluate(date(decision.at), entry.record + '.agent_decision');
429
+ const recordAt = editable ? evaluatedAt : typeof entry.answer?.at === 'string' ? entry.answer.at : null;
430
+ interviewRecords.push({
431
+ questionId: String(item.id),
432
+ record: entry.record,
433
+ setId: set.id,
434
+ ref,
435
+ observedSet,
436
+ timeKind: editable ? 'inspection' : 'answer',
437
+ at: recordAt,
438
+ original: editable ? null : cloneJsonObject(entry.answer!),
439
+ });
440
+ const context = evaluate(editable ? now : date(entry.answer?.at), entry.record, recordAt);
441
+ const rawDecision = entry.answer?.agent_decision;
442
+ if (!editable && rawDecision !== null && typeof rawDecision === 'object' && !Array.isArray(rawDecision)) {
443
+ const decision = rawDecision as RecordData;
444
+ const decisionRecord = entry.record + '.agent_decision';
445
+ if (['at', 'value', 'fragments'].some(key => Object.hasOwn(decision, key))) {
446
+ const decisionAt = typeof decision.at === 'string' ? decision.at : null;
447
+ interviewRecords.push({
448
+ questionId: String(item.id),
449
+ record: decisionRecord,
450
+ setId: set.id,
451
+ ref,
452
+ observedSet,
453
+ timeKind: 'decision',
454
+ at: decisionAt,
455
+ original: cloneJsonObject(decision),
456
+ });
457
+ evaluate(date(decision.at), decisionRecord, decisionAt);
458
+ } else {
459
+ interviewRecords.push({
460
+ questionId: String(item.id),
461
+ record: decisionRecord,
462
+ setId: set.id,
463
+ ref,
464
+ observedSet,
465
+ timeKind: 'decision',
466
+ at: null,
467
+ original: cloneJsonObject(decision),
468
+ });
469
+ }
323
470
  }
324
471
  questions.push({ref, set, editable, answer: entry.answer, ...context});
325
472
  seen.set(ref.sha256, ref.path);
326
473
  }
327
474
  }
328
475
  }
329
- return {root, hash: digest(bytes), project, questions, historical, diagnostics, sceneAnswers};
476
+ const interviewDiagnosticsEnvelope: JobInterviewDiagnostics = {
477
+ schema: 'rig-review/job-interview@1',
478
+ project: { path: 'project.json', sha256: hash },
479
+ evaluatedAt,
480
+ records: interviewRecords,
481
+ diagnostics: interviewDiagnostics,
482
+ };
483
+ return {
484
+ root,
485
+ hash,
486
+ project,
487
+ questions,
488
+ historical,
489
+ diagnostics,
490
+ sceneAnswers,
491
+ interview: interviewDiagnosticsEnvelope,
492
+ };
330
493
  }
package/src/job-act.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { randomUUID } from 'node:crypto';
2
2
  import {
3
- JOB_ACT_SCHEMA, JOB_RECORD_ACT_SCHEMA, JOB_RIG_REVIEW_ACT_SCHEMA, jobStillByteLimit, JOB_ENABLED_ACT_KINDS, JOB_PLAN_BOUND_SIGNOFF_STAGES, JOB_RESERVED_ACT_KINDS,
4
- type ActV3, type JobAct, type JobActValues, type JobPendingAsk, type JobReviewerBinding,
3
+ JOB_ACT_SCHEMA, JOB_API_SCHEMA, JOB_API_V3_SCHEMA, JOB_RECORD_ACT_KINDS, JOB_RECORD_ACT_SCHEMA, JOB_RIG_REVIEW_ACT_SCHEMA, jobStillByteLimit, JOB_ENABLED_ACT_KINDS, JOB_PLAN_BOUND_SIGNOFF_STAGES, JOB_RESERVED_ACT_KINDS,
4
+ type ActV3, type JobAct, type JobActValues, type JobApiSchema, type JobPendingAsk, type JobPublicationMode, type JobReviewerBinding,
5
5
  type JobSourceBinding, type JobSubject, type JobUISubmission, type JobRigCapture, type JobRigStill,
6
- type PendingAskV3, type RecordAct, type RecordSource, type RecordSubject, type SourceV3,
6
+ type PendingAskV3, type RecordAct, type RecordSource, type RecordSubject, type SourceV3, type UISubmissionV3,
7
7
  } from './job-contract.ts';
8
8
  import {
9
9
  isRecordSource, jobHash, jobId, jobInteger, jobObject,
@@ -75,6 +75,12 @@ export function bindJobRecordAsk(subject: unknown, retained: JobRetainedRecordSo
75
75
  }
76
76
  return source;
77
77
  }
78
+ case 'reopen': {
79
+ if (source.subject !== 'plan') {
80
+ throw new JobError('JOB_ASK_INVALID');
81
+ }
82
+ return source;
83
+ }
78
84
  }
79
85
  }
80
86
 
@@ -121,24 +127,45 @@ export function bindJobAsk(subject: JobSubject | RecordSubject, retained: JobRet
121
127
  if (!declared) throw new JobError('JOB_ASK_INVALID');
122
128
  if ((JOB_PLAN_BOUND_SIGNOFF_STAGES as readonly string[]).includes(subject.stage)) requirePlan(source);
123
129
  return source;
130
+ case 'reopen':
131
+ throw new JobError('JOB_ASK_INVALID');
124
132
  }
125
133
  }
126
134
 
127
- export function parseUISubmission(value: unknown): JobUISubmission {
135
+ export function parseUISubmission(
136
+ value: unknown,
137
+ mode?: typeof JOB_API_SCHEMA | 'package-only',
138
+ ): JobUISubmission;
139
+ export function parseUISubmission(
140
+ value: unknown,
141
+ mode: typeof JOB_API_V3_SCHEMA | 'record-and-package',
142
+ ): UISubmissionV3;
143
+ export function parseUISubmission(
144
+ value: unknown,
145
+ mode?: JobApiSchema | JobPublicationMode,
146
+ ): UISubmissionV3;
147
+ export function parseUISubmission(
148
+ value: unknown,
149
+ mode: JobApiSchema | JobPublicationMode = JOB_API_SCHEMA,
150
+ ): JobUISubmission | UISubmissionV3 {
128
151
  const item = jobObject(value, ['publicationId', 'askId', 'pageId', 'displayedNonce', 'expectedGeneration', 'submissionId', 'kind', 'value', 'capture']);
129
152
  if ((JOB_RESERVED_ACT_KINDS as readonly unknown[]).includes(item.kind)) throw new JobError('JOB_ACT_KIND_DISABLED');
130
153
  jobId(item.publicationId); jobId(item.askId); jobId(item.pageId); jobId(item.displayedNonce); jobId(item.submissionId);
131
154
  jobInteger(item.expectedGeneration);
132
- if (!(JOB_ENABLED_ACT_KINDS as readonly string[]).includes(String(item.kind))) {
155
+ const recordEnabled = mode === JOB_API_V3_SCHEMA || mode === 'record-and-package';
156
+ const kind = String(item.kind);
157
+ const allowed = (JOB_ENABLED_ACT_KINDS as readonly string[]).includes(kind) ||
158
+ (recordEnabled && (JOB_RECORD_ACT_KINDS as readonly string[]).includes(kind));
159
+ if (!allowed) {
133
160
  throw new JobError('JOB_ACT_INVALID');
134
161
  }
135
162
  if (item.capture !== undefined && (item.kind !== 'review-verdict' ||
136
163
  (item.value as {stage?: unknown} | null)?.stage !== 'rig-review')) throw new JobError('JOB_REQUEST_INVALID');
137
- return item as unknown as JobUISubmission;
164
+ return item as unknown as UISubmissionV3;
138
165
  }
139
166
 
140
167
  /** The browser supplies pixels and draw provenance, never a trusted hash or path. */
141
- export function prepareJobStill(input: JobUISubmission, ask: JobPendingAsk, retained: JobRetainedSource): {
168
+ export function prepareJobStill(input: UISubmissionV3, ask: JobPendingAsk, retained: JobRetainedSource): {
142
169
  readonly still: JobRigStill;
143
170
  readonly bytes: Buffer;
144
171
  readonly binding: JobRigCapture['binding'];
@@ -201,7 +228,7 @@ function actIdField(value: unknown): string {
201
228
 
202
229
  export function prepareJobRecordAct(options: {
203
230
  readonly jobId: string;
204
- readonly submission: JobUISubmission;
231
+ readonly submission: UISubmissionV3;
205
232
  readonly ask: PendingAskV3;
206
233
  readonly retained: JobRetainedRecordSource;
207
234
  readonly reviewer: JobReviewerBinding;
@@ -379,6 +406,26 @@ export function prepareJobRecordAct(options: {
379
406
  },
380
407
  });
381
408
  }
409
+ case 'reopen': {
410
+ if (boundSource.subject !== 'plan') throw new JobError('JOB_ACT_SOURCE_STALE');
411
+ const value = actObject(submission.value, ['stage', 'words']);
412
+ if (
413
+ !('stage' in value) ||
414
+ !('words' in value) ||
415
+ value.stage !== askSubject.stage
416
+ ) {
417
+ throw new JobError('JOB_ACT_INVALID');
418
+ }
419
+ return parseRecordAct({
420
+ ...envelope,
421
+ kind: 'reopen',
422
+ source: boundSource,
423
+ value: {
424
+ stage: askSubject.stage,
425
+ words: words(value.words),
426
+ },
427
+ });
428
+ }
382
429
  }
383
430
  }
384
431
 
@@ -395,7 +442,7 @@ export function finalizeJobAct(options: {
395
442
  }): JobAct;
396
443
  export function finalizeJobAct(options: {
397
444
  readonly jobId: string;
398
- readonly submission: JobUISubmission;
445
+ readonly submission: UISubmissionV3;
399
446
  readonly ask: PendingAskV3;
400
447
  readonly retained: JobRetainedRecordSource;
401
448
  readonly reviewer: JobReviewerBinding;
@@ -405,7 +452,7 @@ export function finalizeJobAct(options: {
405
452
  }): RecordAct;
406
453
  export function finalizeJobAct(options: {
407
454
  readonly jobId: string;
408
- readonly submission: JobUISubmission;
455
+ readonly submission: UISubmissionV3;
409
456
  readonly ask: JobPendingAsk | PendingAskV3;
410
457
  readonly retained: JobRetainedSourceV3;
411
458
  readonly reviewer: JobReviewerBinding;
@@ -415,7 +462,7 @@ export function finalizeJobAct(options: {
415
462
  }): ActV3;
416
463
  export function finalizeJobAct(options: {
417
464
  readonly jobId: string;
418
- readonly submission: JobUISubmission;
465
+ readonly submission: UISubmissionV3;
419
466
  readonly ask: JobPendingAsk | PendingAskV3;
420
467
  readonly retained: JobRetainedSourceV3;
421
468
  readonly reviewer: JobReviewerBinding;
@@ -435,6 +482,7 @@ export function finalizeJobAct(options: {
435
482
  at: options.at,
436
483
  });
437
484
  }
485
+ if ((options.submission.kind as string) === 'reopen') throw new JobError('JOB_ACT_INVALID');
438
486
  const { submission, ask, retained, reviewer } = options as {
439
487
  readonly jobId: string;
440
488
  readonly submission: JobUISubmission;