@nanobpm/urban 0.96.1 → 0.96.3

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.
@@ -0,0 +1,172 @@
1
+ // Module-load laziness guard for nanobpm/nano-ide#592: the CLI used to import the whole
2
+ // toolkit (`create-urban-app`, `./toolkit/index.ts`) and the runtime barrel at the top of
3
+ // cli.ts, so EVERY command — including the per-data-op `urban data` gateway — paid the full
4
+ // load before dispatching. On small hosts that load is ~90% of a command's cost (measured:
5
+ // ~2s CPU on a 4-core Atom vs ~0.1s bare Node start-up). The fix keeps only argument parsing
6
+ // and stdio/guard helpers at the top level and dynamically imports each command's modules
7
+ // inside its handler.
8
+ //
9
+ // These tests pin the laziness per command: they spawn the real CLI entrypoint with a
10
+ // `--import` preload that registers a module-resolution hook recording every resolved URL,
11
+ // then assert which module trees the command did NOT load.
12
+
13
+ import { test } from "node:test";
14
+ import assert from "node:assert/strict";
15
+ import { spawn } from "node:child_process";
16
+ import { mkdtemp, writeFile, rm } from "node:fs/promises";
17
+ import { tmpdir } from "node:os";
18
+ import { join } from "node:path";
19
+ import { fileURLToPath, pathToFileURL } from "node:url";
20
+ import { isRecord } from "./runtime/core/guards.ts";
21
+
22
+ const cli = fileURLToPath(new URL("./cli.ts", import.meta.url));
23
+
24
+ // Spawning with --experimental-strip-types runs the TypeScript source directly, so the test
25
+ // exercises the authored cli.ts (no build step needed) — same as the >64 KB stdout test above.
26
+ // bin.mjs is Node-only, so skip under Deno (`test:deno`), whose process.execPath is not Node.
27
+ const runtimeIsNode = !("Deno" in globalThis);
28
+
29
+ interface RunResult {
30
+ stdout: string;
31
+ stderr: string;
32
+ code: number | null;
33
+ resolved: string[];
34
+ }
35
+
36
+ /**
37
+ * Spawn `cli.ts <args>` with a preload that records every module URL the process resolves.
38
+ * The preload registers a resolve hook (via `module.register`) on a loader thread that
39
+ * appends each resolved URL to a file; the parent then reads that file.
40
+ */
41
+ async function runCliRecordingModules(
42
+ dir: string,
43
+ args: string[],
44
+ stdin: string,
45
+ ): Promise<RunResult> {
46
+ const resolvedFile = join(dir, "resolved.txt");
47
+ await writeFile(resolvedFile, "");
48
+ const hooks = join(dir, "hooks.mjs");
49
+ await writeFile(
50
+ hooks,
51
+ [
52
+ 'import { appendFileSync } from "node:fs";',
53
+ `const file = ${JSON.stringify(resolvedFile)};`,
54
+ "export async function resolve(specifier, context, nextResolve) {",
55
+ " const result = await nextResolve(specifier, context);",
56
+ " try { appendFileSync(file, result.url + \"\\n\"); } catch {}",
57
+ " return result;",
58
+ "}",
59
+ "",
60
+ ].join("\n"),
61
+ );
62
+ const preload = join(dir, "preload.mjs");
63
+ await writeFile(
64
+ preload,
65
+ [
66
+ 'import { register } from "node:module";',
67
+ `register(${JSON.stringify(pathToFileURL(hooks).href)});`,
68
+ "",
69
+ ].join("\n"),
70
+ );
71
+
72
+ const { stdout, stderr, code } = await new Promise<{
73
+ stdout: Buffer;
74
+ stderr: Buffer;
75
+ code: number | null;
76
+ }>((resolve, reject) => {
77
+ const child = spawn(
78
+ process.execPath,
79
+ ["--experimental-strip-types", `--import=${pathToFileURL(preload).href}`, cli, ...args],
80
+ { stdio: ["pipe", "pipe", "pipe"] },
81
+ );
82
+ const out: Buffer[] = [];
83
+ const err: Buffer[] = [];
84
+ child.stdout.on("data", (c: Buffer) => out.push(c));
85
+ child.stderr.on("data", (c: Buffer) => err.push(c));
86
+ child.on("error", reject);
87
+ child.on("close", (code) =>
88
+ resolve({ stdout: Buffer.concat(out), stderr: Buffer.concat(err), code }),
89
+ );
90
+ child.stdin.end(stdin);
91
+ });
92
+
93
+ const { readFile } = await import("node:fs/promises");
94
+ const resolved = (await readFile(resolvedFile, "utf8")).split("\n").filter((l) => l.length > 0);
95
+ return { stdout: stdout.toString("utf8"), stderr: stderr.toString("utf8"), code, resolved };
96
+ }
97
+
98
+ /** Resolved URLs under the given package path fragments (the heavy module trees of #592). */
99
+ function heavyLoads(resolved: string[]): string[] {
100
+ return resolved.filter(
101
+ (u) =>
102
+ // Node resolves a workspace package's node_modules symlink to its real target, so in a
103
+ // monorepo checkout `create-urban-app` resolves under `packages/create-urban-app/` — match
104
+ // both the installed and the workspace-real shape or a scaffold regression slips through.
105
+ u.includes("/node_modules/create-urban-app/") ||
106
+ u.includes("/packages/create-urban-app/") ||
107
+ u.includes("/dist/toolkit/") ||
108
+ u.includes("/src/toolkit/"),
109
+ );
110
+ }
111
+
112
+ test("urban data does not load the toolkit, scaffold or gen modules", {
113
+ skip: runtimeIsNode ? false : "Node-only: spawns cli.ts via process.execPath (Deno's execPath is not Node)",
114
+ }, async () => {
115
+ const dir = await mkdtemp(join(tmpdir(), "urban-cli-lazy-data-"));
116
+ try {
117
+ // A parseable request, so cmdData gets past JSON.parse and actually runs the command-specific
118
+ // dynamic imports (host detector + data-op runner) — malformed JSON would return from the
119
+ // parse-error branch first and prove nothing about the data path's module graph. The root has
120
+ // no manifest, so the op still fails; the CLI answers with its parseable envelope and exits 0.
121
+ const { stdout, stderr, code, resolved } = await runCliRecordingModules(
122
+ dir,
123
+ ["data", "--root", dir],
124
+ JSON.stringify({ op: "sources" }),
125
+ );
126
+ assert.equal(code, 0, `CLI exited ${code}, expected 0. stderr:\n${stderr}`);
127
+ const reply: unknown = JSON.parse(stdout);
128
+ assert.ok(isRecord(reply));
129
+ assert.equal(reply.ok, false);
130
+
131
+ const heavy = heavyLoads(resolved);
132
+ assert.deepEqual(
133
+ heavy,
134
+ [],
135
+ `urban data eagerly loaded toolkit/scaffold modules:\n${heavy.join("\n")}`,
136
+ );
137
+ } finally {
138
+ await rm(dir, { recursive: true, force: true });
139
+ }
140
+ });
141
+
142
+ test("urban --version loads neither the toolkit nor the runtime modules", {
143
+ skip: runtimeIsNode ? false : "Node-only: spawns cli.ts via process.execPath (Deno's execPath is not Node)",
144
+ }, async () => {
145
+ const dir = await mkdtemp(join(tmpdir(), "urban-cli-lazy-version-"));
146
+ try {
147
+ const { stdout, stderr, code, resolved } = await runCliRecordingModules(dir, ["--version"], "");
148
+ assert.equal(code, 0, `CLI exited ${code}, expected 0. stderr:\n${stderr}`);
149
+ assert.match(stdout, /^\d+\.\d+\.\d+/);
150
+
151
+ // The only runtime-tree modules the CLI itself may touch eagerly are its two tiny
152
+ // stdio/guard helpers — everything else under runtime/ (the barrel, run.ts, the data-op
153
+ // modules, the engine adapters, …) must stay unloaded until a command needs it.
154
+ const allowed = new Set(["/src/runtime/adapters/globals.ts", "/src/runtime/core/guards.ts"]);
155
+ const heavy = resolved.filter(
156
+ (u) =>
157
+ u.includes("/node_modules/create-urban-app/") ||
158
+ u.includes("/packages/create-urban-app/") ||
159
+ u.includes("/dist/toolkit/") ||
160
+ u.includes("/src/toolkit/") ||
161
+ ((u.includes("/dist/runtime/") || u.includes("/src/runtime/")) &&
162
+ ![...allowed].some((a) => u.endsWith(a))),
163
+ );
164
+ assert.deepEqual(
165
+ heavy,
166
+ [],
167
+ `urban --version eagerly loaded toolkit/runtime modules:\n${heavy.join("\n")}`,
168
+ );
169
+ } finally {
170
+ await rm(dir, { recursive: true, force: true });
171
+ }
172
+ });
package/src/cli.ts CHANGED
@@ -11,22 +11,18 @@
11
11
  //
