@ap3x/observe 0.1.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,21 @@
1
+ import type { TraceContext, TraceEvent, TraceEventInput } from "./envelope";
2
+ export interface TraceSink {
3
+ onEvent(event: TraceEvent): void;
4
+ }
5
+ export interface TracePipelineOptions {
6
+ sinks?: TraceSink[];
7
+ /** Test seam for timestamps; defaults to Date.now. */
8
+ now?: () => number;
9
+ }
10
+ /** Stamps ids/seq/ts onto emitter inputs and fans out to sinks. Sink errors never propagate. */
11
+ export declare class TracePipeline {
12
+ #private;
13
+ constructor(options?: TracePipelineOptions);
14
+ get fallbackRoot(): TraceContext;
15
+ get dropped(): number;
16
+ /** Forget a finished trace's seq counter (footer written, run over). */
17
+ releaseTrace(traceId: string): void;
18
+ addSink(sink: TraceSink): () => void;
19
+ emit(input: TraceEventInput, ctx?: TraceContext): TraceEvent;
20
+ }
21
+ //# sourceMappingURL=pipeline.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pipeline.d.ts","sourceRoot":"","sources":["../src/pipeline.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAK5E,MAAM,WAAW,SAAS;IACxB,OAAO,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC;CAClC;AAED,MAAM,WAAW,oBAAoB;IACnC,KAAK,CAAC,EAAE,SAAS,EAAE,CAAC;IACpB,sDAAsD;IACtD,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAED,gGAAgG;AAChG,qBAAa,aAAa;;gBAOZ,OAAO,CAAC,EAAE,oBAAoB;IAK1C,IAAI,YAAY,IAAI,YAAY,CAG/B;IAED,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,wEAAwE;IACxE,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAInC,OAAO,CAAC,IAAI,EAAE,SAAS,GAAG,MAAM,IAAI;IAKpC,IAAI,CAAC,KAAK,EAAE,eAAe,EAAE,GAAG,CAAC,EAAE,YAAY,GAAG,UAAU;CA+B7D"}
@@ -0,0 +1,20 @@
1
+ import type { TraceEvent } from "./envelope";
2
+ import type { TraceSink } from "./pipeline";
3
+ export interface QueuedSinkOptions {
4
+ /** Queue bound; overflow drops events and counts them. Default 1024. */
5
+ maxQueue?: number;
6
+ }
7
+ /**
8
+ * Decouples a sink from the loop (spec: observers can never block or slow a
9
+ * turn). Events enqueue synchronously and drain on the next macrotask; the
10
+ * queue is bounded — overflow drops and increments `dropped`.
11
+ */
12
+ export declare class QueuedSink implements TraceSink {
13
+ #private;
14
+ constructor(inner: TraceSink, options?: QueuedSinkOptions);
15
+ get dropped(): number;
16
+ onEvent(event: TraceEvent): void;
17
+ /** Drain everything queued right now (shutdown, tests). */
18
+ flush(): void;
19
+ }
20
+ //# sourceMappingURL=queued-sink.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queued-sink.d.ts","sourceRoot":"","sources":["../src/queued-sink.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C,MAAM,WAAW,iBAAiB;IAChC,wEAAwE;IACxE,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;;;GAIG;AACH,qBAAa,UAAW,YAAW,SAAS;;gBAO9B,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,iBAAiB;IAKzD,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,OAAO,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI;IAwBhC,2DAA2D;IAC3D,KAAK,IAAI,IAAI;CAGd"}
@@ -0,0 +1,17 @@
1
+ import type { AgentEvent } from "@ap3x/agent-core";
2
+ import type { TraceContext } from "../envelope";
3
+ import type { TracePipeline } from "../pipeline";
4
+ export interface AgentEventSource {
5
+ subscribe(listener: (event: AgentEvent) => void | Promise<void>): () => void;
6
+ }
7
+ export interface ObserveAgentOptions {
8
+ agent?: {
9
+ name?: string;
10
+ model?: string;
11
+ };
12
+ ctx?: TraceContext;
13
+ onDrop?: () => void;
14
+ }
15
+ /** Attaches a trace tap to anything with Agent-style subscribe(). Returns unsubscribe. */
16
+ export declare function observeAgent(source: AgentEventSource, pipeline: TracePipeline, options?: ObserveAgentOptions): () => void;
17
+ //# sourceMappingURL=agent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/taps/agent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,YAAY,EAAmB,MAAM,aAAa,CAAC;AAEjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEjD,MAAM,WAAW,gBAAgB;IAC/B,SAAS,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,MAAM,IAAI,CAAC;CAC9E;AAED,MAAM,WAAW,mBAAmB;IAClC,KAAK,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC1C,GAAG,CAAC,EAAE,YAAY,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;CACrB;AA8DD,0FAA0F;AAC1F,wBAAgB,YAAY,CAC1B,MAAM,EAAE,gBAAgB,EACxB,QAAQ,EAAE,aAAa,EACvB,OAAO,CAAC,EAAE,mBAAmB,GAC5B,MAAM,IAAI,CAiBZ"}
@@ -0,0 +1,12 @@
1
+ import type { TracePipeline } from "../pipeline";
2
+ /** Bridges swarm delegation events into a trace pipeline. Returns unsubscribe. */
3
+ export declare function observeSwarms(pipeline: TracePipeline, options?: {
4
+ onDrop?: () => void;
5
+ }): () => void;
6
+ /**
7
+ * Automatic parent/child span nesting across delegations (P2 line item):
8
+ * every enterDelegation runs its child inside a child TraceContext, so child
9
+ * agent/tool events carry parentSpanId without any explicit ctx threading.
10
+ */
11
+ export declare function enableDelegationSpans(): () => void;
12
+ //# sourceMappingURL=swarm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"swarm.d.ts","sourceRoot":"","sources":["../../src/taps/swarm.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEjD,kFAAkF;AAClF,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,aAAa,EACvB,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,IAAI,CAAA;CAAE,GAChC,MAAM,IAAI,CAmBZ;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,IAAI,CAGlD"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Read-side of the JSONL trace format written by TraceWriter. Pure functions —
3
+ * no filesystem access — so the CLI, tests, and future analyzers share one
4
+ * parser. Tolerant by design: a crashed run has no footer, and a torn write
5
+ * may leave malformed lines; both are counted, never thrown.
6
+ */
7
+ import type { TraceEvent } from "./envelope";
8
+ export interface TraceHeader {
9
+ type: "trace";
10
+ version: number;
11
+ traceId: string;
12
+ startedAt: number;
13
+ }
14
+ export interface TraceFooter {
15
+ type: "trace_end";
16
+ traceId: string;
17
+ dropped: number;
18
+ outcome?: string;
19
+ endedAt: number;
20
+ }
21
+ export interface ParsedTrace {
22
+ header?: TraceHeader;
23
+ events: TraceEvent[];
24
+ footer?: TraceFooter;
25
+ malformedLines: number;
26
+ }
27
+ export declare function parseTraceFile(content: string): ParsedTrace;
28
+ export interface ToolStats {
29
+ calls: number;
30
+ failures: number;
31
+ }
32
+ export interface TraceSummary {
33
+ traceId: string;
34
+ startedAt?: number;
35
+ endedAt?: number;
36
+ durationMs?: number;
37
+ outcome?: string;
38
+ dropped: number;
39
+ events: number;
40
+ malformedLines: number;
41
+ turns: number;
42
+ usage: {
43
+ input: number;
44
+ output: number;
45
+ cacheRead: number;
46
+ cacheWrite: number;
47
+ costTotal?: number;
48
+ };
49
+ toolCalls: number;
50
+ toolFailures: number;
51
+ tools: Record<string, ToolStats>;
52
+ hookRuns: number;
53
+ hookErrors: number;
54
+ agents: string[];
55
+ }
56
+ /** Cost, turns, tool-failure rates, duration — the first optimization-ready dataset. */
57
+ export declare function summarizeTrace(parsed: ParsedTrace): TraceSummary;
58
+ //# sourceMappingURL=trace-file.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trace-file.d.ts","sourceRoot":"","sources":["../src/trace-file.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,MAAM,EAAE,UAAU,EAAE,CAAC;IACrB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,WAAW,CAwB3D;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE;QACL,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,MAAM,CAAC;QACf,SAAS,EAAE,MAAM,CAAC;QAClB,UAAU,EAAE,MAAM,CAAC;QACnB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,CAAC;IACF,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,wFAAwF;AACxF,wBAAgB,cAAc,CAAC,MAAM,EAAE,WAAW,GAAG,YAAY,CA0DhE"}
@@ -0,0 +1,17 @@
1
+ import type { TraceEvent } from "./envelope";
2
+ import type { TraceSink } from "./pipeline";
3
+ export interface TraceWriterOptions {
4
+ dir?: string;
5
+ now?: () => number;
6
+ }
7
+ /** One JSONL file per trace under ~/.ap3x/traces/. Never throws into the pipeline. */
8
+ export declare class TraceWriter implements TraceSink {
9
+ #private;
10
+ constructor(options?: TraceWriterOptions);
11
+ /** Aggregate dropped-event count across all traces. */
12
+ get dropped(): number;
13
+ onEvent(event: TraceEvent): void;
14
+ close(traceId: string, outcome?: "ok" | "error" | "aborted"): void;
15
+ closeAll(outcome?: "ok" | "error" | "aborted"): void;
16
+ }
17
+ //# sourceMappingURL=trace-writer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trace-writer.d.ts","sourceRoot":"","sources":["../src/trace-writer.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C,MAAM,WAAW,kBAAkB;IACjC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AASD,sFAAsF;AAEtF,qBAAa,WAAY,YAAW,SAAS;;gBAS/B,OAAO,CAAC,EAAE,kBAAkB;IAKxC,uDAAuD;IACvD,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,OAAO,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI;IAkBhC,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,GAAG,OAAO,GAAG,SAAS,GAAG,IAAI;IAiBlE,QAAQ,CAAC,OAAO,CAAC,EAAE,IAAI,GAAG,OAAO,GAAG,SAAS,GAAG,IAAI;CAgDrD"}
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@ap3x/observe",
3
+ "version": "0.1.0",
4
+ "description": "Observability seams and a hook engine for agents and swarms",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/AP3X-Dev/AP3X.git",
8
+ "directory": "packages/observe"
9
+ },
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "type": "module",
15
+ "main": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "default": "./dist/index.js"
21
+ }
22
+ },
23
+ "files": ["dist"],
24
+ "scripts": {
25
+ "build": "tsup src/index.ts --format esm --clean --tsconfig tsconfig.build.json && tsc -p tsconfig.build.json",
26
+ "prepublishOnly": "node ../../scripts/publish-gate.mjs",
27
+ "test": "vitest run"
28
+ },
29
+ "dependencies": {
30
+ "@ap3x/agent-core": "0.1.0",
31
+ "@ap3x/ai": "0.1.0",
32
+ "@ap3x/swarms": "0.1.0"
33
+ }
34
+ }