@opengeni/worker-bundle 0.23.4-canary.0 → 0.24.2-canary.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.
Files changed (56) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +7 -0
  2. package/dist/activities/agent-turn/model-usage.d.ts +5 -0
  3. package/dist/activities/agent-turn/quiescence.d.ts +2 -0
  4. package/dist/activities/agent-turn/run.d.ts +9 -0
  5. package/dist/activities/agent-turn/sandbox-runtime.d.ts +15 -0
  6. package/dist/activities/agent-turn/session-title.d.ts +21 -0
  7. package/dist/activities/agent-turn/stream-attempt.d.ts +4 -0
  8. package/dist/activities/agent-turn/tool-environment.d.ts +1 -0
  9. package/dist/activities/agent-turn/turn-context.d.ts +2 -1
  10. package/dist/activities/agent-turn.d.ts +1 -1
  11. package/dist/activities/capabilities.d.ts +1 -0
  12. package/dist/activities/context-compaction.d.ts +2 -0
  13. package/dist/{activities-control-SGU2WEVB.js → activities-control-ZWH7J4KZ.js} +102 -94
  14. package/dist/activities-control-ZWH7J4KZ.js.map +1 -0
  15. package/dist/{activities-turn-7EAQXW6Q.js → activities-turn-I4OPLQLS.js} +613 -284
  16. package/dist/activities-turn-I4OPLQLS.js.map +1 -0
  17. package/dist/{chunk-TJIPCHMP.js → chunk-65JEWP2Q.js} +105 -6
  18. package/dist/chunk-65JEWP2Q.js.map +1 -0
  19. package/dist/{chunk-GRFQTTSQ.js → chunk-GDCLDKSH.js} +7 -3
  20. package/dist/{chunk-GRFQTTSQ.js.map → chunk-GDCLDKSH.js.map} +1 -1
  21. package/dist/{chunk-S2QVRVJW.js → chunk-RQNH6CGR.js} +28 -41
  22. package/dist/chunk-RQNH6CGR.js.map +1 -0
  23. package/dist/index.js +16 -3
  24. package/dist/index.js.map +1 -1
  25. package/dist/observability-metrics.d.ts +33 -7
  26. package/dist/sandbox-reaper-timeout.d.ts +2 -2
  27. package/dist/sandbox-resume.d.ts +6 -1
  28. package/package.json +17 -17
  29. package/src/activities/agent-turn/admission.ts +19 -0
  30. package/src/activities/agent-turn/claim.ts +9 -1
  31. package/src/activities/agent-turn/compaction-prep.ts +6 -1
  32. package/src/activities/agent-turn/finalization.ts +31 -17
  33. package/src/activities/agent-turn/model-usage.ts +33 -4
  34. package/src/activities/agent-turn/quiescence.ts +11 -2
  35. package/src/activities/agent-turn/run.ts +74 -1
  36. package/src/activities/agent-turn/sandbox-runtime.ts +78 -81
  37. package/src/activities/agent-turn/session-title.ts +55 -2
  38. package/src/activities/agent-turn/stream-attempt.ts +258 -135
  39. package/src/activities/agent-turn/tool-environment.ts +41 -7
  40. package/src/activities/agent-turn/turn-context.ts +4 -2
  41. package/src/activities/agent-turn.ts +5 -1
  42. package/src/activities/api-integrations.ts +11 -1
  43. package/src/activities/capabilities.ts +57 -0
  44. package/src/activities/context-compaction.ts +3 -0
  45. package/src/activities/goals.ts +1 -0
  46. package/src/activities/mcp-credentials.ts +53 -0
  47. package/src/activities/rig-verification.ts +5 -3
  48. package/src/activities/scheduled-tasks.ts +111 -98
  49. package/src/index.ts +15 -0
  50. package/src/observability-metrics.ts +152 -14
  51. package/src/sandbox-reaper-timeout.ts +10 -4
  52. package/src/sandbox-resume.ts +58 -8
  53. package/dist/activities-control-SGU2WEVB.js.map +0 -1
  54. package/dist/activities-turn-7EAQXW6Q.js.map +0 -1
  55. package/dist/chunk-S2QVRVJW.js.map +0 -1
  56. package/dist/chunk-TJIPCHMP.js.map +0 -1
@@ -10,7 +10,9 @@ import {
10
10
  clearDurablePendingSessionToolCalls,
11
11
  isSessionCompactionRequested,
12
12
  nextSessionHistoryPosition,
13
+ updateSessionTitleWithEvent,
13
14
  } from "@opengeni/db";
