@bli-cockpit/telemetry-core 0.1.97 → 0.1.98

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,125 @@
1
+ /** Explicit host evidence, never a counterfeit native Codex response or usage record. */
2
+ import { createHash } from "node:crypto";
3
+ const hash = (text) => createHash("sha256").update(text).digest("hex");
4
+ const isHash = (v) => typeof v === "string" && /^[a-f0-9]{64}$/u.test(v);
5
+ const object = (v) => v && typeof v === "object" && !Array.isArray(v) ? v : {};
6
+ /** Decode only the observed assistant projection; unknown tags/roles remain raw evidence. */
7
+ export function readT3ProjectionMessage(record) {
8
+ if (record["type"] !== "tower_t3_message")
9
+ return null;
10
+ const p = object(record["payload"]);
11
+ if (p["schema_version"] !== "t3-message.v1" || p["role"] !== "assistant" ||
12
+ p["provenance"] !== "t3-0.1.7:orchestration_v2_projection_messages" || !isHash(p["text_sha256"]) || typeof p["text"] !== "string")
13
+ return null;
14
+ for (const key of ["native_session_id", "native_turn_id", "native_item_id", "t3_thread_id", "t3_message_id", "created_at", "updated_at"]) {
15
+ if (typeof p[key] !== "string" || !p[key])
16
+ return null;
17
+ }
18
+ if (!Number.isFinite(Date.parse(p["created_at"])) || !Number.isFinite(Date.parse(p["updated_at"])))
19
+ return null;
20
+ // Never validate source hash against masked text: the sanitizer legitimately changes it.
21
+ const decoded = { ...p };
22
+ const copy = object(p["native_copy"]);
23
+ if (!isHash(copy["record_sha256"]) || copy["text_sha256"] !== p["text_sha256"])
24
+ delete decoded.native_copy;
25
+ return decoded;
26
+ }
27
+ /** Match only native IDs and exact source text. Missing IDs never imply a copy. */
28
+ export function createNativeT3MessageIndex() {
29
+ let sessionId = null, turnId = null;
30
+ const copies = new Map();
31
+ const cwds = new Map();
32
+ let cwd = null;
33
+ let metadataBytes = 0;
34
+ const reserve = (key) => {
35
+ metadataBytes += Buffer.byteLength(key) + 64;
36
+ if (copies.size + cwds.size >= 100_000 || metadataBytes > 16 * 1024 * 1024)
37
+ throw new Error("t3_native_identity_index_capped");
38
+ };
39
+ const key = (session, turn, item, textHash) => JSON.stringify([session, turn, item, textHash]);
40
+ return {
41
+ observe(record, recordHash) {
42
+ const p = object(record["payload"]);
43
+ if (record["type"] === "session_meta") {
44
+ sessionId = typeof p["id"] === "string" ? p["id"] : null;
45
+ turnId = null;
46
+ cwd = typeof p["cwd"] === "string" ? p["cwd"] : null;
47
+ }
48
+ if (record["type"] === "turn_context") {
49
+ if (typeof p["turn_id"] === "string")
50
+ turnId = p["turn_id"];
51
+ if (typeof p["cwd"] === "string")
52
+ cwd = p["cwd"];
53
+ }
54
+ if (record["type"] === "event_msg" && p["type"] === "task_started")
55
+ turnId = typeof p["turn_id"] === "string" ? p["turn_id"] : null;
56
+ const explicitTurn = typeof p["turn_id"] === "string" ? p["turn_id"] : turnId;
57
+ if (sessionId && explicitTurn && cwd) {
58
+ const k = JSON.stringify([sessionId, explicitTurn]), seen = cwds.get(k) ?? new Set();
59
+ if (!seen.has(cwd)) {
60
+ reserve(k + cwd);
61
+ seen.add(cwd);
62
+ }
63
+ cwds.set(k, seen);
64
+ }
65
+ if (sessionId && explicitTurn && record["type"] === "response_item" && p["type"] === "message" && p["role"] === "assistant" && typeof p["id"] === "string" && Array.isArray(p["content"])) {
66
+ const blocks = p["content"].map(object);
67
+ // Unknown/non-text blocks cannot prove a text copy.
68
+ if (blocks.every(b => typeof b["text"] === "string")) {
69
+ const text = blocks.map(b => b["text"]).join("\n");
70
+ const identity = key(sessionId, explicitTurn, p["id"], hash(text));
71
+ if (!copies.has(identity))
72
+ reserve(identity);
73
+ copies.set(identity, recordHash);
74
+ }
75
+ }
76
+ if (record["type"] === "event_msg" && ["task_complete", "turn_aborted"].includes(String(p["type"])))
77
+ turnId = null;
78
+ },
79
+ copyOf(message) {
80
+ const recordHash = copies.get(key(message.native_session_id, message.native_turn_id, message.native_item_id, message.text_sha256));
81
+ return recordHash ? { record_sha256: recordHash, text_sha256: message.text_sha256 } : undefined;
82
+ },
83
+ turnCwds(message) {
84
+ return [...(cwds.get(JSON.stringify([message.native_session_id, message.native_turn_id])) ?? [null])];
85
+ },
86
+ };
87
+ }
88
+ /** Consumers use this after their native-copy selector; divergent source revisions stay distinct. */
89
+ export function t3ProjectionMessageSelector() {
90
+ const seen = new Set();
91
+ return record => {
92
+ if (record["type"] !== "tower_t3_message")
93
+ return true;
94
+ const message = readT3ProjectionMessage(record);
95
+ if (!message)
96
+ return false;
97
+ const identity = JSON.stringify([message.native_session_id, message.native_turn_id, message.native_item_id,
98
+ message.t3_thread_id, message.t3_message_id, message.text_sha256]);
99
+ if (seen.has(identity))
100
+ return false;
101
+ seen.add(identity);
102
+ return !message.native_copy;
103
+ };
104
+ }
105
+ /**
106
+ * Insert a late host supplement into a candidate view, never into retained bytes or native tool order.
107
+ * Native order stays authoritative. Missing clocks and ties place the supplement before native candidates,
108
+ * so an appended historical omission cannot displace a native closing response on an invented clock.
109
+ */
110
+ export function placeLateT3Projection(entries, candidate) {
111
+ if (!candidate.t3Projection) {
112
+ entries.push(candidate);
113
+ return;
114
+ }
115
+ const at = candidate.occurredAt === null ? NaN : Date.parse(candidate.occurredAt);
116
+ const before = entries.findIndex(entry => {
117
+ const existing = entry.occurredAt === null ? NaN : Date.parse(entry.occurredAt);
118
+ if (!Number.isFinite(at))
119
+ return !entry.t3Projection;
120
+ if (!Number.isFinite(existing))
121
+ return !entry.t3Projection;
122
+ return existing > at || (existing === at && !entry.t3Projection);
123
+ });
124
+ entries.splice(before < 0 ? entries.length : before, 0, candidate);
125
+ }
@@ -20,8 +20,8 @@
20
20
  * in. So every parser that read the line before still reads it.
21
21
  */
22
22
  import { createHash } from "node:crypto";
23
- import { IMAGE_MIN_BASE64_CHARS, formatImageMarker, sniffImageMediaType, } from "./image-descriptions.js";
24
- const BASE64_RUN = `[A-Za-z0-9+/=]{${IMAGE_MIN_BASE64_CHARS},}`;
23
+ import { IMAGE_MIN_BASE64_CHARS, IMAGE_DATA_URL_MIN_BASE64_CHARS, formatImageMarker, sniffImageMediaType, } from "./image-descriptions.js";
24
+ const BASE64_RUN = `[A-Za-z0-9+/=]{${IMAGE_DATA_URL_MIN_BASE64_CHARS},}`;
25
25
  /**
26
26
  * What the first characters of a base64 run look like for each type we accept: PNG `iVBOR`, JPEG
27
27
  * `/9j/`, GIF `R0lGO`, WebP (RIFF) `UklGR`. Anchoring the whole-string form on these keeps the scan
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bli-cockpit/telemetry-core",
3
- "version": "0.1.97",
3
+ "version": "0.1.98",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",