@deepagents/context 7.10.0 → 7.10.1

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/index.js CHANGED
@@ -158,7 +158,6 @@ import {
158
158
  import { styleText } from "node:util";
159
159
  import z from "zod";
160
160
  import {
161
- createRepairToolCall,
162
161
  withHostOnlyToolMetadata
163
162
  } from "@deepagents/agent";
164
163
 
@@ -387,10 +386,7 @@ var Agent = class _Agent {
387
386
  runtimeContext: this.#options.runtimeContext,
388
387
  model: this.#options.model,
389
388
  instructions: systemPrompt,
390
- messages: await convertToModelMessages(messages, {
391
- ignoreIncompleteToolCalls: true,
392
- tools: this.tools
393
- }),
389
+ messages: await toModelPrompt(messages, this.tools),
394
390
  stopWhen: this.#options.stopWhen ?? DEFAULT_STOP_WHEN,
395
391
  prepareStep: this.#withPrepareStep(
396
392
  this.#options.context.createReminderSession({
@@ -402,10 +398,6 @@ var Agent = class _Agent {
402
398
  tools: this.tools,
403
399
  toolsContext: options?.toolsContext,
404
400
  experimental_toolCallers: this.#options.experimental_toolCallers,
405
- repairToolCall: createRepairToolCall(
406
- this.#options.model,
407
- options?.abortSignal
408
- ),
409
401
  toolChoice: this.#options.toolChoice
410
402
  });
411
403
  }
