@crewhaus/target-batch-worker 0.4.0 → 0.4.2

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
@@ -16,12 +16,19 @@
16
16
  * the job input as the user message; the assistant's terminal text
17
17
  * is the cached result.
18
18
  * 4. Streams JSON events to stdout (`worker_start | job_start |
19
- * job_end | drain_start | drain_end | worker_stop`).
19
+ * job_end | drain_start | drain_end | worker_stop`). A successful
20
+ * `job_end` carries the handler's `result` (the assistant's terminal
21
+ * text) and `resultBytes`, so the run's observable output contains the
22
+ * work product and not just status — `CREWHAUS_BATCH_EMIT_RESULT=0`
23
+ * drops the payload and keeps `resultBytes`.
20
24
  * 5. Installs SIGTERM + SIGINT handlers that drain in-flight jobs
21
25
  * cleanly before exit.
22
26
  * 6. Runs the boot-time self-heal janitor (ops item 36) — session TTL
23
27
  * eviction + orphaned-tool_use report — at boot and hourly
24
28
  * (`CREWHAUS_JANITOR=0` / `CREWHAUS_JANITOR_INTERVAL_MS` to tune).
29
+ * 7. Builds the pending-approval store (G11) so a tool that resolves to
30
+ * `ask` PARKS for an out-of-band grant instead of collapsing to a deny
31
+ * on this surface, which has nobody to prompt.
25
32
  */
26
33
  import { CrewhausError } from "@crewhaus/errors";
27
34
  import { type Bundle, type EmitReadmeOptions, type IrBatchV0 } from "@crewhaus/ir";
package/dist/index.js CHANGED
@@ -16,12 +16,19 @@
16
16
  * the job input as the user message; the assistant's terminal text
17
17
  * is the cached result.
18
18
  * 4. Streams JSON events to stdout (`worker_start | job_start |
19
- * job_end | drain_start | drain_end | worker_stop`).
19
+ * job_end | drain_start | drain_end | worker_stop`). A successful
20
+ * `job_end` carries the handler's `result` (the assistant's terminal
21
+ * text) and `resultBytes`, so the run's observable output contains the
22
+ * work product and not just status — `CREWHAUS_BATCH_EMIT_RESULT=0`
23
+ * drops the payload and keeps `resultBytes`.
20
24
  * 5. Installs SIGTERM + SIGINT handlers that drain in-flight jobs
21
25
  * cleanly before exit.
22
26
  * 6. Runs the boot-time self-heal janitor (ops item 36) — session TTL
23
27
  * eviction + orphaned-tool_use report — at boot and hourly
24
28
  * (`CREWHAUS_JANITOR=0` / `CREWHAUS_JANITOR_INTERVAL_MS` to tune).
29
+ * 7. Builds the pending-approval store (G11) so a tool that resolves to
30
+ * `ask` PARKS for an out-of-band grant instead of collapsing to a deny
31
+ * on this surface, which has nobody to prompt.
25
32
  */
26
33
  import { CrewhausError } from "@crewhaus/errors";
27
34
  import { escapeJsonString } from "@crewhaus/infra-utils";
@@ -108,6 +115,30 @@ function renderPermissionsField(ir) {
108
115
  }
109
116
  return `\n${lines.join("\n")}`;
110
117
  }
