@lotics/app-sdk 0.127.3 → 0.127.4

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 (2) hide show
  1. package/dist/index.js +38 -9
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -809,6 +809,8 @@ var MAX_RANDOM_LENGTH = 1e3;
809
809
  var MAX_RUN_BUILT_BYTES = 512 * 1024 * 1024;
810
810
  var MAX_IN_PLACE_BYTES = 1024 * 1024;
811
811
  var MAX_IN_PLACE_HELPER_BYTES = 8 * 1024;
812
+ var MAX_IN_PLACE_MS = 5;
813
+ var IN_PLACE_GAP_MS = 1;
812
814
  var MAX_PERSISTED_JSON_LENGTH = 64 * 1024 * 1024;
813
815
  var ExpressionArgumentError = class extends Error {
814
816
  constructor(message2) {
@@ -8820,8 +8822,10 @@ var WorkflowTimeBudgetError = class extends Error {
8820
8822
  /** `waitedFor`: what the run's deadline passed waiting on, when that was not the run's own work. */
8821
8823
  constructor(subject, budgetMs, waitedFor) {
8822
8824
  super(`${subject} exceeded its time budget of ${budgetMs / 1e3}s${waitedFor === void 0 ? "" : ` waiting for ${waitedFor}`}.`);
8825
+ this.budgetMs = budgetMs;
8823
8826
  this.name = "WorkflowTimeBudgetError";
8824
8827
  }
8828
+ budgetMs;
8825
8829
  };
8826
8830
  var WorkflowExpressionBudgetError = class extends Error {
8827
8831
  constructor(budget) {
@@ -8858,11 +8862,11 @@ var turn = {
8858
8862
  function startRunBudget(subject, budgetMs, path) {
8859
8863
  return { host: "shared", deadline_ms: Date.now() + budgetMs, budget_ms: budgetMs, subject, path, built_bytes: 0 };
8860
8864
  }
8861
- function runClock(run2) {
8865
+ function runClock(run2, yields = true) {
8862
8866
  if (Date.now() > run2.deadline_ms) {
8863
8867
  throw new WorkflowTimeBudgetError(run2.subject, run2.budget_ms);
8864
8868
  }
8865
- if (run2.host === "isolated") return null;
8869
+ if (run2.host === "isolated" || !yields) return null;
8866
8870
  if (turn.pause === null && performance.now() - turn.slice_started_ms >= EVALUATION_SLICE_MS) {
8867
8871
  turn.pause = new Promise((resolve) => setTimeout(resolve, 0)).then(() => {
8868
8872
  turn.pause = null;
@@ -8958,13 +8962,13 @@ function parsedSize(text2) {
8958
8962
  return text2.length * CHAR_BYTES + nodes * PARSED_NODE_BYTES;
8959
8963
  }
8960
8964
  function assertCheap(budget, ...values2) {
8961
- assertWithin(budget, MAX_IN_PLACE_BYTES, values2);
8965
+ assertWithin(budget, MAX_IN_PLACE_BYTES, values2, writtenSize);
8962
8966
  }
8963
- function assertWithin(budget, limit, values2) {
8967
+ function assertWithin(budget, limit, values2, measure) {
8964
8968
  if (!budget.bounded) return;
8965
8969
  let bytes = 0;
8966
8970
  for (const value of values2) {
8967
- bytes += writtenSize(value, limit - bytes);
8971
+ bytes += measure(value, limit - bytes);
8968
8972
  if (bytes > limit) throw new ExpressionNotCheapError();
8969
8973
  }
8970
8974
  }
@@ -8990,9 +8994,21 @@ function callbackWorkers() {
8990
8994
  };
8991
8995
  }
8992
8996
  function evaluateWorkflowExpression(expr2, ctx, options = {}) {
8993
- const budget = { workers: callbackWorkers(), bounded: options.bounded === true, built: 0, clock_checks: 0, helpers: /* @__PURE__ */ new Map(), settled: false };
8997
+ const budget = {
8998
+ workers: callbackWorkers(),
8999
+ bounded: options.bounded === true,
9000
+ built: 0,
9001
+ clock_checks: 0,
9002
+ nodes: 0,
9003
+ last_node_ms: options.bounded === true ? performance.now() : 0,
9004
+ sync_ms: 0,
9005
+ over_time: false,
9006
+ helpers: /* @__PURE__ */ new Map(),
9007
+ settled: false
9008
+ };
8994
9009
  return evaluateRoot(expr2, { ...ctx, evaluation_budget: budget, anchor_context: evalAnchorContext(ctx) }).finally(() => {
8995
9010
  budget.settled = true;
9011
+ options.onNodes?.(budget.nodes);
8996
9012
  }).then((answer) => {
8997
9013
  if (budget.bounded && expandedSize(answer, MAX_IN_PLACE_BYTES) > MAX_IN_PLACE_BYTES) throw new ExpressionNotCheapError();
8998
9014
  return answer;
@@ -9013,7 +9029,10 @@ async function startBeside(expr2, ctx) {
9013
9029
  return evaluateExpression(expr2, ctx);
9014
9030
  }
9015
9031
  function evaluateExpression(expr2, ctx) {
9016
- assertUnsettled(ctx.evaluation_budget);
9032
+ const budget = ctx.evaluation_budget;
9033
+ assertUnsettled(budget);
9034
+ budget.nodes += 1;
9035
+ if (budget.bounded) assertTime(budget);
9017
9036
  switch (expr2.kind) {
9018
9037
  case "lit":
9019
9038
  return expr2.value;
@@ -9041,6 +9060,16 @@ function evaluateExpression(expr2, ctx) {
9041
9060
  return assertNever(expr2);
9042
9061
  }
9043
9062
  }
9063
+ function assertTime(budget) {
9064
+ if (budget.over_time) throw new ExpressionNotCheapError();
9065
+ const now = performance.now();
9066
+ const gap = now - budget.last_node_ms;
9067
+ budget.last_node_ms = now;
9068
+ if (gap < IN_PLACE_GAP_MS) budget.sync_ms += gap;
9069
+ if (budget.sync_ms <= MAX_IN_PLACE_MS) return;
9070
+ budget.over_time = true;
9071
+ throw new ExpressionNotCheapError();
9072
+ }
9044
9073
  async function branchAfter(cond, then, otherwise, ctx) {
9045
9074
  return evaluateExpression(await cond ? then : otherwise, ctx);
9046
9075
  }
@@ -9081,7 +9110,7 @@ async function fillObjectAfter(out, entries3, at2, value, ctx) {
9081
9110
  }
9082
9111
  function checkClock(ctx) {
9083
9112
  assertUnsettled(ctx.evaluation_budget);
9084
- return readsClock(ctx) ? runClock(ctx.run_budget) : null;
9113
+ return readsClock(ctx) ? runClock(ctx.run_budget, !ctx.evaluation_budget.bounded) : null;
9085
9114
  }
9086
9115
  var ISOLATED_CLOCK_STRIDE = 16;
9087
9116
  function readsClock(ctx) {
@@ -9598,7 +9627,7 @@ async function callAfter(helper, pending, ctx) {
9598
9627
  return callHelper(helper, values2, ctx);
9599
9628
  }
9600
9629
  function callHelper({ fn, call, cost }, values2, ctx) {
9601
- assertWithin(ctx.evaluation_budget, MAX_IN_PLACE_HELPER_BYTES, values2);
9630
+ assertWithin(ctx.evaluation_budget, MAX_IN_PLACE_HELPER_BYTES, values2, cost === "reads" ? writtenSize : expandedSize);
9602
9631
  if (cost === "parses") charge(ctx, parsedSize(values2[0]));
9603
9632
  let answer;
9604
9633
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lotics/app-sdk",
3
- "version": "0.127.3",
3
+ "version": "0.127.4",
4
4
  "description": "The SDK a Lotics custom-code app reads and writes through — typed hooks over the host bridge, cell readers, mount() and AppRouter",
5
5
  "type": "module",
6
6
  "exports": {