@arcforge/platform 2.0.166

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 (207) hide show
  1. package/README.md +31 -0
  2. package/package.json +69 -0
  3. package/src/bin/supervisor.ts +102 -0
  4. package/src/bin/test-api.ts +30 -0
  5. package/src/bin/test-preload.ts +308 -0
  6. package/src/bin/update-helper.ts +32 -0
  7. package/src/build/bench/CLAUDE.md +106 -0
  8. package/src/build/bench/SPEC.md +209 -0
  9. package/src/build/bench/agents.ts +105 -0
  10. package/src/build/bench/axes.ts +84 -0
  11. package/src/build/bench/bench.ts +96 -0
  12. package/src/build/bench/discover.ts +147 -0
  13. package/src/build/bench/index.ts +7 -0
  14. package/src/build/bench/log.ts +47 -0
  15. package/src/build/bench/matrix.ts +23 -0
  16. package/src/build/bench/preload.ts +440 -0
  17. package/src/build/bench/prepare.ts +61 -0
  18. package/src/build/bench/projection.ts +112 -0
  19. package/src/build/bench/resolve.ts +54 -0
  20. package/src/build/bench/runner.ts +309 -0
  21. package/src/build/bench/schema/extract.ts +148 -0
  22. package/src/build/bench/schema/index.ts +2 -0
  23. package/src/build/bench/schema/schema.ts +95 -0
  24. package/src/build/bench/schema/worker.ts +30 -0
  25. package/src/build/bench/typegen.ts +119 -0
  26. package/src/build/bench/workspace/files.ts +139 -0
  27. package/src/build/bench/workspace/handle.ts +133 -0
  28. package/src/build/bench/workspace/index.ts +11 -0
  29. package/src/build/bench/workspace/workspace.ts +14 -0
  30. package/src/build/blueprint/blueprint.ts +398 -0
  31. package/src/build/blueprint/cognet/abi.ts +207 -0
  32. package/src/build/blueprint/cognet/bundle-worker.ts +93 -0
  33. package/src/build/blueprint/cognet/bundle.ts +664 -0
  34. package/src/build/blueprint/cognet/cognet.ts +249 -0
  35. package/src/build/blueprint/cognet/index.ts +10 -0
  36. package/src/build/blueprint/cognet/manifest.ts +38 -0
  37. package/src/build/blueprint/cognet/resolve.ts +113 -0
  38. package/src/build/blueprint/collisions.ts +51 -0
  39. package/src/build/blueprint/index.ts +29 -0
  40. package/src/build/blueprint/modules/entries.ts +117 -0
  41. package/src/build/blueprint/modules/index.ts +300 -0
  42. package/src/build/blueprint/modules/meta.ts +174 -0
  43. package/src/build/blueprint/scan/boot.ts +34 -0
  44. package/src/build/blueprint/scan/cognetImports.ts +56 -0
  45. package/src/build/blueprint/scan/components.ts +45 -0
  46. package/src/build/blueprint/scan/config.ts +234 -0
  47. package/src/build/blueprint/scan/configImports.ts +232 -0
  48. package/src/build/blueprint/scan/declare-worker.ts +61 -0
  49. package/src/build/blueprint/scan/declare.ts +393 -0
  50. package/src/build/blueprint/scan/h3-globals.ts +137 -0
  51. package/src/build/blueprint/scan/knowledge.ts +101 -0
  52. package/src/build/blueprint/scan/middleware.ts +110 -0
  53. package/src/build/blueprint/scan/moduleImports.ts +79 -0
  54. package/src/build/blueprint/scan/plugins.ts +93 -0
  55. package/src/build/blueprint/scan/promptPackages.ts +45 -0
  56. package/src/build/blueprint/scan/prompts.ts +164 -0
  57. package/src/build/blueprint/scan/published.ts +131 -0
  58. package/src/build/blueprint/scan/resolve.ts +398 -0
  59. package/src/build/blueprint/scan/routes.ts +109 -0
  60. package/src/build/blueprint/scan/scripts.ts +119 -0
  61. package/src/build/blueprint/scan/tool-bundle-worker.ts +69 -0
  62. package/src/build/blueprint/scan/tools.ts +610 -0
  63. package/src/build/blueprint/types.ts +28 -0
  64. package/src/build/extensions/config.ts +261 -0
  65. package/src/build/extensions/edit.ts +684 -0
  66. package/src/build/extensions/extensions.ts +731 -0
  67. package/src/build/extensions/index.ts +10 -0
  68. package/src/build/extensions/load.ts +350 -0
  69. package/src/build/extensions/registrations.ts +189 -0
  70. package/src/build/extensions/store.ts +200 -0
  71. package/src/build/frame/frame.ts +145 -0
  72. package/src/build/frame/index.ts +5 -0
  73. package/src/build/frame/migrate.ts +246 -0
  74. package/src/build/project/bundle/agent.ts +382 -0
  75. package/src/build/project/bundle/artifacts.ts +88 -0
  76. package/src/build/project/bundle/assets.ts +385 -0
  77. package/src/build/project/bundle/bundle.ts +160 -0
  78. package/src/build/project/bundle/imports.ts +102 -0
  79. package/src/build/project/bundle/index.ts +16 -0
  80. package/src/build/project/bundle/module.ts +130 -0
  81. package/src/build/project/bundle/source.ts +136 -0
  82. package/src/build/project/bundle/stage.ts +97 -0
  83. package/src/build/project/bundle/types.ts +111 -0
  84. package/src/build/project/cognet.ts +156 -0
  85. package/src/build/project/create/agent.ts +52 -0
  86. package/src/build/project/create/bench.ts +81 -0
  87. package/src/build/project/create/cognet.ts +102 -0
  88. package/src/build/project/create/extension.ts +63 -0
  89. package/src/build/project/create/module.ts +43 -0
  90. package/src/build/project/create/profile.ts +102 -0
  91. package/src/build/project/create/prompt.ts +68 -0
  92. package/src/build/project/create/templates.ts +88 -0
  93. package/src/build/project/deploy.ts +96 -0
  94. package/src/build/project/index.ts +40 -0
  95. package/src/build/project/installer.ts +616 -0
  96. package/src/build/project/kinds.ts +374 -0
  97. package/src/build/project/links.ts +33 -0
  98. package/src/build/project/manifest/bunfig.ts +139 -0
  99. package/src/build/project/manifest/config.ts +246 -0
  100. package/src/build/project/manifest/env.ts +184 -0
  101. package/src/build/project/manifest/index.ts +9 -0
  102. package/src/build/project/manifest/manifest.ts +49 -0
  103. package/src/build/project/manifest/model.ts +124 -0
  104. package/src/build/project/manifest/package.ts +386 -0
  105. package/src/build/project/manifest/text.ts +19 -0
  106. package/src/build/project/models/fetch.ts +123 -0
  107. package/src/build/project/models/index.ts +8 -0
  108. package/src/build/project/models/models.ts +111 -0
  109. package/src/build/project/models/specifier.ts +106 -0
  110. package/src/build/project/models/store.ts +202 -0
  111. package/src/build/project/modules/hash.ts +31 -0
  112. package/src/build/project/modules/index.ts +5 -0
  113. package/src/build/project/modules/lock.ts +78 -0
  114. package/src/build/project/modules/modules.ts +155 -0
  115. package/src/build/project/prepare.ts +709 -0
  116. package/src/build/project/project.ts +253 -0
  117. package/src/build/project/projects.ts +164 -0
  118. package/src/build/project/propagation.ts +120 -0
  119. package/src/build/project/publish/verify.ts +312 -0
  120. package/src/build/project/publish.ts +155 -0
  121. package/src/build/project/reconcile.ts +93 -0
  122. package/src/build/project/specifier.ts +42 -0
  123. package/src/build/project/tree.ts +615 -0
  124. package/src/build/project/treecache.ts +356 -0
  125. package/src/build/project/typegen/axon-dts.ts +362 -0
  126. package/src/build/project/typegen/cognet-dts.ts +91 -0
  127. package/src/build/project/typegen/components.ts +45 -0
  128. package/src/build/project/typegen/env.ts +59 -0
  129. package/src/build/project/typegen/generators.ts +108 -0
  130. package/src/build/project/typegen/hooks.ts +71 -0
  131. package/src/build/project/typegen/index.ts +137 -0
  132. package/src/build/project/typegen/prompt-dts.ts +40 -0
  133. package/src/build/project/typegen/prompts.ts +36 -0
  134. package/src/build/project/typegen/registry-dts.ts +109 -0
  135. package/src/build/project/typegen/scripts.ts +37 -0
  136. package/src/build/project/typegen/tools.ts +31 -0
  137. package/src/build/project/typegen/tsconfig.ts +243 -0
  138. package/src/build/project/typegen/tui-dts.ts +120 -0
  139. package/src/build/project/typegen/write.ts +40 -0
  140. package/src/build/project/verify.ts +268 -0
  141. package/src/build/project/watcher.ts +266 -0
  142. package/src/build/runtime/agent.ts +550 -0
  143. package/src/build/runtime/escalations.ts +159 -0
  144. package/src/build/runtime/index.ts +15 -0
  145. package/src/build/runtime/instances.ts +604 -0
  146. package/src/build/runtime/profile.ts +91 -0
  147. package/src/build/runtime/progress.ts +143 -0
  148. package/src/build/runtime/requests.ts +167 -0
  149. package/src/build/runtime/resolve.ts +159 -0
  150. package/src/build/runtime/runtime.ts +233 -0
  151. package/src/build/runtime/sessions/index.ts +5 -0
  152. package/src/build/runtime/sessions/record.ts +145 -0
  153. package/src/build/runtime/sessions/sessions.ts +240 -0
  154. package/src/build/runtime/zeno.ts +120 -0
  155. package/src/index.ts +11 -0
  156. package/src/link/agent-main.ts +395 -0
  157. package/src/link/index.ts +70 -0
  158. package/src/link/services.ts +120 -0
  159. package/src/platform.ts +293 -0
  160. package/src/procs/index.ts +15 -0
  161. package/src/procs/tree.ts +177 -0
  162. package/src/services/cloud.ts +419 -0
  163. package/src/services/control/client.ts +126 -0
  164. package/src/services/control/dispatch.ts +156 -0
  165. package/src/services/control/index.ts +17 -0
  166. package/src/services/control/protocol.ts +179 -0
  167. package/src/services/control/server.ts +241 -0
  168. package/src/services/deployments.ts +81 -0
  169. package/src/services/files.ts +329 -0
  170. package/src/services/index.ts +5 -0
  171. package/src/services/mic/capture.ts +248 -0
  172. package/src/services/mic/config.ts +109 -0
  173. package/src/services/mic/fft.ts +93 -0
  174. package/src/services/mic/index.ts +139 -0
  175. package/src/services/mic/visualizer.ts +46 -0
  176. package/src/services/ollama/catalog.ts +122 -0
  177. package/src/services/ollama/http.ts +124 -0
  178. package/src/services/ollama/index.ts +13 -0
  179. package/src/services/ollama/models.ts +153 -0
  180. package/src/services/ollama/ollama.ts +89 -0
  181. package/src/services/ollama/registry.ts +112 -0
  182. package/src/services/ollama/types.ts +72 -0
  183. package/src/services/prompts.ts +76 -0
  184. package/src/services/registry.ts +220 -0
  185. package/src/services/resources/hardware.ts +125 -0
  186. package/src/services/resources/index.ts +6 -0
  187. package/src/services/resources/reservations.ts +162 -0
  188. package/src/services/resources/resources.ts +156 -0
  189. package/src/services/running/index.ts +1 -0
  190. package/src/services/running/running.ts +200 -0
  191. package/src/services/store/disk.ts +97 -0
  192. package/src/services/store/index.ts +15 -0
  193. package/src/services/store/store.ts +729 -0
  194. package/src/services/store/types.ts +303 -0
  195. package/src/services/test/child.ts +150 -0
  196. package/src/services/test/events.ts +102 -0
  197. package/src/services/test/files.ts +26 -0
  198. package/src/services/test/frames.ts +39 -0
  199. package/src/services/test/index.ts +7 -0
  200. package/src/services/test/runner.ts +122 -0
  201. package/src/services/test/types.ts +26 -0
  202. package/src/services/update/contract.ts +77 -0
  203. package/src/services/update/index.ts +12 -0
  204. package/src/services/update/installer.ts +139 -0
  205. package/src/services/update/updates.ts +137 -0
  206. package/src/utils/fs.ts +148 -0
  207. package/src/utils/tsast.ts +102 -0
