@crewhaus/target-crew 0.3.2 → 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
@@ -22,4 +22,18 @@ export declare class TargetEmitError extends CrewhausError {
22
22
  readonly name = "TargetEmitError";
23
23
  constructor(message: string, cause?: unknown);
24
24
  }
25
- export declare function emitCrew(ir: IrCrewV0, opts?: EmitReadmeOptions): Bundle;
25
+ /**
26
+ * Evals Wave 4, cluster S (D36 + NEW-shape-1) — `emitCrew` options.
27
+ * `evalEntry: true` (set only by `crewhaus compile --with-eval-harness`)
28
+ * additionally emits an `eval-entry.ts` file exporting
29
+ * `runForEval(input, opts)`: one crew run through the SAME compiled
30
+ * orchestrator + role definitions, with the SAME run options the daemon
31
+ * assembles (permissions/taxonomy/caps/limits/budget/hooks/MCP/memory),
32
+ * minus the stdin/stdout framing — the `crew_done` finalOutput is returned
33
+ * for grading. Purely ADDITIVE: the existing daemon/orchestrator/role files
34
+ * stay byte-identical with or without the option.
35
+ */
36
+ export type EmitCrewOptions = EmitReadmeOptions & {
37
+ readonly evalEntry?: boolean;
38
+ };
39
+ export declare function emitCrew(ir: IrCrewV0, opts?: EmitCrewOptions): Bundle;
package/dist/index.js CHANGED
@@ -77,9 +77,12 @@ export function emitCrew(ir, opts = {}) {
77
77
  .join(", ")})`);
78
78
  }
79
79
  const files = [
80
- { path: "orchestrator.ts", content: renderOrchestrator(ir) },
80
+ { path: "orchestrator.ts", content: renderOrchestrator(ir, entryRole) },
81
81
  { path: "daemon.ts", content: renderDaemon(ir) },
82
82
  ];
83
+ if (opts.evalEntry === true) {
84
+ files.push({ path: "eval-entry.ts", content: renderEvalEntry(ir) });
85
+ }
83
86
  for (const role of ir.roles) {
84
87
  files.push({ path: `agent_${safeFileName(role.name)}.ts`, content: renderRoleAgent(ir, role) });
85
88
  }
@@ -151,6 +154,53 @@ function renderPermissionsField(ir) {
151
154
  }
152
155
  return `\n${lines.join("\n")}`;
153
156
  }
157
+ /**
158
+ * Loop contract 0.4 (Batch C, G11) — the `askMode` + `approvals` crew
159
+ * RunOptions fields (the orchestrator forwards both to every role turn,
160
+ * primary activations and inline A2A peer turns alike). Unlike the CLI's
161
+ * `approvalRunOptions`, a bundle parses no `--ask-mode`, so the spec value is
162
+ * FIXED here at emit time.
163
+ *
164
+ * Deliberately NOT folded into {@link renderPermissionsField}: that renderer
165
+ * early-returns "" when the spec declares neither a mode nor rules — which is
166
+ * exactly the bundle where parking matters MOST, because with no block every
167
+ * unmatched tool resolves to `ask`. So this one is unconditional, including
168
+ * under `"deny"` where it never parks: runtime-core picks its denial wording
169
+ * by testing `approvals === undefined`, so handing it the store lets it
170
+ * honestly report `ask_mode: "deny"` instead of blaming absent plumbing.
171
+ */
172
+ function renderApprovalFields(ir, indent) {
173
+ return (`\n${indent}askMode: ${escapeJsonString(ir.permissions.askMode ?? "pause")},` +
174
+ `\n${indent}approvals: { store: __approvals, surface: "crew" },`);
175
+ }
176
+ /**
177
+ * G11 — the park store the fields above reference, emitted unconditionally
178
+ * into every file that assembles `crew.run` opts. Constructing it does no I/O;
179
+ * the first write happens at a park.
180
+ *
181
+ * `rootArg` is what the file can offer `resolveSessionRootDir`: the daemon has
182
+ * nothing (`undefined` → tenant root, else `CREWHAUS_SESSION_DIR`, else the
183
+ * store's own default), while the eval bridge passes its caller's per-sample
184
+ * directory so a parked sample's record — which embeds the raw tool input —
185
+ * lands in that sample's artifacts instead of the operator's working tree.
186
+ */
187
+ function renderApprovalStoreBoot(rootArg, indent = "") {
188
+ return `
189
+ ${indent}// G11 — a compiled bundle is NON-INTERACTIVE: a tool that lands on \`ask\`
190
+ ${indent}// has nobody to prompt, so without this it collapsed to a deny. Rooted
191
+ ${indent}// where the run's session files land, so parks live beside them (and
192
+ ${indent}// inside a tenant's rebased root when one is active). No I/O until a park.
193
+ ${indent}const __approvalRoot = resolveSessionRootDir(${rootArg});
194
+ ${indent}const __approvals = createPendingApprovalStore(
195
+ ${indent} __approvalRoot !== undefined ? { rootDir: __approvalRoot } : {},
196
+ ${indent});
197
+ `;
198
+ }
199
+ /** The store's imports. A SEPARATE line from any other runtime-core import
200
+ * the file may already carry — duplicate specifiers are already normal in
201
+ * emitted output, and one dedicated line keeps exact-import-line assertions
202
+ * (and diffs) stable. */
203
+ const APPROVAL_STORE_IMPORT = `import { createPendingApprovalStore, resolveSessionRootDir } from "@crewhaus/runtime-core";\n`;
154
204
  /**
155
205
  * Section 55 / item 23 — render the `failureTaxonomy` crew RunOptions field.
156
206
  * The orchestrator threads it into every role's runChatLoop (same crew-wide
@@ -163,25 +213,248 @@ function renderFailureTaxonomyField(ir) {
163
213
  return "";
164
214
  return `\n failureTaxonomy: ${JSON.stringify(taxonomy)},`;
165
215
  }
216
+ /**
217
+ * Loop contract 0.4 (Batch A) — the crew-only orchestration ceilings
218
+ * (`limits.crew`) map onto crew-orchestrator's EXISTING RunOptions caps:
219
+ * `max_activations` → `maxActivations`, `refusal_depth` → `refusalDepth`,
220
+ * `max_a2a_depth` → `maxA2ADepth`. Each cap is emitted only when the spec
221
+ * declared it — the orchestrator owns the defaults (16 / 2 / 3) — so
222
+ * existing bundles stay byte-identical.
223
+ */
224
+ function renderCrewCapsFields(ir) {
225
+ const crew = ir.limits?.crew;
226
+ if (crew === undefined)
227
+ return "";
228
+ const pieces = [];
229
+ if (crew.maxActivations !== undefined) {
230
+ pieces.push(`\n maxActivations: ${crew.maxActivations},`);
231
+ }
232
+ if (crew.refusalDepth !== undefined) {
233
+ pieces.push(`\n refusalDepth: ${crew.refusalDepth},`);
234
+ }
235
+ if (crew.maxA2aDepth !== undefined) {
236
+ pieces.push(`\n maxA2ADepth: ${crew.maxA2aDepth},`);
237
+ }
238
+ return pieces.join("");
239
+ }
240
+ /**
241
+ * Loop contract 0.4 (Batch A) — the per-turn loop ceilings (top-level
242
+ * `limits:` minus the crew-only block above) ride `RunOptions.limits`; the
243
+ * orchestrator forwards them to EVERY role's runChatLoop — primary
244
+ * activations and inline A2A peer turns alike (`composeLoopTuning`). Only
245
+ * declared knobs are present — the runtime owns every default. Mirror:
246
+ * target-cli + target-channel-bot + target-managed render the same knobs
247
+ * (as direct runChatLoop fields — crew is the one shape that routes them
248
+ * through an orchestrator).
249
+ */
250
+ function renderLoopLimitsField(ir) {
251
+ const limits = ir.limits;
252
+ if (limits === undefined)
253
+ return "";
254
+ const loop = {
255
+ ...(limits.maxToolIterations !== undefined
256
+ ? { maxToolIterations: limits.maxToolIterations }
257
+ : {}),
258
+ ...(limits.maxConcurrentTools !== undefined
259
+ ? { maxConcurrentTools: limits.maxConcurrentTools }
260
+ : {}),
261
+ ...(limits.contextLimit !== undefined ? { contextLimit: limits.contextLimit } : {}),
262
+ ...(limits.deadlineMs !== undefined ? { deadlineMs: limits.deadlineMs } : {}),
263
+ ...(limits.turnTimeoutMs !== undefined ? { turnTimeoutMs: limits.turnTimeoutMs } : {}),
264
+ ...(limits.modelCallTimeoutMs !== undefined
265
+ ? { modelCallTimeoutMs: limits.modelCallTimeoutMs }
266
+ : {}),
267
+ ...(limits.loopDetection !== undefined ? { loopDetection: limits.loopDetection } : {}),
268
+ };
269
+ if (Object.keys(loop).length === 0)
270
+ return "";
271
+ return `\n limits: ${JSON.stringify(loop)},`;
272
+ }
273
+ /** Item 27 (Batch A extends it to crew) — run-level spend cap + degradation
274
+ * ladder. The orchestrator forwards it to every role's runChatLoop, so the
275
+ * always-on cost meter accrues the WHOLE crew run against one cap. Mirror:
276
+ * target-cli + target-channel-bot render the same field. */
277
+ function renderBudgetField(ir) {
278
+ if (ir.budget === undefined)
279
+ return "";
280
+ return `\n budget: ${JSON.stringify(ir.budget)},`;
281
+ }
282
+ /**
283
+ * Loop contract 0.4 (Batch A) — spec-declared lifecycle hooks (`hooks:`).
284
+ * IrHook is HookDef-shaped by contract (the spec ↔ hooks-engine cross-check
285
+ * test pins the event list), so codegen embeds the array as a literal on
286
+ * `RunOptions.hooks` and the orchestrator forwards it to every role's
287
+ * runChatLoop. The crew daemon discovers no settings.json hooks (unlike
288
+ * cli), so the spec list is the whole array; declaration order is preserved
289
+ * (hooks run in registration order). Absent/empty leaves bundles
290
+ * byte-identical.
291
+ */
292
+ function renderHooksField(ir) {
293
+ if (ir.hooks === undefined || ir.hooks.length === 0)
294
+ return "";
295
+ return `\n hooks: ${JSON.stringify(ir.hooks)},`;
296
+ }
297
+ /**
298
+ * Loop contract 0.4 (Batch A, G05) — spec-declared MCP servers, wired ONCE
299
+ * at daemon boot (the wire-once pattern: one shared McpHost, one
300
+ * registration pass) into a dedicated catalog whose tools every role
301
+ * receives through the orchestrator's crew-wide `extraTools`. The
302
+ * UNRESOLVED IrSecretRef-valued config is embedded and resolved at boot
303
+ * (mirror of target-cli + target-channel-bot's renderMcpServers; keep in
304
+ * sync). One deliberate divergence: `onRegister` logs to STDERR — the crew
305
+ * daemon's stdout is the JSON-per-line event stream and must stay
306
+ * machine-parseable.
307
+ */
308
+ function renderMcpServers(ir) {
309
+ const entries = Object.entries(ir.mcp_servers);
310
+ if (entries.length === 0) {
311
+ return { imports: [], bootBlock: "", hasAny: false };
312
+ }
313
+ const imports = [
314
+ `import { McpHost, resolveMcpServerConfig } from "@crewhaus/mcp-host";`,
315
+ `import { registerMcpServer } from "@crewhaus/tool-mcp";`,
316
+ ];
317
+ const addLines = entries
318
+ .map(([name, cfg]) => ` mcpHost.addServer(${escapeJsonString(name)}, resolveMcpServerConfig(${JSON.stringify(cfg)}, { name: ${escapeJsonString(name)} }));`)
319
+ .join("\n");
320
+ const registerLines = entries
321
+ .map(([name]) => ` registerMcpServer(mcpHost, ${escapeJsonString(name)}, __mcpCatalog, { onRegister: ({ fullName }) => process.stderr.write(\`[mcp] registered \${fullName}\\n\`) }),`)
322
+ .join("\n");
323
+ const bootBlock = `
324
+ // Loop contract 0.4 (Batch A, G05) — MCP servers, wired ONCE: every
325
+ // role's turns see the remote tools through the orchestrator's crew-wide
326
+ // extraTools; the daemon disconnects the shared host on shutdown.
327
+ const mcpHost = new McpHost();
328
+ ${addLines}
329
+ const __mcpCatalog = new ToolCatalog();
330
+ await Promise.all([
331
+ ${registerLines}
332
+ ]);
333
+ const __mcpTools = __mcpCatalog.list();`;
334
+ return { imports, bootBlock, hasAny: true };
335
+ }
166
336
  // ---------------------------------------------------------------------------
167
337
  // File: agent_<role>.ts — exports the role's RoleDefinition.
168
338
  // ---------------------------------------------------------------------------
339
+ /** Render one IrSubAgentDefinition as a TS object literal — mirrors
340
+ * target-cli + target-channel-bot; keep the three in sync. */
341
+ function renderSubAgentDef(d) {
342
+ const lines = [];
343
+ lines.push(`name: ${escapeJsonString(d.name)}`);
344
+ lines.push(`description: ${escapeJsonString(d.description)}`);
345
+ lines.push(`instructions: ${escapeJsonString(d.instructions)}`);
346
+ lines.push(`tools: ${JSON.stringify(d.tools)}`);
347
+ if (d.model !== undefined)
348
+ lines.push(`model: ${escapeJsonString(d.model)}`);
349
+ if (typeof d.permissions === "string") {
350
+ lines.push(`permissions: ${escapeJsonString(d.permissions)}`);
351
+ }
352
+ else {
353
+ lines.push(`permissions: { allow: ${JSON.stringify(d.permissions.allow)}, deny: ${JSON.stringify(d.permissions.deny)} }`);
354
+ }
355
+ lines.push(`inherit_bypass: ${d.inheritBypass}`);
356
+ return `{ ${lines.join(", ")} }`;
357
+ }
358
+ /**
359
+ * Loop contract 0.4 (Batch A) — render the per-role tuning fields onto the
360
+ * RoleDefinition literal, camelCase-mirroring the IR 1:1: `max_tokens` →
361
+ * `maxTokens` (model max OUTPUT tokens per activation) and `thinking` →
362
+ * `thinking` (extended-thinking selector; exactly one of `{ budgetTokens }`
363
+ * / `{ effort }` by spec construction — the orchestrator forwards it to the
364
+ * role's runChatLoop turns). Each field is emitted only when the spec
365
+ * declared it — the runtime owns every default — so existing bundles stay
366
+ * byte-identical. Mirror: target-cli + target-channel-bot render the same
367
+ * knobs on their agent blocks.
368
+ */
369
+ function renderRoleTuningFields(role) {
370
+ const pieces = [];
371
+ if (role.maxTokens !== undefined) {
372
+ pieces.push(`\n maxTokens: ${role.maxTokens},`);
373
+ }
374
+ if (role.thinking !== undefined) {
375
+ pieces.push(`\n thinking: ${JSON.stringify(role.thinking)},`);
376
+ }
377
+ return pieces.join("");
378
+ }
379
+ /**
380
+ * Loop contract 0.4 (Batch G, item 9 / G37) — render the per-role model-
381
+ * routing fields onto the role's `RoleDefinition` literal. The pooled
382
+ * pattern is adopted verbatim from the cli agent block
383
+ * (`@crewhaus/target-cli`'s `renderModelFailoverFields`), so a PolicyRouter
384
+ * decision per role shares the same `@crewhaus/routing-store` scoreboard the
385
+ * cli/pipeline shapes use — runtime-core owns the router; the orchestrator
386
+ * forwards the RoleDefinition's config into this role's `runChatLoop` turns
387
+ * (primary activations AND inline A2A peer turns) exactly as it already
388
+ * forwards `maxTokens`/`thinking`. The four fields are mutually exclusive in
389
+ * the spec (`model_pool` ⊥ `model_tiers` ⊥ `model_fallbacks`, with
390
+ * `circuit_breaker` riding `model_fallbacks`), so at most one clause fires
391
+ * per role. Model strings pass through `escapeJsonString` (user-controlled
392
+ * spec values in generated source); the breaker/tiers/pool blocks are
393
+ * validated numbers/strings/closed-literal unions safe to `JSON.stringify`.
394
+ * Empty when the role declares none, keeping pre-existing bundles
395
+ * byte-identical. Mirror: target-cli + target-channel-bot + target-managed
396
+ * render the same fields on their agent blocks — keep the four in sync.
397
+ */
398
+ function renderRoleModelFailoverFields(role) {
399
+ const pieces = [];
400
+ const fallbacks = role.modelFallbacks;
401
+ if (fallbacks !== undefined && fallbacks.length > 0) {
402
+ pieces.push(`\n modelFallbacks: [${fallbacks.map((m) => escapeJsonString(m)).join(", ")}],`);
403
+ }
404
+ if (role.circuitBreaker !== undefined) {
405
+ pieces.push(`\n circuitBreaker: ${JSON.stringify(role.circuitBreaker)},`);
406
+ }
407
+ if (role.modelTiers !== undefined) {
408
+ pieces.push(`\n modelTiers: ${JSON.stringify(role.modelTiers)},`);
409
+ }
410
+ if (role.modelPool !== undefined) {
411
+ pieces.push(`\n modelPool: ${JSON.stringify(role.modelPool)},`);
412
+ }
413
+ return pieces.join("");
414
+ }
169
415
  function renderRoleAgent(ir, role) {
170
416
  const { imports, inits, registrations } = resolveTools(role.tools, role.toolConfigs);
171
- const importBlock = imports.length > 0 ? `${imports.join("\n")}\n` : "";
417
+ const hasSubAgents = role.subAgents.length > 0;
418
+ // Section 13 (Batch A, G34) — the role's inline sub-agent definitions:
419
+ // rendered as a Map for the Task tool (createTaskTool resolves
420
+ // subagent_type against it) AND threaded onto the RoleDefinition so the
421
+ // orchestrator forwards them to this role's runChatLoop bridge alongside
422
+ // the daemon-injected spawnSubAgent.
423
+ const subAgentImports = hasSubAgents
424
+ ? [
425
+ `import type { SubAgentDefinition } from "@crewhaus/agent-context-isolation";`,
426
+ `import { createTaskTool } from "@crewhaus/tool-task";`,
427
+ ]
428
+ : [];
429
+ const importBlock = imports.length > 0 || subAgentImports.length > 0
430
+ ? `${[...subAgentImports, ...imports].join("\n")}\n`
431
+ : "";
172
432
  const initLines = inits.length > 0 ? `${inits.join("\n")}\n` : "";
173
- const toolsArrayLiteral = registrations.length > 0 ? `[${registrations.join(", ")}]` : "[]";
433
+ const subAgentEntries = role.subAgents
434
+ .map((d) => ` [${escapeJsonString(d.name)}, ${renderSubAgentDef(d)}],`)
435
+ .join("\n");
436
+ const subAgentBlock = hasSubAgents
437
+ ? `const __subAgents: ReadonlyMap<string, SubAgentDefinition> = new Map<string, SubAgentDefinition>([
438
+ ${subAgentEntries}
439
+ ]);
440
+ `
441
+ : "";
442
+ const toolRegistrations = hasSubAgents
443
+ ? [...registrations, "createTaskTool({ subAgents: __subAgents })"]
444
+ : registrations;
445
+ const toolsArrayLiteral = toolRegistrations.length > 0 ? `[${toolRegistrations.join(", ")}]` : "[]";
446
+ const subAgentsField = hasSubAgents ? "\n subAgents: __subAgents," : "";
174
447
  return `// Generated by crewhaus. DO NOT EDIT.
175
448
  // Source spec: ${escapeJsonString(ir.name)} (target: crew, ir version: ${ir.version}, role: ${escapeJsonString(role.name)})
176
449
  ${importBlock}import type { RoleDefinition } from "@crewhaus/crew-orchestrator";
177
450
 
178
- ${initLines}
451
+ ${initLines}${subAgentBlock}
179
452
  export const role: { name: string; def: RoleDefinition } = {
180
453
  name: ${escapeJsonString(role.name)},
181
454
  def: {
182
455
  model: ${escapeJsonString(role.model)},
183
- instructions: ${escapeJsonString(role.instructions)},
184
- tools: ${toolsArrayLiteral},
456
+ instructions: ${escapeJsonString(role.instructions)},${renderRoleTuningFields(role)}${renderRoleModelFailoverFields(role)}
457
+ tools: ${toolsArrayLiteral},${subAgentsField}
185
458
  },
186
459
  };
187
460
  `;
@@ -189,14 +462,110 @@ export const role: { name: string; def: RoleDefinition } = {
189
462
  // ---------------------------------------------------------------------------
190
463
  // File: orchestrator.ts — composes Crew() from roles + entry + routing.
191
464
  // ---------------------------------------------------------------------------
192
- function renderOrchestrator(ir) {
465
+ /** Emit-time cap on the roster excerpt of each role's instructions inside
466
+ * the llm-router classify prompt — the classify turn must stay small. */
467
+ const LLM_ROUTER_DESCRIPTION_MAX_CHARS = 240;
468
+ /** Runtime cap (rendered into the bundle) on the finishing role's output
469
+ * fed to the classify turn. The TAIL is kept: routing signals ("DONE",
470
+ * "needs review", verdicts) conventionally close an output. */
471
+ const LLM_ROUTER_INPUT_MAX_CHARS = 2000;
472
+ function llmRouterDescription(instructions) {
473
+ return instructions.length > LLM_ROUTER_DESCRIPTION_MAX_CHARS
474
+ ? `${instructions.slice(0, LLM_ROUTER_DESCRIPTION_MAX_CHARS)}…`
475
+ : instructions;
476
+ }
477
+ /**
478
+ * Loop contract 0.4 (Batch A, G08) — `routing.kind: "llm"`: render a
479
+ * model-backed router into the generated orchestrator. One small
480
+ * `runChatLoop` classify turn runs after every role_end: it sees the role
481
+ * roster (names + emit-time-truncated instruction excerpts) plus the tail
482
+ * of the finishing role's output and must answer with EXACTLY one role
483
+ * name or DONE — the exact-token output contract is what keeps the
484
+ * decision stable at any sampling temperature. DONE (or re-picking the
485
+ * finishing role) terminates the crew; an unparseable reply falls back to
486
+ * the entry role. The turn runs on the entry role's model in its own
487
+ * session (`<name> (llm-router)`) so the crew transcript stays clean, and
488
+ * it rides the run's session root + `_adapter` test seam via the
489
+ * orchestrator's RouterArgs passthroughs.
490
+ */
491
+ function renderLlmRouter(ir, entryRole) {
492
+ const rosterEntries = ir.roles
493
+ .map((r) => ` { name: ${escapeJsonString(r.name)}, description: ${escapeJsonString(llmRouterDescription(r.instructions))} },`)
494
+ .join("\n");
495
+ const classifierInstructions = "You are the routing classifier for a multi-role agent crew. After a role finishes its turn you decide who acts next. Respond with EXACTLY one word: the name of the next role, or DONE when the crew's work is complete. No punctuation, no explanation — the exact-match contract keeps routing deterministic at any sampling temperature.";
496
+ const prelude = `
497
+ // Loop contract 0.4 (Batch A, G08) — routing.kind "llm": the roster the
498
+ // classify turn picks from (name + an instructions excerpt truncated at
499
+ // compile time to keep the classify prompt small).
500
+ const __roleRoster: ReadonlyArray<{ name: string; description: string }> = [
501
+ ${rosterEntries}
502
+ ];
503
+ const __roleNames: ReadonlyArray<string> = __roleRoster.map((r) => r.name);
504
+
505
+ /**
506
+ * Parse the classifier's reply into the next role. Priority ladder: exact
507
+ * role-name match (quotes/punctuation stripped) → that role; exact DONE →
508
+ * the finishing role (re-picking it terminates the crew); exactly one
509
+ * roster name mentioned → that role; DONE mentioned → the finishing role;
510
+ * anything else is unparseable and falls back to the entry role.
511
+ */
512
+ function __pickNextRole(reply: string, lastRole: string): string {
513
+ const text = reply.trim().toLowerCase();
514
+ const exact = text.replace(/^["'\`\\s]+|["'\`.!\\s]+$/g, "");
515
+ for (const name of __roleNames) {
516
+ if (name.toLowerCase() === exact) return name;
517
+ }
518
+ if (exact === "done") return lastRole;
519
+ const mentioned = __roleNames.filter((name) => text.includes(name.toLowerCase()));
520
+ const only = mentioned[0];
521
+ if (mentioned.length === 1 && only !== undefined) return only;
522
+ if (/\\bdone\\b/.test(text)) return lastRole;
523
+ return ${escapeJsonString(ir.entry)};
524
+ }
525
+ `;
526
+ const routerBlock = `
527
+ // Loop contract 0.4 (Batch A, G08) — model-backed routing: one small
528
+ // classify turn per role_end picks the next role (see __pickNextRole for
529
+ // the parse ladder). Temperature-stable by construction: exact-token
530
+ // output contract + tail-anchored output excerpt.
531
+ builder.setRouting(async ({ input, lastRole, sessionRootDir, _adapter }) => {
532
+ if (lastRole === undefined) return ${escapeJsonString(ir.entry)};
533
+ const roster = __roleRoster.map((r) => \`- \${r.name}: \${r.description}\`).join("\\n");
534
+ const excerpt =
535
+ input.length > ${LLM_ROUTER_INPUT_MAX_CHARS}
536
+ ? \`…\${input.slice(-${LLM_ROUTER_INPUT_MAX_CHARS})}\`
537
+ : input;
538
+ const reply = await runChatLoop({
539
+ model: ${escapeJsonString(entryRole.model)},
540
+ instructions: ${escapeJsonString(classifierInstructions)},
541
+ sessionName: ${escapeJsonString(`${ir.name} (llm-router)`)},
542
+ sessionTarget: "crew",
543
+ ...(sessionRootDir !== undefined ? { sessionRootDir } : {}),
544
+ singleTurn: true,
545
+ installSigintHandler: false,
546
+ maxTokens: 128,
547
+ seedMessages: [
548
+ {
549
+ role: "user",
550
+ content: \`Roles:\\n\${roster}\\n\\nRole that just finished: \${lastRole}\\n\\nIts output:\\n\${excerpt}\\n\\nWhich role acts next? Answer with exactly one of: \${__roleNames.join(", ")}, or DONE.\`,
551
+ },
552
+ ],
553
+ ...(_adapter !== undefined ? { _adapter } : {}),
554
+ });
555
+ return __pickNextRole(reply, lastRole);
556
+ });`;
557
+ return { prelude, routerBlock };
558
+ }
559
+ function renderOrchestrator(ir, entryRole) {
193
560
  const roleImports = ir.roles
194
561
  .map((r) => `import { role as ${roleVar(r.name)} } from "./agent_${safeFileName(r.name)}.js";`)
195
562
  .join("\n");
196
563
  const adds = ir.roles
197
564
  .map((r) => ` .addRole(${roleVar(r.name)}.name, ${roleVar(r.name)}.def)`)
198
565
  .join("\n");
566
+ let prelude = "";
199
567
  let routerBlock = "";
568
+ let runtimeImport = "";
200
569
  if (ir.routing && ir.routing.kind === "match" && ir.routing.match !== undefined) {
201
570
  const tableLit = JSON.stringify(ir.routing.match, null, 2)
202
571
  .split("\n")
@@ -214,11 +583,17 @@ ${tableLit};
214
583
  return lastRole;
215
584
  });`;
216
585
  }
586
+ else if (ir.routing && ir.routing.kind === "llm") {
587
+ const llm = renderLlmRouter(ir, entryRole);
588
+ prelude = llm.prelude;
589
+ routerBlock = llm.routerBlock;
590
+ runtimeImport = `import { runChatLoop } from "@crewhaus/runtime-core";\n`;
591
+ }
217
592
  return `// Generated by crewhaus. DO NOT EDIT.
218
593
  // Source spec: ${escapeJsonString(ir.name)} (target: crew, ir version: ${ir.version}, file: orchestrator.ts)
219
594
  import { Crew, type RunnableCrew } from "@crewhaus/crew-orchestrator";
220
- ${roleImports}
221
-
595
+ ${runtimeImport}${roleImports}
596
+ ${prelude}
222
597
  export function buildCrew(): RunnableCrew {
223
598
  const builder = Crew()
224
599
  .setName(${escapeJsonString(ir.name)})
@@ -264,12 +639,36 @@ function renderDaemon(ir) {
264
639
  ? `import { BUILTIN_DEFAULT_RULES } from "@crewhaus/permission-engine";\n`
265
640
  : "";
266
641
  const permField = renderPermissionsField(ir);
642
+ const approvalFields = renderApprovalFields(ir, " ");
267
643
  const taxonomyField = renderFailureTaxonomyField(ir);
644
+ // Loop contract 0.4 (Batch A) — the new crew.run knobs.
645
+ const crewCapsFields = renderCrewCapsFields(ir);
646
+ const loopLimitsField = renderLoopLimitsField(ir);
647
+ const budgetField = renderBudgetField(ir);
648
+ const hooksField = renderHooksField(ir);
268
649
  const fabric = memoryFabric(ir);
650
+ const mcp = renderMcpServers(ir);
651
+ // Section 13 (Batch A, G34) — when any role declares sub-agents, inject
652
+ // the spawner once at the run level; the orchestrator stamps it (plus the
653
+ // per-role subAgents map) onto each role's runChatLoop bridge.
654
+ const anySubAgents = ir.roles.some((r) => r.subAgents.length > 0);
655
+ const spawnImport = anySubAgents
656
+ ? `import { spawnSubAgent } from "@crewhaus/sub-agent-spawner";\n`
657
+ : "";
658
+ const spawnField = anySubAgents ? "\n spawnSubAgent," : "";
659
+ // One tool-catalog import line serves both consumers: the MCP wire-once
660
+ // catalog (value) and the memory fabric's collector typing (type).
661
+ const toolCatalogImport = mcp.hasAny && fabric.wired
662
+ ? `import { ToolCatalog, type RegisteredTool } from "@crewhaus/tool-catalog";\n`
663
+ : mcp.hasAny
664
+ ? `import { ToolCatalog } from "@crewhaus/tool-catalog";\n`
665
+ : fabric.wired
666
+ ? `import type { RegisteredTool } from "@crewhaus/tool-catalog";\n`
667
+ : "";
668
+ const mcpImport = mcp.hasAny ? `${mcp.imports.join("\n")}\n` : "";
269
669
  const memImport = fabric.wired
270
670
  ? `import { wireMemory } from "@crewhaus/memory-service";
271
671
  import { createSkillTool } from "@crewhaus/skills-registry";
272
- import type { RegisteredTool } from "@crewhaus/tool-catalog";
273
672
  `
274
673
  : "";
275
674
  const memBoot = fabric.wired
@@ -285,13 +684,23 @@ import type { RegisteredTool } from "@crewhaus/tool-catalog";
285
684
  const __skills = __memWired.options.skills ?? [];
286
685
  if (__skills.length > 0) __memTools.push(createSkillTool(__skills));`
287
686
  : "";
687
+ // Crew-wide extra tools: user-declared MCP tools first (explicit choices
688
+ // win), then the memory fabric's.
689
+ const extraToolsField = mcp.hasAny && fabric.wired
690
+ ? "\n extraTools: [...__mcpTools, ...__memTools],"
691
+ : mcp.hasAny
692
+ ? "\n extraTools: __mcpTools,"
693
+ : fabric.wired
694
+ ? "\n extraTools: __memTools,"
695
+ : "";
288
696
  const memOptsFields = fabric.wired
289
697
  ? `
290
- extraTools: __memTools,
291
698
  ...(__memWired.options.memory !== undefined ? { memory: __memWired.options.memory } : {}),
292
699
  ...(__memWired.options.continuity !== undefined ? { continuity: __memWired.options.continuity } : {}),
293
700
  ...(__skills.length > 0 ? { skills: __skills } : {}),`
294
701
  : "";
702
+ const mcpShutdownOk = mcp.hasAny ? "\n await mcpHost.disconnectAll();" : "";
703
+ const mcpShutdownErr = mcp.hasAny ? "\n await mcpHost.disconnectAll().catch(() => {});" : "";
295
704
  // v0.3.0 Goal 3 — thredz is spec-carried on this shape but not emit-wired
296
705
  // in this release (the one-knob backend flip ships on cli). Surface it,
297
706
  // 0.2.3-style, so nobody wonders why their wiki stayed local.
@@ -302,8 +711,8 @@ import type { RegisteredTool } from "@crewhaus/tool-catalog";
302
711
  // Generated by crewhaus. DO NOT EDIT.
303
712
  // Source spec: ${escapeJsonString(ir.name)} (target: crew, ir version: ${ir.version}, file: daemon.ts)
304
713
  ${thredzWarning}import { formatRunFailure, toFailureReport } from "@crewhaus/errors";
305
- ${permImport}${memImport}import { buildCrew } from "./orchestrator.js";
306
-
714
+ ${APPROVAL_STORE_IMPORT}${permImport}${memImport}${mcpImport}${spawnImport}${toolCatalogImport}import { buildCrew } from "./orchestrator.js";
715
+ ${renderApprovalStoreBoot("undefined")}
307
716
  async function main(): Promise<void> {
308
717
  const crew = buildCrew();
309
718
  const input = await readAllStdin();
@@ -311,8 +720,8 @@ async function main(): Promise<void> {
311
720
  if (trimmed.length === 0) {
312
721
  process.stderr.write("[crew] no input on stdin\\n");
313
722
  process.exit(2);
314
- }${memBoot}
315
- const opts: Parameters<typeof crew.run>[1] = {${permField}${taxonomyField}${memOptsFields}
723
+ }${mcp.bootBlock}${memBoot}
724
+ const opts: Parameters<typeof crew.run>[1] = {${permField}${approvalFields}${taxonomyField}${crewCapsFields}${loopLimitsField}${budgetField}${hooksField}${spawnField}${extraToolsField}${memOptsFields}
316
725
  };
317
726
  let lastFinalOutput = "";
318
727
  try {
@@ -325,9 +734,9 @@ async function main(): Promise<void> {
325
734
  // for a RunFailedError, generic otherwise) and exit with its coded
326
735
  // status instead of the bare "[crew] error:" line + exit 1.
327
736
  const __report = toFailureReport(err);
328
- process.stderr.write(\`\${formatRunFailure(__report, { prefix: "[crew]" })}\\n\`);
737
+ process.stderr.write(\`\${formatRunFailure(__report, { prefix: "[crew]" })}\\n\`);${mcpShutdownErr}
329
738
  process.exit(__report.exitCode);
330
- }
739
+ }${mcpShutdownOk}
331
740
  if (lastFinalOutput.length > 0) {
332
741
  const PREVIEW_LIMIT = 200;
333
742
  const preview =
@@ -355,3 +764,128 @@ main().catch((err) => {
355
764
  });
356
765
  `;
357
766
  }
767
+ // ---------------------------------------------------------------------------
768
+ // File (cluster S, D36/NEW-shape-1): eval-entry.ts — the eval bridge's
769
+ // runtime entry. Emitted ONLY under `emitCrew(ir, { evalEntry: true })`.
770
+ // ---------------------------------------------------------------------------
771
+ /**
772
+ * Render `eval-entry.ts`: one crew run through the compiled orchestrator +
773
+ * roles, assembled with the SAME renderers the daemon uses (so the two run-
774
+ * option sets can never drift), minus the stdin/stdout framing. Each eval
775
+ * sample is one crew run; the `crew_done` finalOutput is the graded output.
776
+ * The runner's per-sample `sessionId`/`sessionRootDir` thread into the crew's
777
+ * own session machinery so the crew transcript lands in the sample artifacts
778
+ * (crew trace events stay on the orchestrator's internal bus in this slice —
779
+ * `crew.run` mints its own RunContext).
780
+ */
781
+ function renderEvalEntry(ir) {
782
+ const hasRules = ir.permissions.rules.length > 0;
783
+ const permImport = hasRules
784
+ ? `import { BUILTIN_DEFAULT_RULES } from "@crewhaus/permission-engine";\n`
785
+ : "";
786
+ const permField = renderPermissionsField(ir);
787
+ const approvalFields = renderApprovalFields(ir, " ");
788
+ const taxonomyField = renderFailureTaxonomyField(ir);
789
+ const crewCapsFields = renderCrewCapsFields(ir);
790
+ const loopLimitsField = renderLoopLimitsField(ir);
791
+ const budgetField = renderBudgetField(ir);
792
+ const hooksField = renderHooksField(ir);
793
+ const fabric = memoryFabric(ir);
794
+ const mcp = renderMcpServers(ir);
795
+ const anySubAgents = ir.roles.some((r) => r.subAgents.length > 0);
796
+ const spawnImport = anySubAgents
797
+ ? `import { spawnSubAgent } from "@crewhaus/sub-agent-spawner";\n`
798
+ : "";
799
+ const spawnField = anySubAgents ? "\n spawnSubAgent," : "";
800
+ const toolCatalogImport = mcp.hasAny && fabric.wired
801
+ ? `import { ToolCatalog, type RegisteredTool } from "@crewhaus/tool-catalog";\n`
802
+ : mcp.hasAny
803
+ ? `import { ToolCatalog } from "@crewhaus/tool-catalog";\n`
804
+ : fabric.wired
805
+ ? `import type { RegisteredTool } from "@crewhaus/tool-catalog";\n`
806
+ : "";
807
+ const mcpImport = mcp.hasAny ? `${mcp.imports.join("\n")}\n` : "";
808
+ const memImport = fabric.wired
809
+ ? `import { wireMemory } from "@crewhaus/memory-service";
810
+ import { createSkillTool } from "@crewhaus/skills-registry";
811
+ `
812
+ : "";
813
+ const memBoot = fabric.wired
814
+ ? `
815
+ // v0.3.0 — the memory fabric, wired ONCE through the stable composition-
816
+ // root call (design §1 principle 1): roles share the spec-scoped stores;
817
+ // the orchestrator threads the tools + seams into every role's turn.
818
+ //
819
+ // Cluster S (D36/NEW-shape-1) — the fabric roots at the caller's per-sample
820
+ // directory when one is supplied, so a bridged eval keeps the Pillar-2
821
+ // isolation invariant: sample N's facts/plan/handoff never leak into sample
822
+ // N+1, and nothing is written into the operator's working tree.
823
+ const __memTools: RegisteredTool[] = [];
824
+ const __memWired = await wireMemory(${fabric.fragmentJson}, {
825
+ catalog: { register: (t: RegisteredTool) => { __memTools.push(t); } },
826
+ cwd: __evalOpts.sessionRootDir ?? process.cwd(),
827
+ });
828
+ const __skills = __memWired.options.skills ?? [];
829
+ if (__skills.length > 0) __memTools.push(createSkillTool(__skills));`
830
+ : "";
831
+ const extraToolsField = mcp.hasAny && fabric.wired
832
+ ? "\n extraTools: [...__mcpTools, ...__memTools],"
833
+ : mcp.hasAny
834
+ ? "\n extraTools: __mcpTools,"
835
+ : fabric.wired
836
+ ? "\n extraTools: __memTools,"
837
+ : "";
838
+ const memOptsFields = fabric.wired
839
+ ? `
840
+ ...(__memWired.options.memory !== undefined ? { memory: __memWired.options.memory } : {}),
841
+ ...(__memWired.options.continuity !== undefined ? { continuity: __memWired.options.continuity } : {}),
842
+ ...(__skills.length > 0 ? { skills: __skills } : {}),`
843
+ : "";
844
+ const runBlock = mcp.hasAny
845
+ ? ` let __final = "";
846
+ try {
847
+ for await (const ev of crew.run(input, opts)) {
848
+ if (ev.kind === "crew_done") __final = ev.finalOutput;
849
+ }
850
+ } finally {
851
+ await mcpHost.disconnectAll().catch(() => {});
852
+ }
853
+ return __final;`
854
+ : ` let __final = "";
855
+ for await (const ev of crew.run(input, opts)) {
856
+ if (ev.kind === "crew_done") __final = ev.finalOutput;
857
+ }
858
+ return __final;`;
859
+ return `// Generated by crewhaus. DO NOT EDIT.
860
+ // Source spec: ${escapeJsonString(ir.name)} (target: crew, ir version: ${ir.version}, file: eval-entry.ts — the compile --with-eval-harness bridge entry)
861
+ ${APPROVAL_STORE_IMPORT}${permImport}${memImport}${mcpImport}${spawnImport}${toolCatalogImport}import { buildCrew } from "./orchestrator.js";
862
+
863
+ type __CrewRunOpts = NonNullable<Parameters<ReturnType<typeof buildCrew>["run"]>[1]>;
864
+
865
+ /**
866
+ * Eval bridge (cluster S, D36/NEW-shape-1) — run ONE crew turn through the
867
+ * compiled orchestrator + role definitions with the SAME run options the
868
+ * daemon assembles, minus the stdin/stdout framing. Each eval sample is one
869
+ * crew run; the crew_done finalOutput is the graded output. \`sessionId\` /
870
+ * \`sessionRootDir\` thread into the crew's own session machinery so the crew
871
+ * transcript lands in the caller's per-sample directory; \`_adapter\` is the
872
+ * scripted-provider test seam every role turn shares.
873
+ */
874
+ export async function runForEval(
875
+ input: string,
876
+ __evalOpts: {
877
+ sessionId?: string;
878
+ sessionRootDir?: string;
879
+ _adapter?: __CrewRunOpts["_adapter"];
880
+ } = {},
881
+ ): Promise<string> {
882
+ const crew = buildCrew();${renderApprovalStoreBoot("__evalOpts.sessionRootDir", " ")}${mcp.bootBlock}${memBoot}
883
+ const opts: __CrewRunOpts = {${permField}${approvalFields}${taxonomyField}${crewCapsFields}${loopLimitsField}${budgetField}${hooksField}${spawnField}${extraToolsField}${memOptsFields}
884
+ ...(__evalOpts.sessionId !== undefined ? { sessionId: __evalOpts.sessionId } : {}),
885
+ ...(__evalOpts.sessionRootDir !== undefined ? { sessionRootDir: __evalOpts.sessionRootDir } : {}),
886
+ ...(__evalOpts._adapter !== undefined ? { _adapter: __evalOpts._adapter } : {}),
887
+ };
888
+ ${runBlock}
889
+ }
890
+ `;
891
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/target-crew",
3
- "version": "0.3.2",
3
+ "version": "0.4.2",
4
4
  "type": "module",
5
5
  "description": "Codegen for the CRW target — emits a daemon + per-role agent files driven by crew-orchestrator (Section 22)",
6
6
  "main": "dist/index.js",
@@ -15,10 +15,10 @@
15
15
  "test": "bun test src"
16
16
  },
17
17
  "dependencies": {
18
- "@crewhaus/errors": "0.3.2",
19
- "@crewhaus/infra-utils": "0.3.2",
20
- "@crewhaus/ir": "0.3.2",
21
- "@crewhaus/memory-service": "0.3.2"
18
+ "@crewhaus/errors": "0.4.2",
19
+ "@crewhaus/infra-utils": "0.4.2",
20
+ "@crewhaus/ir": "0.4.2",
21
+ "@crewhaus/memory-service": "0.4.2"
22
22
  },
23
23
  "license": "Apache-2.0",
24
24
  "author": {