@trigger.dev/sdk 0.0.0-prerelease-20260909121655 → 0.0.0-prerelease-20260911094758

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/esm/v3/ai.js CHANGED
@@ -1592,15 +1592,11 @@ async function installChatInputRouter(chatId, options) {
1592
1592
  checkpoint.appliedThrough = Math.max(checkpoint.appliedThrough ?? replayWindowEnd, replayWindowEnd);
1593
1593
  }
1594
1594
  }
1595
- // A boot that replayed `.in` itself has already answered everything up to
1596
- // `recoveredThrough`, so the floor has to cover it before the tail opens.
1597
- if (options?.recoveredThrough !== undefined) {
1598
- const recovered = options.recoveredThrough;
1599
- checkpoint.resumeFrom = Math.max(checkpoint.resumeFrom ?? recovered, recovered);
1600
- checkpoint.appliedThrough = Math.max(checkpoint.appliedThrough ?? checkpoint.resumeFrom, checkpoint.resumeFrom);
1601
- }
1602
1595
  const router = entry.router;
1603
1596
  router.restore(checkpoint);
1597
+ if (options?.recoveredSeqNums && options.recoveredSeqNums.length > 0) {
1598
+ router.markRecovered(options.recoveredSeqNums);
1599
+ }
1604
1600
  const floor = router.resumeFrom();
