crysk 0.3.3 → 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.
Files changed (116) hide show
  1. package/README.md +26 -17
  2. package/config/models/anthropic.json +1 -1
  3. package/dist/SKILL-dsnffnfp.md +140 -0
  4. package/dist/SKILL-gnrbph72.md +140 -0
  5. package/dist/SKILL-k4k0zn1w.md +143 -0
  6. package/dist/SKILL-kq0xejn3.md +553 -0
  7. package/dist/SKILL-qaqsbygw.md +66 -0
  8. package/dist/SKILL-vhb4c3vv.md +553 -0
  9. package/dist/SKILL-vws0aqb8.md +553 -0
  10. package/dist/SKILL-xpf6kmdw.md +74 -0
  11. package/dist/ci-admission-0ken6ehx.md +128 -0
  12. package/dist/ci-admission-t0f5qwth.md +128 -0
  13. package/dist/cli.js +3366 -1680
  14. package/dist/index.js +1884 -947
  15. package/dist/opencode-ci-ac5zksdn.md +106 -0
  16. package/dist/workflow-4pkyck01.yml +160 -0
  17. package/package.json +2 -2
  18. package/prompts/default/PROVENANCE.md +1 -0
  19. package/skills/crysk/SKILL.md +16 -16
  20. package/skills/crysk-ci-setup/SKILL.md +42 -39
  21. package/skills/crysk-ci-setup/assets/workflow.yml +31 -22
  22. package/skills/crysk-ci-setup/references/ci-admission.md +2 -2
  23. package/skills/crysk-ci-setup/references/opencode-ci.md +53 -88
  24. package/skills/crysk-loop/SKILL.md +20 -9
  25. package/skills/crysk-pr/SKILL.md +1 -1
  26. package/src/asset-manifest.ts +0 -4
  27. package/src/assets.ts +2 -2
  28. package/src/ci/admission-ledger.ts +4 -0
  29. package/src/ci/classify-error.ts +21 -24
  30. package/src/ci/cli-record.ts +53 -1
  31. package/src/ci/crysk-secrets.ts +409 -0
  32. package/src/ci/gates.ts +31 -42
  33. package/src/ci/outcome.ts +421 -330
  34. package/src/ci/platform-config.ts +127 -0
  35. package/src/ci/platform-report.ts +935 -0
  36. package/src/ci/platform-trace.ts +551 -0
  37. package/src/ci/platform.ts +465 -0
  38. package/src/ci/publish.ts +19 -6
  39. package/src/ci/reporter.ts +9 -7
  40. package/src/ci/review-admission.ts +8 -4
  41. package/src/ci/review-levels.ts +8 -7
  42. package/src/ci/run.ts +88 -0
  43. package/src/ci/setup.ts +547 -109
  44. package/src/ci/trace-bundle.ts +1282 -0
  45. package/src/ci/trace-transcripts.ts +415 -0
  46. package/src/ci/trigger.ts +176 -33
  47. package/src/cli.ts +11 -3
  48. package/src/commands/ci-setup-questions.ts +3 -3
  49. package/src/commands/ci-setup.ts +152 -6
  50. package/src/commands/config.ts +3 -0
  51. package/src/commands/doctor.ts +15 -6
  52. package/src/compare/loop.ts +9 -1
  53. package/src/compare/martian-adapter.ts +1 -1
  54. package/src/config/config.ts +16 -3
  55. package/src/diversity/clustering.ts +0 -1
  56. package/src/diversity/identity.ts +4 -5
  57. package/src/diversity/pipeline-integration.ts +1 -9
  58. package/src/doctor.ts +3 -3
  59. package/src/execution/attempt-evidence.ts +10 -5
  60. package/src/execution/claude-session-trace.ts +480 -0
  61. package/src/execution/contracts.ts +21 -4
  62. package/src/execution/harness.ts +105 -8
  63. package/src/execution/model-fallback.ts +239 -0
  64. package/src/execution/usage-normalized.ts +6 -4
  65. package/src/home-preflight.ts +1 -1
  66. package/src/index.ts +1 -1
  67. package/src/model/catalog.ts +13 -37
  68. package/src/model/free-discovery.ts +117 -8
  69. package/src/model/model-name.ts +404 -0
  70. package/src/model/provider-capabilities.ts +22 -7
  71. package/src/model/routing.ts +346 -467
  72. package/src/pr/admission.ts +71 -11
  73. package/src/pr/ci-admission-gate.ts +2 -1
  74. package/src/pr/finding-state-record.ts +58 -0
  75. package/src/pr/gate-record.ts +22 -0
  76. package/src/pr/inline.ts +3 -0
  77. package/src/pr/plan.ts +20 -4
  78. package/src/pr/posting.ts +4 -0
  79. package/src/pr/pr-status.ts +27 -7
  80. package/src/pr/pr.ts +39 -8
  81. package/src/pr/preflight.ts +44 -2
  82. package/src/pr/publish-outcome.ts +11 -3
  83. package/src/pr/review-pr.ts +20 -4
  84. package/src/pr/target.ts +12 -3
  85. package/src/production-runtime.ts +760 -331
  86. package/src/rereview/classify.ts +4 -0
  87. package/src/rereview/prepare.ts +47 -22
  88. package/src/rereview/state.ts +34 -8
  89. package/src/review/pipeline.ts +277 -101
  90. package/src/review/preflight.ts +201 -294
  91. package/src/review/report.ts +60 -40
  92. package/src/review/review.ts +20 -3
  93. package/src/review/route-preflight.ts +79 -11
  94. package/src/review/run.ts +28 -6
  95. package/src/review/scout.ts +1 -1
  96. package/src/review/spec.ts +3 -3
  97. package/src/review/step-runner.ts +70 -1
  98. package/src/runner-authority.ts +127 -9
  99. package/src/security/binding-policy.ts +1 -2
  100. package/src/security/claude-credential.ts +355 -0
  101. package/src/security/credential-broker.ts +56 -40
  102. package/src/security/evidence-redaction.ts +179 -4
  103. package/src/security/opencode-secret-manifest.ts +114 -0
  104. package/src/security/published-text.ts +161 -0
  105. package/src/security/redact.ts +45 -0
  106. package/src/system-tools.ts +48 -21
  107. package/src/transport-registry.ts +12 -2
  108. package/src/transports/claude-code-cli.ts +21 -2
  109. package/src/transports/opencode-sdk.ts +11 -9
  110. package/src/transports/opencode-server.ts +7 -10
  111. package/src/triage/triage.ts +2 -1
  112. package/src/ui/config-edit.ts +9 -20
  113. package/src/ui/config.ts +32 -27
  114. package/src/ui/plan.ts +76 -47
  115. package/src/updater.ts +10 -7
  116. package/prompts/default/deep-review-lifecycle.md +0 -362
