@sentry/junior 0.137.0 → 0.139.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-MZOKEP3J.js → agent-hooks-MJXMD7JP.js} +11 -9
  2. package/dist/api/schema/task.d.ts +100 -1
  3. package/dist/api/schema.d.ts +2 -2
  4. package/dist/api/schema.js +9 -1
  5. package/dist/api.js +202 -55
  6. package/dist/app.d.ts +7 -0
  7. package/dist/app.js +601 -1356
  8. package/dist/{catalog-runtime-MD66YLFV.js → catalog-runtime-GCCFYPIK.js} +6 -6
  9. package/dist/chat/agent/request.d.ts +44 -4
  10. package/dist/chat/agent-dispatch/heartbeat.d.ts +5 -0
  11. package/dist/chat/agent-dispatch/types.d.ts +1 -1
  12. package/dist/chat/agent-invocations/errors.d.ts +8 -0
  13. package/dist/chat/agent-invocations/spawn.d.ts +12 -0
  14. package/dist/chat/agent-invocations/store.d.ts +44 -0
  15. package/dist/chat/agent-invocations/types.d.ts +449 -0
  16. package/dist/chat/agent-invocations/work.d.ts +31 -0
  17. package/dist/chat/config.d.ts +1 -0
  18. package/dist/chat/conversations/sql/store.d.ts +8 -0
  19. package/dist/chat/conversations/store.d.ts +7 -0
  20. package/dist/chat/credentials/context.d.ts +14 -0
  21. package/dist/chat/experimental.d.ts +16 -0
  22. package/dist/chat/resource-events/actor.d.ts +23 -0
  23. package/dist/chat/runtime/agent-runner.d.ts +2 -1
  24. package/dist/chat/services/auth-pause.d.ts +0 -1
  25. package/dist/chat/services/mcp-auth-orchestration.d.ts +3 -2
  26. package/dist/chat/services/plugin-auth-orchestration.d.ts +3 -2
  27. package/dist/chat/state/turn-session.d.ts +1 -5
  28. package/dist/chat/task-execution/state.d.ts +23 -11
  29. package/dist/chat/task-execution/store.d.ts +20 -11
  30. package/dist/chat/task-execution/worker.d.ts +2 -2
  31. package/dist/chat/tasks/execution-stats.d.ts +34 -3
  32. package/dist/chat/tasks/read.d.ts +3 -1
  33. package/dist/chat/tools/runtime/spawn-agent.d.ts +31 -0
  34. package/dist/chat/tools/types.d.ts +2 -0
  35. package/dist/{chunk-T6Z5S6PQ.js → chunk-2ECYPYKP.js} +1 -1
  36. package/dist/chunk-3T2JAGRH.js +115 -0
  37. package/dist/chunk-3Y47VMHV.js +6 -0
  38. package/dist/chunk-4V4QIYJH.js +41 -0
  39. package/dist/{chunk-SYCX4DOK.js → chunk-5GSZWRE2.js} +434 -65
  40. package/dist/chunk-5IDFPCON.js +46 -0
  41. package/dist/{chunk-36EG34YI.js → chunk-6OHTDR4F.js} +1 -1
  42. package/dist/chunk-6SU2DT27.js +441 -0
  43. package/dist/{chunk-UG6ZYPBI.js → chunk-75I3UBIL.js} +73 -60
  44. package/dist/chunk-AEQISMMU.js +711 -0
  45. package/dist/{chunk-5PNJMOY5.js → chunk-C2N6MNO4.js} +9 -2
  46. package/dist/{chunk-BZAMASJP.js → chunk-CXH25EL4.js} +2 -2
  47. package/dist/{chunk-K2NCQJKH.js → chunk-DW4PMHNW.js} +3 -3
  48. package/dist/chunk-EM225ITJ.js +56 -0
  49. package/dist/{chunk-UOXATPSI.js → chunk-G52UXKGO.js} +7 -5
  50. package/dist/chunk-GI52BW74.js +207 -0
  51. package/dist/{chunk-3Q342EJJ.js → chunk-GWQSZT4C.js} +2 -2
  52. package/dist/{chunk-EPBBYMNE.js → chunk-HUO3UTAQ.js} +1 -1
  53. package/dist/{chunk-WPPXIAFB.js → chunk-IIESAYDL.js} +17 -434
  54. package/dist/chunk-IUFM5JWV.js +24 -0
  55. package/dist/{chunk-UZQ4VY3M.js → chunk-J3S57AXY.js} +1 -1
  56. package/dist/{chunk-DQXAAVTR.js → chunk-J6EJQCHY.js} +46 -1471
  57. package/dist/{chunk-6ZK6FZIV.js → chunk-KCBZSJRU.js} +6 -27
  58. package/dist/chunk-MQ5RYATN.js +1523 -0
  59. package/dist/{chunk-5G34IEK2.js → chunk-MXTVUV3I.js} +2 -56
  60. package/dist/{chunk-YRPC6MJY.js → chunk-MXVD2BDS.js} +1 -1
  61. package/dist/{chunk-POBSY47Q.js → chunk-PJW6U46K.js} +337 -306
  62. package/dist/chunk-Q6QZTTLS.js +55 -0
  63. package/dist/{chunk-ZFS67WIR.js → chunk-QTBKPZDA.js} +57 -6
  64. package/dist/chunk-R776HKPK.js +81 -0
  65. package/dist/{chunk-77VDLF3Z.js → chunk-RUIXF3YQ.js} +10 -0
  66. package/dist/chunk-U27DSXKB.js +714 -0
  67. package/dist/{chunk-XZ7NPXAB.js → chunk-U7CMWQ7Z.js} +1 -1
  68. package/dist/{chunk-RB4NJKEX.js → chunk-UDBUS533.js} +125 -51
  69. package/dist/chunk-WQRX57EA.js +668 -0
  70. package/dist/chunk-WZH4JV7R.js +320 -0
  71. package/dist/{chunk-HCIVH3JS.js → chunk-YCTDA7BD.js} +28 -0
  72. package/dist/{chunk-YNFERGKP.js → chunk-YGZ2OLXL.js} +1 -1
  73. package/dist/{chunk-EWDSB24B.js → chunk-ZQCGQMF2.js} +16 -776
  74. package/dist/{chunk-KJ4HGRHN.js → chunk-ZYQSPQYR.js} +515 -601
  75. package/dist/cli/chat.js +68 -29
  76. package/dist/cli/check.js +7 -7
  77. package/dist/cli/plugins.js +15 -13
  78. package/dist/cli/snapshot-create.js +11 -10
  79. package/dist/cli/upgrade.js +8 -8
  80. package/dist/conversation-work-KATFHLG3.js +56 -0
  81. package/dist/db/schema/agent-invocations.d.ts +544 -0
  82. package/dist/db/schema/task-executions.d.ts +23 -2
  83. package/dist/db/schema.d.ts +563 -2
  84. package/dist/{db-66E7VDNC.js → db-UXPPGHDF.js} +7 -7
  85. package/dist/experimental-HH6OM6UH.js +13 -0
  86. package/dist/instrumentation.js +4 -4
  87. package/dist/nitro.js +4 -3
  88. package/dist/{oauth-callback-server-7QPD4Q7P.js → oauth-callback-server-4ZYMD5UA.js} +28 -19
  89. package/dist/{runner-L2BKJKOD.js → runner-MZSEONRZ.js} +32 -18
  90. package/dist/{sandbox-egress-signals-A5HWIQH4.js → sandbox-egress-signals-EKPAVWQL.js} +2 -2
  91. package/dist/{skills-HOQ2NSPW.js → skills-IMVPXSQ7.js} +7 -7
  92. package/dist/spawn-S5LSVOCE.js +38 -0
  93. package/dist/{validation-ENHI7S2C.js → validation-ZKCY2BVR.js} +7 -7
  94. package/dist/version.js +1 -1
  95. package/dist/work-GG3XDYRX.js +46 -0
  96. package/dist/worker-23AUELCJ.js +24 -0
  97. package/migrations/0021_first_warhawk.sql +35 -0
  98. package/migrations/0022_task_execution_status.sql +3 -0
  99. package/migrations/meta/0021_snapshot.json +2176 -0
  100. package/migrations/meta/0022_snapshot.json +2186 -0
  101. package/migrations/meta/_journal.json +15 -1
  102. package/package.json +4 -4
  103. package/dist/chunk-6QW2RRQT.js +0 -233
