@sixb/agent 0.0.0-stage → 0.1.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 (219) 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 +120 -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 +68 -0
  22. package/dist/attachments.d.ts.map +1 -0
  23. package/dist/child-files.d.ts +96 -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 +65 -0
  28. package/dist/computer-runner.d.ts.map +1 -0
  29. package/dist/context-budget.d.ts +28 -0
  30. package/dist/context-budget.d.ts.map +1 -0
  31. package/dist/context-compaction.d.ts +48 -0
  32. package/dist/context-compaction.d.ts.map +1 -0
  33. package/dist/conversation-memory.d.ts +21 -0
  34. package/dist/conversation-memory.d.ts.map +1 -0
  35. package/dist/errors.d.ts +46 -0
  36. package/dist/errors.d.ts.map +1 -0
  37. package/dist/execution-plan.d.ts +34 -0
  38. package/dist/execution-plan.d.ts.map +1 -0
  39. package/dist/failure.d.ts +14 -0
  40. package/dist/failure.d.ts.map +1 -0
  41. package/dist/family-mailbox.d.ts +60 -0
  42. package/dist/family-mailbox.d.ts.map +1 -0
  43. package/dist/file-ref.d.ts +6 -0
  44. package/dist/file-ref.d.ts.map +1 -0
  45. package/dist/image-attachments.d.ts +34 -0
  46. package/dist/image-attachments.d.ts.map +1 -0
  47. package/dist/index.d.ts +35 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +12168 -0
  50. package/dist/index.js.map +65 -0
  51. package/dist/message-authors.d.ts +34 -0
  52. package/dist/message-authors.d.ts.map +1 -0
  53. package/dist/message-times.d.ts +8 -0
  54. package/dist/message-times.d.ts.map +1 -0
  55. package/dist/model-adapters.d.ts +55 -0
  56. package/dist/model-adapters.d.ts.map +1 -0
  57. package/dist/model-lines.d.ts +16 -0
  58. package/dist/model-lines.d.ts.map +1 -0
  59. package/dist/requester.d.ts +15 -0
  60. package/dist/requester.d.ts.map +1 -0
  61. package/dist/run-agent-turn.d.ts +63 -0
  62. package/dist/run-agent-turn.d.ts.map +1 -0
  63. package/dist/run-child.d.ts +77 -0
  64. package/dist/run-child.d.ts.map +1 -0
  65. package/dist/run-conversation-turn.d.ts +42 -0
  66. package/dist/run-conversation-turn.d.ts.map +1 -0
  67. package/dist/run-credential.d.ts +22 -0
  68. package/dist/run-credential.d.ts.map +1 -0
  69. package/dist/run-environment.d.ts +115 -0
  70. package/dist/run-environment.d.ts.map +1 -0
  71. package/dist/run-fate.d.ts +15 -0
  72. package/dist/run-fate.d.ts.map +1 -0
  73. package/dist/run-sandbox.d.ts +60 -0
  74. package/dist/run-sandbox.d.ts.map +1 -0
  75. package/dist/run-subagent.d.ts +51 -0
  76. package/dist/run-subagent.d.ts.map +1 -0
  77. package/dist/run-workflow-agent-node.d.ts +41 -0
  78. package/dist/run-workflow-agent-node.d.ts.map +1 -0
  79. package/dist/sandbox-api-context.d.ts +34 -0
  80. package/dist/sandbox-api-context.d.ts.map +1 -0
  81. package/dist/sandbox-file-registry.d.ts +9 -0
  82. package/dist/sandbox-file-registry.d.ts.map +1 -0
  83. package/dist/sandbox-files.d.ts +71 -0
  84. package/dist/sandbox-files.d.ts.map +1 -0
  85. package/dist/sandbox-handle.d.ts +15 -0
  86. package/dist/sandbox-handle.d.ts.map +1 -0
  87. package/dist/sandbox-readiness.d.ts +8 -0
  88. package/dist/sandbox-readiness.d.ts.map +1 -0
  89. package/dist/skills/built-in.d.ts +15 -0
  90. package/dist/skills/built-in.d.ts.map +1 -0
  91. package/dist/subagent-continuation.d.ts +27 -0
  92. package/dist/subagent-continuation.d.ts.map +1 -0
  93. package/dist/subagent-inbox.d.ts +53 -0
  94. package/dist/subagent-inbox.d.ts.map +1 -0
  95. package/dist/subagent-tools.d.ts +282 -0
  96. package/dist/subagent-tools.d.ts.map +1 -0
  97. package/dist/thread-context.d.ts +12 -0
  98. package/dist/thread-context.d.ts.map +1 -0
  99. package/dist/tools/attachments.d.ts +11 -0
  100. package/dist/tools/attachments.d.ts.map +1 -0
  101. package/dist/tools/bash.d.ts +60 -0
  102. package/dist/tools/bash.d.ts.map +1 -0
  103. package/dist/tools/model-spec.d.ts +4 -0
  104. package/dist/tools/model-spec.d.ts.map +1 -0
  105. package/dist/tools/read.d.ts +43 -0
  106. package/dist/tools/read.d.ts.map +1 -0
  107. package/dist/tools/result-media.d.ts +22 -0
  108. package/dist/tools/result-media.d.ts.map +1 -0
  109. package/dist/tools/result-output.d.ts +18 -0
  110. package/dist/tools/result-output.d.ts.map +1 -0
  111. package/dist/tools/tool-files.d.ts +59 -0
  112. package/dist/tools/tool-files.d.ts.map +1 -0
  113. package/dist/tools/view-file.d.ts +40 -0
  114. package/dist/tools/view-file.d.ts.map +1 -0
  115. package/dist/tsconfig.json +5 -0
  116. package/dist/turn-runtime.d.ts +19 -0
  117. package/dist/turn-runtime.d.ts.map +1 -0
  118. package/dist/types.d.ts +76 -0
  119. package/dist/types.d.ts.map +1 -0
  120. package/dist/workflow-input-attachments.d.ts +18 -0
  121. package/dist/workflow-input-attachments.d.ts.map +1 -0
  122. package/package.json +59 -4
  123. package/src/abort.ts +40 -0
  124. package/src/agent-prompt.ts +758 -0
  125. package/src/agent-runtime/errors.ts +44 -0
  126. package/src/agent-runtime/preflight.ts +314 -0
  127. package/src/agent-runtime/profile.ts +23 -0
  128. package/src/artifacts/layout-check/check.cjs +478 -0
  129. package/src/artifacts/layout-check/layout-check.ts +460 -0
  130. package/src/artifacts/materialize.ts +380 -0
  131. package/src/artifacts/publish-tool.ts +700 -0
  132. package/src/ask-parent-tool.ts +100 -0
  133. package/src/attachments.ts +850 -0
  134. package/src/child-files.ts +540 -0
  135. package/src/child-questions.ts +42 -0
  136. package/src/computer-runner.ts +284 -0
  137. package/src/context-budget.ts +161 -0
  138. package/src/context-compaction.ts +529 -0
  139. package/src/conversation-memory.ts +45 -0
  140. package/src/errors.ts +85 -0
  141. package/src/execution-plan.ts +50 -0
  142. package/src/failure.ts +71 -0
  143. package/src/family-mailbox.ts +189 -0
  144. package/src/file-ref.ts +18 -0
  145. package/src/image-attachments.ts +322 -0
  146. package/src/index.ts +54 -0
  147. package/src/message-authors.ts +77 -0
  148. package/src/message-times.ts +21 -0
  149. package/src/model-adapters.ts +227 -0
  150. package/src/model-lines.ts +28 -0
  151. package/src/requester.ts +21 -0
  152. package/src/run-agent-turn.ts +347 -0
  153. package/src/run-child.ts +319 -0
  154. package/src/run-conversation-turn.ts +257 -0
  155. package/src/run-credential.ts +55 -0
  156. package/src/run-environment.ts +769 -0
  157. package/src/run-fate.ts +51 -0
  158. package/src/run-sandbox.ts +63 -0
  159. package/src/run-subagent.ts +347 -0
  160. package/src/run-workflow-agent-node.ts +270 -0
  161. package/src/sandbox-api-context.ts +234 -0
  162. package/src/sandbox-file-registry.ts +26 -0
  163. package/src/sandbox-files.ts +326 -0
  164. package/src/sandbox-handle.ts +19 -0
  165. package/src/sandbox-readiness.ts +22 -0
  166. package/src/skills/built-in.ts +183 -0
  167. package/src/skills/sixb-artifacts/SKILL.md +95 -0
  168. package/src/skills/sixb-artifacts/canvas.md +104 -0
  169. package/src/skills/sixb-artifacts/document.md +63 -0
  170. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/first-job.html +42 -0
  171. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/region.html +29 -0
  172. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/shared.css +35 -0
  173. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/boards/welcome.html +31 -0
  174. package/src/skills/sixb-artifacts/examples/canvas/onboarding-flow/canvas.json +107 -0
  175. package/src/skills/sixb-artifacts/examples/canvas/retro/canvas.json +158 -0
  176. package/src/skills/sixb-artifacts/examples/document/incident-report/document.md +35 -0
  177. package/src/skills/sixb-artifacts/examples/document/operations-review/document.md +41 -0
  178. package/src/skills/sixb-artifacts/examples/document/operations-review/figures/restorations.svg +1 -0
  179. package/src/skills/sixb-artifacts/examples/page/collections-desk/index.html +169 -0
  180. package/src/skills/sixb-artifacts/examples/page/collections-desk/queries/overdue.json +13 -0
  181. package/src/skills/sixb-artifacts/examples/page/dispatch-board/data/dispatch.json +234 -0
  182. package/src/skills/sixb-artifacts/examples/page/dispatch-board/index.html +245 -0
  183. package/src/skills/sixb-artifacts/examples/page/fleet-planner/data/plan.json +53 -0
  184. package/src/skills/sixb-artifacts/examples/page/fleet-planner/index.html +462 -0
  185. package/src/skills/sixb-artifacts/examples/page/service-explorer/data/service.json +276 -0
  186. package/src/skills/sixb-artifacts/examples/page/service-explorer/index.html +549 -0
  187. package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/queries/overdue.json +13 -0
  188. package/src/skills/sixb-artifacts/examples/sheet/overdue-invoices/workbook.json +30 -0
  189. package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Inventory.csv +13 -0
  190. package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/sheets/Spend.csv +6 -0
  191. package/src/skills/sixb-artifacts/examples/sheet/parts-inventory/workbook.json +47 -0
  192. package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Quarters.csv +5 -0
  193. package/src/skills/sixb-artifacts/examples/sheet/service-metrics/sheets/Regions.csv +5 -0
  194. package/src/skills/sixb-artifacts/examples/sheet/service-metrics/workbook.json +78 -0
  195. package/src/skills/sixb-artifacts/examples/slides/launch-plan/index.html +63 -0
  196. package/src/skills/sixb-artifacts/examples/slides/q3-review/index.html +133 -0
  197. package/src/skills/sixb-artifacts/file.md +17 -0
  198. package/src/skills/sixb-artifacts/page.md +215 -0
  199. package/src/skills/sixb-artifacts/queries.md +66 -0
  200. package/src/skills/sixb-artifacts/references.md +68 -0
  201. package/src/skills/sixb-artifacts/sheet.md +223 -0
  202. package/src/skills/sixb-artifacts/slides.md +137 -0
  203. package/src/skills/sixb-memory/SKILL.md +68 -0
  204. package/src/skills/text-assets.d.ts +25 -0
  205. package/src/subagent-continuation.ts +51 -0
  206. package/src/subagent-inbox.ts +101 -0
  207. package/src/subagent-tools.ts +1609 -0
  208. package/src/thread-context.ts +25 -0
  209. package/src/tools/attachments.ts +28 -0
  210. package/src/tools/bash.ts +217 -0
  211. package/src/tools/model-spec.ts +22 -0
  212. package/src/tools/read.ts +262 -0
  213. package/src/tools/result-media.ts +147 -0
  214. package/src/tools/result-output.ts +44 -0
  215. package/src/tools/tool-files.ts +204 -0
  216. package/src/tools/view-file.ts +155 -0
  217. package/src/turn-runtime.ts +33 -0
  218. package/src/types.ts +79 -0
  219. package/src/workflow-input-attachments.ts +140 -0
