@sentry/junior 0.137.0 → 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 (87) hide show
  1. package/dist/{agent-hooks-MZOKEP3J.js → agent-hooks-SCR74Q3E.js} +11 -9
  2. package/dist/api.js +21 -18
  3. package/dist/app.d.ts +7 -0
  4. package/dist/app.js +248 -689
  5. package/dist/{catalog-runtime-MD66YLFV.js → catalog-runtime-IU3QF4GG.js} +5 -5
  6. package/dist/chat/agent/request.d.ts +32 -2
  7. package/dist/chat/agent-dispatch/heartbeat.d.ts +5 -0
  8. package/dist/chat/agent-dispatch/types.d.ts +1 -1
  9. package/dist/chat/agent-invocations/errors.d.ts +8 -0
  10. package/dist/chat/agent-invocations/spawn.d.ts +12 -0
  11. package/dist/chat/agent-invocations/store.d.ts +44 -0
  12. package/dist/chat/agent-invocations/types.d.ts +449 -0
  13. package/dist/chat/agent-invocations/work.d.ts +31 -0
  14. package/dist/chat/config.d.ts +1 -0
  15. package/dist/chat/conversations/sql/store.d.ts +8 -0
  16. package/dist/chat/conversations/store.d.ts +7 -0
  17. package/dist/chat/experimental.d.ts +16 -0
  18. package/dist/chat/runtime/agent-runner.d.ts +2 -1
  19. package/dist/chat/services/auth-pause.d.ts +0 -1
  20. package/dist/chat/services/mcp-auth-orchestration.d.ts +3 -2
  21. package/dist/chat/services/plugin-auth-orchestration.d.ts +3 -2
  22. package/dist/chat/state/turn-session.d.ts +0 -2
  23. package/dist/chat/task-execution/state.d.ts +23 -11
  24. package/dist/chat/task-execution/store.d.ts +20 -11
  25. package/dist/chat/task-execution/worker.d.ts +2 -2
  26. package/dist/chat/tools/runtime/spawn-agent.d.ts +31 -0
  27. package/dist/chat/tools/types.d.ts +2 -0
  28. package/dist/chunk-3Y47VMHV.js +6 -0
  29. package/dist/chunk-5IDFPCON.js +46 -0
  30. package/dist/chunk-5LTJ4ITU.js +1523 -0
  31. package/dist/{chunk-WPPXIAFB.js → chunk-5MXWHTCK.js} +17 -434
  32. package/dist/{chunk-UOXATPSI.js → chunk-A7WCHXZV.js} +7 -5
  33. package/dist/{chunk-5G34IEK2.js → chunk-BOLJOV5T.js} +2 -56
  34. package/dist/{chunk-UG6ZYPBI.js → chunk-BYW4B4PR.js} +73 -60
  35. package/dist/{chunk-XZ7NPXAB.js → chunk-C7ZVPO4Z.js} +1 -1
  36. package/dist/{chunk-RB4NJKEX.js → chunk-DCLLQGS6.js} +119 -50
  37. package/dist/{chunk-DQXAAVTR.js → chunk-E5UYAIKJ.js} +44 -1467
  38. package/dist/{chunk-6QW2RRQT.js → chunk-ELKW5FIB.js} +5 -5
  39. package/dist/chunk-EM225ITJ.js +56 -0
  40. package/dist/{chunk-SYCX4DOK.js → chunk-FEUNPTGV.js} +7 -60
  41. package/dist/{chunk-YRPC6MJY.js → chunk-FNLBTB4K.js} +1 -1
  42. package/dist/chunk-HIWJWWMT.js +668 -0
  43. package/dist/chunk-IUFM5JWV.js +24 -0
  44. package/dist/{chunk-T6Z5S6PQ.js → chunk-JNNJZURV.js} +1 -1
  45. package/dist/{chunk-6ZK6FZIV.js → chunk-KCBZSJRU.js} +6 -27
  46. package/dist/{chunk-5PNJMOY5.js → chunk-KDCHVT25.js} +1 -1
  47. package/dist/{chunk-BZAMASJP.js → chunk-KFRHRSCJ.js} +1 -1
  48. package/dist/chunk-LPVUNO2W.js +320 -0
  49. package/dist/{chunk-EWDSB24B.js → chunk-N7WARMC6.js} +16 -776
  50. package/dist/chunk-PB3CXNVL.js +41 -0
  51. package/dist/chunk-Q6QZTTLS.js +55 -0
  52. package/dist/{chunk-36EG34YI.js → chunk-QS527AV5.js} +1 -1
  53. package/dist/chunk-R776HKPK.js +81 -0
  54. package/dist/{chunk-KJ4HGRHN.js → chunk-RZIBSCYC.js} +507 -596
  55. package/dist/chunk-SD3OFRWY.js +714 -0
  56. package/dist/{chunk-ZFS67WIR.js → chunk-SIPKWSXK.js} +57 -6
  57. package/dist/chunk-TCS76X5D.js +441 -0
  58. package/dist/{chunk-K2NCQJKH.js → chunk-TDJCLNDH.js} +3 -3
  59. package/dist/{chunk-POBSY47Q.js → chunk-TUUDXEXU.js} +336 -302
  60. package/dist/{chunk-YNFERGKP.js → chunk-VPZFVJND.js} +1 -1
  61. package/dist/{chunk-UZQ4VY3M.js → chunk-YU5QUF67.js} +1 -1
  62. package/dist/chunk-ZBSMCXKV.js +710 -0
  63. package/dist/{chunk-3Q342EJJ.js → chunk-ZXNTJJIE.js} +2 -2
  64. package/dist/cli/chat.js +65 -26
  65. package/dist/cli/check.js +6 -6
  66. package/dist/cli/plugins.js +14 -12
  67. package/dist/cli/snapshot-create.js +10 -9
  68. package/dist/cli/upgrade.js +7 -7
  69. package/dist/conversation-work-KATFHLG3.js +56 -0
  70. package/dist/db/schema/agent-invocations.d.ts +544 -0
  71. package/dist/db/schema.d.ts +543 -1
  72. package/dist/{db-66E7VDNC.js → db-UO2IWXGA.js} +7 -7
  73. package/dist/experimental-HH6OM6UH.js +13 -0
  74. package/dist/instrumentation.js +4 -4
  75. package/dist/nitro.js +4 -3
  76. package/dist/{oauth-callback-server-7QPD4Q7P.js → oauth-callback-server-MAQI43LX.js} +26 -18
  77. package/dist/{runner-L2BKJKOD.js → runner-PIUQEXKE.js} +26 -14
  78. package/dist/{skills-HOQ2NSPW.js → skills-4ARJADHQ.js} +6 -6
  79. package/dist/spawn-G2AWPMCT.js +38 -0
  80. package/dist/{validation-ENHI7S2C.js → validation-S3EE3HWL.js} +6 -6
  81. package/dist/version.js +1 -1
  82. package/dist/work-UARRUAW3.js +46 -0
  83. package/dist/worker-DOCIRVD3.js +24 -0
  84. package/migrations/0021_first_warhawk.sql +35 -0
  85. package/migrations/meta/0021_snapshot.json +2176 -0
  86. package/migrations/meta/_journal.json +8 -1
  87. package/package.json +5 -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-ZFS67WIR.js";
