@arcforge/platform 2.0.191 → 2.0.192

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcforge/platform",
3
- "version": "2.0.191",
3
+ "version": "2.0.192",
4
4
  "description": "The Axon developer platform — headless project discovery, blueprint scanning, typegen, agent lifecycle, registry, benches and tests. Shared by the CLI/TUI and the Fleet extension.",
5
5
  "type": "module",
6
6
  "private": false,
@@ -44,20 +44,20 @@
44
44
  "deploy": "echo \"This package is published ONLY by apps/tui/scripts/release.ts, which pins workspace:* deps to concrete versions first. Publishing it directly ships an unresolvable dependency.\" && exit 1"
45
45
  },
46
46
  "dependencies": {
47
- "@arcforge/session": "2.0.191",
48
- "@arcforge/engines": "2.0.191",
49
- "@arcforge/err": "2.0.191",
50
- "@arcforge/types": "2.0.191",
51
- "@arcforge/cloud": "2.0.191",
52
- "@arcforge/capsule": "2.0.191",
53
- "@arcforge/core": "2.0.191",
54
- "@arcforge/vstr": "2.0.191",
47
+ "@arcforge/session": "2.0.192",
48
+ "@arcforge/engines": "2.0.192",
49
+ "@arcforge/err": "2.0.192",
50
+ "@arcforge/types": "2.0.192",
51
+ "@arcforge/cloud": "2.0.192",
52
+ "@arcforge/capsule": "2.0.192",
53
+ "@arcforge/core": "2.0.192",
54
+ "@arcforge/vstr": "2.0.192",
55
55
  "fft.js": "^4.0.4",
56
56
  "h3": "^1.15.11",
57
57
  "semver": "^7.7.4",
58
58
  "sharp": "^0.35.3",
59
59
  "typescript": "5.9.2",
60
- "@arcforge/link": "2.0.191"
60
+ "@arcforge/link": "2.0.192"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@arcforge/air": "2.0.187",
@@ -1,3 +1,4 @@
1
+ import { workerPath } from "../utils/packaged"
1
2
  import { AsyncLocalStorage } from "node:async_hooks"
2
3
  import { existsSync } from "node:fs"
3
4
  import { resolve } from "node:path"
@@ -283,10 +284,7 @@ Object.assign(globalThis, instrumented)
283
284
  // globalThis alone does not replace them. Transform only the explicitly
284
285
  // selected test file in memory — user source is never rewritten on disk.
285
286
  const target = resolve(process.cwd(), file)
286
- const packagedApi = resolve(import.meta.dir, "test-api.js")
287
- const apiUrl = pathToFileURL(existsSync(packagedApi)
288
- ? packagedApi
289
- : resolve(import.meta.dir, "test-api.ts")).href
287
+ const apiUrl = pathToFileURL(workerPath(import.meta.dir, "test-api.js", "test-api.ts")).href
290
288
  const injected = `import { describe, test, it, xdescribe, xtest, xit, beforeAll, beforeEach, afterEach, afterAll } from ${JSON.stringify(apiUrl)};\n`
291
289
  const targetFilter = new RegExp(`^${target.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`)
292
290
 
@@ -1,3 +1,4 @@
1
+ import { workerPath } from "../../utils/packaged"
1
2
  import { join, resolve } from "node:path"
2
3
  import { existsSync } from "node:fs"
3
4
  import type {
@@ -131,10 +132,7 @@ export function Runner(opts: RunnerOpts) {
131
132
  const requiredIds = new Set(config.measurements.filter(item => item.required).map(item => item.id))
132
133
  const measured = new Set<string>()
133
134
  const measurementStates: BenchMeasurementState[] = []
134
- const packagedPreload = resolve(import.meta.dir, "bench-preload.js")
135
- const preload = existsSync(packagedPreload)
136
- ? packagedPreload
137
- : resolve(import.meta.dir, "preload.ts")
135
+ const preload = workerPath(import.meta.dir, "bench-preload.js", "preload.ts")
138
136
 
139
137
  try {
140
138
  for (let cellIndex = 0; cellIndex < coordinates.length && !budgetExhausted; cellIndex++) {
@@ -40,14 +40,25 @@ import type { BenchMeasurementDefinition, BenchMeasurementReducer, BenchMeasurem
40
40
  * prepare — the author writes a type, the system keeps a value.
41
41
  */
42
42
 
43
- const COMPILER_OPTIONS: ts.CompilerOptions = {
44
- allowJs: false,
45
- noEmit: true,
46
- target: tsc().ScriptTarget.ESNext,
47
- module: tsc().ModuleKind.ESNext,
48
- moduleResolution: tsc().ModuleResolutionKind.Bundler,
49
- skipLibCheck: true,
50
- strict: true,
43
+ /*
44
+ * Built on demand, not at module scope.
45
+ *
46
+ * `tsc()` is a lazy accessor, and calling it in a module-scope initializer
47
+ * defeated it entirely: importing this file loaded the 17MB TypeScript
48
+ * compiler, whether or not anything ever declared a tool. Same defect as
49
+ * air/output.ts's COMPILER_OPTIONS — a lazy loader is only lazy if every
50
+ * caller is inside a function.
51
+ */
52
+ function compilerOptions(): ts.CompilerOptions {
53
+ return {
54
+ allowJs: false,
55
+ noEmit: true,
56
+ target: tsc().ScriptTarget.ESNext,
57
+ module: tsc().ModuleKind.ESNext,
58
+ moduleResolution: tsc().ModuleResolutionKind.Bundler,
59
+ skipLibCheck: true,
60
+ strict: true,
61
+ }
51
62
  }
52
63
 
53
64
  /** Aggregation follows from the kind unless the author says otherwise. */
@@ -59,7 +70,7 @@ const DEFAULT_AGGREGATE: Record<BenchMeasurementValueDefinition["kind"], BenchMe
59
70
  }
60
71
 
61
72
  export function extractBenchSchema(configPath: string): BenchMeasurementDefinition[] {
62
- const program = tsc().createProgram([configPath], COMPILER_OPTIONS)
73
+ const program = tsc().createProgram([configPath], compilerOptions())
63
74
  const source = program.getSourceFile(configPath)
64
75
  if (!source) throw err("BENCH_SCHEMA_UNREADABLE", { detail: configPath, context: { configPath } })
65
76
 
@@ -1,3 +1,4 @@
1
+ import { workerPath } from "../../../utils/packaged"
1
2
  import { createHash } from "node:crypto"
2
3
  import { existsSync } from "node:fs"
3
4
  import { mkdir } from "node:fs/promises"
@@ -37,10 +38,7 @@ export function BenchSchema(opts: { root: string }) {
37
38
  }
38
39
 
39
40
  async function extract(): Promise<BenchMeasurementDefinition[]> {
40
- const packaged = resolve(import.meta.dir, "bench-schema-worker.js")
41
- const worker = existsSync(packaged)
42
- ? packaged
43
- : resolve(import.meta.dir, "worker.ts")
41
+ const worker = workerPath(import.meta.dir, "bench-schema-worker.js", "worker.ts")
44
42
  const proc = Bun.spawn(["bun", "run", worker, configPath], { stdout: "pipe", stderr: "pipe" })
45
43
  const [stdout, stderr, code] = await Promise.all([
46
44
  new Response(proc.stdout).text(),
@@ -1,3 +1,4 @@
1
+ import { workerPath } from "../../../utils/packaged"
1
2
  import { createHash } from "node:crypto"
2
3
  import { readdir, rename, rm } from "node:fs/promises"
3
4
  import { existsSync, lstatSync, realpathSync } from "node:fs"
@@ -19,10 +20,7 @@ import { readCognetAbi } from "./abi"
19
20
  // resolution pattern as declare-worker/tool-bundle-worker — without it the
20
21
  // installed CLI looks for a .ts that was never published and every agent
21
22
  // fails to compile its brain.
22
- const packagedWorker = resolve(import.meta.dir, "bundle-worker.js")
23
- const BUNDLE_WORKER_PATH = existsSync(packagedWorker)
24
- ? packagedWorker
25
- : resolve(import.meta.dir, "bundle-worker.ts")
23
+ const BUNDLE_WORKER_PATH = workerPath(import.meta.dir, "bundle-worker.js", "bundle-worker.ts")
26
24
 
27
25
  type BundleWorkerResult =
28
26
  | { ok: true; code: string; mapCode?: string }
@@ -34,20 +34,31 @@ export type DeclaredFile = {
34
34
  ambientTypes: string[]
35
35
  }
36
36
 
37
- const COMPILER_OPTIONS: ts.CompilerOptions = {
38
- declaration: true,
39
- emitDeclarationOnly: true,
40
- target: tsc().ScriptTarget.ESNext,
41
- module: tsc().ModuleKind.ESNext,
42
- moduleResolution: tsc().ModuleResolutionKind.Bundler,
43
- strict: true,
44
- skipLibCheck: true,
45
- // Declaration emit fails loudly on an implicit any parameter otherwise —
46
- // tool authors write plain, unannotated JS-shaped TS; forcing strict
47
- // parameter typing here would make the SCANNER reject code the runtime
48
- // happily executes. Emission still infers real return types either way.
49
- noImplicitAny: false,
50
- allowImportingTsExtensions: true,
37
+ /*
38
+ * Built on demand, not at module scope.
39
+ *
40
+ * `tsc()` is a lazy accessor, and calling it in a module-scope initializer
41
+ * defeated it entirely: importing this file loaded the 17MB TypeScript
42
+ * compiler, whether or not anything ever declared a tool. Same defect as
43
+ * air/output.ts's COMPILER_OPTIONS — a lazy loader is only lazy if every
44
+ * caller is inside a function.
45
+ */
46
+ function compilerOptions(): ts.CompilerOptions {
47
+ return {
48
+ declaration: true,
49
+ emitDeclarationOnly: true,
50
+ target: tsc().ScriptTarget.ESNext,
51
+ module: tsc().ModuleKind.ESNext,
52
+ moduleResolution: tsc().ModuleResolutionKind.Bundler,
53
+ strict: true,
54
+ skipLibCheck: true,
55
+ // Declaration emit fails loudly on an implicit any parameter otherwise —
56
+ // tool authors write plain, unannotated JS-shaped TS; forcing strict
57
+ // parameter typing here would make the SCANNER reject code the runtime
58
+ // happily executes. Emission still infers real return types either way.
59
+ noImplicitAny: false,
60
+ allowImportingTsExtensions: true,
61
+ }
51
62
  }
52
63
 
53
64
  /**
@@ -97,12 +108,23 @@ const COMPILER_OPTIONS: ts.CompilerOptions = {
97
108
  */
98
109
  const libFiles = new Map<string, ts.SourceFile | undefined>()
99
110
 
100
- /** The directory TypeScript's own lib.*.d.ts files ship in. */
101
- const libDir = normalize(tsc().getDefaultLibFilePath(COMPILER_OPTIONS).replace(/[^/\\]+$/, ""))
111
+ /**
112
+ * The directory TypeScript's own lib.*.d.ts files ship in.
113
+ *
114
+ * Resolved on first use, not at module scope — asking the compiler where its
115
+ * libs live requires loading the compiler, which is the 17MB this file exists
116
+ * to defer. Cached because it cannot change within a process.
117
+ */
118
+ let _libDir: string | null = null
119
+
120
+ function libDir(): string {
121
+ _libDir ??= normalize(tsc().getDefaultLibFilePath(compilerOptions()).replace(/[^/\\]+$/, ""))
122
+ return _libDir
123
+ }
102
124
 
103
125
  /** True for a path inside TypeScript's own lib directory. */
104
126
  function isLibFile(fileName: string): boolean {
105
- return fileName.startsWith(libDir)
127
+ return fileName.startsWith(libDir())
106
128
  }
107
129
 
108
130
  /**
@@ -142,7 +164,7 @@ function unshadow(path: string): string {
142
164
  export function declareTools(fileNames: string[]): Map<string, DeclaredFile> {
143
165
  const emitted = new Map<string, string>()
144
166
 
145
- const host = tsc().createCompilerHost(COMPILER_OPTIONS)
167
+ const host = tsc().createCompilerHost(compilerOptions())
146
168
 
147
169
  // Reads follow the shadow back to the real file on disk. Everything above
148
170
  // this line thinks in shadowed paths; everything below it, and the whole
@@ -199,7 +221,7 @@ export function declareTools(fileNames: string[]): Map<string, DeclaredFile> {
199
221
  // Emitted paths are mapped back, so every caller sees real locations.
200
222
  host.writeFile = (fileName, text) => emitted.set(normalize(unshadow(fileName)), text)
201
223
 
202
- const program = tsc().createProgram(fileNames.map(shadow), COMPILER_OPTIONS, host)
224
+ const program = tsc().createProgram(fileNames.map(shadow), compilerOptions(), host)
203
225
  program.emit()
204
226
  const result = new Map<string, DeclaredFile>()
205
227
 
@@ -1,3 +1,4 @@
1
+ import { workerPath } from "../../../utils/packaged"
1
2
  import { createHash } from "node:crypto"
2
3
  import { existsSync, mkdirSync } from "node:fs"
3
4
  import { rename, rm } from "node:fs/promises"
@@ -14,27 +15,26 @@ import { publishedTools } from "./published"
14
15
  * The subprocess entry that actually runs declareTools() — see its own doc
15
16
  * comment for why this isn't inline.
16
17
  *
17
- * The published CLI is a single bundled app.js, so `import.meta.dir` is the
18
- * package root there, not this source directory. Like the capsule process
19
- * and the update helper, the worker is bundled beside it and preferred when
20
- * present; the .ts source is the workspace/development path.
18
+ * Resolved through `workerPath()`, which knows both shipped layouts: the
19
+ * worker sits beside the app entry, and the code asking for it may be in the
20
+ * entry itself or in `chunks/` one level down (the app bundle is built with
21
+ * `splitting: true`). The .ts source is the workspace/development path.
22
+ *
23
+ * This comment previously read "the published CLI is a single bundled app.js,
24
+ * so import.meta.dir is the package root" — true when written, and the exact
25
+ * assumption splitting invalidated. Six call sites each held their own copy of
26
+ * it, which is why none of them was checked when it changed.
21
27
  *
22
28
  * Getting this wrong is silent in development and total in production —
23
29
  * every tool declaration in every installed agent fails, which is exactly
24
30
  * what happened before the worker was added to packageFiles.
25
31
  */
26
- const packagedWorker = resolve(import.meta.dir, "declare-worker.js")
27
- const DECLARE_WORKER_PATH = existsSync(packagedWorker)
28
- ? packagedWorker
29
- : resolve(import.meta.dir, "declare-worker.ts")
32
+ const DECLARE_WORKER_PATH = workerPath(import.meta.dir, "declare-worker.js", "declare-worker.ts")
30
33
 
31
- // Same packaged-vs-source resolution as the declare worker: in the published
32
- // single-file CLI the worker is bundled beside app.js; in the workspace it is
34
+ // Same packaged-vs-source resolution as the declare worker: in a published
35
+ // install the worker is bundled beside app.js; in the workspace it is
33
36
  // the .ts source. Getting this wrong is silent in dev and total in production.
34
- const packagedToolBundleWorker = resolve(import.meta.dir, "tool-bundle-worker.js")
35
- const TOOL_BUNDLE_WORKER_PATH = existsSync(packagedToolBundleWorker)
36
- ? packagedToolBundleWorker
37
- : resolve(import.meta.dir, "tool-bundle-worker.ts")
37
+ const TOOL_BUNDLE_WORKER_PATH = workerPath(import.meta.dir, "tool-bundle-worker.js", "tool-bundle-worker.ts")
38
38
 
39
39
  type DeclareWorkerResult =
40
40
  | { ok: true; files: [string, DeclaredFile][] }
@@ -126,7 +126,7 @@ export function Projects(opts: ProjectsOpts) {
126
126
  const report = input.onProgress ?? (() => {})
127
127
 
128
128
  report({ step: "scaffolding" })
129
- const root = await KINDS[kind].scaffold({
129
+ const scaffolded = await KINDS[kind].scaffold({
130
130
  name: input.name,
131
131
  dir: input.dir ?? process.cwd(),
132
132
  frameworkVersion: opts.frameworkVersion,
@@ -134,12 +134,47 @@ export function Projects(opts: ProjectsOpts) {
134
134
  ...(opts.repoRoot ? { repoRoot: opts.repoRoot } : {}),
135
135
  ...(input.apiBase !== undefined ? { apiBase: input.apiBase } : {}),
136
136
  })
137
+
138
+ /*
139
+ * What was scaffolded is not always a project of its own.
140
+ *
141
+ * `axon cognet init` inside an agent writes an INLINE cognet at
142
+ * <agent>/cognet/ — deliberately with no package.json, because it
143
+ * is part of the agent rather than a second publishable package.
144
+ * `detectKind()` refuses to claim such a directory, equally
145
+ * deliberately. So opening the scaffolded path threw
146
+ * PROJECT_NOT_FOUND against a directory this function had just
147
+ * written, one line after reporting success: the scaffolder was
148
+ * right and the step after it had not been told.
149
+ *
150
+ * The project to open — and to PREPARE, since an inline cognet is
151
+ * compiled from the agent's node_modules into the agent's frame —
152
+ * is the one that OWNS the scaffolded path. For every other kind
153
+ * that owner is the scaffolded path itself.
154
+ */
155
+ const root = detectKind(scaffolded) ? scaffolded : find(scaffolded)
156
+ if (!root) {
157
+ throw err("PROJECT_NOT_FOUND", {
158
+ detail: `scaffolded ${scaffolded} but found no project at or above it`,
159
+ context: { path: scaffolded, kind },
160
+ })
161
+ }
137
162
  const project = await open(root)
138
163
 
139
164
  report({ step: "preparing", root })
140
165
  await project.prepare()
141
166
 
142
- report({ step: "created", root, name: project.name })
167
+ /*
168
+ * The step describes WHAT WAS CREATED, which for an inline cognet
169
+ * is the cognet — not the agent that now owns it. Reporting the
170
+ * agent's root and name here would tell a user who asked for a
171
+ * cognet that they had made an agent.
172
+ */
173
+ report({
174
+ step: "created",
175
+ root: scaffolded,
176
+ name: root === scaffolded ? project.name : input.name,
177
+ })
143
178
  return project
144
179
  },
145
180
  }
@@ -712,9 +712,28 @@ export type ProfileT = ReturnType<StoreT["profiles"]["get"]>
712
712
  * Development uses a SEPARATE directory (.axon-dev): an installed production
713
713
  * app and a source checkout must never share credentials, or logging into
714
714
  * staging from source silently re-points the installed app.
715
+ *
716
+ * `AXON_STORE_ROOT` overrides both, and exists for ONE reason: test isolation.
717
+ * Most tests pass an explicit `store:` to `Platform()`, but the TUI composable
718
+ * tests go through `usePlatform()`, which constructs the platform the way the
719
+ * real app does — with no override. Those tests therefore read and WROTE the
720
+ * developer's actual profile: a theme unit test called `theme.set()` and
721
+ * persisted `theme: "test-reset-theme"` into a real `profile.config.ts`, which
722
+ * is the user's own file.
723
+ *
724
+ * An env var rather than another `Platform()` option because the escape has to
725
+ * close for code that never threads an option through — the whole failure was
726
+ * a call site that legitimately did not know it was in a test.
715
727
  */
716
728
  export function storeRoot(distribution: Distribution): string {
717
- return join(homedir(), distribution === "production" ? ".axon" : ".axon-dev")
729
+ // The override RELOCATES the pair; it does not merge them. Returning it
730
+ // directly made production and development share one directory, which is
731
+ // the exact invariant the paragraph above exists to state — and
732
+ // `environment.test.ts` caught it immediately. An isolation seam that can
733
+ // switch off a safety property is a worse bug than the one it fixes.
734
+ const override = process.env.AXON_STORE_ROOT
735
+ const leaf = distribution === "production" ? ".axon" : ".axon-dev"
736
+ return join(override ?? homedir(), leaf)
718
737
  }
719
738
 
720
739
  /** Immediate subdirectory names of an absolute path outside ~/.axon. Missing/non-dir = []. */
@@ -1,3 +1,4 @@
1
+ import { workerPath } from "../../utils/packaged"
1
2
  import { err } from "@arcforge/err"
2
3
  import { existsSync } from "node:fs"
3
4
  import { resolve } from "node:path"
@@ -17,10 +18,7 @@ const DISCONNECT_GRACE_MS = 100
17
18
  * --preload` — and is never imported by this one. Packaged builds ship it as a
18
19
  * sibling .js (see the TUI's vterm.config.ts); a source checkout runs the .ts.
19
20
  */
20
- const packagedPreload = resolve(import.meta.dir, "test-preload.js")
21
- const PRELOAD = existsSync(packagedPreload)
22
- ? packagedPreload
23
- : resolve(import.meta.dir, "../../bin/test-preload.ts")
21
+ const PRELOAD = workerPath(import.meta.dir, "test-preload.js", "../../bin/test-preload.ts")
24
22
 
25
23
  export type ChildOutcome = {
26
24
  exitCode: number
@@ -0,0 +1,56 @@
1
+ import { existsSync } from "node:fs"
2
+ import { resolve } from "node:path"
3
+
4
+ /**
5
+ * Where the sibling executables live — workers, preloads, the update helper.
6
+ *
7
+ * ── Why this exists ─────────────────────────────────────────────────────────
8
+ *
9
+ * Several modules spawn a subprocess from a file shipped beside the app, and
10
+ * each resolved it the same way: `resolve(import.meta.dir, "worker.js")`,
11
+ * falling back to the `.ts` source in the workspace. That worked because the
12
+ * published CLI was ONE bundled `app.js`, so `import.meta.dir` was the package
13
+ * root — a fact those call sites each stated in their own comment.
14
+ *
15
+ * Enabling `splitting` broke it. The entry is now a few hundred KB and the rest
16
+ * lives in `chunks/`, so the code doing the resolving sits one directory deeper
17
+ * and `import.meta.dir` points at `chunks/`. Every worker path missed, and the
18
+ * failure was total rather than partial: `axon bench init` died with
19
+ * `Module not found ".../chunks/worker.ts"`.
20
+ *
21
+ * ── Why a helper rather than six fixed call sites ───────────────────────────
22
+ *
23
+ * The six copies were already the bug: one shared assumption, restated in six
24
+ * places, so nothing checked them together when the assumption changed. A
25
+ * seventh worker added tomorrow would copy whichever neighbour it was written
26
+ * next to. This is the one place that knows how the artifact is laid out, and
27
+ * the layout can change again without hunting call sites.
28
+ *
29
+ * Deliberately NOT clever: it checks the directory the caller is in, then its
30
+ * parent. That covers the single-file layout (worker beside the entry) and the
31
+ * split layout (caller in `chunks/`, worker beside the entry one level up),
32
+ * and it stops there — a search that walked further could find a stale worker
33
+ * from an unrelated install, which is worse than failing.
34
+ */
35
+ export function packagedPath(fromDir: string, name: string): string | null {
36
+ const beside = resolve(fromDir, name)
37
+ if (existsSync(beside)) return beside
38
+
39
+ // Split layout: the caller is inside `chunks/`, the worker is beside the entry.
40
+ const up = resolve(fromDir, "..", name)
41
+ if (existsSync(up)) return up
42
+
43
+ return null
44
+ }
45
+
46
+ /**
47
+ * The packaged executable if it shipped, else the workspace source.
48
+ *
49
+ * The two-name shape is the norm at every call site: `worker.js` in a published
50
+ * install, `worker.ts` when running from the repo. Returning the source path
51
+ * unconditionally when nothing is packaged preserves the previous behaviour —
52
+ * a missing file fails at spawn, where the error names the path.
53
+ */
54
+ export function workerPath(fromDir: string, packagedName: string, sourceName: string): string {
55
+ return packagedPath(fromDir, packagedName) ?? resolve(fromDir, sourceName)
56
+ }