@@ -0,0 +1,257 @@
1
+ import { principalsEqual } from "@sixb/core"
2
+ import {
3
+ AgentExecutionLostError,
4
+ AgentFinalizationError,
5
+ type AgentRunSession,
6
+ AgentTurnInterruptedError,
7
+ } from "@sixb/core/internal/agents"
8
+ import { createSixbError } from "@sixb/core/internal/errors"
9
+ import { ModelUsageRecordingError } from "@sixb/core/internal/model-execution"
10
+ import { isAbortError } from "@sixb/core/internal/workers"
11
+ import type {
12
+ AgentRunRecord,
13
+ AgentThreadParticipantRecord,
14
+ AgentThreadRecord,
15
+ ConversationAgentRunRecord,
16
+ ExecutionRecord,
17
+ SubagentRunRecord,
18
+ } from "@sixb/core/storage"
19
+ import { prepareAgentModel } from "./context-budget"
20
+ import { AgentEnvironmentSaveError, AgentRequesterLeftThreadError, isLostExecution } from "./errors"
21
+ import { resolveRunPlan } from "./execution-plan"
22
+ import type { AgentRunFailure } from "./failure"
23
+ import type { FamilyMailbox } from "./family-mailbox"
24
+ import { agentRequester } from "./requester"
25
+ import { runAgentTurn, stoppedByWorker } from "./run-agent-turn"
26
+ import {
27
+ type ConversationAgentExecutionEnvironment,
28
+ createConversationAgentEnvironment,
29
+ type OpenRunSandbox,
30
+ } from "./run-environment"
31
+ import { recordFate } from "./run-fate"
32
+ import { SubagentCoordinator } from "./subagent-tools"
33
+ import { type AgentTurnRuntime, createAgentTurnRuntime } from "./turn-runtime"
34
+ import type { AgentExecutionContext, AgentRunSettings } from "./types"
35
+
36
+ // A conversation turn, from its claimed session to its end, whichever runner runs it: the worker,
37
+ // under a queue delivery, or a runner on a computer, over HTTP. The runner keeps what is its own:
38
+ // how it holds the run, where the turn's sandbox comes from, and where its children run.
39
+
40
+ /** What a runner lends the turns it runs. */
41
+ export interface ConversationTurnRunner {
42
+ /** What every run of the project shares. */
43
+ readonly settings: AgentRunSettings
44
+ /** The sandbox the turn works in, opened when it first needs one. */
45
+ openSandbox(turn: {
46
+ readonly session: AgentRunSession<ConversationAgentRunRecord>
47
+ readonly context: AgentExecutionContext
48
+ readonly execution: ExecutionRecord
49
+ readonly thread: AgentThreadRecord | null
50
+ }): OpenRunSandbox
51
+ /** Run a child the turn spawned or continues, beside the turn, on the turn's mailbox. */
52
+ startChild(run: SubagentRunRecord, family: FamilyMailbox): void
53
+ /** A sandbox teardown that outlives the turn: the runner waits for it before it stops. */
54
+ onDetachedTeardown(teardown: Promise<void>): void
55
+ /** A turn that failed: the runner reports it, once its end is recorded. */
56
+ reportFailure?(error: unknown, run: AgentRunRecord, failure: AgentRunFailure): void
57
+ /**
58
+ * Runs the turn's preparation, which reads and resolves and calls no model: the worker retries a
59
+ * storage blip in place. Runs it once when absent.
60
+ */
61
+ prepare?<T>(prepare: () => Promise<T>): Promise<T>
62
+ }
63
+
64
+ /**
65
+ * Run a claimed conversation turn and record how it ended. Returns once its end is recorded, or
66
+ * at once when its runner lost the run. Throws {@link AgentFinalizationError} when its end could
67
+ * not be recorded, and rethrows its failure when `signal` stopped it, for the runner to release
68
+ * the run rather than call it done.
69
+ */
70
+ export async function runConversationTurn(input: {
71
+ readonly session: AgentRunSession<ConversationAgentRunRecord>
72
+ readonly runner: ConversationTurnRunner
73
+ /** The runner's own stop: its process stopping, or its hold on the run lost. */
74
+ readonly signal: AbortSignal
75
+ }): Promise<void> {
76
+ const { session, runner, signal } = input
77
+ const { run } = session
78
+ let environment: ConversationAgentExecutionEnvironment | null = null
79
+ let runtime: AgentTurnRuntime | null = null
80
+ let delegation: SubagentCoordinator | undefined
81
+ // A user cancel joins the turn's abort sources, so it stops the model stream as a shutdown does.
82
+ const turnSignal = AbortSignal.any([signal, session.cancelled])
83
+
84
+ try {
85
+ const prepare = () => prepareConversationTurn(session, runner.settings)
86
+ const prepared = await (runner.prepare ? runner.prepare(prepare) : prepare())
87
+ const plan = Object.freeze({ ...prepared.plan, model: prepared.model.model })
88
+
89
+ await session.events.publishStarted(run)
90
+ runtime = createAgentTurnRuntime({ signal: turnSignal })
91
+ delegation = new SubagentCoordinator(
92
+ prepared.delegation,
93
+ runner.settings,
94
+ run,
95
+ session,
96
+ (child, family) => runner.startChild(child, family)
97
+ )
98
+ const frameworkTools = await delegation.createTools()
99
+ environment = await createConversationAgentEnvironment({
100
+ session,
101
+ frameworkTools,
102
+ thread: prepared.thread,
103
+ ...(prepared.execution.requestedBy === undefined
104
+ ? {}
105
+ : { requestedBy: prepared.execution.requestedBy }),
106
+ ...(prepared.conversation === undefined ? {} : { conversation: prepared.conversation }),
107
+ shared: prepared.shared,
108
+ openSandbox: runner.openSandbox({
109
+ session,
110
+ context: prepared.context,
111
+ execution: prepared.execution,
112
+ thread: prepared.thread,
113
+ }),
114
+ preflight: { budget: prepared.model.budget, runtime },
115
+ context: prepared.context,
116
+ plan,
117
+ run,
118
+ history: prepared.history,
119
+ signal: runtime.signal,
120
+ onDetachedTeardown: (teardown) => runner.onDetachedTeardown(teardown),
121
+ })
122
+ delegation.attach(environment.runFiles)
123
+ // A resumed turn's paused children go on first, as the model reads that they do.
124
+ await delegation.continuePausedChildren(runtime.signal)
125
+ runtime.assertCanContinue()
126
+ await runAgentTurn({
127
+ context: environment.turnContext,
128
+ plan,
129
+ run,
130
+ session,
131
+ ...(prepared.conversation === undefined ? {} : { conversation: prepared.conversation }),
132
+ signal: turnSignal,
133
+ stopSignal: signal,
134
+ runtime,
135
+ threadContext: environment.threadContext,
136
+ children: await delegation.turnChildren(),
137
+ })
138
+ } catch (caughtError) {
139
+ let error = caughtError
140
+ let environmentSaveFailed = error instanceof AgentEnvironmentSaveError
141
+ // Its runner lost the run: touch nothing; whoever holds it now ends it.
142
+ if (isLostExecution(error, signal)) return
143
+ // It ended, but its end could not be recorded (storage down): its runner tries again.
144
+ if (error instanceof AgentFinalizationError) throw error
145
+ if (environment) {
146
+ try {
147
+ await environment.beforeFinalize()
148
+ } catch (saveError) {
149
+ if (saveError instanceof AgentExecutionLostError) return
150
+ error = saveError
151
+ environmentSaveFailed = true
152
+ }
153
+ }
154
+ // A user cancel is read off its own signal, so it records `cancelled` however the aborted
155
+ // stream surfaced. `recordFate` retries a blip, and throws AgentFinalizationError past it.
156
+ const aborted =
157
+ error instanceof AgentRequesterLeftThreadError ||
158
+ (!environmentSaveFailed &&
159
+ !(error instanceof ModelUsageRecordingError) &&
160
+ (signal.aborted || session.cancelled.aborted || isAbortError(error)))
161
+ // A runner that stops, unlike a user cancel, leaves the user something to resume.
162
+ const interrupted = aborted && stoppedByWorker(turnSignal, signal)
163
+ const finalized = await recordFate(
164
+ session,
165
+ aborted ? "cancelled" : "failed",
166
+ interrupted ? new AgentTurnInterruptedError(run.id) : error
167
+ )
168
+ if (finalized) {
169
+ if (finalized.run.status === "failed") {
170
+ runner.reportFailure?.(error, finalized.run, finalized.failure)
171
+ }
172
+ await session.events.publishRunFinished(finalized.run)
173
+ }
174
+ // Its runner stopped: it releases the run rather than calls it done.
175
+ if (signal.aborted) throw error
176
+ } finally {
177
+ delegation?.close()
178
+ runtime?.dispose()
179
+ await environment?.dispose()
180
+ }
181
+ }
182
+
183
+ /** Everything a turn needs before its first model call, read through its session. */
184
+ async function prepareConversationTurn(
185
+ session: AgentRunSession<ConversationAgentRunRecord>,
186
+ settings: AgentRunSettings
187
+ ) {
188
+ const { run } = session
189
+ const turn = await session.context()
190
+ const { thread, execution, participants } = turn
191
+ const runPlan = await session.plan()
192
+ const plan = resolveRunPlan(runPlan, { model: await session.models.language(runPlan.model) })
193
+ // A turn's plan always carries it; only a child's goes without.
194
+ if (!runPlan.delegation) {
195
+ throw createSixbError(
196
+ "internal.unexpected",
197
+ `[SixbAgent] Agent run '${run.id}' has no models to delegate to.`
198
+ )
199
+ }
200
+ if (!execution) {
201
+ throw createSixbError(
202
+ "internal.unexpected",
203
+ `[SixbAgent] Agent run '${run.id}' references missing execution '${run.executionId}'.`,
204
+ { details: { runId: run.id, threadId: run.threadId, executionId: run.executionId } }
205
+ )
206
+ }
207
+ assertRequesterTakesPart(run, thread, participants, execution)
208
+
209
+ const conversation = thread
210
+ ? { thread, participants: turn.readers, names: turn.names }
211
+ : undefined
212
+ // The rule run tokens apply to memory: someone who loses access may regain it, and read back
213
+ // whatever memory the thread held meanwhile.
214
+ const shared = participants.length > 0
215
+ // A shared thread's system prompt names nobody, so it stays the same whoever sends the turn.
216
+ const requester =
217
+ (conversation?.participants.length ?? 0) > 0
218
+ ? undefined
219
+ : agentRequester(execution.requestedBy, turn.names)
220
+ const context: AgentExecutionContext = {
221
+ ...settings,
222
+ ...(requester === undefined ? {} : { requester }),
223
+ blobStorage: session.files,
224
+ }
225
+ return {
226
+ thread,
227
+ ...(conversation === undefined ? {} : { conversation }),
228
+ shared,
229
+ plan,
230
+ delegation: {
231
+ models: runPlan.delegation,
232
+ toolNames: runPlan.tools.map((tool) => tool.name),
233
+ },
234
+ execution,
235
+ context,
236
+ model: prepareAgentModel(plan),
237
+ history: turn.history,
238
+ }
239
+ }
240
+
241
+ /**
242
+ * A shared thread's turn waits queued: its requester may leave the thread, or be removed, before
243
+ * it starts. Such a turn never reaches the model. One already running when they leave finishes,
244
+ * as it already read the thread.
245
+ */
246
+ function assertRequesterTakesPart(
247
+ run: ConversationAgentRunRecord,
248
+ thread: AgentThreadRecord | null,
249
+ participants: readonly AgentThreadParticipantRecord[],
250
+ execution: ExecutionRecord
251
+ ): void {
252
+ const requester = execution.requestedBy
253
+ if (!thread || !requester || principalsEqual(requester, thread.ownerPrincipal)) return
254
+ if (participants.some((participant) => principalsEqual(participant.principal, requester))) return
255
+
256
+ throw new AgentRequesterLeftThreadError(run.id)
257
+ }
@@ -0,0 +1,55 @@
1
+ import type { SandboxRequestCredential } from "@sixb/core"
2
+ import { createAgentRunnerToken, createAgentRunToken } from "@sixb/core/internal/agents"
3
+
4
+ /** The token the run's commands call the API with, valid while this execution owns the run. */
5
+ export function runTokenFor(input: {
6
+ readonly projectId: string
7
+ readonly runId: string
8
+ readonly executionToken?: string
9
+ }): string {
10
+ if (!input.executionToken) {
11
+ throw new Error(
12
+ `[SixbAgent] Agent execution '${input.runId}' must hold an execution token before it gets a run token.`
13
+ )
14
+ }
15
+ return createAgentRunToken({
16
+ projectId: input.projectId,
17
+ runId: input.runId,
18
+ executionToken: input.executionToken,
19
+ })
20
+ }
21
+
22
+ /**
23
+ * The token a runner over HTTP reaches its run's session with. It is the runner's alone: the run's
24
+ * commands get the run token, which the session refuses.
25
+ */
26
+ export function runnerTokenFor(input: {
27
+ readonly projectId: string
28
+ readonly runId: string
29
+ readonly executionToken?: string
30
+ }): string {
31
+ if (!input.executionToken) {
32
+ throw new Error(
33
+ `[SixbAgent] Agent execution '${input.runId}' must hold an execution token before it gets a runner token.`
34
+ )
35
+ }
36
+ return createAgentRunnerToken({
37
+ projectId: input.projectId,
38
+ runId: input.runId,
39
+ executionToken: input.executionToken,
40
+ })
41
+ }
42
+
43
+ /**
44
+ * The run token as a request credential: on a provider that adds credentials outside the guest,
45
+ * every call to the API under `apiBaseUrl` gets it, and the run's commands never hold it.
46
+ */
47
+ export function runApiCredential(apiBaseUrl: string, runToken: string): SandboxRequestCredential {
48
+ const url = new URL(apiBaseUrl)
49
+ return {
50
+ origin: url.origin,
51
+ path: { prefix: `${url.pathname.replace(/\/+$/, "")}/api/` },
52
+ method: ["GET", "POST", "PUT", "PATCH", "DELETE"],
53
+ headers: { Authorization: `Bearer ${runToken}` },
54
+ }
55
+ }