15
+ import { publishDurableSessionEvents } from "@opengeni/events";
14
16
  import {
15
17
  normalizeModelCallUsage,
16
18
  normalizeSdkEvent,
@@ -29,7 +31,7 @@ import {
29
31
  type RunCredentialCommandSession,
30
32
  type CodemodeTokenWriterSession,
31
33
  } from "@opengeni/runtime";
32
- import { type Settings } from "@opengeni/config";
34
+ import { serviceTierForLatencyMode, type Settings } from "@opengeni/config";
33
35
  import { maybeCompactContext, settleFailedContextCompactionLandmark } from "../context-compaction";
34
36
  import { TurnAttemptFencedError } from "../turn-attempt-fenced";
35
37
  import { type MintedRunGitCredentials } from "../environment";
@@ -109,13 +111,16 @@ import {
109
111
  recordCompletedModelCallBeforeOwnershipFences,
110
112
  TurnEventPublisher,
111
113
  createModelResponseEventState,
114
+ createSessionTitleModelUsageEventState,
112
115
  modelResponseContextSignal,
113
116
  assertModelResponseLatencyMode,
114
117
  processModelResponseTerminalEvent,
118
+ processSessionTitleModelUsageEvent,
115
119
  emitModelCallUsage,
116
120
  recordModelUsageAndDebitCredits,
117
121
  recordAuthoritativeModelCallFact,
118
122
  } from "./model-usage";
123
+ import { startParallelSessionTitleGeneration } from "./session-title";
119
124
  import {
120
125
  assertAgentStreamNotCancelled,
121
126
  assertSuccessfulAgentStreamCompletion,
@@ -142,6 +147,7 @@ export type TurnStreamAttemptDeps = {
142
147
  input: RunAgentTurnInput;
143
148
  settings: Settings;
144
149
  db: ActivityServices["db"];
150
+ bus: ActivityServices["bus"];
145
151
  runtime: ActivityServices["runtime"];
146
152
  objectStorage: ActivityServices["objectStorage"];
147
153
  observability: ActivityServices["observability"];
@@ -196,6 +202,7 @@ export type TurnStreamAttemptDeps = {
196
202
  initialSandbox?: ResumedTurnSandbox,
197
203
  ) => Promise<void>;
198
204
  withProviderRequestContext: <T>(fn: () => Promise<T>) => Promise<T>;
205
+ withSessionTitleProviderRequestContext: <T>(fn: () => Promise<T>) => Promise<T>;
199
206
  publishCompactionLiveEvents: (events: SessionEvent[]) => Promise<void>;
200
207
  publishCompactionOutcomeEvents: (events: SessionEvent[]) => Promise<void>;
201
208
  recordCompanyBrainContributionReceiptOnce: () => void;
@@ -221,6 +228,8 @@ export type TurnStreamAttemptDeps = {
221
228
  providerApi: HistoryProviderApi;
222
229
  runSettings: Settings;
223
230
  turnTools: ReturnType<typeof withFirstPartyTools>;
231
+ generateSessionTitleInParallel: boolean;
232
+ sessionTitlePrompt: string;
224
233
  compactSummarizer: CompactionSummarizer;
225
234
  settleDeferredSteerAfterCompaction: () => Promise<RunAgentTurnResult | null>;
226
235
  compactionModelHistoryProjector: (
@@ -258,6 +267,7 @@ export async function runTurnStreamAttempt(
258
267
  input,
259
268
  settings,
260
269
  db,
270
+ bus,
261
271
  runtime,
262
272
  objectStorage,
263
273
  observability,
@@ -288,6 +298,7 @@ export async function runTurnStreamAttempt(
288
298
  attachCodemodeTokenRenewal,
289
299
  attachRunCredentialRenewal,
290
300
  withProviderRequestContext,
301
+ withSessionTitleProviderRequestContext,
291
302
  publishCompactionLiveEvents,
292
303
  publishCompactionOutcomeEvents,
293
304
  recordCompanyBrainContributionReceiptOnce,
@@ -313,6 +324,8 @@ export async function runTurnStreamAttempt(
313
324
  providerApi,
314
325
  runSettings,
315
326
  turnTools,
327
+ generateSessionTitleInParallel,
328
+ sessionTitlePrompt,
316
329
  compactSummarizer,
317
330
  settleDeferredSteerAfterCompaction,
318
331
  compactionModelHistoryProjector,
@@ -336,6 +349,71 @@ export async function runTurnStreamAttempt(
336
349
  if (!activeTurnId) {
337
350
  throw new Error("Turn id was not initialized");
338
351
  }
352
+ const sessionTitleUsageState = createSessionTitleModelUsageEventState(
353
+ claimedModelUsageSourceKeys,
354
+ );
355
+ let parallelSessionTitle: ReturnType<typeof startParallelSessionTitleGeneration> | null = null;
356
+ let parallelSessionTitleFinished = false;
357
+ const finishParallelSessionTitle = async (): Promise<void> => {
358
+ if (parallelSessionTitleFinished) return;
359
+ parallelSessionTitleFinished = true;
360
+ const generated = await parallelSessionTitle?.finish();
361
+ if (!generated) return;
362
+
363
+ if (generated.usage) {
364
+ await processSessionTitleModelUsageEvent({
365
+ usage: generated.usage,
366
+ state: sessionTitleUsageState,
367
+ dispatchId: modelUsageDispatchId,
368
+ settings,
369
+ db,
370
+ observability,
371
+ publish: eventing.publish,
372
+ accountId: input.accountId,
373
+ workspaceId: input.workspaceId,
374
+ sessionId: input.sessionId,
375
+ turnId: activeTurnId,
376
+ turnAttemptId: input.attemptId,
377
+ provider: resolvedModel?.provider.id ?? settings.openaiProvider,
378
+ providerApi: resolvedModel?.provider.api ?? "responses",
379
+ model: resolvedModel?.configured.id ?? turn.model,
380
+ externallyBilled: billingState.isExternallyBilledTurn,
381
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
382
+ countsTowardTokenCap: billingState.countsTowardTokenCap,
383
+ servingCredentialId: providerTurn.effectiveCodexCredentialId,
384
+ priorSessionCredentialId: providerTurn.priorSessionCodexCredentialId,
385
+ emittedSourceKeys: emittedModelUsageSourceKeys,
386
+ renewLease: () => leases.renewServing("model_usage"),
387
+ leaseLost: leases.servingLost,
388
+ leaseLostMessage: "Provider credential lease expired during session title generation",
389
+ });
390
+ }
391
+ if (!generated.title) return;
392
+
393
+ try {
394
+ const result = await updateSessionTitleWithEvent(db, {
395
+ workspaceId: input.workspaceId,
396
+ sessionId: input.sessionId,
397
+ title: generated.title,
398
+ source: "agent",
399
+ });
400
+ if (result.events.length > 0) {
401
+ await publishDurableSessionEvents(bus, input.workspaceId, input.sessionId, result.events);
402
+ }
403
+ } catch (error) {
404
+ observability.warn("parallel session title persistence failed", {
405
+ ...safeErrorDiagnostic(error),
406
+ errorClass: "ParallelSessionTitlePersistenceError",
407
+ errorCode: "parallel_session_title_persistence_failed",
408
+ origin: "worker",
409
+ });
410
+ }
411
+ };
412
+ const cancelParallelSessionTitle = async (): Promise<void> => {
413
+ if (parallelSessionTitleFinished) return;
414
+ parallelSessionTitleFinished = true;
415
+ await parallelSessionTitle?.cancel();
416
+ };
339
417
  let runInput: Awaited<ReturnType<typeof turnInput>>["input"] | null = null;
340
418
  const prepareRunAttemptInput = async () => {
341
419
  const historyPreparationStartedAt = performance.now();
@@ -1523,6 +1601,7 @@ export async function runTurnStreamAttempt(
1523
1601
  if (!attached.accepted) {
1524
1602
  return claimedResult({ status: "cancelled" });
1525
1603
  }
1604
+ await finishParallelSessionTitle();
1526
1605
  if (
1527
1606
  !(await eventing.settle!({
1528
1607
  events: [
@@ -1568,6 +1647,7 @@ export async function runTurnStreamAttempt(
1568
1647
  // frames). Best-effort: a miss leaves the runner's retention TTL to
1569
1648
  // reap, never fails a completed turn.
1570
1649
  await finalizeTurnOpStreamOps();
1650
+ await finishParallelSessionTitle();
1571
1651
  if (
1572
1652
  !(await eventing.settle!({
1573
1653
  events: [
@@ -1635,151 +1715,194 @@ export async function runTurnStreamAttempt(
1635
1715
  ) {
1636
1716
  return claimedResult({ status: "cancelled" });
1637
1717
  }
1638
- let retriedAfterCompaction = false;
1639
- while (true) {
1640
- try {
1641
- const result = await runStreamAttempt({
1642
- requireTerminalModelResponse: retriedAfterCompaction,
1643
- });
1644
- if (retriedAfterCompaction) {
1645
- observability.info("context compaction recovery succeeded after in-activity retry", {
1718
+ if (
1719
+ generateSessionTitleInParallel &&
1720
+ runtime.generateSessionTitle &&
1721
+ !runtimeCancellationSignal.aborted
1722
+ ) {
1723
+ const serviceTier = serviceTierForLatencyMode(
1724
+ turnExecutionPolicy.providerId,
1725
+ turnExecutionPolicy.latencyMode,
1726
+ );
1727
+ parallelSessionTitle = startParallelSessionTitleGeneration({
1728
+ signal: runtimeCancellationSignal,
1729
+ generate: async (signal) =>
1730
+ await withSessionTitleProviderRequestContext(() =>
1731
+ runtime.generateSessionTitle!(runSettings, sessionTitlePrompt, {
1732
+ ...(resolvedModel
1733
+ ? {
1734
+ client: resolvedModel.client,
1735
+ provider: resolvedModel.provider,
1736
+ model: resolvedModel.model,
1737
+ }
1738
+ : {}),
1739
+ modelName: turnExecutionPolicy.upstreamModelId,
1740
+ ...(serviceTier ? { serviceTier } : {}),
1741
+ signal,
1742
+ }),
1743
+ ),
1744
+ onError: (error) => {
1745
+ observability.warn("parallel session title generation failed", {
1746
+ ...safeErrorDiagnostic(error),
1747
+ errorClass: "ParallelSessionTitleGenerationError",
1748
+ errorCode: "parallel_session_title_generation_failed",
1749
+ origin: "worker",
1750
+ });
1751
+ },
1752
+ });
1753
+ }
1754
+ try {
1755
+ let retriedAfterCompaction = false;
1756
+ while (true) {
1757
+ try {
1758
+ const result = await runStreamAttempt({
1759
+ requireTerminalModelResponse: retriedAfterCompaction,
1760
+ });
1761
+ if (retriedAfterCompaction) {
1762
+ observability.info("context compaction recovery succeeded after in-activity retry", {
1763
+ sessionId: input.sessionId,
1764
+ turnId: activeTurnId,
1765
+ });
1766
+ }
1767
+ return result;
1768
+ } catch (attemptError) {
1769
+ const overflow = classifyContextWindowOverflowError(attemptError);
1770
+ const compactionNeeded = findCompactionNeededError(attemptError);
1771
+ const recoveryKind = compactionNeeded
1772
+ ? compactionNeeded.trigger === "operator"
1773
+ ? "operator"
1774
+ : "proactive"
1775
+ : overflow
1776
+ ? "overflow"
1777
+ : null;
1778
+ if (!recoveryKind || !eventing.publish || !eventing.turnStartedPublished) {
1779
+ throw attemptError;
1780
+ }
1781
+ await flushRuntimeBatcher();
1782
+ await historySink.reconcileConversationTruth({ skipInputOnlyRows: true });
1783
+ observability.warn("context compaction recovery attempted", {
1646
1784
  sessionId: input.sessionId,
1647
1785
  turnId: activeTurnId,
1786
+ reason: recoveryKind,
1787
+ ...safeErrorDiagnostic(attemptError),
1788
+ signalTokens: compactionNeeded?.signalTokens,
1789
+ thresholdTokens: compactionNeeded?.thresholdTokens,
1648
1790
  });
1649
- }
1650
- return result;
1651
- } catch (attemptError) {
1652
- const overflow = classifyContextWindowOverflowError(attemptError);
1653
- const compactionNeeded = findCompactionNeededError(attemptError);
1654
- const recoveryKind = compactionNeeded
1655
- ? compactionNeeded.trigger === "operator"
1656
- ? "operator"
1657
- : "proactive"
1658
- : overflow
1659
- ? "overflow"
1660
- : null;
1661
- if (!recoveryKind || !eventing.publish || !eventing.turnStartedPublished) {
1662
- throw attemptError;
1663
- }
1664
- await flushRuntimeBatcher();
1665
- await historySink.reconcileConversationTruth({ skipInputOnlyRows: true });
1666
- observability.warn("context compaction recovery attempted", {
1667
- sessionId: input.sessionId,
1668
- turnId: activeTurnId,
1669
- reason: recoveryKind,
1670
- ...safeErrorDiagnostic(attemptError),
1671
- signalTokens: compactionNeeded?.signalTokens,
1672
- thresholdTokens: compactionNeeded?.thresholdTokens,
1673
- });
1674
- let compacted = false;
1675
- let compactionHandled = false;
1676
- let compactionFailureMessage: string | null = null;
1677
- let compactionRequestCleared = false;
1678
- try {
1679
- const outcome = await forceContextCompaction(
1680
- recoveryKind,
1681
- compactionNeeded?.signalTokens ?? null,
1682
- );
1683
- compacted = outcome.compacted;
1684
- if (outcome.compacted) {
1685
- compactionHandled = true;
1686
- } else {
1687
- compactionHandled = recoveryKind === "operator" && outcome.requestConsumed;
1688
- if (!compactionHandled) {
1689
- compactionFailureMessage = compactionFailureReason(outcome.reason);
1791
+ let compacted = false;
1792
+ let compactionHandled = false;
1793
+ let compactionFailureMessage: string | null = null;
1794
+ let compactionRequestCleared = false;
1795
+ try {
1796
+ const outcome = await forceContextCompaction(
1797
+ recoveryKind,
1798
+ compactionNeeded?.signalTokens ?? null,
1799
+ );
1800
+ compacted = outcome.compacted;
1801
+ if (outcome.compacted) {
1802
+ compactionHandled = true;
1803
+ } else {
1804
+ compactionHandled = recoveryKind === "operator" && outcome.requestConsumed;
1805
+ if (!compactionHandled) {
1806
+ compactionFailureMessage = compactionFailureReason(outcome.reason);
1807
+ }
1690
1808
  }
1691
- }
1692
- } catch (compactError) {
1693
- // Transient checkpoint-provider failures recover this same accepted
1694
- // turn through the normal provider/capacity path. They are not an
1695
- // empty summary and must not create a new goal continuation.
1696
- if (shouldRecoverCompactionProviderFailure(compactError)) throw compactError;
1697
- if (compactError instanceof TurnAttemptFencedError) throw compactError;
1698
- const landmark = await settleFailedContextCompactionLandmark(
1699
- db,
1700
- {
1701
- accountId: input.accountId,
1702
- workspaceId: input.workspaceId,
1809
+ } catch (compactError) {
1810
+ // Transient checkpoint-provider failures recover this same accepted
1811
+ // turn through the normal provider/capacity path. They are not an
1812
+ // empty summary and must not create a new goal continuation.
1813
+ if (shouldRecoverCompactionProviderFailure(compactError)) throw compactError;
1814
+ if (compactError instanceof TurnAttemptFencedError) throw compactError;
1815
+ const landmark = await settleFailedContextCompactionLandmark(
1816
+ db,
1817
+ {
1818
+ accountId: input.accountId,
1819
+ workspaceId: input.workspaceId,
1820
+ sessionId: input.sessionId,
1821
+ turnId: activeTurnId!,
1822
+ executionGeneration: attempt.executionGeneration,
1823
+ attemptId: input.attemptId,
1824
+ },
1825
+ {
1826
+ clearRequestedCompaction: recoveryKind === "operator",
1827
+ publishLiveEvents: publishCompactionLiveEvents,
1828
+ },
1829
+ );
1830
+ compactionRequestCleared = landmark.requestConsumed;
1831
+ if (!isCompactionSummaryFailure(compactError)) throw compactError;
1832
+ await finishParallelSessionTitle();
1833
+ const deferredSteer = await settleDeferredSteerAfterCompaction();
1834
+ if (deferredSteer) return deferredSteer;
1835
+ compactionFailureMessage = String(compactionFailureReasonFromError(compactError));
1836
+ observability.warn("context compaction recovery compaction failed", {
1703
1837
  sessionId: input.sessionId,
1704
- turnId: activeTurnId!,
1705
- executionGeneration: attempt.executionGeneration,
1706
- attemptId: input.attemptId,
1707
- },
1708
- {
1709
- clearRequestedCompaction: recoveryKind === "operator",
1710
- publishLiveEvents: publishCompactionLiveEvents,
1711
- },
1712
- );
1713
- compactionRequestCleared = landmark.requestConsumed;
1714
- if (!isCompactionSummaryFailure(compactError)) throw compactError;
1838
+ turnId: activeTurnId,
1839
+ ...safeErrorDiagnostic(compactError),
1840
+ });
1841
+ }
1842
+ if (!compactionHandled) {
1843
+ const errorMessage =
1844
+ compactionFailureMessage ??
1845
+ "compaction summarization failed: compaction produced no replacement history";
1846
+ await finishParallelSessionTitle();
1847
+ if (
1848
+ !(await eventing.settle!({
1849
+ events: [
1850
+ {
1851
+ type: "turn.failed",
1852
+ payload: {
1853
+ error: errorMessage,
1854
+ code: "context_compaction_failed",
1855
+ retryable: false,
1856
+ recovery: "user_message",
1857
+ compacted: false,
1858
+ },
1859
+ },
1860
+ {
1861
+ type: "session.status.changed",
1862
+ payload: { status: "idle" },
1863
+ },
1864
+ ],
1865
+ turnStatus: "failed",
1866
+ sessionStatus: "idle",
1867
+ activeTurnId: null,
1868
+ ...(recoveryKind === "operator" && !compactionRequestCleared
1869
+ ? { consumeRequestedCompactionFailure: true }
1870
+ : {}),
1871
+ }))
1872
+ ) {
1873
+ return claimedResult({ status: "cancelled" });
1874
+ }
1875
+ control.turnMetricOutcome = "failed";
1876
+ control.activityStatus = "idle";
1877
+ control.activityError = attemptError;
1878
+ // The failed turn settlement already defers ordinary internal
1879
+ // updates and makes the delivered goal-continuation receipt
1880
+ // terminal. End this workflow run as well: returning plain idle
1881
+ // would immediately synthesize another goal continuation against
1882
+ // the unchanged active history and repeat the same failed
1883
+ // compaction. A new human/API prompt, Steer, or explicitly requested
1884
+ // Compact remains a durable explicit wake and may retry; ordinary
1885
+ // machine updates stay pending for that actionable wake.
1886
+ return claimedResult({ status: "idle", deferredUntilWake: true });
1887
+ }
1888
+ await finishParallelSessionTitle();
1715
1889
  const deferredSteer = await settleDeferredSteerAfterCompaction();
1716
1890
  if (deferredSteer) return deferredSteer;
1717
- compactionFailureMessage = String(compactionFailureReasonFromError(compactError));
1718
- observability.warn("context compaction recovery compaction failed", {
1891
+ // Codex parity: compaction remains inside the same logical turn and
1892
+ // the same activity. Rebuild the model-visible history from the
1893
+ // durable replacement and continue the sampling loop; do not create
1894
+ // a recovery event, a queue row, a fake user message, or a sandbox.
1895
+ retriedAfterCompaction = true;
1896
+ observability.info("context compaction recovery retrying turn after compaction", {
1719
1897
  sessionId: input.sessionId,
1720
1898
  turnId: activeTurnId,
1721
- ...safeErrorDiagnostic(compactError),
1899
+ reason: recoveryKind,
1900
+ compacted,
1722
1901
  });
1902
+ await prepareRunAttemptInput();
1723
1903
  }
1724
- if (!compactionHandled) {
1725
- const errorMessage =
1726
- compactionFailureMessage ??
1727
- "compaction summarization failed: compaction produced no replacement history";
1728
- if (
1729
- !(await eventing.settle!({
1730
- events: [
1731
- {
1732
- type: "turn.failed",
1733
- payload: {
1734
- error: errorMessage,
1735
- code: "context_compaction_failed",
1736
- retryable: false,
1737
- recovery: "user_message",
1738
- compacted: false,
1739
- },
1740
- },
1741
- {
1742
- type: "session.status.changed",
1743
- payload: { status: "idle" },
1744
- },
1745
- ],
1746
- turnStatus: "failed",
1747
- sessionStatus: "idle",
1748
- activeTurnId: null,
1749
- ...(recoveryKind === "operator" && !compactionRequestCleared
1750
- ? { consumeRequestedCompactionFailure: true }
1751
- : {}),
1752
- }))
1753
- ) {
1754
- return claimedResult({ status: "cancelled" });
1755
- }
1756
- control.turnMetricOutcome = "failed";
1757
- control.activityStatus = "idle";
1758
- control.activityError = attemptError;
1759
- // The failed turn settlement already defers ordinary internal
1760
- // updates and makes the delivered goal-continuation receipt
1761
- // terminal. End this workflow run as well: returning plain idle
1762
- // would immediately synthesize another goal continuation against
1763
- // the unchanged active history and repeat the same failed
1764
- // compaction. A new human/API prompt, Steer, or explicitly requested
1765
- // Compact remains a durable explicit wake and may retry; ordinary
1766
- // machine updates stay pending for that actionable wake.
1767
- return claimedResult({ status: "idle", deferredUntilWake: true });
1768
- }
1769
- const deferredSteer = await settleDeferredSteerAfterCompaction();
1770
- if (deferredSteer) return deferredSteer;
1771
- // Codex parity: compaction remains inside the same logical turn and
1772
- // the same activity. Rebuild the model-visible history from the
1773
- // durable replacement and continue the sampling loop; do not create
1774
- // a recovery event, a queue row, a fake user message, or a sandbox.
1775
- retriedAfterCompaction = true;
1776
- observability.info("context compaction recovery retrying turn after compaction", {
1777
- sessionId: input.sessionId,
1778
- turnId: activeTurnId,
1779
- reason: recoveryKind,
1780
- compacted,
1781
- });
1782
- await prepareRunAttemptInput();
1783
1904
  }
1905
+ } finally {
1906
+ await cancelParallelSessionTitle();
1784
1907
  }
1785
1908
  }
@@ -3,6 +3,8 @@ import {
3
3
  getWorkspaceModelPolicy,
4
4
  listWorkspaceGatewayCustomModels,
5
5
  listWorkspaceOpenRouterCustomModels,
6
+ listOrganizationModelProviderCustomModelsForWorkspace,
7
+ organizationModelProviderConnectionActiveForWorkspace,
6
8
  persistAttemptToolCatalog,
7
9
  namedSubjectHasLiveWorkspaceAuthority,
8
10
  updateSessionTitleWithEvent,
@@ -53,7 +55,11 @@ import { SandboxChannelAService } from "@opengeni/runtime/sandbox";
53
55
  import { sandboxRunAs } from "@opengeni/runtime";
54
56
  import { type ToolAuthNeededPayload } from "@opengeni/contracts";
55
57
 
56
- import { shouldPublishToolAuthNeededForTurn, xaiCatalogReadinessAuthority } from "./admission";
58
+ import {
59
+ rollingSafeToolAuthNeededPayload,
60
+ shouldPublishToolAuthNeededForTurn,
61
+ xaiCatalogReadinessAuthority,
62
+ } from "./admission";
57
63
  import { unavailableMcpOperationalContext } from "./errors";
58
64
  import { runtimeResourcesForTurn } from "./file-resources";
59
65
  import { waitForTurnOperation } from "./sandbox-provision";
@@ -427,7 +433,10 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
427
433
  if (!shouldPublishToolAuthNeededForTurn(payload, trigger, turn)) {
428
434
  return;
429
435
  }
430
- await eventing.publish!([{ type: "tool.auth_needed", payload }], true);
436
+ await eventing.publish!(
437
+ [{ type: "tool.auth_needed", payload: rollingSafeToolAuthNeededPayload(payload) }],
438
+ true,
439
+ );
431
440
  };
432
441
  const selectedApiIntegrationServerIds = new Set(turnTools.map((tool) => tool.id));
433
442
  const apiIntegrationMcpServers = buildApiIntegrationServersForTurn({
@@ -500,6 +509,7 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
500
509
  firstPartyMcpTools: selectedFirstPartyMcpTools,
501
510
  firstPartyMcpPermissions: session.firstPartyMcpPermissions,
502
511
  }),
512
+ parallelGenerationAvailable: typeof runtime.generateSessionTitle === "function",
503
513
  });
504
514
  const googleDrivePublicationAllowed =
505
515
  selectedFirstPartyMcpTools.includes("editable_artifact_export") &&
@@ -536,6 +546,10 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
536
546
  workspaceGatewayCustomModels,
537
547
  openRouterConnectionActive,
538
548
  workspaceOpenRouterCustomModels,
549
+ organizationGatewayConnectionActive,
550
+ organizationGatewayCustomModels,
551
+ organizationOpenRouterConnectionActive,
552
+ organizationOpenRouterCustomModels,
539
553
  ] = await Promise.all([
540
554
  getWorkspaceModelPolicy(db, input.workspaceId),
541
555
  workspaceCodexSubscriptionActive(db, currentSettings, input.workspaceId),
@@ -555,6 +569,26 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
555
569
  accountId: input.accountId,
556
570
  workspaceId: input.workspaceId,
557
571
  }),
572
+ organizationModelProviderConnectionActiveForWorkspace(db, {
573
+ accountId: input.accountId,
574
+ workspaceId: input.workspaceId,
575
+ providerKind: "vercel_gateway",
576
+ }),
577
+ listOrganizationModelProviderCustomModelsForWorkspace(db, {
578
+ accountId: input.accountId,
579
+ workspaceId: input.workspaceId,
580
+ providerKind: "vercel_gateway",
581
+ }),
582
+ organizationModelProviderConnectionActiveForWorkspace(db, {
583
+ accountId: input.accountId,
584
+ workspaceId: input.workspaceId,
585
+ providerKind: "openrouter",
586
+ }),
587
+ listOrganizationModelProviderCustomModelsForWorkspace(db, {
588
+ accountId: input.accountId,
589
+ workspaceId: input.workspaceId,
590
+ providerKind: "openrouter",
591
+ }),
558
592
  ]);
559
593
  return {
560
594
  selections: resolveWorkspaceModelSelection({
@@ -566,6 +600,10 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
566
600
  workspaceGatewayCustomModels,
567
601
  workspaceOpenRouterConnectionActive: openRouterConnectionActive,
568
602
  workspaceOpenRouterCustomModels,
603
+ organizationGatewayConnectionActive,
604
+ organizationGatewayCustomModels,
605
+ organizationOpenRouterConnectionActive,
606
+ organizationOpenRouterCustomModels,
569
607
  }),
570
608
  modelNotes: currentCatalog.modelNotes,
571
609
  };
@@ -816,17 +854,13 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
816
854
  } else {
817
855
  activatePreparedToolEnvironment(eventing.preparedTools);
818
856
  }
819
- // Genesis turn = the first user turn (no assistant history reconciled
820
- // yet). Durable Postgres state (countSessionHistoryItems includes
821
- // superseded rows after compaction), NOT a workflow counter (turnsThisRun
822
- // resets on continueAsNew). Drives the one-shot title hint appended to the
823
- // agent's instructions; later attempts and goal continuations never match.
824
857
  return {
825
858
  attemptConnectorActionBindings: [
826
859
  ...googleDriveConnectorBindings,
827
860
  ...githubRestMcp.connectorBindings,
828
861
  ],
829
862
  connectorActionIdentity,
863
+ generateSessionTitleInParallel: titleToolPlan.generateTitleInParallel,
830
864
  postToolPreparationStartedAt,
831
865
  preparationIndependentToolNames: titleToolPlan.preparationIndependentToolNames,
832
866
  };
@@ -97,7 +97,8 @@ export type SandboxRuntimeState = {
97
97
  sandboxHolderId: TurnSandboxLeaseHolderId | null;
98
98
  sandboxGroupId: string | null;
99
99
  leaseHeartbeatTimer: ReturnType<typeof setInterval> | undefined;
100
- rotationInFlight: Promise<void> | null;
100
+ rotationPreemptionInFlight: Promise<void> | null;
101
+ deadlineRotationRequested: boolean;
101
102
  snapshotInFlight: Promise<void> | null;
102
103
  firstProviderRequestStarted: boolean;
103
104
  turnEndCaptureInProgress: boolean;
@@ -225,7 +226,8 @@ export function createTurnContext(input: {
225
226
  sandboxHolderId: null,
226
227
  sandboxGroupId: null,
227
228
  leaseHeartbeatTimer: undefined,
228
- rotationInFlight: null,
229
+ rotationPreemptionInFlight: null,
230
+ deadlineRotationRequested: false,
229
231
  snapshotInFlight: null,
230
232
  firstProviderRequestStarted: false,
231
233
  turnEndCaptureInProgress: false,
@@ -1,5 +1,9 @@
1
1
  /** Public agent-turn surface. Implementation lives in `./agent-turn/*`. */
2
- export { createRunAgentTurnActivity } from "./agent-turn/run";
2
+ export {
3
+ createRunAgentTurnActivity,
4
+ sessionTitleCodexRequestContext,
5
+ sessionTitleXaiRequestContext,
6
+ } from "./agent-turn/run";
3
7
  export * from "./agent-turn/admission";
4
8
  export * from "./agent-turn/codex";
5
9
  export * from "./agent-turn/errors";
@@ -92,7 +92,13 @@ function integrationCredentialResolver(
92
92
  forceRefresh: request.forceRefresh === true,
93
93
  });
94
94
  if (result.status === "auth_needed") {
95
- await publishAuthNeeded(input, integration.serverId, request.operationKey, result);
95
+ await publishAuthNeeded(
96
+ input,
97
+ integration.serverId,
98
+ request.operationKey,
99
+ result,
100
+ connectionRef,
101
+ );
96
102
  return null;
97
103
  }
98
104
  const destination = new URL(request.destinationUrl);
@@ -120,6 +126,7 @@ async function publishAuthNeeded(
120
126
  serverId: string,
121
127
  toolName: string,
122
128
  result: Extract<ResolveConnectionCredentialResult, { status: "auth_needed" }>,
129
+ connectionRef: NonNullable<ApiIntegrationRuntime["connectionRef"]>,
123
130
  ): Promise<void> {
124
131
  try {
125
132
  await input.onAuthNeeded?.({
@@ -129,6 +136,9 @@ async function publishAuthNeeded(
129
136
  ...(result.provider ? { provider: result.provider } : {}),
130
137
  reason: result.reason,
131
138
  ...(result.connectionId ? { connectionId: result.connectionId } : {}),
139
+ ...(result.authoritySource === "host" || connectionRef.authoritySource === "host"
140
+ ? { authoritySource: "host" as const }
141
+ : {}),
132
142
  ...(result.scopes ? { scopes: result.scopes } : {}),
133
143
  ...(result.resource ? { resource: result.resource } : {}),
134
144
  ...(result.selectedResources ? { selectedResources: result.selectedResources } : {}),