@crewhaus/target-batch-worker 0.1.4 → 0.1.6

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,27 @@
1
+ /**
2
+ * Catalog F2 `target-batch-worker` — Section 23 BATCH.
3
+ *
4
+ * Codegen for the queue-worker target. Emits a single self-contained
5
+ * daemon (`agent.ts`) that:
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).
10
+ * 2. Optionally seeds the in-memory adapter with `queue.seedJobs`
11
+ * (test / smoke convenience).
12
+ * 3. Boots a `queue-consumer` with the user's `agent` as the handler
13
+ * — each pulled job runs one `runChatLoop({singleTurn: true})` with
14
+ * the job input as the user message; the assistant's terminal text
15
+ * is the cached result.
16
+ * 4. Streams JSON events to stdout (`worker_start | job_start |
17
+ * job_end | drain_start | drain_end | worker_stop`).
18
+ * 5. Installs SIGTERM + SIGINT handlers that drain in-flight jobs
19
+ * cleanly before exit.
20
+ */
21
+ import { CrewhausError } from "@crewhaus/errors";
22
+ import type { Bundle, IrBatchV0 } from "@crewhaus/ir";
23
+ export declare class TargetEmitError extends CrewhausError {
24
+ readonly name = "TargetEmitError";
25
+ constructor(message: string, cause?: unknown);
26
+ }
27
+ export declare function emitBatchWorker(ir: IrBatchV0): Bundle;
@@ -20,138 +20,111 @@
20
20
  */
21
21
  import { CrewhausError } from "@crewhaus/errors";
22
22
  import { escapeJsonString } from "@crewhaus/infra-utils";
23
- import type { Bundle, IrBatchV0 } from "@crewhaus/ir";
24
-
25
23
  export class TargetEmitError extends CrewhausError {
26
- override readonly name = "TargetEmitError";
27
- constructor(message: string, cause?: unknown) {
28
- super("compiler", message, cause);
29
- }
24
+ name = "TargetEmitError";
25
+ constructor(message, cause) {
26
+ super("compiler", message, cause);
27
+ }
30
28
  }
