@sentry/junior 0.136.1 → 0.138.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 (103) hide show
  1. package/dist/{agent-hooks-2KDSPPO4.js → agent-hooks-SCR74Q3E.js} +11 -9
  2. package/dist/api/conversations/projection.d.ts +1 -0
  3. package/dist/api/conversations/stats.d.ts +1 -0
  4. package/dist/api/schema/conversation.d.ts +2 -0
  5. package/dist/api/schema.js +1 -1
  6. package/dist/api.js +187 -46
  7. package/dist/app.d.ts +8 -1
  8. package/dist/app.js +519 -860
  9. package/dist/{catalog-runtime-WSZQGC4D.js → catalog-runtime-IU3QF4GG.js} +5 -5
  10. package/dist/chat/agent/request.d.ts +32 -2
  11. package/dist/chat/agent-dispatch/heartbeat.d.ts +5 -0
  12. package/dist/chat/agent-dispatch/types.d.ts +1 -1
  13. package/dist/chat/agent-invocations/errors.d.ts +8 -0
  14. package/dist/chat/agent-invocations/spawn.d.ts +12 -0
  15. package/dist/chat/agent-invocations/store.d.ts +44 -0
  16. package/dist/chat/agent-invocations/types.d.ts +449 -0
  17. package/dist/chat/agent-invocations/work.d.ts +31 -0
  18. package/dist/chat/config.d.ts +1 -0
  19. package/dist/chat/configuration/defaults.d.ts +1 -1
  20. package/dist/chat/conversations/sql/store.d.ts +8 -0
  21. package/dist/chat/conversations/store.d.ts +9 -1
  22. package/dist/chat/experimental.d.ts +16 -0
  23. package/dist/chat/pi/client.d.ts +1 -8
  24. package/dist/chat/pi/gateway-auth.d.ts +16 -0
  25. package/dist/chat/runtime/agent-runner.d.ts +2 -1
  26. package/dist/chat/sandbox/docker.d.ts +17 -0
  27. package/dist/chat/services/agent-continue.d.ts +2 -0
  28. package/dist/chat/services/auth-pause.d.ts +0 -1
  29. package/dist/chat/services/mcp-auth-orchestration.d.ts +3 -2
  30. package/dist/chat/services/plugin-auth-orchestration.d.ts +3 -2
  31. package/dist/chat/services/subscribed-reply-router-policy.d.ts +12 -0
  32. package/dist/chat/services/turn-session-routing.d.ts +17 -0
  33. package/dist/chat/slack/message.d.ts +6 -1
  34. package/dist/chat/slack/source-link.d.ts +8 -2
  35. package/dist/chat/slack/team-domain.d.ts +4 -0
  36. package/dist/chat/source.d.ts +5 -5
  37. package/dist/chat/state/turn-session.d.ts +0 -2
  38. package/dist/chat/task-execution/state.d.ts +23 -11
  39. package/dist/chat/task-execution/store.d.ts +20 -11
  40. package/dist/chat/task-execution/worker.d.ts +2 -2
  41. package/dist/chat/tools/runtime/spawn-agent.d.ts +31 -0
  42. package/dist/chat/tools/types.d.ts +2 -0
  43. package/dist/chunk-3Y47VMHV.js +6 -0
  44. package/dist/chunk-5IDFPCON.js +46 -0
  45. package/dist/chunk-5LTJ4ITU.js +1523 -0
  46. package/dist/{chunk-KLBYCSLJ.js → chunk-5MXWHTCK.js} +17 -434
  47. package/dist/{chunk-CZZDCH3Q.js → chunk-A7WCHXZV.js} +206 -78
  48. package/dist/{chunk-4YQ6I6VF.js → chunk-BOLJOV5T.js} +2 -56
  49. package/dist/{chunk-XCZ6COFO.js → chunk-BYW4B4PR.js} +90 -54
  50. package/dist/{chunk-JQRTXJHD.js → chunk-C7ZVPO4Z.js} +2 -1
  51. package/dist/{chunk-MW2QRPGW.js → chunk-DCLLQGS6.js} +119 -50
  52. package/dist/{chunk-CDVCCOMF.js → chunk-E5UYAIKJ.js} +75 -1475
  53. package/dist/{chunk-3SUK3UXX.js → chunk-ELKW5FIB.js} +5 -5
  54. package/dist/chunk-EM225ITJ.js +56 -0
  55. package/dist/{chunk-Y6JNHTRD.js → chunk-FEUNPTGV.js} +34 -70
  56. package/dist/{chunk-TYNMSQKA.js → chunk-FNLBTB4K.js} +1 -1
  57. package/dist/{chunk-TRZSM67C.js → chunk-HCIVH3JS.js} +1 -0
  58. package/dist/chunk-HIWJWWMT.js +668 -0
  59. package/dist/chunk-IUFM5JWV.js +24 -0
  60. package/dist/{chunk-XWPA5KZF.js → chunk-JNNJZURV.js} +1 -1
  61. package/dist/{chunk-6ZK6FZIV.js → chunk-KCBZSJRU.js} +6 -27
  62. package/dist/{chunk-BG3KWWDJ.js → chunk-KDCHVT25.js} +1 -1
  63. package/dist/{chunk-VH6MI4L7.js → chunk-KFRHRSCJ.js} +1 -1
  64. package/dist/chunk-LPVUNO2W.js +320 -0
  65. package/dist/{chunk-G66RYKRS.js → chunk-N7WARMC6.js} +16 -776
  66. package/dist/chunk-PB3CXNVL.js +41 -0
  67. package/dist/chunk-Q6QZTTLS.js +55 -0
  68. package/dist/{chunk-3QNJOHKG.js → chunk-QS527AV5.js} +1 -1
  69. package/dist/chunk-R776HKPK.js +81 -0
  70. package/dist/{chunk-OTJMQYVP.js → chunk-RZIBSCYC.js} +517 -608
  71. package/dist/chunk-SD3OFRWY.js +714 -0
  72. package/dist/{chunk-HIRIWTWD.js → chunk-SIPKWSXK.js} +58 -10
  73. package/dist/chunk-TCS76X5D.js +441 -0
  74. package/dist/{chunk-UX2TNH5D.js → chunk-TDJCLNDH.js} +3 -3
  75. package/dist/{chunk-NVVBEJUB.js → chunk-TUUDXEXU.js} +336 -300
  76. package/dist/{chunk-22Z6MGMK.js → chunk-VPZFVJND.js} +1 -1
  77. package/dist/{chunk-RFDD3Z2I.js → chunk-YU5QUF67.js} +1 -1
  78. package/dist/chunk-ZBSMCXKV.js +710 -0
  79. package/dist/{chunk-FSYXYBWU.js → chunk-ZXNTJJIE.js} +2 -2
  80. package/dist/cli/chat.js +65 -26
  81. package/dist/cli/check.js +7 -7
  82. package/dist/cli/plugins.js +14 -12
  83. package/dist/cli/snapshot-create.js +10 -9
  84. package/dist/cli/upgrade.js +7 -7
  85. package/dist/conversation-work-KATFHLG3.js +56 -0
  86. package/dist/db/schema/agent-invocations.d.ts +544 -0
  87. package/dist/db/schema.d.ts +543 -1
  88. package/dist/{db-Z2NNL2JC.js → db-UO2IWXGA.js} +7 -7
  89. package/dist/experimental-HH6OM6UH.js +13 -0
  90. package/dist/instrumentation.js +4 -4
  91. package/dist/nitro.js +4 -3
  92. package/dist/{oauth-callback-server-PO3M5JMG.js → oauth-callback-server-MAQI43LX.js} +26 -18
  93. package/dist/{runner-QHQYZYJA.js → runner-PIUQEXKE.js} +26 -14
  94. package/dist/{skills-KXMT3DRA.js → skills-4ARJADHQ.js} +6 -6
  95. package/dist/spawn-G2AWPMCT.js +38 -0
  96. package/dist/{validation-Y42ULTT6.js → validation-S3EE3HWL.js} +6 -6
  97. package/dist/version.js +1 -1
  98. package/dist/work-UARRUAW3.js +46 -0
  99. package/dist/worker-DOCIRVD3.js +24 -0
  100. package/migrations/0021_first_warhawk.sql +35 -0
  101. package/migrations/meta/0021_snapshot.json +2176 -0
  102. package/migrations/meta/_journal.json +8 -1
  103. package/package.json +6 -5
