@crewhaus/runtime-core 0.1.2 → 0.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/runtime-core",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "description": "Minimal runtime imported by generated harness bundles",
6
6
  "main": "src/index.ts",
@@ -13,47 +13,47 @@
13
13
  },
14
14
  "dependencies": {
15
15
  "@anthropic-ai/sdk": "^0.96.0",
16
- "@crewhaus/abort-controller": "0.1.2",
17
- "@crewhaus/adapter-anthropic": "0.1.2",
18
- "@crewhaus/agent-context-isolation": "0.1.2",
19
- "@crewhaus/boundary-classifier": "0.1.2",
20
- "@crewhaus/circuit-breaker": "0.1.2",
21
- "@crewhaus/compaction-autocompact": "0.1.2",
22
- "@crewhaus/compaction-snip": "0.1.2",
23
- "@crewhaus/cost-tracker": "0.1.2",
24
- "@crewhaus/egress-classifier": "0.1.2",
25
- "@crewhaus/errors": "0.1.2",
26
- "@crewhaus/event-log": "0.1.2",
27
- "@crewhaus/hooks-engine": "0.1.2",
28
- "@crewhaus/model-router": "0.1.2",
29
- "@crewhaus/permission-engine": "0.1.2",
30
- "@crewhaus/prompt-cache-manager": "0.1.2",
31
- "@crewhaus/prompt-injection-detector": "0.1.2",
32
- "@crewhaus/rate-limiter": "0.1.2",
33
- "@crewhaus/recovery-engine": "0.1.2",
34
- "@crewhaus/run-context": "0.1.2",
35
- "@crewhaus/session-store": "0.1.2",
36
- "@crewhaus/skills-registry": "0.1.2",
37
- "@crewhaus/slash-commands": "0.1.2",
38
- "@crewhaus/state-store": "0.1.2",
39
- "@crewhaus/tenancy": "0.1.2",
40
- "@crewhaus/token-budget": "0.1.2",
41
- "@crewhaus/tool-catalog": "0.1.2",
42
- "@crewhaus/tool-executor": "0.1.2",
43
- "@crewhaus/tool-loop-detection": "0.1.2",
44
- "@crewhaus/tool-orchestrator": "0.1.2",
45
- "@crewhaus/tool-result-store": "0.1.2",
46
- "@crewhaus/streaming-tool-executor": "0.1.2",
47
- "@crewhaus/turn-state-machine": "0.1.2",
48
- "@crewhaus/trace-event-bus": "0.1.2",
49
- "@crewhaus/structured-event-printer": "0.1.2",
50
- "@crewhaus/metrics-collector": "0.1.2",
51
- "@crewhaus/otel-exporter": "0.1.2",
16
+ "@crewhaus/abort-controller": "0.1.4",
17
+ "@crewhaus/adapter-anthropic": "0.1.4",
18
+ "@crewhaus/agent-context-isolation": "0.1.4",
19
+ "@crewhaus/boundary-classifier": "0.1.4",
20
+ "@crewhaus/circuit-breaker": "0.1.4",
21
+ "@crewhaus/compaction-autocompact": "0.1.4",
22
+ "@crewhaus/compaction-snip": "0.1.4",
23
+ "@crewhaus/cost-tracker": "0.1.4",
24
+ "@crewhaus/egress-classifier": "0.1.4",
25
+ "@crewhaus/errors": "0.1.4",
26
+ "@crewhaus/event-log": "0.1.4",
27
+ "@crewhaus/hooks-engine": "0.1.4",
28
+ "@crewhaus/model-router": "0.1.4",
29
+ "@crewhaus/permission-engine": "0.1.4",
30
+ "@crewhaus/prompt-cache-manager": "0.1.4",
31
+ "@crewhaus/prompt-injection-detector": "0.1.4",
32
+ "@crewhaus/rate-limiter": "0.1.4",
33
+ "@crewhaus/recovery-engine": "0.1.4",
34
+ "@crewhaus/run-context": "0.1.4",
35
+ "@crewhaus/session-store": "0.1.4",
36
+ "@crewhaus/skills-registry": "0.1.4",
37
+ "@crewhaus/slash-commands": "0.1.4",
38
+ "@crewhaus/state-store": "0.1.4",
39
+ "@crewhaus/tenancy": "0.1.4",
40
+ "@crewhaus/token-budget": "0.1.4",
41
+ "@crewhaus/tool-catalog": "0.1.4",
42
+ "@crewhaus/tool-executor": "0.1.4",
43
+ "@crewhaus/tool-loop-detection": "0.1.4",
44
+ "@crewhaus/tool-orchestrator": "0.1.4",
45
+ "@crewhaus/tool-result-store": "0.1.4",
46
+ "@crewhaus/streaming-tool-executor": "0.1.4",
47
+ "@crewhaus/turn-state-machine": "0.1.4",
48
+ "@crewhaus/trace-event-bus": "0.1.4",
49
+ "@crewhaus/structured-event-printer": "0.1.4",
50
+ "@crewhaus/metrics-collector": "0.1.4",
51
+ "@crewhaus/otel-exporter": "0.1.4",
52
52
  "zod-to-json-schema": "^3.23.5"
53
53
  },
