@bli-cockpit/telemetry-core 0.1.96 → 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.
- package/dist/agent-artifacts.d.ts +3 -0
- package/dist/agent-artifacts.js +1 -1
- package/dist/codex-record-copies.d.ts +7 -0
- package/dist/codex-record-copies.js +58 -0
- package/dist/evidence-completeness.d.ts +2 -2
- package/dist/evidence-rules-fingerprint.js +2 -1
- package/dist/evidence-stubs.d.ts +1 -1
- package/dist/evidence-stubs.js +2 -2
- package/dist/evidence-transcripts.d.ts +1 -1
- package/dist/evidence-transcripts.js +2 -2
- package/dist/evidence-upload.d.ts +72 -0
- package/dist/evidence-upload.js +1 -1
- package/dist/image-byte-scan.js +5 -5
- package/dist/image-descriptions.d.ts +4 -1
- package/dist/image-descriptions.js +3 -1
- package/dist/image-store.d.ts +1 -0
- package/dist/image-store.js +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/ingest-dto.d.ts +2 -2
- package/dist/local-config.d.ts +1 -1
- package/dist/opencode-transcript.d.ts +9 -0
- package/dist/opencode-transcript.js +97 -0
- package/dist/pi-transcript-extract.d.ts +58 -0
- package/dist/pi-transcript-extract.js +56 -0
- package/dist/pi-transcript-lineage.d.ts +14 -0
- package/dist/pi-transcript-lineage.js +20 -0
- package/dist/pi-transcript-usage.d.ts +27 -0
- package/dist/pi-transcript-usage.js +31 -0
- package/dist/pi-transcript.d.ts +38 -0
- package/dist/pi-transcript.js +105 -0
- package/dist/session-facts-delta.d.ts +3 -0
- package/dist/session-facts-delta.js +11 -0
- package/dist/session-facts-hands-on.d.ts +98 -0
- package/dist/session-facts-hands-on.js +208 -0
- package/dist/session-facts-schema.d.ts +109 -0
- package/dist/session-facts-schema.js +63 -0
- package/dist/session-facts-spawn.d.ts +2 -1
- package/dist/session-facts-spawn.js +3 -1
- package/dist/session-facts-usage-math.d.ts +6 -0
- package/dist/session-facts-usage-math.js +27 -0
- package/dist/session-facts.d.ts +24 -108
- package/dist/session-facts.js +38 -88
- package/dist/shelf.d.ts +28 -16
- package/dist/shelf.js +18 -8
- package/dist/t3-projection-message.d.ts +39 -0
- package/dist/t3-projection-message.js +125 -0
- package/dist/transcript-images.js +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hands-on time estimate (BLI-23623, rule `hands_on.v1`): an ESTIMATE of how long a person was engaged with a
|
|
3
|
+
* session, computed only from the timestamps of the messages THEY sent. It does not measure attention and makes
|
|
4
|
+
* no accuracy claim until it is compared with independently timed real activity.
|
|
5
|
+
*
|
|
6
|
+
* The rule, immutable per rule id:
|
|
7
|
+
* - Every confirmed human event (a Claude prompt `uuid`, a Codex human root `turn_id`) credits the fixed window
|
|
8
|
+
* [t - 3 min, t + 5 min]. Windows do not depend on what the agent did in between.
|
|
9
|
+
* - Windows are unioned inside a session; the server unions again across a person's sessions after deduping
|
|
10
|
+
* replayed events by identity (forks and resumes copy history).
|
|
11
|
+
*
|
|
12
|
+
* Classification is provisional ("v1") until validated on captured real records of known actions. Precedence:
|
|
13
|
+
* Claude: (0) compaction summary: not human, whatever tags it carries (machine-written);
|
|
14
|
+
* (1) `origin.kind` or `turnOrigin` == human: human;
|
|
15
|
+
* (2) `promptSource` typed | queued | suggestion_accepted: human;
|
|
16
|
+
* (3) task-notification / peer / scheduled / system / coordinator origin, isMeta, isSidechain,
|
|
17
|
+
* tool_result-only: not human;
|
|
18
|
+
* (4) anything else (untagged records of an older client, `sdk`): UNKNOWN. Counted in coverage, never
|
|
19
|
+
* silently treated as "no human".
|
|
20
|
+
* Codex: `task_started.turn_attribution.turn_trigger` user | composer | queue with a null `parent_turn_id` and
|
|
21
|
+
* a null `initiating_agent_path`: human; exec | guardian_review (or a nested turn): not human; missing
|
|
22
|
+
* attribution or any other trigger: unknown.
|
|
23
|
+
*
|
|
24
|
+
* Pure. Metadata out: event ids, timestamps, counts. No text is returned.
|
|
25
|
+
*/
|
|
26
|
+
import { z } from "zod";
|
|
27
|
+
export const HANDS_ON_RULE_ID = "hands_on.v1";
|
|
28
|
+
export const HANDS_ON_BEFORE_MS = 3 * 60 * 1000;
|
|
29
|
+
export const HANDS_ON_AFTER_MS = 5 * 60 * 1000;
|
|
30
|
+
/** One session reports at most this many human events; `events_truncated` says how many were dropped. */
|
|
31
|
+
export const HANDS_ON_MAX_EVENTS = 10_000;
|
|
32
|
+
const MAX_EVENT_ID_CHARS = 80;
|
|
33
|
+
const HUMAN_PROMPT_SOURCES = new Set(["typed", "queued", "suggestion_accepted"]);
|
|
34
|
+
const NOT_HUMAN_TURN_ORIGINS = new Set(["task_notification", "peer", "scheduled", "system"]);
|
|
35
|
+
const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
36
|
+
const str = (v) => (typeof v === "string" && v ? v : null);
|
|
37
|
+
/** Is this user record's content only tool results (the agent's tool output echoed back)? */
|
|
38
|
+
function toolResultOnly(record) {
|
|
39
|
+
if (record["toolUseResult"] !== undefined)
|
|
40
|
+
return true;
|
|
41
|
+
const message = record["message"];
|
|
42
|
+
const content = isObj(message) ? message["content"] : undefined;
|
|
43
|
+
return Array.isArray(content) && content.length > 0 && content.every((b) => isObj(b) && b["type"] === "tool_result");
|
|
44
|
+
}
|
|
45
|
+
/** Decide whether one Claude `type:"user"` record is a message the person sent. */
|
|
46
|
+
export function classifyClaudeUserRecord(record) {
|
|
47
|
+
if (record["isCompactSummary"] === true)
|
|
48
|
+
return { label: "not_human", reason: "compact_summary" };
|
|
49
|
+
const origin = isObj(record["origin"]) ? str(record["origin"]["kind"]) : null;
|
|
50
|
+
const turnOrigin = str(record["turnOrigin"]);
|
|
51
|
+
if (origin === "human")
|
|
52
|
+
return { label: "human", reason: "origin_human" };
|
|
53
|
+
if (turnOrigin === "human")
|
|
54
|
+
return { label: "human", reason: "turn_origin_human" };
|
|
55
|
+
const promptSource = str(record["promptSource"]);
|
|
56
|
+
if (promptSource && HUMAN_PROMPT_SOURCES.has(promptSource))
|
|
57
|
+
return { label: "human", reason: "prompt_source_human" };
|
|
58
|
+
if (origin)
|
|
59
|
+
return { label: "not_human", reason: "origin_" + origin.slice(0, 30) };
|
|
60
|
+
if (turnOrigin && NOT_HUMAN_TURN_ORIGINS.has(turnOrigin))
|
|
61
|
+
return { label: "not_human", reason: "turn_origin_" + turnOrigin };
|
|
62
|
+
if (promptSource === "system")
|
|
63
|
+
return { label: "not_human", reason: "prompt_source_system" };
|
|
64
|
+
if (record["isMeta"] === true)
|
|
65
|
+
return { label: "not_human", reason: "meta" };
|
|
66
|
+
if (record["isSidechain"] === true)
|
|
67
|
+
return { label: "not_human", reason: "sidechain" };
|
|
68
|
+
if (toolResultOnly(record))
|
|
69
|
+
return { label: "not_human", reason: "tool_result_only" };
|
|
70
|
+
return { label: "unknown", reason: "untagged" };
|
|
71
|
+
}
|
|
72
|
+
const HUMAN_CODEX_TRIGGERS = new Set(["user", "composer", "queue"]);
|
|
73
|
+
const MACHINE_CODEX_TRIGGERS = new Set(["exec", "guardian_review"]);
|
|
74
|
+
/** Decide whether one Codex `task_started` payload opened a turn the person started. */
|
|
75
|
+
export function classifyCodexTaskStarted(payload) {
|
|
76
|
+
const attribution = payload["turn_attribution"];
|
|
77
|
+
if (!isObj(attribution))
|
|
78
|
+
return { label: "unknown", reason: "no_attribution" };
|
|
79
|
+
const trigger = str(attribution["turn_trigger"]);
|
|
80
|
+
if (!trigger)
|
|
81
|
+
return { label: "unknown", reason: "no_trigger" };
|
|
82
|
+
if (MACHINE_CODEX_TRIGGERS.has(trigger))
|
|
83
|
+
return { label: "not_human", reason: "trigger_" + trigger };
|
|
84
|
+
if (HUMAN_CODEX_TRIGGERS.has(trigger)) {
|
|
85
|
+
const nested = str(attribution["parent_turn_id"]) !== null || str(attribution["initiating_agent_path"]) !== null;
|
|
86
|
+
return nested ? { label: "not_human", reason: "nested_turn" } : { label: "human", reason: "trigger_" + trigger };
|
|
87
|
+
}
|
|
88
|
+
return { label: "unknown", reason: "trigger_other" };
|
|
89
|
+
}
|
|
90
|
+
/** Codex's identity for a human event: the root turn id. */
|
|
91
|
+
export function codexTurnId(payload) {
|
|
92
|
+
const attribution = isObj(payload["turn_attribution"]) ? payload["turn_attribution"] : {};
|
|
93
|
+
return str(attribution["turn_id"]) ?? str(payload["turn_id"]);
|
|
94
|
+
}
|
|
95
|
+
/** Merge `[start, end]` ms intervals (sorted by start, touching intervals join). */
|
|
96
|
+
export function mergeIntervals(intervals) {
|
|
97
|
+
const sorted = [...intervals].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
98
|
+
const out = [];
|
|
99
|
+
for (const [start, end] of sorted) {
|
|
100
|
+
const last = out[out.length - 1];
|
|
101
|
+
if (last && start <= last[1])
|
|
102
|
+
last[1] = Math.max(last[1], end);
|
|
103
|
+
else
|
|
104
|
+
out.push([start, end]);
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
export function handsOnWindow(tsMs) {
|
|
109
|
+
return [tsMs - HANDS_ON_BEFORE_MS, tsMs + HANDS_ON_AFTER_MS];
|
|
110
|
+
}
|
|
111
|
+
export function intervalsMs(intervals) {
|
|
112
|
+
return intervals.reduce((sum, [start, end]) => sum + Math.max(0, end - start), 0);
|
|
113
|
+
}
|
|
114
|
+
export function emptyHandsOnDelta() {
|
|
115
|
+
return { events: [], human_count: 0, not_human_count: 0, unknown_count: 0, turn_starts: 0 };
|
|
116
|
+
}
|
|
117
|
+
/** Collects a session's classified events record by record. */
|
|
118
|
+
export class HandsOnTally {
|
|
119
|
+
byId = new Map();
|
|
120
|
+
human = 0;
|
|
121
|
+
notHuman = 0;
|
|
122
|
+
unknown = 0;
|
|
123
|
+
turnStarts = 0;
|
|
124
|
+
/** Count one classified record. A human record with no usable timestamp cannot credit a window, so it counts as unknown. */
|
|
125
|
+
observe(classification, id, tsMs) {
|
|
126
|
+
if (classification.label === "not_human")
|
|
127
|
+
return void (this.notHuman += 1);
|
|
128
|
+
if (classification.label === "unknown")
|
|
129
|
+
return void (this.unknown += 1);
|
|
130
|
+
if (tsMs == null)
|
|
131
|
+
return void (this.unknown += 1);
|
|
132
|
+
this.human += 1;
|
|
133
|
+
// A record with no identity gets one from its timestamp: replays of it dedupe, two distinct ones in a ms collapse.
|
|
134
|
+
const key = (id ?? "ts-" + tsMs).slice(0, MAX_EVENT_ID_CHARS);
|
|
135
|
+
const seen = this.byId.get(key);
|
|
136
|
+
if (seen === undefined || tsMs < seen)
|
|
137
|
+
this.byId.set(key, tsMs);
|
|
138
|
+
}
|
|
139
|
+
mergeDelta(delta) {
|
|
140
|
+
this.human += delta.human_count;
|
|
141
|
+
this.notHuman += delta.not_human_count;
|
|
142
|
+
this.unknown += delta.unknown_count;
|
|
143
|
+
this.turnStarts += delta.turn_starts;
|
|
144
|
+
for (const [id, ts] of delta.events) {
|
|
145
|
+
const seen = this.byId.get(id);
|
|
146
|
+
if (seen === undefined || ts < seen)
|
|
147
|
+
this.byId.set(id, ts);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
toDelta() {
|
|
151
|
+
return {
|
|
152
|
+
events: [...this.byId.entries()].sort((a, b) => a[1] - b[1] || a[0].localeCompare(b[0])),
|
|
153
|
+
human_count: this.human,
|
|
154
|
+
not_human_count: this.notHuman,
|
|
155
|
+
unknown_count: this.unknown,
|
|
156
|
+
turn_starts: this.turnStarts,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
export const HandsOnFactsSchema = z
|
|
161
|
+
.object({
|
|
162
|
+
rule_id: z.literal(HANDS_ON_RULE_ID),
|
|
163
|
+
seconds: z.number().int().nonnegative(),
|
|
164
|
+
events: z.array(z.tuple([z.string().min(1).max(MAX_EVENT_ID_CHARS), z.number().int()])).max(HANDS_ON_MAX_EVENTS),
|
|
165
|
+
intervals: z.array(z.tuple([z.number().int(), z.number().int()])).max(HANDS_ON_MAX_EVENTS),
|
|
166
|
+
human_count: z.number().int().nonnegative(),
|
|
167
|
+
not_human_count: z.number().int().nonnegative(),
|
|
168
|
+
unknown_count: z.number().int().nonnegative(),
|
|
169
|
+
events_truncated: z.number().int().nonnegative(),
|
|
170
|
+
})
|
|
171
|
+
.strict();
|
|
172
|
+
/**
|
|
173
|
+
* The finished answer. `codexUserPrompts` lets a Codex file with no `task_started` records at all (an older
|
|
174
|
+
* client) report its prompts as unknown instead of reporting nothing.
|
|
175
|
+
*/
|
|
176
|
+
export function finalizeHandsOn(tally, codexUserPrompts) {
|
|
177
|
+
const delta = tally.toDelta();
|
|
178
|
+
const unknown = delta.unknown_count + (delta.turn_starts === 0 && codexUserPrompts > 0 && delta.human_count === 0 ? codexUserPrompts : 0);
|
|
179
|
+
const intervals = mergeIntervals(delta.events.map(([, ts]) => handsOnWindow(ts)));
|
|
180
|
+
const kept = delta.events.slice(0, HANDS_ON_MAX_EVENTS);
|
|
181
|
+
return {
|
|
182
|
+
rule_id: HANDS_ON_RULE_ID,
|
|
183
|
+
seconds: Math.round(intervalsMs(intervals) / 1000),
|
|
184
|
+
events: kept,
|
|
185
|
+
intervals: intervals.slice(0, HANDS_ON_MAX_EVENTS),
|
|
186
|
+
human_count: delta.human_count,
|
|
187
|
+
not_human_count: delta.not_human_count,
|
|
188
|
+
unknown_count: unknown,
|
|
189
|
+
events_truncated: delta.events.length - kept.length,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/** Counts only, for one scan's log line: how many sessions carried the estimate and how the records classified. */
|
|
193
|
+
export function summarizeHandsOn(facts) {
|
|
194
|
+
const out = { rule_id: HANDS_ON_RULE_ID, sessions: 0, sessions_with_events: 0, human: 0, not_human: 0, unknown: 0, seconds: 0 };
|
|
195
|
+
for (const f of facts) {
|
|
196
|
+
const h = f?.hands_on;
|
|
197
|
+
if (!h)
|
|
198
|
+
continue;
|
|
199
|
+
out.sessions += 1;
|
|
200
|
+
if (h.events.length > 0)
|
|
201
|
+
out.sessions_with_events += 1;
|
|
202
|
+
out.human += h.human_count;
|
|
203
|
+
out.not_human += h.not_human_count;
|
|
204
|
+
out.unknown += h.unknown_count;
|
|
205
|
+
out.seconds += h.seconds;
|
|
206
|
+
}
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/** Wire validation and bounded cardinalities shared by collector and server facts. */
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export declare const SESSION_FACTS_MAX_MODELS = 50;
|
|
4
|
+
export declare const SESSION_FACTS_MAX_TOOL_NAMES = 200;
|
|
5
|
+
export declare const MAX_MODEL_NAME_CHARS = 200;
|
|
6
|
+
export declare const MAX_TOOL_NAME_CHARS = 120;
|
|
7
|
+
export declare const SessionFactsUsageSchema: z.ZodObject<{
|
|
8
|
+
input_tokens: z.ZodNumber;
|
|
9
|
+
output_tokens: z.ZodNumber;
|
|
10
|
+
cache_read_input_tokens: z.ZodNumber;
|
|
11
|
+
cache_creation_input_tokens: z.ZodNumber;
|
|
12
|
+
}, z.core.$strict>;
|
|
13
|
+
/**
|
|
14
|
+
* The wire shape. Optional wherever it rides an envelope: a CLI published
|
|
15
|
+
* before this change sends no block at all, and its uploads must still
|
|
16
|
+
* validate.
|
|
17
|
+
*/
|
|
18
|
+
export declare const SessionFactsSchema: z.ZodObject<{
|
|
19
|
+
extractor_version: z.ZodString;
|
|
20
|
+
source: z.ZodEnum<{
|
|
21
|
+
codex: "codex";
|
|
22
|
+
claude_code: "claude_code";
|
|
23
|
+
opencode: "opencode";
|
|
24
|
+
}>;
|
|
25
|
+
usage_accounting: z.ZodOptional<z.ZodEnum<{
|
|
26
|
+
complete: "complete";
|
|
27
|
+
partial: "partial";
|
|
28
|
+
unavailable: "unavailable";
|
|
29
|
+
}>>;
|
|
30
|
+
usage_identity_scope: z.ZodOptional<z.ZodEnum<{
|
|
31
|
+
machine: "machine";
|
|
32
|
+
operator: "operator";
|
|
33
|
+
}>>;
|
|
34
|
+
usage_claims: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
35
|
+
identity: z.ZodString;
|
|
36
|
+
model: z.ZodString;
|
|
37
|
+
usage: z.ZodObject<{
|
|
38
|
+
input_tokens: z.ZodNumber;
|
|
39
|
+
output_tokens: z.ZodNumber;
|
|
40
|
+
cache_read_input_tokens: z.ZodNumber;
|
|
41
|
+
cache_creation_input_tokens: z.ZodNumber;
|
|
42
|
+
}, z.core.$strict>;
|
|
43
|
+
}, z.core.$strict>>>;
|
|
44
|
+
unknown_usage_records: z.ZodOptional<z.ZodNumber>;
|
|
45
|
+
copied_usage_records: z.ZodOptional<z.ZodNumber>;
|
|
46
|
+
usage_by_model: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
47
|
+
input_tokens: z.ZodNumber;
|
|
48
|
+
output_tokens: z.ZodNumber;
|
|
49
|
+
cache_read_input_tokens: z.ZodNumber;
|
|
50
|
+
cache_creation_input_tokens: z.ZodNumber;
|
|
51
|
+
}, z.core.$strict>>;
|
|
52
|
+
models: z.ZodArray<z.ZodString>;
|
|
53
|
+
input_tokens: z.ZodNumber;
|
|
54
|
+
output_tokens: z.ZodNumber;
|
|
55
|
+
cache_read_tokens: z.ZodNumber;
|
|
56
|
+
cache_creation_tokens: z.ZodNumber;
|
|
57
|
+
total_tokens: z.ZodNumber;
|
|
58
|
+
record_count: z.ZodNumber;
|
|
59
|
+
turn_count: z.ZodNumber;
|
|
60
|
+
user_prompt_count: z.ZodNumber;
|
|
61
|
+
assistant_response_count: z.ZodNumber;
|
|
62
|
+
tool_calls_by_name: z.ZodRecord<z.ZodString, z.ZodNumber>;
|
|
63
|
+
tool_call_count: z.ZodNumber;
|
|
64
|
+
first_timestamp: z.ZodNullable<z.ZodString>;
|
|
65
|
+
last_timestamp: z.ZodNullable<z.ZodString>;
|
|
66
|
+
wall_duration_seconds: z.ZodNullable<z.ZodNumber>;
|
|
67
|
+
active_seconds: z.ZodOptional<z.ZodNumber>;
|
|
68
|
+
transcript_bytes: z.ZodNumber;
|
|
69
|
+
transcript_lines: z.ZodNumber;
|
|
70
|
+
transcript_files: z.ZodNumber;
|
|
71
|
+
truncated_models: z.ZodNumber;
|
|
72
|
+
truncated_tool_names: z.ZodNumber;
|
|
73
|
+
skill_use: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
74
|
+
skill: z.ZodString;
|
|
75
|
+
plugin: z.ZodNullable<z.ZodString>;
|
|
76
|
+
by_person: z.ZodNumber;
|
|
77
|
+
by_agent: z.ZodNumber;
|
|
78
|
+
}, z.core.$strict>>>;
|
|
79
|
+
spawn: z.ZodOptional<z.ZodObject<{
|
|
80
|
+
kind: z.ZodEnum<{
|
|
81
|
+
interactive: "interactive";
|
|
82
|
+
worker: "worker";
|
|
83
|
+
subagent: "subagent";
|
|
84
|
+
}>;
|
|
85
|
+
client_app: z.ZodOptional<z.ZodEnum<{
|
|
86
|
+
unknown: "unknown";
|
|
87
|
+
claude_cli: "claude_cli";
|
|
88
|
+
claude_desktop: "claude_desktop";
|
|
89
|
+
claude_ide: "claude_ide";
|
|
90
|
+
codex_cli: "codex_cli";
|
|
91
|
+
codex_desktop: "codex_desktop";
|
|
92
|
+
codex_ide: "codex_ide";
|
|
93
|
+
codex_exec: "codex_exec";
|
|
94
|
+
t3_code: "t3_code";
|
|
95
|
+
}>>;
|
|
96
|
+
parent_session_id: z.ZodOptional<z.ZodString>;
|
|
97
|
+
}, z.core.$strict>>;
|
|
98
|
+
hands_on: z.ZodOptional<z.ZodObject<{
|
|
99
|
+
rule_id: z.ZodLiteral<"hands_on.v1">;
|
|
100
|
+
seconds: z.ZodNumber;
|
|
101
|
+
events: z.ZodArray<z.ZodTuple<[z.ZodString, z.ZodNumber], null>>;
|
|
102
|
+
intervals: z.ZodArray<z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>>;
|
|
103
|
+
human_count: z.ZodNumber;
|
|
104
|
+
not_human_count: z.ZodNumber;
|
|
105
|
+
unknown_count: z.ZodNumber;
|
|
106
|
+
events_truncated: z.ZodNumber;
|
|
107
|
+
}, z.core.$strict>>;
|
|
108
|
+
}, z.core.$strict>;
|
|
109
|
+
export type SessionFactsWire = z.infer<typeof SessionFactsSchema>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/** Wire validation and bounded cardinalities shared by collector and server facts. */
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { HandsOnFactsSchema } from "./session-facts-hands-on.js";
|
|
4
|
+
import { SessionSpawnSchema } from "./session-facts-spawn.js";
|
|
5
|
+
import { SKILL_USE_MAX_SKILLS, SkillUseCountSchema } from "./session-facts-skills.js";
|
|
6
|
+
export const SESSION_FACTS_MAX_MODELS = 50;
|
|
7
|
+
export const SESSION_FACTS_MAX_TOOL_NAMES = 200;
|
|
8
|
+
export const MAX_MODEL_NAME_CHARS = 200;
|
|
9
|
+
export const MAX_TOOL_NAME_CHARS = 120;
|
|
10
|
+
const TokenCountSchema = z.number().int().nonnegative().max(1_000_000_000_000);
|
|
11
|
+
export const SessionFactsUsageSchema = z
|
|
12
|
+
.object({
|
|
13
|
+
input_tokens: TokenCountSchema,
|
|
14
|
+
output_tokens: TokenCountSchema,
|
|
15
|
+
cache_read_input_tokens: TokenCountSchema,
|
|
16
|
+
cache_creation_input_tokens: TokenCountSchema,
|
|
17
|
+
})
|
|
18
|
+
.strict();
|
|
19
|
+
/**
|
|
20
|
+
* The wire shape. Optional wherever it rides an envelope: a CLI published
|
|
21
|
+
* before this change sends no block at all, and its uploads must still
|
|
22
|
+
* validate.
|
|
23
|
+
*/
|
|
24
|
+
export const SessionFactsSchema = z
|
|
25
|
+
.object({
|
|
26
|
+
extractor_version: z.string().trim().min(1).max(40),
|
|
27
|
+
source: z.enum(["codex", "claude_code", "opencode"]),
|
|
28
|
+
usage_accounting: z.enum(["complete", "partial", "unavailable"]).optional(),
|
|
29
|
+
usage_identity_scope: z.enum(["machine", "operator"]).optional(),
|
|
30
|
+
usage_claims: z.array(z.object({ identity: z.string().regex(/^[a-f0-9]{64}$/), model: z.string().min(1).max(200), usage: z.object({ input_tokens: z.number().nonnegative(), output_tokens: z.number().nonnegative(), cache_read_input_tokens: z.number().nonnegative(), cache_creation_input_tokens: z.number().nonnegative() }).strict() }).strict()).max(1000).optional(),
|
|
31
|
+
unknown_usage_records: z.number().int().nonnegative().optional(),
|
|
32
|
+
copied_usage_records: z.number().int().nonnegative().optional(),
|
|
33
|
+
usage_by_model: z
|
|
34
|
+
.record(z.string().min(1).max(MAX_MODEL_NAME_CHARS), SessionFactsUsageSchema)
|
|
35
|
+
.refine((value) => Object.keys(value).length <= SESSION_FACTS_MAX_MODELS, { message: "too many models" }),
|
|
36
|
+
models: z.array(z.string().min(1).max(MAX_MODEL_NAME_CHARS)).max(SESSION_FACTS_MAX_MODELS),
|
|
37
|
+
input_tokens: TokenCountSchema,
|
|
38
|
+
output_tokens: TokenCountSchema,
|
|
39
|
+
cache_read_tokens: TokenCountSchema,
|
|
40
|
+
cache_creation_tokens: TokenCountSchema,
|
|
41
|
+
total_tokens: TokenCountSchema,
|
|
42
|
+
record_count: z.number().int().nonnegative(),
|
|
43
|
+
turn_count: z.number().int().nonnegative(),
|
|
44
|
+
user_prompt_count: z.number().int().nonnegative(),
|
|
45
|
+
assistant_response_count: z.number().int().nonnegative(),
|
|
46
|
+
tool_calls_by_name: z
|
|
47
|
+
.record(z.string().min(1).max(MAX_TOOL_NAME_CHARS), z.number().int().nonnegative())
|
|
48
|
+
.refine((value) => Object.keys(value).length <= SESSION_FACTS_MAX_TOOL_NAMES, { message: "too many tool names" }),
|
|
49
|
+
tool_call_count: z.number().int().nonnegative(),
|
|
50
|
+
first_timestamp: z.string().datetime({ offset: true }).nullable(),
|
|
51
|
+
last_timestamp: z.string().datetime({ offset: true }).nullable(),
|
|
52
|
+
wall_duration_seconds: z.number().int().nonnegative().nullable(),
|
|
53
|
+
active_seconds: z.number().int().nonnegative().optional(),
|
|
54
|
+
transcript_bytes: z.number().int().nonnegative(),
|
|
55
|
+
transcript_lines: z.number().int().nonnegative(),
|
|
56
|
+
transcript_files: z.number().int().nonnegative(),
|
|
57
|
+
truncated_models: z.number().int().nonnegative(),
|
|
58
|
+
truncated_tool_names: z.number().int().nonnegative(),
|
|
59
|
+
skill_use: z.array(SkillUseCountSchema).max(SKILL_USE_MAX_SKILLS).optional(),
|
|
60
|
+
spawn: SessionSpawnSchema.optional(),
|
|
61
|
+
hands_on: HandsOnFactsSchema.optional(),
|
|
62
|
+
})
|
|
63
|
+
.strict();
|
|
@@ -17,7 +17,7 @@ export type SessionSpawnKind = "interactive" | "worker" | "subagent";
|
|
|
17
17
|
* `Claude Code`) and `source` (`exec`, `cli`, `vscode`, or `{subagent: ...}`); Claude Code `entrypoint` (`cli`,
|
|
18
18
|
* `sdk-cli`, `sdk-ts`, `claude-desktop`). A value nobody has observed is `unknown`, never a guess.
|
|
19
19
|
*/
|
|
20
|
-
export declare const CLIENT_APPS: readonly ["claude_cli", "claude_desktop", "claude_ide", "codex_cli", "codex_desktop", "codex_ide", "codex_exec", "unknown"];
|
|
20
|
+
export declare const CLIENT_APPS: readonly ["claude_cli", "claude_desktop", "claude_ide", "codex_cli", "codex_desktop", "codex_ide", "codex_exec", "t3_code", "unknown"];
|
|
21
21
|
export type ClientApp = (typeof CLIENT_APPS)[number];
|
|
22
22
|
export interface SessionSpawn {
|
|
23
23
|
kind: SessionSpawnKind;
|
|
@@ -41,6 +41,7 @@ export declare const SessionSpawnSchema: z.ZodObject<{
|
|
|
41
41
|
codex_desktop: "codex_desktop";
|
|
42
42
|
codex_ide: "codex_ide";
|
|
43
43
|
codex_exec: "codex_exec";
|
|
44
|
+
t3_code: "t3_code";
|
|
44
45
|
}>>;
|
|
45
46
|
parent_session_id: z.ZodOptional<z.ZodString>;
|
|
46
47
|
}, z.core.$strict>;
|
|
@@ -15,7 +15,7 @@ import { z } from "zod";
|
|
|
15
15
|
* `Claude Code`) and `source` (`exec`, `cli`, `vscode`, or `{subagent: ...}`); Claude Code `entrypoint` (`cli`,
|
|
16
16
|
* `sdk-cli`, `sdk-ts`, `claude-desktop`). A value nobody has observed is `unknown`, never a guess.
|
|
17
17
|
*/
|
|
18
|
-
export const CLIENT_APPS = ["claude_cli", "claude_desktop", "claude_ide", "codex_cli", "codex_desktop", "codex_ide", "codex_exec", "unknown"];
|
|
18
|
+
export const CLIENT_APPS = ["claude_cli", "claude_desktop", "claude_ide", "codex_cli", "codex_desktop", "codex_ide", "codex_exec", "t3_code", "unknown"];
|
|
19
19
|
const SPAWN_ID = /^[A-Za-z0-9._-]{1,80}$/u;
|
|
20
20
|
const spawnId = (value) => typeof value === "string" && SPAWN_ID.test(value) ? value : undefined;
|
|
21
21
|
export const SessionSpawnSchema = z
|
|
@@ -88,6 +88,8 @@ export function codexSpawn(current, meta) {
|
|
|
88
88
|
* run, not a Codex client a person used, so it is `unknown`.
|
|
89
89
|
*/
|
|
90
90
|
export function codexClientApp(originator, source) {
|
|
91
|
+
if (originator === "T3 Code")
|
|
92
|
+
return "t3_code";
|
|
91
93
|
if (originator === "codex_exec")
|
|
92
94
|
return "codex_exec";
|
|
93
95
|
if (originator === "Codex Desktop" || originator === "codex_work_desktop")
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Small pure helpers of `session-facts.ts` (moved out so that file stays under the readability band, BLI-23623). */
|
|
2
|
+
import type { SessionFactsUsage } from "./session-facts.js";
|
|
3
|
+
export declare function usageTotal(usage: SessionFactsUsage): number;
|
|
4
|
+
export declare function emptyUsage(): SessionFactsUsage;
|
|
5
|
+
export declare function sumUsage(usages: readonly SessionFactsUsage[]): SessionFactsUsage;
|
|
6
|
+
export declare function byteLength(value: string): number;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function usageTotal(usage) {
|
|
2
|
+
return (usage.input_tokens +
|
|
3
|
+
usage.output_tokens +
|
|
4
|
+
usage.cache_read_input_tokens +
|
|
5
|
+
usage.cache_creation_input_tokens);
|
|
6
|
+
}
|
|
7
|
+
export function emptyUsage() {
|
|
8
|
+
return {
|
|
9
|
+
input_tokens: 0,
|
|
10
|
+
output_tokens: 0,
|
|
11
|
+
cache_read_input_tokens: 0,
|
|
12
|
+
cache_creation_input_tokens: 0,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function sumUsage(usages) {
|
|
16
|
+
return usages.reduce((sum, usage) => ({
|
|
17
|
+
input_tokens: sum.input_tokens + usage.input_tokens,
|
|
18
|
+
output_tokens: sum.output_tokens + usage.output_tokens,
|
|
19
|
+
cache_read_input_tokens: sum.cache_read_input_tokens + usage.cache_read_input_tokens,
|
|
20
|
+
cache_creation_input_tokens: sum.cache_creation_input_tokens + usage.cache_creation_input_tokens,
|
|
21
|
+
}), emptyUsage());
|
|
22
|
+
}
|
|
23
|
+
export function byteLength(value) {
|
|
24
|
+
return typeof Buffer === "undefined"
|
|
25
|
+
? new TextEncoder().encode(value).length
|
|
26
|
+
: Buffer.byteLength(value, "utf8");
|
|
27
|
+
}
|
package/dist/session-facts.d.ts
CHANGED
|
@@ -1,56 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* where the file already is.
|
|
4
|
-
*
|
|
5
|
-
* BLI-4341 part A. Token totals used to be computed only on the server, which downloaded the transcript back out of
|
|
6
|
-
* Storage and summed it (`det.v5`), though the collector holds the same bytes at harvest time and had already read
|
|
7
|
-
* every line; a session with no analysis run had no token count at all.
|
|
8
|
-
*
|
|
9
|
-
* This module is the ONE implementation of those rules. The collector feeds it
|
|
10
|
-
* at upload; the server extractor feeds it the same records during its own
|
|
11
|
-
* pass, so both sides count identically by construction rather than by review.
|
|
12
|
-
* `det.v6` is `det.v5`'s rules with one owner.
|
|
13
|
-
*
|
|
14
|
-
* Contract:
|
|
15
|
-
* - Pure. No filesystem, no network, no clock. Content in, numbers out.
|
|
16
|
-
* - METADATA ONLY on the way out: token counts, turn counts, tool NAMES and
|
|
17
|
-
* their counts, timestamps, byte/line sizes. Text is read in-process to
|
|
18
|
-
* decide whether a turn counted; it is never returned.
|
|
19
|
-
* - Two feeds, one rulebook: `observeSessionFactsRecord` for a caller that
|
|
20
|
-
* is already parsing the JSONL (the server extractor), and
|
|
21
|
-
* `computeSessionFacts` for a caller holding whole file contents (the
|
|
22
|
-
* collector).
|
|
23
|
-
*
|
|
24
|
-
* Counting rules, both verified against real transcripts:
|
|
25
|
-
* - Claude Code writes PER-TURN usage on each assistant record, repeated
|
|
26
|
-
* across the several records of one API request, so usage is summed with a
|
|
27
|
-
* dedupe on `message.id` (fallback `requestId`).
|
|
28
|
-
* - Codex writes a CUMULATIVE `total_token_usage` on every `token_count`
|
|
29
|
-
* event, so the last one wins, attributed to the `turn_context` model. Its
|
|
30
|
-
* `input_tokens` includes cached input and its `output_tokens` already
|
|
31
|
-
* includes reasoning, so input is normalized to fresh-only, matching the
|
|
32
|
-
* Anthropic-shaped usage the price table expects.
|
|
33
|
-
*/
|
|
34
|
-
import { z } from "zod";
|
|
1
|
+
export { SESSION_FACTS_MAX_MODELS, SESSION_FACTS_MAX_TOOL_NAMES } from "./session-facts-schema.js";
|
|
2
|
+
import { HandsOnTally, type HandsOnFacts } from "./session-facts-hands-on.js";
|
|
35
3
|
import { type SessionSpawn } from "./session-facts-spawn.js";
|
|
36
4
|
import { SkillUseTally } from "./session-facts-skills.js";
|
|
37
5
|
import type { SkillUseCount } from "./session-facts-skills.js";
|
|
38
6
|
/** Bump when a counting rule changes, never when a field is added. */
|
|
39
7
|
export declare const SESSION_FACTS_VERSION = "det.v6";
|
|
40
8
|
/** How many distinct models and tool names one session may report. */
|
|
41
|
-
export
|
|
42
|
-
export declare const SESSION_FACTS_MAX_TOOL_NAMES = 200;
|
|
43
|
-
export type SessionFactsSource = "codex" | "claude_code";
|
|
9
|
+
export type SessionFactsSource = "codex" | "claude_code" | "opencode";
|
|
44
10
|
export interface SessionFactsUsage {
|
|
45
11
|
input_tokens: number;
|
|
46
12
|
output_tokens: number;
|
|
47
13
|
cache_read_input_tokens: number;
|
|
48
14
|
cache_creation_input_tokens: number;
|
|
49
15
|
}
|
|
16
|
+
export interface NativeUsageClaim {
|
|
17
|
+
identity: string;
|
|
18
|
+
model: string;
|
|
19
|
+
usage: SessionFactsUsage;
|
|
20
|
+
}
|
|
50
21
|
export interface SessionFacts {
|
|
51
22
|
/** The rules that produced these numbers. */
|
|
52
23
|
extractor_version: string;
|
|
53
24
|
source: SessionFactsSource;
|
|
25
|
+
usage_accounting?: "complete" | "partial" | "unavailable";
|
|
26
|
+
usage_identity_scope?: "machine" | "operator";
|
|
27
|
+
usage_claims?: NativeUsageClaim[];
|
|
28
|
+
unknown_usage_records?: number;
|
|
29
|
+
copied_usage_records?: number;
|
|
54
30
|
usage_by_model: Record<string, SessionFactsUsage>;
|
|
55
31
|
models: string[];
|
|
56
32
|
input_tokens: number;
|
|
@@ -83,9 +59,16 @@ export interface SessionFacts {
|
|
|
83
59
|
skill_use?: SkillUseCount[];
|
|
84
60
|
/** BLI-13066: who started the session (`session-facts-spawn.ts`). Absent from a block a CLI published before this field. */
|
|
85
61
|
spawn?: SessionSpawn;
|
|
62
|
+
/** BLI-23623: hands-on estimate from the person's own message timestamps (`session-facts-hands-on.ts`). Absent from a block a CLI published before this field. */
|
|
63
|
+
hands_on?: HandsOnFacts;
|
|
86
64
|
}
|
|
87
65
|
export interface SessionFactsAccumulator {
|
|
66
|
+
opencode_usage_messages?: number;
|
|
67
|
+
opencode_usage_records?: number;
|
|
68
|
+
opencode_usage_claims?: Map<string, NativeUsageClaim>;
|
|
88
69
|
source: SessionFactsSource;
|
|
70
|
+
unknown_usage_records?: number;
|
|
71
|
+
copied_usage_records?: number;
|
|
89
72
|
usage_by_model: Record<string, SessionFactsUsage>;
|
|
90
73
|
seen_usage_ids: Set<string>;
|
|
91
74
|
record_count: number;
|
|
@@ -103,6 +86,7 @@ export interface SessionFactsAccumulator {
|
|
|
103
86
|
codex_model_hint: string | null;
|
|
104
87
|
skill_tally: SkillUseTally;
|
|
105
88
|
spawn: SessionSpawn | null;
|
|
89
|
+
hands_on: HandsOnTally;
|
|
106
90
|
/** Set only by `session-facts-delta.ts` while it captures one file's contribution (BLI-22797). */
|
|
107
91
|
usage_log?: Array<{
|
|
108
92
|
id: string;
|
|
@@ -117,7 +101,7 @@ export declare function observeSessionFactsFile(acc: SessionFactsAccumulator, fi
|
|
|
117
101
|
lines: number;
|
|
118
102
|
}): void;
|
|
119
103
|
/** One parsed JSONL record. The caller owns parsing; this owns the rules. */
|
|
120
|
-
export declare function observeSessionFactsRecord(acc: SessionFactsAccumulator, record: Record<string, unknown>):
|
|
104
|
+
export declare function observeSessionFactsRecord(acc: SessionFactsAccumulator, record: Record<string, unknown>): boolean;
|
|
121
105
|
export declare function addClaudeUsage(acc: SessionFactsAccumulator, model: string, usage: Record<string, unknown>): void;
|
|
122
106
|
/**
|
|
123
107
|
* OpenAI semantics differ from the Anthropic-shaped usage the price table
|
|
@@ -145,74 +129,6 @@ export declare function computeSessionFacts(options: {
|
|
|
145
129
|
source: SessionFactsSource;
|
|
146
130
|
contents: readonly string[];
|
|
147
131
|
}): SessionFacts;
|
|
148
|
-
export
|
|
149
|
-
|
|
150
|
-
output_tokens: z.ZodNumber;
|
|
151
|
-
cache_read_input_tokens: z.ZodNumber;
|
|
152
|
-
cache_creation_input_tokens: z.ZodNumber;
|
|
153
|
-
}, z.core.$strict>;
|
|
154
|
-
/**
|
|
155
|
-
* The wire shape. Optional wherever it rides an envelope: a CLI published
|
|
156
|
-
* before this change sends no block at all, and its uploads must still
|
|
157
|
-
* validate.
|
|
158
|
-
*/
|
|
159
|
-
export declare const SessionFactsSchema: z.ZodObject<{
|
|
160
|
-
extractor_version: z.ZodString;
|
|
161
|
-
source: z.ZodEnum<{
|
|
162
|
-
codex: "codex";
|
|
163
|
-
claude_code: "claude_code";
|
|
164
|
-
}>;
|
|
165
|
-
usage_by_model: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
166
|
-
input_tokens: z.ZodNumber;
|
|
167
|
-
output_tokens: z.ZodNumber;
|
|
168
|
-
cache_read_input_tokens: z.ZodNumber;
|
|
169
|
-
cache_creation_input_tokens: z.ZodNumber;
|
|
170
|
-
}, z.core.$strict>>;
|
|
171
|
-
models: z.ZodArray<z.ZodString>;
|
|
172
|
-
input_tokens: z.ZodNumber;
|
|
173
|
-
output_tokens: z.ZodNumber;
|
|
174
|
-
cache_read_tokens: z.ZodNumber;
|
|
175
|
-
cache_creation_tokens: z.ZodNumber;
|
|
176
|
-
total_tokens: z.ZodNumber;
|
|
177
|
-
record_count: z.ZodNumber;
|
|
178
|
-
turn_count: z.ZodNumber;
|
|
179
|
-
user_prompt_count: z.ZodNumber;
|
|
180
|
-
assistant_response_count: z.ZodNumber;
|
|
181
|
-
tool_calls_by_name: z.ZodRecord<z.ZodString, z.ZodNumber>;
|
|
182
|
-
tool_call_count: z.ZodNumber;
|
|
183
|
-
first_timestamp: z.ZodNullable<z.ZodString>;
|
|
184
|
-
last_timestamp: z.ZodNullable<z.ZodString>;
|
|
185
|
-
wall_duration_seconds: z.ZodNullable<z.ZodNumber>;
|
|
186
|
-
active_seconds: z.ZodOptional<z.ZodNumber>;
|
|
187
|
-
transcript_bytes: z.ZodNumber;
|
|
188
|
-
transcript_lines: z.ZodNumber;
|
|
189
|
-
transcript_files: z.ZodNumber;
|
|
190
|
-
truncated_models: z.ZodNumber;
|
|
191
|
-
truncated_tool_names: z.ZodNumber;
|
|
192
|
-
skill_use: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
193
|
-
skill: z.ZodString;
|
|
194
|
-
plugin: z.ZodNullable<z.ZodString>;
|
|
195
|
-
by_person: z.ZodNumber;
|
|
196
|
-
by_agent: z.ZodNumber;
|
|
197
|
-
}, z.core.$strict>>>;
|
|
198
|
-
spawn: z.ZodOptional<z.ZodObject<{
|
|
199
|
-
kind: z.ZodEnum<{
|
|
200
|
-
interactive: "interactive";
|
|
201
|
-
worker: "worker";
|
|
202
|
-
subagent: "subagent";
|
|
203
|
-
}>;
|
|
204
|
-
client_app: z.ZodOptional<z.ZodEnum<{
|
|
205
|
-
unknown: "unknown";
|
|
206
|
-
claude_cli: "claude_cli";
|
|
207
|
-
claude_desktop: "claude_desktop";
|
|
208
|
-
claude_ide: "claude_ide";
|
|
209
|
-
codex_cli: "codex_cli";
|
|
210
|
-
codex_desktop: "codex_desktop";
|
|
211
|
-
codex_ide: "codex_ide";
|
|
212
|
-
codex_exec: "codex_exec";
|
|
213
|
-
}>>;
|
|
214
|
-
parent_session_id: z.ZodOptional<z.ZodString>;
|
|
215
|
-
}, z.core.$strict>>;
|
|
216
|
-
}, z.core.$strict>;
|
|
217
|
-
export type SessionFactsWire = z.infer<typeof SessionFactsSchema>;
|
|
132
|
+
export { SessionFactsSchema, SessionFactsUsageSchema } from "./session-facts-schema.js";
|
|
133
|
+
export type { SessionFactsWire } from "./session-facts-schema.js";
|
|
218
134
|
export { claudeAssistantText, claudeUserTurnText, codexAssistantText, codexUserTurnText, normalizeTurnText, textFromPlainTextBlocks, } from "./session-facts-text.js";
|