31
-
32
- type BuiltinToolEntry = {
33
- readonly package: string;
34
- readonly export: string;
35
- readonly initSymbol?: string;
36
- };
37
-
38
- const BUILTIN_TOOL_MAP: Record<string, BuiltinToolEntry> = {
39
- read: { package: "@crewhaus/tool-fs", export: "read" },
40
- write: { package: "@crewhaus/tool-fs", export: "write" },
41
- edit: { package: "@crewhaus/tool-fs", export: "edit" },
42
- glob: { package: "@crewhaus/tool-fs", export: "glob" },
43
- grep: { package: "@crewhaus/tool-fs", export: "grep" },
44
- bash: { package: "@crewhaus/tool-bash", export: "bash" },
45
- webFetch: {
46
- package: "@crewhaus/tool-web",
47
- export: "webFetch",
48
- initSymbol: "registerWebFetchConfig",
49
- },
50
- webSearch: { package: "@crewhaus/tool-web", export: "webSearch" },
51
- fetch: {
52
- package: "@crewhaus/tool-fetch",
53
- export: "fetch",
54
- initSymbol: "registerFetchConfig",
55
- },
29
+ const BUILTIN_TOOL_MAP = {
30
+ read: { package: "@crewhaus/tool-fs", export: "read" },
31
+ write: { package: "@crewhaus/tool-fs", export: "write" },
32
+ edit: { package: "@crewhaus/tool-fs", export: "edit" },
33
+ glob: { package: "@crewhaus/tool-fs", export: "glob" },
34
+ grep: { package: "@crewhaus/tool-fs", export: "grep" },
35
+ bash: { package: "@crewhaus/tool-bash", export: "bash" },
36
+ webFetch: {
37
+ package: "@crewhaus/tool-web",
38
+ export: "webFetch",
39
+ initSymbol: "registerWebFetchConfig",
40
+ },
41
+ webSearch: { package: "@crewhaus/tool-web", export: "webSearch" },
42
+ fetch: {
43
+ package: "@crewhaus/tool-fetch",
44
+ export: "fetch",
45
+ initSymbol: "registerFetchConfig",
46
+ },
56
47
  };
57
-
58
- function resolveTools(
59
- toolNames: readonly string[],
60
- toolConfigs: Readonly<Record<string, unknown>>,
61
- ): {
62
- imports: string[];
63
- inits: string[];
64
- registrations: string[];
65
- } {
66
- if (toolNames.length === 0) return { imports: [], inits: [], registrations: [] };
67
- const byPackage = new Map<string, Set<string>>();
68
- const inits: string[] = [];
69
- const registrations: string[] = [];
70
- for (const name of toolNames) {
71
- const entry = BUILTIN_TOOL_MAP[name];
72
- if (!entry) {
73
- const known = Object.keys(BUILTIN_TOOL_MAP).sort().join(", ");
74
- throw new TargetEmitError(`unknown tool "${name}" — known tools: ${known}`);
48
+ function resolveTools(toolNames, toolConfigs) {
49
+ if (toolNames.length === 0)
50
+ return { imports: [], inits: [], registrations: [] };
51
+ const byPackage = new Map();
52
+ const inits = [];
53
+ const registrations = [];
54
+ for (const name of toolNames) {
55
+ const entry = BUILTIN_TOOL_MAP[name];
56
+ if (!entry) {
57
+ const known = Object.keys(BUILTIN_TOOL_MAP).sort().join(", ");
58
+ throw new TargetEmitError(`unknown tool "${name}" — known tools: ${known}`);
59
+ }
60
+ const set = byPackage.get(entry.package) ?? new Set();
61
+ set.add(entry.export);
62
+ byPackage.set(entry.package, set);
63
+ if (entry.initSymbol !== undefined) {
64
+ const cfg = toolConfigs[name];
65
+ if (cfg !== undefined) {
66
+ set.add(entry.initSymbol);
67
+ inits.push(`${entry.initSymbol}(${JSON.stringify(cfg)});`);
68
+ }
69
+ }
70
+ registrations.push(`defaultCatalog.register(${entry.export});`);
75
71
  }
76
- const set = byPackage.get(entry.package) ?? new Set<string>();
77
- set.add(entry.export);
78
- byPackage.set(entry.package, set);
79
- if (entry.initSymbol !== undefined) {
80
- const cfg = toolConfigs[name];
81
- if (cfg !== undefined) {
82
- set.add(entry.initSymbol);
83
- inits.push(`${entry.initSymbol}(${JSON.stringify(cfg)});`);
84
- }
72
+ const imports = [];
73
+ for (const pkg of [...byPackage.keys()].sort()) {
74
+ const symbols = [...(byPackage.get(pkg) ?? new Set())].sort();
75
+ imports.push(`import { ${symbols.join(", ")} } from "${pkg}";`);
85
76
  }
86
- registrations.push(`defaultCatalog.register(${entry.export});`);
87
- }
88
- const imports: string[] = [];
89
- for (const pkg of [...byPackage.keys()].sort()) {
90
- const symbols = [...(byPackage.get(pkg) ?? new Set<string>())].sort();
91
- imports.push(`import { ${symbols.join(", ")} } from "${pkg}";`);
92
- }
93
- return { imports, inits, registrations };
77
+ return { imports, inits, registrations };
94
78
  }
95
-
96
- function renderPermissionsField(ir: IrBatchV0): string {
97
- const { mode, rules } = ir.permissions;
98
- if (mode === undefined && rules.length === 0) return "";
99
- const lines: string[] = [];
100
- if (mode !== undefined) {
101
- lines.push(` permissionMode: ${escapeJsonString(mode)},`);
102
- }
103
- if (rules.length > 0) {
104
- const ruleLits = rules
105
- .map(
106
- (r) =>
107
- ` { type: ${escapeJsonString(r.type)}, pattern: ${escapeJsonString(r.pattern)}, source: "yaml" },`,
108
- )
109
- .join("\n");
110
- lines.push(
111
- [
112
- " permissionRules: {",
113
- " flag: [],",
114
- " settings: [],",
115
- " yaml: [",
116
- ruleLits,
117
- " ],",
118
- " hooks: [],",
119
- " builtin: BUILTIN_DEFAULT_RULES,",
120
- " },",
121
- ].join("\n"),
122
- );
123
- }
124
- return `\n${lines.join("\n")}`;
79
+ function renderPermissionsField(ir) {
80
+ const { mode, rules } = ir.permissions;
81
+ if (mode === undefined && rules.length === 0)
82
+ return "";
83
+ const lines = [];
84
+ if (mode !== undefined) {
85
+ lines.push(` permissionMode: ${escapeJsonString(mode)},`);
86
+ }
87
+ if (rules.length > 0) {
88
+ const ruleLits = rules
89
+ .map((r) => ` { type: ${escapeJsonString(r.type)}, pattern: ${escapeJsonString(r.pattern)}, source: "yaml" },`)
90
+ .join("\n");
91
+ lines.push([
92
+ " permissionRules: {",
93
+ " flag: [],",
94
+ " settings: [],",
95
+ " yaml: [",
96
+ ruleLits,
97
+ " ],",
98
+ " hooks: [],",
99
+ " builtin: BUILTIN_DEFAULT_RULES,",
100
+ " },",
101
+ ].join("\n"));
102
+ }
103
+ return `\n${lines.join("\n")}`;
125
104
  }
126
-
127
- export function emitBatchWorker(ir: IrBatchV0): Bundle {
128
- if (ir.queue.adapter !== "in-memory") {
129
- // v0 only ships the in-memory adapter; the others are stubs that
130
- // throw at runtime so the operator sees a clean diagnostic.
131
- // Codegen still succeeds — we emit the daemon, but boot fails.
132
- }
133
- return { files: [{ path: "agent.ts", content: renderAgent(ir) }] };
105
+ export function emitBatchWorker(ir) {
106
+ if (ir.queue.adapter !== "in-memory") {
107
+ // v0 only ships the in-memory adapter; the others are stubs that
108
+ // throw at runtime so the operator sees a clean diagnostic.
109
+ // Codegen still succeeds — we emit the daemon, but boot fails.
110
+ }
111
+ return { files: [{ path: "agent.ts", content: renderAgent(ir) }] };
134
112
  }
135
-
136
- function renderAgent(ir: IrBatchV0): string {
137
- const { imports: builtinImports, inits, registrations } = resolveTools(ir.tools, ir.toolConfigs);
138
- const importBlock = builtinImports.length > 0 ? `${builtinImports.join("\n")}\n` : "";
139
- const initLines = inits.length > 0 ? `${inits.join("\n")}\n` : "";
140
- const registrationBlock =
141
- registrations.length > 0
142
- ? `\n// Spec-supplied tools registered at module load.\n${registrations.join("\n")}\n`
143
- : "";
144
- const permField = renderPermissionsField(ir);
145
-
146
- const seedJobsLit = JSON.stringify(ir.queue.seedJobs ?? []);
147
- const visRenewMs =
148
- ir.queue.visibilityRenewIntervalMs ?? Math.floor(ir.queue.visibilityTimeoutMs / 4);
149
- const hasRules = ir.permissions.rules.length > 0;
150
- const permImport = hasRules
151
- ? `import { BUILTIN_DEFAULT_RULES } from "@crewhaus/permission-engine";\n`
152
- : "";
153
-
154
- return `#!/usr/bin/env bun
113
+ function renderAgent(ir) {
114
+ const { imports: builtinImports, inits, registrations } = resolveTools(ir.tools, ir.toolConfigs);
115
+ const importBlock = builtinImports.length > 0 ? `${builtinImports.join("\n")}\n` : "";
116
+ const initLines = inits.length > 0 ? `${inits.join("\n")}\n` : "";
117
+ const registrationBlock = registrations.length > 0
118
+ ? `\n// Spec-supplied tools registered at module load.\n${registrations.join("\n")}\n`
119
+ : "";
120
+ const permField = renderPermissionsField(ir);
121
+ const seedJobsLit = JSON.stringify(ir.queue.seedJobs ?? []);
122
+ const visRenewMs = ir.queue.visibilityRenewIntervalMs ?? Math.floor(ir.queue.visibilityTimeoutMs / 4);
123
+ const hasRules = ir.permissions.rules.length > 0;
124
+ const permImport = hasRules
125
+ ? `import { BUILTIN_DEFAULT_RULES } from "@crewhaus/permission-engine";\n`
126
+ : "";
127
+ return `#!/usr/bin/env bun
155
128
  // Generated by crewhaus. DO NOT EDIT.
156
129
  // Source spec: ${escapeJsonString(ir.name)} (target: batch, ir version: ${ir.version})
157
130
  import { createInMemoryQueue } from "@crewhaus/queue-protocol";
package/package.json CHANGED
@@ -1,20 +1,23 @@
1
1
  {
2
2
  "name": "@crewhaus/target-batch-worker",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "description": "Codegen for the BATCH target — emits a queue-consumer daemon (Section 23 BATCH)",
6
- "main": "src/index.ts",
7
- "types": "src/index.ts",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
8
  "exports": {
9
- ".": "./src/index.ts"
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
10
13
  },
11
14
  "scripts": {
12
15
  "test": "bun test src"
13
16
  },
14
17
  "dependencies": {
15
- "@crewhaus/errors": "0.1.4",
16
- "@crewhaus/infra-utils": "0.1.4",
17
- "@crewhaus/ir": "0.1.4"
18
+ "@crewhaus/errors": "0.1.6",
19
+ "@crewhaus/infra-utils": "0.1.6",
20
+ "@crewhaus/ir": "0.1.6"
18
21
  },
19
22
  "license": "Apache-2.0",
20
23
  "author": {
@@ -34,5 +37,5 @@
34
37
  "publishConfig": {
35
38
  "access": "public"
36
39
  },
37
- "files": ["src", "README.md", "LICENSE", "NOTICE"]
40
+ "files": ["dist", "README.md", "LICENSE", "NOTICE"]
38
41
  }
package/src/index.test.ts DELETED
@@ -1,98 +0,0 @@
1
- import { describe, expect, test } from "bun:test";
2
- import type { IrBatchV0 } from "@crewhaus/ir";
3
- import { TargetEmitError, emitBatchWorker } from "./index.js";
4
-
5
- const baseIr: IrBatchV0 = {
6
- version: 0,
7
- name: "hello-batch",
8
- target: "batch",
9
- agent: {
10
- model: "claude-haiku-4-5-20251001",
11
- instructions: "You are a batch worker.",
12
- },
13
- queue: {
14
- adapter: "in-memory",
15
- visibilityTimeoutMs: 30_000,
16
- maxRetries: 3,
17
- seedJobs: ["job 1 input", "job 2 input"],
18
- },
19
- concurrency: 4,
20
- idempotencyWindowMs: 60_000,
21
- tools: [],
22
- toolConfigs: Object.freeze({}),
23
- mcp_servers: Object.freeze({}),
24
- permissions: { rules: [] },
25
- compaction: {},
26
- };
27
-
28
- describe("emitBatchWorker", () => {
29
- test("emits a single agent.ts file (T1 bundle structure)", () => {
30
- const bundle = emitBatchWorker(baseIr);
31
- expect(bundle.files).toHaveLength(1);
32
- expect(bundle.files[0]?.path).toBe("agent.ts");
33
- });
34
-
35
- test("agent.ts wires queue-protocol + queue-consumer + idempotency-keys", () => {
36
- const code = emitBatchWorker(baseIr).files[0]?.content ?? "";
37
- expect(code).toContain("@crewhaus/queue-protocol");
38
- expect(code).toContain("@crewhaus/queue-consumer");
39
- expect(code).toContain("@crewhaus/idempotency-keys");
40
- expect(code).toContain("createInMemoryQueue");
41
- expect(code).toContain("startConsumer");
42
- });
43
-
44
- test("seeds the in-memory queue with the spec's seedJobs", () => {
45
- const code = emitBatchWorker(baseIr).files[0]?.content ?? "";
46
- expect(code).toContain('"job 1 input"');
47
- expect(code).toContain('"job 2 input"');
48
- expect(code).toContain("initialJobs: SPEC_SEED_JOBS");
49
- });
50
-
51
- test("hard-codes concurrency / visibilityTimeoutMs / idempotencyWindowMs from the spec", () => {
52
- const code = emitBatchWorker(baseIr).files[0]?.content ?? "";
53
- expect(code).toContain("SPEC_CONCURRENCY = 4");
54
- expect(code).toContain("SPEC_VISIBILITY_TIMEOUT_MS = 30000");
55
- expect(code).toContain("SPEC_IDEMPOTENCY_WINDOW_MS = 60000");
56
- expect(code).toContain("SPEC_MAX_RETRIES = 3");
57
- });
58
-
59
- test("emits SIGTERM/SIGINT shutdown handlers and a queue_idle exit path", () => {
60
- const code = emitBatchWorker(baseIr).files[0]?.content ?? "";
61
- expect(code).toContain('process.on("SIGINT"');
62
- expect(code).toContain('process.on("SIGTERM"');
63
- expect(code).toContain('"queue_idle"');
64
- expect(code).toContain('"worker_stop"');
65
- });
66
-
67
- test("non-in-memory adapter compiles, but boot throws (clean diagnostic)", () => {
68
- const ir: IrBatchV0 = {
69
- ...baseIr,
70
- queue: { ...baseIr.queue, adapter: "sqs" },
71
- };
72
- const code = emitBatchWorker(ir).files[0]?.content ?? "";
73
- expect(code).toContain('"in-memory"');
74
- expect(code).toContain("not implemented in v0");
75
- });
76
-
77
- test("rejects unknown spec-side tool names at compile time", () => {
78
- const ir: IrBatchV0 = {
79
- ...baseIr,
80
- tools: ["nonexistent"],
81
- };
82
- expect(() => emitBatchWorker(ir)).toThrow(TargetEmitError);
83
- });
84
-
85
- test("permissions block: passes spec yaml-source rules through", () => {
86
- const ir: IrBatchV0 = {
87
- ...baseIr,
88
- permissions: {
89
- mode: "default",
90
- rules: [{ type: "alwaysAllow", pattern: "Read" }],
91
- },
92
- };
93
- const code = emitBatchWorker(ir).files[0]?.content ?? "";
94
- expect(code).toContain('permissionMode: "default"');
95
- expect(code).toContain('pattern: "Read"');
96
- expect(code).toContain("BUILTIN_DEFAULT_RULES");
97
- });
98
- });