1605
1601
  if (floor !== undefined) {
1606
1602
  sessionStreams.setLastSeqNum(chatId, "in", floor);
@@ -4061,6 +4057,13 @@ function chatAgent(options) {
4061
4057
  // default, `inFlightUsers`). The turn-loop checks this queue ahead of
4062
4058
  // `messagesInput.waitWithIdleTimeout` so recovered turns fire first.
4063
4059
  const bootInjectedQueue = [];
4060
+ const recoveredSeqByPayload = new WeakMap();
4061
+ const dispatchBootInjected = () => bootInjectedQueue.shift();
4062
+ const settleRecoveredTurn = (wirePayload) => {
4063
+ const settledSeq = recoveredSeqByPayload.get(wirePayload);
4064
+ if (settledSeq !== undefined)
4065
+ chatInputRouter().settleRecovered(settledSeq);
4066
+ };
4064
4067
  const couldHavePriorState = payload.continuation === true || ctx.attempt.number > 1;
4065
4068
  // `.in` resume cursor, computed at most once per boot. The boot
4066
4069
  // block below resolves it (snapshot field or records scan) and the
@@ -4201,16 +4204,10 @@ function chatAgent(options) {
4201
4204
  }
4202
4205
  // ── session.in router ──────────────────────────────────────────
4203
4206
  //
4204
- // Reads the turn boundary and subscribes in one call. `bootInCursor` is
4205
- // only a fallback: the boot block above may already have resolved a
4206
- // cursor from the snapshot, which is used when the boundary itself
4207
- // carries none. Everything the boot replayed off `.in` is dispatched from
4208
- // `bootInjectedQueue` below, so it goes into the floor here — folded in
4209
- // after the subscription opens, the live tail re-delivers it as a turn.
4210
- const lastRecoveredInSeq = replayedInTail.length > 0 ? replayedInTail[replayedInTail.length - 1].seqNum : undefined;
4207
+ const recoveredSeqNums = replayedInTail.map((r) => r.seqNum);
4211
4208
  await installChatInputRouter(payload.chatId, {
4212
4209
  fallbackResumeFrom: bootInCursorResolved ? bootInCursor : undefined,
4213
- recoveredThrough: lastRecoveredInSeq,
4210
+ recoveredSeqNums,
4214
4211
  resuming: Boolean(payload.continuation) || ctx.attempt.number > 1,
4215
4212
  });
4216
4213
  // ── Recovery boot + chain reconstruction ────────────────────────
@@ -4293,7 +4290,7 @@ function chatAgent(options) {
4293
4290
  // branches: at n=1 the orphan partial is dropped and the interrupted
4294
4291
  // user is re-dispatched as a fresh turn instead.
4295
4292
  let seedChain;
4296
- let recoveredTurns;
4293
+ let recoveredEntries;
4297
4294
  if (hookChain !== undefined) {
4298
4295
  seedChain = hookChain;
4299
4296
  }
@@ -4304,13 +4301,26 @@ function chatAgent(options) {
4304
4301
  seedChain = settledMessages;
4305
4302
  }
4306
4303
  if (hookRecoveredTurns !== undefined) {
4307
- recoveredTurns = hookRecoveredTurns;
4304
+ const seqNumsByRecoveredId = new Map();
4305
+ for (const entry of replayedInTail) {
4306
+ const existing = seqNumsByRecoveredId.get(entry.message.id);
4307
+ if (existing)
4308
+ existing.push(entry.seqNum);
4309
+ else
4310
+ seqNumsByRecoveredId.set(entry.message.id, [entry.seqNum]);
4311
+ }
4312
+ recoveredEntries = hookRecoveredTurns.map((message) => ({
4313
+ message,
4314
+ seqNum: seqNumsByRecoveredId.get(message.id)?.shift(),
4315
+ }));
4308
4316
  }
4309
4317
  else if (partialAssistant !== undefined && inFlightUsers.length > 1) {
4310
- recoveredTurns = inFlightUsers.slice(1);
4318
+ recoveredEntries = replayedInTail
4319
+ .slice(1)
4320
+ .map((r) => ({ message: r.message, seqNum: r.seqNum }));
4311
4321
  }
4312
4322
  else {
4313
- recoveredTurns = inFlightUsers;
4323
+ recoveredEntries = replayedInTail.map((r) => ({ message: r.message, seqNum: r.seqNum }));
4314
4324
  }
4315
4325
  // `beforeBoot` errors bubble — the customer opted into blocking
4316
4326
  // persistence and a failure there should fail the run rather than
@@ -4340,13 +4350,14 @@ function chatAgent(options) {
4340
4350
  for (const entry of replayedInTail) {
4341
4351
  metadataById.set(entry.message.id, entry.metadata);
4342
4352
  }
4343
- for (const msg of recoveredTurns) {
4353
+ const dispatchedRecoveredSeqs = new Set();
4354
+ for (const { message: msg, seqNum } of recoveredEntries) {
4344
4355
  if (wireMessageId && msg.id === wireMessageId)
4345
4356
  continue;
4346
4357
  const recoveredMetadata = metadataById.has(msg.id)
4347
4358
  ? metadataById.get(msg.id)
4348
4359
  : payload.metadata;
4349
- bootInjectedQueue.push({
4360
+ const injectedPayload = {
4350
4361
  chatId: payload.chatId,
4351
4362
  sessionId: payload.sessionId,
4352
4363
  metadata: recoveredMetadata,
@@ -4355,7 +4366,17 @@ function chatAgent(options) {
4355
4366
  messageId: msg.id,
4356
4367
  continuation: payload.continuation,
4357
4368
  previousRunId: payload.previousRunId,
4358
- });
4369
+ };
4370
+ bootInjectedQueue.push(injectedPayload);
4371
+ if (seqNum !== undefined) {
4372
+ recoveredSeqByPayload.set(injectedPayload, seqNum);
4373
+ dispatchedRecoveredSeqs.add(seqNum);
4374
+ }
4375
+ }
4376
+ for (const entry of replayedInTail) {
4377
+ if (!dispatchedRecoveredSeqs.has(entry.seqNum)) {
4378
+ chatInputRouter().settleRecovered(entry.seqNum);
4379
+ }
4359
4380
  }
4360
4381
  accumulatedUIMessages = seedChain;
4361
4382
  // ── Head-start bootstrap ─────────────────────────────────────
@@ -4515,7 +4536,7 @@ function chatAgent(options) {
4515
4536
  */
4516
4537
  let dispatchedRecoveredFirstTurn = false;
4517
4538
  if (preloaded && bootInjectedQueue.length > 0) {
4518
- currentWirePayload = bootInjectedQueue.shift();
4539
+ currentWirePayload = dispatchBootInjected();
4519
4540
  dispatchedRecoveredFirstTurn = true;
4520
4541
  }
4521
4542
  // Handle preloaded runs — fire onPreload, then wait for the first real message
@@ -4723,7 +4744,7 @@ function chatAgent(options) {
4723
4744
  // waiting on the live session.in. Subsequent recovered turns
4724
4745
  // get drained by the end-of-turn picker below.
4725
4746
  if (bootInjectedQueue.length > 0) {
4726
- currentWirePayload = bootInjectedQueue.shift();
4747
+ currentWirePayload = dispatchBootInjected();
4727
4748
  }
4728
4749
  else {
4729
4750
  const effectiveIdleTimeout = idleTimeoutInSeconds ?? payload.idleTimeoutInSeconds;
@@ -5287,6 +5308,7 @@ function chatAgent(options) {
5287
5308
  chatId: currentWirePayload.chatId,
5288
5309
  messageId: currentWirePayload.messageId,
5289
5310
  });
5311
+ settleRecoveredTurn(currentWirePayload);
5290
5312
  await writeTurnCompleteChunk(currentWirePayload.chatId);
5291
5313
  // Not a turn — don't consume an iteration.
5292
5314
  turn--;
@@ -6002,6 +6024,7 @@ function chatAgent(options) {
6002
6024
  }
6003
6025
  locals.set(chatResponsePartsKey, []);
6004
6026
  }
6027
+ settleRecoveredTurn(currentWirePayload);
6005
6028
  // Write turn-complete control chunk — closes the frontend stream.
6006
6029
  const turnCompleteResult = await writeTurnCompleteChunk(currentWirePayload.chatId, turnAccessToken);
6007
6030
  // Fire onTurnComplete — stream is closed, use for persistence.
@@ -6113,7 +6136,7 @@ function chatAgent(options) {
6113
6136
  // produced these from in-flight user messages on session.in
6114
6137
  // that the dead predecessor never acknowledged.
6115
6138
  if (bootInjectedQueue.length > 0) {
6116
- currentWirePayload = bootInjectedQueue.shift();
6139
+ currentWirePayload = dispatchBootInjected();
6117
6140
  return "continue";
6118
6141
  }
6119
6142
  // chat.requestUpgrade() was called — exit the loop; the handover
@@ -6433,7 +6456,7 @@ function chatAgent(options) {
6433
6456
  // recovered turn shouldn't strand the rest of the boot queue
6434
6457
  // until an unrelated live message arrives.
6435
6458
  if (bootInjectedQueue.length > 0) {
6436
- currentWirePayload = bootInjectedQueue.shift();
6459
+ currentWirePayload = dispatchBootInjected();
6437
6460
  continue;
6438
6461
  }
6439
6462
  // Wait for the next message — same as after a successful turn