@tangle-network/harness-sessions 1.0.4 → 1.0.5

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.
@@ -16,6 +16,19 @@ function textBlocks(value) {
16
16
  return value.map(textBlocks).filter(Boolean).join('');
17
17
  return isRecord(value) ? str(value.text) ?? textBlocks(value.content) : '';
18
18
  }
19
+ function toolText(update) {
20
+ const fromContent = textBlocks(update.content);
21
+ if (fromContent)
22
+ return fromContent;
23
+ const raw = update.rawOutput;
24
+ if (typeof raw === 'string')
25
+ return raw;
26
+ if (isRecord(raw) && typeof raw.output_for_prompt === 'string' && raw.output_for_prompt)
27
+ return raw.output_for_prompt;
28
+ if (isRecord(raw) && Array.isArray(raw.output) && raw.output.every((byte) => typeof byte === 'number' && byte >= 0 && byte <= 255))
29
+ return Buffer.from(raw.output).toString('utf8');
30
+ return textOf(raw);
31
+ }
19
32
  function usageOf(value) {
20
33
  if (!isRecord(value))
21
34
  return null;
@@ -40,7 +53,8 @@ async function readJson(path) {
40
53
  }
41
54
  function createFold(ref, mode, stats, model, usage) {
42
55
  const builder = new SessionBuilder(HARNESS, FORMAT, EVIDENCE[FORMAT].servedModel, mode, ref.nativeSessionId, ref.parentNativeSessionId);
43
- let opened = false;
56
+ let modelCalls = 0;
57
+ let modelCallId = callId(ref.nativeSessionId);
44
58
  const observe = (record) => {
45
59
  const update = isRecord(record.params) && isRecord(record.params.update) ? record.params.update : isRecord(record.update) ? record.update : record;
46
60
  const kind = str(update.sessionUpdate);
@@ -55,22 +69,25 @@ function createFold(ref, mode, stats, model, usage) {
55
69
  return;
56
70
  }
57
71
  if (kind === 'agent_message_chunk' || kind === 'agent_thought_chunk') {
58
- if (!opened)
59
- builder.modelCall({ id: callId(builder.nativeSessionId), at, provider: null, servedModel: model, requestedModel: model, usage, costUsd: null, stopReason: null, error: null });
60
- opened = true;
72
+ if (kind === 'agent_message_chunk' || modelCalls === 0) {
73
+ modelCallId = `${callId(ref.nativeSessionId)}:${modelCalls}`;
74
+ builder.modelCall({ id: modelCallId, at, provider: null, servedModel: model, requestedModel: model, usage: modelCalls === 0 ? usage : null, costUsd: null, stopReason: null, error: null });
75
+ modelCalls += 1;
76
+ }
61
77
  const text = textBlocks(update.content);
62
78
  const parts = kind === 'agent_thought_chunk' ? [{ type: 'reasoning', text, redacted: text.length === 0 }] : [{ type: 'text', text }];
63
79
  if (text || kind === 'agent_thought_chunk')
64
- builder.message({ id, role: 'assistant', actor: 'agent', at, modelCallId: callId(builder.nativeSessionId), parts });
80
+ builder.message({ id, role: 'assistant', actor: 'agent', at, modelCallId, parts });
65
81
  return;
66
82
  }
67
83
  if (kind === 'tool_call' && typeof update.toolCallId === 'string') {
68
84
  builder.toolCall({ id: update.toolCallId, name: str(update.title) ?? str(update.kind) ?? 'unknown', input: update.rawInput ?? null, inputText: null, at, messageId: id });
69
- builder.message({ id, role: 'assistant', actor: 'agent', at, modelCallId: callId(builder.nativeSessionId), parts: [{ type: 'tool-call', toolCallId: update.toolCallId }] });
85
+ builder.message({ id, role: 'assistant', actor: 'agent', at, modelCallId, parts: [{ type: 'tool-call', toolCallId: update.toolCallId }] });
70
86
  return;
71
87
  }
72
88
  if (kind === 'tool_call_update' && typeof update.toolCallId === 'string') {
73
- builder.toolResult(update.toolCallId, { output: update.rawOutput ?? update.content ?? null, text: textOf(update.rawOutput ?? update.content), isError: update.status === 'failed', at, messageId: `${id}:tool` });
89
+ const text = toolText(update);
90
+ builder.toolResult(update.toolCallId, { output: update.rawOutput ?? update.content ?? null, text, isError: update.status === 'failed', at, messageId: `${id}:tool` });
74
91
  builder.message({ id: `${id}:tool`, role: 'tool', actor: 'tool-result', at, modelCallId: null, parts: [{ type: 'tool-result', toolCallId: update.toolCallId }] });
75
92
  }
76
93
  };
package/dist/source.d.ts CHANGED
@@ -22,12 +22,7 @@ export declare class SessionParseError extends Error {
22
22
  readonly line: number;
23
23
  constructor(path: string, line: number, reason: string);
24
24
  }
25
- /**
26
- * Streams the JSON records of one append-only JSONL file. Each record is parsed and yielded in
27
- * order; the whole file is hashed as it is read. An unparsable line is counted (or thrown in
28
- * strict mode). A final line without a newline that does not parse is a record the harness was
29
- * still writing when the copy was taken, so it is reported as a torn tail rather than corruption.
30
- */
25
+ /** Stream one JSONL file in order and hash it. Unparsable lines count, or throw in strict mode. A torn tail is an unfinished last line, not corruption. */
31
26
  export declare function readJsonlRecords(path: string, stats: SourceStats, options?: {
32
27
  strict?: boolean;
33
28
  signal?: AbortSignal;
@@ -40,21 +35,13 @@ export declare function fileRecords(path: string, options?: {
40
35
  }): RecordSource;
41
36
  /** Stats for bytes already in memory. */
42
37
  export declare function memoryStats(label: string, text?: string): SourceStats;
43
- /**
44
- * The records of JSONL text already in memory (a retained copy, a blob), read synchronously.
45
- * `firstLine` numbers the text's first line, for text that continues a longer file.
46
- */
38
+ /** JSONL text already in memory. `firstLine` numbers its first line when the text continues a longer file. */
47
39
  export declare function textRecordsSync(text: string, stats: SourceStats, options?: {
48
40
  strict?: boolean;
49
41
  firstLine?: number;
50
42
  }): Generator<Record<string, unknown>>;
51
43
  /** Records a caller already parsed (one per line, in order, the first on line `firstLine`). Non-objects count as unparsed. */
52
44
  export declare function arrayRecordsSync(values: readonly unknown[], stats: SourceStats, firstLine?: number): Generator<Record<string, unknown>>;
53
- /**
54
- * Run `use` against a SQLite store opened read-only in place ({@link openSqlite}). A store SQLite
55
- * cannot open in place is read from a private copy instead: a write-ahead log with no `-shm` in a
56
- * directory SQLite cannot write (a capture on read-only storage), or a Node whose `node:sqlite`
57
- * does not accept URI file names (before 22.15).
58
- */
45
+ /** Run `use` on a store opened read-only in place. If SQLite cannot, read a private copy: a WAL with no `-shm` on read-only storage, or Node before 22.15 without URI file names. */
59
46
  export declare function withSqlite<T>(path: string, use: (db: DatabaseSync) => T): Promise<T>;
60
47
  export declare function isMissing(error: unknown): boolean;
package/dist/source.js CHANGED
@@ -23,12 +23,7 @@ export class SessionParseError extends Error {
23
23
  }
24
24
  }
25
25
  const NEWLINE = 0x0a;
26
- /**
27
- * Streams the JSON records of one append-only JSONL file. Each record is parsed and yielded in
28
- * order; the whole file is hashed as it is read. An unparsable line is counted (or thrown in
29
- * strict mode). A final line without a newline that does not parse is a record the harness was
30
- * still writing when the copy was taken, so it is reported as a torn tail rather than corruption.
31
- */
26
+ /** Stream one JSONL file in order and hash it. Unparsable lines count, or throw in strict mode. A torn tail is an unfinished last line, not corruption. */
32
27
  export async function* readJsonlRecords(path, stats, options = {}) {
33
28
  const hash = createHash('sha256');
34
29
  let pending = [];
@@ -126,10 +121,7 @@ export function memoryStats(label, text) {
126
121
  const bytes = Buffer.from(text, 'utf8');
127
122
  return { path: label, sha256: `sha256:${createHash('sha256').update(bytes).digest('hex')}`, bytes: bytes.length, unparsed: 0, tornTail: false, line: 0 };
128
123
  }
129
- /**
130
- * The records of JSONL text already in memory (a retained copy, a blob), read synchronously.
131
- * `firstLine` numbers the text's first line, for text that continues a longer file.
132
- */
124
+ /** JSONL text already in memory. `firstLine` numbers its first line when the text continues a longer file. */
133
125
  export function* textRecordsSync(text, stats, options = {}) {
134
126
  const lines = text.split('\n');
135
127
  const first = options.firstLine ?? 1;
@@ -191,14 +183,7 @@ async function exists(path) {
191
183
  throw error;
192
184
  }
193
185
  }
194
- /**
195
- * Open a SQLite store read-only where it lies. A store with a write-ahead log beside it is opened
196
- * `mode=ro`: SQLite reads the log's committed tail and coordinates with a live writer through the
197
- * store's shared-memory index (`-shm`), and never writes the store or its log or checkpoints it. A
198
- * store without a log is opened `immutable=1`: no connection holds it open, the file holds every
199
- * committed row, and SQLite creates no `-wal` or `-shm` beside it. Throws when SQLite cannot read
200
- * the store in place.
201
- */
186
+ /** Open a SQLite store read-only in place. A WAL uses `mode=ro` (committed tail, coordinates through `-shm`, never writes or checkpoints). No log uses `immutable=1` (no live connection, no new `-wal`/`-shm`). Throws when SQLite cannot read it there. */
202
187
  async function openSqlite(path) {
203
188
  const mode = (await exists(`${path}-wal`)) ? 'mode=ro' : 'immutable=1';
204
189
  const { DatabaseSync } = loadSqlite();
@@ -220,12 +205,7 @@ function cannotOpen(error) {
220
205
  const code = error?.errcode;
221
206
  return typeof code === 'number' && (code & 0xff) === 14;
222
207
  }
223
- /**
224
- * Run `use` against a SQLite store opened read-only in place ({@link openSqlite}). A store SQLite
225
- * cannot open in place is read from a private copy instead: a write-ahead log with no `-shm` in a
226
- * directory SQLite cannot write (a capture on read-only storage), or a Node whose `node:sqlite`
227
- * does not accept URI file names (before 22.15).
228
- */
208
+ /** Run `use` on a store opened read-only in place. If SQLite cannot, read a private copy: a WAL with no `-shm` on read-only storage, or Node before 22.15 without URI file names. */
229
209
  export async function withSqlite(path, use) {
230
210
  // SQLite follows a symlinked store to its target and keeps the log there, so the log is looked
231
211
  // for there too. A missing store throws ENOENT here, which callers read as "no sessions".
@@ -276,11 +256,7 @@ async function storeKey(path) {
276
256
  }
277
257
  return `${path}|${parts.join('|')}`;
278
258
  }
279
- /**
280
- * Run `use` against a private copy of a SQLite store: the store and its `-wal` and `-shm` siblings
281
- * are copied and only the copy is opened, writable, so SQLite replays the copied log into it. `use`
282
- * is synchronous so no other read can replace the copy while it runs.
283
- */
259
+ /** Run `use` on a private copy of the store and its `-wal`/`-shm`, opened writable so SQLite replays the log. `use` is synchronous. */
284
260
  async function withSqliteCopy(path, use) {
285
261
  const key = await storeKey(path);
286
262
  if (current?.key !== key) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/harness-sessions",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "One reader for every coding-agent harness's native session (Claude Code, Codex, OpenCode, Pi): store layouts, a normalized session with messages, tool calls, served models, usage and ending errors, and projections to chat messages, OTLP spans and turns",
5
5
  "type": "module",
6
6
  "sideEffects": false,