@crewhaus/target-batch-worker 0.3.1 → 0.4.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.
package/dist/index.d.ts CHANGED
@@ -4,9 +4,11 @@
4
4
  * Codegen for the queue-worker target. Emits a single self-contained
5
5
  * daemon (`agent.ts`) that:
6
6
  *
7
- * 1. Constructs the configured queue adapter (in-memory only in v0;
8
- * SQS / Redis Streams / Postgres adapters stub out at the
9
- * `unsupported queue adapter` error).
7
+ * 1. Constructs the configured queue adapter. `in-memory` is the default
8
+ * and needs no environment; `sqs` / `redis-streams` / `postgres`
9
+ * resolve their connection details from the environment at boot
10
+ * (SQS_QUEUE_URL+AWS_REGION / REDIS_URL / DATABASE_URL — G06) and
11
+ * fail fast with the missing variable's name.
10
12
  * 2. Optionally seeds the in-memory adapter with `queue.seedJobs`
11
13
  * (test / smoke convenience).
12
14
  * 3. Boots a `queue-consumer` with the user's `agent` as the handler
package/dist/index.js CHANGED
@@ -4,9 +4,11 @@
4
4
  * Codegen for the queue-worker target. Emits a single self-contained
5
5
  * daemon (`agent.ts`) that:
6
6
  *
7
- * 1. Constructs the configured queue adapter (in-memory only in v0;
8
- * SQS / Redis Streams / Postgres adapters stub out at the
9
- * `unsupported queue adapter` error).
7
+ * 1. Constructs the configured queue adapter. `in-memory` is the default
8
+ * and needs no environment; `sqs` / `redis-streams` / `postgres`
9
+ * resolve their connection details from the environment at boot
10
+ * (SQS_QUEUE_URL+AWS_REGION / REDIS_URL / DATABASE_URL — G06) and
11
+ * fail fast with the missing variable's name.
10
12
  * 2. Optionally seeds the in-memory adapter with `queue.seedJobs`
11
13
  * (test / smoke convenience).
12
14
  * 3. Boots a `queue-consumer` with the user's `agent` as the handler
@@ -130,12 +132,247 @@ function taxonomyField(ir, indent) {
130
132
  return "";
131
133
  return `\n${indent}failureTaxonomy: ${JSON.stringify(taxonomy)},`;
132
134
  }
