@tangle-network/traces 0.23.1 → 1.0.1

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.
Files changed (75) hide show
  1. package/README.md +61 -1078
  2. package/dist/analyst-model-call-JVUOYZ5V.js +20 -0
  3. package/dist/ask-2KF4KSPB.js +39 -0
  4. package/dist/check-YF6ZHPGG.js +322 -0
  5. package/dist/chunk-2XXABIRL.js +25 -0
  6. package/dist/chunk-4VKPN432.js +138 -0
  7. package/dist/chunk-6GOG5KIK.js +53 -0
  8. package/dist/chunk-7USMM4MA.js +5695 -0
  9. package/dist/chunk-CG5QHQO4.js +492 -0
  10. package/dist/chunk-CQ6UYZG6.js +1217 -0
  11. package/dist/chunk-DU534HAX.js +263 -0
  12. package/dist/chunk-FC4V25CG.js +503 -0
  13. package/dist/chunk-GLUDGUGJ.js +232 -0
  14. package/dist/chunk-GNRXKQEQ.js +120 -0
  15. package/dist/chunk-K2KH2HHD.js +228 -0
  16. package/dist/chunk-M3W6TZHQ.js +381 -0
  17. package/dist/chunk-O52XK7OB.js +19 -0
  18. package/dist/chunk-PYH3PL3N.js +62 -0
  19. package/dist/chunk-QLVP37YB.js +782 -0
  20. package/dist/chunk-QUHUMZ4W.js +759 -0
  21. package/dist/chunk-REO2JQJY.js +535 -0
  22. package/dist/chunk-RZL5GAWH.js +60 -0
  23. package/dist/chunk-TOCNGML5.js +273 -0
  24. package/dist/chunk-UQSAGXPH.js +208 -0
  25. package/dist/chunk-VFOLC4OX.js +407 -0
  26. package/dist/chunk-WF7PTDCZ.js +3921 -0
  27. package/dist/chunk-YAX72TST.js +259 -0
  28. package/dist/cli.js +2086 -2050
  29. package/dist/diff-J4RAIOXO.js +16 -0
  30. package/dist/improvement-LCCVGGUS.js +25 -0
  31. package/dist/index.d.ts +1235 -4357
  32. package/dist/index.js +53 -1204
  33. package/dist/live-XXL7VZWB.js +14 -0
  34. package/dist/mcp-EYIDKBEK.js +85 -0
  35. package/dist/report-E3G2T5TJ.js +338 -0
  36. package/dist/run-watch-RAOOAXOF.js +649 -0
  37. package/dist/supervisor-report-RMPLLBGZ.js +29 -0
  38. package/dist/supervisor-run-context-WO6VOTGV.js +30 -0
  39. package/dist/upload-3R7GRSKS.js +20 -0
  40. package/dist/verify-safe-C5ZXZOZ2.js +8 -0
  41. package/docs/analyze.md +76 -0
  42. package/docs/ask.md +88 -0
  43. package/docs/bundle.md +40 -0
  44. package/docs/check.md +77 -0
  45. package/docs/convert.md +44 -0
  46. package/docs/develop.md +32 -0
  47. package/docs/diff.md +38 -0
  48. package/docs/facts.md +114 -0
  49. package/docs/mcp.md +36 -0
  50. package/docs/migrate-1.0.md +33 -0
  51. package/docs/parsers.md +66 -0
  52. package/docs/release-1.0.0.md +147 -0
  53. package/docs/release-1.0.1.md +4 -0
  54. package/docs/sdk.md +77 -0
  55. package/docs/select.md +70 -0
  56. package/docs/skills.md +46 -0
  57. package/docs/upload.md +47 -0
  58. package/docs/watch-jsonl.v1.example.jsonl +5 -0
  59. package/docs/watch.md +66 -0
  60. package/examples/README.md +9 -0
  61. package/examples/custom-analyst.ts +65 -74
  62. package/install.sh +22 -2
  63. package/package.json +10 -9
  64. package/skills/inspect-agent-traces/SKILL.md +47 -110
  65. package/dist/chunk-7DVZCCZI.js +0 -20983
  66. package/docs/replay-verify.md +0 -123
  67. package/docs/stream-findings.svg +0 -39
  68. package/docs/trace-analysts.md +0 -643
  69. package/examples/custom-backend.ts +0 -21
  70. package/examples/external-engines.ts +0 -62
  71. package/examples/improvement-config.mjs +0 -63
  72. package/examples/observe-and-alert.ts +0 -19
  73. package/examples/register-harness.ts +0 -26
  74. package/skills/build-trace-analyst/SKILL.md +0 -76
  75. package/skills/build-trace-analyst/agents/openai.yaml +0 -4
