@crewhaus/target-batch-worker 0.4.2 → 0.5.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.js +72 -8
- package/package.json +5 -4
package/dist/index.js
CHANGED
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
* on this surface, which has nobody to prompt.
|
|
32
32
|
*/
|
|
33
33
|
import { CrewhausError } from "@crewhaus/errors";
|
|
34
|
+
import { renderControlDrain, renderControlImports, renderControlLane, renderControlPlaneBoot, renderControlStart, renderControlTimer, } from "@crewhaus/gateway-protocol/control-codegen";
|
|
34
35
|
import { escapeJsonString } from "@crewhaus/infra-utils";
|
|
35
36
|
import { renderBundleReadme, } from "@crewhaus/ir";
|
|
36
37
|
export class TargetEmitError extends CrewhausError {
|
|
@@ -502,18 +503,40 @@ function renderAgent(ir) {
|
|
|
502
503
|
// `schedule:` emits nothing, so unscheduled worker bundles stay
|
|
503
504
|
// byte-identical.
|
|
504
505
|
const scheduleImport = ir.schedule
|
|
505
|
-
? `import { armSchedule } from "@crewhaus/durable-execution";\n`
|
|
506
|
+
? `import { armSchedule, nextWakeDelayMs } from "@crewhaus/durable-execution";\n`
|
|
506
507
|
: "";
|
|
508
|
+
// crewhaus.control.v1 — the wake body is a control LANE, so `POST
|
|
509
|
+
// /control/v1/wake {lane:"schedule"}` enqueues a job down the exact path the
|
|
510
|
+
// timer uses (and 409s while an enqueue is in flight). This shape is a
|
|
511
|
+
// PRODUCER: the wake enqueues a job and the consumer's handler mints its own
|
|
512
|
+
// session, so the lane never threads `__tick.sessionId` anywhere. It
|
|
513
|
+
// therefore declares `ownsSession: false` — the 202 answers with the enqueued
|
|
514
|
+
// `jobId` on the event stream rather than a session id that names nothing,
|
|
515
|
+
// and no orphan marker `.jsonl` is left where retention can never reach it.
|
|
507
516
|
const scheduleBoot = ir.schedule
|
|
508
517
|
? `
|
|
509
518
|
// Loop contract 0.4 (Batch F) — schedule: wake loop (cron|interval + jitter)
|
|
510
519
|
const __scheduleInstructions = ${escapeJsonString(ir.schedule.instructions ?? "[scheduled wake]")};
|
|
520
|
+
const __scheduleSpec = ${wakeScheduleLiteral(ir.schedule)} as const;
|
|
511
521
|
let __scheduleTick = 0;
|
|
512
|
-
|
|
522
|
+
${renderControlLane({
|
|
523
|
+
lane: "schedule",
|
|
524
|
+
varName: "__scheduleLane",
|
|
525
|
+
cadence: describeSchedule(ir.schedule),
|
|
526
|
+
nextDueAtExpr: "new Date(Date.now() + nextWakeDelayMs(__scheduleSpec, Date.now())).toISOString()",
|
|
527
|
+
indent: " ",
|
|
528
|
+
ownsSession: false,
|
|
529
|
+
body: `__scheduleTick++;
|
|
530
|
+
const jobId = await queue.enqueue(__scheduleInstructions);
|
|
531
|
+
emit({
|
|
532
|
+
kind: "scheduled_wake",
|
|
533
|
+
tick: __scheduleTick,
|
|
534
|
+
jobId,
|
|
535
|
+
...(__tick.synthetic !== undefined ? { synthetic: true, reason: __tick.synthetic.reason, by: __tick.synthetic.by } : {}),
|
|
536
|
+
});`,
|
|
537
|
+
})} const __schedule = armSchedule(__scheduleSpec, {
|
|
513
538
|
onWake: async () => {
|
|
514
|
-
|
|
515
|
-
const jobId = await queue.enqueue(__scheduleInstructions);
|
|
516
|
-
emit({ kind: "scheduled_wake", tick: __scheduleTick, jobId });
|
|
539
|
+
await __scheduleLane.tick();
|
|
517
540
|
},
|
|
518
541
|
onError: (err) => emit({ kind: "scheduled_wake_error", error: (err as Error).message ?? String(err) }),
|
|
519
542
|
});
|
|
@@ -521,11 +544,37 @@ function renderAgent(ir) {
|
|
|
521
544
|
`
|
|
522
545
|
: "";
|
|
523
546
|
const scheduleShutdown = ir.schedule ? "\n __schedule.cancel();" : "";
|
|
547
|
+
// crewhaus.control.v1, from the shared gateway-protocol renderers. The batch
|
|
548
|
+
// worker has no public HTTP port, so control.v1 is its ONLY remote surface;
|
|
549
|
+
// it is also the shape whose drain semantics already existed (the consumer
|
|
550
|
+
// drains in-flight jobs on SIGTERM) — control.v1 just gives an operator a
|
|
551
|
+
// signal-free way to ask for it.
|
|
552
|
+
const controlPlaneBoot = renderControlPlaneBoot({
|
|
553
|
+
name: ir.name,
|
|
554
|
+
target: "batch",
|
|
555
|
+
indent: " ",
|
|
556
|
+
auditLogExpr: "__controlAudit",
|
|
557
|
+
});
|
|
558
|
+
const controlJanitorTimer = renderControlTimer({
|
|
559
|
+
indent: " ",
|
|
560
|
+
expr: '{ lane: "janitor", cadence: `every ${__janitorIntervalMs}ms`, ' +
|
|
561
|
+
'...(__janitorLastRunAt !== undefined ? { lastFiredAt: __janitorLastRunAt, lastOutcome: "ok" } : {}) }',
|
|
562
|
+
});
|
|
563
|
+
const controlDrain = renderControlDrain({
|
|
564
|
+
indent: " ",
|
|
565
|
+
body: `stopping = true;
|
|
566
|
+
janitor.stop();${ir.schedule ? "\n__schedule.cancel();" : ""}
|
|
567
|
+
emit({ kind: "drain_requested", via: "control.v1" });
|
|
568
|
+
await consumer.drain();${mcp.hasAny ? `\n${mcp.cleanupBlock}` : ""}
|
|
569
|
+
emit({ kind: "worker_stop", stats: await queue.stats() });`,
|
|
570
|
+
});
|
|
571
|
+
const controlStart = renderControlStart({ indent: " " });
|
|
524
572
|
return `#!/usr/bin/env bun
|
|
525
573
|
// Generated by crewhaus. DO NOT EDIT.
|
|
526
574
|
// Source spec: ${escapeJsonString(ir.name)} (target: batch, ir version: ${ir.version})
|
|
527
575
|
${continuityWarning}${queueBuild.importLine}
|
|
528
|
-
import {
|
|
576
|
+
import { openAuditLog } from "@crewhaus/audit-log";
|
|
577
|
+
${renderControlImports()}import { startConsumer } from "@crewhaus/queue-consumer";
|
|
529
578
|
import { createInMemoryIdempotencyStore } from "@crewhaus/idempotency-keys";
|
|
530
579
|
import { loadRetentionConfig } from "@crewhaus/data-retention-engine";
|
|
531
580
|
import { createJanitor, runChatLoop } from "@crewhaus/runtime-core";
|
|
@@ -552,6 +601,13 @@ function emit(event: Record<string, unknown>): void {
|
|
|
552
601
|
${queueBuild.buildFn}
|
|
553
602
|
|
|
554
603
|
async function main(): Promise<void> {
|
|
604
|
+
// control.v1 — the harness-wide hash-chained audit log every control call
|
|
605
|
+
// appends a \`gateway_request\` record to. CREWHAUS_SECURITY_AUDIT=0 opts out.
|
|
606
|
+
const __controlAudit =
|
|
607
|
+
process.env["CREWHAUS_SECURITY_AUDIT"] === "0"
|
|
608
|
+
? undefined
|
|
609
|
+
: await openAuditLog({ rootDir: \`\${process.cwd()}/.crewhaus/audit\` });
|
|
610
|
+
${controlPlaneBoot}
|
|
555
611
|
// Boot-time self-heal janitor (ops item 36): evicts expired sessions on a
|
|
556
612
|
// schedule (TTL eviction otherwise only fires as a list() side-effect an
|
|
557
613
|
// idle worker never triggers) and reports orphaned tool_use entries in
|
|
@@ -579,11 +635,16 @@ async function main(): Promise<void> {
|
|
|
579
635
|
sessionTtlDays: retentionTtlDays,
|
|
580
636
|
pinnedSessionIds: retentionPins,
|
|
581
637
|
});
|
|
638
|
+
const __janitorIntervalMs = Number(process.env["CREWHAUS_JANITOR_INTERVAL_MS"] ?? 3_600_000);
|
|
639
|
+
let __janitorLastRunAt: string | undefined;
|
|
582
640
|
if (process.env["CREWHAUS_JANITOR"] !== "0") {
|
|
583
641
|
const janitorReport = await janitor.runOnce();
|
|
642
|
+
__janitorLastRunAt = new Date().toISOString();
|
|
643
|
+
__control.counters.janitorRuns++;
|
|
584
644
|
process.stderr.write(\`[janitor] \${JSON.stringify(janitorReport.steps)}\\n\`);
|
|
585
|
-
janitor.start(
|
|
645
|
+
janitor.start(__janitorIntervalMs);
|
|
586
646
|
}
|
|
647
|
+
${controlJanitorTimer}
|
|
587
648
|
|
|
588
649
|
${mcpBoot} // G11 — a compiled bundle is NON-INTERACTIVE: a tool that lands on \`ask\`
|
|
589
650
|
// has nobody to prompt, so without this it collapsed to a deny. Rooted
|
|
@@ -625,6 +686,9 @@ ${mcpBoot} // G11 — a compiled bundle is NON-INTERACTIVE: a tool that lands o
|
|
|
625
686
|
installSigintHandler: false,
|
|
626
687
|
maxTokens: ${ir.agent.maxTokens ?? 1024},${budgetField(ir, " ")}${limitsFields(ir, " ")}${hooksField}${permField}${renderApprovalFields(ir, " ")}
|
|
627
688
|
});
|
|
689
|
+
// control.v1 counters.turns — bumped where the model actually ran, so a
|
|
690
|
+
// cache-served re-delivery (\`job_end.fromCache\`) is not double-counted.
|
|
691
|
+
__control.counters.turns++;
|
|
628
692
|
return reply.trim();
|
|
629
693
|
},
|
|
630
694
|
concurrency: SPEC_CONCURRENCY,
|
|
@@ -697,7 +761,7 @@ ${scheduleBoot}
|
|
|
697
761
|
};
|
|
698
762
|
process.on("SIGINT", () => void shutdown("SIGINT"));
|
|
699
763
|
process.on("SIGTERM", () => void shutdown("SIGTERM"));
|
|
700
|
-
|
|
764
|
+
${controlDrain}${controlStart}
|
|
701
765
|
${idleLoop}
|
|
702
766
|
}
|
|
703
767
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crewhaus/target-batch-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.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,10 @@
|
|
|
15
15
|
"test": "bun test src"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@crewhaus/errors": "0.
|
|
19
|
-
"@crewhaus/
|
|
20
|
-
"@crewhaus/
|
|
18
|
+
"@crewhaus/errors": "0.5.0",
|
|
19
|
+
"@crewhaus/gateway-protocol": "0.5.0",
|
|
20
|
+
"@crewhaus/infra-utils": "0.5.0",
|
|
21
|
+
"@crewhaus/ir": "0.5.0"
|
|
21
22
|
},
|
|
22
23
|
"license": "Apache-2.0",
|
|
23
24
|
"author": {
|