@sixb/agent 0.0.0-stage → 0.2.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 (240) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +31 -2
  3. package/dist/abort.d.ts +6 -0
  4. package/dist/abort.d.ts.map +1 -0
  5. package/dist/agent-prompt.d.ts +126 -0
  6. package/dist/agent-prompt.d.ts.map +1 -0
  7. package/dist/agent-runtime/errors.d.ts +13 -0
  8. package/dist/agent-runtime/errors.d.ts.map +1 -0
  9. package/dist/agent-runtime/preflight.d.ts +18 -0
  10. package/dist/agent-runtime/preflight.d.ts.map +1 -0
  11. package/dist/agent-runtime/profile.d.ts +14 -0
  12. package/dist/agent-runtime/profile.d.ts.map +1 -0
  13. package/dist/artifacts/layout-check/layout-check.d.ts +87 -0
  14. package/dist/artifacts/layout-check/layout-check.d.ts.map +1 -0
  15. package/dist/artifacts/materialize.d.ts +115 -0
  16. package/dist/artifacts/materialize.d.ts.map +1 -0
  17. package/dist/artifacts/publish-tool.d.ts +86 -0
  18. package/dist/artifacts/publish-tool.d.ts.map +1 -0
  19. package/dist/ask-parent-tool.d.ts +36 -0
  20. package/dist/ask-parent-tool.d.ts.map +1 -0
  21. package/dist/attachments.d.ts +109 -0
  22. package/dist/attachments.d.ts.map +1 -0
  23. package/dist/child-files.d.ts +91 -0
  24. package/dist/child-files.d.ts.map +1 -0
  25. package/dist/child-questions.d.ts +24 -0
  26. package/dist/child-questions.d.ts.map +1 -0
  27. package/dist/computer-runner.d.ts +74 -0
  28. package/dist/computer-runner.d.ts.map +1 -0
  29. package/dist/context-budget.d.ts +34 -0
  30. package/dist/context-budget.d.ts.map +1 -0
  31. package/dist/context-compaction.d.ts +148 -0
  32. package/dist/context-compaction.d.ts.map +1 -0
  33. package/dist/context-guard.d.ts +69 -0
  34. package/dist/context-guard.d.ts.map +1 -0
  35. package/dist/conversation-memory.d.ts +21 -0
  36. package/dist/conversation-memory.d.ts.map +1 -0
  37. package/dist/conversation-messages.d.ts +33 -0
  38. package/dist/conversation-messages.d.ts.map +1 -0
  39. package/dist/errors.d.ts +46 -0
  40. package/dist/errors.d.ts.map +1 -0
  41. package/dist/execution-plan.d.ts +38 -0
  42. package/dist/execution-plan.d.ts.map +1 -0
  43. package/dist/failure.d.ts +14 -0
  44. package/dist/failure.d.ts.map +1 -0
  45. package/dist/family-mailbox.d.ts +60 -0
  46. package/dist/family-mailbox.d.ts.map +1 -0
  47. package/dist/file-ref.d.ts +6 -0
  48. package/dist/file-ref.d.ts.map +1 -0
  49. package/dist/image-attachments.d.ts +34 -0
  50. package/dist/image-attachments.d.ts.map +1 -0
  51. package/dist/index.d.ts +35 -0
  52. package/dist/index.d.ts.map +1 -0
  53. package/dist/index.js +14387 -0
  54. package/dist/index.js.map +72 -0
  55. package/dist/inline-images.d.ts +28 -0
  56. package/dist/inline-images.d.ts.map +1 -0
  57. package/dist/message-authors.d.ts +34 -0
  58. package/dist/message-authors.d.ts.map +1 -0
  59. package/dist/message-times.d.ts +9 -0
  60. package/dist/message-times.d.ts.map +1 -0
  61. package/dist/model-adapters.d.ts +59 -0
  62. package/dist/model-adapters.d.ts.map +1 -0
  63. package/dist/model-lines.d.ts +16 -0
  64. package/dist/model-lines.d.ts.map +1 -0
  65. package/dist/prompt-cache.d.ts +28 -0
  66. package/dist/prompt-cache.d.ts.map +1 -0
  67. package/dist/requester.d.ts +15 -0
  68. package/dist/requester.d.ts.map +1 -0
  69. package/dist/run-agent-turn.d.ts +93 -0
  70. package/dist/run-agent-turn.d.ts.map +1 -0
  71. package/dist/run-child.d.ts +77 -0
  72. package/dist/run-child.d.ts.map +1 -0
  73. package/dist/run-conversation-turn.d.ts +42 -0
  74. package/dist/run-conversation-turn.d.ts.map +1 -0
  75. package/dist/run-credential.d.ts +22 -0
  76. package/dist/run-credential.d.ts.map +1 -0
  77. package/dist/run-environment.d.ts +145 -0
  78. package/dist/run-environment.d.ts.map +1 -0
  79. package/dist/run-fate.d.ts +15 -0
  80. package/dist/run-fate.d.ts.map +1 -0
  81. package/dist/run-files-lock.d.ts +16 -0
  82. package/dist/run-files-lock.d.ts.map +1 -0
  83. package/dist/run-sandbox.d.ts +60 -0
  84. package/dist/run-sandbox.d.ts.map +1 -0
  85. package/dist/run-subagent.d.ts +65 -0
  86. package/dist/run-subagent.d.ts.map +1 -0
  87. package/dist/run-workflow-agent-node.d.ts +46 -0
  88. package/dist/run-workflow-agent-node.d.ts.map +1 -0
  89. package/dist/sandbox-api-context.d.ts +34 -0
  90. package/dist/sandbox-api-context.d.ts.map +1 -0
  91. package/dist/sandbox-file-registry.d.ts +9 -0
  92. package/dist/sandbox-file-registry.d.ts.map +1 -0
  93. package/dist/sandbox-files.d.ts +71 -0
  94. package/dist/sandbox-files.d.ts.map +1 -0
  95. package/dist/sandbox-handle.d.ts +15 -0
  96. package/dist/sandbox-handle.d.ts.map +1 -0
  97. package/dist/sandbox-readiness.d.ts +8 -0
  98. package/dist/sandbox-readiness.d.ts.map +1 -0
  99. package/dist/skills/built-in.d.ts +15 -0
  100. package/dist/skills/built-in.d.ts.map +1 -0
  101. package/dist/subagent-context.d.ts +50 -0
  102. package/dist/subagent-context.d.ts.map +1 -0
  103. package/dist/subagent-continuation.d.ts +33 -0
  104. package/dist/subagent-continuation.d.ts.map +1 -0
  105. package/dist/subagent-inbox.d.ts +53 -0
  106. package/dist/subagent-inbox.d.ts.map +1 -0
  107. package/dist/subagent-tools.d.ts +293 -0
  108. package/dist/subagent-tools.d.ts.map +1 -0
  109. package/dist/thread-context.d.ts +17 -0
  110. package/dist/thread-context.d.ts.map +1 -0
  111. package/dist/tools/attachments.d.ts +11 -0
  112. package/dist/tools/attachments.d.ts.map +1 -0
  113. package/dist/tools/bash.d.ts +68 -0
  114. package/dist/tools/bash.d.ts.map +1 -0
  115. package/dist/tools/model-spec.d.ts +9 -0
  116. package/dist/tools/model-spec.d.ts.map +1 -0
  117. package/dist/tools/read.d.ts +43 -0
  118. package/dist/tools/read.d.ts.map +1 -0
  119. package/dist/tools/result-media.d.ts +65 -0
  120. package/dist/tools/result-media.d.ts.map +1 -0
  121. package/dist/tools/result-output.d.ts +18 -0
  122. package/dist/tools/result-output.d.ts.map +1 -0
  123. package/dist/tools/tool-files.d.ts +59 -0
  124. package/dist/tools/tool-files.d.ts.map +1 -0
  125. package/dist/tools/tool-outputs.d.ts +63 -0
  126. package/dist/tools/tool-outputs.d.ts.map +1 -0
  127. package/dist/tools/view-file.d.ts +40 -0
  128. package/dist/tools/view-file.d.ts.map +1 -0
  129. package/dist/tsconfig.json +5 -0
  130. package/dist/turn-runtime.d.ts +19 -0
  131. package/dist/turn-runtime.d.ts.map +1 -0
  132. package/dist/types.d.ts +83 -0
  133. package/dist/types.d.ts.map +1 -0
  134. package/dist/workflow-input-attachments.d.ts +18 -0
  135. package/dist/workflow-input-attachments.d.ts.map +1 -0
  136. package/package.json +59 -4
  137. package/src/abort.ts +40 -0
  138. package/src/agent-prompt.ts +860 -0
  139. package/src/agent-runtime/errors.ts +44 -0
  140. package/src/agent-runtime/preflight.ts +314 -0
  141. package/src/agent-runtime/profile.ts +23 -0
  142. package/src/artifacts/layout-check/check.cjs +478 -0
  143. package/src/artifacts/layout-check/layout-check.ts +460 -0
  144. package/src/artifacts/materialize.ts +380 -0
  145. package/src/artifacts/publish-tool.ts +700 -0
  146. package/src/ask-parent-tool.ts +100 -0
  147. package/src/attachments.ts +1041 -0
  148. package/src/child-files.ts +535 -0
  149. package/src/child-questions.ts +42 -0
  150. package/src/computer-runner.ts +336 -0
  151. package/src/context-budget.ts +177 -0
  152. package/src/context-compaction.ts +1349 -0
  153. package/src/context-guard.ts +826 -0
  154. package/src/conversation-memory.ts +45 -0
  155. package/src/conversation-messages.ts +86 -0
  156. package/src/errors.ts +85 -0
  157. package/src/execution-plan.ts +70 -0
  158. package/src/failure.ts +82 -0
  159. package/src/family-mailbox.ts +189 -0
  160. package/src/file-ref.ts +18 -0
  161. package/src/image-attachments.ts +322 -0
  162. package/src/index.ts +54 -0
  163. package/src/inline-images.ts +69 -0
  164. package/src/message-authors.ts +77 -0
  165. package/src/message-times.ts +24 -0
  166. package/src/model-adapters.ts +273 -0
  167. package/src/model-lines.ts +28 -0
  168. package/src/prompt-cache.ts +40 -0
  169. package/src/requester.ts +21 -0
  170. package/src/run-agent-turn.ts +509 -0
  171. package/src/run-child.ts +344 -0
  172. package/src/run-conversation-turn.ts +272 -0
  173. package/src/run-credential.ts +55 -0
  174. package/src/run-environment.ts +1216 -0
  175. package/src/run-fate.ts +51 -0
  176. package/src/run-files-lock.ts +118 -0
  177. package/src/run-sandbox.ts +63 -0
  178. package/src/run-subagent.ts +433 -0
  179. package/src/run-workflow-agent-node.ts +344 -0
  180. package/src/sandbox-api-context.ts +237 -0
  181. package/src/sandbox-file-registry.ts +26 -0
  182. package/src/sandbox-files.ts +326 -0
  183. package/src/sandbox-handle.ts +19 -0
  184. package/src/sandbox-readiness.ts +22 -0
  185. package/src/skills/built-in.ts +183 -0
  186. package/src/skills/sixb-artifacts/SKILL.md +95 -0
  187. package/src/skills/sixb-artifacts/canvas.md +104 -0
  188. package/src/skills/sixb-artifacts/document.md +63 -0
  189. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/first-job.html +42 -0
  190. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/region.html +29 -0
  191. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/shared.css +35 -0
  192. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/welcome.html +31 -0
  193. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/canvas.json +107 -0
  194. package/src/skills/sixb-artifacts/examples/canvas/retro/canvas.json +158 -0
  195. package/src/skills/sixb-artifacts/examples/document/incident-report/document.md +35 -0
  196. package/src/skills/sixb-artifacts/examples/document/operations-review/document.md +41 -0
  197. package/src/skills/sixb-artifacts/examples/document/operations-review/figures/restorations.svg +1 -0
  198. package/src/skills/sixb-artifacts/examples/page/collections-desk/index.html +169 -0
  199. package/src/skills/sixb-artifacts/examples/page/collections-desk/queries/overdue.json +13 -0
  200. package/src/skills/sixb-artifacts/examples/page/dispatch-board/data/dispatch.json +234 -0
  201. package/src/skills/sixb-artifacts/examples/page/dispatch-board/index.html +245 -0
  202. package/src/skills/sixb-artifacts/examples/page/fleet-planner/data/plan.json +53 -0
  203. package/src/skills/sixb-artifacts/examples/page/fleet-planner/index.html +462 -0
  204. package/src/skills/sixb-artifacts/examples/page/service-explorer/data/service.json +276 -0
  205. package/src/skills/sixb-artifacts/examples/page/service-explorer/index.html +549 -0
  206. package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/queries/overdue.json +13 -0
  207. package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/workbook.json +30 -0
  208. package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Inventory.csv +13 -0
  209. package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Spend.csv +6 -0
  210. package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/workbook.json +47 -0
  211. package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Quarters.csv +5 -0
  212. package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Regions.csv +5 -0
  213. package/src/skills/sixb-artifacts/examples/sheet/service-metrics/workbook.json +78 -0
  214. package/src/skills/sixb-artifacts/examples/slides/launch-plan/index.html +63 -0
  215. package/src/skills/sixb-artifacts/examples/slides/q3-review/index.html +133 -0
  216. package/src/skills/sixb-artifacts/file.md +17 -0
  217. package/src/skills/sixb-artifacts/page.md +215 -0
  218. package/src/skills/sixb-artifacts/queries.md +66 -0
  219. package/src/skills/sixb-artifacts/references.md +68 -0
  220. package/src/skills/sixb-artifacts/sheet.md +223 -0
  221. package/src/skills/sixb-artifacts/slides.md +137 -0
  222. package/src/skills/sixb-memory/SKILL.md +68 -0
  223. package/src/skills/text-assets.d.ts +25 -0
  224. package/src/subagent-context.ts +182 -0
  225. package/src/subagent-continuation.ts +56 -0
  226. package/src/subagent-inbox.ts +101 -0
  227. package/src/subagent-tools.ts +1671 -0
  228. package/src/thread-context.ts +31 -0
  229. package/src/tools/attachments.ts +28 -0
  230. package/src/tools/bash.ts +233 -0
  231. package/src/tools/model-spec.ts +27 -0
  232. package/src/tools/read.ts +262 -0
  233. package/src/tools/result-media.ts +351 -0
  234. package/src/tools/result-output.ts +46 -0
  235. package/src/tools/tool-files.ts +204 -0
  236. package/src/tools/tool-outputs.ts +464 -0
  237. package/src/tools/view-file.ts +155 -0
  238. package/src/turn-runtime.ts +33 -0
  239. package/src/types.ts +91 -0
  240. package/src/workflow-input-attachments.ts +140 -0
