@crewhaus/runtime-core 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.
@@ -212,3 +212,23 @@ export type AttachedMcpStatsPersistence = {
212
212
  * persistence hiccup must not abort a turn.
213
213
  */
214
214
  export declare function attachMcpStatsPersistence(bus: TraceEventBus, eventLog: EventLog, runContext: RunContext, env?: NodeJS.ProcessEnv): AttachedMcpStatsPersistence | undefined;
215
+ export declare const WATCHME_MIRRORED_KINDS: ReadonlySet<string>;
216
+ export type AttachedWatchmeCapture = {
217
+ unsubscribe(): void;
218
+ };
219
+ /**
220
+ * "Watch me" (§6.1) — the live capture tap behind `crewhaus watchme`. Appends
221
+ * every BUS-ONLY TraceEvent (envelope included — `formatJsonLine` shape) as
222
+ * one JSON line to `<sessionsDir>/<sessionId>.events.jsonl`, the sibling
223
+ * `sessions export` already reads. Skipped: the ephemeral stream kinds and
224
+ * every durably mirrored kind ({@link WATCHME_MIRRORED_KINDS}), so content
225
+ * stays solely in the session `.jsonl` while the sibling carries exact
226
+ * per-turn model attribution via the envelope `turnNumber` on
227
+ * `model_response`.
228
+ *
229
+ * Gated on CREWHAUS_WATCHME=1|true (stamped by `crewhaus run` /
230
+ * the compiled-bundle preambles); returns undefined otherwise. Appends are
231
+ * synchronous (`appendFileSync`, mode 0600, one line < PIPE_BUF ⇒ atomic) and
232
+ * every failure is swallowed — capture can never crash a run.
233
+ */
234
+ export declare function attachWatchmeCapture(bus: TraceEventBus, sessionsDir: string, sessionId: string, env?: NodeJS.ProcessEnv): AttachedWatchmeCapture | undefined;
@@ -1,7 +1,9 @@
1
+ import { appendFileSync } from "node:fs";
2
+ import { join } from "node:path";
1
3
  import { createCostTracker, formatUsdMicros } from "@crewhaus/cost-tracker";
2
4
  import { attachIfEnvSet as attachMetricsIfEnvSet, } from "@crewhaus/metrics-collector";
3
5
  import { attachIfEnvSet as attachOtelIfEnvSet, } from "@crewhaus/otel-exporter";
4
- import { attachIfEnvSet as attachPrinterIfEnvSet, } from "@crewhaus/structured-event-printer";
6
+ import { attachIfEnvSet as attachPrinterIfEnvSet, formatJsonLine, } from "@crewhaus/structured-event-printer";
5
7
  import { SessionMetricsAccumulator, appendMetricsSnapshot, deriveThresholds, detectBreaches, readMetricsHistory, } from "./alert-watchdog";
6
8
  import { attachSloMonitor, } from "./slo-monitor";
7
9
  /**
@@ -531,3 +533,97 @@ export function attachMcpStatsPersistence(bus, eventLog, runContext, env = proce
531
533
  });
532
534
  return { unsubscribe };
533
535
  }
536
+ // -------- "watch me" live capture tap (design/watch-me.md §6.1) --------
537
+ /**
538
+ * Reconciliation with the SHIPPED G26 `observability:` block (watch-me §4.5 —
539
+ * decided, superseding all "when G26 lands" language): `watchme:` is a SIBLING
540
+ * spec block, not an `observability:` sub-key. `observability.trace.level`
541
+ * controls the ring buffer + printers ONLY; the bus always exists, so this
542
+ * capture subscriber attaches independently of that knob, gated solely on
543
+ * CREWHAUS_WATCHME. The env itself is stamped at the same junctions the G26
544
+ * stamps already use (`applyRunObservabilityEnv` on the interpreter path, the
545
+ * target bundles' boot-stamp emitters), so precedence semantics stay in one
546
+ * place per path.
547
+ */
548
+ /** Stream kinds published per token/chunk — pure progress signal, never captured. */
549
+ const WATCHME_EPHEMERAL_KINDS = new Set([
550
+ "model_stream_token",
551
+ "tool_stream_chunk",
552
+ ]);
553
+ /**
554
+ * The session `.jsonl` mirror vocabulary — every kind the event log persists
555
+ * durably: the transcript kinds the runtime writes itself plus the mirrored
556
+ * kinds emitted by the cost/advisor/mcp-stats subscribers in this file. The
557
+ * capture tap skips these, which keeps the `.events.jsonl` sibling
558
+ * metadata-grade BY CONSTRUCTION (all content-carrying kinds are durable ones)
559
+ * and makes the two files' kind sets disjoint — a pinned invariant. Typed as
560
+ * an exhaustive Record over event-log's `EventKind` so a new durable kind
561
+ * cannot land without this set learning about it.
562
+ */
563
+ const WATCHME_MIRRORED_KIND_RECORD = {
564
+ user_message: true,
565
+ assistant_message: true,
566
+ tool_use: true,
567
+ tool_result: true,
568
+ error: true,
569
+ run_failed: true,
570
+ context_evicted: true,
571
+ compaction: true,
572
+ sub_agent_start: true,
573
+ sub_agent_end: true,
574
+ role_start: true,
575
+ role_end: true,
576
+ handoff: true,
577
+ a2a_message: true,
578
+ a2a_turn_start: true,
579
+ a2a_turn_end: true,
580
+ crew_done: true,
581
+ cost_accrual: true,
582
+ user_feedback: true,
583
+ recovery: true,
584
+ tool_stats: true,
585
+ permission: true,
586
+ model_meta: true,
587
+ mcp_stats: true,
588
+ model_route: true,
589
+ wiki_write: true,
590
+ plan_update: true,
591
+ goal_update: true,
592
+ action_proof: true,
593
+ dream_run: true,
594
+ };
595
+ export const WATCHME_MIRRORED_KINDS = new Set(Object.keys(WATCHME_MIRRORED_KIND_RECORD));
596
+ /**
597
+ * "Watch me" (§6.1) — the live capture tap behind `crewhaus watchme`. Appends
598
+ * every BUS-ONLY TraceEvent (envelope included — `formatJsonLine` shape) as
599
+ * one JSON line to `<sessionsDir>/<sessionId>.events.jsonl`, the sibling
600
+ * `sessions export` already reads. Skipped: the ephemeral stream kinds and
601
+ * every durably mirrored kind ({@link WATCHME_MIRRORED_KINDS}), so content
602
+ * stays solely in the session `.jsonl` while the sibling carries exact
603
+ * per-turn model attribution via the envelope `turnNumber` on
604
+ * `model_response`.
605
+ *
606
+ * Gated on CREWHAUS_WATCHME=1|true (stamped by `crewhaus run` /
607
+ * the compiled-bundle preambles); returns undefined otherwise. Appends are
608
+ * synchronous (`appendFileSync`, mode 0600, one line < PIPE_BUF ⇒ atomic) and
609
+ * every failure is swallowed — capture can never crash a run.
610
+ */
611
+ export function attachWatchmeCapture(bus, sessionsDir, sessionId, env = process.env) {
612
+ const gate = env["CREWHAUS_WATCHME"];
613
+ if (gate !== "1" && gate !== "true")
614
+ return undefined;
615
+ const path = join(sessionsDir, `${sessionId}.events.jsonl`);
616
+ const unsubscribe = bus.subscribe((event) => {
617
+ if (WATCHME_EPHEMERAL_KINDS.has(event.kind))
618
+ return;
619
+ if (WATCHME_MIRRORED_KINDS.has(event.kind))
620
+ return;
621
+ try {
622
+ appendFileSync(path, formatJsonLine(event), { mode: 0o600 });
623
+ }
624
+ catch {
625
+ // Swallowed by design — a capture hiccup must never abort a turn.
626
+ }
627
+ });
628
+ return { unsubscribe };
629
+ }
@@ -0,0 +1,14 @@
1
+ import type { KVLike, WorkerPlatform } from "@crewhaus/worker-runtime";
2
+ /**
3
+ * Build a Node-backed {@link WorkerPlatform}. `overrides` win over the
4
+ * defaults (tests inject a fixed clock / deterministic id / scripted fetch;
5
+ * production callers may pass a durable `storage`).
6
+ */
7
+ export declare function createNodeWorkerPlatform(overrides?: Partial<WorkerPlatform>): WorkerPlatform;
8
+ /**
9
+ * A process-local {@link KVLike} backed by a `Map` — a convenience for tests,
10
+ * single-process daemons, and the Node platform's optional `storage` slot.
11
+ * TTL is honoured against `Date.now()`; it is NOT durable across restarts (a
12
+ * real deployment wires Cloudflare KV, a Durable Object, or a database here).
13
+ */
14
+ export declare function createInMemoryKV(): KVLike;
@@ -0,0 +1,65 @@
1
+ /**
2
+ * runtime-core — the NODE {@link WorkerPlatform} implementation.
3
+ *
4
+ * G12 splits the agent loop into a platform-neutral core
5
+ * (`@crewhaus/worker-runtime`, which imports no `node:*`) and the host
6
+ * services that surround it. On the edge those services are a stateless
7
+ * `WorkerPlatform` the cf-worker bundle constructs; on Node THIS module is the
8
+ * platform — `Date.now()` for the clock, `node:crypto` for ids, the global
9
+ * `fetch`, and (optionally) an in-memory KV — so the same
10
+ * {@link runWorkerLoop} core runs unchanged in a Node process.
11
+ *
12
+ * runtime-core keeps its own richer node-coupled services (event log, session
13
+ * store, compaction, recovery, audit sinks) around `runChatLoop`; this
14
+ * platform is the seam through which a Node caller drives the *shared* core
15
+ * (e.g. to exercise the exact loop a cf-worker will run) without reaching for
16
+ * the edge globals directly.
17
+ */
18
+ import { randomUUID } from "node:crypto";
19
+ /**
20
+ * Build a Node-backed {@link WorkerPlatform}. `overrides` win over the
21
+ * defaults (tests inject a fixed clock / deterministic id / scripted fetch;
22
+ * production callers may pass a durable `storage`).
23
+ */
24
+ export function createNodeWorkerPlatform(overrides = {}) {
25
+ return {
26
+ now: () => Date.now(),
27
+ randomId: () => randomUUID(),
28
+ // Bind to globalThis so the reference stays valid when destructured, and
29
+ // undici's `fetch` keeps its receiver.
30
+ fetch: globalThis.fetch.bind(globalThis),
31
+ ...overrides,
32
+ };
33
+ }
34
+ /**
35
+ * A process-local {@link KVLike} backed by a `Map` — a convenience for tests,
36
+ * single-process daemons, and the Node platform's optional `storage` slot.
37
+ * TTL is honoured against `Date.now()`; it is NOT durable across restarts (a
38
+ * real deployment wires Cloudflare KV, a Durable Object, or a database here).
39
+ */
40
+ export function createInMemoryKV() {
41
+ const store = new Map();
42
+ return {
43
+ async get(key) {
44
+ const entry = store.get(key);
45
+ if (entry === undefined)
46
+ return null;
47
+ if (entry.expiresAt !== undefined && entry.expiresAt <= Date.now()) {
48
+ store.delete(key);
49
+ return null;
50
+ }
51
+ return entry.value;
52
+ },
53
+ async put(key, value, options) {
54
+ store.set(key, {
55
+ value,
56
+ ...(options?.expirationTtl !== undefined
57
+ ? { expiresAt: Date.now() + options.expirationTtl * 1000 }
58
+ : {}),
59
+ });
60
+ },
61
+ async delete(key) {
62
+ store.delete(key);
63
+ },
64
+ };
65
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crewhaus/runtime-core",
3
- "version": "0.3.2",
3
+ "version": "0.4.2",
4
4
  "type": "module",
5
5
  "description": "Minimal runtime imported by generated harness bundles",
6
6
  "main": "dist/index.js",
@@ -16,49 +16,51 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "@anthropic-ai/sdk": "^0.96.0",
19
- "@crewhaus/abort-controller": "0.3.2",
20
- "@crewhaus/adapter-anthropic": "0.3.2",
21
- "@crewhaus/agent-context-isolation": "0.3.2",
22
- "@crewhaus/boundary-classifier": "0.3.2",
23
- "@crewhaus/circuit-breaker": "0.3.2",
24
- "@crewhaus/compaction-autocompact": "0.3.2",
25
- "@crewhaus/compaction-snip": "0.3.2",
26
- "@crewhaus/cost-tracker": "0.3.2",
27
- "@crewhaus/egress-classifier": "0.3.2",
28
- "@crewhaus/errors": "0.3.2",
29
- "@crewhaus/event-log": "0.3.2",
30
- "@crewhaus/hooks-engine": "0.3.2",
31
- "@crewhaus/model-router": "0.3.2",
32
- "@crewhaus/permission-engine": "0.3.2",
33
- "@crewhaus/prompt-cache-manager": "0.3.2",
34
- "@crewhaus/prompt-injection-detector": "0.3.2",
35
- "@crewhaus/rate-limiter": "0.3.2",
36
- "@crewhaus/recovery-engine": "0.3.2",
37
- "@crewhaus/routing-store": "0.3.2",
38
- "@crewhaus/run-context": "0.3.2",
39
- "@crewhaus/session-store": "0.3.2",
40
- "@crewhaus/skills-registry": "0.3.2",
41
- "@crewhaus/slash-commands": "0.3.2",
42
- "@crewhaus/state-store": "0.3.2",
43
- "@crewhaus/tenancy": "0.3.2",
44
- "@crewhaus/token-budget": "0.3.2",
45
- "@crewhaus/tool-catalog": "0.3.2",
46
- "@crewhaus/tool-executor": "0.3.2",
47
- "@crewhaus/tool-loop-detection": "0.3.2",
48
- "@crewhaus/tool-orchestrator": "0.3.2",
49
- "@crewhaus/tool-result-store": "0.3.2",
50
- "@crewhaus/streaming-tool-executor": "0.3.2",
51
- "@crewhaus/turn-state-machine": "0.3.2",
52
- "@crewhaus/trace-event-bus": "0.3.2",
53
- "@crewhaus/structured-event-printer": "0.3.2",
54
- "@crewhaus/metrics-collector": "0.3.2",
55
- "@crewhaus/otel-exporter": "0.3.2",
19
+ "@crewhaus/abort-controller": "0.4.2",
20
+ "@crewhaus/adapter-anthropic": "0.4.2",
21
+ "@crewhaus/agent-context-isolation": "0.4.2",
22
+ "@crewhaus/boundary-classifier": "0.4.2",
23
+ "@crewhaus/circuit-breaker": "0.4.2",
24
+ "@crewhaus/compaction-autocompact": "0.4.2",
25
+ "@crewhaus/compaction-curator": "0.4.2",
26
+ "@crewhaus/compaction-snip": "0.4.2",
27
+ "@crewhaus/cost-tracker": "0.4.2",
28
+ "@crewhaus/egress-classifier": "0.4.2",
29
+ "@crewhaus/errors": "0.4.2",
30
+ "@crewhaus/event-log": "0.4.2",
31
+ "@crewhaus/hooks-engine": "0.4.2",
32
+ "@crewhaus/model-router": "0.4.2",
33
+ "@crewhaus/permission-engine": "0.4.2",
34
+ "@crewhaus/prompt-cache-manager": "0.4.2",
35
+ "@crewhaus/prompt-injection-detector": "0.4.2",
36
+ "@crewhaus/rate-limiter": "0.4.2",
37
+ "@crewhaus/recovery-engine": "0.4.2",
38
+ "@crewhaus/routing-store": "0.4.2",
39
+ "@crewhaus/run-context": "0.4.2",
40
+ "@crewhaus/session-store": "0.4.2",
41
+ "@crewhaus/skills-registry": "0.4.2",
42
+ "@crewhaus/slash-commands": "0.4.2",
43
+ "@crewhaus/state-store": "0.4.2",
44
+ "@crewhaus/tenancy": "0.4.2",
45
+ "@crewhaus/token-budget": "0.4.2",
46
+ "@crewhaus/tool-catalog": "0.4.2",
47
+ "@crewhaus/tool-executor": "0.4.2",
48
+ "@crewhaus/tool-loop-detection": "0.4.2",
49
+ "@crewhaus/tool-orchestrator": "0.4.2",
50
+ "@crewhaus/tool-result-store": "0.4.2",
51
+ "@crewhaus/streaming-tool-executor": "0.4.2",
52
+ "@crewhaus/turn-state-machine": "0.4.2",
53
+ "@crewhaus/trace-event-bus": "0.4.2",
54
+ "@crewhaus/worker-runtime": "0.4.2",
55
+ "@crewhaus/structured-event-printer": "0.4.2",
56
+ "@crewhaus/metrics-collector": "0.4.2",
57
+ "@crewhaus/otel-exporter": "0.4.2",
56
58
  "zod-to-json-schema": "^3.23.5"
57
59
  },
58
60
  "devDependencies": {
59
- "@crewhaus/logging": "0.3.2",
60
- "@crewhaus/tool-builder": "0.3.2",
61
- "@crewhaus/tool-plan": "0.3.2",
61
+ "@crewhaus/logging": "0.4.2",
62
+ "@crewhaus/tool-builder": "0.4.2",
63
+ "@crewhaus/tool-plan": "0.4.2",
62
64
  "zod": "^3.23.8"
63
65
  },
64
66
  "license": "Apache-2.0",