@crewhaus/target-crew 0.4.2 → 0.5.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.
Files changed (2) hide show
  1. package/dist/index.js +176 -17
  2. package/package.json +6 -5
package/dist/index.js CHANGED
@@ -17,6 +17,7 @@
17
17
  * future shell pipeline consumer) can parse them deterministically.
18
18
  */
19
19
  import { CrewhausError } from "@crewhaus/errors";
20
+ import { renderControlDrain, renderControlImports, renderControlPlaneBoot, renderControlStart, } from "@crewhaus/gateway-protocol/control-codegen";
20
21
  import { escapeJsonString } from "@crewhaus/infra-utils";
21
22
  import { renderBundleReadme, } from "@crewhaus/ir";
22
23
  import { memoryFragmentFromIr } from "@crewhaus/memory-service";
@@ -305,8 +306,23 @@ function renderHooksField(ir) {
305
306
  * daemon's stdout is the JSON-per-line event stream and must stay
306
307
  * machine-parseable.
307
308
  */
309
+ /** The `mcp_servers` keys that carry a synthesized Thredz server for this
310
+ * crew: the crew-wide `"thredz"` plus one `"thredz-<slug>"` per role that
311
+ * owns its own key. They are spawned on the shared host but deliberately
312
+ * kept OUT of the namespaced `registerMcpServer` pass — their tools are
313
+ * registered as BARE names into per-role catalogs by `connectThredz`, which
314
+ * is what makes `wiki_write` mean "my wiki" to each role. Namespacing them
315
+ * too would double-register the whole vocabulary as `thredz__wiki_write`. */
316
+ function thredzServerNames(ir) {
317
+ const names = new Set();
318
+ for (const role of ir.roles)
319
+ if (role.thredzServer !== undefined)
320
+ names.add(role.thredzServer);
321
+ return names;
322
+ }
308
323
  function renderMcpServers(ir) {
309
- const entries = Object.entries(ir.mcp_servers);
324
+ const thredzNames = thredzServerNames(ir);
325
+ const entries = Object.entries(ir.mcp_servers).filter(([name]) => !thredzNames.has(name));
310
326
  if (entries.length === 0) {
311
327
  return { imports: [], bootBlock: "", hasAny: false };
312
328
  }
@@ -621,10 +637,20 @@ function memoryFabric(ir) {
621
637
  const memoryOn = ir.memory !== undefined && ir.memory.enabled !== false;
622
638
  const continuityOn = ir.continuity !== undefined;
623
639
  const wired = memoryOn || continuityOn;
640
+ // 0.5.0 — AMENDS the crew-wide scope stated above: when any role carries its
641
+ // own Thredz config, the WIKI stops being crew-wide. Each role gets its own,
642
+ // scoped to its own space, and the crew-wide fabric must not also register
643
+ // the ten LOCAL wiki twins — a role's tool array would then carry both, and
644
+ // duplicate names in the array advertised to the provider are a 400.
645
+ // Facts, continuity, plan and skills stay crew-wide and local.
646
+ const perRoleWiki = ir.roles.some((r) => r.thredz !== undefined);
647
+ const crewMemory = memoryOn && perRoleWiki && ir.memory !== undefined
648
+ ? (({ wiki: _perRole, ...rest }) => rest)(ir.memory)
649
+ : ir.memory;
624
650
  const fragmentJson = wired
625
651
  ? JSON.stringify(memoryFragmentFromIr({
626
652
  name: ir.name,
627
- ...(memoryOn ? { memory: ir.memory } : {}),
653
+ ...(memoryOn ? { memory: crewMemory } : {}),
628
654
  ...(continuityOn ? { continuity: ir.continuity } : {}),
629
655
  // v0.3.0 Goal 2 (PR 17) — learning rides the fragment: wireMemory
630
656
  // renders the learning-loop skill + gates in /study /reflect.
@@ -633,6 +659,100 @@ function memoryFabric(ir) {
633
659
  : "";
634
660
  return { wired, fragmentJson };
635
661
  }
662
+ /**
663
+ * 0.5.0 — the per-role Thredz boot. One `addServer` + one `ToolCatalog` +
664
+ * one `connectThredz` + one `wireWiki` per role that carries a config.
665
+ *
666
+ * The catalog is per role ON PURPOSE: `registerMcpToolAliases` throws on a
667
+ * bare-name collision within the catalog it is handed, so two roles could not
668
+ * both own `wiki_write` in one catalog. A catalog is a boot-time composition
669
+ * device — one Map plus N registrations — and `runChatLoop` never sees one, so
670
+ * this costs essentially nothing and lets every role keep the BARE vocabulary
671
+ * that five prompt sites and four policy surfaces match verbatim.
672
+ */
673
+ function renderRoleThredz(ir) {
674
+ const roles = ir.roles.filter((r) => r.thredz !== undefined && r.thredzServer !== undefined);
675
+ if (roles.length === 0) {
676
+ return {
677
+ imports: [],
678
+ bootBlock: "",
679
+ roleExtraToolsField: "",
680
+ roleMemoryField: "",
681
+ hasAny: false,
682
+ };
683
+ }
684
+ // Distinct servers: roles sharing the crew default share one npx process.
685
+ const servers = new Map();
686
+ for (const r of roles)
687
+ if (!servers.has(r.thredzServer))
688
+ servers.set(r.thredzServer, r);
689
+ const ident = (role) => role.replace(/[^A-Za-z0-9]/g, "_");
690
+ const addLines = [...servers].map(([name, r]) => ` try {
691
+ mcpHost.addServer(${escapeJsonString(name)}, resolveMcpServerConfig(${JSON.stringify(synthesizedThredzServerFor(ir, name))}, { name: ${escapeJsonString(name)} }));
692
+ } catch (__err) {
693
+ const __report = toFailureReport(__err);
694
+ process.stderr.write(\`\${formatRunFailure(__report, { prefix: "[crew]" })}\\n\`);
695
+ process.exit(__report.exitCode);
696
+ }`);
697
+ const connectLines = [...servers].map(([name, r]) => {
698
+ const id = ident(name);
699
+ const agent = r.thredz?.agentName;
700
+ return ` const __thredzCat_${id} = new ToolCatalog();
701
+ const __thredz_${id} = await connectThredz(mcpHost, __thredzCat_${id}, {
702
+ serverName: ${escapeJsonString(name)},${agent !== undefined ? `\n agentName: ${escapeJsonString(agent)},` : ""}
703
+ log: (line) => process.stderr.write(line),
704
+ });`;
705
+ });
706
+ // Per-role wiki seam. With a live connection wireWiki returns
707
+ // { store: null, tools: [] } plus a client-bound recall; on the degrade it
708
+ // returns the ten LOCAL tools — so exactly one of the two arrays is
709
+ // non-empty per role, and both are safe to concatenate.
710
+ const wikiLines = roles.map((r) => {
711
+ const id = ident(r.name);
712
+ const server = ident(r.thredzServer);
713
+ return ` const __wiki_${id} = wireWiki(${JSON.stringify({
714
+ specName: ir.name,
715
+ ...(ir.memory !== undefined ? { memory: ir.memory } : {}),
716
+ thredz: r.thredz,
717
+ })}, { catalog: { register: () => {} }, cwd: process.cwd(), thredz: __thredz_${server} });`;
718
+ });
719
+ const recallK = ir.memory?.wiki?.recallK ?? 5;
720
+ const roleExtraToolsField = `\n roleExtraTools: {${roles
721
+ .map((r) => `\n ${escapeJsonString(r.name)}: [...__thredzCat_${ident(r.thredzServer)}.list(), ...(__wiki_${ident(r.name)}?.tools ?? [])],`)
722
+ .join("")}\n },`;
723
+ const roleMemoryField = `\n roleMemory: {${roles
724
+ .map((r) => `\n ${escapeJsonString(r.name)}: foldWikiRecall(__memWired?.options.memory, __wiki_${ident(r.name)}, ${recallK}),`)
725
+ .join("")}\n },`;
726
+ // The Thredz servers ride the SHARED McpHost. When the crew declares no
727
+ // user MCP servers, `renderMcpServers` emits nothing — so this block owns
728
+ // the host declaration and its imports instead.
729
+ const ownsHost = Object.keys(ir.mcp_servers).every((n) => servers.has(n));
730
+ return {
731
+ imports: [
732
+ `import { connectThredz, wireWiki, foldWikiRecall } from "@crewhaus/memory-service";`,
733
+ ...(ownsHost
734
+ ? [`import { McpHost, resolveMcpServerConfig } from "@crewhaus/mcp-host";`]
735
+ : []),
736
+ ],
737
+ bootBlock: `
738
+ // 0.5.0 — per-role Thredz. One key owns one private wiki space and
739
+ // thredz-mcp reads one key per process, so per-role private memory is one
740
+ // spawned server per role. Each role's vocabulary lands in its OWN catalog.${ownsHost ? "\n const mcpHost = new McpHost();" : ""}
741
+ ${addLines.join("\n")}
742
+ ${connectLines.join("\n")}
743
+ ${wikiLines.join("\n")}`,
744
+ roleExtraToolsField,
745
+ roleMemoryField,
746
+ hasAny: true,
747
+ };
748
+ }
749
+ /** The synthesized server config the compiler put in `ir.mcp_servers`. */
750
+ function synthesizedThredzServerFor(ir, name) {
751
+ const cfg = ir.mcp_servers[name];
752
+ if (cfg === undefined)
753
+ throw new Error(`crew emitter: no mcp_servers entry for ${name}`);
754
+ return cfg;
755
+ }
636
756
  function renderDaemon(ir) {
637
757
  const hasRules = ir.permissions.rules.length > 0;
638
758
  const permImport = hasRules
@@ -648,6 +768,8 @@ function renderDaemon(ir) {
648
768
  const hooksField = renderHooksField(ir);
649
769
  const fabric = memoryFabric(ir);
650
770
  const mcp = renderMcpServers(ir);
771
+ // 0.5.0 — per-role Thredz servers, catalogs and wiki seams.
772
+ const roleThredz = renderRoleThredz(ir);
651
773
  // Section 13 (Batch A, G34) — when any role declares sub-agents, inject
652
774
  // the spawner once at the run level; the orchestrator stamps it (plus the
653
775
  // per-role subAgents map) onto each role's runChatLoop bridge.
@@ -665,7 +787,7 @@ function renderDaemon(ir) {
665
787
  : fabric.wired
666
788
  ? `import type { RegisteredTool } from "@crewhaus/tool-catalog";\n`
667
789
  : "";
668
- const mcpImport = mcp.hasAny ? `${mcp.imports.join("\n")}\n` : "";
790
+ const mcpImport = `${[...(mcp.hasAny ? mcp.imports : []), ...roleThredz.imports].join("\n")}${mcp.hasAny || roleThredz.hasAny ? "\n" : ""}`;
669
791
  const memImport = fabric.wired
670
792
  ? `import { wireMemory } from "@crewhaus/memory-service";
671
793
  import { createSkillTool } from "@crewhaus/skills-registry";
@@ -699,35 +821,69 @@ import { createSkillTool } from "@crewhaus/skills-registry";
699
821
  ...(__memWired.options.continuity !== undefined ? { continuity: __memWired.options.continuity } : {}),
700
822
  ...(__skills.length > 0 ? { skills: __skills } : {}),`
701
823
  : "";
702
- const mcpShutdownOk = mcp.hasAny ? "\n await mcpHost.disconnectAll();" : "";
703
- const mcpShutdownErr = mcp.hasAny ? "\n await mcpHost.disconnectAll().catch(() => {});" : "";
704
- // v0.3.0 Goal 3 — thredz is spec-carried on this shape but not emit-wired
705
- // in this release (the one-knob backend flip ships on cli). Surface it,
706
- // 0.2.3-style, so nobody wonders why their wiki stayed local.
707
- const thredzWarning = ir.thredz !== undefined
708
- ? "// note: thredz configured but ignored on crew in 0.3.0 — the Thredz backend flip is wired on the cli shape (design §4)\n"
709
- : "";
824
+ // The host is shared by user MCP servers and the per-role Thredz servers —
825
+ // disconnect it if EITHER put something on it, or N npx children outlive the
826
+ // run.
827
+ const hostLive = mcp.hasAny || roleThredz.hasAny;
828
+ const mcpShutdownOk = hostLive ? "\n await mcpHost.disconnectAll();" : "";
829
+ const mcpShutdownErr = hostLive ? "\n await mcpHost.disconnectAll().catch(() => {});" : "";
830
+ // 0.5.0 — thredz is EMIT-WIRED on crew. A crew whose roles each carry their
831
+ // own key gets one server, one space and one vocabulary per role; roles that
832
+ // share the crew-wide block share one brain. Nothing is ignored any more.
833
+ const thredzWarning = "";
834
+ // crewhaus.control.v1, from the shared gateway-protocol renderers. The crew
835
+ // daemon is a ONE-SHOT: it consumes stdin, runs the crew and exits, so it
836
+ // arms no pokeable lane — `/control/v1/wake` answers 404 lane_not_armed.
837
+ // What control.v1 gives an operator here is a uniform way to observe the run
838
+ // (`/status`: turns + pid + startedAt) and to ask it to stop taking new work
839
+ // and finish (`/drain`), on exactly the same surface the long-running shapes
840
+ // serve.
841
+ const controlPlaneBoot = renderControlPlaneBoot({
842
+ name: ir.name,
843
+ target: "crew",
844
+ indent: " ",
845
+ auditLogExpr: "__controlAudit",
846
+ });
847
+ const controlDrain = renderControlDrain({
848
+ indent: " ",
849
+ body: `process.stderr.write("[crew] draining — finishing the in-flight run\\n");
850
+ while (__crewRunning) await new Promise((__r) => setTimeout(__r, 100));`,
851
+ });
852
+ const controlStart = renderControlStart({ indent: " " });
710
853
  return `#!/usr/bin/env bun
711
854
  // Generated by crewhaus. DO NOT EDIT.
712
855
  // Source spec: ${escapeJsonString(ir.name)} (target: crew, ir version: ${ir.version}, file: daemon.ts)
713
- ${thredzWarning}import { formatRunFailure, toFailureReport } from "@crewhaus/errors";
714
- ${APPROVAL_STORE_IMPORT}${permImport}${memImport}${mcpImport}${spawnImport}${toolCatalogImport}import { buildCrew } from "./orchestrator.js";
856
+ ${thredzWarning}import { openAuditLog } from "@crewhaus/audit-log";
857
+ import { formatRunFailure, toFailureReport } from "@crewhaus/errors";
858
+ ${renderControlImports()}${APPROVAL_STORE_IMPORT}${permImport}${memImport}${mcpImport}${spawnImport}${toolCatalogImport}import { buildCrew } from "./orchestrator.js";
715
859
  ${renderApprovalStoreBoot("undefined")}
860
+ let __crewRunning = false;
861
+
716
862
  async function main(): Promise<void> {
717
- const crew = buildCrew();
863
+ // control.v1 — the harness-wide hash-chained audit log every control call
864
+ // appends a \`gateway_request\` record to. CREWHAUS_SECURITY_AUDIT=0 opts out.
865
+ const __controlAudit =
866
+ process.env["CREWHAUS_SECURITY_AUDIT"] === "0"
867
+ ? undefined
868
+ : await openAuditLog({ rootDir: \`\${process.cwd()}/.crewhaus/audit\` });
869
+ ${controlPlaneBoot}${controlDrain}${controlStart} const crew = buildCrew();
718
870
  const input = await readAllStdin();
719
871
  const trimmed = input.trim();
720
872
  if (trimmed.length === 0) {
721
873
  process.stderr.write("[crew] no input on stdin\\n");
722
874
  process.exit(2);
723
- }${mcp.bootBlock}${memBoot}
724
- const opts: Parameters<typeof crew.run>[1] = {${permField}${approvalFields}${taxonomyField}${crewCapsFields}${loopLimitsField}${budgetField}${hooksField}${spawnField}${extraToolsField}${memOptsFields}
875
+ }${mcp.bootBlock}${roleThredz.bootBlock}${memBoot}
876
+ const opts: Parameters<typeof crew.run>[1] = {${permField}${approvalFields}${taxonomyField}${crewCapsFields}${loopLimitsField}${budgetField}${hooksField}${spawnField}${extraToolsField}${roleThredz.roleExtraToolsField}${roleThredz.roleMemoryField}${memOptsFields}
725
877
  };
726
878
  let lastFinalOutput = "";
879
+ __crewRunning = true;
727
880
  try {
728
881
  for await (const ev of crew.run(trimmed, opts)) {
729
882
  process.stdout.write(\`\${JSON.stringify(ev)}\\n\`);
730
- if (ev.kind === "crew_done") lastFinalOutput = ev.finalOutput;
883
+ if (ev.kind === "crew_done") {
884
+ lastFinalOutput = ev.finalOutput;
885
+ __control.counters.turns++;
886
+ }
731
887
  }
732
888
  } catch (err) {
733
889
  // v0.3.0 Goal 6 — render the one structured failure report (classified
@@ -735,8 +891,11 @@ async function main(): Promise<void> {
735
891
  // status instead of the bare "[crew] error:" line + exit 1.
736
892
  const __report = toFailureReport(err);
737
893
  process.stderr.write(\`\${formatRunFailure(__report, { prefix: "[crew]" })}\\n\`);${mcpShutdownErr}
894
+ __crewRunning = false;
738
895
  process.exit(__report.exitCode);
739
896
  }${mcpShutdownOk}
897
+ __crewRunning = false;
898
+ await __control.stop();
740
899
  if (lastFinalOutput.length > 0) {
741
900
  const PREVIEW_LIMIT = 200;
742
901
  const preview =
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/target-crew",
3
- "version": "0.4.2",
3
+ "version": "0.5.1",
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,11 @@
15
15
  "test": "bun test src"
16
16
  },
17
17
  "dependencies": {
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"
18
+ "@crewhaus/errors": "0.5.1",
19
+ "@crewhaus/gateway-protocol": "0.5.1",
20
+ "@crewhaus/infra-utils": "0.5.1",
21
+ "@crewhaus/ir": "0.5.1",
22
+ "@crewhaus/memory-service": "0.5.1"
22
23
  },
23
24
  "license": "Apache-2.0",
24
25
  "author": {