@plimsollmark/client 0.23.0 → 0.24.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/README.md CHANGED
@@ -11,7 +11,8 @@ protocol over `fetch` and keeps the promises the Go client keeps:
11
11
 
12
12
  - it states the protocol number on every request;
13
13
  - it checks every answer's **run record**: it recomputes the request and result digests
14
- from what it sent and received ([run-records.md](https://github.com/plimsollmark/plimsoll/blob/main/docs/run-records.md)), and an
14
+ from what it sent and received ([run-records.md](https://github.com/plimsollmark/plimsoll/blob/main/docs/run-records.md)), refuses a
15
+ result of another kind than the request's and a record that ends before it starts, and an
15
16
  answer that does not check, or has no record, is a `data_loss` error with the result
16
17
  attached, because the code may have run;
17
18
  - it checks the reported isolation tier against the caller's floor the same way;
@@ -74,7 +75,7 @@ directory, before the code runs. Its result adds `interpreterStarted` (this call
74
75
  a fresh interpreter, so nothing defined earlier exists) and `interpreterEnded`; its exit code
75
76
  is 0 when the code ran, 1 when it raised and 124 at the deadline. Python needs `python3` in
76
77
  the daemon's image (`plimsoll/sandbox-python` has it, with NumPy and SciPy);
77
- `describe()` lists the languages the daemon proved on `environments.project.languages`.
78
+ `describe()` lists the languages a session's cells run in on `sessionEnvironment.languages`.
78
79
 
79
80
  `close()` sends one `CloseSession` however many times it is called, and every later call
80
81
  gets the first one's summary or error: the daemon forgets a session at its first close and
package/dist/client.js CHANGED
@@ -9,6 +9,7 @@ import { isIP } from "node:net";
9
9
  import { checkRecord, checkUnanswered, requestDigest, sessionFingerprint, softwareRuleAllows, } from "./record.js";
10
10
  import { LANGUAGES, OUTCOME_NAMES, TIERS } from "./tables.gen.js";
11
11
  import { errorFromWire, NOT_DISPATCHED_HEADER, PlimsollError, REQUEST_ID_HEADER, sessionEndFromWire, unboundSuccess } from "./errors.js";
12
+ import { wireBytes } from "./wire.js";
12
13
  /** The wire protocol number this client speaks (protocol.Number in Go). */
13
14
  export const PROTOCOL = 4;
14
15
  const SERVICE = "plimsoll.v1.SandboxService";
@@ -98,7 +99,7 @@ function mergeRules(a, b) {
98
99
  return { mode: ids.length === 1 ? "exact" : "approved", identities: ids };
99
100
  }
100
101
  const num = (v) => Number(v ?? 0);
101
- const bytes = (s) => new Uint8Array(Buffer.from(s ?? "", "base64"));
102
+ const bytes = (s) => new Uint8Array(wireBytes(s));
102
103
  const text = (b) => Buffer.from(b).toString("utf8");
103
104
  function adviceFromWire(a) {
104
105
  return (a ?? []).map((f) => ({
@@ -437,7 +438,7 @@ export class PlimsollClient {
437
438
  return decoded(() => this.checked(env, payload, resp));
438
439
  }
439
440
  checked(env, payload, resp) {
440
- const checked = checkRecord(requestDigest({ protocol: env.protocol, minimumIsolation: env.minimumIsolation ?? "", timeoutMs: env.timeoutMs ?? 0, softwareRule: env.softwareRule, payload: digestPayload(payload) }), env.softwareRule, resp);
441
+ const checked = checkRecord(requestDigest({ protocol: env.protocol, minimumIsolation: env.minimumIsolation ?? "", timeoutMs: env.timeoutMs ?? 0, softwareRule: env.softwareRule, payload: digestPayload(payload) }), "javascript" in payload ? "javascript" : "project" in payload ? "project" : "cell", env.softwareRule, resp);
441
442
  if ("problem" in checked)
442
443
  throw new PlimsollError("data_loss", `plimsoll: ${checked.problem}`, { result: resp });
443
444
  const r = checked.record;
@@ -625,16 +626,16 @@ export class Session {
625
626
  this.#end = e.sessionEnded;
626
627
  throw e;
627
628
  }
628
- return decoded(() => this.#chained(m, env, rule, digest));
629
+ return decoded(() => this.#chained(m, env, rule, digest, "javascript" in payload ? "javascript" : "project" in payload ? "project" : "cell"));
629
630
  }
630
- #chained(m, env, rule, digest) {
631
+ #chained(m, env, rule, digest, kind) {
631
632
  const run = m.run;
632
633
  if (!run)
633
634
  throw new PlimsollError("data_loss", "plimsoll: the session call's answer carries no run");
634
635
  const end = sessionEndFromWire(m.ended);
635
636
  if (end !== "open")
636
637
  this.#end = { reason: end, detail: m.endDetail ?? "" };
637
- const checked = checkRecord(digest, rule, run);
638
+ const checked = checkRecord(digest, kind, rule, run);
638
639
  if ("problem" in checked)
639
640
  throw new PlimsollError("data_loss", `plimsoll: ${checked.problem}`, { result: run });
640
641
  const r = checked.record;
package/dist/errors.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { RunRecord } from "./record.ts";
2
2
  import { REFUSAL_NAMES, SESSION_END_NAMES } from "./tables.gen.ts";
3
- import type { WireError, WireRecord } from "./wire.ts";
3
+ import { type WireError, type WireRecord } from "./wire.ts";
4
4
  /** Why a request was refused before any code ran (plimsoll.v1.NotDispatchedReason). */
5
5
  export type Refusal = (typeof REFUSAL_NAMES)[number];
6
6
  /**
@@ -38,6 +38,7 @@ export declare class PlimsollError extends Error {
38
38
  * The record the daemon sends with a session call that may have run but ended in
39
39
  * an error (an unanswered record, as it came off the wire). The Session checks it
40
40
  * and keeps it in its chain, so the session goes on; the call's outcome is unknown.
41
+ * On a single run's error it is kept as it came and checked by nothing.
41
42
  */
42
43
  readonly unanswered: WireRecord | undefined;
43
44
  /**
package/dist/errors.js CHANGED
@@ -3,6 +3,7 @@
3
3
  // and comes back with notDispatched set; anything else may have followed
4
4
  // execution and is never a safe automatic retry (docs/run-results.md).
5
5
  import { REFUSAL_NAMES, SESSION_END_NAMES } from "./tables.gen.js";
6
+ import { wireBytes } from "./wire.js";
6
7
  export class PlimsollError extends Error {
7
8
  code;
8
9
  /**
@@ -23,6 +24,7 @@ export class PlimsollError extends Error {
23
24
  * The record the daemon sends with a session call that may have run but ended in
24
25
  * an error (an unanswered record, as it came off the wire). The Session checks it
25
26
  * and keeps it in its chain, so the session goes on; the call's outcome is unknown.
27
+ * On a single run's error it is kept as it came and checked by nothing.
26
28
  */
27
29
  unanswered;
28
30
  /**
@@ -145,12 +147,37 @@ function decodeUnanswered(b) {
145
147
  let rec;
146
148
  for (let i = 0; i < b.length;) {
147
149
  const [tag, j] = varint(b, i);
148
- const [n, k] = varint(b, j);
149
- if ((tag & 7) !== 2 || k + n > b.length)
150
- throw new Error("bad detail");
151
- if (tag >>> 3 === 1)
150
+ const field = tag >>> 3;
151
+ const wire = tag & 7;
152
+ if (field === 1) {
153
+ if (wire !== 2)
154
+ throw new Error("UnansweredCall.record is not length-delimited");
155
+ const [n, k] = varint(b, j);
156
+ if (k + n > b.length)
157
+ throw new Error("bad detail");
152
158
  rec = decodeRecord(b.subarray(k, k + n));
153
- i = k + n;
159
+ i = k + n;
160
+ }
161
+ else if (wire === 0) {
162
+ // A field this client does not know is skipped by its wire type, as Go and Python
163
+ // skip it; every field used to be read as length-delimited, so a scalar a newer
164
+ // daemon adds dropped the record and stopped the session (review, 2026-10-26).
165
+ i = varint(b, j)[1];
166
+ }
167
+ else if (wire === 2) {
168
+ const [n, k] = varint(b, j);
169
+ if (k + n > b.length)
170
+ throw new Error("bad detail");
171
+ i = k + n;
172
+ }
173
+ else if (wire === 1 || wire === 5) {
174
+ i = j + (wire === 1 ? 8 : 4);
175
+ if (i > b.length)
176
+ throw new Error("bad detail");
177
+ }
178
+ else {
179
+ throw new Error("unexpected wire type");
180
+ }
154
181
  }
155
182
  return rec;
156
183
  }
@@ -214,7 +241,7 @@ export function errorFromWire(httpStatus, body, answer) {
214
241
  let unanswered;
215
242
  for (const d of Array.isArray(wire?.details) ? wire.details : []) {
216
243
  try {
217
- const bytes = Buffer.from(d.value ?? "", "base64");
244
+ const bytes = wireBytes(d.value);
218
245
  if (d.type === "plimsoll.v1.NotDispatched") {
219
246
  notDispatched = REFUSAL_NAMES[decodeDetail(bytes).reason] ?? "unknown";
220
247
  }
package/dist/record.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { WireRecord, WireRunResponse, WireSoftwareRule } from "./wire.ts";
1
+ import { type WireRecord, type WireRunResponse, type WireSoftwareRule } from "./wire.ts";
2
2
  /** The record encoding version this client computes and checks: every field, every time, for an answered call and an unanswered session call alike. */
3
3
  export declare const RECORD_VERSION = 4;
4
4
  /** A record's instance: "" (the provider names none) or a typed reference such as `k8s-pod-uid:` and a pod's UID. */
@@ -89,7 +89,7 @@ export declare function checkUnanswered(reqDigest: string, floor: string, rule:
89
89
  * both digests, the evidence the response repeats, the software rule and the
90
90
  * record's own digest. Returns the reason it does not check, or undefined.
91
91
  */
92
- export declare function checkRecord(reqDigest: string, rule: WireSoftwareRule | undefined, resp: WireRunResponse): {
92
+ export declare function checkRecord(reqDigest: string, kind: "javascript" | "project" | "cell", rule: WireSoftwareRule | undefined, resp: WireRunResponse): {
93
93
  record: RunRecord;
94
94
  } | {
95
95
  problem: string;
package/dist/record.js CHANGED
@@ -4,6 +4,7 @@
4
4
  import { createHash } from "node:crypto";
5
5
  import { PlimsollError } from "./errors.js";
6
6
  import { OUTCOMES, TIERS } from "./tables.gen.js";
7
+ import { wireBytes, WireShapeError } from "./wire.js";
7
8
  /** The record encoding version this client computes and checks: every field, every time, for an answered call and an unanswered session call alike. */
8
9
  export const RECORD_VERSION = 4;
9
10
  const INSTANCE_FORM = /^[a-z][a-z0-9-]{0,31}:[A-Za-z0-9._:-]{1,127}$/;
@@ -118,7 +119,7 @@ export function outcomeNumber(name) {
118
119
  return name;
119
120
  return OUTCOMES[name];
120
121
  }
121
- const b64 = (s) => Buffer.from(s ?? "", "base64");
122
+ const b64 = wireBytes;
122
123
  /** The result digest of a Run response as it arrived on the wire. */
123
124
  export function resultDigest(resp) {
124
125
  const e = new Encoder("plimsoll.run-result.v1");
@@ -169,8 +170,9 @@ export function resultDigest(resp) {
169
170
  e.bool("interpreter_ended", c.interpreterEnded ?? false);
170
171
  }
171
172
  else {
172
- // A module result, or none: this client sends neither, so the kind check
173
- // refuses the response before its digest matters.
173
+ // A module result, or none: this client sends neither, so the record check
174
+ // refuses the response as not of the request's kind, after this digest is
175
+ // computed and found not to match.
174
176
  e.str("kind", "");
175
177
  }
176
178
  return e.sum();
@@ -314,15 +316,33 @@ export function checkUnanswered(reqDigest, floor, rule, m, fingerprint, prevSeq,
314
316
  * both digests, the evidence the response repeats, the software rule and the
315
317
  * record's own digest. Returns the reason it does not check, or undefined.
316
318
  */
317
- export function checkRecord(reqDigest, rule, resp) {
319
+ export function checkRecord(reqDigest, kind, rule, resp) {
318
320
  if (!resp.record)
319
321
  return { problem: "the response carries no run record" };
320
322
  const r = recordFromWire(resp.record);
321
- const result = resultDigest(resp);
323
+ let result;
324
+ try {
325
+ result = resultDigest(resp);
326
+ }
327
+ catch (e) {
328
+ if (e instanceof WireShapeError)
329
+ return { problem: `the answer is malformed: ${e.message}` };
330
+ throw e;
331
+ }
322
332
  if (r.version !== RECORD_VERSION)
323
333
  return { problem: versionProblem(r.version) };
324
334
  if (r.unanswered !== "")
325
335
  return { problem: "an answered call's record states an unanswered code" };
336
+ // The kind, inside the check as in Go's record.check since 2026-10-26: a result of
337
+ // another kind fails here, before a session's chain moves on, so a daemon caught
338
+ // answering the wrong kind is not used further (review, 2026-10-26).
339
+ const kinds = ["javascript", "project", "module", "cell"].filter((k) => resp[k] !== undefined && resp[k] !== null);
340
+ if (kinds.length > 1)
341
+ return { problem: `the answer holds ${kinds.length} results; a run has one` };
342
+ if (kinds.length !== 1 || kinds[0] !== kind)
343
+ return { problem: `the daemon answered a ${kind} request with ${kinds.length === 1 ? kinds[0] : "no"} result` };
344
+ if (r.endedUnixMs < r.startedUnixMs)
345
+ return { problem: "the record ends before it starts" };
326
346
  if (r.requestSha256 !== reqDigest)
327
347
  return { problem: `request digest ${r.requestSha256}, the request sent digests to ${reqDigest}` };
328
348
  if (r.resultSha256 !== result)
package/dist/wire.d.ts CHANGED
@@ -192,3 +192,14 @@ export type WireError = {
192
192
  value?: string;
193
193
  }[];
194
194
  };
195
+ /** A bytes field that is not base64 text: the answer is not the protocol's. */
196
+ export declare class WireShapeError extends Error {
197
+ }
198
+ /**
199
+ * Decodes a bytes field of the daemon's answer. Only a string (or an absent field) is
200
+ * accepted: the answer is parsed JSON, and Buffer.from given an object with a
201
+ * "length" allocates that many bytes and reads each index, so a 30-byte
202
+ * {"length":2000000000} would cost 2 GB and a blocked event loop past the
203
+ * response's 32 MiB cap (Claude Security scan, 2026-10-27).
204
+ */
205
+ export declare function wireBytes(v: unknown): Buffer;
package/dist/wire.js CHANGED
@@ -3,4 +3,20 @@
3
3
  // JSON names; bytes are base64; 64-bit integers arrive as strings; enums as their
4
4
  // names; a field at its zero value is omitted, which is why everything read is
5
5
  // optional.
6
- export {};
6
+ /** A bytes field that is not base64 text: the answer is not the protocol's. */
7
+ export class WireShapeError extends Error {
8
+ }
9
+ /**
10
+ * Decodes a bytes field of the daemon's answer. Only a string (or an absent field) is
11
+ * accepted: the answer is parsed JSON, and Buffer.from given an object with a
12
+ * "length" allocates that many bytes and reads each index, so a 30-byte
13
+ * {"length":2000000000} would cost 2 GB and a blocked event loop past the
14
+ * response's 32 MiB cap (Claude Security scan, 2026-10-27).
15
+ */
16
+ export function wireBytes(v) {
17
+ if (v === undefined || v === null)
18
+ return Buffer.alloc(0);
19
+ if (typeof v !== "string")
20
+ throw new WireShapeError(`a bytes field holds a ${Array.isArray(v) ? "list" : typeof v}, not base64 text`);
21
+ return Buffer.from(v, "base64");
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plimsollmark/client",
3
- "version": "0.23.0",
3
+ "version": "0.24.0",
4
4
  "description": "TypeScript client for plimsolld, with AI SDK, Trigger.dev and Mastra executeCode add-ons",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/plimsollmark/plimsoll/tree/main/clients/typescript#readme",