@rig-c/review 2.6.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/src/job-host.ts CHANGED
@@ -2,7 +2,15 @@
2
2
  export {startJobHost} from './job-front.ts';
3
3
  export {startJobHub} from './job-hub.ts';
4
4
  export type {JobHubOptions, JobHubJobOptions, JobHubJob, JobOwnerHub} from './job-hub.ts';
5
- export type {JobHostOptions, JobOwnerHost} from './job-front.ts';
5
+ export type {
6
+ JobHostOptions,
7
+ JobOwnerHost,
8
+ JobHostListenerIncarnation,
9
+ JobHostReuseBinding,
10
+ JobHostReuseExpectation,
11
+ JobHostAttachField,
12
+ JobHostAttachFailure,
13
+ } from './job-front.ts';
6
14
  export type {JobSessionOptions, JobSession} from './job-session.ts';
7
15
  export type {JobWorkerOptions} from './job-worker.ts';
8
16
  export type {JobPublicationReader, JobPublishedContext} from './job-source.ts';
package/src/job-hub.ts CHANGED
@@ -6,7 +6,7 @@ import {buildWeb} from './build.ts';
6
6
  import {jobBody, jobObject, sameCapability} from './job-api.ts';
7
7
  import {JOB_API_SCHEMA, JOB_ACT_SCHEMA, JOB_CAPABILITIES, type JobCapabilities, type JobHostPorts, type JobReviewerBinding} from './job-contract.ts';
8
8
  import {assertJobHostCompatible, createJobRequestContext, installJobWriterBinding, jobHostIdentity,
9
- jobWindowResources, type JobHostOptions, type JobHostRecord} from './job-front.ts';
9
+ jobWindowResources, type JobHostOptions, type JobHostReuseExpectation, type JobHostRecord} from './job-front.ts';
10
10
  import {JobSession} from './job-session.ts';
11
11
  import {captureJobRoot} from './job-source.ts';
12
12
  import {JobError} from './job-store.ts';
@@ -19,13 +19,14 @@ export interface JobHubOptions {
19
19
  readonly reviewer: JobReviewerBinding;
20
20
  readonly port?: number;
21
21
  }
22
- export type JobHubJobOptions = Omit<JobHostOptions, 'stateDir' | 'port' | 'reviewer'>;
22
+ export type JobHubJobOptions = Omit<JobHostOptions, 'stateDir' | 'port' | 'reviewer' | 'reuseOnly'>;
23
23
  export interface JobHubJob {
24
24
  readonly session: JobSession;
25
25
  readonly ports: JobHostPorts;
26
26
  readonly origin: string;
27
27
  readonly jobUrl: string;
28
28
  readonly reused: boolean;
29
+ readonly attachment: JobHostReuseExpectation;
29
30
  }
30
31
  export interface JobOwnerHub {
31
32
  readonly origin: string;
@@ -217,6 +218,9 @@ export async function startJobHub(options: JobHubOptions): Promise<JobOwnerHub>
217
218
  await writePrivate(recordPath, record);
218
219
  async function register(input: JobHubJobOptions): Promise<JobHubJob> {
219
220
  if (closing) throw new HubError('JOB_HUB_CLOSED');
221
+ if (!input || typeof input !== 'object' || 'reuseOnly' in (input as Record<string, unknown>)) {
222
+ throw new JobError('JOB_REQUEST_INVALID');
223
+ }
220
224
  const startup: JobHostOptions = {...input, stateDir, reviewer};
221
225
  const identity = jobHostIdentity(startup);
222
226
  const root = captureJobRoot(startup.root).path;
@@ -235,7 +239,7 @@ export async function startJobHub(options: JobHubOptions): Promise<JobOwnerHub>
235
239
  assertJobHostCompatible(existing.context.record, identity);
236
240
  await installJobWriterBinding(existing.session, startup, origin);
237
241
  return {session: existing.session, ports: existing.context.ports, origin,
238
- jobUrl: origin + `/jobs/${session.jobId}/#cap=${existing.session.hostCredentials().ui}`, reused: true};
242
+ jobUrl: origin + `/jobs/${session.jobId}/#cap=${existing.session.hostCredentials().ui}`, reused: true, attachment: existing.context.ownerAttachment()};
239
243
  }
