ravensight-playtest 0.8.1 → 0.8.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ravensight-playtest",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "description": "Local AI playtests and seeded game simulations with reports in Ravensight.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://ravensight.io/docs/#playtest",
@@ -54,7 +54,7 @@ export async function hostedRun(filename, flags = {}, deps = {}) {
54
54
  }
55
55
  if (!flags.wait) {
56
56
  if (flags.json) ui.json(admitted);
57
- else ui.info('The server acknowledged charged_blocked. This does not prove a worker started or produced gameplay or reports.');
57
+ else ui.info(`The server acknowledged the paid admission (${admitted.state}). This does not prove a worker started or produced gameplay or reports.`);
58
58
  return ExitCode.OK;
59
59
  }
60
60
  const view = await waitForRun({ api, gameId, admissionId: admitted.admissionId, apiUrl, sleep, now, onPhase: phaseLine(flags) });
@@ -239,24 +239,51 @@ export function hostedRefusalMessage(error) {
239
239
  const refusalError = (code, error = { code }) => Object.assign(new CliError(hostedRefusalMessage(error), ExitCode.ENVIRONMENT),
240
240
  { refusal: { code, certain: true, retryAfter: Number.isFinite(error.retryAfter) ? error.retryAfter : null } });
241
241
 
242
+ // The states a replayed key can report for an admission it made. The first
243
+ // answer is charged_blocked; a replay after the direct queue moved the run on
244
+ // reports where it is now (game_tracker recordedAdmission REPLAY_STATE). Every
245
+ // one of them means the run was admitted and charged once.
246
+ export const ADMITTED_STATES = Object.freeze(['charged_blocked', 'dispatched', 'running', 'review', 'closed', 'settled', 'canceled']);
247
+ const ACK_KEYS = ['admissionId', 'dispatchAllowed', 'jobId', 'state', 'totalPriceCents'];
248
+ const wholeOrNull = value => value === null || value === undefined || Number.isSafeInteger(value) && value >= 0;
249
+ const timeOrNull = value => value === null || value === undefined || typeof value === 'string' && value.length <= 64 && Number.isFinite(Date.parse(value));
250
+ // The queue place (directQueue.forAdmission). It is information only, never
251
+ // money, but each field it names must still have its own type.
252
+ export function validQueue(q) {
253
+ return Boolean(q) && typeof q === 'object' && !Array.isArray(q) && Number.isSafeInteger(q.position) && q.position >= 0
254
+ && ['ahead', 'running', 'ceiling', 'expectedStartMinutes', 'latestStartMinutes'].every(key => wholeOrNull(q[key]))
255
+ && ['estimatedStartAt', 'expectedStartAt', 'latestStartAt'].every(key => timeOrNull(q[key]))
256
+ && (q.startsNow === undefined || typeof q.startsNow === 'boolean')
257
+ && (q.message === undefined || typeof q.message === 'string' && q.message.length <= 500);
258
+ }
259
+
242
260
  // What a 202 from POST /runs says: the acknowledgement of a new or replayed
243
261
  // admission, the server's record that this key's attempt was refused, or an
244
- // outcome the server itself cannot yet state.
262
+ // outcome the server itself cannot yet state. The keys are checked exactly,
263
+ // because this answer is the record of money taken: a queued admission adds
264
+ // `queued: true` with its `queue` place, and nothing else is accepted.
245
265
  function readAdmissionAnswer(answer, totalPriceCents) {
246
266
  if (answer.status !== 202 || answer.headers.get('content-type')?.split(';')[0].trim().toLowerCase() !== 'application/json'
247
267
  || typeof answer.body !== 'string' || Buffer.byteLength(answer.body) > MAX_QUOTE_BYTES) return { kind: 'unknown' };
248
268
  let ack;
249
269
  try { ack = JSON.parse(answer.body); } catch { return { kind: 'unknown' }; }
250
- if (ack && same(ack, { state: 'failed', dispatchAllowed: false })) return { kind: 'refused_earlier' };
251
- // A queued admission also carries its queue place; it is read through queueInfo, never trusted as money.
252
- const keys = ack && typeof ack === 'object' ? Object.keys(ack).filter(key => key !== 'queue').sort() : [];
253
- if (Object.hasOwn(ack || {}, 'queue') && !queueInfo(ack)) return { kind: 'unknown' };
254
- if (!ack || !same(keys, ['admissionId', 'dispatchAllowed', 'jobId', 'state', 'totalPriceCents'])
255
- || ack.state !== 'charged_blocked' || ack.dispatchAllowed !== false || ack.totalPriceCents !== totalPriceCents
256
- || !admissionPattern.test(ack.admissionId || '') || !/^pt_job_[a-f0-9]{20}$/.test(ack.jobId || '')) return { kind: 'unknown' };
257
- return { kind: 'admitted', ack };
270
+ if (!ack || typeof ack !== 'object' || Array.isArray(ack)) return { kind: 'unknown' };
271
+ if (same(ack, { state: 'failed', dispatchAllowed: false })) return { kind: 'refused_earlier' };
272
+ // Whatever else is wrong, an admission id the server named is kept so the person can find the run.
273
+ const named = { admissionId: admissionPattern.test(ack.admissionId || '') ? ack.admissionId : null,
274
+ jobId: /^pt_job_[a-f0-9]{20}$/.test(ack.jobId || '') ? ack.jobId : null };
275
+ const unknown = { kind: 'unknown', ...named };
276
+ const keys = Object.keys(ack).filter(key => key !== 'queue' && key !== 'queued').sort();
277
+ if (Object.hasOwn(ack, 'queue') && !validQueue(ack.queue)) return unknown;
278
+ if (Object.hasOwn(ack, 'queued') && (ack.queued !== true || !Object.hasOwn(ack, 'queue'))) return unknown;
279
+ if (!same(keys, ACK_KEYS) || !ADMITTED_STATES.includes(ack.state) || ack.dispatchAllowed !== false
280
+ || ack.totalPriceCents !== totalPriceCents || !named.admissionId || !named.jobId) return unknown;
281
+ const { queued, ...rest } = ack;
282
+ return { kind: 'admitted', ack: rest };
258
283
  }
259
284
 
285
+ const namedRun = read => read.admissionId ? ` The server named hosted run ${read.admissionId}${read.jobId ? ` (job ${read.jobId})` : ''}; check it with: ravensight-playtest hosted status ${read.admissionId}` : '';
286
+
260
287
  async function postAdmission(api, { gameId, consent, idempotencyKey }, captureFile) {
261
288
  let responseCaptured = false;
262
289
  const answer = await api.request('POST', `${base(gameId)}/runs`, { body: consent,
@@ -303,7 +330,7 @@ export async function admitHostedOffer({ offer, api, apiUrl, repoRoot, now = Dat
303
330
  }
304
331
  const read = answer ? readAdmissionAnswer(answer, offer.quote.totalPriceCents) : { kind: 'unknown' };
305
332
  if (read.kind === 'refused_earlier') { await clearIntent(journal); throw refusalError(REFUSED_EARLIER); }
306
- if (read.kind !== 'admitted') fail(`The admission acknowledgement needs reconciliation: ${unknownAdvice(journal)}`);
333
+ if (read.kind !== 'admitted') fail(`The admission acknowledgement needs reconciliation: ${unknownAdvice(journal)}.${namedRun(read)}`);
307
334
  await recordAdmitted(journal, state, read.ack);
308
335
  return { state: 'charged_blocked', ...read.ack, queue: queueInfo(read.ack), journal, replayAuthorized: false, gameplayProven: false };
309
336
  }
@@ -365,10 +392,11 @@ export async function resolveHostedIntents({ api, apiUrl, repoRoot, now = Date.n
365
392
  } else if (read.kind === 'admitted') {
366
393
  await recordAdmitted(journal, state, read.ack);
367
394
  results.push({ journal, gameId: state.gameId, state: 'admitted', admissionId: read.ack.admissionId, jobId: read.ack.jobId,
368
- totalPriceCents: read.ack.totalPriceCents, message: `Game ${state.gameId}: the run was admitted as ${read.ack.admissionId} and charged ${money(read.ack.totalPriceCents)}.` });
395
+ totalPriceCents: read.ack.totalPriceCents, runState: read.ack.state,
396
+ message: `Game ${state.gameId}: the run was admitted as ${read.ack.admissionId} (job ${read.ack.jobId}) and charged ${money(read.ack.totalPriceCents)}.` });
369
397
  } else {
370
- results.push({ journal, gameId: state.gameId, state: 'unknown',
371
- message: `Game ${state.gameId}: the server cannot state the outcome yet. The record is kept; nothing was retried. If this persists, write to support@realityse.com with ${path.basename(journal)}.` });
398
+ results.push({ journal, gameId: state.gameId, state: 'unknown', ...(read.admissionId ? { admissionId: read.admissionId } : {}), ...(read.jobId ? { jobId: read.jobId } : {}),
399
+ message: `Game ${state.gameId}: the server cannot state the outcome yet. The record is kept; nothing was retried.${namedRun(read)} If this persists, write to support@realityse.com with ${path.basename(journal)}.` });
372
400
  }
373
401
  }
374
402
  return results;