@opengeni/worker-bundle 0.23.4 → 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 (84) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +17 -2
  2. package/dist/activities/agent-turn/claim.d.ts +1 -0
  3. package/dist/activities/agent-turn/codex.d.ts +2 -2
  4. package/dist/activities/agent-turn/list-models.d.ts +16 -0
  5. package/dist/activities/agent-turn/model-usage.d.ts +15 -1
  6. package/dist/activities/agent-turn/quiescence.d.ts +2 -0
  7. package/dist/activities/agent-turn/run.d.ts +9 -0
  8. package/dist/activities/agent-turn/sandbox-runtime.d.ts +15 -0
  9. package/dist/activities/agent-turn/session-title.d.ts +21 -0
  10. package/dist/activities/agent-turn/stream-attempt.d.ts +4 -0
  11. package/dist/activities/agent-turn/tool-environment.d.ts +2 -0
  12. package/dist/activities/agent-turn/tool-policy.d.ts +36 -5
  13. package/dist/activities/agent-turn/turn-context.d.ts +4 -1
  14. package/dist/activities/agent-turn.d.ts +1 -1
  15. package/dist/activities/automations.d.ts +1 -0
  16. package/dist/activities/capabilities.d.ts +3 -1
  17. package/dist/activities/context-compaction.d.ts +2 -0
  18. package/dist/activities/goals.d.ts +11 -0
  19. package/dist/activities/image-generation-references.d.ts +2 -1
  20. package/dist/activities/scheduled-tasks.d.ts +1 -0
  21. package/dist/activities/types.d.ts +8 -0
  22. package/dist/{activities-control-52W5IDWM.js → activities-control-ZWH7J4KZ.js} +297 -180
  23. package/dist/activities-control-ZWH7J4KZ.js.map +1 -0
  24. package/dist/activities-control.d.ts +2 -0
  25. package/dist/{activities-turn-A2G2KLSY.js → activities-turn-I4OPLQLS.js} +898 -333
  26. package/dist/activities-turn-I4OPLQLS.js.map +1 -0
  27. package/dist/activities.d.ts +2 -0
  28. package/dist/{chunk-GFNXVZVQ.js → chunk-65JEWP2Q.js} +107 -7
  29. package/dist/chunk-65JEWP2Q.js.map +1 -0
  30. package/dist/{chunk-GRFQTTSQ.js → chunk-GDCLDKSH.js} +7 -3
  31. package/dist/{chunk-GRFQTTSQ.js.map → chunk-GDCLDKSH.js.map} +1 -1
  32. package/dist/{chunk-IPVMCAW5.js → chunk-RQNH6CGR.js} +111 -62
  33. package/dist/chunk-RQNH6CGR.js.map +1 -0
  34. package/dist/index.d.ts +1 -0
  35. package/dist/index.js +43 -7
  36. package/dist/index.js.map +1 -1
  37. package/dist/observability-metrics.d.ts +34 -7
  38. package/dist/sandbox-reaper-timeout.d.ts +2 -2
  39. package/dist/sandbox-resume.d.ts +6 -1
  40. package/dist/workflow-bundle.js +26 -3
  41. package/dist/workflows/activities.d.ts +5 -0
  42. package/dist/workflows/automations.d.ts +5 -0
  43. package/package.json +17 -17
  44. package/src/activities/agent-run-admission.ts +7 -1
  45. package/src/activities/agent-turn/admission.ts +47 -6
  46. package/src/activities/agent-turn/claim.ts +33 -5
  47. package/src/activities/agent-turn/codex.ts +2 -2
  48. package/src/activities/agent-turn/compaction-prep.ts +8 -1
  49. package/src/activities/agent-turn/finalization.ts +31 -17
  50. package/src/activities/agent-turn/list-models.ts +101 -0
  51. package/src/activities/agent-turn/model-usage.ts +120 -45
  52. package/src/activities/agent-turn/quiescence.ts +11 -2
  53. package/src/activities/agent-turn/run.ts +80 -4
  54. package/src/activities/agent-turn/sandbox-runtime.ts +78 -81
  55. package/src/activities/agent-turn/session-title.ts +55 -2
  56. package/src/activities/agent-turn/stream-attempt.ts +266 -135
  57. package/src/activities/agent-turn/tool-environment.ts +102 -7
  58. package/src/activities/agent-turn/tool-policy.ts +58 -5
  59. package/src/activities/agent-turn/turn-context.ts +8 -2
  60. package/src/activities/agent-turn.ts +5 -1
  61. package/src/activities/api-integrations.ts +11 -1
  62. package/src/activities/automations.ts +122 -67
  63. package/src/activities/capabilities.ts +129 -2
  64. package/src/activities/context-compaction.ts +3 -0
  65. package/src/activities/goals.ts +108 -31
  66. package/src/activities/image-generation-references.ts +9 -9
  67. package/src/activities/mcp-credentials.ts +53 -0
  68. package/src/activities/rig-verification.ts +5 -3
  69. package/src/activities/scheduled-tasks.ts +217 -110
  70. package/src/activities/site-auth-maintenance.ts +12 -12
  71. package/src/activities/types.ts +6 -0
  72. package/src/activities.ts +1 -0
  73. package/src/activity-services.ts +4 -1
  74. package/src/github-rest-mcp.ts +2 -1
  75. package/src/index.ts +34 -4
  76. package/src/observability-metrics.ts +154 -14
  77. package/src/sandbox-reaper-timeout.ts +10 -4
  78. package/src/sandbox-resume.ts +58 -8
  79. package/src/workflows/activities.ts +15 -0
  80. package/src/workflows/automations.ts +21 -2
  81. package/dist/activities-control-52W5IDWM.js.map +0 -1
  82. package/dist/activities-turn-A2G2KLSY.js.map +0 -1
  83. package/dist/chunk-GFNXVZVQ.js.map +0 -1
  84. package/dist/chunk-IPVMCAW5.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();