@@ -1,3 +1,6 @@
1
+ import {
2
+ JUNIOR_THREAD_STATE_TTL_MS
3
+ } from "./chunk-3Y47VMHV.js";
1
4
  import {
2
5
  JUNIOR_NATIVE_EVENT_NAMESPACE,
3
6
  ToolInputError,
@@ -18,18 +21,20 @@ import {
18
21
  instructionProvenanceFor,
19
22
  sanitizePostgresJson,
20
23
  withConversationEventLock
21
- } from "./chunk-HIRIWTWD.js";
24
+ } from "./chunk-SIPKWSXK.js";
22
25
  import {
23
26
  SANDBOX_WORKSPACE_ROOT,
24
- getStateAdapter,
25
27
  sleep
26
- } from "./chunk-4YQ6I6VF.js";
28
+ } from "./chunk-EM225ITJ.js";
29
+ import {
30
+ getStateAdapter
31
+ } from "./chunk-BOLJOV5T.js";
27
32
  import {
28
33
  botConfig,
29
34
  escapeXml,
30
35
  resolveGatewayModel,
31
36
  unwrapCurrentInstruction
32
- } from "./chunk-XCZ6COFO.js";
37
+ } from "./chunk-BYW4B4PR.js";
33
38
  import {
34
39
  toStoredSlackActor
35
40
  } from "./chunk-HZF5P5UN.js";
@@ -44,7 +49,7 @@ import {
44
49
  setSpanAttributes,
45
50
  toOptionalNumber,
46
51
  toOptionalString
47
- } from "./chunk-XWPA5KZF.js";
52
+ } from "./chunk-JNNJZURV.js";
48
53
 
49
54
  // src/chat/turn-context-tag.ts
50
55
  var TURN_CONTEXT_TAG = "runtime-turn-context";
@@ -1396,30 +1401,10 @@ function getAssistantReplyText(message) {
1396
1401
  return decision.kind === "deliver" ? decision.text : void 0;
1397
1402
  }
1398
1403
 
1399
- // src/chat/services/persist-retry.ts
1400
- var PERSIST_ATTEMPTS = 3;
1401
- async function persistWithRetry(persist) {
1402
- let lastError;
1403
- for (let attempt = 1; attempt <= PERSIST_ATTEMPTS; attempt += 1) {
1404
- try {
1405
- await persist();
1406
- return;
1407
- } catch (error) {
1408
- lastError = error;
1409
- if (attempt < PERSIST_ATTEMPTS) {
1410
- await sleep(attempt * 100);
1411
- }
1412
- }
1413
- }
1414
- throw lastError;
1415
- }
1416
-
1417
1404
  // src/chat/state/turn-session.ts
1418
1405
  import { THREAD_STATE_TTL_MS } from "chat";
1419
1406
  import {
1420
- actorSchema,
1421
- destinationSchema,
1422
- sourceSchema
1407
+ actorSchema
1423
1408
  } from "@sentry/junior-plugin-api";
1424
1409
  import { z as z2 } from "zod";
1425
1410
 
