@bli-cockpit/telemetry-core 0.1.97 → 0.1.99
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 +6 -0
- package/dist/agent-artifacts.js +1 -1
- package/dist/codex-record-copies.js +11 -6
- package/dist/evidence-completeness.d.ts +2 -2
- package/dist/evidence-rules-fingerprint.js +3 -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-abort.d.ts +1 -1
- package/dist/evidence-upload.d.ts +61 -3
- package/dist/evidence-upload.js +1 -1
- package/dist/image-byte-scan.js +35 -6
- package/dist/image-descriptions.d.ts +5 -1
- package/dist/image-descriptions.js +3 -1
- package/dist/image-store.d.ts +2 -0
- package/dist/image-store.js +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/ingest-dto.d.ts +2 -2
- package/dist/local-config.d.ts +1 -1
- package/dist/memory-hook-stats.d.ts +1 -1
- package/dist/native-usage-facts.d.ts +8 -0
- package/dist/native-usage-facts.js +28 -0
- package/dist/opencode-transcript.d.ts +8 -0
- package/dist/opencode-transcript.js +72 -0
- package/dist/pi-session-facts.d.ts +2 -0
- package/dist/pi-session-facts.js +37 -0
- package/dist/pi-transcript-usage.js +2 -0
- package/dist/session-facts-schema.d.ts +110 -0
- package/dist/session-facts-schema.js +63 -0
- package/dist/session-facts-spawn.js +2 -0
- package/dist/session-facts.d.ts +19 -118
- package/dist/session-facts.js +24 -60
- package/dist/shelf.d.ts +31 -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 +14 -2
- package/package.json +1 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** Native OpenCode semantics, shared by collection and downloaded-evidence readers. */
|
|
2
|
+
import { observeNativeUsageFacts } from "./native-usage-facts.js";
|
|
3
|
+
export function nativeObject(value) {
|
|
4
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
5
|
+
}
|
|
6
|
+
/** Text comes from native text parts; reasoning, tool output and unknown parts remain separate evidence. */
|
|
7
|
+
export function openCodeTurnText(record, role) {
|
|
8
|
+
if (record.type === "opencode_part") {
|
|
9
|
+
const part = nativeObject(record.part);
|
|
10
|
+
return !(role === "user" && (part.synthetic === true || part.ignored === true)) && record.role === role && part.type === "text" && typeof part.text === "string" ? part.text : null;
|
|
11
|
+
}
|
|
12
|
+
const info = nativeObject(record.info);
|
|
13
|
+
if (record.type !== "opencode_message" || info.role !== role || !Array.isArray(record.parts))
|
|
14
|
+
return null;
|
|
15
|
+
return record.parts.map(nativeObject).filter((part) => !(role === "user" && (part.synthetic === true || part.ignored === true)) && part.type === "text" && typeof part.text === "string").map((part) => part.text).join("\n") || null;
|
|
16
|
+
}
|
|
17
|
+
/** Only explicit provider item identities can account copied usage; native message IDs change on a real fork. */
|
|
18
|
+
export function openCodeProviderIdentity(info, parts) {
|
|
19
|
+
const provider = typeof info.providerID === "string" ? info.providerID : null;
|
|
20
|
+
const ids = new Set();
|
|
21
|
+
for (const part of parts) {
|
|
22
|
+
for (const [name, value] of Object.entries(nativeObject(part.metadata))) {
|
|
23
|
+
const itemId = nativeObject(value).itemId;
|
|
24
|
+
if (name === provider && typeof itemId === "string" && itemId)
|
|
25
|
+
ids.add(itemId);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return provider && ids.size ? JSON.stringify([provider, ...[...ids].sort()]) : null;
|
|
29
|
+
}
|
|
30
|
+
/** Count native roles/tools and explicitly accounted usage without treating absent usage as zero coverage. */
|
|
31
|
+
export function observeOpenCodeFacts(acc, record) {
|
|
32
|
+
const info = nativeObject(record.info);
|
|
33
|
+
const time = nativeObject(info.time);
|
|
34
|
+
const partTime = nativeObject(nativeObject(record.part).time);
|
|
35
|
+
for (const value of [time.created, time.completed, partTime.start, partTime.end]) {
|
|
36
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
37
|
+
continue;
|
|
38
|
+
acc.timestamps_ms.push(value);
|
|
39
|
+
acc.first_timestamp_ms = acc.first_timestamp_ms == null ? value : Math.min(acc.first_timestamp_ms, value);
|
|
40
|
+
acc.last_timestamp_ms = acc.last_timestamp_ms == null ? value : Math.max(acc.last_timestamp_ms, value);
|
|
41
|
+
}
|
|
42
|
+
if (record.type === "opencode_message" && info.role === "assistant")
|
|
43
|
+
acc.opencode_usage_messages = (acc.opencode_usage_messages ?? 0) + 1;
|
|
44
|
+
const parts = record.type === "opencode_part" ? [nativeObject(record.part)] : Array.isArray(record.parts) ? record.parts.map(nativeObject) : [];
|
|
45
|
+
const role = record.type === "opencode_part" ? record.role : info.role;
|
|
46
|
+
for (const part of parts) {
|
|
47
|
+
const messageId = String(part.messageID ?? info.id ?? "");
|
|
48
|
+
if (!(role === "user" && (part.synthetic === true || part.ignored === true)) && part.type === "text" && typeof part.text === "string" && part.text.trim()) {
|
|
49
|
+
const turnKey = `turn:${role}:${messageId}`;
|
|
50
|
+
if (!acc.seen_usage_ids.has(turnKey)) {
|
|
51
|
+
acc.seen_usage_ids.add(turnKey);
|
|
52
|
+
if (role === "user")
|
|
53
|
+
acc.user_prompt_count += 1;
|
|
54
|
+
if (role === "assistant")
|
|
55
|
+
acc.assistant_response_count += 1;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (part.type === "tool") {
|
|
59
|
+
const key = `tool:${String(part.callID ?? part.id ?? "")}`;
|
|
60
|
+
if (!acc.seen_usage_ids.has(key)) {
|
|
61
|
+
acc.seen_usage_ids.add(key);
|
|
62
|
+
const name = typeof part.tool === "string" ? part.tool.slice(0, 120) : "unknown";
|
|
63
|
+
acc.tool_calls_by_name[name] = (acc.tool_calls_by_name[name] ?? 0) + 1;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (record.type !== "opencode_usage")
|
|
68
|
+
return;
|
|
69
|
+
const tokens = nativeObject(record.tokens), cache = nativeObject(tokens.cache);
|
|
70
|
+
const number = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : 0;
|
|
71
|
+
observeNativeUsageFacts(acc, { status: record.status, identity: record.identity, model: record.model, usage: { input_tokens: number(tokens.input), output_tokens: number(tokens.output) + number(tokens.reasoning), cache_read_input_tokens: number(cache.read), cache_creation_input_tokens: number(cache.write) } });
|
|
72
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Native Pi facts share the existing wire claim contract without pretending Pi is another format. */
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { piObject, piString } from './pi-transcript-usage.js';
|
|
4
|
+
import { piTurnText } from './pi-transcript.js';
|
|
5
|
+
import { observeNativeUsageFacts } from './native-usage-facts.js';
|
|
6
|
+
export function observePiFacts(acc, record) {
|
|
7
|
+
const message = piObject(record.message);
|
|
8
|
+
if (record.type === "pi_raw_fragment" || (record.type === "pi_capture" && Array.isArray(record.pending_reasons) && record.pending_reasons.length))
|
|
9
|
+
acc.unknown_usage_records = (acc.unknown_usage_records ?? 0) + 1;
|
|
10
|
+
if (record.type === 'message') {
|
|
11
|
+
if (typeof record.id === 'string') {
|
|
12
|
+
const key = `pi-entry:${record.id}:${createHash('sha256').update(JSON.stringify({ ...record, parentId: null })).digest('hex')}`;
|
|
13
|
+
if (acc.seen_usage_ids.has(key))
|
|
14
|
+
return;
|
|
15
|
+
acc.seen_usage_ids.add(key);
|
|
16
|
+
}
|
|
17
|
+
if (piTurnText(record, 'user')?.trim())
|
|
18
|
+
acc.user_prompt_count++;
|
|
19
|
+
if (piTurnText(record, 'assistant')?.trim())
|
|
20
|
+
acc.assistant_response_count++;
|
|
21
|
+
if (message.role === 'assistant')
|
|
22
|
+
acc.opencode_usage_messages = (acc.opencode_usage_messages ?? 0) + 1;
|
|
23
|
+
if (message.role === 'assistant')
|
|
24
|
+
for (const block of Array.isArray(message.content) ? message.content.map(piObject) : []) {
|
|
25
|
+
if (block.type !== 'toolCall')
|
|
26
|
+
continue;
|
|
27
|
+
const name = (piString(block.name) ?? 'unknown').slice(0, 120);
|
|
28
|
+
acc.tool_calls_by_name[name] = (acc.tool_calls_by_name[name] ?? 0) + 1;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (record.type !== 'pi_usage')
|
|
32
|
+
return;
|
|
33
|
+
const usage = piObject(record.usage);
|
|
34
|
+
const number = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : 0;
|
|
35
|
+
observeNativeUsageFacts(acc, { status: record.status, identity: record.identity, model: record.model,
|
|
36
|
+
usage: { input_tokens: number(usage.input_tokens), output_tokens: number(usage.output_tokens), cache_read_input_tokens: number(usage.cache_read_input_tokens), cache_creation_input_tokens: number(usage.cache_creation_input_tokens) } });
|
|
37
|
+
}
|
|
@@ -5,6 +5,8 @@ export function accountPiUsage(sessionId, entry, ledger) {
|
|
|
5
5
|
const api = piString(message.api), responseId = piString(message.responseId), entryId = piString(entry.id);
|
|
6
6
|
const identity = provider && api && responseId ? JSON.stringify([provider, api, responseId]) : null;
|
|
7
7
|
const result = { status: 'unknown', identity, ownerSessionId: null, model, provider, usage: null, reason: 'pi_usage_identity_or_fields_missing' };
|
|
8
|
+
if (!['stop', 'toolUse', 'length'].includes(String(message.stopReason)))
|
|
9
|
+
return { ...result, reason: 'pi_response_not_terminal_usage_unverified' };
|
|
8
10
|
const native = piObject(message.usage);
|
|
9
11
|
const fields = ['input', 'output', 'cacheRead', 'cacheWrite', 'totalTokens'];
|
|
10
12
|
if (!identity || !model || !entryId || !fields.every(key => nonnegativeNumber(native[key])))
|
|
@@ -0,0 +1,110 @@
|
|
|
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
|
+
pi: "pi";
|
|
25
|
+
}>;
|
|
26
|
+
usage_accounting: z.ZodOptional<z.ZodEnum<{
|
|
27
|
+
complete: "complete";
|
|
28
|
+
partial: "partial";
|
|
29
|
+
unavailable: "unavailable";
|
|
30
|
+
}>>;
|
|
31
|
+
usage_identity_scope: z.ZodOptional<z.ZodEnum<{
|
|
32
|
+
machine: "machine";
|
|
33
|
+
operator: "operator";
|
|
34
|
+
}>>;
|
|
35
|
+
usage_claims: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
36
|
+
identity: z.ZodString;
|
|
37
|
+
model: z.ZodString;
|
|
38
|
+
usage: z.ZodObject<{
|
|
39
|
+
input_tokens: z.ZodNumber;
|
|
40
|
+
output_tokens: z.ZodNumber;
|
|
41
|
+
cache_read_input_tokens: z.ZodNumber;
|
|
42
|
+
cache_creation_input_tokens: z.ZodNumber;
|
|
43
|
+
}, z.core.$strict>;
|
|
44
|
+
}, z.core.$strict>>>;
|
|
45
|
+
unknown_usage_records: z.ZodOptional<z.ZodNumber>;
|
|
46
|
+
copied_usage_records: z.ZodOptional<z.ZodNumber>;
|
|
47
|
+
usage_by_model: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
48
|
+
input_tokens: z.ZodNumber;
|
|
49
|
+
output_tokens: z.ZodNumber;
|
|
50
|
+
cache_read_input_tokens: z.ZodNumber;
|
|
51
|
+
cache_creation_input_tokens: z.ZodNumber;
|
|
52
|
+
}, z.core.$strict>>;
|
|
53
|
+
models: z.ZodArray<z.ZodString>;
|
|
54
|
+
input_tokens: z.ZodNumber;
|
|
55
|
+
output_tokens: z.ZodNumber;
|
|
56
|
+
cache_read_tokens: z.ZodNumber;
|
|
57
|
+
cache_creation_tokens: z.ZodNumber;
|
|
58
|
+
total_tokens: z.ZodNumber;
|
|
59
|
+
record_count: z.ZodNumber;
|
|
60
|
+
turn_count: z.ZodNumber;
|
|
61
|
+
user_prompt_count: z.ZodNumber;
|
|
62
|
+
assistant_response_count: z.ZodNumber;
|
|
63
|
+
tool_calls_by_name: z.ZodRecord<z.ZodString, z.ZodNumber>;
|
|
64
|
+
tool_call_count: z.ZodNumber;
|
|
65
|
+
first_timestamp: z.ZodNullable<z.ZodString>;
|
|
66
|
+
last_timestamp: z.ZodNullable<z.ZodString>;
|
|
67
|
+
wall_duration_seconds: z.ZodNullable<z.ZodNumber>;
|
|
68
|
+
active_seconds: z.ZodOptional<z.ZodNumber>;
|
|
69
|
+
transcript_bytes: z.ZodNumber;
|
|
70
|
+
transcript_lines: z.ZodNumber;
|
|
71
|
+
transcript_files: z.ZodNumber;
|
|
72
|
+
truncated_models: z.ZodNumber;
|
|
73
|
+
truncated_tool_names: z.ZodNumber;
|
|
74
|
+
skill_use: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
75
|
+
skill: z.ZodString;
|
|
76
|
+
plugin: z.ZodNullable<z.ZodString>;
|
|
77
|
+
by_person: z.ZodNumber;
|
|
78
|
+
by_agent: z.ZodNumber;
|
|
79
|
+
}, z.core.$strict>>>;
|
|
80
|
+
spawn: z.ZodOptional<z.ZodObject<{
|
|
81
|
+
kind: z.ZodEnum<{
|
|
82
|
+
interactive: "interactive";
|
|
83
|
+
worker: "worker";
|
|
84
|
+
subagent: "subagent";
|
|
85
|
+
}>;
|
|
86
|
+
client_app: z.ZodOptional<z.ZodEnum<{
|
|
87
|
+
unknown: "unknown";
|
|
88
|
+
claude_cli: "claude_cli";
|
|
89
|
+
claude_desktop: "claude_desktop";
|
|
90
|
+
claude_ide: "claude_ide";
|
|
91
|
+
codex_cli: "codex_cli";
|
|
92
|
+
codex_desktop: "codex_desktop";
|
|
93
|
+
codex_ide: "codex_ide";
|
|
94
|
+
codex_exec: "codex_exec";
|
|
95
|
+
t3_code: "t3_code";
|
|
96
|
+
}>>;
|
|
97
|
+
parent_session_id: z.ZodOptional<z.ZodString>;
|
|
98
|
+
}, z.core.$strict>>;
|
|
99
|
+
hands_on: z.ZodOptional<z.ZodObject<{
|
|
100
|
+
rule_id: z.ZodLiteral<"hands_on.v1">;
|
|
101
|
+
seconds: z.ZodNumber;
|
|
102
|
+
events: z.ZodArray<z.ZodTuple<[z.ZodString, z.ZodNumber], null>>;
|
|
103
|
+
intervals: z.ZodArray<z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>>;
|
|
104
|
+
human_count: z.ZodNumber;
|
|
105
|
+
not_human_count: z.ZodNumber;
|
|
106
|
+
unknown_count: z.ZodNumber;
|
|
107
|
+
events_truncated: z.ZodNumber;
|
|
108
|
+
}, z.core.$strict>>;
|
|
109
|
+
}, z.core.$strict>;
|
|
110
|
+
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", "pi"]),
|
|
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();
|
|
@@ -48,6 +48,8 @@ export function claudeSpawn(current, record) {
|
|
|
48
48
|
* reports every distinct unmapped value so a real one (a VS Code or JetBrains entrypoint) is added from evidence.
|
|
49
49
|
*/
|
|
50
50
|
export function claudeClientApp(entrypoint) {
|
|
51
|
+
if (entrypoint === "t3-code")
|
|
52
|
+
return "t3_code";
|
|
51
53
|
if (entrypoint === "cli" || entrypoint === "sdk-cli")
|
|
52
54
|
return "claude_cli";
|
|
53
55
|
if (entrypoint === "claude-desktop")
|
package/dist/session-facts.d.ts
CHANGED
|
@@ -1,37 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Session facts: everything deterministic about one transcript, counted once,
|
|
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";
|
|
35
2
|
import { HandsOnTally, type HandsOnFacts } from "./session-facts-hands-on.js";
|
|
36
3
|
import { type SessionSpawn } from "./session-facts-spawn.js";
|
|
37
4
|
import { SkillUseTally } from "./session-facts-skills.js";
|
|
@@ -39,19 +6,27 @@ import type { SkillUseCount } from "./session-facts-skills.js";
|
|
|
39
6
|
/** Bump when a counting rule changes, never when a field is added. */
|
|
40
7
|
export declare const SESSION_FACTS_VERSION = "det.v6";
|
|
41
8
|
/** How many distinct models and tool names one session may report. */
|
|
42
|
-
export
|
|
43
|
-
export declare const SESSION_FACTS_MAX_TOOL_NAMES = 200;
|
|
44
|
-
export type SessionFactsSource = "codex" | "claude_code";
|
|
9
|
+
export type SessionFactsSource = "codex" | "claude_code" | "opencode" | "pi";
|
|
45
10
|
export interface SessionFactsUsage {
|
|
46
11
|
input_tokens: number;
|
|
47
12
|
output_tokens: number;
|
|
48
13
|
cache_read_input_tokens: number;
|
|
49
14
|
cache_creation_input_tokens: number;
|
|
50
15
|
}
|
|
16
|
+
export interface NativeUsageClaim {
|
|
17
|
+
identity: string;
|
|
18
|
+
model: string;
|
|
19
|
+
usage: SessionFactsUsage;
|
|
20
|
+
}
|
|
51
21
|
export interface SessionFacts {
|
|
52
22
|
/** The rules that produced these numbers. */
|
|
53
23
|
extractor_version: string;
|
|
54
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;
|
|
55
30
|
usage_by_model: Record<string, SessionFactsUsage>;
|
|
56
31
|
models: string[];
|
|
57
32
|
input_tokens: number;
|
|
@@ -88,7 +63,12 @@ export interface SessionFacts {
|
|
|
88
63
|
hands_on?: HandsOnFacts;
|
|
89
64
|
}
|
|
90
65
|
export interface SessionFactsAccumulator {
|
|
66
|
+
opencode_usage_messages?: number;
|
|
67
|
+
opencode_usage_records?: number;
|
|
68
|
+
opencode_usage_claims?: Map<string, NativeUsageClaim>;
|
|
91
69
|
source: SessionFactsSource;
|
|
70
|
+
unknown_usage_records?: number;
|
|
71
|
+
copied_usage_records?: number;
|
|
92
72
|
usage_by_model: Record<string, SessionFactsUsage>;
|
|
93
73
|
seen_usage_ids: Set<string>;
|
|
94
74
|
record_count: number;
|
|
@@ -149,85 +129,6 @@ export declare function computeSessionFacts(options: {
|
|
|
149
129
|
source: SessionFactsSource;
|
|
150
130
|
contents: readonly string[];
|
|
151
131
|
}): SessionFacts;
|
|
152
|
-
export
|
|
153
|
-
|
|
154
|
-
output_tokens: z.ZodNumber;
|
|
155
|
-
cache_read_input_tokens: z.ZodNumber;
|
|
156
|
-
cache_creation_input_tokens: z.ZodNumber;
|
|
157
|
-
}, z.core.$strict>;
|
|
158
|
-
/**
|
|
159
|
-
* The wire shape. Optional wherever it rides an envelope: a CLI published
|
|
160
|
-
* before this change sends no block at all, and its uploads must still
|
|
161
|
-
* validate.
|
|
162
|
-
*/
|
|
163
|
-
export declare const SessionFactsSchema: z.ZodObject<{
|
|
164
|
-
extractor_version: z.ZodString;
|
|
165
|
-
source: z.ZodEnum<{
|
|
166
|
-
codex: "codex";
|
|
167
|
-
claude_code: "claude_code";
|
|
168
|
-
}>;
|
|
169
|
-
usage_by_model: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
170
|
-
input_tokens: z.ZodNumber;
|
|
171
|
-
output_tokens: z.ZodNumber;
|
|
172
|
-
cache_read_input_tokens: z.ZodNumber;
|
|
173
|
-
cache_creation_input_tokens: z.ZodNumber;
|
|
174
|
-
}, z.core.$strict>>;
|
|
175
|
-
models: z.ZodArray<z.ZodString>;
|
|
176
|
-
input_tokens: z.ZodNumber;
|
|
177
|
-
output_tokens: z.ZodNumber;
|
|
178
|
-
cache_read_tokens: z.ZodNumber;
|
|
179
|
-
cache_creation_tokens: z.ZodNumber;
|
|
180
|
-
total_tokens: z.ZodNumber;
|
|
181
|
-
record_count: z.ZodNumber;
|
|
182
|
-
turn_count: z.ZodNumber;
|
|
183
|
-
user_prompt_count: z.ZodNumber;
|
|
184
|
-
assistant_response_count: z.ZodNumber;
|
|
185
|
-
tool_calls_by_name: z.ZodRecord<z.ZodString, z.ZodNumber>;
|
|
186
|
-
tool_call_count: z.ZodNumber;
|
|
187
|
-
first_timestamp: z.ZodNullable<z.ZodString>;
|
|
188
|
-
last_timestamp: z.ZodNullable<z.ZodString>;
|
|
189
|
-
wall_duration_seconds: z.ZodNullable<z.ZodNumber>;
|
|
190
|
-
active_seconds: z.ZodOptional<z.ZodNumber>;
|
|
191
|
-
transcript_bytes: z.ZodNumber;
|
|
192
|
-
transcript_lines: z.ZodNumber;
|
|
193
|
-
transcript_files: z.ZodNumber;
|
|
194
|
-
truncated_models: z.ZodNumber;
|
|
195
|
-
truncated_tool_names: z.ZodNumber;
|
|
196
|
-
skill_use: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
197
|
-
skill: z.ZodString;
|
|
198
|
-
plugin: z.ZodNullable<z.ZodString>;
|
|
199
|
-
by_person: z.ZodNumber;
|
|
200
|
-
by_agent: z.ZodNumber;
|
|
201
|
-
}, z.core.$strict>>>;
|
|
202
|
-
spawn: z.ZodOptional<z.ZodObject<{
|
|
203
|
-
kind: z.ZodEnum<{
|
|
204
|
-
interactive: "interactive";
|
|
205
|
-
worker: "worker";
|
|
206
|
-
subagent: "subagent";
|
|
207
|
-
}>;
|
|
208
|
-
client_app: z.ZodOptional<z.ZodEnum<{
|
|
209
|
-
unknown: "unknown";
|
|
210
|
-
claude_cli: "claude_cli";
|
|
211
|
-
claude_desktop: "claude_desktop";
|
|
212
|
-
claude_ide: "claude_ide";
|
|
213
|
-
codex_cli: "codex_cli";
|
|
214
|
-
codex_desktop: "codex_desktop";
|
|
215
|
-
codex_ide: "codex_ide";
|
|
216
|
-
codex_exec: "codex_exec";
|
|
217
|
-
t3_code: "t3_code";
|
|
218
|
-
}>>;
|
|
219
|
-
parent_session_id: z.ZodOptional<z.ZodString>;
|
|
220
|
-
}, z.core.$strict>>;
|
|
221
|
-
hands_on: z.ZodOptional<z.ZodObject<{
|
|
222
|
-
rule_id: z.ZodLiteral<"hands_on.v1">;
|
|
223
|
-
seconds: z.ZodNumber;
|
|
224
|
-
events: z.ZodArray<z.ZodTuple<[z.ZodString, z.ZodNumber], null>>;
|
|
225
|
-
intervals: z.ZodArray<z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>>;
|
|
226
|
-
human_count: z.ZodNumber;
|
|
227
|
-
not_human_count: z.ZodNumber;
|
|
228
|
-
unknown_count: z.ZodNumber;
|
|
229
|
-
events_truncated: z.ZodNumber;
|
|
230
|
-
}, z.core.$strict>>;
|
|
231
|
-
}, z.core.$strict>;
|
|
232
|
-
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";
|
|
233
134
|
export { claudeAssistantText, claudeUserTurnText, codexAssistantText, codexUserTurnText, normalizeTurnText, textFromPlainTextBlocks, } from "./session-facts-text.js";
|
package/dist/session-facts.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { readT3ProjectionMessage } from "./t3-projection-message.js";
|
|
1
2
|
/**
|
|
2
3
|
* Session facts: everything deterministic about one transcript, counted once,
|
|
3
4
|
* where the file already is.
|
|
@@ -31,21 +32,19 @@
|
|
|
31
32
|
* includes reasoning, so input is normalized to fresh-only, matching the
|
|
32
33
|
* Anthropic-shaped usage the price table expects.
|
|
33
34
|
*/
|
|
34
|
-
import {
|
|
35
|
+
import { MAX_MODEL_NAME_CHARS, MAX_TOOL_NAME_CHARS, SESSION_FACTS_MAX_MODELS, SESSION_FACTS_MAX_TOOL_NAMES } from "./session-facts-schema.js";
|
|
36
|
+
export { SESSION_FACTS_MAX_MODELS, SESSION_FACTS_MAX_TOOL_NAMES } from "./session-facts-schema.js";
|
|
37
|
+
import { observePiFacts } from "./pi-session-facts.js";
|
|
38
|
+
import { observeOpenCodeFacts } from "./opencode-transcript.js";
|
|
35
39
|
import { includeCodexRevisionRecord, observeCodexCumulativeUsage, codexRevisionUsageModel } from "./codex-record-copies.js";
|
|
36
40
|
import { claudeAssistantText, claudeUserTurnText, codexAssistantText, codexUserTurnText } from "./session-facts-text.js";
|
|
37
41
|
import { activeSeconds } from "./session-facts-active.js";
|
|
38
42
|
import { byteLength, emptyUsage, sumUsage, usageTotal } from "./session-facts-usage-math.js";
|
|
39
|
-
import { classifyClaudeUserRecord, classifyCodexTaskStarted, codexTurnId, finalizeHandsOn,
|
|
40
|
-
import { claudeSpawn, codexSpawn
|
|
41
|
-
import { observeClaudeSkillUse, observeCodexSkillUse, SKILL_USE_MAX_SKILLS,
|
|
43
|
+
import { classifyClaudeUserRecord, classifyCodexTaskStarted, codexTurnId, finalizeHandsOn, HandsOnTally, } from "./session-facts-hands-on.js";
|
|
44
|
+
import { claudeSpawn, codexSpawn } from "./session-facts-spawn.js";
|
|
45
|
+
import { observeClaudeSkillUse, observeCodexSkillUse, SKILL_USE_MAX_SKILLS, SkillUseTally } from "./session-facts-skills.js";
|
|
42
46
|
/** Bump when a counting rule changes, never when a field is added. */
|
|
43
47
|
export const SESSION_FACTS_VERSION = "det.v6";
|
|
44
|
-
/** How many distinct models and tool names one session may report. */
|
|
45
|
-
export const SESSION_FACTS_MAX_MODELS = 50;
|
|
46
|
-
export const SESSION_FACTS_MAX_TOOL_NAMES = 200;
|
|
47
|
-
const MAX_MODEL_NAME_CHARS = 200;
|
|
48
|
-
const MAX_TOOL_NAME_CHARS = 120;
|
|
49
48
|
export function newSessionFactsAccumulator(source) {
|
|
50
49
|
return {
|
|
51
50
|
source,
|
|
@@ -79,7 +78,7 @@ export function observeSessionFactsRecord(acc, record) {
|
|
|
79
78
|
if (acc.source === "codex" && !includeCodexRevisionRecord(acc, record))
|
|
80
79
|
return false;
|
|
81
80
|
acc.record_count += 1;
|
|
82
|
-
const ms = parseTimestampMs(record["timestamp"]);
|
|
81
|
+
const ms = parseTimestampMs(acc.source === "codex" ? readT3ProjectionMessage(record)?.created_at ?? record["timestamp"] : record["timestamp"]);
|
|
83
82
|
if (ms != null) {
|
|
84
83
|
acc.timestamps_ms.push(ms);
|
|
85
84
|
acc.first_timestamp_ms =
|
|
@@ -87,7 +86,11 @@ export function observeSessionFactsRecord(acc, record) {
|
|
|
87
86
|
acc.last_timestamp_ms =
|
|
88
87
|
acc.last_timestamp_ms == null ? ms : Math.max(acc.last_timestamp_ms, ms);
|
|
89
88
|
}
|
|
90
|
-
if (acc.source === "
|
|
89
|
+
if (acc.source === "pi")
|
|
90
|
+
observePiFacts(acc, record);
|
|
91
|
+
else if (acc.source === "opencode")
|
|
92
|
+
observeOpenCodeFacts(acc, record);
|
|
93
|
+
else if (acc.source === "claude_code")
|
|
91
94
|
observeClaudeRecord(acc, record);
|
|
92
95
|
else
|
|
93
96
|
observeCodexRecord(acc, record);
|
|
@@ -139,6 +142,12 @@ function observeClaudeRecord(acc, record) {
|
|
|
139
142
|
observeClaudeSkillUse(acc.skill_tally, record);
|
|
140
143
|
}
|
|
141
144
|
function observeCodexRecord(acc, record) {
|
|
145
|
+
const projection = readT3ProjectionMessage(record);
|
|
146
|
+
if (projection) {
|
|
147
|
+
if (projection.text.trim())
|
|
148
|
+
acc.assistant_response_count += 1;
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
142
151
|
const type = String(record["type"] ?? "");
|
|
143
152
|
const payload = record["payload"];
|
|
144
153
|
const p = payload && typeof payload === "object" ? payload : {};
|
|
@@ -216,6 +225,8 @@ export function codexUsageFromCumulativeTotal(total) {
|
|
|
216
225
|
};
|
|
217
226
|
}
|
|
218
227
|
export function finalizeSessionFacts(acc) {
|
|
228
|
+
if (acc.source === "opencode" || acc.source === "pi")
|
|
229
|
+
acc.unknown_usage_records = (acc.unknown_usage_records ?? 0) + Math.max(0, (acc.opencode_usage_messages ?? 0) - (acc.opencode_usage_records ?? 0));
|
|
219
230
|
// A Codex session whose usage never named a model is left uncounted, not filed under a guess (as det.v5).
|
|
220
231
|
const usageModel = codexRevisionUsageModel(acc);
|
|
221
232
|
if (acc.codex_total_usage && usageModel) {
|
|
@@ -234,6 +245,7 @@ export function finalizeSessionFacts(acc) {
|
|
|
234
245
|
return {
|
|
235
246
|
extractor_version: SESSION_FACTS_VERSION,
|
|
236
247
|
source: acc.source,
|
|
248
|
+
...((acc.source === "opencode" || acc.source === "pi") ? { usage_identity_scope: "machine", usage_claims: [...(acc.opencode_usage_claims?.values() ?? [])], usage_accounting: acc.unknown_usage_records ? (Object.keys(acc.usage_by_model).length ? "partial" : "unavailable") : "complete", unknown_usage_records: acc.unknown_usage_records ?? 0, copied_usage_records: acc.copied_usage_records ?? 0 } : {}),
|
|
237
249
|
usage_by_model,
|
|
238
250
|
models: keptUsage.map(([model]) => model).sort((a, b) => a.localeCompare(b)),
|
|
239
251
|
input_tokens: totals.input_tokens,
|
|
@@ -309,53 +321,5 @@ function parseTimestampMs(value) {
|
|
|
309
321
|
function num(value) {
|
|
310
322
|
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
311
323
|
}
|
|
312
|
-
|
|
313
|
-
export const SessionFactsUsageSchema = z
|
|
314
|
-
.object({
|
|
315
|
-
input_tokens: TokenCountSchema,
|
|
316
|
-
output_tokens: TokenCountSchema,
|
|
317
|
-
cache_read_input_tokens: TokenCountSchema,
|
|
318
|
-
cache_creation_input_tokens: TokenCountSchema,
|
|
319
|
-
})
|
|
320
|
-
.strict();
|
|
321
|
-
/**
|
|
322
|
-
* The wire shape. Optional wherever it rides an envelope: a CLI published
|
|
323
|
-
* before this change sends no block at all, and its uploads must still
|
|
324
|
-
* validate.
|
|
325
|
-
*/
|
|
326
|
-
export const SessionFactsSchema = z
|
|
327
|
-
.object({
|
|
328
|
-
extractor_version: z.string().trim().min(1).max(40),
|
|
329
|
-
source: z.enum(["codex", "claude_code"]),
|
|
330
|
-
usage_by_model: z
|
|
331
|
-
.record(z.string().min(1).max(MAX_MODEL_NAME_CHARS), SessionFactsUsageSchema)
|
|
332
|
-
.refine((value) => Object.keys(value).length <= SESSION_FACTS_MAX_MODELS, { message: "too many models" }),
|
|
333
|
-
models: z.array(z.string().min(1).max(MAX_MODEL_NAME_CHARS)).max(SESSION_FACTS_MAX_MODELS),
|
|
334
|
-
input_tokens: TokenCountSchema,
|
|
335
|
-
output_tokens: TokenCountSchema,
|
|
336
|
-
cache_read_tokens: TokenCountSchema,
|
|
337
|
-
cache_creation_tokens: TokenCountSchema,
|
|
338
|
-
total_tokens: TokenCountSchema,
|
|
339
|
-
record_count: z.number().int().nonnegative(),
|
|
340
|
-
turn_count: z.number().int().nonnegative(),
|
|
341
|
-
user_prompt_count: z.number().int().nonnegative(),
|
|
342
|
-
assistant_response_count: z.number().int().nonnegative(),
|
|
343
|
-
tool_calls_by_name: z
|
|
344
|
-
.record(z.string().min(1).max(MAX_TOOL_NAME_CHARS), z.number().int().nonnegative())
|
|
345
|
-
.refine((value) => Object.keys(value).length <= SESSION_FACTS_MAX_TOOL_NAMES, { message: "too many tool names" }),
|
|
346
|
-
tool_call_count: z.number().int().nonnegative(),
|
|
347
|
-
first_timestamp: z.string().datetime({ offset: true }).nullable(),
|
|
348
|
-
last_timestamp: z.string().datetime({ offset: true }).nullable(),
|
|
349
|
-
wall_duration_seconds: z.number().int().nonnegative().nullable(),
|
|
350
|
-
active_seconds: z.number().int().nonnegative().optional(),
|
|
351
|
-
transcript_bytes: z.number().int().nonnegative(),
|
|
352
|
-
transcript_lines: z.number().int().nonnegative(),
|
|
353
|
-
transcript_files: z.number().int().nonnegative(),
|
|
354
|
-
truncated_models: z.number().int().nonnegative(),
|
|
355
|
-
truncated_tool_names: z.number().int().nonnegative(),
|
|
356
|
-
skill_use: z.array(SkillUseCountSchema).max(SKILL_USE_MAX_SKILLS).optional(),
|
|
357
|
-
spawn: SessionSpawnSchema.optional(),
|
|
358
|
-
hands_on: HandsOnFactsSchema.optional(),
|
|
359
|
-
})
|
|
360
|
-
.strict();
|
|
324
|
+
export { SessionFactsSchema, SessionFactsUsageSchema } from "./session-facts-schema.js";
|
|
361
325
|
export { claudeAssistantText, claudeUserTurnText, codexAssistantText, codexUserTurnText, normalizeTurnText, textFromPlainTextBlocks, } from "./session-facts-text.js";
|