@@ -0,0 +1,382 @@
1
+ import { dirname, join, relative, resolve, sep } from "node:path"
2
+ import { cp, mkdir, rm } from "node:fs/promises"
3
+ import { axonBaseRef, type AxonConfig } from "@arcforge/types"
4
+ import { err } from "@arcforge/err"
5
+ import { Config, flatten } from "../../blueprint"
6
+ import { Cognet, cognetSourceOf } from "../../blueprint"
7
+ import { fsx } from "../../../utils/fs"
8
+ import { Frame, BUNDLE_COGNET_DIR, type FrameT } from "../../frame"
9
+ import { INLINE_COGNET_DIR } from "../../blueprint/cognet"
10
+ import type { SourceModulesT } from "../modules"
11
+ import { assertConfigContained } from "./imports"
12
+ import { Assets } from "./assets"
13
+ import type { ArtifactsT } from "./artifacts"
14
+ import type { StageT } from "./stage"
15
+ import type { AgentImage, BundleResult } from "./types"
16
+
17
+ /** Config keys the bundle reads that aren't part of the core authoring type yet. */
18
+ type PublishableConfig = AxonConfig & {
19
+ connections?: Record<string, unknown>
20
+ }
21
+
22
+ type AgentOpts = {
23
+ root: string
24
+ artifacts: ArtifactsT
25
+ stage: StageT
26
+ modules: SourceModulesT
27
+ }
28
+
29
+ /**
30
+ * Agent bundle — .agent/build/image.json + source.tar.gz, the deploy upload.
31
+ *
32
+ * Unlike every other kind, an agent ships PRE-BUILT: its executable brain is
33
+ * compiled in, its local source modules are staged and their imports rebased,
34
+ * and its resolved node_modules ride along because the deploy target boots the
35
+ * extracted tarball directly and never runs an install.
36
+ *
37
+ * NOTE the two layouts in play. Locally the bundle's own outputs live in the
38
+ * frame's `build/` area, while the compiled brain lives in `cognet/` — they
39
+ * are siblings, written by different stages. INSIDE the tarball the brain
40
+ * sits at `.agent/cognet/`, which is a wire contract: `stageCognet()` writes
41
+ * that path into the shipped manifest and a deployed container resolves the
42
+ * brain through it. So the staging step below is a deliberate REMAP, not a
43
+ * copy of the local layout, and the two must be allowed to differ.
44
+ */
45
+ export function Agent(opts: AgentOpts) {
46
+ const { root, artifacts, stage, modules } = opts
47
+ const frame = Frame({ root: root, kind: "agent" })
48
+
49
+ return {
50
+ async build(): Promise<BundleResult> {
51
+ // Before anything is compiled or staged. A config that reaches
52
+ // outside the project cannot be published, and finding that out
53
+ // after a bundle exists costs a build nobody wanted — finding it
54
+ // out in a container costs a provisioned deployment.
55
+ await assertConfigContained(root, join(root, "axon.config.ts"))
56
+
57
+ const loaded = await Config(root)
58
+ const config = loaded.value as PublishableConfig
59
+ const pkg = await artifacts.identity()
60
+
61
+ // Clears assets ONLY. Unlike the other bundlers this must not clear
62
+ // its own outputs wholesale — `.agent/build` holds the cognet cache
63
+ // that the compile below reuses, and wiping it would recompile the
64
+ // brain on every publish. These two entries are named because a
65
+ // deleted asset has no other signal that it should be gone.
66
+ const bundleDir = await artifacts.open(frame.path("build"), [
67
+ "assets.tar.gz",
68
+ ".assets",
69
+ ])
70
+
71
+ // A publishable agent includes its executable brain. Preserve the
72
+ // .agent/cognet cache across bundles, then compile/update it from
73
+ // the selected source cognet before assembling the artifact.
74
+ await Cognet({ root: root }).compile(cognetSourceOf(loaded))
75
+
76
+ const image: AgentImage = {
77
+ kind: "agent",
78
+ agentId: pkg.name,
79
+ version: pkg.version,
80
+ // package.json is the one visibility contract for agents and
81
+ // modules: private:true stays private; absent/false publishes.
82
+ public: pkg.public,
83
+ // Registry description comes from package.json — same source as modules.
84
+ ...(pkg.description ? { description: pkg.description } : {}),
85
+ axonVersion: "0.1.0",
86
+ builtAt: new Date().toISOString(),
87
+ ...(config.connections ? { connections: config.connections } : {}),
88
+ }
89
+ await artifacts.image(bundleDir, image)
90
+
91
+ // Groundwork for self-hosting — AxonCloud deploys never build this
92
+ // Dockerfile (source loads onto an already-established shared image),
93
+ // but the bundle ships one so anyone self-hosting can `docker build`
94
+ // it themselves without us needing to invoke Docker on their behalf.
95
+ await Bun.write(join(bundleDir, "Dockerfile"), dockerfile())
96
+ await Bun.write(join(bundleDir, ".dockerignore"), DOCKERIGNORE)
97
+
98
+ const tarball = join(bundleDir, "source.tar.gz")
99
+ const stageDir = join(bundleDir, ".stage")
100
+ try {
101
+ await mkdir(stageDir, { recursive: true })
102
+ await copyTree(root, stageDir)
103
+ await stageSourceModules(root, stageDir, loaded, modules)
104
+ await stageCognet(frame, bundleDir, stageDir)
105
+
106
+ // `data/knowledge/` and nothing else under data/: knowledge is
107
+ // authored material that defines what the agent knows, while
108
+ // workspace/ is scratch and modules/ is module-managed runtime
109
+ // state — neither belongs to anyone but this one installation.
110
+ // The published-files table in the docs has always said
111
+ // `data/knowledge/ ✓ included`; this is what makes that true.
112
+ const entries = ["src", "server", "modules", INLINE_COGNET_DIR, join("data", "knowledge")]
113
+ .filter(dir => fsx.exists(join(stageDir, dir)))
114
+ // Tarball-relative, matching what stageCognet() laid down.
115
+ entries.push(
116
+ "axon.config.ts",
117
+ "package.json",
118
+ `${frame.name}/Dockerfile`,
119
+ `${frame.name}/.dockerignore`,
120
+ `${frame.name}/${BUNDLE_COGNET_DIR}`,
121
+ )
122
+ // The lockfile is what makes the consumer's install the SAME
123
+ // install: without it `bun install` re-resolves every range and
124
+ // the agent that ships is not the agent that was tested.
125
+ if (fsx.exists(join(stageDir, "bun.lock"))) entries.push("bun.lock")
126
+ if (fsx.exists(join(stageDir, "bunfig.toml"))) entries.push("bunfig.toml")
127
+ if (fsx.exists(join(stageDir, "README.md"))) entries.push("README.md")
128
+
129
+ await stage.tar(stageDir, tarball, entries)
130
+ } finally {
131
+ await rm(stageDir, { recursive: true, force: true })
132
+ }
133
+
134
+ // README assets — their own tarball, never a member of the deploy
135
+ // one. `copyTree` above only takes named source directories, so
136
+ // `assets/` was never IN this tarball; what was missing was the
137
+ // separate archive, which meant an agent's screenshots and demo
138
+ // video silently never reached storage. To their author that reads
139
+ // as a broken registry page rather than as a missing upload.
140
+ //
141
+ // Every publishable kind has a registry page, so every publishable
142
+ // kind carries assets. This was wired into the source bundler first
143
+ // and agents and modules were left behind.
144
+ const assets = await Assets({ root, stage }).collect(bundleDir)
145
+
146
+ return {
147
+ image,
148
+ tarball,
149
+ assets: assets.assets,
150
+ ...(assets.tarball ? { assetsTarball: assets.tarball } : {}),
151
+ }
152
+ },
153
+ }
154
+ }
155
+
156
+ export type AgentBundleT = ReturnType<typeof Agent>
157
+
158
+ // ─── Staging ──────────────────────────────────────────────────────────────────
159
+
160
+ /**
161
+ * Copy the agent's own source and manifests into the stage.
162
+ *
163
+ * Dependencies are DECLARED, not shipped. package.json plus bun.lock is the
164
+ * whole dependency statement, and `bun install` at prepare time (local) or
165
+ * image build time (deploy) materializes it — the same split every other
166
+ * project kind already used, and the same one Installer describes: Axon
167
+ * decides what the agent has, Bun decides how those bytes arrive.
168
+ *
169
+ * This used to stage a dereferenced copy of the resolved node_modules, on the
170
+ * grounds that the deploy target boots the extracted tarball and never runs an
171
+ * install. That constraint is real but belongs to DEPLOY, and publish shares
172
+ * this bundler — so an artifact destined for a machine that can obviously
173
+ * install still carried every byte. For an agent with native dependencies that
174
+ * is fatal rather than wasteful: vox declares onnxruntime and kokoro, whose
175
+ * prebuilds for every platform total ~354MB against a 50MB registry limit, so
176
+ * the agent could not be published at all. Shipping source also removes the
177
+ * publisher's machine from the result — a materialized tree bakes in whichever
178
+ * platform's natives that machine resolved.
179
+ */
180
+ async function copyTree(root: string, stageDir: string): Promise<void> {
181
+ // `cognet` is here because an INLINE brain is the agent's own source, and
182
+ // the most load-bearing source it has. The compiled bundle ships too (see
183
+ // stageCognet) and is what actually runs, but shipping only that would
184
+ // publish an agent whose thinking is an opaque .mjs — unreadable in the
185
+ // registry's source view and uneditable by anyone who clones it. An agent
186
+ // with a registry cognet simply has no such directory, so this is a
187
+ // no-op there.
188
+ for (const dir of ["src", "server", "modules", INLINE_COGNET_DIR]) {
189
+ const source = join(root, dir)
190
+ if (fsx.exists(source)) await cp(source, join(stageDir, dir), { recursive: true })
191
+ }
192
+
193
+ // The inline cognet's tsconfig.json is a generated pointer into THIS
194
+ // machine's frame (`../.agent/types/cognet.tsconfig.json`) — prepare
195
+ // rewrites it for whoever clones the agent, and shipping it means
196
+ // publishing a path that resolves to nothing until they do. The source
197
+ // bundler drops tsconfig.json for exactly this reason; the rule is the
198
+ // same here, it just has to be applied after the recursive copy.
199
+ await rm(join(stageDir, INLINE_COGNET_DIR, "tsconfig.json"), { force: true })
200
+
201
+ // bun.lock pins what install resolves; bunfig.toml carries the @axon scope →
202
+ // registry mapping, without which a consumer's install cannot resolve an
203
+ // Axon module at all.
204
+ for (const file of ["package.json", "README.md", "bun.lock", "bunfig.toml"]) {
205
+ const source = join(root, file)
206
+ if (fsx.exists(source)) await cp(source, join(stageDir, file))
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Stage local source modules and rebase every import that reaches them.
212
+ *
213
+ * Registry modules need no handling: they are ordinary packages in the
214
+ * node_modules copied above. Only source modules — which live outside the
215
+ * agent root — must be copied in and have their config imports rewritten.
216
+ */
217
+ async function stageSourceModules(
218
+ root: string,
219
+ stageDir: string,
220
+ loaded: Awaited<ReturnType<typeof Config>>,
221
+ modules: SourceModulesT,
222
+ ): Promise<void> {
223
+ const configTargets = new Map<string, string>()
224
+ const sourceConfigs: Array<{ source: string; target: string }> = []
225
+ const claimedNames = new Set<string>()
226
+
227
+ for (const entry of await flatten(loaded.modules, loaded.modulePaths)) {
228
+ if (entry.kind !== "source") continue
229
+ const module = await modules.resolve(entry.configPath)
230
+ const dest = join(stageDir, "modules", module.name)
231
+ if (claimedNames.has(module.name) || fsx.exists(join(root, "modules", module.name))) {
232
+ throw err("BUNDLE_MODULE_COLLISION", {
233
+ detail: `source module "${module.name}" collides with an installed module at modules/${module.name}`,
234
+ context: { moduleName: module.name },
235
+ })
236
+ }
237
+ claimedNames.add(module.name)
238
+ await mkdir(join(stageDir, "modules"), { recursive: true })
239
+ await cp(module.root, dest, { recursive: true })
240
+
241
+ const source = resolve(entry.configPath)
242
+ const target = join(dest, "module.config.ts")
243
+ configTargets.set(source, target)
244
+ sourceConfigs.push({ source, target })
245
+ }
246
+
247
+ const sourceAgentConfig = join(root, "axon.config.ts")
248
+ const targetAgentConfig = join(stageDir, "axon.config.ts")
249
+ const agentConfig = await fsx.readText(sourceAgentConfig)
250
+ if (agentConfig === null) throw err("CONFIG_NOT_FOUND", { context: { root } })
251
+ await Bun.write(
252
+ targetAgentConfig,
253
+ rebaseImports(agentConfig, sourceAgentConfig, targetAgentConfig, configTargets),
254
+ )
255
+
256
+ // Source modules can themselves hard-import other source modules. Rebase
257
+ // those config files too, relative to their flattened staged locations, so
258
+ // the whole declared graph remains self-contained.
259
+ for (const config of sourceConfigs) {
260
+ const text = await fsx.readText(config.source)
261
+ if (text === null) continue
262
+ await Bun.write(config.target, rebaseImports(text, config.source, config.target, configTargets))
263
+ }
264
+ }
265
+
266
+ /**
267
+ * Copy the compiled brain into the stage, stripped of build scratch.
268
+ *
269
+ * The REMAP from local layout to tarball layout (see this file's header).
270
+ * Locally the Dockerfile comes out of the frame's `build/` area and the brain
271
+ * out of its `cognet/` area; inside the tarball both sit under `.agent/`,
272
+ * because that is the path a deployed container resolves and it cannot move
273
+ * without breaking agents already in production.
274
+ */
275
+ async function stageCognet(frame: FrameT, bundleDir: string, stageDir: string): Promise<void> {
276
+ const shipped = join(stageDir, frame.name)
277
+ await mkdir(shipped, { recursive: true })
278
+ await cp(join(bundleDir, "Dockerfile"), join(shipped, "Dockerfile"))
279
+ await cp(join(bundleDir, ".dockerignore"), join(shipped, ".dockerignore"))
280
+
281
+ const target = join(shipped, BUNDLE_COGNET_DIR)
282
+ await cp(frame.path("cognet"), target, { recursive: true })
283
+
284
+ // Strip everything the compile step scratches into the cognet dir.
285
+ //
286
+ // `.build` is the bundler's own workspace. `.instances` is per-RUNTIME
287
+ // scratch: core copies the compiled cognet to a unique physical path per
288
+ // booted agent (module scope is one brain's resident RAM, so two runtimes
289
+ // must not share a module instance) and removes it on dispose. A `kill -9`
290
+ // skips that dispose, so a developer's crashed local runs leave copies
291
+ // behind — and they were being published.
292
+ //
293
+ // Shipping them is not cosmetic: each is a multi-hundred-KB duplicate of a
294
+ // file already in the tarball, named with a hash and a UUID from the
295
+ // publisher's machine, and long enough (130 chars) that GNU tar emits a
296
+ // `@LongLink` pseudo-entry for it — which is what surfaced in the
297
+ // registry's file tree as unexplained `@LongLink` rows.
298
+ for (const scratch of [".build", ".instances"]) {
299
+ await rm(join(target, scratch), { recursive: true, force: true })
300
+ }
301
+
302
+ // The brain's path AS THE DEPLOYED CONTAINER WILL SEE IT — built from the
303
+ // same two values that placed the file above, so the manifest cannot
304
+ // describe a location the tarball does not have.
305
+ //
306
+ // `sourceDir` is dropped rather than rewritten: it records where the brain
307
+ // was COMPILED FROM on the publisher's machine, which the dev watcher uses
308
+ // to find source to rebuild. Inside a tarball it is meaningless at best —
309
+ // an absolute path from someone else's disk — and for an inline cognet it
310
+ // names a directory the bundle deliberately does not ship. Publishing a
311
+ // path that resolves to nothing invites a consumer to trust it.
312
+ const manifestPath = join(target, "manifest.json")
313
+ const manifest = await fsx.readJson<Record<string, unknown>>(manifestPath)
314
+ if (!manifest) throw err("BUNDLE_INVALID", { detail: "compiled cognet has no manifest" })
315
+ const { sourceDir: _local, ...portable } = manifest
316
+ const shippedPath = `${frame.name}/${BUNDLE_COGNET_DIR}/cognet.mjs`
317
+ await Bun.write(manifestPath, JSON.stringify({ ...portable, path: shippedPath }, null, 4))
318
+ }
319
+
320
+ /** Rewrite only static imports that resolve to source-module config files we staged. */
321
+ function rebaseImports(
322
+ source: string,
323
+ sourceConfigPath: string,
324
+ targetConfigPath: string,
325
+ targets: Map<string, string>,
326
+ ): string {
327
+ const staticImport = /(\b(?:import|export)\s+(?:[^"'`;]*?\s+from\s+)?)(["'])([^"']+)\2/g
328
+
329
+ return source.replace(staticImport, (statement, prefix: string, quote: string, specifier: string) => {
330
+ if (!specifier.startsWith(".")) return statement
331
+
332
+ const unresolved = resolve(dirname(sourceConfigPath), specifier)
333
+ const target = targets.get(unresolved)
334
+ ?? targets.get(`${unresolved}.ts`)
335
+ ?? targets.get(join(unresolved, "index.ts"))
336
+ if (!target) return statement
337
+
338
+ let rebased = relative(dirname(targetConfigPath), target).split(sep).join("/")
339
+ if (!rebased.startsWith(".")) rebased = `./${rebased}`
340
+ return `${prefix}${quote}${rebased}${quote}`
341
+ })
342
+ }
343
+
344
+ // ─── Self-host groundwork ─────────────────────────────────────────────────────
345
+
346
+ const DOCKERIGNORE = ["node_modules", ".env", ".env.*", "*.log", "source.tar.gz", ""].join("\n")
347
+
348
+ /**
349
+ * A self-contained image for one agent.
350
+ *
351
+ * ── Only for callers that cannot mount ────────────────────────────────────
352
+ *
353
+ * The base image takes the agent's source at `/agent` and never bakes it in —
354
+ * that is its whole design, so the ordinary way to self-host is to mount:
355
+ *
356
+ * docker run -v $PWD:/agent -p 8080:8080 axon/base:<version>
357
+ *
358
+ * No build, no per-agent image, and an agent edit is a restart rather than a
359
+ * rebuild. This Dockerfile exists for the cases where a mount is not available
360
+ * — an air-gapped registry, a k8s deployment that ships one artifact — which is
361
+ * why `axon bundle` does not emit an image unless asked.
362
+ *
363
+ * ── Pinned, not floating ──────────────────────────────────────────────────
364
+ *
365
+ * The tag is the exact base version this CLI was built against. `:latest`
366
+ * would mean a rebuild months later silently gets a different runtime under an
367
+ * agent that was working, which is the class of failure immutable versions
368
+ * exist to prevent.
369
+ *
370
+ * It previously read `FROM axon-base:local` — an image in a PRIVATE registry
371
+ * that no user could pull. `docker build` on it failed with "pull access
372
+ * denied", so the self-host path it advertised had never worked.
373
+ */
374
+ function dockerfile(): string {
375
+ return [
376
+ `FROM ${axonBaseRef()}`,
377
+ "",
378
+ "# The base image reads the agent from /agent — see @axon/docker's Hydrate.",
379
+ "COPY . /agent",
380
+ "",
381
+ ].join("\n")
382
+ }
@@ -0,0 +1,88 @@
1
+ import { join } from "node:path"
2
+ import { mkdir, rm } from "node:fs/promises"
3
+ import { err } from "@arcforge/err"
4
+ import type { ManifestT } from "../manifest"
5
+
6
+ type ArtifactsOpts = {
7
+ root: string
8
+ manifest: ManifestT
9
+ }
10
+
11
+ /**
12
+ * Artifacts — the files a bundle writes into the project's `build/` area.
13
+ *
14
+ * The bundle used to write into the frame ROOT, alongside the prepared
15
+ * authoring frame (globals.d.ts, tsconfig.json), which forced a rule on it:
16
+ * clear only its OWN artifacts by name, never the directory, or an otherwise
17
+ * successful publish would delete the editor's types. Now that generated
18
+ * types live in `types/` and bundles in `build/`, the two cannot collide —
19
+ * the clear list survives as a narrower guarantee (a failed publish leaves no
20
+ * stale tarball) rather than as the thing standing between publish and a
21
+ * broken editor.
22
+ *
23
+ * Identity comes from Manifest — this does NOT read package.json itself. That
24
+ * distinction is the boundary: Manifest owns the project's declaration files;
25
+ * this owns the derived artifacts written beside them.
26
+ */
27
+ export function Artifacts(opts: ArtifactsOpts) {
28
+ const { root, manifest } = opts
29
+
30
+ return {
31
+ /**
32
+ * The project's publishable identity. Throws when package.json is
33
+ * absent or unnamed — neither is publishable.
34
+ */
35
+ async identity() {
36
+ const pkg = await manifest.package.read().catch(() => null)
37
+ if (!pkg) {
38
+ throw err("BUNDLE_INVALID", { detail: `no package.json at ${root}`, context: { root } })
39
+ }
40
+ if (!pkg.name) {
41
+ throw err("BUNDLE_INVALID", { detail: `package.json at ${root} has no name`, context: { root } })
42
+ }
43
+
44
+ return {
45
+ name: pkg.name,
46
+ version: pkg.version ?? "0.0.0",
47
+ description: pkg.description,
48
+ // npm convention: "private": true means private; absent/false means public.
49
+ public: pkg.private !== true,
50
+ raw: pkg,
51
+ }
52
+ },
53
+
54
+ /**
55
+ * Prepare the bundle directory, clearing only this bundle's own
56
+ * outputs. Takes an ABSOLUTE path — callers resolve it through
57
+ * `Frame().path("build")` rather than passing a fragment this has to
58
+ * join, so there is one authority on where a bundle lands.
59
+ *
60
+ * `recursive` so an entry naming a DIRECTORY clears too — the source
61
+ * bundler's `.assets` staging area is one, and a non-recursive rm
62
+ * throws EISDIR on it rather than removing it. Still by name, so the
63
+ * guarantee is unchanged: nothing outside the clear list is touched.
64
+ */
65
+ async open(bundleDir: string, clear: string[] = []): Promise<string> {
66
+ await mkdir(bundleDir, { recursive: true })
67
+ await Promise.all(clear.map(file => rm(join(bundleDir, file), { force: true, recursive: true })))
68
+ return bundleDir
69
+ },
70
+
71
+ /** Write image.json — the artifact record every kind ships. */
72
+ async image(bundleDir: string, image: unknown): Promise<void> {
73
+ await Bun.write(join(bundleDir, "image.json"), JSON.stringify(image, null, 2) + "\n")
74
+ },
75
+
76
+ /** Write the package.json copy the registry reads name/version/visibility from. */
77
+ async package(bundleDir: string, raw: unknown): Promise<void> {
78
+ await Bun.write(join(bundleDir, "package.json"), JSON.stringify(raw, null, 2) + "\n")
79
+ },
80
+
81
+ /** Write an arbitrary named JSON artifact into the bundle dir (module manifests). */
82
+ async json(bundleDir: string, name: string, value: unknown): Promise<void> {
83
+ await Bun.write(join(bundleDir, name), JSON.stringify(value, null, 2) + "\n")
84
+ },
85
+ }
86
+ }
87
+
88
+ export type ArtifactsT = ReturnType<typeof Artifacts>