@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,326 @@
1
+ import { posix } from "node:path"
2
+ import type { CommandResult } from "@sixb/core"
3
+ import { type AgentSandboxHandle, contextPath } from "./sandbox-handle"
4
+
5
+ // Every read of sandbox files the worker makes goes through these two scripts. A sandbox has no
6
+ // file-read API, so a file comes back as base64 on stdout; the scripts refuse symbolic links and
7
+ // paths that resolve outside the working directory, and bound how much they print.
8
+
9
+ /** Why a sandbox file could not be listed or read. The message is safe to show to the Agent. */
10
+ export type SandboxFileErrorReason =
11
+ | "invalid_path"
12
+ | "outside_root"
13
+ | "not_found"
14
+ | "too_large"
15
+ | "changed"
16
+ | "failed"
17
+
18
+ export class SandboxFileError extends Error {
19
+ constructor(
20
+ readonly reason: SandboxFileErrorReason,
21
+ message: string,
22
+ options?: ErrorOptions
23
+ ) {
24
+ super(message, options)
25
+ this.name = "SandboxFileError"
26
+ }
27
+ }
28
+
29
+ export interface SandboxFileEntry {
30
+ /** Path relative to the listed directory, with `/` separators and no `.` or `..` segments. */
31
+ readonly relativePath: string
32
+ readonly sizeBytes: number
33
+ }
34
+
35
+ export interface SandboxFileListing {
36
+ /** False when nothing is at the path, or it is not a directory: then nothing is listed. */
37
+ readonly directory: boolean
38
+ /** Regular files under the directory, sorted by path. Symbolic links are never listed. */
39
+ readonly files: readonly SandboxFileEntry[]
40
+ /** True when the directory held more than `maxFiles` files; only the first ones are listed. */
41
+ readonly truncated: boolean
42
+ }
43
+
44
+ interface SandboxCommandOptions {
45
+ readonly signal: AbortSignal
46
+ readonly timeoutMs: number
47
+ }
48
+
49
+ /**
50
+ * Where a run reads files: its working directory, and its context directory when that lies
51
+ * elsewhere, as it does in a folder on a computer. Absolute, the working directory first.
52
+ */
53
+ export function sandboxRoots(handle: AgentSandboxHandle): readonly string[] {
54
+ const root = posix.resolve(handle.sandbox.workingDirectory)
55
+ const context = contextPath(handle)
56
+ return context === root || context.startsWith(`${root}/`) ? [root] : [root, context]
57
+ }
58
+
59
+ /**
60
+ * Resolve `requestedPath` against the sandbox working directory. Throws when the path is empty or
61
+ * leaves both the working directory and the run's context directory; symbolic links are checked
62
+ * when the file is read.
63
+ */
64
+ export function resolveSandboxPath(handle: AgentSandboxHandle, requestedPath: string): string {
65
+ if (
66
+ typeof requestedPath !== "string" ||
67
+ requestedPath.length === 0 ||
68
+ requestedPath.includes("\0")
69
+ ) {
70
+ throw new SandboxFileError("invalid_path", "path must be a non-empty sandbox file path")
71
+ }
72
+ // `resolve`, not `normalize`: it drops a trailing slash, which would break the prefix check.
73
+ const roots = sandboxRoots(handle)
74
+ const absolutePath = posix.resolve(roots[0] ?? "/", requestedPath)
75
+ if (!roots.some((root) => absolutePath.startsWith(`${root}/`))) {
76
+ throw new SandboxFileError(
77
+ "outside_root",
78
+ roots.length > 1
79
+ ? "path must stay inside the working directory or the run's context directory"
80
+ : "path must stay inside the sandbox working directory"
81
+ )
82
+ }
83
+ return absolutePath
84
+ }
85
+
86
+ /**
87
+ * List the regular files under `directory`, up to `maxFiles`. A missing directory lists nothing;
88
+ * output that does not parse throws, since a listing that skips a line drops a file silently.
89
+ */
90
+ export async function listSandboxFiles(
91
+ handle: AgentSandboxHandle,
92
+ input: SandboxCommandOptions & {
93
+ readonly directory: string
94
+ readonly maxFiles: number
95
+ }
96
+ ): Promise<SandboxFileListing> {
97
+ const result = await runScript(handle, LIST_SANDBOX_FILES_SCRIPT, input, {
98
+ SIXB_LIST_DIR: input.directory,
99
+ SIXB_LIST_MAX_FILES: String(input.maxFiles),
100
+ })
101
+ if (result.exitCode !== 0) {
102
+ throw new SandboxFileError(
103
+ "failed",
104
+ `directory could not be listed (${result.stderr.trim() || `exit ${result.exitCode}`})`
105
+ )
106
+ }
107
+
108
+ const files: SandboxFileEntry[] = []
109
+ let directory = false
110
+ let truncated = false
111
+ for (const line of result.stdout.split("\n")) {
112
+ if (!line.trim()) continue
113
+ if (line === "DIRECTORY") {
114
+ directory = true
115
+ continue
116
+ }
117
+ if (line === "LIMIT") {
118
+ truncated = true
119
+ continue
120
+ }
121
+ const [sizeSource = "", relativePathBase64] = line.split("\t")
122
+ const relativePath = relativePathBase64 ? decodeBase64Text(relativePathBase64) : null
123
+ if (!/^\d+$/.test(sizeSource) || !relativePath || !isSafeRelativePath(relativePath)) {
124
+ throw new SandboxFileError(
125
+ "failed",
126
+ `directory could not be listed (an entry did not parse: ${JSON.stringify(line.slice(0, 200))})`
127
+ )
128
+ }
129
+ files.push({ relativePath, sizeBytes: Number(sizeSource) })
130
+ }
131
+
132
+ files.sort((left, right) => left.relativePath.localeCompare(right.relativePath))
133
+ return { directory, files, truncated }
134
+ }
135
+
136
+ /**
137
+ * Read one regular file inside the sandbox working directory. Refuses symbolic links, paths whose
138
+ * parent resolves outside the working directory, and files larger than `maxBytes`.
139
+ */
140
+ export async function readSandboxFile(
141
+ handle: AgentSandboxHandle,
142
+ input: SandboxCommandOptions & {
143
+ /** An absolute path, as `resolveSandboxPath` returns it. */
144
+ readonly path: string
145
+ readonly maxBytes: number
146
+ }
147
+ ): Promise<Uint8Array> {
148
+ const result = await runScript(handle, READ_SANDBOX_FILE_SCRIPT, input, {
149
+ SIXB_READ_PATH_B64: Buffer.from(input.path, "utf-8").toString("base64"),
150
+ SIXB_READ_ROOTS: sandboxRoots(handle).join("\n"),
151
+ SIXB_READ_MAX_BYTES: String(input.maxBytes),
152
+ })
153
+
154
+ if (result.exitCode === 2) {
155
+ throw new SandboxFileError(
156
+ "not_found",
157
+ "file does not exist, is not a regular file, or is a symbolic link"
158
+ )
159
+ }
160
+ if (result.exitCode === 3) {
161
+ throw new SandboxFileError("too_large", `file exceeds the ${input.maxBytes}-byte limit`)
162
+ }
163
+ if (result.exitCode === 4) {
164
+ throw new SandboxFileError(
165
+ "outside_root",
166
+ "resolved path escapes the sandbox working directory and the run's context directory"
167
+ )
168
+ }
169
+ if (result.exitCode !== 0) {
170
+ throw new SandboxFileError(
171
+ "failed",
172
+ `file could not be read (${result.stderr.trim() || `exit ${result.exitCode}`})`
173
+ )
174
+ }
175
+
176
+ const separator = result.stdout.indexOf("\n")
177
+ const sizeBytes = Number(result.stdout.slice(0, separator))
178
+ const bytes = Buffer.from(
179
+ separator < 0 ? "" : result.stdout.slice(separator + 1).trim(),
180
+ "base64"
181
+ )
182
+ if (!Number.isInteger(sizeBytes) || sizeBytes < 0 || bytes.byteLength !== sizeBytes) {
183
+ throw new SandboxFileError("changed", "file changed while it was being read")
184
+ }
185
+ // A view, not a copy: a large file is held once more already, as the base64 it came as.
186
+ return new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength)
187
+ }
188
+
189
+ /** What a path names once its symbolic links are followed. */
190
+ export type SandboxPathEntry =
191
+ | {
192
+ readonly kind: "file" | "directory"
193
+ /** Absolute, every symbolic link along it followed. */
194
+ readonly realPath: string
195
+ }
196
+ | { readonly kind: "missing" }
197
+ | { readonly kind: "link" }
198
+
199
+ /**
200
+ * Follow the symbolic links along `path`, an absolute path as `resolveSandboxPath` returns it, to
201
+ * where it really is. A path that is itself a link is reported as one, as reads refuse it.
202
+ */
203
+ export async function resolveSandboxEntry(
204
+ handle: AgentSandboxHandle,
205
+ input: SandboxCommandOptions & { readonly path: string }
206
+ ): Promise<SandboxPathEntry> {
207
+ const result = await runScript(handle, RESOLVE_SANDBOX_PATH_SCRIPT, input, {
208
+ SIXB_RESOLVE_PATH_B64: Buffer.from(input.path, "utf-8").toString("base64"),
209
+ })
210
+ if (result.exitCode !== 0) {
211
+ throw new SandboxFileError(
212
+ "failed",
213
+ `path could not be resolved (${result.stderr.trim() || `exit ${result.exitCode}`})`
214
+ )
215
+ }
216
+
217
+ const [kind = "", realPathBase64] = result.stdout.trim().split("\n")
218
+ if (kind === "missing") return { kind }
219
+ if (kind === "link") return { kind }
220
+ const realPath = realPathBase64 ? decodeBase64Text(realPathBase64) : null
221
+ if ((kind !== "file" && kind !== "directory") || !realPath?.startsWith("/")) {
222
+ throw new SandboxFileError(
223
+ "failed",
224
+ `path could not be resolved (${result.stdout.slice(0, 200)})`
225
+ )
226
+ }
227
+ return { kind, realPath }
228
+ }
229
+
230
+ async function runScript(
231
+ handle: AgentSandboxHandle,
232
+ script: string,
233
+ options: SandboxCommandOptions,
234
+ env: Readonly<Record<string, string>>
235
+ ): Promise<CommandResult> {
236
+ const result = await handle.sandbox.runCommand("bash", ["-lc", script], {
237
+ env: { ...(handle.env ?? {}), ...env },
238
+ timeout: options.timeoutMs,
239
+ signal: options.signal,
240
+ })
241
+ options.signal.throwIfAborted()
242
+ return result
243
+ }
244
+
245
+ function isSafeRelativePath(value: string): boolean {
246
+ if (!value || value.startsWith("/") || value.includes("\0")) return false
247
+ return value
248
+ .split("/")
249
+ .every((segment) => segment.length > 0 && segment !== "." && segment !== "..")
250
+ }
251
+
252
+ function decodeBase64Text(value: string): string | null {
253
+ try {
254
+ return Buffer.from(value, "base64").toString("utf-8")
255
+ } catch {
256
+ return null
257
+ }
258
+ }
259
+
260
+ // `find | while` rather than process substitution: some sandbox images (Vercel's stock one) have
261
+ // no /dev/fd, so `< <(…)` fails there.
262
+ const LIST_SANDBOX_FILES_SCRIPT = `# sixb-list-sandbox-files
263
+ set -eu
264
+ dir="\${SIXB_LIST_DIR:?}"
265
+ limit="\${SIXB_LIST_MAX_FILES:?}"
266
+ if [ ! -d "$dir" ]; then
267
+ exit 0
268
+ fi
269
+ printf 'DIRECTORY\\n'
270
+ count=0
271
+ find "$dir" -type f -print0 | while IFS= read -r -d '' path; do
272
+ count=$((count + 1))
273
+ if [ "$count" -gt "$limit" ]; then
274
+ printf 'LIMIT\\n'
275
+ break
276
+ fi
277
+ rel="\${path#"$dir"/}"
278
+ size="$(wc -c < "$path" | tr -d ' ')"
279
+ rel64="$(printf '%s' "$rel" | base64 | tr -d '\\n')"
280
+ printf '%s\\t%s\\n' "$size" "$rel64"
281
+ done`
282
+
283
+ // Prints what the path is, then where it really is as base64. `cd -P` follows every link along it.
284
+ const RESOLVE_SANDBOX_PATH_SCRIPT = `# sixb-resolve-sandbox-path
285
+ set -eu
286
+ path="$(printf '%s' "\${SIXB_RESOLVE_PATH_B64:?}" | base64 -d)"
287
+ if [ -L "$path" ]; then
288
+ printf 'link\\n'
289
+ exit 0
290
+ fi
291
+ if [ -d "$path" ]; then
292
+ kind=directory
293
+ real="$(cd -P -- "$path" && pwd -P)"
294
+ elif [ -f "$path" ]; then
295
+ kind=file
296
+ real="$(cd -P -- "$(dirname -- "$path")" && pwd -P)/$(basename -- "$path")"
297
+ else
298
+ printf 'missing\\n'
299
+ exit 0
300
+ fi
301
+ printf '%s\\n%s\\n' "$kind" "$(printf '%s' "$real" | base64 | tr -d '\\n')"`
302
+
303
+ // Prints the file's size on the first line and its bytes as base64 on the second, so the caller
304
+ // can tell a file that changed between the two reads.
305
+ const READ_SANDBOX_FILE_SCRIPT = `# sixb-read-sandbox-file
306
+ set -euo pipefail
307
+ requested="$(printf '%s' "\${SIXB_READ_PATH_B64:?}" | base64 -d)"
308
+ roots="\${SIXB_READ_ROOTS:?}"
309
+ max_bytes="\${SIXB_READ_MAX_BYTES:?}"
310
+ if [ ! -f "$requested" ] || [ -L "$requested" ]; then
311
+ exit 2
312
+ fi
313
+ parent="$(cd -P -- "$(dirname -- "$requested")" && pwd)"
314
+ resolved="$parent/$(basename -- "$requested")"
315
+ inside=0
316
+ while IFS= read -r root; do
317
+ real_root="$(cd -P -- "$root" 2>/dev/null && pwd)" || continue
318
+ case "$resolved" in "$real_root"/*) inside=1 ;; esac
319
+ done <<< "$roots"
320
+ [ "$inside" = 1 ] || exit 4
321
+ size="$(wc -c < "$resolved" | tr -d ' ')"
322
+ if [ "$size" -gt "$max_bytes" ]; then
323
+ exit 3
324
+ fi
325
+ printf '%s\\n' "$size"
326
+ head -c "$max_bytes" "$resolved" | base64 | tr -d '\\n'`
@@ -0,0 +1,19 @@
1
+ import { posix } from "node:path"
2
+ import type { Sandbox } from "@sixb/core"
3
+
4
+ /** The sandbox plus its run-scoped environment, resolved lazily when first needed. */
5
+ export interface AgentSandboxHandle {
6
+ readonly sandbox: Sandbox
7
+ /**
8
+ * Where the run's files are installed: skills, ontology docs, memory, attachments, artifacts,
9
+ * children's files and the agent's `sixb`. As the agent names it: relative to the working
10
+ * directory, or absolute.
11
+ */
12
+ readonly contextDirectory: string
13
+ readonly env?: Readonly<Record<string, string>>
14
+ }
15
+
16
+ /** A path in the run's context directory, absolute in the sandbox. */
17
+ export function contextPath(handle: AgentSandboxHandle, ...segments: string[]): string {
18
+ return posix.resolve(handle.sandbox.workingDirectory, handle.contextDirectory, ...segments)
19
+ }
@@ -0,0 +1,22 @@
1
+ interface SandboxReadinessGuard {
2
+ readonly signal: AbortSignal
3
+ throwIfFailed(): void
4
+ }
5
+
6
+ /** Observe concurrent sandbox provisioning without putting healthy boot on the model's critical path. */
7
+ export function monitorSandboxReadiness(
8
+ ready: Promise<unknown> | undefined
9
+ ): SandboxReadinessGuard {
10
+ const abort = new AbortController()
11
+ let failure: { readonly error: unknown } | undefined
12
+ ready?.catch((error) => {
13
+ failure = { error }
14
+ abort.abort()
15
+ })
16
+ return {
17
+ signal: abort.signal,
18
+ throwIfFailed() {
19
+ if (failure) throw failure.error
20
+ },
21
+ }
22
+ }
@@ -0,0 +1,183 @@
1
+ /// <reference path="./text-assets.d.ts" />
2
+ import type { AgentSkillDefinition } from "@sixb/core"
3
+ import canvasGuide from "./sixb-artifacts/canvas.md" with { type: "text" }
4
+ import documentGuide from "./sixb-artifacts/document.md" with { type: "text" }
5
+ import onboardingFirstJob from "./sixb-artifacts/examples/canvas/onboarding-flow/boards/first-job.html" with {
6
+ type: "text",
7
+ }
8
+ import onboardingRegion from "./sixb-artifacts/examples/canvas/onboarding-flow/boards/region.html" with {
9
+ type: "text",
10
+ }
11
+ import onboardingShared from "./sixb-artifacts/examples/canvas/onboarding-flow/boards/shared.css" with {
12
+ type: "text",
13
+ }
14
+ import onboardingWelcome from "./sixb-artifacts/examples/canvas/onboarding-flow/boards/welcome.html" with {
15
+ type: "text",
16
+ }
17
+ import onboardingCanvas from "./sixb-artifacts/examples/canvas/onboarding-flow/canvas.json" with {
18
+ type: "text",
19
+ }
20
+ import retroCanvas from "./sixb-artifacts/examples/canvas/retro/canvas.json" with { type: "text" }
21
+ import incidentReport from "./sixb-artifacts/examples/document/incident-report/document.md" with {
22
+ type: "text",
23
+ }
24
+ import operationsReview from "./sixb-artifacts/examples/document/operations-review/document.md" with {
25
+ type: "text",
26
+ }
27
+ import restorationsFigure from "./sixb-artifacts/examples/document/operations-review/figures/restorations.svg" with {
28
+ type: "text",
29
+ }
30
+ import collectionsDeskPage from "./sixb-artifacts/examples/page/collections-desk/index.html" with {
31
+ type: "text",
32
+ }
33
+ import collectionsDeskQuery from "./sixb-artifacts/examples/page/collections-desk/queries/overdue.json" with {
34
+ type: "text",
35
+ }
36
+ import dispatchBoardData from "./sixb-artifacts/examples/page/dispatch-board/data/dispatch.json" with {
37
+ type: "text",
38
+ }
39
+ import dispatchBoardPage from "./sixb-artifacts/examples/page/dispatch-board/index.html" with {
40
+ type: "text",
41
+ }
42
+ import fleetPlannerData from "./sixb-artifacts/examples/page/fleet-planner/data/plan.json" with {
43
+ type: "text",
44
+ }
45
+ import fleetPlannerPage from "./sixb-artifacts/examples/page/fleet-planner/index.html" with {
46
+ type: "text",
47
+ }
48
+ import serviceExplorerData from "./sixb-artifacts/examples/page/service-explorer/data/service.json" with {
49
+ type: "text",
50
+ }
51
+ import serviceExplorerPage from "./sixb-artifacts/examples/page/service-explorer/index.html" with {
52
+ type: "text",
53
+ }
54
+ import overdueQuery from "./sixb-artifacts/examples/sheet/overdue-invoices/queries/overdue.json" with {
55
+ type: "text",
56
+ }
57
+ import overdueWorkbook from "./sixb-artifacts/examples/sheet/overdue-invoices/workbook.json" with {
58
+ type: "text",
59
+ }
60
+ import partsInventory from "./sixb-artifacts/examples/sheet/parts-inventory/sheets/Inventory.csv" with {
61
+ type: "text",
62
+ }
63
+ import partsSpend from "./sixb-artifacts/examples/sheet/parts-inventory/sheets/Spend.csv" with {
64
+ type: "text",
65
+ }
66
+ import partsWorkbook from "./sixb-artifacts/examples/sheet/parts-inventory/workbook.json" with {
67
+ type: "text",
68
+ }
69
+ import serviceQuarters from "./sixb-artifacts/examples/sheet/service-metrics/sheets/Quarters.csv" with {
70
+ type: "text",
71
+ }
72
+ import serviceRegions from "./sixb-artifacts/examples/sheet/service-metrics/sheets/Regions.csv" with {
73
+ type: "text",
74
+ }
75
+ import serviceWorkbook from "./sixb-artifacts/examples/sheet/service-metrics/workbook.json" with {
76
+ type: "text",
77
+ }
78
+ import launchPlanDeck from "./sixb-artifacts/examples/slides/launch-plan/index.html" with {
79
+ type: "text",
80
+ }
81
+ import q3ReviewDeck from "./sixb-artifacts/examples/slides/q3-review/index.html" with {
82
+ type: "text",
83
+ }
84
+ import fileGuide from "./sixb-artifacts/file.md" with { type: "text" }
85
+ import pageGuide from "./sixb-artifacts/page.md" with { type: "text" }
86
+ import queriesGuide from "./sixb-artifacts/queries.md" with { type: "text" }
87
+ import referencesGuide from "./sixb-artifacts/references.md" with { type: "text" }
88
+ import skillMarkdown from "./sixb-artifacts/SKILL.md" with { type: "text" }
89
+ import sheetGuide from "./sixb-artifacts/sheet.md" with { type: "text" }
90
+ import slidesGuide from "./sixb-artifacts/slides.md" with { type: "text" }
91
+ import memorySkillMarkdown from "./sixb-memory/SKILL.md" with { type: "text" }
92
+
93
+ /**
94
+ * Skills the worker ships, in every conversation's catalog. They describe formats the publisher
95
+ * validates and the app renders, so they change with them. Their names start with `sixb-`, which
96
+ * skill discovery refuses for a project's skills, so none collides with one.
97
+ */
98
+ export const BUILT_IN_SKILLS: readonly AgentSkillDefinition[] = Object.freeze([
99
+ builtInSkill(skillMarkdown, {
100
+ "document.md": documentGuide,
101
+ "file.md": fileGuide,
102
+ "examples/document/operations-review/document.md": operationsReview,
103
+ "examples/document/operations-review/figures/restorations.svg": restorationsFigure,
104
+ "examples/document/incident-report/document.md": incidentReport,
105
+ "sheet.md": sheetGuide,
106
+ "references.md": referencesGuide,
107
+ "slides.md": slidesGuide,
108
+ "examples/slides/q3-review/index.html": text(q3ReviewDeck),
109
+ "examples/slides/launch-plan/index.html": text(launchPlanDeck),
110
+ "examples/sheet/service-metrics/workbook.json": text(serviceWorkbook),
111
+ "examples/sheet/service-metrics/sheets/Quarters.csv": serviceQuarters,
112
+ "examples/sheet/service-metrics/sheets/Regions.csv": serviceRegions,
113
+ "examples/sheet/parts-inventory/workbook.json": text(partsWorkbook),
114
+ "examples/sheet/parts-inventory/sheets/Inventory.csv": partsInventory,
115
+ "examples/sheet/parts-inventory/sheets/Spend.csv": partsSpend,
116
+ "examples/sheet/overdue-invoices/workbook.json": text(overdueWorkbook),
117
+ "examples/sheet/overdue-invoices/queries/overdue.json": text(overdueQuery),
118
+ "queries.md": queriesGuide,
119
+ "page.md": pageGuide,
120
+ "examples/page/fleet-planner/index.html": text(fleetPlannerPage),
121
+ "examples/page/fleet-planner/data/plan.json": text(fleetPlannerData),
122
+ "examples/page/service-explorer/index.html": text(serviceExplorerPage),
123
+ "examples/page/service-explorer/data/service.json": text(serviceExplorerData),
124
+ "examples/page/dispatch-board/index.html": text(dispatchBoardPage),
125
+ "examples/page/dispatch-board/data/dispatch.json": text(dispatchBoardData),
126
+ "examples/page/collections-desk/index.html": text(collectionsDeskPage),
127
+ "examples/page/collections-desk/queries/overdue.json": text(collectionsDeskQuery),
128
+ "canvas.md": canvasGuide,
129
+ "examples/canvas/onboarding-flow/canvas.json": text(onboardingCanvas),
130
+ "examples/canvas/onboarding-flow/boards/welcome.html": text(onboardingWelcome),
131
+ "examples/canvas/onboarding-flow/boards/region.html": text(onboardingRegion),
132
+ "examples/canvas/onboarding-flow/boards/first-job.html": text(onboardingFirstJob),
133
+ "examples/canvas/onboarding-flow/boards/shared.css": text(onboardingShared),
134
+ "examples/canvas/retro/canvas.json": text(retroCanvas),
135
+ }),
136
+ ])
137
+
138
+ /**
139
+ * How to write and save the Agent's memory. Installed with the memory, outside the catalog: the
140
+ * memory section of the prompt names it, so runs without memory never see it.
141
+ */
142
+ export const MEMORY_SKILL: AgentSkillDefinition = builtInSkill(memorySkillMarkdown, {})
143
+
144
+ /** The project's skills, then the built-in ones. */
145
+ export function withBuiltInSkills(
146
+ projectSkills: readonly AgentSkillDefinition[]
147
+ ): readonly AgentSkillDefinition[] {
148
+ return Object.freeze([...projectSkills, ...BUILT_IN_SKILLS])
149
+ }
150
+
151
+ /**
152
+ * A JSON or HTML file imported as text: TypeScript types the import as the parsed value or Bun's
153
+ * HTML bundle, which it is not.
154
+ */
155
+ function text(value: unknown): string {
156
+ if (typeof value !== "string") {
157
+ throw new Error("[SixbAgent] A built-in skill's file must be imported as text.")
158
+ }
159
+ return value
160
+ }
161
+
162
+ function builtInSkill(skill: string, files: Record<string, string>): AgentSkillDefinition {
163
+ const frontmatter = /^---\n([\s\S]*?)\n---\n/.exec(skill)?.[1]
164
+ const metadata: unknown = frontmatter === undefined ? null : Bun.YAML.parse(frontmatter)
165
+ if (
166
+ typeof metadata !== "object" ||
167
+ metadata === null ||
168
+ !("name" in metadata) ||
169
+ typeof metadata.name !== "string" ||
170
+ !("description" in metadata) ||
171
+ typeof metadata.description !== "string"
172
+ ) {
173
+ throw new Error("[SixbAgent] A built-in skill's SKILL.md needs a name and a description.")
174
+ }
175
+ return Object.freeze({
176
+ name: metadata.name,
177
+ description: metadata.description,
178
+ files: Object.freeze([
179
+ { path: "SKILL.md", contents: skill },
180
+ ...Object.entries(files).map(([path, contents]) => ({ path, contents })),
181
+ ]),
182
+ })
183
+ }
@@ -0,0 +1,95 @@
1
+ ---
2
+ name: sixb-artifacts
3
+ description: Publish a deliverable the user should see, such as a report, a write-up, a table of data, or a file they asked for, as an artifact, and revise it as a new version.
4
+ ---
5
+
6
+ # Artifacts
7
+
8
+ An artifact is a deliverable the user opens beside the conversation. It has a kind, a title, and
9
+ versions that stay readable. Publish one when the user asks for a report, a document, a table, or
10
+ a file, or when the answer is long-form work or data they will read, reuse, or share. When a few sentences answer
11
+ the question, answer in the chat.
12
+
13
+ ## Kinds
14
+
15
+ | Kind | Publish it for | Read first |
16
+ | --- | --- | --- |
17
+ | `document` | A report, brief, memo, plan, or write-up: anything read top to bottom | `document.md` |
18
+ | `sheet` | Tabular data the reader sorts, sums, charts, or downloads: metrics, lists, trackers, exports | `sheet.md` |
19
+ | `slides` | A deck to present: a review, a pitch, a plan for a meeting | `slides.md` |
20
+ | `page` | A small web app the reader works with: a calculator, a planner, a dashboard with filters | `page.md` |
21
+ | `canvas` | Screens side by side with arrows, a brainstorm or retro of notes, a diagram | `canvas.md` |
22
+ | `file` | One file in a format the user asked for (PDF, DOCX, XLSX, PPTX, CSV), or a binary | `file.md` |
23
+
24
+ To show or link to another artifact, such as a sheet's chart in a report, read `references.md`.
25
+ Data from the project's objects goes in a query, so the artifact stays live: read `queries.md`.
26
+
27
+ Prefer a `document` to a DOCX or PDF, a `sheet` to an XLSX or CSV file, and `slides` to a PPTX,
28
+ unless the user asked for that format: they render in the app, and their versions stay readable side by side. A report
29
+ with a table of a dozen rows is a document; data the reader works with is a sheet.
30
+
31
+ ## Publish
32
+
33
+ 1. Write the artifact's files in a working directory, such as `work/q3-review/`.
34
+ 2. Call `publish_artifact` with that directory (or one file), the kind, and a title:
35
+
36
+ ```text
37
+ publish_artifact({ path: "work/q3-review", kind: "document", title: "Q3 operations review" })
38
+ → Published "Q3 operations review" as artifact art_k3m9q2x7tp, version 1.
39
+ ```
40
+
41
+ 3. If it refuses, nothing was published. Fix what the message names and publish again. If you
42
+ cannot, tell the user the deliverable could not be published; never describe it as ready.
43
+ 4. Fix what each warning names and publish its next version when it matters to the reader. A
44
+ deck also comes back with a screenshot of each slide, a page with one at a laptop's width and
45
+ one at a phone's, and a canvas with one of each board: look at them (`slides.md`, `page.md`,
46
+ `canvas.md`, Check).
47
+ 5. In the reply, say in a sentence or two what you published and what stands out. Do not paste
48
+ its content, a path, or its id as text: the user sees the artifact. To show a chart or a range
49
+ of it in place, use its address (`references.md`). A figure you cite from live data is the one
50
+ you read, and what the artifact shows moves on as the objects change, even at a pinned version:
51
+ date it with when you read it, in the user's time zone ("As of Oct 23, 10:42, three invoices
52
+ are overdue.").
53
+
54
+ ## Revise
55
+
56
+ The artifacts this conversation published are listed in `$SIXB_ARTIFACTS`, each with its latest
57
+ version installed under `$SIXB_ARTIFACTS_DIR/<id>/`, including one you published moments ago. To
58
+ change one, start from that version, not from an older working copy:
59
+
60
+ ```bash
61
+ rm -rf work/q3-review && cp -r "$SIXB_ARTIFACTS_DIR/art_k3m9q2x7tp" work/q3-review
62
+ ```
63
+
64
+ Edit the copy, then publish it as the artifact's next version:
65
+
66
+ ```text
67
+ publish_artifact_version({ id: "art_k3m9q2x7tp", path: "work/q3-review", title: "Q3 operations review" })
68
+ → Published "Q3 operations review" as artifact art_k3m9q2x7tp, version 2.
69
+ ```
70
+
71
+ An entry with `"edited": true` has a latest version published outside this conversation after you
72
+ last published it: most often a person's edit in the app, such as a sheet's cells or columns or a
73
+ canvas's notes and layout, but also another conversation or the project's code. Start from that
74
+ version as it is, and keep its changes in yours unless the user asks you to undo them; say what you
75
+ built on when you reply.
76
+
77
+ If the artifact moves on while you work, `publish_artifact_version` refuses rather than publish
78
+ over the newer version, says who published it, and installs it in place of your copy. Start again
79
+ from that copy, keep its changes, make yours again, and publish.
80
+
81
+ An entry without a `directory` is not installed: the conversation's artifacts were too large to
82
+ install together, or its files could not be read. Rebuild it in a working directory with the paths
83
+ in its `files`, then publish its next version. A new version keeps the earlier ones and the
84
+ artifact's kind; the title may change. Call `publish_artifact` again only for a different
85
+ deliverable.
86
+
87
+ ## Rules
88
+
89
+ - A version holds at most 200 files, 25 MB per file, and 100 MB in all.
90
+ - A title is plain text of at most 200 characters that names the deliverable ("Q3 operations
91
+ review"), not the request.
92
+ - Files and folders whose names start with a dot are not published.
93
+ - A sheet of the project's data is live, its rows read by a query, unless the user asked for a
94
+ snapshot (`queries.md`).
95
+ - An artifact holds no secrets, credentials, or internal URLs.