@crewhaus/target-batch-worker 0.1.8 → 0.2.1

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
@@ -17,11 +17,14 @@
17
17
  * job_end | drain_start | drain_end | worker_stop`).
18
18
  * 5. Installs SIGTERM + SIGINT handlers that drain in-flight jobs
19
19
  * cleanly before exit.
20
+ * 6. Runs the boot-time self-heal janitor (ops item 36) — session TTL
21
+ * eviction + orphaned-tool_use report — at boot and hourly
22
+ * (`CREWHAUS_JANITOR=0` / `CREWHAUS_JANITOR_INTERVAL_MS` to tune).
20
23
  */
21
24
  import { CrewhausError } from "@crewhaus/errors";
22
- import type { Bundle, IrBatchV0 } from "@crewhaus/ir";
25
+ import { type Bundle, type EmitReadmeOptions, type IrBatchV0 } from "@crewhaus/ir";
23
26
  export declare class TargetEmitError extends CrewhausError {
24
27
  readonly name = "TargetEmitError";
25
28
  constructor(message: string, cause?: unknown);
26
29
  }
27
- export declare function emitBatchWorker(ir: IrBatchV0): Bundle;
30
+ export declare function emitBatchWorker(ir: IrBatchV0, opts?: EmitReadmeOptions): Bundle;
package/dist/index.js CHANGED
@@ -17,9 +17,13 @@
17
17
  * job_end | drain_start | drain_end | worker_stop`).
18
18
  * 5. Installs SIGTERM + SIGINT handlers that drain in-flight jobs
19
19
  * cleanly before exit.
20
+ * 6. Runs the boot-time self-heal janitor (ops item 36) — session TTL
21
+ * eviction + orphaned-tool_use report — at boot and hourly
22
+ * (`CREWHAUS_JANITOR=0` / `CREWHAUS_JANITOR_INTERVAL_MS` to tune).
20
23
  */
21
24
  import { CrewhausError } from "@crewhaus/errors";
22
25
  import { escapeJsonString } from "@crewhaus/infra-utils";
