@gate-forge/witness 0.18.1 → 0.18.3

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.
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/witness/server.ts"],"names":[],"mappings":"AA0JA,OAAO,EAYL,UAAU,EASX,MAAM,kBAAkB,CAAC;AAwC1B,OAAO,KAAK,EAmDV,aAAa,EACb,cAAc,EACf,MAAM,YAAY,CAAC;AAmMpB,uDAAuD;AACvD,qBAAa,mBAAoB,SAAQ,KAAK;gBAChC,OAAO,EAAE,MAAM;CAI5B;AAojCD;;;;;;;;GAQG;AACH,OAAO,EAAE,UAAU,EAAE,CAAC;AA2FtB;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC,CA+OlF;AAwsKD,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/witness/server.ts"],"names":[],"mappings":"AA0JA,OAAO,EAYL,UAAU,EASX,MAAM,kBAAkB,CAAC;AAwC1B,OAAO,KAAK,EAmDV,aAAa,EACb,cAAc,EACf,MAAM,YAAY,CAAC;AAmMpB,uDAAuD;AACvD,qBAAa,mBAAoB,SAAQ,KAAK;gBAChC,OAAO,EAAE,MAAM;CAI5B;AAojCD;;;;;;;;GAQG;AACH,OAAO,EAAE,UAAU,EAAE,CAAC;AA2FtB;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC,CA+OlF;AA41KD,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC"}
@@ -3124,15 +3124,52 @@ async function handleSessionOpen(state, res, body) {
3124
3124
  const open = state.sessions.get(existingWorkerSession);
3125
3125
  if (open !== undefined && open.testId === testId) {
3126
3126
  // Idempotent re-open of the identical (worker, testId) session.
3127
+ // While the first open's before-snapshots are still running, the
3128
+ // re-open awaits the SAME readiness instead of minting a second
3129
+ // session + proxy (the drain's one retry on a transport failure
3130
+ // — including the client timeout — depends on this staying a
3131
+ // single session): one open per (worker, testId), one snapshot
3132
+ // pass, no orphaned binding.
3133
+ if (open.snapshotReady !== null) {
3134
+ await open.snapshotReady;
3135
+ // The gate also settles when the session DIED unregistered (its
3136
+ // proxy allocation failed, or a different test claimed this
3137
+ // worker mid-round): re-check before answering and mirror the
3138
+ // original open's failure — a 200 session view here would hand
3139
+ // out a credential that can never work (plan P4 finish item 1).
3140
+ if (state.sessions.get(open.sessionId) !== open) {
3141
+ throw new HttpError(500, `session open failed: ${open.openFailure ?? 'the session was unregistered while its before-snapshots were still running'}`);
3142
+ }
3143
+ }
3127
3144
  sendJson(res, 200, sessionView(state, open));
3128
3145
  return;
3129
3146
  }
3130
- sendJson(res, 409, {
3131
- error: `worker ${String(workerIndex)} already carries an open session for testId ` +
3132
- `'${open === undefined ? existingWorkerSession : open.testId}'; a worker runs one test ` +
3133
- 'at a time — close the session before opening another',
3134
- });
3135
- return;
3147
+ if (open !== undefined && open.snapshotReady !== null) {
3148
+ // The worker's slot carries a PENDING session for a DIFFERENT
3149
+ // test. The supervisor opens serially per worker, so THIS open is
3150
+ // the lifecycle signal that the previous test ended — its open
3151
+ // answer died with the transport that abandoned it (the 5 s
3152
+ // client timeout included). Unbind the abandoned session so this
3153
+ // worker is never 409-refused by a response nobody can read
3154
+ // (plan P4 rework, third gate); its snapshot round still finishes
3155
+ // bounded and then discards itself — the completion path answers
3156
+ // the recorded failure, never 200.
3157
+ open.openFailure =
3158
+ "the worker moved to a different test before this session's before-snapshots finished";
3159
+ state.observeSnapshots.delete(open.sessionId);
3160
+ state.sessions.delete(open.sessionId);
3161
+ if (state.workerSessions.get(workerIndex) === open.sessionId) {
3162
+ state.workerSessions.delete(workerIndex);
3163
+ }
3164
+ }
3165
+ else {
3166
+ sendJson(res, 409, {
3167
+ error: `worker ${String(workerIndex)} already carries an open session for testId ` +
3168
+ `'${open === undefined ? existingWorkerSession : open.testId}'; a worker runs one test ` +
3169
+ 'at a time — close the session before opening another',
3170
+ });
3171
+ return;
3172
+ }
3136
3173
  }