12
12
  // Global flags: --root <dir> (default "."), --manifest <file> (default nano.app.json),
13
13
  // --port <n>, -h/--help, -v/--version.
14
+ //
15
+ // Module-load laziness (nano-ide#592): only argument parsing, the stdio/guard helpers and the
16
+ // dispatcher load eagerly. Every command's modules are dynamically imported inside its handler —
17
+ // a top-level import of the toolkit, the scaffold package or the runtime barrel made EVERY
18
+ // command (including the per-data-op `urban data` gateway) pay the full ~2s load on small
19
+ // hosts before dispatching. `urban data` loads only the host detector and the data-op runner.
20
+ // Guarded by cli-lazy.test.ts, which records the URLs each command resolves.
14
21
 
15
- import {
16
- collectManifestIssues,
17
- installSignalHandlers,
18
- loadManifest,
19
- runDataOp,
20
- runDev,
21
- runFromEnv,
22
- selectHost,
23
- } from "./runtime/index.ts";
24
22
  import { denoGlobal, processGlobal } from "./runtime/adapters/globals.ts";
25
23
  import type { WritableStdio } from "./runtime/adapters/globals.ts";
26
24
  import { errorMessage, isRecord } from "./runtime/core/guards.ts";
27
- import { scaffold, slugify } from "create-urban-app";
28
- import type { DataRequest } from "./runtime/index.ts";
29
- import { addConnector, createNodeGenIO, previewModels, generate, runGen } from "./toolkit/index.ts";
25
+ import type { DataRequest } from "./runtime/core/modules/dataops.ts";
30
26
  import { pathToFileURL } from "node:url";
