velocious 1.0.747 → 1.0.748

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.
Files changed (38) hide show
  1. package/README.md +1 -1
  2. package/build/background-jobs/pooled-runner-child.js +41 -4
  3. package/build/background-jobs/pooled-runner-shutdown.js +92 -0
  4. package/build/background-jobs/types.js +16 -0
  5. package/build/background-jobs/worker.js +56 -3
  6. package/build/frontend-model-controller.js +41 -0
  7. package/build/frontend-models/query.js +42 -0
  8. package/build/src/background-jobs/pooled-runner-child.js +39 -5
  9. package/build/src/background-jobs/pooled-runner-shutdown.d.ts +22 -0
  10. package/build/src/background-jobs/pooled-runner-shutdown.d.ts.map +1 -1
  11. package/build/src/background-jobs/pooled-runner-shutdown.js +87 -1
  12. package/build/src/background-jobs/types.d.ts +47 -0
  13. package/build/src/background-jobs/types.d.ts.map +1 -1
  14. package/build/src/background-jobs/types.js +17 -1
  15. package/build/src/background-jobs/worker.d.ts +16 -0
  16. package/build/src/background-jobs/worker.d.ts.map +1 -1
  17. package/build/src/background-jobs/worker.js +56 -4
  18. package/build/src/frontend-model-controller.d.ts +4 -0
  19. package/build/src/frontend-model-controller.d.ts.map +1 -1
  20. package/build/src/frontend-model-controller.js +34 -1
  21. package/build/src/frontend-models/query.d.ts +4 -0
  22. package/build/src/frontend-models/query.d.ts.map +1 -1
  23. package/build/src/frontend-models/query.js +38 -1
  24. package/build/src/sync/sync-client.d.ts.map +1 -1
  25. package/build/src/sync/sync-client.js +2 -1
  26. package/build/src/sync/sync-offline-session.d.ts.map +1 -1
  27. package/build/src/sync/sync-offline-session.js +9 -3
  28. package/build/sync/sync-client.js +1 -0
  29. package/build/sync/sync-offline-session.js +9 -1
  30. package/package.json +1 -1
  31. package/src/background-jobs/pooled-runner-child.js +41 -4
  32. package/src/background-jobs/pooled-runner-shutdown.js +92 -0
  33. package/src/background-jobs/types.js +16 -0
  34. package/src/background-jobs/worker.js +56 -3
  35. package/src/frontend-model-controller.js +41 -0
  36. package/src/frontend-models/query.js +42 -0
  37. package/src/sync/sync-client.js +1 -0
  38. package/src/sync/sync-offline-session.js +9 -1
@@ -15,7 +15,7 @@ import { runWithBackgroundJobPayload } from "./execution-context.js"
15
15
  import { createGenerationWorkerId } from "./generation-identity.js"
16
16
  import { isProcessAlive } from "./owner-liveness.js"
17
17
  import BackgroundJobsGenerationHandshakeTimeoutError, { DEFAULT_GENERATION_HANDSHAKE_TIMEOUT_MS, validateGenerationHandshakeTimeoutMs } from "./generation-handshake-timeout-error.js"
18
- import { POOLED_RUNNER_INFLIGHT_JOB_ID_LIMIT, boundedPooledRunnerInflightJobIds, isPooledChildShutdownReason, isPooledChildShutdownSignal } from "./pooled-runner-shutdown.js"
18
+ import { POOLED_RUNNER_INFLIGHT_JOB_ID_LIMIT, boundedPooledRunnerInflightJobIds, isPooledChildProcessFault, isPooledChildShutdownReason, isPooledChildShutdownSignal } from "./pooled-runner-shutdown.js"
19
19
 
20
20
  /**
21
21
  * Per-forked-child bookkeeping, shared by the wall-clock backstop, the runner's
@@ -160,10 +160,13 @@ function isForkedChildJobReport(message) {
160
160
  */
