rig-review 2.5.0 → 2.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.
- package/docs/PARTS_VIEW.md +82 -3
- package/docs/REVIEW_PACKAGE.md +50 -12
- package/docs/STABILITY.md +232 -15
- package/docs/WORKFLOWS.md +37 -1
- package/package.json +1 -1
- package/src/cli.ts +52 -7
- package/src/job-act.ts +316 -8
- package/src/job-api.ts +821 -8
- package/src/job-cli.ts +219 -52
- package/src/job-contract.ts +299 -0
- package/src/job-front.ts +763 -23
- package/src/job-host.ts +9 -1
- package/src/job-hub.ts +11 -6
- package/src/job-session.ts +68 -9
- package/src/job-source.ts +787 -7
- package/src/job-store.ts +114 -8
- package/src/job-worker.ts +5 -1
- package/src/parts-server.ts +46 -7
- package/src/parts-view.ts +76 -14
- package/web/catalog-en.ts +9 -0
- package/web/catalog-ko.ts +9 -0
- package/web/job-rig-capture.ts +53 -11
- package/web/job-session.ts +196 -7
package/src/job-api.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import { createHash, timingSafeEqual } from 'node:crypto';
|
|
2
2
|
import {
|
|
3
|
-
JOB_API_SCHEMA,
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
JOB_ACT_SCHEMA, JOB_API_SCHEMA, JOB_API_V3_SCHEMA, JOB_ENABLED_ACT_KINDS, JOB_PARTS_ENUMERATOR_RANGE,
|
|
4
|
+
JOB_PLAN_BOUND_SIGNOFF_STAGES, JOB_RECORD_ACT_SCHEMA, JOB_RECORD_INSTANCE_SCHEMA, JOB_RECORD_SOURCE_SCHEMA,
|
|
5
|
+
JOB_RESERVED_ACT_KINDS,
|
|
6
|
+
type AgentRequestV3, type FetchActRequestV3, type JobAgentRequest, type JobApiSchema,
|
|
7
|
+
type JobDisplayAcknowledgment, type JobErrorCode, type JobFetchActRequest, type JobFileIdentity,
|
|
8
|
+
type JobHostPorts, type JobHumanAuthority, type JobPlanIdentity, type JobPublicationMode,
|
|
9
|
+
type JobPublishRequest, type JobResult, type JobSelector, type JobSha256, type JobSubject,
|
|
10
|
+
type OptionContext, type OptionSource, type PlanContext, type PlanSource, type ProjectRecord,
|
|
11
|
+
type PublishRequestV3, type PublishResultV3, type RecordAct, type RecordFreeze,
|
|
12
|
+
type RecordOptionResult, type RecordPartsIdentity, type RecordPicksSubject, type RecordPlanResult, type RecordPlanSubject,
|
|
13
|
+
type RecordResult, type RecordSignoffStage, type RecordSource, type RecordSubject,
|
|
6
14
|
jobCaptureBodyLimit, jobRawActMediaType,
|
|
7
15
|
} from './job-contract.ts';
|
|
8
16
|
import { JobError } from './job-store.ts';
|
|
@@ -136,18 +144,170 @@ export function jobPartsEnumeratorVersion(value: unknown): string {
|
|
|
136
144
|
return value;
|
|
137
145
|
}
|
|
138
146
|
|
|
139
|
-
export function
|
|
140
|
-
if (
|
|
147
|
+
export function jobApiSchemaForMode(mode: JobPublicationMode = 'package-only'): JobApiSchema {
|
|
148
|
+
if (mode === 'package-only') return JOB_API_SCHEMA;
|
|
149
|
+
if (mode === 'record-and-package') return JOB_API_V3_SCHEMA;
|
|
150
|
+
throw new JobError('JOB_REQUEST_INVALID');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function assertJobApiSchema(
|
|
154
|
+
value: unknown,
|
|
155
|
+
expected: JobApiSchema | JobPublicationMode = JOB_API_SCHEMA,
|
|
156
|
+
): void {
|
|
157
|
+
const schema = expected === 'package-only' || expected === 'record-and-package'
|
|
158
|
+
? jobApiSchemaForMode(expected)
|
|
159
|
+
: expected;
|
|
160
|
+
if (value !== schema) {
|
|
141
161
|
throw new JobError('JOB_REQUEST_INVALID', 'JOB_API_SCHEMA_MISMATCH');
|
|
142
162
|
}
|
|
143
163
|
}
|
|
144
164
|
|
|
145
|
-
export function
|
|
165
|
+
export function parseProjectRecord(
|
|
166
|
+
value: unknown,
|
|
167
|
+
errorCode: JobErrorCode = 'JOB_FREEZE_REQUIRED',
|
|
168
|
+
): ProjectRecord {
|
|
169
|
+
if (value === null || value === undefined) throw new JobError(errorCode);
|
|
170
|
+
const record = jobObject(value, ['path', 'sha256']);
|
|
171
|
+
const path = jobPath(record.path);
|
|
172
|
+
const sha256 = jobSha(record.sha256);
|
|
173
|
+
if (path !== 'project.json') throw new JobError(errorCode);
|
|
174
|
+
return { path: 'project.json', sha256 };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function parseRecordFreeze(value: unknown): RecordFreeze {
|
|
178
|
+
if (value === null || value === undefined) throw new JobError('JOB_FREEZE_REQUIRED');
|
|
179
|
+
const freeze = jobObject(value, ['kind', 'holdId', 'record']);
|
|
180
|
+
if (freeze.kind !== 'producer-held') throw new JobError('JOB_FREEZE_REQUIRED');
|
|
181
|
+
const holdId = jobId(freeze.holdId);
|
|
182
|
+
const record = parseProjectRecord(freeze.record, 'JOB_FREEZE_REQUIRED');
|
|
183
|
+
return { kind: 'producer-held', holdId, record };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function parsePublishResultV3(value: unknown): PublishResultV3 {
|
|
187
|
+
const item = jobObject(value, ['kind', 'path', 'selector', 'questionId', 'setId']);
|
|
188
|
+
switch (item.kind) {
|
|
189
|
+
case 'package':
|
|
190
|
+
jobObject(item, ['kind', 'path', 'selector']);
|
|
191
|
+
if (!('selector' in item) || item.selector === undefined) throw new JobError('JOB_REQUEST_INVALID');
|
|
192
|
+
return { kind: 'package', path: jobPath(item.path), selector: parseJobSelector(item.selector) };
|
|
193
|
+
case 'option-set':
|
|
194
|
+
jobObject(item, ['kind', 'questionId', 'setId']);
|
|
195
|
+
return { kind: 'option-set', questionId: jobId(item.questionId), setId: jobId(item.setId) };
|
|
196
|
+
case 'plan':
|
|
197
|
+
jobObject(item, ['kind']);
|
|
198
|
+
return { kind: 'plan' };
|
|
199
|
+
}
|
|
200
|
+
throw new JobError('JOB_REQUEST_INVALID');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function parseRecordResult(value: unknown): RecordResult {
|
|
204
|
+
const result = parsePublishResultV3(value);
|
|
205
|
+
if (result.kind === 'package') throw new JobError('JOB_REQUEST_INVALID');
|
|
206
|
+
return result;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function stripPackageResultKind(
|
|
210
|
+
result: Extract<PublishResultV3, { readonly kind: 'package' }>,
|
|
211
|
+
): JobPublishRequest['result'] {
|
|
212
|
+
const parsed = parsePublishResultV3(result);
|
|
213
|
+
if (parsed.kind !== 'package') throw new JobError('JOB_REQUEST_INVALID');
|
|
214
|
+
return { path: parsed.path, selector: parsed.selector };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function parseRecordSubject(value: unknown): RecordSubject {
|
|
218
|
+
const item = jobObject(value, ['kind', 'questionId', 'setId', 'checkpointId', 'draftId', 'stage']);
|
|
219
|
+
if ((JOB_RESERVED_ACT_KINDS as readonly unknown[]).includes(item.kind)) {
|
|
220
|
+
throw new JobError('JOB_ACT_KIND_DISABLED');
|
|
221
|
+
}
|
|
222
|
+
if (item.kind !== 'interview-picks' && item.kind !== 'checkpoint' && item.kind !== 'signoff') {
|
|
223
|
+
throw new JobError('JOB_ASK_INVALID');
|
|
224
|
+
}
|
|
225
|
+
switch (item.kind) {
|
|
226
|
+
case 'interview-picks':
|
|
227
|
+
jobObject(item, ['kind', 'questionId', 'setId']);
|
|
228
|
+
return { kind: 'interview-picks', questionId: jobId(item.questionId), setId: jobId(item.setId) };
|
|
229
|
+
case 'checkpoint':
|
|
230
|
+
jobObject(item, ['kind', 'checkpointId']);
|
|
231
|
+
return { kind: 'checkpoint', checkpointId: jobId(item.checkpointId) };
|
|
232
|
+
case 'signoff': {
|
|
233
|
+
jobObject(item, ['kind', 'stage']);
|
|
234
|
+
const stage = jobId(item.stage);
|
|
235
|
+
if (!(JOB_PLAN_BOUND_SIGNOFF_STAGES as readonly string[]).includes(stage)) {
|
|
236
|
+
throw new JobError('JOB_ASK_INVALID');
|
|
237
|
+
}
|
|
238
|
+
return { kind: 'signoff', stage: stage as RecordSignoffStage };
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function parseJobRequest(
|
|
244
|
+
value: unknown,
|
|
245
|
+
mode?: typeof JOB_API_SCHEMA | 'package-only',
|
|
246
|
+
): JobAgentRequest | JobFetchActRequest;
|
|
247
|
+
export function parseJobRequest(
|
|
248
|
+
value: unknown,
|
|
249
|
+
mode: typeof JOB_API_V3_SCHEMA | 'record-and-package',
|
|
250
|
+
): AgentRequestV3 | FetchActRequestV3;
|
|
251
|
+
export function parseJobRequest(
|
|
252
|
+
value: unknown,
|
|
253
|
+
mode: JobApiSchema | JobPublicationMode = JOB_API_SCHEMA,
|
|
254
|
+
): JobAgentRequest | JobFetchActRequest | AgentRequestV3 | FetchActRequestV3 {
|
|
146
255
|
const item = jobObject(value, [
|
|
147
256
|
'schema', 'jobId', 'requestId', 'verb', 'expectedGeneration', 'idempotencyKey',
|
|
148
257
|
'result', 'freeze', 'publicationId', 'subject', 'after', 'waitMs', 'reason', 'actId',
|
|
149
258
|
]);
|
|
150
|
-
|
|
259
|
+
const targetSchema = mode === 'package-only' || mode === 'record-and-package'
|
|
260
|
+
? jobApiSchemaForMode(mode)
|
|
261
|
+
: mode;
|
|
262
|
+
assertJobApiSchema(item.schema, targetSchema);
|
|
263
|
+
if (targetSchema === JOB_API_V3_SCHEMA) {
|
|
264
|
+
const base = { schema: JOB_API_V3_SCHEMA, jobId: jobId(item.jobId), requestId: jobId(item.requestId) };
|
|
265
|
+
if (item.verb === 'next') {
|
|
266
|
+
jobObject(item, ['schema', 'jobId', 'requestId', 'verb', 'after', 'waitMs']);
|
|
267
|
+
return { ...base, verb: 'next', after: item.after === null ? null : jobId(item.after), waitMs: jobInteger(item.waitMs, jobWaitLimitMs) };
|
|
268
|
+
}
|
|
269
|
+
if (item.verb === 'fetch-act') {
|
|
270
|
+
jobObject(item, ['schema', 'jobId', 'requestId', 'verb', 'actId']);
|
|
271
|
+
return { ...base, verb: 'fetch-act', actId: jobId(item.actId) };
|
|
272
|
+
}
|
|
273
|
+
const mutation = { ...base, expectedGeneration: jobInteger(item.expectedGeneration), idempotencyKey: jobId(item.idempotencyKey) };
|
|
274
|
+
switch (item.verb) {
|
|
275
|
+
case 'publish': {
|
|
276
|
+
jobObject(item, ['schema', 'jobId', 'requestId', 'verb', 'expectedGeneration', 'idempotencyKey', 'result', 'freeze']);
|
|
277
|
+
const result = parsePublishResultV3(item.result);
|
|
278
|
+
if (result.kind === 'package') {
|
|
279
|
+
const freeze = jobObject(item.freeze, ['kind', 'holdId', 'record']);
|
|
280
|
+
if (freeze.kind !== 'producer-held') throw new JobError('JOB_FREEZE_REQUIRED');
|
|
281
|
+
const record = freeze.record === null ? null : jobObject(freeze.record, ['path', 'sha256']);
|
|
282
|
+
const parsedPublish: PublishRequestV3 = {
|
|
283
|
+
...mutation,
|
|
284
|
+
verb: 'publish',
|
|
285
|
+
result,
|
|
286
|
+
freeze: {
|
|
287
|
+
kind: 'producer-held',
|
|
288
|
+
holdId: jobId(freeze.holdId),
|
|
289
|
+
record: record === null ? null : { path: jobPath(record.path), sha256: jobSha(record.sha256) },
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
return parsedPublish;
|
|
293
|
+
}
|
|
294
|
+
const parsedRecordPublish: PublishRequestV3 = {
|
|
295
|
+
...mutation,
|
|
296
|
+
verb: 'publish',
|
|
297
|
+
result,
|
|
298
|
+
freeze: parseRecordFreeze(item.freeze),
|
|
299
|
+
};
|
|
300
|
+
return parsedRecordPublish;
|
|
301
|
+
}
|
|
302
|
+
case 'ask':
|
|
303
|
+
jobObject(item, ['schema', 'jobId', 'requestId', 'verb', 'expectedGeneration', 'idempotencyKey', 'publicationId', 'subject']);
|
|
304
|
+
return { ...mutation, verb: 'ask', publicationId: jobId(item.publicationId), subject: parseJobSubject(item.subject) };
|
|
305
|
+
case 'close':
|
|
306
|
+
jobObject(item, ['schema', 'jobId', 'requestId', 'verb', 'expectedGeneration', 'idempotencyKey', 'reason']);
|
|
307
|
+
return { ...mutation, verb: 'close', reason: jobText(item.reason, 1024) };
|
|
308
|
+
}
|
|
309
|
+
throw new JobError('JOB_REQUEST_INVALID');
|
|
310
|
+
}
|
|
151
311
|
const base = { schema: JOB_API_SCHEMA, jobId: jobId(item.jobId), requestId: jobId(item.requestId) };
|
|
152
312
|
if (item.verb === 'next') {
|
|
153
313
|
jobObject(item, ['schema', 'jobId', 'requestId', 'verb', 'after', 'waitMs']);
|
|
@@ -184,6 +344,659 @@ export function parseJobRequest(value: unknown): JobAgentRequest | JobFetchActRe
|
|
|
184
344
|
throw new JobError('JOB_REQUEST_INVALID');
|
|
185
345
|
}
|
|
186
346
|
|
|
347
|
+
export function parseJobRequestV3(value: unknown): AgentRequestV3 | FetchActRequestV3 {
|
|
348
|
+
return parseJobRequest(value, JOB_API_V3_SCHEMA);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const maxRecordFiles = 4096;
|
|
352
|
+
const maxActWords = 10_000;
|
|
353
|
+
|
|
354
|
+
function asCode<Value>(fn: () => Value, code: JobErrorCode): Value {
|
|
355
|
+
try {
|
|
356
|
+
return fn();
|
|
357
|
+
} catch (error) {
|
|
358
|
+
if (error instanceof JobError && error.code === code) throw error;
|
|
359
|
+
throw new JobError(code);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function exactObject(
|
|
364
|
+
value: unknown,
|
|
365
|
+
keys: readonly string[],
|
|
366
|
+
code: JobErrorCode,
|
|
367
|
+
): Record<string, unknown> {
|
|
368
|
+
const item = asCode(() => jobObject(value, keys), code);
|
|
369
|
+
if (Object.keys(item).length !== keys.length || keys.some(key => item[key] === undefined)) {
|
|
370
|
+
throw new JobError(code);
|
|
371
|
+
}
|
|
372
|
+
return item;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function parseFileRow(value: unknown, code: JobErrorCode): JobFileIdentity {
|
|
376
|
+
const item = exactObject(value, ['path', 'sha256'], code);
|
|
377
|
+
return asCode(() => ({ path: jobPath(item.path), sha256: jobSha(item.sha256) }), code);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function parseFileClosure(
|
|
381
|
+
rawInputs: unknown,
|
|
382
|
+
rawFiles: unknown,
|
|
383
|
+
record: ProjectRecord | undefined,
|
|
384
|
+
code: JobErrorCode,
|
|
385
|
+
): { readonly inputs: readonly JobFileIdentity[]; readonly files: readonly JobFileIdentity[]; readonly byPath: Map<string, string> } {
|
|
386
|
+
if (!Array.isArray(rawInputs) || rawInputs.length > maxRecordFiles ||
|
|
387
|
+
!Array.isArray(rawFiles) || rawFiles.length === 0 || rawFiles.length > maxRecordFiles) {
|
|
388
|
+
throw new JobError(code);
|
|
389
|
+
}
|
|
390
|
+
const files: JobFileIdentity[] = [];
|
|
391
|
+
const byPath = new Map<string, string>();
|
|
392
|
+
for (let index = 0; index < rawFiles.length; index++) {
|
|
393
|
+
const file = parseFileRow(rawFiles[index], code);
|
|
394
|
+
if (index > 0 && !(files[index - 1]!.path < file.path)) {
|
|
395
|
+
throw new JobError(code);
|
|
396
|
+
}
|
|
397
|
+
files.push(file);
|
|
398
|
+
byPath.set(file.path, file.sha256);
|
|
399
|
+
}
|
|
400
|
+
const sealedProjectHash = byPath.get('project.json');
|
|
401
|
+
if (!sealedProjectHash || (record !== undefined && sealedProjectHash !== record.sha256)) {
|
|
402
|
+
throw new JobError(code);
|
|
403
|
+
}
|
|
404
|
+
const inputs: JobFileIdentity[] = [];
|
|
405
|
+
for (const rawInput of rawInputs) {
|
|
406
|
+
const input = parseFileRow(rawInput, code);
|
|
407
|
+
if (byPath.get(input.path) !== input.sha256) {
|
|
408
|
+
throw new JobError(code);
|
|
409
|
+
}
|
|
410
|
+
inputs.push(input);
|
|
411
|
+
}
|
|
412
|
+
return { inputs, files, byPath };
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function parsePlanIdentity(value: unknown, code: JobErrorCode): {
|
|
416
|
+
readonly plan: JobPlanIdentity;
|
|
417
|
+
readonly stepIds: Set<string>;
|
|
418
|
+
} {
|
|
419
|
+
const item = exactObject(value, ['steps'], code);
|
|
420
|
+
if (!Array.isArray(item.steps) || item.steps.length > maxRecordFiles) {
|
|
421
|
+
throw new JobError(code);
|
|
422
|
+
}
|
|
423
|
+
const stepIds = new Set<string>();
|
|
424
|
+
const steps = item.steps.map(rawStep => {
|
|
425
|
+
const step = exactObject(rawStep, ['id', 'hash'], code);
|
|
426
|
+
const id = asCode(() => jobId(step.id), code);
|
|
427
|
+
const hash = asCode(() => jobSha(step.hash), code);
|
|
428
|
+
if (stepIds.has(id)) throw new JobError(code);
|
|
429
|
+
stepIds.add(id);
|
|
430
|
+
return { id, hash };
|
|
431
|
+
});
|
|
432
|
+
return { plan: { steps }, stepIds };
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function parsePlanSubjects(
|
|
436
|
+
rawSubjects: unknown,
|
|
437
|
+
stepIds: ReadonlySet<string>,
|
|
438
|
+
code: JobErrorCode,
|
|
439
|
+
): readonly RecordPlanSubject[] {
|
|
440
|
+
if (!Array.isArray(rawSubjects) || rawSubjects.length > maxRecordFiles) {
|
|
441
|
+
throw new JobError(code);
|
|
442
|
+
}
|
|
443
|
+
if (rawSubjects.length > 0 && stepIds.size === 0) {
|
|
444
|
+
throw new JobError(code);
|
|
445
|
+
}
|
|
446
|
+
const seen = new Set<string>();
|
|
447
|
+
return rawSubjects.map(raw => {
|
|
448
|
+
const parsed = asCode(() => parseRecordSubject(raw), code);
|
|
449
|
+
if (parsed.kind === 'interview-picks') throw new JobError(code);
|
|
450
|
+
if (parsed.kind === 'checkpoint') {
|
|
451
|
+
if (!stepIds.has(parsed.checkpointId)) throw new JobError(code);
|
|
452
|
+
const key = `checkpoint:${parsed.checkpointId}`;
|
|
453
|
+
if (seen.has(key)) throw new JobError(code);
|
|
454
|
+
seen.add(key);
|
|
455
|
+
return { kind: 'checkpoint', checkpointId: parsed.checkpointId };
|
|
456
|
+
}
|
|
457
|
+
const key = `signoff:${parsed.stage}`;
|
|
458
|
+
if (seen.has(key)) throw new JobError(code);
|
|
459
|
+
seen.add(key);
|
|
460
|
+
return { kind: 'signoff', stage: parsed.stage };
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function parseOptionSubjects(
|
|
465
|
+
rawSubjects: unknown,
|
|
466
|
+
selection: RecordOptionResult,
|
|
467
|
+
code: JobErrorCode,
|
|
468
|
+
): readonly [RecordPicksSubject] {
|
|
469
|
+
if (!Array.isArray(rawSubjects) || rawSubjects.length !== 1) {
|
|
470
|
+
throw new JobError(code);
|
|
471
|
+
}
|
|
472
|
+
const parsed = asCode(() => parseRecordSubject(rawSubjects[0]), code);
|
|
473
|
+
if (parsed.kind !== 'interview-picks' ||
|
|
474
|
+
parsed.questionId !== selection.questionId ||
|
|
475
|
+
parsed.setId !== selection.setId) {
|
|
476
|
+
throw new JobError(code);
|
|
477
|
+
}
|
|
478
|
+
return [{ kind: 'interview-picks', questionId: parsed.questionId, setId: parsed.setId }];
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
export function parseRecordPartsIdentity(
|
|
482
|
+
value: unknown,
|
|
483
|
+
code: JobErrorCode = 'JOB_SOURCE_MISMATCH',
|
|
484
|
+
): RecordPartsIdentity {
|
|
485
|
+
const raw = exactObject(value, ['name', 'version', 'integrity'], code);
|
|
486
|
+
return {
|
|
487
|
+
name: asCode(() => jobText(raw.name), code),
|
|
488
|
+
version: asCode(() => jobText(raw.version), code),
|
|
489
|
+
integrity: asCode(() => jobText(raw.integrity), code),
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
type ParsedRecordPayload =
|
|
494
|
+
| {
|
|
495
|
+
readonly kind: 'option-set';
|
|
496
|
+
readonly inputs: readonly JobFileIdentity[];
|
|
497
|
+
readonly files: readonly JobFileIdentity[];
|
|
498
|
+
readonly plan: null;
|
|
499
|
+
readonly subjects: readonly [RecordPicksSubject];
|
|
500
|
+
}
|
|
501
|
+
| {
|
|
502
|
+
readonly kind: 'plan';
|
|
503
|
+
readonly inputs: readonly JobFileIdentity[];
|
|
504
|
+
readonly files: readonly JobFileIdentity[];
|
|
505
|
+
readonly plan: JobPlanIdentity;
|
|
506
|
+
readonly subjects: readonly RecordPlanSubject[];
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
function parseRecordClosureAndSubjects(
|
|
510
|
+
rawInputs: unknown,
|
|
511
|
+
rawFiles: unknown,
|
|
512
|
+
rawPlan: unknown,
|
|
513
|
+
rawSubjects: unknown,
|
|
514
|
+
selection: RecordResult,
|
|
515
|
+
checkedRecord?: ProjectRecord,
|
|
516
|
+
): ParsedRecordPayload {
|
|
517
|
+
const { inputs, files } = parseFileClosure(rawInputs, rawFiles, checkedRecord, 'JOB_SOURCE_MISMATCH');
|
|
518
|
+
if (selection.kind === 'option-set') {
|
|
519
|
+
if (rawPlan !== null) throw new JobError('JOB_SOURCE_MISMATCH');
|
|
520
|
+
const subjects = parseOptionSubjects(rawSubjects, selection, 'JOB_SOURCE_MISMATCH');
|
|
521
|
+
return { kind: 'option-set', inputs, files, plan: null, subjects };
|
|
522
|
+
}
|
|
523
|
+
if (rawPlan === null) throw new JobError('JOB_SOURCE_MISMATCH');
|
|
524
|
+
const { plan, stepIds } = parsePlanIdentity(rawPlan, 'JOB_SOURCE_MISMATCH');
|
|
525
|
+
const subjects = parsePlanSubjects(rawSubjects, stepIds, 'JOB_SOURCE_MISMATCH');
|
|
526
|
+
return { kind: 'plan', inputs, files, plan, subjects };
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
export function parseRecordPublishedContext(
|
|
530
|
+
value: unknown,
|
|
531
|
+
result: RecordOptionResult,
|
|
532
|
+
record?: ProjectRecord,
|
|
533
|
+
): OptionContext;
|
|
534
|
+
export function parseRecordPublishedContext(
|
|
535
|
+
value: unknown,
|
|
536
|
+
result: RecordPlanResult,
|
|
537
|
+
record?: ProjectRecord,
|
|
538
|
+
): PlanContext;
|
|
539
|
+
export function parseRecordPublishedContext(
|
|
540
|
+
value: unknown,
|
|
541
|
+
result: RecordResult,
|
|
542
|
+
record?: ProjectRecord,
|
|
543
|
+
): OptionContext | PlanContext;
|
|
544
|
+
export function parseRecordPublishedContext(
|
|
545
|
+
value: unknown,
|
|
546
|
+
result: RecordResult,
|
|
547
|
+
record?: ProjectRecord,
|
|
548
|
+
): OptionContext | PlanContext {
|
|
549
|
+
const selection = parseRecordResult(result);
|
|
550
|
+
const checkedRecord = record === undefined ? undefined : parseProjectRecord(record, 'JOB_SOURCE_MISMATCH');
|
|
551
|
+
const item = exactObject(value, ['label', 'parts', 'inputs', 'files', 'plan', 'subjects'], 'JOB_SOURCE_MISMATCH');
|
|
552
|
+
const label = asCode(() => jobText(item.label), 'JOB_SOURCE_MISMATCH');
|
|
553
|
+
const parts = parseRecordPartsIdentity(item.parts, 'JOB_SOURCE_MISMATCH');
|
|
554
|
+
const body = parseRecordClosureAndSubjects(
|
|
555
|
+
item.inputs,
|
|
556
|
+
item.files,
|
|
557
|
+
item.plan,
|
|
558
|
+
item.subjects,
|
|
559
|
+
selection,
|
|
560
|
+
checkedRecord,
|
|
561
|
+
);
|
|
562
|
+
if (body.kind === 'option-set') {
|
|
563
|
+
return { label, parts, inputs: body.inputs, files: body.files, plan: null, subjects: body.subjects };
|
|
564
|
+
}
|
|
565
|
+
return { label, parts, inputs: body.inputs, files: body.files, plan: body.plan, subjects: body.subjects };
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
export function assertEqualRecordContexts(
|
|
569
|
+
inventory: OptionContext | PlanContext,
|
|
570
|
+
retained: OptionContext | PlanContext,
|
|
571
|
+
): void {
|
|
572
|
+
const inferResult = (ctx: OptionContext | PlanContext): RecordResult => {
|
|
573
|
+
if (ctx.plan === null) {
|
|
574
|
+
const first = Array.isArray(ctx.subjects) ? ctx.subjects[0] : undefined;
|
|
575
|
+
if (!first || first.kind !== 'interview-picks') throw new JobError('JOB_SOURCE_MISMATCH');
|
|
576
|
+
return { kind: 'option-set', questionId: first.questionId, setId: first.setId };
|
|
577
|
+
}
|
|
578
|
+
return { kind: 'plan' };
|
|
579
|
+
};
|
|
580
|
+
const selection = inferResult(inventory);
|
|
581
|
+
const a = parseRecordPublishedContext(inventory, selection);
|
|
582
|
+
const b = parseRecordPublishedContext(retained, selection);
|
|
583
|
+
if (JSON.stringify(a) !== JSON.stringify(b)) {
|
|
584
|
+
throw new JobError('JOB_SOURCE_MISMATCH');
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
export interface RecordSourceDigestInput {
|
|
589
|
+
readonly schema?: typeof JOB_RECORD_SOURCE_SCHEMA;
|
|
590
|
+
readonly sourceHash?: JobSha256;
|
|
591
|
+
readonly projectHash?: JobSha256;
|
|
592
|
+
readonly subject?: 'option-set' | 'plan';
|
|
593
|
+
readonly record: ProjectRecord;
|
|
594
|
+
readonly selection: RecordResult;
|
|
595
|
+
readonly inputs: readonly JobFileIdentity[];
|
|
596
|
+
readonly optionSet: { readonly id: string; readonly sha256: JobSha256 } | null;
|
|
597
|
+
readonly optionSetFile: JobFileIdentity | null;
|
|
598
|
+
readonly plan: JobPlanIdentity | null;
|
|
599
|
+
readonly subjects: readonly RecordSubject[];
|
|
600
|
+
readonly files: readonly JobFileIdentity[];
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
export function serializeRecordSourceDigestPayload(input: RecordSourceDigestInput): string {
|
|
604
|
+
const item = asCode(
|
|
605
|
+
() =>
|
|
606
|
+
jobObject(input, [
|
|
607
|
+
'schema',
|
|
608
|
+
'sourceHash',
|
|
609
|
+
'projectHash',
|
|
610
|
+
'subject',
|
|
611
|
+
'record',
|
|
612
|
+
'selection',
|
|
613
|
+
'inputs',
|
|
614
|
+
'optionSet',
|
|
615
|
+
'optionSetFile',
|
|
616
|
+
'plan',
|
|
617
|
+
'subjects',
|
|
618
|
+
'files',
|
|
619
|
+
]),
|
|
620
|
+
'JOB_SOURCE_MISMATCH',
|
|
621
|
+
);
|
|
622
|
+
for (const key of ['record', 'selection', 'inputs', 'optionSet', 'optionSetFile', 'plan', 'subjects', 'files'] as const) {
|
|
623
|
+
if (!(key in item) || item[key] === undefined) throw new JobError('JOB_SOURCE_MISMATCH');
|
|
624
|
+
}
|
|
625
|
+
if (item.schema !== undefined && item.schema !== JOB_RECORD_SOURCE_SCHEMA) {
|
|
626
|
+
throw new JobError('JOB_SOURCE_MISMATCH');
|
|
627
|
+
}
|
|
628
|
+
if (item.sourceHash !== undefined) {
|
|
629
|
+
asCode(() => jobSha(item.sourceHash), 'JOB_SOURCE_MISMATCH');
|
|
630
|
+
}
|
|
631
|
+
const record = parseProjectRecord(item.record, 'JOB_SOURCE_MISMATCH');
|
|
632
|
+
if (item.projectHash !== undefined && asCode(() => jobSha(item.projectHash), 'JOB_SOURCE_MISMATCH') !== record.sha256) {
|
|
633
|
+
throw new JobError('JOB_SOURCE_MISMATCH');
|
|
634
|
+
}
|
|
635
|
+
const selection = asCode(() => parseRecordResult(item.selection), 'JOB_SOURCE_MISMATCH');
|
|
636
|
+
if (item.subject !== undefined && item.subject !== selection.kind) {
|
|
637
|
+
throw new JobError('JOB_SOURCE_MISMATCH');
|
|
638
|
+
}
|
|
639
|
+
const { inputs, files, byPath } = parseFileClosure(item.inputs, item.files, record, 'JOB_SOURCE_MISMATCH');
|
|
640
|
+
|
|
641
|
+
if (selection.kind === 'option-set') {
|
|
642
|
+
if (item.plan !== null || item.optionSet === null || item.optionSetFile === null) {
|
|
643
|
+
throw new JobError('JOB_SOURCE_MISMATCH');
|
|
644
|
+
}
|
|
645
|
+
const rawOptionSet = exactObject(item.optionSet, ['id', 'sha256'], 'JOB_SOURCE_MISMATCH');
|
|
646
|
+
const optionSet = {
|
|
647
|
+
id: asCode(() => jobId(rawOptionSet.id), 'JOB_SOURCE_MISMATCH'),
|
|
648
|
+
sha256: asCode(() => jobSha(rawOptionSet.sha256), 'JOB_SOURCE_MISMATCH'),
|
|
649
|
+
};
|
|
650
|
+
const optionSetFile = parseFileRow(item.optionSetFile, 'JOB_SOURCE_MISMATCH');
|
|
651
|
+
if (optionSet.id !== selection.setId ||
|
|
652
|
+
optionSet.sha256 !== optionSetFile.sha256 ||
|
|
653
|
+
optionSetFile.path === 'project.json' ||
|
|
654
|
+
byPath.get(optionSetFile.path) !== optionSetFile.sha256) {
|
|
655
|
+
throw new JobError('JOB_SOURCE_MISMATCH');
|
|
656
|
+
}
|
|
657
|
+
const subjects = parseOptionSubjects(item.subjects, selection, 'JOB_SOURCE_MISMATCH');
|
|
658
|
+
return JSON.stringify({
|
|
659
|
+
schema: JOB_RECORD_SOURCE_SCHEMA,
|
|
660
|
+
record: { path: 'project.json', sha256: record.sha256 },
|
|
661
|
+
selection: { kind: 'option-set', questionId: selection.questionId, setId: selection.setId },
|
|
662
|
+
inputs,
|
|
663
|
+
optionSet,
|
|
664
|
+
optionSetFile,
|
|
665
|
+
plan: null,
|
|
666
|
+
subjects,
|
|
667
|
+
files,
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
if (item.optionSet !== null || item.optionSetFile !== null || item.plan === null) {
|
|
672
|
+
throw new JobError('JOB_SOURCE_MISMATCH');
|
|
673
|
+
}
|
|
674
|
+
const { plan, stepIds } = parsePlanIdentity(item.plan, 'JOB_SOURCE_MISMATCH');
|
|
675
|
+
const subjects = parsePlanSubjects(item.subjects, stepIds, 'JOB_SOURCE_MISMATCH');
|
|
676
|
+
return JSON.stringify({
|
|
677
|
+
schema: JOB_RECORD_SOURCE_SCHEMA,
|
|
678
|
+
record: { path: 'project.json', sha256: record.sha256 },
|
|
679
|
+
selection: { kind: 'plan' },
|
|
680
|
+
inputs,
|
|
681
|
+
optionSet: null,
|
|
682
|
+
optionSetFile: null,
|
|
683
|
+
plan,
|
|
684
|
+
subjects,
|
|
685
|
+
files,
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
export function recordSourceDigest(input: RecordSourceDigestInput): JobSha256 {
|
|
690
|
+
return jobHash(serializeRecordSourceDigestPayload(input));
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
export function recordInstanceKey(fingerprint: unknown, sourceHash: unknown): JobSha256 {
|
|
694
|
+
if (typeof fingerprint !== 'string') throw new JobError('JOB_REQUEST_INVALID');
|
|
695
|
+
const checkedSourceHash = jobSha(sourceHash);
|
|
696
|
+
return jobHash(
|
|
697
|
+
JSON.stringify({
|
|
698
|
+
schema: JOB_RECORD_INSTANCE_SCHEMA,
|
|
699
|
+
fingerprint,
|
|
700
|
+
sourceHash: checkedSourceHash,
|
|
701
|
+
}),
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
function assembleRecordSource(
|
|
706
|
+
record: ProjectRecord,
|
|
707
|
+
selection: RecordResult,
|
|
708
|
+
body: ParsedRecordPayload,
|
|
709
|
+
rawOptionSetFile?: unknown,
|
|
710
|
+
): OptionSource | PlanSource {
|
|
711
|
+
if (selection.kind === 'option-set') {
|
|
712
|
+
if (body.kind !== 'option-set' || !rawOptionSetFile) throw new JobError('JOB_SOURCE_MISMATCH');
|
|
713
|
+
const optionSetFile = parseFileRow(rawOptionSetFile, 'JOB_SOURCE_MISMATCH');
|
|
714
|
+
const optionSet = { id: selection.setId, sha256: optionSetFile.sha256 };
|
|
715
|
+
const sourceHash = recordSourceDigest({
|
|
716
|
+
record,
|
|
717
|
+
selection,
|
|
718
|
+
inputs: body.inputs,
|
|
719
|
+
optionSet,
|
|
720
|
+
optionSetFile,
|
|
721
|
+
plan: null,
|
|
722
|
+
subjects: body.subjects,
|
|
723
|
+
files: body.files,
|
|
724
|
+
});
|
|
725
|
+
return {
|
|
726
|
+
sourceHash,
|
|
727
|
+
projectHash: record.sha256,
|
|
728
|
+
record,
|
|
729
|
+
inputs: body.inputs,
|
|
730
|
+
files: body.files,
|
|
731
|
+
subject: 'option-set',
|
|
732
|
+
selection: { kind: 'option-set', questionId: selection.questionId, setId: selection.setId },
|
|
733
|
+
optionSet,
|
|
734
|
+
optionSetFile,
|
|
735
|
+
plan: null,
|
|
736
|
+
subjects: body.subjects,
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
if (body.kind !== 'plan' || (rawOptionSetFile !== undefined && rawOptionSetFile !== null)) {
|
|
740
|
+
throw new JobError('JOB_SOURCE_MISMATCH');
|
|
741
|
+
}
|
|
742
|
+
const sourceHash = recordSourceDigest({
|
|
743
|
+
record,
|
|
744
|
+
selection,
|
|
745
|
+
inputs: body.inputs,
|
|
746
|
+
optionSet: null,
|
|
747
|
+
optionSetFile: null,
|
|
748
|
+
plan: body.plan,
|
|
749
|
+
subjects: body.subjects,
|
|
750
|
+
files: body.files,
|
|
751
|
+
});
|
|
752
|
+
return {
|
|
753
|
+
sourceHash,
|
|
754
|
+
projectHash: record.sha256,
|
|
755
|
+
record,
|
|
756
|
+
inputs: body.inputs,
|
|
757
|
+
files: body.files,
|
|
758
|
+
subject: 'plan',
|
|
759
|
+
selection: { kind: 'plan' },
|
|
760
|
+
optionSet: null,
|
|
761
|
+
optionSetFile: null,
|
|
762
|
+
plan: body.plan,
|
|
763
|
+
subjects: body.subjects,
|
|
764
|
+
};
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
export function buildRecordSource(input: {
|
|
768
|
+
readonly record: ProjectRecord;
|
|
769
|
+
readonly selection: RecordOptionResult;
|
|
770
|
+
readonly context: OptionContext;
|
|
771
|
+
readonly optionSetFile: JobFileIdentity;
|
|
772
|
+
}): OptionSource;
|
|
773
|
+
export function buildRecordSource(input: {
|
|
774
|
+
readonly record: ProjectRecord;
|
|
775
|
+
readonly selection: RecordPlanResult;
|
|
776
|
+
readonly context: PlanContext;
|
|
777
|
+
readonly optionSetFile?: null;
|
|
778
|
+
}): PlanSource;
|
|
779
|
+
export function buildRecordSource(input: {
|
|
780
|
+
readonly record: ProjectRecord;
|
|
781
|
+
readonly selection: RecordResult;
|
|
782
|
+
readonly context: OptionContext | PlanContext;
|
|
783
|
+
readonly optionSetFile?: JobFileIdentity | null;
|
|
784
|
+
}): OptionSource | PlanSource {
|
|
785
|
+
const record = parseProjectRecord(input.record, 'JOB_SOURCE_MISMATCH');
|
|
786
|
+
const selection = asCode(() => parseRecordResult(input.selection), 'JOB_SOURCE_MISMATCH');
|
|
787
|
+
const context = parseRecordPublishedContext(input.context, selection, record);
|
|
788
|
+
const body: ParsedRecordPayload =
|
|
789
|
+
context.plan === null
|
|
790
|
+
? {
|
|
791
|
+
kind: 'option-set',
|
|
792
|
+
inputs: context.inputs,
|
|
793
|
+
files: context.files,
|
|
794
|
+
plan: null,
|
|
795
|
+
subjects: context.subjects,
|
|
796
|
+
}
|
|
797
|
+
: {
|
|
798
|
+
kind: 'plan',
|
|
799
|
+
inputs: context.inputs,
|
|
800
|
+
files: context.files,
|
|
801
|
+
plan: context.plan,
|
|
802
|
+
subjects: context.subjects,
|
|
803
|
+
};
|
|
804
|
+
return assembleRecordSource(record, selection, body, input.optionSetFile);
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
export function parseRecordSource(value: unknown): RecordSource {
|
|
808
|
+
const item = exactObject(
|
|
809
|
+
value,
|
|
810
|
+
[
|
|
811
|
+
'sourceHash',
|
|
812
|
+
'projectHash',
|
|
813
|
+
'record',
|
|
814
|
+
'inputs',
|
|
815
|
+
'files',
|
|
816
|
+
'subject',
|
|
817
|
+
'selection',
|
|
818
|
+
'optionSet',
|
|
819
|
+
'optionSetFile',
|
|
820
|
+
'plan',
|
|
821
|
+
'subjects',
|
|
822
|
+
],
|
|
823
|
+
'JOB_SOURCE_MISMATCH',
|
|
824
|
+
);
|
|
825
|
+
const record = parseProjectRecord(item.record, 'JOB_SOURCE_MISMATCH');
|
|
826
|
+
const projectHash = asCode(() => jobSha(item.projectHash), 'JOB_SOURCE_MISMATCH');
|
|
827
|
+
const sourceHash = asCode(() => jobSha(item.sourceHash), 'JOB_SOURCE_MISMATCH');
|
|
828
|
+
if (projectHash !== record.sha256) throw new JobError('JOB_SOURCE_MISMATCH');
|
|
829
|
+
const selection = asCode(() => parseRecordResult(item.selection), 'JOB_SOURCE_MISMATCH');
|
|
830
|
+
if (item.subject !== selection.kind) throw new JobError('JOB_SOURCE_MISMATCH');
|
|
831
|
+
const body = parseRecordClosureAndSubjects(
|
|
832
|
+
item.inputs,
|
|
833
|
+
item.files,
|
|
834
|
+
item.plan,
|
|
835
|
+
item.subjects,
|
|
836
|
+
selection,
|
|
837
|
+
record,
|
|
838
|
+
);
|
|
839
|
+
if (selection.kind === 'option-set') {
|
|
840
|
+
const rawOptionSet = exactObject(item.optionSet, ['id', 'sha256'], 'JOB_SOURCE_MISMATCH');
|
|
841
|
+
const built = assembleRecordSource(record, selection, body, item.optionSetFile) as OptionSource;
|
|
842
|
+
if (rawOptionSet.id !== built.optionSet.id ||
|
|
843
|
+
rawOptionSet.sha256 !== built.optionSet.sha256 ||
|
|
844
|
+
sourceHash !== built.sourceHash) {
|
|
845
|
+
throw new JobError('JOB_SOURCE_MISMATCH');
|
|
846
|
+
}
|
|
847
|
+
return built;
|
|
848
|
+
}
|
|
849
|
+
if (item.optionSet !== null || item.optionSetFile !== null) {
|
|
850
|
+
throw new JobError('JOB_SOURCE_MISMATCH');
|
|
851
|
+
}
|
|
852
|
+
const built = assembleRecordSource(record, selection, body, null) as PlanSource;
|
|
853
|
+
if (sourceHash !== built.sourceHash) throw new JobError('JOB_SOURCE_MISMATCH');
|
|
854
|
+
return built;
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
export function isRecordSource(value: unknown): value is RecordSource {
|
|
858
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
859
|
+
const subject = (value as { subject?: unknown }).subject;
|
|
860
|
+
return subject === 'option-set' || subject === 'plan';
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
export function validateRecordAskSubject(subject: unknown, source: RecordSource): RecordSubject {
|
|
864
|
+
const checkedSource = parseRecordSource(source);
|
|
865
|
+
const parsed = parseRecordSubject(subject);
|
|
866
|
+
if (checkedSource.subject === 'option-set') {
|
|
867
|
+
const expected = checkedSource.subjects[0];
|
|
868
|
+
if (parsed.kind !== 'interview-picks' ||
|
|
869
|
+
parsed.questionId !== expected.questionId ||
|
|
870
|
+
parsed.setId !== expected.setId) {
|
|
871
|
+
throw new JobError('JOB_ASK_INVALID');
|
|
872
|
+
}
|
|
873
|
+
return parsed;
|
|
874
|
+
}
|
|
875
|
+
if (parsed.kind === 'interview-picks' || checkedSource.plan.steps.length === 0) {
|
|
876
|
+
throw new JobError('JOB_ASK_INVALID');
|
|
877
|
+
}
|
|
878
|
+
const declared = checkedSource.subjects.some(candidate =>
|
|
879
|
+
candidate.kind === parsed.kind &&
|
|
880
|
+
(candidate.kind === 'checkpoint'
|
|
881
|
+
? candidate.checkpointId === (parsed as Extract<RecordPlanSubject, { kind: 'checkpoint' }>).checkpointId
|
|
882
|
+
: candidate.stage === (parsed as Extract<RecordPlanSubject, { kind: 'signoff' }>).stage),
|
|
883
|
+
);
|
|
884
|
+
if (!declared) throw new JobError('JOB_ASK_INVALID');
|
|
885
|
+
return parsed;
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
function parseHumanAuthority(value: unknown, code: JobErrorCode): JobHumanAuthority {
|
|
889
|
+
const auth = exactObject(value, ['actor', 'channel', 'reviewer'], code);
|
|
890
|
+
if (auth.actor !== 'human' || auth.channel !== 'review-ui') throw new JobError(code);
|
|
891
|
+
const reviewer = exactObject(auth.reviewer, ['id', 'role'], code);
|
|
892
|
+
if (typeof reviewer.id !== 'string' || !reviewer.id) throw new JobError(code);
|
|
893
|
+
if (reviewer.role !== 'owner' && reviewer.role !== 'delegate') throw new JobError(code);
|
|
894
|
+
return {
|
|
895
|
+
actor: 'human',
|
|
896
|
+
channel: 'review-ui',
|
|
897
|
+
reviewer: { id: reviewer.id, role: reviewer.role },
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
function parseActWords(value: unknown, code: JobErrorCode): string {
|
|
902
|
+
if (typeof value !== 'string' || value.length > maxActWords) throw new JobError(code);
|
|
903
|
+
return value;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
export function parseRecordAct(value: unknown): RecordAct {
|
|
907
|
+
const item = exactObject(
|
|
908
|
+
value,
|
|
909
|
+
['schema', 'actId', 'jobId', 'askId', 'publicationId', 'kind', 'authority', 'at', 'source', 'value'],
|
|
910
|
+
'JOB_ACT_INVALID',
|
|
911
|
+
);
|
|
912
|
+
if (item.schema !== JOB_RECORD_ACT_SCHEMA) throw new JobError('JOB_ACT_INVALID');
|
|
913
|
+
const actId = asCode(() => jobId(item.actId), 'JOB_ACT_INVALID');
|
|
914
|
+
const jobIdValue = asCode(() => jobId(item.jobId), 'JOB_ACT_INVALID');
|
|
915
|
+
const askId = asCode(() => jobId(item.askId), 'JOB_ACT_INVALID');
|
|
916
|
+
const publicationId = asCode(() => jobId(item.publicationId), 'JOB_ACT_INVALID');
|
|
917
|
+
const authority = parseHumanAuthority(item.authority, 'JOB_ACT_INVALID');
|
|
918
|
+
const at = asCode(() => jobText(item.at, 128), 'JOB_ACT_INVALID');
|
|
919
|
+
if (Number.isNaN(Date.parse(at))) throw new JobError('JOB_ACT_INVALID');
|
|
920
|
+
const source = asCode(() => parseRecordSource(item.source), 'JOB_ACT_INVALID');
|
|
921
|
+
|
|
922
|
+
switch (item.kind) {
|
|
923
|
+
case 'interview-picks': {
|
|
924
|
+
if (source.subject !== 'option-set') throw new JobError('JOB_ACT_INVALID');
|
|
925
|
+
const rawValue = exactObject(item.value, ['questionId', 'selections', 'note'], 'JOB_ACT_INVALID');
|
|
926
|
+
const questionId = asCode(() => jobId(rawValue.questionId), 'JOB_ACT_INVALID');
|
|
927
|
+
if (questionId !== source.selection.questionId || !Array.isArray(rawValue.selections) ||
|
|
928
|
+
rawValue.selections.length > maxRecordFiles) {
|
|
929
|
+
throw new JobError('JOB_ACT_INVALID');
|
|
930
|
+
}
|
|
931
|
+
const selections = rawValue.selections.map(rawSelection => {
|
|
932
|
+
const entry = exactObject(rawSelection, ['itemId', 'optionId', 'otherText'], 'JOB_ACT_INVALID');
|
|
933
|
+
const itemId = asCode(() => jobId(entry.itemId), 'JOB_ACT_INVALID');
|
|
934
|
+
const optionId = asCode(() => jobId(entry.optionId), 'JOB_ACT_INVALID');
|
|
935
|
+
const otherText = entry.otherText === null ? null : parseActWords(entry.otherText, 'JOB_ACT_INVALID');
|
|
936
|
+
return { itemId, optionId, otherText };
|
|
937
|
+
});
|
|
938
|
+
const note = parseActWords(rawValue.note, 'JOB_ACT_INVALID');
|
|
939
|
+
return {
|
|
940
|
+
schema: JOB_RECORD_ACT_SCHEMA,
|
|
941
|
+
actId,
|
|
942
|
+
jobId: jobIdValue,
|
|
943
|
+
askId,
|
|
944
|
+
publicationId,
|
|
945
|
+
kind: 'interview-picks',
|
|
946
|
+
authority,
|
|
947
|
+
at,
|
|
948
|
+
source,
|
|
949
|
+
value: { questionId, selections, note },
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
case 'checkpoint': {
|
|
953
|
+
if (source.subject !== 'plan' || source.plan.steps.length === 0) throw new JobError('JOB_ACT_INVALID');
|
|
954
|
+
const rawValue = exactObject(item.value, ['checkpointId', 'planStepId', 'outcome', 'words'], 'JOB_ACT_INVALID');
|
|
955
|
+
const checkpointId = asCode(() => jobId(rawValue.checkpointId), 'JOB_ACT_INVALID');
|
|
956
|
+
const planStepId = asCode(() => jobId(rawValue.planStepId), 'JOB_ACT_INVALID');
|
|
957
|
+
if (planStepId !== checkpointId) throw new JobError('JOB_ACT_INVALID');
|
|
958
|
+
asCode(() => validateRecordAskSubject({ kind: 'checkpoint', checkpointId }, source), 'JOB_ACT_INVALID');
|
|
959
|
+
if (rawValue.outcome !== 'pass' && rawValue.outcome !== 'fail') throw new JobError('JOB_ACT_INVALID');
|
|
960
|
+
const words = parseActWords(rawValue.words, 'JOB_ACT_INVALID');
|
|
961
|
+
return {
|
|
962
|
+
schema: JOB_RECORD_ACT_SCHEMA,
|
|
963
|
+
actId,
|
|
964
|
+
jobId: jobIdValue,
|
|
965
|
+
askId,
|
|
966
|
+
publicationId,
|
|
967
|
+
kind: 'checkpoint',
|
|
968
|
+
authority,
|
|
969
|
+
at,
|
|
970
|
+
source,
|
|
971
|
+
value: { checkpointId, planStepId, outcome: rawValue.outcome, words },
|
|
972
|
+
};
|
|
973
|
+
}
|
|
974
|
+
case 'signoff': {
|
|
975
|
+
if (source.subject !== 'plan' || source.plan.steps.length === 0) throw new JobError('JOB_ACT_INVALID');
|
|
976
|
+
const rawValue = exactObject(item.value, ['stage', 'decision', 'words'], 'JOB_ACT_INVALID');
|
|
977
|
+
const stage = asCode(() => jobId(rawValue.stage), 'JOB_ACT_INVALID');
|
|
978
|
+
if (!(JOB_PLAN_BOUND_SIGNOFF_STAGES as readonly string[]).includes(stage) || rawValue.decision !== 'approved') {
|
|
979
|
+
throw new JobError('JOB_ACT_INVALID');
|
|
980
|
+
}
|
|
981
|
+
asCode(() => validateRecordAskSubject({ kind: 'signoff', stage }, source), 'JOB_ACT_INVALID');
|
|
982
|
+
const words = parseActWords(rawValue.words, 'JOB_ACT_INVALID');
|
|
983
|
+
return {
|
|
984
|
+
schema: JOB_RECORD_ACT_SCHEMA,
|
|
985
|
+
actId,
|
|
986
|
+
jobId: jobIdValue,
|
|
987
|
+
askId,
|
|
988
|
+
publicationId,
|
|
989
|
+
kind: 'signoff',
|
|
990
|
+
authority,
|
|
991
|
+
at,
|
|
992
|
+
source,
|
|
993
|
+
value: { stage: stage as RecordSignoffStage, decision: 'approved', words },
|
|
994
|
+
};
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
throw new JobError('JOB_ACT_INVALID');
|
|
998
|
+
}
|
|
999
|
+
|
|
187
1000
|
export async function jobBody(request: Request, maximum = jobBodyLimit): Promise<unknown> {
|
|
188
1001
|
if (!/^application\/json(?:\s*;|$)/i.test(request.headers.get('content-type') ?? '')) {
|
|
189
1002
|
throw new JobError('JOB_REQUEST_INVALID');
|
|
@@ -280,7 +1093,7 @@ export function createJobHostPorts(session: JobSession, origin: string): JobHost
|
|
|
280
1093
|
const status = failure.error.code.includes('CAPABILITY') || failure.error.code === 'JOB_AUTHORITY_REFUSED' ? 403 :
|
|
281
1094
|
failure.error.code === 'JOB_BODY_LIMIT' ? 413 : failure.error.code === 'JOB_REQUEST_INVALID' ? 400 : 409;
|
|
282
1095
|
return Response.json({ schema: JOB_API_SCHEMA, verb: input?.verb ?? null, jobId: session.jobId,
|
|
283
|
-
requestId: input?.requestId ?? null, generation: session.
|
|
1096
|
+
requestId: input?.requestId ?? null, generation: session.lastObservedGeneration, ...failure }, { status, headers: responseHeaders });
|
|
284
1097
|
}
|
|
285
1098
|
},
|
|
286
1099
|
subscribeJob: input => session.subscribe(input),
|