@@ -0,0 +1,45 @@
1
+ import { AGENT_MEMORY_ENTRY, AGENT_MEMORY_STARTER } from "@sixb/cli-core"
2
+ import type { AgentMemoryRepoView } from "@sixb/core"
3
+ import type { AgentRunSession } from "@sixb/core/internal/agents"
4
+ import type { AgentMemoryPromptContext } from "./agent-prompt"
5
+
6
+ /** The Agent's memory for one conversational run. */
7
+ export interface ConversationMemory {
8
+ /** Installed under `$SIXB_MEMORY_DIR` at their latest revision, for the Agent to read and save. */
9
+ readonly repos: readonly AgentMemoryRepoView[]
10
+ /** Each repo's `MEMORY.md` as of the time the prompt is frozen at. */
11
+ readonly prompt: AgentMemoryPromptContext
12
+ }
13
+
14
+ /**
15
+ * The repos the run's requester can read, through its session. The prompt shows each `MEMORY.md` as
16
+ * it was at `asOf`, the start of the thread or its latest compaction, so a save leaves the prompt
17
+ * and its cache as they are until the context is next compacted; the installed files are the
18
+ * latest.
19
+ */
20
+ export async function loadConversationMemory(input: {
21
+ readonly session: Pick<AgentRunSession, "memory">
22
+ readonly asOf: Date
23
+ }): Promise<ConversationMemory | undefined> {
24
+ const { shared, repos: loaded } = await input.session.memory({ asOf: input.asOf })
25
+ if (loaded.length === 0) return undefined
26
+
27
+ return {
28
+ repos: loaded.map(({ latest }) => latest),
29
+ prompt: {
30
+ ...(shared ? { shared } : {}),
31
+ repos: loaded.map(({ latest, then }) => ({
32
+ repo: latest.repo,
33
+ // A version pruned from the history since is shown as it is now.
34
+ entry:
35
+ entryOf(then) ??
36
+ (then.revision === 0 ? undefined : entryOf(latest)) ??
37
+ AGENT_MEMORY_STARTER,
38
+ })),
39
+ },
40
+ }
41
+ }
42
+
43
+ function entryOf(repo: AgentMemoryRepoView): string | undefined {
44
+ return repo.files.find((file) => file.path === AGENT_MEMORY_ENTRY)?.contents
45
+ }
@@ -0,0 +1,86 @@
1
+ import { toModelMessages } from "@sixb/core/internal/agents"
2
+ import type { ModelCachingIntent, ModelMessage, ModelReasoning } from "@sixb/core/models"
3
+ import type { SentSettings } from "./agent-prompt"
4
+ import {
5
+ attachmentKey,
6
+ type PreparedAgentAttachmentContext,
7
+ toolResultAttachmentKey,
8
+ } from "./attachments"
9
+ import { type ConversationReaders, conversationAuthors } from "./message-authors"
10
+ import { userMessageTimes } from "./message-times"
11
+ import { conversationPromptCaching } from "./prompt-cache"
12
+ import type { LoadedAgentThreadModelContext } from "./thread-context"
13
+ import { toolFilesStepKey } from "./tools/result-media"
14
+
15
+ /**
16
+ * A conversation's history as its model calls send it: each user message closed by when it was
17
+ * sent and who wrote it, its attachments inline, and the files each step's tools showed. Every
18
+ * part comes from stored data, so a turn, and the summary that continues it, send the bytes the
19
+ * turn before sent.
20
+ */
21
+ export function conversationModelMessages(input: {
22
+ readonly threadContext: LoadedAgentThreadModelContext
23
+ readonly attachmentContext?: PreparedAgentAttachmentContext
24
+ /** The thread and who can read it; without it the model is told nothing of its people. */
25
+ readonly conversation?: ConversationReaders
26
+ /** The project's time zone and locale, for a message whose run captured none. */
27
+ readonly project: SentSettings
28
+ }): ModelMessage[] {
29
+ const { threadContext, attachmentContext } = input
30
+ const sentTimes = userMessageTimes(threadContext.history, input.project)
31
+ const authors = input.conversation
32
+ ? conversationAuthors({
33
+ conversation: input.conversation,
34
+ messages: threadContext.retainedMessages,
35
+ })
36
+ : undefined
37
+
38
+ return toModelMessages(threadContext.modelMessages, {
39
+ // When, then who wrote it: `<sixb_author>` stays the message's last tag. Who takes part is in
40
+ // the system prompt, so no message changes when the next one comes.
41
+ userMessageSuffix: (message) => {
42
+ if (!message.id) return undefined
43
+
44
+ const closing = [sentTimes.get(message.id), authors?.byMessageId.get(message.id)]
45
+ return closing.filter(Boolean).join("\n") || undefined
46
+ },
47
+ fileText: ({ message, partIndex }) =>
48
+ message.id
49
+ ? attachmentContext?.promptTextByPartKey.get(attachmentKey(message.id, partIndex))
50
+ : undefined,
51
+ fileData: ({ message, partIndex }) =>
52
+ message.id
53
+ ? attachmentContext?.modelFileDataByPartKey.get(attachmentKey(message.id, partIndex))
54
+ : undefined,
55
+ toolResultFileText: ({ message, partIndex, contentIndex }) =>
56
+ message.id
57
+ ? attachmentContext?.promptTextByPartKey.get(
58
+ toolResultAttachmentKey(message.id, partIndex, contentIndex)
59
+ )
60
+ : undefined,
61
+ afterToolResults: ({ message, partIndex }) =>
62
+ message.id
63
+ ? attachmentContext?.toolFilesByStep?.get(toolFilesStepKey(message.id, partIndex))
64
+ : undefined,
65
+ })
66
+ }
67
+
68
+ /**
69
+ * How a conversation's model calls reason and are cached. Thinking, effort and the cache intent
70
+ * are part of the prefix a provider reuses, so every call that continues the conversation sends
71
+ * the same: the turn's steps, and the summary that compacts it.
72
+ */
73
+ export function conversationModelSettings(input: {
74
+ readonly reasoning: ModelReasoning | undefined
75
+ readonly projectId: string
76
+ readonly threadId: string
77
+ }): { readonly reasoning?: ModelReasoning; readonly caching: ModelCachingIntent } {
78
+ return {
79
+ ...(input.reasoning === undefined ? {} : { reasoning: input.reasoning }),
80
+ caching: conversationPromptCaching({
81
+ projectId: input.projectId,
82
+ threadId: input.threadId,
83
+ lifetime: "long",
84
+ }),
85
+ }
86
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,85 @@
1
+ import { AgentToolPublicError } from "@sixb/core"
2
+ import type { AgentCompactionFailureCode } from "@sixb/core/agents/streams"
3
+ import { AgentExecutionLostError } from "@sixb/core/internal/agents"
4
+ import { QueueDeliveryLeaseLostError } from "@sixb/core/internal/workers"
5
+
6
+ /** A thread could not complete its required preflight compaction. */
7
+ export class AgentContextCompactionError extends Error {
8
+ readonly name = "AgentContextCompactionError"
9
+
10
+ constructor(
11
+ readonly code: AgentCompactionFailureCode,
12
+ readonly runId: string,
13
+ message: string,
14
+ options?: ErrorOptions
15
+ ) {
16
+ super(`[SixbAgent] ${message}`, options)
17
+ }
18
+ }
19
+
20
+ /**
21
+ * Whoever asked for a queued turn no longer owns or takes part in its thread: they left it, or its
22
+ * owner removed them. The turn ends cancelled without reaching the model.
23
+ */
24
+ export class AgentRequesterLeftThreadError extends Error {
25
+ readonly name = "AgentRequesterLeftThreadError"
26
+
27
+ constructor(readonly runId: string) {
28
+ super(
29
+ `[SixbAgent] Agent run '${runId}' was cancelled: whoever asked for it no longer takes part in its thread.`
30
+ )
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Whether this delivery lost its run: its execution token, or its queue lease, which `signal` (the
36
+ * delivery's) may carry. The current owner records the run's fate; this delivery touches nothing.
37
+ */
38
+ export function isLostExecution(error: unknown, signal?: AbortSignal): boolean {
39
+ return isLostExecutionReason(error) || isLostExecutionReason(signal?.reason)
40
+ }
41
+
42
+ /**
43
+ * Throw why a runner lost its run, if that is why `signal` aborted: its execution token went to
44
+ * another runner, or a queue took back its delivery.
45
+ */
46
+ export function throwIfExecutionLost(signal: AbortSignal): void {
47
+ if (isLostExecutionReason(signal.reason)) throw signal.reason
48
+ }
49
+
50
+ function isLostExecutionReason(reason: unknown): boolean {
51
+ return reason instanceof AgentExecutionLostError || reason instanceof QueueDeliveryLeaseLostError
52
+ }
53
+
54
+ /** Keep an untrusted tool failure as the cause while exposing only a generic message to the model. */
55
+ export class AgentToolExecutionError extends Error {
56
+ readonly name = "AgentToolExecutionError"
57
+
58
+ constructor(
59
+ readonly toolName: string,
60
+ options: ErrorOptions
61
+ ) {
62
+ super("An error occurred.", options)
63
+ }
64
+ }
65
+
66
+ /** A selected agent tool returned a value that cannot cross the durable message boundary. */
67
+ export class AgentToolOutputError extends AgentToolPublicError {
68
+ override readonly name = "AgentToolOutputError"
69
+ constructor(
70
+ readonly toolName: string,
71
+ reason: string,
72
+ options?: ErrorOptions
73
+ ) {
74
+ super(`[SixbAgent] Agent tool '${toolName}' returned an invalid result; ${reason}.`, options)
75
+ }
76
+ }
77
+
78
+ /** Failed preservation is a failure even when the run was also cancelled. */
79
+ export class AgentEnvironmentSaveError extends Error {
80
+ readonly name = "AgentEnvironmentSaveError"
81
+
82
+ constructor() {
83
+ super("[SixbAgent] Sandbox save or cleanup could not be confirmed. Recovery is required.")
84
+ }
85
+ }
@@ -0,0 +1,70 @@
1
+ import type { AgentReasoning, AgentSkillDefinition } from "@sixb/core"
2
+ import type {
3
+ AgentContextTokenRateSample,
4
+ AgentRunPlan,
5
+ AgentToolSpec,
6
+ SandboxModel,
7
+ } from "@sixb/core/internal/agents"
8
+ import type { RenderedOntologyDocs } from "@sixb/core/internal/ontology"
9
+ import type { LanguageModel } from "@sixb/core/models"
10
+ import { withBuiltInSkills } from "./skills/built-in"
11
+
12
+ /**
13
+ * Fully resolved inputs shared by the agent execution engines.
14
+ *
15
+ * This internal runtime value is neither a durable run nor a public API contract. Source-specific
16
+ * adapters resolve it before entering the shared execution path.
17
+ */
18
+ export interface ResolvedAgentExecutionPlan {
19
+ readonly model: LanguageModel
20
+ readonly reasoning?: AgentReasoning
21
+ readonly instructions?: string
22
+ /** The project tools it offers, as the model is offered them; they run where the project is. */
23
+ readonly tools: readonly AgentToolSpec[]
24
+ /** Agent Skills installed in the sandbox and listed in the prompt. */
25
+ readonly skills: readonly AgentSkillDefinition[]
26
+ /** The requester's time zone and locale, when the run captured them. */
27
+ readonly timeZone?: string
28
+ readonly locale?: string
29
+ /** Models the sandbox CLI can call beside the agent's own, listed in the prompt. */
30
+ readonly sandboxModels?: readonly SandboxModel[]
31
+ /** Reference docs for the object types the run's requester can see, from their own view. */
32
+ readonly ontologyDocs: RenderedOntologyDocs
33
+ /** What the project's runs measured of the model's tokenizer lately, newest first. */
34
+ readonly tokenRates?: readonly AgentContextTokenRateSample[]
35
+ /** At most this many model calls. Absent: it runs until its model answers. */
36
+ readonly maxSteps?: number
37
+ }
38
+
39
+ /**
40
+ * A turn's or a child's plan, with the session's model, which records each call itself, and the
41
+ * harness's skills added. Its project tools run through the session.
42
+ */
43
+ export function resolveRunPlan(
44
+ plan: AgentRunPlan,
45
+ resolved: { readonly model: LanguageModel }
46
+ ): ResolvedAgentExecutionPlan {
47
+ const {
48
+ reasoning,
49
+ instructions,
50
+ timeZone,
51
+ locale,
52
+ sandboxModels,
53
+ ontologyDocs,
54
+ tools,
55
+ maxSteps,
56
+ } = plan
57
+ return Object.freeze({
58
+ model: resolved.model,
59
+ ontologyDocs,
60
+ tokenRates: plan.tokenRates,
61
+ ...(instructions === undefined ? {} : { instructions }),
62
+ tools,
63
+ skills: withBuiltInSkills(plan.skills),
64
+ ...(reasoning === undefined ? {} : { reasoning }),
65
+ ...(timeZone === undefined ? {} : { timeZone }),
66
+ ...(locale === undefined ? {} : { locale }),
67
+ ...(sandboxModels.length ? { sandboxModels } : {}),
68
+ ...(maxSteps === undefined ? {} : { maxSteps }),
69
+ })
70
+ }
package/src/failure.ts ADDED
@@ -0,0 +1,82 @@
1
+ import type { SixbFailure } from "@sixb/core"
2
+ import {
3
+ captureSixbFailure,
4
+ createSixbError,
5
+ isSixbError,
6
+ summarizeErrorMessage,
7
+ } from "@sixb/core/internal/errors"
8
+ import { AGENT_RUN_FAILURE_CODES, type AgentRunFailureCode } from "@sixb/core/storage"
9
+ import { AgentRuntimeProfileError } from "./agent-runtime/errors"
10
+ import { AgentContextCompactionError } from "./errors"
11
+
12
+ export type AgentRunFailure = SixbFailure<AgentRunFailureCode>
13
+
14
+ interface AgentFailureInput {
15
+ readonly status: "failed" | "cancelled"
16
+ readonly at: Date
17
+ readonly details: Readonly<Record<string, string>>
18
+ }
19
+
20
+ /** Normalize a failure without changing its classification. Used outside active execution. */
21
+ export function toAgentRunFailure(error: unknown, input: AgentFailureInput): AgentRunFailure {
22
+ const failureCode = input.status === "cancelled" ? "runtime.cancelled" : "internal.unexpected"
23
+ return captureSixbFailure(error, {
24
+ allowedCodes: AGENT_RUN_FAILURE_CODES,
25
+ defaultCode: failureCode,
26
+ details: input.details,
27
+ at: input.at,
28
+ })
29
+ }
30
+
31
+ /** Classify only work performed by an active Agent execution. */
32
+ export function toAgentExecutionFailure(error: unknown, input: AgentFailureInput): AgentRunFailure {
33
+ const failureError =
34
+ input.status === "failed" ? translateAgentExecutionError(error, input.details) : error
35
+ return toAgentRunFailure(failureError, input)
36
+ }
37
+
38
+ function translateAgentExecutionError(
39
+ error: unknown,
40
+ details: Readonly<Record<string, string>>
41
+ ): unknown {
42
+ if (error instanceof AgentRuntimeProfileError) {
43
+ return createSixbError("agent.execution_failed", error.message, {
44
+ cause: error,
45
+ details: {
46
+ ...details,
47
+ provider: error.provider,
48
+ runtimeProfile: error.profile,
49
+ runtimeCheck: error.check,
50
+ runtimeFailure: error.reason,
51
+ ...(error.exitCode === undefined ? {} : { runtimeExitCode: String(error.exitCode) }),
52
+ remediation: error.remediation,
53
+ },
54
+ })
55
+ }
56
+
57
+ // Compaction found no way to fit the conversation in the model's window: a retry would fail the
58
+ // same way, so the failure says what gets past it.
59
+ if (error instanceof AgentContextCompactionError && error.code === "context_limit_exceeded") {
60
+ return createSixbError("agent.context_limit_exceeded", error.message, {
61
+ cause: error,
62
+ details,
63
+ })
64
+ }
65
+
66
+ if (
67
+ isSixbError(error) &&
68
+ (error.code === "internal.unexpected" ||
69
+ error.code === "agent.execution_failed" ||
70
+ error.code === "agent.step_limit_reached" ||
71
+ error.code === "ai.usage_limit_exceeded" ||
72
+ error.code === "ai.usage_limit_unavailable")
73
+ ) {
74
+ return error
75
+ }
76
+
77
+ return createSixbError(
78
+ "agent.execution_failed",
79
+ summarizeErrorMessage(error, "Agent execution failed."),
80
+ { cause: error, details }
81
+ )
82
+ }
@@ -0,0 +1,189 @@
1
+ import { waitForAbort } from "./abort"
2
+ import type { ChildQuestion, QuestionRef } from "./child-questions"
3
+
4
+ // A turn and the children it runs share a runner, wherever it runs: the worker, or a computer. So
5
+ // what they say to each other never leaves it. A child's answer and its files reach the turn
6
+ // through the child's run; what it never read when it paused, through its run's result.
7
+
8
+ /** What a turn sends one of its children: a message, or a reply to its question. */
9
+ export interface ChildMail {
10
+ /** Names the turn's tool call, so a replay of the call sends nothing twice. */
11
+ readonly id: string
12
+ readonly kind: "message" | "answer" | "deferred"
13
+ readonly text: string
14
+ /** The question a reply answers. */
15
+ readonly inReplyTo?: string
16
+ }
17
+
18
+ /** A message a child had not read when it stopped. */
19
+ export type UnreadChildMail = Pick<ChildMail, "id" | "text">
20
+
21
+ /** The child stopped reading: it paused or ended, so mail sent to it now would never be read. */
22
+ export class ChildMailClosedError extends Error {
23
+ readonly name = "ChildMailClosedError"
24
+ constructor(readonly childRunId: string) {
25
+ super(`[SixbAgent] Subagent run '${childRunId}' reads no more mail.`)
26
+ }
27
+ }
28
+
29
+ /** One child's side of its turn's mailbox. */
30
+ export interface ChildMailbox {
31
+ /** The messages sent since the last take, in the order they were sent. */
32
+ take(): readonly UnreadChildMail[]
33
+ /** Ask the turn, and wait for its reply: an answer, or `deferred` when the turn ends first. */
34
+ ask(question: QuestionRef, signal: AbortSignal): Promise<ChildMail>
35
+ /** Stop reading: what came and was never taken is returned, and later mail is refused. */
36
+ close(): readonly UnreadChildMail[]
37
+ }
38
+
39
+ /**
40
+ * The mail between a turn and the children it runs, held in their runner, one per turn. The turn
41
+ * sends its children messages and replies, and hears their questions and their ends; each child
42
+ * takes its messages at its step boundaries and asks its questions.
43
+ */
44
+ export class FamilyMailbox {
45
+ readonly #children = new Map<string, ChildBox>()
46
+ /** The questions no reply settled yet, by the child that asked: one at a time each. */
47
+ readonly #questions = new Map<string, ChildQuestion>()
48
+ #wake: { readonly promise: Promise<void>; readonly resolve: () => void } | undefined
49
+ #closed = false
50
+
51
+ /**
52
+ * Send a child mail. Sent twice under one id, it arrives once. Throws
53
+ * {@link ChildMailClosedError} once the child stopped reading, or the turn ended.
54
+ */
55
+ send(childRunId: string, mail: ChildMail): void {
56
+ if (this.#closed) throw new ChildMailClosedError(childRunId)
57
+ this.#box(childRunId).deliver(mail)
58
+ }
59
+
60
+ /** The child's side, for its runner: mail sent before it started is there already. */
61
+ child(childRunId: string): ChildMailbox {
62
+ return this.#box(childRunId)
63
+ }
64
+
65
+ /** The questions the turn's children wait on, in the order they asked. */
66
+ questions(): readonly ChildQuestion[] {
67
+ return [...this.#questions.values()]
68
+ }
69
+
70
+ /** The question `childRunId` waits on, if any. */
71
+ question(childRunId: string): ChildQuestion | undefined {
72
+ return this.#questions.get(childRunId)
73
+ }
74
+
75
+ /** The turn took the question to answer it: no other answer is sent to it. */
76
+ answered(question: ChildQuestion): void {
77
+ if (this.#questions.get(question.runId)?.id === question.id) {
78
+ this.#questions.delete(question.runId)
79
+ }
80
+ }
81
+
82
+ /** Open again a question its answer could not be sent to. */
83
+ reopen(question: ChildQuestion): void {
84
+ if (!this.#questions.has(question.runId)) this.#questions.set(question.runId, question)
85
+ }
86
+
87
+ /** A child's run ended, or paused: its runner says so, and the turn's waits wake. */
88
+ finished(childRunId: string): void {
89
+ this.#children.get(childRunId)?.close()
90
+ this.#questions.delete(childRunId)
91
+ this.#changed()
92
+ }
93
+
94
+ /**
95
+ * Settles when a child asks or finishes. Take it before reading what it wakes for, so what
96
+ * comes during the read wakes the wait.
97
+ */
98
+ changed(): Promise<void> {
99
+ if (!this.#wake) {
100
+ let resolve!: () => void
101
+ const promise = new Promise<void>((settle) => {
102
+ resolve = settle
103
+ })
104
+ this.#wake = { promise, resolve }
105
+ }
106
+ return this.#wake.promise
107
+ }
108
+
109
+ /** The turn ended: it sends nothing more. */
110
+ close(): void {
111
+ this.#closed = true
112
+ }
113
+
114
+ #box(childRunId: string): ChildBox {
115
+ let box = this.#children.get(childRunId)
116
+ if (!box) {
117
+ box = new ChildBox(childRunId, {
118
+ asked: (question) => {
119
+ this.#questions.set(childRunId, { ...question, runId: childRunId })
120
+ this.#changed()
121
+ },
122
+ withdrawn: (question) => {
123
+ if (this.#questions.get(childRunId)?.id === question.id) {
124
+ this.#questions.delete(childRunId)
125
+ }
126
+ },
127
+ })
128
+ this.#children.set(childRunId, box)
129
+ }
130
+ return box
131
+ }
132
+
133
+ #changed(): void {
134
+ this.#wake?.resolve()
135
+ this.#wake = undefined
136
+ }
137
+ }
138
+
139
+ class ChildBox implements ChildMailbox {
140
+ readonly #seen = new Set<string>()
141
+ #unread: UnreadChildMail[] = []
142
+ #asking: { readonly id: string; readonly reply: (mail: ChildMail) => void } | undefined
143
+ #closed = false
144
+
145
+ constructor(
146
+ private readonly runId: string,
147
+ private readonly family: {
148
+ readonly asked: (question: QuestionRef) => void
149
+ readonly withdrawn: (question: QuestionRef) => void
150
+ }
151
+ ) {}
152
+
153
+ deliver(mail: ChildMail): void {
154
+ if (this.#closed) throw new ChildMailClosedError(this.runId)
155
+ if (this.#seen.has(mail.id)) return
156
+ this.#seen.add(mail.id)
157
+
158
+ if (mail.kind === "message") {
159
+ this.#unread.push({ id: mail.id, text: mail.text })
160
+ } else if (this.#asking && mail.inReplyTo === this.#asking.id) {
161
+ this.#asking.reply(mail)
162
+ }
163
+ // A reply to a question the child no longer asks has no one to read it.
164
+ }
165
+
166
+ take(): readonly UnreadChildMail[] {
167
+ const taken = this.#unread
168
+ this.#unread = []
169
+ return taken
170
+ }
171
+
172
+ async ask(question: QuestionRef, signal: AbortSignal): Promise<ChildMail> {
173
+ signal.throwIfAborted()
174
+ const reply = Promise.withResolvers<ChildMail>()
175
+ this.#asking = { id: question.id, reply: reply.resolve }
176
+ this.family.asked(question)
177
+ try {
178
+ return await waitForAbort(reply.promise, signal)
179
+ } finally {
180
+ this.#asking = undefined
181
+ this.family.withdrawn(question)
182
+ }
183
+ }
184
+
185
+ close(): readonly UnreadChildMail[] {
186
+ this.#closed = true
187
+ return this.take()
188
+ }
189
+ }
@@ -0,0 +1,18 @@
1
+ import type { FileRef } from "@sixb/core"
2
+
3
+ /** Content identity used only where byte-for-byte duplicates should collapse. */
4
+ export function fileContentKey(fileRef: FileRef): string {
5
+ return `${fileRef.digest}:${fileRef.sizeBytes}`
6
+ }
7
+
8
+ /** Exact reference identity for metadata-sensitive lookups such as sandbox paths. */
9
+ export function fileRefKey(fileRef: FileRef): string {
10
+ return JSON.stringify([
11
+ fileRef.blobId,
12
+ fileRef.digest,
13
+ fileRef.sizeBytes,
14
+ fileRef.fileName ?? null,
15
+ fileRef.mediaType ?? null,
16
+ fileRef.logicalPath ?? null,
17
+ ])
18
+ }