161
161
  function isChildShutdownObservationMessage(message) {
162
162
  if (!message || typeof message !== "object") return false
163
- const record = /** @type {{childInstanceId?: ReturnType<typeof JSON.parse>, inflightJobIds?: ReturnType<typeof JSON.parse>, inflightJobIdsTruncatedCount?: ReturnType<typeof JSON.parse>, reason?: ReturnType<typeof JSON.parse>, shutdownObservedAtMs?: ReturnType<typeof JSON.parse>, shutdownRequestedAtMs?: ReturnType<typeof JSON.parse>, signal?: ReturnType<typeof JSON.parse>, type?: ReturnType<typeof JSON.parse>}} */ (message)
163
+ const record = /** @type {{childInstanceId?: ReturnType<typeof JSON.parse>, fault?: ReturnType<typeof JSON.parse>, inflightJobIds?: ReturnType<typeof JSON.parse>, inflightJobIdsTruncatedCount?: ReturnType<typeof JSON.parse>, reason?: ReturnType<typeof JSON.parse>, shutdownObservedAtMs?: ReturnType<typeof JSON.parse>, shutdownRequestedAtMs?: ReturnType<typeof JSON.parse>, signal?: ReturnType<typeof JSON.parse>, type?: ReturnType<typeof JSON.parse>}} */ (message)
164
164
 
165
165
  return record.type === "shutdown-observation"
166
166
  && typeof record.childInstanceId === "string"
167
+ // A child predating the fault contract sends no fault field at all; its shutdown
168
+ // provenance must still be accepted.
169
+ && (record.fault === undefined || record.fault === null || isPooledChildProcessFault(record.fault))
167
170
  && Array.isArray(record.inflightJobIds)
168
171
  && record.inflightJobIds.length <= POOLED_RUNNER_INFLIGHT_JOB_ID_LIMIT
169
172
  && record.inflightJobIds.every((jobId) => typeof jobId === "string")
@@ -176,6 +179,23 @@ function isChildShutdownObservationMessage(message) {
176
179
  && isPooledChildShutdownSignal(record.signal)
177
180
  }
178
181
 
182
+ /**
183
+ * Names the failure reported for every job lost with a pooled child. When the child
184
+ * transmitted its process-level fault, that fault is part of the report: the exit
185
+ * line alone is what makes a real framework fault read as infrastructure loss and
186
+ * re-triggers a whole shard with zero reported failures. The exit line is kept so the
187
+ * observed code and signal remain readable in the same record.
188
+ * @param {object} args - Failure details.
189
+ * @param {Error} args.error - Worker-side failure observed for the child.
190
+ * @param {import("./types.js").PooledChildProcessFault | null} args.fault - Fault the child transmitted before exiting.
191
+ * @returns {Error} - Error to report for the lost jobs.
192
+ */
193
+ function pooledChildFailureError({error, fault}) {
194
+ if (!fault) return error
195
+
196
+ return new Error(`${error.message} — ${fault.type}: ${fault.message ?? fault.valuePreview ?? "unspecified fault"}`)
197
+ }
198
+
179
199
  /**
180
200
  * Checks whether an IPC value is a pooled child's bounded memory observation.
181
201
  * The pooled child's stdio is ignored by the worker fork, so this observation
@@ -2102,14 +2122,16 @@ export default class BackgroundJobsWorker {
2102
2122
  const runnerFailure = state
2103
2123
  ? this._pooledRunnerFailure({child, exitCode, origin, signal, state})
2104
2124
  : undefined
2125
+ if (runnerFailure?.processFault) this._logPooledChildProcessFault({child, runnerFailure})
2105
2126
  if (state) state.inflight.clear()
2106
2127
  this.pooledChildStates.delete(child)
2107
2128
 
2129
+ const failureError = pooledChildFailureError({error, fault: runnerFailure?.processFault ?? null})
2108
2130
  const failureReports = entries.map(async (entry) => {
2109
2131
  await this._reportJobResult({
2110
2132
  jobId: entry.payload.id,
2111
2133
  status: "failed",
2112
- error,
2134
+ error: failureError,
2113
2135
  handoffId: entry.payload.handoffId,
2114
2136
  handedOffAtMs: entry.payload.handedOffAtMs,
2115
2137
  runnerFailure,
@@ -2211,6 +2233,7 @@ export default class BackgroundJobsWorker {
2211
2233
  ...boundedInflight,
2212
2234
  oomKilled: signal === "SIGKILL" && shutdownReason === "signal_sigkill" ? null : false,
2213
2235
  origin,
2236
+ processFault: observation?.fault ?? null,
2214
2237
  runnerAgeMs: Math.max(0, Date.now() - state.createdAtMs),
2215
2238
  runnerCreatedAtMs: state.createdAtMs,
2216
2239
  runnerDetached: false,
@@ -2230,6 +2253,36 @@ export default class BackgroundJobsWorker {
2230
2253
  })
2231
2254
  }
2232
2255
 
2256
+ /**
2257
+ * Logs one compact JSON line naming the process-level fault a pooled child
2258
+ * transmitted over IPC before it exited — the same stderr surface the memory
2259
+ * observations use, which reaches the production log while the child's own stdio
2260
+ * is ignored. A child that dies of an unhandled rejection otherwise looks exactly
2261
+ * like an infrastructure exit, so the shard is retried with zero reported failures
2262
+ * and the fault is never attributed.
2263
+ * @param {object} args - Event details.
2264
+ * @param {import("node:child_process").ChildProcess} args.child - Pooled child.
2265
+ * @param {import("./types.js").PooledRunnerFailure} args.runnerFailure - Shared failure provenance carrying the fault.
2266
+ * @returns {void}
2267
+ */
2268
+ _logPooledChildProcessFault({child, runnerFailure}) {
2269
+ const fault = runnerFailure.processFault
2270
+ if (!fault) return
2271
+
2272
+ console.error(JSON.stringify({
2273
+ event: "pooled-child-process-fault",
2274
+ childInstanceId: runnerFailure.childInstanceId,
2275
+ childPid: runnerFailure.runnerPid ?? child.pid ?? null,
2276
+ exitCode: runnerFailure.exitCode,
2277
+ faultMessage: fault.message,
2278
+ faultName: fault.name,
2279
+ faultType: fault.type,
2280
+ jobs: runnerFailure.inflightJobIds.length,
2281
+ shutdownReason: runnerFailure.shutdownReason,
2282
+ valuePreview: fault.valuePreview
2283
+ }))
2284
+ }
2285
+
2233
2286
  /**
2234
2287
  * Runs run job inline.
2235
2288
  * @param {import("./types.js").BackgroundJobPayload} payload - Payload.
@@ -83,6 +83,11 @@ import {RansackQueryError, normalizeRansackGroup, parseRansackSort} from "./util
83
83
 
84
84
  const ATTACHMENT_OWNER_KEY = "__attachmentOwner"
85
85
 
86
+ /** Maximum number of commands accepted in one frontend-model API batch. */
87
+ export const FRONTEND_MODEL_MAX_BATCH_REQUESTS = 100
88
+ /** Maximum UTF-8 byte size of a frontend-model API request payload. */
89
+ export const FRONTEND_MODEL_MAX_REQUEST_BYTES = 256 * 1024
90
+
86
91
  /**
87
92
  * Runs normalize frontend model preload.
88
93
  * @param {import("./database/query/index.js").NestedPreloadRecord | string | string[] | boolean | undefined | null} preload - Preload shorthand.
@@ -211,6 +216,24 @@ function frontendModelQueryError(message) {
211
216
  return VelociousError.safe(message, {code: "frontend-model-query-error"})
212
217
  }
213
218
 
219
+ /**
220
+ * Checks transport-level frontend-model request admission limits.
221
+ * @param {Record<string, ReturnType<typeof JSON.parse>>} params - Decoded request params.
222
+ * @returns {void}
223
+ */
224
+ function assertFrontendModelRequestLimits(params) {
225
+ const serializedParams = JSON.stringify(params)
226
+ const requestBytes = Buffer.byteLength(serializedParams || "", "utf8")
227
+
228
+ if (requestBytes > FRONTEND_MODEL_MAX_REQUEST_BYTES) {
229
+ throw frontendModelQueryError(`Frontend-model request payload exceeds the maximum size of ${FRONTEND_MODEL_MAX_REQUEST_BYTES} bytes`)
230
+ }
231
+
232
+ if (Array.isArray(params.requests) && params.requests.length > FRONTEND_MODEL_MAX_BATCH_REQUESTS) {
233
+ throw frontendModelQueryError(`Frontend-model request batch exceeds the maximum of ${FRONTEND_MODEL_MAX_BATCH_REQUESTS} requests`)
234
+ }
235
+ }
236
+
214
237
  /**
215
238
  * Throws a client-safe frontend-model query error for typed query parser errors.
216
239
  * @param {ReturnType<typeof JSON.parse>} error - Error raised while normalizing client query params.
@@ -4431,6 +4454,24 @@ export default class FrontendModelController extends Controller {
4431
4454
  }
4432
4455
 
4433
4456
  const params = /** @type {Record<string, ReturnType<typeof JSON.parse>>} */ (deserializeFrontendModelTransportValue(this.params()))
4457
+ try {
4458
+ assertFrontendModelRequestLimits(params)
4459
+ } catch (error) {
4460
+ const errorContext = this.frontendModelEndpointErrorContext({
4461
+ action: "frontendApi",
4462
+ error
4463
+ })
4464
+
4465
+ await this.frontendModelLogEndpointError({error, errorContext})
4466
+ await this.render({
4467
+ json: /** @type {Record<string, ReturnType<typeof JSON.parse>>} */ (serializeFrontendModelTransportValue(
4468
+ await this.frontendModelClientErrorPayloadForError(error, errorContext),
4469
+ this.transportSerializationOptions()
4470
+ ))
4471
+ })
4472
+ return
4473
+ }
4474
+
4434
4475
  const requests = Array.isArray(params.requests) ? params.requests : [params]
4435
4476
  /**
4436
4477
  * Responses.
@@ -140,6 +140,42 @@ const FRONTEND_MODEL_INDEX_PAYLOAD_KEYS = new Set([
140
140
  "withCount"
141
141
  ])
142
142
 
143
+ /** Maximum nesting depth accepted in a frontend-model query shape. */
144
+ export const FRONTEND_MODEL_QUERY_MAX_DEPTH = 8
145
+ /** Maximum number of object keys accepted in one frontend-model query shape. */
146
+ export const FRONTEND_MODEL_QUERY_MAX_KEYS = 256
147
+
148
+ /**
149
+ * Checks the resource cost of a nested frontend-model query shape before any
150
+ * query normalization or database work starts.
151
+ * @param {ReturnType<typeof JSON.parse>} value - Query shape.
152
+ * @param {string} label - Query field name for the error message.
153
+ * @param {number} [depth] - Current nesting depth.
154
+ * @param {{keys: number}} [state] - Shared key counter.
155
+ * @returns {void}
156
+ */
157
+ function assertFrontendModelQueryShape(value, label, depth = 0, state = {keys: 0}) {
158
+ if (!Array.isArray(value) && !isPlainObject(value)) return
159
+
160
+ if (depth > FRONTEND_MODEL_QUERY_MAX_DEPTH) {
161
+ throw frontendModelQueryError(`${label} exceeds the maximum nesting depth of ${FRONTEND_MODEL_QUERY_MAX_DEPTH}`)
162
+ }
163
+
164
+ if (Array.isArray(value)) {
165
+ for (const entry of value) assertFrontendModelQueryShape(entry, label, depth + 1, state)
166
+ return
167
+ }
168
+
169
+ for (const entry of Object.values(value)) {
170
+ state.keys += 1
171
+ if (state.keys > FRONTEND_MODEL_QUERY_MAX_KEYS) {
172
+ throw frontendModelQueryError(`${label} exceeds the maximum of ${FRONTEND_MODEL_QUERY_MAX_KEYS} keys`)
173
+ }
174
+
175
+ assertFrontendModelQueryShape(entry, label, depth + 1, state)
176
+ }
177
+ }
178
+
143
179
  /**
144
180
  * Builds a query descriptor error.
145
181
  * @param {string} message - Error message.
@@ -167,6 +203,12 @@ export function assertFrontendModelIndexPayload(payload) {
167
203
  }
168
204
  }
169
205
 
206
+ for (const queryKey of ["where", "joins", "preload", "group", "sort", "pluck"]) {
207
+ if (Object.hasOwn(payload, queryKey)) {
208
+ assertFrontendModelQueryShape(payload[queryKey], queryKey)
209
+ }
210
+ }
211
+
170
212
  return payload
171
213
  }
172
214
 
@@ -539,6 +539,7 @@ export default class SyncClient {
539
539
  baseVersion,
540
540
  conflictTracking: resourceConfig.conflictTracking,
541
541
  data,
542
+ offlineSession: this.config.offline ? this.offlineSession() : undefined,
542
543
  operation,
543
544
  resource: record,
544
545
  resourceType: record.constructor.getModelName(),
@@ -459,7 +459,15 @@ export default class SyncOfflineSession {
459
459
  async _replayPass({batchSize, signal} = {}) {
460
460
  throwIfAborted(signal)
461
461
 
462
- if (!this.isActive()) await this.start()
462
+ // Replay is app-owned: a background drain must not enroll a device or
463
+ // request a grant before the user has started the offline session.
464
+ if (!this._identity) {
465
+ let pendingCount = 0
466
+
467
+ for (const mutationLog of this.mutationLogs) pendingCount += (await mutationLog.pendingRecords()).length
468
+
469
+ return {conflictCount: 0, pendingCount, rejectedCount: 0, results: [], syncedCount: 0}
470
+ }
463
471
 
464
472
  const maxBatchSize = typeof batchSize === "number" && batchSize >= 1 ? Math.floor(batchSize) : 100
465
473
  /** @type {SyncOfflineReplayResult["results"]} */