@fungi.computer/oyster 0.13.1 → 0.14.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.
@@ -42,7 +42,7 @@ export type CdpClient = Readonly<{
42
42
  timeoutMs?: number;
43
43
  signal?: AbortSignal;
44
44
  }>) => Promise<Json>;
45
- /** Resolves on the next matching event, or times out. */
45
+ /** Resolves on the next matching event, or rejects on timeout or closure. */
46
46
  waitForEvent: (method: string, options: Readonly<{
47
47
  sessionId?: string;
48
48
  timeoutMs: number;
@@ -56,11 +56,28 @@ function readCdpMessage(event) {
56
56
  /** Wrap one connected CDP socket with id-correlated calls and event waits. */
57
57
  export function createCdpClient(socket) {
58
58
  const pending = new Map();
59
- const waiters = [];
59
+ const waiters = new Set();
60
60
  const subscribers = new Set();
61
61
  let nextRequestNumber = 1;
62
62
  let closed = false;
63
+ const terminate = (message) => {
64
+ if (closed)
65
+ return;
66
+ closed = true;
67
+ const error = new CloudBrowserUpstreamError({
68
+ op: "cdp.socket",
69
+ status: null,
70
+ message,
71
+ });
72
+ for (const call of pending.values())
73
+ call.reject(error);
74
+ for (const waiter of waiters)
75
+ waiter.reject(error);
76
+ subscribers.clear();
77
+ };
63
78
  socket.addEventListener("message", (event) => {
79
+ if (closed)
80
+ return;
64
81
  const frame = readCdpMessage(event);
65
82
  if (frame.kind === "unreadable")
66
83
  return;
@@ -69,7 +86,6 @@ export function createCdpClient(socket) {
69
86
  const waiting = pending.get(message.id);
70
87
  if (waiting === undefined)
71
88
  return;
72
- pending.delete(message.id);
73
89
  if (message.error !== undefined)
74
90
  waiting.reject(new CloudBrowserUpstreamError({
75
91
  op: "cdp.call",
@@ -85,28 +101,19 @@ export function createCdpClient(socket) {
85
101
  for (const subscriber of subscribers)
86
102
  if (subscriber.method === message.method)
87
103
  subscriber.listener(message.params ?? null, message.sessionId);
88
- for (let index = waiters.length - 1; index >= 0; index -= 1) {
89
- const waiter = waiters[index];
90
- if (waiter?.method === message.method &&
104
+ for (const waiter of waiters) {
105
+ if (waiter.method === message.method &&
91
106
  (waiter.sessionId === undefined ||
92
107
  waiter.sessionId === message.sessionId)) {
93
- waiters.splice(index, 1);
94
108
  waiter.resolve(message.params ?? null);
95
109
  }
96
110
  }
97
111
  });
98
112
  socket.addEventListener("close", () => {
99
- closed = true;
100
- for (const { reject } of pending.values())
101
- reject(new CloudBrowserUpstreamError({
102
- op: "cdp.socket",
103
- status: null,
104
- message: "browser CDP socket closed",
105
- }));
106
- pending.clear();
113
+ terminate("browser CDP socket closed");
107
114
  });
108
115
  socket.addEventListener("error", () => {
109
- closed = true;
116
+ terminate("browser CDP socket failed");
110
117
  });
111
118
  return {
112
119
  call: (method, params, options) => new Promise((resolve, reject) => {
@@ -118,66 +125,95 @@ export function createCdpClient(socket) {
118
125
  }));
119
126
  return;
120
127
  }
128
+ if (options?.signal?.aborted) {
129
+ reject(new CloudBrowserTimeout({ op: method, timeoutMs: 0 }));
130
+ return;
131
+ }
121
132
  const requestNumber = nextRequestNumber;
122
133
  nextRequestNumber += 1;
123
134
  const timeoutMs = options?.timeoutMs ?? 15_000;
124
135
  const timer = setTimeout(() => {
125
- pending.delete(requestNumber);
126
- reject(new CloudBrowserTimeout({ op: method, timeoutMs }));
136
+ call.reject(new CloudBrowserTimeout({ op: method, timeoutMs }));
127
137
  }, timeoutMs);
128
138
  const onAbort = () => {
139
+ call.reject(new CloudBrowserTimeout({ op: method, timeoutMs: 0 }));
140
+ };
141
+ const cleanup = () => {
129
142
  pending.delete(requestNumber);
130
143
  clearTimeout(timer);
131
- reject(new CloudBrowserTimeout({ op: method, timeoutMs: 0 }));
144
+ options?.signal?.removeEventListener("abort", onAbort);
132
145
  };
133
- options?.signal?.addEventListener("abort", onAbort, { once: true });
134
- pending.set(requestNumber, {
146
+ const call = {
135
147
  resolve: (value) => {
136
- clearTimeout(timer);
137
- options?.signal?.removeEventListener("abort", onAbort);
148
+ cleanup();
138
149
  resolve(value);
139
150
  },
140
151
  reject: (error) => {
141
- clearTimeout(timer);
142
- options?.signal?.removeEventListener("abort", onAbort);
152
+ cleanup();
143
153
  reject(error);
144
154
  },
145
- });
155
+ };
156
+ pending.set(requestNumber, call);
157
+ options?.signal?.addEventListener("abort", onAbort, { once: true });
146
158
  const frame = { id: requestNumber, method, params };
147
- socket.send(JSON.stringify(options?.sessionId === undefined
148
- ? frame
149
- : { ...frame, sessionId: options.sessionId }));
159
+ try {
160
+ socket.send(JSON.stringify(options?.sessionId === undefined
161
+ ? frame
162
+ : { ...frame, sessionId: options.sessionId }));
163
+ }
164
+ catch (cause) {
165
+ cleanup();
166
+ throw new CloudBrowserUpstreamError({
167
+ op: method,
168
+ status: null,
169
+ message: "Could not send the browser CDP command",
170
+ cause,
171
+ });
172
+ }
150
173
  }),
151
174
  waitForEvent: (method, options) => new Promise((resolve, reject) => {
175
+ if (closed) {
176
+ reject(new CloudBrowserUpstreamError({
177
+ op: method,
178
+ status: null,
179
+ message: "browser CDP socket is closed",
180
+ }));
181
+ return;
182
+ }
183
+ const cleanup = () => {
184
+ waiters.delete(waiter);
185
+ clearTimeout(timer);
186
+ };
152
187
  const waiter = {
153
188
  method,
154
189
  sessionId: options.sessionId,
155
190
  resolve: (value) => {
156
- clearTimeout(timer);
191
+ cleanup();
157
192
  resolve(value);
158
193
  },
194
+ reject: (error) => {
195
+ cleanup();
196
+ reject(error);
197
+ },
159
198
  };
160
199
  const timer = setTimeout(() => {
161
- const index = waiters.indexOf(waiter);
162
- if (index >= 0)
163
- waiters.splice(index, 1);
164
- reject(new CloudBrowserTimeout({
200
+ waiter.reject(new CloudBrowserTimeout({
165
201
  op: method,
166
202
  timeoutMs: options.timeoutMs,
167
203
  }));
168
204
  }, options.timeoutMs);
169
- waiters.push(waiter);
205
+ waiters.add(waiter);
170
206
  }),
171
207
  on: (method, listener) => {
172
208
  const subscriber = { method, listener };
173
- subscribers.add(subscriber);
209
+ if (!closed)
210
+ subscribers.add(subscriber);
174
211
  return () => {
175
212
  subscribers.delete(subscriber);
176
213
  };
177
214
  },
178
215
  close: () => {
179
- closed = true;
180
- subscribers.clear();
216
+ terminate("browser CDP socket closed");
181
217
  socket.close();
182
218
  },
183
219
  };
@@ -22,6 +22,8 @@ export declare class CloudBrowserUpstreamError extends CloudBrowserUpstreamError
22
22
  readonly op: string;
23
23
  readonly status: number | null;
24
24
  readonly message: string;
25
+ /** Original transport failure retained for host diagnostics. */
26
+ readonly cause?: unknown;
25
27
  }> {
26
28
  }
27
29
  declare const CloudBrowserTimeout_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
@@ -40,7 +40,11 @@ export type LeaseRegistry = Readonly<{
40
40
  /** Turn a reservation into an open lease. */
41
41
  /** `openedAt` is when the provider session was requested: lifetime and billing start there. */
42
42
  register: (leaseId: string, owner: LeaseOwner, openedAt: number) => void;
43
- /** Throws `CloudBrowserLeaseNotFound` for a wrong owner, unknown, or expired lease. */
43
+ /**
44
+ * Throws `CloudBrowserLeaseNotFound` for a wrong owner, unknown, or expired
45
+ * lease. Expired records remain until the browser owner releases them for
46
+ * provider cleanup and settlement.
47
+ */
44
48
  touch: (leaseId: string, owner: LeaseOwner) => void;
45
49
  release: (leaseId: string) => void;
46
50
  /** Leases past their idle timeout or max lifetime, for a reap sweep. */
@@ -49,10 +49,8 @@ export function createLeaseRegistry(config) {
49
49
  if (lease === undefined)
50
50
  throw new CloudBrowserLeaseNotFound({ leaseId });
51
51
  const at = now();
52
- if (isExpired(lease, at)) {
53
- leases.delete(leaseId);
52
+ if (isExpired(lease, at))
54
53
  throw new CloudBrowserLeaseNotFound({ leaseId });
55
- }
56
54
  if (!ownedBy(lease, owner))
57
55
  throw new CloudBrowserLeaseNotFound({ leaseId });
58
56
  if (lease.paused)
@@ -64,10 +62,8 @@ export function createLeaseRegistry(config) {
64
62
  if (lease === undefined)
65
63
  throw new CloudBrowserLeaseNotFound({ leaseId });
66
64
  const at = now();
67
- if (isExpired(lease, at)) {
68
- leases.delete(leaseId);
65
+ if (isExpired(lease, at))
69
66
  throw new CloudBrowserLeaseNotFound({ leaseId });
70
- }
71
67
  if (lease.teamId !== teamId)
72
68
  throw new CloudBrowserLeaseNotFound({ leaseId });
73
69
  lease.lastActiveAt = at;
@@ -14544,21 +14544,6 @@ function makeIsGreaterThanOrEqualTo(options) {
14544
14544
  });
14545
14545
  };
14546
14546
  }
14547
- function makeIsLessThanOrEqualTo(options) {
14548
- const lte = isLessThanOrEqualTo(options.order);
14549
- const formatter = options.formatter ?? format;
14550
- return (maximum, annotations) => {
14551
- return makeFilter2((input) => lte(input, maximum), {
14552
- expected: `a value less than or equal to ${formatter(maximum)}`,
14553
- arbitraryConstraint: {
14554
- order: options.order,
14555
- maximum
14556
- },
14557
- ...options.annotate?.(maximum),
14558
- ...annotations
14559
- });
14560
- };
14561
- }
14562
14547
  function makeIsBetween(deriveOptions) {
14563
14548
  const greaterThanOrEqualTo = isGreaterThanOrEqualTo(deriveOptions.order);
14564
14549
  const greaterThan = isGreaterThan(deriveOptions.order);
@@ -14626,23 +14611,6 @@ var isGreaterThanOrEqualTo2 = /* @__PURE__ */ makeIsGreaterThanOrEqualTo({
14626
14611
  })
14627
14612
  })
14628
14613
  });
14629
- var isLessThanOrEqualTo2 = /* @__PURE__ */ makeIsLessThanOrEqualTo({
14630
- order: Number2,
14631
- annotate: (maximum) => ({
14632
- representation: {
14633
- id: "effect/schema/isLessThanOrEqualTo",
14634
- payload: {
14635
- maximum: encodeNumberPayload(maximum)
14636
- }
14637
- },
14638
- toJsonSchema: () => ({
14639
- maximum
14640
- }),
14641
- toCode: () => ({
14642
- runtime: `Schema.isLessThanOrEqualTo(${format(maximum)})`
14643
- })
14644
- })
14645
- });
14646
14614
  var isBetween = /* @__PURE__ */ makeIsBetween({
14647
14615
  order: Number2,
14648
14616
  annotate: (options) => {
@@ -16838,14 +16806,14 @@ var $ZodObjectJIT = /* @__PURE__ */ $constructor("$ZodObjectJIT", (inst, def) =>
16838
16806
  return `shape[${k}]._zod.run({ value: input[${k}], issues: [] }, ctx)`;
16839
16807
  };
16840
16808
  doc.write(`const input = payload.value;`);
16841
- const ids2 = /* @__PURE__ */ Object.create(null);
16809
+ const ids = /* @__PURE__ */ Object.create(null);
16842
16810
  let counter = 0;
16843
16811
  for (const key of normalized.keys) {
16844
- ids2[key] = `key_${counter++}`;
16812
+ ids[key] = `key_${counter++}`;
16845
16813
  }
16846
16814
  doc.write(`const newResult = {};`);
16847
16815
  for (const key of normalized.keys) {
16848
- const id = ids2[key];
16816
+ const id = ids[key];
16849
16817
  const k = esc(key);
16850
16818
  const schema2 = shape[key];
16851
16819
  const isOptionalIn = schema2?._zod?.optin === "optional";
@@ -19938,7 +19906,11 @@ var gillResponseSchema = Union2([
19938
19906
  state: Literal2("error"),
19939
19907
  rows: Tuple([]),
19940
19908
  /** Host-owned permanent execution refusal; an ordinary error may still be refreshed. */
19941
- reason: optional2(Literals(["exec_isolation_required", "params_required"])),
19909
+ reason: optional2(Literals([
19910
+ "exec_isolation_required",
19911
+ "params_required",
19912
+ "disabled"
19913
+ ])),
19942
19914
  error: String4.pipe(check(isMinLength(1)), check(isMaxLength(512))),
19943
19915
  /** The host supplies the same-placement picker and this App's launch. */
19944
19916
  recovery: optional2(Union2([
@@ -21689,73 +21661,6 @@ var publicRouteConfigurationSchema = Struct({
21689
21661
  // src/build/candidate-cli.ts
21690
21662
  import { readFile as readFile6 } from "node:fs/promises";
21691
21663
 
21692
- // ../carrier/dist/internal/post-contract.js
21693
- var CursorResultSchema = Struct({
21694
- status: Literals(["accepted", "duplicate"]),
21695
- cursor: Int.check(isGreaterThanOrEqualTo2(0))
21696
- });
21697
- var PostDeliveryResultSchema = Union2([
21698
- Literals(["accepted", "duplicate", "refused"]),
21699
- CursorResultSchema,
21700
- Struct({
21701
- status: Literals(["accepted", "duplicate", "refused"]),
21702
- receipt: Json2
21703
- })
21704
- ]);
21705
- var PostError = class extends TaggedError2("PostError") {
21706
- constructor(message, reason) {
21707
- super(reason === void 0 ? { message } : { message, reason });
21708
- }
21709
- };
21710
-
21711
- // ../carrier/dist/post.js
21712
- var Key = String4.check(isMinLength(1), isMaxLength(256));
21713
- var JobPayload = Struct({ postKey: Key });
21714
- var PageLimit = Int.check(isGreaterThanOrEqualTo2(1), isLessThanOrEqualTo2(64));
21715
- var maxRouteDeadlineMs = 7 * 24 * 60 * 60 * 1e3;
21716
- var ids = defineIdFamily({
21717
- family: "post",
21718
- version: 1,
21719
- kinds: { work: { prefix: "post_work" } }
21720
- });
21721
- var PostEnvelopeSchema = Struct({
21722
- key: Key,
21723
- kind: Key,
21724
- address: Unknown2,
21725
- event: Unknown2
21726
- });
21727
- var PostRowSchema = fromJsonString2(Union2([
21728
- Struct({
21729
- state: Literal2("pending"),
21730
- envelope: PostEnvelopeSchema,
21731
- attempts: Int.check(isGreaterThanOrEqualTo2(0)),
21732
- nextAttemptAt: Number5.check(isGreaterThanOrEqualTo2(0)),
21733
- deadlineAt: optional2(Number5.check(isGreaterThanOrEqualTo2(0))),
21734
- jobId: optional2(Key),
21735
- delivery: optional2(PostEnvelopeSchema),
21736
- nextKey: optional2(Key)
21737
- }),
21738
- Struct({
21739
- state: Literal2("delivered"),
21740
- receipt: optional2(Json2),
21741
- nextKey: optional2(Key)
21742
- }),
21743
- Struct({
21744
- state: Literal2("dead"),
21745
- envelope: PostEnvelopeSchema,
21746
- attempts: Int.check(isGreaterThanOrEqualTo2(0)),
21747
- reason: Literals(["refused", "exhausted", "expired"]),
21748
- deadlineAt: optional2(Number5.check(isGreaterThanOrEqualTo2(0))),
21749
- receipt: optional2(Json2),
21750
- delivery: optional2(PostEnvelopeSchema),
21751
- nextKey: optional2(Key)
21752
- })
21753
- ]));
21754
- var StoredRow = Struct({ key: String4, value: String4 });
21755
- var DueRow = Struct({
21756
- due: NullOr(Number5.check(isGreaterThanOrEqualTo2(0)))
21757
- });
21758
-
21759
21664
  // ../bird-dog/dist/effect.js
21760
21665
  var Identifier = String4.check(isMinLength(1), isMaxLength(256));
21761
21666
  var BIRD_DOG_MAIL_CONTENT_MAX_BYTES = 256 * 1024;
@@ -21862,7 +21767,8 @@ var BirdDogAgentWorkSchema = Struct({
21862
21767
  sessionId: Identifier,
21863
21768
  commandFactId: Identifier,
21864
21769
  recovery: optional2(Struct({
21865
- maxRecoveries: Int.check(isGreaterThanOrEqualTo2(0))
21770
+ maxRecoveries: Int.check(isGreaterThanOrEqualTo2(0)),
21771
+ windowMs: optionalKey2(Int.check(isGreaterThan2(0)))
21866
21772
  }))
21867
21773
  });
21868
21774
  var BirdDogAgentJobPayloadSchema = Struct({