@sema-agent/client-core 0.93.1 → 0.95.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 (321) hide show
  1. package/CHANGELOG.md +554 -0
  2. package/README.md +89 -55
  3. package/dist/adapt/arms.js +13 -11
  4. package/dist/adapt/panelTasks.js +4 -10
  5. package/dist/adapt/toolCards.js +2 -2
  6. package/dist/adapt/wireShapes.js +3 -8
  7. package/dist/adapter/activeRunSelfHeal.d.ts +8 -3
  8. package/dist/adapter/activeRunSelfHeal.js +20 -12
  9. package/dist/adapter/downstream/eventToSdkMessage.js +24 -17
  10. package/dist/adapter/downstream/terminalToSdkResult.js +13 -6
  11. package/dist/adapter/runStream.js +2 -1
  12. package/dist/adapter/types.d.ts +7 -0
  13. package/dist/adapter/types.js +15 -0
  14. package/dist/agentSession/backgroundView.js +5 -1
  15. package/dist/agentSession/engineAgentRegistryRead.js +2 -1
  16. package/dist/agentSession/engineSessionBackgroundStop.js +5 -3
  17. package/dist/agentsWireCaps.d.ts +17 -0
  18. package/dist/agentsWireCaps.js +111 -1
  19. package/dist/approvalDecidedByCapability.d.ts +0 -1
  20. package/dist/approvalDecidedByCapability.js +2 -7
  21. package/dist/approvalReachWire.d.ts +18 -0
  22. package/dist/approvalReachWire.js +58 -0
  23. package/dist/approvalsStreamLiveCapability.d.ts +0 -1
  24. package/dist/approvalsStreamLiveCapability.js +15 -14
  25. package/dist/atMostOnce.d.ts +8 -0
  26. package/dist/atMostOnce.js +36 -0
  27. package/dist/attachmentSeatCapability.d.ts +0 -1
  28. package/dist/attachmentSeatCapability.js +0 -3
  29. package/dist/attachmentsWireCaps.js +11 -6
  30. package/dist/bashExitReading.d.ts +10 -0
  31. package/dist/bashExitReading.js +12 -0
  32. package/dist/callerCwdCapability.d.ts +15 -0
  33. package/dist/callerCwdCapability.js +42 -0
  34. package/dist/centerWiringCapability.d.ts +1 -3
  35. package/dist/centerWiringCapability.js +18 -21
  36. package/dist/classifierStatus.js +4 -4
  37. package/dist/cloudConfigWireCaps.js +43 -21
  38. package/dist/compensations.js +1 -1
  39. package/dist/controlRouter.js +3 -10
  40. package/dist/coreValuePorts.d.ts +64 -1
  41. package/dist/coreValuePorts.js +30 -0
  42. package/dist/cycleOrder.d.ts +15 -0
  43. package/dist/cycleOrder.js +45 -0
  44. package/dist/decideFailureNote.js +4 -1
  45. package/dist/decideReceipt.d.ts +2 -1
  46. package/dist/decideReceipt.js +41 -13
  47. package/dist/declared/supportFloorDeclared.d.ts +1 -0
  48. package/dist/declared/supportFloorDeclared.js +5 -0
  49. package/dist/detachWire.d.ts +1 -4
  50. package/dist/detachWire.js +1 -1
  51. package/dist/deviceExecutorManagementCapability.d.ts +0 -1
  52. package/dist/deviceExecutorManagementCapability.js +36 -40
  53. package/dist/deviceFlowPolling.d.ts +5 -0
  54. package/dist/deviceFlowPolling.js +28 -0
  55. package/dist/deviceRebind.d.ts +88 -0
  56. package/dist/deviceRebind.js +242 -0
  57. package/dist/displayUntrusted.d.ts +1 -0
  58. package/dist/displayUntrusted.js +963 -30
  59. package/dist/engineAgentAbsence.d.ts +2 -2
  60. package/dist/engineAgentPanelStore.d.ts +8 -18
  61. package/dist/engineAgentPanelStore.js +19 -537
  62. package/dist/engineCapReader.d.ts +6 -6
  63. package/dist/engineCapReader.js +30 -43
  64. package/dist/engineCapsCache.d.ts +7 -2
  65. package/dist/engineCapsCache.js +64 -26
  66. package/dist/engineCredentialRequirement.d.ts +9 -0
  67. package/dist/engineCredentialRequirement.js +23 -0
  68. package/dist/engineEnvSourcing.d.ts +53 -0
  69. package/dist/engineEnvSourcing.js +81 -0
  70. package/dist/engineErrorCodes.d.ts +19 -4
  71. package/dist/engineErrorCodes.js +39 -21
  72. package/dist/engineIdentity.js +2 -0
  73. package/dist/engineNoticeCodes.d.ts +22 -2
  74. package/dist/engineNoticeCodes.js +73 -6
  75. package/dist/engineRefusedToolName.d.ts +1 -0
  76. package/dist/engineRefusedToolName.js +8 -0
  77. package/dist/engineWireSdk.js +2 -2
  78. package/dist/executionLaneCapability.d.ts +2 -3
  79. package/dist/executionLaneCapability.js +28 -28
  80. package/dist/fileHistoryCaptureCapability.d.ts +0 -1
  81. package/dist/fileHistoryCaptureCapability.js +13 -12
  82. package/dist/fileHistoryLegNotice.d.ts +14 -2
  83. package/dist/fileHistoryLegNotice.js +101 -15
  84. package/dist/fleet/fleetLedger.d.ts +2 -0
  85. package/dist/fleet/fleetLedger.js +74 -52
  86. package/dist/fleet/fleetProjection.d.ts +0 -3
  87. package/dist/fleet/fleetProjection.js +9 -16
  88. package/dist/fleetAgentPanelProjection.d.ts +4 -17
  89. package/dist/fleetAgentPanelProjection.js +7 -197
  90. package/dist/fleetAgentRowLike.d.ts +16 -0
  91. package/dist/fleetAgentRowLike.js +1 -0
  92. package/dist/gateOutcome.d.ts +2 -1
  93. package/dist/gateOutcome.js +33 -18
  94. package/dist/gateVocabulary.js +1 -1
  95. package/dist/generated/engineAskFaceTables.d.ts +3 -0
  96. package/dist/generated/engineAskFaceTables.js +12 -0
  97. package/dist/generated/engineFactTables.d.ts +4 -2
  98. package/dist/generated/engineFactTables.js +13 -0
  99. package/dist/generated/engineNoticeTables.d.ts +5 -0
  100. package/dist/generated/engineNoticeTables.js +7 -0
  101. package/dist/generated/sdkFactTables.d.ts +5 -0
  102. package/dist/generated/sdkFactTables.js +34 -0
  103. package/dist/generated/settingsSchemaFns.d.ts +10 -0
  104. package/dist/generated/settingsSchemaFns.js +22 -0
  105. package/dist/generated/settingsSchemaTables.d.ts +3 -0
  106. package/dist/generated/settingsSchemaTables.js +3 -0
  107. package/dist/headlessPermissionModeWire.d.ts +2 -2
  108. package/dist/headlessPermissionModeWire.js +11 -21
  109. package/dist/headlessReconnectWire.js +49 -20
  110. package/dist/hitl/allowRuleShadow.d.ts +1 -1
  111. package/dist/hitl/allowRuleShadow.js +49 -8
  112. package/dist/hitl/approvalCompleteParked.d.ts +1 -0
  113. package/dist/hitl/approvalCompleteParked.js +14 -0
  114. package/dist/hitl/approvalEndingCause.d.ts +5 -0
  115. package/dist/hitl/approvalEndingCause.js +61 -0
  116. package/dist/hitl/approvalHolds.d.ts +46 -0
  117. package/dist/hitl/approvalHolds.js +140 -0
  118. package/dist/hitl/approvalOutcomeNote.js +2 -1
  119. package/dist/hitl/approvalResolution.js +9 -1
  120. package/dist/hitl/approvalsFeed.js +23 -6
  121. package/dist/hitl/askGateWire.d.ts +2 -1
  122. package/dist/hitl/askGateWire.js +29 -1
  123. package/dist/hitl/automaticAnswerVerdict.d.ts +8 -2
  124. package/dist/hitl/automaticAnswerVerdict.js +71 -23
  125. package/dist/hitl/crashConverged.js +2 -0
  126. package/dist/hitl/decisionConflict.d.ts +3 -0
  127. package/dist/hitl/decisionConflict.js +43 -0
  128. package/dist/hitl/decisionRefusal.d.ts +23 -0
  129. package/dist/hitl/decisionRefusal.js +88 -0
  130. package/dist/hitl/frameRouter.d.ts +10 -0
  131. package/dist/hitl/frameRouter.js +71 -32
  132. package/dist/hitl/gateLedger.d.ts +8 -0
  133. package/dist/hitl/gateLedger.js +38 -0
  134. package/dist/hitl/hitlBridge.d.ts +14 -3
  135. package/dist/hitl/hitlBridge.js +75 -62
  136. package/dist/hitl/hitlHostSurface.d.ts +3 -0
  137. package/dist/hitl/hitlHostSurface.js +17 -15
  138. package/dist/hitl/livePendingAsk.d.ts +16 -6
  139. package/dist/hitl/livePendingAsk.js +36 -4
  140. package/dist/hitl/localAllowRule.js +2 -1
  141. package/dist/hitl/noAllowCause.d.ts +9 -0
  142. package/dist/hitl/noAllowCause.js +26 -0
  143. package/dist/hitl/parkExpiryJudgements.d.ts +19 -0
  144. package/dist/hitl/parkExpiryJudgements.js +46 -0
  145. package/dist/hitl/parkResolver.d.ts +3 -7
  146. package/dist/hitl/parkResolver.js +55 -28
  147. package/dist/hitl/parkRowBirthWait.js +2 -1
  148. package/dist/hitl/persistedRulesWire.d.ts +6 -1
  149. package/dist/hitl/persistedRulesWire.js +16 -4
  150. package/dist/hitl/planReviewWire.d.ts +2 -3
  151. package/dist/hitl/planReviewWire.js +86 -39
  152. package/dist/hitl/replayedApproval.d.ts +8 -0
  153. package/dist/hitl/replayedApproval.js +125 -10
  154. package/dist/hitl/resumeRunningCard.d.ts +2 -0
  155. package/dist/hitl/resumeRunningCard.js +15 -4
  156. package/dist/hitl/sessionPolicyDeliverable.d.ts +1 -1
  157. package/dist/hitl/sessionPolicyDeliverable.js +3 -8
  158. package/dist/hitl/sessionPolicyWire.js +1 -1
  159. package/dist/hitl/toolApprovalWire.d.ts +50 -5
  160. package/dist/hitl/toolApprovalWire.js +368 -56
  161. package/dist/hooksWireCaps.js +38 -6
  162. package/dist/host.d.ts +5 -0
  163. package/dist/host.js +3 -1
  164. package/dist/hostCallbackGuard.d.ts +1 -0
  165. package/dist/hostCallbackGuard.js +25 -0
  166. package/dist/humanTurnRead.d.ts +15 -1
  167. package/dist/humanTurnRead.js +28 -2
  168. package/dist/index.d.ts +45 -14
  169. package/dist/index.js +33 -12
  170. package/dist/interactiveInterrupt.d.ts +8 -0
  171. package/dist/interactiveInterrupt.js +54 -0
  172. package/dist/limitsWire.d.ts +5 -1
  173. package/dist/limitsWire.js +51 -13
  174. package/dist/liveModelCatalog.d.ts +109 -0
  175. package/dist/liveModelCatalog.js +344 -18
  176. package/dist/mcpEngineLeg.d.ts +25 -0
  177. package/dist/mcpEngineLeg.js +71 -15
  178. package/dist/mcpPanel.js +2 -1
  179. package/dist/mcpProbeCapability.d.ts +0 -1
  180. package/dist/mcpProbeCapability.js +13 -12
  181. package/dist/mcpReconnect.d.ts +1 -3
  182. package/dist/mcpReconnect.js +20 -10
  183. package/dist/memoryComplianceCapability.d.ts +0 -1
  184. package/dist/memoryComplianceCapability.js +14 -13
  185. package/dist/memoryOriginCapability.d.ts +0 -1
  186. package/dist/memoryOriginCapability.js +14 -13
  187. package/dist/memoryVerbsWire.d.ts +1 -1
  188. package/dist/memoryVerbsWire.js +4 -0
  189. package/dist/model/liveCatalogShape.d.ts +14 -0
  190. package/dist/model/liveCatalogShape.js +71 -0
  191. package/dist/model/modelSupplyRules.d.ts +1 -0
  192. package/dist/model/modelSupplyRules.js +30 -9
  193. package/dist/model/modelsPolicy.d.ts +21 -0
  194. package/dist/model/modelsPolicy.js +90 -0
  195. package/dist/modelIdentityResolvability.d.ts +4 -2
  196. package/dist/modelIdentityResolvability.js +25 -5
  197. package/dist/modelSwitchFallback.d.ts +69 -0
  198. package/dist/modelSwitchFallback.js +206 -0
  199. package/dist/notifications.js +12 -11
  200. package/dist/outputStyleSeatCapability.d.ts +15 -0
  201. package/dist/outputStyleSeatCapability.js +42 -0
  202. package/dist/ownKey.d.ts +7 -0
  203. package/dist/ownKey.js +58 -0
  204. package/dist/panelReduce.d.ts +114 -0
  205. package/dist/panelReduce.js +1135 -0
  206. package/dist/panelStateHolder.d.ts +17 -0
  207. package/dist/panelStateHolder.js +201 -0
  208. package/dist/peerLaneCapability.d.ts +0 -1
  209. package/dist/peerLaneCapability.js +15 -14
  210. package/dist/permissionModesCapability.d.ts +0 -1
  211. package/dist/permissionModesCapability.js +5 -10
  212. package/dist/permissionRuleIssue.js +1 -1
  213. package/dist/permissionRulesWriteCapability.d.ts +0 -1
  214. package/dist/permissionRulesWriteCapability.js +15 -14
  215. package/dist/permissionsAllowGrantCapability.d.ts +2 -1
  216. package/dist/permissionsAllowGrantCapability.js +35 -19
  217. package/dist/permissionsDeclaration.d.ts +16 -1
  218. package/dist/permissionsDeclaration.js +142 -15
  219. package/dist/printToolResultFrame.js +2 -10
  220. package/dist/promptDelivery.js +0 -8
  221. package/dist/protocolSegment.d.ts +1 -0
  222. package/dist/protocolSegment.js +13 -1
  223. package/dist/request/taskRequest.d.ts +11 -3
  224. package/dist/request/taskRequest.js +13 -1
  225. package/dist/resultFrameKeys.d.ts +4 -0
  226. package/dist/resultFrameKeys.js +13 -0
  227. package/dist/resumeRefusalCopy.d.ts +18 -1
  228. package/dist/resumeRefusalCopy.js +132 -31
  229. package/dist/retryStatus.js +6 -4
  230. package/dist/rewindArchiveCapability.d.ts +2 -1
  231. package/dist/rewindArchiveCapability.js +28 -15
  232. package/dist/rewindEngineAction.d.ts +21 -0
  233. package/dist/rewindEngineAction.js +36 -0
  234. package/dist/rewindWireCaps.d.ts +2 -1
  235. package/dist/rewindWireCaps.js +1 -0
  236. package/dist/{hitl/rosterMountedNames.d.ts → rosterMountedNames.d.ts} +1 -1
  237. package/dist/runCancelContext.js +2 -1
  238. package/dist/runTerminal.d.ts +0 -11
  239. package/dist/runTerminal.js +4 -27
  240. package/dist/sdkRegistryTransit.d.ts +2 -1
  241. package/dist/sdkRegistryTransit.js +1 -0
  242. package/dist/sealedKeyCapability.d.ts +0 -1
  243. package/dist/sealedKeyCapability.js +19 -20
  244. package/dist/seatContract.d.ts +85 -28
  245. package/dist/seatContract.js +512 -148
  246. package/dist/sessionIdSegment.d.ts +1 -0
  247. package/dist/sessionIdSegment.js +11 -0
  248. package/dist/sessionMemoryErase.d.ts +1 -1
  249. package/dist/sessionMemoryErase.js +6 -1
  250. package/dist/sessionMemoryStatus.js +3 -2
  251. package/dist/sessionPolicyCapability.d.ts +0 -1
  252. package/dist/sessionPolicyCapability.js +14 -13
  253. package/dist/sessionReadRedactionCapability.d.ts +15 -0
  254. package/dist/sessionReadRedactionCapability.js +42 -0
  255. package/dist/sessionSyncFileHistoryCapability.d.ts +0 -1
  256. package/dist/sessionSyncFileHistoryCapability.js +13 -12
  257. package/dist/skillToolListsCapability.d.ts +0 -1
  258. package/dist/skillToolListsCapability.js +13 -12
  259. package/dist/skillsWireCaps.d.ts +4 -0
  260. package/dist/skillsWireCaps.js +66 -1
  261. package/dist/sqlEngineCapability.d.ts +0 -1
  262. package/dist/sqlEngineCapability.js +21 -21
  263. package/dist/steerPriorityCapability.d.ts +0 -1
  264. package/dist/steerPriorityCapability.js +18 -19
  265. package/dist/storePostureCapability.d.ts +1 -1
  266. package/dist/storePostureCapability.js +16 -16
  267. package/dist/streamDrainingLegs.d.ts +6 -0
  268. package/dist/streamDrainingLegs.js +23 -0
  269. package/dist/subagent/agentStateDetail.d.ts +1 -0
  270. package/dist/subagent/agentStateDetail.js +41 -0
  271. package/dist/subagent/engineCompactWire.d.ts +15 -0
  272. package/dist/subagent/engineCompactWire.js +82 -45
  273. package/dist/subagent/engineDelegatedPrompt.js +10 -6
  274. package/dist/subagent/engineSubagentOutput.d.ts +0 -1
  275. package/dist/subagent/engineSubagentOutput.js +5 -20
  276. package/dist/subagent/engineSubagentTail.d.ts +0 -1
  277. package/dist/subagent/engineSubagentTail.js +7 -32
  278. package/dist/subagentContentStore.js +3 -3
  279. package/dist/supportFloor.js +2 -5
  280. package/dist/taskRequestWords.d.ts +2 -4
  281. package/dist/taskRequestWords.js +3 -2
  282. package/dist/terminalStatuses.d.ts +30 -0
  283. package/dist/terminalStatuses.js +67 -0
  284. package/dist/toolResult.d.ts +5 -0
  285. package/dist/toolResult.js +88 -36
  286. package/dist/types/engineState.d.ts +2 -1
  287. package/dist/usageAnalytics.d.ts +146 -0
  288. package/dist/usageAnalytics.js +311 -0
  289. package/dist/userAttachmentEnvelope.d.ts +22 -1
  290. package/dist/userAttachmentEnvelope.js +90 -2
  291. package/dist/userInvokedSkillsCapability.d.ts +0 -1
  292. package/dist/userInvokedSkillsCapability.js +13 -12
  293. package/dist/webSearchBackendCapability.d.ts +0 -1
  294. package/dist/webSearchBackendCapability.js +20 -19
  295. package/dist/webSearchWireCaps.js +3 -11
  296. package/dist/wireErrorTriage.d.ts +11 -0
  297. package/dist/wireErrorTriage.js +35 -0
  298. package/dist/wireFailureShape.d.ts +10 -0
  299. package/dist/wireFailureShape.js +72 -5
  300. package/dist/wireRefusalCopy.d.ts +6 -1
  301. package/dist/wireRefusalCopy.js +23 -25
  302. package/dist/wiringInstructionsAttribution.d.ts +10 -0
  303. package/dist/wiringInstructionsAttribution.js +67 -0
  304. package/dist/workflowClient.js +19 -2
  305. package/dist/workflowsJournalCapability.d.ts +0 -1
  306. package/dist/workflowsJournalCapability.js +13 -12
  307. package/dist/writeProtectionCapability.d.ts +0 -1
  308. package/dist/writeProtectionCapability.js +26 -26
  309. package/docs/INTEGRATION-CLIENTS.md +1733 -52
  310. package/package.json +5 -5
  311. package/dist/askSurvivesStandingAnswer.d.ts +0 -13
  312. package/dist/askSurvivesStandingAnswer.js +0 -53
  313. package/dist/engineCapsGenerationGuard.d.ts +0 -4
  314. package/dist/engineCapsGenerationGuard.js +0 -19
  315. package/dist/hitl/askSurvivesPosture.d.ts +0 -20
  316. package/dist/hitl/askSurvivesPosture.js +0 -48
  317. package/dist/panelRunningHistory.d.ts +0 -22
  318. package/dist/panelRunningHistory.js +0 -82
  319. package/dist/registryConflict.d.ts +0 -3
  320. package/dist/registryConflict.js +0 -44
  321. /package/dist/{hitl/rosterMountedNames.js → rosterMountedNames.js} +0 -0