@@ -911,6 +989,8 @@ export async function runTurnStreamAttempt(
911
989
  latencyMode: turnExecutionPolicy.latencyMode,
912
990
  metricProvider: streamProvider,
913
991
  externallyBilled: billingState.isExternallyBilledTurn,
992
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
993
+ countsTowardTokenCap: billingState.countsTowardTokenCap,
914
994
  servingCredentialId: providerTurn.effectiveCodexCredentialId,
915
995
  priorSessionCredentialId: providerTurn.priorSessionCodexCredentialId,
916
996
  emittedSourceKeys: emittedModelUsageSourceKeys,
@@ -976,6 +1056,8 @@ export async function runTurnStreamAttempt(
976
1056
  input.workspaceId,
977
1057
  billingState.isExternallyBilledTurn,
978
1058
  entitlements,
1059
+ billingState.chargesOpenGeniCredits,
1060
+ billingState.countsTowardTokenCap,
979
1061
  );
980
1062
  } catch (limitError) {
981
1063
  // Capture the run state at the boundary so the budget valve in
@@ -1351,6 +1433,8 @@ export async function runTurnStreamAttempt(
1351
1433
  turnAttemptId: input.attemptId,
1352
1434
  model: turn.model,
1353
1435
  externallyBilled: billingState.isExternallyBilledTurn,
1436
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
1437
+ countsTowardTokenCap: billingState.countsTowardTokenCap,
1354
1438
  usage: aggregateUsage,
1355
1439
  normalizedUsage: normalizedAggregateUsage,
1356
1440
  sourceKey: aggregateSourceKey,
@@ -1372,6 +1456,8 @@ export async function runTurnStreamAttempt(
1372
1456
  sourceKey: aggregateSourceKey,
1373
1457
  usage: { usage: aggregateUsage },
1374
1458
  normalizedUsage: normalizedAggregateUsage,
1459
+ ...(billing ? { billingPath: billing.billingPath } : {}),
1460
+ ...(billing?.upstreamProvider ? { upstreamProvider: billing.upstreamProvider } : {}),
1375
1461
  servingAccountHash: aggregateAccountCtx.servingAccountHash,
1376
1462
  accountChangedFromPrevCall: aggregateAccountCtx.accountChangedFromPrevCall,
1377
1463
  emittedSourceKeys: emittedModelUsageSourceKeys,
@@ -1515,6 +1601,7 @@ export async function runTurnStreamAttempt(
1515
1601
  if (!attached.accepted) {
1516
1602
  return claimedResult({ status: "cancelled" });
1517
1603
  }
1604
+ await finishParallelSessionTitle();
1518
1605
  if (
1519
1606
  !(await eventing.settle!({
1520
1607
  events: [
@@ -1560,6 +1647,7 @@ export async function runTurnStreamAttempt(
1560
1647
  // frames). Best-effort: a miss leaves the runner's retention TTL to
1561
1648
  // reap, never fails a completed turn.
1562
1649
  await finalizeTurnOpStreamOps();
1650
+ await finishParallelSessionTitle();
1563
1651
  if (
1564
1652
  !(await eventing.settle!({
1565
1653
  events: [
@@ -1627,151 +1715,194 @@ export async function runTurnStreamAttempt(
1627
1715
  ) {
1628
1716
  return claimedResult({ status: "cancelled" });
1629
1717
  }
1630
- let retriedAfterCompaction = false;
1631
- while (true) {
1632
- try {
1633
- const result = await runStreamAttempt({
1634
- requireTerminalModelResponse: retriedAfterCompaction,
1635
- });
1636
- if (retriedAfterCompaction) {
1637
- 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", {
1638
1784
  sessionId: input.sessionId,
1639
1785
  turnId: activeTurnId,
1786
+ reason: recoveryKind,
1787
+ ...safeErrorDiagnostic(attemptError),
1788
+ signalTokens: compactionNeeded?.signalTokens,
1789
+ thresholdTokens: compactionNeeded?.thresholdTokens,
1640
1790
  });
1641
- }
1642
- return result;
1643
- } catch (attemptError) {
1644
- const overflow = classifyContextWindowOverflowError(attemptError);
1645
- const compactionNeeded = findCompactionNeededError(attemptError);
1646
- const recoveryKind = compactionNeeded
1647
- ? compactionNeeded.trigger === "operator"
1648
- ? "operator"
1649
- : "proactive"
1650
- : overflow
1651
- ? "overflow"
1652
- : null;
1653
- if (!recoveryKind || !eventing.publish || !eventing.turnStartedPublished) {
1654
- throw attemptError;
1655
- }
1656
- await flushRuntimeBatcher();
1657
- await historySink.reconcileConversationTruth({ skipInputOnlyRows: true });
1658
- observability.warn("context compaction recovery attempted", {
1659
- sessionId: input.sessionId,
1660
- turnId: activeTurnId,
1661
- reason: recoveryKind,
1662
- ...safeErrorDiagnostic(attemptError),
1663
- signalTokens: compactionNeeded?.signalTokens,
1664
- thresholdTokens: compactionNeeded?.thresholdTokens,
1665
- });
1666
- let compacted = false;
1667
- let compactionHandled = false;
1668
- let compactionFailureMessage: string | null = null;
1669
- let compactionRequestCleared = false;
1670
- try {
1671
- const outcome = await forceContextCompaction(
1672
- recoveryKind,
1673
- compactionNeeded?.signalTokens ?? null,
1674
- );
1675
- compacted = outcome.compacted;
1676
- if (outcome.compacted) {
1677
- compactionHandled = true;
1678
- } else {
1679
- compactionHandled = recoveryKind === "operator" && outcome.requestConsumed;
1680
- if (!compactionHandled) {
1681
- 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
+ }
1682
1808
  }
1683
- }
1684
- } catch (compactError) {
1685
- // Transient checkpoint-provider failures recover this same accepted
1686
- // turn through the normal provider/capacity path. They are not an
1687
- // empty summary and must not create a new goal continuation.
1688
- if (shouldRecoverCompactionProviderFailure(compactError)) throw compactError;
1689
- if (compactError instanceof TurnAttemptFencedError) throw compactError;
1690
- const landmark = await settleFailedContextCompactionLandmark(
1691
- db,
1692
- {
1693
- accountId: input.accountId,
1694
- 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", {
1695
1837
  sessionId: input.sessionId,
1696
- turnId: activeTurnId!,
1697
- executionGeneration: attempt.executionGeneration,
1698
- attemptId: input.attemptId,
1699
- },
1700
- {
1701
- clearRequestedCompaction: recoveryKind === "operator",
1702
- publishLiveEvents: publishCompactionLiveEvents,
1703
- },
1704
- );
1705
- compactionRequestCleared = landmark.requestConsumed;
1706
- 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();
1707
1889
  const deferredSteer = await settleDeferredSteerAfterCompaction();
1708
1890
  if (deferredSteer) return deferredSteer;
1709
- compactionFailureMessage = String(compactionFailureReasonFromError(compactError));
1710
- 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", {
1711
1897
  sessionId: input.sessionId,
1712
1898
  turnId: activeTurnId,
1713
- ...safeErrorDiagnostic(compactError),
1899
+ reason: recoveryKind,
1900
+ compacted,
1714
1901
  });
1902
+ await prepareRunAttemptInput();
1715
1903
  }
1716
- if (!compactionHandled) {
1717
- const errorMessage =
1718
- compactionFailureMessage ??
1719
- "compaction summarization failed: compaction produced no replacement history";
1720
- if (
1721
- !(await eventing.settle!({
1722
- events: [
1723
- {
1724
- type: "turn.failed",
1725
- payload: {
1726
- error: errorMessage,
1727
- code: "context_compaction_failed",
1728
- retryable: false,
1729
- recovery: "user_message",
1730
- compacted: false,
1731
- },
1732
- },
1733
- {
1734
- type: "session.status.changed",
1735
- payload: { status: "idle" },
1736
- },
1737
- ],
1738
- turnStatus: "failed",
1739
- sessionStatus: "idle",
1740
- activeTurnId: null,
1741
- ...(recoveryKind === "operator" && !compactionRequestCleared
1742
- ? { consumeRequestedCompactionFailure: true }
1743
- : {}),
1744
- }))
1745
- ) {
1746
- return claimedResult({ status: "cancelled" });
1747
- }
1748
- control.turnMetricOutcome = "failed";
1749
- control.activityStatus = "idle";
1750
- control.activityError = attemptError;
1751
- // The failed turn settlement already defers ordinary internal
1752
- // updates and makes the delivered goal-continuation receipt
1753
- // terminal. End this workflow run as well: returning plain idle
1754
- // would immediately synthesize another goal continuation against
1755
- // the unchanged active history and repeat the same failed
1756
- // compaction. A new human/API prompt, Steer, or explicitly requested
1757
- // Compact remains a durable explicit wake and may retry; ordinary
1758
- // machine updates stay pending for that actionable wake.
1759
- return claimedResult({ status: "idle", deferredUntilWake: true });
1760
- }
1761
- const deferredSteer = await settleDeferredSteerAfterCompaction();
1762
- if (deferredSteer) return deferredSteer;
1763
- // Codex parity: compaction remains inside the same logical turn and
1764
- // the same activity. Rebuild the model-visible history from the
1765
- // durable replacement and continue the sampling loop; do not create
1766
- // a recovery event, a queue row, a fake user message, or a sandbox.
1767
- retriedAfterCompaction = true;
1768
- observability.info("context compaction recovery retrying turn after compaction", {
1769
- sessionId: input.sessionId,
1770
- turnId: activeTurnId,
1771
- reason: recoveryKind,
1772
- compacted,
1773
- });
1774
- await prepareRunAttemptInput();
1775
1904
  }
1905
+ } finally {
1906
+ await cancelParallelSessionTitle();
1776
1907
  }
1777
1908
  }