@@ -0,0 +1,263 @@
1
+ // src/adapters/tool-io.ts
2
+ import { createHash as createHash2 } from "crypto";
3
+
4
+ // src/source-location.ts
5
+ import { createHash } from "crypto";
6
+ import { resolve } from "path";
7
+ var SOURCE_ATTRIBUTE_PREFIX = "traces.source_record.";
8
+ var locations = /* @__PURE__ */ new WeakMap();
9
+ function sourceFileId(path) {
10
+ return createHash("sha256").update(resolve(path)).digest("hex");
11
+ }
12
+ function pointerPart(value) {
13
+ return value.replaceAll("~", "~0").replaceAll("/", "~1");
14
+ }
15
+ function locateSourceObjects(value, path, bytes, offset = 0) {
16
+ if (!value || typeof value !== "object") return;
17
+ const record = {
18
+ sourceId: sourceFileId(path),
19
+ recordOffset: offset,
20
+ recordBytes: bytes.length,
21
+ recordSha256: createHash("sha256").update(bytes).digest("hex")
22
+ };
23
+ const pending = [{ value, pointer: "#" }];
24
+ while (pending.length > 0) {
25
+ const current = pending.pop();
26
+ locations.set(current.value, { ...record, fieldLocator: current.pointer });
27
+ for (const [key, child] of Object.entries(current.value)) {
28
+ if (child && typeof child === "object") {
29
+ pending.push({ value: child, pointer: `${current.pointer}/${pointerPart(key)}` });
30
+ }
31
+ }
32
+ }
33
+ }
34
+ function sourceOf(value, ...fields) {
35
+ if (!value || typeof value !== "object") return void 0;
36
+ const location = locations.get(value);
37
+ return location ? {
38
+ ...location,
39
+ fieldLocator: `${location.fieldLocator}${fields.map((field) => `/${pointerPart(field)}`).join("")}`
40
+ } : void 0;
41
+ }
42
+ function textSources(value, field) {
43
+ if (!value || typeof value !== "object") return void 0;
44
+ const content = value[field];
45
+ if (typeof content === "string") return sourceOf(value, field);
46
+ if (!Array.isArray(content)) return void 0;
47
+ return content.flatMap((block, index) => {
48
+ if (typeof block === "string") {
49
+ const ref2 = sourceOf(value, field, String(index));
50
+ return ref2 ? [ref2] : [];
51
+ }
52
+ if (!block || typeof block !== "object" || typeof block.text !== "string") return [];
53
+ if (block.type !== void 0 && !["text", "input_text", "output_text"].includes(block.type)) return [];
54
+ const ref = sourceOf(block, "text");
55
+ return ref ? [ref] : [];
56
+ });
57
+ }
58
+ function sourceAttributes(attribute, sources) {
59
+ const refs = sources === void 0 ? [] : Array.isArray(sources) ? sources : [sources];
60
+ return refs.length === 0 ? {} : { [`${SOURCE_ATTRIBUTE_PREFIX}${attribute}`]: JSON.stringify(refs) };
61
+ }
62
+ function appendSourceAttributes(attributes, attribute, sources) {
63
+ const key = `${SOURCE_ATTRIBUTE_PREFIX}${attribute}`;
64
+ const added = sourceAttributes(attribute, sources)[key];
65
+ if (typeof added !== "string") return;
66
+ const prior = attributes[key];
67
+ const refs = typeof prior === "string" ? JSON.parse(prior) : [];
68
+ const known = new Set(refs.map((ref) => JSON.stringify(ref)));
69
+ for (const ref of JSON.parse(added)) {
70
+ if (!known.has(JSON.stringify(ref))) {
71
+ refs.push(ref);
72
+ known.add(JSON.stringify(ref));
73
+ }
74
+ }
75
+ attributes[key] = JSON.stringify(refs);
76
+ }
77
+ function stripSourceAttributes(attributes) {
78
+ return Object.fromEntries(Object.entries(attributes).filter(([key]) => !key.startsWith(SOURCE_ATTRIBUTE_PREFIX)));
79
+ }
80
+
81
+ // src/adapters/tool-io.ts
82
+ var TOOL_IO_VALUE_MAX_BYTES = 16 * 1024;
83
+ var TOOL_CALL_LEVEL_ATTR = "traces.tool_call.level";
84
+ var INNER_TOOL_CALL_LEVEL = "inner";
85
+ function isInnerToolCall(attributes) {
86
+ return attributes[TOOL_CALL_LEVEL_ATTR] === INNER_TOOL_CALL_LEVEL;
87
+ }
88
+ var TOOL_IO_SIDES = ["input", "output"];
89
+ var LEGACY_TRUNCATION_MARKER = /\n\[truncated (\d+) bytes; sha256=[a-f0-9]{64}\]$/;
90
+ var COUNTED_TRUNCATION_MARKER = /\n\[truncated \d+ bytes\]$/;
91
+ var TRUNCATION_MARKER = /\n\[truncated\]$/;
92
+ function canonicalJson(value) {
93
+ const sort = (item) => {
94
+ if (Array.isArray(item)) return item.map(sort);
95
+ if (!item || typeof item !== "object") return item;
96
+ return Object.fromEntries(
97
+ Object.entries(item).filter(([, entry]) => entry !== void 0).sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0).map(([key, entry]) => [key, sort(entry)])
98
+ );
99
+ };
100
+ const json = JSON.stringify(sort(value));
101
+ if (json === void 0) throw new TypeError("tool I/O value is not JSON-serializable");
102
+ return json;
103
+ }
104
+ function serialize(value) {
105
+ if (value === void 0) return void 0;
106
+ if (typeof value === "string") {
107
+ const trimmed = value.trim();
108
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
109
+ try {
110
+ return { value: canonicalJson(JSON.parse(trimmed)), mimeType: "application/json" };
111
+ } catch {
112
+ }
113
+ }
114
+ return { value, mimeType: "text/plain" };
115
+ }
116
+ return { value: canonicalJson(value), mimeType: "application/json" };
117
+ }
118
+ function utf8Prefix(value, maxBytes) {
119
+ let bytes = 0;
120
+ let end = 0;
121
+ for (const char of value) {
122
+ const charBytes = Buffer.byteLength(char);
123
+ if (bytes + charBytes > maxBytes) break;
124
+ bytes += charBytes;
125
+ end += char.length;
126
+ }
127
+ return Buffer.from(value.slice(0, end), "utf8").toString("utf8");
128
+ }
129
+ function truncatedValue(value, bytes) {
130
+ const longestMarker = `
131
+ [truncated ${bytes} bytes]`;
132
+ const prefix = utf8Prefix(value, TOOL_IO_VALUE_MAX_BYTES - Buffer.byteLength(longestMarker));
133
+ const omittedBytes = bytes - Buffer.byteLength(prefix);
134
+ return `${prefix}
135
+ [truncated ${omittedBytes} bytes]`;
136
+ }
137
+ function boundedString(value) {
138
+ if (value.length <= TOOL_IO_VALUE_MAX_BYTES / 4) return value;
139
+ const bytes = Buffer.byteLength(value);
140
+ return bytes > TOOL_IO_VALUE_MAX_BYTES ? truncatedValue(value, bytes) : value;
141
+ }
142
+ function valueAttributes(side, raw) {
143
+ const serialized = serialize(raw);
144
+ if (!serialized) return {};
145
+ const bytes = Buffer.byteLength(serialized.value);
146
+ const sha256 = createHash2("sha256").update(serialized.value).digest("hex");
147
+ const truncated = bytes > TOOL_IO_VALUE_MAX_BYTES;
148
+ const value = truncated ? truncatedValue(serialized.value, bytes) : serialized.value;
149
+ return {
150
+ [`${side}.value`]: value,
151
+ [`${side}.mime_type`]: serialized.mimeType,
152
+ [`traces.${side}.bytes`]: bytes,
153
+ [`traces.${side}.sha256`]: sha256,
154
+ [`traces.${side}.truncated`]: truncated
155
+ };
156
+ }
157
+ function toolIoAttributes(io) {
158
+ return {
159
+ ...valueAttributes("input", io.input),
160
+ ...valueAttributes("output", io.output),
161
+ ...io.input === void 0 ? {} : sourceAttributes("input.value", io.inputSource),
162
+ ...io.output === void 0 ? {} : sourceAttributes("output.value", io.outputSource),
163
+ ...io.argsCaptured === void 0 ? {} : { "tool.args_captured": io.argsCaptured }
164
+ };
165
+ }
166
+ function toolArgumentsFromAttributes(attributes) {
167
+ const declared = attributes["tool.args_captured"];
168
+ const explicit = typeof declared === "boolean" ? declared : declared === "true" ? true : declared === "false" ? false : void 0;
169
+ const argsCaptured = explicit ?? attributes["input.value"] !== void 0;
170
+ return {
171
+ args: argsCaptured ? attributes["input.value"] : void 0,
172
+ argsCaptured
173
+ };
174
+ }
175
+ function normalizeToolIoAttributes(attributes) {
176
+ for (const side of TOOL_IO_SIDES) {
177
+ const valueKey = `${side}.value`;
178
+ const mimeKey = `${side}.mime_type`;
179
+ const bytesKey = `traces.${side}.bytes`;
180
+ const digestKey = `traces.${side}.sha256`;
181
+ const truncatedKey = `traces.${side}.truncated`;
182
+ const rawValue = attributes[valueKey];
183
+ if (typeof rawValue !== "string") {
184
+ delete attributes[mimeKey];
185
+ delete attributes[bytesKey];
186
+ delete attributes[digestKey];
187
+ delete attributes[truncatedKey];
188
+ continue;
189
+ }
190
+ let value = rawValue.replace(LEGACY_TRUNCATION_MARKER, "\n[truncated]").replace(COUNTED_TRUNCATION_MARKER, "\n[truncated]");
191
+ let truncated = TRUNCATION_MARKER.test(value);
192
+ const unboundedBytes = Buffer.byteLength(value);
193
+ if (unboundedBytes > TOOL_IO_VALUE_MAX_BYTES) {
194
+ value = truncatedValue(value, unboundedBytes);
195
+ truncated = true;
196
+ }
197
+ attributes[valueKey] = value;
198
+ attributes[bytesKey] = Buffer.byteLength(value);
199
+ attributes[digestKey] = createHash2("sha256").update(value).digest("hex");
200
+ attributes[truncatedKey] = truncated;
201
+ }
202
+ }
203
+ function recordToolOutput(toolSpan, output, source) {
204
+ if (!toolSpan) return;
205
+ if (output === void 0) return;
206
+ delete toolSpan.attributes[`${SOURCE_ATTRIBUTE_PREFIX}output.value`];
207
+ Object.assign(toolSpan.attributes, valueAttributes("output", output));
208
+ Object.assign(toolSpan.attributes, sourceAttributes("output.value", source));
209
+ }
210
+
211
+ // src/time.ts
212
+ function parseIsoToEpochMs(ts) {
213
+ if (typeof ts !== "string" || ts.trim().length === 0) {
214
+ throw new TypeError("timestamp must be a non-empty string");
215
+ }
216
+ const value = ts.trim();
217
+ const n = /^\d+$/.test(value) ? Number(value) : Date.parse(value);
218
+ if (!Number.isFinite(n) || !Number.isFinite(new Date(n).getTime())) {
219
+ throw new TypeError(`invalid timestamp: ${ts}`);
220
+ }
221
+ return n;
222
+ }
223
+ function parseSince(value) {
224
+ return parseTimeBound(value, "--since");
225
+ }
226
+ function relativeBoundMs(value) {
227
+ const m = value.match(/^(\d+)\s*([mhd])$/i);
228
+ if (!m) return void 0;
229
+ const unit = m[2].toLowerCase();
230
+ return Number(m[1]) * (unit === "m" ? 6e4 : unit === "h" ? 36e5 : 864e5);
231
+ }
232
+ function parseTimeBound(value, flag) {
233
+ const relative = relativeBoundMs(value);
234
+ if (relative !== void 0) return Date.now() - relative;
235
+ const t = Date.parse(value);
236
+ if (Number.isNaN(t)) throw new Error(`${flag}: expected 30m / 2h / 7d or an ISO date, got "${value}"`);
237
+ return t;
238
+ }
239
+
240
+ export {
241
+ SOURCE_ATTRIBUTE_PREFIX,
242
+ sourceFileId,
243
+ locateSourceObjects,
244
+ sourceOf,
245
+ textSources,
246
+ sourceAttributes,
247
+ appendSourceAttributes,
248
+ stripSourceAttributes,
249
+ TOOL_IO_VALUE_MAX_BYTES,
250
+ TOOL_CALL_LEVEL_ATTR,
251
+ INNER_TOOL_CALL_LEVEL,
252
+ isInnerToolCall,
253
+ canonicalJson,
254
+ boundedString,
255
+ toolIoAttributes,
256
+ toolArgumentsFromAttributes,
257
+ normalizeToolIoAttributes,
258
+ recordToolOutput,
259
+ parseIsoToEpochMs,
260
+ parseSince,
261
+ relativeBoundMs,
262
+ parseTimeBound
263
+ };