@@ -3,6 +3,8 @@ import { markEngineWorkflowNotified, registerOutstandingBgTask, registerOutstand
3
3
  import { parseWorkflowPollEnvelope, projectWorkflowTaskOutput } from './workflow.js';
4
4
  import { asModelFallbackReason } from './engineErrorCodes.js';
5
5
  import { frozenSet } from './frozenSet.js';
6
+ import { bashExitOf, bashExitReadingFailed } from './bashExitReading.js';
7
+ import { isCoreSemaTaskTerminal } from './terminalStatuses.js';
6
8
  import { CORE_ENGINE_CARD_TYPES } from './generated/engineFactTables.js';
7
9
  export const STRUCTURED_DETAIL_TYPES = frozenSet(CORE_ENGINE_CARD_TYPES);
8
10
  export function structuredDetailType(structured) {
@@ -12,11 +14,6 @@ export function structuredDetailType(structured) {
12
14
  return typeof t === 'string' && STRUCTURED_DETAIL_TYPES.has(t) ? t : undefined;
13
15
  }
14
16
  const MAX_STRUCTURED_FIELD_CHARS = 1_000_000;
15
- const WF_TERMINAL_STATUSES = new Set([
16
- 'completed',
17
- 'failed',
18
- 'cancelled',
19
- ]);
20
17
  export const PROTOCOL_MARKERS = {
21
18
  bashHead: /^exit code: (-?\d+)(?: \(([^)\n]*)\))?\n(--- stdout ---\n)?/,
22
19
  bashStderrSep: '\n--- stderr ---\n',
@@ -28,6 +25,7 @@ export const PROTOCOL_MARKERS = {
28
25
  delegationNoTextResult: '(no text result)',
29
26
  delegationTrailer: 'Verify this result before concluding. If it leaves more to do, continue or delegate again.',
30
27
  delegationTrailerFork: 'Verify this result before concluding. If it leaves more to do, continue the work yourself or fork again.',
28
+ delegationFieldLine: /^(status|error|error_kind|observer|stats|worktree):(?: ([^\r\u2028\u2029\u0085]*))?/,
31
29
  taskOutputLabel: /^TaskOutput\s+(\S+)$/,
32
30
  taskOutputStatus: /^status:\s*(.+)$/m,
33
31
  taskOutputHeadSlots: /^(\r?\n)(?:(state: ([^\r\n\u2028\u2029\u0085]*)(?:\r?\n|$)))?(?:(error: ([^\r\n\u2028\u2029\u0085]*)(?:\r?\n|$)))?/,
@@ -35,7 +33,7 @@ export const PROTOCOL_MARKERS = {
35
33
  taskOutputNoneSentinel: '(none)',
36
34
  taskOutputStdoutLine: /^--- (?:new )?stdout[^\n]*---\n?/m,
37
35
  taskOutputStderrLine: /^--- (?:new )?stderr[^\n]*---\n?/m,
38
- taskOutputResultLine: /^--- result(?: \([^)\n]*\))? ---\n?/m,
36
+ taskOutputResultLine: /^--- result(?: \((?:[^)\n\r\u2028\u2029]|[\r\u2028\u2029](?!--- result))*\))? ---\n?/m,
39
37
  spoolStdoutFull: '--- stdout (full, re-readable) ---',
40
38
  spoolStdoutIncremental: '--- new stdout ---',
41
39
  };
@@ -78,6 +76,59 @@ export function stripUntrustedFence(text) {
78
76
  return null;
79
77
  return { label: m[1] ?? '', body: m[2] ?? '' };
80
78
  }
79
+ function delegationFrameSlots(lines, headAt, labelAt, fromAt) {
80
+ const read = (i) => {
81
+ const line = lines[i] ?? '';
82
+ const hit = PROTOCOL_MARKERS.delegationFieldLine.exec(line);
83
+ if (hit === null)
84
+ return null;
85
+ const value = hit[2];
86
+ return { key: hit[1] ?? '', value: value !== undefined && hit[0].length === line.length && value.trim() !== '' ? value : undefined };
87
+ };
88
+ const fields = [];
89
+ for (let i = headAt + 1; i < labelAt; i++)
90
+ fields.push(read(i));
91
+ const errorFollowedByEngineField = (at) => {
92
+ const next = fields[at + 1];
93
+ return next !== null && next !== undefined && next.value !== undefined && (next.key === 'error_kind' || next.key === 'observer' || next.key === 'stats');
94
+ };
95
+ const worktreeBefore = (at) => fields.slice(0, at).some((f) => f?.key === 'worktree');
96
+ const soleAt = (key) => {
97
+ let at = -1;
98
+ for (let i = 0; i < fields.length; i++) {
99
+ if (fields[i]?.key !== key)
100
+ continue;
101
+ if (at >= 0)
102
+ return -1;
103
+ at = i;
104
+ }
105
+ return at;
106
+ };
107
+ const claimed = new Set([headAt, labelAt]);
108
+ if (fromAt > labelAt + 1)
109
+ claimed.add(labelAt + 1);
110
+ const slots = {};
111
+ const first = fields[0] ?? null;
112
+ if (first !== null && first.key === 'status' && first.value !== undefined && soleAt('status') === 0) {
113
+ slots.status = first.value.trim();
114
+ claimed.add(headAt + 1);
115
+ for (const key of ['error', 'stats']) {
116
+ const at = soleAt(key);
117
+ const value = at >= 0 ? fields[at]?.value : undefined;
118
+ if (value === undefined || (key === 'error' && (!errorFollowedByEngineField(at) || worktreeBefore(at))))
119
+ continue;
120
+ slots[key] = value;
121
+ claimed.add(headAt + 1 + at);
122
+ }
123
+ }
124
+ let rest = '';
125
+ for (let i = 0; i < fromAt; i++)
126
+ if (!claimed.has(i))
127
+ rest += `${lines[i] ?? ''}\n`;
128
+ if (rest.trim() !== '')
129
+ slots.headRest = rest;
130
+ return slots;
131
+ }
81
132
  function locateDelegationResultSection(text) {
82
133
  const lines = text.split('\n');
83
134
  const soleLineAt = (hit) => {
@@ -107,14 +158,14 @@ function locateDelegationResultSection(text) {
107
158
  };
108
159
  if (trailerAt === labelAt + 2 && lines[labelAt + 1] === PROTOCOL_MARKERS.delegationNoTextResult) {
109
160
  const c = cut(labelAt + 1);
110
- return { section: 'no_text_result', before: c.before, after: c.after };
161
+ return { section: 'no_text_result', before: c.before, after: c.after, slots: delegationFrameSlots(lines, headAt, labelAt, labelAt + 1) };
111
162
  }
112
163
  const partial = lines[labelAt + 1] === PROTOCOL_MARKERS.delegationPartialFindingsLabel;
113
164
  const from = partial ? labelAt + 2 : labelAt + 1;
114
165
  if (from >= trailerAt)
115
166
  return null;
116
167
  const c = cut(from);
117
- return { section: partial ? 'partial_findings' : 'result', before: c.before, fenceText: c.middle, after: c.after };
168
+ return { section: partial ? 'partial_findings' : 'result', before: c.before, fenceText: c.middle, after: c.after, slots: delegationFrameSlots(lines, headAt, labelAt, from) };
118
169
  }
119
170
  export function displayBody(text, origin = 'any_tool_result') {
120
171
  if (typeof text !== 'string')
@@ -128,14 +179,14 @@ export function displayBody(text, origin = 'any_tool_result') {
128
179
  if (located === null)
129
180
  return { fenced: false, body: text };
130
181
  if (located.section === 'no_text_result') {
131
- const { before, after, section } = located;
132
- return { fenced: false, body: '', frame: { before, after, section } };
182
+ const { before, after, section, slots } = located;
183
+ return { fenced: false, body: '', frame: { before, after, section, ...slots } };
133
184
  }
134
185
  const inner = stripUntrustedFence(located.fenceText);
135
186
  if (inner === null)
136
187
  return { fenced: false, body: text };
137
- const { before, after, section } = located;
138
- return { fenced: true, label: inner.label, body: inner.body, frame: { before, after, section } };
188
+ const { before, after, section, slots } = located;
189
+ return { fenced: true, label: inner.label, body: inner.body, frame: { before, after, section, ...slots } };
139
190
  }
140
191
  export function parseModelFacingTaskOutput(text) {
141
192
  modelFacingParseCount++;
@@ -241,7 +292,7 @@ export function wireOutputToBody(toolName, output, isError, truncated, flatten,
241
292
  : typeof bgBlock?.task_id === 'string'
242
293
  ? bgBlock.task_id
243
294
  : undefined;
244
- if (s?.detached === true)
295
+ if (readBashDetachView(structured)?.handoffTaskId !== undefined)
245
296
  bgTaskId = undefined;
246
297
  }
247
298
  else {
@@ -255,20 +306,23 @@ export function wireOutputToBody(toolName, output, isError, truncated, flatten,
255
306
  };
256
307
  }
257
308
  }
258
- const slots = sType === 'bash' && (typeof s?.stdout === 'string' || typeof s?.stderr === 'string')
309
+ let parsedMemo;
310
+ const parseOnce = () => (parsedMemo === undefined ? (parsedMemo = parseModelFacingBash(text)) : parsedMemo);
311
+ const exit = bashExitOf(structured, parseOnce);
312
+ const structuredSlots = sType === 'bash' && (typeof s?.stdout === 'string' || typeof s?.stderr === 'string');
313
+ const textSlots = structuredSlots ? null : parseOnce();
314
+ const slots = structuredSlots
259
315
  ? {
260
316
  stdout: typeof s?.stdout === 'string' ? s.stdout : '',
261
317
  stderr: typeof s?.stderr === 'string' ? s.stderr : '',
262
- exitCode: typeof s?.exitCode === 'number' ? s.exitCode : null,
318
+ exitCode: exit.exitCode,
263
319
  }
264
- : parseModelFacingBash(text);
320
+ : textSlots !== null
321
+ ? { ...textSlots, exitCode: exit.exitCode }
322
+ : null;
265
323
  if (slots) {
266
- const benignInterp = typeof s?.returnCodeInterpretation === 'string' && s.returnCodeInterpretation.length > 0
267
- ? s.returnCodeInterpretation
268
- : 'note' in slots && typeof slots.note === 'string' && slots.note.length > 0
269
- ? slots.note
270
- : undefined;
271
- const failed = isError || (slots.exitCode != null && slots.exitCode !== 0 && benignInterp === undefined);
324
+ const benignInterp = exit.benignNote;
325
+ const failed = isError || bashExitReadingFailed(exit);
272
326
  if (failed) {
273
327
  const reason = slots.stderr.length > 0 ? slots.stderr : slots.stdout;
274
328
  let body = slots.exitCode != null ? `Exit code ${slots.exitCode}\n${reason}` : reason;
@@ -293,7 +347,7 @@ export function wireOutputToBody(toolName, output, isError, truncated, flatten,
293
347
  };
294
348
  }
295
349
  const stdout = truncated ? `${text}\n…(truncated)` : text;
296
- if (isError)
350
+ if (isError || bashExitReadingFailed(exit))
297
351
  return { content: stdout, isError: true };
298
352
  return { content: stdout, isError, toolUseResult: { stdout, stderr: '', interrupted: false } };
299
353
  }
@@ -316,7 +370,7 @@ export function wireOutputToBody(toolName, output, isError, truncated, flatten,
316
370
  }
317
371
  const wfEnvelope = parseWorkflowPollEnvelope(text);
318
372
  if (wfEnvelope) {
319
- if (leaderLedger && WF_TERMINAL_STATUSES.has(wfEnvelope.status))
373
+ if (leaderLedger && isCoreSemaTaskTerminal(wfEnvelope.status))
320
374
  markEngineWorkflowNotified(wfEnvelope.taskId);
321
375
  return wrap({
322
376
  task_id: wfEnvelope.taskId,
@@ -362,23 +416,22 @@ export function structuredToToolUseResult(structured, modelText, leaderLedger =
362
416
  };
363
417
  }
364
418
  case 'bash': {
365
- if (s.detached === true && typeof s.task_id === 'string') {
419
+ const handoffTaskId = readBashDetachView(structured)?.handoffTaskId;
420
+ if (handoffTaskId !== undefined) {
366
421
  return {
367
422
  toolUseResult: {
368
423
  stdout: '',
369
424
  stderr: '',
370
425
  interrupted: false,
371
- backgroundTaskId: s.task_id,
426
+ backgroundTaskId: handoffTaskId,
372
427
  backgroundedByUser: true,
373
428
  },
374
429
  };
375
430
  }
376
431
  if (typeof s.stdout !== 'string' && typeof s.stderr !== 'string')
377
432
  return null;
378
- const exitCode = typeof s.exitCode === 'number' ? s.exitCode : null;
379
- const benignInterp = typeof s.returnCodeInterpretation === 'string' && s.returnCodeInterpretation.length > 0
380
- ? s.returnCodeInterpretation
381
- : undefined;
433
+ const exit = bashExitOf(structured, () => null);
434
+ const benignInterp = exit.benignNote;
382
435
  return {
383
436
  toolUseResult: {
384
437
  stdout: typeof s.stdout === 'string' ? s.stdout : '',
@@ -386,7 +439,7 @@ export function structuredToToolUseResult(structured, modelText, leaderLedger =
386
439
  interrupted: false,
387
440
  ...(benignInterp !== undefined ? { returnCodeInterpretation: benignInterp } : {}),
388
441
  },
389
- ...(exitCode !== null && exitCode !== 0 && benignInterp === undefined ? { isError: true } : {}),
442
+ ...(bashExitReadingFailed(exit) ? { isError: true } : {}),
390
443
  };
391
444
  }
392
445
  case 'edit': {
@@ -650,7 +703,7 @@ export function structuredToToolUseResult(structured, modelText, leaderLedger =
650
703
  const runErrorCode = typeof s.errorCode === 'string' && s.errorCode.length > 0 ? s.errorCode : undefined;
651
704
  let output = bodyText ?? '';
652
705
  if (taskType === 'workflow') {
653
- if (leaderLedger && WF_TERMINAL_STATUSES.has(status))
706
+ if (leaderLedger && isCoreSemaTaskTerminal(status))
654
707
  markEngineWorkflowNotified(taskId);
655
708
  const jsonSrc = output.length > 0 ? output : (modelText ?? '');
656
709
  output = projectWorkflowTaskOutput(jsonSrc);
@@ -716,9 +769,9 @@ export function detectEngineBgShellDetach(toolName, rawInput, structured, output
716
769
  if (toolName !== 'Bash')
717
770
  return null;
718
771
  const view = readBashDetachView(structured);
719
- if (view === null || !view.detached || view.registerFailed || view.taskId === undefined)
772
+ if (view === null || view.registerFailed || view.handoffTaskId === undefined)
720
773
  return null;
721
- return engineBgShellRegistrationBody(view.taskId, rawInput, typeof outputText === 'string' ? outputText : '', 'bash');
774
+ return engineBgShellRegistrationBody(view.handoffTaskId, rawInput, typeof outputText === 'string' ? outputText : '', 'bash');
722
775
  }
723
776
  function readBashDetachView(structured) {
724
777
  if (typeof structured !== 'object' || structured === null)
@@ -731,9 +784,8 @@ function readBashDetachView(structured) {
731
784
  const registerFailed = s.registerFailed;
732
785
  const taskId = s.task_id;
733
786
  return {
734
- detached: detached === true,
787
+ handoffTaskId: detached === true && typeof taskId === 'string' && taskId.length > 0 ? taskId : undefined,
735
788
  registerFailed: registerFailed === true,
736
- taskId: typeof taskId === 'string' && taskId.length > 0 ? taskId : undefined,
737
789
  };
738
790
  }
739
791
  export function todoWriteToolUseResult(rawInput, oldTodos, isError) {
@@ -1,5 +1,5 @@
1
1
  import type { EngineHealth } from '@sema-agent/sdk';
2
- export interface HealthResponse extends Pick<EngineHealth, 'status' | 'model' | 'sessionBackend' | 'sessions' | 'instanceId' | 'dataRoot' | 'configHash' | 'pid' | 'port' | 'restartRequired' | 'version'> {
2
+ export interface HealthResponse extends Pick<EngineHealth, 'status' | 'model' | 'sessionBackend' | 'sessions' | 'instanceId' | 'dataRoot' | 'configHash' | 'pid' | 'port' | 'restartRequired' | 'version' | 'startedAt'> {
3
3
  }
4
4
  export interface EnginePortFile {
5
5
  port: number;
@@ -19,6 +19,7 @@ export interface EngineInstance {
19
19
  instanceId?: string;
20
20
  dataRoot?: string;
21
21
  configHash?: string;
22
+ startedAt?: number;
22
23
  logPath?: string;
23
24
  ownedByThisShell: boolean;
24
25
  child?: unknown;
@@ -0,0 +1,146 @@
1
+ import type { UsageAnalyticsParams, UsageBreakdown, UsageDimension, UsageGranularity, UsageMetric, UsageSeries, UsageSummary } from '@sema-agent/sdk';
2
+ export declare const USAGE_SENTENCES: Readonly<{
3
+ analyticsUnavailable: "Usage analytics aren’t available on this backend.";
4
+ analyticsUnread: "Usage analytics couldn’t be read just now.";
5
+ modelsUnavailable: "Per-model usage breakdown isn't available on this backend.";
6
+ modelsUnread: "Per-model usage breakdown couldn’t be read just now.";
7
+ }>;
8
+ export interface UsageSummaryQuery extends UsageAnalyticsParams {
9
+ signal?: AbortSignal;
10
+ }
11
+ export interface UsageSeriesQuery extends UsageAnalyticsParams {
12
+ metric?: UsageMetric;
13
+ granularity?: UsageGranularity;
14
+ signal?: AbortSignal;
15
+ }
16
+ export interface UsageBreakdownQuery extends UsageAnalyticsParams {
17
+ dimension?: UsageDimension;
18
+ signal?: AbortSignal;
19
+ }
20
+ export interface UsageAnalyticsVerbs {
21
+ summary?: (opts?: UsageSummaryQuery) => Promise<UsageSummary>;
22
+ series?: (opts?: UsageSeriesQuery) => Promise<UsageSeries>;
23
+ breakdown?: (opts?: UsageBreakdownQuery) => Promise<UsageBreakdown>;
24
+ }
25
+ export interface UsageAnalyticsClientLike {
26
+ usage: UsageAnalyticsVerbs;
27
+ }
28
+ export interface UsageReadOptions {
29
+ from?: string | number;
30
+ to?: string | number;
31
+ principal?: string;
32
+ signal?: AbortSignal;
33
+ }
34
+ export interface UsageSeriesReadOptions extends UsageReadOptions {
35
+ metric?: UsageMetric;
36
+ granularity?: UsageGranularity;
37
+ }
38
+ export interface UsageBreakdownReadOptions extends UsageReadOptions {
39
+ dimension?: UsageDimension;
40
+ }
41
+ export type UsageBound = 'exact' | 'lower' | 'approximate';
42
+ export type UsagePrecision = 'exact' | 'estimated';
43
+ export interface UsageTotalsView {
44
+ tasks?: number;
45
+ tokensIn?: number;
46
+ tokensOut?: number;
47
+ cost?: number;
48
+ precision: UsagePrecision;
49
+ }
50
+ export interface UsageWindowView {
51
+ from?: string;
52
+ to?: string;
53
+ truncated: boolean;
54
+ }
55
+ export type UsageSummaryView = UsageWindowView & UsageTotalsView;
56
+ export interface UsageSeriesPointView {
57
+ t: string;
58
+ v: number;
59
+ estimated?: boolean;
60
+ }
61
+ export interface UsageSeriesView extends UsageWindowView {
62
+ points: UsageSeriesPointView[];
63
+ unreadable: number;
64
+ }
65
+ export type UsageBreakdownRowView = UsageTotalsView & {
66
+ key: string;
67
+ };
68
+ export interface UsageBreakdownView extends UsageWindowView {
69
+ rows: UsageBreakdownRowView[];
70
+ unreadable: number;
71
+ }
72
+ export type UsageUnavailableWhy = 'capability' | 'feature' | 'route';
73
+ export type UsageReadVerdict<T> = {
74
+ readonly kind: 'ok';
75
+ readonly value: T;
76
+ } | {
77
+ readonly kind: 'unavailable';
78
+ readonly why: UsageUnavailableWhy;
79
+ } | {
80
+ readonly kind: 'failed';
81
+ readonly error: unknown;
82
+ };
83
+ export declare function classifyUsageFailure(e: unknown): UsageReadVerdict<never>;
84
+ export declare function projectUsageSummary(body: unknown): UsageSummaryView | undefined;
85
+ export declare function projectUsageSeries(body: unknown): UsageSeriesView | undefined;
86
+ export declare function projectUsageBreakdown(body: unknown): UsageBreakdownView | undefined;
87
+ export declare function readUsageSummary(client: UsageAnalyticsClientLike, opts?: UsageReadOptions): Promise<UsageReadVerdict<UsageSummaryView>>;
88
+ export declare function readUsageSeries(client: UsageAnalyticsClientLike, opts?: UsageSeriesReadOptions): Promise<UsageReadVerdict<UsageSeriesView>>;
89
+ export declare function readUsageBreakdown(client: UsageAnalyticsClientLike, opts?: UsageBreakdownReadOptions): Promise<UsageReadVerdict<UsageBreakdownView>>;
90
+ export interface UsageAmount {
91
+ readonly value: number;
92
+ readonly bound: UsageBound;
93
+ }
94
+ export declare function usageBoundPrefix(bound: UsageBound): '' | '≥' | '≈';
95
+ export interface UsageHeatmapCell {
96
+ readonly date: string;
97
+ readonly tasks: number;
98
+ }
99
+ export type UsageUnavailableCause = 'no_backend_face' | 'read_failed';
100
+ export type UsageModelsView = {
101
+ readonly state: 'available';
102
+ readonly rows: readonly UsageBreakdownRowView[];
103
+ readonly unreadable: number;
104
+ } | {
105
+ readonly state: 'unavailable';
106
+ readonly cause: UsageUnavailableCause;
107
+ readonly sentence: string;
108
+ };
109
+ export interface UsageAnalyticsReadings {
110
+ readonly state: 'available' | 'estimated';
111
+ readonly truncated: boolean;
112
+ readonly runs?: number;
113
+ readonly tokens?: UsageAmount;
114
+ readonly cost?: UsageAmount;
115
+ readonly activeDays?: number;
116
+ readonly peakHourUtc?: number;
117
+ readonly favoriteModel?: string;
118
+ readonly heatmap: readonly UsageHeatmapCell[] | null;
119
+ readonly models: UsageModelsView;
120
+ }
121
+ export interface UsageAnalyticsUnavailable {
122
+ readonly state: 'unavailable';
123
+ readonly cause: UsageUnavailableCause;
124
+ readonly sentence: string;
125
+ readonly models: Extract<UsageModelsView, {
126
+ state: 'unavailable';
127
+ }>;
128
+ }
129
+ export type UsageAnalyticsView = UsageAnalyticsReadings | UsageAnalyticsUnavailable;
130
+ export interface UsageAnalyticsInputs {
131
+ summary: UsageReadVerdict<UsageSummaryView>;
132
+ daySeries: UsageReadVerdict<UsageSeriesView>;
133
+ hourSeries: UsageReadVerdict<UsageSeriesView>;
134
+ modelBreakdown: UsageReadVerdict<UsageBreakdownView>;
135
+ }
136
+ export interface UsageAnalyticsWindow {
137
+ readonly from: number;
138
+ readonly to: number;
139
+ }
140
+ export type UsageRange = 'all' | '30d' | '7d';
141
+ export declare const USAGE_HEATMAP_MAX_DAYS = 70;
142
+ export declare function usageWindowOfRange(range: UsageRange, now: number): UsageAnalyticsWindow;
143
+ export declare function projectUsageAnalytics(inputs: UsageAnalyticsInputs, window: UsageAnalyticsWindow): UsageAnalyticsView;
144
+ export declare function readUsageAnalytics(client: UsageAnalyticsClientLike, window: UsageAnalyticsWindow, opts?: {
145
+ signal?: AbortSignal;
146
+ }): Promise<UsageAnalyticsView>;