24
+ } from "./chunk-SIPKWSXK.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-BOLJOV5T.js";
27
32
  import {
28
33
  botConfig,
29
34
  escapeXml,
30
35
  resolveGatewayModel,
31
36
  unwrapCurrentInstruction
32
- } from "./chunk-UG6ZYPBI.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-T6Z5S6PQ.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,17 +1801,14 @@ 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(),
1811
+ // TODO(#1267): Remove actor from this projection once resume rebuilds it.
1825
1812
  actor: actorSchema.optional(),
1826
1813
  resumeReason: agentTurnResumeReasonSchema.optional(),
1827
1814
  resumedFromSliceId: z2.number().int().nonnegative().optional(),
@@ -1832,7 +1819,7 @@ var agentTurnSessionSummarySchema = z2.object({
1832
1819
  surface: agentTurnSurfaceSchema.optional(),
1833
1820
  traceId: z2.string().optional(),
1834
1821
  updatedAtMs: nonNegativeNumberSchema
1835
- }).strict();
1822
+ }).strip();
1836
1823
  var storedAgentTurnSessionRecordSchema = agentTurnSessionSummarySchema.extend({
1837
1824
  actors: z2.array(actorSchema).optional(),
1838
1825
  committedSeq: seqCursorSchema,
@@ -1840,7 +1827,7 @@ var storedAgentTurnSessionRecordSchema = agentTurnSessionSummarySchema.extend({
1840
1827
  errorMessage: z2.string().optional(),
1841
1828
  turnStartSeq: seqCursorSchema.optional(),
1842
1829
  runtimeContext: z2.array(piMessageSchema).optional()
1843
- }).strict();
1830
+ }).strip();
1844
1831
  function conversationExecutionFromSummary(summary) {
1845
1832
  const status = summary.state === "completed" || summary.state === "abandoned" ? "idle" : summary.state;
1846
1833
  return {
@@ -1859,38 +1846,44 @@ function parseAgentTurnSessionSummary(value) {
1859
1846
  return agentTurnSessionSummarySchema.parse(value);
1860
1847
  }
1861
1848
  async function appendAgentTurnSessionSummary(summary, ttlMs) {
1849
+ const projectedSummary = agentTurnSessionSummarySchema.parse(summary);
1862
1850
  const stateAdapter = getStateAdapter();
1863
1851
  await Promise.all([
1864
- stateAdapter.appendToList(AGENT_TURN_SESSION_INDEX_KEY, summary, {
1852
+ stateAdapter.appendToList(AGENT_TURN_SESSION_INDEX_KEY, projectedSummary, {
1865
1853
  maxLength: AGENT_TURN_SESSION_INDEX_MAX_LENGTH,
1866
1854
  ttlMs
1867
1855
  }),
1868
1856
  stateAdapter.appendToList(
1869
- agentTurnSessionConversationIndexKey(summary.conversationId),
1870
- summary,
1857
+ agentTurnSessionConversationIndexKey(projectedSummary.conversationId),
1858
+ projectedSummary,
1871
1859
  { ttlMs }
1872
1860
  )
1873
1861
  ]);
1874
1862
  }
1875
1863
  async function recordConversationActivityMetadata(args) {
1876
1864
  const conversationStore = args.conversationStore ?? getConversationStore();
1877
- 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;
1878
1871
  await conversationStore.recordActivity({
1879
1872
  activityAtMs: args.summary.updatedAtMs,
1880
1873
  channelName: args.summary.channelName,
1881
1874
  conversationId: args.summary.conversationId,
1882
- destination: args.summary.destination,
1875
+ destination,
1883
1876
  nowMs: args.nowMs,
1884
1877
  actor: sessionLogActor(args.summary.actor),
1885
- source,
1886
- ...args.summary.source ? { sessionSource: args.summary.source } : {},
1887
- visibility: args.destinationVisibility
1878
+ ...definedProps({ source: activitySource }),
1879
+ ...args.source ? { sessionSource: args.source } : {},
1880
+ visibility: isChild ? void 0 : args.destinationVisibility
1888
1881
  });
1889
1882
  await conversationStore.recordExecution({
1890
1883
  channelName: args.summary.channelName,
1891
1884
  conversationId: args.summary.conversationId,
1892
1885
  createdAtMs: args.summary.startedAtMs,
1893
- destination: args.summary.destination,
1886
+ destination,
1894
1887
  execution: conversationExecutionFromSummary(args.summary),
1895
1888
  lastActivityAtMs: args.summary.updatedAtMs,
1896
1889
  metrics: {
@@ -1898,16 +1891,15 @@ async function recordConversationActivityMetadata(args) {
1898
1891
  ...args.summary.cumulativeUsage ? { usage: args.summary.cumulativeUsage } : {}
1899
1892
  },
1900
1893
  actor: sessionLogActor(args.summary.actor),
1901
- source,
1894
+ ...definedProps({ source: activitySource }),
1902
1895
  updatedAtMs: args.nowMs,
1903
- visibility: args.destinationVisibility
1896
+ visibility: isChild ? void 0 : args.destinationVisibility
1904
1897
  });
1905
1898
  }
1906
1899
  function materializeAgentTurnSessionRecord(stored, piProjection, turnStartMessageIndex, restoreVolatileContext = true) {
1907
1900
  const restoredProjection = restoreVolatileContext && (stored.state === "running" || stored.state === "awaiting_resume") ? restoreRuntimeContext(piProjection, stored.runtimeContext) : piProjection;
1908
1901
  return {
1909
1902
  version: stored.version,
1910
- ...stored.channelName ? { channelName: stored.channelName } : {},
1911
1903
  conversationId: stored.conversationId,
1912
1904
  sessionId: stored.sessionId,
1913
1905
  sliceId: stored.sliceId,
@@ -1919,22 +1911,23 @@ function materializeAgentTurnSessionRecord(stored, piProjection, turnStartMessag
1919
1911
  piMessageProvenance: restoredProjection.provenance,
1920
1912
  actors: stored.actors ?? instructionActors(piProjection.provenance),
1921
1913
  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 } : {}
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
+ })
1938
1931
  };
1939
1932
  }
1940
1933
  function restoreRuntimeContext(projection, runtimeContext) {
@@ -2029,7 +2022,6 @@ function buildStoredRecord(args) {
2029
2022
  const nowMs = Date.now();
2030
2023
  return {
2031
2024
  version: (args.previousVersion ?? 0) + 1,
2032
- ...args.channelName ? { channelName: args.channelName } : {},
2033
2025
  conversationId: args.conversationId,
2034
2026
  sessionId: args.sessionId,
2035
2027
  sliceId: args.sliceId,
@@ -2038,40 +2030,44 @@ function buildStoredRecord(args) {
2038
2030
  lastProgressAtMs: args.lastProgressAtMs ?? nowMs,
2039
2031
  updatedAtMs: nowMs,
2040
2032
  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
2033
  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 } : {}
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
+ })
2061
2054
  };
2062
2055
  }
2063
2056
  async function setStoredRecord(args) {
2064
2057
  const stateAdapter = getStateAdapter();
2065
2058
  await stateAdapter.connect();
2059
+ const storedRecord = storedAgentTurnSessionRecordSchema.parse(args.record);
2066
2060
  await recordConversationActivityMetadata({
2067
2061
  conversationStore: args.conversationStore,
2062
+ destination: args.destination,
2068
2063
  destinationVisibility: args.destinationVisibility,
2069
2064
  nowMs: Date.now(),
2070
- summary: args.record
2065
+ source: args.source,
2066
+ summary: storedRecord
2071
2067
  });
2072
2068
  await stateAdapter.set(
2073
- agentTurnSessionKey(args.record.conversationId, args.record.sessionId),
2074
- args.record,
2069
+ agentTurnSessionKey(storedRecord.conversationId, storedRecord.sessionId),
2070
+ storedRecord,
2075
2071
  args.ttlMs
2076
2072
  );
2077
2073
  const {
@@ -2082,10 +2078,10 @@ async function setStoredRecord(args) {
2082
2078
  turnStartSeq: _turnStartSeq,
2083
2079
  runtimeContext: _runtimeContext,
2084
2080
  ...summary
2085
- } = args.record;
2081
+ } = storedRecord;
2086
2082
  await appendAgentTurnSessionSummary(summary, args.ttlMs);
2087
2083
  return materializeAgentTurnSessionRecord(
2088
- args.record,
2084
+ storedRecord,
2089
2085
  {
2090
2086
  messages: [...args.piMessages],
2091
2087
  provenance: [...args.piMessageProvenance]
@@ -2105,37 +2101,39 @@ async function updateAgentTurnSessionState(args) {
2105
2101
  piMessages: args.existing.piMessages,
2106
2102
  piMessageProvenance: args.existing.piMessageProvenance,
2107
2103
  ttlMs: AGENT_TURN_SESSION_TTL_MS,
2108
- ...args.existing.turnStartMessageIndex !== void 0 ? { turnStartMessageIndex: args.existing.turnStartMessageIndex } : {},
2104
+ ...definedProps({
2105
+ turnStartMessageIndex: args.existing.turnStartMessageIndex
2106
+ }),
2109
2107
  record: buildStoredRecord({
2110
2108
  conversationId: args.existing.conversationId,
2111
2109
  sessionId: args.existing.sessionId,
2112
2110
  sliceId: args.existing.sliceId,
2113
2111
  state: args.state,
2114
2112
  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
2113
  startedAtMs: parsed.startedAtMs,
2120
2114
  lastProgressAtMs: parsed.lastProgressAtMs,
2121
2115
  previousVersion: parsed.version,
2122
2116
  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
2117
  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 } : {}
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
+ })
2139
2137
  })
2140
2138
  });
2141
2139
  }