@@ -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-ZFS67WIR.js";
24
+ } from "./chunk-QTBKPZDA.js";
22
25
  import {
23
26
  SANDBOX_WORKSPACE_ROOT,
24
- getStateAdapter,
25
27
  sleep
26
- } from "./chunk-5G34IEK2.js";
28
+ } from "./chunk-EM225ITJ.js";
29
+ import {
30
+ getStateAdapter
31
+ } from "./chunk-MXTVUV3I.js";
27
32
  import {
28
33
  botConfig,
29
34
  escapeXml,
30
35
  resolveGatewayModel,
31
36
  unwrapCurrentInstruction
32
- } from "./chunk-UG6ZYPBI.js";
37
+ } from "./chunk-75I3UBIL.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-T6Z5S6PQ.js";
52
+ } from "./chunk-2ECYPYKP.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,18 +1801,13 @@ var agentTurnSessionSummarySchema = z2.object({
1811
1801
  conversationId: z2.string().min(1),
1812
1802
  cumulativeDurationMs: nonNegativeNumberSchema,
1813
1803
  cumulativeUsage: agentTurnUsageSchema.optional(),
1814
- // TODO(#1267): Remove destination/source and their Redis write paths once
1815
- // SQL-only readers are deployed; existing records do not need to expire.
1816
- destination: destinationSchema.optional(),
1817
1804
  dispatchId: z2.string().min(1).optional(),
1818
1805
  dispatchOutcome: z2.enum(["blocked", "completed", "failed"]).optional(),
1819
1806
  resultMessageId: z2.string().min(1).optional(),
1820
- source: sourceSchema.optional(),
1821
1807
  lastProgressAtMs: nonNegativeNumberSchema,
1822
1808
  loadedSkillNames: z2.array(z2.string()).optional(),
1823
1809
  modelId: z2.string().min(1).optional(),
1824
1810
  reasoningLevel: z2.string().min(1).optional(),
1825
- actor: actorSchema.optional(),
1826
1811
  resumeReason: agentTurnResumeReasonSchema.optional(),
1827
1812
  resumedFromSliceId: z2.number().int().nonnegative().optional(),
1828
1813
  sessionId: z2.string().min(1),
