@jitsusama/agentic-harness.core 0.3.1 → 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,11 @@
1
+ import type { LedgerScan } from "./types.js";
2
+ /**
3
+ * Read billable turns out of a session log's lines.
4
+ *
5
+ * Every line is offered to the parser and every outcome is counted, so a
6
+ * malformed line costs one entry rather than the remainder of the file.
7
+ * Both places a turn can carry usage are read: assistant turns hold it
8
+ * under `message`, and compactions hold it at the top level beside
9
+ * `type`.
10
+ */
11
+ export declare function readTurns(sessionId: string, lines: Iterable<string>): LedgerScan;
@@ -0,0 +1,159 @@
1
+ import { createHash } from "node:crypto";
2
+ import { SessionCollector } from "./session.js";
3
+ /** Width of a stored content address. 96 bits is ample for a corpus of
4
+ * a few million turns and keeps the index small. */
5
+ const DIGEST_CHARS = 24;
6
+ /**
7
+ * Read billable turns out of a session log's lines.
8
+ *
9
+ * Every line is offered to the parser and every outcome is counted, so a
10
+ * malformed line costs one entry rather than the remainder of the file.
11
+ * Both places a turn can carry usage are read: assistant turns hold it
12
+ * under `message`, and compactions hold it at the top level beside
13
+ * `type`.
14
+ */
15
+ export function readTurns(sessionId, lines) {
16
+ const turns = [];
17
+ const session = new SessionCollector(sessionId);
18
+ let count = 0;
19
+ let parsed = 0;
20
+ let unparseable = 0;
21
+ let billable = 0;
22
+ let unmetered = 0;
23
+ for (const line of lines) {
24
+ count += 1;
25
+ if (!line.trim())
26
+ continue;
27
+ let entry;
28
+ try {
29
+ const value = JSON.parse(line);
30
+ if (typeof value !== "object" || value === null) {
31
+ unparseable += 1;
32
+ continue;
33
+ }
34
+ entry = value;
35
+ }
36
+ catch {
37
+ // A truncated or interleaved write. Counted, never fatal: one
38
+ // unreadable line once reduced a corpus-wide total to a tenth
39
+ // of the truth by aborting the pipeline that met it.
40
+ unparseable += 1;
41
+ continue;
42
+ }
43
+ parsed += 1;
44
+ if (entry.type === "session") {
45
+ session.observeHeader(entry);
46
+ continue;
47
+ }
48
+ if (entry.customType === "quest-workflow") {
49
+ const data = asRecord(entry.data);
50
+ if (data)
51
+ session.observeWorkflow(data);
52
+ continue;
53
+ }
54
+ const turn = turnFrom(sessionId, entry);
55
+ if (!turn)
56
+ continue;
57
+ session.observeTurn(turn.timestamp);
58
+ turns.push(turn);
59
+ if (turn.cost)
60
+ billable += 1;
61
+ else
62
+ unmetered += 1;
63
+ }
64
+ return {
65
+ turns,
66
+ coverage: { lines: count, parsed, unparseable, billable, unmetered },
67
+ session: session.record(),
68
+ };
69
+ }
70
+ function turnFrom(sessionId, entry) {
71
+ const kind = kindOf(entry);
72
+ if (!kind)
73
+ return null;
74
+ const message = asRecord(entry.message);
75
+ const usage = asRecord(kind === "compaction" ? entry.usage : message?.usage);
76
+ const entryId = typeof entry.id === "string" ? entry.id : "";
77
+ const timestamp = typeof entry.timestamp === "string" ? entry.timestamp : "";
78
+ const model = typeof message?.model === "string" ? message.model : "";
79
+ return {
80
+ entryId,
81
+ sessionId,
82
+ timestamp,
83
+ kind,
84
+ model,
85
+ tokens: tokensFrom(usage),
86
+ cost: costFrom(usage),
87
+ cacheWrite1h: usage?.cacheWrite1h ?? 0,
88
+ droppedBefore: kind === "compaction" && typeof entry.tokensBefore === "number"
89
+ ? entry.tokensBefore
90
+ : null,
91
+ firstKeptEntryId: typeof entry.firstKeptEntryId === "string"
92
+ ? entry.firstKeptEntryId
93
+ : null,
94
+ digest: digestOf(entryId, timestamp, kind, usage),
95
+ };
96
+ }
97
+ /**
98
+ * Which turns are billable at all. An assistant turn and a compaction
99
+ * both cost money; a user message, a tool result and a state change do
100
+ * not. Enumerated rather than filtered, so a new entry type is ignored
101
+ * by omission instead of silently swept into a total.
102
+ */
103
+ function kindOf(entry) {
104
+ if (entry.type === "compaction")
105
+ return "compaction";
106
+ const message = asRecord(entry.message);
107
+ if (message?.role === "assistant")
108
+ return "assistant";
109
+ return null;
110
+ }
111
+ function tokensFrom(usage) {
112
+ const input = usage?.input ?? 0;
113
+ const output = usage?.output ?? 0;
114
+ const cacheRead = usage?.cacheRead ?? 0;
115
+ const cacheWrite = usage?.cacheWrite ?? 0;
116
+ return {
117
+ input,
118
+ output,
119
+ cacheRead,
120
+ cacheWrite,
121
+ total: usage?.totalTokens ?? input + output + cacheRead + cacheWrite,
122
+ };
123
+ }
124
+ /** Null when the entry reported no cost, which is not the same as free. */
125
+ function costFrom(usage) {
126
+ const cost = usage?.cost;
127
+ if (!cost || typeof cost.total !== "number")
128
+ return null;
129
+ return {
130
+ input: cost.input ?? 0,
131
+ output: cost.output ?? 0,
132
+ cacheRead: cost.cacheRead ?? 0,
133
+ cacheWrite: cost.cacheWrite ?? 0,
134
+ total: cost.total,
135
+ };
136
+ }
137
+ /**
138
+ * Address a turn by what it is rather than where it was found. Forking a
139
+ * session copies entries verbatim, ids included, so the same turn appears
140
+ * in several files and a naive count bills it more than once.
141
+ */
142
+ function digestOf(entryId, timestamp, kind, usage) {
143
+ const canonical = JSON.stringify([
144
+ entryId,
145
+ timestamp,
146
+ kind,
147
+ usage?.cost?.total ?? null,
148
+ usage?.totalTokens ?? null,
149
+ ]);
150
+ return createHash("sha256")
151
+ .update(canonical)
152
+ .digest("hex")
153
+ .slice(0, DIGEST_CHARS);
154
+ }
155
+ function asRecord(value) {
156
+ return typeof value === "object" && value !== null
157
+ ? value
158
+ : null;
159
+ }
@@ -0,0 +1,38 @@
1
+ import type { SessionRecord } from "./types.js";
2
+ /**
3
+ * Name the repo a working directory belongs to, or nothing when the path
4
+ * names none.
5
+ *
6
+ * A monorepo zone is named by the zone rather than the worktree it was
7
+ * cut into, because two trees of the same zone are the same subject and
8
+ * naming the tree would split one zone's spend across every tree ever
9
+ * cut for it.
10
+ */
11
+ export declare function repoOf(cwd: string | null): string | null;
12
+ /**
13
+ * Accumulates what a log says about its session as the log is read, so
14
+ * one pass serves both the turns and their attribution.
15
+ */
16
+ export declare class SessionCollector {
17
+ private readonly sessionId;
18
+ private cwd;
19
+ private quest;
20
+ private first;
21
+ private last;
22
+ constructor(sessionId: string);
23
+ /**
24
+ * Take the working directory from a session's header entry. Every log
25
+ * opens with one, which is what makes attribution complete rather than
26
+ * limited to the quarter of sessions that also name a quest.
27
+ */
28
+ observeHeader(entry: Record<string, unknown>): void;
29
+ /**
30
+ * Take the working directory and quest a workflow entry names. The
31
+ * last one wins, because a session can be re-pointed at another quest
32
+ * part way through and the later statement is the current one.
33
+ */
34
+ observeWorkflow(data: Record<string, unknown>): void;
35
+ /** Widen the span to include a billed turn. */
36
+ observeTurn(timestamp: string): void;
37
+ record(): SessionRecord;
38
+ }
@@ -0,0 +1,89 @@
1
+ /** Where a per-host checkout tree begins, as `src/{host}/{owner}/{repo}`. */
2
+ const CHECKOUT_MARKER = "/src/";
3
+ /** Where a monorepo worktree begins, as `world/trees/{tree}/src/{zone}`. */
4
+ const MONOREPO_MARKER = "/world/trees/";
5
+ /** Segments naming a repo under the checkout marker: host, owner, name. */
6
+ const REPO_SEGMENTS = 3;
7
+ /**
8
+ * Name the repo a working directory belongs to, or nothing when the path
9
+ * names none.
10
+ *
11
+ * A monorepo zone is named by the zone rather than the worktree it was
12
+ * cut into, because two trees of the same zone are the same subject and
13
+ * naming the tree would split one zone's spend across every tree ever
14
+ * cut for it.
15
+ */
16
+ export function repoOf(cwd) {
17
+ if (!cwd)
18
+ return null;
19
+ const monorepo = cwd.indexOf(MONOREPO_MARKER);
20
+ if (monorepo >= 0) {
21
+ const tail = cwd.slice(monorepo + MONOREPO_MARKER.length);
22
+ const zone = tail.split("/src/")[1];
23
+ return zone ? `world/${zone}` : null;
24
+ }
25
+ const checkout = cwd.indexOf(CHECKOUT_MARKER);
26
+ if (checkout >= 0) {
27
+ const parts = cwd
28
+ .slice(checkout + CHECKOUT_MARKER.length)
29
+ .split("/")
30
+ .filter(Boolean);
31
+ if (parts.length >= REPO_SEGMENTS) {
32
+ return parts.slice(0, REPO_SEGMENTS).join("/");
33
+ }
34
+ }
35
+ return null;
36
+ }
37
+ /**
38
+ * Accumulates what a log says about its session as the log is read, so
39
+ * one pass serves both the turns and their attribution.
40
+ */
41
+ export class SessionCollector {
42
+ sessionId;
43
+ cwd = null;
44
+ quest = null;
45
+ first = null;
46
+ last = null;
47
+ constructor(sessionId) {
48
+ this.sessionId = sessionId;
49
+ }
50
+ /**
51
+ * Take the working directory from a session's header entry. Every log
52
+ * opens with one, which is what makes attribution complete rather than
53
+ * limited to the quarter of sessions that also name a quest.
54
+ */
55
+ observeHeader(entry) {
56
+ if (typeof entry.cwd === "string")
57
+ this.cwd = entry.cwd;
58
+ }
59
+ /**
60
+ * Take the working directory and quest a workflow entry names. The
61
+ * last one wins, because a session can be re-pointed at another quest
62
+ * part way through and the later statement is the current one.
63
+ */
64
+ observeWorkflow(data) {
65
+ if (typeof data.cwd === "string")
66
+ this.cwd = data.cwd;
67
+ if (typeof data.questId === "string")
68
+ this.quest = data.questId;
69
+ }
70
+ /** Widen the span to include a billed turn. */
71
+ observeTurn(timestamp) {
72
+ if (!timestamp)
73
+ return;
74
+ if (!this.first || timestamp < this.first)
75
+ this.first = timestamp;
76
+ if (!this.last || timestamp > this.last)
77
+ this.last = timestamp;
78
+ }
79
+ record() {
80
+ return {
81
+ sessionId: this.sessionId,
82
+ cwd: this.cwd,
83
+ repo: repoOf(this.cwd),
84
+ quest: this.quest,
85
+ firstSeen: this.first,
86
+ lastSeen: this.last,
87
+ };
88
+ }
89
+ }
@@ -21,7 +21,7 @@ export interface RecordOutcome {
21
21
  readonly duplicates: number;
22
22
  }