54
54
  "devDependencies": {
55
- "@crewhaus/logging": "0.1.2",
56
- "@crewhaus/tool-builder": "0.1.2",
55
+ "@crewhaus/logging": "0.1.4",
56
+ "@crewhaus/tool-builder": "0.1.4",
57
57
  "zod": "^3.23.8"
58
58
  },
59
59
  "license": "Apache-2.0",
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Section 27 — cost_accrual persistence into the session JSONL.
3
+ *
4
+ * When CREWHAUS_COST_TRACKING is on, the runtime mirrors each per-call
5
+ * cost_accrual the trace bus carries into the session event-log so
6
+ * `crewhaus cost-summary --session <id>` can sum spend after the run. These
7
+ * tests drive the real `runChatLoop` (stub adapter) and assert the JSONL
8
+ * envelope shape and the env-gating.
9
+ */
10
+ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
11
+ import { mkdtempSync, readFileSync, readdirSync, rmSync } from "node:fs";
12
+ import { tmpdir } from "node:os";
13
+ import { join } from "node:path";
14
+ import type { ProviderAdapter } from "@crewhaus/adapter-anthropic";
15
+ import { createRunContext } from "@crewhaus/run-context";
16
+ import type { ProviderId } from "@crewhaus/trace-event-bus";
17
+ import { runChatLoop } from "./index";
18
+
19
+ /** Single text-only turn: 100 input tokens, 10 output tokens. */
20
+ function makeTextAdapter(providerId: ProviderId): ProviderAdapter {
21
+ return {
22
+ providerId,
23
+ features: {
24
+ caching: "explicit",
25
+ tool_use: true,
26
+ vision: true,
27
+ thinking: false,
28
+ web_search: false,
29
+ },
30
+ estimateTokens: () => 0,
31
+ stream: () =>
32
+ (async function* () {
33
+ yield { kind: "message_start", usage: { input: 100, output: 0 } } as const;
34
+ yield {
35
+ kind: "content_block_start",
36
+ index: 0,
37
+ block: { type: "text", text: "" },
38
+ } as const;
39
+ yield {
40
+ kind: "content_block_delta",
41
+ index: 0,
42
+ delta: { type: "text_delta", text: "ok" },
43
+ } as const;
44
+ yield { kind: "content_block_stop", index: 0 } as const;
45
+ yield {
46
+ kind: "message_delta",
47
+ stopReason: "end_turn",
48
+ usage: { input: 100, output: 10 },
49
+ } as const;
50
+ yield { kind: "message_stop" } as const;
51
+ })(),
52
+ };
53
+ }
54
+
55
+ let sessionRoot: string;
56
+ const savedTracking = process.env["CREWHAUS_COST_TRACKING"];
57
+
58
+ beforeEach(() => {
59
+ sessionRoot = mkdtempSync(join(tmpdir(), "crewhaus-cost-persist-"));
60
+ });
61
+ afterEach(() => {
62
+ rmSync(sessionRoot, { recursive: true, force: true });
63
+ process.env["CREWHAUS_COST_TRACKING"] = savedTracking;
64
+ });
65
+
66
+ async function runOnce(): Promise<void> {
67
+ await runChatLoop({
68
+ model: "claude-sonnet-4-6",
69
+ instructions: "test",
70
+ _adapter: makeTextAdapter("anthropic"),
71
+ runContext: createRunContext(),
72
+ singleTurn: true,
73
+ seedMessages: [{ role: "user", content: "hello" }],
74
+ sessionRootDir: sessionRoot,
75
+ });
76
+ }
77
+
78
+ type AccrualLine = {
79
+ kind: string;
80
+ payload?: { provider?: string; modelId?: string; costUsdMicros?: number };
81
+ };
82
+
83
+ function readCostAccruals(): AccrualLine[] {
84
+ const accruals: AccrualLine[] = [];
85
+ for (const file of readdirSync(sessionRoot).filter((f) => f.endsWith(".jsonl"))) {
86
+ for (const line of readFileSync(join(sessionRoot, file), "utf-8").split("\n")) {
87
+ if (line === "") continue;
88
+ const ev = JSON.parse(line) as AccrualLine;
89
+ if (ev.kind === "cost_accrual") accruals.push(ev);
90
+ }
91
+ }
92
+ return accruals;
93
+ }
94
+
95
+ describe("cost_accrual persistence into the session JSONL (Section 27)", () => {
96
+ test("persists a per-call cost_accrual (envelope shape) when CREWHAUS_COST_TRACKING=1", async () => {
97
+ process.env["CREWHAUS_COST_TRACKING"] = "1";
98
+ await runOnce();
99
+ const accruals = readCostAccruals();
100
+ expect(accruals.length).toBe(1);
101
+ // event-log wraps the event; cost fields live under `payload`.
102
+ expect(accruals[0]?.payload?.provider).toBe("anthropic");
103
+ expect(accruals[0]?.payload?.modelId).toBe("claude-sonnet-4-6");
104
+ // claude-sonnet-4 row: 100 in × $3/M + 10 out × $15/M = 450 micros.
105
+ expect(accruals[0]?.payload?.costUsdMicros).toBe(450);
106
+ });
107
+
108
+ test("writes no cost_accrual line when cost tracking is disabled", async () => {
109
+ process.env["CREWHAUS_COST_TRACKING"] = undefined;
110
+ await runOnce();
111
+ expect(readCostAccruals().length).toBe(0);
112
+ });
113
+ });
package/src/index.ts CHANGED
@@ -64,7 +64,7 @@ import { executeTool } from "@crewhaus/tool-executor";
64
64
  import { type LoopDetection, detectLoop } from "@crewhaus/tool-loop-detection";