@@ -1784,6 +1769,11 @@ function agentTurnSessionConversationIndexKey(conversationId) {
1784
1769
  var AGENT_TURN_SESSION_INDEX_KEY = `${AGENT_TURN_SESSION_PREFIX}:index`;
1785
1770
  var AGENT_TURN_SESSION_INDEX_MAX_LENGTH = 5e3;
1786
1771
  var AGENT_TURN_SESSION_TTL_MS = THREAD_STATE_TTL_MS;
1772
+ function definedProps(values) {
1773
+ return Object.fromEntries(
1774
+ Object.entries(values).filter(([, value]) => value !== void 0)
1775
+ );
1776
+ }
1787
1777
  var agentTurnSessionStatusSchema = z2.enum([
1788
1778
  "running",
1789
1779
  "awaiting_resume",
@@ -1811,15 +1801,14 @@ var agentTurnSessionSummarySchema = z2.object({
1811
1801
  conversationId: z2.string().min(1),
1812
1802
  cumulativeDurationMs: nonNegativeNumberSchema,
1813
1803
  cumulativeUsage: agentTurnUsageSchema.optional(),
1814
- destination: destinationSchema.optional(),
1815
1804
  dispatchId: z2.string().min(1).optional(),
1816
1805
  dispatchOutcome: z2.enum(["blocked", "completed", "failed"]).optional(),
1817
1806
  resultMessageId: z2.string().min(1).optional(),
1818
- source: sourceSchema.optional(),
1819
1807
  lastProgressAtMs: nonNegativeNumberSchema,
1820
1808
  loadedSkillNames: z2.array(z2.string()).optional(),
1821
1809
  modelId: z2.string().min(1).optional(),
1822
1810
  reasoningLevel: z2.string().min(1).optional(),
1811
+ // TODO(#1267): Remove actor from this projection once resume rebuilds it.
1823
1812
  actor: actorSchema.optional(),
1824
1813
  resumeReason: agentTurnResumeReasonSchema.optional(),
1825
1814
  resumedFromSliceId: z2.number().int().nonnegative().optional(),
@@ -1830,7 +1819,7 @@ var agentTurnSessionSummarySchema = z2.object({
1830
1819
  surface: agentTurnSurfaceSchema.optional(),
1831
1820
  traceId: z2.string().optional(),
1832
1821
  updatedAtMs: nonNegativeNumberSchema
1833
- }).strict();
1822
+ }).strip();
1834
1823
  var storedAgentTurnSessionRecordSchema = agentTurnSessionSummarySchema.extend({
1835
1824
  actors: z2.array(actorSchema).optional(),
1836
1825
  committedSeq: seqCursorSchema,
@@ -1838,7 +1827,7 @@ var storedAgentTurnSessionRecordSchema = agentTurnSessionSummarySchema.extend({
1838
1827
  errorMessage: z2.string().optional(),
1839
1828
  turnStartSeq: seqCursorSchema.optional(),
1840
1829
  runtimeContext: z2.array(piMessageSchema).optional()
1841
- }).strict();
1830
+ }).strip();
1842
1831
  function conversationExecutionFromSummary(summary) {
1843
1832
  const status = summary.state === "completed" || summary.state === "abandoned" ? "idle" : summary.state;
1844
1833
  return {
@@ -1857,38 +1846,44 @@ function parseAgentTurnSessionSummary(value) {
1857
1846
  return agentTurnSessionSummarySchema.parse(value);
1858
1847
  }
1859
1848
  async function appendAgentTurnSessionSummary(summary, ttlMs) {
1849
+ const projectedSummary = agentTurnSessionSummarySchema.parse(summary);
1860
1850
  const stateAdapter = getStateAdapter();
1861
1851
  await Promise.all([
1862
- stateAdapter.appendToList(AGENT_TURN_SESSION_INDEX_KEY, summary, {
1852
+ stateAdapter.appendToList(AGENT_TURN_SESSION_INDEX_KEY, projectedSummary, {
1863
1853
  maxLength: AGENT_TURN_SESSION_INDEX_MAX_LENGTH,
1864
1854
  ttlMs
1865
1855
  }),
1866
1856
  stateAdapter.appendToList(
1867
- agentTurnSessionConversationIndexKey(summary.conversationId),
1868
- summary,
1857
+ agentTurnSessionConversationIndexKey(projectedSummary.conversationId),
1858
+ projectedSummary,
1869
1859
  { ttlMs }
1870
1860
  )
1871
1861
  ]);
1872
1862
  }
1873
1863
  async function recordConversationActivityMetadata(args) {
1874
1864
  const conversationStore = args.conversationStore ?? getConversationStore();
1875
- const source = args.summary.destination?.platform === "local" ? "local" : args.summary.surface;
1865
+ const conversation = await conversationStore.get({
1866
+ conversationId: args.summary.conversationId
1867
+ });
1868
+ const isChild = Boolean(conversation?.lineage);
1869
+ const destination = isChild ? void 0 : args.destination;
1870
+ const activitySource = isChild ? "internal" : destination?.platform === "local" ? "local" : destination ? args.summary.surface : void 0;
1876
1871
  await conversationStore.recordActivity({
1877
1872
  activityAtMs: args.summary.updatedAtMs,
1878
1873
  channelName: args.summary.channelName,
1879
1874
  conversationId: args.summary.conversationId,
1880
- destination: args.summary.destination,
1875
+ destination,
1881
1876
  nowMs: args.nowMs,
1882
1877
  actor: sessionLogActor(args.summary.actor),
1883
- source,
1884
- ...args.summary.source ? { sessionSource: args.summary.source } : {},
1885
- visibility: args.destinationVisibility
1878
+ ...definedProps({ source: activitySource }),
1879
+ ...args.source ? { sessionSource: args.source } : {},
1880
+ visibility: isChild ? void 0 : args.destinationVisibility
1886
1881
  });
1887
1882
  await conversationStore.recordExecution({
1888
1883
  channelName: args.summary.channelName,
1889
1884
  conversationId: args.summary.conversationId,
1890
1885
  createdAtMs: args.summary.startedAtMs,
1891
- destination: args.summary.destination,
1886
+ destination,
1892
1887
  execution: conversationExecutionFromSummary(args.summary),
1893
1888
  lastActivityAtMs: args.summary.updatedAtMs,
1894
1889
  metrics: {
@@ -1896,16 +1891,15 @@ async function recordConversationActivityMetadata(args) {
1896
1891
  ...args.summary.cumulativeUsage ? { usage: args.summary.cumulativeUsage } : {}
1897
1892
  },
1898
1893
  actor: sessionLogActor(args.summary.actor),
1899
- source,
1894
+ ...definedProps({ source: activitySource }),
1900
1895
  updatedAtMs: args.nowMs,
1901
- visibility: args.destinationVisibility
1896
+ visibility: isChild ? void 0 : args.destinationVisibility
1902
1897
  });
1903
1898
  }
1904
1899
  function materializeAgentTurnSessionRecord(stored, piProjection, turnStartMessageIndex, restoreVolatileContext = true) {
1905
1900
  const restoredProjection = restoreVolatileContext && (stored.state === "running" || stored.state === "awaiting_resume") ? restoreRuntimeContext(piProjection, stored.runtimeContext) : piProjection;
1906
1901
  return {
1907
1902
  version: stored.version,
1908
- ...stored.channelName ? { channelName: stored.channelName } : {},
1909
1903
  conversationId: stored.conversationId,
1910
1904
  sessionId: stored.sessionId,
1911
1905
  sliceId: stored.sliceId,
@@ -1917,22 +1911,23 @@ function materializeAgentTurnSessionRecord(stored, piProjection, turnStartMessag
1917
1911
  piMessageProvenance: restoredProjection.provenance,
1918
1912
  actors: stored.actors ?? instructionActors(piProjection.provenance),
1919
1913
  cumulativeDurationMs: stored.cumulativeDurationMs,
1920
- ...stored.destination ? { destination: stored.destination } : {},
1921
- ...stored.dispatchId ? { dispatchId: stored.dispatchId } : {},
1922
- ...stored.dispatchOutcome ? { dispatchOutcome: stored.dispatchOutcome } : {},
1923
- ...stored.resultMessageId ? { resultMessageId: stored.resultMessageId } : {},
1924
- ...stored.source ? { source: stored.source } : {},
1925
- ...stored.cumulativeUsage ? { cumulativeUsage: stored.cumulativeUsage } : {},
1926
- ...stored.resumeReason ? { resumeReason: stored.resumeReason } : {},
1927
- ...stored.errorMessage ? { errorMessage: stored.errorMessage } : {},
1928
- ...stored.loadedSkillNames ? { loadedSkillNames: stored.loadedSkillNames } : {},
1929
- ...stored.modelId ? { modelId: stored.modelId } : {},
1930
- ...stored.reasoningLevel ? { reasoningLevel: stored.reasoningLevel } : {},
1931
- ...stored.actor ? { actor: stored.actor } : {},
1932
- ...stored.resumedFromSliceId !== void 0 ? { resumedFromSliceId: stored.resumedFromSliceId } : {},
1933
- ...stored.surface ? { surface: stored.surface } : {},
1934
- ...stored.traceId ? { traceId: stored.traceId } : {},
1935
- ...turnStartMessageIndex !== void 0 ? { turnStartMessageIndex } : {}
1914
+ ...definedProps({
1915
+ actor: stored.actor,
1916
+ channelName: stored.channelName,
1917
+ cumulativeUsage: stored.cumulativeUsage,
1918
+ dispatchId: stored.dispatchId,
1919
+ dispatchOutcome: stored.dispatchOutcome,
1920
+ errorMessage: stored.errorMessage,
1921
+ loadedSkillNames: stored.loadedSkillNames,
1922
+ modelId: stored.modelId,
1923
+ reasoningLevel: stored.reasoningLevel,
1924
+ resumeReason: stored.resumeReason,
1925
+ resultMessageId: stored.resultMessageId,
1926
+ resumedFromSliceId: stored.resumedFromSliceId,
1927
+ surface: stored.surface,
1928
+ traceId: stored.traceId,
1929
+ turnStartMessageIndex
1930
+ })
1936
1931
  };
1937
1932
  }
1938
1933
  function restoreRuntimeContext(projection, runtimeContext) {
@@ -2027,7 +2022,6 @@ function buildStoredRecord(args) {
2027
2022
  const nowMs = Date.now();
2028
2023
  return {
2029
2024
  version: (args.previousVersion ?? 0) + 1,
2030
- ...args.channelName ? { channelName: args.channelName } : {},
2031
2025
  conversationId: args.conversationId,
2032
2026
  sessionId: args.sessionId,
2033
2027
  sliceId: args.sliceId,
@@ -2036,40 +2030,44 @@ function buildStoredRecord(args) {
2036
2030
  lastProgressAtMs: args.lastProgressAtMs ?? nowMs,
2037
2031
  updatedAtMs: nowMs,
2038
2032
  committedSeq: args.committedSeq,
2039
- ...args.historyVersion !== void 0 ? { historyVersion: args.historyVersion } : {},
2040
- ...args.turnStartSeq !== void 0 ? { turnStartSeq: args.turnStartSeq } : {},
2041
- ...args.runtimeContext && args.runtimeContext.length > 0 ? { runtimeContext: args.runtimeContext } : {},
2042
2033
  cumulativeDurationMs: args.cumulativeDurationMs,
2043
- ...args.cumulativeUsage ? { cumulativeUsage: args.cumulativeUsage } : {},
2044
- ...args.destination ? { destination: args.destination } : {},
2045
- ...args.dispatchId ? { dispatchId: args.dispatchId } : {},
2046
- ...args.dispatchOutcome ? { dispatchOutcome: args.dispatchOutcome } : {},
2047
- ...args.resultMessageId ? { resultMessageId: args.resultMessageId } : {},
2048
- ...args.source ? { source: args.source } : {},
2049
- ...args.actor ? { actor: args.actor } : {},
2050
- ...args.actors ? { actors: args.actors } : {},
2051
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2052
- ...args.modelId ? { modelId: args.modelId } : {},
2053
- ...args.reasoningLevel ? { reasoningLevel: args.reasoningLevel } : {},
2054
- ...args.resumeReason ? { resumeReason: args.resumeReason } : {},
2055
- ...args.errorMessage ? { errorMessage: args.errorMessage } : {},
2056
- ...args.resumedFromSliceId !== void 0 ? { resumedFromSliceId: args.resumedFromSliceId } : {},
2057
- ...args.surface ? { surface: args.surface } : {},
2058
- ...args.traceId ? { traceId: args.traceId } : {}
2034
+ ...definedProps({
2035
+ actor: args.actor,
2036
+ actors: args.actors,
2037
+ channelName: args.channelName,
2038
+ cumulativeUsage: args.cumulativeUsage,
2039
+ dispatchId: args.dispatchId,
2040
+ dispatchOutcome: args.dispatchOutcome,
2041
+ errorMessage: args.errorMessage,
2042
+ historyVersion: args.historyVersion,
2043
+ loadedSkillNames: args.loadedSkillNames,
2044
+ modelId: args.modelId,
2045
+ reasoningLevel: args.reasoningLevel,
2046
+ resumeReason: args.resumeReason,
2047
+ resultMessageId: args.resultMessageId,
2048
+ resumedFromSliceId: args.resumedFromSliceId,
2049
+ runtimeContext: args.runtimeContext && args.runtimeContext.length > 0 ? args.runtimeContext : void 0,
2050
+ surface: args.surface,
2051
+ traceId: args.traceId,
2052
+ turnStartSeq: args.turnStartSeq
2053
+ })
2059
2054
  };
2060
2055
  }
2061
2056
  async function setStoredRecord(args) {
2062
2057
  const stateAdapter = getStateAdapter();
2063
2058
  await stateAdapter.connect();
2059
+ const storedRecord = storedAgentTurnSessionRecordSchema.parse(args.record);
2064
2060
  await recordConversationActivityMetadata({
2065
2061
  conversationStore: args.conversationStore,
2062
+ destination: args.destination,
2066
2063
  destinationVisibility: args.destinationVisibility,
2067
2064
  nowMs: Date.now(),
2068
- summary: args.record
2065
+ source: args.source,
2066
+ summary: storedRecord
2069
2067
  });
2070
2068
  await stateAdapter.set(
2071
- agentTurnSessionKey(args.record.conversationId, args.record.sessionId),
2072
- args.record,
2069
+ agentTurnSessionKey(storedRecord.conversationId, storedRecord.sessionId),
2070
+ storedRecord,
2073
2071
  args.ttlMs
2074
2072
  );
2075
2073
  const {
@@ -2080,10 +2078,10 @@ async function setStoredRecord(args) {
2080
2078
  turnStartSeq: _turnStartSeq,
2081
2079
  runtimeContext: _runtimeContext,
2082
2080
  ...summary
2083
- } = args.record;
2081
+ } = storedRecord;
2084
2082
  await appendAgentTurnSessionSummary(summary, args.ttlMs);
2085
2083
  return materializeAgentTurnSessionRecord(
2086
- args.record,
2084
+ storedRecord,
2087
2085
  {
2088
2086
  messages: [...args.piMessages],
2089
2087
  provenance: [...args.piMessageProvenance]
@@ -2103,37 +2101,39 @@ async function updateAgentTurnSessionState(args) {
2103
2101
  piMessages: args.existing.piMessages,
2104
2102
  piMessageProvenance: args.existing.piMessageProvenance,
2105
2103
  ttlMs: AGENT_TURN_SESSION_TTL_MS,
2106
- ...args.existing.turnStartMessageIndex !== void 0 ? { turnStartMessageIndex: args.existing.turnStartMessageIndex } : {},
2104
+ ...definedProps({
2105
+ turnStartMessageIndex: args.existing.turnStartMessageIndex
2106
+ }),
2107
2107
  record: buildStoredRecord({
2108
2108
  conversationId: args.existing.conversationId,
2109
2109
  sessionId: args.existing.sessionId,
2110
2110
  sliceId: args.existing.sliceId,
2111
2111
  state: args.state,
2112
2112
  committedSeq: parsed.committedSeq,
2113
- ...parsed.historyVersion !== void 0 ? { historyVersion: parsed.historyVersion } : {},
2114
- ...parsed.turnStartSeq !== void 0 ? { turnStartSeq: parsed.turnStartSeq } : {},
2115
- ...parsed.runtimeContext ? { runtimeContext: parsed.runtimeContext } : {},
2116
- ...parsed.channelName ? { channelName: parsed.channelName } : {},
2117
2113
  startedAtMs: parsed.startedAtMs,
2118
2114
  lastProgressAtMs: parsed.lastProgressAtMs,
2119
2115
  previousVersion: parsed.version,
2120
2116
  cumulativeDurationMs: args.existing.cumulativeDurationMs,
2121
- ...args.existing.cumulativeUsage ? { cumulativeUsage: args.existing.cumulativeUsage } : {},
2122
- ...args.existing.destination ? { destination: args.existing.destination } : {},
2123
- ...args.existing.dispatchId ? { dispatchId: args.existing.dispatchId } : {},
2124
- ...args.existing.dispatchOutcome ? { dispatchOutcome: args.existing.dispatchOutcome } : {},
2125
- ...args.existing.resultMessageId ? { resultMessageId: args.existing.resultMessageId } : {},
2126
- ...args.existing.source ? { source: args.existing.source } : {},
2127
- ...args.existing.loadedSkillNames ? { loadedSkillNames: args.existing.loadedSkillNames } : {},
2128
- ...args.existing.modelId ? { modelId: args.existing.modelId } : {},
2129
- ...args.existing.reasoningLevel ? { reasoningLevel: args.existing.reasoningLevel } : {},
2130
- ...args.existing.actor ? { actor: args.existing.actor } : {},
2131
2117
  actors: args.existing.actors,
2132
- ...args.existing.resumeReason ? { resumeReason: args.existing.resumeReason } : {},
2133
- ...args.existing.resumedFromSliceId !== void 0 ? { resumedFromSliceId: args.existing.resumedFromSliceId } : {},
2134
- ...args.existing.surface ? { surface: args.existing.surface } : {},
2135
- ...args.existing.traceId ? { traceId: args.existing.traceId } : {},
2136
- ...args.errorMessage ?? args.existing.errorMessage ? { errorMessage: args.errorMessage ?? args.existing.errorMessage } : {}
2118
+ ...definedProps({
2119
+ actor: args.existing.actor,
2120
+ channelName: parsed.channelName,
2121
+ cumulativeUsage: args.existing.cumulativeUsage,
2122
+ dispatchId: args.existing.dispatchId,
2123
+ dispatchOutcome: args.existing.dispatchOutcome,
2124
+ errorMessage: args.errorMessage ?? args.existing.errorMessage,
2125
+ historyVersion: parsed.historyVersion,
2126
+ loadedSkillNames: args.existing.loadedSkillNames,
2127
+ modelId: args.existing.modelId,
2128
+ reasoningLevel: args.existing.reasoningLevel,
2129
+ resumeReason: args.existing.resumeReason,
2130
+ resultMessageId: args.existing.resultMessageId,
2131
+ resumedFromSliceId: args.existing.resumedFromSliceId,
2132
+ runtimeContext: parsed.runtimeContext,
2133
+ surface: args.existing.surface,
2134
+ traceId: args.existing.traceId,
2135
+ turnStartSeq: parsed.turnStartSeq
2136
+ })
2137
2137
  })
2138
2138
  });
2139
2139
  }
@@ -2171,51 +2171,47 @@ async function upsertAgentTurnSessionRecord(args) {
2171
2171
  const turnStartMessageIndex = durableTurnStartMessageIndex ?? (turnStartSeq === void 0 ? void 0 : commit.messageSeqs.filter((seq) => seq <= turnStartSeq).length);
2172
2172
  return await setStoredRecord({
2173
2173
  conversationStore: args.conversationStore,
2174
+ destination: args.destination,
2174
2175
  destinationVisibility: args.destinationVisibility,
2176
+ source: args.source,
2175
2177
  piMessages: commit.messages,
2176
2178
  piMessageProvenance: commit.provenance,
2177
2179
  ttlMs,
2178
- ...turnStartMessageIndex !== void 0 ? { turnStartMessageIndex } : {},
2180
+ turnStartMessageIndex,
2179
2181
  record: buildStoredRecord({
2180
- ...args.channelName ?? existingRecord?.channelName ? { channelName: args.channelName ?? existingRecord?.channelName } : {},
2181
2182
  conversationId: args.conversationId,
2182
2183
  sessionId: args.sessionId,
2183
2184
  sliceId: args.sliceId,
2184
2185
  state: args.state,
2185
- ...existingRecord?.startedAtMs !== void 0 ? { startedAtMs: existingRecord.startedAtMs } : {},
2186
- ...args.lastProgressAtMs !== void 0 ? { lastProgressAtMs: args.lastProgressAtMs } : {},
2187
2186
  committedSeq: commit.committedSeq,
2188
2187
  historyVersion: commit.historyVersion,
2189
- ...turnStartSeq !== void 0 ? { turnStartSeq } : {},
2190
- ...retainedRuntimeContext ? { runtimeContext: retainedRuntimeContext } : {},
2191
2188
  previousVersion: existingRecord?.version,
2192
2189
  cumulativeDurationMs: args.cumulativeDurationMs ?? existingRecord?.cumulativeDurationMs ?? 0,
2193
- ...args.cumulativeUsage ? { cumulativeUsage: args.cumulativeUsage } : {},
2194
- ...args.destination ?? existingRecord?.destination ? { destination: args.destination ?? existingRecord?.destination } : {},
2195
- ...args.dispatchId ?? existingRecord?.dispatchId ? { dispatchId: args.dispatchId ?? existingRecord?.dispatchId } : {},
2196
- ...args.dispatchOutcome ?? existingRecord?.dispatchOutcome ? {
2197
- dispatchOutcome: args.dispatchOutcome ?? existingRecord?.dispatchOutcome
2198
- } : {},
2199
- ...args.resultMessageId ?? existingRecord?.resultMessageId ? {
2200
- resultMessageId: args.resultMessageId ?? existingRecord?.resultMessageId
2201
- } : {},
2202
- ...args.source ?? existingRecord?.source ? { source: args.source ?? existingRecord?.source } : {},
2203
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2204
2190
  modelId: args.modelId,
2205
- ...args.reasoningLevel ?? existingRecord?.reasoningLevel ? {
2206
- reasoningLevel: args.reasoningLevel ?? existingRecord?.reasoningLevel
2207
- } : {},
2208
- ...args.actor ?? existingRecord?.actor ? { actor: args.actor ?? existingRecord?.actor } : {},
2209
2191
  actors: instructionActors([
2210
2192
  ...(existingRecord?.actors ?? []).map(instructionProvenanceFor),
2211
2193
  ...(args.actors ?? []).map(instructionProvenanceFor),
2212
2194
  ...commit.provenance
2213
2195
  ]),
2214
- ...args.resumeReason ? { resumeReason: args.resumeReason } : {},
2215
- ...args.errorMessage ? { errorMessage: args.errorMessage } : {},
2216
- ...args.resumedFromSliceId !== void 0 ? { resumedFromSliceId: args.resumedFromSliceId } : {},
2217
- ...args.surface ?? existingRecord?.surface ? { surface: args.surface ?? existingRecord?.surface } : {},
2218
- ...args.traceId ?? existingRecord?.traceId ? { traceId: args.traceId ?? existingRecord?.traceId } : {}
2196
+ ...definedProps({
2197
+ actor: args.actor ?? existingRecord?.actor,
2198
+ channelName: args.channelName ?? existingRecord?.channelName,
2199
+ cumulativeUsage: args.cumulativeUsage,
2200
+ dispatchId: args.dispatchId ?? existingRecord?.dispatchId,
2201
+ dispatchOutcome: args.dispatchOutcome ?? existingRecord?.dispatchOutcome,
2202
+ errorMessage: args.errorMessage,
2203
+ lastProgressAtMs: args.lastProgressAtMs,
2204
+ loadedSkillNames: args.loadedSkillNames,
2205
+ reasoningLevel: args.reasoningLevel ?? existingRecord?.reasoningLevel,
2206
+ resumeReason: args.resumeReason,
2207
+ resultMessageId: args.resultMessageId ?? existingRecord?.resultMessageId,
2208
+ resumedFromSliceId: args.resumedFromSliceId,
2209
+ runtimeContext: retainedRuntimeContext,
2210
+ startedAtMs: existingRecord?.startedAtMs,
2211
+ surface: args.surface ?? existingRecord?.surface,
2212
+ traceId: args.traceId ?? existingRecord?.traceId,
2213
+ turnStartSeq
2214
+ })
2219
2215
  })
2220
2216
  });
2221
2217
  }
@@ -2238,7 +2234,6 @@ async function recordAgentTurnSessionSummary(args) {
2238
2234
  const ttlMs = Math.max(1, args.ttlMs ?? AGENT_TURN_SESSION_TTL_MS);
2239
2235
  const summary = {
2240
2236
  version: existing?.version ?? 0,
2241
- ...args.channelName ?? existing?.channelName ? { channelName: args.channelName ?? existing?.channelName } : {},
2242
2237
  conversationId: args.conversationId,
2243
2238
  sessionId: args.sessionId,
2244
2239
  sliceId: args.sliceId,
@@ -2247,24 +2242,27 @@ async function recordAgentTurnSessionSummary(args) {
2247
2242
  state: args.state,
2248
2243
  updatedAtMs: nowMs,
2249
2244
  cumulativeDurationMs: args.cumulativeDurationMs ?? existing?.cumulativeDurationMs ?? 0,
2250
- ...args.cumulativeUsage ?? existing?.cumulativeUsage ? { cumulativeUsage: args.cumulativeUsage ?? existing?.cumulativeUsage } : {},
2251
- ...args.destination ?? existing?.destination ? { destination: args.destination ?? existing?.destination } : {},
2252
- ...args.dispatchId ?? existingDispatchId ? { dispatchId: args.dispatchId ?? existingDispatchId } : {},
2253
- ...args.dispatchOutcome ?? existingDispatchOutcome ? { dispatchOutcome: args.dispatchOutcome ?? existingDispatchOutcome } : {},
2254
- ...args.resultMessageId ?? existingResultMessageId ? { resultMessageId: args.resultMessageId ?? existingResultMessageId } : {},
2255
- ...args.source ?? existing?.source ? { source: args.source ?? existing?.source } : {},
2256
- ...args.actor ?? existing?.actor ? { actor: args.actor ?? existing?.actor } : {},
2257
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : existing?.loadedSkillNames ? { loadedSkillNames: existing.loadedSkillNames } : {},
2258
- ...args.modelId ?? existing?.modelId ? { modelId: args.modelId ?? existing?.modelId } : {},
2259
- ...args.reasoningLevel ?? existing?.reasoningLevel ? { reasoningLevel: args.reasoningLevel ?? existing?.reasoningLevel } : {},
2260
- ...args.resumeReason ? { resumeReason: args.resumeReason } : {},
2261
- ...args.surface ?? existing?.surface ? { surface: args.surface ?? existing?.surface } : {},
2262
- ...args.traceId ?? existing?.traceId ? { traceId: args.traceId ?? existing?.traceId } : {}
2245
+ ...definedProps({
2246
+ actor: args.actor ?? existing?.actor,
2247
+ channelName: args.channelName ?? existing?.channelName,
2248
+ cumulativeUsage: args.cumulativeUsage ?? existing?.cumulativeUsage,
2249
+ dispatchId: args.dispatchId ?? existingDispatchId,
2250
+ dispatchOutcome: args.dispatchOutcome ?? existingDispatchOutcome,
2251
+ loadedSkillNames: args.loadedSkillNames ?? existing?.loadedSkillNames,
2252
+ modelId: args.modelId ?? existing?.modelId,
2253
+ reasoningLevel: args.reasoningLevel ?? existing?.reasoningLevel,
2254
+ resumeReason: args.resumeReason,
2255
+ resultMessageId: args.resultMessageId ?? existingResultMessageId,
2256
+ surface: args.surface ?? existing?.surface,
2257
+ traceId: args.traceId ?? existing?.traceId
2258
+ })
2263
2259
  };
2264
2260
  await recordConversationActivityMetadata({
2265
2261
  conversationStore: args.conversationStore,
2262
+ destination: args.destination,
2266
2263
  destinationVisibility: args.destinationVisibility,
2267
2264
  nowMs,
2265
+ source: args.source,
2268
2266
  summary
2269
2267
  });
2270
2268
  await appendAgentTurnSessionSummary(summary, ttlMs);
@@ -2339,6 +2337,24 @@ async function failAgentTurnSessionRecord(args) {
2339
2337
  });
2340
2338
  }
2341
2339
 
2340
+ // src/chat/services/persist-retry.ts
2341
+ var PERSIST_ATTEMPTS = 3;
2342
+ async function persistWithRetry(persist) {
2343
+ let lastError;
2344
+ for (let attempt = 1; attempt <= PERSIST_ATTEMPTS; attempt += 1) {
2345
+ try {
2346
+ await persist();
2347
+ return;
2348
+ } catch (error) {
2349
+ lastError = error;
2350
+ if (attempt < PERSIST_ATTEMPTS) {
2351
+ await sleep(attempt * 100);
2352
+ }
2353
+ }
2354
+ }
2355
+ throw lastError;
2356
+ }
2357
+
2342
2358
  // src/chat/services/turn-limit.ts
2343
2359
  var TurnSliceLimitExceededError = class extends Error {
2344
2360
  constructor(maxSlices) {
@@ -2351,6 +2367,32 @@ function buildTurnLimitResponse(eventId) {
2351
2367
  }
2352
2368
 
2353
2369
  // src/chat/services/turn-session-record.ts
2370
+ function definedProps2(values) {
2371
+ return Object.fromEntries(
2372
+ Object.entries(values).filter(([, value]) => value !== void 0)
2373
+ );
2374
+ }
2375
+ function sessionWriteContext(args, latest) {
2376
+ return {
2377
+ conversationId: args.conversationId,
2378
+ modelId: args.modelId,
2379
+ sessionId: args.sessionId,
2380
+ ...definedProps2({
2381
+ actor: args.actor ?? latest?.actor,
2382
+ channelName: args.channelName ?? latest?.channelName,
2383
+ destination: args.destination,
2384
+ destinationVisibility: args.destinationVisibility,
2385
+ dispatchId: args.dispatchId ?? latest?.dispatchId,
2386
+ // Caller-resolved: some paths inherit these, others stay caller-only.
2387
+ loadedSkillNames: args.loadedSkillNames,
2388
+ reasoningLevel: args.reasoningLevel,
2389
+ source: args.source,
2390
+ surface: args.surface ?? latest?.surface,
2391
+ traceId: getActiveTraceId() ?? latest?.traceId,
2392
+ turnStartMessageIndex: args.turnStartMessageIndex ?? latest?.turnStartMessageIndex
2393
+ })
2394
+ };
2395
+ }
2354
2396
  function logSessionRecordError(error, eventName, args, attributes) {
2355
2397
  logException(error, eventName, {
2356
2398
  "app.ai.resume_conversation_id": args.conversationId,
@@ -2398,29 +2440,33 @@ async function persistRunningSessionRecord(args) {
2398
2440
  args.sessionId
2399
2441
  );
2400
2442
  await upsertAgentTurnSessionRecord({
2401
- ...args.channelName ?? latestSessionRecord?.channelName ? { channelName: args.channelName ?? latestSessionRecord?.channelName } : {},
2402
- conversationId: args.conversationId,
2443
+ ...sessionWriteContext(
2444
+ {
2445
+ actor: args.actor,
2446
+ channelName: args.channelName,
2447
+ conversationId: args.conversationId,
2448
+ destination: args.destination,
2449
+ destinationVisibility: args.destinationVisibility,
2450
+ dispatchId: args.dispatchId,
2451
+ loadedSkillNames: args.loadedSkillNames,
2452
+ modelId: args.modelId,
2453
+ reasoningLevel: args.reasoningLevel,
2454
+ sessionId: args.sessionId,
2455
+ source: args.source,
2456
+ surface: args.surface,
2457
+ turnStartMessageIndex: args.turnStartMessageIndex
2458
+ },
2459
+ latestSessionRecord
2460
+ ),
2461
+ ...definedProps2({
2462
+ trailingMessageProvenance: args.trailingMessageProvenance,
2463
+ turnContexts: args.turnContexts
2464
+ }),
2403
2465
  cumulativeDurationMs: latestSessionRecord?.cumulativeDurationMs,
2404
2466
  cumulativeUsage: latestSessionRecord?.cumulativeUsage,
2405
- ...args.destination ?? latestSessionRecord?.destination ? { destination: args.destination ?? latestSessionRecord?.destination } : {},
2406
- destinationVisibility: args.destinationVisibility,
2407
- ...args.dispatchId ?? latestSessionRecord?.dispatchId ? { dispatchId: args.dispatchId ?? latestSessionRecord?.dispatchId } : {},
2408
- ...args.source ?? latestSessionRecord?.source ? { source: args.source ?? latestSessionRecord?.source } : {},
2409
- sessionId: args.sessionId,
2410
- sliceId: args.sliceId,
2411
- state: "running",
2412
2467
  piMessages: args.messages,
2413
- ...args.trailingMessageProvenance ? { trailingMessageProvenance: args.trailingMessageProvenance } : {},
2414
- ...args.surface ?? latestSessionRecord?.surface ? { surface: args.surface ?? latestSessionRecord?.surface } : {},
2415
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2416
- modelId: args.modelId,
2417
- ...args.reasoningLevel ? { reasoningLevel: args.reasoningLevel } : {},
2418
- ...args.turnContexts ? { turnContexts: args.turnContexts } : {},
2419
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2420
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {},
2421
- ...(args.turnStartMessageIndex ?? latestSessionRecord?.turnStartMessageIndex) !== void 0 ? {
2422
- turnStartMessageIndex: args.turnStartMessageIndex ?? latestSessionRecord?.turnStartMessageIndex
2423
- } : {}
2468
+ sliceId: args.sliceId,
2469
+ state: "running"
2424
2470
  });
2425
2471
  return true;
2426
2472
  } catch (recordError) {
@@ -2449,45 +2495,41 @@ async function persistCompletedSessionRecord(args) {
2449
2495
  "Completed session record requires a slice id from the caller or the latest stored record"
2450
2496
  );
2451
2497
  }
2452
- const modelId = args.modelId;
2453
- const reasoningLevel = args.reasoningLevel ?? latestSessionRecord?.reasoningLevel;
2454
2498
  const target = {
2455
- ...args.channelName ?? latestSessionRecord?.channelName ? { channelName: args.channelName ?? latestSessionRecord?.channelName } : {},
2456
- conversationId: args.conversationId,
2457
- cumulativeDurationMs: addDurationMs(
2458
- latestSessionRecord?.cumulativeDurationMs,
2459
- args.currentDurationMs
2460
- ),
2461
- cumulativeUsage: addAgentTurnUsage(
2462
- latestSessionRecord?.cumulativeUsage,
2463
- args.currentUsage
2499
+ ...sessionWriteContext(
2500
+ {
2501
+ actor: args.actor,
2502
+ channelName: args.channelName,
2503
+ conversationId: args.conversationId,
2504
+ destination: args.destination,
2505
+ destinationVisibility: args.destinationVisibility,
2506
+ dispatchId: args.dispatchId,
2507
+ loadedSkillNames: args.loadedSkillNames ?? latestSessionRecord?.loadedSkillNames,
2508
+ modelId: args.modelId,
2509
+ reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel,
2510
+ sessionId: args.sessionId,
2511
+ source: args.source,
2512
+ surface: args.surface,
2513
+ turnStartMessageIndex: args.turnStartMessageIndex
2514
+ },
2515
+ latestSessionRecord
2464
2516
  ),
2465
- ...args.destination ?? latestSessionRecord?.destination ? { destination: args.destination ?? latestSessionRecord?.destination } : {},
2466
- ...args.dispatchId ?? latestSessionRecord?.dispatchId ? { dispatchId: args.dispatchId ?? latestSessionRecord?.dispatchId } : {},
2467
- ...args.dispatchOutcome ?? latestSessionRecord?.dispatchOutcome ? {
2468
- dispatchOutcome: args.dispatchOutcome ?? latestSessionRecord?.dispatchOutcome
2469
- } : {},
2470
- ...args.errorMessage ? { errorMessage: args.errorMessage } : {},
2471
- ...args.resultMessageId ?? latestSessionRecord?.resultMessageId ? {
2517
+ ...definedProps2({
2518
+ cumulativeDurationMs: addDurationMs(
2519
+ latestSessionRecord?.cumulativeDurationMs,
2520
+ args.currentDurationMs
2521
+ ),
2522
+ cumulativeUsage: addAgentTurnUsage(
2523
+ latestSessionRecord?.cumulativeUsage,
2524
+ args.currentUsage
2525
+ ),
2526
+ dispatchOutcome: args.dispatchOutcome ?? latestSessionRecord?.dispatchOutcome,
2527
+ errorMessage: args.errorMessage,
2472
2528
  resultMessageId: args.resultMessageId ?? latestSessionRecord?.resultMessageId
2473
- } : {},
2474
- ...args.source ?? latestSessionRecord?.source ? { source: args.source ?? latestSessionRecord?.source } : {},
2475
- ...args.destinationVisibility ? { destinationVisibility: args.destinationVisibility } : {},
2476
- sessionId: args.sessionId,
2477
- sliceId,
2478
- state: "completed",
2529
+ }),
2479
2530
  piMessages: args.allMessages,
2480
- ...args.surface ?? latestSessionRecord?.surface ? { surface: args.surface ?? latestSessionRecord?.surface } : {},
2481
- ...args.loadedSkillNames ?? latestSessionRecord?.loadedSkillNames ? {
2482
- loadedSkillNames: args.loadedSkillNames ?? latestSessionRecord?.loadedSkillNames
2483
- } : {},
2484
- modelId,
2485
- ...reasoningLevel ? { reasoningLevel } : {},
2486
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2487
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {},
2488
- ...(args.turnStartMessageIndex ?? latestSessionRecord?.turnStartMessageIndex) !== void 0 ? {
2489
- turnStartMessageIndex: args.turnStartMessageIndex ?? latestSessionRecord?.turnStartMessageIndex
2490
- } : {}
2531
+ sliceId,
2532
+ state: "completed"
2491
2533
  };
2492
2534
  await persistWithRetry(async () => {
2493
2535
  await upsertAgentTurnSessionRecord(target);
@@ -2532,35 +2574,40 @@ async function persistAuthPauseSessionRecord(args) {
2532
2574
  return void 0;
2533
2575
  }
2534
2576
  return await upsertAgentTurnSessionRecord({
2535
- ...args.channelName ?? latestSessionRecord?.channelName ? { channelName: args.channelName ?? latestSessionRecord?.channelName } : {},
2536
- conversationId: args.conversationId,
2537
- cumulativeDurationMs: addDurationMs(
2538
- latestSessionRecord?.cumulativeDurationMs,
2539
- args.currentDurationMs
2540
- ),
2541
- cumulativeUsage: addAgentTurnUsage(
2542
- latestSessionRecord?.cumulativeUsage,
2543
- args.currentUsage
2577
+ ...sessionWriteContext(
2578
+ {
2579
+ actor: args.actor,
2580
+ channelName: args.channelName,
2581
+ conversationId: args.conversationId,
2582
+ destination: args.destination,
2583
+ destinationVisibility: args.destinationVisibility,
2584
+ dispatchId: args.dispatchId,
2585
+ // Auth-pause keeps caller-owned skill names only.
2586
+ loadedSkillNames: args.loadedSkillNames,
2587
+ modelId: args.modelId,
2588
+ reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel,
2589
+ sessionId: args.sessionId,
2590
+ source: args.source,
2591
+ surface: args.surface
2592
+ },
2593
+ latestSessionRecord
2544
2594
  ),
2545
- ...args.destination ?? latestSessionRecord?.destination ? { destination: args.destination ?? latestSessionRecord?.destination } : {},
2546
- destinationVisibility: args.destinationVisibility,
2547
- ...args.dispatchId ?? latestSessionRecord?.dispatchId ? { dispatchId: args.dispatchId ?? latestSessionRecord?.dispatchId } : {},
2548
- ...args.source ?? latestSessionRecord?.source ? { source: args.source ?? latestSessionRecord?.source } : {},
2549
- sessionId: args.sessionId,
2550
- sliceId: nextSliceId,
2551
- state: "awaiting_resume",
2595
+ ...definedProps2({
2596
+ cumulativeDurationMs: addDurationMs(
2597
+ latestSessionRecord?.cumulativeDurationMs,
2598
+ args.currentDurationMs
2599
+ ),
2600
+ cumulativeUsage: addAgentTurnUsage(
2601
+ latestSessionRecord?.cumulativeUsage,
2602
+ args.currentUsage
2603
+ )
2604
+ }),
2605
+ errorMessage: args.errorMessage,
2552
2606
  piMessages,
2553
- ...args.surface ?? latestSessionRecord?.surface ? { surface: args.surface ?? latestSessionRecord?.surface } : {},
2554
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2555
- modelId: args.modelId,
2556
- ...args.reasoningLevel ?? latestSessionRecord?.reasoningLevel ? {
2557
- reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel
2558
- } : {},
2559
2607
  resumeReason: "auth",
2560
2608
  resumedFromSliceId: args.currentSliceId,
2561
- errorMessage: args.errorMessage,
2562
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2563
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {}
2609
+ sliceId: nextSliceId,
2610
+ state: "awaiting_resume"
2564
2611
  });
2565
2612
  } catch (recordError) {
2566
2613
  logSessionRecordError(
@@ -2597,63 +2644,51 @@ async function persistContinuationSessionRecord(args) {
2597
2644
  latestSessionRecord?.cumulativeUsage,
2598
2645
  args.currentUsage
2599
2646
  );
2647
+ const shared = {
2648
+ ...sessionWriteContext(
2649
+ {
2650
+ actor: args.actor,
2651
+ channelName: args.channelName,
2652
+ conversationId: args.conversationId,
2653
+ destination: args.destination,
2654
+ destinationVisibility: args.destinationVisibility,
2655
+ dispatchId: args.dispatchId,
2656
+ // Continuation keeps caller-owned skill names only.
2657
+ loadedSkillNames: args.loadedSkillNames,
2658
+ modelId: args.modelId,
2659
+ reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel,
2660
+ sessionId: args.sessionId,
2661
+ source: args.source,
2662
+ surface: args.surface
2663
+ },
2664
+ latestSessionRecord
2665
+ ),
2666
+ ...definedProps2({
2667
+ cumulativeDurationMs,
2668
+ cumulativeUsage
2669
+ }),
2670
+ piMessages,
2671
+ resumeReason: args.resumeReason
2672
+ };
2600
2673
  if (nextSliceId > botConfig.maxSlicesPerTurn) {
2601
2674
  return await upsertAgentTurnSessionRecord({
2602
- ...args.channelName ?? latestSessionRecord?.channelName ? {
2603
- channelName: args.channelName ?? latestSessionRecord?.channelName
2604
- } : {},
2605
- conversationId: args.conversationId,
2606
- cumulativeDurationMs,
2607
- cumulativeUsage,
2608
- ...args.destination ?? latestSessionRecord?.destination ? {
2609
- destination: args.destination ?? latestSessionRecord?.destination
2610
- } : {},
2611
- destinationVisibility: args.destinationVisibility,
2612
- ...args.dispatchId ?? latestSessionRecord?.dispatchId ? { dispatchId: args.dispatchId ?? latestSessionRecord?.dispatchId } : {},
2613
- ...args.source ?? latestSessionRecord?.source ? { source: args.source ?? latestSessionRecord?.source } : {},
2614
- sessionId: args.sessionId,
2615
- sliceId: args.currentSliceId,
2616
- state: "failed",
2617
- piMessages,
2618
- ...args.surface ?? latestSessionRecord?.surface ? { surface: args.surface ?? latestSessionRecord?.surface } : {},
2619
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2620
- modelId: args.modelId,
2621
- ...args.reasoningLevel ?? latestSessionRecord?.reasoningLevel ? {
2622
- reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel
2623
- } : {},
2624
- resumeReason: args.resumeReason,
2625
- resumedFromSliceId: latestSessionRecord?.resumedFromSliceId,
2675
+ ...shared,
2676
+ ...definedProps2({
2677
+ resumedFromSliceId: latestSessionRecord?.resumedFromSliceId
2678
+ }),
2626
2679
  errorMessage: new TurnSliceLimitExceededError(
2627
2680
  botConfig.maxSlicesPerTurn
2628
2681
  ).message,
2629
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2630
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {}
2682
+ sliceId: args.currentSliceId,
2683
+ state: "failed"
2631
2684
  });
2632
2685
  }
2633
2686
  return await upsertAgentTurnSessionRecord({
2634
- ...args.channelName ?? latestSessionRecord?.channelName ? { channelName: args.channelName ?? latestSessionRecord?.channelName } : {},
2635
- conversationId: args.conversationId,
2636
- cumulativeDurationMs,
2637
- cumulativeUsage,
2638
- ...args.destination ?? latestSessionRecord?.destination ? { destination: args.destination ?? latestSessionRecord?.destination } : {},
2639
- destinationVisibility: args.destinationVisibility,
2640
- ...args.dispatchId ?? latestSessionRecord?.dispatchId ? { dispatchId: args.dispatchId ?? latestSessionRecord?.dispatchId } : {},
2641
- ...args.source ?? latestSessionRecord?.source ? { source: args.source ?? latestSessionRecord?.source } : {},
2642
- sessionId: args.sessionId,
2643
- sliceId: nextSliceId,
2644
- state: "awaiting_resume",
2645
- piMessages,
2646
- ...args.surface ?? latestSessionRecord?.surface ? { surface: args.surface ?? latestSessionRecord?.surface } : {},
2647
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2648
- modelId: args.modelId,
2649
- ...args.reasoningLevel ?? latestSessionRecord?.reasoningLevel ? {
2650
- reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel
2651
- } : {},
2652
- resumeReason: args.resumeReason,
2653
- resumedFromSliceId: args.currentSliceId,
2687
+ ...shared,
2654
2688
  errorMessage: args.errorMessage,
2655
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2656
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {}
2689
+ resumedFromSliceId: args.currentSliceId,
2690
+ sliceId: nextSliceId,
2691
+ state: "awaiting_resume"
2657
2692
  });
2658
2693
  } catch (recordError) {
2659
2694
  logSessionRecordError(
@@ -2679,34 +2714,39 @@ async function persistYieldSessionRecord(args) {
2679
2714
  args.sessionId
2680
2715
  );
2681
2716
  return await upsertAgentTurnSessionRecord({
2682
- ...args.channelName ?? latestSessionRecord?.channelName ? { channelName: args.channelName ?? latestSessionRecord?.channelName } : {},
2683
- conversationId: args.conversationId,
2684
- cumulativeDurationMs: addDurationMs(
2685
- latestSessionRecord?.cumulativeDurationMs,
2686
- args.currentDurationMs
2687
- ),
2688
- cumulativeUsage: addAgentTurnUsage(
2689
- latestSessionRecord?.cumulativeUsage,
2690
- args.currentUsage
2717
+ ...sessionWriteContext(
2718
+ {
2719
+ actor: args.actor,
2720
+ channelName: args.channelName,
2721
+ conversationId: args.conversationId,
2722
+ destination: args.destination,
2723
+ dispatchId: args.dispatchId,
2724
+ // Yield keeps caller-owned skill names only.
2725
+ loadedSkillNames: args.loadedSkillNames,
2726
+ modelId: args.modelId,
2727
+ reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel,
2728
+ sessionId: args.sessionId,
2729
+ source: args.source,
2730
+ surface: args.surface
2731
+ },
2732
+ latestSessionRecord
2691
2733
  ),
2692
- ...args.destination ?? latestSessionRecord?.destination ? { destination: args.destination ?? latestSessionRecord?.destination } : {},
2693
- ...args.dispatchId ?? latestSessionRecord?.dispatchId ? { dispatchId: args.dispatchId ?? latestSessionRecord?.dispatchId } : {},
2694
- ...args.source ?? latestSessionRecord?.source ? { source: args.source ?? latestSessionRecord?.source } : {},
2695
- sessionId: args.sessionId,
2696
- sliceId: args.currentSliceId,
2697
- state: "awaiting_resume",
2734
+ ...definedProps2({
2735
+ cumulativeDurationMs: addDurationMs(
2736
+ latestSessionRecord?.cumulativeDurationMs,
2737
+ args.currentDurationMs
2738
+ ),
2739
+ cumulativeUsage: addAgentTurnUsage(
2740
+ latestSessionRecord?.cumulativeUsage,
2741
+ args.currentUsage
2742
+ ),
2743
+ resumedFromSliceId: latestSessionRecord?.resumedFromSliceId
2744
+ }),
2745
+ errorMessage: args.errorMessage,
2698
2746
  piMessages,
2699
- ...args.surface ?? latestSessionRecord?.surface ? { surface: args.surface ?? latestSessionRecord?.surface } : {},
2700
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2701
- modelId: args.modelId,
2702
- ...args.reasoningLevel ?? latestSessionRecord?.reasoningLevel ? {
2703
- reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel
2704
- } : {},
2705
2747
  resumeReason: "yield",
2706
- resumedFromSliceId: latestSessionRecord?.resumedFromSliceId,
2707
- errorMessage: args.errorMessage,
2708
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2709
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {}
2748
+ sliceId: args.currentSliceId,
2749
+ state: "awaiting_resume"
2710
2750
  });
2711
2751
  } catch (recordError) {
2712
2752
  logSessionRecordError(
@@ -3086,9 +3126,6 @@ function createChannelConfigurationService(storage) {
3086
3126
  };
3087
3127
  }
3088
3128
 
3089
- // src/chat/state/ttl.ts
3090
- var JUNIOR_THREAD_STATE_TTL_MS = 7 * 24 * 60 * 60 * 1e3;
3091
-
3092
3129
  // src/chat/runtime/thread-state.ts
3093
3130
  function threadStateKey(threadId) {
3094
3131
  return `thread-state:${threadId}`;
@@ -3381,7 +3418,6 @@ export {
3381
3418
  persistAuthPauseSessionRecord,
3382
3419
  persistContinuationSessionRecord,
3383
3420
  persistYieldSessionRecord,
3384
- JUNIOR_THREAD_STATE_TTL_MS,
3385
3421
  canReusePendingAuthLink,
3386
3422
  getConversationPendingAuth,
3387
3423
  clearPendingAuth,