23
23
  /** What a total or a slice may be narrowed to. */
24
- export type CostDimension = "model" | "session" | "kind" | "day" | "repo" | "quest";
24
+ export type CostDimension = "model" | "session" | "kind" | "day" | "repo" | "quest" | "thinking";
25
25
  /** A content-addressed store of billable turns. */
26
26
  export interface TurnStore {
27
27
  recordTurns(turns: readonly TurnRecord[]): Promise<RecordOutcome>;
@@ -40,6 +40,8 @@ export interface TurnStore {
40
40
  paybackReplay(): Promise<PaybackReplay>;
41
41
  total(): Promise<LedgerTotal>;
42
42
  costBy(dimension: CostDimension): Promise<CostSlice[]>;
43
+ /** Every session the ledger holds, for another store to join to. */
44
+ sessions(): Promise<SessionRecord[]>;
43
45
  close(): Promise<void>;
44
46
  }
45
47
  /**
@@ -80,6 +80,7 @@ const GROUP_BY = {
80
80
  // that quietly excluded most of the money.
81
81
  repo: "COALESCE(sessions.repo, '')",
82
82
  quest: "COALESCE(sessions.quest, '')",
83
+ thinking: "COALESCE(turns.thinking_level, '')",
83
84
  };
84
85
  /**
85
86
  * Open (creating if needed) a turn ledger at the given path. Safe to
@@ -90,8 +91,22 @@ export async function openTurnStore(dbPath) {
90
91
  const db = await openDb(dbPath);
91
92
  await db.exec("PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 5000;");
92
93
  await db.exec(SCHEMA);
94
+ await migrate(db);
93
95
  return new SqliteTurnStore(db);
94
96
  }
97
+ /**
98
+ * Bring a ledger written at an older shape up to this one. A new ledger
99
+ * is created at the original shape and migrated like any other, so every
100
+ * test that opens a fresh store also exercises the path an existing file
101
+ * meets. Every step is additive, so nothing a ledger holds can be lost by
102
+ * opening it.
103
+ */
104
+ async function migrate(db) {
105
+ const columns = new Set((await db.all("PRAGMA table_info(turns)")).map((column) => column.name));
106
+ if (!columns.has("thinking_level")) {
107
+ await db.exec("ALTER TABLE turns ADD COLUMN thinking_level TEXT");
108
+ }
109
+ }
95
110
  class SqliteTurnStore {
96
111
  db;
97
112
  constructor(db) {
@@ -128,6 +143,7 @@ class SqliteTurnStore {
128
143
  // Seen before, so not billed again, but the sighting is still
129
144
  // recorded: deduplicating must not hide that it happened.
130
145
  await this.sight(t);
146
+ await this.fillThinkingLevel(t);
131
147
  continue;
132
148
  }
133
149
  inserted += 1;
@@ -136,8 +152,8 @@ class SqliteTurnStore {
136
152
  tokens_input, tokens_output, tokens_cache_read,
137
153
  tokens_cache_write, tokens_total, cache_write_1h,
138
154
  cost_input, cost_output, cost_cache_read, cost_cache_write,
139
- cost_total, dropped_before, first_kept_entry_id
140
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
155
+ cost_total, dropped_before, first_kept_entry_id, thinking_level
156
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
141
157
  t.digest,
142
158
  t.entryId,
143
159
  t.sessionId,
@@ -157,6 +173,7 @@ class SqliteTurnStore {
157
173
  t.cost?.total ?? null,
158
174
  t.droppedBefore,
159
175
  t.firstKeptEntryId,
176
+ t.thinkingLevel,
160
177
  ]);
161
178
  await this.sight(t);
162
179
  }
@@ -498,9 +515,32 @@ class SqliteTurnStore {
498
515
  turns: r.turns,
499
516
  }));
