@hmharness/observability 0.7.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.
- package/dist/events.d.ts +67 -0
- package/dist/events.js +47 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/recorder.d.ts +14 -0
- package/dist/recorder.js +49 -0
- package/dist/store.d.ts +14 -0
- package/dist/store.js +90 -0
- package/package.json +28 -0
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/observability - RunEvent schema v1 (V2 blueprint M1).
|
|
3
|
+
*
|
|
4
|
+
* Every agent run becomes a reproducible experiment sample: one append-only
|
|
5
|
+
* trajectory.jsonl of typed events plus a summary. Schema rules:
|
|
6
|
+
* - `type` is one of RUN_EVENT_TYPES below (closed set, versioned here;
|
|
7
|
+
* unknown types must still deserialize - readers skip them, so old
|
|
8
|
+
* replays keep working when the schema grows)
|
|
9
|
+
* - payloads carry SUMMARIES, never secrets: tool name, truncated args,
|
|
10
|
+
* outcome flags, durations. No env vars, no config dumps.
|
|
11
|
+
* - append-only: no event is ever rewritten; corrections are new events.
|
|
12
|
+
*/
|
|
13
|
+
export type RunId = string;
|
|
14
|
+
export type EventId = string;
|
|
15
|
+
export type EventActor = 'user' | 'agent' | 'tool' | 'system' | 'judge';
|
|
16
|
+
/** Closed set for schema v1. The build/judge/repair event families are
|
|
17
|
+
* emitted once those subsystems route through the recorder (M2+); the
|
|
18
|
+
* schema ships first. */
|
|
19
|
+
export declare const RUN_EVENT_TYPES: readonly ["run.created", "run.started", "context.assembled", "model.requested", "model.responded", "tool.requested", "tool.approved", "tool.denied", "tool.completed", "build.started", "build.completed", "test.started", "test.completed", "error.observed", "repair.proposed", "repair.applied", "judge.started", "judge.completed", "checkpoint.created", "checkpoint.restored", "run.completed", "run.failed"];
|
|
20
|
+
export type RunEventType = (typeof RUN_EVENT_TYPES)[number];
|
|
21
|
+
export interface RunEvent<T = unknown> {
|
|
22
|
+
id: EventId;
|
|
23
|
+
runId: RunId;
|
|
24
|
+
/** ISO timestamp. */
|
|
25
|
+
ts: string;
|
|
26
|
+
type: RunEventType | string;
|
|
27
|
+
actor: EventActor;
|
|
28
|
+
payload: T;
|
|
29
|
+
parentEventId?: EventId;
|
|
30
|
+
}
|
|
31
|
+
export interface RunMetrics {
|
|
32
|
+
turns?: number;
|
|
33
|
+
toolUses?: number;
|
|
34
|
+
promptTokens?: number;
|
|
35
|
+
completionTokens?: number;
|
|
36
|
+
durationMs?: number;
|
|
37
|
+
}
|
|
38
|
+
export interface RunOutcome {
|
|
39
|
+
success: boolean;
|
|
40
|
+
/** loop stop reason: final | idle | turn-valve | token-valve | interrupted | error */
|
|
41
|
+
reason?: string;
|
|
42
|
+
error?: string;
|
|
43
|
+
}
|
|
44
|
+
export interface Trajectory {
|
|
45
|
+
runId: RunId;
|
|
46
|
+
task: string;
|
|
47
|
+
model?: string;
|
|
48
|
+
cwd?: string;
|
|
49
|
+
startedAt: string;
|
|
50
|
+
finishedAt?: string;
|
|
51
|
+
outcome?: RunOutcome;
|
|
52
|
+
metrics?: RunMetrics;
|
|
53
|
+
events: RunEvent[];
|
|
54
|
+
}
|
|
55
|
+
export interface RunSummary {
|
|
56
|
+
runId: RunId;
|
|
57
|
+
task: string;
|
|
58
|
+
model?: string;
|
|
59
|
+
cwd?: string;
|
|
60
|
+
startedAt: string;
|
|
61
|
+
finishedAt?: string;
|
|
62
|
+
outcome?: RunOutcome;
|
|
63
|
+
metrics?: RunMetrics;
|
|
64
|
+
eventCount?: number;
|
|
65
|
+
}
|
|
66
|
+
/** Truncate a value into a trajectory-safe payload summary (no secrets, bounded). */
|
|
67
|
+
export declare function brief(value: unknown, max?: number): string;
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/observability - RunEvent schema v1 (V2 blueprint M1).
|
|
3
|
+
*
|
|
4
|
+
* Every agent run becomes a reproducible experiment sample: one append-only
|
|
5
|
+
* trajectory.jsonl of typed events plus a summary. Schema rules:
|
|
6
|
+
* - `type` is one of RUN_EVENT_TYPES below (closed set, versioned here;
|
|
7
|
+
* unknown types must still deserialize - readers skip them, so old
|
|
8
|
+
* replays keep working when the schema grows)
|
|
9
|
+
* - payloads carry SUMMARIES, never secrets: tool name, truncated args,
|
|
10
|
+
* outcome flags, durations. No env vars, no config dumps.
|
|
11
|
+
* - append-only: no event is ever rewritten; corrections are new events.
|
|
12
|
+
*/
|
|
13
|
+
/** Closed set for schema v1. The build/judge/repair event families are
|
|
14
|
+
* emitted once those subsystems route through the recorder (M2+); the
|
|
15
|
+
* schema ships first. */
|
|
16
|
+
export const RUN_EVENT_TYPES = [
|
|
17
|
+
'run.created',
|
|
18
|
+
'run.started',
|
|
19
|
+
'context.assembled',
|
|
20
|
+
'model.requested',
|
|
21
|
+
'model.responded',
|
|
22
|
+
'tool.requested',
|
|
23
|
+
'tool.approved',
|
|
24
|
+
'tool.denied',
|
|
25
|
+
'tool.completed',
|
|
26
|
+
'build.started',
|
|
27
|
+
'build.completed',
|
|
28
|
+
'test.started',
|
|
29
|
+
'test.completed',
|
|
30
|
+
'error.observed',
|
|
31
|
+
'repair.proposed',
|
|
32
|
+
'repair.applied',
|
|
33
|
+
'judge.started',
|
|
34
|
+
'judge.completed',
|
|
35
|
+
'checkpoint.created',
|
|
36
|
+
'checkpoint.restored',
|
|
37
|
+
'run.completed',
|
|
38
|
+
'run.failed',
|
|
39
|
+
];
|
|
40
|
+
/** Truncate a value into a trajectory-safe payload summary (no secrets, bounded). */
|
|
41
|
+
export function brief(value, max = 160) {
|
|
42
|
+
const s = typeof value === 'string' ? value : JSON.stringify(value);
|
|
43
|
+
if (s === undefined)
|
|
44
|
+
return '';
|
|
45
|
+
const flat = s.replace(/\s+/g, ' ').trim();
|
|
46
|
+
return flat.length > max ? flat.slice(0, max) + '…' : flat;
|
|
47
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type TrajectoryStore } from './store.ts';
|
|
2
|
+
import { type EventActor, type RunEventType, type RunMetrics, type RunOutcome } from './events.ts';
|
|
3
|
+
export interface TrajectoryRecorder {
|
|
4
|
+
readonly runId: string;
|
|
5
|
+
emit(type: RunEventType | string, actor: EventActor, payload: unknown, parentEventId?: string): void;
|
|
6
|
+
finish(outcome: RunOutcome, metrics?: RunMetrics): void;
|
|
7
|
+
}
|
|
8
|
+
export declare function newRunId(now?: Date): string;
|
|
9
|
+
/** Fire-and-forget queue: appends happen serially so JSONL lines never interleave. */
|
|
10
|
+
export declare function createTrajectoryRecorder(home: string, meta: {
|
|
11
|
+
task: string;
|
|
12
|
+
model?: string;
|
|
13
|
+
cwd?: string;
|
|
14
|
+
}, store?: TrajectoryStore): TrajectoryRecorder;
|
package/dist/recorder.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/observability - TrajectoryRecorder.
|
|
3
|
+
*
|
|
4
|
+
* The runner-side convenience wrapper: creates a run id, stamps events with
|
|
5
|
+
* ids/timestamps, and swallows every storage error - observability must never
|
|
6
|
+
* fail a run (ADR-0001 rule 2). One recorder per agent task; finish() writes
|
|
7
|
+
* summary.json.
|
|
8
|
+
*/
|
|
9
|
+
import { randomBytes } from 'node:crypto';
|
|
10
|
+
import { jsonlTrajectoryStore } from "./store.js";
|
|
11
|
+
import { brief } from "./events.js";
|
|
12
|
+
export function newRunId(now = new Date()) {
|
|
13
|
+
const t = now.toISOString().replace(/[-:T]/g, '').slice(0, 14);
|
|
14
|
+
return `run_${t}_${randomBytes(3).toString('hex')}`;
|
|
15
|
+
}
|
|
16
|
+
/** Fire-and-forget queue: appends happen serially so JSONL lines never interleave. */
|
|
17
|
+
export function createTrajectoryRecorder(home, meta, store = jsonlTrajectoryStore(home)) {
|
|
18
|
+
const runId = newRunId();
|
|
19
|
+
const startedAt = new Date().toISOString();
|
|
20
|
+
let seq = 0;
|
|
21
|
+
let chain = Promise.resolve();
|
|
22
|
+
let finished = false;
|
|
23
|
+
const push = (fn) => {
|
|
24
|
+
chain = chain.then(fn).catch(() => undefined); // best-effort, ordered
|
|
25
|
+
};
|
|
26
|
+
const emit = (type, actor, payload, parentEventId) => {
|
|
27
|
+
const ev = { id: `${runId}-${++seq}`, runId, ts: new Date().toISOString(), type, actor, payload };
|
|
28
|
+
if (parentEventId)
|
|
29
|
+
ev.parentEventId = parentEventId;
|
|
30
|
+
push(() => store.append(ev));
|
|
31
|
+
};
|
|
32
|
+
emit('run.created', 'system', { task: brief(meta.task, 400), model: meta.model, cwd: meta.cwd });
|
|
33
|
+
return {
|
|
34
|
+
runId,
|
|
35
|
+
emit,
|
|
36
|
+
finish(outcome, metrics) {
|
|
37
|
+
if (finished)
|
|
38
|
+
return;
|
|
39
|
+
finished = true;
|
|
40
|
+
emit(outcome.success ? 'run.completed' : 'run.failed', 'system', { ...outcome, ...metrics });
|
|
41
|
+
push(async () => {
|
|
42
|
+
await store.saveSummary(runId, {
|
|
43
|
+
runId, task: meta.task, model: meta.model, cwd: meta.cwd,
|
|
44
|
+
startedAt, finishedAt: new Date().toISOString(), outcome, metrics,
|
|
45
|
+
}, seq);
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RunEvent, RunId, RunMetrics, RunOutcome, RunSummary, Trajectory } from './events.ts';
|
|
2
|
+
export interface TrajectoryStore {
|
|
3
|
+
runDir(runId: RunId): string;
|
|
4
|
+
append(event: RunEvent): Promise<void>;
|
|
5
|
+
getRun(runId: RunId): Promise<Trajectory>;
|
|
6
|
+
listRuns(limit?: number): Promise<RunSummary[]>;
|
|
7
|
+
/** Persist the run summary (called once at finish; cheap enough to re-call). */
|
|
8
|
+
saveSummary(runId: RunId, head: Omit<Trajectory, 'events'>, eventCount: number): Promise<void>;
|
|
9
|
+
exportRun(runId: RunId, format: 'jsonl' | 'json'): Promise<string>;
|
|
10
|
+
}
|
|
11
|
+
export declare function runsRoot(home: string): string;
|
|
12
|
+
export declare function jsonlTrajectoryStore(home: string): TrajectoryStore;
|
|
13
|
+
/** Outcome/metrics convenience for finish() callers. */
|
|
14
|
+
export type { RunOutcome, RunMetrics };
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/observability - TrajectoryStore (V2 blueprint M1 storage).
|
|
3
|
+
*
|
|
4
|
+
* Deliberately boring for v1: append-only JSONL per run under
|
|
5
|
+
* <home>/runs/<run-id>/trajectory.jsonl plus summary.json. The interface is
|
|
6
|
+
* the contract; a SQLite/Postgres adapter can replace the implementation
|
|
7
|
+
* later without touching callers. Every method is best-effort by design in
|
|
8
|
+
* the RECORDER (which wraps this store); the store itself surfaces errors so
|
|
9
|
+
* tests can assert on real behavior.
|
|
10
|
+
*/
|
|
11
|
+
import { appendFile, mkdir, readdir, readFile, writeFile } from 'node:fs/promises';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
export function runsRoot(home) {
|
|
14
|
+
return join(home, 'runs');
|
|
15
|
+
}
|
|
16
|
+
export function jsonlTrajectoryStore(home) {
|
|
17
|
+
const dirOf = (runId) => join(runsRoot(home), runId.replace(/[^a-zA-Z0-9_-]/g, ''));
|
|
18
|
+
return {
|
|
19
|
+
runDir: dirOf,
|
|
20
|
+
async append(event) {
|
|
21
|
+
const dir = dirOf(event.runId);
|
|
22
|
+
await mkdir(dir, { recursive: true });
|
|
23
|
+
await appendFile(join(dir, 'trajectory.jsonl'), JSON.stringify(event) + '\n', 'utf8');
|
|
24
|
+
},
|
|
25
|
+
async getRun(runId) {
|
|
26
|
+
const dir = dirOf(runId);
|
|
27
|
+
let head;
|
|
28
|
+
try {
|
|
29
|
+
head = JSON.parse(await readFile(join(dir, 'summary.json'), 'utf8'));
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// unfinished run: synthesize a head from the first event
|
|
33
|
+
const lines = (await readFile(join(dir, 'trajectory.jsonl'), 'utf8')).trim();
|
|
34
|
+
const first = lines ? JSON.parse(lines.split('\n')[0]) : null;
|
|
35
|
+
head = {
|
|
36
|
+
runId,
|
|
37
|
+
task: first?.type === 'run.started' ? String(first.payload?.task ?? '') : '',
|
|
38
|
+
startedAt: first?.ts ?? new Date().toISOString(),
|
|
39
|
+
events: [],
|
|
40
|
+
};
|
|
41
|
+
delete head.events;
|
|
42
|
+
}
|
|
43
|
+
const events = [];
|
|
44
|
+
try {
|
|
45
|
+
const raw = await readFile(join(dir, 'trajectory.jsonl'), 'utf8');
|
|
46
|
+
for (const line of raw.trim().split('\n')) {
|
|
47
|
+
if (!line)
|
|
48
|
+
continue;
|
|
49
|
+
try {
|
|
50
|
+
events.push(JSON.parse(line));
|
|
51
|
+
}
|
|
52
|
+
catch { /* torn tail line */ }
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
catch { /* no events yet */ }
|
|
56
|
+
return { ...head, events };
|
|
57
|
+
},
|
|
58
|
+
async listRuns(limit = 20) {
|
|
59
|
+
let ids = [];
|
|
60
|
+
try {
|
|
61
|
+
ids = (await readdir(runsRoot(home))).filter((d) => d.startsWith('run_')).sort().reverse();
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
const out = [];
|
|
67
|
+
for (const id of ids.slice(0, limit)) {
|
|
68
|
+
try {
|
|
69
|
+
const s = JSON.parse(await readFile(join(runsRoot(home), id, 'summary.json'), 'utf8'));
|
|
70
|
+
out.push({ ...s, runId: id });
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
out.push({ runId: id, task: '(unfinished)', startedAt: '' });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
},
|
|
78
|
+
async saveSummary(runId, head, eventCount) {
|
|
79
|
+
const dir = dirOf(runId);
|
|
80
|
+
await mkdir(dir, { recursive: true });
|
|
81
|
+
await writeFile(join(dir, 'summary.json'), JSON.stringify({ ...head, eventCount }, null, 2) + '\n', 'utf8');
|
|
82
|
+
},
|
|
83
|
+
async exportRun(runId, format) {
|
|
84
|
+
const t = await this.getRun(runId);
|
|
85
|
+
if (format === 'json')
|
|
86
|
+
return JSON.stringify(t, null, 2);
|
|
87
|
+
return t.events.map((e) => JSON.stringify(e)).join('\n') + '\n';
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hmharness/observability",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "hmharness observability: typed RunEvent schema + append-only TrajectoryStore. Every agent run becomes a reproducible, comparable, exportable experiment sample (V2 blueprint M1 - flight recorder). Zero runtime dependencies.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc -p tsconfig.build.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/swsgbl/hmharness.git"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=22"
|
|
27
|
+
}
|
|
28
|
+
}
|