@@ -2173,51 +2171,47 @@ async function upsertAgentTurnSessionRecord(args) {
2173
2171
  const turnStartMessageIndex = durableTurnStartMessageIndex ?? (turnStartSeq === void 0 ? void 0 : commit.messageSeqs.filter((seq) => seq <= turnStartSeq).length);
2174
2172
  return await setStoredRecord({
2175
2173
  conversationStore: args.conversationStore,
2174
+ destination: args.destination,
2176
2175
  destinationVisibility: args.destinationVisibility,
2176
+ source: args.source,
2177
2177
  piMessages: commit.messages,
2178
2178
  piMessageProvenance: commit.provenance,
2179
2179
  ttlMs,
2180
- ...turnStartMessageIndex !== void 0 ? { turnStartMessageIndex } : {},
2180
+ turnStartMessageIndex,
2181
2181
  record: buildStoredRecord({
2182
- ...args.channelName ?? existingRecord?.channelName ? { channelName: args.channelName ?? existingRecord?.channelName } : {},
2183
2182
  conversationId: args.conversationId,
2184
2183
  sessionId: args.sessionId,
2185
2184
  sliceId: args.sliceId,
2186
2185
  state: args.state,
2187
- ...existingRecord?.startedAtMs !== void 0 ? { startedAtMs: existingRecord.startedAtMs } : {},
2188
- ...args.lastProgressAtMs !== void 0 ? { lastProgressAtMs: args.lastProgressAtMs } : {},
2189
2186
  committedSeq: commit.committedSeq,
2190
2187
  historyVersion: commit.historyVersion,
2191
- ...turnStartSeq !== void 0 ? { turnStartSeq } : {},
2192
- ...retainedRuntimeContext ? { runtimeContext: retainedRuntimeContext } : {},
2193
2188
  previousVersion: existingRecord?.version,
2194
2189
  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
2190
  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
2191
  actors: instructionActors([
2212
2192
  ...(existingRecord?.actors ?? []).map(instructionProvenanceFor),
2213
2193
  ...(args.actors ?? []).map(instructionProvenanceFor),
2214
2194
  ...commit.provenance
2215
2195
  ]),
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 } : {}
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
+ })
2221
2215
  })
