@crewhaus/target-crew 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
@@ -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
@@ -80,6 +80,9 @@ export function emitCrew(ir, opts = {}) {
80
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
@@ -589,6 +639,7 @@ function renderDaemon(ir) {
589
639
  ? `import { BUILTIN_DEFAULT_RULES } from "@crewhaus/permission-engine";\n`
590
640
  : "";
591
641
  const permField = renderPermissionsField(ir);
642
+ const approvalFields = renderApprovalFields(ir, " ");
592
643
  const taxonomyField = renderFailureTaxonomyField(ir);
593
644
  // Loop contract 0.4 (Batch A) — the new crew.run knobs.
594
645
  const crewCapsFields = renderCrewCapsFields(ir);
@@ -660,8 +711,8 @@ import { createSkillTool } from "@crewhaus/skills-registry";
660
711
  // Generated by crewhaus. DO NOT EDIT.
661
712
  // Source spec: ${escapeJsonString(ir.name)} (target: crew, ir version: ${ir.version}, file: daemon.ts)
662
713
  ${thredzWarning}import { formatRunFailure, toFailureReport } from "@crewhaus/errors";
663
- ${permImport}${memImport}${mcpImport}${spawnImport}${toolCatalogImport}import { buildCrew } from "./orchestrator.js";
664
-
714
+ ${APPROVAL_STORE_IMPORT}${permImport}${memImport}${mcpImport}${spawnImport}${toolCatalogImport}import { buildCrew } from "./orchestrator.js";
715
+ ${renderApprovalStoreBoot("undefined")}
665
716
  async function main(): Promise<void> {
666
717
  const crew = buildCrew();
667
718
  const input = await readAllStdin();
@@ -670,7 +721,7 @@ async function main(): Promise<void> {
670
721
  process.stderr.write("[crew] no input on stdin\\n");
671
722
  process.exit(2);
672
723
  }${mcp.bootBlock}${memBoot}
673
- const opts: Parameters<typeof crew.run>[1] = {${permField}${taxonomyField}${crewCapsFields}${loopLimitsField}${budgetField}${hooksField}${spawnField}${extraToolsField}${memOptsFields}
724
+ const opts: Parameters<typeof crew.run>[1] = {${permField}${approvalFields}${taxonomyField}${crewCapsFields}${loopLimitsField}${budgetField}${hooksField}${spawnField}${extraToolsField}${memOptsFields}
674
725
  };
675
726
  let lastFinalOutput = "";
676
727
  try {
@@ -713,3 +764,128 @@ main().catch((err) => {
713
764
  });
714
765
  `;
715
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.4.0",
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.4.0",
19
- "@crewhaus/infra-utils": "0.4.0",
20
- "@crewhaus/ir": "0.4.0",
21
- "@crewhaus/memory-service": "0.4.0"
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": {