3137
3174
  // Phase 4 claim injection: the supervisor carries the mapped obligation
3138
3175
  // claims for this test on the session-open path. Claims stay
@@ -3170,15 +3207,33 @@ async function handleSessionOpen(state, res, body) {
3170
3207
  // is being graded, not compared.
3171
3208
  const observationOnly = claims.length === 0 && (registered?.observationOnly ?? false);
3172
3209
  const sessionId = randomUUID();
3210
+ // Readiness gate (plan 20261021_0030_gf_infra_visibility P4): the
3211
+ // deferred settles when the session's before-snapshots are stored —
3212
+ // or finalized as error snapshots; `takeObserveSnapshots` turns a
3213
+ // failing resource into an error snapshot instead of throwing, so the
3214
+ // gate always opens. The session registers under its (worker, testId)
3215
+ // identity IMMEDIATELY (idempotency for the drain's re-open), while
3216
+ // /sessions/resolve refuses it until this promise resolves. Executor
3217
+ // form: this package's lib predates Promise.withResolvers.
3218
+ let resolveSnapshotReady;
3219
+ const snapshotReady = new Promise((resolveSnapshotReadyOnce) => {
3220
+ resolveSnapshotReady = resolveSnapshotReadyOnce;
3221
+ });
3173
3222
  const session = {
3174
3223
  sessionId,
3175
3224
  token: randomUUID(),
3176
3225
  testId,
3177
3226
  workerIndex,
3178
3227
  status: 'open',
3228
+ snapshotReady,
3179
3229
  openedTick: (state.tick += 1),
3180
3230
  sealedTick: null,
3181
3231
  outcome: null,
3232
+ // Why the session died UNREGISTERED (plan P4 finish): the proxy
3233
+ // allocation failure message, or the abandonment reason when a
3234
+ // different test claimed the worker mid-round. Null while alive; a
3235
+ // re-open that waited on the gate answers with exactly this.
3236
+ openFailure: null,
3182
3237
  intervals: new Map(),
3183
3238
  // Filled below: the session's DEDICATED observation-proxy ports, one
3184
3239
  // per attested app origin this run declares.
@@ -3210,22 +3265,67 @@ async function handleSessionOpen(state, res, body) {
3210
3265
  observationOnly,
3211
3266
  twinShapes: [],
3212
3267
  };
3213
- // Session attribution channel (plan Phase 1): a dedicated loopback
3214
- // proxy port whose traffic is attributed to THIS session. Exists only
3215
- // when an observation proxy is wired; otherwise nothing browser-side
3216
- // is attributable.
3217
- await startSessionProxy(state, session);
3268
+ // Registration happens IMMEDIATELY (plan 20261021_0030_gf_infra_
3269
+ // visibility P4 rework): an identical (worker, testId) re-open must
3270
+ // find this session and await its readiness — minting a second
3271
+ // session + proxy for the drain's one retry orphans the first binding
3272
+ // and can 409 the worker's next test. The session is still NOT
3273
+ // hand-out-able until its before-state exists: /sessions/resolve
3274
+ // refuses a not-ready session exactly like an unknown one (404), so a
3275
+ // fixture can never act against a half-open session.
3218
3276
  state.sessions.set(sessionId, session);
3219
3277
  state.workerSessions.set(workerIndex, sessionId);
3220
- // Observe before-snapshots (Phase 2): the witness lists the
3221
- // observe-declared resources ITSELF at open. Total by construction —
3222
- // a snapshot failure is finalize data, never an open failure.
3223
3278
  try {
3279
+ // Session attribution channel (plan Phase 1): a dedicated loopback
3280
+ // proxy port whose traffic is attributed to THIS session. Exists
3281
+ // only when an observation proxy is wired; otherwise nothing
3282
+ // browser-side is attributable.
3283
+ await startSessionProxy(state, session);
3284
+ // Observe before-snapshots (Phase 2): the witness lists the
3285
+ // observe-declared resources ITSELF at open. Total by construction —
3286
+ // a snapshot failure is finalize data, never an open failure.
3224
3287
  await takeObserveSnapshots(state, session);
3225
3288
  }
3226
- catch {
3289
+ catch (error) {
3290
+ // The session never became usable (e.g. the proxy port could not be
3291
+ // allocated): undo the registration so the worker is not blocked by
3292
+ // a session that no open can ever return, and fail the request as
3293
+ // before. Snapshots themselves never throw (a failing resource
3294
+ // stores its error snapshot), so this is proxy-allocation failure
3295
+ // surface only. The readiness gate settles RESOLVED — never
3296
+ // rejected: an unobserved rejection is an unhandledRejection — so a
3297
+ // concurrent re-open awaiting it wakes into the unregistered
3298
+ // re-check below and answers this same failure instead of hanging
3299
+ // forever (plan P4 finish item 1).
3300
+ session.openFailure = error instanceof Error ? error.message : String(error);
3227
3301
  state.observeSnapshots.delete(sessionId);
3302
+ state.sessions.delete(sessionId);
3303
+ if (state.workerSessions.get(workerIndex) === sessionId) {
3304
+ state.workerSessions.delete(workerIndex);
3305
+ }
3306
+ session.snapshotReady = null;
3307
+ resolveSnapshotReady();
3308
+ throw error;
3228
3309
  }
3310
+ if (state.sessions.get(sessionId) !== session) {
3311
+ // The session was UNBOUND while its snapshot round ran (a different
3312
+ // test claimed the worker — the abandonment path in the open guard
3313
+ // below): its open answer can no longer launch a test, so discard
3314
+ // the late snapshots, free the dedicated proxy and answer with the
3315
+ // recorded failure — never 200 (a session view here would hand out
3316
+ // a credential every submission refuses). The gate still resolves
3317
+ // so any waiter wakes into the unregistered re-check.
3318
+ state.observeSnapshots.delete(sessionId);
3319
+ await stopSessionProxy(session);
3320
+ await stopPageObserver(state, sessionId);
3321
+ session.snapshotReady = null;
3322
+ resolveSnapshotReady();
3323
+ throw new HttpError(500, `session open failed: ${session.openFailure ?? 'the session was unregistered while its before-snapshots were still running'}`);
3324
+ }
3325
+ // Ready: the before-state exists. Clear the gate first so a waiter
3326
+ // waking on the promise observes a fully ready session.
3327
+ session.snapshotReady = null;
3328
+ resolveSnapshotReady();
3229
3329
  sendJson(res, 200, sessionView(state, session));
3230
3330
  }
3231
3331
  /** The session view returned to supervisor and worker (the credential). */
@@ -3275,6 +3375,18 @@ async function handleSessionClose(state, res, body) {
3275
3375
  if (session === undefined) {
3276
3376
  throw new HttpError(400, `session '${sessionId}' is unknown (never opened on this witness)`);
3277
3377
  }
3378
+ // The seal must never overtake the snapshot round (plan P4 finish
3379
+ // item 2): snapshots are total, so awaiting the gate is bounded, and
3380
+ // a late snapshot can then never store before-state into an already
3381
+ // sealed session. The session may also have died unregistered while
3382
+ // we waited (proxy failure, or a different test claimed the worker) —
3383
+ // refuse it like an unknown one instead of sealing a ghost.
3384
+ if (session.snapshotReady !== null) {
3385
+ await session.snapshotReady;
3386
+ if (state.sessions.get(sessionId) !== session) {
3387
+ throw new HttpError(400, `session '${sessionId}' is unknown (never opened on this witness)`);
3388
+ }
3389
+ }
3278
3390
  if (session.status === 'outcome-pending') {
3279
3391
  if (typeof outcome === 'string') {
3280
3392
  if (session.outcome !== null && session.outcome !== outcome) {
@@ -3501,6 +3613,18 @@ async function handleSessionRelease(state, res, body) {
3501
3613
  if (session === undefined) {
3502
3614
  throw new HttpError(400, `session '${sessionId}' is unknown (never opened on this witness)`);
3503
3615
  }
3616
+ // Same bounded wait as the close (plan P4 finish item 2): the release
3617
+ // seals the session's lifecycle (outcome-pending) and kills its
3618
+ // dedicated proxy, so it must not outrun the snapshot round — a late
3619
+ // snapshot must never store before-state into a released session
3620
+ // whose proxy is already dead. A session that died unregistered while
3621
+ // we waited is refused like an unknown one.
3622
+ if (session.snapshotReady !== null) {
3623
+ await session.snapshotReady;
3624
+ if (state.sessions.get(sessionId) !== session) {
3625
+ throw new HttpError(400, `session '${sessionId}' is unknown (never opened on this witness)`);
3626
+ }
3627
+ }
3504
3628
  if (session.status === 'open') {
3505
3629
  session.status = 'outcome-pending';
3506
3630
  session.sealedTick = (state.tick += 1);
@@ -3538,7 +3662,14 @@ async function handleSessionResolve(state, res, body) {
3538
3662
  }
3539
3663
  const sessionId = state.workerSessions.get(workerIndex);
3540
3664
  const session = sessionId === undefined ? undefined : state.sessions.get(sessionId);
3541
- if (session === undefined || session.status !== 'open' || session.testId !== testId) {
3665
+ if (session === undefined ||
3666
+ session.status !== 'open' ||
3667
+ session.testId !== testId ||
3668
+ // Readiness gate (plan P4): a session whose before-snapshots are
3669
+ // still running is handed out to NO ONE — the fixture polls and
3670
+ // gets the same 404 shape an unknown session produces, until the
3671
+ // snapshot pass stores the before-state.
3672
+ session.snapshotReady !== null) {
3542
3673
  sendJson(res, 404, {
3543
3674
  error: `no open session for (workerIndex ${String(workerIndex)}, testId '${testId}'); the ` +
3544
3675
  'supervisor (the gateforge reporter) opens a session per started test — run under ' +
@@ -4258,8 +4389,16 @@ async function takeObserveSnapshots(state, session) {
4258
4389
  return;
4259
4390
  const perSession = new Map();
4260
4391
  state.observeSnapshots.set(session.sessionId, perSession);
4261
- for (const resourceId of [...resources].sort(compareStrings)) {
4262
- perSession.set(resourceId, await snapshotObserveResource(state, resourceId));
4392
+ // One slow (or failing) resource must not stretch session open by the
4393
+ // SUM of every resource's list time: the snapshots run concurrently
4394
+ // (plan 20261021_0030_gf_infra_visibility P1), and the results are
4395
+ // inserted in the same sorted order a serial loop produced, so the
4396
+ // stored map is unchanged. `snapshotObserveResource` never throws, so
4397
+ // a resource's error snapshot arrives exactly as before.
4398
+ const sortedResourceIds = [...resources].sort(compareStrings);
4399
+ const snapshots = await Promise.all(sortedResourceIds.map(async (resourceId) => ({ resourceId, snapshot: await snapshotObserveResource(state, resourceId) })));
4400
+ for (const { resourceId, snapshot } of snapshots) {
4401
+ perSession.set(resourceId, snapshot);
4263
4402
  }
4264
4403
  session.activity += 1;
4265
4404
  }