crysk 0.4.0 → 0.5.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/README.md +26 -17
- package/config/models/anthropic.json +1 -1
- package/dist/SKILL-k4k0zn1w.md +143 -0
- package/dist/SKILL-vhb4c3vv.md +553 -0
- package/dist/SKILL-xpf6kmdw.md +74 -0
- package/dist/ci-admission-0ken6ehx.md +128 -0
- package/dist/cli.js +3056 -1623
- package/dist/index.js +1744 -938
- package/dist/opencode-ci-ac5zksdn.md +106 -0
- package/dist/workflow-4pkyck01.yml +160 -0
- package/package.json +1 -1
- package/prompts/default/PROVENANCE.md +1 -0
- package/skills/crysk/SKILL.md +16 -16
- package/skills/crysk-ci-setup/SKILL.md +41 -38
- package/skills/crysk-ci-setup/assets/workflow.yml +31 -22
- package/skills/crysk-ci-setup/references/ci-admission.md +1 -1
- package/skills/crysk-ci-setup/references/opencode-ci.md +53 -88
- package/skills/crysk-loop/SKILL.md +1 -1
- package/src/asset-manifest.ts +0 -4
- package/src/assets.ts +1 -1
- package/src/ci/admission-ledger.ts +4 -0
- package/src/ci/classify-error.ts +21 -24
- package/src/ci/cli-record.ts +53 -1
- package/src/ci/crysk-secrets.ts +409 -0
- package/src/ci/gates.ts +22 -36
- package/src/ci/outcome.ts +421 -330
- package/src/ci/platform-config.ts +2 -2
- package/src/ci/platform-report.ts +274 -41
- package/src/ci/platform-trace.ts +551 -0
- package/src/ci/platform.ts +28 -12
- package/src/ci/publish.ts +19 -6
- package/src/ci/review-levels.ts +8 -7
- package/src/ci/run.ts +88 -0
- package/src/ci/setup.ts +453 -118
- package/src/ci/trace-bundle.ts +1282 -0
- package/src/ci/trace-transcripts.ts +415 -0
- package/src/ci/trigger.ts +176 -33
- package/src/cli.ts +11 -3
- package/src/commands/ci-setup-questions.ts +3 -3
- package/src/commands/ci-setup.ts +152 -6
- package/src/commands/config.ts +3 -0
- package/src/commands/doctor.ts +15 -6
- package/src/config/config.ts +16 -3
- package/src/diversity/clustering.ts +0 -1
- package/src/diversity/identity.ts +4 -5
- package/src/diversity/pipeline-integration.ts +1 -9
- package/src/doctor.ts +3 -3
- package/src/execution/attempt-evidence.ts +10 -5
- package/src/execution/claude-session-trace.ts +480 -0
- package/src/execution/contracts.ts +21 -4
- package/src/execution/harness.ts +105 -8
- package/src/execution/model-fallback.ts +239 -0
- package/src/execution/usage-normalized.ts +6 -4
- package/src/home-preflight.ts +1 -1
- package/src/index.ts +1 -1
- package/src/model/catalog.ts +13 -37
- package/src/model/free-discovery.ts +117 -8
- package/src/model/model-name.ts +404 -0
- package/src/model/provider-capabilities.ts +22 -7
- package/src/model/routing.ts +346 -467
- package/src/pr/admission.ts +18 -0
- package/src/pr/plan.ts +20 -4
- package/src/pr/preflight.ts +1 -1
- package/src/pr/publish-outcome.ts +1 -1
- package/src/pr/review-pr.ts +15 -1
- package/src/pr/target.ts +12 -3
- package/src/production-runtime.ts +760 -331
- package/src/review/pipeline.ts +277 -101
- package/src/review/preflight.ts +201 -294
- package/src/review/report.ts +26 -26
- package/src/review/review.ts +20 -3
- package/src/review/route-preflight.ts +79 -11
- package/src/review/run.ts +28 -6
- package/src/review/scout.ts +1 -1
- package/src/review/spec.ts +3 -3
- package/src/review/step-runner.ts +70 -1
- package/src/runner-authority.ts +127 -9
- package/src/security/binding-policy.ts +1 -2
- package/src/security/claude-credential.ts +355 -0
- package/src/security/credential-broker.ts +56 -40
- package/src/security/evidence-redaction.ts +179 -4
- package/src/security/opencode-secret-manifest.ts +114 -0
- package/src/security/redact.ts +45 -0
- package/src/system-tools.ts +48 -21
- package/src/transport-registry.ts +12 -2
- package/src/transports/claude-code-cli.ts +21 -2
- package/src/transports/opencode-sdk.ts +11 -9
- package/src/transports/opencode-server.ts +7 -10
- package/src/ui/config-edit.ts +9 -20
- package/src/ui/config.ts +32 -27
- package/src/ui/plan.ts +76 -47
- package/src/updater.ts +10 -7
- package/prompts/default/deep-review-lifecycle.md +0 -362
|
@@ -0,0 +1,1282 @@
|
|
|
1
|
+
// Builds the review trace a platform run uploads (crysk.trace.v1): one
|
|
2
|
+
// OpenTelemetry trace of the whole run in OTLP/JSON, plus the raw per-attempt
|
|
3
|
+
// transcripts it was built from. Pure: the trace step (src/ci/platform-trace.ts)
|
|
4
|
+
// reads the run directory and hands the files in; nothing here touches disk or
|
|
5
|
+
// the network.
|
|
6
|
+
//
|
|
7
|
+
// SHAPE (OpenTelemetry GenAI semantic conventions, GENAI_SEMCONV_VERSION):
|
|
8
|
+
// - a root span `invoke_workflow crysk.review` for the run;
|
|
9
|
+
// - a child `invoke_agent <step>` per pipeline step;
|
|
10
|
+
// - under it a `chat <model>` per attempt, carrying the model, provider, token
|
|
11
|
+
// usage (inclusive of cache and reasoning, as the conventions count it) and
|
|
12
|
+
// cost, so a backend sees each model call once;
|
|
13
|
+
// - under that an `execute_tool <tool>` per tool call the model made.
|
|
14
|
+
// Engine facts ride as `crysk.*` attributes with snake_case keys. Every leaf of
|
|
15
|
+
// each pipeline.json step lands on its step span (crysk.step.*), every leaf of
|
|
16
|
+
// the run's records on the root (crysk.pipeline.*, crysk.findings.*,
|
|
17
|
+
// crysk.cli.*), so nothing the run recorded is lost to the conversion.
|
|
18
|
+
//
|
|
19
|
+
// CONTENT (`content` = /runs/token's `trace_content`): prompts, outputs, tool
|
|
20
|
+
// arguments and results, status messages, the transcripts, and every string
|
|
21
|
+
// that is not a single word (a failure detail, a finding's claim) are content.
|
|
22
|
+
// With `content` false none of them is emitted. The platform strips again on
|
|
23
|
+
// its side by an allowlist (apps/api/src/runs/trace.ts); this side is the
|
|
24
|
+
// first of the two.
|
|
25
|
+
//
|
|
26
|
+
// REDACTION: every string the OTLP document carries passes the session copy's
|
|
27
|
+
// redaction (redactSessionText / redactSessionDocument in
|
|
28
|
+
// src/execution/claude-session-trace.ts), a single word as much as a prompt: a
|
|
29
|
+
// bare key is one word. One choke point holds it: an attribute's string can
|
|
30
|
+
// only be a `Redacted`, which only redactText and redactJson make, and span
|
|
31
|
+
// names, status messages and the transcripts' step and model go through them
|
|
32
|
+
// too. The Claude session transcripts are that copy already (the CLI's
|
|
33
|
+
// redactSessionJsonl, src/execution/claude-session-trace.ts), so their text is
|
|
34
|
+
// not redacted a second time; an OpenCode capture is redacted here, by key.
|
|
35
|
+
//
|
|
36
|
+
// BUILTINS ONLY: see trace-transcripts.ts.
|
|
37
|
+
|
|
38
|
+
import { createHash } from "node:crypto";
|
|
39
|
+
import { crc32, deflateRawSync, constants as zlibConstants } from "node:zlib";
|
|
40
|
+
import {
|
|
41
|
+
redactSessionDocument,
|
|
42
|
+
redactSessionText,
|
|
43
|
+
} from "../execution/claude-session-trace";
|
|
44
|
+
import { metadataOnlyFinding } from "./platform-report";
|
|
45
|
+
import {
|
|
46
|
+
type ChatMessage,
|
|
47
|
+
type Conversation,
|
|
48
|
+
openCodeConversation,
|
|
49
|
+
parseClaudeSession,
|
|
50
|
+
type ToolCall,
|
|
51
|
+
withoutContent,
|
|
52
|
+
} from "./trace-transcripts";
|
|
53
|
+
|
|
54
|
+
export const TRACE_SCHEMA = "crysk.trace.v1";
|
|
55
|
+
// The GenAI conventions are still in Development status. The message JSON
|
|
56
|
+
// (gen_ai.input.messages, gen_ai.output.messages, gen_ai.system_instructions)
|
|
57
|
+
// follows this version's schema:
|
|
58
|
+
// https://github.com/open-telemetry/semantic-conventions/blob/v1.41.0/docs/gen-ai/gen-ai-input-messages.json
|
|
59
|
+
export const GENAI_SEMCONV_VERSION = "1.41.0";
|
|
60
|
+
|
|
61
|
+
// The platform's caps (apps/api/src/runs/trace-body.ts): 413 beyond either.
|
|
62
|
+
export const TRACE_COMPRESSED_MAX_BYTES = 25 * 1024 * 1024;
|
|
63
|
+
export const TRACE_DECOMPRESSED_MAX_BYTES = 200 * 1024 * 1024;
|
|
64
|
+
|
|
65
|
+
export type TraceOutcome =
|
|
66
|
+
| "reviewed"
|
|
67
|
+
| "hunters-failed"
|
|
68
|
+
| "skipped"
|
|
69
|
+
| "yielded"
|
|
70
|
+
| "error";
|
|
71
|
+
|
|
72
|
+
export type TranscriptKind = "claude-session-jsonl" | "opencode-capture";
|
|
73
|
+
|
|
74
|
+
export interface TraceTranscript {
|
|
75
|
+
step: string;
|
|
76
|
+
attempt: number;
|
|
77
|
+
model: string;
|
|
78
|
+
kind: TranscriptKind;
|
|
79
|
+
text: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
declare const redactedBrand: unique symbol;
|
|
83
|
+
// A string that went through the secret redaction. Nothing else may become an
|
|
84
|
+
// attribute's string value.
|
|
85
|
+
type Redacted = string & { readonly [redactedBrand]: true };
|
|
86
|
+
|
|
87
|
+
function redactText(value: string): Redacted {
|
|
88
|
+
return redactSessionText(value) as Redacted;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// A document's strings, redacted by key and by text, as JSON.
|
|
92
|
+
function redactJson(value: unknown): Redacted {
|
|
93
|
+
return JSON.stringify(redactSessionDocument(value)) as Redacted;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
type AnyValue =
|
|
97
|
+
| { stringValue: string }
|
|
98
|
+
| { intValue: string }
|
|
99
|
+
| { doubleValue: number }
|
|
100
|
+
| { boolValue: boolean }
|
|
101
|
+
| { arrayValue: { values: AnyValue[] } };
|
|
102
|
+
|
|
103
|
+
export interface KeyValue {
|
|
104
|
+
key: string;
|
|
105
|
+
value: AnyValue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface OtlpSpan {
|
|
109
|
+
traceId: string;
|
|
110
|
+
spanId: string;
|
|
111
|
+
parentSpanId?: string;
|
|
112
|
+
name: string;
|
|
113
|
+
kind: number;
|
|
114
|
+
startTimeUnixNano: string;
|
|
115
|
+
endTimeUnixNano: string;
|
|
116
|
+
attributes: KeyValue[];
|
|
117
|
+
status: { code: number; message?: string };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface TraceBundle {
|
|
121
|
+
schema: typeof TRACE_SCHEMA;
|
|
122
|
+
outcome: TraceOutcome;
|
|
123
|
+
code: string | null;
|
|
124
|
+
content: boolean;
|
|
125
|
+
truncated: boolean;
|
|
126
|
+
otlp: {
|
|
127
|
+
resourceSpans: Array<{
|
|
128
|
+
resource: { attributes: KeyValue[] };
|
|
129
|
+
scopeSpans: Array<{
|
|
130
|
+
scope: { name: string; version?: string };
|
|
131
|
+
spans: OtlpSpan[];
|
|
132
|
+
}>;
|
|
133
|
+
}>;
|
|
134
|
+
};
|
|
135
|
+
transcripts: TraceTranscript[];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface TraceAttemptInput {
|
|
139
|
+
attempt: number;
|
|
140
|
+
// steps/evidence.<step>.attempt<n>.json, parsed.
|
|
141
|
+
evidence?: unknown;
|
|
142
|
+
// steps/trace.<step>.attempt<n>.jsonl (Claude CLI), already redacted.
|
|
143
|
+
sessionJsonl?: string;
|
|
144
|
+
// steps/evidence.<step>.attempt<n>.capture.json (OpenCode).
|
|
145
|
+
openCodeCapture?: string;
|
|
146
|
+
// What a metadata trace reads of that transcript or capture instead of its
|
|
147
|
+
// text (src/ci/platform-trace.ts readRunSteps): tool calls without arguments
|
|
148
|
+
// or results, the finish reason, the response model and the times. Used
|
|
149
|
+
// only when neither text is present.
|
|
150
|
+
transcriptMetadata?: Conversation;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface TraceStepInput {
|
|
154
|
+
name: string;
|
|
155
|
+
// The step's system prompt file (steps/<name>.system.md or a shared one).
|
|
156
|
+
systemPrompt?: string;
|
|
157
|
+
// steps/<name>.body.md: a hunter's rendered body, used as the prompt when no
|
|
158
|
+
// transcript names one.
|
|
159
|
+
promptBody?: string;
|
|
160
|
+
// The final attempt's output file (the step's outPath).
|
|
161
|
+
output?: string;
|
|
162
|
+
attempts: TraceAttemptInput[];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export interface TraceRunInput {
|
|
166
|
+
outcome: TraceOutcome;
|
|
167
|
+
code: string | null;
|
|
168
|
+
// cli.json, pipeline.json and findings.json, parsed, when present.
|
|
169
|
+
cli?: unknown;
|
|
170
|
+
pipeline?: unknown;
|
|
171
|
+
findings?: unknown;
|
|
172
|
+
steps: TraceStepInput[];
|
|
173
|
+
prNumber?: number;
|
|
174
|
+
headSha?: string;
|
|
175
|
+
engineVersion?: string;
|
|
176
|
+
// When the run began and the trace was built, the bounds of the root span
|
|
177
|
+
// when no attempt recorded a wider time.
|
|
178
|
+
startedAtMs: number;
|
|
179
|
+
endedAtMs: number;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const SPAN_KIND_INTERNAL = 1;
|
|
183
|
+
const SPAN_KIND_CLIENT = 3;
|
|
184
|
+
const STATUS_UNSET = 0;
|
|
185
|
+
const STATUS_OK = 1;
|
|
186
|
+
const STATUS_ERROR = 2;
|
|
187
|
+
|
|
188
|
+
// A single printable word, at most 200 characters: an id, a model name, a
|
|
189
|
+
// status, a path. Anything else (prose, an error sentence, code) is content.
|
|
190
|
+
const WORD = /^[\x21-\x7e]{1,200}$/;
|
|
191
|
+
// The platform keeps a `crysk.*` key only when it is this short and this
|
|
192
|
+
// lowercase (CRYSK_KEY in apps/api/src/runs/trace.ts).
|
|
193
|
+
const MAX_CRYSK_KEY = 106;
|
|
194
|
+
// Generic flattening stops here, so one record cannot flood a span.
|
|
195
|
+
const MAX_DEPTH = 6;
|
|
196
|
+
const MAX_ARRAY_ITEMS = 100;
|
|
197
|
+
|
|
198
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
199
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// camelCase, kebab-case and dotted names, as snake_case.
|
|
203
|
+
export function snakeKey(name: string): string {
|
|
204
|
+
return name
|
|
205
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
|
206
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2")
|
|
207
|
+
.toLowerCase()
|
|
208
|
+
.replace(/[^a-z0-9_]+/g, "_");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
type NumberValue = { intValue: string } | { doubleValue: number };
|
|
212
|
+
type ScalarValue =
|
|
213
|
+
| NumberValue
|
|
214
|
+
| { boolValue: boolean }
|
|
215
|
+
| { arrayValue: { values: NumberValue[] } };
|
|
216
|
+
|
|
217
|
+
// Thrown when the content strings alone pass the budget: the content cannot
|
|
218
|
+
// fit, so encodeTrace stops building it and sends the metadata.
|
|
219
|
+
class ContentOverBudget extends Error {}
|
|
220
|
+
|
|
221
|
+
// The content characters a build may hold; each is at least one byte of JSON.
|
|
222
|
+
interface ContentBudget {
|
|
223
|
+
left: number;
|
|
224
|
+
// Redacting this much content leaves garbage worth collecting (encodeTrace).
|
|
225
|
+
settle?: { every: number; since: number; run: () => void };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
class Attributes {
|
|
229
|
+
readonly list: KeyValue[] = [];
|
|
230
|
+
constructor(
|
|
231
|
+
private readonly content: boolean,
|
|
232
|
+
private readonly budget?: ContentBudget,
|
|
233
|
+
) {}
|
|
234
|
+
|
|
235
|
+
private spend(value: Redacted): Redacted {
|
|
236
|
+
const budget = this.budget;
|
|
237
|
+
if (budget === undefined) return value;
|
|
238
|
+
budget.left -= value.length;
|
|
239
|
+
if (budget.left < 0) throw new ContentOverBudget();
|
|
240
|
+
const settle = budget.settle;
|
|
241
|
+
if (settle !== undefined) {
|
|
242
|
+
settle.since += value.length;
|
|
243
|
+
if (settle.since >= settle.every) {
|
|
244
|
+
settle.since = 0;
|
|
245
|
+
settle.run();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return value;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private set(key: string, value: AnyValue): void {
|
|
252
|
+
const at = this.list.findIndex((item) => item.key === key);
|
|
253
|
+
if (at === -1) this.list.push({ key, value });
|
|
254
|
+
else this.list[at] = { key, value };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// The only ways in: a string must be Redacted.
|
|
258
|
+
private put(key: string, value: ScalarValue): void {
|
|
259
|
+
this.set(key, value);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private putString(key: string, value: Redacted): void {
|
|
263
|
+
this.set(key, { stringValue: value });
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// What a metadata trace may keep of a string: one word before and after
|
|
267
|
+
// redaction. With content, anything, redacted.
|
|
268
|
+
private kept(value: string): Redacted | undefined {
|
|
269
|
+
if (!this.content && !WORD.test(value)) return undefined;
|
|
270
|
+
const safe = redactText(value);
|
|
271
|
+
return this.content || WORD.test(safe) ? safe : undefined;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// A word always; anything longer or with spaces only as content. Redacted
|
|
275
|
+
// either way.
|
|
276
|
+
text(key: string, value: string | undefined): void {
|
|
277
|
+
if (value === undefined || value === "") return;
|
|
278
|
+
const safe = this.kept(value);
|
|
279
|
+
if (safe !== undefined) this.putString(key, safe);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Content only, whatever its shape, redacted as text.
|
|
283
|
+
contentText(key: string, value: string | undefined): void {
|
|
284
|
+
if (this.content && value !== undefined) {
|
|
285
|
+
this.putString(key, this.spend(redactText(value)));
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Content only: a structured value as JSON, redacted by key and by text.
|
|
290
|
+
contentJson(key: string, value: unknown): void {
|
|
291
|
+
if (this.content && value !== undefined) {
|
|
292
|
+
this.putString(key, this.spend(redactJson(value)));
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
number(key: string, value: unknown): void {
|
|
297
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return;
|
|
298
|
+
this.put(
|
|
299
|
+
key,
|
|
300
|
+
Number.isSafeInteger(value)
|
|
301
|
+
? { intValue: String(value) }
|
|
302
|
+
: { doubleValue: value },
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
bool(key: string, value: unknown): void {
|
|
307
|
+
if (typeof value === "boolean") this.put(key, { boolValue: value });
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
words(key: string, values: string[]): void {
|
|
311
|
+
const kept = values
|
|
312
|
+
.map((value) => this.kept(value))
|
|
313
|
+
.filter((value): value is Redacted => value !== undefined);
|
|
314
|
+
if (kept.length > 0) {
|
|
315
|
+
this.set(key, {
|
|
316
|
+
arrayValue: { values: kept.map((value) => ({ stringValue: value })) },
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Every leaf of a record under `prefix`, keys snake_cased. Scalar arrays
|
|
322
|
+
// become array values; arrays of records are indexed.
|
|
323
|
+
flatten(prefix: string, value: unknown, depth = 0): void {
|
|
324
|
+
if (prefix.length > MAX_CRYSK_KEY) return;
|
|
325
|
+
if (typeof value === "string") this.text(prefix, value);
|
|
326
|
+
else if (typeof value === "number") this.number(prefix, value);
|
|
327
|
+
else if (typeof value === "boolean") this.bool(prefix, value);
|
|
328
|
+
else if (Array.isArray(value)) {
|
|
329
|
+
if (value.every((item) => typeof item === "string")) {
|
|
330
|
+
this.words(prefix, value as string[]);
|
|
331
|
+
} else if (value.every((item) => typeof item === "number")) {
|
|
332
|
+
const values = (value as number[]).filter(Number.isFinite);
|
|
333
|
+
if (values.length > 0) {
|
|
334
|
+
this.put(prefix, {
|
|
335
|
+
arrayValue: {
|
|
336
|
+
values: values.map((item) =>
|
|
337
|
+
Number.isSafeInteger(item)
|
|
338
|
+
? { intValue: String(item) }
|
|
339
|
+
: { doubleValue: item },
|
|
340
|
+
),
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
} else if (depth < MAX_DEPTH) {
|
|
345
|
+
value.slice(0, MAX_ARRAY_ITEMS).forEach((item, index) => {
|
|
346
|
+
this.flatten(`${prefix}.${index}`, item, depth + 1);
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
} else if (isRecord(value) && depth < MAX_DEPTH) {
|
|
350
|
+
for (const [key, item] of Object.entries(value)) {
|
|
351
|
+
this.flatten(`${prefix}.${snakeKey(key)}`, item, depth + 1);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function hex(seed: string, length: number): string {
|
|
358
|
+
return createHash("sha256").update(seed).digest("hex").slice(0, length);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function nanos(ms: number): string {
|
|
362
|
+
return (BigInt(Math.max(0, Math.round(ms))) * 1_000_000n).toString();
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function timeOf(value: unknown): number | undefined {
|
|
366
|
+
if (typeof value !== "string") return undefined;
|
|
367
|
+
const ms = Date.parse(value);
|
|
368
|
+
return Number.isFinite(ms) ? ms : undefined;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function str(value: unknown): string | undefined {
|
|
372
|
+
return typeof value === "string" && value !== "" ? value : undefined;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function num(value: unknown): number | undefined {
|
|
376
|
+
return typeof value === "number" && Number.isFinite(value)
|
|
377
|
+
? value
|
|
378
|
+
: undefined;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// A span name is `{operation} {target}`. The target, redacted, keeps the
|
|
382
|
+
// characters a backend reads as one token; anything else becomes `_`.
|
|
383
|
+
function spanName(operation: string, target: string | undefined): string {
|
|
384
|
+
if (target === undefined || target === "") return operation;
|
|
385
|
+
const safe = redactText(target);
|
|
386
|
+
return `${operation} ${safe.replace(/[^\x21-\x7e]/g, "_").slice(0, 200)}`;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
interface Window {
|
|
390
|
+
start?: number;
|
|
391
|
+
end?: number;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function widen(window: Window, start?: number, end?: number): void {
|
|
395
|
+
for (const ms of [start, end]) {
|
|
396
|
+
if (ms === undefined) continue;
|
|
397
|
+
if (window.start === undefined || ms < window.start) window.start = ms;
|
|
398
|
+
if (window.end === undefined || ms > window.end) window.end = ms;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// What one attempt's evidence says, read defensively: older engines wrote less.
|
|
403
|
+
interface AttemptFacts {
|
|
404
|
+
model?: string;
|
|
405
|
+
provider?: string;
|
|
406
|
+
completion?: string;
|
|
407
|
+
usage?: Record<string, unknown>;
|
|
408
|
+
start?: number;
|
|
409
|
+
end?: number;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function attemptFacts(evidence: unknown): AttemptFacts {
|
|
413
|
+
if (!isRecord(evidence)) return {};
|
|
414
|
+
const identity = isRecord(evidence.identity) ? evidence.identity : {};
|
|
415
|
+
const route = isRecord(identity.route) ? identity.route : {};
|
|
416
|
+
const outcome = isRecord(evidence.outcome) ? evidence.outcome : {};
|
|
417
|
+
const settlement = isRecord(evidence.settlement) ? evidence.settlement : {};
|
|
418
|
+
const times = isRecord(settlement.timestamps) ? settlement.timestamps : {};
|
|
419
|
+
const facts: AttemptFacts = {};
|
|
420
|
+
// The route's model is the one that ran: the Claude CLI's `--model` or the
|
|
421
|
+
// OpenCode model id. `executionModel` is the step's logical model, which a
|
|
422
|
+
// route plan may send elsewhere; it stays on the span as
|
|
423
|
+
// `crysk.attempt.identity.execution_model` and names only evidence that
|
|
424
|
+
// carries no route.
|
|
425
|
+
const model = str(route.modelSnapshot) ?? str(identity.executionModel);
|
|
426
|
+
if (model !== undefined) facts.model = model;
|
|
427
|
+
const provider = str(route.provider);
|
|
428
|
+
if (provider !== undefined) facts.provider = provider;
|
|
429
|
+
const completion = str(outcome.completion) ?? str(settlement.outcome);
|
|
430
|
+
if (completion !== undefined) facts.completion = completion;
|
|
431
|
+
if (isRecord(outcome.usage)) facts.usage = outcome.usage;
|
|
432
|
+
const start = timeOf(times.startedAt);
|
|
433
|
+
const end =
|
|
434
|
+
timeOf(times.settledAt) ??
|
|
435
|
+
timeOf(times.terminationConfirmedAt) ??
|
|
436
|
+
timeOf(times.leaseInvalidatedAt);
|
|
437
|
+
if (start !== undefined) facts.start = start;
|
|
438
|
+
if (end !== undefined) facts.end = end;
|
|
439
|
+
return facts;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function sumKnown(...values: unknown[]): number | undefined {
|
|
443
|
+
const known = values.map(num).filter((v): v is number => v !== undefined);
|
|
444
|
+
return known.length === 0 ? undefined : known.reduce((a, b) => a + b, 0);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// The engine's usage leaves are disjoint; the conventions count input with its
|
|
448
|
+
// cache and output with its reasoning. List cost follows the report step's
|
|
449
|
+
// rule: a subscription's notional cost, a metered run's cash cost.
|
|
450
|
+
function writeUsage(attrs: Attributes, usage: Record<string, unknown>): void {
|
|
451
|
+
const tokens = isRecord(usage.tokens) ? usage.tokens : {};
|
|
452
|
+
attrs.number(
|
|
453
|
+
"gen_ai.usage.input_tokens",
|
|
454
|
+
num(tokens.inputKnown) ??
|
|
455
|
+
sumKnown(
|
|
456
|
+
tokens.inputUncached,
|
|
457
|
+
tokens.inputCacheRead,
|
|
458
|
+
tokens.inputCacheWrite,
|
|
459
|
+
tokens.inputOther,
|
|
460
|
+
),
|
|
461
|
+
);
|
|
462
|
+
attrs.number(
|
|
463
|
+
"gen_ai.usage.output_tokens",
|
|
464
|
+
num(tokens.outputKnown) ??
|
|
465
|
+
sumKnown(
|
|
466
|
+
tokens.outputVisible,
|
|
467
|
+
tokens.outputReasoning,
|
|
468
|
+
tokens.outputOther,
|
|
469
|
+
),
|
|
470
|
+
);
|
|
471
|
+
attrs.number("gen_ai.usage.cache_read.input_tokens", tokens.inputCacheRead);
|
|
472
|
+
attrs.number(
|
|
473
|
+
"gen_ai.usage.cache_creation.input_tokens",
|
|
474
|
+
tokens.inputCacheWrite,
|
|
475
|
+
);
|
|
476
|
+
attrs.number("gen_ai.usage.reasoning.output_tokens", tokens.outputReasoning);
|
|
477
|
+
const listCost =
|
|
478
|
+
usage.billingMode === "subscription"
|
|
479
|
+
? num(usage.notionalCostUsd)
|
|
480
|
+
: usage.billingMode === "metered"
|
|
481
|
+
? num(usage.cashCostUsd)
|
|
482
|
+
: undefined;
|
|
483
|
+
attrs.number("gen_ai.usage.cost", listCost);
|
|
484
|
+
attrs.flatten("crysk.usage", usage);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function stepRole(pipeline: unknown, name: string): string {
|
|
488
|
+
const plan =
|
|
489
|
+
isRecord(pipeline) && isRecord(pipeline.route_plan)
|
|
490
|
+
? pipeline.route_plan
|
|
491
|
+
: undefined;
|
|
492
|
+
const planned = Array.isArray(plan?.steps)
|
|
493
|
+
? plan.steps.filter(isRecord).find((step) => step.stepKey === name)
|
|
494
|
+
: undefined;
|
|
495
|
+
const role = str(planned?.role);
|
|
496
|
+
if (role !== undefined) return role;
|
|
497
|
+
return name.split(/[-.]/)[0] ?? name;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function pipelineSteps(pipeline: unknown): Record<string, unknown>[] {
|
|
501
|
+
return isRecord(pipeline) && Array.isArray(pipeline.steps)
|
|
502
|
+
? pipeline.steps.filter(isRecord)
|
|
503
|
+
: [];
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
interface ParsedAttempt {
|
|
507
|
+
conversation: Conversation;
|
|
508
|
+
// The OpenCode capture, parsed once for the conversation and the transcript;
|
|
509
|
+
// undefined when it does not parse (and is then never sent).
|
|
510
|
+
capture?: unknown;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function parseAttempt(attempt: TraceAttemptInput): ParsedAttempt {
|
|
514
|
+
if (attempt.sessionJsonl !== undefined) {
|
|
515
|
+
return { conversation: parseClaudeSession(attempt.sessionJsonl) };
|
|
516
|
+
}
|
|
517
|
+
if (attempt.openCodeCapture !== undefined) {
|
|
518
|
+
try {
|
|
519
|
+
const capture: unknown = JSON.parse(attempt.openCodeCapture);
|
|
520
|
+
return { conversation: openCodeConversation(capture), capture };
|
|
521
|
+
} catch {
|
|
522
|
+
return { conversation: { input: [], output: [], tools: [] } };
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
if (attempt.transcriptMetadata !== undefined) {
|
|
526
|
+
return { conversation: withoutContent(attempt.transcriptMetadata) };
|
|
527
|
+
}
|
|
528
|
+
return { conversation: { input: [], output: [], tools: [] } };
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// What a metadata trace reads of a conversation: no message, no tool argument
|
|
532
|
+
// or result. encodeTrace keeps these from its content build, so its metadata
|
|
533
|
+
// fallback never parses the transcripts again.
|
|
534
|
+
type MetadataViews = Map<TraceAttemptInput, Conversation>;
|
|
535
|
+
|
|
536
|
+
const metadataView = withoutContent;
|
|
537
|
+
|
|
538
|
+
// Root-level `crysk.*` keys, beside the flattened records.
|
|
539
|
+
function rootAttributes(run: TraceRunInput, content: boolean): Attributes {
|
|
540
|
+
const attrs = new Attributes(content);
|
|
541
|
+
const findings = isRecord(run.findings) ? run.findings : {};
|
|
542
|
+
const cli = isRecord(run.cli) ? run.cli : {};
|
|
543
|
+
const cls = isRecord(cli.cls) ? cli.cls : {};
|
|
544
|
+
attrs.text("gen_ai.operation.name", "invoke_workflow");
|
|
545
|
+
attrs.text("gen_ai.agent.name", "crysk.review");
|
|
546
|
+
attrs.text("crysk.engine.version", run.engineVersion);
|
|
547
|
+
attrs.text("crysk.run.outcome", run.outcome);
|
|
548
|
+
attrs.text("crysk.run.status", str(findings.run_status));
|
|
549
|
+
attrs.text("crysk.run.code", run.code ?? undefined);
|
|
550
|
+
attrs.text("crysk.cli.class", str(cls.kind));
|
|
551
|
+
attrs.number("crysk.pr.number", run.prNumber);
|
|
552
|
+
attrs.text("crysk.head.sha", run.headSha);
|
|
553
|
+
attrs.text("crysk.model.setup", str(findings.model));
|
|
554
|
+
attrs.bool("crysk.trace.content", content);
|
|
555
|
+
attrs.text("crysk.genai.semconv", GENAI_SEMCONV_VERSION);
|
|
556
|
+
if (isRecord(run.pipeline)) {
|
|
557
|
+
// Steps get spans of their own; routePlan and routeFingerprint repeat
|
|
558
|
+
// route_plan and route_fingerprint.
|
|
559
|
+
const rest = { ...run.pipeline };
|
|
560
|
+
for (const key of ["steps", "routePlan", "routeFingerprint"]) {
|
|
561
|
+
delete rest[key];
|
|
562
|
+
}
|
|
563
|
+
attrs.flatten("crysk.pipeline", rest);
|
|
564
|
+
}
|
|
565
|
+
if (isRecord(run.findings)) {
|
|
566
|
+
attrs.flatten(
|
|
567
|
+
"crysk.findings",
|
|
568
|
+
content ? run.findings : metadataFindings(run.findings),
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
if (isRecord(run.cli)) attrs.flatten("crysk.cli", run.cli);
|
|
572
|
+
return attrs;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// findings.json for a metadata trace. A single word can still quote the code (a
|
|
576
|
+
// symbol, a dedupe key, a hop), so each finding keeps only the fields the
|
|
577
|
+
// report step sends for a metadata_only repository, and the debug records
|
|
578
|
+
// (refuted and merged findings, root-cause clusters) become their counts.
|
|
579
|
+
function metadataFindings(
|
|
580
|
+
findings: Record<string, unknown>,
|
|
581
|
+
): Record<string, unknown> {
|
|
582
|
+
const doc: Record<string, unknown> = { ...findings };
|
|
583
|
+
if (Array.isArray(doc.findings)) {
|
|
584
|
+
doc.findings = doc.findings.map(metadataOnlyFinding);
|
|
585
|
+
}
|
|
586
|
+
if (doc.debug !== undefined) doc.debug = countsOf(doc.debug);
|
|
587
|
+
return doc;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// Arrays become their length; numbers and booleans stay; text goes.
|
|
591
|
+
function countsOf(value: unknown): unknown {
|
|
592
|
+
if (Array.isArray(value)) return value.length;
|
|
593
|
+
if (typeof value === "number" || typeof value === "boolean") return value;
|
|
594
|
+
if (!isRecord(value)) return undefined;
|
|
595
|
+
const out: Record<string, unknown> = {};
|
|
596
|
+
for (const [key, item] of Object.entries(value)) {
|
|
597
|
+
const counted = countsOf(item);
|
|
598
|
+
if (counted !== undefined) out[key] = counted;
|
|
599
|
+
}
|
|
600
|
+
return out;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
export function buildTraceBundle(
|
|
604
|
+
run: TraceRunInput,
|
|
605
|
+
content: boolean,
|
|
606
|
+
): TraceBundle {
|
|
607
|
+
return build(run, content, { views: new Map() });
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
interface BuildOptions {
|
|
611
|
+
views: MetadataViews;
|
|
612
|
+
budget?: ContentBudget;
|
|
613
|
+
// Called after each attempt that had a transcript (see encodeTrace).
|
|
614
|
+
settle?: () => void;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
function build(
|
|
618
|
+
run: TraceRunInput,
|
|
619
|
+
content: boolean,
|
|
620
|
+
{ views, budget, settle }: BuildOptions,
|
|
621
|
+
): TraceBundle {
|
|
622
|
+
const seed = `${run.headSha ?? ""}:${run.startedAtMs}`;
|
|
623
|
+
const traceId = hex(`trace:${seed}`, 32);
|
|
624
|
+
const id = (key: string) => hex(`span:${seed}:${key}`, 16);
|
|
625
|
+
const rootId = id("root");
|
|
626
|
+
const runWindow: Window = {};
|
|
627
|
+
widen(runWindow, run.startedAtMs, run.endedAtMs);
|
|
628
|
+
const children: OtlpSpan[] = [];
|
|
629
|
+
const transcripts: TraceTranscript[] = [];
|
|
630
|
+
const metas = pipelineSteps(run.pipeline);
|
|
631
|
+
const metaOf = (name: string) => metas.find((meta) => meta.name === name);
|
|
632
|
+
|
|
633
|
+
// Steps in pipeline order, then any step only its files name.
|
|
634
|
+
const ordered = [
|
|
635
|
+
...metas.map(
|
|
636
|
+
(meta) =>
|
|
637
|
+
run.steps.find((step) => step.name === meta.name) ?? {
|
|
638
|
+
name: String(meta.name),
|
|
639
|
+
attempts: [],
|
|
640
|
+
},
|
|
641
|
+
),
|
|
642
|
+
...run.steps.filter(
|
|
643
|
+
(step) => !metas.some((meta) => meta.name === step.name),
|
|
644
|
+
),
|
|
645
|
+
];
|
|
646
|
+
|
|
647
|
+
const pending: Array<{ span: OtlpSpan; window: Window }> = [];
|
|
648
|
+
for (const step of ordered) {
|
|
649
|
+
const meta = metaOf(step.name);
|
|
650
|
+
const stepId = id(`step:${step.name}`);
|
|
651
|
+
const stepWindow: Window = {};
|
|
652
|
+
const stepSpans: OtlpSpan[] = [];
|
|
653
|
+
const attempts = [...step.attempts].sort((a, b) => a.attempt - b.attempt);
|
|
654
|
+
const finalAttempt = attempts.at(-1)?.attempt;
|
|
655
|
+
for (const attempt of attempts) {
|
|
656
|
+
const facts = attemptFacts(attempt.evidence);
|
|
657
|
+
const known = content ? undefined : views.get(attempt);
|
|
658
|
+
const parsed: ParsedAttempt =
|
|
659
|
+
known === undefined ? parseAttempt(attempt) : { conversation: known };
|
|
660
|
+
const conversation = parsed.conversation;
|
|
661
|
+
if (known === undefined) views.set(attempt, metadataView(conversation));
|
|
662
|
+
const chatId = id(`chat:${step.name}:${attempt.attempt}`);
|
|
663
|
+
const chatWindow: Window = {};
|
|
664
|
+
widen(chatWindow, facts.start, facts.end);
|
|
665
|
+
if (chatWindow.start === undefined) {
|
|
666
|
+
widen(chatWindow, conversation.startMs, conversation.endMs);
|
|
667
|
+
}
|
|
668
|
+
const attrs = new Attributes(content, budget);
|
|
669
|
+
const model = facts.model ?? str(meta?.model);
|
|
670
|
+
attrs.text("gen_ai.operation.name", "chat");
|
|
671
|
+
attrs.text("gen_ai.provider.name", facts.provider);
|
|
672
|
+
attrs.text("gen_ai.request.model", model);
|
|
673
|
+
attrs.text("gen_ai.response.model", conversation.responseModel ?? model);
|
|
674
|
+
if (conversation.finishReason !== undefined) {
|
|
675
|
+
attrs.words("gen_ai.response.finish_reasons", [
|
|
676
|
+
conversation.finishReason,
|
|
677
|
+
]);
|
|
678
|
+
}
|
|
679
|
+
if (facts.usage !== undefined) writeUsage(attrs, facts.usage);
|
|
680
|
+
attrs.text("crysk.step.name", step.name);
|
|
681
|
+
attrs.number("crysk.attempt.number", attempt.attempt);
|
|
682
|
+
attrs.text("crysk.attempt.status", facts.completion);
|
|
683
|
+
const cause = modelCause(meta, model);
|
|
684
|
+
attrs.text("crysk.attempt.cause", cause);
|
|
685
|
+
if (attempt.evidence !== undefined) {
|
|
686
|
+
attrs.flatten("crysk.attempt", attempt.evidence);
|
|
687
|
+
}
|
|
688
|
+
if (content) {
|
|
689
|
+
writeChatContent(attrs, step, attempt, conversation, {
|
|
690
|
+
final: attempt.attempt === finalAttempt,
|
|
691
|
+
completion: facts.completion,
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
const toolSpans = conversation.tools.map((call, index) =>
|
|
695
|
+
toolSpan(call, {
|
|
696
|
+
traceId,
|
|
697
|
+
spanId: id(`tool:${step.name}:${attempt.attempt}:${index}`),
|
|
698
|
+
parentSpanId: chatId,
|
|
699
|
+
content,
|
|
700
|
+
...(budget === undefined ? {} : { budget }),
|
|
701
|
+
fallbackMs: chatWindow.start ?? run.startedAtMs,
|
|
702
|
+
}),
|
|
703
|
+
);
|
|
704
|
+
for (const span of toolSpans) {
|
|
705
|
+
widen(
|
|
706
|
+
chatWindow,
|
|
707
|
+
Number(span.startTimeUnixNano) / 1e6,
|
|
708
|
+
Number(span.endTimeUnixNano) / 1e6,
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
const failed =
|
|
712
|
+
facts.completion !== undefined &&
|
|
713
|
+
facts.completion !== "success" &&
|
|
714
|
+
facts.completion !== "completed";
|
|
715
|
+
const chat: OtlpSpan = {
|
|
716
|
+
traceId,
|
|
717
|
+
spanId: chatId,
|
|
718
|
+
parentSpanId: stepId,
|
|
719
|
+
name: spanName("chat", model),
|
|
720
|
+
kind: SPAN_KIND_CLIENT,
|
|
721
|
+
startTimeUnixNano: "",
|
|
722
|
+
endTimeUnixNano: "",
|
|
723
|
+
attributes: attrs.list,
|
|
724
|
+
status: failed
|
|
725
|
+
? withMessage(
|
|
726
|
+
STATUS_ERROR,
|
|
727
|
+
content ? (cause ?? facts.completion) : undefined,
|
|
728
|
+
)
|
|
729
|
+
: { code: facts.completion === undefined ? STATUS_UNSET : STATUS_OK },
|
|
730
|
+
};
|
|
731
|
+
pending.push({ span: chat, window: chatWindow });
|
|
732
|
+
stepSpans.push(chat, ...toolSpans);
|
|
733
|
+
widen(stepWindow, chatWindow.start, chatWindow.end);
|
|
734
|
+
if (content) {
|
|
735
|
+
transcripts.push(...transcriptsOf(step.name, attempt, parsed, model));
|
|
736
|
+
}
|
|
737
|
+
if (
|
|
738
|
+
known === undefined &&
|
|
739
|
+
(attempt.sessionJsonl !== undefined ||
|
|
740
|
+
attempt.openCodeCapture !== undefined)
|
|
741
|
+
) {
|
|
742
|
+
settle?.();
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
const attrs = new Attributes(content);
|
|
747
|
+
attrs.text("gen_ai.operation.name", "invoke_agent");
|
|
748
|
+
attrs.text("gen_ai.agent.name", step.name);
|
|
749
|
+
attrs.text("crysk.step.role", stepRole(run.pipeline, step.name));
|
|
750
|
+
if (meta !== undefined) attrs.flatten("crysk.step", meta);
|
|
751
|
+
attrs.text("crysk.step.name", step.name);
|
|
752
|
+
attrs.text("crysk.model.effort", str(meta?.effort));
|
|
753
|
+
const status = str(meta?.status);
|
|
754
|
+
const span: OtlpSpan = {
|
|
755
|
+
traceId,
|
|
756
|
+
spanId: stepId,
|
|
757
|
+
parentSpanId: rootId,
|
|
758
|
+
name: spanName("invoke_agent", step.name),
|
|
759
|
+
kind: SPAN_KIND_INTERNAL,
|
|
760
|
+
startTimeUnixNano: "",
|
|
761
|
+
endTimeUnixNano: "",
|
|
762
|
+
attributes: attrs.list,
|
|
763
|
+
status:
|
|
764
|
+
status === "failed"
|
|
765
|
+
? withMessage(STATUS_ERROR, content ? str(meta?.failure) : undefined)
|
|
766
|
+
: { code: status === "ok" ? STATUS_OK : STATUS_UNSET },
|
|
767
|
+
};
|
|
768
|
+
pending.push({ span, window: stepWindow });
|
|
769
|
+
children.push(span, ...stepSpans);
|
|
770
|
+
widen(runWindow, stepWindow.start, stepWindow.end);
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
// A span the run recorded no time for sits at the run's start, zero long.
|
|
774
|
+
const runStart = runWindow.start ?? run.startedAtMs;
|
|
775
|
+
const runEnd = runWindow.end ?? run.endedAtMs;
|
|
776
|
+
for (const { span, window } of pending) {
|
|
777
|
+
span.startTimeUnixNano = nanos(window.start ?? runStart);
|
|
778
|
+
span.endTimeUnixNano = nanos(window.end ?? window.start ?? runStart);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
const failedRun = run.outcome === "error" || run.outcome === "hunters-failed";
|
|
782
|
+
const root: OtlpSpan = {
|
|
783
|
+
traceId,
|
|
784
|
+
spanId: rootId,
|
|
785
|
+
name: spanName("invoke_workflow", "crysk.review"),
|
|
786
|
+
kind: SPAN_KIND_INTERNAL,
|
|
787
|
+
startTimeUnixNano: nanos(runStart),
|
|
788
|
+
endTimeUnixNano: nanos(runEnd),
|
|
789
|
+
attributes: rootAttributes(run, content).list,
|
|
790
|
+
status: failedRun
|
|
791
|
+
? withMessage(
|
|
792
|
+
STATUS_ERROR,
|
|
793
|
+
content ? (run.code ?? run.outcome) : undefined,
|
|
794
|
+
)
|
|
795
|
+
: { code: STATUS_OK },
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
const resource = new Attributes(content);
|
|
799
|
+
resource.text("service.name", "crysk");
|
|
800
|
+
resource.text("service.version", run.engineVersion);
|
|
801
|
+
const version =
|
|
802
|
+
run.engineVersion === undefined ? undefined : redactText(run.engineVersion);
|
|
803
|
+
return {
|
|
804
|
+
schema: TRACE_SCHEMA,
|
|
805
|
+
outcome: run.outcome,
|
|
806
|
+
code: run.code === null ? null : redactText(run.code),
|
|
807
|
+
content,
|
|
808
|
+
truncated: false,
|
|
809
|
+
otlp: {
|
|
810
|
+
resourceSpans: [
|
|
811
|
+
{
|
|
812
|
+
resource: { attributes: resource.list },
|
|
813
|
+
scopeSpans: [
|
|
814
|
+
{
|
|
815
|
+
scope: {
|
|
816
|
+
name: "crysk",
|
|
817
|
+
...(version === undefined ? {} : { version }),
|
|
818
|
+
},
|
|
819
|
+
spans: [root, ...children],
|
|
820
|
+
},
|
|
821
|
+
],
|
|
822
|
+
},
|
|
823
|
+
],
|
|
824
|
+
},
|
|
825
|
+
transcripts,
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
function withMessage(
|
|
830
|
+
code: number,
|
|
831
|
+
message: string | undefined,
|
|
832
|
+
): { code: number; message?: string } {
|
|
833
|
+
return message === undefined
|
|
834
|
+
? { code }
|
|
835
|
+
: { code, message: redactText(message) };
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
// The cause pipeline.json recorded for the model this attempt ran, when that
|
|
839
|
+
// model failed (`model_attempts`, written for a step that tried several).
|
|
840
|
+
function modelCause(
|
|
841
|
+
meta: Record<string, unknown> | undefined,
|
|
842
|
+
model: string | undefined,
|
|
843
|
+
): string | undefined {
|
|
844
|
+
if (meta === undefined || !Array.isArray(meta.model_attempts)) {
|
|
845
|
+
return undefined;
|
|
846
|
+
}
|
|
847
|
+
const entry = meta.model_attempts
|
|
848
|
+
.filter(isRecord)
|
|
849
|
+
.find((item) => item.model === model && item.status === "failed");
|
|
850
|
+
return str(entry?.cause);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
function writeChatContent(
|
|
854
|
+
attrs: Attributes,
|
|
855
|
+
step: TraceStepInput,
|
|
856
|
+
attempt: TraceAttemptInput,
|
|
857
|
+
conversation: Conversation,
|
|
858
|
+
context: { final: boolean; completion?: string },
|
|
859
|
+
): void {
|
|
860
|
+
if (step.systemPrompt !== undefined) {
|
|
861
|
+
attrs.contentJson("gen_ai.system_instructions", [
|
|
862
|
+
{ type: "text", content: step.systemPrompt },
|
|
863
|
+
]);
|
|
864
|
+
}
|
|
865
|
+
const input: ChatMessage[] =
|
|
866
|
+
conversation.input.length > 0
|
|
867
|
+
? conversation.input
|
|
868
|
+
: step.promptBody !== undefined
|
|
869
|
+
? [
|
|
870
|
+
{
|
|
871
|
+
role: "user",
|
|
872
|
+
parts: [{ type: "text", content: step.promptBody }],
|
|
873
|
+
},
|
|
874
|
+
]
|
|
875
|
+
: [];
|
|
876
|
+
if (input.length > 0) {
|
|
877
|
+
attrs.contentJson("gen_ai.input.messages", input);
|
|
878
|
+
}
|
|
879
|
+
// Without a transcript, the final attempt's output file is what it answered.
|
|
880
|
+
const output: ChatMessage[] =
|
|
881
|
+
conversation.output.length > 0
|
|
882
|
+
? conversation.output
|
|
883
|
+
: context.final &&
|
|
884
|
+
step.output !== undefined &&
|
|
885
|
+
attempt.evidence !== undefined
|
|
886
|
+
? [
|
|
887
|
+
{
|
|
888
|
+
role: "assistant",
|
|
889
|
+
parts: [{ type: "text", content: step.output }],
|
|
890
|
+
finish_reason:
|
|
891
|
+
context.completion === "success" ? "stop" : "error",
|
|
892
|
+
},
|
|
893
|
+
]
|
|
894
|
+
: [];
|
|
895
|
+
if (output.length > 0) {
|
|
896
|
+
attrs.contentJson("gen_ai.output.messages", output);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
function toolSpan(
|
|
901
|
+
call: ToolCall,
|
|
902
|
+
context: {
|
|
903
|
+
traceId: string;
|
|
904
|
+
spanId: string;
|
|
905
|
+
parentSpanId: string;
|
|
906
|
+
content: boolean;
|
|
907
|
+
budget?: ContentBudget;
|
|
908
|
+
// Where a call with no recorded time sits: its attempt's start.
|
|
909
|
+
fallbackMs: number;
|
|
910
|
+
},
|
|
911
|
+
): OtlpSpan {
|
|
912
|
+
const attrs = new Attributes(context.content, context.budget);
|
|
913
|
+
attrs.text("gen_ai.operation.name", "execute_tool");
|
|
914
|
+
attrs.text("gen_ai.tool.name", call.name);
|
|
915
|
+
attrs.text("gen_ai.tool.call.id", call.id);
|
|
916
|
+
attrs.bool("crysk.tool.is_error", call.isError === true);
|
|
917
|
+
if (typeof call.arguments === "string") {
|
|
918
|
+
attrs.contentText("gen_ai.tool.call.arguments", call.arguments);
|
|
919
|
+
} else {
|
|
920
|
+
attrs.contentJson("gen_ai.tool.call.arguments", call.arguments);
|
|
921
|
+
}
|
|
922
|
+
attrs.contentText("gen_ai.tool.call.result", call.result);
|
|
923
|
+
const start = call.startMs ?? call.endMs ?? context.fallbackMs;
|
|
924
|
+
const end = call.endMs ?? start;
|
|
925
|
+
return {
|
|
926
|
+
traceId: context.traceId,
|
|
927
|
+
spanId: context.spanId,
|
|
928
|
+
parentSpanId: context.parentSpanId,
|
|
929
|
+
name: spanName("execute_tool", call.name),
|
|
930
|
+
kind: SPAN_KIND_INTERNAL,
|
|
931
|
+
startTimeUnixNano: nanos(start),
|
|
932
|
+
endTimeUnixNano: nanos(Math.max(start, end)),
|
|
933
|
+
attributes: attrs.list,
|
|
934
|
+
status: { code: call.isError === true ? STATUS_ERROR : STATUS_UNSET },
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
function transcriptsOf(
|
|
939
|
+
step: string,
|
|
940
|
+
attempt: TraceAttemptInput,
|
|
941
|
+
parsed: ParsedAttempt,
|
|
942
|
+
model: string | undefined,
|
|
943
|
+
): TraceTranscript[] {
|
|
944
|
+
const base = {
|
|
945
|
+
step: redactText(step),
|
|
946
|
+
attempt: attempt.attempt,
|
|
947
|
+
model: model === undefined ? "" : redactText(model),
|
|
948
|
+
};
|
|
949
|
+
if (attempt.sessionJsonl !== undefined) {
|
|
950
|
+
// The CLI's redacted copy (see REDACTION in the header).
|
|
951
|
+
return [
|
|
952
|
+
{ ...base, kind: "claude-session-jsonl", text: attempt.sessionJsonl },
|
|
953
|
+
];
|
|
954
|
+
}
|
|
955
|
+
// Never sent raw: a capture that does not parse is left out.
|
|
956
|
+
if (parsed.capture !== undefined) {
|
|
957
|
+
return [
|
|
958
|
+
{ ...base, kind: "opencode-capture", text: redactJson(parsed.capture) },
|
|
959
|
+
];
|
|
960
|
+
}
|
|
961
|
+
return [];
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
const SETTLE_EVERY_CHARS = 16 * 1024 * 1024;
|
|
965
|
+
|
|
966
|
+
export type EncodedTrace =
|
|
967
|
+
| {
|
|
968
|
+
ok: true;
|
|
969
|
+
body: Uint8Array;
|
|
970
|
+
bundle: TraceBundle;
|
|
971
|
+
// Transcripts dropped to fit, and whether the content went too.
|
|
972
|
+
droppedTranscripts: number;
|
|
973
|
+
droppedContent: boolean;
|
|
974
|
+
}
|
|
975
|
+
| { ok: false; compressedBytes: number; jsonBytes: number };
|
|
976
|
+
|
|
977
|
+
export interface TraceLimits {
|
|
978
|
+
compressed: number;
|
|
979
|
+
decompressed: number;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
// How the body is compressed, one piece at a time. The pieces are compressed
|
|
983
|
+
// independently and joined in order, so a piece's compressed size is final
|
|
984
|
+
// the moment it is compressed and a piece can be left out without touching
|
|
985
|
+
// the others. The default is gzip (gzipCodec); tests pass an identity codec.
|
|
986
|
+
export interface TraceCodec {
|
|
987
|
+
// Bytes the join adds around the pieces.
|
|
988
|
+
overhead: number;
|
|
989
|
+
piece(bytes: Uint8Array): Uint8Array;
|
|
990
|
+
// The body: the pieces in order, given the uncompressed whole's length and
|
|
991
|
+
// CRC-32.
|
|
992
|
+
join(pieces: Uint8Array[], raw: { length: number; crc: number }): Uint8Array;
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
// One gzip member (RFC 1952) whose deflate stream is the pieces in order. Each
|
|
996
|
+
// piece is raw deflate ending on a sync flush: byte-aligned, with no final
|
|
997
|
+
// block and no reference before its own start, so pieces join by
|
|
998
|
+
// concatenation; an empty final block closes the stream.
|
|
999
|
+
const gzipCodec: TraceCodec = {
|
|
1000
|
+
overhead: 10 + 2 + 8,
|
|
1001
|
+
piece: (bytes) =>
|
|
1002
|
+
deflateRawSync(bytes, { finishFlush: zlibConstants.Z_SYNC_FLUSH }),
|
|
1003
|
+
join(pieces, raw) {
|
|
1004
|
+
const header = Uint8Array.from([0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 0, 255]);
|
|
1005
|
+
const trailer = new Uint8Array(8);
|
|
1006
|
+
const view = new DataView(trailer.buffer);
|
|
1007
|
+
view.setUint32(0, raw.crc >>> 0, true);
|
|
1008
|
+
view.setUint32(4, raw.length >>> 0, true);
|
|
1009
|
+
return Buffer.concat([header, ...pieces, Uint8Array.from([3, 0]), trailer]);
|
|
1010
|
+
},
|
|
1011
|
+
};
|
|
1012
|
+
|
|
1013
|
+
// Fits the bundle under both platform caps (contract: drop transcripts, the
|
|
1014
|
+
// largest first, then the content, and set `truncated`; the OTLP metadata
|
|
1015
|
+
// always survives). `ok: false` when even the metadata does not fit.
|
|
1016
|
+
//
|
|
1017
|
+
// WHY pieces: re-encoding the whole bundle after each dropped transcript grew
|
|
1018
|
+
// with the square of the transcripts, and a runner ran out of memory on eight
|
|
1019
|
+
// 30 MB sessions. Now the OTLP document and each transcript are serialized and
|
|
1020
|
+
// compressed once, as pieces (TraceCodec); both sizes of any choice of
|
|
1021
|
+
// transcripts are then exact sums, and the body joins the chosen pieces. The
|
|
1022
|
+
// bound: every transcript is compressed at most once, the OTLP document at
|
|
1023
|
+
// most twice (with its content, then without), and the body never again.
|
|
1024
|
+
export function encodeTrace(
|
|
1025
|
+
run: TraceRunInput,
|
|
1026
|
+
content: boolean,
|
|
1027
|
+
limits: TraceLimits = {
|
|
1028
|
+
compressed: TRACE_COMPRESSED_MAX_BYTES,
|
|
1029
|
+
decompressed: TRACE_DECOMPRESSED_MAX_BYTES,
|
|
1030
|
+
},
|
|
1031
|
+
codec: TraceCodec = gzipCodec,
|
|
1032
|
+
): EncodedTrace {
|
|
1033
|
+
const views: MetadataViews = new Map();
|
|
1034
|
+
// WHY a collection after each transcript: parsing and redacting a 30 MB
|
|
1035
|
+
// session leaves several times that in garbage, and the runtime let it pile
|
|
1036
|
+
// up across sessions (eight of them peaked at 2.4 GB; collected, 1.6 GB).
|
|
1037
|
+
// Content redacted in between counts too (SETTLE_EVERY_CHARS).
|
|
1038
|
+
const settle = () => Bun.gc(true);
|
|
1039
|
+
// Content strings past the uncompressed cap cannot fit with any transcript
|
|
1040
|
+
// dropped, so the build stops there and the metadata goes (below).
|
|
1041
|
+
let bundle: TraceBundle | undefined;
|
|
1042
|
+
try {
|
|
1043
|
+
bundle = build(run, content, {
|
|
1044
|
+
views,
|
|
1045
|
+
budget: {
|
|
1046
|
+
left: limits.decompressed,
|
|
1047
|
+
settle: { every: SETTLE_EVERY_CHARS, since: 0, run: settle },
|
|
1048
|
+
},
|
|
1049
|
+
settle,
|
|
1050
|
+
});
|
|
1051
|
+
} catch (error) {
|
|
1052
|
+
if (!(error instanceof ContentOverBudget)) throw error;
|
|
1053
|
+
}
|
|
1054
|
+
const total =
|
|
1055
|
+
bundle?.transcripts.length ??
|
|
1056
|
+
run.steps.reduce(
|
|
1057
|
+
(n, step) =>
|
|
1058
|
+
n +
|
|
1059
|
+
step.attempts.filter(
|
|
1060
|
+
(a) =>
|
|
1061
|
+
a.sessionJsonl !== undefined || a.openCodeCapture !== undefined,
|
|
1062
|
+
).length,
|
|
1063
|
+
0,
|
|
1064
|
+
);
|
|
1065
|
+
let failure = { compressedBytes: 0, jsonBytes: 0 };
|
|
1066
|
+
|
|
1067
|
+
if (bundle !== undefined) {
|
|
1068
|
+
const full = bundle;
|
|
1069
|
+
const otlp = pieceOf(codec, full.otlp, limits);
|
|
1070
|
+
if (otlp.ok) {
|
|
1071
|
+
const comma = pieceOf(codec, ",", limits, true);
|
|
1072
|
+
// Largest first, each serialized and compressed once, and only while
|
|
1073
|
+
// it could still fit on its own.
|
|
1074
|
+
const transcripts = full.transcripts
|
|
1075
|
+
.map((transcript, index) => ({
|
|
1076
|
+
transcript,
|
|
1077
|
+
index,
|
|
1078
|
+
piece: pieceOf(codec, transcript, limits),
|
|
1079
|
+
}))
|
|
1080
|
+
.sort((a, b) => b.transcript.text.length - a.transcript.text.length);
|
|
1081
|
+
let dropped = 0;
|
|
1082
|
+
for (; dropped <= transcripts.length; dropped++) {
|
|
1083
|
+
// The smallest survive, sent in the bundle's own order.
|
|
1084
|
+
const kept = transcripts
|
|
1085
|
+
.slice(dropped)
|
|
1086
|
+
.sort((a, b) => a.index - b.index);
|
|
1087
|
+
if (kept.some((item) => !item.piece.ok)) continue;
|
|
1088
|
+
const truncated = dropped > 0;
|
|
1089
|
+
const pieces = [
|
|
1090
|
+
headPiece(codec, full, truncated, limits),
|
|
1091
|
+
otlp,
|
|
1092
|
+
pieceOf(codec, ',"transcripts":[', limits, true),
|
|
1093
|
+
...kept.flatMap((item, i) =>
|
|
1094
|
+
i === 0 ? [item.piece] : [comma, item.piece],
|
|
1095
|
+
),
|
|
1096
|
+
pieceOf(codec, "]}", limits, true),
|
|
1097
|
+
];
|
|
1098
|
+
const joined = join(codec, pieces, limits);
|
|
1099
|
+
if (!joined.ok) {
|
|
1100
|
+
failure = joined;
|
|
1101
|
+
continue;
|
|
1102
|
+
}
|
|
1103
|
+
return {
|
|
1104
|
+
ok: true,
|
|
1105
|
+
body: joined.body,
|
|
1106
|
+
bundle: {
|
|
1107
|
+
...full,
|
|
1108
|
+
truncated,
|
|
1109
|
+
transcripts: kept.map((item) => item.transcript),
|
|
1110
|
+
},
|
|
1111
|
+
droppedTranscripts: dropped,
|
|
1112
|
+
droppedContent: false,
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
} else {
|
|
1116
|
+
failure = otlp;
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
// The content attributes go next, all together; `content` keeps saying what
|
|
1121
|
+
// the engine was told, and `truncated` says why the content is absent.
|
|
1122
|
+
const metadata: TraceBundle = {
|
|
1123
|
+
...build(run, false, { views, settle }),
|
|
1124
|
+
content,
|
|
1125
|
+
truncated: true,
|
|
1126
|
+
};
|
|
1127
|
+
const otlp = pieceOf(codec, metadata.otlp, limits);
|
|
1128
|
+
const joined = otlp.ok
|
|
1129
|
+
? join(
|
|
1130
|
+
codec,
|
|
1131
|
+
[
|
|
1132
|
+
headPiece(codec, metadata, true, limits),
|
|
1133
|
+
otlp,
|
|
1134
|
+
pieceOf(codec, ',"transcripts":[]}', limits, true),
|
|
1135
|
+
],
|
|
1136
|
+
limits,
|
|
1137
|
+
)
|
|
1138
|
+
: otlp;
|
|
1139
|
+
if (!joined.ok) {
|
|
1140
|
+
return {
|
|
1141
|
+
ok: false,
|
|
1142
|
+
compressedBytes: joined.compressedBytes,
|
|
1143
|
+
jsonBytes: Math.max(joined.jsonBytes, failure.jsonBytes),
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
return {
|
|
1147
|
+
ok: true,
|
|
1148
|
+
body: joined.body,
|
|
1149
|
+
bundle: metadata,
|
|
1150
|
+
droppedTranscripts: total,
|
|
1151
|
+
droppedContent: content,
|
|
1152
|
+
};
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
type Piece =
|
|
1156
|
+
| { ok: true; compressed: Uint8Array; length: number; crc: number }
|
|
1157
|
+
| { ok: false; compressedBytes: number; jsonBytes: number };
|
|
1158
|
+
|
|
1159
|
+
// A value as JSON (a string given `literal` is taken as written), compressed.
|
|
1160
|
+
// Serializing or compressing that throws (JSON.stringify's RangeError past the
|
|
1161
|
+
// runtime's string limit, a compression out of memory), or a piece past
|
|
1162
|
+
// either cap alone, cannot be sent.
|
|
1163
|
+
function pieceOf(
|
|
1164
|
+
codec: TraceCodec,
|
|
1165
|
+
value: unknown,
|
|
1166
|
+
limits: TraceLimits,
|
|
1167
|
+
literal = false,
|
|
1168
|
+
): Piece {
|
|
1169
|
+
let length = 0;
|
|
1170
|
+
try {
|
|
1171
|
+
const text = literal ? String(value) : JSON.stringify(value);
|
|
1172
|
+
const bytes = new TextEncoder().encode(text);
|
|
1173
|
+
length = bytes.byteLength;
|
|
1174
|
+
if (length > limits.decompressed) {
|
|
1175
|
+
return {
|
|
1176
|
+
ok: false,
|
|
1177
|
+
compressedBytes: Number.POSITIVE_INFINITY,
|
|
1178
|
+
jsonBytes: length,
|
|
1179
|
+
};
|
|
1180
|
+
}
|
|
1181
|
+
const compressed = codec.piece(bytes);
|
|
1182
|
+
if (compressed.byteLength > limits.compressed) {
|
|
1183
|
+
return {
|
|
1184
|
+
ok: false,
|
|
1185
|
+
compressedBytes: compressed.byteLength,
|
|
1186
|
+
jsonBytes: length,
|
|
1187
|
+
};
|
|
1188
|
+
}
|
|
1189
|
+
return { ok: true, compressed, length, crc: crc32(bytes) };
|
|
1190
|
+
} catch {
|
|
1191
|
+
return {
|
|
1192
|
+
ok: false,
|
|
1193
|
+
compressedBytes: Number.POSITIVE_INFINITY,
|
|
1194
|
+
jsonBytes: length,
|
|
1195
|
+
};
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
// The bundle's fields before `transcripts`, in the order
|
|
1200
|
+
// JSON.stringify(bundle) writes them, up to the OTLP document.
|
|
1201
|
+
function headPiece(
|
|
1202
|
+
codec: TraceCodec,
|
|
1203
|
+
bundle: TraceBundle,
|
|
1204
|
+
truncated: boolean,
|
|
1205
|
+
limits: TraceLimits,
|
|
1206
|
+
): Piece {
|
|
1207
|
+
const head = JSON.stringify({
|
|
1208
|
+
schema: bundle.schema,
|
|
1209
|
+
outcome: bundle.outcome,
|
|
1210
|
+
code: bundle.code,
|
|
1211
|
+
content: bundle.content,
|
|
1212
|
+
truncated,
|
|
1213
|
+
});
|
|
1214
|
+
return pieceOf(codec, `${head.slice(0, -1)},"otlp":`, limits, true);
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
function join(
|
|
1218
|
+
codec: TraceCodec,
|
|
1219
|
+
pieces: Piece[],
|
|
1220
|
+
limits: TraceLimits,
|
|
1221
|
+
):
|
|
1222
|
+
| { ok: true; body: Uint8Array }
|
|
1223
|
+
| { ok: false; compressedBytes: number; jsonBytes: number } {
|
|
1224
|
+
let length = 0;
|
|
1225
|
+
let compressedBytes = codec.overhead;
|
|
1226
|
+
let crc = 0;
|
|
1227
|
+
for (const piece of pieces) {
|
|
1228
|
+
if (!piece.ok) return piece;
|
|
1229
|
+
crc = crc32Combine(crc, piece.crc, piece.length);
|
|
1230
|
+
length += piece.length;
|
|
1231
|
+
compressedBytes += piece.compressed.byteLength;
|
|
1232
|
+
}
|
|
1233
|
+
if (length > limits.decompressed || compressedBytes > limits.compressed) {
|
|
1234
|
+
return { ok: false, compressedBytes, jsonBytes: length };
|
|
1235
|
+
}
|
|
1236
|
+
const body = codec.join(
|
|
1237
|
+
pieces.map((piece) => (piece as { compressed: Uint8Array }).compressed),
|
|
1238
|
+
{ length, crc },
|
|
1239
|
+
);
|
|
1240
|
+
return { ok: true, body };
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
// zlib's crc32_combine: the CRC-32 of A followed by B, from both CRCs and B's
|
|
1244
|
+
// length, by multiplying A's CRC through len(B) zero bytes in GF(2).
|
|
1245
|
+
function gf2Times(matrix: Uint32Array, vector: number): number {
|
|
1246
|
+
let sum = 0;
|
|
1247
|
+
let row = 0;
|
|
1248
|
+
let rest = vector >>> 0;
|
|
1249
|
+
while (rest !== 0) {
|
|
1250
|
+
if (rest & 1) sum ^= matrix[row] ?? 0;
|
|
1251
|
+
rest >>>= 1;
|
|
1252
|
+
row += 1;
|
|
1253
|
+
}
|
|
1254
|
+
return sum >>> 0;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
function gf2Square(out: Uint32Array, matrix: Uint32Array): void {
|
|
1258
|
+
for (let n = 0; n < 32; n++) out[n] = gf2Times(matrix, matrix[n] ?? 0);
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
function crc32Combine(crc1: number, crc2: number, len2: number): number {
|
|
1262
|
+
if (len2 <= 0) return crc1 >>> 0;
|
|
1263
|
+
const even = new Uint32Array(32);
|
|
1264
|
+
const odd = new Uint32Array(32);
|
|
1265
|
+
odd[0] = 0xedb88320;
|
|
1266
|
+
for (let n = 1, row = 1; n < 32; n++, row <<= 1) odd[n] = row;
|
|
1267
|
+
gf2Square(even, odd);
|
|
1268
|
+
gf2Square(odd, even);
|
|
1269
|
+
let crc = crc1 >>> 0;
|
|
1270
|
+
let rest = len2;
|
|
1271
|
+
for (;;) {
|
|
1272
|
+
gf2Square(even, odd);
|
|
1273
|
+
if (rest % 2 === 1) crc = gf2Times(even, crc);
|
|
1274
|
+
rest = Math.floor(rest / 2);
|
|
1275
|
+
if (rest === 0) break;
|
|
1276
|
+
gf2Square(odd, even);
|
|
1277
|
+
if (rest % 2 === 1) crc = gf2Times(odd, crc);
|
|
1278
|
+
rest = Math.floor(rest / 2);
|
|
1279
|
+
if (rest === 0) break;
|
|
1280
|
+
}
|
|
1281
|
+
return (crc ^ crc2) >>> 0;
|
|
1282
|
+
}
|