133
- export function emitBatchWorker(ir, opts = {}) {
134
- if (ir.queue.adapter !== "in-memory") {
135
- // v0 only ships the in-memory adapter; the others are stubs that
136
- // throw at runtime so the operator sees a clean diagnostic.
137
- // Codegen still succeeds — we emit the daemon, but boot fails.
135
+ /**
136
+ * Item 27 (Batch A extends it to this shape) — render the `budget`
137
+ * runChatLoop field. Empty when the spec omits it. Mirror: target-cli +
138
+ * target-channel-bot + target-managed render the same field. NOTE: the
139
+ * runtime meters cost per `runChatLoop` call, so on this shape the cap is a
140
+ * PER-JOB ceiling (every pulled job runs its own single-turn loop), not a
141
+ * worker-lifetime one.
142
+ */
143
+ function budgetField(ir, indent) {
144
+ if (ir.budget === undefined)
145
+ return "";
146
+ return `\n${indent}budget: ${JSON.stringify(ir.budget)},`;
147
+ }
148
+ /**
149
+ * Loop contract 0.4 (Batch A) — thread the top-level `limits:` ceilings into
150
+ * the per-job `runChatLoop` call as the runtime's individual top-level
151
+ * knobs, camelCase-mirroring the IR 1:1. Every knob is emitted only when
152
+ * declared — the runtime owns every default — so existing bundles stay
153
+ * byte-identical. Like `budget`, the time knobs (`deadlineMs` /
154
+ * `turnTimeoutMs`) bound ONE job's loop — size them against
155
+ * `queue.visibilityTimeoutMs`, not the worker's lifetime. `limits.crew`
156
+ * never reaches this shape (crew-only; the spec rejects it everywhere
157
+ * else). Mirror: target-cli + target-channel-bot + target-managed render
158
+ * the same fields.
159
+ */
160
+ function limitsFields(ir, indent) {
161
+ const limits = ir.limits;
162
+ if (limits === undefined)
163
+ return "";
164
+ const pieces = [];
165
+ if (limits.maxToolIterations !== undefined) {
166
+ pieces.push(`\n${indent}maxToolIterations: ${limits.maxToolIterations},`);
167
+ }
168
+ if (limits.maxConcurrentTools !== undefined) {
169
+ pieces.push(`\n${indent}maxConcurrentTools: ${limits.maxConcurrentTools},`);
170
+ }
171
+ if (limits.contextLimit !== undefined) {
172
+ pieces.push(`\n${indent}contextLimit: ${limits.contextLimit},`);
173
+ }
174
+ if (limits.deadlineMs !== undefined) {
175
+ pieces.push(`\n${indent}deadlineMs: ${limits.deadlineMs},`);
176
+ }
177
+ if (limits.turnTimeoutMs !== undefined) {
178
+ pieces.push(`\n${indent}turnTimeoutMs: ${limits.turnTimeoutMs},`);
179
+ }
180
+ if (limits.modelCallTimeoutMs !== undefined) {
181
+ pieces.push(`\n${indent}modelCallTimeoutMs: ${limits.modelCallTimeoutMs},`);
182
+ }
183
+ if (limits.loopDetection !== undefined) {
184
+ pieces.push(`\n${indent}loopDetection: ${JSON.stringify(limits.loopDetection)},`);
185
+ }
186
+ return pieces.join("");
187
+ }
188
+ /**
189
+ * Section 9 / G05 (Batch A) — emit the `McpHost` boot block when the IR
190
+ * carries mcp_servers. Wire-once: the host boots once in `main()` (before
191
+ * the consumer starts) and registers every server's namespaced tools onto
192
+ * the shared `defaultCatalog`, so EVERY pulled job's `runChatLoop` carries
193
+ * them (`tools: defaultCatalog.list()` — no per-job reconnect). Mirror of
194
+ * target-channel-bot's renderMcpServers (keep in sync).
195
+ *
196
+ * 0.3.0 — env/header values are `IrSecretRef` objects; the UNRESOLVED
197
+ * config is embedded verbatim (so no secret value ever lands in the
198
+ * artifact) and `resolveMcpServerConfig` materialises it from the running
199
+ * process's environment at boot, failing fast with the variable's name
200
+ * when a referenced env var is unset.
201
+ *
202
+ * Empty `mcp_servers` returns empty strings so spec files without MCP get
203
+ * no MCP plumbing at all and prior bundles stay byte-identical.
204
+ */
205
+ function renderMcpServers(ir) {
206
+ const entries = Object.entries(ir.mcp_servers);
207
+ if (entries.length === 0) {
208
+ return { imports: [], bootBlock: "", cleanupBlock: "", hasAny: false };
209
+ }
210
+ const imports = [
211
+ `import { McpHost, resolveMcpServerConfig } from "@crewhaus/mcp-host";`,
212
+ `import { registerMcpServer } from "@crewhaus/tool-mcp";`,
213
+ ];
214
+ const addLines = entries
215
+ .map(([name, cfg]) => `mcpHost.addServer(${escapeJsonString(name)}, resolveMcpServerConfig(${JSON.stringify(cfg)}, { name: ${escapeJsonString(name)} }));`)
216
+ .join("\n");
217
+ const registerLines = entries
218
+ .map(([name]) => ` registerMcpServer(mcpHost, ${escapeJsonString(name)}, defaultCatalog, { onRegister: ({ fullName }) => process.stdout.write(\`[mcp] registered \${fullName}\\n\`) }),`)
219
+ .join("\n");
220
+ const bootBlock = [
221
+ "const mcpHost = new McpHost();",
222
+ addLines,
223
+ "await Promise.all([",
224
+ registerLines,
225
+ "]);",
226
+ ].join("\n");
227
+ return {
228
+ imports,
229
+ bootBlock,
230
+ cleanupBlock: "await mcpHost.disconnectAll();",
231
+ hasAny: true,
232
+ };
233
+ }
234
+ /**
235
+ * G06 — per-adapter queue construction. The adapter is fixed at CODEGEN
236
+ * time (`queue.adapter` in the spec), so the bundle carries only the
237
+ * selected backend's import + `buildQueue()` body. Non-in-memory backends
238
+ * resolve their connection details from the environment at boot via the
239
+ * emitted `requireQueueEnv` helper and throw ONLY when the selected
240
+ * backend's env is missing, naming the variable; everything past the env
241
+ * gate (SDK presence, credential chains) is `@crewhaus/queue-protocol`'s
242
+ * own boot contract. The identifier knobs each backend needs beyond its
243
+ * connection URL get worker-conventional defaults derived from the spec
244
+ * name, overridable by env.
245
+ *
246
+ * The in-memory body is emitted BYTE-IDENTICALLY to its pre-G06 text
247
+ * (including the now-historical unreachable `throw` arm) — the
248
+ * `pre-continuity` byte-restore pins in packages/compiler cover the
249
+ * in-memory batch bundle, and G06 must not disturb them.
250
+ */
251
+ function renderQueueBuild(ir) {
252
+ const seedJobsLit = JSON.stringify(ir.queue.seedJobs ?? []);
253
+ switch (ir.queue.adapter) {
254
+ case "in-memory":
255
+ return {
256
+ importLine: `import { createInMemoryQueue } from "@crewhaus/queue-protocol";`,
257
+ seedConst: `\nconst SPEC_SEED_JOBS: string[] = ${seedJobsLit};`,
258
+ buildFn: `function buildQueue(): ReturnType<typeof createInMemoryQueue<string>> {
259
+ if (SPEC_QUEUE_ADAPTER === "in-memory") {
260
+ return createInMemoryQueue<string>({ initialJobs: SPEC_SEED_JOBS });
261
+ }
262
+ throw new Error(
263
+ "[batch-worker] queue adapter \\"" + SPEC_QUEUE_ADAPTER +
264
+ "\\" is not implemented in v0 — only \\"in-memory\\" is supported. " +
265
+ "SQS / Redis Streams / Postgres adapters land in a follow-up.",
266
+ );
267
+ }`,
268
+ };
269
+ case "sqs":
270
+ return {
271
+ importLine: `import { createSqsAdapter, type QueueAdapter } from "@crewhaus/queue-protocol";`,
272
+ seedConst: "",
273
+ buildFn: `${REQUIRE_QUEUE_ENV_FN}
274
+
275
+ function buildQueue(): QueueAdapter<string> {
276
+ const queueUrl = requireQueueEnv("SQS_QUEUE_URL", "the queue's full https URL");
277
+ const region =
278
+ process.env["AWS_REGION"] ??
279
+ process.env["AWS_DEFAULT_REGION"] ??
280
+ requireQueueEnv("AWS_REGION", "the queue's AWS region, e.g. us-east-1");
281
+ // Credentials are optional here: absent, the SDK falls back to its own
282
+ // chain (env, profile, IAM instance role).
283
+ const accessKeyId = process.env["AWS_ACCESS_KEY_ID"];
284
+ const secretAccessKey = process.env["AWS_SECRET_ACCESS_KEY"];
285
+ return createSqsAdapter<string>({
286
+ queueUrl,
287
+ region,
288
+ ...(accessKeyId !== undefined ? { accessKeyId } : {}),
289
+ ...(secretAccessKey !== undefined ? { secretAccessKey } : {}),
290
+ });
291
+ }`,
292
+ };
293
+ case "redis-streams":
294
+ return {
295
+ importLine: `import { createRedisStreamsAdapter, type QueueAdapter } from "@crewhaus/queue-protocol";`,
296
+ seedConst: "",
297
+ buildFn: `${REQUIRE_QUEUE_ENV_FN}
298
+
299
+ function buildQueue(): QueueAdapter<string> {
300
+ // ioredis reads REDIS_URL when the client connects; the presence check
301
+ // here fails fast at boot with the variable's name instead of a hung
302
+ // first pull.
303
+ requireQueueEnv("REDIS_URL", "redis[s]://[user:pass@]host:port[/db]");
304
+ return createRedisStreamsAdapter<string>({
305
+ streamKey: process.env["REDIS_STREAM_KEY"] ?? \`crewhaus:\${SPEC_NAME}:jobs\`,
306
+ consumerGroup: process.env["REDIS_CONSUMER_GROUP"] ?? \`crewhaus:\${SPEC_NAME}:workers\`,
307
+ consumerName: process.env["REDIS_CONSUMER_NAME"] ?? \`worker-\${process.pid}\`,
308
+ });
309
+ }`,
310
+ };
311
+ case "postgres":
312
+ return {
313
+ importLine: `import { createPostgresAdapter, type QueueAdapter } from "@crewhaus/queue-protocol";`,
314
+ seedConst: "",
315
+ buildFn: `${REQUIRE_QUEUE_ENV_FN}
316
+
317
+ function buildQueue(): QueueAdapter<string> {
318
+ // pg reads DATABASE_URL when the pool connects; the presence check here
319
+ // fails fast at boot with the variable's name instead of a hung first
320
+ // pull.
321
+ requireQueueEnv("DATABASE_URL", "postgres://user:pass@host:port/db");
322
+ const deadLetterTable = process.env["CREWHAUS_JOBS_DLQ_TABLE"];
323
+ return createPostgresAdapter<string>({
324
+ tableName: process.env["CREWHAUS_JOBS_TABLE"] ?? "crewhaus_jobs",
325
+ ...(deadLetterTable !== undefined ? { deadLetterTable } : {}),
326
+ });
327
+ }`,
328
+ };
138
329
  }
330
+ }
331
+ /**
332
+ * The env gate every non-in-memory `buildQueue()` opens with. Emitted (not
333
+ * imported) so the bundle stays self-contained; the message names the
334
+ * adapter, the variable, and its expected shape.
335
+ */
336
+ const REQUIRE_QUEUE_ENV_FN = `function requireQueueEnv(name: string, hint: string): string {
337
+ const v = process.env[name];
338
+ if (v === undefined || v === "") {
339
+ throw new Error(
340
+ \`[batch-worker] queue adapter "\${SPEC_QUEUE_ADAPTER}" requires the \${name} environment variable (\${hint}) — set it and restart the worker.\`,
341
+ );
342
+ }
343
+ return v;
344
+ }`;
345
+ /**
346
+ * Loop contract 0.4 (Batch F) — render the `IrSchedule` as a
347
+ * durable-execution `WakeSchedule` object literal (the IR MINUS
348
+ * `instructions`, which the worker enqueues as a job on each wake). Numeric
349
+ * fields are ms-normalized at lower time, so JSON.stringify is valid,
350
+ * byte-stable JS. Shared verbatim with target-channel-bot — keep in sync.
351
+ */
352
+ function wakeScheduleLiteral(schedule) {
353
+ const literal = schedule.kind === "cron"
354
+ ? {
355
+ kind: "cron",
356
+ cron: schedule.cron,
357
+ ...(schedule.timezone !== undefined ? { timezone: schedule.timezone } : {}),
358
+ ...(schedule.jitterMs !== undefined ? { jitterMs: schedule.jitterMs } : {}),
359
+ }
360
+ : {
361
+ kind: "interval",
362
+ everyMs: schedule.everyMs,
363
+ ...(schedule.jitterMs !== undefined ? { jitterMs: schedule.jitterMs } : {}),
364
+ };
365
+ return JSON.stringify(literal);
366
+ }
367
+ /** A one-line human description of a schedule for the boot log. Shared
368
+ * verbatim with target-channel-bot — keep in sync. */
369
+ function describeSchedule(schedule) {
370
+ const base = schedule.kind === "cron"
371
+ ? `cron "${schedule.cron}"${schedule.timezone !== undefined ? ` ${schedule.timezone}` : " UTC"}`
372
+ : `every ${schedule.everyMs}ms`;
373
+ return schedule.jitterMs !== undefined ? `${base} +/-${schedule.jitterMs}ms jitter` : base;
374
+ }
375
+ export function emitBatchWorker(ir, opts = {}) {
139
376
  const files = [{ path: "agent.ts", content: renderAgent(ir) }];
140
377
  // Item 42 — generated bundle README; default ON (`crewhaus compile
141
378
  // --no-readme` opts out).
@@ -152,28 +389,118 @@ function renderAgent(ir) {
152
389
  ? `\n// Spec-supplied tools registered at module load.\n${registrations.join("\n")}\n`
153
390
  : "";
154
391
  const permField = renderPermissionsField(ir);
155
- const seedJobsLit = JSON.stringify(ir.queue.seedJobs ?? []);
392
+ const queueBuild = renderQueueBuild(ir);
156
393
  const visRenewMs = ir.queue.visibilityRenewIntervalMs ?? Math.floor(ir.queue.visibilityTimeoutMs / 4);
157
394
  const hasRules = ir.permissions.rules.length > 0;
158
395
  const permImport = hasRules
159
396
  ? `import { BUILTIN_DEFAULT_RULES } from "@crewhaus/permission-engine";\n`
160
397
  : "";
398
+ const mcp = renderMcpServers(ir);
399
+ const mcpImportBlock = mcp.imports.length > 0 ? `${mcp.imports.join("\n")}\n` : "";
400
+ const mcpBoot = mcp.hasAny
401
+ ? ` // G05 — MCP servers, wired ONCE onto the shared defaultCatalog so every\n // pulled job's runChatLoop carries the namespaced tools.\n ${mcp.bootBlock.split("\n").join("\n ")}\n\n`
402
+ : "";
403
+ const mcpShutdownCleanup = mcp.hasAny ? `\n ${mcp.cleanupBlock}` : "";
404
+ const mcpIdleCleanup = mcp.hasAny ? `\n ${mcp.cleanupBlock}` : "";
405
+ // Loop contract 0.4 (Batch A) — spec-declared lifecycle hooks (`hooks:`).
406
+ // IrHook is HookDef-shaped by contract (the spec ↔ hooks-engine
407
+ // cross-check test pins the event vocabulary), so the literal threads
408
+ // straight into the per-job runChatLoop. Declaration order from the spec
409
+ // is preserved (hooks run in registration order). Absent/empty emits
410
+ // nothing so existing bundles stay byte-identical.
411
+ const specHooks = ir.hooks !== undefined && ir.hooks.length > 0 ? ir.hooks : undefined;
412
+ const hooksImport = specHooks !== undefined ? `import type { HookDef } from "@crewhaus/hooks-engine";\n` : "";
413
+ const hooksConst = specHooks !== undefined
414
+ ? `\n// Loop contract 0.4 — spec-declared lifecycle hooks (registration order preserved).\nconst SPEC_HOOKS: ReadonlyArray<HookDef> = ${JSON.stringify(specHooks)};`
415
+ : "";
416
+ const hooksField = specHooks !== undefined ? "\n hooks: SPEC_HOOKS," : "";
417
+ // The queue_idle fast-exit only exists on the in-memory adapter: its
418
+ // stats() are authoritative for "the queue is empty and nothing is in
419
+ // flight". SQS/Redis/Postgres stats cannot cheaply distinguish "empty
420
+ // right now" from "drained forever" (SQS pending, for one, is a
421
+ // CloudWatch metric), so those workers run until signaled.
422
+ // Loop contract 0.4 (Batch F) — a scheduled worker is a long-running cron
423
+ // daemon: it must NOT idle-exit between wakes, so a declared `schedule:`
424
+ // forces the keep-alive loop even for the in-memory adapter. Unscheduled
425
+ // specs keep the exact pre-Batch-F selection, so their bundles stay
426
+ // byte-identical.
427
+ const idleLoop = ir.queue.adapter === "in-memory" && ir.schedule === undefined
428
+ ? ` // For the hello-batch smoke: when the queue empties + no in-flight,
429
+ // emit a queue_idle event and exit so the smoke can assert on a
430
+ // clean shutdown without sending a signal. Production users will
431
+ // typically run forever and rely on signals.
432
+ const idleCheckMs = 200;
433
+ while (!stopping) {
434
+ await new Promise((r) => setTimeout(r, idleCheckMs));
435
+ const stats = await queue.stats();
436
+ if (stats.pending === 0 && stats.inFlight === 0 && consumer.inFlight() === 0) {
437
+ // Idle — check once more after a short delay to avoid racing with
438
+ // a job mid-pull.
439
+ await new Promise((r) => setTimeout(r, idleCheckMs));
440
+ const recheck = await queue.stats();
441
+ if (
442
+ recheck.pending === 0 &&
443
+ recheck.inFlight === 0 &&
444
+ consumer.inFlight() === 0
445
+ ) {
446
+ emit({ kind: "queue_idle", stats: recheck });
447
+ janitor.stop();
448
+ await consumer.drain();${mcpIdleCleanup}
449
+ emit({ kind: "worker_stop", stats: await queue.stats() });
450
+ return;
451
+ }
452
+ }
453
+ }`
454
+ : ` // Non-in-memory backend: no queue_idle fast-exit (the adapter's stats
455
+ // cannot prove the queue is drained) — the worker runs until SIGTERM /
456
+ // SIGINT drives the drain-and-stop path above.
457
+ const keepAliveMs = 200;
458
+ while (!stopping) {
459
+ await new Promise((r) => setTimeout(r, keepAliveMs));
460
+ }`;
161
461
  // v0.3.0 — continuity is spec-carried on this shape but not emit-wired in
162
462
  // 0.3.0 (only the five agent-loop shapes are). Surface it, 0.2.3-style.
163
463
  const continuityWarning = ir.continuity !== undefined
164
464
  ? "// note: continuity configured but ignored on batch in 0.3.0\n"
165
465
  : "";
466
+ // Loop contract 0.4 (Batch F, temporal contract / G84) — the `schedule:`
467
+ // wake loop. On each cron/interval wake the worker ENQUEUES a synthetic job
468
+ // (the schedule's `instructions`) so the running consumer pulls and
469
+ // processes it like any other — a scheduled job producer. All cron/interval
470
+ // + jitter arithmetic lives in durable-execution's `armSchedule`. Absent
471
+ // `schedule:` emits nothing, so unscheduled worker bundles stay
472
+ // byte-identical.
473
+ const scheduleImport = ir.schedule
474
+ ? `import { armSchedule } from "@crewhaus/durable-execution";\n`
475
+ : "";
476
+ const scheduleBoot = ir.schedule
477
+ ? `
478
+ // Loop contract 0.4 (Batch F) — schedule: wake loop (cron|interval + jitter)
479
+ const __scheduleInstructions = ${escapeJsonString(ir.schedule.instructions ?? "[scheduled wake]")};
480
+ let __scheduleTick = 0;
481
+ const __schedule = armSchedule(${wakeScheduleLiteral(ir.schedule)}, {
482
+ onWake: async () => {
483
+ __scheduleTick++;
484
+ const jobId = await queue.enqueue(__scheduleInstructions);
485
+ emit({ kind: "scheduled_wake", tick: __scheduleTick, jobId });
486
+ },
487
+ onError: (err) => emit({ kind: "scheduled_wake_error", error: (err as Error).message ?? String(err) }),
488
+ });
489
+ emit({ kind: "schedule_armed", schedule: ${escapeJsonString(describeSchedule(ir.schedule))} });
490
+ `
491
+ : "";
492
+ const scheduleShutdown = ir.schedule ? "\n __schedule.cancel();" : "";
166
493
  return `#!/usr/bin/env bun
167
494
  // Generated by crewhaus. DO NOT EDIT.
168
495
  // Source spec: ${escapeJsonString(ir.name)} (target: batch, ir version: ${ir.version})
169
- ${continuityWarning}import { createInMemoryQueue } from "@crewhaus/queue-protocol";
496
+ ${continuityWarning}${queueBuild.importLine}
170
497
  import { startConsumer } from "@crewhaus/queue-consumer";
171
498
  import { createInMemoryIdempotencyStore } from "@crewhaus/idempotency-keys";
172
499
  import { loadRetentionConfig } from "@crewhaus/data-retention-engine";
173
500
  import { createJanitor, runChatLoop } from "@crewhaus/runtime-core";
174
501
  import { createRunContext } from "@crewhaus/run-context";
175
502
  import { defaultCatalog } from "@crewhaus/tool-catalog";
176
- ${permImport}${importBlock}
503
+ ${permImport}${hooksImport}${scheduleImport}${mcpImportBlock}${importBlock}
177
504
 
178
505
  ${initLines}${registrationBlock}
179
506
  const SPEC_NAME = ${escapeJsonString(ir.name)};
@@ -184,23 +511,13 @@ const SPEC_VISIBILITY_TIMEOUT_MS = ${ir.queue.visibilityTimeoutMs};
184
511
  const SPEC_VISIBILITY_RENEW_MS = ${visRenewMs};
185
512
  const SPEC_MAX_RETRIES = ${ir.queue.maxRetries};
186
513
  const SPEC_CONCURRENCY = ${ir.concurrency};
187
- const SPEC_IDEMPOTENCY_WINDOW_MS = ${ir.idempotencyWindowMs};
188
- const SPEC_SEED_JOBS: string[] = ${seedJobsLit};
514
+ const SPEC_IDEMPOTENCY_WINDOW_MS = ${ir.idempotencyWindowMs};${queueBuild.seedConst}${hooksConst}
189
515
 
190
516
  function emit(event: Record<string, unknown>): void {
191
517
  process.stdout.write(\`\${JSON.stringify(event)}\\n\`);
192
518
  }
193
519
 
194
- function buildQueue(): ReturnType<typeof createInMemoryQueue<string>> {
195
- if (SPEC_QUEUE_ADAPTER === "in-memory") {
196
- return createInMemoryQueue<string>({ initialJobs: SPEC_SEED_JOBS });
197
- }
198
- throw new Error(
199
- "[batch-worker] queue adapter \\"" + SPEC_QUEUE_ADAPTER +
200
- "\\" is not implemented in v0 — only \\"in-memory\\" is supported. " +
201
- "SQS / Redis Streams / Postgres adapters land in a follow-up.",
202
- );
203
- }
520
+ ${queueBuild.buildFn}
204
521
 
205
522
  async function main(): Promise<void> {
206
523
  // Boot-time self-heal janitor (ops item 36): evicts expired sessions on a
@@ -236,7 +553,7 @@ async function main(): Promise<void> {
236
553
  janitor.start(Number(process.env["CREWHAUS_JANITOR_INTERVAL_MS"] ?? 3_600_000));
237
554
  }
238
555
 
239
- const queue = buildQueue();
556
+ ${mcpBoot} const queue = buildQueue();
240
557
  const idempotencyStore = createInMemoryIdempotencyStore<string>();
241
558
  emit({ kind: "worker_start", queueAdapter: SPEC_QUEUE_ADAPTER, concurrency: SPEC_CONCURRENCY });
242
559
 
@@ -255,7 +572,7 @@ async function main(): Promise<void> {
255
572
  seedMessages: [{ role: "user", content: input }],
256
573
  tools: defaultCatalog.list(),
257
574
  installSigintHandler: false,
258
- maxTokens: 1024,${permField}
575
+ maxTokens: ${ir.agent.maxTokens ?? 1024},${budgetField(ir, " ")}${limitsFields(ir, " ")}${hooksField}${permField}
259
576
  });
260
577
  return reply.trim();
261
578
  },
@@ -284,14 +601,14 @@ async function main(): Promise<void> {
284
601
  onDrainEnd: () => emit({ kind: "drain_end" }),
285
602
  },
286
603
  });
287
-
604
+ ${scheduleBoot}
288
605
  let stopping = false;
289
606
  const shutdown = async (signal: string): Promise<void> => {
290
607
  if (stopping) return;
291
608
  stopping = true;
292
- janitor.stop();
609
+ janitor.stop();${scheduleShutdown}
293
610
  emit({ kind: "shutdown_received", signal });
294
- await consumer.drain();
611
+ await consumer.drain();${mcpShutdownCleanup}
295
612
  const stats = await queue.stats();
296
613
  emit({ kind: "worker_stop", stats });
297
614
  process.exit(0);
@@ -299,32 +616,7 @@ async function main(): Promise<void> {
299
616
  process.on("SIGINT", () => void shutdown("SIGINT"));
300
617
  process.on("SIGTERM", () => void shutdown("SIGTERM"));
301
618
 
302
- // For the hello-batch smoke: when the queue empties + no in-flight,
303
- // emit a queue_idle event and exit so the smoke can assert on a
304
- // clean shutdown without sending a signal. Production users will
305
- // typically run forever and rely on signals.
306
- const idleCheckMs = 200;
307
- while (!stopping) {
308
- await new Promise((r) => setTimeout(r, idleCheckMs));
309
- const stats = await queue.stats();
310
- if (stats.pending === 0 && stats.inFlight === 0 && consumer.inFlight() === 0) {
311
- // Idle — check once more after a short delay to avoid racing with
312
- // a job mid-pull.
313
- await new Promise((r) => setTimeout(r, idleCheckMs));
314
- const recheck = await queue.stats();
315
- if (
316
- recheck.pending === 0 &&
317
- recheck.inFlight === 0 &&
318
- consumer.inFlight() === 0
319
- ) {
320
- emit({ kind: "queue_idle", stats: recheck });
321
- janitor.stop();
322
- await consumer.drain();
323
- emit({ kind: "worker_stop", stats: await queue.stats() });
324
- return;
325
- }
326
- }
327
- }
619
+ ${idleLoop}
328
620
  }
329
621
 
330
622
  main().catch((err) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/target-batch-worker",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "description": "Codegen for the BATCH target — emits a queue-consumer daemon (Section 23 BATCH)",
6
6
  "main": "dist/index.js",
@@ -15,9 +15,9 @@
15
15
  "test": "bun test src"
16
16
  },
17
17
  "dependencies": {
18
- "@crewhaus/errors": "0.3.1",
19
- "@crewhaus/infra-utils": "0.3.1",
20
- "@crewhaus/ir": "0.3.1"
18
+ "@crewhaus/errors": "0.4.0",
19
+ "@crewhaus/infra-utils": "0.4.0",
20
+ "@crewhaus/ir": "0.4.0"
21
21
  },
22
22
  "license": "Apache-2.0",
23
23
  "author": {