@@ -0,0 +1,415 @@
1
+ // Reads one attempt's raw transcript into the conversation a review trace
2
+ // shows (src/ci/trace-bundle.ts): the prompt the model got, what it answered,
3
+ // and each tool call with its arguments, result and timing.
4
+ //
5
+ // Two transcripts exist. A Claude CLI attempt leaves its session JSONL
6
+ // (steps/trace.<step>.attempt<n>.jsonl, copied and redacted by
7
+ // src/execution/claude-session-trace.ts). An OpenCode attempt leaves its
8
+ // HTTP/SSE capture (steps/evidence.<step>.attempt<n>.capture.json, written by
9
+ // src/execution/attempt-evidence.ts), whose last `readback` record holds the
10
+ // session's messages.
11
+ //
12
+ // WHY every read is best effort: these files come from CLIs this engine does
13
+ // not own, and a format change must cost the conversation, never the trace. A
14
+ // line or record that does not have the expected shape is skipped.
15
+ //
16
+ // The message parts follow the OpenTelemetry GenAI message schema (TextPart,
17
+ // ReasoningPart, ToolCallRequestPart; see GENAI_SEMCONV_VERSION in
18
+ // trace-bundle.ts).
19
+ //
20
+ // BUILTINS ONLY: the trace step runs under always(), so it can run before
21
+ // `bun install` (test/ci/platform-trace.test.ts walks the import closure).
22
+
23
+ import { closeSync, openSync, readSync } from "node:fs";
24
+
25
+ export type MessagePart =
26
+ | { type: "text"; content: string }
27
+ | { type: "reasoning"; content: string }
28
+ | { type: "tool_call"; id?: string; name: string; arguments?: unknown };
29
+
30
+ export interface ChatMessage {
31
+ role: "user" | "assistant";
32
+ parts: MessagePart[];
33
+ finish_reason?: string;
34
+ }
35
+
36
+ export interface ToolCall {
37
+ id?: string;
38
+ name: string;
39
+ arguments?: unknown;
40
+ result?: string;
41
+ isError?: boolean;
42
+ startMs?: number;
43
+ endMs?: number;
44
+ }
45
+
46
+ export interface Conversation {
47
+ input: ChatMessage[];
48
+ output: ChatMessage[];
49
+ tools: ToolCall[];
50
+ // The model the provider says answered, when the transcript names one.
51
+ responseModel?: string;
52
+ // The last answer's finish reason, as the provider spelled it.
53
+ finishReason?: string;
54
+ startMs?: number;
55
+ endMs?: number;
56
+ }
57
+
58
+ function isRecord(value: unknown): value is Record<string, unknown> {
59
+ return typeof value === "object" && value !== null && !Array.isArray(value);
60
+ }
61
+
62
+ function timeOf(value: unknown): number | undefined {
63
+ if (typeof value === "number" && Number.isFinite(value) && value > 0) {
64
+ return value;
65
+ }
66
+ if (typeof value !== "string") return undefined;
67
+ const ms = Date.parse(value);
68
+ return Number.isFinite(ms) ? ms : undefined;
69
+ }
70
+
71
+ function nonEmpty(value: unknown): string | undefined {
72
+ return typeof value === "string" && value !== "" ? value : undefined;
73
+ }
74
+
75
+ // A tool result's content: a string, or the text blocks of a block list.
76
+ function resultText(content: unknown): string | undefined {
77
+ if (typeof content === "string") return content;
78
+ if (!Array.isArray(content)) return undefined;
79
+ const texts = content
80
+ .filter(isRecord)
81
+ .map((block) => (typeof block.text === "string" ? block.text : undefined))
82
+ .filter((text): text is string => text !== undefined);
83
+ return texts.length > 0 ? texts.join("\n") : undefined;
84
+ }
85
+
86
+ function widen(conversation: Conversation, ms: number | undefined): void {
87
+ if (ms === undefined) return;
88
+ if (conversation.startMs === undefined || ms < conversation.startMs) {
89
+ conversation.startMs = ms;
90
+ }
91
+ if (conversation.endMs === undefined || ms > conversation.endMs) {
92
+ conversation.endMs = ms;
93
+ }
94
+ }
95
+
96
+ // The Claude CLI writes one line per content block, so the blocks of one
97
+ // answer share its message id and are joined back into one message here.
98
+ export function parseClaudeSession(text: string): Conversation {
99
+ const reader = new ClaudeSessionReader(true);
100
+ for (const line of text.split("\n")) reader.line(line);
101
+ return reader.conversation;
102
+ }
103
+
104
+ // What a metadata trace keeps of a conversation: the tool calls' names, ids,
105
+ // times and status, the finish reason, the response model and the times. No
106
+ // message, no tool argument and no tool result.
107
+ export function withoutContent(conversation: Conversation): Conversation {
108
+ return {
109
+ ...conversation,
110
+ input: [],
111
+ output: [],
112
+ tools: conversation.tools.map(
113
+ ({ arguments: _a, result: _r, ...call }) => call,
114
+ ),
115
+ };
116
+ }
117
+
118
+ export interface ScanLimits {
119
+ // Bytes read from the file at a time.
120
+ chunkBytes: number;
121
+ // A longer line is skipped without being held whole.
122
+ lineBytes: number;
123
+ }
124
+
125
+ // The session copy's own line cap (MAX_LINE_BYTES in
126
+ // src/execution/claude-session-trace.ts): the copy omits a longer line, so a
127
+ // line past it is no line the copy wrote.
128
+ const SCAN_LIMITS: ScanLimits = {
129
+ chunkBytes: 1024 * 1024,
130
+ lineBytes: 4 * 1024 * 1024,
131
+ };
132
+ const NEWLINE = 0x0a;
133
+
134
+ // A metadata trace's read of a Claude session file: the same reducer as
135
+ // parseClaudeSession, fed one line at a time and keeping no content.
136
+ //
137
+ // WHY it streams: a session can reach 64 MiB and a run holds one per attempt;
138
+ // reading each whole only to drop its text cost a metadata trace memory it
139
+ // never sends (eight 30 MB sessions held several hundred MB). Here the file is
140
+ // read in chunks, only the current line is held (at most lineBytes), and each
141
+ // line is parsed and dropped before the next. undefined when the file cannot
142
+ // be opened; a read error keeps what was read before it.
143
+ export function scanClaudeSessionFile(
144
+ file: string,
145
+ limits: ScanLimits = SCAN_LIMITS,
146
+ ): Conversation | undefined {
147
+ let fd: number;
148
+ try {
149
+ fd = openSync(file, "r");
150
+ } catch {
151
+ return undefined;
152
+ }
153
+ const reader = new ClaudeSessionReader(false);
154
+ const buffer = Buffer.allocUnsafe(limits.chunkBytes);
155
+ // The current line's bytes from earlier chunks, copied out of the buffer the
156
+ // next read overwrites.
157
+ let pending: Buffer[] = [];
158
+ let pendingBytes = 0;
159
+ let overLimit = false;
160
+ const hold = (bytes: Buffer): void => {
161
+ if (overLimit || bytes.length === 0) return;
162
+ if (pendingBytes + bytes.length > limits.lineBytes) {
163
+ overLimit = true;
164
+ pending = [];
165
+ pendingBytes = 0;
166
+ return;
167
+ }
168
+ pending.push(Buffer.from(bytes));
169
+ pendingBytes += bytes.length;
170
+ };
171
+ // Decoded only once whole, so a character split across chunks stays whole.
172
+ const finishLine = (tail: Buffer): void => {
173
+ if (!overLimit) {
174
+ if (pendingBytes === 0) {
175
+ if (tail.length <= limits.lineBytes) reader.line(tail.toString("utf8"));
176
+ } else if (pendingBytes + tail.length <= limits.lineBytes) {
177
+ reader.line(
178
+ Buffer.concat(
179
+ [...pending, tail],
180
+ pendingBytes + tail.length,
181
+ ).toString("utf8"),
182
+ );
183
+ }
184
+ }
185
+ pending = [];
186
+ pendingBytes = 0;
187
+ overLimit = false;
188
+ };
189
+ try {
190
+ for (;;) {
191
+ const read = readSync(fd, buffer, 0, limits.chunkBytes, null);
192
+ if (read === 0) break;
193
+ const chunk = buffer.subarray(0, read);
194
+ let start = 0;
195
+ for (
196
+ let at = chunk.indexOf(NEWLINE);
197
+ at !== -1;
198
+ at = chunk.indexOf(NEWLINE, start)
199
+ ) {
200
+ finishLine(chunk.subarray(start, at));
201
+ start = at + 1;
202
+ }
203
+ hold(chunk.subarray(start));
204
+ }
205
+ } catch {
206
+ // Best effort: what was read stands.
207
+ } finally {
208
+ closeSync(fd);
209
+ }
210
+ finishLine(Buffer.alloc(0));
211
+ return reader.conversation;
212
+ }
213
+
214
+ // One session line at a time. Without content it keeps only what
215
+ // withoutContent would leave: no message, thinking, tool argument or result.
216
+ class ClaudeSessionReader {
217
+ readonly conversation: Conversation = { input: [], output: [], tools: [] };
218
+ private readonly byId = new Map<string, ToolCall>();
219
+ private current: { id: string | undefined; message: ChatMessage } | undefined;
220
+
221
+ constructor(private readonly content: boolean) {}
222
+
223
+ line(line: string): void {
224
+ if (line.trim() === "") return;
225
+ let entry: unknown;
226
+ try {
227
+ entry = JSON.parse(line);
228
+ } catch {
229
+ return;
230
+ }
231
+ if (!isRecord(entry) || !isRecord(entry.message)) return;
232
+ const at = timeOf(entry.timestamp);
233
+ if (entry.type === "assistant") this.assistant(entry.message, at);
234
+ else if (entry.type === "user") this.user(entry.message, at);
235
+ }
236
+
237
+ private assistant(message: Record<string, unknown>, at?: number): void {
238
+ const conversation = this.conversation;
239
+ widen(conversation, at);
240
+ const id = nonEmpty(message.id);
241
+ if (
242
+ this.current === undefined ||
243
+ id === undefined ||
244
+ this.current.id !== id
245
+ ) {
246
+ this.current = { id, message: { role: "assistant", parts: [] } };
247
+ if (this.content) conversation.output.push(this.current.message);
248
+ }
249
+ const current = this.current.message;
250
+ const model = nonEmpty(message.model);
251
+ if (model !== undefined) conversation.responseModel = model;
252
+ const stop = nonEmpty(message.stop_reason);
253
+ if (stop !== undefined) {
254
+ current.finish_reason = stop;
255
+ conversation.finishReason = stop;
256
+ }
257
+ for (const block of Array.isArray(message.content)
258
+ ? message.content.filter(isRecord)
259
+ : []) {
260
+ const part = claudePart(block);
261
+ if (part === undefined) continue;
262
+ if (this.content) current.parts.push(part);
263
+ if (part.type === "tool_call") {
264
+ const call: ToolCall = {
265
+ ...(part.id === undefined ? {} : { id: part.id }),
266
+ name: part.name,
267
+ ...(part.arguments === undefined || !this.content
268
+ ? {}
269
+ : { arguments: part.arguments }),
270
+ ...(at === undefined ? {} : { startMs: at }),
271
+ };
272
+ conversation.tools.push(call);
273
+ if (part.id !== undefined) this.byId.set(part.id, call);
274
+ }
275
+ }
276
+ }
277
+
278
+ private user(message: Record<string, unknown>, at?: number): void {
279
+ const conversation = this.conversation;
280
+ widen(conversation, at);
281
+ this.current = undefined;
282
+ const content = message.content;
283
+ if (typeof content === "string") {
284
+ if (this.content && conversation.output.length === 0) {
285
+ conversation.input.push(userText(content));
286
+ }
287
+ return;
288
+ }
289
+ if (!Array.isArray(content)) return;
290
+ for (const block of content.filter(isRecord)) {
291
+ if (block.type === "tool_result") {
292
+ const call = this.byId.get(String(block.tool_use_id));
293
+ if (call === undefined) continue;
294
+ if (this.content) {
295
+ const result = resultText(block.content);
296
+ if (result !== undefined) call.result = result;
297
+ }
298
+ if (block.is_error === true) call.isError = true;
299
+ if (at !== undefined) call.endMs = at;
300
+ } else if (
301
+ this.content &&
302
+ block.type === "text" &&
303
+ typeof block.text === "string" &&
304
+ conversation.output.length === 0
305
+ ) {
306
+ conversation.input.push(userText(block.text));
307
+ }
308
+ }
309
+ }
310
+ }
311
+
312
+ function userText(content: string): ChatMessage {
313
+ return { role: "user", parts: [{ type: "text", content }] };
314
+ }
315
+
316
+ function claudePart(block: Record<string, unknown>): MessagePart | undefined {
317
+ if (block.type === "text" && typeof block.text === "string") {
318
+ return { type: "text", content: block.text };
319
+ }
320
+ // The CLI can record a thinking block with its text withheld; an empty one
321
+ // says nothing and is left out.
322
+ if (block.type === "thinking" && nonEmpty(block.thinking) !== undefined) {
323
+ return { type: "reasoning", content: block.thinking as string };
324
+ }
325
+ if (block.type === "tool_use" && typeof block.name === "string") {
326
+ return {
327
+ type: "tool_call",
328
+ ...(nonEmpty(block.id) === undefined ? {} : { id: block.id as string }),
329
+ name: block.name,
330
+ ...(block.input === undefined ? {} : { arguments: block.input }),
331
+ };
332
+ }
333
+ return undefined;
334
+ }
335
+
336
+ // The capture's newest readback: the session's messages as OpenCode returned
337
+ // them, `{ info: { role, modelID, finish, time }, parts: [...] }` each. An
338
+ // absent or elided readback leaves the conversation empty. The capture comes
339
+ // parsed: the trace builder parses each one once, for its conversation and its
340
+ // redacted transcript.
341
+ export function openCodeConversation(capture: unknown): Conversation {
342
+ const conversation: Conversation = { input: [], output: [], tools: [] };
343
+ const records =
344
+ isRecord(capture) && Array.isArray(capture.records)
345
+ ? capture.records.filter(isRecord)
346
+ : [];
347
+ for (const record of records) widen(conversation, timeOf(record.observedMs));
348
+ const readback = records
349
+ .filter(
350
+ (record) =>
351
+ record.kind === "readback" &&
352
+ isRecord(record.data) &&
353
+ Array.isArray(record.data.messages),
354
+ )
355
+ .at(-1);
356
+ const messages =
357
+ readback !== undefined && isRecord(readback.data)
358
+ ? (readback.data.messages as unknown[]).filter(isRecord)
359
+ : [];
360
+ for (const item of messages) {
361
+ const info = isRecord(item.info) ? item.info : item;
362
+ const parts = Array.isArray(item.parts) ? item.parts.filter(isRecord) : [];
363
+ if (info.role === "user") {
364
+ const texts = parts
365
+ .filter((part) => part.type === "text" && typeof part.text === "string")
366
+ .map((part) => ({ type: "text" as const, content: String(part.text) }));
367
+ if (texts.length > 0)
368
+ conversation.input.push({ role: "user", parts: texts });
369
+ continue;
370
+ }
371
+ if (info.role !== "assistant") continue;
372
+ const message: ChatMessage = { role: "assistant", parts: [] };
373
+ const model = nonEmpty(info.modelID);
374
+ if (model !== undefined) conversation.responseModel = model;
375
+ const finish = nonEmpty(info.finish);
376
+ if (finish !== undefined) {
377
+ message.finish_reason = finish;
378
+ conversation.finishReason = finish;
379
+ }
380
+ for (const part of parts) {
381
+ if (part.type === "text" && typeof part.text === "string") {
382
+ message.parts.push({ type: "text", content: part.text });
383
+ } else if (part.type === "reasoning" && nonEmpty(part.text)) {
384
+ message.parts.push({ type: "reasoning", content: String(part.text) });
385
+ } else if (part.type === "tool" && typeof part.tool === "string") {
386
+ const state = isRecord(part.state) ? part.state : {};
387
+ const time = isRecord(state.time) ? state.time : {};
388
+ const id = nonEmpty(part.callID);
389
+ const call: ToolCall = {
390
+ ...(id === undefined ? {} : { id }),
391
+ name: part.tool,
392
+ ...(state.input === undefined ? {} : { arguments: state.input }),
393
+ };
394
+ const result = nonEmpty(state.output) ?? nonEmpty(state.error);
395
+ if (result !== undefined) call.result = result;
396
+ if (state.status === "error") call.isError = true;
397
+ const start = timeOf(time.start);
398
+ const end = timeOf(time.end);
399
+ if (start !== undefined) call.startMs = start;
400
+ if (end !== undefined) call.endMs = end;
401
+ conversation.tools.push(call);
402
+ message.parts.push({
403
+ type: "tool_call",
404
+ ...(id === undefined ? {} : { id }),
405
+ name: part.tool,
406
+ ...(call.arguments === undefined
407
+ ? {}
408
+ : { arguments: call.arguments }),
409
+ });
410
+ }
411
+ }
412
+ conversation.output.push(message);
413
+ }
414
+ return conversation;
415
+ }