agents 0.11.2 → 0.11.3

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/dist/client.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { r as Agent } from "./index-BBh8iKgk.js";
1
+ import { r as Agent } from "./index-D49HdAiY.js";
2
2
  import {
3
3
  i as SerializableValue,
4
4
  n as RPCMethod,
@@ -1640,6 +1640,11 @@ type FiberRecoveryContext = {
1640
1640
  /** Fiber ID. */ id: string /** Name passed to `runFiber`. */;
1641
1641
  name: string /** Last checkpoint data from `stash()`, or null if never stashed. */;
1642
1642
  snapshot: unknown | null;
1643
+ /**
1644
+ * Epoch milliseconds when the fiber row was inserted (when `runFiber`
1645
+ * started). Use `Date.now() - createdAt` to gate stale recoveries.
1646
+ */
1647
+ createdAt: number;
1643
1648
  [key: string]: unknown;
1644
1649
  };
1645
1650
  /**
@@ -1898,6 +1903,18 @@ declare class Agent<
1898
1903
  * @param excludeIds Additional connection IDs to exclude (e.g. the source)
1899
1904
  */
1900
1905
  private _broadcastProtocol;
1906
+ /**
1907
+ * When running as a facet, the parent DO owns the WebSocket registry
1908
+ * (`ctx.getWebSockets()`). Iterating from the child isolate throws
1909
+ * "Cannot perform I/O on behalf of a different Durable Object".
1910
+ * Downstream callers (e.g. chat-streaming paths) invoke
1911
+ * `this.broadcast()` directly, bypassing `_broadcastProtocol`'s
1912
+ * guard, so override at the base to catch every path.
1913
+ */
1914
+ broadcast(
1915
+ msg: string | ArrayBuffer | ArrayBufferView,
1916
+ without?: string[]
1917
+ ): void;
1901
1918
  private _setStateInternal;
1902
1919
  /**
1903
1920
  * Update the Agent's state
@@ -2369,12 +2386,23 @@ declare class Agent<
2369
2386
  */
2370
2387
  alarm(): Promise<void>;
2371
2388
  /**
2372
- * Marks this agent as running inside a facet (sub-agent). Once set,
2373
- * scheduling methods throw a clear error instead of crashing on
2374
- * `setAlarm()` (which is not supported in facets).
2375
- * @internal
2389
+ * Initialize this agent as a facet in a single RPC.
2390
+ *
2391
+ * Runs entirely inside the child's isolate, so every storage write
2392
+ * and `onStart()` I/O is owned by the child DO. This replaces the
2393
+ * previous "construct a Request in the parent DO and `stub.fetch()`
2394
+ * it on the child" handshake, whose native I/O was tied to the
2395
+ * parent and triggered "Cannot perform I/O on behalf of a different
2396
+ * Durable Object" on the child.
2397
+ *
2398
+ * Order matters: set `_isFacet` BEFORE triggering initialization, so
2399
+ * the first `onStart()` run (which calls `broadcastMcpServers`) sees
2400
+ * the flag and skips broadcasts that would touch the parent DO's
2401
+ * WebSocket registry.
2402
+ *
2403
+ * @internal Called by {@link subAgent}.
2376
2404
  */
2377
- _cf_markAsFacet(): Promise<void>;
2405
+ _cf_initAsFacet(name: string): Promise<void>;
2378
2406
  /**
2379
2407
  * Get or create a named sub-agent — a child Durable Object (facet)
2380
2408
  * with its own isolated SQLite storage running on the same machine.
@@ -2383,7 +2411,7 @@ declare class Agent<
2383
2411
  * entry point. The first call for a given name triggers the child's
2384
2412
  * `onStart()`. Subsequent calls return the existing instance.
2385
2413
  *
2386
- * @experimental Requires the `"experimental"` compatibility flag.
2414
+ * @experimental The API surface may change before stabilizing.
2387
2415
  *
2388
2416
  * @param cls The Agent subclass (must be exported from the worker)
2389
2417
  * @param name Unique name for this child instance
@@ -2405,7 +2433,7 @@ declare class Agent<
2405
2433
  * Pending RPC calls receive the reason as an error.
2406
2434
  * Transitively aborts the child's own children.
2407
2435
  *
2408
- * @experimental Requires the `"experimental"` compatibility flag.
2436
+ * @experimental The API surface may change before stabilizing.
2409
2437
  *
2410
2438
  * @param cls The Agent subclass used when creating the child
2411
2439
  * @param name Name of the child to abort
@@ -2416,7 +2444,7 @@ declare class Agent<
2416
2444
  * Delete a sub-agent: abort it if running, then permanently wipe its
2417
2445
  * storage. Transitively deletes the child's own children.
2418
2446
  *
2419
- * @experimental Requires the `"experimental"` compatibility flag.
2447
+ * @experimental The API surface may change before stabilizing.
2420
2448
  *
2421
2449
  * @param cls The Agent subclass used when creating the child
2422
2450
  * @param name Name of the child to delete
@@ -3080,4 +3108,4 @@ export {
3080
3108
  QueueItem as y,
3081
3109
  MCPClientOAuthResult as z
3082
3110
  };
3083
- //# sourceMappingURL=index-BBh8iKgk.d.ts.map
3111
+ //# sourceMappingURL=index-D49HdAiY.d.ts.map
package/dist/index.d.ts CHANGED
@@ -37,7 +37,7 @@ import {
37
37
  w as SqlError,
38
38
  x as RPCResponse,
39
39
  y as QueueItem
40
- } from "./index-BBh8iKgk.js";
40
+ } from "./index-D49HdAiY.js";
41
41
  import {
42
42
  n as AgentsOAuthProvider,
43
43
  r as DurableObjectOAuthClientProvider,
package/dist/index.js CHANGED
@@ -718,10 +718,23 @@ var Agent = class Agent extends Server {
718
718
  * @param excludeIds Additional connection IDs to exclude (e.g. the source)
719
719
  */
720
720
  _broadcastProtocol(msg, excludeIds = []) {
721
+ if (this._isFacet) return;
721
722
  const exclude = [...excludeIds];
722
723
  for (const conn of this.getConnections()) if (!this.isConnectionProtocolEnabled(conn)) exclude.push(conn.id);
723
724
  this.broadcast(msg, exclude);
724
725
  }
726
+ /**
727
+ * When running as a facet, the parent DO owns the WebSocket registry
728
+ * (`ctx.getWebSockets()`). Iterating from the child isolate throws
729
+ * "Cannot perform I/O on behalf of a different Durable Object".
730
+ * Downstream callers (e.g. chat-streaming paths) invoke
731
+ * `this.broadcast()` directly, bypassing `_broadcastProtocol`'s
732
+ * guard, so override at the base to catch every path.
733
+ */
734
+ broadcast(msg, without) {
735
+ if (this._isFacet) return;
736
+ super.broadcast(msg, without);
737
+ }
725
738
  _setStateInternal(nextState, source = "server") {
726
739
  this.validateStateChange(nextState, source);
727
740
  this._state = nextState;
@@ -1801,7 +1814,7 @@ var Agent = class Agent extends Server {
1801
1814
  if (this._runFiberRecoveryInProgress) return;
1802
1815
  this._runFiberRecoveryInProgress = true;
1803
1816
  try {
1804
- const rows = this.sql`SELECT id, name, snapshot FROM cf_agents_runs`;
1817
+ const rows = this.sql`SELECT id, name, snapshot, created_at FROM cf_agents_runs`;
1805
1818
  for (const row of rows) {
1806
1819
  if (this._runFiberActiveFibers.has(row.id)) continue;
1807
1820
  let snapshot = null;
@@ -1813,7 +1826,8 @@ var Agent = class Agent extends Server {
1813
1826
  const ctx = {
1814
1827
  id: row.id,
1815
1828
  name: row.name,
1816
- snapshot
1829
+ snapshot,
1830
+ createdAt: row.created_at
1817
1831
  };
1818
1832
  try {
1819
1833
  if (!await this._handleInternalFiberRecovery(ctx)) await this.onFiberRecovered(ctx);
@@ -1987,14 +2001,26 @@ var Agent = class Agent extends Server {
1987
2001
  await this._scheduleNextAlarm();
1988
2002
  }
1989
2003
  /**
1990
- * Marks this agent as running inside a facet (sub-agent). Once set,
1991
- * scheduling methods throw a clear error instead of crashing on
1992
- * `setAlarm()` (which is not supported in facets).
1993
- * @internal
2004
+ * Initialize this agent as a facet in a single RPC.
2005
+ *
2006
+ * Runs entirely inside the child's isolate, so every storage write
2007
+ * and `onStart()` I/O is owned by the child DO. This replaces the
2008
+ * previous "construct a Request in the parent DO and `stub.fetch()`
2009
+ * it on the child" handshake, whose native I/O was tied to the
2010
+ * parent and triggered "Cannot perform I/O on behalf of a different
2011
+ * Durable Object" on the child.
2012
+ *
2013
+ * Order matters: set `_isFacet` BEFORE triggering initialization, so
2014
+ * the first `onStart()` run (which calls `broadcastMcpServers`) sees
2015
+ * the flag and skips broadcasts that would touch the parent DO's
2016
+ * WebSocket registry.
2017
+ *
2018
+ * @internal Called by {@link subAgent}.
1994
2019
  */
1995
- async _cf_markAsFacet() {
2020
+ async _cf_initAsFacet(name) {
1996
2021
  this._isFacet = true;
1997
- await this.ctx.storage.put("cf_agents_is_facet", true);
2022
+ await Promise.all([this.ctx.storage.put("cf_agents_is_facet", true), this.ctx.storage.put("__ps_name", name)]);
2023
+ await this.__unsafe_ensureInitialized();
1998
2024
  }
1999
2025
  /**
2000
2026
  * Get or create a named sub-agent — a child Durable Object (facet)
@@ -2004,7 +2030,7 @@ var Agent = class Agent extends Server {
2004
2030
  * entry point. The first call for a given name triggers the child's
2005
2031
  * `onStart()`. Subsequent calls return the existing instance.
2006
2032
  *
2007
- * @experimental Requires the `"experimental"` compatibility flag.
2033
+ * @experimental The API surface may change before stabilizing.
2008
2034
  *
2009
2035
  * @param cls The Agent subclass (must be exported from the worker)
2010
2036
  * @param name Unique name for this child instance
@@ -2018,14 +2044,11 @@ var Agent = class Agent extends Server {
2018
2044
  */
2019
2045
  async subAgent(cls, name) {
2020
2046
  const ctx = this.ctx;
2021
- if (!ctx.facets || !ctx.exports) throw new Error("subAgent() requires the \"experimental\" compatibility flag. Add it to your wrangler.jsonc compatibility_flags.");
2047
+ if (!ctx.facets || !ctx.exports) throw new Error("subAgent() is not supported in this runtime — `ctx.facets` / `ctx.exports` are unavailable. Update to the latest `compatibility_date` in your wrangler.jsonc.");
2022
2048
  if (!ctx.exports[cls.name]) throw new Error(`Sub-agent class "${cls.name}" not found in worker exports. Make sure the class is exported from your worker entry point and that the export name matches the class name.`);
2023
2049
  const facetKey = `${cls.name}\0${name}`;
2024
2050
  const stub = ctx.facets.get(facetKey, () => ({ class: ctx.exports[cls.name] }));
2025
- const req = new Request("http://dummy-example.cloudflare.com/cdn-cgi/partyserver/set-name/");
2026
- req.headers.set("x-partykit-room", name);
2027
- await stub.fetch(req).then((res) => res.text());
2028
- await stub._cf_markAsFacet();
2051
+ await stub._cf_initAsFacet(name);
2029
2052
  return stub;
2030
2053
  }
2031
2054
  /**
@@ -2034,7 +2057,7 @@ var Agent = class Agent extends Server {
2034
2057
  * Pending RPC calls receive the reason as an error.
2035
2058
  * Transitively aborts the child's own children.
2036
2059
  *
2037
- * @experimental Requires the `"experimental"` compatibility flag.
2060
+ * @experimental The API surface may change before stabilizing.
2038
2061
  *
2039
2062
  * @param cls The Agent subclass used when creating the child
2040
2063
  * @param name Name of the child to abort
@@ -2042,7 +2065,7 @@ var Agent = class Agent extends Server {
2042
2065
  */
2043
2066
  abortSubAgent(cls, name, reason) {
2044
2067
  const ctx = this.ctx;
2045
- if (!ctx.facets) throw new Error("abortSubAgent() requires the \"experimental\" compatibility flag.");
2068
+ if (!ctx.facets) throw new Error("abortSubAgent() is not supported in this runtime — `ctx.facets` is unavailable. Update to the latest `compatibility_date` in your wrangler.jsonc.");
2046
2069
  const facetKey = `${cls.name}\0${name}`;
2047
2070
  ctx.facets.abort(facetKey, reason);
2048
2071
  }
@@ -2050,14 +2073,14 @@ var Agent = class Agent extends Server {
2050
2073
  * Delete a sub-agent: abort it if running, then permanently wipe its
2051
2074
  * storage. Transitively deletes the child's own children.
2052
2075
  *
2053
- * @experimental Requires the `"experimental"` compatibility flag.
2076
+ * @experimental The API surface may change before stabilizing.
2054
2077
  *
2055
2078
  * @param cls The Agent subclass used when creating the child
2056
2079
  * @param name Name of the child to delete
2057
2080
  */
2058
2081
  deleteSubAgent(cls, name) {
2059
2082
  const ctx = this.ctx;
2060
- if (!ctx.facets) throw new Error("deleteSubAgent() requires the \"experimental\" compatibility flag.");
2083
+ if (!ctx.facets) throw new Error("deleteSubAgent() is not supported in this runtime — `ctx.facets` is unavailable. Update to the latest `compatibility_date` in your wrangler.jsonc.");
2061
2084
  const facetKey = `${cls.name}\0${name}`;
2062
2085
  ctx.facets.delete(facetKey);
2063
2086
  }