31
27
  import { readFileSync } from "node:fs";
32
28
  import { spawnSync } from "node:child_process";
@@ -132,6 +128,9 @@ Engine address: $CAMUNDA_REST_ADDRESS (default http://localhost:8080/v2).
132
128
  `;
133
129
 
134
130
  async function cmdCheck(f: Flags): Promise<number> {
131
+ const { selectHost } = await import("./runtime/adapters/detect.ts");
132
+ const { loadManifest } = await import("./runtime/core/manifest.ts");
133
+ const { collectManifestIssues } = await import("./runtime/core/validate.ts");
135
134
  // The host is anchored at f.root, so manifest paths are already root-relative —
136
135
  // pass the bare manifest filename (prefixing root again would double it).
137
136
  const host = selectHost({ cwd: f.root });
@@ -147,6 +146,7 @@ async function cmdCheck(f: Flags): Promise<number> {
147
146
  }
148
147
 
149
148
  async function cmdGen(f: Flags): Promise<number> {
149
+ const { createNodeGenIO, generate } = await import("./toolkit/index.ts");
150
150
  const io = createNodeGenIO();
151
151
  const res = await generate({ root: f.root, io, manifestFile: f.manifest, check: f.check, emitModels: f.models });
152
152
  if (res.incomplete) {
@@ -209,6 +209,7 @@ async function cmdDerive(f: Flags): Promise<number> {
209
209
  );
210
210
  return 2;
211
211
  }
212
+ const { createNodeGenIO, previewModels, runGen } = await import("./toolkit/index.ts");
212
213
  const io = createNodeGenIO();
213
214
 
214
215
  // Non-writing preview: emit `{ models: [{ id, kind, xml }], incomplete }` to stdout for the
@@ -250,6 +251,7 @@ async function cmdDerive(f: Flags): Promise<number> {
250
251
  }
251
252
 
252
253
  async function cmdRun(f: Flags, mount?: Record<string, boolean>): Promise<number> {
254
+ const { runFromEnv } = await import("./runtime/run.ts");
253
255
  const app = await runFromEnv({
254
256
  root: f.root,
255
257
  manifestPath: f.manifest,
@@ -262,12 +264,15 @@ async function cmdRun(f: Flags, mount?: Record<string, boolean>): Promise<number
262
264
  }
263
265
 
264
266
  async function cmdDev(f: Flags): Promise<number> {
267
+ const { runDev } = await import("./runtime/devserver.ts");
268
+ const { installSignalHandlers } = await import("./runtime/run.ts");
265
269
  const dev = await runDev({ root: f.root, manifestPath: f.manifest, port: f.port });
266
270
  installSignalHandlers(() => dev.stop());
267
271
  return -1; // keep the process alive; signal handlers stop the dev server
268
272
  }
269
273
 
270
274
  async function cmdDeploy(f: Flags): Promise<number> {
275
+ const { runFromEnv } = await import("./runtime/run.ts");
271
276
  const app = await runFromEnv({
272
277
  root: f.root,
273
278
  manifestPath: f.manifest,
@@ -322,6 +327,12 @@ export async function cmdData(f: Flags, readInput: () => Promise<string> = readS
322
327
  );
323
328
  return 0;
324
329
  }
330
+ // Lazily the only modules `urban data` loads (nano-ide#592): the host detector (both
331
+ // adapters, no toolkit) and the data-op runner (gateway + SQLite adapter; the toolkit
332
+ // derivers behind the `domaintypes` op load only when that op is dispatched). Imported
333
+ // directly rather than via the runtime barrel, which would pull in the whole runtime.
334
+ const { selectHost } = await import("./runtime/adapters/detect.ts");
335
+ const { runDataOp } = await import("./runtime/core/modules/dataops.ts");
325
336
  const host = selectHost({ cwd: f.root });
326
337
  try {
327
338
  // Mirror runFromEnv: the host is anchored at f.root, so pass root "." to avoid
@@ -342,6 +353,7 @@ async function cmdNew(f: Flags): Promise<number> {
342
353
  console.error("usage: urban new <name> [--root <path>] [--deno] [--style model|code] (or --code-first)");
343
354
  return 1;
344
355
  }
356
+ const { scaffold, slugify } = await import("create-urban-app");
345
357
  const dir = f.root !== "." ? f.root : `./${slugify(name)}`;
346
358
  const res = await scaffold({ name, dir, deno: f.deno, style: f.style });
347
359
  console.log(`✔ scaffolded "${res.id}" in ${res.dir} (${res.files.length} files)`);
@@ -380,6 +392,7 @@ async function cmdAdd(f: Flags): Promise<number> {
380
392
  }
381
393
 
382
394
  try {
395
+ const { addConnector, createNodeGenIO } = await import("./toolkit/index.ts");
383
396
  const result = await addConnector({
384
397
  root: f.root,
385
398
  pkg,
package/src/index.ts CHANGED
@@ -31,6 +31,7 @@ export {
31
31
  declarativeToBpmn,
32
32
  declarativeToLayoutedBpmn,
33
33
  externalJobTypes,
34
+ inlineJobTypes,
34
35
  envelope,
35
36
  layoutBpmn,
36
37
  toBpmn,
@@ -27,43 +27,17 @@ import { validateManifest } from "../validate.ts";
27
27
  import { makeGateway, type DataSource as GatewayDataSource } from "./gateway.ts";
28
28
  import { applyMigrations, MIGRATIONS_TABLE, openSqliteSource, resolveAppPath } from "./datasource.ts";
29
29
  // The `domaintypes` op composes the domain reifier from the pure toolkit derivers (single source of
30
- // truth for the IDE's codegen, host dry-out nano-bpm#576). Imported directly from the deriver files
31
- // (not the `toolkit/` barrel, which pulls the Node fs IO) so core stays free of `node:*`/Deno; the
32
- // derivers' only core dependency is the gateway's `TableMeta`/`ColumnMeta` *types*, which erase.
33
- import { GENERATED_DIR } from "../../../toolkit/artifact.ts";
34
- import {
35
- DOMAIN_BINDINGS,
36
- type DomainTypeDef,
37
- type DomainTypeRegistry,
38
- emitDomainBindings,
39
- emitDomainModel,
40
- registryFromManifest,
41
- type SourceSchema,
42
- } from "../../../toolkit/derivers/domain.ts";
43
- import {
44
- DOMAIN_MODEL_JSON,
45
- emitDomainModelJson,
46
- type FusedMetaDecl,
47
- resolveShapes,
48
- type ShapeDecl,
49
- } from "../../../toolkit/derivers/shapes.ts";
50
- import {
51
- DOMAIN_DTS,
52
- emitWorkerBindings,
53
- emitWorkerBindingsRuntime,
54
- overlayDerivedWorkerIo,
55
- WORKER_BINDINGS_DTS,
56
- WORKER_BINDINGS_TS,
57
- type WorkerBindingDecl,
58
- } from "../../../toolkit/derivers/worker-io.ts";
59
- import {
60
- emitMessageBindings,
61
- emitMessageBindingsRuntime,
62
- MESSAGE_BINDINGS_DTS,
63
- MESSAGE_BINDINGS_TS,
64
- type MessageBindingDecl,
65
- } from "../../../toolkit/derivers/messages.ts";
66
- import { emitMeta, META_TS, type MetaDecl } from "../../../toolkit/derivers/meta.ts";
30
+ // truth for the IDE's codegen, host dry-out nano-bpm#576). They are imported lazily inside the
31
+ // `domaintypes` case — a static import would make EVERY `urban data` op (the latency-sensitive
32
+ // gateway path, nano-ide#592) load the whole `src/toolkit/` tree before dispatching, when only
33
+ // `domaintypes` ever runs a deriver. The derivers are pure (no host-specific IO); their only core
34
+ // dependency is the gateway's `TableMeta`/`ColumnMeta` *types*, which erase. The deriver *types* the
35
+ // request carries are type-only imports (erased), so they cost nothing at module load.
36
+ import type { DomainTypeDef, DomainTypeRegistry, SourceSchema } from "../../../toolkit/derivers/domain.ts";
37
+ import type { FusedMetaDecl, ShapeDecl } from "../../../toolkit/derivers/shapes.ts";
38
+ import type { WorkerBindingDecl } from "../../../toolkit/derivers/worker-io.ts";
39
+ import type { MessageBindingDecl } from "../../../toolkit/derivers/messages.ts";
40
+ import type { MetaDecl } from "../../../toolkit/derivers/meta.ts";
67
41
 
68
42
  /** The DB-manager op protocol's known op set — every op (including `domaintypes`) is dispatched by
69
43
  * `runDataOp`. Typing `op` as this union (instead of `string`) gives SDK/console callers type-safety
@@ -349,6 +323,36 @@ export async function runDataOp(
349
323
  // debounced keystroke has no DB side-effect and pays only for `text` + `shapeDiagnostics`.
350
324
  const { default: def, sources } = listSources(manifest);
351
325
  const persist = req.write !== false;
326
+ // The derivers are the only toolkit modules `urban data` ever needs, and only this op runs
327
+ // them — load them here, not at module scope, so every other op (the hot path of the data
328
+ // gateway, nano-ide#592) never pays for the `src/toolkit/` tree. Imported directly from the
329
+ // deriver files (not the `toolkit/` barrel, which pulls the Node fs IO) so core stays free
330
+ // of host-specific IO.
331
+ const { GENERATED_DIR } = await import("../../../toolkit/artifact.ts");
332
+ const {
333
+ DOMAIN_BINDINGS,
334
+ emitDomainBindings,
335
+ emitDomainModel,
336
+ registryFromManifest,
337
+ } = await import("../../../toolkit/derivers/domain.ts");
338
+ const { DOMAIN_MODEL_JSON, emitDomainModelJson, resolveShapes } = await import(
339
+ "../../../toolkit/derivers/shapes.ts"
340
+ );
341
+ const {
342
+ DOMAIN_DTS,
343
+ emitWorkerBindings,
344
+ emitWorkerBindingsRuntime,
345
+ overlayDerivedWorkerIo,
346
+ WORKER_BINDINGS_DTS,
347
+ WORKER_BINDINGS_TS,
348
+ } = await import("../../../toolkit/derivers/worker-io.ts");
349
+ const {
350
+ emitMessageBindings,
351
+ emitMessageBindingsRuntime,
352
+ MESSAGE_BINDINGS_DTS,
353
+ MESSAGE_BINDINGS_TS,
354
+ } = await import("../../../toolkit/derivers/messages.ts");
355
+ const { emitMeta, META_TS } = await import("../../../toolkit/derivers/meta.ts");
352
356
  // Introspect each sqlite source's live schema. When persisting, migrate first (the domain
353
357
  // model is derived from `gw.schema()`, so a regen that raced ahead of `migrate` on a fresh DB
354
358
  // would emit an empty `Domain`); the migration runs on the shared `_urban_migrations` ledger.
@@ -13,7 +13,6 @@ import {
13
13
  type InsertObserver,
14
14
  } from "./gateway.ts";
15
15
  import { currentJobContext } from "../execContext.ts";
16
- import { isAbsolutePath } from "../../../toolkit/artifact.ts";
17
16
  import { SQL_IDENT, isReservedObjectName } from "../read-model.ts";
18
17
 
19
18
  export function sqlitePathFromUrl(url: string): string {
@@ -27,10 +26,14 @@ export function sqlitePathFromUrl(url: string): string {
27
26
  * that starts with a single backslash ("\data\app.db") and a Windows UNC path
28
27
  * ("\\\\server\\share"). A single leading backslash covers both the drive-root and UNC cases,
29
28
  * matching Node's `path.win32.isAbsolute`. Used by `resolveAppPath` so a caller-supplied absolute
30
- * path is never incorrectly prefixed with the app root. Defined once in `toolkit/artifact.ts` (the
31
- * shared, dependency-free layer) and re-exported here so the toolkit's gen path-join and this
32
- * runtime resolver apply the identical rule (no gen/runtime drift). */
33
- export { isAbsolutePath };
29
+ * path is never incorrectly prefixed with the app root. Defined here in the runtime (not imported
30
+ * from `toolkit/artifact.ts`) so the `urban data` gateway's static module graph never touches the
31
+ * `src/toolkit/` tree (nano-ide#592); `toolkit/artifact.ts` re-exports this definition so the
32
+ * toolkit's gen path-join and this runtime resolver apply the identical rule (no gen/runtime
33
+ * drift). */
34
+ export function isAbsolutePath(p: string): boolean {
35
+ return /^(\/|\\|[A-Za-z]:[/\\])/.test(p);
36
+ }
34
37
 
35
38
  /** Resolve `p` against the app `root`: an absolute `p` (see `isAbsolutePath`) is returned as-is;
36
39
  * a relative `p` is joined onto `root`. Trims a trailing separator of either kind off `root` so
@@ -48,9 +48,9 @@ export function sortArtifacts(a: DerivedArtifact[]): DerivedArtifact[] {
48
48
  }
49
49
 
50
50
  /** True when `p` is an absolute path — POSIX root (`/`), a drive-letter root (`C:\` or `C:/`), or a
51
- * Windows UNC/drive-root backslash (`\`). The single source of truth for the absolute-path rule,
52
- * shared by the toolkit's path join (gen.ts) and the runtime's resolveAppPath so both agree on
53
- * whether a manifest path is app-root-relative or absolute (no gen/runtime drift). */
54
- export function isAbsolutePath(p: string): boolean {
55
- return /^(\/|\\|[A-Za-z]:[/\\])/.test(p);
56
- }
51
+ * Windows UNC/drive-root backslash (`\`). The single source of truth for the absolute-path rule is
52
+ * the runtime's `resolveAppPath` side (`runtime/core/modules/datasource.ts`); it is re-exported
53
+ * here so the toolkit's path join (gen.ts) and the runtime resolver agree on whether a manifest
54
+ * path is app-root-relative or absolute (no gen/runtime drift) without the runtime's data gateway
55
+ * statically importing the toolkit tree (nano-ide#592). */
56
+ export { isAbsolutePath } from "../runtime/core/modules/datasource.ts";
@@ -65,6 +65,16 @@ export interface GenResult {
65
65
  incomplete: boolean;
66
66
  /** Per-file model-derivation errors (empty on success). */
67
67
  modelErrors: ModelError[];
68
+ /** Job types of inline-handled (`w.run`) code-first steps — the set the app's in-process
69
+ * Worker already serves (nano-ide#595). The worker-stub scaffolder skips these; empty for a
70
+ * model-first app. */
71
+ inlineTaskTypes: string[];
72
+ /** The code-first models derived in-memory this run (`deriveModels().models`) — the
73
+ * worker-stub scaffolder plans against these in addition to the on-disk models, since a
74
+ * declared `models` block (e.g. `models.workflows`) suppresses the `resources/` convention
75
+ * scan that would otherwise pick the written `.bpmn` up (nano-ide#595). Empty for a
76
+ * model-first app. */
77
+ derivedModels: ModelSource[];
68
78
  /** Non-fatal spec-hygiene warnings (e.g. a requestBody schema shared by >1 operation). Empty on
69
79
  * a clean spec. Surfaced by the CLI but never fail gen/check — advisory only. */
70
80
  warnings: string[];
@@ -76,7 +86,8 @@ function join(root: string, rel: string): string {
76
86
  // pass either style without gen/runtime drift over where a file resolves.
77
87
  const norm = (s: string): string => s.replace(/\\/g, "/");
78
88
  // An absolute `rel` resolves to itself — never prefixed with `root` — mirroring the runtime's
79
- // resolveAppPath (isAbsolutePath is the shared SoT in artifact.ts). Without this an absolute
89
+ // resolveAppPath (isAbsolutePath is the shared SoT, defined in runtime's datasource.ts and
90
+ // re-exported from artifact.ts). Without this an absolute
80
91
  // manifest path (e.g. "/abs/openapi.json") would be stripped to root-relative and gen would
81
92
  // read/derive a different file than the runtime resolves. Trailing edge separators are still
82
93
  // trimmed for stable, comparable artifact keys.
@@ -332,7 +343,7 @@ export async function runGen(opts: GenOptions & { check?: boolean }): Promise<Ge
332
343
  }
333
344
  }
334
345
 
335
- return { artifacts, drift, swept: swept.sort(), incomplete: derived.incomplete, modelErrors: derived.errors, warnings };
346
+ return { artifacts, drift, swept: swept.sort(), incomplete: derived.incomplete, modelErrors: derived.errors, inlineTaskTypes: derived.inlineTaskTypes, derivedModels: derived.models, warnings };
336
347
  }
337
348
 
338
349
  export { dirOf, join as joinPath };
@@ -2,9 +2,13 @@ import { test } from "node:test";
2
2
  import assert from "node:assert/strict";
3
3
  import { generate } from "./generate.ts";
4
4
  import type { GenIO } from "./gen.ts";
5
+ import { defineFlow, envelope } from "@nanobpm/workflow";
5
6
 
6
7
  /** In-memory filesystem mirroring gen.test.ts's memIO (IO-free, deterministic). */
7
- function memIO(files: Record<string, string>): GenIO & { files: Record<string, string> } {
8
+ function memIO(
9
+ files: Record<string, string>,
10
+ modules: Record<string, Record<string, unknown>> = {},
11
+ ): GenIO & { files: Record<string, string> } {
8
12
  return {
9
13
  files,
10
14
  async readText(p) {
@@ -25,12 +29,28 @@ function memIO(files: Record<string, string>): GenIO & { files: Record<string, s
25
29
  }
26
30
  return [...names];
27
31
  },
32
+ async listSubdirs(p) {
33
+ const prefix = p.replace(/\/+$/, "") + "/";
34
+ const names = new Set<string>();
35
+ for (const f of Object.keys(files)) {
36
+ if (f.startsWith(prefix)) {
37
+ const rest = f.slice(prefix.length);
38
+ const slash = rest.indexOf("/");
39
+ if (slash > 0) names.add(rest.slice(0, slash));
40
+ }
41
+ }
42
+ return [...names];
43
+ },
28
44
  async exists(p) {
29
45
  return p in files;
30
46
  },
31
47
  async remove(p) {
32
48
  delete files[p];
33
49
  },
50
+ async importModule(p) {
51
+ if (!(p in modules)) throw new Error(`no module registered for ${p}`);
52
+ return modules[p];
53
+ },
34
54
  };
35
55
  }
36
56
 
@@ -133,3 +153,68 @@ test("generate keeps existing human-owned stubs (write-once, never clobbered)",
133
153
  assert.deepEqual(res.operationStubs.map((s) => s.status), ["kept"]);
134
154
  assert.deepEqual(res.missingStubs, []);
135
155
  });
156
+
157
+ // --- code-first: w.run steps are inline-handled, so gen must never scaffold stubs for them ---
158
+
159
+ const GreetIn = envelope("GreetIn", { who: "string" });
160
+ const GreetOut = envelope("GreetOut", { message: "string" });
161
+
162
+ /** The stock code-first template's flow: one inline-handled `w.run` step (nano-ide#595's repro). */
163
+ const greetFlow = defineFlow("greet", { hello: { in: GreetIn, out: GreetOut } }, (w) => {
164
+ w.run("hello", async () => ({ message: "hi" }));
165
+ });
166
+
167
+ test("generate on a code-first app scaffolds NO stub for an inline w.run step (nano-ide#595)", async () => {
168
+ // The stock code-first app (create-urban-app --code-first): `greet:hello` is served by
169
+ // `w.run("hello", …)` and hosted in-process by main.ts's Worker. Scaffolding a throwing stub
170
+ // for it puts a second, always-failing worker on the same job type.
171
+ const io = memIO(
172
+ {
173
+ "/cf/nano.app.json": JSON.stringify({
174
+ id: "cf",
175
+ data: { default: "app" },
176
+ models: { workflows: ["workflows/*.ts"] },
177
+ }),
178
+ "/cf/workflows/greet.ts": "// derived in-memory via importModule stub",
179
+ },
180
+ { "/cf/workflows/greet.ts": { greet: greetFlow } },
181
+ );
182
+
183
+ const res = await generate({ root: "/cf", io });
184
+
185
+ assert.equal(res.incomplete, false);
186
+ assert.deepEqual(res.workerStubs, [], "no worker stub is scaffolded for an inline-handled step");
187
+ assert.deepEqual(res.wiredWorkers, [], "nothing is wired into workers[]");
188
+ assert.equal(res.manifestPatched, false, "the manifest is untouched");
189
+ assert.equal(io.files["/cf/workers/greet-hello/worker.ts"], undefined, "no throwing stub on disk");
190
+ const manifest = JSON.parse(io.files["/cf/nano.app.json"]);
191
+ assert.equal(manifest.workers, undefined, "workers[] stays undeclared");
192
+ // The model itself is still derived and written.
193
+ assert.ok(io.files["/cf/resources/processes/greet.bpmn"], "the derived .bpmn still lands");
194
+ });
195
+
196
+ test("generate on a code-first app still scaffolds a stub for an external w.task step", async () => {
197
+ // The other half of the split: only `w.run` steps are inline; a `w.task` step has no in-process
198
+ // handler, so it still needs its write-once stub.
199
+ const flow = defineFlow("orders", (w) => {
200
+ w.run("charge", async () => ({}));
201
+ w.task("sign"); // external worker
202
+ });
203
+ const io = memIO(
204
+ {
205
+ "/cf/nano.app.json": JSON.stringify({
206
+ id: "cf",
207
+ data: { default: "app" },
208
+ models: { workflows: ["workflows/*.ts"] },
209
+ }),
210
+ "/cf/workflows/orders.ts": "// derived in-memory via importModule stub",
211
+ },
212
+ { "/cf/workflows/orders.ts": { flow } },
213
+ );
214
+
215
+ const res = await generate({ root: "/cf", io });
216
+
217
+ assert.deepEqual(res.workerStubs.map((s) => s.taskType), ["orders:sign"]);
218
+ assert.ok(io.files["/cf/workers/orders-sign/worker.ts"], "external task gets its stub");
219
+ assert.equal(io.files["/cf/workers/orders-charge/worker.ts"], undefined, "inline step gets none");
220
+ });
@@ -52,7 +52,15 @@ export async function generate(opts: GenOptions & { check?: boolean }): Promise<
52
52
 
53
53
  const write = !opts.check;
54
54
  const scaffoldOpts = { root: opts.root, io: opts.io, manifestFile: opts.manifestFile, write };
55
- const workers = await scaffoldWorkers(scaffoldOpts);
55
+ // Inline-handled (`w.run`) steps are already served by the app's in-process Worker — never
56
+ // scaffold a competing throwing stub for them (nano-ide#595). The derived models are also
57
+ // handed over: a code-first manifest's declared `models` block suppresses the resources/
58
+ // convention scan in readModels, so without them the scaffolder would plan from nothing.
59
+ const workers = await scaffoldWorkers({
60
+ ...scaffoldOpts,
61
+ inlineTaskTypes: res.inlineTaskTypes,
62
+ extraModels: res.derivedModels,
63
+ });
56
64
  const operations = await scaffoldOperations(scaffoldOpts);
57
65
 
58
66
  const missingStubs = [
@@ -157,3 +157,24 @@ test("deriveModels treats a genuine same-module re-export as one model, no error
157
157
  assert.deepEqual(d.errors, []);
158
158
  assert.equal(d.ids.length, 1);
159
159
  });
160
+
161
+ test("deriveModels reports the inline-handled (w.run) task types alongside the model (nano-ide#595)", async () => {
162
+ // The worker-stub scaffolder consumes `inlineTaskTypes` to skip steps whose handler is hosted
163
+ // in-process by the flow's Worker — only `w.task` (external) steps may be scaffolded.
164
+ const mixed = defineFlow("orders", (w) => {
165
+ w.run("charge", async () => ({}));
166
+ w.task("sign"); // external — must NOT be reported inline
167
+ w.run("ship", async () => ({}));
168
+ });
169
+ const io = memIO(
170
+ { "/app/workflows/orders.ts": "// source" },
171
+ { "workflows/orders.ts": { mixed } },
172
+ );
173
+ const d = await deriveModels("/app", io, CODE_FIRST_MANIFEST);
174
+ assert.equal(d.incomplete, false);
175
+ assert.deepEqual(d.inlineTaskTypes.sort(), ["orders:charge", "orders:ship"]);
176
+ // A no-op derivation (no loader / no workflow files) reports an empty set, not undefined.
177
+ const noLoader = memIO({ "/app/workflows/orders.ts": "// source" });
178
+ delete noLoader.importModule;
179
+ assert.deepEqual((await deriveModels("/app", noLoader, CODE_FIRST_MANIFEST)).inlineTaskTypes, []);
180
+ });
@@ -9,7 +9,7 @@
9
9
  // loader is absent — or the app authors its models directly (model-first) — derivation is a
10
10
  // no-op and the rest of `urban gen` proceeds on the authored/derived models unchanged.
11
11
 
12
- import { toDeployableBpmn } from "@nanobpm/workflow";
12
+ import { inlineJobTypes, toDeployableBpmn } from "@nanobpm/workflow";
13
13
  import type { Workflow } from "@nanobpm/workflow";
14
14
  import type { DerivedArtifact } from "./artifact.ts";
15
15
  import type { GenIO } from "./gen.ts";
@@ -78,6 +78,13 @@ export interface DerivedModels {
78
78
  models: ModelSource[];
79
79
  /** Derived flow ids (used for the provenance-scoped stale sweep). */
80
80
  ids: string[];
81
+ /** Job types of inline-handled (`w.run`) steps across every derived declarative flow —
82
+ * the set the app's in-process Worker already serves (nano-ide#595). The worker-stub
83
+ * scaffolder skips these; only `w.task` (external) steps may be scaffolded. Empty when
84
+ * derivation was a no-op (no loader / no workflow files) or all flows are imperative
85
+ * (an imperative flow's steps run inside its single `:__orchestrate` job, which the
86
+ * planner already skips). */
87
+ inlineTaskTypes: string[];
81
88
  /** Per-flow `{ id, kind, xml }` for the non-writing preview (`urban derive --stdout`). */
82
89
  list: DerivedModelInfo[];
83
90
  /** The output directory the artifacts were written into. */
@@ -101,6 +108,7 @@ function emptyDerived(outDir: string): DerivedModels {
101
108
  artifacts: [],
102
109
  models: [],
103
110
  ids: [],
111
+ inlineTaskTypes: [],
104
112
  list: [],
105
113
  outDir,
106
114
  attempted: false,
@@ -184,6 +192,7 @@ export async function deriveModels(
184
192
  const path = `${outDir}/${bpmnFilename(id)}`;
185
193
  out.artifacts.push({ path, content: xml });
186
194
  out.models.push({ path, xml });
195
+ if (value.kind === "declarative") out.inlineTaskTypes.push(...inlineJobTypes(value));
187
196
  out.list.push({ id, kind: value.kind, xml });
188
197
  out.ids.push(id);
189
198
  } catch (e) {