26
+ import { renderBundleReadme, } from "@crewhaus/ir";
23
27
  export class TargetEmitError extends CrewhausError {
24
28
  name = "TargetEmitError";
25
29
  constructor(message, cause) {
@@ -102,13 +106,19 @@ function renderPermissionsField(ir) {
102
106
  }
103
107
  return `\n${lines.join("\n")}`;
104
108
  }
105
- export function emitBatchWorker(ir) {
109
+ export function emitBatchWorker(ir, opts = {}) {
106
110
  if (ir.queue.adapter !== "in-memory") {
107
111
  // v0 only ships the in-memory adapter; the others are stubs that
108
112
  // throw at runtime so the operator sees a clean diagnostic.
109
113
  // Codegen still succeeds — we emit the daemon, but boot fails.
110
114
  }
111
- return { files: [{ path: "agent.ts", content: renderAgent(ir) }] };
115
+ const files = [{ path: "agent.ts", content: renderAgent(ir) }];
116
+ // Item 42 — generated bundle README; default ON (`crewhaus compile
117
+ // --no-readme` opts out).
118
+ if (opts.readme !== false) {
119
+ files.push({ path: "README.md", content: renderBundleReadme(ir) });
120
+ }
121
+ return { files };
112
122
  }
113
123
  function renderAgent(ir) {
114
124
  const { imports: builtinImports, inits, registrations } = resolveTools(ir.tools, ir.toolConfigs);
@@ -130,7 +140,8 @@ function renderAgent(ir) {
130
140
  import { createInMemoryQueue } from "@crewhaus/queue-protocol";
131
141
  import { startConsumer } from "@crewhaus/queue-consumer";
132
142
  import { createInMemoryIdempotencyStore } from "@crewhaus/idempotency-keys";
133
- import { runChatLoop } from "@crewhaus/runtime-core";
143
+ import { loadRetentionConfig } from "@crewhaus/data-retention-engine";
144
+ import { createJanitor, runChatLoop } from "@crewhaus/runtime-core";
134
145
  import { createRunContext } from "@crewhaus/run-context";
135
146
  import { defaultCatalog } from "@crewhaus/tool-catalog";
136
147
  ${permImport}${importBlock}
@@ -163,6 +174,39 @@ function buildQueue(): ReturnType<typeof createInMemoryQueue<string>> {
163
174
  }
164
175
 
165
176
  async function main(): Promise<void> {
177
+ // Boot-time self-heal janitor (ops item 36): evicts expired sessions on a
178
+ // schedule (TTL eviction otherwise only fires as a list() side-effect an
179
+ // idle worker never triggers) and reports orphaned tool_use entries in
180
+ // recent transcripts (report-only). Eviction honors
181
+ // .crewhaus/retention.json (ops item 35) — the SAME pins +
182
+ // sessions.maxAgeDays the \`crewhaus retention\` CLI enforces; a malformed
183
+ // config fails safe (eviction disabled, worker keeps running). The report
184
+ // goes to stderr so the stdout JSON event stream stays untouched.
185
+ // CREWHAUS_JANITOR=0 disables entirely; CREWHAUS_JANITOR_INTERVAL_MS
186
+ // overrides the hourly re-run (0 keeps only the boot run; the timer is
187
+ // unref'd so the queue_idle exit path still terminates the process).
188
+ let retentionTtlDays: number;
189
+ let retentionPins: readonly string[] = [];
190
+ try {
191
+ const retention = await loadRetentionConfig(process.cwd());
192
+ retentionTtlDays = retention.sessionMaxAgeDays;
193
+ retentionPins = retention.pins;
194
+ } catch (err) {
195
+ process.stderr.write(
196
+ \`[batch-worker] .crewhaus/retention.json unreadable — janitor session eviction disabled: \${(err as Error).message}\\n\`,
197
+ );
198
+ retentionTtlDays = Number.POSITIVE_INFINITY; // fail-safe: evict nothing
199
+ }
200
+ const janitor = createJanitor({
201
+ sessionTtlDays: retentionTtlDays,
202
+ pinnedSessionIds: retentionPins,
203
+ });
204
+ if (process.env["CREWHAUS_JANITOR"] !== "0") {
205
+ const janitorReport = await janitor.runOnce();
206
+ process.stderr.write(\`[janitor] \${JSON.stringify(janitorReport.steps)}\\n\`);
207
+ janitor.start(Number(process.env["CREWHAUS_JANITOR_INTERVAL_MS"] ?? 3_600_000));
208
+ }
209
+
166
210
  const queue = buildQueue();
167
211
  const idempotencyStore = createInMemoryIdempotencyStore<string>();
168
212
  emit({ kind: "worker_start", queueAdapter: SPEC_QUEUE_ADAPTER, concurrency: SPEC_CONCURRENCY });
@@ -216,6 +260,7 @@ async function main(): Promise<void> {
216
260
  const shutdown = async (signal: string): Promise<void> => {
217
261
  if (stopping) return;
218
262
  stopping = true;
263
+ janitor.stop();
219
264
  emit({ kind: "shutdown_received", signal });
220
265
  await consumer.drain();
221
266
  const stats = await queue.stats();
@@ -244,6 +289,7 @@ async function main(): Promise<void> {
244
289
  consumer.inFlight() === 0
245
290
  ) {
246
291
  emit({ kind: "queue_idle", stats: recheck });
292
+ janitor.stop();
247
293
  await consumer.drain();
248
294
  emit({ kind: "worker_stop", stats: await queue.stats() });
249
295
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/target-batch-worker",
3
- "version": "0.1.8",
3
+ "version": "0.2.1",
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.1.8",
19
- "@crewhaus/infra-utils": "0.1.8",
20
- "@crewhaus/ir": "0.1.8"
18
+ "@crewhaus/errors": "0.2.1",
19
+ "@crewhaus/infra-utils": "0.2.1",
20
+ "@crewhaus/ir": "0.2.1"
21
21
  },
22
22
  "license": "Apache-2.0",
23
23
  "author": {