65
65
  import { partitionToolCalls } from "@crewhaus/tool-orchestrator";
66
66
  import { storeAndPreview } from "@crewhaus/tool-result-store";
67
- import { TraceEventBus } from "@crewhaus/trace-event-bus";
67
+ import { type CostAccrualEvent, TraceEventBus, type Unsubscribe } from "@crewhaus/trace-event-bus";
68
68
  import {
69
69
  type ToolUseBlock as TsmToolUseBlock,
70
70
  type TurnState,
@@ -745,6 +745,42 @@ export async function runChatLoop(opts: RunChatLoopOptions): Promise<string> {
745
745
 
746
746
  const eventLog: EventLog = await openEventLog(sessionId, { rootDir: sessionRootDir });
747
747
 
748
+ // Section 27 — mirror per-call cost_accrual events from the trace bus into
749
+ // the session JSONL so `crewhaus cost-summary --session <id>` can sum spend
750
+ // after the run. Gated on the same switch that attaches the cost-tracker
751
+ // (CREWHAUS_COST_TRACKING): no tracker → no accruals to persist, and the
752
+ // message-only transcript stays unchanged by default. Per-call accruals only
753
+ // — skip the FR-003 terminal `summary` aggregate so a run total never
754
+ // double-counts. `append()` writes synchronously, and a persist failure must
755
+ // never abort a turn, so the result is logged rather than thrown.
756
+ let costPersistUnsubscribe: Unsubscribe | undefined;
757
+ if (subscribers.costTracker !== undefined) {
758
+ costPersistUnsubscribe = bus.subscribe((event): void => {
759
+ if (event.kind !== "cost_accrual") return;
760
+ const ev = event as CostAccrualEvent;
761
+ if (ev.summary === true) return;
762
+ void eventLog
763
+ .append({
764
+ kind: "cost_accrual",
765
+ payload: {
766
+ provider: ev.provider,
767
+ modelId: ev.modelId,
768
+ ...(ev.specModel !== undefined ? { specModel: ev.specModel } : {}),
769
+ inputTokens: ev.inputTokens,
770
+ outputTokens: ev.outputTokens,
771
+ cachedReadTokens: ev.cachedReadTokens,
772
+ costUsdMicros: ev.costUsdMicros,
773
+ ...(ev.tenantId !== undefined ? { tenantId: ev.tenantId } : {}),
774
+ },
775
+ })
776
+ .catch((err) => {
777
+ runContext.logger.error("cost_accrual.persist_failed", {
778
+ message: err instanceof Error ? err.message : String(err),
779
+ });
780
+ });
781
+ });
782
+ }
783
+
748
784
  // Per-run state container — coordination surface for hooks/skills/tools
749
785
  // landing in Section 11+. Section 10 only ships the plumbing; the
750
786
  // underscore prefix marks the intentional non-consumer.
@@ -1999,6 +2035,7 @@ export async function runChatLoop(opts: RunChatLoopOptions): Promise<string> {
1999
2035
  });
2000
2036
  await subscribers.flushAll();
2001
2037
  await subscribers.shutdownAll();
2038
+ costPersistUnsubscribe?.();
2002
2039
  await sessionStore
2003
2040
  .update(sessionId, { lastTurnIndex: runContext.turnNumber })
2004
2041
  .catch((err) => {
@@ -2212,6 +2249,7 @@ export async function runChatLoop(opts: RunChatLoopOptions): Promise<string> {
2212
2249
  });
2213
2250
  await subscribers.flushAll();
2214
2251
  await subscribers.shutdownAll();
2252
+ costPersistUnsubscribe?.();
2215
2253
  if (shouldInstallSigint) {
2216
2254
  process.removeListener("SIGINT", sigintHandler);
2217
2255
  }