118
+ /**
119
+ * Loop contract 0.4 (G11) — the `askMode` + `approvals` fields. Unlike the
120
+ * CLI's `approvalRunOptions`, a bundle parses no `--ask-mode`, so the spec
121
+ * value is FIXED here at emit time. The store is built UNCONDITIONALLY,
122
+ * including under `"deny"` where it never parks, so runtime-core's denial
123
+ * diagnostic can honestly say `ask_mode: "deny"` instead of blaming absent
124
+ * plumbing (it branches on `approvals === undefined`).
125
+ *
126
+ * Deliberately SEPARATE from renderPermissionsField, which early-returns ""
127
+ * when the spec declares no `permissions:` block — precisely the case where
128
+ * parking matters most, since with no block every unmatched tool resolves to
129
+ * `ask`.
130
+ *
131
+ * CAVEAT unique to this shape: a park throws `RunFailedError`
132
+ * (`approval_pending`) out of the queue handler, so the queue-consumer sees a
133
+ * failed job and retries it per SPEC_MAX_RETRIES — the job can dead-letter
134
+ * before a human ever grants. The store keys on (toolName, inputHash), so a
135
+ * later re-delivery DOES find the grant and re-resolves pre-decided, but only
136
+ * while the retry budget lasts.
137
+ */
138
+ function renderApprovalFields(ir, indent) {
139
+ return (`\n${indent}askMode: ${escapeJsonString(ir.permissions.askMode ?? "pause")},` +
140
+ `\n${indent}approvals: { store: __approvals, surface: "batch" },`);
141
+ }
111
142
  /**
112
143
  * Adaptive model routing — render the `modelPool` runChatLoop field.
113
144
  * JSON.stringify safely quotes the validated pool object (mirroring
@@ -498,6 +529,7 @@ import { startConsumer } from "@crewhaus/queue-consumer";
498
529
  import { createInMemoryIdempotencyStore } from "@crewhaus/idempotency-keys";
499
530
  import { loadRetentionConfig } from "@crewhaus/data-retention-engine";
500
531
  import { createJanitor, runChatLoop } from "@crewhaus/runtime-core";
532
+ import { createPendingApprovalStore, resolveSessionRootDir } from "@crewhaus/runtime-core";
501
533
  import { createRunContext } from "@crewhaus/run-context";
502
534
  import { defaultCatalog } from "@crewhaus/tool-catalog";
503
535
  ${permImport}${hooksImport}${scheduleImport}${mcpImportBlock}${importBlock}
@@ -553,7 +585,26 @@ async function main(): Promise<void> {
553
585
  janitor.start(Number(process.env["CREWHAUS_JANITOR_INTERVAL_MS"] ?? 3_600_000));
554
586
  }
555
587
 
556
- ${mcpBoot} const queue = buildQueue();
588
+ ${mcpBoot} // G11 — a compiled bundle is NON-INTERACTIVE: a tool that lands on \`ask\`
589
+ // has nobody to prompt, so without this it collapsed to a deny. Rooted
590
+ // where the run's session files land, so parks live beside them (and
591
+ // inside a tenant's rebased root when one is active). No I/O until a park.
592
+ // A park fails the job, so it is retried per SPEC_MAX_RETRIES — an
593
+ // out-of-band grant lands on the same (toolName, inputHash) key and the
594
+ // next delivery within that budget proceeds.
595
+ const __approvalRoot = resolveSessionRootDir(undefined);
596
+ const __approvals = createPendingApprovalStore(
597
+ __approvalRoot !== undefined ? { rootDir: __approvalRoot } : {},
598
+ );
599
+
600
+ const queue = buildQueue();
601
+ // Idempotency window (SPEC_IDEMPOTENCY_WINDOW_MS): caches each job's
602
+ // SUCCESSFUL result under its job id for the window, so a job that is
603
+ // re-delivered after already completing (a lost ack, a crash between
604
+ // handler and ack, an expired visibility lease, a competing consumer)
605
+ // is served from cache instead of paying for the model twice — that
606
+ // job_end reports \`fromCache: true\`. A clean run never re-delivers, so
607
+ // \`fromCache\` staying false throughout means nothing was duplicated.
557
608
  const idempotencyStore = createInMemoryIdempotencyStore<string>();
558
609
  emit({ kind: "worker_start", queueAdapter: SPEC_QUEUE_ADAPTER, concurrency: SPEC_CONCURRENCY });
559
610
 
@@ -572,7 +623,7 @@ ${mcpBoot} const queue = buildQueue();
572
623
  seedMessages: [{ role: "user", content: input }],
573
624
  tools: defaultCatalog.list(),
574
625
  installSigintHandler: false,
575
- maxTokens: ${ir.agent.maxTokens ?? 1024},${budgetField(ir, " ")}${limitsFields(ir, " ")}${hooksField}${permField}
626
+ maxTokens: ${ir.agent.maxTokens ?? 1024},${budgetField(ir, " ")}${limitsFields(ir, " ")}${hooksField}${permField}${renderApprovalFields(ir, " ")}
576
627
  });
577
628
  return reply.trim();
578
629
  },
@@ -585,7 +636,38 @@ ${mcpBoot} const queue = buildQueue();
585
636
  observer: {
586
637
  onJobEnd: (job, outcome) => {
587
638
  if (outcome.kind === "ok") {
588
- emit({ kind: "job_end", jobId: job.id, attempt: job.attempt, status: "ok", fromCache: outcome.fromCache });
639
+ // The handler's return value IS the work product — a batch run
640
+ // whose stdout carried only status JSON had no observable result
641
+ // at all (replies were recoverable only from the session
642
+ // transcript). Emit it verbatim, plus its size so the line stays
643
+ // scannable. CREWHAUS_BATCH_EMIT_RESULT=0 suppresses the payload
644
+ // (large or sensitive results shipped to logs) while keeping
645
+ // resultBytes.
646
+ const includeResult = process.env["CREWHAUS_BATCH_EMIT_RESULT"] !== "0";
647
+ emit({
648
+ kind: "job_end",
649
+ jobId: job.id,
650
+ attempt: job.attempt,
651
+ status: "ok",
652
+ fromCache: outcome.fromCache,
653
+ resultBytes: Buffer.byteLength(outcome.value ?? "", "utf8"),
654
+ ...(includeResult ? { result: outcome.value } : {}),
655
+ });
656
+ } else if (outcome.kind === "deferred") {
657
+ // NOT a failure: the turn parked on a tool permission that needs a
658
+ // human. The job is neither ack'd nor dead-lettered — it returns to
659
+ // the queue after the defer window and the attempt does not count
660
+ // against maxRetries. Reported under its own status so an operator
661
+ // watching this stream can tell "waiting on someone" apart from
662
+ // "broken", and knows to run \`crewhaus approvals list\`.
663
+ emit({
664
+ kind: "job_end",
665
+ jobId: job.id,
666
+ attempt: job.attempt,
667
+ status: "awaiting_approval",
668
+ defers: outcome.defers,
669
+ error: (outcome.error as Error).message ?? String(outcome.error),
670
+ });
589
671
  } else {
590
672
  emit({
591
673
  kind: "job_end",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/target-batch-worker",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
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.4.0",
19
- "@crewhaus/infra-utils": "0.4.0",
20
- "@crewhaus/ir": "0.4.0"
18
+ "@crewhaus/errors": "0.4.2",
19
+ "@crewhaus/infra-utils": "0.4.2",
20
+ "@crewhaus/ir": "0.4.2"
21
21
  },
22
22
  "license": "Apache-2.0",
23
23
  "author": {