@fiducian/runtime 0.4.0

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.
@@ -0,0 +1,41 @@
1
+ import { Conductor, CONTEXT_HEADER, contextFromHeader, headerFromContext, Actorchestra, actorchestra, type StoryContext, type TraceEvent, type Violation } from "@fiducian/conductor";
2
+ import { compileMonitor, evaluateTrace } from "@fiducian/compiler";
3
+ import { parseWaltzFile, type PropertySpec } from "@fiducian/spec";
4
+ import { type AlertOptions } from "@fiducian/alerts";
5
+ export interface FiduciaTraceOptions {
6
+ spec: PropertySpec;
7
+ conductor?: Conductor;
8
+ alerts?: AlertOptions;
9
+ onViolation?: (violation: Violation) => void;
10
+ }
11
+ export declare class FiduciaTrace {
12
+ readonly conductor: Conductor;
13
+ readonly spec: PropertySpec;
14
+ private monitor;
15
+ private violations;
16
+ private passedContexts;
17
+ private readonly alerts?;
18
+ private readonly onViolation?;
19
+ constructor(options: FiduciaTraceOptions);
20
+ static fromFile(specPath: string): FiduciaTrace;
21
+ startStory(parentContext?: string): StoryContext;
22
+ startStoryWithId(contextId: string, parentContext?: string): StoryContext;
23
+ emit(ctx: StoryContext, message: Omit<TraceEvent, "contextId" | "timestamp" | "op"> & {
24
+ op?: string;
25
+ }): TraceEvent;
26
+ private handleEvent;
27
+ getViolations(): Violation[];
28
+ hasPassed(contextId: string): boolean;
29
+ evaluateRecorded(): ReturnType<typeof evaluateTrace>;
30
+ exportTraceJsonl(): string;
31
+ }
32
+ export interface HttpLikeRequest {
33
+ headers: Record<string, string | string[] | undefined>;
34
+ }
35
+ export declare function extractContext(req: HttpLikeRequest): StoryContext | undefined;
36
+ export declare function attachContext(ctx: StoryContext, headers: Record<string, string>): Record<string, string>;
37
+ export declare function middleware(ct: FiduciaTrace): (req: HttpLikeRequest, run: (ctx: StoryContext) => void) => StoryContext;
38
+ export { Conductor, CONTEXT_HEADER, contextFromHeader, headerFromContext, Actorchestra, actorchestra, compileMonitor, evaluateTrace, parseWaltzFile, };
39
+ export type { StoryContext, TraceEvent, Violation, PropertySpec };
40
+ export type { AlertOptions } from "@fiducian/alerts";
41
+ export { notifyTraceResult, notifyViolations } from "@fiducian/alerts";
package/dist/index.js ADDED
@@ -0,0 +1,85 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { Conductor, CONTEXT_HEADER, contextFromHeader, headerFromContext, Actorchestra, actorchestra, } from "@fiducian/conductor";
3
+ import { compileMonitor, evaluateTrace } from "@fiducian/compiler";
4
+ import { parseWaltzFile } from "@fiducian/spec";
5
+ import { notifyViolations } from "@fiducian/alerts";
6
+ export class FiduciaTrace {
7
+ conductor;
8
+ spec;
9
+ monitor;
10
+ violations = [];
11
+ passedContexts = new Set();
12
+ alerts;
13
+ onViolation;
14
+ constructor(options) {
15
+ this.conductor = options.conductor ?? new Conductor();
16
+ this.spec = options.spec;
17
+ this.alerts = options.alerts;
18
+ this.onViolation = options.onViolation;
19
+ this.monitor = compileMonitor(this.spec);
20
+ this.conductor.subscribe((event) => this.handleEvent(event));
21
+ }
22
+ static fromFile(specPath) {
23
+ const source = readFileSync(specPath, "utf8");
24
+ return new FiduciaTrace({ spec: parseWaltzFile(source) });
25
+ }
26
+ startStory(parentContext) {
27
+ return this.conductor.startStory(parentContext);
28
+ }
29
+ startStoryWithId(contextId, parentContext) {
30
+ return this.conductor.startStoryWithId(contextId, parentContext);
31
+ }
32
+ emit(ctx, message) {
33
+ return this.conductor.emit(ctx, {
34
+ op: message.op ?? "send",
35
+ from: message.from,
36
+ to: message.to,
37
+ payload: message.payload,
38
+ });
39
+ }
40
+ handleEvent(event) {
41
+ const state = this.monitor.onEvent(event);
42
+ if (state.violation) {
43
+ this.violations.push(state.violation);
44
+ this.onViolation?.(state.violation);
45
+ if (this.alerts) {
46
+ void notifyViolations([state.violation], this.alerts);
47
+ }
48
+ }
49
+ if (state.status === "passed") {
50
+ this.passedContexts.add(event.contextId);
51
+ }
52
+ }
53
+ getViolations() {
54
+ return [...this.violations];
55
+ }
56
+ hasPassed(contextId) {
57
+ return this.passedContexts.has(contextId);
58
+ }
59
+ evaluateRecorded() {
60
+ return evaluateTrace(this.spec, [...this.conductor.getEvents()]);
61
+ }
62
+ exportTraceJsonl() {
63
+ return this.conductor
64
+ .getEvents()
65
+ .map((e) => JSON.stringify(e))
66
+ .join("\n");
67
+ }
68
+ }
69
+ export function extractContext(req) {
70
+ const raw = req.headers[CONTEXT_HEADER];
71
+ const header = Array.isArray(raw) ? raw[0] : raw;
72
+ return contextFromHeader(header);
73
+ }
74
+ export function attachContext(ctx, headers) {
75
+ return { ...headers, [CONTEXT_HEADER]: headerFromContext(ctx) };
76
+ }
77
+ export function middleware(ct) {
78
+ return (req, run) => {
79
+ const ctx = extractContext(req) ?? ct.startStory();
80
+ run(ctx);
81
+ return ctx;
82
+ };
83
+ }
84
+ export { Conductor, CONTEXT_HEADER, contextFromHeader, headerFromContext, Actorchestra, actorchestra, compileMonitor, evaluateTrace, parseWaltzFile, };
85
+ export { notifyTraceResult, notifyViolations } from "@fiducian/alerts";
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@fiducian/runtime",
3
+ "version": "0.4.0",
4
+ "description": "FiduciaTrace runtime integration for live causal chain checking",
5
+ "license": "AGPL-3.0-or-later",
6
+ "keywords": ["fiducia", "fiducia", "runtime", "microservices", "agents"],
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/thesnmc/Fiducian.git",
10
+ "directory": "trace/packages/runtime"
11
+ },
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "files": ["dist"],
16
+ "type": "module",
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js"
23
+ }
24
+ },
25
+ "dependencies": {
26
+ "@fiducian/alerts": "0.4.0",
27
+ "@fiducian/conductor": "0.4.0",
28
+ "@fiducian/compiler": "0.4.0",
29
+ "@fiducian/spec": "0.4.0"
30
+ },
31
+ "scripts": {
32
+ "build": "tsc -p tsconfig.json",
33
+ "test": "node --test dist/**/*.test.js",
34
+ "prepublishOnly": "npm run build"
35
+ }
36
+ }