2222
2216
  });
2223
2217
  }
@@ -2240,7 +2234,6 @@ async function recordAgentTurnSessionSummary(args) {
2240
2234
  const ttlMs = Math.max(1, args.ttlMs ?? AGENT_TURN_SESSION_TTL_MS);
2241
2235
  const summary = {
2242
2236
  version: existing?.version ?? 0,
2243
- ...args.channelName ?? existing?.channelName ? { channelName: args.channelName ?? existing?.channelName } : {},
2244
2237
  conversationId: args.conversationId,
2245
2238
  sessionId: args.sessionId,
2246
2239
  sliceId: args.sliceId,
@@ -2249,24 +2242,27 @@ async function recordAgentTurnSessionSummary(args) {
2249
2242
  state: args.state,
2250
2243
  updatedAtMs: nowMs,
2251
2244
  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 } : {}
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
+ })
2265
2259
  };
2266
2260
  await recordConversationActivityMetadata({
2267
2261
  conversationStore: args.conversationStore,
2262
+ destination: args.destination,
2268
2263
  destinationVisibility: args.destinationVisibility,
2269
2264
  nowMs,
2265
+ source: args.source,
2270
2266
  summary
2271
2267
  });
2272
2268
  await appendAgentTurnSessionSummary(summary, ttlMs);