@@ -1832,7 +1817,7 @@ var agentTurnSessionSummarySchema = z2.object({
1832
1817
  surface: agentTurnSurfaceSchema.optional(),
1833
1818
  traceId: z2.string().optional(),
1834
1819
  updatedAtMs: nonNegativeNumberSchema
1835
- }).strict();
1820
+ }).strip();
1836
1821
  var storedAgentTurnSessionRecordSchema = agentTurnSessionSummarySchema.extend({
1837
1822
  actors: z2.array(actorSchema).optional(),
1838
1823
  committedSeq: seqCursorSchema,
@@ -1840,7 +1825,7 @@ var storedAgentTurnSessionRecordSchema = agentTurnSessionSummarySchema.extend({
1840
1825
  errorMessage: z2.string().optional(),
1841
1826
  turnStartSeq: seqCursorSchema.optional(),
1842
1827
  runtimeContext: z2.array(piMessageSchema).optional()
1843
- }).strict();
1828
+ }).strip();
1844
1829
  function conversationExecutionFromSummary(summary) {
1845
1830
  const status = summary.state === "completed" || summary.state === "abandoned" ? "idle" : summary.state;
1846
1831
  return {
@@ -1859,55 +1844,60 @@ function parseAgentTurnSessionSummary(value) {
1859
1844
  return agentTurnSessionSummarySchema.parse(value);
1860
1845
  }
1861
1846
  async function appendAgentTurnSessionSummary(summary, ttlMs) {
1847
+ const projectedSummary = agentTurnSessionSummarySchema.parse(summary);
1862
1848
  const stateAdapter = getStateAdapter();
1863
1849
  await Promise.all([
1864
- stateAdapter.appendToList(AGENT_TURN_SESSION_INDEX_KEY, summary, {
1850
+ stateAdapter.appendToList(AGENT_TURN_SESSION_INDEX_KEY, projectedSummary, {
1865
1851
  maxLength: AGENT_TURN_SESSION_INDEX_MAX_LENGTH,
1866
1852
  ttlMs
1867
1853
  }),
1868
1854
  stateAdapter.appendToList(
1869
- agentTurnSessionConversationIndexKey(summary.conversationId),
1870
- summary,
1855
+ agentTurnSessionConversationIndexKey(projectedSummary.conversationId),
1856
+ projectedSummary,
1871
1857
  { ttlMs }
1872
1858
  )
1873
1859
  ]);
1874
1860
  }
1875
1861
  async function recordConversationActivityMetadata(args) {
1876
1862
  const conversationStore = args.conversationStore ?? getConversationStore();
1877
- const source = args.summary.destination?.platform === "local" ? "local" : args.summary.surface;
1863
+ const conversation = await conversationStore.get({
1864
+ conversationId: args.summary.conversationId
1865
+ });
1866
+ const isChild = Boolean(conversation?.lineage);
1867
+ const destination = isChild ? void 0 : args.destination;
1868
+ const activitySource = isChild ? "internal" : destination?.platform === "local" ? "local" : destination ? args.summary.surface : void 0;
1878
1869
  await conversationStore.recordActivity({
1879
1870
  activityAtMs: args.summary.updatedAtMs,
1880
1871
  channelName: args.summary.channelName,
1881
1872
  conversationId: args.summary.conversationId,
1882
- destination: args.summary.destination,
1873
+ destination,
1883
1874
  nowMs: args.nowMs,
1884
- actor: sessionLogActor(args.summary.actor),
1885
- source,
1886
- ...args.summary.source ? { sessionSource: args.summary.source } : {},
1887
- visibility: args.destinationVisibility
1875
+ actor: sessionLogActor(args.actor),
1876
+ ...definedProps({ source: activitySource }),
1877
+ ...args.source ? { sessionSource: args.source } : {},
1878
+ visibility: isChild ? void 0 : args.destinationVisibility
1888
1879
  });
1889
1880
  await conversationStore.recordExecution({
1890
1881
  channelName: args.summary.channelName,
1891
1882
  conversationId: args.summary.conversationId,
1892
1883
  createdAtMs: args.summary.startedAtMs,
1893
- destination: args.summary.destination,
1884
+ destination,
1894
1885
  execution: conversationExecutionFromSummary(args.summary),
1895
1886
  lastActivityAtMs: args.summary.updatedAtMs,
1896
1887
  metrics: {
1897
1888
  durationMs: args.summary.cumulativeDurationMs,
1898
1889
  ...args.summary.cumulativeUsage ? { usage: args.summary.cumulativeUsage } : {}
1899
1890
  },
1900
- actor: sessionLogActor(args.summary.actor),
1901
- source,
1891
+ actor: sessionLogActor(args.actor),
1892
+ ...definedProps({ source: activitySource }),
1902
1893
  updatedAtMs: args.nowMs,
1903
- visibility: args.destinationVisibility
1894
+ visibility: isChild ? void 0 : args.destinationVisibility
1904
1895
  });
1905
1896
  }
1906
1897
  function materializeAgentTurnSessionRecord(stored, piProjection, turnStartMessageIndex, restoreVolatileContext = true) {
1907
1898
  const restoredProjection = restoreVolatileContext && (stored.state === "running" || stored.state === "awaiting_resume") ? restoreRuntimeContext(piProjection, stored.runtimeContext) : piProjection;
1908
1899
  return {
1909
1900
  version: stored.version,
1910
- ...stored.channelName ? { channelName: stored.channelName } : {},
1911
1901
  conversationId: stored.conversationId,
1912
1902
  sessionId: stored.sessionId,
1913
1903
  sliceId: stored.sliceId,
@@ -1919,22 +1909,22 @@ function materializeAgentTurnSessionRecord(stored, piProjection, turnStartMessag
1919
1909
  piMessageProvenance: restoredProjection.provenance,
1920
1910
  actors: stored.actors ?? instructionActors(piProjection.provenance),
1921
1911
  cumulativeDurationMs: stored.cumulativeDurationMs,
1922
- ...stored.destination ? { destination: stored.destination } : {},
1923
- ...stored.dispatchId ? { dispatchId: stored.dispatchId } : {},
1924
- ...stored.dispatchOutcome ? { dispatchOutcome: stored.dispatchOutcome } : {},
1925
- ...stored.resultMessageId ? { resultMessageId: stored.resultMessageId } : {},
1926
- ...stored.source ? { source: stored.source } : {},
1927
- ...stored.cumulativeUsage ? { cumulativeUsage: stored.cumulativeUsage } : {},
1928
- ...stored.resumeReason ? { resumeReason: stored.resumeReason } : {},
1929
- ...stored.errorMessage ? { errorMessage: stored.errorMessage } : {},
1930
- ...stored.loadedSkillNames ? { loadedSkillNames: stored.loadedSkillNames } : {},
1931
- ...stored.modelId ? { modelId: stored.modelId } : {},
1932
- ...stored.reasoningLevel ? { reasoningLevel: stored.reasoningLevel } : {},
1933
- ...stored.actor ? { actor: stored.actor } : {},
1934
- ...stored.resumedFromSliceId !== void 0 ? { resumedFromSliceId: stored.resumedFromSliceId } : {},
1935
- ...stored.surface ? { surface: stored.surface } : {},
1936
- ...stored.traceId ? { traceId: stored.traceId } : {},
1937
- ...turnStartMessageIndex !== void 0 ? { turnStartMessageIndex } : {}
1912
+ ...definedProps({
1913
+ channelName: stored.channelName,
1914
+ cumulativeUsage: stored.cumulativeUsage,
1915
+ dispatchId: stored.dispatchId,
1916
+ dispatchOutcome: stored.dispatchOutcome,
1917
+ errorMessage: stored.errorMessage,
1918
+ loadedSkillNames: stored.loadedSkillNames,
1919
+ modelId: stored.modelId,
1920
+ reasoningLevel: stored.reasoningLevel,
1921
+ resumeReason: stored.resumeReason,
1922
+ resultMessageId: stored.resultMessageId,
1923
+ resumedFromSliceId: stored.resumedFromSliceId,
1924
+ surface: stored.surface,
1925
+ traceId: stored.traceId,
1926
+ turnStartMessageIndex
1927
+ })
1938
1928
  };
1939
1929
  }
1940
1930
  function restoreRuntimeContext(projection, runtimeContext) {
@@ -2029,7 +2019,6 @@ function buildStoredRecord(args) {
2029
2019
  const nowMs = Date.now();
2030
2020
  return {
2031
2021
  version: (args.previousVersion ?? 0) + 1,
2032
- ...args.channelName ? { channelName: args.channelName } : {},
2033
2022
  conversationId: args.conversationId,
2034
2023
  sessionId: args.sessionId,
2035
2024
  sliceId: args.sliceId,
@@ -2038,40 +2027,44 @@ function buildStoredRecord(args) {
2038
2027
  lastProgressAtMs: args.lastProgressAtMs ?? nowMs,
2039
2028
  updatedAtMs: nowMs,
2040
2029
  committedSeq: args.committedSeq,
2041
- ...args.historyVersion !== void 0 ? { historyVersion: args.historyVersion } : {},
2042
- ...args.turnStartSeq !== void 0 ? { turnStartSeq: args.turnStartSeq } : {},
2043
- ...args.runtimeContext && args.runtimeContext.length > 0 ? { runtimeContext: args.runtimeContext } : {},
2044
2030
  cumulativeDurationMs: args.cumulativeDurationMs,
2045
- ...args.cumulativeUsage ? { cumulativeUsage: args.cumulativeUsage } : {},
2046
- ...args.destination ? { destination: args.destination } : {},
2047
- ...args.dispatchId ? { dispatchId: args.dispatchId } : {},
2048
- ...args.dispatchOutcome ? { dispatchOutcome: args.dispatchOutcome } : {},
2049
- ...args.resultMessageId ? { resultMessageId: args.resultMessageId } : {},
2050
- ...args.source ? { source: args.source } : {},
2051
- ...args.actor ? { actor: args.actor } : {},
2052
- ...args.actors ? { actors: args.actors } : {},
2053
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2054
- ...args.modelId ? { modelId: args.modelId } : {},
2055
- ...args.reasoningLevel ? { reasoningLevel: args.reasoningLevel } : {},
2056
- ...args.resumeReason ? { resumeReason: args.resumeReason } : {},
2057
- ...args.errorMessage ? { errorMessage: args.errorMessage } : {},
2058
- ...args.resumedFromSliceId !== void 0 ? { resumedFromSliceId: args.resumedFromSliceId } : {},
2059
- ...args.surface ? { surface: args.surface } : {},
2060
- ...args.traceId ? { traceId: args.traceId } : {}
2031
+ ...definedProps({
2032
+ actors: args.actors,
2033
+ channelName: args.channelName,
2034
+ cumulativeUsage: args.cumulativeUsage,
2035
+ dispatchId: args.dispatchId,
2036
+ dispatchOutcome: args.dispatchOutcome,
2037
+ errorMessage: args.errorMessage,
2038
+ historyVersion: args.historyVersion,
2039
+ loadedSkillNames: args.loadedSkillNames,
2040
+ modelId: args.modelId,
2041
+ reasoningLevel: args.reasoningLevel,
2042
+ resumeReason: args.resumeReason,
2043
+ resultMessageId: args.resultMessageId,
2044
+ resumedFromSliceId: args.resumedFromSliceId,
2045
+ runtimeContext: args.runtimeContext && args.runtimeContext.length > 0 ? args.runtimeContext : void 0,
2046
+ surface: args.surface,
2047
+ traceId: args.traceId,
2048
+ turnStartSeq: args.turnStartSeq
2049
+ })
2061
2050
  };
2062
2051
  }
2063
2052
  async function setStoredRecord(args) {
2064
2053
  const stateAdapter = getStateAdapter();
2065
2054
  await stateAdapter.connect();
2055
+ const storedRecord = storedAgentTurnSessionRecordSchema.parse(args.record);
2066
2056
  await recordConversationActivityMetadata({
2057
+ actor: args.actor,
2067
2058
  conversationStore: args.conversationStore,
2059
+ destination: args.destination,
2068
2060
  destinationVisibility: args.destinationVisibility,
2069
2061
  nowMs: Date.now(),
2070
- summary: args.record
2062
+ source: args.source,
2063
+ summary: storedRecord
2071
2064
  });
2072
2065
  await stateAdapter.set(
2073
- agentTurnSessionKey(args.record.conversationId, args.record.sessionId),
2074
- args.record,
2066
+ agentTurnSessionKey(storedRecord.conversationId, storedRecord.sessionId),
2067
+ storedRecord,
2075
2068
  args.ttlMs
2076
2069
  );
2077
2070
  const {
@@ -2082,10 +2075,10 @@ async function setStoredRecord(args) {
2082
2075
  turnStartSeq: _turnStartSeq,
2083
2076
  runtimeContext: _runtimeContext,
2084
2077
  ...summary
2085
- } = args.record;
2078
+ } = storedRecord;
2086
2079
  await appendAgentTurnSessionSummary(summary, args.ttlMs);
2087
2080
  return materializeAgentTurnSessionRecord(
2088
- args.record,
2081
+ storedRecord,
2089
2082
  {
2090
2083
  messages: [...args.piMessages],
2091
2084
  provenance: [...args.piMessageProvenance]
@@ -2105,37 +2098,38 @@ async function updateAgentTurnSessionState(args) {
2105
2098
  piMessages: args.existing.piMessages,
2106
2099
  piMessageProvenance: args.existing.piMessageProvenance,
2107
2100
  ttlMs: AGENT_TURN_SESSION_TTL_MS,
2108
- ...args.existing.turnStartMessageIndex !== void 0 ? { turnStartMessageIndex: args.existing.turnStartMessageIndex } : {},
2101
+ ...definedProps({
2102
+ turnStartMessageIndex: args.existing.turnStartMessageIndex
2103
+ }),
2109
2104
  record: buildStoredRecord({
2110
2105
  conversationId: args.existing.conversationId,
2111
2106
  sessionId: args.existing.sessionId,
2112
2107
  sliceId: args.existing.sliceId,
2113
2108
  state: args.state,
2114
2109
  committedSeq: parsed.committedSeq,
2115
- ...parsed.historyVersion !== void 0 ? { historyVersion: parsed.historyVersion } : {},
2116
- ...parsed.turnStartSeq !== void 0 ? { turnStartSeq: parsed.turnStartSeq } : {},
2117
- ...parsed.runtimeContext ? { runtimeContext: parsed.runtimeContext } : {},
2118
- ...parsed.channelName ? { channelName: parsed.channelName } : {},
2119
2110
  startedAtMs: parsed.startedAtMs,
2120
2111
  lastProgressAtMs: parsed.lastProgressAtMs,
2121
2112
  previousVersion: parsed.version,
2122
2113
  cumulativeDurationMs: args.existing.cumulativeDurationMs,
2123
- ...args.existing.cumulativeUsage ? { cumulativeUsage: args.existing.cumulativeUsage } : {},
2124
- ...args.existing.destination ? { destination: args.existing.destination } : {},
2125
- ...args.existing.dispatchId ? { dispatchId: args.existing.dispatchId } : {},
2126
- ...args.existing.dispatchOutcome ? { dispatchOutcome: args.existing.dispatchOutcome } : {},
2127
- ...args.existing.resultMessageId ? { resultMessageId: args.existing.resultMessageId } : {},
2128
- ...args.existing.source ? { source: args.existing.source } : {},
2129
- ...args.existing.loadedSkillNames ? { loadedSkillNames: args.existing.loadedSkillNames } : {},
2130
- ...args.existing.modelId ? { modelId: args.existing.modelId } : {},
2131
- ...args.existing.reasoningLevel ? { reasoningLevel: args.existing.reasoningLevel } : {},
2132
- ...args.existing.actor ? { actor: args.existing.actor } : {},
2133
2114
  actors: args.existing.actors,
2134
- ...args.existing.resumeReason ? { resumeReason: args.existing.resumeReason } : {},
2135
- ...args.existing.resumedFromSliceId !== void 0 ? { resumedFromSliceId: args.existing.resumedFromSliceId } : {},
2136
- ...args.existing.surface ? { surface: args.existing.surface } : {},
2137
- ...args.existing.traceId ? { traceId: args.existing.traceId } : {},
2138
- ...args.errorMessage ?? args.existing.errorMessage ? { errorMessage: args.errorMessage ?? args.existing.errorMessage } : {}
2115
+ ...definedProps({
2116
+ channelName: parsed.channelName,
2117
+ cumulativeUsage: args.existing.cumulativeUsage,
2118
+ dispatchId: args.existing.dispatchId,
2119
+ dispatchOutcome: args.existing.dispatchOutcome,
2120
+ errorMessage: args.errorMessage ?? args.existing.errorMessage,
2121
+ historyVersion: parsed.historyVersion,
2122
+ loadedSkillNames: args.existing.loadedSkillNames,
2123
+ modelId: args.existing.modelId,
2124
+ reasoningLevel: args.existing.reasoningLevel,
2125
+ resumeReason: args.existing.resumeReason,
2126
+ resultMessageId: args.existing.resultMessageId,
2127
+ resumedFromSliceId: args.existing.resumedFromSliceId,
2128
+ runtimeContext: parsed.runtimeContext,
2129
+ surface: args.existing.surface,
2130
+ traceId: args.existing.traceId,
2131
+ turnStartSeq: parsed.turnStartSeq
2132
+ })
2139
2133
  })
2140
2134
  });
2141
2135
  }
@@ -2151,7 +2145,7 @@ async function upsertAgentTurnSessionRecord(args) {
2151
2145
  );
2152
2146
  }
2153
2147
  const ttlMs = Math.max(1, args.ttlMs ?? AGENT_TURN_SESSION_TTL_MS);
2154
- const instructionActor = args.actor ?? existingRecord?.actor;
2148
+ const instructionActor = args.actor;
2155
2149
  const commit = await commitMessages({
2156
2150
  conversationId: args.conversationId,
2157
2151
  messages: args.piMessages,
@@ -2172,52 +2166,48 @@ async function upsertAgentTurnSessionRecord(args) {
2172
2166
  const turnStartSeq = durableTurnStartMessageIndex === void 0 ? existingRecord?.turnStartSeq : durableTurnStartMessageIndex <= 0 ? -1 : commit.messageSeqs[durableTurnStartMessageIndex - 1] ?? commit.committedSeq;
2173
2167
  const turnStartMessageIndex = durableTurnStartMessageIndex ?? (turnStartSeq === void 0 ? void 0 : commit.messageSeqs.filter((seq) => seq <= turnStartSeq).length);
2174
2168
  return await setStoredRecord({
2169
+ actor: args.actor,
2175
2170
  conversationStore: args.conversationStore,
2171
+ destination: args.destination,
2176
2172
  destinationVisibility: args.destinationVisibility,
2173
+ source: args.source,
2177
2174
  piMessages: commit.messages,
2178
2175
  piMessageProvenance: commit.provenance,
2179
2176
  ttlMs,
2180
- ...turnStartMessageIndex !== void 0 ? { turnStartMessageIndex } : {},
2177
+ turnStartMessageIndex,
2181
2178
  record: buildStoredRecord({
2182
- ...args.channelName ?? existingRecord?.channelName ? { channelName: args.channelName ?? existingRecord?.channelName } : {},
2183
2179
  conversationId: args.conversationId,
2184
2180
  sessionId: args.sessionId,
2185
2181
  sliceId: args.sliceId,
2186
2182
  state: args.state,
2187
- ...existingRecord?.startedAtMs !== void 0 ? { startedAtMs: existingRecord.startedAtMs } : {},
2188
- ...args.lastProgressAtMs !== void 0 ? { lastProgressAtMs: args.lastProgressAtMs } : {},
2189
2183
  committedSeq: commit.committedSeq,
2190
2184
  historyVersion: commit.historyVersion,
2191
- ...turnStartSeq !== void 0 ? { turnStartSeq } : {},
2192
- ...retainedRuntimeContext ? { runtimeContext: retainedRuntimeContext } : {},
2193
2185
  previousVersion: existingRecord?.version,
2194
2186
  cumulativeDurationMs: args.cumulativeDurationMs ?? existingRecord?.cumulativeDurationMs ?? 0,
2195
- ...args.cumulativeUsage ? { cumulativeUsage: args.cumulativeUsage } : {},
2196
- ...args.destination ?? existingRecord?.destination ? { destination: args.destination ?? existingRecord?.destination } : {},
2197
- ...args.dispatchId ?? existingRecord?.dispatchId ? { dispatchId: args.dispatchId ?? existingRecord?.dispatchId } : {},
2198
- ...args.dispatchOutcome ?? existingRecord?.dispatchOutcome ? {
2199
- dispatchOutcome: args.dispatchOutcome ?? existingRecord?.dispatchOutcome
2200
- } : {},
2201
- ...args.resultMessageId ?? existingRecord?.resultMessageId ? {
2202
- resultMessageId: args.resultMessageId ?? existingRecord?.resultMessageId
2203
- } : {},
2204
- ...args.source ?? existingRecord?.source ? { source: args.source ?? existingRecord?.source } : {},
2205
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2206
2187
  modelId: args.modelId,
2207
- ...args.reasoningLevel ?? existingRecord?.reasoningLevel ? {
2208
- reasoningLevel: args.reasoningLevel ?? existingRecord?.reasoningLevel
2209
- } : {},
2210
- ...args.actor ?? existingRecord?.actor ? { actor: args.actor ?? existingRecord?.actor } : {},
2211
2188
  actors: instructionActors([
2212
2189
  ...(existingRecord?.actors ?? []).map(instructionProvenanceFor),
2213
2190
  ...(args.actors ?? []).map(instructionProvenanceFor),
2214
2191
  ...commit.provenance
2215
2192
  ]),
2216
- ...args.resumeReason ? { resumeReason: args.resumeReason } : {},
2217
- ...args.errorMessage ? { errorMessage: args.errorMessage } : {},
2218
- ...args.resumedFromSliceId !== void 0 ? { resumedFromSliceId: args.resumedFromSliceId } : {},
2219
- ...args.surface ?? existingRecord?.surface ? { surface: args.surface ?? existingRecord?.surface } : {},
2220
- ...args.traceId ?? existingRecord?.traceId ? { traceId: args.traceId ?? existingRecord?.traceId } : {}
2193
+ ...definedProps({
2194
+ channelName: args.channelName ?? existingRecord?.channelName,
2195
+ cumulativeUsage: args.cumulativeUsage,
2196
+ dispatchId: args.dispatchId ?? existingRecord?.dispatchId,
2197
+ dispatchOutcome: args.dispatchOutcome ?? existingRecord?.dispatchOutcome,
2198
+ errorMessage: args.errorMessage,
2199
+ lastProgressAtMs: args.lastProgressAtMs,
2200
+ loadedSkillNames: args.loadedSkillNames,
2201
+ reasoningLevel: args.reasoningLevel ?? existingRecord?.reasoningLevel,
2202
+ resumeReason: args.resumeReason,
2203
+ resultMessageId: args.resultMessageId ?? existingRecord?.resultMessageId,
2204
+ resumedFromSliceId: args.resumedFromSliceId,
2205
+ runtimeContext: retainedRuntimeContext,
2206
+ startedAtMs: existingRecord?.startedAtMs,
2207
+ surface: args.surface ?? existingRecord?.surface,
2208
+ traceId: args.traceId ?? existingRecord?.traceId,
2209
+ turnStartSeq
2210
+ })
2221
2211
  })
2222
2212
  });
2223
2213
  }
@@ -2240,7 +2230,6 @@ async function recordAgentTurnSessionSummary(args) {
2240
2230
  const ttlMs = Math.max(1, args.ttlMs ?? AGENT_TURN_SESSION_TTL_MS);
2241
2231
  const summary = {
2242
2232
  version: existing?.version ?? 0,
2243
- ...args.channelName ?? existing?.channelName ? { channelName: args.channelName ?? existing?.channelName } : {},
2244
2233
  conversationId: args.conversationId,
2245
2234
  sessionId: args.sessionId,
2246
2235
  sliceId: args.sliceId,
@@ -2249,24 +2238,27 @@ async function recordAgentTurnSessionSummary(args) {
2249
2238
  state: args.state,
2250
2239
  updatedAtMs: nowMs,
2251
2240
  cumulativeDurationMs: args.cumulativeDurationMs ?? existing?.cumulativeDurationMs ?? 0,
2252
- ...args.cumulativeUsage ?? existing?.cumulativeUsage ? { cumulativeUsage: args.cumulativeUsage ?? existing?.cumulativeUsage } : {},
2253
- ...args.destination ?? existing?.destination ? { destination: args.destination ?? existing?.destination } : {},
2254
- ...args.dispatchId ?? existingDispatchId ? { dispatchId: args.dispatchId ?? existingDispatchId } : {},
2255
- ...args.dispatchOutcome ?? existingDispatchOutcome ? { dispatchOutcome: args.dispatchOutcome ?? existingDispatchOutcome } : {},
2256
- ...args.resultMessageId ?? existingResultMessageId ? { resultMessageId: args.resultMessageId ?? existingResultMessageId } : {},
2257
- ...args.source ?? existing?.source ? { source: args.source ?? existing?.source } : {},
2258
- ...args.actor ?? existing?.actor ? { actor: args.actor ?? existing?.actor } : {},
2259
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : existing?.loadedSkillNames ? { loadedSkillNames: existing.loadedSkillNames } : {},
2260
- ...args.modelId ?? existing?.modelId ? { modelId: args.modelId ?? existing?.modelId } : {},
2261
- ...args.reasoningLevel ?? existing?.reasoningLevel ? { reasoningLevel: args.reasoningLevel ?? existing?.reasoningLevel } : {},
2262
- ...args.resumeReason ? { resumeReason: args.resumeReason } : {},
2263
- ...args.surface ?? existing?.surface ? { surface: args.surface ?? existing?.surface } : {},
2264
- ...args.traceId ?? existing?.traceId ? { traceId: args.traceId ?? existing?.traceId } : {}
2241
+ ...definedProps({
2242
+ channelName: args.channelName ?? existing?.channelName,
2243
+ cumulativeUsage: args.cumulativeUsage ?? existing?.cumulativeUsage,
2244
+ dispatchId: args.dispatchId ?? existingDispatchId,
2245
+ dispatchOutcome: args.dispatchOutcome ?? existingDispatchOutcome,
2246
+ loadedSkillNames: args.loadedSkillNames ?? existing?.loadedSkillNames,
2247
+ modelId: args.modelId ?? existing?.modelId,
2248
+ reasoningLevel: args.reasoningLevel ?? existing?.reasoningLevel,
2249
+ resumeReason: args.resumeReason,
2250
+ resultMessageId: args.resultMessageId ?? existingResultMessageId,
2251
+ surface: args.surface ?? existing?.surface,
2252
+ traceId: args.traceId ?? existing?.traceId
2253
+ })
2265
2254
  };
2266
2255
  await recordConversationActivityMetadata({
2256
+ actor: args.actor,
2267
2257
  conversationStore: args.conversationStore,
2258
+ destination: args.destination,
2268
2259
  destinationVisibility: args.destinationVisibility,
2269
2260
  nowMs,
2261
+ source: args.source,
2270
2262
  summary
2271
2263
  });
2272
2264
  await appendAgentTurnSessionSummary(summary, ttlMs);
@@ -2341,6 +2333,24 @@ async function failAgentTurnSessionRecord(args) {
2341
2333
  });
2342
2334
  }
2343
2335
 
2336
+ // src/chat/services/persist-retry.ts
2337
+ var PERSIST_ATTEMPTS = 3;
2338
+ async function persistWithRetry(persist) {
2339
+ let lastError;
2340
+ for (let attempt = 1; attempt <= PERSIST_ATTEMPTS; attempt += 1) {
2341
+ try {
2342
+ await persist();
2343
+ return;
2344
+ } catch (error) {
2345
+ lastError = error;
2346
+ if (attempt < PERSIST_ATTEMPTS) {
2347
+ await sleep(attempt * 100);
2348
+ }
2349
+ }
2350
+ }
2351
+ throw lastError;
2352
+ }
2353
+
2344
2354
  // src/chat/services/turn-limit.ts
2345
2355
  var TurnSliceLimitExceededError = class extends Error {
2346
2356
  constructor(maxSlices) {
@@ -2353,6 +2363,33 @@ function buildTurnLimitResponse(eventId) {
2353
2363
  }
2354
2364
 
2355
2365
  // src/chat/services/turn-session-record.ts
2366
+ function definedProps2(values) {
2367
+ return Object.fromEntries(
2368
+ Object.entries(values).filter(([, value]) => value !== void 0)
2369
+ );
2370
+ }
2371
+ function sessionWriteContext(args, latest) {
2372
+ return {
2373
+ conversationId: args.conversationId,
2374
+ modelId: args.modelId,
2375
+ sessionId: args.sessionId,
2376
+ ...definedProps2({
2377
+ // Live only: Redis no longer stores execution actor across resumes.
2378
+ actor: args.actor,
2379
+ channelName: args.channelName ?? latest?.channelName,
2380
+ destination: args.destination,
2381
+ destinationVisibility: args.destinationVisibility,
2382
+ dispatchId: args.dispatchId ?? latest?.dispatchId,
2383
+ // Caller-resolved: some paths inherit these, others stay caller-only.
2384
+ loadedSkillNames: args.loadedSkillNames,
2385
+ reasoningLevel: args.reasoningLevel,
2386
+ source: args.source,
2387
+ surface: args.surface ?? latest?.surface,
2388
+ traceId: getActiveTraceId() ?? latest?.traceId,
2389
+ turnStartMessageIndex: args.turnStartMessageIndex ?? latest?.turnStartMessageIndex
2390
+ })
2391
+ };
2392
+ }
2356
2393
  function logSessionRecordError(error, eventName, args, attributes) {
2357
2394
  logException(error, eventName, {
2358
2395
  "app.ai.resume_conversation_id": args.conversationId,
@@ -2400,29 +2437,33 @@ async function persistRunningSessionRecord(args) {
2400
2437
  args.sessionId
2401
2438
  );
2402
2439
  await upsertAgentTurnSessionRecord({
2403
- ...args.channelName ?? latestSessionRecord?.channelName ? { channelName: args.channelName ?? latestSessionRecord?.channelName } : {},
2404
- conversationId: args.conversationId,
2440
+ ...sessionWriteContext(
2441
+ {
2442
+ actor: args.actor,
2443
+ channelName: args.channelName,
2444
+ conversationId: args.conversationId,
2445
+ destination: args.destination,
2446
+ destinationVisibility: args.destinationVisibility,
2447
+ dispatchId: args.dispatchId,
2448
+ loadedSkillNames: args.loadedSkillNames,
2449
+ modelId: args.modelId,
2450
+ reasoningLevel: args.reasoningLevel,
2451
+ sessionId: args.sessionId,
2452
+ source: args.source,
2453
+ surface: args.surface,
2454
+ turnStartMessageIndex: args.turnStartMessageIndex
2455
+ },
2456
+ latestSessionRecord
2457
+ ),
2458
+ ...definedProps2({
2459
+ trailingMessageProvenance: args.trailingMessageProvenance,
2460
+ turnContexts: args.turnContexts
2461
+ }),
2405
2462
  cumulativeDurationMs: latestSessionRecord?.cumulativeDurationMs,
2406
2463
  cumulativeUsage: latestSessionRecord?.cumulativeUsage,
2407
- ...args.destination ?? latestSessionRecord?.destination ? { destination: args.destination ?? latestSessionRecord?.destination } : {},
2408
- destinationVisibility: args.destinationVisibility,
2409
- ...args.dispatchId ?? latestSessionRecord?.dispatchId ? { dispatchId: args.dispatchId ?? latestSessionRecord?.dispatchId } : {},
2410
- ...args.source ?? latestSessionRecord?.source ? { source: args.source ?? latestSessionRecord?.source } : {},
2411
- sessionId: args.sessionId,
2412
- sliceId: args.sliceId,
2413
- state: "running",
2414
2464
  piMessages: args.messages,
2415
- ...args.trailingMessageProvenance ? { trailingMessageProvenance: args.trailingMessageProvenance } : {},
2416
- ...args.surface ?? latestSessionRecord?.surface ? { surface: args.surface ?? latestSessionRecord?.surface } : {},
2417
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2418
- modelId: args.modelId,
2419
- ...args.reasoningLevel ? { reasoningLevel: args.reasoningLevel } : {},
2420
- ...args.turnContexts ? { turnContexts: args.turnContexts } : {},
2421
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2422
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {},
2423
- ...(args.turnStartMessageIndex ?? latestSessionRecord?.turnStartMessageIndex) !== void 0 ? {
2424
- turnStartMessageIndex: args.turnStartMessageIndex ?? latestSessionRecord?.turnStartMessageIndex
2425
- } : {}
2465
+ sliceId: args.sliceId,
2466
+ state: "running"
2426
2467
  });
2427
2468
  return true;
2428
2469
  } catch (recordError) {
@@ -2451,45 +2492,41 @@ async function persistCompletedSessionRecord(args) {
2451
2492
  "Completed session record requires a slice id from the caller or the latest stored record"
2452
2493
  );
2453
2494
  }
2454
- const modelId = args.modelId;
2455
- const reasoningLevel = args.reasoningLevel ?? latestSessionRecord?.reasoningLevel;
2456
2495
  const target = {
2457
- ...args.channelName ?? latestSessionRecord?.channelName ? { channelName: args.channelName ?? latestSessionRecord?.channelName } : {},
2458
- conversationId: args.conversationId,
2459
- cumulativeDurationMs: addDurationMs(
2460
- latestSessionRecord?.cumulativeDurationMs,
2461
- args.currentDurationMs
2462
- ),
2463
- cumulativeUsage: addAgentTurnUsage(
2464
- latestSessionRecord?.cumulativeUsage,
2465
- args.currentUsage
2496
+ ...sessionWriteContext(
2497
+ {
2498
+ actor: args.actor,
2499
+ channelName: args.channelName,
2500
+ conversationId: args.conversationId,
2501
+ destination: args.destination,
2502
+ destinationVisibility: args.destinationVisibility,
2503
+ dispatchId: args.dispatchId,
2504
+ loadedSkillNames: args.loadedSkillNames ?? latestSessionRecord?.loadedSkillNames,
2505
+ modelId: args.modelId,
2506
+ reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel,
2507
+ sessionId: args.sessionId,
2508
+ source: args.source,
2509
+ surface: args.surface,
2510
+ turnStartMessageIndex: args.turnStartMessageIndex
2511
+ },
2512
+ latestSessionRecord
2466
2513
  ),
2467
- ...args.destination ?? latestSessionRecord?.destination ? { destination: args.destination ?? latestSessionRecord?.destination } : {},
2468
- ...args.dispatchId ?? latestSessionRecord?.dispatchId ? { dispatchId: args.dispatchId ?? latestSessionRecord?.dispatchId } : {},
2469
- ...args.dispatchOutcome ?? latestSessionRecord?.dispatchOutcome ? {
2470
- dispatchOutcome: args.dispatchOutcome ?? latestSessionRecord?.dispatchOutcome
2471
- } : {},
2472
- ...args.errorMessage ? { errorMessage: args.errorMessage } : {},
2473
- ...args.resultMessageId ?? latestSessionRecord?.resultMessageId ? {
2514
+ ...definedProps2({
2515
+ cumulativeDurationMs: addDurationMs(
2516
+ latestSessionRecord?.cumulativeDurationMs,
2517
+ args.currentDurationMs
2518
+ ),
2519
+ cumulativeUsage: addAgentTurnUsage(
2520
+ latestSessionRecord?.cumulativeUsage,
2521
+ args.currentUsage
2522
+ ),
2523
+ dispatchOutcome: args.dispatchOutcome ?? latestSessionRecord?.dispatchOutcome,
2524
+ errorMessage: args.errorMessage,
2474
2525
  resultMessageId: args.resultMessageId ?? latestSessionRecord?.resultMessageId
2475
- } : {},
2476
- ...args.source ?? latestSessionRecord?.source ? { source: args.source ?? latestSessionRecord?.source } : {},
2477
- ...args.destinationVisibility ? { destinationVisibility: args.destinationVisibility } : {},
2478
- sessionId: args.sessionId,
2479
- sliceId,
2480
- state: "completed",
2526
+ }),
2481
2527
  piMessages: args.allMessages,
2482
- ...args.surface ?? latestSessionRecord?.surface ? { surface: args.surface ?? latestSessionRecord?.surface } : {},
2483
- ...args.loadedSkillNames ?? latestSessionRecord?.loadedSkillNames ? {
2484
- loadedSkillNames: args.loadedSkillNames ?? latestSessionRecord?.loadedSkillNames
2485
- } : {},
2486
- modelId,
2487
- ...reasoningLevel ? { reasoningLevel } : {},
2488
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2489
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {},
2490
- ...(args.turnStartMessageIndex ?? latestSessionRecord?.turnStartMessageIndex) !== void 0 ? {
2491
- turnStartMessageIndex: args.turnStartMessageIndex ?? latestSessionRecord?.turnStartMessageIndex
2492
- } : {}
2528
+ sliceId,
2529
+ state: "completed"
2493
2530
  };
2494
2531
  await persistWithRetry(async () => {
2495
2532
  await upsertAgentTurnSessionRecord(target);
@@ -2534,35 +2571,40 @@ async function persistAuthPauseSessionRecord(args) {
2534
2571
  return void 0;
2535
2572
  }
2536
2573
  return await upsertAgentTurnSessionRecord({
2537
- ...args.channelName ?? latestSessionRecord?.channelName ? { channelName: args.channelName ?? latestSessionRecord?.channelName } : {},
2538
- conversationId: args.conversationId,
2539
- cumulativeDurationMs: addDurationMs(
2540
- latestSessionRecord?.cumulativeDurationMs,
2541
- args.currentDurationMs
2542
- ),
2543
- cumulativeUsage: addAgentTurnUsage(
2544
- latestSessionRecord?.cumulativeUsage,
2545
- args.currentUsage
2574
+ ...sessionWriteContext(
2575
+ {
2576
+ actor: args.actor,
2577
+ channelName: args.channelName,
2578
+ conversationId: args.conversationId,
2579
+ destination: args.destination,
2580
+ destinationVisibility: args.destinationVisibility,
2581
+ dispatchId: args.dispatchId,
2582
+ // Auth-pause keeps caller-owned skill names only.
2583
+ loadedSkillNames: args.loadedSkillNames,
2584
+ modelId: args.modelId,
2585
+ reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel,
2586
+ sessionId: args.sessionId,
2587
+ source: args.source,
2588
+ surface: args.surface
2589
+ },
2590
+ latestSessionRecord
2546
2591
  ),
2547
- ...args.destination ?? latestSessionRecord?.destination ? { destination: args.destination ?? latestSessionRecord?.destination } : {},
2548
- destinationVisibility: args.destinationVisibility,
2549
- ...args.dispatchId ?? latestSessionRecord?.dispatchId ? { dispatchId: args.dispatchId ?? latestSessionRecord?.dispatchId } : {},
2550
- ...args.source ?? latestSessionRecord?.source ? { source: args.source ?? latestSessionRecord?.source } : {},
2551
- sessionId: args.sessionId,
2552
- sliceId: nextSliceId,
2553
- state: "awaiting_resume",
2592
+ ...definedProps2({
2593
+ cumulativeDurationMs: addDurationMs(
2594
+ latestSessionRecord?.cumulativeDurationMs,
2595
+ args.currentDurationMs
2596
+ ),
2597
+ cumulativeUsage: addAgentTurnUsage(
2598
+ latestSessionRecord?.cumulativeUsage,
2599
+ args.currentUsage
2600
+ )
2601
+ }),
2602
+ errorMessage: args.errorMessage,
2554
2603
  piMessages,
2555
- ...args.surface ?? latestSessionRecord?.surface ? { surface: args.surface ?? latestSessionRecord?.surface } : {},
2556
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2557
- modelId: args.modelId,
2558
- ...args.reasoningLevel ?? latestSessionRecord?.reasoningLevel ? {
2559
- reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel
2560
- } : {},
2561
2604
  resumeReason: "auth",
2562
2605
  resumedFromSliceId: args.currentSliceId,
2563
- errorMessage: args.errorMessage,
2564
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2565
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {}
2606
+ sliceId: nextSliceId,
2607
+ state: "awaiting_resume"
2566
2608
  });
2567
2609
  } catch (recordError) {
2568
2610
  logSessionRecordError(
@@ -2599,63 +2641,51 @@ async function persistContinuationSessionRecord(args) {
2599
2641
  latestSessionRecord?.cumulativeUsage,
2600
2642
  args.currentUsage
2601
2643
  );
2644
+ const shared = {
2645
+ ...sessionWriteContext(
2646
+ {
2647
+ actor: args.actor,
2648
+ channelName: args.channelName,
2649
+ conversationId: args.conversationId,
2650
+ destination: args.destination,
2651
+ destinationVisibility: args.destinationVisibility,
2652
+ dispatchId: args.dispatchId,
2653
+ // Continuation keeps caller-owned skill names only.
2654
+ loadedSkillNames: args.loadedSkillNames,
2655
+ modelId: args.modelId,
2656
+ reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel,
2657
+ sessionId: args.sessionId,
2658
+ source: args.source,
2659
+ surface: args.surface
2660
+ },
2661
+ latestSessionRecord
2662
+ ),
2663
+ ...definedProps2({
2664
+ cumulativeDurationMs,
2665
+ cumulativeUsage
2666
+ }),
2667
+ piMessages,
2668
+ resumeReason: args.resumeReason
2669
+ };
2602
2670
  if (nextSliceId > botConfig.maxSlicesPerTurn) {
2603
2671
  return await upsertAgentTurnSessionRecord({
2604
- ...args.channelName ?? latestSessionRecord?.channelName ? {
2605
- channelName: args.channelName ?? latestSessionRecord?.channelName
2606
- } : {},
2607
- conversationId: args.conversationId,
2608
- cumulativeDurationMs,
2609
- cumulativeUsage,
2610
- ...args.destination ?? latestSessionRecord?.destination ? {
2611
- destination: args.destination ?? latestSessionRecord?.destination
2612
- } : {},
2613
- destinationVisibility: args.destinationVisibility,
2614
- ...args.dispatchId ?? latestSessionRecord?.dispatchId ? { dispatchId: args.dispatchId ?? latestSessionRecord?.dispatchId } : {},
2615
- ...args.source ?? latestSessionRecord?.source ? { source: args.source ?? latestSessionRecord?.source } : {},
2616
- sessionId: args.sessionId,
2617
- sliceId: args.currentSliceId,
2618
- state: "failed",
2619
- piMessages,
2620
- ...args.surface ?? latestSessionRecord?.surface ? { surface: args.surface ?? latestSessionRecord?.surface } : {},
2621
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2622
- modelId: args.modelId,
2623
- ...args.reasoningLevel ?? latestSessionRecord?.reasoningLevel ? {
2624
- reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel
2625
- } : {},
2626
- resumeReason: args.resumeReason,
2627
- resumedFromSliceId: latestSessionRecord?.resumedFromSliceId,
2672
+ ...shared,
2673
+ ...definedProps2({
2674
+ resumedFromSliceId: latestSessionRecord?.resumedFromSliceId
2675
+ }),
2628
2676
  errorMessage: new TurnSliceLimitExceededError(
2629
2677
  botConfig.maxSlicesPerTurn
2630
2678
  ).message,
2631
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2632
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {}
2679
+ sliceId: args.currentSliceId,
2680
+ state: "failed"
2633
2681
  });
2634
2682
  }
2635
2683
  return await upsertAgentTurnSessionRecord({
2636
- ...args.channelName ?? latestSessionRecord?.channelName ? { channelName: args.channelName ?? latestSessionRecord?.channelName } : {},
2637
- conversationId: args.conversationId,
2638
- cumulativeDurationMs,
2639
- cumulativeUsage,
2640
- ...args.destination ?? latestSessionRecord?.destination ? { destination: args.destination ?? latestSessionRecord?.destination } : {},
2641
- destinationVisibility: args.destinationVisibility,
2642
- ...args.dispatchId ?? latestSessionRecord?.dispatchId ? { dispatchId: args.dispatchId ?? latestSessionRecord?.dispatchId } : {},
2643
- ...args.source ?? latestSessionRecord?.source ? { source: args.source ?? latestSessionRecord?.source } : {},
2644
- sessionId: args.sessionId,
2645
- sliceId: nextSliceId,
2646
- state: "awaiting_resume",
2647
- piMessages,
2648
- ...args.surface ?? latestSessionRecord?.surface ? { surface: args.surface ?? latestSessionRecord?.surface } : {},
2649
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2650
- modelId: args.modelId,
2651
- ...args.reasoningLevel ?? latestSessionRecord?.reasoningLevel ? {
2652
- reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel
2653
- } : {},
2654
- resumeReason: args.resumeReason,
2655
- resumedFromSliceId: args.currentSliceId,
2684
+ ...shared,
2656
2685
  errorMessage: args.errorMessage,
2657
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2658
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {}
2686
+ resumedFromSliceId: args.currentSliceId,
2687
+ sliceId: nextSliceId,
2688
+ state: "awaiting_resume"
2659
2689
  });
2660
2690
  } catch (recordError) {
2661
2691
  logSessionRecordError(
@@ -2681,34 +2711,39 @@ async function persistYieldSessionRecord(args) {
2681
2711
  args.sessionId
2682
2712
  );
2683
2713
  return await upsertAgentTurnSessionRecord({
2684
- ...args.channelName ?? latestSessionRecord?.channelName ? { channelName: args.channelName ?? latestSessionRecord?.channelName } : {},
2685
- conversationId: args.conversationId,
2686
- cumulativeDurationMs: addDurationMs(
2687
- latestSessionRecord?.cumulativeDurationMs,
2688
- args.currentDurationMs
2689
- ),
2690
- cumulativeUsage: addAgentTurnUsage(
2691
- latestSessionRecord?.cumulativeUsage,
2692
- args.currentUsage
2714
+ ...sessionWriteContext(
2715
+ {
2716
+ actor: args.actor,
2717
+ channelName: args.channelName,
2718
+ conversationId: args.conversationId,
2719
+ destination: args.destination,
2720
+ dispatchId: args.dispatchId,
2721
+ // Yield keeps caller-owned skill names only.
2722
+ loadedSkillNames: args.loadedSkillNames,
2723
+ modelId: args.modelId,
2724
+ reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel,
2725
+ sessionId: args.sessionId,
2726
+ source: args.source,
2727
+ surface: args.surface
2728
+ },
2729
+ latestSessionRecord
2693
2730
  ),
2694
- ...args.destination ?? latestSessionRecord?.destination ? { destination: args.destination ?? latestSessionRecord?.destination } : {},
2695
- ...args.dispatchId ?? latestSessionRecord?.dispatchId ? { dispatchId: args.dispatchId ?? latestSessionRecord?.dispatchId } : {},
2696
- ...args.source ?? latestSessionRecord?.source ? { source: args.source ?? latestSessionRecord?.source } : {},
2697
- sessionId: args.sessionId,
2698
- sliceId: args.currentSliceId,
2699
- state: "awaiting_resume",
2731
+ ...definedProps2({
2732
+ cumulativeDurationMs: addDurationMs(
2733
+ latestSessionRecord?.cumulativeDurationMs,
2734
+ args.currentDurationMs
2735
+ ),
2736
+ cumulativeUsage: addAgentTurnUsage(
2737
+ latestSessionRecord?.cumulativeUsage,
2738
+ args.currentUsage
2739
+ ),
2740
+ resumedFromSliceId: latestSessionRecord?.resumedFromSliceId
2741
+ }),
2742
+ errorMessage: args.errorMessage,
2700
2743
  piMessages,
2701
- ...args.surface ?? latestSessionRecord?.surface ? { surface: args.surface ?? latestSessionRecord?.surface } : {},
2702
- ...args.loadedSkillNames ? { loadedSkillNames: args.loadedSkillNames } : {},
2703
- modelId: args.modelId,
2704
- ...args.reasoningLevel ?? latestSessionRecord?.reasoningLevel ? {
2705
- reasoningLevel: args.reasoningLevel ?? latestSessionRecord?.reasoningLevel
2706
- } : {},
2707
2744
  resumeReason: "yield",
2708
- resumedFromSliceId: latestSessionRecord?.resumedFromSliceId,
2709
- errorMessage: args.errorMessage,
2710
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2711
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {}
2745
+ sliceId: args.currentSliceId,
2746
+ state: "awaiting_resume"
2712
2747
  });
2713
2748
  } catch (recordError) {
2714
2749
  logSessionRecordError(
@@ -3088,9 +3123,6 @@ function createChannelConfigurationService(storage) {
3088
3123
  };
3089
3124
  }
3090
3125
 
3091
- // src/chat/state/ttl.ts
3092
- var JUNIOR_THREAD_STATE_TTL_MS = 7 * 24 * 60 * 60 * 1e3;
3093
-
3094
3126
  // src/chat/runtime/thread-state.ts
3095
3127
  function threadStateKey(threadId) {
3096
3128
  return `thread-state:${threadId}`;
@@ -3383,7 +3415,6 @@ export {
3383
3415
  persistAuthPauseSessionRecord,
3384
3416
  persistContinuationSessionRecord,
3385
3417
  persistYieldSessionRecord,
3386
- JUNIOR_THREAD_STATE_TTL_MS,
3387
3418
  canReusePendingAuthLink,
3388
3419
  getConversationPendingAuth,
3389
3420
  clearPendingAuth,