500
517
  }
518
+ async sessions() {
519
+ const rows = await this.db.all(`SELECT session_id, cwd, repo, quest, first_seen, last_seen
520
+ FROM sessions ORDER BY session_id`);
521
+ return rows.map((r) => ({
522
+ sessionId: r.session_id,
523
+ cwd: r.cwd,
524
+ repo: r.repo,
525
+ quest: r.quest,
526
+ firstSeen: r.first_seen,
527
+ lastSeen: r.last_seen,
528
+ }));
529
+ }
501
530
  async close() {
502
531
  await this.db.close();
503
532
  }
533
+ /**
534
+ * Give a held turn the thinking level a later scan learned, when it
535
+ * had none. This is how a ledger indexed before the column existed
536
+ * gets it on the next rescan, without billing anything twice. A level
537
+ * already known is left alone: the same entry cannot have run at two.
538
+ */
539
+ async fillThinkingLevel(t) {
540
+ if (t.thinkingLevel === null)
541
+ return;
542
+ await this.db.run("UPDATE turns SET thinking_level = ? WHERE digest = ? AND thinking_level IS NULL", [t.thinkingLevel, t.digest]);
543
+ }
504
544
  async sight(t) {
505
545
  await this.db.run("INSERT OR IGNORE INTO sightings (digest, session_id) VALUES (?, ?)", [t.digest, t.sessionId]);
506
546
  }
@@ -17,6 +17,13 @@ export interface TurnRecord {
17
17
  readonly kind: TurnKind;
18
18
  /** Resolved model id, or empty when the entry did not say. */
19
19
  readonly model: string;
20
+ /**
21
+ * The thinking level the turn ran at, as the log last set it before
22
+ * the turn, or null when the log never said. Unknown is not a level:
23
+ * guessing the harness default would put spend on a setting that may
24
+ * not have been in force.
25
+ */
26
+ readonly thinkingLevel: string | null;
20
27
  readonly tokens: RunTokens;
21
28
  /** Null when unmetered. Never coerced to zero. */
22
29
  readonly cost: RunCost | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jitsusama/agentic-harness.core",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "Pi-agnostic business logic for agentic-harness: state machines, guardian decisions, quest/TDD domain model.",
5
5
  "license": "MIT",
6
6
  "type": "module",