@plimsollmark/client 0.14.0 → 0.15.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 +8 -2
- package/dist/client.d.ts +3 -1
- package/dist/client.js +39 -4
- package/dist/errors.d.ts +8 -1
- package/dist/errors.js +83 -1
- package/dist/record.d.ts +16 -1
- package/dist/record.js +34 -1
- package/dist/sandboxes.d.ts +11 -3
- package/dist/sandboxes.js +44 -11
- package/dist/wire.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -107,8 +107,14 @@ across conversations would show one user another's files. A language the daemon'
|
|
|
107
107
|
cannot run is refused before anything is sent.
|
|
108
108
|
|
|
109
109
|
Closing a sandbox checks the daemon's count of calls against the chain the client saw; a
|
|
110
|
-
mismatch means
|
|
111
|
-
a
|
|
110
|
+
mismatch means the daemon counted a call the client has no answer for (one that ended in an
|
|
111
|
+
abort, a timeout, a dropped connection or a proxy's error), or, if none did, that someone
|
|
112
|
+
else held the session ID. It goes to `onCloseError` (by default a warning) rather than being
|
|
113
|
+
dropped at teardown. A call that may have run but ended in an error comes with its record
|
|
114
|
+
(`PlimsollError.unanswered`, a version 3 record); the session checks it, keeps it in its
|
|
115
|
+
chain and goes on. A call whose answer never arrived carries none, so the client sends
|
|
116
|
+
nothing more on that session, and the conversation's next call opens a new sandbox and
|
|
117
|
+
reports `freshSandbox: true`.
|
|
112
118
|
|
|
113
119
|
The session ID is a capability. It lives in the client's memory and nowhere else: not in
|
|
114
120
|
a log line, not in a record, not in Trigger.dev's `chat.local` (which is serialized into
|
package/dist/client.d.ts
CHANGED
|
@@ -236,7 +236,9 @@ export declare function projectPayload(req: ProjectRequest, grantProfile: string
|
|
|
236
236
|
/**
|
|
237
237
|
* An open session. Calls are serialized here as they are on the daemon, so the
|
|
238
238
|
* chain this client tracks follows the daemon's; a record that does not continue
|
|
239
|
-
* it means someone else holding the session ID made a call.
|
|
239
|
+
* it means someone else holding the session ID made a call. A call that ends
|
|
240
|
+
* without an answer this client can check (any error not marked notDispatched)
|
|
241
|
+
* may have run, so the session refuses every later call, marked notDispatched.
|
|
240
242
|
*/
|
|
241
243
|
export declare class Session {
|
|
242
244
|
#private;
|
package/dist/client.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// isolation evidence against the caller's floor, and tracks a session's chain of
|
|
6
6
|
// records so a call it did not make is caught.
|
|
7
7
|
import { isIP } from "node:net";
|
|
8
|
-
import { checkRecord, requestDigest, sessionFingerprint, softwareRuleAllows, } from "./record.js";
|
|
8
|
+
import { checkRecord, checkUnanswered, requestDigest, sessionFingerprint, softwareRuleAllows, } from "./record.js";
|
|
9
9
|
import { errorFromWire, PlimsollError, sessionEndFromWire } from "./errors.js";
|
|
10
10
|
/** The wire protocol number this client speaks (protocol.Number in Go). */
|
|
11
11
|
export const PROTOCOL = 2;
|
|
@@ -372,7 +372,9 @@ function finish(resp, record, map, kind, floor) {
|
|
|
372
372
|
/**
|
|
373
373
|
* An open session. Calls are serialized here as they are on the daemon, so the
|
|
374
374
|
* chain this client tracks follows the daemon's; a record that does not continue
|
|
375
|
-
* it means someone else holding the session ID made a call.
|
|
375
|
+
* it means someone else holding the session ID made a call. A call that ends
|
|
376
|
+
* without an answer this client can check (any error not marked notDispatched)
|
|
377
|
+
* may have run, so the session refuses every later call, marked notDispatched.
|
|
376
378
|
*/
|
|
377
379
|
export class Session {
|
|
378
380
|
/** The SHA-256 of the session ID, as the session's records carry it. */
|
|
@@ -389,6 +391,10 @@ export class Session {
|
|
|
389
391
|
#calls = 0n;
|
|
390
392
|
#last = "";
|
|
391
393
|
#end;
|
|
394
|
+
// Set once a call ended without an answer this client could check: the daemon may
|
|
395
|
+
// have run it and moved its chain on, so every later call would run and then fail
|
|
396
|
+
// the chain check. They are refused here instead, before anything is sent.
|
|
397
|
+
#unknown;
|
|
392
398
|
#queue = Promise.resolve();
|
|
393
399
|
/** @internal */
|
|
394
400
|
constructor(client, m, rule) {
|
|
@@ -440,7 +446,36 @@ export class Session {
|
|
|
440
446
|
#call(payload, opts) {
|
|
441
447
|
const rule = mergeRules(this.#rule, validateRule(opts.software));
|
|
442
448
|
return this.#serial(async () => {
|
|
449
|
+
if (this.#unknown !== undefined) {
|
|
450
|
+
throw new PlimsollError("failed_precondition", `plimsoll: an earlier call of this session ended without an answer this client could check (${this.#unknown}); it may have run, so this client sends nothing more on the session: open a new one`, { notDispatched: "request" });
|
|
451
|
+
}
|
|
443
452
|
const env = this.#client.envelope(opts, rule);
|
|
453
|
+
const digest = requestDigest({ protocol: env.protocol, minimumIsolation: env.minimumIsolation ?? "", timeoutMs: env.timeoutMs ?? 0, softwareRule: rule, payload: digestPayload(payload) });
|
|
454
|
+
try {
|
|
455
|
+
return await this.#exchange(payload, opts, rule, env, digest);
|
|
456
|
+
}
|
|
457
|
+
catch (e) {
|
|
458
|
+
if (e instanceof PlimsollError && e.notDispatched)
|
|
459
|
+
throw e;
|
|
460
|
+
if (e instanceof PlimsollError && e.unanswered) {
|
|
461
|
+
// The call may have run, and the daemon chained its record: check it and keep
|
|
462
|
+
// it in the chain, so the session goes on and the call is not hidden.
|
|
463
|
+
const checked = checkUnanswered(digest, rule, e.unanswered, this.fingerprint, this.#calls, this.#last);
|
|
464
|
+
if ("problem" in checked) {
|
|
465
|
+
this.#unknown = checked.problem;
|
|
466
|
+
throw new PlimsollError("data_loss", `plimsoll: ${checked.problem}`, { cause: e });
|
|
467
|
+
}
|
|
468
|
+
this.#calls = checked.record.sequence;
|
|
469
|
+
this.#last = checked.record.sha256;
|
|
470
|
+
throw e;
|
|
471
|
+
}
|
|
472
|
+
this.#unknown = e instanceof Error ? e.message : String(e);
|
|
473
|
+
throw e;
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
async #exchange(payload, opts, rule, env, digest) {
|
|
478
|
+
{
|
|
444
479
|
let m;
|
|
445
480
|
try {
|
|
446
481
|
m = await this.#client.call("SessionRun", { ...env, sessionId: this.#id, ...payload }, opts.signal);
|
|
@@ -456,7 +491,7 @@ export class Session {
|
|
|
456
491
|
const end = sessionEndFromWire(m.ended);
|
|
457
492
|
if (end !== "open")
|
|
458
493
|
this.#end = { reason: end, detail: m.endDetail ?? "" };
|
|
459
|
-
const checked = checkRecord(
|
|
494
|
+
const checked = checkRecord(digest, env.protocol, rule, run);
|
|
460
495
|
if ("problem" in checked)
|
|
461
496
|
throw new PlimsollError("data_loss", `plimsoll: ${checked.problem}`, { result: run });
|
|
462
497
|
const r = checked.record;
|
|
@@ -466,7 +501,7 @@ export class Session {
|
|
|
466
501
|
this.#calls = r.sequence;
|
|
467
502
|
this.#last = r.sha256;
|
|
468
503
|
return { run, record: r };
|
|
469
|
-
}
|
|
504
|
+
}
|
|
470
505
|
}
|
|
471
506
|
/**
|
|
472
507
|
* Ends the session (or collects one that ended by itself) and checks the
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { WireError } from "./wire.ts";
|
|
1
|
+
import type { WireError, WireRecord } from "./wire.ts";
|
|
2
2
|
/** Why a request was refused before any code ran (plimsoll.v1.NotDispatchedReason). */
|
|
3
3
|
export type Refusal = "request" | "permission" | "protocol" | "unsupported" | "isolation" | "capacity" | "environment" | "unknown";
|
|
4
4
|
/** Why a session ended (plimsoll.v1.SessionEnd), "open" while it has not. */
|
|
@@ -27,6 +27,12 @@ export declare class PlimsollError extends Error {
|
|
|
27
27
|
* have executed, and this is what came back.
|
|
28
28
|
*/
|
|
29
29
|
readonly result: unknown;
|
|
30
|
+
/**
|
|
31
|
+
* The record the daemon sends with a session call that may have run but ended in
|
|
32
|
+
* an error (a version 3 record, as it came off the wire). The Session checks it
|
|
33
|
+
* and keeps it in its chain, so the session goes on; the call's outcome is unknown.
|
|
34
|
+
*/
|
|
35
|
+
readonly unanswered: WireRecord | undefined;
|
|
30
36
|
constructor(code: Code, message: string, opts?: {
|
|
31
37
|
notDispatched?: Refusal;
|
|
32
38
|
sessionEnded?: {
|
|
@@ -34,6 +40,7 @@ export declare class PlimsollError extends Error {
|
|
|
34
40
|
detail: string;
|
|
35
41
|
};
|
|
36
42
|
result?: unknown;
|
|
43
|
+
unanswered?: WireRecord;
|
|
37
44
|
cause?: unknown;
|
|
38
45
|
});
|
|
39
46
|
}
|
package/dist/errors.js
CHANGED
|
@@ -17,6 +17,12 @@ export class PlimsollError extends Error {
|
|
|
17
17
|
* have executed, and this is what came back.
|
|
18
18
|
*/
|
|
19
19
|
result;
|
|
20
|
+
/**
|
|
21
|
+
* The record the daemon sends with a session call that may have run but ended in
|
|
22
|
+
* an error (a version 3 record, as it came off the wire). The Session checks it
|
|
23
|
+
* and keeps it in its chain, so the session goes on; the call's outcome is unknown.
|
|
24
|
+
*/
|
|
25
|
+
unanswered;
|
|
20
26
|
constructor(code, message, opts = {}) {
|
|
21
27
|
super(message, opts.cause === undefined ? undefined : { cause: opts.cause });
|
|
22
28
|
this.name = "PlimsollError";
|
|
@@ -24,6 +30,7 @@ export class PlimsollError extends Error {
|
|
|
24
30
|
this.notDispatched = opts.notDispatched;
|
|
25
31
|
this.sessionEnded = opts.sessionEnded;
|
|
26
32
|
this.result = opts.result;
|
|
33
|
+
this.unanswered = opts.unanswered;
|
|
27
34
|
}
|
|
28
35
|
}
|
|
29
36
|
const REFUSALS = ["unknown", "request", "permission", "protocol", "unsupported", "isolation", "capacity", "environment"];
|
|
@@ -88,11 +95,83 @@ function decodeDetail(b) {
|
|
|
88
95
|
}
|
|
89
96
|
return { reason, detail };
|
|
90
97
|
}
|
|
98
|
+
// Reads one varint at b[i] as a bigint (an int64 or uint64 field); returns [value,
|
|
99
|
+
// next index].
|
|
100
|
+
function bigVarint(b, i) {
|
|
101
|
+
let v = 0n;
|
|
102
|
+
for (let shift = 0n; i < b.length && shift < 70n; shift += 7n) {
|
|
103
|
+
const c = b[i++];
|
|
104
|
+
v |= BigInt(c & 0x7f) << shift;
|
|
105
|
+
if ((c & 0x80) === 0)
|
|
106
|
+
return [v, i];
|
|
107
|
+
}
|
|
108
|
+
throw new Error("bad varint");
|
|
109
|
+
}
|
|
110
|
+
const RECORD_STRINGS = {
|
|
111
|
+
2: "requestSha256", 3: "resultSha256", 4: "provider", 5: "isolation", 6: "environment", 7: "policy",
|
|
112
|
+
10: "session", 12: "previousSha256", 13: "recordSha256", 14: "softwareIdentity", 15: "softwareRuleId", 16: "unanswered",
|
|
113
|
+
};
|
|
114
|
+
// Decodes an UnansweredCall detail (field 1, a RunRecord) by hand, into the record's
|
|
115
|
+
// proto3 JSON form, so the record checks exactly as one read off a JSON answer.
|
|
116
|
+
function decodeUnanswered(b) {
|
|
117
|
+
let rec;
|
|
118
|
+
for (let i = 0; i < b.length;) {
|
|
119
|
+
const [tag, j] = varint(b, i);
|
|
120
|
+
const [n, k] = varint(b, j);
|
|
121
|
+
if ((tag & 7) !== 2 || k + n > b.length)
|
|
122
|
+
throw new Error("bad detail");
|
|
123
|
+
if (tag >>> 3 === 1)
|
|
124
|
+
rec = decodeRecord(b.subarray(k, k + n));
|
|
125
|
+
i = k + n;
|
|
126
|
+
}
|
|
127
|
+
return rec;
|
|
128
|
+
}
|
|
129
|
+
function decodeRecord(b) {
|
|
130
|
+
const m = {};
|
|
131
|
+
for (let i = 0; i < b.length;) {
|
|
132
|
+
const [tag, j] = varint(b, i);
|
|
133
|
+
const field = Math.floor(tag / 8);
|
|
134
|
+
const wire = tag & 7;
|
|
135
|
+
i = j;
|
|
136
|
+
if (wire === 0) {
|
|
137
|
+
const [v, k] = bigVarint(b, i);
|
|
138
|
+
i = k;
|
|
139
|
+
if (field === 1)
|
|
140
|
+
m.version = Number(v);
|
|
141
|
+
else if (field === 8)
|
|
142
|
+
m.startedUnixMs = BigInt.asIntN(64, v).toString();
|
|
143
|
+
else if (field === 9)
|
|
144
|
+
m.endedUnixMs = BigInt.asIntN(64, v).toString();
|
|
145
|
+
else if (field === 11)
|
|
146
|
+
m.sequence = v.toString();
|
|
147
|
+
}
|
|
148
|
+
else if (wire === 2) {
|
|
149
|
+
const [n, k] = varint(b, i);
|
|
150
|
+
if (k + n > b.length)
|
|
151
|
+
throw new Error("bad length");
|
|
152
|
+
const name = RECORD_STRINGS[field];
|
|
153
|
+
if (name)
|
|
154
|
+
m[name] = new TextDecoder("utf-8", { fatal: true }).decode(b.subarray(k, k + n));
|
|
155
|
+
i = k + n;
|
|
156
|
+
}
|
|
157
|
+
else if (wire === 1) {
|
|
158
|
+
i += 8;
|
|
159
|
+
}
|
|
160
|
+
else if (wire === 5) {
|
|
161
|
+
i += 4;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
throw new Error("unexpected wire type");
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return m;
|
|
168
|
+
}
|
|
91
169
|
/** The error a Connect error body describes, with its plimsoll details restored. */
|
|
92
170
|
export function errorFromWire(httpStatus, body) {
|
|
93
171
|
const code = body?.code ?? httpStatusCode(httpStatus);
|
|
94
172
|
let notDispatched;
|
|
95
173
|
let sessionEnded;
|
|
174
|
+
let unanswered;
|
|
96
175
|
for (const d of body?.details ?? []) {
|
|
97
176
|
try {
|
|
98
177
|
const bytes = Buffer.from(d.value ?? "", "base64");
|
|
@@ -103,12 +182,15 @@ export function errorFromWire(httpStatus, body) {
|
|
|
103
182
|
const m = decodeDetail(bytes);
|
|
104
183
|
sessionEnded = { reason: sessionEndFromWire(m.reason), detail: m.detail };
|
|
105
184
|
}
|
|
185
|
+
else if (d.type === "plimsoll.v1.UnansweredCall") {
|
|
186
|
+
unanswered = decodeUnanswered(bytes);
|
|
187
|
+
}
|
|
106
188
|
}
|
|
107
189
|
catch {
|
|
108
190
|
// An undecodable detail states nothing; in particular not that nothing ran.
|
|
109
191
|
}
|
|
110
192
|
}
|
|
111
|
-
return new PlimsollError(code, body?.message ?? `HTTP ${httpStatus}`, { notDispatched, sessionEnded });
|
|
193
|
+
return new PlimsollError(code, body?.message ?? `HTTP ${httpStatus}`, { notDispatched, sessionEnded, unanswered: notDispatched ? undefined : unanswered });
|
|
112
194
|
}
|
|
113
195
|
// Connect's mapping for a response without a Connect error body.
|
|
114
196
|
function httpStatusCode(status) {
|
package/dist/record.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { WireRecord, WireRunResponse, WireSoftwareRule } from "./wire.ts";
|
|
2
|
-
/** The record encoding version this client computes and checks. */
|
|
2
|
+
/** The record encoding version of an answered call, which this client computes and checks. */
|
|
3
3
|
export declare const RECORD_VERSION = 2;
|
|
4
|
+
/** The record encoding version of a session call that may have run but ended in an error. */
|
|
5
|
+
export declare const UNANSWERED_RECORD_VERSION = 3;
|
|
4
6
|
/** The payload of a request as the digest sees it. */
|
|
5
7
|
export type DigestPayload = {
|
|
6
8
|
kind: "javascript";
|
|
@@ -53,6 +55,8 @@ export type RunRecord = {
|
|
|
53
55
|
session: string;
|
|
54
56
|
sequence: bigint;
|
|
55
57
|
previousSha256: string;
|
|
58
|
+
/** On a version 3 record, the Connect code a session call that may have run ended with. */
|
|
59
|
+
unanswered: string;
|
|
56
60
|
sha256: string;
|
|
57
61
|
};
|
|
58
62
|
export declare function recordFromWire(m: WireRecord): RunRecord;
|
|
@@ -66,6 +70,17 @@ export declare function sessionFingerprint(sessionId: string): string;
|
|
|
66
70
|
*/
|
|
67
71
|
export declare function softwareRuleId(rule: WireSoftwareRule | undefined): string;
|
|
68
72
|
export declare function softwareRuleAllows(rule: WireSoftwareRule | undefined, identity: string): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Checks the record the daemon sends with an unanswered session call's error
|
|
75
|
+
* (record.CheckUnanswered): version 3, no result digest, a Connect code, the digest of
|
|
76
|
+
* the request sent, its software rule, its own digest, and its place in the chain.
|
|
77
|
+
* Returns the reason it does not check, or the record.
|
|
78
|
+
*/
|
|
79
|
+
export declare function checkUnanswered(reqDigest: string, rule: WireSoftwareRule | undefined, m: WireRecord, fingerprint: string, prevSeq: bigint, prev: string): {
|
|
80
|
+
record: RunRecord;
|
|
81
|
+
} | {
|
|
82
|
+
problem: string;
|
|
83
|
+
};
|
|
69
84
|
/**
|
|
70
85
|
* Checks everything a record states that the caller can recompute: the version,
|
|
71
86
|
* both digests, the evidence the response repeats, the software rule and the
|
package/dist/record.js
CHANGED
|
@@ -2,8 +2,15 @@
|
|
|
2
2
|
// SHA-256 over a length-prefixed encoding, never over protobuf or JSON bytes.
|
|
3
3
|
// The golden vectors in record/record_test.go are checked in test/record.test.ts.
|
|
4
4
|
import { createHash } from "node:crypto";
|
|
5
|
-
/** The record encoding version this client computes and checks. */
|
|
5
|
+
/** The record encoding version of an answered call, which this client computes and checks. */
|
|
6
6
|
export const RECORD_VERSION = 2;
|
|
7
|
+
/** The record encoding version of a session call that may have run but ended in an error. */
|
|
8
|
+
export const UNANSWERED_RECORD_VERSION = 3;
|
|
9
|
+
const UNANSWERED_CODES = new Set([
|
|
10
|
+
"canceled", "unknown", "invalid_argument", "deadline_exceeded", "not_found", "already_exists", "permission_denied",
|
|
11
|
+
"resource_exhausted", "failed_precondition", "aborted", "out_of_range", "unimplemented", "internal", "unavailable",
|
|
12
|
+
"data_loss", "unauthenticated",
|
|
13
|
+
]);
|
|
7
14
|
class Encoder {
|
|
8
15
|
h = createHash("sha256");
|
|
9
16
|
constructor(domain) {
|
|
@@ -171,6 +178,7 @@ export function recordFromWire(m) {
|
|
|
171
178
|
session: m.session ?? "",
|
|
172
179
|
sequence: BigInt(m.sequence ?? 0),
|
|
173
180
|
previousSha256: m.previousSha256 ?? "",
|
|
181
|
+
unanswered: m.unanswered ?? "",
|
|
174
182
|
sha256: m.recordSha256 ?? "",
|
|
175
183
|
};
|
|
176
184
|
}
|
|
@@ -179,6 +187,8 @@ export function recordDigest(r) {
|
|
|
179
187
|
const e = new Encoder(`plimsoll.run-record.v${r.version}`);
|
|
180
188
|
e.str("request_sha256", r.requestSha256);
|
|
181
189
|
e.str("result_sha256", r.resultSha256);
|
|
190
|
+
if (r.version >= 3)
|
|
191
|
+
e.str("unanswered", r.unanswered);
|
|
182
192
|
e.str("provider", r.provider);
|
|
183
193
|
e.str("isolation", r.isolation);
|
|
184
194
|
e.str("environment", r.environment);
|
|
@@ -216,6 +226,29 @@ export function softwareRuleAllows(rule, identity) {
|
|
|
216
226
|
return true;
|
|
217
227
|
return identity !== "" && rule.identities.includes(identity);
|
|
218
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Checks the record the daemon sends with an unanswered session call's error
|
|
231
|
+
* (record.CheckUnanswered): version 3, no result digest, a Connect code, the digest of
|
|
232
|
+
* the request sent, its software rule, its own digest, and its place in the chain.
|
|
233
|
+
* Returns the reason it does not check, or the record.
|
|
234
|
+
*/
|
|
235
|
+
export function checkUnanswered(reqDigest, rule, m, fingerprint, prevSeq, prev) {
|
|
236
|
+
const r = recordFromWire(m);
|
|
237
|
+
if (r.version !== UNANSWERED_RECORD_VERSION)
|
|
238
|
+
return { problem: `an unanswered call's record is version ${r.version}` };
|
|
239
|
+
if (r.resultSha256 !== "" || !UNANSWERED_CODES.has(r.unanswered))
|
|
240
|
+
return { problem: "an unanswered call's record states a result or no error code" };
|
|
241
|
+
if (r.requestSha256 !== reqDigest)
|
|
242
|
+
return { problem: `request digest ${r.requestSha256}, the request sent digests to ${reqDigest}` };
|
|
243
|
+
if (r.softwareRuleId !== softwareRuleId(rule))
|
|
244
|
+
return { problem: "the record's software rule is not the request's" };
|
|
245
|
+
if (r.sha256 !== recordDigest(r))
|
|
246
|
+
return { problem: `record digest ${r.sha256}, its fields digest to ${recordDigest(r)}` };
|
|
247
|
+
if (r.session !== fingerprint || r.sequence !== prevSeq + 1n || r.previousSha256 !== prev) {
|
|
248
|
+
return { problem: `the session's chain is broken: call ${r.sequence} after "${r.previousSha256}", this client's last was call ${prevSeq}, "${prev}"` };
|
|
249
|
+
}
|
|
250
|
+
return { record: r };
|
|
251
|
+
}
|
|
219
252
|
/**
|
|
220
253
|
* Checks everything a record states that the caller can recompute: the version,
|
|
221
254
|
* both digests, the evidence the response repeats, the software rule and the
|
package/dist/sandboxes.d.ts
CHANGED
|
@@ -35,8 +35,10 @@ export type CodeSandboxesOptions = {
|
|
|
35
35
|
maxOutputChars?: number;
|
|
36
36
|
/**
|
|
37
37
|
* Called when closing a sandbox fails. A "data_loss" error here means the
|
|
38
|
-
* daemon counted calls this client
|
|
39
|
-
*
|
|
38
|
+
* daemon counted calls this client has no answer for: a call that ended
|
|
39
|
+
* without one (an abort, a timeout, a dropped connection, a proxy's error),
|
|
40
|
+
* or, if none did, someone else holding the session ID. Default: console.warn.
|
|
41
|
+
* The error never contains the session ID.
|
|
40
42
|
*/
|
|
41
43
|
onCloseError?: (key: string, err: unknown) => void;
|
|
42
44
|
};
|
|
@@ -87,7 +89,13 @@ export type ExecuteCodeOutput = {
|
|
|
87
89
|
};
|
|
88
90
|
/** @internal A fresh call on a daemon without projects: the code as a snippet, its last value printed. Node and QuickJS alike. */
|
|
89
91
|
export declare function snippetRunner(code: string): string;
|
|
90
|
-
/**
|
|
92
|
+
/**
|
|
93
|
+
* @internal A fresh run prints its last expression as a cell does, through these
|
|
94
|
+
* runners, and takes what a cell takes: top-level await, and imports from the work
|
|
95
|
+
* directory (Python's own imports first, in isolated mode, as the session kernel
|
|
96
|
+
* does; Node's require resolving from the work directory, and the image's baked
|
|
97
|
+
* packages above it).
|
|
98
|
+
*/
|
|
91
99
|
export declare const RUNNERS: Record<Language, {
|
|
92
100
|
path: string;
|
|
93
101
|
file: string;
|
package/dist/sandboxes.js
CHANGED
|
@@ -39,35 +39,63 @@ export function snippetRunner(code) {
|
|
|
39
39
|
"",
|
|
40
40
|
].join("\n");
|
|
41
41
|
}
|
|
42
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* @internal A fresh run prints its last expression as a cell does, through these
|
|
44
|
+
* runners, and takes what a cell takes: top-level await, and imports from the work
|
|
45
|
+
* directory (Python's own imports first, in isolated mode, as the session kernel
|
|
46
|
+
* does; Node's require resolving from the work directory, and the image's baked
|
|
47
|
+
* packages above it).
|
|
48
|
+
*/
|
|
43
49
|
export const RUNNERS = {
|
|
44
50
|
python: {
|
|
45
51
|
path: ".plimsoll/run.py",
|
|
46
52
|
file: ".plimsoll/cell.py",
|
|
47
|
-
step: "python3 .plimsoll/run.py",
|
|
53
|
+
step: "python3 -I .plimsoll/run.py",
|
|
48
54
|
source: [
|
|
49
|
-
"import ast",
|
|
55
|
+
"import ast, asyncio, inspect, sys",
|
|
56
|
+
"sys.path.insert(0, '')",
|
|
50
57
|
"src = open('.plimsoll/cell.py').read()",
|
|
51
58
|
"tree = ast.parse(src, 'cell', 'exec')",
|
|
52
59
|
"last = ast.Expression(tree.body.pop().value) if tree.body and isinstance(tree.body[-1], ast.Expr) else None",
|
|
53
60
|
"ns = {'__name__': '__main__'}",
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
" if
|
|
58
|
-
"
|
|
61
|
+
"flags = ast.PyCF_ALLOW_TOP_LEVEL_AWAIT",
|
|
62
|
+
"async def main():",
|
|
63
|
+
" r = eval(compile(tree, 'cell', 'exec', flags=flags), ns)",
|
|
64
|
+
" if inspect.iscoroutine(r):",
|
|
65
|
+
" await r",
|
|
66
|
+
" if last is not None:",
|
|
67
|
+
" value = eval(compile(last, 'cell', 'eval', flags=flags), ns)",
|
|
68
|
+
" if inspect.iscoroutine(value):",
|
|
69
|
+
" value = await value",
|
|
70
|
+
" if value is not None:",
|
|
71
|
+
" print(repr(value))",
|
|
72
|
+
"asyncio.run(main())",
|
|
59
73
|
"",
|
|
60
74
|
].join("\n"),
|
|
61
75
|
},
|
|
62
76
|
javascript: {
|
|
63
77
|
path: ".plimsoll/run.cjs",
|
|
64
78
|
file: ".plimsoll/cell.js",
|
|
65
|
-
step: "node .plimsoll/run.cjs",
|
|
79
|
+
step: "node --expose-internals .plimsoll/run.cjs",
|
|
66
80
|
source: [
|
|
67
81
|
"const fs = require('node:fs'), util = require('node:util'), vm = require('node:vm');",
|
|
68
|
-
"
|
|
82
|
+
"globalThis.require = require('node:module').createRequire(process.cwd() + '/');",
|
|
83
|
+
"let tla = null;",
|
|
84
|
+
"try { tla = require('internal/repl/await').processTopLevelAwait; } catch {}",
|
|
85
|
+
"const src = fs.readFileSync('.plimsoll/cell.js', 'utf8');",
|
|
86
|
+
"let wrapped = null;",
|
|
87
|
+
"if (tla) { try { wrapped = tla(src); } catch {} }",
|
|
88
|
+
"(async () => {",
|
|
89
|
+
" let v;",
|
|
90
|
+
" if (wrapped) {",
|
|
91
|
+
" const out = await vm.runInThisContext(wrapped, { filename: 'cell.js' });",
|
|
92
|
+
" v = out === undefined ? undefined : out.value;",
|
|
93
|
+
" } else {",
|
|
94
|
+
" v = vm.runInThisContext(src, { filename: 'cell.js' });",
|
|
95
|
+
" if (v instanceof Promise) v = await v;",
|
|
96
|
+
" }",
|
|
69
97
|
" if (v !== undefined) console.log(util.inspect(v, { depth: 4 }));",
|
|
70
|
-
"});",
|
|
98
|
+
"})().catch((e) => { console.error(e); process.exitCode = 1; });",
|
|
71
99
|
"",
|
|
72
100
|
].join("\n"),
|
|
73
101
|
},
|
|
@@ -169,6 +197,11 @@ export class CodeSandboxes {
|
|
|
169
197
|
this.#drop(key, session);
|
|
170
198
|
continue;
|
|
171
199
|
}
|
|
200
|
+
// A call that may have run without an answer leaves the session unusable
|
|
201
|
+
// (the Session refuses its later calls), so the next call opens a new
|
|
202
|
+
// sandbox and says so with freshSandbox.
|
|
203
|
+
if (session && !(e instanceof PlimsollError && e.notDispatched))
|
|
204
|
+
this.#drop(key, session);
|
|
172
205
|
throw e;
|
|
173
206
|
}
|
|
174
207
|
finally {
|
package/dist/wire.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plimsollmark/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "TypeScript client for plimsolld, with 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",
|