240
244
  if (members.size >= maximumJobs) throw new HubError('JOB_HUB_LIMIT');
241
245
  const path = join(control, session.jobId + '.json');
@@ -265,16 +269,17 @@ export async function startJobHub(options: JobHubOptions): Promise<JobOwnerHub>
265
269
  if (!('claim' in acquiredJob)) throw new HubError('JOB_HUB_EXTERNAL_HOST');
266
270
  claim = acquiredJob.claim;
267
271
  await refuseExternal();
268
- const context = createJobRequestContext(session, startup, origin, identity, resources, incarnation);
272
+ const context = createJobRequestContext(session, startup, origin, identity, resources, incarnation, incarnation, () => !closing && !!member?.ready);
269
273
  Object.assign(context.record, {hubIncarnation: incarnation});
270
274
  member = {root, fingerprint: startup.fingerprint, session, context, recordPath: path, ready: false};
271
275
  members.set(session.jobId, member);
272
276
  await installJobWriterBinding(session, startup, origin);
273
277
  if (closing) throw new HubError('JOB_HUB_CLOSED');
274
- await writePrivate(path, context.record);
278
+ await writePrivate(path, context.descriptor().record);
279
+ context.publish();
275
280
  member.ready = true; retained = true;
276
281
  return {session, ports: context.ports, origin,
277
- jobUrl: origin + `/jobs/${session.jobId}/#cap=${session.hostCredentials().ui}`, reused: false};
282
+ jobUrl: origin + `/jobs/${session.jobId}/#cap=${session.hostCredentials().ui}`, reused: false, attachment: context.ownerAttachment()};
278
283
  } finally {
279
284
  if (!retained) {
280
285
  if (member) {
@@ -78,8 +78,16 @@ export class JobSession {
78
78
  private readonly listeners = new Set<() => void>();
79
79
  private operations = 0;
80
80
  private disposed = false;
81
+ private observedGeneration: number;
81
82
 
82
- constructor(private readonly options: JobSessionOptions) {
83
+ /** Last durable generation successfully read by this session. Requires no I/O. */
84
+ get lastObservedGeneration(): number { return this.observedGeneration; }
85
+
86
+ static openReadOnly(options: JobSessionOptions, jobId: string): JobSession {
87
+ return new JobSession(options, { jobId });
88
+ }
89
+
90
+ constructor(private readonly options: JobSessionOptions, readonlyTarget?: { readonly jobId: string }) {
83
91
  const root = captureJobRoot(options.root), state = canonicalStatePath(options.stateDir);
84
92
  if (within(root.path, state)) {
85
93
  throw new JobError('JOB_SOURCE_UNRETAINABLE', 'Job state must be outside the designated work root');
@@ -89,21 +97,41 @@ export class JobSession {
89
97
  }
90
98
  if (!options.reviewer.id || !['owner', 'delegate'].includes(options.reviewer.role)) throw new JobError('JOB_AUTHORITY_REFUSED');
91
99
  if (options.partsEnumerator) jobPartsEnumeratorVersion(options.partsEnumerator.version);
92
- this.store = new JobStore(state);
100
+ const rootKey = jobHash(JSON.stringify([root.path, options.fingerprint]));
101
+ this.store = new JobStore(state, readonlyTarget ? { readonly: true } : {});
93
102
  try {
94
- const row = this.store.open(jobHash(JSON.stringify([root.path, options.fingerprint])), () => ({
95
- apiSchema: JOB_API_SCHEMA, actSchema: JOB_ACT_SCHEMA,
96
- root, fingerprint: options.fingerprint, reviewer: structuredClone(options.reviewer),
97
- agentCapability: randomBytes(32).toString('hex'), uiCapability: randomBytes(32).toString('hex'),
98
- fetchCapability: randomBytes(32).toString('hex'), generation: 0, head: null,
99
- publications: {}, asks: {}, acts: {}, events: [], replays: {}, pages: {},
100
- }), root.path);
103
+ const row = readonlyTarget
104
+ ? this.store.get(readonlyTarget.jobId)
105
+ : this.store.open(rootKey, () => ({
106
+ apiSchema: JOB_API_SCHEMA, actSchema: JOB_ACT_SCHEMA,
107
+ root, fingerprint: options.fingerprint, reviewer: structuredClone(options.reviewer),
108
+ agentCapability: randomBytes(32).toString('hex'), uiCapability: randomBytes(32).toString('hex'),
109
+ fetchCapability: randomBytes(32).toString('hex'), generation: 0, head: null,
110
+ publications: {}, asks: {}, acts: {}, events: [], replays: {}, pages: {},
111
+ }), root.path);
101
112
  this.jobId = row.jobId;
102
113
  assertStoredSchemas(row.data);
114
+ this.observedGeneration = row.closure?.generation ?? row.data.generation;
103
115
  assertJobRoot(row.data.root);
116
+ if (row.rootKey !== rootKey || row.data.root.path !== root.path || row.data.fingerprint !== options.fingerprint) {
117
+ throw new JobError('JOB_SOURCE_MISMATCH');
118
+ }
104
119
  if (requestDigest(row.data.reviewer) !== requestDigest(options.reviewer)) {
105
120
  throw new JobError('JOB_AUTHORITY_REFUSED', 'Active window reviewer binding differs');
106
121
  }
122
+ if (readonlyTarget && !row.active) throw new JobError('JOB_CLOSED');
123
+ if (readonlyTarget) {
124
+ const identity = (item: StoredJob<JobData>) => JSON.stringify([item.jobId, item.rootKey,
125
+ item.data.root, item.data.fingerprint, item.data.reviewer, item.data.apiSchema,
126
+ item.data.actSchema, item.data.agentCapability, item.data.uiCapability]);
127
+ const captured = identity(row);
128
+ this.store.allowExistingWrites(() => {
129
+ const current = this.store.get(this.jobId);
130
+ if (identity(current) !== captured) throw new JobError('JOB_STORAGE_FAILED');
131
+ assertJobRoot(current.data.root);
132
+ });
133
+ }
134
+
107
135
  } catch (error) {
108
136
  this.store.close();
109
137
  throw error;
@@ -127,6 +155,7 @@ export class JobSession {
127
155
  row.data.events.push(row.closure.event);
128
156
  row.data.replays[row.closure.replayKey] = row.closure.replay;
129
157
  }
158
+ this.observedGeneration = row.data.generation;
130
159
  return row;
131
160
  }
132
161
 
@@ -166,6 +195,36 @@ export class JobSession {
166
195
  };
167
196
  }
168
197
 
198
+ /** Internal synchronous owner acceptance snapshot. No external hooks run here. */
199
+ inspectAttach<Value>(inspect: () => Value): Value {
200
+ return this.store.inspect(() => { this.store.assertStorage(); return inspect(); });
201
+ }
202
+
203
+ attestation(): {
204
+ readonly jobId: string;
205
+ readonly status: 'active' | 'closed';
206
+ readonly revision: number;
207
+ readonly root: JobRootIdentity;
208
+ readonly stateDir: string;
209
+ readonly fingerprint: string;
210
+ readonly reviewer: JobReviewerBinding;
211
+ readonly apiSchema: typeof JOB_API_SCHEMA;
212
+ readonly actSchema: typeof JOB_ACT_SCHEMA;
213
+ } {
214
+ const current = this.current();
215
+ return {
216
+ jobId: this.jobId,
217
+ status: current.active ? 'active' : 'closed',
218
+ revision: current.revision,
219
+ root: structuredClone(current.data.root),
220
+ stateDir: this.store.root,
221
+ fingerprint: current.data.fingerprint,
222
+ reviewer: structuredClone(current.data.reviewer),
223
+ apiSchema: current.data.apiSchema,
224
+ actSchema: current.data.actSchema,
225
+ };
226
+ }
227
+
169
228
  retainedSource(publicationId: string): JobRetainedSource {
170
229
  const current = this.current();
171
230
  if (!current.active) throw new JobError('JOB_CLOSED');