@@ -2341,6 +2337,24 @@ async function failAgentTurnSessionRecord(args) {
2341
2337
  });
2342
2338
  }
2343
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
+
2344
2358
  // src/chat/services/turn-limit.ts
2345
2359
  var TurnSliceLimitExceededError = class extends Error {
2346
2360
  constructor(maxSlices) {
@@ -2353,6 +2367,32 @@ function buildTurnLimitResponse(eventId) {
2353
2367
  }
2354
2368
 
2355
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
+ }
2356
2396
  function logSessionRecordError(error, eventName, args, attributes) {
2357
2397
  logException(error, eventName, {
2358
2398
  "app.ai.resume_conversation_id": args.conversationId,
@@ -2400,29 +2440,33 @@ async function persistRunningSessionRecord(args) {
2400
2440
  args.sessionId
2401
2441
  );
2402
2442
  await upsertAgentTurnSessionRecord({
2403
- ...args.channelName ?? latestSessionRecord?.channelName ? { channelName: args.channelName ?? latestSessionRecord?.channelName } : {},
2404
- 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
+ }),
2405
2465
  cumulativeDurationMs: latestSessionRecord?.cumulativeDurationMs,
2406
2466
  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
2467
  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
- } : {}
2468
+ sliceId: args.sliceId,
2469
+ state: "running"
2426
2470
  });
2427
2471
  return true;
2428
2472
  } catch (recordError) {
@@ -2451,45 +2495,41 @@ async function persistCompletedSessionRecord(args) {
2451
2495
  "Completed session record requires a slice id from the caller or the latest stored record"
2452
2496
  );
2453
2497
  }
