@tangle-network/harness-sessions 1.0.10 → 1.0.12

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.
@@ -165,14 +165,16 @@ function foldSession(db, ref, mode, builder) {
165
165
  }
166
166
  }
167
167
  if (steps === 0) {
168
- builder.modelCall({ id: `${row.id}:request`, at, provider: messageError ? null : provider, servedModel: messageError ? null : model, requestedModel: model, usage: usageOf(data.tokens), costUsd: count(data.cost), stopReason: str(data.finish), error: messageError });
168
+ // Pending requests have zero defaults; a live fork can copy measured counters before its parts.
169
+ const observed = messageError === null && isoTime(time.completed) !== null && str(data.finish) !== null;
170
+ const usage = usageOf(data.tokens), costUsd = count(data.cost);
171
+ const accounted = observed || (costUsd ?? 0) > 0 || Object.values(usage ?? {}).some(value => (value ?? 0) > 0);
172
+ builder.modelCall({ id: `${row.id}:request`, at, provider: observed ? provider : null, servedModel: observed ? model : null, requestedModel: model, usage: accounted ? usage : null, costUsd: accounted ? costUsd : null, stopReason: str(data.finish), error: messageError });
169
173
  if (messageError)
170
174
  builder.message({ id: row.id, role: 'assistant', actor: 'injected', at, modelCallId: `${row.id}:request`, parts: [{ type: 'text', text: messageError.message }] });
171
175
  }
172
- else if (messageError) {
173
- const last = builder.modelCalls[builder.modelCalls.length - 1];
174
- last.error = messageError;
175
- }
176
+ else if (messageError)
177
+ builder.modelCalls[builder.modelCalls.length - 1].error = messageError;
176
178
  aborted = messageError?.kind === 'MessageAbortedError' ? { at: messageError.at, error: messageError } : null;
177
179
  }
178
180
  const children = db.prepare('SELECT id FROM session WHERE parent_id = ? ORDER BY time_created, id').all(ref.nativeSessionId);
package/dist/source.js CHANGED
@@ -26,7 +26,6 @@ const NEWLINE = 0x0a;
26
26
  export async function* readJsonlRecords(path, stats, options = {}) {
27
27
  const hash = createHash('sha256');
28
28
  let pending = [];
29
- let pendingBytes = 0;
30
29
  let lineNumber = 0;
31
30
  const parse = (bytes, terminated) => {
32
31
  lineNumber += 1;
@@ -53,6 +52,7 @@ export async function* readJsonlRecords(path, stats, options = {}) {
53
52
  noteUnparsed(stats, lineNumber);
54
53
  return undefined;
55
54
  }
55
+ stats.line = lineNumber;
56
56
  return value;
57
57
  };
58
58
  if (typeof constants.O_NOFOLLOW !== 'number' || typeof constants.O_NONBLOCK !== 'number')
@@ -61,6 +61,8 @@ export async function* readJsonlRecords(path, stats, options = {}) {
61
61
  try {
62
62
  if (!(await file.stat()).isFile())
63
63
  throw new Error(`${path}: native session must be a regular file`);
64
+ // A stream created on an aborted signal emits an 'error' no one listens to, which crashes the process.
65
+ options.signal?.throwIfAborted();
64
66
  const stream = file.createReadStream({ highWaterMark: 1 << 20, signal: options.signal, autoClose: false });
65
67
  for await (const chunk of stream) {
66
68
  hash.update(chunk);
@@ -71,33 +73,26 @@ export async function* readJsonlRecords(path, stats, options = {}) {
71
73
  if (index === -1)
72
74
  break;
73
75
  let line;
74
- if (pendingBytes > 0) {
76
+ if (pending.length > 0) {
75
77
  pending.push(chunk.subarray(start, index));
76
78
  line = Buffer.concat(pending);
77
79
  pending = [];
78
- pendingBytes = 0;
79
80
  }
80
81
  else {
81
82
  line = chunk.subarray(start, index);
82
83
  }
83
84
  const record = parse(line, true);
84
- if (record !== undefined) {
85
- stats.line = lineNumber;
85
+ if (record !== undefined)
86
86
  yield record;
87
- }
88
87
  start = index + 1;
89
88
  }
90
- if (start < chunk.length) {
89
+ if (start < chunk.length)
91
90
  pending.push(chunk.subarray(start));
92
- pendingBytes += chunk.length - start;
93
- }
94
91
  }
95
- if (pendingBytes > 0) {
92
+ if (pending.length > 0) {
96
93
  const record = parse(Buffer.concat(pending), false);
97
- if (record !== undefined) {
98
- stats.line = lineNumber;
94
+ if (record !== undefined)
99
95
  yield record;
100
- }
101
96
  }
102
97
  stats.sha256 = `sha256:${hash.digest('hex')}`;
103
98
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/harness-sessions",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
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,