@rig-c/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-cli.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
2
|
import {closeSync, constants, fstatSync, fsyncSync, lstatSync, openSync, readFileSync, renameSync, unlinkSync, writeSync} from 'node:fs';
|
|
3
3
|
import {join} from 'node:path';
|
|
4
|
-
import { JOB_API_SCHEMA, JOB_ACT_SCHEMA, JOB_RIG_REVIEW_ACT_SCHEMA, jobRawActMediaType, jobStillByteLimit,
|
|
5
|
-
type JobAct, type JobAgentRequest } from './job-contract.ts';
|
|
6
|
-
import { assertJobApiSchema, jobFailure, jobHash, jobId, jobObject, parseJobRequest } from './job-api.ts';
|
|
4
|
+
import { JOB_API_SCHEMA, JOB_ACT_SCHEMA, JOB_RIG_REVIEW_ACT_SCHEMA, JOB_ENABLED_ACT_KINDS, jobRawActMediaType, jobStillByteLimit,
|
|
5
|
+
type JobAct, type JobAgentRequest, type JobNextRequest, type JobNextValue } from './job-contract.ts';
|
|
6
|
+
import { assertJobApiSchema, jobFailure, jobHash, jobId, jobInteger, jobObject, jobWaitLimitMs, parseJobRequest } from './job-api.ts';
|
|
7
7
|
import { JobError } from './job-store.ts';
|
|
8
8
|
import {validatePngBytes} from './comment-crops.ts';
|
|
9
9
|
import {destinationDirectory} from './destination-directory.ts';
|
|
@@ -29,7 +29,7 @@ const commonFlags = ['generation', 'key', 'request-id'];
|
|
|
29
29
|
const verbFlags: Record<string, readonly string[]> = {
|
|
30
30
|
publish: [...commonFlags, 'result', 'selector', 'freeze-hold', 'record', 'record-hash'],
|
|
31
31
|
ask: [...commonFlags, 'publication', 'subject'],
|
|
32
|
-
next: ['after', 'wait-ms', 'request-id'],
|
|
32
|
+
next: ['after', 'wait-ms', 'request-id', 'until'],
|
|
33
33
|
close: [...commonFlags, 'reason'],
|
|
34
34
|
};
|
|
35
35
|
|
|
@@ -59,15 +59,24 @@ function semanticJSON(value: string): unknown {
|
|
|
59
59
|
/** Public semantic request construction only. There is no act-writing or
|
|
60
60
|
* consumer-fetch verb, no internal JSON input file and no Review state access.
|
|
61
61
|
*/
|
|
62
|
-
export function parseJobCLI(args: readonly string[]): { readonly endpoint: string; readonly request: JobAgentRequest } {
|
|
62
|
+
export function parseJobCLI(args: readonly string[]): { readonly endpoint: string; readonly request: JobAgentRequest; readonly waitUntilActMs?: number } {
|
|
63
63
|
const [verb, locator, ...rest] = args;
|
|
64
64
|
if (!verbFlags[verb]) throw new JobError('JOB_REQUEST_INVALID');
|
|
65
65
|
const {url, job} = jobLocator(locator);
|
|
66
66
|
const values = flags(rest, verbFlags[verb]);
|
|
67
67
|
const base = { schema: JOB_API_SCHEMA, verb, jobId: job, requestId: values.get('request-id') ?? randomUUID() };
|
|
68
68
|
let payload: unknown;
|
|
69
|
+
let waitUntilActMs: number | undefined;
|
|
69
70
|
if (verb === 'next') {
|
|
70
|
-
|
|
71
|
+
let waitMs = Number(values.get('wait-ms') ?? '0');
|
|
72
|
+
if (values.has('until')) {
|
|
73
|
+
if (values.get('until') !== 'act' || !Number.isSafeInteger(waitMs) || waitMs < 1 || waitMs > 600_000) {
|
|
74
|
+
throw new JobError('JOB_REQUEST_INVALID');
|
|
75
|
+
}
|
|
76
|
+
waitUntilActMs = waitMs;
|
|
77
|
+
waitMs = Math.min(waitMs, jobWaitLimitMs);
|
|
78
|
+
}
|
|
79
|
+
payload = { ...base, after: values.get('after') ?? null, waitMs };
|
|
71
80
|
} else {
|
|
72
81
|
const mutation = { ...base, expectedGeneration: Number(required(values, 'generation')), idempotencyKey: required(values, 'key') };
|
|
73
82
|
switch (verb) {
|
|
@@ -89,7 +98,7 @@ export function parseJobCLI(args: readonly string[]): { readonly endpoint: strin
|
|
|
89
98
|
}
|
|
90
99
|
const request = parseJobRequest(payload);
|
|
91
100
|
if (request.verb === 'fetch-act') throw new JobError('JOB_AUTHORITY_REFUSED');
|
|
92
|
-
return { endpoint: `${url.origin}/jobs/${job}/api`, request };
|
|
101
|
+
return { endpoint: `${url.origin}/jobs/${job}/api`, request, ...(waitUntilActMs === undefined ? {} : {waitUntilActMs}) };
|
|
93
102
|
}
|
|
94
103
|
|
|
95
104
|
function jobLocator(locator: string): {url: URL; job: string} {
|
|
@@ -170,32 +179,46 @@ function outputConsumerAct(actBytes: Buffer, png: Buffer | undefined, act: JobAc
|
|
|
170
179
|
let temporaryIdentity: {dev: number; ino: number} | undefined;
|
|
171
180
|
let installed: string | undefined;
|
|
172
181
|
let installedIdentity: {dev: number; ino: number} | undefined;
|
|
182
|
+
let pngDescriptor: number | undefined;
|
|
173
183
|
let lock: string | undefined;
|
|
174
184
|
let lockDescriptor: number | undefined;
|
|
175
185
|
let emitted = 0;
|
|
176
186
|
try {
|
|
177
187
|
if (png && destination && act.schema === JOB_RIG_REVIEW_ACT_SCHEMA) {
|
|
178
188
|
const directory = destinationDirectory(destination, 'JOB_STILL_OUTPUT_FAILED', false);
|
|
179
|
-
|
|
180
|
-
lockDescriptor = openSync(lock, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY | constants.O_NOFOLLOW, 0o600);
|
|
181
|
-
const target = join(directory, act.still.path);
|
|
182
|
-
let existing = false;
|
|
189
|
+
const directoryDescriptor = openSync(directory, constants.O_RDONLY | (constants.O_DIRECTORY ?? 0) | constants.O_NOFOLLOW);
|
|
183
190
|
try {
|
|
184
|
-
const
|
|
191
|
+
const directoryIdentity = fstatSync(directoryDescriptor);
|
|
192
|
+
if (!directoryIdentity.isDirectory()) throw new JobError('JOB_STILL_OUTPUT_FAILED');
|
|
193
|
+
lock = join(directory, '.rig-review-still.lock');
|
|
185
194
|
try {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
195
|
+
lockDescriptor = openSync(lock, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY | constants.O_NOFOLLOW, 0o600);
|
|
196
|
+
} catch (error) {
|
|
197
|
+
if ((error as NodeJS.ErrnoException).code === 'EEXIST') {
|
|
198
|
+
throw new JobError('JOB_STILL_OUTPUT_FAILED',
|
|
199
|
+
`JOB_STILL_OUTPUT_FAILED: retained lock ${lock} already exists; wait for another writer to finish or remove ${lock} if a killed process left it`);
|
|
189
200
|
}
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
195
|
-
if (!existing) {
|
|
196
|
-
temporary = join(directory, '.rig-review-still-' + randomUUID() + '.tmp');
|
|
197
|
-
const descriptor = openSync(temporary, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY | constants.O_NOFOLLOW, 0o600);
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
const target = join(directory, act.still.path);
|
|
204
|
+
let existing = false;
|
|
198
205
|
try {
|
|
206
|
+
const descriptor = openSync(target, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
207
|
+
try {
|
|
208
|
+
const info = fstatSync(descriptor);
|
|
209
|
+
if (!info.isFile() || info.size > jobStillByteLimit || jobHash(readFileSync(descriptor)) !== act.still.sha256) {
|
|
210
|
+
throw new JobError('JOB_STILL_OUTPUT_FAILED');
|
|
211
|
+
}
|
|
212
|
+
existing = true;
|
|
213
|
+
} finally { closeSync(descriptor); }
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
|
|
216
|
+
}
|
|
217
|
+
if (!existing) {
|
|
218
|
+
temporary = join(directory, '.rig-review-still-' + randomUUID() + '.tmp');
|
|
219
|
+
const descriptor = openSync(temporary, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY | constants.O_NOFOLLOW, 0o600);
|
|
220
|
+
// Pin this inode through output and ownership cleanup, even if its path is replaced.
|
|
221
|
+
pngDescriptor = descriptor;
|
|
199
222
|
const owned = fstatSync(descriptor);
|
|
200
223
|
temporaryIdentity = {dev: owned.dev, ino: owned.ino};
|
|
201
224
|
let offset = 0;
|
|
@@ -207,11 +230,16 @@ function outputConsumerAct(actBytes: Buffer, png: Buffer | undefined, act: JobAc
|
|
|
207
230
|
fsyncSync(descriptor);
|
|
208
231
|
const info = fstatSync(descriptor);
|
|
209
232
|
installedIdentity = {dev: info.dev, ino: info.ino};
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
233
|
+
renameSync(temporary, target);
|
|
234
|
+
temporary = undefined;
|
|
235
|
+
installed = target;
|
|
236
|
+
}
|
|
237
|
+
const currentDirectory = lstatSync(directory);
|
|
238
|
+
if (currentDirectory.dev !== directoryIdentity.dev || currentDirectory.ino !== directoryIdentity.ino) {
|
|
239
|
+
throw new JobError('JOB_STILL_OUTPUT_FAILED');
|
|
240
|
+
}
|
|
241
|
+
fsyncSync(directoryDescriptor);
|
|
242
|
+
} finally { closeSync(directoryDescriptor); }
|
|
215
243
|
}
|
|
216
244
|
if (options.write && !options.writeBytes) {
|
|
217
245
|
// A legacy callback cannot report partial progress, so preserve an installed PNG on a thrown write.
|
|
@@ -225,17 +253,23 @@ function outputConsumerAct(actBytes: Buffer, png: Buffer | undefined, act: JobAc
|
|
|
225
253
|
emitted += written;
|
|
226
254
|
}
|
|
227
255
|
}
|
|
228
|
-
} catch {
|
|
256
|
+
} catch (error) {
|
|
229
257
|
if (emitted === 0 && installed && installedIdentity) {
|
|
230
258
|
unlinkOwned(installed, installedIdentity);
|
|
231
259
|
}
|
|
232
|
-
throw new JobError('JOB_STILL_OUTPUT_FAILED');
|
|
260
|
+
throw error instanceof JobError ? error : new JobError('JOB_STILL_OUTPUT_FAILED');
|
|
233
261
|
} finally {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
262
|
+
try {
|
|
263
|
+
try {
|
|
264
|
+
if (temporary && temporaryIdentity) unlinkOwned(temporary, temporaryIdentity);
|
|
265
|
+
} finally {
|
|
266
|
+
if (lockDescriptor !== undefined) {
|
|
267
|
+
try { unlinkOwned(lock!, fstatSync(lockDescriptor)); }
|
|
268
|
+
finally { closeSync(lockDescriptor); }
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
} finally {
|
|
272
|
+
if (pngDescriptor !== undefined) closeSync(pngDescriptor);
|
|
239
273
|
}
|
|
240
274
|
}
|
|
241
275
|
}
|
|
@@ -283,29 +317,14 @@ async function fetchActCommand(args: readonly string[], options: JobCLIOptions):
|
|
|
283
317
|
outputConsumerAct(bytes, png, act, destination, options);
|
|
284
318
|
}
|
|
285
319
|
|
|
286
|
-
|
|
287
|
-
if (args[0] === 'fetch-act') return fetchActCommand(args, options);
|
|
288
|
-
const write = options.write ?? (text => console.log(text));
|
|
289
|
-
if (args[0] === 'open') {
|
|
290
|
-
if (args.length !== 2 || !options.open) throw new JobError('JOB_AUTHORITY_REFUSED', 'Managed job opening requires the Review host');
|
|
291
|
-
write((await options.open(args[1])).ownerUrl);
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
294
|
-
const input = parseJobCLI(args);
|
|
295
|
-
const capability = options.agentCapability ?? process.env.RIG_REVIEW_JOB_AGENT_CAPABILITY;
|
|
296
|
-
if (!capability) throw new JobError('JOB_CAPABILITY_REQUIRED');
|
|
297
|
-
const response = await (options.fetch ?? fetch)(input.endpoint, {
|
|
298
|
-
method: 'POST', redirect: 'error', signal: options.signal,
|
|
299
|
-
headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + capability },
|
|
300
|
-
body: JSON.stringify(input.request),
|
|
301
|
-
});
|
|
320
|
+
async function readJobResponse(response: Response, interrupted?: Promise<never>): Promise<unknown> {
|
|
302
321
|
const reader = response.body?.getReader();
|
|
303
322
|
if (!reader) throw new JobError('JOB_REQUEST_INVALID');
|
|
304
323
|
const chunks: Uint8Array[] = [];
|
|
305
324
|
let size = 0;
|
|
306
325
|
try {
|
|
307
326
|
for (;;) {
|
|
308
|
-
const { done, value } = await reader.read();
|
|
327
|
+
const { done, value } = await (interrupted ? Promise.race([reader.read(), interrupted]) : reader.read());
|
|
309
328
|
if (done) break;
|
|
310
329
|
size += value.byteLength;
|
|
311
330
|
if (size > 1024 * 1024) {
|
|
@@ -314,6 +333,11 @@ export async function jobCommand(args: readonly string[], options: JobCLIOptions
|
|
|
314
333
|
}
|
|
315
334
|
chunks.push(value);
|
|
316
335
|
}
|
|
336
|
+
} catch (error) {
|
|
337
|
+
if (interrupted) {
|
|
338
|
+
try { void reader.cancel().catch(() => {}); } catch {}
|
|
339
|
+
}
|
|
340
|
+
throw error;
|
|
317
341
|
} finally {
|
|
318
342
|
reader.releaseLock();
|
|
319
343
|
}
|
|
@@ -321,6 +345,149 @@ export async function jobCommand(args: readonly string[], options: JobCLIOptions
|
|
|
321
345
|
try { value = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(Buffer.concat(chunks, size))); }
|
|
322
346
|
catch { throw new JobError('JOB_REQUEST_INVALID'); }
|
|
323
347
|
assertJobApiSchema(value !== null && typeof value === 'object' ? (value as { schema?: unknown }).schema : undefined);
|
|
348
|
+
return value;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** Check the complete existing next batch before accepting its read cursor. */
|
|
352
|
+
function nextValue(value: unknown, request: JobNextRequest): JobNextValue {
|
|
353
|
+
const result = jobObject(value, ['schema', 'verb', 'jobId', 'requestId', 'generation', 'ok', 'value']);
|
|
354
|
+
if (result.ok !== true || result.verb !== 'next' || result.jobId !== request.jobId) throw new JobError('JOB_REQUEST_INVALID');
|
|
355
|
+
jobInteger(result.generation);
|
|
356
|
+
const batch = jobObject(result.value, ['capabilities', 'events', 'cursor', 'status']);
|
|
357
|
+
if (!Array.isArray(batch.events) || !['events', 'timeout', 'closed'].includes(batch.status as string)) throw new JobError('JOB_REQUEST_INVALID');
|
|
358
|
+
if (batch.cursor !== null) jobId(batch.cursor);
|
|
359
|
+
for (const raw of batch.events) {
|
|
360
|
+
const event = jobObject(raw, ['cursor', 'jobId', 'generation', 'type', 'publication', 'ask', 'act', 'reason']);
|
|
361
|
+
jobId(event.cursor);
|
|
362
|
+
jobInteger(event.generation);
|
|
363
|
+
if (event.jobId !== request.jobId) throw new JobError('JOB_REQUEST_INVALID');
|
|
364
|
+
switch (event.type) {
|
|
365
|
+
case 'act': {
|
|
366
|
+
const actId = jobId((event.act as {actId?: unknown} | null)?.actId);
|
|
367
|
+
const act = parseConsumerAct(Buffer.from(JSON.stringify(event.act)), request.jobId, actId);
|
|
368
|
+
if (!(JOB_ENABLED_ACT_KINDS as readonly string[]).includes(act.kind)) throw new JobError('JOB_ACT_INVALID');
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
case 'published':
|
|
372
|
+
case 'pending': {
|
|
373
|
+
const payload = event.type === 'published' ? event.publication : event.ask;
|
|
374
|
+
if (payload === null || typeof payload !== 'object' || Array.isArray(payload)) throw new JobError('JOB_REQUEST_INVALID');
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
case 'closed':
|
|
378
|
+
if (typeof event.reason !== 'string') throw new JobError('JOB_REQUEST_INVALID');
|
|
379
|
+
break;
|
|
380
|
+
default: throw new JobError('JOB_REQUEST_INVALID');
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const last = batch.events.at(-1);
|
|
384
|
+
if (batch.cursor !== (last?.cursor ?? request.after) || (batch.status === 'events') !== (batch.events.length > 0)) {
|
|
385
|
+
throw new JobError('JOB_REQUEST_INVALID');
|
|
386
|
+
}
|
|
387
|
+
return batch as unknown as JobNextValue;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
async function waitForAct(endpoint: string, initial: JobNextRequest, waitMs: number, capability: string,
|
|
391
|
+
write: (text: string) => void, options: JobCLIOptions): Promise<void> {
|
|
392
|
+
const controller = new AbortController();
|
|
393
|
+
const timeout = new Error('JOB_NEXT_TIMEOUT');
|
|
394
|
+
const cancel = () => controller.abort(options.signal?.reason);
|
|
395
|
+
options.signal?.addEventListener('abort', cancel, {once: true});
|
|
396
|
+
if (options.signal?.aborted) cancel();
|
|
397
|
+
const deadline = performance.now() + waitMs;
|
|
398
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
399
|
+
const expire = () => {
|
|
400
|
+
if (controller.signal.aborted) return;
|
|
401
|
+
const budget = deadline - performance.now();
|
|
402
|
+
if (budget <= 0) controller.abort(timeout);
|
|
403
|
+
else timer = setTimeout(expire, Math.ceil(budget));
|
|
404
|
+
};
|
|
405
|
+
timer = setTimeout(expire, waitMs);
|
|
406
|
+
const interrupted = new Promise<never>((_resolve, reject) => {
|
|
407
|
+
controller.signal.addEventListener('abort', () => reject(controller.signal.reason), {once: true});
|
|
408
|
+
if (controller.signal.aborted) reject(controller.signal.reason);
|
|
409
|
+
});
|
|
410
|
+
// Pre-cancellation can reject before the first network race is installed.
|
|
411
|
+
void interrupted.catch(() => {});
|
|
412
|
+
function remaining(): number {
|
|
413
|
+
const budget = deadline - performance.now();
|
|
414
|
+
if (!controller.signal.aborted && budget <= 0) controller.abort(timeout);
|
|
415
|
+
controller.signal.throwIfAborted();
|
|
416
|
+
return budget;
|
|
417
|
+
}
|
|
418
|
+
let after = initial.after;
|
|
419
|
+
let lastResponse: unknown;
|
|
420
|
+
try {
|
|
421
|
+
for (;;) {
|
|
422
|
+
const budget = remaining();
|
|
423
|
+
const request = {...initial, after, waitMs: Math.max(0, Math.floor(Math.min(budget, jobWaitLimitMs)))};
|
|
424
|
+
// A delayed transport must also dispose its body if cancellation won the race.
|
|
425
|
+
const pending = (options.fetch ?? fetch)(endpoint, {
|
|
426
|
+
method: 'POST', redirect: 'error', signal: controller.signal,
|
|
427
|
+
headers: {'Content-Type': 'application/json', Authorization: 'Bearer ' + capability},
|
|
428
|
+
body: JSON.stringify(request),
|
|
429
|
+
}).then(response => {
|
|
430
|
+
if (controller.signal.aborted) {
|
|
431
|
+
try { void response.body?.cancel().catch(() => {}); } catch {}
|
|
432
|
+
controller.signal.throwIfAborted();
|
|
433
|
+
}
|
|
434
|
+
return response;
|
|
435
|
+
});
|
|
436
|
+
const response = await Promise.race([pending, interrupted]);
|
|
437
|
+
const value = await readJobResponse(response, interrupted);
|
|
438
|
+
remaining();
|
|
439
|
+
if (!response.ok || (value as {ok?: unknown}).ok === false) {
|
|
440
|
+
write(JSON.stringify(value));
|
|
441
|
+
process.exitCode = 1;
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const batch = nextValue(value, request);
|
|
445
|
+
remaining();
|
|
446
|
+
if (batch.events.some(event => event.type === 'act')) {
|
|
447
|
+
write(JSON.stringify(value));
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (batch.status === 'closed' || batch.events.some(event => event.type === 'closed')) {
|
|
451
|
+
write(JSON.stringify(value));
|
|
452
|
+
console.error('JOB_CLOSED');
|
|
453
|
+
process.exitCode = 1;
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
lastResponse = value;
|
|
457
|
+
after = batch.cursor;
|
|
458
|
+
}
|
|
459
|
+
} catch (error) {
|
|
460
|
+
if (!controller.signal.aborted && performance.now() >= deadline) controller.abort(timeout);
|
|
461
|
+
if (controller.signal.reason !== timeout) throw error;
|
|
462
|
+
if (lastResponse !== undefined) write(JSON.stringify(lastResponse));
|
|
463
|
+
console.error('JOB_NEXT_TIMEOUT');
|
|
464
|
+
process.exitCode = 3;
|
|
465
|
+
} finally {
|
|
466
|
+
clearTimeout(timer);
|
|
467
|
+
options.signal?.removeEventListener('abort', cancel);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export async function jobCommand(args: readonly string[], options: JobCLIOptions = {}): Promise<void> {
|
|
472
|
+
if (args[0] === 'fetch-act') return fetchActCommand(args, options);
|
|
473
|
+
const write = options.write ?? (text => console.log(text));
|
|
474
|
+
if (args[0] === 'open') {
|
|
475
|
+
if (args.length !== 2 || !options.open) throw new JobError('JOB_AUTHORITY_REFUSED', 'Managed job opening requires the Review host');
|
|
476
|
+
write((await options.open(args[1])).ownerUrl);
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
const input = parseJobCLI(args);
|
|
480
|
+
const capability = options.agentCapability ?? process.env.RIG_REVIEW_JOB_AGENT_CAPABILITY;
|
|
481
|
+
if (!capability) throw new JobError('JOB_CAPABILITY_REQUIRED');
|
|
482
|
+
if (input.waitUntilActMs !== undefined && input.request.verb === 'next') {
|
|
483
|
+
return waitForAct(input.endpoint, input.request, input.waitUntilActMs, capability, write, options);
|
|
484
|
+
}
|
|
485
|
+
const response = await (options.fetch ?? fetch)(input.endpoint, {
|
|
486
|
+
method: 'POST', redirect: 'error', signal: options.signal,
|
|
487
|
+
headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + capability },
|
|
488
|
+
body: JSON.stringify(input.request),
|
|
489
|
+
});
|
|
490
|
+
const value = await readJobResponse(response);
|
|
324
491
|
write(JSON.stringify(value));
|
|
325
492
|
if (!response.ok) process.exitCode = 1;
|
|
326
493
|
}
|
package/src/job-contract.ts
CHANGED
|
@@ -11,10 +11,21 @@
|
|
|
11
11
|
* Only explicit close ends an active job. Direction carriage is metadata-only.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
import type { JobPublicationReader } from './job-source.ts';
|
|
15
|
+
|
|
14
16
|
export const JOB_API_SCHEMA = 'rig-review/job-api@2' as const;
|
|
17
|
+
export const JOB_API_V3_SCHEMA = 'rig-review/job-api@3' as const;
|
|
15
18
|
export const JOB_ACT_SCHEMA = 'rig-review/act@2' as const;
|
|
16
19
|
/** New rig verdicts only. Store/reuse markers and all other acts retain @2. */
|
|
17
20
|
export const JOB_RIG_REVIEW_ACT_SCHEMA = 'rig-review/act@3' as const;
|
|
21
|
+
/** Record-source acts only under opt-in API@3. Package/image acts retain @2/@3. */
|
|
22
|
+
export const JOB_RECORD_ACT_SCHEMA = 'rig-review/act@4' as const;
|
|
23
|
+
export const JOB_RECORD_READER_ID = 'rig-create/publication-reader/2' as const;
|
|
24
|
+
export const JOB_RECORD_SOURCE_SCHEMA = 'rig-review-job-record-source/1' as const;
|
|
25
|
+
export const JOB_RECORD_INSTANCE_SCHEMA = 'rig-review-job-record-instance/1' as const;
|
|
26
|
+
export const JOB_PUBLICATION_MODES = ['package-only', 'record-and-package'] as const;
|
|
27
|
+
export type JobPublicationMode = typeof JOB_PUBLICATION_MODES[number];
|
|
28
|
+
export type JobApiSchema = typeof JOB_API_SCHEMA | typeof JOB_API_V3_SCHEMA;
|
|
18
29
|
export const jobStillByteLimit = 8 * 1024 * 1024;
|
|
19
30
|
export const jobCaptureBodyLimit = 12 * 1024 * 1024;
|
|
20
31
|
export const jobRawActMediaType = 'application/vnd.rig-review.act+json';
|
|
@@ -639,3 +650,291 @@ export interface JobConsumerGrant {
|
|
|
639
650
|
readonly actKinds: readonly JobEnabledActKind[];
|
|
640
651
|
readonly lifetime: 'until-explicit-close';
|
|
641
652
|
}
|
|
653
|
+
|
|
654
|
+
export interface RecordOptionResult {
|
|
655
|
+
readonly kind: 'option-set';
|
|
656
|
+
readonly questionId: string;
|
|
657
|
+
readonly setId: string;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
export interface RecordPlanResult {
|
|
661
|
+
readonly kind: 'plan';
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
export type RecordResult = RecordOptionResult | RecordPlanResult;
|
|
665
|
+
|
|
666
|
+
export interface PackageResultV3 {
|
|
667
|
+
readonly kind: 'package';
|
|
668
|
+
readonly path: string;
|
|
669
|
+
readonly selector: JobSelector | null;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
export type PublishResultV3 = PackageResultV3 | RecordResult;
|
|
673
|
+
|
|
674
|
+
export type ProjectRecord = JobFileIdentity & { readonly path: 'project.json' };
|
|
675
|
+
|
|
676
|
+
export type RecordFreeze = Omit<JobPublishRequest['freeze'], 'record'> & {
|
|
677
|
+
readonly record: ProjectRecord;
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
export type PublishRequestV3 = Omit<JobPublishRequest, 'schema' | 'result' | 'freeze'> & {
|
|
681
|
+
readonly schema: typeof JOB_API_V3_SCHEMA;
|
|
682
|
+
} & (
|
|
683
|
+
| {
|
|
684
|
+
readonly result: Extract<PublishResultV3, { readonly kind: 'package' }>;
|
|
685
|
+
readonly freeze: JobPublishRequest['freeze'];
|
|
686
|
+
}
|
|
687
|
+
| {
|
|
688
|
+
readonly result: RecordResult;
|
|
689
|
+
readonly freeze: RecordFreeze;
|
|
690
|
+
}
|
|
691
|
+
);
|
|
692
|
+
|
|
693
|
+
export type AskRequestV3 = Omit<JobAskRequest, 'schema'> & {
|
|
694
|
+
readonly schema: typeof JOB_API_V3_SCHEMA;
|
|
695
|
+
};
|
|
696
|
+
|
|
697
|
+
export type NextRequestV3 = Omit<JobNextRequest, 'schema'> & {
|
|
698
|
+
readonly schema: typeof JOB_API_V3_SCHEMA;
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
export type CloseRequestV3 = Omit<JobCloseRequest, 'schema'> & {
|
|
702
|
+
readonly schema: typeof JOB_API_V3_SCHEMA;
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
export type AgentRequestV3 =
|
|
706
|
+
| PublishRequestV3
|
|
707
|
+
| AskRequestV3
|
|
708
|
+
| NextRequestV3
|
|
709
|
+
| CloseRequestV3;
|
|
710
|
+
|
|
711
|
+
export type FetchActRequestV3 = Omit<JobFetchActRequest, 'schema'> & {
|
|
712
|
+
readonly schema: typeof JOB_API_V3_SCHEMA;
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
export interface RecordPicksSubject {
|
|
716
|
+
readonly kind: 'interview-picks';
|
|
717
|
+
readonly questionId: string;
|
|
718
|
+
readonly setId: string;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
export interface RecordCheckpointSubject {
|
|
722
|
+
readonly kind: 'checkpoint';
|
|
723
|
+
readonly checkpointId: string;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
export type RecordSignoffStage = typeof JOB_PLAN_BOUND_SIGNOFF_STAGES[number];
|
|
727
|
+
|
|
728
|
+
export interface RecordSignoffSubject {
|
|
729
|
+
readonly kind: 'signoff';
|
|
730
|
+
readonly stage: RecordSignoffStage;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
export type RecordPlanSubject = RecordCheckpointSubject | RecordSignoffSubject;
|
|
734
|
+
export type RecordSubject = RecordPicksSubject | RecordPlanSubject;
|
|
735
|
+
|
|
736
|
+
export interface RecordPartsIdentity {
|
|
737
|
+
readonly name: string;
|
|
738
|
+
readonly version: string;
|
|
739
|
+
readonly integrity: string;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
export interface RecordContextBase {
|
|
743
|
+
readonly label: string;
|
|
744
|
+
readonly parts: RecordPartsIdentity;
|
|
745
|
+
readonly inputs: readonly JobFileIdentity[];
|
|
746
|
+
readonly files: readonly JobFileIdentity[];
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
export type OptionContext = RecordContextBase & {
|
|
750
|
+
readonly plan: null;
|
|
751
|
+
readonly subjects: readonly [RecordPicksSubject];
|
|
752
|
+
};
|
|
753
|
+
|
|
754
|
+
export type PlanContext = RecordContextBase & {
|
|
755
|
+
readonly plan: JobPlanIdentity;
|
|
756
|
+
readonly subjects: readonly RecordPlanSubject[];
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
export type RecordPublishedContext = OptionContext | PlanContext;
|
|
760
|
+
|
|
761
|
+
export interface RecordReaderInput<Result extends RecordResult = RecordResult> {
|
|
762
|
+
readonly root: string;
|
|
763
|
+
readonly record: ProjectRecord;
|
|
764
|
+
readonly result: Result;
|
|
765
|
+
readonly phase: 'inventory' | 'retained';
|
|
766
|
+
readonly parts?: RecordPartsIdentity;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
export type ReaderValue<Value> = Value | Promise<Value>;
|
|
770
|
+
|
|
771
|
+
export interface RecordPublicationReader {
|
|
772
|
+
(input: RecordReaderInput<RecordOptionResult>): ReaderValue<OptionContext>;
|
|
773
|
+
(input: RecordReaderInput<RecordPlanResult>): ReaderValue<PlanContext>;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
export interface RecordHostHooks {
|
|
777
|
+
readonly publicationMode: 'record-and-package';
|
|
778
|
+
readonly readerId: typeof JOB_RECORD_READER_ID;
|
|
779
|
+
readonly readPublished: JobPublicationReader;
|
|
780
|
+
readonly readRecordPublished: RecordPublicationReader;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
export interface RecordSourceBase {
|
|
784
|
+
readonly sourceHash: JobSha256;
|
|
785
|
+
readonly projectHash: JobSha256;
|
|
786
|
+
readonly record: ProjectRecord;
|
|
787
|
+
readonly inputs: readonly JobFileIdentity[];
|
|
788
|
+
readonly files: readonly JobFileIdentity[];
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
export type OptionSource = RecordSourceBase & {
|
|
792
|
+
readonly subject: 'option-set';
|
|
793
|
+
readonly selection: RecordOptionResult;
|
|
794
|
+
readonly optionSet: { readonly id: string; readonly sha256: JobSha256 };
|
|
795
|
+
readonly optionSetFile: JobFileIdentity;
|
|
796
|
+
readonly plan: null;
|
|
797
|
+
readonly subjects: readonly [RecordPicksSubject];
|
|
798
|
+
};
|
|
799
|
+
|
|
800
|
+
export type PlanSource = RecordSourceBase & {
|
|
801
|
+
readonly subject: 'plan';
|
|
802
|
+
readonly selection: RecordPlanResult;
|
|
803
|
+
readonly optionSet: null;
|
|
804
|
+
readonly optionSetFile: null;
|
|
805
|
+
readonly plan: JobPlanIdentity;
|
|
806
|
+
readonly subjects: readonly RecordPlanSubject[];
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
export type RecordSource = OptionSource | PlanSource;
|
|
810
|
+
|
|
811
|
+
export type RecordSignoffValue = Omit<JobActValues['signoff'], 'stage'> & {
|
|
812
|
+
readonly stage: RecordSignoffStage;
|
|
813
|
+
};
|
|
814
|
+
|
|
815
|
+
export type RecordAct = Pick<JobLegacyAct,
|
|
816
|
+
'actId' | 'jobId' | 'askId' | 'publicationId' | 'authority' | 'at'> & {
|
|
817
|
+
readonly schema: typeof JOB_RECORD_ACT_SCHEMA;
|
|
818
|
+
} & (
|
|
819
|
+
| {
|
|
820
|
+
readonly kind: 'interview-picks';
|
|
821
|
+
readonly source: OptionSource;
|
|
822
|
+
readonly value: JobActValues['interview-picks'];
|
|
823
|
+
}
|
|
824
|
+
| {
|
|
825
|
+
readonly kind: 'checkpoint';
|
|
826
|
+
readonly source: PlanSource;
|
|
827
|
+
readonly value: JobActValues['checkpoint'];
|
|
828
|
+
}
|
|
829
|
+
| {
|
|
830
|
+
readonly kind: 'signoff';
|
|
831
|
+
readonly source: PlanSource;
|
|
832
|
+
readonly value: RecordSignoffValue;
|
|
833
|
+
}
|
|
834
|
+
);
|
|
835
|
+
|
|
836
|
+
export type SourceV3 = JobSourceBinding | RecordSource;
|
|
837
|
+
export type ActV3 = JobAct | RecordAct;
|
|
838
|
+
|
|
839
|
+
export interface JobCapabilitiesV3 extends JobCapabilities {
|
|
840
|
+
readonly recordPublication: true;
|
|
841
|
+
readonly recordActSchema: typeof JOB_RECORD_ACT_SCHEMA;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
export const JOB_CAPABILITIES_V3: JobCapabilitiesV3 = Object.freeze({
|
|
845
|
+
imageSetVerdict: true,
|
|
846
|
+
recordPublication: true,
|
|
847
|
+
recordActSchema: JOB_RECORD_ACT_SCHEMA,
|
|
848
|
+
});
|
|
849
|
+
|
|
850
|
+
export interface PendingAskV3 extends Omit<JobPendingAsk, 'source'> {
|
|
851
|
+
readonly source: SourceV3;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
export interface PublicationV3 extends Omit<JobPublication, 'source'> {
|
|
855
|
+
readonly source: SourceV3;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
export interface StatusV3 extends Omit<JobStatus, 'publication' | 'pending'> {
|
|
859
|
+
readonly publication: PublicationV3 | null;
|
|
860
|
+
readonly pending: readonly PendingAskV3[];
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
export type EventV3 = {
|
|
864
|
+
readonly cursor: JobCursor;
|
|
865
|
+
readonly jobId: JobId;
|
|
866
|
+
readonly generation: number;
|
|
867
|
+
} & (
|
|
868
|
+
| { readonly type: 'published'; readonly publication: PublicationV3 }
|
|
869
|
+
| { readonly type: 'pending'; readonly ask: PendingAskV3 }
|
|
870
|
+
| { readonly type: 'act'; readonly act: ActV3 }
|
|
871
|
+
| { readonly type: 'closed'; readonly reason: string }
|
|
872
|
+
);
|
|
873
|
+
|
|
874
|
+
export interface NextValueV3 {
|
|
875
|
+
readonly capabilities?: JobCapabilitiesV3;
|
|
876
|
+
readonly events: readonly EventV3[];
|
|
877
|
+
readonly cursor: JobCursor | null;
|
|
878
|
+
readonly status: 'events' | 'timeout' | 'closed';
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
export interface ResponseValuesV3 {
|
|
882
|
+
readonly publish: PublicationV3;
|
|
883
|
+
readonly ask: PendingAskV3;
|
|
884
|
+
readonly next: NextValueV3;
|
|
885
|
+
readonly close: { readonly status: 'closed'; readonly cursor: JobCursor | null };
|
|
886
|
+
readonly 'fetch-act': ActV3;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
export type ApiResponseV3 = {
|
|
890
|
+
[Verb in keyof ResponseValuesV3]: {
|
|
891
|
+
readonly schema: typeof JOB_API_V3_SCHEMA;
|
|
892
|
+
readonly verb: Verb;
|
|
893
|
+
readonly jobId: JobId;
|
|
894
|
+
readonly requestId: string;
|
|
895
|
+
readonly generation: number;
|
|
896
|
+
} & JobResult<ResponseValuesV3[Verb]>;
|
|
897
|
+
}[keyof ResponseValuesV3];
|
|
898
|
+
|
|
899
|
+
export type JobRecordOptionResult = RecordOptionResult;
|
|
900
|
+
export type JobRecordPlanResult = RecordPlanResult;
|
|
901
|
+
export type JobRecordResult = RecordResult;
|
|
902
|
+
export type JobPublishResultV3 = PublishResultV3;
|
|
903
|
+
export type JobProjectRecord = ProjectRecord;
|
|
904
|
+
export type JobRecordFreeze = RecordFreeze;
|
|
905
|
+
export type JobPublishRequestV3 = PublishRequestV3;
|
|
906
|
+
export type JobAskRequestV3 = AskRequestV3;
|
|
907
|
+
export type JobNextRequestV3 = NextRequestV3;
|
|
908
|
+
export type JobCloseRequestV3 = CloseRequestV3;
|
|
909
|
+
export type JobAgentRequestV3 = AgentRequestV3;
|
|
910
|
+
export type JobFetchActRequestV3 = FetchActRequestV3;
|
|
911
|
+
export type JobRecordPicksSubject = RecordPicksSubject;
|
|
912
|
+
export type JobRecordCheckpointSubject = RecordCheckpointSubject;
|
|
913
|
+
export type JobRecordSignoffStage = RecordSignoffStage;
|
|
914
|
+
export type JobRecordSignoffSubject = RecordSignoffSubject;
|
|
915
|
+
export type JobRecordPlanSubject = RecordPlanSubject;
|
|
916
|
+
export type JobRecordSubject = RecordSubject;
|
|
917
|
+
export type JobRecordPartsIdentity = RecordPartsIdentity;
|
|
918
|
+
export type JobRecordContextBase = RecordContextBase;
|
|
919
|
+
export type JobOptionContext = OptionContext;
|
|
920
|
+
export type JobPlanContext = PlanContext;
|
|
921
|
+
export type JobRecordPublishedContext = RecordPublishedContext;
|
|
922
|
+
export type JobRecordReaderInput<Result extends RecordResult = RecordResult> = RecordReaderInput<Result>;
|
|
923
|
+
export type JobRecordPublicationReader = RecordPublicationReader;
|
|
924
|
+
export type JobRecordHostHooks = RecordHostHooks;
|
|
925
|
+
export type JobRecordSourceBase = RecordSourceBase;
|
|
926
|
+
export type JobOptionSource = OptionSource;
|
|
927
|
+
export type JobPlanSource = PlanSource;
|
|
928
|
+
export type JobRecordSource = RecordSource;
|
|
929
|
+
export type JobRecordSignoffValue = RecordSignoffValue;
|
|
930
|
+
export type JobRecordAct = RecordAct;
|
|
931
|
+
export type JobSourceV3 = SourceV3;
|
|
932
|
+
export type JobActV3 = ActV3;
|
|
933
|
+
export type JobPendingAskV3 = PendingAskV3;
|
|
934
|
+
export type JobPublicationV3 = PublicationV3;
|
|
935
|
+
export type JobStatusV3 = StatusV3;
|
|
936
|
+
export type JobEventV3 = EventV3;
|
|
937
|
+
export type JobNextValueV3 = NextValueV3;
|
|
938
|
+
export type JobResponseValuesV3 = ResponseValuesV3;
|
|
939
|
+
export type JobApiResponseV3 = ApiResponseV3;
|
|
940
|
+
|