2454
- const modelId = args.modelId;
2455
- const reasoningLevel = args.reasoningLevel ?? latestSessionRecord?.reasoningLevel;
2456
2498
  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
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
2466
2516
  ),
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 ? {
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,
2474
2528
  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",
2529
+ }),
2481
2530
  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
- } : {}
2531
+ sliceId,
2532
+ state: "completed"
2493
2533
  };
2494
2534
  await persistWithRetry(async () => {
2495
2535
  await upsertAgentTurnSessionRecord(target);
@@ -2534,35 +2574,40 @@ async function persistAuthPauseSessionRecord(args) {
2534
2574
  return void 0;
2535
2575
  }
2536
2576
  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
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
2546
2594
  ),
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",
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,
2554
2606
  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
2607
  resumeReason: "auth",
2562
2608
  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 } : {}
2609
+ sliceId: nextSliceId,
2610
+ state: "awaiting_resume"
2566
2611
  });
2567
2612
  } catch (recordError) {
2568
2613
  logSessionRecordError(
@@ -2599,63 +2644,51 @@ async function persistContinuationSessionRecord(args) {
2599
2644
  latestSessionRecord?.cumulativeUsage,
2600
2645
  args.currentUsage
2601
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
+ };
2602
2673
  if (nextSliceId > botConfig.maxSlicesPerTurn) {
2603
2674
  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,
2675
+ ...shared,
2676
+ ...definedProps2({
2677
+ resumedFromSliceId: latestSessionRecord?.resumedFromSliceId
2678
+ }),
2628
2679
  errorMessage: new TurnSliceLimitExceededError(
2629
2680
  botConfig.maxSlicesPerTurn
2630
2681
  ).message,
2631
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2632
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {}
2682
+ sliceId: args.currentSliceId,
2683
+ state: "failed"
2633
2684
  });
2634
2685
  }
2635
2686
  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,
2687
+ ...shared,
2656
2688
  errorMessage: args.errorMessage,
2657
- ...args.actor ?? latestSessionRecord?.actor ? { actor: args.actor ?? latestSessionRecord?.actor } : {},
2658
- ...getActiveTraceId() ?? latestSessionRecord?.traceId ? { traceId: getActiveTraceId() ?? latestSessionRecord?.traceId } : {}
2689
+ resumedFromSliceId: args.currentSliceId,
2690
+ sliceId: nextSliceId,
2691
+ state: "awaiting_resume"
2659
2692
  });
2660
2693
  } catch (recordError) {
2661
2694
  logSessionRecordError(
@@ -2681,34 +2714,39 @@ async function persistYieldSessionRecord(args) {
2681
2714
  args.sessionId
2682
2715
  );
2683
2716
  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
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
2693
2733
  ),
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",
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,
2700
2746
  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
2747
  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 } : {}
2748
+ sliceId: args.currentSliceId,
2749
+ state: "awaiting_resume"
2712
2750
  });
2713
2751
  } catch (recordError) {
2714
2752
  logSessionRecordError(
@@ -3088,9 +3126,6 @@ function createChannelConfigurationService(storage) {
3088
3126
  };
3089
3127
  }
3090
3128
 
3091
- // src/chat/state/ttl.ts
3092
- var JUNIOR_THREAD_STATE_TTL_MS = 7 * 24 * 60 * 60 * 1e3;
3093
-
3094
3129
  // src/chat/runtime/thread-state.ts
3095
3130
  function threadStateKey(threadId) {
3096
3131
  return `thread-state:${threadId}`;
@@ -3383,7 +3418,6 @@ export {
3383
3418
  persistAuthPauseSessionRecord,
3384
3419
  persistContinuationSessionRecord,
3385
3420
  persistYieldSessionRecord,
3386
- JUNIOR_THREAD_STATE_TTL_MS,
3387
3421
  canReusePendingAuthLink,
3388
3422
  getConversationPendingAuth,
3389
3423
  clearPendingAuth,