@@ -508,11 +500,7 @@ var Agent = class _Agent {
508
500
  runtimeContext: this.#options.runtimeContext,
509
501
  model,
510
502
  instructions: systemPrompt,
511
- messages: await convertToModelMessages(messages, {
512
- ignoreIncompleteToolCalls: true,
513
- tools: this.tools
514
- }),
515
- repairToolCall: createRepairToolCall(model, config?.abortSignal),
503
+ messages: await toModelPrompt(messages, this.tools),
516
504
  stopWhen: this.#options.stopWhen ?? DEFAULT_STOP_WHEN,
517
505
  prepareStep,
518
506
  experimental_transform: config?.transform ?? smoothStream(),
@@ -792,15 +780,8 @@ function structuredOutput(options) {
792
780
  telemetry: options.telemetry,
793
781
  model: options.model,
794
782
  instructions: systemPrompt,
795
- messages: await convertToModelMessages(messages, {
796
- ignoreIncompleteToolCalls: true,
797
- tools
798
- }),
783
+ messages: await toModelPrompt(messages, tools),
799
784
  stopWhen: options.stopWhen ?? DEFAULT_STOP_WHEN,
800
- repairToolCall: createRepairToolCall(
801
- options.model,
802
- callOptions?.abortSignal
803
- ),
804
785
  toolsContext: callOptions?.toolsContext,
805
786
  output: Output.object({ schema: options.schema }),
806
787
  tools
@@ -830,14 +811,7 @@ function structuredOutput(options) {
830
811
  telemetry: options.telemetry,
831
812
  model: options.model,
832
813
  instructions: systemPrompt,
833
- repairToolCall: createRepairToolCall(
834
- options.model,
835
- callOptions?.abortSignal
836
- ),
837
- messages: await convertToModelMessages(messages, {
838
- ignoreIncompleteToolCalls: true,
839
- tools
840
- }),
814
+ messages: await toModelPrompt(messages, tools),
841
815
  stopWhen: options.stopWhen ?? DEFAULT_STOP_WHEN,
842
816
  experimental_transform: callOptions?.transform ?? smoothStream(),
843
817
  toolsContext: callOptions?.toolsContext,
@@ -847,6 +821,44 @@ function structuredOutput(options) {
847
821
  }
848
822
  };
849
823
  }
824
+ async function toModelPrompt(messages, tools) {
825
+ return withReasoningItemsOnce(
826
+ await convertToModelMessages(messages, {
827
+ ignoreIncompleteToolCalls: true,
828
+ tools
829
+ })
830
+ );
831
+ }
832
+ function withReasoningItemsOnce(messages) {
833
+ const contentByItemId = /* @__PURE__ */ new Map();
834
+ return messages.flatMap((message2) => {
835
+ if (message2.role !== "assistant" || typeof message2.content === "string") {
836
+ return [message2];
837
+ }
838
+ const content = [];
839
+ for (const part of message2.content) {
840
+ const itemId = reasoningItemIdOf2(part);
841
+ const owner = itemId === void 0 ? void 0 : contentByItemId.get(itemId);
842
+ if (itemId === void 0) {
843
+ content.push(part);
844
+ } else if (owner === void 0) {
845
+ contentByItemId.set(itemId, content);
846
+ content.push(part);
847
+ } else {
848
+ const lastOfItem = owner.findLastIndex(
849
+ (other) => reasoningItemIdOf2(other) === itemId
850
+ );
851
+ owner.splice(lastOfItem + 1, 0, part);
852
+ }
853
+ }
854
+ return content.length === 0 ? [] : [{ ...message2, content }];
855
+ });
856
+ }
857
+ function reasoningItemIdOf2(part) {
858
+ if (part.type !== "reasoning") return void 0;
859
+ const itemId = part.providerOptions?.openai?.itemId;
860
+ return typeof itemId === "string" ? itemId : void 0;
861
+ }
850
862
  function chunkId(chunk) {
851
863
  if (typeof chunk !== "object" || chunk === null || !("id" in chunk)) {
852
864
  return void 0;
@@ -876,6 +888,7 @@ function writeText(writer, text3, openPartId) {
876
888
  import {
877
889
  APICallError as APICallError2,
878
890
  InvalidToolInputError,
891
+ NoOutputGeneratedError,
879
892
  NoSuchToolError,
880
893
  ToolCallRepairError,
881
894
  createUIMessageStream as createUIMessageStream2,
@@ -4194,8 +4207,10 @@ var OpenChat = class {
4194
4207
  var ContextEngine = class _ContextEngine {
4195
4208
  /** Non-message fragments (role, hints, etc.) - not persisted in graph */
4196
4209
  #fragments = [];
4197
- /** Pending message fragments to be added to graph */
4210
+ /** Message fragments not yet committed to the graph, including those a save() is writing */
4198
4211
  #pendingMessages = [];
4212
+ /** Pending fragments an in-flight save() is writing; they stay pending until it commits */
4213
+ #savingMessages = /* @__PURE__ */ new Set();
4199
4214
  #store;
4200
4215
  #chatId;
4201
4216
  #userId;
@@ -4402,19 +4417,19 @@ var ContextEngine = class _ContextEngine {
4402
4417
  */
4403
4418
  async getMessages() {
4404
4419
  const open3 = await this.#open();
4420
+ const headMessageId = open3.branch.headMessageId;
4421
+ const pending = [...this.#pendingMessages];
4405
4422
  const messages = [];
4406
4423
  const indexById = /* @__PURE__ */ new Map();
4407
- if (open3.branch.headMessageId) {
4408
- const chain = await this.#store.getMessageChain(
4409
- open3.branch.headMessageId
4410
- );
4424
+ if (headMessageId) {
4425
+ const chain = await this.#store.getMessageChain(headMessageId);
4411
4426
  for (const msg of chain) {
4412
4427
  const data = requireUIMessage(msg.data, `Stored message "${msg.id}"`);
4413
4428
  if (isEmptyAssistantPlaceholder(data)) continue;
4414
4429
  indexById.set(data.id, messages.push(data) - 1);
4415
4430
  }
4416
4431
  }
4417
- for (const fragment2 of this.#pendingMessages) {
4432
+ for (const fragment2 of pending) {
4418
4433
  if (!fragment2.codec) {
4419
4434
  throw new Error(`Fragment "${fragment2.name}" is missing codec.`);
4420
4435
  }
@@ -4533,6 +4548,9 @@ var ContextEngine = class _ContextEngine {
4533
4548
  * Each message is added as a node with parentId pointing to the previous message.
4534
4549
  * The branch head is updated to point to the last message.
4535
4550
  *
4551
+ * The messages stay pending until the head commits, so `getMessages()`
4552
+ * during the save still returns them; a failed save leaves them pending.
4553
+ *
4536
4554
  * @example
4537
4555
  * ```ts
4538
4556
  * context.set(user('Hello'));
@@ -4543,11 +4561,14 @@ var ContextEngine = class _ContextEngine {
4543
4561
  */
4544
4562
  async save(options) {
4545
4563
  const open3 = await this.#open();
4546
- if (this.#pendingMessages.length === 0) {
4564
+ const taken = this.#pendingMessages.filter(
4565
+ (fragment2) => !this.#savingMessages.has(fragment2)
4566
+ );
4567
+ if (taken.length === 0) {
4547
4568
  return { headMessageId: open3.branch.headMessageId ?? void 0 };
4548
4569
  }
4549
- const pending = this.#pendingMessages;
4550
- this.#pendingMessages = [];
4570
+ for (const fragment2 of taken) this.#savingMessages.add(fragment2);
4571
+ const pending = [...taken];
4551
4572
  const pipeline = new SavePipeline(
4552
4573
  this.#asSavePipelineEngine(open3),
4553
4574
  pending,
@@ -4556,10 +4577,14 @@ var ContextEngine = class _ContextEngine {
4556
4577
  try {
4557
4578
  await pipeline.applyUpdateBranching();
4558
4579
  await this.#foldUserReminders(pending);
4559
- return await pipeline.persist();
4560
- } catch (error) {
4561
- this.#pendingMessages.unshift(...pending);
4562
- throw error;
4580
+ const result = await pipeline.persist();
4581
+ const committed = new Set(taken);
4582
+ this.#pendingMessages = this.#pendingMessages.filter(
4583
+ (fragment2) => !committed.has(fragment2)
4584
+ );
4585
+ return result;
4586
+ } finally {
4587
+ for (const fragment2 of taken) this.#savingMessages.delete(fragment2);
4563
4588
  }
4564
4589
  }
4565
4590
  /**
@@ -5878,7 +5903,12 @@ async function chat(agent2, ...args) {
5878
5903
  };
5879
5904
  const message2 = Object.keys(mergedMetadata).length > 0 ? { ...settled, metadata: mergedMetadata } : settled;
5880
5905
  await writeAssistantSegment(message2);
5881
- const usage = await result?.result.usage;
5906
+ const usage = await Promise.resolve(result?.result.usage).catch(
5907
+ (error) => {
5908
+ if (NoOutputGeneratedError.isInstance(error)) return void 0;
5909
+ throw error;
5910
+ }
5911
+ );
5882
5912
  if (usage !== void 0) {
5883
5913
  await context.trackUsage(usage);
5884
5914
  }
@@ -7192,9 +7222,6 @@ function render(tag, ...fragments) {
7192
7222
  return renderer.render([wrapped]);
7193
7223
  }
7194
7224
 
7195
- // packages/context/src/index.ts
7196
- import { createRepairToolCall as createRepairToolCall2 } from "@deepagents/agent";
7197
-
7198
7225
  // packages/context/src/lib/sandbox/apple-container-sandbox.ts
7199
7226
  import { TemporaryFiles } from "@zukhruf/fs";
7200
7227
  import spawn, { SubprocessError } from "nano-spawn";
@@ -8572,6 +8599,7 @@ var BashException = class extends Error {
8572
8599
  };
8573
8600
 
8574
8601
  // packages/context/src/lib/sandbox/bash-tool.ts
8602
+ import { experimental_createBashTool } from "@ai-sdk/tool-bash";
8575
8603
  import { tool as tool3 } from "ai";
8576
8604
  import fg from "fast-glob";
8577
8605
  import { BashTransformPipeline, TeePlugin } from "just-bash";
@@ -8773,12 +8801,10 @@ var TEE_OUTPUT_DIR = "/tmp/bash-tool";
8773
8801
  var MAX_CONCURRENT_WRITES = 20;
8774
8802
  var COMMAND_TIMEOUT_EXIT_CODE = 124;
8775
8803
  var REASONING_INSTRUCTION = 'Every bash tool call must include a brief non-empty "reasoning" input explaining why the command is needed.';
8776
- function truncateOutput(output, maxLength, streamName) {
8804
+ function truncateOutput(output, maxLength) {
8777
8805
  if (output.length <= maxLength) return output;
8778
- const removed = output.length - maxLength;
8779
8806
  return `${output.slice(0, maxLength)}
8780
-
8781
- [${streamName} truncated: ${removed} characters removed]`;
8807
+ [Output truncated: ${output.length - maxLength} characters omitted.]`;
8782
8808
  }
8783
8809
  function makeTeeScriptPortable(script) {
8784
8810
  const withoutStatusAssignments = script.replace(
@@ -8832,37 +8858,22 @@ async function uploadInitialFiles(sandbox, workingDirectory, files) {
8832
8858
  );
8833
8859
  }
8834
8860
  }
8835
- function createBashDescription(options) {
8836
- return [
8837
- "Run Bash commands in the sandbox. " + (options.workingDirectory !== void 0 ? `Every command starts in ${options.workingDirectory}. ` : "Commands run in the sandbox's default working directory. ") + "Use ls to discover files and commands such as cat, grep, sort, awk, and jq to inspect and transform them. Pipes and redirections are supported. Returns stdout, stderr, and exitCode; long output is truncated.",
8838
- options.sandboxDescription,
8839
- options.experimentalTeeTransform && `Pipeline stdout is captured under ${TEE_OUTPUT_DIR}/. Use the returned teeFiles paths to inspect full intermediate output.`,
8840
- options.extraInstructions,
8841
- REASONING_INSTRUCTION
8842
- ].filter(Boolean).join("\n\n");
8843
- }
8844
8861
  async function createBashTool(options) {
8845
8862
  const { workingDirectory, sandbox } = options;
8846
8863
  const initialFiles = await listInitialFiles(options);
8847
- if (workingDirectory !== void 0) {
8848
- const created = await sandbox.run({
8849
- command: `mkdir -p -- ${shellQuote(workingDirectory)}`
8850
- });
8851
- if (created.exitCode !== 0) {
8852
- throw new Error(
8853
- `Could not set up working directory ${workingDirectory}: ${created.stderr}`
8854
- );
8855
- }
8856
- }
8864
+ const aiSdkBash = await experimental_createBashTool({
8865
+ sandbox,
8866
+ workingDirectory
8867
+ });
8857
8868
  await uploadInitialFiles(sandbox, workingDirectory, initialFiles);
8858
8869
  const experimentalTeeTransform = options.experimentalTeeTransform ?? false;
8859
8870
  const bash = tool3({
8860
- description: createBashDescription({
8861
- workingDirectory,
8862
- sandboxDescription: sandbox.description,
8863
- extraInstructions: options.extraInstructions,
8864
- experimentalTeeTransform
8865
- }),
8871
+ description: [
8872
+ aiSdkBash.description,
8873
+ experimentalTeeTransform && `Pipeline stdout is captured under ${TEE_OUTPUT_DIR}/. Use the returned teeFiles paths to inspect full intermediate output.`,
8874
+ options.extraInstructions,
8875
+ REASONING_INSTRUCTION
8876
+ ].filter(Boolean).join("\n\n"),
8866
8877
  inputSchema: z12.object({
8867
8878
  command: z12.string().describe("The bash command to execute"),
8868
8879
  reasoning: z12.string().trim().min(1).describe("Brief reason for executing this command")
@@ -8911,8 +8922,8 @@ async function createBashTool(options) {
8911
8922
  const maxOutputLength = options.maxOutputLength ?? DEFAULT_MAX_OUTPUT_LENGTH;
8912
8923
  result = {
8913
8924
  ...result,
8914
- stdout: truncateOutput(result.stdout, maxOutputLength, "stdout"),
8915
- stderr: truncateOutput(result.stderr, maxOutputLength, "stderr"),
8925
+ stdout: truncateOutput(result.stdout, maxOutputLength),
8926
+ stderr: truncateOutput(result.stderr, maxOutputLength),
8916
8927
  ...teeFiles ? { teeFiles } : {}
8917
8928
  };
8918
8929
  const executionMeta = result.meta;
@@ -12910,60 +12921,94 @@ var StreamStore = class {
12910
12921
  };
12911
12922
 
12912
12923
  // packages/context/src/lib/stream-buffer.ts
12924
+ var MAX_QUEUED_CHUNKS = 128;
12913
12925
  async function persistedWriter(options) {
12914
- const {
12915
- writer,
12916
- store,
12917
- streamId,
12918
- strategy = "buffered",
12919
- flushSize = 20
12920
- } = options;
12926
+ const { writer, store, streamId } = options;
12921
12927
  let seq = 0;
12922
- let buffer = [];
12928
+ let queue = [];
12929
+ let appending;
12930
+ let appendSettled = Promise.resolve();
12931
+ let failure;
12923
12932
  let failedByChunk = false;
12924
12933
  let abortedByChunk = false;
12925
- async function flush() {
12926
- if (buffer.length === 0) return;
12927
- const batch = buffer;
12928
- buffer = [];
12929
- await appendBatch(batch);
12930
- }
12931
- function makeChunk(part) {
12932
- return {
12933
- streamId,
12934
- seq: seq++,
12935
- data: part,
12936
- createdAt: Date.now()
12937
- };
12938
- }
12939
- async function appendBatch(batch) {
12940
- await store.appendChunks(batch);
12941
- failedByChunk ||= collectStreamFailures(batch).length > 0;
12942
- }
12943
- async function persistChunk(chunk) {
12944
- if (chunk.data.type === "abort") {
12945
- abortedByChunk = true;
12934
+ function enqueue(part, committed, failed) {
12935
+ if (failure) {
12936
+ failed(failure.error);
12937
+ return;
12946
12938
  }
12947
- if (strategy === "immediate") {
12948
- await appendBatch([chunk]);
12949
- } else {
12950
- buffer.push(chunk);
12951
- if (buffer.length >= flushSize || chunk.data.type.startsWith("data-")) {
12952
- await flush();
12939
+ queue.push({
12940
+ chunk: { streamId, seq: seq++, data: part, createdAt: Date.now() },
12941
+ committed,
12942
+ failed
12943
+ });
12944
+ appending ??= appendQueued();
12945
+ }
12946
+ async function appendQueued() {
12947
+ while (queue.length > 0) {
12948
+ const batch = queue;
12949
+ queue = [];
12950
+ const chunks = batch.map(({ chunk }) => chunk);
12951
+ const append = Promise.try(() => store.appendChunks(chunks));
12952
+ appendSettled = append.then(
12953
+ () => void 0,
12954
+ () => void 0
12955
+ );
12956
+ try {
12957
+ await append;
12958
+ } catch (error) {
12959
+ failure = { error };
12960
+ const rejected = [...batch, ...queue];
12961
+ queue = [];
12962
+ for (const entry of rejected) entry.failed(error);
12963
+ break;
12953
12964
  }
12965
+ failedByChunk ||= collectStreamFailures(chunks).length > 0;
12966
+ abortedByChunk ||= chunks.some(({ data }) => data.type === "abort");
12967
+ for (const entry of batch) entry.committed();
12954
12968
  }
12969
+ appending = void 0;
12970
+ }
12971
+ async function settle() {
12972
+ while (appending) await appending;
12973
+ }
12974
+ async function flush() {
12975
+ await settle();
12976
+ if (failure) throw failure.error;
12955
12977
  }
12956
12978
  const wrappedWriter = {
12957
12979
  onError: writer.onError,
12958
- async write(part) {
12959
- await persistChunk(makeChunk(part));
12960
- writer.write(part);
12980
+ write(part) {
12981
+ const { promise, resolve: resolve3, reject } = Promise.withResolvers();
12982
+ enqueue(
12983
+ part,
12984
+ () => {
12985
+ writer.write(part);
12986
+ resolve3();
12987
+ },
12988
+ reject
12989
+ );
12990
+ return promise;
12961
12991
  },
12962
12992
  merge(stream) {
12993
+ let cancelled = false;
12963
12994
  const transform = new TransformStream({
12964
- async transform(chunk, controller) {
12965
- await persistChunk(makeChunk(chunk));
12966
- controller.enqueue(chunk);
12995
+ async transform(part, controller) {
12996
+ enqueue(
12997
+ part,
12998
+ () => {
12999
+ if (!cancelled) controller.enqueue(part);
13000
+ },
13001
+ (error) => {
13002
+ if (!cancelled) controller.error(error);
13003
+ }
13004
+ );
13005
+ while (queue.length >= MAX_QUEUED_CHUNKS && !failure) {
13006
+ await appendSettled;
13007
+ }
13008
+ },
13009
+ flush: () => flush(),
13010
+ cancel() {
13011
+ cancelled = true;
12967
13012
  }
12968
13013
  });
12969
13014
  writer.merge(stream.pipeThrough(transform));
@@ -12982,7 +13027,7 @@ async function persistedWriter(options) {
12982
13027
  );
12983
13028
  },
12984
13029
  async fail(error) {
12985
- await flush();
13030
+ await settle();
12986
13031
  if (failedByChunk) return;
12987
13032
  await store.updateStreamStatus(streamId, "failed", { error });
12988
13033
  },
@@ -12993,105 +13038,27 @@ async function persistedWriter(options) {
12993
13038
  }
12994
13039
 
12995
13040
  // packages/context/src/lib/stream/polling-change-source.ts
13041
+ import { Backoff } from "@zukhruf/async";
12996
13042
  import { setTimeout as delay2 } from "node:timers/promises";
12997
-
12998
- // packages/context/src/lib/stream/polling-policy.ts
12999
- var DEFAULT_WATCH_POLLING = {
13000
- minMs: 25,
13001
- maxMs: 500,
13002
- multiplier: 2,
13003
- jitterRatio: 0.15,
13004
- statusCheckEvery: 3
13005
- };
13006
- var DEFAULT_CANCEL_POLLING = {
13007
- minMs: 50,
13008
- maxMs: 500,
13009
- multiplier: 2,
13010
- jitterRatio: 0.15
13011
- };
13012
- function normalizeWatchPolling(polling, fallback = DEFAULT_WATCH_POLLING) {
13013
- const merged = {
13014
- ...fallback,
13015
- ...polling
13016
- };
13017
- const normalizedBase = normalizeAdaptivePolling(merged, fallback);
13018
- return {
13019
- ...normalizedBase,
13020
- statusCheckEvery: clampInt(merged.statusCheckEvery, 1, 1e4)
13021
- };
13022
- }
13023
- function normalizeCancelPolling(polling, fallback = DEFAULT_CANCEL_POLLING) {
13024
- return normalizeAdaptivePolling(polling, fallback);
13025
- }
13026
- function createAdaptivePollingState(config) {
13027
- return {
13028
- config,
13029
- currentMs: config.minMs
13030
- };
13031
- }
13032
- function resetAdaptivePolling(state) {
13033
- state.currentMs = state.config.minMs;
13034
- }
13035
- function nextAdaptivePollingDelay(state) {
13036
- const current = clampInt(
13037
- state.currentMs,
13038
- state.config.minMs,
13039
- state.config.maxMs
13040
- );
13041
- const delay4 = applyJitter(
13042
- current,
13043
- state.config.jitterRatio,
13044
- state.config.minMs,
13045
- state.config.maxMs
13046
- );
13047
- state.currentMs = clampInt(
13048
- Math.ceil(current * state.config.multiplier),
13049
- state.config.minMs,
13050
- state.config.maxMs
13051
- );
13052
- return delay4;
13053
- }
13054
- function normalizeAdaptivePolling(polling, fallback) {
13055
- const merged = {
13056
- ...fallback,
13057
- ...polling
13058
- };
13059
- const minMs = clampInt(merged.minMs, 1, 6e4);
13060
- const maxMs = clampInt(merged.maxMs, minMs, 6e4);
13061
- return {
13062
- minMs,
13063
- maxMs,
13064
- multiplier: clampFloat(merged.multiplier, 1, 10),
13065
- jitterRatio: clampFloat(merged.jitterRatio, 0, 1)
13066
- };
13067
- }
13068
- function applyJitter(value, jitterRatio, min, max) {
13069
- if (jitterRatio <= 0) return value;
13070
- const radius = value * jitterRatio;
13071
- const lowerBound = Math.max(0, value - radius);
13072
- const upperBound = value + radius;
13073
- const jittered = Math.round(
13074
- lowerBound + Math.random() * (upperBound - lowerBound)
13075
- );
13076
- return clampInt(jittered, min, max);
13077
- }
13078
- function clampInt(value, min, max) {
13079
- if (!Number.isFinite(value)) return min;
13080
- return Math.min(max, Math.max(min, Math.round(value)));
13081
- }
13082
- function clampFloat(value, min, max) {
13083
- if (!Number.isFinite(value)) return min;
13084
- return Math.min(max, Math.max(min, value));
13085
- }
13086
-
13087
- // packages/context/src/lib/stream/polling-change-source.ts
13088
13043
  var PollingChangeSource = class {
13089
13044
  #reads;
13090
- #config;
13045
+ #backoff;
13046
+ #statusCheckEvery;
13091
13047
  #onPoll;
13092
13048
  constructor(options) {
13049
+ const statusCheckEvery = options.statusCheckEvery ?? 3;
13050
+ if (!(Number.isInteger(statusCheckEvery) && statusCheckEvery >= 1))
13051
+ throw new RangeError(
13052
+ `statusCheckEvery must be a positive integer, not ${statusCheckEvery}`
13053
+ );
13093
13054
  this.#reads = options.reads;
13094
- this.#config = normalizeWatchPolling(options.config, DEFAULT_WATCH_POLLING);
13055
+ this.#backoff = options.backoff ?? new Backoff({
13056
+ initial: 25,
13057
+ multiplier: 2,
13058
+ max: 500,
13059
+ jitter: { kind: "ratio", ratio: 0.15 }
13060
+ });
13061
+ this.#statusCheckEvery = statusCheckEvery;
13095
13062
  this.#onPoll = options.onPoll;
13096
13063
  }
13097
13064
  async *subscribe(streamId, signal) {
@@ -13101,15 +13068,15 @@ var PollingChangeSource = class {
13101
13068
  }
13102
13069
  yield { kind: "tick" };
13103
13070
  if (isTerminal(initialStatus)) return;
13104
- const delayState = createAdaptivePollingState(this.#config);
13071
+ let retries = 0;
13105
13072
  let pollsSinceStatus = 0;
13106
13073
  let lastStatus = initialStatus;
13107
13074
  while (!signal.aborted) {
13108
- const delayMs = nextAdaptivePollingDelay(delayState);
13075
+ const delayMs = this.#backoff.duration(retries++);
13109
13076
  this.#emit({ type: "idle", streamId, delayMs });
13110
13077
  if (!await waitForDelay(delayMs, signal)) return;
13111
13078
  pollsSinceStatus += 1;
13112
- const shouldCheckStatus = pollsSinceStatus >= this.#config.statusCheckEvery;
13079
+ const shouldCheckStatus = pollsSinceStatus >= this.#statusCheckEvery;
13113
13080
  if (!shouldCheckStatus) {
13114
13081
  yield { kind: "chunks" };
13115
13082
  continue;
@@ -13127,7 +13094,7 @@ var PollingChangeSource = class {
13127
13094
  return;
13128
13095
  }
13129
13096
  if (status === "running" && lastStatus !== "running") {
13130
- resetAdaptivePolling(delayState);
13097
+ retries = 0;
13131
13098
  }
13132
13099
  lastStatus = status;
13133
13100
  if (isTerminal(status)) {
@@ -14053,8 +14020,16 @@ var SqliteStreamStore = class extends StreamStore {
14053
14020
  };
14054
14021
 
14055
14022
  // packages/context/src/lib/stream/stream-manager.ts
14023
+ import { Backoff as Backoff2 } from "@zukhruf/async";
14056
14024
  import { createUIMessageStream as createUIMessageStream3 } from "ai";
14057
14025
  import { setTimeout as delay3 } from "node:timers/promises";
14026
+ var CANCEL_WATCHER_RETRY_MAX_MS = 500;
14027
+ var CANCEL_WATCHER_RETRY = new Backoff2({
14028
+ initial: 50,
14029
+ multiplier: 2,
14030
+ max: CANCEL_WATCHER_RETRY_MAX_MS,
14031
+ jitter: { kind: "ratio", ratio: 0.15 }
14032
+ });
14058
14033
  function isTerminal3(status) {
14059
14034
  return status !== "queued" && status !== "running";
14060
14035
  }
@@ -14137,9 +14112,7 @@ var StreamManager = class {
14137
14112
  pw = await persistedWriter({
14138
14113
  writer,
14139
14114
  store: this.#store,
14140
- streamId,
14141
- strategy: options?.strategy,
14142
- flushSize: options?.flushSize
14115
+ streamId
14143
14116
  });
14144
14117
  pw.writer.merge(stream);
14145
14118
  }
@@ -14183,7 +14156,7 @@ var StreamManager = class {
14183
14156
  ac.abort();
14184
14157
  await this.#notifyCancellation(streamId, options);
14185
14158
  };
14186
- const retryState = createAdaptivePollingState(DEFAULT_CANCEL_POLLING);
14159
+ let retries = 0;
14187
14160
  const listeners = this.#localCancellationListeners.get(streamId) ?? /* @__PURE__ */ new Set();
14188
14161
  listeners.add(detect);
14189
14162
  this.#localCancellationListeners.set(streamId, listeners);
@@ -14206,8 +14179,8 @@ var StreamManager = class {
14206
14179
  streamId,
14207
14180
  ac.signal
14208
14181
  )) {
14209
- if (!subscriptionStabilized && Date.now() - subscribedAt >= DEFAULT_CANCEL_POLLING.maxMs) {
14210
- resetAdaptivePolling(retryState);
14182
+ if (!subscriptionStabilized && Date.now() - subscribedAt >= CANCEL_WATCHER_RETRY_MAX_MS) {
14183
+ retries = 0;
14211
14184
  subscriptionStabilized = true;
14212
14185
  }
14213
14186
  if (change.kind === "chunks") continue;
@@ -14225,7 +14198,10 @@ var StreamManager = class {
14225
14198
  } catch {
14226
14199
  if (ac.signal.aborted || detected) return;
14227
14200
  }
14228
- if (!await waitForDelay2(nextAdaptivePollingDelay(retryState), ac.signal)) {
14201
+ if (!await waitForDelay2(
14202
+ CANCEL_WATCHER_RETRY.duration(retries++),
14203
+ ac.signal
14204
+ )) {
14229
14205
  return;
14230
14206
  }
14231
14207
  }
@@ -14470,9 +14446,7 @@ export {
14470
14446
  ContextEngine,
14471
14447
  ContextRenderer,
14472
14448
  ContextStore,
14473
- DEFAULT_CANCEL_POLLING,
14474
14449
  DEFAULT_STOP_WHEN,
14475
- DEFAULT_WATCH_POLLING,
14476
14450
  DaytonaCreationError,
14477
14451
  DaytonaSandboxError,
14478
14452
  DockerEngine,
@@ -14553,7 +14527,6 @@ export {
14553
14527
  contentMatches,
14554
14528
  contentPattern,
14555
14529
  correction,
14556
- createAdaptivePollingState,
14557
14530
  createAppleContainerSandbox,
14558
14531
  createBashTool,
14559
14532
  createBinaryBridges,
@@ -14561,7 +14534,6 @@ export {
14561
14534
  createDockerSandbox,
14562
14535
  createInstallerContext,
14563
14536
  createMicrosandboxSandbox,
14564
- createRepairToolCall2 as createRepairToolCall,
14565
14537
  createVirtualSandbox,
14566
14538
  dateReminder,
14567
14539
  dayChanged,
@@ -14620,9 +14592,6 @@ export {
14620
14592
  message,
14621
14593
  monthChanged,
14622
14594
  monthReminder,
14623
- nextAdaptivePollingDelay,
14624
- normalizeCancelPolling,
14625
- normalizeWatchPolling,
14626
14595
  not,
14627
14596
  npm,
14628
14597
  nullUsage,
@@ -14644,7 +14613,6 @@ export {
14644
14613
  reminder,
14645
14614
  render,
14646
14615
  repairQuotedArg,
14647
- resetAdaptivePolling,
14648
14616
  resolveReminder,
14649
14617
  resolveReminderAsync,
14650
14618
  resolveTz,