@trazum/core 2.5.2 → 2.7.0
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/gateway.d.ts.map +1 -1
- package/dist/gateway.js +7 -6
- package/dist/gateway.js.map +1 -1
- package/dist/gemini-cli.d.ts +65 -0
- package/dist/gemini-cli.d.ts.map +1 -0
- package/dist/gemini-cli.js +162 -0
- package/dist/gemini-cli.js.map +1 -0
- package/dist/harnesses.d.ts +2 -2
- package/dist/harnesses.d.ts.map +1 -1
- package/dist/harnesses.js +1 -0
- package/dist/harnesses.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/task-analysis.d.ts +9 -0
- package/dist/task-analysis.d.ts.map +1 -0
- package/dist/task-analysis.js +379 -0
- package/dist/task-analysis.js.map +1 -0
- package/dist/task-types.d.ts +95 -0
- package/dist/task-types.d.ts.map +1 -0
- package/dist/task-types.js +14 -0
- package/dist/task-types.js.map +1 -0
- package/dist/task.d.ts +14 -0
- package/dist/task.d.ts.map +1 -0
- package/dist/task.js +226 -0
- package/dist/task.js.map +1 -0
- package/dist/usage.d.ts +33 -0
- package/dist/usage.d.ts.map +1 -1
- package/dist/usage.js +81 -2
- package/dist/usage.js.map +1 -1
- package/package.json +2 -2
- package/src/gateway.ts +7 -5
- package/src/gemini-cli.ts +187 -0
- package/src/harnesses.ts +3 -2
- package/src/index.ts +9 -0
- package/src/task-analysis.ts +283 -0
- package/src/task-types.ts +88 -0
- package/src/task.ts +172 -0
- package/src/usage.ts +109 -2
package/src/gateway.ts
CHANGED
|
@@ -547,7 +547,7 @@ function completeUsage(shape: WireShape, usage: Record<string, unknown>): boolea
|
|
|
547
547
|
: ['promptTokenCount', 'candidatesTokenCount'];
|
|
548
548
|
if (!required.every((key) => count(usage[key]))) return false;
|
|
549
549
|
const optional = shape === 'anthropic' ? ['cache_read_input_tokens', 'cache_creation_input_tokens']
|
|
550
|
-
: shape === 'google' ? ['cachedContentTokenCount', 'thoughtsTokenCount'] : [];
|
|
550
|
+
: shape === 'google' ? ['cachedContentTokenCount', 'thoughtsTokenCount', 'toolUsePromptTokenCount'] : [];
|
|
551
551
|
if (optional.some((key) => usage[key] !== undefined && !count(usage[key]))) return false;
|
|
552
552
|
if (shape === 'anthropic' && usage.cache_creation !== undefined) {
|
|
553
553
|
const breakdown = cacheWriteBreakdown(usage);
|
|
@@ -562,8 +562,6 @@ function completeUsage(shape: WireShape, usage: Record<string, unknown>): boolea
|
|
|
562
562
|
}
|
|
563
563
|
if (shape === 'google') {
|
|
564
564
|
if ((usage.cachedContentTokenCount as number | undefined ?? 0) > (usage.promptTokenCount as number)) return false;
|
|
565
|
-
// The current price path has no separate reasoning-token treatment.
|
|
566
|
-
if ((usage.thoughtsTokenCount as number | undefined ?? 0) > 0) return false;
|
|
567
565
|
}
|
|
568
566
|
return true;
|
|
569
567
|
}
|
|
@@ -773,8 +771,12 @@ export function usageFromResponse(
|
|
|
773
771
|
// part is subtracted rather than added. `usage.ts` has made this exact
|
|
774
772
|
// correction for imported Gemini logs since the importer landed; the
|
|
775
773
|
// gateway is not allowed to make it differently.
|
|
776
|
-
|
|
777
|
-
|
|
774
|
+
// Tool results fed back to the model are input outside
|
|
775
|
+
// `promptTokenCount`, and a thinking model's reasoning is output
|
|
776
|
+
// outside `candidatesTokenCount`: both billed, both read here the way
|
|
777
|
+
// `usage.ts` reads them, so live spend and the imported log agree.
|
|
778
|
+
inputTokens: Math.max(0, num(u.promptTokenCount) - cached) + num(u.toolUsePromptTokenCount),
|
|
779
|
+
outputTokens: num(u.candidatesTokenCount) + num(u.thoughtsTokenCount),
|
|
778
780
|
cacheReadTokens: cached,
|
|
779
781
|
// Gemini's implicit cache reports no write count. Zero here is a fact
|
|
780
782
|
// about the response, not a stand-in for one that failed to arrive.
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gemini CLI session files, from explicit caller-supplied text.
|
|
3
|
+
*
|
|
4
|
+
* Read against the recorder's own source rather than a sample: google-gemini/
|
|
5
|
+
* gemini-cli, `packages/core/src/services/chatRecordingService.ts` and
|
|
6
|
+
* `chatRecordingTypes.ts` (Apache-2.0). Sessions live under
|
|
7
|
+
* `~/.gemini/tmp/<project>/chats/` as `session-*.jsonl`, one JSON record per
|
|
8
|
+
* line; older versions wrote one `session-*.json` document per chat, which the
|
|
9
|
+
* CLI converts on resume and which is read here too. No prompt, reply,
|
|
10
|
+
* thought, tool argument, tool result or path is copied: only the model, the
|
|
11
|
+
* moment, the session and the token counts.
|
|
12
|
+
*
|
|
13
|
+
* How the recorder writes, and what follows for a bill:
|
|
14
|
+
*
|
|
15
|
+
* - **A message is appended whole every time it changes.** Tokens arrive after
|
|
16
|
+
* the reply, so the same message id appears twice or more, and the last one
|
|
17
|
+
* stands. Counting every line would bill one reply several times.
|
|
18
|
+
* - **Tokens attach once per reply.** `recordMessageTokens` sets them only on a
|
|
19
|
+
* `gemini` message that has none, so one id carries one response's usage.
|
|
20
|
+
* - **History edits do not undo spend.** `$rewindTo`, `$patch.removeIds` and a
|
|
21
|
+
* rewritten file change what the conversation shows, not what was called; a
|
|
22
|
+
* reply that was billed stays billed.
|
|
23
|
+
* - **`$set.messages`** is a deprecated full-history checkpoint: its messages
|
|
24
|
+
* are read through the same id rule, so a checkpoint never counts twice.
|
|
25
|
+
*/
|
|
26
|
+
export interface GeminiCliRecord {
|
|
27
|
+
model: string;
|
|
28
|
+
ts?: string;
|
|
29
|
+
session?: string;
|
|
30
|
+
label?: string;
|
|
31
|
+
usage: { input_tokens: number; output_tokens: number; cache_read_input_tokens: number };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface GeminiCliConversion {
|
|
35
|
+
records: GeminiCliRecord[];
|
|
36
|
+
/** Lines that repeat a message already read: the recorder's own rewrites, not calls. */
|
|
37
|
+
repeats: number;
|
|
38
|
+
unparseable: number;
|
|
39
|
+
/** Replies whose token counts were present and impossible: refused, never zeroed. */
|
|
40
|
+
unsupported: number;
|
|
41
|
+
missingModel: number;
|
|
42
|
+
/**
|
|
43
|
+
* Replies from a recorder that kept no reasoning or tool-use count, whose
|
|
44
|
+
* total exceeded the parts it did keep. The remainder is priced as output,
|
|
45
|
+
* the dearer side, rather than dropped; counted so the bill can say so.
|
|
46
|
+
*/
|
|
47
|
+
remainderAsOutput: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const object = (value: unknown): value is Record<string, unknown> =>
|
|
51
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
52
|
+
|
|
53
|
+
const count = (value: unknown): number | null =>
|
|
54
|
+
typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : null;
|
|
55
|
+
|
|
56
|
+
/** A signature, not a filename: the session header, or a reply with its token summary. */
|
|
57
|
+
export function looksLikeGeminiCliSession(text: string): boolean {
|
|
58
|
+
const trimmed = text.trimStart();
|
|
59
|
+
if (trimmed.startsWith('{')) {
|
|
60
|
+
// A legacy single-document session, which the recorder pretty-printed:
|
|
61
|
+
// the whole text parses as one object. JSONL fails fast at its second line.
|
|
62
|
+
try {
|
|
63
|
+
const doc: unknown = JSON.parse(trimmed);
|
|
64
|
+
if (object(doc) && typeof doc.sessionId === 'string' && typeof doc.projectHash === 'string' &&
|
|
65
|
+
Array.isArray(doc.messages)) return true;
|
|
66
|
+
} catch { /* fall through to the line rule */ }
|
|
67
|
+
}
|
|
68
|
+
for (const line of text.split('\n').slice(0, 50)) {
|
|
69
|
+
if (line.trim() === '') continue;
|
|
70
|
+
try {
|
|
71
|
+
const row: unknown = JSON.parse(line);
|
|
72
|
+
if (!object(row)) continue;
|
|
73
|
+
if (typeof row.sessionId === 'string' && typeof row.projectHash === 'string') return true;
|
|
74
|
+
if (row.type === 'gemini' && typeof row.id === 'string' && object(row.tokens)) return true;
|
|
75
|
+
} catch { /* Malformed lines are reported by conversion, never executed. */ }
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface Reply {
|
|
81
|
+
model: unknown;
|
|
82
|
+
timestamp: unknown;
|
|
83
|
+
tokens: Record<string, unknown>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* One usage record per reply that carried token counts.
|
|
88
|
+
*
|
|
89
|
+
* Gemini's `input` is `promptTokenCount`, which **includes** the cached tokens,
|
|
90
|
+
* so the cached part is taken out of it; `thoughts` (reasoning, billed as
|
|
91
|
+
* output) and `tool` (tool results fed back to the model, billed as input) sit
|
|
92
|
+
* outside the first two counts, and `total` is the sum of all four.
|
|
93
|
+
*/
|
|
94
|
+
export function geminiCliRecords(text: string, options: { label?: string } = {}): GeminiCliConversion {
|
|
95
|
+
const result: GeminiCliConversion = {
|
|
96
|
+
records: [], repeats: 0, unparseable: 0, unsupported: 0, missingModel: 0, remainderAsOutput: 0,
|
|
97
|
+
};
|
|
98
|
+
let session: string | null = null;
|
|
99
|
+
// Insertion order is first appearance; the value is the last version seen.
|
|
100
|
+
const replies = new Map<string, Reply>();
|
|
101
|
+
const sessionOf = new Map<string, string | null>();
|
|
102
|
+
const seen = new Set<string>();
|
|
103
|
+
|
|
104
|
+
const take = (message: unknown): void => {
|
|
105
|
+
if (!object(message) || typeof message.id !== 'string' || message.type !== 'gemini') return;
|
|
106
|
+
const key = `${session ?? ''}\n${message.id}`;
|
|
107
|
+
if (seen.has(key)) result.repeats++;
|
|
108
|
+
seen.add(key);
|
|
109
|
+
if (!object(message.tokens)) return;
|
|
110
|
+
replies.set(key, { model: message.model, timestamp: message.timestamp, tokens: message.tokens });
|
|
111
|
+
if (!sessionOf.has(key)) sessionOf.set(key, session);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const trimmed = text.trimStart();
|
|
115
|
+
let legacy: Record<string, unknown> | null = null;
|
|
116
|
+
if (trimmed.startsWith('{')) {
|
|
117
|
+
try {
|
|
118
|
+
const doc: unknown = JSON.parse(trimmed);
|
|
119
|
+
if (object(doc) && Array.isArray(doc.messages) && typeof doc.sessionId === 'string') legacy = doc;
|
|
120
|
+
} catch { /* not a single document: read it as lines */ }
|
|
121
|
+
}
|
|
122
|
+
if (legacy !== null) {
|
|
123
|
+
session = legacy.sessionId as string;
|
|
124
|
+
for (const message of legacy.messages as unknown[]) take(message);
|
|
125
|
+
} else {
|
|
126
|
+
for (const line of text.split('\n')) {
|
|
127
|
+
if (line.trim() === '') continue;
|
|
128
|
+
let row: unknown;
|
|
129
|
+
try { row = JSON.parse(line); } catch { result.unparseable++; continue; }
|
|
130
|
+
if (!object(row)) { result.unparseable++; continue; }
|
|
131
|
+
if (typeof row.sessionId === 'string' && row.sessionId !== '') session = row.sessionId;
|
|
132
|
+
if (object(row.$set) && Array.isArray(row.$set.messages)) {
|
|
133
|
+
for (const message of row.$set.messages as unknown[]) take(message);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
take(row);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
for (const [key, reply] of replies) {
|
|
141
|
+
const t = reply.tokens;
|
|
142
|
+
const input = count(t.input);
|
|
143
|
+
const output = count(t.output);
|
|
144
|
+
const cached = t.cached === undefined ? 0 : count(t.cached);
|
|
145
|
+
const thoughts = t.thoughts === undefined || t.thoughts === null ? null : count(t.thoughts);
|
|
146
|
+
const tool = t.tool === undefined || t.tool === null ? null : count(t.tool);
|
|
147
|
+
const total = t.total === undefined ? 0 : count(t.total);
|
|
148
|
+
if (input === null || output === null || cached === null || total === null ||
|
|
149
|
+
(t.thoughts !== undefined && t.thoughts !== null && thoughts === null) ||
|
|
150
|
+
(t.tool !== undefined && t.tool !== null && tool === null) ||
|
|
151
|
+
cached > input) {
|
|
152
|
+
result.unsupported++;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const known = input + output + (thoughts ?? 0) + (tool ?? 0);
|
|
156
|
+
// A total below its own parts is a record that contradicts itself.
|
|
157
|
+
if (total > 0 && total < known) { result.unsupported++; continue; }
|
|
158
|
+
let extraOutput = 0;
|
|
159
|
+
if (total > known) {
|
|
160
|
+
// Parts the recorder kept add up short of the total it also kept. With
|
|
161
|
+
// both breakdowns present that is a contradiction; without them the
|
|
162
|
+
// remainder is reasoning or tool input that went unrecorded, and output
|
|
163
|
+
// is the dearer reading.
|
|
164
|
+
if (thoughts !== null && tool !== null) { result.unsupported++; continue; }
|
|
165
|
+
extraOutput = total - known;
|
|
166
|
+
result.remainderAsOutput++;
|
|
167
|
+
}
|
|
168
|
+
if (input + output + (thoughts ?? 0) + (tool ?? 0) + extraOutput === 0) continue;
|
|
169
|
+
if (typeof reply.model !== 'string' || reply.model.trim() === '') { result.missingModel++; continue; }
|
|
170
|
+
const ts = typeof reply.timestamp === 'string' && Number.isFinite(Date.parse(reply.timestamp))
|
|
171
|
+
? reply.timestamp
|
|
172
|
+
: undefined;
|
|
173
|
+
const owner = sessionOf.get(key) ?? null;
|
|
174
|
+
result.records.push({
|
|
175
|
+
model: reply.model,
|
|
176
|
+
...(ts === undefined ? {} : { ts }),
|
|
177
|
+
...(owner === null ? {} : { session: owner }),
|
|
178
|
+
...(options.label === undefined ? {} : { label: options.label }),
|
|
179
|
+
usage: {
|
|
180
|
+
input_tokens: input - cached + (tool ?? 0),
|
|
181
|
+
output_tokens: output + (thoughts ?? 0) + extraOutput,
|
|
182
|
+
cache_read_input_tokens: cached,
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
}
|
package/src/harnesses.ts
CHANGED
|
@@ -9,8 +9,8 @@ export interface HarnessCapability {
|
|
|
9
9
|
readonly id: string;
|
|
10
10
|
readonly name: string;
|
|
11
11
|
readonly nativeImport: {
|
|
12
|
-
readonly command: 'from-claude-code' | 'from-codex';
|
|
13
|
-
readonly format: 'claude-code-transcript-jsonl' | 'codex-session-jsonl';
|
|
12
|
+
readonly command: 'from-claude-code' | 'from-codex' | 'from-gemini-cli';
|
|
13
|
+
readonly format: 'claude-code-transcript-jsonl' | 'codex-session-jsonl' | 'gemini-cli-session-jsonl';
|
|
14
14
|
} | null;
|
|
15
15
|
readonly normalizedUsage: { readonly command: 'bill'; readonly format: 'trazum-usage-jsonl' };
|
|
16
16
|
readonly billingMode: 'unknown';
|
|
@@ -42,6 +42,7 @@ export const HARNESS_CAPABILITIES: HarnessCapabilities = Object.freeze({
|
|
|
42
42
|
harnesses: Object.freeze([
|
|
43
43
|
harness('claude-code', 'Claude Code', { command: 'from-claude-code', format: 'claude-code-transcript-jsonl' }),
|
|
44
44
|
harness('codex', 'Codex', { command: 'from-codex', format: 'codex-session-jsonl' }),
|
|
45
|
+
harness('gemini-cli', 'Gemini CLI', { command: 'from-gemini-cli', format: 'gemini-cli-session-jsonl' }),
|
|
45
46
|
harness('cursor', 'Cursor', null),
|
|
46
47
|
harness('mcp-client', 'Generic MCP client', null),
|
|
47
48
|
]),
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export * from './types.js';
|
|
2
2
|
export { MAX_INPUT_CHARS } from './limits.js';
|
|
3
|
+
export { analyzeTask, parseTaskReport, compareTasks, taskPolicy, TASK_POLICY_INSTRUCTIONS } from './task.js';
|
|
4
|
+
export type { TaskCategory, TaskComparison, TaskFinding, TaskFormat, TaskInput, TaskOutcome, TaskPolicy, TaskPolicyId, TaskReport } from './task.js';
|
|
3
5
|
export {
|
|
4
6
|
ESTIMATE_ERROR_BAND_PCT,
|
|
5
7
|
BAND_CALIBRATED_PROVIDER,
|
|
@@ -15,12 +17,16 @@ export {
|
|
|
15
17
|
profileUsage,
|
|
16
18
|
sharesOf,
|
|
17
19
|
spendByMonth,
|
|
20
|
+
spendByPeriod,
|
|
18
21
|
} from './usage.js';
|
|
19
22
|
export type {
|
|
20
23
|
CacheEconomics,
|
|
21
24
|
CacheVerdict,
|
|
22
25
|
MonthSpend,
|
|
23
26
|
SpendByMonth,
|
|
27
|
+
BillPeriodUnit,
|
|
28
|
+
PeriodSpend,
|
|
29
|
+
SpendByPeriod,
|
|
24
30
|
UsageProfileOptions,
|
|
25
31
|
UsageBreakdown,
|
|
26
32
|
FieldCoverage,
|
|
@@ -666,6 +672,9 @@ export type {
|
|
|
666
672
|
|
|
667
673
|
export { codexRecords, looksLikeCodexTranscript } from './codex.js';
|
|
668
674
|
export type { CodexRecord, CodexConversion } from './codex.js';
|
|
675
|
+
// Gemini CLI session files, read against the recorder's own source. See gemini-cli.ts.
|
|
676
|
+
export { geminiCliRecords, looksLikeGeminiCliSession } from './gemini-cli.js';
|
|
677
|
+
export type { GeminiCliRecord, GeminiCliConversion } from './gemini-cli.js';
|
|
669
678
|
|
|
670
679
|
export { HARNESS_CAPABILITIES, getHarnessCapability } from './harnesses.js';
|
|
671
680
|
export type { HarnessCapability, HarnessCapabilities } from './harnesses.js';
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { claudeCodeRecords } from './claude-code.js';
|
|
2
|
+
import { codexRecords } from './codex.js';
|
|
3
|
+
import { MAX_INPUT_CHARS } from './limits.js';
|
|
4
|
+
import { BUNDLED_CATALOGUE } from './pricing.js';
|
|
5
|
+
import { parseUsageLine, profileUsage } from './usage.js';
|
|
6
|
+
import { TASK_LIMITATIONS } from './task-types.js';
|
|
7
|
+
import type { TaskCategory, TaskFinding, TaskFormat, TaskInput, TaskReport } from './task-types.js';
|
|
8
|
+
|
|
9
|
+
export const taskObject = (value: unknown): value is Record<string, unknown> =>
|
|
10
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
11
|
+
|
|
12
|
+
export function taskError(): never { throw new Error('Invalid task data. See the task workflow schema and limits.'); }
|
|
13
|
+
|
|
14
|
+
export function taskKeys(value: unknown, allowed: readonly string[]): asserts value is Record<string, unknown> {
|
|
15
|
+
if (!taskObject(value) || Object.keys(value).some((key) => !allowed.includes(key))) taskError();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const taskText = (value: unknown, max: number): value is string =>
|
|
19
|
+
typeof value === 'string' && value.trim() !== '' && value.length <= max && !/[\u0000-\u001f\u007f]/u.test(value);
|
|
20
|
+
export const taskId = (value: unknown): value is string =>
|
|
21
|
+
typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9._-]{0,79}$/u.test(value);
|
|
22
|
+
|
|
23
|
+
function validateInput(value: unknown): TaskInput {
|
|
24
|
+
taskKeys(value, ['taskId', 'taskType', 'criterion', 'policyId', 'outcome', 'assessor', 'sources']);
|
|
25
|
+
if (!taskId(value.taskId) || !taskId(value.taskType) || !taskText(value.criterion, 500) ||
|
|
26
|
+
(value.policyId !== undefined && value.policyId !== 'baseline' && value.policyId !== 'repeat-review-v1') ||
|
|
27
|
+
(value.outcome !== undefined && (typeof value.outcome !== 'string' || !['accepted', 'rejected', 'unknown'].includes(value.outcome))) ||
|
|
28
|
+
(value.assessor !== undefined && !taskText(value.assessor, 100)) ||
|
|
29
|
+
((value.outcome === 'accepted' || value.outcome === 'rejected') && !taskText(value.assessor, 100)) ||
|
|
30
|
+
!Array.isArray(value.sources) || value.sources.length < 1 || value.sources.length > 16) taskError();
|
|
31
|
+
let size = 0;
|
|
32
|
+
for (const source of value.sources) {
|
|
33
|
+
taskKeys(source, ['format', 'text']);
|
|
34
|
+
if ((source.format !== 'claude-code' && source.format !== 'codex') || typeof source.text !== 'string' || !source.text.trim()) taskError();
|
|
35
|
+
size += source.text.length;
|
|
36
|
+
if (size > MAX_INPUT_CHARS) taskError();
|
|
37
|
+
}
|
|
38
|
+
return value as unknown as TaskInput;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Only used to refuse possible reuse. A collision withholds a verdict. */
|
|
42
|
+
function fingerprint(text: string): string {
|
|
43
|
+
let a = 0x811c9dc5;
|
|
44
|
+
let b = 0x9e3779b9;
|
|
45
|
+
for (let i = 0; i < text.length; i++) {
|
|
46
|
+
a = Math.imul(a ^ text.charCodeAt(i), 0x01000193);
|
|
47
|
+
b = Math.imul(b ^ text.charCodeAt(i), 0x85ebca6b);
|
|
48
|
+
}
|
|
49
|
+
return `${(a >>> 0).toString(16).padStart(8, '0')}${(b >>> 0).toString(16).padStart(8, '0')}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function canonical(value: unknown, depth = 0): string {
|
|
53
|
+
if (depth > 40) taskError();
|
|
54
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean') return JSON.stringify(value);
|
|
55
|
+
if (typeof value === 'number' && Number.isFinite(value)) return JSON.stringify(value);
|
|
56
|
+
if (Array.isArray(value)) return `[${value.map((item) => canonical(item, depth + 1)).join(',')}]`;
|
|
57
|
+
if (taskObject(value)) return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonical(value[key], depth + 1)}`).join(',')}}`;
|
|
58
|
+
taskError();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function category(name: string): TaskCategory {
|
|
62
|
+
if (['Read', 'read_file', 'read_text_file'].includes(name)) return 'read';
|
|
63
|
+
if (['Grep', 'Glob', 'grep', 'search', 'search_files', 'list_files'].includes(name)) return 'search';
|
|
64
|
+
if (['Bash', 'exec_command', 'shell_command', 'shell'].includes(name)) return 'shell';
|
|
65
|
+
return 'other';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type Call = { key: string; name: string; namespace: string | null; category: TaskCategory; line: number; resultLine?: number; result?: string; failed?: boolean; conflict: boolean };
|
|
69
|
+
type ParsedSource = {
|
|
70
|
+
sessions: Set<string>;
|
|
71
|
+
missingSession: boolean;
|
|
72
|
+
calls: Map<string, Call>;
|
|
73
|
+
duplicates: number;
|
|
74
|
+
unsupported: number;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
function inspectTools(text: string, format: TaskFormat): ParsedSource {
|
|
78
|
+
const state: ParsedSource = { sessions: new Set(), missingSession: false, calls: new Map(), duplicates: 0, unsupported: 0 };
|
|
79
|
+
let codexSession: string | null = null;
|
|
80
|
+
const requests = new Map<string, string>();
|
|
81
|
+
const results = new Map<string, { result: string; failed: boolean; line: number; conflict: boolean; name?: string; namespace?: string }>();
|
|
82
|
+
const sessionFor = (row: Record<string, unknown>): string | null => {
|
|
83
|
+
const session = format === 'codex' ? codexSession : typeof row.sessionId === 'string' && row.sessionId.trim() ? row.sessionId : null;
|
|
84
|
+
if (session !== null) state.sessions.add(session);
|
|
85
|
+
return session;
|
|
86
|
+
};
|
|
87
|
+
const addCall = (session: string | null, id: unknown, name: unknown, args: unknown, line: number, namespace: string | null = null) => {
|
|
88
|
+
if (session === null) { state.missingSession = true; state.unsupported++; return; }
|
|
89
|
+
if (typeof id !== 'string' || !id || typeof name !== 'string' || !name) { state.unsupported++; return; }
|
|
90
|
+
let signature: string;
|
|
91
|
+
try { signature = canonical([session, namespace, name, args]); } catch { state.unsupported++; return; }
|
|
92
|
+
const callKey = canonical([session, id]);
|
|
93
|
+
const existing = state.calls.get(callKey);
|
|
94
|
+
if (existing) {
|
|
95
|
+
state.duplicates++;
|
|
96
|
+
if (existing.key !== signature) { existing.conflict = true; state.unsupported++; }
|
|
97
|
+
} else state.calls.set(callKey, { key: signature, name, namespace, category: category(name), line, conflict: false });
|
|
98
|
+
};
|
|
99
|
+
const addResult = (session: string | null, id: unknown, output: unknown, failed: boolean, line: number, name?: string, namespace?: string) => {
|
|
100
|
+
if (session === null) { state.missingSession = true; state.unsupported++; return; }
|
|
101
|
+
if (typeof id !== 'string' || !id || output === undefined) { state.unsupported++; return; }
|
|
102
|
+
let serialized: string;
|
|
103
|
+
try { serialized = canonical(output); } catch { state.unsupported++; return; }
|
|
104
|
+
const key = canonical([session, id]);
|
|
105
|
+
const existing = results.get(key);
|
|
106
|
+
if (existing && (existing.result !== serialized || existing.failed !== failed || existing.name !== name || existing.namespace !== namespace)) {
|
|
107
|
+
existing.conflict = true;
|
|
108
|
+
state.unsupported++;
|
|
109
|
+
} else if (!existing) results.set(key, { result: serialized, failed, line, conflict: false, name, namespace });
|
|
110
|
+
};
|
|
111
|
+
const lines = text.split('\n');
|
|
112
|
+
for (let i = 0; i < lines.length; i++) {
|
|
113
|
+
if (!lines[i]!.trim()) continue;
|
|
114
|
+
let row: unknown;
|
|
115
|
+
try { row = JSON.parse(lines[i]!); } catch { continue; }
|
|
116
|
+
if (!taskObject(row)) continue;
|
|
117
|
+
if (format === 'codex') {
|
|
118
|
+
if (!taskObject(row.payload)) continue;
|
|
119
|
+
const payload = row.payload;
|
|
120
|
+
if (row.type === 'session_meta') {
|
|
121
|
+
codexSession = typeof payload.id === 'string' && payload.id.trim() ? payload.id : null;
|
|
122
|
+
if (codexSession !== null) state.sessions.add(codexSession);
|
|
123
|
+
else state.missingSession = true;
|
|
124
|
+
}
|
|
125
|
+
if (row.type === 'event_msg' && payload.type === 'token_count' && codexSession === null) state.missingSession = true;
|
|
126
|
+
if (row.type !== 'response_item') continue;
|
|
127
|
+
if (payload.type === 'function_call') {
|
|
128
|
+
if ((payload.namespace !== undefined && payload.namespace !== null && typeof payload.namespace !== 'string') ||
|
|
129
|
+
(payload.encrypted_function_args !== undefined && payload.encrypted_function_args !== null)) { state.unsupported++; continue; }
|
|
130
|
+
let args: unknown;
|
|
131
|
+
try { if (typeof payload.arguments !== 'string') throw new Error(); args = JSON.parse(payload.arguments); }
|
|
132
|
+
catch { state.unsupported++; continue; }
|
|
133
|
+
addCall(codexSession, payload.call_id, payload.name, args, i + 1, typeof payload.namespace === 'string' ? payload.namespace : null);
|
|
134
|
+
} else if (payload.type === 'function_call_output') {
|
|
135
|
+
// Codex records textual output. Prose mentioning a failure is not an exit status.
|
|
136
|
+
if (typeof payload.output !== 'string' ||
|
|
137
|
+
(payload.namespace !== undefined && payload.namespace !== null && typeof payload.namespace !== 'string') ||
|
|
138
|
+
(payload.name !== undefined && payload.name !== null && typeof payload.name !== 'string')) { state.unsupported++; continue; }
|
|
139
|
+
addResult(codexSession, payload.call_id, payload.output, false, i + 1,
|
|
140
|
+
typeof payload.name === 'string' ? payload.name : undefined,
|
|
141
|
+
typeof payload.namespace === 'string' ? payload.namespace : undefined);
|
|
142
|
+
} else if (typeof payload.type === 'string' && /_call(?:_output)?$/u.test(payload.type)) {
|
|
143
|
+
state.unsupported++;
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
const session = sessionFor(row);
|
|
147
|
+
if (row.type !== 'assistant' && row.type !== 'user') continue;
|
|
148
|
+
if (session === null) state.missingSession = true;
|
|
149
|
+
if (row.type === 'assistant' && typeof row.requestId === 'string' && session !== null) {
|
|
150
|
+
const previous = requests.get(row.requestId);
|
|
151
|
+
if (previous !== undefined && previous !== session) throw new Error('Ambiguous Claude request identifiers across sessions. Supply separate session sources.');
|
|
152
|
+
requests.set(row.requestId, session);
|
|
153
|
+
}
|
|
154
|
+
if (!taskObject(row.message) || !Array.isArray(row.message.content)) continue;
|
|
155
|
+
for (const block of row.message.content) {
|
|
156
|
+
if (!taskObject(block)) continue;
|
|
157
|
+
if (row.type === 'assistant' && block.type === 'tool_use') addCall(session, block.id, block.name, block.input, i + 1);
|
|
158
|
+
if (row.type === 'user' && block.type === 'tool_result') {
|
|
159
|
+
if (block.is_error !== undefined && typeof block.is_error !== 'boolean') { state.unsupported++; continue; }
|
|
160
|
+
addResult(session, block.tool_use_id, block.content, block.is_error === true, i + 1);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
for (const [key, call] of state.calls) {
|
|
166
|
+
const result = results.get(key);
|
|
167
|
+
if (result && ((result.name !== undefined && result.name !== call.name) || (result.namespace !== undefined && result.namespace !== call.namespace))) {
|
|
168
|
+
state.unsupported++;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (result && result.line > call.line && !result.conflict && !call.conflict) {
|
|
172
|
+
call.result = result.result;
|
|
173
|
+
call.resultLine = result.line;
|
|
174
|
+
call.failed = result.failed;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
for (const key of results.keys()) if (!state.calls.has(key)) state.unsupported++;
|
|
178
|
+
return state;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Analyze caller-supplied native transcripts in memory. No content is executed. */
|
|
182
|
+
export function analyzeTask(input: unknown): TaskReport {
|
|
183
|
+
const value = validateInput(input);
|
|
184
|
+
const report: TaskReport = {
|
|
185
|
+
version: 1, kind: 'trazum-task',
|
|
186
|
+
task: { id: value.taskId, type: value.taskType, criterion: value.criterion, policyId: value.policyId ?? 'baseline', outcome: value.outcome ?? 'unknown', assessor: value.assessor ?? null },
|
|
187
|
+
usage: {
|
|
188
|
+
records: 0, inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0, totalTokens: 0, apiEquivalentUsd: null, unpricedRecords: 0,
|
|
189
|
+
pricing: { source: 'bundled', lastReviewed: BUNDLED_CATALOGUE.lastReviewed, basis: 'api-equivalent' },
|
|
190
|
+
},
|
|
191
|
+
sources: [], coverage: { harnesses: [], models: [], gaps: [] },
|
|
192
|
+
tools: { calls: 0, duplicateCalls: 0, missingResults: 0, unsupported: 0, findings: [] },
|
|
193
|
+
limitations: [...TASK_LIMITATIONS],
|
|
194
|
+
};
|
|
195
|
+
const seenSessions = new Set<string>();
|
|
196
|
+
const seenSources = new Set<string>();
|
|
197
|
+
const missingByFormat = new Set<TaskFormat>();
|
|
198
|
+
const presentByFormat = new Set<TaskFormat>();
|
|
199
|
+
const models = new Set<string>();
|
|
200
|
+
const gaps = new Set<string>();
|
|
201
|
+
const usageLines: string[] = [];
|
|
202
|
+
for (let i = 0; i < value.sources.length; i++) {
|
|
203
|
+
const { format, text } = value.sources[i]!;
|
|
204
|
+
const inspected = inspectTools(text, format);
|
|
205
|
+
const sourceKey = fingerprint(`${format}:${text}`);
|
|
206
|
+
if (seenSources.has(sourceKey)) throw new Error('Duplicate task sources are not supported. Supply each session once.');
|
|
207
|
+
seenSources.add(sourceKey);
|
|
208
|
+
if (inspected.sessions.size === 0) inspected.missingSession = true;
|
|
209
|
+
if ((inspected.missingSession && presentByFormat.has(format)) || missingByFormat.has(format)) {
|
|
210
|
+
throw new Error('Multiple sources from the same harness require explicit, disjoint sessions.');
|
|
211
|
+
}
|
|
212
|
+
presentByFormat.add(format);
|
|
213
|
+
if (inspected.missingSession) { missingByFormat.add(format); gaps.add('missing-session'); }
|
|
214
|
+
const sessionKeys = [...inspected.sessions].map((session) => fingerprint(`${format}:${session}`)).sort();
|
|
215
|
+
for (const key of sessionKeys) {
|
|
216
|
+
if (seenSessions.has(key)) throw new Error('Overlapping task sessions are not supported. Supply each session once.');
|
|
217
|
+
seenSessions.add(key);
|
|
218
|
+
}
|
|
219
|
+
const conversion = format === 'claude-code' ? claudeCodeRecords(text) : codexRecords(text);
|
|
220
|
+
const { records, ...diagnostics } = conversion;
|
|
221
|
+
report.sources.push({ index: i + 1, format, fingerprint: sourceKey, sessionKeys, sessionCount: sessionKeys.length, diagnostics, completeness: 'not-established' });
|
|
222
|
+
if (conversion.unparseable) gaps.add('malformed-lines');
|
|
223
|
+
if ('unsupported' in conversion && (conversion.unsupported || conversion.missingModel)) gaps.add('incomplete-usage');
|
|
224
|
+
if ('assistantWithoutUsage' in conversion) {
|
|
225
|
+
if (conversion.assistantWithoutUsage) gaps.add('incomplete-usage');
|
|
226
|
+
if (conversion.disagreements) gaps.add('usage-disagreement');
|
|
227
|
+
if (conversion.noRequestId) gaps.add('missing-request-id');
|
|
228
|
+
}
|
|
229
|
+
if (!records.length) gaps.add('missing-usage');
|
|
230
|
+
for (const record of records) {
|
|
231
|
+
const line = JSON.stringify(record);
|
|
232
|
+
const parsed = parseUsageLine(line);
|
|
233
|
+
if (!parsed || ![parsed.inputTokens, parsed.outputTokens, parsed.cacheReadTokens, parsed.cacheWrite5mTokens, parsed.cacheWrite1hTokens].every((amount) => Number.isSafeInteger(amount) && amount >= 0)) {
|
|
234
|
+
gaps.add('incomplete-usage'); continue;
|
|
235
|
+
}
|
|
236
|
+
const model = BUNDLED_CATALOGUE.byId.get(parsed.model);
|
|
237
|
+
if (model) models.add(model.id);
|
|
238
|
+
else { gaps.add('unrecognized-model'); gaps.add('unpriced-model'); }
|
|
239
|
+
if (parsed.ts === null) gaps.add('missing-timestamp');
|
|
240
|
+
usageLines.push(line);
|
|
241
|
+
}
|
|
242
|
+
report.tools.calls += inspected.calls.size;
|
|
243
|
+
report.tools.duplicateCalls += inspected.duplicates;
|
|
244
|
+
report.tools.unsupported += inspected.unsupported;
|
|
245
|
+
const groups = new Map<string, Call[]>();
|
|
246
|
+
for (const call of inspected.calls.values()) {
|
|
247
|
+
if (call.result === undefined) { report.tools.missingResults++; continue; }
|
|
248
|
+
const key = canonical([call.key, call.result, call.failed]);
|
|
249
|
+
const group = groups.get(key) ?? [];
|
|
250
|
+
group.push(call); groups.set(key, group);
|
|
251
|
+
}
|
|
252
|
+
for (const group of groups.values()) {
|
|
253
|
+
if (group.length < 2) continue;
|
|
254
|
+
const first = group[0]!;
|
|
255
|
+
// Arbitrary tool names are never copied or assigned an optimization policy.
|
|
256
|
+
if (first.category === 'other') continue;
|
|
257
|
+
if (report.tools.findings.length >= 100) { gaps.add('findings-truncated'); continue; }
|
|
258
|
+
const finding: TaskFinding = {
|
|
259
|
+
kind: first.failed ? 'repeated-failure' : 'repeated-result', category: first.category,
|
|
260
|
+
count: group.length,
|
|
261
|
+
occurrences: group.slice(0, 20).map((call) => ({ source: i + 1, line: call.line, resultLine: call.resultLine! })),
|
|
262
|
+
};
|
|
263
|
+
if (group.length > 20) gaps.add('findings-truncated');
|
|
264
|
+
report.tools.findings.push(finding);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const profile = profileUsage(usageLines.join('\n'), { catalogue: BUNDLED_CATALOGUE });
|
|
268
|
+
for (const name of ['inputTokens', 'outputTokens', 'cacheReadTokens', 'cacheWriteTokens'] as const) {
|
|
269
|
+
report.usage[name] = profile.total[name] + profile.unpriced[name];
|
|
270
|
+
if (!Number.isSafeInteger(report.usage[name])) throw new Error('Task usage exceeds safe numeric limits.');
|
|
271
|
+
}
|
|
272
|
+
report.usage.totalTokens = report.usage.inputTokens + report.usage.outputTokens + report.usage.cacheReadTokens + report.usage.cacheWriteTokens;
|
|
273
|
+
if (!Number.isSafeInteger(report.usage.totalTokens)) throw new Error('Task usage exceeds safe numeric limits.');
|
|
274
|
+
report.usage.records = profile.total.calls + profile.unpriced.calls;
|
|
275
|
+
if (report.usage.records === 0) gaps.add('missing-usage');
|
|
276
|
+
report.usage.unpricedRecords = profile.unpriced.calls;
|
|
277
|
+
if (profile.total.assumedWriteTtlCalls) gaps.add('assumed-cache-ttl');
|
|
278
|
+
if (profile.skippedLines.length) gaps.add('incomplete-usage');
|
|
279
|
+
if (report.tools.missingResults || report.tools.unsupported) gaps.add('incomplete-tools');
|
|
280
|
+
if (report.usage.records && !report.usage.unpricedRecords && !gaps.has('assumed-cache-ttl')) report.usage.apiEquivalentUsd = profile.total.totalUsd;
|
|
281
|
+
report.coverage = { harnesses: [...presentByFormat].sort(), models: [...models].sort(), gaps: [...gaps].sort() };
|
|
282
|
+
return report;
|
|
283
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
export type TaskFormat = 'claude-code' | 'codex';
|
|
2
|
+
export type TaskOutcome = 'accepted' | 'rejected' | 'unknown';
|
|
3
|
+
export type TaskPolicyId = 'baseline' | 'repeat-review-v1';
|
|
4
|
+
export type TaskCategory = 'read' | 'search' | 'shell' | 'other';
|
|
5
|
+
|
|
6
|
+
export interface TaskInput {
|
|
7
|
+
taskId: string;
|
|
8
|
+
taskType: string;
|
|
9
|
+
criterion: string;
|
|
10
|
+
policyId?: TaskPolicyId;
|
|
11
|
+
outcome?: TaskOutcome;
|
|
12
|
+
assessor?: string;
|
|
13
|
+
sources: Array<{ format: TaskFormat; text: string }>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface TaskFinding {
|
|
17
|
+
kind: 'repeated-result' | 'repeated-failure';
|
|
18
|
+
category: TaskCategory;
|
|
19
|
+
count: number;
|
|
20
|
+
occurrences: Array<{ source: number; line: number; resultLine: number }>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface TaskReport {
|
|
24
|
+
version: 1;
|
|
25
|
+
kind: 'trazum-task';
|
|
26
|
+
task: {
|
|
27
|
+
id: string;
|
|
28
|
+
type: string;
|
|
29
|
+
criterion: string;
|
|
30
|
+
policyId: TaskPolicyId;
|
|
31
|
+
outcome: TaskOutcome;
|
|
32
|
+
assessor: string | null;
|
|
33
|
+
};
|
|
34
|
+
usage: {
|
|
35
|
+
records: number;
|
|
36
|
+
inputTokens: number;
|
|
37
|
+
outputTokens: number;
|
|
38
|
+
cacheReadTokens: number;
|
|
39
|
+
cacheWriteTokens: number;
|
|
40
|
+
totalTokens: number;
|
|
41
|
+
apiEquivalentUsd: number | null;
|
|
42
|
+
unpricedRecords: number;
|
|
43
|
+
pricing: { source: 'bundled'; lastReviewed: string; basis: 'api-equivalent' };
|
|
44
|
+
};
|
|
45
|
+
sources: Array<{
|
|
46
|
+
index: number;
|
|
47
|
+
format: TaskFormat;
|
|
48
|
+
/** Non-cryptographic reuse checks. These do not authenticate reports. */
|
|
49
|
+
fingerprint: string;
|
|
50
|
+
sessionKeys: string[];
|
|
51
|
+
sessionCount: number;
|
|
52
|
+
diagnostics: Record<string, number | { line: number; records: number }>;
|
|
53
|
+
completeness: 'not-established';
|
|
54
|
+
}>;
|
|
55
|
+
coverage: { harnesses: TaskFormat[]; models: string[]; gaps: string[] };
|
|
56
|
+
tools: {
|
|
57
|
+
calls: number;
|
|
58
|
+
duplicateCalls: number;
|
|
59
|
+
missingResults: number;
|
|
60
|
+
unsupported: number;
|
|
61
|
+
findings: TaskFinding[];
|
|
62
|
+
};
|
|
63
|
+
limitations: string[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface TaskComparison {
|
|
67
|
+
version: 1;
|
|
68
|
+
kind: 'trazum-task-comparison';
|
|
69
|
+
verdict: 'lower-usage' | 'higher-usage' | 'similar-usage' | 'quality-regressed' | 'inconclusive';
|
|
70
|
+
reasons: string[];
|
|
71
|
+
tokenChange: { before: number; after: number; delta: number; percent: number | null } | null;
|
|
72
|
+
limitations: string[];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const TASK_LIMITATIONS = [
|
|
76
|
+
'Source completeness is not established. Only supplied records are measured.',
|
|
77
|
+
'API-equivalent valuation is not an invoice, subscription charge or quota measurement.',
|
|
78
|
+
'Identical tool calls and recorded results can be deliberate verification; repetition does not prove waste or unchanged external state.',
|
|
79
|
+
'Outcomes, criteria and policy application are caller declarations, not independently verified quality or execution evidence.',
|
|
80
|
+
'A paired observation is not a causal experiment. No monetary savings are inferred.',
|
|
81
|
+
'Reuse fingerprints do not authenticate reports or guarantee anonymization.',
|
|
82
|
+
] as const;
|
|
83
|
+
|
|
84
|
+
export const TASK_GAPS = [
|
|
85
|
+
'missing-session', 'missing-usage', 'malformed-lines', 'incomplete-usage',
|
|
86
|
+
'usage-disagreement', 'missing-request-id', 'unpriced-model', 'unrecognized-model',
|
|
87
|
+
'missing-timestamp', 'assumed-cache-ttl', 'incomplete-tools', 'findings-truncated',
|
|
88
|
+
] as const;
|